heaptrc.pp 43 KB

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