1996-12-03 05:40:28 -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-12-03 05:40:28 -08:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/* Interface with the debugger */
|
|
|
|
|
1996-12-11 08:29:28 -08:00
|
|
|
#include <string.h>
|
|
|
|
|
1996-12-03 05:40:28 -08:00
|
|
|
#include "config.h"
|
|
|
|
#include "debugger.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "fix_code.h"
|
|
|
|
#include "instruct.h"
|
|
|
|
#include "intext.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "stacks.h"
|
|
|
|
#include "sys.h"
|
|
|
|
|
|
|
|
int debugger_in_use = 0;
|
|
|
|
unsigned long event_count;
|
|
|
|
|
1996-12-10 06:45:58 -08:00
|
|
|
#if !defined(HAS_SOCKETS) || defined(_WIN32)
|
1996-12-03 05:40:28 -08:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void debugger_init(void)
|
1996-12-03 05:40:28 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void debugger(enum event_kind event)
|
1996-12-03 05:40:28 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#ifdef HAS_UNISTD
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
static int sock_domain; /* Socket domain for the debugger */
|
|
|
|
static union { /* Socket address for the debugger */
|
|
|
|
struct sockaddr s_gen;
|
|
|
|
struct sockaddr_un s_unix;
|
|
|
|
struct sockaddr_in s_inet;
|
|
|
|
} sock_addr;
|
|
|
|
static int sock_addr_len; /* Length of sock_addr */
|
|
|
|
|
|
|
|
static int dbg_socket = -1; /* The socket connected to the debugger */
|
|
|
|
static struct channel * dbg_in; /* Input channel on the socket */
|
|
|
|
static struct channel * dbg_out;/* Output channel on the socket */
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void open_connection(void)
|
1996-12-03 05:40:28 -08:00
|
|
|
{
|
|
|
|
dbg_socket = socket(sock_domain, SOCK_STREAM, 0);
|
|
|
|
if (dbg_socket == -1 ||
|
|
|
|
connect(dbg_socket, &sock_addr.s_gen, sock_addr_len) == -1)
|
|
|
|
fatal_error("cannot connect to debugger");
|
2001-10-09 08:14:01 -07:00
|
|
|
dbg_in = open_descriptor_in(dbg_socket);
|
|
|
|
dbg_out = open_descriptor_out(dbg_socket);
|
1997-03-22 12:06:05 -08:00
|
|
|
if (!debugger_in_use) putword(dbg_out, -1); /* first connection */
|
1996-12-03 05:40:28 -08:00
|
|
|
putword(dbg_out, getpid());
|
|
|
|
flush(dbg_out);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void close_connection(void)
|
1996-12-03 05:40:28 -08:00
|
|
|
{
|
|
|
|
close_channel(dbg_in);
|
|
|
|
close_channel(dbg_out);
|
|
|
|
dbg_socket = -1; /* was closed by close_channel */
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void debugger_init(void)
|
1996-12-03 05:40:28 -08:00
|
|
|
{
|
|
|
|
char * address;
|
|
|
|
char * port, * p;
|
|
|
|
struct hostent * host;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
address = getenv("CAML_DEBUG_SOCKET");
|
|
|
|
if (address == NULL) return;
|
|
|
|
|
|
|
|
/* Parse the address */
|
|
|
|
port = NULL;
|
|
|
|
for (p = address; *p != 0; p++) {
|
|
|
|
if (*p == ':') { *p = 0; port = p+1; break; }
|
|
|
|
}
|
|
|
|
if (port == NULL) {
|
|
|
|
/* Unix domain */
|
|
|
|
sock_domain = PF_UNIX;
|
|
|
|
sock_addr.s_unix.sun_family = AF_UNIX;
|
|
|
|
strncpy(sock_addr.s_unix.sun_path, address,
|
|
|
|
sizeof(sock_addr.s_unix.sun_path));
|
|
|
|
sock_addr_len =
|
|
|
|
((char *)&(sock_addr.s_unix.sun_path) - (char *)&(sock_addr.s_unix))
|
|
|
|
+ strlen(address);
|
|
|
|
} else {
|
|
|
|
/* Internet domain */
|
|
|
|
sock_domain = PF_INET;
|
|
|
|
for (p = (char *) &sock_addr.s_inet, n = sizeof(sock_addr.s_inet);
|
|
|
|
n > 0; n--) *p++ = 0;
|
|
|
|
sock_addr.s_inet.sin_family = AF_INET;
|
|
|
|
sock_addr.s_inet.sin_addr.s_addr = inet_addr(address);
|
|
|
|
if (sock_addr.s_inet.sin_addr.s_addr == -1) {
|
|
|
|
host = gethostbyname(address);
|
|
|
|
if (host == NULL)
|
|
|
|
fatal_error_arg("Unknown debugging host %s\n", address);
|
2000-10-12 11:05:42 -07:00
|
|
|
memmove(&sock_addr.s_inet.sin_addr, host->h_addr, host->h_length);
|
1996-12-03 05:40:28 -08:00
|
|
|
}
|
|
|
|
sock_addr.s_inet.sin_port = htons(atoi(port));
|
|
|
|
sock_addr_len = sizeof(sock_addr.s_inet);
|
|
|
|
}
|
|
|
|
open_connection();
|
|
|
|
debugger_in_use = 1;
|
|
|
|
trap_barrier = stack_high;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static value getval(struct channel *chan)
|
1996-12-03 05:40:28 -08:00
|
|
|
{
|
|
|
|
value res;
|
|
|
|
if (really_getblock(chan, (char *) &res, sizeof(res)) == 0)
|
|
|
|
raise_end_of_file(); /* Bad, but consistent with getword */
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void putval(struct channel *chan, value val)
|
1996-12-03 05:40:28 -08:00
|
|
|
{
|
|
|
|
really_putblock(chan, (char *) &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void safe_output_value(struct channel *chan, value val)
|
1997-06-13 08:49:36 -07:00
|
|
|
{
|
|
|
|
struct longjmp_buffer raise_buf, * saved_external_raise;
|
|
|
|
|
1997-08-29 08:37:22 -07:00
|
|
|
/* Catch exceptions raised by output_val */
|
1997-06-13 08:49:36 -07:00
|
|
|
saved_external_raise = external_raise;
|
1999-06-05 05:02:48 -07:00
|
|
|
if (sigsetjmp(raise_buf.buf, 0) == 0) {
|
1997-06-13 08:49:36 -07:00
|
|
|
external_raise = &raise_buf;
|
1997-08-29 08:37:22 -07:00
|
|
|
output_val(chan, val, Val_unit);
|
1997-06-13 08:49:36 -07:00
|
|
|
} else {
|
|
|
|
/* Send wrong magic number, will cause input_value to fail */
|
|
|
|
really_putblock(chan, "\000\000\000\000", 4);
|
|
|
|
}
|
|
|
|
external_raise = saved_external_raise;
|
|
|
|
}
|
|
|
|
|
2001-08-13 06:53:51 -07:00
|
|
|
#define Pc(sp) ((code_t)((sp)[0]))
|
|
|
|
#define Env(sp) ((sp)[1])
|
|
|
|
#define Extra_args(sp) (Long_val(((sp)[2])))
|
|
|
|
#define Locals(sp) ((sp) + 3)
|
1996-12-03 05:40:28 -08:00
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void debugger(enum event_kind event)
|
1996-12-03 05:40:28 -08:00
|
|
|
{
|
|
|
|
int frame_number;
|
|
|
|
value * frame;
|
|
|
|
long i, pos;
|
|
|
|
value val;
|
|
|
|
|
|
|
|
if (dbg_socket == -1) return; /* Not connected to a debugger. */
|
|
|
|
|
|
|
|
/* Reset current frame */
|
|
|
|
frame_number = 0;
|
|
|
|
frame = extern_sp + 1;
|
|
|
|
|
|
|
|
/* Report the event to the debugger */
|
|
|
|
switch(event) {
|
|
|
|
case PROGRAM_START: /* Nothing to report */
|
|
|
|
goto command_loop;
|
|
|
|
case EVENT_COUNT:
|
|
|
|
putch(dbg_out, REP_EVENT);
|
|
|
|
break;
|
|
|
|
case BREAKPOINT:
|
|
|
|
putch(dbg_out, REP_BREAKPOINT);
|
|
|
|
break;
|
|
|
|
case PROGRAM_EXIT:
|
|
|
|
putch(dbg_out, REP_EXITED);
|
|
|
|
break;
|
|
|
|
case TRAP_BARRIER:
|
|
|
|
putch(dbg_out, REP_TRAP);
|
|
|
|
break;
|
|
|
|
case UNCAUGHT_EXC:
|
|
|
|
putch(dbg_out, REP_UNCAUGHT_EXC);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
putword(dbg_out, event_count);
|
1997-06-13 08:49:36 -07:00
|
|
|
if (event == EVENT_COUNT || event == BREAKPOINT) {
|
|
|
|
putword(dbg_out, stack_high - frame);
|
|
|
|
putword(dbg_out, (Pc(frame) - start_code) * sizeof(opcode_t));
|
|
|
|
} else {
|
|
|
|
/* No PC and no stack frame associated with other events */
|
|
|
|
putword(dbg_out, 0);
|
|
|
|
putword(dbg_out, 0);
|
|
|
|
}
|
1996-12-03 05:40:28 -08:00
|
|
|
flush(dbg_out);
|
|
|
|
|
|
|
|
command_loop:
|
|
|
|
|
|
|
|
/* Read and execute the commands sent by the debugger */
|
|
|
|
while(1) {
|
|
|
|
switch(getch(dbg_in)) {
|
|
|
|
case REQ_SET_EVENT:
|
|
|
|
pos = getword(dbg_in);
|
2000-08-23 10:10:03 -07:00
|
|
|
Assert (pos >= 0);
|
|
|
|
Assert (pos < code_size);
|
1996-12-03 05:40:28 -08:00
|
|
|
set_instruction(start_code + pos / sizeof(opcode_t), EVENT);
|
|
|
|
break;
|
|
|
|
case REQ_SET_BREAKPOINT:
|
|
|
|
pos = getword(dbg_in);
|
2000-08-23 10:10:03 -07:00
|
|
|
Assert (pos >= 0);
|
|
|
|
Assert (pos < code_size);
|
1996-12-03 05:40:28 -08:00
|
|
|
set_instruction(start_code + pos / sizeof(opcode_t), BREAK);
|
|
|
|
break;
|
|
|
|
case REQ_RESET_INSTR:
|
|
|
|
pos = getword(dbg_in);
|
2000-08-23 10:10:03 -07:00
|
|
|
Assert (pos >= 0);
|
|
|
|
Assert (pos < code_size);
|
1996-12-03 05:40:28 -08:00
|
|
|
pos = pos / sizeof(opcode_t);
|
|
|
|
set_instruction(start_code + pos, saved_code[pos]);
|
|
|
|
break;
|
|
|
|
case REQ_CHECKPOINT:
|
|
|
|
i = fork();
|
|
|
|
if (i == 0) {
|
1997-05-19 08:42:21 -07:00
|
|
|
close_connection(); /* Close parent connection. */
|
|
|
|
open_connection(); /* Open new connection with debugger */
|
1996-12-03 05:40:28 -08:00
|
|
|
} else {
|
1997-05-19 08:42:21 -07:00
|
|
|
putword(dbg_out, i);
|
|
|
|
flush(dbg_out);
|
1996-12-03 05:40:28 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REQ_GO:
|
|
|
|
event_count = getword(dbg_in);
|
|
|
|
return;
|
|
|
|
case REQ_STOP:
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case REQ_WAIT:
|
|
|
|
wait(NULL);
|
|
|
|
break;
|
|
|
|
case REQ_INITIAL_FRAME:
|
|
|
|
frame = extern_sp + 1;
|
|
|
|
/* Fall through */
|
|
|
|
case REQ_GET_FRAME:
|
|
|
|
putword(dbg_out, stack_high - frame);
|
2002-10-22 05:30:03 -07:00
|
|
|
if (frame < stack_high){
|
|
|
|
putword(dbg_out, (Pc(frame) - start_code) * sizeof(opcode_t));
|
|
|
|
}else{
|
|
|
|
putword (dbg_out, 0);
|
|
|
|
}
|
1996-12-03 05:40:28 -08:00
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_SET_FRAME:
|
|
|
|
i = getword(dbg_in);
|
|
|
|
frame = stack_high - i;
|
|
|
|
break;
|
|
|
|
case REQ_UP_FRAME:
|
|
|
|
i = getword(dbg_in);
|
1997-06-18 13:18:41 -07:00
|
|
|
if (frame + Extra_args(frame) + i + 3 >= stack_high) {
|
1996-12-03 05:40:28 -08:00
|
|
|
putword(dbg_out, -1);
|
|
|
|
} else {
|
1997-06-18 13:18:41 -07:00
|
|
|
frame += Extra_args(frame) + i + 3;
|
1996-12-03 05:40:28 -08:00
|
|
|
putword(dbg_out, stack_high - frame);
|
|
|
|
putword(dbg_out, (Pc(frame) - start_code) * sizeof(opcode_t));
|
|
|
|
}
|
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_SET_TRAP_BARRIER:
|
|
|
|
i = getword(dbg_in);
|
|
|
|
trap_barrier = stack_high - i;
|
|
|
|
break;
|
|
|
|
case REQ_GET_LOCAL:
|
1997-02-19 08:08:57 -08:00
|
|
|
i = getword(dbg_in);
|
1996-12-03 05:40:28 -08:00
|
|
|
putval(dbg_out, Locals(frame)[i]);
|
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_GET_ENVIRONMENT:
|
1997-02-19 08:08:57 -08:00
|
|
|
i = getword(dbg_in);
|
1996-12-03 05:40:28 -08:00
|
|
|
putval(dbg_out, Field(Env(frame), i));
|
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_GET_GLOBAL:
|
|
|
|
i = getword(dbg_in);
|
|
|
|
putval(dbg_out, Field(global_data, i));
|
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_GET_ACCU:
|
|
|
|
putval(dbg_out, *extern_sp);
|
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_GET_HEADER:
|
|
|
|
val = getval(dbg_in);
|
|
|
|
putword(dbg_out, Hd_val(val));
|
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_GET_FIELD:
|
|
|
|
val = getval(dbg_in);
|
|
|
|
i = getword(dbg_in);
|
1999-05-15 08:07:44 -07:00
|
|
|
if (Tag_val(val) != Double_array_tag) {
|
|
|
|
putch(dbg_out, 0);
|
|
|
|
putval(dbg_out, Field(val, i));
|
|
|
|
} else {
|
1999-05-15 10:01:27 -07:00
|
|
|
double d = Double_field(val, i);
|
1999-05-15 08:07:44 -07:00
|
|
|
putch(dbg_out, 1);
|
1999-05-15 10:01:27 -07:00
|
|
|
really_putblock(dbg_out, (char *) &d, 8);
|
1999-05-15 08:07:44 -07:00
|
|
|
}
|
1996-12-03 05:40:28 -08:00
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_MARSHAL_OBJ:
|
|
|
|
val = getval(dbg_in);
|
1997-06-13 08:49:36 -07:00
|
|
|
safe_output_value(dbg_out, val);
|
1996-12-03 05:40:28 -08:00
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
case REQ_GET_CLOSURE_CODE:
|
|
|
|
val = getval(dbg_in);
|
|
|
|
putword(dbg_out, (Code_val(val) - start_code) * sizeof(opcode_t));
|
|
|
|
flush(dbg_out);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|