]>
Commit | Line | Data |
---|---|---|
ef1b3fda KS |
1 | //===-- sanitizer_stoptheworld_linux_libcdep.cc ---------------------------===// |
2 | // | |
3 | // This file is distributed under the University of Illinois Open Source | |
4 | // License. See LICENSE.TXT for details. | |
5 | // | |
6 | //===----------------------------------------------------------------------===// | |
7 | // | |
8 | // See sanitizer_stoptheworld.h for details. | |
9 | // This implementation was inspired by Markus Gutschke's linuxthreads.cc. | |
10 | // | |
11 | //===----------------------------------------------------------------------===// | |
12 | ||
ef1b3fda | 13 | #include "sanitizer_platform.h" |
696d846a MO |
14 | |
15 | #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__) || \ | |
10189819 MO |
16 | defined(__aarch64__) || defined(__powerpc64__) || \ |
17 | defined(__s390__)) | |
ef1b3fda KS |
18 | |
19 | #include "sanitizer_stoptheworld.h" | |
20 | ||
df77f0e4 | 21 | #include "sanitizer_platform_limits_posix.h" |
696d846a | 22 | #include "sanitizer_atomic.h" |
df77f0e4 | 23 | |
ef1b3fda KS |
24 | #include <errno.h> |
25 | #include <sched.h> // for CLONE_* definitions | |
26 | #include <stddef.h> | |
27 | #include <sys/prctl.h> // for PR_* definitions | |
28 | #include <sys/ptrace.h> // for PTRACE_* definitions | |
29 | #include <sys/types.h> // for pid_t | |
696d846a MO |
30 | #include <sys/uio.h> // for iovec |
31 | #include <elf.h> // for NT_PRSTATUS | |
ef1b3fda KS |
32 | #if SANITIZER_ANDROID && defined(__arm__) |
33 | # include <linux/user.h> // for pt_regs | |
34 | #else | |
696d846a MO |
35 | # ifdef __aarch64__ |
36 | // GLIBC 2.20+ sys/user does not include asm/ptrace.h | |
37 | # include <asm/ptrace.h> | |
38 | # endif | |
ef1b3fda | 39 | # include <sys/user.h> // for user_regs_struct |
10189819 MO |
40 | # if SANITIZER_ANDROID && SANITIZER_MIPS |
41 | # include <asm/reg.h> // for mips SP register in sys/user.h | |
42 | # endif | |
ef1b3fda KS |
43 | #endif |
44 | #include <sys/wait.h> // for signal-related stuff | |
45 | ||
df77f0e4 KS |
46 | #ifdef sa_handler |
47 | # undef sa_handler | |
48 | #endif | |
49 | ||
50 | #ifdef sa_sigaction | |
51 | # undef sa_sigaction | |
52 | #endif | |
53 | ||
ef1b3fda | 54 | #include "sanitizer_common.h" |
df77f0e4 | 55 | #include "sanitizer_flags.h" |
ef1b3fda KS |
56 | #include "sanitizer_libc.h" |
57 | #include "sanitizer_linux.h" | |
58 | #include "sanitizer_mutex.h" | |
59 | #include "sanitizer_placement_new.h" | |
60 | ||
61 | // This module works by spawning a Linux task which then attaches to every | |
62 | // thread in the caller process with ptrace. This suspends the threads, and | |
63 | // PTRACE_GETREGS can then be used to obtain their register state. The callback | |
64 | // supplied to StopTheWorld() is run in the tracer task while the threads are | |
65 | // suspended. | |
66 | // The tracer task must be placed in a different thread group for ptrace to | |
67 | // work, so it cannot be spawned as a pthread. Instead, we use the low-level | |
68 | // clone() interface (we want to share the address space with the caller | |
69 | // process, so we prefer clone() over fork()). | |
70 | // | |
df77f0e4 KS |
71 | // We don't use any libc functions, relying instead on direct syscalls. There |
72 | // are two reasons for this: | |
ef1b3fda KS |
73 | // 1. calling a library function while threads are suspended could cause a |
74 | // deadlock, if one of the treads happens to be holding a libc lock; | |
75 | // 2. it's generally not safe to call libc functions from the tracer task, | |
76 | // because clone() does not set up a thread-local storage for it. Any | |
77 | // thread-local variables used by libc will be shared between the tracer task | |
78 | // and the thread which spawned it. | |
ef1b3fda | 79 | |
ef1b3fda | 80 | namespace __sanitizer { |
696d846a | 81 | |
10189819 MO |
82 | COMPILER_CHECK(sizeof(SuspendedThreadID) == sizeof(pid_t)); |
83 | ||
696d846a MO |
84 | // Structure for passing arguments into the tracer thread. |
85 | struct TracerThreadArgument { | |
86 | StopTheWorldCallback callback; | |
87 | void *callback_argument; | |
88 | // The tracer thread waits on this mutex while the parent finishes its | |
89 | // preparations. | |
90 | BlockingMutex mutex; | |
91 | // Tracer thread signals its completion by setting done. | |
92 | atomic_uintptr_t done; | |
93 | uptr parent_pid; | |
94 | }; | |
95 | ||
ef1b3fda KS |
96 | // This class handles thread suspending/unsuspending in the tracer thread. |
97 | class ThreadSuspender { | |
98 | public: | |
696d846a MO |
99 | explicit ThreadSuspender(pid_t pid, TracerThreadArgument *arg) |
100 | : arg(arg) | |
101 | , pid_(pid) { | |
ef1b3fda KS |
102 | CHECK_GE(pid, 0); |
103 | } | |
104 | bool SuspendAllThreads(); | |
105 | void ResumeAllThreads(); | |
106 | void KillAllThreads(); | |
107 | SuspendedThreadsList &suspended_threads_list() { | |
108 | return suspended_threads_list_; | |
109 | } | |
696d846a | 110 | TracerThreadArgument *arg; |
ef1b3fda KS |
111 | private: |
112 | SuspendedThreadsList suspended_threads_list_; | |
113 | pid_t pid_; | |
114 | bool SuspendThread(SuspendedThreadID thread_id); | |
115 | }; | |
116 | ||
696d846a | 117 | bool ThreadSuspender::SuspendThread(SuspendedThreadID tid) { |
ef1b3fda KS |
118 | // Are we already attached to this thread? |
119 | // Currently this check takes linear time, however the number of threads is | |
120 | // usually small. | |
696d846a | 121 | if (suspended_threads_list_.Contains(tid)) |
ef1b3fda KS |
122 | return false; |
123 | int pterrno; | |
696d846a | 124 | if (internal_iserror(internal_ptrace(PTRACE_ATTACH, tid, nullptr, nullptr), |
ef1b3fda KS |
125 | &pterrno)) { |
126 | // Either the thread is dead, or something prevented us from attaching. | |
127 | // Log this event and move on. | |
696d846a | 128 | VReport(1, "Could not attach to thread %d (errno %d).\n", tid, pterrno); |
ef1b3fda KS |
129 | return false; |
130 | } else { | |
696d846a | 131 | VReport(2, "Attached to thread %d.\n", tid); |
ef1b3fda | 132 | // The thread is not guaranteed to stop before ptrace returns, so we must |
696d846a MO |
133 | // wait on it. Note: if the thread receives a signal concurrently, |
134 | // we can get notification about the signal before notification about stop. | |
135 | // In such case we need to forward the signal to the thread, otherwise | |
136 | // the signal will be missed (as we do PTRACE_DETACH with arg=0) and | |
137 | // any logic relying on signals will break. After forwarding we need to | |
138 | // continue to wait for stopping, because the thread is not stopped yet. | |
139 | // We do ignore delivery of SIGSTOP, because we want to make stop-the-world | |
140 | // as invisible as possible. | |
141 | for (;;) { | |
142 | int status; | |
143 | uptr waitpid_status; | |
144 | HANDLE_EINTR(waitpid_status, internal_waitpid(tid, &status, __WALL)); | |
145 | int wperrno; | |
146 | if (internal_iserror(waitpid_status, &wperrno)) { | |
147 | // Got a ECHILD error. I don't think this situation is possible, but it | |
148 | // doesn't hurt to report it. | |
149 | VReport(1, "Waiting on thread %d failed, detaching (errno %d).\n", | |
150 | tid, wperrno); | |
151 | internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr); | |
152 | return false; | |
153 | } | |
154 | if (WIFSTOPPED(status) && WSTOPSIG(status) != SIGSTOP) { | |
155 | internal_ptrace(PTRACE_CONT, tid, nullptr, | |
156 | (void*)(uptr)WSTOPSIG(status)); | |
157 | continue; | |
158 | } | |
159 | break; | |
ef1b3fda | 160 | } |
696d846a | 161 | suspended_threads_list_.Append(tid); |
ef1b3fda KS |
162 | return true; |
163 | } | |
164 | } | |
165 | ||
166 | void ThreadSuspender::ResumeAllThreads() { | |
167 | for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++) { | |
168 | pid_t tid = suspended_threads_list_.GetThreadID(i); | |
169 | int pterrno; | |
696d846a | 170 | if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, nullptr, nullptr), |
ef1b3fda | 171 | &pterrno)) { |
696d846a | 172 | VReport(2, "Detached from thread %d.\n", tid); |
ef1b3fda KS |
173 | } else { |
174 | // Either the thread is dead, or we are already detached. | |
175 | // The latter case is possible, for instance, if this function was called | |
176 | // from a signal handler. | |
dee5ea7a | 177 | VReport(1, "Could not detach from thread %d (errno %d).\n", tid, pterrno); |
ef1b3fda KS |
178 | } |
179 | } | |
180 | } | |
181 | ||
182 | void ThreadSuspender::KillAllThreads() { | |
183 | for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++) | |
184 | internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i), | |
696d846a | 185 | nullptr, nullptr); |
ef1b3fda KS |
186 | } |
187 | ||
188 | bool ThreadSuspender::SuspendAllThreads() { | |
189 | ThreadLister thread_lister(pid_); | |
190 | bool added_threads; | |
10189819 | 191 | bool first_iteration = true; |
ef1b3fda KS |
192 | do { |
193 | // Run through the directory entries once. | |
194 | added_threads = false; | |
195 | pid_t tid = thread_lister.GetNextTID(); | |
196 | while (tid >= 0) { | |
197 | if (SuspendThread(tid)) | |
198 | added_threads = true; | |
199 | tid = thread_lister.GetNextTID(); | |
200 | } | |
10189819 | 201 | if (thread_lister.error() || (first_iteration && !added_threads)) { |
ef1b3fda KS |
202 | // Detach threads and fail. |
203 | ResumeAllThreads(); | |
204 | return false; | |
205 | } | |
206 | thread_lister.Reset(); | |
10189819 | 207 | first_iteration = false; |
ef1b3fda KS |
208 | } while (added_threads); |
209 | return true; | |
210 | } | |
211 | ||
212 | // Pointer to the ThreadSuspender instance for use in signal handler. | |
696d846a | 213 | static ThreadSuspender *thread_suspender_instance = nullptr; |
ef1b3fda | 214 | |
696d846a MO |
215 | // Synchronous signals that should not be blocked. |
216 | static const int kSyncSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV, SIGBUS, | |
217 | SIGXCPU, SIGXFSZ }; | |
ef1b3fda KS |
218 | |
219 | static void TracerThreadDieCallback() { | |
220 | // Generally a call to Die() in the tracer thread should be fatal to the | |
221 | // parent process as well, because they share the address space. | |
222 | // This really only works correctly if all the threads are suspended at this | |
223 | // point. So we correctly handle calls to Die() from within the callback, but | |
224 | // not those that happen before or after the callback. Hopefully there aren't | |
225 | // a lot of opportunities for that to happen... | |
696d846a MO |
226 | ThreadSuspender *inst = thread_suspender_instance; |
227 | if (inst && stoptheworld_tracer_pid == internal_getpid()) { | |
228 | inst->KillAllThreads(); | |
229 | thread_suspender_instance = nullptr; | |
230 | } | |
231 | } | |
232 | ||
233 | // Signal handler to wake up suspended threads when the tracer thread dies. | |
234 | static void TracerThreadSignalHandler(int signum, void *siginfo, void *uctx) { | |
235 | SignalContext ctx = SignalContext::Create(siginfo, uctx); | |
10189819 MO |
236 | Printf("Tracer caught signal %d: addr=0x%zx pc=0x%zx sp=0x%zx\n", signum, |
237 | ctx.addr, ctx.pc, ctx.sp); | |
696d846a MO |
238 | ThreadSuspender *inst = thread_suspender_instance; |
239 | if (inst) { | |
240 | if (signum == SIGABRT) | |
241 | inst->KillAllThreads(); | |
242 | else | |
243 | inst->ResumeAllThreads(); | |
244 | RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback)); | |
245 | thread_suspender_instance = nullptr; | |
246 | atomic_store(&inst->arg->done, 1, memory_order_relaxed); | |
247 | } | |
248 | internal__exit((signum == SIGABRT) ? 1 : 2); | |
ef1b3fda KS |
249 | } |
250 | ||
251 | // Size of alternative stack for signal handlers in the tracer thread. | |
252 | static const int kHandlerStackSize = 4096; | |
253 | ||
254 | // This function will be run as a cloned task. | |
255 | static int TracerThread(void* argument) { | |
256 | TracerThreadArgument *tracer_thread_argument = | |
257 | (TracerThreadArgument *)argument; | |
258 | ||
df77f0e4 KS |
259 | internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); |
260 | // Check if parent is already dead. | |
261 | if (internal_getppid() != tracer_thread_argument->parent_pid) | |
262 | internal__exit(4); | |
263 | ||
ef1b3fda KS |
264 | // Wait for the parent thread to finish preparations. |
265 | tracer_thread_argument->mutex.Lock(); | |
266 | tracer_thread_argument->mutex.Unlock(); | |
267 | ||
696d846a | 268 | RAW_CHECK(AddDieCallback(TracerThreadDieCallback)); |
ef1b3fda | 269 | |
696d846a | 270 | ThreadSuspender thread_suspender(internal_getppid(), tracer_thread_argument); |
ef1b3fda KS |
271 | // Global pointer for the signal handler. |
272 | thread_suspender_instance = &thread_suspender; | |
273 | ||
274 | // Alternate stack for signal handling. | |
275 | InternalScopedBuffer<char> handler_stack_memory(kHandlerStackSize); | |
144e36a7 | 276 | stack_t handler_stack; |
ef1b3fda KS |
277 | internal_memset(&handler_stack, 0, sizeof(handler_stack)); |
278 | handler_stack.ss_sp = handler_stack_memory.data(); | |
279 | handler_stack.ss_size = kHandlerStackSize; | |
696d846a MO |
280 | internal_sigaltstack(&handler_stack, nullptr); |
281 | ||
282 | // Install our handler for synchronous signals. Other signals should be | |
283 | // blocked by the mask we inherited from the parent thread. | |
284 | for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++) { | |
285 | __sanitizer_sigaction act; | |
286 | internal_memset(&act, 0, sizeof(act)); | |
287 | act.sigaction = TracerThreadSignalHandler; | |
288 | act.sa_flags = SA_ONSTACK | SA_SIGINFO; | |
289 | internal_sigaction_norestorer(kSyncSignals[i], &act, 0); | |
ef1b3fda KS |
290 | } |
291 | ||
292 | int exit_code = 0; | |
293 | if (!thread_suspender.SuspendAllThreads()) { | |
dee5ea7a | 294 | VReport(1, "Failed suspending threads.\n"); |
ef1b3fda KS |
295 | exit_code = 3; |
296 | } else { | |
297 | tracer_thread_argument->callback(thread_suspender.suspended_threads_list(), | |
298 | tracer_thread_argument->callback_argument); | |
299 | thread_suspender.ResumeAllThreads(); | |
300 | exit_code = 0; | |
301 | } | |
696d846a MO |
302 | RAW_CHECK(RemoveDieCallback(TracerThreadDieCallback)); |
303 | thread_suspender_instance = nullptr; | |
304 | atomic_store(&tracer_thread_argument->done, 1, memory_order_relaxed); | |
ef1b3fda KS |
305 | return exit_code; |
306 | } | |
307 | ||
308 | class ScopedStackSpaceWithGuard { | |
309 | public: | |
310 | explicit ScopedStackSpaceWithGuard(uptr stack_size) { | |
311 | stack_size_ = stack_size; | |
312 | guard_size_ = GetPageSizeCached(); | |
313 | // FIXME: Omitting MAP_STACK here works in current kernels but might break | |
314 | // in the future. | |
315 | guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_, | |
316 | "ScopedStackWithGuard"); | |
696d846a | 317 | CHECK(MprotectNoAccess((uptr)guard_start_, guard_size_)); |
ef1b3fda KS |
318 | } |
319 | ~ScopedStackSpaceWithGuard() { | |
320 | UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_); | |
321 | } | |
322 | void *Bottom() const { | |
323 | return (void *)(guard_start_ + stack_size_ + guard_size_); | |
324 | } | |
325 | ||
326 | private: | |
327 | uptr stack_size_; | |
328 | uptr guard_size_; | |
329 | uptr guard_start_; | |
330 | }; | |
331 | ||
ef1b3fda KS |
332 | // We have a limitation on the stack frame size, so some stuff had to be moved |
333 | // into globals. | |
dee5ea7a KS |
334 | static __sanitizer_sigset_t blocked_sigset; |
335 | static __sanitizer_sigset_t old_sigset; | |
ef1b3fda KS |
336 | |
337 | class StopTheWorldScope { | |
338 | public: | |
339 | StopTheWorldScope() { | |
ef1b3fda KS |
340 | // Make this process dumpable. Processes that are not dumpable cannot be |
341 | // attached to. | |
342 | process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0); | |
343 | if (!process_was_dumpable_) | |
344 | internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); | |
ef1b3fda KS |
345 | } |
346 | ||
347 | ~StopTheWorldScope() { | |
ef1b3fda KS |
348 | // Restore the dumpable flag. |
349 | if (!process_was_dumpable_) | |
350 | internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); | |
ef1b3fda KS |
351 | } |
352 | ||
353 | private: | |
354 | int process_was_dumpable_; | |
355 | }; | |
356 | ||
c4c16f74 KS |
357 | // When sanitizer output is being redirected to file (i.e. by using log_path), |
358 | // the tracer should write to the parent's log instead of trying to open a new | |
359 | // file. Alert the logging code to the fact that we have a tracer. | |
360 | struct ScopedSetTracerPID { | |
361 | explicit ScopedSetTracerPID(uptr tracer_pid) { | |
362 | stoptheworld_tracer_pid = tracer_pid; | |
363 | stoptheworld_tracer_ppid = internal_getpid(); | |
364 | } | |
365 | ~ScopedSetTracerPID() { | |
366 | stoptheworld_tracer_pid = 0; | |
367 | stoptheworld_tracer_ppid = 0; | |
368 | } | |
369 | }; | |
370 | ||
ef1b3fda KS |
371 | void StopTheWorld(StopTheWorldCallback callback, void *argument) { |
372 | StopTheWorldScope in_stoptheworld; | |
373 | // Prepare the arguments for TracerThread. | |
374 | struct TracerThreadArgument tracer_thread_argument; | |
375 | tracer_thread_argument.callback = callback; | |
376 | tracer_thread_argument.callback_argument = argument; | |
df77f0e4 | 377 | tracer_thread_argument.parent_pid = internal_getpid(); |
696d846a | 378 | atomic_store(&tracer_thread_argument.done, 0, memory_order_relaxed); |
ef1b3fda KS |
379 | const uptr kTracerStackSize = 2 * 1024 * 1024; |
380 | ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize); | |
381 | // Block the execution of TracerThread until after we have set ptrace | |
382 | // permissions. | |
383 | tracer_thread_argument.mutex.Lock(); | |
696d846a MO |
384 | // Signal handling story. |
385 | // We don't want async signals to be delivered to the tracer thread, | |
386 | // so we block all async signals before creating the thread. An async signal | |
387 | // handler can temporary modify errno, which is shared with this thread. | |
388 | // We ought to use pthread_sigmask here, because sigprocmask has undefined | |
389 | // behavior in multithreaded programs. However, on linux sigprocmask is | |
390 | // equivalent to pthread_sigmask with the exception that pthread_sigmask | |
391 | // does not allow to block some signals used internally in pthread | |
392 | // implementation. We are fine with blocking them here, we are really not | |
393 | // going to pthread_cancel the thread. | |
394 | // The tracer thread should not raise any synchronous signals. But in case it | |
395 | // does, we setup a special handler for sync signals that properly kills the | |
396 | // parent as well. Note: we don't pass CLONE_SIGHAND to clone, so handlers | |
397 | // in the tracer thread won't interfere with user program. Double note: if a | |
398 | // user does something along the lines of 'kill -11 pid', that can kill the | |
399 | // process even if user setup own handler for SEGV. | |
400 | // Thing to watch out for: this code should not change behavior of user code | |
401 | // in any observable way. In particular it should not override user signal | |
402 | // handlers. | |
403 | internal_sigfillset(&blocked_sigset); | |
404 | for (uptr i = 0; i < ARRAY_SIZE(kSyncSignals); i++) | |
405 | internal_sigdelset(&blocked_sigset, kSyncSignals[i]); | |
406 | int rv = internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset); | |
407 | CHECK_EQ(rv, 0); | |
ef1b3fda KS |
408 | uptr tracer_pid = internal_clone( |
409 | TracerThread, tracer_stack.Bottom(), | |
410 | CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED, | |
696d846a MO |
411 | &tracer_thread_argument, nullptr /* parent_tidptr */, |
412 | nullptr /* newtls */, nullptr /* child_tidptr */); | |
413 | internal_sigprocmask(SIG_SETMASK, &old_sigset, 0); | |
ef1b3fda KS |
414 | int local_errno = 0; |
415 | if (internal_iserror(tracer_pid, &local_errno)) { | |
dee5ea7a | 416 | VReport(1, "Failed spawning a tracer thread (errno %d).\n", local_errno); |
ef1b3fda KS |
417 | tracer_thread_argument.mutex.Unlock(); |
418 | } else { | |
c4c16f74 | 419 | ScopedSetTracerPID scoped_set_tracer_pid(tracer_pid); |
ef1b3fda KS |
420 | // On some systems we have to explicitly declare that we want to be traced |
421 | // by the tracer thread. | |
422 | #ifdef PR_SET_PTRACER | |
423 | internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0); | |
424 | #endif | |
425 | // Allow the tracer thread to start. | |
426 | tracer_thread_argument.mutex.Unlock(); | |
696d846a MO |
427 | // NOTE: errno is shared between this thread and the tracer thread. |
428 | // internal_waitpid() may call syscall() which can access/spoil errno, | |
429 | // so we can't call it now. Instead we for the tracer thread to finish using | |
430 | // the spin loop below. Man page for sched_yield() says "In the Linux | |
431 | // implementation, sched_yield() always succeeds", so let's hope it does not | |
432 | // spoil errno. Note that this spin loop runs only for brief periods before | |
433 | // the tracer thread has suspended us and when it starts unblocking threads. | |
434 | while (atomic_load(&tracer_thread_argument.done, memory_order_relaxed) == 0) | |
435 | sched_yield(); | |
436 | // Now the tracer thread is about to exit and does not touch errno, | |
437 | // wait for it. | |
438 | for (;;) { | |
439 | uptr waitpid_status = internal_waitpid(tracer_pid, nullptr, __WALL); | |
440 | if (!internal_iserror(waitpid_status, &local_errno)) | |
441 | break; | |
442 | if (local_errno == EINTR) | |
443 | continue; | |
dee5ea7a KS |
444 | VReport(1, "Waiting on the tracer thread failed (errno %d).\n", |
445 | local_errno); | |
696d846a MO |
446 | break; |
447 | } | |
ef1b3fda KS |
448 | } |
449 | } | |
450 | ||
451 | // Platform-specific methods from SuspendedThreadsList. | |
452 | #if SANITIZER_ANDROID && defined(__arm__) | |
453 | typedef pt_regs regs_struct; | |
454 | #define REG_SP ARM_sp | |
455 | ||
456 | #elif SANITIZER_LINUX && defined(__arm__) | |
457 | typedef user_regs regs_struct; | |
458 | #define REG_SP uregs[13] | |
459 | ||
460 | #elif defined(__i386__) || defined(__x86_64__) | |
461 | typedef user_regs_struct regs_struct; | |
462 | #if defined(__i386__) | |
463 | #define REG_SP esp | |
464 | #else | |
465 | #define REG_SP rsp | |
466 | #endif | |
467 | ||
468 | #elif defined(__powerpc__) || defined(__powerpc64__) | |
469 | typedef pt_regs regs_struct; | |
470 | #define REG_SP gpr[PT_R1] | |
471 | ||
472 | #elif defined(__mips__) | |
473 | typedef struct user regs_struct; | |
10189819 MO |
474 | # if SANITIZER_ANDROID |
475 | # define REG_SP regs[EF_R29] | |
476 | # else | |
477 | # define REG_SP regs[EF_REG29] | |
478 | # endif | |
ef1b3fda | 479 | |
696d846a MO |
480 | #elif defined(__aarch64__) |
481 | typedef struct user_pt_regs regs_struct; | |
482 | #define REG_SP sp | |
483 | #define ARCH_IOVEC_FOR_GETREGSET | |
484 | ||
10189819 MO |
485 | #elif defined(__s390__) |
486 | typedef _user_regs_struct regs_struct; | |
487 | #define REG_SP gprs[15] | |
488 | #define ARCH_IOVEC_FOR_GETREGSET | |
489 | ||
ef1b3fda KS |
490 | #else |
491 | #error "Unsupported architecture" | |
492 | #endif // SANITIZER_ANDROID && defined(__arm__) | |
493 | ||
494 | int SuspendedThreadsList::GetRegistersAndSP(uptr index, | |
495 | uptr *buffer, | |
496 | uptr *sp) const { | |
497 | pid_t tid = GetThreadID(index); | |
498 | regs_struct regs; | |
499 | int pterrno; | |
696d846a MO |
500 | #ifdef ARCH_IOVEC_FOR_GETREGSET |
501 | struct iovec regset_io; | |
502 | regset_io.iov_base = ®s; | |
503 | regset_io.iov_len = sizeof(regs_struct); | |
504 | bool isErr = internal_iserror(internal_ptrace(PTRACE_GETREGSET, tid, | |
505 | (void*)NT_PRSTATUS, (void*)®set_io), | |
506 | &pterrno); | |
507 | #else | |
508 | bool isErr = internal_iserror(internal_ptrace(PTRACE_GETREGS, tid, nullptr, | |
509 | ®s), &pterrno); | |
510 | #endif | |
511 | if (isErr) { | |
dee5ea7a KS |
512 | VReport(1, "Could not get registers from thread %d (errno %d).\n", tid, |
513 | pterrno); | |
ef1b3fda KS |
514 | return -1; |
515 | } | |
516 | ||
517 | *sp = regs.REG_SP; | |
518 | internal_memcpy(buffer, ®s, sizeof(regs)); | |
519 | return 0; | |
520 | } | |
521 | ||
522 | uptr SuspendedThreadsList::RegisterCount() { | |
523 | return sizeof(regs_struct) / sizeof(uptr); | |
524 | } | |
696d846a | 525 | } // namespace __sanitizer |
ef1b3fda | 526 | |
10189819 MO |
527 | #endif // SANITIZER_LINUX && (defined(__x86_64__) || defined(__mips__) |
528 | // || defined(__aarch64__) || defined(__powerpc64__) | |
529 | // || defined(__s390__) |