#![warn(missing_docs)]
#![warn(clippy::unwrap_used, clippy::panic, clippy::expect_used)]
pub mod debug_info;
pub mod function_die;
pub mod registers;
pub mod stack_frame;
pub mod stepping_mode;
pub mod unit_info;
pub mod variable;
pub mod variable_cache;
pub use self::{
debug_info::*, registers::*, stack_frame::StackFrame, variable::*,
variable_cache::VariableCache,
};
use crate::{core::Core, MemoryInterface};
use gimli::DebuggingInformationEntry;
use num_traits::Zero;
use std::{
io,
path::PathBuf,
str::Utf8Error,
sync::atomic::{AtomicI64, Ordering},
vec,
};
#[derive(Debug, thiserror::Error)]
pub enum DebugError {
#[error("IO Error while accessing debug data")]
Io(#[from] io::Error),
#[error("Error accessing debug data")]
DebugData(#[from] object::read::Error),
#[error("Error parsing debug data")]
Parse(#[from] gimli::read::Error),
#[error("Non-UTF8 data found in debug data")]
NonUtf8(#[from] Utf8Error),
#[error("Error using the probe")]
Probe(#[from] crate::Error),
#[error(transparent)]
CharConversion(#[from] std::char::CharTryFromError),
#[error(transparent)]
IntConversion(#[from] std::num::TryFromIntError),
#[error("{message} @program_counter={:#010X}.", pc_at_error)]
NoValidHaltLocation {
message: String,
pc_at_error: u64,
},
#[error(transparent)]
Other(#[from] anyhow::Error),
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ColumnType {
LeftEdge,
Column(u64),
}
impl From<gimli::ColumnType> for ColumnType {
fn from(column: gimli::ColumnType) -> Self {
match column {
gimli::ColumnType::LeftEdge => ColumnType::LeftEdge,
gimli::ColumnType::Column(c) => ColumnType::Column(c.get()),
}
}
}
static CACHE_KEY: AtomicI64 = AtomicI64::new(1);
pub fn get_sequential_key() -> i64 {
CACHE_KEY.fetch_add(1, Ordering::SeqCst)
}
#[derive(Clone, Debug, PartialEq)]
pub struct SourceLocation {
pub line: Option<u64>,
pub column: Option<ColumnType>,
pub file: Option<String>,
pub directory: Option<PathBuf>,
pub low_pc: Option<u32>,
pub high_pc: Option<u32>,
}
fn extract_file(
debug_info: &DebugInfo,
unit: &gimli::Unit<GimliReader>,
attribute_value: gimli::AttributeValue<GimliReader>,
) -> Option<(PathBuf, String)> {
match attribute_value {
gimli::AttributeValue::FileIndex(index) => unit.line_program.as_ref().and_then(|ilnp| {
let header = ilnp.header();
if let Some(file_entry) = header.file(index) {
if let Some((Some(path), Some(file))) = debug_info
.find_file_and_directory(unit, header, file_entry)
.map(|(file, path)| (path, file))
{
Some((path, file))
} else {
log::warn!("Unable to extract file or path from {:?}.", attribute_value);
None
}
} else {
log::warn!("Unable to extract file entry for {:?}.", attribute_value);
None
}
}),
other => {
log::warn!(
"Unable to extract file information from attribute value {:?}: Not implemented.",
other
);
None
}
}
}
fn extract_byte_size(
_debug_info: &DebugInfo,
di_entry: &DebuggingInformationEntry<GimliReader>,
) -> u64 {
match di_entry.attr(gimli::DW_AT_byte_size) {
Ok(optional_byte_size_attr) => match optional_byte_size_attr {
Some(byte_size_attr) => match byte_size_attr.value() {
gimli::AttributeValue::Udata(byte_size) => byte_size,
other => {
log::warn!("Unimplemented: DW_AT_byte_size value: {:?} ", other);
0
}
},
None => 0,
},
Err(error) => {
log::warn!(
"Failed to extract byte_size: {:?} for debug_entry {:?}",
error,
di_entry.tag().static_string()
);
0
}
}
}
fn extract_line(attribute_value: gimli::AttributeValue<GimliReader>) -> Option<u64> {
match attribute_value {
gimli::AttributeValue::Udata(line) => Some(line),
_ => None,
}
}
fn extract_name(
debug_info: &DebugInfo,
attribute_value: gimli::AttributeValue<GimliReader>,
) -> String {
match attribute_value {
gimli::AttributeValue::DebugStrRef(name_ref) => {
if let Ok(name_raw) = debug_info.dwarf.string(name_ref) {
String::from_utf8_lossy(&name_raw).to_string()
} else {
"Invalid DW_AT_name value".to_string()
}
}
gimli::AttributeValue::String(name) => String::from_utf8_lossy(&name).to_string(),
other => format!("Unimplemented: Evaluate name from {:?}", other),
}
}
#[allow(clippy::unwrap_used, clippy::expect_used)]
pub(crate) fn _print_all_attributes(
core: &mut Core<'_>,
stackframe_cfa: Option<u64>,
dwarf: &gimli::Dwarf<DwarfReader>,
unit: &gimli::Unit<DwarfReader>,
tag: &gimli::DebuggingInformationEntry<DwarfReader>,
print_depth: usize,
) {
let mut attrs = tag.attrs();
while let Some(attr) = attrs.next().unwrap() {
for _ in 0..(print_depth) {
print!("\t");
}
print!("{}: ", attr.name());
use gimli::AttributeValue::*;
match attr.value() {
Addr(a) => println!("{:#010x}", a),
DebugStrRef(_) => {
let val = dwarf.attr_string(unit, attr.value()).unwrap();
println!("{}", std::str::from_utf8(&val).unwrap());
}
Exprloc(e) => {
let mut evaluation = e.evaluation(unit.encoding());
let mut result = evaluation.evaluate().unwrap();
loop {
use gimli::EvaluationResult::*;
result = match result {
Complete => break,
RequiresMemory { address, size, .. } => {
let mut buff = vec![0u8; size as usize];
core.read(address, &mut buff)
.expect("Failed to read memory");
match size {
1 => evaluation
.resume_with_memory(gimli::Value::U8(buff[0]))
.unwrap(),
2 => {
let val = u16::from(buff[0]) << 8 | u16::from(buff[1]);
evaluation
.resume_with_memory(gimli::Value::U16(val))
.unwrap()
}
4 => {
let val = u32::from(buff[0]) << 24
| u32::from(buff[1]) << 16
| u32::from(buff[2]) << 8
| u32::from(buff[3]);
evaluation
.resume_with_memory(gimli::Value::U32(val))
.unwrap()
}
x => {
log::error!(
"Requested memory with size {}, which is not supported yet.",
x
);
unimplemented!();
}
}
}
RequiresFrameBase => evaluation
.resume_with_frame_base(stackframe_cfa.unwrap())
.unwrap(),
RequiresRegister {
register,
base_type,
} => {
let raw_value: u64 = core
.read_core_reg(register.0 as u16)
.expect("Failed to read memory");
if base_type != gimli::UnitOffset(0) {
unimplemented!(
"Support for units in RequiresRegister request is not yet implemented."
)
}
evaluation
.resume_with_register(gimli::Value::Generic(raw_value))
.unwrap()
}
RequiresRelocatedAddress(address_index) => {
if address_index.is_zero() {
evaluation.resume_with_relocated_address(u64::MAX).unwrap()
} else {
evaluation
.resume_with_relocated_address(address_index)
.unwrap()
}
}
x => {
println!("print_all_attributes {:?}", x);
todo!()
}
}
}
let result = evaluation.result();
println!("Expression: {:x?}", &result[0]);
}
LocationListsRef(_) => {
println!("LocationList");
}
DebugLocListsBase(_) => {
println!(" LocationList");
}
DebugLocListsIndex(_) => {
println!(" LocationList");
}
_ => {
println!("print_all_attributes {:?}", attr.value());
}
}
}
}