Quantr Dwarf Library - Print address/lineNo/ColNo from specific address
2024-04-07
File elf = new File(cmd.getOptionValue("i").replaceFirst("^~", System.getProperty("user.home")));
System.out.println(elf.getAbsolutePath());
final ArrayList<Dwarf> dwarfArrayList = DwarfLib.init(elf, 0, false);
long address = 0x8000002al;
List<VariableWithAddress> variableWithAddresses = new ArrayList<VariableWithAddress>();
CompileUnit cu = DwarfLib.getCompileUnit(dwarfArrayList, address);
for (DwarfLine line : cu.dwarfDebugLineHeader.lines) {
if (line.address.compareTo(BigInteger.valueOf(address)) < 0) {
continue;
}
// System.out.println(line.file_num + " = " + cu.dwarfDebugLineHeader.filenames.get((int) line.file_num).file.getAbsolutePath() + "\t" + line);
VariableWithAddress variableWithAddress = new VariableWithAddress();
variableWithAddress.file_num = line.file_num;
variableWithAddress.line_num = line.line_num;
variableWithAddress.address = line.address;
variableWithAddresses.add(variableWithAddress);
}
for (DebugInfoEntry debugInfoEntry : cu.debugInfoEntries) {
for (DebugInfoEntry debugInfoEntry2 : debugInfoEntry.debugInfoEntries) {
if (debugInfoEntry2.name.equals("DW_TAG_variable")) {
} else if (debugInfoEntry2.name.equals("DW_TAG_subprogram")) {
for (DebugInfoEntry debugInfoEntry3 : debugInfoEntry2.debugInfoEntries) {
if (debugInfoEntry3.name.equals("DW_TAG_variable")) {
try {
// System.out.println("\t\t\t\t" + debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_name"));
// System.out.println("\t\t\t\t" + debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_decl_file"));
// System.out.println("\t\t\t\tclass=" + debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_decl_line"));
// System.out.println("\t\t\t\tclass=" + debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_decl_line").value.getClass());
int lineNo;
if (debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_decl_line").form == Definition.DW_FORM_data1) {
lineNo = Integer.parseInt(debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_decl_line").value.toString());
} else {
lineNo = Integer.parseInt(debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_decl_line").value.toString(), 16);
}
int colNo = Integer.parseInt(debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_decl_column").value.toString());
int fileNo = (int) debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_decl_file").value;
List<VariableWithAddress> result = variableWithAddresses.stream().filter(a -> a.file_num == fileNo).collect(Collectors.toList());
boolean check = false;
if (result.get(0).line_num <= lineNo && lineNo <= result.get(result.size() - 1).line_num) {
check = true;
}
if (!check) {
return;
}
String filePath = cu.dwarfDebugLineHeader.filenames.get((int) fileNo).file.getAbsolutePath();
List<String> lines = FileUtils.readLines(new File(filePath), Charset.defaultCharset());
System.out.printf("%x %s:%d \t= %s, %s\n", address, filePath, lineNo, debugInfoEntry3.debugInfoAbbrevEntries.get("DW_AT_name").value, lines.get(fileNo));
} catch (IOException ex) {
Logger.getLogger(QemuLogToMap.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
}