heaptrc.pp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. Heap tracer
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit heaptrc;
  13. interface
  14. { 1.0.x doesn't have good rangechecking for cardinals }
  15. {$ifdef VER1_0}
  16. {$R-}
  17. {$endif}
  18. Procedure DumpHeap;
  19. Procedure MarkHeap;
  20. { define EXTRA to add more
  21. tests :
  22. - keep all memory after release and
  23. check by CRC value if not changed after release
  24. WARNING this needs extremely much memory (PM) }
  25. type
  26. tFillExtraInfoProc = procedure(p : pointer);
  27. tdisplayextrainfoProc = procedure (var ptext : text;p : pointer);
  28. { Allows to add info pre memory block, see ppheap.pas of the compiler
  29. for example source }
  30. procedure SetHeapExtraInfo( size : longint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  31. { Redirection of the output to a file }
  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. { install heaptrc memorymanager }
  42. useheaptrace : boolean=true;
  43. { less checking }
  44. quicktrace : boolean=true;
  45. { calls halt() on error by default !! }
  46. HaltOnError : boolean = true;
  47. { set this to true if you suspect that memory
  48. is freed several times }
  49. {$ifdef EXTRA}
  50. keepreleased : boolean=true;
  51. {$else EXTRA}
  52. keepreleased : boolean=false;
  53. {$endif EXTRA}
  54. { add a small footprint at the end of memory blocks, this
  55. can check for memory overwrites at the end of a block }
  56. add_tail : boolean = true;
  57. { put crc in sig
  58. this allows to test for writing into that part }
  59. usecrc : boolean = true;
  60. implementation
  61. type
  62. plongint = ^longint;
  63. const
  64. { allows to add custom info in heap_mem_info, this is the size that will
  65. be allocated for this information }
  66. extra_info_size : longint = 0;
  67. exact_info_size : longint = 0;
  68. EntryMemUsed : longint = 0;
  69. { function to fill this info up }
  70. fill_extra_info_proc : TFillExtraInfoProc = nil;
  71. display_extra_info_proc : TDisplayExtraInfoProc = nil;
  72. error_in_heap : boolean = false;
  73. inside_trace_getmem : boolean = false;
  74. type
  75. pheap_extra_info = ^theap_extra_info;
  76. theap_extra_info = record
  77. check : cardinal; { used to check if the procvar is still valid }
  78. fillproc : tfillextrainfoProc;
  79. displayproc : tdisplayextrainfoProc;
  80. data : record
  81. end;
  82. end;
  83. { warning the size of theap_mem_info
  84. must be a multiple of 8
  85. because otherwise you will get
  86. problems when releasing the usual memory part !!
  87. sizeof(theap_mem_info = 16+tracesize*4 so
  88. tracesize must be even !! PM }
  89. pheap_mem_info = ^theap_mem_info;
  90. theap_mem_info = record
  91. previous,
  92. next : pheap_mem_info;
  93. size : longint;
  94. sig : longword;
  95. {$ifdef EXTRA}
  96. release_sig : longword;
  97. prev_valid : pheap_mem_info;
  98. {$endif EXTRA}
  99. calls : array [1..tracesize] of longint;
  100. exact_info_size : word;
  101. extra_info_size : word;
  102. extra_info : pheap_extra_info;
  103. end;
  104. var
  105. ptext : ^text;
  106. ownfile : text;
  107. {$ifdef EXTRA}
  108. error_file : text;
  109. heap_valid_first,
  110. heap_valid_last : pheap_mem_info;
  111. {$endif EXTRA}
  112. heap_mem_root : pheap_mem_info;
  113. getmem_cnt,
  114. freemem_cnt : longint;
  115. getmem_size,
  116. freemem_size : longint;
  117. getmem8_size,
  118. freemem8_size : longint;
  119. {*****************************************************************************
  120. Crc 32
  121. *****************************************************************************}
  122. var
  123. Crc32Tbl : array[0..255] of longword;
  124. procedure MakeCRC32Tbl;
  125. var
  126. crc : longword;
  127. i,n : byte;
  128. begin
  129. for i:=0 to 255 do
  130. begin
  131. crc:=i;
  132. for n:=1 to 8 do
  133. if odd(crc) then
  134. crc:=(crc shr 1) xor $edb88320
  135. else
  136. crc:=crc shr 1;
  137. Crc32Tbl[i]:=crc;
  138. end;
  139. end;
  140. Function UpdateCrc32(InitCrc:longword;var InBuf;InLen:Longint):longword;
  141. var
  142. i : longint;
  143. p : pchar;
  144. begin
  145. p:=@InBuf;
  146. for i:=1 to InLen do
  147. begin
  148. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  149. inc(p);
  150. end;
  151. UpdateCrc32:=InitCrc;
  152. end;
  153. Function calculate_sig(p : pheap_mem_info) : longword;
  154. var
  155. crc : longword;
  156. pl : plongint;
  157. begin
  158. crc:=longword($ffffffff);
  159. crc:=UpdateCrc32(crc,p^.size,sizeof(longint));
  160. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(longint));
  161. if p^.extra_info_size>0 then
  162. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  163. if add_tail then
  164. begin
  165. { Check also 4 bytes just after allocation !! }
  166. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size;
  167. crc:=UpdateCrc32(crc,pl^,sizeof(longint));
  168. end;
  169. calculate_sig:=crc;
  170. end;
  171. {$ifdef EXTRA}
  172. Function calculate_release_sig(p : pheap_mem_info) : longint;
  173. var
  174. crc : longword;
  175. pl : plongint;
  176. begin
  177. crc:=$ffffffff;
  178. crc:=UpdateCrc32(crc,p^.size,sizeof(longint));
  179. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(longint));
  180. if p^.extra_info_size>0 then
  181. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  182. { Check the whole of the whole allocation }
  183. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info);
  184. crc:=UpdateCrc32(crc,pl^,p^.size);
  185. { Check also 4 bytes just after allocation !! }
  186. if add_tail then
  187. begin
  188. { Check also 4 bytes just after allocation !! }
  189. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size;
  190. crc:=UpdateCrc32(crc,pl^,sizeof(longint));
  191. end;
  192. calculate_release_sig:=crc;
  193. end;
  194. {$endif EXTRA}
  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,BackTraceStrFunc(pp^.calls[i]));
  206. { the check is done to be sure that the procvar is not overwritten }
  207. if assigned(pp^.extra_info) and
  208. (pp^.extra_info^.check=$12345678) and
  209. assigned(pp^.extra_info^.displayproc) then
  210. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  211. end;
  212. procedure call_free_stack(pp : pheap_mem_info;var ptext : text);
  213. var
  214. i : longint;
  215. begin
  216. writeln(ptext,'Call trace for block at 0x',hexstr(longint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
  217. for i:=1 to tracesize div 2 do
  218. if pp^.calls[i]<>0 then
  219. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  220. writeln(ptext,' was released at ');
  221. for i:=(tracesize div 2)+1 to tracesize do
  222. if pp^.calls[i]<>0 then
  223. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  224. { the check is done to be sure that the procvar is not overwritten }
  225. if assigned(pp^.extra_info) and
  226. (pp^.extra_info^.check=$12345678) and
  227. assigned(pp^.extra_info^.displayproc) then
  228. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  229. end;
  230. procedure dump_already_free(p : pheap_mem_info;var ptext : text);
  231. begin
  232. Writeln(ptext,'Marked memory at 0x',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' released');
  233. call_free_stack(p,ptext);
  234. Writeln(ptext,'freed again at');
  235. dump_stack(ptext,get_caller_frame(get_frame));
  236. end;
  237. procedure dump_error(p : pheap_mem_info;var ptext : text);
  238. begin
  239. Writeln(ptext,'Marked memory at 0x',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  240. Writeln(ptext,'Wrong signature $',hexstr(p^.sig,8),' instead of ',hexstr(calculate_sig(p),8));
  241. dump_stack(ptext,get_caller_frame(get_frame));
  242. end;
  243. {$ifdef EXTRA}
  244. procedure dump_change_after(p : pheap_mem_info;var ptext : text);
  245. var pp : pchar;
  246. i : longint;
  247. begin
  248. Writeln(ptext,'Marked memory at 0x',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  249. Writeln(ptext,'Wrong release CRC $',hexstr(p^.release_sig,8),' instead of ',hexstr(calculate_release_sig(p),8));
  250. Writeln(ptext,'This memory was changed after call to freemem !');
  251. call_free_stack(p,ptext);
  252. pp:=pointer(p)+sizeof(theap_mem_info);
  253. for i:=0 to p^.size-1 do
  254. if byte(pp[i])<>$F0 then
  255. Writeln(ptext,'offset',i,':$',hexstr(i,8),'"',pp[i],'"');
  256. end;
  257. {$endif EXTRA}
  258. procedure dump_wrong_size(p : pheap_mem_info;size : longint;var ptext : text);
  259. begin
  260. Writeln(ptext,'Marked memory at 0x',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  261. Writeln(ptext,'Wrong size : ',p^.size,' allocated ',size,' freed');
  262. dump_stack(ptext,get_caller_frame(get_frame));
  263. { the check is done to be sure that the procvar is not overwritten }
  264. if assigned(p^.extra_info) and
  265. (p^.extra_info^.check=$12345678) and
  266. assigned(p^.extra_info^.displayproc) then
  267. p^.extra_info^.displayproc(ptext,@p^.extra_info^.data);
  268. call_stack(p,ptext);
  269. end;
  270. function is_in_getmem_list (p : pheap_mem_info) : boolean;
  271. var
  272. i : longint;
  273. pp : pheap_mem_info;
  274. begin
  275. is_in_getmem_list:=false;
  276. pp:=heap_mem_root;
  277. i:=0;
  278. while pp<>nil do
  279. begin
  280. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  281. ((pp^.sig<>calculate_sig(pp)) or not usecrc) and
  282. (pp^.sig <>$AAAAAAAA) then
  283. begin
  284. writeln(ptext^,'error in linked list of heap_mem_info');
  285. RunError(204);
  286. end;
  287. if pp=p then
  288. is_in_getmem_list:=true;
  289. pp:=pp^.previous;
  290. inc(i);
  291. if i>getmem_cnt-freemem_cnt then
  292. writeln(ptext^,'error in linked list of heap_mem_info');
  293. end;
  294. end;
  295. {*****************************************************************************
  296. TraceGetMem
  297. *****************************************************************************}
  298. Function TraceGetMem(size:longint):pointer;
  299. var
  300. allocsize,i,bp : longint;
  301. pl : pdword;
  302. p : pointer;
  303. pp : pheap_mem_info;
  304. begin
  305. inc(getmem_size,size);
  306. inc(getmem8_size,((size+7) div 8)*8);
  307. { Do the real GetMem, but alloc also for the info block }
  308. allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
  309. if add_tail then
  310. inc(allocsize,sizeof(longint));
  311. p:=SysGetMem(allocsize);
  312. pp:=pheap_mem_info(p);
  313. inc(p,sizeof(theap_mem_info));
  314. { Create the info block }
  315. pp^.sig:=$DEADBEEF;
  316. pp^.size:=size;
  317. pp^.extra_info_size:=extra_info_size;
  318. pp^.exact_info_size:=exact_info_size;
  319. {
  320. the end of the block contains:
  321. <tail> 4 bytes
  322. <extra_info> X bytes
  323. }
  324. if extra_info_size>0 then
  325. begin
  326. pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
  327. fillchar(pp^.extra_info^,extra_info_size,0);
  328. pp^.extra_info^.check:=$12345678;
  329. pp^.extra_info^.fillproc:=fill_extra_info_proc;
  330. pp^.extra_info^.displayproc:=display_extra_info_proc;
  331. if assigned(fill_extra_info_proc) then
  332. begin
  333. inside_trace_getmem:=true;
  334. fill_extra_info_proc(@pp^.extra_info^.data);
  335. inside_trace_getmem:=false;
  336. end;
  337. end
  338. else
  339. pp^.extra_info:=nil;
  340. if add_tail then
  341. begin
  342. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(longint);
  343. pl^:=$DEADBEEF;
  344. end;
  345. { clear the memory }
  346. fillchar(p^,size,#255);
  347. { retrieve backtrace info }
  348. bp:=get_caller_frame(get_frame);
  349. for i:=1 to tracesize do
  350. begin
  351. pp^.calls[i]:=get_caller_addr(bp);
  352. bp:=get_caller_frame(bp);
  353. end;
  354. { insert in the linked list }
  355. if heap_mem_root<>nil then
  356. heap_mem_root^.next:=pp;
  357. pp^.previous:=heap_mem_root;
  358. pp^.next:=nil;
  359. {$ifdef EXTRA}
  360. pp^.prev_valid:=heap_valid_last;
  361. heap_valid_last:=pp;
  362. if not assigned(heap_valid_first) then
  363. heap_valid_first:=pp;
  364. {$endif EXTRA}
  365. heap_mem_root:=pp;
  366. { must be changed before fill_extra_info is called
  367. because checkpointer can be called from within
  368. fill_extra_info PM }
  369. inc(getmem_cnt);
  370. { update the signature }
  371. if usecrc then
  372. pp^.sig:=calculate_sig(pp);
  373. TraceGetmem:=p;
  374. end;
  375. {*****************************************************************************
  376. TraceFreeMem
  377. *****************************************************************************}
  378. function TraceFreeMemSize(var p:pointer;size:longint):longint;
  379. var
  380. i,bp, ppsize : longint;
  381. pp : pheap_mem_info;
  382. {$ifdef EXTRA}
  383. pp2 : pheap_mem_info;
  384. {$endif}
  385. extra_size : longint;
  386. begin
  387. inc(freemem_size,size);
  388. inc(freemem8_size,((size+7) div 8)*8);
  389. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  390. ppsize:= size + sizeof(theap_mem_info)+pp^.extra_info_size;
  391. if add_tail then
  392. inc(ppsize,sizeof(longint));
  393. if not quicktrace then
  394. begin
  395. if not(is_in_getmem_list(pp)) then
  396. RunError(204);
  397. end;
  398. if (pp^.sig=$AAAAAAAA) and not usecrc then
  399. begin
  400. error_in_heap:=true;
  401. dump_already_free(pp,ptext^);
  402. if haltonerror then halt(1);
  403. end
  404. else if ((pp^.sig<>$DEADBEEF) or usecrc) and
  405. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  406. begin
  407. error_in_heap:=true;
  408. dump_error(pp,ptext^);
  409. {$ifdef EXTRA}
  410. dump_error(pp,error_file);
  411. {$endif EXTRA}
  412. { don't release anything in this case !! }
  413. if haltonerror then halt(1);
  414. exit;
  415. end
  416. else if pp^.size<>size then
  417. begin
  418. error_in_heap:=true;
  419. dump_wrong_size(pp,size,ptext^);
  420. {$ifdef EXTRA}
  421. dump_wrong_size(pp,size,error_file);
  422. {$endif EXTRA}
  423. if haltonerror then halt(1);
  424. { don't release anything in this case !! }
  425. exit;
  426. end;
  427. { save old values }
  428. extra_size:=pp^.extra_info_size;
  429. { now it is released !! }
  430. pp^.sig:=$AAAAAAAA;
  431. if not keepreleased then
  432. begin
  433. if pp^.next<>nil then
  434. pp^.next^.previous:=pp^.previous;
  435. if pp^.previous<>nil then
  436. pp^.previous^.next:=pp^.next;
  437. if pp=heap_mem_root then
  438. heap_mem_root:=heap_mem_root^.previous;
  439. end
  440. else
  441. begin
  442. bp:=get_caller_frame(get_frame);
  443. for i:=(tracesize div 2)+1 to tracesize do
  444. begin
  445. pp^.calls[i]:=get_caller_addr(bp);
  446. bp:=get_caller_frame(bp);
  447. end;
  448. end;
  449. inc(freemem_cnt);
  450. { clear the memory }
  451. fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
  452. { this way we keep all info about all released memory !! }
  453. if keepreleased then
  454. begin
  455. {$ifdef EXTRA}
  456. { We want to check if the memory was changed after release !! }
  457. pp^.release_sig:=calculate_release_sig(pp);
  458. if pp=heap_valid_last then
  459. begin
  460. heap_valid_last:=pp^.prev_valid;
  461. if pp=heap_valid_first then
  462. heap_valid_first:=nil;
  463. TraceFreememsize:=size;
  464. p:=nil;
  465. exit;
  466. end;
  467. pp2:=heap_valid_last;
  468. while assigned(pp2) do
  469. begin
  470. if pp2^.prev_valid=pp then
  471. begin
  472. pp2^.prev_valid:=pp^.prev_valid;
  473. if pp=heap_valid_first then
  474. heap_valid_first:=pp2;
  475. TraceFreememsize:=size;
  476. p:=nil;
  477. exit;
  478. end
  479. else
  480. pp2:=pp2^.prev_valid;
  481. end;
  482. {$endif EXTRA}
  483. TraceFreememsize:=size;
  484. p:=nil;
  485. exit;
  486. end;
  487. { release the normal memory at least }
  488. i:=SysFreeMemSize(pp,ppsize);
  489. { return the correct size }
  490. dec(i,sizeof(theap_mem_info)+extra_size);
  491. if add_tail then
  492. dec(i,sizeof(longint));
  493. TraceFreeMemSize:=i;
  494. end;
  495. function TraceMemSize(p:pointer):Longint;
  496. var
  497. l : longint;
  498. pp : pheap_mem_info;
  499. begin
  500. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  501. l:=SysMemSize(pp);
  502. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  503. if add_tail then
  504. dec(l,sizeof(longint));
  505. TraceMemSize:=l;
  506. end;
  507. function TraceFreeMem(var p:pointer):longint;
  508. var
  509. size : longint;
  510. pp : pheap_mem_info;
  511. begin
  512. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  513. size:=TraceMemSize(p);
  514. { this can never happend normaly }
  515. if pp^.size>size then
  516. begin
  517. dump_wrong_size(pp,size,ptext^);
  518. {$ifdef EXTRA}
  519. dump_wrong_size(pp,size,error_file);
  520. {$endif EXTRA}
  521. end;
  522. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  523. end;
  524. {*****************************************************************************
  525. ReAllocMem
  526. *****************************************************************************}
  527. function TraceReAllocMem(var p:pointer;size:longint):Pointer;
  528. var
  529. newP: pointer;
  530. oldsize,
  531. allocsize,
  532. i,bp : longint;
  533. pl : pdword;
  534. pp : pheap_mem_info;
  535. oldextrasize,
  536. oldexactsize : longint;
  537. old_fill_extra_info_proc : tfillextrainfoproc;
  538. old_display_extra_info_proc : tdisplayextrainfoproc;
  539. begin
  540. { Free block? }
  541. if size=0 then
  542. begin
  543. if p<>nil then
  544. TraceFreeMem(p);
  545. TraceReallocMem:=P;
  546. exit;
  547. end;
  548. { Allocate a new block? }
  549. if p=nil then
  550. begin
  551. p:=TraceGetMem(size);
  552. TraceReallocMem:=P;
  553. exit;
  554. end;
  555. { Resize block }
  556. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  557. { test block }
  558. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  559. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  560. begin
  561. error_in_heap:=true;
  562. dump_error(pp,ptext^);
  563. {$ifdef EXTRA}
  564. dump_error(pp,error_file);
  565. {$endif EXTRA}
  566. { don't release anything in this case !! }
  567. if haltonerror then halt(1);
  568. exit;
  569. end;
  570. { save info }
  571. oldextrasize:=pp^.extra_info_size;
  572. oldexactsize:=pp^.exact_info_size;
  573. if pp^.extra_info_size>0 then
  574. begin
  575. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  576. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  577. end;
  578. { Do the real ReAllocMem, but alloc also for the info block }
  579. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  580. if add_tail then
  581. inc(allocsize,sizeof(longint));
  582. { Try to resize the block, if not possible we need to do a
  583. getmem, move data, freemem }
  584. if not SysTryResizeMem(pp,allocsize) then
  585. begin
  586. { get a new block }
  587. oldsize:=TraceMemSize(p);
  588. newP := TraceGetMem(size);
  589. { move the data }
  590. if newP <> nil then
  591. move(p^,newP^,oldsize);
  592. { release p }
  593. traceFreeMem(p);
  594. { return the new pointer }
  595. p:=newp;
  596. traceReAllocMem := newp;
  597. exit;
  598. end;
  599. { adjust like a freemem and then a getmem, so you get correct
  600. results in the summary display }
  601. inc(freemem_size,pp^.size);
  602. inc(freemem8_size,((pp^.size+7) div 8)*8);
  603. inc(getmem_size,size);
  604. inc(getmem8_size,((size+7) div 8)*8);
  605. { Recreate the info block }
  606. pp^.sig:=$DEADBEEF;
  607. pp^.size:=size;
  608. pp^.extra_info_size:=oldextrasize;
  609. pp^.exact_info_size:=oldexactsize;
  610. { add the new extra_info and tail }
  611. if pp^.extra_info_size>0 then
  612. begin
  613. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  614. fillchar(pp^.extra_info^,extra_info_size,0);
  615. pp^.extra_info^.check:=$12345678;
  616. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  617. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  618. if assigned(pp^.extra_info^.fillproc) then
  619. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  620. end
  621. else
  622. pp^.extra_info:=nil;
  623. if add_tail then
  624. begin
  625. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(longint);
  626. pl^:=$DEADBEEF;
  627. end;
  628. { generate new backtrace }
  629. bp:=get_caller_frame(get_frame);
  630. for i:=1 to tracesize do
  631. begin
  632. pp^.calls[i]:=get_caller_addr(bp);
  633. bp:=get_caller_frame(bp);
  634. end;
  635. { regenerate signature }
  636. if usecrc then
  637. pp^.sig:=calculate_sig(pp);
  638. { return the pointer }
  639. p:=pointer(pp)+sizeof(theap_mem_info);
  640. TraceReAllocmem:=p;
  641. end;
  642. {*****************************************************************************
  643. Check pointer
  644. *****************************************************************************}
  645. {$ifndef Unix}
  646. {$S-}
  647. {$endif}
  648. {$ifdef go32v2}
  649. var
  650. __stklen : cardinal;external name '__stklen';
  651. __stkbottom : cardinal;external name '__stkbottom';
  652. edata : cardinal; external name 'edata';
  653. heap_at_init : pointer;
  654. {$endif go32v2}
  655. {$ifdef win32}
  656. var
  657. StartUpHeapEnd : pointer;
  658. { I found no symbol for start of text section :(
  659. so we usee the _mainCRTStartup which should be
  660. in wprt0.ow or wdllprt0.ow PM }
  661. text_begin : cardinal;external name '_mainCRTStartup';
  662. data_end : cardinal;external name '__data_end__';
  663. {$endif}
  664. procedure CheckPointer(p : pointer);[public, alias : 'FPC_CHECKPOINTER'];
  665. var
  666. i : longint;
  667. pp : pheap_mem_info;
  668. get_ebp,stack_top : cardinal;
  669. data_end : cardinal;
  670. label
  671. _exit;
  672. begin
  673. asm
  674. pushal
  675. end;
  676. if p=nil then
  677. goto _exit;
  678. i:=0;
  679. {$ifdef go32v2}
  680. if cardinal(p)<$1000 then
  681. runerror(216);
  682. asm
  683. movl %ebp,get_ebp
  684. leal edata,%eax
  685. movl %eax,data_end
  686. end;
  687. stack_top:=__stkbottom+__stklen;
  688. { allow all between start of code and end of data }
  689. if cardinal(p)<=data_end then
  690. goto _exit;
  691. { .bss section }
  692. if cardinal(p)<=cardinal(heap_at_init) then
  693. goto _exit;
  694. { stack can be above heap !! }
  695. if (cardinal(p)>=get_ebp) and (cardinal(p)<=stack_top) then
  696. goto _exit;
  697. {$endif go32v2}
  698. { I don't know where the stack is in other OS !! }
  699. {$ifdef win32}
  700. if (cardinal(p)>=$40000) and (p<=HeapOrg) then
  701. goto _exit;
  702. { inside stack ? }
  703. asm
  704. movl %ebp,get_ebp
  705. end;
  706. if (cardinal(p)>get_ebp) and
  707. (cardinal(p)<Win32StackTop) then
  708. goto _exit;
  709. {$endif win32}
  710. if p>=heapptr then
  711. runerror(216);
  712. { first try valid list faster }
  713. {$ifdef EXTRA}
  714. pp:=heap_valid_last;
  715. while pp<>nil do
  716. begin
  717. { inside this valid block ! }
  718. { we can be changing the extrainfo !! }
  719. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  720. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  721. begin
  722. { check allocated block }
  723. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  724. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  725. { special case of the fill_extra_info call }
  726. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  727. and inside_trace_getmem) then
  728. goto _exit
  729. else
  730. begin
  731. writeln(ptext^,'corrupted heap_mem_info');
  732. dump_error(pp,ptext^);
  733. halt(1);
  734. end;
  735. end
  736. else
  737. pp:=pp^.prev_valid;
  738. inc(i);
  739. if i>getmem_cnt-freemem_cnt then
  740. begin
  741. writeln(ptext^,'error in linked list of heap_mem_info');
  742. halt(1);
  743. end;
  744. end;
  745. i:=0;
  746. {$endif EXTRA}
  747. pp:=heap_mem_root;
  748. while pp<>nil do
  749. begin
  750. { inside this block ! }
  751. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info)+cardinal(extra_info_size)) and
  752. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+cardinal(extra_info_size)+cardinal(pp^.size)) then
  753. { allocated block }
  754. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  755. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  756. goto _exit
  757. else
  758. begin
  759. writeln(ptext^,'pointer $',hexstr(longint(p),8),' points into invalid memory block');
  760. dump_error(pp,ptext^);
  761. runerror(204);
  762. end;
  763. pp:=pp^.previous;
  764. inc(i);
  765. if i>getmem_cnt then
  766. begin
  767. writeln(ptext^,'error in linked list of heap_mem_info');
  768. halt(1);
  769. end;
  770. end;
  771. writeln(ptext^,'pointer $',hexstr(longint(p),8),' does not point to valid memory block');
  772. runerror(204);
  773. _exit:
  774. asm
  775. popal
  776. end;
  777. end;
  778. {*****************************************************************************
  779. Dump Heap
  780. *****************************************************************************}
  781. procedure dumpheap;
  782. var
  783. pp : pheap_mem_info;
  784. i : longint;
  785. ExpectedMemAvail : longint;
  786. begin
  787. pp:=heap_mem_root;
  788. Writeln(ptext^,'Heap dump by heaptrc unit');
  789. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  790. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  791. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  792. Write(ptext^,'True heap size : ',system.HeapSize);
  793. if EntryMemUsed > 0 then
  794. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  795. else
  796. Writeln(ptext^);
  797. Writeln(ptext^,'True free heap : ',MemAvail);
  798. ExpectedMemAvail:=system.HeapSize-(getmem8_size-freemem8_size)-
  799. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)-EntryMemUsed;
  800. If ExpectedMemAvail<>MemAvail then
  801. Writeln(ptext^,'Should be : ',ExpectedMemAvail);
  802. i:=getmem_cnt-freemem_cnt;
  803. while pp<>nil do
  804. begin
  805. if i<0 then
  806. begin
  807. Writeln(ptext^,'Error in heap memory list');
  808. Writeln(ptext^,'More memory blocks than expected');
  809. exit;
  810. end;
  811. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  812. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  813. begin
  814. { this one was not released !! }
  815. if exitcode<>203 then
  816. call_stack(pp,ptext^);
  817. dec(i);
  818. end
  819. else if pp^.sig<>$AAAAAAAA then
  820. begin
  821. dump_error(pp,ptext^);
  822. {$ifdef EXTRA}
  823. dump_error(pp,error_file);
  824. {$endif EXTRA}
  825. error_in_heap:=true;
  826. end
  827. {$ifdef EXTRA}
  828. else if pp^.release_sig<>calculate_release_sig(pp) then
  829. begin
  830. dump_change_after(pp,ptext^);
  831. dump_change_after(pp,error_file);
  832. error_in_heap:=true;
  833. end
  834. {$endif EXTRA}
  835. ;
  836. pp:=pp^.previous;
  837. end;
  838. end;
  839. procedure markheap;
  840. var
  841. pp : pheap_mem_info;
  842. begin
  843. pp:=heap_mem_root;
  844. while pp<>nil do
  845. begin
  846. pp^.sig:=$AAAAAAAA;
  847. pp:=pp^.previous;
  848. end;
  849. end;
  850. {*****************************************************************************
  851. AllocMem
  852. *****************************************************************************}
  853. function TraceAllocMem(size:longint):Pointer;
  854. begin
  855. TraceAllocMem:=SysAllocMem(size);
  856. end;
  857. {*****************************************************************************
  858. No specific tracing calls
  859. *****************************************************************************}
  860. function TraceMemAvail:longint;
  861. begin
  862. TraceMemAvail:=SysMemAvail;
  863. end;
  864. function TraceMaxAvail:longint;
  865. begin
  866. TraceMaxAvail:=SysMaxAvail;
  867. end;
  868. function TraceHeapSize:longint;
  869. begin
  870. TraceHeapSize:=SysHeapSize;
  871. end;
  872. {*****************************************************************************
  873. Program Hooks
  874. *****************************************************************************}
  875. Procedure SetHeapTraceOutput(const name : string);
  876. var i : longint;
  877. begin
  878. if ptext<>@stderr then
  879. begin
  880. ptext:=@stderr;
  881. close(ownfile);
  882. end;
  883. assign(ownfile,name);
  884. {$I-}
  885. append(ownfile);
  886. if IOResult<>0 then
  887. Rewrite(ownfile);
  888. {$I+}
  889. ptext:=@ownfile;
  890. for i:=0 to Paramcount do
  891. write(ptext^,paramstr(i),' ');
  892. writeln(ptext^);
  893. end;
  894. procedure SetHeapExtraInfo( size : longint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  895. begin
  896. { the total size must stay multiple of 8, also allocate 2 pointers for
  897. the fill and display procvars }
  898. exact_info_size:=size + sizeof(theap_extra_info);
  899. extra_info_size:=((exact_info_size+7) div 8)*8;
  900. fill_extra_info_proc:=fillproc;
  901. display_extra_info_proc:=displayproc;
  902. end;
  903. {*****************************************************************************
  904. Install MemoryManager
  905. *****************************************************************************}
  906. const
  907. TraceManager:TMemoryManager=(
  908. Getmem : TraceGetMem;
  909. Freemem : TraceFreeMem;
  910. FreememSize : TraceFreeMemSize;
  911. AllocMem : TraceAllocMem;
  912. ReAllocMem : TraceReAllocMem;
  913. MemSize : TraceMemSize;
  914. MemAvail : TraceMemAvail;
  915. MaxAvail : TraceMaxAvail;
  916. HeapSize : TraceHeapsize;
  917. );
  918. procedure TraceInit;
  919. begin
  920. EntryMemUsed:=System.HeapSize-MemAvail;
  921. MakeCRC32Tbl;
  922. SetMemoryManager(TraceManager);
  923. ptext:=@stderr;
  924. {$ifdef EXTRA}
  925. Assign(error_file,'heap.err');
  926. Rewrite(error_file);
  927. {$endif EXTRA}
  928. { checkpointer init }
  929. {$ifdef go32v2}
  930. Heap_at_init:=HeapPtr;
  931. {$endif}
  932. {$ifdef win32}
  933. StartupHeapEnd:=HeapEnd;
  934. {$endif}
  935. end;
  936. procedure TraceExit;
  937. begin
  938. { no dump if error
  939. because this gives long long listings }
  940. { clear inoutres, in case the program that quit didn't }
  941. ioresult;
  942. if (exitcode<>0) and (erroraddr<>nil) then
  943. begin
  944. Writeln(ptext^,'No heap dump by heaptrc unit');
  945. Writeln(ptext^,'Exitcode = ',exitcode);
  946. if ptext<>@stderr then
  947. begin
  948. ptext:=@stderr;
  949. close(ownfile);
  950. end;
  951. exit;
  952. end;
  953. if not error_in_heap then
  954. Dumpheap;
  955. if error_in_heap and (exitcode=0) then
  956. exitcode:=203;
  957. {$ifdef EXTRA}
  958. Close(error_file);
  959. {$endif EXTRA}
  960. if ptext<>@stderr then
  961. begin
  962. ptext:=@stderr;
  963. close(ownfile);
  964. end;
  965. end;
  966. {$ifdef win32}
  967. function GetEnvironmentStrings : pchar;
  968. external 'kernel32' name 'GetEnvironmentStringsA';
  969. function FreeEnvironmentStrings(p : pchar) : longbool;
  970. external 'kernel32' name 'FreeEnvironmentStringsA';
  971. Function GetEnv(envvar: string): string;
  972. var
  973. s : string;
  974. i : longint;
  975. hp,p : pchar;
  976. begin
  977. getenv:='';
  978. p:=GetEnvironmentStrings;
  979. hp:=p;
  980. while hp^<>#0 do
  981. begin
  982. s:=strpas(hp);
  983. i:=pos('=',s);
  984. if upcase(copy(s,1,i-1))=upcase(envvar) then
  985. begin
  986. getenv:=copy(s,i+1,length(s)-i);
  987. break;
  988. end;
  989. { next string entry}
  990. hp:=hp+strlen(hp)+1;
  991. end;
  992. FreeEnvironmentStrings(p);
  993. end;
  994. {$else}
  995. Function GetEnv(P:string):Pchar;
  996. {
  997. Searches the environment for a string with name p and
  998. returns a pchar to it's value.
  999. A pchar is used to accomodate for strings of length > 255
  1000. }
  1001. var
  1002. ep : ppchar;
  1003. i : longint;
  1004. found : boolean;
  1005. Begin
  1006. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1007. ep:=envp;
  1008. found:=false;
  1009. if ep<>nil then
  1010. begin
  1011. while (not found) and (ep^<>nil) do
  1012. begin
  1013. found:=true;
  1014. for i:=1 to length(p) do
  1015. if p[i]<>ep^[i-1] then
  1016. begin
  1017. found:=false;
  1018. break;
  1019. end;
  1020. if not found then
  1021. inc(ep);
  1022. end;
  1023. end;
  1024. if found then
  1025. getenv:=ep^+length(p)
  1026. else
  1027. getenv:=nil;
  1028. end;
  1029. {$endif}
  1030. procedure LoadEnvironment;
  1031. var
  1032. i,j : longint;
  1033. s,hs : string;
  1034. begin
  1035. s:=Getenv('HEAPTRC');
  1036. if pos('keepreleased',s)>0 then
  1037. keepreleased:=true;
  1038. if pos('disabled',s)>0 then
  1039. useheaptrace:=false;
  1040. if pos('nohalt',s)>0 then
  1041. haltonerror:=false;
  1042. i:=pos('log=',s);
  1043. if i>0 then
  1044. begin
  1045. hs:=copy(s,i+4,255);
  1046. j:=pos(' ',hs);
  1047. if j=0 then
  1048. j:=length(hs)+1;
  1049. delete(hs,j,255);
  1050. SetHeapTraceOutput(hs);
  1051. end;
  1052. end;
  1053. Initialization
  1054. LoadEnvironment;
  1055. { heaptrc can be disabled from the environment }
  1056. if useheaptrace then
  1057. TraceInit;
  1058. finalization
  1059. if useheaptrace then
  1060. TraceExit;
  1061. end.
  1062. {
  1063. $Log$
  1064. Revision 1.11 2001-04-13 18:05:34 peter
  1065. * win32 getenv version
  1066. Revision 1.10 2001/04/13 01:18:08 peter
  1067. * always clear memory in getmem and freemem
  1068. Revision 1.8 2001/04/11 14:08:31 peter
  1069. * some small fixes to my previous commit
  1070. Revision 1.7 2001/04/11 12:34:50 peter
  1071. * extra info update so it can be always be set on/off
  1072. Revision 1.6 2000/12/16 15:57:17 jonas
  1073. * removed 64bit evaluations when range checking is on
  1074. Revision 1.5 2000/12/07 17:19:47 jonas
  1075. * new constant handling: from now on, hex constants >$7fffffff are
  1076. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1077. and became $ffffffff80000000), all constants in the longint range
  1078. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1079. are cardinals and the rest are int64's.
  1080. * added lots of longint typecast to prevent range check errors in the
  1081. compiler and rtl
  1082. * type casts of symbolic ordinal constants are now preserved
  1083. * fixed bug where the original resulttype wasn't restored correctly
  1084. after doing a 64bit rangecheck
  1085. Revision 1.4 2000/11/13 13:40:03 marco
  1086. * Renamefest
  1087. Revision 1.3 2000/08/24 09:01:07 jonas
  1088. * clear inoutres in traceexit before writing anything (to avoid an RTE
  1089. when writing the heaptrc output when a program didn't handle ioresult)
  1090. (merged from fixes branch)
  1091. Revision 1.2 2000/07/13 11:33:44 michael
  1092. + removed logs
  1093. }