Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Enable demangling of C++ symbols in the stacktrace
Browse files Browse the repository at this point in the history
This allows you to view demangled and pretty printed C++ symbols
in the stacktrace.
Is especially useful considering that D is getting more and more support for C++.

Now the C++ symbols in the stacktrace will be printed like this:
        test.d:7 [C++] Test<int>::SomeName(int, long, int) [0x55711efee36f]
        test.d:18 [C++] testcpp() [0x557228e17978]

Signed-off-by: Ernesto Castellotti <mail@ernestocastellotti.it>
  • Loading branch information
Ernesto Castellotti committed Mar 24, 2020
1 parent 475c85f commit dfb01dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/core/runtime.d
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ unittest
static if (hasExecinfo) private class DefaultTraceInfo : Throwable.TraceInfo
{
import core.demangle;
import core.internal.cppdemangle;
import core.stdc.stdlib : free;
import core.stdc.string : strlen, memchr, memmove;

Expand Down Expand Up @@ -888,6 +889,11 @@ private:

auto sym = demangle(buf[symBeg .. symEnd], fixbuf[symBeg .. $]);

if (sym == buf[symBeg .. symEnd]) // Retry with cppdemangle
{
sym = cppdemangle(buf[symBeg .. symEnd], fixbuf[symBeg .. $], true);
}

if (sym.ptr !is fixbuf.ptr + symBeg)
{
// demangle reallocated the buffer, copy the symbol to fixbuf
Expand Down
10 changes: 9 additions & 1 deletion src/core/sys/windows/stacktrace.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module core.sys.windows.stacktrace;
version (Windows):

import core.demangle;
import core.internal.cppdemangle;
import core.runtime;
import core.stdc.stdlib;
import core.stdc.string;
Expand Down Expand Up @@ -294,7 +295,14 @@ private:
size_t decodeIndex = 0;
tempSymName = decodeDmdString(tempSymName, decodeIndex);
}
res ~= demangle(tempSymName, demangleBuf);
auto demangledName = demangle(tempSymName, demangleBuf);

if (demangledName == tempSymName) // Retry with cppdemangle
{
demangledName = cppdemangle(tempSymName, demangleBuf, true);
}

res ~= demangledName;
return res;
}

Expand Down
13 changes: 12 additions & 1 deletion src/rt/backtrace/dwarf.d
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,19 @@ bool runStateMachine(ref const(LineNumberProgram) lp, scope RunStateMachineCallb
const(char)[] getDemangledSymbol(const(char)[] btSymbol, return ref char[1024] buffer)
{
import core.demangle;
import core.internal.cppdemangle;

const mangledName = getMangledSymbolName(btSymbol);
return !mangledName.length ? buffer[0..0] : demangle(mangledName, buffer[]);
if (!mangledName.length) return buffer[0..0];

auto demangledName = demangle(mangledName, buffer[]);

if (demangledName == mangledName) // Retry with cppdemangle
{
demangledName = cppdemangle(mangledName, buffer[], true);
}

return demangledName;
}

T read(T)(ref const(ubyte)[] buffer) @nogc nothrow
Expand Down

0 comments on commit dfb01dc

Please sign in to comment.