GHIDRA Plugin : line_disasm



Source:

// Dump the RAW disassembler output for the current line
//@author
//@category _NEW_
//@classification
//@keybinding
//@menupath
//@toolbar

import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.listing.*;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.pcode.PcodeOp;
import ghidra.util.Msg;
import ghidra.util.constraint.ProgramDecisionTree;
import ghidra.util.task.TaskMonitor;

import ghidra.app.script.ImproperUseException;
import ghidra.program.model.address.AddressRange;
import ghidra.program.model.mem.Memory;


public class line_disasm extends GhidraScript {

    public void run() throws Exception {

        outputToConsole("");
        outputToConsole("");
        outputToConsole("");
        outputToConsole("RAW disasm of selection:");
       
        if (currentSelection == null)
        {
            outputToConsole("Nothing selected!");
            return;
        }
        
        AddressRange addrRange = currentSelection.getFirstRange();

        if (addrRange == null)
        {
            outputToConsole("Nothing selected!");
            return;
        }

        Listing list = currentProgram.getListing();

        Address minAddress = addrRange.getMinAddress();
        Address maxAddress = addrRange.getMaxAddress();

        Instruction currentInstruction;
        String instructionString;
        String addrStr;
        
        Address currentAddress = minAddress;
        
        while ( currentAddress.compareTo(maxAddress) <= 0 )
        {
            currentInstruction = list.getInstructionAt(currentAddress);
            instructionString = currentInstruction.toString();
            instructionString = instructionString.replace(",", ", ");
            
            addrStr = currentInstruction.getAddress().toString(false);
            
            outputToConsole( addrStr + ": " + instructionString );
            
            currentAddress = currentAddress.add(currentInstruction.getLength());
        }
        
        outputToConsole("");

    }


    public void outputToConsole(String output)
    {
        println(output);
    }

}