1 /**************************************************************************/
2 /*                                                                        */
3 /*                                 OCaml                                  */
4 /*                                                                        */
5 /*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
6 /*                                                                        */
7 /*   Copyright 1996 Institut National de Recherche en Informatique et     */
8 /*     en Automatique.                                                    */
9 /*                                                                        */
10 /*   All rights reserved.  This file is distributed under the terms of    */
11 /*   the GNU Lesser General Public License version 2.1, with the          */
12 /*   special exception on linking described in the file LICENSE.          */
13 /*                                                                        */
14 /**************************************************************************/
15 
16 /* Interface with the debugger */
17 
18 #ifndef CAML_DEBUGGER_H
19 #define CAML_DEBUGGER_H
20 
21 #ifdef CAML_INTERNALS
22 
23 #include "misc.h"
24 #include "mlvalues.h"
25 
26 CAMLextern int caml_debugger_in_use;
27 CAMLextern int caml_debugger_fork_mode; /* non-zero for parent */
28 extern uintnat caml_event_count;
29 
30 enum event_kind {
31   EVENT_COUNT, BREAKPOINT, PROGRAM_START, PROGRAM_EXIT,
32   TRAP_BARRIER, UNCAUGHT_EXC
33 };
34 
35 void caml_debugger_init (void);
36 void caml_debugger (enum event_kind event);
37 void caml_debugger_cleanup_fork (void);
38 
39 /* Communication protocol */
40 
41 /* Requests from the debugger to the runtime system */
42 
43 enum debugger_request {
44   REQ_SET_EVENT = 'e',          /* uint32_t pos */
45   /* Set an event on the instruction at position pos */
46   REQ_SET_BREAKPOINT = 'B',     /* uint32_t pos, (char k) */
47   /* Set a breakpoint at position pos */
48   /* In profiling mode, the breakpoint kind is set to k */
49   REQ_RESET_INSTR = 'i',        /* uint32_t pos */
50   /* Clear an event or breapoint at position pos, restores initial instr. */
51   REQ_CHECKPOINT = 'c',         /* no args */
52   /* Checkpoint the runtime system by forking a child process.
53      Reply is pid of child process or -1 if checkpoint failed. */
54   REQ_GO = 'g',                 /* uint32_t n */
55   /* Run the program for n events.
56      Reply is one of debugger_reply described below. */
57   REQ_STOP = 's',               /* no args */
58   /* Terminate the runtime system */
59   REQ_WAIT = 'w',               /* no args */
60   /* Reap one dead child (a discarded checkpoint). */
61   REQ_INITIAL_FRAME = '0',      /* no args */
62   /* Set current frame to bottom frame (the one currently executing).
63      Reply is stack offset and current pc. */
64   REQ_GET_FRAME = 'f',          /* no args */
65   /* Return current frame location (stack offset + current pc). */
66   REQ_SET_FRAME = 'S',          /* uint32_t stack_offset */
67   /* Set current frame to given stack offset. No reply. */
68   REQ_UP_FRAME = 'U',           /* uint32_t n */
69   /* Move one frame up. Argument n is size of current frame (in words).
70      Reply is stack offset and current pc, or -1 if top of stack reached. */
71   REQ_SET_TRAP_BARRIER = 'b',   /* uint32_t offset */
72   /* Set the trap barrier at the given offset. */
73   REQ_GET_LOCAL = 'L',          /* uint32_t slot_number */
74   /* Return the local variable at the given slot in the current frame.
75      Reply is one value. */
76   REQ_GET_ENVIRONMENT = 'E',    /* uint32_t slot_number */
77   /* Return the local variable at the given slot in the heap environment
78      of the current frame. Reply is one value. */
79   REQ_GET_GLOBAL = 'G',         /* uint32_t global_number */
80   /* Return the specified global variable. Reply is one value. */
81   REQ_GET_ACCU = 'A',           /* no args */
82   /* Return the current contents of the accumulator. Reply is one value. */
83   REQ_GET_HEADER = 'H',         /* mlvalue v */
84   /* As REQ_GET_OBJ, but sends only the header. */
85   REQ_GET_FIELD = 'F',          /* mlvalue v, uint32_t fieldnum */
86   /* As REQ_GET_OBJ, but sends only one field. */
87   REQ_MARSHAL_OBJ = 'M',        /* mlvalue v */
88   /* Send a copy of the data structure rooted at v, using the same
89      format as [caml_output_value]. */
90   REQ_GET_CLOSURE_CODE = 'C',   /* mlvalue v */
91   /* Send the code address of the given closure.
92      Reply is one uint32_t. */
93   REQ_SET_FORK_MODE = 'K'       /* uint32_t m */
94   /* Set whether to follow the child (m=0) or the parent on fork. */
95 };
96 
97 /* Replies to a REQ_GO request. All replies are followed by three uint32_t:
98    - the value of the event counter
99    - the position of the stack
100    - the current pc. */
101 
102 enum debugger_reply {
103   REP_EVENT = 'e',
104   /* Event counter reached 0. */
105   REP_BREAKPOINT = 'b',
106   /* Breakpoint hit. */
107   REP_EXITED = 'x',
108   /* Program exited by calling exit or reaching the end of the source. */
109   REP_TRAP = 's',
110   /* Trap barrier crossed. */
111   REP_UNCAUGHT_EXC = 'u'
112   /* Program exited due to a stray exception. */
113 };
114 
115 #endif /* CAML_INTERNALS */
116 
117 #endif /* CAML_DEBUGGER_H */
118