jvmtiExport.hpp revision 2062:3582bf76420e
1/* 2 * Copyright (c) 1998, 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_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 43class JvmtiEventControllerPrivate; 44class JvmtiManageCapabilities; 45class JvmtiEnv; 46class JvmtiThreadState; 47class 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// 68class 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 private: 144 // CompiledMethodUnload events are reported from the VM thread so they 145 // are collected in lists (of jmethodID/addresses) and the events are posted later 146 // from threads posting CompieldMethodLoad or DynamicCodeGenerated events. 147 static bool _have_pending_compiled_method_unload_events; 148 static GrowableArray<jmethodID>* _pending_compiled_method_unload_method_ids; 149 static GrowableArray<const void *>* _pending_compiled_method_unload_code_begins; 150 static JavaThread* _current_poster; 151 152 // tests if there are CompiledMethodUnload events pending 153 inline static bool have_pending_compiled_method_unload_events() { 154 return _have_pending_compiled_method_unload_events; 155 } 156 157 // posts any pending CompiledMethodUnload events. 158 static void post_pending_compiled_method_unload_events(); 159 160 // Perform the actual notification to interested JvmtiEnvs. 161 static void post_compiled_method_unload_internal(JavaThread* self, jmethodID mid, const void* code_begin); 162 163 // posts a DynamicCodeGenerated event (internal/private implementation). 164 // The public post_dynamic_code_generated* functions make use of the 165 // internal implementation. 166 static void post_dynamic_code_generated_internal(const char *name, const void *code_begin, const void *code_end) KERNEL_RETURN; 167 168 169 // GenerateEvents support to allow posting of CompiledMethodLoad and 170 // DynamicCodeGenerated events for a given environment. 171 friend class JvmtiCodeBlobEvents; 172 173 static void post_compiled_method_load(JvmtiEnv* env, const jmethodID method, const jint length, 174 const void *code_begin, const jint map_length, 175 const jvmtiAddrLocationMap* map) KERNEL_RETURN; 176 static void post_dynamic_code_generated(JvmtiEnv* env, const char *name, const void *code_begin, 177 const void *code_end) KERNEL_RETURN; 178 179 // The RedefineClasses() API breaks some invariants in the "regular" 180 // system. For example, there are sanity checks when GC'ing nmethods 181 // that require the containing class to be unloading. However, when a 182 // method is redefined, the old method and nmethod can become GC'able 183 // without the containing class unloading. The state of becoming 184 // GC'able can be asynchronous to the RedefineClasses() call since 185 // the old method may still be running and cannot be GC'ed until 186 // after all old invocations have finished. Additionally, a method 187 // that has not been redefined may have an nmethod that depends on 188 // the redefined method. The dependent nmethod will get deopted in 189 // this case and may also be GC'able without the containing class 190 // being unloaded. 191 // 192 // This flag indicates whether RedefineClasses() has ever redefined 193 // one or more classes during the lifetime of the VM. The flag should 194 // only be set by the friend class and can be queried by other sub 195 // systems as needed to relax invariant checks. 196 static bool _has_redefined_a_class; 197 friend class VM_RedefineClasses; 198 inline static void set_has_redefined_a_class() { 199 _has_redefined_a_class = true; 200 } 201 202 // Flag to indicate if the compiler has recorded all dependencies. When the 203 // can_redefine_classes capability is enabled in the OnLoad phase then the compiler 204 // records all dependencies from startup. However if the capability is first 205 // enabled some time later then the dependencies recorded by the compiler 206 // are incomplete. This flag is used by RedefineClasses to know if the 207 // dependency information is complete or not. 208 static bool _all_dependencies_are_recorded; 209 210 public: 211 inline static bool has_redefined_a_class() { 212 return _has_redefined_a_class; 213 } 214 215 inline static bool all_dependencies_are_recorded() { 216 return _all_dependencies_are_recorded; 217 } 218 219 inline static void set_all_dependencies_are_recorded(bool on) { 220 _all_dependencies_are_recorded = (on != 0); 221 } 222 223 224 // let JVMTI know that the JVM_OnLoad code is running 225 static void enter_onload_phase(); 226 227 // let JVMTI know that the VM isn't up yet (and JVM_OnLoad code isn't running) 228 static void enter_primordial_phase(); 229 230 // let JVMTI know that the VM isn't up yet but JNI is live 231 static void enter_start_phase(); 232 233 // let JVMTI know that the VM is fully up and running now 234 static void enter_live_phase(); 235 236 // ------ can_* conditions (below) are set at OnLoad and never changed ------------ 237 inline static bool can_modify_any_class() { return _can_modify_any_class; } 238 inline static bool can_access_local_variables() { return _can_access_local_variables; } 239 inline static bool can_hotswap_or_post_breakpoint() { return _can_hotswap_or_post_breakpoint; } 240 inline static bool can_walk_any_space() { return _can_walk_any_space; } 241 242 // field access management 243 static address get_field_access_count_addr(); 244 245 // field modification management 246 static address get_field_modification_count_addr(); 247 248 // ----------------- 249 250 static bool is_jvmti_version(jint version) { return (version & JVMTI_VERSION_MASK) == JVMTI_VERSION_VALUE; } 251 static bool is_jvmdi_version(jint version) { return (version & JVMTI_VERSION_MASK) == JVMDI_VERSION_VALUE; } 252 static jint get_jvmti_interface(JavaVM *jvm, void **penv, jint version); 253 static void decode_version_values(jint version, int * major, int * minor, 254 int * micro); 255 256 // single stepping management methods 257 static void at_single_stepping_point(JavaThread *thread, methodOop method, address location) KERNEL_RETURN; 258 static void expose_single_stepping(JavaThread *thread) KERNEL_RETURN; 259 static bool hide_single_stepping(JavaThread *thread) KERNEL_RETURN_(return false;); 260 261 // Methods that notify the debugger that something interesting has happened in the VM. 262 static void post_vm_start (); 263 static void post_vm_initialized (); 264 static void post_vm_death (); 265 266 static void post_single_step (JavaThread *thread, methodOop method, address location) KERNEL_RETURN; 267 static void post_raw_breakpoint (JavaThread *thread, methodOop method, address location) KERNEL_RETURN; 268 269 static void post_exception_throw (JavaThread *thread, methodOop method, address location, oop exception) KERNEL_RETURN; 270 static void notice_unwind_due_to_exception (JavaThread *thread, methodOop method, address location, oop exception, bool in_handler_frame) KERNEL_RETURN; 271 272 static oop jni_GetField_probe (JavaThread *thread, jobject jobj, 273 oop obj, klassOop klass, jfieldID fieldID, bool is_static) 274 KERNEL_RETURN_(return NULL;); 275 static oop jni_GetField_probe_nh (JavaThread *thread, jobject jobj, 276 oop obj, klassOop klass, jfieldID fieldID, bool is_static) 277 KERNEL_RETURN_(return NULL;); 278 static void post_field_access_by_jni (JavaThread *thread, oop obj, 279 klassOop klass, jfieldID fieldID, bool is_static) KERNEL_RETURN; 280 static void post_field_access (JavaThread *thread, methodOop method, 281 address location, KlassHandle field_klass, Handle object, jfieldID field) KERNEL_RETURN; 282 static oop jni_SetField_probe (JavaThread *thread, jobject jobj, 283 oop obj, klassOop klass, jfieldID fieldID, bool is_static, char sig_type, 284 jvalue *value) KERNEL_RETURN_(return NULL;); 285 static oop jni_SetField_probe_nh (JavaThread *thread, jobject jobj, 286 oop obj, klassOop klass, jfieldID fieldID, bool is_static, char sig_type, 287 jvalue *value) KERNEL_RETURN_(return NULL;); 288 static void post_field_modification_by_jni(JavaThread *thread, oop obj, 289 klassOop klass, jfieldID fieldID, bool is_static, char sig_type, 290 jvalue *value); 291 static void post_raw_field_modification(JavaThread *thread, methodOop method, 292 address location, KlassHandle field_klass, Handle object, jfieldID field, 293 char sig_type, jvalue *value) KERNEL_RETURN; 294 295 static void post_method_entry (JavaThread *thread, methodOop method, frame current_frame) KERNEL_RETURN; 296 static void post_method_exit (JavaThread *thread, methodOop method, frame current_frame) KERNEL_RETURN; 297 298 static void post_class_load (JavaThread *thread, klassOop klass) KERNEL_RETURN; 299 static void post_class_unload (klassOop klass) KERNEL_RETURN; 300 static void post_class_prepare (JavaThread *thread, klassOop klass) KERNEL_RETURN; 301 302 static void post_thread_start (JavaThread *thread) KERNEL_RETURN; 303 static void post_thread_end (JavaThread *thread) KERNEL_RETURN; 304 305 // Support for java.lang.instrument agent loading. 306 static bool _should_post_class_file_load_hook; 307 inline static void set_should_post_class_file_load_hook(bool on) { _should_post_class_file_load_hook = on; } 308 inline static bool should_post_class_file_load_hook() { return _should_post_class_file_load_hook; } 309 static void post_class_file_load_hook(Symbol* h_name, Handle class_loader, 310 Handle h_protection_domain, 311 unsigned char **data_ptr, unsigned char **end_ptr, 312 unsigned char **cached_data_ptr, 313 jint *cached_length_ptr); 314 static void post_native_method_bind(methodOop method, address* function_ptr) KERNEL_RETURN; 315 static void post_compiled_method_load(nmethod *nm) KERNEL_RETURN; 316 static void post_dynamic_code_generated(const char *name, const void *code_begin, const void *code_end) KERNEL_RETURN; 317 318 // used to post a CompiledMethodUnload event 319 static void post_compiled_method_unload(jmethodID mid, const void *code_begin) KERNEL_RETURN; 320 321 // similiar to post_dynamic_code_generated except that it can be used to 322 // post a DynamicCodeGenerated event while holding locks in the VM. Any event 323 // posted using this function is recorded by the enclosing event collector 324 // -- JvmtiDynamicCodeEventCollector. 325 static void post_dynamic_code_generated_while_holding_locks(const char* name, address code_begin, address code_end) KERNEL_RETURN; 326 327 static void post_garbage_collection_finish() KERNEL_RETURN; 328 static void post_garbage_collection_start() KERNEL_RETURN; 329 static void post_data_dump() KERNEL_RETURN; 330 static void post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) KERNEL_RETURN; 331 static void post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) KERNEL_RETURN; 332 static void post_monitor_wait(JavaThread *thread, oop obj, jlong timeout) KERNEL_RETURN; 333 static void post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) KERNEL_RETURN; 334 static void post_object_free(JvmtiEnv* env, jlong tag) KERNEL_RETURN; 335 static void post_resource_exhausted(jint resource_exhausted_flags, const char* detail) KERNEL_RETURN; 336 static void record_vm_internal_object_allocation(oop object) KERNEL_RETURN; 337 // Post objects collected by vm_object_alloc_event_collector. 338 static void post_vm_object_alloc(JavaThread *thread, oop object) KERNEL_RETURN; 339 // Collects vm internal objects for later event posting. 340 inline static void vm_object_alloc_event_collector(oop object) { 341 if (should_post_vm_object_alloc()) { 342 record_vm_internal_object_allocation(object); 343 } 344 } 345 346 static void cleanup_thread (JavaThread* thread) KERNEL_RETURN; 347 348 static void oops_do(OopClosure* f) KERNEL_RETURN; 349 static void weak_oops_do(BoolObjectClosure* b, OopClosure* f) KERNEL_RETURN; 350 static void gc_epilogue() KERNEL_RETURN; 351 352 static void transition_pending_onload_raw_monitors() KERNEL_RETURN; 353 354#ifndef SERVICES_KERNEL 355 // attach support 356 static jint load_agent_library(AttachOperation* op, outputStream* out); 357#endif // SERVICES_KERNEL 358 359 // SetNativeMethodPrefix support 360 static char** get_all_native_method_prefixes(int* count_ptr); 361}; 362 363// Support class used by JvmtiDynamicCodeEventCollector and others. It 364// describes a single code blob by name and address range. 365class JvmtiCodeBlobDesc : public CHeapObj { 366 private: 367 char _name[64]; 368 address _code_begin; 369 address _code_end; 370 371 public: 372 JvmtiCodeBlobDesc(const char *name, address code_begin, address code_end) { 373 assert(name != NULL, "all code blobs must be named"); 374 strncpy(_name, name, sizeof(_name)); 375 _name[sizeof(_name)-1] = '\0'; 376 _code_begin = code_begin; 377 _code_end = code_end; 378 } 379 char* name() { return _name; } 380 address code_begin() { return _code_begin; } 381 address code_end() { return _code_end; } 382}; 383 384// JvmtiEventCollector is a helper class to setup thread for 385// event collection. 386class JvmtiEventCollector : public StackObj { 387 private: 388 JvmtiEventCollector* _prev; // Save previous one to support nested event collector. 389 390 public: 391 void setup_jvmti_thread_state(); // Set this collector in current thread. 392 void unset_jvmti_thread_state(); // Reset previous collector in current thread. 393 virtual bool is_dynamic_code_event() { return false; } 394 virtual bool is_vm_object_alloc_event(){ return false; } 395 JvmtiEventCollector *get_prev() { return _prev; } 396}; 397 398// A JvmtiDynamicCodeEventCollector is a helper class for the JvmtiExport 399// interface. It collects "dynamic code generated" events that are posted 400// while holding locks. When the event collector goes out of scope the 401// events will be posted. 402// 403// Usage :- 404// 405// { 406// JvmtiDynamicCodeEventCollector event_collector; 407// : 408// { MutexLocker ml(...) 409// : 410// JvmtiExport::post_dynamic_code_generated_while_holding_locks(...) 411// } 412// // event collector goes out of scope => post events to profiler. 413// } 414 415class JvmtiDynamicCodeEventCollector : public JvmtiEventCollector { 416 private: 417 GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected code blob events 418 419 friend class JvmtiExport; 420 void register_stub(const char* name, address start, address end); 421 422 public: 423 JvmtiDynamicCodeEventCollector() KERNEL_RETURN; 424 ~JvmtiDynamicCodeEventCollector() KERNEL_RETURN; 425 bool is_dynamic_code_event() { return true; } 426 427}; 428 429// Used to record vm internally allocated object oops and post 430// vm object alloc event for objects visible to java world. 431// Constructor enables JvmtiThreadState flag and all vm allocated 432// objects are recorded in a growable array. When destructor is 433// called the vm object alloc event is posted for each objects 434// visible to java world. 435// See jvm.cpp file for its usage. 436// 437class JvmtiVMObjectAllocEventCollector : public JvmtiEventCollector { 438 private: 439 GrowableArray<oop>* _allocated; // field to record vm internally allocated object oop. 440 bool _enable; // This flag is enabled in constructor and disabled 441 // in destructor before posting event. To avoid 442 // collection of objects allocated while running java code inside 443 // agent post_vm_object_alloc() event handler. 444 445 //GC support 446 void oops_do(OopClosure* f); 447 448 friend class JvmtiExport; 449 // Record vm allocated object oop. 450 inline void record_allocation(oop obj); 451 452 //GC support 453 static void oops_do_for_all_threads(OopClosure* f); 454 455 public: 456 JvmtiVMObjectAllocEventCollector() KERNEL_RETURN; 457 ~JvmtiVMObjectAllocEventCollector() KERNEL_RETURN; 458 bool is_vm_object_alloc_event() { return true; } 459 460 bool is_enabled() { return _enable; } 461 void set_enabled(bool on) { _enable = on; } 462}; 463 464 465 466// Marker class to disable the posting of VMObjectAlloc events 467// within its scope. 468// 469// Usage :- 470// 471// { 472// NoJvmtiVMObjectAllocMark njm; 473// : 474// // VMObjAlloc event will not be posted 475// JvmtiExport::vm_object_alloc_event_collector(obj); 476// : 477// } 478 479class NoJvmtiVMObjectAllocMark : public StackObj { 480 private: 481 // enclosing collector if enabled, NULL otherwise 482 JvmtiVMObjectAllocEventCollector *_collector; 483 484 bool was_enabled() { return _collector != NULL; } 485 486 public: 487 NoJvmtiVMObjectAllocMark() KERNEL_RETURN; 488 ~NoJvmtiVMObjectAllocMark() KERNEL_RETURN; 489}; 490 491 492// Base class for reporting GC events to JVMTI. 493class JvmtiGCMarker : public StackObj { 494 public: 495 JvmtiGCMarker() KERNEL_RETURN; 496 ~JvmtiGCMarker() KERNEL_RETURN; 497}; 498 499// JvmtiHideSingleStepping is a helper class for hiding 500// internal single step events. 501class JvmtiHideSingleStepping : public StackObj { 502 private: 503 bool _single_step_hidden; 504 JavaThread * _thread; 505 506 public: 507 JvmtiHideSingleStepping(JavaThread * thread) { 508 assert(thread != NULL, "sanity check"); 509 510 _single_step_hidden = false; 511 _thread = thread; 512 if (JvmtiExport::should_post_single_step()) { 513 _single_step_hidden = JvmtiExport::hide_single_stepping(_thread); 514 } 515 } 516 517 ~JvmtiHideSingleStepping() { 518 if (_single_step_hidden) { 519 JvmtiExport::expose_single_stepping(_thread); 520 } 521 } 522}; 523 524#endif // SHARE_VM_PRIMS_JVMTIEXPORT_HPP 525