]>
Commit | Line | Data |
---|---|---|
f35db108 WM |
1 | //===-- sanitizer_symbolizer_win.cc ---------------------------------------===// |
2 | // | |
3 | // This file is distributed under the University of Illinois Open Source | |
4 | // License. See LICENSE.TXT for details. | |
5 | // | |
6 | //===----------------------------------------------------------------------===// | |
7 | // | |
8 | // This file is shared between AddressSanitizer and ThreadSanitizer | |
9 | // run-time libraries. | |
10 | // Windows-specific implementation of symbolizer parts. | |
11 | //===----------------------------------------------------------------------===// | |
f35db108 | 12 | |
ef1b3fda KS |
13 | #include "sanitizer_platform.h" |
14 | #if SANITIZER_WINDOWS | |
dee5ea7a KS |
15 | #include <windows.h> |
16 | #include <dbghelp.h> | |
17 | #pragma comment(lib, "dbghelp.lib") | |
18 | ||
f35db108 WM |
19 | #include "sanitizer_symbolizer.h" |
20 | ||
21 | namespace __sanitizer { | |
22 | ||
dee5ea7a KS |
23 | class WinSymbolizer : public Symbolizer { |
24 | public: | |
25 | WinSymbolizer() : initialized_(false) {} | |
26 | ||
27 | uptr SymbolizePC(uptr addr, AddressInfo *frames, uptr max_frames) { | |
28 | if (max_frames == 0) | |
29 | return 0; | |
30 | ||
31 | BlockingMutexLock l(&dbghelp_mu_); | |
32 | if (!initialized_) { | |
866e32ad KS |
33 | if (!TrySymInitialize()) { |
34 | // OK, maybe the client app has called SymInitialize already. | |
35 | // That's a bit unfortunate for us as all the DbgHelp functions are | |
36 | // single-threaded and we can't coordinate with the app. | |
37 | // FIXME: Can we stop the other threads at this point? | |
38 | // Anyways, we have to reconfigure stuff to make sure that SymInitialize | |
39 | // has all the appropriate options set. | |
40 | // Cross our fingers and reinitialize DbgHelp. | |
41 | Report("*** WARNING: Failed to initialize DbgHelp! ***\n"); | |
42 | Report("*** Most likely this means that the app is already ***\n"); | |
43 | Report("*** using DbgHelp, possibly with incompatible flags. ***\n"); | |
44 | Report("*** Due to technical reasons, symbolization might crash ***\n"); | |
45 | Report("*** or produce wrong results. ***\n"); | |
46 | SymCleanup(GetCurrentProcess()); | |
47 | TrySymInitialize(); | |
48 | } | |
dee5ea7a KS |
49 | initialized_ = true; |
50 | } | |
51 | ||
52 | // See http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx | |
53 | char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(CHAR)]; | |
54 | PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer; | |
55 | symbol->SizeOfStruct = sizeof(SYMBOL_INFO); | |
56 | symbol->MaxNameLen = MAX_SYM_NAME; | |
57 | DWORD64 offset = 0; | |
58 | BOOL got_objname = SymFromAddr(GetCurrentProcess(), | |
59 | (DWORD64)addr, &offset, symbol); | |
60 | if (!got_objname) | |
61 | return 0; | |
62 | ||
63 | DWORD unused; | |
64 | IMAGEHLP_LINE64 line_info; | |
65 | line_info.SizeOfStruct = sizeof(IMAGEHLP_LINE64); | |
66 | BOOL got_fileline = SymGetLineFromAddr64(GetCurrentProcess(), (DWORD64)addr, | |
67 | &unused, &line_info); | |
68 | AddressInfo *info = &frames[0]; | |
69 | info->Clear(); | |
70 | info->function = internal_strdup(symbol->Name); | |
71 | info->function_offset = (uptr)offset; | |
72 | if (got_fileline) { | |
73 | info->file = internal_strdup(line_info.FileName); | |
74 | info->line = line_info.LineNumber; | |
75 | } | |
76 | ||
77 | IMAGEHLP_MODULE64 mod_info; | |
78 | internal_memset(&mod_info, 0, sizeof(mod_info)); | |
79 | mod_info.SizeOfStruct = sizeof(mod_info); | |
80 | if (SymGetModuleInfo64(GetCurrentProcess(), addr, &mod_info)) | |
81 | info->FillAddressAndModuleInfo(addr, mod_info.ImageName, | |
82 | addr - (uptr)mod_info.BaseOfImage); | |
83 | return 1; | |
84 | } | |
85 | ||
86 | bool CanReturnFileLineInfo() { | |
87 | return true; | |
88 | } | |
89 | ||
90 | const char *Demangle(const char *name) { | |
91 | CHECK(initialized_); | |
92 | static char demangle_buffer[1000]; | |
93 | if (name[0] == '\01' && | |
94 | UnDecorateSymbolName(name + 1, demangle_buffer, sizeof(demangle_buffer), | |
95 | UNDNAME_NAME_ONLY)) | |
96 | return demangle_buffer; | |
97 | else | |
98 | return name; | |
99 | } | |
100 | ||
101 | // FIXME: Implement GetModuleNameAndOffsetForPC(). | |
102 | ||
103 | private: | |
866e32ad KS |
104 | bool TrySymInitialize() { |
105 | SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME | SYMOPT_LOAD_LINES); | |
106 | return SymInitialize(GetCurrentProcess(), 0, TRUE); | |
107 | // FIXME: We don't call SymCleanup() on exit yet - should we? | |
108 | } | |
109 | ||
dee5ea7a KS |
110 | // All DbgHelp functions are single threaded, so we should use a mutex to |
111 | // serialize accesses. | |
112 | BlockingMutex dbghelp_mu_; | |
113 | bool initialized_; | |
114 | }; | |
115 | ||
866e32ad | 116 | Symbolizer *Symbolizer::PlatformInit() { |
dee5ea7a KS |
117 | static bool called_once = false; |
118 | CHECK(!called_once && "Shouldn't create more than one symbolizer"); | |
119 | called_once = true; | |
120 | return new(symbolizer_allocator_) WinSymbolizer(); | |
121 | } | |
e9772e16 | 122 | |
f35db108 WM |
123 | } // namespace __sanitizer |
124 | ||
125 | #endif // _WIN32 |