| 1 |
use super::{CommonThreadInfo, NT_Elf}; |
| 2 |
use crate::{errors::ThreadInfoError, minidump_cpu::RawContextCPU, minidump_format::format, Pid}; |
| 3 |
use core::mem::size_of_val; |
| 4 |
#[cfg(all(not(target_os = "android"), target_arch = "x86"))] |
| 5 |
use libc::user_fpxregs_struct; |
| 6 |
#[cfg(not(all(target_os = "android", target_arch = "x86")))] |
| 7 |
use libc::{user, user_fpregs_struct, user_regs_struct}; |
| 8 |
use nix::sys::ptrace; |
| 9 |
use scroll::Pwrite; |
| 10 |
|
| 11 |
type Result<T> = std::result::Result<T, ThreadInfoError>; |
| 12 |
|
| 13 |
// Not defined by libc on Android |
| 14 |
#[cfg(all(target_os = "android", target_arch = "x86"))] |
| 15 |
#[allow(non_camel_case_types)] |
| 16 |
#[repr(C)] |
| 17 |
pub struct user_regs_struct { |
| 18 |
pub ebx: libc::c_long, |
| 19 |
pub ecx: libc::c_long, |
| 20 |
pub edx: libc::c_long, |
| 21 |
pub esi: libc::c_long, |
| 22 |
pub edi: libc::c_long, |
| 23 |
pub ebp: libc::c_long, |
| 24 |
pub eax: libc::c_long, |
| 25 |
pub xds: libc::c_long, |
| 26 |
pub xes: libc::c_long, |
| 27 |
pub xfs: libc::c_long, |
| 28 |
pub xgs: libc::c_long, |
| 29 |
pub orig_eax: libc::c_long, |
| 30 |
pub eip: libc::c_long, |
| 31 |
pub xcs: libc::c_long, |
| 32 |
pub eflags: libc::c_long, |
| 33 |
pub esp: libc::c_long, |
| 34 |
pub xss: libc::c_long, |
| 35 |
} |
| 36 |
|
| 37 |
// Not defined by libc on Android |
| 38 |
#[cfg(all(target_os = "android", target_arch = "x86"))] |
| 39 |
#[allow(non_camel_case_types)] |
| 40 |
#[repr(C)] |
| 41 |
pub struct user_fpxregs_struct { |
| 42 |
pub cwd: libc::c_ushort, |
| 43 |
pub swd: libc::c_ushort, |
| 44 |
pub twd: libc::c_ushort, |
| 45 |
pub fop: libc::c_ushort, |
| 46 |
pub fip: libc::c_long, |
| 47 |
pub fcs: libc::c_long, |
| 48 |
pub foo: libc::c_long, |
| 49 |
pub fos: libc::c_long, |
| 50 |
pub mxcsr: libc::c_long, |
| 51 |
__reserved: libc::c_long, |
| 52 |
pub st_space: [libc::c_long; 32], |
| 53 |
pub xmm_space: [libc::c_long; 32], |
| 54 |
padding: [libc::c_long; 56], |
| 55 |
} |
| 56 |
|
| 57 |
// Not defined by libc on Android |
| 58 |
#[cfg(all(target_os = "android", target_arch = "x86"))] |
| 59 |
#[allow(non_camel_case_types)] |
| 60 |
#[repr(C)] |
| 61 |
pub struct user_fpregs_struct { |
| 62 |
pub cwd: libc::c_long, |
| 63 |
pub swd: libc::c_long, |
| 64 |
pub twd: libc::c_long, |
| 65 |
pub fip: libc::c_long, |
| 66 |
pub fcs: libc::c_long, |
| 67 |
pub foo: libc::c_long, |
| 68 |
pub fos: libc::c_long, |
| 69 |
pub st_space: [libc::c_long; 20], |
| 70 |
} |
| 71 |
|
| 72 |
#[cfg(all(target_os = "android", target_arch = "x86"))] |
| 73 |
#[allow(non_camel_case_types)] |
| 74 |
#[repr(C)] |
| 75 |
pub struct user { |
| 76 |
pub regs: user_regs_struct, |
| 77 |
pub u_fpvalid: libc::c_long, |
| 78 |
pub i387: user_fpregs_struct, |
| 79 |
pub u_tsize: libc::c_ulong, |
| 80 |
pub u_dsize: libc::c_ulong, |
| 81 |
pub u_ssize: libc::c_ulong, |
| 82 |
pub start_code: libc::c_ulong, |
| 83 |
pub start_stack: libc::c_ulong, |
| 84 |
pub signal: libc::c_long, |
| 85 |
__reserved: libc::c_int, |
| 86 |
pub u_ar0: *mut user_regs_struct, |
| 87 |
pub u_fpstate: *mut user_fpregs_struct, |
| 88 |
pub magic: libc::c_ulong, |
| 89 |
pub u_comm: [libc::c_char; 32], |
| 90 |
pub u_debugreg: [libc::c_int; 8], |
| 91 |
} |
| 92 |
|
| 93 |
const NUM_DEBUG_REGISTERS: usize = 8; |
| 94 |
|
| 95 |
pub struct ThreadInfoX86 { |
| 96 |
pub stack_pointer: usize, |
| 97 |
pub tgid: Pid, // thread group id |
| 98 |
pub ppid: Pid, // parent process |
| 99 |
pub regs: user_regs_struct, |
| 100 |
pub fpregs: user_fpregs_struct, |
| 101 |
#[cfg(target_arch = "x86_64")] |
| 102 |
pub dregs: [libc::c_ulonglong; NUM_DEBUG_REGISTERS], |
| 103 |
#[cfg(target_arch = "x86")] |
| 104 |
pub dregs: [libc::c_int; NUM_DEBUG_REGISTERS], |
| 105 |
#[cfg(target_arch = "x86")] |
| 106 |
pub fpxregs: user_fpxregs_struct, |
| 107 |
} |
| 108 |
|
| 109 |
impl CommonThreadInfo for ThreadInfoX86 {} |
| 110 |
|
| 111 |
impl ThreadInfoX86 { |
| 112 |
// nix currently doesn't support PTRACE_GETREGSET, so we have to do it ourselves |
| 113 |
fn getregset(pid: Pid) -> Result<user_regs_struct> { |
| 114 |
Self::ptrace_get_data_via_io( |
| 115 |
0x4204 as ptrace::RequestType, // PTRACE_GETREGSET |
| 116 |
Some(NT_Elf::NT_PRSTATUS), |
| 117 |
nix::unistd::Pid::from_raw(pid), |
| 118 |
) |
| 119 |
} |
| 120 |
|
| 121 |
pub fn getregs(pid: Pid) -> Result<user_regs_struct> { |
| 122 |
// TODO: nix restricts PTRACE_GETREGS to arm android for some reason |
| 123 |
Self::ptrace_get_data( |
| 124 |
12 as ptrace::RequestType, // PTRACE_GETREGS |
| 125 |
None, |
| 126 |
nix::unistd::Pid::from_raw(pid), |
| 127 |
) |
| 128 |
} |
| 129 |
|
| 130 |
// nix currently doesn't support PTRACE_GETREGSET, so we have to do it ourselves |
| 131 |
fn getfpregset(pid: Pid) -> Result<user_fpregs_struct> { |
| 132 |
Self::ptrace_get_data_via_io( |
| 133 |
0x4204 as ptrace::RequestType, // PTRACE_GETREGSET |
| 134 |
Some(NT_Elf::NT_PRFPREGSET), |
| 135 |
nix::unistd::Pid::from_raw(pid), |
| 136 |
) |
| 137 |
} |
| 138 |
|
| 139 |
// nix currently doesn't support PTRACE_GETFPREGS, so we have to do it ourselves |
| 140 |
fn getfpregs(pid: Pid) -> Result<user_fpregs_struct> { |
| 141 |
Self::ptrace_get_data( |
| 142 |
14 as ptrace::RequestType, // PTRACE_GETFPREGS |
| 143 |
None, |
| 144 |
nix::unistd::Pid::from_raw(pid), |
| 145 |
) |
| 146 |
} |
| 147 |
|
| 148 |
// nix currently doesn't support PTRACE_GETFPXREGS, so we have to do it ourselves |
| 149 |
#[cfg(target_arch = "x86")] |
| 150 |
fn getfpxregs(pid: Pid) -> Result<user_fpxregs_struct> { |
| 151 |
Self::ptrace_get_data( |
| 152 |
18 as ptrace::RequestType, // PTRACE_GETFPXREGS |
| 153 |
None, |
| 154 |
nix::unistd::Pid::from_raw(pid), |
| 155 |
) |
| 156 |
} |
| 157 |
|
| 158 |
fn peek_user(pid: Pid, addr: ptrace::AddressType) -> nix::Result<libc::c_long> { |
| 159 |
Self::ptrace_peek( |
| 160 |
ptrace::Request::PTRACE_PEEKUSER as ptrace::RequestType, |
| 161 |
nix::unistd::Pid::from_raw(pid), |
| 162 |
addr, |
| 163 |
std::ptr::null_mut(), |
| 164 |
) |
| 165 |
} |
| 166 |
|
| 167 |
pub fn create_impl(_pid: Pid, tid: Pid) -> Result<Self> { |
| 168 |
let (ppid, tgid) = Self::get_ppid_and_tgid(tid)?; |
| 169 |
let regs = Self::getregset(tid).or_else(|_| Self::getregs(tid))?; |
| 170 |
let fpregs = Self::getfpregset(tid).or_else(|_| Self::getfpregs(tid))?; |
| 171 |
#[cfg(target_arch = "x86")] |
| 172 |
let fpxregs: user_fpxregs_struct; |
| 173 |
#[cfg(target_arch = "x86")] |
| 174 |
{ |
| 175 |
if cfg!(target_feature = "fxsr") { |
| 176 |
fpxregs = Self::getfpxregs(tid)?; |
| 177 |
} else { |
| 178 |
fpxregs = unsafe { std::mem::zeroed() }; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
#[cfg(target_arch = "x86_64")] |
| 183 |
let mut dregs: [libc::c_ulonglong; NUM_DEBUG_REGISTERS] = [0; NUM_DEBUG_REGISTERS]; |
| 184 |
#[cfg(target_arch = "x86")] |
| 185 |
let mut dregs: [libc::c_int; NUM_DEBUG_REGISTERS] = [0; NUM_DEBUG_REGISTERS]; |
| 186 |
|
| 187 |
let debug_offset = memoffset::offset_of!(user, u_debugreg); |
| 188 |
let elem_offset = size_of_val(&dregs[0]); |
| 189 |
for (idx, dreg) in dregs.iter_mut().enumerate() { |
| 190 |
let chunk = Self::peek_user( |
| 191 |
tid, |
| 192 |
(debug_offset + idx * elem_offset) as ptrace::AddressType, |
| 193 |
)?; |
| 194 |
#[cfg(target_arch = "x86_64")] |
| 195 |
{ |
| 196 |
*dreg = chunk as u64; // libc / ptrace is very messy wrt int types used... |
| 197 |
} |
| 198 |
#[cfg(target_arch = "x86")] |
| 199 |
{ |
| 200 |
*dreg = chunk as i32; // libc / ptrace is very messy wrt int types used... |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#[cfg(target_arch = "x86_64")] |
| 205 |
let stack_pointer = regs.rsp as usize; |
| 206 |
#[cfg(target_arch = "x86")] |
| 207 |
let stack_pointer = regs.esp as usize; |
| 208 |
|
| 209 |
Ok(Self { |
| 210 |
stack_pointer, |
| 211 |
tgid, |
| 212 |
ppid, |
| 213 |
regs, |
| 214 |
fpregs, |
| 215 |
dregs, |
| 216 |
#[cfg(target_arch = "x86")] |
| 217 |
fpxregs, |
| 218 |
}) |
| 219 |
} |
| 220 |
|
| 221 |
#[cfg(target_arch = "x86_64")] |
| 222 |
pub fn get_instruction_pointer(&self) -> usize { |
| 223 |
self.regs.rip as usize |
| 224 |
} |
| 225 |
|
| 226 |
#[cfg(target_arch = "x86")] |
| 227 |
pub fn get_instruction_pointer(&self) -> usize { |
| 228 |
self.regs.eip as usize |
| 229 |
} |
| 230 |
|
| 231 |
#[cfg(target_arch = "x86_64")] |
| 232 |
pub fn fill_cpu_context(&self, out: &mut RawContextCPU) { |
| 233 |
use format::ContextFlagsAmd64; |
| 234 |
|
| 235 |
out.context_flags = ContextFlagsAmd64::CONTEXT_AMD64_FULL.bits() |
| 236 |
| ContextFlagsAmd64::CONTEXT_AMD64_SEGMENTS.bits(); |
| 237 |
|
| 238 |
out.cs = self.regs.cs as u16; // TODO: This is u64, do we loose information by doing this? |
| 239 |
|
| 240 |
out.ds = self.regs.ds as u16; // TODO: This is u64, do we loose information by doing this? |
| 241 |
out.es = self.regs.es as u16; // TODO: This is u64, do we loose information by doing this? |
| 242 |
out.fs = self.regs.fs as u16; // TODO: This is u64, do we loose information by doing this? |
| 243 |
out.gs = self.regs.gs as u16; // TODO: This is u64, do we loose information by doing this? |
| 244 |
|
| 245 |
out.ss = self.regs.ss as u16; // TODO: This is u64, do we loose information by doing this? |
| 246 |
out.eflags = self.regs.eflags as u32; // TODO: This is u64, do we loose information by doing this? |
| 247 |
|
| 248 |
out.dr0 = self.dregs[0]; |
| 249 |
out.dr1 = self.dregs[1]; |
| 250 |
out.dr2 = self.dregs[2]; |
| 251 |
out.dr3 = self.dregs[3]; |
| 252 |
// 4 and 5 deliberatly omitted because they aren't included in the minidump |
| 253 |
// format. |
| 254 |
out.dr6 = self.dregs[6]; |
| 255 |
out.dr7 = self.dregs[7]; |
| 256 |
|
| 257 |
out.rax = self.regs.rax; |
| 258 |
out.rcx = self.regs.rcx; |
| 259 |
out.rdx = self.regs.rdx; |
| 260 |
out.rbx = self.regs.rbx; |
| 261 |
|
| 262 |
out.rsp = self.regs.rsp; |
| 263 |
|
| 264 |
out.rbp = self.regs.rbp; |
| 265 |
out.rsi = self.regs.rsi; |
| 266 |
out.rdi = self.regs.rdi; |
| 267 |
out.r8 = self.regs.r8; |
| 268 |
out.r9 = self.regs.r9; |
| 269 |
out.r10 = self.regs.r10; |
| 270 |
out.r11 = self.regs.r11; |
| 271 |
out.r12 = self.regs.r12; |
| 272 |
out.r13 = self.regs.r13; |
| 273 |
out.r14 = self.regs.r14; |
| 274 |
out.r15 = self.regs.r15; |
| 275 |
|
| 276 |
out.rip = self.regs.rip; |
| 277 |
|
| 278 |
{ |
| 279 |
let fs = &self.fpregs; |
| 280 |
let mut float_save = crate::minidump_cpu::FloatStateCPU { |
| 281 |
control_word: fs.cwd, |
| 282 |
status_word: fs.swd, |
| 283 |
tag_word: fs.ftw as u8, |
| 284 |
error_opcode: fs.fop, |
| 285 |
error_offset: fs.rip as u32, |
| 286 |
data_offset: fs.rdp as u32, |
| 287 |
error_selector: 0, // We don't have this. |
| 288 |
data_selector: 0, // We don't have this. |
| 289 |
mx_csr: fs.mxcsr, |
| 290 |
mx_csr_mask: fs.mxcr_mask, |
| 291 |
..Default::default() |
| 292 |
}; |
| 293 |
|
| 294 |
super::copy_u32_registers(&mut float_save.float_registers, &fs.st_space); |
| 295 |
super::copy_u32_registers(&mut float_save.xmm_registers, &fs.xmm_space); |
| 296 |
|
| 297 |
out.float_save |
| 298 |
.pwrite_with(float_save, 0, scroll::Endian::Little) |
| 299 |
.expect("this is impossible"); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
#[cfg(target_arch = "x86")] |
| 304 |
pub fn fill_cpu_context(&self, out: &mut RawContextCPU) { |
| 305 |
out.context_flags = format::ContextFlagsX86::CONTEXT_X86_ALL.bits(); |
| 306 |
|
| 307 |
out.dr0 = self.dregs[0] as u32; |
| 308 |
out.dr3 = self.dregs[3] as u32; |
| 309 |
out.dr1 = self.dregs[1] as u32; |
| 310 |
out.dr2 = self.dregs[2] as u32; |
| 311 |
// 4 and 5 deliberatly omitted because they aren't included in the minidump |
| 312 |
// format. |
| 313 |
out.dr6 = self.dregs[6] as u32; |
| 314 |
out.dr7 = self.dregs[7] as u32; |
| 315 |
|
| 316 |
out.gs = self.regs.xgs as u32; |
| 317 |
out.fs = self.regs.xfs as u32; |
| 318 |
out.es = self.regs.xes as u32; |
| 319 |
out.ds = self.regs.xds as u32; |
| 320 |
|
| 321 |
out.edi = self.regs.edi as u32; |
| 322 |
out.esi = self.regs.esi as u32; |
| 323 |
out.ebx = self.regs.ebx as u32; |
| 324 |
out.edx = self.regs.edx as u32; |
| 325 |
out.ecx = self.regs.ecx as u32; |
| 326 |
out.eax = self.regs.eax as u32; |
| 327 |
|
| 328 |
out.ebp = self.regs.ebp as u32; |
| 329 |
out.eip = self.regs.eip as u32; |
| 330 |
out.cs = self.regs.xcs as u32; |
| 331 |
out.eflags = self.regs.eflags as u32; |
| 332 |
out.esp = self.regs.esp as u32; |
| 333 |
out.ss = self.regs.xss as u32; |
| 334 |
|
| 335 |
out.float_save.control_word = self.fpregs.cwd as u32; |
| 336 |
out.float_save.status_word = self.fpregs.swd as u32; |
| 337 |
out.float_save.tag_word = self.fpregs.twd as u32; |
| 338 |
out.float_save.error_offset = self.fpregs.fip as u32; |
| 339 |
out.float_save.error_selector = self.fpregs.fcs as u32; |
| 340 |
out.float_save.data_offset = self.fpregs.foo as u32; |
| 341 |
out.float_save.data_selector = self.fpregs.fos as u32; |
| 342 |
|
| 343 |
{ |
| 344 |
let ra = &mut out.float_save.register_area; |
| 345 |
// 8 registers * 10 bytes per register. |
| 346 |
for (idx, block) in self.fpregs.st_space.iter().enumerate() { |
| 347 |
let offset = idx * std::mem::size_of::<u32>(); |
| 348 |
if offset >= ra.len() { |
| 349 |
break; |
| 350 |
} |
| 351 |
|
| 352 |
ra.pwrite_with(block, offset, scroll::Endian::Little) |
| 353 |
.expect("this is impossible"); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
#[allow(unused_assignments)] |
| 358 |
{ |
| 359 |
let mut offset = 0; |
| 360 |
macro_rules! write_er { |
| 361 |
($reg:expr) => { |
| 362 |
offset += out |
| 363 |
.extended_registers |
| 364 |
.pwrite_with($reg, offset, scroll::Endian::Little) |
| 365 |
.unwrap() |
| 366 |
}; |
| 367 |
} |
| 368 |
|
| 369 |
// This matches the Intel fpsave format. |
| 370 |
write_er!(self.fpregs.cwd as u16); |
| 371 |
write_er!(self.fpregs.swd as u16); |
| 372 |
write_er!(self.fpregs.twd as u16); |
| 373 |
write_er!(self.fpxregs.fop); |
| 374 |
write_er!(self.fpxregs.fip); |
| 375 |
write_er!(self.fpxregs.fcs); |
| 376 |
write_er!(self.fpregs.foo); |
| 377 |
write_er!(self.fpregs.fos); |
| 378 |
write_er!(self.fpxregs.mxcsr); |
| 379 |
|
| 380 |
offset = 32; |
| 381 |
|
| 382 |
for val in &self.fpxregs.st_space { |
| 383 |
write_er!(val); |
| 384 |
} |
| 385 |
|
| 386 |
debug_assert_eq!(offset, 160); |
| 387 |
|
| 388 |
for val in &self.fpxregs.xmm_space { |
| 389 |
write_er!(val); |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} |