heaptrc.pp 33 KB

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