init
import re
from ghidra.program.model.listing import CodeUnit
program = getCurrentProgram()
listing = program.getListing()
instructionIterator = listing.getInstructions(True)
instruction_set = set()
operand_set = set()
operand_pattern = re.compile(r'([A-Za-z]+)')
while instructionIterator.hasNext():
instr = instructionIterator.next()
mnemonic = instr.getMnemonicString()
instruction_set.add(mnemonic)
for i in range(instr.getNumOperands()):
operand = instr.getDefaultOperandRepresentation(i)
match = operand_pattern.match(operand)
if match:
basic_operand = match.group(1)
operand_set.add(basic_operand)
print("\nInstruction types:")
for instr in sorted(instruction_set):
print(instr)
print("\nOperand types:")
for operand in sorted(operand_set):
print(operand)
print("Script finished!")
add
import re
import json
import os
from ghidra.program.model.listing import CodeUnit
from ghidra.util import Msg
script_directory = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(script_directory, 'instruction_descriptions.json'), 'r') as f:
instruction_descriptions = json.load(f)
with open(os.path.join(script_directory, 'operand_descriptions.json'), 'r') as f:
operand_descriptions = json.load(f)
COMMENT_TYPE = CodeUnit.EOL_COMMENT
program = getCurrentProgram()
listing = program.getListing()
instructionIterator = listing.getInstructions(True)
while instructionIterator.hasNext():
instr = instructionIterator.next()
mnemonic = instr.getMnemonicString()
if mnemonic in instruction_descriptions:
operands = [instr.getDefaultOperandRepresentation(i) for i in range(instr.getNumOperands())]
description_operands = [operand_descriptions.get(op, op) for op in operands]
try:
CommentGenerate = instruction_descriptions[mnemonic].format(*description_operands)
instr.setComment(COMMENT_TYPE, CommentGenerate)
except KeyError:
print("Missing instruction description for: {}".format(mnemonic))
CommentGenerate = "{} {}".format(mnemonic, ' '.join(description_operands))
except IndexError:
print("Mismatch in operands for: {}".format(mnemonic))
CommentGenerate = "{} {}".format(mnemonic, ' '.join(description_operands))
print("Script finished!")
clean
from ghidra.program.model.listing import CodeUnit
from ghidra.util import Msg
program = getCurrentProgram()
listing = program.getListing()
instructionIterator = listing.getInstructions(True)
comment_types = [
CodeUnit.EOL_COMMENT,
CodeUnit.PRE_COMMENT,
CodeUnit.POST_COMMENT,
CodeUnit.REPEATABLE_COMMENT,
CodeUnit.PLATE_COMMENT
]
while instructionIterator.hasNext():
instr = instructionIterator.next()
for comment_type in comment_types:
if instr.getComment(comment_type) is not None:
instr.setComment(comment_type, None)
print("Cleared comment of type {} at {}".format(comment_type, instr.getAddress()))
print("Script finished!")
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2a8d49ae28d74fdcb5e3995e38f3c3e3.png)