heaptrc.pp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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. {$checkpointer off}
  12. unit heaptrc;
  13. interface
  14. {$inline on}
  15. {$ifdef FPC_HEAPTRC_EXTRA}
  16. {$define EXTRA}
  17. {$inline off}
  18. {$endif FPC_HEAPTRC_EXTRA}
  19. {$TYPEDADDRESS on}
  20. {$if defined(win32) or defined(wince)}
  21. {$define windows}
  22. {$endif}
  23. Procedure DumpHeap;
  24. Procedure DumpHeap(SkipIfNoLeaks : Boolean);
  25. { define EXTRA to add more
  26. tests :
  27. - keep all memory after release and
  28. check by CRC value if not changed after release
  29. WARNING this needs extremely much memory (PM) }
  30. type
  31. tFillExtraInfoProc = procedure(p : pointer);
  32. tdisplayextrainfoProc = procedure (var ptext : text;p : pointer);
  33. { Allows to add info pre memory block, see ppheap.pas of the compiler
  34. for example source }
  35. procedure SetHeapExtraInfo(size : ptruint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  36. { Redirection of the output to a file }
  37. procedure SetHeapTraceOutput(const name : string);overload;
  38. procedure SetHeapTraceOutput(var ATextOutput : Text);overload;
  39. procedure CheckPointer(p : pointer);
  40. const
  41. { tracing level
  42. splitted in two if memory is released !! }
  43. {$ifdef EXTRA}
  44. tracesize = 32;
  45. {$else EXTRA}
  46. tracesize = 16;
  47. {$endif EXTRA}
  48. { install heaptrc memorymanager }
  49. useheaptrace : boolean=true;
  50. { less checking }
  51. quicktrace : boolean=true;
  52. { calls halt() on error by default !! }
  53. HaltOnError : boolean = true;
  54. { Halt on exit if any memory was not freed }
  55. HaltOnNotReleased : boolean = false;
  56. { set this to true if you suspect that memory
  57. is freed several times }
  58. {$ifdef EXTRA}
  59. keepreleased : boolean=true;
  60. {$else EXTRA}
  61. keepreleased : boolean=false;
  62. {$endif EXTRA}
  63. { add a small footprint at the end of memory blocks, this
  64. can check for memory overwrites at the end of a block }
  65. add_tail : boolean = true;
  66. tail_size : longint = sizeof(ptruint);
  67. { put crc in sig
  68. this allows to test for writing into that part }
  69. usecrc : boolean = true;
  70. printleakedblock: boolean = false;
  71. printfaultyblock: boolean = false;
  72. maxprintedblocklength: integer = 128;
  73. GlobalSkipIfNoLeaks : Boolean = False;
  74. implementation
  75. const
  76. { allows to add custom info in heap_mem_info, this is the size that will
  77. be allocated for this information }
  78. extra_info_size : ptruint = 0;
  79. exact_info_size : ptruint = 0;
  80. EntryMemUsed : ptruint = 0;
  81. { function to fill this info up }
  82. fill_extra_info_proc : TFillExtraInfoProc = nil;
  83. display_extra_info_proc : TDisplayExtraInfoProc = nil;
  84. { indicates where the output will be redirected }
  85. { only set using environment variables }
  86. outputstr : shortstring = '';
  87. ReleaseSig = $AAAAAAAA;
  88. AllocateSig = $DEADBEEF;
  89. CheckSig = $12345678;
  90. type
  91. pheap_extra_info = ^theap_extra_info;
  92. theap_extra_info = record
  93. check : cardinal; { used to check if the procvar is still valid }
  94. fillproc : tfillextrainfoProc;
  95. displayproc : tdisplayextrainfoProc;
  96. data : record
  97. end;
  98. end;
  99. ppheap_mem_info = ^pheap_mem_info;
  100. pheap_mem_info = ^theap_mem_info;
  101. { warning the size of theap_mem_info
  102. must be a multiple of 8
  103. because otherwise you will get
  104. problems when releasing the usual memory part !!
  105. sizeof(theap_mem_info = 16+tracesize*4 so
  106. tracesize must be even !! PM }
  107. theap_mem_info = record
  108. previous,
  109. next : pheap_mem_info;
  110. todolist : ppheap_mem_info;
  111. todonext : pheap_mem_info;
  112. size : ptruint;
  113. sig : longword;
  114. {$ifdef EXTRA}
  115. release_sig : longword;
  116. prev_valid : pheap_mem_info;
  117. {$endif EXTRA}
  118. calls : array [1..tracesize] of codepointer;
  119. exact_info_size : word;
  120. extra_info_size : word;
  121. extra_info : pheap_extra_info;
  122. end;
  123. pheap_info = ^theap_info;
  124. theap_info = record
  125. {$ifdef EXTRA}
  126. heap_valid_first,
  127. heap_valid_last : pheap_mem_info;
  128. {$endif EXTRA}
  129. heap_mem_root : pheap_mem_info;
  130. heap_free_todo : pheap_mem_info;
  131. getmem_cnt,
  132. freemem_cnt : ptruint;
  133. getmem_size,
  134. freemem_size : ptruint;
  135. getmem8_size,
  136. freemem8_size : ptruint;
  137. error_in_heap : boolean;
  138. inside_trace_getmem : boolean;
  139. end;
  140. var
  141. useownfile, useowntextoutput : boolean;
  142. ownfile : text;
  143. {$ifdef EXTRA}
  144. error_file : text;
  145. {$endif EXTRA}
  146. main_orig_todolist: ppheap_mem_info;
  147. main_relo_todolist: ppheap_mem_info;
  148. orphaned_info: theap_info;
  149. todo_lock: trtlcriticalsection;
  150. textoutput : ^text;
  151. {$ifdef FPC_HAS_FEATURE_THREADING}
  152. threadvar
  153. {$else}
  154. var
  155. {$endif}
  156. heap_info: theap_info;
  157. {*****************************************************************************
  158. Crc 32
  159. *****************************************************************************}
  160. var
  161. Crc32Tbl : array[0..255] of longword;
  162. const
  163. Crc32Seed = $ffffffff;
  164. Crc32Pattern = $edb88320;
  165. procedure MakeCRC32Tbl;
  166. var
  167. crc : longword;
  168. i,n : byte;
  169. begin
  170. for i:=0 to 255 do
  171. begin
  172. crc:=i;
  173. for n:=1 to 8 do
  174. if odd(crc) then
  175. crc:=(crc shr 1) xor longword(CRC32Pattern)
  176. else
  177. crc:=crc shr 1;
  178. Crc32Tbl[i]:=crc;
  179. end;
  180. end;
  181. Function UpdateCrc32(InitCrc:longword;var InBuf;InLen:ptruint):longword;
  182. var
  183. i : ptruint;
  184. p : pchar;
  185. begin
  186. p:=@InBuf;
  187. for i:=1 to InLen do
  188. begin
  189. InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
  190. inc(p);
  191. end;
  192. UpdateCrc32:=InitCrc;
  193. end;
  194. Function calculate_sig(p : pheap_mem_info) : longword;
  195. var
  196. crc : longword;
  197. pl : pptruint;
  198. begin
  199. crc:=longword(CRC32Seed);
  200. crc:=UpdateCrc32(crc,p^.size,sizeof(ptruint));
  201. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(codepointer));
  202. if p^.extra_info_size>0 then
  203. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  204. if add_tail then
  205. begin
  206. { Check also 4 bytes just after allocation !! }
  207. pl:=pointer(p)+sizeof(theap_mem_info)+p^.size;
  208. crc:=UpdateCrc32(crc,pl^,tail_size);
  209. end;
  210. calculate_sig:=crc;
  211. end;
  212. {$ifdef EXTRA}
  213. Function calculate_release_sig(p : pheap_mem_info) : longword;
  214. var
  215. crc : longword;
  216. pl : pptruint;
  217. begin
  218. crc:=longword(CRC32Seed);
  219. crc:=UpdateCrc32(crc,p^.size,sizeof(ptruint));
  220. crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(codepointer));
  221. if p^.extra_info_size>0 then
  222. crc:=UpdateCrc32(crc,p^.extra_info^,p^.exact_info_size);
  223. { Check the whole of the whole allocation }
  224. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info);
  225. crc:=UpdateCrc32(crc,pl^,p^.size);
  226. { Check also 4 bytes just after allocation !! }
  227. if add_tail then
  228. begin
  229. { Check also 4 bytes just after allocation !! }
  230. pl:=pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size;
  231. crc:=UpdateCrc32(crc,pl^,tail_size);
  232. end;
  233. calculate_release_sig:=crc;
  234. end;
  235. {$endif EXTRA}
  236. {*****************************************************************************
  237. Helpers
  238. *****************************************************************************}
  239. function InternalFreeMemSize(loc_info: pheap_info; p: pointer; pp: pheap_mem_info;
  240. size: ptruint; release_todo_lock: boolean): ptruint; forward;
  241. function TraceFreeMem(p: pointer): ptruint; forward;
  242. procedure printhex(p : pointer; const size : PtrUInt; var ptext : text);
  243. var s: PtrUInt;
  244. i: Integer;
  245. begin
  246. s := size;
  247. if s > maxprintedblocklength then
  248. s := maxprintedblocklength;
  249. for i:=0 to s-1 do
  250. write(ptext, hexstr(pbyte(p + i)^,2));
  251. if size > maxprintedblocklength then
  252. writeln(ptext,'.. - ')
  253. else
  254. writeln(ptext, ' - ');
  255. for i:=0 to s-1 do
  256. if pchar(p + sizeof(theap_mem_info) + i)^ < ' ' then
  257. write(ptext, ' ')
  258. else
  259. write(ptext, pchar(p + i)^);
  260. if size > maxprintedblocklength then
  261. writeln(ptext,'..')
  262. else
  263. writeln(ptext);
  264. end;
  265. procedure call_stack(pp : pheap_mem_info;var ptext : text);
  266. var
  267. i : ptruint;
  268. begin
  269. writeln(ptext,'Call trace for block $',hexstr(pointer(pp)+sizeof(theap_mem_info)),' size ',pp^.size);
  270. if printleakedblock then
  271. begin
  272. write(ptext, 'Block content: ');
  273. printhex(pointer(pp) + sizeof(theap_mem_info), pp^.size, ptext);
  274. end;
  275. for i:=1 to tracesize do
  276. if pp^.calls[i]<>nil then
  277. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  278. { the check is done to be sure that the procvar is not overwritten }
  279. if assigned(pp^.extra_info) and
  280. (pp^.extra_info^.check=cardinal(CheckSig)) and
  281. assigned(pp^.extra_info^.displayproc) then
  282. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  283. end;
  284. procedure call_free_stack(pp : pheap_mem_info;var ptext : text);
  285. var
  286. i : ptruint;
  287. begin
  288. writeln(ptext,'Call trace for block at $',hexstr(pointer(pp)+sizeof(theap_mem_info)),' size ',pp^.size);
  289. for i:=1 to tracesize div 2 do
  290. if pp^.calls[i]<>nil then
  291. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  292. writeln(ptext,' was released at ');
  293. for i:=(tracesize div 2)+1 to tracesize do
  294. if pp^.calls[i]<>nil then
  295. writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
  296. { the check is done to be sure that the procvar is not overwritten }
  297. if assigned(pp^.extra_info) and
  298. (pp^.extra_info^.check=cardinal(CheckSig)) and
  299. assigned(pp^.extra_info^.displayproc) then
  300. pp^.extra_info^.displayproc(ptext,@pp^.extra_info^.data);
  301. end;
  302. procedure dump_already_free(p : pheap_mem_info;var ptext : text);
  303. begin
  304. Writeln(ptext,'Marked memory at $',HexStr(pointer(p)+sizeof(theap_mem_info)),' released');
  305. call_free_stack(p,ptext);
  306. Writeln(ptext,'freed again at');
  307. dump_stack(ptext,1);
  308. end;
  309. procedure dump_error(p : pheap_mem_info;var ptext : text);
  310. begin
  311. Writeln(ptext,'Marked memory at $',HexStr(pointer(p)+sizeof(theap_mem_info)),' invalid');
  312. Writeln(ptext,'Wrong signature $',hexstr(p^.sig,8),' instead of ',hexstr(calculate_sig(p),8));
  313. if printfaultyblock then
  314. begin
  315. write(ptext, 'Block content: ');
  316. printhex(pointer(p) + sizeof(theap_mem_info), p^.size, ptext);
  317. end;
  318. dump_stack(ptext,1);
  319. end;
  320. function released_modified(p : pheap_mem_info;var ptext : text) : boolean;
  321. var pl : pdword;
  322. pb : pbyte;
  323. i : longint;
  324. begin
  325. released_modified:=false;
  326. { Check tail_size bytes just after allocation !! }
  327. pl:=pointer(p)+sizeof(theap_mem_info)+p^.size;
  328. pb:=pointer(p)+sizeof(theap_mem_info);
  329. for i:=0 to p^.size-1 do
  330. if pb[i]<>$F0 then
  331. begin
  332. Writeln(ptext,'offset',i,':$',hexstr(i,2*sizeof(pointer)),'"',hexstr(pb[i],2),'"');
  333. released_modified:=true;
  334. end;
  335. for i:=1 to (tail_size div sizeof(dword)) do
  336. begin
  337. if unaligned(pl^) <> AllocateSig then
  338. begin
  339. released_modified:=true;
  340. writeln(ptext,'Tail modified after release at pos ',i*sizeof(ptruint));
  341. printhex(pointer(p)+p^.extra_info_size+sizeof(theap_mem_info)+p^.size,tail_size,ptext);
  342. break;
  343. end;
  344. inc(pointer(pl),sizeof(dword));
  345. end;
  346. if released_modified then
  347. begin
  348. dump_already_free(p,ptext);
  349. if @stderr<>@ptext then
  350. dump_already_free(p,stderr);
  351. end;
  352. end;
  353. {$ifdef EXTRA}
  354. procedure dump_change_after(p : pheap_mem_info;var ptext : text);
  355. var pp : pchar;
  356. i : ptruint;
  357. begin
  358. Writeln(ptext,'Marked memory at $',HexStr(pointer(p)+sizeof(theap_mem_info)),' invalid');
  359. Writeln(ptext,'Wrong release CRC $',hexstr(p^.release_sig,8),' instead of ',hexstr(calculate_release_sig(p),8));
  360. Writeln(ptext,'This memory was changed after call to freemem !');
  361. call_free_stack(p,ptext);
  362. pp:=pointer(p)+sizeof(theap_mem_info);
  363. for i:=0 to p^.size-1 do
  364. if byte(pp[i])<>$F0 then
  365. Writeln(ptext,'offset',i,':$',hexstr(i,2*sizeof(pointer)),'"',pp[i],'"');
  366. end;
  367. {$endif EXTRA}
  368. procedure dump_wrong_size(p : pheap_mem_info;size : ptruint;var ptext : text);
  369. begin
  370. Writeln(ptext,'Marked memory at $',HexStr(pointer(p)+sizeof(theap_mem_info)),' invalid');
  371. Writeln(ptext,'Wrong size : ',p^.size,' allocated ',size,' freed');
  372. dump_stack(ptext,1);
  373. { the check is done to be sure that the procvar is not overwritten }
  374. if assigned(p^.extra_info) and
  375. (p^.extra_info^.check=cardinal(CheckSig)) and
  376. assigned(p^.extra_info^.displayproc) then
  377. p^.extra_info^.displayproc(ptext,@p^.extra_info^.data);
  378. call_stack(p,ptext);
  379. end;
  380. function is_in_getmem_list (loc_info: pheap_info; p : pheap_mem_info) : boolean;
  381. var
  382. i : ptruint;
  383. pp : pheap_mem_info;
  384. begin
  385. is_in_getmem_list:=false;
  386. pp:=loc_info^.heap_mem_root;
  387. i:=0;
  388. while pp<>nil do
  389. begin
  390. if ((pp^.sig<>longword(AllocateSig)) or usecrc) and
  391. ((pp^.sig<>calculate_sig(pp)) or not usecrc) and
  392. (pp^.sig <>longword(ReleaseSig)) then
  393. begin
  394. if useownfile then
  395. writeln(ownfile,'error in linked list of heap_mem_info')
  396. else
  397. writeln(textoutput^,'error in linked list of heap_mem_info');
  398. RunError(204);
  399. end;
  400. if pp=p then
  401. is_in_getmem_list:=true;
  402. pp:=pp^.previous;
  403. inc(i);
  404. if i>loc_info^.getmem_cnt-loc_info^.freemem_cnt then
  405. if useownfile then
  406. writeln(ownfile,'error in linked list of heap_mem_info')
  407. else
  408. writeln(textoutput^,'error in linked list of heap_mem_info');
  409. end;
  410. end;
  411. procedure finish_heap_free_todo_list(loc_info: pheap_info);
  412. var
  413. bp: pointer;
  414. pp: pheap_mem_info;
  415. list: ppheap_mem_info;
  416. begin
  417. list := @loc_info^.heap_free_todo;
  418. repeat
  419. pp := list^;
  420. list^ := list^^.todonext;
  421. bp := pointer(pp)+sizeof(theap_mem_info);
  422. InternalFreeMemSize(loc_info,bp,pp,pp^.size,false);
  423. until list^ = nil;
  424. end;
  425. procedure try_finish_heap_free_todo_list(loc_info: pheap_info);
  426. begin
  427. if loc_info^.heap_free_todo <> nil then
  428. begin
  429. {$ifdef FPC_HAS_FEATURE_THREADING}
  430. entercriticalsection(todo_lock);
  431. {$endif}
  432. finish_heap_free_todo_list(loc_info);
  433. {$ifdef FPC_HAS_FEATURE_THREADING}
  434. leavecriticalsection(todo_lock);
  435. {$endif}
  436. end;
  437. end;
  438. {*****************************************************************************
  439. TraceGetMem
  440. *****************************************************************************}
  441. Function TraceGetMem(size:ptruint):pointer;
  442. var
  443. i, allocsize : ptruint;
  444. pl : pdword;
  445. p : pointer;
  446. pp : pheap_mem_info;
  447. loc_info: pheap_info;
  448. begin
  449. loc_info := @heap_info;
  450. try_finish_heap_free_todo_list(loc_info);
  451. { Do the real GetMem, but alloc also for the info block }
  452. {$ifdef cpuarm}
  453. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+extra_info_size;
  454. {$else cpuarm}
  455. allocsize:=size+sizeof(theap_mem_info)+extra_info_size;
  456. {$endif cpuarm}
  457. if add_tail then
  458. inc(allocsize,tail_size);
  459. { if ReturnNilIfGrowHeapFails is true
  460. SysGetMem can return nil }
  461. p:=SysGetMem(allocsize);
  462. if (p=nil) then
  463. begin
  464. TraceGetMem:=nil;
  465. exit;
  466. end;
  467. pp:=pheap_mem_info(p);
  468. inc(p,sizeof(theap_mem_info));
  469. { Update getmem_size and getmem8_size only after successful call
  470. to SysGetMem }
  471. inc(loc_info^.getmem_size,size);
  472. inc(loc_info^.getmem8_size,(size+7) and not 7);
  473. { Create the info block }
  474. pp^.sig:=longword(AllocateSig);
  475. pp^.todolist:=@loc_info^.heap_free_todo;
  476. pp^.todonext:=nil;
  477. pp^.size:=size;
  478. pp^.extra_info_size:=extra_info_size;
  479. pp^.exact_info_size:=exact_info_size;
  480. fillchar(pp^.calls[1],sizeof(pp^.calls),#0);
  481. {
  482. the end of the block contains:
  483. <tail> 4 bytes
  484. <extra_info> X bytes
  485. }
  486. if extra_info_size>0 then
  487. begin
  488. pp^.extra_info:=pointer(pp)+allocsize-extra_info_size;
  489. fillchar(pp^.extra_info^,extra_info_size,0);
  490. pp^.extra_info^.check:=cardinal(CheckSig);
  491. pp^.extra_info^.fillproc:=fill_extra_info_proc;
  492. pp^.extra_info^.displayproc:=display_extra_info_proc;
  493. if assigned(fill_extra_info_proc) then
  494. begin
  495. loc_info^.inside_trace_getmem:=true;
  496. fill_extra_info_proc(@pp^.extra_info^.data);
  497. loc_info^.inside_trace_getmem:=false;
  498. end;
  499. end
  500. else
  501. pp^.extra_info:=nil;
  502. if add_tail then
  503. begin
  504. { Calculate position from start because of arm
  505. specific alignment }
  506. pl:=pointer(pp)+sizeof(theap_mem_info)+pp^.size;
  507. for i:=1 to tail_size div sizeof(dword) do
  508. begin
  509. unaligned(pl^):=dword(AllocateSig);
  510. inc(pointer(pl),sizeof(dword));
  511. end;
  512. end;
  513. { clear the memory }
  514. fillchar(p^,size,#255);
  515. { retrieve backtrace info }
  516. CaptureBacktrace(1,tracesize,@pp^.calls[1]);
  517. { insert in the linked list }
  518. if loc_info^.heap_mem_root<>nil then
  519. loc_info^.heap_mem_root^.next:=pp;
  520. pp^.previous:=loc_info^.heap_mem_root;
  521. pp^.next:=nil;
  522. {$ifdef EXTRA}
  523. pp^.prev_valid:=loc_info^.heap_valid_last;
  524. loc_info^.heap_valid_last:=pp;
  525. if not assigned(loc_info^.heap_valid_first) then
  526. loc_info^.heap_valid_first:=pp;
  527. {$endif EXTRA}
  528. loc_info^.heap_mem_root:=pp;
  529. { must be changed before fill_extra_info is called
  530. because checkpointer can be called from within
  531. fill_extra_info PM }
  532. inc(loc_info^.getmem_cnt);
  533. { update the signature }
  534. if usecrc then
  535. pp^.sig:=calculate_sig(pp);
  536. TraceGetmem:=p;
  537. end;
  538. {*****************************************************************************
  539. TraceFreeMem
  540. *****************************************************************************}
  541. function CheckFreeMemSize(loc_info: pheap_info; pp: pheap_mem_info;
  542. size, ppsize: ptruint): boolean; inline;
  543. var
  544. ptext : ^text;
  545. {$ifdef EXTRA}
  546. pp2 : pheap_mem_info;
  547. {$endif}
  548. begin
  549. if useownfile then
  550. ptext:=@ownfile
  551. else
  552. ptext:=textoutput;
  553. inc(loc_info^.freemem_size,size);
  554. inc(loc_info^.freemem8_size,(size+7) and not 7);
  555. if not quicktrace then
  556. begin
  557. if not(is_in_getmem_list(loc_info, pp)) then
  558. RunError(204);
  559. end;
  560. if (pp^.sig=longword(ReleaseSig)) then
  561. begin
  562. loc_info^.error_in_heap:=true;
  563. dump_already_free(pp,ptext^);
  564. if haltonerror then halt(1);
  565. end
  566. else if ((pp^.sig<>longword(AllocateSig)) or usecrc) and
  567. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  568. begin
  569. loc_info^.error_in_heap:=true;
  570. dump_error(pp,ptext^);
  571. {$ifdef EXTRA}
  572. dump_error(pp,error_file);
  573. {$endif EXTRA}
  574. { don't release anything in this case !! }
  575. if haltonerror then halt(1);
  576. exit;
  577. end
  578. else if pp^.size<>size then
  579. begin
  580. loc_info^.error_in_heap:=true;
  581. dump_wrong_size(pp,size,ptext^);
  582. {$ifdef EXTRA}
  583. dump_wrong_size(pp,size,error_file);
  584. {$endif EXTRA}
  585. if haltonerror then halt(1);
  586. { don't release anything in this case !! }
  587. exit;
  588. end;
  589. { now it is released !! }
  590. pp^.sig:=longword(ReleaseSig);
  591. if not keepreleased then
  592. begin
  593. if pp^.next<>nil then
  594. pp^.next^.previous:=pp^.previous;
  595. if pp^.previous<>nil then
  596. pp^.previous^.next:=pp^.next;
  597. if pp=loc_info^.heap_mem_root then
  598. loc_info^.heap_mem_root:=loc_info^.heap_mem_root^.previous;
  599. end
  600. else
  601. CaptureBacktrace(1,(tracesize div 2)-1,@pp^.calls[(tracesize div 2)+1]);
  602. inc(loc_info^.freemem_cnt);
  603. { clear the memory, $F0 will lead to GFP if used as pointer ! }
  604. fillchar((pointer(pp)+sizeof(theap_mem_info))^,size,#240);
  605. { this way we keep all info about all released memory !! }
  606. if keepreleased then
  607. begin
  608. {$ifdef EXTRA}
  609. { We want to check if the memory was changed after release !! }
  610. pp^.release_sig:=calculate_release_sig(pp);
  611. if pp=loc_info^.heap_valid_last then
  612. begin
  613. loc_info^.heap_valid_last:=pp^.prev_valid;
  614. if pp=loc_info^.heap_valid_first then
  615. loc_info^.heap_valid_first:=nil;
  616. exit(false);
  617. end;
  618. pp2:=loc_info^.heap_valid_last;
  619. while assigned(pp2) do
  620. begin
  621. if pp2^.prev_valid=pp then
  622. begin
  623. pp2^.prev_valid:=pp^.prev_valid;
  624. if pp=loc_info^.heap_valid_first then
  625. loc_info^.heap_valid_first:=pp2;
  626. exit(false);
  627. end
  628. else
  629. pp2:=pp2^.prev_valid;
  630. end;
  631. {$endif EXTRA}
  632. exit(false);
  633. end;
  634. CheckFreeMemSize:=true;
  635. end;
  636. function InternalFreeMemSize(loc_info: pheap_info; p: pointer; pp: pheap_mem_info;
  637. size: ptruint; release_todo_lock: boolean): ptruint;
  638. var
  639. i,ppsize : ptruint;
  640. extra_size: ptruint;
  641. release_mem: boolean;
  642. begin
  643. { save old values }
  644. extra_size:=pp^.extra_info_size;
  645. ppsize:= size+sizeof(theap_mem_info)+pp^.extra_info_size;
  646. if add_tail then
  647. inc(ppsize,tail_size);
  648. { do various checking }
  649. release_mem := CheckFreeMemSize(loc_info, pp, size, ppsize);
  650. {$ifdef FPC_HAS_FEATURE_THREADING}
  651. if release_todo_lock then
  652. leavecriticalsection(todo_lock);
  653. {$endif}
  654. if release_mem then
  655. begin
  656. { release the normal memory at least }
  657. i:=SysFreeMemSize(pp,ppsize);
  658. { return the correct size }
  659. dec(i,sizeof(theap_mem_info)+extra_size);
  660. if add_tail then
  661. dec(i,tail_size);
  662. InternalFreeMemSize:=i;
  663. end else
  664. InternalFreeMemSize:=size;
  665. end;
  666. function TraceFreeMemSize(p:pointer;size:ptruint):ptruint;
  667. var
  668. loc_info: pheap_info;
  669. pp: pheap_mem_info;
  670. release_lock: boolean;
  671. begin
  672. if p=nil then
  673. begin
  674. TraceFreeMemSize:=0;
  675. exit;
  676. end;
  677. loc_info:=@heap_info;
  678. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  679. release_lock:=false;
  680. if @loc_info^.heap_free_todo <> pp^.todolist then
  681. begin
  682. if pp^.todolist = main_orig_todolist then
  683. pp^.todolist := main_relo_todolist;
  684. {$ifdef FPC_HAS_FEATURE_THREADING}
  685. entercriticalsection(todo_lock);
  686. {$endif}
  687. release_lock:=true;
  688. if pp^.todolist = @orphaned_info.heap_free_todo then
  689. begin
  690. loc_info := @orphaned_info;
  691. end else
  692. if pp^.todolist <> @loc_info^.heap_free_todo then
  693. begin
  694. { allocated in different heap, push to that todolist }
  695. pp^.todonext := pp^.todolist^;
  696. pp^.todolist^ := pp;
  697. TraceFreeMemSize := pp^.size;
  698. {$ifdef FPC_HAS_FEATURE_THREADING}
  699. leavecriticalsection(todo_lock);
  700. {$endif}
  701. exit;
  702. end;
  703. end;
  704. TraceFreeMemSize:=InternalFreeMemSize(loc_info,p,pp,size,release_lock);
  705. end;
  706. function TraceMemSize(p:pointer):ptruint;
  707. var
  708. pp : pheap_mem_info;
  709. begin
  710. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  711. TraceMemSize:=pp^.size;
  712. end;
  713. function TraceFreeMem(p:pointer):ptruint;
  714. var
  715. l : ptruint;
  716. pp : pheap_mem_info;
  717. begin
  718. if p=nil then
  719. begin
  720. TraceFreeMem:=0;
  721. exit;
  722. end;
  723. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  724. l:=SysMemSize(pp);
  725. dec(l,sizeof(theap_mem_info)+pp^.extra_info_size);
  726. if add_tail then
  727. dec(l,tail_size);
  728. { this can never happend normaly }
  729. if pp^.size>l then
  730. begin
  731. if useownfile then
  732. dump_wrong_size(pp,l,ownfile)
  733. else
  734. dump_wrong_size(pp,l,textoutput^);
  735. {$ifdef EXTRA}
  736. dump_wrong_size(pp,l,error_file);
  737. {$endif EXTRA}
  738. end;
  739. TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
  740. end;
  741. {*****************************************************************************
  742. ReAllocMem
  743. *****************************************************************************}
  744. function TraceReAllocMem(var p:pointer;size:ptruint):Pointer;
  745. var
  746. newP: pointer;
  747. i, allocsize,
  748. movesize : ptruint;
  749. pl : pdword;
  750. pp : pheap_mem_info;
  751. oldsize,
  752. oldextrasize,
  753. oldexactsize : ptruint;
  754. old_fill_extra_info_proc : tfillextrainfoproc;
  755. old_display_extra_info_proc : tdisplayextrainfoproc;
  756. loc_info: pheap_info;
  757. begin
  758. { Free block? }
  759. if size=0 then
  760. begin
  761. if p<>nil then
  762. TraceFreeMem(p);
  763. p:=nil;
  764. TraceReallocMem:=P;
  765. exit;
  766. end;
  767. { Allocate a new block? }
  768. if p=nil then
  769. begin
  770. p:=TraceGetMem(size);
  771. TraceReallocMem:=P;
  772. exit;
  773. end;
  774. { Resize block }
  775. loc_info:=@heap_info;
  776. pp:=pheap_mem_info(p-sizeof(theap_mem_info));
  777. { test block }
  778. if ((pp^.sig<>longword(AllocateSig)) or usecrc) and
  779. ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
  780. begin
  781. loc_info^.error_in_heap:=true;
  782. if useownfile then
  783. dump_error(pp,ownfile)
  784. else
  785. dump_error(pp,textoutput^);
  786. {$ifdef EXTRA}
  787. dump_error(pp,error_file);
  788. {$endif EXTRA}
  789. { don't release anything in this case !! }
  790. if haltonerror then halt(1);
  791. exit;
  792. end;
  793. { save info }
  794. oldsize:=pp^.size;
  795. oldextrasize:=pp^.extra_info_size;
  796. oldexactsize:=pp^.exact_info_size;
  797. if pp^.extra_info_size>0 then
  798. begin
  799. old_fill_extra_info_proc:=pp^.extra_info^.fillproc;
  800. old_display_extra_info_proc:=pp^.extra_info^.displayproc;
  801. end;
  802. { Do the real ReAllocMem, but alloc also for the info block }
  803. {$ifdef cpuarm}
  804. allocsize:=(size + 3) and not 3+sizeof(theap_mem_info)+pp^.extra_info_size;
  805. {$else cpuarm}
  806. allocsize:=size+sizeof(theap_mem_info)+pp^.extra_info_size;
  807. {$endif cpuarm}
  808. if add_tail then
  809. inc(allocsize,tail_size);
  810. { Try to resize the block, if not possible we need to do a
  811. getmem, move data, freemem }
  812. if not SysTryResizeMem(pp,allocsize) then
  813. begin
  814. { get a new block }
  815. newP := TraceGetMem(size);
  816. { move the data }
  817. if newP <> nil then
  818. begin
  819. movesize:=TraceMemSize(p);
  820. {if the old size is larger than the new size,
  821. move only the new size}
  822. if movesize>size then
  823. movesize:=size;
  824. move(p^,newP^,movesize);
  825. end;
  826. { release p }
  827. traceFreeMem(p);
  828. { return the new pointer }
  829. p:=newp;
  830. traceReAllocMem := newp;
  831. exit;
  832. end;
  833. { Recreate the info block }
  834. pp^.sig:=longword(AllocateSig);
  835. pp^.size:=size;
  836. pp^.extra_info_size:=oldextrasize;
  837. pp^.exact_info_size:=oldexactsize;
  838. { add the new extra_info and tail }
  839. if pp^.extra_info_size>0 then
  840. begin
  841. pp^.extra_info:=pointer(pp)+allocsize-pp^.extra_info_size;
  842. fillchar(pp^.extra_info^,extra_info_size,0);
  843. pp^.extra_info^.check:=cardinal(CheckSig);
  844. pp^.extra_info^.fillproc:=old_fill_extra_info_proc;
  845. pp^.extra_info^.displayproc:=old_display_extra_info_proc;
  846. if assigned(pp^.extra_info^.fillproc) then
  847. pp^.extra_info^.fillproc(@pp^.extra_info^.data);
  848. end
  849. else
  850. pp^.extra_info:=nil;
  851. if add_tail then
  852. begin
  853. { Calculate position from start because of arm
  854. specific alignment }
  855. pl:=pointer(pp)+sizeof(theap_mem_info)+pp^.size;
  856. for i:=1 to tail_size div sizeof(dword) do
  857. begin
  858. unaligned(pl^):=dword(AllocateSig);
  859. inc(pointer(pl),sizeof(dword));
  860. end;
  861. end;
  862. { adjust like a freemem and then a getmem, so you get correct
  863. results in the summary display }
  864. inc(loc_info^.freemem_size,oldsize);
  865. inc(loc_info^.freemem8_size,(oldsize+7) and not 7);
  866. inc(loc_info^.getmem_size,size);
  867. inc(loc_info^.getmem8_size,(size+7) and not 7);
  868. { generate new backtrace }
  869. CaptureBacktrace(1,tracesize,@pp^.calls[1]);
  870. { regenerate signature }
  871. if usecrc then
  872. pp^.sig:=calculate_sig(pp);
  873. { return the pointer }
  874. p:=pointer(pp)+sizeof(theap_mem_info);
  875. TraceReAllocmem:=p;
  876. end;
  877. {*****************************************************************************
  878. Check pointer
  879. *****************************************************************************}
  880. {$ifndef Unix}
  881. {$S-}
  882. {$endif}
  883. {$ifdef go32v2}
  884. var
  885. __stklen : longword;external name '__stklen';
  886. __stkbottom : longword;external name '__stkbottom';
  887. ebss : longword; external name 'end';
  888. {$endif go32v2}
  889. {$ifdef linux}
  890. var
  891. etext: ptruint; external name '_etext';
  892. edata : ptruint; external name '_edata';
  893. eend : ptruint; external name '_end';
  894. {$endif}
  895. {$ifdef freebsd}
  896. var
  897. text_start: ptruint; external name '__executable_start';
  898. etext: ptruint; external name '_etext';
  899. eend : ptruint; external name '_end';
  900. {$endif}
  901. {$ifdef os2}
  902. (* Currently still EMX based - possibly to be changed in the future. *)
  903. var
  904. etext: ptruint; external name '_etext';
  905. edata : ptruint; external name '_edata';
  906. eend : ptruint; external name '_end';
  907. {$endif}
  908. {$ifdef windows}
  909. var
  910. sdata : ptruint; external name '__data_start__';
  911. edata : ptruint; external name '__data_end__';
  912. sbss : ptruint; external name '__bss_start__';
  913. ebss : ptruint; external name '__bss_end__';
  914. TLSKey : PDWord; external name '_FPC_TlsKey';
  915. TLSSize : DWord; external name '_FPC_TlsSize';
  916. function TlsGetValue(dwTlsIndex : DWord) : pointer;
  917. {$ifdef wince}cdecl{$else}stdcall{$endif};external KernelDLL name 'TlsGetValue';
  918. {$endif}
  919. {$ifdef BEOS}
  920. const
  921. B_ERROR = -1;
  922. type
  923. area_id = Longint;
  924. function area_for(addr : Pointer) : area_id;
  925. cdecl; external 'root' name 'area_for';
  926. {$endif BEOS}
  927. procedure CheckPointer(p : pointer); [public, alias : 'FPC_CHECKPOINTER'];
  928. var
  929. i : ptruint;
  930. pp : pheap_mem_info;
  931. loc_info: pheap_info;
  932. {$ifdef go32v2}
  933. get_ebp,stack_top : longword;
  934. bss_end : longword;
  935. {$endif go32v2}
  936. {$ifdef windows}
  937. datap : pointer;
  938. {$endif windows}
  939. ptext : ^text;
  940. begin
  941. if p=nil then
  942. runerror(204);
  943. i:=0;
  944. loc_info:=@heap_info;
  945. if useownfile then
  946. ptext:=@ownfile
  947. else
  948. ptext:=textoutput;
  949. {$ifdef go32v2}
  950. if ptruint(p)<$1000 then
  951. runerror(216);
  952. asm
  953. movl %ebp,get_ebp
  954. leal ebss,%eax
  955. movl %eax,bss_end
  956. end;
  957. stack_top:=__stkbottom+__stklen;
  958. { allow all between start of code and end of bss }
  959. if ptruint(p)<=bss_end then
  960. exit;
  961. { stack can be above heap !! }
  962. if (ptruint(p)>=get_ebp) and (ptruint(p)<=stack_top) then
  963. exit;
  964. {$endif go32v2}
  965. { I don't know where the stack is in other OS !! }
  966. {$ifdef windows}
  967. { inside stack ? }
  968. if (ptruint(p)>ptruint(get_frame)) and
  969. (p<StackTop) then
  970. exit;
  971. { inside data, rdata ... bss }
  972. if (ptruint(p)>=ptruint(@sdata)) and (ptruint(p)<ptruint(@ebss)) then
  973. exit;
  974. { is program multi-threaded and p inside Threadvar range? }
  975. if TlsKey^<>-1 then
  976. begin
  977. datap:=TlsGetValue(tlskey^);
  978. if ((ptruint(p)>=ptruint(datap)) and
  979. (ptruint(p)<ptruint(datap)+TlsSize)) then
  980. exit;
  981. end;
  982. {$endif windows}
  983. {$IFDEF OS2}
  984. { inside stack ? }
  985. if (PtrUInt (P) > PtrUInt (Get_Frame)) and
  986. (PtrUInt (P) < PtrUInt (StackTop)) then
  987. exit;
  988. { inside data or bss ? }
  989. if (PtrUInt (P) >= PtrUInt (@etext)) and (PtrUInt (P) < PtrUInt (@eend)) then
  990. exit;
  991. {$ENDIF OS2}
  992. {$ifdef linux}
  993. { inside stack ? }
  994. if (ptruint(p)>ptruint(get_frame)) and
  995. (ptruint(p)<ptruint(StackTop)) then
  996. exit;
  997. { inside data or bss ? }
  998. if (ptruint(p)>=ptruint(@etext)) and (ptruint(p)<ptruint(@eend)) then
  999. exit;
  1000. {$endif linux}
  1001. {$ifdef freebsd}
  1002. { inside stack ? }
  1003. if (ptruint(p)>ptruint(get_frame)) and
  1004. (ptruint(p)<ptruint(StackTop)) then
  1005. exit;
  1006. { inside data or bss ? }
  1007. if (ptruint(p)>=ptruint(@text_start)) and (ptruint(p)<ptruint(@eend)) then
  1008. exit;
  1009. {$endif linux}
  1010. {$ifdef morphos}
  1011. { inside stack ? }
  1012. if (ptruint(p)<ptruint(StackTop)) and (ptruint(p)>ptruint(StackBottom)) then
  1013. exit;
  1014. { inside data or bss ? }
  1015. {$WARNING data and bss checking missing }
  1016. {$endif morphos}
  1017. {$ifdef darwin}
  1018. {$warning No checkpointer support yet for Darwin}
  1019. exit;
  1020. {$endif}
  1021. {$ifdef BEOS}
  1022. // if we find the address in a known area in our current process,
  1023. // then it is a valid one
  1024. if area_for(p) <> B_ERROR then
  1025. exit;
  1026. {$endif BEOS}
  1027. { first try valid list faster }
  1028. {$ifdef EXTRA}
  1029. pp:=loc_info^.heap_valid_last;
  1030. while pp<>nil do
  1031. begin
  1032. { inside this valid block ! }
  1033. { we can be changing the extrainfo !! }
  1034. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info){+extra_info_size}) and
  1035. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
  1036. begin
  1037. { check allocated block }
  1038. if ((pp^.sig=longword(AllocateSig)) and not usecrc) or
  1039. ((pp^.sig=calculate_sig(pp)) and usecrc) or
  1040. { special case of the fill_extra_info call }
  1041. ((pp=loc_info^.heap_valid_last) and usecrc and (pp^.sig=longword(AllocateSig))
  1042. and loc_info^.inside_trace_getmem) then
  1043. exit
  1044. else
  1045. begin
  1046. writeln(ptext^,'corrupted heap_mem_info');
  1047. dump_error(pp,ptext^);
  1048. halt(1);
  1049. end;
  1050. end
  1051. else
  1052. pp:=pp^.prev_valid;
  1053. inc(i);
  1054. if i>loc_info^.getmem_cnt-loc_info^.freemem_cnt then
  1055. begin
  1056. writeln(ptext^,'error in linked list of heap_mem_info');
  1057. halt(1);
  1058. end;
  1059. end;
  1060. i:=0;
  1061. {$endif EXTRA}
  1062. pp:=loc_info^.heap_mem_root;
  1063. while pp<>nil do
  1064. begin
  1065. { inside this block ! }
  1066. if (ptruint(p)>=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)) and
  1067. (ptruint(p)<=ptruint(pp)+sizeof(theap_mem_info)+ptruint(extra_info_size)+ptruint(pp^.size)) then
  1068. { allocated block }
  1069. if ((pp^.sig=longword(AllocateSig)) and not usecrc) or
  1070. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  1071. exit
  1072. else
  1073. begin
  1074. writeln(ptext^,'pointer $',hexstr(p),' points into invalid memory block');
  1075. dump_error(pp,ptext^);
  1076. runerror(204);
  1077. end;
  1078. pp:=pp^.previous;
  1079. inc(i);
  1080. if i>loc_info^.getmem_cnt then
  1081. begin
  1082. writeln(ptext^,'error in linked list of heap_mem_info');
  1083. halt(1);
  1084. end;
  1085. end;
  1086. writeln(ptext^,'pointer $',hexstr(p),' does not point to valid memory block');
  1087. dump_stack(ptext^,1);
  1088. runerror(204);
  1089. end;
  1090. {*****************************************************************************
  1091. Dump Heap
  1092. *****************************************************************************}
  1093. procedure dumpheap;
  1094. begin
  1095. DumpHeap(GlobalSkipIfNoLeaks);
  1096. end;
  1097. procedure dumpheap(SkipIfNoLeaks : Boolean);
  1098. var
  1099. pp : pheap_mem_info;
  1100. i : ptrint;
  1101. ExpectedHeapFree : ptruint;
  1102. status : TFPCHeapStatus;
  1103. ptext : ^text;
  1104. loc_info: pheap_info;
  1105. begin
  1106. loc_info:=@heap_info;
  1107. if useownfile then
  1108. ptext:=@ownfile
  1109. else
  1110. ptext:=textoutput;
  1111. pp:=loc_info^.heap_mem_root;
  1112. if ((loc_info^.getmem_size-loc_info^.freemem_size)=0) and SkipIfNoLeaks then
  1113. exit;
  1114. Writeln(ptext^,'Heap dump by heaptrc unit of '+ParamStr(0));
  1115. Writeln(ptext^,loc_info^.getmem_cnt, ' memory blocks allocated : ',
  1116. loc_info^.getmem_size,'/',loc_info^.getmem8_size);
  1117. Writeln(ptext^,loc_info^.freemem_cnt,' memory blocks freed : ',
  1118. loc_info^.freemem_size,'/',loc_info^.freemem8_size);
  1119. Writeln(ptext^,loc_info^.getmem_cnt-loc_info^.freemem_cnt,
  1120. ' unfreed memory blocks : ',loc_info^.getmem_size-loc_info^.freemem_size);
  1121. status:=SysGetFPCHeapStatus;
  1122. Write(ptext^,'True heap size : ',status.CurrHeapSize);
  1123. if EntryMemUsed > 0 then
  1124. Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
  1125. else
  1126. Writeln(ptext^);
  1127. Writeln(ptext^,'True free heap : ',status.CurrHeapFree);
  1128. ExpectedHeapFree:=status.CurrHeapSize
  1129. -(loc_info^.getmem8_size-loc_info^.freemem8_size)
  1130. -(loc_info^.getmem_cnt-loc_info^.freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)
  1131. -EntryMemUsed;
  1132. If ExpectedHeapFree<>status.CurrHeapFree then
  1133. Writeln(ptext^,'Should be : ',ExpectedHeapFree);
  1134. i:=loc_info^.getmem_cnt-loc_info^.freemem_cnt;
  1135. while pp<>nil do
  1136. begin
  1137. if i<0 then
  1138. begin
  1139. Writeln(ptext^,'Error in heap memory list');
  1140. Writeln(ptext^,'More memory blocks than expected');
  1141. exit;
  1142. end;
  1143. if ((pp^.sig=longword(AllocateSig)) and not usecrc) or
  1144. ((pp^.sig=calculate_sig(pp)) and usecrc) then
  1145. begin
  1146. { this one was not released !! }
  1147. if exitcode<>203 then
  1148. call_stack(pp,ptext^);
  1149. dec(i);
  1150. end
  1151. else if pp^.sig<>longword(ReleaseSig) then
  1152. begin
  1153. dump_error(pp,ptext^);
  1154. if @stderr<>ptext then
  1155. dump_error(pp,stderr);
  1156. {$ifdef EXTRA}
  1157. dump_error(pp,error_file);
  1158. {$endif EXTRA}
  1159. loc_info^.error_in_heap:=true;
  1160. end
  1161. {$ifdef EXTRA}
  1162. else if pp^.release_sig<>calculate_release_sig(pp) then
  1163. begin
  1164. dump_change_after(pp,ptext^);
  1165. dump_change_after(pp,error_file);
  1166. loc_info^.error_in_heap:=true;
  1167. end
  1168. {$else not EXTRA}
  1169. else
  1170. begin
  1171. if released_modified(pp,ptext^) then
  1172. exitcode:=203;
  1173. end;
  1174. {$endif EXTRA}
  1175. ;
  1176. pp:=pp^.previous;
  1177. end;
  1178. if HaltOnNotReleased and (loc_info^.getmem_cnt<>loc_info^.freemem_cnt) then
  1179. exitcode:=203;
  1180. end;
  1181. {*****************************************************************************
  1182. AllocMem
  1183. *****************************************************************************}
  1184. function TraceAllocMem(size:ptruint):Pointer;
  1185. begin
  1186. TraceAllocMem:=SysAllocMem(size);
  1187. end;
  1188. {*****************************************************************************
  1189. No specific tracing calls
  1190. *****************************************************************************}
  1191. procedure TraceInitThread;
  1192. var
  1193. loc_info: pheap_info;
  1194. begin
  1195. loc_info := @heap_info;
  1196. {$ifdef EXTRA}
  1197. loc_info^.heap_valid_first := nil;
  1198. loc_info^.heap_valid_last := nil;
  1199. {$endif}
  1200. loc_info^.heap_mem_root := nil;
  1201. loc_info^.getmem_cnt := 0;
  1202. loc_info^.freemem_cnt := 0;
  1203. loc_info^.getmem_size := 0;
  1204. loc_info^.freemem_size := 0;
  1205. loc_info^.getmem8_size := 0;
  1206. loc_info^.freemem8_size := 0;
  1207. loc_info^.error_in_heap := false;
  1208. loc_info^.inside_trace_getmem := false;
  1209. EntryMemUsed := SysGetFPCHeapStatus.CurrHeapUsed;
  1210. end;
  1211. procedure TraceRelocateHeap;
  1212. begin
  1213. main_relo_todolist := @heap_info.heap_free_todo;
  1214. {$ifdef FPC_HAS_FEATURE_THREADING}
  1215. initcriticalsection(todo_lock);
  1216. {$endif}
  1217. end;
  1218. procedure move_heap_info(src_info, dst_info: pheap_info);
  1219. var
  1220. heap_mem: pheap_mem_info;
  1221. begin
  1222. if src_info^.heap_free_todo <> nil then
  1223. finish_heap_free_todo_list(src_info);
  1224. if dst_info^.heap_free_todo <> nil then
  1225. finish_heap_free_todo_list(dst_info);
  1226. heap_mem := src_info^.heap_mem_root;
  1227. if heap_mem <> nil then
  1228. begin
  1229. repeat
  1230. heap_mem^.todolist := @dst_info^.heap_free_todo;
  1231. if heap_mem^.previous = nil then break;
  1232. heap_mem := heap_mem^.previous;
  1233. until false;
  1234. heap_mem^.previous := dst_info^.heap_mem_root;
  1235. if dst_info^.heap_mem_root <> nil then
  1236. dst_info^.heap_mem_root^.next := heap_mem;
  1237. dst_info^.heap_mem_root := src_info^.heap_mem_root;
  1238. end;
  1239. inc(dst_info^.getmem_cnt, src_info^.getmem_cnt);
  1240. inc(dst_info^.getmem_size, src_info^.getmem_size);
  1241. inc(dst_info^.getmem8_size, src_info^.getmem8_size);
  1242. inc(dst_info^.freemem_cnt, src_info^.freemem_cnt);
  1243. inc(dst_info^.freemem_size, src_info^.freemem_size);
  1244. inc(dst_info^.freemem8_size, src_info^.freemem8_size);
  1245. dst_info^.error_in_heap := dst_info^.error_in_heap or src_info^.error_in_heap;
  1246. {$ifdef EXTRA}
  1247. if assigned(dst_info^.heap_valid_first) then
  1248. dst_info^.heap_valid_first^.prev_valid := src_info^.heap_valid_last
  1249. else
  1250. dst_info^.heap_valid_last := src_info^.heap_valid_last;
  1251. dst_info^.heap_valid_first := src_info^.heap_valid_first;
  1252. {$endif}
  1253. end;
  1254. procedure TraceExitThread;
  1255. var
  1256. loc_info: pheap_info;
  1257. begin
  1258. loc_info := @heap_info;
  1259. {$ifdef FPC_HAS_FEATURE_THREADING}
  1260. entercriticalsection(todo_lock);
  1261. {$endif}
  1262. move_heap_info(loc_info, @orphaned_info);
  1263. {$ifdef FPC_HAS_FEATURE_THREADING}
  1264. leavecriticalsection(todo_lock);
  1265. {$endif}
  1266. end;
  1267. function TraceGetHeapStatus:THeapStatus;
  1268. begin
  1269. TraceGetHeapStatus:=SysGetHeapStatus;
  1270. end;
  1271. function TraceGetFPCHeapStatus:TFPCHeapStatus;
  1272. begin
  1273. TraceGetFPCHeapStatus:=SysGetFPCHeapStatus;
  1274. end;
  1275. {*****************************************************************************
  1276. Program Hooks
  1277. *****************************************************************************}
  1278. Procedure SetHeapTraceOutput(const name : string);
  1279. var i : ptruint;
  1280. begin
  1281. if useownfile then
  1282. begin
  1283. useownfile:=false;
  1284. close(ownfile);
  1285. end;
  1286. assign(ownfile,name);
  1287. {$I-}
  1288. append(ownfile);
  1289. if IOResult<>0 then
  1290. begin
  1291. Rewrite(ownfile);
  1292. if IOResult<>0 then
  1293. begin
  1294. Writeln(textoutput^,'[heaptrc] Unable to open "',name,'", writing output to stderr instead.');
  1295. useownfile:=false;
  1296. exit;
  1297. end;
  1298. end;
  1299. {$I+}
  1300. useownfile:=true;
  1301. for i:=0 to Paramcount do
  1302. write(ownfile,paramstr(i),' ');
  1303. writeln(ownfile);
  1304. end;
  1305. procedure SetHeapTraceOutput(var ATextOutput : Text);
  1306. Begin
  1307. useowntextoutput := True;
  1308. textoutput := @ATextOutput;
  1309. end;
  1310. procedure SetHeapExtraInfo( size : ptruint;fillproc : tfillextrainfoproc;displayproc : tdisplayextrainfoproc);
  1311. begin
  1312. { the total size must stay multiple of 8, also allocate 2 pointers for
  1313. the fill and display procvars }
  1314. exact_info_size:=size + sizeof(theap_extra_info);
  1315. extra_info_size:=(exact_info_size+7) and not 7;
  1316. fill_extra_info_proc:=fillproc;
  1317. display_extra_info_proc:=displayproc;
  1318. end;
  1319. {*****************************************************************************
  1320. Install MemoryManager
  1321. *****************************************************************************}
  1322. const
  1323. TraceManager:TMemoryManager=(
  1324. NeedLock : true;
  1325. Getmem : @TraceGetMem;
  1326. Freemem : @TraceFreeMem;
  1327. FreememSize : @TraceFreeMemSize;
  1328. AllocMem : @TraceAllocMem;
  1329. ReAllocMem : @TraceReAllocMem;
  1330. MemSize : @TraceMemSize;
  1331. InitThread: @TraceInitThread;
  1332. DoneThread: @TraceExitThread;
  1333. RelocateHeap: @TraceRelocateHeap;
  1334. GetHeapStatus : @TraceGetHeapStatus;
  1335. GetFPCHeapStatus : @TraceGetFPCHeapStatus;
  1336. );
  1337. var
  1338. PrevMemoryManager : TMemoryManager;
  1339. procedure TraceInit;
  1340. begin
  1341. textoutput := @stderr;
  1342. useowntextoutput := false;
  1343. MakeCRC32Tbl;
  1344. main_orig_todolist := @heap_info.heap_free_todo;
  1345. main_relo_todolist := nil;
  1346. TraceInitThread;
  1347. GetMemoryManager(PrevMemoryManager);
  1348. SetMemoryManager(TraceManager);
  1349. useownfile:=false;
  1350. if outputstr <> '' then
  1351. SetHeapTraceOutput(outputstr);
  1352. {$ifdef EXTRA}
  1353. {$i-}
  1354. Assign(error_file,'heap.err');
  1355. Rewrite(error_file);
  1356. {$i+}
  1357. if IOResult<>0 then
  1358. begin
  1359. writeln('[heaptrc] Unable to create heap.err extra log file, writing output to screen.');
  1360. Assign(error_file,'');
  1361. Rewrite(error_file);
  1362. end;
  1363. {$endif EXTRA}
  1364. { if multithreading was initialized before heaptrc gets initialized (this is currently
  1365. the case for windows dlls), then RelocateHeap gets never called and the lock
  1366. must be initialized already here,
  1367. however, IsMultithread is not set in this case on windows,
  1368. it is set only if a new thread is started
  1369. }
  1370. {$IfNDef WINDOWS}
  1371. if IsMultithread then
  1372. {$EndIf WINDOWS}
  1373. TraceRelocateHeap;
  1374. end;
  1375. procedure TraceExit;
  1376. begin
  1377. { no dump if error
  1378. because this gives long long listings }
  1379. { clear inoutres, in case the program that quit didn't }
  1380. ioresult;
  1381. if (exitcode<>0) and (erroraddr<>nil) then
  1382. begin
  1383. if useownfile then
  1384. begin
  1385. Writeln(ownfile,'No heap dump by heaptrc unit');
  1386. Writeln(ownfile,'Exitcode = ',exitcode);
  1387. end
  1388. else
  1389. begin
  1390. Writeln(textoutput^,'No heap dump by heaptrc unit');
  1391. Writeln(textoutput^,'Exitcode = ',exitcode);
  1392. end;
  1393. if useownfile then
  1394. begin
  1395. useownfile:=false;
  1396. close(ownfile);
  1397. end;
  1398. exit;
  1399. end;
  1400. { Disable heaptrc memory manager to avoid problems }
  1401. SetMemoryManager(PrevMemoryManager);
  1402. move_heap_info(@orphaned_info, @heap_info);
  1403. dumpheap;
  1404. if heap_info.error_in_heap and (exitcode=0) then
  1405. exitcode:=203;
  1406. {$ifdef FPC_HAS_FEATURE_THREADING}
  1407. if main_relo_todolist <> nil then
  1408. donecriticalsection(todo_lock);
  1409. {$endif}
  1410. {$ifdef EXTRA}
  1411. Close(error_file);
  1412. {$endif EXTRA}
  1413. if useownfile then
  1414. begin
  1415. useownfile:=false;
  1416. close(ownfile);
  1417. end;
  1418. if useowntextoutput then
  1419. begin
  1420. useowntextoutput := false;
  1421. close(textoutput^);
  1422. end;
  1423. end;
  1424. {$if defined(win32) or defined(win64)}
  1425. function GetEnvironmentStrings : pchar; stdcall;
  1426. external 'kernel32' name 'GetEnvironmentStringsA';
  1427. function FreeEnvironmentStrings(p : pchar) : longbool; stdcall;
  1428. external 'kernel32' name 'FreeEnvironmentStringsA';
  1429. Function GetEnv(envvar: string): string;
  1430. var
  1431. s : string;
  1432. i : ptruint;
  1433. hp,p : pchar;
  1434. begin
  1435. getenv:='';
  1436. p:=GetEnvironmentStrings;
  1437. hp:=p;
  1438. while hp^<>#0 do
  1439. begin
  1440. s:=strpas(hp);
  1441. i:=pos('=',s);
  1442. if upcase(copy(s,1,i-1))=upcase(envvar) then
  1443. begin
  1444. getenv:=copy(s,i+1,length(s)-i);
  1445. break;
  1446. end;
  1447. { next string entry}
  1448. hp:=hp+strlen(hp)+1;
  1449. end;
  1450. FreeEnvironmentStrings(p);
  1451. end;
  1452. {$elseif defined(wince)}
  1453. Function GetEnv(P:string):Pchar;
  1454. begin
  1455. { WinCE does not have environment strings.
  1456. Add some way to specify heaptrc options? }
  1457. GetEnv:=nil;
  1458. end;
  1459. {$elseif defined(msdos)}
  1460. type
  1461. PFarChar=^Char;far;
  1462. PPFarChar=^PFarChar;
  1463. var
  1464. envp: PPFarChar;external name '__fpc_envp';
  1465. Function GetEnv(P:string):string;
  1466. var
  1467. ep : ppfarchar;
  1468. pc : pfarchar;
  1469. i : smallint;
  1470. found : boolean;
  1471. Begin
  1472. getenv:='';
  1473. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1474. ep:=envp;
  1475. found:=false;
  1476. if ep<>nil then
  1477. begin
  1478. while (not found) and (ep^<>nil) do
  1479. begin
  1480. found:=true;
  1481. for i:=1 to length(p) do
  1482. if p[i]<>ep^[i-1] then
  1483. begin
  1484. found:=false;
  1485. break;
  1486. end;
  1487. if not found then
  1488. inc(ep);
  1489. end;
  1490. end;
  1491. if found then
  1492. begin
  1493. pc:=ep^+length(p);
  1494. while pc^<>#0 do
  1495. begin
  1496. getenv:=getenv+pc^;
  1497. Inc(pc);
  1498. end;
  1499. end;
  1500. end;
  1501. {$else}
  1502. Function GetEnv(P:string):Pchar;
  1503. {
  1504. Searches the environment for a string with name p and
  1505. returns a pchar to it's value.
  1506. A pchar is used to accomodate for strings of length > 255
  1507. }
  1508. var
  1509. ep : ppchar;
  1510. i : ptruint;
  1511. found : boolean;
  1512. Begin
  1513. p:=p+'='; {Else HOST will also find HOSTNAME, etc}
  1514. ep:=envp;
  1515. found:=false;
  1516. if ep<>nil then
  1517. begin
  1518. while (not found) and (ep^<>nil) do
  1519. begin
  1520. found:=true;
  1521. for i:=1 to length(p) do
  1522. if p[i]<>ep^[i-1] then
  1523. begin
  1524. found:=false;
  1525. break;
  1526. end;
  1527. if not found then
  1528. inc(ep);
  1529. end;
  1530. end;
  1531. if found then
  1532. getenv:=ep^+length(p)
  1533. else
  1534. getenv:=nil;
  1535. end;
  1536. {$endif}
  1537. procedure LoadEnvironment;
  1538. var
  1539. i,j : ptruint;
  1540. s,s2 : string;
  1541. err : word;
  1542. begin
  1543. s:=Getenv('HEAPTRC');
  1544. if pos('keepreleased',s)>0 then
  1545. keepreleased:=true;
  1546. if pos('disabled',s)>0 then
  1547. useheaptrace:=false;
  1548. if pos('nohalt',s)>0 then
  1549. haltonerror:=false;
  1550. if pos('haltonnotreleased',s)>0 then
  1551. HaltOnNotReleased :=true;
  1552. if pos('skipifnoleaks',s)>0 then
  1553. GlobalSkipIfNoLeaks :=true;
  1554. if pos('tail_size=',s)>0 then
  1555. begin
  1556. i:=pos('tail_size=',s)+length('tail_size=');
  1557. s2:='';
  1558. while (i<=length(s)) and (s[i] in ['0'..'9']) do
  1559. begin
  1560. s2:=s2+s[i];
  1561. inc(i);
  1562. end;
  1563. val(s2,tail_size,err);
  1564. if err=0 then
  1565. tail_size:=((tail_size + sizeof(ptruint)-1) div sizeof(ptruint)) * sizeof(ptruint)
  1566. else
  1567. tail_size:=sizeof(ptruint);
  1568. add_tail:=(tail_size > 0);
  1569. end;
  1570. i:=pos('log=',s);
  1571. if i>0 then
  1572. begin
  1573. outputstr:=copy(s,i+4,255);
  1574. j:=pos(' ',outputstr);
  1575. if j=0 then
  1576. j:=length(outputstr)+1;
  1577. delete(outputstr,j,255);
  1578. end;
  1579. end;
  1580. Initialization
  1581. LoadEnvironment;
  1582. { heaptrc can be disabled from the environment }
  1583. if useheaptrace then
  1584. TraceInit;
  1585. finalization
  1586. if useheaptrace then
  1587. TraceExit;
  1588. end.