src/share/vm/prims/jvmtiThreadState.hpp

changeset 435
a61af66fc99e
child 1043
0386097d43d8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/prims/jvmtiThreadState.hpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,396 @@
     1.4 +/*
     1.5 + * Copyright 2003-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef _JAVA_JVMTITHREADSTATE_H_
    1.29 +#define _JAVA_JVMTITHREADSTATE_H_
    1.30 +
    1.31 +//
    1.32 +// Forward Declarations
    1.33 +//
    1.34 +
    1.35 +class JvmtiEnvBase;
    1.36 +class JvmtiEnvThreadState;
    1.37 +class JvmtiDynamicCodeEventCollector;
    1.38 +
    1.39 +enum JvmtiClassLoadKind {
    1.40 +  jvmti_class_load_kind_load = 100,
    1.41 +  jvmti_class_load_kind_retransform,
    1.42 +  jvmti_class_load_kind_redefine
    1.43 +};
    1.44 +
    1.45 +///////////////////////////////////////////////////////////////
    1.46 +//
    1.47 +// class JvmtiEnvThreadStateIterator
    1.48 +//
    1.49 +// The only safe means of iterating through the JvmtiEnvThreadStates
    1.50 +// in a JvmtiThreadState.
    1.51 +// Note that this iteratation includes invalid environments pending
    1.52 +// deallocation -- in fact, some uses depend on this behavior.
    1.53 +//
    1.54 +class JvmtiEnvThreadStateIterator : public StackObj {
    1.55 + private:
    1.56 +  JvmtiThreadState* state;
    1.57 + public:
    1.58 +  JvmtiEnvThreadStateIterator(JvmtiThreadState* thread_state);
    1.59 +  ~JvmtiEnvThreadStateIterator();
    1.60 +  JvmtiEnvThreadState* first();
    1.61 +  JvmtiEnvThreadState* next(JvmtiEnvThreadState* ets);
    1.62 +};
    1.63 +
    1.64 +
    1.65 +///////////////////////////////////////////////////////////////
    1.66 +//
    1.67 +// class JvmtiThreadState
    1.68 +//
    1.69 +// The Jvmti state for each thread (across all JvmtiEnv):
    1.70 +// 1. Local table of enabled events.
    1.71 +class JvmtiThreadState : public CHeapObj {
    1.72 + private:
    1.73 +  friend class JvmtiEnv;
    1.74 +  JavaThread        *_thread;
    1.75 +  bool              _exception_detected;
    1.76 +  bool              _exception_caught;
    1.77 +  bool              _hide_single_stepping;
    1.78 +  bool              _pending_step_for_popframe;
    1.79 +  bool              _pending_step_for_earlyret;
    1.80 +  int               _hide_level;
    1.81 +
    1.82 +  // Used to send class being redefined/retransformed and kind of transform
    1.83 +  // info to the class file load hook event handler.
    1.84 +  KlassHandle           *_class_being_redefined;
    1.85 +  JvmtiClassLoadKind    _class_load_kind;
    1.86 +
    1.87 +  // This is only valid when is_interp_only_mode() returns true
    1.88 +  int               _cur_stack_depth;
    1.89 +
    1.90 +  JvmtiThreadEventEnable _thread_event_enable;
    1.91 +
    1.92 +  // for support of JvmtiEnvThreadState
    1.93 +  JvmtiEnvThreadState*   _head_env_thread_state;
    1.94 +
    1.95 +  // doubly-linked linear list of active thread state
    1.96 +  // needed in order to iterate the list without holding Threads_lock
    1.97 +  static JvmtiThreadState *_head;
    1.98 +  JvmtiThreadState *_next;
    1.99 +  JvmtiThreadState *_prev;
   1.100 +
   1.101 +  // holds the current dynamic code event collector, NULL if no event collector in use
   1.102 +  JvmtiDynamicCodeEventCollector* _dynamic_code_event_collector;
   1.103 +  // holds the current vm object alloc event collector, NULL if no event collector in use
   1.104 +  JvmtiVMObjectAllocEventCollector* _vm_object_alloc_event_collector;
   1.105 +
   1.106 +  // Should only be created by factory methods
   1.107 +  JvmtiThreadState(JavaThread *thread);
   1.108 +
   1.109 +  friend class JvmtiEnvThreadStateIterator;
   1.110 +  inline JvmtiEnvThreadState* head_env_thread_state();
   1.111 +  inline void set_head_env_thread_state(JvmtiEnvThreadState* ets);
   1.112 +
   1.113 + public:
   1.114 +  ~JvmtiThreadState();
   1.115 +
   1.116 +  // is event_type enabled and usable for this thread in any enviroments?
   1.117 +  bool is_enabled(jvmtiEvent event_type) {
   1.118 +    return _thread_event_enable.is_enabled(event_type);
   1.119 +  }
   1.120 +
   1.121 +  JvmtiThreadEventEnable *thread_event_enable() {
   1.122 +    return &_thread_event_enable;
   1.123 +  }
   1.124 +
   1.125 +  // Must only be called in situations where the state is for the current thread and
   1.126 +  // the environment can not go away.  To be safe, the returned JvmtiEnvThreadState
   1.127 +  // must be used in such a way as there can be no intervening safepoints.
   1.128 +  inline JvmtiEnvThreadState* env_thread_state(JvmtiEnvBase *env);
   1.129 +
   1.130 +  static void periodic_clean_up();
   1.131 +
   1.132 +  void add_env(JvmtiEnvBase *env);
   1.133 +
   1.134 +  // Used by the interpreter for fullspeed debugging support
   1.135 +  bool is_interp_only_mode()                { return _thread->is_interp_only_mode(); }
   1.136 +  void enter_interp_only_mode();
   1.137 +  void leave_interp_only_mode();
   1.138 +
   1.139 +  // access to the linked list of all JVMTI thread states
   1.140 +  static JvmtiThreadState *first() {
   1.141 +    assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
   1.142 +    return _head;
   1.143 +  }
   1.144 +
   1.145 +  JvmtiThreadState *next()                  {
   1.146 +    return _next;
   1.147 +  }
   1.148 +
   1.149 +  // Current stack depth is only valid when is_interp_only_mode() returns true.
   1.150 +  // These functions should only be called at a safepoint - usually called from same thread.
   1.151 +  // Returns the number of Java activations on the stack.
   1.152 +  int cur_stack_depth();
   1.153 +  void invalidate_cur_stack_depth();
   1.154 +  void incr_cur_stack_depth();
   1.155 +  void decr_cur_stack_depth();
   1.156 +
   1.157 +  int count_frames();
   1.158 +
   1.159 +  inline JavaThread *get_thread()      { return _thread;              }
   1.160 +  inline bool is_exception_detected()  { return _exception_detected;  }
   1.161 +  inline bool is_exception_caught()    { return _exception_caught;  }
   1.162 +  inline void set_exception_detected() { _exception_detected = true;
   1.163 +                                         _exception_caught = false; }
   1.164 +  inline void set_exception_caught()   { _exception_caught = true;
   1.165 +                                         _exception_detected = false; }
   1.166 +
   1.167 +  inline void clear_hide_single_stepping() {
   1.168 +    if (_hide_level > 0) {
   1.169 +      _hide_level--;
   1.170 +    } else {
   1.171 +      assert(_hide_single_stepping, "hide_single_stepping is out of phase");
   1.172 +      _hide_single_stepping = false;
   1.173 +    }
   1.174 +  }
   1.175 +  inline bool hide_single_stepping() { return _hide_single_stepping; }
   1.176 +  inline void set_hide_single_stepping() {
   1.177 +    if (_hide_single_stepping) {
   1.178 +      _hide_level++;
   1.179 +    } else {
   1.180 +      assert(_hide_level == 0, "hide_level is out of phase");
   1.181 +      _hide_single_stepping = true;
   1.182 +    }
   1.183 +  }
   1.184 +
   1.185 +  // Step pending flag is set when PopFrame is called and it is cleared
   1.186 +  // when step for the Pop Frame is completed.
   1.187 +  // This logic is used to distinguish b/w step for pop frame and repeat step.
   1.188 +  void set_pending_step_for_popframe() { _pending_step_for_popframe = true;  }
   1.189 +  void clr_pending_step_for_popframe() { _pending_step_for_popframe = false; }
   1.190 +  bool is_pending_step_for_popframe()  { return _pending_step_for_popframe;  }
   1.191 +  void process_pending_step_for_popframe();
   1.192 +
   1.193 +  // Step pending flag is set when ForceEarlyReturn is called and it is cleared
   1.194 +  // when step for the ForceEarlyReturn is completed.
   1.195 +  // This logic is used to distinguish b/w step for early return and repeat step.
   1.196 +  void set_pending_step_for_earlyret() { _pending_step_for_earlyret = true;  }
   1.197 +  void clr_pending_step_for_earlyret() { _pending_step_for_earlyret = false; }
   1.198 +  bool is_pending_step_for_earlyret()  { return _pending_step_for_earlyret;  }
   1.199 +  void process_pending_step_for_earlyret();
   1.200 +
   1.201 +  // Setter and getter method is used to send redefined class info
   1.202 +  // when class file load hook event is posted.
   1.203 +  // It is set while loading redefined class and cleared before the
   1.204 +  // class file load hook event is posted.
   1.205 +  inline void set_class_being_redefined(KlassHandle *h_class, JvmtiClassLoadKind kind) {
   1.206 +    _class_being_redefined = h_class;
   1.207 +    _class_load_kind = kind;
   1.208 +  }
   1.209 +
   1.210 +  inline void clear_class_being_redefined() {
   1.211 +    _class_being_redefined = NULL;
   1.212 +    _class_load_kind = jvmti_class_load_kind_load;
   1.213 +  }
   1.214 +
   1.215 +  inline KlassHandle *get_class_being_redefined() {
   1.216 +    return _class_being_redefined;
   1.217 +  }
   1.218 +
   1.219 +  inline JvmtiClassLoadKind get_class_load_kind() {
   1.220 +    return _class_load_kind;
   1.221 +  }
   1.222 +
   1.223 +  // RedefineClasses support
   1.224 +  // The bug 6214132 caused the verification to fail.
   1.225 +  //
   1.226 +  // Below is the detailed description of the fix approach taken:
   1.227 +  // 1. What's done in RedefineClasses() before verification:
   1.228 +  //  a) A reference to the class being redefined (_the_class) and a
   1.229 +  //     reference to new version of the class (_scratch_class) are
   1.230 +  //     saved here for use during the bytecode verification phase of
   1.231 +  //     RedefineClasses. See RedefineVerifyMark for how these fields
   1.232 +  //     are managed.
   1.233 +  //   b) The _java_mirror field from _the_class is copied to the
   1.234 +  //     _java_mirror field in _scratch_class. This means that a jclass
   1.235 +  //     returned for _the_class or _scratch_class will refer to the
   1.236 +  //     same Java mirror. The verifier will see the "one true mirror"
   1.237 +  //     for the class being verified.
   1.238 +  // 2. What is done at verification:
   1.239 +  //   When the verifier makes calls into the VM to ask questions about
   1.240 +  //   the class being verified, it will pass the jclass to JVM_* functions.
   1.241 +  //   The jclass is always pointing to the mirror of _the_class.
   1.242 +  //   ~28 JVM_* functions called by the verifier for the information
   1.243 +  //   about CP entries and klass structure should check the jvmtiThreadState
   1.244 +  //   info about equivalent klass versions and use it to replace a klassOop
   1.245 +  //   of _the_class with a klassOop of _scratch_class. The function
   1.246 +  //   class_to_verify_considering_redefinition() must be called for it.
   1.247 +  //
   1.248 +  //   Note again, that this redirection happens only for the verifier thread.
   1.249 +  //   Other threads have very small overhead by checking the existence
   1.250 +  //   of the jvmtiThreadSate and the information about klasses equivalence.
   1.251 +  //   No JNI functions need to be changed, they don't reference the klass guts.
   1.252 +  //   The JavaThread pointer is already available in all JVM_* functions
   1.253 +  //   used by the verifier, so there is no extra performance issue with it.
   1.254 +
   1.255 + private:
   1.256 +  KlassHandle *_the_class_for_redefinition_verification;
   1.257 +  KlassHandle *_scratch_class_for_redefinition_verification;
   1.258 +
   1.259 + public:
   1.260 +  inline void set_class_versions_map(KlassHandle *the_class,
   1.261 +                                     KlassHandle *scratch_class) {
   1.262 +    _the_class_for_redefinition_verification = the_class;
   1.263 +    _scratch_class_for_redefinition_verification = scratch_class;
   1.264 +  }
   1.265 +
   1.266 +  inline void clear_class_versions_map() { set_class_versions_map(NULL, NULL); }
   1.267 +
   1.268 +  static inline
   1.269 +  klassOop class_to_verify_considering_redefinition(klassOop klass,
   1.270 +                                                    JavaThread *thread) {
   1.271 +    JvmtiThreadState *state = thread->jvmti_thread_state();
   1.272 +    if (state != NULL && state->_the_class_for_redefinition_verification != NULL) {
   1.273 +      if ((*(state->_the_class_for_redefinition_verification))() == klass) {
   1.274 +        klass = (*(state->_scratch_class_for_redefinition_verification))();
   1.275 +      }
   1.276 +    }
   1.277 +    return klass;
   1.278 +  }
   1.279 +
   1.280 +  // Todo: get rid of this!
   1.281 + private:
   1.282 +  bool _debuggable;
   1.283 + public:
   1.284 +  // Should the thread be enumerated by jvmtiInternal::GetAllThreads?
   1.285 +  bool is_debuggable()                 { return _debuggable; }
   1.286 +  // If a thread cannot be suspended (has no valid last_java_frame) then it gets marked !debuggable
   1.287 +  void set_debuggable(bool debuggable) { _debuggable = debuggable; }
   1.288 +
   1.289 + public:
   1.290 +
   1.291 +  bool may_be_walked();
   1.292 +
   1.293 +  // Thread local event collector setter and getter methods.
   1.294 +  JvmtiDynamicCodeEventCollector* get_dynamic_code_event_collector() {
   1.295 +    return _dynamic_code_event_collector;
   1.296 +  }
   1.297 +  JvmtiVMObjectAllocEventCollector* get_vm_object_alloc_event_collector() {
   1.298 +    return _vm_object_alloc_event_collector;
   1.299 +  }
   1.300 +  void set_dynamic_code_event_collector(JvmtiDynamicCodeEventCollector* collector) {
   1.301 +    _dynamic_code_event_collector = collector;
   1.302 +  }
   1.303 +  void set_vm_object_alloc_event_collector(JvmtiVMObjectAllocEventCollector* collector) {
   1.304 +    _vm_object_alloc_event_collector = collector;
   1.305 +  }
   1.306 +
   1.307 +
   1.308 +  //
   1.309 +  // Frame routines
   1.310 +  //
   1.311 +
   1.312 + public:
   1.313 +
   1.314 +  //  true when the thread was suspended with a pointer to the last Java frame.
   1.315 +  bool has_last_frame()                     { return _thread->has_last_Java_frame(); }
   1.316 +
   1.317 +  void update_for_pop_top_frame();
   1.318 +
   1.319 +  // already holding JvmtiThreadState_lock - retrieve or create JvmtiThreadState
   1.320 +  inline static JvmtiThreadState *state_for_while_locked(JavaThread *thread) {
   1.321 +    assert(JvmtiThreadState_lock->is_locked(), "sanity check");
   1.322 +
   1.323 +    JvmtiThreadState *state = thread->jvmti_thread_state();
   1.324 +    if (state == NULL) {
   1.325 +      state = new JvmtiThreadState(thread);
   1.326 +    }
   1.327 +    return state;
   1.328 +  }
   1.329 +
   1.330 +  // retrieve or create JvmtiThreadState
   1.331 +  inline static JvmtiThreadState *state_for(JavaThread *thread) {
   1.332 +    JvmtiThreadState *state = thread->jvmti_thread_state();
   1.333 +    if (state == NULL) {
   1.334 +      MutexLocker mu(JvmtiThreadState_lock);
   1.335 +      // check again with the lock held
   1.336 +      state = state_for_while_locked(thread);
   1.337 +    } else {
   1.338 +      CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
   1.339 +    }
   1.340 +    return state;
   1.341 +  }
   1.342 +
   1.343 +  // JVMTI ForceEarlyReturn support
   1.344 +
   1.345 +  // This is set to earlyret_pending to signal that top Java frame
   1.346 +  // should be returned immediately
   1.347 + public:
   1.348 +  int           _earlyret_state;
   1.349 +  TosState      _earlyret_tos;
   1.350 +  jvalue        _earlyret_value;
   1.351 +  oop           _earlyret_oop;         // Used to return an oop result into Java code from
   1.352 +                                       // ForceEarlyReturnObject, GC-preserved
   1.353 +
   1.354 +  // Setting and clearing earlyret_state
   1.355 +  // earlyret_pending indicates that a ForceEarlyReturn() has been
   1.356 +  // requested and not yet been completed.
   1.357 + public:
   1.358 +  enum EarlyretState {
   1.359 +    earlyret_inactive = 0,
   1.360 +    earlyret_pending  = 1
   1.361 +  };
   1.362 +
   1.363 +  void set_earlyret_pending(void) { _earlyret_state = earlyret_pending;  }
   1.364 +  void clr_earlyret_pending(void) { _earlyret_state = earlyret_inactive; }
   1.365 +  bool is_earlyret_pending(void)  { return (_earlyret_state == earlyret_pending);  }
   1.366 +
   1.367 +  TosState earlyret_tos()                            { return _earlyret_tos; }
   1.368 +  oop  earlyret_oop() const                          { return _earlyret_oop; }
   1.369 +  void set_earlyret_oop (oop x)                      { _earlyret_oop = x;    }
   1.370 +  jvalue earlyret_value()                            { return _earlyret_value; }
   1.371 +  void set_earlyret_value(jvalue val, TosState tos)  { _earlyret_tos = tos;  _earlyret_value = val;  }
   1.372 +  void clr_earlyret_value()                          { _earlyret_tos = ilgl; _earlyret_value.j = 0L; }
   1.373 +
   1.374 +  static ByteSize earlyret_state_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_state); }
   1.375 +  static ByteSize earlyret_tos_offset()   { return byte_offset_of(JvmtiThreadState, _earlyret_tos); }
   1.376 +  static ByteSize earlyret_oop_offset()   { return byte_offset_of(JvmtiThreadState, _earlyret_oop); }
   1.377 +  static ByteSize earlyret_value_offset() { return byte_offset_of(JvmtiThreadState, _earlyret_value); }
   1.378 +
   1.379 +  void oops_do(OopClosure* f); // GC support
   1.380 +};
   1.381 +
   1.382 +class RedefineVerifyMark : public StackObj {
   1.383 + private:
   1.384 +  JvmtiThreadState *_state;
   1.385 +
   1.386 + public:
   1.387 +  RedefineVerifyMark(KlassHandle *the_class, KlassHandle *scratch_class,
   1.388 +                     JvmtiThreadState *state) : _state(state)
   1.389 +  {
   1.390 +    _state->set_class_versions_map(the_class, scratch_class);
   1.391 +    (*scratch_class)->set_java_mirror((*the_class)->java_mirror());
   1.392 +  }
   1.393 +
   1.394 +  ~RedefineVerifyMark() {
   1.395 +    _state->clear_class_versions_map();
   1.396 +  }
   1.397 +};
   1.398 +
   1.399 +#endif   /* _JAVA_JVMTITHREADSTATE_H_ */

mercurial