Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- SystemRuntimeMacOSX.cpp -------------------------------------------===// |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9 | #include "Plugins/Process/Utility/HistoryThread.h" |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 10 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Module.h" |
| 12 | #include "lldb/Core/ModuleSpec.h" |
| 13 | #include "lldb/Core/PluginManager.h" |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Section.h" |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 15 | #include "lldb/Symbol/TypeSystemClang.h" |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/ObjectFile.h" |
| 17 | #include "lldb/Symbol/SymbolContext.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include "lldb/Target/Process.h" |
Zachary Turner | 01c3243 | 2017-02-14 19:06:07 +0000 | [diff] [blame] | 19 | #include "lldb/Target/ProcessStructReader.h" |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 20 | #include "lldb/Target/Queue.h" |
| 21 | #include "lldb/Target/QueueList.h" |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 22 | #include "lldb/Target/Target.h" |
| 23 | #include "lldb/Target/Thread.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/DataBufferHeap.h" |
| 25 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | 5713a05 | 2017-03-22 18:40:07 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/FileSpec.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/StreamString.h" |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 29 | |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 30 | #include "SystemRuntimeMacOSX.h" |
| 31 | |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 32 | #include <memory> |
| 33 | |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 34 | using namespace lldb; |
| 35 | using namespace lldb_private; |
| 36 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 37 | // Create an instance of this class. This function is filled into the plugin |
| 38 | // info class that gets handed out by the plugin factory and allows the lldb to |
| 39 | // instantiate an instance of this class. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | SystemRuntime *SystemRuntimeMacOSX::CreateInstance(Process *process) { |
| 41 | bool create = false; |
| 42 | if (!create) { |
| 43 | create = true; |
| 44 | Module *exe_module = process->GetTarget().GetExecutableModulePointer(); |
| 45 | if (exe_module) { |
| 46 | ObjectFile *object_file = exe_module->GetObjectFile(); |
| 47 | if (object_file) { |
| 48 | create = (object_file->GetStrata() == ObjectFile::eStrataUser); |
| 49 | } |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 50 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | |
| 52 | if (create) { |
| 53 | const llvm::Triple &triple_ref = |
| 54 | process->GetTarget().GetArchitecture().GetTriple(); |
| 55 | switch (triple_ref.getOS()) { |
| 56 | case llvm::Triple::Darwin: |
| 57 | case llvm::Triple::MacOSX: |
| 58 | case llvm::Triple::IOS: |
| 59 | case llvm::Triple::TvOS: |
| 60 | case llvm::Triple::WatchOS: |
Jason Molenda | 32762fd | 2018-10-11 00:28:35 +0000 | [diff] [blame] | 61 | // NEED_BRIDGEOS_TRIPLE case llvm::Triple::BridgeOS: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | create = triple_ref.getVendor() == llvm::Triple::Apple; |
| 63 | break; |
| 64 | default: |
| 65 | create = false; |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (create) |
| 72 | return new SystemRuntimeMacOSX(process); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 73 | return nullptr; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 76 | // Constructor |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 77 | SystemRuntimeMacOSX::SystemRuntimeMacOSX(Process *process) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | : SystemRuntime(process), m_break_id(LLDB_INVALID_BREAK_ID), m_mutex(), |
| 79 | m_get_queues_handler(process), m_get_pending_items_handler(process), |
| 80 | m_get_item_info_handler(process), m_get_thread_item_info_handler(process), |
| 81 | m_page_to_free(LLDB_INVALID_ADDRESS), m_page_to_free_size(0), |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 82 | m_lib_backtrace_recording_info(), |
| 83 | m_dispatch_queue_offsets_addr(LLDB_INVALID_ADDRESS), |
| 84 | m_libdispatch_offsets(), |
| 85 | m_libpthread_layout_offsets_addr(LLDB_INVALID_ADDRESS), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | m_libpthread_offsets(), m_dispatch_tsd_indexes_addr(LLDB_INVALID_ADDRESS), |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 87 | m_libdispatch_tsd_indexes(), |
| 88 | m_dispatch_voucher_offsets_addr(LLDB_INVALID_ADDRESS), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | m_libdispatch_voucher_offsets() {} |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 90 | |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 91 | // Destructor |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | SystemRuntimeMacOSX::~SystemRuntimeMacOSX() { Clear(true); } |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 93 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | void SystemRuntimeMacOSX::Detach() { |
| 95 | m_get_queues_handler.Detach(); |
| 96 | m_get_pending_items_handler.Detach(); |
| 97 | m_get_item_info_handler.Detach(); |
| 98 | m_get_thread_item_info_handler.Detach(); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 101 | // Clear out the state of this class. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | void SystemRuntimeMacOSX::Clear(bool clear_process) { |
| 103 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 104 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | if (m_process->IsAlive() && LLDB_BREAK_ID_IS_VALID(m_break_id)) |
| 106 | m_process->ClearBreakpointSiteByID(m_break_id); |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | if (clear_process) |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 109 | m_process = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | m_break_id = LLDB_INVALID_BREAK_ID; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 113 | std::string |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | SystemRuntimeMacOSX::GetQueueNameFromThreadQAddress(addr_t dispatch_qaddr) { |
| 115 | std::string dispatch_queue_name; |
| 116 | if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0) |
| 117 | return ""; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | ReadLibdispatchOffsets(); |
| 120 | if (m_libdispatch_offsets.IsValid()) { |
| 121 | // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 122 | // thread - deref it to get the address of the dispatch_queue_t structure |
| 123 | // for this thread's queue. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 124 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | addr_t dispatch_queue_addr = |
| 126 | m_process->ReadPointerFromMemory(dispatch_qaddr, error); |
| 127 | if (error.Success()) { |
| 128 | if (m_libdispatch_offsets.dqo_version >= 4) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 129 | // libdispatch versions 4+, pointer to dispatch name is in the queue |
| 130 | // structure. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | addr_t pointer_to_label_address = |
| 132 | dispatch_queue_addr + m_libdispatch_offsets.dqo_label; |
| 133 | addr_t label_addr = |
| 134 | m_process->ReadPointerFromMemory(pointer_to_label_address, error); |
| 135 | if (error.Success()) { |
| 136 | m_process->ReadCStringFromMemory(label_addr, dispatch_queue_name, |
| 137 | error); |
| 138 | } |
| 139 | } else { |
| 140 | // libdispatch versions 1-3, dispatch name is a fixed width char array |
| 141 | // in the queue structure. |
| 142 | addr_t label_addr = |
| 143 | dispatch_queue_addr + m_libdispatch_offsets.dqo_label; |
| 144 | dispatch_queue_name.resize(m_libdispatch_offsets.dqo_label_size, '\0'); |
| 145 | size_t bytes_read = |
| 146 | m_process->ReadMemory(label_addr, &dispatch_queue_name[0], |
| 147 | m_libdispatch_offsets.dqo_label_size, error); |
| 148 | if (bytes_read < m_libdispatch_offsets.dqo_label_size) |
| 149 | dispatch_queue_name.erase(bytes_read); |
| 150 | } |
Jason Molenda | aac16e0 | 2014-03-13 02:54:54 +0000 | [diff] [blame] | 151 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | } |
| 153 | return dispatch_queue_name; |
Jason Molenda | aac16e0 | 2014-03-13 02:54:54 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | lldb::addr_t SystemRuntimeMacOSX::GetLibdispatchQueueAddressFromThreadQAddress( |
| 157 | addr_t dispatch_qaddr) { |
| 158 | addr_t libdispatch_queue_t_address = LLDB_INVALID_ADDRESS; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 159 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | libdispatch_queue_t_address = |
| 161 | m_process->ReadPointerFromMemory(dispatch_qaddr, error); |
| 162 | if (!error.Success()) { |
| 163 | libdispatch_queue_t_address = LLDB_INVALID_ADDRESS; |
| 164 | } |
| 165 | return libdispatch_queue_t_address; |
Jason Molenda | aac16e0 | 2014-03-13 02:54:54 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | lldb::QueueKind SystemRuntimeMacOSX::GetQueueKind(addr_t dispatch_queue_addr) { |
| 169 | if (dispatch_queue_addr == LLDB_INVALID_ADDRESS || dispatch_queue_addr == 0) |
| 170 | return eQueueKindUnknown; |
| 171 | |
| 172 | QueueKind kind = eQueueKindUnknown; |
| 173 | ReadLibdispatchOffsets(); |
| 174 | if (m_libdispatch_offsets.IsValid() && |
| 175 | m_libdispatch_offsets.dqo_version >= 4) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 176 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | uint64_t width = m_process->ReadUnsignedIntegerFromMemory( |
| 178 | dispatch_queue_addr + m_libdispatch_offsets.dqo_width, |
| 179 | m_libdispatch_offsets.dqo_width_size, 0, error); |
| 180 | if (error.Success()) { |
| 181 | if (width == 1) { |
| 182 | kind = eQueueKindSerial; |
| 183 | } |
| 184 | if (width > 1) { |
| 185 | kind = eQueueKindConcurrent; |
| 186 | } |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 187 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | } |
| 189 | return kind; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 192 | void SystemRuntimeMacOSX::AddThreadExtendedInfoPacketHints( |
| 193 | lldb_private::StructuredData::ObjectSP dict_sp) { |
| 194 | StructuredData::Dictionary *dict = dict_sp->GetAsDictionary(); |
| 195 | if (dict) { |
| 196 | ReadLibpthreadOffsets(); |
| 197 | if (m_libpthread_offsets.IsValid()) { |
| 198 | dict->AddIntegerItem("plo_pthread_tsd_base_offset", |
| 199 | m_libpthread_offsets.plo_pthread_tsd_base_offset); |
| 200 | dict->AddIntegerItem( |
| 201 | "plo_pthread_tsd_base_address_offset", |
| 202 | m_libpthread_offsets.plo_pthread_tsd_base_address_offset); |
| 203 | dict->AddIntegerItem("plo_pthread_tsd_entry_size", |
| 204 | m_libpthread_offsets.plo_pthread_tsd_entry_size); |
Jason Molenda | b4892cd | 2014-05-13 22:02:48 +0000 | [diff] [blame] | 205 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | |
| 207 | ReadLibdispatchTSDIndexes(); |
| 208 | if (m_libdispatch_tsd_indexes.IsValid()) { |
| 209 | dict->AddIntegerItem("dti_queue_index", |
| 210 | m_libdispatch_tsd_indexes.dti_queue_index); |
| 211 | dict->AddIntegerItem("dti_voucher_index", |
| 212 | m_libdispatch_tsd_indexes.dti_voucher_index); |
| 213 | dict->AddIntegerItem("dti_qos_class_index", |
| 214 | m_libdispatch_tsd_indexes.dti_qos_class_index); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | bool SystemRuntimeMacOSX::SafeToCallFunctionsOnThisThread(ThreadSP thread_sp) { |
| 220 | if (thread_sp && thread_sp->GetStackFrameCount() > 0 && |
| 221 | thread_sp->GetFrameWithConcreteFrameIndex(0)) { |
| 222 | const SymbolContext sym_ctx( |
| 223 | thread_sp->GetFrameWithConcreteFrameIndex(0)->GetSymbolContext( |
| 224 | eSymbolContextSymbol)); |
| 225 | static ConstString g_select_symbol("__select"); |
| 226 | if (sym_ctx.GetFunctionName() == g_select_symbol) { |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 | return true; |
Jason Molenda | b4892cd | 2014-05-13 22:02:48 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 233 | lldb::queue_id_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | SystemRuntimeMacOSX::GetQueueIDFromThreadQAddress(lldb::addr_t dispatch_qaddr) { |
| 235 | queue_id_t queue_id = LLDB_INVALID_QUEUE_ID; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 236 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | if (dispatch_qaddr == LLDB_INVALID_ADDRESS || dispatch_qaddr == 0) |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 238 | return queue_id; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 239 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 240 | ReadLibdispatchOffsets(); |
| 241 | if (m_libdispatch_offsets.IsValid()) { |
| 242 | // dispatch_qaddr is from a thread_info(THREAD_IDENTIFIER_INFO) call for a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 243 | // thread - deref it to get the address of the dispatch_queue_t structure |
| 244 | // for this thread's queue. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 245 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | uint64_t dispatch_queue_addr = |
| 247 | m_process->ReadPointerFromMemory(dispatch_qaddr, error); |
| 248 | if (error.Success()) { |
| 249 | addr_t serialnum_address = |
| 250 | dispatch_queue_addr + m_libdispatch_offsets.dqo_serialnum; |
| 251 | queue_id_t serialnum = m_process->ReadUnsignedIntegerFromMemory( |
| 252 | serialnum_address, m_libdispatch_offsets.dqo_serialnum_size, |
| 253 | LLDB_INVALID_QUEUE_ID, error); |
| 254 | if (error.Success()) { |
| 255 | queue_id = serialnum; |
| 256 | } |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 257 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return queue_id; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 263 | void SystemRuntimeMacOSX::ReadLibdispatchOffsetsAddress() { |
| 264 | if (m_dispatch_queue_offsets_addr != LLDB_INVALID_ADDRESS) |
| 265 | return; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 266 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 267 | static ConstString g_dispatch_queue_offsets_symbol_name( |
| 268 | "dispatch_queue_offsets"); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 269 | const Symbol *dispatch_queue_offsets_symbol = nullptr; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | // libdispatch symbols were in libSystem.B.dylib up through Mac OS X 10.6 |
| 272 | // ("Snow Leopard") |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 273 | ModuleSpec libSystem_module_spec(FileSpec("libSystem.B.dylib")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule( |
| 275 | libSystem_module_spec)); |
| 276 | if (module_sp) |
| 277 | dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType( |
| 278 | g_dispatch_queue_offsets_symbol_name, eSymbolTypeData); |
| 279 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 280 | // libdispatch symbols are in their own dylib as of Mac OS X 10.7 ("Lion") |
| 281 | // and later |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 282 | if (dispatch_queue_offsets_symbol == nullptr) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 283 | ModuleSpec libdispatch_module_spec(FileSpec("libdispatch.dylib")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | module_sp = m_process->GetTarget().GetImages().FindFirstModule( |
| 285 | libdispatch_module_spec); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 286 | if (module_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | dispatch_queue_offsets_symbol = module_sp->FindFirstSymbolWithNameAndType( |
| 288 | g_dispatch_queue_offsets_symbol_name, eSymbolTypeData); |
| 289 | } |
| 290 | if (dispatch_queue_offsets_symbol) |
| 291 | m_dispatch_queue_offsets_addr = |
| 292 | dispatch_queue_offsets_symbol->GetLoadAddress(&m_process->GetTarget()); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | void SystemRuntimeMacOSX::ReadLibdispatchOffsets() { |
| 296 | if (m_libdispatch_offsets.IsValid()) |
| 297 | return; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 298 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 299 | ReadLibdispatchOffsetsAddress(); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 300 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 301 | uint8_t memory_buffer[sizeof(struct LibdispatchOffsets)]; |
| 302 | DataExtractor data(memory_buffer, sizeof(memory_buffer), |
| 303 | m_process->GetByteOrder(), |
| 304 | m_process->GetAddressByteSize()); |
| 305 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 306 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 307 | if (m_process->ReadMemory(m_dispatch_queue_offsets_addr, memory_buffer, |
| 308 | sizeof(memory_buffer), |
| 309 | error) == sizeof(memory_buffer)) { |
| 310 | lldb::offset_t data_offset = 0; |
| 311 | |
| 312 | // The struct LibdispatchOffsets is a series of uint16_t's - extract them |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 313 | // all in one big go. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | data.GetU16(&data_offset, &m_libdispatch_offsets.dqo_version, |
| 315 | sizeof(struct LibdispatchOffsets) / sizeof(uint16_t)); |
| 316 | } |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 319 | void SystemRuntimeMacOSX::ReadLibpthreadOffsetsAddress() { |
| 320 | if (m_libpthread_layout_offsets_addr != LLDB_INVALID_ADDRESS) |
| 321 | return; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 323 | static ConstString g_libpthread_layout_offsets_symbol_name( |
| 324 | "pthread_layout_offsets"); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 325 | const Symbol *libpthread_layout_offsets_symbol = nullptr; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 326 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 327 | ModuleSpec libpthread_module_spec(FileSpec("libsystem_pthread.dylib")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 328 | ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule( |
| 329 | libpthread_module_spec)); |
| 330 | if (module_sp) { |
| 331 | libpthread_layout_offsets_symbol = |
| 332 | module_sp->FindFirstSymbolWithNameAndType( |
| 333 | g_libpthread_layout_offsets_symbol_name, eSymbolTypeData); |
| 334 | if (libpthread_layout_offsets_symbol) { |
| 335 | m_libpthread_layout_offsets_addr = |
| 336 | libpthread_layout_offsets_symbol->GetLoadAddress( |
| 337 | &m_process->GetTarget()); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 338 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 339 | } |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | void SystemRuntimeMacOSX::ReadLibpthreadOffsets() { |
| 343 | if (m_libpthread_offsets.IsValid()) |
| 344 | return; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 345 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 346 | ReadLibpthreadOffsetsAddress(); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 347 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | if (m_libpthread_layout_offsets_addr != LLDB_INVALID_ADDRESS) { |
| 349 | uint8_t memory_buffer[sizeof(struct LibpthreadOffsets)]; |
| 350 | DataExtractor data(memory_buffer, sizeof(memory_buffer), |
| 351 | m_process->GetByteOrder(), |
| 352 | m_process->GetAddressByteSize()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 353 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | if (m_process->ReadMemory(m_libpthread_layout_offsets_addr, memory_buffer, |
| 355 | sizeof(memory_buffer), |
| 356 | error) == sizeof(memory_buffer)) { |
| 357 | lldb::offset_t data_offset = 0; |
Jason Molenda | 59ac67e | 2014-09-12 00:09:04 +0000 | [diff] [blame] | 358 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 359 | // The struct LibpthreadOffsets is a series of uint16_t's - extract them |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 360 | // all in one big go. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | data.GetU16(&data_offset, &m_libpthread_offsets.plo_version, |
| 362 | sizeof(struct LibpthreadOffsets) / sizeof(uint16_t)); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void SystemRuntimeMacOSX::ReadLibdispatchTSDIndexesAddress() { |
| 368 | if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) |
| 369 | return; |
| 370 | |
| 371 | static ConstString g_libdispatch_tsd_indexes_symbol_name( |
| 372 | "dispatch_tsd_indexes"); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 373 | const Symbol *libdispatch_tsd_indexes_symbol = nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 374 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 375 | ModuleSpec libpthread_module_spec(FileSpec("libdispatch.dylib")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 376 | ModuleSP module_sp(m_process->GetTarget().GetImages().FindFirstModule( |
| 377 | libpthread_module_spec)); |
| 378 | if (module_sp) { |
| 379 | libdispatch_tsd_indexes_symbol = module_sp->FindFirstSymbolWithNameAndType( |
| 380 | g_libdispatch_tsd_indexes_symbol_name, eSymbolTypeData); |
| 381 | if (libdispatch_tsd_indexes_symbol) { |
| 382 | m_dispatch_tsd_indexes_addr = |
| 383 | libdispatch_tsd_indexes_symbol->GetLoadAddress( |
| 384 | &m_process->GetTarget()); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void SystemRuntimeMacOSX::ReadLibdispatchTSDIndexes() { |
| 390 | if (m_libdispatch_tsd_indexes.IsValid()) |
| 391 | return; |
| 392 | |
| 393 | ReadLibdispatchTSDIndexesAddress(); |
| 394 | |
| 395 | if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) { |
| 396 | |
| 397 | // We don't need to check the version number right now, it will be at least 2, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 398 | // but keep this code around to fetch just the version # for the future where |
| 399 | // we need to fetch alternate versions of the struct. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 400 | #if 0 |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 401 | uint16_t dti_version = 2; |
Jason Molenda | 59ac67e | 2014-09-12 00:09:04 +0000 | [diff] [blame] | 402 | Address dti_struct_addr; |
| 403 | if (m_process->GetTarget().ResolveLoadAddress (m_dispatch_tsd_indexes_addr, dti_struct_addr)) |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 404 | { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 405 | Status error; |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 406 | uint16_t version = m_process->GetTarget().ReadUnsignedIntegerFromMemory (dti_struct_addr, false, 2, UINT16_MAX, error); |
| 407 | if (error.Success() && dti_version != UINT16_MAX) |
| 408 | { |
| 409 | dti_version = version; |
| 410 | } |
| 411 | } |
Jason Molenda | 59ac67e | 2014-09-12 00:09:04 +0000 | [diff] [blame] | 412 | #endif |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 413 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 414 | TypeSystemClang *ast_ctx = |
| 415 | TypeSystemClang::GetScratch(m_process->GetTarget()); |
Raphael Isemann | f9f49d3 | 2019-12-21 22:40:52 +0100 | [diff] [blame] | 416 | if (m_dispatch_tsd_indexes_addr != LLDB_INVALID_ADDRESS) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 417 | CompilerType uint16 = |
| 418 | ast_ctx->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16); |
| 419 | CompilerType dispatch_tsd_indexes_s = ast_ctx->CreateRecordType( |
| 420 | nullptr, lldb::eAccessPublic, "__lldb_dispatch_tsd_indexes_s", |
| 421 | clang::TTK_Struct, lldb::eLanguageTypeC); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 422 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 423 | TypeSystemClang::StartTagDeclarationDefinition(dispatch_tsd_indexes_s); |
| 424 | TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | "dti_version", uint16, |
| 426 | lldb::eAccessPublic, 0); |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 427 | TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | "dti_queue_index", uint16, |
| 429 | lldb::eAccessPublic, 0); |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 430 | TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 431 | "dti_voucher_index", uint16, |
| 432 | lldb::eAccessPublic, 0); |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 433 | TypeSystemClang::AddFieldToRecordType(dispatch_tsd_indexes_s, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 434 | "dti_qos_class_index", uint16, |
| 435 | lldb::eAccessPublic, 0); |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 436 | TypeSystemClang::CompleteTagDeclarationDefinition(dispatch_tsd_indexes_s); |
Jason Molenda | 59ac67e | 2014-09-12 00:09:04 +0000 | [diff] [blame] | 437 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | ProcessStructReader struct_reader(m_process, m_dispatch_tsd_indexes_addr, |
| 439 | dispatch_tsd_indexes_s); |
Jason Molenda | 59ac67e | 2014-09-12 00:09:04 +0000 | [diff] [blame] | 440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 441 | m_libdispatch_tsd_indexes.dti_version = |
| 442 | struct_reader.GetField<uint16_t>(ConstString("dti_version")); |
| 443 | m_libdispatch_tsd_indexes.dti_queue_index = |
| 444 | struct_reader.GetField<uint16_t>(ConstString("dti_queue_index")); |
| 445 | m_libdispatch_tsd_indexes.dti_voucher_index = |
| 446 | struct_reader.GetField<uint16_t>(ConstString("dti_voucher_index")); |
| 447 | m_libdispatch_tsd_indexes.dti_qos_class_index = |
| 448 | struct_reader.GetField<uint16_t>(ConstString("dti_qos_class_index")); |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 449 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | } |
Jason Molenda | 705b180 | 2014-06-13 02:37:02 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 453 | ThreadSP SystemRuntimeMacOSX::GetExtendedBacktraceThread(ThreadSP real_thread, |
| 454 | ConstString type) { |
| 455 | ThreadSP originating_thread_sp; |
Raphael Isemann | 05cfdb0 | 2019-04-26 07:21:36 +0000 | [diff] [blame] | 456 | if (BacktraceRecordingHeadersInitialized() && type == "libdispatch") { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 457 | Status error; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 458 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 459 | // real_thread is either an actual, live thread (in which case we need to |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 460 | // call into libBacktraceRecording to find its originator) or it is an |
| 461 | // extended backtrace itself, in which case we get the token from it and |
| 462 | // call into libBacktraceRecording to find the originator of that token. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 463 | |
| 464 | if (real_thread->GetExtendedBacktraceToken() != LLDB_INVALID_ADDRESS) { |
| 465 | originating_thread_sp = GetExtendedBacktraceFromItemRef( |
| 466 | real_thread->GetExtendedBacktraceToken()); |
| 467 | } else { |
| 468 | ThreadSP cur_thread_sp( |
| 469 | m_process->GetThreadList().GetExpressionExecutionThread()); |
| 470 | AppleGetThreadItemInfoHandler::GetThreadItemInfoReturnInfo ret = |
| 471 | m_get_thread_item_info_handler.GetThreadItemInfo( |
| 472 | *cur_thread_sp.get(), real_thread->GetID(), m_page_to_free, |
| 473 | m_page_to_free_size, error); |
| 474 | m_page_to_free = LLDB_INVALID_ADDRESS; |
| 475 | m_page_to_free_size = 0; |
| 476 | if (ret.item_buffer_ptr != 0 && |
| 477 | ret.item_buffer_ptr != LLDB_INVALID_ADDRESS && |
| 478 | ret.item_buffer_size > 0) { |
| 479 | DataBufferHeap data(ret.item_buffer_size, 0); |
| 480 | if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(), |
| 481 | ret.item_buffer_size, error) && |
| 482 | error.Success()) { |
| 483 | DataExtractor extractor(data.GetBytes(), data.GetByteSize(), |
| 484 | m_process->GetByteOrder(), |
| 485 | m_process->GetAddressByteSize()); |
| 486 | ItemInfo item = ExtractItemInfoFromBuffer(extractor); |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 487 | originating_thread_sp = std::make_shared<HistoryThread>( |
Alex Langford | 86df61c | 2019-06-19 21:33:44 +0000 | [diff] [blame] | 488 | *m_process, item.enqueuing_thread_id, item.enqueuing_callstack); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | originating_thread_sp->SetExtendedBacktraceToken( |
| 490 | item.item_that_enqueued_this); |
| 491 | originating_thread_sp->SetQueueName( |
| 492 | item.enqueuing_queue_label.c_str()); |
| 493 | originating_thread_sp->SetQueueID(item.enqueuing_queue_serialnum); |
| 494 | // originating_thread_sp->SetThreadName |
| 495 | // (item.enqueuing_thread_label.c_str()); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 496 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 497 | m_page_to_free = ret.item_buffer_ptr; |
| 498 | m_page_to_free_size = ret.item_buffer_size; |
| 499 | } |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 500 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 501 | } |
| 502 | return originating_thread_sp; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | ThreadSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 506 | SystemRuntimeMacOSX::GetExtendedBacktraceFromItemRef(lldb::addr_t item_ref) { |
| 507 | ThreadSP return_thread_sp; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 508 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 509 | AppleGetItemInfoHandler::GetItemInfoReturnInfo ret; |
| 510 | ThreadSP cur_thread_sp( |
| 511 | m_process->GetThreadList().GetExpressionExecutionThread()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 512 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 513 | ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref, |
| 514 | m_page_to_free, m_page_to_free_size, |
| 515 | error); |
| 516 | m_page_to_free = LLDB_INVALID_ADDRESS; |
| 517 | m_page_to_free_size = 0; |
| 518 | if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS && |
| 519 | ret.item_buffer_size > 0) { |
| 520 | DataBufferHeap data(ret.item_buffer_size, 0); |
| 521 | if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(), |
| 522 | ret.item_buffer_size, error) && |
| 523 | error.Success()) { |
| 524 | DataExtractor extractor(data.GetBytes(), data.GetByteSize(), |
| 525 | m_process->GetByteOrder(), |
| 526 | m_process->GetAddressByteSize()); |
| 527 | ItemInfo item = ExtractItemInfoFromBuffer(extractor); |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 528 | return_thread_sp = std::make_shared<HistoryThread>( |
Alex Langford | 86df61c | 2019-06-19 21:33:44 +0000 | [diff] [blame] | 529 | *m_process, item.enqueuing_thread_id, item.enqueuing_callstack); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 530 | return_thread_sp->SetExtendedBacktraceToken(item.item_that_enqueued_this); |
| 531 | return_thread_sp->SetQueueName(item.enqueuing_queue_label.c_str()); |
| 532 | return_thread_sp->SetQueueID(item.enqueuing_queue_serialnum); |
| 533 | // return_thread_sp->SetThreadName |
| 534 | // (item.enqueuing_thread_label.c_str()); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 535 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 536 | m_page_to_free = ret.item_buffer_ptr; |
| 537 | m_page_to_free_size = ret.item_buffer_size; |
| 538 | } |
| 539 | } |
| 540 | return return_thread_sp; |
| 541 | } |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 542 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 543 | ThreadSP |
| 544 | SystemRuntimeMacOSX::GetExtendedBacktraceForQueueItem(QueueItemSP queue_item_sp, |
| 545 | ConstString type) { |
| 546 | ThreadSP extended_thread_sp; |
Raphael Isemann | 05cfdb0 | 2019-04-26 07:21:36 +0000 | [diff] [blame] | 547 | if (type != "libdispatch") |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 548 | return extended_thread_sp; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 550 | extended_thread_sp = std::make_shared<HistoryThread>( |
| 551 | *m_process, queue_item_sp->GetEnqueueingThreadID(), |
Alex Langford | 86df61c | 2019-06-19 21:33:44 +0000 | [diff] [blame] | 552 | queue_item_sp->GetEnqueueingBacktrace()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 553 | extended_thread_sp->SetExtendedBacktraceToken( |
| 554 | queue_item_sp->GetItemThatEnqueuedThis()); |
| 555 | extended_thread_sp->SetQueueName(queue_item_sp->GetQueueLabel().c_str()); |
| 556 | extended_thread_sp->SetQueueID(queue_item_sp->GetEnqueueingQueueID()); |
| 557 | // extended_thread_sp->SetThreadName |
| 558 | // (queue_item_sp->GetThreadLabel().c_str()); |
| 559 | |
| 560 | return extended_thread_sp; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* Returns true if we were able to get the version / offset information |
| 564 | * out of libBacktraceRecording. false means we were unable to retrieve |
| 565 | * this; the queue_info_version field will be 0. |
| 566 | */ |
| 567 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 568 | bool SystemRuntimeMacOSX::BacktraceRecordingHeadersInitialized() { |
| 569 | if (m_lib_backtrace_recording_info.queue_info_version != 0) |
| 570 | return true; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 571 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 572 | addr_t queue_info_version_address = LLDB_INVALID_ADDRESS; |
| 573 | addr_t queue_info_data_offset_address = LLDB_INVALID_ADDRESS; |
| 574 | addr_t item_info_version_address = LLDB_INVALID_ADDRESS; |
| 575 | addr_t item_info_data_offset_address = LLDB_INVALID_ADDRESS; |
| 576 | Target &target = m_process->GetTarget(); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 577 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 578 | static ConstString introspection_dispatch_queue_info_version( |
| 579 | "__introspection_dispatch_queue_info_version"); |
| 580 | SymbolContextList sc_list; |
Adrian Prantl | 1ad655e | 2019-10-17 19:56:40 +0000 | [diff] [blame] | 581 | m_process->GetTarget().GetImages().FindSymbolsWithNameAndType( |
| 582 | introspection_dispatch_queue_info_version, eSymbolTypeData, sc_list); |
| 583 | if (!sc_list.IsEmpty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 584 | SymbolContext sc; |
| 585 | sc_list.GetContextAtIndex(0, sc); |
| 586 | AddressRange addr_range; |
| 587 | sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range); |
| 588 | queue_info_version_address = |
| 589 | addr_range.GetBaseAddress().GetLoadAddress(&target); |
| 590 | } |
| 591 | sc_list.Clear(); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 592 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 593 | static ConstString introspection_dispatch_queue_info_data_offset( |
| 594 | "__introspection_dispatch_queue_info_data_offset"); |
Adrian Prantl | 1ad655e | 2019-10-17 19:56:40 +0000 | [diff] [blame] | 595 | m_process->GetTarget().GetImages().FindSymbolsWithNameAndType( |
| 596 | introspection_dispatch_queue_info_data_offset, eSymbolTypeData, sc_list); |
| 597 | if (!sc_list.IsEmpty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 598 | SymbolContext sc; |
| 599 | sc_list.GetContextAtIndex(0, sc); |
| 600 | AddressRange addr_range; |
| 601 | sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range); |
| 602 | queue_info_data_offset_address = |
| 603 | addr_range.GetBaseAddress().GetLoadAddress(&target); |
| 604 | } |
| 605 | sc_list.Clear(); |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 606 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 607 | static ConstString introspection_dispatch_item_info_version( |
| 608 | "__introspection_dispatch_item_info_version"); |
Adrian Prantl | 1ad655e | 2019-10-17 19:56:40 +0000 | [diff] [blame] | 609 | m_process->GetTarget().GetImages().FindSymbolsWithNameAndType( |
| 610 | introspection_dispatch_item_info_version, eSymbolTypeData, sc_list); |
| 611 | if (!sc_list.IsEmpty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 612 | SymbolContext sc; |
| 613 | sc_list.GetContextAtIndex(0, sc); |
| 614 | AddressRange addr_range; |
| 615 | sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range); |
| 616 | item_info_version_address = |
| 617 | addr_range.GetBaseAddress().GetLoadAddress(&target); |
| 618 | } |
| 619 | sc_list.Clear(); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 620 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 621 | static ConstString introspection_dispatch_item_info_data_offset( |
| 622 | "__introspection_dispatch_item_info_data_offset"); |
Adrian Prantl | 1ad655e | 2019-10-17 19:56:40 +0000 | [diff] [blame] | 623 | m_process->GetTarget().GetImages().FindSymbolsWithNameAndType( |
| 624 | introspection_dispatch_item_info_data_offset, eSymbolTypeData, sc_list); |
| 625 | if (!sc_list.IsEmpty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 626 | SymbolContext sc; |
| 627 | sc_list.GetContextAtIndex(0, sc); |
| 628 | AddressRange addr_range; |
| 629 | sc.GetAddressRange(eSymbolContextSymbol, 0, false, addr_range); |
| 630 | item_info_data_offset_address = |
| 631 | addr_range.GetBaseAddress().GetLoadAddress(&target); |
| 632 | } |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 633 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 634 | if (queue_info_version_address != LLDB_INVALID_ADDRESS && |
| 635 | queue_info_data_offset_address != LLDB_INVALID_ADDRESS && |
| 636 | item_info_version_address != LLDB_INVALID_ADDRESS && |
| 637 | item_info_data_offset_address != LLDB_INVALID_ADDRESS) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 638 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 639 | m_lib_backtrace_recording_info.queue_info_version = |
| 640 | m_process->ReadUnsignedIntegerFromMemory(queue_info_version_address, 2, |
| 641 | 0, error); |
| 642 | if (error.Success()) { |
| 643 | m_lib_backtrace_recording_info.queue_info_data_offset = |
| 644 | m_process->ReadUnsignedIntegerFromMemory( |
| 645 | queue_info_data_offset_address, 2, 0, error); |
| 646 | if (error.Success()) { |
| 647 | m_lib_backtrace_recording_info.item_info_version = |
| 648 | m_process->ReadUnsignedIntegerFromMemory(item_info_version_address, |
| 649 | 2, 0, error); |
| 650 | if (error.Success()) { |
| 651 | m_lib_backtrace_recording_info.item_info_data_offset = |
| 652 | m_process->ReadUnsignedIntegerFromMemory( |
| 653 | item_info_data_offset_address, 2, 0, error); |
| 654 | if (!error.Success()) { |
| 655 | m_lib_backtrace_recording_info.queue_info_version = 0; |
| 656 | } |
| 657 | } else { |
| 658 | m_lib_backtrace_recording_info.queue_info_version = 0; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 659 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 660 | } else { |
| 661 | m_lib_backtrace_recording_info.queue_info_version = 0; |
| 662 | } |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 663 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 664 | } |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 665 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 666 | return m_lib_backtrace_recording_info.queue_info_version != 0; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | const std::vector<ConstString> & |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 670 | SystemRuntimeMacOSX::GetExtendedBacktraceTypes() { |
| 671 | if (m_types.size() == 0) { |
| 672 | m_types.push_back(ConstString("libdispatch")); |
| 673 | // We could have pthread as another type in the future if we have a way of |
| 674 | // gathering that information & it's useful to distinguish between them. |
| 675 | } |
| 676 | return m_types; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 679 | void SystemRuntimeMacOSX::PopulateQueueList( |
| 680 | lldb_private::QueueList &queue_list) { |
| 681 | if (BacktraceRecordingHeadersInitialized()) { |
| 682 | AppleGetQueuesHandler::GetQueuesReturnInfo queue_info_pointer; |
| 683 | ThreadSP cur_thread_sp( |
| 684 | m_process->GetThreadList().GetExpressionExecutionThread()); |
| 685 | if (cur_thread_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 686 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 687 | queue_info_pointer = m_get_queues_handler.GetCurrentQueues( |
| 688 | *cur_thread_sp.get(), m_page_to_free, m_page_to_free_size, error); |
| 689 | m_page_to_free = LLDB_INVALID_ADDRESS; |
| 690 | m_page_to_free_size = 0; |
| 691 | if (error.Success()) { |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 692 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 693 | if (queue_info_pointer.count > 0 && |
| 694 | queue_info_pointer.queues_buffer_size > 0 && |
| 695 | queue_info_pointer.queues_buffer_ptr != 0 && |
| 696 | queue_info_pointer.queues_buffer_ptr != LLDB_INVALID_ADDRESS) { |
| 697 | PopulateQueuesUsingLibBTR(queue_info_pointer.queues_buffer_ptr, |
| 698 | queue_info_pointer.queues_buffer_size, |
| 699 | queue_info_pointer.count, queue_list); |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 700 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 701 | } |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 702 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 703 | } |
Jason Molenda | b9ffa98 | 2014-04-25 00:01:15 +0000 | [diff] [blame] | 704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 705 | // We either didn't have libBacktraceRecording (and need to create the queues |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 706 | // list based on threads) or we did get the queues list from |
| 707 | // libBacktraceRecording but some special queues may not be included in its |
| 708 | // information. This is needed because libBacktraceRecording will only list |
| 709 | // queues with pending or running items by default - but the magic com.apple |
| 710 | // .main-thread queue on thread 1 is always around. |
Jason Molenda | b9ffa98 | 2014-04-25 00:01:15 +0000 | [diff] [blame] | 711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 712 | for (ThreadSP thread_sp : m_process->Threads()) { |
| 713 | if (thread_sp->GetAssociatedWithLibdispatchQueue() != eLazyBoolNo) { |
| 714 | if (thread_sp->GetQueueID() != LLDB_INVALID_QUEUE_ID) { |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 715 | if (queue_list.FindQueueByID(thread_sp->GetQueueID()).get() == |
| 716 | nullptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 717 | QueueSP queue_sp(new Queue(m_process->shared_from_this(), |
| 718 | thread_sp->GetQueueID(), |
| 719 | thread_sp->GetQueueName())); |
| 720 | if (thread_sp->ThreadHasQueueInformation()) { |
| 721 | queue_sp->SetKind(thread_sp->GetQueueKind()); |
| 722 | queue_sp->SetLibdispatchQueueAddress( |
| 723 | thread_sp->GetQueueLibdispatchQueueAddress()); |
| 724 | queue_list.AddQueue(queue_sp); |
| 725 | } else { |
| 726 | queue_sp->SetKind( |
| 727 | GetQueueKind(thread_sp->GetQueueLibdispatchQueueAddress())); |
| 728 | queue_sp->SetLibdispatchQueueAddress( |
| 729 | thread_sp->GetQueueLibdispatchQueueAddress()); |
| 730 | queue_list.AddQueue(queue_sp); |
| 731 | } |
Jason Molenda | b9ffa98 | 2014-04-25 00:01:15 +0000 | [diff] [blame] | 732 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 733 | } |
Jason Molenda | b9ffa98 | 2014-04-25 00:01:15 +0000 | [diff] [blame] | 734 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 735 | } |
Jason Molenda | 5e8dce4 | 2013-12-13 00:29:16 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 738 | // Returns either an array of introspection_dispatch_item_info_ref's for the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 739 | // pending items on a queue or an array introspection_dispatch_item_info_ref's |
| 740 | // and code addresses for the pending items on a queue. The information about |
| 741 | // each of these pending items then needs to be fetched individually by passing |
| 742 | // the ref to libBacktraceRecording. |
Jason Molenda | 37e9b5a | 2014-03-09 21:17:08 +0000 | [diff] [blame] | 743 | |
| 744 | SystemRuntimeMacOSX::PendingItemsForQueue |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 745 | SystemRuntimeMacOSX::GetPendingItemRefsForQueue(lldb::addr_t queue) { |
| 746 | PendingItemsForQueue pending_item_refs; |
| 747 | AppleGetPendingItemsHandler::GetPendingItemsReturnInfo pending_items_pointer; |
| 748 | ThreadSP cur_thread_sp( |
| 749 | m_process->GetThreadList().GetExpressionExecutionThread()); |
| 750 | if (cur_thread_sp) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 751 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 752 | pending_items_pointer = m_get_pending_items_handler.GetPendingItems( |
| 753 | *cur_thread_sp.get(), queue, m_page_to_free, m_page_to_free_size, |
| 754 | error); |
Jason Molenda | e32cd19 | 2014-03-10 08:42:03 +0000 | [diff] [blame] | 755 | m_page_to_free = LLDB_INVALID_ADDRESS; |
| 756 | m_page_to_free_size = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 757 | if (error.Success()) { |
| 758 | if (pending_items_pointer.count > 0 && |
| 759 | pending_items_pointer.items_buffer_size > 0 && |
| 760 | pending_items_pointer.items_buffer_ptr != 0 && |
| 761 | pending_items_pointer.items_buffer_ptr != LLDB_INVALID_ADDRESS) { |
| 762 | DataBufferHeap data(pending_items_pointer.items_buffer_size, 0); |
| 763 | if (m_process->ReadMemory( |
| 764 | pending_items_pointer.items_buffer_ptr, data.GetBytes(), |
| 765 | pending_items_pointer.items_buffer_size, error)) { |
| 766 | DataExtractor extractor(data.GetBytes(), data.GetByteSize(), |
| 767 | m_process->GetByteOrder(), |
| 768 | m_process->GetAddressByteSize()); |
| 769 | |
| 770 | // We either have an array of |
| 771 | // void* item_ref |
| 772 | // (old style) or we have a structure returned which looks like |
| 773 | // |
| 774 | // struct introspection_dispatch_pending_item_info_s { |
| 775 | // void *item_ref; |
| 776 | // void *function_or_block; |
| 777 | // }; |
| 778 | // |
| 779 | // struct introspection_dispatch_pending_items_array_s { |
| 780 | // uint32_t version; |
| 781 | // uint32_t size_of_item_info; |
| 782 | // introspection_dispatch_pending_item_info_s items[]; |
| 783 | // } |
| 784 | |
| 785 | offset_t offset = 0; |
| 786 | int i = 0; |
| 787 | uint32_t version = extractor.GetU32(&offset); |
| 788 | if (version == 1) { |
| 789 | pending_item_refs.new_style = true; |
| 790 | uint32_t item_size = extractor.GetU32(&offset); |
| 791 | uint32_t start_of_array_offset = offset; |
| 792 | while (offset < pending_items_pointer.items_buffer_size && |
| 793 | static_cast<size_t>(i) < pending_items_pointer.count) { |
| 794 | offset = start_of_array_offset + (i * item_size); |
| 795 | ItemRefAndCodeAddress item; |
| 796 | item.item_ref = extractor.GetPointer(&offset); |
| 797 | item.code_address = extractor.GetPointer(&offset); |
| 798 | pending_item_refs.item_refs_and_code_addresses.push_back(item); |
| 799 | i++; |
| 800 | } |
| 801 | } else { |
| 802 | offset = 0; |
| 803 | pending_item_refs.new_style = false; |
| 804 | while (offset < pending_items_pointer.items_buffer_size && |
| 805 | static_cast<size_t>(i) < pending_items_pointer.count) { |
| 806 | ItemRefAndCodeAddress item; |
| 807 | item.item_ref = extractor.GetPointer(&offset); |
| 808 | item.code_address = LLDB_INVALID_ADDRESS; |
| 809 | pending_item_refs.item_refs_and_code_addresses.push_back(item); |
| 810 | i++; |
| 811 | } |
| 812 | } |
Jason Molenda | e32cd19 | 2014-03-10 08:42:03 +0000 | [diff] [blame] | 813 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 814 | m_page_to_free = pending_items_pointer.items_buffer_ptr; |
| 815 | m_page_to_free_size = pending_items_pointer.items_buffer_size; |
| 816 | } |
Jason Molenda | e32cd19 | 2014-03-10 08:42:03 +0000 | [diff] [blame] | 817 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 818 | } |
| 819 | return pending_item_refs; |
Jason Molenda | e32cd19 | 2014-03-10 08:42:03 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 822 | void SystemRuntimeMacOSX::PopulatePendingItemsForQueue(Queue *queue) { |
| 823 | if (BacktraceRecordingHeadersInitialized()) { |
| 824 | PendingItemsForQueue pending_item_refs = |
| 825 | GetPendingItemRefsForQueue(queue->GetLibdispatchQueueAddress()); |
| 826 | for (ItemRefAndCodeAddress pending_item : |
| 827 | pending_item_refs.item_refs_and_code_addresses) { |
| 828 | Address addr; |
| 829 | m_process->GetTarget().ResolveLoadAddress(pending_item.code_address, |
| 830 | addr); |
| 831 | QueueItemSP queue_item_sp(new QueueItem(queue->shared_from_this(), |
| 832 | m_process->shared_from_this(), |
| 833 | pending_item.item_ref, addr)); |
| 834 | queue->PushPendingQueueItem(queue_item_sp); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 835 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 836 | } |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 839 | void SystemRuntimeMacOSX::CompleteQueueItem(QueueItem *queue_item, |
| 840 | addr_t item_ref) { |
| 841 | AppleGetItemInfoHandler::GetItemInfoReturnInfo ret; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 842 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 843 | ThreadSP cur_thread_sp( |
| 844 | m_process->GetThreadList().GetExpressionExecutionThread()); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 845 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 846 | ret = m_get_item_info_handler.GetItemInfo(*cur_thread_sp.get(), item_ref, |
| 847 | m_page_to_free, m_page_to_free_size, |
| 848 | error); |
| 849 | m_page_to_free = LLDB_INVALID_ADDRESS; |
| 850 | m_page_to_free_size = 0; |
| 851 | if (ret.item_buffer_ptr != 0 && ret.item_buffer_ptr != LLDB_INVALID_ADDRESS && |
| 852 | ret.item_buffer_size > 0) { |
| 853 | DataBufferHeap data(ret.item_buffer_size, 0); |
| 854 | if (m_process->ReadMemory(ret.item_buffer_ptr, data.GetBytes(), |
| 855 | ret.item_buffer_size, error) && |
| 856 | error.Success()) { |
| 857 | DataExtractor extractor(data.GetBytes(), data.GetByteSize(), |
| 858 | m_process->GetByteOrder(), |
| 859 | m_process->GetAddressByteSize()); |
| 860 | ItemInfo item = ExtractItemInfoFromBuffer(extractor); |
| 861 | queue_item->SetItemThatEnqueuedThis(item.item_that_enqueued_this); |
| 862 | queue_item->SetEnqueueingThreadID(item.enqueuing_thread_id); |
| 863 | queue_item->SetEnqueueingQueueID(item.enqueuing_queue_serialnum); |
| 864 | queue_item->SetStopID(item.stop_id); |
| 865 | queue_item->SetEnqueueingBacktrace(item.enqueuing_callstack); |
| 866 | queue_item->SetThreadLabel(item.enqueuing_thread_label); |
| 867 | queue_item->SetQueueLabel(item.enqueuing_queue_label); |
| 868 | queue_item->SetTargetQueueLabel(item.target_queue_label); |
| 869 | } |
| 870 | m_page_to_free = ret.item_buffer_ptr; |
| 871 | m_page_to_free_size = ret.item_buffer_size; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | void SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR( |
| 876 | lldb::addr_t queues_buffer, uint64_t queues_buffer_size, uint64_t count, |
| 877 | lldb_private::QueueList &queue_list) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 878 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 879 | DataBufferHeap data(queues_buffer_size, 0); |
| 880 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYSTEM_RUNTIME)); |
| 881 | if (m_process->ReadMemory(queues_buffer, data.GetBytes(), queues_buffer_size, |
| 882 | error) == queues_buffer_size && |
| 883 | error.Success()) { |
| 884 | // We've read the information out of inferior memory; free it on the next |
| 885 | // call we make |
| 886 | m_page_to_free = queues_buffer; |
| 887 | m_page_to_free_size = queues_buffer_size; |
| 888 | |
| 889 | DataExtractor extractor(data.GetBytes(), data.GetByteSize(), |
| 890 | m_process->GetByteOrder(), |
| 891 | m_process->GetAddressByteSize()); |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 892 | offset_t offset = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 893 | uint64_t queues_read = 0; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 894 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 895 | // The information about the queues is stored in this format (v1): typedef |
| 896 | // struct introspection_dispatch_queue_info_s { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 897 | // uint32_t offset_to_next; |
| 898 | // dispatch_queue_t queue; |
| 899 | // uint64_t serialnum; // queue's serialnum in the process, as |
| 900 | // provided by libdispatch |
| 901 | // uint32_t running_work_items_count; |
| 902 | // uint32_t pending_work_items_count; |
| 903 | // |
| 904 | // char data[]; // Starting here, we have variable-length data: |
| 905 | // // char queue_label[]; |
| 906 | // } introspection_dispatch_queue_info_s; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 907 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 908 | while (queues_read < count && offset < queues_buffer_size) { |
| 909 | offset_t start_of_this_item = offset; |
| 910 | |
| 911 | uint32_t offset_to_next = extractor.GetU32(&offset); |
| 912 | |
| 913 | offset += 4; // Skip over the 4 bytes of reserved space |
| 914 | addr_t queue = extractor.GetPointer(&offset); |
| 915 | uint64_t serialnum = extractor.GetU64(&offset); |
| 916 | uint32_t running_work_items_count = extractor.GetU32(&offset); |
| 917 | uint32_t pending_work_items_count = extractor.GetU32(&offset); |
| 918 | |
| 919 | // Read the first field of the variable length data |
| 920 | offset = start_of_this_item + |
| 921 | m_lib_backtrace_recording_info.queue_info_data_offset; |
| 922 | const char *queue_label = extractor.GetCStr(&offset); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 923 | if (queue_label == nullptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 924 | queue_label = ""; |
| 925 | |
| 926 | offset_t start_of_next_item = start_of_this_item + offset_to_next; |
| 927 | offset = start_of_next_item; |
| 928 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 929 | LLDB_LOGF(log, |
| 930 | "SystemRuntimeMacOSX::PopulateQueuesUsingLibBTR added " |
| 931 | "queue with dispatch_queue_t 0x%" PRIx64 |
| 932 | ", serial number 0x%" PRIx64 |
| 933 | ", running items %d, pending items %d, name '%s'", |
| 934 | queue, serialnum, running_work_items_count, |
| 935 | pending_work_items_count, queue_label); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 936 | |
| 937 | QueueSP queue_sp( |
| 938 | new Queue(m_process->shared_from_this(), serialnum, queue_label)); |
| 939 | queue_sp->SetNumRunningWorkItems(running_work_items_count); |
| 940 | queue_sp->SetNumPendingWorkItems(pending_work_items_count); |
| 941 | queue_sp->SetLibdispatchQueueAddress(queue); |
| 942 | queue_sp->SetKind(GetQueueKind(queue)); |
| 943 | queue_list.AddQueue(queue_sp); |
| 944 | queues_read++; |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 945 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 946 | } |
Jason Molenda | 2fd8335 | 2014-02-05 05:44:54 +0000 | [diff] [blame] | 947 | } |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 948 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 949 | SystemRuntimeMacOSX::ItemInfo SystemRuntimeMacOSX::ExtractItemInfoFromBuffer( |
| 950 | lldb_private::DataExtractor &extractor) { |
| 951 | ItemInfo item; |
| 952 | |
| 953 | offset_t offset = 0; |
| 954 | |
| 955 | item.item_that_enqueued_this = extractor.GetPointer(&offset); |
| 956 | item.function_or_block = extractor.GetPointer(&offset); |
| 957 | item.enqueuing_thread_id = extractor.GetU64(&offset); |
| 958 | item.enqueuing_queue_serialnum = extractor.GetU64(&offset); |
| 959 | item.target_queue_serialnum = extractor.GetU64(&offset); |
| 960 | item.enqueuing_callstack_frame_count = extractor.GetU32(&offset); |
| 961 | item.stop_id = extractor.GetU32(&offset); |
| 962 | |
| 963 | offset = m_lib_backtrace_recording_info.item_info_data_offset; |
| 964 | |
| 965 | for (uint32_t i = 0; i < item.enqueuing_callstack_frame_count; i++) { |
| 966 | item.enqueuing_callstack.push_back(extractor.GetPointer(&offset)); |
| 967 | } |
| 968 | item.enqueuing_thread_label = extractor.GetCStr(&offset); |
| 969 | item.enqueuing_queue_label = extractor.GetCStr(&offset); |
| 970 | item.target_queue_label = extractor.GetCStr(&offset); |
| 971 | |
| 972 | return item; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 975 | void SystemRuntimeMacOSX::Initialize() { |
| 976 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 977 | GetPluginDescriptionStatic(), CreateInstance); |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 980 | void SystemRuntimeMacOSX::Terminate() { |
| 981 | PluginManager::UnregisterPlugin(CreateInstance); |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 984 | lldb_private::ConstString SystemRuntimeMacOSX::GetPluginNameStatic() { |
| 985 | static ConstString g_name("systemruntime-macosx"); |
| 986 | return g_name; |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 989 | const char *SystemRuntimeMacOSX::GetPluginDescriptionStatic() { |
| 990 | return "System runtime plugin for Mac OS X native libraries."; |
| 991 | } |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 992 | |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 993 | // PluginInterface protocol |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 994 | lldb_private::ConstString SystemRuntimeMacOSX::GetPluginName() { |
| 995 | return GetPluginNameStatic(); |
Jason Molenda | a7b5afa | 2013-11-15 00:17:32 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 998 | uint32_t SystemRuntimeMacOSX::GetPluginVersion() { return 1; } |