heaptrc.pp 33 KB

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