有没有办法知道我当前所在的方法名称?
这样就可以:
procedure TMyObject.SomeMethod();
begin
Writeln('my name is: ' + <hocus pocus>);
end;
将产生此输出:
我的名字是:SomeMethod
JCL是免费的,并且具有相关功能。它取决于堆栈跟踪的质量以及调试信息的多少。
JclDebug.pas
function FileByLevel(const Level: Integer = 0): string;
function ModuleByLevel(const Level: Integer = 0): string;
function ProcByLevel(const Level: Integer = 0): string;
function LineByLevel(const Level: Integer = 0): Integer;
请参阅我们的TSynMapFile
类。
它能够加载一个.map
文件,并将其压缩成优化的二进制格式。它比.map
本身要小得多(例如900 KB的.map
-> 70 KB的.mab
)。这个.mab
可以很容易地嵌入到exe中。因此,它比JCL或MadExcept使用的格式更小,也比Delphi编译时嵌入的信息更小。
您可以按如下方式使用它:
Map := TSynMapFile.Create; // or specify an exe name
try
i := Map.FindSymbol(SymbolAddr);
if i>=0 then
writeln(Map.Symbols[i].Name);
// or for your point:
writeln(Map.FindLocation(Addr)); // e.g. 'SynSelfTests.TestPeopleProc (784)'
finally
Map.Free;
end;
procedure TSynLog.Log(Level: TSynLogInfo);
var aCaller: PtrUInt;
begin
if (self<>nil) and (Level in fFamily.fLevel) then begin
LogHeaderLock(Level);
asm
mov eax,[ebp+4] // retrieve caller EIP from push ebp; mov ebp,esp
sub eax,5 // ignore call TSynLog.Enter op codes
mov aCaller,eax
end;
TSynMapFile.Log(fWriter,aCaller); // here it will call TSynMapFile for the current exe
LogTrailerUnLock(Level);
end;
end;
uses
EDebugInfo;
procedure TMyObject.SomeMethod();
begin
Writeln('my name is: ' + __FUNCTION__);
end;
还有__FILE__
、__MODULE__
、__UNIT__
、__LINE__
,以及一个通用的GetLocationInfoStr
函数。
然而: