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)
22e8d8bef9SDimitry Andric     : CommandObjectParsed(interpreter, name, help, syntax, flags) {}
23e8d8bef9SDimitry Andric 
24e8d8bef9SDimitry Andric bool CommandObjectIterateOverThreads::DoExecute(Args &command,
25e8d8bef9SDimitry Andric                                                 CommandReturnObject &result) {
26e8d8bef9SDimitry Andric   result.SetStatus(m_success_return);
27e8d8bef9SDimitry Andric 
28e8d8bef9SDimitry Andric   bool all_threads = false;
29e8d8bef9SDimitry Andric   if (command.GetArgumentCount() == 0) {
30e8d8bef9SDimitry Andric     Thread *thread = m_exe_ctx.GetThreadPtr();
31e8d8bef9SDimitry Andric     if (!thread || !HandleOneThread(thread->GetID(), result))
32e8d8bef9SDimitry Andric       return false;
33e8d8bef9SDimitry Andric     return result.Succeeded();
34e8d8bef9SDimitry Andric   } else if (command.GetArgumentCount() == 1) {
35e8d8bef9SDimitry Andric     all_threads = ::strcmp(command.GetArgumentAtIndex(0), "all") == 0;
36e8d8bef9SDimitry Andric     m_unique_stacks = ::strcmp(command.GetArgumentAtIndex(0), "unique") == 0;
37e8d8bef9SDimitry Andric   }
38e8d8bef9SDimitry Andric 
39e8d8bef9SDimitry Andric   // Use tids instead of ThreadSPs to prevent deadlocking problems which
40e8d8bef9SDimitry Andric   // result from JIT-ing code while iterating over the (locked) ThreadSP
41e8d8bef9SDimitry Andric   // list.
42e8d8bef9SDimitry Andric   std::vector<lldb::tid_t> tids;
43e8d8bef9SDimitry Andric 
44e8d8bef9SDimitry Andric   if (all_threads || m_unique_stacks) {
45e8d8bef9SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
46e8d8bef9SDimitry Andric 
47e8d8bef9SDimitry Andric     for (ThreadSP thread_sp : process->Threads())
48e8d8bef9SDimitry Andric       tids.push_back(thread_sp->GetID());
49e8d8bef9SDimitry Andric   } else {
50e8d8bef9SDimitry Andric     const size_t num_args = command.GetArgumentCount();
51e8d8bef9SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
52e8d8bef9SDimitry Andric 
53e8d8bef9SDimitry Andric     std::lock_guard<std::recursive_mutex> guard(
54e8d8bef9SDimitry Andric         process->GetThreadList().GetMutex());
55e8d8bef9SDimitry Andric 
56e8d8bef9SDimitry Andric     for (size_t i = 0; i < num_args; i++) {
57e8d8bef9SDimitry Andric       uint32_t thread_idx;
58e8d8bef9SDimitry Andric       if (!llvm::to_integer(command.GetArgumentAtIndex(i), thread_idx)) {
59e8d8bef9SDimitry Andric         result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n",
60e8d8bef9SDimitry Andric                                      command.GetArgumentAtIndex(i));
61e8d8bef9SDimitry Andric         result.SetStatus(eReturnStatusFailed);
62e8d8bef9SDimitry Andric         return false;
63e8d8bef9SDimitry Andric       }
64e8d8bef9SDimitry Andric 
65e8d8bef9SDimitry Andric       ThreadSP thread =
66e8d8bef9SDimitry Andric           process->GetThreadList().FindThreadByIndexID(thread_idx);
67e8d8bef9SDimitry Andric 
68e8d8bef9SDimitry Andric       if (!thread) {
69e8d8bef9SDimitry Andric         result.AppendErrorWithFormat("no thread with index: \"%s\"\n",
70e8d8bef9SDimitry Andric                                      command.GetArgumentAtIndex(i));
71e8d8bef9SDimitry Andric         result.SetStatus(eReturnStatusFailed);
72e8d8bef9SDimitry Andric         return false;
73e8d8bef9SDimitry Andric       }
74e8d8bef9SDimitry Andric 
75e8d8bef9SDimitry Andric       tids.push_back(thread->GetID());
76e8d8bef9SDimitry Andric     }
77e8d8bef9SDimitry Andric   }
78e8d8bef9SDimitry Andric 
79e8d8bef9SDimitry Andric   if (m_unique_stacks) {
80e8d8bef9SDimitry Andric     // Iterate over threads, finding unique stack buckets.
81e8d8bef9SDimitry Andric     std::set<UniqueStack> unique_stacks;
82e8d8bef9SDimitry Andric     for (const lldb::tid_t &tid : tids) {
83e8d8bef9SDimitry Andric       if (!BucketThread(tid, unique_stacks, result)) {
84e8d8bef9SDimitry Andric         return false;
85e8d8bef9SDimitry Andric       }
86e8d8bef9SDimitry Andric     }
87e8d8bef9SDimitry Andric 
88e8d8bef9SDimitry Andric     // Write the thread id's and unique call stacks to the output stream
89e8d8bef9SDimitry Andric     Stream &strm = result.GetOutputStream();
90e8d8bef9SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
91e8d8bef9SDimitry Andric     for (const UniqueStack &stack : unique_stacks) {
92e8d8bef9SDimitry Andric       // List the common thread ID's
93e8d8bef9SDimitry Andric       const std::vector<uint32_t> &thread_index_ids =
94e8d8bef9SDimitry Andric           stack.GetUniqueThreadIndexIDs();
95e8d8bef9SDimitry Andric       strm.Format("{0} thread(s) ", thread_index_ids.size());
96e8d8bef9SDimitry Andric       for (const uint32_t &thread_index_id : thread_index_ids) {
97e8d8bef9SDimitry Andric         strm.Format("#{0} ", thread_index_id);
98e8d8bef9SDimitry Andric       }
99e8d8bef9SDimitry Andric       strm.EOL();
100e8d8bef9SDimitry Andric 
101e8d8bef9SDimitry Andric       // List the shared call stack for this set of threads
102e8d8bef9SDimitry Andric       uint32_t representative_thread_id = stack.GetRepresentativeThread();
103e8d8bef9SDimitry Andric       ThreadSP thread = process->GetThreadList().FindThreadByIndexID(
104e8d8bef9SDimitry Andric           representative_thread_id);
105e8d8bef9SDimitry Andric       if (!HandleOneThread(thread->GetID(), result)) {
106e8d8bef9SDimitry Andric         return false;
107e8d8bef9SDimitry Andric       }
108e8d8bef9SDimitry Andric     }
109e8d8bef9SDimitry Andric   } else {
110e8d8bef9SDimitry Andric     uint32_t idx = 0;
111e8d8bef9SDimitry Andric     for (const lldb::tid_t &tid : tids) {
112e8d8bef9SDimitry Andric       if (idx != 0 && m_add_return)
113e8d8bef9SDimitry Andric         result.AppendMessage("");
114e8d8bef9SDimitry Andric 
115e8d8bef9SDimitry Andric       if (!HandleOneThread(tid, result))
116e8d8bef9SDimitry Andric         return false;
117e8d8bef9SDimitry Andric 
118e8d8bef9SDimitry Andric       ++idx;
119e8d8bef9SDimitry Andric     }
120e8d8bef9SDimitry Andric   }
121e8d8bef9SDimitry Andric   return result.Succeeded();
122e8d8bef9SDimitry Andric }
123e8d8bef9SDimitry Andric 
124e8d8bef9SDimitry Andric bool CommandObjectIterateOverThreads::BucketThread(
125e8d8bef9SDimitry Andric     lldb::tid_t tid, std::set<UniqueStack> &unique_stacks,
126e8d8bef9SDimitry Andric     CommandReturnObject &result) {
127e8d8bef9SDimitry Andric   // Grab the corresponding thread for the given thread id.
128e8d8bef9SDimitry Andric   Process *process = m_exe_ctx.GetProcessPtr();
129e8d8bef9SDimitry Andric   Thread *thread = process->GetThreadList().FindThreadByID(tid).get();
130e8d8bef9SDimitry Andric   if (thread == nullptr) {
131e8d8bef9SDimitry Andric     result.AppendErrorWithFormatv("Failed to process thread #{0}.\n", tid);
132e8d8bef9SDimitry Andric     result.SetStatus(eReturnStatusFailed);
133e8d8bef9SDimitry Andric     return false;
134e8d8bef9SDimitry Andric   }
135e8d8bef9SDimitry Andric 
136e8d8bef9SDimitry Andric   // Collect the each frame's address for this call-stack
137e8d8bef9SDimitry Andric   std::stack<lldb::addr_t> stack_frames;
138e8d8bef9SDimitry Andric   const uint32_t frame_count = thread->GetStackFrameCount();
139e8d8bef9SDimitry Andric   for (uint32_t frame_index = 0; frame_index < frame_count; frame_index++) {
140e8d8bef9SDimitry Andric     const lldb::StackFrameSP frame_sp =
141e8d8bef9SDimitry Andric         thread->GetStackFrameAtIndex(frame_index);
142e8d8bef9SDimitry Andric     const lldb::addr_t pc = frame_sp->GetStackID().GetPC();
143e8d8bef9SDimitry Andric     stack_frames.push(pc);
144e8d8bef9SDimitry Andric   }
145e8d8bef9SDimitry Andric 
146e8d8bef9SDimitry Andric   uint32_t thread_index_id = thread->GetIndexID();
147e8d8bef9SDimitry Andric   UniqueStack new_unique_stack(stack_frames, thread_index_id);
148e8d8bef9SDimitry Andric 
149e8d8bef9SDimitry Andric   // Try to match the threads stack to and existing entry.
150e8d8bef9SDimitry Andric   std::set<UniqueStack>::iterator matching_stack =
151e8d8bef9SDimitry Andric       unique_stacks.find(new_unique_stack);
152e8d8bef9SDimitry Andric   if (matching_stack != unique_stacks.end()) {
153e8d8bef9SDimitry Andric     matching_stack->AddThread(thread_index_id);
154e8d8bef9SDimitry Andric   } else {
155e8d8bef9SDimitry Andric     unique_stacks.insert(new_unique_stack);
156e8d8bef9SDimitry Andric   }
157e8d8bef9SDimitry Andric   return true;
158e8d8bef9SDimitry Andric }
159