From 5dfcfa1e639062b23152a066c22f395b5c779e9d Mon Sep 17 00:00:00 2001 From: Ben Frederickson Date: Thu, 31 Oct 2024 22:39:28 -0700 Subject: [PATCH] limit debug messages --- src/python_process_info.rs | 81 +++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/src/python_process_info.rs b/src/python_process_info.rs index 052f1ab6..aae0ac42 100644 --- a/src/python_process_info.rs +++ b/src/python_process_info.rs @@ -358,7 +358,13 @@ where )?; // Make sure the interpreter addr is valid before returning - match check_interpreter_addresses(&[addr], &*python_info.maps, process, version) { + match check_interpreter_addresses( + &[addr], + &*python_info.maps, + process, + version, + true, + ) { Ok(addr) => return Ok(addr), Err(_) => { warn!( @@ -379,7 +385,13 @@ where .copy_struct(addr as usize + pyruntime::get_interp_head_offset(version))?; // Make sure the interpreter addr is valid before returning - match check_interpreter_addresses(&[addr], &*python_info.maps, process, version) { + match check_interpreter_addresses( + &[addr], + &*python_info.maps, + process, + version, + true, + ) { Ok(addr) => return Ok(addr), Err(_) => { warn!( @@ -393,7 +405,13 @@ where _ => { if let Some(&addr) = python_info.get_symbol("interp_head") { let addr = process.copy_struct(addr as usize)?; - match check_interpreter_addresses(&[addr], &*python_info.maps, process, version) { + match check_interpreter_addresses( + &[addr], + &*python_info.maps, + process, + version, + true, + ) { Ok(addr) => return Ok(addr), Err(_) => { warn!( @@ -451,7 +469,7 @@ where let addrs = unsafe { slice::from_raw_parts(bss.as_ptr() as *const usize, bss.len() / size_of::()) }; - if let Ok(addr) = check_interpreter_addresses(addrs, maps, process, version) { + if let Ok(addr) = check_interpreter_addresses(addrs, maps, process, version, true) { return Ok(addr); } info!("failed to get interpreter address from pyruntime section"); @@ -465,7 +483,7 @@ where let addrs = unsafe { slice::from_raw_parts(bss.as_ptr() as *const usize, bss.len() / size_of::()) }; - check_interpreter_addresses(addrs, maps, process, version) + check_interpreter_addresses(addrs, maps, process, version, false) } // Checks whether a block of memory (from BSS/.data etc) contains pointers that are pointing @@ -475,39 +493,56 @@ fn check_interpreter_addresses

( maps: &dyn ContainsAddr, process: &P, version: &Version, + show_progress: bool, ) -> Result where P: ProcessMemory, { // This function does all the work, but needs a type of the interpreter - fn check(addrs: &[usize], maps: &dyn ContainsAddr, process: &P) -> Result + fn check( + addrs: &[usize], + maps: &dyn ContainsAddr, + process: &P, + show_progress: bool, + ) -> Result where I: InterpreterState, P: ProcessMemory, { for &addr in addrs { - info!("checking addr {:016x}", addr); + if show_progress { + info!("checking addr {:016x}", addr); + } if maps.contains_addr(addr) { - info!(" -> contains addr"); + if show_progress { + info!(" -> contains addr"); + } // this address points to valid memory. try loading it up as a PyInterpreterState // to further check let interp: I = match process.copy_struct(addr) { Ok(interp) => interp, Err(_) => continue, }; + if show_progress { + info!(" -> copied interp"); + } // get the pythreadstate pointer from the interpreter object, and if it is also // a valid pointer then load it up. let threads = interp.head(); if maps.contains_addr(threads as usize) { - info!(" -> contains threads"); + if show_progress { + info!(" -> contains threads"); + } // If the threadstate points back to the interpreter like we expect, then // this is almost certainly the address of the intrepreter let thread = match process.copy_pointer(threads) { Ok(thread) => thread, Err(_) => continue, }; - info!(" -> pointer matches {}", thread.interp() as usize == addr); + if show_progress { + info!(" -> pointer matches {}", thread.interp() as usize == addr); + } // as a final sanity check, try getting the stack_traces, and only return if this works if thread.interp() as usize == addr && get_stack_traces(&interp, process, 0, None).is_ok() @@ -528,56 +563,56 @@ where major: 2, minor: 3..=7, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 3, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 4..=5, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 6, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 7, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 8, patch: 0, .. } => match version.release_flags.as_ref() { - "a1" | "a2" | "a3" => check::(addrs, maps, process), - _ => check::(addrs, maps, process), + "a1" | "a2" | "a3" => check::(addrs, maps, process, show_progress), + _ => check::(addrs, maps, process, show_progress), }, Version { major: 3, minor: 8, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 9, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 10, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 11, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 12, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), Version { major: 3, minor: 13, .. - } => check::(addrs, maps, process), + } => check::(addrs, maps, process, show_progress), _ => Err(format_err!("Unsupported version of Python: {}", version)), } }