Skip to content

Commit 556e419

Browse files
authored
read/macros: combine MacInfoIter and MacroIter (#772)
Also use consistent capitalisation for Macinfo.
1 parent dddaed7 commit 556e419

File tree

5 files changed

+122
-263
lines changed

5 files changed

+122
-263
lines changed

crates/examples/src/bin/dwarfdump.rs

Lines changed: 34 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,13 +1205,13 @@ fn dump_entries<R: Reader, W: Write>(
12051205
for offset in deferred_macinfo {
12061206
writeln!(w)?;
12071207
writeln!(w, "Macros <.debug_macinfo+0x{:08x}>", offset.0)?;
1208-
dump_macinfo_list(w, unit, offset)?;
1208+
dump_macros(w, unit, unit.macinfo(offset)?, false)?;
12091209
}
12101210

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

12171217
Ok(())
@@ -2292,106 +2292,32 @@ fn dump_addr<R: Reader, W: Write>(w: &mut W, debug_addr: &gimli::DebugAddr<R>) -
22922292
Ok(())
22932293
}
22942294

2295-
fn dump_macinfo_list<R: Reader, W: Write>(
2295+
fn dump_macros<R: Reader, W: Write>(
22962296
w: &mut W,
22972297
unit: gimli::UnitRef<'_, R>,
2298-
offset: gimli::DebugMacinfoOffset,
2298+
mut macros: gimli::MacroIter<R>,
2299+
is_macro: bool,
22992300
) -> Result<()> {
23002301
let mut indent = 2; // base indent is 2 spaces
2301-
let mut macinfo_iter = unit.macinfo(offset)?;
2302-
while let Some(macinfo) = macinfo_iter.next()? {
2303-
match macinfo {
2302+
let prefix = if is_macro { "DW_MACRO_" } else { "DW_MACINFO_" };
2303+
while let Some(entry) = macros.next()? {
2304+
match entry {
23042305
gimli::MacroEntry::StartFile { .. } => {
23052306
// print the item first, then indent
2306-
write!(w, "{:indent$}", "", indent = indent)?;
2307-
dump_macinfo(w, unit, macinfo)?;
2307+
write!(w, "{:indent$}{prefix}", "", indent = indent)?;
2308+
dump_macro(w, unit, entry)?;
23082309
indent += 2;
23092310
}
23102311
gimli::MacroEntry::EndFile => {
23112312
// unindent first, then print the item
23122313
indent -= 2;
2313-
write!(w, "{:indent$}", "", indent = indent)?;
2314-
dump_macinfo(w, unit, macinfo)?;
2314+
write!(w, "{:indent$}{prefix}", "", indent = indent)?;
2315+
dump_macro(w, unit, entry)?;
23152316
}
23162317
_ => {
23172318
// no indentation change
2318-
write!(w, "{:indent$}", "", indent = indent)?;
2319-
dump_macinfo(w, unit, macinfo)?;
2320-
}
2321-
}
2322-
}
2323-
Ok(())
2324-
}
2325-
2326-
fn dump_macinfo<R: Reader, W: Write>(
2327-
w: &mut W,
2328-
unit: gimli::UnitRef<'_, R>,
2329-
macinfo: gimli::MacroEntry<R>,
2330-
) -> Result<()> {
2331-
match macinfo {
2332-
gimli::MacroEntry::Define { line, text } => {
2333-
writeln!(
2334-
w,
2335-
"DW_MACINFO_define - lineno: {line}, macro: {}",
2336-
text.string(unit)?.to_string_lossy()?
2337-
)?;
2338-
}
2339-
gimli::MacroEntry::Undef { line, name } => {
2340-
writeln!(
2341-
w,
2342-
"DW_MACINFO_undef - lineno: {line}, macro: {}",
2343-
name.string(unit)?.to_string_lossy()?
2344-
)?;
2345-
}
2346-
gimli::MacroEntry::StartFile { line, file } => {
2347-
write!(w, "DW_MACINFO_start_file - lineno: {line}, file: ")?;
2348-
dump_file_index(w, file, unit)?;
2349-
writeln!(w)?;
2350-
}
2351-
gimli::MacroEntry::EndFile => {
2352-
writeln!(w, "DW_MACINFO_end_file")?;
2353-
}
2354-
gimli::MacroEntry::VendorExt { numeric, string } => {
2355-
writeln!(
2356-
w,
2357-
"DW_MACINFO_vendor_ext - number: {numeric}, string: {}",
2358-
string.to_string_lossy()?
2359-
)?;
2360-
}
2361-
gimli::MacroEntry::Import { .. } | gimli::MacroEntry::ImportSup { .. } => {
2362-
unreachable!(
2363-
"Import and ImportSup are only used in .debug_macro, but this code is for .debug_macinfo"
2364-
);
2365-
}
2366-
}
2367-
Ok(())
2368-
}
2369-
2370-
fn dump_macro_list<R: Reader, W: Write>(
2371-
w: &mut W,
2372-
unit: gimli::UnitRef<'_, R>,
2373-
offset: gimli::DebugMacroOffset,
2374-
) -> Result<()> {
2375-
let mut indent = 2; // base indent is 2 spaces
2376-
let mut macro_iter = unit.macros(offset)?;
2377-
while let Some(macro_item) = macro_iter.next()? {
2378-
match macro_item {
2379-
gimli::MacroEntry::StartFile { .. } => {
2380-
// print the item first, then indent
2381-
write!(w, "{:indent$}", "", indent = indent)?;
2382-
dump_macro(w, unit, macro_item)?;
2383-
indent += 2;
2384-
}
2385-
gimli::MacroEntry::EndFile => {
2386-
// unindent first, then print the item
2387-
indent -= 2;
2388-
write!(w, "{:indent$}", "", indent = indent)?;
2389-
dump_macro(w, unit, macro_item)?;
2390-
}
2391-
_ => {
2392-
// no indentation change
2393-
write!(w, "{:indent$}", "", indent = indent)?;
2394-
dump_macro(w, unit, macro_item)?;
2319+
write!(w, "{:indent$}{prefix}", "", indent = indent)?;
2320+
dump_macro(w, unit, entry)?;
23952321
}
23962322
}
23972323
}
@@ -2401,29 +2327,29 @@ fn dump_macro_list<R: Reader, W: Write>(
24012327
fn dump_macro<R: Reader, W: Write>(
24022328
w: &mut W,
24032329
unit: gimli::UnitRef<'_, R>,
2404-
macro_item: gimli::MacroEntry<R>,
2330+
entry: gimli::MacroEntry<R>,
24052331
) -> Result<()> {
2406-
match macro_item {
2332+
match entry {
24072333
gimli::MacroEntry::Define { line, text } => {
24082334
match text {
24092335
gimli::MacroString::Direct(text) => writeln!(
24102336
w,
2411-
"DW_MACRO_define - lineno: {line}, macro: {}",
2337+
"define - lineno: {line}, macro: {}",
24122338
text.to_string_lossy()?
24132339
)?,
24142340
gimli::MacroString::StringPointer(_) => writeln!(
24152341
w,
2416-
"DW_MACRO_define_strp - lineno: {line}, macro: {}",
2342+
"define_strp - lineno: {line}, macro: {}",
24172343
text.string(unit)?.to_string_lossy()?
24182344
)?,
24192345
gimli::MacroString::IndirectStringPointer(_) => writeln!(
24202346
w,
2421-
"DW_MACRO_define_strx - lineno: {line}, macro: {}",
2347+
"define_strx - lineno: {line}, macro: {}",
24222348
text.string(unit)?.to_string_lossy()?
24232349
)?,
24242350
gimli::MacroString::Supplementary(_) => writeln!(
24252351
w,
2426-
"DW_MACRO_define_sup - lineno: {line}, macro: {}",
2352+
"define_sup - lineno: {line}, macro: {}",
24272353
text.string(unit)?.to_string_lossy()?
24282354
)?,
24292355
};
@@ -2432,44 +2358,46 @@ fn dump_macro<R: Reader, W: Write>(
24322358
match name {
24332359
gimli::MacroString::Direct(name) => writeln!(
24342360
w,
2435-
"DW_MACRO_undef - lineno: {line}, macro: {}",
2361+
"undef - lineno: {line}, macro: {}",
24362362
name.to_string_lossy()?
24372363
)?,
24382364
gimli::MacroString::StringPointer(_) => writeln!(
24392365
w,
2440-
"DW_MACRO_undef_strp - lineno: {line}, macro: {}",
2366+
"undef_strp - lineno: {line}, macro: {}",
24412367
name.string(unit)?.to_string_lossy()?
24422368
)?,
24432369
gimli::MacroString::IndirectStringPointer(_) => writeln!(
24442370
w,
2445-
"DW_MACRO_undef_strx - lineno: {line}, macro: {}",
2371+
"undef_strx - lineno: {line}, macro: {}",
24462372
name.string(unit)?.to_string_lossy()?
24472373
)?,
24482374
gimli::MacroString::Supplementary(_) => writeln!(
24492375
w,
2450-
"DW_MACRO_undef_sup - lineno: {line}, macro: {}",
2376+
"undef_sup - lineno: {line}, macro: {}",
24512377
name.string(unit)?.to_string_lossy()?
24522378
)?,
24532379
};
24542380
}
24552381
gimli::MacroEntry::StartFile { line, file } => {
2456-
write!(w, "DW_MACRO_start_file - lineno: {line}, file: ")?;
2382+
write!(w, "start_file - lineno: {line}, file: ")?;
24572383
dump_file_index(w, file, unit)?;
24582384
writeln!(w)?;
24592385
}
24602386
gimli::MacroEntry::EndFile => {
2461-
writeln!(w, "DW_MACRO_end_file")?;
2387+
writeln!(w, "end_file")?;
24622388
}
24632389
gimli::MacroEntry::Import { offset } => {
2464-
writeln!(w, "<.debug_macro+0x{:08x}>", offset.0)?;
2390+
writeln!(w, "import <.debug_macro+0x{:08x}>", offset.0)?;
24652391
}
24662392
gimli::MacroEntry::ImportSup { offset } => {
2467-
writeln!(w, "<.debug_macro(sup)+0x{:08x}>", offset.0)?;
2393+
writeln!(w, "import_sup <.debug_macro(sup)+0x{:08x}>", offset.0)?;
24682394
}
2469-
gimli::MacroEntry::VendorExt { .. } => {
2470-
unreachable!(
2471-
"VendorExt is only used in .debug_macinfo, but this code is for .debug_macro"
2472-
);
2395+
gimli::MacroEntry::VendorExt { numeric, string } => {
2396+
writeln!(
2397+
w,
2398+
"vendor_ext - number: {numeric}, string: {}",
2399+
string.to_string_lossy()?
2400+
)?;
24732401
}
24742402
}
24752403
Ok(())

src/constants.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,10 +1059,22 @@ DwLnct(u16) {
10591059
DW_LNCT_hi_user = 0x3fff,
10601060
});
10611061

1062+
dw!(
1063+
/// Type codes for macro definitions in the `.debug_macinfo` section.
1064+
///
1065+
/// See Section 7.22, Figure 39 for DWARF 4.
1066+
DwMacinfo(u8) {
1067+
DW_MACINFO_define = 0x01,
1068+
DW_MACINFO_undef = 0x02,
1069+
DW_MACINFO_start_file = 0x03,
1070+
DW_MACINFO_end_file = 0x04,
1071+
DW_MACINFO_vendor_ext = 0xff,
1072+
});
1073+
10621074
dw!(
10631075
/// The encodings for macro information entry types.
10641076
///
1065-
/// See Section 7.23, Table 7.28.
1077+
/// See Section 7.23, Table 7.28 for DWARF 5.
10661078
DwMacro(u8) {
10671079
DW_MACRO_define = 0x01,
10681080
DW_MACRO_undef = 0x02,
@@ -1402,26 +1414,6 @@ impl DwEhPe {
14021414
}
14031415
}
14041416

1405-
dw!(
1406-
/// Type codes for macro definitions in the `.debug_macinfo section`.
1407-
///
1408-
/// `.debug_macinfo` is defined in Dwarf 2, 3, and 4. Dwarf 5 defines `.debug_macro` instead.
1409-
/// See Section 7.22 (Macro Information) in the Dwarf 4 Standard.
1410-
DwMacInfo(u8) {
1411-
// "The series of entries for a given compilation unit ends with an entry containing a type code of 0"
1412-
DW_MACINFO_null = 0x00,
1413-
// macro definition; uses two operands: line number (LEB128) and the defined macro symbol (null terminated string)
1414-
DW_MACINFO_define = 0x01,
1415-
// macro undefinition; uses two operands: line number (LEB128) and the undefined macro symbol (null terminated string)
1416-
DW_MACINFO_undef = 0x02,
1417-
// 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).
1418-
DW_MACINFO_start_file = 0x03,
1419-
// The end of the current source file inclusion. Has no operands.
1420-
DW_MACINFO_end_file = 0x04,
1421-
// Vendor specific macro information directives. Has two operands: a constant (LEB128) and a null terminated string, whose meaning is vendor specific.
1422-
DW_MACINFO_vendor_ext = 0xff,
1423-
});
1424-
14251417
#[cfg(test)]
14261418
mod tests {
14271419
use super::*;

src/read/dwarf.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ use crate::common::{
1111
use crate::read::{
1212
Abbreviations, AbbreviationsCache, AbbreviationsCacheStrategy, AttributeValue, DebugAbbrev,
1313
DebugAddr, DebugAranges, DebugCuIndex, DebugInfo, DebugInfoUnitHeadersIter, DebugLine,
14-
DebugLineStr, DebugLoc, DebugLocLists, DebugMacInfo, DebugMacro, DebugRanges, DebugRngLists,
14+
DebugLineStr, DebugLoc, DebugLocLists, DebugMacinfo, DebugMacro, DebugRanges, DebugRngLists,
1515
DebugStr, DebugStrOffsets, DebugTuIndex, DebugTypes, DebugTypesUnitHeadersIter,
1616
DebuggingInformationEntry, EntriesCursor, EntriesRaw, EntriesTree, Error,
17-
IncompleteLineProgram, IndexSectionId, LocListIter, LocationLists, MacInfoIter, MacroIter,
18-
Range, RangeLists, RawLocListIter, RawRngListIter, Reader, ReaderOffset, ReaderOffsetId,
19-
Result, RngListIter, Section, UnitHeader, UnitIndex, UnitIndexSectionIterator, UnitOffset,
20-
UnitType,
17+
IncompleteLineProgram, IndexSectionId, LocListIter, LocationLists, MacroIter, Range,
18+
RangeLists, RawLocListIter, RawRngListIter, Reader, ReaderOffset, ReaderOffsetId, Result,
19+
RngListIter, Section, UnitHeader, UnitIndex, UnitIndexSectionIterator, UnitOffset, UnitType,
2120
};
2221
use crate::{constants, DebugMacroOffset};
2322

@@ -63,7 +62,7 @@ pub struct DwarfSections<T> {
6362
/// The `.debug_line_str` section.
6463
pub debug_line_str: DebugLineStr<T>,
6564
/// The `.debug_macinfo` section.
66-
pub debug_macinfo: DebugMacInfo<T>,
65+
pub debug_macinfo: DebugMacinfo<T>,
6766
/// The `.debug_macro` section.
6867
pub debug_macro: DebugMacro<T>,
6968
/// The `.debug_str` section.
@@ -188,7 +187,7 @@ pub struct Dwarf<R> {
188187
pub debug_line_str: DebugLineStr<R>,
189188

190189
/// The `.debug_macinfo` section.
191-
pub debug_macinfo: DebugMacInfo<R>,
190+
pub debug_macinfo: DebugMacinfo<R>,
192191

193192
/// The `.debug_macro` section.
194193
pub debug_macro: DebugMacro<R>,
@@ -747,7 +746,7 @@ impl<R: Reader> Dwarf<R> {
747746
}
748747

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

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

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

1570-
/// Try to return an iterator for the list of `DebugMacroItem` at the given offset.
1569+
/// Try to return an iterator for the list of macros at the given `.debug_macro` offset.
15711570
pub fn macros(&self, offset: DebugMacroOffset<R::Offset>) -> Result<MacroIter<R>> {
15721571
self.dwarf.macros(offset)
15731572
}

0 commit comments

Comments
 (0)