2 * mini-exceptions.c: generic exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #include <mono/metadata/appdomain.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/threads.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/gc-internal.h>
21 #include <mono/metadata/mono-debug.h>
25 #define IS_ON_SIGALTSTACK(jit_tls) ((jit_tls) && ((guint8*)&(jit_tls) > (guint8*)(jit_tls)->signal_stack) && ((guint8*)&(jit_tls) < ((guint8*)(jit_tls)->signal_stack + (jit_tls)->signal_stack_size)))
27 /* mono_find_jit_info:
29 * This function is used to gather information from @ctx. It return the
30 * MonoJitInfo of the corresponding function, unwinds one stack frame and
31 * stores the resulting context into @new_ctx. It also stores a string
32 * describing the stack location into @trace (if not NULL), and modifies
33 * the @lmf if necessary. @native_offset return the IP offset from the
34 * start of the function or -1 if that info is not available.
37 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
38 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
42 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
54 ji = mono_arch_find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, NULL, lmf, NULL, &managed2);
56 if (ji == (gpointer)-1)
59 if (managed2 || ji->method->wrapper_type) {
60 char *source_location, *tmpaddr, *fname;
61 gint32 address, iloffset;
63 address = (char *)ip - (char *)ji->code_start;
66 *native_offset = address;
69 if (!ji->method->wrapper_type)
73 source_location = mono_debug_source_location_from_address (ji->method, address, NULL, domain);
74 iloffset = mono_debug_il_offset_from_address (ji->method, address, domain);
77 tmpaddr = g_strdup_printf ("<0x%05x>", address);
79 tmpaddr = g_strdup_printf ("[0x%05x]", iloffset);
81 fname = mono_method_full_name (ji->method, TRUE);
84 *trace = g_strdup_printf ("in %s (at %s) %s", tmpaddr, source_location, fname);
86 *trace = g_strdup_printf ("in %s %s", tmpaddr, fname);
89 g_free (source_location);
95 char *fname = mono_method_full_name (res->method, TRUE);
96 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
105 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
107 MonoDomain *domain = mono_domain_get ();
109 MonoArray *ta = exc->trace_ips;
113 /* Exception is not thrown yet */
114 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
117 len = mono_array_length (ta);
119 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
121 for (i = skip; i < len; i++) {
123 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
124 gpointer ip = mono_array_get (ta, gpointer, i);
126 ji = mono_jit_info_table_find (domain, ip);
128 /* Unmanaged frame */
129 mono_array_set (res, gpointer, i, sf);
133 g_assert (ji != NULL);
135 sf->method = mono_method_get_object (domain, ji->method, NULL);
136 sf->native_offset = (char *)ip - (char *)ji->code_start;
138 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
140 if (need_file_info) {
143 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
145 sf->filename = filename? mono_string_new (domain, filename): NULL;
151 mono_array_set (res, gpointer, i, sf);
158 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data) {
159 MonoDomain *domain = mono_domain_get ();
160 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
161 MonoLMF *lmf = jit_tls->lmf;
162 MonoJitInfo *ji, rji;
163 gint native_offset, il_offset;
166 MonoContext ctx, new_ctx;
168 mono_arch_flush_register_windows ();
170 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
171 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
173 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
175 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
178 if (ji == (gpointer)-1)
181 il_offset = do_il_offset ? mono_debug_il_offset_from_address (ji->method, native_offset, domain): -1;
183 if (func (ji->method, native_offset, il_offset, managed, user_data))
191 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
192 MonoReflectionMethod **method,
193 gint32 *iloffset, gint32 *native_offset,
194 MonoString **file, gint32 *line, gint32 *column)
196 MonoDomain *domain = mono_domain_get ();
197 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
198 MonoLMF *lmf = jit_tls->lmf;
199 MonoJitInfo *ji, rji;
200 MonoContext ctx, new_ctx;
202 mono_arch_flush_register_windows ();
204 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
205 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
210 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
214 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
217 /* skip all wrappers ??*/
218 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
219 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
220 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
227 *method = mono_method_get_object (domain, ji->method, NULL);
228 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
230 if (need_file_info) {
233 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
235 *file = filename? mono_string_new (domain, filename): NULL;
245 glist_to_array (GList *list)
247 MonoDomain *domain = mono_domain_get ();
254 len = g_list_length (list);
255 res = mono_array_new (domain, mono_defaults.int_class, len);
257 for (i = 0; list; list = list->next, i++)
258 mono_array_set (res, gpointer, i, list->data);
264 * mono_handle_exception:
265 * @ctx: saved processor state
266 * @obj: the exception object
267 * @test_only: only test if the exception is caught, but dont call handlers
271 mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
273 MonoDomain *domain = mono_domain_get ();
274 MonoJitInfo *ji, rji;
275 static int (*call_filter) (MonoContext *, gpointer) = NULL;
276 static void (*restore_context) (void *);
277 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
278 MonoLMF *lmf = jit_tls->lmf;
279 GList *trace_ips = NULL;
280 MonoException *mono_ex;
281 gboolean stack_overflow = FALSE;
282 MonoContext initial_ctx;
284 gboolean gc_disabled = FALSE;
285 MonoString *initial_stack_trace = NULL;
286 GString *trace_str = NULL;
289 * This function might execute on an alternate signal stack, and Boehm GC
291 * Also, since the altstack is small, stack space intensive operations like
292 * JIT compilation should be avoided.
294 if (IS_ON_SIGALTSTACK (jit_tls)) {
296 * FIXME: disabling/enabling GC while already on a signal stack might
297 * not be safe either.
299 /* Have to reenable it later */
304 g_assert (ctx != NULL);
306 MonoException *ex = mono_get_exception_null_reference ();
307 ex->message = mono_string_new (domain, "Object reference not set to an instance of an object");
308 obj = (MonoObject *)ex;
312 * Allocate a new exception object instead of the preconstructed ones.
313 * We can't do this in sigsegv_signal_handler, since GC is not yet
316 if (obj == domain->stack_overflow_ex) {
317 obj = mono_get_exception_stack_overflow ();
319 else if (obj == domain->null_reference_ex) {
320 obj = mono_get_exception_null_reference ();
323 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
324 mono_ex = (MonoException*)obj;
325 initial_stack_trace = mono_ex->stack_trace;
330 if (obj == domain->stack_overflow_ex)
331 stack_overflow = TRUE;
334 call_filter = mono_arch_get_call_filter ();
336 if (!restore_context)
337 restore_context = mono_arch_get_restore_context ();
339 g_assert (jit_tls->end_of_stack);
340 g_assert (jit_tls->abort_func);
343 MonoContext ctx_cp = *ctx;
344 if (mono_jit_trace_calls != NULL)
345 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
346 if (!mono_handle_exception (&ctx_cp, obj, original_ip, TRUE)) {
347 if (mono_break_on_exc)
349 mono_unhandled_exception (obj);
351 if (mono_debugger_unhandled_exception (original_ip, MONO_CONTEXT_GET_SP (ctx), obj)) {
353 * If this returns true, then we're running inside the
354 * Mono Debugger and the debugger wants us to restore the
355 * context and continue (normally, the debugger inserts
356 * a breakpoint on the `original_ip', so it regains control
357 * immediately after restoring the context).
359 MONO_CONTEXT_SET_IP (ctx, original_ip);
360 restore_context (ctx);
361 g_assert_not_reached ();
367 memset (&rji, 0, sizeof (rji));
372 gboolean need_trace = FALSE;
375 if (test_only && (frame_count < 1000)) {
378 trace_str = g_string_new ("");
381 ji = mono_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
382 need_trace ? &trace : NULL, &lmf, NULL, NULL);
384 g_warning ("Exception inside function without unwind info");
385 g_assert_not_reached ();
388 if (ji != (gpointer)-1) {
390 //printf ("M: %s %d %d.\n", mono_method_full_name (ji->method, TRUE), frame_count, test_only);
392 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
394 * Avoid overwriting the stack trace if the exception is
395 * rethrown. Also avoid giant stack traces during a stack
398 if (!initial_stack_trace && (frame_count < 1000)) {
399 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
401 g_string_append (trace_str, trace);
402 g_string_append_c (trace_str, '\n');
407 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
409 free_stack = 0xffffff;
412 * During stack overflow, wait till the unwinding frees some stack
413 * space before running handlers/finalizers.
415 if ((free_stack > (64 * 1024)) && ji->num_clauses) {
418 g_assert (ji->clauses);
420 for (i = 0; i < ji->num_clauses; i++) {
421 MonoJitExceptionInfo *ei = &ji->clauses [i];
422 gboolean filtered = FALSE;
424 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
425 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
428 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
429 /* store the exception object int cfg->excvar */
430 g_assert (ji->exvar_offset);
431 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
432 if (!initial_stack_trace && trace_str) {
433 mono_ex->stack_trace = mono_string_new (domain, trace_str->str);
437 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
438 mono_debugger_handle_exception (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), obj);
439 filtered = call_filter (ctx, ei->data.filter);
442 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
443 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) || filtered) {
446 trace_ips = g_list_reverse (trace_ips);
447 mono_ex->trace_ips = glist_to_array (trace_ips);
449 g_list_free (trace_ips);
455 g_string_free (trace_str, TRUE);
458 if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
459 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
460 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
461 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
468 g_string_free (trace_str, TRUE);
471 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
472 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
473 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
474 if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
475 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
476 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
477 call_filter (ctx, ei->handler_start);
489 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
496 if (IS_ON_SIGALTSTACK (jit_tls)) {
497 /* Switch back to normal stack */
499 /* Free up some stack space */
500 MONO_CONTEXT_SET_SP (&initial_ctx, (guint32)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
501 MONO_CONTEXT_SET_IP (&initial_ctx, (unsigned int)jit_tls->abort_func);
502 restore_context (&initial_ctx);
505 jit_tls->abort_func (obj);
506 g_assert_not_reached ();
509 trace_ips = g_list_reverse (trace_ips);
510 mono_ex->trace_ips = glist_to_array (trace_ips);
512 g_list_free (trace_ips);
514 g_string_free (trace_str, TRUE);
520 g_assert_not_reached ();