Repository navigation
Expand file tree
/
Copy pathhh_record.ml
More file actions
113 lines (96 loc) · 3.58 KB
/
Copy pathhh_record.ml
File metadata and controls
113 lines (96 loc) · 3.58 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(**
* 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 oprint_events events =
List.iter (fun e -> Printf.printf "%s\n" (Recorder_types.to_string e)) events
let print_and_exit recorder =
let events = Recorder.get_events recorder in
oprint_events events;
exit 0
let rec read_and_record recorder (d_in: Debug_port.in_port) =
let event = try Debug_port.read d_in with
| Debug_port.Port_closed ->
let () = Printf.eprintf "Port closed\n%!" in
print_and_exit !recorder
in
let () = recorder := Recorder.add_event event !recorder in
if Recorder.is_finished !recorder
then
print_and_exit !recorder
else
read_and_record recorder d_in
let stop_recording () =
Globals.recorder := (Recorder.add_event DE.Stop_recording !Globals.recorder);
print_and_exit !Globals.recorder
(** Create a pipe. The child out end of the pipe should be closed in the parent
* process *after* forking. This is an unfortunately imperative style. *)
let create_pipe () =
let parent_in, child_out = Unix.pipe () in
Unix.set_close_on_exec parent_in;
parent_in, child_out
let () =
Daemon.check_entry_point (); (* this call might not return *)
let () = Globals.recorder := Recorder.start () in
(** 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 parent_in, child_out = create_pipe () in
let start_env = {
ClientStart.root = args.Args.root;
ai_mode = None;
no_load = false;
silent = false;
debug_port = (Some child_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 () = Unix.close child_out in
read_and_record Globals.recorder (Debug_port.in_port_of_fd parent_in)
| _ ->
Exit_status.(exit e)