summaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
authorGlenn Smith <glenn@vector35.com>2022-09-26 17:07:52 -0400
committerGlenn Smith <glenn@vector35.com>2022-09-29 21:02:25 -0400
commit571d4ed2f01c1117237b1a9684ad9b1d1ad8963a (patch)
treed63530c2510fc123496e7550625e7ebfc8c594f5 /rust/src
parentb6442a914783cb2861ff8d1cd6a3865fed29e0f7 (diff)
DebugInfo: make parseInfo have a progress callback
This will enable us to have progress bars and cancellation of debuginfo application
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/debuginfo.rs59
1 files changed, 54 insertions, 5 deletions
diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs
index 4e33e009..57e40f71 100644
--- a/rust/src/debuginfo.rs
+++ b/rust/src/debuginfo.rs
@@ -40,7 +40,7 @@
//! true
//! }
//!
-//! fn parse_info(&self, _debug_info: &mut DebugInfo, _view: &BinaryView) {
+//! fn parse_info(&self, _debug_info: &mut DebugInfo, _view: &BinaryView, _progress: Box<dyn Fn(usize, usize) -> bool>) {
//! println!("Parsing info");
//! }
//! }
@@ -76,8 +76,11 @@ use crate::{
types::{DataVariableAndName, NameAndType, Type},
};
+use std::mem::transmute;
use std::{hash::Hash, mem, os::raw::c_void, ptr, slice};
+struct ProgressContext(Option<Box<dyn Fn(usize, usize) -> Result<(), ()>>>);
+
//////////////////////
// DebugInfoParser
@@ -131,17 +134,43 @@ impl DebugInfoParser {
unsafe { BNIsDebugInfoParserValidForView(self.handle, view.handle) }
}
+ extern "C" fn cb_progress(ctxt: *mut c_void, cur: usize, max: usize) -> bool {
+ ffi_wrap!("DebugInfoParser::cb_progress", unsafe {
+ let progress = ctxt as *mut ProgressContext;
+ match &(*progress).0 {
+ Some(func) => (func)(cur, max).is_ok(),
+ None => true,
+ }
+ })
+ }
+
/// Returns a `DebugInfo` object populated with debug info by this debug-info parser. Only provide a `DebugInfo` object if you wish to append to the existing debug info
pub fn parse_debug_info(
&self,
view: &BinaryView,
existing_debug_info: Option<&DebugInfo>,
+ progress: Option<Box<dyn Fn(usize, usize) -> Result<(), ()>>>,
) -> Option<Ref<DebugInfo>> {
+ let mut progress_raw = ProgressContext(progress);
let info: *mut BNDebugInfo = match existing_debug_info {
Some(debug_info) => unsafe {
- BNParseDebugInfo(self.handle, view.handle, debug_info.handle)
+ BNParseDebugInfo(
+ self.handle,
+ view.handle,
+ debug_info.handle,
+ Some(Self::cb_progress),
+ &mut progress_raw as *mut _ as *mut c_void,
+ )
+ },
+ None => unsafe {
+ BNParseDebugInfo(
+ self.handle,
+ view.handle,
+ ptr::null_mut(),
+ Some(Self::cb_progress),
+ &mut progress_raw as *mut _ as *mut c_void,
+ )
},
- None => unsafe { BNParseDebugInfo(self.handle, view.handle, ptr::null_mut()) },
};
if info.is_null() {
return None;
@@ -171,6 +200,8 @@ impl DebugInfoParser {
ctxt: *mut c_void,
debug_info: *mut BNDebugInfo,
view: *mut BNBinaryView,
+ progress: Option<unsafe extern "C" fn(*mut c_void, usize, usize) -> bool>,
+ progress_ctxt: *mut c_void,
) -> bool
where
C: CustomDebugInfoParser,
@@ -180,7 +211,20 @@ impl DebugInfoParser {
let view = BinaryView::from_raw(view);
let mut debug_info = DebugInfo::from_raw(debug_info);
- cmd.parse_info(&mut debug_info, &view)
+ cmd.parse_info(
+ &mut debug_info,
+ &view,
+ Box::new(move |cur: usize, max: usize| match progress {
+ Some(func) => {
+ if func(progress_ctxt, cur, max) {
+ Ok(())
+ } else {
+ Err(())
+ }
+ }
+ _ => Ok(()),
+ }),
+ )
})
}
@@ -787,5 +831,10 @@ impl ToOwned for DebugInfo {
/// Implement this trait to implement a debug info parser. See `DebugInfoParser` for more details.
pub trait CustomDebugInfoParser: 'static + Sync {
fn is_valid(&self, view: &BinaryView) -> bool;
- fn parse_info(&self, debug_info: &mut DebugInfo, view: &BinaryView) -> bool;
+ fn parse_info(
+ &self,
+ debug_info: &mut DebugInfo,
+ view: &BinaryView,
+ progress: Box<dyn Fn(usize, usize) -> Result<(), ()>>,
+ ) -> bool;
}