2 * mini-exceptions.c: generic exception support
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #ifdef HAVE_EXECINFO_H
19 #ifndef PLATFORM_WIN32
20 #include <sys/types.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/tabledefs.h>
26 #include <mono/metadata/threads.h>
27 #include <mono/metadata/debug-helpers.h>
28 #include <mono/metadata/exception.h>
29 #include <mono/metadata/gc-internal.h>
30 #include <mono/metadata/mono-debug.h>
31 #include <mono/metadata/profiler.h>
32 #include <mono/utils/mono-mmap.h>
38 #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)))
40 #ifndef MONO_ARCH_CONTEXT_DEF
41 #define MONO_ARCH_CONTEXT_DEF
44 #ifndef mono_find_jit_info
46 /* mono_find_jit_info:
48 * This function is used to gather information from @ctx. It return the
49 * MonoJitInfo of the corresponding function, unwinds one stack frame and
50 * stores the resulting context into @new_ctx. It also stores a string
51 * describing the stack location into @trace (if not NULL), and modifies
52 * the @lmf if necessary. @native_offset return the IP offset from the
53 * start of the function or -1 if that info is not available.
56 mono_find_jit_info (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoJitInfo *res, MonoJitInfo *prev_ji, MonoContext *ctx,
57 MonoContext *new_ctx, char **trace, MonoLMF **lmf, int *native_offset,
61 gpointer ip = MONO_CONTEXT_GET_IP (ctx);
73 ji = mono_arch_find_jit_info (domain, jit_tls, res, prev_ji, ctx, new_ctx, NULL, lmf, NULL, &managed2);
75 if (ji == (gpointer)-1)
78 if (managed2 || ji->method->wrapper_type) {
79 const char *real_ip, *start;
82 start = (const char *)ji->code_start;
84 /* ctx->ip points into native code */
85 real_ip = (const char*)MONO_CONTEXT_GET_IP (new_ctx);
87 real_ip = (const char*)ip;
89 if ((real_ip >= start) && (real_ip <= start + ji->code_size))
90 offset = real_ip - start;
95 *native_offset = offset;
98 if (!ji->method->wrapper_type)
102 *trace = mono_debug_print_stack_frame (ji->method, offset, domain);
105 char *fname = mono_method_full_name (res->method, TRUE);
106 *trace = g_strdup_printf ("in (unmanaged) %s", fname);
114 #endif /* mono_find_jit_info */
117 ves_icall_System_Exception_get_trace (MonoException *ex)
119 MonoDomain *domain = mono_domain_get ();
121 MonoArray *ta = ex->trace_ips;
126 /* Exception is not thrown yet */
129 len = mono_array_length (ta);
130 trace_str = g_string_new ("");
131 for (i = 0; i < len; i++) {
133 gpointer ip = mono_array_get (ta, gpointer, i);
135 ji = mono_jit_info_table_find (domain, ip);
137 /* Unmanaged frame */
138 g_string_append_printf (trace_str, "in (unmanaged) %p\n", ip);
143 address = (char *)ip - (char *)ji->code_start;
144 location = mono_debug_print_stack_frame (
145 ji->method, address, ex->object.vtable->domain);
147 g_string_append_printf (trace_str, "%s\n", location);
152 res = mono_string_new (ex->object.vtable->domain, trace_str->str);
153 g_string_free (trace_str, TRUE);
159 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
161 MonoDomain *domain = mono_domain_get ();
163 MonoArray *ta = exc->trace_ips;
164 MonoDebugSourceLocation *location;
168 /* Exception is not thrown yet */
169 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
172 len = mono_array_length (ta);
174 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
176 for (i = skip; i < len; i++) {
178 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
179 gpointer ip = mono_array_get (ta, gpointer, i);
181 ji = mono_jit_info_table_find (domain, ip);
183 /* Unmanaged frame */
184 mono_array_setref (res, i, sf);
188 g_assert (ji != NULL);
190 if (ji->method->wrapper_type) {
194 s = mono_method_full_name (ji->method, TRUE);
195 MONO_OBJECT_SETREF (sf, internal_method_name, mono_string_new (domain, s));
199 MONO_OBJECT_SETREF (sf, method, mono_method_get_object (domain, ji->method, NULL));
200 sf->native_offset = (char *)ip - (char *)ji->code_start;
203 * mono_debug_lookup_source_location() returns both the file / line number information
204 * and the IL offset. Note that computing the IL offset is already an expensive
205 * operation, so we shouldn't call this method twice.
207 location = mono_debug_lookup_source_location (ji->method, sf->native_offset, domain);
209 sf->il_offset = location->il_offset;
213 if (need_file_info) {
215 MONO_OBJECT_SETREF (sf, filename, mono_string_new (domain, location->source_file));
216 sf->line = location->row;
217 sf->column = location->column;
219 sf->line = sf->column = 0;
224 mono_debug_free_source_location (location);
225 mono_array_setref (res, i, sf);
233 * @domain: starting appdomain
234 * @jit_tls: JIT data for the thread
235 * @start_ctx: starting state of the stack frame
236 * @func: callback to call for each stack frame
237 * @user_data: data passed to the callback
239 * This function walks the stack of a thread, starting from the state
240 * represented by jit_tls and start_ctx. For each frame the callback
241 * function is called with the relevant info. The walk ends when no more
242 * managed stack frames are found or when the callback returns a TRUE value.
243 * Note that the function can be used to walk the stack of a thread
244 * different from the current.
247 mono_walk_stack (MonoDomain *domain, MonoJitTlsData *jit_tls, MonoContext *start_ctx, MonoStackFrameWalk func, gpointer user_data)
249 MonoLMF *lmf = mono_get_lmf ();
250 MonoJitInfo *ji, rji;
253 MonoContext ctx, new_ctx;
257 while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
259 * FIXME: mono_find_jit_info () will need to be able to return a different
260 * MonoDomain when apddomain transitions are found on the stack.
262 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
263 if (!ji || ji == (gpointer)-1)
266 if (func (domain, &new_ctx, ji, user_data))
273 #ifndef CUSTOM_STACK_WALK
276 mono_jit_walk_stack_from_ctx (MonoStackWalk func, MonoContext *start_ctx, gboolean do_il_offset, gpointer user_data)
278 MonoDomain *domain = mono_domain_get ();
279 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
280 MonoLMF *lmf = mono_get_lmf ();
281 MonoJitInfo *ji, rji;
282 gint native_offset, il_offset;
284 MonoContext ctx, new_ctx;
286 MONO_ARCH_CONTEXT_DEF
288 mono_arch_flush_register_windows ();
291 memcpy (&ctx, start_ctx, sizeof (MonoContext));
293 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
294 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
296 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, mono_jit_walk_stack_from_ctx);
300 while (MONO_CONTEXT_GET_SP (&ctx) < jit_tls->end_of_stack) {
301 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
304 if (ji == (gpointer)-1)
308 MonoDebugSourceLocation *source;
310 source = mono_debug_lookup_source_location (ji->method, native_offset, domain);
311 il_offset = source ? source->il_offset : -1;
312 mono_debug_free_source_location (source);
316 if (func (ji->method, native_offset, il_offset, managed, user_data))
324 mono_jit_walk_stack (MonoStackWalk func, gboolean do_il_offset, gpointer user_data)
326 mono_jit_walk_stack_from_ctx (func, NULL, do_il_offset, user_data);
330 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
331 MonoReflectionMethod **method,
332 gint32 *iloffset, gint32 *native_offset,
333 MonoString **file, gint32 *line, gint32 *column)
335 MonoDomain *domain = mono_domain_get ();
336 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
337 MonoLMF *lmf = mono_get_lmf ();
338 MonoJitInfo *ji, rji;
339 MonoContext ctx, new_ctx;
340 MonoDebugSourceLocation *location;
342 MONO_ARCH_CONTEXT_DEF;
344 mono_arch_flush_register_windows ();
346 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
347 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
349 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_get_frame_info);
353 * FIXME: This is needed because of the LMF stuff which doesn't exist on ia64.
354 * Probably the whole mono_find_jit_info () stuff needs to be fixed so this isn't
355 * needed even on other platforms. This is also true for s390/s390x.
357 #if !defined(__ia64__) && !defined(__s390__) && !defined(__s390x__)
362 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
366 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_SP (&ctx) >= jit_tls->end_of_stack)
369 /* skip all wrappers ??*/
370 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
371 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
372 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
373 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
374 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
381 *method = mono_method_get_object (domain, ji->method, NULL);
383 location = mono_debug_lookup_source_location (ji->method, *native_offset, domain);
385 *iloffset = location->il_offset;
389 if (need_file_info) {
391 *file = mono_string_new (domain, location->source_file);
392 *line = location->row;
393 *column = location->column;
400 mono_debug_free_source_location (location);
405 #endif /* CUSTOM_STACK_WALK */
409 MonoSecurityFrame *frame;
410 } MonoFrameSecurityInfo;
413 callback_get_first_frame_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
415 MonoFrameSecurityInfo *si = (MonoFrameSecurityInfo*) data;
417 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
418 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
419 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
420 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
421 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
422 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
431 si->frame = mono_declsec_create_frame (domain, ji);
433 /* Stop - we only want the first frame (e.g. LinkDemand and InheritanceDemand) */
438 * ves_icall_System_Security_SecurityFrame_GetSecurityFrame:
439 * @skip: the number of stack frames to skip
441 * This function returns a the security informations of a single stack frame
442 * (after the skipped ones). This is required for [NonCas]LinkDemand[Choice]
443 * and [NonCas]InheritanceDemand[Choice] as only the caller security is
447 ves_icall_System_Security_SecurityFrame_GetSecurityFrame (gint32 skip)
449 MonoDomain *domain = mono_domain_get ();
450 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
451 MonoFrameSecurityInfo si;
454 MONO_ARCH_CONTEXT_DEF
456 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
457 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
459 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityFrame);
464 mono_walk_stack (domain, jit_tls, &ctx, callback_get_first_frame_security_info, (gpointer)&si);
466 return (si.skips == 0) ? si.frame : NULL;
478 grow_array (MonoSecurityStack *stack)
480 MonoDomain *domain = mono_domain_get ();
481 guint32 newsize = (stack->maximum << 1);
482 MonoArray *newstack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, newsize);
484 for (i=0; i < stack->maximum; i++) {
485 gpointer frame = mono_array_get (stack->stack, gpointer, i);
486 mono_array_setref (newstack, i, frame);
488 stack->maximum = newsize;
489 stack->stack = newstack;
493 callback_get_stack_frames_security_info (MonoDomain *domain, MonoContext *ctx, MonoJitInfo *ji, gpointer data)
495 MonoSecurityStack *ss = (MonoSecurityStack*) data;
497 /* FIXME: skip all wrappers ?? probably not - case by case testing is required */
498 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
499 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_INVOKE ||
500 ji->method->wrapper_type == MONO_WRAPPER_XDOMAIN_DISPATCH ||
501 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
502 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE) {
511 if (ss->count == ss->maximum)
514 mono_array_setref (ss->stack, ss->count++, mono_declsec_create_frame (domain, ji));
516 /* continue down the stack */
521 glist_to_array (GList *list, MonoClass *eclass)
523 MonoDomain *domain = mono_domain_get ();
530 len = g_list_length (list);
531 res = mono_array_new (domain, eclass, len);
533 for (i = 0; list; list = list->next, i++)
534 mono_array_set (res, gpointer, i, list->data);
540 * ves_icall_System_Security_SecurityFrame_GetSecurityStack:
541 * @skip: the number of stack frames to skip
543 * This function returns an managed array of containing the security
544 * informations for each frame (after the skipped ones). This is used for
545 * [NonCas]Demand[Choice] where the complete evaluation of the stack is
549 ves_icall_System_Security_SecurityFrame_GetSecurityStack (gint32 skip)
551 MonoDomain *domain = mono_domain_get ();
552 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
553 MonoSecurityStack ss;
556 MONO_ARCH_CONTEXT_DEF
558 #ifdef MONO_INIT_CONTEXT_FROM_CURRENT
559 MONO_INIT_CONTEXT_FROM_CURRENT (&ctx);
561 MONO_INIT_CONTEXT_FROM_FUNC (&ctx, ves_icall_System_Security_SecurityFrame_GetSecurityStack);
566 ss.maximum = MONO_CAS_INITIAL_STACK_SIZE;
567 ss.stack = mono_array_new (domain, mono_defaults.runtimesecurityframe_class, ss.maximum);
568 mono_walk_stack (domain, jit_tls, &ctx, callback_get_stack_frames_security_info, (gpointer)&ss);
569 /* g_warning ("STACK RESULT: %d out of %d", ss.count, ss.maximum); */
573 #ifndef CUSTOM_EXCEPTION_HANDLING
576 * mono_handle_exception_internal:
577 * @ctx: saved processor state
578 * @obj: the exception object
579 * @test_only: only test if the exception is caught, but dont call handlers
580 * @out_filter_idx: out parameter. if test_only is true, set to the index of
581 * the first filter clause which caught the exception.
584 mono_handle_exception_internal (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only, gint32 *out_filter_idx)
586 MonoDomain *domain = mono_domain_get ();
587 MonoJitInfo *ji, rji;
588 static int (*call_filter) (MonoContext *, gpointer) = NULL;
589 static void (*restore_context) (void *);
590 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
591 MonoLMF *lmf = mono_get_lmf ();
592 MonoArray *initial_trace_ips = NULL;
593 GList *trace_ips = NULL;
594 MonoException *mono_ex;
595 gboolean stack_overflow = FALSE;
596 MonoContext initial_ctx;
598 gboolean has_dynamic_methods = FALSE;
599 gint32 filter_idx, first_filter_idx;
601 g_assert (ctx != NULL);
603 MonoException *ex = mono_get_exception_null_reference ();
604 MONO_OBJECT_SETREF (ex, message, mono_string_new (domain, "Object reference not set to an instance of an object"));
605 obj = (MonoObject *)ex;
609 * Allocate a new exception object instead of the preconstructed ones.
610 * We can't do this in sigsegv_signal_handler, since GC is not yet
613 if (obj == domain->stack_overflow_ex) {
614 obj = mono_get_exception_stack_overflow ();
615 stack_overflow = TRUE;
617 else if (obj == domain->null_reference_ex) {
618 obj = mono_get_exception_null_reference ();
621 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
622 mono_ex = (MonoException*)obj;
623 initial_trace_ips = mono_ex->trace_ips;
629 call_filter = mono_arch_get_call_filter ();
631 if (!restore_context)
632 restore_context = mono_arch_get_restore_context ();
634 g_assert (jit_tls->end_of_stack);
635 g_assert (jit_tls->abort_func);
638 MonoContext ctx_cp = *ctx;
639 if (mono_trace_is_enabled ())
640 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
641 mono_profiler_exception_thrown (obj);
642 if (!mono_handle_exception_internal (&ctx_cp, obj, original_ip, TRUE, &first_filter_idx)) {
643 if (mono_break_on_exc)
645 mono_unhandled_exception (obj);
647 if (mono_debugger_unhandled_exception (original_ip, MONO_CONTEXT_GET_SP (ctx), obj)) {
649 * If this returns true, then we're running inside the
650 * Mono Debugger and the debugger wants us to restore the
651 * context and continue (normally, the debugger inserts
652 * a breakpoint on the `original_ip', so it regains control
653 * immediately after restoring the context).
655 MONO_CONTEXT_SET_IP (ctx, original_ip);
656 restore_context (ctx);
657 g_assert_not_reached ();
663 *out_filter_idx = -1;
666 memset (&rji, 0, sizeof (rji));
672 ji = mono_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
673 NULL, &lmf, NULL, NULL);
675 g_warning ("Exception inside function without unwind info");
676 g_assert_not_reached ();
679 if (ji != (gpointer)-1) {
681 //printf ("M: %s %d %d.\n", mono_method_full_name (ji->method, TRUE), frame_count, test_only);
683 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
685 * Avoid overwriting the stack trace if the exception is
686 * rethrown. Also avoid giant stack traces during a stack
689 if (!initial_trace_ips && (frame_count < 1000)) {
690 trace_ips = g_list_prepend (trace_ips, MONO_CONTEXT_GET_IP (ctx));
694 if (ji->method->dynamic)
695 has_dynamic_methods = TRUE;
698 #ifndef MONO_ARCH_STACK_GROWS_UP
699 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx));
701 free_stack = (guint8*)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (guint8*)(MONO_CONTEXT_GET_SP (ctx));
704 free_stack = 0xffffff;
707 * During stack overflow, wait till the unwinding frees some stack
708 * space before running handlers/finalizers.
710 if ((free_stack > (64 * 1024)) && ji->num_clauses) {
713 for (i = 0; i < ji->num_clauses; i++) {
714 MonoJitExceptionInfo *ei = &ji->clauses [i];
715 gboolean filtered = FALSE;
718 if (ei->try_start < MONO_CONTEXT_GET_IP (ctx) &&
720 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
722 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
725 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
726 /* store the exception object in bp + ei->exvar_offset */
727 *((gpointer *)(gpointer)((char *)MONO_CONTEXT_GET_BP (ctx) + ei->exvar_offset)) = obj;
730 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
731 // mono_debugger_handle_exception (ei->data.filter, MONO_CONTEXT_GET_SP (ctx), obj);
733 filtered = call_filter (ctx, ei->data.filter);
734 if (filtered && out_filter_idx)
735 *out_filter_idx = filter_idx;
739 * Filter clauses should only be run in the
740 * first pass of exception handling.
742 filtered = (filter_idx == first_filter_idx);
747 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
748 mono_object_isinst (obj, ei->data.catch_class)) || filtered) {
750 if (mono_ex && !initial_trace_ips) {
751 trace_ips = g_list_reverse (trace_ips);
752 MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
753 if (has_dynamic_methods)
754 /* These methods could go away anytime, so compute the stack trace now */
755 MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
757 g_list_free (trace_ips);
761 if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
762 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
763 mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
764 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
765 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
766 *(mono_get_lmf_addr ()) = lmf;
770 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
771 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
772 (ei->flags == MONO_EXCEPTION_CLAUSE_FAULT)) {
773 if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
774 g_print ("EXCEPTION: fault clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
775 mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
776 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
777 call_filter (ctx, ei->handler_start);
779 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
780 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
781 (ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)) {
782 if (mono_trace_is_enabled () && mono_trace_eval (ji->method))
783 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
784 mono_profiler_exception_clause_handler (ji->method, ei->flags, i);
785 mono_debugger_handle_exception (ei->handler_start, MONO_CONTEXT_GET_SP (ctx), obj);
786 call_filter (ctx, ei->handler_start);
793 mono_profiler_exception_method_leave (ji->method);
798 if (ji == (gpointer)-1) {
801 *(mono_get_lmf_addr ()) = lmf;
803 if (IS_ON_SIGALTSTACK (jit_tls)) {
804 /* Switch back to normal stack */
805 if (stack_overflow) {
806 /* Free up some stack space */
807 #ifndef MONO_ARCH_STACK_GROWS_UP
808 MONO_CONTEXT_SET_SP (&initial_ctx, (gssize)(MONO_CONTEXT_GET_SP (&initial_ctx)) + (64 * 1024));
809 g_assert ((gssize)MONO_CONTEXT_GET_SP (&initial_ctx) < (gssize)jit_tls->end_of_stack);
811 MONO_CONTEXT_SET_SP (&initial_ctx, (gssize)(MONO_CONTEXT_GET_SP (&initial_ctx)) - (64 * 1024));
812 g_assert ((gssize)MONO_CONTEXT_GET_SP (&initial_ctx) > (gssize)jit_tls->end_of_stack);
815 #ifdef MONO_CONTEXT_SET_FUNC
816 /* jit_tls->abort_func is a function descriptor on ia64 */
817 MONO_CONTEXT_SET_FUNC (&initial_ctx, (gssize)jit_tls->abort_func);
819 MONO_CONTEXT_SET_IP (&initial_ctx, (gssize)jit_tls->abort_func);
821 restore_context (&initial_ctx);
824 jit_tls->abort_func (obj);
825 g_assert_not_reached ();
827 if (mono_ex && !initial_trace_ips) {
828 trace_ips = g_list_reverse (trace_ips);
829 MONO_OBJECT_SETREF (mono_ex, trace_ips, glist_to_array (trace_ips, mono_defaults.int_class));
830 if (has_dynamic_methods)
831 /* These methods could go away anytime, so compute the stack trace now */
832 MONO_OBJECT_SETREF (mono_ex, stack_trace, ves_icall_System_Exception_get_trace (mono_ex));
834 g_list_free (trace_ips);
840 g_assert_not_reached ();
844 * mono_debugger_run_finally:
845 * @start_ctx: saved processor state
847 * This method is called by the Mono Debugger to call all `finally' clauses of the
848 * current stack frame. It's used when the user issues a `return' command to make
849 * the current stack frame return. After returning from this method, the debugger
850 * unwinds the stack one frame and gives control back to the user.
852 * NOTE: This method is only used when running inside the Mono Debugger.
855 mono_debugger_run_finally (MonoContext *start_ctx)
857 static int (*call_filter) (MonoContext *, gpointer) = NULL;
858 MonoDomain *domain = mono_domain_get ();
859 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
860 MonoLMF *lmf = mono_get_lmf ();
861 MonoContext ctx, new_ctx;
862 MonoJitInfo *ji, rji;
867 ji = mono_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
868 if (!ji || ji == (gpointer)-1)
872 call_filter = mono_arch_get_call_filter ();
874 for (i = 0; i < ji->num_clauses; i++) {
875 MonoJitExceptionInfo *ei = &ji->clauses [i];
877 if ((ei->try_start <= MONO_CONTEXT_GET_IP (&ctx)) &&
878 (MONO_CONTEXT_GET_IP (&ctx) < ei->try_end) &&
879 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
880 call_filter (&ctx, ei->handler_start);
886 * mono_handle_exception:
887 * @ctx: saved processor state
888 * @obj: the exception object
889 * @test_only: only test if the exception is caught, but dont call handlers
892 mono_handle_exception (MonoContext *ctx, gpointer obj, gpointer original_ip, gboolean test_only)
894 return mono_handle_exception_internal (ctx, obj, original_ip, test_only, NULL);
897 #endif /* CUSTOM_EXCEPTION_HANDLING */
899 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
901 #ifndef MONO_ARCH_USE_SIGACTION
902 #error "Can't use sigaltstack without sigaction"
906 mono_setup_altstack (MonoJitTlsData *tls)
908 pthread_t self = pthread_self();
911 struct sigaltstack sa;
912 guint8 *staddr = NULL;
913 guint8 *current = (guint8*)&staddr;
915 if (mono_running_on_valgrind ())
918 /* Determine stack boundaries */
919 pthread_attr_init( &attr );
920 #ifdef HAVE_PTHREAD_GETATTR_NP
921 pthread_getattr_np( self, &attr );
923 #ifdef HAVE_PTHREAD_ATTR_GET_NP
924 pthread_attr_get_np( self, &attr );
926 pthread_attr_getstacksize( &attr, &stsize );
928 #error "Not implemented"
932 #ifdef HAVE_PTHREAD_ATTR_GETSTACK
933 pthread_attr_getstack( &attr, (void**)&staddr, &stsize );
936 pthread_attr_destroy (&attr);
940 g_assert ((current > staddr) && (current < staddr + stsize));
942 tls->end_of_stack = staddr + stsize;
944 /*g_print ("thread %p, stack_base: %p, stack_size: %d\n", (gpointer)pthread_self (), staddr, stsize);*/
946 tls->stack_ovf_guard_base = staddr + mono_pagesize ();
947 tls->stack_ovf_guard_size = mono_pagesize () * 8;
949 if (mono_mprotect (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE)) {
950 /* mprotect can fail for the main thread stack */
951 gpointer gaddr = mono_valloc (tls->stack_ovf_guard_base, tls->stack_ovf_guard_size, MONO_MMAP_NONE|MONO_MMAP_PRIVATE|MONO_MMAP_ANON|MONO_MMAP_FIXED);
952 g_assert (gaddr == tls->stack_ovf_guard_base);
956 * threads created by nptl does not seem to have a guard page, and
957 * since the main thread is not created by us, we can't even set one.
958 * Increasing stsize fools the SIGSEGV signal handler into thinking this
959 * is a stack overflow exception.
961 tls->stack_size = stsize + mono_pagesize ();
963 /* Setup an alternate signal stack */
964 tls->signal_stack = mono_valloc (0, MONO_ARCH_SIGNAL_STACK_SIZE, MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC|MONO_MMAP_PRIVATE|MONO_MMAP_ANON);
965 tls->signal_stack_size = MONO_ARCH_SIGNAL_STACK_SIZE;
967 g_assert (tls->signal_stack);
969 sa.ss_sp = tls->signal_stack;
970 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
971 sa.ss_flags = SS_ONSTACK;
972 sigaltstack (&sa, NULL);
976 mono_free_altstack (MonoJitTlsData *tls)
978 struct sigaltstack sa;
981 sa.ss_sp = tls->signal_stack;
982 sa.ss_size = MONO_ARCH_SIGNAL_STACK_SIZE;
983 sa.ss_flags = SS_DISABLE;
984 err = sigaltstack (&sa, NULL);
987 if (tls->signal_stack)
988 mono_vfree (tls->signal_stack, MONO_ARCH_SIGNAL_STACK_SIZE);
991 #else /* !MONO_ARCH_SIGSEGV_ON_ALTSTACK */
994 mono_setup_altstack (MonoJitTlsData *tls)
999 mono_free_altstack (MonoJitTlsData *tls)
1003 #endif /* MONO_ARCH_SIGSEGV_ON_ALTSTACK */
1006 print_stack_frame (MonoMethod *method, gint32 native_offset, gint32 il_offset, gboolean managed, gpointer data)
1008 FILE *stream = (FILE*)data;
1011 gchar *location = mono_debug_print_stack_frame (method, native_offset, mono_domain_get ());
1012 fprintf (stream, " %s\n", location);
1015 fprintf (stream, " at <unknown> <0x%05x>\n", native_offset);
1020 static gboolean handling_sigsegv = FALSE;
1023 * mono_handle_native_sigsegv:
1025 * Handle a SIGSEGV received while in native code by printing diagnostic
1026 * information and aborting.
1029 mono_handle_native_sigsegv (int signal, void *ctx)
1031 #ifndef PLATFORM_WIN32
1032 struct sigaction sa;
1034 if (handling_sigsegv)
1037 /* To prevent infinite loops when the stack walk causes a crash */
1038 handling_sigsegv = TRUE;
1040 fprintf (stderr, "Stacktrace:\n\n");
1042 mono_jit_walk_stack (print_stack_frame, TRUE, stderr);
1046 #ifdef HAVE_BACKTRACE_SYMBOLS
1054 const char *signal_str = (signal == SIGSEGV) ? "SIGSEGV" : "SIGABRT";
1056 fprintf (stderr, "\nNative stacktrace:\n\n");
1058 size = backtrace (array, 256);
1059 names = backtrace_symbols (array, size);
1060 for (i =0; i < size; ++i) {
1061 fprintf (stderr, "\t%s\n", names [i]);
1067 /* Try to get more meaningful information using gdb */
1069 #ifndef PLATFORM_WIN32
1070 sprintf (cmd, "gdb --ex 'attach %ld' --ex 'info threads' --ex 'thread apply all bt' --batch", (long)getpid ());
1072 int res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
1075 fprintf (stderr, "\nDebug info from gdb:\n\n");
1076 fprintf (stderr, "%s\n", out);
1081 * A SIGSEGV indicates something went very wrong so we can no longer depend
1082 * on anything working. So try to print out lots of diagnostics, starting
1083 * with ones which have a greater chance of working.
1087 "=================================================================\n"
1088 "Got a %s while executing native code. This usually indicates\n"
1089 "a fatal error in the mono runtime or one of the native libraries \n"
1090 "used by your application.\n"
1091 "=================================================================\n"
1097 #ifndef PLATFORM_WIN32
1099 /* Remove our SIGABRT handler */
1100 sa.sa_handler = SIG_DFL;
1101 sigemptyset (&sa.sa_mask);
1104 g_assert (sigaction (SIGABRT, &sa, NULL) != -1);
1112 * mono_print_thread_dump:
1114 * Print information about the current thread to stdout.
1117 mono_print_thread_dump (void *sigctx)
1119 MonoThread *thread = mono_thread_current ();
1120 #if defined(__i386__) || defined(__x86_64__)
1124 GError *error = NULL;
1127 name = g_utf16_to_utf8 (thread->name, thread->name_len, NULL, NULL, &error);
1129 fprintf (stdout, "\n\"%s\"", name);
1133 fprintf (stdout, "\n\"\"");
1135 fprintf (stdout, " tid=0x%p this=0x%p:\n", (gpointer)(gsize)thread->tid, thread);
1138 #if defined(__i386__) || defined(__x86_64__)
1139 mono_arch_sigctx_to_monoctx (sigctx, &ctx);
1141 mono_jit_walk_stack_from_ctx (print_stack_frame, &ctx, TRUE, stdout);
1143 printf ("\t<Stack traces in thread dumps not supported on this platform>\n");