Skip to content

Commit

Permalink
Enable feature unwind by default
Browse files Browse the repository at this point in the history
Enable feature `unwind` by default, user can disable it by using:
`cargo build --no-default-features`

Signed-off-by: Jiang Liu <[email protected]>
  • Loading branch information
jiangliu committed Oct 15, 2024
1 parent 6ff51b6 commit 1f1ed01
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 23 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[features]
unwind = []

[package]
name = "py-spy"
version = "0.3.14"
Expand Down Expand Up @@ -38,7 +35,7 @@ serde_derive = "1.0"
serde_json = "1.0"
rand = "0.8"
rand_distr = "0.4"
remoteprocess = {version="0.4.12", features=["unwind"]}
remoteprocess = "0.4.12"
chrono = "0.4.26"

[dev-dependencies]
Expand All @@ -49,3 +46,7 @@ termios = "0.3.3"

[target.'cfg(windows)'.dependencies]
winapi = {version = "0.3", features = ["errhandlingapi", "winbase", "consoleapi", "wincon", "handleapi", "timeapi", "processenv" ]}

[features]
default = ["unwind"]
unwind = ["remoteprocess/unwind"]
2 changes: 1 addition & 1 deletion src/binary_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct BinaryInfo {
}

impl BinaryInfo {
#[cfg(feature = "unwind")]
#[cfg(all(feature = "unwind", target_os = "linux", target_arch = "x86_64"))]
pub fn contains(&self, addr: u64) -> bool {
addr >= self.addr && addr < (self.addr + self.size)
}
Expand Down
14 changes: 9 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl Config {
.help("PID of a running python program to spy on")
.takes_value(true);

#[cfg(feature = "unwind")]
#[cfg(all(feature = "unwind", target_os = "linux", target_arch = "x86_64"))]
let native = Arg::new("native")
.short('n')
.long("native")
Expand Down Expand Up @@ -328,11 +328,11 @@ impl Config {
);

// add native unwinding if appropriate
#[cfg(feature = "unwind")]
#[cfg(all(feature = "unwind", target_os = "linux", target_arch = "x86_64"))]
let record = record.arg(native.clone());
#[cfg(feature = "unwind")]
#[cfg(all(feature = "unwind", target_os = "linux", target_arch = "x86_64"))]
let top = top.arg(native.clone());
#[cfg(feature = "unwind")]
#[cfg(all(feature = "unwind", target_os = "linux", target_arch = "x86_64"))]
let dump = dump.arg(native.clone());

// Nonblocking isn't an option for freebsd, remove
Expand Down Expand Up @@ -429,7 +429,11 @@ impl Config {
.value_of("pid")
.map(|p| p.parse().expect("invalid pid"));
config.full_filenames = matches.occurrences_of("full_filenames") > 0;
if cfg!(feature = "unwind") {
if cfg!(all(
feature = "unwind",
target_os = "linux",
target_arch = "x86_64"
)) {
config.native = matches.occurrences_of("native") > 0;
}

Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ pub mod binary_parser;
pub mod config;
#[cfg(target_os = "linux")]
pub mod coredump;
#[cfg(feature = "unwind")]
mod cython;
pub mod dump;
#[cfg(feature = "unwind")]
#[cfg_attr(
not(all(target_os = "linux", target_arch = "x86_64")),
path = "native_stack_trace_stub.rs"
)]
mod native_stack_trace;
mod python_bindings;
mod python_data_access;
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ mod config;
mod console_viewer;
#[cfg(target_os = "linux")]
mod coredump;
#[cfg(feature = "unwind")]
mod cython;
mod dump;
mod flamegraph;
#[cfg(feature = "unwind")]
#[cfg_attr(
not(all(target_os = "linux", target_arch = "x86_64")),
path = "native_stack_trace_stub.rs"
)]
mod native_stack_trace;
mod python_bindings;
mod python_data_access;
Expand Down
4 changes: 3 additions & 1 deletion src/native_stack_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use lru::LruCache;
use remoteprocess::{self, Pid};

use crate::binary_parser::BinaryInfo;
use crate::cython;
use crate::stack_trace::Frame;
use crate::utils::resolve_filename;

#[path = "cython.rs"]
mod cython;

pub struct NativeStack {
should_reload: bool,
python: Option<BinaryInfo>,
Expand Down
25 changes: 25 additions & 0 deletions src/native_stack_trace_stub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anyhow::Error;
use remoteprocess::Pid;

use crate::binary_parser::BinaryInfo;
use crate::Frame;

pub struct NativeStack {}

impl NativeStack {
pub fn new(
_pid: Pid,
_python: Option<BinaryInfo>,
_libpython: Option<BinaryInfo>,
) -> Result<NativeStack, Error> {
Ok(NativeStack {})
}

pub fn merge_native_thread(
&mut self,
frames: &Vec<Frame>,
_thread: &remoteprocess::Thread,
) -> Result<Vec<Frame>, Error> {
Ok(frames.to_owned())
}
}
18 changes: 11 additions & 7 deletions src/python_spy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::collections::HashMap;
#[cfg(all(target_os = "linux", feature = "unwind"))]
use std::collections::HashSet;
#[cfg(all(target_os = "linux", feature = "unwind"))]
use std::iter::FromIterator;
use std::path::Path;

use anyhow::{Context, Error, Result};
Expand Down Expand Up @@ -385,8 +381,11 @@ impl PythonSpy {
}
Ok(None)
}
}

#[cfg(all(target_os = "linux", not(feature = "unwind")))]
#[cfg(target_os = "linux")]
impl PythonSpy {
#[cfg(not(all(feature = "unwind", target_arch = "x86_64")))]
fn _get_os_thread_id<I: InterpreterState>(
&mut self,
_python_thread_id: u64,
Expand All @@ -395,12 +394,15 @@ impl PythonSpy {
Ok(None)
}

#[cfg(all(target_os = "linux", feature = "unwind"))]
#[cfg(all(feature = "unwind", target_arch = "x86_64"))]
fn _get_os_thread_id<I: InterpreterState>(
&mut self,
python_thread_id: u64,
interp: &I,
) -> Result<Option<Tid>, Error> {
use std::collections::HashSet;
use std::iter::FromIterator;

// in nonblocking mode, we can't get the threadid reliably (method here requires reading the RBX
// register which requires a ptrace attach). fallback to heuristic thread activity here
if self.config.blocking == LockingStrategy::NonBlocking {
Expand Down Expand Up @@ -480,7 +482,7 @@ impl PythonSpy {
Ok(None)
}

#[cfg(all(target_os = "linux", feature = "unwind"))]
#[cfg(all(feature = "unwind", target_arch = "x86_64"))]
pub fn _get_pthread_id(
&self,
unwinder: &remoteprocess::Unwinder,
Expand All @@ -503,7 +505,9 @@ impl PythonSpy {

Ok(pthread_id)
}
}

impl PythonSpy {
#[cfg(target_os = "freebsd")]
fn _get_os_thread_id<I: InterpreterState>(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "unwind")]
#[cfg(all(feature = "unwind", target_os = "linux", target_arch = "x86_64"))]
pub fn resolve_filename(filename: &str, modulename: &str) -> Option<String> {
// check the filename first, if it exists use it
use std::path::Path;
Expand Down

0 comments on commit 1f1ed01

Please sign in to comment.