Overview
Theinstruction_iterator.py example shows how to:
- Iterate through all functions in a binary
- Navigate function basic blocks
- Access Low Level IL (LLIL) instructions
- Access machine code instructions
- Use proper logging for analysis output
Complete Source Code
Key Concepts Explained
The
bv.functions property returns an iterable of all functions discovered during analysis. Use repr(func) to get a detailed string representation including the function’s address and name.for block in func.low_level_il:
log_info("\t{0}".format(block))
for insn in block:
log_info("\t\t{0}".format(insn))
func.low_level_il - Returns the LLIL function objectfor block in func:
log_info("\t{0}".format(block))
for insn in block:
log_info("\t\t{0}".format(insn))
func is iterable and yields basic blocksLogLevel.DebugLog - Verbose debugging informationLogLevel.InfoLog - General informationLogLevel.WarningLog - Warnings onlyLogLevel.ErrorLog - Errors onlyRunning the Example
Command Line Mode
Plugin Mode
When loaded as a plugin:- Open a binary in Binary Ninja
- Run
Tools > Instruction Iterator - Check the Log window for output
Expected Output
Understanding the Output
LLIL Output Format
INDEX- IL instruction index within the functionADDRESS- Original address in the binaryINSTRUCTION- Human-readable IL operation
Machine Code Output Format
ADDRESS- Instruction address in the binaryDISASSEMBLY- Architecture-specific assembly instruction
Advanced Iteration Patterns
Filter Functions by Name
Count Instructions per Function
Access Both IL and Disassembly
Use Cases
- Static Analysis - Analyze all code paths in a binary
- Pattern Detection - Search for specific instruction sequences
- Code Coverage - Map all executable code
- Metrics Collection - Gather statistics about binary structure
- IL Learning - Understand Binary Ninja’s IL representations
Related Examples
- IL Parsing - Deep dive into IL expression parsing
- Custom IL Visitor - Advanced IL traversal patterns
- Basic Analysis - Foundation for binary analysis