ReactOS  0.4.12-dev-18-gf469aca
stacktrace.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: Dr. Watson crash reporter
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Print a stacktrace
5  * COPYRIGHT: Copyright 2017,2018 Mark Jansen (mark.jansen@reactos.org)
6  */
7 
8 #include "precomp.h"
9 #include <dbghelp.h>
10 
11 #define STACK_MAX_RECURSION_DEPTH 50
12 
13 
15 {
16  DWORD symOptions = SymGetOptions();
18  SymSetOptions(symOptions);
20 }
21 
23 {
25 }
26 
27 static char ToChar(UCHAR data)
28 {
29  if (data < 0xa)
30  return '0' + data;
31  else if (data <= 0xf)
32  return 'a' + data - 0xa;
33  return '?';
34 }
35 
37 {
38  DWORD MachineType;
39  STACKFRAME64 StackFrame = { { 0 } };
40 
41 #ifdef _M_X64
42  MachineType = IMAGE_FILE_MACHINE_AMD64;
43  StackFrame.AddrPC.Offset = thread.Context.Rip;
44  StackFrame.AddrPC.Mode = AddrModeFlat;
45  StackFrame.AddrStack.Offset = thread.Context.Rsp;
46  StackFrame.AddrStack.Mode = AddrModeFlat;
47  StackFrame.AddrFrame.Offset = thread.Context.Rbp;
48  StackFrame.AddrFrame.Mode = AddrModeFlat;
49 #else
50  MachineType = IMAGE_FILE_MACHINE_I386;
51  StackFrame.AddrPC.Offset = thread.Context.Eip;
52  StackFrame.AddrPC.Mode = AddrModeFlat;
53  StackFrame.AddrStack.Offset = thread.Context.Esp;
54  StackFrame.AddrStack.Mode = AddrModeFlat;
55  StackFrame.AddrFrame.Offset = thread.Context.Ebp;
56  StackFrame.AddrFrame.Mode = AddrModeFlat;
57 #endif
58 
59 
60 #define STACKWALK_MAX_NAMELEN 512
61  char buf[sizeof(SYMBOL_INFO) + STACKWALK_MAX_NAMELEN] = {0};
62  SYMBOL_INFO* sym = (SYMBOL_INFO *)buf;
63  IMAGEHLP_MODULE64 Module = { 0 };
64  LONG RecursionDepth = 0;
65  sym->SizeOfStruct = sizeof(sym);
66 
67  /* FIXME: dump x bytes at EIP here + disasm it! */
68 
69  xfprintf(output, NEWLINE "*----> Stack Back Trace <----*" NEWLINE NEWLINE);
70  bool first = true;
71  while (StackWalk64(MachineType, data.ProcessHandle, thread.Handle, &StackFrame, &thread.Context,
73  {
74  if (StackFrame.AddrPC.Offset == StackFrame.AddrReturn.Offset)
75  {
76  if (RecursionDepth++ > STACK_MAX_RECURSION_DEPTH)
77  {
78  xfprintf(output, "- Aborting stackwalk -" NEWLINE);
79  break;
80  }
81  }
82  else
83  {
84  RecursionDepth = 0;
85  }
86 
87  if (first)
88  {
89  xfprintf(output, "FramePtr ReturnAd Param#1 Param#2 Param#3 Param#4 Function Name" NEWLINE);
90  first = false;
91  }
92 
93  Module.SizeOfStruct = sizeof(Module);
94  DWORD64 ModBase = SymGetModuleBase64(data.ProcessHandle, StackFrame.AddrPC.Offset);
95  if (!ModBase || !SymGetModuleInfo64(data.ProcessHandle, ModBase, &Module))
96  strcpy(Module.ModuleName, "<nomod>");
97 
98  memset(sym, '\0', sizeof(*sym) + STACKWALK_MAX_NAMELEN);
99  sym->SizeOfStruct = sizeof(*sym);
101  DWORD64 displacement = 0;
102 
103  if (!StackFrame.AddrPC.Offset || !SymFromAddr(data.ProcessHandle, StackFrame.AddrPC.Offset, &displacement, sym))
104  strcpy(sym->Name, "<nosymbols>");
105 
106  xfprintf(output, "%p %p %p %p %p %p %s!%s +0x%I64x" NEWLINE,
107  (ULONG_PTR)StackFrame.AddrFrame.Offset, (ULONG_PTR)StackFrame.AddrPC.Offset,
108  (ULONG_PTR)StackFrame.Params[0], (ULONG_PTR)StackFrame.Params[1],
109  (ULONG_PTR)StackFrame.Params[2], (ULONG_PTR)StackFrame.Params[3],
110  Module.ModuleName, sym->Name, displacement);
111  }
112 
113  UCHAR stackData[0x10 * 10];
114  SIZE_T sizeRead;
115 #if defined(_M_IX86)
116  ULONG_PTR stackPointer = thread.Context.Esp;
117 #elif defined(_M_AMD64)
118  ULONG_PTR stackPointer = thread.Context.Rsp;
119 #else
120 #error Unknown architecture
121 #endif
122  if (!ReadProcessMemory(data.ProcessHandle, (PVOID)stackPointer, stackData, sizeof(stackData), &sizeRead))
123  return;
124 
125  xfprintf(output, NEWLINE "*----> Raw Stack Dump <----*" NEWLINE NEWLINE);
126  for (size_t n = 0; n < sizeof(stackData); n += 0x10)
127  {
128  char HexData1[] = "?? ?? ?? ?? ?? ?? ?? ??";
129  char HexData2[] = "?? ?? ?? ?? ?? ?? ?? ??";
130  char AsciiData1[] = "????????";
131  char AsciiData2[] = "????????";
132 
133  for (size_t j = 0; j < 8; ++j)
134  {
135  size_t idx = j + n;
136  if (idx < sizeRead)
137  {
138  HexData1[j * 3] = ToChar(stackData[idx] >> 4);
139  HexData1[j * 3 + 1] = ToChar(stackData[idx] & 0xf);
140  AsciiData1[j] = isprint(stackData[idx]) ? stackData[idx] : '.';
141  }
142  idx += 8;
143  if (idx < sizeRead)
144  {
145  HexData2[j * 3] = ToChar(stackData[idx] >> 4);
146  HexData2[j * 3 + 1] = ToChar(stackData[idx] & 0xf);
147  AsciiData2[j] = isprint(stackData[idx]) ? stackData[idx] : '.';
148  }
149  }
150 
151  xfprintf(output, "%p %s - %s %s%s" NEWLINE, stackPointer+n, HexData1, HexData2, AsciiData1, AsciiData2);
152  }
153 }
BOOL WINAPI SymCleanup(HANDLE hProcess)
Definition: dbghelp.c:416
CHAR Name[1]
Definition: compat.h:694
GLenum GLclampf GLint GLenum GLuint GLenum GLenum GLsizei GLenum const GLvoid GLfloat GLfloat GLfloat GLfloat GLclampd GLint GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean GLboolean GLboolean GLboolean GLint GLenum GLsizei const GLvoid GLenum GLint GLenum GLint GLint GLsizei GLint GLenum GLint GLint GLint GLint GLsizei GLenum GLsizei const GLuint GLboolean GLenum GLenum GLint GLsizei GLenum GLsizei GLenum const GLvoid GLboolean const GLboolean GLenum const GLdouble const GLfloat const GLdouble const GLfloat GLenum GLint GLint GLint GLint GLint GLint j
Definition: glfuncs.h:98
#define TRUE
Definition: types.h:120
static UCHAR ULONG UCHAR ULONG UCHAR * output
Definition: bcrypt.c:29
ULONG Esp
Definition: nt_native.h:1479
void xfprintf(FILE *stream, const char *fmt,...)
Definition: main.cpp:34
ULONG MaxNameLen
Definition: compat.h:693
ULONG Eip
Definition: nt_native.h:1476
DWORD WINAPI SymSetOptions(DWORD opts)
Definition: dbghelp.c:443
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
const GLint * first
Definition: glext.h:5794
CHAR ModuleName[32]
Definition: compat.h:879
PVOID WINAPI SymFunctionTableAccess64(HANDLE, DWORD64)
Definition: module.c:1169
BOOL WINAPI StackWalk64(DWORD MachineType, HANDLE hProcess, HANDLE hThread, LPSTACKFRAME64 frame, PVOID ctx, PREAD_PROCESS_MEMORY_ROUTINE64 f_read_mem, PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine, PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine, PTRANSLATE_ADDRESS_ROUTINE64 f_xlat_adr)
Definition: stack.c:210
#define SYMOPT_AUTO_PUBLICS
Definition: compat.h:643
void PrintStackBacktrace(FILE *output, DumpData &data, ThreadData &thread)
Definition: stacktrace.cpp:36
DWORD64 Params[4]
Definition: compat.h:1041
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:415
#define IMAGE_FILE_MACHINE_AMD64
Definition: ntimage.h:17
#define SYMOPT_DEFERRED_LOADS
Definition: compat.h:638
uint32_t ULONG_PTR
Definition: typedefs.h:63
GLuint n
Definition: s_context.h:57
long LONG
Definition: pedump.c:60
unsigned int idx
Definition: utils.c:41
CONTEXT Context
Definition: drwtsn32.h:26
smooth NULL
Definition: ftsmooth.c:416
BOOL WINAPI SymInitialize(HANDLE hProcess, PCSTR UserSearchPath, BOOL fInvadeProcess)
Definition: dbghelp.c:393
DWORD SizeOfStruct
Definition: compat.h:872
#define IMAGE_FILE_MACHINE_I386
Definition: pedump.c:174
HANDLE Handle
Definition: drwtsn32.h:25
DWORD64 Offset
Definition: compat.h:808
struct _SYMBOL_INFO SYMBOL_INFO
ADDRESS64 AddrReturn
Definition: compat.h:1036
unsigned long DWORD
Definition: ntddk_ex.h:95
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
void EndStackBacktrace(DumpData &data)
Definition: stacktrace.cpp:22
#define SYMOPT_UNDNAME
Definition: compat.h:637
#define STACK_MAX_RECURSION_DEPTH
Definition: stacktrace.cpp:11
static HANDLE thread
Definition: service.c:33
unsigned char UCHAR
Definition: xmlstorage.h:181
ADDRESS64 AddrStack
Definition: compat.h:1038
void BeginStackBacktrace(DumpData &data)
Definition: stacktrace.cpp:14
DWORD64 WINAPI SymGetModuleBase64(HANDLE, DWORD64)
Definition: module.c:1107
ULONG SizeOfStruct
Definition: compat.h:680
BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address, DWORD64 *Displacement, PSYMBOL_INFO Symbol)
Definition: symbol.c:1242
ULONG_PTR SIZE_T
Definition: typedefs.h:78
uint64_t DWORD64
Definition: typedefs.h:65
#define STACKWALK_MAX_NAMELEN
ADDRESS64 AddrPC
Definition: compat.h:1035
DWORD WINAPI SymGetOptions(void)
Definition: dbghelp.c:458
#define isprint(c)
Definition: acclib.h:73
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define NEWLINE
Definition: drwtsn32.h:53
#define memset(x, y, z)
Definition: compat.h:39
ADDRESS_MODE Mode
Definition: compat.h:810
HANDLE ProcessHandle
Definition: drwtsn32.h:43
ULONG Ebp
Definition: nt_native.h:1475
BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr, PIMAGEHLP_MODULE64 ModuleInfo)
Definition: module.c:1013
ADDRESS64 AddrFrame
Definition: compat.h:1037
static char ToChar(UCHAR data)
Definition: stacktrace.cpp:27