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         return false;
62e8d8bef9SDimitry Andric       }
63e8d8bef9SDimitry Andric 
64e8d8bef9SDimitry Andric       ThreadSP thread =
65e8d8bef9SDimitry Andric           process->GetThreadList().FindThreadByIndexID(thread_idx);
66e8d8bef9SDimitry Andric 
67e8d8bef9SDimitry Andric       if (!thread) {
68e8d8bef9SDimitry Andric         result.AppendErrorWithFormat("no thread with index: \"%s\"\n",
69e8d8bef9SDimitry Andric                                      command.GetArgumentAtIndex(i));
70e8d8bef9SDimitry Andric         return false;
71e8d8bef9SDimitry Andric       }
72e8d8bef9SDimitry Andric 
73e8d8bef9SDimitry Andric       tids.push_back(thread->GetID());
74e8d8bef9SDimitry Andric     }
75e8d8bef9SDimitry Andric   }
76e8d8bef9SDimitry Andric 
77e8d8bef9SDimitry Andric   if (m_unique_stacks) {
78e8d8bef9SDimitry Andric     // Iterate over threads, finding unique stack buckets.
79e8d8bef9SDimitry Andric     std::set<UniqueStack> unique_stacks;
80e8d8bef9SDimitry Andric     for (const lldb::tid_t &tid : tids) {
81e8d8bef9SDimitry Andric       if (!BucketThread(tid, unique_stacks, result)) {
82e8d8bef9SDimitry Andric         return false;
83e8d8bef9SDimitry Andric       }
84e8d8bef9SDimitry Andric     }
85e8d8bef9SDimitry Andric 
86e8d8bef9SDimitry Andric     // Write the thread id's and unique call stacks to the output stream
87e8d8bef9SDimitry Andric     Stream &strm = result.GetOutputStream();
88e8d8bef9SDimitry Andric     Process *process = m_exe_ctx.GetProcessPtr();
89e8d8bef9SDimitry Andric     for (const UniqueStack &stack : unique_stacks) {
90e8d8bef9SDimitry Andric       // List the common thread ID's
91e8d8bef9SDimitry Andric       const std::vector<uint32_t> &thread_index_ids =
92e8d8bef9SDimitry Andric           stack.GetUniqueThreadIndexIDs();
93e8d8bef9SDimitry Andric       strm.Format("{0} thread(s) ", thread_index_ids.size());
94e8d8bef9SDimitry Andric       for (const uint32_t &thread_index_id : thread_index_ids) {
95e8d8bef9SDimitry Andric         strm.Format("#{0} ", thread_index_id);
96e8d8bef9SDimitry Andric       }
97e8d8bef9SDimitry Andric       strm.EOL();
98e8d8bef9SDimitry Andric 
99e8d8bef9SDimitry Andric       // List the shared call stack for this set of threads
100e8d8bef9SDimitry Andric       uint32_t representative_thread_id = stack.GetRepresentativeThread();
101e8d8bef9SDimitry Andric       ThreadSP thread = process->GetThreadList().FindThreadByIndexID(
102e8d8bef9SDimitry Andric           representative_thread_id);
103e8d8bef9SDimitry Andric       if (!HandleOneThread(thread->GetID(), result)) {
104e8d8bef9SDimitry Andric         return false;
105e8d8bef9SDimitry Andric       }
106e8d8bef9SDimitry Andric     }
107e8d8bef9SDimitry Andric   } else {
108e8d8bef9SDimitry Andric     uint32_t idx = 0;
109e8d8bef9SDimitry Andric     for (const lldb::tid_t &tid : tids) {
110e8d8bef9SDimitry Andric       if (idx != 0 && m_add_return)
111e8d8bef9SDimitry Andric         result.AppendMessage("");
112e8d8bef9SDimitry Andric 
113e8d8bef9SDimitry Andric       if (!HandleOneThread(tid, result))
114e8d8bef9SDimitry Andric         return false;
115e8d8bef9SDimitry Andric 
116e8d8bef9SDimitry Andric       ++idx;
117e8d8bef9SDimitry Andric     }
118e8d8bef9SDimitry Andric   }
119e8d8bef9SDimitry Andric   return result.Succeeded();
120e8d8bef9SDimitry Andric }
121e8d8bef9SDimitry Andric 
122e8d8bef9SDimitry Andric bool CommandObjectIterateOverThreads::BucketThread(
123e8d8bef9SDimitry Andric     lldb::tid_t tid, std::set<UniqueStack> &unique_stacks,
124e8d8bef9SDimitry Andric     CommandReturnObject &result) {
125e8d8bef9SDimitry Andric   // Grab the corresponding thread for the given thread id.
126e8d8bef9SDimitry Andric   Process *process = m_exe_ctx.GetProcessPtr();
127e8d8bef9SDimitry Andric   Thread *thread = process->GetThreadList().FindThreadByID(tid).get();
128e8d8bef9SDimitry Andric   if (thread == nullptr) {
129e8d8bef9SDimitry Andric     result.AppendErrorWithFormatv("Failed to process thread #{0}.\n", tid);
130e8d8bef9SDimitry Andric     return false;
131e8d8bef9SDimitry Andric   }
132e8d8bef9SDimitry Andric 
133e8d8bef9SDimitry Andric   // Collect the each frame's address for this call-stack
134e8d8bef9SDimitry Andric   std::stack<lldb::addr_t> stack_frames;
135e8d8bef9SDimitry Andric   const uint32_t frame_count = thread->GetStackFrameCount();
136e8d8bef9SDimitry Andric   for (uint32_t frame_index = 0; frame_index < frame_count; frame_index++) {
137e8d8bef9SDimitry Andric     const lldb::StackFrameSP frame_sp =
138e8d8bef9SDimitry Andric         thread->GetStackFrameAtIndex(frame_index);
139e8d8bef9SDimitry Andric     const lldb::addr_t pc = frame_sp->GetStackID().GetPC();
140e8d8bef9SDimitry Andric     stack_frames.push(pc);
141e8d8bef9SDimitry Andric   }
142e8d8bef9SDimitry Andric 
143e8d8bef9SDimitry Andric   uint32_t thread_index_id = thread->GetIndexID();
144e8d8bef9SDimitry Andric   UniqueStack new_unique_stack(stack_frames, thread_index_id);
145e8d8bef9SDimitry Andric 
146e8d8bef9SDimitry Andric   // Try to match the threads stack to and existing entry.
147e8d8bef9SDimitry Andric   std::set<UniqueStack>::iterator matching_stack =
148e8d8bef9SDimitry Andric       unique_stacks.find(new_unique_stack);
149e8d8bef9SDimitry Andric   if (matching_stack != unique_stacks.end()) {
150e8d8bef9SDimitry Andric     matching_stack->AddThread(thread_index_id);
151e8d8bef9SDimitry Andric   } else {
152e8d8bef9SDimitry Andric     unique_stacks.insert(new_unique_stack);
153e8d8bef9SDimitry Andric   }
154e8d8bef9SDimitry Andric   return true;
155e8d8bef9SDimitry Andric }
156fe6060f1SDimitry Andric 
157fe6060f1SDimitry Andric bool CommandObjectMultipleThreads::DoExecute(Args &command,
158fe6060f1SDimitry Andric                                              CommandReturnObject &result) {
159fe6060f1SDimitry Andric   Process &process = m_exe_ctx.GetProcessRef();
160fe6060f1SDimitry Andric 
161fe6060f1SDimitry Andric   std::vector<lldb::tid_t> tids;
162fe6060f1SDimitry Andric   const size_t num_args = command.GetArgumentCount();
163fe6060f1SDimitry Andric 
164fe6060f1SDimitry Andric   std::lock_guard<std::recursive_mutex> guard(
165fe6060f1SDimitry Andric       process.GetThreadList().GetMutex());
166fe6060f1SDimitry Andric 
167fe6060f1SDimitry Andric   if (num_args > 0 && ::strcmp(command.GetArgumentAtIndex(0), "all") == 0) {
168fe6060f1SDimitry Andric     for (ThreadSP thread_sp : process.Threads())
169fe6060f1SDimitry Andric       tids.push_back(thread_sp->GetID());
170fe6060f1SDimitry Andric   } else {
171fe6060f1SDimitry Andric     if (num_args == 0) {
172fe6060f1SDimitry Andric       Thread &thread = m_exe_ctx.GetThreadRef();
173fe6060f1SDimitry Andric       tids.push_back(thread.GetID());
174fe6060f1SDimitry Andric     }
175fe6060f1SDimitry Andric 
176fe6060f1SDimitry Andric     for (size_t i = 0; i < num_args; i++) {
177fe6060f1SDimitry Andric       uint32_t thread_idx;
178fe6060f1SDimitry Andric       if (!llvm::to_integer(command.GetArgumentAtIndex(i), thread_idx)) {
179fe6060f1SDimitry Andric         result.AppendErrorWithFormat("invalid thread specification: \"%s\"\n",
180fe6060f1SDimitry Andric                                      command.GetArgumentAtIndex(i));
181fe6060f1SDimitry Andric         return false;
182fe6060f1SDimitry Andric       }
183fe6060f1SDimitry Andric 
184fe6060f1SDimitry Andric       ThreadSP thread = process.GetThreadList().FindThreadByIndexID(thread_idx);
185fe6060f1SDimitry Andric 
186fe6060f1SDimitry Andric       if (!thread) {
187fe6060f1SDimitry Andric         result.AppendErrorWithFormat("no thread with index: \"%s\"\n",
188fe6060f1SDimitry Andric                                      command.GetArgumentAtIndex(i));
189fe6060f1SDimitry Andric         return false;
190fe6060f1SDimitry Andric       }
191fe6060f1SDimitry Andric 
192fe6060f1SDimitry Andric       tids.push_back(thread->GetID());
193fe6060f1SDimitry Andric     }
194fe6060f1SDimitry Andric   }
195fe6060f1SDimitry Andric 
196fe6060f1SDimitry Andric   return DoExecuteOnThreads(command, result, tids);
197fe6060f1SDimitry Andric }
198