Debian Codegraph @

src/symbols/symbols_with_dbghelp.cpp

raw
1 #ifdef CPPTRACE_GET_SYMBOLS_WITH_DBGHELP
2
3 #include <cpptrace/basic.hpp>
4 #include <cpptrace/utils.hpp>
5 #include "symbols/symbols.hpp"
6 #include "platform/dbghelp_utils.hpp"
7 #include "binary/object.hpp"
8 #include "utils/common.hpp"
9 #include "utils/error.hpp"
10 #include "utils/utils.hpp"
11 #include "options.hpp"
12 #include "logging.hpp"
13
14 #include <regex>
15 #include <system_error>
16 #include <vector>
17
18 #ifndef WIN32_LEAN_AND_MEAN
19 #define WIN32_LEAN_AND_MEAN
20 #endif
21 #include <windows.h>
22 #include <dbghelp.h>
23 #include <psapi.h>
24
25 CPPTRACE_BEGIN_NAMESPACE
26 namespace detail {
27 namespace dbghelp {
28 // SymFromAddr only returns the function's name. In order to get information about parameters,
29 // important for C++ stack traces where functions may be overloaded, we have to manually use
30 // Windows DIA to walk debug info structures. Resources:
31 // https://web.archive.org/web/20201027025750/http://www.debuginfo.com/articles/dbghelptypeinfo.html
32 // https://web.archive.org/web/20201203160805/http://www.debuginfo.com/articles/dbghelptypeinfofigures.html
33 // https://github.com/DynamoRIO/dynamorio/blob/master/ext/drsyms/drsyms_windows.c#L1370-L1439
34 // TODO: Currently unable to detect rvalue references
35 // TODO: Currently unable to detect const
36 enum class SymTagEnum {
37 SymTagNull, SymTagExe, SymTagCompiland, SymTagCompilandDetails, SymTagCompilandEnv,
38 SymTagFunction, SymTagBlock, SymTagData, SymTagAnnotation, SymTagLabel, SymTagPublicSymbol,
39 SymTagUDT, SymTagEnum, SymTagFunctionType, SymTagPointerType, SymTagArrayType,
40 SymTagBaseType, SymTagTypedef, SymTagBaseClass, SymTagFriend, SymTagFunctionArgType,
41 SymTagFuncDebugStart, SymTagFuncDebugEnd, SymTagUsingNamespace, SymTagVTableShape,
42 SymTagVTable, SymTagCustom, SymTagThunk, SymTagCustomType, SymTagManagedType,
43 SymTagDimension, SymTagCallSite, SymTagInlineSite, SymTagBaseInterface, SymTagVectorType,
44 SymTagMatrixType, SymTagHLSLType, SymTagCaller, SymTagCallee, SymTagExport,
45 SymTagHeapAllocationSite, SymTagCoffGroup, SymTagMax
46 };
47
48 enum class IMAGEHLP_SYMBOL_TYPE_INFO {
49 TI_GET_SYMTAG, TI_GET_SYMNAME, TI_GET_LENGTH, TI_GET_TYPE, TI_GET_TYPEID, TI_GET_BASETYPE,
50 TI_GET_ARRAYINDEXTYPEID, TI_FINDCHILDREN, TI_GET_DATAKIND, TI_GET_ADDRESSOFFSET,
51 TI_GET_OFFSET, TI_GET_VALUE, TI_GET_COUNT, TI_GET_CHILDRENCOUNT, TI_GET_BITPOSITION,
52 TI_GET_VIRTUALBASECLASS, TI_GET_VIRTUALTABLESHAPEID, TI_GET_VIRTUALBASEPOINTEROFFSET,
53 TI_GET_CLASSPARENTID, TI_GET_NESTED, TI_GET_SYMINDEX, TI_GET_LEXICALPARENT, TI_GET_ADDRESS,
54 TI_GET_THISADJUST, TI_GET_UDTKIND, TI_IS_EQUIV_TO, TI_GET_CALLING_CONVENTION,
55 TI_IS_CLOSE_EQUIV_TO, TI_GTIEX_REQS_VALID, TI_GET_VIRTUALBASEOFFSET,
56 TI_GET_VIRTUALBASEDISPINDEX, TI_GET_IS_REFERENCE, TI_GET_INDIRECTVIRTUALBASECLASS,
57 TI_GET_VIRTUALBASETABLETYPE, TI_GET_OBJECTPOINTERTYPE, IMAGEHLP_SYMBOL_TYPE_INFO_MAX
58 };
59
60 enum class BasicType {
61 btNoType = 0, btVoid = 1, btChar = 2, btWChar = 3, btInt = 6, btUInt = 7, btFloat = 8,
62 btBCD = 9, btBool = 10, btLong = 13, btULong = 14, btCurrency = 25, btDate = 26,
63 btVariant = 27, btComplex = 28, btBit = 29, btBSTR = 30, btHresult = 31
64 };
65
66 // SymGetTypeInfo utility
67 template<typename T, IMAGEHLP_SYMBOL_TYPE_INFO SymType, bool FAILABLE = false>
68 T get_info(ULONG type_index, HANDLE proc, ULONG64 modbase) {
69 T info;
70 if(
71 !SymGetTypeInfo(
72 proc,
73 modbase,
74 type_index,
75 static_cast<::IMAGEHLP_SYMBOL_TYPE_INFO>(SymType),
76 &info
77 )
78 ) {
79 if(FAILABLE) {
80 return (T)-1;
81 } else {
82 throw internal_error(
83 "SymGetTypeInfo failed: {}", std::system_error(GetLastError(), std::system_category()).what()
84 );
85 }
86 }
87 return info;
88 }
89
90 template<IMAGEHLP_SYMBOL_TYPE_INFO SymType, bool FAILABLE = false>
91 std::string get_info_wchar(ULONG type_index, HANDLE proc, ULONG64 modbase) {
92 WCHAR* info;
93 if(
94 !SymGetTypeInfo(proc, modbase, type_index, static_cast<::IMAGEHLP_SYMBOL_TYPE_INFO>(SymType), &info)
95 ) {
96 throw internal_error(
97 "SymGetTypeInfo failed: {}", std::system_error(GetLastError(), std::system_category()).what()
98 );
99 }
100 // special case to properly free a buffer and convert string to narrow chars, only used for
101 // TI_GET_SYMNAME
102 static_assert(
103 SymType == IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_SYMNAME,
104 "get_info_wchar called with unexpected IMAGEHLP_SYMBOL_TYPE_INFO"
105 );
106 std::wstring wstr(info);
107 std::string str;
108 str.reserve(wstr.size());
109 for(const auto c : wstr) {
110 str.push_back(static_cast<char>(c));
111 }
112 LocalFree(info);
113 return str;
114 }
115
116 // Translate basic types to string
117 static std::string get_basic_type(ULONG type_index, HANDLE proc, ULONG64 modbase) {
118 auto basic_type = get_info<BasicType, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_BASETYPE>(
119 type_index,
120 proc,
121 modbase
122 );
123 //auto length = get_info<ULONG64, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_LENGTH>(type_index, proc, modbase);
124 switch(basic_type) {
125 case BasicType::btNoType:
126 return "<no basic type>";
127 case BasicType::btVoid:
128 return "void";
129 case BasicType::btChar:
130 return "char";
131 case BasicType::btWChar:
132 return "wchar_t";
133 case BasicType::btInt:
134 return "int";
135 case BasicType::btUInt:
136 return "unsigned int";
137 case BasicType::btFloat:
138 return "float";
139 case BasicType::btBool:
140 return "bool";
141 case BasicType::btLong:
142 return "long";
143 case BasicType::btULong:
144 return "unsigned long";
145 default:
146 return "<unknown basic type>";
147 }
148 }
149
150 static std::string resolve_type(ULONG type_index, HANDLE proc, ULONG64 modbase);
151
152 struct class_name_result {
153 bool has_class_name;
154 std::string name;
155 };
156 // Helper for member pointers
157 static class_name_result lookup_class_name(ULONG type_index, HANDLE proc, ULONG64 modbase) {
158 DWORD class_parent_id = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_CLASSPARENTID, true>(
159 type_index,
160 proc,
161 modbase
162 );
163 if(class_parent_id == (DWORD)-1) {
164 return {false, ""};
165 } else {
166 return {true, resolve_type(class_parent_id, proc, modbase)};
167 }
168 }
169
170 struct type_result {
171 std::string base;
172 std::string extent;
173 };
174 // Resolve more complex types
175 // returns [base, extent]
176 static type_result lookup_type(ULONG type_index, HANDLE proc, ULONG64 modbase) {
177 auto tag = get_info<SymTagEnum, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_SYMTAG>(type_index, proc, modbase);
178 switch(tag) {
179 case SymTagEnum::SymTagBaseType:
180 return {get_basic_type(type_index, proc, modbase), ""};
181 case SymTagEnum::SymTagPointerType: {
182 DWORD underlying_type_id = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_TYPEID>(
183 type_index,
184 proc,
185 modbase
186 );
187 bool is_ref = get_info<BOOL, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_IS_REFERENCE>(
188 type_index,
189 proc,
190 modbase
191 );
192 std::string pp = is_ref ? "&" : "*"; // pointer punctuator
193 auto class_name_res = lookup_class_name(type_index, proc, modbase);
194 if(class_name_res.has_class_name) {
195 pp = class_name_res.name + "::" + pp;
196 }
197 const auto type = lookup_type(underlying_type_id, proc, modbase);
198 if(type.extent.empty()) {
199 return {type.base + (pp.size() > 1 ? " " : "") + pp, ""};
200 } else {
201 return {type.base + "(" + pp, ")" + type.extent};
202 }
203 }
204 case SymTagEnum::SymTagArrayType: {
205 DWORD underlying_type_id = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_TYPEID>(
206 type_index,
207 proc,
208 modbase
209 );
210 DWORD length = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_COUNT>(
211 type_index,
212 proc,
213 modbase
214 );
215 const auto type = lookup_type(underlying_type_id, proc, modbase);
216 return {type.base, "[" + std::to_string(length) + "]" + type.extent};
217 }
218 case SymTagEnum::SymTagFunctionType: {
219 DWORD return_type_id = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_TYPEID>(
220 type_index,
221 proc,
222 modbase
223 );
224 DWORD n_children = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_COUNT, true>(
225 type_index,
226 proc,
227 modbase
228 );
229 DWORD class_parent_id = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_CLASSPARENTID, true>(
230 type_index,
231 proc,
232 modbase
233 );
234 int n_ignore = class_parent_id != (DWORD)-1; // ignore this param
235 // this must be ignored before TI_FINDCHILDREN_PARAMS::Count is set, else error
236 n_children -= n_ignore;
237 // return type
238 const auto return_type = lookup_type(return_type_id, proc, modbase);
239 if(n_children == 0) {
240 return {return_type.base, "()" + return_type.extent};
241 } else {
242 // alignment should be fine
243 std::size_t sz = sizeof(TI_FINDCHILDREN_PARAMS) +
244 (n_children) * sizeof(TI_FINDCHILDREN_PARAMS::ChildId[0]);
245 TI_FINDCHILDREN_PARAMS* children = (TI_FINDCHILDREN_PARAMS*) new char[sz];
246 auto guard = scope_exit([&] {
247 delete[] (char*) children;
248 });
249 children->Start = 0;
250 children->Count = n_children;
251 if(
252 !SymGetTypeInfo(
253 proc, modbase, type_index,
254 static_cast<::IMAGEHLP_SYMBOL_TYPE_INFO>(
255 IMAGEHLP_SYMBOL_TYPE_INFO::TI_FINDCHILDREN
256 ),
257 children
258 )
259 ) {
260 throw internal_error(
261 "SymGetTypeInfo failed: {}",
262 std::system_error(GetLastError(), std::system_category()).what()
263 );
264 }
265 // get children type
266 std::string extent = "(";
267 if(children->Start != 0) {
268 throw internal_error("Error: children->Start == 0");
269 }
270 for(std::size_t i = 0; i < n_children; i++) {
271 extent += (i == 0 ? "" : ", ") + resolve_type(children->ChildId[i], proc, modbase);
272 }
273 extent += ")";
274 return {return_type.base, extent + return_type.extent};
275 }
276 }
277 case SymTagEnum::SymTagFunctionArgType: {
278 DWORD underlying_type_id =
279 get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_TYPEID>(type_index, proc, modbase);
280 return {resolve_type(underlying_type_id, proc, modbase), ""};
281 }
282 case SymTagEnum::SymTagTypedef:
283 case SymTagEnum::SymTagEnum:
284 case SymTagEnum::SymTagUDT:
285 case SymTagEnum::SymTagBaseClass:
286 return {
287 get_info_wchar<IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_SYMNAME>(type_index, proc, modbase), ""
288 };
289 default:
290 return {
291 "<unknown type " +
292 std::to_string(static_cast<std::underlying_type<SymTagEnum>::type>(tag)) +
293 ">",
294 ""
295 };
296 };
297 }
298
299 static std::string resolve_type(ULONG type_index, HANDLE proc, ULONG64 modbase) {
300 const auto type = lookup_type(type_index, proc, modbase);
301 return type.base + type.extent;
302 }
303
304 struct function_info {
305 HANDLE proc;
306 ULONG64 modbase;
307 int counter;
308 int n_children;
309 int n_ignore;
310 std::string str;
311 };
312
313 // Enumerates function parameters
314 static BOOL __stdcall enumerator_callback(
315 PSYMBOL_INFO symbol_info,
316 ULONG,
317 PVOID data
318 ) {
319 function_info* ctx = (function_info*)data;
320 if(ctx->counter++ >= ctx->n_children) {
321 return false;
322 }
323 if(ctx->n_ignore-- > 0) {
324 return true; // just skip
325 }
326 ctx->str += resolve_type(symbol_info->TypeIndex, ctx->proc, ctx->modbase);
327 if(ctx->counter < ctx->n_children) {
328 ctx->str += ", ";
329 }
330 return true;
331 }
332
333 // TODO: Handle backtrace_pcinfo calling the callback multiple times on inlined functions
334 stacktrace_frame resolve_frame(HANDLE proc, frame_ptr addr) {
335 // The get_frame_object_info() ends up being inexpensive, at on my machine
336 // debug release
337 // uncached trace resolution (29 frames) 1.9-2.1 ms 1.4-1.8 ms
338 // cached trace resolution (29 frames) 1.1-1.2 ms 0.2-0.4 ms
339 // get_frame_object_info() 0.001-0.002 ms 0.0003-0.0006 ms
340 // At some point it might make sense to make an option to control this.
341 auto object_frame = get_frame_object_info(addr);
342 // Dbghelp is is single-threaded, so acquire a lock.
343 auto lock = get_dbghelp_lock();
344 alignas(SYMBOL_INFO) char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
345 SYMBOL_INFO* symbol = (SYMBOL_INFO*)buffer;
346 symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
347 symbol->MaxNameLen = MAX_SYM_NAME;
348 union { DWORD64 a; DWORD b; } displacement;
349 IMAGEHLP_LINE line;
350 bool got_line = SymGetLineFromAddr(proc, addr, &displacement.b, &line);
351 if(SymFromAddr(proc, addr, &displacement.a, symbol)) {
352 if(got_line) {
353 IMAGEHLP_STACK_FRAME frame;
354 frame.InstructionOffset = symbol->Address;
355 // https://docs.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-symsetcontext
356 // "If you call SymSetContext to set the context to its current value, the
357 // function fails but GetLastError returns ERROR_SUCCESS."
358 // This is the stupidest fucking api I've ever worked with.
359 if(SymSetContext(proc, &frame, nullptr) == FALSE && GetLastError() != ERROR_SUCCESS) {
360 log::error("Stack trace: Internal error while calling SymSetContext");
361 return {
362 addr,
363 object_frame.object_address,
364 { static_cast<std::uint32_t>(line.LineNumber) },
365 nullable<std::uint32_t>::null(),
366 line.FileName,
367 symbol->Name,
368 false
369 };
370 }
371 DWORD n_children = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_COUNT, true>(
372 symbol->TypeIndex,
373 proc,
374 symbol->ModBase
375 );
376 DWORD class_parent_id = get_info<DWORD, IMAGEHLP_SYMBOL_TYPE_INFO::TI_GET_CLASSPARENTID, true>(
377 symbol->TypeIndex,
378 proc,
379 symbol->ModBase
380 );
381 function_info fi {
382 proc,
383 symbol->ModBase,
384 0,
385 int(n_children),
386 class_parent_id != (DWORD)-1,
387 ""
388 };
389 SymEnumSymbols(proc, 0, nullptr, enumerator_callback, &fi);
390 std::string signature = symbol->Name + std::string("(") + fi.str + ")";
391 // There's a phenomina with DIA not inserting commas after template parameters. Fix them here.
392 static std::regex comma_re(R"(,(?=\S))");
393 signature = std::regex_replace(signature, comma_re, ", ");
394 return {
395 addr,
396 object_frame.object_address,
397 { static_cast<std::uint32_t>(line.LineNumber) },
398 nullable<std::uint32_t>::null(),
399 line.FileName,
400 signature,
401 false,
402 };
403 } else {
404 return {
405 addr,
406 object_frame.object_address,
407 nullable<std::uint32_t>::null(),
408 nullable<std::uint32_t>::null(),
409 "",
410 symbol->Name,
411 false
412 };
413 }
414 } else {
415 return {
416 addr,
417 object_frame.object_address,
418 nullable<std::uint32_t>::null(),
419 nullable<std::uint32_t>::null(),
420 "",
421 "",
422 false
423 };
424 }
425 }
426
427 std::vector<stacktrace_frame> resolve_frames(const std::vector<frame_ptr>& frames) {
428 // Dbghelp is is single-threaded, so acquire a lock.
429 auto lock = get_dbghelp_lock();
430 std::vector<stacktrace_frame> trace;
431 trace.reserve(frames.size());
432
433 // TODO: When does this need to be called? Can it be moved to the symbolizer?
434 SymSetOptions(SYMOPT_ALLOW_ABSOLUTE_SYMBOLS);
435
436 auto syminit_info = ensure_syminit();
437 for(const auto frame : frames) {
438 try {
439 trace.push_back(resolve_frame(syminit_info.get_process_handle() , frame));
440 } catch(...) { // NOSONAR
441 detail::log_and_maybe_propagate_exception(std::current_exception());
442 auto entry = null_frame();
443 entry.raw_address = frame;
444 trace.push_back(entry);
445 }
446 }
447 return trace;
448 }
449 }
450 }
451 CPPTRACE_END_NAMESPACE
452
453 CPPTRACE_BEGIN_NAMESPACE
454 /*
455 When a module was loaded at runtime with LoadLibrary after SymInitialize was already called,
456 it is necessary to manually load the symbols from that module with SymLoadModuleEx.
457
458 See "Symbol Handler Initialization" in Microsoft documentation at
459 https://learn.microsoft.com/en-us/windows/win32/debug/symbol-handler-initialization
460 */
461 void load_symbols_for_file(const std::string& filename) {
462 HMODULE hModule = GetModuleHandleA(filename.c_str());
463 if (hModule == NULL) {
464 throw detail::internal_error(
465 "Unable to get module handle for file '{}' : {}",
466 filename,
467 std::system_error(GetLastError(), std::system_category()).what()
468 );
469 }
470
471 // SymLoadModuleEx needs the module's base address and size, so get these with GetModuleInformation.
472 MODULEINFO module_info;
473 if (
474 !GetModuleInformation(
475 GetCurrentProcess(),
476 hModule,
477 &module_info,
478 sizeof(module_info)
479 )
480 ) {
481 throw detail::internal_error(
482 "Unable to get module information for file '{}' : {}",
483 filename,
484 std::system_error(GetLastError(), std::system_category()).what()
485 );
486 }
487
488 auto lock = detail::get_dbghelp_lock();
489 HANDLE syminit_handle = detail::ensure_syminit().get_process_handle();
490 if (
491 !SymLoadModuleEx(
492 syminit_handle,
493 NULL,
494 filename.c_str(),
495 NULL,
496 (DWORD64)module_info.lpBaseOfDll,
497 // The documentation says this is optional, but if omitted (0), symbol loading fails
498 module_info.SizeOfImage,
499 NULL,
500 0
501 )
502 ) {
503 throw detail::internal_error(
504 "Unable to load symbols for file '{}' : {}",
505 filename,
506 std::system_error(GetLastError(), std::system_category()).what()
507 );
508 }
509 }
510 CPPTRACE_END_NAMESPACE
511
512 #endif