heaptrc.pp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. {
  2. $Id: heaptrc.pp,v 1.44 2005/04/04 15:16:26 peter Exp $
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team.
  5. Heap tracer
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit heaptrc;
  13. interface
  14. {$goto on}
  15. Procedure DumpHeap;
  16. Procedure MarkHeap;
  17. { define EXTRA to add more
  18. tests :
  19. - keep all memory after release and
  20. check by CRC value if not changed after release
  21. WARNING this needs extremely much memory (PM) }
  22. type
  23. tFillExtraInfoProc = procedure(p : pointer);
  24. tdisplayextrainfoProc = procedure (var ptext : text;p : pointer);
  25. { Allows to add info pre memory block, see ppheap.pas of the compiler
  26. for example source }
  27. procedure SetHeapExtraInfo( size : ptrint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  28. { Redirection of the output to a file }
  29. procedure SetHeapTraceOutput(const name : string);
  30. const
  31. { tracing level
  32. splitted in two if memory is released !! }
  33. {$ifdef EXTRA}
  34. tracesize = 16;
  35. {$else EXTRA}
  36. tracesize = 8;
  37. {$endif EXTRA}
  38. { install heaptrc memorymanager }
  39. useheaptrace : boolean=true;
  40. { less checking }
  41. quicktrace : boolean=true;
  42. { calls halt() on error by default !! }
  43. HaltOnError : boolean = true;
  44. { set this to true if you suspect that memory
  45. is freed several times }
  46. {$ifdef EXTRA}
  47. keepreleased : boolean=true;
  48. {$else EXTRA}
  49. keepreleased : boolean=false;
  50. {$endif EXTRA}
  51. { add a small footprint at the end of memory blocks, this
  52. can check for memory overwrites at the end of a block }
  53. add_tail : boolean = true;
  54. { put crc in sig
  55. this allows to test for writing into that part }
  56. usecrc : boolean = true;
  57. implementation
  58. type
  59. pptrint = ^ptrint;
  60. const
  61. { allows to add custom info in heap_mem_info, this is the size that will
  62. be allocated for this information }
  63. extra_info_size : ptrint = 0;
  64. exact_info_size : ptrint = 0;
  65. EntryMemUsed : ptrint = 0;
  66. { function to fill this info up }
  67. fill_extra_info_proc : TFillExtraInfoProc = nil;
  68. display_extra_info_proc : TDisplayExtraInfoProc = nil;
  69. error_in_heap : boolean = false;
  70. inside_trace_getmem : boolean = false;
  71. { indicates where the output will be redirected }
  72. { only set using environment variables }
  73. outputstr : shortstring = '';
  74. type
  75. pheap_extra_info = ^theap_extra_info;
  76. theap_extra_info = record
  77. check : cardinal; { used to check if the procvar is still valid }
  78. fillproc : tfillextrainfoProc;
  79. displayproc : tdisplayextrainfoProc;
  80. data : record
  81. end;
  82. end;
  83. { warning the size of theap_mem_info
  84. must be a multiple of 8
  85. because otherwise you will get
  86. problems when releasing the usual memory part !!
  87. sizeof(theap_mem_info = 16+tracesize*4 so
  88. tracesize must be even !! PM }
  89. pheap_mem_info = ^theap_mem_info;
  90. theap_mem_info = record
  91. previous,
  92. next : pheap_mem_info;
  93. size : ptrint;
  94. sig : longword;
  95. {$ifdef EXTRA}
  96. release_sig : longword;
  97. prev_valid : pheap_mem_info;
  98. {$endif EXTRA}
  99. calls : array [1..tracesize] of pointer;
  100. exact_info_size : word;
  101. extra_info_size : word;
  102. extra_info : pheap_extra_info;
  103. end;
  104. var
  105. ptext : ^text;
  106. ownfile : text;
  107. {$ifdef EXTRA}
  108. error_file : text;
  109. heap_valid_first,
  110. heap_valid_last : pheap_mem_info;
  111. {$endif EXTRA}
  112. heap_mem_root : pheap_mem_info;
  113. getmem_cnt,
  114. freemem_cnt : ptrint;
  115. getmem_size,
  116. freemem_size : ptrint;
  117. getmem8_size,
  118. freemem8_size : ptrint;
  119. {*****************************************************************************
  120. Crc 32
  121. *****************************************************************************}
  122. var
  123. Crc32Tbl : array[0..255] of longword;
  124. procedure MakeCRC32Tbl;
  125. var
  126. crc : longword;
  127. i,n : byte;
  128. begin
  129. for i:=0 to 255 do
  130. begin
  131. crc:=i;
  132. for n:=1 to 8 do
  133. if odd(crc) then
  134. crc:=(crc shr 1) xor $edb88320
  135. else
  136. crc:=crc shr 1;
  137. Crc32Tbl[i]:=crc;
  138. end;
  139. end;
  140. Function UpdateCrc32(InitCrc:longword;var InBuf;InLen:ptrint):longword;
  141. var
  142. i : ptrint;
  143. p : pchar;
  144. begin
  145. p:=@InBuf;
  146. for i:=1 to InLen do
  147. begin
  148. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  149. inc(p);
  150. end;
  151. UpdateCrc32:=InitCrc;
  152. end;
  153. Function calculate_sig(p : pheap_mem_info) : longword;
  154. var
  155. crc : longword;
  156. pl : pptrint;
  157. begin
  158. crc:=cardinal($ffffffff);
  159. crc:=UpdateCrc32(crc,p^.size,sizeof(ptrint));
  160. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(ptrint));
  161. if p^.extra_info_size>0 then
  162. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  163. if add_tail then
  164. begin
  165. { Check also 4 bytes just after allocation !! }
  166. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size;
  167. crc:=UpdateCrc32(crc,pl^,sizeof(ptrint));
  168. end;
  169. calculate_sig:=crc;
  170. end;
  171. {$ifdef EXTRA}
  172. Function calculate_release_sig(p : pheap_mem_info) : longword;
  173. var
  174. crc : longword;
  175. pl : pptrint;
  176. begin
  177. crc:=$ffffffff;
  178. crc:=UpdateCrc32(crc,p^.size,sizeof(ptrint));
  179. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(ptrint));
  180. if p^.extra_info_size>0 then
  181. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  182. { Check the whole of the whole allocation }
  183. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info);
  184. crc:=UpdateCrc32(crc,pl^,p^.size);
  185. { Check also 4 bytes just after allocation !! }
  186. if add_tail then
  187. begin
  188. { Check also 4 bytes just after allocation !! }
  189. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size;
  190. crc:=UpdateCrc32(crc,pl^,sizeof(ptrint));
  191. end;
  192. calculate_release_sig:=crc;
  193. end;
  194. {$endif EXTRA}
  195. {*****************************************************************************
  196. Helpers
  197. *****************************************************************************}
  198. procedure call_stack(pp : pheap_mem_info;var ptext : text);
  199. var
  200. i : ptrint;
  201. begin
  202. writeln(ptext,'Call trace for block $',hexstr(ptrint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
  203. for i:=1 to tracesize do
  204. if pp^.calls[i]<>nil then
  205. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  206. { the check is done to be sure that the procvar is not overwritten }
  207. if assigned(pp^.extra_info) and
  208. (pp^.extra_info^.check=$12345678) and
  209. assigned(pp^.extra_info^.displayproc) then
  210. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  211. end;
  212. procedure call_free_stack(pp : pheap_mem_info;var ptext : text);
  213. var
  214. i : ptrint;
  215. begin
  216. writeln(ptext,'Call trace for block at $',hexstr(ptrint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
  217. for i:=1 to tracesize div 2 do
  218. if pp^.calls[i]<>nil then
  219. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  220. writeln(ptext,' was released at ');
  221. for i:=(tracesize div 2)+1 to tracesize do
  222. if pp^.calls[i]<>nil then
  223. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  224. { the check is done to be sure that the procvar is not overwritten }
  225. if assigned(pp^.extra_info) and
  226. (pp^.extra_info^.check=$12345678) and
  227. assigned(pp^.extra_info^.displayproc) then
  228. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  229. end;
  230. procedure dump_already_free(p : pheap_mem_info;var ptext : text);
  231. begin
  232. Writeln(ptext,'Marked memory at $',HexStr(ptrint(pointer(p)+sizeof(theap_mem_info)),8),' released');
  233. call_free_stack(p,ptext);
  234. Writeln(ptext,'freed again at');
  235. dump_stack(ptext,get_caller_frame(get_frame));
  236. end;
  237. procedure dump_error(p : pheap_mem_info;var ptext : text);
  238. begin
  239. Writeln(ptext,'Marked memory at $',HexStr(ptrint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  240. Writeln(ptext,'Wrong signature $',hexstr(p^.sig,8),' instead of ',hexstr(calculate_sig(p),8));
  241. dump_stack(ptext,get_caller_frame(get_frame));
  242. end;
  243. {$ifdef EXTRA}
  244. procedure dump_change_after(p : pheap_mem_info;var ptext : text);
  245. var pp : pchar;
  246. i : ptrint;
  247. begin
  248. Writeln(ptext,'Marked memory at $',HexStr(ptrint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  249. Writeln(ptext,'Wrong release CRC $',hexstr(p^.release_sig,8),' instead of ',hexstr(calculate_release_sig(p),8));
  250. Writeln(ptext,'This memory was changed after call to freemem !');
  251. call_free_stack(p,ptext);
  252. pp:=pointer(p)+sizeof(theap_mem_info);
  253. for i:=0 to p^.size-1 do
  254. if byte(pp[i])<>$F0 then
  255. Writeln(ptext,'offset',i,':$',hexstr(i,8),'"',pp[i],'"');
  256. end;
  257. {$endif EXTRA}
  258. procedure dump_wrong_size(p : pheap_mem_info;size : ptrint;var ptext : text);
  259. begin
  260. Writeln(ptext,'Marked memory at $',HexStr(ptrint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
  261. Writeln(ptext,'Wrong size : ',p^.size,' allocated ',size,' freed');
  262. dump_stack(ptext,get_caller_frame(get_frame));
  263. { the check is done to be sure that the procvar is not overwritten }
  264. if assigned(p^.extra_info) and
  265. (p^.extra_info^.check=$12345678) and
  266. assigned(p^.extra_info^.displayproc) then
  267. p^.extra_info^.displayproc(ptext,@p^.extra_info^.data);
  268. call_stack(p,ptext);
  269. end;
  270. function is_in_getmem_list (p : pheap_mem_info) : boolean;
  271. var
  272. i : ptrint;
  273. pp : pheap_mem_info;
  274. begin
  275. is_in_getmem_list:=false;
  276. pp:=heap_mem_root;
  277. i:=0;
  278. while pp<>nil do
  279. begin
  280. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  281. ((pp^.sig<>calculate_sig(pp)) or not usecrc) and
  282. (pp^.sig <>$AAAAAAAA) then
  283. begin
  284. writeln(ptext^,'error in linked list of heap_mem_info');
  285. RunError(204);
  286. end;
  287. if pp=p then
  288. is_in_getmem_list:=true;
  289. pp:=pp^.previous;
  290. inc(i);
  291. if i>getmem_cnt-freemem_cnt then
  292. writeln(ptext^,'error in linked list of heap_mem_info');
  293. end;
  294. end;
  295. {*****************************************************************************
  296. TraceGetMem
  297. *****************************************************************************}
  298. Function TraceGetMem(size:ptrint):pointer;
  299. var
  300. allocsize,i : ptrint;
  301. oldbp,
  302. bp : pointer;
  303. pl : pdword;
  304. p : pointer;
  305. pp : pheap_mem_info;
  306. begin
  307. inc(getmem_size,size);
  308. inc(getmem8_size,((size+7) div 8)*8);
  309. { Do the real GetMem, but alloc also for the info block }
  310. allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
  311. if add_tail then
  312. inc(allocsize,sizeof(ptrint));
  313. p:=SysGetMem(allocsize);
  314. pp:=pheap_mem_info(p);
  315. inc(p,sizeof(theap_mem_info));
  316. { Create the info block }
  317. pp^.sig:=$DEADBEEF;
  318. pp^.size:=size;
  319. pp^.extra_info_size:=extra_info_size;
  320. pp^.exact_info_size:=exact_info_size;
  321. {
  322. the end of the block contains:
  323. <tail> 4 bytes
  324. <extra_info> X bytes
  325. }
  326. if extra_info_size>0 then
  327. begin
  328. pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
  329. fillchar(pp^.extra_info^,extra_info_size,0);
  330. pp^.extra_info^.check:=$12345678;
  331. pp^.extra_info^.fillproc:=fill_extra_info_proc;
  332. pp^.extra_info^.displayproc:=display_extra_info_proc;
  333. if assigned(fill_extra_info_proc) then
  334. begin
  335. inside_trace_getmem:=true;
  336. fill_extra_info_proc(@pp^.extra_info^.data);
  337. inside_trace_getmem:=false;
  338. end;
  339. end
  340. else
  341. pp^.extra_info:=nil;
  342. if add_tail then
  343. begin
  344. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptrint);
  345. pl^:=$DEADBEEF;
  346. end;
  347. { clear the memory }
  348. fillchar(p^,size,#255);
  349. { retrieve backtrace info }
  350. bp:=get_caller_frame(get_frame);
  351. for i:=1 to tracesize do
  352. begin
  353. pp^.calls[i]:=get_caller_addr(bp);
  354. oldbp:=bp;
  355. bp:=get_caller_frame(bp);
  356. if (bp<oldbp) or (bp>(StackBottom + StackLength)) then
  357. bp:=nil;
  358. end;
  359. { insert in the linked list }
  360. if heap_mem_root<>nil then
  361. heap_mem_root^.next:=pp;
  362. pp^.previous:=heap_mem_root;
  363. pp^.next:=nil;
  364. {$ifdef EXTRA}
  365. pp^.prev_valid:=heap_valid_last;
  366. heap_valid_last:=pp;
  367. if not assigned(heap_valid_first) then
  368. heap_valid_first:=pp;
  369. {$endif EXTRA}
  370. heap_mem_root:=pp;
  371. { must be changed before fill_extra_info is called
  372. because checkpointer can be called from within
  373. fill_extra_info PM }
  374. inc(getmem_cnt);
  375. { update the signature }
  376. if usecrc then
  377. pp^.sig:=calculate_sig(pp);
  378. TraceGetmem:=p;
  379. end;
  380. {*****************************************************************************
  381. TraceFreeMem
  382. *****************************************************************************}
  383. function TraceFreeMemSize(p:pointer;size:ptrint):ptrint;
  384. var
  385. i,ppsize : ptrint;
  386. bp : pointer;
  387. pp : pheap_mem_info;
  388. {$ifdef EXTRA}
  389. pp2 : pheap_mem_info;
  390. {$endif}
  391. extra_size : ptrint;
  392. begin
  393. inc(freemem_size,size);
  394. inc(freemem8_size,((size+7) div 8)*8);
  395. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  396. ppsize:= size + sizeof(theap_mem_info)+pp^.extra_info_size;
  397. if add_tail then
  398. inc(ppsize,sizeof(ptrint));
  399. if not quicktrace then
  400. begin
  401. if not(is_in_getmem_list(pp)) then
  402. RunError(204);
  403. end;
  404. if (pp^.sig=$AAAAAAAA) and not usecrc then
  405. begin
  406. error_in_heap:=true;
  407. dump_already_free(pp,ptext^);
  408. if haltonerror then halt(1);
  409. end
  410. else if ((pp^.sig<>$DEADBEEF) or usecrc) and
  411. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  412. begin
  413. error_in_heap:=true;
  414. dump_error(pp,ptext^);
  415. {$ifdef EXTRA}
  416. dump_error(pp,error_file);
  417. {$endif EXTRA}
  418. { don't release anything in this case !! }
  419. if haltonerror then halt(1);
  420. exit;
  421. end
  422. else if pp^.size<>size then
  423. begin
  424. error_in_heap:=true;
  425. dump_wrong_size(pp,size,ptext^);
  426. {$ifdef EXTRA}
  427. dump_wrong_size(pp,size,error_file);
  428. {$endif EXTRA}
  429. if haltonerror then halt(1);
  430. { don't release anything in this case !! }
  431. exit;
  432. end;
  433. { save old values }
  434. extra_size:=pp^.extra_info_size;
  435. { now it is released !! }
  436. pp^.sig:=$AAAAAAAA;
  437. if not keepreleased then
  438. begin
  439. if pp^.next<>nil then
  440. pp^.next^.previous:=pp^.previous;
  441. if pp^.previous<>nil then
  442. pp^.previous^.next:=pp^.next;
  443. if pp=heap_mem_root then
  444. heap_mem_root:=heap_mem_root^.previous;
  445. end
  446. else
  447. begin
  448. bp:=get_caller_frame(get_frame);
  449. for i:=(tracesize div 2)+1 to tracesize do
  450. begin
  451. pp^.calls[i]:=get_caller_addr(bp);
  452. bp:=get_caller_frame(bp);
  453. end;
  454. end;
  455. inc(freemem_cnt);
  456. { clear the memory }
  457. fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
  458. { this way we keep all info about all released memory !! }
  459. if keepreleased then
  460. begin
  461. {$ifdef EXTRA}
  462. { We want to check if the memory was changed after release !! }
  463. pp^.release_sig:=calculate_release_sig(pp);
  464. if pp=heap_valid_last then
  465. begin
  466. heap_valid_last:=pp^.prev_valid;
  467. if pp=heap_valid_first then
  468. heap_valid_first:=nil;
  469. TraceFreememsize:=size;
  470. exit;
  471. end;
  472. pp2:=heap_valid_last;
  473. while assigned(pp2) do
  474. begin
  475. if pp2^.prev_valid=pp then
  476. begin
  477. pp2^.prev_valid:=pp^.prev_valid;
  478. if pp=heap_valid_first then
  479. heap_valid_first:=pp2;
  480. TraceFreememsize:=size;
  481. exit;
  482. end
  483. else
  484. pp2:=pp2^.prev_valid;
  485. end;
  486. {$endif EXTRA}
  487. TraceFreememsize:=size;
  488. exit;
  489. end;
  490. { release the normal memory at least }
  491. i:=SysFreeMemSize(pp,ppsize);
  492. { return the correct size }
  493. dec(i,sizeof(theap_mem_info)+extra_size);
  494. if add_tail then
  495. dec(i,sizeof(ptrint));
  496. TraceFreeMemSize:=i;
  497. end;
  498. function TraceMemSize(p:pointer):ptrint;
  499. var
  500. pp : pheap_mem_info;
  501. begin
  502. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  503. TraceMemSize:=pp^.size;
  504. end;
  505. function TraceFreeMem(p:pointer):ptrint;
  506. var
  507. l : ptrint;
  508. pp : pheap_mem_info;
  509. begin
  510. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  511. l:=SysMemSize(pp);
  512. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  513. if add_tail then
  514. dec(l,sizeof(ptrint));
  515. { this can never happend normaly }
  516. if pp^.size>l then
  517. begin
  518. dump_wrong_size(pp,l,ptext^);
  519. {$ifdef EXTRA}
  520. dump_wrong_size(pp,l,error_file);
  521. {$endif EXTRA}
  522. end;
  523. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  524. end;
  525. {*****************************************************************************
  526. ReAllocMem
  527. *****************************************************************************}
  528. function TraceReAllocMem(var p:pointer;size:ptrint):Pointer;
  529. var
  530. newP: pointer;
  531. allocsize,
  532. movesize,
  533. i : ptrint;
  534. bp : pointer;
  535. pl : pdword;
  536. pp : pheap_mem_info;
  537. oldsize,
  538. oldextrasize,
  539. oldexactsize : ptrint;
  540. old_fill_extra_info_proc : tfillextrainfoproc;
  541. old_display_extra_info_proc : tdisplayextrainfoproc;
  542. begin
  543. { Free block? }
  544. if size=0 then
  545. begin
  546. if p<>nil then
  547. TraceFreeMem(p);
  548. p:=nil;
  549. TraceReallocMem:=P;
  550. exit;
  551. end;
  552. { Allocate a new block? }
  553. if p=nil then
  554. begin
  555. p:=TraceGetMem(size);
  556. TraceReallocMem:=P;
  557. exit;
  558. end;
  559. { Resize block }
  560. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  561. { test block }
  562. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  563. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  564. begin
  565. error_in_heap:=true;
  566. dump_error(pp,ptext^);
  567. {$ifdef EXTRA}
  568. dump_error(pp,error_file);
  569. {$endif EXTRA}
  570. { don't release anything in this case !! }
  571. if haltonerror then halt(1);
  572. exit;
  573. end;
  574. { save info }
  575. oldsize:=pp^.size;
  576. oldextrasize:=pp^.extra_info_size;
  577. oldexactsize:=pp^.exact_info_size;
  578. if pp^.extra_info_size>0 then
  579. begin
  580. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  581. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  582. end;
  583. { Do the real ReAllocMem, but alloc also for the info block }
  584. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  585. if add_tail then
  586. inc(allocsize,sizeof(ptrint));
  587. { Try to resize the block, if not possible we need to do a
  588. getmem, move data, freemem }
  589. if not SysTryResizeMem(pp,allocsize) then
  590. begin
  591. { get a new block }
  592. newP := TraceGetMem(size);
  593. { move the data }
  594. if newP <> nil then
  595. begin
  596. movesize:=TraceMemSize(p);
  597. {if the old size is larger than the new size,
  598. move only the new size}
  599. if movesize>size then
  600. movesize:=size;
  601. move(p^,newP^,movesize);
  602. end;
  603. { release p }
  604. traceFreeMem(p);
  605. { return the new pointer }
  606. p:=newp;
  607. traceReAllocMem := newp;
  608. exit;
  609. end;
  610. { Recreate the info block }
  611. pp^.sig:=$DEADBEEF;
  612. pp^.size:=size;
  613. pp^.extra_info_size:=oldextrasize;
  614. pp^.exact_info_size:=oldexactsize;
  615. { add the new extra_info and tail }
  616. if pp^.extra_info_size>0 then
  617. begin
  618. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  619. fillchar(pp^.extra_info^,extra_info_size,0);
  620. pp^.extra_info^.check:=$12345678;
  621. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  622. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  623. if assigned(pp^.extra_info^.fillproc) then
  624. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  625. end
  626. else
  627. pp^.extra_info:=nil;
  628. if add_tail then
  629. begin
  630. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(ptrint);
  631. pl^:=$DEADBEEF;
  632. end;
  633. { adjust like a freemem and then a getmem, so you get correct
  634. results in the summary display }
  635. inc(freemem_size,oldsize);
  636. inc(freemem8_size,((oldsize+7) div 8)*8);
  637. inc(getmem_size,size);
  638. inc(getmem8_size,((size+7) div 8)*8);
  639. { generate new backtrace }
  640. bp:=get_caller_frame(get_frame);
  641. for i:=1 to tracesize do
  642. begin
  643. pp^.calls[i]:=get_caller_addr(bp);
  644. bp:=get_caller_frame(bp);
  645. end;
  646. { regenerate signature }
  647. if usecrc then
  648. pp^.sig:=calculate_sig(pp);
  649. { return the pointer }
  650. p:=pointer(pp)+sizeof(theap_mem_info);
  651. TraceReAllocmem:=p;
  652. end;
  653. {*****************************************************************************
  654. Check pointer
  655. *****************************************************************************}
  656. {$ifndef Unix}
  657. {$S-}
  658. {$endif}
  659. {$ifdef go32v2}
  660. var
  661. __stklen : longword;external name '__stklen';
  662. __stkbottom : longword;external name '__stkbottom';
  663. edata : longword; external name 'edata';
  664. {$endif go32v2}
  665. {$ifdef linux}
  666. var
  667. etext: ptruint; external name '_etext';
  668. edata : ptruint; external name '_edata';
  669. eend : ptruint; external name '_end';
  670. {$endif}
  671. {$ifdef win32}
  672. var
  673. sdata : ptruint; external name '__data_start__';
  674. edata : ptruint; external name '__data_end__';
  675. sbss : ptruint; external name '__bss_start__';
  676. ebss : ptruint; external name '__bss_end__';
  677. {$endif}
  678. procedure CheckPointer(p : pointer);{$ifndef NOSAVEREGISTERS}saveregisters;{$endif}[public, alias : 'FPC_CHECKPOINTER'];
  679. var
  680. i : ptrint;
  681. pp : pheap_mem_info;
  682. {$ifdef go32v2}
  683. get_ebp,stack_top : longword;
  684. data_end : longword;
  685. {$endif go32v2}
  686. label
  687. _exit;
  688. begin
  689. if p=nil then
  690. runerror(204);
  691. i:=0;
  692. {$ifdef go32v2}
  693. if ptruint(p)<$1000 then
  694. runerror(216);
  695. asm
  696. movl %ebp,get_ebp
  697. leal edata,%eax
  698. movl %eax,data_end
  699. end;
  700. stack_top:=__stkbottom+__stklen;
  701. { allow all between start of code and end of data }
  702. if ptruint(p)<=data_end then
  703. goto _exit;
  704. { stack can be above heap !! }
  705. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  706. goto _exit;
  707. {$endif go32v2}
  708. { I don't know where the stack is in other OS !! }
  709. {$ifdef win32}
  710. { inside stack ? }
  711. if (ptruint(p)>ptruint(get_frame)) and
  712. (ptruint(p)<Win32StackTop) then
  713. goto _exit;
  714. { inside data ? }
  715. if (ptruint(p)>=ptruint(@sdata)) and (ptruint(p)<ptruint(@edata)) then
  716. goto _exit;
  717. { inside bss ? }
  718. if (ptruint(p)>=ptruint(@sbss)) and (ptruint(p)<ptruint(@ebss)) then
  719. goto _exit;
  720. {$endif win32}
  721. {$ifdef linux}
  722. { inside stack ? }
  723. if (ptruint(p)>ptruint(get_frame)) and
  724. (ptruint(p)<$c0000000) then //todo: 64bit!
  725. goto _exit;
  726. { inside data or bss ? }
  727. if (ptruint(p)>=ptruint(@etext)) and (ptruint(p)<ptruint(@eend)) then
  728. goto _exit;
  729. {$endif linux}
  730. { first try valid list faster }
  731. {$ifdef EXTRA}
  732. pp:=heap_valid_last;
  733. while pp<>nil do
  734. begin
  735. { inside this valid block ! }
  736. { we can be changing the extrainfo !! }
  737. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  738. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  739. begin
  740. { check allocated block }
  741. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  742. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  743. { special case of the fill_extra_info call }
  744. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  745. and inside_trace_getmem) then
  746. goto _exit
  747. else
  748. begin
  749. writeln(ptext^,'corrupted heap_mem_info');
  750. dump_error(pp,ptext^);
  751. halt(1);
  752. end;
  753. end
  754. else
  755. pp:=pp^.prev_valid;
  756. inc(i);
  757. if i>getmem_cnt-freemem_cnt then
  758. begin
  759. writeln(ptext^,'error in linked list of heap_mem_info');
  760. halt(1);
  761. end;
  762. end;
  763. i:=0;
  764. {$endif EXTRA}
  765. pp:=heap_mem_root;
  766. while pp<>nil do
  767. begin
  768. { inside this block ! }
  769. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  770. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  771. { allocated block }
  772. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  773. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  774. goto _exit
  775. else
  776. begin
  777. writeln(ptext^,'pointer $',hexstr(ptrint(p),8),' points into invalid memory block');
  778. dump_error(pp,ptext^);
  779. runerror(204);
  780. end;
  781. pp:=pp^.previous;
  782. inc(i);
  783. if i>getmem_cnt then
  784. begin
  785. writeln(ptext^,'error in linked list of heap_mem_info');
  786. halt(1);
  787. end;
  788. end;
  789. writeln(ptext^,'pointer $',hexstr(ptrint(p),8),' does not point to valid memory block');
  790. runerror(204);
  791. _exit:
  792. end;
  793. {*****************************************************************************
  794. Dump Heap
  795. *****************************************************************************}
  796. procedure dumpheap;
  797. var
  798. pp : pheap_mem_info;
  799. i : ptrint;
  800. ExpectedHeapFree : ptrint;
  801. {$ifdef HASGETFPCHEAPSTATUS}
  802. status : TFPCHeapStatus;
  803. {$else HASGETFPCHEAPSTATUS}
  804. status : THeapStatus;
  805. {$endif HASGETFPCHEAPSTATUS}
  806. begin
  807. pp:=heap_mem_root;
  808. Writeln(ptext^,'Heap dump by heaptrc unit');
  809. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  810. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  811. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  812. {$ifdef HASGETFPCHEAPSTATUS}
  813. status:=SysGetFPCHeapStatus;
  814. {$else HASGETFPCHEAPSTATUS}
  815. SysGetHeapStatus(status);
  816. {$endif HASGETFPCHEAPSTATUS}
  817. Write(ptext^,'True heap size : ',status.CurrHeapSize);
  818. if EntryMemUsed > 0 then
  819. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  820. else
  821. Writeln(ptext^);
  822. Writeln(ptext^,'True free heap : ',status.CurrHeapFree);
  823. ExpectedHeapFree:=status.CurrHeapSize-(getmem8_size-freemem8_size)-
  824. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)-EntryMemUsed;
  825. If ExpectedHeapFree<>status.CurrHeapFree then
  826. Writeln(ptext^,'Should be : ',ExpectedHeapFree);
  827. i:=getmem_cnt-freemem_cnt;
  828. while pp<>nil do
  829. begin
  830. if i<0 then
  831. begin
  832. Writeln(ptext^,'Error in heap memory list');
  833. Writeln(ptext^,'More memory blocks than expected');
  834. exit;
  835. end;
  836. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  837. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  838. begin
  839. { this one was not released !! }
  840. if exitcode<>203 then
  841. call_stack(pp,ptext^);
  842. dec(i);
  843. end
  844. else if pp^.sig<>$AAAAAAAA then
  845. begin
  846. dump_error(pp,ptext^);
  847. {$ifdef EXTRA}
  848. dump_error(pp,error_file);
  849. {$endif EXTRA}
  850. error_in_heap:=true;
  851. end
  852. {$ifdef EXTRA}
  853. else if pp^.release_sig<>calculate_release_sig(pp) then
  854. begin
  855. dump_change_after(pp,ptext^);
  856. dump_change_after(pp,error_file);
  857. error_in_heap:=true;
  858. end
  859. {$endif EXTRA}
  860. ;
  861. pp:=pp^.previous;
  862. end;
  863. end;
  864. procedure markheap;
  865. var
  866. pp : pheap_mem_info;
  867. begin
  868. pp:=heap_mem_root;
  869. while pp<>nil do
  870. begin
  871. pp^.sig:=$AAAAAAAA;
  872. pp:=pp^.previous;
  873. end;
  874. end;
  875. {*****************************************************************************
  876. AllocMem
  877. *****************************************************************************}
  878. function TraceAllocMem(size:ptrint):Pointer;
  879. begin
  880. TraceAllocMem:=SysAllocMem(size);
  881. end;
  882. {*****************************************************************************
  883. No specific tracing calls
  884. *****************************************************************************}
  885. {$ifdef HASGETFPCHEAPSTATUS}
  886. function TraceGetHeapStatus:THeapStatus;
  887. begin
  888. TraceGetHeapStatus:=SysGetHeapStatus;
  889. end;
  890. function TraceGetFPCHeapStatus:TFPCHeapStatus;
  891. begin
  892. TraceGetFPCHeapStatus:=SysGetFPCHeapStatus;
  893. end;
  894. {$else HASGETFPCHEAPSTATUS}
  895. procedure TraceGetHeapStatus(var status:THeapStatus);
  896. begin
  897. SysGetHeapStatus(status);
  898. end;
  899. {$endif HASGETFPCHEAPSTATUS}
  900. {*****************************************************************************
  901. Program Hooks
  902. *****************************************************************************}
  903. Procedure SetHeapTraceOutput(const name : string);
  904. var i : ptrint;
  905. begin
  906. if ptext<>@stderr then
  907. begin
  908. ptext:=@stderr;
  909. close(ownfile);
  910. end;
  911. assign(ownfile,name);
  912. {$I-}
  913. append(ownfile);
  914. if IOResult<>0 then
  915. Rewrite(ownfile);
  916. {$I+}
  917. ptext:=@ownfile;
  918. for i:=0 to Paramcount do
  919. write(ptext^,paramstr(i),' ');
  920. writeln(ptext^);
  921. end;
  922. procedure SetHeapExtraInfo( size : ptrint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  923. begin
  924. { the total size must stay multiple of 8, also allocate 2 pointers for
  925. the fill and display procvars }
  926. exact_info_size:=size + sizeof(theap_extra_info);
  927. extra_info_size:=((exact_info_size+7) div 8)*8;
  928. fill_extra_info_proc:=fillproc;
  929. display_extra_info_proc:=displayproc;
  930. end;
  931. {*****************************************************************************
  932. Install MemoryManager
  933. *****************************************************************************}
  934. const
  935. TraceManager:TMemoryManager=(
  936. NeedLock : true;
  937. Getmem : @TraceGetMem;
  938. Freemem : @TraceFreeMem;
  939. FreememSize : @TraceFreeMemSize;
  940. AllocMem : @TraceAllocMem;
  941. ReAllocMem : @TraceReAllocMem;
  942. MemSize : @TraceMemSize;
  943. {$ifdef HASGETFPCHEAPSTATUS}
  944. GetHeapStatus : @TraceGetHeapStatus;
  945. GetFPCHeapStatus : @TraceGetFPCHeapStatus;
  946. {$else HASGETFPCHEAPSTATUS}
  947. GetHeapStatus : @TraceGetHeapStatus;
  948. {$endif HASGETFPCHEAPSTATUS}
  949. );
  950. procedure TraceInit;
  951. var
  952. {$ifdef HASGETFPCHEAPSTATUS}
  953. initheapstatus : TFPCHeapStatus;
  954. {$else HASGETFPCHEAPSTATUS}
  955. initheapstatus : THeapStatus;
  956. {$endif HASGETFPCHEAPSTATUS}
  957. begin
  958. {$ifdef HASGETFPCHEAPSTATUS}
  959. initheapstatus:=SysGetFPCHeapStatus;
  960. {$else HASGETFPCHEAPSTATUS}
  961. SysGetHeapStatus(initheapstatus);
  962. {$endif HASGETFPCHEAPSTATUS}
  963. EntryMemUsed:=initheapstatus.CurrHeapUsed;
  964. MakeCRC32Tbl;
  965. SetMemoryManager(TraceManager);
  966. ptext:=@stderr;
  967. if outputstr <> '' then
  968. SetHeapTraceOutput(outputstr);
  969. {$ifdef EXTRA}
  970. Assign(error_file,'heap.err');
  971. Rewrite(error_file);
  972. {$endif EXTRA}
  973. end;
  974. procedure TraceExit;
  975. begin
  976. { no dump if error
  977. because this gives long long listings }
  978. { clear inoutres, in case the program that quit didn't }
  979. ioresult;
  980. if (exitcode<>0) and (erroraddr<>nil) then
  981. begin
  982. Writeln(ptext^,'No heap dump by heaptrc unit');
  983. Writeln(ptext^,'Exitcode = ',exitcode);
  984. if ptext<>@stderr then
  985. begin
  986. ptext:=@stderr;
  987. close(ownfile);
  988. end;
  989. exit;
  990. end;
  991. if not error_in_heap then
  992. Dumpheap;
  993. if error_in_heap and (exitcode=0) then
  994. exitcode:=203;
  995. {$ifdef EXTRA}
  996. Close(error_file);
  997. {$endif EXTRA}
  998. if ptext<>@stderr then
  999. begin
  1000. ptext:=@stderr;
  1001. close(ownfile);
  1002. end;
  1003. end;
  1004. {$ifdef win32}
  1005. function GetEnvironmentStrings : pchar; stdcall;
  1006. external 'kernel32' name 'GetEnvironmentStringsA';
  1007. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  1008. external 'kernel32' name 'FreeEnvironmentStringsA';
  1009. Function GetEnv(envvar: string): string;
  1010. var
  1011. s : string;
  1012. i : ptrint;
  1013. hp,p : pchar;
  1014. begin
  1015. getenv:='';
  1016. p:=GetEnvironmentStrings;
  1017. hp:=p;
  1018. while hp^<>#0 do
  1019. begin
  1020. s:=strpas(hp);
  1021. i:=pos('=',s);
  1022. if upcase(copy(s,1,i-1))=upcase(envvar) then
  1023. begin
  1024. getenv:=copy(s,i+1,length(s)-i);
  1025. break;
  1026. end;
  1027. { next string entry}
  1028. hp:=hp+strlen(hp)+1;
  1029. end;
  1030. FreeEnvironmentStrings(p);
  1031. end;
  1032. {$else}
  1033. Function GetEnv(P:string):Pchar;
  1034. {
  1035. Searches the environment for a string with name p and
  1036. returns a pchar to it's value.
  1037. A pchar is used to accomodate for strings of length > 255
  1038. }
  1039. var
  1040. ep : ppchar;
  1041. i : ptrint;
  1042. found : boolean;
  1043. Begin
  1044. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1045. ep:=envp;
  1046. found:=false;
  1047. if ep<>nil then
  1048. begin
  1049. while (not found) and (ep^<>nil) do
  1050. begin
  1051. found:=true;
  1052. for i:=1 to length(p) do
  1053. if p[i]<>ep^[i-1] then
  1054. begin
  1055. found:=false;
  1056. break;
  1057. end;
  1058. if not found then
  1059. inc(ep);
  1060. end;
  1061. end;
  1062. if found then
  1063. getenv:=ep^+length(p)
  1064. else
  1065. getenv:=nil;
  1066. end;
  1067. {$endif}
  1068. procedure LoadEnvironment;
  1069. var
  1070. i,j : ptrint;
  1071. s : string;
  1072. begin
  1073. s:=Getenv('HEAPTRC');
  1074. if pos('keepreleased',s)>0 then
  1075. keepreleased:=true;
  1076. if pos('disabled',s)>0 then
  1077. useheaptrace:=false;
  1078. if pos('nohalt',s)>0 then
  1079. haltonerror:=false;
  1080. i:=pos('log=',s);
  1081. if i>0 then
  1082. begin
  1083. outputstr:=copy(s,i+4,255);
  1084. j:=pos(' ',outputstr);
  1085. if j=0 then
  1086. j:=length(outputstr)+1;
  1087. delete(outputstr,j,255);
  1088. end;
  1089. end;
  1090. Initialization
  1091. LoadEnvironment;
  1092. { heaptrc can be disabled from the environment }
  1093. if useheaptrace then
  1094. TraceInit;
  1095. finalization
  1096. if useheaptrace then
  1097. TraceExit;
  1098. end.
  1099. {
  1100. $Log: heaptrc.pp,v $
  1101. Revision 1.44 2005/04/04 15:16:26 peter
  1102. * fixed crash in tracereallocmem statictics
  1103. Revision 1.43 2005/03/25 22:53:39 jonas
  1104. * fixed several warnings and notes about unused variables (mainly) or
  1105. uninitialised use of variables/function results (a few)
  1106. Revision 1.42 2005/03/10 20:36:31 florian
  1107. * fixed pointer checking for win32, thx to Martin Schreiber for the patch
  1108. Revision 1.41 2005/03/04 16:49:34 peter
  1109. * fix getheapstatus bootstrapping
  1110. Revision 1.40 2005/02/28 15:38:38 marco
  1111. * getFPCheapstatus (no, FPC HEAP, not FP CHEAP!)
  1112. Revision 1.39 2005/02/14 17:13:22 peter
  1113. * truncate log
  1114. Revision 1.38 2005/01/21 15:56:32 peter
  1115. * uses _eend instead of _edata in checkpointer, patch by
  1116. Martin Schreiber
  1117. }