Skip to content

Instantly share code, notes, and snippets.

@davestevens
Created January 25, 2011 14:51
Show Gist options
  • Save davestevens/795006 to your computer and use it in GitHub Desktop.
Save davestevens/795006 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# read from object file, output all variable names and types.
# object needs to be compiled with -g CFLAG
if($ARGV[0] eq '') {
print "need to specify an objectfile\n";
}
else {
$objfile = $ARGV[0];
}
@array = readpipe("objdump --dwarf=pubnames $objfile");
foreach $line (@array) {
if($line =~ /^\s+(\w+)\s+(\w+)$/) {
if($1 ne "Offset") {
$names{$2} = hex($1);
$names2{hex($1)} = $2;
}
}
}
@array = readpipe("objdump --dwarf=info $objfile");
foreach $line (@array) {
if($line =~ /^\s+(<(\w+)>){1,2}/) {
$lines[hex($2)] = $line;
}
}
foreach $key (keys %names) {
&findName($names{$key}, $key, 1);
}
foreach $names (keys %type) {
print "$names -> $type{$names}\n";
}
sub findName() {
$element = $_[0];
$name = $_[1];
if($_[2] == 1) {
$found = 0;
}
for($i=$element;$i<@lines;$i++) {
if($lines[$i] =~ /DW_AT_name\s+:\s+/) {
$lines[$i] =~ /:\s((\w+\s*)*)\s*\n$/;
$n = $1;
$n =~ s/\s*$//g;
if($n ne $name) {
$type{$name} = $n;
$found = 1;
return;
}
}
elsif($lines[$i] =~ /DW_AT_type\s+:\s+<0x(\w+)>/) {
&findName(hex($1), $name, 0);
if($found) {
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment