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"
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport int backtrace_active = 0;
|
|
|
|
CAMLexport int backtrace_pos = 0;
|
|
|
|
CAMLexport code_t * backtrace_buffer = NULL;
|
2001-11-26 04:47:03 -08:00
|
|
|
CAMLexport value 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 */
|
|
|
|
|
|
|
|
void init_backtrace(void)
|
|
|
|
{
|
|
|
|
backtrace_active = 1;
|
|
|
|
register_global_root(&backtrace_last_exn);
|
|
|
|
/* Note: lazy initialization of backtrace_buffer in stash_backtrace
|
|
|
|
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 */
|
|
|
|
|
2001-11-26 04:47:03 -08:00
|
|
|
void stash_backtrace(value exn, code_t pc, value * sp)
|
2001-02-19 04:29:00 -08:00
|
|
|
{
|
2001-11-26 04:47:03 -08:00
|
|
|
code_t end_code = (code_t) ((char *) start_code + code_size);
|
2001-02-19 04:29:00 -08:00
|
|
|
if (pc != NULL) pc = pc - 1;
|
2001-11-26 04:47:03 -08:00
|
|
|
if (exn != backtrace_last_exn) {
|
|
|
|
backtrace_pos = 0;
|
|
|
|
backtrace_last_exn = exn;
|
|
|
|
}
|
2001-06-15 07:22:38 -07:00
|
|
|
if (backtrace_buffer == NULL) {
|
|
|
|
backtrace_buffer = malloc(BACKTRACE_BUFFER_SIZE * sizeof(code_t));
|
|
|
|
if (backtrace_buffer == NULL) return;
|
|
|
|
}
|
2001-11-26 04:47:03 -08:00
|
|
|
if (backtrace_pos >= BACKTRACE_BUFFER_SIZE) return;
|
2001-02-19 04:29:00 -08:00
|
|
|
backtrace_buffer[backtrace_pos++] = pc;
|
|
|
|
for (/*nothing*/; sp < trapsp; sp++) {
|
|
|
|
code_t p = (code_t) *sp;
|
|
|
|
if (p >= start_code && p < end_code) {
|
|
|
|
if (backtrace_pos >= BACKTRACE_BUFFER_SIZE) break;
|
|
|
|
backtrace_buffer[backtrace_pos++] = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2001-02-19 04:29:00 -08:00
|
|
|
fd = attempt_open(&exec_name, &trail, 1);
|
|
|
|
if (fd < 0) CAMLreturn(Val_false);
|
|
|
|
read_section_descriptors(fd, &trail);
|
|
|
|
if (seek_optional_section(fd, &trail, "DBUG") == -1) {
|
|
|
|
close(fd);
|
|
|
|
CAMLreturn(Val_false);
|
|
|
|
}
|
2001-10-09 08:14:01 -07:00
|
|
|
chan = open_descriptor_in(fd);
|
2001-02-19 04:29:00 -08:00
|
|
|
num_events = getword(chan);
|
|
|
|
events = alloc(num_events, 0);
|
|
|
|
for (i = 0; i < num_events; i++) {
|
|
|
|
orig = getword(chan);
|
|
|
|
evl = input_val(chan);
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
close_channel(chan);
|
|
|
|
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
|
|
|
|
|
|
|
Assert(pc >= start_code && pc < start_code + code_size);
|
|
|
|
pos = Val_long((char *) pc - (char *) start_code);
|
|
|
|
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
|
|
|
{
|
2001-11-26 04:47:03 -08:00
|
|
|
code_t pc = backtrace_buffer[index];
|
2001-02-19 04:29:00 -08:00
|
|
|
char * info;
|
|
|
|
value ev;
|
|
|
|
|
|
|
|
ev = event_for_location(events, pc);
|
|
|
|
if (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 */
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport void 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;
|
|
|
|
}
|
2001-02-19 04:29:00 -08:00
|
|
|
for (i = 0; i < backtrace_pos; i++)
|
2001-11-26 04:47:03 -08:00
|
|
|
print_location(events, i);
|
2001-02-19 04:29:00 -08:00
|
|
|
}
|