heaptrc.pp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. Heap tracer
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit heaptrc;
  13. interface
  14. { 1.0.x doesn't have good rangechecking for cardinals }
  15. {$ifdef VER1_0}
  16. {$R-}
  17. {$endif}
  18. {$goto on}
  19. Procedure DumpHeap;
  20. Procedure MarkHeap;
  21. { define EXTRA to add more
  22. tests :
  23. - keep all memory after release and
  24. check by CRC value if not changed after release
  25. WARNING this needs extremely much memory (PM) }
  26. type
  27. tFillExtraInfoProc = procedure(p : pointer);
  28. tdisplayextrainfoProc = procedure (var ptext : text;p : pointer);
  29. { Allows to add info pre memory block, see ppheap.pas of the compiler
  30. for example source }
  31. procedure SetHeapExtraInfo( size : ptrint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  32. { Redirection of the output to a file }
  33. procedure SetHeapTraceOutput(const name : string);
  34. const
  35. { tracing level
  36. splitted in two if memory is released !! }
  37. {$ifdef EXTRA}
  38. tracesize = 16;
  39. {$else EXTRA}
  40. tracesize = 8;
  41. {$endif EXTRA}
  42. { install heaptrc memorymanager }
  43. useheaptrace : boolean=true;
  44. { less checking }
  45. quicktrace : boolean=true;
  46. { calls halt() on error by default !! }
  47. HaltOnError : boolean = true;
  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. ptext : ^text;
  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)),8),' 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)),8),' 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)),8),' 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)),8),' 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)),8),' 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,8),'"',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)),8),' 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. writeln(ptext^,'error in linked list of heap_mem_info');
  289. RunError(204);
  290. end;
  291. if pp=p then
  292. is_in_getmem_list:=true;
  293. pp:=pp^.previous;
  294. inc(i);
  295. if i>getmem_cnt-freemem_cnt then
  296. writeln(ptext^,'error in linked list of heap_mem_info');
  297. end;
  298. end;
  299. {*****************************************************************************
  300. TraceGetMem
  301. *****************************************************************************}
  302. Function TraceGetMem(size:ptrint):pointer;
  303. var
  304. allocsize,i : ptrint;
  305. oldbp,
  306. bp : pointer;
  307. pl : pdword;
  308. p : pointer;
  309. pp : pheap_mem_info;
  310. begin
  311. inc(getmem_size,size);
  312. inc(getmem8_size,((size+7) div 8)*8);
  313. { Do the real GetMem, but alloc also for the info block }
  314. allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
  315. if add_tail then
  316. inc(allocsize,sizeof(ptrint));
  317. p:=SysGetMem(allocsize);
  318. pp:=pheap_mem_info(p);
  319. inc(p,sizeof(theap_mem_info));
  320. { Create the info block }
  321. pp^.sig:=$DEADBEEF;
  322. pp^.size:=size;
  323. pp^.extra_info_size:=extra_info_size;
  324. pp^.exact_info_size:=exact_info_size;
  325. {
  326. the end of the block contains:
  327. <tail> 4 bytes
  328. <extra_info> X bytes
  329. }
  330. if extra_info_size>0 then
  331. begin
  332. pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
  333. fillchar(pp^.extra_info^,extra_info_size,0);
  334. pp^.extra_info^.check:=$12345678;
  335. pp^.extra_info^.fillproc:=fill_extra_info_proc;
  336. pp^.extra_info^.displayproc:=display_extra_info_proc;
  337. if assigned(fill_extra_info_proc) then
  338. begin
  339. inside_trace_getmem:=true;
  340. fill_extra_info_proc(@pp^.extra_info^.data);
  341. inside_trace_getmem:=false;
  342. end;
  343. end
  344. else
  345. pp^.extra_info:=nil;
  346. if add_tail then
  347. begin
  348. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptrint);
  349. pl^:=$DEADBEEF;
  350. end;
  351. { clear the memory }
  352. fillchar(p^,size,#255);
  353. { retrieve backtrace info }
  354. bp:=get_caller_frame(get_frame);
  355. for i:=1 to tracesize do
  356. begin
  357. pp^.calls[i]:=get_caller_addr(bp);
  358. oldbp:=bp;
  359. bp:=get_caller_frame(bp);
  360. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  361. bp:=nil;
  362. end;
  363. { insert in the linked list }
  364. if heap_mem_root<>nil then
  365. heap_mem_root^.next:=pp;
  366. pp^.previous:=heap_mem_root;
  367. pp^.next:=nil;
  368. {$ifdef EXTRA}
  369. pp^.prev_valid:=heap_valid_last;
  370. heap_valid_last:=pp;
  371. if not assigned(heap_valid_first) then
  372. heap_valid_first:=pp;
  373. {$endif EXTRA}
  374. heap_mem_root:=pp;
  375. { must be changed before fill_extra_info is called
  376. because checkpointer can be called from within
  377. fill_extra_info PM }
  378. inc(getmem_cnt);
  379. { update the signature }
  380. if usecrc then
  381. pp^.sig:=calculate_sig(pp);
  382. TraceGetmem:=p;
  383. end;
  384. {*****************************************************************************
  385. TraceFreeMem
  386. *****************************************************************************}
  387. function TraceFreeMemSize(p:pointer;size:ptrint):ptrint;
  388. var
  389. i,ppsize : ptrint;
  390. bp : pointer;
  391. pp : pheap_mem_info;
  392. {$ifdef EXTRA}
  393. pp2 : pheap_mem_info;
  394. {$endif}
  395. extra_size : ptrint;
  396. begin
  397. inc(freemem_size,size);
  398. inc(freemem8_size,((size+7) div 8)*8);
  399. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  400. ppsize:= size + sizeof(theap_mem_info)+pp^.extra_info_size;
  401. if add_tail then
  402. inc(ppsize,sizeof(ptrint));
  403. if not quicktrace then
  404. begin
  405. if not(is_in_getmem_list(pp)) then
  406. RunError(204);
  407. end;
  408. if (pp^.sig=$AAAAAAAA) and not usecrc then
  409. begin
  410. error_in_heap:=true;
  411. dump_already_free(pp,ptext^);
  412. if haltonerror then halt(1);
  413. end
  414. else if ((pp^.sig<>$DEADBEEF) or usecrc) and
  415. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  416. begin
  417. error_in_heap:=true;
  418. dump_error(pp,ptext^);
  419. {$ifdef EXTRA}
  420. dump_error(pp,error_file);
  421. {$endif EXTRA}
  422. { don't release anything in this case !! }
  423. if haltonerror then halt(1);
  424. exit;
  425. end
  426. else if pp^.size<>size then
  427. begin
  428. error_in_heap:=true;
  429. dump_wrong_size(pp,size,ptext^);
  430. {$ifdef EXTRA}
  431. dump_wrong_size(pp,size,error_file);
  432. {$endif EXTRA}
  433. if haltonerror then halt(1);
  434. { don't release anything in this case !! }
  435. exit;
  436. end;
  437. { save old values }
  438. extra_size:=pp^.extra_info_size;
  439. { now it is released !! }
  440. pp^.sig:=$AAAAAAAA;
  441. if not keepreleased then
  442. begin
  443. if pp^.next<>nil then
  444. pp^.next^.previous:=pp^.previous;
  445. if pp^.previous<>nil then
  446. pp^.previous^.next:=pp^.next;
  447. if pp=heap_mem_root then
  448. heap_mem_root:=heap_mem_root^.previous;
  449. end
  450. else
  451. begin
  452. bp:=get_caller_frame(get_frame);
  453. for i:=(tracesize div 2)+1 to tracesize do
  454. begin
  455. pp^.calls[i]:=get_caller_addr(bp);
  456. bp:=get_caller_frame(bp);
  457. end;
  458. end;
  459. inc(freemem_cnt);
  460. { clear the memory }
  461. fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
  462. { this way we keep all info about all released memory !! }
  463. if keepreleased then
  464. begin
  465. {$ifdef EXTRA}
  466. { We want to check if the memory was changed after release !! }
  467. pp^.release_sig:=calculate_release_sig(pp);
  468. if pp=heap_valid_last then
  469. begin
  470. heap_valid_last:=pp^.prev_valid;
  471. if pp=heap_valid_first then
  472. heap_valid_first:=nil;
  473. TraceFreememsize:=size;
  474. exit;
  475. end;
  476. pp2:=heap_valid_last;
  477. while assigned(pp2) do
  478. begin
  479. if pp2^.prev_valid=pp then
  480. begin
  481. pp2^.prev_valid:=pp^.prev_valid;
  482. if pp=heap_valid_first then
  483. heap_valid_first:=pp2;
  484. TraceFreememsize:=size;
  485. exit;
  486. end
  487. else
  488. pp2:=pp2^.prev_valid;
  489. end;
  490. {$endif EXTRA}
  491. TraceFreememsize:=size;
  492. exit;
  493. end;
  494. { release the normal memory at least }
  495. i:=SysFreeMemSize(pp,ppsize);
  496. { return the correct size }
  497. dec(i,sizeof(theap_mem_info)+extra_size);
  498. if add_tail then
  499. dec(i,sizeof(ptrint));
  500. TraceFreeMemSize:=i;
  501. end;
  502. function TraceMemSize(p:pointer):ptrint;
  503. var
  504. pp : pheap_mem_info;
  505. begin
  506. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  507. TraceMemSize:=pp^.size;
  508. end;
  509. function TraceFreeMem(p:pointer):ptrint;
  510. var
  511. l : ptrint;
  512. pp : pheap_mem_info;
  513. begin
  514. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  515. l:=SysMemSize(pp);
  516. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  517. if add_tail then
  518. dec(l,sizeof(ptrint));
  519. { this can never happend normaly }
  520. if pp^.size>l then
  521. begin
  522. dump_wrong_size(pp,l,ptext^);
  523. {$ifdef EXTRA}
  524. dump_wrong_size(pp,l,error_file);
  525. {$endif EXTRA}
  526. end;
  527. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  528. end;
  529. {*****************************************************************************
  530. ReAllocMem
  531. *****************************************************************************}
  532. function TraceReAllocMem(var p:pointer;size:ptrint):Pointer;
  533. var
  534. newP: pointer;
  535. allocsize,
  536. movesize,
  537. i : ptrint;
  538. bp : pointer;
  539. pl : pdword;
  540. pp : pheap_mem_info;
  541. oldextrasize,
  542. oldexactsize : ptrint;
  543. old_fill_extra_info_proc : tfillextrainfoproc;
  544. old_display_extra_info_proc : tdisplayextrainfoproc;
  545. begin
  546. { Free block? }
  547. if size=0 then
  548. begin
  549. if p<>nil then
  550. TraceFreeMem(p);
  551. p:=nil;
  552. TraceReallocMem:=P;
  553. exit;
  554. end;
  555. { Allocate a new block? }
  556. if p=nil then
  557. begin
  558. p:=TraceGetMem(size);
  559. TraceReallocMem:=P;
  560. exit;
  561. end;
  562. { Resize block }
  563. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  564. { test block }
  565. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  566. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  567. begin
  568. error_in_heap:=true;
  569. dump_error(pp,ptext^);
  570. {$ifdef EXTRA}
  571. dump_error(pp,error_file);
  572. {$endif EXTRA}
  573. { don't release anything in this case !! }
  574. if haltonerror then halt(1);
  575. exit;
  576. end;
  577. { save info }
  578. oldextrasize:=pp^.extra_info_size;
  579. oldexactsize:=pp^.exact_info_size;
  580. if pp^.extra_info_size>0 then
  581. begin
  582. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  583. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  584. end;
  585. { Do the real ReAllocMem, but alloc also for the info block }
  586. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  587. if add_tail then
  588. inc(allocsize,sizeof(ptrint));
  589. { Try to resize the block, if not possible we need to do a
  590. getmem, move data, freemem }
  591. if not SysTryResizeMem(pp,allocsize) then
  592. begin
  593. { get a new block }
  594. newP := TraceGetMem(size);
  595. { move the data }
  596. if newP <> nil then
  597. begin
  598. movesize:=TraceMemSize(p);
  599. {if the old size is larger than the new size,
  600. move only the new size}
  601. if movesize>size then
  602. movesize:=size;
  603. move(p^,newP^,movesize);
  604. end;
  605. { release p }
  606. traceFreeMem(p);
  607. { return the new pointer }
  608. p:=newp;
  609. traceReAllocMem := newp;
  610. exit;
  611. end;
  612. { adjust like a freemem and then a getmem, so you get correct
  613. results in the summary display }
  614. inc(freemem_size,pp^.size);
  615. inc(freemem8_size,((pp^.size+7) div 8)*8);
  616. inc(getmem_size,size);
  617. inc(getmem8_size,((size+7) div 8)*8);
  618. { Recreate the info block }
  619. pp^.sig:=$DEADBEEF;
  620. pp^.size:=size;
  621. pp^.extra_info_size:=oldextrasize;
  622. pp^.exact_info_size:=oldexactsize;
  623. { add the new extra_info and tail }
  624. if pp^.extra_info_size>0 then
  625. begin
  626. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  627. fillchar(pp^.extra_info^,extra_info_size,0);
  628. pp^.extra_info^.check:=$12345678;
  629. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  630. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  631. if assigned(pp^.extra_info^.fillproc) then
  632. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  633. end
  634. else
  635. pp^.extra_info:=nil;
  636. if add_tail then
  637. begin
  638. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptrint);
  639. pl^:=$DEADBEEF;
  640. end;
  641. { generate new backtrace }
  642. bp:=get_caller_frame(get_frame);
  643. for i:=1 to tracesize do
  644. begin
  645. pp^.calls[i]:=get_caller_addr(bp);
  646. bp:=get_caller_frame(bp);
  647. end;
  648. { regenerate signature }
  649. if usecrc then
  650. pp^.sig:=calculate_sig(pp);
  651. { return the pointer }
  652. p:=pointer(pp)+sizeof(theap_mem_info);
  653. TraceReAllocmem:=p;
  654. end;
  655. {*****************************************************************************
  656. Check pointer
  657. *****************************************************************************}
  658. {$ifndef Unix}
  659. {$S-}
  660. {$endif}
  661. {$ifdef go32v2}
  662. var
  663. __stklen : longword;external name '__stklen';
  664. __stkbottom : longword;external name '__stkbottom';
  665. edata : longword; external name 'edata';
  666. {$endif go32v2}
  667. {$ifdef linux}
  668. var
  669. etext: ptruint; external name '_etext';
  670. edata : ptruint; external name '_edata';
  671. {$endif}
  672. procedure CheckPointer(p : pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[public, alias : 'FPC_CHECKPOINTER'];
  673. var
  674. i : ptrint;
  675. pp : pheap_mem_info;
  676. get_ebp,stack_top : longword;
  677. data_end : longword;
  678. label
  679. _exit;
  680. begin
  681. if p=nil then
  682. runerror(204);
  683. i:=0;
  684. {$ifdef go32v2}
  685. if ptruint(p)<$1000 then
  686. runerror(216);
  687. asm
  688. movl %ebp,get_ebp
  689. leal edata,%eax
  690. movl %eax,data_end
  691. end;
  692. stack_top:=__stkbottom+__stklen;
  693. { allow all between start of code and end of data }
  694. if ptruint(p)<=data_end then
  695. goto _exit;
  696. { stack can be above heap !! }
  697. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  698. goto _exit;
  699. {$endif go32v2}
  700. { I don't know where the stack is in other OS !! }
  701. {$ifdef win32}
  702. { inside stack ? }
  703. if (ptruint(p)>ptruint(get_frame)) and
  704. (ptruint(p)<Win32StackTop) then
  705. goto _exit;
  706. {$endif win32}
  707. {$ifdef linux}
  708. { inside stack ? }
  709. if (ptruint(p)>ptruint(get_frame)) and
  710. (ptruint(p)<$c0000000) then //todo: 64bit!
  711. goto _exit;
  712. { inside data ? }
  713. if (ptruint(p)>=ptruint(@etext)) and (ptruint(p)<ptruint(@edata)) then
  714. goto _exit;
  715. {$endif linux}
  716. { first try valid list faster }
  717. {$ifdef EXTRA}
  718. pp:=heap_valid_last;
  719. while pp<>nil do
  720. begin
  721. { inside this valid block ! }
  722. { we can be changing the extrainfo !! }
  723. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  724. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  725. begin
  726. { check allocated block }
  727. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  728. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  729. { special case of the fill_extra_info call }
  730. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  731. and inside_trace_getmem) then
  732. goto _exit
  733. else
  734. begin
  735. writeln(ptext^,'corrupted heap_mem_info');
  736. dump_error(pp,ptext^);
  737. halt(1);
  738. end;
  739. end
  740. else
  741. pp:=pp^.prev_valid;
  742. inc(i);
  743. if i>getmem_cnt-freemem_cnt then
  744. begin
  745. writeln(ptext^,'error in linked list of heap_mem_info');
  746. halt(1);
  747. end;
  748. end;
  749. i:=0;
  750. {$endif EXTRA}
  751. pp:=heap_mem_root;
  752. while pp<>nil do
  753. begin
  754. { inside this block ! }
  755. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  756. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  757. { allocated block }
  758. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  759. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  760. goto _exit
  761. else
  762. begin
  763. writeln(ptext^,'pointer $',hexstr(ptrint(p),8),' points into invalid memory block');
  764. dump_error(pp,ptext^);
  765. runerror(204);
  766. end;
  767. pp:=pp^.previous;
  768. inc(i);
  769. if i>getmem_cnt then
  770. begin
  771. writeln(ptext^,'error in linked list of heap_mem_info');
  772. halt(1);
  773. end;
  774. end;
  775. writeln(ptext^,'pointer $',hexstr(ptrint(p),8),' does not point to valid memory block');
  776. runerror(204);
  777. _exit:
  778. end;
  779. {*****************************************************************************
  780. Dump Heap
  781. *****************************************************************************}
  782. procedure dumpheap;
  783. var
  784. pp : pheap_mem_info;
  785. i : ptrint;
  786. ExpectedHeapFree : ptrint;
  787. status : THeapStatus;
  788. begin
  789. pp:=heap_mem_root;
  790. Writeln(ptext^,'Heap dump by heaptrc unit');
  791. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  792. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  793. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  794. SysGetHeapStatus(status);
  795. Write(ptext^,'True heap size : ',status.CurrHeapSize);
  796. if EntryMemUsed > 0 then
  797. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  798. else
  799. Writeln(ptext^);
  800. Writeln(ptext^,'True free heap : ',status.CurrHeapFree);
  801. ExpectedHeapFree:=status.CurrHeapSize-(getmem8_size-freemem8_size)-
  802. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)-EntryMemUsed;
  803. If ExpectedHeapFree<>status.CurrHeapFree then
  804. Writeln(ptext^,'Should be : ',ExpectedHeapFree);
  805. i:=getmem_cnt-freemem_cnt;
  806. while pp<>nil do
  807. begin
  808. if i<0 then
  809. begin
  810. Writeln(ptext^,'Error in heap memory list');
  811. Writeln(ptext^,'More memory blocks than expected');
  812. exit;
  813. end;
  814. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  815. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  816. begin
  817. { this one was not released !! }
  818. if exitcode<>203 then
  819. call_stack(pp,ptext^);
  820. dec(i);
  821. end
  822. else if pp^.sig<>$AAAAAAAA then
  823. begin
  824. dump_error(pp,ptext^);
  825. {$ifdef EXTRA}
  826. dump_error(pp,error_file);
  827. {$endif EXTRA}
  828. error_in_heap:=true;
  829. end
  830. {$ifdef EXTRA}
  831. else if pp^.release_sig<>calculate_release_sig(pp) then
  832. begin
  833. dump_change_after(pp,ptext^);
  834. dump_change_after(pp,error_file);
  835. error_in_heap:=true;
  836. end
  837. {$endif EXTRA}
  838. ;
  839. pp:=pp^.previous;
  840. end;
  841. end;
  842. procedure markheap;
  843. var
  844. pp : pheap_mem_info;
  845. begin
  846. pp:=heap_mem_root;
  847. while pp<>nil do
  848. begin
  849. pp^.sig:=$AAAAAAAA;
  850. pp:=pp^.previous;
  851. end;
  852. end;
  853. {*****************************************************************************
  854. AllocMem
  855. *****************************************************************************}
  856. function TraceAllocMem(size:ptrint):Pointer;
  857. begin
  858. TraceAllocMem:=SysAllocMem(size);
  859. end;
  860. {*****************************************************************************
  861. No specific tracing calls
  862. *****************************************************************************}
  863. procedure TraceGetHeapStatus(var status:THeapStatus);
  864. begin
  865. SysGetHeapStatus(status);
  866. end;
  867. {*****************************************************************************
  868. Program Hooks
  869. *****************************************************************************}
  870. Procedure SetHeapTraceOutput(const name : string);
  871. var i : ptrint;
  872. begin
  873. if ptext<>@stderr then
  874. begin
  875. ptext:=@stderr;
  876. close(ownfile);
  877. end;
  878. assign(ownfile,name);
  879. {$I-}
  880. append(ownfile);
  881. if IOResult<>0 then
  882. Rewrite(ownfile);
  883. {$I+}
  884. ptext:=@ownfile;
  885. for i:=0 to Paramcount do
  886. write(ptext^,paramstr(i),' ');
  887. writeln(ptext^);
  888. end;
  889. procedure SetHeapExtraInfo( size : ptrint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  890. begin
  891. { the total size must stay multiple of 8, also allocate 2 pointers for
  892. the fill and display procvars }
  893. exact_info_size:=size + sizeof(theap_extra_info);
  894. extra_info_size:=((exact_info_size+7) div 8)*8;
  895. fill_extra_info_proc:=fillproc;
  896. display_extra_info_proc:=displayproc;
  897. end;
  898. {*****************************************************************************
  899. Install MemoryManager
  900. *****************************************************************************}
  901. const
  902. TraceManager:TMemoryManager=(
  903. NeedLock : true;
  904. Getmem : @TraceGetMem;
  905. Freemem : @TraceFreeMem;
  906. FreememSize : @TraceFreeMemSize;
  907. AllocMem : @TraceAllocMem;
  908. ReAllocMem : @TraceReAllocMem;
  909. MemSize : @TraceMemSize;
  910. GetHeapStatus : @TraceGetHeapStatus;
  911. );
  912. procedure TraceInit;
  913. var
  914. initheapstatus : THeapStatus;
  915. begin
  916. SysGetHeapStatus(initheapstatus);
  917. EntryMemUsed:=initheapstatus.CurrHeapUsed;
  918. MakeCRC32Tbl;
  919. SetMemoryManager(TraceManager);
  920. ptext:=@stderr;
  921. if outputstr <> '' then
  922. SetHeapTraceOutput(outputstr);
  923. {$ifdef EXTRA}
  924. Assign(error_file,'heap.err');
  925. Rewrite(error_file);
  926. {$endif EXTRA}
  927. end;
  928. procedure TraceExit;
  929. begin
  930. { no dump if error
  931. because this gives long long listings }
  932. { clear inoutres, in case the program that quit didn't }
  933. ioresult;
  934. if (exitcode<>0) and (erroraddr<>nil) then
  935. begin
  936. Writeln(ptext^,'No heap dump by heaptrc unit');
  937. Writeln(ptext^,'Exitcode = ',exitcode);
  938. if ptext<>@stderr then
  939. begin
  940. ptext:=@stderr;
  941. close(ownfile);
  942. end;
  943. exit;
  944. end;
  945. if not error_in_heap then
  946. Dumpheap;
  947. if error_in_heap and (exitcode=0) then
  948. exitcode:=203;
  949. {$ifdef EXTRA}
  950. Close(error_file);
  951. {$endif EXTRA}
  952. if ptext<>@stderr then
  953. begin
  954. ptext:=@stderr;
  955. close(ownfile);
  956. end;
  957. end;
  958. {$ifdef win32}
  959. function GetEnvironmentStrings : pchar; stdcall;
  960. external 'kernel32' name 'GetEnvironmentStringsA';
  961. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  962. external 'kernel32' name 'FreeEnvironmentStringsA';
  963. Function GetEnv(envvar: string): string;
  964. var
  965. s : string;
  966. i : ptrint;
  967. hp,p : pchar;
  968. begin
  969. getenv:='';
  970. p:=GetEnvironmentStrings;
  971. hp:=p;
  972. while hp^<>#0 do
  973. begin
  974. s:=strpas(hp);
  975. i:=pos('=',s);
  976. if upcase(copy(s,1,i-1))=upcase(envvar) then
  977. begin
  978. getenv:=copy(s,i+1,length(s)-i);
  979. break;
  980. end;
  981. { next string entry}
  982. hp:=hp+strlen(hp)+1;
  983. end;
  984. FreeEnvironmentStrings(p);
  985. end;
  986. {$else}
  987. Function GetEnv(P:string):Pchar;
  988. {
  989. Searches the environment for a string with name p and
  990. returns a pchar to it's value.
  991. A pchar is used to accomodate for strings of length > 255
  992. }
  993. var
  994. ep : ppchar;
  995. i : ptrint;
  996. found : boolean;
  997. Begin
  998. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  999. ep:=envp;
  1000. found:=false;
  1001. if ep<>nil then
  1002. begin
  1003. while (not found) and (ep^<>nil) do
  1004. begin
  1005. found:=true;
  1006. for i:=1 to length(p) do
  1007. if p[i]<>ep^[i-1] then
  1008. begin
  1009. found:=false;
  1010. break;
  1011. end;
  1012. if not found then
  1013. inc(ep);
  1014. end;
  1015. end;
  1016. if found then
  1017. getenv:=ep^+length(p)
  1018. else
  1019. getenv:=nil;
  1020. end;
  1021. {$endif}
  1022. procedure LoadEnvironment;
  1023. var
  1024. i,j : ptrint;
  1025. s : string;
  1026. begin
  1027. s:=Getenv('HEAPTRC');
  1028. if pos('keepreleased',s)>0 then
  1029. keepreleased:=true;
  1030. if pos('disabled',s)>0 then
  1031. useheaptrace:=false;
  1032. if pos('nohalt',s)>0 then
  1033. haltonerror:=false;
  1034. i:=pos('log=',s);
  1035. if i>0 then
  1036. begin
  1037. outputstr:=copy(s,i+4,255);
  1038. j:=pos(' ',outputstr);
  1039. if j=0 then
  1040. j:=length(outputstr)+1;
  1041. delete(outputstr,j,255);
  1042. end;
  1043. end;
  1044. Initialization
  1045. LoadEnvironment;
  1046. { heaptrc can be disabled from the environment }
  1047. if useheaptrace then
  1048. TraceInit;
  1049. finalization
  1050. if useheaptrace then
  1051. TraceExit;
  1052. end.
  1053. {
  1054. $Log$
  1055. Revision 1.37 2004-11-22 19:34:58 peter
  1056. * GetHeapStatus added, removed MaxAvail,MemAvail,HeapSize
  1057. Revision 1.36 2004/10/25 17:04:07 peter
  1058. * fix for non-i386
  1059. Revision 1.35 2004/10/25 15:38:59 peter
  1060. * compiler defined HEAP and HEAPSIZE removed
  1061. Revision 1.34 2004/10/24 20:01:41 peter
  1062. * saveregisters calling convention is obsolete
  1063. Revision 1.33 2004/09/21 14:49:29 peter
  1064. * fix reallocmem
  1065. Revision 1.32 2004/09/16 07:21:08 michael
  1066. Fix tracememsize for ansistrings (From Peter)
  1067. Revision 1.31 2004/06/20 09:24:40 peter
  1068. fixed go32v2 compile
  1069. Revision 1.30 2004/06/17 16:16:13 peter
  1070. * New heapmanager that releases memory back to the OS, donated
  1071. by Micha Nelissen
  1072. Revision 1.29 2004/05/22 20:35:52 peter
  1073. check whether bp is in the stack value allocated by the main program
  1074. Revision 1.28 2004/04/28 20:48:20 peter
  1075. * ordinal-pointer conversions fixed
  1076. Revision 1.27 2004/03/15 21:48:26 peter
  1077. * cmem moved to rtl
  1078. * longint replaced with ptrint in heapmanagers
  1079. Revision 1.26 2004/03/15 14:22:39 michael
  1080. + Fix from peter for win32 SIGTRAp signal
  1081. Revision 1.25 2004/02/06 20:17:12 daniel
  1082. * Use $ for hex numbers instead of alien 0x
  1083. Revision 1.24 2003/09/11 15:54:27 peter
  1084. * when retrieving stackdump check if bp is smaller than the previous
  1085. bp
  1086. Revision 1.23 2003/03/17 14:30:11 peter
  1087. * changed address parameter/return values to pointer instead
  1088. of ptrint
  1089. Revision 1.22 2002/12/26 10:46:54 peter
  1090. * set p to nil when 0 is passed to reallocmem
  1091. Revision 1.21 2002/11/30 23:34:43 carl
  1092. * nil should give an error!
  1093. Revision 1.20 2002/10/30 20:39:13 peter
  1094. * MemoryManager record has a field NeedLock if the wrapper functions
  1095. need to provide locking for multithreaded programs
  1096. Revision 1.19 2002/10/05 15:19:46 carl
  1097. * bugfix of assigning to external filename output
  1098. Revision 1.18 2002/09/09 15:45:49 jonas
  1099. * made result type of calculate_release_sig() a longword instead of a
  1100. ptrint
  1101. Revision 1.17 2002/09/07 15:07:45 peter
  1102. * old logs removed and tabs fixed
  1103. }