LLDB mainline
Policy.cpp
Go to the documentation of this file.
1//===-- Policy.cpp --------------------------------------------------------===//
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
11#include "lldb/Utility/Log.h"
12#include "lldb/Utility/Stream.h"
14#include "llvm/Support/ErrorHandling.h"
15
16using namespace lldb_private;
17
18// Declared out-of-line so every shared library resolves to the same
19// instance: a function-local static in an inline function is not
20// shared across shared library boundaries under hidden visibility.
22 static thread_local PolicyStack s_stack;
23 return s_stack;
24}
25
27 Policy p = m_stack.back();
28 // `Current()` is called on every read of the current policy (e.g. every
29 // `Process::GetState()`, itself called on every prompt redraw), so log
30 // only when verbose is set to avoid drowning out the process log.
31 if (Log *log = GetLog(LLDBLog::Process)) {
32 if (log->GetVerbose()) {
34 p.Dump(s);
35 LLDB_LOG(log, "{0}", s.GetData());
36 }
37 }
38 return p;
39}
40
41// CreatePublicState is the baseline, not a transition. The stack returns to
42// public state by popping the private-state guards, not by pushing a
43// "public" policy on top. This factory exists only as a reference value
44// (tests, dump comparisons); it never reads the current stack.
46
60
66
68 if (!m_active)
69 return;
70 if (m_thread_id != std::this_thread::get_id())
71 llvm::report_fatal_error(
72 "PolicyStack::Guard destroyed on a different thread than the one "
73 "that created it");
74 Get().Pop();
75}
76
78 : m_thread_id(other.m_thread_id), m_active(other.m_active) {
79 if (m_active && m_thread_id != std::this_thread::get_id())
80 llvm::report_fatal_error("PolicyStack::Guard moved across threads");
81 other.m_active = false;
82}
83
85 if (this != &other) {
86 if (other.m_active && other.m_thread_id != std::this_thread::get_id())
87 llvm::report_fatal_error("PolicyStack::Guard moved across threads");
88 if (m_active) {
89 if (m_thread_id != std::this_thread::get_id())
90 llvm::report_fatal_error(
91 "PolicyStack::Guard destroyed on a different thread than the "
92 "one that created it");
93 Get().Pop();
94 }
95 m_thread_id = other.m_thread_id;
96 m_active = other.m_active;
97 other.m_active = false;
98 }
99 return *this;
100}
101
102void Policy::Dump(Stream &s) const {
103 s << "policy: view=" << (view == View::Public ? "public" : "private");
104 s << ", capabilities={";
105 s << "eval_expr=" << capabilities.can_evaluate_expressions;
106 s << " run_all=" << capabilities.can_run_all_threads;
107 s << " try_all=" << capabilities.can_try_all_threads;
108 s << " bp_actions=" << capabilities.can_run_breakpoint_actions;
109 s << " frame_providers=" << capabilities.can_load_frame_providers;
110 s << " frame_recognizers=" << capabilities.can_run_frame_recognizers;
111 s << '}';
112}
113
114void PolicyStack::Dump(Stream &s) const {
115 s.Printf("PolicyStack depth=%zu\n", m_stack.size());
116 for (size_t i = 0; i < m_stack.size(); i++) {
117 s.Printf(" [%zu] ", i);
118 m_stack[i].Dump(s);
119 s << '\n';
120 }
121}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
RAII guard that pops a policy on destruction.
Definition Policy.h:110
std::thread::id m_thread_id
Definition Policy.h:123
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
llvm::SmallVector< Policy > m_stack
Definition Policy.h:151
static PolicyStack & Get()
Definition Policy.cpp:21
Policy Current() const
Definition Policy.cpp:26
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
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
@ 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