Optional Two-phase heap tracing
[hiphop-php.git] / hphp / util / abi-cxx.cpp
blob70cae262cc0589ac3a47f49c255714cd9e0d1597
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #include "hphp/util/abi-cxx.h"
18 #include <algorithm>
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cstring>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <unordered_map>
27 #ifdef _MSC_VER
28 # include <windows.h>
29 # include <dbghelp.h>
30 #else
31 # include <cxxabi.h>
32 # include <execinfo.h>
33 #endif
35 #include <folly/Format.h>
37 #include "hphp/util/functional.h"
38 #include "hphp/util/compatibility.h"
40 #ifdef FACEBOOK
41 #include <folly/experimental/symbolizer/Symbolizer.h>
42 #endif
44 namespace HPHP {
46 //////////////////////////////////////////////////////////////////////
48 namespace {
50 typedef std::lock_guard<std::mutex> G;
51 std::mutex nameCacheLock;
52 std::unordered_map<void*, std::string, pointer_hash<void>> nameCache;
56 //////////////////////////////////////////////////////////////////////
58 std::string getNativeFunctionName(void* codeAddr) {
60 G g(nameCacheLock);
61 auto it = nameCache.find(codeAddr);
62 if (it != end(nameCache)) return it->second;
64 std::string functionName;
66 #ifdef _MSC_VER
67 HANDLE process = GetCurrentProcess();
68 SYMBOL_INFO *symbol;
69 DWORD64 addr_disp = 0;
71 // syminitialize and symcleanup should really be once per process
72 SymInitialize(process, nullptr, TRUE);
74 symbol = (SYMBOL_INFO *)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
75 symbol->MaxNameLen = 255;
76 symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
78 if(SymFromAddr(process, (DWORD64) codeAddr, &addr_disp, symbol)) {
79 functionName.assign(symbol->Name);
81 int status;
82 char* demangledName = (char*)calloc(1024, sizeof(char));
83 status = !(int)UnDecorateSymbolName(
84 symbol->Name, demangledName, 1023, UNDNAME_COMPLETE);
85 SCOPE_EXIT { free(demangledName); };
86 if (status == 0) functionName.assign(demangledName);
89 free(symbol);
91 SymCleanup(process);
92 #elif defined(FACEBOOK)
94 folly::symbolizer::Symbolizer symbolizer;
95 folly::symbolizer::SymbolizedFrame frame;
96 if (symbolizer.symbolize(uintptr_t(codeAddr), frame)) {
97 functionName = frame.demangledName().toStdString();
100 #else
101 void* buf[1] = {codeAddr};
102 char** symbols = backtrace_symbols(buf, 1);
104 if (symbols != nullptr) {
106 // the output from backtrace_symbols looks like this:
107 // ../path/hhvm/hhvm(_ZN4HPHP2VM6Transl17interpOneIterInitEv+0) [0x17cebe9]
109 // we first want to extract the mangled name from it to get this:
110 // _ZN4HPHP2VM6Transl17interpOneIterInitEv
112 // and then pass this to abi::__cxa_demangle to get the demanged name:
113 // HPHP::jit::interpOneIterInit()
115 // Sometimes, though, backtrace_symbols can't find the function name
116 // and ends up giving us a blank managled name, like this:
117 // ../path/hhvm/hhvm() [0x17e4d01]
118 // or this: [0x7fffca800130]
120 char* start = strchr(*symbols, '(');
121 if (start) {
122 start++;
123 char* end = strchr(start, '+');
124 if (end != nullptr) {
125 functionName.assign(start, end);
126 int status;
127 char* demangledName = abi::__cxa_demangle(functionName.c_str(),
128 0, 0, &status);
129 SCOPE_EXIT { free(demangledName); };
130 if (status == 0) functionName.assign(demangledName);
135 // backtrace_symbols requires that we free the array of strings but not the
136 // strings themselves.
137 free(symbols);
138 #endif
140 if (functionName.empty()) functionName = folly::format("{}", codeAddr).str();
142 G g(nameCacheLock);
143 return nameCache[codeAddr] = functionName;
146 //////////////////////////////////////////////////////////////////////