1 /* 2 * Copyright (c) 1998, 2011, 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_JVMTIEXPORT_HPP 26 #define SHARE_VM_PRIMS_JVMTIEXPORT_HPP 27 28 #include "jvmtifiles/jvmti.h" 29 #include "memory/allocation.hpp" 30 #include "memory/iterator.hpp" 31 #include "oops/oop.hpp" 32 #include "oops/oopsHierarchy.hpp" 33 #include "runtime/frame.hpp" 34 #include "runtime/handles.hpp" 35 #include "utilities/globalDefinitions.hpp" 36 #include "utilities/growableArray.hpp" 37 38 // Must be included after jvmti.h. 39 #include "code/jvmticmlr.h" 40 41 // Forward declarations 42 43 class JvmtiEventControllerPrivate; 44 class JvmtiManageCapabilities; 45 class JvmtiEnv; 46 class JvmtiThreadState; 47 class AttachOperation; 48 49 #ifndef JVMTI_KERNEL 50 #define JVMTI_SUPPORT_FLAG(key) \ 51 private: \ 52 static bool _##key; \ 53 public: \ 54 inline static void set_##key(bool on) { _##key = (on != 0); } \ 55 inline static bool key() { return _##key; } 56 #else // JVMTI_KERNEL 57 #define JVMTI_SUPPORT_FLAG(key) \ 58 private: \ 59 const static bool _##key = false; \ 60 public: \ 61 inline static void set_##key(bool on) { report_unsupported(on); } \ 62 inline static bool key() { return _##key; } 63 #endif // JVMTI_KERNEL 64 65 66 // This class contains the JVMTI interface for the rest of hotspot. 67 // 68 class JvmtiExport : public AllStatic { 69 private: 70 static int _field_access_count; 71 static int _field_modification_count; 72 73 static bool _can_access_local_variables; 74 static bool _can_hotswap_or_post_breakpoint; 75 static bool _can_modify_any_class; 76 static bool _can_walk_any_space; 77 78 JVMTI_SUPPORT_FLAG(can_get_source_debug_extension) 79 JVMTI_SUPPORT_FLAG(can_maintain_original_method_order) 80 JVMTI_SUPPORT_FLAG(can_post_interpreter_events) 81 JVMTI_SUPPORT_FLAG(can_post_on_exceptions) 82 JVMTI_SUPPORT_FLAG(can_post_breakpoint) 83 JVMTI_SUPPORT_FLAG(can_post_field_access) 84 JVMTI_SUPPORT_FLAG(can_post_field_modification) 85 JVMTI_SUPPORT_FLAG(can_post_method_entry) 86 JVMTI_SUPPORT_FLAG(can_post_method_exit) 87 JVMTI_SUPPORT_FLAG(can_pop_frame) 88 JVMTI_SUPPORT_FLAG(can_force_early_return) 89 90 friend class JvmtiEventControllerPrivate; // should only modify these flags 91 JVMTI_SUPPORT_FLAG(should_post_single_step) 92 JVMTI_SUPPORT_FLAG(should_post_field_access) 93 JVMTI_SUPPORT_FLAG(should_post_field_modification) 94 JVMTI_SUPPORT_FLAG(should_post_class_load) 95 JVMTI_SUPPORT_FLAG(should_post_class_prepare) 96 JVMTI_SUPPORT_FLAG(should_post_class_unload) 97 JVMTI_SUPPORT_FLAG(should_post_native_method_bind) 98 JVMTI_SUPPORT_FLAG(should_post_compiled_method_load) 99 JVMTI_SUPPORT_FLAG(should_post_compiled_method_unload) 100 JVMTI_SUPPORT_FLAG(should_post_dynamic_code_generated) 101 JVMTI_SUPPORT_FLAG(should_post_monitor_contended_enter) 102 JVMTI_SUPPORT_FLAG(should_post_monitor_contended_entered) 103 JVMTI_SUPPORT_FLAG(should_post_monitor_wait) 104 JVMTI_SUPPORT_FLAG(should_post_monitor_waited) 105 JVMTI_SUPPORT_FLAG(should_post_data_dump) 106 JVMTI_SUPPORT_FLAG(should_post_garbage_collection_start) 107 JVMTI_SUPPORT_FLAG(should_post_garbage_collection_finish) 108 JVMTI_SUPPORT_FLAG(should_post_on_exceptions) 109 110 // ------ the below maybe don't have to be (but are for now) 111 // fixed conditions here ------------ 112 // any events can be enabled 113 JVMTI_SUPPORT_FLAG(should_post_thread_life) 114 JVMTI_SUPPORT_FLAG(should_post_object_free) 115 JVMTI_SUPPORT_FLAG(should_post_resource_exhausted) 116 117 // we are holding objects on the heap - need to talk to GC - e.g. 118 // breakpoint info 119 JVMTI_SUPPORT_FLAG(should_clean_up_heap_objects) 120 JVMTI_SUPPORT_FLAG(should_post_vm_object_alloc) 121 122 // If flag cannot be implemented, give an error if on=true 123 static void report_unsupported(bool on); 124 125 // these should only be called by the friend class 126 friend class JvmtiManageCapabilities; 127 inline static void set_can_modify_any_class(bool on) { _can_modify_any_class = (on != 0); } 128 inline static void set_can_access_local_variables(bool on) { _can_access_local_variables = (on != 0); } 129 inline static void set_can_hotswap_or_post_breakpoint(bool on) { _can_hotswap_or_post_breakpoint = (on != 0); } 130 inline static void set_can_walk_any_space(bool on) { _can_walk_any_space = (on != 0); } 131 132 enum { 133 JVMTI_VERSION_MASK = 0x70000000, 134 JVMTI_VERSION_VALUE = 0x30000000, 135 JVMDI_VERSION_VALUE = 0x20000000 136 }; 137 138 static void post_field_modification(JavaThread *thread, methodOop method, address location, 139 KlassHandle field_klass, Handle object, jfieldID field, 140 char sig_type, jvalue *value); 141 142 143 // posts a DynamicCodeGenerated event (internal/private implementation). 144 // The public post_dynamic_code_generated* functions make use of the 145 // internal implementation. Also called from JvmtiDeferredEvent::post() 146 static void post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) KERNEL_RETURN; 147 148 private: 149 150 // GenerateEvents support to allow posting of CompiledMethodLoad and 151 // DynamicCodeGenerated events for a given environment. 152 friend class JvmtiCodeBlobEvents; 153 154 static void post_compiled_method_load(JvmtiEnv* env, const jmethodID method, const jint length, 155 const void *code_begin, const jint map_length, 156 const jvmtiAddrLocationMap* map) KERNEL_RETURN; 157 static void post_dynamic_code_generated(JvmtiEnv* env, const char *name, const void *code_begin, 158 const void *code_end) KERNEL_RETURN; 159 160 // The RedefineClasses() API breaks some invariants in the "regular" 161 // system. For example, there are sanity checks when GC'ing nmethods 162 // that require the containing class to be unloading. However, when a 163 // method is redefined, the old method and nmethod can become GC'able 164 // without the containing class unloading. The state of becoming 165 // GC'able can be asynchronous to the RedefineClasses() call since 166 // the old method may still be running and cannot be GC'ed until 167 // after all old invocations have finished. Additionally, a method 168 // that has not been redefined may have an nmethod that depends on 169 // the redefined method. The dependent nmethod will get deopted in 170 // this case and may also be GC'able without the containing class 171 // being unloaded. 172 // 173 // This flag indicates whether RedefineClasses() has ever redefined 174 // one or more classes during the lifetime of the VM. The flag should 175 // only be set by the friend class and can be queried by other sub 176 // systems as needed to relax invariant checks. 177 static bool _has_redefined_a_class; 178 friend class VM_RedefineClasses; 179 inline static void set_has_redefined_a_class() { 180 _has_redefined_a_class = true; 181 } 182 183 // Flag to indicate if the compiler has recorded all dependencies. When the 184 // can_redefine_classes capability is enabled in the OnLoad phase then the compiler 185 // records all dependencies from startup. However if the capability is first 186 // enabled some time later then the dependencies recorded by the compiler 187 // are incomplete. This flag is used by RedefineClasses to know if the 188 // dependency information is complete or not. 189 static bool _all_dependencies_are_recorded; 190 191 public: 192 inline static bool has_redefined_a_class() { 193 return _has_redefined_a_class; 194 } 195 196 inline static bool all_dependencies_are_recorded() { 197 return _all_dependencies_are_recorded; 198 } 199 200 inline static void set_all_dependencies_are_recorded(bool on) { 201 _all_dependencies_are_recorded = (on != 0); 202 } 203 204 205 // let JVMTI know that the JVM_OnLoad code is running 206 static void enter_onload_phase(); 207 208 // let JVMTI know that the VM isn't up yet (and JVM_OnLoad code isn't running) 209 static void enter_primordial_phase(); 210 211 // let JVMTI know that the VM isn't up yet but JNI is live 212 static void enter_start_phase(); 213 214 // let JVMTI know that the VM is fully up and running now 215 static void enter_live_phase(); 216 217 // ------ can_* conditions (below) are set at OnLoad and never changed ------------ 218 inline static bool can_modify_any_class() { return _can_modify_any_class; } 219 inline static bool can_access_local_variables() { return _can_access_local_variables; } 220 inline static bool can_hotswap_or_post_breakpoint() { return _can_hotswap_or_post_breakpoint; } 221 inline static bool can_walk_any_space() { return _can_walk_any_space; } 222 223 // field access management 224 static address get_field_access_count_addr(); 225 226 // field modification management 227 static address get_field_modification_count_addr(); 228 229 // ----------------- 230 231 static bool is_jvmti_version(jint version) { return (version & JVMTI_VERSION_MASK) == JVMTI_VERSION_VALUE; } 232 static bool is_jvmdi_version(jint version) { return (version & JVMTI_VERSION_MASK) == JVMDI_VERSION_VALUE; } 233 static jint get_jvmti_interface(JavaVM *jvm, void **penv, jint version); 234 static void decode_version_values(jint version, int * major, int * minor, 235 int * micro); 236 237 // single stepping management methods 238 static void at_single_stepping_point(JavaThread *thread, methodOop method, address location) KERNEL_RETURN; 239 static void expose_single_stepping(JavaThread *thread) KERNEL_RETURN; 240 static bool hide_single_stepping(JavaThread *thread) KERNEL_RETURN_(false); 241 242 // Methods that notify the debugger that something interesting has happened in the VM. 243 static void post_vm_start (); 244 static void post_vm_initialized (); 245 static void post_vm_death (); 246 247 static void post_single_step (JavaThread *thread, methodOop method, address location) KERNEL_RETURN; 248 static void post_raw_breakpoint (JavaThread *thread, methodOop method, address location) KERNEL_RETURN; 249 250 static void post_exception_throw (JavaThread *thread, methodOop method, address location, oop exception) KERNEL_RETURN; 251 static void notice_unwind_due_to_exception (JavaThread *thread, methodOop method, address location, oop exception, bool in_handler_frame) KERNEL_RETURN; 252 253 static oop jni_GetField_probe (JavaThread *thread, jobject jobj, 254 oop obj, klassOop klass, jfieldID fieldID, bool is_static) 255 KERNEL_RETURN_(NULL); 256 static oop jni_GetField_probe_nh (JavaThread *thread, jobject jobj, 257 oop obj, klassOop klass, jfieldID fieldID, bool is_static) 258 KERNEL_RETURN_(NULL); 259 static void post_field_access_by_jni (JavaThread *thread, oop obj, 260 klassOop klass, jfieldID fieldID, bool is_static) KERNEL_RETURN; 261 static void post_field_access (JavaThread *thread, methodOop method, 262 address location, KlassHandle field_klass, Handle object, jfieldID field) KERNEL_RETURN; 263 static oop jni_SetField_probe (JavaThread *thread, jobject jobj, 264 oop obj, klassOop klass, jfieldID fieldID, bool is_static, char sig_type, 265 jvalue *value) KERNEL_RETURN_(NULL); 266 static oop jni_SetField_probe_nh (JavaThread *thread, jobject jobj, 267 oop obj, klassOop klass, jfieldID fieldID, bool is_static, char sig_type, 268 jvalue *value) KERNEL_RETURN_(NULL); 269 static void post_field_modification_by_jni(JavaThread *thread, oop obj, 270 klassOop klass, jfieldID fieldID, bool is_static, char sig_type, 271 jvalue *value); 272 static void post_raw_field_modification(JavaThread *thread, methodOop method, 273 address location, KlassHandle field_klass, Handle object, jfieldID field, 274 char sig_type, jvalue *value) KERNEL_RETURN; 275 276 static void post_method_entry (JavaThread *thread, methodOop method, frame current_frame) KERNEL_RETURN; 277 static void post_method_exit (JavaThread *thread, methodOop method, frame current_frame) KERNEL_RETURN; 278 279 static void post_class_load (JavaThread *thread, klassOop klass) KERNEL_RETURN; 280 static void post_class_unload (klassOop klass) KERNEL_RETURN; 281 static void post_class_prepare (JavaThread *thread, klassOop klass) KERNEL_RETURN; 282 283 static void post_thread_start (JavaThread *thread) KERNEL_RETURN; 284 static void post_thread_end (JavaThread *thread) KERNEL_RETURN; 285 286 // Support for java.lang.instrument agent loading. 287 static bool _should_post_class_file_load_hook; 288 inline static void set_should_post_class_file_load_hook(bool on) { _should_post_class_file_load_hook = on; } 289 inline static bool should_post_class_file_load_hook() { return _should_post_class_file_load_hook; } 290 static void post_class_file_load_hook(Symbol* h_name, Handle class_loader, 291 Handle h_protection_domain, 292 unsigned char **data_ptr, unsigned char **end_ptr, 293 unsigned char **cached_data_ptr, 294 jint *cached_length_ptr); 295 static void post_native_method_bind(methodOop method, address* function_ptr) KERNEL_RETURN; 296 static void post_compiled_method_load(nmethod *nm) KERNEL_RETURN; 297 static void post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) KERNEL_RETURN; 298 299 // used to post a CompiledMethodUnload event 300 static void post_compiled_method_unload(jmethodID mid, const void *code_begin) KERNEL_RETURN; 301 302 // similiar to post_dynamic_code_generated except that it can be used to 303 // post a DynamicCodeGenerated event while holding locks in the VM. Any event 304 // posted using this function is recorded by the enclosing event collector 305 // -- JvmtiDynamicCodeEventCollector. 306 static void post_dynamic_code_generated_while_holding_locks(const char* name, address code_begin, address code_end) KERNEL_RETURN; 307 308 static void post_garbage_collection_finish() KERNEL_RETURN; 309 static void post_garbage_collection_start() KERNEL_RETURN; 310 static void post_data_dump() KERNEL_RETURN; 311 static void post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) KERNEL_RETURN; 312 static void post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) KERNEL_RETURN; 313 static void post_monitor_wait(JavaThread *thread, oop obj, jlong timeout) KERNEL_RETURN; 314 static void post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) KERNEL_RETURN; 315 static void post_object_free(JvmtiEnv* env, jlong tag) KERNEL_RETURN; 316 static void post_resource_exhausted(jint resource_exhausted_flags, const char* detail) KERNEL_RETURN; 317 static void record_vm_internal_object_allocation(oop object) KERNEL_RETURN; 318 // Post objects collected by vm_object_alloc_event_collector. 319 static void post_vm_object_alloc(JavaThread *thread, oop object) KERNEL_RETURN; 320 // Collects vm internal objects for later event posting. 321 inline static void vm_object_alloc_event_collector(oop object) { 322 if (should_post_vm_object_alloc()) { 323 record_vm_internal_object_allocation(object); 324 } 325 } 326 327 static void cleanup_thread (JavaThread* thread) KERNEL_RETURN; 328 329 static void oops_do(OopClosure* f) KERNEL_RETURN; 330 static void weak_oops_do(BoolObjectClosure* b, OopClosure* f) KERNEL_RETURN; 331 static void gc_epilogue() KERNEL_RETURN; 332 333 static void transition_pending_onload_raw_monitors() KERNEL_RETURN; 334 335 #ifndef SERVICES_KERNEL 336 // attach support 337 static jint load_agent_library(AttachOperation* op, outputStream* out); 338 #endif // SERVICES_KERNEL 339 340 // SetNativeMethodPrefix support 341 static char** get_all_native_method_prefixes(int* count_ptr); 342 }; 343 344 // Support class used by JvmtiDynamicCodeEventCollector and others. It 345 // describes a single code blob by name and address range. 346 class JvmtiCodeBlobDesc : public CHeapObj { 347 private: 348 char _name[64]; 349 address _code_begin; 350 address _code_end; 351 352 public: 353 JvmtiCodeBlobDesc(const char *name, address code_begin, address code_end) { 354 assert(name != NULL, "all code blobs must be named"); 355 strncpy(_name, name, sizeof(_name)); 356 _name[sizeof(_name)-1] = '\0'; 357 _code_begin = code_begin; 358 _code_end = code_end; 359 } 360 char* name() { return _name; } 361 address code_begin() { return _code_begin; } 362 address code_end() { return _code_end; } 363 }; 364 365 // JvmtiEventCollector is a helper class to setup thread for 366 // event collection. 367 class JvmtiEventCollector : public StackObj { 368 private: 369 JvmtiEventCollector* _prev; // Save previous one to support nested event collector. 370 371 public: 372 void setup_jvmti_thread_state(); // Set this collector in current thread. 373 void unset_jvmti_thread_state(); // Reset previous collector in current thread. 374 virtual bool is_dynamic_code_event() { return false; } 375 virtual bool is_vm_object_alloc_event(){ return false; } 376 JvmtiEventCollector *get_prev() { return _prev; } 377 }; 378 379 // A JvmtiDynamicCodeEventCollector is a helper class for the JvmtiExport 380 // interface. It collects "dynamic code generated" events that are posted 381 // while holding locks. When the event collector goes out of scope the 382 // events will be posted. 383 // 384 // Usage :- 385 // 386 // { 387 // JvmtiDynamicCodeEventCollector event_collector; 388 // : 389 // { MutexLocker ml(...) 390 // : 391 // JvmtiExport::post_dynamic_code_generated_while_holding_locks(...) 392 // } 393 // // event collector goes out of scope => post events to profiler. 394 // } 395 396 class JvmtiDynamicCodeEventCollector : public JvmtiEventCollector { 397 private: 398 GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected code blob events 399 400 friend class JvmtiExport; 401 void register_stub(const char* name, address start, address end); 402 403 public: 404 JvmtiDynamicCodeEventCollector() KERNEL_RETURN; 405 ~JvmtiDynamicCodeEventCollector() KERNEL_RETURN; 406 bool is_dynamic_code_event() { return true; } 407 408 }; 409 410 // Used to record vm internally allocated object oops and post 411 // vm object alloc event for objects visible to java world. 412 // Constructor enables JvmtiThreadState flag and all vm allocated 413 // objects are recorded in a growable array. When destructor is 414 // called the vm object alloc event is posted for each objects 415 // visible to java world. 416 // See jvm.cpp file for its usage. 417 // 418 class JvmtiVMObjectAllocEventCollector : public JvmtiEventCollector { 419 private: 420 GrowableArray<oop>* _allocated; // field to record vm internally allocated object oop. 421 bool _enable; // This flag is enabled in constructor and disabled 422 // in destructor before posting event. To avoid 423 // collection of objects allocated while running java code inside 424 // agent post_vm_object_alloc() event handler. 425 426 //GC support 427 void oops_do(OopClosure* f); 428 429 friend class JvmtiExport; 430 // Record vm allocated object oop. 431 inline void record_allocation(oop obj); 432 433 //GC support 434 static void oops_do_for_all_threads(OopClosure* f); 435 436 public: 437 JvmtiVMObjectAllocEventCollector() KERNEL_RETURN; 438 ~JvmtiVMObjectAllocEventCollector() KERNEL_RETURN; 439 bool is_vm_object_alloc_event() { return true; } 440 441 bool is_enabled() { return _enable; } 442 void set_enabled(bool on) { _enable = on; } 443 }; 444 445 446 447 // Marker class to disable the posting of VMObjectAlloc events 448 // within its scope. 449 // 450 // Usage :- 451 // 452 // { 453 // NoJvmtiVMObjectAllocMark njm; 454 // : 455 // // VMObjAlloc event will not be posted 456 // JvmtiExport::vm_object_alloc_event_collector(obj); 457 // : 458 // } 459 460 class NoJvmtiVMObjectAllocMark : public StackObj { 461 private: 462 // enclosing collector if enabled, NULL otherwise 463 JvmtiVMObjectAllocEventCollector *_collector; 464 465 bool was_enabled() { return _collector != NULL; } 466 467 public: 468 NoJvmtiVMObjectAllocMark() KERNEL_RETURN; 469 ~NoJvmtiVMObjectAllocMark() KERNEL_RETURN; 470 }; 471 472 473 // Base class for reporting GC events to JVMTI. 474 class JvmtiGCMarker : public StackObj { 475 public: 476 JvmtiGCMarker() KERNEL_RETURN; 477 ~JvmtiGCMarker() KERNEL_RETURN; 478 }; 479 480 // JvmtiHideSingleStepping is a helper class for hiding 481 // internal single step events. 482 class JvmtiHideSingleStepping : public StackObj { 483 private: 484 bool _single_step_hidden; 485 JavaThread * _thread; 486 487 public: 488 JvmtiHideSingleStepping(JavaThread * thread) { 489 assert(thread != NULL, "sanity check"); 490 491 _single_step_hidden = false; 492 _thread = thread; 493 if (JvmtiExport::should_post_single_step()) { 494 _single_step_hidden = JvmtiExport::hide_single_stepping(_thread); 495 } 496 } 497 498 ~JvmtiHideSingleStepping() { 499 if (_single_step_hidden) { 500 JvmtiExport::expose_single_stepping(_thread); 501 } 502 } 503 }; 504 505 #endif // SHARE_VM_PRIMS_JVMTIEXPORT_HPP