LLDB mainline
Policy.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_UTILITY_POLICY_H
10#define LLDB_UTILITY_POLICY_H
11
12#include "llvm/ADT/SmallVector.h"
13
14#include <cassert>
15#include <thread>
16
17namespace lldb_private {
18
19class Stream;
20
21/// Describes what view of the process a thread should see and what
22/// operations it is allowed to perform.
23///
24/// This replaces ad-hoc checks like CurrentThreadIsPrivateStateThread() with
25/// a unified, composable mechanism. Code consults the current policy on the
26/// per-thread PolicyStack instead of comparing host thread identities.
27///
28/// One motivating case is frame providers, which layer a public illusion on
29/// top of the private unwinder stack. The private state thread must see the
30/// raw unwinder frames, while public clients see the augmented view. Rather
31/// than checking thread identity at every callsite, the private state thread
32/// pushes Policy::CreatePrivateState() and the rest follows from the policy.
33struct Policy {
34 /// What view of the process this thread sees.
35 enum class View {
36 Public, ///< Provider-augmented frames, public state, public run lock.
37 Private, ///< Parent (unwinder) frames, private state, private run lock.
38 };
39
40 /// What operations this thread is allowed to perform.
41 /// Enforced at specific callsites, not by the policy itself.
42 struct Capabilities {
44 /// Whether expression evaluation may resume all threads to avoid
45 /// deadlocks (e.g. when a lock is held by another thread).
47 /// Whether the expression runner may fall back to running all threads
48 /// after a single-thread attempt times out.
53 };
54
55 /// Why a private-state policy is being pushed. Distinguishes a PST's
56 /// ordinary private-state processing from a PST created to service
57 /// RunThreadPlan expression evaluation, which must not run frame
58 /// providers or recognizers (see StackFrameList::SelectMostRelevantFrame
59 /// and Thread::GetStackFrameList).
64
67
68 /// @name Factories
69 ///
70 /// CreatePublicState is the baseline (returns default Policy{}). The
71 /// transition factories below start from PolicyStack::Get().Current() and
72 /// apply their named change on top.
73 /// @{
78 /// @}
79
80 void Dump(Stream &s) const;
81};
82
83/// Per-thread policy stack.
84///
85/// The stack lives in thread_local storage. Each thread has its own stack,
86/// initialized with a default-constructed base entry that is never popped.
87/// RAII guards (Guard) push and pop policies.
88///
89/// Policies are pushed via named factory methods (PushPrivateState, etc.)
90/// that return an RAII Guard. Direct Push is private to prevent callers
91/// from assembling arbitrary capability combinations.
92///
93/// For thread pool workers that don't inherit thread_local storage, the
94/// policy must be passed into the lambda and pushed onto the worker
95/// thread's stack when the task starts.
97public:
98 static PolicyStack &Get();
99
100 Policy Current() const;
101
102 void Dump(Stream &s) const;
103
104 /// RAII guard that pops a policy on destruction.
105 ///
106 /// A Guard is bound to the thread that created it: the policy stack lives
107 /// in thread_local storage, so popping from a different thread would
108 /// corrupt that thread's stack. Guards may be moved, but only on the
109 /// owning thread; a cross-thread move or destruction is a fatal error.
110 class Guard {
111 friend class PolicyStack;
112
113 public:
114 ~Guard();
115 Guard(Guard &&other);
116 Guard &operator=(Guard &&other);
117
118 Guard(const Guard &) = delete;
119 Guard &operator=(const Guard &) = delete;
120
121 private:
122 Guard() : m_thread_id(std::this_thread::get_id()), m_active(true) {}
123 std::thread::id m_thread_id;
124 bool m_active = false;
125 };
126
127 /// All Push* methods delegate to the named static factories on Policy,
128 /// which already inherit from Current(). So the pushed policy preserves
129 /// existing stack state instead of resetting unrelated fields.
130
131 [[nodiscard]] Guard
137
142
143private:
144 void Push(Policy policy) { m_stack.push_back(std::move(policy)); }
145
146 void Pop() {
147 assert(!m_stack.empty() && "can't pop the base policy");
148 m_stack.pop_back();
149 }
150
151 llvm::SmallVector<Policy> m_stack = {Policy{}};
152};
153
154} // namespace lldb_private
155
156#endif // LLDB_UTILITY_POLICY_H
RAII guard that pops a policy on destruction.
Definition Policy.h:110
Guard & operator=(const Guard &)=delete
std::thread::id m_thread_id
Definition Policy.h:123
Guard(const Guard &)=delete
Guard & operator=(Guard &&other)
Definition Policy.cpp:84
Per-thread policy stack.
Definition Policy.h:96
void Dump(Stream &s) const
Definition Policy.cpp:114
Guard PushPrivateState(Policy::PrivateStatePurpose purpose=Policy::PrivateStatePurpose::Default)
All Push* methods delegate to the named static factories on Policy, which already inherit from Curren...
Definition Policy.h:132
Guard PushPublicStateRunningExpression()
Definition Policy.h:138
void Push(Policy policy)
Definition Policy.h:144
llvm::SmallVector< Policy > m_stack
Definition Policy.h:151
static PolicyStack & Get()
Definition Policy.cpp:21
Policy Current() const
Definition Policy.cpp:26
A stream class that can stream formatted output to a file.
Definition Stream.h:28
A class that represents a running process on the host machine.
What operations this thread is allowed to perform.
Definition Policy.h:42
bool can_run_all_threads
Whether expression evaluation may resume all threads to avoid deadlocks (e.g.
Definition Policy.h:46
bool can_try_all_threads
Whether the expression runner may fall back to running all threads after a single-thread attempt time...
Definition Policy.h:49
Describes what view of the process a thread should see and what operations it is allowed to perform.
Definition Policy.h:33
static Policy CreatePublicState()
Definition Policy.cpp:45
Capabilities capabilities
Definition Policy.h:66
View
What view of the process this thread sees.
Definition Policy.h:35
@ Public
Provider-augmented frames, public state, public run lock.
Definition Policy.h:36
@ Private
Parent (unwinder) frames, private state, private run lock.
Definition Policy.h:37
void Dump(Stream &s) const
Definition Policy.cpp:102
PrivateStatePurpose
Why a private-state policy is being pushed.
Definition Policy.h:60
static Policy CreatePublicStateRunningExpression()
Definition Policy.cpp:61
static Policy CreatePrivateState(PrivateStatePurpose purpose=PrivateStatePurpose::Default)
Definition Policy.cpp:47