Skip to content

Commit 9c7c2e1

Browse files
authored
Merge pull request #425 from tmm1/mac-child-procs
Profile child processes on macOS
2 parents 36646f1 + bd8308c commit 9c7c2e1

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

Cargo.lock

Lines changed: 74 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samply/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ lazy_static = "1.5.0"
6565
flate2 = "1.0"
6666
sysctl = "0.6.0"
6767
plist = "1.7.0"
68+
libproc = "0.14.10"
6869

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

samply/src/mac/process_launcher.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::sync::Arc;
99
use std::time::Duration;
1010

1111
use flate2::write::GzDecoder;
12+
use libproc::processes;
1213
use mach::task::{task_resume, task_suspend};
1314
use mach::traps::task_for_pid;
1415
use tempfile::tempdir;
@@ -295,6 +296,23 @@ impl RootTaskRunner for ExistingProcessRunner {
295296
}
296297

297298
impl ExistingProcessRunner {
299+
fn get_all_descendant_pids(pid: u32) -> Vec<u32> {
300+
let mut descendants = Vec::new();
301+
let mut queue = vec![pid];
302+
303+
while let Some(current_pid) = queue.pop() {
304+
let filter = processes::ProcFilter::ByParentProcess { ppid: current_pid };
305+
if let Ok(child_pids) = processes::pids_by_type(filter) {
306+
for child_pid in child_pids {
307+
descendants.push(child_pid);
308+
queue.push(child_pid);
309+
}
310+
}
311+
}
312+
313+
descendants
314+
}
315+
298316
pub fn new(pid: u32, task_accepter: &mut TaskAccepter) -> ExistingProcessRunner {
299317
let mut queue_pid = |pid, failure_is_ok| {
300318
let task = unsafe {
@@ -326,7 +344,11 @@ impl ExistingProcessRunner {
326344
// always root pid first
327345
queue_pid(pid, false);
328346

329-
// TODO: find all its children
347+
// find all its descendants recursively
348+
let descendant_pids = Self::get_all_descendant_pids(pid);
349+
for pid in descendant_pids {
350+
queue_pid(pid, true);
351+
}
330352

331353
ExistingProcessRunner {
332354
pid,

0 commit comments

Comments
 (0)