Skip to content

Commit 5c8e4df

Browse files
committed
Show registers addr in hexdump
* If register points to addr, show in right column
1 parent 993bf8e commit 5c8e4df

File tree

1 file changed

+102
-5
lines changed

1 file changed

+102
-5
lines changed

src/ui/hexdump.rs

+102-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std::sync::atomic::Ordering;
2+
3+
use deku::ctx::Endian;
4+
use log::{debug, trace};
15
use ratatui::{
26
layout::{Constraint, Flex, Layout, Rect},
37
style::{Color, Style, Stylize},
@@ -13,7 +17,13 @@ use super::{BLUE, DARK_GRAY, GREEN, ORANGE, SCROLL_CONTROL_TEXT, YELLOW};
1317
pub const HEXDUMP_WIDTH: usize = 16;
1418

1519
/// Convert bytes in hexdump, `skip` that many lines, `take` that many lines
16-
fn to_hexdump_str(buffer: &[u8], skip: usize, take: usize) -> Vec<Line> {
20+
fn to_hexdump_str<'a>(
21+
app: &mut App,
22+
pos: u64,
23+
buffer: &[u8],
24+
skip: usize,
25+
take: usize,
26+
) -> Vec<Line<'a>> {
1727
let mut lines = Vec::new();
1828
for (offset, chunk) in buffer.chunks(16).skip(skip).take(take).enumerate() {
1929
let mut hex_spans = Vec::new();
@@ -31,10 +41,46 @@ fn to_hexdump_str(buffer: &[u8], skip: usize, take: usize) -> Vec<Line> {
3141
hex_spans.push(Span::styled(ascii_char.to_string(), Style::default().fg(color)));
3242
}
3343

44+
// check if value has a register reference
45+
let thirty = app.thirty_two_bit.load(Ordering::Relaxed);
46+
47+
let mut ref_spans = Vec::new();
48+
let endian = app.endian.lock().unwrap();
49+
let registers = app.registers.lock().unwrap();
50+
51+
ref_spans.push(Span::raw("| "));
52+
53+
// NOTE: This is disabled, since it's mostly useless?
54+
//deref_bytes_to_registers(&endian, chunk, thirty, &mut ref_spans, &registers);
55+
56+
let windows = if thirty { 4 } else { 8 };
57+
for r in registers.iter() {
58+
if let Some(reg) = &r.register {
59+
if !reg.is_set() {
60+
continue;
61+
}
62+
if let Some(reg_value) = &reg.value {
63+
if let Ok(val) = u64::from_str_radix(&reg_value[2..], 16) {
64+
for n in 0..=windows {
65+
if val as usize == pos as usize + ((offset + skip) * HEXDUMP_WIDTH + n)
66+
{
67+
ref_spans.push(Span::raw(format!(
68+
"← ${}(0x{:02x}) ",
69+
r.name.clone(),
70+
val
71+
)));
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
3479
let line = Line::from_iter(
3580
vec![Span::raw(format!("{:08x}: ", (skip + offset) * HEXDUMP_WIDTH)), Span::raw("")]
3681
.into_iter()
37-
.chain(hex_spans),
82+
.chain(hex_spans)
83+
.chain(ref_spans),
3884
);
3985

4086
lines.push(line);
@@ -43,6 +89,56 @@ fn to_hexdump_str(buffer: &[u8], skip: usize, take: usize) -> Vec<Line> {
4389
lines
4490
}
4591

92+
fn deref_bytes_to_registers(
93+
endian: &Option<Endian>,
94+
chunk: &[u8],
95+
thirty: bool,
96+
ref_spans: &mut Vec<Span<'_>>,
97+
registers: &Vec<crate::register::RegisterStorage>,
98+
) {
99+
let windows = if thirty { 4 } else { 8 };
100+
for w in chunk.windows(windows) {
101+
let bytes_val = if thirty {
102+
let val = if endian.unwrap() == Endian::Big {
103+
// TODO: try_into()
104+
u32::from_be_bytes([w[0], w[1], w[2], w[3]])
105+
} else {
106+
u32::from_le_bytes([w[0], w[1], w[2], w[3]])
107+
};
108+
109+
val as u64
110+
} else {
111+
if endian.unwrap() == Endian::Big {
112+
u64::from_be_bytes([w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7]])
113+
} else {
114+
u64::from_le_bytes([w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7]])
115+
}
116+
};
117+
118+
for r in registers.iter() {
119+
if let Some(reg) = &r.register {
120+
if !reg.is_set() {
121+
continue;
122+
}
123+
if let Some(reg_value) = &reg.value {
124+
if let Ok(val) = u64::from_str_radix(&reg_value[2..], 16) {
125+
if val != 0 {
126+
// Find registers that are pointing to the value at a byte offset
127+
if bytes_val == val {
128+
ref_spans.push(Span::raw(format!(
129+
"${}(0x{:02x?}) ",
130+
r.name.clone(),
131+
val
132+
)));
133+
}
134+
}
135+
}
136+
}
137+
}
138+
}
139+
}
140+
}
141+
46142
fn color(byte: u8) -> Color {
47143
if byte == 0x00 {
48144
DARK_GRAY
@@ -73,16 +169,17 @@ fn block(pos: &str) -> Block {
73169
}
74170

75171
pub fn draw_hexdump(app: &mut App, f: &mut Frame, hexdump: Rect, show_popup: bool) {
76-
let hexdump_lock = app.hexdump.lock().unwrap();
172+
let hexdump_active = app.hexdump.lock().unwrap().is_some();
77173
let mut pos = "".to_string();
78174

79-
if let Some(r) = hexdump_lock.as_ref() {
175+
if hexdump_active {
176+
let r = app.hexdump.lock().unwrap().clone().unwrap();
80177
pos = format!("(0x{:02x?})", r.0);
81178
let data = &r.1;
82179

83180
let skip = app.hexdump_scroll;
84181
let take = hexdump.height;
85-
let lines = to_hexdump_str(data, skip as usize, take as usize);
182+
let lines = to_hexdump_str(app, r.0, data, skip as usize, take as usize);
86183
let content_len = data.len() / HEXDUMP_WIDTH;
87184

88185
let lines: Vec<Line> = lines.into_iter().collect();

0 commit comments

Comments
 (0)