+
Skip to content

read/macros: combine MacInfoIter and MacroIter #772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 34 additions & 106 deletions crates/examples/src/bin/dwarfdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,13 +1205,13 @@ fn dump_entries<R: Reader, W: Write>(
for offset in deferred_macinfo {
writeln!(w)?;
writeln!(w, "Macros <.debug_macinfo+0x{:08x}>", offset.0)?;
dump_macinfo_list(w, unit, offset)?;
dump_macros(w, unit, unit.macinfo(offset)?, false)?;
}

for offset in deferred_macros {
writeln!(w)?;
writeln!(w, "Macros <.debug_macro+0x{:08x}>", offset.0)?;
dump_macro_list(w, unit, offset)?;
dump_macros(w, unit, unit.macros(offset)?, true)?;
}

Ok(())
Expand Down Expand Up @@ -2292,106 +2292,32 @@ fn dump_addr<R: Reader, W: Write>(w: &mut W, debug_addr: &gimli::DebugAddr<R>) -
Ok(())
}

fn dump_macinfo_list<R: Reader, W: Write>(
fn dump_macros<R: Reader, W: Write>(
w: &mut W,
unit: gimli::UnitRef<'_, R>,
offset: gimli::DebugMacinfoOffset,
mut macros: gimli::MacroIter<R>,
is_macro: bool,
) -> Result<()> {
let mut indent = 2; // base indent is 2 spaces
let mut macinfo_iter = unit.macinfo(offset)?;
while let Some(macinfo) = macinfo_iter.next()? {
match macinfo {
let prefix = if is_macro { "DW_MACRO_" } else { "DW_MACINFO_" };
while let Some(entry) = macros.next()? {
match entry {
gimli::MacroEntry::StartFile { .. } => {
// print the item first, then indent
write!(w, "{:indent$}", "", indent = indent)?;
dump_macinfo(w, unit, macinfo)?;
write!(w, "{:indent$}{prefix}", "", indent = indent)?;
dump_macro(w, unit, entry)?;
indent += 2;
}
gimli::MacroEntry::EndFile => {
// unindent first, then print the item
indent -= 2;
write!(w, "{:indent$}", "", indent = indent)?;
dump_macinfo(w, unit, macinfo)?;
write!(w, "{:indent$}{prefix}", "", indent = indent)?;
dump_macro(w, unit, entry)?;
}
_ => {
// no indentation change
write!(w, "{:indent$}", "", indent = indent)?;
dump_macinfo(w, unit, macinfo)?;
}
}
}
Ok(())
}

fn dump_macinfo<R: Reader, W: Write>(
w: &mut W,
unit: gimli::UnitRef<'_, R>,
macinfo: gimli::MacroEntry<R>,
) -> Result<()> {
match macinfo {
gimli::MacroEntry::Define { line, text } => {
writeln!(
w,
"DW_MACINFO_define - lineno: {line}, macro: {}",
text.string(unit)?.to_string_lossy()?
)?;
}
gimli::MacroEntry::Undef { line, name } => {
writeln!(
w,
"DW_MACINFO_undef - lineno: {line}, macro: {}",
name.string(unit)?.to_string_lossy()?
)?;
}
gimli::MacroEntry::StartFile { line, file } => {
write!(w, "DW_MACINFO_start_file - lineno: {line}, file: ")?;
dump_file_index(w, file, unit)?;
writeln!(w)?;
}
gimli::MacroEntry::EndFile => {
writeln!(w, "DW_MACINFO_end_file")?;
}
gimli::MacroEntry::VendorExt { numeric, string } => {
writeln!(
w,
"DW_MACINFO_vendor_ext - number: {numeric}, string: {}",
string.to_string_lossy()?
)?;
}
gimli::MacroEntry::Import { .. } | gimli::MacroEntry::ImportSup { .. } => {
unreachable!(
"Import and ImportSup are only used in .debug_macro, but this code is for .debug_macinfo"
);
}
}
Ok(())
}

fn dump_macro_list<R: Reader, W: Write>(
w: &mut W,
unit: gimli::UnitRef<'_, R>,
offset: gimli::DebugMacroOffset,
) -> Result<()> {
let mut indent = 2; // base indent is 2 spaces
let mut macro_iter = unit.macros(offset)?;
while let Some(macro_item) = macro_iter.next()? {
match macro_item {
gimli::MacroEntry::StartFile { .. } => {
// print the item first, then indent
write!(w, "{:indent$}", "", indent = indent)?;
dump_macro(w, unit, macro_item)?;
indent += 2;
}
gimli::MacroEntry::EndFile => {
// unindent first, then print the item
indent -= 2;
write!(w, "{:indent$}", "", indent = indent)?;
dump_macro(w, unit, macro_item)?;
}
_ => {
// no indentation change
write!(w, "{:indent$}", "", indent = indent)?;
dump_macro(w, unit, macro_item)?;
write!(w, "{:indent$}{prefix}", "", indent = indent)?;
dump_macro(w, unit, entry)?;
}
}
}
Expand All @@ -2401,29 +2327,29 @@ fn dump_macro_list<R: Reader, W: Write>(
fn dump_macro<R: Reader, W: Write>(
w: &mut W,
unit: gimli::UnitRef<'_, R>,
macro_item: gimli::MacroEntry<R>,
entry: gimli::MacroEntry<R>,
) -> Result<()> {
match macro_item {
match entry {
gimli::MacroEntry::Define { line, text } => {
match text {
gimli::MacroString::Direct(text) => writeln!(
w,
"DW_MACRO_define - lineno: {line}, macro: {}",
"define - lineno: {line}, macro: {}",
text.to_string_lossy()?
)?,
gimli::MacroString::StringPointer(_) => writeln!(
w,
"DW_MACRO_define_strp - lineno: {line}, macro: {}",
"define_strp - lineno: {line}, macro: {}",
text.string(unit)?.to_string_lossy()?
)?,
gimli::MacroString::IndirectStringPointer(_) => writeln!(
w,
"DW_MACRO_define_strx - lineno: {line}, macro: {}",
"define_strx - lineno: {line}, macro: {}",
text.string(unit)?.to_string_lossy()?
)?,
gimli::MacroString::Supplementary(_) => writeln!(
w,
"DW_MACRO_define_sup - lineno: {line}, macro: {}",
"define_sup - lineno: {line}, macro: {}",
text.string(unit)?.to_string_lossy()?
)?,
};
Expand All @@ -2432,44 +2358,46 @@ fn dump_macro<R: Reader, W: Write>(
match name {
gimli::MacroString::Direct(name) => writeln!(
w,
"DW_MACRO_undef - lineno: {line}, macro: {}",
"undef - lineno: {line}, macro: {}",
name.to_string_lossy()?
)?,
gimli::MacroString::StringPointer(_) => writeln!(
w,
"DW_MACRO_undef_strp - lineno: {line}, macro: {}",
"undef_strp - lineno: {line}, macro: {}",
name.string(unit)?.to_string_lossy()?
)?,
gimli::MacroString::IndirectStringPointer(_) => writeln!(
w,
"DW_MACRO_undef_strx - lineno: {line}, macro: {}",
"undef_strx - lineno: {line}, macro: {}",
name.string(unit)?.to_string_lossy()?
)?,
gimli::MacroString::Supplementary(_) => writeln!(
w,
"DW_MACRO_undef_sup - lineno: {line}, macro: {}",
"undef_sup - lineno: {line}, macro: {}",
name.string(unit)?.to_string_lossy()?
)?,
};
}
gimli::MacroEntry::StartFile { line, file } => {
write!(w, "DW_MACRO_start_file - lineno: {line}, file: ")?;
write!(w, "start_file - lineno: {line}, file: ")?;
dump_file_index(w, file, unit)?;
writeln!(w)?;
}
gimli::MacroEntry::EndFile => {
writeln!(w, "DW_MACRO_end_file")?;
writeln!(w, "end_file")?;
}
gimli::MacroEntry::Import { offset } => {
writeln!(w, "<.debug_macro+0x{:08x}>", offset.0)?;
writeln!(w, "import <.debug_macro+0x{:08x}>", offset.0)?;
}
gimli::MacroEntry::ImportSup { offset } => {
writeln!(w, "<.debug_macro(sup)+0x{:08x}>", offset.0)?;
writeln!(w, "import_sup <.debug_macro(sup)+0x{:08x}>", offset.0)?;
}
gimli::MacroEntry::VendorExt { .. } => {
unreachable!(
"VendorExt is only used in .debug_macinfo, but this code is for .debug_macro"
);
gimli::MacroEntry::VendorExt { numeric, string } => {
writeln!(
w,
"vendor_ext - number: {numeric}, string: {}",
string.to_string_lossy()?
)?;
}
}
Ok(())
Expand Down
34 changes: 13 additions & 21 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,10 +1059,22 @@ DwLnct(u16) {
DW_LNCT_hi_user = 0x3fff,
});

dw!(
/// Type codes for macro definitions in the `.debug_macinfo` section.
///
/// See Section 7.22, Figure 39 for DWARF 4.
DwMacinfo(u8) {
DW_MACINFO_define = 0x01,
DW_MACINFO_undef = 0x02,
DW_MACINFO_start_file = 0x03,
DW_MACINFO_end_file = 0x04,
DW_MACINFO_vendor_ext = 0xff,
});

dw!(
/// The encodings for macro information entry types.
///
/// See Section 7.23, Table 7.28.
/// See Section 7.23, Table 7.28 for DWARF 5.
DwMacro(u8) {
DW_MACRO_define = 0x01,
DW_MACRO_undef = 0x02,
Expand Down Expand Up @@ -1402,26 +1414,6 @@ impl DwEhPe {
}
}

dw!(
/// Type codes for macro definitions in the `.debug_macinfo section`.
///
/// `.debug_macinfo` is defined in Dwarf 2, 3, and 4. Dwarf 5 defines `.debug_macro` instead.
/// See Section 7.22 (Macro Information) in the Dwarf 4 Standard.
DwMacInfo(u8) {
// "The series of entries for a given compilation unit ends with an entry containing a type code of 0"
DW_MACINFO_null = 0x00,
// macro definition; uses two operands: line number (LEB128) and the defined macro symbol (null terminated string)
DW_MACINFO_define = 0x01,
// macro undefinition; uses two operands: line number (LEB128) and the undefined macro symbol (null terminated string)
DW_MACINFO_undef = 0x02,
// The start of a new source file inclusion. Uses two operands: line number (LEB128) and an index into the line number table of the compilation unit (LEB128).
DW_MACINFO_start_file = 0x03,
// The end of the current source file inclusion. Has no operands.
DW_MACINFO_end_file = 0x04,
// Vendor specific macro information directives. Has two operands: a constant (LEB128) and a null terminated string, whose meaning is vendor specific.
DW_MACINFO_vendor_ext = 0xff,
});

#[cfg(test)]
mod tests {
use super::*;
Expand Down
21 changes: 10 additions & 11 deletions src/read/dwarf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ use crate::common::{
use crate::read::{
Abbreviations, AbbreviationsCache, AbbreviationsCacheStrategy, AttributeValue, DebugAbbrev,
DebugAddr, DebugAranges, DebugCuIndex, DebugInfo, DebugInfoUnitHeadersIter, DebugLine,
DebugLineStr, DebugLoc, DebugLocLists, DebugMacInfo, DebugMacro, DebugRanges, DebugRngLists,
DebugLineStr, DebugLoc, DebugLocLists, DebugMacinfo, DebugMacro, DebugRanges, DebugRngLists,
DebugStr, DebugStrOffsets, DebugTuIndex, DebugTypes, DebugTypesUnitHeadersIter,
DebuggingInformationEntry, EntriesCursor, EntriesRaw, EntriesTree, Error,
IncompleteLineProgram, IndexSectionId, LocListIter, LocationLists, MacInfoIter, MacroIter,
Range, RangeLists, RawLocListIter, RawRngListIter, Reader, ReaderOffset, ReaderOffsetId,
Result, RngListIter, Section, UnitHeader, UnitIndex, UnitIndexSectionIterator, UnitOffset,
UnitType,
IncompleteLineProgram, IndexSectionId, LocListIter, LocationLists, MacroIter, Range,
RangeLists, RawLocListIter, RawRngListIter, Reader, ReaderOffset, ReaderOffsetId, Result,
RngListIter, Section, UnitHeader, UnitIndex, UnitIndexSectionIterator, UnitOffset, UnitType,
};
use crate::{constants, DebugMacroOffset};

Expand Down Expand Up @@ -63,7 +62,7 @@ pub struct DwarfSections<T> {
/// The `.debug_line_str` section.
pub debug_line_str: DebugLineStr<T>,
/// The `.debug_macinfo` section.
pub debug_macinfo: DebugMacInfo<T>,
pub debug_macinfo: DebugMacinfo<T>,
/// The `.debug_macro` section.
pub debug_macro: DebugMacro<T>,
/// The `.debug_str` section.
Expand Down Expand Up @@ -188,7 +187,7 @@ pub struct Dwarf<R> {
pub debug_line_str: DebugLineStr<R>,

/// The `.debug_macinfo` section.
pub debug_macinfo: DebugMacInfo<R>,
pub debug_macinfo: DebugMacinfo<R>,

/// The `.debug_macro` section.
pub debug_macro: DebugMacro<R>,
Expand Down Expand Up @@ -747,7 +746,7 @@ impl<R: Reader> Dwarf<R> {
}

/// Return a fallible iterator over the macro information from `.debug_macinfo` for the given offset.
pub fn macinfo(&self, offset: DebugMacinfoOffset<R::Offset>) -> Result<MacInfoIter<R>> {
pub fn macinfo(&self, offset: DebugMacinfoOffset<R::Offset>) -> Result<MacroIter<R>> {
self.debug_macinfo.get_macinfo(offset)
}

Expand Down Expand Up @@ -1562,12 +1561,12 @@ impl<'a, R: Reader> UnitRef<'a, R> {
self.dwarf.attr_locations(self.unit, attr)
}

/// Try to return an iterator for the list of `DebugMacInfoItem` at the given offset.
pub fn macinfo(&self, offset: DebugMacinfoOffset<R::Offset>) -> Result<MacInfoIter<R>> {
/// Try to return an iterator for the list of macros at the given `.debug_macinfo` offset.
pub fn macinfo(&self, offset: DebugMacinfoOffset<R::Offset>) -> Result<MacroIter<R>> {
self.dwarf.macinfo(offset)
}

/// Try to return an iterator for the list of `DebugMacroItem` at the given offset.
/// Try to return an iterator for the list of macros at the given `.debug_macro` offset.
pub fn macros(&self, offset: DebugMacroOffset<R::Offset>) -> Result<MacroIter<R>> {
self.dwarf.macros(offset)
}
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载