diff options
| author | Josh Ferrell <josh@vector35.com> | 2025-10-12 15:58:05 -0400 |
|---|---|---|
| committer | Josh Ferrell <josh@vector35.com> | 2025-10-12 15:58:05 -0400 |
| commit | 49558b88ed5dacaac0bdf1946291bc37a4a88cb9 (patch) | |
| tree | 8a91157664f21f98566c456dbb3f850970ac52dd /plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs | |
| parent | 573b03c844090b53fb49b2156deae7540c8c3059 (diff) | |
Replace calls to unwrap() in dwarf_import
Diffstat (limited to 'plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs')
| -rw-r--r-- | plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs | 120 |
1 files changed, 85 insertions, 35 deletions
diff --git a/plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs b/plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs index 2dff2b35..0dc6b7fb 100644 --- a/plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs +++ b/plugins/dwarf/dwarf_import/src/dwarfdebuginfo.rs @@ -189,7 +189,18 @@ impl<R: ReaderType> DebugInfoBuilderContext<R> { .get(&get_uid( dwarf, entry_unit, - &entry_unit.entry(entry_offset).unwrap(), + match &entry_unit.entry(entry_offset) { + Ok(x) => x, + Err(e) => { + log::error!( + "Failed to get entry {:?} in unit {:?}: {}", + entry_offset, + entry_unit.header.offset(), + e + ); + return None; + } + }, )) .cloned(), DieReference::Err => None, @@ -252,11 +263,15 @@ impl DebugInfoBuilder { // update the function // if the full name exists, update the stored index for the full name if let Some(idx) = self.raw_function_name_indices.get(ident) { - let function = self.functions.get_mut(*idx).unwrap(); + let function = self.functions.get_mut(*idx).or_else(|| { + log::error!("Failed to get function with index {}", idx); + None + })?; - if function.full_name.is_some() && function.full_name != full_name { - self.full_function_name_indices - .remove(function.full_name.as_ref().unwrap()); + if function.full_name != full_name { + if let Some(existing_full_name) = &function.full_name { + self.full_function_name_indices.remove(existing_full_name); + } } function.update( @@ -268,9 +283,9 @@ impl DebugInfoBuilder { frame_base, ); - if function.full_name.is_some() { + if let Some(existing_full_name) = &function.full_name { self.full_function_name_indices - .insert(function.full_name.clone().unwrap(), *idx); + .insert(existing_full_name.clone(), *idx); } return Some(*idx); @@ -281,11 +296,15 @@ impl DebugInfoBuilder { // update the function // if the raw name exists, update the stored index for the raw name if let Some(idx) = self.full_function_name_indices.get(ident) { - let function = self.functions.get_mut(*idx).unwrap(); + let function = self.functions.get_mut(*idx).or_else(|| { + log::error!("Failed to get function with index {}", idx); + None + })?; - if function.raw_name.is_some() && function.raw_name != raw_name { - self.raw_function_name_indices - .remove(function.raw_name.as_ref().unwrap()); + if function.raw_name != raw_name { + if let Some(existing_raw_name) = &function.raw_name { + self.raw_function_name_indices.remove(existing_raw_name); + } } function.update( @@ -297,9 +316,9 @@ impl DebugInfoBuilder { frame_base, ); - if function.raw_name.is_some() { + if let Some(existing_raw_name) = &function.raw_name { self.raw_function_name_indices - .insert(function.raw_name.clone().unwrap(), *idx); + .insert(existing_raw_name.clone(), *idx); } return Some(*idx); @@ -412,10 +431,11 @@ impl DebugInfoBuilder { }; // Either get the known type or use a 0 confidence void type so we at least get the name applied - let ty = match type_uid { - Some(uid) => Conf::new(self.get_type(uid).unwrap().ty.clone(), 128), - None => Conf::new(Type::void(), 0), - }; + let ty = type_uid + .and_then(|uid| self.get_type(uid)) + .map(|t| Conf::new(t.ty.clone(), 128)) + .unwrap_or_else(|| Conf::new(Type::void(), 0)); + let function = &mut self.functions[function_index]; // TODO: If we can't find a known offset can we try to guess somehow? @@ -503,11 +523,32 @@ impl DebugInfoBuilder { if let Some((_existing_name, existing_type_uid)) = self.data_variables.insert(address, (name, type_uid)) { - let existing_type = self.get_type(existing_type_uid).unwrap().ty.as_ref(); - let new_type = self.get_type(type_uid).unwrap().ty.as_ref(); + let existing_type = match self.get_type(existing_type_uid) { + Some(x) => x.ty.as_ref(), + None => { + log::error!( + "Failed to find existing type with uid {} for data variable at {:#x}", + existing_type_uid, + address + ); + return; + } + }; + + let new_type = match self.get_type(type_uid) { + Some(x) => x.ty.as_ref(), + None => { + log::error!( + "Failed to find new type with uid {} for data variable at {:#x}", + type_uid, + address + ); + return; + } + }; if existing_type_uid != type_uid || existing_type != new_type { - warn!("DWARF info contains duplicate data variable definition. Overwriting data variable at 0x{:08x} (`{}`) with `{}`", + warn!("DWARF info contains duplicate data variable definition. Overwriting data variable at {:#08x} (`{}`) with `{}`", address, existing_type, new_type @@ -575,9 +616,16 @@ impl DebugInfoBuilder { // TODO : Consume data? fn commit_data_variables(&self, debug_info: &mut DebugInfo) { for (&address, (name, type_uid)) in &self.data_variables { + let data_var_type = match self.get_type(*type_uid) { + Some(x) => &x.ty, + None => { + log::error!("Failed to find type for data variable at {:#x}", address); + continue; + } + }; assert!(debug_info.add_data_variable( address, - &self.get_type(*type_uid).unwrap().ty, + data_var_type, name.as_deref(), &[] // TODO : Components )); @@ -585,24 +633,26 @@ impl DebugInfoBuilder { } fn get_function_type(&self, function: &FunctionInfoBuilder) -> Ref<Type> { - let return_type = match function.return_type { - Some(return_type_id) => { - Conf::new(self.get_type(return_type_id).unwrap().ty.clone(), 128) - } - _ => Conf::new(Type::void(), 0), - }; + let return_type = function + .return_type + .and_then(|return_type_id| self.get_type(return_type_id)) + .map(|t| Conf::new(t.ty.clone(), 128)) + .unwrap_or_else(|| Conf::new(Type::void(), 0)); let parameters: Vec<FunctionParameter> = function .parameters .iter() - .filter_map(|parameter| match parameter { - Some((name, 0)) => Some(FunctionParameter::new(Type::void(), name.clone(), None)), - Some((name, uid)) => Some(FunctionParameter::new( - self.get_type(*uid).unwrap().ty.clone(), - name.clone(), - None, - )), - _ => None, + .filter_map(|parameter| { + parameter.as_ref().map(|(name, uid)| { + let ty = match uid { + 0 => Type::void(), + uid => self + .get_type(*uid) + .map(|t| t.ty.clone()) + .unwrap_or_else(Type::void), + }; + FunctionParameter::new(ty, name.clone(), None) + }) }) .collect(); |
