2001-02-19 04:29:00 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 2000 Institut National de Recherche en Informatique et */
|
|
|
|
/* 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. */
|
2001-02-19 04:29:00 -08:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/* Stack backtrace for uncaught exceptions */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2001-06-15 07:22:38 -07:00
|
|
|
#include <stdlib.h>
|
2001-02-19 04:29:00 -08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include "config.h"
|
|
|
|
#ifdef HAS_UNISTD
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "alloc.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "instruct.h"
|
|
|
|
#include "intext.h"
|
|
|
|
#include "exec.h"
|
|
|
|
#include "fix_code.h"
|
|
|
|
#include "startup.h"
|
|
|
|
#include "stacks.h"
|
|
|
|
#include "sys.h"
|
|
|
|
#include "backtrace.h"
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLexport int caml_backtrace_active = 0;
|
|
|
|
CAMLexport int caml_backtrace_pos = 0;
|
|
|
|
CAMLexport code_t * caml_backtrace_buffer = NULL;
|
|
|
|
CAMLexport value caml_backtrace_last_exn = Val_unit;
|
2001-02-19 04:29:00 -08:00
|
|
|
#define BACKTRACE_BUFFER_SIZE 1024
|
|
|
|
|
|
|
|
/* Location of fields in the Instruct.debug_event record */
|
|
|
|
enum { EV_POS = 0,
|
|
|
|
EV_MODULE = 1,
|
|
|
|
EV_CHAR = 2,
|
|
|
|
EV_KIND = 3 };
|
|
|
|
|
2002-11-04 04:39:57 -08:00
|
|
|
/* Location of fields in the Lexing.position record. */
|
|
|
|
enum {
|
|
|
|
POS_FNAME = 0,
|
|
|
|
POS_LNUM = 1,
|
|
|
|
POS_BOL = 2,
|
2002-12-16 08:42:13 -08:00
|
|
|
POS_CNUM = 3
|
2002-11-04 04:39:57 -08:00
|
|
|
};
|
|
|
|
|
2001-11-26 04:47:03 -08:00
|
|
|
/* Initialize the backtrace machinery */
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
void caml_init_backtrace(void)
|
2001-11-26 04:47:03 -08:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_backtrace_active = 1;
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_register_global_root(&caml_backtrace_last_exn);
|
2003-12-31 06:20:40 -08:00
|
|
|
/* Note: lazy initialization of caml_backtrace_buffer in caml_stash_backtrace
|
2001-11-26 04:47:03 -08:00
|
|
|
to simplify the interface with the thread libraries */
|
|
|
|
}
|
|
|
|
|
2001-02-19 04:29:00 -08:00
|
|
|
/* Store the return addresses contained in the given stack fragment
|
|
|
|
into the backtrace array */
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
void caml_stash_backtrace(value exn, code_t pc, value * sp)
|
2001-02-19 04:29:00 -08:00
|
|
|
{
|
2004-01-02 11:23:29 -08:00
|
|
|
code_t end_code = (code_t) ((char *) caml_start_code + caml_code_size);
|
2001-02-19 04:29:00 -08:00
|
|
|
if (pc != NULL) pc = pc - 1;
|
2003-12-31 06:20:40 -08:00
|
|
|
if (exn != caml_backtrace_last_exn) {
|
|
|
|
caml_backtrace_pos = 0;
|
|
|
|
caml_backtrace_last_exn = exn;
|
2001-11-26 04:47:03 -08:00
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_backtrace_buffer == NULL) {
|
|
|
|
caml_backtrace_buffer = malloc(BACKTRACE_BUFFER_SIZE * sizeof(code_t));
|
|
|
|
if (caml_backtrace_buffer == NULL) return;
|
2001-06-15 07:22:38 -07:00
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_backtrace_pos >= BACKTRACE_BUFFER_SIZE) return;
|
2004-01-02 11:23:29 -08:00
|
|
|
if (pc >= caml_start_code && pc < end_code){
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_backtrace_buffer[caml_backtrace_pos++] = pc;
|
2003-02-24 08:44:48 -08:00
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
for (/*nothing*/; sp < caml_trapsp; sp++) {
|
2001-02-19 04:29:00 -08:00
|
|
|
code_t p = (code_t) *sp;
|
2004-01-02 11:23:29 -08:00
|
|
|
if (p >= caml_start_code && p < end_code) {
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_backtrace_pos >= BACKTRACE_BUFFER_SIZE) break;
|
|
|
|
caml_backtrace_buffer[caml_backtrace_pos++] = p;
|
2001-02-19 04:29:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the debugging info contained in the current bytecode executable.
|
|
|
|
Return a Caml array of Caml lists of debug_event records in "events",
|
|
|
|
or Val_false on failure. */
|
|
|
|
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static value read_debug_info(void)
|
|
|
|
{
|
|
|
|
CAMLparam0();
|
|
|
|
CAMLlocal1(events);
|
|
|
|
char * exec_name;
|
|
|
|
int fd;
|
|
|
|
struct exec_trailer trail;
|
|
|
|
struct channel * chan;
|
|
|
|
uint32 num_events, orig, i;
|
|
|
|
value evl, l;
|
|
|
|
|
2002-02-11 05:51:40 -08:00
|
|
|
exec_name = caml_exe_name;
|
2003-12-31 06:20:40 -08:00
|
|
|
fd = caml_attempt_open(&exec_name, &trail, 1);
|
2001-02-19 04:29:00 -08:00
|
|
|
if (fd < 0) CAMLreturn(Val_false);
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_read_section_descriptors(fd, &trail);
|
|
|
|
if (caml_seek_optional_section(fd, &trail, "DBUG") == -1) {
|
2001-02-19 04:29:00 -08:00
|
|
|
close(fd);
|
|
|
|
CAMLreturn(Val_false);
|
|
|
|
}
|
2003-12-29 14:15:02 -08:00
|
|
|
chan = caml_open_descriptor_in(fd);
|
|
|
|
num_events = caml_getword(chan);
|
|
|
|
events = caml_alloc(num_events, 0);
|
2001-02-19 04:29:00 -08:00
|
|
|
for (i = 0; i < num_events; i++) {
|
2003-12-29 14:15:02 -08:00
|
|
|
orig = caml_getword(chan);
|
2004-01-01 08:42:43 -08:00
|
|
|
evl = caml_input_val(chan);
|
2001-02-19 04:29:00 -08:00
|
|
|
/* Relocate events in event list */
|
|
|
|
for (l = evl; l != Val_int(0); l = Field(l, 1)) {
|
|
|
|
value ev = Field(l, 0);
|
|
|
|
Field(ev, EV_POS) = Val_long(Long_val(Field(ev, EV_POS)) + orig);
|
|
|
|
}
|
|
|
|
/* Record event list */
|
|
|
|
Store_field(events, i, evl);
|
|
|
|
}
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_close_channel(chan);
|
2001-02-19 04:29:00 -08:00
|
|
|
CAMLreturn(events);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search the event for the given PC. Return Val_false if not found. */
|
|
|
|
|
|
|
|
static value event_for_location(value events, code_t pc)
|
|
|
|
{
|
|
|
|
mlsize_t i;
|
2002-11-26 02:47:21 -08:00
|
|
|
value pos, l, ev, ev_pos;
|
2001-02-19 04:29:00 -08:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
Assert(pc >= caml_start_code && pc < caml_start_code + caml_code_size);
|
|
|
|
pos = Val_long((char *) pc - (char *) caml_start_code);
|
2001-02-19 04:29:00 -08:00
|
|
|
for (i = 0; i < Wosize_val(events); i++) {
|
|
|
|
for (l = Field(events, i); l != Val_int(0); l = Field(l, 1)) {
|
|
|
|
ev = Field(l, 0);
|
2002-11-26 02:47:21 -08:00
|
|
|
ev_pos = Field(ev, EV_POS);
|
|
|
|
/* ocamlc sometimes moves an event past a following PUSH instruction;
|
|
|
|
allow mismatch by 1 instruction. */
|
|
|
|
if (ev_pos == pos || ev_pos == pos + 8) return ev;
|
2001-02-19 04:29:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Val_false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the location corresponding to the given PC */
|
|
|
|
|
2001-11-26 04:47:03 -08:00
|
|
|
static void print_location(value events, int index)
|
2001-02-19 04:29:00 -08:00
|
|
|
{
|
2003-12-31 06:20:40 -08:00
|
|
|
code_t pc = caml_backtrace_buffer[index];
|
2001-02-19 04:29:00 -08:00
|
|
|
char * info;
|
|
|
|
value ev;
|
|
|
|
|
|
|
|
ev = event_for_location(events, pc);
|
2004-01-02 11:23:29 -08:00
|
|
|
if (caml_is_instruction(*pc, RAISE)) {
|
2001-11-26 04:47:03 -08:00
|
|
|
/* Ignore compiler-inserted raise */
|
2001-02-19 04:29:00 -08:00
|
|
|
if (ev == Val_false) return;
|
2001-11-26 04:47:03 -08:00
|
|
|
/* Initial raise if index == 0, re-raise otherwise */
|
|
|
|
if (index == 0)
|
|
|
|
info = "Raised at";
|
|
|
|
else
|
|
|
|
info = "Re-raised at";
|
2001-02-19 04:29:00 -08:00
|
|
|
} else {
|
2002-11-26 02:47:21 -08:00
|
|
|
if (index == 0)
|
|
|
|
info = "Raised by primitive operation at";
|
|
|
|
else
|
|
|
|
info = "Called from";
|
2001-02-19 04:29:00 -08:00
|
|
|
}
|
|
|
|
if (ev == Val_false) {
|
|
|
|
fprintf(stderr, "%s unknown location\n", info);
|
|
|
|
} else {
|
2002-11-04 04:39:57 -08:00
|
|
|
value ev_char = Field (ev, EV_CHAR);
|
|
|
|
char *fname = String_val (Field (ev_char, POS_FNAME));
|
|
|
|
int lnum = Int_val (Field (ev_char, POS_LNUM));
|
|
|
|
int chr = Int_val (Field (ev_char, POS_CNUM))
|
|
|
|
- Int_val (Field (ev_char, POS_BOL));
|
2002-11-04 05:57:21 -08:00
|
|
|
fprintf (stderr, "%s file \"%s\", line %d, character %d\n", info, fname,
|
|
|
|
lnum, chr);
|
2001-02-19 04:29:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print a backtrace */
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLexport void caml_print_exception_backtrace(void)
|
2001-02-19 04:29:00 -08:00
|
|
|
{
|
|
|
|
value events;
|
2001-09-24 05:45:55 -07:00
|
|
|
int i;
|
2001-02-19 04:29:00 -08:00
|
|
|
|
|
|
|
events = read_debug_info();
|
2001-10-09 07:02:23 -07:00
|
|
|
if (events == Val_false) {
|
|
|
|
fprintf(stderr,
|
2002-07-22 01:22:27 -07:00
|
|
|
"(Program not linked with -g, cannot print stack backtrace)\n");
|
2001-10-09 07:02:23 -07:00
|
|
|
return;
|
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
for (i = 0; i < caml_backtrace_pos; i++)
|
2001-11-26 04:47:03 -08:00
|
|
|
print_location(events, i);
|
2001-02-19 04:29:00 -08:00
|
|
|
}
|