1996-11-29 08:55:09 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Jerome Vouillon, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* Objective Caml port by John Malecki and Xavier Leroy *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Handling of symbol tables (globals and events) *)
|
|
|
|
|
|
|
|
open Instruct
|
|
|
|
open Debugger_config (* Toplevel *)
|
|
|
|
|
|
|
|
let verbose = ref true
|
|
|
|
|
|
|
|
let modules =
|
|
|
|
ref ([] : string list)
|
|
|
|
|
|
|
|
let events =
|
|
|
|
ref ([] : debug_event list)
|
|
|
|
let events_by_pc =
|
|
|
|
(Hashtbl.create 257 : (int, debug_event) Hashtbl.t)
|
1997-02-19 08:09:23 -08:00
|
|
|
let events_by_module =
|
1996-11-29 08:55:09 -08:00
|
|
|
(Hashtbl.create 17 : (string, debug_event array) Hashtbl.t)
|
1997-03-30 11:43:47 -08:00
|
|
|
let all_events_by_module =
|
|
|
|
(Hashtbl.create 17 : (string, debug_event list) Hashtbl.t)
|
1996-11-29 08:55:09 -08:00
|
|
|
|
|
|
|
let read_symbols' bytecode_file =
|
|
|
|
let ic = open_in_bin bytecode_file in
|
|
|
|
let pos_trailer =
|
|
|
|
in_channel_length ic - 16 - String.length Config.exec_magic_number in
|
|
|
|
seek_in ic pos_trailer;
|
|
|
|
let code_size = input_binary_int ic in
|
|
|
|
let data_size = input_binary_int ic in
|
|
|
|
let symbol_size = input_binary_int ic in
|
|
|
|
let debug_size = input_binary_int ic in
|
|
|
|
let magic = String.create (String.length Config.exec_magic_number) in
|
|
|
|
really_input ic magic 0 (String.length Config.exec_magic_number);
|
|
|
|
if magic <> Config.exec_magic_number then begin
|
|
|
|
prerr_string bytecode_file; prerr_endline " is not a bytecode file.";
|
|
|
|
exit 2
|
|
|
|
end;
|
|
|
|
if debug_size = 0 then begin
|
|
|
|
prerr_string bytecode_file; prerr_endline " has no debugging info.";
|
|
|
|
exit 2
|
|
|
|
end;
|
|
|
|
seek_in ic (pos_trailer - debug_size - symbol_size);
|
|
|
|
Symtable.restore_state (input_value ic);
|
|
|
|
let all_events = (input_value ic : debug_event list list) in
|
|
|
|
close_in ic;
|
|
|
|
all_events
|
|
|
|
|
|
|
|
let read_symbols bytecode_file =
|
|
|
|
let all_events = read_symbols' bytecode_file in
|
|
|
|
List.iter
|
|
|
|
(fun evl ->
|
|
|
|
List.iter
|
|
|
|
(fun ev ->
|
1997-05-19 08:42:21 -07:00
|
|
|
events := ev :: !events;
|
|
|
|
Hashtbl.add events_by_pc ev.ev_pos ev)
|
1996-11-29 08:55:09 -08:00
|
|
|
evl)
|
|
|
|
all_events;
|
|
|
|
|
|
|
|
List.iter
|
|
|
|
(function
|
|
|
|
[] -> ()
|
|
|
|
| ev :: _ as evl ->
|
1997-05-19 08:42:21 -07:00
|
|
|
let md = ev.ev_module in
|
1997-03-30 11:43:47 -08:00
|
|
|
let sorted_evl =
|
1997-02-19 08:09:23 -08:00
|
|
|
Sort.list (fun ev1 ev2 -> ev1.ev_char <= ev2.ev_char) evl in
|
1997-05-19 08:42:21 -07:00
|
|
|
modules := md :: !modules;
|
1997-03-30 11:43:47 -08:00
|
|
|
Hashtbl.add all_events_by_module md sorted_evl;
|
|
|
|
let real_evl =
|
|
|
|
Primitives.filter
|
|
|
|
(function
|
|
|
|
{ev_kind = Event_function | Event_return _} -> false
|
|
|
|
| _ -> true)
|
|
|
|
sorted_evl
|
|
|
|
in
|
|
|
|
Hashtbl.add events_by_module md (Array.of_list real_evl))
|
1996-11-29 08:55:09 -08:00
|
|
|
all_events
|
|
|
|
|
1997-03-27 13:08:43 -08:00
|
|
|
let any_event_at_pc pc =
|
1996-11-29 08:55:09 -08:00
|
|
|
Hashtbl.find events_by_pc pc
|
|
|
|
|
1997-03-27 13:08:43 -08:00
|
|
|
let event_at_pc pc =
|
|
|
|
let ev = any_event_at_pc pc in
|
1997-03-30 11:43:47 -08:00
|
|
|
match ev.ev_kind with
|
|
|
|
Event_function | Event_return _ -> raise Not_found
|
|
|
|
| _ -> ev
|
1996-11-29 08:55:09 -08:00
|
|
|
|
1997-02-19 08:09:23 -08:00
|
|
|
(* List all events in module *)
|
|
|
|
let events_in_module mdle =
|
|
|
|
try
|
1997-03-30 11:43:47 -08:00
|
|
|
Hashtbl.find all_events_by_module mdle
|
1997-02-19 08:09:23 -08:00
|
|
|
with Not_found ->
|
1997-03-30 11:43:47 -08:00
|
|
|
[]
|
1997-02-19 08:09:23 -08:00
|
|
|
|
|
|
|
(* Binary search of event at or just after char *)
|
|
|
|
let find_event ev char =
|
1996-11-29 08:55:09 -08:00
|
|
|
let rec bsearch lo hi =
|
1997-03-25 10:15:58 -08:00
|
|
|
if lo >= hi then begin
|
|
|
|
if ev.(hi).ev_char < char then raise Not_found;
|
|
|
|
hi
|
|
|
|
end else begin
|
1996-11-29 08:55:09 -08:00
|
|
|
let pivot = (lo + hi) / 2 in
|
|
|
|
let e = ev.(pivot) in
|
1997-03-25 10:15:58 -08:00
|
|
|
if char <= e.ev_char then bsearch lo pivot
|
|
|
|
else bsearch (pivot + 1) hi
|
1997-02-19 08:09:23 -08:00
|
|
|
end
|
1997-03-25 10:15:58 -08:00
|
|
|
in
|
|
|
|
bsearch 0 (Array.length ev - 1)
|
1997-02-19 08:09:23 -08:00
|
|
|
|
|
|
|
(* Return first event after the given position. *)
|
1997-03-25 10:15:58 -08:00
|
|
|
(* Raise [Not_found] if module is unknown or no event is found. *)
|
1997-02-19 08:09:23 -08:00
|
|
|
let event_at_pos md char =
|
|
|
|
let ev = Hashtbl.find events_by_module md in
|
|
|
|
ev.(find_event ev char)
|
|
|
|
|
|
|
|
(* Return event closest to given position *)
|
1997-03-25 10:15:58 -08:00
|
|
|
(* Raise [Not_found] if module is unknown or no event is found. *)
|
1997-02-19 08:09:23 -08:00
|
|
|
let event_near_pos md char =
|
|
|
|
let ev = Hashtbl.find events_by_module md in
|
1997-03-25 10:15:58 -08:00
|
|
|
try
|
|
|
|
let pos = find_event ev char in
|
|
|
|
(* Desired event is either ev.(pos) or ev.(pos - 1),
|
|
|
|
whichever is closest *)
|
|
|
|
if pos > 0 && char - ev.(pos - 1).ev_char <= ev.(pos).ev_char - char
|
|
|
|
then ev.(pos - 1)
|
|
|
|
else ev.(pos)
|
|
|
|
with Not_found ->
|
|
|
|
let pos = Array.length ev - 1 in
|
|
|
|
if pos < 0 then raise Not_found;
|
|
|
|
ev.(pos)
|
1996-11-29 08:55:09 -08:00
|
|
|
|
1997-02-19 08:09:23 -08:00
|
|
|
(* Flip "event" bit on all instructions *)
|
1996-11-29 08:55:09 -08:00
|
|
|
let set_all_events () =
|
|
|
|
Hashtbl.iter
|
1997-03-27 13:08:43 -08:00
|
|
|
(fun pc ev ->
|
1997-03-30 11:43:47 -08:00
|
|
|
match ev.ev_kind with
|
|
|
|
Event_function | Event_return _ -> ()
|
|
|
|
| _ -> Debugcom.set_event ev.ev_pos)
|
1996-11-29 08:55:09 -08:00
|
|
|
events_by_pc
|