1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Jerome Vouillon, projet Cristal, INRIA Rocquencourt *)
(* OCaml port by John Malecki and Xavier Leroy *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Low-level communication with the debuggee *)
open Int64ops
open Primitives
(* The current connection with the debuggee *)
let conn = ref Primitives.std_io
(* Set which process the debugger follows on fork. *)
type follow_fork_mode =
Fork_child
| Fork_parent
let fork_mode = ref Fork_parent
let update_follow_fork_mode () =
let a = match !fork_mode with Fork_child -> 0 | Fork_parent -> 1 in
output_char !conn.io_out 'K';
output_binary_int !conn.io_out a
(* Set the current connection, and update the fork mode in case it has
* changed. *)
let set_current_connection io_chan =
conn := io_chan;
update_follow_fork_mode ()
(* Modify the program code *)
type pc =
{ frag : int;
pos : int; }
module Sp = struct
(* Position in the debuggee's stack. *)
type t = {
block : int;
offset : int;
}
let null = { block = -1; offset = -1}
let base sp n = {sp with offset = sp.offset - n}
let compare sp1 sp2 =
match Stdlib.compare sp1.block sp2.block with
| 0 -> Stdlib.compare sp1.offset sp2.offset
| x -> x
end
(* Identifier of the code fragment for the main program.
Numbering starts at 1 and the runtime registers 2 fragments before
the main program: one for uncaught exceptions and one for callbacks.
*)
let main_frag = 3
let set_event {frag; pos} =
output_char !conn.io_out 'e';
output_binary_int !conn.io_out frag;
output_binary_int !conn.io_out pos
let set_breakpoint {frag; pos} =
output_char !conn.io_out 'B';
output_binary_int !conn.io_out frag;
output_binary_int !conn.io_out pos
let reset_instr {frag; pos} =
output_char !conn.io_out 'i';
output_binary_int !conn.io_out frag;
output_binary_int !conn.io_out pos
(* Basic commands for flow control *)
type execution_summary =
Event
| Breakpoint
| Exited
| Trap_barrier
| Uncaught_exc
| Debug_info of Instruct.debug_event list array
| Code_loaded of int
| Code_unloaded of int
type report = {
rep_type : execution_summary;
rep_event_count : int64;
rep_stack_pointer : Sp.t;
rep_program_pointer : pc
}
type checkpoint_report =
Checkpoint_done of int
| Checkpoint_failed
(* Run the debuggee for N events *)
let do_go_smallint n =
output_char !conn.io_out 'g';
output_binary_int !conn.io_out n;
flush !conn.io_out;
Input_handling.execute_with_other_controller
Input_handling.exit_main_loop
!conn
(function () ->
Input_handling.main_loop ();
let summary =
match input_char !conn.io_in with
'e' -> Event
| 'b' -> Breakpoint
| 'x' -> Exited
| 's' -> Trap_barrier
| 'u' -> Uncaught_exc
| 'D' -> Debug_info (input_value !conn.io_in :
Instruct.debug_event list array)
| 'L' -> Code_loaded (input_binary_int !conn.io_in)
| 'U' -> Code_unloaded (input_binary_int !conn.io_in)
| c -> Misc.fatal_error (Printf.sprintf "Debugcom.do_go %c" c)
in
let event_counter = input_binary_int !conn.io_in in
let block = input_binary_int !conn.io_in in
let offset = input_binary_int !conn.io_in in
let frag = input_binary_int !conn.io_in in
let pos = input_binary_int !conn.io_in in
{ rep_type = summary;
rep_event_count = Int64.of_int event_counter;
rep_stack_pointer = Sp.{block; offset};
rep_program_pointer = {frag; pos} })
let rec do_go n =
assert (n >= _0);
if n > max_small_int then
begin match do_go_smallint max_int with
| { rep_type = Event } ->
do_go (n -- max_small_int)
| report ->
{ report with
rep_event_count = report.rep_event_count ++ (n -- max_small_int) }
end
else
do_go_smallint (Int64.to_int n)
(* Perform a checkpoint *)
let do_checkpoint () =
match Sys.os_type with
"Win32" -> failwith "do_checkpoint"
| _ ->
output_char !conn.io_out 'c';
flush !conn.io_out;
let pid = input_binary_int !conn.io_in in
if pid = -1 then Checkpoint_failed else Checkpoint_done pid
(* Kill the given process. *)
let stop chan =
try
output_char chan.io_out 's';
flush chan.io_out
with
Sys_error _ | End_of_file -> ()
(* Ask a process to wait for its child which has been killed. *)
(* (so as to eliminate zombies). *)
let wait_child chan =
try
output_char chan.io_out 'w'
with
Sys_error _ | End_of_file -> ()
(* Move to initial frame (that of current function). *)
(* Return stack position and current pc *)
let initial_frame () =
output_char !conn.io_out '0';
flush !conn.io_out;
let block = input_binary_int !conn.io_in in
let offset = input_binary_int !conn.io_in in
let frag = input_binary_int !conn.io_in in
let pos = input_binary_int !conn.io_in in
(Sp.{block; offset}, {frag; pos})
let set_initial_frame () =
ignore(initial_frame ())
(* Move up one frame *)
(* Return stack position and current pc.
If there's no frame above, return (-1, 0). *)
let up_frame stacksize =
output_char !conn.io_out 'U';
output_binary_int !conn.io_out stacksize;
flush !conn.io_out;
let block = input_binary_int !conn.io_in in
let offset = input_binary_int !conn.io_in in
let frag, pos =
if block = -1 then
begin
assert (offset = -1);
0, 0
end else begin
let frag = input_binary_int !conn.io_in in
let pos = input_binary_int !conn.io_in in
frag, pos
end
in
(Sp.{block; offset}, { frag; pos })
(* Get and set the current frame position *)
let get_frame () =
output_char !conn.io_out 'f';
flush !conn.io_out;
let block = input_binary_int !conn.io_in in
let offset = input_binary_int !conn.io_in in
let frag = input_binary_int !conn.io_in in
let pos = input_binary_int !conn.io_in in
(Sp.{block; offset}, {frag; pos})
let set_frame stack_pos =
output_char !conn.io_out 'S';
output_binary_int !conn.io_out stack_pos.Sp.block;
output_binary_int !conn.io_out stack_pos.Sp.offset
(* Set the trap barrier to given stack position. *)
let set_trap_barrier pos =
output_char !conn.io_out 'b';
output_binary_int !conn.io_out pos.Sp.block;
output_binary_int !conn.io_out pos.Sp.offset
(* Handling of remote values *)
let value_size = if 1 lsl 31 = 0 then 4 else 8
let input_remote_value ic =
really_input_string ic value_size
let output_remote_value ic v =
output_substring ic v 0 value_size
exception Marshalling_error
module Remote_value =
struct
type t = Remote of string | Local of Obj.t
let repr x = Local (Obj.repr x)
let obj = function
| Local obj -> Obj.obj obj
| Remote v ->
output_char !conn.io_out 'M';
output_remote_value !conn.io_out v;
flush !conn.io_out;
try
input_value !conn.io_in
with End_of_file | Failure _ ->
raise Marshalling_error
let is_block = function
| Local obj -> Obj.is_block obj
| Remote v -> Obj.is_block (Array.unsafe_get (Obj.magic v : Obj.t array) 0)
let tag obj =
if not (is_block obj) then Obj.int_tag
else match obj with
| Local obj -> Obj.tag obj
| Remote v ->
output_char !conn.io_out 'H';
output_remote_value !conn.io_out v;
flush !conn.io_out;
let header = input_binary_int !conn.io_in in
header land 0xFF
let size = function
| Local obj -> Obj.size obj
| Remote v ->
output_char !conn.io_out 'H';
output_remote_value !conn.io_out v;
flush !conn.io_out;
let header = input_binary_int !conn.io_in in
if header land 0xFF = Obj.double_array_tag && Sys.word_size = 32
then header lsr 11
else header lsr 10
let field v n =
match v with
| Local obj -> Local(Obj.field obj n)
| Remote v ->
output_char !conn.io_out 'F';
output_remote_value !conn.io_out v;
output_binary_int !conn.io_out n;
flush !conn.io_out;
if input_byte !conn.io_in = 0 then
Remote(input_remote_value !conn.io_in)
else begin
let buf = really_input_string !conn.io_in 8 in
let floatbuf = float n (* force allocation of a new float *) in
String.unsafe_blit buf 0 (Obj.magic floatbuf) 0 8;
Local(Obj.repr floatbuf)
end
let double_field v n =
match v with
| Local obj -> Obj.double_field obj n
| Remote v ->
output_char !conn.io_out 'F';
output_remote_value !conn.io_out v;
output_binary_int !conn.io_out n;
flush !conn.io_out;
if input_byte !conn.io_in = 0 then
raise Marshalling_error
else begin
let buf = really_input_string !conn.io_in 8 in
let floatbuf = float n (* force allocation of a new float *) in
String.unsafe_blit buf 0 (Obj.magic floatbuf) 0 8;
floatbuf
end
let double_array_tag = Obj.double_array_tag
let of_int n =
Local(Obj.repr n)
let local pos =
output_char !conn.io_out 'L';
output_binary_int !conn.io_out pos;
flush !conn.io_out;
Remote(input_remote_value !conn.io_in)
let from_environment pos =
output_char !conn.io_out 'E';
output_binary_int !conn.io_out pos;
flush !conn.io_out;
Remote(input_remote_value !conn.io_in)
let global pos =
output_char !conn.io_out 'G';
output_binary_int !conn.io_out pos;
flush !conn.io_out;
Remote(input_remote_value !conn.io_in)
let accu () =
output_char !conn.io_out 'A';
flush !conn.io_out;
Remote(input_remote_value !conn.io_in)
let closure_code = function
| Local _ -> assert false
| Remote v ->
output_char !conn.io_out 'C';
output_remote_value !conn.io_out v;
flush !conn.io_out;
let frag = input_binary_int !conn.io_in in
let pos = input_binary_int !conn.io_in in
{frag;pos}
let same rv1 rv2 =
match (rv1, rv2) with
(Local obj1, Local obj2) -> obj1 == obj2
| (Remote v1, Remote v2) -> v1 = v2
(* string equality -> equality of remote pointers *)
| (_, _) -> false
let pointer rv =
match rv with
| Remote v ->
let bytes = ref [] in
String.iter (fun c -> bytes := c :: !bytes) v;
let obytes = if Sys.big_endian then List.rev !bytes else !bytes in
let to_hex c = Printf.sprintf "%02x" (Char.code c) in
String.concat "" (List.map to_hex obytes)
| Local _ -> ""
end
|