Skip to content
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

Add the ability to dump specific DIEs with --debug-info=0xdeadbeef. #564

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
92 changes: 68 additions & 24 deletions examples/dwarfdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ struct Flags {
eh_frame: bool,
goff: bool,
info: bool,
info_offset: Option<gimli::DebugInfoOffset>,
line: bool,
pubnames: bool,
pubtypes: bool,
Expand Down Expand Up @@ -386,7 +387,12 @@ fn main() {
"print .eh-frame exception handling frame information",
);
opts.optflag("G", "", "show global die offsets");
opts.optflag("i", "", "print .debug_info and .debug_types sections");
opts.optflagopt(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think optflagopt is a good choice. Previously you could do dwarfdump -i filename but now that doesn't work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what should we do instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dwarfdump -i -- filename works but yeah that's not ideal.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about -o/--offset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could do that, though the reason I chose this was to match llvm-dwarfdump.

Maybe we should switch to structopt instead. I think it can be made to do the right thing here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, yeah matching llvm-dwarfdump would be good (llvm-dwarfdump --help didn't show that option). If you want, can leave this how it is and switch later.

"i",
"debug-info",
"print .debug_info and .debug_types sections",
"offset",
);
opts.optflag("l", "", "print .debug_line section");
opts.optflag("p", "", "print .debug_pubnames section");
opts.optflag("r", "", "print .debug_aranges section");
Expand Down Expand Up @@ -427,6 +433,24 @@ fn main() {
}
if matches.opt_present("i") {
flags.info = true;
if let Some(offset) = matches.opt_str("i") {
let offset = match offset
.get(2..)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if the 0x was optional.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My primary concern was that we were skipping the first two characters without checking what they were. llvm-dwarfdump treats it as decimal if 0x is missing, so I guess we need to match that, or give an error.

.and_then(|o| usize::from_str_radix(o, 16).ok())
{
Some(offset) => offset,
None => {
writeln!(
&mut io::stderr(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use eprintln! (and there's an existing call that could be changed too).

"Could not parse offset argument '{}'",
offset
)
.ok();
print_usage(&opts);
}
};
flags.info_offset = Some(gimli::DebugInfoOffset(offset));
}
all = false;
}
if matches.opt_present("l") {
Expand Down Expand Up @@ -970,30 +994,37 @@ where
}
};
let process_unit = |header: UnitHeader<R>, buf: &mut Vec<u8>| -> Result<()> {
writeln!(
buf,
"\nUNIT<header overall offset = 0x{:08x}>:",
header.offset().as_debug_info_offset().unwrap().0,
)?;
let header_offset = header.offset().as_debug_info_offset().unwrap();
if flags
.info_offset
.map(|o| o == header_offset)
.unwrap_or(true)
{
writeln!(
buf,
"\nUNIT<header overall offset = 0x{:08x}>:",
header_offset.0,
)?;

match header.type_() {
UnitType::Compilation | UnitType::Partial => (),
UnitType::Type {
type_signature,
type_offset,
}
| UnitType::SplitType {
type_signature,
type_offset,
} => {
write!(buf, " signature = ")?;
dump_type_signature(buf, type_signature)?;
writeln!(buf)?;
writeln!(buf, " typeoffset = 0x{:08x}", type_offset.0,)?;
}
UnitType::Skeleton(dwo_id) | UnitType::SplitCompilation(dwo_id) => {
write!(buf, " dwo_id = ")?;
writeln!(buf, "0x{:016x}", dwo_id.0)?;
match header.type_() {
UnitType::Compilation | UnitType::Partial => (),
UnitType::Type {
type_signature,
type_offset,
}
| UnitType::SplitType {
type_signature,
type_offset,
} => {
write!(buf, " signature = ")?;
dump_type_signature(buf, type_signature)?;
writeln!(buf)?;
writeln!(buf, " typeoffset = 0x{:08x}", type_offset.0,)?;
}
UnitType::Skeleton(dwo_id) | UnitType::SplitCompilation(dwo_id) => {
write!(buf, " dwo_id = ")?;
writeln!(buf, "0x{:016x}", dwo_id.0)?;
}
}
}

Expand Down Expand Up @@ -1102,9 +1133,22 @@ fn dump_entries<R: Reader, W: Write>(
let mut spaces_buf = String::new();

let mut depth = 0;
let mut processing_subtree = false;
let mut entries = unit.entries();
while let Some((delta_depth, entry)) = entries.next_dfs()? {
depth += delta_depth;
if let Some(o) = flags.info_offset {
if o == entry.offset().to_debug_info_offset(&unit.header).unwrap() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will panic if there is a .debug_types section.

processing_subtree = true;
depth = 0;
} else if processing_subtree == true {
if depth <= 0 {
break;
}
} else {
continue;
}
}
assert!(depth >= 0);
let mut indent = depth as usize * 2 + 2;
write!(w, "<{}{}>", if depth < 10 { " " } else { "" }, depth)?;
Expand Down