heaptrc.pp 32 KB

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