1 /* 2 * Copyright (c) 2003, 2017, 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_JVMTIENVBASE_HPP 26 #define SHARE_VM_PRIMS_JVMTIENVBASE_HPP 27 28 #include "classfile/classLoader.hpp" 29 #include "prims/jvmtiEnvThreadState.hpp" 30 #include "prims/jvmtiEventController.hpp" 31 #include "prims/jvmtiThreadState.hpp" 32 #include "prims/jvmtiThreadState.inline.hpp" 33 #include "runtime/fieldDescriptor.hpp" 34 #include "runtime/frame.hpp" 35 #include "runtime/handles.inline.hpp" 36 #include "runtime/thread.hpp" 37 #include "runtime/vm_operations.hpp" 38 #include "utilities/growableArray.hpp" 39 #include "utilities/macros.hpp" 40 41 // 42 // Forward Declarations 43 // 44 45 class JvmtiEnv; 46 class JvmtiThreadState; 47 class JvmtiRawMonitor; // for jvmtiEnv.hpp 48 class JvmtiEventControllerPrivate; 49 class JvmtiTagMap; 50 51 52 53 // One JvmtiEnv object is created per jvmti attachment; 54 // done via JNI GetEnv() call. Multiple attachments are 55 // allowed in jvmti. 56 57 class JvmtiEnvBase : public CHeapObj<mtInternal> { 58 59 private: 60 61 #if INCLUDE_JVMTI 62 static JvmtiEnvBase* _head_environment; // head of environment list 63 #endif // INCLUDE_JVMTI 64 65 static bool _globally_initialized; 66 static jvmtiPhase _phase; 67 static volatile int _dying_thread_env_iteration_count; 68 69 public: 70 71 enum { 72 JDK15_JVMTI_VERSION = JVMTI_VERSION_1_0 + 33, /* version: 1.0.33 */ 73 JDK16_JVMTI_VERSION = JVMTI_VERSION_1_1 + 102, /* version: 1.1.102 */ 74 JDK17_JVMTI_VERSION = JVMTI_VERSION_1_2 + 2 /* version: 1.2.2 */ 75 }; 76 77 static jvmtiPhase get_phase() { return _phase; } 78 static jvmtiPhase get_phase(jvmtiEnv* env) { return ((JvmtiEnvBase*)JvmtiEnv_from_jvmti_env(env))->phase(); } 79 static void set_phase(jvmtiPhase phase) { _phase = phase; } 80 static bool is_vm_live() { return _phase == JVMTI_PHASE_LIVE; } 81 82 static void entering_dying_thread_env_iteration() { ++_dying_thread_env_iteration_count; } 83 static void leaving_dying_thread_env_iteration() { --_dying_thread_env_iteration_count; } 84 static bool is_inside_dying_thread_env_iteration(){ return _dying_thread_env_iteration_count > 0; } 85 86 private: 87 88 enum { 89 JVMTI_MAGIC = 0x71EE, 90 DISPOSED_MAGIC = 0xDEFC, 91 BAD_MAGIC = 0xDEAD 92 }; 93 94 jvmtiEnv _jvmti_external; 95 jint _magic; 96 jint _version; // version value passed to JNI GetEnv() 97 JvmtiEnvBase* _next; 98 bool _is_retransformable; 99 const void *_env_local_storage; // per env agent allocated data. 100 jvmtiEventCallbacks _event_callbacks; 101 jvmtiExtEventCallbacks _ext_event_callbacks; 102 JvmtiTagMap* _tag_map; 103 JvmtiEnvEventEnable _env_event_enable; 104 jvmtiCapabilities _current_capabilities; 105 jvmtiCapabilities _prohibited_capabilities; 106 volatile bool _class_file_load_hook_ever_enabled; 107 static volatile bool _needs_clean_up; 108 char** _native_method_prefixes; 109 int _native_method_prefix_count; 110 111 protected: 112 JvmtiEnvBase(jint version); 113 ~JvmtiEnvBase(); 114 void dispose(); 115 void env_dispose(); 116 117 void set_env_local_storage(const void* data) { _env_local_storage = data; } 118 const void* get_env_local_storage() { return _env_local_storage; } 119 120 void record_class_file_load_hook_enabled(); 121 void record_first_time_class_file_load_hook_enabled(); 122 123 char** get_native_method_prefixes() { return _native_method_prefixes; } 124 int get_native_method_prefix_count() { return _native_method_prefix_count; } 125 jvmtiError set_native_method_prefixes(jint prefix_count, char** prefixes); 126 127 private: 128 friend class JvmtiEventControllerPrivate; 129 void initialize(); 130 void set_event_callbacks(const jvmtiEventCallbacks* callbacks, jint size_of_callbacks); 131 static void globally_initialize(); 132 static void periodic_clean_up(); 133 134 friend class JvmtiEnvIterator; 135 JvmtiEnv* next_environment() { return (JvmtiEnv*)_next; } 136 void set_next_environment(JvmtiEnvBase* env) { _next = env; } 137 static JvmtiEnv* head_environment() { 138 JVMTI_ONLY(return (JvmtiEnv*)_head_environment); 139 NOT_JVMTI(return NULL); 140 } 141 142 public: 143 144 jvmtiPhase phase(); 145 bool is_valid(); 146 147 bool use_version_1_0_semantics(); // agent asked for version 1.0 148 bool use_version_1_1_semantics(); // agent asked for version 1.1 149 bool use_version_1_2_semantics(); // agent asked for version 1.2 150 151 bool is_retransformable() { return _is_retransformable; } 152 153 static ByteSize jvmti_external_offset() { 154 return byte_offset_of(JvmtiEnvBase, _jvmti_external); 155 }; 156 157 static JvmtiEnv* JvmtiEnv_from_jvmti_env(jvmtiEnv *env) { 158 return (JvmtiEnv*)((intptr_t)env - in_bytes(jvmti_external_offset())); 159 }; 160 161 jvmtiCapabilities *get_capabilities() { return &_current_capabilities; } 162 163 jvmtiCapabilities *get_prohibited_capabilities() { return &_prohibited_capabilities; } 164 165 bool early_class_hook_env() { 166 return get_capabilities()->can_generate_early_class_hook_events != 0 167 && get_capabilities()->can_generate_all_class_hook_events != 0; 168 } 169 170 bool early_vmstart_env() { 171 return get_capabilities()->can_generate_early_vmstart != 0; 172 } 173 174 static char** get_all_native_method_prefixes(int* count_ptr); 175 176 // This test will answer true when all environments have been disposed and some have 177 // not yet been deallocated. As a result, this test should only be used as an 178 // optimization for the no environment case. 179 static bool environments_might_exist() { 180 return head_environment() != NULL; 181 } 182 183 static void check_for_periodic_clean_up(); 184 185 JvmtiEnvEventEnable *env_event_enable() { 186 return &_env_event_enable; 187 } 188 189 jvmtiError allocate(jlong size, unsigned char** mem_ptr) { 190 if (size < 0) { 191 return JVMTI_ERROR_ILLEGAL_ARGUMENT; 192 } 193 if (size == 0) { 194 *mem_ptr = NULL; 195 } else { 196 *mem_ptr = (unsigned char *)os::malloc((size_t)size, mtInternal); 197 if (*mem_ptr == NULL) { 198 return JVMTI_ERROR_OUT_OF_MEMORY; 199 } 200 } 201 return JVMTI_ERROR_NONE; 202 } 203 204 jvmtiError deallocate(unsigned char* mem) { 205 if (mem != NULL) { 206 os::free(mem); 207 } 208 return JVMTI_ERROR_NONE; 209 } 210 211 212 // Memory functions 213 unsigned char* jvmtiMalloc(jlong size); // don't use this - call allocate 214 215 // method to create a local handle 216 jobject jni_reference(Handle hndl) { 217 return JNIHandles::make_local(hndl()); 218 } 219 220 // method to create a local handle. 221 // This function allows caller to specify which 222 // threads local handle table to use. 223 jobject jni_reference(JavaThread *thread, Handle hndl) { 224 return JNIHandles::make_local(thread, hndl()); 225 } 226 227 // method to destroy a local handle 228 void destroy_jni_reference(jobject jobj) { 229 JNIHandles::destroy_local(jobj); 230 } 231 232 // method to destroy a local handle. 233 // This function allows caller to specify which 234 // threads local handle table to use although currently it is 235 // not used. 236 void destroy_jni_reference(JavaThread *thread, jobject jobj) { 237 destroy_jni_reference(jobj); 238 } 239 240 jvmtiEnv* jvmti_external() { return &_jvmti_external; }; 241 242 // Event Dispatch 243 244 bool has_callback(jvmtiEvent event_type) { 245 assert(event_type >= JVMTI_MIN_EVENT_TYPE_VAL && 246 event_type <= JVMTI_MAX_EVENT_TYPE_VAL, "checking"); 247 return ((void**)&_event_callbacks)[event_type-JVMTI_MIN_EVENT_TYPE_VAL] != NULL; 248 } 249 250 jvmtiEventCallbacks* callbacks() { 251 return &_event_callbacks; 252 } 253 254 jvmtiExtEventCallbacks* ext_callbacks() { 255 return &_ext_event_callbacks; 256 } 257 258 void set_tag_map(JvmtiTagMap* tag_map) { 259 _tag_map = tag_map; 260 } 261 262 JvmtiTagMap* tag_map() { 263 return _tag_map; 264 } 265 266 267 // return true if event is enabled globally or for any thread 268 // True only if there is a callback for it. 269 bool is_enabled(jvmtiEvent event_type) { 270 return _env_event_enable.is_enabled(event_type); 271 } 272 273 // Random Utilities 274 275 protected: 276 // helper methods for creating arrays of global JNI Handles from local Handles 277 // allocated into environment specific storage 278 jobject * new_jobjectArray(int length, Handle *handles); 279 jthread * new_jthreadArray(int length, Handle *handles); 280 jthreadGroup * new_jthreadGroupArray(int length, Handle *handles); 281 282 // convert from JNIHandle to JavaThread * 283 JavaThread * get_JavaThread(jthread jni_thread); 284 285 // convert to a jni jclass from a non-null Klass* 286 jclass get_jni_class_non_null(Klass* k); 287 288 jint count_locked_objects(JavaThread *java_thread, Handle hobj); 289 jvmtiError get_locked_objects_in_frame(JavaThread *calling_thread, 290 JavaThread* java_thread, 291 javaVFrame *jvf, 292 GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, 293 jint depth); 294 vframe* vframeFor(JavaThread* java_thread, jint depth); 295 296 public: 297 // get a field descriptor for the specified class and field 298 static bool get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd); 299 // test for suspend - most (all?) of these should go away 300 static bool is_thread_fully_suspended(JavaThread *thread, 301 bool wait_for_suspend, 302 uint32_t *bits); 303 304 305 // JVMTI API helper functions which are called at safepoint or thread is suspended. 306 jvmtiError get_frame_count(JvmtiThreadState *state, jint *count_ptr); 307 jvmtiError get_frame_location(JavaThread* java_thread, jint depth, 308 jmethodID* method_ptr, jlocation* location_ptr); 309 jvmtiError get_object_monitor_usage(JavaThread *calling_thread, 310 jobject object, jvmtiMonitorUsage* info_ptr); 311 jvmtiError get_stack_trace(JavaThread *java_thread, 312 jint stack_depth, jint max_count, 313 jvmtiFrameInfo* frame_buffer, jint* count_ptr); 314 jvmtiError get_current_contended_monitor(JavaThread *calling_thread, 315 JavaThread *java_thread, 316 jobject *monitor_ptr); 317 jvmtiError get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread, 318 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list); 319 jvmtiError check_top_frame(JavaThread* current_thread, JavaThread* java_thread, 320 jvalue value, TosState tos, Handle* ret_ob_h); 321 jvmtiError force_early_return(JavaThread* java_thread, jvalue value, TosState tos); 322 }; 323 324 // This class is the only safe means of iterating through environments. 325 // Note that this iteratation includes invalid environments pending 326 // deallocation -- in fact, some uses depend on this behavior. 327 328 class JvmtiEnvIterator : public StackObj { 329 private: 330 bool _entry_was_marked; 331 public: 332 JvmtiEnvIterator() { 333 if (Threads::number_of_threads() == 0) { 334 _entry_was_marked = false; // we are single-threaded, no need 335 } else { 336 Thread::current()->entering_jvmti_env_iteration(); 337 _entry_was_marked = true; 338 } 339 } 340 ~JvmtiEnvIterator() { 341 if (_entry_was_marked) { 342 Thread::current()->leaving_jvmti_env_iteration(); 343 } 344 } 345 JvmtiEnv* first() { return JvmtiEnvBase::head_environment(); } 346 JvmtiEnv* next(JvmtiEnvBase* env) { return env->next_environment(); } 347 }; 348 349 // VM operation to update for pop top frame. 350 class VM_UpdateForPopTopFrame : public VM_Operation { 351 private: 352 JvmtiThreadState* _state; 353 jvmtiError _result; 354 355 public: 356 VM_UpdateForPopTopFrame(JvmtiThreadState* state) { 357 _state = state; 358 _result = JVMTI_ERROR_NONE; 359 } 360 VMOp_Type type() const { return VMOp_UpdateForPopTopFrame; } 361 jvmtiError result() { return _result; } 362 void doit() { 363 JavaThread* jt = _state->get_thread(); 364 if (Threads::includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) { 365 _state->update_for_pop_top_frame(); 366 } else { 367 _result = JVMTI_ERROR_THREAD_NOT_ALIVE; 368 } 369 } 370 }; 371 372 // VM operation to set frame pop. 373 class VM_SetFramePop : public VM_Operation { 374 private: 375 JvmtiEnv *_env; 376 JvmtiThreadState* _state; 377 jint _depth; 378 jvmtiError _result; 379 380 public: 381 VM_SetFramePop(JvmtiEnv *env, JvmtiThreadState* state, jint depth) { 382 _env = env; 383 _state = state; 384 _depth = depth; 385 _result = JVMTI_ERROR_NONE; 386 } 387 // Nested operation must be allowed for the VM_EnterInterpOnlyMode that is 388 // called from the JvmtiEventControllerPrivate::recompute_thread_enabled. 389 bool allow_nested_vm_operations() const { return true; } 390 VMOp_Type type() const { return VMOp_SetFramePop; } 391 jvmtiError result() { return _result; } 392 void doit() { 393 JavaThread* jt = _state->get_thread(); 394 if (Threads::includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) { 395 int frame_number = _state->count_frames() - _depth; 396 _state->env_thread_state((JvmtiEnvBase*)_env)->set_frame_pop(frame_number); 397 } else { 398 _result = JVMTI_ERROR_THREAD_NOT_ALIVE; 399 } 400 } 401 }; 402 403 404 // VM operation to get monitor information with stack depth. 405 class VM_GetOwnedMonitorInfo : public VM_Operation { 406 private: 407 JvmtiEnv *_env; 408 JavaThread* _calling_thread; 409 JavaThread *_java_thread; 410 jvmtiError _result; 411 GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list; 412 413 public: 414 VM_GetOwnedMonitorInfo(JvmtiEnv* env, JavaThread* calling_thread, 415 JavaThread* java_thread, 416 GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitor_list) { 417 _env = env; 418 _calling_thread = calling_thread; 419 _java_thread = java_thread; 420 _owned_monitors_list = owned_monitor_list; 421 _result = JVMTI_ERROR_NONE; 422 } 423 VMOp_Type type() const { return VMOp_GetOwnedMonitorInfo; } 424 void doit() { 425 _result = JVMTI_ERROR_THREAD_NOT_ALIVE; 426 if (Threads::includes(_java_thread) && !_java_thread->is_exiting() 427 && _java_thread->threadObj() != NULL) { 428 _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, _java_thread, 429 _owned_monitors_list); 430 } 431 } 432 jvmtiError result() { return _result; } 433 }; 434 435 436 // VM operation to get object monitor usage. 437 class VM_GetObjectMonitorUsage : public VM_Operation { 438 private: 439 JvmtiEnv *_env; 440 jobject _object; 441 JavaThread* _calling_thread; 442 jvmtiMonitorUsage* _info_ptr; 443 jvmtiError _result; 444 445 public: 446 VM_GetObjectMonitorUsage(JvmtiEnv *env, JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) { 447 _env = env; 448 _object = object; 449 _calling_thread = calling_thread; 450 _info_ptr = info_ptr; 451 } 452 VMOp_Type type() const { return VMOp_GetObjectMonitorUsage; } 453 jvmtiError result() { return _result; } 454 void doit() { 455 _result = ((JvmtiEnvBase*) _env)->get_object_monitor_usage(_calling_thread, _object, _info_ptr); 456 } 457 458 }; 459 460 // VM operation to get current contended monitor. 461 class VM_GetCurrentContendedMonitor : public VM_Operation { 462 private: 463 JvmtiEnv *_env; 464 JavaThread *_calling_thread; 465 JavaThread *_java_thread; 466 jobject *_owned_monitor_ptr; 467 jvmtiError _result; 468 469 public: 470 VM_GetCurrentContendedMonitor(JvmtiEnv *env, JavaThread *calling_thread, JavaThread *java_thread, jobject *mon_ptr) { 471 _env = env; 472 _calling_thread = calling_thread; 473 _java_thread = java_thread; 474 _owned_monitor_ptr = mon_ptr; 475 } 476 VMOp_Type type() const { return VMOp_GetCurrentContendedMonitor; } 477 jvmtiError result() { return _result; } 478 void doit() { 479 _result = JVMTI_ERROR_THREAD_NOT_ALIVE; 480 if (Threads::includes(_java_thread) && !_java_thread->is_exiting() && 481 _java_thread->threadObj() != NULL) { 482 _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,_java_thread,_owned_monitor_ptr); 483 } 484 } 485 }; 486 487 // VM operation to get stack trace at safepoint. 488 class VM_GetStackTrace : public VM_Operation { 489 private: 490 JvmtiEnv *_env; 491 JavaThread *_java_thread; 492 jint _start_depth; 493 jint _max_count; 494 jvmtiFrameInfo *_frame_buffer; 495 jint *_count_ptr; 496 jvmtiError _result; 497 498 public: 499 VM_GetStackTrace(JvmtiEnv *env, JavaThread *java_thread, 500 jint start_depth, jint max_count, 501 jvmtiFrameInfo* frame_buffer, jint* count_ptr) { 502 _env = env; 503 _java_thread = java_thread; 504 _start_depth = start_depth; 505 _max_count = max_count; 506 _frame_buffer = frame_buffer; 507 _count_ptr = count_ptr; 508 } 509 jvmtiError result() { return _result; } 510 VMOp_Type type() const { return VMOp_GetStackTrace; } 511 void doit() { 512 _result = JVMTI_ERROR_THREAD_NOT_ALIVE; 513 if (Threads::includes(_java_thread) && !_java_thread->is_exiting() 514 && _java_thread->threadObj() != NULL) { 515 _result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread, 516 _start_depth, _max_count, 517 _frame_buffer, _count_ptr); 518 } 519 } 520 }; 521 522 // forward declaration 523 struct StackInfoNode; 524 525 // VM operation to get stack trace at safepoint. 526 class VM_GetMultipleStackTraces : public VM_Operation { 527 private: 528 JvmtiEnv *_env; 529 jint _max_frame_count; 530 jvmtiStackInfo *_stack_info; 531 jvmtiError _result; 532 int _frame_count_total; 533 struct StackInfoNode *_head; 534 535 JvmtiEnvBase *env() { return (JvmtiEnvBase *)_env; } 536 jint max_frame_count() { return _max_frame_count; } 537 struct StackInfoNode *head() { return _head; } 538 void set_head(StackInfoNode *head) { _head = head; } 539 540 protected: 541 void set_result(jvmtiError result) { _result = result; } 542 void fill_frames(jthread jt, JavaThread *thr, oop thread_oop); 543 void allocate_and_fill_stacks(jint thread_count); 544 545 public: 546 VM_GetMultipleStackTraces(JvmtiEnv *env, jint max_frame_count) { 547 _env = env; 548 _max_frame_count = max_frame_count; 549 _frame_count_total = 0; 550 _head = NULL; 551 _result = JVMTI_ERROR_NONE; 552 } 553 VMOp_Type type() const { return VMOp_GetMultipleStackTraces; } 554 jvmtiStackInfo *stack_info() { return _stack_info; } 555 jvmtiError result() { return _result; } 556 }; 557 558 559 // VM operation to get stack trace at safepoint. 560 class VM_GetAllStackTraces : public VM_GetMultipleStackTraces { 561 private: 562 JavaThread *_calling_thread; 563 jint _final_thread_count; 564 565 public: 566 VM_GetAllStackTraces(JvmtiEnv *env, JavaThread *calling_thread, 567 jint max_frame_count) 568 : VM_GetMultipleStackTraces(env, max_frame_count) { 569 _calling_thread = calling_thread; 570 } 571 VMOp_Type type() const { return VMOp_GetAllStackTraces; } 572 void doit(); 573 jint final_thread_count() { return _final_thread_count; } 574 }; 575 576 // VM operation to get stack trace at safepoint. 577 class VM_GetThreadListStackTraces : public VM_GetMultipleStackTraces { 578 private: 579 jint _thread_count; 580 const jthread* _thread_list; 581 582 public: 583 VM_GetThreadListStackTraces(JvmtiEnv *env, jint thread_count, const jthread* thread_list, jint max_frame_count) 584 : VM_GetMultipleStackTraces(env, max_frame_count) { 585 _thread_count = thread_count; 586 _thread_list = thread_list; 587 } 588 VMOp_Type type() const { return VMOp_GetThreadListStackTraces; } 589 void doit(); 590 }; 591 592 593 // VM operation to count stack frames at safepoint. 594 class VM_GetFrameCount : public VM_Operation { 595 private: 596 JvmtiEnv *_env; 597 JvmtiThreadState *_state; 598 jint *_count_ptr; 599 jvmtiError _result; 600 601 public: 602 VM_GetFrameCount(JvmtiEnv *env, JvmtiThreadState *state, jint *count_ptr) { 603 _env = env; 604 _state = state; 605 _count_ptr = count_ptr; 606 } 607 VMOp_Type type() const { return VMOp_GetFrameCount; } 608 jvmtiError result() { return _result; } 609 void doit() { 610 _result = JVMTI_ERROR_THREAD_NOT_ALIVE; 611 JavaThread* jt = _state->get_thread(); 612 if (Threads::includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) { 613 _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr); 614 } 615 } 616 }; 617 618 // VM operation to frame location at safepoint. 619 class VM_GetFrameLocation : public VM_Operation { 620 private: 621 JvmtiEnv *_env; 622 JavaThread* _java_thread; 623 jint _depth; 624 jmethodID* _method_ptr; 625 jlocation* _location_ptr; 626 jvmtiError _result; 627 628 public: 629 VM_GetFrameLocation(JvmtiEnv *env, JavaThread* java_thread, jint depth, 630 jmethodID* method_ptr, jlocation* location_ptr) { 631 _env = env; 632 _java_thread = java_thread; 633 _depth = depth; 634 _method_ptr = method_ptr; 635 _location_ptr = location_ptr; 636 } 637 VMOp_Type type() const { return VMOp_GetFrameLocation; } 638 jvmtiError result() { return _result; } 639 void doit() { 640 _result = JVMTI_ERROR_THREAD_NOT_ALIVE; 641 if (Threads::includes(_java_thread) && !_java_thread->is_exiting() && 642 _java_thread->threadObj() != NULL) { 643 _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth, 644 _method_ptr, _location_ptr); 645 } 646 } 647 }; 648 649 650 // ResourceTracker 651 // 652 // ResourceTracker works a little like a ResourceMark. All allocates 653 // using the resource tracker are recorded. If an allocate using the 654 // resource tracker fails the destructor will free any resources 655 // that were allocated using the tracker. 656 // The motive for this class is to avoid messy error recovery code 657 // in situations where multiple allocations are done in sequence. If 658 // the second or subsequent allocation fails it avoids any code to 659 // release memory allocated in the previous calls. 660 // 661 // Usage :- 662 // ResourceTracker rt(env); 663 // : 664 // err = rt.allocate(1024, &ptr); 665 666 class ResourceTracker : public StackObj { 667 private: 668 JvmtiEnv* _env; 669 GrowableArray<unsigned char*> *_allocations; 670 bool _failed; 671 public: 672 ResourceTracker(JvmtiEnv* env); 673 ~ResourceTracker(); 674 jvmtiError allocate(jlong size, unsigned char** mem_ptr); 675 unsigned char* allocate(jlong size); 676 char* strdup(const char* str); 677 }; 678 679 680 // Jvmti monitor closure to collect off stack monitors. 681 class JvmtiMonitorClosure: public MonitorClosure { 682 private: 683 JavaThread *_java_thread; 684 JavaThread *_calling_thread; 685 GrowableArray<jvmtiMonitorStackDepthInfo*> *_owned_monitors_list; 686 jvmtiError _error; 687 JvmtiEnvBase *_env; 688 689 public: 690 JvmtiMonitorClosure(JavaThread* thread, JavaThread *calling_thread, 691 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors, 692 JvmtiEnvBase *env) { 693 _java_thread = thread; 694 _calling_thread = calling_thread; 695 _owned_monitors_list = owned_monitors; 696 _error = JVMTI_ERROR_NONE; 697 _env = env; 698 } 699 void do_monitor(ObjectMonitor* mon); 700 jvmtiError error() { return _error;} 701 }; 702 703 704 // Jvmti module closure to collect all modules loaded to the system. 705 class JvmtiModuleClosure : public StackObj { 706 private: 707 static GrowableArray<jobject> *_tbl; // Protected with Module_lock 708 709 static void do_module(ModuleEntry* entry) { 710 assert_locked_or_safepoint(Module_lock); 711 jobject module = entry->module_handle(); 712 guarantee(module != NULL, "module object is NULL"); 713 _tbl->push(module); 714 } 715 716 public: 717 jvmtiError get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr); 718 }; 719 720 #endif // SHARE_VM_PRIMS_JVMTIENVBASE_HPP