heaptrc.pp 44 KB

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