1 //===-- CommandObjectThreadUtil.h -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_COMMANDS_COMMANDOBJECTTHREADUTIL_H
10 #define LLDB_SOURCE_COMMANDS_COMMANDOBJECTTHREADUTIL_H
12 #include "lldb/Interpreter/CommandObjectMultiword.h"
14 namespace lldb_private
{
16 class CommandObjectIterateOverThreads
: public CommandObjectParsed
{
20 UniqueStack(std::stack
<lldb::addr_t
> stack_frames
, uint32_t thread_index_id
)
21 : m_stack_frames(stack_frames
) {
22 m_thread_index_ids
.push_back(thread_index_id
);
25 void AddThread(uint32_t thread_index_id
) const {
26 m_thread_index_ids
.push_back(thread_index_id
);
29 const std::vector
<uint32_t> &GetUniqueThreadIndexIDs() const {
30 return m_thread_index_ids
;
33 lldb::tid_t
GetRepresentativeThread() const {
34 return m_thread_index_ids
.front();
37 friend bool inline operator<(const UniqueStack
&lhs
,
38 const UniqueStack
&rhs
) {
39 return lhs
.m_stack_frames
< rhs
.m_stack_frames
;
43 // Mark the thread index as mutable, as we don't care about it from a const
44 // perspective, we only care about m_stack_frames so we keep our std::set
46 mutable std::vector
<uint32_t> m_thread_index_ids
;
47 std::stack
<lldb::addr_t
> m_stack_frames
;
51 CommandObjectIterateOverThreads(CommandInterpreter
&interpreter
,
52 const char *name
, const char *help
,
53 const char *syntax
, uint32_t flags
);
55 ~CommandObjectIterateOverThreads() override
= default;
57 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
;
60 // Override this to do whatever you need to do for one thread.
62 // If you return false, the iteration will stop, otherwise it will proceed.
63 // The result is set to m_success_return (defaults to
64 // eReturnStatusSuccessFinishResult) before the iteration, so you only need
65 // to set the return status in HandleOneThread if you want to indicate an
66 // error. If m_add_return is true, a blank line will be inserted between each
67 // of the listings (except the last one.)
69 virtual bool HandleOneThread(lldb::tid_t
, CommandReturnObject
&result
) = 0;
71 bool BucketThread(lldb::tid_t tid
, std::set
<UniqueStack
> &unique_stacks
,
72 CommandReturnObject
&result
);
74 lldb::ReturnStatus m_success_return
= lldb::eReturnStatusSuccessFinishResult
;
75 bool m_unique_stacks
= false;
76 bool m_add_return
= true;
79 /// Class similar to \a CommandObjectIterateOverThreads, but which performs
80 /// an action on multiple threads at once instead of iterating over each thread.
81 class CommandObjectMultipleThreads
: public CommandObjectParsed
{
83 CommandObjectMultipleThreads(CommandInterpreter
&interpreter
,
84 const char *name
, const char *help
,
85 const char *syntax
, uint32_t flags
);
87 void DoExecute(Args
&command
, CommandReturnObject
&result
) override
;
90 /// Method that handles the command after the main arguments have been parsed.
93 /// The thread ids passed as arguments.
96 /// A boolean result similar to the one expected from \a DoExecute.
97 virtual bool DoExecuteOnThreads(Args
&command
, CommandReturnObject
&result
,
98 llvm::ArrayRef
<lldb::tid_t
> tids
) = 0;
101 } // namespace lldb_private
103 #endif // LLDB_SOURCE_COMMANDS_COMMANDOBJECTTHREADUTIL_H