How to Log Stack Frames with Windows x64

前端 未结 12 1036
-上瘾入骨i
-上瘾入骨i 2020-12-07 14:48

I am using Stackdumps with Win32, to write all return adresses into my logfile. I match these with a mapfile later on (see my article [Post Mortem Debugging][1]).

<

12条回答
  •  悲哀的现实
    2020-12-07 15:29

    I finally found a reliable way to log the stack frames in x64, using the Windows function CaptureStackBackTrace(). As I did not want to update my SDK, I call it via GetProcAddress(LoadLibrary());

       typedef USHORT (WINAPI *CaptureStackBackTraceType)(__in ULONG, __in ULONG, __out PVOID*, __out_opt PULONG);
       CaptureStackBackTraceType func = (CaptureStackBackTraceType)(GetProcAddress(LoadLibrary("kernel32.dll"), "RtlCaptureStackBackTrace"));
    
       if(func == NULL)
           return; // WOE 29.SEP.2010
    
       // Quote from Microsoft Documentation:
       // ## Windows Server 2003 and Windows XP:  
       // ## The sum of the FramesToSkip and FramesToCapture parameters must be less than 63.
       const int kMaxCallers = 62; 
    
       void* callers[kMaxCallers];
       int count = (func)(0, kMaxCallers, callers, NULL);
       for(i = 0; i < count; i++)
          printf(TraceFile, "*** %d called from %016I64LX\n", i, callers[i]);
    

提交回复
热议问题