diff options
| author | Mason Reed <mason@vector35.com> | 2025-12-17 21:23:46 -0500 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2026-01-11 10:36:01 -0800 |
| commit | 168a3fd34824adc9c6a606cd144219701f15cccf (patch) | |
| tree | 9bbac31ece5ea6ab6627998d995a77f7f7e2f8e6 /rust/src/command.rs | |
| parent | ca91bc1933976c62d24248f0f7c35af38451ff11 (diff) | |
[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.
Diffstat (limited to 'rust/src/command.rs')
| -rw-r--r-- | rust/src/command.rs | 183 |
1 files changed, 62 insertions, 121 deletions
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<C: 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<C>(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::<C>), Some(cb_valid::<C>), ctxt as *mut _, @@ -197,42 +186,31 @@ pub fn register_command_for_address<C: AddressCommand>(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<C>(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::<C>), Some(cb_valid::<C>), 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<C>( @@ -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::<C>), Some(cb_valid::<C>), ctxt as *mut _, @@ -401,17 +368,13 @@ pub fn register_command_for_function<C: FunctionCommand>(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<C>( @@ -422,31 +385,22 @@ pub fn register_command_for_function<C: FunctionCommand>(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::<C>), Some(cb_valid::<C>), ctxt as *mut _, @@ -464,42 +418,29 @@ pub fn register_command_for_project<C: ProjectCommand>(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<C>(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::<C>), Some(cb_valid::<C>), ctxt as *mut _, |
