1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* 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. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Trace the instructions executed */
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2004-04-19 01:56:51 -07:00
|
|
|
#include <string.h>
|
2004-04-23 16:16:15 -07:00
|
|
|
#include <ctype.h>
|
2004-04-19 01:56:51 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "instruct.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
#include "opnames.h"
|
2003-05-26 05:41:54 -07:00
|
|
|
#include "prims.h"
|
2004-04-22 02:51:46 -07:00
|
|
|
#include "stacks.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
extern code_t caml_start_code;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat caml_icount = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
void caml_stop_here () {}
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
int caml_trace_flag = 0;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
void caml_disasm_instr(pc)
|
1995-05-04 03:15:53 -07:00
|
|
|
code_t pc;
|
|
|
|
{
|
|
|
|
int instr = *pc;
|
2004-01-02 11:23:29 -08:00
|
|
|
printf("%6ld %s", (long) (pc - caml_start_code),
|
1995-05-04 03:15:53 -07:00
|
|
|
instr < 0 || instr > STOP ? "???" : names_of_instructions[instr]);
|
|
|
|
pc++;
|
|
|
|
switch(instr) {
|
1998-10-07 12:01:42 -07:00
|
|
|
/* Instructions with one integer operand */
|
|
|
|
case PUSHACC: case ACC: case POP: case ASSIGN:
|
|
|
|
case PUSHENVACC: case ENVACC: case PUSH_RETADDR: case APPLY:
|
|
|
|
case APPTERM1: case APPTERM2: case APPTERM3: case RETURN:
|
|
|
|
case GRAB: case PUSHGETGLOBAL: case GETGLOBAL: case SETGLOBAL:
|
|
|
|
case PUSHATOM: case ATOM: case MAKEBLOCK1: case MAKEBLOCK2:
|
|
|
|
case MAKEBLOCK3: case MAKEFLOATBLOCK:
|
|
|
|
case GETFIELD: case SETFIELD: case GETFLOATFIELD: case SETFLOATFIELD:
|
|
|
|
case BRANCH: case BRANCHIF: case BRANCHIFNOT: case PUSHTRAP:
|
|
|
|
case CONSTINT: case PUSHCONSTINT: case OFFSETINT: case OFFSETREF:
|
|
|
|
case OFFSETCLOSURE: case PUSHOFFSETCLOSURE:
|
|
|
|
printf(" %d\n", pc[0]); break;
|
|
|
|
/* Instructions with two operands */
|
|
|
|
case APPTERM: case CLOSURE: case CLOSUREREC: case PUSHGETGLOBALFIELD:
|
|
|
|
case GETGLOBALFIELD: case MAKEBLOCK:
|
2010-01-22 04:48:24 -08:00
|
|
|
case BEQ: case BNEQ: case BLTINT: case BLEINT: case BGTINT: case BGEINT:
|
2000-10-02 07:18:05 -07:00
|
|
|
case BULTINT: case BUGEINT:
|
1998-10-07 12:01:42 -07:00
|
|
|
printf(" %d, %d\n", pc[0], pc[1]); break;
|
|
|
|
/* Instructions with a C primitive as operand */
|
|
|
|
case C_CALLN:
|
2003-05-26 05:41:54 -07:00
|
|
|
printf(" %d,", pc[0]); pc++;
|
|
|
|
/* fallthrough */
|
|
|
|
case C_CALL1: case C_CALL2: case C_CALL3: case C_CALL4: case C_CALL5:
|
2004-01-01 08:42:43 -08:00
|
|
|
if (pc[0] < 0 || pc[0] >= caml_prim_name_table.size)
|
2003-05-26 05:41:54 -07:00
|
|
|
printf(" unknown primitive %d\n", pc[0]);
|
|
|
|
else
|
2004-01-01 08:42:43 -08:00
|
|
|
printf(" %s\n", (char *) caml_prim_name_table.contents[pc[0]]);
|
2003-05-26 05:41:54 -07:00
|
|
|
break;
|
1998-10-07 12:01:42 -07:00
|
|
|
default:
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
fflush (stdout);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2005-10-18 07:04:13 -07:00
|
|
|
char * caml_instr_string (code_t pc)
|
2004-04-19 01:56:51 -07:00
|
|
|
{
|
2005-10-18 07:04:13 -07:00
|
|
|
static char buf[256];
|
|
|
|
char nambuf[128];
|
2004-04-19 01:56:51 -07:00
|
|
|
int instr = *pc;
|
2005-10-18 07:04:13 -07:00
|
|
|
char *nam;
|
|
|
|
|
2004-04-19 01:56:51 -07:00
|
|
|
nam = (instr < 0 || instr > STOP)
|
2005-10-18 07:04:13 -07:00
|
|
|
? (sprintf (nambuf, "???%d", instr), nambuf)
|
2004-04-19 01:56:51 -07:00
|
|
|
: names_of_instructions[instr];
|
|
|
|
pc++;
|
|
|
|
switch (instr) {
|
|
|
|
/* Instructions with one integer operand */
|
|
|
|
case PUSHACC:
|
|
|
|
case ACC:
|
|
|
|
case POP:
|
|
|
|
case ASSIGN:
|
|
|
|
case PUSHENVACC:
|
|
|
|
case ENVACC:
|
|
|
|
case PUSH_RETADDR:
|
|
|
|
case APPLY:
|
|
|
|
case APPTERM1:
|
|
|
|
case APPTERM2:
|
|
|
|
case APPTERM3:
|
|
|
|
case RETURN:
|
|
|
|
case GRAB:
|
|
|
|
case PUSHGETGLOBAL:
|
|
|
|
case GETGLOBAL:
|
|
|
|
case SETGLOBAL:
|
|
|
|
case PUSHATOM:
|
|
|
|
case ATOM:
|
|
|
|
case MAKEBLOCK1:
|
|
|
|
case MAKEBLOCK2:
|
|
|
|
case MAKEBLOCK3:
|
|
|
|
case MAKEFLOATBLOCK:
|
|
|
|
case GETFIELD:
|
|
|
|
case SETFIELD:
|
|
|
|
case GETFLOATFIELD:
|
|
|
|
case SETFLOATFIELD:
|
|
|
|
case BRANCH:
|
|
|
|
case BRANCHIF:
|
|
|
|
case BRANCHIFNOT:
|
|
|
|
case PUSHTRAP:
|
|
|
|
case CONSTINT:
|
|
|
|
case PUSHCONSTINT:
|
|
|
|
case OFFSETINT:
|
|
|
|
case OFFSETREF:
|
|
|
|
case OFFSETCLOSURE:
|
|
|
|
case PUSHOFFSETCLOSURE:
|
2005-10-18 07:04:13 -07:00
|
|
|
sprintf(buf, "%s %d", nam, pc[0]);
|
2004-04-19 01:56:51 -07:00
|
|
|
break;
|
|
|
|
/* Instructions with two operands */
|
|
|
|
case APPTERM:
|
|
|
|
case CLOSURE:
|
|
|
|
case CLOSUREREC:
|
|
|
|
case PUSHGETGLOBALFIELD:
|
|
|
|
case GETGLOBALFIELD:
|
|
|
|
case MAKEBLOCK:
|
|
|
|
case BEQ:
|
|
|
|
case BNEQ:
|
|
|
|
case BLTINT:
|
|
|
|
case BLEINT:
|
|
|
|
case BGTINT:
|
|
|
|
case BGEINT:
|
|
|
|
case BULTINT:
|
|
|
|
case BUGEINT:
|
2005-10-18 07:04:13 -07:00
|
|
|
sprintf(buf, "%s %d, %d", nam, pc[0], pc[1]);
|
2004-04-19 01:56:51 -07:00
|
|
|
break;
|
|
|
|
case SWITCH:
|
2005-10-18 07:04:13 -07:00
|
|
|
sprintf(buf, "SWITCH sz%#lx=%ld::ntag%ld nint%ld",
|
|
|
|
(long) pc[0], (long) pc[0], (unsigned long) pc[0] >> 16,
|
|
|
|
(unsigned long) pc[0] & 0xffff);
|
2004-04-19 01:56:51 -07:00
|
|
|
break;
|
|
|
|
/* Instructions with a C primitive as operand */
|
|
|
|
case C_CALLN:
|
2005-10-18 07:04:13 -07:00
|
|
|
sprintf(buf, "%s %d,", nam, pc[0]);
|
2004-04-19 01:56:51 -07:00
|
|
|
pc++;
|
|
|
|
/* fallthrough */
|
|
|
|
case C_CALL1:
|
|
|
|
case C_CALL2:
|
|
|
|
case C_CALL3:
|
|
|
|
case C_CALL4:
|
|
|
|
case C_CALL5:
|
|
|
|
if (pc[0] < 0 || pc[0] >= caml_prim_name_table.size)
|
2005-10-18 07:04:13 -07:00
|
|
|
sprintf(buf, "%s unknown primitive %d", nam, pc[0]);
|
2004-04-19 01:56:51 -07:00
|
|
|
else
|
2005-10-18 07:04:13 -07:00
|
|
|
sprintf(buf, "%s %s", nam, (char *) caml_prim_name_table.contents[pc[0]]);
|
2004-04-19 01:56:51 -07:00
|
|
|
break;
|
|
|
|
default:
|
2005-10-18 07:04:13 -07:00
|
|
|
sprintf(buf, "%s", nam);
|
2004-04-19 01:56:51 -07:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2004-04-22 02:48:04 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
caml_trace_value_file (value v, code_t prog, int proglen, FILE * f)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
fprintf (f, "%#lx", v);
|
|
|
|
if (!v)
|
|
|
|
return;
|
2008-01-03 01:37:10 -08:00
|
|
|
if (prog && v % sizeof (int) == 0
|
2010-01-22 04:48:24 -08:00
|
|
|
&& (code_t) v >= prog
|
|
|
|
&& (code_t) v < (code_t) ((char *) prog + proglen))
|
2004-04-22 02:48:04 -07:00
|
|
|
fprintf (f, "=code@%d", (code_t) v - prog);
|
|
|
|
else if (Is_long (v))
|
2005-09-22 07:21:50 -07:00
|
|
|
fprintf (f, "=long%" ARCH_INTNAT_PRINTF_FORMAT "d", Long_val (v));
|
2010-01-22 04:48:24 -08:00
|
|
|
else if ((void*)v >= (void*)caml_stack_low
|
|
|
|
&& (void*)v < (void*)caml_stack_high)
|
2005-09-22 07:21:50 -07:00
|
|
|
fprintf (f, "=stack_%d", (intnat*)caml_stack_high - (intnat*)v);
|
2004-04-22 02:48:04 -07:00
|
|
|
else if (Is_block (v)) {
|
|
|
|
int s = Wosize_val (v);
|
|
|
|
int tg = Tag_val (v);
|
|
|
|
int l = 0;
|
|
|
|
switch (tg) {
|
|
|
|
case Closure_tag:
|
|
|
|
fprintf (f, "=closure[s%d,cod%d]", s, (code_t) (Code_val (v)) - prog);
|
|
|
|
goto displayfields;
|
|
|
|
case String_tag:
|
|
|
|
l = caml_string_length (v);
|
|
|
|
fprintf (f, "=string[s%dL%d]'", s, l);
|
|
|
|
for (i = 0; i < ((l>0x1f)?0x1f:l) ; i++) {
|
2010-01-22 04:48:24 -08:00
|
|
|
if (isprint (Byte (v, i)))
|
|
|
|
putc (Byte (v, i), f);
|
|
|
|
else
|
|
|
|
putc ('?', f);
|
2004-04-22 02:48:04 -07:00
|
|
|
};
|
|
|
|
fprintf (f, "'");
|
|
|
|
goto displayfields;
|
|
|
|
case Double_tag:
|
|
|
|
fprintf (f, "=float[s%d]=%g", s, Double_val (v));
|
|
|
|
goto displayfields;
|
|
|
|
case Double_array_tag:
|
|
|
|
fprintf (f, "=floatarray[s%d]", s);
|
|
|
|
for (i = 0; i < ((s>0xf)?0xf:s); i++)
|
2010-01-22 04:48:24 -08:00
|
|
|
fprintf (f, " %g", Double_field (v, i));
|
2004-04-22 02:48:04 -07:00
|
|
|
goto displayfields;
|
|
|
|
case Abstract_tag:
|
|
|
|
fprintf (f, "=abstract[s%d]", s);
|
|
|
|
goto displayfields;
|
|
|
|
case Custom_tag:
|
|
|
|
fprintf (f, "=custom[s%d]", s);
|
|
|
|
goto displayfields;
|
|
|
|
default:
|
|
|
|
fprintf (f, "=block<T%d/s%d>", tg, s);
|
|
|
|
displayfields:
|
|
|
|
if (s > 0)
|
2010-01-22 04:48:24 -08:00
|
|
|
fputs ("=(", f);
|
2004-04-22 02:48:04 -07:00
|
|
|
for (i = 0; i < s; i++) {
|
2010-01-22 04:48:24 -08:00
|
|
|
if (i > 20) {
|
|
|
|
fputs ("....", f);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
if (i > 0)
|
|
|
|
putc (' ', f);
|
|
|
|
fprintf (f, "%#lx", Field (v, i));
|
2004-04-22 02:48:04 -07:00
|
|
|
};
|
|
|
|
if (s > 0)
|
2010-01-22 04:48:24 -08:00
|
|
|
putc (')', f);
|
2004-04-22 02:48:04 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
caml_trace_accu_sp_file (value accu, value * sp, code_t prog, int proglen,
|
2010-01-22 04:48:24 -08:00
|
|
|
FILE * f)
|
2004-04-22 02:48:04 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
value *p;
|
|
|
|
fprintf (f, "accu=");
|
|
|
|
caml_trace_value_file (accu, prog, proglen, f);
|
2005-09-22 07:21:50 -07:00
|
|
|
fprintf (f, "\n sp=%#" ARCH_INTNAT_PRINTF_FORMAT "x @%d:",
|
|
|
|
(intnat) sp, caml_stack_high - sp);
|
2004-04-22 02:48:04 -07:00
|
|
|
for (p = sp, i = 0; i < 12 + (1 << caml_trace_flag) && p < caml_stack_high;
|
|
|
|
p++, i++) {
|
|
|
|
fprintf (f, "\n[%d] ", caml_stack_high - p);
|
|
|
|
caml_trace_value_file (*p, prog, proglen, f);
|
|
|
|
};
|
|
|
|
putc ('\n', f);
|
|
|
|
fflush (f);
|
|
|
|
}
|
|
|
|
|
2003-12-15 10:10:51 -08:00
|
|
|
#endif /* DEBUG */
|