OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "vm/native_symbol.h" | |
6 #include "vm/thread.h" | |
7 | |
8 #if defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) || \ | |
9 defined(TARGET_OS_MACOS) | |
10 #include <dlfcn.h> // NOLINT | |
11 #elif defined(TARGET_OS_WINDOWS) | |
12 #include <dbghelp.h> // NOLINT | |
13 #else | |
14 #error No native symbol implementation for your target. | |
15 #endif | |
16 | |
17 namespace dart { | |
18 | |
19 void NativeSymbolResolver::InitOnce() { | |
20 #if defined(TARGET_OS_WINDOWS) | |
21 ASSERT(running_ == false); | |
22 SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS); | |
23 HANDLE hProcess = GetCurrentProcess(); | |
24 if (!SymInitialize(hProcess, NULL, TRUE)) { | |
25 DWORD error = GetLastError(); | |
26 printf("Failed to init NativeSymbolResolver (SymInitialize %d)\n", error); | |
27 return; | |
28 } | |
29 lock_ = new Mutex(); | |
30 running_ = true; | |
31 #else | |
32 // No initialization needed. | |
33 running_ = true; | |
34 #endif | |
35 } | |
36 | |
37 | |
38 void NativeSymbolResolver::ShutdownOnce() { | |
39 #if defined(TARGET_OS_WINDOWS) | |
40 ScopedMutexLock lock(lock_); | |
41 if (!running_) { | |
42 return; | |
43 } | |
44 running_ = false; | |
45 HANDLE hProcess = GetCurrentProcess(); | |
46 if (!SymCleanup(hProcess)) { | |
47 DWORD error = GetLastError(); | |
48 printf("Failed to shutdown NativeSymbolResolver (SymCleanup %d)\n", error); | |
49 } | |
50 #endif | |
51 } | |
52 | |
53 | |
54 const char* NativeSymbolResolver::LookupSymbolName(uintptr_t pc) { | |
55 #if defined(TARGET_OS_LINUX) || defined(TARGET_OS_MACOS) || \ | |
56 defined(TARGET_OS_ANDROID) | |
57 Dl_info info; | |
58 int r = dladdr(reinterpret_cast<void*>(pc), &info); | |
59 if (r == 0) { | |
60 return NULL; | |
61 } | |
62 return info.dli_sname; | |
63 #elif defined(TARGET_OS_WINDOWS) | |
64 static const intptr_t kMaxNameLength = 2048; | |
65 static const intptr_t kSymbolInfoSize = sizeof(SYMBOL_INFO); // NOLINT. | |
66 static char buffer[kSymbolInfoSize + kMaxNameLength]; | |
67 static char name_buffer[kMaxNameLength]; | |
68 ScopedMutexLock lock(lock_); | |
69 if (!running_) { | |
70 return NULL; | |
71 } | |
72 memset(&buffer[0], 0, sizeof(buffer)); | |
73 HANDLE hProcess = GetCurrentProcess(); | |
74 DWORD64 address = static_cast<DWORD64>(pc); | |
75 PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(&buffer[0]); | |
76 pSymbol->SizeOfStruct = kSymbolInfoSize; | |
77 pSymbol->MaxNameLen = kMaxNameLength; | |
78 BOOL r = SymFromAddr(hProcess, address, NULL, pSymbol); | |
79 if (r == FALSE) { | |
80 return NULL; | |
81 } | |
82 return strdup(pSymbol->Name); | |
83 #else | |
84 UNIMPLEMENTED(); | |
85 return NULL; | |
86 #endif | |
87 } | |
88 | |
89 | |
90 void NativeSymbolResolver::FreeSymbolName(const char* name) { | |
91 #if defined(TARGET_OS_WINDOWS) | |
92 // Windows allocates memory for returned symbol name, free it. | |
93 free(const_cast<char*>(name)); | |
94 #endif | |
95 } | |
96 | |
97 | |
98 bool NativeSymbolResolver::running_ = false; | |
99 Mutex* NativeSymbolResolver::lock_ = NULL; | |
siva
2013/10/28 05:19:21
Why not have files
native_symbol_windows.cc
native
Cutch
2013/11/04 20:36:05
Done.
| |
100 | |
101 } // namespace dart | |
OLD | NEW |