heaptrc.pp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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. procedure CheckPointer(p : pointer);[saveregisters,public, alias : 'FPC_CHECKPOINTER'];
  668. var
  669. i : ptrint;
  670. pp : pheap_mem_info;
  671. get_ebp,stack_top : longword;
  672. data_end : longword;
  673. label
  674. _exit;
  675. begin
  676. if p=nil then
  677. runerror(204);
  678. i:=0;
  679. {$ifdef go32v2}
  680. if ptruint(p)<$1000 then
  681. runerror(216);
  682. asm
  683. movl %ebp,get_ebp
  684. leal edata,%eax
  685. movl %eax,data_end
  686. end;
  687. stack_top:=__stkbottom+__stklen;
  688. { allow all between start of code and end of data }
  689. if ptruint(p)<=data_end then
  690. goto _exit;
  691. { stack can be above heap !! }
  692. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  693. goto _exit;
  694. {$endif go32v2}
  695. { I don't know where the stack is in other OS !! }
  696. {$ifdef win32}
  697. { inside stack ? }
  698. asm
  699. movl %ebp,get_ebp
  700. end;
  701. if (ptruint(p)>get_ebp) and
  702. (ptruint(p)<Win32StackTop) then
  703. goto _exit;
  704. {$endif win32}
  705. { first try valid list faster }
  706. {$ifdef EXTRA}
  707. pp:=heap_valid_last;
  708. while pp<>nil do
  709. begin
  710. { inside this valid block ! }
  711. { we can be changing the extrainfo !! }
  712. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  713. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  714. begin
  715. { check allocated block }
  716. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  717. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  718. { special case of the fill_extra_info call }
  719. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  720. and inside_trace_getmem) then
  721. goto _exit
  722. else
  723. begin
  724. writeln(ptext^,'corrupted heap_mem_info');
  725. dump_error(pp,ptext^);
  726. halt(1);
  727. end;
  728. end
  729. else
  730. pp:=pp^.prev_valid;
  731. inc(i);
  732. if i>getmem_cnt-freemem_cnt then
  733. begin
  734. writeln(ptext^,'error in linked list of heap_mem_info');
  735. halt(1);
  736. end;
  737. end;
  738. i:=0;
  739. {$endif EXTRA}
  740. pp:=heap_mem_root;
  741. while pp<>nil do
  742. begin
  743. { inside this block ! }
  744. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  745. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  746. { allocated block }
  747. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  748. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  749. goto _exit
  750. else
  751. begin
  752. writeln(ptext^,'pointer $',hexstr(ptrint(p),8),' points into invalid memory block');
  753. dump_error(pp,ptext^);
  754. runerror(204);
  755. end;
  756. pp:=pp^.previous;
  757. inc(i);
  758. if i>getmem_cnt then
  759. begin
  760. writeln(ptext^,'error in linked list of heap_mem_info');
  761. halt(1);
  762. end;
  763. end;
  764. writeln(ptext^,'pointer $',hexstr(ptrint(p),8),' does not point to valid memory block');
  765. runerror(204);
  766. _exit:
  767. end;
  768. {*****************************************************************************
  769. Dump Heap
  770. *****************************************************************************}
  771. procedure dumpheap;
  772. var
  773. pp : pheap_mem_info;
  774. i : ptrint;
  775. ExpectedMemAvail : ptrint;
  776. begin
  777. pp:=heap_mem_root;
  778. Writeln(ptext^,'Heap dump by heaptrc unit');
  779. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  780. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  781. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  782. Write(ptext^,'True heap size : ',system.HeapSize);
  783. if EntryMemUsed > 0 then
  784. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  785. else
  786. Writeln(ptext^);
  787. Writeln(ptext^,'True free heap : ',MemAvail);
  788. ExpectedMemAvail:=system.HeapSize-(getmem8_size-freemem8_size)-
  789. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)-EntryMemUsed;
  790. If ExpectedMemAvail<>MemAvail then
  791. Writeln(ptext^,'Should be : ',ExpectedMemAvail);
  792. i:=getmem_cnt-freemem_cnt;
  793. while pp<>nil do
  794. begin
  795. if i<0 then
  796. begin
  797. Writeln(ptext^,'Error in heap memory list');
  798. Writeln(ptext^,'More memory blocks than expected');
  799. exit;
  800. end;
  801. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  802. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  803. begin
  804. { this one was not released !! }
  805. if exitcode<>203 then
  806. call_stack(pp,ptext^);
  807. dec(i);
  808. end
  809. else if pp^.sig<>$AAAAAAAA then
  810. begin
  811. dump_error(pp,ptext^);
  812. {$ifdef EXTRA}
  813. dump_error(pp,error_file);
  814. {$endif EXTRA}
  815. error_in_heap:=true;
  816. end
  817. {$ifdef EXTRA}
  818. else if pp^.release_sig<>calculate_release_sig(pp) then
  819. begin
  820. dump_change_after(pp,ptext^);
  821. dump_change_after(pp,error_file);
  822. error_in_heap:=true;
  823. end
  824. {$endif EXTRA}
  825. ;
  826. pp:=pp^.previous;
  827. end;
  828. end;
  829. procedure markheap;
  830. var
  831. pp : pheap_mem_info;
  832. begin
  833. pp:=heap_mem_root;
  834. while pp<>nil do
  835. begin
  836. pp^.sig:=$AAAAAAAA;
  837. pp:=pp^.previous;
  838. end;
  839. end;
  840. {*****************************************************************************
  841. AllocMem
  842. *****************************************************************************}
  843. function TraceAllocMem(size:ptrint):Pointer;
  844. begin
  845. TraceAllocMem:=SysAllocMem(size);
  846. end;
  847. {*****************************************************************************
  848. No specific tracing calls
  849. *****************************************************************************}
  850. function TraceMemAvail:ptrint;
  851. begin
  852. TraceMemAvail:=SysMemAvail;
  853. end;
  854. function TraceMaxAvail:ptrint;
  855. begin
  856. TraceMaxAvail:=SysMaxAvail;
  857. end;
  858. function TraceHeapSize:ptrint;
  859. begin
  860. TraceHeapSize:=SysHeapSize;
  861. end;
  862. {*****************************************************************************
  863. Program Hooks
  864. *****************************************************************************}
  865. Procedure SetHeapTraceOutput(const name : string);
  866. var i : ptrint;
  867. begin
  868. if ptext<>@stderr then
  869. begin
  870. ptext:=@stderr;
  871. close(ownfile);
  872. end;
  873. assign(ownfile,name);
  874. {$I-}
  875. append(ownfile);
  876. if IOResult<>0 then
  877. Rewrite(ownfile);
  878. {$I+}
  879. ptext:=@ownfile;
  880. for i:=0 to Paramcount do
  881. write(ptext^,paramstr(i),' ');
  882. writeln(ptext^);
  883. end;
  884. procedure SetHeapExtraInfo( size : ptrint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  885. begin
  886. { the total size must stay multiple of 8, also allocate 2 pointers for
  887. the fill and display procvars }
  888. exact_info_size:=size + sizeof(theap_extra_info);
  889. extra_info_size:=((exact_info_size+7) div 8)*8;
  890. fill_extra_info_proc:=fillproc;
  891. display_extra_info_proc:=displayproc;
  892. end;
  893. {*****************************************************************************
  894. Install MemoryManager
  895. *****************************************************************************}
  896. const
  897. TraceManager:TMemoryManager=(
  898. NeedLock : true;
  899. Getmem : @TraceGetMem;
  900. Freemem : @TraceFreeMem;
  901. FreememSize : @TraceFreeMemSize;
  902. AllocMem : @TraceAllocMem;
  903. ReAllocMem : @TraceReAllocMem;
  904. MemSize : @TraceMemSize;
  905. MemAvail : @TraceMemAvail;
  906. MaxAvail : @TraceMaxAvail;
  907. HeapSize : @TraceHeapsize;
  908. );
  909. procedure TraceInit;
  910. begin
  911. EntryMemUsed:=System.HeapSize-MemAvail;
  912. MakeCRC32Tbl;
  913. SetMemoryManager(TraceManager);
  914. ptext:=@stderr;
  915. if outputstr <> '' then
  916. SetHeapTraceOutput(outputstr);
  917. {$ifdef EXTRA}
  918. Assign(error_file,'heap.err');
  919. Rewrite(error_file);
  920. {$endif EXTRA}
  921. end;
  922. procedure TraceExit;
  923. begin
  924. { no dump if error
  925. because this gives long long listings }
  926. { clear inoutres, in case the program that quit didn't }
  927. ioresult;
  928. if (exitcode<>0) and (erroraddr<>nil) then
  929. begin
  930. Writeln(ptext^,'No heap dump by heaptrc unit');
  931. Writeln(ptext^,'Exitcode = ',exitcode);
  932. if ptext<>@stderr then
  933. begin
  934. ptext:=@stderr;
  935. close(ownfile);
  936. end;
  937. exit;
  938. end;
  939. if not error_in_heap then
  940. Dumpheap;
  941. if error_in_heap and (exitcode=0) then
  942. exitcode:=203;
  943. {$ifdef EXTRA}
  944. Close(error_file);
  945. {$endif EXTRA}
  946. if ptext<>@stderr then
  947. begin
  948. ptext:=@stderr;
  949. close(ownfile);
  950. end;
  951. end;
  952. {$ifdef win32}
  953. function GetEnvironmentStrings : pchar; stdcall;
  954. external 'kernel32' name 'GetEnvironmentStringsA';
  955. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  956. external 'kernel32' name 'FreeEnvironmentStringsA';
  957. Function GetEnv(envvar: string): string;
  958. var
  959. s : string;
  960. i : ptrint;
  961. hp,p : pchar;
  962. begin
  963. getenv:='';
  964. p:=GetEnvironmentStrings;
  965. hp:=p;
  966. while hp^<>#0 do
  967. begin
  968. s:=strpas(hp);
  969. i:=pos('=',s);
  970. if upcase(copy(s,1,i-1))=upcase(envvar) then
  971. begin
  972. getenv:=copy(s,i+1,length(s)-i);
  973. break;
  974. end;
  975. { next string entry}
  976. hp:=hp+strlen(hp)+1;
  977. end;
  978. FreeEnvironmentStrings(p);
  979. end;
  980. {$else}
  981. Function GetEnv(P:string):Pchar;
  982. {
  983. Searches the environment for a string with name p and
  984. returns a pchar to it's value.
  985. A pchar is used to accomodate for strings of length > 255
  986. }
  987. var
  988. ep : ppchar;
  989. i : ptrint;
  990. found : boolean;
  991. Begin
  992. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  993. ep:=envp;
  994. found:=false;
  995. if ep<>nil then
  996. begin
  997. while (not found) and (ep^<>nil) do
  998. begin
  999. found:=true;
  1000. for i:=1 to length(p) do
  1001. if p[i]<>ep^[i-1] then
  1002. begin
  1003. found:=false;
  1004. break;
  1005. end;
  1006. if not found then
  1007. inc(ep);
  1008. end;
  1009. end;
  1010. if found then
  1011. getenv:=ep^+length(p)
  1012. else
  1013. getenv:=nil;
  1014. end;
  1015. {$endif}
  1016. procedure LoadEnvironment;
  1017. var
  1018. i,j : ptrint;
  1019. s : string;
  1020. begin
  1021. s:=Getenv('HEAPTRC');
  1022. if pos('keepreleased',s)>0 then
  1023. keepreleased:=true;
  1024. if pos('disabled',s)>0 then
  1025. useheaptrace:=false;
  1026. if pos('nohalt',s)>0 then
  1027. haltonerror:=false;
  1028. i:=pos('log=',s);
  1029. if i>0 then
  1030. begin
  1031. outputstr:=copy(s,i+4,255);
  1032. j:=pos(' ',outputstr);
  1033. if j=0 then
  1034. j:=length(outputstr)+1;
  1035. delete(outputstr,j,255);
  1036. end;
  1037. end;
  1038. Initialization
  1039. LoadEnvironment;
  1040. { heaptrc can be disabled from the environment }
  1041. if useheaptrace then
  1042. TraceInit;
  1043. finalization
  1044. if useheaptrace then
  1045. TraceExit;
  1046. end.
  1047. {
  1048. $Log$
  1049. Revision 1.33 2004-09-21 14:49:29 peter
  1050. * fix reallocmem
  1051. Revision 1.32 2004/09/16 07:21:08 michael
  1052. Fix tracememsize for ansistrings (From Peter)
  1053. Revision 1.31 2004/06/20 09:24:40 peter
  1054. fixed go32v2 compile
  1055. Revision 1.30 2004/06/17 16:16:13 peter
  1056. * New heapmanager that releases memory back to the OS, donated
  1057. by Micha Nelissen
  1058. Revision 1.29 2004/05/22 20:35:52 peter
  1059. check whether bp is in the stack value allocated by the main program
  1060. Revision 1.28 2004/04/28 20:48:20 peter
  1061. * ordinal-pointer conversions fixed
  1062. Revision 1.27 2004/03/15 21:48:26 peter
  1063. * cmem moved to rtl
  1064. * longint replaced with ptrint in heapmanagers
  1065. Revision 1.26 2004/03/15 14:22:39 michael
  1066. + Fix from peter for win32 SIGTRAp signal
  1067. Revision 1.25 2004/02/06 20:17:12 daniel
  1068. * Use $ for hex numbers instead of alien 0x
  1069. Revision 1.24 2003/09/11 15:54:27 peter
  1070. * when retrieving stackdump check if bp is smaller than the previous
  1071. bp
  1072. Revision 1.23 2003/03/17 14:30:11 peter
  1073. * changed address parameter/return values to pointer instead
  1074. of ptrint
  1075. Revision 1.22 2002/12/26 10:46:54 peter
  1076. * set p to nil when 0 is passed to reallocmem
  1077. Revision 1.21 2002/11/30 23:34:43 carl
  1078. * nil should give an error!
  1079. Revision 1.20 2002/10/30 20:39:13 peter
  1080. * MemoryManager record has a field NeedLock if the wrapper functions
  1081. need to provide locking for multithreaded programs
  1082. Revision 1.19 2002/10/05 15:19:46 carl
  1083. * bugfix of assigning to external filename output
  1084. Revision 1.18 2002/09/09 15:45:49 jonas
  1085. * made result type of calculate_release_sig() a longword instead of a
  1086. ptrint
  1087. Revision 1.17 2002/09/07 15:07:45 peter
  1088. * old logs removed and tabs fixed
  1089. }