Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to enable stack unwinding in betterC mode #16174

Closed
wants to merge 1 commit into from

Conversation

ryuukk
Copy link
Contributor

@ryuukk ryuukk commented Feb 10, 2024

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

@dlang-bot
Copy link
Contributor

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"

@rikkimax
Copy link
Contributor

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

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

@rikkimax
Copy link
Contributor

#7305 no mention as to why it was disabled.

@ryuukk
Copy link
Contributor Author

ryuukk commented Feb 10, 2024

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

@rikkimax
Copy link
Contributor

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

@RazvanN7
Copy link
Contributor

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.

@rikkimax
Copy link
Contributor

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.

@RazvanN7
Copy link
Contributor

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

@RazvanN7
Copy link
Contributor

cc @WalterBright

@dkorpel
Copy link
Contributor

dkorpel commented Feb 19, 2024

Can't you remove -betterC when debugging?

@ryuukk
Copy link
Contributor Author

ryuukk commented Feb 19, 2024

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

@dkorpel
Copy link
Contributor

dkorpel commented Feb 19, 2024

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.

@ryuukk
Copy link
Contributor Author

ryuukk commented Feb 19, 2024

I also sent this PR after riki comment #16177 is it better?

@dkorpel
Copy link
Contributor

dkorpel commented Feb 19, 2024

I think so

@RazvanN7 RazvanN7 closed this Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants