summaryrefslogtreecommitdiffstats
path: root/debug/risc-v-gsp-rm-vm/ghidra/MarkGreenEyeHits.java
blob: b140ddd422e049a0875ebf33c268451b1f49df2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Annotate green-eye runtime hits in the current Ghidra program.
//
// Input: the TSV produced by greeneye.ghidra.export_hits():
//   addr <TAB> hits <TAB> symbol <TAB> annotation
//
// Usage (Ghidra headless):
//   analyzeHeadless <proj> <name> -process <program> -noanalysis \
//       -scriptPath debug/risc-v-gsp-rm-vm/ghidra \
//       -postScript MarkGreenEyeHits.java /abs/path/greeneye_hits.tsv
//
// For each address it adds a plate comment on the containing function
// ("green-eye: N hits") and a bookmark at the exact address.
//
//@category OpenIOV
//@menupath Tools.OpenIOV.Mark green-eye hits

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Function;

public class MarkGreenEyeHits extends GhidraScript {

    @Override
    public void run() throws Exception {
        String[] args = getScriptArgs();
        if (args.length < 1) {
            println("usage: MarkGreenEyeHits.java <greeneye_hits.tsv>");
            return;
        }
        File in = new File(args[0]);
        int marked = 0;
        int missed = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(in))) {
            String line;
            boolean header = true;
            while ((line = reader.readLine()) != null) {
                if (header) {
                    header = false;
                    if (line.startsWith("addr")) {
                        continue;
                    }
                }
                if (line.isBlank()) {
                    continue;
                }
                String[] cols = line.split("\t");
                if (cols.length < 2) {
                    continue;
                }
                long addrValue;
                try {
                    addrValue = Long.parseUnsignedLong(cols[0].replace("0x", ""), 16);
                } catch (NumberFormatException e) {
                    continue;
                }
                String hits = cols[1];
                String symbol = cols.length > 2 ? cols[2] : "";
                Address address = toAddr(addrValue);
                Function function = getFunctionContaining(address);
                if (function != null) {
                    String note = "green-eye: " + hits + " hit(s)"
                        + (symbol.isEmpty() ? "" : " (" + symbol + ")");
                    String existing = function.getComment();
                    function.setComment(existing == null ? note : existing + "\n" + note);
                    marked++;
                } else {
                    missed++;
                }
                createBookmark(address, "Note", "green-eye");
            }
        }
        println("green-eye: annotated " + marked + " functions (" + missed
            + " addresses outside any function) from " + in.getAbsolutePath());
    }
}