heaptrc.pp 27 KB

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