Skip to content

Commit

Permalink
Get thread activity from the OS
Browse files Browse the repository at this point in the history
Rather than use a heuristic to figure out if a thread is active, query the OS
to see if the thread is running. This should provide a more accurate view on
what's actually happening.

The tricky part here is matching the OS thread id to the python thread id,
which we were already doing for the native code unwinding. This refactors to
allow getting the native thread id even when not getting the native stack.

#92
  • Loading branch information
benfred committed Jun 30, 2019
1 parent 288189d commit b5c0ac3
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 249 deletions.
2 changes: 1 addition & 1 deletion remoteprocess/src/dylib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ macro_rules! dlsym {
static $name: self::dylib::Symbol<unsafe extern fn($($t),*) -> $ret> =
self::dylib::Symbol {
name: concat!(stringify!($name), "\0"),
addr: ::std::sync::atomic::ATOMIC_USIZE_INIT,
addr: ::std::sync::atomic::AtomicUsize::new(0),
_marker: ::std::marker::PhantomData,
};
)*)
Expand Down
8 changes: 4 additions & 4 deletions remoteprocess/src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::io::Read;
use std::os::unix::io::AsRawFd;
use std::fs::File;

#[cfg(unwind)]
use crate::dwarf_unwind::Registers;
use super::Error;

Expand All @@ -25,6 +24,7 @@ pub use self::libunwind::{LibUnwind};
use read_process_memory::{CopyAddress};

pub type Pid = pid_t;
pub type Tid = pid_t;

pub struct Process {
pub pid: Pid,
Expand Down Expand Up @@ -106,7 +106,7 @@ impl super::ProcessMemory for Process {
}

impl Thread {
pub fn new(threadid: i32) -> Thread{
pub fn new(threadid: i32) -> Thread {
Thread{tid: nix::unistd::Pid::from_raw(threadid)}
}

Expand All @@ -128,8 +128,8 @@ impl Thread {
}
}

pub fn id(&self) -> Result<u64, Error> {
Ok(self.tid.as_raw() as u64)
pub fn id(&self) -> Result<Tid, Error> {
Ok(self.tid.as_raw())
}

pub fn active(&self) -> Result<bool, Error> {
Expand Down
13 changes: 11 additions & 2 deletions remoteprocess/src/osx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use self::unwinder::Unwinder;
use libproc::libproc::proc_pid::{pidpath, pidinfo, PIDInfo, PidInfoFlavor};

pub type Pid = pid_t;
pub type Tid = u32;

pub struct Process {
pub pid: Pid,
Expand All @@ -35,7 +36,7 @@ pub struct Process {

#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct Thread {
pub tid: u32
pub tid: Tid
}

impl Process {
Expand Down Expand Up @@ -95,7 +96,15 @@ use self::mach_thread_bindings::{thread_info, thread_basic_info, thread_identifi


impl Thread {
pub fn id(&self) -> Result<u64, Error> {
pub fn new(tid: Tid) -> Thread {
Thread{tid}
}

pub fn id(&self) -> Result<Tid, Error> {
Ok(self.tid)
}

pub fn thread_handle(&self) -> Result<u64, Error> {
let thread_id = self.get_thread_identifier_info()?;
Ok(thread_id.thread_handle)
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ fn pyspy_main() -> Result<(), Error> {
}

fn main() {
env_logger::init();

if let Err(err) = pyspy_main() {
#[cfg(unix)]
{
Expand Down
Loading

0 comments on commit b5c0ac3

Please sign in to comment.