ghostscope_platform/
register_mapping.rs1use tracing::warn;
6
7pub mod pt_regs_indices {
13 use aya_ebpf_bindings::bindings::pt_regs;
14
15 const U64_SIZE: usize = core::mem::size_of::<u64>();
17
18 pub const R15: usize = core::mem::offset_of!(pt_regs, r15) / U64_SIZE;
20 pub const R14: usize = core::mem::offset_of!(pt_regs, r14) / U64_SIZE;
21 pub const R13: usize = core::mem::offset_of!(pt_regs, r13) / U64_SIZE;
22 pub const R12: usize = core::mem::offset_of!(pt_regs, r12) / U64_SIZE;
23 pub const RBP: usize = core::mem::offset_of!(pt_regs, rbp) / U64_SIZE; pub const RBX: usize = core::mem::offset_of!(pt_regs, rbx) / U64_SIZE;
25 pub const R11: usize = core::mem::offset_of!(pt_regs, r11) / U64_SIZE;
26 pub const R10: usize = core::mem::offset_of!(pt_regs, r10) / U64_SIZE;
27 pub const R9: usize = core::mem::offset_of!(pt_regs, r9) / U64_SIZE;
28 pub const R8: usize = core::mem::offset_of!(pt_regs, r8) / U64_SIZE;
29 pub const RAX: usize = core::mem::offset_of!(pt_regs, rax) / U64_SIZE; pub const RCX: usize = core::mem::offset_of!(pt_regs, rcx) / U64_SIZE; pub const RDX: usize = core::mem::offset_of!(pt_regs, rdx) / U64_SIZE; pub const RSI: usize = core::mem::offset_of!(pt_regs, rsi) / U64_SIZE; pub const RDI: usize = core::mem::offset_of!(pt_regs, rdi) / U64_SIZE; pub const ORIG_RAX: usize = core::mem::offset_of!(pt_regs, orig_rax) / U64_SIZE; pub const RIP: usize = core::mem::offset_of!(pt_regs, rip) / U64_SIZE; pub const CS: usize = core::mem::offset_of!(pt_regs, cs) / U64_SIZE; pub const EFLAGS: usize = core::mem::offset_of!(pt_regs, eflags) / U64_SIZE; pub const RSP: usize = core::mem::offset_of!(pt_regs, rsp) / U64_SIZE; pub const SS: usize = core::mem::offset_of!(pt_regs, ss) / U64_SIZE; }
43
44pub fn dwarf_reg_to_pt_regs_byte_offset_x86_64(dwarf_reg: u16) -> Option<usize> {
77 const U64_SIZE: usize = core::mem::size_of::<u64>();
78 match dwarf_reg {
79 0 => Some(pt_regs_indices::RAX * U64_SIZE), 1 => Some(pt_regs_indices::RDX * U64_SIZE), 2 => Some(pt_regs_indices::RCX * U64_SIZE), 3 => Some(pt_regs_indices::RBX * U64_SIZE), 4 => Some(pt_regs_indices::RSI * U64_SIZE), 5 => Some(pt_regs_indices::RDI * U64_SIZE), 6 => Some(pt_regs_indices::RBP * U64_SIZE), 7 => Some(pt_regs_indices::RSP * U64_SIZE), 8 => Some(pt_regs_indices::R8 * U64_SIZE), 9 => Some(pt_regs_indices::R9 * U64_SIZE), 10 => Some(pt_regs_indices::R10 * U64_SIZE), 11 => Some(pt_regs_indices::R11 * U64_SIZE), 12 => Some(pt_regs_indices::R12 * U64_SIZE), 13 => Some(pt_regs_indices::R13 * U64_SIZE), 14 => Some(pt_regs_indices::R14 * U64_SIZE), 15 => Some(pt_regs_indices::R15 * U64_SIZE), 16 => Some(pt_regs_indices::RIP * U64_SIZE), _ => {
98 warn!("Unknown DWARF register {} for x86_64", dwarf_reg);
99 None
100 }
101 }
102}
103
104pub fn dwarf_reg_to_name_x86_64(dwarf_reg: u16) -> Option<&'static str> {
109 match dwarf_reg {
110 0 => Some("RAX"), 1 => Some("RDX"), 2 => Some("RCX"), 3 => Some("RBX"), 4 => Some("RSI"), 5 => Some("RDI"), 6 => Some("RBP"), 7 => Some("RSP"), 8 => Some("R8"), 9 => Some("R9"), 10 => Some("R10"), 11 => Some("R11"), 12 => Some("R12"), 13 => Some("R13"), 14 => Some("R14"), 15 => Some("R15"), 16 => Some("RIP"), _ => None,
128 }
129}
130
131pub fn dwarf_reg_to_name(dwarf_reg: u16) -> Option<&'static str> {
136 dwarf_reg_to_name_x86_64(dwarf_reg)
139}
140
141pub fn dwarf_reg_to_pt_regs_byte_offset(dwarf_reg: u16) -> Option<usize> {
146 dwarf_reg_to_pt_regs_byte_offset_x86_64(dwarf_reg)
149}
150
151#[cfg(test)]
152mod tests {
153 use super::*;
154
155 #[test]
156 fn test_x86_64_dwarf_to_pt_regs_mapping() {
157 assert_eq!(dwarf_reg_to_pt_regs_byte_offset_x86_64(0), Some(80)); assert_eq!(dwarf_reg_to_pt_regs_byte_offset_x86_64(6), Some(32)); assert_eq!(dwarf_reg_to_pt_regs_byte_offset_x86_64(7), Some(152)); assert_eq!(dwarf_reg_to_pt_regs_byte_offset_x86_64(16), Some(128)); assert_eq!(dwarf_reg_to_pt_regs_byte_offset_x86_64(99), None);
165 }
166
167 #[test]
168 fn test_x86_64_dwarf_to_name_mapping() {
169 assert_eq!(dwarf_reg_to_name_x86_64(0), Some("RAX"));
171 assert_eq!(dwarf_reg_to_name_x86_64(1), Some("RDX"));
172 assert_eq!(dwarf_reg_to_name_x86_64(4), Some("RSI"));
173 assert_eq!(dwarf_reg_to_name_x86_64(5), Some("RDI"));
174 assert_eq!(dwarf_reg_to_name_x86_64(6), Some("RBP"));
175 assert_eq!(dwarf_reg_to_name_x86_64(7), Some("RSP"));
176
177 assert_eq!(dwarf_reg_to_name_x86_64(8), Some("R8"));
179 assert_eq!(dwarf_reg_to_name_x86_64(13), Some("R13"));
180 assert_eq!(dwarf_reg_to_name_x86_64(15), Some("R15"));
181
182 assert_eq!(dwarf_reg_to_name_x86_64(16), Some("RIP"));
184
185 assert_eq!(dwarf_reg_to_name_x86_64(99), None);
187 }
188
189 #[test]
190 fn test_dwarf_reg_to_name_generic() {
191 assert_eq!(dwarf_reg_to_name(0), Some("RAX"));
193 assert_eq!(dwarf_reg_to_name(5), Some("RDI"));
194 assert_eq!(dwarf_reg_to_name(13), Some("R13"));
195 assert_eq!(dwarf_reg_to_name(99), None);
196 }
197
198 #[test]
199 fn test_dwarf_reg_to_pt_regs_byte_offset_generic() {
200 assert_eq!(dwarf_reg_to_pt_regs_byte_offset(0), Some(80)); assert_eq!(dwarf_reg_to_pt_regs_byte_offset(5), Some(112)); assert_eq!(dwarf_reg_to_pt_regs_byte_offset(99), None);
204 }
205}