heaptrc.pp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. begin
  342. if loc_info^.heap_free_todo <> nil then
  343. begin
  344. entercriticalsection(todo_lock);
  345. finish_heap_free_todo_list(loc_info);
  346. leavecriticalsection(todo_lock);
  347. end;
  348. end;
  349. {*****************************************************************************
  350. TraceGetMem
  351. *****************************************************************************}
  352. Function TraceGetMem(size:ptruint):pointer;
  353. var
  354. allocsize,i : ptruint;
  355. oldbp,
  356. bp : pointer;
  357. pl : pdword;
  358. p : pointer;
  359. pp : pheap_mem_info;
  360. loc_info: pheap_info;
  361. begin
  362. loc_info := @heap_info;
  363. try_finish_heap_free_todo_list(loc_info);
  364. inc(loc_info^.getmem_size,size);
  365. inc(loc_info^.getmem8_size,(size+7) and not 7);
  366. { Do the real GetMem, but alloc also for the info block }
  367. {$ifdef cpuarm}
  368. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+extra_info_size;
  369. {$else cpuarm}
  370. allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
  371. {$endif cpuarm}
  372. if add_tail then
  373. inc(allocsize,sizeof(ptruint));
  374. { if ReturnNilIfGrowHeapFails is true
  375. SysGetMem can return nil }
  376. p:=SysGetMem(allocsize);
  377. if (p=nil) then
  378. begin
  379. TraceGetMem:=nil;
  380. exit;
  381. end;
  382. pp:=pheap_mem_info(p);
  383. inc(p,sizeof(theap_mem_info));
  384. { Create the info block }
  385. pp^.sig:=$DEADBEEF;
  386. pp^.todolist:=@loc_info^.heap_free_todo;
  387. pp^.todonext:=nil;
  388. pp^.size:=size;
  389. pp^.extra_info_size:=extra_info_size;
  390. pp^.exact_info_size:=exact_info_size;
  391. {
  392. the end of the block contains:
  393. <tail> 4 bytes
  394. <extra_info> X bytes
  395. }
  396. if extra_info_size>0 then
  397. begin
  398. pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
  399. fillchar(pp^.extra_info^,extra_info_size,0);
  400. pp^.extra_info^.check:=$12345678;
  401. pp^.extra_info^.fillproc:=fill_extra_info_proc;
  402. pp^.extra_info^.displayproc:=display_extra_info_proc;
  403. if assigned(fill_extra_info_proc) then
  404. begin
  405. loc_info^.inside_trace_getmem:=true;
  406. fill_extra_info_proc(@pp^.extra_info^.data);
  407. loc_info^.inside_trace_getmem:=false;
  408. end;
  409. end
  410. else
  411. pp^.extra_info:=nil;
  412. if add_tail then
  413. begin
  414. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptruint);
  415. {$ifdef FPC_SUPPORTS_UNALIGNED}
  416. unaligned(pl^):=$DEADBEEF;
  417. {$else FPC_SUPPORTS_UNALIGNED}
  418. pl^:=$DEADBEEF;
  419. {$endif FPC_SUPPORTS_UNALIGNED}
  420. end;
  421. { clear the memory }
  422. fillchar(p^,size,#255);
  423. { retrieve backtrace info }
  424. bp:=get_caller_frame(get_frame);
  425. { valid bp? }
  426. if (bp>=StackBottom) and (bp<(StackBottom + StackLength)) then
  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. break;
  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. if (bp>=StackBottom) and (bp<(StackBottom + StackLength)) then
  524. for i:=(tracesize div 2)+1 to tracesize do
  525. begin
  526. pp^.calls[i]:=get_caller_addr(bp);
  527. bp:=get_caller_frame(bp);
  528. if not((bp>=StackBottom) and (bp<(StackBottom + StackLength))) then
  529. break;
  530. end;
  531. end;
  532. inc(loc_info^.freemem_cnt);
  533. { clear the memory, $F0 will lead to GFP if used as pointer ! }
  534. fillchar((pointer(pp)+sizeof(theap_mem_info))^,size,#240);
  535. { this way we keep all info about all released memory !! }
  536. if keepreleased then
  537. begin
  538. {$ifdef EXTRA}
  539. { We want to check if the memory was changed after release !! }
  540. pp^.release_sig:=calculate_release_sig(pp);
  541. if pp=loc_info^.heap_valid_last then
  542. begin
  543. loc_info^.heap_valid_last:=pp^.prev_valid;
  544. if pp=loc_info^.heap_valid_first then
  545. loc_info^.heap_valid_first:=nil;
  546. exit(false);
  547. end;
  548. pp2:=loc_info^.heap_valid_last;
  549. while assigned(pp2) do
  550. begin
  551. if pp2^.prev_valid=pp then
  552. begin
  553. pp2^.prev_valid:=pp^.prev_valid;
  554. if pp=loc_info^.heap_valid_first then
  555. loc_info^.heap_valid_first:=pp2;
  556. exit(false);
  557. end
  558. else
  559. pp2:=pp2^.prev_valid;
  560. end;
  561. {$endif EXTRA}
  562. exit(false);
  563. end;
  564. CheckFreeMemSize:=true;
  565. end;
  566. function InternalFreeMemSize(loc_info: pheap_info; p: pointer; pp: pheap_mem_info;
  567. size: ptruint; release_todo_lock: boolean): ptruint;
  568. var
  569. i,ppsize : ptruint;
  570. extra_size: ptruint;
  571. release_mem: boolean;
  572. begin
  573. { save old values }
  574. extra_size:=pp^.extra_info_size;
  575. ppsize:= size+sizeof(theap_mem_info)+pp^.extra_info_size;
  576. if add_tail then
  577. inc(ppsize,sizeof(ptruint));
  578. { do various checking }
  579. release_mem := CheckFreeMemSize(loc_info, pp, size, ppsize);
  580. if release_todo_lock then
  581. leavecriticalsection(todo_lock);
  582. if release_mem then
  583. begin
  584. { release the normal memory at least }
  585. i:=SysFreeMemSize(pp,ppsize);
  586. { return the correct size }
  587. dec(i,sizeof(theap_mem_info)+extra_size);
  588. if add_tail then
  589. dec(i,sizeof(ptruint));
  590. InternalFreeMemSize:=i;
  591. end else
  592. InternalFreeMemSize:=size;
  593. end;
  594. function TraceFreeMemSize(p:pointer;size:ptruint):ptruint;
  595. var
  596. loc_info: pheap_info;
  597. pp: pheap_mem_info;
  598. release_lock: boolean;
  599. begin
  600. if p=nil then
  601. begin
  602. TraceFreeMemSize:=0;
  603. exit;
  604. end;
  605. loc_info:=@heap_info;
  606. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  607. release_lock:=false;
  608. if @loc_info^.heap_free_todo <> pp^.todolist then
  609. begin
  610. if pp^.todolist = main_orig_todolist then
  611. pp^.todolist := main_relo_todolist;
  612. entercriticalsection(todo_lock);
  613. release_lock:=true;
  614. if pp^.todolist = @orphaned_info.heap_free_todo then
  615. begin
  616. loc_info := @orphaned_info;
  617. end else
  618. if pp^.todolist <> @loc_info^.heap_free_todo then
  619. begin
  620. { allocated in different heap, push to that todolist }
  621. pp^.todonext := pp^.todolist^;
  622. pp^.todolist^ := pp;
  623. TraceFreeMemSize := pp^.size;
  624. leavecriticalsection(todo_lock);
  625. exit;
  626. end;
  627. end;
  628. TraceFreeMemSize:=InternalFreeMemSize(loc_info,p,pp,size,release_lock);
  629. end;
  630. function TraceMemSize(p:pointer):ptruint;
  631. var
  632. pp : pheap_mem_info;
  633. begin
  634. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  635. TraceMemSize:=pp^.size;
  636. end;
  637. function TraceFreeMem(p:pointer):ptruint;
  638. var
  639. l : ptruint;
  640. pp : pheap_mem_info;
  641. begin
  642. if p=nil then
  643. begin
  644. TraceFreeMem:=0;
  645. exit;
  646. end;
  647. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  648. l:=SysMemSize(pp);
  649. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  650. if add_tail then
  651. dec(l,sizeof(ptruint));
  652. { this can never happend normaly }
  653. if pp^.size>l then
  654. begin
  655. if useownfile then
  656. dump_wrong_size(pp,l,ownfile)
  657. else
  658. dump_wrong_size(pp,l,stderr);
  659. {$ifdef EXTRA}
  660. dump_wrong_size(pp,l,error_file);
  661. {$endif EXTRA}
  662. end;
  663. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  664. end;
  665. {*****************************************************************************
  666. ReAllocMem
  667. *****************************************************************************}
  668. function TraceReAllocMem(var p:pointer;size:ptruint):Pointer;
  669. var
  670. newP: pointer;
  671. allocsize,
  672. movesize,
  673. i : ptruint;
  674. oldbp,
  675. bp : pointer;
  676. pl : pdword;
  677. pp : pheap_mem_info;
  678. oldsize,
  679. oldextrasize,
  680. oldexactsize : ptruint;
  681. old_fill_extra_info_proc : tfillextrainfoproc;
  682. old_display_extra_info_proc : tdisplayextrainfoproc;
  683. loc_info: pheap_info;
  684. begin
  685. { Free block? }
  686. if size=0 then
  687. begin
  688. if p<>nil then
  689. TraceFreeMem(p);
  690. p:=nil;
  691. TraceReallocMem:=P;
  692. exit;
  693. end;
  694. { Allocate a new block? }
  695. if p=nil then
  696. begin
  697. p:=TraceGetMem(size);
  698. TraceReallocMem:=P;
  699. exit;
  700. end;
  701. { Resize block }
  702. loc_info:=@heap_info;
  703. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  704. { test block }
  705. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  706. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  707. begin
  708. loc_info^.error_in_heap:=true;
  709. if useownfile then
  710. dump_error(pp,ownfile)
  711. else
  712. dump_error(pp,stderr);
  713. {$ifdef EXTRA}
  714. dump_error(pp,error_file);
  715. {$endif EXTRA}
  716. { don't release anything in this case !! }
  717. if haltonerror then halt(1);
  718. exit;
  719. end;
  720. { save info }
  721. oldsize:=pp^.size;
  722. oldextrasize:=pp^.extra_info_size;
  723. oldexactsize:=pp^.exact_info_size;
  724. if pp^.extra_info_size>0 then
  725. begin
  726. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  727. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  728. end;
  729. { Do the real ReAllocMem, but alloc also for the info block }
  730. {$ifdef cpuarm}
  731. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+pp^.extra_info_size;
  732. {$else cpuarm}
  733. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  734. {$endif cpuarm}
  735. if add_tail then
  736. inc(allocsize,sizeof(ptruint));
  737. { Try to resize the block, if not possible we need to do a
  738. getmem, move data, freemem }
  739. if not SysTryResizeMem(pp,allocsize) then
  740. begin
  741. { get a new block }
  742. newP := TraceGetMem(size);
  743. { move the data }
  744. if newP <> nil then
  745. begin
  746. movesize:=TraceMemSize(p);
  747. {if the old size is larger than the new size,
  748. move only the new size}
  749. if movesize>size then
  750. movesize:=size;
  751. move(p^,newP^,movesize);
  752. end;
  753. { release p }
  754. traceFreeMem(p);
  755. { return the new pointer }
  756. p:=newp;
  757. traceReAllocMem := newp;
  758. exit;
  759. end;
  760. { Recreate the info block }
  761. pp^.sig:=$DEADBEEF;
  762. pp^.size:=size;
  763. pp^.extra_info_size:=oldextrasize;
  764. pp^.exact_info_size:=oldexactsize;
  765. { add the new extra_info and tail }
  766. if pp^.extra_info_size>0 then
  767. begin
  768. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  769. fillchar(pp^.extra_info^,extra_info_size,0);
  770. pp^.extra_info^.check:=$12345678;
  771. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  772. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  773. if assigned(pp^.extra_info^.fillproc) then
  774. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  775. end
  776. else
  777. pp^.extra_info:=nil;
  778. if add_tail then
  779. begin
  780. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptruint);
  781. {$ifdef FPC_SUPPORTS_UNALIGNED}
  782. unaligned(pl^):=$DEADBEEF;
  783. {$else FPC_SUPPORTS_UNALIGNED}
  784. pl^:=$DEADBEEF;
  785. {$endif FPC_SUPPORTS_UNALIGNED}
  786. end;
  787. { adjust like a freemem and then a getmem, so you get correct
  788. results in the summary display }
  789. inc(loc_info^.freemem_size,oldsize);
  790. inc(loc_info^.freemem8_size,(oldsize+7) and not 7);
  791. inc(loc_info^.getmem_size,size);
  792. inc(loc_info^.getmem8_size,(size+7) and not 7);
  793. { generate new backtrace }
  794. bp:=get_caller_frame(get_frame);
  795. if (bp>=StackBottom) and (bp<(StackBottom + StackLength)) then
  796. for i:=1 to tracesize do
  797. begin
  798. pp^.calls[i]:=get_caller_addr(bp);
  799. oldbp:=bp;
  800. bp:=get_caller_frame(bp);
  801. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  802. break;
  803. end;
  804. { regenerate signature }
  805. if usecrc then
  806. pp^.sig:=calculate_sig(pp);
  807. { return the pointer }
  808. p:=pointer(pp)+sizeof(theap_mem_info);
  809. TraceReAllocmem:=p;
  810. end;
  811. {*****************************************************************************
  812. Check pointer
  813. *****************************************************************************}
  814. {$ifndef Unix}
  815. {$S-}
  816. {$endif}
  817. {$ifdef go32v2}
  818. var
  819. __stklen : longword;external name '__stklen';
  820. __stkbottom : longword;external name '__stkbottom';
  821. edata : longword; external name 'edata';
  822. {$endif go32v2}
  823. {$ifdef linux}
  824. var
  825. etext: ptruint; external name '_etext';
  826. edata : ptruint; external name '_edata';
  827. eend : ptruint; external name '_end';
  828. {$endif}
  829. {$ifdef os2}
  830. (* Currently still EMX based - possibly to be changed in the future. *)
  831. var
  832. etext: ptruint; external name '_etext';
  833. edata : ptruint; external name '_edata';
  834. eend : ptruint; external name '_end';
  835. {$endif}
  836. {$ifdef windows}
  837. var
  838. sdata : ptruint; external name '__data_start__';
  839. edata : ptruint; external name '__data_end__';
  840. sbss : ptruint; external name '__bss_start__';
  841. ebss : ptruint; external name '__bss_end__';
  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. data_end : longword;
  859. {$endif go32v2}
  860. {$ifdef morphos}
  861. stack_top: longword;
  862. {$endif morphos}
  863. ptext : ^text;
  864. label
  865. _exit;
  866. begin
  867. if p=nil then
  868. runerror(204);
  869. i:=0;
  870. loc_info:=@heap_info;
  871. if useownfile then
  872. ptext:=@ownfile
  873. else
  874. ptext:=@stderr;
  875. {$ifdef go32v2}
  876. if ptruint(p)<$1000 then
  877. runerror(216);
  878. asm
  879. movl %ebp,get_ebp
  880. leal edata,%eax
  881. movl %eax,data_end
  882. end;
  883. stack_top:=__stkbottom+__stklen;
  884. { allow all between start of code and end of data }
  885. if ptruint(p)<=data_end then
  886. goto _exit;
  887. { stack can be above heap !! }
  888. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  889. goto _exit;
  890. {$endif go32v2}
  891. { I don't know where the stack is in other OS !! }
  892. {$ifdef windows}
  893. { inside stack ? }
  894. if (ptruint(p)>ptruint(get_frame)) and
  895. (p<StackTop) then
  896. goto _exit;
  897. { inside data ? }
  898. if (ptruint(p)>=ptruint(@sdata)) and (ptruint(p)<ptruint(@edata)) then
  899. goto _exit;
  900. { inside bss ? }
  901. if (ptruint(p)>=ptruint(@sbss)) and (ptruint(p)<ptruint(@ebss)) then
  902. goto _exit;
  903. {$endif windows}
  904. {$IFDEF OS2}
  905. { inside stack ? }
  906. if (PtrUInt (P) > PtrUInt (Get_Frame)) and
  907. (PtrUInt (P) < PtrUInt (StackTop)) then
  908. goto _exit;
  909. { inside data or bss ? }
  910. if (PtrUInt (P) >= PtrUInt (@etext)) and (PtrUInt (P) < PtrUInt (@eend)) then
  911. goto _exit;
  912. {$ENDIF OS2}
  913. {$ifdef linux}
  914. { inside stack ? }
  915. if (ptruint(p)>ptruint(get_frame)) and
  916. (ptruint(p)<$c0000000) then //todo: 64bit!
  917. goto _exit;
  918. { inside data or bss ? }
  919. if (ptruint(p)>=ptruint(@etext)) and (ptruint(p)<ptruint(@eend)) then
  920. goto _exit;
  921. {$endif linux}
  922. {$ifdef morphos}
  923. { inside stack ? }
  924. stack_top:=ptruint(StackBottom)+StackLength;
  925. if (ptruint(p)<stack_top) and (ptruint(p)>ptruint(StackBottom)) then
  926. goto _exit;
  927. { inside data or bss ? }
  928. {$WARNING data and bss checking missing }
  929. {$endif morphos}
  930. {$ifdef darwin}
  931. {$warning No checkpointer support yet for Darwin}
  932. exit;
  933. {$endif}
  934. {$ifdef BEOS}
  935. // if we find the address in a known area in our current process,
  936. // then it is a valid one
  937. if area_for(p) <> B_ERROR then
  938. goto _exit;
  939. {$endif BEOS}
  940. { first try valid list faster }
  941. {$ifdef EXTRA}
  942. pp:=loc_info^.heap_valid_last;
  943. while pp<>nil do
  944. begin
  945. { inside this valid block ! }
  946. { we can be changing the extrainfo !! }
  947. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  948. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  949. begin
  950. { check allocated block }
  951. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  952. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  953. { special case of the fill_extra_info call }
  954. ((pp=loc_info^.heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  955. and loc_info^.inside_trace_getmem) then
  956. goto _exit
  957. else
  958. begin
  959. writeln(ptext^,'corrupted heap_mem_info');
  960. dump_error(pp,ptext^);
  961. halt(1);
  962. end;
  963. end
  964. else
  965. pp:=pp^.prev_valid;
  966. inc(i);
  967. if i>loc_info^.getmem_cnt-loc_info^.freemem_cnt then
  968. begin
  969. writeln(ptext^,'error in linked list of heap_mem_info');
  970. halt(1);
  971. end;
  972. end;
  973. i:=0;
  974. {$endif EXTRA}
  975. pp:=loc_info^.heap_mem_root;
  976. while pp<>nil do
  977. begin
  978. { inside this block ! }
  979. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  980. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  981. { allocated block }
  982. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  983. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  984. goto _exit
  985. else
  986. begin
  987. writeln(ptext^,'pointer $',hexstr(p),' points into invalid memory block');
  988. dump_error(pp,ptext^);
  989. runerror(204);
  990. end;
  991. pp:=pp^.previous;
  992. inc(i);
  993. if i>loc_info^.getmem_cnt then
  994. begin
  995. writeln(ptext^,'error in linked list of heap_mem_info');
  996. halt(1);
  997. end;
  998. end;
  999. writeln(ptext^,'pointer $',hexstr(p),' does not point to valid memory block');
  1000. dump_error(p,ptext^);
  1001. runerror(204);
  1002. _exit:
  1003. end;
  1004. {*****************************************************************************
  1005. Dump Heap
  1006. *****************************************************************************}
  1007. procedure dumpheap;
  1008. var
  1009. pp : pheap_mem_info;
  1010. i : ptrint;
  1011. ExpectedHeapFree : ptruint;
  1012. status : TFPCHeapStatus;
  1013. ptext : ^text;
  1014. loc_info: pheap_info;
  1015. begin
  1016. loc_info:=@heap_info;
  1017. if useownfile then
  1018. ptext:=@ownfile
  1019. else
  1020. ptext:=@stderr;
  1021. pp:=loc_info^.heap_mem_root;
  1022. Writeln(ptext^,'Heap dump by heaptrc unit');
  1023. Writeln(ptext^,loc_info^.getmem_cnt, ' memory blocks allocated : ',
  1024. loc_info^.getmem_size,'/',loc_info^.getmem8_size);
  1025. Writeln(ptext^,loc_info^.freemem_cnt,' memory blocks freed : ',
  1026. loc_info^.freemem_size,'/',loc_info^.freemem8_size);
  1027. Writeln(ptext^,loc_info^.getmem_cnt-loc_info^.freemem_cnt,
  1028. ' unfreed memory blocks : ',loc_info^.getmem_size-loc_info^.freemem_size);
  1029. status:=SysGetFPCHeapStatus;
  1030. Write(ptext^,'True heap size : ',status.CurrHeapSize);
  1031. if EntryMemUsed > 0 then
  1032. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  1033. else
  1034. Writeln(ptext^);
  1035. Writeln(ptext^,'True free heap : ',status.CurrHeapFree);
  1036. ExpectedHeapFree:=status.CurrHeapSize
  1037. -(loc_info^.getmem8_size-loc_info^.freemem8_size)
  1038. -(loc_info^.getmem_cnt-loc_info^.freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)
  1039. -EntryMemUsed;
  1040. If ExpectedHeapFree<>status.CurrHeapFree then
  1041. Writeln(ptext^,'Should be : ',ExpectedHeapFree);
  1042. i:=loc_info^.getmem_cnt-loc_info^.freemem_cnt;
  1043. while pp<>nil do
  1044. begin
  1045. if i<0 then
  1046. begin
  1047. Writeln(ptext^,'Error in heap memory list');
  1048. Writeln(ptext^,'More memory blocks than expected');
  1049. exit;
  1050. end;
  1051. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  1052. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  1053. begin
  1054. { this one was not released !! }
  1055. if exitcode<>203 then
  1056. call_stack(pp,ptext^);
  1057. dec(i);
  1058. end
  1059. else if pp^.sig<>$AAAAAAAA then
  1060. begin
  1061. dump_error(pp,ptext^);
  1062. {$ifdef EXTRA}
  1063. dump_error(pp,error_file);
  1064. {$endif EXTRA}
  1065. loc_info^.error_in_heap:=true;
  1066. end
  1067. {$ifdef EXTRA}
  1068. else if pp^.release_sig<>calculate_release_sig(pp) then
  1069. begin
  1070. dump_change_after(pp,ptext^);
  1071. dump_change_after(pp,error_file);
  1072. loc_info^.error_in_heap:=true;
  1073. end
  1074. {$endif EXTRA}
  1075. ;
  1076. pp:=pp^.previous;
  1077. end;
  1078. if HaltOnNotReleased and (loc_info^.getmem_cnt<>loc_info^.freemem_cnt) then
  1079. exitcode:=203;
  1080. end;
  1081. {*****************************************************************************
  1082. AllocMem
  1083. *****************************************************************************}
  1084. function TraceAllocMem(size:ptruint):Pointer;
  1085. begin
  1086. TraceAllocMem:=SysAllocMem(size);
  1087. end;
  1088. {*****************************************************************************
  1089. No specific tracing calls
  1090. *****************************************************************************}
  1091. procedure TraceInitThread;
  1092. var
  1093. loc_info: pheap_info;
  1094. begin
  1095. loc_info := @heap_info;
  1096. {$ifdef EXTRA}
  1097. loc_info^.heap_valid_first := nil;
  1098. loc_info^.heap_valid_last := nil;
  1099. {$endif}
  1100. loc_info^.heap_mem_root := nil;
  1101. loc_info^.getmem_cnt := 0;
  1102. loc_info^.freemem_cnt := 0;
  1103. loc_info^.getmem_size := 0;
  1104. loc_info^.freemem_size := 0;
  1105. loc_info^.getmem8_size := 0;
  1106. loc_info^.freemem8_size := 0;
  1107. loc_info^.error_in_heap := false;
  1108. loc_info^.inside_trace_getmem := false;
  1109. EntryMemUsed := SysGetFPCHeapStatus.CurrHeapUsed;
  1110. end;
  1111. procedure TraceRelocateHeap;
  1112. begin
  1113. main_relo_todolist := @heap_info.heap_free_todo;
  1114. initcriticalsection(todo_lock);
  1115. end;
  1116. procedure move_heap_info(src_info, dst_info: pheap_info);
  1117. var
  1118. heap_mem: pheap_mem_info;
  1119. begin
  1120. if src_info^.heap_free_todo <> nil then
  1121. finish_heap_free_todo_list(src_info);
  1122. if dst_info^.heap_free_todo <> nil then
  1123. finish_heap_free_todo_list(dst_info);
  1124. heap_mem := src_info^.heap_mem_root;
  1125. if heap_mem <> nil then
  1126. begin
  1127. repeat
  1128. heap_mem^.todolist := @dst_info^.heap_free_todo;
  1129. if heap_mem^.previous = nil then break;
  1130. heap_mem := heap_mem^.previous;
  1131. until false;
  1132. heap_mem^.previous := dst_info^.heap_mem_root;
  1133. if dst_info^.heap_mem_root <> nil then
  1134. dst_info^.heap_mem_root^.next := heap_mem;
  1135. dst_info^.heap_mem_root := src_info^.heap_mem_root;
  1136. end;
  1137. inc(dst_info^.getmem_cnt, src_info^.getmem_cnt);
  1138. inc(dst_info^.getmem_size, src_info^.getmem_size);
  1139. inc(dst_info^.getmem8_size, src_info^.getmem8_size);
  1140. inc(dst_info^.freemem_cnt, src_info^.freemem_cnt);
  1141. inc(dst_info^.freemem_size, src_info^.freemem_size);
  1142. inc(dst_info^.freemem8_size, src_info^.freemem8_size);
  1143. dst_info^.error_in_heap := dst_info^.error_in_heap or src_info^.error_in_heap;
  1144. {$ifdef EXTRA}
  1145. if assigned(dst_info^.heap_valid_first) then
  1146. dst_info^.heap_valid_first^.prev_valid := src_info^.heap_valid_last
  1147. else
  1148. dst_info^.heap_valid_last := src_info^.heap_valid_last;
  1149. dst_info^.heap_valid_first := src_info^.heap_valid_first;
  1150. {$endif}
  1151. end;
  1152. procedure TraceExitThread;
  1153. var
  1154. loc_info: pheap_info;
  1155. begin
  1156. loc_info := @heap_info;
  1157. entercriticalsection(todo_lock);
  1158. move_heap_info(loc_info, @orphaned_info);
  1159. leavecriticalsection(todo_lock);
  1160. end;
  1161. function TraceGetHeapStatus:THeapStatus;
  1162. begin
  1163. TraceGetHeapStatus:=SysGetHeapStatus;
  1164. end;
  1165. function TraceGetFPCHeapStatus:TFPCHeapStatus;
  1166. begin
  1167. TraceGetFPCHeapStatus:=SysGetFPCHeapStatus;
  1168. end;
  1169. {*****************************************************************************
  1170. Program Hooks
  1171. *****************************************************************************}
  1172. Procedure SetHeapTraceOutput(const name : string);
  1173. var i : ptruint;
  1174. begin
  1175. if useownfile then
  1176. begin
  1177. useownfile:=false;
  1178. close(ownfile);
  1179. end;
  1180. assign(ownfile,name);
  1181. {$I-}
  1182. append(ownfile);
  1183. if IOResult<>0 then
  1184. Rewrite(ownfile);
  1185. {$I+}
  1186. useownfile:=true;
  1187. for i:=0 to Paramcount do
  1188. write(ownfile,paramstr(i),' ');
  1189. writeln(ownfile);
  1190. end;
  1191. procedure SetHeapExtraInfo( size : ptruint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  1192. begin
  1193. { the total size must stay multiple of 8, also allocate 2 pointers for
  1194. the fill and display procvars }
  1195. exact_info_size:=size + sizeof(theap_extra_info);
  1196. extra_info_size:=(exact_info_size+7) and not 7;
  1197. fill_extra_info_proc:=fillproc;
  1198. display_extra_info_proc:=displayproc;
  1199. end;
  1200. {*****************************************************************************
  1201. Install MemoryManager
  1202. *****************************************************************************}
  1203. const
  1204. TraceManager:TMemoryManager=(
  1205. NeedLock : true;
  1206. Getmem : @TraceGetMem;
  1207. Freemem : @TraceFreeMem;
  1208. FreememSize : @TraceFreeMemSize;
  1209. AllocMem : @TraceAllocMem;
  1210. ReAllocMem : @TraceReAllocMem;
  1211. MemSize : @TraceMemSize;
  1212. InitThread: @TraceInitThread;
  1213. DoneThread: @TraceExitThread;
  1214. RelocateHeap: @TraceRelocateHeap;
  1215. GetHeapStatus : @TraceGetHeapStatus;
  1216. GetFPCHeapStatus : @TraceGetFPCHeapStatus;
  1217. );
  1218. procedure TraceInit;
  1219. begin
  1220. MakeCRC32Tbl;
  1221. main_orig_todolist := @heap_info.heap_free_todo;
  1222. main_relo_todolist := nil;
  1223. TraceInitThread;
  1224. SetMemoryManager(TraceManager);
  1225. useownfile:=false;
  1226. if outputstr <> '' then
  1227. SetHeapTraceOutput(outputstr);
  1228. {$ifdef EXTRA}
  1229. Assign(error_file,'heap.err');
  1230. Rewrite(error_file);
  1231. {$endif EXTRA}
  1232. end;
  1233. procedure TraceExit;
  1234. begin
  1235. { no dump if error
  1236. because this gives long long listings }
  1237. { clear inoutres, in case the program that quit didn't }
  1238. ioresult;
  1239. if (exitcode<>0) and (erroraddr<>nil) then
  1240. begin
  1241. if useownfile then
  1242. begin
  1243. Writeln(ownfile,'No heap dump by heaptrc unit');
  1244. Writeln(ownfile,'Exitcode = ',exitcode);
  1245. end
  1246. else
  1247. begin
  1248. Writeln(stderr,'No heap dump by heaptrc unit');
  1249. Writeln(stderr,'Exitcode = ',exitcode);
  1250. end;
  1251. if useownfile then
  1252. begin
  1253. useownfile:=false;
  1254. close(ownfile);
  1255. end;
  1256. exit;
  1257. end;
  1258. move_heap_info(@orphaned_info, @heap_info);
  1259. dumpheap;
  1260. if heap_info.error_in_heap and (exitcode=0) then
  1261. exitcode:=203;
  1262. if main_relo_todolist <> nil then
  1263. donecriticalsection(todo_lock);
  1264. {$ifdef EXTRA}
  1265. Close(error_file);
  1266. {$endif EXTRA}
  1267. if useownfile then
  1268. begin
  1269. useownfile:=false;
  1270. close(ownfile);
  1271. end;
  1272. end;
  1273. {$if defined(win32) or defined(win64)}
  1274. function GetEnvironmentStrings : pchar; stdcall;
  1275. external 'kernel32' name 'GetEnvironmentStringsA';
  1276. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  1277. external 'kernel32' name 'FreeEnvironmentStringsA';
  1278. Function GetEnv(envvar: string): string;
  1279. var
  1280. s : string;
  1281. i : ptruint;
  1282. hp,p : pchar;
  1283. begin
  1284. getenv:='';
  1285. p:=GetEnvironmentStrings;
  1286. hp:=p;
  1287. while hp^<>#0 do
  1288. begin
  1289. s:=strpas(hp);
  1290. i:=pos('=',s);
  1291. if upcase(copy(s,1,i-1))=upcase(envvar) then
  1292. begin
  1293. getenv:=copy(s,i+1,length(s)-i);
  1294. break;
  1295. end;
  1296. { next string entry}
  1297. hp:=hp+strlen(hp)+1;
  1298. end;
  1299. FreeEnvironmentStrings(p);
  1300. end;
  1301. {$else defined(win32) or defined(win64)}
  1302. {$ifdef wince}
  1303. Function GetEnv(P:string):Pchar;
  1304. begin
  1305. { WinCE does not have environment strings.
  1306. Add some way to specify heaptrc options? }
  1307. GetEnv:=nil;
  1308. end;
  1309. {$else wince}
  1310. Function GetEnv(P:string):Pchar;
  1311. {
  1312. Searches the environment for a string with name p and
  1313. returns a pchar to it's value.
  1314. A pchar is used to accomodate for strings of length > 255
  1315. }
  1316. var
  1317. ep : ppchar;
  1318. i : ptruint;
  1319. found : boolean;
  1320. Begin
  1321. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1322. ep:=envp;
  1323. found:=false;
  1324. if ep<>nil then
  1325. begin
  1326. while (not found) and (ep^<>nil) do
  1327. begin
  1328. found:=true;
  1329. for i:=1 to length(p) do
  1330. if p[i]<>ep^[i-1] then
  1331. begin
  1332. found:=false;
  1333. break;
  1334. end;
  1335. if not found then
  1336. inc(ep);
  1337. end;
  1338. end;
  1339. if found then
  1340. getenv:=ep^+length(p)
  1341. else
  1342. getenv:=nil;
  1343. end;
  1344. {$endif wince}
  1345. {$endif win32}
  1346. procedure LoadEnvironment;
  1347. var
  1348. i,j : ptruint;
  1349. s : string;
  1350. begin
  1351. s:=Getenv('HEAPTRC');
  1352. if pos('keepreleased',s)>0 then
  1353. keepreleased:=true;
  1354. if pos('disabled',s)>0 then
  1355. useheaptrace:=false;
  1356. if pos('nohalt',s)>0 then
  1357. haltonerror:=false;
  1358. if pos('haltonnotreleased',s)>0 then
  1359. HaltOnNotReleased :=true;
  1360. i:=pos('log=',s);
  1361. if i>0 then
  1362. begin
  1363. outputstr:=copy(s,i+4,255);
  1364. j:=pos(' ',outputstr);
  1365. if j=0 then
  1366. j:=length(outputstr)+1;
  1367. delete(outputstr,j,255);
  1368. end;
  1369. end;
  1370. Initialization
  1371. LoadEnvironment;
  1372. { heaptrc can be disabled from the environment }
  1373. if useheaptrace then
  1374. TraceInit;
  1375. finalization
  1376. if useheaptrace then
  1377. TraceExit;
  1378. end.