Files
objdiff/objdiff-gui/src/views/symbol_diff.rs
T

831 lines
32 KiB
Rust
Raw Normal View History

use std::{collections::BTreeMap, mem::take, ops::Bound};
2023-08-12 14:18:09 -04:00
2022-09-08 17:19:20 -04:00
use egui::{
2025-02-02 17:32:08 -07:00
style::ScrollAnimation, text::LayoutJob, CollapsingHeader, Color32, Id, OpenUrl, ScrollArea,
SelectableLabel, Ui, Widget,
2022-09-08 17:19:20 -04:00
};
use objdiff_core::{
2024-09-09 19:43:10 -06:00
arch::ObjArch,
2024-10-09 21:44:18 -06:00
diff::{display::HighlightKind, ObjDiff, ObjSymbolDiff},
2024-10-11 18:37:14 -06:00
jobs::{create_scratch::CreateScratchResult, objdiff::ObjDiffResult, Job, JobQueue, JobResult},
2024-10-28 17:54:49 -06:00
obj::{
ObjInfo, ObjSection, ObjSectionKind, ObjSymbol, ObjSymbolFlags, SymbolRef, SECTION_COMMON,
},
};
2024-07-21 23:01:58 -06:00
use regex::{Regex, RegexBuilder};
2022-09-08 17:19:20 -04:00
use crate::{
2024-09-28 10:55:22 -06:00
app::AppStateRef,
hotkeys,
2024-10-11 18:37:14 -06:00
jobs::{is_create_scratch_available, start_create_scratch},
2025-02-02 17:32:08 -07:00
views::{appearance::Appearance, function_diff::FunctionViewState, write_text},
2022-09-08 17:19:20 -04:00
};
2025-02-02 17:32:08 -07:00
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SymbolRefByName {
pub symbol_name: String,
2024-10-09 21:44:18 -06:00
pub section_name: Option<String>,
}
2024-10-09 21:44:18 -06:00
impl SymbolRefByName {
pub fn new(symbol: &ObjSymbol, section: Option<&ObjSection>) -> Self {
Self { symbol_name: symbol.name.clone(), section_name: section.map(|s| s.name.clone()) }
}
}
#[expect(clippy::enum_variant_names)]
#[derive(Debug, Default, Eq, PartialEq, Copy, Clone, Hash)]
pub enum View {
#[default]
SymbolDiff,
FunctionDiff,
DataDiff,
2024-07-22 00:25:54 -04:00
ExtabDiff,
}
2024-10-09 21:44:18 -06:00
#[derive(Debug, Clone)]
pub enum DiffViewAction {
/// Queue a rebuild of the current object(s)
Build,
/// Navigate to a new diff view
Navigate(DiffViewNavigation),
/// Set the highlighted symbols in the symbols view, optionally scrolling them into view.
SetSymbolHighlight(Option<SymbolRef>, Option<SymbolRef>, bool),
2024-10-09 21:44:18 -06:00
/// Set the symbols view search filter
SetSearch(String),
/// Submit the current function to decomp.me
CreateScratch(String),
/// Open the source path of the current object
OpenSourcePath,
/// Set the highlight for a diff column
SetDiffHighlight(usize, HighlightKind),
/// Clear the highlight for all diff columns
ClearDiffHighlight,
/// Start selecting a left symbol for mapping.
/// The symbol reference is the right symbol to map to.
SelectingLeft(SymbolRefByName),
/// Start selecting a right symbol for mapping.
/// The symbol reference is the left symbol to map to.
SelectingRight(SymbolRefByName),
/// Set a symbol mapping.
SetMapping(View, SymbolRefByName, SymbolRefByName),
/// Set the show_mapped_symbols flag
SetShowMappedSymbols(bool),
}
2025-02-02 17:32:08 -07:00
#[derive(Debug, Clone, Default, Eq, PartialEq)]
2024-10-09 21:44:18 -06:00
pub struct DiffViewNavigation {
2025-02-02 17:32:08 -07:00
pub view: View,
2024-10-09 21:44:18 -06:00
pub left_symbol: Option<SymbolRefByName>,
pub right_symbol: Option<SymbolRefByName>,
}
impl DiffViewNavigation {
pub fn symbol_diff() -> Self {
2025-02-02 17:32:08 -07:00
Self { view: View::SymbolDiff, left_symbol: None, right_symbol: None }
2024-10-09 21:44:18 -06:00
}
pub fn with_symbols(
view: View,
other_ctx: Option<SymbolDiffContext<'_>>,
symbol: &ObjSymbol,
section: &ObjSection,
symbol_diff: &ObjSymbolDiff,
column: usize,
) -> Self {
let symbol1 = Some(SymbolRefByName::new(symbol, Some(section)));
let symbol2 = symbol_diff.target_symbol.and_then(|symbol_ref| {
other_ctx.map(|ctx| {
let (section, symbol) = ctx.obj.section_symbol(symbol_ref);
SymbolRefByName::new(symbol, section)
})
});
match column {
2025-02-02 17:32:08 -07:00
0 => Self { view, left_symbol: symbol1, right_symbol: symbol2 },
1 => Self { view, left_symbol: symbol2, right_symbol: symbol1 },
_ => unreachable!("Invalid column index"),
}
}
pub fn data_diff(section: &ObjSection, column: usize) -> Self {
let symbol = Some(SymbolRefByName {
symbol_name: "".to_string(),
section_name: Some(section.name.clone()),
});
match column {
0 => Self {
view: View::DataDiff,
left_symbol: symbol.clone(),
right_symbol: symbol.clone(),
},
1 => Self { view: View::DataDiff, left_symbol: symbol.clone(), right_symbol: symbol },
2024-10-09 21:44:18 -06:00
_ => unreachable!("Invalid column index"),
}
}
}
#[derive(Default)]
pub struct DiffViewState {
pub build: Option<Box<ObjDiffResult>>,
2024-01-20 22:53:40 -07:00
pub scratch: Option<Box<CreateScratchResult>>,
pub current_view: View,
2023-08-12 14:18:09 -04:00
pub symbol_state: SymbolViewState,
2023-10-05 23:40:45 -04:00
pub function_state: FunctionViewState,
2023-08-12 14:18:09 -04:00
pub search: String,
2024-07-21 23:01:58 -06:00
pub search_regex: Option<Regex>,
2023-08-12 14:18:09 -04:00
pub build_running: bool,
2024-01-20 22:53:40 -07:00
pub scratch_available: bool,
pub scratch_running: bool,
2024-09-28 10:55:25 -06:00
pub source_path_available: bool,
2024-10-09 21:44:18 -06:00
pub post_build_nav: Option<DiffViewNavigation>,
pub object_name: String,
2023-08-12 14:18:09 -04:00
}
#[derive(Default)]
pub struct SymbolViewState {
pub highlighted_symbol: (Option<SymbolRef>, Option<SymbolRef>),
pub autoscroll_to_highlighted_symbols: bool,
2024-10-09 21:44:18 -06:00
pub left_symbol: Option<SymbolRefByName>,
pub right_symbol: Option<SymbolRefByName>,
2023-08-12 14:18:09 -04:00
pub reverse_fn_order: bool,
pub disable_reverse_fn_order: bool,
pub show_hidden_symbols: bool,
2024-10-09 21:44:18 -06:00
pub show_mapped_symbols: bool,
2023-08-12 14:18:09 -04:00
}
impl DiffViewState {
2024-09-28 10:55:22 -06:00
pub fn pre_update(&mut self, jobs: &mut JobQueue, state: &AppStateRef) {
2024-01-20 22:53:40 -07:00
jobs.results.retain_mut(|result| match result {
JobResult::ObjDiff(result) => {
2023-08-12 14:18:09 -04:00
self.build = take(result);
2024-10-09 21:44:18 -06:00
// TODO: where should this go?
if let Some(result) = self.post_build_nav.take() {
2025-02-02 17:32:08 -07:00
self.current_view = result.view;
2024-10-09 21:44:18 -06:00
self.symbol_state.left_symbol = result.left_symbol;
self.symbol_state.right_symbol = result.right_symbol;
}
2023-08-12 14:18:09 -04:00
false
}
2024-01-20 22:53:40 -07:00
JobResult::CreateScratch(result) => {
self.scratch = take(result);
false
}
_ => true,
2023-08-12 14:18:09 -04:00
});
self.build_running = jobs.is_running(Job::ObjDiff);
2024-01-20 22:53:40 -07:00
self.scratch_running = jobs.is_running(Job::CreateScratch);
2023-08-12 14:18:09 -04:00
self.symbol_state.disable_reverse_fn_order = false;
2024-09-28 10:55:22 -06:00
if let Ok(state) = state.read() {
if let Some(obj_config) = &state.config.selected_obj {
2023-09-03 09:28:22 -04:00
if let Some(value) = obj_config.reverse_fn_order {
self.symbol_state.reverse_fn_order = value;
self.symbol_state.disable_reverse_fn_order = true;
2023-08-12 14:18:09 -04:00
}
2024-09-28 10:55:25 -06:00
self.source_path_available = obj_config.source_path.is_some();
} else {
self.source_path_available = false;
2023-08-12 14:18:09 -04:00
}
2024-10-11 18:37:14 -06:00
self.scratch_available = is_create_scratch_available(&state.config);
2024-10-09 21:44:18 -06:00
self.object_name =
state.config.selected_obj.as_ref().map(|o| o.name.clone()).unwrap_or_default();
2023-08-12 14:18:09 -04:00
}
}
2024-10-09 21:44:18 -06:00
pub fn post_update(
&mut self,
action: Option<DiffViewAction>,
ctx: &egui::Context,
jobs: &mut JobQueue,
state: &AppStateRef,
) {
2024-01-20 22:53:40 -07:00
if let Some(result) = take(&mut self.scratch) {
ctx.output_mut(|o| o.open_url = Some(OpenUrl::new_tab(result.scratch_url)));
}
// Clear the autoscroll flag so that it doesn't scroll continuously.
self.symbol_state.autoscroll_to_highlighted_symbols = false;
2024-10-09 21:44:18 -06:00
let Some(action) = action else {
return;
};
match action {
DiffViewAction::Build => {
if let Ok(mut state) = state.write() {
state.queue_build = true;
}
2023-08-12 14:18:09 -04:00
}
2024-10-09 21:44:18 -06:00
DiffViewAction::Navigate(nav) => {
if self.post_build_nav.is_some() {
// Ignore action if we're already navigating
return;
}
let Ok(mut state) = state.write() else {
return;
};
if (nav.left_symbol.is_some() && nav.right_symbol.is_some())
|| (nav.left_symbol.is_none() && nav.right_symbol.is_none())
2025-02-02 17:32:08 -07:00
|| nav.view != View::FunctionDiff
2024-10-09 21:44:18 -06:00
{
// Regular navigation
if state.is_selecting_symbol() {
// Cancel selection and reload
state.clear_selection();
self.post_build_nav = Some(nav);
} else {
// Navigate immediately
2025-02-02 17:32:08 -07:00
self.current_view = nav.view;
2024-10-09 21:44:18 -06:00
self.symbol_state.left_symbol = nav.left_symbol;
self.symbol_state.right_symbol = nav.right_symbol;
}
} else {
// Enter selection mode
match (&nav.left_symbol, &nav.right_symbol) {
(Some(left_ref), None) => {
state.set_selecting_right(&left_ref.symbol_name);
2024-01-20 22:53:40 -07:00
}
2024-10-09 21:44:18 -06:00
(None, Some(right_ref)) => {
state.set_selecting_left(&right_ref.symbol_name);
}
(Some(_), Some(_)) => unreachable!(),
(None, None) => unreachable!(),
}
self.post_build_nav = Some(nav);
}
}
DiffViewAction::SetSymbolHighlight(left, right, autoscroll) => {
2024-10-09 21:44:18 -06:00
self.symbol_state.highlighted_symbol = (left, right);
self.symbol_state.autoscroll_to_highlighted_symbols = autoscroll;
2024-10-09 21:44:18 -06:00
}
DiffViewAction::SetSearch(search) => {
self.search_regex = if search.is_empty() {
None
} else {
2025-02-07 00:15:21 -07:00
RegexBuilder::new(&search).case_insensitive(true).build().ok()
2024-10-09 21:44:18 -06:00
};
self.search = search;
}
DiffViewAction::CreateScratch(function_name) => {
let Ok(state) = state.read() else {
return;
};
2024-10-11 18:37:14 -06:00
start_create_scratch(ctx, jobs, &state, function_name);
2024-01-20 22:53:40 -07:00
}
2024-10-09 21:44:18 -06:00
DiffViewAction::OpenSourcePath => {
let Ok(state) = state.read() else {
return;
};
2025-02-07 00:10:49 -07:00
if let Some(source_path) =
state.config.selected_obj.as_ref().and_then(|obj| obj.source_path.as_ref())
{
log::info!("Opening file {}", source_path);
2024-09-28 10:55:25 -06:00
open::that_detached(source_path).unwrap_or_else(|err| {
log::error!("Failed to open source file: {err}");
});
}
}
2024-10-09 21:44:18 -06:00
DiffViewAction::SetDiffHighlight(column, kind) => {
self.function_state.set_highlight(column, kind);
}
DiffViewAction::ClearDiffHighlight => {
self.function_state.clear_highlight();
}
DiffViewAction::SelectingLeft(right_ref) => {
if self.post_build_nav.is_some() {
// Ignore action if we're already navigating
return;
}
let Ok(mut state) = state.write() else {
return;
};
state.set_selecting_left(&right_ref.symbol_name);
self.post_build_nav = Some(DiffViewNavigation {
2025-02-02 17:32:08 -07:00
view: View::FunctionDiff,
2024-10-09 21:44:18 -06:00
left_symbol: None,
right_symbol: Some(right_ref),
});
}
DiffViewAction::SelectingRight(left_ref) => {
if self.post_build_nav.is_some() {
// Ignore action if we're already navigating
return;
}
let Ok(mut state) = state.write() else {
return;
};
state.set_selecting_right(&left_ref.symbol_name);
self.post_build_nav = Some(DiffViewNavigation {
2025-02-02 17:32:08 -07:00
view: View::FunctionDiff,
2024-10-09 21:44:18 -06:00
left_symbol: Some(left_ref),
right_symbol: None,
});
}
DiffViewAction::SetMapping(view, left_ref, right_ref) => {
if self.post_build_nav.is_some() {
// Ignore action if we're already navigating
return;
}
let Ok(mut state) = state.write() else {
return;
};
state.set_symbol_mapping(
left_ref.symbol_name.clone(),
right_ref.symbol_name.clone(),
);
if view == View::SymbolDiff {
self.post_build_nav = Some(DiffViewNavigation::symbol_diff());
} else {
self.post_build_nav = Some(DiffViewNavigation {
2025-02-02 17:32:08 -07:00
view,
2024-10-09 21:44:18 -06:00
left_symbol: Some(left_ref),
right_symbol: Some(right_ref),
});
}
}
DiffViewAction::SetShowMappedSymbols(value) => {
self.symbol_state.show_mapped_symbols = value;
}
2024-09-28 10:55:25 -06:00
}
2023-08-12 14:18:09 -04:00
}
}
pub fn match_color_for_symbol(match_percent: f32, appearance: &Appearance) -> Color32 {
2022-12-06 17:53:32 -05:00
if match_percent == 100.0 {
appearance.insert_color
2022-12-06 17:53:32 -05:00
} else if match_percent >= 50.0 {
appearance.replace_color
2022-09-08 17:19:20 -04:00
} else {
appearance.delete_color
2022-09-08 17:19:20 -04:00
}
}
2024-07-22 00:25:54 -04:00
fn symbol_context_menu_ui(
ui: &mut Ui,
2024-10-09 21:44:18 -06:00
ctx: SymbolDiffContext<'_>,
other_ctx: Option<SymbolDiffContext<'_>>,
2024-07-22 00:25:54 -04:00
symbol: &ObjSymbol,
2024-10-09 21:44:18 -06:00
symbol_diff: &ObjSymbolDiff,
2024-07-22 00:25:54 -04:00
section: Option<&ObjSection>,
2024-10-09 21:44:18 -06:00
column: usize,
) -> Option<DiffViewNavigation> {
2024-09-09 19:43:10 -06:00
let mut ret = None;
2022-09-14 13:12:45 -04:00
ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
2024-09-09 19:26:46 -06:00
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
2022-09-14 13:12:45 -04:00
if let Some(name) = &symbol.demangled_name {
2022-12-06 18:09:19 -05:00
if ui.button(format!("Copy \"{name}\"")).clicked() {
ui.output_mut(|output| output.copied_text.clone_from(name));
2022-09-14 13:12:45 -04:00
ui.close_menu();
}
}
if ui.button(format!("Copy \"{}\"", symbol.name)).clicked() {
ui.output_mut(|output| output.copied_text.clone_from(&symbol.name));
2022-09-14 13:12:45 -04:00
ui.close_menu();
}
if let Some(address) = symbol.virtual_address {
if ui.button(format!("Copy \"{:#x}\" (virtual address)", address)).clicked() {
ui.output_mut(|output| output.copied_text = format!("{:#x}", address));
ui.close_menu();
}
}
2024-07-22 00:25:54 -04:00
if let Some(section) = section {
2024-10-09 21:44:18 -06:00
let has_extab =
ctx.obj.arch.ppc().and_then(|ppc| ppc.extab_for_symbol(symbol)).is_some();
2024-09-09 19:43:10 -06:00
if has_extab && ui.button("Decode exception table").clicked() {
2024-10-09 21:44:18 -06:00
ret = Some(DiffViewNavigation::with_symbols(
View::ExtabDiff,
other_ctx,
symbol,
section,
symbol_diff,
column,
));
ui.close_menu();
}
if ui.button("Map symbol").clicked() {
let symbol_ref = SymbolRefByName::new(symbol, Some(section));
if column == 0 {
ret = Some(DiffViewNavigation {
2025-02-02 17:32:08 -07:00
view: View::FunctionDiff,
2024-10-09 21:44:18 -06:00
left_symbol: Some(symbol_ref),
right_symbol: None,
});
} else {
ret = Some(DiffViewNavigation {
2025-02-02 17:32:08 -07:00
view: View::FunctionDiff,
2024-10-09 21:44:18 -06:00
left_symbol: None,
right_symbol: Some(symbol_ref),
});
}
2024-07-22 00:25:54 -04:00
ui.close_menu();
}
}
2022-09-14 13:12:45 -04:00
});
2024-09-09 19:43:10 -06:00
ret
2022-09-14 13:12:45 -04:00
}
2024-09-09 19:43:10 -06:00
fn symbol_hover_ui(ui: &mut Ui, arch: &dyn ObjArch, symbol: &ObjSymbol, appearance: &Appearance) {
2022-09-14 13:12:45 -04:00
ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
2024-09-09 19:26:46 -06:00
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
2022-09-14 13:12:45 -04:00
ui.colored_label(appearance.highlight_color, format!("Name: {}", symbol.name));
ui.colored_label(appearance.highlight_color, format!("Address: {:x}", symbol.address));
2022-12-06 17:53:32 -05:00
if symbol.size_known {
ui.colored_label(appearance.highlight_color, format!("Size: {:x}", symbol.size));
2022-12-06 17:53:32 -05:00
} else {
ui.colored_label(
appearance.highlight_color,
format!("Size: {:x} (assumed)", symbol.size),
);
2022-12-06 17:53:32 -05:00
}
if let Some(address) = symbol.virtual_address {
ui.colored_label(appearance.replace_color, format!("Virtual address: {:#x}", address));
}
2024-09-09 19:43:10 -06:00
if let Some(extab) = arch.ppc().and_then(|ppc| ppc.extab_for_symbol(symbol)) {
ui.colored_label(
appearance.highlight_color,
format!("extab symbol: {}", &extab.etb_symbol.name),
);
ui.colored_label(
appearance.highlight_color,
format!("extabindex symbol: {}", &extab.eti_symbol.name),
);
2024-07-22 00:25:54 -04:00
}
2022-09-14 13:12:45 -04:00
});
}
2023-08-12 14:18:09 -04:00
#[must_use]
2024-10-09 21:44:18 -06:00
#[expect(clippy::too_many_arguments)]
2022-09-08 17:19:20 -04:00
fn symbol_ui(
ui: &mut Ui,
2024-10-09 21:44:18 -06:00
ctx: SymbolDiffContext<'_>,
other_ctx: Option<SymbolDiffContext<'_>>,
2022-09-08 17:19:20 -04:00
symbol: &ObjSymbol,
symbol_diff: &ObjSymbolDiff,
2022-12-10 20:28:01 -05:00
section: Option<&ObjSection>,
2024-10-09 21:44:18 -06:00
state: &SymbolViewState,
appearance: &Appearance,
2024-10-09 21:44:18 -06:00
column: usize,
) -> Option<DiffViewAction> {
2023-08-12 14:18:09 -04:00
let mut ret = None;
2024-10-09 21:44:18 -06:00
if symbol.flags.0.contains(ObjSymbolFlags::Hidden) && !state.show_hidden_symbols {
return ret;
}
2022-09-08 17:19:20 -04:00
let mut job = LayoutJob::default();
let name: &str =
if let Some(demangled) = &symbol.demangled_name { demangled } else { &symbol.name };
let mut selected = false;
if let Some(sym_ref) =
2024-10-09 21:44:18 -06:00
if column == 0 { state.highlighted_symbol.0 } else { state.highlighted_symbol.1 }
{
selected = symbol_diff.symbol_ref == sym_ref;
2022-09-08 17:19:20 -04:00
}
2024-08-11 14:27:27 -06:00
if !symbol.flags.0.is_empty() {
write_text("[", appearance.text_color, &mut job, appearance.code_font.clone());
if symbol.flags.0.contains(ObjSymbolFlags::Common) {
write_text("c", appearance.replace_color, &mut job, appearance.code_font.clone());
} else if symbol.flags.0.contains(ObjSymbolFlags::Global) {
write_text("g", appearance.insert_color, &mut job, appearance.code_font.clone());
} else if symbol.flags.0.contains(ObjSymbolFlags::Local) {
write_text("l", appearance.text_color, &mut job, appearance.code_font.clone());
}
if symbol.flags.0.contains(ObjSymbolFlags::Weak) {
write_text("w", appearance.text_color, &mut job, appearance.code_font.clone());
}
2024-09-09 19:43:10 -06:00
if symbol.flags.0.contains(ObjSymbolFlags::HasExtra) {
write_text("e", appearance.text_color, &mut job, appearance.code_font.clone());
}
2024-08-11 14:27:27 -06:00
if symbol.flags.0.contains(ObjSymbolFlags::Hidden) {
2024-08-11 14:29:58 -06:00
write_text(
"h",
appearance.deemphasized_text_color,
&mut job,
appearance.code_font.clone(),
);
2024-08-11 14:27:27 -06:00
}
write_text("] ", appearance.text_color, &mut job, appearance.code_font.clone());
2022-09-11 13:52:55 -04:00
}
if let Some(match_percent) = symbol_diff.match_percent {
write_text("(", appearance.text_color, &mut job, appearance.code_font.clone());
write_text(
2024-10-07 20:17:56 -06:00
&format!("{:.0}%", match_percent.floor()),
match_color_for_symbol(match_percent, appearance),
&mut job,
appearance.code_font.clone(),
);
write_text(") ", appearance.text_color, &mut job, appearance.code_font.clone());
}
write_text(name, appearance.highlight_color, &mut job, appearance.code_font.clone());
2024-10-09 21:44:18 -06:00
let response = SelectableLabel::new(selected, job).ui(ui).on_hover_ui_at_pointer(|ui| {
symbol_hover_ui(ui, ctx.obj.arch.as_ref(), symbol, appearance)
});
2024-09-09 19:43:10 -06:00
response.context_menu(|ui| {
2024-10-09 21:44:18 -06:00
if let Some(result) =
symbol_context_menu_ui(ui, ctx, other_ctx, symbol, symbol_diff, section, column)
{
ret = Some(DiffViewAction::Navigate(result));
}
2024-09-09 19:43:10 -06:00
});
if selected && state.autoscroll_to_highlighted_symbols {
// Automatically scroll the view to encompass the selected symbol in case the user selected
// an offscreen symbol by using a keyboard shortcut.
ui.scroll_to_rect_animation(response.rect, None, ScrollAnimation::none());
// This autoscroll state flag will be reset in DiffViewState::post_update at the end of
// every frame so that we don't continuously scroll the view back when the user is trying to
// manually scroll away.
}
if response.clicked() || (selected && hotkeys::enter_pressed(ui.ctx())) {
2022-12-10 20:28:01 -05:00
if let Some(section) = section {
2024-10-09 21:44:18 -06:00
match section.kind {
ObjSectionKind::Code => {
ret = Some(DiffViewAction::Navigate(DiffViewNavigation::with_symbols(
View::FunctionDiff,
other_ctx,
symbol,
section,
symbol_diff,
column,
)));
}
ObjSectionKind::Data => {
2025-02-02 17:32:08 -07:00
ret = Some(DiffViewAction::Navigate(DiffViewNavigation::data_diff(
section, column,
2024-10-09 21:44:18 -06:00
)));
}
ObjSectionKind::Bss => {}
2022-11-06 00:49:46 -04:00
}
}
2022-09-08 17:19:20 -04:00
} else if response.hovered() {
ret = Some(if column == 0 {
DiffViewAction::SetSymbolHighlight(
Some(symbol_diff.symbol_ref),
symbol_diff.target_symbol,
false,
)
} else {
DiffViewAction::SetSymbolHighlight(
symbol_diff.target_symbol,
Some(symbol_diff.symbol_ref),
false,
)
2024-10-09 21:44:18 -06:00
});
2022-09-08 17:19:20 -04:00
}
2023-08-12 14:18:09 -04:00
ret
2022-09-08 17:19:20 -04:00
}
2024-10-09 21:44:18 -06:00
fn symbol_matches_filter(
symbol: &ObjSymbol,
diff: &ObjSymbolDiff,
filter: SymbolFilter<'_>,
) -> bool {
match filter {
SymbolFilter::None => true,
SymbolFilter::Search(regex) => {
regex.is_match(&symbol.name)
2025-02-02 17:32:08 -07:00
|| symbol.demangled_name.as_deref().is_some_and(|s| regex.is_match(s))
}
SymbolFilter::Mapping(symbol_ref, regex) => {
diff.target_symbol == Some(symbol_ref)
&& regex.is_none_or(|r| {
r.is_match(&symbol.name)
|| symbol.demangled_name.as_deref().is_some_and(|s| r.is_match(s))
})
2024-10-09 21:44:18 -06:00
}
2024-07-21 23:01:58 -06:00
}
}
2024-10-09 21:44:18 -06:00
#[derive(Copy, Clone)]
pub enum SymbolFilter<'a> {
None,
Search(&'a Regex),
2025-02-02 17:32:08 -07:00
Mapping(SymbolRef, Option<&'a Regex>),
2024-10-09 21:44:18 -06:00
}
2023-08-12 14:18:09 -04:00
#[must_use]
#[expect(clippy::too_many_arguments)]
2024-10-09 21:44:18 -06:00
pub fn symbol_list_ui(
2022-09-08 17:19:20 -04:00
ui: &mut Ui,
2024-10-09 21:44:18 -06:00
ctx: SymbolDiffContext<'_>,
other_ctx: Option<SymbolDiffContext<'_>>,
state: &SymbolViewState,
filter: SymbolFilter<'_>,
appearance: &Appearance,
2024-10-09 21:44:18 -06:00
column: usize,
open_sections: Option<bool>,
2024-10-09 21:44:18 -06:00
) -> Option<DiffViewAction> {
2023-08-12 14:18:09 -04:00
let mut ret = None;
2022-09-08 17:19:20 -04:00
ScrollArea::both().auto_shrink([false, false]).show(ui, |ui| {
2024-10-09 21:44:18 -06:00
let mut mapping = BTreeMap::new();
2025-02-02 17:32:08 -07:00
if let SymbolFilter::Mapping(_, _) = filter {
2024-10-09 21:44:18 -06:00
let mut show_mapped_symbols = state.show_mapped_symbols;
if ui.checkbox(&mut show_mapped_symbols, "Show mapped symbols").changed() {
ret = Some(DiffViewAction::SetShowMappedSymbols(show_mapped_symbols));
}
for mapping_diff in &ctx.diff.mapping_symbols {
2025-02-02 17:32:08 -07:00
let symbol = ctx.obj.section_symbol(mapping_diff.symbol_ref).1;
if !symbol_matches_filter(symbol, mapping_diff, filter) {
continue;
2024-10-09 21:44:18 -06:00
}
2025-02-02 17:32:08 -07:00
if !show_mapped_symbols {
let symbol_diff = ctx.diff.symbol_diff(mapping_diff.symbol_ref);
if symbol_diff.target_symbol.is_some() {
continue;
}
}
mapping.insert(mapping_diff.symbol_ref, mapping_diff);
2024-10-09 21:44:18 -06:00
}
} else {
for (symbol, diff) in ctx.obj.common.iter().zip(&ctx.diff.common) {
if !symbol_matches_filter(symbol, diff, filter) {
continue;
}
mapping.insert(diff.symbol_ref, diff);
}
for (section, section_diff) in ctx.obj.sections.iter().zip(&ctx.diff.sections) {
for (symbol, symbol_diff) in section.symbols.iter().zip(&section_diff.symbols) {
if !symbol_matches_filter(symbol, symbol_diff, filter) {
continue;
}
mapping.insert(symbol_diff.symbol_ref, symbol_diff);
}
}
}
hotkeys::check_scroll_hotkeys(ui, false);
let mut new_key_value_to_highlight = None;
if let Some(sym_ref) =
if column == 0 { state.highlighted_symbol.0 } else { state.highlighted_symbol.1 }
{
let up = if hotkeys::consume_up_key(ui.ctx()) {
Some(true)
} else if hotkeys::consume_down_key(ui.ctx()) {
Some(false)
} else {
None
};
if let Some(mut up) = up {
if state.reverse_fn_order {
up = !up;
}
new_key_value_to_highlight = if up {
mapping.range(..sym_ref).next_back()
} else {
mapping.range((Bound::Excluded(sym_ref), Bound::Unbounded)).next()
};
};
} else {
// No symbol is highlighted in this column. Select the topmost symbol instead.
// Note that we intentionally do not consume the up/down key presses in this case, but
// we do when a symbol is highlighted. This is so that if only one column has a symbol
// highlighted, that one takes precedence over the one with nothing highlighted.
if hotkeys::up_pressed(ui.ctx()) || hotkeys::down_pressed(ui.ctx()) {
new_key_value_to_highlight = if state.reverse_fn_order {
mapping.last_key_value()
} else {
mapping.first_key_value()
};
}
}
if let Some((new_sym_ref, new_symbol_diff)) = new_key_value_to_highlight {
ret = Some(if column == 0 {
DiffViewAction::SetSymbolHighlight(
Some(*new_sym_ref),
new_symbol_diff.target_symbol,
true,
)
} else {
DiffViewAction::SetSymbolHighlight(
new_symbol_diff.target_symbol,
Some(*new_sym_ref),
true,
)
});
}
2022-09-08 17:19:20 -04:00
ui.scope(|ui| {
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
2024-09-09 19:26:46 -06:00
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
2022-09-08 17:19:20 -04:00
2024-10-09 21:44:18 -06:00
// Skip sections with all symbols filtered out
2024-10-28 17:54:49 -06:00
if mapping.keys().any(|symbol_ref| symbol_ref.section_idx == SECTION_COMMON) {
2022-09-08 17:19:20 -04:00
CollapsingHeader::new(".comm").default_open(true).show(ui, |ui| {
2024-10-09 21:44:18 -06:00
for (symbol_ref, symbol_diff) in mapping
.iter()
2024-10-28 17:54:49 -06:00
.filter(|(symbol_ref, _)| symbol_ref.section_idx == SECTION_COMMON)
2024-10-09 21:44:18 -06:00
{
let symbol = ctx.obj.section_symbol(*symbol_ref).1;
if let Some(result) = symbol_ui(
ui,
2024-10-09 21:44:18 -06:00
ctx,
other_ctx,
symbol,
symbol_diff,
None,
state,
appearance,
2024-10-09 21:44:18 -06:00
column,
) {
ret = Some(result);
}
2022-09-08 17:19:20 -04:00
}
});
}
2024-10-09 21:44:18 -06:00
for ((section_index, section), section_diff) in
ctx.obj.sections.iter().enumerate().zip(&ctx.diff.sections)
{
// Skip sections with all symbols filtered out
if !mapping.keys().any(|symbol_ref| symbol_ref.section_idx == section_index) {
continue;
}
2024-05-21 12:02:00 -06:00
let mut header = LayoutJob::simple_singleline(
format!("{} ({:x})", section.name, section.size),
appearance.code_font.clone(),
Color32::PLACEHOLDER,
);
if let Some(match_percent) = section_diff.match_percent {
write_text(
" (",
Color32::PLACEHOLDER,
&mut header,
appearance.code_font.clone(),
);
write_text(
2024-10-07 20:17:56 -06:00
&format!("{:.0}%", match_percent.floor()),
2024-05-21 12:02:00 -06:00
match_color_for_symbol(match_percent, appearance),
&mut header,
appearance.code_font.clone(),
);
write_text(
")",
Color32::PLACEHOLDER,
&mut header,
appearance.code_font.clone(),
);
}
CollapsingHeader::new(header)
2024-09-26 23:51:49 -06:00
.id_salt(Id::new(section.name.clone()).with(section.orig_index))
2022-09-08 17:19:20 -04:00
.default_open(true)
.open(open_sections)
2022-09-08 17:19:20 -04:00
.show(ui, |ui| {
2023-08-12 14:18:09 -04:00
if section.kind == ObjSectionKind::Code && state.reverse_fn_order {
2024-10-09 21:44:18 -06:00
for (symbol, symbol_diff) in mapping
.iter()
.filter(|(symbol_ref, _)| symbol_ref.section_idx == section_index)
.rev()
{
2024-10-09 21:44:18 -06:00
let symbol = ctx.obj.section_symbol(*symbol).1;
if let Some(result) = symbol_ui(
ui,
2024-10-09 21:44:18 -06:00
ctx,
other_ctx,
symbol,
symbol_diff,
Some(section),
state,
appearance,
2024-10-09 21:44:18 -06:00
column,
) {
ret = Some(result);
}
2022-09-08 17:19:20 -04:00
}
} else {
2024-10-09 21:44:18 -06:00
for (symbol, symbol_diff) in mapping
.iter()
.filter(|(symbol_ref, _)| symbol_ref.section_idx == section_index)
{
2024-10-09 21:44:18 -06:00
let symbol = ctx.obj.section_symbol(*symbol).1;
if let Some(result) = symbol_ui(
ui,
2024-10-09 21:44:18 -06:00
ctx,
other_ctx,
symbol,
symbol_diff,
Some(section),
state,
appearance,
2024-10-09 21:44:18 -06:00
column,
) {
ret = Some(result);
}
2022-09-08 17:19:20 -04:00
}
}
});
}
});
});
2023-08-12 14:18:09 -04:00
ret
2022-09-08 17:19:20 -04:00
}
2024-10-09 21:44:18 -06:00
#[derive(Copy, Clone)]
pub struct SymbolDiffContext<'a> {
pub obj: &'a ObjInfo,
pub diff: &'a ObjDiff,
}