|
1 /* |
|
2 * Copyright 2003-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 # include "incls/_precompiled.incl" |
|
26 # include "incls/_jvmtiThreadState.cpp.incl" |
|
27 |
|
28 // marker for when the stack depth has been reset and is now unknown. |
|
29 // any negative number would work but small ones might obscure an |
|
30 // underrun error. |
|
31 static const int UNKNOWN_STACK_DEPTH = -99; |
|
32 |
|
33 /////////////////////////////////////////////////////////////// |
|
34 // |
|
35 // class JvmtiThreadState |
|
36 // |
|
37 // Instances of JvmtiThreadState hang off of each thread. |
|
38 // Thread local storage for JVMTI. |
|
39 // |
|
40 |
|
41 JvmtiThreadState *JvmtiThreadState::_head = NULL; |
|
42 |
|
43 JvmtiThreadState::JvmtiThreadState(JavaThread* thread) |
|
44 : _thread_event_enable() { |
|
45 assert(JvmtiThreadState_lock->is_locked(), "sanity check"); |
|
46 _thread = thread; |
|
47 _exception_detected = false; |
|
48 _exception_caught = false; |
|
49 _debuggable = true; |
|
50 _hide_single_stepping = false; |
|
51 _hide_level = 0; |
|
52 _pending_step_for_popframe = false; |
|
53 _class_being_redefined = NULL; |
|
54 _class_load_kind = jvmti_class_load_kind_load; |
|
55 _head_env_thread_state = NULL; |
|
56 _dynamic_code_event_collector = NULL; |
|
57 _vm_object_alloc_event_collector = NULL; |
|
58 _the_class_for_redefinition_verification = NULL; |
|
59 _scratch_class_for_redefinition_verification = NULL; |
|
60 |
|
61 // JVMTI ForceEarlyReturn support |
|
62 _pending_step_for_earlyret = false; |
|
63 _earlyret_state = earlyret_inactive; |
|
64 _earlyret_tos = ilgl; |
|
65 _earlyret_value.j = 0L; |
|
66 _earlyret_oop = NULL; |
|
67 |
|
68 // add all the JvmtiEnvThreadState to the new JvmtiThreadState |
|
69 { |
|
70 JvmtiEnvIterator it; |
|
71 for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) { |
|
72 if (env->is_valid()) { |
|
73 add_env(env); |
|
74 } |
|
75 } |
|
76 } |
|
77 |
|
78 // link us into the list |
|
79 { |
|
80 // The thread state list manipulation code must not have safepoints. |
|
81 // See periodic_clean_up(). |
|
82 debug_only(No_Safepoint_Verifier nosafepoint;) |
|
83 |
|
84 _prev = NULL; |
|
85 _next = _head; |
|
86 if (_head != NULL) { |
|
87 _head->_prev = this; |
|
88 } |
|
89 _head = this; |
|
90 } |
|
91 |
|
92 // set this as the state for the thread |
|
93 thread->set_jvmti_thread_state(this); |
|
94 } |
|
95 |
|
96 |
|
97 JvmtiThreadState::~JvmtiThreadState() { |
|
98 assert(JvmtiThreadState_lock->is_locked(), "sanity check"); |
|
99 |
|
100 // clear this as the state for the thread |
|
101 get_thread()->set_jvmti_thread_state(NULL); |
|
102 |
|
103 // zap our env thread states |
|
104 { |
|
105 JvmtiEnvBase::entering_dying_thread_env_iteration(); |
|
106 JvmtiEnvThreadStateIterator it(this); |
|
107 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ) { |
|
108 JvmtiEnvThreadState* zap = ets; |
|
109 ets = it.next(ets); |
|
110 delete zap; |
|
111 } |
|
112 JvmtiEnvBase::leaving_dying_thread_env_iteration(); |
|
113 } |
|
114 |
|
115 // remove us from the list |
|
116 { |
|
117 // The thread state list manipulation code must not have safepoints. |
|
118 // See periodic_clean_up(). |
|
119 debug_only(No_Safepoint_Verifier nosafepoint;) |
|
120 |
|
121 if (_prev == NULL) { |
|
122 assert(_head == this, "sanity check"); |
|
123 _head = _next; |
|
124 } else { |
|
125 assert(_head != this, "sanity check"); |
|
126 _prev->_next = _next; |
|
127 } |
|
128 if (_next != NULL) { |
|
129 _next->_prev = _prev; |
|
130 } |
|
131 _next = NULL; |
|
132 _prev = NULL; |
|
133 } |
|
134 } |
|
135 |
|
136 |
|
137 void |
|
138 JvmtiThreadState::periodic_clean_up() { |
|
139 assert(SafepointSynchronize::is_at_safepoint(), "at safepoint"); |
|
140 |
|
141 // This iteration is initialized with "_head" instead of "JvmtiThreadState::first()" |
|
142 // because the latter requires the JvmtiThreadState_lock. |
|
143 // This iteration is safe at a safepoint as well, see the No_Safepoint_Verifier |
|
144 // asserts at all list manipulation sites. |
|
145 for (JvmtiThreadState *state = _head; state != NULL; state = state->next()) { |
|
146 // For each environment thread state corresponding to an invalid environment |
|
147 // unlink it from the list and deallocate it. |
|
148 JvmtiEnvThreadStateIterator it(state); |
|
149 JvmtiEnvThreadState* previous_ets = NULL; |
|
150 JvmtiEnvThreadState* ets = it.first(); |
|
151 while (ets != NULL) { |
|
152 if (ets->get_env()->is_valid()) { |
|
153 previous_ets = ets; |
|
154 ets = it.next(ets); |
|
155 } else { |
|
156 // This one isn't valid, remove it from the list and deallocate it |
|
157 JvmtiEnvThreadState* defunct_ets = ets; |
|
158 ets = ets->next(); |
|
159 if (previous_ets == NULL) { |
|
160 assert(state->head_env_thread_state() == defunct_ets, "sanity check"); |
|
161 state->set_head_env_thread_state(ets); |
|
162 } else { |
|
163 previous_ets->set_next(ets); |
|
164 } |
|
165 delete defunct_ets; |
|
166 } |
|
167 } |
|
168 } |
|
169 } |
|
170 |
|
171 void JvmtiThreadState::add_env(JvmtiEnvBase *env) { |
|
172 assert(JvmtiThreadState_lock->is_locked(), "sanity check"); |
|
173 |
|
174 JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(_thread, env); |
|
175 // add this environment thread state to the end of the list (order is important) |
|
176 { |
|
177 // list deallocation (which occurs at a safepoint) cannot occur simultaneously |
|
178 debug_only(No_Safepoint_Verifier nosafepoint;) |
|
179 |
|
180 JvmtiEnvThreadStateIterator it(this); |
|
181 JvmtiEnvThreadState* previous_ets = NULL; |
|
182 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { |
|
183 previous_ets = ets; |
|
184 } |
|
185 if (previous_ets == NULL) { |
|
186 set_head_env_thread_state(new_ets); |
|
187 } else { |
|
188 previous_ets->set_next(new_ets); |
|
189 } |
|
190 } |
|
191 } |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 void JvmtiThreadState::enter_interp_only_mode() { |
|
197 assert(_thread->get_interp_only_mode() == 0, "entering interp only when mode not zero"); |
|
198 _thread->increment_interp_only_mode(); |
|
199 } |
|
200 |
|
201 |
|
202 void JvmtiThreadState::leave_interp_only_mode() { |
|
203 assert(_thread->get_interp_only_mode() == 1, "leaving interp only when mode not one"); |
|
204 _thread->decrement_interp_only_mode(); |
|
205 } |
|
206 |
|
207 |
|
208 // Helper routine used in several places |
|
209 int JvmtiThreadState::count_frames() { |
|
210 #ifdef ASSERT |
|
211 uint32_t debug_bits = 0; |
|
212 #endif |
|
213 assert(SafepointSynchronize::is_at_safepoint() || |
|
214 JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), |
|
215 "at safepoint or must be suspended"); |
|
216 |
|
217 if (!get_thread()->has_last_Java_frame()) return 0; // no Java frames |
|
218 |
|
219 ResourceMark rm; |
|
220 RegisterMap reg_map(get_thread()); |
|
221 javaVFrame *jvf = get_thread()->last_java_vframe(®_map); |
|
222 int n = 0; |
|
223 // tty->print_cr("CSD: counting frames on %s ...", |
|
224 // JvmtiTrace::safe_get_thread_name(get_thread())); |
|
225 while (jvf != NULL) { |
|
226 methodOop method = jvf->method(); |
|
227 // tty->print_cr("CSD: frame - method %s.%s - loc %d", |
|
228 // method->klass_name()->as_C_string(), |
|
229 // method->name()->as_C_string(), |
|
230 // jvf->bci() ); |
|
231 jvf = jvf->java_sender(); |
|
232 n++; |
|
233 } |
|
234 // tty->print_cr("CSD: frame count: %d", n); |
|
235 return n; |
|
236 } |
|
237 |
|
238 |
|
239 void JvmtiThreadState::invalidate_cur_stack_depth() { |
|
240 Thread *cur = Thread::current(); |
|
241 uint32_t debug_bits = 0; |
|
242 |
|
243 // The caller can be the VMThread at a safepoint, the current thread |
|
244 // or the target thread must be suspended. |
|
245 guarantee((cur->is_VM_thread() && SafepointSynchronize::is_at_safepoint()) || |
|
246 (JavaThread *)cur == get_thread() || |
|
247 JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), |
|
248 "sanity check"); |
|
249 |
|
250 _cur_stack_depth = UNKNOWN_STACK_DEPTH; |
|
251 } |
|
252 |
|
253 void JvmtiThreadState::incr_cur_stack_depth() { |
|
254 guarantee(JavaThread::current() == get_thread(), "must be current thread"); |
|
255 |
|
256 if (!is_interp_only_mode()) { |
|
257 _cur_stack_depth = UNKNOWN_STACK_DEPTH; |
|
258 } |
|
259 if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) { |
|
260 ++_cur_stack_depth; |
|
261 } |
|
262 } |
|
263 |
|
264 void JvmtiThreadState::decr_cur_stack_depth() { |
|
265 guarantee(JavaThread::current() == get_thread(), "must be current thread"); |
|
266 |
|
267 if (!is_interp_only_mode()) { |
|
268 _cur_stack_depth = UNKNOWN_STACK_DEPTH; |
|
269 } |
|
270 if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) { |
|
271 --_cur_stack_depth; |
|
272 assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch"); |
|
273 } |
|
274 } |
|
275 |
|
276 int JvmtiThreadState::cur_stack_depth() { |
|
277 uint32_t debug_bits = 0; |
|
278 guarantee(JavaThread::current() == get_thread() || |
|
279 JvmtiEnv::is_thread_fully_suspended(get_thread(), false, &debug_bits), |
|
280 "must be current thread or suspended"); |
|
281 |
|
282 if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) { |
|
283 _cur_stack_depth = count_frames(); |
|
284 } else { |
|
285 // heavy weight assert |
|
286 assert(_cur_stack_depth == count_frames(), |
|
287 "cur_stack_depth out of sync"); |
|
288 } |
|
289 return _cur_stack_depth; |
|
290 } |
|
291 |
|
292 bool JvmtiThreadState::may_be_walked() { |
|
293 return (get_thread()->is_being_ext_suspended() || (JavaThread::current() == get_thread())); |
|
294 } |
|
295 |
|
296 |
|
297 void JvmtiThreadState::process_pending_step_for_popframe() { |
|
298 // We are single stepping as the last part of the PopFrame() dance |
|
299 // so we have some house keeping to do. |
|
300 |
|
301 JavaThread *thr = get_thread(); |
|
302 if (thr->popframe_condition() != JavaThread::popframe_inactive) { |
|
303 // If the popframe_condition field is not popframe_inactive, then |
|
304 // we missed all of the popframe_field cleanup points: |
|
305 // |
|
306 // - unpack_frames() was not called (nothing to deopt) |
|
307 // - remove_activation_preserving_args_entry() was not called |
|
308 // (did not get suspended in a call_vm() family call and did |
|
309 // not complete a call_vm() family call on the way here) |
|
310 thr->clear_popframe_condition(); |
|
311 } |
|
312 |
|
313 // clearing the flag indicates we are done with the PopFrame() dance |
|
314 clr_pending_step_for_popframe(); |
|
315 |
|
316 // If step is pending for popframe then it may not be |
|
317 // a repeat step. The new_bci and method_id is same as current_bci |
|
318 // and current method_id after pop and step for recursive calls. |
|
319 // Force the step by clearing the last location. |
|
320 JvmtiEnvThreadStateIterator it(this); |
|
321 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { |
|
322 ets->clear_current_location(); |
|
323 } |
|
324 } |
|
325 |
|
326 |
|
327 // Class: JvmtiThreadState |
|
328 // Function: update_for_pop_top_frame |
|
329 // Description: |
|
330 // This function removes any frame pop notification request for |
|
331 // the top frame and invalidates both the current stack depth and |
|
332 // all cached frameIDs. |
|
333 // |
|
334 // Called by: PopFrame |
|
335 // |
|
336 void JvmtiThreadState::update_for_pop_top_frame() { |
|
337 if (is_interp_only_mode()) { |
|
338 // remove any frame pop notification request for the top frame |
|
339 // in any environment |
|
340 int popframe_number = cur_stack_depth(); |
|
341 { |
|
342 JvmtiEnvThreadStateIterator it(this); |
|
343 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { |
|
344 if (ets->is_frame_pop(popframe_number)) { |
|
345 ets->clear_frame_pop(popframe_number); |
|
346 } |
|
347 } |
|
348 } |
|
349 // force stack depth to be recalculated |
|
350 invalidate_cur_stack_depth(); |
|
351 } else { |
|
352 assert(!is_enabled(JVMTI_EVENT_FRAME_POP), "Must have no framepops set"); |
|
353 } |
|
354 } |
|
355 |
|
356 |
|
357 void JvmtiThreadState::process_pending_step_for_earlyret() { |
|
358 // We are single stepping as the last part of the ForceEarlyReturn |
|
359 // dance so we have some house keeping to do. |
|
360 |
|
361 if (is_earlyret_pending()) { |
|
362 // If the earlyret_state field is not earlyret_inactive, then |
|
363 // we missed all of the earlyret_field cleanup points: |
|
364 // |
|
365 // - remove_activation() was not called |
|
366 // (did not get suspended in a call_vm() family call and did |
|
367 // not complete a call_vm() family call on the way here) |
|
368 // |
|
369 // One legitimate way for us to miss all the cleanup points is |
|
370 // if we got here right after handling a compiled return. If that |
|
371 // is the case, then we consider our return from compiled code to |
|
372 // complete the ForceEarlyReturn request and we clear the condition. |
|
373 clr_earlyret_pending(); |
|
374 set_earlyret_oop(NULL); |
|
375 clr_earlyret_value(); |
|
376 } |
|
377 |
|
378 // clearing the flag indicates we are done with |
|
379 // the ForceEarlyReturn() dance |
|
380 clr_pending_step_for_earlyret(); |
|
381 |
|
382 // If step is pending for earlyret then it may not be a repeat step. |
|
383 // The new_bci and method_id is same as current_bci and current |
|
384 // method_id after earlyret and step for recursive calls. |
|
385 // Force the step by clearing the last location. |
|
386 JvmtiEnvThreadStateIterator it(this); |
|
387 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) { |
|
388 ets->clear_current_location(); |
|
389 } |
|
390 } |
|
391 |
|
392 void JvmtiThreadState::oops_do(OopClosure* f) { |
|
393 f->do_oop((oop*) &_earlyret_oop); |
|
394 } |