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