heaptrc.pp 35 KB

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