Debian Codegraph @

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

raw
1 use crate::{errors::ThreadInfoError, Pid};
2 use nix::{errno::Errno, sys::ptrace, unistd};
3 use std::{
4 io::{self, BufRead},
5 path,
6 };
7
8 type Result<T> = std::result::Result<T, ThreadInfoError>;
9
10 cfg_if::cfg_if! {
11 if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
12 mod x86;
13 pub type ThreadInfo = x86::ThreadInfoX86;
14 } else if #[cfg(target_arch = "arm")] {
15 mod arm;
16 pub type ThreadInfo = arm::ThreadInfoArm;
17 } else if #[cfg(target_arch = "aarch64")] {
18 mod aarch64;
19 pub type ThreadInfo = aarch64::ThreadInfoAarch64;
20 } else if #[cfg(target_arch = "mips")] {
21 mod mips;
22 pub type ThreadInfo = mips::ThreadInfoMips;
23 }
24 }
25
26 #[derive(Debug)]
27 #[allow(non_camel_case_types, dead_code)]
28 enum NT_Elf {
29 NT_NONE = 0,
30 NT_PRSTATUS = 1,
31 NT_PRFPREGSET = 2,
32 //NT_PRPSINFO = 3,
33 //NT_TASKSTRUCT = 4,
34 //NT_AUXV = 6,
35 NT_ARM_VFP = 0x400, // ARM VFP/NEON registers
36 }
37
38 #[inline]
39 pub fn copy_u32_registers(dst: &mut [u128], src: &[u32]) {
40 // SAFETY: We are copying a block of memory from ptrace as u32s to the u128
41 // format of minidump-common
42 unsafe {
43 let dst: &mut [u8] =
44 std::slice::from_raw_parts_mut(dst.as_mut_ptr().cast(), dst.len() * 16);
45 let src: &[u8] = std::slice::from_raw_parts(src.as_ptr().cast(), src.len() * 4);
46
47 let to_copy = std::cmp::min(dst.len(), src.len());
48 dst[..to_copy].copy_from_slice(&src[..to_copy]);
49 }
50 }
51
52 trait CommonThreadInfo {
53 fn get_ppid_and_tgid(tid: Pid) -> Result<(Pid, Pid)> {
54 let mut ppid = -1;
55 let mut tgid = -1;
56
57 let status_path = path::PathBuf::from(format!("/proc/{}/status", tid));
58 let status_file = std::fs::File::open(status_path)?;
59 for line in io::BufReader::new(status_file).lines() {
60 let l = line?;
61 let start = l
62 .get(0..6)
63 .ok_or_else(|| ThreadInfoError::InvalidProcStatusFile(tid, l.clone()))?;
64 match start {
65 "Tgid:\t" => {
66 tgid = l
67 .get(6..)
68 .ok_or_else(|| ThreadInfoError::InvalidProcStatusFile(tid, l.clone()))?
69 .parse::<Pid>()?;
70 }
71 "PPid:\t" => {
72 ppid = l
73 .get(6..)
74 .ok_or_else(|| ThreadInfoError::InvalidProcStatusFile(tid, l.clone()))?
75 .parse::<Pid>()?;
76 }
77 _ => continue,
78 }
79 }
80 if ppid == -1 || tgid == -1 {
81 return Err(ThreadInfoError::InvalidPid(
82 format!("/proc/{}/status", tid),
83 ppid,
84 tgid,
85 ));
86 }
87 Ok((ppid, tgid))
88 }
89
90 /// SLIGHTLY MODIFIED COPY FROM CRATE nix
91 /// Function for ptrace requests that return values from the data field.
92 /// Some ptrace get requests populate structs or larger elements than `c_long`
93 /// and therefore use the data field to return values. This function handles these
94 /// requests.
95 fn ptrace_get_data<T>(
96 request: ptrace::RequestType,
97 flag: Option<NT_Elf>,
98 pid: nix::unistd::Pid,
99 ) -> Result<T> {
100 let mut data = std::mem::MaybeUninit::uninit();
101 let res = unsafe {
102 libc::ptrace(
103 request,
104 libc::pid_t::from(pid),
105 flag.unwrap_or(NT_Elf::NT_NONE),
106 data.as_mut_ptr(),
107 )
108 };
109 Errno::result(res)?;
110 Ok(unsafe { data.assume_init() })
111 }
112
113 /// SLIGHTLY MODIFIED COPY FROM CRATE nix
114 /// Function for ptrace requests that return values from the data field.
115 /// Some ptrace get requests populate structs or larger elements than `c_long`
116 /// and therefore use the data field to return values. This function handles these
117 /// requests.
118 fn ptrace_get_data_via_io<T>(
119 request: ptrace::RequestType,
120 flag: Option<NT_Elf>,
121 pid: nix::unistd::Pid,
122 ) -> Result<T> {
123 let mut data = std::mem::MaybeUninit::<T>::uninit();
124 let io = libc::iovec {
125 iov_base: data.as_mut_ptr().cast(),
126 iov_len: std::mem::size_of::<T>(),
127 };
128 let res = unsafe {
129 libc::ptrace(
130 request,
131 libc::pid_t::from(pid),
132 flag.unwrap_or(NT_Elf::NT_NONE),
133 &io as *const _,
134 )
135 };
136 Errno::result(res)?;
137 Ok(unsafe { data.assume_init() })
138 }
139
140 /// COPY FROM CRATE nix BECAUSE ITS NOT PUBLIC
141 fn ptrace_peek(
142 request: ptrace::RequestType,
143 pid: unistd::Pid,
144 addr: ptrace::AddressType,
145 data: *mut libc::c_void,
146 ) -> nix::Result<libc::c_long> {
147 let ret = unsafe {
148 Errno::clear();
149 libc::ptrace(request, libc::pid_t::from(pid), addr, data)
150 };
151 match Errno::result(ret) {
152 Ok(..) | Err(Errno::UnknownErrno) => Ok(ret),
153 err @ Err(..) => err,
154 }
155 }
156 }
157 impl ThreadInfo {
158 pub fn create(pid: Pid, tid: Pid) -> std::result::Result<Self, ThreadInfoError> {
159 Self::create_impl(pid, tid)
160 }
161 }