A robust Java bytecode disassembler and Control Flow Graph generator. This tool parses Java class files and generates visual representations of method logic using the ASM library and Graphviz.
- Bytecode Disassembly: Transform
.classfiles into human-readable bytecode. - Graph Generation: Automatically identify basic blocks and control flow edges.
- Multiple Formats: Export CFGs to DOT format or PDF.
- Comprehensive Support: Handles conditionals, loops, arrays, and object allocations.
| Java Source Code | Generated Control Flow Graph |
|---|---|
public int factorial(int n) {
int result = 1;
while (n > 1) {
result *= n;
n--;
}
return result;
} |
|
Key Components:
- S / X: Start and Exit nodes.
- Basic Blocks: Nodes containing sequential bytecode instructions.
- Edges: Paths of execution (labeled "T" for conditional branches).
- Java 21 or higher
- Graphviz (optional, required for PDF rendering)
- Gradle (included wrapper)
Try the included example to see the tool in action:
cd example
./run-example.shThis will:
- Compile
SimpleCalculator.java - Generate bytecode disassembly (
.asm.txtfiles) - Generate control flow graphs (
.dotfiles) - Convert to PDF (if Graphviz is installed)
The output will be in example/output/.
./gradlew run --args "/path/to/YourClass.class /path/to/output_dir"First, build the project:
./gradlew buildThen run the JAR with your class file:
java -cp build/libs/lab-03-lucadibello.jar:lib/asm-9.7.jar:lib/asm-tree-9.7.jar:lib/asm-util-9.7.jar \
lab.App /path/to/YourClass.class /path/to/outputThe tool generates two files per method in your specified output directory:
| File Extension | Description |
|---|---|
| .asm.txt | Disassembled bytecode with labels and stack maps. |
| .dot | Graphviz-compatible description of the CFG. |
- Bytecode Analysis: Utilizes the ASM library to traverse class structures and instruction sequences.
- Basic Block Construction: Identifies "leaders" (targets of jumps or instructions following jumps) to partition bytecode.
- Graph Mapping: Maps jumps (GOTO, IF_ICMP, etc.) to edges between blocks.
- Visualization: Outputs the resulting data structure into the DOT language for rendering.
.
├── example/
│ ├── SimpleCalculator.java # Example Java program
│ ├── run-example.sh # Script to compile and analyze the example
│ └── output/ # Generated CFGs and bytecode (after running)
├── src/
│ ├── main/java/lab/
│ │ ├── App.java # Main application entry point
│ │ ├── bytecode/ # Bytecode disassembler
│ │ └── cfg/ # Control flow graph generator
│ └── test/ # Test files
└── build.gradle # Gradle build configuration
This project is open source and available for personal and educational use.
