2 +----------------------------------------------------------------------+
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"
25 #include <unordered_map>
32 # include <execinfo.h>
35 #include <folly/Demangle.h>
36 #include <folly/Format.h>
38 #include "hphp/util/functional.h"
39 #include "hphp/util/compatibility.h"
42 #include <folly/experimental/symbolizer/Symbolizer.h>
47 //////////////////////////////////////////////////////////////////////
51 typedef std::lock_guard
<std::mutex
> G
;
52 std::mutex nameCacheLock
;
53 std::unordered_map
<void*, std::string
, pointer_hash
<void>> nameCache
;
57 //////////////////////////////////////////////////////////////////////
59 std::string
getNativeFunctionName(void* codeAddr
) {
62 auto it
= nameCache
.find(codeAddr
);
63 if (it
!= end(nameCache
)) return it
->second
;
65 std::string functionName
;
68 HANDLE process
= GetCurrentProcess();
70 DWORD64 addr_disp
= 0;
72 // syminitialize and symcleanup should really be once per process
73 SymInitialize(process
, nullptr, TRUE
);
75 symbol
= (SYMBOL_INFO
*)calloc(sizeof(SYMBOL_INFO
) + 256 * sizeof(char), 1);
76 symbol
->MaxNameLen
= 255;
77 symbol
->SizeOfStruct
= sizeof(SYMBOL_INFO
);
79 if(SymFromAddr(process
, (DWORD64
) codeAddr
, &addr_disp
, symbol
)) {
80 functionName
.assign(symbol
->Name
);
83 char* demangledName
= (char*)calloc(1024, sizeof(char));
84 status
= !(int)UnDecorateSymbolName(
85 symbol
->Name
, demangledName
, 1023, UNDNAME_COMPLETE
);
86 SCOPE_EXIT
{ free(demangledName
); };
87 if (status
== 0) functionName
.assign(demangledName
);
93 #elif defined(FACEBOOK)
95 folly::symbolizer::Symbolizer symbolizer
;
96 folly::symbolizer::SymbolizedFrame frame
;
97 if (symbolizer
.symbolize(uintptr_t(codeAddr
), frame
)) {
98 functionName
= folly::demangle(frame
.name
).toStdString();
102 void* buf
[1] = {codeAddr
};
103 char** symbols
= backtrace_symbols(buf
, 1);
105 if (symbols
!= nullptr) {
107 // the output from backtrace_symbols looks like this:
108 // ../path/hhvm/hhvm(_ZN4HPHP2VM6Transl17interpOneIterInitEv+0) [0x17cebe9]
110 // we first want to extract the mangled name from it to get this:
111 // _ZN4HPHP2VM6Transl17interpOneIterInitEv
113 // and then pass this to abi::__cxa_demangle to get the demanged name:
114 // HPHP::jit::interpOneIterInit()
116 // Sometimes, though, backtrace_symbols can't find the function name
117 // and ends up giving us a blank managled name, like this:
118 // ../path/hhvm/hhvm() [0x17e4d01]
119 // or this: [0x7fffca800130]
121 char* start
= strchr(*symbols
, '(');
124 char* end
= strchr(start
, '+');
125 if (end
!= nullptr) {
126 functionName
.assign(start
, end
);
128 char* demangledName
= abi::__cxa_demangle(functionName
.c_str(),
130 SCOPE_EXIT
{ free(demangledName
); };
131 if (status
== 0) functionName
.assign(demangledName
);
136 // backtrace_symbols requires that we free the array of strings but not the
137 // strings themselves.
141 if (functionName
.empty()) functionName
= folly::format("{}", codeAddr
).str();
144 return nameCache
[codeAddr
] = functionName
;
147 //////////////////////////////////////////////////////////////////////