jvmtiImpl.cpp revision 1879:f95d63e2154a
1193323Sed/*
2193323Sed * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4193323Sed *
5193323Sed * This code is free software; you can redistribute it and/or modify it
6193323Sed * under the terms of the GNU General Public License version 2 only, as
7193323Sed * published by the Free Software Foundation.
8193323Sed *
9193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT
10193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11193323Sed * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12193323Sed * version 2 for more details (a copy is included in the LICENSE file that
13193323Sed * accompanied this code).
14193323Sed *
15193323Sed * You should have received a copy of the GNU General Public License version
16193323Sed * 2 along with this work; if not, write to the Free Software Foundation,
17193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18234353Sdim *
19193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20234353Sdim * or visit www.oracle.com if you need additional information or have any
21193323Sed * questions.
22198090Srdivacky *
23193323Sed */
24193323Sed
25193323Sed#include "precompiled.hpp"
26249423Sdim#include "classfile/systemDictionary.hpp"
27249423Sdim#include "interpreter/interpreter.hpp"
28193323Sed#include "jvmtifiles/jvmtiEnv.hpp"
29193323Sed#include "memory/resourceArea.hpp"
30193323Sed#include "oops/instanceKlass.hpp"
31193323Sed#include "prims/jvmtiAgentThread.hpp"
32193323Sed#include "prims/jvmtiEventController.inline.hpp"
33193323Sed#include "prims/jvmtiImpl.hpp"
34193323Sed#include "prims/jvmtiRedefineClasses.hpp"
35193323Sed#include "runtime/deoptimization.hpp"
36193323Sed#include "runtime/handles.hpp"
37193323Sed#include "runtime/handles.inline.hpp"
38193323Sed#include "runtime/interfaceSupport.hpp"
39193323Sed#include "runtime/javaCalls.hpp"
40198090Srdivacky#include "runtime/signature.hpp"
41193323Sed#include "runtime/vframe.hpp"
42193323Sed#include "runtime/vframe_hp.hpp"
43193323Sed#include "runtime/vm_operations.hpp"
44193323Sed#include "utilities/exceptions.hpp"
45193323Sed#ifdef TARGET_OS_FAMILY_linux
46193323Sed# include "thread_linux.inline.hpp"
47193323Sed#endif
48198090Srdivacky#ifdef TARGET_OS_FAMILY_solaris
49198090Srdivacky# include "thread_solaris.inline.hpp"
50198090Srdivacky#endif
51198090Srdivacky#ifdef TARGET_OS_FAMILY_windows
52226633Sdim# include "thread_windows.inline.hpp"
53198090Srdivacky#endif
54198090Srdivacky
55198090Srdivacky//
56198090Srdivacky// class JvmtiAgentThread
57198090Srdivacky//
58226633Sdim// JavaThread used to wrap a thread started by an agent
59198090Srdivacky// using the JVMTI method RunAgentThread.
60198090Srdivacky//
61198090Srdivacky
62226633SdimJvmtiAgentThread::JvmtiAgentThread(JvmtiEnv* env, jvmtiStartFunction start_fn, const void *start_arg)
63198090Srdivacky    : JavaThread(start_function_wrapper) {
64226633Sdim    _env = env;
65226633Sdim    _start_fn = start_fn;
66198090Srdivacky    _start_arg = start_arg;
67198090Srdivacky}
68198090Srdivacky
69198090Srdivackyvoid
70198090SrdivackyJvmtiAgentThread::start_function_wrapper(JavaThread *thread, TRAPS) {
71198090Srdivacky    // It is expected that any Agent threads will be created as
72198090Srdivacky    // Java Threads.  If this is the case, notification of the creation
73198090Srdivacky    // of the thread is given in JavaThread::thread_main().
74198090Srdivacky    assert(thread->is_Java_thread(), "debugger thread should be a Java Thread");
75198090Srdivacky    assert(thread == JavaThread::current(), "sanity check");
76193323Sed
77193323Sed    JvmtiAgentThread *dthread = (JvmtiAgentThread *)thread;
78193323Sed    dthread->call_start_function();
79198090Srdivacky}
80193323Sed
81193323Sedvoid
82193323SedJvmtiAgentThread::call_start_function() {
83193323Sed    ThreadToNativeFromVM transition(this);
84193323Sed    _start_fn(_env->jvmti_external(), jni_environment(), (void*)_start_arg);
85193323Sed}
86193323Sed
87193323Sed
88193323Sed//
89226633Sdim// class GrowableCache - private methods
90198090Srdivacky//
91226633Sdim
92193323Sedvoid GrowableCache::recache() {
93193323Sed  int len = _elements->length();
94193323Sed
95193323Sed  FREE_C_HEAP_ARRAY(address, _cache);
96193323Sed  _cache = NEW_C_HEAP_ARRAY(address,len+1);
97193323Sed
98226633Sdim  for (int i=0; i<len; i++) {
99193323Sed    _cache[i] = _elements->at(i)->getCacheValue();
100226633Sdim    //
101193323Sed    // The cache entry has gone bad. Without a valid frame pointer
102193323Sed    // value, the entry is useless so we simply delete it in product
103193323Sed    // mode. The call to remove() will rebuild the cache again
104193323Sed    // without the bad entry.
105193323Sed    //
106226633Sdim    if (_cache[i] == NULL) {
107226633Sdim      assert(false, "cannot recache NULL elements");
108226633Sdim      remove(i);
109226633Sdim      return;
110226633Sdim    }
111226633Sdim  }
112226633Sdim  _cache[len] = NULL;
113226633Sdim
114226633Sdim  _listener_fun(_this_obj,_cache);
115226633Sdim}
116210299Sed
117193323Sedbool GrowableCache::equals(void* v, GrowableElement *e2) {
118198090Srdivacky  GrowableElement *e1 = (GrowableElement *) v;
119198090Srdivacky  assert(e1 != NULL, "e1 != NULL");
120193323Sed  assert(e2 != NULL, "e2 != NULL");
121193323Sed
122193323Sed  return e1->equals(e2);
123193323Sed}
124193323Sed
125193323Sed//
126193323Sed// class GrowableCache - public methods
127193323Sed//
128193323Sed
129193323SedGrowableCache::GrowableCache() {
130193323Sed  _this_obj       = NULL;
131193323Sed  _listener_fun   = NULL;
132193323Sed  _elements       = NULL;
133193323Sed  _cache          = NULL;
134193323Sed}
135193323Sed
136193323SedGrowableCache::~GrowableCache() {
137193323Sed  clear();
138193323Sed  delete _elements;
139193323Sed  FREE_C_HEAP_ARRAY(address, _cache);
140198090Srdivacky}
141193323Sed
142193323Sedvoid GrowableCache::initialize(void *this_obj, void listener_fun(void *, address*) ) {
143193323Sed  _this_obj       = this_obj;
144193323Sed  _listener_fun   = listener_fun;
145193323Sed  _elements       = new (ResourceObj::C_HEAP) GrowableArray<GrowableElement*>(5,true);
146193323Sed  recache();
147193323Sed}
148193323Sed
149193323Sed// number of elements in the collection
150193323Sedint GrowableCache::length() {
151193323Sed  return _elements->length();
152193323Sed}
153193323Sed
154193323Sed// get the value of the index element in the collection
155198090SrdivackyGrowableElement* GrowableCache::at(int index) {
156193323Sed  GrowableElement *e = (GrowableElement *) _elements->at(index);
157193323Sed  assert(e != NULL, "e != NULL");
158193323Sed  return e;
159193323Sed}
160193323Sed
161193323Sedint GrowableCache::find(GrowableElement* e) {
162193323Sed  return _elements->find(e, GrowableCache::equals);
163193323Sed}
164193323Sed
165193323Sed// append a copy of the element to the end of the collection
166193323Sedvoid GrowableCache::append(GrowableElement* e) {
167198090Srdivacky  GrowableElement *new_e = e->clone();
168193323Sed  _elements->append(new_e);
169193323Sed  recache();
170193323Sed}
171193323Sed
172193323Sed// insert a copy of the element using lessthan()
173193323Sedvoid GrowableCache::insert(GrowableElement* e) {
174193323Sed  GrowableElement *new_e = e->clone();
175193323Sed  _elements->append(new_e);
176193323Sed
177193323Sed  int n = length()-2;
178198090Srdivacky  for (int i=n; i>=0; i--) {
179193323Sed    GrowableElement *e1 = _elements->at(i);
180193323Sed    GrowableElement *e2 = _elements->at(i+1);
181193323Sed    if (e2->lessThan(e1)) {
182193323Sed      _elements->at_put(i+1, e1);
183193323Sed      _elements->at_put(i,   e2);
184193323Sed    }
185193323Sed  }
186193323Sed
187193323Sed  recache();
188193323Sed}
189193323Sed
190193323Sed// remove the element at index
191193323Sedvoid GrowableCache::remove (int index) {
192193323Sed  GrowableElement *e = _elements->at(index);
193193323Sed  assert(e != NULL, "e != NULL");
194193323Sed  _elements->remove(e);
195193323Sed  delete e;
196193323Sed  recache();
197198090Srdivacky}
198193323Sed
199193323Sed// clear out all elements, release all heap space and
200193323Sed// let our listener know that things have changed.
201193323Sedvoid GrowableCache::clear() {
202193323Sed  int len = _elements->length();
203193323Sed  for (int i=0; i<len; i++) {
204198090Srdivacky    delete _elements->at(i);
205198090Srdivacky  }
206193323Sed  _elements->clear();
207193323Sed  recache();
208193323Sed}
209193323Sed
210193323Sedvoid GrowableCache::oops_do(OopClosure* f) {
211193323Sed  int len = _elements->length();
212193323Sed  for (int i=0; i<len; i++) {
213193323Sed    GrowableElement *e = _elements->at(i);
214198090Srdivacky    e->oops_do(f);
215193323Sed  }
216193323Sed}
217193323Sed
218193323Sedvoid GrowableCache::gc_epilogue() {
219193323Sed  int len = _elements->length();
220193323Sed  // recompute the new cache value after GC
221193323Sed  for (int i=0; i<len; i++) {
222193323Sed    _cache[i] = _elements->at(i)->getCacheValue();
223193323Sed  }
224193323Sed}
225193323Sed
226198090Srdivacky//
227193323Sed// class JvmtiBreakpoint
228193323Sed//
229193323Sed
230193323SedJvmtiBreakpoint::JvmtiBreakpoint() {
231193323Sed  _method = NULL;
232193323Sed  _bci    = 0;
233193323Sed#ifdef CHECK_UNHANDLED_OOPS
234198090Srdivacky  // This one is always allocated with new, but check it just in case.
235193323Sed  Thread *thread = Thread::current();
236193323Sed  if (thread->is_in_stack((address)&_method)) {
237198090Srdivacky    thread->allow_unhandled_oop((oop*)&_method);
238193323Sed  }
239193323Sed#endif // CHECK_UNHANDLED_OOPS
240193323Sed}
241193323Sed
242193323SedJvmtiBreakpoint::JvmtiBreakpoint(methodOop m_method, jlocation location) {
243193323Sed  _method        = m_method;
244193323Sed  assert(_method != NULL, "_method != NULL");
245193323Sed  _bci           = (int) location;
246193323Sed#ifdef CHECK_UNHANDLED_OOPS
247193323Sed  // Could be allocated with new and wouldn't be on the unhandled oop list.
248193323Sed  Thread *thread = Thread::current();
249193323Sed  if (thread->is_in_stack((address)&_method)) {
250198090Srdivacky    thread->allow_unhandled_oop(&_method);
251193323Sed  }
252193323Sed#endif // CHECK_UNHANDLED_OOPS
253198090Srdivacky
254193323Sed  assert(_bci >= 0, "_bci >= 0");
255193323Sed}
256193323Sed
257193323Sedvoid JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) {
258193323Sed  _method   = bp._method;
259193323Sed  _bci      = bp._bci;
260193323Sed}
261198090Srdivacky
262193323Sedbool JvmtiBreakpoint::lessThan(JvmtiBreakpoint& bp) {
263193323Sed  Unimplemented();
264198090Srdivacky  return false;
265193323Sed}
266193323Sed
267193323Sedbool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) {
268193323Sed  return _method   == bp._method
269193323Sed    &&   _bci      == bp._bci;
270193323Sed}
271193323Sed
272193323Sedbool JvmtiBreakpoint::is_valid() {
273193323Sed  return _method != NULL &&
274193323Sed         _bci >= 0;
275193323Sed}
276193323Sed
277198090Srdivackyaddress JvmtiBreakpoint::getBcp() {
278193323Sed  return _method->bcp_from(_bci);
279193323Sed}
280198090Srdivacky
281193323Sedvoid JvmtiBreakpoint::each_method_version_do(method_action meth_act) {
282193323Sed  ((methodOopDesc*)_method->*meth_act)(_bci);
283193323Sed
284193323Sed  // add/remove breakpoint to/from versions of the method that
285193323Sed  // are EMCP. Directly or transitively obsolete methods are
286193323Sed  // not saved in the PreviousVersionInfo.
287203954Srdivacky  Thread *thread = Thread::current();
288198090Srdivacky  instanceKlassHandle ikh = instanceKlassHandle(thread, _method->method_holder());
289193323Sed  symbolOop m_name = _method->name();
290193323Sed  symbolOop m_signature = _method->signature();
291193323Sed
292193323Sed  {
293193323Sed    ResourceMark rm(thread);
294193323Sed    // PreviousVersionInfo objects returned via PreviousVersionWalker
295193323Sed    // contain a GrowableArray of handles. We have to clean up the
296193323Sed    // GrowableArray _after_ the PreviousVersionWalker destructor
297193323Sed    // has destroyed the handles.
298193323Sed    {
299193323Sed      // search previous versions if they exist
300193323Sed      PreviousVersionWalker pvw((instanceKlass *)ikh()->klass_part());
301193323Sed      for (PreviousVersionInfo * pv_info = pvw.next_previous_version();
302193323Sed           pv_info != NULL; pv_info = pvw.next_previous_version()) {
303193323Sed        GrowableArray<methodHandle>* methods =
304193323Sed          pv_info->prev_EMCP_method_handles();
305193323Sed
306193323Sed        if (methods == NULL) {
307193323Sed          // We have run into a PreviousVersion generation where
308193323Sed          // all methods were made obsolete during that generation's
309193323Sed          // RedefineClasses() operation. At the time of that
310198090Srdivacky          // operation, all EMCP methods were flushed so we don't
311193323Sed          // have to go back any further.
312193323Sed          //
313193323Sed          // A NULL methods array is different than an empty methods
314193323Sed          // array. We cannot infer any optimizations about older
315193323Sed          // generations from an empty methods array for the current
316198090Srdivacky          // generation.
317193323Sed          break;
318193323Sed        }
319193323Sed
320193323Sed        for (int i = methods->length() - 1; i >= 0; i--) {
321193323Sed          methodHandle method = methods->at(i);
322198090Srdivacky          if (method->name() == m_name && method->signature() == m_signature) {
323193323Sed            RC_TRACE(0x00000800, ("%sing breakpoint in %s(%s)",
324193323Sed              meth_act == &methodOopDesc::set_breakpoint ? "sett" : "clear",
325193323Sed              method->name()->as_C_string(),
326193323Sed              method->signature()->as_C_string()));
327193323Sed            assert(!method->is_obsolete(), "only EMCP methods here");
328193323Sed
329193323Sed            ((methodOopDesc*)method()->*meth_act)(_bci);
330193323Sed            break;
331193323Sed          }
332193323Sed        }
333193323Sed      }
334193323Sed    } // pvw is cleaned up
335193323Sed  } // rm is cleaned up
336193323Sed}
337193323Sed
338193323Sedvoid JvmtiBreakpoint::set() {
339193323Sed  each_method_version_do(&methodOopDesc::set_breakpoint);
340193323Sed}
341193323Sed
342193323Sedvoid JvmtiBreakpoint::clear() {
343193323Sed  each_method_version_do(&methodOopDesc::clear_breakpoint);
344193323Sed}
345193323Sed
346193323Sedvoid JvmtiBreakpoint::print() {
347193323Sed#ifndef PRODUCT
348198090Srdivacky  const char *class_name  = (_method == NULL) ? "NULL" : _method->klass_name()->as_C_string();
349193323Sed  const char *method_name = (_method == NULL) ? "NULL" : _method->name()->as_C_string();
350193323Sed
351193323Sed  tty->print("Breakpoint(%s,%s,%d,%p)",class_name, method_name, _bci, getBcp());
352193323Sed#endif
353193323Sed}
354193323Sed
355193323Sed
356193323Sed//
357193323Sed// class VM_ChangeBreakpoints
358193323Sed//
359193323Sed// Modify the Breakpoints data structure at a safepoint
360193323Sed//
361193323Sed
362193323Sedvoid VM_ChangeBreakpoints::doit() {
363193323Sed  switch (_operation) {
364193323Sed  case SET_BREAKPOINT:
365193323Sed    _breakpoints->set_at_safepoint(*_bp);
366198090Srdivacky    break;
367193323Sed  case CLEAR_BREAKPOINT:
368193323Sed    _breakpoints->clear_at_safepoint(*_bp);
369193323Sed    break;
370193323Sed  case CLEAR_ALL_BREAKPOINT:
371193323Sed    _breakpoints->clearall_at_safepoint();
372193323Sed    break;
373193323Sed  default:
374193323Sed    assert(false, "Unknown operation");
375218893Sdim  }
376193323Sed}
377193323Sed
378193323Sedvoid VM_ChangeBreakpoints::oops_do(OopClosure* f) {
379193323Sed  // This operation keeps breakpoints alive
380193323Sed  if (_breakpoints != NULL) {
381193323Sed    _breakpoints->oops_do(f);
382193323Sed  }
383193323Sed  if (_bp != NULL) {
384193323Sed    _bp->oops_do(f);
385193323Sed  }
386193323Sed}
387218893Sdim
388193323Sed//
389193323Sed// class JvmtiBreakpoints
390226633Sdim//
391193323Sed// a JVMTI internal collection of JvmtiBreakpoint
392193323Sed//
393193323Sed
394193323SedJvmtiBreakpoints::JvmtiBreakpoints(void listener_fun(void *,address *)) {
395193323Sed  _bps.initialize(this,listener_fun);
396193323Sed}
397193323Sed
398193323SedJvmtiBreakpoints:: ~JvmtiBreakpoints() {}
399193323Sed
400193323Sedvoid  JvmtiBreakpoints::oops_do(OopClosure* f) {
401193323Sed  _bps.oops_do(f);
402193323Sed}
403193323Sed
404193323Sedvoid  JvmtiBreakpoints::gc_epilogue() {
405193323Sed  _bps.gc_epilogue();
406193323Sed}
407193323Sed
408193323Sedvoid  JvmtiBreakpoints::print() {
409193323Sed#ifndef PRODUCT
410193323Sed  ResourceMark rm;
411193323Sed
412193323Sed  int n = _bps.length();
413193323Sed  for (int i=0; i<n; i++) {
414193323Sed    JvmtiBreakpoint& bp = _bps.at(i);
415193323Sed    tty->print("%d: ", i);
416193323Sed    bp.print();
417193323Sed    tty->print_cr("");
418193323Sed  }
419193323Sed#endif
420193323Sed}
421193323Sed
422193323Sed
423193323Sedvoid JvmtiBreakpoints::set_at_safepoint(JvmtiBreakpoint& bp) {
424193323Sed  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
425193323Sed
426193323Sed  int i = _bps.find(bp);
427198090Srdivacky  if (i == -1) {
428193323Sed    _bps.append(bp);
429193323Sed    bp.set();
430193323Sed  }
431193323Sed}
432193323Sed
433193323Sedvoid JvmtiBreakpoints::clear_at_safepoint(JvmtiBreakpoint& bp) {
434193323Sed  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
435193323Sed
436193323Sed  int i = _bps.find(bp);
437193323Sed  if (i != -1) {
438193323Sed    _bps.remove(i);
439193323Sed    bp.clear();
440193323Sed  }
441193323Sed}
442193323Sed
443193323Sedvoid JvmtiBreakpoints::clearall_at_safepoint() {
444193323Sed  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
445193323Sed
446193323Sed  int len = _bps.length();
447193323Sed  for (int i=0; i<len; i++) {
448193323Sed    _bps.at(i).clear();
449193323Sed  }
450193323Sed  _bps.clear();
451193323Sed}
452193323Sed
453193323Sedint JvmtiBreakpoints::length() { return _bps.length(); }
454193323Sed
455193323Sedint JvmtiBreakpoints::set(JvmtiBreakpoint& bp) {
456193323Sed  if ( _bps.find(bp) != -1) {
457193323Sed     return JVMTI_ERROR_DUPLICATE;
458193323Sed  }
459193323Sed  VM_ChangeBreakpoints set_breakpoint(this,VM_ChangeBreakpoints::SET_BREAKPOINT, &bp);
460193323Sed  VMThread::execute(&set_breakpoint);
461193323Sed  return JVMTI_ERROR_NONE;
462193323Sed}
463193323Sed
464193323Sedint JvmtiBreakpoints::clear(JvmtiBreakpoint& bp) {
465193323Sed  if ( _bps.find(bp) == -1) {
466226633Sdim     return JVMTI_ERROR_NOT_FOUND;
467193323Sed  }
468193323Sed
469193323Sed  VM_ChangeBreakpoints clear_breakpoint(this,VM_ChangeBreakpoints::CLEAR_BREAKPOINT, &bp);
470193323Sed  VMThread::execute(&clear_breakpoint);
471193323Sed  return JVMTI_ERROR_NONE;
472193323Sed}
473193323Sed
474193323Sedvoid JvmtiBreakpoints::clearall_in_class_at_safepoint(klassOop klass) {
475193323Sed  bool changed = true;
476193323Sed  // We are going to run thru the list of bkpts
477193323Sed  // and delete some.  This deletion probably alters
478193323Sed  // the list in some implementation defined way such
479193323Sed  // that when we delete entry i, the next entry might
480193323Sed  // no longer be at i+1.  To be safe, each time we delete
481193323Sed  // an entry, we'll just start again from the beginning.
482193323Sed  // We'll stop when we make a pass thru the whole list without
483193323Sed  // deleting anything.
484193323Sed  while (changed) {
485193323Sed    int len = _bps.length();
486193323Sed    changed = false;
487193323Sed    for (int i = 0; i < len; i++) {
488193323Sed      JvmtiBreakpoint& bp = _bps.at(i);
489193323Sed      if (bp.method()->method_holder() == klass) {
490193323Sed        bp.clear();
491193323Sed        _bps.remove(i);
492193323Sed        // This changed 'i' so we have to start over.
493198090Srdivacky        changed = true;
494193323Sed        break;
495193323Sed      }
496193323Sed    }
497193323Sed  }
498193323Sed}
499193323Sed
500193323Sedvoid JvmtiBreakpoints::clearall() {
501193323Sed  VM_ChangeBreakpoints clearall_breakpoint(this,VM_ChangeBreakpoints::CLEAR_ALL_BREAKPOINT);
502198090Srdivacky  VMThread::execute(&clearall_breakpoint);
503193323Sed}
504193323Sed
505193323Sed//
506193323Sed// class JvmtiCurrentBreakpoints
507193323Sed//
508193323Sed
509193323SedJvmtiBreakpoints *JvmtiCurrentBreakpoints::_jvmti_breakpoints  = NULL;
510193323Sedaddress *         JvmtiCurrentBreakpoints::_breakpoint_list    = NULL;
511193323Sed
512193323Sed
513193323SedJvmtiBreakpoints& JvmtiCurrentBreakpoints::get_jvmti_breakpoints() {
514193323Sed  if (_jvmti_breakpoints != NULL) return (*_jvmti_breakpoints);
515193323Sed  _jvmti_breakpoints = new JvmtiBreakpoints(listener_fun);
516193323Sed  assert(_jvmti_breakpoints != NULL, "_jvmti_breakpoints != NULL");
517193323Sed  return (*_jvmti_breakpoints);
518193323Sed}
519193323Sed
520193323Sedvoid  JvmtiCurrentBreakpoints::listener_fun(void *this_obj, address *cache) {
521193323Sed  JvmtiBreakpoints *this_jvmti = (JvmtiBreakpoints *) this_obj;
522193323Sed  assert(this_jvmti != NULL, "this_jvmti != NULL");
523193323Sed
524193323Sed  debug_only(int n = this_jvmti->length(););
525193323Sed  assert(cache[n] == NULL, "cache must be NULL terminated");
526193323Sed
527193323Sed  set_breakpoint_list(cache);
528193323Sed}
529193323Sed
530193323Sed
531193323Sedvoid JvmtiCurrentBreakpoints::oops_do(OopClosure* f) {
532193323Sed  if (_jvmti_breakpoints != NULL) {
533193323Sed    _jvmti_breakpoints->oops_do(f);
534193323Sed  }
535193323Sed}
536193323Sed
537193323Sedvoid JvmtiCurrentBreakpoints::gc_epilogue() {
538193323Sed  if (_jvmti_breakpoints != NULL) {
539198090Srdivacky    _jvmti_breakpoints->gc_epilogue();
540193323Sed  }
541198090Srdivacky}
542193323Sed
543193323Sed
544193323Sed///////////////////////////////////////////////////////////////
545193323Sed//
546193323Sed// class VM_GetOrSetLocal
547193323Sed//
548193323Sed
549193323Sed// Constructor for non-object getter
550193323SedVM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type)
551193323Sed  : _thread(thread)
552193323Sed  , _calling_thread(NULL)
553193323Sed  , _depth(depth)
554193323Sed  , _index(index)
555193323Sed  , _type(type)
556193323Sed  , _set(false)
557193323Sed  , _jvf(NULL)
558193323Sed  , _result(JVMTI_ERROR_NONE)
559193323Sed{
560193323Sed}
561218893Sdim
562249423Sdim// Constructor for object or non-object setter
563193323SedVM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, jint depth, int index, BasicType type, jvalue value)
564193323Sed  : _thread(thread)
565193323Sed  , _calling_thread(NULL)
566218893Sdim  , _depth(depth)
567249423Sdim  , _index(index)
568193323Sed  , _type(type)
569193323Sed  , _value(value)
570193323Sed  , _set(true)
571193323Sed  , _jvf(NULL)
572193323Sed  , _result(JVMTI_ERROR_NONE)
573193323Sed{
574193323Sed}
575193323Sed
576193323Sed// Constructor for object getter
577193323SedVM_GetOrSetLocal::VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, int index)
578193323Sed  : _thread(thread)
579198090Srdivacky  , _calling_thread(calling_thread)
580193323Sed  , _depth(depth)
581193323Sed  , _index(index)
582193323Sed  , _type(T_OBJECT)
583218893Sdim  , _set(false)
584198090Srdivacky  , _jvf(NULL)
585193323Sed  , _result(JVMTI_ERROR_NONE)
586198090Srdivacky{
587193323Sed}
588193323Sed
589193323Sed
590193323Sedvframe *VM_GetOrSetLocal::get_vframe() {
591193323Sed  if (!_thread->has_last_Java_frame()) {
592218893Sdim    return NULL;
593198090Srdivacky  }
594193323Sed  RegisterMap reg_map(_thread);
595198090Srdivacky  vframe *vf = _thread->last_java_vframe(&reg_map);
596193323Sed  int d = 0;
597193323Sed  while ((vf != NULL) && (d < _depth)) {
598193323Sed    vf = vf->java_sender();
599193323Sed    d++;
600193323Sed  }
601198090Srdivacky  return vf;
602193323Sed}
603193323Sed
604218893SdimjavaVFrame *VM_GetOrSetLocal::get_java_vframe() {
605193323Sed  vframe* vf = get_vframe();
606218893Sdim  if (vf == NULL) {
607218893Sdim    _result = JVMTI_ERROR_NO_MORE_FRAMES;
608193323Sed    return NULL;
609193323Sed  }
610210299Sed  javaVFrame *jvf = (javaVFrame*)vf;
611198090Srdivacky
612226633Sdim  if (!vf->is_java_frame() || jvf->method()->is_native()) {
613226633Sdim    _result = JVMTI_ERROR_OPAQUE_FRAME;
614226633Sdim    return NULL;
615193323Sed  }
616198090Srdivacky  return jvf;
617198090Srdivacky}
618198090Srdivacky
619198090Srdivacky// Check that the klass is assignable to a type with the given signature.
620198090Srdivacky// Another solution could be to use the function Klass::is_subtype_of(type).
621198090Srdivacky// But the type class can be forced to load/initialize eagerly in such a case.
622198090Srdivacky// This may cause unexpected consequences like CFLH or class-init JVMTI events.
623193323Sed// It is better to avoid such a behavior.
624198090Srdivackybool VM_GetOrSetLocal::is_assignable(const char* ty_sign, Klass* klass, Thread* thread) {
625193323Sed  assert(ty_sign != NULL, "type signature must not be NULL");
626198090Srdivacky  assert(thread != NULL, "thread must not be NULL");
627193323Sed  assert(klass != NULL, "klass must not be NULL");
628193323Sed
629193323Sed  int len = (int) strlen(ty_sign);
630193323Sed  if (ty_sign[0] == 'L' && ty_sign[len-1] == ';') { // Need pure class/interface name
631193323Sed    ty_sign++;
632193323Sed    len -= 2;
633193323Sed  }
634193323Sed  symbolHandle ty_sym = oopFactory::new_symbol_handle(ty_sign, len, thread);
635193323Sed  if (klass->name() == ty_sym()) {
636226633Sdim    return true;
637226633Sdim  }
638193323Sed  // Compare primary supers
639193323Sed  int super_depth = klass->super_depth();
640193323Sed  int idx;
641193323Sed  for (idx = 0; idx < super_depth; idx++) {
642193323Sed    if (Klass::cast(klass->primary_super_of_depth(idx))->name() == ty_sym()) {
643198090Srdivacky      return true;
644198090Srdivacky    }
645198090Srdivacky  }
646226633Sdim  // Compare secondary supers
647226633Sdim  objArrayOop sec_supers = klass->secondary_supers();
648226633Sdim  for (idx = 0; idx < sec_supers->length(); idx++) {
649193323Sed    if (Klass::cast((klassOop) sec_supers->obj_at(idx))->name() == ty_sym()) {
650193323Sed      return true;
651198090Srdivacky    }
652193323Sed  }
653198090Srdivacky  return false;
654198090Srdivacky}
655198090Srdivacky
656198090Srdivacky// Checks error conditions:
657198090Srdivacky//   JVMTI_ERROR_INVALID_SLOT
658198090Srdivacky//   JVMTI_ERROR_TYPE_MISMATCH
659198090Srdivacky// Returns: 'true' - everything is Ok, 'false' - error code
660198090Srdivacky
661193323Sedbool VM_GetOrSetLocal::check_slot_type(javaVFrame* jvf) {
662193323Sed  methodOop method_oop = jvf->method();
663234353Sdim  if (!method_oop->has_localvariable_table()) {
664234353Sdim    // Just to check index boundaries
665234353Sdim    jint extra_slot = (_type == T_LONG || _type == T_DOUBLE) ? 1 : 0;
666193323Sed    if (_index < 0 || _index + extra_slot >= method_oop->max_locals()) {
667234353Sdim      _result = JVMTI_ERROR_INVALID_SLOT;
668193323Sed      return false;
669193323Sed    }
670193323Sed    return true;
671193323Sed  }
672193323Sed
673193323Sed  jint num_entries = method_oop->localvariable_table_length();
674193323Sed  if (num_entries == 0) {
675193323Sed    _result = JVMTI_ERROR_INVALID_SLOT;
676193323Sed    return false;       // There are no slots
677198090Srdivacky  }
678193323Sed  int signature_idx = -1;
679193323Sed  int vf_bci = jvf->bci();
680193323Sed  LocalVariableTableElement* table = method_oop->localvariable_table_start();
681193323Sed  for (int i = 0; i < num_entries; i++) {
682203954Srdivacky    int start_bci = table[i].start_bci;
683203954Srdivacky    int end_bci = start_bci + table[i].length;
684203954Srdivacky
685203954Srdivacky    // Here we assume that locations of LVT entries
686203954Srdivacky    // with the same slot number cannot be overlapped
687203954Srdivacky    if (_index == (jint) table[i].slot && start_bci <= vf_bci && vf_bci <= end_bci) {
688203954Srdivacky      signature_idx = (int) table[i].descriptor_cp_index;
689203954Srdivacky      break;
690203954Srdivacky    }
691203954Srdivacky  }
692203954Srdivacky  if (signature_idx == -1) {
693203954Srdivacky    _result = JVMTI_ERROR_INVALID_SLOT;
694203954Srdivacky    return false;       // Incorrect slot index
695263508Sdim  }
696203954Srdivacky  symbolOop   sign_sym  = method_oop->constants()->symbol_at(signature_idx);
697203954Srdivacky  const char* signature = (const char *) sign_sym->as_utf8();
698203954Srdivacky  BasicType slot_type = char2type(signature[0]);
699193323Sed
700193323Sed  switch (slot_type) {
701193323Sed  case T_BYTE:
702263508Sdim  case T_SHORT:
703193323Sed  case T_CHAR:
704193323Sed  case T_BOOLEAN:
705193323Sed    slot_type = T_INT;
706203954Srdivacky    break;
707193323Sed  case T_ARRAY:
708193323Sed    slot_type = T_OBJECT;
709193323Sed    break;
710193323Sed  };
711234353Sdim  if (_type != slot_type) {
712193323Sed    _result = JVMTI_ERROR_TYPE_MISMATCH;
713193323Sed    return false;
714193323Sed  }
715193323Sed
716193323Sed  jobject jobj = _value.l;
717193323Sed  if (_set && slot_type == T_OBJECT && jobj != NULL) { // NULL reference is allowed
718193323Sed    // Check that the jobject class matches the return type signature.
719193323Sed    JavaThread* cur_thread = JavaThread::current();
720193323Sed    HandleMark hm(cur_thread);
721193323Sed
722234353Sdim    Handle obj = Handle(cur_thread, JNIHandles::resolve_external_guard(jobj));
723193323Sed    NULL_CHECK(obj, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
724193323Sed    KlassHandle ob_kh = KlassHandle(cur_thread, obj->klass());
725193323Sed    NULL_CHECK(ob_kh, (_result = JVMTI_ERROR_INVALID_OBJECT, false));
726193323Sed
727193323Sed    if (!is_assignable(signature, Klass::cast(ob_kh()), cur_thread)) {
728234353Sdim      _result = JVMTI_ERROR_TYPE_MISMATCH;
729193323Sed      return false;
730193323Sed    }
731193323Sed  }
732193323Sed  return true;
733193323Sed}
734193323Sed
735193323Sedstatic bool can_be_deoptimized(vframe* vf) {
736193323Sed  return (vf->is_compiled_frame() && vf->fr().can_be_deoptimized());
737193323Sed}
738263508Sdim
739193323Sedbool VM_GetOrSetLocal::doit_prologue() {
740193323Sed  _jvf = get_java_vframe();
741193323Sed  NULL_CHECK(_jvf, false);
742193323Sed
743193323Sed  if (!check_slot_type(_jvf)) {
744263508Sdim    return false;
745193323Sed  }
746193323Sed  return true;
747193323Sed}
748193323Sed
749193323Sedvoid VM_GetOrSetLocal::doit() {
750193323Sed  if (_set) {
751193323Sed    // Force deoptimization of frame if compiled because it's
752193323Sed    // possible the compiler emitted some locals as constant values,
753193323Sed    // meaning they are not mutable.
754193323Sed    if (can_be_deoptimized(_jvf)) {
755193323Sed
756193323Sed      // Schedule deoptimization so that eventually the local
757193323Sed      // update will be written to an interpreter frame.
758193323Sed      Deoptimization::deoptimize_frame(_jvf->thread(), _jvf->fr().id());
759193323Sed
760193323Sed      // Now store a new value for the local which will be applied
761193323Sed      // once deoptimization occurs. Note however that while this
762193323Sed      // write is deferred until deoptimization actually happens
763193323Sed      // can vframe created after this point will have its locals
764193323Sed      // reflecting this update so as far as anyone can see the
765234353Sdim      // write has already taken place.
766234353Sdim
767234353Sdim      // If we are updating an oop then get the oop from the handle
768234353Sdim      // since the handle will be long gone by the time the deopt
769234353Sdim      // happens. The oop stored in the deferred local will be
770234353Sdim      // gc'd on its own.
771234353Sdim      if (_type == T_OBJECT) {
772234353Sdim        _value.l = (jobject) (JNIHandles::resolve_external_guard(_value.l));
773234353Sdim      }
774234353Sdim      // Re-read the vframe so we can see that it is deoptimized
775234353Sdim      // [ Only need because of assert in update_local() ]
776234353Sdim      _jvf = get_java_vframe();
777193323Sed      ((compiledVFrame*)_jvf)->update_local(_type, _index, _value);
778193323Sed      return;
779193323Sed    }
780193323Sed    StackValueCollection *locals = _jvf->locals();
781234353Sdim    HandleMark hm;
782193323Sed
783234353Sdim    switch (_type) {
784193323Sed    case T_INT:    locals->set_int_at   (_index, _value.i); break;
785193323Sed    case T_LONG:   locals->set_long_at  (_index, _value.j); break;
786193323Sed    case T_FLOAT:  locals->set_float_at (_index, _value.f); break;
787193323Sed    case T_DOUBLE: locals->set_double_at(_index, _value.d); break;
788193323Sed    case T_OBJECT: {
789234353Sdim      Handle ob_h(JNIHandles::resolve_external_guard(_value.l));
790234353Sdim      locals->set_obj_at (_index, ob_h);
791193323Sed      break;
792234353Sdim    }
793234353Sdim    default: ShouldNotReachHere();
794234353Sdim    }
795234353Sdim    _jvf->set_locals(locals);
796234353Sdim  } else {
797234353Sdim    StackValueCollection *locals = _jvf->locals();
798234353Sdim
799234353Sdim    if (locals->at(_index)->type() == T_CONFLICT) {
800193323Sed      memset(&_value, 0, sizeof(_value));
801234353Sdim      _value.l = NULL;
802193323Sed      return;
803193323Sed    }
804198090Srdivacky
805193323Sed    switch (_type) {
806193323Sed    case T_INT:    _value.i = locals->int_at   (_index);   break;
807193323Sed    case T_LONG:   _value.j = locals->long_at  (_index);   break;
808193323Sed    case T_FLOAT:  _value.f = locals->float_at (_index);   break;
809193323Sed    case T_DOUBLE: _value.d = locals->double_at(_index);   break;
810193323Sed    case T_OBJECT: {
811193323Sed      // Wrap the oop to be returned in a local JNI handle since
812193323Sed      // oops_do() no longer applies after doit() is finished.
813193323Sed      oop obj = locals->obj_at(_index)();
814193323Sed      _value.l = JNIHandles::make_local(_calling_thread, obj);
815193323Sed      break;
816193323Sed    }
817193323Sed    default: ShouldNotReachHere();
818193323Sed    }
819193323Sed  }
820193323Sed}
821193323Sed
822193323Sed
823193323Sedbool VM_GetOrSetLocal::allow_nested_vm_operations() const {
824193323Sed  return true; // May need to deoptimize
825193323Sed}
826193323Sed
827193323Sed
828193323Sed/////////////////////////////////////////////////////////////////////////////////////////
829193323Sed
830193323Sed//
831193323Sed// class JvmtiSuspendControl - see comments in jvmtiImpl.hpp
832193323Sed//
833193323Sed
834193323Sedbool JvmtiSuspendControl::suspend(JavaThread *java_thread) {
835193323Sed  // external suspend should have caught suspending a thread twice
836193323Sed
837198090Srdivacky  // Immediate suspension required for JPDA back-end so JVMTI agent threads do
838193323Sed  // not deadlock due to later suspension on transitions while holding
839193323Sed  // raw monitors.  Passing true causes the immediate suspension.
840193323Sed  // java_suspend() will catch threads in the process of exiting
841193323Sed  // and will ignore them.
842193323Sed  java_thread->java_suspend();
843193323Sed
844193323Sed  // It would be nice to have the following assertion in all the time,
845193323Sed  // but it is possible for a racing resume request to have resumed
846193323Sed  // this thread right after we suspended it. Temporarily enable this
847193323Sed  // assertion if you are chasing a different kind of bug.
848193323Sed  //
849193323Sed  // assert(java_lang_Thread::thread(java_thread->threadObj()) == NULL ||
850193323Sed  //   java_thread->is_being_ext_suspended(), "thread is not suspended");
851198090Srdivacky
852193323Sed  if (java_lang_Thread::thread(java_thread->threadObj()) == NULL) {
853193323Sed    // check again because we can get delayed in java_suspend():
854193323Sed    // the thread is in process of exiting.
855193323Sed    return false;
856193323Sed  }
857198090Srdivacky
858193323Sed  return true;
859193323Sed}
860193323Sed
861198090Srdivackybool JvmtiSuspendControl::resume(JavaThread *java_thread) {
862193323Sed  // external suspend should have caught resuming a thread twice
863193323Sed  assert(java_thread->is_being_ext_suspended(), "thread should be suspended");
864198090Srdivacky
865193323Sed  // resume thread
866193323Sed  {
867198090Srdivacky    // must always grab Threads_lock, see JVM_SuspendThread
868193323Sed    MutexLocker ml(Threads_lock);
869193323Sed    java_thread->java_resume();
870193323Sed  }
871193323Sed
872193323Sed  return true;
873193323Sed}
874193323Sed
875193323Sed
876193323Sedvoid JvmtiSuspendControl::print() {
877193323Sed#ifndef PRODUCT
878193323Sed  MutexLocker mu(Threads_lock);
879193323Sed  ResourceMark rm;
880193323Sed
881193323Sed  tty->print("Suspended Threads: [");
882193323Sed  for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) {
883193323Sed#if JVMTI_TRACE
884193323Sed    const char *name   = JvmtiTrace::safe_get_thread_name(thread);
885193323Sed#else
886193323Sed    const char *name   = "";
887193323Sed#endif /*JVMTI_TRACE */
888198090Srdivacky    tty->print("%s(%c ", name, thread->is_being_ext_suspended() ? 'S' : '_');
889193323Sed    if (!thread->has_last_Java_frame()) {
890193323Sed      tty->print("no stack");
891193323Sed    }
892193323Sed    tty->print(") ");
893193323Sed  }
894193323Sed  tty->print_cr("]");
895193323Sed#endif
896193323Sed}
897193323Sed