ReactOS 0.4.15-dev-8408-g466a198
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.

◆ STACKWALK_MAX_NAMELEN

#define STACKWALK_MAX_NAMELEN   512

Function Documentation

◆ BeginStackBacktrace()

void BeginStackBacktrace ( DumpData data)

Definition at line 14 of file stacktrace.cpp.

15{
16 DWORD symOptions = SymGetOptions();
18 SymSetOptions(symOptions);
19 SymInitialize(data.ProcessHandle, NULL, TRUE);
20}
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
BOOL WINAPI SymInitialize(HANDLE hProcess, PCSTR UserSearchPath, BOOL fInvadeProcess)
Definition: dbghelp.c:534
#define SYMOPT_AUTO_PUBLICS
Definition: compat.h:994
#define SYMOPT_DEFERRED_LOADS
Definition: compat.h:989
#define SYMOPT_UNDNAME
Definition: compat.h:988
DWORD WINAPI SymSetOptions(DWORD opts)
Definition: dbghelp.c:585
DWORD WINAPI SymGetOptions(void)
Definition: dbghelp.c:600
unsigned long DWORD
Definition: ntddk_ex.h:95
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950

Referenced by PrintBugreport().

◆ EndStackBacktrace()

void EndStackBacktrace ( DumpData data)

Definition at line 22 of file stacktrace.cpp.

23{
24 SymCleanup(data.ProcessHandle);
25}
BOOL WINAPI SymCleanup(HANDLE hProcess)
Definition: dbghelp.c:557

Referenced by PrintBugreport().

◆ PrintStackBacktrace()

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

Definition at line 36 of file stacktrace.cpp.

37{
38 DWORD MachineType;
39 STACKFRAME64 StackFrame = { { 0 } };
40
41 StackFrame.AddrPC.Mode = AddrModeFlat;
42 StackFrame.AddrReturn.Mode = AddrModeFlat;
43 StackFrame.AddrFrame.Mode = AddrModeFlat;
44 StackFrame.AddrStack.Mode = AddrModeFlat;
45 StackFrame.AddrBStore.Mode = AddrModeFlat;
46
47
48#if defined(_M_IX86)
49 MachineType = IMAGE_FILE_MACHINE_I386;
50 StackFrame.AddrPC.Offset = thread.Context.Eip;
51 StackFrame.AddrStack.Offset = thread.Context.Esp;
52 StackFrame.AddrFrame.Offset = thread.Context.Ebp;
53#elif defined(_M_AMD64)
54 MachineType = IMAGE_FILE_MACHINE_AMD64;
55 StackFrame.AddrPC.Offset = thread.Context.Rip;
56 StackFrame.AddrStack.Offset = thread.Context.Rsp;
57 StackFrame.AddrFrame.Offset = thread.Context.Rbp;
58#elif defined(_M_ARM)
59 MachineType = IMAGE_FILE_MACHINE_ARMNT;
60 StackFrame.AddrPC.Offset = thread.Context.Pc;
61 StackFrame.AddrStack.Offset = thread.Context.Sp;
62 StackFrame.AddrFrame.Offset = thread.Context.R11;
63#elif defined(_M_ARM64)
64 MachineType = IMAGE_FILE_MACHINE_ARM64;
65 StackFrame.AddrPC.Offset = thread.Context.Pc;
66 StackFrame.AddrStack.Offset = thread.Context.Sp;
67 StackFrame.AddrFrame.Offset = thread.Context.u.s.Fp;
68#else
69#error "Unknown architecture"
70#endif
71
72#define STACKWALK_MAX_NAMELEN 512
73 char buf[sizeof(SYMBOL_INFO) + STACKWALK_MAX_NAMELEN] = {0};
74 SYMBOL_INFO* sym = (SYMBOL_INFO *)buf;
75 IMAGEHLP_MODULE64 Module = { 0 };
76 LONG RecursionDepth = 0;
77 sym->SizeOfStruct = sizeof(sym);
78
79 /* FIXME: dump x bytes at PC here + disasm it! */
80
81 xfprintf(output, NEWLINE "*----> Stack Back Trace <----*" NEWLINE NEWLINE);
82 bool first = true;
83 while (StackWalk64(MachineType, data.ProcessHandle, thread.Handle, &StackFrame, &thread.Context,
85 {
86 if (StackFrame.AddrPC.Offset == StackFrame.AddrReturn.Offset)
87 {
88 if (RecursionDepth++ > STACK_MAX_RECURSION_DEPTH)
89 {
90 xfprintf(output, "- Aborting stackwalk -" NEWLINE);
91 break;
92 }
93 }
94 else
95 {
96 RecursionDepth = 0;
97 }
98
99 if (first)
100 {
101 xfprintf(output, "FramePtr ReturnAd Param#1 Param#2 Param#3 Param#4 Function Name" NEWLINE);
102 first = false;
103 }
104
105 Module.SizeOfStruct = sizeof(Module);
106 DWORD64 ModBase = SymGetModuleBase64(data.ProcessHandle, StackFrame.AddrPC.Offset);
107 if (!ModBase || !SymGetModuleInfo64(data.ProcessHandle, ModBase, &Module))
108 strcpy(Module.ModuleName, "<nomod>");
109
110 memset(sym, '\0', sizeof(*sym) + STACKWALK_MAX_NAMELEN);
111 sym->SizeOfStruct = sizeof(*sym);
113 DWORD64 displacement = 0;
114
115 if (!StackFrame.AddrPC.Offset || !SymFromAddr(data.ProcessHandle, StackFrame.AddrPC.Offset, &displacement, sym))
116 strcpy(sym->Name, "<nosymbols>");
117
118 xfprintf(output, "%p %p %p %p %p %p %s!%s +0x%I64x" NEWLINE,
119 (ULONG_PTR)StackFrame.AddrFrame.Offset, (ULONG_PTR)StackFrame.AddrPC.Offset,
120 (ULONG_PTR)StackFrame.Params[0], (ULONG_PTR)StackFrame.Params[1],
121 (ULONG_PTR)StackFrame.Params[2], (ULONG_PTR)StackFrame.Params[3],
122 Module.ModuleName, sym->Name, displacement);
123 }
124
125 UCHAR stackData[0x10 * 10];
126 SIZE_T sizeRead;
127#if defined(_M_IX86)
128 ULONG_PTR stackPointer = thread.Context.Esp;
129#elif defined(_M_AMD64)
130 ULONG_PTR stackPointer = thread.Context.Rsp;
131#elif defined(_M_ARM) || defined(_M_ARM64)
132 ULONG_PTR stackPointer = thread.Context.Sp;
133#else
134#error Unknown architecture
135#endif
136 if (!ReadProcessMemory(data.ProcessHandle, (PVOID)stackPointer, stackData, sizeof(stackData), &sizeRead))
137 return;
138
139 xfprintf(output, NEWLINE "*----> Raw Stack Dump <----*" NEWLINE NEWLINE);
140 for (size_t n = 0; n < sizeof(stackData); n += 0x10)
141 {
142 char HexData1[] = "?? ?? ?? ?? ?? ?? ?? ??";
143 char HexData2[] = "?? ?? ?? ?? ?? ?? ?? ??";
144 char AsciiData1[] = "????????";
145 char AsciiData2[] = "????????";
146
147 for (size_t j = 0; j < 8; ++j)
148 {
149 size_t idx = j + n;
150 if (idx < sizeRead)
151 {
152 HexData1[j * 3] = ToChar(stackData[idx] >> 4);
153 HexData1[j * 3 + 1] = ToChar(stackData[idx] & 0xf);
154 AsciiData1[j] = isprint(stackData[idx]) ? stackData[idx] : '.';
155 }
156 idx += 8;
157 if (idx < sizeRead)
158 {
159 HexData2[j * 3] = ToChar(stackData[idx] >> 4);
160 HexData2[j * 3 + 1] = ToChar(stackData[idx] & 0xf);
161 AsciiData2[j] = isprint(stackData[idx]) ? stackData[idx] : '.';
162 }
163 }
164
165 xfprintf(output, "%p %s - %s %s%s" NEWLINE, stackPointer+n, HexData1, HexData2, AsciiData1, AsciiData2);
166 }
167}
#define isprint(c)
Definition: acclib.h:73
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
static HANDLE thread
Definition: service.c:33
unsigned int idx
Definition: utils.c:41
BOOL WINAPI SymFromAddr(HANDLE hProcess, DWORD64 Address, DWORD64 *Displacement, PSYMBOL_INFO Symbol)
Definition: symbol.c:1263
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:758
#define IMAGE_FILE_MACHINE_ARMNT
Definition: compat.h:127
PVOID WINAPI SymFunctionTableAccess64(HANDLE, DWORD64)
Definition: module.c:1371
DWORD64 WINAPI SymGetModuleBase64(HANDLE, DWORD64)
Definition: module.c:1313
@ AddrModeFlat
Definition: compat.h:1159
struct _SYMBOL_INFO SYMBOL_INFO
#define IMAGE_FILE_MACHINE_ARM64
Definition: compat.h:129
BOOL WINAPI SymGetModuleInfo64(HANDLE hProcess, DWORD64 dwAddr, PIMAGEHLP_MODULE64 ModuleInfo)
Definition: module.c:1219
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 NEWLINE
Definition: drwtsn32.h:53
void xfprintf(FILE *stream, const char *fmt,...)
Definition: main.cpp:34
GLdouble n
Definition: glext.h:7729
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
const GLint * first
Definition: glext.h:5794
GLsizei GLenum const GLvoid GLsizei GLenum 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 const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define IMAGE_FILE_MACHINE_AMD64
Definition: ntimage.h:17
#define IMAGE_FILE_MACHINE_I386
Definition: pedump.c:174
long LONG
Definition: pedump.c:60
#define memset(x, y, z)
Definition: compat.h:39
#define STACK_MAX_RECURSION_DEPTH
Definition: stacktrace.cpp:11
#define STACKWALK_MAX_NAMELEN
static char ToChar(UCHAR data)
Definition: stacktrace.cpp:27
DWORD SizeOfStruct
Definition: compat.h:1238
CHAR ModuleName[32]
Definition: compat.h:1245
ADDRESS64 AddrBStore
Definition: compat.h:1405
ADDRESS64 AddrStack
Definition: compat.h:1404
ADDRESS64 AddrFrame
Definition: compat.h:1403
ADDRESS64 AddrPC
Definition: compat.h:1401
DWORD64 Params[4]
Definition: compat.h:1407
ADDRESS64 AddrReturn
Definition: compat.h:1402
ULONG SizeOfStruct
Definition: compat.h:1038
CHAR Name[1]
Definition: compat.h:1052
ULONG MaxNameLen
Definition: compat.h:1051
ADDRESS_MODE Mode
Definition: compat.h:1176
DWORD64 Offset
Definition: compat.h:1174
uint64_t DWORD64
Definition: typedefs.h:67
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint32_t ULONG_PTR
Definition: typedefs.h:65
unsigned char UCHAR
Definition: xmlstorage.h:181

Referenced by PrintThread().

◆ ToChar()

static char ToChar ( UCHAR  data)
static

Definition at line 27 of file stacktrace.cpp.

28{
29 if (data < 0xa)
30 return '0' + data;
31 else if (data <= 0xf)
32 return 'a' + data - 0xa;
33 return '?';
34}

Referenced by PrintStackBacktrace().