heaptrc.pp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993-98 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. { 0.99.12 had a bug that initialization/finalization only worked for
  14. objfpc,delphi mode }
  15. {$ifdef VER0_99_12}
  16. {$mode objfpc}
  17. {$endif}
  18. interface
  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. FillExtraInfoType = procedure(p : pointer);
  28. { allows to add several longint value that can help
  29. to debug :
  30. see for instance ppheap.pas unit of the compiler source PM }
  31. Procedure SetExtraInfo( size : longint;func : FillExtraInfoType);
  32. Procedure SetHeapTraceOutput(const name : string);
  33. const
  34. { tracing level
  35. splitted in two if memory is released !! }
  36. {$ifdef EXTRA}
  37. tracesize = 16;
  38. {$else EXTRA}
  39. tracesize = 8;
  40. {$endif EXTRA}
  41. quicktrace : boolean=true;
  42. { calls halt() on error by default !! }
  43. HaltOnError : boolean = true;
  44. { set this to true if you suspect that memory
  45. is freed several times }
  46. {$ifdef EXTRA}
  47. keepreleased : boolean=true;
  48. add_tail : boolean = true;
  49. {$else EXTRA}
  50. keepreleased : boolean=false;
  51. add_tail : boolean = false;
  52. {$endif EXTRA}
  53. { put crc in sig
  54. this allows to test for writing into that part }
  55. usecrc : boolean = true;
  56. implementation
  57. type
  58. plongint = ^longint;
  59. const
  60. { allows to add custom info in heap_mem_info }
  61. extra_info_size : longint = 0;
  62. exact_info_size : longint = 0;
  63. { function to fill this info up }
  64. fill_extra_info : FillExtraInfoType = nil;
  65. error_in_heap : boolean = false;
  66. inside_trace_getmem : boolean = false;
  67. type
  68. pheap_mem_info = ^theap_mem_info;
  69. { warning the size of theap_mem_info
  70. must be a multiple of 8
  71. because otherwise you will get
  72. problems when releasing the usual memory part !!
  73. sizeof(theap_mem_info = 16+tracesize*4 so
  74. tracesize must be even !! PM }
  75. theap_mem_info = record
  76. previous,
  77. next : pheap_mem_info;
  78. size : longint;
  79. sig : longint;
  80. {$ifdef EXTRA}
  81. release_sig : longint;
  82. prev_valid : pheap_mem_info;
  83. {$endif EXTRA}
  84. calls : array [1..tracesize] of longint;
  85. extra_info : record
  86. end;
  87. end;
  88. var
  89. ptext : ^text;
  90. ownfile : text;
  91. {$ifdef EXTRA}
  92. error_file : text;
  93. heap_valid_first,
  94. heap_valid_last : pheap_mem_info;
  95. {$endif EXTRA}
  96. heap_mem_root : pheap_mem_info;
  97. getmem_cnt,
  98. freemem_cnt : longint;
  99. getmem_size,
  100. freemem_size : longint;
  101. getmem8_size,
  102. freemem8_size : longint;
  103. {*****************************************************************************
  104. Crc 32
  105. *****************************************************************************}
  106. var
  107. {$ifdef Delphi}
  108. Crc32Tbl : array[0..255] of longword;
  109. {$else Delphi}
  110. Crc32Tbl : array[0..255] of longint;
  111. {$endif Delphi}
  112. procedure MakeCRC32Tbl;
  113. var
  114. {$ifdef Delphi}
  115. crc : longword;
  116. {$else Delphi}
  117. crc : longint;
  118. {$endif Delphi}
  119. i,n : byte;
  120. begin
  121. for i:=0 to 255 do
  122. begin
  123. crc:=i;
  124. for n:=1 to 8 do
  125. if odd(crc) then
  126. crc:=(crc shr 1) xor $edb88320
  127. else
  128. crc:=crc shr 1;
  129. Crc32Tbl[i]:=crc;
  130. end;
  131. end;
  132. {$ifopt R+}
  133. {$define Range_check_on}
  134. {$endif opt R+}
  135. {$R- needed here }
  136. Function UpdateCrc32(InitCrc:longint;var InBuf;InLen:Longint):longint;
  137. var
  138. i : longint;
  139. p : pchar;
  140. begin
  141. p:=@InBuf;
  142. for i:=1 to InLen do
  143. begin
  144. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  145. inc(longint(p));
  146. end;
  147. UpdateCrc32:=InitCrc;
  148. end;
  149. Function calculate_sig(p : pheap_mem_info) : longint;
  150. var
  151. crc : longint;
  152. pl : plongint;
  153. begin
  154. crc:=$ffffffff;
  155. crc:=UpdateCrc32(crc,p^.size,sizeof(longint));
  156. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(longint));
  157. if extra_info_size>0 then
  158. crc:=UpdateCrc32(crc,p^.extra_info,exact_info_size);
  159. if add_tail then
  160. begin
  161. { Check also 4 bytes just after allocation !! }
  162. pl:=pointer(p)+extra_info_size+sizeof(theap_mem_info)+p^.size;
  163. crc:=UpdateCrc32(crc,pl^,sizeof(longint));
  164. end;
  165. calculate_sig:=crc;
  166. end;
  167. {$ifdef EXTRA}
  168. Function calculate_release_sig(p : pheap_mem_info) : longint;
  169. var
  170. crc : longint;
  171. pl : plongint;
  172. begin
  173. crc:=$ffffffff;
  174. crc:=UpdateCrc32(crc,p^.size,sizeof(longint));
  175. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(longint));
  176. if extra_info_size>0 then
  177. crc:=UpdateCrc32(crc,p^.extra_info,exact_info_size);
  178. { Check the whole of the whole allocation }
  179. pl:=pointer(p)+extra_info_size+sizeof(theap_mem_info);
  180. crc:=UpdateCrc32(crc,pl^,p^.size);
  181. { Check also 4 bytes just after allocation !! }
  182. if add_tail then
  183. begin
  184. { Check also 4 bytes just after allocation !! }
  185. pl:=pointer(p)+extra_info_size+sizeof(theap_mem_info)+p^.size;
  186. crc:=UpdateCrc32(crc,pl^,sizeof(longint));
  187. end;
  188. calculate_release_sig:=crc;
  189. end;
  190. {$endif EXTRA}
  191. {$ifdef Range_check_on}
  192. {$R+}
  193. {$undef Range_check_on}
  194. {$endif Range_check_on}
  195. {*****************************************************************************
  196. Helpers
  197. *****************************************************************************}
  198. procedure call_stack(pp : pheap_mem_info;var ptext : text);
  199. var
  200. i : longint;
  201. begin
  202. writeln(ptext,'Call trace for block 0x',hexstr(longint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
  203. for i:=1 to tracesize do
  204. if pp^.calls[i]<>0 then
  205. writeln(ptext,' 0x',hexstr(pp^.calls[i],8));
  206. for i:=0 to (exact_info_size div 4)-1 do
  207. writeln(ptext,'info ',i,'=',plongint(pointer(@pp^.extra_info)+4*i)^);
  208. end;
  209. procedure call_free_stack(pp : pheap_mem_info;var ptext : text);
  210. var
  211. i : longint;
  212. begin
  213. writeln(ptext,'Call trace for block 0x',hexstr(longint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
  214. for i:=1 to tracesize div 2 do
  215. if pp^.calls[i]<>0 then
  216. writeln(ptext,' 0x',hexstr(pp^.calls[i],8));
  217. writeln(ptext,' was released at ');
  218. for i:=(tracesize div 2)+1 to tracesize do
  219. if pp^.calls[i]<>0 then
  220. writeln(ptext,' 0x',hexstr(pp^.calls[i],8));
  221. for i:=0 to (exact_info_size div 4)-1 do
  222. writeln(ptext,'info ',i,'=',plongint(pointer(@pp^.extra_info)+4*i)^);
  223. end;
  224. procedure dump_already_free(p : pheap_mem_info;var ptext : text);
  225. begin
  226. Writeln(ptext,'Marked memory at ',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' released');
  227. call_free_stack(p,ptext);
  228. Writeln(ptext,'freed again at');
  229. dump_stack(ptext,get_caller_frame(get_frame));
  230. end;
  231. procedure dump_error(p : pheap_mem_info;var ptext : text);
  232. begin
  233. Writeln(ptext,'Marked memory at ',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  234. Writeln(ptext,'Wrong signature $',hexstr(p^.sig,8)
  235. ,' instead of ',hexstr(calculate_sig(p),8));
  236. dump_stack(ptext,get_caller_frame(get_frame));
  237. end;
  238. {$ifdef EXTRA}
  239. procedure dump_change_after(p : pheap_mem_info;var ptext : text);
  240. var pp : pchar;
  241. i : longint;
  242. begin
  243. Writeln(ptext,'Marked memory at ',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  244. Writeln(ptext,'Wrong release CRC $',hexstr(p^.release_sig,8)
  245. ,' instead of ',hexstr(calculate_release_sig(p),8));
  246. Writeln(ptext,'This memory was changed after call to freemem !');
  247. call_free_stack(p,ptext);
  248. pp:=pointer(p)+sizeof(theap_mem_info)+extra_info_size;
  249. for i:=0 to p^.size-1 do
  250. if byte(pp[i])<>$F0 then
  251. Writeln(ptext,'offset',i,':$',hexstr(i,8),'"',pp[i],'"');
  252. end;
  253. {$endif EXTRA}
  254. procedure dump_wrong_size(p : pheap_mem_info;size : longint;var ptext : text);
  255. var
  256. i : longint;
  257. begin
  258. Writeln(ptext,'Marked memory at ',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  259. Writeln(ptext,'Wrong size : ',p^.size,' allocated ',size,' freed');
  260. dump_stack(ptext,get_caller_frame(get_frame));
  261. for i:=0 to (exact_info_size div 4)-1 do
  262. writeln(ptext,'info ',i,'=',plongint(@p^.extra_info+4*i)^);
  263. call_stack(p,ptext);
  264. end;
  265. function is_in_getmem_list (p : pheap_mem_info) : boolean;
  266. var
  267. i : longint;
  268. pp : pheap_mem_info;
  269. begin
  270. is_in_getmem_list:=false;
  271. pp:=heap_mem_root;
  272. i:=0;
  273. while pp<>nil do
  274. begin
  275. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  276. ((pp^.sig<>calculate_sig(pp)) or not usecrc) and
  277. (pp^.sig <> $AAAAAAAA) then
  278. begin
  279. writeln(ptext^,'error in linked list of heap_mem_info');
  280. RunError(204);
  281. end;
  282. if pp=p then
  283. is_in_getmem_list:=true;
  284. pp:=pp^.previous;
  285. inc(i);
  286. if i>getmem_cnt-freemem_cnt then
  287. writeln(ptext^,'error in linked list of heap_mem_info');
  288. end;
  289. end;
  290. {*****************************************************************************
  291. TraceGetMem
  292. *****************************************************************************}
  293. Function TraceGetMem(size:longint):pointer;
  294. var
  295. i,bp : longint;
  296. pl : plongint;
  297. p : pointer;
  298. begin
  299. inc(getmem_size,size);
  300. inc(getmem8_size,((size+7) div 8)*8);
  301. { Do the real GetMem, but alloc also for the info block }
  302. bp:=size+sizeof(theap_mem_info)+extra_info_size;
  303. if add_tail then
  304. inc(bp,sizeof(longint));
  305. p:=SysGetMem(bp);
  306. { Create the info block }
  307. pheap_mem_info(p)^.sig:=$DEADBEEF;
  308. pheap_mem_info(p)^.size:=size;
  309. if add_tail then
  310. begin
  311. pl:=pointer(p)+bp-sizeof(longint);
  312. pl^:=$DEADBEEF;
  313. end;
  314. bp:=get_caller_frame(get_frame);
  315. for i:=1 to tracesize do
  316. begin
  317. pheap_mem_info(p)^.calls[i]:=get_caller_addr(bp);
  318. bp:=get_caller_frame(bp);
  319. end;
  320. { insert in the linked list }
  321. if heap_mem_root<>nil then
  322. heap_mem_root^.next:=pheap_mem_info(p);
  323. pheap_mem_info(p)^.previous:=heap_mem_root;
  324. pheap_mem_info(p)^.next:=nil;
  325. {$ifdef EXTRA}
  326. pheap_mem_info(p)^.prev_valid:=heap_valid_last;
  327. heap_valid_last:=pheap_mem_info(p);
  328. if not assigned(heap_valid_first) then
  329. heap_valid_first:=pheap_mem_info(p);
  330. {$endif EXTRA}
  331. heap_mem_root:=p;
  332. { must be changed before fill_extra_info is called
  333. because checkpointer can be called from within
  334. fill_extra_info PM }
  335. inc(getmem_cnt);
  336. if assigned(fill_extra_info) then
  337. begin
  338. inside_trace_getmem:=true;
  339. fill_extra_info(@pheap_mem_info(p)^.extra_info);
  340. inside_trace_getmem:=false;
  341. end;
  342. { update the pointer }
  343. if usecrc then
  344. pheap_mem_info(p)^.sig:=calculate_sig(pheap_mem_info(p));
  345. inc(p,sizeof(theap_mem_info)+extra_info_size);
  346. TraceGetmem:=p;
  347. end;
  348. {*****************************************************************************
  349. TraceFreeMem
  350. *****************************************************************************}
  351. function TraceFreeMemSize(var p:pointer;size:longint):longint;
  352. var
  353. i,bp, ppsize : longint;
  354. pp : pheap_mem_info;
  355. {$ifdef EXTRA}
  356. pp2 : pheap_mem_info;
  357. {$endif}
  358. begin
  359. inc(freemem_size,size);
  360. inc(freemem8_size,((size+7) div 8)*8);
  361. ppsize:= size + sizeof(theap_mem_info)+extra_info_size;
  362. if add_tail then
  363. ppsize:=ppsize+sizeof(longint);
  364. dec(p,sizeof(theap_mem_info)+extra_info_size);
  365. pp:=pheap_mem_info(p);
  366. if not quicktrace and not(is_in_getmem_list(pp)) then
  367. RunError(204);
  368. if (pp^.sig=$AAAAAAAA) and not usecrc then
  369. begin
  370. error_in_heap:=true;
  371. dump_already_free(pp,ptext^);
  372. if haltonerror then halt(1);
  373. end
  374. else if ((pp^.sig<>$DEADBEEF) or usecrc) and
  375. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  376. begin
  377. error_in_heap:=true;
  378. dump_error(pp,ptext^);
  379. {$ifdef EXTRA}
  380. dump_error(pp,error_file);
  381. {$endif EXTRA}
  382. { don't release anything in this case !! }
  383. if haltonerror then halt(1);
  384. exit;
  385. end
  386. else if pp^.size<>size then
  387. begin
  388. error_in_heap:=true;
  389. dump_wrong_size(pp,size,ptext^);
  390. {$ifdef EXTRA}
  391. dump_wrong_size(pp,size,error_file);
  392. {$endif EXTRA}
  393. if haltonerror then halt(1);
  394. { don't release anything in this case !! }
  395. exit;
  396. end;
  397. { now it is released !! }
  398. pp^.sig:=$AAAAAAAA;
  399. if not keepreleased then
  400. begin
  401. if pp^.next<>nil then
  402. pp^.next^.previous:=pp^.previous;
  403. if pp^.previous<>nil then
  404. pp^.previous^.next:=pp^.next;
  405. if pp=heap_mem_root then
  406. heap_mem_root:=heap_mem_root^.previous;
  407. end
  408. else
  409. begin
  410. bp:=get_caller_frame(get_frame);
  411. for i:=(tracesize div 2)+1 to tracesize do
  412. begin
  413. pp^.calls[i]:=get_caller_addr(bp);
  414. bp:=get_caller_frame(bp);
  415. end;
  416. end;
  417. inc(freemem_cnt);
  418. { release the normal memory at least !! }
  419. { this way we keep all info about all released memory !! }
  420. if keepreleased then
  421. begin
  422. {$ifndef EXTRA}
  423. dec(ppsize,sizeof(theap_mem_info)+extra_info_size);
  424. inc(p,sizeof(theap_mem_info)+extra_info_size);
  425. {$else EXTRA}
  426. inc(p,sizeof(theap_mem_info)+extra_info_size);
  427. fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
  428. { We want to check if the memory was changed after release !! }
  429. pp^.release_sig:=calculate_release_sig(pp);
  430. if pp=heap_valid_last then
  431. begin
  432. heap_valid_last:=pp^.prev_valid;
  433. if pp=heap_valid_first then
  434. heap_valid_first:=nil;
  435. exit;
  436. end;
  437. pp2:=heap_valid_last;
  438. while assigned(pp2) do
  439. begin
  440. if pp2^.prev_valid=pp then
  441. begin
  442. pp2^.prev_valid:=pp^.prev_valid;
  443. if pp=heap_valid_first then
  444. heap_valid_first:=pp2;
  445. exit;
  446. end
  447. else
  448. pp2:=pp2^.prev_valid;
  449. end;
  450. exit;
  451. {$endif EXTRA}
  452. end;
  453. i:=SysFreeMemSize(p,ppsize);
  454. dec(i,sizeof(theap_mem_info)+extra_info_size);
  455. if add_tail then
  456. dec(i,sizeof(longint));
  457. TraceFreeMemSize:=i;
  458. end;
  459. function TraceMemSize(p:pointer):Longint;
  460. var
  461. l : longint;
  462. begin
  463. l:=SysMemSize(p-(sizeof(theap_mem_info)+extra_info_size));
  464. dec(l,sizeof(theap_mem_info)+extra_info_size);
  465. if add_tail then
  466. dec(l,sizeof(longint));
  467. TraceMemSize:=l;
  468. end;
  469. function TraceFreeMem(var p:pointer):longint;
  470. var
  471. size : longint;
  472. pp : pheap_mem_info;
  473. begin
  474. pp:=pheap_mem_info(pointer(p)-(sizeof(theap_mem_info)+extra_info_size));
  475. size:=TraceMemSize(p);
  476. { this can never happend normaly }
  477. if pp^.size>size then
  478. begin
  479. dump_wrong_size(pp,size,ptext^);
  480. {$ifdef EXTRA}
  481. dump_wrong_size(pp,size,error_file);
  482. {$endif EXTRA}
  483. end;
  484. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  485. end;
  486. {*****************************************************************************
  487. Check pointer
  488. *****************************************************************************}
  489. {$ifdef go32v2}
  490. var
  491. __stklen : cardinal;external name '__stklen';
  492. __stkbottom : cardinal;external name '__stkbottom';
  493. edata : cardinal; external name 'edata';
  494. {$endif go32v2}
  495. {$ifndef linux}
  496. {$S-}
  497. {$endif}
  498. var
  499. heap_at_init : pointer;
  500. StartUpHeapEnd : pointer;
  501. procedure CheckPointer(p : pointer);[public, alias : 'FPC_CHECKPOINTER'];
  502. var
  503. i : longint;
  504. pp : pheap_mem_info;
  505. get_ebp,stack_top : cardinal;
  506. data_end : cardinal;
  507. label
  508. _exit;
  509. begin
  510. asm
  511. pushal
  512. end;
  513. if p=nil then
  514. goto _exit;
  515. i:=0;
  516. {$ifdef go32v2}
  517. if cardinal(p)<$1000 then
  518. runerror(216);
  519. asm
  520. movl %ebp,get_ebp
  521. leal edata,%eax
  522. movl %eax,data_end
  523. end;
  524. stack_top:=__stkbottom+__stklen;
  525. { allow all between start of code and end of data }
  526. if cardinal(p)<=data_end then
  527. goto _exit;
  528. { .bss section }
  529. if cardinal(p)<=cardinal(heap_at_init) then
  530. goto _exit;
  531. { stack can be above heap !! }
  532. if (cardinal(p)>=get_ebp) and (cardinal(p)<=stack_top) then
  533. goto _exit;
  534. {$endif go32v2}
  535. { I don't know where the stack is in other OS !! }
  536. {$ifdef win32}
  537. if (cardinal(p)>=$40000) and (p<=HeapOrg) then
  538. goto _exit;
  539. { inside stack ? }
  540. if (cardinal(startupheapend)<Win32StackTop) and (cardinal(p)>cardinal(startupheapend)) and
  541. (cardinal(p)<Win32StackTop) then
  542. goto _exit;
  543. {$endif win32}
  544. if p>=heapptr then
  545. runerror(216);
  546. { first try valid list faster }
  547. {$ifdef EXTRA}
  548. pp:=heap_valid_last;
  549. while pp<>nil do
  550. begin
  551. { inside this valid block ! }
  552. { we can be changing the extrainfo !! }
  553. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  554. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  555. begin
  556. { check allocated block }
  557. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  558. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  559. { special case of the fill_extra_info call }
  560. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  561. and inside_trace_getmem) then
  562. goto _exit
  563. else
  564. begin
  565. writeln(ptext^,'corrupted heap_mem_info');
  566. dump_error(pp,ptext^);
  567. halt(1);
  568. end;
  569. end
  570. else
  571. pp:=pp^.prev_valid;
  572. inc(i);
  573. if i>getmem_cnt-freemem_cnt then
  574. begin
  575. writeln(ptext^,'error in linked list of heap_mem_info');
  576. halt(1);
  577. end;
  578. end;
  579. i:=0;
  580. {$endif EXTRA}
  581. pp:=heap_mem_root;
  582. while pp<>nil do
  583. begin
  584. { inside this block ! }
  585. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size) and
  586. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  587. { allocated block }
  588. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  589. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  590. goto _exit
  591. else
  592. begin
  593. writeln(ptext^,'pointer $',hexstr(longint(p),8),' points into invalid memory block');
  594. dump_error(pp,ptext^);
  595. runerror(204);
  596. end;
  597. pp:=pp^.previous;
  598. inc(i);
  599. if i>getmem_cnt then
  600. begin
  601. writeln(ptext^,'error in linked list of heap_mem_info');
  602. halt(1);
  603. end;
  604. end;
  605. writeln(ptext^,'pointer $',hexstr(longint(p),8),' does not point to valid memory block');
  606. runerror(204);
  607. _exit:
  608. asm
  609. popal
  610. end;
  611. end;
  612. {*****************************************************************************
  613. Dump Heap
  614. *****************************************************************************}
  615. procedure dumpheap;
  616. var
  617. pp : pheap_mem_info;
  618. i : longint;
  619. begin
  620. pp:=heap_mem_root;
  621. Writeln(ptext^,'Heap dump by heaptrc unit');
  622. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  623. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  624. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  625. Writeln(ptext^,'True heap size : ',system.HeapSize);
  626. Writeln(ptext^,'True free heap : ',MemAvail);
  627. Writeln(ptext^,'Should be : ',system.HeapSize-(getmem8_size-freemem8_size)-
  628. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size));
  629. i:=getmem_cnt-freemem_cnt;
  630. while pp<>nil do
  631. begin
  632. if i<0 then
  633. begin
  634. Writeln(ptext^,'Error in heap memory list');
  635. Writeln(ptext^,'More memory blocks than expected');
  636. exit;
  637. end;
  638. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  639. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  640. begin
  641. { this one was not released !! }
  642. if exitcode<>203 then
  643. call_stack(pp,ptext^);
  644. dec(i);
  645. end
  646. else if pp^.sig<>$AAAAAAAA then
  647. begin
  648. dump_error(pp,ptext^);
  649. {$ifdef EXTRA}
  650. dump_error(pp,error_file);
  651. {$endif EXTRA}
  652. error_in_heap:=true;
  653. end
  654. {$ifdef EXTRA}
  655. else if pp^.release_sig<>calculate_release_sig(pp) then
  656. begin
  657. dump_change_after(pp,ptext^);
  658. dump_change_after(pp,error_file);
  659. error_in_heap:=true;
  660. end
  661. {$endif EXTRA}
  662. ;
  663. pp:=pp^.previous;
  664. end;
  665. end;
  666. procedure markheap;
  667. var
  668. pp : pheap_mem_info;
  669. begin
  670. pp:=heap_mem_root;
  671. while pp<>nil do
  672. begin
  673. pp^.sig:=$AAAAAAAA;
  674. pp:=pp^.previous;
  675. end;
  676. end;
  677. {*****************************************************************************
  678. AllocMem
  679. *****************************************************************************}
  680. function TraceAllocMem(size:longint):Pointer;
  681. begin
  682. TraceAllocMem:=SysAllocMem(size);
  683. end;
  684. {*****************************************************************************
  685. ReAllocMem
  686. *****************************************************************************}
  687. function TraceReAllocMem(var p:pointer;size:longint):Pointer;
  688. var
  689. i,bp : longint;
  690. pl : plongint;
  691. pp : pheap_mem_info;
  692. begin
  693. dec(p,sizeof(theap_mem_info)+extra_info_size);
  694. { remove heap_mem_info for linked list }
  695. pp:=pheap_mem_info(p);
  696. if pp^.next<>nil then
  697. pp^.next^.previous:=pp^.previous;
  698. if pp^.previous<>nil then
  699. pp^.previous^.next:=pp^.next;
  700. if pp=heap_mem_root then
  701. heap_mem_root:=heap_mem_root^.previous;
  702. { Do the real GetMem, but alloc also for the info block }
  703. bp:=size+sizeof(theap_mem_info)+extra_info_size;
  704. if add_tail then
  705. inc(bp,sizeof(longint));
  706. p:=SysReAllocMem(p,bp);
  707. { Create the info block }
  708. pheap_mem_info(p)^.sig:=$DEADBEEF;
  709. pheap_mem_info(p)^.size:=size;
  710. if add_tail then
  711. begin
  712. pl:=pointer(p)+bp-sizeof(longint);
  713. pl^:=$DEADBEEF;
  714. end;
  715. bp:=get_caller_frame(get_frame);
  716. for i:=1 to tracesize do
  717. begin
  718. pheap_mem_info(p)^.calls[i]:=get_caller_addr(bp);
  719. bp:=get_caller_frame(bp);
  720. end;
  721. { insert in the linked list }
  722. if heap_mem_root<>nil then
  723. heap_mem_root^.next:=pheap_mem_info(p);
  724. pheap_mem_info(p)^.previous:=heap_mem_root;
  725. pheap_mem_info(p)^.next:=nil;
  726. {$ifdef EXTRA}
  727. pheap_mem_info(p)^.next_valid:=nil;
  728. if assigned(heap_valid_last) then
  729. heap_valid_last^.next_valid:=pheap_mem_info(p);
  730. heap_valid_last:=pheap_mem_info(p);
  731. if not assigned(heap_valid_first) then
  732. heap_valid_first:=pheap_mem_info(p);
  733. {$endif EXTRA}
  734. heap_mem_root:=p;
  735. if assigned(fill_extra_info) then
  736. fill_extra_info(@pheap_mem_info(p)^.extra_info);
  737. { update the pointer }
  738. if usecrc then
  739. pheap_mem_info(p)^.sig:=calculate_sig(pheap_mem_info(p));
  740. inc(p,sizeof(theap_mem_info)+extra_info_size);
  741. inc(getmem_cnt);
  742. TraceReAllocmem:=p;
  743. end;
  744. {*****************************************************************************
  745. No specific tracing calls
  746. *****************************************************************************}
  747. function TraceMemAvail:longint;
  748. begin
  749. TraceMemAvail:=SysMemAvail;
  750. end;
  751. function TraceMaxAvail:longint;
  752. begin
  753. TraceMaxAvail:=SysMaxAvail;
  754. end;
  755. function TraceHeapSize:longint;
  756. begin
  757. TraceHeapSize:=SysHeapSize;
  758. end;
  759. {*****************************************************************************
  760. Install MemoryManager
  761. *****************************************************************************}
  762. const
  763. TraceManager:TMemoryManager=(
  764. Getmem : TraceGetMem;
  765. Freemem : TraceFreeMem;
  766. FreememSize : TraceFreeMemSize;
  767. AllocMem : TraceAllocMem;
  768. ReAllocMem : TraceReAllocMem;
  769. MemSize : TraceMemSize;
  770. MemAvail : TraceMemAvail;
  771. MaxAvail : TraceMaxAvail;
  772. HeapSize : TraceHeapsize;
  773. );
  774. procedure TraceExit;
  775. begin
  776. { no dump if error
  777. because this gives long long listings }
  778. if (exitcode<>0) and (erroraddr<>nil) then
  779. begin
  780. Writeln(ptext^,'No heap dump by heaptrc unit');
  781. Writeln(ptext^,'Exitcode = ',exitcode);
  782. if ptext<>@stderr then
  783. begin
  784. ptext:=@stderr;
  785. close(ownfile);
  786. end;
  787. exit;
  788. end;
  789. if not error_in_heap then
  790. Dumpheap;
  791. if error_in_heap and (exitcode=0) then
  792. exitcode:=203;
  793. {$ifdef EXTRA}
  794. Close(error_file);
  795. {$endif EXTRA}
  796. if ptext<>@stderr then
  797. begin
  798. ptext:=@stderr;
  799. close(ownfile);
  800. end;
  801. end;
  802. Procedure SetHeapTraceOutput(const name : string);
  803. var i : longint;
  804. begin
  805. if ptext<>@stderr then
  806. begin
  807. ptext:=@stderr;
  808. close(ownfile);
  809. end;
  810. assign(ownfile,name);
  811. {$I-}
  812. append(ownfile);
  813. if IOResult<>0 then
  814. Rewrite(ownfile);
  815. {$I+}
  816. ptext:=@ownfile;
  817. for i:=0 to Paramcount do
  818. write(ptext^,paramstr(i),' ');
  819. writeln(ptext^);
  820. end;
  821. procedure SetExtraInfo( size : longint;func : fillextrainfotype);
  822. begin
  823. if getmem_cnt>0 then
  824. begin
  825. writeln(ptext^,'Setting extra info is only possible at start !! ');
  826. dumpheap;
  827. end
  828. else
  829. begin
  830. { the total size must stay multiple of 8 !! }
  831. exact_info_size:=size;
  832. extra_info_size:=((size+7) div 8)*8;
  833. fill_extra_info:=func;
  834. end;
  835. end;
  836. Initialization
  837. MakeCRC32Tbl;
  838. SetMemoryManager(TraceManager);
  839. ptext:=@stderr;
  840. {$ifdef EXTRA}
  841. Assign(error_file,'heap.err');
  842. Rewrite(error_file);
  843. {$endif EXTRA}
  844. Heap_at_init:=HeapPtr;
  845. StartupHeapEnd:=HeapEnd;
  846. finalization
  847. TraceExit;
  848. end.
  849. {
  850. $Log$
  851. Revision 1.29 1999-11-14 21:35:04 peter
  852. * removed warnings
  853. Revision 1.28 1999/11/09 22:32:23 pierre
  854. * several extra_size_info fixes
  855. Revision 1.27 1999/11/06 14:35:38 peter
  856. * truncated log
  857. Revision 1.26 1999/11/01 13:56:50 peter
  858. * freemem,reallocmem now get var argument
  859. Revision 1.25 1999/10/30 17:39:05 peter
  860. * memorymanager expanded with allocmem/reallocmem
  861. Revision 1.24 1999/09/17 17:14:12 peter
  862. + new heap manager supporting delphi freemem(pointer)
  863. Revision 1.23 1999/09/10 17:13:41 peter
  864. * fixed missing var
  865. Revision 1.22 1999/09/08 16:14:41 peter
  866. * pointer fixes
  867. Revision 1.21 1999/08/18 12:03:16 peter
  868. * objfpc mode for 0.99.12
  869. Revision 1.20 1999/08/17 14:56:03 michael
  870. Removed the mode for objpas
  871. Revision 1.19 1999/07/10 10:33:50 peter
  872. * merged
  873. Revision 1.18 1999/07/09 10:38:10 michael
  874. + + heaptrc now uses finalize instead of exitproc
  875. Revision 1.17 1999/07/05 20:22:08 peter
  876. * merged
  877. Revision 1.16.2.3 1999/07/10 10:31:56 peter
  878. * removed unused var
  879. Revision 1.16.2.2 1999/07/09 10:44:23 michael
  880. + Merged finalize
  881. Revision 1.16 1999/05/23 00:07:17 pierre
  882. * support for heap allocated before TraceGetMem is used in
  883. FPC_CHECKPOINTER
  884. * faster CHECKPOINTER routine (list of valid blocks only !)
  885. Revision 1.15 1999/05/18 22:15:55 pierre
  886. * allow for .bss section below heaporg in go32v2 code
  887. Revision 1.14 1999/05/16 23:56:09 pierre
  888. * allow nil pointer in FPC_CHECKPOINTER !!
  889. Revision 1.13 1999/05/12 16:49:29 pierre
  890. + with EXTRA memory is filled with $F0 and checked at end
  891. Revision 1.12 1999/05/11 12:52:42 pierre
  892. + extra's with -dEXTRA, uses a CRC check for released memory
  893. Revision 1.11 1999/03/26 19:10:34 peter
  894. * show also allocation stack for a wrong size
  895. Revision 1.10 1999/02/16 17:20:26 pierre
  896. * no heap dump if program has an heap error !
  897. Revision 1.9 1999/01/22 12:39:22 pierre
  898. + added text arg for dump_stack
  899. }