|
| 1 | +/** |
| 2 | +* This module provides routines to demangle C++ symbols |
| 3 | +* |
| 4 | +* For Posix platform the demangling is done by function `__cxa_demangle` presents in Itanium C++ ABI. |
| 5 | +* If the function is not found or something fails, the original mangled C++ name will be returned. |
| 6 | +* This module currently only supports POSIX. |
| 7 | +* |
| 8 | +* Copyright: Copyright © 2019, The D Language Foundation |
| 9 | +* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 10 | +* Authors: Ernesto Castellotti |
| 11 | +* Source: $(DRUNTIMESRC core/internal/_cppdemangle.d) |
| 12 | +*/ |
| 13 | +module core.internal.cppdemangle; |
| 14 | + |
| 15 | +/** |
| 16 | +* Demangles C++ mangled names. |
| 17 | +* |
| 18 | +* If it is not a C++ mangled name or cppdemangle is not supported by your platform, the original mangled C++ name will be returned. |
| 19 | +* The optional destination buffer and return will be contains the same string if the demangle is successful. |
| 20 | +* |
| 21 | + * Params: |
| 22 | + * buf = The string to demangle. |
| 23 | + * dst = The destination buffer, if the size of the destination buffer is <= the size of cppdemangle output the return string would be incomplete. |
| 24 | + * withPrefix = If true, add the prefix "[C++] " before the C++ demangled name, make sure the target buffer is at least large to contain the prefix. |
| 25 | + * |
| 26 | + * Returns: |
| 27 | + * The demangled name or the original string if the name is not a mangled C++ name. |
| 28 | + */ |
| 29 | +version (iOS) |
| 30 | +{ |
| 31 | + // Not supported (dlopen doesn't work) |
| 32 | + // Fix me |
| 33 | +} |
| 34 | +else version (Posix) |
| 35 | +{ |
| 36 | + version = SupportCppDemangle; |
| 37 | +} |
| 38 | + |
| 39 | +char[] cppdemangle(const(char)[] buf, char[] dst, bool withPrefix = false) @safe @nogc nothrow |
| 40 | +{ |
| 41 | + enum prefix = "[C++] "; |
| 42 | + auto dstStart = withPrefix ? prefix.length : 0; |
| 43 | + assert(dst !is null && dst.length > dstStart, "The destination buffer is null or too small for perform demangle"); |
| 44 | + |
| 45 | + version (SupportCppDemangle) |
| 46 | + { |
| 47 | + auto demangle = _cppdemangle(buf, dst[dstStart..$]); |
| 48 | + if (demangle is null) return copyResult(buf, dst); |
| 49 | + if (withPrefix) dst[0..dstStart] = prefix; |
| 50 | + return dst[0..(demangle.length + dstStart)]; |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + return copyResult(buf, dst); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +private char[] copyResult(const(char)[] input, char[] dst) @safe @nogc nothrow |
| 59 | +{ |
| 60 | + auto len = input.length <= dst.length ? input.length : dst.length; |
| 61 | + dst[0..len] = input[0..len]; |
| 62 | + dst[len..$] = '\0'; |
| 63 | + return dst[0..len]; |
| 64 | +} |
| 65 | + |
| 66 | +version (Posix) |
| 67 | +{ |
| 68 | + private char[] _cppdemangle(const(char)[] buf, char[] dst) @trusted @nogc nothrow |
| 69 | + { |
| 70 | + import core.memory : pureCalloc, pureFree; |
| 71 | + import core.stdc.string : strlen; |
| 72 | + |
| 73 | + int status; |
| 74 | + auto mangledName = cast(char*) pureCalloc(buf.length + 1, char.sizeof); |
| 75 | + scope(exit) pureFree(mangledName); |
| 76 | + mangledName[0..buf.length] = buf[]; |
| 77 | + |
| 78 | + auto demangle = CXADemangleAPI.instance().__cxa_demangle; |
| 79 | + if (demangle is null) return null; |
| 80 | + |
| 81 | + auto result = demangle(mangledName, null, null, &status); |
| 82 | + scope(exit) pureFree(result); |
| 83 | + |
| 84 | + if (status != 0) return null; |
| 85 | + return copyResult(result[0..strlen(result)], dst); |
| 86 | + } |
| 87 | + |
| 88 | + private static struct CXADemangleAPI |
| 89 | + { |
| 90 | + static struct API |
| 91 | + { |
| 92 | + @nogc nothrow extern(C): |
| 93 | + private __gshared extern(C) char* function(const char* mangled_name, char* dst_buffer, size_t* length, int* status) __cxa_demangle; |
| 94 | + } |
| 95 | + |
| 96 | + private __gshared API _api; |
| 97 | + private __gshared void* _handle; |
| 98 | + |
| 99 | + static ref API instance() @nogc nothrow |
| 100 | + { |
| 101 | + if (_api.__cxa_demangle is null) _handle = loadAPI(); |
| 102 | + return _api; |
| 103 | + } |
| 104 | + |
| 105 | + static void* loadAPI() @nogc nothrow |
| 106 | + { |
| 107 | + static extern(C) void cleanup() |
| 108 | + { |
| 109 | + if (_handle is null) return; |
| 110 | + import core.sys.posix.dlfcn : dlclose; |
| 111 | + dlclose(_handle); |
| 112 | + _handle = null; |
| 113 | + _api.__cxa_demangle = null; |
| 114 | + } |
| 115 | + |
| 116 | + static void* setSym(void* handle, void* ptrSym) |
| 117 | + { |
| 118 | + import core.stdc.stdlib : atexit; |
| 119 | + atexit(&cleanup); |
| 120 | + _api.__cxa_demangle = cast(typeof(CXADemangleAPI.API.__cxa_demangle)) ptrSym; |
| 121 | + return handle; |
| 122 | + } |
| 123 | + |
| 124 | + import core.sys.posix.dlfcn : dlsym, dlopen, dlclose, RTLD_LAZY; |
| 125 | + |
| 126 | + auto handle = dlopen(null, RTLD_LAZY); |
| 127 | + if (handle is null) return null; |
| 128 | + |
| 129 | + auto p = dlsym(handle, "__cxa_demangle"); |
| 130 | + if (p !is null) return setSym(handle, p); |
| 131 | + |
| 132 | + dlclose(handle); |
| 133 | + |
| 134 | + version (OSX) |
| 135 | + enum names = ["libc++abi.dylib", "libstdc++.dylib"]; |
| 136 | + else version (Posix) |
| 137 | + { |
| 138 | + enum names = ["libstdc++.so", "libc++abi.so", |
| 139 | + "libc++abi.so.1"]; |
| 140 | + } |
| 141 | + else version (MinGW) |
| 142 | + enum names = ["libstdc++.dll", "libstdc++-6.dll"]; |
| 143 | + |
| 144 | + foreach (name; names) |
| 145 | + { |
| 146 | + handle = dlopen(name.ptr, RTLD_LAZY); |
| 147 | + if (handle !is null) break; |
| 148 | + } |
| 149 | + |
| 150 | + if (handle is null) return null; |
| 151 | + p = dlsym(handle, "__cxa_demangle"); |
| 152 | + |
| 153 | + if (p is null) |
| 154 | + { |
| 155 | + dlclose(handle); |
| 156 | + return null; |
| 157 | + } |
| 158 | + |
| 159 | + return setSym(handle, p); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments