1 //===-- sanitizer_stoptheworld_mac.cc -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // See sanitizer_stoptheworld.h for details.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
16 #if SANITIZER_MAC && (defined(__x86_64__) || defined(__aarch64__) || \
19 #include <mach/mach.h>
20 #include <mach/thread_info.h>
23 #include "sanitizer_stoptheworld.h"
25 namespace __sanitizer {
29 } SuspendedThreadInfo;
31 class SuspendedThreadsListMac : public SuspendedThreadsList {
33 SuspendedThreadsListMac() : threads_(1024) {}
35 tid_t GetThreadID(uptr index) const;
36 thread_t GetThread(uptr index) const;
37 uptr ThreadCount() const;
38 bool ContainsThread(thread_t thread) const;
39 void Append(thread_t thread);
41 PtraceRegistersStatus GetRegistersAndSP(uptr index, uptr *buffer,
43 uptr RegisterCount() const;
46 InternalMmapVector<SuspendedThreadInfo> threads_;
49 struct RunThreadArgs {
50 StopTheWorldCallback callback;
54 void RunThread(void *arg) {
55 struct RunThreadArgs *run_args = (struct RunThreadArgs *)arg;
56 SuspendedThreadsListMac suspended_threads_list;
58 thread_array_t threads;
59 mach_msg_type_number_t num_threads;
60 kern_return_t err = task_threads(mach_task_self(), &threads, &num_threads);
61 if (err != KERN_SUCCESS) {
62 VReport(1, "Failed to get threads for task (errno %d).\n", err);
66 thread_t thread_self = mach_thread_self();
67 for (unsigned int i = 0; i < num_threads; ++i) {
68 if (threads[i] == thread_self) continue;
70 thread_suspend(threads[i]);
71 suspended_threads_list.Append(threads[i]);
74 run_args->callback(suspended_threads_list, run_args->argument);
76 uptr num_suspended = suspended_threads_list.ThreadCount();
77 for (unsigned int i = 0; i < num_suspended; ++i) {
78 thread_resume(suspended_threads_list.GetThread(i));
82 void StopTheWorld(StopTheWorldCallback callback, void *argument) {
83 struct RunThreadArgs arg = {callback, argument};
84 pthread_t run_thread = (pthread_t)internal_start_thread(RunThread, &arg);
85 internal_join_thread(run_thread);
88 #if defined(__x86_64__)
89 typedef x86_thread_state64_t regs_struct;
93 #elif defined(__aarch64__)
94 typedef arm_thread_state64_t regs_struct;
102 #elif defined(__i386)
103 typedef x86_thread_state32_t regs_struct;
108 #error "Unsupported architecture"
111 tid_t SuspendedThreadsListMac::GetThreadID(uptr index) const {
112 CHECK_LT(index, threads_.size());
113 return threads_[index].tid;
116 thread_t SuspendedThreadsListMac::GetThread(uptr index) const {
117 CHECK_LT(index, threads_.size());
118 return threads_[index].thread;
121 uptr SuspendedThreadsListMac::ThreadCount() const {
122 return threads_.size();
125 bool SuspendedThreadsListMac::ContainsThread(thread_t thread) const {
126 for (uptr i = 0; i < threads_.size(); i++) {
127 if (threads_[i].thread == thread) return true;
132 void SuspendedThreadsListMac::Append(thread_t thread) {
133 thread_identifier_info_data_t info;
134 mach_msg_type_number_t info_count = THREAD_IDENTIFIER_INFO_COUNT;
135 kern_return_t err = thread_info(thread, THREAD_IDENTIFIER_INFO,
136 (thread_info_t)&info, &info_count);
137 if (err != KERN_SUCCESS) {
138 VReport(1, "Error - unable to get thread ident for a thread\n");
141 threads_.push_back({info.thread_id, thread});
144 PtraceRegistersStatus SuspendedThreadsListMac::GetRegistersAndSP(
145 uptr index, uptr *buffer, uptr *sp) const {
146 thread_t thread = GetThread(index);
149 mach_msg_type_number_t reg_count = MACHINE_THREAD_STATE_COUNT;
150 err = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)®s,
152 if (err != KERN_SUCCESS) {
153 VReport(1, "Error - unable to get registers for a thread\n");
154 // KERN_INVALID_ARGUMENT indicates that either the flavor is invalid,
155 // or the thread does not exist. The other possible error case,
156 // MIG_ARRAY_TOO_LARGE, means that the state is too large, but it's
157 // still safe to proceed.
158 return err == KERN_INVALID_ARGUMENT ? REGISTERS_UNAVAILABLE_FATAL
159 : REGISTERS_UNAVAILABLE;
162 internal_memcpy(buffer, ®s, sizeof(regs));
165 // On x86_64 and aarch64, we must account for the stack redzone, which is 128
167 if (SANITIZER_WORDSIZE == 64) *sp -= 128;
169 return REGISTERS_AVAILABLE;
172 uptr SuspendedThreadsListMac::RegisterCount() const {
173 return MACHINE_THREAD_STATE_COUNT;
175 } // namespace __sanitizer
177 #endif // SANITIZER_MAC && (defined(__x86_64__) || defined(__aarch64__)) ||