1
- #include < vector>
1
+ #include " common.h"
2
+
3
+ #include < dbghelp.h>
4
+ #include < inttypes.h>
2
5
#include < string>
6
+ #include < vector>
3
7
#include < windows.h>
4
- #include < inttypes.h>
5
- #include < dbghelp.h>
6
- #include " common.h"
7
8
8
- #define SLANG_EXAMPLE_LOG_ERROR (...) \
9
- printf (" error: %s: %d: " , __FILE__, __LINE__); \
10
- print (__VA_ARGS__); \
9
+ #define SLANG_EXAMPLE_LOG_ERROR (...) \
10
+ printf (" error: %s: %d: " , __FILE__, __LINE__); \
11
+ print (__VA_ARGS__); \
11
12
printf (" \n " );
12
13
13
14
static void print () {}
@@ -21,19 +22,16 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN
21
22
{
22
23
HMODULE module = NULL ;
23
24
{
24
- BOOL result =
25
- GetModuleHandleEx (
26
- GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
27
- GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
28
- (LPCTSTR)address,
29
- &module
30
- );
31
- if (result == 0 )
25
+ BOOL result = GetModuleHandleEx (
26
+ GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
27
+ (LPCTSTR)address,
28
+ &module);
29
+ if (result == 0 )
32
30
{
33
31
SLANG_EXAMPLE_LOG_ERROR (GetLastError ());
34
32
return false ;
35
33
}
36
- if (module == NULL )
34
+ if (module == NULL )
37
35
{
38
36
SLANG_EXAMPLE_LOG_ERROR ();
39
37
return false ;
@@ -42,15 +40,15 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN
42
40
43
41
std::vector<char > buffer (1U << 8U );
44
42
uint32_t constexpr maxBufferSize = 1U << 20 ;
45
- while (buffer.size () < maxBufferSize)
43
+ while (buffer.size () < maxBufferSize)
46
44
{
47
45
DWORD result = GetModuleFileNameA (module, buffer.data (), buffer.size ());
48
- if (result == 0 )
46
+ if (result == 0 )
49
47
{
50
48
SLANG_EXAMPLE_LOG_ERROR (GetLastError ());
51
49
return false ;
52
50
}
53
- else if (result == ERROR_INSUFFICIENT_BUFFER)
51
+ else if (result == ERROR_INSUFFICIENT_BUFFER)
54
52
{
55
53
buffer.resize (buffer.size () << 1U );
56
54
}
@@ -59,7 +57,7 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN
59
57
break ;
60
58
}
61
59
}
62
- if (buffer.size () == maxBufferSize)
60
+ if (buffer.size () == maxBufferSize)
63
61
{
64
62
SLANG_EXAMPLE_LOG_ERROR ();
65
63
return false ;
@@ -87,26 +85,24 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
87
85
STACKFRAME64 frame = {};
88
86
constexpr uint32_t maxFrameCount = 1U << 10 ;
89
87
uint32_t frameIndex = 0U ;
90
- while (frameIndex < maxFrameCount)
88
+ while (frameIndex < maxFrameCount)
91
89
{
92
90
// Use the default routine
93
91
PREAD_PROCESS_MEMORY_ROUTINE64 readMemoryRoutine = NULL ;
94
92
// Not sure what this is for, but documentation says most callers can pass NULL
95
93
PTRANSLATE_ADDRESS_ROUTINE64 translateAddressRoutine = NULL ;
96
94
{
97
- BOOL result =
98
- StackWalk64 (
99
- machineType,
100
- process,
101
- thread,
102
- &frame,
103
- &contextCopy,
104
- readMemoryRoutine,
105
- SymFunctionTableAccess64,
106
- SymGetModuleBase64,
107
- translateAddressRoutine
108
- );
109
- if (result == FALSE )
95
+ BOOL result = StackWalk64 (
96
+ machineType,
97
+ process,
98
+ thread,
99
+ &frame,
100
+ &contextCopy,
101
+ readMemoryRoutine,
102
+ SymFunctionTableAccess64,
103
+ SymGetModuleBase64,
104
+ translateAddressRoutine);
105
+ if (result == FALSE )
110
106
break ;
111
107
}
112
108
@@ -117,9 +113,8 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
117
113
DWORD64 address = frame.AddrPC .Offset ;
118
114
// Not required, we want to look up the symbol exactly at the address
119
115
PDWORD64 displacement = NULL ;
120
- BOOL result =
121
- SymFromAddr (process, address, displacement, maybeSymbol);
122
- if (result == FALSE )
116
+ BOOL result = SymFromAddr (process, address, displacement, maybeSymbol);
117
+ if (result == FALSE )
123
118
{
124
119
SLANG_EXAMPLE_LOG_ERROR (GetLastError ());
125
120
maybeSymbol = NULL ;
@@ -129,10 +124,10 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
129
124
printf (" %u" , frameIndex);
130
125
131
126
std::string moduleFileName;
132
- if (getModuleFileNameAtAddress (frame.AddrPC .Offset , moduleFileName))
127
+ if (getModuleFileNameAtAddress (frame.AddrPC .Offset , moduleFileName))
133
128
printf (" : %s" , moduleFileName.c_str ());
134
129
135
- if (maybeSymbol)
130
+ if (maybeSymbol)
136
131
{
137
132
PSYMBOL_INFO& symbol = maybeSymbol;
138
133
@@ -142,8 +137,7 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
142
137
DWORD displacement;
143
138
if (SymGetLineFromAddr64 (process, frame.AddrPC .Offset , &displacement, &line))
144
139
{
145
- printf (" : %s: %s: %lu" ,
146
- symbol->Name , line.FileName , line.LineNumber );
140
+ printf (" : %s: %s: %lu" , symbol->Name , line.FileName , line.LineNumber );
147
141
}
148
142
else
149
143
{
@@ -162,8 +156,9 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context)
162
156
163
157
int exceptionFilter (_EXCEPTION_POINTERS* exception )
164
158
{
165
- printf (" error: Exception 0x%x occurred. Stack trace:\n " ,
166
- exception ->ExceptionRecord ->ExceptionCode );
159
+ printf (
160
+ " error: Exception 0x%x occurred. Stack trace:\n " ,
161
+ exception ->ExceptionRecord ->ExceptionCode );
167
162
168
163
HANDLE process = GetCurrentProcess ();
169
164
HANDLE thread = GetCurrentThread ();
@@ -173,9 +168,8 @@ int exceptionFilter(_EXCEPTION_POINTERS* exception)
173
168
// The default search paths should suffice
174
169
PCSTR symbolFileSearchPath = NULL ;
175
170
BOOL loadSymbolsOfLoadedModules = TRUE ;
176
- BOOL result =
177
- SymInitialize (process, symbolFileSearchPath, loadSymbolsOfLoadedModules);
178
- if (result == FALSE )
171
+ BOOL result = SymInitialize (process, symbolFileSearchPath, loadSymbolsOfLoadedModules);
172
+ if (result == FALSE )
179
173
{
180
174
printf (" warning: Failed to load symbols\n " );
181
175
}
@@ -185,15 +179,15 @@ int exceptionFilter(_EXCEPTION_POINTERS* exception)
185
179
}
186
180
}
187
181
188
- if (!printStack (process, thread, *exception ->ContextRecord ))
182
+ if (!printStack (process, thread, *exception ->ContextRecord ))
189
183
{
190
184
printf (" warning: Failed to print complete stack trace!\n " );
191
185
}
192
186
193
- if (symbolsLoaded)
187
+ if (symbolsLoaded)
194
188
{
195
189
BOOL result = SymCleanup (process);
196
- if (result == FALSE )
190
+ if (result == FALSE )
197
191
{
198
192
SLANG_EXAMPLE_LOG_ERROR (GetLastError ());
199
193
}
0 commit comments