digitalmars.D.learn - catching segfault using try_ catch
- seany (2/2) Jul 11 2021 Is it possible to catch a segfault - using try/catch loop?
- Paul Backus (4/6) Jul 11 2021 I know it's possible on Linux using the `etc.linux.memoryerror`
- Adam D Ruppe (3/5) Jul 11 2021 With -m32, it just works. With -m32mscoff I'm not sure. With
- seany (3/5) Jul 11 2021 Linux would be sufficient.
- Adam D Ruppe (4/5) Jul 11 2021 You just call the registerMemoryHandler() function from that
- seany (6/11) Jul 13 2021 OK
- Adam D Ruppe (24/27) Jul 13 2021 You mean transparently rerun some code? That's better done with
- seany (4/11) Jul 13 2021 Thank you.
- wjoe (18/33) Jul 14 2021 Try core.sys.posix.signal. kill provides a list of signals via
- seany (7/34) Jul 22 2021 how can I specify this with dub ?
- seany (10/57) Jul 22 2021 I have added this to my dub.json :
- Adam D Ruppe (3/4) Jul 22 2021 Probably easiest to just copy the memoryerror.d file from
- seany (3/7) Jul 22 2021 I did copy in subfolder /etc/linux. same as this :
- seany (2/7) Jul 13 2021 PS will the function will work with LDC / LLVM based compiler?
Is it possible to catch a segfault - using try/catch loop? Thank you
Jul 11 2021
On Sunday, 11 July 2021 at 20:18:18 UTC, seany wrote:Is it possible to catch a segfault - using try/catch loop? Thank youI 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
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
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
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
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: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.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 13 2021
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
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:Thank you. Is there a tutorial on the low level sigaction handlers you speak of? Thank oyu[...]true if it succeeded.[...]You mean transparently rerun some code? That's better done with the lowlevel sigaction handler. [...]
Jul 13 2021
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: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); ```On Tuesday, 13 July 2021 at 16:52:43 UTC, seany wrote:Thank you. Is there a tutorial on the low level sigaction handlers you speak of? Thank oyu[...]true if it succeeded.[...]You mean transparently rerun some code? That's better done with the lowlevel sigaction handler. [...]
Jul 14 2021
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: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.`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 22 2021
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: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' `On Tuesday, 13 July 2021 at 16:52:43 UTC, seany wrote: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.`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 22 2021
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
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: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`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
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:PS will the function will work with LDC / LLVM based compiler?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 13 2021