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