Skip to content

Commit

Permalink
Profile child processes on macOS
Browse files Browse the repository at this point in the history
closes #419
  • Loading branch information
tmm1 committed Nov 18, 2024
1 parent 36646f1 commit bd8308c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
76 changes: 74 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions samply/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ lazy_static = "1.5.0"
flate2 = "1.0"
sysctl = "0.6.0"
plist = "1.7.0"
libproc = "0.14.10"

[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]

Expand Down
24 changes: 23 additions & 1 deletion samply/src/mac/process_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::Arc;
use std::time::Duration;

use flate2::write::GzDecoder;
use libproc::processes;
use mach::task::{task_resume, task_suspend};
use mach::traps::task_for_pid;
use tempfile::tempdir;
Expand Down Expand Up @@ -295,6 +296,23 @@ impl RootTaskRunner for ExistingProcessRunner {
}

impl ExistingProcessRunner {
fn get_all_descendant_pids(pid: u32) -> Vec<u32> {
let mut descendants = Vec::new();
let mut queue = vec![pid];

while let Some(current_pid) = queue.pop() {
let filter = processes::ProcFilter::ByParentProcess { ppid: current_pid };
if let Ok(child_pids) = processes::pids_by_type(filter) {
for child_pid in child_pids {
descendants.push(child_pid);
queue.push(child_pid);
}
}
}

descendants
}

pub fn new(pid: u32, task_accepter: &mut TaskAccepter) -> ExistingProcessRunner {
let mut queue_pid = |pid, failure_is_ok| {
let task = unsafe {
Expand Down Expand Up @@ -326,7 +344,11 @@ impl ExistingProcessRunner {
// always root pid first
queue_pid(pid, false);

// TODO: find all its children
// find all its descendants recursively
let descendant_pids = Self::get_all_descendant_pids(pid);
for pid in descendant_pids {
queue_pid(pid, true);
}

ExistingProcessRunner {
pid,
Expand Down

0 comments on commit bd8308c

Please sign in to comment.