1 /*
  2  * Copyright (c) 2003, 2021, 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_JVMTITHREADSTATE_HPP
 26 #define SHARE_PRIMS_JVMTITHREADSTATE_HPP
 27 
 28 #include "jvmtifiles/jvmti.h"
 29 #include "memory/allocation.hpp"
 30 #include "prims/jvmtiEventController.hpp"
 31 #include "prims/jvmtiExport.hpp"
 32 #include "runtime/thread.hpp"
 33 #include "utilities/growableArray.hpp"
 34 
 35 //
 36 // Forward Declarations
 37 //
 38 
 39 class JvmtiEnvBase;
 40 class JvmtiEnvThreadState;
 41 class JvmtiDynamicCodeEventCollector;
 42 
 43 class JvmtiDeferredEvent;
 44 class JvmtiDeferredEventQueue;
 45 
 46 enum JvmtiClassLoadKind {
 47   jvmti_class_load_kind_load = 100,
 48   jvmti_class_load_kind_retransform,
 49   jvmti_class_load_kind_redefine
 50 };
 51 
 52 ///////////////////////////////////////////////////////////////
 53 //
 54 // class JvmtiEnvThreadStateIterator
 55 //
 56 // The only safe means of iterating through the JvmtiEnvThreadStates
 57 // in a JvmtiThreadState.
 58 // Note that this iteratation includes invalid environments pending
 59 // deallocation -- in fact, some uses depend on this behavior.
 60 //
 61 class JvmtiEnvThreadStateIterator : public StackObj {
 62  private:
 63   JvmtiThreadState* state;
 64  public:
 65   JvmtiEnvThreadStateIterator(JvmtiThreadState* thread_state);
 66   ~JvmtiEnvThreadStateIterator();
 67   JvmtiEnvThreadState* first();
 68   JvmtiEnvThreadState* next(JvmtiEnvThreadState* ets);
 69 };
 70 
 71 
 72 ///////////////////////////////////////////////////////////////
 73 //
 74 // class JvmtiThreadState
 75 //
 76 // The Jvmti state for each thread (across all JvmtiEnv):
 77 // 1. Local table of enabled events.
 78 class JvmtiThreadState : public CHeapObj<mtInternal> {
 79  private:
 80   friend class JvmtiEnv;
 81   JavaThread        *_thread;
 82   // Jvmti Events that cannot be posted in their current context.
 83   JvmtiDeferredEventQueue* _jvmti_event_queue;
 84   bool              _hide_single_stepping;
 85   bool              _pending_step_for_popframe;
 86   bool              _pending_step_for_earlyret;
 87   int               _hide_level;
 88 
 89  public:
 90   enum ExceptionState {
 91     ES_CLEARED,
 92     ES_DETECTED,
 93     ES_CAUGHT
 94   };
 95 
 96  private:
 97   ExceptionState _exception_state;
 98 
 99   // Used to send class being redefined/retransformed and kind of transform
100   // info to the class file load hook event handler.
101   Klass*                _class_being_redefined;
102   JvmtiClassLoadKind    _class_load_kind;
103   GrowableArray<Klass*>* _classes_being_redefined;
104 
105   // This is only valid when is_interp_only_mode() returns true
106   int               _cur_stack_depth;
107 
108   JvmtiThreadEventEnable _thread_event_enable;
109 
110   // for support of JvmtiEnvThreadState
111   JvmtiEnvThreadState*   _head_env_thread_state;
112 
113   // doubly-linked linear list of active thread state
114   // needed in order to iterate the list without holding Threads_lock
115   static JvmtiThreadState *_head;
116   JvmtiThreadState *_next;
117   JvmtiThreadState *_prev;
118 
119   // holds the current dynamic code event collector, NULL if no event collector in use
120   JvmtiDynamicCodeEventCollector* _dynamic_code_event_collector;
121   // holds the current vm object alloc event collector, NULL if no event collector in use
122   JvmtiVMObjectAllocEventCollector* _vm_object_alloc_event_collector;
123   // holds the current sampled object alloc event collector, NULL if no event collector in use
124   JvmtiSampledObjectAllocEventCollector* _sampled_object_alloc_event_collector;
125 
126   // Should only be created by factory methods
127   JvmtiThreadState(JavaThread *thread);
128 
129   friend class JvmtiEnvThreadStateIterator;
130   inline JvmtiEnvThreadState* head_env_thread_state();
131   inline void set_head_env_thread_state(JvmtiEnvThreadState* ets);
132 
133  public:
134   ~JvmtiThreadState();
135 
136   // is event_type enabled and usable for this thread in any enviroments?
137   bool is_enabled(jvmtiEvent event_type) {
138     return _thread_event_enable.is_enabled(event_type);
139   }
140 
141   JvmtiThreadEventEnable *thread_event_enable() {
142     return &_thread_event_enable;
143   }
144 
145   // Must only be called in situations where the state is for the current thread and
146   // the environment can not go away.  To be safe, the returned JvmtiEnvThreadState
147   // must be used in such a way as there can be no intervening safepoints.
148   inline JvmtiEnvThreadState* env_thread_state(JvmtiEnvBase *env);
149 
150   static void periodic_clean_up();
151 
152   void add_env(JvmtiEnvBase *env);
153 
154   // Used by the interpreter for fullspeed debugging support
155   bool is_interp_only_mode()                { return _thread->is_interp_only_mode(); }
156   void enter_interp_only_mode();
157   void leave_interp_only_mode();
158 
159   // access to the linked list of all JVMTI thread states
160   static JvmtiThreadState *first() {
161     assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
162     return _head;
163   }
164 
165   JvmtiThreadState *next()                  {
166     return _next;
167   }
168 
169   // Current stack depth is only valid when is_interp_only_mode() returns true.
170   // These functions should only be called at a safepoint - usually called from same thread.
171   // Returns the number of Java activations on the stack.
172   int cur_stack_depth();
173   void invalidate_cur_stack_depth();
174   void incr_cur_stack_depth();
175   void decr_cur_stack_depth();
176 
177   int count_frames();
178 
179   inline JavaThread *get_thread()      { return _thread;              }
180 
181   inline bool is_exception_detected()  { return _exception_state == ES_DETECTED;  }
182   inline bool is_exception_caught()    { return _exception_state == ES_CAUGHT;  }
183 
184   inline void set_exception_detected() { _exception_state = ES_DETECTED; }
185   inline void set_exception_caught()   { _exception_state = ES_CAUGHT; }
186 
187   inline void clear_exception_state() { _exception_state = ES_CLEARED; }
188 
189   // We need to save and restore exception state inside JvmtiEventMark
190   inline ExceptionState get_exception_state() { return _exception_state; }
191   inline void restore_exception_state(ExceptionState state) { _exception_state = state; }
192 
193   inline void clear_hide_single_stepping() {
194     if (_hide_level > 0) {
195       _hide_level--;
196     } else {
197       assert(_hide_single_stepping, "hide_single_stepping is out of phase");
198       _hide_single_stepping = false;
199     }
200   }
201   inline bool hide_single_stepping() { return _hide_single_stepping; }
202   inline void set_hide_single_stepping() {
203     if (_hide_single_stepping) {
204       _hide_level++;
205     } else {
206       assert(_hide_level == 0, "hide_level is out of phase");
207       _hide_single_stepping = true;
208     }
209   }
210 
211   // Step pending flag is set when PopFrame is called and it is cleared
212   // when step for the Pop Frame is completed.
213   // This logic is used to distinguish b/w step for pop frame and repeat step.
214   void set_pending_step_for_popframe() { _pending_step_for_popframe = true;  }
215   void clr_pending_step_for_popframe() { _pending_step_for_popframe = false; }
216   bool is_pending_step_for_popframe()  { return _pending_step_for_popframe;  }
217   void process_pending_step_for_popframe();
218 
219   // Step pending flag is set when ForceEarlyReturn is called and it is cleared
220   // when step for the ForceEarlyReturn is completed.
221   // This logic is used to distinguish b/w step for early return and repeat step.
222   void set_pending_step_for_earlyret() { _pending_step_for_earlyret = true;  }
223   void clr_pending_step_for_earlyret() { _pending_step_for_earlyret = false; }
224   bool is_pending_step_for_earlyret()  { return _pending_step_for_earlyret;  }
225   void process_pending_step_for_earlyret();
226 
227   // Setter and getter method is used to send redefined class info
228   // when class file load hook event is posted.
229   // It is set while loading redefined class and cleared before the
230   // class file load hook event is posted.
231   inline void set_class_being_redefined(Klass* k, JvmtiClassLoadKind kind) {
232     _class_being_redefined = k;
233     _class_load_kind = kind;
234   }
235 
236   inline void clear_class_being_redefined() {
237     _class_being_redefined = NULL;
238     _class_load_kind = jvmti_class_load_kind_load;
239   }
240 
241   inline Klass* get_class_being_redefined() {
242     return _class_being_redefined;
243   }
244 
245   inline JvmtiClassLoadKind get_class_load_kind() {
246     return _class_load_kind;
247   }
248 
249   // Get the classes that are currently being redefined by this thread.
250   inline GrowableArray<Klass*>* get_classes_being_redefined() {
251     return _classes_being_redefined;
252   }
253 
254   inline void set_classes_being_redefined(GrowableArray<Klass*>* redef_classes) {
255     _classes_being_redefined = redef_classes;
256   }
257 
258   // RedefineClasses support
259   // The bug 6214132 caused the verification to fail.
260   //
261   // What is done at verification:
262   //   (This seems to only apply to the old verifier.)
263   //   When the verifier makes calls into the VM to ask questions about
264   //   the class being verified, it will pass the jclass to JVM_* functions.
265   //   The jclass is always pointing to the mirror of _the_class.
266   //   ~28 JVM_* functions called by the verifier for the information
267   //   about CP entries and klass structure should check the jvmtiThreadState
268   //   info about equivalent klass versions and use it to replace a Klass*
269   //   of _the_class with a Klass* of _scratch_class. The function
270   //   class_to_verify_considering_redefinition() must be called for it.
271   //
272   //   Note again, that this redirection happens only for the verifier thread.
273   //   Other threads have very small overhead by checking the existence
274   //   of the jvmtiThreadSate and the information about klasses equivalence.
275   //   No JNI functions need to be changed, they don't reference the klass guts.
276   //   The JavaThread pointer is already available in all JVM_* functions
277   //   used by the verifier, so there is no extra performance issue with it.
278 
279  private:
280   Klass* _the_class_for_redefinition_verification;
281   Klass* _scratch_class_for_redefinition_verification;
282 
283  public:
284   inline void set_class_versions_map(Klass* the_class,
285                                      Klass* scratch_class) {
286     _the_class_for_redefinition_verification = the_class;
287     _scratch_class_for_redefinition_verification = scratch_class;
288   }
289 
290   inline void clear_class_versions_map() { set_class_versions_map(NULL, NULL); }
291 
292   static inline
293   Klass* class_to_verify_considering_redefinition(Klass* klass,
294                                                     JavaThread *thread) {
295     JvmtiThreadState *state = thread->jvmti_thread_state();
296     if (state != NULL && state->_the_class_for_redefinition_verification != NULL) {
297       if (state->_the_class_for_redefinition_verification == klass) {
298         klass = state->_scratch_class_for_redefinition_verification;
299       }
300     }
301     return klass;
302   }
303 
304   // Todo: get rid of this!
305  private:
306   bool _debuggable;
307  public:
308   // Should the thread be enumerated by jvmtiInternal::GetAllThreads?
309   bool is_debuggable()                 { return _debuggable; }
310   // If a thread cannot be suspended (has no valid last_java_frame) then it gets marked !debuggable
311   void set_debuggable(bool debuggable) { _debuggable = debuggable; }
312 
313  public:
314 
315   // Thread local event collector setter and getter methods.
316   JvmtiDynamicCodeEventCollector* get_dynamic_code_event_collector() {
317     return _dynamic_code_event_collector;
318   }
319   JvmtiVMObjectAllocEventCollector* get_vm_object_alloc_event_collector() {
320     return _vm_object_alloc_event_collector;
321   }
322   JvmtiSampledObjectAllocEventCollector* get_sampled_object_alloc_event_collector() {
323     return _sampled_object_alloc_event_collector;
324   }
325   void set_dynamic_code_event_collector(JvmtiDynamicCodeEventCollector* collector) {
326     _dynamic_code_event_collector = collector;
327   }
328   void set_vm_object_alloc_event_collector(JvmtiVMObjectAllocEventCollector* collector) {
329     _vm_object_alloc_event_collector = collector;
330   }
331   void set_sampled_object_alloc_event_collector(JvmtiSampledObjectAllocEventCollector* collector) {
332     _sampled_object_alloc_event_collector = collector;
333   }
334 
335 
336   //
337   // Frame routines
338   //
339 
340  public:
341 
342   //  true when the thread was suspended with a pointer to the last Java frame.
343   bool has_last_frame()                     { return _thread->has_last_Java_frame(); }
344 
345   void update_for_pop_top_frame();
346 
347   // already holding JvmtiThreadState_lock - retrieve or create JvmtiThreadState
348   // Can return NULL if JavaThread is exiting.
349   static JvmtiThreadState *state_for_while_locked(JavaThread *thread);
350   // retrieve or create JvmtiThreadState
351   // Can return NULL if JavaThread is exiting.
352   static JvmtiThreadState *state_for(JavaThread *thread);
353 
354   // JVMTI ForceEarlyReturn support
355 
356   // This is set to earlyret_pending to signal that top Java frame
357   // should be returned immediately
358  public:
359   int           _earlyret_state;
360   TosState      _earlyret_tos;
361   jvalue        _earlyret_value;
362   oop           _earlyret_oop;         // Used to return an oop result into Java code from
363                                        // ForceEarlyReturnObject, GC-preserved
364 
365   // Setting and clearing earlyret_state
366   // earlyret_pending indicates that a ForceEarlyReturn() has been
367   // requested and not yet been completed.
368  public:
369   enum EarlyretState {
370     earlyret_inactive = 0,
371     earlyret_pending  = 1
372   };
373 
374   void set_earlyret_pending(void) { _earlyret_state = earlyret_pending;  }
375   void clr_earlyret_pending(void) { _earlyret_state = earlyret_inactive; }
376   bool is_earlyret_pending(void)  { return (_earlyret_state == earlyret_pending);  }
377 
378   TosState earlyret_tos()                            { return _earlyret_tos; }
379   oop  earlyret_oop() const                          { return _earlyret_oop; }
380   void set_earlyret_oop (oop x)                      { _earlyret_oop = x;    }
381   jvalue earlyret_value()                            { return _earlyret_value; }
382   void set_earlyret_value(jvalue val, TosState tos)  { _earlyret_tos = tos;  _earlyret_value = val;  }
383   void clr_earlyret_value()                          { _earlyret_tos = ilgl; _earlyret_value.j = 0L; }
384 
385   static ByteSize earlyret_state_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_state); }
386   static ByteSize earlyret_tos_offset()   { return byte_offset_of(JvmtiThreadState, _earlyret_tos); }
387   static ByteSize earlyret_oop_offset()   { return byte_offset_of(JvmtiThreadState, _earlyret_oop); }
388   static ByteSize earlyret_value_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_value); }
389 
390   void oops_do(OopClosure* f, CodeBlobClosure* cf) NOT_JVMTI_RETURN; // GC support
391   void nmethods_do(CodeBlobClosure* cf) NOT_JVMTI_RETURN;
392 
393 public:
394   void set_should_post_on_exceptions(bool val) { _thread->set_should_post_on_exceptions_flag(val ? JNI_TRUE : JNI_FALSE); }
395 
396   // Thread local event queue, which doesn't require taking the Service_lock.
397   void enqueue_event(JvmtiDeferredEvent* event) NOT_JVMTI_RETURN;
398   void post_events(JvmtiEnv* env);
399   void run_nmethod_entry_barriers();
400 };
401 
402 #endif // SHARE_PRIMS_JVMTITHREADSTATE_HPP