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
open RdbgArg
type vars = (Data.ident * Data.t) list
let debug_msg msg =
if args.debug_rdbg then (output_string stdout (msg^"\n"); flush stdout)
let (step_channel : string -> in_channel -> out_channel -> vars -> vars ->
Data.subst list -> Data.subst list) =
fun label ic oc in_vars out_vars sl ->
let my_string_of_float v = Mypervasives.my_string_of_float v args.precision in
let in_vals_str =
List.fold_left
(fun acc (name, _) ->
try
let value = List.assoc name sl in
acc ^ " "^ (Data.val_to_rif_string my_string_of_float value)
with Not_found -> acc
)
""
in_vars
in
let res =
debug_msg (Printf.sprintf "%s receives '%s'.\n" label in_vals_str);
output_string oc (in_vals_str ^"\n");
flush oc;
RifIO.read
~debug:args.debug_rdbg ~label:("read the result of "^label) ic None out_vars
in
res
open RdbgPlugin
let (make_gen : in_channel -> out_channel -> (string -> unit) -> string -> string ->
RdbgPlugin.t) =
fun cmd_ic cmd_oc kill ctx_id ctx_msg ->
let cmd_in, cmd_out =
try
if args.debug_rdbg then (
Printf.fprintf stderr "Wait for interface declarations on stdin (%s).\n" ctx_msg;
flush stderr);
flush cmd_oc;
RifIO.read_interface ~debug:args.debug_rdbg ~label:ctx_msg cmd_ic (Some stderr)
with RifIO.Bye -> exit 2
in
let step =
if args.debug_rdbg then (Printf.fprintf stderr "\nStep %s.\n" ctx_msg; flush stderr);
step_channel ctx_id cmd_ic cmd_oc cmd_in cmd_out
in
let reset () =
debug_msg (Printf.sprintf "==> send '#reset' to %s\n" ctx_msg) ;
RifIO.write cmd_oc "#reset\n"; flush cmd_oc
in
let step_dbg sl ctx cont =
let cont2 sl_out ctx =
{ ctx with
RdbgEvent.kind = RdbgEvent.Exit;
RdbgEvent.lang = "RIF";
RdbgEvent.sinfo = None;
RdbgEvent.data = sl_out;
RdbgEvent.name=ctx_id;
RdbgEvent.inputs = cmd_in;
RdbgEvent.outputs = cmd_out;
RdbgEvent.locals = [];
RdbgEvent.next = (
fun () ->
let ctx = { ctx with
RdbgEvent.data = sl_out;
RdbgEvent.nb = ctx.RdbgEvent.nb+1 } in
cont sl_out ctx );
}
in
{ ctx with
RdbgEvent.kind = RdbgEvent.Call;
RdbgEvent.lang = "RIF";
RdbgEvent.sinfo = None;
RdbgEvent.name=ctx_id;
RdbgEvent.inputs = cmd_in;
RdbgEvent.outputs = cmd_out;
RdbgEvent.locals = [];
RdbgEvent.next = (
fun () ->
let ctx = { ctx with RdbgEvent.nb = ctx.RdbgEvent.nb+1 } in
cont2 (step sl) ctx );
}
in
{
id = ctx_msg;
inputs = cmd_in;
outputs= cmd_out;
reset=reset;
kill=kill;
save_state= (fun _ -> ());
restore_state= (fun _ -> ());
init_inputs=[];
init_outputs=[];
step=step;
step_dbg=step_dbg;
}
let (make : string -> RdbgPlugin.t) =
fun cmd ->
let (stdin_in, stdin_out) = Unix.pipe () in
let (stdout_in, stdout_out) = Unix.pipe () in
let cmd_ic = Unix.in_channel_of_descr stdout_in in
let cmd_oc = Unix.out_channel_of_descr stdin_out in
let _ =
set_binary_mode_in cmd_ic false;
set_binary_mode_out cmd_oc false;
in
let arg_list = Str.split (Str.regexp "[ \t]+") cmd in
let prog, _ = match arg_list with x::l -> x,l | [] -> assert false in
let pid_cmd =
let arg_array = Array.of_list arg_list in
try
if args.debug_rdbg then (
let msg = Printf.sprintf "Unix.create_process %s [%s]\n"
prog (String.concat "," arg_list)
in
output_string stderr msg ;
flush stderr
);
Unix.create_process prog arg_array stdin_in stdout_out Unix.stderr
with Unix.Unix_error(e,_, prog) ->
let msg = Unix.error_message e in
Printf.eprintf "*** Error when creating process with %s: %s\n" prog msg;
exit 2
in
let _ =
Printf.eprintf "Process %d (%s) created\n" pid_cmd cmd; flush stderr
in
let kill msg =
Unix.close stdin_in;
Unix.close stdin_out;
Unix.close stdout_in;
Unix.close stdout_out;
(try
Printf.eprintf "%s\nKilling process %d (%s)\n" msg pid_cmd cmd;
flush stderr;
Unix.kill pid_cmd Sys.sigterm
with e ->
Printf.printf "Killing %s process failed: %s\n" prog (Printexc.to_string e))
in
make_gen cmd_ic cmd_oc kill prog cmd
let rec connect_loop sock addr k =
try Unix.connect sock addr
with _ ->
if k > 0
then (
if args.debug_rdbg then (
let ni = Unix.getnameinfo addr [] in
Printf.fprintf stderr "connect %s:%s failed; try again in a second.\n"
ni.Unix.ni_hostname ni.Unix.ni_service;
flush stderr
);
Unix.sleep 1;
connect_loop sock addr (k-1)
)
else failwith "rdbg: cannot connect to the socket"
let (make_socket : string -> int -> RdbgPlugin.t) =
fun sock_adr port ->
let _ =
if args.debug_rdbg then (
Printf.fprintf stderr "Start a connection on %s:%d\n" sock_adr port;
flush stderr)
in
let inet_addr = Unix.inet_addr_of_string sock_adr in
let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
let (sock_in, sock_out) =
try
connect_loop sock (Unix.ADDR_INET(inet_addr, port)) 10 ;
if args.debug_rdbg then (
Printf.fprintf stderr "Socket %s:%d connected \n" sock_adr port;
flush stderr);
(Unix.in_channel_of_descr sock, Unix.out_channel_of_descr sock)
with
Unix.Unix_error(errcode, funcstr, paramstr) ->
failwith ("rdbg socket connect failure: " ^ (Unix.error_message errcode) ^
"(" ^ funcstr ^ " " ^ paramstr ^")\n")
in
let kill msg =
Printf.printf "Killing the socket process (%s:%i)\n" sock_adr port;
print_string ("'"^msg^"'");
flush stdout;
output_string sock_out msg;
flush sock_out;
let str = input_line sock_in in
print_string (str ^"\n");
flush stdout;
Unix.shutdown sock Unix.SHUTDOWN_ALL
in
let label = Printf.sprintf "[%s:%i] " sock_adr port in
let _ =
if args.debug_rdbg then (
Printf.fprintf stderr "\nInterface declarations: ok.\n"; flush stderr);
in
make_gen sock_in sock_out kill label label
let (make_socket_init : string -> int -> RdbgPlugin.t) =
fun _addr _port ->
assert false
let (make_init : string -> RdbgPlugin.t) =
fun _cmd ->
assert false