summaryrefslogtreecommitdiff
path: root/rust/examples
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-12-19 19:15:38 -0500
committerMason Reed <35282038+emesare@users.noreply.github.com>2026-01-11 10:36:01 -0800
commit587a5c0c537a4b3bc5c737e3b3fc1fd5fbca3a59 (patch)
treea01357f3a1ad00bdf6ebde29dfae673da2f78a57 /rust/examples
parent6f75ca031aa7e8f7e1c706d1880b202137b1996f (diff)
[Rust] Update examples to use tracing
Diffstat (limited to 'rust/examples')
-rw-r--r--rust/examples/bndb_to_type_library.rs43
-rw-r--r--rust/examples/create_type_library.rs14
-rw-r--r--rust/examples/decompile.rs15
-rw-r--r--rust/examples/demangler.rs17
-rw-r--r--rust/examples/disassemble.rs15
-rw-r--r--rust/examples/dump_settings.rs15
-rw-r--r--rust/examples/dump_type_library.rs11
-rw-r--r--rust/examples/flowgraph.rs33
-rw-r--r--rust/examples/high_level_il.rs13
-rw-r--r--rust/examples/medium_level_il.rs13
-rw-r--r--rust/examples/simple.rs13
-rw-r--r--rust/examples/type_printer.rs7
-rw-r--r--rust/examples/workflow.rs17
13 files changed, 149 insertions, 77 deletions
diff --git a/rust/examples/bndb_to_type_library.rs b/rust/examples/bndb_to_type_library.rs
index 84515cf7..2cfaf83e 100644
--- a/rust/examples/bndb_to_type_library.rs
+++ b/rust/examples/bndb_to_type_library.rs
@@ -1,9 +1,21 @@
// Usage: cargo run --example bndb_to_type_library <bndb_path> <type_library_path>
use binaryninja::binary_view::BinaryViewExt;
+use binaryninja::tracing::TracingLogListener;
use binaryninja::types::{QualifiedName, TypeLibrary};
+use tracing_indicatif::span_ext::IndicatifSpanExt;
+use tracing_indicatif::style::ProgressStyle;
+use tracing_subscriber::layer::SubscriberExt;
+use tracing_subscriber::util::SubscriberInitExt;
fn main() {
+ let indicatif_layer = tracing_indicatif::IndicatifLayer::new();
+ tracing_subscriber::registry()
+ .with(tracing_subscriber::fmt::layer().with_writer(indicatif_layer.get_stderr_writer()))
+ .with(indicatif_layer)
+ .init();
+ let _listener = TracingLogListener::new().register();
+
let bndb_path_str = std::env::args().nth(1).expect("No header provided");
let bndb_path = std::path::Path::new(&bndb_path_str);
@@ -11,28 +23,43 @@ fn main() {
let type_lib_path = std::path::Path::new(&type_lib_path_str);
let type_lib_name = type_lib_path.file_stem().unwrap().to_str().unwrap();
- println!("Starting session...");
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- let file = headless_session
- .load(bndb_path)
- .expect("Failed to load BNDB");
+ let file = {
+ let loading_span = tracing::info_span!("loading");
+ let progress_style = ProgressStyle::with_template("{msg} {elapsed} {wide_bar}").unwrap();
+ loading_span.pb_set_style(&progress_style);
+ loading_span.pb_set_message("Loading database");
+ loading_span.pb_set_finish_message("Database loaded");
+ loading_span.in_scope(|| {
+ headless_session
+ .load_with_progress(bndb_path, |pos, total| {
+ loading_span.pb_set_length(total as u64);
+ loading_span.pb_set_position(pos as u64);
+ true
+ })
+ .expect("Failed to load BNDB")
+ })
+ };
let type_lib = TypeLibrary::new(file.default_arch().unwrap(), type_lib_name);
- for ty in &file.types() {
- println!("Adding type: {}", ty.name);
+ let types = file.types();
+ tracing::info!("Adding {} types", types.len());
+ for ty in &types {
type_lib.add_named_type(ty.name, &ty.ty);
}
- for func in &file.functions() {
- println!("Adding function: {}", func.symbol());
+ let functions = file.functions();
+ tracing::info!("Adding {} functions", functions.len());
+ for func in &functions {
let qualified_name =
QualifiedName::from(func.symbol().short_name().to_string_lossy().to_string());
type_lib.add_named_object(qualified_name, &func.function_type());
}
+ tracing::info!("Writing out file... {:?}", type_lib_path);
type_lib.write_to_file(&type_lib_path);
}
diff --git a/rust/examples/create_type_library.rs b/rust/examples/create_type_library.rs
index 74d93765..14c387a4 100644
--- a/rust/examples/create_type_library.rs
+++ b/rust/examples/create_type_library.rs
@@ -1,9 +1,13 @@
// Usage: cargo run --example create_type_library <header_file_path> <platform> <type_library_path>
use binaryninja::platform::Platform;
+use binaryninja::tracing::TracingLogListener;
use binaryninja::types::{CoreTypeParser, TypeLibrary, TypeParser};
fn main() {
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
let header_path_str = std::env::args().nth(1).expect("No header provided");
let header_path = std::path::Path::new(&header_path_str);
let header_name = header_path.file_stem().unwrap().to_str().unwrap();
@@ -14,7 +18,6 @@ fn main() {
let header_contents = std::fs::read_to_string(header_path).expect("Failed to read header file");
- println!("Starting session...");
// This loads all the core architecture, platform, etc plugins
let _headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
@@ -38,14 +41,19 @@ fn main() {
.expect("Parsed types");
for ty in parsed_types.types {
- println!("Adding type: {}", ty.name);
+ tracing::debug!("Adding type: {}", ty.name);
type_lib.add_named_type(ty.name, &ty.ty);
}
for func in parsed_types.functions {
- println!("Adding function: {}", func.name);
+ tracing::debug!("Adding function: {}", func.name);
type_lib.add_named_object(func.name, &func.ty);
}
+ tracing::info!(
+ "Created type library with {} types and {} functions",
+ type_lib.named_types().len(),
+ type_lib.named_objects().len()
+ );
type_lib.write_to_file(&type_lib_path);
}
diff --git a/rust/examples/decompile.rs b/rust/examples/decompile.rs
index 8bf3ecc3..6ba9cbf4 100644
--- a/rust/examples/decompile.rs
+++ b/rust/examples/decompile.rs
@@ -2,6 +2,7 @@ use binaryninja::binary_view::{BinaryView, BinaryViewBase, BinaryViewExt};
use binaryninja::disassembly::{DisassemblyOption, DisassemblySettings};
use binaryninja::function::Function;
use binaryninja::linear_view::LinearViewObject;
+use binaryninja::tracing::TracingLogListener;
fn decompile_to_c(view: &BinaryView, func: &Function) {
let settings = DisassemblySettings::new();
@@ -22,26 +23,28 @@ fn decompile_to_c(view: &BinaryView, func: &Function) {
let lines = first.into_iter().chain(&last);
for line in lines {
- println!("{}", line);
+ tracing::info!("{}", line);
}
}
pub fn main() {
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
let filename = std::env::args().nth(1).expect("No filename provided");
- println!("Starting session...");
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Loading binary...");
+ tracing::info!("Loading binary...");
let bv = headless_session
.load(&filename)
.expect("Couldn't open file!");
- println!("Filename: `{}`", bv.file().filename());
- println!("File size: `{:#x}`", bv.len());
- println!("Function count: {}", bv.functions().len());
+ tracing::info!("Filename: `{}`", bv.file().filename());
+ tracing::info!("File size: `{:#x}`", bv.len());
+ tracing::info!("Function count: {}", bv.functions().len());
for func in &bv.functions() {
decompile_to_c(bv.as_ref(), func.as_ref());
diff --git a/rust/examples/demangler.rs b/rust/examples/demangler.rs
index b2e5eb66..edefaec1 100644
--- a/rust/examples/demangler.rs
+++ b/rust/examples/demangler.rs
@@ -2,6 +2,7 @@ use binaryninja::architecture::CoreArchitecture;
use binaryninja::binary_view::BinaryView;
use binaryninja::demangle::{CustomDemangler, Demangler};
use binaryninja::rc::Ref;
+use binaryninja::tracing::TracingLogListener;
use binaryninja::types::{QualifiedName, Type};
struct TestDemangler;
@@ -26,32 +27,34 @@ impl CustomDemangler for TestDemangler {
}
fn main() {
- println!("Starting session...");
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
// This loads all the core architecture, platform, etc plugins
let _headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Registering demangler...");
+ tracing::info!("Registering demangler...");
Demangler::register("Test", TestDemangler);
let placeholder_arch = CoreArchitecture::by_name("x86_64").expect("x86 exists");
for d in Demangler::list().iter() {
- println!("{}", d.name());
+ tracing::info!("{}", d.name());
- println!(
+ tracing::info!(
" \"__ZN1AC2Ei\" is mangled? {}",
d.is_mangled_string("__ZN1AC2Ei")
);
- println!(
+ tracing::info!(
" \"__ZN1AC2Ei\" : {:?}",
d.demangle(&placeholder_arch, "__ZN1AC2Ei", None)
);
- println!(
+ tracing::info!(
" \"test_name\" : {:?}",
d.demangle(&placeholder_arch, "test_name", None)
);
- println!(
+ tracing::info!(
" \"test_name2\" : {:?}",
d.demangle(&placeholder_arch, "test_name2", None)
);
diff --git a/rust/examples/disassemble.rs b/rust/examples/disassemble.rs
index 2065c787..ce3ff765 100644
--- a/rust/examples/disassemble.rs
+++ b/rust/examples/disassemble.rs
@@ -1,6 +1,7 @@
use binaryninja::binary_view::{BinaryViewBase, BinaryViewExt};
use binaryninja::disassembly::{DisassemblyOption, DisassemblySettings, DisassemblyTextRenderer};
use binaryninja::function::Function;
+use binaryninja::tracing::TracingLogListener;
fn disassemble(func: &Function) {
let settings = DisassemblySettings::new();
@@ -17,28 +18,30 @@ fn disassemble(func: &Function) {
if let Some((text, _len)) = text_renderer.instruction_text(instr_addr) {
// TODO: This only ever appears to return a single string?
let text_string: Vec<_> = text.iter().map(|t| t.to_string()).collect();
- println!("{}", text_string.join(""));
+ tracing::info!("{}", text_string.join(""));
}
}
}
}
pub fn main() {
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
let filename = std::env::args().nth(1).expect("No filename provided");
- println!("Starting session...");
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Loading binary...");
+ tracing::info!("Loading binary...");
let bv = headless_session
.load(&filename)
.expect("Couldn't open file!");
- println!("Filename: `{}`", bv.file().filename());
- println!("File size: `{:#x}`", bv.len());
- println!("Function count: {}", bv.functions().len());
+ tracing::info!("Filename: `{}`", bv.file().filename());
+ tracing::info!("File size: `{:#x}`", bv.len());
+ tracing::info!("Function count: {}", bv.functions().len());
for func in &bv.functions() {
disassemble(func.as_ref());
diff --git a/rust/examples/dump_settings.rs b/rust/examples/dump_settings.rs
index 51452ca7..8c4af057 100644
--- a/rust/examples/dump_settings.rs
+++ b/rust/examples/dump_settings.rs
@@ -1,7 +1,10 @@
use binaryninja::settings::Settings;
+use binaryninja::tracing::TracingLogListener;
fn main() {
- println!("Starting session...");
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
// This loads all the core architecture, platform, etc plugins
let _headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
@@ -12,10 +15,10 @@ fn main() {
let default_value = settings.get_property_string(key, "default");
let title = settings.get_property_string(key, "title");
let description = settings.get_property_string(key, "description");
- println!("{}:", key);
- println!(" value: {}", value);
- println!(" default_value: {}", default_value);
- println!(" title: {}", title);
- println!(" description: {}", description);
+ tracing::info!("{}:", key);
+ tracing::info!(" value: {}", value);
+ tracing::info!(" default_value: {}", default_value);
+ tracing::info!(" title: {}", title);
+ tracing::info!(" description: {}", description);
}
}
diff --git a/rust/examples/dump_type_library.rs b/rust/examples/dump_type_library.rs
index f7398d28..9e6b19c2 100644
--- a/rust/examples/dump_type_library.rs
+++ b/rust/examples/dump_type_library.rs
@@ -2,21 +2,24 @@
use binaryninja::binary_view::BinaryView;
use binaryninja::file_metadata::FileMetadata;
+use binaryninja::tracing::TracingLogListener;
use binaryninja::types::library::TypeLibrary;
use binaryninja::types::printer::{CoreTypePrinter, TokenEscapingType};
fn main() {
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
let type_lib_str = std::env::args().nth(1).expect("No type library provided");
let type_lib_path = std::path::Path::new(&type_lib_str);
- println!("Starting session...");
// This loads all the core architecture, platform, etc plugins
let _headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
let type_lib = TypeLibrary::load_from_file(type_lib_path).expect("Failed to load type library");
- println!("Name: `{}`", type_lib.name());
- println!("GUID: `{}`", type_lib.guid());
+ tracing::info!("Name: `{}`", type_lib.name());
+ tracing::info!("GUID: `{}`", type_lib.guid());
// Print out all the types as a c header.
let type_lib_header_path = type_lib_path.with_extension("h");
@@ -27,7 +30,7 @@ fn main() {
.chain(type_lib.named_objects().iter())
.collect();
- println!(
+ tracing::info!(
"Dumping {} types to: `{:?}`",
all_types.len(),
type_lib_header_path
diff --git a/rust/examples/flowgraph.rs b/rust/examples/flowgraph.rs
index 75c854a7..cb03234e 100644
--- a/rust/examples/flowgraph.rs
+++ b/rust/examples/flowgraph.rs
@@ -6,6 +6,7 @@ use binaryninja::interaction::handler::{
register_interaction_handler, InteractionHandler, InteractionHandlerTask,
};
use binaryninja::interaction::{MessageBoxButtonResult, MessageBoxButtonSet, MessageBoxIcon};
+use binaryninja::tracing::TracingLogListener;
use binaryninja::{
architecture::BranchType,
binary_view::{BinaryView, BinaryViewExt},
@@ -17,22 +18,22 @@ pub struct GraphPrinter;
impl GraphPrinter {
pub fn print_graph(&self, graph: &FlowGraph) {
- println!("Printing flow graph:");
+ tracing::info!("Printing flow graph:");
for node in &graph.nodes() {
// Print all disassembly lines in the node
- println!("Node @ {:?}:", node.position());
- println!("------------------");
- println!("Disassembly lines:");
+ tracing::info!("Node @ {:?}:", node.position());
+ tracing::info!("------------------");
+ tracing::info!("Disassembly lines:");
for line in &node.lines() {
- println!(" {}", line);
+ tracing::info!(" {}", line);
}
// Print outgoing edges
- println!("Outgoing edges:");
+ tracing::info!("Outgoing edges:");
for edge in &node.outgoing_edges() {
- println!(" {:?} => {:?}", edge.branch_type, edge.target.position());
+ tracing::info!(" {:?} => {:?}", edge.branch_type, edge.target.position());
}
- println!("------------------");
+ tracing::info!("------------------");
}
}
}
@@ -62,14 +63,14 @@ impl InteractionHandler for GraphPrinter {
}
fn show_plain_text_report(&mut self, _view: Option<&BinaryView>, title: &str, contents: &str) {
- println!("Plain text report");
- println!("Title: {}", title);
- println!("Contents: {}", contents);
+ tracing::info!("Plain text report");
+ tracing::info!("Title: {}", title);
+ tracing::info!("Contents: {}", contents);
}
fn show_graph_report(&mut self, _view: Option<&BinaryView>, title: &str, graph: &FlowGraph) {
- println!("Graph report");
- println!("Title: {}", title);
+ tracing::info!("Graph report");
+ tracing::info!("Title: {}", title);
self.print_graph(graph);
}
@@ -115,12 +116,14 @@ fn test_graph() {
}
fn main() {
- println!("Starting session...");
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Loading binary...");
+ tracing::info!("Loading binary...");
let bv = headless_session
.load("/bin/cat")
.expect("Couldn't open `/bin/cat`");
diff --git a/rust/examples/high_level_il.rs b/rust/examples/high_level_il.rs
index ddc8f08d..a099437a 100644
--- a/rust/examples/high_level_il.rs
+++ b/rust/examples/high_level_il.rs
@@ -1,19 +1,22 @@
use binaryninja::binary_view::{BinaryViewBase, BinaryViewExt};
+use binaryninja::tracing::TracingLogListener;
fn main() {
- println!("Starting session...");
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Loading binary...");
+ tracing::info!("Loading binary...");
let bv = headless_session
.load("/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());
+ tracing::info!("Filename: `{}`", bv.file().filename());
+ tracing::info!("File size: `{:#x}`", bv.len());
+ tracing::info!("Function count: {}", bv.functions().len());
for func in &bv.functions() {
println!("{:?}:", func.symbol().full_name());
diff --git a/rust/examples/medium_level_il.rs b/rust/examples/medium_level_il.rs
index 543a0856..c9412d08 100644
--- a/rust/examples/medium_level_il.rs
+++ b/rust/examples/medium_level_il.rs
@@ -1,19 +1,22 @@
use binaryninja::binary_view::{BinaryViewBase, BinaryViewExt};
+use binaryninja::tracing::TracingLogListener;
fn main() {
- println!("Starting session...");
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Loading binary...");
+ tracing::info!("Loading binary...");
let bv = headless_session
.load("/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());
+ tracing::info!("Filename: `{}`", bv.file().filename());
+ tracing::info!("File size: `{:#x}`", bv.len());
+ tracing::info!("Function count: {}", bv.functions().len());
for func in &bv.functions() {
println!("{:?}:", func.symbol().full_name());
diff --git a/rust/examples/simple.rs b/rust/examples/simple.rs
index 66dbdfc0..c01ef74e 100644
--- a/rust/examples/simple.rs
+++ b/rust/examples/simple.rs
@@ -1,20 +1,23 @@
use binaryninja::architecture::Architecture;
use binaryninja::binary_view::{BinaryViewBase, BinaryViewExt};
+use binaryninja::tracing::TracingLogListener;
fn main() {
- println!("Starting session...");
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Loading binary...");
+ tracing::info!("Loading binary...");
let bv = headless_session
.load("/bin/cat")
.expect("Couldn't open `/bin/cat`");
- println!("File: `{}`", bv.file());
- println!("File size: `{:#x}`", bv.len());
- println!("Function count: {}", bv.functions().len());
+ tracing::info!("File: `{}`", bv.file());
+ tracing::info!("File size: `{:#x}`", bv.len());
+ tracing::info!("Function count: {}", bv.functions().len());
for func in &bv.functions() {
println!("{}:", func.symbol());
diff --git a/rust/examples/type_printer.rs b/rust/examples/type_printer.rs
index b88a0233..7407197e 100644
--- a/rust/examples/type_printer.rs
+++ b/rust/examples/type_printer.rs
@@ -1,13 +1,16 @@
+use binaryninja::tracing::TracingLogListener;
use binaryninja::types::printer::{CoreTypePrinter, TokenEscapingType};
use binaryninja::types::{MemberAccess, MemberScope, Structure, StructureMember, Type};
fn main() {
- println!("Starting session...");
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Loading binary...");
+ tracing::info!("Loading binary...");
let bv = headless_session
.load("/bin/cat")
.expect("Couldn't open `/bin/cat`");
diff --git a/rust/examples/workflow.rs b/rust/examples/workflow.rs
index d030209a..bfbbbdb2 100644
--- a/rust/examples/workflow.rs
+++ b/rust/examples/workflow.rs
@@ -2,6 +2,7 @@ use binaryninja::binary_view::BinaryViewExt;
use binaryninja::low_level_il::expression::{ExpressionHandler, LowLevelILExpressionKind};
use binaryninja::low_level_il::instruction::InstructionHandler;
use binaryninja::low_level_il::VisitorAction;
+use binaryninja::tracing::TracingLogListener;
use binaryninja::workflow::{Activity, AnalysisContext, Workflow};
const RUST_ACTIVITY_NAME: &str = "analysis.plugins.rustexample";
@@ -31,7 +32,11 @@ fn example_activity(analysis_context: &AnalysisContext) {
llil_instr.visit_tree(&mut |expr| {
if let LowLevelILExpressionKind::Const(_op) = expr.kind() {
// Replace all consts with 0x1337.
- println!("Replacing llil expression @ 0x{:x} : {}", instr, expr.index);
+ tracing::debug!(
+ "Replacing llil expression @ 0x{:x} : {}",
+ instr,
+ expr.index
+ );
unsafe {
llil.replace_expression(expr.index, llil.const_int(4, 0x1337))
};
@@ -46,12 +51,14 @@ fn example_activity(analysis_context: &AnalysisContext) {
}
pub fn main() {
- println!("Starting session...");
+ tracing_subscriber::fmt::init();
+ let _listener = TracingLogListener::new().register();
+
// This loads all the core architecture, platform, etc plugins
let headless_session =
binaryninja::headless::Session::new().expect("Failed to initialize session");
- println!("Registering workflow...");
+ tracing::info!("Registering workflow...");
let activity = Activity::new_with_action(RUST_ACTIVITY_CONFIG, example_activity);
// Update the meta-workflow with our new activity.
Workflow::cloned("core.function.metaAnalysis")
@@ -61,7 +68,7 @@ pub fn main() {
.register()
.expect("Couldn't register activity");
- println!("Loading binary...");
+ tracing::info!("Loading binary...");
let bv = headless_session
.load("/bin/cat")
.expect("Couldn't open `/bin/cat`");
@@ -74,7 +81,7 @@ pub fn main() {
instr.visit_tree(&mut |expr| {
if let LowLevelILExpressionKind::Const(value) = expr.kind() {
if value.value() == 0x1337 {
- println!(
+ tracing::info!(
"Found constant 0x1337 at instruction 0x{:x} in function {}",
instr.address(),
func.start()