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