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