1 //===-- sanitizer_symbolizer_win.cc ---------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // Windows-specific implementation of symbolizer parts.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
17 #pragma comment(lib, "dbghelp.lib")
19 #include "sanitizer_symbolizer.h"
21 namespace __sanitizer
{
23 class WinSymbolizer
: public Symbolizer
{
25 WinSymbolizer() : initialized_(false) {}
27 uptr
SymbolizePC(uptr addr
, AddressInfo
*frames
, uptr max_frames
) {
31 BlockingMutexLock
l(&dbghelp_mu_
);
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());
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
;
58 BOOL got_objname
= SymFromAddr(GetCurrentProcess(),
59 (DWORD64
)addr
, &offset
, symbol
);
64 IMAGEHLP_LINE64 line_info
;
65 line_info
.SizeOfStruct
= sizeof(IMAGEHLP_LINE64
);
66 BOOL got_fileline
= SymGetLineFromAddr64(GetCurrentProcess(), (DWORD64
)addr
,
68 AddressInfo
*info
= &frames
[0];
70 info
->function
= internal_strdup(symbol
->Name
);
71 info
->function_offset
= (uptr
)offset
;
73 info
->file
= internal_strdup(line_info
.FileName
);
74 info
->line
= line_info
.LineNumber
;
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
);
86 bool CanReturnFileLineInfo() {
90 const char *Demangle(const char *name
) {
92 static char demangle_buffer
[1000];
93 if (name
[0] == '\01' &&
94 UnDecorateSymbolName(name
+ 1, demangle_buffer
, sizeof(demangle_buffer
),
96 return demangle_buffer
;
101 // FIXME: Implement GetModuleNameAndOffsetForPC().
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?
110 // All DbgHelp functions are single threaded, so we should use a mutex to
111 // serialize accesses.
112 BlockingMutex dbghelp_mu_
;
116 Symbolizer
*Symbolizer::PlatformInit() {
117 static bool called_once
= false;
118 CHECK(!called_once
&& "Shouldn't create more than one symbolizer");
120 return new(symbolizer_allocator_
) WinSymbolizer();
123 } // namespace __sanitizer