2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/Demangle.h>
22 #include <folly/Malloc.h>
24 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
29 // TODO(tudorb): Detect this with autoconf for the open-source version.
31 // __attribute__((__weak__)) doesn't work, because cplus_demangle_v3_callback
32 // is exported by an object file in libiberty.a, and the ELF spec says
33 // "The link editor does not extract archive members to resolve undefined weak
34 // symbols" (but, interestingly enough, will resolve undefined weak symbols
35 // with definitions from archive members that were extracted in order to
36 // resolve an undefined global (strong) symbol)
39 # define FOLLY_DEFINED_DMGL 1
40 # define DMGL_NO_OPTS 0 /* For readability... */
41 # define DMGL_PARAMS (1 << 0) /* Include function args */
42 # define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
43 # define DMGL_JAVA (1 << 2) /* Demangle as Java rather than C++. */
44 # define DMGL_VERBOSE (1 << 3) /* Include implementation details. */
45 # define DMGL_TYPES (1 << 4) /* Also try to demangle type encodings. */
46 # define DMGL_RET_POSTFIX (1 << 5) /* Print function return types (when
47 present) after function signature */
50 extern "C" int cplus_demangle_v3_callback(
52 int options, // We use DMGL_PARAMS | DMGL_TYPES, aka 0x11
53 void (*callback)(const char*, size_t, void*),
60 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
62 fbstring demangle(const char* name) {
65 // malloc() memory for the demangled type name
66 char* demangled = abi::__cxa_demangle(name, nullptr, &len, &status);
70 // len is the length of the buffer (including NUL terminator and maybe
72 return fbstring(demangled, strlen(demangled), len, AcquireMallocatedString());
83 void demangleCallback(const char* str, size_t size, void* p) {
84 DemangleBuf* buf = static_cast<DemangleBuf*>(p);
85 size_t n = std::min(buf->remaining, size);
86 memcpy(buf->dest, str, n);
94 size_t demangle(const char* name, char* out, size_t outSize) {
97 dbuf.remaining = outSize ? outSize - 1 : 0; // leave room for null term
100 // Unlike most library functions, this returns 1 on success and 0 on failure
101 int status = cplus_demangle_v3_callback(
103 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES,
106 if (status == 0) { // failed, return original
107 return folly::strlcpy(out, name, outSize);
117 fbstring demangle(const char* name) {
121 size_t demangle(const char* name, char* out, size_t outSize) {
122 return folly::strlcpy(out, name, outSize);
127 size_t strlcpy(char* dest, const char* const src, size_t size) {
128 size_t len = strlen(src);
130 size_t n = std::min(len, size - 1); // always null terminate!
131 memcpy(dest, src, n);