heaptrc.pp 33 KB

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