LLDB  mainline
CommandObjectThreadUtil.h
Go to the documentation of this file.
1 //===-- CommandObjectThreadUtil.h -------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_COMMANDS_COMMANDOBJECTTHREADUTIL_H
10 #define LLDB_SOURCE_COMMANDS_COMMANDOBJECTTHREADUTIL_H
11 
13 
14 namespace lldb_private {
15 
17 
18  class UniqueStack {
19  public:
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);
23  }
24 
25  void AddThread(uint32_t thread_index_id) const {
26  m_thread_index_ids.push_back(thread_index_id);
27  }
28 
29  const std::vector<uint32_t> &GetUniqueThreadIndexIDs() const {
30  return m_thread_index_ids;
31  }
32 
34  return m_thread_index_ids.front();
35  }
36 
37  friend bool inline operator<(const UniqueStack &lhs,
38  const UniqueStack &rhs) {
39  return lhs.m_stack_frames < rhs.m_stack_frames;
40  }
41 
42  protected:
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
45  // sorted.
46  mutable std::vector<uint32_t> m_thread_index_ids;
47  std::stack<lldb::addr_t> m_stack_frames;
48  };
49 
50 public:
52  const char *name, const char *help,
53  const char *syntax, uint32_t flags);
54 
55  ~CommandObjectIterateOverThreads() override = default;
56 
57  bool DoExecute(Args &command, CommandReturnObject &result) override;
58 
59 protected:
60  // Override this to do whatever you need to do for one thread.
61  //
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.)
68 
69  virtual bool HandleOneThread(lldb::tid_t, CommandReturnObject &result) = 0;
70 
71  bool BucketThread(lldb::tid_t tid, std::set<UniqueStack> &unique_stacks,
72  CommandReturnObject &result);
73 
75  bool m_unique_stacks = false;
76  bool m_add_return = true;
77 };
78 
79 /// Class similar to \a CommandObjectIterateOverThreads, but which performs
80 /// an action on multiple threads at once instead of iterating over each thread.
82 public:
84 
85  bool DoExecute(Args &command, CommandReturnObject &result) override;
86 
87 protected:
88  /// Method that handles the command after the main arguments have been parsed.
89  ///
90  /// \param[in] tids
91  /// The thread ids passed as arguments.
92  ///
93  /// \return
94  /// A boolean result similar to the one expected from \a DoExecute.
95  virtual bool DoExecuteOnThreads(Args &command, CommandReturnObject &result,
96  llvm::ArrayRef<lldb::tid_t> tids) = 0;
97 };
98 
99 } // namespace lldb_private
100 
101 #endif // LLDB_SOURCE_COMMANDS_COMMANDOBJECTTHREADUTIL_H
lldb_private::CommandObjectParsed
Definition: CommandObject.h:394
lldb_private::CommandObjectIterateOverThreads::m_unique_stacks
bool m_unique_stacks
Definition: CommandObjectThreadUtil.h:75
lldb_private::CommandObjectIterateOverThreads::m_success_return
lldb::ReturnStatus m_success_return
Definition: CommandObjectThreadUtil.h:74
lldb_private::CommandObjectIterateOverThreads::DoExecute
bool DoExecute(Args &command, CommandReturnObject &result) override
Definition: CommandObjectThreadUtil.cpp:24
lldb_private::CommandObjectMultipleThreads::DoExecute
bool DoExecute(Args &command, CommandReturnObject &result) override
Definition: CommandObjectThreadUtil.cpp:157
lldb_private::CommandObjectIterateOverThreads::UniqueStack::m_thread_index_ids
std::vector< uint32_t > m_thread_index_ids
Definition: CommandObjectThreadUtil.h:46
lldb_private::Args
Definition: Args.h:33
lldb_private::CommandObjectMultipleThreads::DoExecuteOnThreads
virtual bool DoExecuteOnThreads(Args &command, CommandReturnObject &result, llvm::ArrayRef< lldb::tid_t > tids)=0
Method that handles the command after the main arguments have been parsed.
lldb::ReturnStatus
ReturnStatus
Command Return Status Types.
Definition: lldb-enumerations.h:258
lldb_private::CommandObjectParsed::CommandObjectParsed
CommandObjectParsed(CommandInterpreter &interpreter, const char *name, const char *help=nullptr, const char *syntax=nullptr, uint32_t flags=0)
Definition: CommandObject.h:396
lldb_private::CommandObjectIterateOverThreads::UniqueStack::GetRepresentativeThread
lldb::tid_t GetRepresentativeThread() const
Definition: CommandObjectThreadUtil.h:33
lldb_private::CommandObjectIterateOverThreads::UniqueStack::AddThread
void AddThread(uint32_t thread_index_id) const
Definition: CommandObjectThreadUtil.h:25
lldb_private::CommandObjectIterateOverThreads::UniqueStack::m_stack_frames
std::stack< lldb::addr_t > m_stack_frames
Definition: CommandObjectThreadUtil.h:47
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:214
CommandObjectMultiword.h
lldb_private::CommandObjectMultipleThreads
Class similar to CommandObjectIterateOverThreads, but which performs an action on multiple threads at...
Definition: CommandObjectThreadUtil.h:81
lldb_private::CommandObjectIterateOverThreads::UniqueStack::GetUniqueThreadIndexIDs
const std::vector< uint32_t > & GetUniqueThreadIndexIDs() const
Definition: CommandObjectThreadUtil.h:29
lldb_private::CommandObjectIterateOverThreads::m_add_return
bool m_add_return
Definition: CommandObjectThreadUtil.h:76
lldb_private::CommandObjectIterateOverThreads::CommandObjectIterateOverThreads
CommandObjectIterateOverThreads(CommandInterpreter &interpreter, const char *name, const char *help, const char *syntax, uint32_t flags)
Definition: CommandObjectThreadUtil.cpp:19
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:25
uint32_t
lldb::eReturnStatusSuccessFinishResult
@ eReturnStatusSuccessFinishResult
Definition: lldb-enumerations.h:261
lldb_private::CommandObjectIterateOverThreads::UniqueStack
Definition: CommandObjectThreadUtil.h:18
lldb_private::CommandObjectIterateOverThreads::UniqueStack::UniqueStack
UniqueStack(std::stack< lldb::addr_t > stack_frames, uint32_t thread_index_id)
Definition: CommandObjectThreadUtil.h:20
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::CommandObjectIterateOverThreads::HandleOneThread
virtual bool HandleOneThread(lldb::tid_t, CommandReturnObject &result)=0
lldb_private::CommandObjectIterateOverThreads::BucketThread
bool BucketThread(lldb::tid_t tid, std::set< UniqueStack > &unique_stacks, CommandReturnObject &result)
Definition: CommandObjectThreadUtil.cpp:122
lldb_private::CommandObjectIterateOverThreads
Definition: CommandObjectThreadUtil.h:16
lldb_private::CommandObjectIterateOverThreads::~CommandObjectIterateOverThreads
~CommandObjectIterateOverThreads() override=default
lldb_private::CommandObjectIterateOverThreads::UniqueStack::operator<
friend bool operator<(const UniqueStack &lhs, const UniqueStack &rhs)
Definition: CommandObjectThreadUtil.h:37
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86