3922f74fcb458cbd83115714e7ea79ae725563f9
[folly.git] / folly / Demangle.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "folly/Demangle.h"
18
19 #include <algorithm>
20 #include <string.h>
21
22 #include "folly/Malloc.h"
23
24 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
25 # include <cxxabi.h>
26
27 // From libiberty
28 //
29 // TODO(tudorb): Detect this with autoconf for the open-source version.
30 //
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)
37
38 # ifndef DMGL_NO_OPTS
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 */
48 # endif
49
50 extern "C" int cplus_demangle_v3_callback(
51     const char* mangled,
52     int options,  // We use DMGL_PARAMS | DMGL_TYPES, aka 0x11
53     void (*callback)(const char*, size_t, void*),
54     void* arg);
55
56 #endif
57
58 namespace {
59
60 // glibc doesn't have strlcpy
61 size_t my_strlcpy(char* dest, const char* src, size_t size) {
62   size_t len = strlen(src);
63   if (size != 0) {
64     size_t n = std::min(len, size - 1);  // always null terminate!
65     memcpy(dest, src, n);
66     dest[n] = '\0';
67   }
68   return len;
69 }
70
71 }  // namespace
72
73 namespace folly {
74
75 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
76
77 fbstring demangle(const char* name) {
78   int status;
79   size_t len = 0;
80   // malloc() memory for the demangled type name
81   char* demangled = abi::__cxa_demangle(name, nullptr, &len, &status);
82   if (status != 0) {
83     return name;
84   }
85   // len is the length of the buffer (including NUL terminator and maybe
86   // other junk)
87   return fbstring(demangled, strlen(demangled), len, AcquireMallocatedString());
88 }
89
90 namespace {
91
92 struct DemangleBuf {
93   char* dest;
94   size_t remaining;
95   size_t total;
96 };
97
98 void demangleCallback(const char* str, size_t size, void* p) {
99   DemangleBuf* buf = static_cast<DemangleBuf*>(p);
100   size_t n = std::min(buf->remaining, size);
101   memcpy(buf->dest, str, n);
102   buf->dest += n;
103   buf->remaining -= n;
104   buf->total += size;
105 }
106
107 }  // namespace
108
109 size_t demangle(const char* name, char* out, size_t outSize) {
110   DemangleBuf dbuf;
111   dbuf.dest = out;
112   dbuf.remaining = outSize ? outSize - 1 : 0;   // leave room for null term
113   dbuf.total = 0;
114
115   // Unlike most library functions, this returns 1 on success and 0 on failure
116   int status = cplus_demangle_v3_callback(
117       name,
118       DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES,
119       demangleCallback,
120       &dbuf);
121   if (status == 0) {  // failed, return original
122     return my_strlcpy(out, name, outSize);
123   }
124   if (outSize != 0) {
125     *dbuf.dest = '\0';
126   }
127   return dbuf.total;
128 }
129
130 #else
131
132 fbstring demangle(const char* name) {
133   return name;
134 }
135
136 size_t demangle(const char* name, char* out, size_t outSize) {
137   return my_strlcpy(out, name, outSize);
138 }
139
140 #endif
141
142 } // folly