Source file trace_writer.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
open! Core
open! Import
let debug = ref false
let is_kernel_address addr = Int64.(addr < 0L)
module Mapped_time : sig
type t = private Time_ns.Span.t [@@deriving sexp_of, compare]
include Comparable with type t := t
val start_of_trace : t
val create : Time_ns.Span.t -> base_time:Time_ns.Span.t -> t
val is_base_time : t -> bool
val add : t -> Time_ns.Span.t -> t
val diff : t -> t -> Time_ns.Span.t
end = struct
module T = struct
type t = Time_ns.Span.t [@@deriving sexp, compare]
end
let start_of_trace = Time_ns.Span.zero
let create t ~base_time = Time_ns.Span.( - ) t base_time
let is_base_time = Time_ns.Span.( = ) Time_ns.Span.zero
let add = Time_ns.Span.( + )
let diff = Time_ns.Span.( - )
include T
include Comparable.Make (T)
end
module Pending_event = struct
module Kind = struct
type t =
| Call of
{ addr : int64
; offset : int
; from_untraced : bool
}
| Ret
| Ret_from_untraced of { reset_time : Mapped_time.t }
[@@deriving sexp_of]
end
type t =
{ symbol : Symbol.t
; kind : Kind.t
}
[@@deriving sexp_of]
let create_call location ~from_untraced =
let { Event.Location.instruction_pointer; symbol; symbol_offset } = location in
{ symbol
; kind = Call { addr = instruction_pointer; offset = symbol_offset; from_untraced }
}
;;
end
module Callstack = struct
type t =
{ stack : Symbol.t Stack.t
; create_time : Mapped_time.t
}
[@@deriving sexp_of]
let create ~create_time = { stack = Stack.create (); create_time }
let push t v = Stack.push t.stack v
let pop t = Stack.pop t.stack
let top t = Stack.top t.stack
let is_empty t = Stack.is_empty t.stack
end
module Thread_info = struct
type 'thread t =
{ thread : ('thread[@sexp.opaque])
;
mutable callstack : Callstack.t
; inactive_callstacks : Callstack.t Stack.t
; mutable last_decode_error_time : Mapped_time.t
;
frames_to_unwind : int ref
; mutable pending_events : Pending_event.t list
; mutable pending_time : Mapped_time.t
; start_events : (Mapped_time.t * Pending_event.t) Deque.t
; mutable last_event_time : Mapped_time.t
}
[@@deriving sexp_of]
let set_callstack t ~is_kernel_address ~time =
let create_time = if is_kernel_address then time else t.last_decode_error_time in
t.callstack <- Callstack.create ~create_time
;;
let set_callstack_from_addr t ~addr ~time =
set_callstack t ~is_kernel_address:(is_kernel_address addr) ~time
;;
end
module type Trace = Trace_writer_intf.S_trace
type 'thread inner =
{ debug_info : Elf.Addr_table.t
; thread_info : 'thread Thread_info.t Hashtbl.M(Event.Thread).t
; base_time : Time_ns.Span.t
; trace_mode : Trace_mode.t
; trace : (module Trace with type thread = 'thread)
; annotate_inferred_start_times : bool
}
type t = T : 'thread inner -> t
let sexp_of_inner inner =
[%sexp_of: _ Thread_info.t Hashtbl.M(Event.Thread).t] inner.thread_info
;;
let sexp_of_t (T inner) = sexp_of_inner inner
let allocate_pid (type thread) (t : thread inner) ~name : int =
let module T = (val t.trace) in
T.allocate_pid ~name
;;
let allocate_thread (type thread) (t : thread inner) ~pid ~name : thread =
let module T = (val t.trace) in
T.allocate_thread ~pid ~name
;;
let write_duration_begin
(type thread)
(t : thread inner)
~args
~thread
~name
~(time : Mapped_time.t)
: unit
=
let module T = (val t.trace) in
T.write_duration_begin ~args ~thread ~name ~time:(time :> Time_ns.Span.t)
;;
let write_duration_end
(type thread)
(t : thread inner)
~args
~thread
~name
~(time : Mapped_time.t)
: unit
=
let module T = (val t.trace) in
T.write_duration_end ~args ~thread ~name ~time:(time :> Time_ns.Span.t)
;;
let write_duration_complete
(type thread)
(t : thread inner)
~args
~thread
~name
~(time : Mapped_time.t)
~(time_end : Mapped_time.t)
: unit
=
let module T = (val t.trace) in
T.write_duration_complete
~args
~thread
~name
~time:(time :> Time_ns.Span.t)
~time_end:(time_end :> Time_ns.Span.t)
;;
let write_duration_instant
(type thread)
(t : thread inner)
~args
~thread
~name
~(time : Mapped_time.t)
: unit
=
let module T = (val t.trace) in
T.write_duration_instant ~args ~thread ~name ~time:(time :> Time_ns.Span.t)
;;
let real_trace (trace : Tracing.Trace.t) =
let module T = struct
type thread = Tracing.Trace.Thread.t
let allocate_pid = Tracing.Trace.allocate_pid trace
let allocate_thread = Tracing.Trace.allocate_thread trace
let write_duration_begin = Tracing.Trace.write_duration_begin trace ~category:""
let write_duration_end = Tracing.Trace.write_duration_end trace ~category:""
let write_duration_complete = Tracing.Trace.write_duration_complete trace ~category:""
let write_duration_instant = Tracing.Trace.write_duration_instant trace ~category:""
end
in
(module T : Trace with type thread = Tracing.Trace.Thread.t)
;;
let map_time t time = Mapped_time.create time ~base_time:t.base_time
let write_hits (T t) hits =
if not (List.is_empty hits)
then (
let pid = allocate_pid t ~name:"Snapshot symbol hits" in
let thread = allocate_thread t ~pid ~name:"hits" in
List.iter hits ~f:(fun (sym, (hit : Breakpoint.Hit.t)) ->
let is_default_symbol = String.( = ) sym Magic_trace.Private.stop_symbol in
let name = [%string "hit %{sym}"] in
let time = map_time t hit.timestamp in
let args =
Tracing.Trace.Arg.
[ "timestamp", Int (Time_ns.Span.to_int_ns hit.timestamp)
; "tid", Int (Pid.to_int hit.tid)
; "ip", Pointer hit.ip
]
in
let args =
if is_default_symbol
then
Tracing.Trace.Arg.
[ "timestamp_passed", Int (Time_ns.Span.to_int_ns hit.passed_timestamp)
; "arg", Int hit.passed_val
]
@ args
else args
in
let valid_timestamp =
Time_ns.Span.(
hit.passed_timestamp > t.base_time && hit.passed_timestamp < hit.timestamp)
in
let start =
if is_default_symbol && valid_timestamp
then map_time t hit.passed_timestamp
else time
in
write_duration_complete t ~thread ~args ~name ~time:start ~time_end:time))
;;
let create_expert
~trace_mode
~debug_info
~earliest_time
~hits
~annotate_inferred_start_times
trace
=
let base_time =
List.fold hits ~init:earliest_time ~f:(fun acc (_, (hit : Breakpoint.Hit.t)) ->
Time_ns.Span.min acc hit.timestamp)
in
let t =
T
{ debug_info = Option.value debug_info ~default:(Int.Table.create ())
; thread_info = Hashtbl.create (module Event.Thread)
; base_time
; trace_mode
; trace
; annotate_inferred_start_times
}
in
write_hits t hits;
t
;;
let create
~trace_mode
~debug_info
~earliest_time
~hits
~annotate_inferred_start_times
trace
=
create_expert
~trace_mode
~debug_info
~earliest_time
~hits
~annotate_inferred_start_times
(real_trace trace)
;;
let write_pending_event'
(type thread)
(t : thread inner)
(thread : thread Thread_info.t)
time
{ Pending_event.symbol; kind }
=
let symbol = Symbol.demangle symbol in
let display_name = Symbol.display_name symbol in
match kind with
| Call { addr; offset; from_untraced } ->
let base_address = Int64.(addr - of_int offset) in
let open Tracing.Trace.Arg in
let symbol_args =
let address = [ "address", Pointer addr ] in
match symbol with
| From_perf_map { start_addr = _; size = _; function_ = _ } ->
address @ [ "symbol", Interned display_name ]
| _ ->
(match Option.bind (Int64.to_int base_address) ~f:(Hashtbl.find t.debug_info) with
| None -> address @ [ "symbol", Interned display_name ]
| Some (info : Elf.Location.t) ->
address
@ [ "line", Int info.line
; "col", Int info.col
; "symbol", Interned display_name
]
@
(match info.filename with
| Some x -> [ "file", Interned x ]
| None -> []))
in
let inferred_start_time_arg =
if from_untraced then [ "inferred_start_time", Interned "true" ] else []
in
let args = symbol_args @ inferred_start_time_arg in
let name =
if t.annotate_inferred_start_times && from_untraced
then display_name ^ " [inferred start time]"
else display_name
in
write_duration_begin t ~thread:thread.thread ~name ~time ~args
| Ret -> write_duration_end t ~name:display_name ~time ~thread:thread.thread ~args:[]
| Ret_from_untraced { reset_time } ->
write_duration_complete
t
~time:reset_time
~time_end:time
~name:(Symbol.display_name Unknown)
~thread:thread.thread
~args:[]
;;
let consumes_time { Pending_event.symbol = _; kind } =
match kind with
| Call _ -> true
| Ret | Ret_from_untraced _ -> false
;;
let write_pending_event
(t : _ inner)
(thread : _ Thread_info.t)
time
(ev : Pending_event.t)
=
match ev.kind with
| Ret_from_untraced _ | Call { from_untraced = true; _ } ->
Deque.enqueue_front thread.start_events (time, ev)
| Call _ when Mapped_time.is_base_time time ->
Deque.enqueue_back thread.start_events (time, ev)
| _ -> write_pending_event' t thread time ev
;;
let flush (t : _ inner) ~to_time (thread : _ Thread_info.t) =
let count = List.count thread.pending_events ~f:consumes_time in
let total_ns = Mapped_time.diff to_time thread.pending_time |> Time_ns.Span.to_int_ns in
let ns_offset = ref 0 in
let shares_consumed = ref 0 in
List.iter (List.rev thread.pending_events) ~f:(fun ev ->
let ns_share =
if consumes_time ev
then (
incr shares_consumed;
(total_ns - !ns_offset) / (count - !shares_consumed + 1))
else 0
in
let time =
Mapped_time.add thread.pending_time (Time_ns.Span.of_int_ns !ns_offset)
in
ns_offset := !ns_offset + ns_share;
write_pending_event t thread time ev);
thread.pending_time <- to_time;
thread.pending_events <- []
;;
let add_event (t : _ inner) (thread : _ Thread_info.t) time ev =
if Mapped_time.( <> ) time thread.pending_time then flush t ~to_time:time thread;
thread.pending_events <- ev :: thread.pending_events
;;
let opt_pid_to_string opt_pid =
match opt_pid with
| None -> "?"
| Some pid -> Pid.to_string pid
;;
let hack_155 (thread_info : _ Thread_info.t) time =
let last_decode_error_time = thread_info.last_decode_error_time in
if Mapped_time.( = ) time last_decode_error_time
&& Mapped_time.( <> ) last_decode_error_time Mapped_time.start_of_trace
then Mapped_time.add time (Time_ns.Span.of_int_ns 1)
else time
;;
let event_time t (event : Event.t) (thread_info : _ Thread_info.t) =
let event_time = Event.time event in
let unadjusted_time =
match%optional.Time_ns_unix.Span.Option event_time with
| None ->
thread_info.last_event_time
| Some time ->
let time = map_time t time in
thread_info.last_event_time <- time;
time
in
hack_155 thread_info unadjusted_time
;;
let create_thread t event =
let thread = Event.thread event in
let effective_time =
match%optional.Time_ns_unix.Span.Option Event.time event with
| None -> Mapped_time.start_of_trace
| Some time -> map_time t time
in
let trace_pid =
allocate_pid
t
~name:[%string "%{opt_pid_to_string thread.pid}/%{opt_pid_to_string thread.tid}"]
in
let thread = allocate_thread t ~pid:trace_pid ~name:"main" in
{ Thread_info.thread
; callstack = Callstack.create ~create_time:effective_time
; inactive_callstacks = Stack.create ()
; last_decode_error_time = effective_time
; frames_to_unwind = ref 0
; pending_events = []
; pending_time = Mapped_time.start_of_trace
; start_events = Deque.create ()
; last_event_time = effective_time
}
;;
let call t thread_info ~time ~location =
let ev = Pending_event.create_call location ~from_untraced:false in
add_event t thread_info time ev;
Callstack.push thread_info.callstack location.symbol
;;
let ret t (thread_info : _ Thread_info.t) ~time =
match Callstack.pop thread_info.callstack with
| Some symbol -> add_event t thread_info time { symbol; kind = Ret }
| None ->
add_event
t
thread_info
time
{ symbol = From_perf "[unknown]"
; kind = Ret_from_untraced { reset_time = thread_info.callstack.create_time }
}
;;
let check_current_symbol
t
(thread_info : _ Thread_info.t)
~time
(location : Event.Location.t)
=
match Callstack.top thread_info.callstack with
| Some known when not ([%compare.equal: Symbol.t] known location.symbol) ->
ret t thread_info ~time;
call t thread_info ~time ~location
| Some _ -> ()
| None ->
let ev = Pending_event.create_call location ~from_untraced:true in
write_pending_event t thread_info thread_info.callstack.create_time ev;
Callstack.push thread_info.callstack location.symbol
;;
let unwind_stack t (thread_info : _ Thread_info.t) ~time diff =
let frames_to_unwind = thread_info.frames_to_unwind in
for _ = 0 to !frames_to_unwind + diff do
ret t thread_info ~time
done;
frames_to_unwind := 0
;;
let ret_track_exn_data t thread_info ~time =
let { Thread_info.callstack; frames_to_unwind; _ } = thread_info in
(match Callstack.top callstack with
| Some (From_perf symbol) ->
(match symbol with
| "caml_next_frame_descriptor" -> incr frames_to_unwind
| "caml_raise_exn" -> unwind_stack t thread_info ~time (-2)
| "caml_raise_exception" -> unwind_stack t thread_info ~time 1
| _ -> ())
| _ -> ());
ret t thread_info ~time
;;
let rec clear_callstack t (thread_info : _ Thread_info.t) ~time =
match Callstack.top thread_info.callstack with
| None -> ()
| Some _ ->
ret t thread_info ~time;
clear_callstack t thread_info ~time
;;
let rec clear_all_callstacks t thread_info ~time =
clear_callstack t thread_info ~time;
match Stack.pop thread_info.inactive_callstacks with
| None -> ()
| Some callstack ->
thread_info.callstack <- callstack;
clear_all_callstacks t thread_info ~time
;;
let assert_trace_mode t event trace_modes =
if List.find trace_modes ~f:(Trace_mode.equal t.trace_mode) |> Option.is_none
then
Core.eprint_s
[%message
"BUG: assumptions violated, saw an unexpected event for this trace mode"
~trace_mode:(t.trace_mode : Trace_mode.t)
(event : Event.t)]
;;
let end_of_thread t (thread_info : _ Thread_info.t) ~time : unit =
let to_time = thread_info.pending_time in
Deque.iter' thread_info.start_events `front_to_back ~f:(fun (time, ev) ->
write_pending_event' t thread_info time ev);
Deque.clear thread_info.start_events;
clear_all_callstacks t thread_info ~time;
flush t ~to_time thread_info
;;
let end_of_trace (T t) =
Hashtbl.iter t.thread_info ~f:(fun thread_info ->
end_of_thread t thread_info ~time:thread_info.last_event_time)
;;
let write_event (T t) event =
let thread = Event.thread event in
let thread_info =
Hashtbl.find_or_add t.thread_info thread ~default:(fun () -> create_thread t event)
in
let thread = thread_info.thread in
let time = event_time t event thread_info in
let outer_event = event in
match event with
| Error { thread = _; instruction_pointer; message; time = _ } ->
let name = sprintf !"[decode error: %s]" message in
write_duration_instant t ~thread ~name ~time ~args:[];
end_of_thread t thread_info ~time;
thread_info.last_decode_error_time <- time;
let is_kernel_address =
match instruction_pointer with
| None -> false
| Some ip -> is_kernel_address ip
in
Thread_info.set_callstack thread_info ~is_kernel_address ~time
| Ok event ->
let { Event.Ok.thread = _
; time = _
; kind
; trace_state_change
; src = _
; dst
}
=
event
in
(match kind, trace_state_change with
| Some Call, (None | Some End) -> call t thread_info ~time ~location:dst
| ( Some (Call | Syscall | Return | Hardware_interrupt | Iret | Sysret | Jump)
, Some Start )
| Some (Hardware_interrupt | Jump), Some End ->
raise_s
[%message
"BUG: magic-trace devs thought this event was impossible, but you just proved \
them wrong. Please report this to \
https://github.com/janestreet/magic-trace/issues/"
(event : Event.Ok.t)]
| None, Some End -> call t thread_info ~time ~location:Event.Location.untraced
| Some Syscall, Some End ->
assert_trace_mode t outer_event [ Userspace ];
call t thread_info ~time ~location:Event.Location.syscall
| Some Return, Some End -> call t thread_info ~time ~location:Event.Location.returned
| Some Return, None ->
ret_track_exn_data t thread_info ~time;
check_current_symbol t thread_info ~time dst
| None, Some Start ->
if Trace_mode.equal t.trace_mode Kernel
then (
clear_callstack t thread_info ~time;
Thread_info.set_callstack_from_addr
thread_info
~addr:dst.instruction_pointer
~time)
else if Callstack.is_empty thread_info.callstack
then
call t thread_info ~time ~location:dst
else
ret_track_exn_data t thread_info ~time
| Some ((Syscall | Hardware_interrupt) as kind), None ->
[ [ Trace_mode.Userspace_and_kernel ]
; (if [%compare.equal: Event.Kind.t] kind Hardware_interrupt
then [ Kernel ]
else [])
]
|> List.concat
|> assert_trace_mode t outer_event;
Stack.push thread_info.inactive_callstacks thread_info.callstack;
Thread_info.set_callstack_from_addr thread_info ~addr:dst.instruction_pointer ~time;
call t thread_info ~time ~location:dst
| Some (Iret | Sysret), Some End ->
assert_trace_mode t outer_event [ Kernel ];
clear_callstack t thread_info ~time;
call t thread_info ~time ~location:Event.Location.untraced
| Some ((Iret | Sysret) as kind), None ->
[ [ Trace_mode.Userspace_and_kernel ]
; (if [%compare.equal: Event.Kind.t] kind Iret then [ Kernel ] else [])
]
|> List.concat
|> assert_trace_mode t outer_event;
clear_callstack t thread_info ~time;
(match Stack.pop thread_info.inactive_callstacks with
| Some callstack -> thread_info.callstack <- callstack
| None ->
Thread_info.set_callstack_from_addr
thread_info
~addr:dst.instruction_pointer
~time;
check_current_symbol t thread_info ~time dst)
| Some Jump, None -> check_current_symbol t thread_info ~time dst
| None, _ -> ());
if !debug then print_s (sexp_of_inner t)
;;