1 /*
   2  * Copyright (c) 2012, 2015, 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 #include "precompiled.hpp"
  26 
  27 #include <new>
  28 
  29 #include "classfile/classLoaderData.hpp"
  30 #include "classfile/stringTable.hpp"
  31 #include "code/codeCache.hpp"
  32 #include "jvmtifiles/jvmtiEnv.hpp"
  33 #include "memory/metadataFactory.hpp"
  34 #include "memory/universe.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "prims/wbtestmethods/parserTests.hpp"
  37 #include "prims/whitebox.hpp"
  38 #include "runtime/arguments.hpp"
  39 #include "runtime/compilationPolicy.hpp"
  40 #include "runtime/deoptimization.hpp"
  41 #include "runtime/interfaceSupport.hpp"
  42 #include "runtime/os.hpp"
  43 #include "runtime/sweeper.hpp"
  44 #include "runtime/javaCalls.hpp"
  45 #include "runtime/thread.hpp"
  46 #include "runtime/vm_version.hpp"
  47 #include "utilities/array.hpp"
  48 #include "utilities/debug.hpp"
  49 #include "utilities/exceptions.hpp"
  50 #include "utilities/macros.hpp"
  51 #if INCLUDE_ALL_GCS
  52 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.inline.hpp"
  53 #include "gc_implementation/g1/concurrentMark.hpp"
  54 #include "gc_implementation/g1/concurrentMarkThread.hpp"
  55 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  56 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  57 #endif // INCLUDE_ALL_GCS
  58 #if INCLUDE_NMT
  59 #include "services/mallocSiteTable.hpp"
  60 #include "services/memTracker.hpp"
  61 #include "utilities/nativeCallStack.hpp"
  62 #endif // INCLUDE_NMT
  63 
  64 
  65 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  66 
  67 #define SIZE_T_MAX_VALUE ((size_t) -1)
  68 
  69 bool WhiteBox::_used = false;
  70 volatile bool WhiteBox::compilation_locked = false;
  71 
  72 class VM_WhiteBoxOperation : public VM_Operation {
  73  public:
  74   VM_WhiteBoxOperation()                         { }
  75   VMOp_Type type()                  const        { return VMOp_WhiteBoxOperation; }
  76   bool allow_nested_vm_operations() const        { return true; }
  77 };
  78 
  79 
  80 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
  81   return (jlong)(void*)JNIHandles::resolve(obj);
  82 WB_END
  83 
  84 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o))
  85   return heapOopSize;
  86 WB_END
  87 
  88 WB_ENTRY(jint, WB_GetVMPageSize(JNIEnv* env, jobject o))
  89   return os::vm_page_size();
  90 WB_END
  91 
  92 class WBIsKlassAliveClosure : public KlassClosure {
  93     Symbol* _name;
  94     bool _found;
  95 public:
  96     WBIsKlassAliveClosure(Symbol* name) : _name(name), _found(false) {}
  97 
  98     void do_klass(Klass* k) {
  99       if (_found) return;
 100       Symbol* ksym = k->name();
 101       if (ksym->fast_compare(_name) == 0) {
 102         _found = true;
 103       }
 104     }
 105 
 106     bool found() const {
 107         return _found;
 108     }
 109 };
 110 
 111 WB_ENTRY(jboolean, WB_IsClassAlive(JNIEnv* env, jobject target, jstring name))
 112   Handle h_name = JNIHandles::resolve(name);
 113   if (h_name.is_null()) return false;
 114   Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_false);
 115   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
 116 
 117   WBIsKlassAliveClosure closure(sym);
 118   ClassLoaderDataGraph::classes_do(&closure);
 119 
 120   return closure.found();
 121 WB_END
 122 
 123 WB_ENTRY(void, WB_AddToBootstrapClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
 124 #if INCLUDE_JVMTI
 125   ResourceMark rm;
 126   const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
 127   JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
 128   jvmtiError err = jvmti_env->AddToBootstrapClassLoaderSearch(seg);
 129   assert(err == JVMTI_ERROR_NONE, "must not fail");
 130 #endif
 131 }
 132 WB_END
 133 
 134 WB_ENTRY(void, WB_AddToSystemClassLoaderSearch(JNIEnv* env, jobject o, jstring segment)) {
 135 #if INCLUDE_JVMTI
 136   ResourceMark rm;
 137   const char* seg = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(segment));
 138   JvmtiEnv* jvmti_env = JvmtiEnv::create_a_jvmti(JVMTI_VERSION);
 139   jvmtiError err = jvmti_env->AddToSystemClassLoaderSearch(seg);
 140   assert(err == JVMTI_ERROR_NONE, "must not fail");
 141 #endif
 142 }
 143 WB_END
 144 
 145 
 146 WB_ENTRY(jlong, WB_GetCompressedOopsMaxHeapSize(JNIEnv* env, jobject o)) {
 147   return (jlong)Arguments::max_heap_for_compressed_oops();
 148 }
 149 WB_END
 150 
 151 WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) {
 152   CollectorPolicy * p = Universe::heap()->collector_policy();
 153   gclog_or_tty->print_cr("Minimum heap "SIZE_FORMAT" Initial heap "
 154     SIZE_FORMAT" Maximum heap "SIZE_FORMAT" Space alignment "SIZE_FORMAT" Heap alignment "SIZE_FORMAT,
 155     p->min_heap_byte_size(), p->initial_heap_byte_size(), p->max_heap_byte_size(),
 156     p->space_alignment(), p->heap_alignment());
 157 }
 158 WB_END
 159 
 160 #ifndef PRODUCT
 161 // Forward declaration
 162 void TestReservedSpace_test();
 163 void TestReserveMemorySpecial_test();
 164 void TestVirtualSpace_test();
 165 void TestMetaspaceAux_test();
 166 #endif
 167 
 168 WB_ENTRY(void, WB_RunMemoryUnitTests(JNIEnv* env, jobject o))
 169 #ifndef PRODUCT
 170   TestReservedSpace_test();
 171   TestReserveMemorySpecial_test();
 172   TestVirtualSpace_test();
 173   TestMetaspaceAux_test();
 174 #endif
 175 WB_END
 176 
 177 WB_ENTRY(void, WB_ReadFromNoaccessArea(JNIEnv* env, jobject o))
 178   size_t granularity = os::vm_allocation_granularity();
 179   ReservedHeapSpace rhs(100 * granularity, granularity, false);
 180   VirtualSpace vs;
 181   vs.initialize(rhs, 50 * granularity);
 182 
 183   // Check if constraints are complied
 184   if (!( UseCompressedOops && rhs.base() != NULL &&
 185          Universe::narrow_oop_base() != NULL &&
 186          Universe::narrow_oop_use_implicit_null_checks() )) {
 187     tty->print_cr("WB_ReadFromNoaccessArea method is useless:\n "
 188                   "\tUseCompressedOops is %d\n"
 189                   "\trhs.base() is "PTR_FORMAT"\n"
 190                   "\tUniverse::narrow_oop_base() is "PTR_FORMAT"\n"
 191                   "\tUniverse::narrow_oop_use_implicit_null_checks() is %d",
 192                   UseCompressedOops,
 193                   rhs.base(),
 194                   Universe::narrow_oop_base(),
 195                   Universe::narrow_oop_use_implicit_null_checks());
 196     return;
 197   }
 198   tty->print_cr("Reading from no access area... ");
 199   tty->print_cr("*(vs.low_boundary() - rhs.noaccess_prefix() / 2 ) = %c",
 200                 *(vs.low_boundary() - rhs.noaccess_prefix() / 2 ));
 201 WB_END
 202 
 203 static jint wb_stress_virtual_space_resize(size_t reserved_space_size,
 204                                            size_t magnitude, size_t iterations) {
 205   size_t granularity = os::vm_allocation_granularity();
 206   ReservedHeapSpace rhs(reserved_space_size * granularity, granularity, false);
 207   VirtualSpace vs;
 208   if (!vs.initialize(rhs, 0)) {
 209     tty->print_cr("Failed to initialize VirtualSpace. Can't proceed.");
 210     return 3;
 211   }
 212 
 213   long seed = os::random();
 214   tty->print_cr("Random seed is %ld", seed);
 215   os::init_random(seed);
 216 
 217   for (size_t i = 0; i < iterations; i++) {
 218 
 219     // Whether we will shrink or grow
 220     bool shrink = os::random() % 2L == 0;
 221 
 222     // Get random delta to resize virtual space
 223     size_t delta = (size_t)os::random() % magnitude;
 224 
 225     // If we are about to shrink virtual space below zero, then expand instead
 226     if (shrink && vs.committed_size() < delta) {
 227       shrink = false;
 228     }
 229 
 230     // Resizing by delta
 231     if (shrink) {
 232       vs.shrink_by(delta);
 233     } else {
 234       // If expanding fails expand_by will silently return false
 235       vs.expand_by(delta, true);
 236     }
 237   }
 238   return 0;
 239 }
 240 
 241 WB_ENTRY(jint, WB_StressVirtualSpaceResize(JNIEnv* env, jobject o,
 242         jlong reserved_space_size, jlong magnitude, jlong iterations))
 243   tty->print_cr("reservedSpaceSize="JLONG_FORMAT", magnitude="JLONG_FORMAT", "
 244                 "iterations="JLONG_FORMAT"\n", reserved_space_size, magnitude,
 245                 iterations);
 246   if (reserved_space_size < 0 || magnitude < 0 || iterations < 0) {
 247     tty->print_cr("One of variables printed above is negative. Can't proceed.\n");
 248     return 1;
 249   }
 250 
 251   // sizeof(size_t) depends on whether OS is 32bit or 64bit. sizeof(jlong) is
 252   // always 8 byte. That's why we should avoid overflow in case of 32bit platform.
 253   if (sizeof(size_t) < sizeof(jlong)) {
 254     jlong size_t_max_value = (jlong) SIZE_T_MAX_VALUE;
 255     if (reserved_space_size > size_t_max_value || magnitude > size_t_max_value
 256         || iterations > size_t_max_value) {
 257       tty->print_cr("One of variables printed above overflows size_t. Can't proceed.\n");
 258       return 2;
 259     }
 260   }
 261 
 262   return wb_stress_virtual_space_resize((size_t) reserved_space_size,
 263                                         (size_t) magnitude, (size_t) iterations);
 264 WB_END
 265 
 266 WB_ENTRY(jboolean, WB_isObjectInOldGen(JNIEnv* env, jobject o, jobject obj))
 267   oop p = JNIHandles::resolve(obj);
 268 #if INCLUDE_ALL_GCS
 269   if (UseG1GC) {
 270     G1CollectedHeap* g1 = G1CollectedHeap::heap();
 271     const HeapRegion* hr = g1->heap_region_containing(p);
 272     if (hr == NULL) {
 273       return false;
 274     }
 275     return !(hr->is_young());
 276   } else if (UseParallelGC) {
 277     ParallelScavengeHeap* psh = ParallelScavengeHeap::heap();
 278     return !psh->is_in_young(p);
 279   }
 280 #endif // INCLUDE_ALL_GCS
 281   GenCollectedHeap* gch = GenCollectedHeap::heap();
 282   return !gch->is_in_young(p);
 283 WB_END
 284 
 285 WB_ENTRY(jlong, WB_GetObjectSize(JNIEnv* env, jobject o, jobject obj))
 286   oop p = JNIHandles::resolve(obj);
 287   return p->size() * HeapWordSize;
 288 WB_END
 289 
 290 #if INCLUDE_ALL_GCS
 291 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj))
 292   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 293   oop result = JNIHandles::resolve(obj);
 294   const HeapRegion* hr = g1->heap_region_containing(result);
 295   return hr->is_humongous();
 296 WB_END
 297 
 298 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o))
 299   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 300   size_t nr = g1->num_free_regions();
 301   return (jlong)nr;
 302 WB_END
 303 
 304 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o))
 305   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 306   return g1->concurrent_mark()->cmThread()->during_cycle();
 307 WB_END
 308 
 309 WB_ENTRY(jboolean, WB_G1StartMarkCycle(JNIEnv* env, jobject o))
 310   G1CollectedHeap* g1h = G1CollectedHeap::heap();
 311   if (!g1h->concurrent_mark()->cmThread()->during_cycle()) {
 312     g1h->collect(GCCause::_wb_conc_mark);
 313     return true;
 314   }
 315   return false;
 316 WB_END
 317 
 318 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o))
 319   return (jint)HeapRegion::GrainBytes;
 320 WB_END
 321 #endif // INCLUDE_ALL_GCS
 322 
 323 #if INCLUDE_NMT
 324 // Alloc memory using the test memory type so that we can use that to see if
 325 // NMT picks it up correctly
 326 WB_ENTRY(jlong, WB_NMTMalloc(JNIEnv* env, jobject o, jlong size))
 327   jlong addr = 0;
 328   addr = (jlong)(uintptr_t)os::malloc(size, mtTest);
 329   return addr;
 330 WB_END
 331 
 332 // Alloc memory with pseudo call stack. The test can create psudo malloc
 333 // allocation site to stress the malloc tracking.
 334 WB_ENTRY(jlong, WB_NMTMallocWithPseudoStack(JNIEnv* env, jobject o, jlong size, jint pseudo_stack))
 335   address pc = (address)(size_t)pseudo_stack;
 336   NativeCallStack stack(&pc, 1);
 337   return (jlong)(uintptr_t)os::malloc(size, mtTest, stack);
 338 WB_END
 339 
 340 // Free the memory allocated by NMTAllocTest
 341 WB_ENTRY(void, WB_NMTFree(JNIEnv* env, jobject o, jlong mem))
 342   os::free((void*)(uintptr_t)mem);
 343 WB_END
 344 
 345 WB_ENTRY(jlong, WB_NMTReserveMemory(JNIEnv* env, jobject o, jlong size))
 346   jlong addr = 0;
 347 
 348   addr = (jlong)(uintptr_t)os::reserve_memory(size);
 349   MemTracker::record_virtual_memory_type((address)addr, mtTest);
 350 
 351   return addr;
 352 WB_END
 353 
 354 WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 355   os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem);
 356   MemTracker::record_virtual_memory_type((address)(uintptr_t)addr, mtTest);
 357 WB_END
 358 
 359 WB_ENTRY(void, WB_NMTUncommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 360   os::uncommit_memory((char *)(uintptr_t)addr, size);
 361 WB_END
 362 
 363 WB_ENTRY(void, WB_NMTReleaseMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 364   os::release_memory((char *)(uintptr_t)addr, size);
 365 WB_END
 366 
 367 WB_ENTRY(jboolean, WB_NMTChangeTrackingLevel(JNIEnv* env))
 368   // Test that we can downgrade NMT levels but not upgrade them.
 369   if (MemTracker::tracking_level() == NMT_off) {
 370     MemTracker::transition_to(NMT_off);
 371     return MemTracker::tracking_level() == NMT_off;
 372   } else {
 373     assert(MemTracker::tracking_level() == NMT_detail, "Should start out as detail tracking");
 374     MemTracker::transition_to(NMT_summary);
 375     assert(MemTracker::tracking_level() == NMT_summary, "Should be summary now");
 376 
 377     // Can't go to detail once NMT is set to summary.
 378     MemTracker::transition_to(NMT_detail);
 379     assert(MemTracker::tracking_level() == NMT_summary, "Should still be summary now");
 380 
 381     // Shutdown sets tracking level to minimal.
 382     MemTracker::shutdown();
 383     assert(MemTracker::tracking_level() == NMT_minimal, "Should be minimal now");
 384 
 385     // Once the tracking level is minimal, we cannot increase to summary.
 386     // The code ignores this request instead of asserting because if the malloc site
 387     // table overflows in another thread, it tries to change the code to summary.
 388     MemTracker::transition_to(NMT_summary);
 389     assert(MemTracker::tracking_level() == NMT_minimal, "Should still be minimal now");
 390 
 391     // Really can never go up to detail, verify that the code would never do this.
 392     MemTracker::transition_to(NMT_detail);
 393     assert(MemTracker::tracking_level() == NMT_minimal, "Should still be minimal now");
 394     return MemTracker::tracking_level() == NMT_minimal;
 395   }
 396 WB_END
 397 
 398 WB_ENTRY(jint, WB_NMTGetHashSize(JNIEnv* env, jobject o))
 399   int hash_size = MallocSiteTable::hash_buckets();
 400   assert(hash_size > 0, "NMT hash_size should be > 0");
 401   return (jint)hash_size;
 402 WB_END
 403 #endif // INCLUDE_NMT
 404 
 405 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) {
 406   assert(method != NULL, "method should not be null");
 407   ThreadToNativeFromVM ttn(thread);
 408   return env->FromReflectedMethod(method);
 409 }
 410 
 411 // Deoptimizes all compiled frames and makes nmethods not entrant if it's requested
 412 class VM_WhiteBoxDeoptimizeFrames : public VM_WhiteBoxOperation {
 413  private:
 414   int _result;
 415   const bool _make_not_entrant;
 416  public:
 417   VM_WhiteBoxDeoptimizeFrames(bool make_not_entrant) :
 418         _result(0), _make_not_entrant(make_not_entrant) { }
 419   int  result() const { return _result; }
 420 
 421   void doit() {
 422     for (JavaThread* t = Threads::first(); t != NULL; t = t->next()) {
 423       if (t->has_last_Java_frame()) {
 424         for (StackFrameStream fst(t, UseBiasedLocking); !fst.is_done(); fst.next()) {
 425           frame* f = fst.current();
 426           if (f->can_be_deoptimized() && !f->is_deoptimized_frame()) {
 427             RegisterMap* reg_map = fst.register_map();
 428             Deoptimization::deoptimize(t, *f, reg_map);
 429             if (_make_not_entrant) {
 430                 nmethod* nm = CodeCache::find_nmethod(f->pc());
 431                 assert(nm != NULL, "sanity check");
 432                 nm->make_not_entrant();
 433             }
 434             ++_result;
 435           }
 436         }
 437       }
 438     }
 439   }
 440 };
 441 
 442 WB_ENTRY(jint, WB_DeoptimizeFrames(JNIEnv* env, jobject o, jboolean make_not_entrant))
 443   VM_WhiteBoxDeoptimizeFrames op(make_not_entrant == JNI_TRUE);
 444   VMThread::execute(&op);
 445   return op.result();
 446 WB_END
 447 
 448 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o))
 449   MutexLockerEx mu(Compile_lock);
 450   CodeCache::mark_all_nmethods_for_deoptimization();
 451   VM_Deoptimize op;
 452   VMThread::execute(&op);
 453 WB_END
 454 
 455 WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 456   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 457   int result = 0;
 458   CHECK_JNI_EXCEPTION_(env, result);
 459   MutexLockerEx mu(Compile_lock);
 460   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 461   if (is_osr) {
 462     result += mh->mark_osr_nmethods();
 463   } else if (mh->code() != NULL) {
 464     mh->code()->mark_for_deoptimization();
 465     ++result;
 466   }
 467   result += CodeCache::mark_for_deoptimization(mh());
 468   if (result > 0) {
 469     VM_Deoptimize op;
 470     VMThread::execute(&op);
 471   }
 472   return result;
 473 WB_END
 474 
 475 WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 476   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 477   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 478   MutexLockerEx mu(Compile_lock);
 479   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 480   nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 481   if (code == NULL) {
 482     return JNI_FALSE;
 483   }
 484   return (code->is_alive() && !code->is_marked_for_deoptimization());
 485 WB_END
 486 
 487 WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 488   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 489   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 490   MutexLockerEx mu(Compile_lock);
 491   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 492   if (is_osr) {
 493     return CompilationPolicy::can_be_osr_compiled(mh, comp_level);
 494   } else {
 495     return CompilationPolicy::can_be_compiled(mh, comp_level);
 496   }
 497 WB_END
 498 
 499 WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method))
 500   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 501   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 502   MutexLockerEx mu(Compile_lock);
 503   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 504   return mh->queued_for_compilation();
 505 WB_END
 506 
 507 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 508   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 509   CHECK_JNI_EXCEPTION_(env, CompLevel_none);
 510   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 511   nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 512   return (code != NULL ? code->comp_level() : CompLevel_none);
 513 WB_END
 514 
 515 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 516   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 517   CHECK_JNI_EXCEPTION(env);
 518   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 519   if (is_osr) {
 520     mh->set_not_osr_compilable(comp_level, true /* report */, "WhiteBox");
 521   } else {
 522     mh->set_not_compilable(comp_level, true /* report */, "WhiteBox");
 523   }
 524 WB_END
 525 
 526 WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method))
 527   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 528   CHECK_JNI_EXCEPTION_(env, InvocationEntryBci);
 529   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 530   nmethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false);
 531   return (code != NULL && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci);
 532 WB_END
 533 
 534 WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 535   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 536   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 537   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 538   bool result = mh->dont_inline();
 539   mh->set_dont_inline(value == JNI_TRUE);
 540   return result;
 541 WB_END
 542 
 543 WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level))
 544   if (comp_level == CompLevel_any) {
 545     return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ +
 546         CompileBroker::queue_size(CompLevel_full_profile) /* C1 */;
 547   } else {
 548     return CompileBroker::queue_size(comp_level);
 549   }
 550 WB_END
 551 
 552 WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 553   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 554   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 555   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 556   bool result = mh->force_inline();
 557   mh->set_force_inline(value == JNI_TRUE);
 558   return result;
 559 WB_END
 560 
 561 WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci))
 562   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 563   CHECK_JNI_EXCEPTION_(env, JNI_FALSE);
 564   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 565   nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), "WhiteBox", THREAD);
 566   MutexLockerEx mu(Compile_lock);
 567   return (mh->queued_for_compilation() || nm != NULL);
 568 WB_END
 569 
 570 class AlwaysFalseClosure : public BoolObjectClosure {
 571  public:
 572   bool do_object_b(oop p) { return false; }
 573 };
 574 
 575 static AlwaysFalseClosure always_false;
 576 
 577 WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method))
 578   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 579   CHECK_JNI_EXCEPTION(env);
 580   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 581   MutexLockerEx mu(Compile_lock);
 582   MethodData* mdo = mh->method_data();
 583   MethodCounters* mcs = mh->method_counters();
 584 
 585   if (mdo != NULL) {
 586     mdo->init();
 587     ResourceMark rm;
 588     int arg_count = mdo->method()->size_of_parameters();
 589     for (int i = 0; i < arg_count; i++) {
 590       mdo->set_arg_modified(i, 0);
 591     }
 592     MutexLockerEx mu(mdo->extra_data_lock());
 593     mdo->clean_method_data(&always_false);
 594   }
 595 
 596   mh->clear_not_c1_compilable();
 597   mh->clear_not_c2_compilable();
 598   mh->clear_not_c2_osr_compilable();
 599   NOT_PRODUCT(mh->set_compiled_invocation_count(0));
 600   if (mcs != NULL) {
 601     mcs->backedge_counter()->init();
 602     mcs->invocation_counter()->init();
 603     mcs->set_interpreter_invocation_count(0);
 604     mcs->set_interpreter_throwout_count(0);
 605 
 606 #ifdef TIERED
 607     mcs->set_rate(0.0F);
 608     mh->set_prev_event_count(0);
 609     mh->set_prev_time(0);
 610 #endif
 611   }
 612 WB_END
 613 
 614 template <typename T>
 615 static bool GetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, bool (*TAt)(const char*, T*, bool, bool)) {
 616   if (name == NULL) {
 617     return false;
 618   }
 619   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 620   const char* flag_name = env->GetStringUTFChars(name, NULL);
 621   bool result = (*TAt)(flag_name, value, true, true);
 622   env->ReleaseStringUTFChars(name, flag_name);
 623   return result;
 624 }
 625 
 626 template <typename T>
 627 static bool SetVMFlag(JavaThread* thread, JNIEnv* env, jstring name, T* value, bool (*TAtPut)(const char*, T*, Flag::Flags)) {
 628   if (name == NULL) {
 629     return false;
 630   }
 631   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 632   const char* flag_name = env->GetStringUTFChars(name, NULL);
 633   bool result = (*TAtPut)(flag_name, value, Flag::INTERNAL);
 634   env->ReleaseStringUTFChars(name, flag_name);
 635   return result;
 636 }
 637 
 638 template <typename T>
 639 static jobject box(JavaThread* thread, JNIEnv* env, Symbol* name, Symbol* sig, T value) {
 640   ResourceMark rm(thread);
 641   jclass clazz = env->FindClass(name->as_C_string());
 642   CHECK_JNI_EXCEPTION_(env, NULL);
 643   jmethodID methodID = env->GetStaticMethodID(clazz,
 644         vmSymbols::valueOf_name()->as_C_string(),
 645         sig->as_C_string());
 646   CHECK_JNI_EXCEPTION_(env, NULL);
 647   jobject result = env->CallStaticObjectMethod(clazz, methodID, value);
 648   CHECK_JNI_EXCEPTION_(env, NULL);
 649   return result;
 650 }
 651 
 652 static jobject booleanBox(JavaThread* thread, JNIEnv* env, jboolean value) {
 653   return box(thread, env, vmSymbols::java_lang_Boolean(), vmSymbols::Boolean_valueOf_signature(), value);
 654 }
 655 static jobject integerBox(JavaThread* thread, JNIEnv* env, jint value) {
 656   return box(thread, env, vmSymbols::java_lang_Integer(), vmSymbols::Integer_valueOf_signature(), value);
 657 }
 658 static jobject longBox(JavaThread* thread, JNIEnv* env, jlong value) {
 659   return box(thread, env, vmSymbols::java_lang_Long(), vmSymbols::Long_valueOf_signature(), value);
 660 }
 661 /* static jobject floatBox(JavaThread* thread, JNIEnv* env, jfloat value) {
 662   return box(thread, env, vmSymbols::java_lang_Float(), vmSymbols::Float_valueOf_signature(), value);
 663 }*/
 664 static jobject doubleBox(JavaThread* thread, JNIEnv* env, jdouble value) {
 665   return box(thread, env, vmSymbols::java_lang_Double(), vmSymbols::Double_valueOf_signature(), value);
 666 }
 667 
 668 static Flag* getVMFlag(JavaThread* thread, JNIEnv* env, jstring name) {
 669   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 670   const char* flag_name = env->GetStringUTFChars(name, NULL);
 671   Flag* result = Flag::find_flag(flag_name, strlen(flag_name), true, true);
 672   env->ReleaseStringUTFChars(name, flag_name);
 673   return result;
 674 }
 675 
 676 WB_ENTRY(jboolean, WB_IsConstantVMFlag(JNIEnv* env, jobject o, jstring name))
 677   Flag* flag = getVMFlag(thread, env, name);
 678   return (flag != NULL) && flag->is_constant_in_binary();
 679 WB_END
 680 
 681 WB_ENTRY(jboolean, WB_IsLockedVMFlag(JNIEnv* env, jobject o, jstring name))
 682   Flag* flag = getVMFlag(thread, env, name);
 683   return (flag != NULL) && !(flag->is_unlocked() || flag->is_unlocker());
 684 WB_END
 685 
 686 WB_ENTRY(jobject, WB_GetBooleanVMFlag(JNIEnv* env, jobject o, jstring name))
 687   bool result;
 688   if (GetVMFlag <bool> (thread, env, name, &result, &CommandLineFlags::boolAt)) {
 689     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 690     return booleanBox(thread, env, result);
 691   }
 692   return NULL;
 693 WB_END
 694 
 695 WB_ENTRY(jobject, WB_GetIntxVMFlag(JNIEnv* env, jobject o, jstring name))
 696   intx result;
 697   if (GetVMFlag <intx> (thread, env, name, &result, &CommandLineFlags::intxAt)) {
 698     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 699     return longBox(thread, env, result);
 700   }
 701   return NULL;
 702 WB_END
 703 
 704 WB_ENTRY(jobject, WB_GetUintxVMFlag(JNIEnv* env, jobject o, jstring name))
 705   uintx result;
 706   if (GetVMFlag <uintx> (thread, env, name, &result, &CommandLineFlags::uintxAt)) {
 707     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 708     return longBox(thread, env, result);
 709   }
 710   return NULL;
 711 WB_END
 712 
 713 WB_ENTRY(jobject, WB_GetUint64VMFlag(JNIEnv* env, jobject o, jstring name))
 714   uint64_t result;
 715   if (GetVMFlag <uint64_t> (thread, env, name, &result, &CommandLineFlags::uint64_tAt)) {
 716     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 717     return longBox(thread, env, result);
 718   }
 719   return NULL;
 720 WB_END
 721 
 722 WB_ENTRY(jobject, WB_GetSizeTVMFlag(JNIEnv* env, jobject o, jstring name))
 723   uintx result;
 724   if (GetVMFlag <size_t> (thread, env, name, &result, &CommandLineFlags::size_tAt)) {
 725     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 726     return longBox(thread, env, result);
 727   }
 728   return NULL;
 729 WB_END
 730 
 731 WB_ENTRY(jobject, WB_GetDoubleVMFlag(JNIEnv* env, jobject o, jstring name))
 732   double result;
 733   if (GetVMFlag <double> (thread, env, name, &result, &CommandLineFlags::doubleAt)) {
 734     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 735     return doubleBox(thread, env, result);
 736   }
 737   return NULL;
 738 WB_END
 739 
 740 WB_ENTRY(jstring, WB_GetStringVMFlag(JNIEnv* env, jobject o, jstring name))
 741   ccstr ccstrResult;
 742   if (GetVMFlag <ccstr> (thread, env, name, &ccstrResult, &CommandLineFlags::ccstrAt)) {
 743     ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 744     jstring result = env->NewStringUTF(ccstrResult);
 745     CHECK_JNI_EXCEPTION_(env, NULL);
 746     return result;
 747   }
 748   return NULL;
 749 WB_END
 750 
 751 WB_ENTRY(void, WB_SetBooleanVMFlag(JNIEnv* env, jobject o, jstring name, jboolean value))
 752   bool result = value == JNI_TRUE ? true : false;
 753   SetVMFlag <bool> (thread, env, name, &result, &CommandLineFlags::boolAtPut);
 754 WB_END
 755 
 756 WB_ENTRY(void, WB_SetIntxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 757   intx result = value;
 758   SetVMFlag <intx> (thread, env, name, &result, &CommandLineFlags::intxAtPut);
 759 WB_END
 760 
 761 WB_ENTRY(void, WB_SetUintxVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 762   uintx result = value;
 763   SetVMFlag <uintx> (thread, env, name, &result, &CommandLineFlags::uintxAtPut);
 764 WB_END
 765 
 766 WB_ENTRY(void, WB_SetUint64VMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 767   uint64_t result = value;
 768   SetVMFlag <uint64_t> (thread, env, name, &result, &CommandLineFlags::uint64_tAtPut);
 769 WB_END
 770 
 771 WB_ENTRY(void, WB_SetSizeTVMFlag(JNIEnv* env, jobject o, jstring name, jlong value))
 772   size_t result = value;
 773   SetVMFlag <size_t> (thread, env, name, &result, &CommandLineFlags::size_tAtPut);
 774 WB_END
 775 
 776 WB_ENTRY(void, WB_SetDoubleVMFlag(JNIEnv* env, jobject o, jstring name, jdouble value))
 777   double result = value;
 778   SetVMFlag <double> (thread, env, name, &result, &CommandLineFlags::doubleAtPut);
 779 WB_END
 780 
 781 WB_ENTRY(void, WB_SetStringVMFlag(JNIEnv* env, jobject o, jstring name, jstring value))
 782   ThreadToNativeFromVM ttnfv(thread);   // can't be in VM when we call JNI
 783   const char* ccstrValue = (value == NULL) ? NULL : env->GetStringUTFChars(value, NULL);
 784   ccstr ccstrResult = ccstrValue;
 785   bool needFree;
 786   {
 787     ThreadInVMfromNative ttvfn(thread); // back to VM
 788     needFree = SetVMFlag <ccstr> (thread, env, name, &ccstrResult, &CommandLineFlags::ccstrAtPut);
 789   }
 790   if (value != NULL) {
 791     env->ReleaseStringUTFChars(value, ccstrValue);
 792   }
 793   if (needFree) {
 794     FREE_C_HEAP_ARRAY(char, ccstrResult);
 795   }
 796 WB_END
 797 
 798 WB_ENTRY(void, WB_LockCompilation(JNIEnv* env, jobject o, jlong timeout))
 799   WhiteBox::compilation_locked = true;
 800 WB_END
 801 
 802 WB_ENTRY(void, WB_UnlockCompilation(JNIEnv* env, jobject o))
 803   MonitorLockerEx mo(Compilation_lock, Mutex::_no_safepoint_check_flag);
 804   WhiteBox::compilation_locked = false;
 805   mo.notify_all();
 806 WB_END
 807 
 808 void WhiteBox::sweeper_thread_entry(JavaThread* thread, TRAPS) {
 809   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
 810   {
 811     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 812     NMethodSweeper::_should_sweep = true;
 813   }
 814   NMethodSweeper::possibly_sweep();
 815 }
 816 
 817 JavaThread* WhiteBox::create_sweeper_thread(TRAPS) {
 818   // create sweeper thread w/ custom entry -- one iteration instead of loop
 819   CodeCacheSweeperThread* sweeper_thread = new CodeCacheSweeperThread();
 820   sweeper_thread->set_entry_point(&WhiteBox::sweeper_thread_entry);
 821 
 822   // create j.l.Thread object and associate it w/ sweeper thread
 823   {
 824     // inherit deamon property from current thread
 825     bool is_daemon = java_lang_Thread::is_daemon(JavaThread::current()->threadObj());
 826 
 827     HandleMark hm(THREAD);
 828     Handle thread_group(THREAD, Universe::system_thread_group());
 829     const char* name = "WB Sweeper thread";
 830     sweeper_thread->allocate_threadObj(thread_group, name, is_daemon, THREAD);
 831   }
 832 
 833   {
 834     MutexLocker mu(Threads_lock, THREAD);
 835     Threads::add(sweeper_thread);
 836   }
 837   return sweeper_thread;
 838 }
 839 
 840 WB_ENTRY(jobject, WB_ForceNMethodSweep(JNIEnv* env, jobject o))
 841   JavaThread* sweeper_thread = WhiteBox::create_sweeper_thread(Thread::current());
 842   if (sweeper_thread == NULL) {
 843     return NULL;
 844   }
 845   jobject result = JNIHandles::make_local(env, sweeper_thread->threadObj());
 846   Thread::start(sweeper_thread);
 847   return result;
 848 WB_END
 849 
 850 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString))
 851   ResourceMark rm(THREAD);
 852   int len;
 853   jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false);
 854   return (StringTable::lookup(name, len) != NULL);
 855 WB_END
 856 
 857 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o))
 858   Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(true);
 859   Universe::heap()->collect(GCCause::_last_ditch_collection);
 860 #if INCLUDE_ALL_GCS
 861   if (UseG1GC) {
 862     // Needs to be cleared explicitly for G1
 863     Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(false);
 864   }
 865 #endif // INCLUDE_ALL_GCS
 866 WB_END
 867 
 868 WB_ENTRY(void, WB_YoungGC(JNIEnv* env, jobject o))
 869   Universe::heap()->collect(GCCause::_wb_young_gc);
 870 WB_END
 871 
 872 WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o))
 873   // static+volatile in order to force the read to happen
 874   // (not be eliminated by the compiler)
 875   static char c;
 876   static volatile char* p;
 877 
 878   p = os::reserve_memory(os::vm_allocation_granularity(), NULL, 0);
 879   if (p == NULL) {
 880     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory");
 881   }
 882 
 883   c = *p;
 884 WB_END
 885 
 886 WB_ENTRY(jstring, WB_GetCPUFeatures(JNIEnv* env, jobject o))
 887   const char* cpu_features = VM_Version::cpu_features();
 888   ThreadToNativeFromVM ttn(thread);
 889   jstring features_string = env->NewStringUTF(cpu_features);
 890 
 891   CHECK_JNI_EXCEPTION_(env, NULL);
 892 
 893   return features_string;
 894 WB_END
 895 
 896 int WhiteBox::get_blob_type(const CodeBlob* code) {
 897   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
 898   return CodeCache::get_code_heap(code)->code_blob_type();
 899 }
 900 
 901 CodeHeap* WhiteBox::get_code_heap(int blob_type) {
 902   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
 903   return CodeCache::get_code_heap(blob_type);
 904 }
 905 
 906 struct CodeBlobStub {
 907   CodeBlobStub(const CodeBlob* blob) :
 908       name(os::strdup(blob->name())),
 909       size(blob->size()),
 910       blob_type(WhiteBox::get_blob_type(blob)) { }
 911   ~CodeBlobStub() { os::free((void*) name); }
 912   const char* const name;
 913   const int         size;
 914   const int         blob_type;
 915 };
 916 
 917 static jobjectArray codeBlob2objectArray(JavaThread* thread, JNIEnv* env, CodeBlobStub* cb) {
 918   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
 919   CHECK_JNI_EXCEPTION_(env, NULL);
 920   jobjectArray result = env->NewObjectArray(3, clazz, NULL);
 921 
 922   jstring name = env->NewStringUTF(cb->name);
 923   CHECK_JNI_EXCEPTION_(env, NULL);
 924   env->SetObjectArrayElement(result, 0, name);
 925 
 926   jobject obj = integerBox(thread, env, cb->size);
 927   CHECK_JNI_EXCEPTION_(env, NULL);
 928   env->SetObjectArrayElement(result, 1, obj);
 929 
 930   obj = integerBox(thread, env, cb->blob_type);
 931   CHECK_JNI_EXCEPTION_(env, NULL);
 932   env->SetObjectArrayElement(result, 2, obj);
 933 
 934   return result;
 935 }
 936 
 937 WB_ENTRY(jobjectArray, WB_GetNMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 938   ResourceMark rm(THREAD);
 939   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 940   CHECK_JNI_EXCEPTION_(env, NULL);
 941   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 942   nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 943   jobjectArray result = NULL;
 944   if (code == NULL) {
 945     return result;
 946   }
 947   int insts_size = code->insts_size();
 948 
 949   ThreadToNativeFromVM ttn(thread);
 950   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
 951   CHECK_JNI_EXCEPTION_(env, NULL);
 952   result = env->NewObjectArray(4, clazz, NULL);
 953   if (result == NULL) {
 954     return result;
 955   }
 956 
 957   CodeBlobStub stub(code);
 958   jobjectArray codeBlob = codeBlob2objectArray(thread, env, &stub);
 959   env->SetObjectArrayElement(result, 0, codeBlob);
 960 
 961   jobject level = integerBox(thread, env, code->comp_level());
 962   CHECK_JNI_EXCEPTION_(env, NULL);
 963   env->SetObjectArrayElement(result, 1, level);
 964 
 965   jbyteArray insts = env->NewByteArray(insts_size);
 966   CHECK_JNI_EXCEPTION_(env, NULL);
 967   env->SetByteArrayRegion(insts, 0, insts_size, (jbyte*) code->insts_begin());
 968   env->SetObjectArrayElement(result, 2, insts);
 969 
 970   jobject id = integerBox(thread, env, code->compile_id());
 971   CHECK_JNI_EXCEPTION_(env, NULL);
 972   env->SetObjectArrayElement(result, 3, id);
 973 
 974   return result;
 975 WB_END
 976 
 977 CodeBlob* WhiteBox::allocate_code_blob(int size, int blob_type) {
 978   guarantee(WhiteBoxAPI, "internal testing API :: WhiteBox has to be enabled");
 979   BufferBlob* blob;
 980   int full_size = CodeBlob::align_code_offset(sizeof(BufferBlob));
 981   if (full_size < size) {
 982     full_size += round_to(size - full_size, oopSize);
 983   }
 984   {
 985     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 986     blob = (BufferBlob*) CodeCache::allocate(full_size, blob_type);
 987     ::new (blob) BufferBlob("WB::DummyBlob", full_size);
 988   }
 989   // Track memory usage statistic after releasing CodeCache_lock
 990   MemoryService::track_code_cache_memory_usage();
 991   return blob;
 992 }
 993 
 994 WB_ENTRY(jlong, WB_AllocateCodeBlob(JNIEnv* env, jobject o, jint size, jint blob_type))
 995     return (jlong) WhiteBox::allocate_code_blob(size, blob_type);
 996 WB_END
 997 
 998 WB_ENTRY(void, WB_FreeCodeBlob(JNIEnv* env, jobject o, jlong addr))
 999     BufferBlob::free((BufferBlob*) addr);
1000 WB_END
1001 
1002 WB_ENTRY(jobjectArray, WB_GetCodeHeapEntries(JNIEnv* env, jobject o, jint blob_type))
1003   ResourceMark rm;
1004   GrowableArray<CodeBlobStub*> blobs;
1005   {
1006     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1007     CodeHeap* heap = WhiteBox::get_code_heap(blob_type);
1008     if (heap == NULL) {
1009       return NULL;
1010     }
1011     for (CodeBlob* cb = (CodeBlob*) heap->first();
1012          cb != NULL; cb = (CodeBlob*) heap->next(cb)) {
1013       CodeBlobStub* stub = NEW_RESOURCE_OBJ(CodeBlobStub);
1014       new (stub) CodeBlobStub(cb);
1015       blobs.append(stub);
1016     }
1017   }
1018   if (blobs.length() == 0) {
1019     return NULL;
1020   }
1021   ThreadToNativeFromVM ttn(thread);
1022   jobjectArray result = NULL;
1023   jclass clazz = env->FindClass(vmSymbols::java_lang_Object()->as_C_string());
1024   CHECK_JNI_EXCEPTION_(env, NULL);
1025   result = env->NewObjectArray(blobs.length(), clazz, NULL);
1026   if (result == NULL) {
1027     return result;
1028   }
1029   int i = 0;
1030   for (GrowableArrayIterator<CodeBlobStub*> it = blobs.begin();
1031        it != blobs.end(); ++it) {
1032     jobjectArray obj = codeBlob2objectArray(thread, env, *it);
1033     env->SetObjectArrayElement(result, i, obj);
1034     ++i;
1035   }
1036   return result;
1037 WB_END
1038 
1039 WB_ENTRY(jint, WB_GetCompilationActivityMode(JNIEnv* env, jobject o))
1040   return CompileBroker::get_compilation_activity_mode();
1041 WB_END
1042 
1043 WB_ENTRY(jobjectArray, WB_GetCodeBlob(JNIEnv* env, jobject o, jlong addr))
1044     ThreadToNativeFromVM ttn(thread);
1045     CodeBlobStub stub((CodeBlob*) addr);
1046     return codeBlob2objectArray(thread, env, &stub);
1047 WB_END
1048 
1049 WB_ENTRY(jlong, WB_GetThreadStackSize(JNIEnv* env, jobject o))
1050   return (jlong) Thread::current()->stack_size();
1051 WB_END
1052 
1053 WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o))
1054   JavaThread* t = JavaThread::current();
1055   return (jlong) t->stack_available(os::current_stack_pointer()) - (jlong) StackShadowPages * os::vm_page_size();
1056 WB_END
1057 
1058 int WhiteBox::array_bytes_to_length(size_t bytes) {
1059   return Array<u1>::bytes_to_length(bytes);
1060 }
1061 
1062 WB_ENTRY(jlong, WB_AllocateMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong size))
1063   if (size < 0) {
1064     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1065         err_msg("WB_AllocateMetaspace: size is negative: " JLONG_FORMAT, size));
1066   }
1067 
1068   oop class_loader_oop = JNIHandles::resolve(class_loader);
1069   ClassLoaderData* cld = class_loader_oop != NULL
1070       ? java_lang_ClassLoader::loader_data(class_loader_oop)
1071       : ClassLoaderData::the_null_class_loader_data();
1072 
1073   void* metadata = MetadataFactory::new_writeable_array<u1>(cld, WhiteBox::array_bytes_to_length((size_t)size), thread);
1074 
1075   return (jlong)(uintptr_t)metadata;
1076 WB_END
1077 
1078 WB_ENTRY(void, WB_FreeMetaspace(JNIEnv* env, jobject wb, jobject class_loader, jlong addr, jlong size))
1079   oop class_loader_oop = JNIHandles::resolve(class_loader);
1080   ClassLoaderData* cld = class_loader_oop != NULL
1081       ? java_lang_ClassLoader::loader_data(class_loader_oop)
1082       : ClassLoaderData::the_null_class_loader_data();
1083 
1084   MetadataFactory::free_array(cld, (Array<u1>*)(uintptr_t)addr);
1085 WB_END
1086 
1087 WB_ENTRY(jlong, WB_IncMetaspaceCapacityUntilGC(JNIEnv* env, jobject wb, jlong inc))
1088   if (inc < 0) {
1089     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1090         err_msg("WB_IncMetaspaceCapacityUntilGC: inc is negative: " JLONG_FORMAT, inc));
1091   }
1092 
1093   jlong max_size_t = (jlong) ((size_t) -1);
1094   if (inc > max_size_t) {
1095     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1096         err_msg("WB_IncMetaspaceCapacityUntilGC: inc does not fit in size_t: " JLONG_FORMAT, inc));
1097   }
1098 
1099   size_t new_cap_until_GC = 0;
1100   size_t aligned_inc = align_size_down((size_t) inc, Metaspace::commit_alignment());
1101   bool success = MetaspaceGC::inc_capacity_until_GC(aligned_inc, &new_cap_until_GC);
1102   if (!success) {
1103     THROW_MSG_0(vmSymbols::java_lang_IllegalStateException(),
1104                 "WB_IncMetaspaceCapacityUntilGC: could not increase capacity until GC "
1105                 "due to contention with another thread");
1106   }
1107   return (jlong) new_cap_until_GC;
1108 WB_END
1109 
1110 WB_ENTRY(jlong, WB_MetaspaceCapacityUntilGC(JNIEnv* env, jobject wb))
1111   return (jlong) MetaspaceGC::capacity_until_GC();
1112 WB_END
1113 
1114 WB_ENTRY(void, WB_AssertMatchingSafepointCalls(JNIEnv* env, jobject o, jboolean mutexSafepointValue, jboolean attemptedNoSafepointValue))
1115   Monitor::SafepointCheckRequired sfpt_check_required = mutexSafepointValue ?
1116                                            Monitor::_safepoint_check_always :
1117                                            Monitor::_safepoint_check_never;
1118   MutexLockerEx ml(new Mutex(Mutex::leaf, "SFPT_Test_lock", true, sfpt_check_required),
1119                    attemptedNoSafepointValue == JNI_TRUE);
1120 WB_END
1121 
1122 WB_ENTRY(jboolean, WB_IsMonitorInflated(JNIEnv* env, jobject wb, jobject obj))
1123   oop obj_oop = JNIHandles::resolve(obj);
1124   return (jboolean) obj_oop->mark()->has_monitor();
1125 WB_END
1126 
1127 WB_ENTRY(void, WB_ForceSafepoint(JNIEnv* env, jobject wb))
1128   VM_ForceSafepoint force_safepoint_op;
1129   VMThread::execute(&force_safepoint_op);
1130 WB_END
1131 
1132 template <typename T>
1133 static bool GetMethodOption(JavaThread* thread, JNIEnv* env, jobject method, jstring name, T* value) {
1134   assert(value != NULL, "sanity");
1135   if (method == NULL || name == NULL) {
1136     return false;
1137   }
1138   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
1139   CHECK_JNI_EXCEPTION_(env, false);
1140   methodHandle mh(thread, Method::checked_resolve_jmethod_id(jmid));
1141   // can't be in VM when we call JNI
1142   ThreadToNativeFromVM ttnfv(thread);
1143   const char* flag_name = env->GetStringUTFChars(name, NULL);
1144   bool result =  CompilerOracle::has_option_value(mh, flag_name, *value);
1145   env->ReleaseStringUTFChars(name, flag_name);
1146   return result;
1147 }
1148 
1149 WB_ENTRY(jobject, WB_GetMethodBooleaneOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1150   bool result;
1151   if (GetMethodOption<bool> (thread, env, method, name, &result)) {
1152     // can't be in VM when we call JNI
1153     ThreadToNativeFromVM ttnfv(thread);
1154     return booleanBox(thread, env, result);
1155   }
1156   return NULL;
1157 WB_END
1158 
1159 WB_ENTRY(jobject, WB_GetMethodIntxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1160   intx result;
1161   if (GetMethodOption <intx> (thread, env, method, name, &result)) {
1162     // can't be in VM when we call JNI
1163     ThreadToNativeFromVM ttnfv(thread);
1164     return longBox(thread, env, result);
1165   }
1166   return NULL;
1167 WB_END
1168 
1169 WB_ENTRY(jobject, WB_GetMethodUintxOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1170   uintx result;
1171   if (GetMethodOption <uintx> (thread, env, method, name, &result)) {
1172     // can't be in VM when we call JNI
1173     ThreadToNativeFromVM ttnfv(thread);
1174     return longBox(thread, env, result);
1175   }
1176   return NULL;
1177 WB_END
1178 
1179 WB_ENTRY(jobject, WB_GetMethodDoubleOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1180   double result;
1181   if (GetMethodOption <double> (thread, env, method, name, &result)) {
1182     // can't be in VM when we call JNI
1183     ThreadToNativeFromVM ttnfv(thread);
1184     return doubleBox(thread, env, result);
1185   }
1186   return NULL;
1187 WB_END
1188 
1189 WB_ENTRY(jobject, WB_GetMethodStringOption(JNIEnv* env, jobject wb, jobject method, jstring name))
1190   ccstr ccstrResult;
1191   if (GetMethodOption <ccstr> (thread, env, method, name, &ccstrResult)) {
1192     // can't be in VM when we call JNI
1193     ThreadToNativeFromVM ttnfv(thread);
1194     jstring result = env->NewStringUTF(ccstrResult);
1195     CHECK_JNI_EXCEPTION_(env, NULL);
1196     return result;
1197   }
1198   return NULL;
1199 WB_END
1200 
1201 //Some convenience methods to deal with objects from java
1202 int WhiteBox::offset_for_field(const char* field_name, oop object,
1203     Symbol* signature_symbol) {
1204   assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid");
1205   Thread* THREAD = Thread::current();
1206 
1207   //Get the class of our object
1208   Klass* arg_klass = object->klass();
1209   //Turn it into an instance-klass
1210   InstanceKlass* ik = InstanceKlass::cast(arg_klass);
1211 
1212   //Create symbols to look for in the class
1213   TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name),
1214       THREAD);
1215 
1216   //To be filled in with an offset of the field we're looking for
1217   fieldDescriptor fd;
1218 
1219   Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
1220   if (res == NULL) {
1221     tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
1222         name_symbol->as_C_string());
1223     fatal("Invalid layout of preloaded class");
1224   }
1225 
1226   //fetch the field at the offset we've found
1227   int dest_offset = fd.offset();
1228 
1229   return dest_offset;
1230 }
1231 
1232 
1233 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
1234   int offset = offset_for_field(field_name, object,
1235       vmSymbols::string_signature());
1236   oop string = object->obj_field(offset);
1237   if (string == NULL) {
1238     return NULL;
1239   }
1240   const char* ret = java_lang_String::as_utf8_string(string);
1241   return ret;
1242 }
1243 
1244 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
1245   int offset =
1246       offset_for_field(field_name, object, vmSymbols::bool_signature());
1247   bool ret = (object->bool_field(offset) == JNI_TRUE);
1248   return ret;
1249 }
1250 
1251 void WhiteBox::register_methods(JNIEnv* env, jclass wbclass, JavaThread* thread, JNINativeMethod* method_array, int method_count) {
1252   ResourceMark rm;
1253   ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
1254 
1255   //  one by one registration natives for exception catching
1256   jclass no_such_method_error_klass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string());
1257   CHECK_JNI_EXCEPTION(env);
1258   for (int i = 0, n = method_count; i < n; ++i) {
1259     // Skip dummy entries
1260     if (method_array[i].fnPtr == NULL) continue;
1261     if (env->RegisterNatives(wbclass, &method_array[i], 1) != 0) {
1262       jthrowable throwable_obj = env->ExceptionOccurred();
1263       if (throwable_obj != NULL) {
1264         env->ExceptionClear();
1265         if (env->IsInstanceOf(throwable_obj, no_such_method_error_klass)) {
1266           // NoSuchMethodError is thrown when a method can't be found or a method is not native.
1267           // Ignoring the exception since it is not preventing use of other WhiteBox methods.
1268           tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s",
1269               method_array[i].name, method_array[i].signature);
1270         }
1271       } else {
1272         // Registration failed unexpectedly.
1273         tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered",
1274             method_array[i].name, method_array[i].signature);
1275         env->UnregisterNatives(wbclass);
1276         break;
1277       }
1278     }
1279   }
1280 }
1281 
1282 #define CC (char*)
1283 
1284 static JNINativeMethod methods[] = {
1285   {CC"getObjectAddress",   CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress  },
1286   {CC"getObjectSize",      CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectSize     },
1287   {CC"isObjectInOldGen",   CC"(Ljava/lang/Object;)Z", (void*)&WB_isObjectInOldGen  },
1288   {CC"getHeapOopSize",     CC"()I",                   (void*)&WB_GetHeapOopSize    },
1289   {CC"getVMPageSize",      CC"()I",                   (void*)&WB_GetVMPageSize     },
1290   {CC"isClassAlive0",      CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive      },
1291   {CC"parseCommandLine",
1292       CC"(Ljava/lang/String;C[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;",
1293       (void*) &WB_ParseCommandLine
1294   },
1295   {CC"addToBootstrapClassLoaderSearch", CC"(Ljava/lang/String;)V",
1296                                                       (void*)&WB_AddToBootstrapClassLoaderSearch},
1297   {CC"addToSystemClassLoaderSearch",    CC"(Ljava/lang/String;)V",
1298                                                       (void*)&WB_AddToSystemClassLoaderSearch},
1299   {CC"getCompressedOopsMaxHeapSize", CC"()J",
1300       (void*)&WB_GetCompressedOopsMaxHeapSize},
1301   {CC"printHeapSizes",     CC"()V",                   (void*)&WB_PrintHeapSizes    },
1302   {CC"runMemoryUnitTests", CC"()V",                   (void*)&WB_RunMemoryUnitTests},
1303   {CC"readFromNoaccessArea",CC"()V",                  (void*)&WB_ReadFromNoaccessArea},
1304   {CC"stressVirtualSpaceResize",CC"(JJJ)I",           (void*)&WB_StressVirtualSpaceResize},
1305 #if INCLUDE_ALL_GCS
1306   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
1307   {CC"g1IsHumongous",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
1308   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
1309   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
1310   {CC"g1StartConcMarkCycle",       CC"()Z",           (void*)&WB_G1StartMarkCycle  },
1311 #endif // INCLUDE_ALL_GCS
1312 #if INCLUDE_NMT
1313   {CC"NMTMalloc",           CC"(J)J",                 (void*)&WB_NMTMalloc          },
1314   {CC"NMTMallocWithPseudoStack", CC"(JI)J",           (void*)&WB_NMTMallocWithPseudoStack},
1315   {CC"NMTFree",             CC"(J)V",                 (void*)&WB_NMTFree            },
1316   {CC"NMTReserveMemory",    CC"(J)J",                 (void*)&WB_NMTReserveMemory   },
1317   {CC"NMTCommitMemory",     CC"(JJ)V",                (void*)&WB_NMTCommitMemory    },
1318   {CC"NMTUncommitMemory",   CC"(JJ)V",                (void*)&WB_NMTUncommitMemory  },
1319   {CC"NMTReleaseMemory",    CC"(JJ)V",                (void*)&WB_NMTReleaseMemory   },
1320   {CC"NMTChangeTrackingLevel", CC"()Z",               (void*)&WB_NMTChangeTrackingLevel},
1321   {CC"NMTGetHashSize",      CC"()I",                  (void*)&WB_NMTGetHashSize     },
1322 #endif // INCLUDE_NMT
1323   {CC"deoptimizeFrames",   CC"(Z)I",                  (void*)&WB_DeoptimizeFrames  },
1324   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
1325   {CC"deoptimizeMethod",   CC"(Ljava/lang/reflect/Executable;Z)I",
1326                                                       (void*)&WB_DeoptimizeMethod  },
1327   {CC"isMethodCompiled",   CC"(Ljava/lang/reflect/Executable;Z)Z",
1328                                                       (void*)&WB_IsMethodCompiled  },
1329   {CC"isMethodCompilable", CC"(Ljava/lang/reflect/Executable;IZ)Z",
1330                                                       (void*)&WB_IsMethodCompilable},
1331   {CC"isMethodQueuedForCompilation",
1332       CC"(Ljava/lang/reflect/Executable;)Z",          (void*)&WB_IsMethodQueuedForCompilation},
1333   {CC"makeMethodNotCompilable",
1334       CC"(Ljava/lang/reflect/Executable;IZ)V",        (void*)&WB_MakeMethodNotCompilable},
1335   {CC"testSetDontInlineMethod",
1336       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetDontInlineMethod},
1337   {CC"getMethodCompilationLevel",
1338       CC"(Ljava/lang/reflect/Executable;Z)I",         (void*)&WB_GetMethodCompilationLevel},
1339   {CC"getMethodEntryBci",
1340       CC"(Ljava/lang/reflect/Executable;)I",          (void*)&WB_GetMethodEntryBci},
1341   {CC"getCompileQueueSize",
1342       CC"(I)I",                                       (void*)&WB_GetCompileQueueSize},
1343   {CC"testSetForceInlineMethod",
1344       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetForceInlineMethod},
1345   {CC"enqueueMethodForCompilation",
1346       CC"(Ljava/lang/reflect/Executable;II)Z",        (void*)&WB_EnqueueMethodForCompilation},
1347   {CC"clearMethodState",
1348       CC"(Ljava/lang/reflect/Executable;)V",          (void*)&WB_ClearMethodState},
1349   {CC"lockCompilation",    CC"()V",                   (void*)&WB_LockCompilation},
1350   {CC"unlockCompilation",  CC"()V",                   (void*)&WB_UnlockCompilation},
1351   {CC"isConstantVMFlag",   CC"(Ljava/lang/String;)Z", (void*)&WB_IsConstantVMFlag},
1352   {CC"isLockedVMFlag",     CC"(Ljava/lang/String;)Z", (void*)&WB_IsLockedVMFlag},
1353   {CC"setBooleanVMFlag",   CC"(Ljava/lang/String;Z)V",(void*)&WB_SetBooleanVMFlag},
1354   {CC"setIntxVMFlag",      CC"(Ljava/lang/String;J)V",(void*)&WB_SetIntxVMFlag},
1355   {CC"setUintxVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetUintxVMFlag},
1356   {CC"setUint64VMFlag",    CC"(Ljava/lang/String;J)V",(void*)&WB_SetUint64VMFlag},
1357   {CC"setSizeTVMFlag",     CC"(Ljava/lang/String;J)V",(void*)&WB_SetSizeTVMFlag},
1358   {CC"setDoubleVMFlag",    CC"(Ljava/lang/String;D)V",(void*)&WB_SetDoubleVMFlag},
1359   {CC"setStringVMFlag",    CC"(Ljava/lang/String;Ljava/lang/String;)V",
1360                                                       (void*)&WB_SetStringVMFlag},
1361   {CC"getBooleanVMFlag",   CC"(Ljava/lang/String;)Ljava/lang/Boolean;",
1362                                                       (void*)&WB_GetBooleanVMFlag},
1363   {CC"getIntxVMFlag",      CC"(Ljava/lang/String;)Ljava/lang/Long;",
1364                                                       (void*)&WB_GetIntxVMFlag},
1365   {CC"getUintxVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
1366                                                       (void*)&WB_GetUintxVMFlag},
1367   {CC"getUint64VMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Long;",
1368                                                       (void*)&WB_GetUint64VMFlag},
1369   {CC"getSizeTVMFlag",     CC"(Ljava/lang/String;)Ljava/lang/Long;",
1370                                                       (void*)&WB_GetSizeTVMFlag},
1371   {CC"getDoubleVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/Double;",
1372                                                       (void*)&WB_GetDoubleVMFlag},
1373   {CC"getStringVMFlag",    CC"(Ljava/lang/String;)Ljava/lang/String;",
1374                                                       (void*)&WB_GetStringVMFlag},
1375   {CC"isInStringTable",    CC"(Ljava/lang/String;)Z", (void*)&WB_IsInStringTable  },
1376   {CC"fullGC",   CC"()V",                             (void*)&WB_FullGC },
1377   {CC"youngGC",  CC"()V",                             (void*)&WB_YoungGC },
1378   {CC"readReservedMemory", CC"()V",                   (void*)&WB_ReadReservedMemory },
1379   {CC"allocateMetaspace",
1380      CC"(Ljava/lang/ClassLoader;J)J",                 (void*)&WB_AllocateMetaspace },
1381   {CC"freeMetaspace",
1382      CC"(Ljava/lang/ClassLoader;JJ)V",                (void*)&WB_FreeMetaspace },
1383   {CC"incMetaspaceCapacityUntilGC", CC"(J)J",         (void*)&WB_IncMetaspaceCapacityUntilGC },
1384   {CC"metaspaceCapacityUntilGC", CC"()J",             (void*)&WB_MetaspaceCapacityUntilGC },
1385   {CC"getCPUFeatures",     CC"()Ljava/lang/String;",  (void*)&WB_GetCPUFeatures     },
1386   {CC"getNMethod",         CC"(Ljava/lang/reflect/Executable;Z)[Ljava/lang/Object;",
1387                                                       (void*)&WB_GetNMethod         },
1388   {CC"forceNMethodSweep0", CC"()Ljava/lang/Thread;",  (void*)&WB_ForceNMethodSweep  },
1389   {CC"allocateCodeBlob",   CC"(II)J",                 (void*)&WB_AllocateCodeBlob   },
1390   {CC"freeCodeBlob",       CC"(J)V",                  (void*)&WB_FreeCodeBlob       },
1391   {CC"getCodeHeapEntries", CC"(I)[Ljava/lang/Object;",(void*)&WB_GetCodeHeapEntries },
1392   {CC"getCompilationActivityMode",
1393                            CC"()I",                   (void*)&WB_GetCompilationActivityMode},
1394   {CC"getCodeBlob",        CC"(J)[Ljava/lang/Object;",(void*)&WB_GetCodeBlob        },
1395   {CC"getThreadStackSize", CC"()J",                   (void*)&WB_GetThreadStackSize },
1396   {CC"getThreadRemainingStackSize", CC"()J",          (void*)&WB_GetThreadRemainingStackSize },
1397   {CC"assertMatchingSafepointCalls", CC"(ZZ)V",       (void*)&WB_AssertMatchingSafepointCalls },
1398   {CC"isMonitorInflated",  CC"(Ljava/lang/Object;)Z", (void*)&WB_IsMonitorInflated  },
1399   {CC"forceSafepoint",     CC"()V",                   (void*)&WB_ForceSafepoint     },
1400   {CC"getMethodBooleanOption",
1401       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Boolean;",
1402                                                       (void*)&WB_GetMethodBooleaneOption},
1403   {CC"getMethodIntxOption",
1404       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
1405                                                       (void*)&WB_GetMethodIntxOption},
1406   {CC"getMethodUintxOption",
1407       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Long;",
1408                                                       (void*)&WB_GetMethodUintxOption},
1409   {CC"getMethodDoubleOption",
1410       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Double;",
1411                                                       (void*)&WB_GetMethodDoubleOption},
1412   {CC"getMethodStringOption",
1413       CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/String;",
1414                                                       (void*)&WB_GetMethodStringOption},
1415 };
1416 
1417 #undef CC
1418 
1419 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
1420   {
1421     if (WhiteBoxAPI) {
1422       // Make sure that wbclass is loaded by the null classloader
1423       instanceKlassHandle ikh = instanceKlassHandle(JNIHandles::resolve(wbclass)->klass());
1424       Handle loader(ikh->class_loader());
1425       if (loader.is_null()) {
1426         WhiteBox::register_methods(env, wbclass, thread, methods, sizeof(methods) / sizeof(methods[0]));
1427         WhiteBox::register_extended(env, wbclass, thread);
1428         WhiteBox::set_used();
1429       }
1430     }
1431   }
1432 JVM_END
--- EOF ---