How to catch Result::Err in GDB?

Hi,

In my project I have errors handling done via Errors propagation operator : "?" and from user point of view it works fine. But what I miss is that when I use debugger (GDB) I would like to catch a moment when error is created (before propagation happens), so I can inspect faulty situation.
So How can I make GDB to stop exection when Result::Err is created ?

Thanks in advance,

1 Like

In theory, the ? operator is implemented via an internal Try trait:

If you could have a breakpoint on Try's methods, it could catch errors being returned.

However, all of this code gets completely optimized out. I wouldn't be surprised if the compiler cheated and generated the ? code more directly, so I don't expect this to work in practice.

The next thing you can hook up to is Error::from() that is called for the returned Error type when ? is used on a different type. This of course depends on types of errors, so there isn't one place to put breakpoints on.

Another manual option is to create a trait with your own version of a method like Result::inspect_err() that does whatever it needs to stop GDB, and then find'n'replace ? in the code to spam it all over the place.

Thanks very much for your suggestions. I ended up to implementing my own Error type where at the moment of Creation of it I create an instance of backtrace::Backtrace (I wanted to have backtrace included ) and then in GDB I set breakpoint on this creation of backtrace:

b backtrace::capture::Backtrace::new

Thanks