dump_traces/
dump_traces.rs

1use log::error;
2
3// Simple example of showing how to use the rust API to
4// print out stack traces from a python program
5
6fn print_python_stacks(pid: remoteprocess::Pid) -> Result<(), anyhow::Error> {
7    // Create a new PythonSpy object with the default config options
8    let config = py_spy_for_datakit::Config::default();
9    let mut process = py_spy_for_datakit::PythonSpy::new(pid, &config)?;
10
11    // get stack traces for each thread in the process
12    let traces = process.get_stack_traces()?;
13
14    // Print out the python stack for each thread
15    for trace in traces {
16        println!("Thread {:#X} ({})", trace.thread_id, trace.status_str());
17        for frame in &trace.frames {
18            println!("\t {} ({}:{})", frame.name, frame.filename, frame.line);
19        }
20    }
21    Ok(())
22}
23
24fn main() {
25    env_logger::init();
26    let args: Vec<String> = std::env::args().collect();
27    let pid = if args.len() > 1 {
28        args[1].parse().expect("invalid pid")
29    } else {
30        error!("you must specify a pid!");
31        return;
32    };
33
34    if let Err(e) = print_python_stacks(pid) {
35        error!("failed to print stack traces: {:?}", e);
36    }
37}