heaptrc.pp 34 KB

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