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
from ghidra. program. model. listing import CodeUnit
from ghidra. util import Msg
operand_descriptions = { "EBP" : u"基指针寄存器" , "ESP" : u"栈指针寄存器" . . .
}
instruction_descriptions = { "PUSH" : u"将 {} 入栈" , "POP" : u"将栈顶数据弹出到 {}" , . . .
}
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] CommentGenerate = instruction_descriptions[ mnemonic] . format ( * description_operands) CommentGenerate_lines = CommentGenerate. split( ", " ) formatted_CommentGenerate = "\n" . join( CommentGenerate_lines) instr. setComment( COMMENT_TYPE, formatted_CommentGenerate) Msg. info( None , "Added CommentGenerate to {} instruction at {}" . format ( mnemonic, instr. getAddress( ) ) )
Msg. info( None , "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 ) Msg. info( None , "Cleared comment of type {} at {}" . format ( comment_type, instr. getAddress( ) ) )
Msg. info( None , "Script finished!" )