update ghidra marker import script (headless, import all types from all benchs)
This commit is contained in:
@ -1,8 +1,3 @@
|
|||||||
// CSV format per line:
|
|
||||||
// 0x401000,<count>
|
|
||||||
//
|
|
||||||
// Creates NOTE bookmarks in category: FAIL_MARKER
|
|
||||||
|
|
||||||
import ghidra.app.script.GhidraScript;
|
import ghidra.app.script.GhidraScript;
|
||||||
import ghidra.program.model.address.Address;
|
import ghidra.program.model.address.Address;
|
||||||
import ghidra.program.model.listing.BookmarkManager;
|
import ghidra.program.model.listing.BookmarkManager;
|
||||||
@ -12,46 +7,63 @@ import java.io.FileReader;
|
|||||||
|
|
||||||
public class ImportMarkersAsBookmarks extends GhidraScript {
|
public class ImportMarkersAsBookmarks extends GhidraScript {
|
||||||
|
|
||||||
private static final String BOOKMARK_TYPE = "NOTE";
|
|
||||||
private static final String BOOKMARK_CATEGORY = "FAIL_MARKER";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
File input = askFile("Select CSV file", "Import");
|
// Can't do this in headless mode
|
||||||
|
// File input = askFile("Select CSV file", "Import");
|
||||||
|
|
||||||
|
String[] args = getScriptArgs();
|
||||||
|
if (args.length < 1) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Missing CSV file path.\nUsage: ImportMarkersAsBookmarks <markers.csv>");
|
||||||
|
}
|
||||||
|
|
||||||
|
File input = new File(args[0]);
|
||||||
|
if (!input.isFile()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"CSV file does not exist or is not a regular file: " + input.getAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Importing bookmarks from: " + input.getAbsolutePath());
|
||||||
|
|
||||||
|
// https://ghidra.re/ghidra_docs/api/ghidra/program/model/listing/BookmarkManager.html
|
||||||
BookmarkManager bm = currentProgram.getBookmarkManager();
|
BookmarkManager bm = currentProgram.getBookmarkManager();
|
||||||
|
|
||||||
int imported = 0;
|
int imported = 0;
|
||||||
int skipped = 0;
|
int skipped = 0;
|
||||||
int lineNo = 0;
|
int lineNo = 0;
|
||||||
|
|
||||||
|
// CSV columns: (benchmark, resulttype, faults, fault_address)
|
||||||
|
// Example: (ip , OK_MARKER , 4311 , 0x10001B )
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(input))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(input))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = br.readLine()) != null) {
|
while ((line = br.readLine()) != null) {
|
||||||
lineNo++;
|
lineNo++;
|
||||||
|
|
||||||
if (monitor.isCancelled()) {
|
|
||||||
println("Cancelled.");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
if (line.isEmpty() || line.startsWith("#")) {
|
if (line.isEmpty() || line.startsWith("#") || line.startsWith("benchmark")) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] parts = splitCsvLine(line);
|
// Make sure to always use "," delimiter and never quotations
|
||||||
if (parts.length < 1) {
|
String[] parts = line.split(",");
|
||||||
|
if (parts.length != 4) {
|
||||||
skipped++;
|
skipped++;
|
||||||
printerr("Line " + lineNo + ": missing address");
|
printerr("Line " + lineNo + ": malformed");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String addrText = parts[0].trim();
|
String benchText = parts[0].trim();
|
||||||
String description = parts.length >= 2 ? parts[1].trim() : "";
|
String typeText = parts[1].trim();
|
||||||
|
String countText = parts[2].trim();
|
||||||
|
String addrText = parts[3].trim();
|
||||||
|
|
||||||
if (addrText.isEmpty()) {
|
if (benchText.isEmpty()
|
||||||
|
|| typeText.isEmpty()
|
||||||
|
|| countText.isEmpty()
|
||||||
|
|| addrText.isEmpty()) {
|
||||||
skipped++;
|
skipped++;
|
||||||
printerr("Line " + lineNo + ": empty address");
|
printerr("Line " + lineNo + ": malformed");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,8 +74,21 @@ public class ImportMarkersAsBookmarks extends GhidraScript {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Adding bookmark at " + addr + " with description " + description);
|
println(
|
||||||
bm.setBookmark(addr, BOOKMARK_TYPE, BOOKMARK_CATEGORY, description);
|
"Adding bookmark at "
|
||||||
|
+ addr
|
||||||
|
+ " with type "
|
||||||
|
+ benchText
|
||||||
|
+ " with category "
|
||||||
|
+ benchText // ip, mem or regs
|
||||||
|
+ " - "
|
||||||
|
+ typeText
|
||||||
|
+ " description "
|
||||||
|
+ countText
|
||||||
|
+ "x");
|
||||||
|
|
||||||
|
// TYPE CATEGORY DESCRIPTION
|
||||||
|
bm.setBookmark(addr, benchText, benchText + " - " + typeText, countText + "x");
|
||||||
imported++;
|
imported++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,10 +102,6 @@ public class ImportMarkersAsBookmarks extends GhidraScript {
|
|||||||
public Address parseAddress(String text) {
|
public Address parseAddress(String text) {
|
||||||
text = text.trim();
|
text = text.trim();
|
||||||
|
|
||||||
if (text.startsWith("\"") && text.endsWith("\"") && text.length() >= 2) {
|
|
||||||
text = text.substring(1, text.length() - 1).trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (text.startsWith("0x") || text.startsWith("0X")) {
|
if (text.startsWith("0x") || text.startsWith("0X")) {
|
||||||
text = text.substring(2);
|
text = text.substring(2);
|
||||||
}
|
}
|
||||||
@ -91,31 +112,4 @@ public class ImportMarkersAsBookmarks extends GhidraScript {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String[] splitCsvLine(String line) {
|
|
||||||
java.util.ArrayList<String> fields = new java.util.ArrayList<>();
|
|
||||||
StringBuilder current = new StringBuilder();
|
|
||||||
boolean inQuotes = false;
|
|
||||||
|
|
||||||
for (int i = 0; i < line.length(); i++) {
|
|
||||||
char c = line.charAt(i);
|
|
||||||
|
|
||||||
if (c == '"') {
|
|
||||||
if (inQuotes && i + 1 < line.length() && line.charAt(i + 1) == '"') {
|
|
||||||
current.append('"');
|
|
||||||
i++;
|
|
||||||
} else {
|
|
||||||
inQuotes = !inQuotes;
|
|
||||||
}
|
|
||||||
} else if (c == ',' && !inQuotes) {
|
|
||||||
fields.add(current.toString());
|
|
||||||
current.setLength(0);
|
|
||||||
} else {
|
|
||||||
current.append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fields.add(current.toString());
|
|
||||||
return fields.toArray(new String[0]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user