heaptrc.pp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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 : longint;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. plongint = ^longint;
  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 : longint = 0;
  68. exact_info_size : longint = 0;
  69. EntryMemUsed : longint = 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 : longint;
  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 longint;
  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 : longint;
  119. getmem_size,
  120. freemem_size : longint;
  121. getmem8_size,
  122. freemem8_size : longint;
  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:Longint):longword;
  145. var
  146. i : longint;
  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 : plongint;
  161. begin
  162. crc:=cardinal($ffffffff);
  163. crc:=UpdateCrc32(crc,p^.size,sizeof(longint));
  164. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(longint));
  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(longint));
  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 : plongint;
  180. begin
  181. crc:=$ffffffff;
  182. crc:=UpdateCrc32(crc,p^.size,sizeof(longint));
  183. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(longint));
  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(longint));
  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 : longint;
  205. begin
  206. writeln(ptext,'Call trace for block 0x',hexstr(longint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
  207. for i:=1 to tracesize do
  208. if pp^.calls[i]<>0 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 : longint;
  219. begin
  220. writeln(ptext,'Call trace for block at 0x',hexstr(longint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
  221. for i:=1 to tracesize div 2 do
  222. if pp^.calls[i]<>0 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]<>0 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 0x',HexStr(longint(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 0x',HexStr(longint(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 : longint;
  251. begin
  252. Writeln(ptext,'Marked memory at 0x',HexStr(longint(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 : longint;var ptext : text);
  263. begin
  264. Writeln(ptext,'Marked memory at 0x',HexStr(longint(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 : longint;
  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:longint):pointer;
  303. var
  304. allocsize,i,bp : longint;
  305. pl : pdword;
  306. p : pointer;
  307. pp : pheap_mem_info;
  308. begin
  309. inc(getmem_size,size);
  310. inc(getmem8_size,((size+7) div 8)*8);
  311. { Do the real GetMem, but alloc also for the info block }
  312. allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
  313. if add_tail then
  314. inc(allocsize,sizeof(longint));
  315. p:=SysGetMem(allocsize);
  316. pp:=pheap_mem_info(p);
  317. inc(p,sizeof(theap_mem_info));
  318. { Create the info block }
  319. pp^.sig:=$DEADBEEF;
  320. pp^.size:=size;
  321. pp^.extra_info_size:=extra_info_size;
  322. pp^.exact_info_size:=exact_info_size;
  323. {
  324. the end of the block contains:
  325. <tail> 4 bytes
  326. <extra_info> X bytes
  327. }
  328. if extra_info_size>0 then
  329. begin
  330. pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
  331. fillchar(pp^.extra_info^,extra_info_size,0);
  332. pp^.extra_info^.check:=$12345678;
  333. pp^.extra_info^.fillproc:=fill_extra_info_proc;
  334. pp^.extra_info^.displayproc:=display_extra_info_proc;
  335. if assigned(fill_extra_info_proc) then
  336. begin
  337. inside_trace_getmem:=true;
  338. fill_extra_info_proc(@pp^.extra_info^.data);
  339. inside_trace_getmem:=false;
  340. end;
  341. end
  342. else
  343. pp^.extra_info:=nil;
  344. if add_tail then
  345. begin
  346. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(longint);
  347. pl^:=$DEADBEEF;
  348. end;
  349. { clear the memory }
  350. fillchar(p^,size,#255);
  351. { retrieve backtrace info }
  352. bp:=get_caller_frame(get_frame);
  353. for i:=1 to tracesize do
  354. begin
  355. pp^.calls[i]:=get_caller_addr(bp);
  356. bp:=get_caller_frame(bp);
  357. end;
  358. { insert in the linked list }
  359. if heap_mem_root<>nil then
  360. heap_mem_root^.next:=pp;
  361. pp^.previous:=heap_mem_root;
  362. pp^.next:=nil;
  363. {$ifdef EXTRA}
  364. pp^.prev_valid:=heap_valid_last;
  365. heap_valid_last:=pp;
  366. if not assigned(heap_valid_first) then
  367. heap_valid_first:=pp;
  368. {$endif EXTRA}
  369. heap_mem_root:=pp;
  370. { must be changed before fill_extra_info is called
  371. because checkpointer can be called from within
  372. fill_extra_info PM }
  373. inc(getmem_cnt);
  374. { update the signature }
  375. if usecrc then
  376. pp^.sig:=calculate_sig(pp);
  377. TraceGetmem:=p;
  378. end;
  379. {*****************************************************************************
  380. TraceFreeMem
  381. *****************************************************************************}
  382. function TraceFreeMemSize(p:pointer;size:longint):longint;
  383. var
  384. i,bp, ppsize : longint;
  385. pp : pheap_mem_info;
  386. {$ifdef EXTRA}
  387. pp2 : pheap_mem_info;
  388. {$endif}
  389. extra_size : longint;
  390. begin
  391. inc(freemem_size,size);
  392. inc(freemem8_size,((size+7) div 8)*8);
  393. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  394. ppsize:= size + sizeof(theap_mem_info)+pp^.extra_info_size;
  395. if add_tail then
  396. inc(ppsize,sizeof(longint));
  397. if not quicktrace then
  398. begin
  399. if not(is_in_getmem_list(pp)) then
  400. RunError(204);
  401. end;
  402. if (pp^.sig=$AAAAAAAA) and not usecrc then
  403. begin
  404. error_in_heap:=true;
  405. dump_already_free(pp,ptext^);
  406. if haltonerror then halt(1);
  407. end
  408. else if ((pp^.sig<>$DEADBEEF) or usecrc) and
  409. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  410. begin
  411. error_in_heap:=true;
  412. dump_error(pp,ptext^);
  413. {$ifdef EXTRA}
  414. dump_error(pp,error_file);
  415. {$endif EXTRA}
  416. { don't release anything in this case !! }
  417. if haltonerror then halt(1);
  418. exit;
  419. end
  420. else if pp^.size<>size then
  421. begin
  422. error_in_heap:=true;
  423. dump_wrong_size(pp,size,ptext^);
  424. {$ifdef EXTRA}
  425. dump_wrong_size(pp,size,error_file);
  426. {$endif EXTRA}
  427. if haltonerror then halt(1);
  428. { don't release anything in this case !! }
  429. exit;
  430. end;
  431. { save old values }
  432. extra_size:=pp^.extra_info_size;
  433. { now it is released !! }
  434. pp^.sig:=$AAAAAAAA;
  435. if not keepreleased then
  436. begin
  437. if pp^.next<>nil then
  438. pp^.next^.previous:=pp^.previous;
  439. if pp^.previous<>nil then
  440. pp^.previous^.next:=pp^.next;
  441. if pp=heap_mem_root then
  442. heap_mem_root:=heap_mem_root^.previous;
  443. end
  444. else
  445. begin
  446. bp:=get_caller_frame(get_frame);
  447. for i:=(tracesize div 2)+1 to tracesize do
  448. begin
  449. pp^.calls[i]:=get_caller_addr(bp);
  450. bp:=get_caller_frame(bp);
  451. end;
  452. end;
  453. inc(freemem_cnt);
  454. { clear the memory }
  455. fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
  456. { this way we keep all info about all released memory !! }
  457. if keepreleased then
  458. begin
  459. {$ifdef EXTRA}
  460. { We want to check if the memory was changed after release !! }
  461. pp^.release_sig:=calculate_release_sig(pp);
  462. if pp=heap_valid_last then
  463. begin
  464. heap_valid_last:=pp^.prev_valid;
  465. if pp=heap_valid_first then
  466. heap_valid_first:=nil;
  467. TraceFreememsize:=size;
  468. exit;
  469. end;
  470. pp2:=heap_valid_last;
  471. while assigned(pp2) do
  472. begin
  473. if pp2^.prev_valid=pp then
  474. begin
  475. pp2^.prev_valid:=pp^.prev_valid;
  476. if pp=heap_valid_first then
  477. heap_valid_first:=pp2;
  478. TraceFreememsize:=size;
  479. exit;
  480. end
  481. else
  482. pp2:=pp2^.prev_valid;
  483. end;
  484. {$endif EXTRA}
  485. TraceFreememsize:=size;
  486. exit;
  487. end;
  488. { release the normal memory at least }
  489. i:=SysFreeMemSize(pp,ppsize);
  490. { return the correct size }
  491. dec(i,sizeof(theap_mem_info)+extra_size);
  492. if add_tail then
  493. dec(i,sizeof(longint));
  494. TraceFreeMemSize:=i;
  495. end;
  496. function TraceMemSize(p:pointer):Longint;
  497. var
  498. l : longint;
  499. pp : pheap_mem_info;
  500. begin
  501. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  502. l:=SysMemSize(pp);
  503. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  504. if add_tail then
  505. dec(l,sizeof(longint));
  506. TraceMemSize:=l;
  507. end;
  508. function TraceFreeMem(p:pointer):longint;
  509. var
  510. size : longint;
  511. pp : pheap_mem_info;
  512. begin
  513. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  514. size:=TraceMemSize(p);
  515. { this can never happend normaly }
  516. if pp^.size>size then
  517. begin
  518. dump_wrong_size(pp,size,ptext^);
  519. {$ifdef EXTRA}
  520. dump_wrong_size(pp,size,error_file);
  521. {$endif EXTRA}
  522. end;
  523. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  524. end;
  525. {*****************************************************************************
  526. ReAllocMem
  527. *****************************************************************************}
  528. function TraceReAllocMem(var p:pointer;size:longint):Pointer;
  529. var
  530. newP: pointer;
  531. oldsize,
  532. allocsize,
  533. i,bp : longint;
  534. pl : pdword;
  535. pp : pheap_mem_info;
  536. oldextrasize,
  537. oldexactsize : longint;
  538. old_fill_extra_info_proc : tfillextrainfoproc;
  539. old_display_extra_info_proc : tdisplayextrainfoproc;
  540. begin
  541. { Free block? }
  542. if size=0 then
  543. begin
  544. if p<>nil then
  545. TraceFreeMem(p);
  546. p:=nil;
  547. TraceReallocMem:=P;
  548. exit;
  549. end;
  550. { Allocate a new block? }
  551. if p=nil then
  552. begin
  553. p:=TraceGetMem(size);
  554. TraceReallocMem:=P;
  555. exit;
  556. end;
  557. { Resize block }
  558. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  559. { test block }
  560. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  561. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  562. begin
  563. error_in_heap:=true;
  564. dump_error(pp,ptext^);
  565. {$ifdef EXTRA}
  566. dump_error(pp,error_file);
  567. {$endif EXTRA}
  568. { don't release anything in this case !! }
  569. if haltonerror then halt(1);
  570. exit;
  571. end;
  572. { save info }
  573. oldextrasize:=pp^.extra_info_size;
  574. oldexactsize:=pp^.exact_info_size;
  575. if pp^.extra_info_size>0 then
  576. begin
  577. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  578. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  579. end;
  580. { Do the real ReAllocMem, but alloc also for the info block }
  581. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  582. if add_tail then
  583. inc(allocsize,sizeof(longint));
  584. { Try to resize the block, if not possible we need to do a
  585. getmem, move data, freemem }
  586. if not SysTryResizeMem(pp,allocsize) then
  587. begin
  588. { get a new block }
  589. oldsize:=TraceMemSize(p);
  590. newP := TraceGetMem(size);
  591. { move the data }
  592. if newP <> nil then
  593. move(p^,newP^,oldsize);
  594. { release p }
  595. traceFreeMem(p);
  596. { return the new pointer }
  597. p:=newp;
  598. traceReAllocMem := newp;
  599. exit;
  600. end;
  601. { adjust like a freemem and then a getmem, so you get correct
  602. results in the summary display }
  603. inc(freemem_size,pp^.size);
  604. inc(freemem8_size,((pp^.size+7) div 8)*8);
  605. inc(getmem_size,size);
  606. inc(getmem8_size,((size+7) div 8)*8);
  607. { Recreate the info block }
  608. pp^.sig:=$DEADBEEF;
  609. pp^.size:=size;
  610. pp^.extra_info_size:=oldextrasize;
  611. pp^.exact_info_size:=oldexactsize;
  612. { add the new extra_info and tail }
  613. if pp^.extra_info_size>0 then
  614. begin
  615. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  616. fillchar(pp^.extra_info^,extra_info_size,0);
  617. pp^.extra_info^.check:=$12345678;
  618. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  619. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  620. if assigned(pp^.extra_info^.fillproc) then
  621. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  622. end
  623. else
  624. pp^.extra_info:=nil;
  625. if add_tail then
  626. begin
  627. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(longint);
  628. pl^:=$DEADBEEF;
  629. end;
  630. { generate new backtrace }
  631. bp:=get_caller_frame(get_frame);
  632. for i:=1 to tracesize do
  633. begin
  634. pp^.calls[i]:=get_caller_addr(bp);
  635. bp:=get_caller_frame(bp);
  636. end;
  637. { regenerate signature }
  638. if usecrc then
  639. pp^.sig:=calculate_sig(pp);
  640. { return the pointer }
  641. p:=pointer(pp)+sizeof(theap_mem_info);
  642. TraceReAllocmem:=p;
  643. end;
  644. {*****************************************************************************
  645. Check pointer
  646. *****************************************************************************}
  647. {$ifndef Unix}
  648. {$S-}
  649. {$endif}
  650. {$ifdef go32v2}
  651. var
  652. __stklen : longword;external name '__stklen';
  653. __stkbottom : longword;external name '__stkbottom';
  654. edata : longword; external name 'edata';
  655. heap_at_init : pointer;
  656. {$endif go32v2}
  657. {$ifdef win32}
  658. var
  659. StartUpHeapEnd : pointer;
  660. { I found no symbol for start of text section :(
  661. so we usee the _mainCRTStartup which should be
  662. in wprt0.ow or wdllprt0.ow PM }
  663. text_begin : longword;external name '_mainCRTStartup';
  664. data_end : longword;external name '__data_end__';
  665. {$endif}
  666. procedure CheckPointer(p : pointer);[saveregisters,public, alias : 'FPC_CHECKPOINTER'];
  667. var
  668. i : longint;
  669. pp : pheap_mem_info;
  670. get_ebp,stack_top : longword;
  671. data_end : longword;
  672. label
  673. _exit;
  674. begin
  675. if p=nil then
  676. runerror(204);
  677. i:=0;
  678. {$ifdef go32v2}
  679. if cardinal(p)<$1000 then
  680. runerror(216);
  681. asm
  682. movl %ebp,get_ebp
  683. leal edata,%eax
  684. movl %eax,data_end
  685. end;
  686. stack_top:=__stkbottom+__stklen;
  687. { allow all between start of code and end of data }
  688. if cardinal(p)<=data_end then
  689. goto _exit;
  690. { .bss section }
  691. if cardinal(p)<=cardinal(heap_at_init) then
  692. goto _exit;
  693. { stack can be above heap !! }
  694. if (cardinal(p)>=get_ebp) and (cardinal(p)<=stack_top) then
  695. goto _exit;
  696. {$endif go32v2}
  697. { I don't know where the stack is in other OS !! }
  698. {$ifdef win32}
  699. if (cardinal(p)>=$40000) and (p<=HeapOrg) then
  700. goto _exit;
  701. { inside stack ? }
  702. asm
  703. movl %ebp,get_ebp
  704. end;
  705. if (cardinal(p)>get_ebp) and
  706. (cardinal(p)<Win32StackTop) then
  707. goto _exit;
  708. {$endif win32}
  709. { first try valid list faster }
  710. {$ifdef EXTRA}
  711. pp:=heap_valid_last;
  712. while pp<>nil do
  713. begin
  714. { inside this valid block ! }
  715. { we can be changing the extrainfo !! }
  716. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  717. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  718. begin
  719. { check allocated block }
  720. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  721. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  722. { special case of the fill_extra_info call }
  723. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  724. and inside_trace_getmem) then
  725. goto _exit
  726. else
  727. begin
  728. writeln(ptext^,'corrupted heap_mem_info');
  729. dump_error(pp,ptext^);
  730. halt(1);
  731. end;
  732. end
  733. else
  734. pp:=pp^.prev_valid;
  735. inc(i);
  736. if i>getmem_cnt-freemem_cnt then
  737. begin
  738. writeln(ptext^,'error in linked list of heap_mem_info');
  739. halt(1);
  740. end;
  741. end;
  742. i:=0;
  743. {$endif EXTRA}
  744. pp:=heap_mem_root;
  745. while pp<>nil do
  746. begin
  747. { inside this block ! }
  748. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info)+cardinal(extra_info_size)) and
  749. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+cardinal(extra_info_size)+cardinal(pp^.size)) then
  750. { allocated block }
  751. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  752. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  753. goto _exit
  754. else
  755. begin
  756. writeln(ptext^,'pointer $',hexstr(longint(p),8),' points into invalid memory block');
  757. dump_error(pp,ptext^);
  758. runerror(204);
  759. end;
  760. pp:=pp^.previous;
  761. inc(i);
  762. if i>getmem_cnt then
  763. begin
  764. writeln(ptext^,'error in linked list of heap_mem_info');
  765. halt(1);
  766. end;
  767. end;
  768. writeln(ptext^,'pointer $',hexstr(longint(p),8),' does not point to valid memory block');
  769. runerror(204);
  770. _exit:
  771. end;
  772. {*****************************************************************************
  773. Dump Heap
  774. *****************************************************************************}
  775. procedure dumpheap;
  776. var
  777. pp : pheap_mem_info;
  778. i : longint;
  779. ExpectedMemAvail : longint;
  780. begin
  781. pp:=heap_mem_root;
  782. Writeln(ptext^,'Heap dump by heaptrc unit');
  783. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  784. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  785. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  786. Write(ptext^,'True heap size : ',system.HeapSize);
  787. if EntryMemUsed > 0 then
  788. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  789. else
  790. Writeln(ptext^);
  791. Writeln(ptext^,'True free heap : ',MemAvail);
  792. ExpectedMemAvail:=system.HeapSize-(getmem8_size-freemem8_size)-
  793. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)-EntryMemUsed;
  794. If ExpectedMemAvail<>MemAvail then
  795. Writeln(ptext^,'Should be : ',ExpectedMemAvail);
  796. i:=getmem_cnt-freemem_cnt;
  797. while pp<>nil do
  798. begin
  799. if i<0 then
  800. begin
  801. Writeln(ptext^,'Error in heap memory list');
  802. Writeln(ptext^,'More memory blocks than expected');
  803. exit;
  804. end;
  805. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  806. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  807. begin
  808. { this one was not released !! }
  809. if exitcode<>203 then
  810. call_stack(pp,ptext^);
  811. dec(i);
  812. end
  813. else if pp^.sig<>$AAAAAAAA then
  814. begin
  815. dump_error(pp,ptext^);
  816. {$ifdef EXTRA}
  817. dump_error(pp,error_file);
  818. {$endif EXTRA}
  819. error_in_heap:=true;
  820. end
  821. {$ifdef EXTRA}
  822. else if pp^.release_sig<>calculate_release_sig(pp) then
  823. begin
  824. dump_change_after(pp,ptext^);
  825. dump_change_after(pp,error_file);
  826. error_in_heap:=true;
  827. end
  828. {$endif EXTRA}
  829. ;
  830. pp:=pp^.previous;
  831. end;
  832. end;
  833. procedure markheap;
  834. var
  835. pp : pheap_mem_info;
  836. begin
  837. pp:=heap_mem_root;
  838. while pp<>nil do
  839. begin
  840. pp^.sig:=$AAAAAAAA;
  841. pp:=pp^.previous;
  842. end;
  843. end;
  844. {*****************************************************************************
  845. AllocMem
  846. *****************************************************************************}
  847. function TraceAllocMem(size:longint):Pointer;
  848. begin
  849. TraceAllocMem:=SysAllocMem(size);
  850. end;
  851. {*****************************************************************************
  852. No specific tracing calls
  853. *****************************************************************************}
  854. function TraceMemAvail:longint;
  855. begin
  856. TraceMemAvail:=SysMemAvail;
  857. end;
  858. function TraceMaxAvail:longint;
  859. begin
  860. TraceMaxAvail:=SysMaxAvail;
  861. end;
  862. function TraceHeapSize:longint;
  863. begin
  864. TraceHeapSize:=SysHeapSize;
  865. end;
  866. {*****************************************************************************
  867. Program Hooks
  868. *****************************************************************************}
  869. Procedure SetHeapTraceOutput(const name : string);
  870. var i : longint;
  871. begin
  872. if ptext<>@stderr then
  873. begin
  874. ptext:=@stderr;
  875. close(ownfile);
  876. end;
  877. assign(ownfile,name);
  878. {$I-}
  879. append(ownfile);
  880. if IOResult<>0 then
  881. Rewrite(ownfile);
  882. {$I+}
  883. ptext:=@ownfile;
  884. for i:=0 to Paramcount do
  885. write(ptext^,paramstr(i),' ');
  886. writeln(ptext^);
  887. end;
  888. procedure SetHeapExtraInfo( size : longint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  889. begin
  890. { the total size must stay multiple of 8, also allocate 2 pointers for
  891. the fill and display procvars }
  892. exact_info_size:=size + sizeof(theap_extra_info);
  893. extra_info_size:=((exact_info_size+7) div 8)*8;
  894. fill_extra_info_proc:=fillproc;
  895. display_extra_info_proc:=displayproc;
  896. end;
  897. {*****************************************************************************
  898. Install MemoryManager
  899. *****************************************************************************}
  900. const
  901. TraceManager:TMemoryManager=(
  902. NeedLock : true;
  903. Getmem : @TraceGetMem;
  904. Freemem : @TraceFreeMem;
  905. FreememSize : @TraceFreeMemSize;
  906. AllocMem : @TraceAllocMem;
  907. ReAllocMem : @TraceReAllocMem;
  908. MemSize : @TraceMemSize;
  909. MemAvail : @TraceMemAvail;
  910. MaxAvail : @TraceMaxAvail;
  911. HeapSize : @TraceHeapsize;
  912. );
  913. procedure TraceInit;
  914. begin
  915. EntryMemUsed:=System.HeapSize-MemAvail;
  916. MakeCRC32Tbl;
  917. SetMemoryManager(TraceManager);
  918. ptext:=@stderr;
  919. if outputstr <> '' then
  920. SetHeapTraceOutput(outputstr);
  921. {$ifdef EXTRA}
  922. Assign(error_file,'heap.err');
  923. Rewrite(error_file);
  924. {$endif EXTRA}
  925. { checkpointer init }
  926. {$ifdef go32v2}
  927. Heap_at_init:=HeapPtr;
  928. {$endif}
  929. {$ifdef win32}
  930. StartupHeapEnd:=HeapEnd;
  931. {$endif}
  932. end;
  933. procedure TraceExit;
  934. begin
  935. { no dump if error
  936. because this gives long long listings }
  937. { clear inoutres, in case the program that quit didn't }
  938. ioresult;
  939. if (exitcode<>0) and (erroraddr<>nil) then
  940. begin
  941. Writeln(ptext^,'No heap dump by heaptrc unit');
  942. Writeln(ptext^,'Exitcode = ',exitcode);
  943. if ptext<>@stderr then
  944. begin
  945. ptext:=@stderr;
  946. close(ownfile);
  947. end;
  948. exit;
  949. end;
  950. if not error_in_heap then
  951. Dumpheap;
  952. if error_in_heap and (exitcode=0) then
  953. exitcode:=203;
  954. {$ifdef EXTRA}
  955. Close(error_file);
  956. {$endif EXTRA}
  957. if ptext<>@stderr then
  958. begin
  959. ptext:=@stderr;
  960. close(ownfile);
  961. end;
  962. end;
  963. {$ifdef win32}
  964. function GetEnvironmentStrings : pchar;
  965. external 'kernel32' name 'GetEnvironmentStringsA';
  966. function FreeEnvironmentStrings(p : pchar) : longbool;
  967. external 'kernel32' name 'FreeEnvironmentStringsA';
  968. Function GetEnv(envvar: string): string;
  969. var
  970. s : string;
  971. i : longint;
  972. hp,p : pchar;
  973. begin
  974. getenv:='';
  975. p:=GetEnvironmentStrings;
  976. hp:=p;
  977. while hp^<>#0 do
  978. begin
  979. s:=strpas(hp);
  980. i:=pos('=',s);
  981. if upcase(copy(s,1,i-1))=upcase(envvar) then
  982. begin
  983. getenv:=copy(s,i+1,length(s)-i);
  984. break;
  985. end;
  986. { next string entry}
  987. hp:=hp+strlen(hp)+1;
  988. end;
  989. FreeEnvironmentStrings(p);
  990. end;
  991. {$else}
  992. Function GetEnv(P:string):Pchar;
  993. {
  994. Searches the environment for a string with name p and
  995. returns a pchar to it's value.
  996. A pchar is used to accomodate for strings of length > 255
  997. }
  998. var
  999. ep : ppchar;
  1000. i : longint;
  1001. found : boolean;
  1002. Begin
  1003. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1004. ep:=envp;
  1005. found:=false;
  1006. if ep<>nil then
  1007. begin
  1008. while (not found) and (ep^<>nil) do
  1009. begin
  1010. found:=true;
  1011. for i:=1 to length(p) do
  1012. if p[i]<>ep^[i-1] then
  1013. begin
  1014. found:=false;
  1015. break;
  1016. end;
  1017. if not found then
  1018. inc(ep);
  1019. end;
  1020. end;
  1021. if found then
  1022. getenv:=ep^+length(p)
  1023. else
  1024. getenv:=nil;
  1025. end;
  1026. {$endif}
  1027. procedure LoadEnvironment;
  1028. var
  1029. i,j : longint;
  1030. s,hs : string;
  1031. begin
  1032. s:=Getenv('HEAPTRC');
  1033. if pos('keepreleased',s)>0 then
  1034. keepreleased:=true;
  1035. if pos('disabled',s)>0 then
  1036. useheaptrace:=false;
  1037. if pos('nohalt',s)>0 then
  1038. haltonerror:=false;
  1039. i:=pos('log=',s);
  1040. if i>0 then
  1041. begin
  1042. outputstr:=copy(s,i+4,255);
  1043. j:=pos(' ',outputstr);
  1044. if j=0 then
  1045. j:=length(outputstr)+1;
  1046. delete(outputstr,j,255);
  1047. end;
  1048. end;
  1049. Initialization
  1050. LoadEnvironment;
  1051. { heaptrc can be disabled from the environment }
  1052. if useheaptrace then
  1053. TraceInit;
  1054. finalization
  1055. if useheaptrace then
  1056. TraceExit;
  1057. end.
  1058. {
  1059. $Log$
  1060. Revision 1.22 2002-12-26 10:46:54 peter
  1061. * set p to nil when 0 is passed to reallocmem
  1062. Revision 1.21 2002/11/30 23:34:43 carl
  1063. * nil should give an error!
  1064. Revision 1.20 2002/10/30 20:39:13 peter
  1065. * MemoryManager record has a field NeedLock if the wrapper functions
  1066. need to provide locking for multithreaded programs
  1067. Revision 1.19 2002/10/05 15:19:46 carl
  1068. * bugfix of assigning to external filename output
  1069. Revision 1.18 2002/09/09 15:45:49 jonas
  1070. * made result type of calculate_release_sig() a longword instead of a
  1071. longint
  1072. Revision 1.17 2002/09/07 15:07:45 peter
  1073. * old logs removed and tabs fixed
  1074. }