-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrace.ml
272 lines (243 loc) · 8.24 KB
/
trace.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
open Eio.Std
open Wayland
let motion = ref true
let shm = ref true
let delete = ref true
let region = ref true
let drawing = ref true
let hints = ref true
let trace iface name =
match iface, name with
| "wl_display", "delete_id" -> !delete
(* Motion *)
| "wl_pointer", ("frame" | "motion") -> !motion
(* Drawing *)
| "wl_surface", ("attach" | "frame" | "damage" | "damage_region") -> !drawing
(* Regions *)
| "wl_compositor", "create_region"
| "wl_region", _
| "wl_surface", "set_input_region" -> !region
(* Shared memory *)
| ("wl_shm" | "wl_shm_pool" | "wl_buffer"), _ -> !shm
(* Hints *)
| "xdg_toplevel", ("set_min_size" | "set_max_size")
| "xdg_surface", "set_window_geometry"
| "wl_surface", ("set_buffer_scale") -> !hints
| _ -> true
module Host = struct
let src = Logs.Src.create "wl-server" ~doc:"host-side of Wayland proxy"
module Log = (val Logs.src_log src : Logs.LOG)
type role = [`Client]
let inbound (type a) (proxy : (a, _, _) Proxy.t) msg =
Log.info (fun f ->
let (module M : Metadata.S with type t = a) = Proxy.metadata proxy in
let msg_name, arg_info = M.events (Msg.op msg) in
if trace M.interface msg_name then (
f "@[<h> <- %a.%s %a@]"
Proxy.pp proxy
msg_name
(Msg.pp_args arg_info) msg
)
)
let outbound (type a) (proxy : (a, _, _) Proxy.t) msg =
Log.info (fun f ->
let (module M) = Proxy.metadata proxy in
let msg_name, arg_info = M.requests (Msg.op msg) in
if trace M.interface msg_name then (
f "@[<h> -> %a.%s %a@]"
Proxy.pp proxy
msg_name
(Msg.pp_args arg_info) msg
)
)
end
module Client = struct
let src = Logs.Src.create "wl-client" ~doc:"client-side of Wayland proxy"
module Log = (val Logs.src_log src : Logs.LOG)
type role = [`Server]
let inbound (type a) (proxy : (a, _, _) Proxy.t) msg =
Log.info (fun f ->
let (module M : Metadata.S with type t = a) = Proxy.metadata proxy in
let msg_name, arg_info = M.requests (Msg.op msg) in
if trace M.interface msg_name then (
f "%a -> @[<h>%a.%s %a@]"
Proxy.pp_transport proxy
Proxy.pp proxy
msg_name
(Msg.pp_args arg_info) msg
)
)
let outbound (type a) (proxy : (a, _, _) Proxy.t) msg =
Log.info (fun f ->
let (module M) = Proxy.metadata proxy in
let msg_name, arg_info = M.events (Msg.op msg) in
if trace M.interface msg_name then (
f "%a <- @[<h>%a.%s %a@]"
Proxy.pp_transport proxy
Proxy.pp proxy
msg_name
(Msg.pp_args arg_info) msg
)
)
end
let pp_status f = function
| Unix.WEXITED x -> Fmt.pf f "exited with status %d" x
| Unix.WSIGNALED x -> Fmt.pf f "killed by signal %d" x
| Unix.WSTOPPED x -> Fmt.pf f "stopped by signal %d" x
module Ring_buffer = struct
type t = {
path : string;
buffer_capacity : int;
mutable active : int; (* Buffer currently being written to *)
buffers : Buffer.t array;
}
let add t msg =
let b = t.buffers.(t.active) in
let b =
if Buffer.length b + String.length msg <= t.buffer_capacity then b
else (
t.active <- (t.active + 1) mod Array.length t.buffers;
let b = t.buffers.(t.active) in
Buffer.reset b;
b
)
in
Buffer.add_string b msg
let create ~log_ring_size path =
let n = 4 in
let buffer_capacity = max (log_ring_size / n) 100 in
let buffers = Array.init n (fun _ -> Buffer.create buffer_capacity) in
{ path; active = 0; buffers; buffer_capacity }
let flush_to_channel t ch =
let rec show_after i =
let i = (i + 1) mod Array.length t.buffers in
Buffer.output_buffer ch t.buffers.(i);
Buffer.reset t.buffers.(i);
if i <> t.active then show_after i
in
show_after t.active
let flush_to_file t =
let ch = open_out_gen [Open_append; Open_creat] 0o666 t.path in
Fun.protect ~finally:(fun () -> close_out ch) @@ fun () ->
output_string ch "=== flushing log to file ===\n";
flush_to_channel t ch;
flush ch
end
let create_ring_control_socket ~sw ~fs ~wayland_display ring =
let ring_buffer_log_ctl = Printf.sprintf "/run/user/%d/%s-ctl" (Unix.getuid ()) (Filename.basename wayland_display) in
if Sys.file_exists ring_buffer_log_ctl then Unix.unlink ring_buffer_log_ctl;
Unix.mkfifo ring_buffer_log_ctl 0o600;
let ring_buffer_log_ctl = (fs, ring_buffer_log_ctl) in
Fiber.fork_daemon ~sw (fun () ->
while true do
Eio.Path.with_open_in ring_buffer_log_ctl @@ fun pipe ->
match Eio.Buf_read.(parse line) pipe ~max_size:1024 with
| Ok cmd ->
Log.warn (fun f -> f "Got command on %a: %S (dumping log)" Eio.Path.pp ring_buffer_log_ctl cmd);
Ring_buffer.flush_to_file ring
| Error (`Msg e) ->
Log.warn (fun f -> f "No command on %a: %s" Eio.Path.pp ring_buffer_log_ctl e);
done;
assert false
)
let ms_of_time x = (truncate (x *. 1000.)) mod 1000
let pp_timestamp f x =
let open Unix in
let tm = localtime x in
Fmt.pf f "%04d-%02d-%02d %02d:%02d:%02d.%03d" (tm.tm_year + 1900) (tm.tm_mon + 1)
tm.tm_mday tm.tm_hour tm.tm_min tm.tm_sec (ms_of_time x)
let reporter ring =
let report src level ~over k msgf =
let src = Logs.Src.name src in
msgf @@ fun ?header ?tags:_ fmt ->
Fmt.kstr (fun line ->
begin match ring with
| Some ring ->
Ring_buffer.add ring line;
if level < Logs.Info then (
output_string stderr line;
flush stderr;
)
| None ->
output_string stderr line;
flush stderr;
end;
over ();
k ()
)
("%a %11s %a: @[" ^^ fmt ^^ "@]@.")
pp_timestamp (Unix.gettimeofday ())
src
Logs.pp_header (level, header)
in
{ Logs.report = report }
let setup_logging ~sw ~fs ~verbose ~log_suppress ~log_ring_file ~log_ring_size ~wayland_display =
let ring = Option.map (Ring_buffer.create ~log_ring_size) log_ring_file in
Logs.set_reporter (reporter ring);
Option.iter (create_ring_control_socket ~sw ~fs ~wayland_display) ring;
let log_level = if verbose then Logs.Info else Logs.Warning in
Logs.(set_level (Some log_level));
let wayland_env, wayland =
match Sys.getenv_opt "WAYLAND_DEBUG_PROXY" with
| Some x -> "WAYLAND_DEBUG_PROXY", x
| None -> "WAYLAND_DEBUG", (Sys.getenv_opt "WAYLAND_DEBUG" |> Option.value ~default:"")
in
let wayland =
if wayland = "1" then ["client"; "server"]
else String.split_on_char ',' wayland
in
wayland |> List.iter (function
| "client" -> Logs.Src.set_level Client.src (Some Logs.Info)
| "server" -> Logs.Src.set_level Host.src (Some Logs.Info)
| "xwayland" ->
Logs.Src.set_level X11.log_src (Some Logs.Info);
Logs.Src.set_level Log.xwayland_src (Some Logs.Info)
| "" -> ()
| x -> Log.warn (fun f -> f "Unknown $%s item %S" wayland_env x)
);
log_suppress |> List.iter (function
| `Motion -> motion := false
| `Shm -> shm := false
| `Delete -> delete := false
| `Region -> region := false
| `Drawing -> drawing := false
| `Hints -> hints := false
)
open Cmdliner
let verbose =
Arg.value @@
Arg.flag @@
Arg.info
~doc:"Verbose logging"
["v"; "verbose"]
let suppress = Arg.enum [
"motion", `Motion;
"shm", `Shm;
"delete", `Delete;
"region", `Region;
"drawing", `Drawing;
"hints", `Hints;
]
let log_suppress =
Arg.value @@
Arg.(opt (list suppress)) [] @@
Arg.info
~doc:"Suppress some log messages"
["log-suppress"]
let log_ring_file =
Arg.value @@
Arg.(opt (some string)) None @@
Arg.info
~doc:"Where to dump the log-ring on error"
["log-ring-path"]
let log_ring_size =
Arg.value @@
Arg.(opt int) 0x80000 @@
Arg.info
~doc:"Size of the log ring buffer (if used)"
["log-ring-size"]
let cmdliner ~sw ~fs =
let make verbose log_suppress log_ring_file log_ring_size ~wayland_display =
setup_logging ~sw ~fs ~verbose ~log_suppress ~log_ring_file ~log_ring_size ~wayland_display
in
Term.(const make $ verbose $ log_suppress $ log_ring_file $ log_ring_size)