From acb7eb226b95f389caa42962ff7bbe58270c460e Mon Sep 17 00:00:00 2001 From: Dmitri Makarov Date: Thu, 15 Dec 2022 20:08:00 -0500 Subject: [PATCH] Add an rbpf-cli option to dump the program execution trace (#29289) --- rbpf-cli/src/main.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rbpf-cli/src/main.rs b/rbpf-cli/src/main.rs index f951eddab6..d32f799a96 100644 --- a/rbpf-cli/src/main.rs +++ b/rbpf-cli/src/main.rs @@ -157,6 +157,14 @@ before execting it in the virtual machine.", .takes_value(true) .possible_values(["json", "json-compact"]), ) + .arg( + Arg::new("trace") + .help("Output instruction trace") + .short('t') + .long("trace") + .takes_value(true) + .value_name("FILE"), + ) .get_matches(); let loader_id = bpf_loader::id(); @@ -297,6 +305,23 @@ before execting it in the virtual machine.", } let (instruction_count, result) = vm.execute_program(matches.value_of("use").unwrap() != "jit"); let duration = Instant::now() - start_time; + if matches.occurrences_of("trace") > 0 { + let trace_log = vm.env.context_object_pointer.trace_log.as_slice(); + if matches.value_of("trace").unwrap() == "stdout" { + analysis + .analyze() + .disassemble_trace_log(&mut std::io::stdout(), trace_log) + .unwrap(); + } else { + analysis + .analyze() + .disassemble_trace_log( + &mut File::create(matches.value_of("trace").unwrap()).unwrap(), + trace_log, + ) + .unwrap(); + } + } drop(vm); let output = Output {