heaptrc.pp 42 KB

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