heaptrc.pp 33 KB

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