heaptrc.pp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  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. {$TYPEDADDRESS on}
  20. {$if defined(win32) or defined(wince)}
  21. {$define windows}
  22. {$endif}
  23. Procedure DumpHeap;
  24. { define EXTRA to add more
  25. tests :
  26. - keep all memory after release and
  27. check by CRC value if not changed after release
  28. WARNING this needs extremely much memory (PM) }
  29. type
  30. tFillExtraInfoProc = procedure(p : pointer);
  31. tdisplayextrainfoProc = procedure (var ptext : text;p : pointer);
  32. { Allows to add info pre memory block, see ppheap.pas of the compiler
  33. for example source }
  34. procedure SetHeapExtraInfo(size : ptruint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  35. { Redirection of the output to a file }
  36. procedure SetHeapTraceOutput(const name : string);
  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. unaligned(pl^):=$DEADBEEF;
  416. end;
  417. { clear the memory }
  418. fillchar(p^,size,#255);
  419. { retrieve backtrace info }
  420. bp:=get_caller_frame(get_frame);
  421. { valid bp? }
  422. if (bp>=StackBottom) and (bp<(StackBottom + StackLength)) then
  423. for i:=1 to tracesize do
  424. begin
  425. pp^.calls[i]:=get_caller_addr(bp);
  426. oldbp:=bp;
  427. bp:=get_caller_frame(bp);
  428. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  429. break;
  430. end;
  431. { insert in the linked list }
  432. if loc_info^.heap_mem_root<>nil then
  433. loc_info^.heap_mem_root^.next:=pp;
  434. pp^.previous:=loc_info^.heap_mem_root;
  435. pp^.next:=nil;
  436. {$ifdef EXTRA}
  437. pp^.prev_valid:=loc_info^.heap_valid_last;
  438. loc_info^.heap_valid_last:=pp;
  439. if not assigned(loc_info^.heap_valid_first) then
  440. loc_info^.heap_valid_first:=pp;
  441. {$endif EXTRA}
  442. loc_info^.heap_mem_root:=pp;
  443. { must be changed before fill_extra_info is called
  444. because checkpointer can be called from within
  445. fill_extra_info PM }
  446. inc(loc_info^.getmem_cnt);
  447. { update the signature }
  448. if usecrc then
  449. pp^.sig:=calculate_sig(pp);
  450. TraceGetmem:=p;
  451. end;
  452. {*****************************************************************************
  453. TraceFreeMem
  454. *****************************************************************************}
  455. function CheckFreeMemSize(loc_info: pheap_info; pp: pheap_mem_info;
  456. size, ppsize: ptruint): boolean; inline;
  457. var
  458. i: ptruint;
  459. bp : pointer;
  460. ptext : ^text;
  461. {$ifdef EXTRA}
  462. pp2 : pheap_mem_info;
  463. {$endif}
  464. begin
  465. if useownfile then
  466. ptext:=@ownfile
  467. else
  468. ptext:=@stderr;
  469. inc(loc_info^.freemem_size,size);
  470. inc(loc_info^.freemem8_size,(size+7) and not 7);
  471. if not quicktrace then
  472. begin
  473. if not(is_in_getmem_list(loc_info, pp)) then
  474. RunError(204);
  475. end;
  476. if (pp^.sig=$AAAAAAAA) and not usecrc then
  477. begin
  478. loc_info^.error_in_heap:=true;
  479. dump_already_free(pp,ptext^);
  480. if haltonerror then halt(1);
  481. end
  482. else if ((pp^.sig<>$DEADBEEF) or usecrc) and
  483. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  484. begin
  485. loc_info^.error_in_heap:=true;
  486. dump_error(pp,ptext^);
  487. {$ifdef EXTRA}
  488. dump_error(pp,error_file);
  489. {$endif EXTRA}
  490. { don't release anything in this case !! }
  491. if haltonerror then halt(1);
  492. exit;
  493. end
  494. else if pp^.size<>size then
  495. begin
  496. loc_info^.error_in_heap:=true;
  497. dump_wrong_size(pp,size,ptext^);
  498. {$ifdef EXTRA}
  499. dump_wrong_size(pp,size,error_file);
  500. {$endif EXTRA}
  501. if haltonerror then halt(1);
  502. { don't release anything in this case !! }
  503. exit;
  504. end;
  505. { now it is released !! }
  506. pp^.sig:=$AAAAAAAA;
  507. if not keepreleased then
  508. begin
  509. if pp^.next<>nil then
  510. pp^.next^.previous:=pp^.previous;
  511. if pp^.previous<>nil then
  512. pp^.previous^.next:=pp^.next;
  513. if pp=loc_info^.heap_mem_root then
  514. loc_info^.heap_mem_root:=loc_info^.heap_mem_root^.previous;
  515. end
  516. else
  517. begin
  518. bp:=get_caller_frame(get_frame);
  519. if (bp>=StackBottom) and (bp<(StackBottom + StackLength)) then
  520. for i:=(tracesize div 2)+1 to tracesize do
  521. begin
  522. pp^.calls[i]:=get_caller_addr(bp);
  523. bp:=get_caller_frame(bp);
  524. if not((bp>=StackBottom) and (bp<(StackBottom + StackLength))) then
  525. break;
  526. end;
  527. end;
  528. inc(loc_info^.freemem_cnt);
  529. { clear the memory, $F0 will lead to GFP if used as pointer ! }
  530. fillchar((pointer(pp)+sizeof(theap_mem_info))^,size,#240);
  531. { this way we keep all info about all released memory !! }
  532. if keepreleased then
  533. begin
  534. {$ifdef EXTRA}
  535. { We want to check if the memory was changed after release !! }
  536. pp^.release_sig:=calculate_release_sig(pp);
  537. if pp=loc_info^.heap_valid_last then
  538. begin
  539. loc_info^.heap_valid_last:=pp^.prev_valid;
  540. if pp=loc_info^.heap_valid_first then
  541. loc_info^.heap_valid_first:=nil;
  542. exit(false);
  543. end;
  544. pp2:=loc_info^.heap_valid_last;
  545. while assigned(pp2) do
  546. begin
  547. if pp2^.prev_valid=pp then
  548. begin
  549. pp2^.prev_valid:=pp^.prev_valid;
  550. if pp=loc_info^.heap_valid_first then
  551. loc_info^.heap_valid_first:=pp2;
  552. exit(false);
  553. end
  554. else
  555. pp2:=pp2^.prev_valid;
  556. end;
  557. {$endif EXTRA}
  558. exit(false);
  559. end;
  560. CheckFreeMemSize:=true;
  561. end;
  562. function InternalFreeMemSize(loc_info: pheap_info; p: pointer; pp: pheap_mem_info;
  563. size: ptruint; release_todo_lock: boolean): ptruint;
  564. var
  565. i,ppsize : ptruint;
  566. extra_size: ptruint;
  567. release_mem: boolean;
  568. begin
  569. { save old values }
  570. extra_size:=pp^.extra_info_size;
  571. ppsize:= size+sizeof(theap_mem_info)+pp^.extra_info_size;
  572. if add_tail then
  573. inc(ppsize,sizeof(ptruint));
  574. { do various checking }
  575. release_mem := CheckFreeMemSize(loc_info, pp, size, ppsize);
  576. if release_todo_lock then
  577. leavecriticalsection(todo_lock);
  578. if release_mem then
  579. begin
  580. { release the normal memory at least }
  581. i:=SysFreeMemSize(pp,ppsize);
  582. { return the correct size }
  583. dec(i,sizeof(theap_mem_info)+extra_size);
  584. if add_tail then
  585. dec(i,sizeof(ptruint));
  586. InternalFreeMemSize:=i;
  587. end else
  588. InternalFreeMemSize:=size;
  589. end;
  590. function TraceFreeMemSize(p:pointer;size:ptruint):ptruint;
  591. var
  592. loc_info: pheap_info;
  593. pp: pheap_mem_info;
  594. release_lock: boolean;
  595. begin
  596. if p=nil then
  597. begin
  598. TraceFreeMemSize:=0;
  599. exit;
  600. end;
  601. loc_info:=@heap_info;
  602. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  603. release_lock:=false;
  604. if @loc_info^.heap_free_todo <> pp^.todolist then
  605. begin
  606. if pp^.todolist = main_orig_todolist then
  607. pp^.todolist := main_relo_todolist;
  608. entercriticalsection(todo_lock);
  609. release_lock:=true;
  610. if pp^.todolist = @orphaned_info.heap_free_todo then
  611. begin
  612. loc_info := @orphaned_info;
  613. end else
  614. if pp^.todolist <> @loc_info^.heap_free_todo then
  615. begin
  616. { allocated in different heap, push to that todolist }
  617. pp^.todonext := pp^.todolist^;
  618. pp^.todolist^ := pp;
  619. TraceFreeMemSize := pp^.size;
  620. leavecriticalsection(todo_lock);
  621. exit;
  622. end;
  623. end;
  624. TraceFreeMemSize:=InternalFreeMemSize(loc_info,p,pp,size,release_lock);
  625. end;
  626. function TraceMemSize(p:pointer):ptruint;
  627. var
  628. pp : pheap_mem_info;
  629. begin
  630. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  631. TraceMemSize:=pp^.size;
  632. end;
  633. function TraceFreeMem(p:pointer):ptruint;
  634. var
  635. l : ptruint;
  636. pp : pheap_mem_info;
  637. begin
  638. if p=nil then
  639. begin
  640. TraceFreeMem:=0;
  641. exit;
  642. end;
  643. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  644. l:=SysMemSize(pp);
  645. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  646. if add_tail then
  647. dec(l,sizeof(ptruint));
  648. { this can never happend normaly }
  649. if pp^.size>l then
  650. begin
  651. if useownfile then
  652. dump_wrong_size(pp,l,ownfile)
  653. else
  654. dump_wrong_size(pp,l,stderr);
  655. {$ifdef EXTRA}
  656. dump_wrong_size(pp,l,error_file);
  657. {$endif EXTRA}
  658. end;
  659. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  660. end;
  661. {*****************************************************************************
  662. ReAllocMem
  663. *****************************************************************************}
  664. function TraceReAllocMem(var p:pointer;size:ptruint):Pointer;
  665. var
  666. newP: pointer;
  667. allocsize,
  668. movesize,
  669. i : ptruint;
  670. oldbp,
  671. bp : pointer;
  672. pl : pdword;
  673. pp : pheap_mem_info;
  674. oldsize,
  675. oldextrasize,
  676. oldexactsize : ptruint;
  677. old_fill_extra_info_proc : tfillextrainfoproc;
  678. old_display_extra_info_proc : tdisplayextrainfoproc;
  679. loc_info: pheap_info;
  680. begin
  681. { Free block? }
  682. if size=0 then
  683. begin
  684. if p<>nil then
  685. TraceFreeMem(p);
  686. p:=nil;
  687. TraceReallocMem:=P;
  688. exit;
  689. end;
  690. { Allocate a new block? }
  691. if p=nil then
  692. begin
  693. p:=TraceGetMem(size);
  694. TraceReallocMem:=P;
  695. exit;
  696. end;
  697. { Resize block }
  698. loc_info:=@heap_info;
  699. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  700. { test block }
  701. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  702. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  703. begin
  704. loc_info^.error_in_heap:=true;
  705. if useownfile then
  706. dump_error(pp,ownfile)
  707. else
  708. dump_error(pp,stderr);
  709. {$ifdef EXTRA}
  710. dump_error(pp,error_file);
  711. {$endif EXTRA}
  712. { don't release anything in this case !! }
  713. if haltonerror then halt(1);
  714. exit;
  715. end;
  716. { save info }
  717. oldsize:=pp^.size;
  718. oldextrasize:=pp^.extra_info_size;
  719. oldexactsize:=pp^.exact_info_size;
  720. if pp^.extra_info_size>0 then
  721. begin
  722. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  723. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  724. end;
  725. { Do the real ReAllocMem, but alloc also for the info block }
  726. {$ifdef cpuarm}
  727. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+pp^.extra_info_size;
  728. {$else cpuarm}
  729. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  730. {$endif cpuarm}
  731. if add_tail then
  732. inc(allocsize,sizeof(ptruint));
  733. { Try to resize the block, if not possible we need to do a
  734. getmem, move data, freemem }
  735. if not SysTryResizeMem(pp,allocsize) then
  736. begin
  737. { get a new block }
  738. newP := TraceGetMem(size);
  739. { move the data }
  740. if newP <> nil then
  741. begin
  742. movesize:=TraceMemSize(p);
  743. {if the old size is larger than the new size,
  744. move only the new size}
  745. if movesize>size then
  746. movesize:=size;
  747. move(p^,newP^,movesize);
  748. end;
  749. { release p }
  750. traceFreeMem(p);
  751. { return the new pointer }
  752. p:=newp;
  753. traceReAllocMem := newp;
  754. exit;
  755. end;
  756. { Recreate the info block }
  757. pp^.sig:=$DEADBEEF;
  758. pp^.size:=size;
  759. pp^.extra_info_size:=oldextrasize;
  760. pp^.exact_info_size:=oldexactsize;
  761. { add the new extra_info and tail }
  762. if pp^.extra_info_size>0 then
  763. begin
  764. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  765. fillchar(pp^.extra_info^,extra_info_size,0);
  766. pp^.extra_info^.check:=$12345678;
  767. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  768. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  769. if assigned(pp^.extra_info^.fillproc) then
  770. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  771. end
  772. else
  773. pp^.extra_info:=nil;
  774. if add_tail then
  775. begin
  776. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptruint);
  777. unaligned(pl^):=$DEADBEEF;
  778. end;
  779. { adjust like a freemem and then a getmem, so you get correct
  780. results in the summary display }
  781. inc(loc_info^.freemem_size,oldsize);
  782. inc(loc_info^.freemem8_size,(oldsize+7) and not 7);
  783. inc(loc_info^.getmem_size,size);
  784. inc(loc_info^.getmem8_size,(size+7) and not 7);
  785. { generate new backtrace }
  786. bp:=get_caller_frame(get_frame);
  787. if (bp>=StackBottom) and (bp<(StackBottom + StackLength)) then
  788. for i:=1 to tracesize do
  789. begin
  790. pp^.calls[i]:=get_caller_addr(bp);
  791. oldbp:=bp;
  792. bp:=get_caller_frame(bp);
  793. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  794. break;
  795. end;
  796. { regenerate signature }
  797. if usecrc then
  798. pp^.sig:=calculate_sig(pp);
  799. { return the pointer }
  800. p:=pointer(pp)+sizeof(theap_mem_info);
  801. TraceReAllocmem:=p;
  802. end;
  803. {*****************************************************************************
  804. Check pointer
  805. *****************************************************************************}
  806. {$ifndef Unix}
  807. {$S-}
  808. {$endif}
  809. {$ifdef go32v2}
  810. var
  811. __stklen : longword;external name '__stklen';
  812. __stkbottom : longword;external name '__stkbottom';
  813. ebss : longword; external name 'end';
  814. {$endif go32v2}
  815. {$ifdef linux}
  816. var
  817. etext: ptruint; external name '_etext';
  818. edata : ptruint; external name '_edata';
  819. eend : ptruint; external name '_end';
  820. {$endif}
  821. {$ifdef os2}
  822. (* Currently still EMX based - possibly to be changed in the future. *)
  823. var
  824. etext: ptruint; external name '_etext';
  825. edata : ptruint; external name '_edata';
  826. eend : ptruint; external name '_end';
  827. {$endif}
  828. {$ifdef windows}
  829. var
  830. sdata : ptruint; external name '__data_start__';
  831. edata : ptruint; external name '__data_end__';
  832. sbss : ptruint; external name '__bss_start__';
  833. ebss : ptruint; external name '__bss_end__';
  834. {$endif}
  835. {$ifdef BEOS}
  836. const
  837. B_ERROR = -1;
  838. type
  839. area_id = Longint;
  840. function area_for(addr : Pointer) : area_id;
  841. cdecl; external 'root' name 'area_for';
  842. {$endif BEOS}
  843. procedure CheckPointer(p : pointer); [public, alias : 'FPC_CHECKPOINTER'];
  844. var
  845. i : ptruint;
  846. pp : pheap_mem_info;
  847. loc_info: pheap_info;
  848. {$ifdef go32v2}
  849. get_ebp,stack_top : longword;
  850. bss_end : longword;
  851. {$endif go32v2}
  852. {$ifdef morphos}
  853. stack_top: longword;
  854. {$endif morphos}
  855. ptext : ^text;
  856. label
  857. _exit;
  858. begin
  859. if p=nil then
  860. runerror(204);
  861. i:=0;
  862. loc_info:=@heap_info;
  863. if useownfile then
  864. ptext:=@ownfile
  865. else
  866. ptext:=@stderr;
  867. {$ifdef go32v2}
  868. if ptruint(p)<$1000 then
  869. runerror(216);
  870. asm
  871. movl %ebp,get_ebp
  872. leal ebss,%eax
  873. movl %eax,bss_end
  874. end;
  875. stack_top:=__stkbottom+__stklen;
  876. { allow all between start of code and end of bss }
  877. if ptruint(p)<=bss_end then
  878. goto _exit;
  879. { stack can be above heap !! }
  880. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  881. goto _exit;
  882. {$endif go32v2}
  883. { I don't know where the stack is in other OS !! }
  884. {$ifdef windows}
  885. { inside stack ? }
  886. if (ptruint(p)>ptruint(get_frame)) and
  887. (p<StackTop) then
  888. goto _exit;
  889. { inside data ? }
  890. if (ptruint(p)>=ptruint(@sdata)) and (ptruint(p)<ptruint(@edata)) then
  891. goto _exit;
  892. { inside bss ? }
  893. if (ptruint(p)>=ptruint(@sbss)) and (ptruint(p)<ptruint(@ebss)) then
  894. goto _exit;
  895. {$endif windows}
  896. {$IFDEF OS2}
  897. { inside stack ? }
  898. if (PtrUInt (P) > PtrUInt (Get_Frame)) and
  899. (PtrUInt (P) < PtrUInt (StackTop)) then
  900. goto _exit;
  901. { inside data or bss ? }
  902. if (PtrUInt (P) >= PtrUInt (@etext)) and (PtrUInt (P) < PtrUInt (@eend)) then
  903. goto _exit;
  904. {$ENDIF OS2}
  905. {$ifdef linux}
  906. { inside stack ? }
  907. if (ptruint(p)>ptruint(get_frame)) and
  908. (ptruint(p)<$c0000000) then //todo: 64bit!
  909. goto _exit;
  910. { inside data or bss ? }
  911. if (ptruint(p)>=ptruint(@etext)) and (ptruint(p)<ptruint(@eend)) then
  912. goto _exit;
  913. {$endif linux}
  914. {$ifdef morphos}
  915. { inside stack ? }
  916. stack_top:=ptruint(StackBottom)+StackLength;
  917. if (ptruint(p)<stack_top) and (ptruint(p)>ptruint(StackBottom)) then
  918. goto _exit;
  919. { inside data or bss ? }
  920. {$WARNING data and bss checking missing }
  921. {$endif morphos}
  922. {$ifdef darwin}
  923. {$warning No checkpointer support yet for Darwin}
  924. exit;
  925. {$endif}
  926. {$ifdef BEOS}
  927. // if we find the address in a known area in our current process,
  928. // then it is a valid one
  929. if area_for(p) <> B_ERROR then
  930. goto _exit;
  931. {$endif BEOS}
  932. { first try valid list faster }
  933. {$ifdef EXTRA}
  934. pp:=loc_info^.heap_valid_last;
  935. while pp<>nil do
  936. begin
  937. { inside this valid block ! }
  938. { we can be changing the extrainfo !! }
  939. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  940. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  941. begin
  942. { check allocated block }
  943. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  944. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  945. { special case of the fill_extra_info call }
  946. ((pp=loc_info^.heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  947. and loc_info^.inside_trace_getmem) then
  948. goto _exit
  949. else
  950. begin
  951. writeln(ptext^,'corrupted heap_mem_info');
  952. dump_error(pp,ptext^);
  953. halt(1);
  954. end;
  955. end
  956. else
  957. pp:=pp^.prev_valid;
  958. inc(i);
  959. if i>loc_info^.getmem_cnt-loc_info^.freemem_cnt then
  960. begin
  961. writeln(ptext^,'error in linked list of heap_mem_info');
  962. halt(1);
  963. end;
  964. end;
  965. i:=0;
  966. {$endif EXTRA}
  967. pp:=loc_info^.heap_mem_root;
  968. while pp<>nil do
  969. begin
  970. { inside this block ! }
  971. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  972. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  973. { allocated block }
  974. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  975. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  976. goto _exit
  977. else
  978. begin
  979. writeln(ptext^,'pointer $',hexstr(p),' points into invalid memory block');
  980. dump_error(pp,ptext^);
  981. runerror(204);
  982. end;
  983. pp:=pp^.previous;
  984. inc(i);
  985. if i>loc_info^.getmem_cnt then
  986. begin
  987. writeln(ptext^,'error in linked list of heap_mem_info');
  988. halt(1);
  989. end;
  990. end;
  991. writeln(ptext^,'pointer $',hexstr(p),' does not point to valid memory block');
  992. dump_error(p,ptext^);
  993. runerror(204);
  994. _exit:
  995. end;
  996. {*****************************************************************************
  997. Dump Heap
  998. *****************************************************************************}
  999. procedure dumpheap;
  1000. var
  1001. pp : pheap_mem_info;
  1002. i : ptrint;
  1003. ExpectedHeapFree : ptruint;
  1004. status : TFPCHeapStatus;
  1005. ptext : ^text;
  1006. loc_info: pheap_info;
  1007. begin
  1008. loc_info:=@heap_info;
  1009. if useownfile then
  1010. ptext:=@ownfile
  1011. else
  1012. ptext:=@stderr;
  1013. pp:=loc_info^.heap_mem_root;
  1014. Writeln(ptext^,'Heap dump by heaptrc unit');
  1015. Writeln(ptext^,loc_info^.getmem_cnt, ' memory blocks allocated : ',
  1016. loc_info^.getmem_size,'/',loc_info^.getmem8_size);
  1017. Writeln(ptext^,loc_info^.freemem_cnt,' memory blocks freed : ',
  1018. loc_info^.freemem_size,'/',loc_info^.freemem8_size);
  1019. Writeln(ptext^,loc_info^.getmem_cnt-loc_info^.freemem_cnt,
  1020. ' unfreed memory blocks : ',loc_info^.getmem_size-loc_info^.freemem_size);
  1021. status:=SysGetFPCHeapStatus;
  1022. Write(ptext^,'True heap size : ',status.CurrHeapSize);
  1023. if EntryMemUsed > 0 then
  1024. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  1025. else
  1026. Writeln(ptext^);
  1027. Writeln(ptext^,'True free heap : ',status.CurrHeapFree);
  1028. ExpectedHeapFree:=status.CurrHeapSize
  1029. -(loc_info^.getmem8_size-loc_info^.freemem8_size)
  1030. -(loc_info^.getmem_cnt-loc_info^.freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)
  1031. -EntryMemUsed;
  1032. If ExpectedHeapFree<>status.CurrHeapFree then
  1033. Writeln(ptext^,'Should be : ',ExpectedHeapFree);
  1034. i:=loc_info^.getmem_cnt-loc_info^.freemem_cnt;
  1035. while pp<>nil do
  1036. begin
  1037. if i<0 then
  1038. begin
  1039. Writeln(ptext^,'Error in heap memory list');
  1040. Writeln(ptext^,'More memory blocks than expected');
  1041. exit;
  1042. end;
  1043. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  1044. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  1045. begin
  1046. { this one was not released !! }
  1047. if exitcode<>203 then
  1048. call_stack(pp,ptext^);
  1049. dec(i);
  1050. end
  1051. else if pp^.sig<>$AAAAAAAA then
  1052. begin
  1053. dump_error(pp,ptext^);
  1054. {$ifdef EXTRA}
  1055. dump_error(pp,error_file);
  1056. {$endif EXTRA}
  1057. loc_info^.error_in_heap:=true;
  1058. end
  1059. {$ifdef EXTRA}
  1060. else if pp^.release_sig<>calculate_release_sig(pp) then
  1061. begin
  1062. dump_change_after(pp,ptext^);
  1063. dump_change_after(pp,error_file);
  1064. loc_info^.error_in_heap:=true;
  1065. end
  1066. {$endif EXTRA}
  1067. ;
  1068. pp:=pp^.previous;
  1069. end;
  1070. if HaltOnNotReleased and (loc_info^.getmem_cnt<>loc_info^.freemem_cnt) then
  1071. exitcode:=203;
  1072. end;
  1073. {*****************************************************************************
  1074. AllocMem
  1075. *****************************************************************************}
  1076. function TraceAllocMem(size:ptruint):Pointer;
  1077. begin
  1078. TraceAllocMem:=SysAllocMem(size);
  1079. end;
  1080. {*****************************************************************************
  1081. No specific tracing calls
  1082. *****************************************************************************}
  1083. procedure TraceInitThread;
  1084. var
  1085. loc_info: pheap_info;
  1086. begin
  1087. loc_info := @heap_info;
  1088. {$ifdef EXTRA}
  1089. loc_info^.heap_valid_first := nil;
  1090. loc_info^.heap_valid_last := nil;
  1091. {$endif}
  1092. loc_info^.heap_mem_root := nil;
  1093. loc_info^.getmem_cnt := 0;
  1094. loc_info^.freemem_cnt := 0;
  1095. loc_info^.getmem_size := 0;
  1096. loc_info^.freemem_size := 0;
  1097. loc_info^.getmem8_size := 0;
  1098. loc_info^.freemem8_size := 0;
  1099. loc_info^.error_in_heap := false;
  1100. loc_info^.inside_trace_getmem := false;
  1101. EntryMemUsed := SysGetFPCHeapStatus.CurrHeapUsed;
  1102. end;
  1103. procedure TraceRelocateHeap;
  1104. begin
  1105. main_relo_todolist := @heap_info.heap_free_todo;
  1106. initcriticalsection(todo_lock);
  1107. end;
  1108. procedure move_heap_info(src_info, dst_info: pheap_info);
  1109. var
  1110. heap_mem: pheap_mem_info;
  1111. begin
  1112. if src_info^.heap_free_todo <> nil then
  1113. finish_heap_free_todo_list(src_info);
  1114. if dst_info^.heap_free_todo <> nil then
  1115. finish_heap_free_todo_list(dst_info);
  1116. heap_mem := src_info^.heap_mem_root;
  1117. if heap_mem <> nil then
  1118. begin
  1119. repeat
  1120. heap_mem^.todolist := @dst_info^.heap_free_todo;
  1121. if heap_mem^.previous = nil then break;
  1122. heap_mem := heap_mem^.previous;
  1123. until false;
  1124. heap_mem^.previous := dst_info^.heap_mem_root;
  1125. if dst_info^.heap_mem_root <> nil then
  1126. dst_info^.heap_mem_root^.next := heap_mem;
  1127. dst_info^.heap_mem_root := src_info^.heap_mem_root;
  1128. end;
  1129. inc(dst_info^.getmem_cnt, src_info^.getmem_cnt);
  1130. inc(dst_info^.getmem_size, src_info^.getmem_size);
  1131. inc(dst_info^.getmem8_size, src_info^.getmem8_size);
  1132. inc(dst_info^.freemem_cnt, src_info^.freemem_cnt);
  1133. inc(dst_info^.freemem_size, src_info^.freemem_size);
  1134. inc(dst_info^.freemem8_size, src_info^.freemem8_size);
  1135. dst_info^.error_in_heap := dst_info^.error_in_heap or src_info^.error_in_heap;
  1136. {$ifdef EXTRA}
  1137. if assigned(dst_info^.heap_valid_first) then
  1138. dst_info^.heap_valid_first^.prev_valid := src_info^.heap_valid_last
  1139. else
  1140. dst_info^.heap_valid_last := src_info^.heap_valid_last;
  1141. dst_info^.heap_valid_first := src_info^.heap_valid_first;
  1142. {$endif}
  1143. end;
  1144. procedure TraceExitThread;
  1145. var
  1146. loc_info: pheap_info;
  1147. begin
  1148. loc_info := @heap_info;
  1149. entercriticalsection(todo_lock);
  1150. move_heap_info(loc_info, @orphaned_info);
  1151. leavecriticalsection(todo_lock);
  1152. end;
  1153. function TraceGetHeapStatus:THeapStatus;
  1154. begin
  1155. TraceGetHeapStatus:=SysGetHeapStatus;
  1156. end;
  1157. function TraceGetFPCHeapStatus:TFPCHeapStatus;
  1158. begin
  1159. TraceGetFPCHeapStatus:=SysGetFPCHeapStatus;
  1160. end;
  1161. {*****************************************************************************
  1162. Program Hooks
  1163. *****************************************************************************}
  1164. Procedure SetHeapTraceOutput(const name : string);
  1165. var i : ptruint;
  1166. begin
  1167. if useownfile then
  1168. begin
  1169. useownfile:=false;
  1170. close(ownfile);
  1171. end;
  1172. assign(ownfile,name);
  1173. {$I-}
  1174. append(ownfile);
  1175. if IOResult<>0 then
  1176. begin
  1177. Rewrite(ownfile);
  1178. if IOResult<>0 then
  1179. begin
  1180. Writeln(stderr,'[heaptrc] Unable to open "',name,'", writing output to stderr instead.');
  1181. useownfile:=false;
  1182. exit;
  1183. end;
  1184. end;
  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. {$i-}
  1230. Assign(error_file,'heap.err');
  1231. Rewrite(error_file);
  1232. {$i+}
  1233. if IOResult<>0 then
  1234. begin
  1235. writeln('[heaptrc] Unable to create heap.err extra log file, writing output to screen.');
  1236. Assign(error_file,'');
  1237. Rewrite(error_file);
  1238. end;
  1239. {$endif EXTRA}
  1240. end;
  1241. procedure TraceExit;
  1242. begin
  1243. { no dump if error
  1244. because this gives long long listings }
  1245. { clear inoutres, in case the program that quit didn't }
  1246. ioresult;
  1247. if (exitcode<>0) and (erroraddr<>nil) then
  1248. begin
  1249. if useownfile then
  1250. begin
  1251. Writeln(ownfile,'No heap dump by heaptrc unit');
  1252. Writeln(ownfile,'Exitcode = ',exitcode);
  1253. end
  1254. else
  1255. begin
  1256. Writeln(stderr,'No heap dump by heaptrc unit');
  1257. Writeln(stderr,'Exitcode = ',exitcode);
  1258. end;
  1259. if useownfile then
  1260. begin
  1261. useownfile:=false;
  1262. close(ownfile);
  1263. end;
  1264. exit;
  1265. end;
  1266. move_heap_info(@orphaned_info, @heap_info);
  1267. dumpheap;
  1268. if heap_info.error_in_heap and (exitcode=0) then
  1269. exitcode:=203;
  1270. if main_relo_todolist <> nil then
  1271. donecriticalsection(todo_lock);
  1272. {$ifdef EXTRA}
  1273. Close(error_file);
  1274. {$endif EXTRA}
  1275. if useownfile then
  1276. begin
  1277. useownfile:=false;
  1278. close(ownfile);
  1279. end;
  1280. end;
  1281. {$if defined(win32) or defined(win64)}
  1282. function GetEnvironmentStrings : pchar; stdcall;
  1283. external 'kernel32' name 'GetEnvironmentStringsA';
  1284. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  1285. external 'kernel32' name 'FreeEnvironmentStringsA';
  1286. Function GetEnv(envvar: string): string;
  1287. var
  1288. s : string;
  1289. i : ptruint;
  1290. hp,p : pchar;
  1291. begin
  1292. getenv:='';
  1293. p:=GetEnvironmentStrings;
  1294. hp:=p;
  1295. while hp^<>#0 do
  1296. begin
  1297. s:=strpas(hp);
  1298. i:=pos('=',s);
  1299. if upcase(copy(s,1,i-1))=upcase(envvar) then
  1300. begin
  1301. getenv:=copy(s,i+1,length(s)-i);
  1302. break;
  1303. end;
  1304. { next string entry}
  1305. hp:=hp+strlen(hp)+1;
  1306. end;
  1307. FreeEnvironmentStrings(p);
  1308. end;
  1309. {$else defined(win32) or defined(win64)}
  1310. {$ifdef wince}
  1311. Function GetEnv(P:string):Pchar;
  1312. begin
  1313. { WinCE does not have environment strings.
  1314. Add some way to specify heaptrc options? }
  1315. GetEnv:=nil;
  1316. end;
  1317. {$else wince}
  1318. Function GetEnv(P:string):Pchar;
  1319. {
  1320. Searches the environment for a string with name p and
  1321. returns a pchar to it's value.
  1322. A pchar is used to accomodate for strings of length > 255
  1323. }
  1324. var
  1325. ep : ppchar;
  1326. i : ptruint;
  1327. found : boolean;
  1328. Begin
  1329. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1330. ep:=envp;
  1331. found:=false;
  1332. if ep<>nil then
  1333. begin
  1334. while (not found) and (ep^<>nil) do
  1335. begin
  1336. found:=true;
  1337. for i:=1 to length(p) do
  1338. if p[i]<>ep^[i-1] then
  1339. begin
  1340. found:=false;
  1341. break;
  1342. end;
  1343. if not found then
  1344. inc(ep);
  1345. end;
  1346. end;
  1347. if found then
  1348. getenv:=ep^+length(p)
  1349. else
  1350. getenv:=nil;
  1351. end;
  1352. {$endif wince}
  1353. {$endif win32}
  1354. procedure LoadEnvironment;
  1355. var
  1356. i,j : ptruint;
  1357. s : string;
  1358. begin
  1359. s:=Getenv('HEAPTRC');
  1360. if pos('keepreleased',s)>0 then
  1361. keepreleased:=true;
  1362. if pos('disabled',s)>0 then
  1363. useheaptrace:=false;
  1364. if pos('nohalt',s)>0 then
  1365. haltonerror:=false;
  1366. if pos('haltonnotreleased',s)>0 then
  1367. HaltOnNotReleased :=true;
  1368. i:=pos('log=',s);
  1369. if i>0 then
  1370. begin
  1371. outputstr:=copy(s,i+4,255);
  1372. j:=pos(' ',outputstr);
  1373. if j=0 then
  1374. j:=length(outputstr)+1;
  1375. delete(outputstr,j,255);
  1376. end;
  1377. end;
  1378. Initialization
  1379. LoadEnvironment;
  1380. { heaptrc can be disabled from the environment }
  1381. if useheaptrace then
  1382. TraceInit;
  1383. finalization
  1384. if useheaptrace then
  1385. TraceExit;
  1386. end.