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