Re: [Vala] Stack traces for unhandled exceptions



Interesting. Thanks Thomas for posting these pointers.
Another (possibly worse) way would be to have Vala generate code such as:
e.stack_trace += CFunctionNameToValaFunctionName(__func__);
just next to where-ever Vala is currently calling g_propagate_error().
I'm not sure which approach is better but it would be nice to have
something that worked without full debug info and also in release
builds with heavy optimizations (inlining etc). Especially for open
source programs that rely so heavily on getting bug reports from users.
Besides --debug Vala might get --release-with-stacktraces-on-exceptions?
I won't be able to work on any stack trace patch right now though, but
maybe the dear lazy web will build it.


                Martin



Thomas Chust wrote:
Martin Olsson schrieb:
[...]
One thing I really miss from C# is stack traces printed to the terminal
when an exception occurs.

* Would this be hard to add for Vala?
* What kind of infrastructure is needed before this can be implemented
in a _robust_ way?
[...]

Hello,

strictly speaking, it is impossible to add, since Vala doesn't support
exceptions, only syntactic sugar for GError handling. When a C / Vala /
whatever program signals a GError, the signalling function *returns*
(usually a special value) and sets the value of a special output
parameter. If the error is propagated through multiple calls, all the
functions up to the one that decides to handle the error return in that way.

Therefore, at the point your error handler gets to see the error object,
the stack trace leading to the point where the error was signalled is no
longer available. To support stack traces, the g_set_error function from
GLib would have to be modified such that it associates its own stack
trace with the error object that it creates.

However, you may want to have a look at the functions
g_on_error_stack_trace from GLib [1], which is bound in Vala as
GLib.on_error_stack_trace, or backtrace and friends from the GNU C
library [2]. Using these you can get stack traces for C programs
compiled with debugging information in a not completely unportable way
;-) Using the latter set of functions you could even write replacement
code for g_set_error that stuffs a stack trace in the error message and
signals a GError and using some dynamic linker magic (LD_PRELOAD=...) or
textual substitution in the C code generated by Vala you could even
trick your Vala programs to use that replacement instead of g_set_error
while you are developing.

cu,
Thomas


[1]
http://library.gnome.org/devel/glib/2.18/glib-Warnings-and-Assertions.html#g-on-error-stack-trace

[2] http://www.gnu.org/software/libtool/manual/libc/Backtraces.html





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]