xref: /aosp_15_r20/external/swiftshader/src/Vulkan/Debug/Context.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker //    http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker 
15*03ce13f7SAndroid Build Coastguard Worker #ifndef VK_DEBUG_CONTEXT_HPP_
16*03ce13f7SAndroid Build Coastguard Worker #define VK_DEBUG_CONTEXT_HPP_
17*03ce13f7SAndroid Build Coastguard Worker 
18*03ce13f7SAndroid Build Coastguard Worker #include "ID.hpp"
19*03ce13f7SAndroid Build Coastguard Worker 
20*03ce13f7SAndroid Build Coastguard Worker #include <memory>
21*03ce13f7SAndroid Build Coastguard Worker #include <string>
22*03ce13f7SAndroid Build Coastguard Worker #include <unordered_set>
23*03ce13f7SAndroid Build Coastguard Worker #include <vector>
24*03ce13f7SAndroid Build Coastguard Worker 
25*03ce13f7SAndroid Build Coastguard Worker namespace vk {
26*03ce13f7SAndroid Build Coastguard Worker namespace dbg {
27*03ce13f7SAndroid Build Coastguard Worker 
28*03ce13f7SAndroid Build Coastguard Worker // Forward declarations.
29*03ce13f7SAndroid Build Coastguard Worker class Thread;
30*03ce13f7SAndroid Build Coastguard Worker class File;
31*03ce13f7SAndroid Build Coastguard Worker class Frame;
32*03ce13f7SAndroid Build Coastguard Worker class Scope;
33*03ce13f7SAndroid Build Coastguard Worker class Variables;
34*03ce13f7SAndroid Build Coastguard Worker class ClientEventListener;
35*03ce13f7SAndroid Build Coastguard Worker class ServerEventListener;
36*03ce13f7SAndroid Build Coastguard Worker 
37*03ce13f7SAndroid Build Coastguard Worker // Context holds the full state of the debugger, including all current files,
38*03ce13f7SAndroid Build Coastguard Worker // threads, frames and variables. It also holds a list of EventListeners that
39*03ce13f7SAndroid Build Coastguard Worker // can be broadcast to using the Context::broadcast() interface.
40*03ce13f7SAndroid Build Coastguard Worker // Context requires locking before accessing any state. The lock is
41*03ce13f7SAndroid Build Coastguard Worker // non-reentrant and careful use is required to prevent accidentical
42*03ce13f7SAndroid Build Coastguard Worker // double-locking by the same thread.
43*03ce13f7SAndroid Build Coastguard Worker class Context
44*03ce13f7SAndroid Build Coastguard Worker {
45*03ce13f7SAndroid Build Coastguard Worker 	class Impl;
46*03ce13f7SAndroid Build Coastguard Worker 
47*03ce13f7SAndroid Build Coastguard Worker public:
48*03ce13f7SAndroid Build Coastguard Worker 	// Lock is the interface to the Context's state.
49*03ce13f7SAndroid Build Coastguard Worker 	// The lock is automatically released when the Lock is destructed.
50*03ce13f7SAndroid Build Coastguard Worker 	class Lock
51*03ce13f7SAndroid Build Coastguard Worker 	{
52*03ce13f7SAndroid Build Coastguard Worker 	public:
53*03ce13f7SAndroid Build Coastguard Worker 		Lock(Impl *);
54*03ce13f7SAndroid Build Coastguard Worker 		Lock(Lock &&);
55*03ce13f7SAndroid Build Coastguard Worker 		~Lock();
56*03ce13f7SAndroid Build Coastguard Worker 
57*03ce13f7SAndroid Build Coastguard Worker 		// move-assignment operator.
58*03ce13f7SAndroid Build Coastguard Worker 		Lock &operator=(Lock &&);
59*03ce13f7SAndroid Build Coastguard Worker 
60*03ce13f7SAndroid Build Coastguard Worker 		// unlock() explicitly unlocks before the Lock destructor is called.
61*03ce13f7SAndroid Build Coastguard Worker 		// It is illegal to call any other methods after calling unlock().
62*03ce13f7SAndroid Build Coastguard Worker 		void unlock();
63*03ce13f7SAndroid Build Coastguard Worker 
64*03ce13f7SAndroid Build Coastguard Worker 		// currentThread() creates (or returns an existing) a Thread that
65*03ce13f7SAndroid Build Coastguard Worker 		// represents the currently executing thread.
66*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<Thread> currentThread();
67*03ce13f7SAndroid Build Coastguard Worker 
68*03ce13f7SAndroid Build Coastguard Worker 		// get() returns the thread with the given ID, or null if the thread
69*03ce13f7SAndroid Build Coastguard Worker 		// does not exist or no longer has any external shared_ptr references.
70*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<Thread> get(ID<Thread>);
71*03ce13f7SAndroid Build Coastguard Worker 
72*03ce13f7SAndroid Build Coastguard Worker 		// threads() returns the full list of threads that still have an
73*03ce13f7SAndroid Build Coastguard Worker 		// external shared_ptr reference.
74*03ce13f7SAndroid Build Coastguard Worker 		std::vector<std::shared_ptr<Thread>> threads();
75*03ce13f7SAndroid Build Coastguard Worker 
76*03ce13f7SAndroid Build Coastguard Worker 		// createVirtualFile() returns a new file that is not backed by the
77*03ce13f7SAndroid Build Coastguard Worker 		// filesystem.
78*03ce13f7SAndroid Build Coastguard Worker 		// name is the unique name of the file.
79*03ce13f7SAndroid Build Coastguard Worker 		// source is the content of the file.
80*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<File> createVirtualFile(const std::string &name,
81*03ce13f7SAndroid Build Coastguard Worker 		                                        const std::string &source);
82*03ce13f7SAndroid Build Coastguard Worker 
83*03ce13f7SAndroid Build Coastguard Worker 		// createPhysicalFile() returns a new file that is backed by the file
84*03ce13f7SAndroid Build Coastguard Worker 		// at path.
85*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<File> createPhysicalFile(const std::string &path);
86*03ce13f7SAndroid Build Coastguard Worker 
87*03ce13f7SAndroid Build Coastguard Worker 		// get() returns the file with the given ID, or null if the file
88*03ce13f7SAndroid Build Coastguard Worker 		// does not exist or no longer has any external shared_ptr references.
89*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<File> get(ID<File>);
90*03ce13f7SAndroid Build Coastguard Worker 
91*03ce13f7SAndroid Build Coastguard Worker 		// findFile() returns the file with the given path, or nullptr if not
92*03ce13f7SAndroid Build Coastguard Worker 		// found.
93*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<File> findFile(const std::string &path);
94*03ce13f7SAndroid Build Coastguard Worker 
95*03ce13f7SAndroid Build Coastguard Worker 		// files() returns the full list of files.
96*03ce13f7SAndroid Build Coastguard Worker 		std::vector<std::shared_ptr<File>> files();
97*03ce13f7SAndroid Build Coastguard Worker 
98*03ce13f7SAndroid Build Coastguard Worker 		// createFrame() returns a new frame for the given file and function
99*03ce13f7SAndroid Build Coastguard Worker 		// name.
100*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<Frame> createFrame(
101*03ce13f7SAndroid Build Coastguard Worker 		    const std::shared_ptr<File> &file, std::string function);
102*03ce13f7SAndroid Build Coastguard Worker 
103*03ce13f7SAndroid Build Coastguard Worker 		// get() returns the frame with the given ID, or null if the frame
104*03ce13f7SAndroid Build Coastguard Worker 		// does not exist or no longer has any external shared_ptr references.
105*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<Frame> get(ID<Frame>);
106*03ce13f7SAndroid Build Coastguard Worker 
107*03ce13f7SAndroid Build Coastguard Worker 		// createScope() returns a new scope for the given file.
108*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<Scope> createScope(
109*03ce13f7SAndroid Build Coastguard Worker 		    const std::shared_ptr<File> &file);
110*03ce13f7SAndroid Build Coastguard Worker 
111*03ce13f7SAndroid Build Coastguard Worker 		// get() returns the scope with the given ID, or null if the scope
112*03ce13f7SAndroid Build Coastguard Worker 		// does not exist.
113*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<Scope> get(ID<Scope>);
114*03ce13f7SAndroid Build Coastguard Worker 
115*03ce13f7SAndroid Build Coastguard Worker 		// track() registers the variables with the context so it can be
116*03ce13f7SAndroid Build Coastguard Worker 		// retrieved by get(). Note that the context does not hold a strong
117*03ce13f7SAndroid Build Coastguard Worker 		// reference to the variables, and get() will return nullptr if all
118*03ce13f7SAndroid Build Coastguard Worker 		// strong external references are dropped.
119*03ce13f7SAndroid Build Coastguard Worker 		void track(const std::shared_ptr<Variables> &);
120*03ce13f7SAndroid Build Coastguard Worker 
121*03ce13f7SAndroid Build Coastguard Worker 		// get() returns the variables with the given ID, or null if the
122*03ce13f7SAndroid Build Coastguard Worker 		// variables does not exist or no longer has any external shared_ptr
123*03ce13f7SAndroid Build Coastguard Worker 		// references.
124*03ce13f7SAndroid Build Coastguard Worker 		std::shared_ptr<Variables> get(ID<Variables>);
125*03ce13f7SAndroid Build Coastguard Worker 
126*03ce13f7SAndroid Build Coastguard Worker 		// clearFunctionBreakpoints() removes all function breakpoints.
127*03ce13f7SAndroid Build Coastguard Worker 		void clearFunctionBreakpoints();
128*03ce13f7SAndroid Build Coastguard Worker 
129*03ce13f7SAndroid Build Coastguard Worker 		// addFunctionBreakpoint() adds a breakpoint to the start of the
130*03ce13f7SAndroid Build Coastguard Worker 		// function with the given name.
131*03ce13f7SAndroid Build Coastguard Worker 		void addFunctionBreakpoint(const std::string &name);
132*03ce13f7SAndroid Build Coastguard Worker 
133*03ce13f7SAndroid Build Coastguard Worker 		// addPendingBreakpoints() adds a number of breakpoints to the file with
134*03ce13f7SAndroid Build Coastguard Worker 		// the given name which has not yet been created with a call to
135*03ce13f7SAndroid Build Coastguard Worker 		// createVirtualFile() or createPhysicalFile().
136*03ce13f7SAndroid Build Coastguard Worker 		void addPendingBreakpoints(const std::string &name, const std::vector<int> &lines);
137*03ce13f7SAndroid Build Coastguard Worker 
138*03ce13f7SAndroid Build Coastguard Worker 		// isFunctionBreakpoint() returns true if the function with the given
139*03ce13f7SAndroid Build Coastguard Worker 		// name has a function breakpoint set.
140*03ce13f7SAndroid Build Coastguard Worker 		bool isFunctionBreakpoint(const std::string &name);
141*03ce13f7SAndroid Build Coastguard Worker 
142*03ce13f7SAndroid Build Coastguard Worker 		// getFunctionBreakpoints() returns all the set function breakpoints.
143*03ce13f7SAndroid Build Coastguard Worker 		std::unordered_set<std::string> getFunctionBreakpoints();
144*03ce13f7SAndroid Build Coastguard Worker 
145*03ce13f7SAndroid Build Coastguard Worker 	private:
146*03ce13f7SAndroid Build Coastguard Worker 		Lock(const Lock &) = delete;
147*03ce13f7SAndroid Build Coastguard Worker 		Lock &operator=(const Lock &) = delete;
148*03ce13f7SAndroid Build Coastguard Worker 		Impl *ctx;
149*03ce13f7SAndroid Build Coastguard Worker 	};
150*03ce13f7SAndroid Build Coastguard Worker 
151*03ce13f7SAndroid Build Coastguard Worker 	// create() creates and returns a new Context.
152*03ce13f7SAndroid Build Coastguard Worker 	static std::shared_ptr<Context> create();
153*03ce13f7SAndroid Build Coastguard Worker 
154*03ce13f7SAndroid Build Coastguard Worker 	virtual ~Context() = default;
155*03ce13f7SAndroid Build Coastguard Worker 
156*03ce13f7SAndroid Build Coastguard Worker 	// lock() returns a Lock which exclusively locks the context for state
157*03ce13f7SAndroid Build Coastguard Worker 	// access.
158*03ce13f7SAndroid Build Coastguard Worker 	virtual Lock lock() = 0;
159*03ce13f7SAndroid Build Coastguard Worker 
160*03ce13f7SAndroid Build Coastguard Worker 	// addListener() registers an ClientEventListener for event notifications.
161*03ce13f7SAndroid Build Coastguard Worker 	virtual void addListener(ClientEventListener *) = 0;
162*03ce13f7SAndroid Build Coastguard Worker 
163*03ce13f7SAndroid Build Coastguard Worker 	// removeListener() unregisters an ClientEventListener that was previously
164*03ce13f7SAndroid Build Coastguard Worker 	// registered by a call to addListener().
165*03ce13f7SAndroid Build Coastguard Worker 	virtual void removeListener(ClientEventListener *) = 0;
166*03ce13f7SAndroid Build Coastguard Worker 
167*03ce13f7SAndroid Build Coastguard Worker 	// clientEventBroadcast() returns an ClientEventListener that will broadcast
168*03ce13f7SAndroid Build Coastguard Worker 	// all method calls on to all registered ServerEventListeners.
169*03ce13f7SAndroid Build Coastguard Worker 	virtual ClientEventListener *clientEventBroadcast() = 0;
170*03ce13f7SAndroid Build Coastguard Worker 
171*03ce13f7SAndroid Build Coastguard Worker 	// addListener() registers an ServerEventListener for event notifications.
172*03ce13f7SAndroid Build Coastguard Worker 	virtual void addListener(ServerEventListener *) = 0;
173*03ce13f7SAndroid Build Coastguard Worker 
174*03ce13f7SAndroid Build Coastguard Worker 	// removeListener() unregisters an ServerEventListener that was previously
175*03ce13f7SAndroid Build Coastguard Worker 	// registered by a call to addListener().
176*03ce13f7SAndroid Build Coastguard Worker 	virtual void removeListener(ServerEventListener *) = 0;
177*03ce13f7SAndroid Build Coastguard Worker 
178*03ce13f7SAndroid Build Coastguard Worker 	// serverEventBroadcast() returns an ServerEventListener that will broadcast
179*03ce13f7SAndroid Build Coastguard Worker 	// all method calls on to all registered ServerEventListeners.
180*03ce13f7SAndroid Build Coastguard Worker 	virtual ServerEventListener *serverEventBroadcast() = 0;
181*03ce13f7SAndroid Build Coastguard Worker };
182*03ce13f7SAndroid Build Coastguard Worker 
183*03ce13f7SAndroid Build Coastguard Worker }  // namespace dbg
184*03ce13f7SAndroid Build Coastguard Worker }  // namespace vk
185*03ce13f7SAndroid Build Coastguard Worker 
186*03ce13f7SAndroid Build Coastguard Worker #endif  // VK_DEBUG_CONTEXT_HPP_
187