Bug 235033

Summary: [feature suggestion] Stack should be unwound and exception should be printed when a C++ application throws an uncaught exception
Product: Base System Reporter: Yuri Victorovich <yuri>
Component: miscAssignee: freebsd-bugs (Nobody) <bugs>
Status: New ---    
Severity: Affects Only Me CC: cem
Priority: ---    
Version: CURRENT   
Hardware: Any   
OS: Any   

Description Yuri Victorovich freebsd_committer freebsd_triage 2019-01-17 21:28:56 UTC
This program
> int main() {
>   throw "Hello!";
> }
prints
> Abort trap

It could unwind stack and print the exception message in cases when there are pending exceptions.                

The C++17 standard draft says
> 18.3 (note 9) If no matching handler is found, the function std::terminate() is called; whether or not the stack is unwound before this call to std::terminate() is implementation-defined.

(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf)

I propose that such stack unwinding is performed. This would make the behavior of programs that fail to catch exceptions more user friendly, and their error message more informative.
Comment 1 Yuri Victorovich freebsd_committer freebsd_triage 2019-01-17 22:02:11 UTC
I take it back. There is no general way to print exceptions when multiple languages can be present.
Comment 2 Conrad Meyer freebsd_committer freebsd_triage 2019-01-17 22:12:19 UTC
I agree it'd be nice, and match a lot of higher level languages!

Not sure what multiple languages means.  If C code is compiled with -fexceptions, C++ exceptions can be raised across C libraries.  I'm not sure if we do that.  Probably not; we don't have a lot of C++ libraries in base.

I think a similar behavior might be useful for C programs on SIGABRT.  In fact, we do this (grabbing the signal-induced exit stack) at Isilon as part of coredump() in kern_sig.c, and log it to syslog.
Comment 3 Yuri Victorovich freebsd_committer freebsd_triage 2019-01-17 22:54:29 UTC
(In reply to Conrad Meyer from comment #2)

I think that it isn't possible to generically catch exceptions. Even in C++ alone, object of any type can be thrown, ex. a primitive type, std::string, char* or even void*. These types can only be printed in application-specific manner.

I closed this PR on the second thought.

It would be nice if exception is identified as a C++ exception and printed, but I also feel that this would be kind-of a hack.

But if somebody feels differently, and thinks that it makes sense to implement such special C++ exceptions handling, they are welcome to do so. :-)
Comment 4 Conrad Meyer freebsd_committer freebsd_triage 2019-01-17 23:17:29 UTC
Yeah, that makes sense.  The stack might still be useful, even without exception information?  At least the stack + information about how the process exited (due to exception raised, or some other reason -- segfault? abort?) might be interesting.
Comment 5 Yuri Victorovich freebsd_committer freebsd_triage 2019-01-17 23:22:12 UTC
(In reply to Conrad Meyer from comment #4)

The stack might be useful to infer the exception type and to print it in case it was forgotten.

I am reopening this PR in order to attract more comments. I see the potential usefulness of this, but I don't like the hacklike character of the proposed solution.