llvm::sys::PrintBacktrace(FILE*) is supposed to print a backtrace of the current thread given the current PC. This function was unimplemented on Windows, and instead the only time we could print a backtrace was as the result of an exception through LLVMUnhandledExceptionFilter. This patch implements backtracing of self by using setjmp() to get the current values of the instruction pointer, frame pointer, and stack pointer, and then calling the same function already used by LLVM to print backtraces given the same information. The one difference is that with an exception, we have a full CONTEXT structure that we can pass to StackWalk64. MSDN documents that this is an optional field to StackWalk64, which only provides the function the ability to handle more situations. Testing shows that even without this function, the results are still good.
|388 ↗||(On Diff #21242)|
Thanks, I missed that. That led me to find one more improvement, which is that we can set the value of StackFrame.ReturnAddr to _GetReturnAddress(). I don't know how much it'll help, but it certainly won't hurt, so I'll submit the patch with your suggested improvement as well the one to set StackFrame.AddrReturn.
Random thing I noticed:
_JUMP_BUFFER *JumpBuf = reinterpret_cast<__JUMP_BUFFER *>(Buffer);
The cast type has two underscores, but the variable type has one underscore
(__JUMP_BUFFER and _JUMP_BUFFER respectively). A comment would be nice.
- Sean Silva
I found a problem with my initial implementation. MSDN says that on x64, a full CONTEXT structure is required. So this patch was broken for x64. Luckily I learned of a new function which I was previously unaware of, RtlCaptureContext(). It captures a context for the current thread which is exactly what we need. This patch is updated to reflect these changes, and we now no longer require setjmp.
Will commit this tomorrow.