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);[saveregisters, 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. if p=nil then
  674. goto _exit;
  675. i:=0;
  676. {$ifdef go32v2}
  677. if cardinal(p)<$1000 then
  678. runerror(216);
  679. asm
  680. movl %ebp,get_ebp
  681. leal edata,%eax
  682. movl %eax,data_end
  683. end;
  684. stack_top:=__stkbottom+__stklen;
  685. { allow all between start of code and end of data }
  686. if cardinal(p)<=data_end then
  687. goto _exit;
  688. { .bss section }
  689. if cardinal(p)<=cardinal(heap_at_init) then
  690. goto _exit;
  691. { stack can be above heap !! }
  692. if (cardinal(p)>=get_ebp) and (cardinal(p)<=stack_top) then
  693. goto _exit;
  694. {$endif go32v2}
  695. { I don't know where the stack is in other OS !! }
  696. {$ifdef win32}
  697. if (cardinal(p)>=$40000) and (p<=HeapOrg) then
  698. goto _exit;
  699. { inside stack ? }
  700. asm
  701. movl %ebp,get_ebp
  702. end;
  703. if (cardinal(p)>get_ebp) and
  704. (cardinal(p)<Win32StackTop) then
  705. goto _exit;
  706. {$endif win32}
  707. if p>=heapptr then
  708. runerror(216);
  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. Getmem : TraceGetMem;
  903. Freemem : TraceFreeMem;
  904. FreememSize : TraceFreeMemSize;
  905. AllocMem : TraceAllocMem;
  906. ReAllocMem : TraceReAllocMem;
  907. MemSize : TraceMemSize;
  908. MemAvail : TraceMemAvail;
  909. MaxAvail : TraceMaxAvail;
  910. HeapSize : TraceHeapsize;
  911. );
  912. procedure TraceInit;
  913. begin
  914. EntryMemUsed:=System.HeapSize-MemAvail;
  915. MakeCRC32Tbl;
  916. SetMemoryManager(TraceManager);
  917. ptext:=@stderr;
  918. {$ifdef EXTRA}
  919. Assign(error_file,'heap.err');
  920. Rewrite(error_file);
  921. {$endif EXTRA}
  922. { checkpointer init }
  923. {$ifdef go32v2}
  924. Heap_at_init:=HeapPtr;
  925. {$endif}
  926. {$ifdef win32}
  927. StartupHeapEnd:=HeapEnd;
  928. {$endif}
  929. end;
  930. procedure TraceExit;
  931. begin
  932. { no dump if error
  933. because this gives long long listings }
  934. { clear inoutres, in case the program that quit didn't }
  935. ioresult;
  936. if (exitcode<>0) and (erroraddr<>nil) then
  937. begin
  938. Writeln(ptext^,'No heap dump by heaptrc unit');
  939. Writeln(ptext^,'Exitcode = ',exitcode);
  940. if ptext<>@stderr then
  941. begin
  942. ptext:=@stderr;
  943. close(ownfile);
  944. end;
  945. exit;
  946. end;
  947. if not error_in_heap then
  948. Dumpheap;
  949. if error_in_heap and (exitcode=0) then
  950. exitcode:=203;
  951. {$ifdef EXTRA}
  952. Close(error_file);
  953. {$endif EXTRA}
  954. if ptext<>@stderr then
  955. begin
  956. ptext:=@stderr;
  957. close(ownfile);
  958. end;
  959. end;
  960. {$ifdef win32}
  961. function GetEnvironmentStrings : pchar;
  962. external 'kernel32' name 'GetEnvironmentStringsA';
  963. function FreeEnvironmentStrings(p : pchar) : longbool;
  964. external 'kernel32' name 'FreeEnvironmentStringsA';
  965. Function GetEnv(envvar: string): string;
  966. var
  967. s : string;
  968. i : longint;
  969. hp,p : pchar;
  970. begin
  971. getenv:='';
  972. p:=GetEnvironmentStrings;
  973. hp:=p;
  974. while hp^<>#0 do
  975. begin
  976. s:=strpas(hp);
  977. i:=pos('=',s);
  978. if upcase(copy(s,1,i-1))=upcase(envvar) then
  979. begin
  980. getenv:=copy(s,i+1,length(s)-i);
  981. break;
  982. end;
  983. { next string entry}
  984. hp:=hp+strlen(hp)+1;
  985. end;
  986. FreeEnvironmentStrings(p);
  987. end;
  988. {$else}
  989. Function GetEnv(P:string):Pchar;
  990. {
  991. Searches the environment for a string with name p and
  992. returns a pchar to it's value.
  993. A pchar is used to accomodate for strings of length > 255
  994. }
  995. var
  996. ep : ppchar;
  997. i : longint;
  998. found : boolean;
  999. Begin
  1000. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1001. ep:=envp;
  1002. found:=false;
  1003. if ep<>nil then
  1004. begin
  1005. while (not found) and (ep^<>nil) do
  1006. begin
  1007. found:=true;
  1008. for i:=1 to length(p) do
  1009. if p[i]<>ep^[i-1] then
  1010. begin
  1011. found:=false;
  1012. break;
  1013. end;
  1014. if not found then
  1015. inc(ep);
  1016. end;
  1017. end;
  1018. if found then
  1019. getenv:=ep^+length(p)
  1020. else
  1021. getenv:=nil;
  1022. end;
  1023. {$endif}
  1024. procedure LoadEnvironment;
  1025. var
  1026. i,j : longint;
  1027. s,hs : string;
  1028. begin
  1029. s:=Getenv('HEAPTRC');
  1030. if pos('keepreleased',s)>0 then
  1031. keepreleased:=true;
  1032. if pos('disabled',s)>0 then
  1033. useheaptrace:=false;
  1034. if pos('nohalt',s)>0 then
  1035. haltonerror:=false;
  1036. i:=pos('log=',s);
  1037. if i>0 then
  1038. begin
  1039. hs:=copy(s,i+4,255);
  1040. j:=pos(' ',hs);
  1041. if j=0 then
  1042. j:=length(hs)+1;
  1043. delete(hs,j,255);
  1044. SetHeapTraceOutput(hs);
  1045. end;
  1046. end;
  1047. Initialization
  1048. LoadEnvironment;
  1049. { heaptrc can be disabled from the environment }
  1050. if useheaptrace then
  1051. TraceInit;
  1052. finalization
  1053. if useheaptrace then
  1054. TraceExit;
  1055. end.
  1056. {
  1057. $Log$
  1058. Revision 1.13 2001-04-23 18:25:44 peter
  1059. * m68k updates
  1060. Revision 1.12 2001/04/21 12:18:09 peter
  1061. * add nop after popa (merged)
  1062. Revision 1.11 2001/04/13 18:05:34 peter
  1063. * win32 getenv version
  1064. Revision 1.10 2001/04/13 01:18:08 peter
  1065. * always clear memory in getmem and freemem
  1066. Revision 1.8 2001/04/11 14:08:31 peter
  1067. * some small fixes to my previous commit
  1068. Revision 1.7 2001/04/11 12:34:50 peter
  1069. * extra info update so it can be always be set on/off
  1070. Revision 1.6 2000/12/16 15:57:17 jonas
  1071. * removed 64bit evaluations when range checking is on
  1072. Revision 1.5 2000/12/07 17:19:47 jonas
  1073. * new constant handling: from now on, hex constants >$7fffffff are
  1074. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1075. and became $ffffffff80000000), all constants in the longint range
  1076. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1077. are cardinals and the rest are int64's.
  1078. * added lots of longint typecast to prevent range check errors in the
  1079. compiler and rtl
  1080. * type casts of symbolic ordinal constants are now preserved
  1081. * fixed bug where the original resulttype wasn't restored correctly
  1082. after doing a 64bit rangecheck
  1083. Revision 1.4 2000/11/13 13:40:03 marco
  1084. * Renamefest
  1085. Revision 1.3 2000/08/24 09:01:07 jonas
  1086. * clear inoutres in traceexit before writing anything (to avoid an RTE
  1087. when writing the heaptrc output when a program didn't handle ioresult)
  1088. (merged from fixes branch)
  1089. Revision 1.2 2000/07/13 11:33:44 michael
  1090. + removed logs
  1091. }