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