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.
54 };
55
56 /// Why a private-state policy is being pushed. Distinguishes a PST's
57 /// ordinary private-state processing from a PST created to service
58 /// RunThreadPlan expression evaluation, which must not run frame
59 /// providers or recognizers (see StackFrameList::SelectMostRelevantFrame
60 /// and Thread::GetStackFrameList).
65
68
69 /// @name Factories
70 ///
71 /// CreatePublicState is the baseline (returns default Policy{}). The
72 /// transition factories below start from PolicyStack::Get().Current() and
73 /// apply their named change on top.
74 /// @{
80 /// @}
81
82 void Dump(Stream &s) const;
83};
84
85/// Per-thread policy stack.
86///
87/// The stack lives in thread_local storage. Each thread has its own stack,
88/// initialized with a default-constructed base entry that is never popped.
89/// RAII guards (Guard) push and pop policies.
90///
91/// Policies are pushed via named factory methods (PushPrivateState, etc.)
92/// that return an RAII Guard. Direct Push is private to prevent callers
93/// from assembling arbitrary capability combinations.
94///
95/// For thread pool workers that don't inherit thread_local storage, the
96/// policy must be passed into the lambda and pushed onto the worker
97/// thread's stack when the task starts.
99public:
100 static PolicyStack &Get();
101
102 Policy Current() const;
103
104 void Dump(Stream &s) const;
105
106 /// RAII guard that pops a policy on destruction.
107 ///
108 /// A Guard is bound to the thread that created it: the policy stack lives
109 /// in thread_local storage, so popping from a different thread would
110 /// corrupt that thread's stack. Guards may be moved, but only on the
111 /// owning thread; a cross-thread move or destruction is a fatal error.
112 class Guard {
113 friend class PolicyStack;
114
115 public:
116 ~Guard();
117 Guard(Guard &&other);
118 Guard &operator=(Guard &&other);
119
120 Guard(const Guard &) = delete;
121 Guard &operator=(const Guard &) = delete;
122
123 private:
124 Guard() : m_thread_id(std::this_thread::get_id()), m_active(true) {}
125 std::thread::id m_thread_id;
126 bool m_active = false;
127 };
128
129 /// All Push* methods delegate to the named static factories on Policy,
130 /// which already inherit from Current(). So the pushed policy preserves
131 /// existing stack state instead of resetting unrelated fields.
132
133 [[nodiscard]] Guard
139
144
147 return Guard();
148 }
149
150private:
151 void Push(Policy policy) { m_stack.push_back(std::move(policy)); }
152
153 void Pop() {
154 assert(!m_stack.empty() && "can't pop the base policy");
155 m_stack.pop_back();
156 }
157
158 llvm::SmallVector<Policy> m_stack = {Policy{}};
159};
160
161} // namespace lldb_private
162
163#endif // LLDB_UTILITY_POLICY_H
RAII guard that pops a policy on destruction.
Definition Policy.h:112
Guard & operator=(const Guard &)=delete
std::thread::id m_thread_id
Definition Policy.h:125
Guard(const Guard &)=delete
Guard & operator=(Guard &&other)
Definition Policy.cpp:90
Per-thread policy stack.
Definition Policy.h:98
void Dump(Stream &s) const
Definition Policy.cpp:121
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:134
Guard PushScriptedExtensionCall()
Definition Policy.h:145
Guard PushPublicStateRunningExpression()
Definition Policy.h:140
void Push(Policy policy)
Definition Policy.h:151
llvm::SmallVector< Policy > m_stack
Definition Policy.h:158
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 CreateScriptedExtensionCall()
Definition Policy.cpp:67
static Policy CreatePublicState()
Definition Policy.cpp:45
Capabilities capabilities
Definition Policy.h:67
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:108
PrivateStatePurpose
Why a private-state policy is being pushed.
Definition Policy.h:61
static Policy CreatePublicStateRunningExpression()
Definition Policy.cpp:61
static Policy CreatePrivateState(PrivateStatePurpose purpose=PrivateStatePurpose::Default)
Definition Policy.cpp:47