heaptrc.pp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998-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. { 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. {$ifndef linux}
  490. {$S-}
  491. {$endif}
  492. {$ifdef go32v2}
  493. var
  494. __stklen : cardinal;external name '__stklen';
  495. __stkbottom : cardinal;external name '__stkbottom';
  496. edata : cardinal; external name 'edata';
  497. heap_at_init : pointer;
  498. {$endif go32v2}
  499. {$ifdef win32}
  500. var
  501. StartUpHeapEnd : pointer;
  502. {$endif}
  503. procedure CheckPointer(p : pointer);[public, alias : 'FPC_CHECKPOINTER'];
  504. var
  505. i : longint;
  506. pp : pheap_mem_info;
  507. get_ebp,stack_top : cardinal;
  508. data_end : cardinal;
  509. label
  510. _exit;
  511. begin
  512. asm
  513. pushal
  514. end;
  515. if p=nil then
  516. goto _exit;
  517. i:=0;
  518. {$ifdef go32v2}
  519. if cardinal(p)<$1000 then
  520. runerror(216);
  521. asm
  522. movl %ebp,get_ebp
  523. leal edata,%eax
  524. movl %eax,data_end
  525. end;
  526. stack_top:=__stkbottom+__stklen;
  527. { allow all between start of code and end of data }
  528. if cardinal(p)<=data_end then
  529. goto _exit;
  530. { .bss section }
  531. if cardinal(p)<=cardinal(heap_at_init) then
  532. goto _exit;
  533. { stack can be above heap !! }
  534. if (cardinal(p)>=get_ebp) and (cardinal(p)<=stack_top) then
  535. goto _exit;
  536. {$endif go32v2}
  537. { I don't know where the stack is in other OS !! }
  538. {$ifdef win32}
  539. if (cardinal(p)>=$40000) and (p<=HeapOrg) then
  540. goto _exit;
  541. { inside stack ? }
  542. if (cardinal(startupheapend)<Win32StackTop) and (cardinal(p)>cardinal(startupheapend)) and
  543. (cardinal(p)<Win32StackTop) then
  544. goto _exit;
  545. {$endif win32}
  546. if p>=heapptr then
  547. runerror(216);
  548. { first try valid list faster }
  549. {$ifdef EXTRA}
  550. pp:=heap_valid_last;
  551. while pp<>nil do
  552. begin
  553. { inside this valid block ! }
  554. { we can be changing the extrainfo !! }
  555. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  556. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  557. begin
  558. { check allocated block }
  559. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  560. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  561. { special case of the fill_extra_info call }
  562. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  563. and inside_trace_getmem) then
  564. goto _exit
  565. else
  566. begin
  567. writeln(ptext^,'corrupted heap_mem_info');
  568. dump_error(pp,ptext^);
  569. halt(1);
  570. end;
  571. end
  572. else
  573. pp:=pp^.prev_valid;
  574. inc(i);
  575. if i>getmem_cnt-freemem_cnt then
  576. begin
  577. writeln(ptext^,'error in linked list of heap_mem_info');
  578. halt(1);
  579. end;
  580. end;
  581. i:=0;
  582. {$endif EXTRA}
  583. pp:=heap_mem_root;
  584. while pp<>nil do
  585. begin
  586. { inside this block ! }
  587. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size) and
  588. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  589. { allocated block }
  590. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  591. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  592. goto _exit
  593. else
  594. begin
  595. writeln(ptext^,'pointer $',hexstr(longint(p),8),' points into invalid memory block');
  596. dump_error(pp,ptext^);
  597. runerror(204);
  598. end;
  599. pp:=pp^.previous;
  600. inc(i);
  601. if i>getmem_cnt then
  602. begin
  603. writeln(ptext^,'error in linked list of heap_mem_info');
  604. halt(1);
  605. end;
  606. end;
  607. writeln(ptext^,'pointer $',hexstr(longint(p),8),' does not point to valid memory block');
  608. runerror(204);
  609. _exit:
  610. asm
  611. popal
  612. end;
  613. end;
  614. {*****************************************************************************
  615. Dump Heap
  616. *****************************************************************************}
  617. procedure dumpheap;
  618. var
  619. pp : pheap_mem_info;
  620. i : longint;
  621. begin
  622. pp:=heap_mem_root;
  623. Writeln(ptext^,'Heap dump by heaptrc unit');
  624. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  625. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  626. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  627. Writeln(ptext^,'True heap size : ',system.HeapSize);
  628. Writeln(ptext^,'True free heap : ',MemAvail);
  629. Writeln(ptext^,'Should be : ',system.HeapSize-(getmem8_size-freemem8_size)-
  630. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size));
  631. i:=getmem_cnt-freemem_cnt;
  632. while pp<>nil do
  633. begin
  634. if i<0 then
  635. begin
  636. Writeln(ptext^,'Error in heap memory list');
  637. Writeln(ptext^,'More memory blocks than expected');
  638. exit;
  639. end;
  640. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  641. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  642. begin
  643. { this one was not released !! }
  644. if exitcode<>203 then
  645. call_stack(pp,ptext^);
  646. dec(i);
  647. end
  648. else if pp^.sig<>$AAAAAAAA then
  649. begin
  650. dump_error(pp,ptext^);
  651. {$ifdef EXTRA}
  652. dump_error(pp,error_file);
  653. {$endif EXTRA}
  654. error_in_heap:=true;
  655. end
  656. {$ifdef EXTRA}
  657. else if pp^.release_sig<>calculate_release_sig(pp) then
  658. begin
  659. dump_change_after(pp,ptext^);
  660. dump_change_after(pp,error_file);
  661. error_in_heap:=true;
  662. end
  663. {$endif EXTRA}
  664. ;
  665. pp:=pp^.previous;
  666. end;
  667. end;
  668. procedure markheap;
  669. var
  670. pp : pheap_mem_info;
  671. begin
  672. pp:=heap_mem_root;
  673. while pp<>nil do
  674. begin
  675. pp^.sig:=$AAAAAAAA;
  676. pp:=pp^.previous;
  677. end;
  678. end;
  679. {*****************************************************************************
  680. AllocMem
  681. *****************************************************************************}
  682. function TraceAllocMem(size:longint):Pointer;
  683. begin
  684. TraceAllocMem:=SysAllocMem(size);
  685. end;
  686. {*****************************************************************************
  687. ReAllocMem
  688. *****************************************************************************}
  689. function TraceReAllocMem(var p:pointer;size:longint):Pointer;
  690. var
  691. i,bp : longint;
  692. pl : plongint;
  693. pp : pheap_mem_info;
  694. begin
  695. if not assigned(p) then
  696. begin
  697. p:=TraceGetMem(size);
  698. TraceReallocMem:=P;
  699. exit;
  700. end;
  701. dec(p,sizeof(theap_mem_info)+extra_info_size);
  702. { remove heap_mem_info for linked list }
  703. pp:=pheap_mem_info(p);
  704. if pp^.next<>nil then
  705. pp^.next^.previous:=pp^.previous;
  706. if pp^.previous<>nil then
  707. pp^.previous^.next:=pp^.next;
  708. if pp=heap_mem_root then
  709. heap_mem_root:=heap_mem_root^.previous;
  710. { Do the real ReAllocMem, but alloc also for the info block }
  711. bp:=size+sizeof(theap_mem_info)+extra_info_size;
  712. if add_tail then
  713. inc(bp,sizeof(longint));
  714. p:=SysReAllocMem(p,bp);
  715. { Create the info block }
  716. pheap_mem_info(p)^.sig:=$DEADBEEF;
  717. pheap_mem_info(p)^.size:=size;
  718. if add_tail then
  719. begin
  720. pl:=pointer(p)+bp-sizeof(longint);
  721. pl^:=$DEADBEEF;
  722. end;
  723. bp:=get_caller_frame(get_frame);
  724. for i:=1 to tracesize do
  725. begin
  726. pheap_mem_info(p)^.calls[i]:=get_caller_addr(bp);
  727. bp:=get_caller_frame(bp);
  728. end;
  729. { insert in the linked list }
  730. if heap_mem_root<>nil then
  731. heap_mem_root^.next:=pheap_mem_info(p);
  732. pheap_mem_info(p)^.previous:=heap_mem_root;
  733. pheap_mem_info(p)^.next:=nil;
  734. {$ifdef EXTRA}
  735. pheap_mem_info(p)^.next_valid:=nil;
  736. if assigned(heap_valid_last) then
  737. heap_valid_last^.next_valid:=pheap_mem_info(p);
  738. heap_valid_last:=pheap_mem_info(p);
  739. if not assigned(heap_valid_first) then
  740. heap_valid_first:=pheap_mem_info(p);
  741. {$endif EXTRA}
  742. heap_mem_root:=p;
  743. if assigned(fill_extra_info) then
  744. fill_extra_info(@pheap_mem_info(p)^.extra_info);
  745. { update the pointer }
  746. if usecrc then
  747. pheap_mem_info(p)^.sig:=calculate_sig(pheap_mem_info(p));
  748. inc(p,sizeof(theap_mem_info)+extra_info_size);
  749. inc(getmem_cnt);
  750. TraceReAllocmem:=p;
  751. end;
  752. {*****************************************************************************
  753. No specific tracing calls
  754. *****************************************************************************}
  755. function TraceMemAvail:longint;
  756. begin
  757. TraceMemAvail:=SysMemAvail;
  758. end;
  759. function TraceMaxAvail:longint;
  760. begin
  761. TraceMaxAvail:=SysMaxAvail;
  762. end;
  763. function TraceHeapSize:longint;
  764. begin
  765. TraceHeapSize:=SysHeapSize;
  766. end;
  767. {*****************************************************************************
  768. Install MemoryManager
  769. *****************************************************************************}
  770. const
  771. TraceManager:TMemoryManager=(
  772. Getmem : TraceGetMem;
  773. Freemem : TraceFreeMem;
  774. FreememSize : TraceFreeMemSize;
  775. AllocMem : TraceAllocMem;
  776. ReAllocMem : TraceReAllocMem;
  777. MemSize : TraceMemSize;
  778. MemAvail : TraceMemAvail;
  779. MaxAvail : TraceMaxAvail;
  780. HeapSize : TraceHeapsize;
  781. );
  782. procedure TraceExit;
  783. begin
  784. { no dump if error
  785. because this gives long long listings }
  786. if (exitcode<>0) and (erroraddr<>nil) then
  787. begin
  788. Writeln(ptext^,'No heap dump by heaptrc unit');
  789. Writeln(ptext^,'Exitcode = ',exitcode);
  790. if ptext<>@stderr then
  791. begin
  792. ptext:=@stderr;
  793. close(ownfile);
  794. end;
  795. exit;
  796. end;
  797. if not error_in_heap then
  798. Dumpheap;
  799. if error_in_heap and (exitcode=0) then
  800. exitcode:=203;
  801. {$ifdef EXTRA}
  802. Close(error_file);
  803. {$endif EXTRA}
  804. if ptext<>@stderr then
  805. begin
  806. ptext:=@stderr;
  807. close(ownfile);
  808. end;
  809. end;
  810. Procedure SetHeapTraceOutput(const name : string);
  811. var i : longint;
  812. begin
  813. if ptext<>@stderr then
  814. begin
  815. ptext:=@stderr;
  816. close(ownfile);
  817. end;
  818. assign(ownfile,name);
  819. {$I-}
  820. append(ownfile);
  821. if IOResult<>0 then
  822. Rewrite(ownfile);
  823. {$I+}
  824. ptext:=@ownfile;
  825. for i:=0 to Paramcount do
  826. write(ptext^,paramstr(i),' ');
  827. writeln(ptext^);
  828. end;
  829. procedure SetExtraInfo( size : longint;func : fillextrainfotype);
  830. begin
  831. if getmem_cnt>0 then
  832. begin
  833. writeln(ptext^,'Setting extra info is only possible at start !! ');
  834. dumpheap;
  835. end
  836. else
  837. begin
  838. { the total size must stay multiple of 8 !! }
  839. exact_info_size:=size;
  840. extra_info_size:=((size+7) div 8)*8;
  841. fill_extra_info:=func;
  842. end;
  843. end;
  844. Initialization
  845. MakeCRC32Tbl;
  846. SetMemoryManager(TraceManager);
  847. ptext:=@stderr;
  848. {$ifdef EXTRA}
  849. Assign(error_file,'heap.err');
  850. Rewrite(error_file);
  851. {$endif EXTRA}
  852. { checkpointer init }
  853. {$ifdef go32v2}
  854. Heap_at_init:=HeapPtr;
  855. {$endif}
  856. {$ifdef win32}
  857. StartupHeapEnd:=HeapEnd;
  858. {$endif}
  859. finalization
  860. TraceExit;
  861. end.
  862. {
  863. $Log$
  864. Revision 1.32 2000-01-07 16:32:24 daniel
  865. * copyright 2000 added
  866. Revision 1.31 2000/01/05 13:56:55 jonas
  867. * fixed traceReallocMem with nil pointer (simply calls traceGetMem now in
  868. such a case)
  869. Revision 1.30 2000/01/03 19:37:52 peter
  870. * fixed reallocmem with p=nil
  871. Revision 1.29 1999/11/14 21:35:04 peter
  872. * removed warnings
  873. Revision 1.28 1999/11/09 22:32:23 pierre
  874. * several extra_size_info fixes
  875. Revision 1.27 1999/11/06 14:35:38 peter
  876. * truncated log
  877. Revision 1.26 1999/11/01 13:56:50 peter
  878. * freemem,reallocmem now get var argument
  879. Revision 1.25 1999/10/30 17:39:05 peter
  880. * memorymanager expanded with allocmem/reallocmem
  881. Revision 1.24 1999/09/17 17:14:12 peter
  882. + new heap manager supporting delphi freemem(pointer)
  883. Revision 1.23 1999/09/10 17:13:41 peter
  884. * fixed missing var
  885. Revision 1.22 1999/09/08 16:14:41 peter
  886. * pointer fixes
  887. Revision 1.21 1999/08/18 12:03:16 peter
  888. * objfpc mode for 0.99.12
  889. Revision 1.20 1999/08/17 14:56:03 michael
  890. Removed the mode for objpas
  891. Revision 1.19 1999/07/10 10:33:50 peter
  892. * merged
  893. Revision 1.18 1999/07/09 10:38:10 michael
  894. + + heaptrc now uses finalize instead of exitproc
  895. Revision 1.17 1999/07/05 20:22:08 peter
  896. * merged
  897. Revision 1.16.2.3 1999/07/10 10:31:56 peter
  898. * removed unused var
  899. Revision 1.16.2.2 1999/07/09 10:44:23 michael
  900. + Merged finalize
  901. Revision 1.16 1999/05/23 00:07:17 pierre
  902. * support for heap allocated before TraceGetMem is used in
  903. FPC_CHECKPOINTER
  904. * faster CHECKPOINTER routine (list of valid blocks only !)
  905. Revision 1.15 1999/05/18 22:15:55 pierre
  906. * allow for .bss section below heaporg in go32v2 code
  907. Revision 1.14 1999/05/16 23:56:09 pierre
  908. * allow nil pointer in FPC_CHECKPOINTER !!
  909. Revision 1.13 1999/05/12 16:49:29 pierre
  910. + with EXTRA memory is filled with $F0 and checked at end
  911. Revision 1.12 1999/05/11 12:52:42 pierre
  912. + extra's with -dEXTRA, uses a CRC check for released memory
  913. Revision 1.11 1999/03/26 19:10:34 peter
  914. * show also allocation stack for a wrong size
  915. Revision 1.10 1999/02/16 17:20:26 pierre
  916. * no heap dump if program has an heap error !
  917. Revision 1.9 1999/01/22 12:39:22 pierre
  918. + added text arg for dump_stack
  919. }