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)))
28 ves_icall_get_trace (MonoException *exc, gint32 skip, MonoBoolean need_file_info)
30 MonoDomain *domain = mono_domain_get ();
32 MonoArray *ta = exc->trace_ips;
36 /* Exception is not thrown yet */
37 return mono_array_new (domain, mono_defaults.stack_frame_class, 0);
40 len = mono_array_length (ta);
42 res = mono_array_new (domain, mono_defaults.stack_frame_class, len > skip ? len - skip : 0);
44 for (i = skip; i < len; i++) {
46 MonoStackFrame *sf = (MonoStackFrame *)mono_object_new (domain, mono_defaults.stack_frame_class);
47 gpointer ip = mono_array_get (ta, gpointer, i);
49 ji = mono_jit_info_table_find (domain, ip);
52 mono_array_set (res, gpointer, i, sf);
56 g_assert (ji != NULL);
58 sf->method = mono_method_get_object (domain, ji->method, NULL);
59 sf->native_offset = (char *)ip - (char *)ji->code_start;
61 sf->il_offset = mono_debug_il_offset_from_address (ji->method, sf->native_offset, domain);
66 filename = mono_debug_source_location_from_address (ji->method, sf->native_offset, &sf->line, domain);
68 sf->filename = filename? mono_string_new (domain, filename): NULL;
74 mono_array_set (res, gpointer, i, sf);
81 mono_jit_walk_stack (MonoStackWalk func, gpointer user_data) {
82 MonoDomain *domain = mono_domain_get ();
83 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
84 MonoLMF *lmf = jit_tls->lmf;
86 gint native_offset, il_offset;
89 MonoContext ctx, new_ctx;
91 MONO_CONTEXT_SET_IP (&ctx, __builtin_return_address (0));
92 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (1));
94 while (MONO_CONTEXT_GET_BP (&ctx) < jit_tls->end_of_stack) {
96 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, &native_offset, &managed);
99 if (ji == (gpointer)-1)
102 il_offset = mono_debug_il_offset_from_address (ji->method, native_offset, domain);
104 if (func (ji->method, native_offset, il_offset, managed, user_data))
112 ves_icall_get_frame_info (gint32 skip, MonoBoolean need_file_info,
113 MonoReflectionMethod **method,
114 gint32 *iloffset, gint32 *native_offset,
115 MonoString **file, gint32 *line, gint32 *column)
117 MonoDomain *domain = mono_domain_get ();
118 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
119 MonoLMF *lmf = jit_tls->lmf;
120 MonoJitInfo *ji, rji;
121 MonoContext ctx, new_ctx;
123 MONO_CONTEXT_SET_IP (&ctx, ves_icall_get_frame_info);
124 MONO_CONTEXT_SET_BP (&ctx, __builtin_frame_address (0));
129 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, NULL, &ctx, &new_ctx, NULL, &lmf, native_offset, NULL);
133 if (!ji || ji == (gpointer)-1 || MONO_CONTEXT_GET_BP (&ctx) >= jit_tls->end_of_stack)
136 /* skip all wrappers ??*/
137 if (ji->method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE ||
138 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK ||
139 ji->method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE)
146 *method = mono_method_get_object (domain, ji->method, NULL);
147 *iloffset = mono_debug_il_offset_from_address (ji->method, *native_offset, domain);
149 if (need_file_info) {
152 filename = mono_debug_source_location_from_address (ji->method, *native_offset, line, domain);
154 *file = filename? mono_string_new (domain, filename): NULL;
164 glist_to_array (GList *list)
166 MonoDomain *domain = mono_domain_get ();
173 len = g_list_length (list);
174 res = mono_array_new (domain, mono_defaults.int_class, len);
176 for (i = 0; list; list = list->next, i++)
177 mono_array_set (res, gpointer, i, list->data);
183 * mono_handle_exception:
184 * @ctx: saved processor state
185 * @obj: the exception object
186 * @test_only: only test if the exception is caught, but dont call handlers
190 mono_handle_exception (MonoContext *ctx, gpointer obj, gboolean test_only)
192 MonoDomain *domain = mono_domain_get ();
193 MonoJitInfo *ji, rji;
194 static int (*call_filter) (MonoContext *, gpointer) = NULL;
195 static void (*restore_context) (struct sigcontext *);
196 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
197 MonoLMF *lmf = jit_tls->lmf;
198 GList *trace_ips = NULL;
199 MonoException *mono_ex;
200 gboolean stack_overflow = FALSE;
201 MonoContext initial_ctx;
203 gboolean gc_disabled = FALSE;
204 MonoString *initial_stack_trace;
207 * This function might execute on an alternate signal stack, and Boehm GC
209 * Also, since the altstack is small, stack space intensive operations like
210 * JIT compilation should be avoided.
212 if (IS_ON_SIGALTSTACK (jit_tls)) {
214 * FIXME: disabling/enabling GC while already on a signal stack might
215 * not be safe either.
217 /* Have to reenable it later */
222 g_assert (ctx != NULL);
224 MonoException *ex = mono_get_exception_null_reference ();
225 ex->message = mono_string_new (domain, "Object reference not set to an instance of an object");
226 obj = (MonoObject *)ex;
230 * Allocate a new exception object instead of the preconstructed ones.
231 * We can't do this in sigsegv_signal_handler, since GC is not yet
234 if (obj == domain->stack_overflow_ex) {
235 obj = mono_get_exception_stack_overflow ();
237 else if (obj == domain->null_reference_ex) {
238 obj = mono_get_exception_null_reference ();
241 if (mono_object_isinst (obj, mono_defaults.exception_class)) {
242 mono_ex = (MonoException*)obj;
243 initial_stack_trace = mono_ex->stack_trace;
248 if (obj == domain->stack_overflow_ex)
249 stack_overflow = TRUE;
252 call_filter = mono_arch_get_call_filter ();
254 if (!restore_context)
255 restore_context = mono_arch_get_restore_context ();
257 g_assert (jit_tls->end_of_stack);
258 g_assert (jit_tls->abort_func);
261 MonoContext ctx_cp = *ctx;
262 if (mono_jit_trace_calls != NULL)
263 g_print ("EXCEPTION handling: %s\n", mono_object_class (obj)->name);
264 if (!mono_handle_exception (&ctx_cp, obj, TRUE)) {
265 if (mono_break_on_exc)
267 mono_unhandled_exception (obj);
272 memset (&rji, 0, sizeof (rji));
277 gboolean need_trace = FALSE;
280 if (test_only && (frame_count < 1000))
283 ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, ctx, &new_ctx,
284 need_trace ? &trace : NULL, &lmf, NULL, NULL);
286 g_warning ("Exception inside function without unwind info");
287 g_assert_not_reached ();
290 if (ji != (gpointer)-1) {
292 //printf ("M: %s %p %p %d.\n", mono_method_full_name (ji->method, TRUE), jit_tls->end_of_stack, ctx->ebp, count);
294 if (test_only && ji->method->wrapper_type != MONO_WRAPPER_RUNTIME_INVOKE && mono_ex) {
298 * Avoid overwriting the stack trace if the exception is
299 * rethrown. Also avoid giant stack traces during a stack
302 if (!initial_stack_trace && (frame_count < 1000)) {
303 trace_ips = g_list_append (trace_ips, MONO_CONTEXT_GET_IP (ctx));
305 if (!mono_ex->stack_trace)
306 strace = g_strdup ("");
308 strace = mono_string_to_utf8 (mono_ex->stack_trace);
310 tmp = g_strdup_printf ("%s%s\n", strace, trace);
313 mono_ex->stack_trace = mono_string_new (domain, tmp);
320 free_stack = (guint8*)(MONO_CONTEXT_GET_BP (ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
322 free_stack = 0xffffff;
325 * During stack overflow, wait till the unwinding frees some stack
326 * space before running handlers/finalizers.
328 if ((free_stack > (64 * 1024)) && ji->num_clauses) {
331 g_assert (ji->clauses);
333 for (i = 0; i < ji->num_clauses; i++) {
334 MonoJitExceptionInfo *ei = &ji->clauses [i];
336 if (ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
337 MONO_CONTEXT_GET_IP (ctx) <= ei->try_end) {
340 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE) || (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)) {
341 /* store the exception object int cfg->excvar */
342 g_assert (ji->exvar_offset);
343 *((gpointer *)((char *)MONO_CONTEXT_GET_BP (ctx) + ji->exvar_offset)) = obj;
346 if ((ei->flags == MONO_EXCEPTION_CLAUSE_NONE &&
347 mono_object_isinst (obj, mono_class_get (ji->method->klass->image, ei->data.token))) ||
348 ((ei->flags == MONO_EXCEPTION_CLAUSE_FILTER &&
349 call_filter (ctx, ei->data.filter)))) {
352 mono_ex->trace_ips = glist_to_array (trace_ips);
353 g_list_free (trace_ips);
360 if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
361 g_print ("EXCEPTION: catch found at clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
362 MONO_CONTEXT_SET_IP (ctx, ei->handler_start);
370 if (!test_only && ei->try_start <= MONO_CONTEXT_GET_IP (ctx) &&
371 MONO_CONTEXT_GET_IP (ctx) < ei->try_end &&
372 (ei->flags & MONO_EXCEPTION_CLAUSE_FINALLY)) {
373 if (mono_jit_trace_calls != NULL && mono_trace_eval (ji->method))
374 g_print ("EXCEPTION: finally clause %d of %s\n", i, mono_method_full_name (ji->method, TRUE));
375 call_filter (ctx, ei->handler_start);
387 if ((ji == (gpointer)-1) || MONO_CONTEXT_GET_BP (ctx) >= jit_tls->end_of_stack) {
394 if (IS_ON_SIGALTSTACK (jit_tls)) {
395 /* Switch back to normal stack */
397 /* Free up some stack space */
398 initial_ctx.SC_ESP += (64 * 1024);
399 initial_ctx.SC_EIP = (unsigned int)jit_tls->abort_func;
400 restore_context (&initial_ctx);
403 jit_tls->abort_func (obj);
404 g_assert_not_reached ();
407 mono_ex->trace_ips = glist_to_array (trace_ips);
408 g_list_free (trace_ips);
414 g_assert_not_reached ();