< prev index next >

src/hotspot/share/prims/jvmtiEnvThreadState.cpp

Print this page

 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 #include "precompiled.hpp"
 26 #include "interpreter/interpreter.hpp"
 27 #include "jvmtifiles/jvmtiEnv.hpp"
 28 #include "memory/resourceArea.hpp"
 29 #include "prims/jvmtiEnvThreadState.hpp"

 30 #include "prims/jvmtiEventController.inline.hpp"
 31 #include "prims/jvmtiImpl.hpp"
 32 #include "runtime/handles.hpp"
 33 #include "runtime/handles.inline.hpp"
 34 #include "runtime/interfaceSupport.inline.hpp"
 35 #include "runtime/javaCalls.hpp"
 36 #include "runtime/signature.hpp"
 37 #include "runtime/thread.inline.hpp"
 38 #include "runtime/vframe.hpp"
 39 #include "runtime/vmOperations.hpp"
 40 
 41 
 42 ///////////////////////////////////////////////////////////////
 43 //
 44 // class JvmtiFramePop
 45 //
 46 
 47 #ifndef PRODUCT
 48 void JvmtiFramePop::print() {
 49   tty->print_cr("_frame_number=%d", _frame_number);

109   ResourceMark rm;
110 
111   int n = _pops->length();
112   for (int i=0; i<n; i++) {
113     JvmtiFramePop fp = JvmtiFramePop(_pops->at(i));
114     tty->print("%d: ", i);
115     fp.print();
116     tty->cr();
117   }
118 }
119 #endif
120 
121 ///////////////////////////////////////////////////////////////
122 //
123 // class JvmtiEnvThreadState
124 //
125 // Instances of JvmtiEnvThreadState hang off of each JvmtiThreadState,
126 // one per JvmtiEnv.
127 //
128 
129 JvmtiEnvThreadState::JvmtiEnvThreadState(JavaThread *thread, JvmtiEnvBase *env) :
130   _event_enable() {
131   _thread                 = thread;
132   _env                    = (JvmtiEnv*)env;
133   _next                   = NULL;
134   _frame_pops             = NULL;
135   _current_bci            = 0;
136   _current_method_id      = NULL;
137   _breakpoint_posted      = false;
138   _single_stepping_posted = false;
139   _agent_thread_local_storage_data = NULL;
140 }
141 
142 JvmtiEnvThreadState::~JvmtiEnvThreadState()   {
143   delete _frame_pops;
144   _frame_pops = NULL;
145 }
146 






















147 // Given that a new (potential) event has come in,
148 // maintain the current JVMTI location on a per-thread per-env basis
149 // and use it to filter out duplicate events:
150 // - instruction rewrites
151 // - breakpoint followed by single step
152 // - single step at a breakpoint
153 void JvmtiEnvThreadState::compare_and_set_current_location(Method* new_method,
154                                                            address new_location, jvmtiEvent event) {
155 
156   int new_bci = new_location - new_method->code_base();
157 
158   // The method is identified and stored as a jmethodID which is safe in this
159   // case because the class cannot be unloaded while a method is executing.
160   jmethodID new_method_id = new_method->jmethod_id();
161 
162   // the last breakpoint or single step was at this same location
163   if (_current_bci == new_bci && _current_method_id == new_method_id) {
164     switch (event) {
165     case JVMTI_EVENT_BREAKPOINT:
166       // Repeat breakpoint is complicated. If we previously posted a breakpoint

176       // This has been handled by clearing the location
177       _single_stepping_posted = true;
178       break;
179     default:
180       assert(false, "invalid event value passed");
181       break;
182     }
183     return;
184   }
185 
186   set_current_location(new_method_id, new_bci);
187   _breakpoint_posted = false;
188   _single_stepping_posted = false;
189 }
190 
191 
192 JvmtiFramePops* JvmtiEnvThreadState::get_frame_pops() {
193 #ifdef ASSERT
194   Thread *current = Thread::current();
195 #endif
196   assert(get_thread()->is_handshake_safe_for(current),
197          "frame pop data only accessible from same thread or direct handshake");
198   if (_frame_pops == NULL) {
199     _frame_pops = new JvmtiFramePops();
200     assert(_frame_pops != NULL, "_frame_pops != NULL");
201   }
202   return _frame_pops;
203 }
204 
205 
206 bool JvmtiEnvThreadState::has_frame_pops() {
207   return _frame_pops == NULL? false : (_frame_pops->length() > 0);
208 }
209 
210 void JvmtiEnvThreadState::set_frame_pop(int frame_number) {
211 #ifdef ASSERT
212   Thread *current = Thread::current();
213 #endif
214   assert(get_thread()->is_handshake_safe_for(current),
215          "frame pop data only accessible from same thread or direct handshake");
216   JvmtiFramePop fpop(frame_number);
217   JvmtiEventController::set_frame_pop(this, fpop);
218 }
219 
220 
221 void JvmtiEnvThreadState::clear_frame_pop(int frame_number) {
222 #ifdef ASSERT
223   Thread *current = Thread::current();
224 #endif
225   assert(get_thread()->is_handshake_safe_for(current),
226          "frame pop data only accessible from same thread or direct handshake");
227   JvmtiFramePop fpop(frame_number);
228   JvmtiEventController::clear_frame_pop(this, fpop);
229 }
230 
231 
232 bool JvmtiEnvThreadState::is_frame_pop(int cur_frame_number) {
233 #ifdef ASSERT
234   Thread *current = Thread::current();
235 #endif
236   assert(get_thread()->is_handshake_safe_for(current),
237          "frame pop data only accessible from same thread or direct handshake");
238   if (!get_thread()->is_interp_only_mode() || _frame_pops == NULL) {
239     return false;
240   }
241   JvmtiFramePop fp(cur_frame_number);
242   return get_frame_pops()->contains(fp);
243 }
244 






































245 
246 class GetCurrentLocationClosure : public HandshakeClosure {
247  private:
248    jmethodID _method_id;
249    int _bci;
250    bool _completed;
251  public:
252   GetCurrentLocationClosure()
253     : HandshakeClosure("GetCurrentLocation"),
254       _method_id(NULL),
255       _bci(0),
256       _completed(false) {}
257   void do_thread(Thread *target) {
258     JavaThread *jt = JavaThread::cast(target);
259     ResourceMark rmark; // jt != Thread::current()
260     RegisterMap rm(jt, false);
261     // There can be a race condition between a handshake
262     // and the target thread exiting from Java execution.
263     // We must recheck that the last Java frame still exists.
264     if (!jt->is_exiting() && jt->has_last_Java_frame()) {

291 
292   // If single-stepping is disabled, clear current location so that
293   // single-stepping to the same method and bcp at a later time will be
294   // detected if single-stepping is enabled at that time (see 4388912).
295 
296   // If single-stepping is enabled, set the current location to the
297   // current method and bcp. This covers the following type of case,
298   // e.g., the debugger stepi command:
299   // - bytecode single stepped
300   // - SINGLE_STEP event posted and SINGLE_STEP event disabled
301   // - SINGLE_STEP event reenabled
302   // - bytecode rewritten to fast version
303 
304   // If breakpoint event is disabled, clear current location only if
305   // single-stepping is not enabled.  Otherwise, keep the thread location
306   // to detect any duplicate events.
307 
308   if (enabled) {
309     // If enabling breakpoint, no need to reset.
310     // Can't do anything if empty stack.
311     if (event_type == JVMTI_EVENT_SINGLE_STEP && _thread->has_last_Java_frame()) {




















312       jmethodID method_id;
313       int bci;
314       // The java thread stack may not be walkable for a running thread
315       // so get current location with direct handshake.
316       GetCurrentLocationClosure op;
317       Thread *current = Thread::current();
318       if (_thread->is_handshake_safe_for(current)) {
319         op.do_thread(_thread);
320       } else {
321         Handshake::execute(&op, _thread);
322         guarantee(op.completed(), "Handshake failed. Target thread is not alive?");
323       }
324       op.get_current_location(&method_id, &bci);
325       set_current_location(method_id, bci);
326     }
327   } else if (event_type == JVMTI_EVENT_SINGLE_STEP || !is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
328     // If this is to disable breakpoint, also check if single-step is not enabled
329     clear_current_location();
330   }
331 }

 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 #include "precompiled.hpp"
 26 #include "interpreter/interpreter.hpp"
 27 #include "jvmtifiles/jvmtiEnv.hpp"
 28 #include "memory/resourceArea.hpp"
 29 #include "prims/jvmtiEnvThreadState.hpp"
 30 #include "prims/jvmtiThreadState.inline.hpp"
 31 #include "prims/jvmtiEventController.inline.hpp"
 32 #include "prims/jvmtiImpl.hpp"
 33 #include "runtime/handles.hpp"
 34 #include "runtime/handles.inline.hpp"
 35 #include "runtime/interfaceSupport.inline.hpp"
 36 #include "runtime/javaCalls.hpp"
 37 #include "runtime/signature.hpp"
 38 #include "runtime/thread.inline.hpp"
 39 #include "runtime/vframe.hpp"
 40 #include "runtime/vmOperations.hpp"
 41 
 42 
 43 ///////////////////////////////////////////////////////////////
 44 //
 45 // class JvmtiFramePop
 46 //
 47 
 48 #ifndef PRODUCT
 49 void JvmtiFramePop::print() {
 50   tty->print_cr("_frame_number=%d", _frame_number);

110   ResourceMark rm;
111 
112   int n = _pops->length();
113   for (int i=0; i<n; i++) {
114     JvmtiFramePop fp = JvmtiFramePop(_pops->at(i));
115     tty->print("%d: ", i);
116     fp.print();
117     tty->cr();
118   }
119 }
120 #endif
121 
122 ///////////////////////////////////////////////////////////////
123 //
124 // class JvmtiEnvThreadState
125 //
126 // Instances of JvmtiEnvThreadState hang off of each JvmtiThreadState,
127 // one per JvmtiEnv.
128 //
129 
130 JvmtiEnvThreadState::JvmtiEnvThreadState(JvmtiThreadState* state, JvmtiEnvBase *env) :
131   _event_enable() {
132   _state                  = state;
133   _env                    = (JvmtiEnv*)env;
134   _next                   = NULL;
135   _frame_pops             = NULL;
136   _current_bci            = 0;
137   _current_method_id      = NULL;
138   _breakpoint_posted      = false;
139   _single_stepping_posted = false;
140   _agent_thread_local_storage_data = NULL;
141 }
142 
143 JvmtiEnvThreadState::~JvmtiEnvThreadState()   {
144   delete _frame_pops;
145   _frame_pops = NULL;
146 }
147 
148 bool JvmtiEnvThreadState::is_virtual() {
149   return _state->is_virtual();
150 }
151 
152 // Use _thread_saved if cthread is detached from JavaThread (_thread == NULL).
153 JavaThread* JvmtiEnvThreadState::get_thread_or_saved() {
154   return _state->get_thread_or_saved();
155 }
156 
157 JavaThread* JvmtiEnvThreadState::get_thread() {
158   return _state->get_thread();
159 }
160 
161 void* JvmtiEnvThreadState::get_agent_thread_local_storage_data() {
162   return _agent_thread_local_storage_data;
163 }
164 
165 void JvmtiEnvThreadState::set_agent_thread_local_storage_data (void *data) {
166   _agent_thread_local_storage_data = data;
167 }
168 
169 
170 // Given that a new (potential) event has come in,
171 // maintain the current JVMTI location on a per-thread per-env basis
172 // and use it to filter out duplicate events:
173 // - instruction rewrites
174 // - breakpoint followed by single step
175 // - single step at a breakpoint
176 void JvmtiEnvThreadState::compare_and_set_current_location(Method* new_method,
177                                                            address new_location, jvmtiEvent event) {
178 
179   int new_bci = new_location - new_method->code_base();
180 
181   // The method is identified and stored as a jmethodID which is safe in this
182   // case because the class cannot be unloaded while a method is executing.
183   jmethodID new_method_id = new_method->jmethod_id();
184 
185   // the last breakpoint or single step was at this same location
186   if (_current_bci == new_bci && _current_method_id == new_method_id) {
187     switch (event) {
188     case JVMTI_EVENT_BREAKPOINT:
189       // Repeat breakpoint is complicated. If we previously posted a breakpoint

199       // This has been handled by clearing the location
200       _single_stepping_posted = true;
201       break;
202     default:
203       assert(false, "invalid event value passed");
204       break;
205     }
206     return;
207   }
208 
209   set_current_location(new_method_id, new_bci);
210   _breakpoint_posted = false;
211   _single_stepping_posted = false;
212 }
213 
214 
215 JvmtiFramePops* JvmtiEnvThreadState::get_frame_pops() {
216 #ifdef ASSERT
217   Thread *current = Thread::current();
218 #endif
219   assert(get_thread() == NULL || get_thread()->is_handshake_safe_for(current),
220          "frame pop data only accessible from same or detached thread or direct handshake");
221   if (_frame_pops == NULL) {
222     _frame_pops = new JvmtiFramePops();
223     assert(_frame_pops != NULL, "_frame_pops != NULL");
224   }
225   return _frame_pops;
226 }
227 
228 
229 bool JvmtiEnvThreadState::has_frame_pops() {
230   return _frame_pops == NULL? false : (_frame_pops->length() > 0);
231 }
232 
233 void JvmtiEnvThreadState::set_frame_pop(int frame_number) {
234 #ifdef ASSERT
235   Thread *current = Thread::current();
236 #endif
237   assert(get_thread() == NULL || get_thread()->is_handshake_safe_for(current),
238          "frame pop data only accessible from same or detached thread or direct handshake");
239   JvmtiFramePop fpop(frame_number);
240   JvmtiEventController::set_frame_pop(this, fpop);
241 }
242 
243 
244 void JvmtiEnvThreadState::clear_frame_pop(int frame_number) {
245 #ifdef ASSERT
246   Thread *current = Thread::current();
247 #endif
248   assert(get_thread() == NULL || get_thread()->is_handshake_safe_for(current),
249          "frame pop data only accessible from same or detached thread or direct handshake");
250   JvmtiFramePop fpop(frame_number);
251   JvmtiEventController::clear_frame_pop(this, fpop);
252 }
253 
254 
255 bool JvmtiEnvThreadState::is_frame_pop(int cur_frame_number) {
256 #ifdef ASSERT
257   Thread *current = Thread::current();
258 #endif
259   assert(get_thread() == NULL || get_thread()->is_handshake_safe_for(current),
260          "frame pop data only accessible from same or detached thread or direct handshake");
261   if (!jvmti_thread_state()->is_interp_only_mode() || _frame_pops == NULL) {
262     return false;
263   }
264   JvmtiFramePop fp(cur_frame_number);
265   return get_frame_pops()->contains(fp);
266 }
267 
268 class VM_VThreadGetCurrentLocation : public VM_Operation {
269  private:
270    Handle _vthread_h;
271    jmethodID _method_id;
272    int _bci;
273    bool _completed;
274 
275  public:
276   VM_VThreadGetCurrentLocation(Handle vthread_h)
277     : _vthread_h(vthread_h),
278       _method_id(NULL),
279       _bci(0),
280       _completed(false)
281   {}
282 
283   VMOp_Type type() const { return VMOp_VThreadGetCurrentLocation; }
284   void doit() {
285     if (!JvmtiEnvBase::is_vthread_alive(_vthread_h())) {
286       return; // _completed remains false
287     }
288     ResourceMark rm;
289     javaVFrame* jvf = JvmtiEnvBase::get_vthread_jvf(_vthread_h());
290 
291     if (jvf != NULL) { // TBD: jvf can be NULL, most likely, when vthread is exiting
292       Method* method = jvf->method();
293       _method_id = method->jmethod_id();
294       _bci = jvf->bci();
295     }
296     _completed = true;
297   }
298   void get_current_location(jmethodID *method_id, int *bci) {
299     *method_id = _method_id;
300     *bci = _bci;
301   }
302   bool completed() {
303     return _completed;
304   }
305 };
306 
307 class GetCurrentLocationClosure : public HandshakeClosure {
308  private:
309    jmethodID _method_id;
310    int _bci;
311    bool _completed;
312  public:
313   GetCurrentLocationClosure()
314     : HandshakeClosure("GetCurrentLocation"),
315       _method_id(NULL),
316       _bci(0),
317       _completed(false) {}
318   void do_thread(Thread *target) {
319     JavaThread *jt = JavaThread::cast(target);
320     ResourceMark rmark; // jt != Thread::current()
321     RegisterMap rm(jt, false);
322     // There can be a race condition between a handshake
323     // and the target thread exiting from Java execution.
324     // We must recheck that the last Java frame still exists.
325     if (!jt->is_exiting() && jt->has_last_Java_frame()) {

352 
353   // If single-stepping is disabled, clear current location so that
354   // single-stepping to the same method and bcp at a later time will be
355   // detected if single-stepping is enabled at that time (see 4388912).
356 
357   // If single-stepping is enabled, set the current location to the
358   // current method and bcp. This covers the following type of case,
359   // e.g., the debugger stepi command:
360   // - bytecode single stepped
361   // - SINGLE_STEP event posted and SINGLE_STEP event disabled
362   // - SINGLE_STEP event reenabled
363   // - bytecode rewritten to fast version
364 
365   // If breakpoint event is disabled, clear current location only if
366   // single-stepping is not enabled.  Otherwise, keep the thread location
367   // to detect any duplicate events.
368 
369   if (enabled) {
370     // If enabling breakpoint, no need to reset.
371     // Can't do anything if empty stack.
372 
373     JavaThread* thread = get_thread_or_saved();
374 
375     oop thread_oop = jvmti_thread_state()->get_thread_oop();
376 
377     // Check for an unmounted virual thread case.
378     if (thread == NULL && event_type == JVMTI_EVENT_SINGLE_STEP && is_virtual()) {
379       jmethodID method_id;
380       int bci;
381       JavaThread* cur_thread = JavaThread::current();
382       HandleMark hm(cur_thread);
383       VM_VThreadGetCurrentLocation op(Handle(cur_thread, thread_oop));
384       VMThread::execute(&op);
385       // do nothing if virtual thread has been already terminated
386       if (op.completed()) {
387         op.get_current_location(&method_id, &bci);
388         set_current_location(method_id, bci);
389       }
390       return;
391     }
392     if (event_type == JVMTI_EVENT_SINGLE_STEP && thread->has_last_Java_frame()) {
393       jmethodID method_id;
394       int bci;
395       // The java thread stack may not be walkable for a running thread
396       // so get current location with direct handshake.
397       GetCurrentLocationClosure op;
398       Thread *current = Thread::current();
399       if (thread->is_handshake_safe_for(current)) {
400         op.do_thread(thread);
401       } else {
402         Handshake::execute(&op, thread);
403         guarantee(op.completed(), "Handshake failed. Target thread is not alive?");
404       }
405       op.get_current_location(&method_id, &bci);
406       set_current_location(method_id, bci);
407     }
408   } else if (event_type == JVMTI_EVENT_SINGLE_STEP || !is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
409     // If this is to disable breakpoint, also check if single-step is not enabled
410     clear_current_location();
411   }
412 }
< prev index next >