rev 5901 : 8032970: Add stack size check methods to WhiteBox API
Reviewed-by:
Contributed-by: kirill.shirokov@oracle.com

   1 /*
   2  * Copyright (c) 2012, 2013, 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 "memory/universe.hpp"
  28 #include "oops/oop.inline.hpp"
  29 
  30 #include "classfile/symbolTable.hpp"
  31 #include "classfile/classLoaderData.hpp"
  32 
  33 #include "prims/whitebox.hpp"
  34 #include "prims/wbtestmethods/parserTests.hpp"
  35 
  36 #include "runtime/thread.hpp"
  37 #include "runtime/arguments.hpp"
  38 #include "runtime/interfaceSupport.hpp"
  39 #include "runtime/os.hpp"
  40 
  41 #include "utilities/debug.hpp"
  42 #include "utilities/macros.hpp"
  43 #include "utilities/exceptions.hpp"
  44 
  45 #if INCLUDE_ALL_GCS
  46 #include "gc_implementation/g1/concurrentMark.hpp"
  47 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  48 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  49 #endif // INCLUDE_ALL_GCS
  50 
  51 #ifdef INCLUDE_NMT
  52 #include "services/memTracker.hpp"
  53 #endif // INCLUDE_NMT
  54 
  55 #include "compiler/compileBroker.hpp"
  56 #include "runtime/compilationPolicy.hpp"
  57 
  58 #define SIZE_T_MAX_VALUE ((size_t) -1)
  59 
  60 bool WhiteBox::_used = false;
  61 
  62 WB_ENTRY(jlong, WB_GetObjectAddress(JNIEnv* env, jobject o, jobject obj))
  63   return (jlong)(void*)JNIHandles::resolve(obj);
  64 WB_END
  65 
  66 WB_ENTRY(jint, WB_GetHeapOopSize(JNIEnv* env, jobject o))
  67   return heapOopSize;
  68 WB_END
  69 
  70 
  71 class WBIsKlassAliveClosure : public KlassClosure {
  72     Symbol* _name;
  73     bool _found;
  74 public:
  75     WBIsKlassAliveClosure(Symbol* name) : _name(name), _found(false) {}
  76 
  77     void do_klass(Klass* k) {
  78       if (_found) return;
  79       Symbol* ksym = k->name();
  80       if (ksym->fast_compare(_name) == 0) {
  81         _found = true;
  82       }
  83     }
  84 
  85     bool found() const {
  86         return _found;
  87     }
  88 };
  89 
  90 WB_ENTRY(jboolean, WB_IsClassAlive(JNIEnv* env, jobject target, jstring name))
  91   Handle h_name = JNIHandles::resolve(name);
  92   if (h_name.is_null()) return false;
  93   Symbol* sym = java_lang_String::as_symbol(h_name, CHECK_false);
  94   TempNewSymbol tsym(sym); // Make sure to decrement reference count on sym on return
  95 
  96   WBIsKlassAliveClosure closure(sym);
  97   ClassLoaderDataGraph::classes_do(&closure);
  98 
  99   return closure.found();
 100 WB_END
 101 
 102 WB_ENTRY(jlong, WB_GetCompressedOopsMaxHeapSize(JNIEnv* env, jobject o)) {
 103   return (jlong)Arguments::max_heap_for_compressed_oops();
 104 }
 105 WB_END
 106 
 107 WB_ENTRY(void, WB_PrintHeapSizes(JNIEnv* env, jobject o)) {
 108   CollectorPolicy * p = Universe::heap()->collector_policy();
 109   gclog_or_tty->print_cr("Minimum heap "SIZE_FORMAT" Initial heap "
 110     SIZE_FORMAT" Maximum heap "SIZE_FORMAT" Min alignment "SIZE_FORMAT" Max alignment "SIZE_FORMAT,
 111     p->min_heap_byte_size(), p->initial_heap_byte_size(), p->max_heap_byte_size(),
 112     p->space_alignment(), p->heap_alignment());
 113 }
 114 WB_END
 115 
 116 #ifndef PRODUCT
 117 // Forward declaration
 118 void TestReservedSpace_test();
 119 void TestReserveMemorySpecial_test();
 120 void TestVirtualSpace_test();
 121 void TestMetaspaceAux_test();
 122 #endif
 123 
 124 WB_ENTRY(void, WB_RunMemoryUnitTests(JNIEnv* env, jobject o))
 125 #ifndef PRODUCT
 126   TestReservedSpace_test();
 127   TestReserveMemorySpecial_test();
 128   TestVirtualSpace_test();
 129   TestMetaspaceAux_test();
 130 #endif
 131 WB_END
 132 
 133 WB_ENTRY(void, WB_ReadFromNoaccessArea(JNIEnv* env, jobject o))
 134   size_t granularity = os::vm_allocation_granularity();
 135   ReservedHeapSpace rhs(100 * granularity, granularity, false, NULL);
 136   VirtualSpace vs;
 137   vs.initialize(rhs, 50 * granularity);
 138 
 139   //Check if constraints are complied
 140   if (!( UseCompressedOops && rhs.base() != NULL &&
 141          Universe::narrow_oop_base() != NULL &&
 142          Universe::narrow_oop_use_implicit_null_checks() )) {
 143     tty->print_cr("WB_ReadFromNoaccessArea method is useless:\n "
 144                   "\tUseCompressedOops is %d\n"
 145                   "\trhs.base() is "PTR_FORMAT"\n"
 146                   "\tUniverse::narrow_oop_base() is "PTR_FORMAT"\n"
 147                   "\tUniverse::narrow_oop_use_implicit_null_checks() is %d",
 148                   UseCompressedOops,
 149                   rhs.base(),
 150                   Universe::narrow_oop_base(),
 151                   Universe::narrow_oop_use_implicit_null_checks());
 152     return;
 153   }
 154   tty->print_cr("Reading from no access area... ");
 155   tty->print_cr("*(vs.low_boundary() - rhs.noaccess_prefix() / 2 ) = %c",
 156                 *(vs.low_boundary() - rhs.noaccess_prefix() / 2 ));
 157 WB_END
 158 
 159 static jint wb_stress_virtual_space_resize(size_t reserved_space_size,
 160                                            size_t magnitude, size_t iterations) {
 161   size_t granularity = os::vm_allocation_granularity();
 162   ReservedHeapSpace rhs(reserved_space_size * granularity, granularity, false, NULL);
 163   VirtualSpace vs;
 164   if (!vs.initialize(rhs, 0)) {
 165     tty->print_cr("Failed to initialize VirtualSpace. Can't proceed.");
 166     return 3;
 167   }
 168 
 169   long seed = os::random();
 170   tty->print_cr("Random seed is %ld", seed);
 171   os::init_random(seed);
 172 
 173   for (size_t i = 0; i < iterations; i++) {
 174 
 175     // Whether we will shrink or grow
 176     bool shrink = os::random() % 2L == 0;
 177 
 178     // Get random delta to resize virtual space
 179     size_t delta = (size_t)os::random() % magnitude;
 180 
 181     // If we are about to shrink virtual space below zero, then expand instead
 182     if (shrink && vs.committed_size() < delta) {
 183       shrink = false;
 184     }
 185 
 186     // Resizing by delta
 187     if (shrink) {
 188       vs.shrink_by(delta);
 189     } else {
 190       // If expanding fails expand_by will silently return false
 191       vs.expand_by(delta, true);
 192     }
 193   }
 194   return 0;
 195 }
 196 
 197 WB_ENTRY(jint, WB_StressVirtualSpaceResize(JNIEnv* env, jobject o,
 198         jlong reserved_space_size, jlong magnitude, jlong iterations))
 199   tty->print_cr("reservedSpaceSize="JLONG_FORMAT", magnitude="JLONG_FORMAT", "
 200                 "iterations="JLONG_FORMAT"\n", reserved_space_size, magnitude,
 201                 iterations);
 202   if (reserved_space_size < 0 || magnitude < 0 || iterations < 0) {
 203     tty->print_cr("One of variables printed above is negative. Can't proceed.\n");
 204     return 1;
 205   }
 206 
 207   // sizeof(size_t) depends on whether OS is 32bit or 64bit. sizeof(jlong) is
 208   // always 8 byte. That's why we should avoid overflow in case of 32bit platform.
 209   if (sizeof(size_t) < sizeof(jlong)) {
 210     jlong size_t_max_value = (jlong) SIZE_T_MAX_VALUE;
 211     if (reserved_space_size > size_t_max_value || magnitude > size_t_max_value
 212         || iterations > size_t_max_value) {
 213       tty->print_cr("One of variables printed above overflows size_t. Can't proceed.\n");
 214       return 2;
 215     }
 216   }
 217 
 218   return wb_stress_virtual_space_resize((size_t) reserved_space_size,
 219                                         (size_t) magnitude, (size_t) iterations);
 220 WB_END
 221 
 222 #if INCLUDE_ALL_GCS
 223 WB_ENTRY(jboolean, WB_G1IsHumongous(JNIEnv* env, jobject o, jobject obj))
 224   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 225   oop result = JNIHandles::resolve(obj);
 226   const HeapRegion* hr = g1->heap_region_containing(result);
 227   return hr->isHumongous();
 228 WB_END
 229 
 230 WB_ENTRY(jlong, WB_G1NumFreeRegions(JNIEnv* env, jobject o))
 231   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 232   size_t nr = g1->free_regions();
 233   return (jlong)nr;
 234 WB_END
 235 
 236 WB_ENTRY(jboolean, WB_G1InConcurrentMark(JNIEnv* env, jobject o))
 237   G1CollectedHeap* g1 = G1CollectedHeap::heap();
 238   ConcurrentMark* cm = g1->concurrent_mark();
 239   return cm->concurrent_marking_in_progress();
 240 WB_END
 241 
 242 WB_ENTRY(jint, WB_G1RegionSize(JNIEnv* env, jobject o))
 243   return (jint)HeapRegion::GrainBytes;
 244 WB_END
 245 #endif // INCLUDE_ALL_GCS
 246 
 247 #if INCLUDE_NMT
 248 // Alloc memory using the test memory type so that we can use that to see if
 249 // NMT picks it up correctly
 250 WB_ENTRY(jlong, WB_NMTMalloc(JNIEnv* env, jobject o, jlong size))
 251   jlong addr = 0;
 252 
 253   if (MemTracker::is_on() && !MemTracker::shutdown_in_progress()) {
 254     addr = (jlong)(uintptr_t)os::malloc(size, mtTest);
 255   }
 256 
 257   return addr;
 258 WB_END
 259 
 260 // Free the memory allocated by NMTAllocTest
 261 WB_ENTRY(void, WB_NMTFree(JNIEnv* env, jobject o, jlong mem))
 262   os::free((void*)(uintptr_t)mem, mtTest);
 263 WB_END
 264 
 265 WB_ENTRY(jlong, WB_NMTReserveMemory(JNIEnv* env, jobject o, jlong size))
 266   jlong addr = 0;
 267 
 268   if (MemTracker::is_on() && !MemTracker::shutdown_in_progress()) {
 269     addr = (jlong)(uintptr_t)os::reserve_memory(size);
 270     MemTracker::record_virtual_memory_type((address)addr, mtTest);
 271   }
 272 
 273   return addr;
 274 WB_END
 275 
 276 
 277 WB_ENTRY(void, WB_NMTCommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 278   os::commit_memory((char *)(uintptr_t)addr, size, !ExecMem);
 279   MemTracker::record_virtual_memory_type((address)(uintptr_t)addr, mtTest);
 280 WB_END
 281 
 282 WB_ENTRY(void, WB_NMTUncommitMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 283   os::uncommit_memory((char *)(uintptr_t)addr, size);
 284 WB_END
 285 
 286 WB_ENTRY(void, WB_NMTReleaseMemory(JNIEnv* env, jobject o, jlong addr, jlong size))
 287   os::release_memory((char *)(uintptr_t)addr, size);
 288 WB_END
 289 
 290 // Block until the current generation of NMT data to be merged, used to reliably test the NMT feature
 291 WB_ENTRY(jboolean, WB_NMTWaitForDataMerge(JNIEnv* env))
 292 
 293   if (!MemTracker::is_on() || MemTracker::shutdown_in_progress()) {
 294     return false;
 295   }
 296 
 297   return MemTracker::wbtest_wait_for_data_merge();
 298 WB_END
 299 
 300 WB_ENTRY(jboolean, WB_NMTIsDetailSupported(JNIEnv* env))
 301   return MemTracker::tracking_level() == MemTracker::NMT_detail;
 302 WB_END
 303 
 304 #endif // INCLUDE_NMT
 305 
 306 static jmethodID reflected_method_to_jmid(JavaThread* thread, JNIEnv* env, jobject method) {
 307   assert(method != NULL, "method should not be null");
 308   ThreadToNativeFromVM ttn(thread);
 309   return env->FromReflectedMethod(method);
 310 }
 311 
 312 WB_ENTRY(void, WB_DeoptimizeAll(JNIEnv* env, jobject o))
 313   MutexLockerEx mu(Compile_lock);
 314   CodeCache::mark_all_nmethods_for_deoptimization();
 315   VM_Deoptimize op;
 316   VMThread::execute(&op);
 317 WB_END
 318 
 319 WB_ENTRY(jint, WB_DeoptimizeMethod(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 320   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 321   MutexLockerEx mu(Compile_lock);
 322   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 323   int result = 0;
 324   nmethod* code;
 325   if (is_osr) {
 326     int bci = InvocationEntryBci;
 327     while ((code = mh->lookup_osr_nmethod_for(bci, CompLevel_none, false)) != NULL) {
 328       code->mark_for_deoptimization();
 329       ++result;
 330       bci = code->osr_entry_bci() + 1;
 331     }
 332   } else {
 333     code = mh->code();
 334   }
 335   if (code != NULL) {
 336     code->mark_for_deoptimization();
 337     ++result;
 338   }
 339   result += CodeCache::mark_for_deoptimization(mh());
 340   if (result > 0) {
 341     VM_Deoptimize op;
 342     VMThread::execute(&op);
 343   }
 344   return result;
 345 WB_END
 346 
 347 WB_ENTRY(jboolean, WB_IsMethodCompiled(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 348   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 349   MutexLockerEx mu(Compile_lock);
 350   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 351   nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 352   if (code == NULL) {
 353     return JNI_FALSE;
 354   }
 355   return (code->is_alive() && !code->is_marked_for_deoptimization());
 356 WB_END
 357 
 358 WB_ENTRY(jboolean, WB_IsMethodCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 359   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 360   MutexLockerEx mu(Compile_lock);
 361   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 362   if (is_osr) {
 363     return CompilationPolicy::can_be_osr_compiled(mh, comp_level);
 364   } else {
 365     return CompilationPolicy::can_be_compiled(mh, comp_level);
 366   }
 367 WB_END
 368 
 369 WB_ENTRY(jboolean, WB_IsMethodQueuedForCompilation(JNIEnv* env, jobject o, jobject method))
 370   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 371   MutexLockerEx mu(Compile_lock);
 372   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 373   return mh->queued_for_compilation();
 374 WB_END
 375 
 376 WB_ENTRY(jint, WB_GetMethodCompilationLevel(JNIEnv* env, jobject o, jobject method, jboolean is_osr))
 377   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 378   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 379   nmethod* code = is_osr ? mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false) : mh->code();
 380   return (code != NULL ? code->comp_level() : CompLevel_none);
 381 WB_END
 382 
 383 WB_ENTRY(void, WB_MakeMethodNotCompilable(JNIEnv* env, jobject o, jobject method, jint comp_level, jboolean is_osr))
 384   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 385   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 386   if (is_osr) {
 387     mh->set_not_osr_compilable(comp_level, true /* report */, "WhiteBox");
 388   } else {
 389     mh->set_not_compilable(comp_level, true /* report */, "WhiteBox");
 390   }
 391 WB_END
 392 
 393 WB_ENTRY(jint, WB_GetMethodEntryBci(JNIEnv* env, jobject o, jobject method))
 394   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 395   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 396   nmethod* code = mh->lookup_osr_nmethod_for(InvocationEntryBci, CompLevel_none, false);
 397   return (code != NULL && code->is_osr_method() ? code->osr_entry_bci() : InvocationEntryBci);
 398 WB_END
 399 
 400 WB_ENTRY(jboolean, WB_TestSetDontInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 401   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 402   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 403   bool result = mh->dont_inline();
 404   mh->set_dont_inline(value == JNI_TRUE);
 405   return result;
 406 WB_END
 407 
 408 WB_ENTRY(jint, WB_GetCompileQueueSize(JNIEnv* env, jobject o, jint comp_level))
 409   if (comp_level == CompLevel_any) {
 410     return CompileBroker::queue_size(CompLevel_full_optimization) /* C2 */ +
 411         CompileBroker::queue_size(CompLevel_full_profile) /* C1 */;
 412   } else {
 413     return CompileBroker::queue_size(comp_level);
 414   }
 415 WB_END
 416 
 417 WB_ENTRY(jboolean, WB_TestSetForceInlineMethod(JNIEnv* env, jobject o, jobject method, jboolean value))
 418   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 419   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 420   bool result = mh->force_inline();
 421   mh->set_force_inline(value == JNI_TRUE);
 422   return result;
 423 WB_END
 424 
 425 WB_ENTRY(jboolean, WB_EnqueueMethodForCompilation(JNIEnv* env, jobject o, jobject method, jint comp_level, jint bci))
 426   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 427   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 428   nmethod* nm = CompileBroker::compile_method(mh, bci, comp_level, mh, mh->invocation_count(), "WhiteBox", THREAD);
 429   MutexLockerEx mu(Compile_lock);
 430   return (mh->queued_for_compilation() || nm != NULL);
 431 WB_END
 432 
 433 WB_ENTRY(void, WB_ClearMethodState(JNIEnv* env, jobject o, jobject method))
 434   jmethodID jmid = reflected_method_to_jmid(thread, env, method);
 435   methodHandle mh(THREAD, Method::checked_resolve_jmethod_id(jmid));
 436   MutexLockerEx mu(Compile_lock);
 437   MethodData* mdo = mh->method_data();
 438   MethodCounters* mcs = mh->method_counters();
 439 
 440   if (mdo != NULL) {
 441     mdo->init();
 442     ResourceMark rm;
 443     int arg_count = mdo->method()->size_of_parameters();
 444     for (int i = 0; i < arg_count; i++) {
 445       mdo->set_arg_modified(i, 0);
 446     }
 447   }
 448 
 449   mh->clear_not_c1_compilable();
 450   mh->clear_not_c2_compilable();
 451   mh->clear_not_c2_osr_compilable();
 452   NOT_PRODUCT(mh->set_compiled_invocation_count(0));
 453   if (mcs != NULL) {
 454     mcs->backedge_counter()->init();
 455     mcs->invocation_counter()->init();
 456     mcs->set_interpreter_invocation_count(0);
 457     mcs->set_interpreter_throwout_count(0);
 458 
 459 #ifdef TIERED
 460     mcs->set_rate(0.0F);
 461     mh->set_prev_event_count(0, THREAD);
 462     mh->set_prev_time(0, THREAD);
 463 #endif
 464   }
 465 WB_END
 466 
 467 WB_ENTRY(jboolean, WB_IsInStringTable(JNIEnv* env, jobject o, jstring javaString))
 468   ResourceMark rm(THREAD);
 469   int len;
 470   jchar* name = java_lang_String::as_unicode_string(JNIHandles::resolve(javaString), len, CHECK_false);
 471   return (StringTable::lookup(name, len) != NULL);
 472 WB_END
 473 
 474 WB_ENTRY(void, WB_FullGC(JNIEnv* env, jobject o))
 475   Universe::heap()->collector_policy()->set_should_clear_all_soft_refs(true);
 476   Universe::heap()->collect(GCCause::_last_ditch_collection);
 477 WB_END
 478 
 479 
 480 WB_ENTRY(void, WB_ReadReservedMemory(JNIEnv* env, jobject o))
 481   // static+volatile in order to force the read to happen
 482   // (not be eliminated by the compiler)
 483   static char c;
 484   static volatile char* p;
 485 
 486   p = os::reserve_memory(os::vm_allocation_granularity(), NULL, 0);
 487   if (p == NULL) {
 488     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Failed to reserve memory");
 489   }
 490 
 491   c = *p;
 492 WB_END
 493 
 494 WB_ENTRY(jlong, WB_GetThreadFullStackSize(JNIEnv* env, jobject o))
 495   return (jlong) Thread::current()->stack_size();
 496 WB_END
 497 
 498 WB_ENTRY(jlong, WB_GetThreadRemainingStackSize(JNIEnv* env, jobject o))
 499   JavaThread* t = JavaThread::current();
 500   return (jlong) ( (ptrdiff_t) t->stack_available(os::current_stack_pointer()) - StackShadowPages * os::vm_page_size() );
 501 WB_END
 502 
 503 //Some convenience methods to deal with objects from java
 504 int WhiteBox::offset_for_field(const char* field_name, oop object,
 505     Symbol* signature_symbol) {
 506   assert(field_name != NULL && strlen(field_name) > 0, "Field name not valid");
 507   Thread* THREAD = Thread::current();
 508 
 509   //Get the class of our object
 510   Klass* arg_klass = object->klass();
 511   //Turn it into an instance-klass
 512   InstanceKlass* ik = InstanceKlass::cast(arg_klass);
 513 
 514   //Create symbols to look for in the class
 515   TempNewSymbol name_symbol = SymbolTable::lookup(field_name, (int) strlen(field_name),
 516       THREAD);
 517 
 518   //To be filled in with an offset of the field we're looking for
 519   fieldDescriptor fd;
 520 
 521   Klass* res = ik->find_field(name_symbol, signature_symbol, &fd);
 522   if (res == NULL) {
 523     tty->print_cr("Invalid layout of %s at %s", ik->external_name(),
 524         name_symbol->as_C_string());
 525     fatal("Invalid layout of preloaded class");
 526   }
 527 
 528   //fetch the field at the offset we've found
 529   int dest_offset = fd.offset();
 530 
 531   return dest_offset;
 532 }
 533 
 534 
 535 const char* WhiteBox::lookup_jstring(const char* field_name, oop object) {
 536   int offset = offset_for_field(field_name, object,
 537       vmSymbols::string_signature());
 538   oop string = object->obj_field(offset);
 539   if (string == NULL) {
 540     return NULL;
 541   }
 542   const char* ret = java_lang_String::as_utf8_string(string);
 543   return ret;
 544 }
 545 
 546 bool WhiteBox::lookup_bool(const char* field_name, oop object) {
 547   int offset =
 548       offset_for_field(field_name, object, vmSymbols::bool_signature());
 549   bool ret = (object->bool_field(offset) == JNI_TRUE);
 550   return ret;
 551 }
 552 
 553 
 554 #define CC (char*)
 555 
 556 static JNINativeMethod methods[] = {
 557   {CC"getObjectAddress",   CC"(Ljava/lang/Object;)J", (void*)&WB_GetObjectAddress  },
 558   {CC"getHeapOopSize",     CC"()I",                   (void*)&WB_GetHeapOopSize    },
 559   {CC"isClassAlive0",      CC"(Ljava/lang/String;)Z", (void*)&WB_IsClassAlive      },
 560   {CC"parseCommandLine",
 561       CC"(Ljava/lang/String;[Lsun/hotspot/parser/DiagnosticCommand;)[Ljava/lang/Object;",
 562       (void*) &WB_ParseCommandLine
 563   },
 564   {CC"getCompressedOopsMaxHeapSize", CC"()J",
 565       (void*)&WB_GetCompressedOopsMaxHeapSize},
 566   {CC"printHeapSizes",     CC"()V",                   (void*)&WB_PrintHeapSizes    },
 567   {CC"runMemoryUnitTests", CC"()V",                   (void*)&WB_RunMemoryUnitTests},
 568   {CC"readFromNoaccessArea",CC"()V",                  (void*)&WB_ReadFromNoaccessArea},
 569   {CC"stressVirtualSpaceResize",CC"(JJJ)I",           (void*)&WB_StressVirtualSpaceResize},
 570 #if INCLUDE_ALL_GCS
 571   {CC"g1InConcurrentMark", CC"()Z",                   (void*)&WB_G1InConcurrentMark},
 572   {CC"g1IsHumongous",      CC"(Ljava/lang/Object;)Z", (void*)&WB_G1IsHumongous     },
 573   {CC"g1NumFreeRegions",   CC"()J",                   (void*)&WB_G1NumFreeRegions  },
 574   {CC"g1RegionSize",       CC"()I",                   (void*)&WB_G1RegionSize      },
 575 #endif // INCLUDE_ALL_GCS
 576 #if INCLUDE_NMT
 577   {CC"NMTMalloc",           CC"(J)J",                 (void*)&WB_NMTMalloc          },
 578   {CC"NMTFree",             CC"(J)V",                 (void*)&WB_NMTFree            },
 579   {CC"NMTReserveMemory",    CC"(J)J",                 (void*)&WB_NMTReserveMemory   },
 580   {CC"NMTCommitMemory",     CC"(JJ)V",                (void*)&WB_NMTCommitMemory    },
 581   {CC"NMTUncommitMemory",   CC"(JJ)V",                (void*)&WB_NMTUncommitMemory  },
 582   {CC"NMTReleaseMemory",    CC"(JJ)V",                (void*)&WB_NMTReleaseMemory   },
 583   {CC"NMTWaitForDataMerge", CC"()Z",                  (void*)&WB_NMTWaitForDataMerge},
 584   {CC"NMTIsDetailSupported",CC"()Z",                  (void*)&WB_NMTIsDetailSupported},
 585 #endif // INCLUDE_NMT
 586   {CC"deoptimizeAll",      CC"()V",                   (void*)&WB_DeoptimizeAll     },
 587   {CC"deoptimizeMethod",   CC"(Ljava/lang/reflect/Executable;Z)I",
 588                                                       (void*)&WB_DeoptimizeMethod  },
 589   {CC"isMethodCompiled",   CC"(Ljava/lang/reflect/Executable;Z)Z",
 590                                                       (void*)&WB_IsMethodCompiled  },
 591   {CC"isMethodCompilable", CC"(Ljava/lang/reflect/Executable;IZ)Z",
 592                                                       (void*)&WB_IsMethodCompilable},
 593   {CC"isMethodQueuedForCompilation",
 594       CC"(Ljava/lang/reflect/Executable;)Z",          (void*)&WB_IsMethodQueuedForCompilation},
 595   {CC"makeMethodNotCompilable",
 596       CC"(Ljava/lang/reflect/Executable;IZ)V",        (void*)&WB_MakeMethodNotCompilable},
 597   {CC"testSetDontInlineMethod",
 598       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetDontInlineMethod},
 599   {CC"getMethodCompilationLevel",
 600       CC"(Ljava/lang/reflect/Executable;Z)I",         (void*)&WB_GetMethodCompilationLevel},
 601   {CC"getMethodEntryBci",
 602       CC"(Ljava/lang/reflect/Executable;)I",          (void*)&WB_GetMethodEntryBci},
 603   {CC"getCompileQueueSize",
 604       CC"(I)I",                                       (void*)&WB_GetCompileQueueSize},
 605   {CC"testSetForceInlineMethod",
 606       CC"(Ljava/lang/reflect/Executable;Z)Z",         (void*)&WB_TestSetForceInlineMethod},
 607   {CC"enqueueMethodForCompilation",
 608       CC"(Ljava/lang/reflect/Executable;II)Z",        (void*)&WB_EnqueueMethodForCompilation},
 609   {CC"clearMethodState",
 610       CC"(Ljava/lang/reflect/Executable;)V",          (void*)&WB_ClearMethodState},
 611   {CC"isInStringTable",   CC"(Ljava/lang/String;)Z",  (void*)&WB_IsInStringTable  },
 612   {CC"fullGC",   CC"()V",                             (void*)&WB_FullGC },
 613   {CC"readReservedMemory", CC"()V",                   (void*)&WB_ReadReservedMemory },
 614   {CC"getThreadFullStackSize", CC"()J",               (void*)&WB_GetThreadFullStackSize },
 615   {CC"getThreadRemainingStackSize", CC"()J",          (void*)&WB_GetThreadRemainingStackSize },
 616 };
 617 
 618 #undef CC
 619 
 620 JVM_ENTRY(void, JVM_RegisterWhiteBoxMethods(JNIEnv* env, jclass wbclass))
 621   {
 622     if (WhiteBoxAPI) {
 623       // Make sure that wbclass is loaded by the null classloader
 624       instanceKlassHandle ikh = instanceKlassHandle(JNIHandles::resolve(wbclass)->klass());
 625       Handle loader(ikh->class_loader());
 626       if (loader.is_null()) {
 627         ResourceMark rm;
 628         ThreadToNativeFromVM ttnfv(thread); // can't be in VM when we call JNI
 629         bool result = true;
 630         //  one by one registration natives for exception catching
 631         jclass exceptionKlass = env->FindClass(vmSymbols::java_lang_NoSuchMethodError()->as_C_string());
 632         for (int i = 0, n = sizeof(methods) / sizeof(methods[0]); i < n; ++i) {
 633           if (env->RegisterNatives(wbclass, methods + i, 1) != 0) {
 634             result = false;
 635             if (env->ExceptionCheck() && env->IsInstanceOf(env->ExceptionOccurred(), exceptionKlass)) {
 636               // j.l.NoSuchMethodError is thrown when a method can't be found or a method is not native
 637               // ignoring the exception
 638               tty->print_cr("Warning: 'NoSuchMethodError' on register of sun.hotspot.WhiteBox::%s%s", methods[i].name, methods[i].signature);
 639               env->ExceptionClear();
 640             } else {
 641               // register is failed w/o exception or w/ unexpected exception
 642               tty->print_cr("Warning: unexpected error on register of sun.hotspot.WhiteBox::%s%s. All methods will be unregistered", methods[i].name, methods[i].signature);
 643               env->UnregisterNatives(wbclass);
 644               break;
 645             }
 646           }
 647         }
 648 
 649         if (result) {
 650           WhiteBox::set_used();
 651         }
 652       }
 653     }
 654   }
 655 JVM_END
--- EOF ---