heaptrc.pp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. Heap tracer
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit heaptrc;
  12. interface
  13. {$inline on}
  14. {$ifdef FPC_HEAPTRC_EXTRA}
  15. {$define EXTRA}
  16. {$endif FPC_HEAPTRC_EXTRA}
  17. {$checkpointer off}
  18. {$goto on}
  19. {$if defined(win32) or defined(wince)}
  20. {$define windows}
  21. {$endif}
  22. Procedure DumpHeap;
  23. { define EXTRA to add more
  24. tests :
  25. - keep all memory after release and
  26. check by CRC value if not changed after release
  27. WARNING this needs extremely much memory (PM) }
  28. type
  29. tFillExtraInfoProc = procedure(p : pointer);
  30. tdisplayextrainfoProc = procedure (var ptext : text;p : pointer);
  31. { Allows to add info pre memory block, see ppheap.pas of the compiler
  32. for example source }
  33. procedure SetHeapExtraInfo( size : ptrint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  34. { Redirection of the output to a file }
  35. procedure SetHeapTraceOutput(const name : string);
  36. const
  37. { tracing level
  38. splitted in two if memory is released !! }
  39. {$ifdef EXTRA}
  40. tracesize = 16;
  41. {$else EXTRA}
  42. tracesize = 8;
  43. {$endif EXTRA}
  44. { install heaptrc memorymanager }
  45. useheaptrace : boolean=true;
  46. { less checking }
  47. quicktrace : boolean=true;
  48. { calls halt() on error by default !! }
  49. HaltOnError : boolean = true;
  50. { Halt on exit if any memory was not freed }
  51. HaltOnNotReleased : boolean = false;
  52. { set this to true if you suspect that memory
  53. is freed several times }
  54. {$ifdef EXTRA}
  55. keepreleased : boolean=true;
  56. {$else EXTRA}
  57. keepreleased : boolean=false;
  58. {$endif EXTRA}
  59. { add a small footprint at the end of memory blocks, this
  60. can check for memory overwrites at the end of a block }
  61. add_tail : boolean = true;
  62. { put crc in sig
  63. this allows to test for writing into that part }
  64. usecrc : boolean = true;
  65. implementation
  66. type
  67. pptrint = ^ptrint;
  68. const
  69. { allows to add custom info in heap_mem_info, this is the size that will
  70. be allocated for this information }
  71. extra_info_size : ptrint = 0;
  72. exact_info_size : ptrint = 0;
  73. EntryMemUsed : ptrint = 0;
  74. { function to fill this info up }
  75. fill_extra_info_proc : TFillExtraInfoProc = nil;
  76. display_extra_info_proc : TDisplayExtraInfoProc = nil;
  77. error_in_heap : boolean = false;
  78. inside_trace_getmem : boolean = false;
  79. { indicates where the output will be redirected }
  80. { only set using environment variables }
  81. outputstr : shortstring = '';
  82. type
  83. pheap_extra_info = ^theap_extra_info;
  84. theap_extra_info = record
  85. check : cardinal; { used to check if the procvar is still valid }
  86. fillproc : tfillextrainfoProc;
  87. displayproc : tdisplayextrainfoProc;
  88. data : record
  89. end;
  90. end;
  91. { warning the size of theap_mem_info
  92. must be a multiple of 8
  93. because otherwise you will get
  94. problems when releasing the usual memory part !!
  95. sizeof(theap_mem_info = 16+tracesize*4 so
  96. tracesize must be even !! PM }
  97. pheap_mem_info = ^theap_mem_info;
  98. theap_mem_info = record
  99. previous,
  100. next : pheap_mem_info;
  101. size : ptrint;
  102. sig : longword;
  103. {$ifdef EXTRA}
  104. release_sig : longword;
  105. prev_valid : pheap_mem_info;
  106. {$endif EXTRA}
  107. calls : array [1..tracesize] of pointer;
  108. exact_info_size : word;
  109. extra_info_size : word;
  110. extra_info : pheap_extra_info;
  111. end;
  112. var
  113. useownfile : boolean;
  114. ownfile : text;
  115. {$ifdef EXTRA}
  116. error_file : text;
  117. heap_valid_first,
  118. heap_valid_last : pheap_mem_info;
  119. {$endif EXTRA}
  120. heap_mem_root : pheap_mem_info;
  121. getmem_cnt,
  122. freemem_cnt : ptrint;
  123. getmem_size,
  124. freemem_size : ptrint;
  125. getmem8_size,
  126. freemem8_size : ptrint;
  127. {*****************************************************************************
  128. Crc 32
  129. *****************************************************************************}
  130. var
  131. Crc32Tbl : array[0..255] of longword;
  132. procedure MakeCRC32Tbl;
  133. var
  134. crc : longword;
  135. i,n : byte;
  136. begin
  137. for i:=0 to 255 do
  138. begin
  139. crc:=i;
  140. for n:=1 to 8 do
  141. if odd(crc) then
  142. crc:=(crc shr 1) xor $edb88320
  143. else
  144. crc:=crc shr 1;
  145. Crc32Tbl[i]:=crc;
  146. end;
  147. end;
  148. Function UpdateCrc32(InitCrc:longword;var InBuf;InLen:ptrint):longword;
  149. var
  150. i : ptrint;
  151. p : pchar;
  152. begin
  153. p:=@InBuf;
  154. for i:=1 to InLen do
  155. begin
  156. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  157. inc(p);
  158. end;
  159. UpdateCrc32:=InitCrc;
  160. end;
  161. Function calculate_sig(p : pheap_mem_info) : longword;
  162. var
  163. crc : longword;
  164. pl : pptrint;
  165. begin
  166. crc:=cardinal($ffffffff);
  167. crc:=UpdateCrc32(crc,p^.size,sizeof(ptrint));
  168. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(ptrint));
  169. if p^.extra_info_size>0 then
  170. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  171. if add_tail then
  172. begin
  173. { Check also 4 bytes just after allocation !! }
  174. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size;
  175. crc:=UpdateCrc32(crc,pl^,sizeof(ptrint));
  176. end;
  177. calculate_sig:=crc;
  178. end;
  179. {$ifdef EXTRA}
  180. Function calculate_release_sig(p : pheap_mem_info) : longword;
  181. var
  182. crc : longword;
  183. pl : pptrint;
  184. begin
  185. crc:=$ffffffff;
  186. crc:=UpdateCrc32(crc,p^.size,sizeof(ptrint));
  187. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(ptrint));
  188. if p^.extra_info_size>0 then
  189. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  190. { Check the whole of the whole allocation }
  191. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info);
  192. crc:=UpdateCrc32(crc,pl^,p^.size);
  193. { Check also 4 bytes just after allocation !! }
  194. if add_tail then
  195. begin
  196. { Check also 4 bytes just after allocation !! }
  197. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size;
  198. crc:=UpdateCrc32(crc,pl^,sizeof(ptrint));
  199. end;
  200. calculate_release_sig:=crc;
  201. end;
  202. {$endif EXTRA}
  203. {*****************************************************************************
  204. Helpers
  205. *****************************************************************************}
  206. procedure call_stack(pp : pheap_mem_info;var ptext : text);
  207. var
  208. i : ptrint;
  209. begin
  210. writeln(ptext,'Call trace for block $',hexstr(ptrint(pointer(pp)+sizeof(theap_mem_info)),2*sizeof(pointer)),' size ',pp^.size);
  211. for i:=1 to tracesize do
  212. if pp^.calls[i]<>nil then
  213. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  214. { the check is done to be sure that the procvar is not overwritten }
  215. if assigned(pp^.extra_info) and
  216. (pp^.extra_info^.check=$12345678) and
  217. assigned(pp^.extra_info^.displayproc) then
  218. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  219. end;
  220. procedure call_free_stack(pp : pheap_mem_info;var ptext : text);
  221. var
  222. i : ptrint;
  223. begin
  224. writeln(ptext,'Call trace for block at $',hexstr(ptrint(pointer(pp)+sizeof(theap_mem_info)),2*sizeof(pointer)),' size ',pp^.size);
  225. for i:=1 to tracesize div 2 do
  226. if pp^.calls[i]<>nil then
  227. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  228. writeln(ptext,' was released at ');
  229. for i:=(tracesize div 2)+1 to tracesize do
  230. if pp^.calls[i]<>nil then
  231. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  232. { the check is done to be sure that the procvar is not overwritten }
  233. if assigned(pp^.extra_info) and
  234. (pp^.extra_info^.check=$12345678) and
  235. assigned(pp^.extra_info^.displayproc) then
  236. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  237. end;
  238. procedure dump_already_free(p : pheap_mem_info;var ptext : text);
  239. begin
  240. Writeln(ptext,'Marked memory at $',HexStr(ptrint(pointer(p)+sizeof(theap_mem_info)),2*sizeof(pointer)),' released');
  241. call_free_stack(p,ptext);
  242. Writeln(ptext,'freed again at');
  243. dump_stack(ptext,get_caller_frame(get_frame));
  244. end;
  245. procedure dump_error(p : pheap_mem_info;var ptext : text);
  246. begin
  247. Writeln(ptext,'Marked memory at $',HexStr(ptrint(pointer(p)+sizeof(theap_mem_info)),2*sizeof(pointer)),' invalid');
  248. Writeln(ptext,'Wrong signature $',hexstr(p^.sig,8),' instead of ',hexstr(calculate_sig(p),8));
  249. dump_stack(ptext,get_caller_frame(get_frame));
  250. end;
  251. {$ifdef EXTRA}
  252. procedure dump_change_after(p : pheap_mem_info;var ptext : text);
  253. var pp : pchar;
  254. i : ptrint;
  255. begin
  256. Writeln(ptext,'Marked memory at $',HexStr(ptrint(pointer(p)+sizeof(theap_mem_info)),2*sizeof(pointer)),' invalid');
  257. Writeln(ptext,'Wrong release CRC $',hexstr(p^.release_sig,8),' instead of ',hexstr(calculate_release_sig(p),8));
  258. Writeln(ptext,'This memory was changed after call to freemem !');
  259. call_free_stack(p,ptext);
  260. pp:=pointer(p)+sizeof(theap_mem_info);
  261. for i:=0 to p^.size-1 do
  262. if byte(pp[i])<>$F0 then
  263. Writeln(ptext,'offset',i,':$',hexstr(i,2*sizeof(pointer)),'"',pp[i],'"');
  264. end;
  265. {$endif EXTRA}
  266. procedure dump_wrong_size(p : pheap_mem_info;size : ptrint;var ptext : text);
  267. begin
  268. Writeln(ptext,'Marked memory at $',HexStr(ptrint(pointer(p)+sizeof(theap_mem_info)),2*sizeof(pointer)),' invalid');
  269. Writeln(ptext,'Wrong size : ',p^.size,' allocated ',size,' freed');
  270. dump_stack(ptext,get_caller_frame(get_frame));
  271. { the check is done to be sure that the procvar is not overwritten }
  272. if assigned(p^.extra_info) and
  273. (p^.extra_info^.check=$12345678) and
  274. assigned(p^.extra_info^.displayproc) then
  275. p^.extra_info^.displayproc(ptext,@p^.extra_info^.data);
  276. call_stack(p,ptext);
  277. end;
  278. function is_in_getmem_list (p : pheap_mem_info) : boolean;
  279. var
  280. i : ptrint;
  281. pp : pheap_mem_info;
  282. begin
  283. is_in_getmem_list:=false;
  284. pp:=heap_mem_root;
  285. i:=0;
  286. while pp<>nil do
  287. begin
  288. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  289. ((pp^.sig<>calculate_sig(pp)) or not usecrc) and
  290. (pp^.sig <>$AAAAAAAA) then
  291. begin
  292. if useownfile then
  293. writeln(ownfile,'error in linked list of heap_mem_info')
  294. else
  295. writeln(stderr,'error in linked list of heap_mem_info');
  296. RunError(204);
  297. end;
  298. if pp=p then
  299. is_in_getmem_list:=true;
  300. pp:=pp^.previous;
  301. inc(i);
  302. if i>getmem_cnt-freemem_cnt then
  303. if useownfile then
  304. writeln(ownfile,'error in linked list of heap_mem_info')
  305. else
  306. writeln(stderr,'error in linked list of heap_mem_info');
  307. end;
  308. end;
  309. {*****************************************************************************
  310. TraceGetMem
  311. *****************************************************************************}
  312. Function TraceGetMem(size:ptrint):pointer;
  313. var
  314. allocsize,i : ptrint;
  315. oldbp,
  316. bp : pointer;
  317. pl : pdword;
  318. p : pointer;
  319. pp : pheap_mem_info;
  320. begin
  321. inc(getmem_size,size);
  322. inc(getmem8_size,((size+7) div 8)*8);
  323. { Do the real GetMem, but alloc also for the info block }
  324. {$ifdef cpuarm}
  325. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+extra_info_size;
  326. {$else cpuarm}
  327. allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
  328. {$endif cpuarm}
  329. if add_tail then
  330. inc(allocsize,sizeof(ptrint));
  331. { if ReturnNilIfGrowHeapFails is true
  332. SysGetMem can return nil }
  333. p:=SysGetMem(allocsize);
  334. if (p=nil) then
  335. begin
  336. TraceGetMem:=nil;
  337. exit;
  338. end;
  339. pp:=pheap_mem_info(p);
  340. inc(p,sizeof(theap_mem_info));
  341. { Create the info block }
  342. pp^.sig:=$DEADBEEF;
  343. pp^.size:=size;
  344. pp^.extra_info_size:=extra_info_size;
  345. pp^.exact_info_size:=exact_info_size;
  346. {
  347. the end of the block contains:
  348. <tail> 4 bytes
  349. <extra_info> X bytes
  350. }
  351. if extra_info_size>0 then
  352. begin
  353. pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
  354. fillchar(pp^.extra_info^,extra_info_size,0);
  355. pp^.extra_info^.check:=$12345678;
  356. pp^.extra_info^.fillproc:=fill_extra_info_proc;
  357. pp^.extra_info^.displayproc:=display_extra_info_proc;
  358. if assigned(fill_extra_info_proc) then
  359. begin
  360. inside_trace_getmem:=true;
  361. fill_extra_info_proc(@pp^.extra_info^.data);
  362. inside_trace_getmem:=false;
  363. end;
  364. end
  365. else
  366. pp^.extra_info:=nil;
  367. if add_tail then
  368. begin
  369. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptrint);
  370. {$ifdef FPC_SUPPORTS_UNALIGNED}
  371. unaligned(pl^):=$DEADBEEF;
  372. {$else FPC_SUPPORTS_UNALIGNED}
  373. pl^:=$DEADBEEF;
  374. {$endif FPC_SUPPORTS_UNALIGNED}
  375. end;
  376. { clear the memory }
  377. fillchar(p^,size,#255);
  378. { retrieve backtrace info }
  379. bp:=get_caller_frame(get_frame);
  380. for i:=1 to tracesize do
  381. begin
  382. { valid bp? }
  383. if (bp>=StackBottom) and (bp<(StackBottom + StackLength)) then
  384. pp^.calls[i]:=get_caller_addr(bp);
  385. oldbp:=bp;
  386. bp:=get_caller_frame(bp);
  387. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  388. bp:=nil;
  389. end;
  390. { insert in the linked list }
  391. if heap_mem_root<>nil then
  392. heap_mem_root^.next:=pp;
  393. pp^.previous:=heap_mem_root;
  394. pp^.next:=nil;
  395. {$ifdef EXTRA}
  396. pp^.prev_valid:=heap_valid_last;
  397. heap_valid_last:=pp;
  398. if not assigned(heap_valid_first) then
  399. heap_valid_first:=pp;
  400. {$endif EXTRA}
  401. heap_mem_root:=pp;
  402. { must be changed before fill_extra_info is called
  403. because checkpointer can be called from within
  404. fill_extra_info PM }
  405. inc(getmem_cnt);
  406. { update the signature }
  407. if usecrc then
  408. pp^.sig:=calculate_sig(pp);
  409. TraceGetmem:=p;
  410. end;
  411. {*****************************************************************************
  412. TraceFreeMem
  413. *****************************************************************************}
  414. function TraceFreeMemSize(p:pointer;size:ptrint):ptrint;
  415. var
  416. i,ppsize : ptrint;
  417. bp : pointer;
  418. pp : pheap_mem_info;
  419. {$ifdef EXTRA}
  420. pp2 : pheap_mem_info;
  421. {$endif}
  422. extra_size : ptrint;
  423. ptext : ^text;
  424. begin
  425. if useownfile then
  426. ptext:=@ownfile
  427. else
  428. ptext:=@stderr;
  429. if p=nil then
  430. begin
  431. TraceFreeMemSize:=0;
  432. exit;
  433. end;
  434. inc(freemem_size,size);
  435. inc(freemem8_size,((size+7) div 8)*8);
  436. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  437. ppsize:= size + sizeof(theap_mem_info)+pp^.extra_info_size;
  438. if add_tail then
  439. inc(ppsize,sizeof(ptrint));
  440. if not quicktrace then
  441. begin
  442. if not(is_in_getmem_list(pp)) then
  443. RunError(204);
  444. end;
  445. if (pp^.sig=$AAAAAAAA) and not usecrc then
  446. begin
  447. error_in_heap:=true;
  448. dump_already_free(pp,ptext^);
  449. if haltonerror then halt(1);
  450. end
  451. else if ((pp^.sig<>$DEADBEEF) or usecrc) and
  452. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  453. begin
  454. error_in_heap:=true;
  455. dump_error(pp,ptext^);
  456. {$ifdef EXTRA}
  457. dump_error(pp,error_file);
  458. {$endif EXTRA}
  459. { don't release anything in this case !! }
  460. if haltonerror then halt(1);
  461. exit;
  462. end
  463. else if pp^.size<>size then
  464. begin
  465. error_in_heap:=true;
  466. dump_wrong_size(pp,size,ptext^);
  467. {$ifdef EXTRA}
  468. dump_wrong_size(pp,size,error_file);
  469. {$endif EXTRA}
  470. if haltonerror then halt(1);
  471. { don't release anything in this case !! }
  472. exit;
  473. end;
  474. { save old values }
  475. extra_size:=pp^.extra_info_size;
  476. { now it is released !! }
  477. pp^.sig:=$AAAAAAAA;
  478. if not keepreleased then
  479. begin
  480. if pp^.next<>nil then
  481. pp^.next^.previous:=pp^.previous;
  482. if pp^.previous<>nil then
  483. pp^.previous^.next:=pp^.next;
  484. if pp=heap_mem_root then
  485. heap_mem_root:=heap_mem_root^.previous;
  486. end
  487. else
  488. begin
  489. bp:=get_caller_frame(get_frame);
  490. for i:=(tracesize div 2)+1 to tracesize do
  491. begin
  492. pp^.calls[i]:=get_caller_addr(bp);
  493. bp:=get_caller_frame(bp);
  494. end;
  495. end;
  496. inc(freemem_cnt);
  497. { clear the memory }
  498. fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
  499. { this way we keep all info about all released memory !! }
  500. if keepreleased then
  501. begin
  502. {$ifdef EXTRA}
  503. { We want to check if the memory was changed after release !! }
  504. pp^.release_sig:=calculate_release_sig(pp);
  505. if pp=heap_valid_last then
  506. begin
  507. heap_valid_last:=pp^.prev_valid;
  508. if pp=heap_valid_first then
  509. heap_valid_first:=nil;
  510. TraceFreememsize:=size;
  511. exit;
  512. end;
  513. pp2:=heap_valid_last;
  514. while assigned(pp2) do
  515. begin
  516. if pp2^.prev_valid=pp then
  517. begin
  518. pp2^.prev_valid:=pp^.prev_valid;
  519. if pp=heap_valid_first then
  520. heap_valid_first:=pp2;
  521. TraceFreememsize:=size;
  522. exit;
  523. end
  524. else
  525. pp2:=pp2^.prev_valid;
  526. end;
  527. {$endif EXTRA}
  528. TraceFreememsize:=size;
  529. exit;
  530. end;
  531. { release the normal memory at least }
  532. i:=SysFreeMemSize(pp,ppsize);
  533. { return the correct size }
  534. dec(i,sizeof(theap_mem_info)+extra_size);
  535. if add_tail then
  536. dec(i,sizeof(ptrint));
  537. TraceFreeMemSize:=i;
  538. end;
  539. function TraceMemSize(p:pointer):ptrint;
  540. var
  541. pp : pheap_mem_info;
  542. begin
  543. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  544. TraceMemSize:=pp^.size;
  545. end;
  546. function TraceFreeMem(p:pointer):ptrint;
  547. var
  548. l : ptrint;
  549. pp : pheap_mem_info;
  550. begin
  551. if p=nil then
  552. begin
  553. TraceFreeMem:=0;
  554. exit;
  555. end;
  556. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  557. l:=SysMemSize(pp);
  558. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  559. if add_tail then
  560. dec(l,sizeof(ptrint));
  561. { this can never happend normaly }
  562. if pp^.size>l then
  563. begin
  564. if useownfile then
  565. dump_wrong_size(pp,l,ownfile)
  566. else
  567. dump_wrong_size(pp,l,stderr);
  568. {$ifdef EXTRA}
  569. dump_wrong_size(pp,l,error_file);
  570. {$endif EXTRA}
  571. end;
  572. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  573. end;
  574. {*****************************************************************************
  575. ReAllocMem
  576. *****************************************************************************}
  577. function TraceReAllocMem(var p:pointer;size:ptrint):Pointer;
  578. var
  579. newP: pointer;
  580. allocsize,
  581. movesize,
  582. i : ptrint;
  583. oldbp,
  584. bp : pointer;
  585. pl : pdword;
  586. pp : pheap_mem_info;
  587. oldsize,
  588. oldextrasize,
  589. oldexactsize : ptrint;
  590. old_fill_extra_info_proc : tfillextrainfoproc;
  591. old_display_extra_info_proc : tdisplayextrainfoproc;
  592. begin
  593. { Free block? }
  594. if size=0 then
  595. begin
  596. if p<>nil then
  597. TraceFreeMem(p);
  598. p:=nil;
  599. TraceReallocMem:=P;
  600. exit;
  601. end;
  602. { Allocate a new block? }
  603. if p=nil then
  604. begin
  605. p:=TraceGetMem(size);
  606. TraceReallocMem:=P;
  607. exit;
  608. end;
  609. { Resize block }
  610. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  611. { test block }
  612. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  613. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  614. begin
  615. error_in_heap:=true;
  616. if useownfile then
  617. dump_error(pp,ownfile)
  618. else
  619. dump_error(pp,stderr);
  620. {$ifdef EXTRA}
  621. dump_error(pp,error_file);
  622. {$endif EXTRA}
  623. { don't release anything in this case !! }
  624. if haltonerror then halt(1);
  625. exit;
  626. end;
  627. { save info }
  628. oldsize:=pp^.size;
  629. oldextrasize:=pp^.extra_info_size;
  630. oldexactsize:=pp^.exact_info_size;
  631. if pp^.extra_info_size>0 then
  632. begin
  633. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  634. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  635. end;
  636. { Do the real ReAllocMem, but alloc also for the info block }
  637. {$ifdef cpuarm}
  638. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+pp^.extra_info_size;
  639. {$else cpuarm}
  640. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  641. {$endif cpuarm}
  642. if add_tail then
  643. inc(allocsize,sizeof(ptrint));
  644. { Try to resize the block, if not possible we need to do a
  645. getmem, move data, freemem }
  646. if not SysTryResizeMem(pp,allocsize) then
  647. begin
  648. { get a new block }
  649. newP := TraceGetMem(size);
  650. { move the data }
  651. if newP <> nil then
  652. begin
  653. movesize:=TraceMemSize(p);
  654. {if the old size is larger than the new size,
  655. move only the new size}
  656. if movesize>size then
  657. movesize:=size;
  658. move(p^,newP^,movesize);
  659. end;
  660. { release p }
  661. traceFreeMem(p);
  662. { return the new pointer }
  663. p:=newp;
  664. traceReAllocMem := newp;
  665. exit;
  666. end;
  667. { Recreate the info block }
  668. pp^.sig:=$DEADBEEF;
  669. pp^.size:=size;
  670. pp^.extra_info_size:=oldextrasize;
  671. pp^.exact_info_size:=oldexactsize;
  672. { add the new extra_info and tail }
  673. if pp^.extra_info_size>0 then
  674. begin
  675. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  676. fillchar(pp^.extra_info^,extra_info_size,0);
  677. pp^.extra_info^.check:=$12345678;
  678. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  679. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  680. if assigned(pp^.extra_info^.fillproc) then
  681. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  682. end
  683. else
  684. pp^.extra_info:=nil;
  685. if add_tail then
  686. begin
  687. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptrint);
  688. {$ifdef FPC_SUPPORTS_UNALIGNED}
  689. unaligned(pl^):=$DEADBEEF;
  690. {$else FPC_SUPPORTS_UNALIGNED}
  691. pl^:=$DEADBEEF;
  692. {$endif FPC_SUPPORTS_UNALIGNED}
  693. end;
  694. { adjust like a freemem and then a getmem, so you get correct
  695. results in the summary display }
  696. inc(freemem_size,oldsize);
  697. inc(freemem8_size,((oldsize+7) div 8)*8);
  698. inc(getmem_size,size);
  699. inc(getmem8_size,((size+7) div 8)*8);
  700. { generate new backtrace }
  701. bp:=get_caller_frame(get_frame);
  702. for i:=1 to tracesize do
  703. begin
  704. pp^.calls[i]:=get_caller_addr(bp);
  705. oldbp:=bp;
  706. bp:=get_caller_frame(bp);
  707. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  708. bp:=nil;
  709. end;
  710. { regenerate signature }
  711. if usecrc then
  712. pp^.sig:=calculate_sig(pp);
  713. { return the pointer }
  714. p:=pointer(pp)+sizeof(theap_mem_info);
  715. TraceReAllocmem:=p;
  716. end;
  717. {*****************************************************************************
  718. Check pointer
  719. *****************************************************************************}
  720. {$ifndef Unix}
  721. {$S-}
  722. {$endif}
  723. {$ifdef go32v2}
  724. var
  725. __stklen : longword;external name '__stklen';
  726. __stkbottom : longword;external name '__stkbottom';
  727. edata : longword; external name 'edata';
  728. {$endif go32v2}
  729. {$ifdef linux}
  730. var
  731. etext: ptruint; external name '_etext';
  732. edata : ptruint; external name '_edata';
  733. eend : ptruint; external name '_end';
  734. {$endif}
  735. {$ifdef os2}
  736. (* Currently still EMX based - possibly to be changed in the future. *)
  737. var
  738. etext: ptruint; external name '_etext';
  739. edata : ptruint; external name '_edata';
  740. eend : ptruint; external name '_end';
  741. {$endif}
  742. {$ifdef windows}
  743. var
  744. sdata : ptruint; external name '__data_start__';
  745. edata : ptruint; external name '__data_end__';
  746. sbss : ptruint; external name '__bss_start__';
  747. ebss : ptruint; external name '__bss_end__';
  748. {$endif}
  749. {$ifdef BEOS}
  750. const
  751. B_ERROR = -1;
  752. type
  753. area_id = Longint;
  754. function area_for(addr : Pointer) : area_id;
  755. cdecl; external 'root' name 'area_for';
  756. {$endif BEOS}
  757. procedure CheckPointer(p : pointer); [public, alias : 'FPC_CHECKPOINTER'];
  758. var
  759. i : ptrint;
  760. pp : pheap_mem_info;
  761. {$ifdef go32v2}
  762. get_ebp,stack_top : longword;
  763. data_end : longword;
  764. {$endif go32v2}
  765. {$ifdef morphos}
  766. stack_top: longword;
  767. {$endif morphos}
  768. ptext : ^text;
  769. label
  770. _exit;
  771. begin
  772. if p=nil then
  773. runerror(204);
  774. i:=0;
  775. if useownfile then
  776. ptext:=@ownfile
  777. else
  778. ptext:=@stderr;
  779. {$ifdef go32v2}
  780. if ptruint(p)<$1000 then
  781. runerror(216);
  782. asm
  783. movl %ebp,get_ebp
  784. leal edata,%eax
  785. movl %eax,data_end
  786. end;
  787. stack_top:=__stkbottom+__stklen;
  788. { allow all between start of code and end of data }
  789. if ptruint(p)<=data_end then
  790. goto _exit;
  791. { stack can be above heap !! }
  792. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  793. goto _exit;
  794. {$endif go32v2}
  795. { I don't know where the stack is in other OS !! }
  796. {$ifdef windows}
  797. { inside stack ? }
  798. if (ptruint(p)>ptruint(get_frame)) and
  799. (p<StackTop) then
  800. goto _exit;
  801. { inside data ? }
  802. if (ptruint(p)>=ptruint(@sdata)) and (ptruint(p)<ptruint(@edata)) then
  803. goto _exit;
  804. { inside bss ? }
  805. if (ptruint(p)>=ptruint(@sbss)) and (ptruint(p)<ptruint(@ebss)) then
  806. goto _exit;
  807. {$endif windows}
  808. {$IFDEF OS2}
  809. { inside stack ? }
  810. if (PtrUInt (P) > PtrUInt (Get_Frame)) and
  811. (PtrUInt (P) < PtrUInt (StackTop)) then
  812. goto _exit;
  813. { inside data or bss ? }
  814. if (PtrUInt (P) >= PtrUInt (@etext)) and (PtrUInt (P) < PtrUInt (@eend)) then
  815. goto _exit;
  816. {$ENDIF OS2}
  817. {$ifdef linux}
  818. { inside stack ? }
  819. if (ptruint(p)>ptruint(get_frame)) and
  820. (ptruint(p)<$c0000000) then //todo: 64bit!
  821. goto _exit;
  822. { inside data or bss ? }
  823. if (ptruint(p)>=ptruint(@etext)) and (ptruint(p)<ptruint(@eend)) then
  824. goto _exit;
  825. {$endif linux}
  826. {$ifdef morphos}
  827. { inside stack ? }
  828. stack_top:=ptruint(StackBottom)+StackLength;
  829. if (ptruint(p)<stack_top) and (ptruint(p)>ptruint(StackBottom)) then
  830. goto _exit;
  831. { inside data or bss ? }
  832. {$WARNING data and bss checking missing }
  833. {$endif morphos}
  834. {$ifdef darwin}
  835. {$warning No checkpointer support yet for Darwin}
  836. exit;
  837. {$endif}
  838. {$ifdef BEOS}
  839. // if we find the address in a known area in our current process,
  840. // then it is a valid one
  841. if area_for(p) <> B_ERROR then
  842. goto _exit;
  843. {$endif BEOS}
  844. { first try valid list faster }
  845. {$ifdef EXTRA}
  846. pp:=heap_valid_last;
  847. while pp<>nil do
  848. begin
  849. { inside this valid block ! }
  850. { we can be changing the extrainfo !! }
  851. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  852. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  853. begin
  854. { check allocated block }
  855. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  856. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  857. { special case of the fill_extra_info call }
  858. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  859. and inside_trace_getmem) then
  860. goto _exit
  861. else
  862. begin
  863. writeln(ptext^,'corrupted heap_mem_info');
  864. dump_error(pp,ptext^);
  865. halt(1);
  866. end;
  867. end
  868. else
  869. pp:=pp^.prev_valid;
  870. inc(i);
  871. if i>getmem_cnt-freemem_cnt then
  872. begin
  873. writeln(ptext^,'error in linked list of heap_mem_info');
  874. halt(1);
  875. end;
  876. end;
  877. i:=0;
  878. {$endif EXTRA}
  879. pp:=heap_mem_root;
  880. while pp<>nil do
  881. begin
  882. { inside this block ! }
  883. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  884. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  885. { allocated block }
  886. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  887. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  888. goto _exit
  889. else
  890. begin
  891. writeln(ptext^,'pointer $',hexstr(ptrint(p),2*sizeof(pointer)),' points into invalid memory block');
  892. dump_error(pp,ptext^);
  893. runerror(204);
  894. end;
  895. pp:=pp^.previous;
  896. inc(i);
  897. if i>getmem_cnt then
  898. begin
  899. writeln(ptext^,'error in linked list of heap_mem_info');
  900. halt(1);
  901. end;
  902. end;
  903. writeln(ptext^,'pointer $',hexstr(ptrint(p),2*sizeof(pointer)),' does not point to valid memory block');
  904. dump_error(p,ptext^);
  905. runerror(204);
  906. _exit:
  907. end;
  908. {*****************************************************************************
  909. Dump Heap
  910. *****************************************************************************}
  911. procedure dumpheap;
  912. var
  913. pp : pheap_mem_info;
  914. i : ptrint;
  915. ExpectedHeapFree : ptrint;
  916. status : TFPCHeapStatus;
  917. ptext : ^text;
  918. begin
  919. if useownfile then
  920. ptext:=@ownfile
  921. else
  922. ptext:=@stderr;
  923. pp:=heap_mem_root;
  924. Writeln(ptext^,'Heap dump by heaptrc unit');
  925. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  926. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  927. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  928. status:=SysGetFPCHeapStatus;
  929. Write(ptext^,'True heap size : ',status.CurrHeapSize);
  930. if EntryMemUsed > 0 then
  931. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  932. else
  933. Writeln(ptext^);
  934. Writeln(ptext^,'True free heap : ',status.CurrHeapFree);
  935. ExpectedHeapFree:=status.CurrHeapSize-(getmem8_size-freemem8_size)-
  936. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)-EntryMemUsed;
  937. If ExpectedHeapFree<>status.CurrHeapFree then
  938. Writeln(ptext^,'Should be : ',ExpectedHeapFree);
  939. i:=getmem_cnt-freemem_cnt;
  940. while pp<>nil do
  941. begin
  942. if i<0 then
  943. begin
  944. Writeln(ptext^,'Error in heap memory list');
  945. Writeln(ptext^,'More memory blocks than expected');
  946. exit;
  947. end;
  948. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  949. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  950. begin
  951. { this one was not released !! }
  952. if exitcode<>203 then
  953. call_stack(pp,ptext^);
  954. dec(i);
  955. end
  956. else if pp^.sig<>$AAAAAAAA then
  957. begin
  958. dump_error(pp,ptext^);
  959. {$ifdef EXTRA}
  960. dump_error(pp,error_file);
  961. {$endif EXTRA}
  962. error_in_heap:=true;
  963. end
  964. {$ifdef EXTRA}
  965. else if pp^.release_sig<>calculate_release_sig(pp) then
  966. begin
  967. dump_change_after(pp,ptext^);
  968. dump_change_after(pp,error_file);
  969. error_in_heap:=true;
  970. end
  971. {$endif EXTRA}
  972. ;
  973. pp:=pp^.previous;
  974. end;
  975. if HaltOnNotReleased and (getmem_cnt<>freemem_cnt) then
  976. exitcode:=203;
  977. end;
  978. {*****************************************************************************
  979. AllocMem
  980. *****************************************************************************}
  981. function TraceAllocMem(size:ptrint):Pointer;
  982. begin
  983. TraceAllocMem:=SysAllocMem(size);
  984. end;
  985. {*****************************************************************************
  986. No specific tracing calls
  987. *****************************************************************************}
  988. function TraceGetHeapStatus:THeapStatus;
  989. begin
  990. TraceGetHeapStatus:=SysGetHeapStatus;
  991. end;
  992. function TraceGetFPCHeapStatus:TFPCHeapStatus;
  993. begin
  994. TraceGetFPCHeapStatus:=SysGetFPCHeapStatus;
  995. end;
  996. {*****************************************************************************
  997. Program Hooks
  998. *****************************************************************************}
  999. Procedure SetHeapTraceOutput(const name : string);
  1000. var i : ptrint;
  1001. begin
  1002. if useownfile then
  1003. begin
  1004. useownfile:=false;
  1005. close(ownfile);
  1006. end;
  1007. assign(ownfile,name);
  1008. {$I-}
  1009. append(ownfile);
  1010. if IOResult<>0 then
  1011. Rewrite(ownfile);
  1012. {$I+}
  1013. useownfile:=true;
  1014. for i:=0 to Paramcount do
  1015. write(ownfile,paramstr(i),' ');
  1016. writeln(ownfile);
  1017. end;
  1018. procedure SetHeapExtraInfo( size : ptrint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  1019. begin
  1020. { the total size must stay multiple of 8, also allocate 2 pointers for
  1021. the fill and display procvars }
  1022. exact_info_size:=size + sizeof(theap_extra_info);
  1023. extra_info_size:=((exact_info_size+7) div 8)*8;
  1024. fill_extra_info_proc:=fillproc;
  1025. display_extra_info_proc:=displayproc;
  1026. end;
  1027. {*****************************************************************************
  1028. Install MemoryManager
  1029. *****************************************************************************}
  1030. const
  1031. TraceManager:TMemoryManager=(
  1032. NeedLock : true;
  1033. Getmem : @TraceGetMem;
  1034. Freemem : @TraceFreeMem;
  1035. FreememSize : @TraceFreeMemSize;
  1036. AllocMem : @TraceAllocMem;
  1037. ReAllocMem : @TraceReAllocMem;
  1038. MemSize : @TraceMemSize;
  1039. GetHeapStatus : @TraceGetHeapStatus;
  1040. GetFPCHeapStatus : @TraceGetFPCHeapStatus;
  1041. );
  1042. procedure TraceInit;
  1043. var
  1044. initheapstatus : TFPCHeapStatus;
  1045. begin
  1046. initheapstatus:=SysGetFPCHeapStatus;
  1047. EntryMemUsed:=initheapstatus.CurrHeapUsed;
  1048. MakeCRC32Tbl;
  1049. SetMemoryManager(TraceManager);
  1050. useownfile:=false;
  1051. if outputstr <> '' then
  1052. SetHeapTraceOutput(outputstr);
  1053. {$ifdef EXTRA}
  1054. Assign(error_file,'heap.err');
  1055. Rewrite(error_file);
  1056. {$endif EXTRA}
  1057. end;
  1058. procedure TraceExit;
  1059. begin
  1060. { no dump if error
  1061. because this gives long long listings }
  1062. { clear inoutres, in case the program that quit didn't }
  1063. ioresult;
  1064. if (exitcode<>0) and (erroraddr<>nil) then
  1065. begin
  1066. if useownfile then
  1067. begin
  1068. Writeln(ownfile,'No heap dump by heaptrc unit');
  1069. Writeln(ownfile,'Exitcode = ',exitcode);
  1070. end
  1071. else
  1072. begin
  1073. Writeln(stderr,'No heap dump by heaptrc unit');
  1074. Writeln(stderr,'Exitcode = ',exitcode);
  1075. end;
  1076. if useownfile then
  1077. begin
  1078. useownfile:=false;
  1079. close(ownfile);
  1080. end;
  1081. exit;
  1082. end;
  1083. if not error_in_heap then
  1084. Dumpheap;
  1085. if error_in_heap and (exitcode=0) then
  1086. exitcode:=203;
  1087. {$ifdef EXTRA}
  1088. Close(error_file);
  1089. {$endif EXTRA}
  1090. if useownfile then
  1091. begin
  1092. useownfile:=false;
  1093. close(ownfile);
  1094. end;
  1095. end;
  1096. {$if defined(win32) or defined(win64)}
  1097. function GetEnvironmentStrings : pchar; stdcall;
  1098. external 'kernel32' name 'GetEnvironmentStringsA';
  1099. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  1100. external 'kernel32' name 'FreeEnvironmentStringsA';
  1101. Function GetEnv(envvar: string): string;
  1102. var
  1103. s : string;
  1104. i : ptrint;
  1105. hp,p : pchar;
  1106. begin
  1107. getenv:='';
  1108. p:=GetEnvironmentStrings;
  1109. hp:=p;
  1110. while hp^<>#0 do
  1111. begin
  1112. s:=strpas(hp);
  1113. i:=pos('=',s);
  1114. if upcase(copy(s,1,i-1))=upcase(envvar) then
  1115. begin
  1116. getenv:=copy(s,i+1,length(s)-i);
  1117. break;
  1118. end;
  1119. { next string entry}
  1120. hp:=hp+strlen(hp)+1;
  1121. end;
  1122. FreeEnvironmentStrings(p);
  1123. end;
  1124. {$else defined(win32) or defined(win64)}
  1125. {$ifdef wince}
  1126. Function GetEnv(P:string):Pchar;
  1127. begin
  1128. { WinCE does not have environment strings.
  1129. Add some way to specify heaptrc options? }
  1130. GetEnv:=nil;
  1131. end;
  1132. {$else wince}
  1133. Function GetEnv(P:string):Pchar;
  1134. {
  1135. Searches the environment for a string with name p and
  1136. returns a pchar to it's value.
  1137. A pchar is used to accomodate for strings of length > 255
  1138. }
  1139. var
  1140. ep : ppchar;
  1141. i : ptrint;
  1142. found : boolean;
  1143. Begin
  1144. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1145. ep:=envp;
  1146. found:=false;
  1147. if ep<>nil then
  1148. begin
  1149. while (not found) and (ep^<>nil) do
  1150. begin
  1151. found:=true;
  1152. for i:=1 to length(p) do
  1153. if p[i]<>ep^[i-1] then
  1154. begin
  1155. found:=false;
  1156. break;
  1157. end;
  1158. if not found then
  1159. inc(ep);
  1160. end;
  1161. end;
  1162. if found then
  1163. getenv:=ep^+length(p)
  1164. else
  1165. getenv:=nil;
  1166. end;
  1167. {$endif wince}
  1168. {$endif win32}
  1169. procedure LoadEnvironment;
  1170. var
  1171. i,j : ptrint;
  1172. s : string;
  1173. begin
  1174. s:=Getenv('HEAPTRC');
  1175. if pos('keepreleased',s)>0 then
  1176. keepreleased:=true;
  1177. if pos('disabled',s)>0 then
  1178. useheaptrace:=false;
  1179. if pos('nohalt',s)>0 then
  1180. haltonerror:=false;
  1181. if pos('haltonnotreleased',s)>0 then
  1182. HaltOnNotReleased :=true;
  1183. i:=pos('log=',s);
  1184. if i>0 then
  1185. begin
  1186. outputstr:=copy(s,i+4,255);
  1187. j:=pos(' ',outputstr);
  1188. if j=0 then
  1189. j:=length(outputstr)+1;
  1190. delete(outputstr,j,255);
  1191. end;
  1192. end;
  1193. Initialization
  1194. LoadEnvironment;
  1195. { heaptrc can be disabled from the environment }
  1196. if useheaptrace then
  1197. TraceInit;
  1198. finalization
  1199. if useheaptrace then
  1200. TraceExit;
  1201. end.