From 8403aeefe41b2cba912157927a74916b49a71fcd Mon Sep 17 00:00:00 2001 From: Jack Roper Date: Mon, 7 Aug 2023 16:30:32 -0400 Subject: [PATCH 1/2] add json serialization for subprocesses --- src/dump.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/dump.rs b/src/dump.rs index 39624e6d..2883200c 100644 --- a/src/dump.rs +++ b/src/dump.rs @@ -7,10 +7,37 @@ use crate::stack_trace::StackTrace; use remoteprocess::Pid; +fn get_stack_traces_with_config(pid: Pid, config: &Config) -> Result, Error> { + let mut process = PythonSpy::new(pid, config)?; + let mut traces = process.get_stack_traces()?; + if config.subprocesses { + let sub_results: Result>, Error> = process + .process + .child_processes() + .expect("failed to get subprocesses") + .into_iter() + .filter_map(|(cpid, ppid)| { + // child_processes() returns the whole process tree, since we're recursing here + // though we could end up printing grandchild processes multiple times. Limit down + // to just once + if ppid == pid { + Some(get_stack_traces_with_config(cpid, config)) + } else { + None + } + }) + .collect(); + let mut subtraces = sub_results?.into_iter().flatten().collect(); + traces.append(&mut subtraces); + } + + Ok(traces) +} + pub fn print_traces(pid: Pid, config: &Config, parent: Option) -> Result<(), Error> { let mut process = PythonSpy::new(pid, config)?; if config.dump_json { - let traces = process.get_stack_traces()?; + let traces = get_stack_traces_with_config(pid, config)?; println!("{}", serde_json::to_string_pretty(&traces)?); return Ok(()); } From aec291c716d71312034951cdee08664dd1b59210 Mon Sep 17 00:00:00 2001 From: Jack Roper Date: Mon, 7 Aug 2023 16:33:40 -0400 Subject: [PATCH 2/2] rename var --- src/dump.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dump.rs b/src/dump.rs index 2883200c..e854c87a 100644 --- a/src/dump.rs +++ b/src/dump.rs @@ -11,7 +11,7 @@ fn get_stack_traces_with_config(pid: Pid, config: &Config) -> Result>, Error> = process + let unflattened: Result>, Error> = process .process .child_processes() .expect("failed to get subprocesses") @@ -27,7 +27,7 @@ fn get_stack_traces_with_config(pid: Pid, config: &Config) -> Result