Skip to content

Commit

Permalink
limit debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
benfred committed Nov 1, 2024
1 parent 1553396 commit 5dfcfa1
Showing 1 changed file with 58 additions and 23 deletions.
81 changes: 58 additions & 23 deletions src/python_process_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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!(
Expand All @@ -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!(
Expand Down Expand Up @@ -451,7 +469,7 @@ where
let addrs = unsafe {
slice::from_raw_parts(bss.as_ptr() as *const usize, bss.len() / size_of::<usize>())
};
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");
Expand All @@ -465,7 +483,7 @@ where
let addrs = unsafe {
slice::from_raw_parts(bss.as_ptr() as *const usize, bss.len() / size_of::<usize>())
};
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
Expand All @@ -475,39 +493,56 @@ fn check_interpreter_addresses<P>(
maps: &dyn ContainsAddr,
process: &P,
version: &Version,
show_progress: bool,
) -> Result<usize, Error>
where
P: ProcessMemory,
{
// This function does all the work, but needs a type of the interpreter
fn check<I, P>(addrs: &[usize], maps: &dyn ContainsAddr, process: &P) -> Result<usize, Error>
fn check<I, P>(
addrs: &[usize],
maps: &dyn ContainsAddr,
process: &P,
show_progress: bool,
) -> Result<usize, Error>
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()
Expand All @@ -528,56 +563,56 @@ where
major: 2,
minor: 3..=7,
..
} => check::<v2_7_15::_is, P>(addrs, maps, process),
} => check::<v2_7_15::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3, minor: 3, ..
} => check::<v3_3_7::_is, P>(addrs, maps, process),
} => check::<v3_3_7::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3,
minor: 4..=5,
..
} => check::<v3_5_5::_is, P>(addrs, maps, process),
} => check::<v3_5_5::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3, minor: 6, ..
} => check::<v3_6_6::_is, P>(addrs, maps, process),
} => check::<v3_6_6::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3, minor: 7, ..
} => check::<v3_7_0::_is, P>(addrs, maps, process),
} => check::<v3_7_0::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3,
minor: 8,
patch: 0,
..
} => match version.release_flags.as_ref() {
"a1" | "a2" | "a3" => check::<v3_7_0::_is, P>(addrs, maps, process),
_ => check::<v3_8_0::_is, P>(addrs, maps, process),
"a1" | "a2" | "a3" => check::<v3_7_0::_is, P>(addrs, maps, process, show_progress),
_ => check::<v3_8_0::_is, P>(addrs, maps, process, show_progress),
},
Version {
major: 3, minor: 8, ..
} => check::<v3_8_0::_is, P>(addrs, maps, process),
} => check::<v3_8_0::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3, minor: 9, ..
} => check::<v3_9_5::_is, P>(addrs, maps, process),
} => check::<v3_9_5::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3,
minor: 10,
..
} => check::<v3_10_0::_is, P>(addrs, maps, process),
} => check::<v3_10_0::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3,
minor: 11,
..
} => check::<v3_11_0::_is, P>(addrs, maps, process),
} => check::<v3_11_0::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3,
minor: 12,
..
} => check::<v3_12_0::_is, P>(addrs, maps, process),
} => check::<v3_12_0::_is, P>(addrs, maps, process, show_progress),
Version {
major: 3,
minor: 13,
..
} => check::<v3_13_0::_is, P>(addrs, maps, process),
} => check::<v3_13_0::_is, P>(addrs, maps, process, show_progress),
_ => Err(format_err!("Unsupported version of Python: {}", version)),
}
}
Expand Down

0 comments on commit 5dfcfa1

Please sign in to comment.