heaptrc.pp 40 KB

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