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

Make -betterC not disable EH, let -nothrow handle that #16177

Closed
wants to merge 3 commits into from

Conversation

ryuukk
Copy link
Contributor

@ryuukk ryuukk commented Feb 11, 2024

See #16174 for context

-nothrow was added recently, wich is what the user should use to disable EH, otherwise APIs like backtrace can't be used to generate stack traces, wich is useful for debugging

I added a change log entry, but my english might be poor, so please don't hesitate to rephrase it

@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#16177"

@ryuukk
Copy link
Contributor Author

ryuukk commented Aug 6, 2024

Can someone review this please?

@ryuukk
Copy link
Contributor Author

ryuukk commented Aug 6, 2024

To help understand what this PR does:

For cases where one uses -betterC or uses a custom runtime, it makes it possible to still use debugging APIs like backtrace

Example code:

extern(C) void main()
{
    rt_register_crash_handler();
    crash_me();
}


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

(full code + rt_register_crash_handler impl: https://gist.github.com/ryuukk/)e2d5260b4c4acec1b972ad9ce9228753)

The code should crash and print:

-------------------------------------------------------------------+
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

But if i compile with -betterC or with a custom runtime, i get:

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

Not helpful at all..

This PR solves that

ping @WalterBright

Copy link
Contributor

@dkorpel dkorpel left a comment

Choose a reason for hiding this comment

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

The idea sounds good, but tests are currently failing and the log is expired so I can't see why.

It's also curious that there are no changes for Windows, does that already generate the necessary code for backtraces with -betterC?

@@ -0,0 +1,5 @@
Stack unwinding is now working in `-betterC` mode.

It can now properly use APIs like `backtrace` to generate stack traces, wich is very useful for debugging purposes.
Copy link
Contributor

Choose a reason for hiding this comment

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

When you say 'working', it might give the impression that a failing assert now gives a stack trace out of the box. Perhaps clarify that it 'works' on a more technical level, that the generated code contains the unwind info needed for e.g. libunwind.

@WalterBright
Copy link
Member

The idea of -betterC is to not require the use of the D runtime library - only the C runtime library is needed. This PR requires linking in druntime, defeating the entire purpose of betterC.

To get backtraces in betterC, do one of:

  1. do it like C compilers do
  2. turn off -betterC, debug the code, then turn it back on

Copy link
Member

@WalterBright WalterBright left a comment

Choose a reason for hiding this comment

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

See my other comment

@ryuukk
Copy link
Contributor Author

ryuukk commented Aug 20, 2024

The idea of -betterC is to not require the use of the D runtime library - only the C runtime library is needed. This PR requires linking in druntime, defeating the entire purpose of betterC.

To get backtraces in betterC, do one of:

1. do it like C compilers do

2. turn off -betterC, debug the code, then turn it back on

I didn't know it would require linking druntime, in that case, this is a bad PR

  1. i'll investigate, if you have pointers/ideas please share
  2. that's what i am doing currently, but would prefer not to, since i rely on -betterC to make sure i don't do things that require TypeInfo

I'll close it until i can find a better solution

@ryuukk ryuukk closed this Aug 20, 2024
@rikkimax
Copy link
Contributor

I've had to do another look into ldc, looks like I've been wrong, it does turn off the unwinding tables for -betterC too.

Which is unfortunate, this certainly could be templated and turned on, oh well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants