From 168a3fd34824adc9c6a606cd144219701f15cccf Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Wed, 17 Dec 2025 21:23:46 -0500 Subject: [Rust] Replace `log` with `tracing` - Added more documentation - Replaced global named logger for plugins, fixing the issue when the CU has multiple (e.g. statically linked demo) - Simplified some misc code This is a breaking change, but I believe there is no better time to make it, we cannot continue to use the `log` crate, it is too limited for our needs. --- rust/src/command.rs | 183 ++++++++++++++++++---------------------------------- 1 file changed, 62 insertions(+), 121 deletions(-) (limited to 'rust/src/command.rs') diff --git a/rust/src/command.rs b/rust/src/command.rs index 660e265b..9f585633 100644 --- a/rust/src/command.rs +++ b/rust/src/command.rs @@ -100,42 +100,31 @@ pub fn register_command(name: &str, desc: &str, command: C) { where C: Command, { - ffi_wrap!("Command::action", unsafe { - let cmd = &*(ctxt as *const C); - - debug_assert!(!view.is_null()); - let view = BinaryView { handle: view }; - - cmd.action(&view); - }) + let cmd = unsafe { &*(ctxt as *const C) }; + debug_assert!(!view.is_null()); + let view = BinaryView { handle: view }; + let _span = ffi_span!("Command::action", view); + cmd.action(&view); } extern "C" fn cb_valid(ctxt: *mut c_void, view: *mut BNBinaryView) -> bool where C: Command, { - ffi_wrap!("Command::valid", unsafe { - let cmd = &*(ctxt as *const C); - - debug_assert!(!view.is_null()); - let view = BinaryView { handle: view }; - - cmd.valid(&view) - }) + let cmd = unsafe { &*(ctxt as *const C) }; + debug_assert!(!view.is_null()); + let view = BinaryView { handle: view }; + let _span = ffi_span!("Command::valid", view); + cmd.valid(&view) } let name = name.to_cstr(); let desc = desc.to_cstr(); - - let name_ptr = name.as_ptr(); - let desc_ptr = desc.as_ptr(); - let ctxt = Box::into_raw(Box::new(command)); - unsafe { BNRegisterPluginCommand( - name_ptr, - desc_ptr, + name.as_ptr(), + desc.as_ptr(), Some(cb_action::), Some(cb_valid::), ctxt as *mut _, @@ -197,42 +186,31 @@ pub fn register_command_for_address(name: &str, desc: &str, c where C: AddressCommand, { - ffi_wrap!("AddressCommand::action", unsafe { - let cmd = &*(ctxt as *const C); - - debug_assert!(!view.is_null()); - let view = BinaryView { handle: view }; - - cmd.action(&view, addr); - }) + let cmd = unsafe { &*(ctxt as *const C) }; + debug_assert!(!view.is_null()); + let view = BinaryView { handle: view }; + let _span = ffi_span!("AddressCommand::action", view); + cmd.action(&view, addr); } extern "C" fn cb_valid(ctxt: *mut c_void, view: *mut BNBinaryView, addr: u64) -> bool where C: AddressCommand, { - ffi_wrap!("AddressCommand::valid", unsafe { - let cmd = &*(ctxt as *const C); - - debug_assert!(!view.is_null()); - let view = BinaryView { handle: view }; - - cmd.valid(&view, addr) - }) + let cmd = unsafe { &*(ctxt as *const C) }; + debug_assert!(!view.is_null()); + let view = BinaryView { handle: view }; + let _span = ffi_span!("AddressCommand::valid", view); + cmd.valid(&view, addr) } let name = name.to_cstr(); let desc = desc.to_cstr(); - - let name_ptr = name.as_ptr(); - let desc_ptr = desc.as_ptr(); - let ctxt = Box::into_raw(Box::new(command)); - unsafe { BNRegisterPluginCommandForAddress( - name_ptr, - desc_ptr, + name.as_ptr(), + desc.as_ptr(), Some(cb_action::), Some(cb_valid::), ctxt as *mut _, @@ -298,14 +276,11 @@ where where C: RangeCommand, { - ffi_wrap!("RangeCommand::action", unsafe { - let cmd = &*(ctxt as *const C); - - debug_assert!(!view.is_null()); - let view = BinaryView { handle: view }; - - cmd.action(&view, addr..addr.wrapping_add(len)); - }) + let cmd = unsafe { &*(ctxt as *const C) }; + debug_assert!(!view.is_null()); + let view = BinaryView { handle: view }; + let _span = ffi_span!("RangeCommand::action", view); + cmd.action(&view, addr..addr.wrapping_add(len)); } extern "C" fn cb_valid( @@ -317,28 +292,20 @@ where where C: RangeCommand, { - ffi_wrap!("RangeCommand::valid", unsafe { - let cmd = &*(ctxt as *const C); - - debug_assert!(!view.is_null()); - let view = BinaryView { handle: view }; - - cmd.valid(&view, addr..addr.wrapping_add(len)) - }) + let cmd = unsafe { &*(ctxt as *const C) }; + debug_assert!(!view.is_null()); + let view = BinaryView { handle: view }; + let _span = ffi_span!("RangeCommand::valid", view); + cmd.valid(&view, addr..addr.wrapping_add(len)) } let name = name.to_cstr(); let desc = desc.to_cstr(); - - let name_ptr = name.as_ptr(); - let desc_ptr = desc.as_ptr(); - let ctxt = Box::into_raw(Box::new(command)); - unsafe { BNRegisterPluginCommandForRange( - name_ptr, - desc_ptr, + name.as_ptr(), + desc.as_ptr(), Some(cb_action::), Some(cb_valid::), ctxt as *mut _, @@ -401,17 +368,13 @@ pub fn register_command_for_function(name: &str, desc: &str, where C: FunctionCommand, { - ffi_wrap!("FunctionCommand::action", unsafe { - let cmd = &*(ctxt as *const C); - - debug_assert!(!view.is_null()); - let view = BinaryView { handle: view }; - - debug_assert!(!func.is_null()); - let func = Function { handle: func }; - - cmd.action(&view, &func); - }) + let cmd = unsafe { &*(ctxt as *const C) }; + debug_assert!(!view.is_null()); + let view = BinaryView { handle: view }; + debug_assert!(!func.is_null()); + let func = Function { handle: func }; + let _span = ffi_span!("FunctionCommand::action", view); + cmd.action(&view, &func); } extern "C" fn cb_valid( @@ -422,31 +385,22 @@ pub fn register_command_for_function(name: &str, desc: &str, where C: FunctionCommand, { - ffi_wrap!("FunctionCommand::valid", unsafe { - let cmd = &*(ctxt as *const C); - - debug_assert!(!view.is_null()); - let view = BinaryView { handle: view }; - - debug_assert!(!func.is_null()); - let func = Function { handle: func }; - - cmd.valid(&view, &func) - }) + let cmd = unsafe { &*(ctxt as *const C) }; + debug_assert!(!view.is_null()); + let view = BinaryView { handle: view }; + debug_assert!(!func.is_null()); + let func = Function { handle: func }; + let _span = ffi_span!("FunctionCommand::valid", view); + cmd.valid(&view, &func) } let name = name.to_cstr(); let desc = desc.to_cstr(); - - let name_ptr = name.as_ptr(); - let desc_ptr = desc.as_ptr(); - let ctxt = Box::into_raw(Box::new(command)); - unsafe { BNRegisterPluginCommandForFunction( - name_ptr, - desc_ptr, + name.as_ptr(), + desc.as_ptr(), Some(cb_action::), Some(cb_valid::), ctxt as *mut _, @@ -464,42 +418,29 @@ pub fn register_command_for_project(name: &str, desc: &str, c where C: ProjectCommand, { - ffi_wrap!("Command::action", unsafe { - let cmd = &*(ctxt as *const C); - - let handle = NonNull::new(project).expect("project handle is null"); - let project = Project { handle }; - - cmd.action(&project); - }) + let cmd = unsafe { &*(ctxt as *const C) }; + let handle = NonNull::new(project).expect("project handle is null"); + let project = Project { handle }; + cmd.action(&project); } extern "C" fn cb_valid(ctxt: *mut c_void, project: *mut BNProject) -> bool where C: ProjectCommand, { - ffi_wrap!("Command::valid", unsafe { - let cmd = &*(ctxt as *const C); - - let handle = NonNull::new(project).expect("project handle is null"); - let project = Project { handle }; - - cmd.valid(&project) - }) + let cmd = unsafe { &*(ctxt as *const C) }; + let handle = NonNull::new(project).expect("project handle is null"); + let project = Project { handle }; + cmd.valid(&project) } let name = name.to_cstr(); let desc = desc.to_cstr(); - - let name_ptr = name.as_ptr(); - let desc_ptr = desc.as_ptr(); - let ctxt = Box::into_raw(Box::new(command)); - unsafe { BNRegisterPluginCommandForProject( - name_ptr, - desc_ptr, + name.as_ptr(), + desc.as_ptr(), Some(cb_action::), Some(cb_valid::), ctxt as *mut _, -- cgit v1.3.1