heaptrc.pp 34 KB

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