forked from rems-project/sail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdbmi.ml
257 lines (224 loc) · 10.4 KB
/
gdbmi.ml
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
(**************************************************************************)
(* Sail *)
(* *)
(* Copyright (c) 2013-2017 *)
(* Kathyrn Gray *)
(* Shaked Flur *)
(* Stephen Kell *)
(* Gabriel Kerneis *)
(* Robert Norton-Wright *)
(* Christopher Pulte *)
(* Peter Sewell *)
(* Alasdair Armstrong *)
(* Brian Campbell *)
(* Thomas Bauereiss *)
(* Anthony Fox *)
(* Jon French *)
(* Dominic Mulligan *)
(* Stephen Kell *)
(* Mark Wassell *)
(* *)
(* All rights reserved. *)
(* *)
(* This software was developed by the University of Cambridge Computer *)
(* Laboratory as part of the Rigorous Engineering of Mainstream Systems *)
(* (REMS) project, funded by EPSRC grant EP/K008528/1. *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided that the following conditions *)
(* are met: *)
(* 1. Redistributions of source code must retain the above copyright *)
(* notice, this list of conditions and the following disclaimer. *)
(* 2. Redistributions in binary form must reproduce the above copyright *)
(* notice, this list of conditions and the following disclaimer in *)
(* the documentation and/or other materials provided with the *)
(* distribution. *)
(* *)
(* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' *)
(* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *)
(* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *)
(* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR *)
(* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *)
(* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *)
(* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF *)
(* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *)
(* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, *)
(* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *)
(* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *)
(* SUCH DAMAGE. *)
(**************************************************************************)
open Ast
open Ast_util
open Printf
open Gdbmi_types
let parse_gdb_response str =
let lexbuf = Lexing.from_string str in
Gdbmi_parser.response_eof Gdbmi_lexer.token lexbuf
type gdb_session = (in_channel * out_channel * in_channel) option
let gdb_command = ref "gdb-multiarch"
let gdb_token_counter = ref 0
let gdb_token () =
incr gdb_token_counter;
!gdb_token_counter
let not_connected = Reporting.err_general Parse_ast.Unknown "Not connected to gdb"
let rec wait_for' regexp stdout =
let line = input_line stdout in
if Str.string_match regexp line 0 then (
line
) else (
print_endline Util.(line |> dim |> clear);
wait_for' regexp stdout
)
let wait_for token stdout =
let regexp = Str.regexp (sprintf "^%i\\^" token) in
wait_for' regexp stdout
let wait_for_gdb stdout =
let regexp = Str.regexp_string "(gdb)" in
wait_for' regexp stdout
let send_sync session cmd =
match session with
| None -> raise not_connected
| Some (stdout, stdin, _) ->
let token = gdb_token () in
let cmd = sprintf "%i-%s\n" token cmd in
print_string Util.(cmd |> yellow |> clear);
flush stdin;
output_string stdin cmd;
flush stdin;
wait_for token stdout
let send_regular session cmd =
match session with
| None -> raise not_connected
| Some (stdout, stdin, _) ->
let token = gdb_token () in
print_endline Util.(cmd |> yellow |> clear);
flush stdin;
output_string stdin (cmd ^ "\n");
flush stdin;
ignore (wait_for_gdb stdout)
let synced_registers = ref []
let gdb_sync session =
let gdb_register_names = parse_gdb_response (send_sync session "data-list-register-names") in
let gdb_register_values = parse_gdb_response (send_sync session "data-list-register-values x") in
let names = match gdb_register_names with
| Result (_, "done", output) ->
List.assoc "register-names" output |> gdb_seq |> List.map gdb_string
| _ -> failwith "GDB could not get register names"
in
let values = match gdb_register_values with
| Result (_, "done", output) ->
List.assoc "register-values" output
|> gdb_seq
|> List.map gdb_assoc
|> List.map (List.assoc "value")
|> List.map gdb_string
| _ -> failwith "GDB could not get register names"
in
synced_registers := List.combine names values
let gdb_list_registers session =
gdb_sync session;
List.iter (fun (name, value) ->
print_endline (sprintf "%s: %s" name value)
) !synced_registers
let gdb_read_mem session addr data_size =
let open Value in
let cmd = sprintf "data-read-memory %s x 1 1 %i" (Sail_lib.string_of_bits addr) (Big_int.to_int data_size) in
(* An example response looks something like:
7^done,addr="0x0000000040009e64",nr-bytes="4",total-bytes="4",next-row="0x0000000040009e68",
prev-row="0x0000000040009e60",next-page="0x0000000040009e68",prev-page="0x0000000040009e60",
memory=[{addr="0x0000000040009e64",data=["0x03","0xfc","0x5a","0xd3"]}]
*)
match parse_gdb_response (send_sync session cmd) with
| Result (_, "done", output) ->
List.assoc "memory" output |> gdb_seq
|> List.hd |> gdb_assoc
|> List.assoc "data" |> gdb_seq
|> List.rev_map (fun byte -> Sail_lib.byte_of_int (int_of_string (gdb_string byte)))
|> List.concat
| _ -> failwith "Unexpected response from GDB"
let value_gdb_read_ram session =
let open Value in
function
| [addr_size; data_size; _; addr] ->
mk_vector (gdb_read_mem session (coerce_bv addr) (coerce_int data_size))
| _ -> failwith "gdb_read_ram"
let gdb_effect_interp session state eff =
let open Value in
let open Interpreter in
let lstate, gstate = state in
match eff with
| Read_mem (rk, addr, len, cont) ->
let result = mk_vector (gdb_read_mem session (coerce_bv addr) (coerce_int len)) in
cont result state
| Read_reg (name, cont) ->
begin match List.assoc_opt name !synced_registers with
| Some value ->
let value = mk_vector (Sail_lib.to_bits' (64, Big_int.of_string value)) in
cont value state
| None ->
cont (Bindings.find (mk_id name) gstate.registers) state
end
| Write_reg (name, v, cont) ->
let id = mk_id name in
if Bindings.mem id gstate.registers then
let state' = (lstate, { gstate with registers = Bindings.add id v gstate.registers }) in
cont () state'
else
failwith ("Write of nonexistent register: " ^ name)
| _ ->
failwith "Unsupported in GDB state"
let gdb_hooks session =
Value.add_primop "read_ram" (value_gdb_read_ram session);
Interpreter.set_effect_interp (gdb_effect_interp session)
let () =
let open Interactive in
let session = ref None in
let gdb_start arg =
let stdout, stdin, stderr = Unix.open_process_full (sprintf "%s --interpreter=mi" !gdb_command) [||] in
session := Some (stdout, stdin, stderr);
wait_for_gdb stdout |> ignore;
if arg = "" then () else print_endline (send_sync !session arg)
in
let gdb_send arg =
if arg = "" then () else print_endline (send_sync !session arg)
in
register_command
~name:"gdb_command"
~help:"Use specified gdb. Default is gdb-multiarch. This is the \
correct version on Ubuntu, but other Linux distros and \
operating systems may differ in how they package gdb with \
support for multiple architectures."
(ArgString ("gdb", fun arg -> Action (fun () -> gdb_command := arg)));
register_command
~name:"gdb_start"
~help:"Start a child GDB process sending :0 as the first command, waiting for it to complete"
(ArgString ("command", fun cmd -> Action (fun () -> gdb_start cmd)));
(ArgString ("port", fun port -> Action (fun () ->
if port = "" then
gdb_start "target-select remote localhost:1234"
else
gdb_start ("target-select remote localhost:" ^ port)
))) |> register_command
~name:"gdb_qemu"
~help:"Connect GDB to a remote QEMU target on localhost port :0 (default is 1234, as per -s option for QEMU)";
register_command
~name:"gdb_send"
~help:"Send a GDB/MI command to a child GDB process and wait for it to complete"
(ArgString ("command", fun cmd -> Action (fun () -> gdb_send cmd)));
register_command
~name:"gdb_sync"
~help:"Sync sail registers with GDB"
(Action (fun () -> gdb_sync !session));
register_command
~name:"gdb_list_registers"
~help:"Sync sail registers with GDB and list them"
(Action (fun () -> gdb_list_registers !session));
register_command
~name:"gdb_hooks"
~help:"Make reading and writing memory go via GDB"
(Action (fun () -> gdb_hooks !session));
(ArgString ("symbol_file", fun file -> Action (fun () ->
send_regular !session ("symbol-file " ^ file)
))) |> register_command
~name:"gdb_symbol_file"
~help:"Load debugging symbols into GDB";