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 static thread_local PolicyStack s_stack;
100 return s_stack;
101 }
102
103 Policy Current() const;
104
105 void Dump(Stream &s) const;
106
107 /// RAII guard that pops a policy on destruction.
108 ///
109 /// A Guard is bound to the thread that created it: the policy stack lives
110 /// in thread_local storage, so popping from a different thread would
111 /// corrupt that thread's stack. Guards may be moved, but only on the
112 /// owning thread; a cross-thread move or destruction is a fatal error.
113 class Guard {
114 friend class PolicyStack;
115
116 public:
117 ~Guard();
118 Guard(Guard &&other);
119 Guard &operator=(Guard &&other);
120
121 Guard(const Guard &) = delete;
122 Guard &operator=(const Guard &) = delete;
123
124 private:
125 Guard() : m_thread_id(std::this_thread::get_id()), m_active(true) {}
126 std::thread::id m_thread_id;
127 bool m_active = false;
128 };
129
130 /// All Push* methods delegate to the named static factories on Policy,
131 /// which already inherit from Current(). So the pushed policy preserves
132 /// existing stack state instead of resetting unrelated fields.
133
134 [[nodiscard]] Guard
140
145
146private:
147 void Push(Policy policy) { m_stack.push_back(std::move(policy)); }
148
149 void Pop() {
150 assert(!m_stack.empty() && "can't pop the base policy");
151 m_stack.pop_back();
152 }
153
154 llvm::SmallVector<Policy> m_stack = {Policy{}};
155};
156
157} // namespace lldb_private
158
159#endif // LLDB_UTILITY_POLICY_H
RAII guard that pops a policy on destruction.
Definition Policy.h:113
Guard & operator=(const Guard &)=delete
std::thread::id m_thread_id
Definition Policy.h:126
Guard(const Guard &)=delete
Guard & operator=(Guard &&other)
Definition Policy.cpp:71
Per-thread policy stack.
Definition Policy.h:96
void Dump(Stream &s) const
Definition Policy.cpp:101
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:135
Guard PushPublicStateRunningExpression()
Definition Policy.h:141
void Push(Policy policy)
Definition Policy.h:147
static PolicyStack & Get()
Definition Policy.h:98
llvm::SmallVector< Policy > m_stack
Definition Policy.h:154
Policy Current() const
Definition Policy.cpp:18
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:32
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:89
PrivateStatePurpose
Why a private-state policy is being pushed.
Definition Policy.h:60
static Policy CreatePublicStateRunningExpression()
Definition Policy.cpp:48
static Policy CreatePrivateState(PrivateStatePurpose purpose=PrivateStatePurpose::Default)
Definition Policy.cpp:34