heaptrc.pp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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. {$checkpointer off}
  12. unit heaptrc;
  13. interface
  14. {$inline on}
  15. {$ifdef FPC_HEAPTRC_EXTRA}
  16. {$define EXTRA}
  17. {$inline off}
  18. {$endif FPC_HEAPTRC_EXTRA}
  19. {$TYPEDADDRESS on}
  20. {$if defined(win32) or defined(wince)}
  21. {$define windows}
  22. {$endif}
  23. Procedure DumpHeap;
  24. Procedure DumpHeap(SkipIfNoLeaks : Boolean);
  25. { define EXTRA to add more
  26. tests :
  27. - keep all memory after release and
  28. check by CRC value if not changed after release
  29. WARNING this needs extremely much memory (PM) }
  30. type
  31. tFillExtraInfoProc = procedure(p : pointer);
  32. tdisplayextrainfoProc = procedure (var ptext : text;p : pointer);
  33. { Allows to add info pre memory block, see ppheap.pas of the compiler
  34. for example source }
  35. procedure SetHeapExtraInfo(size : ptruint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  36. { Redirection of the output to a file }
  37. procedure SetHeapTraceOutput(const name : string);overload;
  38. procedure SetHeapTraceOutput(var ATextOutput : Text);overload;
  39. procedure CheckPointer(p : pointer);
  40. const
  41. { tracing level
  42. splitted in two if memory is released !! }
  43. {$ifdef EXTRA}
  44. tracesize = 16;
  45. {$else EXTRA}
  46. tracesize = 8;
  47. {$endif EXTRA}
  48. { install heaptrc memorymanager }
  49. useheaptrace : boolean=true;
  50. { less checking }
  51. quicktrace : boolean=true;
  52. { calls halt() on error by default !! }
  53. HaltOnError : boolean = true;
  54. { Halt on exit if any memory was not freed }
  55. HaltOnNotReleased : boolean = false;
  56. { set this to true if you suspect that memory
  57. is freed several times }
  58. {$ifdef EXTRA}
  59. keepreleased : boolean=true;
  60. {$else EXTRA}
  61. keepreleased : boolean=false;
  62. {$endif EXTRA}
  63. { add a small footprint at the end of memory blocks, this
  64. can check for memory overwrites at the end of a block }
  65. add_tail : boolean = true;
  66. tail_size : longint = sizeof(ptruint);
  67. { put crc in sig
  68. this allows to test for writing into that part }
  69. usecrc : boolean = true;
  70. printleakedblock: boolean = false;
  71. printfaultyblock: boolean = false;
  72. maxprintedblocklength: integer = 128;
  73. GlobalSkipIfNoLeaks : Boolean = False;
  74. implementation
  75. const
  76. { allows to add custom info in heap_mem_info, this is the size that will
  77. be allocated for this information }
  78. extra_info_size : ptruint = 0;
  79. exact_info_size : ptruint = 0;
  80. EntryMemUsed : ptruint = 0;
  81. { function to fill this info up }
  82. fill_extra_info_proc : TFillExtraInfoProc = nil;
  83. display_extra_info_proc : TDisplayExtraInfoProc = nil;
  84. { indicates where the output will be redirected }
  85. { only set using environment variables }
  86. outputstr : shortstring = '';
  87. ReleaseSig = $AAAAAAAA;
  88. AllocateSig = $DEADBEEF;
  89. CheckSig = $12345678;
  90. type
  91. pheap_extra_info = ^theap_extra_info;
  92. theap_extra_info = record
  93. check : cardinal; { used to check if the procvar is still valid }
  94. fillproc : tfillextrainfoProc;
  95. displayproc : tdisplayextrainfoProc;
  96. data : record
  97. end;
  98. end;
  99. ppheap_mem_info = ^pheap_mem_info;
  100. pheap_mem_info = ^theap_mem_info;
  101. { warning the size of theap_mem_info
  102. must be a multiple of 8
  103. because otherwise you will get
  104. problems when releasing the usual memory part !!
  105. sizeof(theap_mem_info = 16+tracesize*4 so
  106. tracesize must be even !! PM }
  107. theap_mem_info = record
  108. previous,
  109. next : pheap_mem_info;
  110. todolist : ppheap_mem_info;
  111. todonext : pheap_mem_info;
  112. size : ptruint;
  113. sig : longword;
  114. {$ifdef EXTRA}
  115. release_sig : longword;
  116. prev_valid : pheap_mem_info;
  117. {$endif EXTRA}
  118. calls : array [1..tracesize] of codepointer;
  119. exact_info_size : word;
  120. extra_info_size : word;
  121. extra_info : pheap_extra_info;
  122. end;
  123. pheap_info = ^theap_info;
  124. theap_info = record
  125. {$ifdef EXTRA}
  126. heap_valid_first,
  127. heap_valid_last : pheap_mem_info;
  128. {$endif EXTRA}
  129. heap_mem_root : pheap_mem_info;
  130. heap_free_todo : pheap_mem_info;
  131. getmem_cnt,
  132. freemem_cnt : ptruint;
  133. getmem_size,
  134. freemem_size : ptruint;
  135. getmem8_size,
  136. freemem8_size : ptruint;
  137. error_in_heap : boolean;
  138. inside_trace_getmem : boolean;
  139. end;
  140. var
  141. useownfile, useowntextoutput : boolean;
  142. ownfile : text;
  143. {$ifdef EXTRA}
  144. error_file : text;
  145. {$endif EXTRA}
  146. main_orig_todolist: ppheap_mem_info;
  147. main_relo_todolist: ppheap_mem_info;
  148. orphaned_info: theap_info;
  149. todo_lock: trtlcriticalsection;
  150. textoutput : ^text;
  151. threadvar
  152. heap_info: theap_info;
  153. {*****************************************************************************
  154. Crc 32
  155. *****************************************************************************}
  156. var
  157. Crc32Tbl : array[0..255] of longword;
  158. const
  159. Crc32Seed = $ffffffff;
  160. Crc32Pattern = $edb88320;
  161. procedure MakeCRC32Tbl;
  162. var
  163. crc : longword;
  164. i,n : byte;
  165. begin
  166. for i:=0 to 255 do
  167. begin
  168. crc:=i;
  169. for n:=1 to 8 do
  170. if odd(crc) then
  171. crc:=(crc shr 1) xor longword(CRC32Pattern)
  172. else
  173. crc:=crc shr 1;
  174. Crc32Tbl[i]:=crc;
  175. end;
  176. end;
  177. Function UpdateCrc32(InitCrc:longword;var InBuf;InLen:ptruint):longword;
  178. var
  179. i : ptruint;
  180. p : pchar;
  181. begin
  182. p:=@InBuf;
  183. for i:=1 to InLen do
  184. begin
  185. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  186. inc(p);
  187. end;
  188. UpdateCrc32:=InitCrc;
  189. end;
  190. Function calculate_sig(p : pheap_mem_info) : longword;
  191. var
  192. crc : longword;
  193. pl : pptruint;
  194. begin
  195. crc:=longword(CRC32Seed);
  196. crc:=UpdateCrc32(crc,p^.size,sizeof(ptruint));
  197. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(codepointer));
  198. if p^.extra_info_size>0 then
  199. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  200. if add_tail then
  201. begin
  202. { Check also 4 bytes just after allocation !! }
  203. pl:=pointer(p)+sizeof(theap_mem_info)+p^.size;
  204. crc:=UpdateCrc32(crc,pl^,tail_size);
  205. end;
  206. calculate_sig:=crc;
  207. end;
  208. {$ifdef EXTRA}
  209. Function calculate_release_sig(p : pheap_mem_info) : longword;
  210. var
  211. crc : longword;
  212. pl : pptruint;
  213. begin
  214. crc:=longword(CRC32Seed);
  215. crc:=UpdateCrc32(crc,p^.size,sizeof(ptruint));
  216. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(codepointer));
  217. if p^.extra_info_size>0 then
  218. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  219. { Check the whole of the whole allocation }
  220. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info);
  221. crc:=UpdateCrc32(crc,pl^,p^.size);
  222. { Check also 4 bytes just after allocation !! }
  223. if add_tail then
  224. begin
  225. { Check also 4 bytes just after allocation !! }
  226. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size;
  227. crc:=UpdateCrc32(crc,pl^,tail_size);
  228. end;
  229. calculate_release_sig:=crc;
  230. end;
  231. {$endif EXTRA}
  232. {*****************************************************************************
  233. Helpers
  234. *****************************************************************************}
  235. function InternalFreeMemSize(loc_info: pheap_info; p: pointer; pp: pheap_mem_info;
  236. size: ptruint; release_todo_lock: boolean): ptruint; forward;
  237. function TraceFreeMem(p: pointer): ptruint; forward;
  238. procedure printhex(p : pointer; const size : PtrUInt; var ptext : text);
  239. var s: PtrUInt;
  240. i: Integer;
  241. begin
  242. s := size;
  243. if s > maxprintedblocklength then
  244. s := maxprintedblocklength;
  245. for i:=0 to s-1 do
  246. write(ptext, hexstr(pbyte(p + i)^,2));
  247. if size > maxprintedblocklength then
  248. writeln(ptext,'.. - ')
  249. else
  250. writeln(ptext, ' - ');
  251. for i:=0 to s-1 do
  252. if pchar(p + sizeof(theap_mem_info) + i)^ < ' ' then
  253. write(ptext, ' ')
  254. else
  255. write(ptext, pchar(p + i)^);
  256. if size > maxprintedblocklength then
  257. writeln(ptext,'..')
  258. else
  259. writeln(ptext);
  260. end;
  261. procedure call_stack(pp : pheap_mem_info;var ptext : text);
  262. var
  263. i : ptruint;
  264. begin
  265. writeln(ptext,'Call trace for block $',hexstr(pointer(pp)+sizeof(theap_mem_info)),' size ',pp^.size);
  266. if printleakedblock then
  267. begin
  268. write(ptext, 'Block content: ');
  269. printhex(pointer(pp) + sizeof(theap_mem_info), pp^.size, ptext);
  270. end;
  271. for i:=1 to tracesize do
  272. if pp^.calls[i]<>nil then
  273. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  274. { the check is done to be sure that the procvar is not overwritten }
  275. if assigned(pp^.extra_info) and
  276. (pp^.extra_info^.check=cardinal(CheckSig)) and
  277. assigned(pp^.extra_info^.displayproc) then
  278. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  279. end;
  280. procedure call_free_stack(pp : pheap_mem_info;var ptext : text);
  281. var
  282. i : ptruint;
  283. begin
  284. writeln(ptext,'Call trace for block at $',hexstr(pointer(pp)+sizeof(theap_mem_info)),' size ',pp^.size);
  285. for i:=1 to tracesize div 2 do
  286. if pp^.calls[i]<>nil then
  287. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  288. writeln(ptext,' was released at ');
  289. for i:=(tracesize div 2)+1 to tracesize do
  290. if pp^.calls[i]<>nil then
  291. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  292. { the check is done to be sure that the procvar is not overwritten }
  293. if assigned(pp^.extra_info) and
  294. (pp^.extra_info^.check=cardinal(CheckSig)) and
  295. assigned(pp^.extra_info^.displayproc) then
  296. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  297. end;
  298. procedure dump_already_free(p : pheap_mem_info;var ptext : text);
  299. begin
  300. Writeln(ptext,'Marked memory at $',HexStr(pointer(p)+sizeof(theap_mem_info)),' released');
  301. call_free_stack(p,ptext);
  302. Writeln(ptext,'freed again at');
  303. dump_stack(ptext,1);
  304. end;
  305. procedure dump_error(p : pheap_mem_info;var ptext : text);
  306. begin
  307. Writeln(ptext,'Marked memory at $',HexStr(pointer(p)+sizeof(theap_mem_info)),' invalid');
  308. Writeln(ptext,'Wrong signature $',hexstr(p^.sig,8),' instead of ',hexstr(calculate_sig(p),8));
  309. if printfaultyblock then
  310. begin
  311. write(ptext, 'Block content: ');
  312. printhex(pointer(p) + sizeof(theap_mem_info), p^.size, ptext);
  313. end;
  314. dump_stack(ptext,1);
  315. end;
  316. function released_modified(p : pheap_mem_info;var ptext : text) : boolean;
  317. var pl : pdword;
  318. pb : pbyte;
  319. i : longint;
  320. begin
  321. released_modified:=false;
  322. { Check tail_size bytes just after allocation !! }
  323. pl:=pointer(p)+sizeof(theap_mem_info)+p^.size;
  324. pb:=pointer(p)+sizeof(theap_mem_info);
  325. for i:=0 to p^.size-1 do
  326. if pb[i]<>$F0 then
  327. begin
  328. Writeln(ptext,'offset',i,':$',hexstr(i,2*sizeof(pointer)),'"',hexstr(pb[i],2),'"');
  329. released_modified:=true;
  330. end;
  331. for i:=1 to (tail_size div sizeof(dword)) do
  332. begin
  333. if unaligned(pl^) <> AllocateSig then
  334. begin
  335. released_modified:=true;
  336. writeln(ptext,'Tail modified after release at pos ',i*sizeof(ptruint));
  337. printhex(pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size,tail_size,ptext);
  338. break;
  339. end;
  340. inc(pointer(pl),sizeof(dword));
  341. end;
  342. if released_modified then
  343. begin
  344. dump_already_free(p,ptext);
  345. if @stderr<>@ptext then
  346. dump_already_free(p,stderr);
  347. end;
  348. end;
  349. {$ifdef EXTRA}
  350. procedure dump_change_after(p : pheap_mem_info;var ptext : text);
  351. var pp : pchar;
  352. i : ptruint;
  353. begin
  354. Writeln(ptext,'Marked memory at $',HexStr(pointer(p)+sizeof(theap_mem_info)),' invalid');
  355. Writeln(ptext,'Wrong release CRC $',hexstr(p^.release_sig,8),' instead of ',hexstr(calculate_release_sig(p),8));
  356. Writeln(ptext,'This memory was changed after call to freemem !');
  357. call_free_stack(p,ptext);
  358. pp:=pointer(p)+sizeof(theap_mem_info);
  359. for i:=0 to p^.size-1 do
  360. if byte(pp[i])<>$F0 then
  361. Writeln(ptext,'offset',i,':$',hexstr(i,2*sizeof(pointer)),'"',pp[i],'"');
  362. end;
  363. {$endif EXTRA}
  364. procedure dump_wrong_size(p : pheap_mem_info;size : ptruint;var ptext : text);
  365. begin
  366. Writeln(ptext,'Marked memory at $',HexStr(pointer(p)+sizeof(theap_mem_info)),' invalid');
  367. Writeln(ptext,'Wrong size : ',p^.size,' allocated ',size,' freed');
  368. dump_stack(ptext,1);
  369. { the check is done to be sure that the procvar is not overwritten }
  370. if assigned(p^.extra_info) and
  371. (p^.extra_info^.check=cardinal(CheckSig)) and
  372. assigned(p^.extra_info^.displayproc) then
  373. p^.extra_info^.displayproc(ptext,@p^.extra_info^.data);
  374. call_stack(p,ptext);
  375. end;
  376. function is_in_getmem_list (loc_info: pheap_info; p : pheap_mem_info) : boolean;
  377. var
  378. i : ptruint;
  379. pp : pheap_mem_info;
  380. begin
  381. is_in_getmem_list:=false;
  382. pp:=loc_info^.heap_mem_root;
  383. i:=0;
  384. while pp<>nil do
  385. begin
  386. if ((pp^.sig<>longword(AllocateSig)) or usecrc) and
  387. ((pp^.sig<>calculate_sig(pp)) or not usecrc) and
  388. (pp^.sig <>longword(ReleaseSig)) then
  389. begin
  390. if useownfile then
  391. writeln(ownfile,'error in linked list of heap_mem_info')
  392. else
  393. writeln(textoutput^,'error in linked list of heap_mem_info');
  394. RunError(204);
  395. end;
  396. if pp=p then
  397. is_in_getmem_list:=true;
  398. pp:=pp^.previous;
  399. inc(i);
  400. if i>loc_info^.getmem_cnt-loc_info^.freemem_cnt then
  401. if useownfile then
  402. writeln(ownfile,'error in linked list of heap_mem_info')
  403. else
  404. writeln(textoutput^,'error in linked list of heap_mem_info');
  405. end;
  406. end;
  407. procedure finish_heap_free_todo_list(loc_info: pheap_info);
  408. var
  409. bp: pointer;
  410. pp: pheap_mem_info;
  411. list: ppheap_mem_info;
  412. begin
  413. list := @loc_info^.heap_free_todo;
  414. repeat
  415. pp := list^;
  416. list^ := list^^.todonext;
  417. bp := pointer(pp)+sizeof(theap_mem_info);
  418. InternalFreeMemSize(loc_info,bp,pp,pp^.size,false);
  419. until list^ = nil;
  420. end;
  421. procedure try_finish_heap_free_todo_list(loc_info: pheap_info);
  422. begin
  423. if loc_info^.heap_free_todo <> nil then
  424. begin
  425. entercriticalsection(todo_lock);
  426. finish_heap_free_todo_list(loc_info);
  427. leavecriticalsection(todo_lock);
  428. end;
  429. end;
  430. {*****************************************************************************
  431. TraceGetMem
  432. *****************************************************************************}
  433. Function TraceGetMem(size:ptruint):pointer;
  434. var
  435. i, allocsize : ptruint;
  436. pl : pdword;
  437. p : pointer;
  438. pp : pheap_mem_info;
  439. loc_info: pheap_info;
  440. begin
  441. loc_info := @heap_info;
  442. try_finish_heap_free_todo_list(loc_info);
  443. inc(loc_info^.getmem_size,size);
  444. inc(loc_info^.getmem8_size,(size+7) and not 7);
  445. { Do the real GetMem, but alloc also for the info block }
  446. {$ifdef cpuarm}
  447. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+extra_info_size;
  448. {$else cpuarm}
  449. allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
  450. {$endif cpuarm}
  451. if add_tail then
  452. inc(allocsize,tail_size);
  453. { if ReturnNilIfGrowHeapFails is true
  454. SysGetMem can return nil }
  455. p:=SysGetMem(allocsize);
  456. if (p=nil) then
  457. begin
  458. TraceGetMem:=nil;
  459. exit;
  460. end;
  461. pp:=pheap_mem_info(p);
  462. inc(p,sizeof(theap_mem_info));
  463. { Create the info block }
  464. pp^.sig:=longword(AllocateSig);
  465. pp^.todolist:=@loc_info^.heap_free_todo;
  466. pp^.todonext:=nil;
  467. pp^.size:=size;
  468. pp^.extra_info_size:=extra_info_size;
  469. pp^.exact_info_size:=exact_info_size;
  470. {
  471. the end of the block contains:
  472. <tail> 4 bytes
  473. <extra_info> X bytes
  474. }
  475. if extra_info_size>0 then
  476. begin
  477. pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
  478. fillchar(pp^.extra_info^,extra_info_size,0);
  479. pp^.extra_info^.check:=cardinal(CheckSig);
  480. pp^.extra_info^.fillproc:=fill_extra_info_proc;
  481. pp^.extra_info^.displayproc:=display_extra_info_proc;
  482. if assigned(fill_extra_info_proc) then
  483. begin
  484. loc_info^.inside_trace_getmem:=true;
  485. fill_extra_info_proc(@pp^.extra_info^.data);
  486. loc_info^.inside_trace_getmem:=false;
  487. end;
  488. end
  489. else
  490. pp^.extra_info:=nil;
  491. if add_tail then
  492. begin
  493. pl:=pointer(pp)+allocsize-pp^.extra_info_size-tail_size;
  494. for i:=1 to tail_size div sizeof(dword) do
  495. begin
  496. unaligned(pl^):=dword(AllocateSig);
  497. inc(pointer(pl),sizeof(dword));
  498. end;
  499. end;
  500. { clear the memory }
  501. fillchar(p^,size,#255);
  502. { retrieve backtrace info }
  503. CaptureBacktrace(1,tracesize,@pp^.calls[1]);
  504. { insert in the linked list }
  505. if loc_info^.heap_mem_root<>nil then
  506. loc_info^.heap_mem_root^.next:=pp;
  507. pp^.previous:=loc_info^.heap_mem_root;
  508. pp^.next:=nil;
  509. {$ifdef EXTRA}
  510. pp^.prev_valid:=loc_info^.heap_valid_last;
  511. loc_info^.heap_valid_last:=pp;
  512. if not assigned(loc_info^.heap_valid_first) then
  513. loc_info^.heap_valid_first:=pp;
  514. {$endif EXTRA}
  515. loc_info^.heap_mem_root:=pp;
  516. { must be changed before fill_extra_info is called
  517. because checkpointer can be called from within
  518. fill_extra_info PM }
  519. inc(loc_info^.getmem_cnt);
  520. { update the signature }
  521. if usecrc then
  522. pp^.sig:=calculate_sig(pp);
  523. TraceGetmem:=p;
  524. end;
  525. {*****************************************************************************
  526. TraceFreeMem
  527. *****************************************************************************}
  528. function CheckFreeMemSize(loc_info: pheap_info; pp: pheap_mem_info;
  529. size, ppsize: ptruint): boolean; inline;
  530. var
  531. ptext : ^text;
  532. {$ifdef EXTRA}
  533. pp2 : pheap_mem_info;
  534. {$endif}
  535. begin
  536. if useownfile then
  537. ptext:=@ownfile
  538. else
  539. ptext:=textoutput;
  540. inc(loc_info^.freemem_size,size);
  541. inc(loc_info^.freemem8_size,(size+7) and not 7);
  542. if not quicktrace then
  543. begin
  544. if not(is_in_getmem_list(loc_info, pp)) then
  545. RunError(204);
  546. end;
  547. if (pp^.sig=longword(ReleaseSig)) then
  548. begin
  549. loc_info^.error_in_heap:=true;
  550. dump_already_free(pp,ptext^);
  551. if haltonerror then halt(1);
  552. end
  553. else if ((pp^.sig<>longword(AllocateSig)) or usecrc) and
  554. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  555. begin
  556. loc_info^.error_in_heap:=true;
  557. dump_error(pp,ptext^);
  558. {$ifdef EXTRA}
  559. dump_error(pp,error_file);
  560. {$endif EXTRA}
  561. { don't release anything in this case !! }
  562. if haltonerror then halt(1);
  563. exit;
  564. end
  565. else if pp^.size<>size then
  566. begin
  567. loc_info^.error_in_heap:=true;
  568. dump_wrong_size(pp,size,ptext^);
  569. {$ifdef EXTRA}
  570. dump_wrong_size(pp,size,error_file);
  571. {$endif EXTRA}
  572. if haltonerror then halt(1);
  573. { don't release anything in this case !! }
  574. exit;
  575. end;
  576. { now it is released !! }
  577. pp^.sig:=longword(ReleaseSig);
  578. if not keepreleased then
  579. begin
  580. if pp^.next<>nil then
  581. pp^.next^.previous:=pp^.previous;
  582. if pp^.previous<>nil then
  583. pp^.previous^.next:=pp^.next;
  584. if pp=loc_info^.heap_mem_root then
  585. loc_info^.heap_mem_root:=loc_info^.heap_mem_root^.previous;
  586. end
  587. else
  588. CaptureBacktrace(1,(tracesize div 2)-1,@pp^.calls[(tracesize div 2)+1]);
  589. inc(loc_info^.freemem_cnt);
  590. { clear the memory, $F0 will lead to GFP if used as pointer ! }
  591. fillchar((pointer(pp)+sizeof(theap_mem_info))^,size,#240);
  592. { this way we keep all info about all released memory !! }
  593. if keepreleased then
  594. begin
  595. {$ifdef EXTRA}
  596. { We want to check if the memory was changed after release !! }
  597. pp^.release_sig:=calculate_release_sig(pp);
  598. if pp=loc_info^.heap_valid_last then
  599. begin
  600. loc_info^.heap_valid_last:=pp^.prev_valid;
  601. if pp=loc_info^.heap_valid_first then
  602. loc_info^.heap_valid_first:=nil;
  603. exit(false);
  604. end;
  605. pp2:=loc_info^.heap_valid_last;
  606. while assigned(pp2) do
  607. begin
  608. if pp2^.prev_valid=pp then
  609. begin
  610. pp2^.prev_valid:=pp^.prev_valid;
  611. if pp=loc_info^.heap_valid_first then
  612. loc_info^.heap_valid_first:=pp2;
  613. exit(false);
  614. end
  615. else
  616. pp2:=pp2^.prev_valid;
  617. end;
  618. {$endif EXTRA}
  619. exit(false);
  620. end;
  621. CheckFreeMemSize:=true;
  622. end;
  623. function InternalFreeMemSize(loc_info: pheap_info; p: pointer; pp: pheap_mem_info;
  624. size: ptruint; release_todo_lock: boolean): ptruint;
  625. var
  626. i,ppsize : ptruint;
  627. extra_size: ptruint;
  628. release_mem: boolean;
  629. begin
  630. { save old values }
  631. extra_size:=pp^.extra_info_size;
  632. ppsize:= size+sizeof(theap_mem_info)+pp^.extra_info_size;
  633. if add_tail then
  634. inc(ppsize,tail_size);
  635. { do various checking }
  636. release_mem := CheckFreeMemSize(loc_info, pp, size, ppsize);
  637. if release_todo_lock then
  638. leavecriticalsection(todo_lock);
  639. if release_mem then
  640. begin
  641. { release the normal memory at least }
  642. i:=SysFreeMemSize(pp,ppsize);
  643. { return the correct size }
  644. dec(i,sizeof(theap_mem_info)+extra_size);
  645. if add_tail then
  646. dec(i,tail_size);
  647. InternalFreeMemSize:=i;
  648. end else
  649. InternalFreeMemSize:=size;
  650. end;
  651. function TraceFreeMemSize(p:pointer;size:ptruint):ptruint;
  652. var
  653. loc_info: pheap_info;
  654. pp: pheap_mem_info;
  655. release_lock: boolean;
  656. begin
  657. if p=nil then
  658. begin
  659. TraceFreeMemSize:=0;
  660. exit;
  661. end;
  662. loc_info:=@heap_info;
  663. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  664. release_lock:=false;
  665. if @loc_info^.heap_free_todo <> pp^.todolist then
  666. begin
  667. if pp^.todolist = main_orig_todolist then
  668. pp^.todolist := main_relo_todolist;
  669. entercriticalsection(todo_lock);
  670. release_lock:=true;
  671. if pp^.todolist = @orphaned_info.heap_free_todo then
  672. begin
  673. loc_info := @orphaned_info;
  674. end else
  675. if pp^.todolist <> @loc_info^.heap_free_todo then
  676. begin
  677. { allocated in different heap, push to that todolist }
  678. pp^.todonext := pp^.todolist^;
  679. pp^.todolist^ := pp;
  680. TraceFreeMemSize := pp^.size;
  681. leavecriticalsection(todo_lock);
  682. exit;
  683. end;
  684. end;
  685. TraceFreeMemSize:=InternalFreeMemSize(loc_info,p,pp,size,release_lock);
  686. end;
  687. function TraceMemSize(p:pointer):ptruint;
  688. var
  689. pp : pheap_mem_info;
  690. begin
  691. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  692. TraceMemSize:=pp^.size;
  693. end;
  694. function TraceFreeMem(p:pointer):ptruint;
  695. var
  696. l : ptruint;
  697. pp : pheap_mem_info;
  698. begin
  699. if p=nil then
  700. begin
  701. TraceFreeMem:=0;
  702. exit;
  703. end;
  704. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  705. l:=SysMemSize(pp);
  706. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  707. if add_tail then
  708. dec(l,tail_size);
  709. { this can never happend normaly }
  710. if pp^.size>l then
  711. begin
  712. if useownfile then
  713. dump_wrong_size(pp,l,ownfile)
  714. else
  715. dump_wrong_size(pp,l,textoutput^);
  716. {$ifdef EXTRA}
  717. dump_wrong_size(pp,l,error_file);
  718. {$endif EXTRA}
  719. end;
  720. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  721. end;
  722. {*****************************************************************************
  723. ReAllocMem
  724. *****************************************************************************}
  725. function TraceReAllocMem(var p:pointer;size:ptruint):Pointer;
  726. var
  727. newP: pointer;
  728. i, allocsize,
  729. movesize : ptruint;
  730. pl : pdword;
  731. pp : pheap_mem_info;
  732. oldsize,
  733. oldextrasize,
  734. oldexactsize : ptruint;
  735. old_fill_extra_info_proc : tfillextrainfoproc;
  736. old_display_extra_info_proc : tdisplayextrainfoproc;
  737. loc_info: pheap_info;
  738. begin
  739. { Free block? }
  740. if size=0 then
  741. begin
  742. if p<>nil then
  743. TraceFreeMem(p);
  744. p:=nil;
  745. TraceReallocMem:=P;
  746. exit;
  747. end;
  748. { Allocate a new block? }
  749. if p=nil then
  750. begin
  751. p:=TraceGetMem(size);
  752. TraceReallocMem:=P;
  753. exit;
  754. end;
  755. { Resize block }
  756. loc_info:=@heap_info;
  757. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  758. { test block }
  759. if ((pp^.sig<>longword(AllocateSig)) or usecrc) and
  760. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  761. begin
  762. loc_info^.error_in_heap:=true;
  763. if useownfile then
  764. dump_error(pp,ownfile)
  765. else
  766. dump_error(pp,textoutput^);
  767. {$ifdef EXTRA}
  768. dump_error(pp,error_file);
  769. {$endif EXTRA}
  770. { don't release anything in this case !! }
  771. if haltonerror then halt(1);
  772. exit;
  773. end;
  774. { save info }
  775. oldsize:=pp^.size;
  776. oldextrasize:=pp^.extra_info_size;
  777. oldexactsize:=pp^.exact_info_size;
  778. if pp^.extra_info_size>0 then
  779. begin
  780. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  781. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  782. end;
  783. { Do the real ReAllocMem, but alloc also for the info block }
  784. {$ifdef cpuarm}
  785. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+pp^.extra_info_size;
  786. {$else cpuarm}
  787. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  788. {$endif cpuarm}
  789. if add_tail then
  790. inc(allocsize,tail_size);
  791. { Try to resize the block, if not possible we need to do a
  792. getmem, move data, freemem }
  793. if not SysTryResizeMem(pp,allocsize) then
  794. begin
  795. { get a new block }
  796. newP := TraceGetMem(size);
  797. { move the data }
  798. if newP <> nil then
  799. begin
  800. movesize:=TraceMemSize(p);
  801. {if the old size is larger than the new size,
  802. move only the new size}
  803. if movesize>size then
  804. movesize:=size;
  805. move(p^,newP^,movesize);
  806. end;
  807. { release p }
  808. traceFreeMem(p);
  809. { return the new pointer }
  810. p:=newp;
  811. traceReAllocMem := newp;
  812. exit;
  813. end;
  814. { Recreate the info block }
  815. pp^.sig:=longword(AllocateSig);
  816. pp^.size:=size;
  817. pp^.extra_info_size:=oldextrasize;
  818. pp^.exact_info_size:=oldexactsize;
  819. { add the new extra_info and tail }
  820. if pp^.extra_info_size>0 then
  821. begin
  822. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  823. fillchar(pp^.extra_info^,extra_info_size,0);
  824. pp^.extra_info^.check:=cardinal(CheckSig);
  825. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  826. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  827. if assigned(pp^.extra_info^.fillproc) then
  828. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  829. end
  830. else
  831. pp^.extra_info:=nil;
  832. if add_tail then
  833. begin
  834. pl:=pointer(pp)+allocsize-pp^.extra_info_size-tail_size;
  835. for i:=1 to tail_size div sizeof(dword) do
  836. begin
  837. unaligned(pl^):=dword(AllocateSig);
  838. inc(pointer(pl),sizeof(dword));
  839. end;
  840. end;
  841. { adjust like a freemem and then a getmem, so you get correct
  842. results in the summary display }
  843. inc(loc_info^.freemem_size,oldsize);
  844. inc(loc_info^.freemem8_size,(oldsize+7) and not 7);
  845. inc(loc_info^.getmem_size,size);
  846. inc(loc_info^.getmem8_size,(size+7) and not 7);
  847. { generate new backtrace }
  848. CaptureBacktrace(1,tracesize,@pp^.calls[1]);
  849. { regenerate signature }
  850. if usecrc then
  851. pp^.sig:=calculate_sig(pp);
  852. { return the pointer }
  853. p:=pointer(pp)+sizeof(theap_mem_info);
  854. TraceReAllocmem:=p;
  855. end;
  856. {*****************************************************************************
  857. Check pointer
  858. *****************************************************************************}
  859. {$ifndef Unix}
  860. {$S-}
  861. {$endif}
  862. {$ifdef go32v2}
  863. var
  864. __stklen : longword;external name '__stklen';
  865. __stkbottom : longword;external name '__stkbottom';
  866. ebss : longword; external name 'end';
  867. {$endif go32v2}
  868. {$ifdef linux}
  869. var
  870. etext: ptruint; external name '_etext';
  871. edata : ptruint; external name '_edata';
  872. eend : ptruint; external name '_end';
  873. {$endif}
  874. {$ifdef os2}
  875. (* Currently still EMX based - possibly to be changed in the future. *)
  876. var
  877. etext: ptruint; external name '_etext';
  878. edata : ptruint; external name '_edata';
  879. eend : ptruint; external name '_end';
  880. {$endif}
  881. {$ifdef windows}
  882. var
  883. sdata : ptruint; external name '__data_start__';
  884. edata : ptruint; external name '__data_end__';
  885. sbss : ptruint; external name '__bss_start__';
  886. ebss : ptruint; external name '__bss_end__';
  887. TLSKey : PDWord; external name '_FPC_TlsKey';
  888. TLSSize : DWord; external name '_FPC_TlsSize';
  889. function TlsGetValue(dwTlsIndex : DWord) : pointer;
  890. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'TlsGetValue';
  891. {$endif}
  892. {$ifdef BEOS}
  893. const
  894. B_ERROR = -1;
  895. type
  896. area_id = Longint;
  897. function area_for(addr : Pointer) : area_id;
  898. cdecl; external 'root' name 'area_for';
  899. {$endif BEOS}
  900. procedure CheckPointer(p : pointer); [public, alias : 'FPC_CHECKPOINTER'];
  901. var
  902. i : ptruint;
  903. pp : pheap_mem_info;
  904. loc_info: pheap_info;
  905. {$ifdef go32v2}
  906. get_ebp,stack_top : longword;
  907. bss_end : longword;
  908. {$endif go32v2}
  909. {$ifdef windows}
  910. datap : pointer;
  911. {$endif windows}
  912. ptext : ^text;
  913. begin
  914. if p=nil then
  915. runerror(204);
  916. i:=0;
  917. loc_info:=@heap_info;
  918. if useownfile then
  919. ptext:=@ownfile
  920. else
  921. ptext:=textoutput;
  922. {$ifdef go32v2}
  923. if ptruint(p)<$1000 then
  924. runerror(216);
  925. asm
  926. movl %ebp,get_ebp
  927. leal ebss,%eax
  928. movl %eax,bss_end
  929. end;
  930. stack_top:=__stkbottom+__stklen;
  931. { allow all between start of code and end of bss }
  932. if ptruint(p)<=bss_end then
  933. exit;
  934. { stack can be above heap !! }
  935. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  936. exit;
  937. {$endif go32v2}
  938. { I don't know where the stack is in other OS !! }
  939. {$ifdef windows}
  940. { inside stack ? }
  941. if (ptruint(p)>ptruint(get_frame)) and
  942. (p<StackTop) then
  943. exit;
  944. { inside data, rdata ... bss }
  945. if (ptruint(p)>=ptruint(@sdata)) and (ptruint(p)<ptruint(@ebss)) then
  946. exit;
  947. { is program multi-threaded and p inside Threadvar range? }
  948. if TlsKey^<>-1 then
  949. begin
  950. datap:=TlsGetValue(tlskey^);
  951. if ((ptruint(p)>=ptruint(datap)) and
  952. (ptruint(p)<ptruint(datap)+TlsSize)) then
  953. exit;
  954. end;
  955. {$endif windows}
  956. {$IFDEF OS2}
  957. { inside stack ? }
  958. if (PtrUInt (P) > PtrUInt (Get_Frame)) and
  959. (PtrUInt (P) < PtrUInt (StackTop)) then
  960. exit;
  961. { inside data or bss ? }
  962. if (PtrUInt (P) >= PtrUInt (@etext)) and (PtrUInt (P) < PtrUInt (@eend)) then
  963. exit;
  964. {$ENDIF OS2}
  965. {$ifdef linux}
  966. { inside stack ? }
  967. if (ptruint(p)>ptruint(get_frame)) and
  968. (ptruint(p)<ptruint(StackTop)) then
  969. exit;
  970. { inside data or bss ? }
  971. if (ptruint(p)>=ptruint(@etext)) and (ptruint(p)<ptruint(@eend)) then
  972. exit;
  973. {$endif linux}
  974. {$ifdef morphos}
  975. { inside stack ? }
  976. if (ptruint(p)<ptruint(StackTop)) and (ptruint(p)>ptruint(StackBottom)) then
  977. exit;
  978. { inside data or bss ? }
  979. {$WARNING data and bss checking missing }
  980. {$endif morphos}
  981. {$ifdef darwin}
  982. {$warning No checkpointer support yet for Darwin}
  983. exit;
  984. {$endif}
  985. {$ifdef BEOS}
  986. // if we find the address in a known area in our current process,
  987. // then it is a valid one
  988. if area_for(p) <> B_ERROR then
  989. exit;
  990. {$endif BEOS}
  991. { first try valid list faster }
  992. {$ifdef EXTRA}
  993. pp:=loc_info^.heap_valid_last;
  994. while pp<>nil do
  995. begin
  996. { inside this valid block ! }
  997. { we can be changing the extrainfo !! }
  998. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  999. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  1000. begin
  1001. { check allocated block }
  1002. if ((pp^.sig=longword(AllocateSig)) and not usecrc) or
  1003. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  1004. { special case of the fill_extra_info call }
  1005. ((pp=loc_info^.heap_valid_last) and usecrc and (pp^.sig=longword(AllocateSig))
  1006. and loc_info^.inside_trace_getmem) then
  1007. exit
  1008. else
  1009. begin
  1010. writeln(ptext^,'corrupted heap_mem_info');
  1011. dump_error(pp,ptext^);
  1012. halt(1);
  1013. end;
  1014. end
  1015. else
  1016. pp:=pp^.prev_valid;
  1017. inc(i);
  1018. if i>loc_info^.getmem_cnt-loc_info^.freemem_cnt then
  1019. begin
  1020. writeln(ptext^,'error in linked list of heap_mem_info');
  1021. halt(1);
  1022. end;
  1023. end;
  1024. i:=0;
  1025. {$endif EXTRA}
  1026. pp:=loc_info^.heap_mem_root;
  1027. while pp<>nil do
  1028. begin
  1029. { inside this block ! }
  1030. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  1031. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  1032. { allocated block }
  1033. if ((pp^.sig=longword(AllocateSig)) and not usecrc) or
  1034. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  1035. exit
  1036. else
  1037. begin
  1038. writeln(ptext^,'pointer $',hexstr(p),' points into invalid memory block');
  1039. dump_error(pp,ptext^);
  1040. runerror(204);
  1041. end;
  1042. pp:=pp^.previous;
  1043. inc(i);
  1044. if i>loc_info^.getmem_cnt then
  1045. begin
  1046. writeln(ptext^,'error in linked list of heap_mem_info');
  1047. halt(1);
  1048. end;
  1049. end;
  1050. writeln(ptext^,'pointer $',hexstr(p),' does not point to valid memory block');
  1051. dump_stack(ptext^,1);
  1052. runerror(204);
  1053. end;
  1054. {*****************************************************************************
  1055. Dump Heap
  1056. *****************************************************************************}
  1057. procedure dumpheap;
  1058. begin
  1059. DumpHeap(GlobalSkipIfNoLeaks);
  1060. end;
  1061. procedure dumpheap(SkipIfNoLeaks : Boolean);
  1062. var
  1063. pp : pheap_mem_info;
  1064. i : ptrint;
  1065. ExpectedHeapFree : ptruint;
  1066. status : TFPCHeapStatus;
  1067. ptext : ^text;
  1068. loc_info: pheap_info;
  1069. begin
  1070. loc_info:=@heap_info;
  1071. if useownfile then
  1072. ptext:=@ownfile
  1073. else
  1074. ptext:=textoutput;
  1075. pp:=loc_info^.heap_mem_root;
  1076. if ((loc_info^.getmem_size-loc_info^.freemem_size)=0) and SkipIfNoLeaks then
  1077. exit;
  1078. Writeln(ptext^,'Heap dump by heaptrc unit');
  1079. Writeln(ptext^,loc_info^.getmem_cnt, ' memory blocks allocated : ',
  1080. loc_info^.getmem_size,'/',loc_info^.getmem8_size);
  1081. Writeln(ptext^,loc_info^.freemem_cnt,' memory blocks freed : ',
  1082. loc_info^.freemem_size,'/',loc_info^.freemem8_size);
  1083. Writeln(ptext^,loc_info^.getmem_cnt-loc_info^.freemem_cnt,
  1084. ' unfreed memory blocks : ',loc_info^.getmem_size-loc_info^.freemem_size);
  1085. status:=SysGetFPCHeapStatus;
  1086. Write(ptext^,'True heap size : ',status.CurrHeapSize);
  1087. if EntryMemUsed > 0 then
  1088. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  1089. else
  1090. Writeln(ptext^);
  1091. Writeln(ptext^,'True free heap : ',status.CurrHeapFree);
  1092. ExpectedHeapFree:=status.CurrHeapSize
  1093. -(loc_info^.getmem8_size-loc_info^.freemem8_size)
  1094. -(loc_info^.getmem_cnt-loc_info^.freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)
  1095. -EntryMemUsed;
  1096. If ExpectedHeapFree<>status.CurrHeapFree then
  1097. Writeln(ptext^,'Should be : ',ExpectedHeapFree);
  1098. i:=loc_info^.getmem_cnt-loc_info^.freemem_cnt;
  1099. while pp<>nil do
  1100. begin
  1101. if i<0 then
  1102. begin
  1103. Writeln(ptext^,'Error in heap memory list');
  1104. Writeln(ptext^,'More memory blocks than expected');
  1105. exit;
  1106. end;
  1107. if ((pp^.sig=longword(AllocateSig)) and not usecrc) or
  1108. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  1109. begin
  1110. { this one was not released !! }
  1111. if exitcode<>203 then
  1112. call_stack(pp,ptext^);
  1113. dec(i);
  1114. end
  1115. else if pp^.sig<>longword(ReleaseSig) then
  1116. begin
  1117. dump_error(pp,ptext^);
  1118. if @stderr<>ptext then
  1119. dump_error(pp,stderr);
  1120. {$ifdef EXTRA}
  1121. dump_error(pp,error_file);
  1122. {$endif EXTRA}
  1123. loc_info^.error_in_heap:=true;
  1124. end
  1125. {$ifdef EXTRA}
  1126. else if pp^.release_sig<>calculate_release_sig(pp) then
  1127. begin
  1128. dump_change_after(pp,ptext^);
  1129. dump_change_after(pp,error_file);
  1130. loc_info^.error_in_heap:=true;
  1131. end
  1132. {$else not EXTRA}
  1133. else
  1134. begin
  1135. if released_modified(pp,ptext^) then
  1136. exitcode:=203;
  1137. end;
  1138. {$endif EXTRA}
  1139. ;
  1140. pp:=pp^.previous;
  1141. end;
  1142. if HaltOnNotReleased and (loc_info^.getmem_cnt<>loc_info^.freemem_cnt) then
  1143. exitcode:=203;
  1144. end;
  1145. {*****************************************************************************
  1146. AllocMem
  1147. *****************************************************************************}
  1148. function TraceAllocMem(size:ptruint):Pointer;
  1149. begin
  1150. TraceAllocMem:=SysAllocMem(size);
  1151. end;
  1152. {*****************************************************************************
  1153. No specific tracing calls
  1154. *****************************************************************************}
  1155. procedure TraceInitThread;
  1156. var
  1157. loc_info: pheap_info;
  1158. begin
  1159. loc_info := @heap_info;
  1160. {$ifdef EXTRA}
  1161. loc_info^.heap_valid_first := nil;
  1162. loc_info^.heap_valid_last := nil;
  1163. {$endif}
  1164. loc_info^.heap_mem_root := nil;
  1165. loc_info^.getmem_cnt := 0;
  1166. loc_info^.freemem_cnt := 0;
  1167. loc_info^.getmem_size := 0;
  1168. loc_info^.freemem_size := 0;
  1169. loc_info^.getmem8_size := 0;
  1170. loc_info^.freemem8_size := 0;
  1171. loc_info^.error_in_heap := false;
  1172. loc_info^.inside_trace_getmem := false;
  1173. EntryMemUsed := SysGetFPCHeapStatus.CurrHeapUsed;
  1174. end;
  1175. procedure TraceRelocateHeap;
  1176. begin
  1177. main_relo_todolist := @heap_info.heap_free_todo;
  1178. initcriticalsection(todo_lock);
  1179. end;
  1180. procedure move_heap_info(src_info, dst_info: pheap_info);
  1181. var
  1182. heap_mem: pheap_mem_info;
  1183. begin
  1184. if src_info^.heap_free_todo <> nil then
  1185. finish_heap_free_todo_list(src_info);
  1186. if dst_info^.heap_free_todo <> nil then
  1187. finish_heap_free_todo_list(dst_info);
  1188. heap_mem := src_info^.heap_mem_root;
  1189. if heap_mem <> nil then
  1190. begin
  1191. repeat
  1192. heap_mem^.todolist := @dst_info^.heap_free_todo;
  1193. if heap_mem^.previous = nil then break;
  1194. heap_mem := heap_mem^.previous;
  1195. until false;
  1196. heap_mem^.previous := dst_info^.heap_mem_root;
  1197. if dst_info^.heap_mem_root <> nil then
  1198. dst_info^.heap_mem_root^.next := heap_mem;
  1199. dst_info^.heap_mem_root := src_info^.heap_mem_root;
  1200. end;
  1201. inc(dst_info^.getmem_cnt, src_info^.getmem_cnt);
  1202. inc(dst_info^.getmem_size, src_info^.getmem_size);
  1203. inc(dst_info^.getmem8_size, src_info^.getmem8_size);
  1204. inc(dst_info^.freemem_cnt, src_info^.freemem_cnt);
  1205. inc(dst_info^.freemem_size, src_info^.freemem_size);
  1206. inc(dst_info^.freemem8_size, src_info^.freemem8_size);
  1207. dst_info^.error_in_heap := dst_info^.error_in_heap or src_info^.error_in_heap;
  1208. {$ifdef EXTRA}
  1209. if assigned(dst_info^.heap_valid_first) then
  1210. dst_info^.heap_valid_first^.prev_valid := src_info^.heap_valid_last
  1211. else
  1212. dst_info^.heap_valid_last := src_info^.heap_valid_last;
  1213. dst_info^.heap_valid_first := src_info^.heap_valid_first;
  1214. {$endif}
  1215. end;
  1216. procedure TraceExitThread;
  1217. var
  1218. loc_info: pheap_info;
  1219. begin
  1220. loc_info := @heap_info;
  1221. entercriticalsection(todo_lock);
  1222. move_heap_info(loc_info, @orphaned_info);
  1223. leavecriticalsection(todo_lock);
  1224. end;
  1225. function TraceGetHeapStatus:THeapStatus;
  1226. begin
  1227. TraceGetHeapStatus:=SysGetHeapStatus;
  1228. end;
  1229. function TraceGetFPCHeapStatus:TFPCHeapStatus;
  1230. begin
  1231. TraceGetFPCHeapStatus:=SysGetFPCHeapStatus;
  1232. end;
  1233. {*****************************************************************************
  1234. Program Hooks
  1235. *****************************************************************************}
  1236. Procedure SetHeapTraceOutput(const name : string);
  1237. var i : ptruint;
  1238. begin
  1239. if useownfile then
  1240. begin
  1241. useownfile:=false;
  1242. close(ownfile);
  1243. end;
  1244. assign(ownfile,name);
  1245. {$I-}
  1246. append(ownfile);
  1247. if IOResult<>0 then
  1248. begin
  1249. Rewrite(ownfile);
  1250. if IOResult<>0 then
  1251. begin
  1252. Writeln(textoutput^,'[heaptrc] Unable to open "',name,'", writing output to stderr instead.');
  1253. useownfile:=false;
  1254. exit;
  1255. end;
  1256. end;
  1257. {$I+}
  1258. useownfile:=true;
  1259. for i:=0 to Paramcount do
  1260. write(ownfile,paramstr(i),' ');
  1261. writeln(ownfile);
  1262. end;
  1263. procedure SetHeapTraceOutput(var ATextOutput : Text);
  1264. Begin
  1265. useowntextoutput := True;
  1266. textoutput := @ATextOutput;
  1267. end;
  1268. procedure SetHeapExtraInfo( size : ptruint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  1269. begin
  1270. { the total size must stay multiple of 8, also allocate 2 pointers for
  1271. the fill and display procvars }
  1272. exact_info_size:=size + sizeof(theap_extra_info);
  1273. extra_info_size:=(exact_info_size+7) and not 7;
  1274. fill_extra_info_proc:=fillproc;
  1275. display_extra_info_proc:=displayproc;
  1276. end;
  1277. {*****************************************************************************
  1278. Install MemoryManager
  1279. *****************************************************************************}
  1280. const
  1281. TraceManager:TMemoryManager=(
  1282. NeedLock : true;
  1283. Getmem : @TraceGetMem;
  1284. Freemem : @TraceFreeMem;
  1285. FreememSize : @TraceFreeMemSize;
  1286. AllocMem : @TraceAllocMem;
  1287. ReAllocMem : @TraceReAllocMem;
  1288. MemSize : @TraceMemSize;
  1289. InitThread: @TraceInitThread;
  1290. DoneThread: @TraceExitThread;
  1291. RelocateHeap: @TraceRelocateHeap;
  1292. GetHeapStatus : @TraceGetHeapStatus;
  1293. GetFPCHeapStatus : @TraceGetFPCHeapStatus;
  1294. );
  1295. var
  1296. PrevMemoryManager : TMemoryManager;
  1297. procedure TraceInit;
  1298. begin
  1299. textoutput := @stderr;
  1300. useowntextoutput := false;
  1301. MakeCRC32Tbl;
  1302. main_orig_todolist := @heap_info.heap_free_todo;
  1303. main_relo_todolist := nil;
  1304. TraceInitThread;
  1305. GetMemoryManager(PrevMemoryManager);
  1306. SetMemoryManager(TraceManager);
  1307. useownfile:=false;
  1308. if outputstr <> '' then
  1309. SetHeapTraceOutput(outputstr);
  1310. {$ifdef EXTRA}
  1311. {$i-}
  1312. Assign(error_file,'heap.err');
  1313. Rewrite(error_file);
  1314. {$i+}
  1315. if IOResult<>0 then
  1316. begin
  1317. writeln('[heaptrc] Unable to create heap.err extra log file, writing output to screen.');
  1318. Assign(error_file,'');
  1319. Rewrite(error_file);
  1320. end;
  1321. {$endif EXTRA}
  1322. { if multithreading was initialized before heaptrc gets initialized (this is currently
  1323. the case for windows dlls), then RelocateHeap gets never called and the lock
  1324. must be initialized already here
  1325. }
  1326. if IsMultithread then
  1327. TraceRelocateHeap;
  1328. end;
  1329. procedure TraceExit;
  1330. begin
  1331. { no dump if error
  1332. because this gives long long listings }
  1333. { clear inoutres, in case the program that quit didn't }
  1334. ioresult;
  1335. if (exitcode<>0) and (erroraddr<>nil) then
  1336. begin
  1337. if useownfile then
  1338. begin
  1339. Writeln(ownfile,'No heap dump by heaptrc unit');
  1340. Writeln(ownfile,'Exitcode = ',exitcode);
  1341. end
  1342. else
  1343. begin
  1344. Writeln(textoutput^,'No heap dump by heaptrc unit');
  1345. Writeln(textoutput^,'Exitcode = ',exitcode);
  1346. end;
  1347. if useownfile then
  1348. begin
  1349. useownfile:=false;
  1350. close(ownfile);
  1351. end;
  1352. exit;
  1353. end;
  1354. { Disable heaptrc memory manager to avoid problems }
  1355. SetMemoryManager(PrevMemoryManager);
  1356. move_heap_info(@orphaned_info, @heap_info);
  1357. dumpheap;
  1358. if heap_info.error_in_heap and (exitcode=0) then
  1359. exitcode:=203;
  1360. if main_relo_todolist <> nil then
  1361. donecriticalsection(todo_lock);
  1362. {$ifdef EXTRA}
  1363. Close(error_file);
  1364. {$endif EXTRA}
  1365. if useownfile then
  1366. begin
  1367. useownfile:=false;
  1368. close(ownfile);
  1369. end;
  1370. if useowntextoutput then
  1371. begin
  1372. useowntextoutput := false;
  1373. close(textoutput^);
  1374. end;
  1375. end;
  1376. {$if defined(win32) or defined(win64)}
  1377. function GetEnvironmentStrings : pchar; stdcall;
  1378. external 'kernel32' name 'GetEnvironmentStringsA';
  1379. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  1380. external 'kernel32' name 'FreeEnvironmentStringsA';
  1381. Function GetEnv(envvar: string): string;
  1382. var
  1383. s : string;
  1384. i : ptruint;
  1385. hp,p : pchar;
  1386. begin
  1387. getenv:='';
  1388. p:=GetEnvironmentStrings;
  1389. hp:=p;
  1390. while hp^<>#0 do
  1391. begin
  1392. s:=strpas(hp);
  1393. i:=pos('=',s);
  1394. if upcase(copy(s,1,i-1))=upcase(envvar) then
  1395. begin
  1396. getenv:=copy(s,i+1,length(s)-i);
  1397. break;
  1398. end;
  1399. { next string entry}
  1400. hp:=hp+strlen(hp)+1;
  1401. end;
  1402. FreeEnvironmentStrings(p);
  1403. end;
  1404. {$elseif defined(wince)}
  1405. Function GetEnv(P:string):Pchar;
  1406. begin
  1407. { WinCE does not have environment strings.
  1408. Add some way to specify heaptrc options? }
  1409. GetEnv:=nil;
  1410. end;
  1411. {$elseif defined(msdos)}
  1412. type
  1413. PFarChar=^Char;far;
  1414. PPFarChar=^PFarChar;
  1415. var
  1416. envp: PPFarChar;external name '__fpc_envp';
  1417. Function GetEnv(P:string):string;
  1418. var
  1419. ep : ppfarchar;
  1420. pc : pfarchar;
  1421. i : smallint;
  1422. found : boolean;
  1423. Begin
  1424. getenv:='';
  1425. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1426. ep:=envp;
  1427. found:=false;
  1428. if ep<>nil then
  1429. begin
  1430. while (not found) and (ep^<>nil) do
  1431. begin
  1432. found:=true;
  1433. for i:=1 to length(p) do
  1434. if p[i]<>ep^[i-1] then
  1435. begin
  1436. found:=false;
  1437. break;
  1438. end;
  1439. if not found then
  1440. inc(ep);
  1441. end;
  1442. end;
  1443. if found then
  1444. begin
  1445. pc:=ep^+length(p);
  1446. while pc^<>#0 do
  1447. begin
  1448. getenv:=getenv+pc^;
  1449. Inc(pc);
  1450. end;
  1451. end;
  1452. end;
  1453. {$else}
  1454. Function GetEnv(P:string):Pchar;
  1455. {
  1456. Searches the environment for a string with name p and
  1457. returns a pchar to it's value.
  1458. A pchar is used to accomodate for strings of length > 255
  1459. }
  1460. var
  1461. ep : ppchar;
  1462. i : ptruint;
  1463. found : boolean;
  1464. Begin
  1465. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1466. ep:=envp;
  1467. found:=false;
  1468. if ep<>nil then
  1469. begin
  1470. while (not found) and (ep^<>nil) do
  1471. begin
  1472. found:=true;
  1473. for i:=1 to length(p) do
  1474. if p[i]<>ep^[i-1] then
  1475. begin
  1476. found:=false;
  1477. break;
  1478. end;
  1479. if not found then
  1480. inc(ep);
  1481. end;
  1482. end;
  1483. if found then
  1484. getenv:=ep^+length(p)
  1485. else
  1486. getenv:=nil;
  1487. end;
  1488. {$endif}
  1489. procedure LoadEnvironment;
  1490. var
  1491. i,j : ptruint;
  1492. s,s2 : string;
  1493. err : word;
  1494. begin
  1495. s:=Getenv('HEAPTRC');
  1496. if pos('keepreleased',s)>0 then
  1497. keepreleased:=true;
  1498. if pos('disabled',s)>0 then
  1499. useheaptrace:=false;
  1500. if pos('nohalt',s)>0 then
  1501. haltonerror:=false;
  1502. if pos('haltonnotreleased',s)>0 then
  1503. HaltOnNotReleased :=true;
  1504. if pos('skipifnoleaks',s)>0 then
  1505. GlobalSkipIfNoLeaks :=true;
  1506. if pos('tail_size=',s)>0 then
  1507. begin
  1508. i:=pos('tail_size=',s)+length('tail_size=');
  1509. s2:='';
  1510. while (i<=length(s)) and (s[i] in ['0'..'9']) do
  1511. begin
  1512. s2:=s2+s[i];
  1513. inc(i);
  1514. end;
  1515. val(s2,tail_size,err);
  1516. if err=0 then
  1517. tail_size:=((tail_size + sizeof(ptruint)-1) div sizeof(ptruint)) * sizeof(ptruint)
  1518. else
  1519. tail_size:=sizeof(ptruint);
  1520. add_tail:=(tail_size > 0);
  1521. end;
  1522. i:=pos('log=',s);
  1523. if i>0 then
  1524. begin
  1525. outputstr:=copy(s,i+4,255);
  1526. j:=pos(' ',outputstr);
  1527. if j=0 then
  1528. j:=length(outputstr)+1;
  1529. delete(outputstr,j,255);
  1530. end;
  1531. end;
  1532. Initialization
  1533. LoadEnvironment;
  1534. { heaptrc can be disabled from the environment }
  1535. if useheaptrace then
  1536. TraceInit;
  1537. finalization
  1538. if useheaptrace then
  1539. TraceExit;
  1540. end.