dmd icon indicating copy to clipboard operation
dmd copied to clipboard

Add option to enable stack unwinding in betterC mode

Open ryuukk opened this issue 1 year ago • 9 comments

I wanted to debug my code in my project, but i'm using -betterC, wich removes the ability for the compiler to generate eh_frame, so backtrace wasn't working

This adds an option for -betterC to still get the benefits of nothrow static analysis + being able to debug code with ease, this option is disabled in -release

Documentation perhaps could be improved

void crash_me()
{
    int* a;
    *a = 42;
}

Before:

-------------------------------------------------------------------+
Received signal 'SIGSEGV' (11)
-------------------------------------------------------------------+
executable: /tmp/dmd_run1UPD7W
backtrace: 1

After:

-------------------------------------------------------------------+
Received signal 'SIGSEGV' (11)
-------------------------------------------------------------------+
executable: /tmp/dmd_runXn753y
backtrace: 7
    ??:? _start+0x25
    ??:? __libc_start_main+0x8a
    ??:? +0x25cd0
    /run/media/ryuukk/E0C0C01FC0BFFA3C/dev/kdom/./_.d:4 main+0xe
    /run/media/ryuukk/E0C0C01FC0BFFA3C/dev/kdom/./_.d:11 void _.crash_me()+0xe

Test code:

dmd -fPIC -fPIE -gs -debug -g -betterC -run _.d https://gist.github.com/ryuukk/e2d5260b4c4acec1b972ad9ce9228753

ryuukk avatar Feb 10 '24 20:02 ryuukk

Thanks for your pull request and interest in making D better, @ryuukk! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#16174"

dlang-bot avatar Feb 10 '24 20:02 dlang-bot

I'm curious, why was this disabled in the first place?

-betterC does not imply that the entire process does not use exceptions.

rikkimax avatar Feb 10 '24 20:02 rikkimax

https://github.com/dlang/dmd/pull/7305 no mention as to why it was disabled.

rikkimax avatar Feb 10 '24 20:02 rikkimax

I'm curious, why was this disabled in the first place?

-betterC does not imply that the entire process does not use exceptions.

I don't know, but when using -betterC, it sets: params.useExceptions = false;

Perhaps the right thing to do is to remove that and let user decide by using -nothrow directly, i think that's how it should be done, but i don't know the rationale behind the default

ryuukk avatar Feb 10 '24 20:02 ryuukk

Agreed, opt-out is better for this I think.

rikkimax avatar Feb 10 '24 23:02 rikkimax

I'm curious, why was this disabled in the first place? -betterC does not imply that the entire process does not use exceptions.

Actually it does. Exceptions are by default allocated with the gc which is not available if you're not using the runtime. The fact that a function is nothrow does not mean that exceptions are not going to be allocated. There is a compiler flag -dip1008 which makes exceptions to be ref counted. This should work with betterC, however, it has a limitation: exceptions that are catched cannot escape the catch block. However, that does not seem to be the case in your situation.

RazvanN7 avatar Feb 12 '24 12:02 RazvanN7

I'm curious, why was this disabled in the first place? -betterC does not imply that the entire process does not use exceptions.

Actually it does. Exceptions are by default allocated with the gc which is not available if you're not using the runtime. The fact that a function is nothrow does not mean that exceptions are not going to be allocated. There is a compiler flag -dip1008 which makes exceptions to be ref counted. This should work with betterC, however, it has a limitation: exceptions that are catched cannot escape the catch block. However, that does not seem to be the case in your situation.

You have misunderstood this.

The unwinding tables are separate from being able to throw and catch.

rikkimax avatar Feb 12 '24 12:02 rikkimax

It might be possible that the implementer made the same term confusion as I did.

RazvanN7 avatar Feb 12 '24 12:02 RazvanN7

cc @WalterBright

RazvanN7 avatar Feb 12 '24 12:02 RazvanN7

Can't you remove -betterC when debugging?

dkorpel avatar Feb 19 '24 13:02 dkorpel

The point is to improve debugging with -betterC (technically it is -nothrow), not to ignore it

ryuukk avatar Feb 19 '24 14:02 ryuukk

Generating unwinding tables in -betterC is fine, but I'm not a fan of the added -unwinding flag. In general, 'flags are bugs', and this one undoes the undoing of another flag.

dkorpel avatar Feb 19 '24 14:02 dkorpel

I also sent this PR after riki comment https://github.com/dlang/dmd/pull/16177 is it better?

ryuukk avatar Feb 19 '24 14:02 ryuukk

I think so

dkorpel avatar Feb 19 '24 14:02 dkorpel