heaptrc.pp 42 KB

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