Skip to content

Commit 26a74a0

Browse files
committed
Add print PID option under MAGIC_TRACE_DEBUG (-z-print-pid)
Signed-off-by: Dhruv Maroo <[email protected]>
1 parent 9fdee3b commit 26a74a0

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

core/backend_intf.ml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module type S = sig
2121
val attach_and_record
2222
: Record_opts.t
2323
-> debug_print_perf_commands:bool
24+
-> debug_print_pid:bool
2425
-> subcommand:Subcommand.t
2526
-> when_to_snapshot:When_to_snapshot.t
2627
-> trace_scope:Trace_scope.t

src/perf_tool_backend.ml

+4
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ module Recording = struct
216216
let attach_and_record
217217
{ Record_opts.multi_thread; full_execution; snapshot_size; callgraph_mode }
218218
~debug_print_perf_commands
219+
~debug_print_pid
219220
~(subcommand : Subcommand.t)
220221
~(when_to_snapshot : When_to_snapshot.t)
221222
~(trace_scope : Trace_scope.t)
@@ -392,6 +393,9 @@ module Recording = struct
392393
in
393394
if debug_print_perf_commands then Core.printf "%s\n%!" (String.concat ~sep:" " argv);
394395
(* Perf prints output we don't care about and --quiet doesn't work for some reason *)
396+
if debug_print_pid
397+
then Core.printf "Passed PIDs to perf: %s\n%!" (String.concat ~sep:"" pid_opt);
398+
(* No need to format `pid_opt` again, since it already is a list of comma separated PIDs *)
395399
let perf_pid = perf_fork_exec ~env:perf_env ~prog:"perf" ~argv () in
396400
(* This detaches the perf process from our "process group" but not our session. This
397401
makes it so that when Ctrl-C is sent to magic_trace in the terminal to end an attach

src/trace.ml

+26-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ let debug_print_perf_commands =
7575
|> debug_flag
7676
;;
7777

78+
let debug_print_pid =
79+
let open Command.Param in
80+
flag
81+
"-z-print-pid"
82+
no_arg
83+
~doc:"Prints the PIDs of the processes magic-trace attaches to."
84+
|> debug_flag
85+
;;
86+
7887
module Null_writer : Trace_writer_intf.S_trace = struct
7988
type thread = unit
8089

@@ -314,6 +323,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
314323
(opts : Record_opts.t)
315324
~elf
316325
~debug_print_perf_commands
326+
~debug_print_pid
317327
~subcommand
318328
~collection_mode
319329
pids
@@ -350,6 +360,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
350360
Backend.Recording.attach_and_record
351361
opts.backend_opts
352362
~debug_print_perf_commands
363+
~debug_print_pid
353364
~subcommand
354365
~when_to_snapshot:opts.when_to_snapshot
355366
~trace_scope:opts.trace_scope
@@ -436,6 +447,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
436447
record_opts
437448
~elf
438449
~debug_print_perf_commands
450+
~debug_print_pid
439451
~prog
440452
~argv
441453
~collection_mode
@@ -447,6 +459,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
447459
record_opts
448460
~elf
449461
~debug_print_perf_commands
462+
~debug_print_pid
450463
~subcommand:Run
451464
~collection_mode
452465
[ pid ]
@@ -482,12 +495,20 @@ module Make_commands (Backend : Backend_intf.S) = struct
482495
return pid
483496
;;
484497

485-
let attach_and_record record_opts ~elf ~debug_print_perf_commands ~collection_mode pids =
498+
let attach_and_record
499+
record_opts
500+
~elf
501+
~debug_print_perf_commands
502+
~debug_print_pid
503+
~collection_mode
504+
pids
505+
=
486506
let%bind.Deferred.Or_error attachment =
487507
attach
488508
record_opts
489509
~elf
490510
~debug_print_perf_commands
511+
~debug_print_pid
491512
~subcommand:Attach
492513
~collection_mode
493514
pids
@@ -585,6 +606,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
585606
(let%map_open.Command record_opt_fn = record_flags
586607
and decode_opts = decode_flags
587608
and debug_print_perf_commands = debug_print_perf_commands
609+
and debug_print_pid = debug_print_pid
588610
and prog = anon ("COMMAND" %: string)
589611
and argv =
590612
flag "--" escape ~doc:"ARGS Arguments for the command. Ignored by magic-trace."
@@ -608,6 +630,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
608630
opts
609631
~elf
610632
~debug_print_perf_commands
633+
~debug_print_pid
611634
~prog
612635
~argv
613636
~collection_mode:opts.collection_mode
@@ -680,6 +703,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
680703
(let%map_open.Command record_opt_fn = record_flags
681704
and decode_opts = decode_flags
682705
and debug_print_perf_commands = debug_print_perf_commands
706+
and debug_print_pid = debug_print_pid
683707
and pids =
684708
flag
685709
"-pid"
@@ -719,6 +743,7 @@ module Make_commands (Backend : Backend_intf.S) = struct
719743
opts
720744
~elf
721745
~debug_print_perf_commands
746+
~debug_print_pid
722747
~collection_mode
723748
pids
724749
in

0 commit comments

Comments
 (0)