[m-dev.] for review: move exception handling into the standard library

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Aug 31 22:53:36 AEST 1999


The diff that I posted earlier had some bugs in the code for printing out
the stack dump.  Also the event number that it printed out was not very
useful.  The following relative diff is an improved version.

I plan to commit this RSN, unless I hear any objections...

--- exception.m.old	Tue Aug 31 19:20:56 1999
+++ exception.m	Tue Aug 31 22:41:45 1999
@@ -455,6 +455,7 @@
 	#include ""mercury_deep_copy.h""
 	#include ""mercury_trace_base.h""
 	#include ""mercury_stack_trace.h""
+	#include ""mercury_layout_util.h""
 
 	MR_DECLARE_TYPE_CTOR_INFO_STRUCT( \
 			mercury_data_std_util__type_ctor_info_univ_0);
@@ -941,7 +942,8 @@
 	Word exception = r1;
 	Word handler;
 	enum CodeModel catch_code_model;
-	Word *orig_curfr = MR_curfr;
+	Word *orig_curfr;
+	Unsigned exception_event_number = MR_trace_event_number;
 
 	/*
 	** let the debugger trace exception throwing
@@ -964,21 +966,51 @@
 	** are still on the nondet stack because they left choice points
 	** behind.
 	*/
+	orig_curfr = MR_curfr;
 	while (MR_redoip_slot(MR_curfr) != ENTRY(exception_handler_do_fail)) {
 		MR_curfr = MR_succfr_slot(MR_curfr);
 		if (MR_curfr < MR_CONTEXT(nondetstack_zone)->min) {
+			Word saved_regs[MAX_FAKE_REG];
 			/*
 			** There was no exception handler.
 			** 
 			** We restore the original value of MR_curfr,
 			** print out some diagnostics,
 			** and then terminate execution.
+			**
+			** We need to save & restore the registers to a
+			** separate `saved_reg' array across the call to
+			** ML_report_uncaught_exception(), since that is
+			** Mercury code which may clobber both the real
+			** machine registers and also the fake_reg array.
 			*/
 			MR_curfr = orig_curfr;
 			fflush(stdout);
+			MR_copy_regs_to_saved_regs(MR_MAX_SPECIAL_REG_MR + 1,
+				saved_regs);
 			ML_report_uncaught_exception(exception);
+			MR_copy_saved_regs_to_regs(MR_MAX_SPECIAL_REG_MR + 1,
+				saved_regs);
 			MR_trace_report(stderr);
-			MR_dump_stack(MR_succip, MR_sp, MR_curfr, FALSE);
+			if (exception_event_number > 0) {
+				fprintf(stderr, ""Last trace event before ""
+					""the unhandled exception was ""
+					""event #%ld.\\n"",
+					(long) exception_event_number);
+			}
+			if (MR_trace_enabled) {
+				/*
+				** The stack has already been unwound
+				** by MR_trace_throw(), so we can't dump it.
+				** (In fact, if we tried to dump the now-empty
+				** stack, we'd get incorrect results, since
+				** MR_trace_throw() does not restore MR_succip
+				** to the appropriate value.)
+				*/
+			} else {
+				MR_dump_stack(MR_succip, MR_sp, MR_curfr,
+					FALSE);
+			}
 			exit(1);
 		}
 	}

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list