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(var 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. p:=nil;
  466. exit;
  467. end;
  468. pp2:=heap_valid_last;
  469. while assigned(pp2) do
  470. begin
  471. if pp2^.prev_valid=pp then
  472. begin
  473. pp2^.prev_valid:=pp^.prev_valid;
  474. if pp=heap_valid_first then
  475. heap_valid_first:=pp2;
  476. TraceFreememsize:=size;
  477. p:=nil;
  478. exit;
  479. end
  480. else
  481. pp2:=pp2^.prev_valid;
  482. end;
  483. {$endif EXTRA}
  484. TraceFreememsize:=size;
  485. p:=nil;
  486. exit;
  487. end;
  488. { release the normal memory at least }
  489. i:=SysFreeMemSize(pp,ppsize);
  490. { return the correct size }
  491. dec(i,sizeof(theap_mem_info)+extra_size);
  492. if add_tail then
  493. dec(i,sizeof(longint));
  494. TraceFreeMemSize:=i;
  495. end;
  496. function TraceMemSize(p:pointer):Longint;
  497. var
  498. l : longint;
  499. pp : pheap_mem_info;
  500. begin
  501. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  502. l:=SysMemSize(pp);
  503. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  504. if add_tail then
  505. dec(l,sizeof(longint));
  506. TraceMemSize:=l;
  507. end;
  508. function TraceFreeMem(var p:pointer):longint;
  509. var
  510. size : longint;
  511. pp : pheap_mem_info;
  512. begin
  513. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  514. size:=TraceMemSize(p);
  515. { this can never happend normaly }
  516. if pp^.size>size then
  517. begin
  518. dump_wrong_size(pp,size,ptext^);
  519. {$ifdef EXTRA}
  520. dump_wrong_size(pp,size,error_file);
  521. {$endif EXTRA}
  522. end;
  523. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  524. end;
  525. {*****************************************************************************
  526. ReAllocMem
  527. *****************************************************************************}
  528. function TraceReAllocMem(var p:pointer;size:longint):Pointer;
  529. var
  530. newP: pointer;
  531. oldsize,
  532. allocsize,
  533. i,bp : longint;
  534. pl : pdword;
  535. pp : pheap_mem_info;
  536. oldextrasize,
  537. oldexactsize : longint;
  538. old_fill_extra_info_proc : tfillextrainfoproc;
  539. old_display_extra_info_proc : tdisplayextrainfoproc;
  540. begin
  541. { Free block? }
  542. if size=0 then
  543. begin
  544. if p<>nil then
  545. TraceFreeMem(p);
  546. TraceReallocMem:=P;
  547. exit;
  548. end;
  549. { Allocate a new block? }
  550. if p=nil then
  551. begin
  552. p:=TraceGetMem(size);
  553. TraceReallocMem:=P;
  554. exit;
  555. end;
  556. { Resize block }
  557. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  558. { test block }
  559. if ((pp^.sig<>$DEADBEEF) or usecrc) and
  560. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  561. begin
  562. error_in_heap:=true;
  563. dump_error(pp,ptext^);
  564. {$ifdef EXTRA}
  565. dump_error(pp,error_file);
  566. {$endif EXTRA}
  567. { don't release anything in this case !! }
  568. if haltonerror then halt(1);
  569. exit;
  570. end;
  571. { save info }
  572. oldextrasize:=pp^.extra_info_size;
  573. oldexactsize:=pp^.exact_info_size;
  574. if pp^.extra_info_size>0 then
  575. begin
  576. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  577. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  578. end;
  579. { Do the real ReAllocMem, but alloc also for the info block }
  580. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  581. if add_tail then
  582. inc(allocsize,sizeof(longint));
  583. { Try to resize the block, if not possible we need to do a
  584. getmem, move data, freemem }
  585. if not SysTryResizeMem(pp,allocsize) then
  586. begin
  587. { get a new block }
  588. oldsize:=TraceMemSize(p);
  589. newP := TraceGetMem(size);
  590. { move the data }
  591. if newP <> nil then
  592. move(p^,newP^,oldsize);
  593. { release p }
  594. traceFreeMem(p);
  595. { return the new pointer }
  596. p:=newp;
  597. traceReAllocMem := newp;
  598. exit;
  599. end;
  600. { adjust like a freemem and then a getmem, so you get correct
  601. results in the summary display }
  602. inc(freemem_size,pp^.size);
  603. inc(freemem8_size,((pp^.size+7) div 8)*8);
  604. inc(getmem_size,size);
  605. inc(getmem8_size,((size+7) div 8)*8);
  606. { Recreate the info block }
  607. pp^.sig:=$DEADBEEF;
  608. pp^.size:=size;
  609. pp^.extra_info_size:=oldextrasize;
  610. pp^.exact_info_size:=oldexactsize;
  611. { add the new extra_info and tail }
  612. if pp^.extra_info_size>0 then
  613. begin
  614. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  615. fillchar(pp^.extra_info^,extra_info_size,0);
  616. pp^.extra_info^.check:=$12345678;
  617. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  618. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  619. if assigned(pp^.extra_info^.fillproc) then
  620. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  621. end
  622. else
  623. pp^.extra_info:=nil;
  624. if add_tail then
  625. begin
  626. pl:=pointer(pp)+allocsize-pp^.extra_info_size-sizeof(longint);
  627. pl^:=$DEADBEEF;
  628. end;
  629. { generate new backtrace }
  630. bp:=get_caller_frame(get_frame);
  631. for i:=1 to tracesize do
  632. begin
  633. pp^.calls[i]:=get_caller_addr(bp);
  634. bp:=get_caller_frame(bp);
  635. end;
  636. { regenerate signature }
  637. if usecrc then
  638. pp^.sig:=calculate_sig(pp);
  639. { return the pointer }
  640. p:=pointer(pp)+sizeof(theap_mem_info);
  641. TraceReAllocmem:=p;
  642. end;
  643. {*****************************************************************************
  644. Check pointer
  645. *****************************************************************************}
  646. {$ifndef Unix}
  647. {$S-}
  648. {$endif}
  649. {$ifdef go32v2}
  650. var
  651. __stklen : cardinal;external name '__stklen';
  652. __stkbottom : cardinal;external name '__stkbottom';
  653. edata : cardinal; external name 'edata';
  654. heap_at_init : pointer;
  655. {$endif go32v2}
  656. {$ifdef win32}
  657. var
  658. StartUpHeapEnd : pointer;
  659. { I found no symbol for start of text section :(
  660. so we usee the _mainCRTStartup which should be
  661. in wprt0.ow or wdllprt0.ow PM }
  662. text_begin : cardinal;external name '_mainCRTStartup';
  663. data_end : cardinal;external name '__data_end__';
  664. {$endif}
  665. procedure CheckPointer(p : pointer);[saveregisters,public, alias : 'FPC_CHECKPOINTER'];
  666. var
  667. i : longint;
  668. pp : pheap_mem_info;
  669. get_ebp,stack_top : cardinal;
  670. data_end : cardinal;
  671. label
  672. _exit;
  673. begin
  674. if p=nil then
  675. goto _exit;
  676. i:=0;
  677. {$ifdef go32v2}
  678. if cardinal(p)<$1000 then
  679. runerror(216);
  680. asm
  681. movl %ebp,get_ebp
  682. leal edata,%eax
  683. movl %eax,data_end
  684. end;
  685. stack_top:=__stkbottom+__stklen;
  686. { allow all between start of code and end of data }
  687. if cardinal(p)<=data_end then
  688. goto _exit;
  689. { .bss section }
  690. if cardinal(p)<=cardinal(heap_at_init) then
  691. goto _exit;
  692. { stack can be above heap !! }
  693. if (cardinal(p)>=get_ebp) and (cardinal(p)<=stack_top) then
  694. goto _exit;
  695. {$endif go32v2}
  696. { I don't know where the stack is in other OS !! }
  697. {$ifdef win32}
  698. if (cardinal(p)>=$40000) and (p<=HeapOrg) then
  699. goto _exit;
  700. { inside stack ? }
  701. asm
  702. movl %ebp,get_ebp
  703. end;
  704. if (cardinal(p)>get_ebp) and
  705. (cardinal(p)<Win32StackTop) then
  706. goto _exit;
  707. {$endif win32}
  708. if p>=heapptr then
  709. runerror(216);
  710. { first try valid list faster }
  711. {$ifdef EXTRA}
  712. pp:=heap_valid_last;
  713. while pp<>nil do
  714. begin
  715. { inside this valid block ! }
  716. { we can be changing the extrainfo !! }
  717. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  718. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  719. begin
  720. { check allocated block }
  721. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  722. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  723. { special case of the fill_extra_info call }
  724. ((pp=heap_valid_last) and usecrc and (pp^.sig=$DEADBEEF)
  725. and inside_trace_getmem) then
  726. goto _exit
  727. else
  728. begin
  729. writeln(ptext^,'corrupted heap_mem_info');
  730. dump_error(pp,ptext^);
  731. halt(1);
  732. end;
  733. end
  734. else
  735. pp:=pp^.prev_valid;
  736. inc(i);
  737. if i>getmem_cnt-freemem_cnt then
  738. begin
  739. writeln(ptext^,'error in linked list of heap_mem_info');
  740. halt(1);
  741. end;
  742. end;
  743. i:=0;
  744. {$endif EXTRA}
  745. pp:=heap_mem_root;
  746. while pp<>nil do
  747. begin
  748. { inside this block ! }
  749. if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info)+cardinal(extra_info_size)) and
  750. (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+cardinal(extra_info_size)+cardinal(pp^.size)) then
  751. { allocated block }
  752. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  753. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  754. goto _exit
  755. else
  756. begin
  757. writeln(ptext^,'pointer $',hexstr(longint(p),8),' points into invalid memory block');
  758. dump_error(pp,ptext^);
  759. runerror(204);
  760. end;
  761. pp:=pp^.previous;
  762. inc(i);
  763. if i>getmem_cnt then
  764. begin
  765. writeln(ptext^,'error in linked list of heap_mem_info');
  766. halt(1);
  767. end;
  768. end;
  769. writeln(ptext^,'pointer $',hexstr(longint(p),8),' does not point to valid memory block');
  770. runerror(204);
  771. _exit:
  772. end;
  773. {*****************************************************************************
  774. Dump Heap
  775. *****************************************************************************}
  776. procedure dumpheap;
  777. var
  778. pp : pheap_mem_info;
  779. i : longint;
  780. ExpectedMemAvail : longint;
  781. begin
  782. pp:=heap_mem_root;
  783. Writeln(ptext^,'Heap dump by heaptrc unit');
  784. Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
  785. Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
  786. Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
  787. Write(ptext^,'True heap size : ',system.HeapSize);
  788. if EntryMemUsed > 0 then
  789. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  790. else
  791. Writeln(ptext^);
  792. Writeln(ptext^,'True free heap : ',MemAvail);
  793. ExpectedMemAvail:=system.HeapSize-(getmem8_size-freemem8_size)-
  794. (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)-EntryMemUsed;
  795. If ExpectedMemAvail<>MemAvail then
  796. Writeln(ptext^,'Should be : ',ExpectedMemAvail);
  797. i:=getmem_cnt-freemem_cnt;
  798. while pp<>nil do
  799. begin
  800. if i<0 then
  801. begin
  802. Writeln(ptext^,'Error in heap memory list');
  803. Writeln(ptext^,'More memory blocks than expected');
  804. exit;
  805. end;
  806. if ((pp^.sig=$DEADBEEF) and not usecrc) or
  807. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  808. begin
  809. { this one was not released !! }
  810. if exitcode<>203 then
  811. call_stack(pp,ptext^);
  812. dec(i);
  813. end
  814. else if pp^.sig<>$AAAAAAAA then
  815. begin
  816. dump_error(pp,ptext^);
  817. {$ifdef EXTRA}
  818. dump_error(pp,error_file);
  819. {$endif EXTRA}
  820. error_in_heap:=true;
  821. end
  822. {$ifdef EXTRA}
  823. else if pp^.release_sig<>calculate_release_sig(pp) then
  824. begin
  825. dump_change_after(pp,ptext^);
  826. dump_change_after(pp,error_file);
  827. error_in_heap:=true;
  828. end
  829. {$endif EXTRA}
  830. ;
  831. pp:=pp^.previous;
  832. end;
  833. end;
  834. procedure markheap;
  835. var
  836. pp : pheap_mem_info;
  837. begin
  838. pp:=heap_mem_root;
  839. while pp<>nil do
  840. begin
  841. pp^.sig:=$AAAAAAAA;
  842. pp:=pp^.previous;
  843. end;
  844. end;
  845. {*****************************************************************************
  846. AllocMem
  847. *****************************************************************************}
  848. function TraceAllocMem(size:longint):Pointer;
  849. begin
  850. TraceAllocMem:=SysAllocMem(size);
  851. end;
  852. {*****************************************************************************
  853. No specific tracing calls
  854. *****************************************************************************}
  855. function TraceMemAvail:longint;
  856. begin
  857. TraceMemAvail:=SysMemAvail;
  858. end;
  859. function TraceMaxAvail:longint;
  860. begin
  861. TraceMaxAvail:=SysMaxAvail;
  862. end;
  863. function TraceHeapSize:longint;
  864. begin
  865. TraceHeapSize:=SysHeapSize;
  866. end;
  867. {*****************************************************************************
  868. Program Hooks
  869. *****************************************************************************}
  870. Procedure SetHeapTraceOutput(const name : string);
  871. var i : longint;
  872. begin
  873. if ptext<>@stderr then
  874. begin
  875. ptext:=@stderr;
  876. close(ownfile);
  877. end;
  878. assign(ownfile,name);
  879. {$I-}
  880. append(ownfile);
  881. if IOResult<>0 then
  882. Rewrite(ownfile);
  883. {$I+}
  884. ptext:=@ownfile;
  885. for i:=0 to Paramcount do
  886. write(ptext^,paramstr(i),' ');
  887. writeln(ptext^);
  888. end;
  889. procedure SetHeapExtraInfo( size : longint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  890. begin
  891. { the total size must stay multiple of 8, also allocate 2 pointers for
  892. the fill and display procvars }
  893. exact_info_size:=size + sizeof(theap_extra_info);
  894. extra_info_size:=((exact_info_size+7) div 8)*8;
  895. fill_extra_info_proc:=fillproc;
  896. display_extra_info_proc:=displayproc;
  897. end;
  898. {*****************************************************************************
  899. Install MemoryManager
  900. *****************************************************************************}
  901. const
  902. TraceManager:TMemoryManager=(
  903. Getmem : @TraceGetMem;
  904. Freemem : @TraceFreeMem;
  905. FreememSize : @TraceFreeMemSize;
  906. AllocMem : @TraceAllocMem;
  907. ReAllocMem : @TraceReAllocMem;
  908. MemSize : @TraceMemSize;
  909. MemAvail : @TraceMemAvail;
  910. MaxAvail : @TraceMaxAvail;
  911. HeapSize : @TraceHeapsize;
  912. );
  913. procedure TraceInit;
  914. begin
  915. EntryMemUsed:=System.HeapSize-MemAvail;
  916. MakeCRC32Tbl;
  917. SetMemoryManager(TraceManager);
  918. ptext:=@stderr;
  919. {$ifdef EXTRA}
  920. Assign(error_file,'heap.err');
  921. Rewrite(error_file);
  922. {$endif EXTRA}
  923. { checkpointer init }
  924. {$ifdef go32v2}
  925. Heap_at_init:=HeapPtr;
  926. {$endif}
  927. {$ifdef win32}
  928. StartupHeapEnd:=HeapEnd;
  929. {$endif}
  930. end;
  931. procedure TraceExit;
  932. begin
  933. { no dump if error
  934. because this gives long long listings }
  935. { clear inoutres, in case the program that quit didn't }
  936. ioresult;
  937. if (exitcode<>0) and (erroraddr<>nil) then
  938. begin
  939. Writeln(ptext^,'No heap dump by heaptrc unit');
  940. Writeln(ptext^,'Exitcode = ',exitcode);
  941. if ptext<>@stderr then
  942. begin
  943. ptext:=@stderr;
  944. close(ownfile);
  945. end;
  946. exit;
  947. end;
  948. if not error_in_heap then
  949. Dumpheap;
  950. if error_in_heap and (exitcode=0) then
  951. exitcode:=203;
  952. {$ifdef EXTRA}
  953. Close(error_file);
  954. {$endif EXTRA}
  955. if ptext<>@stderr then
  956. begin
  957. ptext:=@stderr;
  958. close(ownfile);
  959. end;
  960. end;
  961. {$ifdef win32}
  962. function GetEnvironmentStrings : pchar;
  963. external 'kernel32' name 'GetEnvironmentStringsA';
  964. function FreeEnvironmentStrings(p : pchar) : longbool;
  965. external 'kernel32' name 'FreeEnvironmentStringsA';
  966. Function GetEnv(envvar: string): string;
  967. var
  968. s : string;
  969. i : longint;
  970. hp,p : pchar;
  971. begin
  972. getenv:='';
  973. p:=GetEnvironmentStrings;
  974. hp:=p;
  975. while hp^<>#0 do
  976. begin
  977. s:=strpas(hp);
  978. i:=pos('=',s);
  979. if upcase(copy(s,1,i-1))=upcase(envvar) then
  980. begin
  981. getenv:=copy(s,i+1,length(s)-i);
  982. break;
  983. end;
  984. { next string entry}
  985. hp:=hp+strlen(hp)+1;
  986. end;
  987. FreeEnvironmentStrings(p);
  988. end;
  989. {$else}
  990. Function GetEnv(P:string):Pchar;
  991. {
  992. Searches the environment for a string with name p and
  993. returns a pchar to it's value.
  994. A pchar is used to accomodate for strings of length > 255
  995. }
  996. var
  997. ep : ppchar;
  998. i : longint;
  999. found : boolean;
  1000. Begin
  1001. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1002. ep:=envp;
  1003. found:=false;
  1004. if ep<>nil then
  1005. begin
  1006. while (not found) and (ep^<>nil) do
  1007. begin
  1008. found:=true;
  1009. for i:=1 to length(p) do
  1010. if p[i]<>ep^[i-1] then
  1011. begin
  1012. found:=false;
  1013. break;
  1014. end;
  1015. if not found then
  1016. inc(ep);
  1017. end;
  1018. end;
  1019. if found then
  1020. getenv:=ep^+length(p)
  1021. else
  1022. getenv:=nil;
  1023. end;
  1024. {$endif}
  1025. procedure LoadEnvironment;
  1026. var
  1027. i,j : longint;
  1028. s,hs : string;
  1029. begin
  1030. s:=Getenv('HEAPTRC');
  1031. if pos('keepreleased',s)>0 then
  1032. keepreleased:=true;
  1033. if pos('disabled',s)>0 then
  1034. useheaptrace:=false;
  1035. if pos('nohalt',s)>0 then
  1036. haltonerror:=false;
  1037. i:=pos('log=',s);
  1038. if i>0 then
  1039. begin
  1040. hs:=copy(s,i+4,255);
  1041. j:=pos(' ',hs);
  1042. if j=0 then
  1043. j:=length(hs)+1;
  1044. delete(hs,j,255);
  1045. SetHeapTraceOutput(hs);
  1046. end;
  1047. end;
  1048. Initialization
  1049. LoadEnvironment;
  1050. { heaptrc can be disabled from the environment }
  1051. if useheaptrace then
  1052. TraceInit;
  1053. finalization
  1054. if useheaptrace then
  1055. TraceExit;
  1056. end.
  1057. {
  1058. $Log$
  1059. Revision 1.15 2001-07-29 13:43:57 peter
  1060. * m68k updates merged
  1061. Revision 1.14 2001/06/06 17:20:22 jonas
  1062. * fixed wrong typed constant procvars in preparation of my fix which will
  1063. disallow them in FPC mode (plus some other unmerged changes since
  1064. LAST_MERGE)
  1065. Revision 1.13 2001/04/23 18:25:44 peter
  1066. * m68k updates
  1067. Revision 1.12 2001/04/21 12:18:09 peter
  1068. * add nop after popa (merged)
  1069. Revision 1.11 2001/04/13 18:05:34 peter
  1070. * win32 getenv version
  1071. Revision 1.10 2001/04/13 01:18:08 peter
  1072. * always clear memory in getmem and freemem
  1073. Revision 1.8 2001/04/11 14:08:31 peter
  1074. * some small fixes to my previous commit
  1075. Revision 1.7 2001/04/11 12:34:50 peter
  1076. * extra info update so it can be always be set on/off
  1077. Revision 1.6 2000/12/16 15:57:17 jonas
  1078. * removed 64bit evaluations when range checking is on
  1079. Revision 1.5 2000/12/07 17:19:47 jonas
  1080. * new constant handling: from now on, hex constants >$7fffffff are
  1081. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1082. and became $ffffffff80000000), all constants in the longint range
  1083. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1084. are cardinals and the rest are int64's.
  1085. * added lots of longint typecast to prevent range check errors in the
  1086. compiler and rtl
  1087. * type casts of symbolic ordinal constants are now preserved
  1088. * fixed bug where the original resulttype wasn't restored correctly
  1089. after doing a 64bit rangecheck
  1090. Revision 1.4 2000/11/13 13:40:03 marco
  1091. * Renamefest
  1092. Revision 1.3 2000/08/24 09:01:07 jonas
  1093. * clear inoutres in traceexit before writing anything (to avoid an RTE
  1094. when writing the heaptrc output when a program didn't handle ioresult)
  1095. (merged from fixes branch)
  1096. Revision 1.2 2000/07/13 11:33:44 michael
  1097. + removed logs
  1098. }