www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - catching segfault using try_ catch

reply seany <seany uni-bonn.de> writes:
Is it possible to catch a segfault - using try/catch loop?
Thank you
Jul 11 2021
parent reply Paul Backus <snarwin gmail.com> writes:
On Sunday, 11 July 2021 at 20:18:18 UTC, seany wrote:
 Is it possible to catch a segfault - using try/catch loop?
 Thank you
I know it's possible on Linux using the `etc.linux.memoryerror` module [1]. Not sure about Windows. [1] https://druntime.dpldocs.info/etc.linux.memoryerror.html
Jul 11 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Sunday, 11 July 2021 at 21:15:30 UTC, Paul Backus wrote:
 I know it's possible on Linux using the `etc.linux.memoryerror` 
 module [1]. Not sure about Windows.
With -m32, it just works. With -m32mscoff I'm not sure. With -m64, as far as I know, there is no way.
Jul 11 2021
parent reply seany <seany uni-bonn.de> writes:
  On Sunday, 11 July 2021 at 21:15:30 UTC, Paul Backus wrote:
 I know it's possible on Linux using the `etc.linux.memoryerror` 
 module [1]. Not sure about Windows.
Linux would be sufficient. Is there an example i can use Thank you.
Jul 11 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Sunday, 11 July 2021 at 23:34:38 UTC, seany wrote:
 Is there an example i can use Thank you.
You just call the registerMemoryHandler() function from that module at some point in your main function before doing other work.
Jul 11 2021
next sibling parent reply seany <seany uni-bonn.de> writes:
On Monday, 12 July 2021 at 00:04:05 UTC, Adam D Ruppe wrote:
 On Sunday, 11 July 2021 at 23:34:38 UTC, seany wrote:
 Is there an example i can use Thank you.
You just call the registerMemoryHandler() function from that module at some point in your main function before doing other work.
OK Here, the [source](https://druntime.dpldocs.info/etc.linux.memoryerror.registerMemo yErrorHandler.html) says that function is undocumented. What will it return to me? I want to catch the segfault and a segfault has occured, I want run a different code at that point.
Jul 13 2021
parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Tuesday, 13 July 2021 at 16:52:43 UTC, seany wrote:
 What will it return to me?
true if it succeeded.
 I want to catch the segfault and a segfault has occured, I want 
 run a different code at that point.
You mean transparently rerun some code? That's better done with the lowlevel sigaction handler. But if you just want to standard try/catch then do something in the catch block, this is fine. import etc.linux.memoryerror; void main() { registerMemoryErrorHandler(); int* a; try { *a = 4; } catch(Throwable e) { import std.stdio; writeln("Caught"); } } It can be used on ldc2 too but it isn't as reliable since ldc considers null access to be undefined behavior anyway and thus may optimize out your catch.... You also need to compile in the module with ldc so build it like $ ldc2 seg.d ~/d/ldc/import/etc/linux/memoryerror.d just including the memoryerror file o the command line lets it link.
Jul 13 2021
next sibling parent reply seany <seany uni-bonn.de> writes:
On Tuesday, 13 July 2021 at 17:49:54 UTC, Adam D Ruppe wrote:
 On Tuesday, 13 July 2021 at 16:52:43 UTC, seany wrote:
 [...]
true if it succeeded.
 [...]
You mean transparently rerun some code? That's better done with the lowlevel sigaction handler. [...]
Thank you. Is there a tutorial on the low level sigaction handlers you speak of? Thank oyu
Jul 13 2021
parent wjoe <invalid example.com> writes:
On Wednesday, 14 July 2021 at 00:10:59 UTC, seany wrote:
 On Tuesday, 13 July 2021 at 17:49:54 UTC, Adam D Ruppe wrote:
 On Tuesday, 13 July 2021 at 16:52:43 UTC, seany wrote:
 [...]
true if it succeeded.
 [...]
You mean transparently rerun some code? That's better done with the lowlevel sigaction handler. [...]
Thank you. Is there a tutorial on the low level sigaction handlers you speak of? Thank oyu
Try core.sys.posix.signal. kill provides a list of signals via kill -L. basically what you do, if I recall correctly (it's been a few years), is something like this: ```D import core.sys.signal; enum SIGSEGV = 11; extern (C) void handler(int sig) { switch(sig){ case SIGSEGV: // handle sigsegv break; } } sigset(SIGSEGV, handler); ```
Jul 14 2021
prev sibling parent reply seany <seany uni-bonn.de> writes:
On Tuesday, 13 July 2021 at 17:49:54 UTC, Adam D Ruppe wrote:
 On Tuesday, 13 July 2021 at 16:52:43 UTC, seany wrote:
 What will it return to me?
true if it succeeded.
 I want to catch the segfault and a segfault has occured, I 
 want run a different code at that point.
You mean transparently rerun some code? That's better done with the lowlevel sigaction handler. But if you just want to standard try/catch then do something in the catch block, this is fine. import etc.linux.memoryerror; void main() { registerMemoryErrorHandler(); int* a; try { *a = 4; } catch(Throwable e) { import std.stdio; writeln("Caught"); } } It can be used on ldc2 too but it isn't as reliable since ldc considers null access to be undefined behavior anyway and thus may optimize out your catch.... You also need to compile in the module with ldc so build it like $ ldc2 seg.d ~/d/ldc/import/etc/linux/memoryerror.d just including the memoryerror file o the command line lets it link.
how can I specify this with dub ? If i do `dub build -b release --compiler=ldc2 /usr/include/dmd/druntime/import/etc/linux/memoryerror.d` ; I get : `Package '/usr/include/dmd/druntime/import/etc/linux/memoryerror.d' was neither found locally nor online.`
Jul 22 2021
next sibling parent seany <seany uni-bonn.de> writes:
On Thursday, 22 July 2021 at 14:19:37 UTC, seany wrote:
 On Tuesday, 13 July 2021 at 17:49:54 UTC, Adam D Ruppe wrote:
 On Tuesday, 13 July 2021 at 16:52:43 UTC, seany wrote:
 What will it return to me?
true if it succeeded.
 I want to catch the segfault and a segfault has occured, I 
 want run a different code at that point.
You mean transparently rerun some code? That's better done with the lowlevel sigaction handler. But if you just want to standard try/catch then do something in the catch block, this is fine. import etc.linux.memoryerror; void main() { registerMemoryErrorHandler(); int* a; try { *a = 4; } catch(Throwable e) { import std.stdio; writeln("Caught"); } } It can be used on ldc2 too but it isn't as reliable since ldc considers null access to be undefined behavior anyway and thus may optimize out your catch.... You also need to compile in the module with ldc so build it like $ ldc2 seg.d ~/d/ldc/import/etc/linux/memoryerror.d just including the memoryerror file o the command line lets it link.
how can I specify this with dub ? If i do `dub build -b release --compiler=ldc2 /usr/include/dmd/druntime/import/etc/linux/memoryerror.d` ; I get : `Package '/usr/include/dmd/druntime/import/etc/linux/memoryerror.d' was neither found locally nor online.`
I have added this to my dub.json : "mylib":{ "versions": "~master", "path": "/usr/include/dmd/druntime/import/" }, Result : `.dub/build/application-release-linux.posix-x86_64-ldc_v1.24.0-5496805BDFFAF7D74739CE42F7A6E3B0/tracker_ai o:traits.d:function _D14analysisEngine9geoEngine15analyze_tillageMFZv: error: undefined reference to '_D3etc5linux11memoryerror26registerMemoryErrorHandlerFZb' `
Jul 22 2021
prev sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Thursday, 22 July 2021 at 14:19:37 UTC, seany wrote:
 how can I specify this with dub ?
Probably easiest to just copy the memoryerror.d file from druntime into your dub source dir.
Jul 22 2021
parent seany <seany uni-bonn.de> writes:
On Thursday, 22 July 2021 at 15:12:37 UTC, Adam D Ruppe wrote:
 On Thursday, 22 July 2021 at 14:19:37 UTC, seany wrote:
 how can I specify this with dub ?
Probably easiest to just copy the memoryerror.d file from druntime into your dub source dir.
I did copy in subfolder /etc/linux. same as this : `.dub/build/application-release-linux.posix-x86_64-ldc_v1.24.0-5496805BDFFAF7D74739CE42F7A6E3B0/tracker_ai o:traits.d:function _D14analysisEngine9geoEngine15analyze_tillageMFZv: error: undefined reference to '_D3etc5linux11memoryerror26registerMemoryErrorHandlerFZb`
Jul 22 2021
prev sibling parent seany <seany uni-bonn.de> writes:
On Monday, 12 July 2021 at 00:04:05 UTC, Adam D Ruppe wrote:
 On Sunday, 11 July 2021 at 23:34:38 UTC, seany wrote:
 Is there an example i can use Thank you.
You just call the registerMemoryHandler() function from that module at some point in your main function before doing other work.
PS will the function will work with LDC / LLVM based compiler?
Jul 13 2021