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