21 * questions.
22 *
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <elf.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/ptrace.h>
34 #include <sys/uio.h>
35 #include "libproc_impl.h"
36
37 #if defined(x86_64) && !defined(amd64)
38 #define amd64 1
39 #endif
40
41 #ifndef __WALL
42 #define __WALL 0x40000000 // Copied from /usr/include/linux/wait.h
43 #endif
44
45 // This file has the libproc implementation specific to live process
46 // For core files, refer to ps_core.c
47
48 static inline uintptr_t align(uintptr_t ptr, size_t size) {
49 return (ptr & ~(size - 1));
50 }
51
52 // ---------------------------------------------
53 // ptrace functions
54 // ---------------------------------------------
55
56 // read "size" bytes of data from "addr" within the target process.
57 // unlike the standard ptrace() function, process_read_data() can handle
58 // unaligned address - alignment check, if required, should be done
59 // before calling process_read_data.
60
132 #define PTRACE_GETREGS_REQ PTRACE_GETREGS
133 #elif defined(PT_GETREGS)
134 #define PTRACE_GETREGS_REQ PT_GETREGS
135 #endif
136
137 #ifdef PTRACE_GETREGS_REQ
138 if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) {
139 print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
140 return false;
141 }
142 return true;
143 #elif defined(PTRACE_GETREGSET)
144 struct iovec iov;
145 iov.iov_base = user;
146 iov.iov_len = sizeof(*user);
147 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void*) &iov) < 0) {
148 print_debug("ptrace(PTRACE_GETREGSET, ...) failed for lwp %d\n", pid);
149 return false;
150 }
151 return true;
152 #else
153 print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n");
154 return false;
155 #endif
156
157 }
158
159 static bool ptrace_continue(pid_t pid, int signal) {
160 // pass the signal to the process so we don't swallow it
161 if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) {
162 print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid);
163 return false;
164 }
165 return true;
166 }
167
168 // waits until the ATTACH has stopped the process
169 // by signal SIGSTOP
170 static bool ptrace_waitpid(pid_t pid) {
171 int ret;
|
21 * questions.
22 *
23 */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <elf.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/ptrace.h>
34 #include <sys/uio.h>
35 #include "libproc_impl.h"
36
37 #if defined(x86_64) && !defined(amd64)
38 #define amd64 1
39 #endif
40
41 #ifdef aarch64
42 #include <sys/uio.h> // For struct iovec
43 #include <elf.h>
44 #endif
45
46 #ifndef __WALL
47 #define __WALL 0x40000000 // Copied from /usr/include/linux/wait.h
48 #endif
49
50 // This file has the libproc implementation specific to live process
51 // For core files, refer to ps_core.c
52
53 static inline uintptr_t align(uintptr_t ptr, size_t size) {
54 return (ptr & ~(size - 1));
55 }
56
57 // ---------------------------------------------
58 // ptrace functions
59 // ---------------------------------------------
60
61 // read "size" bytes of data from "addr" within the target process.
62 // unlike the standard ptrace() function, process_read_data() can handle
63 // unaligned address - alignment check, if required, should be done
64 // before calling process_read_data.
65
137 #define PTRACE_GETREGS_REQ PTRACE_GETREGS
138 #elif defined(PT_GETREGS)
139 #define PTRACE_GETREGS_REQ PT_GETREGS
140 #endif
141
142 #ifdef PTRACE_GETREGS_REQ
143 if (ptrace_getregs(PTRACE_GETREGS_REQ, pid, user, NULL) < 0) {
144 print_debug("ptrace(PTRACE_GETREGS, ...) failed for lwp %d\n", pid);
145 return false;
146 }
147 return true;
148 #elif defined(PTRACE_GETREGSET)
149 struct iovec iov;
150 iov.iov_base = user;
151 iov.iov_len = sizeof(*user);
152 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void*) &iov) < 0) {
153 print_debug("ptrace(PTRACE_GETREGSET, ...) failed for lwp %d\n", pid);
154 return false;
155 }
156 return true;
157 #elif defined(aarch64)
158 {
159 struct iovec iovec;
160 iovec.iov_base = user;
161 iovec.iov_len = sizeof (struct user_regs_struct);
162 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iovec) < 0) {
163 print_debug("ptrace(PTRACE_GETREGSET, ...) failed for lwp %d\n", pid);
164 return false;
165 }
166 return true;
167 }
168 #else
169 print_debug("ptrace(PTRACE_GETREGS, ...) not supported\n");
170 return false;
171 #endif
172
173 }
174
175 static bool ptrace_continue(pid_t pid, int signal) {
176 // pass the signal to the process so we don't swallow it
177 if (ptrace(PTRACE_CONT, pid, NULL, signal) < 0) {
178 print_debug("ptrace(PTRACE_CONT, ..) failed for %d\n", pid);
179 return false;
180 }
181 return true;
182 }
183
184 // waits until the ATTACH has stopped the process
185 // by signal SIGSTOP
186 static bool ptrace_waitpid(pid_t pid) {
187 int ret;
|