From fabf7745ee58357f2cc494f5c529c1a4da91e4e1 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Mon, 20 Apr 2026 00:17:46 +0200 Subject: [PATCH] update ghidra marker import script (headless, import all types from all benchs) --- ghidra/scripts/ImportMarkersAsBookmarks.java | 104 +++++++++---------- 1 file changed, 49 insertions(+), 55 deletions(-) diff --git a/ghidra/scripts/ImportMarkersAsBookmarks.java b/ghidra/scripts/ImportMarkersAsBookmarks.java index 4fbd9e8..c3f0c15 100644 --- a/ghidra/scripts/ImportMarkersAsBookmarks.java +++ b/ghidra/scripts/ImportMarkersAsBookmarks.java @@ -1,8 +1,3 @@ -// CSV format per line: -// 0x401000, -// -// Creates NOTE bookmarks in category: FAIL_MARKER - import ghidra.app.script.GhidraScript; import ghidra.program.model.address.Address; import ghidra.program.model.listing.BookmarkManager; @@ -12,46 +7,63 @@ import java.io.FileReader; public class ImportMarkersAsBookmarks extends GhidraScript { - private static final String BOOKMARK_TYPE = "NOTE"; - private static final String BOOKMARK_CATEGORY = "FAIL_MARKER"; - @Override 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 "); + } + + 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(); int imported = 0; int skipped = 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))) { String line; while ((line = br.readLine()) != null) { lineNo++; - if (monitor.isCancelled()) { - println("Cancelled."); - break; - } - line = line.trim(); - if (line.isEmpty() || line.startsWith("#")) { + if (line.isEmpty() || line.startsWith("#") || line.startsWith("benchmark")) { continue; } - String[] parts = splitCsvLine(line); - if (parts.length < 1) { + // Make sure to always use "," delimiter and never quotations + String[] parts = line.split(","); + if (parts.length != 4) { skipped++; - printerr("Line " + lineNo + ": missing address"); + printerr("Line " + lineNo + ": malformed"); continue; } - String addrText = parts[0].trim(); - String description = parts.length >= 2 ? parts[1].trim() : ""; + String benchText = parts[0].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++; - printerr("Line " + lineNo + ": empty address"); + printerr("Line " + lineNo + ": malformed"); continue; } @@ -62,8 +74,21 @@ public class ImportMarkersAsBookmarks extends GhidraScript { continue; } - println("Adding bookmark at " + addr + " with description " + description); - bm.setBookmark(addr, BOOKMARK_TYPE, BOOKMARK_CATEGORY, description); + println( + "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++; } } @@ -77,10 +102,6 @@ public class ImportMarkersAsBookmarks extends GhidraScript { public Address parseAddress(String text) { 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")) { text = text.substring(2); } @@ -91,31 +112,4 @@ public class ImportMarkersAsBookmarks extends GhidraScript { return null; } } - - private String[] splitCsvLine(String line) { - java.util.ArrayList 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]); - } }