From 49591fd3e0cd37c8c829b792ead7182797f10e73 Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Fri, 10 Feb 2023 12:32:57 -0500 Subject: fixup instruction iterator to work headless and in the UI --- python/examples/instruction_iterator.py | 69 ++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 28 deletions(-) (limited to 'python/examples') diff --git a/python/examples/instruction_iterator.py b/python/examples/instruction_iterator.py index 86626970..741f03c4 100644 --- a/python/examples/instruction_iterator.py +++ b/python/examples/instruction_iterator.py @@ -20,31 +20,44 @@ # IN THE SOFTWARE. import sys -import binaryninja as binja - -if len(sys.argv) > 1: - target = sys.argv[1] - -bv = binja.BinaryViewType.get_view_of_file(target) -binja.log_to_stdout(True) -binja.log_info("-------- %s --------" % target) -binja.log_info("START: 0x%x" % bv.start) -binja.log_info("ENTRY: 0x%x" % bv.entry_point) -binja.log_info("ARCH: %s" % bv.arch.name) -binja.log_info("\n-------- Function List --------") -""" print all the functions, their basic blocks, and their il instructions """ -for func in bv.functions: - binja.log_info(repr(func)) - for block in func.low_level_il: - binja.log_info("\t{0}".format(block)) - - for insn in block: - binja.log_info("\t\t{0}".format(insn)) -""" print all the functions, their basic blocks, and their mc instructions """ -for func in bv.functions: - binja.log_info(repr(func)) - for block in func: - binja.log_info("\t{0}".format(block)) - - for insn in block: - binja.log_info("\t\t{0}".format(insn)) +from binaryninja.log import log_info, log_to_stdout +from binaryninja import open_view, BinaryView +from binaryninja import PluginCommand, LogLevel + + +def iterate(bv: BinaryView): + log_info("-------- %s --------" % bv.file.filename) + log_info("START: 0x%x" % bv.start) + log_info("ENTRY: 0x%x" % bv.entry_point) + log_info("ARCH: %s" % bv.arch.name) + log_info("\n-------- Function List --------") + """ print all the functions, their basic blocks, and their il instructions """ + for func in bv.functions: + log_info(repr(func)) + for block in func.low_level_il: + log_info("\t{0}".format(block)) + + for insn in block: + log_info("\t\t{0}".format(insn)) + """ print all the functions, their basic blocks, and their mc instructions """ + for func in bv.functions: + log_info(repr(func)) + for block in func: + log_info("\t{0}".format(block)) + + for insn in block: + log_info("\t\t{0}".format(insn)) + + +if __name__ == "__main__": + if len(sys.argv) > 1: + target = sys.argv[1] + log_to_stdout(LogLevel.WarningLog) + with open_view(target) as bv: + log_to_stdout(LogLevel.InfoLog) + iterate(bv) + else: + print(f"{sys.argv[0]} ") +else: + PluginCommand.register("Instruction Iterator", "Iterates Instruction to the log window", iterate) + -- cgit v1.3.1