1 //===-- MachThread.cpp ------------------------------------------*- C++ -*-===// 2 // 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 // Created by Greg Clayton on 6/19/07. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MachThread.h" 15 #include "DNB.h" 16 #include "DNBLog.h" 17 #include "MachProcess.h" 18 #include "ThreadInfo.h" 19 #include <dlfcn.h> 20 #include <inttypes.h> 21 #include <mach/thread_policy.h> 22 23 static uint32_t GetSequenceID() { 24 static uint32_t g_nextID = 0; 25 return ++g_nextID; 26 } 27 28 MachThread::MachThread(MachProcess *process, bool is_64_bit, 29 uint64_t unique_thread_id, thread_t mach_port_num) 30 : m_process(process), m_unique_id(unique_thread_id), 31 m_mach_port_number(mach_port_num), m_seq_id(GetSequenceID()), 32 m_state(eStateUnloaded), m_state_mutex(PTHREAD_MUTEX_RECURSIVE), 33 m_suspend_count(0), m_stop_exception(), 34 m_arch_ap(DNBArchProtocol::Create(this)), m_reg_sets(NULL), 35 m_num_reg_sets(0), m_ident_info(), m_proc_threadinfo(), 36 m_dispatch_queue_name(), m_is_64_bit(is_64_bit), 37 m_pthread_qos_class_decode(nullptr) { 38 nub_size_t num_reg_sets = 0; 39 m_reg_sets = m_arch_ap->GetRegisterSetInfo(&num_reg_sets); 40 m_num_reg_sets = num_reg_sets; 41 42 m_pthread_qos_class_decode = 43 (unsigned int (*)(unsigned long, int *, unsigned long *))dlsym( 44 RTLD_DEFAULT, "_pthread_qos_class_decode"); 45 46 // Get the thread state so we know if a thread is in a state where we can't 47 // muck with it and also so we get the suspend count correct in case it was 48 // already suspended 49 GetBasicInfo(); 50 DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, 51 "MachThread::MachThread ( process = %p, tid = 0x%8.8" PRIx64 52 ", seq_id = %u )", 53 &m_process, m_unique_id, m_seq_id); 54 } 55 56 MachThread::~MachThread() { 57 DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, 58 "MachThread::~MachThread() for tid = 0x%8.8" PRIx64 " (%u)", 59 m_unique_id, m_seq_id); 60 } 61 62 void MachThread::Suspend() { 63 DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", 64 __FUNCTION__); 65 if (MachPortNumberIsValid(m_mach_port_number)) { 66 DNBError err(::thread_suspend(m_mach_port_number), DNBError::MachKernel); 67 if (err.Success()) 68 m_suspend_count++; 69 if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail()) 70 err.LogThreaded("::thread_suspend (%4.4" PRIx32 ")", m_mach_port_number); 71 } 72 } 73 74 void MachThread::Resume(bool others_stopped) { 75 DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", 76 __FUNCTION__); 77 if (MachPortNumberIsValid(m_mach_port_number)) { 78 SetSuspendCountBeforeResume(others_stopped); 79 } 80 } 81 82 bool MachThread::SetSuspendCountBeforeResume(bool others_stopped) { 83 DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", 84 __FUNCTION__); 85 DNBError err; 86 if (MachPortNumberIsValid(m_mach_port_number) == false) 87 return false; 88 89 integer_t times_to_resume; 90 91 if (others_stopped) { 92 if (GetBasicInfo()) { 93 times_to_resume = m_basic_info.suspend_count; 94 m_suspend_count = -(times_to_resume - m_suspend_count); 95 } else 96 times_to_resume = 0; 97 } else { 98 times_to_resume = m_suspend_count; 99 m_suspend_count = 0; 100 } 101 102 if (times_to_resume > 0) { 103 while (times_to_resume > 0) { 104 err = ::thread_resume(m_mach_port_number); 105 if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail()) 106 err.LogThreaded("::thread_resume (%4.4" PRIx32 ")", m_mach_port_number); 107 if (err.Success()) 108 --times_to_resume; 109 else { 110 if (GetBasicInfo()) 111 times_to_resume = m_basic_info.suspend_count; 112 else 113 times_to_resume = 0; 114 } 115 } 116 } 117 return true; 118 } 119 120 bool MachThread::RestoreSuspendCountAfterStop() { 121 DNBLogThreadedIf(LOG_THREAD | LOG_VERBOSE, "MachThread::%s ( )", 122 __FUNCTION__); 123 DNBError err; 124 if (MachPortNumberIsValid(m_mach_port_number) == false) 125 return false; 126 127 if (m_suspend_count > 0) { 128 while (m_suspend_count > 0) { 129 err = ::thread_resume(m_mach_port_number); 130 if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail()) 131 err.LogThreaded("::thread_resume (%4.4" PRIx32 ")", m_mach_port_number); 132 if (err.Success()) 133 --m_suspend_count; 134 else { 135 if (GetBasicInfo()) 136 m_suspend_count = m_basic_info.suspend_count; 137 else 138 m_suspend_count = 0; 139 return false; // ??? 140 } 141 } 142 } else if (m_suspend_count < 0) { 143 while (m_suspend_count < 0) { 144 err = ::thread_suspend(m_mach_port_number); 145 if (err.Success()) 146 ++m_suspend_count; 147 if (DNBLogCheckLogBit(LOG_THREAD) || err.Fail()) { 148 err.LogThreaded("::thread_suspend (%4.4" PRIx32 ")", 149 m_mach_port_number); 150 return false; 151 } 152 } 153 } 154 return true; 155 } 156 157 const char *MachThread::GetBasicInfoAsString() const { 158 static char g_basic_info_string[1024]; 159 struct thread_basic_info basicInfo; 160 161 if (GetBasicInfo(m_mach_port_number, &basicInfo)) { 162 163 // char run_state_str[32]; 164 // size_t run_state_str_size = sizeof(run_state_str); 165 // switch (basicInfo.run_state) 166 // { 167 // case TH_STATE_RUNNING: strncpy(run_state_str, "running", 168 // run_state_str_size); break; 169 // case TH_STATE_STOPPED: strncpy(run_state_str, "stopped", 170 // run_state_str_size); break; 171 // case TH_STATE_WAITING: strncpy(run_state_str, "waiting", 172 // run_state_str_size); break; 173 // case TH_STATE_UNINTERRUPTIBLE: strncpy(run_state_str, 174 // "uninterruptible", run_state_str_size); break; 175 // case TH_STATE_HALTED: strncpy(run_state_str, "halted", 176 // run_state_str_size); break; 177 // default: snprintf(run_state_str, 178 // run_state_str_size, "%d", basicInfo.run_state); break; // ??? 179 // } 180 float user = (float)basicInfo.user_time.seconds + 181 (float)basicInfo.user_time.microseconds / 1000000.0f; 182 float system = (float)basicInfo.user_time.seconds + 183 (float)basicInfo.user_time.microseconds / 1000000.0f; 184 snprintf(g_basic_info_string, sizeof(g_basic_info_string), 185 "Thread 0x%8.8" PRIx64 ": user=%f system=%f cpu=%d sleep_time=%d", 186 m_unique_id, user, system, basicInfo.cpu_usage, 187 basicInfo.sleep_time); 188 189 return g_basic_info_string; 190 } 191 return NULL; 192 } 193 194 // Finds the Mach port number for a given thread in the inferior process' port 195 // namespace. 196 thread_t MachThread::InferiorThreadID() const { 197 mach_msg_type_number_t i; 198 mach_port_name_array_t names; 199 mach_port_type_array_t types; 200 mach_msg_type_number_t ncount, tcount; 201 thread_t inferior_tid = INVALID_NUB_THREAD; 202 task_t my_task = ::mach_task_self(); 203 task_t task = m_process->Task().TaskPort(); 204 205 kern_return_t kret = 206 ::mach_port_names(task, &names, &ncount, &types, &tcount); 207 if (kret == KERN_SUCCESS) { 208 209 for (i = 0; i < ncount; i++) { 210 mach_port_t my_name; 211 mach_msg_type_name_t my_type; 212 213 kret = ::mach_port_extract_right(task, names[i], MACH_MSG_TYPE_COPY_SEND, 214 &my_name, &my_type); 215 if (kret == KERN_SUCCESS) { 216 ::mach_port_deallocate(my_task, my_name); 217 if (my_name == m_mach_port_number) { 218 inferior_tid = names[i]; 219 break; 220 } 221 } 222 } 223 // Free up the names and types 224 ::vm_deallocate(my_task, (vm_address_t)names, 225 ncount * sizeof(mach_port_name_t)); 226 ::vm_deallocate(my_task, (vm_address_t)types, 227 tcount * sizeof(mach_port_type_t)); 228 } 229 return inferior_tid; 230 } 231 232 bool MachThread::IsUserReady() { 233 if (m_basic_info.run_state == 0) 234 GetBasicInfo(); 235 236 switch (m_basic_info.run_state) { 237 default: 238 case TH_STATE_UNINTERRUPTIBLE: 239 break; 240 241 case TH_STATE_RUNNING: 242 case TH_STATE_STOPPED: 243 case TH_STATE_WAITING: 244 case TH_STATE_HALTED: 245 return true; 246 } 247 return false; 248 } 249 250 struct thread_basic_info *MachThread::GetBasicInfo() { 251 if (MachThread::GetBasicInfo(m_mach_port_number, &m_basic_info)) 252 return &m_basic_info; 253 return NULL; 254 } 255 256 bool MachThread::GetBasicInfo(thread_t thread, 257 struct thread_basic_info *basicInfoPtr) { 258 if (MachPortNumberIsValid(thread)) { 259 unsigned int info_count = THREAD_BASIC_INFO_COUNT; 260 kern_return_t err = ::thread_info(thread, THREAD_BASIC_INFO, 261 (thread_info_t)basicInfoPtr, &info_count); 262 if (err == KERN_SUCCESS) 263 return true; 264 } 265 ::memset(basicInfoPtr, 0, sizeof(struct thread_basic_info)); 266 return false; 267 } 268 269 bool MachThread::ThreadIDIsValid(uint64_t thread) { return thread != 0; } 270 271 bool MachThread::MachPortNumberIsValid(thread_t thread) { 272 return thread != THREAD_NULL; 273 } 274 275 bool MachThread::GetRegisterState(int flavor, bool force) { 276 return m_arch_ap->GetRegisterState(flavor, force) == KERN_SUCCESS; 277 } 278 279 bool MachThread::SetRegisterState(int flavor) { 280 return m_arch_ap->SetRegisterState(flavor) == KERN_SUCCESS; 281 } 282 283 uint64_t MachThread::GetPC(uint64_t failValue) { 284 // Get program counter 285 return m_arch_ap->GetPC(failValue); 286 } 287 288 bool MachThread::SetPC(uint64_t value) { 289 // Set program counter 290 return m_arch_ap->SetPC(value); 291 } 292 293 uint64_t MachThread::GetSP(uint64_t failValue) { 294 // Get stack pointer 295 return m_arch_ap->GetSP(failValue); 296 } 297 298 nub_process_t MachThread::ProcessID() const { 299 if (m_process) 300 return m_process->ProcessID(); 301 return INVALID_NUB_PROCESS; 302 } 303 304 void MachThread::Dump(uint32_t index) { 305 const char *thread_run_state = NULL; 306 307 switch (m_basic_info.run_state) { 308 case TH_STATE_RUNNING: 309 thread_run_state = "running"; 310 break; // 1 thread is running normally 311 case TH_STATE_STOPPED: 312 thread_run_state = "stopped"; 313 break; // 2 thread is stopped 314 case TH_STATE_WAITING: 315 thread_run_state = "waiting"; 316 break; // 3 thread is waiting normally 317 case TH_STATE_UNINTERRUPTIBLE: 318 thread_run_state = "uninter"; 319 break; // 4 thread is in an uninterruptible wait 320 case TH_STATE_HALTED: 321 thread_run_state = "halted "; 322 break; // 5 thread is halted at a 323 default: 324 thread_run_state = "???"; 325 break; 326 } 327 328 DNBLogThreaded( 329 "[%3u] #%3u tid: 0x%8.8" PRIx64 ", pc: 0x%16.16" PRIx64 330 ", sp: 0x%16.16" PRIx64 331 ", user: %d.%6.6d, system: %d.%6.6d, cpu: %2d, policy: %2d, run_state: " 332 "%2d (%s), flags: %2d, suspend_count: %2d (current %2d), sleep_time: %d", 333 index, m_seq_id, m_unique_id, GetPC(INVALID_NUB_ADDRESS), 334 GetSP(INVALID_NUB_ADDRESS), m_basic_info.user_time.seconds, 335 m_basic_info.user_time.microseconds, m_basic_info.system_time.seconds, 336 m_basic_info.system_time.microseconds, m_basic_info.cpu_usage, 337 m_basic_info.policy, m_basic_info.run_state, thread_run_state, 338 m_basic_info.flags, m_basic_info.suspend_count, m_suspend_count, 339 m_basic_info.sleep_time); 340 // DumpRegisterState(0); 341 } 342 343 void MachThread::ThreadWillResume(const DNBThreadResumeAction *thread_action, 344 bool others_stopped) { 345 if (thread_action->addr != INVALID_NUB_ADDRESS) 346 SetPC(thread_action->addr); 347 348 SetState(thread_action->state); 349 switch (thread_action->state) { 350 case eStateStopped: 351 case eStateSuspended: 352 assert(others_stopped == false); 353 Suspend(); 354 break; 355 356 case eStateRunning: 357 case eStateStepping: 358 Resume(others_stopped); 359 break; 360 default: 361 break; 362 } 363 m_arch_ap->ThreadWillResume(); 364 m_stop_exception.Clear(); 365 } 366 367 DNBBreakpoint *MachThread::CurrentBreakpoint() { 368 return m_process->Breakpoints().FindByAddress(GetPC()); 369 } 370 371 bool MachThread::ShouldStop(bool &step_more) { 372 // See if this thread is at a breakpoint? 373 DNBBreakpoint *bp = CurrentBreakpoint(); 374 375 if (bp) { 376 // This thread is sitting at a breakpoint, ask the breakpoint 377 // if we should be stopping here. 378 return true; 379 } else { 380 if (m_arch_ap->StepNotComplete()) { 381 step_more = true; 382 return false; 383 } 384 // The thread state is used to let us know what the thread was 385 // trying to do. MachThread::ThreadWillResume() will set the 386 // thread state to various values depending if the thread was 387 // the current thread and if it was to be single stepped, or 388 // resumed. 389 if (GetState() == eStateRunning) { 390 // If our state is running, then we should continue as we are in 391 // the process of stepping over a breakpoint. 392 return false; 393 } else { 394 // Stop if we have any kind of valid exception for this 395 // thread. 396 if (GetStopException().IsValid()) 397 return true; 398 } 399 } 400 return false; 401 } 402 bool MachThread::IsStepping() { return GetState() == eStateStepping; } 403 404 bool MachThread::ThreadDidStop() { 405 // This thread has existed prior to resuming under debug nub control, 406 // and has just been stopped. Do any cleanup that needs to be done 407 // after running. 408 409 // The thread state and breakpoint will still have the same values 410 // as they had prior to resuming the thread, so it makes it easy to check 411 // if we were trying to step a thread, or we tried to resume while being 412 // at a breakpoint. 413 414 // When this method gets called, the process state is still in the 415 // state it was in while running so we can act accordingly. 416 m_arch_ap->ThreadDidStop(); 417 418 // We may have suspended this thread so the primary thread could step 419 // without worrying about race conditions, so lets restore our suspend 420 // count. 421 RestoreSuspendCountAfterStop(); 422 423 // Update the basic information for a thread 424 MachThread::GetBasicInfo(m_mach_port_number, &m_basic_info); 425 426 if (m_basic_info.suspend_count > 0) 427 SetState(eStateSuspended); 428 else 429 SetState(eStateStopped); 430 return true; 431 } 432 433 bool MachThread::NotifyException(MachException::Data &exc) { 434 // Allow the arch specific protocol to process (MachException::Data &)exc 435 // first before possible reassignment of m_stop_exception with exc. 436 // See also MachThread::GetStopException(). 437 bool handled = m_arch_ap->NotifyException(exc); 438 439 if (m_stop_exception.IsValid()) { 440 // We may have more than one exception for a thread, but we need to 441 // only remember the one that we will say is the reason we stopped. 442 // We may have been single stepping and also gotten a signal exception, 443 // so just remember the most pertinent one. 444 if (m_stop_exception.IsBreakpoint()) 445 m_stop_exception = exc; 446 } else { 447 m_stop_exception = exc; 448 } 449 450 return handled; 451 } 452 453 nub_state_t MachThread::GetState() { 454 // If any other threads access this we will need a mutex for it 455 PTHREAD_MUTEX_LOCKER(locker, m_state_mutex); 456 return m_state; 457 } 458 459 void MachThread::SetState(nub_state_t state) { 460 PTHREAD_MUTEX_LOCKER(locker, m_state_mutex); 461 m_state = state; 462 DNBLogThreadedIf(LOG_THREAD, 463 "MachThread::SetState ( %s ) for tid = 0x%8.8" PRIx64 "", 464 DNBStateAsString(state), m_unique_id); 465 } 466 467 nub_size_t MachThread::GetNumRegistersInSet(nub_size_t regSet) const { 468 if (regSet < m_num_reg_sets) 469 return m_reg_sets[regSet].num_registers; 470 return 0; 471 } 472 473 const char *MachThread::GetRegisterSetName(nub_size_t regSet) const { 474 if (regSet < m_num_reg_sets) 475 return m_reg_sets[regSet].name; 476 return NULL; 477 } 478 479 const DNBRegisterInfo *MachThread::GetRegisterInfo(nub_size_t regSet, 480 nub_size_t regIndex) const { 481 if (regSet < m_num_reg_sets) 482 if (regIndex < m_reg_sets[regSet].num_registers) 483 return &m_reg_sets[regSet].registers[regIndex]; 484 return NULL; 485 } 486 void MachThread::DumpRegisterState(nub_size_t regSet) { 487 if (regSet == REGISTER_SET_ALL) { 488 for (regSet = 1; regSet < m_num_reg_sets; regSet++) 489 DumpRegisterState(regSet); 490 } else { 491 if (m_arch_ap->RegisterSetStateIsValid((int)regSet)) { 492 const size_t numRegisters = GetNumRegistersInSet(regSet); 493 uint32_t regIndex = 0; 494 DNBRegisterValueClass reg; 495 for (regIndex = 0; regIndex < numRegisters; ++regIndex) { 496 if (m_arch_ap->GetRegisterValue((uint32_t)regSet, regIndex, ®)) { 497 reg.Dump(NULL, NULL); 498 } 499 } 500 } else { 501 DNBLog("%s: registers are not currently valid.", 502 GetRegisterSetName(regSet)); 503 } 504 } 505 } 506 507 const DNBRegisterSetInfo * 508 MachThread::GetRegisterSetInfo(nub_size_t *num_reg_sets) const { 509 *num_reg_sets = m_num_reg_sets; 510 return &m_reg_sets[0]; 511 } 512 513 bool MachThread::GetRegisterValue(uint32_t set, uint32_t reg, 514 DNBRegisterValue *value) { 515 return m_arch_ap->GetRegisterValue(set, reg, value); 516 } 517 518 bool MachThread::SetRegisterValue(uint32_t set, uint32_t reg, 519 const DNBRegisterValue *value) { 520 return m_arch_ap->SetRegisterValue(set, reg, value); 521 } 522 523 nub_size_t MachThread::GetRegisterContext(void *buf, nub_size_t buf_len) { 524 return m_arch_ap->GetRegisterContext(buf, buf_len); 525 } 526 527 nub_size_t MachThread::SetRegisterContext(const void *buf, nub_size_t buf_len) { 528 return m_arch_ap->SetRegisterContext(buf, buf_len); 529 } 530 531 uint32_t MachThread::SaveRegisterState() { 532 return m_arch_ap->SaveRegisterState(); 533 } 534 bool MachThread::RestoreRegisterState(uint32_t save_id) { 535 return m_arch_ap->RestoreRegisterState(save_id); 536 } 537 538 uint32_t MachThread::EnableHardwareBreakpoint(const DNBBreakpoint *bp) { 539 if (bp != NULL && bp->IsBreakpoint()) 540 return m_arch_ap->EnableHardwareBreakpoint(bp->Address(), bp->ByteSize()); 541 return INVALID_NUB_HW_INDEX; 542 } 543 544 uint32_t MachThread::EnableHardwareWatchpoint(const DNBBreakpoint *wp, 545 bool also_set_on_task) { 546 if (wp != NULL && wp->IsWatchpoint()) 547 return m_arch_ap->EnableHardwareWatchpoint( 548 wp->Address(), wp->ByteSize(), wp->WatchpointRead(), 549 wp->WatchpointWrite(), also_set_on_task); 550 return INVALID_NUB_HW_INDEX; 551 } 552 553 bool MachThread::RollbackTransForHWP() { 554 return m_arch_ap->RollbackTransForHWP(); 555 } 556 557 bool MachThread::FinishTransForHWP() { return m_arch_ap->FinishTransForHWP(); } 558 559 bool MachThread::DisableHardwareBreakpoint(const DNBBreakpoint *bp) { 560 if (bp != NULL && bp->IsHardware()) 561 return m_arch_ap->DisableHardwareBreakpoint(bp->GetHardwareIndex()); 562 return false; 563 } 564 565 bool MachThread::DisableHardwareWatchpoint(const DNBBreakpoint *wp, 566 bool also_set_on_task) { 567 if (wp != NULL && wp->IsHardware()) 568 return m_arch_ap->DisableHardwareWatchpoint(wp->GetHardwareIndex(), 569 also_set_on_task); 570 return false; 571 } 572 573 uint32_t MachThread::NumSupportedHardwareWatchpoints() const { 574 return m_arch_ap->NumSupportedHardwareWatchpoints(); 575 } 576 577 bool MachThread::GetIdentifierInfo() { 578 // Don't try to get the thread info once and cache it for the life of the 579 // thread. It changes over time, for instance 580 // if the thread name changes, then the thread_handle also changes... So you 581 // have to refetch it every time. 582 mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT; 583 kern_return_t kret = ::thread_info(m_mach_port_number, THREAD_IDENTIFIER_INFO, 584 (thread_info_t)&m_ident_info, &count); 585 return kret == KERN_SUCCESS; 586 587 return false; 588 } 589 590 const char *MachThread::GetName() { 591 if (GetIdentifierInfo()) { 592 int len = ::proc_pidinfo(m_process->ProcessID(), PROC_PIDTHREADINFO, 593 m_ident_info.thread_handle, &m_proc_threadinfo, 594 sizeof(m_proc_threadinfo)); 595 596 if (len && m_proc_threadinfo.pth_name[0]) 597 return m_proc_threadinfo.pth_name; 598 } 599 return NULL; 600 } 601 602 uint64_t 603 MachThread::GetGloballyUniqueThreadIDForMachPortID(thread_t mach_port_id) { 604 kern_return_t kr; 605 thread_identifier_info_data_t tident; 606 mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT; 607 kr = thread_info(mach_port_id, THREAD_IDENTIFIER_INFO, (thread_info_t)&tident, 608 &tident_count); 609 if (kr != KERN_SUCCESS) { 610 return mach_port_id; 611 } 612 return tident.thread_id; 613 } 614 615 nub_addr_t MachThread::GetPThreadT() { 616 nub_addr_t pthread_t_value = INVALID_NUB_ADDRESS; 617 if (MachPortNumberIsValid(m_mach_port_number)) { 618 kern_return_t kr; 619 thread_identifier_info_data_t tident; 620 mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT; 621 kr = thread_info(m_mach_port_number, THREAD_IDENTIFIER_INFO, 622 (thread_info_t)&tident, &tident_count); 623 if (kr == KERN_SUCCESS) { 624 // Dereference thread_handle to get the pthread_t value for this thread. 625 if (m_is_64_bit) { 626 uint64_t addr; 627 if (m_process->ReadMemory(tident.thread_handle, 8, &addr) == 8) { 628 if (addr != 0) { 629 pthread_t_value = addr; 630 } 631 } 632 } else { 633 uint32_t addr; 634 if (m_process->ReadMemory(tident.thread_handle, 4, &addr) == 4) { 635 if (addr != 0) { 636 pthread_t_value = addr; 637 } 638 } 639 } 640 } 641 } 642 return pthread_t_value; 643 } 644 645 // Return this thread's TSD (Thread Specific Data) address. 646 // This is computed based on this thread's pthread_t value. 647 // 648 // We compute the TSD from the pthread_t by one of two methods. 649 // 650 // If plo_pthread_tsd_base_offset is non-zero, this is a simple offset that we 651 // add to 652 // the pthread_t to get the TSD base address. 653 // 654 // Else we read a pointer from memory at pthread_t + 655 // plo_pthread_tsd_base_address_offset and 656 // that gives us the TSD address. 657 // 658 // These plo_pthread_tsd_base values must be read out of libpthread by lldb & 659 // provided to debugserver. 660 661 nub_addr_t 662 MachThread::GetTSDAddressForThread(uint64_t plo_pthread_tsd_base_address_offset, 663 uint64_t plo_pthread_tsd_base_offset, 664 uint64_t plo_pthread_tsd_entry_size) { 665 nub_addr_t tsd_addr = INVALID_NUB_ADDRESS; 666 nub_addr_t pthread_t_value = GetPThreadT(); 667 if (plo_pthread_tsd_base_offset != 0 && 668 plo_pthread_tsd_base_offset != INVALID_NUB_ADDRESS) { 669 tsd_addr = pthread_t_value + plo_pthread_tsd_base_offset; 670 } else { 671 if (plo_pthread_tsd_entry_size == 4) { 672 uint32_t addr = 0; 673 if (m_process->ReadMemory(pthread_t_value + 674 plo_pthread_tsd_base_address_offset, 675 4, &addr) == 4) { 676 if (addr != 0) { 677 tsd_addr = addr; 678 } 679 } 680 } 681 if (plo_pthread_tsd_entry_size == 4) { 682 uint64_t addr = 0; 683 if (m_process->ReadMemory(pthread_t_value + 684 plo_pthread_tsd_base_address_offset, 685 8, &addr) == 8) { 686 if (addr != 0) { 687 tsd_addr = addr; 688 } 689 } 690 } 691 } 692 return tsd_addr; 693 } 694 695 nub_addr_t MachThread::GetDispatchQueueT() { 696 nub_addr_t dispatch_queue_t_value = INVALID_NUB_ADDRESS; 697 if (MachPortNumberIsValid(m_mach_port_number)) { 698 kern_return_t kr; 699 thread_identifier_info_data_t tident; 700 mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT; 701 kr = thread_info(m_mach_port_number, THREAD_IDENTIFIER_INFO, 702 (thread_info_t)&tident, &tident_count); 703 if (kr == KERN_SUCCESS && tident.dispatch_qaddr != 0 && 704 tident.dispatch_qaddr != INVALID_NUB_ADDRESS) { 705 // Dereference dispatch_qaddr to get the dispatch_queue_t value for this 706 // thread's queue, if any. 707 if (m_is_64_bit) { 708 uint64_t addr; 709 if (m_process->ReadMemory(tident.dispatch_qaddr, 8, &addr) == 8) { 710 if (addr != 0) 711 dispatch_queue_t_value = addr; 712 } 713 } else { 714 uint32_t addr; 715 if (m_process->ReadMemory(tident.dispatch_qaddr, 4, &addr) == 4) { 716 if (addr != 0) 717 dispatch_queue_t_value = addr; 718 } 719 } 720 } 721 } 722 return dispatch_queue_t_value; 723 } 724 725 ThreadInfo::QoS MachThread::GetRequestedQoS(nub_addr_t tsd, 726 uint64_t dti_qos_class_index) { 727 ThreadInfo::QoS qos_value; 728 if (MachPortNumberIsValid(m_mach_port_number) && 729 m_pthread_qos_class_decode != nullptr) { 730 uint64_t pthread_priority_value = 0; 731 if (m_is_64_bit) { 732 uint64_t pri; 733 if (m_process->ReadMemory(tsd + (dti_qos_class_index * 8), 8, &pri) == 734 8) { 735 pthread_priority_value = pri; 736 } 737 } else { 738 uint32_t pri; 739 if (m_process->ReadMemory(tsd + (dti_qos_class_index * 4), 4, &pri) == 740 4) { 741 pthread_priority_value = pri; 742 } 743 } 744 745 uint32_t requested_qos = 746 m_pthread_qos_class_decode(pthread_priority_value, NULL, NULL); 747 748 switch (requested_qos) { 749 // These constants from <pthread/qos.h> 750 case 0x21: 751 qos_value.enum_value = requested_qos; 752 qos_value.constant_name = "QOS_CLASS_USER_INTERACTIVE"; 753 qos_value.printable_name = "User Interactive"; 754 break; 755 case 0x19: 756 qos_value.enum_value = requested_qos; 757 qos_value.constant_name = "QOS_CLASS_USER_INITIATED"; 758 qos_value.printable_name = "User Initiated"; 759 break; 760 case 0x15: 761 qos_value.enum_value = requested_qos; 762 qos_value.constant_name = "QOS_CLASS_DEFAULT"; 763 qos_value.printable_name = "Default"; 764 break; 765 case 0x11: 766 qos_value.enum_value = requested_qos; 767 qos_value.constant_name = "QOS_CLASS_UTILITY"; 768 qos_value.printable_name = "Utility"; 769 break; 770 case 0x09: 771 qos_value.enum_value = requested_qos; 772 qos_value.constant_name = "QOS_CLASS_BACKGROUND"; 773 qos_value.printable_name = "Background"; 774 break; 775 case 0x00: 776 qos_value.enum_value = requested_qos; 777 qos_value.constant_name = "QOS_CLASS_UNSPECIFIED"; 778 qos_value.printable_name = "Unspecified"; 779 break; 780 } 781 } 782 return qos_value; 783 } 784