Debian Codegraph @

third_party/rust/minidump-writer/src/linux/thread_info/aarch64.rs

raw
1 use super::{CommonThreadInfo, NT_Elf};
2 use crate::{
3 errors::ThreadInfoError,
4 minidump_cpu::{RawContextCPU, FP_REG_COUNT, GP_REG_COUNT},
5 Pid,
6 };
7 use nix::sys::ptrace;
8
9 /// https://github.com/rust-lang/libc/pull/2719
10 #[derive(Debug)]
11 #[allow(non_camel_case_types)]
12 pub struct user_fpsimd_struct {
13 pub vregs: [u128; 32],
14 pub fpsr: u32,
15 pub fpcr: u32,
16 }
17
18 type Result<T> = std::result::Result<T, ThreadInfoError>;
19
20 #[cfg(target_arch = "aarch64")]
21 #[derive(Debug)]
22 pub struct ThreadInfoAarch64 {
23 pub stack_pointer: usize,
24 pub tgid: Pid, // thread group id
25 pub ppid: Pid, // parent process
26 pub regs: libc::user_regs_struct,
27 pub fpregs: user_fpsimd_struct,
28 }
29
30 impl CommonThreadInfo for ThreadInfoAarch64 {}
31
32 impl ThreadInfoAarch64 {
33 pub fn get_instruction_pointer(&self) -> usize {
34 self.regs.pc as usize
35 }
36
37 // nix currently doesn't support PTRACE_GETREGSET, so we have to do it ourselves
38 fn getregset(pid: Pid) -> Result<libc::user_regs_struct> {
39 Self::ptrace_get_data_via_io(
40 0x4204 as ptrace::RequestType, // PTRACE_GETREGSET
41 Some(NT_Elf::NT_PRSTATUS),
42 nix::unistd::Pid::from_raw(pid),
43 )
44 }
45
46 fn getregs(pid: Pid) -> Result<libc::user_regs_struct> {
47 // TODO: nix restricts PTRACE_GETREGS to arm android for some reason
48 Self::ptrace_get_data(
49 12 as ptrace::RequestType, // PTRACE_GETREGS
50 None,
51 nix::unistd::Pid::from_raw(pid),
52 )
53 }
54
55 // nix currently doesn't support PTRACE_GETREGSET, so we have to do it ourselves
56 fn getfpregset(pid: Pid) -> Result<user_fpsimd_struct> {
57 Self::ptrace_get_data_via_io(
58 0x4204 as ptrace::RequestType, // PTRACE_GETREGSET
59 Some(NT_Elf::NT_PRFPREGSET),
60 nix::unistd::Pid::from_raw(pid),
61 )
62 }
63
64 // nix currently doesn't support PTRACE_GETFPREGS, so we have to do it ourselves
65 fn getfpregs(pid: Pid) -> Result<user_fpsimd_struct> {
66 Self::ptrace_get_data(
67 14 as ptrace::RequestType, // PTRACE_GETFPREGS
68 None,
69 nix::unistd::Pid::from_raw(pid),
70 )
71 }
72
73 pub fn fill_cpu_context(&self, out: &mut RawContextCPU) {
74 out.context_flags =
75 minidump_common::format::ContextFlagsArm64Old::CONTEXT_ARM64_OLD_FULL.bits() as u64;
76
77 out.cpsr = self.regs.pstate as u32;
78 out.iregs[..GP_REG_COUNT].copy_from_slice(&self.regs.regs[..GP_REG_COUNT]);
79 out.sp = self.regs.sp;
80 // Note that in breakpad this was the last member of the iregs field
81 // which was 33 in length, but in rust-minidump it is its own separate
82 // field instead
83 out.pc = self.regs.pc;
84
85 out.fpsr = self.fpregs.fpsr;
86 out.fpcr = self.fpregs.fpcr;
87 out.float_regs[..FP_REG_COUNT].copy_from_slice(&self.fpregs.vregs[..FP_REG_COUNT]);
88 }
89
90 pub fn create_impl(_pid: Pid, tid: Pid) -> Result<Self> {
91 let (ppid, tgid) = Self::get_ppid_and_tgid(tid)?;
92 let regs = Self::getregset(tid).or_else(|_| Self::getregs(tid))?;
93 let fpregs = Self::getfpregset(tid).or_else(|_| Self::getfpregs(tid))?;
94
95 let stack_pointer = regs.sp as usize;
96
97 Ok(Self {
98 stack_pointer,
99 tgid,
100 ppid,
101 regs,
102 fpregs,
103 })
104 }
105 }