heaptrc.pp 32 KB

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