heaptrc.pp 35 KB

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