1e8d8bef9SDimitry Andric //===-- CommandObjectThreadUtil.cpp -----------------------------*- C++ -*-===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric 9e8d8bef9SDimitry Andric #include "CommandObjectThreadUtil.h" 10e8d8bef9SDimitry Andric 11e8d8bef9SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h" 12e8d8bef9SDimitry Andric #include "lldb/Target/Process.h" 13e8d8bef9SDimitry Andric #include "lldb/Target/Thread.h" 14e8d8bef9SDimitry Andric 15e8d8bef9SDimitry Andric using namespace lldb; 16e8d8bef9SDimitry Andric using namespace lldb_private; 17e8d8bef9SDimitry Andric using namespace llvm; 18e8d8bef9SDimitry Andric 19e8d8bef9SDimitry Andric CommandObjectIterateOverThreads::CommandObjectIterateOverThreads( 20e8d8bef9SDimitry Andric CommandInterpreter &interpreter, const char *name, const char *help, 21e8d8bef9SDimitry Andric const char *syntax, uint32_t flags) 2281ad6265SDimitry Andric : CommandObjectParsed(interpreter, name, help, syntax, flags) { 2381ad6265SDimitry Andric // These commands all take thread ID's as arguments. 24*0fca6ea1SDimitry Andric AddSimpleArgumentList(eArgTypeThreadIndex, eArgRepeatStar); 2581ad6265SDimitry Andric } 2681ad6265SDimitry Andric 2781ad6265SDimitry Andric CommandObjectMultipleThreads::CommandObjectMultipleThreads( 2881ad6265SDimitry Andric CommandInterpreter &interpreter, const char *name, const char *help, 2981ad6265SDimitry Andric const char *syntax, uint32_t flags) 3081ad6265SDimitry Andric : CommandObjectParsed(interpreter, name, help, syntax, flags) { 3181ad6265SDimitry Andric // These commands all take thread ID's as arguments. 32*0fca6ea1SDimitry Andric AddSimpleArgumentList(eArgTypeThreadIndex, eArgRepeatStar); 3381ad6265SDimitry Andric } 34e8d8bef9SDimitry Andric 355f757f3fSDimitry Andric void CommandObjectIterateOverThreads::DoExecute(Args &command, 36e8d8bef9SDimitry Andric CommandReturnObject &result) { 37e8d8bef9SDimitry Andric result.SetStatus(m_success_return); 38e8d8bef9SDimitry Andric 39e8d8bef9SDimitry Andric bool all_threads = false; 40e8d8bef9SDimitry Andric if (command.GetArgumentCount() == 0) { 41e8d8bef9SDimitry Andric Thread *thread = m_exe_ctx.GetThreadPtr(); 425f757f3fSDimitry Andric if (thread) 435f757f3fSDimitry Andric HandleOneThread(thread->GetID(), result); 445f757f3fSDimitry Andric return; 45e8d8bef9SDimitry Andric } else if (command.GetArgumentCount() == 1) { 46e8d8bef9SDimitry Andric all_threads = ::strcmp(command.GetArgumentAtIndex(0), "all") == 0; 47e8d8bef9SDimitry Andric m_unique_stacks = ::strcmp(command.GetArgumentAtIndex(0), "unique") == 0; 48e8d8bef9SDimitry Andric } 49e8d8bef9SDimitry Andric 50e8d8bef9SDimitry Andric // Use tids instead of ThreadSPs to prevent deadlocking problems which 51e8d8bef9SDimitry Andric // result from JIT-ing code while iterating over the (locked) ThreadSP 52e8d8bef9SDimitry Andric // list. 53e8d8bef9SDimitry Andric std::vector<lldb::tid_t> tids; 54e8d8bef9SDimitry Andric 55e8d8bef9SDimitry Andric if (all_threads || m_unique_stacks) { 56e8d8bef9SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 57e8d8bef9SDimitry Andric 58e8d8bef9SDimitry Andric for (ThreadSP thread_sp : process->Threads()) 59e8d8bef9SDimitry Andric tids.push_back(thread_sp->GetID()); 60e8d8bef9SDimitry Andric } else { 61e8d8bef9SDimitry Andric const size_t num_args = command.GetArgumentCount(); 62e8d8bef9SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 63e8d8bef9SDimitry Andric 64e8d8bef9SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 65e8d8bef9SDimitry Andric process->GetThreadList().GetMutex()); 66e8d8bef9SDimitry Andric 67e8d8bef9SDimitry Andric for (size_t i = 0; i < num_args; i++) { 68e8d8bef9SDimitry Andric uint32_t thread_idx; 69e8d8bef9SDimitry Andric if (!llvm::to_integer(command.GetArgumentAtIndex(i), thread_idx)) { 70e8d8bef9SDimitry Andric result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n", 71e8d8bef9SDimitry Andric command.GetArgumentAtIndex(i)); 725f757f3fSDimitry Andric return; 73e8d8bef9SDimitry Andric } 74e8d8bef9SDimitry Andric 75e8d8bef9SDimitry Andric ThreadSP thread = 76e8d8bef9SDimitry Andric process->GetThreadList().FindThreadByIndexID(thread_idx); 77e8d8bef9SDimitry Andric 78e8d8bef9SDimitry Andric if (!thread) { 79e8d8bef9SDimitry Andric result.AppendErrorWithFormat("no thread with index: \"%s\"\n", 80e8d8bef9SDimitry Andric command.GetArgumentAtIndex(i)); 815f757f3fSDimitry Andric return; 82e8d8bef9SDimitry Andric } 83e8d8bef9SDimitry Andric 84e8d8bef9SDimitry Andric tids.push_back(thread->GetID()); 85e8d8bef9SDimitry Andric } 86e8d8bef9SDimitry Andric } 87e8d8bef9SDimitry Andric 88e8d8bef9SDimitry Andric if (m_unique_stacks) { 89e8d8bef9SDimitry Andric // Iterate over threads, finding unique stack buckets. 90e8d8bef9SDimitry Andric std::set<UniqueStack> unique_stacks; 91e8d8bef9SDimitry Andric for (const lldb::tid_t &tid : tids) { 92e8d8bef9SDimitry Andric if (!BucketThread(tid, unique_stacks, result)) { 935f757f3fSDimitry Andric return; 94e8d8bef9SDimitry Andric } 95e8d8bef9SDimitry Andric } 96e8d8bef9SDimitry Andric 97e8d8bef9SDimitry Andric // Write the thread id's and unique call stacks to the output stream 98e8d8bef9SDimitry Andric Stream &strm = result.GetOutputStream(); 99e8d8bef9SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 100e8d8bef9SDimitry Andric for (const UniqueStack &stack : unique_stacks) { 101e8d8bef9SDimitry Andric // List the common thread ID's 102e8d8bef9SDimitry Andric const std::vector<uint32_t> &thread_index_ids = 103e8d8bef9SDimitry Andric stack.GetUniqueThreadIndexIDs(); 104e8d8bef9SDimitry Andric strm.Format("{0} thread(s) ", thread_index_ids.size()); 105e8d8bef9SDimitry Andric for (const uint32_t &thread_index_id : thread_index_ids) { 106e8d8bef9SDimitry Andric strm.Format("#{0} ", thread_index_id); 107e8d8bef9SDimitry Andric } 108e8d8bef9SDimitry Andric strm.EOL(); 109e8d8bef9SDimitry Andric 110e8d8bef9SDimitry Andric // List the shared call stack for this set of threads 111e8d8bef9SDimitry Andric uint32_t representative_thread_id = stack.GetRepresentativeThread(); 112e8d8bef9SDimitry Andric ThreadSP thread = process->GetThreadList().FindThreadByIndexID( 113e8d8bef9SDimitry Andric representative_thread_id); 114e8d8bef9SDimitry Andric if (!HandleOneThread(thread->GetID(), result)) { 1155f757f3fSDimitry Andric return; 116e8d8bef9SDimitry Andric } 117e8d8bef9SDimitry Andric } 118e8d8bef9SDimitry Andric } else { 119e8d8bef9SDimitry Andric uint32_t idx = 0; 120e8d8bef9SDimitry Andric for (const lldb::tid_t &tid : tids) { 121e8d8bef9SDimitry Andric if (idx != 0 && m_add_return) 122e8d8bef9SDimitry Andric result.AppendMessage(""); 123e8d8bef9SDimitry Andric 124e8d8bef9SDimitry Andric if (!HandleOneThread(tid, result)) 1255f757f3fSDimitry Andric return; 126e8d8bef9SDimitry Andric 127e8d8bef9SDimitry Andric ++idx; 128e8d8bef9SDimitry Andric } 129e8d8bef9SDimitry Andric } 130e8d8bef9SDimitry Andric } 131e8d8bef9SDimitry Andric 132e8d8bef9SDimitry Andric bool CommandObjectIterateOverThreads::BucketThread( 133e8d8bef9SDimitry Andric lldb::tid_t tid, std::set<UniqueStack> &unique_stacks, 134e8d8bef9SDimitry Andric CommandReturnObject &result) { 135e8d8bef9SDimitry Andric // Grab the corresponding thread for the given thread id. 136e8d8bef9SDimitry Andric Process *process = m_exe_ctx.GetProcessPtr(); 137e8d8bef9SDimitry Andric Thread *thread = process->GetThreadList().FindThreadByID(tid).get(); 138e8d8bef9SDimitry Andric if (thread == nullptr) { 139e8d8bef9SDimitry Andric result.AppendErrorWithFormatv("Failed to process thread #{0}.\n", tid); 140e8d8bef9SDimitry Andric return false; 141e8d8bef9SDimitry Andric } 142e8d8bef9SDimitry Andric 143e8d8bef9SDimitry Andric // Collect the each frame's address for this call-stack 144e8d8bef9SDimitry Andric std::stack<lldb::addr_t> stack_frames; 145e8d8bef9SDimitry Andric const uint32_t frame_count = thread->GetStackFrameCount(); 146e8d8bef9SDimitry Andric for (uint32_t frame_index = 0; frame_index < frame_count; frame_index++) { 147e8d8bef9SDimitry Andric const lldb::StackFrameSP frame_sp = 148e8d8bef9SDimitry Andric thread->GetStackFrameAtIndex(frame_index); 149e8d8bef9SDimitry Andric const lldb::addr_t pc = frame_sp->GetStackID().GetPC(); 150e8d8bef9SDimitry Andric stack_frames.push(pc); 151e8d8bef9SDimitry Andric } 152e8d8bef9SDimitry Andric 153e8d8bef9SDimitry Andric uint32_t thread_index_id = thread->GetIndexID(); 154e8d8bef9SDimitry Andric UniqueStack new_unique_stack(stack_frames, thread_index_id); 155e8d8bef9SDimitry Andric 156e8d8bef9SDimitry Andric // Try to match the threads stack to and existing entry. 157e8d8bef9SDimitry Andric std::set<UniqueStack>::iterator matching_stack = 158e8d8bef9SDimitry Andric unique_stacks.find(new_unique_stack); 159e8d8bef9SDimitry Andric if (matching_stack != unique_stacks.end()) { 160e8d8bef9SDimitry Andric matching_stack->AddThread(thread_index_id); 161e8d8bef9SDimitry Andric } else { 162e8d8bef9SDimitry Andric unique_stacks.insert(new_unique_stack); 163e8d8bef9SDimitry Andric } 164e8d8bef9SDimitry Andric return true; 165e8d8bef9SDimitry Andric } 166fe6060f1SDimitry Andric 1675f757f3fSDimitry Andric void CommandObjectMultipleThreads::DoExecute(Args &command, 168fe6060f1SDimitry Andric CommandReturnObject &result) { 169fe6060f1SDimitry Andric Process &process = m_exe_ctx.GetProcessRef(); 170fe6060f1SDimitry Andric 171fe6060f1SDimitry Andric std::vector<lldb::tid_t> tids; 172fe6060f1SDimitry Andric const size_t num_args = command.GetArgumentCount(); 173fe6060f1SDimitry Andric 174fe6060f1SDimitry Andric std::lock_guard<std::recursive_mutex> guard( 175fe6060f1SDimitry Andric process.GetThreadList().GetMutex()); 176fe6060f1SDimitry Andric 177fe6060f1SDimitry Andric if (num_args > 0 && ::strcmp(command.GetArgumentAtIndex(0), "all") == 0) { 178fe6060f1SDimitry Andric for (ThreadSP thread_sp : process.Threads()) 179fe6060f1SDimitry Andric tids.push_back(thread_sp->GetID()); 180fe6060f1SDimitry Andric } else { 181fe6060f1SDimitry Andric if (num_args == 0) { 182fe6060f1SDimitry Andric Thread &thread = m_exe_ctx.GetThreadRef(); 183fe6060f1SDimitry Andric tids.push_back(thread.GetID()); 184fe6060f1SDimitry Andric } 185fe6060f1SDimitry Andric 186fe6060f1SDimitry Andric for (size_t i = 0; i < num_args; i++) { 187fe6060f1SDimitry Andric uint32_t thread_idx; 188fe6060f1SDimitry Andric if (!llvm::to_integer(command.GetArgumentAtIndex(i), thread_idx)) { 189fe6060f1SDimitry Andric result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n", 190fe6060f1SDimitry Andric command.GetArgumentAtIndex(i)); 1915f757f3fSDimitry Andric return; 192fe6060f1SDimitry Andric } 193fe6060f1SDimitry Andric 194fe6060f1SDimitry Andric ThreadSP thread = process.GetThreadList().FindThreadByIndexID(thread_idx); 195fe6060f1SDimitry Andric 196fe6060f1SDimitry Andric if (!thread) { 197fe6060f1SDimitry Andric result.AppendErrorWithFormat("no thread with index: \"%s\"\n", 198fe6060f1SDimitry Andric command.GetArgumentAtIndex(i)); 1995f757f3fSDimitry Andric return; 200fe6060f1SDimitry Andric } 201fe6060f1SDimitry Andric 202fe6060f1SDimitry Andric tids.push_back(thread->GetID()); 203fe6060f1SDimitry Andric } 204fe6060f1SDimitry Andric } 205fe6060f1SDimitry Andric 2065f757f3fSDimitry Andric DoExecuteOnThreads(command, result, tids); 207fe6060f1SDimitry Andric } 208