Chromium Code Reviews| Index: runtime/vm/native_symbol.cc |
| diff --git a/runtime/vm/native_symbol.cc b/runtime/vm/native_symbol.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d2e1b62a9a19547a254edf37ae2a1d5206e91bf0 |
| --- /dev/null |
| +++ b/runtime/vm/native_symbol.cc |
| @@ -0,0 +1,101 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +#include "vm/native_symbol.h" |
| +#include "vm/thread.h" |
| + |
| +#if defined(TARGET_OS_LINUX) || defined(TARGET_OS_ANDROID) || \ |
| + defined(TARGET_OS_MACOS) |
| +#include <dlfcn.h> // NOLINT |
| +#elif defined(TARGET_OS_WINDOWS) |
| +#include <dbghelp.h> // NOLINT |
| +#else |
| +#error No native symbol implementation for your target. |
| +#endif |
| + |
| +namespace dart { |
| + |
| +void NativeSymbolResolver::InitOnce() { |
| +#if defined(TARGET_OS_WINDOWS) |
| + ASSERT(running_ == false); |
| + SymSetOptions(SYMOPT_UNDNAME | SYMOPT_DEFERRED_LOADS); |
| + HANDLE hProcess = GetCurrentProcess(); |
| + if (!SymInitialize(hProcess, NULL, TRUE)) { |
| + DWORD error = GetLastError(); |
| + printf("Failed to init NativeSymbolResolver (SymInitialize %d)\n", error); |
| + return; |
| + } |
| + lock_ = new Mutex(); |
| + running_ = true; |
| +#else |
| + // No initialization needed. |
| + running_ = true; |
| +#endif |
| +} |
| + |
| + |
| +void NativeSymbolResolver::ShutdownOnce() { |
| +#if defined(TARGET_OS_WINDOWS) |
| + ScopedMutexLock lock(lock_); |
| + if (!running_) { |
| + return; |
| + } |
| + running_ = false; |
| + HANDLE hProcess = GetCurrentProcess(); |
| + if (!SymCleanup(hProcess)) { |
| + DWORD error = GetLastError(); |
| + printf("Failed to shutdown NativeSymbolResolver (SymCleanup %d)\n", error); |
| + } |
| +#endif |
| +} |
| + |
| + |
| +const char* NativeSymbolResolver::LookupSymbolName(uintptr_t pc) { |
| +#if defined(TARGET_OS_LINUX) || defined(TARGET_OS_MACOS) || \ |
| + defined(TARGET_OS_ANDROID) |
| + Dl_info info; |
| + int r = dladdr(reinterpret_cast<void*>(pc), &info); |
| + if (r == 0) { |
| + return NULL; |
| + } |
| + return info.dli_sname; |
| +#elif defined(TARGET_OS_WINDOWS) |
| + static const intptr_t kMaxNameLength = 2048; |
| + static const intptr_t kSymbolInfoSize = sizeof(SYMBOL_INFO); // NOLINT. |
| + static char buffer[kSymbolInfoSize + kMaxNameLength]; |
| + static char name_buffer[kMaxNameLength]; |
| + ScopedMutexLock lock(lock_); |
| + if (!running_) { |
| + return NULL; |
| + } |
| + memset(&buffer[0], 0, sizeof(buffer)); |
| + HANDLE hProcess = GetCurrentProcess(); |
| + DWORD64 address = static_cast<DWORD64>(pc); |
| + PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(&buffer[0]); |
| + pSymbol->SizeOfStruct = kSymbolInfoSize; |
| + pSymbol->MaxNameLen = kMaxNameLength; |
| + BOOL r = SymFromAddr(hProcess, address, NULL, pSymbol); |
| + if (r == FALSE) { |
| + return NULL; |
| + } |
| + return strdup(pSymbol->Name); |
| +#else |
| + UNIMPLEMENTED(); |
| + return NULL; |
| +#endif |
| +} |
| + |
| + |
| +void NativeSymbolResolver::FreeSymbolName(const char* name) { |
| +#if defined(TARGET_OS_WINDOWS) |
| + // Windows allocates memory for returned symbol name, free it. |
| + free(const_cast<char*>(name)); |
| +#endif |
| +} |
| + |
| + |
| +bool NativeSymbolResolver::running_ = false; |
| +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.
|
| + |
| +} // namespace dart |