1 /*
2 * Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #ifndef SHARE_PRIMS_JVMTIIMPL_HPP
26 #define SHARE_PRIMS_JVMTIIMPL_HPP
27
28 #include "jvmtifiles/jvmti.h"
29 #include "oops/objArrayOop.hpp"
30 #include "prims/jvmtiEnvThreadState.hpp"
31 #include "prims/jvmtiEventController.hpp"
32 #include "prims/jvmtiTrace.hpp"
33 #include "prims/jvmtiUtil.hpp"
34 #include "runtime/escapeBarrier.hpp"
35 #include "runtime/stackValueCollection.hpp"
36 #include "runtime/vmOperations.hpp"
37 #include "utilities/ostream.hpp"
38
39 // Forward Declaration
40
41 class JvmtiEnv;
42
43 ///////////////////////////////////////////////////////////////
44 //
45 // class JvmtiBreakpoint
46 //
47 // A JvmtiBreakpoint describes a location (class, method, bci) to break at.
48 //
49
50 typedef void (Method::*method_action)(int _bci);
51
52 class JvmtiBreakpoint : public CHeapObj<mtInternal> {
53 private:
54 Method* _method;
55 int _bci;
56 OopHandle _class_holder; // keeps _method memory from being deallocated
57
58 public:
59 JvmtiBreakpoint(Method* m_method, jlocation location);
60 JvmtiBreakpoint(const JvmtiBreakpoint& bp);
61 virtual ~JvmtiBreakpoint();
62 bool equals(const JvmtiBreakpoint& bp) const;
63 address getBcp() const;
64 void each_method_version_do(method_action meth_act);
65 void set();
66 void clear();
67 void print_on(outputStream* out) const;
68
69 Method* method() const { return _method; }
70 };
71
72 ///////////////////////////////////////////////////////////////
73 //
74 // class JvmtiBreakpoints
75 //
76 // Contains growable array of JvmtiBreakpoint.
77 // All changes to the array occur at a safepoint.
78 //
79
80 class JvmtiBreakpoints : public CHeapObj<mtInternal> {
81 private:
82 GrowableArray<JvmtiBreakpoint*> _elements;
83
84 int length() { return _elements.length(); }
85 JvmtiBreakpoint& at(int index) { return *_elements.at(index); }
86 int find(JvmtiBreakpoint& e) {
87 return _elements.find_if([&](const JvmtiBreakpoint * other_e) { return e.equals(*other_e); });
88 }
89 void append(JvmtiBreakpoint& e) {
90 JvmtiBreakpoint* new_e = new JvmtiBreakpoint(e);
91 _elements.append(new_e);
92 }
93 void remove(int index) {
94 JvmtiBreakpoint* e = _elements.at(index);
95 assert(e != nullptr, "e != nullptr");
96 _elements.remove_at(index);
97 delete e;
98 }
99
100 friend class JvmtiCurrentBreakpoints;
101 JvmtiBreakpoints(); // accessible only for JvmtiCurrentBreakpoints
102
103 public:
104 ~JvmtiBreakpoints();
105
106 void print();
107
108 int set(JvmtiBreakpoint& bp);
109 int clear(JvmtiBreakpoint& bp);
110
111 // used by VM_ChangeBreakpoints
112 void set_at_safepoint(JvmtiBreakpoint& bp);
113 void clear_at_safepoint(JvmtiBreakpoint& bp);
114 // used by VM_RedefineClasses
115 void clearall_in_class_at_safepoint(Klass* klass);
116 };
117
118 ///////////////////////////////////////////////////////////////
119 //
120 // class JvmtiCurrentBreakpoints
121 //
122 // A static wrapper class for the JvmtiBreakpoints that provides
123 // a function for lazily creating the JvmtiBreakpoints class.
124 //
125
126 class JvmtiCurrentBreakpoints : public AllStatic {
127 private:
128 // Current breakpoints, lazily initialized by get_jvmti_breakpoints();
129 static JvmtiBreakpoints *_jvmti_breakpoints;
130
131 public:
132 // lazily create _jvmti_breakpoints
133 static JvmtiBreakpoints& get_jvmti_breakpoints();
134 };
135
136 ///////////////////////////////////////////////////////////////
137 //
138 // VM_ChangeBreakpoints implements a VM_Operation for ALL modifications to the JvmtiBreakpoints class.
139 //
140
141 class VM_ChangeBreakpoints : public VM_Operation {
142 private:
143 JvmtiBreakpoints* _breakpoints;
144 int _operation;
145 JvmtiBreakpoint* _bp;
146
147 public:
148 enum { SET_BREAKPOINT=0, CLEAR_BREAKPOINT=1 };
149
150 VM_ChangeBreakpoints(int operation, JvmtiBreakpoint *bp) {
151 JvmtiBreakpoints& current_bps = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
152 _breakpoints = ¤t_bps;
153 _bp = bp;
154 _operation = operation;
155 assert(bp != nullptr, "bp != null");
156 }
157
158 VMOp_Type type() const { return VMOp_ChangeBreakpoints; }
159 void doit();
160 };
161
162
163 ///////////////////////////////////////////////////////////////
164 // The get/set local operations must only be done by the VM thread
165 // because the interpreter version needs to access oop maps, which can
166 // only safely be done by the VM thread
167 //
168 // I'm told that in 1.5 oop maps are now protected by a lock and
169 // we could get rid of the VM op
170 // However if the VM op is removed then the target thread must
171 // be suspended AND a lock will be needed to prevent concurrent
172 // setting of locals to the same java thread. This lock is needed
173 // to prevent compiledVFrames from trying to add deferred updates
174 // to the thread simultaneously.
175 //
176 class VM_BaseGetOrSetLocal : public VM_Operation {
177 protected:
178 JavaThread* _calling_thread;
179 jint _depth;
180 jint _index;
181 BasicType _type;
182 jvalue _value;
183 javaVFrame* _jvf;
184 bool _set;
185 bool _self;
186
187 static const jvalue _DEFAULT_VALUE;
188
189 // It is possible to get the receiver out of a non-static native wrapper
190 // frame. Use VM_GetReceiver to do this.
191 virtual bool getting_receiver() const { return false; }
192
193 jvmtiError _result;
194
195 virtual javaVFrame* get_java_vframe() = 0;
196 bool check_slot_type_lvt(javaVFrame* vf);
197 bool check_slot_type_no_lvt(javaVFrame* vf);
198
199 public:
200 VM_BaseGetOrSetLocal(JavaThread* calling_thread, jint depth, jint index,
201 BasicType type, jvalue value, bool set, bool self);
202
203 jvalue value() { return _value; }
204 jvmtiError result() { return _result; }
205
206 void doit();
207 bool allow_nested_vm_operations() const;
208 virtual const char* name() const = 0;
209
210 // Check that the klass is assignable to a type with the given signature.
211 static bool is_assignable(const char* ty_sign, Klass* klass, Thread* thread);
212 };
213
214
215 class VM_GetOrSetLocal : public VM_BaseGetOrSetLocal {
216 protected:
217 JavaThread* _thread;
218 EscapeBarrier _eb;
219
220 vframe* get_vframe();
221 javaVFrame* get_java_vframe();
222
223 public:
224 // Constructor for non-object getter
225 VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, bool self);
226
227 // Constructor for object or non-object setter
228 VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value, bool self);
229
230 // Constructor for object getter
231 VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index, bool self);
232
233 VMOp_Type type() const { return VMOp_GetOrSetLocal; }
234
235 bool doit_prologue();
236
237 const char* name() const { return "get/set locals"; }
238 };
239
240 class VM_GetReceiver : public VM_GetOrSetLocal {
241 protected:
242 virtual bool getting_receiver() const { return true; }
243
244 public:
245 VM_GetReceiver(JavaThread* thread, JavaThread* calling_thread, jint depth, bool self);
246 const char* name() const { return "get receiver"; }
247 };
248
249 // VM operation to get or set virtual thread local.
250 class VM_VirtualThreadGetOrSetLocal : public VM_BaseGetOrSetLocal {
251 protected:
252 JvmtiEnv *_env;
253 Handle _vthread_h;
254
255 javaVFrame* get_java_vframe();
256
257 public:
258 // Constructor for non-object getter.
259 VM_VirtualThreadGetOrSetLocal(JvmtiEnv* env, Handle vthread_h, jint depth, jint index, BasicType type, bool self);
260
261 // Constructor for object or non-object setter.
262 VM_VirtualThreadGetOrSetLocal(JvmtiEnv* env, Handle vthread_h, jint depth,
263 jint index, BasicType type, jvalue value, bool self);
264
265 // Constructor for object getter.
266 VM_VirtualThreadGetOrSetLocal(JvmtiEnv* env, Handle vthread_h, JavaThread* calling_thread,
267 jint depth, int index, bool self);
268
269 VMOp_Type type() const { return VMOp_VirtualThreadGetOrSetLocal; }
270
271 const char* name() const { return "virtual thread get/set locals"; }
272 };
273
274 class VM_VirtualThreadGetReceiver : public VM_VirtualThreadGetOrSetLocal {
275 protected:
276 virtual bool getting_receiver() const { return true; }
277
278 public:
279 VM_VirtualThreadGetReceiver(JvmtiEnv* env, Handle vthread_h, JavaThread* calling_thread, jint depth, bool self);
280 const char* name() const { return "virtual thread get receiver"; }
281 };
282
283
284 /**
285 * When a thread (such as the compiler thread or VM thread) cannot post a
286 * JVMTI event itself because the event needs to be posted from a Java
287 * thread, then it can defer the event to the Service thread for posting.
288 * The information needed to post the event is encapsulated into this class
289 * and then enqueued onto the JvmtiDeferredEventQueue, where the Service
290 * thread will pick it up and post it.
291 *
292 * This is currently only used for posting compiled-method-load and unload
293 * events, which we don't want posted from the compiler thread.
294 */
295 class JvmtiDeferredEvent {
296 friend class JvmtiDeferredEventQueue;
297 private:
298 typedef enum {
299 TYPE_NONE,
300 TYPE_COMPILED_METHOD_LOAD,
301 TYPE_COMPILED_METHOD_UNLOAD,
302 TYPE_DYNAMIC_CODE_GENERATED,
303 TYPE_CLASS_UNLOAD
304 } Type;
305
306 Type _type;
307 union {
308 nmethod* compiled_method_load;
309 struct {
310 jmethodID method_id;
311 const void* code_begin;
312 } compiled_method_unload;
313 struct {
314 const char* name;
315 const void* code_begin;
316 const void* code_end;
317 } dynamic_code_generated;
318 struct {
319 const char* name;
320 } class_unload;
321 } _event_data;
322
323 JvmtiDeferredEvent(Type t) : _type(t) {}
324
325 public:
326
327 JvmtiDeferredEvent() : _type(TYPE_NONE) {}
328
329 // Factory methods
330 static JvmtiDeferredEvent compiled_method_load_event(nmethod* nm)
331 NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
332 static JvmtiDeferredEvent compiled_method_unload_event(
333 jmethodID id, const void* code) NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
334 static JvmtiDeferredEvent dynamic_code_generated_event(
335 const char* name, const void* begin, const void* end)
336 NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
337 static JvmtiDeferredEvent class_unload_event(
338 const char* name) NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
339
340 // Actually posts the event.
341 void post() NOT_JVMTI_RETURN;
342 void post_compiled_method_load_event(JvmtiEnv* env) NOT_JVMTI_RETURN;
343 void run_nmethod_entry_barriers() NOT_JVMTI_RETURN;
344 // GC support to keep nmethods from unloading while in the queue.
345 void nmethods_do(NMethodClosure* cf) NOT_JVMTI_RETURN;
346 // GC support to keep nmethod from being unloaded while in the queue.
347 void oops_do(OopClosure* f, NMethodClosure* cf) NOT_JVMTI_RETURN;
348 };
349
350 /**
351 * Events enqueued on this queue wake up the Service thread which dequeues
352 * and posts the events. The Service_lock is required to be held
353 * when operating on the queue.
354 */
355 class JvmtiDeferredEventQueue : public CHeapObj<mtInternal> {
356 friend class JvmtiDeferredEvent;
357 private:
358 class QueueNode : public CHeapObj<mtInternal> {
359 private:
360 JvmtiDeferredEvent _event;
361 QueueNode* _next;
362
363 public:
364 QueueNode(const JvmtiDeferredEvent& event)
365 : _event(event), _next(nullptr) {}
366
367 JvmtiDeferredEvent& event() { return _event; }
368 QueueNode* next() const { return _next; }
369
370 void set_next(QueueNode* next) { _next = next; }
371 };
372
373 QueueNode* _queue_head;
374 QueueNode* _queue_tail;
375
376 public:
377 JvmtiDeferredEventQueue() : _queue_head(nullptr), _queue_tail(nullptr) {}
378
379 bool has_events() NOT_JVMTI_RETURN_(false);
380 JvmtiDeferredEvent dequeue() NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
381
382 // Post all events in the queue for the current Jvmti environment
383 void post(JvmtiEnv* env) NOT_JVMTI_RETURN;
384 void enqueue(JvmtiDeferredEvent event) NOT_JVMTI_RETURN;
385 void run_nmethod_entry_barriers();
386
387 // GC support to keep nmethods from unloading while in the queue.
388 void nmethods_do(NMethodClosure* cf) NOT_JVMTI_RETURN;
389 // GC support to keep nmethod from being unloaded while in the queue.
390 void oops_do(OopClosure* f, NMethodClosure* cf) NOT_JVMTI_RETURN;
391 };
392
393 // Utility macro that checks for null pointers:
394 #define NULL_CHECK(X, Y) if ((X) == nullptr) { return (Y); }
395
396 #endif // SHARE_PRIMS_JVMTIIMPL_HPP