heaptrc.pp 33 KB

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