Repository navigation
Expand file tree
/
Copy pathhh_record.ml
More file actions
98 lines (83 loc) · 3.1 KB
/
Copy pathhh_record.ml
File metadata and controls
98 lines (83 loc) · 3.1 KB
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
(**
* Copyright (c) 2016, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "hack" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
(*****************************************************************************)
(** This binary connects to the debug port on a hack server and sends the
* stream of events to the Recorder to create a valid recording.
*
* Note: Currently, it only supports starting a new Hack server instance, and
* fails if one is already running (primarily for simplicity of an initial
* implementation - connect to debug port by creating a pipe then forking
* the server process). This will be improved in the future to add the ability
* to attach to an existing server process. *)
(*****************************************************************************)
module DE = Debug_event
module Globals = struct
(**
* We need to have a global reference here to support asynchronous
* interruptions from Unix signals.
*
* This makes me sad too, but other approaches are essentially equivalent
* to this. *)
let recorder = ref Recorder.default_instance
end
module Args = struct
type t = {
root : Path.t;
}
let usage = Printf.sprintf "Usage: %s [WWW DIRECTORY]\n" Sys.argv.(0)
let parse () =
let root = ref None in
let () = Arg.parse [] (fun s -> root := (Some s)) usage in
let root = ClientArgsUtils.get_root !root in
{
root = root;
}
let root args = args.root
end
let rec read_and_record (d_in: Debug_port.in_port) =
let event = Debug_port.read d_in in
match event with
| Debug_event.Stop_recording ->
let () = Globals.recorder := Recorder.add_event event !Globals.recorder in
Exit_status.(exit No_error)
| _ ->
let () = Globals.recorder := Recorder.add_event event !Globals.recorder in
read_and_record d_in
let eprint_events = Recorder.Transcribe_to_consumer (fun events ->
List.iter (fun e -> Printf.eprintf "%s\n" (Recorder_types.to_string e)) events
)
let stop_recording () =
Globals.recorder := (Recorder.add_event DE.Stop_recording !Globals.recorder)
let () =
(** Stop the recording when we get a Unix interrupt signal. *)
Sys_utils.set_signal Sys.sigint (Sys.Signal_handle (fun _ ->
let () = stop_recording () in
raise Exit_status.(Exit_with Interrupted)));
let args = Args.parse () in
HackEventLogger.client_init @@ Args.root args;
let d_in, d_out = Debug_port.create () in
let start_env = {
ClientStart.root = args.Args.root;
ai_mode = None;
no_load = false;
silent = false;
debug_port = (Some d_out);
} in
let e = ClientStart.main start_env in
match e with
| Exit_status.No_error ->
let () = Printf.eprintf "Server started with debug port attached.\n" in
let () = Printf.eprintf "Starting recording.\n" in
let () = Globals.recorder := (Recorder.start {
Recorder.transcriber = eprint_events }) in
let code = read_and_record d_in in
Exit_status.exit code
| _ ->
Exit_status.(exit e)