How can I get a backtrace on segfault?

Jonathan Levi catanscout at gmail.com
Fri Dec 11 22:46:26 UTC 2020


On Wednesday, 14 September 2011 at 12:16:02 UTC, Steven 
Schveighoffer wrote:
> In any case, have you tried this? Works on unixen only.
>
> import core.stdc.signal;
>
> extern(C) void handleSegv(int) { assert(0); }
>
> void main()
> {
>    signal(SIGSEGV, &handleSegv);
>    ...
> }
>
> Not sure if it prints a stack trace on a stack overflow, 
> however.
>
> -Steve

Update for anyone reading this old thread:

You need to add `nothrow @nogc @system` to `handleSegv`.

```
import core.stdc.signal;

nothrow @nogc @system
extern(C) void handleSegv(int) { assert(0); }

shared static this() {
	signal(SIGSEGV, &handleSegv);
}
```

This answer worked great for me.




More information about the Digitalmars-d-learn mailing list