1996-11-07 05:12:33 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1996-11-07 05:12:33 -08:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/* Print an uncaught exception and abort */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1997-05-15 06:26:08 -07:00
|
|
|
#include <string.h>
|
2001-02-19 04:29:00 -08:00
|
|
|
#include "backtrace.h"
|
2001-10-30 01:22:49 -08:00
|
|
|
#include "callback.h"
|
2001-02-19 04:29:00 -08:00
|
|
|
#include "debugger.h"
|
1996-11-07 05:12:33 -08:00
|
|
|
#include "fail.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
2001-06-15 07:22:38 -07:00
|
|
|
#include "printexc.h"
|
1996-11-07 05:12:33 -08:00
|
|
|
|
1997-05-15 06:26:08 -07:00
|
|
|
struct stringbuf {
|
|
|
|
char * ptr;
|
|
|
|
char * end;
|
|
|
|
char data[256];
|
|
|
|
};
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void add_char(struct stringbuf *buf, char c)
|
1997-05-15 06:26:08 -07:00
|
|
|
{
|
|
|
|
if (buf->ptr < buf->end) *(buf->ptr++) = c;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void add_string(struct stringbuf *buf, char *s)
|
1997-05-15 06:26:08 -07:00
|
|
|
{
|
|
|
|
int len = strlen(s);
|
|
|
|
if (buf->ptr + len > buf->end) len = buf->end - buf->ptr;
|
2000-10-12 11:05:42 -07:00
|
|
|
if (len > 0) memmove(buf->ptr, s, len);
|
1997-05-15 06:26:08 -07:00
|
|
|
buf->ptr += len;
|
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport char * caml_format_exception(value exn)
|
1996-11-07 05:12:33 -08:00
|
|
|
{
|
1997-05-15 06:26:08 -07:00
|
|
|
mlsize_t start, i;
|
|
|
|
value bucket, v;
|
|
|
|
struct stringbuf buf;
|
|
|
|
char intbuf[64];
|
1999-02-18 08:26:16 -08:00
|
|
|
char * res;
|
1996-11-07 05:12:33 -08:00
|
|
|
|
1997-05-15 06:26:08 -07:00
|
|
|
buf.ptr = buf.data;
|
|
|
|
buf.end = buf.data + sizeof(buf.data) - 1;
|
|
|
|
add_string(&buf, String_val(Field(Field(exn, 0), 0)));
|
1996-11-07 05:12:33 -08:00
|
|
|
if (Wosize_val(exn) >= 2) {
|
1997-05-15 06:26:08 -07:00
|
|
|
/* Check for exceptions in the style of Match_failure and Assert_failure */
|
|
|
|
if (Wosize_val(exn) == 2 &&
|
|
|
|
Is_block(Field(exn, 1)) &&
|
|
|
|
Tag_val(Field(exn, 1)) == 0) {
|
|
|
|
bucket = Field(exn, 1);
|
|
|
|
start = 0;
|
|
|
|
} else {
|
|
|
|
bucket = exn;
|
|
|
|
start = 1;
|
1996-11-07 05:12:33 -08:00
|
|
|
}
|
1997-05-15 06:26:08 -07:00
|
|
|
add_char(&buf, '(');
|
|
|
|
for (i = start; i < Wosize_val(bucket); i++) {
|
|
|
|
if (i > start) add_string(&buf, ", ");
|
|
|
|
v = Field(bucket, i);
|
|
|
|
if (Is_long(v)) {
|
|
|
|
sprintf(intbuf, "%ld", Long_val(v));
|
|
|
|
add_string(&buf, intbuf);
|
|
|
|
} else if (Tag_val(v) == String_tag) {
|
|
|
|
add_char(&buf, '"');
|
|
|
|
add_string(&buf, String_val(v));
|
|
|
|
add_char(&buf, '"');
|
|
|
|
} else {
|
|
|
|
add_char(&buf, '_');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_char(&buf, ')');
|
1996-11-07 05:12:33 -08:00
|
|
|
}
|
1997-05-15 06:26:08 -07:00
|
|
|
*buf.ptr = 0; /* Terminate string */
|
1999-02-18 08:26:16 -08:00
|
|
|
i = buf.ptr - buf.data + 1;
|
|
|
|
res = malloc(i);
|
|
|
|
if (res == NULL) return NULL;
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(res, buf.data, i);
|
1999-02-18 08:26:16 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_fatal_uncaught_exception(value exn)
|
1999-02-18 08:26:16 -08:00
|
|
|
{
|
2001-10-30 01:22:49 -08:00
|
|
|
char * msg;
|
|
|
|
value * at_exit;
|
|
|
|
#ifndef NATIVE_CODE
|
|
|
|
int saved_backtrace_active, saved_backtrace_pos;
|
|
|
|
#endif
|
|
|
|
/* Build a string representation of the exception */
|
2004-01-01 08:42:43 -08:00
|
|
|
msg = caml_format_exception(exn);
|
2001-10-30 01:22:49 -08:00
|
|
|
/* Perform "at_exit" processing, ignoring all exceptions that may
|
|
|
|
be triggered by this */
|
|
|
|
#ifndef NATIVE_CODE
|
2003-12-31 06:20:40 -08:00
|
|
|
saved_backtrace_active = caml_backtrace_active;
|
|
|
|
saved_backtrace_pos = caml_backtrace_pos;
|
|
|
|
caml_backtrace_active = 0;
|
2001-10-30 01:22:49 -08:00
|
|
|
#endif
|
|
|
|
at_exit = caml_named_value("Pervasives.do_at_exit");
|
2003-12-31 06:20:40 -08:00
|
|
|
if (at_exit != NULL) caml_callback_exn(*at_exit, Val_unit);
|
2001-10-30 01:22:49 -08:00
|
|
|
#ifndef NATIVE_CODE
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_backtrace_active = saved_backtrace_active;
|
|
|
|
caml_backtrace_pos = saved_backtrace_pos;
|
2001-10-30 01:22:49 -08:00
|
|
|
#endif
|
|
|
|
/* Display the uncaught exception */
|
2001-12-03 02:14:09 -08:00
|
|
|
fprintf(stderr, "Fatal error: exception %s\n", msg);
|
1999-02-18 08:26:16 -08:00
|
|
|
free(msg);
|
2001-10-30 01:22:49 -08:00
|
|
|
/* Display the backtrace if available */
|
2001-02-19 05:14:26 -08:00
|
|
|
#ifndef NATIVE_CODE
|
2004-01-01 08:42:43 -08:00
|
|
|
if (caml_backtrace_active && !caml_debugger_in_use){
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_print_exception_backtrace();
|
|
|
|
}
|
2001-02-19 05:14:26 -08:00
|
|
|
#endif
|
2001-10-30 01:22:49 -08:00
|
|
|
/* Terminate the process */
|
1996-11-07 05:12:33 -08:00
|
|
|
exit(2);
|
|
|
|
}
|