< prev index next >

src/share/vm/prims/jvmtiEnv.cpp

Print this page




 383   jvmtiClassDefinition* class_definitions =
 384                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
 385   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
 386 
 387   for (index = 0; index < class_count; index++) {
 388     HandleMark hm(current_thread);
 389 
 390     jclass jcls = classes[index];
 391     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
 392     if (k_mirror == NULL) {
 393       return JVMTI_ERROR_INVALID_CLASS;
 394     }
 395     if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
 396       return JVMTI_ERROR_INVALID_CLASS;
 397     }
 398 
 399     if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
 400       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
 401     }
 402 
 403     Klass* k_oop = java_lang_Class::as_Klass(k_mirror);
 404     KlassHandle klass(current_thread, k_oop);
 405 
 406     jint status = klass->jvmti_class_status();
 407     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
 408       return JVMTI_ERROR_INVALID_CLASS;
 409     }
 410 
 411     instanceKlassHandle ikh(current_thread, k_oop);
 412     if (ikh->get_cached_class_file_bytes() == NULL) {
 413       // Not cached, we need to reconstitute the class file from the
 414       // VM representation. We don't attach the reconstituted class
 415       // bytes to the InstanceKlass here because they have not been
 416       // validated and we're not at a safepoint.
 417       JvmtiClassFileReconstituter reconstituter(ikh);
 418       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
 419         return reconstituter.get_error();
 420       }
 421 
 422       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
 423       class_definitions[index].class_bytes      = (unsigned char*)
 424                                                        reconstituter.class_file_bytes();
 425     } else {
 426       // it is cached, get it from the cache
 427       class_definitions[index].class_byte_count = ikh->get_cached_class_file_len();
 428       class_definitions[index].class_bytes      = ikh->get_cached_class_file_bytes();
 429     }
 430     class_definitions[index].klass              = jcls;
 431   }
 432   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
 433   VMThread::execute(&op);
 434   return (op.check_error());
 435 } /* end RetransformClasses */
 436 
 437 
 438 // class_count - pre-checked to be greater than or equal to 0
 439 // class_definitions - pre-checked for NULL
 440 jvmtiError
 441 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
 442 //TODO: add locking
 443   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
 444   VMThread::execute(&op);
 445   return (op.check_error());
 446 } /* end RedefineClasses */
 447 
 448 


 650     // check that the segment is indeed a zip file).
 651     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
 652     if (zip_entry == NULL) {
 653       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 654     }
 655     delete zip_entry;   // no longer needed
 656 
 657     // lock the loader
 658     Thread* THREAD = Thread::current();
 659     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 660 
 661     ObjectLocker ol(loader, THREAD);
 662 
 663     // need the path as java.lang.String
 664     Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
 665     if (HAS_PENDING_EXCEPTION) {
 666       CLEAR_PENDING_EXCEPTION;
 667       return JVMTI_ERROR_INTERNAL;
 668     }
 669 
 670     instanceKlassHandle loader_ik(THREAD, loader->klass());
 671 
 672     // Invoke the appendToClassPathForInstrumentation method - if the method
 673     // is not found it means the loader doesn't support adding to the class path
 674     // in the live phase.
 675     {
 676       JavaValue res(T_VOID);
 677       JavaCalls::call_special(&res,
 678                               loader,
 679                               loader_ik,
 680                               vmSymbols::appendToClassPathForInstrumentation_name(),
 681                               vmSymbols::appendToClassPathForInstrumentation_signature(),
 682                               path,
 683                               THREAD);
 684       if (HAS_PENDING_EXCEPTION) {
 685         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 686         CLEAR_PENDING_EXCEPTION;
 687 
 688         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
 689           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
 690         } else {
 691           return JVMTI_ERROR_INTERNAL;
 692         }
 693       }
 694     }
 695 
 696     return JVMTI_ERROR_NONE;
 697   } else {
 698     return JVMTI_ERROR_WRONG_PHASE;
 699   }


1764 // java_thread - pre-checked
1765 jvmtiError
1766 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
1767   jvalue val;
1768   val.j = 0L;
1769   return force_early_return(java_thread, val, vtos);
1770 } /* end ForceEarlyReturnVoid */
1771 
1772 
1773   //
1774   // Heap functions
1775   //
1776 
1777 // klass - NULL is a valid value, must be checked
1778 // initial_object - NULL is a valid value, must be checked
1779 // callbacks - pre-checked for NULL
1780 // user_data - NULL is a valid value, must be checked
1781 jvmtiError
1782 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1783   // check klass if provided
1784   Klass* k_oop = NULL;
1785   if (klass != NULL) {
1786     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1787     if (k_mirror == NULL) {
1788       return JVMTI_ERROR_INVALID_CLASS;
1789     }
1790     if (java_lang_Class::is_primitive(k_mirror)) {
1791       return JVMTI_ERROR_NONE;
1792     }
1793     k_oop = java_lang_Class::as_Klass(k_mirror);
1794     if (k_oop == NULL) {
1795       return JVMTI_ERROR_INVALID_CLASS;
1796     }
1797   }
1798 
1799   Thread *thread = Thread::current();
1800   HandleMark hm(thread);
1801   KlassHandle kh (thread, k_oop);
1802 
1803   TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1804   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, kh, initial_object, callbacks, user_data);
1805   return JVMTI_ERROR_NONE;
1806 } /* end FollowReferences */
1807 
1808 
1809 // klass - NULL is a valid value, must be checked
1810 // callbacks - pre-checked for NULL
1811 // user_data - NULL is a valid value, must be checked
1812 jvmtiError
1813 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1814   // check klass if provided
1815   Klass* k_oop = NULL;
1816   if (klass != NULL) {
1817     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1818     if (k_mirror == NULL) {
1819       return JVMTI_ERROR_INVALID_CLASS;
1820     }
1821     if (java_lang_Class::is_primitive(k_mirror)) {
1822       return JVMTI_ERROR_NONE;
1823     }
1824     k_oop = java_lang_Class::as_Klass(k_mirror);
1825     if (k_oop == NULL) {
1826       return JVMTI_ERROR_INVALID_CLASS;
1827     }
1828   }
1829 
1830   Thread *thread = Thread::current();
1831   HandleMark hm(thread);
1832   KlassHandle kh (thread, k_oop);
1833 
1834   TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1835   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, kh, callbacks, user_data);
1836   return JVMTI_ERROR_NONE;
1837 } /* end IterateThroughHeap */
1838 
1839 
1840 // tag_ptr - pre-checked for NULL
1841 jvmtiError
1842 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1843   oop o = JNIHandles::resolve_external_guard(object);
1844   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1845   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1846   return JVMTI_ERROR_NONE;
1847 } /* end GetTag */
1848 
1849 
1850 jvmtiError
1851 JvmtiEnv::SetTag(jobject object, jlong tag) {
1852   oop o = JNIHandles::resolve_external_guard(object);
1853   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1854   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1855   return JVMTI_ERROR_NONE;


1892 
1893 // heap_root_callback - NULL is a valid value, must be checked
1894 // stack_ref_callback - NULL is a valid value, must be checked
1895 // object_ref_callback - NULL is a valid value, must be checked
1896 // user_data - NULL is a valid value, must be checked
1897 jvmtiError
1898 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1899   TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1900   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1901   return JVMTI_ERROR_NONE;
1902 } /* end IterateOverReachableObjects */
1903 
1904 
1905 // heap_object_callback - pre-checked for NULL
1906 // user_data - NULL is a valid value, must be checked
1907 jvmtiError
1908 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1909   TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1910   Thread *thread = Thread::current();
1911   HandleMark hm(thread);
1912   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, KlassHandle(), heap_object_callback, user_data);
1913   return JVMTI_ERROR_NONE;
1914 } /* end IterateOverHeap */
1915 
1916 
1917 // k_mirror - may be primitive, this must be checked
1918 // heap_object_callback - pre-checked for NULL
1919 // user_data - NULL is a valid value, must be checked
1920 jvmtiError
1921 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1922   if (java_lang_Class::is_primitive(k_mirror)) {
1923     // DO PRIMITIVE CLASS PROCESSING
1924     return JVMTI_ERROR_NONE;
1925   }
1926   Klass* k_oop = java_lang_Class::as_Klass(k_mirror);
1927   if (k_oop == NULL) {
1928     return JVMTI_ERROR_INVALID_CLASS;
1929   }
1930   Thread *thread = Thread::current();
1931   HandleMark hm(thread);
1932   KlassHandle klass (thread, k_oop);
1933   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
1934   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
1935   return JVMTI_ERROR_NONE;
1936 } /* end IterateOverInstancesOfClass */
1937 
1938 
1939   //
1940   // Local Variable functions
1941   //
1942 
1943 // Threads_lock NOT held, java_thread not protected by lock
1944 // java_thread - pre-checked
1945 // java_thread - unchecked
1946 // depth - pre-checked as non-negative
1947 // value_ptr - pre-checked for NULL
1948 jvmtiError
1949 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
1950   JavaThread* current_thread = JavaThread::current();
1951   // rm object is created to clean up the javaVFrame created in
1952   // doit_prologue(), but after doit() is finished with it.


2391   HandleMark hm(current_thread);
2392 
2393   if (java_lang_Class::is_primitive(k_mirror)) {
2394     *method_count_ptr = 0;
2395     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2396     return JVMTI_ERROR_NONE;
2397   }
2398   Klass* k = java_lang_Class::as_Klass(k_mirror);
2399   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2400 
2401   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2402   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2403     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2404   }
2405 
2406   if (!k->is_instance_klass()) {
2407     *method_count_ptr = 0;
2408     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2409     return JVMTI_ERROR_NONE;
2410   }
2411   instanceKlassHandle instanceK_h(current_thread, k);
2412   // Allocate the result and fill it in
2413   int result_length = instanceK_h->methods()->length();
2414   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2415   int index;
2416   bool jmethodids_found = true;
2417 
2418   if (JvmtiExport::can_maintain_original_method_order()) {
2419     // Use the original method ordering indices stored in the class, so we can emit
2420     // jmethodIDs in the order they appeared in the class file
2421     for (index = 0; index < result_length; index++) {
2422       Method* m = instanceK_h->methods()->at(index);
2423       int original_index = instanceK_h->method_ordering()->at(index);
2424       assert(original_index >= 0 && original_index < result_length, "invalid original method index");
2425       jmethodID id;
2426       if (jmethodids_found) {
2427         id = m->find_jmethod_id_or_null();
2428         if (id == NULL) {
2429           // If we find an uninitialized value, make sure there is
2430           // enough space for all the uninitialized values we might
2431           // find.
2432           instanceK_h->ensure_space_for_methodids(index);
2433           jmethodids_found = false;
2434           id = m->jmethod_id();
2435         }
2436       } else {
2437         id = m->jmethod_id();
2438       }
2439       result_list[original_index] = id;
2440     }
2441   } else {
2442     // otherwise just copy in any order
2443     for (index = 0; index < result_length; index++) {
2444       Method* m = instanceK_h->methods()->at(index);
2445       jmethodID id;
2446       if (jmethodids_found) {
2447         id = m->find_jmethod_id_or_null();
2448         if (id == NULL) {
2449           // If we find an uninitialized value, make sure there is
2450           // enough space for all the uninitialized values we might
2451           // find.
2452           instanceK_h->ensure_space_for_methodids(index);
2453           jmethodids_found = false;
2454           id = m->jmethod_id();
2455         }
2456       } else {
2457         id = m->jmethod_id();
2458       }
2459       result_list[index] = id;
2460     }
2461   }
2462   // Fill in return value.
2463   *method_count_ptr = result_length;
2464   *methods_ptr = result_list;
2465 
2466   return JVMTI_ERROR_NONE;
2467 } /* end GetClassMethods */
2468 
2469 
2470 // k_mirror - may be primitive, this must be checked
2471 // field_count_ptr - pre-checked for NULL
2472 // fields_ptr - pre-checked for NULL


2477     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2478     return JVMTI_ERROR_NONE;
2479   }
2480   JavaThread* current_thread = JavaThread::current();
2481   HandleMark hm(current_thread);
2482   Klass* k = java_lang_Class::as_Klass(k_mirror);
2483   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2484 
2485   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2486   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2487     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2488   }
2489 
2490   if (!k->is_instance_klass()) {
2491     *field_count_ptr = 0;
2492     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2493     return JVMTI_ERROR_NONE;
2494   }
2495 
2496 
2497   instanceKlassHandle instanceK_h(current_thread, k);
2498 
2499   int result_count = 0;
2500   // First, count the fields.
2501   FilteredFieldStream flds(instanceK_h, true, true);
2502   result_count = flds.field_count();
2503 
2504   // Allocate the result and fill it in
2505   jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2506   // The JVMTI spec requires fields in the order they occur in the class file,
2507   // this is the reverse order of what FieldStream hands out.
2508   int id_index = (result_count - 1);
2509 
2510   for (FilteredFieldStream src_st(instanceK_h, true, true); !src_st.eos(); src_st.next()) {
2511     result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2512                                             instanceK_h, src_st.offset(),
2513                                             src_st.access_flags().is_static());
2514   }
2515   assert(id_index == -1, "just checking");
2516   // Fill in the results
2517   *field_count_ptr = result_count;
2518   *fields_ptr = result_list;
2519 
2520   return JVMTI_ERROR_NONE;
2521 } /* end GetClassFields */
2522 
2523 
2524 // k_mirror - may be primitive, this must be checked
2525 // interface_count_ptr - pre-checked for NULL
2526 // interfaces_ptr - pre-checked for NULL
2527 jvmtiError
2528 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2529   {
2530     if (java_lang_Class::is_primitive(k_mirror)) {
2531       *interface_count_ptr = 0;
2532       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));


2557       oop mirror_at = klass_at->java_mirror();
2558       Handle handle_at = Handle(current_thread, mirror_at);
2559       result_list[i_index] = (jclass) jni_reference(handle_at);
2560     }
2561     *interface_count_ptr = result_length;
2562     *interfaces_ptr = result_list;
2563   }
2564 
2565   return JVMTI_ERROR_NONE;
2566 } /* end GetImplementedInterfaces */
2567 
2568 
2569 // k_mirror - may be primitive, this must be checked
2570 // minor_version_ptr - pre-checked for NULL
2571 // major_version_ptr - pre-checked for NULL
2572 jvmtiError
2573 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2574   if (java_lang_Class::is_primitive(k_mirror)) {
2575     return JVMTI_ERROR_ABSENT_INFORMATION;
2576   }
2577   Klass* k_oop = java_lang_Class::as_Klass(k_mirror);
2578   Thread *thread = Thread::current();
2579   HandleMark hm(thread);
2580   KlassHandle klass(thread, k_oop);
2581 
2582   jint status = klass->jvmti_class_status();
2583   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2584     return JVMTI_ERROR_INVALID_CLASS;
2585   }
2586   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2587     return JVMTI_ERROR_ABSENT_INFORMATION;
2588   }
2589 
2590   instanceKlassHandle ik(thread, k_oop);
2591   *minor_version_ptr = ik->minor_version();
2592   *major_version_ptr = ik->major_version();
2593 
2594   return JVMTI_ERROR_NONE;
2595 } /* end GetClassVersionNumbers */
2596 
2597 
2598 // k_mirror - may be primitive, this must be checked
2599 // constant_pool_count_ptr - pre-checked for NULL
2600 // constant_pool_byte_count_ptr - pre-checked for NULL
2601 // constant_pool_bytes_ptr - pre-checked for NULL
2602 jvmtiError
2603 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2604   if (java_lang_Class::is_primitive(k_mirror)) {
2605     return JVMTI_ERROR_ABSENT_INFORMATION;
2606   }
2607 
2608   Klass* k_oop = java_lang_Class::as_Klass(k_mirror);
2609   Thread *thread = Thread::current();
2610   HandleMark hm(thread);
2611   ResourceMark rm(thread);
2612   KlassHandle klass(thread, k_oop);
2613 
2614   jint status = klass->jvmti_class_status();
2615   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2616     return JVMTI_ERROR_INVALID_CLASS;
2617   }
2618   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2619     return JVMTI_ERROR_ABSENT_INFORMATION;
2620   }
2621 
2622   instanceKlassHandle ikh(thread, k_oop);
2623   JvmtiConstantPoolReconstituter reconstituter(ikh);
2624   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2625     return reconstituter.get_error();
2626   }
2627 
2628   unsigned char *cpool_bytes;
2629   int cpool_size = reconstituter.cpool_size();
2630   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2631     return reconstituter.get_error();
2632   }
2633   jvmtiError res = allocate(cpool_size, &cpool_bytes);
2634   if (res != JVMTI_ERROR_NONE) {
2635     return res;
2636   }
2637   reconstituter.copy_cpool_bytes(cpool_bytes);
2638   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2639     return reconstituter.get_error();
2640   }
2641 
2642   constantPoolHandle  constants(thread, ikh->constants());
2643   *constant_pool_count_ptr      = constants->length();
2644   *constant_pool_byte_count_ptr = cpool_size;
2645   *constant_pool_bytes_ptr      = cpool_bytes;
2646 
2647   return JVMTI_ERROR_NONE;
2648 } /* end GetConstantPool */
2649 
2650 
2651 // k_mirror - may be primitive, this must be checked
2652 // is_interface_ptr - pre-checked for NULL
2653 jvmtiError
2654 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2655   {
2656     bool result = false;
2657     if (!java_lang_Class::is_primitive(k_mirror)) {
2658       Klass* k = java_lang_Class::as_Klass(k_mirror);
2659       if (k != NULL && k->is_interface()) {
2660         result = true;
2661       }
2662     }




 383   jvmtiClassDefinition* class_definitions =
 384                             NEW_RESOURCE_ARRAY(jvmtiClassDefinition, class_count);
 385   NULL_CHECK(class_definitions, JVMTI_ERROR_OUT_OF_MEMORY);
 386 
 387   for (index = 0; index < class_count; index++) {
 388     HandleMark hm(current_thread);
 389 
 390     jclass jcls = classes[index];
 391     oop k_mirror = JNIHandles::resolve_external_guard(jcls);
 392     if (k_mirror == NULL) {
 393       return JVMTI_ERROR_INVALID_CLASS;
 394     }
 395     if (!k_mirror->is_a(SystemDictionary::Class_klass())) {
 396       return JVMTI_ERROR_INVALID_CLASS;
 397     }
 398 
 399     if (!VM_RedefineClasses::is_modifiable_class(k_mirror)) {
 400       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
 401     }
 402 
 403     Klass* klass = java_lang_Class::as_Klass(k_mirror);

 404 
 405     jint status = klass->jvmti_class_status();
 406     if (status & (JVMTI_CLASS_STATUS_ERROR)) {
 407       return JVMTI_ERROR_INVALID_CLASS;
 408     }
 409 
 410     InstanceKlass* ik = InstanceKlass::cast(klass);
 411     if (ik->get_cached_class_file_bytes() == NULL) {
 412       // Not cached, we need to reconstitute the class file from the
 413       // VM representation. We don't attach the reconstituted class
 414       // bytes to the InstanceKlass here because they have not been
 415       // validated and we're not at a safepoint.
 416       JvmtiClassFileReconstituter reconstituter(ik);
 417       if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
 418         return reconstituter.get_error();
 419       }
 420 
 421       class_definitions[index].class_byte_count = (jint)reconstituter.class_file_size();
 422       class_definitions[index].class_bytes      = (unsigned char*)
 423                                                        reconstituter.class_file_bytes();
 424     } else {
 425       // it is cached, get it from the cache
 426       class_definitions[index].class_byte_count = ik->get_cached_class_file_len();
 427       class_definitions[index].class_bytes      = ik->get_cached_class_file_bytes();
 428     }
 429     class_definitions[index].klass              = jcls;
 430   }
 431   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_retransform);
 432   VMThread::execute(&op);
 433   return (op.check_error());
 434 } /* end RetransformClasses */
 435 
 436 
 437 // class_count - pre-checked to be greater than or equal to 0
 438 // class_definitions - pre-checked for NULL
 439 jvmtiError
 440 JvmtiEnv::RedefineClasses(jint class_count, const jvmtiClassDefinition* class_definitions) {
 441 //TODO: add locking
 442   VM_RedefineClasses op(class_count, class_definitions, jvmti_class_load_kind_redefine);
 443   VMThread::execute(&op);
 444   return (op.check_error());
 445 } /* end RedefineClasses */
 446 
 447 


 649     // check that the segment is indeed a zip file).
 650     ClassPathZipEntry* zip_entry = ClassLoader::create_class_path_zip_entry(segment, false);
 651     if (zip_entry == NULL) {
 652       return JVMTI_ERROR_ILLEGAL_ARGUMENT;
 653     }
 654     delete zip_entry;   // no longer needed
 655 
 656     // lock the loader
 657     Thread* THREAD = Thread::current();
 658     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
 659 
 660     ObjectLocker ol(loader, THREAD);
 661 
 662     // need the path as java.lang.String
 663     Handle path = java_lang_String::create_from_platform_dependent_str(segment, THREAD);
 664     if (HAS_PENDING_EXCEPTION) {
 665       CLEAR_PENDING_EXCEPTION;
 666       return JVMTI_ERROR_INTERNAL;
 667     }
 668 


 669     // Invoke the appendToClassPathForInstrumentation method - if the method
 670     // is not found it means the loader doesn't support adding to the class path
 671     // in the live phase.
 672     {
 673       JavaValue res(T_VOID);
 674       JavaCalls::call_special(&res,
 675                               loader,
 676                               loader->klass(),
 677                               vmSymbols::appendToClassPathForInstrumentation_name(),
 678                               vmSymbols::appendToClassPathForInstrumentation_signature(),
 679                               path,
 680                               THREAD);
 681       if (HAS_PENDING_EXCEPTION) {
 682         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
 683         CLEAR_PENDING_EXCEPTION;
 684 
 685         if (ex_name == vmSymbols::java_lang_NoSuchMethodError()) {
 686           return JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED;
 687         } else {
 688           return JVMTI_ERROR_INTERNAL;
 689         }
 690       }
 691     }
 692 
 693     return JVMTI_ERROR_NONE;
 694   } else {
 695     return JVMTI_ERROR_WRONG_PHASE;
 696   }


1761 // java_thread - pre-checked
1762 jvmtiError
1763 JvmtiEnv::ForceEarlyReturnVoid(JavaThread* java_thread) {
1764   jvalue val;
1765   val.j = 0L;
1766   return force_early_return(java_thread, val, vtos);
1767 } /* end ForceEarlyReturnVoid */
1768 
1769 
1770   //
1771   // Heap functions
1772   //
1773 
1774 // klass - NULL is a valid value, must be checked
1775 // initial_object - NULL is a valid value, must be checked
1776 // callbacks - pre-checked for NULL
1777 // user_data - NULL is a valid value, must be checked
1778 jvmtiError
1779 JvmtiEnv::FollowReferences(jint heap_filter, jclass klass, jobject initial_object, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1780   // check klass if provided
1781   Klass* k = NULL;
1782   if (klass != NULL) {
1783     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1784     if (k_mirror == NULL) {
1785       return JVMTI_ERROR_INVALID_CLASS;
1786     }
1787     if (java_lang_Class::is_primitive(k_mirror)) {
1788       return JVMTI_ERROR_NONE;
1789     }
1790     k = java_lang_Class::as_Klass(k_mirror);
1791     if (klass == NULL) {
1792       return JVMTI_ERROR_INVALID_CLASS;
1793     }
1794   }
1795 
1796   Thread *thread = Thread::current();
1797   HandleMark hm(thread);

1798 
1799   TraceTime t("FollowReferences", TRACETIME_LOG(Debug, jvmti, objecttagging));
1800   JvmtiTagMap::tag_map_for(this)->follow_references(heap_filter, k, initial_object, callbacks, user_data);
1801   return JVMTI_ERROR_NONE;
1802 } /* end FollowReferences */
1803 
1804 
1805 // klass - NULL is a valid value, must be checked
1806 // callbacks - pre-checked for NULL
1807 // user_data - NULL is a valid value, must be checked
1808 jvmtiError
1809 JvmtiEnv::IterateThroughHeap(jint heap_filter, jclass klass, const jvmtiHeapCallbacks* callbacks, const void* user_data) {
1810   // check klass if provided
1811   Klass* k = NULL;
1812   if (klass != NULL) {
1813     oop k_mirror = JNIHandles::resolve_external_guard(klass);
1814     if (k_mirror == NULL) {
1815       return JVMTI_ERROR_INVALID_CLASS;
1816     }
1817     if (java_lang_Class::is_primitive(k_mirror)) {
1818       return JVMTI_ERROR_NONE;
1819     }
1820     k = java_lang_Class::as_Klass(k_mirror);
1821     if (k == NULL) {
1822       return JVMTI_ERROR_INVALID_CLASS;
1823     }
1824   }
1825 




1826   TraceTime t("IterateThroughHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1827   JvmtiTagMap::tag_map_for(this)->iterate_through_heap(heap_filter, k, callbacks, user_data);
1828   return JVMTI_ERROR_NONE;
1829 } /* end IterateThroughHeap */
1830 
1831 
1832 // tag_ptr - pre-checked for NULL
1833 jvmtiError
1834 JvmtiEnv::GetTag(jobject object, jlong* tag_ptr) {
1835   oop o = JNIHandles::resolve_external_guard(object);
1836   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1837   *tag_ptr = JvmtiTagMap::tag_map_for(this)->get_tag(object);
1838   return JVMTI_ERROR_NONE;
1839 } /* end GetTag */
1840 
1841 
1842 jvmtiError
1843 JvmtiEnv::SetTag(jobject object, jlong tag) {
1844   oop o = JNIHandles::resolve_external_guard(object);
1845   NULL_CHECK(o, JVMTI_ERROR_INVALID_OBJECT);
1846   JvmtiTagMap::tag_map_for(this)->set_tag(object, tag);
1847   return JVMTI_ERROR_NONE;


1884 
1885 // heap_root_callback - NULL is a valid value, must be checked
1886 // stack_ref_callback - NULL is a valid value, must be checked
1887 // object_ref_callback - NULL is a valid value, must be checked
1888 // user_data - NULL is a valid value, must be checked
1889 jvmtiError
1890 JvmtiEnv::IterateOverReachableObjects(jvmtiHeapRootCallback heap_root_callback, jvmtiStackReferenceCallback stack_ref_callback, jvmtiObjectReferenceCallback object_ref_callback, const void* user_data) {
1891   TraceTime t("IterateOverReachableObjects", TRACETIME_LOG(Debug, jvmti, objecttagging));
1892   JvmtiTagMap::tag_map_for(this)->iterate_over_reachable_objects(heap_root_callback, stack_ref_callback, object_ref_callback, user_data);
1893   return JVMTI_ERROR_NONE;
1894 } /* end IterateOverReachableObjects */
1895 
1896 
1897 // heap_object_callback - pre-checked for NULL
1898 // user_data - NULL is a valid value, must be checked
1899 jvmtiError
1900 JvmtiEnv::IterateOverHeap(jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1901   TraceTime t("IterateOverHeap", TRACETIME_LOG(Debug, jvmti, objecttagging));
1902   Thread *thread = Thread::current();
1903   HandleMark hm(thread);
1904   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, NULL, heap_object_callback, user_data);
1905   return JVMTI_ERROR_NONE;
1906 } /* end IterateOverHeap */
1907 
1908 
1909 // k_mirror - may be primitive, this must be checked
1910 // heap_object_callback - pre-checked for NULL
1911 // user_data - NULL is a valid value, must be checked
1912 jvmtiError
1913 JvmtiEnv::IterateOverInstancesOfClass(oop k_mirror, jvmtiHeapObjectFilter object_filter, jvmtiHeapObjectCallback heap_object_callback, const void* user_data) {
1914   if (java_lang_Class::is_primitive(k_mirror)) {
1915     // DO PRIMITIVE CLASS PROCESSING
1916     return JVMTI_ERROR_NONE;
1917   }
1918   Klass* klass = java_lang_Class::as_Klass(k_mirror);
1919   if (klass == NULL) {
1920     return JVMTI_ERROR_INVALID_CLASS;
1921   }



1922   TraceTime t("IterateOverInstancesOfClass", TRACETIME_LOG(Debug, jvmti, objecttagging));
1923   JvmtiTagMap::tag_map_for(this)->iterate_over_heap(object_filter, klass, heap_object_callback, user_data);
1924   return JVMTI_ERROR_NONE;
1925 } /* end IterateOverInstancesOfClass */
1926 
1927 
1928   //
1929   // Local Variable functions
1930   //
1931 
1932 // Threads_lock NOT held, java_thread not protected by lock
1933 // java_thread - pre-checked
1934 // java_thread - unchecked
1935 // depth - pre-checked as non-negative
1936 // value_ptr - pre-checked for NULL
1937 jvmtiError
1938 JvmtiEnv::GetLocalObject(JavaThread* java_thread, jint depth, jint slot, jobject* value_ptr) {
1939   JavaThread* current_thread = JavaThread::current();
1940   // rm object is created to clean up the javaVFrame created in
1941   // doit_prologue(), but after doit() is finished with it.


2380   HandleMark hm(current_thread);
2381 
2382   if (java_lang_Class::is_primitive(k_mirror)) {
2383     *method_count_ptr = 0;
2384     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2385     return JVMTI_ERROR_NONE;
2386   }
2387   Klass* k = java_lang_Class::as_Klass(k_mirror);
2388   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2389 
2390   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2391   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2392     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2393   }
2394 
2395   if (!k->is_instance_klass()) {
2396     *method_count_ptr = 0;
2397     *methods_ptr = (jmethodID*) jvmtiMalloc(0 * sizeof(jmethodID));
2398     return JVMTI_ERROR_NONE;
2399   }
2400   InstanceKlass* ik = InstanceKlass::cast(k);
2401   // Allocate the result and fill it in
2402   int result_length = ik->methods()->length();
2403   jmethodID* result_list = (jmethodID*)jvmtiMalloc(result_length * sizeof(jmethodID));
2404   int index;
2405   bool jmethodids_found = true;
2406 
2407   if (JvmtiExport::can_maintain_original_method_order()) {
2408     // Use the original method ordering indices stored in the class, so we can emit
2409     // jmethodIDs in the order they appeared in the class file
2410     for (index = 0; index < result_length; index++) {
2411       Method* m = ik->methods()->at(index);
2412       int original_index = ik->method_ordering()->at(index);
2413       assert(original_index >= 0 && original_index < result_length, "invalid original method index");
2414       jmethodID id;
2415       if (jmethodids_found) {
2416         id = m->find_jmethod_id_or_null();
2417         if (id == NULL) {
2418           // If we find an uninitialized value, make sure there is
2419           // enough space for all the uninitialized values we might
2420           // find.
2421           ik->ensure_space_for_methodids(index);
2422           jmethodids_found = false;
2423           id = m->jmethod_id();
2424         }
2425       } else {
2426         id = m->jmethod_id();
2427       }
2428       result_list[original_index] = id;
2429     }
2430   } else {
2431     // otherwise just copy in any order
2432     for (index = 0; index < result_length; index++) {
2433       Method* m = ik->methods()->at(index);
2434       jmethodID id;
2435       if (jmethodids_found) {
2436         id = m->find_jmethod_id_or_null();
2437         if (id == NULL) {
2438           // If we find an uninitialized value, make sure there is
2439           // enough space for all the uninitialized values we might
2440           // find.
2441           ik->ensure_space_for_methodids(index);
2442           jmethodids_found = false;
2443           id = m->jmethod_id();
2444         }
2445       } else {
2446         id = m->jmethod_id();
2447       }
2448       result_list[index] = id;
2449     }
2450   }
2451   // Fill in return value.
2452   *method_count_ptr = result_length;
2453   *methods_ptr = result_list;
2454 
2455   return JVMTI_ERROR_NONE;
2456 } /* end GetClassMethods */
2457 
2458 
2459 // k_mirror - may be primitive, this must be checked
2460 // field_count_ptr - pre-checked for NULL
2461 // fields_ptr - pre-checked for NULL


2466     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2467     return JVMTI_ERROR_NONE;
2468   }
2469   JavaThread* current_thread = JavaThread::current();
2470   HandleMark hm(current_thread);
2471   Klass* k = java_lang_Class::as_Klass(k_mirror);
2472   NULL_CHECK(k, JVMTI_ERROR_INVALID_CLASS);
2473 
2474   // Return CLASS_NOT_PREPARED error as per JVMTI spec.
2475   if (!(k->jvmti_class_status() & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY) )) {
2476     return JVMTI_ERROR_CLASS_NOT_PREPARED;
2477   }
2478 
2479   if (!k->is_instance_klass()) {
2480     *field_count_ptr = 0;
2481     *fields_ptr = (jfieldID*) jvmtiMalloc(0 * sizeof(jfieldID));
2482     return JVMTI_ERROR_NONE;
2483   }
2484 
2485 
2486   InstanceKlass* ik = InstanceKlass::cast(k);
2487 
2488   int result_count = 0;
2489   // First, count the fields.
2490   FilteredFieldStream flds(ik, true, true);
2491   result_count = flds.field_count();
2492 
2493   // Allocate the result and fill it in
2494   jfieldID* result_list = (jfieldID*) jvmtiMalloc(result_count * sizeof(jfieldID));
2495   // The JVMTI spec requires fields in the order they occur in the class file,
2496   // this is the reverse order of what FieldStream hands out.
2497   int id_index = (result_count - 1);
2498 
2499   for (FilteredFieldStream src_st(ik, true, true); !src_st.eos(); src_st.next()) {
2500     result_list[id_index--] = jfieldIDWorkaround::to_jfieldID(
2501                                             ik, src_st.offset(),
2502                                             src_st.access_flags().is_static());
2503   }
2504   assert(id_index == -1, "just checking");
2505   // Fill in the results
2506   *field_count_ptr = result_count;
2507   *fields_ptr = result_list;
2508 
2509   return JVMTI_ERROR_NONE;
2510 } /* end GetClassFields */
2511 
2512 
2513 // k_mirror - may be primitive, this must be checked
2514 // interface_count_ptr - pre-checked for NULL
2515 // interfaces_ptr - pre-checked for NULL
2516 jvmtiError
2517 JvmtiEnv::GetImplementedInterfaces(oop k_mirror, jint* interface_count_ptr, jclass** interfaces_ptr) {
2518   {
2519     if (java_lang_Class::is_primitive(k_mirror)) {
2520       *interface_count_ptr = 0;
2521       *interfaces_ptr = (jclass*) jvmtiMalloc(0 * sizeof(jclass));


2546       oop mirror_at = klass_at->java_mirror();
2547       Handle handle_at = Handle(current_thread, mirror_at);
2548       result_list[i_index] = (jclass) jni_reference(handle_at);
2549     }
2550     *interface_count_ptr = result_length;
2551     *interfaces_ptr = result_list;
2552   }
2553 
2554   return JVMTI_ERROR_NONE;
2555 } /* end GetImplementedInterfaces */
2556 
2557 
2558 // k_mirror - may be primitive, this must be checked
2559 // minor_version_ptr - pre-checked for NULL
2560 // major_version_ptr - pre-checked for NULL
2561 jvmtiError
2562 JvmtiEnv::GetClassVersionNumbers(oop k_mirror, jint* minor_version_ptr, jint* major_version_ptr) {
2563   if (java_lang_Class::is_primitive(k_mirror)) {
2564     return JVMTI_ERROR_ABSENT_INFORMATION;
2565   }
2566   Klass* klass = java_lang_Class::as_Klass(k_mirror);



2567 
2568   jint status = klass->jvmti_class_status();
2569   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2570     return JVMTI_ERROR_INVALID_CLASS;
2571   }
2572   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2573     return JVMTI_ERROR_ABSENT_INFORMATION;
2574   }
2575 
2576   InstanceKlass* ik = InstanceKlass::cast(klass);
2577   *minor_version_ptr = ik->minor_version();
2578   *major_version_ptr = ik->major_version();
2579 
2580   return JVMTI_ERROR_NONE;
2581 } /* end GetClassVersionNumbers */
2582 
2583 
2584 // k_mirror - may be primitive, this must be checked
2585 // constant_pool_count_ptr - pre-checked for NULL
2586 // constant_pool_byte_count_ptr - pre-checked for NULL
2587 // constant_pool_bytes_ptr - pre-checked for NULL
2588 jvmtiError
2589 JvmtiEnv::GetConstantPool(oop k_mirror, jint* constant_pool_count_ptr, jint* constant_pool_byte_count_ptr, unsigned char** constant_pool_bytes_ptr) {
2590   if (java_lang_Class::is_primitive(k_mirror)) {
2591     return JVMTI_ERROR_ABSENT_INFORMATION;
2592   }
2593 
2594   Klass* klass = java_lang_Class::as_Klass(k_mirror);
2595   Thread *thread = Thread::current();

2596   ResourceMark rm(thread);

2597 
2598   jint status = klass->jvmti_class_status();
2599   if (status & (JVMTI_CLASS_STATUS_ERROR)) {
2600     return JVMTI_ERROR_INVALID_CLASS;
2601   }
2602   if (status & (JVMTI_CLASS_STATUS_ARRAY)) {
2603     return JVMTI_ERROR_ABSENT_INFORMATION;
2604   }
2605 
2606   InstanceKlass* ik = InstanceKlass::cast(klass);
2607   JvmtiConstantPoolReconstituter reconstituter(ik);
2608   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2609     return reconstituter.get_error();
2610   }
2611 
2612   unsigned char *cpool_bytes;
2613   int cpool_size = reconstituter.cpool_size();
2614   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2615     return reconstituter.get_error();
2616   }
2617   jvmtiError res = allocate(cpool_size, &cpool_bytes);
2618   if (res != JVMTI_ERROR_NONE) {
2619     return res;
2620   }
2621   reconstituter.copy_cpool_bytes(cpool_bytes);
2622   if (reconstituter.get_error() != JVMTI_ERROR_NONE) {
2623     return reconstituter.get_error();
2624   }
2625 
2626   constantPoolHandle  constants(thread, ik->constants());
2627   *constant_pool_count_ptr      = constants->length();
2628   *constant_pool_byte_count_ptr = cpool_size;
2629   *constant_pool_bytes_ptr      = cpool_bytes;
2630 
2631   return JVMTI_ERROR_NONE;
2632 } /* end GetConstantPool */
2633 
2634 
2635 // k_mirror - may be primitive, this must be checked
2636 // is_interface_ptr - pre-checked for NULL
2637 jvmtiError
2638 JvmtiEnv::IsInterface(oop k_mirror, jboolean* is_interface_ptr) {
2639   {
2640     bool result = false;
2641     if (!java_lang_Class::is_primitive(k_mirror)) {
2642       Klass* k = java_lang_Class::as_Klass(k_mirror);
2643       if (k != NULL && k->is_interface()) {
2644         result = true;
2645       }
2646     }


< prev index next >