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