Timestamping callback interface in folly::AsyncSocket
[folly.git] / folly / Demangle.cpp
1 /*
2  * Copyright 2017 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 #include <folly/portability/Config.h>
24
25 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
26 # include <cxxabi.h>
27
28 // From libiberty
29 //
30 // TODO(tudorb): Detect this with autoconf for the open-source version.
31 //
32 // __attribute__((__weak__)) doesn't work, because cplus_demangle_v3_callback
33 // is exported by an object file in libiberty.a, and the ELF spec says
34 // "The link editor does not extract archive members to resolve undefined weak
35 // symbols" (but, interestingly enough, will resolve undefined weak symbols
36 // with definitions from archive members that were extracted in order to
37 // resolve an undefined global (strong) symbol)
38
39 # ifndef DMGL_NO_OPTS
40 #  define FOLLY_DEFINED_DMGL 1
41 #  define DMGL_NO_OPTS    0          /* For readability... */
42 #  define DMGL_PARAMS     (1 << 0)   /* Include function args */
43 #  define DMGL_ANSI       (1 << 1)   /* Include const, volatile, etc */
44 #  define DMGL_JAVA       (1 << 2)   /* Demangle as Java rather than C++. */
45 #  define DMGL_VERBOSE    (1 << 3)   /* Include implementation details.  */
46 #  define DMGL_TYPES      (1 << 4)   /* Also try to demangle type encodings.  */
47 #  define DMGL_RET_POSTFIX (1 << 5)  /* Print function return types (when
48                                         present) after function signature */
49 # endif
50
51 extern "C" int cplus_demangle_v3_callback(
52     const char* mangled,
53     int options,  // We use DMGL_PARAMS | DMGL_TYPES, aka 0x11
54     void (*callback)(const char*, size_t, void*),
55     void* arg);
56
57 #endif
58
59 namespace folly {
60
61 #if FOLLY_HAVE_CPLUS_DEMANGLE_V3_CALLBACK
62
63 fbstring demangle(const char* name) {
64 #ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
65   // GCC's __cxa_demangle() uses on-stack data structures for the
66   // parser state which are linear in the number of components of the
67   // symbol. For extremely long symbols, this can cause a stack
68   // overflow. We set an arbitrary symbol length limit above which we
69   // just return the mangled name.
70   size_t mangledLen = strlen(name);
71   if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
72     return fbstring(name, mangledLen);
73   }
74 #endif
75
76   int status;
77   size_t len = 0;
78   // malloc() memory for the demangled type name
79   char* demangled = abi::__cxa_demangle(name, nullptr, &len, &status);
80   if (status != 0) {
81     return name;
82   }
83   // len is the length of the buffer (including NUL terminator and maybe
84   // other junk)
85   return fbstring(demangled, strlen(demangled), len, AcquireMallocatedString());
86 }
87
88 namespace {
89
90 struct DemangleBuf {
91   char* dest;
92   size_t remaining;
93   size_t total;
94 };
95
96 void demangleCallback(const char* str, size_t size, void* p) {
97   DemangleBuf* buf = static_cast<DemangleBuf*>(p);
98   size_t n = std::min(buf->remaining, size);
99   memcpy(buf->dest, str, n);
100   buf->dest += n;
101   buf->remaining -= n;
102   buf->total += size;
103 }
104
105 }  // namespace
106
107 size_t demangle(const char* name, char* out, size_t outSize) {
108 #ifdef FOLLY_DEMANGLE_MAX_SYMBOL_SIZE
109   size_t mangledLen = strlen(name);
110   if (mangledLen > FOLLY_DEMANGLE_MAX_SYMBOL_SIZE) {
111     if (outSize) {
112       size_t n = std::min(mangledLen, outSize - 1);
113       memcpy(out, name, n);
114       out[n] = '\0';
115     }
116     return mangledLen;
117   }
118 #endif
119
120   DemangleBuf dbuf;
121   dbuf.dest = out;
122   dbuf.remaining = outSize ? outSize - 1 : 0;   // leave room for null term
123   dbuf.total = 0;
124
125   // Unlike most library functions, this returns 1 on success and 0 on failure
126   int status = cplus_demangle_v3_callback(
127       name,
128       DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES,
129       demangleCallback,
130       &dbuf);
131   if (status == 0) {  // failed, return original
132     return folly::strlcpy(out, name, outSize);
133   }
134   if (outSize != 0) {
135     *dbuf.dest = '\0';
136   }
137   return dbuf.total;
138 }
139
140 #else
141
142 fbstring demangle(const char* name) {
143   return name;
144 }
145
146 size_t demangle(const char* name, char* out, size_t outSize) {
147   return folly::strlcpy(out, name, outSize);
148 }
149
150 #endif
151
152 size_t strlcpy(char* dest, const char* const src, size_t size) {
153   size_t len = strlen(src);
154   if (size != 0) {
155     size_t n = std::min(len, size - 1);  // always null terminate!
156     memcpy(dest, src, n);
157     dest[n] = '\0';
158   }
159   return len;
160 }
161
162 } // folly