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

[WIP] [DONT MERGE] Enable demangling of C++ symbols in the stacktrace #3003

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions changelog/cpptrace.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Added experimental `C++` symbol demanling in the stacktrace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Added experimental `C++` symbol demanling in the stacktrace
The runtime can now demangle C++ names in stack traces

A bit more selling to the user (we tend to mark everything as "experimental" and that doesn't inspire confidence).
You can just note later that it is not enabled by default.


This feature is availableby passing the following switches to your executable (not to the compiler):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, the release note should start by describing the feature itself. You've been working on it for quite some time, so you know exactly what it entails, but not everyone does. Something like:

When a stack trace was generated by the runtime (e.g. when calling `Throwable.toString`), C++ names were not previously demangled. Hence the following code:
---
<INSERT CODE>
---
Would lead to the following stack trace:
---
<INSERT MANGLED STACK TRACE>
---

Starting from this release, C++ names will be demangled whenever possible, hence the previous stack trace will now show as:
---
<INSERT DEMANGLED STACK TRACE>
---

This feature is still experimental and must be opted-in by <BLAH BLAH>.
Demangling is only possible when <BLAH BLAH>.

Note that this is just an example, but it illustrate a few points:

  • Always describe the feature first, what it brings to the user: imagine giving an elevator pitch about it;
  • Try to show the difference between the old behavior and the new one: remember users might not be familiar with the old behavior, especially if someone looks at this release note in a few year;
  • Include a code example whenever possible (which you did already);
  • Put restrictions / limitations at the end;

- `--DRT-cpptrace=enable:y`: Enable demangling of C++ symbols
- `--DRT-cpptrace=prefix:<your_prefix>`: Change the prefix preceding the demangled C++ name (by default would be `[C++]`)
- `--DRT-cpptrace=noprefix:n`: Disable adding the prefix to C ++ demangle names

To use this function in the Posix platform you will need to link your executable to the phobos shared library, compile the program by passing -defaultlib=libphobos2.so to DMD.

Example:

```
module cpp_trace.d

extern(C++) void f1()
{
throw new Exception("exception");
}

void main()
{
try
{
f1();
}
catch (Exception e)
{
import core.stdc.stdio;
auto str = e.toString();
printf("%.*s\n", cast(int)str.length, str.ptr);
}
}
```

If you run the executable with the following switches `./<executable> --DRT-cpptrace=enable:y`, you would get this output:

```
object.Exception@cpp_trace.d(3): exception
----------------
src/cpp_trace.d:5 [C++] f1() [ADDR]
src/cpp_trace.d:12 _Dmain [ADDR]
```

Instead, if you run the executable with the following switches `./<executable> --DRT-cpptrace=enable:y --DRT-cpptrace=prefix:CPP`, you would get this output:

```
object.Exception@cpp_trace.d(5): exception
----------------
src/cpp_trace.d:5 [CPP] f1() [ADDR]
src/cpp_trace.d:12 _Dmain [ADDR]
```
2 changes: 2 additions & 0 deletions mak/COPY
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ COPY=\
$(IMPDIR)\core\internal\atomic.d \
$(IMPDIR)\core\internal\attributes.d \
$(IMPDIR)\core\internal\convert.d \
$(IMPDIR)\core\internal\cppdemangle.d \
$(IMPDIR)\core\internal\cpptrace.d \
$(IMPDIR)\core\internal\dassert.d \
$(IMPDIR)\core\internal\destruction.d \
$(IMPDIR)\core\internal\entrypoint.d \
Expand Down
2 changes: 2 additions & 0 deletions mak/SRCS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SRCS=\
src\core\internal\atomic.d \
src\core\internal\attributes.d \
src\core\internal\convert.d \
src\core\internal\cppdemangle.d \
src\core\internal\cpptrace.d \
src\core\internal\dassert.d \
src\core\internal\destruction.d \
src\core\internal\entrypoint.d \
Expand Down
6 changes: 6 additions & 0 deletions mak/WINDOWS
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ $(IMPDIR)\core\internal\attributes.d : src\core\internal\attributes.d
$(IMPDIR)\core\internal\convert.d : src\core\internal\convert.d
copy $** $@

$(IMPDIR)\core\internal\cppdemangle.d : src\core\internal\cppdemangle.d
copy $** $@

$(IMPDIR)\core\internal\cpptrace.d : src\core\internal\cpptrace.d
copy $** $@

$(IMPDIR)\core\internal\dassert.d : src\core\internal\dassert.d
copy $** $@

Expand Down
3 changes: 3 additions & 0 deletions posix.mak
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ $(ROOT)/unittest/% : $(ROOT)/unittest/test_runner

$(addsuffix /.run,$(filter-out test/shared,$(ADDITIONAL_TESTS))): $(DRUNTIME)
test/shared/.run: $(DRUNTIMESO)
ifeq (1,$(SHARED))
test/exceptions/.run: $(DRUNTIMESO) $(DRUNTIME)
endif

test/%/.run: test/%/Makefile $(DMD)
$(QUIET)$(MAKE) -C test/$* MODEL=$(MODEL) OS=$(OS) DMD=$(abspath $(DMD)) BUILD=$(BUILD) \
Expand Down