sp1_cli/commands/
trace.rs

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
//! RISC-V tracer for SP1 traces. This tool can be used to analyze function call graphs and
//! instruction counts from a trace file from SP1 execution by setting the `TRACE_FILE` env
//! variable.
//
// Adapted from Sovereign's RISC-V tracer tool: https://github.com/Sovereign-Labs/riscv-cycle-tracer.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Modified by Succinct Labs on July 25, 2024.

use anyhow::Result;
use clap::Parser;
use goblin::elf::{sym::STT_FUNC, Elf};
use indicatif::{ProgressBar, ProgressStyle};
use prettytable::{format, Cell, Row, Table};
use regex::Regex;
use rustc_demangle::demangle;
use std::{
    cmp::Ordering,
    collections::HashMap,
    io::Read,
    process::Command,
    str,
    sync::{atomic::AtomicBool, Arc},
};
use textwrap::wrap;

#[derive(Parser, Debug)]
#[command(name = "trace", about = "Trace a program execution and analyze cycle counts.")]
pub struct TraceCmd {
    /// Include the "top" number of functions.
    #[arg(short, long, default_value_t = 30)]
    top: usize,

    /// Don't print stack aware instruction counts
    #[arg(long)]
    no_stack_counts: bool,

    /// Don't print raw (stack un-aware) instruction counts.
    #[arg(long)]
    no_raw_counts: bool,

    /// Path to the ELF.
    #[arg(long, required = true)]
    elf: String,

    /// Path to the trace file. Simply run the program with `TRACE_FILE=trace.log` environment
    /// variable. File must be one u64 program counter per line
    #[arg(long, required = true)]
    trace: String,

    /// Strip the hashes from the function name while printing.
    #[arg(short, long)]
    keep_hashes: bool,

    /// Function name to target for getting stack counts.
    #[arg(short, long)]
    function_name: Option<String>,

    /// Exclude functions matching these patterns from display.
    ///
    /// Usage: `-e func1 -e func2 -e func3`.
    #[arg(short, long)]
    exclude_view: Vec<String>,
}

fn strip_hash(name_with_hash: &str) -> String {
    let re = Regex::new(r"::h[0-9a-fA-F]{16}").unwrap();
    let mut result = re.replace(name_with_hash, "").to_string();
    let re2 = Regex::new(r"^<(.+) as .+>").unwrap();
    result = re2.replace(&result, "$1").to_string();
    let re2 = Regex::new(r"^<(.+) as .+>").unwrap();
    result = re2.replace(&result, "$1").to_string();
    let re2 = Regex::new(r"([^\:])<.+>::").unwrap();
    result = re2.replace_all(&result, "$1::").to_string();
    result
}

fn print_instruction_counts(
    first_header: &str,
    count_vec: Vec<(String, usize)>,
    top_n: usize,
    strip_hashes: bool,
    exclude_list: Option<&[String]>,
) {
    let mut table = Table::new();
    table.set_format(*format::consts::FORMAT_NO_LINESEP);
    table.set_titles(Row::new(vec![Cell::new(first_header), Cell::new("Instruction Count")]));

    let wrap_width = 120;
    let mut row_count = 0;
    for (key, value) in count_vec {
        let mut cont = false;
        if let Some(ev) = exclude_list {
            for e in ev {
                if key.contains(e) {
                    cont = true;
                    break;
                }
            }
            if cont {
                continue;
            }
        }
        let mut stripped_key = key.clone();
        if strip_hashes {
            stripped_key = strip_hash(&key);
        }
        row_count += 1;
        if row_count > top_n {
            break;
        }
        let wrapped_key = wrap(&stripped_key, wrap_width);
        let key_cell_content = wrapped_key.join("\n");
        table.add_row(Row::new(vec![Cell::new(&key_cell_content), Cell::new(&value.to_string())]));
    }

    table.printstd();
}

fn focused_stack_counts(
    function_stack: &[String],
    filtered_stack_counts: &mut HashMap<Vec<String>, usize>,
    function_name: &str,
    num_instructions: usize,
) {
    if let Some(index) = function_stack.iter().position(|s| s == function_name) {
        let truncated_stack = &function_stack[0..=index];
        let count = filtered_stack_counts.entry(truncated_stack.to_vec()).or_insert(0);
        *count += num_instructions;
    }
}

fn _build_radare2_lookups(
    start_lookup: &mut HashMap<u64, String>,
    end_lookup: &mut HashMap<u64, String>,
    func_range_lookup: &mut HashMap<String, (u64, u64)>,
    elf_name: &str,
) -> std::io::Result<()> {
    let output = Command::new("r2").arg("-q").arg("-c").arg("aa;afl").arg(elf_name).output()?;

    if output.status.success() {
        let result_str = str::from_utf8(&output.stdout).unwrap();
        for line in result_str.lines() {
            let parts: Vec<&str> = line.split_whitespace().collect();
            let address = u64::from_str_radix(&parts[0][2..], 16).unwrap();
            let size = parts[2].parse::<u64>().unwrap();
            let end_address = address + size - 4;
            let function_name = parts[3];
            start_lookup.insert(address, function_name.to_string());
            end_lookup.insert(end_address, function_name.to_string());
            func_range_lookup.insert(function_name.to_string(), (address, end_address));
        }
    } else {
        eprintln!("Error executing command: {}", str::from_utf8(&output.stderr).unwrap());
    }
    Ok(())
}

fn build_goblin_lookups(
    start_lookup: &mut HashMap<u64, String>,
    end_lookup: &mut HashMap<u64, String>,
    func_range_lookup: &mut HashMap<String, (u64, u64)>,
    elf_name: &str,
) -> std::io::Result<()> {
    let buffer = std::fs::read(elf_name).unwrap();
    let elf = Elf::parse(&buffer).unwrap();

    for sym in &elf.syms {
        if sym.st_type() == STT_FUNC {
            let name = elf.strtab.get_at(sym.st_name).unwrap_or("");
            let demangled_name = demangle(name);
            let size = sym.st_size;
            let start_address = sym.st_value;
            let end_address = start_address + size - 4;
            start_lookup.insert(start_address, demangled_name.to_string());
            end_lookup.insert(end_address, demangled_name.to_string());
            func_range_lookup.insert(demangled_name.to_string(), (start_address, end_address));
        }
    }
    Ok(())
}

fn increment_stack_counts(
    instruction_counts: &mut HashMap<String, usize>,
    function_stack: &[String],
    filtered_stack_counts: &mut HashMap<Vec<String>, usize>,
    function_name: &Option<String>,
    num_instructions: usize,
) {
    for f in function_stack {
        *instruction_counts.entry(f.clone()).or_insert(0) += num_instructions;
    }
    if let Some(f) = function_name {
        focused_stack_counts(function_stack, filtered_stack_counts, f, num_instructions)
    }
}

impl TraceCmd {
    pub fn run(&self) -> Result<()> {
        let top_n = self.top;
        let elf_path = self.elf.clone();
        let trace_path = self.trace.clone();
        let no_stack_counts = self.no_stack_counts;
        let no_raw_counts = self.no_raw_counts;
        let strip_hashes = !self.keep_hashes;
        let function_name = self.function_name.clone();
        let exclude_view = self.exclude_view.clone();

        let mut start_lookup = HashMap::new();
        let mut end_lookup = HashMap::new();
        let mut func_range_lookup = HashMap::new();
        build_goblin_lookups(&mut start_lookup, &mut end_lookup, &mut func_range_lookup, &elf_path)
            .unwrap();

        let mut function_ranges: Vec<(u64, u64, String)> =
            func_range_lookup.iter().map(|(f, &(start, end))| (start, end, f.clone())).collect();

        function_ranges.sort_by_key(|&(start, _, _)| start);

        let file = std::fs::File::open(trace_path).unwrap();
        let file_size = file.metadata().unwrap().len();
        let mut buf = std::io::BufReader::new(file);
        let mut function_stack: Vec<String> = Vec::new();
        let mut instruction_counts: HashMap<String, usize> = HashMap::new();
        let mut counts_without_callgraph: HashMap<String, usize> = HashMap::new();
        let mut filtered_stack_counts: HashMap<Vec<String>, usize> = HashMap::new();
        let total_lines = file_size / 4;
        let mut current_function_range: (u64, u64) = (0, 0);

        let update_interval = 1000usize;
        let pb = ProgressBar::new(total_lines);
        pb.set_style(
            ProgressStyle::default_bar()
                .template(
                    "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta})",
                )
                .unwrap()
                .progress_chars("#>-"),
        );

        let running = Arc::new(AtomicBool::new(true));
        let r = running.clone();

        ctrlc::set_handler(move || {
            r.store(false, std::sync::atomic::Ordering::SeqCst);
        })
        .expect("Error setting Ctrl-C handler");

        for c in 0..total_lines {
            if (c as usize) % update_interval == 0 {
                pb.inc(update_interval as u64);
                if !running.load(std::sync::atomic::Ordering::SeqCst) {
                    pb.finish_with_message("Interrupted");
                    break;
                }
            }

            // Parse pc from hex.
            let mut pc_bytes = [0u8; 4];
            buf.read_exact(&mut pc_bytes).unwrap();
            let pc = u32::from_be_bytes(pc_bytes) as u64;

            // Only 1 instruction per opcode.
            let num_instructions = 1;

            // Raw counts without considering the callgraph at all we're just checking if the PC
            // belongs to a function if so we're incrementing. This would ignore the call stack
            // so for example "main" would only have a hundred instructions or so.
            if let Ok(index) = function_ranges.binary_search_by(|&(start, end, _)| {
                if pc < start {
                    Ordering::Greater
                } else if pc > end {
                    Ordering::Less
                } else {
                    Ordering::Equal
                }
            }) {
                let (_, _, fname) = &function_ranges[index];
                *counts_without_callgraph.entry(fname.clone()).or_insert(0) += num_instructions
            } else {
                *counts_without_callgraph.entry("anonymous".to_string()).or_insert(0) +=
                    num_instructions;
            }

            // The next section considers the callstack. We build a callstack and maintain it based
            // on some rules. Functions lower in the stack get their counts incremented.

            // We are still in the current function.
            if pc > current_function_range.0 && pc <= current_function_range.1 {
                increment_stack_counts(
                    &mut instruction_counts,
                    &function_stack,
                    &mut filtered_stack_counts,
                    &function_name,
                    num_instructions,
                );
                continue;
            }

            // Jump to a new function (or the same one).
            if let Some(f) = start_lookup.get(&pc) {
                increment_stack_counts(
                    &mut instruction_counts,
                    &function_stack,
                    &mut filtered_stack_counts,
                    &function_name,
                    num_instructions,
                );

                // Jump to a new function (not recursive).
                if !function_stack.contains(f) {
                    function_stack.push(f.clone());
                    current_function_range = *func_range_lookup.get(f).unwrap();
                }
            } else {
                // This means pc now points to an instruction that is
                //
                // 1. not in the current function's range
                // 2. not a new function call
                //
                // We now account for a new possibility where we're returning to a function in the
                // stack this need not be the immediate parent and can be any of the existing
                // functions in the stack due to some optimizations that the compiler can make.
                let mut unwind_point = 0;
                let mut unwind_found = false;
                for (c, f) in function_stack.iter().enumerate() {
                    let (s, e) = func_range_lookup.get(f).unwrap();
                    if pc > *s && pc <= *e {
                        unwind_point = c;
                        unwind_found = true;
                        break;
                    }
                }

                // Unwinding until the parent.
                if unwind_found {
                    function_stack.truncate(unwind_point + 1);
                    increment_stack_counts(
                        &mut instruction_counts,
                        &function_stack,
                        &mut filtered_stack_counts,
                        &function_name,
                        num_instructions,
                    );
                    continue;
                }

                // If no unwind point has been found, that means we jumped to some random location
                // so we'll just increment the counts for everything in the stack.
                increment_stack_counts(
                    &mut instruction_counts,
                    &function_stack,
                    &mut filtered_stack_counts,
                    &function_name,
                    num_instructions,
                );
            }
        }

        pb.finish_with_message("done");

        let mut raw_counts: Vec<(String, usize)> =
            instruction_counts.iter().map(|(key, value)| (key.clone(), *value)).collect();
        raw_counts.sort_by(|a, b| b.1.cmp(&a.1));

        println!("\n\nTotal instructions in trace: {}", total_lines);
        if !no_stack_counts {
            println!("\n\n Instruction counts considering call graph");
            print_instruction_counts(
                "Function Name",
                raw_counts,
                top_n,
                strip_hashes,
                Some(&exclude_view),
            );
        }

        let mut raw_counts: Vec<(String, usize)> =
            counts_without_callgraph.iter().map(|(key, value)| (key.clone(), *value)).collect();
        raw_counts.sort_by(|a, b| b.1.cmp(&a.1));
        if !no_raw_counts {
            println!("\n\n Instruction counts ignoring call graph");
            print_instruction_counts(
                "Function Name",
                raw_counts,
                top_n,
                strip_hashes,
                Some(&exclude_view),
            );
        }

        let mut raw_counts: Vec<(String, usize)> = filtered_stack_counts
            .iter()
            .map(|(stack, count)| {
                let numbered_stack = stack
                    .iter()
                    .rev()
                    .enumerate()
                    .map(|(index, line)| {
                        let modified_line =
                            if strip_hashes { strip_hash(line) } else { line.clone() };
                        format!("({}) {}", index + 1, modified_line)
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                (numbered_stack, *count)
            })
            .collect();

        raw_counts.sort_by(|a, b| b.1.cmp(&a.1));
        if let Some(f) = function_name {
            println!("\n\n Stack patterns for function '{f}' ");
            print_instruction_counts("Function Stack", raw_counts, top_n, strip_hashes, None);
        }
        Ok(())
    }
}