summaryrefslogtreecommitdiff
path: root/rust/examples/basic_script/src/main.rs
blob: ae4554e86e39950fd8c6619df5cda7c5513a94b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use binaryninja::architecture::Architecture;
use binaryninja::binaryview::{BinaryViewBase, BinaryViewExt};

fn main() {
    println!("Loading plugins..."); // This loads all the core architecture, platform, etc plugins
    binaryninja::headless::init();

    println!("Loading binary...");
    let bv = binaryninja::open_view("/bin/cat").expect("Couldn't open `/bin/cat`");

    println!("Filename:  `{}`", bv.file().filename());
    println!("File size: `{:#x}`", bv.len());
    println!("Function count: {}", bv.functions().len());

    for func in &bv.functions() {
        println!("  `{}`:", func.symbol().full_name());
        for basic_block in &func.basic_blocks() {
            // TODO : This is intended to be refactored to be more nice to work with soon(TM)
            for addr in basic_block.as_ref() {
                print!("    {}  ", addr);
                match func.arch().instruction_text(
                    bv.read_buffer(addr, func.arch().max_instr_len())
                        .unwrap()
                        .get_data(),
                    addr,
                ) {
                    Some((_, tokens)) => {
                        tokens
                            .iter()
                            .for_each(|token| print!("{}", token.text().as_str()));
                        println!("")
                    }
                    _ => (),
                }
            }
        }
    }

    // Important!  You need to call shutdown or your script will hang forever
    binaryninja::headless::shutdown();
}