ReactOS  0.4.12-dev-43-g63b00d8
stacktrace.cpp File Reference
#include "precomp.h"
#include <dbghelp.h>
Include dependency graph for stacktrace.cpp:

Go to the source code of this file.

Macros

#define STACK_MAX_RECURSION_DEPTH   50
 
#define STACKWALK_MAX_NAMELEN   512
 

Functions

void BeginStackBacktrace (DumpData &data)
 
void EndStackBacktrace (DumpData &data)
 
static char ToChar (UCHAR data)
 
void PrintStackBacktrace (FILE *output, DumpData &data, ThreadData &thread)
 

Macro Definition Documentation

◆ STACK_MAX_RECURSION_DEPTH

#define STACK_MAX_RECURSION_DEPTH   50

Definition at line 11 of file stacktrace.cpp.

Referenced by PrintStackBacktrace().

◆ STACKWALK_MAX_NAMELEN

#define STACKWALK_MAX_NAMELEN   512

Referenced by PrintStackBacktrace().

Function Documentation

◆ BeginStackBacktrace()

void BeginStackBacktrace ( DumpData data)

Definition at line 14 of file stacktrace.cpp.

Referenced by PrintBugreport().

15 {
16  DWORD symOptions = SymGetOptions();
18  SymSetOptions(symOptions);
20 }
#define TRUE
Definition: types.h:120
DWORD WINAPI SymSetOptions(DWORD opts)
Definition: dbghelp.c:443
#define SYMOPT_AUTO_PUBLICS
Definition: compat.h:643
#define SYMOPT_DEFERRED_LOADS
Definition: compat.h:638
smooth NULL
Definition: ftsmooth.c:416
BOOL WINAPI SymInitialize(HANDLE hProcess, PCSTR UserSearchPath, BOOL fInvadeProcess)
Definition: dbghelp.c:393
unsigned long DWORD
Definition: ntddk_ex.h:95
#define SYMOPT_UNDNAME
Definition: compat.h:637
DWORD WINAPI SymGetOptions(void)
Definition: dbghelp.c:458
HANDLE ProcessHandle
Definition: drwtsn32.h:43

◆ EndStackBacktrace()

void EndStackBacktrace ( DumpData data)

Definition at line 22 of file stacktrace.cpp.

Referenced by PrintBugreport().

23 {
25 }
BOOL WINAPI SymCleanup(HANDLE hProcess)
Definition: dbghelp.c:416
HANDLE ProcessHandle
Definition: drwtsn32.h:43

◆ PrintStackBacktrace()

void PrintStackBacktrace ( FILE output,
DumpData data,
ThreadData thread 
)

Definition at line 36 of file stacktrace.cpp.

Referenced by PrintThread().

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 }
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
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
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
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
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
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
#define STACK_MAX_RECURSION_DEPTH
Definition: stacktrace.cpp:11
unsigned char UCHAR
Definition: xmlstorage.h:181
ADDRESS64 AddrStack
Definition: compat.h:1038
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
#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

◆ ToChar()

static char ToChar ( UCHAR  data)
static

Definition at line 27 of file stacktrace.cpp.

Referenced by PrintStackBacktrace().

28 {
29  if (data < 0xa)
30  return '0' + data;
31  else if (data <= 0xf)
32  return 'a' + data - 0xa;
33  return '?';
34 }
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950