heaptrc.pp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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. { valid bp? }
  430. if (bp>=StackBottom) and (bp<(StackBottom + StackLength)) then
  431. pp^.calls[i]:=get_caller_addr(bp);
  432. oldbp:=bp;
  433. bp:=get_caller_frame(bp);
  434. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  435. bp:=nil;
  436. end;
  437. { insert in the linked list }
  438. if loc_info^.heap_mem_root<>nil then
  439. loc_info^.heap_mem_root^.next:=pp;
  440. pp^.previous:=loc_info^.heap_mem_root;
  441. pp^.next:=nil;
  442. {$ifdef EXTRA}
  443. pp^.prev_valid:=loc_info^.heap_valid_last;
  444. loc_info^.heap_valid_last:=pp;
  445. if not assigned(loc_info^.heap_valid_first) then
  446. loc_info^.heap_valid_first:=pp;
  447. {$endif EXTRA}
  448. loc_info^.heap_mem_root:=pp;
  449. { must be changed before fill_extra_info is called
  450. because checkpointer can be called from within
  451. fill_extra_info PM }
  452. inc(loc_info^.getmem_cnt);
  453. { update the signature }
  454. if usecrc then
  455. pp^.sig:=calculate_sig(pp);
  456. TraceGetmem:=p;
  457. end;
  458. {*****************************************************************************
  459. TraceFreeMem
  460. *****************************************************************************}
  461. function CheckFreeMemSize(loc_info: pheap_info; pp: pheap_mem_info;
  462. size, ppsize: ptruint): boolean; inline;
  463. var
  464. i: ptruint;
  465. bp : pointer;
  466. ptext : ^text;
  467. {$ifdef EXTRA}
  468. pp2 : pheap_mem_info;
  469. {$endif}
  470. begin
  471. if useownfile then
  472. ptext:=@ownfile
  473. else
  474. ptext:=@stderr;
  475. inc(loc_info^.freemem_size,size);
  476. inc(loc_info^.freemem8_size,(size+7) and not 7);
  477. if not quicktrace then
  478. begin
  479. if not(is_in_getmem_list(loc_info, pp)) then
  480. RunError(204);
  481. end;
  482. if (pp^.sig=$AAAAAAAA) and not usecrc then
  483. begin
  484. loc_info^.error_in_heap:=true;
  485. dump_already_free(pp,ptext^);
  486. if haltonerror then halt(1);
  487. end
  488. else if ((pp^.sig<>$DEADBEEF) or usecrc) and
  489. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  490. begin
  491. loc_info^.error_in_heap:=true;
  492. dump_error(pp,ptext^);
  493. {$ifdef EXTRA}
  494. dump_error(pp,error_file);
  495. {$endif EXTRA}
  496. { don't release anything in this case !! }
  497. if haltonerror then halt(1);
  498. exit;
  499. end
  500. else if pp^.size<>size then
  501. begin
  502. loc_info^.error_in_heap:=true;
  503. dump_wrong_size(pp,size,ptext^);
  504. {$ifdef EXTRA}
  505. dump_wrong_size(pp,size,error_file);
  506. {$endif EXTRA}
  507. if haltonerror then halt(1);
  508. { don't release anything in this case !! }
  509. exit;
  510. end;
  511. { now it is released !! }
  512. pp^.sig:=$AAAAAAAA;
  513. if not keepreleased then
  514. begin
  515. if pp^.next<>nil then
  516. pp^.next^.previous:=pp^.previous;
  517. if pp^.previous<>nil then
  518. pp^.previous^.next:=pp^.next;
  519. if pp=loc_info^.heap_mem_root then
  520. loc_info^.heap_mem_root:=loc_info^.heap_mem_root^.previous;
  521. end
  522. else
  523. begin
  524. bp:=get_caller_frame(get_frame);
  525. for i:=(tracesize div 2)+1 to tracesize do
  526. begin
  527. pp^.calls[i]:=get_caller_addr(bp);
  528. bp:=get_caller_frame(bp);
  529. end;
  530. end;
  531. inc(loc_info^.freemem_cnt);
  532. { clear the memory, $F0 will lead to GFP if used as pointer ! }
  533. fillchar((pointer(pp)+sizeof(theap_mem_info))^,size,#240);
  534. { this way we keep all info about all released memory !! }
  535. if keepreleased then
  536. begin
  537. {$ifdef EXTRA}
  538. { We want to check if the memory was changed after release !! }
  539. pp^.release_sig:=calculate_release_sig(pp);
  540. if pp=loc_info^.heap_valid_last then
  541. begin
  542. loc_info^.heap_valid_last:=pp^.prev_valid;
  543. if pp=loc_info^.heap_valid_first then
  544. loc_info^.heap_valid_first:=nil;
  545. exit(false);
  546. end;
  547. pp2:=loc_info^.heap_valid_last;
  548. while assigned(pp2) do
  549. begin
  550. if pp2^.prev_valid=pp then
  551. begin
  552. pp2^.prev_valid:=pp^.prev_valid;
  553. if pp=loc_info^.heap_valid_first then
  554. loc_info^.heap_valid_first:=pp2;
  555. exit(false);
  556. end
  557. else
  558. pp2:=pp2^.prev_valid;
  559. end;
  560. {$endif EXTRA}
  561. exit(false);
  562. end;
  563. CheckFreeMemSize:=true;
  564. end;
  565. function InternalFreeMemSize(loc_info: pheap_info; p: pointer; pp: pheap_mem_info;
  566. size: ptruint; release_todo_lock: boolean): ptruint;
  567. var
  568. i,ppsize : ptruint;
  569. bp : pointer;
  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. for i:=1 to tracesize do
  796. begin
  797. pp^.calls[i]:=get_caller_addr(bp);
  798. oldbp:=bp;
  799. bp:=get_caller_frame(bp);
  800. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  801. bp:=nil;
  802. end;
  803. { regenerate signature }
  804. if usecrc then
  805. pp^.sig:=calculate_sig(pp);
  806. { return the pointer }
  807. p:=pointer(pp)+sizeof(theap_mem_info);
  808. TraceReAllocmem:=p;
  809. end;
  810. {*****************************************************************************
  811. Check pointer
  812. *****************************************************************************}
  813. {$ifndef Unix}
  814. {$S-}
  815. {$endif}
  816. {$ifdef go32v2}
  817. var
  818. __stklen : longword;external name '__stklen';
  819. __stkbottom : longword;external name '__stkbottom';
  820. edata : longword; external name 'edata';
  821. {$endif go32v2}
  822. {$ifdef linux}
  823. var
  824. etext: ptruint; external name '_etext';
  825. edata : ptruint; external name '_edata';
  826. eend : ptruint; external name '_end';
  827. {$endif}
  828. {$ifdef os2}
  829. (* Currently still EMX based - possibly to be changed in the future. *)
  830. var
  831. etext: ptruint; external name '_etext';
  832. edata : ptruint; external name '_edata';
  833. eend : ptruint; external name '_end';
  834. {$endif}
  835. {$ifdef windows}
  836. var
  837. sdata : ptruint; external name '__data_start__';
  838. edata : ptruint; external name '__data_end__';
  839. sbss : ptruint; external name '__bss_start__';
  840. ebss : ptruint; external name '__bss_end__';
  841. {$endif}
  842. procedure CheckPointer(p : pointer); [public, alias : 'FPC_CHECKPOINTER'];
  843. var
  844. i : ptruint;
  845. pp : pheap_mem_info;
  846. loc_info: pheap_info;
  847. {$ifdef go32v2}
  848. get_ebp,stack_top : longword;
  849. data_end : longword;
  850. {$endif go32v2}
  851. {$ifdef morphos}
  852. stack_top: longword;
  853. {$endif morphos}
  854. ptext : ^text;
  855. label
  856. _exit;
  857. begin
  858. if p=nil then
  859. runerror(204);
  860. i:=0;
  861. loc_info:=@heap_info;
  862. if useownfile then
  863. ptext:=@ownfile
  864. else
  865. ptext:=@stderr;
  866. {$ifdef go32v2}
  867. if ptruint(p)<$1000 then
  868. runerror(216);
  869. asm
  870. movl %ebp,get_ebp
  871. leal edata,%eax
  872. movl %eax,data_end
  873. end;
  874. stack_top:=__stkbottom+__stklen;
  875. { allow all between start of code and end of data }
  876. if ptruint(p)<=data_end then
  877. goto _exit;
  878. { stack can be above heap !! }
  879. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  880. goto _exit;
  881. {$endif go32v2}
  882. { I don't know where the stack is in other OS !! }
  883. {$ifdef windows}
  884. { inside stack ? }
  885. if (ptruint(p)>ptruint(get_frame)) and
  886. (p<StackTop) then
  887. goto _exit;
  888. { inside data ? }
  889. if (ptruint(p)>=ptruint(@sdata)) and (ptruint(p)<ptruint(@edata)) then
  890. goto _exit;
  891. { inside bss ? }
  892. if (ptruint(p)>=ptruint(@sbss)) and (ptruint(p)<ptruint(@ebss)) then
  893. goto _exit;
  894. {$endif windows}
  895. {$IFDEF OS2}
  896. { inside stack ? }
  897. if (PtrUInt (P) > PtrUInt (Get_Frame)) and
  898. (PtrUInt (P) < PtrUInt (StackTop)) then
  899. goto _exit;
  900. { inside data or bss ? }
  901. if (PtrUInt (P) >= PtrUInt (@etext)) and (PtrUInt (P) < PtrUInt (@eend)) then
  902. goto _exit;
  903. {$ENDIF OS2}
  904. {$ifdef linux}
  905. { inside stack ? }
  906. if (ptruint(p)>ptruint(get_frame)) and
  907. (ptruint(p)<$c0000000) then //todo: 64bit!
  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 linux}
  913. {$ifdef morphos}
  914. { inside stack ? }
  915. stack_top:=ptruint(StackBottom)+StackLength;
  916. if (ptruint(p)<stack_top) and (ptruint(p)>ptruint(StackBottom)) then
  917. goto _exit;
  918. { inside data or bss ? }
  919. {$WARNING data and bss checking missing }
  920. {$endif morphos}
  921. {$ifdef darwin}
  922. {$warning No checkpointer support yet for Darwin}
  923. exit;
  924. {$endif}
  925. { first try valid list faster }
  926. {$ifdef EXTRA}
  927. pp:=loc_info^.heap_valid_last;
  928. while pp<>nil do
  929. begin
  930. { inside this valid block ! }
  931. { we can be changing the extrainfo !! }
  932. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  933. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  934. begin
  935. { check allocated block }
  936. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  937. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  938. { special case of the fill_extra_info call }
  939. ((pp=loc_info^.heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  940. and loc_info^.inside_trace_getmem) then
  941. goto _exit
  942. else
  943. begin
  944. writeln(ptext^,'corrupted heap_mem_info');
  945. dump_error(pp,ptext^);
  946. halt(1);
  947. end;
  948. end
  949. else
  950. pp:=pp^.prev_valid;
  951. inc(i);
  952. if i>loc_info^.getmem_cnt-loc_info^.freemem_cnt then
  953. begin
  954. writeln(ptext^,'error in linked list of heap_mem_info');
  955. halt(1);
  956. end;
  957. end;
  958. i:=0;
  959. {$endif EXTRA}
  960. pp:=loc_info^.heap_mem_root;
  961. while pp<>nil do
  962. begin
  963. { inside this block ! }
  964. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  965. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  966. { allocated block }
  967. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  968. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  969. goto _exit
  970. else
  971. begin
  972. writeln(ptext^,'pointer $',hexstr(p),' points into invalid memory block');
  973. dump_error(pp,ptext^);
  974. runerror(204);
  975. end;
  976. pp:=pp^.previous;
  977. inc(i);
  978. if i>loc_info^.getmem_cnt then
  979. begin
  980. writeln(ptext^,'error in linked list of heap_mem_info');
  981. halt(1);
  982. end;
  983. end;
  984. writeln(ptext^,'pointer $',hexstr(p),' does not point to valid memory block');
  985. dump_error(p,ptext^);
  986. runerror(204);
  987. _exit:
  988. end;
  989. {*****************************************************************************
  990. Dump Heap
  991. *****************************************************************************}
  992. procedure dumpheap;
  993. var
  994. pp : pheap_mem_info;
  995. i : ptrint;
  996. ExpectedHeapFree : ptruint;
  997. status : TFPCHeapStatus;
  998. ptext : ^text;
  999. loc_info: pheap_info;
  1000. begin
  1001. loc_info:=@heap_info;
  1002. if useownfile then
  1003. ptext:=@ownfile
  1004. else
  1005. ptext:=@stderr;
  1006. pp:=loc_info^.heap_mem_root;
  1007. Writeln(ptext^,'Heap dump by heaptrc unit');
  1008. Writeln(ptext^,loc_info^.getmem_cnt, ' memory blocks allocated : ',
  1009. loc_info^.getmem_size,'/',loc_info^.getmem8_size);
  1010. Writeln(ptext^,loc_info^.freemem_cnt,' memory blocks freed : ',
  1011. loc_info^.freemem_size,'/',loc_info^.freemem8_size);
  1012. Writeln(ptext^,loc_info^.getmem_cnt-loc_info^.freemem_cnt,
  1013. ' unfreed memory blocks : ',loc_info^.getmem_size-loc_info^.freemem_size);
  1014. status:=SysGetFPCHeapStatus;
  1015. Write(ptext^,'True heap size : ',status.CurrHeapSize);
  1016. if EntryMemUsed > 0 then
  1017. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  1018. else
  1019. Writeln(ptext^);
  1020. Writeln(ptext^,'True free heap : ',status.CurrHeapFree);
  1021. ExpectedHeapFree:=status.CurrHeapSize
  1022. -(loc_info^.getmem8_size-loc_info^.freemem8_size)
  1023. -(loc_info^.getmem_cnt-loc_info^.freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)
  1024. -EntryMemUsed;
  1025. If ExpectedHeapFree<>status.CurrHeapFree then
  1026. Writeln(ptext^,'Should be : ',ExpectedHeapFree);
  1027. i:=loc_info^.getmem_cnt-loc_info^.freemem_cnt;
  1028. while pp<>nil do
  1029. begin
  1030. if i<0 then
  1031. begin
  1032. Writeln(ptext^,'Error in heap memory list');
  1033. Writeln(ptext^,'More memory blocks than expected');
  1034. exit;
  1035. end;
  1036. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  1037. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  1038. begin
  1039. { this one was not released !! }
  1040. if exitcode<>203 then
  1041. call_stack(pp,ptext^);
  1042. dec(i);
  1043. end
  1044. else if pp^.sig<>$AAAAAAAA then
  1045. begin
  1046. dump_error(pp,ptext^);
  1047. {$ifdef EXTRA}
  1048. dump_error(pp,error_file);
  1049. {$endif EXTRA}
  1050. loc_info^.error_in_heap:=true;
  1051. end
  1052. {$ifdef EXTRA}
  1053. else if pp^.release_sig<>calculate_release_sig(pp) then
  1054. begin
  1055. dump_change_after(pp,ptext^);
  1056. dump_change_after(pp,error_file);
  1057. loc_info^.error_in_heap:=true;
  1058. end
  1059. {$endif EXTRA}
  1060. ;
  1061. pp:=pp^.previous;
  1062. end;
  1063. if HaltOnNotReleased and (loc_info^.getmem_cnt<>loc_info^.freemem_cnt) then
  1064. exitcode:=203;
  1065. end;
  1066. {*****************************************************************************
  1067. AllocMem
  1068. *****************************************************************************}
  1069. function TraceAllocMem(size:ptruint):Pointer;
  1070. begin
  1071. TraceAllocMem:=SysAllocMem(size);
  1072. end;
  1073. {*****************************************************************************
  1074. No specific tracing calls
  1075. *****************************************************************************}
  1076. procedure TraceInitThread;
  1077. var
  1078. loc_info: pheap_info;
  1079. begin
  1080. loc_info := @heap_info;
  1081. {$ifdef EXTRA}
  1082. loc_info^.heap_valid_first := nil;
  1083. loc_info^.heap_valid_last := nil;
  1084. {$endif}
  1085. loc_info^.heap_mem_root := nil;
  1086. loc_info^.getmem_cnt := 0;
  1087. loc_info^.freemem_cnt := 0;
  1088. loc_info^.getmem_size := 0;
  1089. loc_info^.freemem_size := 0;
  1090. loc_info^.getmem8_size := 0;
  1091. loc_info^.freemem8_size := 0;
  1092. loc_info^.error_in_heap := false;
  1093. loc_info^.inside_trace_getmem := false;
  1094. EntryMemUsed := SysGetFPCHeapStatus.CurrHeapUsed;
  1095. end;
  1096. procedure TraceRelocateHeap;
  1097. begin
  1098. main_relo_todolist := @heap_info.heap_free_todo;
  1099. initcriticalsection(todo_lock);
  1100. end;
  1101. procedure move_heap_info(src_info, dst_info: pheap_info);
  1102. var
  1103. heap_mem: pheap_mem_info;
  1104. begin
  1105. if src_info^.heap_free_todo <> nil then
  1106. finish_heap_free_todo_list(src_info);
  1107. if dst_info^.heap_free_todo <> nil then
  1108. finish_heap_free_todo_list(dst_info);
  1109. heap_mem := src_info^.heap_mem_root;
  1110. if heap_mem <> nil then
  1111. begin
  1112. repeat
  1113. heap_mem^.todolist := @dst_info^.heap_free_todo;
  1114. if heap_mem^.previous = nil then break;
  1115. heap_mem := heap_mem^.previous;
  1116. until false;
  1117. heap_mem^.previous := dst_info^.heap_mem_root;
  1118. if dst_info^.heap_mem_root <> nil then
  1119. dst_info^.heap_mem_root^.next := heap_mem;
  1120. dst_info^.heap_mem_root := src_info^.heap_mem_root;
  1121. end;
  1122. inc(dst_info^.getmem_cnt, src_info^.getmem_cnt);
  1123. inc(dst_info^.getmem_size, src_info^.getmem_size);
  1124. inc(dst_info^.getmem8_size, src_info^.getmem8_size);
  1125. inc(dst_info^.freemem_cnt, src_info^.freemem_cnt);
  1126. inc(dst_info^.freemem_size, src_info^.freemem_size);
  1127. inc(dst_info^.freemem8_size, src_info^.freemem8_size);
  1128. dst_info^.error_in_heap := dst_info^.error_in_heap or src_info^.error_in_heap;
  1129. {$ifdef EXTRA}
  1130. if assigned(dst_info^.heap_valid_first) then
  1131. dst_info^.heap_valid_first^.prev_valid := src_info^.heap_valid_last
  1132. else
  1133. dst_info^.heap_valid_last := src_info^.heap_valid_last;
  1134. dst_info^.heap_valid_first := src_info^.heap_valid_first;
  1135. {$endif}
  1136. end;
  1137. procedure TraceExitThread;
  1138. var
  1139. loc_info: pheap_info;
  1140. heap_mem: pheap_mem_info;
  1141. begin
  1142. loc_info := @heap_info;
  1143. entercriticalsection(todo_lock);
  1144. move_heap_info(loc_info, @orphaned_info);
  1145. leavecriticalsection(todo_lock);
  1146. end;
  1147. function TraceGetHeapStatus:THeapStatus;
  1148. begin
  1149. TraceGetHeapStatus:=SysGetHeapStatus;
  1150. end;
  1151. function TraceGetFPCHeapStatus:TFPCHeapStatus;
  1152. begin
  1153. TraceGetFPCHeapStatus:=SysGetFPCHeapStatus;
  1154. end;
  1155. {*****************************************************************************
  1156. Program Hooks
  1157. *****************************************************************************}
  1158. Procedure SetHeapTraceOutput(const name : string);
  1159. var i : ptruint;
  1160. begin
  1161. if useownfile then
  1162. begin
  1163. useownfile:=false;
  1164. close(ownfile);
  1165. end;
  1166. assign(ownfile,name);
  1167. {$I-}
  1168. append(ownfile);
  1169. if IOResult<>0 then
  1170. Rewrite(ownfile);
  1171. {$I+}
  1172. useownfile:=true;
  1173. for i:=0 to Paramcount do
  1174. write(ownfile,paramstr(i),' ');
  1175. writeln(ownfile);
  1176. end;
  1177. procedure SetHeapExtraInfo( size : ptruint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  1178. begin
  1179. { the total size must stay multiple of 8, also allocate 2 pointers for
  1180. the fill and display procvars }
  1181. exact_info_size:=size + sizeof(theap_extra_info);
  1182. extra_info_size:=(exact_info_size+7) and not 7;
  1183. fill_extra_info_proc:=fillproc;
  1184. display_extra_info_proc:=displayproc;
  1185. end;
  1186. {*****************************************************************************
  1187. Install MemoryManager
  1188. *****************************************************************************}
  1189. const
  1190. TraceManager:TMemoryManager=(
  1191. NeedLock : true;
  1192. Getmem : @TraceGetMem;
  1193. Freemem : @TraceFreeMem;
  1194. FreememSize : @TraceFreeMemSize;
  1195. AllocMem : @TraceAllocMem;
  1196. ReAllocMem : @TraceReAllocMem;
  1197. MemSize : @TraceMemSize;
  1198. InitThread: @TraceInitThread;
  1199. DoneThread: @TraceExitThread;
  1200. RelocateHeap: @TraceRelocateHeap;
  1201. GetHeapStatus : @TraceGetHeapStatus;
  1202. GetFPCHeapStatus : @TraceGetFPCHeapStatus;
  1203. );
  1204. procedure TraceInit;
  1205. begin
  1206. MakeCRC32Tbl;
  1207. main_orig_todolist := @heap_info.heap_free_todo;
  1208. main_relo_todolist := nil;
  1209. TraceInitThread;
  1210. SetMemoryManager(TraceManager);
  1211. useownfile:=false;
  1212. if outputstr <> '' then
  1213. SetHeapTraceOutput(outputstr);
  1214. {$ifdef EXTRA}
  1215. Assign(error_file,'heap.err');
  1216. Rewrite(error_file);
  1217. {$endif EXTRA}
  1218. end;
  1219. procedure TraceExit;
  1220. begin
  1221. { no dump if error
  1222. because this gives long long listings }
  1223. { clear inoutres, in case the program that quit didn't }
  1224. ioresult;
  1225. if (exitcode<>0) and (erroraddr<>nil) then
  1226. begin
  1227. if useownfile then
  1228. begin
  1229. Writeln(ownfile,'No heap dump by heaptrc unit');
  1230. Writeln(ownfile,'Exitcode = ',exitcode);
  1231. end
  1232. else
  1233. begin
  1234. Writeln(stderr,'No heap dump by heaptrc unit');
  1235. Writeln(stderr,'Exitcode = ',exitcode);
  1236. end;
  1237. if useownfile then
  1238. begin
  1239. useownfile:=false;
  1240. close(ownfile);
  1241. end;
  1242. exit;
  1243. end;
  1244. move_heap_info(@orphaned_info, @heap_info);
  1245. dumpheap;
  1246. if heap_info.error_in_heap and (exitcode=0) then
  1247. exitcode:=203;
  1248. if main_relo_todolist <> nil then
  1249. donecriticalsection(todo_lock);
  1250. {$ifdef EXTRA}
  1251. Close(error_file);
  1252. {$endif EXTRA}
  1253. if useownfile then
  1254. begin
  1255. useownfile:=false;
  1256. close(ownfile);
  1257. end;
  1258. end;
  1259. {$if defined(win32) or defined(win64)}
  1260. function GetEnvironmentStrings : pchar; stdcall;
  1261. external 'kernel32' name 'GetEnvironmentStringsA';
  1262. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  1263. external 'kernel32' name 'FreeEnvironmentStringsA';
  1264. Function GetEnv(envvar: string): string;
  1265. var
  1266. s : string;
  1267. i : ptruint;
  1268. hp,p : pchar;
  1269. begin
  1270. getenv:='';
  1271. p:=GetEnvironmentStrings;
  1272. hp:=p;
  1273. while hp^<>#0 do
  1274. begin
  1275. s:=strpas(hp);
  1276. i:=pos('=',s);
  1277. if upcase(copy(s,1,i-1))=upcase(envvar) then
  1278. begin
  1279. getenv:=copy(s,i+1,length(s)-i);
  1280. break;
  1281. end;
  1282. { next string entry}
  1283. hp:=hp+strlen(hp)+1;
  1284. end;
  1285. FreeEnvironmentStrings(p);
  1286. end;
  1287. {$else defined(win32) or defined(win64)}
  1288. {$ifdef wince}
  1289. Function GetEnv(P:string):Pchar;
  1290. begin
  1291. { WinCE does not have environment strings.
  1292. Add some way to specify heaptrc options? }
  1293. GetEnv:=nil;
  1294. end;
  1295. {$else wince}
  1296. Function GetEnv(P:string):Pchar;
  1297. {
  1298. Searches the environment for a string with name p and
  1299. returns a pchar to it's value.
  1300. A pchar is used to accomodate for strings of length > 255
  1301. }
  1302. var
  1303. ep : ppchar;
  1304. i : ptruint;
  1305. found : boolean;
  1306. Begin
  1307. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1308. ep:=envp;
  1309. found:=false;
  1310. if ep<>nil then
  1311. begin
  1312. while (not found) and (ep^<>nil) do
  1313. begin
  1314. found:=true;
  1315. for i:=1 to length(p) do
  1316. if p[i]<>ep^[i-1] then
  1317. begin
  1318. found:=false;
  1319. break;
  1320. end;
  1321. if not found then
  1322. inc(ep);
  1323. end;
  1324. end;
  1325. if found then
  1326. getenv:=ep^+length(p)
  1327. else
  1328. getenv:=nil;
  1329. end;
  1330. {$endif wince}
  1331. {$endif win32}
  1332. procedure LoadEnvironment;
  1333. var
  1334. i,j : ptruint;
  1335. s : string;
  1336. begin
  1337. s:=Getenv('HEAPTRC');
  1338. if pos('keepreleased',s)>0 then
  1339. keepreleased:=true;
  1340. if pos('disabled',s)>0 then
  1341. useheaptrace:=false;
  1342. if pos('nohalt',s)>0 then
  1343. haltonerror:=false;
  1344. if pos('haltonnotreleased',s)>0 then
  1345. HaltOnNotReleased :=true;
  1346. i:=pos('log=',s);
  1347. if i>0 then
  1348. begin
  1349. outputstr:=copy(s,i+4,255);
  1350. j:=pos(' ',outputstr);
  1351. if j=0 then
  1352. j:=length(outputstr)+1;
  1353. delete(outputstr,j,255);
  1354. end;
  1355. end;
  1356. Initialization
  1357. LoadEnvironment;
  1358. { heaptrc can be disabled from the environment }
  1359. if useheaptrace then
  1360. TraceInit;
  1361. finalization
  1362. if useheaptrace then
  1363. TraceExit;
  1364. end.