heaptrc.pp 36 KB

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