From 705b1809641ba3e102d437cffff29bfe2b611cab Mon Sep 17 00:00:00 2001 From: Jason Molenda Date: Fri, 13 Jun 2014 02:37:02 +0000 Subject: Initial merge of some of the iOS 8 / Mac OS X Yosemite specific lldb support. I'll be doing more testing & cleanup but I wanted to get the initial checkin done. This adds a new SBExpressionOptions::SetLanguage API for selecting a language of an expression. I added adds a new SBThread::GetInfoItemByPathString for retriving information about a thread from that thread's StructuredData. I added a new StructuredData class for representing key-value/array/dictionary information (e.g. JSON formatted data). Helper functions to read JSON and create a StructuredData object, and to print a StructuredData object in JSON format are included. A few Cocoa / Cocoa Touch data formatters were updated by Enrico to track changes in iOS 8 / Yosemite. Before we query a thread's extended information, the system runtime may provide hints to the remote debug stub that it will use to retrieve values out of runtime structures. I added a new SystemRuntime method AddThreadExtendedInfoPacketHints which allows the SystemRuntime to add key-value type data to the initial request that we send to the remote stub. The thread-format formatter string can now retrieve values out of a thread's extended info structured data. The default thread-format string picks up two of these - thread.info.activity.name and thread.info.trace_messages. I added a new "jThreadExtendedInfo" packet in debugserver; I will add documentation to the lldb-gdb-remote.txt doc soon. It accepts JSON formatted arguments (most importantly, "thread":threadnum) and it returns a variety of information regarding the thread to lldb in JSON format. This JSON return is scanned into a StructuredData object that is associated with the thread; UI layers can query the thread's StructuredData to see if key-values are present, and if so, show them to the user. These key-values are likely to be specific to different targets with some commonality among many targets. For instance, many targets will be able to advertise the pthread_t value for a thread. I added an initial rough cut of "thread info" command which will print the information about a thread from the jThreadExtendedInfo result. I need to do more work to make this format reasonably. Han Ming added calls into the pmenergy and pmsample libraries if debugserver is run on Mac OS X Yosemite to get information about the inferior's power use. I added support to debugserver for gathering the Genealogy information about threads, if it exists, and returning it in the jThreadExtendedInfo JSON result. llvm-svn: 210874 --- .../tools/debugserver/source/MacOSX/MachThread.cpp | 205 ++++++++++++++++++++- 1 file changed, 203 insertions(+), 2 deletions(-) (limited to 'lldb/tools/debugserver/source/MacOSX/MachThread.cpp') diff --git a/lldb/tools/debugserver/source/MacOSX/MachThread.cpp b/lldb/tools/debugserver/source/MacOSX/MachThread.cpp index f3a9d51c808..f0650d34783 100644 --- a/lldb/tools/debugserver/source/MacOSX/MachThread.cpp +++ b/lldb/tools/debugserver/source/MacOSX/MachThread.cpp @@ -12,10 +12,13 @@ //===----------------------------------------------------------------------===// #include +#include +#include #include "MachThread.h" #include "MachProcess.h" #include "DNBLog.h" #include "DNB.h" +#include "ThreadInfo.h" static uint32_t GetSequenceID() @@ -24,7 +27,7 @@ GetSequenceID() return ++g_nextID; } -MachThread::MachThread (MachProcess *process, uint64_t unique_thread_id, thread_t mach_port_num) : +MachThread::MachThread (MachProcess *process, bool is_64_bit, uint64_t unique_thread_id, thread_t mach_port_num) : m_process (process), m_unique_id (unique_thread_id), m_mach_port_number (mach_port_num), @@ -38,12 +41,16 @@ MachThread::MachThread (MachProcess *process, uint64_t unique_thread_id, thread_ m_num_reg_sets (0), m_ident_info(), m_proc_threadinfo(), - m_dispatch_queue_name() + m_dispatch_queue_name(), + m_is_64_bit(is_64_bit), + m_pthread_qos_class_decode (nullptr) { nub_size_t num_reg_sets = 0; m_reg_sets = m_arch_ap->GetRegisterSetInfo (&num_reg_sets); m_num_reg_sets = num_reg_sets; + m_pthread_qos_class_decode = (unsigned int (*)(unsigned long, int*, unsigned long*)) dlsym (RTLD_DEFAULT, "_pthread_qos_class_decode"); + // Get the thread state so we know if a thread is in a state where we can't // muck with it and also so we get the suspend count correct in case it was // already suspended @@ -719,3 +726,197 @@ MachThread::GetGloballyUniqueThreadIDForMachPortID (thread_t mach_port_id) } return tident.thread_id; } + +nub_addr_t +MachThread::GetPThreadT () +{ + nub_addr_t pthread_t_value = INVALID_NUB_ADDRESS; + if (MachPortNumberIsValid (m_mach_port_number)) + { + kern_return_t kr; + thread_identifier_info_data_t tident; + mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT; + kr = thread_info (m_mach_port_number, THREAD_IDENTIFIER_INFO, + (thread_info_t) &tident, &tident_count); + if (kr == KERN_SUCCESS) + { + // Dereference thread_handle to get the pthread_t value for this thread. + if (m_is_64_bit) + { + uint64_t addr; + if (m_process->ReadMemory (tident.thread_handle, 8, &addr) == 8) + { + if (addr != 0) + { + pthread_t_value = addr; + } + } + } + else + { + uint32_t addr; + if (m_process->ReadMemory (tident.thread_handle, 4, &addr) == 4) + { + if (addr != 0) + { + pthread_t_value = addr; + } + } + } + } + } + return pthread_t_value; +} + +// Return this thread's TSD (Thread Specific Data) address. +// This is computed based on this thread's pthread_t value. +// +// We compute the TSD from the pthread_t by one of two methods. +// +// If plo_pthread_tsd_base_offset is non-zero, this is a simple offset that we add to +// the pthread_t to get the TSD base address. +// +// Else we read a pointer from memory at pthread_t + plo_pthread_tsd_base_address_offset and +// that gives us the TSD address. +// +// These plo_pthread_tsd_base values must be read out of libpthread by lldb & provided to debugserver. + +nub_addr_t +MachThread::GetTSDAddressForThread (uint64_t plo_pthread_tsd_base_address_offset, uint64_t plo_pthread_tsd_base_offset, uint64_t plo_pthread_tsd_entry_size) +{ + nub_addr_t tsd_addr = INVALID_NUB_ADDRESS; + nub_addr_t pthread_t_value = GetPThreadT(); + if (plo_pthread_tsd_base_offset != 0 && plo_pthread_tsd_base_offset != INVALID_NUB_ADDRESS) + { + tsd_addr = pthread_t_value + plo_pthread_tsd_base_offset; + } + else + { + if (plo_pthread_tsd_entry_size == 4) + { + uint32_t addr = 0; + if (m_process->ReadMemory (pthread_t_value + plo_pthread_tsd_base_address_offset, 4, &addr) == 4) + { + if (addr != 0) + { + tsd_addr = addr; + } + } + } + if (plo_pthread_tsd_entry_size == 4) + { + uint64_t addr = 0; + if (m_process->ReadMemory (pthread_t_value + plo_pthread_tsd_base_address_offset, 8, &addr) == 8) + { + if (addr != 0) + { + tsd_addr = addr; + } + } + } + } + return tsd_addr; +} + + +nub_addr_t +MachThread::GetDispatchQueueT () +{ + nub_addr_t dispatch_queue_t_value = INVALID_NUB_ADDRESS; + if (MachPortNumberIsValid (m_mach_port_number)) + { + kern_return_t kr; + thread_identifier_info_data_t tident; + mach_msg_type_number_t tident_count = THREAD_IDENTIFIER_INFO_COUNT; + kr = thread_info (m_mach_port_number, THREAD_IDENTIFIER_INFO, + (thread_info_t) &tident, &tident_count); + if (kr == KERN_SUCCESS && tident.dispatch_qaddr != 0 && tident.dispatch_qaddr != INVALID_NUB_ADDRESS) + { + // Dereference dispatch_qaddr to get the dispatch_queue_t value for this thread's queue, if any. + if (m_is_64_bit) + { + uint64_t addr; + if (m_process->ReadMemory (tident.dispatch_qaddr, 8, &addr) == 8) + { + if (addr != 0) + dispatch_queue_t_value = addr; + } + } + else + { + uint32_t addr; + if (m_process->ReadMemory (tident.dispatch_qaddr, 4, &addr) == 4) + { + if (addr != 0) + dispatch_queue_t_value = addr; + } + } + } + } + return dispatch_queue_t_value; +} + + +ThreadInfo::QoS +MachThread::GetRequestedQoS (nub_addr_t tsd, uint64_t dti_qos_class_index) +{ + ThreadInfo::QoS qos_value; + if (MachPortNumberIsValid (m_mach_port_number) && m_pthread_qos_class_decode != nullptr) + { + uint64_t pthread_priority_value = 0; + if (m_is_64_bit) + { + uint64_t pri; + if (m_process->ReadMemory (tsd + (dti_qos_class_index * 8), 8, &pri) == 8) + { + pthread_priority_value = pri; + } + } + else + { + uint32_t pri; + if (m_process->ReadMemory (tsd + (dti_qos_class_index * 4), 4, &pri) == 4) + { + pthread_priority_value = pri; + } + } + + uint32_t requested_qos = m_pthread_qos_class_decode (pthread_priority_value, NULL, NULL); + + switch (requested_qos) + { + // These constants from + case 0x21: + qos_value.enum_value = requested_qos; + qos_value.constant_name = "QOS_CLASS_USER_INTERACTIVE"; + qos_value.printable_name = "User Interactive"; + break; + case 0x19: + qos_value.enum_value = requested_qos; + qos_value.constant_name = "QOS_CLASS_USER_INITIATED"; + qos_value.printable_name = "User Initiated"; + break; + case 0x15: + qos_value.enum_value = requested_qos; + qos_value.constant_name = "QOS_CLASS_DEFAULT"; + qos_value.printable_name = "Default"; + break; + case 0x11: + qos_value.enum_value = requested_qos; + qos_value.constant_name = "QOS_CLASS_UTILITY"; + qos_value.printable_name = "Utility"; + break; + case 0x09: + qos_value.enum_value = requested_qos; + qos_value.constant_name = "QOS_CLASS_BACKGROUND"; + qos_value.printable_name = "Background"; + break; + case 0x00: + qos_value.enum_value = requested_qos; + qos_value.constant_name = "QOS_CLASS_UNSPECIFIED"; + qos_value.printable_name = "Unspecified"; + break; + } + } + return qos_value; +} -- cgit v1.2.3