blob: c5578096bc0f8d718770c287d1fcccfdf61adda0 [file] [log] [blame]
Peter Collingbourne705e3102013-05-21 11:38:39 +00001//===-- sanitizer_stoptheworld_linux_libcdep.cc ---------------------------===//
Alexander Potapenko3614c162013-03-15 14:37:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// See sanitizer_stoptheworld.h for details.
11// This implementation was inspired by Markus Gutschke's linuxthreads.cc.
12//
13//===----------------------------------------------------------------------===//
14
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000015
16#include "sanitizer_platform.h"
Sergey Matveevcb339102013-09-02 11:36:19 +000017#if SANITIZER_LINUX && defined(__x86_64__)
Alexander Potapenko3614c162013-03-15 14:37:21 +000018
19#include "sanitizer_stoptheworld.h"
20
21#include <errno.h>
Sergey Matveevcb339102013-09-02 11:36:19 +000022#include <sched.h> // for CLONE_* definitions
Alexander Potapenko3614c162013-03-15 14:37:21 +000023#include <stddef.h>
24#include <sys/prctl.h> // for PR_* definitions
25#include <sys/ptrace.h> // for PTRACE_* definitions
26#include <sys/types.h> // for pid_t
Sergey Matveev115accb2013-05-13 10:35:20 +000027#if SANITIZER_ANDROID && defined(__arm__)
Alexey Samsonovbb090b52013-04-03 07:06:10 +000028# include <linux/user.h> // for pt_regs
29#else
30# include <sys/user.h> // for user_regs_struct
31#endif
Alexander Potapenko3614c162013-03-15 14:37:21 +000032#include <sys/wait.h> // for signal-related stuff
33
34#include "sanitizer_common.h"
35#include "sanitizer_libc.h"
36#include "sanitizer_linux.h"
37#include "sanitizer_mutex.h"
Dmitry Vyukov49960be2013-03-18 08:09:42 +000038#include "sanitizer_placement_new.h"
Alexander Potapenko3614c162013-03-15 14:37:21 +000039
40// This module works by spawning a Linux task which then attaches to every
41// thread in the caller process with ptrace. This suspends the threads, and
42// PTRACE_GETREGS can then be used to obtain their register state. The callback
43// supplied to StopTheWorld() is run in the tracer task while the threads are
44// suspended.
45// The tracer task must be placed in a different thread group for ptrace to
46// work, so it cannot be spawned as a pthread. Instead, we use the low-level
47// clone() interface (we want to share the address space with the caller
48// process, so we prefer clone() over fork()).
49//
Sergey Matveev2191fca2013-10-15 11:54:38 +000050// We don't use any libc functions, relying instead on direct syscalls. There
51// are two reasons for this:
Alexander Potapenko3614c162013-03-15 14:37:21 +000052// 1. calling a library function while threads are suspended could cause a
53// deadlock, if one of the treads happens to be holding a libc lock;
54// 2. it's generally not safe to call libc functions from the tracer task,
55// because clone() does not set up a thread-local storage for it. Any
56// thread-local variables used by libc will be shared between the tracer task
57// and the thread which spawned it.
Alexander Potapenko3614c162013-03-15 14:37:21 +000058
59COMPILER_CHECK(sizeof(SuspendedThreadID) == sizeof(pid_t));
60
61namespace __sanitizer {
62// This class handles thread suspending/unsuspending in the tracer thread.
63class ThreadSuspender {
64 public:
65 explicit ThreadSuspender(pid_t pid)
66 : pid_(pid) {
67 CHECK_GE(pid, 0);
68 }
69 bool SuspendAllThreads();
70 void ResumeAllThreads();
71 void KillAllThreads();
72 SuspendedThreadsList &suspended_threads_list() {
73 return suspended_threads_list_;
74 }
75 private:
76 SuspendedThreadsList suspended_threads_list_;
77 pid_t pid_;
78 bool SuspendThread(SuspendedThreadID thread_id);
79};
80
81bool ThreadSuspender::SuspendThread(SuspendedThreadID thread_id) {
82 // Are we already attached to this thread?
83 // Currently this check takes linear time, however the number of threads is
84 // usually small.
85 if (suspended_threads_list_.Contains(thread_id))
86 return false;
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000087 int pterrno;
88 if (internal_iserror(internal_ptrace(PTRACE_ATTACH, thread_id, NULL, NULL),
89 &pterrno)) {
Alexander Potapenko3614c162013-03-15 14:37:21 +000090 // Either the thread is dead, or something prevented us from attaching.
91 // Log this event and move on.
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000092 Report("Could not attach to thread %d (errno %d).\n", thread_id, pterrno);
Alexander Potapenko3614c162013-03-15 14:37:21 +000093 return false;
94 } else {
95 if (SanitizerVerbosity > 0)
96 Report("Attached to thread %d.\n", thread_id);
97 // The thread is not guaranteed to stop before ptrace returns, so we must
98 // wait on it.
Peter Collingbourne9578a3e2013-05-08 14:43:49 +000099 uptr waitpid_status;
Alexander Potapenko3614c162013-03-15 14:37:21 +0000100 HANDLE_EINTR(waitpid_status, internal_waitpid(thread_id, NULL, __WALL));
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000101 int wperrno;
102 if (internal_iserror(waitpid_status, &wperrno)) {
Alexander Potapenko3614c162013-03-15 14:37:21 +0000103 // Got a ECHILD error. I don't think this situation is possible, but it
104 // doesn't hurt to report it.
105 Report("Waiting on thread %d failed, detaching (errno %d).\n", thread_id,
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000106 wperrno);
Alexander Potapenko3614c162013-03-15 14:37:21 +0000107 internal_ptrace(PTRACE_DETACH, thread_id, NULL, NULL);
108 return false;
109 }
110 suspended_threads_list_.Append(thread_id);
111 return true;
112 }
113}
114
115void ThreadSuspender::ResumeAllThreads() {
116 for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++) {
117 pid_t tid = suspended_threads_list_.GetThreadID(i);
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000118 int pterrno;
119 if (!internal_iserror(internal_ptrace(PTRACE_DETACH, tid, NULL, NULL),
120 &pterrno)) {
Alexander Potapenko3614c162013-03-15 14:37:21 +0000121 if (SanitizerVerbosity > 0)
122 Report("Detached from thread %d.\n", tid);
123 } else {
124 // Either the thread is dead, or we are already detached.
125 // The latter case is possible, for instance, if this function was called
126 // from a signal handler.
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000127 Report("Could not detach from thread %d (errno %d).\n", tid, pterrno);
Alexander Potapenko3614c162013-03-15 14:37:21 +0000128 }
129 }
130}
131
132void ThreadSuspender::KillAllThreads() {
133 for (uptr i = 0; i < suspended_threads_list_.thread_count(); i++)
134 internal_ptrace(PTRACE_KILL, suspended_threads_list_.GetThreadID(i),
135 NULL, NULL);
136}
137
138bool ThreadSuspender::SuspendAllThreads() {
Alexey Samsonov10f3ab72013-04-05 07:41:21 +0000139 ThreadLister thread_lister(pid_);
Alexander Potapenko3614c162013-03-15 14:37:21 +0000140 bool added_threads;
141 do {
142 // Run through the directory entries once.
143 added_threads = false;
Alexey Samsonov10f3ab72013-04-05 07:41:21 +0000144 pid_t tid = thread_lister.GetNextTID();
Alexander Potapenko3614c162013-03-15 14:37:21 +0000145 while (tid >= 0) {
146 if (SuspendThread(tid))
147 added_threads = true;
Alexey Samsonov10f3ab72013-04-05 07:41:21 +0000148 tid = thread_lister.GetNextTID();
Alexander Potapenko3614c162013-03-15 14:37:21 +0000149 }
Alexey Samsonov10f3ab72013-04-05 07:41:21 +0000150 if (thread_lister.error()) {
Alexander Potapenko3614c162013-03-15 14:37:21 +0000151 // Detach threads and fail.
152 ResumeAllThreads();
153 return false;
154 }
Alexey Samsonov10f3ab72013-04-05 07:41:21 +0000155 thread_lister.Reset();
Alexander Potapenko3614c162013-03-15 14:37:21 +0000156 } while (added_threads);
157 return true;
158}
159
160// Pointer to the ThreadSuspender instance for use in signal handler.
161static ThreadSuspender *thread_suspender_instance = NULL;
162
163// Signals that should not be blocked (this is used in the parent thread as well
164// as the tracer thread).
165static const int kUnblockedSignals[] = { SIGABRT, SIGILL, SIGFPE, SIGSEGV,
166 SIGBUS, SIGXCPU, SIGXFSZ };
167
168// Structure for passing arguments into the tracer thread.
169struct TracerThreadArgument {
170 StopTheWorldCallback callback;
171 void *callback_argument;
Sergey Matveevd41fa1b2013-10-09 13:36:20 +0000172 // The tracer thread waits on this mutex while the parent finishes its
Alexey Samsonov6d036062013-03-18 06:27:13 +0000173 // preparations.
174 BlockingMutex mutex;
Sergey Matveevd41fa1b2013-10-09 13:36:20 +0000175 uptr parent_pid;
Alexander Potapenko3614c162013-03-15 14:37:21 +0000176};
177
Sergey Matveev90629fb2013-08-26 13:20:31 +0000178static DieCallbackType old_die_callback;
179
Alexander Potapenko3614c162013-03-15 14:37:21 +0000180// Signal handler to wake up suspended threads when the tracer thread dies.
181void TracerThreadSignalHandler(int signum, siginfo_t *siginfo, void *) {
182 if (thread_suspender_instance != NULL) {
183 if (signum == SIGABRT)
184 thread_suspender_instance->KillAllThreads();
185 else
186 thread_suspender_instance->ResumeAllThreads();
187 }
188 internal__exit((signum == SIGABRT) ? 1 : 2);
189}
190
Sergey Matveev90629fb2013-08-26 13:20:31 +0000191static void TracerThreadDieCallback() {
192 // Generally a call to Die() in the tracer thread should be fatal to the
193 // parent process as well, because they share the address space.
194 // This really only works correctly if all the threads are suspended at this
195 // point. So we correctly handle calls to Die() from within the callback, but
196 // not those that happen before or after the callback. Hopefully there aren't
197 // a lot of opportunities for that to happen...
198 if (thread_suspender_instance)
199 thread_suspender_instance->KillAllThreads();
200 if (old_die_callback)
201 old_die_callback();
202}
203
Alexander Potapenko3614c162013-03-15 14:37:21 +0000204// Size of alternative stack for signal handlers in the tracer thread.
205static const int kHandlerStackSize = 4096;
206
207// This function will be run as a cloned task.
Alexey Samsonov6d036062013-03-18 06:27:13 +0000208static int TracerThread(void* argument) {
Alexander Potapenko3614c162013-03-15 14:37:21 +0000209 TracerThreadArgument *tracer_thread_argument =
210 (TracerThreadArgument *)argument;
211
Sergey Matveev23a7a432013-10-08 18:01:03 +0000212 internal_prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
213 // Check if parent is already dead.
Sergey Matveevd41fa1b2013-10-09 13:36:20 +0000214 if (internal_getppid() != tracer_thread_argument->parent_pid)
Sergey Matveev23a7a432013-10-08 18:01:03 +0000215 internal__exit(4);
216
Alexander Potapenko3614c162013-03-15 14:37:21 +0000217 // Wait for the parent thread to finish preparations.
Alexey Samsonov6d036062013-03-18 06:27:13 +0000218 tracer_thread_argument->mutex.Lock();
219 tracer_thread_argument->mutex.Unlock();
Alexander Potapenko3614c162013-03-15 14:37:21 +0000220
Sergey Matveev90629fb2013-08-26 13:20:31 +0000221 SetDieCallback(TracerThreadDieCallback);
222
Alexander Potapenko3614c162013-03-15 14:37:21 +0000223 ThreadSuspender thread_suspender(internal_getppid());
224 // Global pointer for the signal handler.
225 thread_suspender_instance = &thread_suspender;
226
227 // Alternate stack for signal handling.
228 InternalScopedBuffer<char> handler_stack_memory(kHandlerStackSize);
229 struct sigaltstack handler_stack;
230 internal_memset(&handler_stack, 0, sizeof(handler_stack));
231 handler_stack.ss_sp = handler_stack_memory.data();
232 handler_stack.ss_size = kHandlerStackSize;
233 internal_sigaltstack(&handler_stack, NULL);
234
235 // Install our handler for fatal signals. Other signals should be blocked by
236 // the mask we inherited from the caller thread.
237 for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
238 signal_index++) {
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000239 kernel_sigaction_t new_sigaction;
Alexander Potapenko3614c162013-03-15 14:37:21 +0000240 internal_memset(&new_sigaction, 0, sizeof(new_sigaction));
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000241 new_sigaction.sigaction = TracerThreadSignalHandler;
Alexander Potapenko3614c162013-03-15 14:37:21 +0000242 new_sigaction.sa_flags = SA_ONSTACK | SA_SIGINFO;
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000243 internal_sigfillset(&new_sigaction.sa_mask);
244 internal_sigaction(kUnblockedSignals[signal_index], &new_sigaction, NULL);
Alexander Potapenko3614c162013-03-15 14:37:21 +0000245 }
246
247 int exit_code = 0;
248 if (!thread_suspender.SuspendAllThreads()) {
249 Report("Failed suspending threads.\n");
250 exit_code = 3;
251 } else {
252 tracer_thread_argument->callback(thread_suspender.suspended_threads_list(),
253 tracer_thread_argument->callback_argument);
254 thread_suspender.ResumeAllThreads();
255 exit_code = 0;
256 }
257 thread_suspender_instance = NULL;
258 handler_stack.ss_flags = SS_DISABLE;
259 internal_sigaltstack(&handler_stack, NULL);
260 return exit_code;
261}
262
Alexander Potapenkofd8726c2013-04-01 14:38:56 +0000263class ScopedStackSpaceWithGuard {
264 public:
265 explicit ScopedStackSpaceWithGuard(uptr stack_size) {
266 stack_size_ = stack_size;
267 guard_size_ = GetPageSizeCached();
268 // FIXME: Omitting MAP_STACK here works in current kernels but might break
269 // in the future.
270 guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_,
271 "ScopedStackWithGuard");
272 CHECK_EQ(guard_start_, (uptr)Mprotect((uptr)guard_start_, guard_size_));
273 }
274 ~ScopedStackSpaceWithGuard() {
275 UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_);
276 }
277 void *Bottom() const {
278 return (void *)(guard_start_ + stack_size_ + guard_size_);
279 }
280
281 private:
282 uptr stack_size_;
283 uptr guard_size_;
284 uptr guard_start_;
285};
286
Sergey Matveev90629fb2013-08-26 13:20:31 +0000287// We have a limitation on the stack frame size, so some stuff had to be moved
288// into globals.
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000289static kernel_sigset_t blocked_sigset;
290static kernel_sigset_t old_sigset;
291static kernel_sigaction_t old_sigactions[ARRAY_SIZE(kUnblockedSignals)];
Dmitry Vyukov49960be2013-03-18 08:09:42 +0000292
Sergey Matveev90629fb2013-08-26 13:20:31 +0000293class StopTheWorldScope {
294 public:
295 StopTheWorldScope() {
Alexey Samsonovc1548202013-08-28 11:26:09 +0000296 // Block all signals that can be blocked safely, and install
297 // default handlers for the remaining signals.
Sergey Matveev90629fb2013-08-26 13:20:31 +0000298 // We cannot allow user-defined handlers to run while the ThreadSuspender
299 // thread is active, because they could conceivably call some libc functions
300 // which modify errno (which is shared between the two threads).
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000301 internal_sigfillset(&blocked_sigset);
Sergey Matveev90629fb2013-08-26 13:20:31 +0000302 for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
303 signal_index++) {
304 // Remove the signal from the set of blocked signals.
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000305 internal_sigdelset(&blocked_sigset, kUnblockedSignals[signal_index]);
Sergey Matveev90629fb2013-08-26 13:20:31 +0000306 // Install the default handler.
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000307 kernel_sigaction_t new_sigaction;
Sergey Matveev90629fb2013-08-26 13:20:31 +0000308 internal_memset(&new_sigaction, 0, sizeof(new_sigaction));
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000309 new_sigaction.handler = SIG_DFL;
310 internal_sigfillset(&new_sigaction.sa_mask);
311 internal_sigaction(kUnblockedSignals[signal_index], &new_sigaction,
Sergey Matveev90629fb2013-08-26 13:20:31 +0000312 &old_sigactions[signal_index]);
313 }
314 int sigprocmask_status =
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000315 internal_sigprocmask(SIG_BLOCK, &blocked_sigset, &old_sigset);
Sergey Matveev90629fb2013-08-26 13:20:31 +0000316 CHECK_EQ(sigprocmask_status, 0); // sigprocmask should never fail
317 // Make this process dumpable. Processes that are not dumpable cannot be
318 // attached to.
319 process_was_dumpable_ = internal_prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
320 if (!process_was_dumpable_)
321 internal_prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
322 old_die_callback = GetDieCallback();
Alexander Potapenko3614c162013-03-15 14:37:21 +0000323 }
Sergey Matveev90629fb2013-08-26 13:20:31 +0000324
325 ~StopTheWorldScope() {
326 SetDieCallback(old_die_callback);
327 // Restore the dumpable flag.
328 if (!process_was_dumpable_)
329 internal_prctl(PR_SET_DUMPABLE, 0, 0, 0, 0);
330 // Restore the signal handlers.
331 for (uptr signal_index = 0; signal_index < ARRAY_SIZE(kUnblockedSignals);
332 signal_index++) {
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000333 internal_sigaction(kUnblockedSignals[signal_index],
Sergey Matveev90629fb2013-08-26 13:20:31 +0000334 &old_sigactions[signal_index], NULL);
335 }
Dmitry Vyukov0f7a2ac2013-10-15 11:31:51 +0000336 internal_sigprocmask(SIG_SETMASK, &old_sigset, &old_sigset);
Sergey Matveev90629fb2013-08-26 13:20:31 +0000337 }
Alexey Samsonovc1548202013-08-28 11:26:09 +0000338
Sergey Matveev90629fb2013-08-26 13:20:31 +0000339 private:
340 int process_was_dumpable_;
341};
342
343void StopTheWorld(StopTheWorldCallback callback, void *argument) {
344 StopTheWorldScope in_stoptheworld;
Alexander Potapenko3614c162013-03-15 14:37:21 +0000345 // Prepare the arguments for TracerThread.
346 struct TracerThreadArgument tracer_thread_argument;
347 tracer_thread_argument.callback = callback;
348 tracer_thread_argument.callback_argument = argument;
Sergey Matveevd41fa1b2013-10-09 13:36:20 +0000349 tracer_thread_argument.parent_pid = internal_getpid();
Alexander Potapenkofd8726c2013-04-01 14:38:56 +0000350 const uptr kTracerStackSize = 2 * 1024 * 1024;
351 ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize);
Alexey Samsonov6d036062013-03-18 06:27:13 +0000352 // Block the execution of TracerThread until after we have set ptrace
353 // permissions.
354 tracer_thread_argument.mutex.Lock();
Sergey Matveevcb339102013-09-02 11:36:19 +0000355 uptr tracer_pid = internal_clone(
356 TracerThread, tracer_stack.Bottom(),
357 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,
358 &tracer_thread_argument, 0 /* parent_tidptr */, 0 /* newtls */, 0
359 /* child_tidptr */);
360 int local_errno = 0;
361 if (internal_iserror(tracer_pid, &local_errno)) {
362 Report("Failed spawning a tracer thread (errno %d).\n", local_errno);
Alexey Samsonov6d036062013-03-18 06:27:13 +0000363 tracer_thread_argument.mutex.Unlock();
Alexander Potapenko3614c162013-03-15 14:37:21 +0000364 } else {
365 // On some systems we have to explicitly declare that we want to be traced
366 // by the tracer thread.
367#ifdef PR_SET_PTRACER
368 internal_prctl(PR_SET_PTRACER, tracer_pid, 0, 0, 0);
369#endif
370 // Allow the tracer thread to start.
Alexey Samsonov6d036062013-03-18 06:27:13 +0000371 tracer_thread_argument.mutex.Unlock();
Alexander Potapenko3614c162013-03-15 14:37:21 +0000372 // Since errno is shared between this thread and the tracer thread, we
373 // must avoid using errno while the tracer thread is running.
374 // At this point, any signal will either be blocked or kill us, so waitpid
375 // should never return (and set errno) while the tracer thread is alive.
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000376 uptr waitpid_status = internal_waitpid(tracer_pid, NULL, __WALL);
Sergey Matveevcb339102013-09-02 11:36:19 +0000377 if (internal_iserror(waitpid_status, &local_errno))
378 Report("Waiting on the tracer thread failed (errno %d).\n", local_errno);
Alexander Potapenko3614c162013-03-15 14:37:21 +0000379 }
Alexander Potapenko3614c162013-03-15 14:37:21 +0000380}
381
Alexander Potapenko53c18d72013-04-01 13:36:42 +0000382// Platform-specific methods from SuspendedThreadsList.
Sergey Matveev115accb2013-05-13 10:35:20 +0000383#if SANITIZER_ANDROID && defined(__arm__)
Alexey Samsonovbb090b52013-04-03 07:06:10 +0000384typedef pt_regs regs_struct;
Sergey Matveev115accb2013-05-13 10:35:20 +0000385#define REG_SP ARM_sp
386
387#elif SANITIZER_LINUX && defined(__arm__)
388typedef user_regs regs_struct;
389#define REG_SP uregs[13]
390
391#elif defined(__i386__) || defined(__x86_64__)
Alexey Samsonovbb090b52013-04-03 07:06:10 +0000392typedef user_regs_struct regs_struct;
Sergey Matveev115accb2013-05-13 10:35:20 +0000393#if defined(__i386__)
394#define REG_SP esp
395#else
396#define REG_SP rsp
Alexey Samsonovbb090b52013-04-03 07:06:10 +0000397#endif
398
Kostya Serebryanyf931da82013-05-15 12:36:29 +0000399#elif defined(__powerpc__) || defined(__powerpc64__)
400typedef pt_regs regs_struct;
401#define REG_SP gpr[PT_R1]
402
Kostya Serebryany40527a52013-06-03 14:49:25 +0000403#elif defined(__mips__)
404typedef struct user regs_struct;
405#define REG_SP regs[EF_REG29]
406
Sergey Matveev115accb2013-05-13 10:35:20 +0000407#else
408#error "Unsupported architecture"
409#endif // SANITIZER_ANDROID && defined(__arm__)
410
Alexander Potapenko53c18d72013-04-01 13:36:42 +0000411int SuspendedThreadsList::GetRegistersAndSP(uptr index,
412 uptr *buffer,
413 uptr *sp) const {
414 pid_t tid = GetThreadID(index);
Alexey Samsonovbb090b52013-04-03 07:06:10 +0000415 regs_struct regs;
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000416 int pterrno;
417 if (internal_iserror(internal_ptrace(PTRACE_GETREGS, tid, NULL, &regs),
418 &pterrno)) {
Alexander Potapenko53c18d72013-04-01 13:36:42 +0000419 Report("Could not get registers from thread %d (errno %d).\n",
Peter Collingbourne9578a3e2013-05-08 14:43:49 +0000420 tid, pterrno);
Alexander Potapenko53c18d72013-04-01 13:36:42 +0000421 return -1;
422 }
Sergey Matveev115accb2013-05-13 10:35:20 +0000423
424 *sp = regs.REG_SP;
Alexander Potapenko53c18d72013-04-01 13:36:42 +0000425 internal_memcpy(buffer, &regs, sizeof(regs));
426 return 0;
427}
428
429uptr SuspendedThreadsList::RegisterCount() {
Alexey Samsonovbb090b52013-04-03 07:06:10 +0000430 return sizeof(regs_struct) / sizeof(uptr);
Alexander Potapenko53c18d72013-04-01 13:36:42 +0000431}
Alexander Potapenko3614c162013-03-15 14:37:21 +0000432} // namespace __sanitizer
433
Sergey Matveevcb339102013-09-02 11:36:19 +0000434#endif // SANITIZER_LINUX && defined(__x86_64__)