LLDB mainline
TargetGroup.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_TARGET_TARGETGROUP_H
10#define LLDB_TARGET_TARGETGROUP_H
11
12#include "lldb/lldb-forward.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/ExtensibleRTTI.h"
16
17#include <memory>
18#include <mutex>
19#include <optional>
20#include <string>
21#include <vector>
22
23namespace lldb_private {
24
25class TargetGroupList;
26
27/// A group of related targets and the roles they play in that relationship.
28///
29/// Target::Destroy() automatically removes a target from all its groups;
30/// clients do not need to remove memberships during target destruction.
31class TargetGroup : public llvm::RTTIExtends<TargetGroup, llvm::RTTIRoot>,
32 public std::enable_shared_from_this<TargetGroup> {
33public:
34 static char ID;
35
36 /// Describes the part a target plays in this group.
37 enum class Role {
40 };
41
42 /// Target groups have identity and maintain backlinks in their member
43 /// targets, so they cannot be copied or moved. Create one with
44 /// Debugger::GetTargetGroupList().CreateTargetGroup().
45 TargetGroup(const TargetGroup &) = delete;
46 TargetGroup &operator=(const TargetGroup &) = delete;
49 ~TargetGroup() override;
50
51 llvm::StringRef GetDescription() const { return m_description; }
52
53 /// Add \p target_sp to the group with \p role. Adding an existing member
54 /// succeeds only when its role matches \p role.
55 llvm::Error AddTarget(const lldb::TargetSP &target_sp, Role role);
56
57 /// Remove \p target_sp from the group.
58 bool RemoveTarget(const lldb::TargetSP &target_sp);
59
60 /// Return the role assigned to \p target_sp, if it is a member.
61 std::optional<Role> GetRole(const lldb::TargetSP &target_sp) const;
62
63 /// Return every target in the group.
64 std::vector<lldb::TargetSP> GetTargets() const;
65
66 /// Return targets whose role is \p role.
67 std::vector<lldb::TargetSP> GetTargets(Role role) const;
68
69protected:
70 TargetGroup(TargetGroupList &group_list, llvm::StringRef description);
71
72 /// Validate a new membership while the membership lock is held. Success
73 /// accepts the addition; an error rejects it without modifying the group.
74 /// Overrides may inspect existing members through GetTargets or GetRole.
75 /// Removing a target is not validated.
76 virtual llvm::Error ValidateTargetAddition(const lldb::TargetSP &target_sp,
77 Role role) const;
78
79private:
80 friend class TargetGroupList;
81
86
88 std::string m_description;
89 std::shared_ptr<std::recursive_mutex> m_mutex;
90 /// Targets are weakly held so membership does not extend their lifetime.
91 std::vector<Entry> m_entries;
92};
93
94/// A group that accepts a CPU target as its first member, followed by
95/// accelerator targets.
97 : public llvm::RTTIExtends<TargetGroupCPUAndAccelerators, TargetGroup> {
98 using Base = llvm::RTTIExtends<TargetGroupCPUAndAccelerators, TargetGroup>;
99
100public:
101 static char ID;
102
103 /// Return the CPU target, or nullptr if the group has no CPU target.
105
106private:
107 friend class TargetGroupList;
108
110 llvm::StringRef description);
111
112 llvm::Error ValidateTargetAddition(const lldb::TargetSP &target_sp,
113 Role role) const override;
114};
115
116} // namespace lldb_private
117
118#endif // LLDB_TARGET_TARGETGROUP_H
lldb::TargetSP GetCPUTarget() const
Return the CPU target, or nullptr if the group has no CPU target.
llvm::Error ValidateTargetAddition(const lldb::TargetSP &target_sp, Role role) const override
TargetGroupCPUAndAccelerators(TargetGroupList &group_list, llvm::StringRef description)
llvm::RTTIExtends< TargetGroupCPUAndAccelerators, TargetGroup > Base
Definition TargetGroup.h:98
A thread-safe collection of target groups.
std::shared_ptr< std::recursive_mutex > m_mutex
Definition TargetGroup.h:89
llvm::StringRef GetDescription() const
Definition TargetGroup.h:51
friend class TargetGroupList
Definition TargetGroup.h:80
bool RemoveTarget(const lldb::TargetSP &target_sp)
Remove target_sp from the group.
std::vector< Entry > m_entries
Targets are weakly held so membership does not extend their lifetime.
Definition TargetGroup.h:91
TargetGroup & operator=(const TargetGroup &)=delete
Role
Describes the part a target plays in this group.
Definition TargetGroup.h:37
llvm::Error AddTarget(const lldb::TargetSP &target_sp, Role role)
Add target_sp to the group with role.
std::optional< Role > GetRole(const lldb::TargetSP &target_sp) const
Return the role assigned to target_sp, if it is a member.
TargetGroup(const TargetGroup &)=delete
Target groups have identity and maintain backlinks in their member targets, so they cannot be copied ...
std::vector< lldb::TargetSP > GetTargets() const
Return every target in the group.
virtual llvm::Error ValidateTargetAddition(const lldb::TargetSP &target_sp, Role role) const
Validate a new membership while the membership lock is held.
lldb::DebuggerWP m_debugger_wp
Definition TargetGroup.h:87
TargetGroup(TargetGroup &&)=delete
TargetGroup & operator=(TargetGroup &&)=delete
A class that represents a running process on the host machine.
std::weak_ptr< lldb_private::Debugger > DebuggerWP
std::weak_ptr< lldb_private::Target > TargetWP
std::shared_ptr< lldb_private::Target > TargetSP