Repository navigation
Expand file tree
/
Copy pathprint_stacktrace.cpp
More file actions
352 lines (292 loc) · 11.3 KB
/
Copy pathprint_stacktrace.cpp
File metadata and controls
352 lines (292 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "hook.h"
#include "winnt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbghelp.h>
#include <cstdint>
#define MAX_SYMBOLS 10000
#define MAX_STRING_LENGTH 256
#define MAX_FIELDS 10
// Structure to hold symbol information from CSV
typedef struct {
char name[MAX_STRING_LENGTH];
char symbol_type[MAX_STRING_LENGTH];
char namespace_name[MAX_STRING_LENGTH];
char source[MAX_STRING_LENGTH];
} CSV_SYMBOL_INFO;
// Structure for symbol map entry
typedef struct {
DWORD64 offset;
CSV_SYMBOL_INFO symbol_info;
} SYMBOL_MAP_ENTRY;
// Global symbol storage
static SYMBOL_MAP_ENTRY g_symbolMap[MAX_SYMBOLS];
static int g_symbolCount = 0;
static BOOL g_csvLoaded = FALSE;
// Structure to hold module information
typedef struct {
HMODULE hModule;
char szModuleName[MAX_PATH];
LPVOID lpBaseOfDll;
DWORD SizeOfImage;
} MODULE_INFO;
// Helper function to trim whitespace
void TrimWhitespace(char* str) {
char* end;
// Trim leading space
while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') str++;
// All spaces?
if (*str == 0) return;
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r')) end--;
// Write new null terminator
*(end + 1) = 0;
}
// Parse a CSV line, handling potential commas in quoted fields
int ParseCSVLine(const char* line, char fields[][MAX_STRING_LENGTH]) {
int fieldCount = 0;
int fieldIndex = 0;
BOOL inQuotes = FALSE;
int lineLen = strlen(line);
if (fieldCount >= MAX_FIELDS) return fieldCount;
for (int i = 0; i < lineLen && fieldCount < MAX_FIELDS; i++) {
char c = line[i];
if (c == '"') {
inQuotes = !inQuotes;
} else if (c == ',' && !inQuotes) {
fields[fieldCount][fieldIndex] = '\0';
TrimWhitespace(fields[fieldCount]);
fieldCount++;
fieldIndex = 0;
} else {
if (fieldIndex < MAX_STRING_LENGTH - 1) {
fields[fieldCount][fieldIndex] = c;
fieldIndex++;
}
}
}
// Add the last field
if (fieldCount < MAX_FIELDS) {
fields[fieldCount][fieldIndex] = '\0';
TrimWhitespace(fields[fieldCount]);
fieldCount++;
}
return fieldCount;
}
// Load CSV symbol file
BOOL LoadSymbolCSV(const char* csvPath) {
FILE* file = fopen(csvPath, "r");
if (!file) {
LogMessage("Failed to open CSV file: %s", csvPath);
return FALSE;
}
g_symbolCount = 0;
char line[1024];
BOOL isFirstLine = TRUE;
LogMessage("Loading symbols from CSV: %s", csvPath);
LogMessage("Base address: 0x%llx", (unsigned long long)g_whgame_base);
while (fgets(line, sizeof(line), file) && g_symbolCount < MAX_SYMBOLS) {
// Skip header line
if (isFirstLine) {
isFirstLine = FALSE;
continue;
}
// Skip empty lines
if (strlen(line) <= 1) {
continue;
}
char fields[MAX_FIELDS][MAX_STRING_LENGTH];
int fieldCount = ParseCSVLine(line, fields);
// Ensure we have enough fields
if (fieldCount < 6) {
LogMessage("Warning: Invalid CSV line (not enough fields): %s", line);
continue;
}
// Parse hex_offset (field index 1)
DWORD64 offset = 0;
if (sscanf(fields[1], "%llx", (unsigned long long*)&offset) != 1) {
LogMessage("Warning: Invalid hex offset: %s", fields[1]);
continue;
}
// Store symbol information
g_symbolMap[g_symbolCount].offset = offset;
strncpy(g_symbolMap[g_symbolCount].symbol_info.name, fields[2], MAX_STRING_LENGTH - 1);
strncpy(g_symbolMap[g_symbolCount].symbol_info.symbol_type, fields[3], MAX_STRING_LENGTH - 1);
strncpy(g_symbolMap[g_symbolCount].symbol_info.namespace_name, fields[4], MAX_STRING_LENGTH - 1);
strncpy(g_symbolMap[g_symbolCount].symbol_info.source, fields[5], MAX_STRING_LENGTH - 1);
// Ensure null termination
g_symbolMap[g_symbolCount].symbol_info.name[MAX_STRING_LENGTH - 1] = '\0';
g_symbolMap[g_symbolCount].symbol_info.symbol_type[MAX_STRING_LENGTH - 1] = '\0';
g_symbolMap[g_symbolCount].symbol_info.namespace_name[MAX_STRING_LENGTH - 1] = '\0';
g_symbolMap[g_symbolCount].symbol_info.source[MAX_STRING_LENGTH - 1] = '\0';
g_symbolCount++;
}
fclose(file);
g_csvLoaded = TRUE;
LogMessage("Loaded %d symbols from CSV", g_symbolCount);
return TRUE;
}
// Get module name from address
BOOL GetModuleFromAddress(LPVOID address, char* moduleName, size_t nameSize) {
HMODULE hMods[1024];
DWORD cbNeeded;
if (!EnumProcessModules(GetCurrentProcess(), hMods, sizeof(hMods), &cbNeeded)) {
return FALSE;
}
for (DWORD i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) {
MODULEINFO modInfo;
if (GetModuleInformation(GetCurrentProcess(), hMods[i], &modInfo, sizeof(modInfo))) {
LPVOID modStart = modInfo.lpBaseOfDll;
LPVOID modEnd = (LPVOID)((BYTE*)modStart + modInfo.SizeOfImage);
if (address >= modStart && address < modEnd) {
if (GetModuleBaseNameA(GetCurrentProcess(), hMods[i], moduleName, (DWORD)nameSize)) {
return TRUE;
}
}
}
}
strcpy_s(moduleName, nameSize, "Unknown");
return FALSE;
}
// Initialize symbol handler (call once at startup)
BOOL InitializeSymbolHandler() {
HANDLE hProcess = GetCurrentProcess();
if (!SymInitialize(hProcess, NULL, TRUE)) {
return FALSE;
}
// Set symbol options for better symbol resolution
SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES);
return TRUE;
}
// Initialize symbols with CSV file
BOOL InitializeSymbolsWithCSV(const char* csvPath) {
// Initialize traditional symbol handler
if (!InitializeSymbolHandler()) {
LogMessage("Warning: Failed to initialize symbol handler");
}
// Load CSV symbols
return LoadSymbolCSV(csvPath);
}
// Cleanup symbol handler (call at shutdown)
void CleanupSymbolHandler() {
SymCleanup(GetCurrentProcess());
g_symbolCount = 0;
g_csvLoaded = FALSE;
}
// Print stack trace with module information and CSV symbol lookup
void PrintStackTrace() {
LogMessage("=== DETAILED CALLER STACK TRACE ===");
void* stack[35];
WORD frames = CaptureStackBackTrace(1, 35, stack, NULL);
for (WORD i = 0; i < frames; i++) {
DWORD64 address = (DWORD64)stack[i];
// Get module name
char moduleName[MAX_PATH];
GetModuleFromAddress((LPVOID)address, moduleName, sizeof(moduleName));
char symbolName[256] = "???";
DWORD64 displacement = 0;
BOOL foundInCSV = FALSE;
const char* sourceMarker = "[SYM]";
// Debug info for WHGame.dll addresses
if (strstr(moduleName, "WHGame") != NULL) {
DWORD64 offset = address - (DWORD64)g_whgame_base;
if (g_csvLoaded) {
// Try CSV lookup
int bestMatch = -1;
DWORD64 smallestDistance = UINT64_MAX;
for (int j = 0; j < g_symbolCount; j++) {
if (g_symbolMap[j].offset <= offset) {
DWORD64 distance = offset - g_symbolMap[j].offset;
if (distance < smallestDistance) {
smallestDistance = distance;
bestMatch = j;
}
}
}
if (bestMatch >= 0) {
if (smallestDistance <= 0x10000) {
const CSV_SYMBOL_INFO* symbol = &g_symbolMap[bestMatch].symbol_info;
if (strlen(symbol->namespace_name) > 0 && strcmp(symbol->namespace_name, "Global") != 0) {
_snprintf_s(symbolName, sizeof(symbolName), _TRUNCATE, "%s::%s",
symbol->namespace_name, symbol->name);
} else {
_snprintf_s(symbolName, sizeof(symbolName), _TRUNCATE, "%s", symbol->name);
}
displacement = smallestDistance;
foundInCSV = TRUE;
sourceMarker = "[CSV]";
}
}
}
}
// Fall back to traditional symbol lookup
if (!foundInCSV) {
char symbolBuffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
SYMBOL_INFO* symbol = (SYMBOL_INFO*)symbolBuffer;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;
HANDLE hProcess = GetCurrentProcess();
if (SymFromAddr(hProcess, address, &displacement, symbol)) {
strncpy_s(symbolName, sizeof(symbolName), symbol->Name, _TRUNCATE);
}
}
LogMessage("[%02d] 0x%p (%s) %s %s+0x%llx",
i,
(void*)address,
moduleName,
sourceMarker,
symbolName,
(unsigned long long)displacement);
}
LogMessage("=== END DETAILED STACK TRACE ===");
}
// Utility function to find the nearest symbol for debugging
void FindNearestSymbol(DWORD64 address) {
if (!g_csvLoaded || g_whgame_base == 0) {
LogMessage("CSV symbols not loaded");
return;
}
DWORD64 offset = address - (DWORD64)g_whgame_base;
int bestMatch = -1;
DWORD64 smallestDistance = UINT64_MAX;
LogMessage("=== NEAREST SYMBOL LOOKUP ===");
LogMessage("Address: 0x%llx, Offset: 0x%llx", (unsigned long long)address, (unsigned long long)offset);
// Find exact match first
for (int i = 0; i < g_symbolCount; i++) {
if (g_symbolMap[i].offset == offset) {
const CSV_SYMBOL_INFO* symbol = &g_symbolMap[i].symbol_info;
LogMessage("Exact match found:");
if (strlen(symbol->namespace_name) > 0 && strcmp(symbol->namespace_name, "Global") != 0) {
LogMessage(" %s::%s() at offset 0x%llx",
symbol->namespace_name, symbol->name, (unsigned long long)g_symbolMap[i].offset);
} else {
LogMessage(" %s() at offset 0x%llx", symbol->name, (unsigned long long)g_symbolMap[i].offset);
}
return;
}
}
// Find nearest symbol (before the address)
for (int i = 0; i < g_symbolCount; i++) {
if (g_symbolMap[i].offset <= offset) {
DWORD64 distance = offset - g_symbolMap[i].offset;
if (distance < smallestDistance) {
smallestDistance = distance;
bestMatch = i;
}
}
}
if (bestMatch >= 0) {
const CSV_SYMBOL_INFO* symbol = &g_symbolMap[bestMatch].symbol_info;
LogMessage("Nearest symbol (before):");
if (strlen(symbol->namespace_name) > 0 && strcmp(symbol->namespace_name, "Global") != 0) {
LogMessage(" %s::%s() + 0x%llx",
symbol->namespace_name, symbol->name, (unsigned long long)smallestDistance);
} else {
LogMessage(" %s() + 0x%llx", symbol->name, (unsigned long long)smallestDistance);
}
} else {
LogMessage("No symbols found");
}
}