Skip to content

Commit

Permalink
add stack
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Fricke <[email protected]>
  • Loading branch information
krfricke committed Nov 16, 2023
1 parent 327683d commit 790538c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ impl Config {
.takes_value(true);

#[cfg(unwind)]
let native = Arg::new("native")
let native = Arg::new("native")
.short('n')
.long("native")
.help("Collect stack traces from native extensions written in Cython, C or C++");

#[cfg(unwind)]
let native_all = Arg::new("native-all")
let native_all = Arg::new("native-all")
.short('N')
.long("native-all")
.help("Collect stack traces from native-only threads. Implies `--native`.");
Expand Down
42 changes: 38 additions & 4 deletions src/native_stack_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::num::NonZeroUsize;
use cpp_demangle::{BorrowedSymbol, DemangleOptions};
use lazy_static::lazy_static;
use lru::LruCache;
use remoteprocess::{self, Pid};
use remoteprocess::{self, Pid, Tid};

use crate::binary_parser::BinaryInfo;
use crate::cython;
use crate::stack_trace::Frame;
use crate::stack_trace::{Frame, StackTrace};
use crate::utils::resolve_filename;

pub struct NativeStack {
Expand Down Expand Up @@ -242,8 +242,42 @@ impl NativeStack {
}
}

pub fn add_native_only_threads(&self, process: &remoteprocess::Process, traces: &Vec<&remoteprocess::StackFrame>) {
todo!()
pub fn add_native_only_threads(
&mut self,
process: &remoteprocess::Process,
traces: &mut Vec<StackTrace>,
) -> Result<(), Error> {
// Set of all threads we already processed
let seen_threads =
HashSet::<Tid>::from_iter(traces.iter().map(|t| t.os_thread_id.unwrap_or(0) as Tid));

for native_thread in process.threads()?.into_iter() {
let tid = native_thread.id()?;

if seen_threads.contains(&tid) {
// We've already seen this thread, don't add it again
continue;
}

// We are reusing the `merge_native_stack` method and just pass an
// empty python stack.
let native_stack = self.get_thread(&native_thread)?;
let python_stack = Vec::new();
let symbolized_stack = self.merge_native_stack(&python_stack, native_stack)?;

// Push new stack trace
traces.push(StackTrace {
pid: process.pid,
thread_id: tid.try_into().unwrap_or(0),
thread_name: None,
os_thread_id: tid.try_into().ok(),
active: native_thread.active().unwrap_or(false),
owns_gil: false,
frames: symbolized_stack,
process_info: None,
});
}
Ok(())
}

/// translates a native frame into a optional frame. none indicates we should ignore this frame
Expand Down
2 changes: 1 addition & 1 deletion src/python_spy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl PythonSpy {
#[cfg(unwind)]
if self.config.native_all {
if let Some(native) = self.native.as_mut() {
native.add_native_only_threads(&self.process, &traces);
native.add_native_only_threads(&self.process, &mut traces)?;
}
}

Expand Down

0 comments on commit 790538c

Please sign in to comment.