1061da546Spatrick //===-- MachThreadList.cpp --------------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick //
9061da546Spatrick // Created by Greg Clayton on 6/19/07.
10061da546Spatrick //
11061da546Spatrick //===----------------------------------------------------------------------===//
12061da546Spatrick
13061da546Spatrick #include "MachThreadList.h"
14061da546Spatrick
15061da546Spatrick #include "DNB.h"
16061da546Spatrick #include "DNBLog.h"
17061da546Spatrick #include "DNBThreadResumeActions.h"
18061da546Spatrick #include "MachProcess.h"
19061da546Spatrick
20be691f3bSpatrick #include <cinttypes>
21061da546Spatrick #include <sys/sysctl.h>
22061da546Spatrick
23061da546Spatrick #include <memory>
24061da546Spatrick
MachThreadList()25061da546Spatrick MachThreadList::MachThreadList()
26061da546Spatrick : m_threads(), m_threads_mutex(PTHREAD_MUTEX_RECURSIVE),
27061da546Spatrick m_is_64_bit(false) {}
28061da546Spatrick
29*f6aab3d8Srobert MachThreadList::~MachThreadList() = default;
30061da546Spatrick
GetState(nub_thread_t tid)31061da546Spatrick nub_state_t MachThreadList::GetState(nub_thread_t tid) {
32061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
33061da546Spatrick if (thread_sp)
34061da546Spatrick return thread_sp->GetState();
35061da546Spatrick return eStateInvalid;
36061da546Spatrick }
37061da546Spatrick
GetName(nub_thread_t tid)38061da546Spatrick const char *MachThreadList::GetName(nub_thread_t tid) {
39061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
40061da546Spatrick if (thread_sp)
41061da546Spatrick return thread_sp->GetName();
42061da546Spatrick return NULL;
43061da546Spatrick }
44061da546Spatrick
GetRequestedQoS(nub_thread_t tid,nub_addr_t tsd,uint64_t dti_qos_class_index)45061da546Spatrick ThreadInfo::QoS MachThreadList::GetRequestedQoS(nub_thread_t tid,
46061da546Spatrick nub_addr_t tsd,
47061da546Spatrick uint64_t dti_qos_class_index) {
48061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
49061da546Spatrick if (thread_sp)
50061da546Spatrick return thread_sp->GetRequestedQoS(tsd, dti_qos_class_index);
51061da546Spatrick return ThreadInfo::QoS();
52061da546Spatrick }
53061da546Spatrick
GetPThreadT(nub_thread_t tid)54061da546Spatrick nub_addr_t MachThreadList::GetPThreadT(nub_thread_t tid) {
55061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
56061da546Spatrick if (thread_sp)
57061da546Spatrick return thread_sp->GetPThreadT();
58061da546Spatrick return INVALID_NUB_ADDRESS;
59061da546Spatrick }
60061da546Spatrick
GetDispatchQueueT(nub_thread_t tid)61061da546Spatrick nub_addr_t MachThreadList::GetDispatchQueueT(nub_thread_t tid) {
62061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
63061da546Spatrick if (thread_sp)
64061da546Spatrick return thread_sp->GetDispatchQueueT();
65061da546Spatrick return INVALID_NUB_ADDRESS;
66061da546Spatrick }
67061da546Spatrick
GetTSDAddressForThread(nub_thread_t tid,uint64_t plo_pthread_tsd_base_address_offset,uint64_t plo_pthread_tsd_base_offset,uint64_t plo_pthread_tsd_entry_size)68061da546Spatrick nub_addr_t MachThreadList::GetTSDAddressForThread(
69061da546Spatrick nub_thread_t tid, uint64_t plo_pthread_tsd_base_address_offset,
70061da546Spatrick uint64_t plo_pthread_tsd_base_offset, uint64_t plo_pthread_tsd_entry_size) {
71061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
72061da546Spatrick if (thread_sp)
73061da546Spatrick return thread_sp->GetTSDAddressForThread(
74061da546Spatrick plo_pthread_tsd_base_address_offset, plo_pthread_tsd_base_offset,
75061da546Spatrick plo_pthread_tsd_entry_size);
76061da546Spatrick return INVALID_NUB_ADDRESS;
77061da546Spatrick }
78061da546Spatrick
SetCurrentThread(nub_thread_t tid)79061da546Spatrick nub_thread_t MachThreadList::SetCurrentThread(nub_thread_t tid) {
80061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
81061da546Spatrick if (thread_sp) {
82061da546Spatrick m_current_thread = thread_sp;
83061da546Spatrick return tid;
84061da546Spatrick }
85061da546Spatrick return INVALID_NUB_THREAD;
86061da546Spatrick }
87061da546Spatrick
GetThreadStoppedReason(nub_thread_t tid,struct DNBThreadStopInfo * stop_info) const88061da546Spatrick bool MachThreadList::GetThreadStoppedReason(
89061da546Spatrick nub_thread_t tid, struct DNBThreadStopInfo *stop_info) const {
90061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
91061da546Spatrick if (thread_sp)
92061da546Spatrick return thread_sp->GetStopException().GetStopInfo(stop_info);
93061da546Spatrick return false;
94061da546Spatrick }
95061da546Spatrick
GetIdentifierInfo(nub_thread_t tid,thread_identifier_info_data_t * ident_info)96061da546Spatrick bool MachThreadList::GetIdentifierInfo(
97061da546Spatrick nub_thread_t tid, thread_identifier_info_data_t *ident_info) {
98061da546Spatrick thread_t mach_port_number = GetMachPortNumberByThreadID(tid);
99061da546Spatrick
100061da546Spatrick mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
101061da546Spatrick return ::thread_info(mach_port_number, THREAD_IDENTIFIER_INFO,
102061da546Spatrick (thread_info_t)ident_info, &count) == KERN_SUCCESS;
103061da546Spatrick }
104061da546Spatrick
DumpThreadStoppedReason(nub_thread_t tid) const105061da546Spatrick void MachThreadList::DumpThreadStoppedReason(nub_thread_t tid) const {
106061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
107061da546Spatrick if (thread_sp)
108061da546Spatrick thread_sp->GetStopException().DumpStopReason();
109061da546Spatrick }
110061da546Spatrick
GetThreadInfo(nub_thread_t tid) const111061da546Spatrick const char *MachThreadList::GetThreadInfo(nub_thread_t tid) const {
112061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
113061da546Spatrick if (thread_sp)
114061da546Spatrick return thread_sp->GetBasicInfoAsString();
115061da546Spatrick return NULL;
116061da546Spatrick }
117061da546Spatrick
GetThreadByID(nub_thread_t tid) const118061da546Spatrick MachThreadSP MachThreadList::GetThreadByID(nub_thread_t tid) const {
119061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
120061da546Spatrick MachThreadSP thread_sp;
121061da546Spatrick const size_t num_threads = m_threads.size();
122061da546Spatrick for (size_t idx = 0; idx < num_threads; ++idx) {
123061da546Spatrick if (m_threads[idx]->ThreadID() == tid) {
124061da546Spatrick thread_sp = m_threads[idx];
125061da546Spatrick break;
126061da546Spatrick }
127061da546Spatrick }
128061da546Spatrick return thread_sp;
129061da546Spatrick }
130061da546Spatrick
131061da546Spatrick MachThreadSP
GetThreadByMachPortNumber(thread_t mach_port_number) const132061da546Spatrick MachThreadList::GetThreadByMachPortNumber(thread_t mach_port_number) const {
133061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
134061da546Spatrick MachThreadSP thread_sp;
135061da546Spatrick const size_t num_threads = m_threads.size();
136061da546Spatrick for (size_t idx = 0; idx < num_threads; ++idx) {
137061da546Spatrick if (m_threads[idx]->MachPortNumber() == mach_port_number) {
138061da546Spatrick thread_sp = m_threads[idx];
139061da546Spatrick break;
140061da546Spatrick }
141061da546Spatrick }
142061da546Spatrick return thread_sp;
143061da546Spatrick }
144061da546Spatrick
145061da546Spatrick nub_thread_t
GetThreadIDByMachPortNumber(thread_t mach_port_number) const146061da546Spatrick MachThreadList::GetThreadIDByMachPortNumber(thread_t mach_port_number) const {
147061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
148061da546Spatrick MachThreadSP thread_sp;
149061da546Spatrick const size_t num_threads = m_threads.size();
150061da546Spatrick for (size_t idx = 0; idx < num_threads; ++idx) {
151061da546Spatrick if (m_threads[idx]->MachPortNumber() == mach_port_number) {
152061da546Spatrick return m_threads[idx]->ThreadID();
153061da546Spatrick }
154061da546Spatrick }
155061da546Spatrick return INVALID_NUB_THREAD;
156061da546Spatrick }
157061da546Spatrick
GetMachPortNumberByThreadID(nub_thread_t globally_unique_id) const158061da546Spatrick thread_t MachThreadList::GetMachPortNumberByThreadID(
159061da546Spatrick nub_thread_t globally_unique_id) const {
160061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
161061da546Spatrick MachThreadSP thread_sp;
162061da546Spatrick const size_t num_threads = m_threads.size();
163061da546Spatrick for (size_t idx = 0; idx < num_threads; ++idx) {
164061da546Spatrick if (m_threads[idx]->ThreadID() == globally_unique_id) {
165061da546Spatrick return m_threads[idx]->MachPortNumber();
166061da546Spatrick }
167061da546Spatrick }
168061da546Spatrick return 0;
169061da546Spatrick }
170061da546Spatrick
GetRegisterValue(nub_thread_t tid,uint32_t set,uint32_t reg,DNBRegisterValue * reg_value) const171061da546Spatrick bool MachThreadList::GetRegisterValue(nub_thread_t tid, uint32_t set,
172061da546Spatrick uint32_t reg,
173061da546Spatrick DNBRegisterValue *reg_value) const {
174061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
175061da546Spatrick if (thread_sp)
176061da546Spatrick return thread_sp->GetRegisterValue(set, reg, reg_value);
177061da546Spatrick
178061da546Spatrick return false;
179061da546Spatrick }
180061da546Spatrick
SetRegisterValue(nub_thread_t tid,uint32_t set,uint32_t reg,const DNBRegisterValue * reg_value) const181061da546Spatrick bool MachThreadList::SetRegisterValue(nub_thread_t tid, uint32_t set,
182061da546Spatrick uint32_t reg,
183061da546Spatrick const DNBRegisterValue *reg_value) const {
184061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
185061da546Spatrick if (thread_sp)
186061da546Spatrick return thread_sp->SetRegisterValue(set, reg, reg_value);
187061da546Spatrick
188061da546Spatrick return false;
189061da546Spatrick }
190061da546Spatrick
GetRegisterContext(nub_thread_t tid,void * buf,size_t buf_len)191061da546Spatrick nub_size_t MachThreadList::GetRegisterContext(nub_thread_t tid, void *buf,
192061da546Spatrick size_t buf_len) {
193061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
194061da546Spatrick if (thread_sp)
195061da546Spatrick return thread_sp->GetRegisterContext(buf, buf_len);
196061da546Spatrick return 0;
197061da546Spatrick }
198061da546Spatrick
SetRegisterContext(nub_thread_t tid,const void * buf,size_t buf_len)199061da546Spatrick nub_size_t MachThreadList::SetRegisterContext(nub_thread_t tid, const void *buf,
200061da546Spatrick size_t buf_len) {
201061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
202061da546Spatrick if (thread_sp)
203061da546Spatrick return thread_sp->SetRegisterContext(buf, buf_len);
204061da546Spatrick return 0;
205061da546Spatrick }
206061da546Spatrick
SaveRegisterState(nub_thread_t tid)207061da546Spatrick uint32_t MachThreadList::SaveRegisterState(nub_thread_t tid) {
208061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
209061da546Spatrick if (thread_sp)
210061da546Spatrick return thread_sp->SaveRegisterState();
211061da546Spatrick return 0;
212061da546Spatrick }
213061da546Spatrick
RestoreRegisterState(nub_thread_t tid,uint32_t save_id)214061da546Spatrick bool MachThreadList::RestoreRegisterState(nub_thread_t tid, uint32_t save_id) {
215061da546Spatrick MachThreadSP thread_sp(GetThreadByID(tid));
216061da546Spatrick if (thread_sp)
217061da546Spatrick return thread_sp->RestoreRegisterState(save_id);
218061da546Spatrick return false;
219061da546Spatrick }
220061da546Spatrick
NumThreads() const221061da546Spatrick nub_size_t MachThreadList::NumThreads() const {
222061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
223061da546Spatrick return m_threads.size();
224061da546Spatrick }
225061da546Spatrick
ThreadIDAtIndex(nub_size_t idx) const226061da546Spatrick nub_thread_t MachThreadList::ThreadIDAtIndex(nub_size_t idx) const {
227061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
228061da546Spatrick if (idx < m_threads.size())
229061da546Spatrick return m_threads[idx]->ThreadID();
230061da546Spatrick return INVALID_NUB_THREAD;
231061da546Spatrick }
232061da546Spatrick
CurrentThreadID()233061da546Spatrick nub_thread_t MachThreadList::CurrentThreadID() {
234061da546Spatrick MachThreadSP thread_sp;
235061da546Spatrick CurrentThread(thread_sp);
236061da546Spatrick if (thread_sp.get())
237061da546Spatrick return thread_sp->ThreadID();
238061da546Spatrick return INVALID_NUB_THREAD;
239061da546Spatrick }
240061da546Spatrick
NotifyException(MachException::Data & exc)241061da546Spatrick bool MachThreadList::NotifyException(MachException::Data &exc) {
242061da546Spatrick MachThreadSP thread_sp(GetThreadByMachPortNumber(exc.thread_port));
243061da546Spatrick if (thread_sp) {
244061da546Spatrick thread_sp->NotifyException(exc);
245061da546Spatrick return true;
246061da546Spatrick }
247061da546Spatrick return false;
248061da546Spatrick }
249061da546Spatrick
Clear()250061da546Spatrick void MachThreadList::Clear() {
251061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
252061da546Spatrick m_threads.clear();
253061da546Spatrick }
254061da546Spatrick
255061da546Spatrick uint32_t
UpdateThreadList(MachProcess * process,bool update,MachThreadList::collection * new_threads)256061da546Spatrick MachThreadList::UpdateThreadList(MachProcess *process, bool update,
257061da546Spatrick MachThreadList::collection *new_threads) {
258061da546Spatrick // locker will keep a mutex locked until it goes out of scope
259061da546Spatrick DNBLogThreadedIf(LOG_THREAD, "MachThreadList::UpdateThreadList (pid = %4.4x, "
260061da546Spatrick "update = %u) process stop count = %u",
261061da546Spatrick process->ProcessID(), update, process->StopCount());
262061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
263061da546Spatrick
264061da546Spatrick if (process->StopCount() == 0) {
265061da546Spatrick int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, process->ProcessID()};
266061da546Spatrick struct kinfo_proc processInfo;
267061da546Spatrick size_t bufsize = sizeof(processInfo);
268061da546Spatrick if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo,
269061da546Spatrick &bufsize, NULL, 0) == 0 &&
270061da546Spatrick bufsize > 0) {
271061da546Spatrick if (processInfo.kp_proc.p_flag & P_LP64)
272061da546Spatrick m_is_64_bit = true;
273061da546Spatrick }
274061da546Spatrick #if defined(__i386__) || defined(__x86_64__)
275061da546Spatrick if (m_is_64_bit)
276061da546Spatrick DNBArchProtocol::SetArchitecture(CPU_TYPE_X86_64);
277061da546Spatrick else
278061da546Spatrick DNBArchProtocol::SetArchitecture(CPU_TYPE_I386);
279061da546Spatrick #elif defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
280061da546Spatrick if (m_is_64_bit)
281061da546Spatrick DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64);
282061da546Spatrick else {
283061da546Spatrick if (process->GetCPUType() == CPU_TYPE_ARM64_32)
284061da546Spatrick DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM64_32);
285061da546Spatrick else
286061da546Spatrick DNBArchProtocol::SetArchitecture(CPU_TYPE_ARM);
287061da546Spatrick }
288061da546Spatrick #endif
289061da546Spatrick }
290061da546Spatrick
291061da546Spatrick if (m_threads.empty() || update) {
292061da546Spatrick thread_array_t thread_list = NULL;
293061da546Spatrick mach_msg_type_number_t thread_list_count = 0;
294061da546Spatrick task_t task = process->Task().TaskPort();
295061da546Spatrick DNBError err(::task_threads(task, &thread_list, &thread_list_count),
296061da546Spatrick DNBError::MachKernel);
297061da546Spatrick
298061da546Spatrick if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail())
299061da546Spatrick err.LogThreaded("::task_threads ( task = 0x%4.4x, thread_list => %p, "
300061da546Spatrick "thread_list_count => %u )",
301061da546Spatrick task, thread_list, thread_list_count);
302061da546Spatrick
303061da546Spatrick if (err.Status() == KERN_SUCCESS && thread_list_count > 0) {
304061da546Spatrick MachThreadList::collection currThreads;
305061da546Spatrick size_t idx;
306061da546Spatrick // Iterator through the current thread list and see which threads
307061da546Spatrick // we already have in our list (keep them), which ones we don't
308061da546Spatrick // (add them), and which ones are not around anymore (remove them).
309061da546Spatrick for (idx = 0; idx < thread_list_count; ++idx) {
310061da546Spatrick const thread_t mach_port_num = thread_list[idx];
311061da546Spatrick
312061da546Spatrick uint64_t unique_thread_id =
313061da546Spatrick MachThread::GetGloballyUniqueThreadIDForMachPortID(mach_port_num);
314061da546Spatrick MachThreadSP thread_sp(GetThreadByID(unique_thread_id));
315061da546Spatrick if (thread_sp) {
316061da546Spatrick // Keep the existing thread class
317061da546Spatrick currThreads.push_back(thread_sp);
318061da546Spatrick } else {
319061da546Spatrick // We don't have this thread, lets add it.
320061da546Spatrick thread_sp = std::make_shared<MachThread>(
321061da546Spatrick process, m_is_64_bit, unique_thread_id, mach_port_num);
322061da546Spatrick
323061da546Spatrick // Add the new thread regardless of its is user ready state...
324061da546Spatrick // Make sure the thread is ready to be displayed and shown to users
325061da546Spatrick // before we add this thread to our list...
326061da546Spatrick if (thread_sp->IsUserReady()) {
327061da546Spatrick if (new_threads)
328061da546Spatrick new_threads->push_back(thread_sp);
329061da546Spatrick
330061da546Spatrick currThreads.push_back(thread_sp);
331061da546Spatrick }
332061da546Spatrick }
333061da546Spatrick }
334061da546Spatrick
335061da546Spatrick m_threads.swap(currThreads);
336061da546Spatrick m_current_thread.reset();
337061da546Spatrick
338061da546Spatrick // Free the vm memory given to us by ::task_threads()
339061da546Spatrick vm_size_t thread_list_size =
340061da546Spatrick (vm_size_t)(thread_list_count * sizeof(thread_t));
341061da546Spatrick ::vm_deallocate(::mach_task_self(), (vm_address_t)thread_list,
342061da546Spatrick thread_list_size);
343061da546Spatrick }
344061da546Spatrick }
345061da546Spatrick return static_cast<uint32_t>(m_threads.size());
346061da546Spatrick }
347061da546Spatrick
CurrentThread(MachThreadSP & thread_sp)348061da546Spatrick void MachThreadList::CurrentThread(MachThreadSP &thread_sp) {
349061da546Spatrick // locker will keep a mutex locked until it goes out of scope
350061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
351061da546Spatrick if (m_current_thread.get() == NULL) {
352061da546Spatrick // Figure out which thread is going to be our current thread.
353061da546Spatrick // This is currently done by finding the first thread in the list
354061da546Spatrick // that has a valid exception.
355061da546Spatrick const size_t num_threads = m_threads.size();
356061da546Spatrick for (uint32_t idx = 0; idx < num_threads; ++idx) {
357061da546Spatrick if (m_threads[idx]->GetStopException().IsValid()) {
358061da546Spatrick m_current_thread = m_threads[idx];
359061da546Spatrick break;
360061da546Spatrick }
361061da546Spatrick }
362061da546Spatrick }
363061da546Spatrick thread_sp = m_current_thread;
364061da546Spatrick }
365061da546Spatrick
Dump() const366061da546Spatrick void MachThreadList::Dump() const {
367061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
368061da546Spatrick const size_t num_threads = m_threads.size();
369061da546Spatrick for (uint32_t idx = 0; idx < num_threads; ++idx) {
370061da546Spatrick m_threads[idx]->Dump(idx);
371061da546Spatrick }
372061da546Spatrick }
373061da546Spatrick
ProcessWillResume(MachProcess * process,const DNBThreadResumeActions & thread_actions)374061da546Spatrick void MachThreadList::ProcessWillResume(
375061da546Spatrick MachProcess *process, const DNBThreadResumeActions &thread_actions) {
376061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
377061da546Spatrick
378061da546Spatrick // Update our thread list, because sometimes libdispatch or the kernel
379061da546Spatrick // will spawn threads while a task is suspended.
380061da546Spatrick MachThreadList::collection new_threads;
381061da546Spatrick
382061da546Spatrick // First figure out if we were planning on running only one thread, and if so
383061da546Spatrick // force that thread to resume.
384061da546Spatrick bool run_one_thread;
385061da546Spatrick nub_thread_t solo_thread = INVALID_NUB_THREAD;
386061da546Spatrick if (thread_actions.GetSize() > 0 &&
387061da546Spatrick thread_actions.NumActionsWithState(eStateStepping) +
388061da546Spatrick thread_actions.NumActionsWithState(eStateRunning) ==
389061da546Spatrick 1) {
390061da546Spatrick run_one_thread = true;
391061da546Spatrick const DNBThreadResumeAction *action_ptr = thread_actions.GetFirst();
392061da546Spatrick size_t num_actions = thread_actions.GetSize();
393061da546Spatrick for (size_t i = 0; i < num_actions; i++, action_ptr++) {
394061da546Spatrick if (action_ptr->state == eStateStepping ||
395061da546Spatrick action_ptr->state == eStateRunning) {
396061da546Spatrick solo_thread = action_ptr->tid;
397061da546Spatrick break;
398061da546Spatrick }
399061da546Spatrick }
400061da546Spatrick } else
401061da546Spatrick run_one_thread = false;
402061da546Spatrick
403061da546Spatrick UpdateThreadList(process, true, &new_threads);
404061da546Spatrick
405061da546Spatrick DNBThreadResumeAction resume_new_threads = {-1U, eStateRunning, 0,
406061da546Spatrick INVALID_NUB_ADDRESS};
407061da546Spatrick // If we are planning to run only one thread, any new threads should be
408061da546Spatrick // suspended.
409061da546Spatrick if (run_one_thread)
410061da546Spatrick resume_new_threads.state = eStateSuspended;
411061da546Spatrick
412061da546Spatrick const size_t num_new_threads = new_threads.size();
413061da546Spatrick const size_t num_threads = m_threads.size();
414061da546Spatrick for (uint32_t idx = 0; idx < num_threads; ++idx) {
415061da546Spatrick MachThread *thread = m_threads[idx].get();
416061da546Spatrick bool handled = false;
417061da546Spatrick for (uint32_t new_idx = 0; new_idx < num_new_threads; ++new_idx) {
418061da546Spatrick if (thread == new_threads[new_idx].get()) {
419061da546Spatrick thread->ThreadWillResume(&resume_new_threads);
420061da546Spatrick handled = true;
421061da546Spatrick break;
422061da546Spatrick }
423061da546Spatrick }
424061da546Spatrick
425061da546Spatrick if (!handled) {
426061da546Spatrick const DNBThreadResumeAction *thread_action =
427061da546Spatrick thread_actions.GetActionForThread(thread->ThreadID(), true);
428061da546Spatrick // There must always be a thread action for every thread.
429061da546Spatrick assert(thread_action);
430061da546Spatrick bool others_stopped = false;
431061da546Spatrick if (solo_thread == thread->ThreadID())
432061da546Spatrick others_stopped = true;
433061da546Spatrick thread->ThreadWillResume(thread_action, others_stopped);
434061da546Spatrick }
435061da546Spatrick }
436061da546Spatrick
437061da546Spatrick if (new_threads.size()) {
438061da546Spatrick for (uint32_t idx = 0; idx < num_new_threads; ++idx) {
439061da546Spatrick DNBLogThreadedIf(
440061da546Spatrick LOG_THREAD, "MachThreadList::ProcessWillResume (pid = %4.4x) "
441061da546Spatrick "stop-id=%u, resuming newly discovered thread: "
442061da546Spatrick "0x%8.8" PRIx64 ", thread-is-user-ready=%i)",
443061da546Spatrick process->ProcessID(), process->StopCount(),
444061da546Spatrick new_threads[idx]->ThreadID(), new_threads[idx]->IsUserReady());
445061da546Spatrick }
446061da546Spatrick }
447061da546Spatrick }
448061da546Spatrick
ProcessDidStop(MachProcess * process)449061da546Spatrick uint32_t MachThreadList::ProcessDidStop(MachProcess *process) {
450061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
451061da546Spatrick // Update our thread list
452061da546Spatrick const uint32_t num_threads = UpdateThreadList(process, true);
453061da546Spatrick for (uint32_t idx = 0; idx < num_threads; ++idx) {
454061da546Spatrick m_threads[idx]->ThreadDidStop();
455061da546Spatrick }
456061da546Spatrick return num_threads;
457061da546Spatrick }
458061da546Spatrick
459061da546Spatrick // Check each thread in our thread list to see if we should notify our
460061da546Spatrick // client of the current halt in execution.
461061da546Spatrick //
462061da546Spatrick // Breakpoints can have callback functions associated with them than
463061da546Spatrick // can return true to stop, or false to continue executing the inferior.
464061da546Spatrick //
465061da546Spatrick // RETURNS
466061da546Spatrick // true if we should stop and notify our clients
467061da546Spatrick // false if we should resume our child process and skip notification
ShouldStop(bool & step_more)468061da546Spatrick bool MachThreadList::ShouldStop(bool &step_more) {
469061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
470061da546Spatrick uint32_t should_stop = false;
471061da546Spatrick const size_t num_threads = m_threads.size();
472061da546Spatrick for (uint32_t idx = 0; !should_stop && idx < num_threads; ++idx) {
473061da546Spatrick should_stop = m_threads[idx]->ShouldStop(step_more);
474061da546Spatrick }
475061da546Spatrick return should_stop;
476061da546Spatrick }
477061da546Spatrick
NotifyBreakpointChanged(const DNBBreakpoint * bp)478061da546Spatrick void MachThreadList::NotifyBreakpointChanged(const DNBBreakpoint *bp) {
479061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
480061da546Spatrick const size_t num_threads = m_threads.size();
481061da546Spatrick for (uint32_t idx = 0; idx < num_threads; ++idx) {
482061da546Spatrick m_threads[idx]->NotifyBreakpointChanged(bp);
483061da546Spatrick }
484061da546Spatrick }
485061da546Spatrick
DoHardwareBreakpointAction(const DNBBreakpoint * bp,HardwareBreakpointAction action) const486dda28197Spatrick uint32_t MachThreadList::DoHardwareBreakpointAction(
487dda28197Spatrick const DNBBreakpoint *bp, HardwareBreakpointAction action) const {
488dda28197Spatrick if (bp == NULL)
489dda28197Spatrick return INVALID_NUB_HW_INDEX;
490dda28197Spatrick
491dda28197Spatrick uint32_t hw_index = INVALID_NUB_HW_INDEX;
492dda28197Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
493061da546Spatrick const size_t num_threads = m_threads.size();
494dda28197Spatrick // On Mac OS X we have to prime the control registers for new threads. We do
495dda28197Spatrick // this using the control register data for the first thread, for lack of a
496dda28197Spatrick // better way of choosing.
497dda28197Spatrick bool also_set_on_task = true;
498dda28197Spatrick for (uint32_t idx = 0; idx < num_threads; ++idx) {
499dda28197Spatrick switch (action) {
500dda28197Spatrick case HardwareBreakpointAction::EnableWatchpoint:
501dda28197Spatrick hw_index = m_threads[idx]->EnableHardwareWatchpoint(bp, also_set_on_task);
502dda28197Spatrick break;
503dda28197Spatrick case HardwareBreakpointAction::DisableWatchpoint:
504dda28197Spatrick hw_index =
505dda28197Spatrick m_threads[idx]->DisableHardwareWatchpoint(bp, also_set_on_task);
506dda28197Spatrick break;
507dda28197Spatrick case HardwareBreakpointAction::EnableBreakpoint:
508dda28197Spatrick hw_index = m_threads[idx]->EnableHardwareBreakpoint(bp, also_set_on_task);
509dda28197Spatrick break;
510dda28197Spatrick case HardwareBreakpointAction::DisableBreakpoint:
511dda28197Spatrick hw_index =
512dda28197Spatrick m_threads[idx]->DisableHardwareBreakpoint(bp, also_set_on_task);
513dda28197Spatrick break;
514061da546Spatrick }
515dda28197Spatrick if (hw_index == INVALID_NUB_HW_INDEX) {
516dda28197Spatrick // We know that idx failed for some reason. Let's rollback the
517dda28197Spatrick // transaction for [0, idx).
518dda28197Spatrick for (uint32_t i = 0; i < idx; ++i)
519dda28197Spatrick m_threads[i]->RollbackTransForHWP();
520061da546Spatrick return INVALID_NUB_HW_INDEX;
521061da546Spatrick }
522dda28197Spatrick also_set_on_task = false;
523061da546Spatrick }
524dda28197Spatrick // Notify each thread to commit the pending transaction.
525dda28197Spatrick for (uint32_t idx = 0; idx < num_threads; ++idx)
526dda28197Spatrick m_threads[idx]->FinishTransForHWP();
527dda28197Spatrick return hw_index;
528061da546Spatrick }
529061da546Spatrick
530061da546Spatrick // DNBWatchpointSet() -> MachProcess::CreateWatchpoint() ->
531061da546Spatrick // MachProcess::EnableWatchpoint()
532061da546Spatrick // -> MachThreadList::EnableHardwareWatchpoint().
533061da546Spatrick uint32_t
EnableHardwareWatchpoint(const DNBBreakpoint * wp) const534061da546Spatrick MachThreadList::EnableHardwareWatchpoint(const DNBBreakpoint *wp) const {
535dda28197Spatrick return DoHardwareBreakpointAction(wp,
536dda28197Spatrick HardwareBreakpointAction::EnableWatchpoint);
537061da546Spatrick }
538061da546Spatrick
DisableHardwareWatchpoint(const DNBBreakpoint * wp) const539061da546Spatrick bool MachThreadList::DisableHardwareWatchpoint(const DNBBreakpoint *wp) const {
540dda28197Spatrick return DoHardwareBreakpointAction(
541dda28197Spatrick wp, HardwareBreakpointAction::DisableWatchpoint) !=
542dda28197Spatrick INVALID_NUB_HW_INDEX;
543dda28197Spatrick }
544061da546Spatrick
545dda28197Spatrick uint32_t
EnableHardwareBreakpoint(const DNBBreakpoint * bp) const546dda28197Spatrick MachThreadList::EnableHardwareBreakpoint(const DNBBreakpoint *bp) const {
547dda28197Spatrick return DoHardwareBreakpointAction(bp,
548dda28197Spatrick HardwareBreakpointAction::EnableBreakpoint);
549061da546Spatrick }
550061da546Spatrick
DisableHardwareBreakpoint(const DNBBreakpoint * bp) const551dda28197Spatrick bool MachThreadList::DisableHardwareBreakpoint(const DNBBreakpoint *bp) const {
552dda28197Spatrick return DoHardwareBreakpointAction(
553dda28197Spatrick bp, HardwareBreakpointAction::DisableBreakpoint) !=
554dda28197Spatrick INVALID_NUB_HW_INDEX;
555061da546Spatrick }
556061da546Spatrick
NumSupportedHardwareWatchpoints() const557061da546Spatrick uint32_t MachThreadList::NumSupportedHardwareWatchpoints() const {
558061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
559061da546Spatrick const size_t num_threads = m_threads.size();
560061da546Spatrick // Use an arbitrary thread to retrieve the number of supported hardware
561061da546Spatrick // watchpoints.
562061da546Spatrick if (num_threads)
563061da546Spatrick return m_threads[0]->NumSupportedHardwareWatchpoints();
564061da546Spatrick return 0;
565061da546Spatrick }
566061da546Spatrick
GetThreadIndexForThreadStoppedWithSignal(const int signo) const567061da546Spatrick uint32_t MachThreadList::GetThreadIndexForThreadStoppedWithSignal(
568061da546Spatrick const int signo) const {
569061da546Spatrick PTHREAD_MUTEX_LOCKER(locker, m_threads_mutex);
570061da546Spatrick uint32_t should_stop = false;
571061da546Spatrick const size_t num_threads = m_threads.size();
572061da546Spatrick for (uint32_t idx = 0; !should_stop && idx < num_threads; ++idx) {
573061da546Spatrick if (m_threads[idx]->GetStopException().SoftSignal() == signo)
574061da546Spatrick return idx;
575061da546Spatrick }
576061da546Spatrick return UINT32_MAX;
577061da546Spatrick }
578