12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025 |
- {
- $Id$
- This file is part of the Free Pascal run time library.
- Copyright (c) 1999-2000 by the Free Pascal development team.
- Heap tracer
- See the file COPYING.FPC, included in this distribution,
- for details about the copyright.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- **********************************************************************}
- unit heaptrc;
- { 0.99.12 had a bug that initialization/finalization only worked for
- objfpc,delphi mode }
- {$ifdef VER0_99_12}
- {$mode objfpc}
- {$endif}
- interface
- Procedure DumpHeap;
- Procedure MarkHeap;
- { define EXTRA to add more
- tests :
- - keep all memory after release and
- check by CRC value if not changed after release
- WARNING this needs extremely much memory (PM) }
- type
- FillExtraInfoType = procedure(p : pointer);
- { allows to add several longint value that can help
- to debug :
- see for instance ppheap.pas unit of the compiler source PM }
- Procedure SetExtraInfo( size : longint;func : FillExtraInfoType);
- Procedure SetHeapTraceOutput(const name : string);
- const
- { tracing level
- splitted in two if memory is released !! }
- {$ifdef EXTRA}
- tracesize = 16;
- {$else EXTRA}
- tracesize = 8;
- {$endif EXTRA}
- quicktrace : boolean=true;
- { calls halt() on error by default !! }
- HaltOnError : boolean = true;
- { set this to true if you suspect that memory
- is freed several times }
- {$ifdef EXTRA}
- keepreleased : boolean=true;
- add_tail : boolean = true;
- {$else EXTRA}
- keepreleased : boolean=false;
- add_tail : boolean = false;
- {$endif EXTRA}
- { put crc in sig
- this allows to test for writing into that part }
- usecrc : boolean = true;
- implementation
- type
- plongint = ^longint;
- const
- { allows to add custom info in heap_mem_info }
- extra_info_size : longint = 0;
- exact_info_size : longint = 0;
- EntryMemUsed : longint = 0;
- { function to fill this info up }
- fill_extra_info : FillExtraInfoType = nil;
- error_in_heap : boolean = false;
- inside_trace_getmem : boolean = false;
- type
- pheap_mem_info = ^theap_mem_info;
- { warning the size of theap_mem_info
- must be a multiple of 8
- because otherwise you will get
- problems when releasing the usual memory part !!
- sizeof(theap_mem_info = 16+tracesize*4 so
- tracesize must be even !! PM }
- theap_mem_info = record
- previous,
- next : pheap_mem_info;
- size : longint;
- sig : longint;
- {$ifdef EXTRA}
- release_sig : longint;
- prev_valid : pheap_mem_info;
- {$endif EXTRA}
- calls : array [1..tracesize] of longint;
- extra_info : record
- end;
- end;
- var
- ptext : ^text;
- ownfile : text;
- {$ifdef EXTRA}
- error_file : text;
- heap_valid_first,
- heap_valid_last : pheap_mem_info;
- {$endif EXTRA}
- heap_mem_root : pheap_mem_info;
- getmem_cnt,
- freemem_cnt : longint;
- getmem_size,
- freemem_size : longint;
- getmem8_size,
- freemem8_size : longint;
- {*****************************************************************************
- Crc 32
- *****************************************************************************}
- var
- {$ifdef Delphi}
- Crc32Tbl : array[0..255] of longword;
- {$else Delphi}
- Crc32Tbl : array[0..255] of longint;
- {$endif Delphi}
- procedure MakeCRC32Tbl;
- var
- {$ifdef Delphi}
- crc : longword;
- {$else Delphi}
- crc : longint;
- {$endif Delphi}
- i,n : byte;
- begin
- for i:=0 to 255 do
- begin
- crc:=i;
- for n:=1 to 8 do
- if odd(crc) then
- {$ifdef Delphi}
- crc:=(crc shr 1) xor $edb88320
- {$else Delphi}
- crc:=longint(cardinal(crc shr 1) xor $edb88320)
- {$endif Delphi}
- else
- crc:=crc shr 1;
- Crc32Tbl[i]:=crc;
- end;
- end;
- {$ifopt R+}
- {$define Range_check_on}
- {$endif opt R+}
- {$R- needed here }
- Function UpdateCrc32(InitCrc:longint;var InBuf;InLen:Longint):longint;
- var
- i : longint;
- p : pchar;
- begin
- p:=@InBuf;
- for i:=1 to InLen do
- begin
- InitCrc:=Crc32Tbl[byte(InitCrc) xor byte(p^)] xor (InitCrc shr 8);
- inc(longint(p));
- end;
- UpdateCrc32:=InitCrc;
- end;
- Function calculate_sig(p : pheap_mem_info) : longint;
- var
- crc : longint;
- pl : plongint;
- begin
- crc:=longint($ffffffff);
- crc:=UpdateCrc32(crc,p^.size,sizeof(longint));
- crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(longint));
- if extra_info_size>0 then
- crc:=UpdateCrc32(crc,p^.extra_info,exact_info_size);
- if add_tail then
- begin
- { Check also 4 bytes just after allocation !! }
- pl:=pointer(p)+extra_info_size+sizeof(theap_mem_info)+p^.size;
- crc:=UpdateCrc32(crc,pl^,sizeof(longint));
- end;
- calculate_sig:=crc;
- end;
- {$ifdef EXTRA}
- Function calculate_release_sig(p : pheap_mem_info) : longint;
- var
- crc : longint;
- pl : plongint;
- begin
- crc:=longint($ffffffff);
- crc:=UpdateCrc32(crc,p^.size,sizeof(longint));
- crc:=UpdateCrc32(crc,p^.calls,tracesize*sizeof(longint));
- if extra_info_size>0 then
- crc:=UpdateCrc32(crc,p^.extra_info,exact_info_size);
- { Check the whole of the whole allocation }
- pl:=pointer(p)+extra_info_size+sizeof(theap_mem_info);
- crc:=UpdateCrc32(crc,pl^,p^.size);
- { Check also 4 bytes just after allocation !! }
- if add_tail then
- begin
- { Check also 4 bytes just after allocation !! }
- pl:=pointer(p)+extra_info_size+sizeof(theap_mem_info)+p^.size;
- crc:=UpdateCrc32(crc,pl^,sizeof(longint));
- end;
- calculate_release_sig:=crc;
- end;
- {$endif EXTRA}
- {$ifdef Range_check_on}
- {$R+}
- {$undef Range_check_on}
- {$endif Range_check_on}
- {*****************************************************************************
- Helpers
- *****************************************************************************}
- procedure call_stack(pp : pheap_mem_info;var ptext : text);
- var
- i : longint;
- begin
- writeln(ptext,'Call trace for block 0x',hexstr(longint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
- for i:=1 to tracesize do
- if pp^.calls[i]<>0 then
- writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
- for i:=0 to (exact_info_size div 4)-1 do
- writeln(ptext,'info ',i,'=',plongint(pointer(@pp^.extra_info)+4*i)^);
- end;
- procedure call_free_stack(pp : pheap_mem_info;var ptext : text);
- var
- i : longint;
- begin
- writeln(ptext,'Call trace for block at 0x',hexstr(longint(pointer(pp)+sizeof(theap_mem_info)),8),' size ',pp^.size);
- for i:=1 to tracesize div 2 do
- if pp^.calls[i]<>0 then
- writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
- writeln(ptext,' was released at ');
- for i:=(tracesize div 2)+1 to tracesize do
- if pp^.calls[i]<>0 then
- writeln(ptext,BackTraceStrFunc(pp^.calls[i]));
- for i:=0 to (exact_info_size div 4)-1 do
- writeln(ptext,'info ',i,'=',plongint(pointer(@pp^.extra_info)+4*i)^);
- end;
- procedure dump_already_free(p : pheap_mem_info;var ptext : text);
- begin
- Writeln(ptext,'Marked memory at 0x',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' released');
- call_free_stack(p,ptext);
- Writeln(ptext,'freed again at');
- dump_stack(ptext,get_caller_frame(get_frame));
- end;
- procedure dump_error(p : pheap_mem_info;var ptext : text);
- begin
- Writeln(ptext,'Marked memory at 0x',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
- Writeln(ptext,'Wrong signature $',hexstr(p^.sig,8)
- ,' instead of ',hexstr(calculate_sig(p),8));
- dump_stack(ptext,get_caller_frame(get_frame));
- end;
- {$ifdef EXTRA}
- procedure dump_change_after(p : pheap_mem_info;var ptext : text);
- var pp : pchar;
- i : longint;
- begin
- Writeln(ptext,'Marked memory at 0x',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
- Writeln(ptext,'Wrong release CRC $',hexstr(p^.release_sig,8)
- ,' instead of ',hexstr(calculate_release_sig(p),8));
- Writeln(ptext,'This memory was changed after call to freemem !');
- call_free_stack(p,ptext);
- pp:=pointer(p)+sizeof(theap_mem_info)+extra_info_size;
- for i:=0 to p^.size-1 do
- if byte(pp[i])<>$F0 then
- Writeln(ptext,'offset',i,':$',hexstr(i,8),'"',pp[i],'"');
- end;
- {$endif EXTRA}
- procedure dump_wrong_size(p : pheap_mem_info;size : longint;var ptext : text);
- var
- i : longint;
- begin
- Writeln(ptext,'Marked memory at 0x',HexStr(longint(pointer(p)+sizeof(theap_mem_info)),8),' invalid');
- Writeln(ptext,'Wrong size : ',p^.size,' allocated ',size,' freed');
- dump_stack(ptext,get_caller_frame(get_frame));
- for i:=0 to (exact_info_size div 4)-1 do
- writeln(ptext,'info ',i,'=',plongint(@p^.extra_info+4*i)^);
- call_stack(p,ptext);
- end;
- function is_in_getmem_list (p : pheap_mem_info) : boolean;
- var
- i : longint;
- pp : pheap_mem_info;
- begin
- is_in_getmem_list:=false;
- pp:=heap_mem_root;
- i:=0;
- while pp<>nil do
- begin
- if ((pp^.sig<>longint($DEADBEEF)) or usecrc) and
- ((pp^.sig<>calculate_sig(pp)) or not usecrc) and
- (pp^.sig <> longint($AAAAAAAA)) then
- begin
- writeln(ptext^,'error in linked list of heap_mem_info');
- RunError(204);
- end;
- if pp=p then
- is_in_getmem_list:=true;
- pp:=pp^.previous;
- inc(i);
- if i>getmem_cnt-freemem_cnt then
- writeln(ptext^,'error in linked list of heap_mem_info');
- end;
- end;
- {*****************************************************************************
- TraceGetMem
- *****************************************************************************}
- Function TraceGetMem(size:longint):pointer;
- var
- i,bp : longint;
- pl : plongint;
- p : pointer;
- begin
- inc(getmem_size,size);
- inc(getmem8_size,((size+7) div 8)*8);
- { Do the real GetMem, but alloc also for the info block }
- bp:=size+sizeof(theap_mem_info)+extra_info_size;
- if add_tail then
- inc(bp,sizeof(longint));
- p:=SysGetMem(bp);
- { Create the info block }
- pheap_mem_info(p)^.sig:=longint($DEADBEEF);
- pheap_mem_info(p)^.size:=size;
- if add_tail then
- begin
- pl:=pointer(p)+bp-sizeof(longint);
- pl^:=longint($DEADBEEF);
- end;
- bp:=get_caller_frame(get_frame);
- for i:=1 to tracesize do
- begin
- pheap_mem_info(p)^.calls[i]:=get_caller_addr(bp);
- bp:=get_caller_frame(bp);
- end;
- { insert in the linked list }
- if heap_mem_root<>nil then
- heap_mem_root^.next:=pheap_mem_info(p);
- pheap_mem_info(p)^.previous:=heap_mem_root;
- pheap_mem_info(p)^.next:=nil;
- {$ifdef EXTRA}
- pheap_mem_info(p)^.prev_valid:=heap_valid_last;
- heap_valid_last:=pheap_mem_info(p);
- if not assigned(heap_valid_first) then
- heap_valid_first:=pheap_mem_info(p);
- {$endif EXTRA}
- heap_mem_root:=p;
- { must be changed before fill_extra_info is called
- because checkpointer can be called from within
- fill_extra_info PM }
- inc(getmem_cnt);
- if assigned(fill_extra_info) then
- begin
- inside_trace_getmem:=true;
- fill_extra_info(@pheap_mem_info(p)^.extra_info);
- inside_trace_getmem:=false;
- end;
- { update the pointer }
- if usecrc then
- pheap_mem_info(p)^.sig:=calculate_sig(pheap_mem_info(p));
- inc(p,sizeof(theap_mem_info)+extra_info_size);
- TraceGetmem:=p;
- end;
- {*****************************************************************************
- TraceFreeMem
- *****************************************************************************}
- function TraceFreeMemSize(var p:pointer;size:longint):longint;
- var
- i,bp, ppsize : longint;
- pp : pheap_mem_info;
- {$ifdef EXTRA}
- pp2 : pheap_mem_info;
- {$endif}
- begin
- inc(freemem_size,size);
- inc(freemem8_size,((size+7) div 8)*8);
- ppsize:= size + sizeof(theap_mem_info)+extra_info_size;
- if add_tail then
- ppsize:=ppsize+sizeof(longint);
- dec(p,sizeof(theap_mem_info)+extra_info_size);
- pp:=pheap_mem_info(p);
- if not quicktrace and not(is_in_getmem_list(pp)) then
- RunError(204);
- if (pp^.sig=longint($AAAAAAAA)) and not usecrc then
- begin
- error_in_heap:=true;
- dump_already_free(pp,ptext^);
- if haltonerror then halt(1);
- end
- else if ((pp^.sig<>longint($DEADBEEF)) or usecrc) and
- ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
- begin
- error_in_heap:=true;
- dump_error(pp,ptext^);
- {$ifdef EXTRA}
- dump_error(pp,error_file);
- {$endif EXTRA}
- { don't release anything in this case !! }
- if haltonerror then halt(1);
- exit;
- end
- else if pp^.size<>size then
- begin
- error_in_heap:=true;
- dump_wrong_size(pp,size,ptext^);
- {$ifdef EXTRA}
- dump_wrong_size(pp,size,error_file);
- {$endif EXTRA}
- if haltonerror then halt(1);
- { don't release anything in this case !! }
- exit;
- end;
- { now it is released !! }
- pp^.sig:=longint($AAAAAAAA);
- if not keepreleased then
- begin
- if pp^.next<>nil then
- pp^.next^.previous:=pp^.previous;
- if pp^.previous<>nil then
- pp^.previous^.next:=pp^.next;
- if pp=heap_mem_root then
- heap_mem_root:=heap_mem_root^.previous;
- end
- else
- begin
- bp:=get_caller_frame(get_frame);
- for i:=(tracesize div 2)+1 to tracesize do
- begin
- pp^.calls[i]:=get_caller_addr(bp);
- bp:=get_caller_frame(bp);
- end;
- end;
- inc(freemem_cnt);
- { release the normal memory at least !! }
- { this way we keep all info about all released memory !! }
- if keepreleased then
- begin
- {$ifndef EXTRA}
- dec(ppsize,sizeof(theap_mem_info)+extra_info_size);
- inc(p,sizeof(theap_mem_info)+extra_info_size);
- {$else EXTRA}
- inc(p,sizeof(theap_mem_info)+extra_info_size);
- fillchar(p^,size,#240); { $F0 will lead to GFP if used as pointer ! }
- { We want to check if the memory was changed after release !! }
- pp^.release_sig:=calculate_release_sig(pp);
- if pp=heap_valid_last then
- begin
- heap_valid_last:=pp^.prev_valid;
- if pp=heap_valid_first then
- heap_valid_first:=nil;
- exit;
- end;
- pp2:=heap_valid_last;
- while assigned(pp2) do
- begin
- if pp2^.prev_valid=pp then
- begin
- pp2^.prev_valid:=pp^.prev_valid;
- if pp=heap_valid_first then
- heap_valid_first:=pp2;
- exit;
- end
- else
- pp2:=pp2^.prev_valid;
- end;
- exit;
- {$endif EXTRA}
- end;
- i:=SysFreeMemSize(p,ppsize);
- dec(i,sizeof(theap_mem_info)+extra_info_size);
- if add_tail then
- dec(i,sizeof(longint));
- TraceFreeMemSize:=i;
- end;
- function TraceMemSize(p:pointer):Longint;
- var
- l : longint;
- begin
- l:=SysMemSize(p-(sizeof(theap_mem_info)+extra_info_size));
- dec(l,sizeof(theap_mem_info)+extra_info_size);
- if add_tail then
- dec(l,sizeof(longint));
- TraceMemSize:=l;
- end;
- function TraceFreeMem(var p:pointer):longint;
- var
- size : longint;
- pp : pheap_mem_info;
- begin
- pp:=pheap_mem_info(pointer(p)-(sizeof(theap_mem_info)+extra_info_size));
- size:=TraceMemSize(p);
- { this can never happend normaly }
- if pp^.size>size then
- begin
- dump_wrong_size(pp,size,ptext^);
- {$ifdef EXTRA}
- dump_wrong_size(pp,size,error_file);
- {$endif EXTRA}
- end;
- TraceFreeMem:=TraceFreeMemSize(p,pp^.size);
- end;
- {*****************************************************************************
- ReAllocMem
- *****************************************************************************}
- function TraceReAllocMem(var p:pointer;size:longint):Pointer;
- var
- newP: pointer;
- oldsize,
- i,bp : longint;
- pl : plongint;
- pp : pheap_mem_info;
- begin
- { Free block? }
- if size=0 then
- begin
- if p<>nil then
- TraceFreeMem(p);
- TraceReallocMem:=P;
- exit;
- end;
- { Allocate a new block? }
- if p=nil then
- begin
- p:=TraceGetMem(size);
- TraceReallocMem:=P;
- exit;
- end;
- { Resize block }
- dec(p,sizeof(theap_mem_info)+extra_info_size);
- pp:=pheap_mem_info(p);
- { test block }
- if ((pp^.sig<>longint($DEADBEEF)) or usecrc) and
- ((pp^.sig<>calculate_sig(pp)) or not usecrc) then
- begin
- error_in_heap:=true;
- dump_error(pp,ptext^);
- {$ifdef EXTRA}
- dump_error(pp,error_file);
- {$endif EXTRA}
- { don't release anything in this case !! }
- if haltonerror then halt(1);
- exit;
- end;
- { Do the real ReAllocMem, but alloc also for the info block }
- bp:=size+sizeof(theap_mem_info)+extra_info_size;
- if add_tail then
- inc(bp,sizeof(longint));
- { the internal ReAllocMem is not allowed to move any data }
- if not SysTryResizeMem(p,bp) then
- begin
- { restore p }
- inc(p,sizeof(theap_mem_info)+extra_info_size);
- { get a new block }
- oldsize:=TraceMemSize(p);
- newP := TraceGetMem(size);
- { move the data }
- if newP <> nil then
- move(p^,newP^,oldsize);
- { release p }
- traceFreeMem(p);
- p := newP;
- traceReAllocMem := p;
- exit;
- end;
- pp:=pheap_mem_info(p);
- { adjust like a freemem and then a getmem, so you get correct
- results in the summary display }
- inc(freemem_size,pp^.size);
- inc(freemem8_size,((pp^.size+7) div 8)*8);
- inc(getmem_size,size);
- inc(getmem8_size,((size+7) div 8)*8);
- { Create the info block }
- pp^.sig:=longint($DEADBEEF);
- pp^.size:=size;
- if add_tail then
- begin
- pl:=pointer(p)+bp-sizeof(longint);
- pl^:=longint($DEADBEEF);
- end;
- bp:=get_caller_frame(get_frame);
- for i:=1 to tracesize do
- begin
- pp^.calls[i]:=get_caller_addr(bp);
- bp:=get_caller_frame(bp);
- end;
- if assigned(fill_extra_info) then
- fill_extra_info(@pp^.extra_info);
- { update the pointer }
- if usecrc then
- pp^.sig:=calculate_sig(pp);
- inc(p,sizeof(theap_mem_info)+extra_info_size);
- TraceReAllocmem:=p;
- end;
- {*****************************************************************************
- Check pointer
- *****************************************************************************}
- {$ifndef Unix}
- {$S-}
- {$endif}
- {$ifdef go32v2}
- var
- __stklen : cardinal;external name '__stklen';
- __stkbottom : cardinal;external name '__stkbottom';
- edata : cardinal; external name 'edata';
- heap_at_init : pointer;
- {$endif go32v2}
- {$ifdef win32}
- var
- StartUpHeapEnd : pointer;
- { I found no symbol for start of text section :(
- so we usee the _mainCRTStartup which should be
- in wprt0.ow or wdllprt0.ow PM }
- text_begin : cardinal;external name '_mainCRTStartup';
- data_end : cardinal;external name '__data_end__';
- {$endif}
- procedure CheckPointer(p : pointer);[public, alias : 'FPC_CHECKPOINTER'];
- var
- i : longint;
- pp : pheap_mem_info;
- get_ebp,stack_top : cardinal;
- data_end : cardinal;
- label
- _exit;
- begin
- asm
- pushal
- end;
- if p=nil then
- goto _exit;
- i:=0;
- {$ifdef go32v2}
- if cardinal(p)<$1000 then
- runerror(216);
- asm
- movl %ebp,get_ebp
- leal edata,%eax
- movl %eax,data_end
- end;
- stack_top:=__stkbottom+__stklen;
- { allow all between start of code and end of data }
- if cardinal(p)<=data_end then
- goto _exit;
- { .bss section }
- if cardinal(p)<=cardinal(heap_at_init) then
- goto _exit;
- { stack can be above heap !! }
- if (cardinal(p)>=get_ebp) and (cardinal(p)<=stack_top) then
- goto _exit;
- {$endif go32v2}
- { I don't know where the stack is in other OS !! }
- {$ifdef win32}
- if (cardinal(p)>=$40000) and (p<=HeapOrg) then
- goto _exit;
- { inside stack ? }
- asm
- movl %ebp,get_ebp
- end;
- if (cardinal(p)>get_ebp) and
- (cardinal(p)<Win32StackTop) then
- goto _exit;
- {$endif win32}
- if p>=heapptr then
- runerror(216);
- { first try valid list faster }
- {$ifdef EXTRA}
- pp:=heap_valid_last;
- while pp<>nil do
- begin
- { inside this valid block ! }
- { we can be changing the extrainfo !! }
- if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info){+extra_info_size}) and
- (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+extra_info_size+pp^.size) then
- begin
- { check allocated block }
- if ((pp^.sig=longint($DEADBEEF)) and not usecrc) or
- ((pp^.sig=calculate_sig(pp)) and usecrc) or
- { special case of the fill_extra_info call }
- ((pp=heap_valid_last) and usecrc and (pp^.sig=longint($DEADBEEF))
- and inside_trace_getmem) then
- goto _exit
- else
- begin
- writeln(ptext^,'corrupted heap_mem_info');
- dump_error(pp,ptext^);
- halt(1);
- end;
- end
- else
- pp:=pp^.prev_valid;
- inc(i);
- if i>getmem_cnt-freemem_cnt then
- begin
- writeln(ptext^,'error in linked list of heap_mem_info');
- halt(1);
- end;
- end;
- i:=0;
- {$endif EXTRA}
- pp:=heap_mem_root;
- while pp<>nil do
- begin
- { inside this block ! }
- if (cardinal(p)>=cardinal(pp)+sizeof(theap_mem_info)+cardinal(extra_info_size)) and
- (cardinal(p)<=cardinal(pp)+sizeof(theap_mem_info)+cardinal(extra_info_size)+cardinal(pp^.size)) then
- { allocated block }
- if ((pp^.sig=longint($DEADBEEF)) and not usecrc) or
- ((pp^.sig=calculate_sig(pp)) and usecrc) then
- goto _exit
- else
- begin
- writeln(ptext^,'pointer $',hexstr(longint(p),8),' points into invalid memory block');
- dump_error(pp,ptext^);
- runerror(204);
- end;
- pp:=pp^.previous;
- inc(i);
- if i>getmem_cnt then
- begin
- writeln(ptext^,'error in linked list of heap_mem_info');
- halt(1);
- end;
- end;
- writeln(ptext^,'pointer $',hexstr(longint(p),8),' does not point to valid memory block');
- runerror(204);
- _exit:
- asm
- popal
- end;
- end;
- {*****************************************************************************
- Dump Heap
- *****************************************************************************}
- procedure dumpheap;
- var
- pp : pheap_mem_info;
- i : longint;
- ExpectedMemAvail : longint;
- begin
- pp:=heap_mem_root;
- Writeln(ptext^,'Heap dump by heaptrc unit');
- Writeln(ptext^,getmem_cnt, ' memory blocks allocated : ',getmem_size,'/',getmem8_size);
- Writeln(ptext^,freemem_cnt,' memory blocks freed : ',freemem_size,'/',freemem8_size);
- Writeln(ptext^,getmem_cnt-freemem_cnt,' unfreed memory blocks : ',getmem_size-freemem_size);
- Write(ptext^,'True heap size : ',system.HeapSize);
- if EntryMemUsed > 0 then
- Writeln(ptext^,' (',EntryMemUsed,' used in System startup)')
- else
- Writeln(ptext^);
- Writeln(ptext^,'True free heap : ',MemAvail);
- ExpectedMemAvail:=system.HeapSize-(getmem8_size-freemem8_size)-
- (getmem_cnt-freemem_cnt)*(sizeof(theap_mem_info)+extra_info_size)-EntryMemUsed;
- If ExpectedMemAvail<>MemAvail then
- Writeln(ptext^,'Should be : ',ExpectedMemAvail);
- i:=getmem_cnt-freemem_cnt;
- while pp<>nil do
- begin
- if i<0 then
- begin
- Writeln(ptext^,'Error in heap memory list');
- Writeln(ptext^,'More memory blocks than expected');
- exit;
- end;
- if ((pp^.sig=longint($DEADBEEF)) and not usecrc) or
- ((pp^.sig=calculate_sig(pp)) and usecrc) then
- begin
- { this one was not released !! }
- if exitcode<>203 then
- call_stack(pp,ptext^);
- dec(i);
- end
- else if pp^.sig<>longint($AAAAAAAA) then
- begin
- dump_error(pp,ptext^);
- {$ifdef EXTRA}
- dump_error(pp,error_file);
- {$endif EXTRA}
- error_in_heap:=true;
- end
- {$ifdef EXTRA}
- else if pp^.release_sig<>calculate_release_sig(pp) then
- begin
- dump_change_after(pp,ptext^);
- dump_change_after(pp,error_file);
- error_in_heap:=true;
- end
- {$endif EXTRA}
- ;
- pp:=pp^.previous;
- end;
- end;
- procedure markheap;
- var
- pp : pheap_mem_info;
- begin
- pp:=heap_mem_root;
- while pp<>nil do
- begin
- pp^.sig:=longint($AAAAAAAA);
- pp:=pp^.previous;
- end;
- end;
- {*****************************************************************************
- AllocMem
- *****************************************************************************}
- function TraceAllocMem(size:longint):Pointer;
- begin
- TraceAllocMem:=SysAllocMem(size);
- end;
- {*****************************************************************************
- No specific tracing calls
- *****************************************************************************}
- function TraceMemAvail:longint;
- begin
- TraceMemAvail:=SysMemAvail;
- end;
- function TraceMaxAvail:longint;
- begin
- TraceMaxAvail:=SysMaxAvail;
- end;
- function TraceHeapSize:longint;
- begin
- TraceHeapSize:=SysHeapSize;
- end;
- {*****************************************************************************
- Install MemoryManager
- *****************************************************************************}
- const
- TraceManager:TMemoryManager=(
- Getmem : TraceGetMem;
- Freemem : TraceFreeMem;
- FreememSize : TraceFreeMemSize;
- AllocMem : TraceAllocMem;
- ReAllocMem : TraceReAllocMem;
- MemSize : TraceMemSize;
- MemAvail : TraceMemAvail;
- MaxAvail : TraceMaxAvail;
- HeapSize : TraceHeapsize;
- );
- procedure TraceExit;
- begin
- { no dump if error
- because this gives long long listings }
- { clear inoutres, in case the program that quit didn't }
- ioresult;
- if (exitcode<>0) and (erroraddr<>nil) then
- begin
- Writeln(ptext^,'No heap dump by heaptrc unit');
- Writeln(ptext^,'Exitcode = ',exitcode);
- if ptext<>@stderr then
- begin
- ptext:=@stderr;
- close(ownfile);
- end;
- exit;
- end;
- if not error_in_heap then
- Dumpheap;
- if error_in_heap and (exitcode=0) then
- exitcode:=203;
- {$ifdef EXTRA}
- Close(error_file);
- {$endif EXTRA}
- if ptext<>@stderr then
- begin
- ptext:=@stderr;
- close(ownfile);
- end;
- end;
- Procedure SetHeapTraceOutput(const name : string);
- var i : longint;
- begin
- if ptext<>@stderr then
- begin
- ptext:=@stderr;
- close(ownfile);
- end;
- assign(ownfile,name);
- {$I-}
- append(ownfile);
- if IOResult<>0 then
- Rewrite(ownfile);
- {$I+}
- ptext:=@ownfile;
- for i:=0 to Paramcount do
- write(ptext^,paramstr(i),' ');
- writeln(ptext^);
- end;
- procedure SetExtraInfo( size : longint;func : fillextrainfotype);
- begin
- if getmem_cnt>0 then
- begin
- writeln(ptext^,'Setting extra info is only possible at start !! ');
- dumpheap;
- end
- else
- begin
- { the total size must stay multiple of 8 !! }
- exact_info_size:=size;
- extra_info_size:=((size+7) div 8)*8;
- fill_extra_info:=func;
- end;
- end;
- Initialization
- EntryMemUsed:=System.HeapSize-MemAvail;
- MakeCRC32Tbl;
- SetMemoryManager(TraceManager);
- ptext:=@stderr;
- {$ifdef EXTRA}
- Assign(error_file,'heap.err');
- Rewrite(error_file);
- {$endif EXTRA}
- { checkpointer init }
- {$ifdef go32v2}
- Heap_at_init:=HeapPtr;
- {$endif}
- {$ifdef win32}
- StartupHeapEnd:=HeapEnd;
- {$endif}
- finalization
- TraceExit;
- end.
- {
- $Log$
- Revision 1.6 2000-12-16 15:57:17 jonas
- * removed 64bit evaluations when range checking is on
- Revision 1.5 2000/12/07 17:19:47 jonas
- * new constant handling: from now on, hex constants >$7fffffff are
- parsed as unsigned constants (otherwise, $80000000 got sign extended
- and became $ffffffff80000000), all constants in the longint range
- become longints, all constants >$7fffffff and <=cardinal($ffffffff)
- are cardinals and the rest are int64's.
- * added lots of longint typecast to prevent range check errors in the
- compiler and rtl
- * type casts of symbolic ordinal constants are now preserved
- * fixed bug where the original resulttype wasn't restored correctly
- after doing a 64bit rangecheck
- Revision 1.4 2000/11/13 13:40:03 marco
- * Renamefest
- Revision 1.3 2000/08/24 09:01:07 jonas
- * clear inoutres in traceexit before writing anything (to avoid an RTE
- when writing the heaptrc output when a program didn't handle ioresult)
- (merged from fixes branch)
- Revision 1.2 2000/07/13 11:33:44 michael
- + removed logs
- }
|