1 /*
   2  * Copyright (c) 1999, 2007, Oracle and/or its affiliates. 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 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 //
  26 // Forward Declarations
  27 //
  28 
  29 class JvmtiBreakpoint;
  30 class JvmtiBreakpoints;
  31 
  32 
  33 ///////////////////////////////////////////////////////////////
  34 //
  35 // class GrowableCache, GrowableElement
  36 // Used by              : JvmtiBreakpointCache
  37 // Used by JVMTI methods: none directly.
  38 //
  39 // GrowableCache is a permanent CHeap growable array of <GrowableElement *>
  40 //
  41 // In addition, the GrowableCache maintains a NULL terminated cache array of type address
  42 // that's created from the element array using the function:
  43 //     address GrowableElement::getCacheValue().
  44 //
  45 // Whenever the GrowableArray changes size, the cache array gets recomputed into a new C_HEAP allocated
  46 // block of memory. Additionally, every time the cache changes its position in memory, the
  47 //    void (*_listener_fun)(void *this_obj, address* cache)
  48 // gets called with the cache's new address. This gives the user of the GrowableCache a callback
  49 // to update its pointer to the address cache.
  50 //
  51 
  52 class GrowableElement : public CHeapObj {
  53 public:
  54   virtual address getCacheValue()          =0;
  55   virtual bool equals(GrowableElement* e)  =0;
  56   virtual bool lessThan(GrowableElement *e)=0;
  57   virtual GrowableElement *clone()         =0;
  58   virtual void oops_do(OopClosure* f)      =0;
  59 };
  60 
  61 class GrowableCache VALUE_OBJ_CLASS_SPEC {
  62 
  63 private:
  64   // Object pointer passed into cache & listener functions.
  65   void *_this_obj;
  66 
  67   // Array of elements in the collection
  68   GrowableArray<GrowableElement *> *_elements;
  69 
  70   // Parallel array of cached values
  71   address *_cache;
  72 
  73   // Listener for changes to the _cache field.
  74   // Called whenever the _cache field has it's value changed
  75   // (but NOT when cached elements are recomputed).
  76   void (*_listener_fun)(void *, address*);
  77 
  78   static bool equals(void *, GrowableElement *);
  79 
  80   // recache all elements after size change, notify listener
  81   void recache();
  82 
  83 public:
  84    GrowableCache();
  85    ~GrowableCache();
  86 
  87   void initialize(void *this_obj, void listener_fun(void *, address*) );
  88 
  89   // number of elements in the collection
  90   int length();
  91   // get the value of the index element in the collection
  92   GrowableElement* at(int index);
  93   // find the index of the element, -1 if it doesn't exist
  94   int find(GrowableElement* e);
  95   // append a copy of the element to the end of the collection, notify listener
  96   void append(GrowableElement* e);
  97   // insert a copy of the element using lessthan(), notify listener
  98   void insert(GrowableElement* e);
  99   // remove the element at index, notify listener
 100   void remove (int index);
 101   // clear out all elements and release all heap space, notify listener
 102   void clear();
 103   // apply f to every element and update the cache
 104   void oops_do(OopClosure* f);
 105   void gc_epilogue();
 106 };
 107 
 108 
 109 ///////////////////////////////////////////////////////////////
 110 //
 111 // class JvmtiBreakpointCache
 112 // Used by              : JvmtiBreakpoints
 113 // Used by JVMTI methods: none directly.
 114 // Note   : typesafe wrapper for GrowableCache of JvmtiBreakpoint
 115 //
 116 
 117 class JvmtiBreakpointCache : public CHeapObj {
 118 
 119 private:
 120   GrowableCache _cache;
 121 
 122 public:
 123   JvmtiBreakpointCache()  {}
 124   ~JvmtiBreakpointCache() {}
 125 
 126   void initialize(void *this_obj, void listener_fun(void *, address*) ) {
 127     _cache.initialize(this_obj,listener_fun);
 128   }
 129 
 130   int length()                          { return _cache.length(); }
 131   JvmtiBreakpoint& at(int index)        { return (JvmtiBreakpoint&) *(_cache.at(index)); }
 132   int find(JvmtiBreakpoint& e)          { return _cache.find((GrowableElement *) &e); }
 133   void append(JvmtiBreakpoint& e)       { _cache.append((GrowableElement *) &e); }
 134   void remove (int index)               { _cache.remove(index); }
 135   void clear()                          { _cache.clear(); }
 136   void oops_do(OopClosure* f)           { _cache.oops_do(f); }
 137   void gc_epilogue()                    { _cache.gc_epilogue(); }
 138 };
 139 
 140 
 141 ///////////////////////////////////////////////////////////////
 142 //
 143 // class JvmtiBreakpoint
 144 // Used by              : JvmtiBreakpoints
 145 // Used by JVMTI methods: SetBreakpoint, ClearBreakpoint, ClearAllBreakpoints
 146 // Note: Extends GrowableElement for use in a GrowableCache
 147 //
 148 // A JvmtiBreakpoint describes a location (class, method, bci) to break at.
 149 //
 150 
 151 typedef void (methodOopDesc::*method_action)(int _bci);
 152 
 153 class JvmtiBreakpoint : public GrowableElement {
 154 private:
 155   methodOop             _method;
 156   int                   _bci;
 157   Bytecodes::Code       _orig_bytecode;
 158 
 159 public:
 160   JvmtiBreakpoint();
 161   JvmtiBreakpoint(methodOop m_method, jlocation location);
 162   bool equals(JvmtiBreakpoint& bp);
 163   bool lessThan(JvmtiBreakpoint &bp);
 164   void copy(JvmtiBreakpoint& bp);
 165   bool is_valid();
 166   address getBcp();
 167   void each_method_version_do(method_action meth_act);
 168   void set();
 169   void clear();
 170   void print();
 171 
 172   methodOop method() { return _method; }
 173 
 174   // GrowableElement implementation
 175   address getCacheValue()         { return getBcp(); }
 176   bool lessThan(GrowableElement* e) { Unimplemented(); return false; }
 177   bool equals(GrowableElement* e) { return equals((JvmtiBreakpoint&) *e); }
 178   void oops_do(OopClosure* f)     { f->do_oop((oop *) &_method); }
 179   GrowableElement *clone()        {
 180     JvmtiBreakpoint *bp = new JvmtiBreakpoint();
 181     bp->copy(*this);
 182     return bp;
 183   }
 184 };
 185 
 186 
 187 ///////////////////////////////////////////////////////////////
 188 //
 189 // class VM_ChangeBreakpoints
 190 // Used by              : JvmtiBreakpoints
 191 // Used by JVMTI methods: none directly.
 192 // Note: A Helper class.
 193 //
 194 // VM_ChangeBreakpoints implements a VM_Operation for ALL modifications to the JvmtiBreakpoints class.
 195 //
 196 
 197 class VM_ChangeBreakpoints : public VM_Operation {
 198 private:
 199   JvmtiBreakpoints* _breakpoints;
 200   int               _operation;
 201   JvmtiBreakpoint*  _bp;
 202 
 203 public:
 204   enum { SET_BREAKPOINT=0, CLEAR_BREAKPOINT=1, CLEAR_ALL_BREAKPOINT=2 };
 205 
 206   VM_ChangeBreakpoints(JvmtiBreakpoints* breakpoints, int operation) {
 207     _breakpoints = breakpoints;
 208     _bp = NULL;
 209     _operation = operation;
 210     assert(breakpoints != NULL, "breakpoints != NULL");
 211     assert(operation == CLEAR_ALL_BREAKPOINT, "unknown breakpoint operation");
 212   }
 213   VM_ChangeBreakpoints(JvmtiBreakpoints* breakpoints, int operation, JvmtiBreakpoint *bp) {
 214     _breakpoints = breakpoints;
 215     _bp = bp;
 216     _operation = operation;
 217     assert(breakpoints != NULL, "breakpoints != NULL");
 218     assert(bp != NULL, "bp != NULL");
 219     assert(operation == SET_BREAKPOINT || operation == CLEAR_BREAKPOINT , "unknown breakpoint operation");
 220   }
 221 
 222   VMOp_Type type() const { return VMOp_ChangeBreakpoints; }
 223   void doit();
 224   void oops_do(OopClosure* f);
 225 };
 226 
 227 
 228 ///////////////////////////////////////////////////////////////
 229 //
 230 // class JvmtiBreakpoints
 231 // Used by              : JvmtiCurrentBreakpoints
 232 // Used by JVMTI methods: none directly
 233 // Note: A Helper class
 234 //
 235 // JvmtiBreakpoints is a GrowableCache of JvmtiBreakpoint.
 236 // All changes to the GrowableCache occur at a safepoint using VM_ChangeBreakpoints.
 237 //
 238 // Because _bps is only modified at safepoints, its possible to always use the
 239 // cached byte code pointers from _bps without doing any synchronization (see JvmtiCurrentBreakpoints).
 240 //
 241 // It would be possible to make JvmtiBreakpoints a static class, but I've made it
 242 // CHeap allocated to emphasize its similarity to JvmtiFramePops.
 243 //
 244 
 245 class JvmtiBreakpoints : public CHeapObj {
 246 private:
 247 
 248   JvmtiBreakpointCache _bps;
 249 
 250   // These should only be used by VM_ChangeBreakpoints
 251   // to insure they only occur at safepoints.
 252   // Todo: add checks for safepoint
 253   friend class VM_ChangeBreakpoints;
 254   void set_at_safepoint(JvmtiBreakpoint& bp);
 255   void clear_at_safepoint(JvmtiBreakpoint& bp);
 256   void clearall_at_safepoint();
 257 
 258   static void do_element(GrowableElement *e);
 259 
 260 public:
 261   JvmtiBreakpoints(void listener_fun(void *, address *));
 262   ~JvmtiBreakpoints();
 263 
 264   int length();
 265   void oops_do(OopClosure* f);
 266   void gc_epilogue();
 267   void print();
 268 
 269   int  set(JvmtiBreakpoint& bp);
 270   int  clear(JvmtiBreakpoint& bp);
 271   void clearall_in_class_at_safepoint(klassOop klass);
 272   void clearall();
 273 };
 274 
 275 
 276 ///////////////////////////////////////////////////////////////
 277 //
 278 // class JvmtiCurrentBreakpoints
 279 //
 280 // A static wrapper class for the JvmtiBreakpoints that provides:
 281 // 1. a fast inlined function to check if a byte code pointer is a breakpoint (is_breakpoint).
 282 // 2. a function for lazily creating the JvmtiBreakpoints class (this is not strictly necessary,
 283 //    but I'm copying the code from JvmtiThreadState which needs to lazily initialize
 284 //    JvmtiFramePops).
 285 // 3. An oops_do entry point for GC'ing the breakpoint array.
 286 //
 287 
 288 class JvmtiCurrentBreakpoints : public AllStatic {
 289 
 290 private:
 291 
 292   // Current breakpoints, lazily initialized by get_jvmti_breakpoints();
 293   static JvmtiBreakpoints *_jvmti_breakpoints;
 294 
 295   // NULL terminated cache of byte-code pointers corresponding to current breakpoints.
 296   // Updated only at safepoints (with listener_fun) when the cache is moved.
 297   // It exists only to make is_breakpoint fast.
 298   static address          *_breakpoint_list;
 299   static inline void set_breakpoint_list(address *breakpoint_list) { _breakpoint_list = breakpoint_list; }
 300   static inline address *get_breakpoint_list()                     { return _breakpoint_list; }
 301 
 302   // Listener for the GrowableCache in _jvmti_breakpoints, updates _breakpoint_list.
 303   static void listener_fun(void *this_obj, address *cache);
 304 
 305 public:
 306   static void initialize();
 307   static void destroy();
 308 
 309   // lazily create _jvmti_breakpoints and _breakpoint_list
 310   static JvmtiBreakpoints& get_jvmti_breakpoints();
 311 
 312   // quickly test whether the bcp matches a cached breakpoint in the list
 313   static inline bool is_breakpoint(address bcp);
 314 
 315   static void oops_do(OopClosure* f);
 316   static void gc_epilogue();
 317 };
 318 
 319 // quickly test whether the bcp matches a cached breakpoint in the list
 320 bool JvmtiCurrentBreakpoints::is_breakpoint(address bcp) {
 321     address *bps = get_breakpoint_list();
 322     if (bps == NULL) return false;
 323     for ( ; (*bps) != NULL; bps++) {
 324       if ((*bps) == bcp) return true;
 325     }
 326     return false;
 327 }
 328 
 329 ///////////////////////////////////////////////////////////////
 330 // The get/set local operations must only be done by the VM thread
 331 // because the interpreter version needs to access oop maps, which can
 332 // only safely be done by the VM thread
 333 //
 334 // I'm told that in 1.5 oop maps are now protected by a lock and
 335 // we could get rid of the VM op
 336 // However if the VM op is removed then the target thread must
 337 // be suspended AND a lock will be needed to prevent concurrent
 338 // setting of locals to the same java thread. This lock is needed
 339 // to prevent compiledVFrames from trying to add deferred updates
 340 // to the thread simultaneously.
 341 //
 342 class VM_GetOrSetLocal : public VM_Operation {
 343 private:
 344   JavaThread* _thread;
 345   JavaThread* _calling_thread;
 346   jint        _depth;
 347   jint        _index;
 348   BasicType   _type;
 349   jvalue      _value;
 350   javaVFrame* _jvf;
 351   bool        _set;
 352 
 353   jvmtiError  _result;
 354 
 355   vframe* get_vframe();
 356   javaVFrame* get_java_vframe();
 357   bool check_slot_type(javaVFrame* vf);
 358 
 359 public:
 360   // Constructor for non-object getter
 361   VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type);
 362 
 363   // Constructor for object or non-object setter
 364   VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value);
 365 
 366   // Constructor for object getter
 367   VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth,
 368                    int index);
 369 
 370   VMOp_Type type() const { return VMOp_GetOrSetLocal; }
 371   jvalue value()         { return _value; }
 372   jvmtiError result()    { return _result; }
 373 
 374   bool doit_prologue();
 375   void doit();
 376   bool allow_nested_vm_operations() const;
 377   const char* name() const                       { return "get/set locals"; }
 378 
 379   // Check that the klass is assignable to a type with the given signature.
 380   static bool is_assignable(const char* ty_sign, Klass* klass, Thread* thread);
 381 };
 382 
 383 
 384 ///////////////////////////////////////////////////////////////
 385 //
 386 // class JvmtiSuspendControl
 387 //
 388 // Convenience routines for suspending and resuming threads.
 389 //
 390 // All attempts by JVMTI to suspend and resume threads must go through the
 391 // JvmtiSuspendControl interface.
 392 //
 393 // methods return true if successful
 394 //
 395 class JvmtiSuspendControl : public AllStatic {
 396 public:
 397   // suspend the thread, taking it to a safepoint
 398   static bool suspend(JavaThread *java_thread);
 399   // resume the thread
 400   static bool resume(JavaThread *java_thread);
 401 
 402   static void print();
 403 };
 404 
 405 // Utility macro that checks for NULL pointers:
 406 #define NULL_CHECK(X, Y) if ((X) == NULL) { return (Y); }