4 * Copyright: Copyright Benjamin Thaut 2010 - 2013.
5 * License: Distributed under the
6 * $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7 * (See accompanying file LICENSE)
8 * Authors: Benjamin Thaut, Sean Kelly
9 * Source: $(DRUNTIMESRC core/sys/windows/_stacktrace.d)
12 module core.sys.windows.stacktrace;
16 import core.stdc.stdlib;
17 import core.stdc.string;
18 import core.sys.windows.dbghelp;
19 import core.sys.windows.imagehlp /+: ADDRESS_MODE+/;
20 import core.sys.windows.winbase;
21 import core.sys.windows.windef;
24 debug(PRINTF) import core.stdc.stdio;
27 extern(Windows) void RtlCaptureContext(CONTEXT* ContextRecord) @nogc;
28 extern(Windows) DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR pBuffer, DWORD nSize);
30 extern(Windows) alias USHORT function(ULONG FramesToSkip, ULONG FramesToCapture, PVOID *BackTrace, PULONG BackTraceHash) @nogc RtlCaptureStackBackTraceFunc;
32 private __gshared RtlCaptureStackBackTraceFunc RtlCaptureStackBackTrace;
33 private __gshared CRITICAL_SECTION mutex; // cannot use core.sync.mutex.Mutex unfortunately (cyclic dependency...)
34 private __gshared immutable bool initialized;
37 class StackTrace : Throwable.TraceInfo
43 * skip = The number of stack frames to skip.
44 * context = The context to receive the stack trace from. Can be null.
46 this(size_t skip, CONTEXT* context) @nogc
51 static enum INTERNALFRAMES = 3;
53 static enum INTERNALFRAMES = 2;
55 skip += INTERNALFRAMES; //skip the stack frames within the StackTrace class
59 //When a exception context is given the first stack frame is repeated for some reason
61 static enum INTERNALFRAMES = 1;
63 static enum INTERNALFRAMES = 1;
65 skip += INTERNALFRAMES;
68 m_trace = trace(tracebuf[], skip, context);
71 override int opApply( scope int delegate(ref const(char[])) dg ) const
73 return opApply( (ref size_t, ref const(char[]) buf)
80 override int opApply( scope int delegate(ref size_t, ref const(char[])) dg ) const
83 foreach ( i, e; resolve(m_trace) )
85 if ( (result = dg( i, e )) != 0 )
92 @trusted override string toString() const
104 * Receive a stack trace in the form of an address list. One form accepts
105 * an allocated buffer, the other form automatically allocates the buffer.
108 * skip = How many stack frames should be skipped.
109 * context = The context that should be used. If null the current context is used.
110 * buffer = The buffer to use for the trace. This should be at least 63 elements.
112 * A list of addresses that can be passed to resolve at a later point in time.
114 static ulong[] trace(size_t skip = 0, CONTEXT* context = null)
116 return trace(new ulong[63], skip, context);
120 static ulong[] trace(ulong[] buffer, size_t skip = 0, CONTEXT* context = null) @nogc
122 EnterCriticalSection(&mutex);
123 scope(exit) LeaveCriticalSection(&mutex);
125 return traceNoSync(buffer, skip, context);
129 * Resolve a stack trace.
131 * addresses = A list of addresses to resolve.
133 * An array of strings with the results.
135 @trusted static char[][] resolve(const(ulong)[] addresses)
137 // FIXME: make @nogc to avoid having to disable resolution within finalizers
138 import core.memory : GC;
142 EnterCriticalSection(&mutex);
143 scope(exit) LeaveCriticalSection(&mutex);
145 return resolveNoSync(addresses);
153 static ulong[] traceNoSync(ulong[] buffer, size_t skip, CONTEXT* context) @nogc
155 auto dbghelp = DbgHelp.get();
157 return []; // dbghelp.dll not available
159 if (buffer.length >= 63 && RtlCaptureStackBackTrace !is null &&
164 auto bufptr = cast(void**)buffer.ptr;
168 size_t[63] bufstorage = void; // On windows xp the sum of "frames to skip" and "frames to capture" can't be greater then 63
169 auto bufptr = cast(void**)bufstorage.ptr;
171 auto backtraceLength = RtlCaptureStackBackTrace(cast(ULONG)skip, cast(ULONG)(63 - skip), bufptr, null);
173 // If we get a backtrace and it does not have the maximum length use it.
174 // Otherwise rely on tracing through StackWalk64 which is slower but works when no frame pointers are available.
175 if (backtraceLength > 1 && backtraceLength < 63 - skip)
177 debug(PRINTF) printf("Using result from RtlCaptureStackBackTrace\n");
180 foreach (i, ref e; buffer[0 .. backtraceLength])
185 return buffer[0..backtraceLength];
189 HANDLE hThread = GetCurrentThread();
190 HANDLE hProcess = GetCurrentProcess();
195 ctxt.ContextFlags = CONTEXT_FULL;
196 RtlCaptureContext(&ctxt);
204 STACKFRAME64 stackframe;
209 enum Flat = ADDRESS_MODE.AddrModeFlat;
210 AddrPC.Offset = ctxt.Eip;
212 AddrFrame.Offset = ctxt.Ebp;
213 AddrFrame.Mode = Flat;
214 AddrStack.Offset = ctxt.Esp;
215 AddrStack.Mode = Flat;
217 else version (X86_64)
219 enum Flat = ADDRESS_MODE.AddrModeFlat;
220 AddrPC.Offset = ctxt.Rip;
222 AddrFrame.Offset = ctxt.Rbp;
223 AddrFrame.Mode = Flat;
224 AddrStack.Offset = ctxt.Rsp;
225 AddrStack.Mode = Flat;
229 version (X86) enum imageType = IMAGE_FILE_MACHINE_I386;
230 else version (X86_64) enum imageType = IMAGE_FILE_MACHINE_AMD64;
231 else static assert(0, "unimplemented");
236 // do ... while so that we don't skip the first stackframe
239 if (frameNum >= skip)
241 buffer[nframes++] = stackframe.AddrPC.Offset;
245 while (dbghelp.StackWalk64(imageType, hProcess, hThread, &stackframe,
246 &ctxt, null, null, null, null));
247 return buffer[0 .. nframes];
250 static char[][] resolveNoSync(const(ulong)[] addresses)
252 auto dbghelp = DbgHelp.get();
254 return []; // dbghelp.dll not available
256 HANDLE hProcess = GetCurrentProcess();
258 static struct BufSymbol
261 IMAGEHLP_SYMBOLA64 _base;
262 TCHAR[1024] _buf = void;
264 BufSymbol bufSymbol=void;
265 IMAGEHLP_SYMBOLA64* symbol = &bufSymbol._base;
266 symbol.SizeOfStruct = IMAGEHLP_SYMBOLA64.sizeof;
267 symbol.MaxNameLength = bufSymbol._buf.length;
270 foreach (pc; addresses)
273 if (dbghelp.SymGetSymFromAddr64(hProcess, pc, null, symbol) &&
277 IMAGEHLP_LINEA64 line=void;
278 line.SizeOfStruct = IMAGEHLP_LINEA64.sizeof;
280 if (dbghelp.SymGetLineFromAddr64(hProcess, pc, &disp, &line))
281 res = formatStackFrame(cast(void*)pc, symbol.Name.ptr,
282 line.FileName, line.LineNumber);
284 res = formatStackFrame(cast(void*)pc, symbol.Name.ptr);
287 res = formatStackFrame(cast(void*)pc);
293 static char[] formatStackFrame(void* pc)
295 import core.stdc.stdio : snprintf;
296 char[2+2*size_t.sizeof+1] buf=void;
298 immutable len = snprintf(buf.ptr, buf.length, "0x%p", pc);
299 cast(uint)len < buf.length || assert(0);
300 return buf[0 .. len].dup;
303 static char[] formatStackFrame(void* pc, char* symName)
305 char[2048] demangleBuf=void;
307 auto res = formatStackFrame(pc);
309 const(char)[] tempSymName = symName[0 .. strlen(symName)];
310 // Deal with dmd mangling of long names for OMF 32 bits builds
311 // Note that `target.d` only defines `CRuntime_DigitalMars` for OMF builds
312 version (CRuntime_DigitalMars)
314 size_t decodeIndex = 0;
315 tempSymName = decodeDmdString(tempSymName, decodeIndex);
317 res ~= demangle(tempSymName, demangleBuf);
321 static char[] formatStackFrame(void* pc, char* symName,
322 const scope char* fileName, uint lineNum)
324 import core.stdc.stdio : snprintf;
327 auto res = formatStackFrame(pc, symName);
329 res ~= fileName[0 .. strlen(fileName)];
331 immutable len = snprintf(buf.ptr, buf.length, "%u", lineNum);
332 cast(uint)len < buf.length || assert(0);
333 res ~= buf[0 .. len];
340 // Workaround OPTLINK bug (Bugzilla 8263)
341 extern(Windows) BOOL FixupDebugHeader(HANDLE hProcess, ULONG ActionCode,
342 ulong CallbackContext, ulong UserContext)
344 if (ActionCode == CBA_READ_MEMORY)
346 auto p = cast(IMAGEHLP_CBA_READ_MEMORY*)CallbackContext;
347 if (!(p.addr & 0xFF) && p.bytes == 0x1C &&
348 // IMAGE_DEBUG_DIRECTORY.PointerToRawData
349 (*cast(DWORD*)(p.addr + 24) & 0xFF) == 0x20)
351 immutable base = DbgHelp.get().SymGetModuleBase64(hProcess, p.addr);
352 // IMAGE_DEBUG_DIRECTORY.AddressOfRawData
353 if (base + *cast(DWORD*)(p.addr + 20) == p.addr + 0x1C &&
354 *cast(DWORD*)(p.addr + 0x1C) == 0 &&
355 *cast(DWORD*)(p.addr + 0x20) == ('N'|'B'<<8|'0'<<16|'9'<<24))
357 debug(PRINTF) printf("fixup IMAGE_DEBUG_DIRECTORY.AddressOfRawData\n");
358 memcpy(p.buf, cast(void*)p.addr, 0x1C);
359 *cast(DWORD*)(p.buf + 20) = cast(DWORD)(p.addr - base) + 0x20;
368 private string generateSearchPath()
370 __gshared string[3] defaultPathList = ["_NT_SYMBOL_PATH",
371 "_NT_ALTERNATE_SYMBOL_PATH",
375 char[2048] temp = void;
378 foreach ( e; defaultPathList )
380 if ( (len = GetEnvironmentVariableA( e.ptr, temp.ptr, temp.length )) > 0 )
382 path ~= temp[0 .. len];
393 auto dbghelp = DbgHelp.get();
395 if ( dbghelp is null )
396 return; // dbghelp.dll not available
398 auto kernel32Handle = LoadLibraryA( "kernel32.dll" );
399 if (kernel32Handle !is null)
401 RtlCaptureStackBackTrace = cast(RtlCaptureStackBackTraceFunc) GetProcAddress(kernel32Handle, "RtlCaptureStackBackTrace");
404 if (RtlCaptureStackBackTrace !is null)
405 printf("Found RtlCaptureStackBackTrace\n");
411 API_VERSION* dbghelpVersion = dbghelp.ImagehlpApiVersion();
412 printf("DbgHelp Version %d.%d.%d\n", dbghelpVersion.MajorVersion, dbghelpVersion.MinorVersion, dbghelpVersion.Revision);
415 HANDLE hProcess = GetCurrentProcess();
417 DWORD symOptions = dbghelp.SymGetOptions();
418 symOptions |= SYMOPT_LOAD_LINES;
419 symOptions |= SYMOPT_FAIL_CRITICAL_ERRORS;
420 symOptions |= SYMOPT_DEFERRED_LOAD;
421 symOptions = dbghelp.SymSetOptions( symOptions );
423 debug(PRINTF) printf("Search paths: %s\n", generateSearchPath().ptr);
425 if (!dbghelp.SymInitialize(hProcess, generateSearchPath().ptr, TRUE))
428 dbghelp.SymRegisterCallback64(hProcess, &FixupDebugHeader, 0);
430 InitializeCriticalSection(&mutex);