1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
use anyhow::Result;
use gimli::{DebuggingInformationEntry, Dwarf, Reader, Unit};
/// Contains all the information about where the code was declared in the source code.
#[derive(Debug, Clone)]
pub struct SourceInformation {
/// The source code directory where the debug information was declared.
pub directory: Option<String>,
/// The relative source code file path where the debug information was declared.
pub file: Option<String>,
/// The source code line number where the debug information was declared.
pub line: Option<u64>,
/// The source code column number where the debug information was declared.
pub column: Option<u64>,
}
impl SourceInformation {
/// Retrieves the information about where the given DIE was declared in the source code.
///
/// Description:
///
/// * `dwarf` - A reference to gimli-rs `Dwarf` struct.
/// * `unit` - A reference to gimli-rs `Unit` struct, which the given DIE is located in.
/// * `die` - A reference to the DIE containing attributes starting with `DW_AT_decl_`.
/// * `cwd` - The work directory of the debugged program.
///
///This function will retrieve the information stored in the attributes starting with
///`DW_AT_decl_` from the given DIE>
pub fn get_die_source_information<R: Reader<Offset = usize>>(
dwarf: &Dwarf<R>,
unit: &Unit<R>,
die: &DebuggingInformationEntry<'_, '_, R>,
cwd: &str,
) -> Result<SourceInformation> {
let (file, directory) = match die.attr_value(gimli::DW_AT_decl_file)? {
Some(gimli::AttributeValue::FileIndex(v)) => match &unit.line_program {
Some(lp) => {
let header = lp.header();
match header.file(v) {
Some(file_entry) => {
let (file, directory) = match file_entry.directory(header) {
Some(dir_av) => {
let mut dir_raw =
dwarf.attr_string(&unit, dir_av)?.to_string()?.to_string();
let file_raw = dwarf
.attr_string(&unit, file_entry.path_name())?
.to_string()?
.to_string();
let file = file_raw.trim_start_matches(&dir_raw).to_string();
if !dir_raw.starts_with("/") {
dir_raw = format!("{}/{}", cwd, dir_raw);
}
(file, Some(dir_raw))
}
None => (
dwarf
.attr_string(&unit, file_entry.path_name())?
.to_string()?
.to_string(),
None,
),
};
(Some(file), directory)
}
None => (None, None),
}
}
None => (None, None),
},
None => (None, None),
Some(v) => unimplemented!("{:?}", v),
};
let line = match die.attr_value(gimli::DW_AT_decl_line)? {
Some(gimli::AttributeValue::Udata(v)) => Some(v),
None => None,
Some(v) => unimplemented!("{:?}", v),
};
let column = match die.attr_value(gimli::DW_AT_decl_column)? {
Some(gimli::AttributeValue::Udata(v)) => Some(v),
None => None,
Some(v) => unimplemented!("{:?}", v),
};
Ok(SourceInformation {
directory,
file,
line,
column,
})
}
}
/// Find the machine code address that corresponds to a line in the source file.
///
/// Description:
///
/// * `dwarf` - A reference to gimli-rs `Dwarf` struct.
/// * `cwd` - The work directory of the debugged program.
/// * `path` - The relative path to the source file from the work directory of the debugged program.
/// * `line` - A line number in the source program.
/// * `column` - A optional column number in the source program.
///
/// Finds the machine code address that is generated from the given source code file and line
/// number.
/// If there are multiple machine codes for that line number it takes the first one and the one.
// Good source: DWARF section 6.2
pub fn find_breakpoint_location<'a, R: Reader<Offset = usize>>(
dwarf: &'a Dwarf<R>,
cwd: &str,
path: &str,
line: u64,
column: Option<u64>,
) -> Result<Option<u64>> {
let mut locations = vec![];
let mut units = dwarf.units();
while let Some(unit_header) = units.next()? {
let unit = dwarf.unit(unit_header)?;
if let Some(ref line_program) = unit.line_program {
let lp_header = line_program.header();
for file_entry in lp_header.file_names() {
let directory = match file_entry.directory(lp_header) {
Some(dir_av) => {
let dir_raw = dwarf.attr_string(&unit, dir_av)?;
dir_raw.to_string()?.to_string()
}
None => continue,
};
let file_raw = dwarf.attr_string(&unit, file_entry.path_name())?;
let mut file_path = format!("{}/{}", directory, file_raw.to_string()?.to_string());
if !file_path.starts_with("/") {
// TODO: Find a better solution
file_path = format!("{}/{}", cwd, file_path);
}
if path == &file_path {
let mut rows = line_program.clone().rows();
while let Some((header, row)) = rows.next_row()? {
let file_entry = match row.file(header) {
Some(v) => v,
None => continue,
};
let directory = match file_entry.directory(header) {
Some(dir_av) => {
let dir_raw = dwarf.attr_string(&unit, dir_av)?;
dir_raw.to_string()?.to_string()
}
None => continue,
};
let file_raw = dwarf.attr_string(&unit, file_entry.path_name())?;
let mut file_path =
format!("{}/{}", directory, file_raw.to_string()?.to_string());
if !file_path.starts_with("/") {
// TODO: Find a better solution
file_path = format!("{}/{}", cwd, file_path);
}
if path == &file_path {
if let Some(l) = row.line() {
if line == l {
locations.push((row.column(), row.address()));
}
}
}
}
}
}
}
}
match locations.len() {
0 => return Ok(None),
len => {
let search = match column {
Some(v) => gimli::ColumnType::Column(v),
None => gimli::ColumnType::LeftEdge,
};
let mut res = locations[0];
for i in 1..len {
if locations[i].0 <= search && locations[i].0 > res.0 {
res = locations[i];
}
}
return Ok(Some(res.1));
}
};
}