1 /*
   2  * Copyright 1997-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 # include "incls/_precompiled.incl"
  26 # include "incls/_nmethod.cpp.incl"
  27 
  28 #ifdef DTRACE_ENABLED
  29 
  30 
  31 // Only bother with this argument setup if dtrace is available
  32 
  33 HS_DTRACE_PROBE_DECL8(hotspot, compiled__method__load,
  34   const char*, int, const char*, int, const char*, int, void*, size_t);
  35 
  36 HS_DTRACE_PROBE_DECL6(hotspot, compiled__method__unload,
  37   char*, int, char*, int, char*, int);
  38 
  39 #define DTRACE_METHOD_UNLOAD_PROBE(method)                                \
  40   {                                                                       \
  41     methodOop m = (method);                                               \
  42     if (m != NULL) {                                                      \
  43       symbolOop klass_name = m->klass_name();                             \
  44       symbolOop name = m->name();                                         \
  45       symbolOop signature = m->signature();                               \
  46       HS_DTRACE_PROBE6(hotspot, compiled__method__unload,                 \
  47         klass_name->bytes(), klass_name->utf8_length(),                   \
  48         name->bytes(), name->utf8_length(),                               \
  49         signature->bytes(), signature->utf8_length());                    \
  50     }                                                                     \
  51   }
  52 
  53 #else //  ndef DTRACE_ENABLED
  54 
  55 #define DTRACE_METHOD_UNLOAD_PROBE(method)
  56 
  57 #endif
  58 
  59 bool nmethod::is_compiled_by_c1() const {
  60   if (is_native_method()) return false;
  61   assert(compiler() != NULL, "must be");
  62   return compiler()->is_c1();
  63 }
  64 bool nmethod::is_compiled_by_c2() const {
  65   if (is_native_method()) return false;
  66   assert(compiler() != NULL, "must be");
  67   return compiler()->is_c2();
  68 }
  69 
  70 
  71 
  72 //---------------------------------------------------------------------------------
  73 // NMethod statistics
  74 // They are printed under various flags, including:
  75 //   PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation.
  76 // (In the latter two cases, they like other stats are printed to the log only.)
  77 
  78 #ifndef PRODUCT
  79 // These variables are put into one block to reduce relocations
  80 // and make it simpler to print from the debugger.
  81 static
  82 struct nmethod_stats_struct {
  83   int nmethod_count;
  84   int total_size;
  85   int relocation_size;
  86   int code_size;
  87   int stub_size;
  88   int consts_size;
  89   int scopes_data_size;
  90   int scopes_pcs_size;
  91   int dependencies_size;
  92   int handler_table_size;
  93   int nul_chk_table_size;
  94   int oops_size;
  95 
  96   void note_nmethod(nmethod* nm) {
  97     nmethod_count += 1;
  98     total_size          += nm->size();
  99     relocation_size     += nm->relocation_size();
 100     code_size           += nm->code_size();
 101     stub_size           += nm->stub_size();
 102     consts_size         += nm->consts_size();
 103     scopes_data_size    += nm->scopes_data_size();
 104     scopes_pcs_size     += nm->scopes_pcs_size();
 105     dependencies_size   += nm->dependencies_size();
 106     handler_table_size  += nm->handler_table_size();
 107     nul_chk_table_size  += nm->nul_chk_table_size();
 108     oops_size += nm->oops_size();
 109   }
 110   void print_nmethod_stats() {
 111     if (nmethod_count == 0)  return;
 112     tty->print_cr("Statistics for %d bytecoded nmethods:", nmethod_count);
 113     if (total_size != 0)          tty->print_cr(" total in heap  = %d", total_size);
 114     if (relocation_size != 0)     tty->print_cr(" relocation     = %d", relocation_size);
 115     if (code_size != 0)           tty->print_cr(" main code      = %d", code_size);
 116     if (stub_size != 0)           tty->print_cr(" stub code      = %d", stub_size);
 117     if (consts_size != 0)         tty->print_cr(" constants      = %d", consts_size);
 118     if (scopes_data_size != 0)    tty->print_cr(" scopes data    = %d", scopes_data_size);
 119     if (scopes_pcs_size != 0)     tty->print_cr(" scopes pcs     = %d", scopes_pcs_size);
 120     if (dependencies_size != 0)   tty->print_cr(" dependencies   = %d", dependencies_size);
 121     if (handler_table_size != 0)  tty->print_cr(" handler table  = %d", handler_table_size);
 122     if (nul_chk_table_size != 0)  tty->print_cr(" nul chk table  = %d", nul_chk_table_size);
 123     if (oops_size != 0)           tty->print_cr(" oops           = %d", oops_size);
 124   }
 125 
 126   int native_nmethod_count;
 127   int native_total_size;
 128   int native_relocation_size;
 129   int native_code_size;
 130   int native_oops_size;
 131   void note_native_nmethod(nmethod* nm) {
 132     native_nmethod_count += 1;
 133     native_total_size       += nm->size();
 134     native_relocation_size  += nm->relocation_size();
 135     native_code_size        += nm->code_size();
 136     native_oops_size        += nm->oops_size();
 137   }
 138   void print_native_nmethod_stats() {
 139     if (native_nmethod_count == 0)  return;
 140     tty->print_cr("Statistics for %d native nmethods:", native_nmethod_count);
 141     if (native_total_size != 0)       tty->print_cr(" N. total size  = %d", native_total_size);
 142     if (native_relocation_size != 0)  tty->print_cr(" N. relocation  = %d", native_relocation_size);
 143     if (native_code_size != 0)        tty->print_cr(" N. main code   = %d", native_code_size);
 144     if (native_oops_size != 0)        tty->print_cr(" N. oops        = %d", native_oops_size);
 145   }
 146 
 147   int pc_desc_resets;   // number of resets (= number of caches)
 148   int pc_desc_queries;  // queries to nmethod::find_pc_desc
 149   int pc_desc_approx;   // number of those which have approximate true
 150   int pc_desc_repeats;  // number of _last_pc_desc hits
 151   int pc_desc_hits;     // number of LRU cache hits
 152   int pc_desc_tests;    // total number of PcDesc examinations
 153   int pc_desc_searches; // total number of quasi-binary search steps
 154   int pc_desc_adds;     // number of LUR cache insertions
 155 
 156   void print_pc_stats() {
 157     tty->print_cr("PcDesc Statistics:  %d queries, %.2f comparisons per query",
 158                   pc_desc_queries,
 159                   (double)(pc_desc_tests + pc_desc_searches)
 160                   / pc_desc_queries);
 161     tty->print_cr("  caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d",
 162                   pc_desc_resets,
 163                   pc_desc_queries, pc_desc_approx,
 164                   pc_desc_repeats, pc_desc_hits,
 165                   pc_desc_tests, pc_desc_searches, pc_desc_adds);
 166   }
 167 } nmethod_stats;
 168 #endif //PRODUCT
 169 
 170 //---------------------------------------------------------------------------------
 171 
 172 
 173 // The _unwind_handler is a special marker address, which says that
 174 // for given exception oop and address, the frame should be removed
 175 // as the tuple cannot be caught in the nmethod
 176 address ExceptionCache::_unwind_handler = (address) -1;
 177 
 178 
 179 ExceptionCache::ExceptionCache(Handle exception, address pc, address handler) {
 180   assert(pc != NULL, "Must be non null");
 181   assert(exception.not_null(), "Must be non null");
 182   assert(handler != NULL, "Must be non null");
 183 
 184   _count = 0;
 185   _exception_type = exception->klass();
 186   _next = NULL;
 187 
 188   add_address_and_handler(pc,handler);
 189 }
 190 
 191 
 192 address ExceptionCache::match(Handle exception, address pc) {
 193   assert(pc != NULL,"Must be non null");
 194   assert(exception.not_null(),"Must be non null");
 195   if (exception->klass() == exception_type()) {
 196     return (test_address(pc));
 197   }
 198 
 199   return NULL;
 200 }
 201 
 202 
 203 bool ExceptionCache::match_exception_with_space(Handle exception) {
 204   assert(exception.not_null(),"Must be non null");
 205   if (exception->klass() == exception_type() && count() < cache_size) {
 206     return true;
 207   }
 208   return false;
 209 }
 210 
 211 
 212 address ExceptionCache::test_address(address addr) {
 213   for (int i=0; i<count(); i++) {
 214     if (pc_at(i) == addr) {
 215       return handler_at(i);
 216     }
 217   }
 218   return NULL;
 219 }
 220 
 221 
 222 bool ExceptionCache::add_address_and_handler(address addr, address handler) {
 223   if (test_address(addr) == handler) return true;
 224   if (count() < cache_size) {
 225     set_pc_at(count(),addr);
 226     set_handler_at(count(), handler);
 227     increment_count();
 228     return true;
 229   }
 230   return false;
 231 }
 232 
 233 
 234 // private method for handling exception cache
 235 // These methods are private, and used to manipulate the exception cache
 236 // directly.
 237 ExceptionCache* nmethod::exception_cache_entry_for_exception(Handle exception) {
 238   ExceptionCache* ec = exception_cache();
 239   while (ec != NULL) {
 240     if (ec->match_exception_with_space(exception)) {
 241       return ec;
 242     }
 243     ec = ec->next();
 244   }
 245   return NULL;
 246 }
 247 
 248 
 249 //-----------------------------------------------------------------------------
 250 
 251 
 252 // Helper used by both find_pc_desc methods.
 253 static inline bool match_desc(PcDesc* pc, int pc_offset, bool approximate) {
 254   NOT_PRODUCT(++nmethod_stats.pc_desc_tests);
 255   if (!approximate)
 256     return pc->pc_offset() == pc_offset;
 257   else
 258     return (pc-1)->pc_offset() < pc_offset && pc_offset <= pc->pc_offset();
 259 }
 260 
 261 void PcDescCache::reset_to(PcDesc* initial_pc_desc) {
 262   if (initial_pc_desc == NULL) {
 263     _last_pc_desc = NULL;  // native method
 264     return;
 265   }
 266   NOT_PRODUCT(++nmethod_stats.pc_desc_resets);
 267   // reset the cache by filling it with benign (non-null) values
 268   assert(initial_pc_desc->pc_offset() < 0, "must be sentinel");
 269   _last_pc_desc = initial_pc_desc + 1;  // first valid one is after sentinel
 270   for (int i = 0; i < cache_size; i++)
 271     _pc_descs[i] = initial_pc_desc;
 272 }
 273 
 274 PcDesc* PcDescCache::find_pc_desc(int pc_offset, bool approximate) {
 275   NOT_PRODUCT(++nmethod_stats.pc_desc_queries);
 276   NOT_PRODUCT(if (approximate)  ++nmethod_stats.pc_desc_approx);
 277 
 278   // In order to prevent race conditions do not load cache elements
 279   // repeatedly, but use a local copy:
 280   PcDesc* res;
 281 
 282   // Step one:  Check the most recently returned value.
 283   res = _last_pc_desc;
 284   if (res == NULL)  return NULL;  // native method; no PcDescs at all
 285   if (match_desc(res, pc_offset, approximate)) {
 286     NOT_PRODUCT(++nmethod_stats.pc_desc_repeats);
 287     return res;
 288   }
 289 
 290   // Step two:  Check the LRU cache.
 291   for (int i = 0; i < cache_size; i++) {
 292     res = _pc_descs[i];
 293     if (res->pc_offset() < 0)  break;  // optimization: skip empty cache
 294     if (match_desc(res, pc_offset, approximate)) {
 295       NOT_PRODUCT(++nmethod_stats.pc_desc_hits);
 296       _last_pc_desc = res;  // record this cache hit in case of repeat
 297       return res;
 298     }
 299   }
 300 
 301   // Report failure.
 302   return NULL;
 303 }
 304 
 305 void PcDescCache::add_pc_desc(PcDesc* pc_desc) {
 306   NOT_PRODUCT(++nmethod_stats.pc_desc_adds);
 307   // Update the LRU cache by shifting pc_desc forward:
 308   for (int i = 0; i < cache_size; i++)  {
 309     PcDesc* next = _pc_descs[i];
 310     _pc_descs[i] = pc_desc;
 311     pc_desc = next;
 312   }
 313   // Note:  Do not update _last_pc_desc.  It fronts for the LRU cache.
 314 }
 315 
 316 // adjust pcs_size so that it is a multiple of both oopSize and
 317 // sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple
 318 // of oopSize, then 2*sizeof(PcDesc) is)
 319 static int  adjust_pcs_size(int pcs_size) {
 320   int nsize = round_to(pcs_size,   oopSize);
 321   if ((nsize % sizeof(PcDesc)) != 0) {
 322     nsize = pcs_size + sizeof(PcDesc);
 323   }
 324   assert((nsize %  oopSize) == 0, "correct alignment");
 325   return nsize;
 326 }
 327 
 328 //-----------------------------------------------------------------------------
 329 
 330 
 331 void nmethod::add_exception_cache_entry(ExceptionCache* new_entry) {
 332   assert(ExceptionCache_lock->owned_by_self(),"Must hold the ExceptionCache_lock");
 333   assert(new_entry != NULL,"Must be non null");
 334   assert(new_entry->next() == NULL, "Must be null");
 335 
 336   if (exception_cache() != NULL) {
 337     new_entry->set_next(exception_cache());
 338   }
 339   set_exception_cache(new_entry);
 340 }
 341 
 342 void nmethod::remove_from_exception_cache(ExceptionCache* ec) {
 343   ExceptionCache* prev = NULL;
 344   ExceptionCache* curr = exception_cache();
 345   assert(curr != NULL, "nothing to remove");
 346   // find the previous and next entry of ec
 347   while (curr != ec) {
 348     prev = curr;
 349     curr = curr->next();
 350     assert(curr != NULL, "ExceptionCache not found");
 351   }
 352   // now: curr == ec
 353   ExceptionCache* next = curr->next();
 354   if (prev == NULL) {
 355     set_exception_cache(next);
 356   } else {
 357     prev->set_next(next);
 358   }
 359   delete curr;
 360 }
 361 
 362 
 363 // public method for accessing the exception cache
 364 // These are the public access methods.
 365 address nmethod::handler_for_exception_and_pc(Handle exception, address pc) {
 366   // We never grab a lock to read the exception cache, so we may
 367   // have false negatives. This is okay, as it can only happen during
 368   // the first few exception lookups for a given nmethod.
 369   ExceptionCache* ec = exception_cache();
 370   while (ec != NULL) {
 371     address ret_val;
 372     if ((ret_val = ec->match(exception,pc)) != NULL) {
 373       return ret_val;
 374     }
 375     ec = ec->next();
 376   }
 377   return NULL;
 378 }
 379 
 380 
 381 void nmethod::add_handler_for_exception_and_pc(Handle exception, address pc, address handler) {
 382   // There are potential race conditions during exception cache updates, so we
 383   // must own the ExceptionCache_lock before doing ANY modifications. Because
 384   // we dont lock during reads, it is possible to have several threads attempt
 385   // to update the cache with the same data. We need to check for already inserted
 386   // copies of the current data before adding it.
 387 
 388   MutexLocker ml(ExceptionCache_lock);
 389   ExceptionCache* target_entry = exception_cache_entry_for_exception(exception);
 390 
 391   if (target_entry == NULL || !target_entry->add_address_and_handler(pc,handler)) {
 392     target_entry = new ExceptionCache(exception,pc,handler);
 393     add_exception_cache_entry(target_entry);
 394   }
 395 }
 396 
 397 
 398 //-------------end of code for ExceptionCache--------------
 399 
 400 
 401 void nmFlags::clear() {
 402   assert(sizeof(nmFlags) == sizeof(int), "using more than one word for nmFlags");
 403   *(jint*)this = 0;
 404 }
 405 
 406 int nmethod::total_size() const {
 407   return
 408     code_size()          +
 409     stub_size()          +
 410     consts_size()        +
 411     scopes_data_size()   +
 412     scopes_pcs_size()    +
 413     handler_table_size() +
 414     nul_chk_table_size();
 415 }
 416 
 417 const char* nmethod::compile_kind() const {
 418   if (method() == NULL)    return "unloaded";
 419   if (is_native_method())  return "c2n";
 420   if (is_osr_method())     return "osr";
 421   return NULL;
 422 }
 423 
 424 // %%% This variable is no longer used?
 425 int nmethod::_zombie_instruction_size = NativeJump::instruction_size;
 426 
 427 
 428 nmethod* nmethod::new_native_nmethod(methodHandle method,
 429   CodeBuffer *code_buffer,
 430   int vep_offset,
 431   int frame_complete,
 432   int frame_size,
 433   ByteSize basic_lock_owner_sp_offset,
 434   ByteSize basic_lock_sp_offset,
 435   OopMapSet* oop_maps) {
 436   // create nmethod
 437   nmethod* nm = NULL;
 438   {
 439     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 440     int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod));
 441     const int dummy = -1;               // Flag to force proper "operator new"
 442     CodeOffsets offsets;
 443     offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
 444     offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
 445     nm = new (native_nmethod_size)
 446       nmethod(method(), native_nmethod_size, &offsets,
 447               code_buffer, frame_size,
 448               basic_lock_owner_sp_offset, basic_lock_sp_offset,
 449               oop_maps);
 450     NOT_PRODUCT(if (nm != NULL)  nmethod_stats.note_native_nmethod(nm));
 451     if (PrintAssembly && nm != NULL)
 452       Disassembler::decode(nm);
 453   }
 454   // verify nmethod
 455   debug_only(if (nm) nm->verify();) // might block
 456 
 457   if (nm != NULL) {
 458     nm->log_new_nmethod();
 459   }
 460 
 461   return nm;
 462 }
 463 
 464 nmethod* nmethod::new_nmethod(methodHandle method,
 465   int compile_id,
 466   int entry_bci,
 467   CodeOffsets* offsets,
 468   int orig_pc_offset,
 469   DebugInformationRecorder* debug_info,
 470   Dependencies* dependencies,
 471   CodeBuffer* code_buffer, int frame_size,
 472   OopMapSet* oop_maps,
 473   ExceptionHandlerTable* handler_table,
 474   ImplicitExceptionTable* nul_chk_table,
 475   AbstractCompiler* compiler,
 476   int comp_level
 477 )
 478 {
 479   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
 480   // create nmethod
 481   nmethod* nm = NULL;
 482   { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 483     int nmethod_size =
 484       allocation_size(code_buffer, sizeof(nmethod))
 485       + adjust_pcs_size(debug_info->pcs_size())
 486       + round_to(dependencies->size_in_bytes() , oopSize)
 487       + round_to(handler_table->size_in_bytes(), oopSize)
 488       + round_to(nul_chk_table->size_in_bytes(), oopSize)
 489       + round_to(debug_info->data_size()       , oopSize);
 490     nm = new (nmethod_size)
 491       nmethod(method(), nmethod_size, compile_id, entry_bci, offsets,
 492               orig_pc_offset, debug_info, dependencies, code_buffer, frame_size,
 493               oop_maps,
 494               handler_table,
 495               nul_chk_table,
 496               compiler,
 497               comp_level);
 498     if (nm != NULL) {
 499       // To make dependency checking during class loading fast, record
 500       // the nmethod dependencies in the classes it is dependent on.
 501       // This allows the dependency checking code to simply walk the
 502       // class hierarchy above the loaded class, checking only nmethods
 503       // which are dependent on those classes.  The slow way is to
 504       // check every nmethod for dependencies which makes it linear in
 505       // the number of methods compiled.  For applications with a lot
 506       // classes the slow way is too slow.
 507       for (Dependencies::DepStream deps(nm); deps.next(); ) {
 508         klassOop klass = deps.context_type();
 509         if (klass == NULL)  continue;  // ignore things like evol_method
 510 
 511         // record this nmethod as dependent on this klass
 512         instanceKlass::cast(klass)->add_dependent_nmethod(nm);
 513       }
 514     }
 515     NOT_PRODUCT(if (nm != NULL)  nmethod_stats.note_nmethod(nm));
 516     if (PrintAssembly && nm != NULL)
 517       Disassembler::decode(nm);
 518   }
 519 
 520   // verify nmethod
 521   debug_only(if (nm) nm->verify();) // might block
 522 
 523   if (nm != NULL) {
 524     nm->log_new_nmethod();
 525   }
 526 
 527   // done
 528   return nm;
 529 }
 530 
 531 
 532 // For native wrappers
 533 nmethod::nmethod(
 534   methodOop method,
 535   int nmethod_size,
 536   CodeOffsets* offsets,
 537   CodeBuffer* code_buffer,
 538   int frame_size,
 539   ByteSize basic_lock_owner_sp_offset,
 540   ByteSize basic_lock_sp_offset,
 541   OopMapSet* oop_maps )
 542   : CodeBlob("native nmethod", code_buffer, sizeof(nmethod),
 543              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
 544   _compiled_synchronized_native_basic_lock_owner_sp_offset(basic_lock_owner_sp_offset),
 545   _compiled_synchronized_native_basic_lock_sp_offset(basic_lock_sp_offset)
 546 {
 547   {
 548     debug_only(No_Safepoint_Verifier nsv;)
 549     assert_locked_or_safepoint(CodeCache_lock);
 550 
 551     NOT_PRODUCT(_has_debug_info = false; )
 552     _method                  = method;
 553     _entry_bci               = InvocationEntryBci;
 554     _link                    = NULL;
 555     _compiler                = NULL;
 556     // We have no exception handler or deopt handler make the
 557     // values something that will never match a pc like the nmethod vtable entry
 558     _exception_offset        = 0;
 559     _deoptimize_offset       = 0;
 560     _orig_pc_offset          = 0;
 561     _stub_offset             = data_offset();
 562     _consts_offset           = data_offset();
 563     _scopes_data_offset      = data_offset();
 564     _scopes_pcs_offset       = _scopes_data_offset;
 565     _dependencies_offset     = _scopes_pcs_offset;
 566     _handler_table_offset    = _dependencies_offset;
 567     _nul_chk_table_offset    = _handler_table_offset;
 568     _nmethod_end_offset      = _nul_chk_table_offset;
 569     _compile_id              = 0;  // default
 570     _comp_level              = CompLevel_none;
 571     _entry_point             = instructions_begin();
 572     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
 573     _osr_entry_point         = NULL;
 574     _exception_cache         = NULL;
 575     _pc_desc_cache.reset_to(NULL);
 576 
 577     flags.clear();
 578     flags.state              = alive;
 579     _markedForDeoptimization = 0;
 580 
 581     _lock_count = 0;
 582     _stack_traversal_mark    = 0;
 583 
 584     code_buffer->copy_oops_to(this);
 585     debug_only(check_store();)
 586     CodeCache::commit(this);
 587     VTune::create_nmethod(this);
 588   }
 589 
 590   if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) {
 591     ttyLocker ttyl;  // keep the following output all in one block
 592     // This output goes directly to the tty, not the compiler log.
 593     // To enable tools to match it up with the compilation activity,
 594     // be sure to tag this tty output with the compile ID.
 595     if (xtty != NULL) {
 596       xtty->begin_head("print_native_nmethod");
 597       xtty->method(_method);
 598       xtty->stamp();
 599       xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
 600     }
 601     // print the header part first
 602     print();
 603     // then print the requested information
 604     if (PrintNativeNMethods) {
 605       print_code();
 606       oop_maps->print();
 607     }
 608     if (PrintRelocations) {
 609       print_relocations();
 610     }
 611     if (xtty != NULL) {
 612       xtty->tail("print_native_nmethod");
 613     }
 614   }
 615   Events::log("Create nmethod " INTPTR_FORMAT, this);
 616 }
 617 
 618 
 619 void* nmethod::operator new(size_t size, int nmethod_size) {
 620   // Always leave some room in the CodeCache for I2C/C2I adapters
 621   if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) return NULL;
 622   return CodeCache::allocate(nmethod_size);
 623 }
 624 
 625 
 626 nmethod::nmethod(
 627   methodOop method,
 628   int nmethod_size,
 629   int compile_id,
 630   int entry_bci,
 631   CodeOffsets* offsets,
 632   int orig_pc_offset,
 633   DebugInformationRecorder* debug_info,
 634   Dependencies* dependencies,
 635   CodeBuffer *code_buffer,
 636   int frame_size,
 637   OopMapSet* oop_maps,
 638   ExceptionHandlerTable* handler_table,
 639   ImplicitExceptionTable* nul_chk_table,
 640   AbstractCompiler* compiler,
 641   int comp_level
 642   )
 643   : CodeBlob("nmethod", code_buffer, sizeof(nmethod),
 644              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
 645   _compiled_synchronized_native_basic_lock_owner_sp_offset(in_ByteSize(-1)),
 646   _compiled_synchronized_native_basic_lock_sp_offset(in_ByteSize(-1))
 647 {
 648   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
 649   {
 650     debug_only(No_Safepoint_Verifier nsv;)
 651     assert_locked_or_safepoint(CodeCache_lock);
 652 
 653     NOT_PRODUCT(_has_debug_info = false; )
 654     _method                  = method;
 655     _compile_id              = compile_id;
 656     _comp_level              = comp_level;
 657     _entry_bci               = entry_bci;
 658     _link                    = NULL;
 659     _compiler                = compiler;
 660     _orig_pc_offset          = orig_pc_offset;
 661     _stub_offset             = instructions_offset() + code_buffer->total_offset_of(code_buffer->stubs()->start());
 662 
 663     // Exception handler and deopt handler are in the stub section
 664     _exception_offset        = _stub_offset + offsets->value(CodeOffsets::Exceptions);
 665     _deoptimize_offset       = _stub_offset + offsets->value(CodeOffsets::Deopt);
 666     _consts_offset           = instructions_offset() + code_buffer->total_offset_of(code_buffer->consts()->start());
 667     _scopes_data_offset      = data_offset();
 668     _scopes_pcs_offset       = _scopes_data_offset   + round_to(debug_info->data_size         (), oopSize);
 669     _dependencies_offset     = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
 670     _handler_table_offset    = _dependencies_offset  + round_to(dependencies->size_in_bytes (), oopSize);
 671     _nul_chk_table_offset    = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize);
 672     _nmethod_end_offset      = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize);
 673 
 674     _entry_point             = instructions_begin();
 675     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
 676     _osr_entry_point         = instructions_begin() + offsets->value(CodeOffsets::OSR_Entry);
 677     _exception_cache         = NULL;
 678     _pc_desc_cache.reset_to(scopes_pcs_begin());
 679 
 680     flags.clear();
 681     flags.state              = alive;
 682     _markedForDeoptimization = 0;
 683 
 684     _unload_reported         = false;           // jvmti state
 685 
 686     _lock_count = 0;
 687     _stack_traversal_mark    = 0;
 688 
 689     // Copy contents of ScopeDescRecorder to nmethod
 690     code_buffer->copy_oops_to(this);
 691     debug_info->copy_to(this);
 692     dependencies->copy_to(this);
 693     debug_only(check_store();)
 694 
 695     CodeCache::commit(this);
 696 
 697     VTune::create_nmethod(this);
 698 
 699     // Copy contents of ExceptionHandlerTable to nmethod
 700     handler_table->copy_to(this);
 701     nul_chk_table->copy_to(this);
 702 
 703     // we use the information of entry points to find out if a method is
 704     // static or non static
 705     assert(compiler->is_c2() ||
 706            _method->is_static() == (entry_point() == _verified_entry_point),
 707            " entry points must be same for static methods and vice versa");
 708   }
 709 
 710   bool printnmethods = PrintNMethods || CompilerOracle::has_option_string(_method, "PrintNMethods");
 711   if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) {
 712     print_nmethod(printnmethods);
 713   }
 714 
 715   // Note: Do not verify in here as the CodeCache_lock is
 716   //       taken which would conflict with the CompiledIC_lock
 717   //       which taken during the verification of call sites.
 718   //       (was bug - gri 10/25/99)
 719 
 720   Events::log("Create nmethod " INTPTR_FORMAT, this);
 721 }
 722 
 723 
 724 // Print a short set of xml attributes to identify this nmethod.  The
 725 // output should be embedded in some other element.
 726 void nmethod::log_identity(xmlStream* log) const {
 727   log->print(" compile_id='%d'", compile_id());
 728   const char* nm_kind = compile_kind();
 729   if (nm_kind != NULL)  log->print(" compile_kind='%s'", nm_kind);
 730   if (compiler() != NULL) {
 731     log->print(" compiler='%s'", compiler()->name());
 732   }
 733 #ifdef TIERED
 734   log->print(" level='%d'", comp_level());
 735 #endif // TIERED
 736 }
 737 
 738 
 739 #define LOG_OFFSET(log, name)                    \
 740   if ((intptr_t)name##_end() - (intptr_t)name##_begin()) \
 741     log->print(" " XSTR(name) "_offset='%d'"    , \
 742                (intptr_t)name##_begin() - (intptr_t)this)
 743 
 744 
 745 void nmethod::log_new_nmethod() const {
 746   if (LogCompilation && xtty != NULL) {
 747     ttyLocker ttyl;
 748     HandleMark hm;
 749     xtty->begin_elem("nmethod");
 750     log_identity(xtty);
 751     xtty->print(" entry='" INTPTR_FORMAT "' size='%d'",
 752                 instructions_begin(), size());
 753     xtty->print(" address='" INTPTR_FORMAT "'", (intptr_t) this);
 754 
 755     LOG_OFFSET(xtty, relocation);
 756     LOG_OFFSET(xtty, code);
 757     LOG_OFFSET(xtty, stub);
 758     LOG_OFFSET(xtty, consts);
 759     LOG_OFFSET(xtty, scopes_data);
 760     LOG_OFFSET(xtty, scopes_pcs);
 761     LOG_OFFSET(xtty, dependencies);
 762     LOG_OFFSET(xtty, handler_table);
 763     LOG_OFFSET(xtty, nul_chk_table);
 764     LOG_OFFSET(xtty, oops);
 765 
 766     xtty->method(method());
 767     xtty->stamp();
 768     xtty->end_elem();
 769   }
 770 }
 771 
 772 #undef LOG_OFFSET
 773 
 774 
 775 // Print out more verbose output usually for a newly created nmethod.
 776 void nmethod::print_on(outputStream* st, const char* title) const {
 777   if (st != NULL) {
 778     ttyLocker ttyl;
 779     // Print a little tag line that looks like +PrintCompilation output:
 780     st->print("%3d%c  %s",
 781               compile_id(),
 782               is_osr_method() ? '%' :
 783               method() != NULL &&
 784               is_native_method() ? 'n' : ' ',
 785               title);
 786 #ifdef TIERED
 787     st->print(" (%d) ", comp_level());
 788 #endif // TIERED
 789     if (WizardMode) st->print(" (" INTPTR_FORMAT ")", this);
 790     if (method() != NULL) {
 791       method()->print_short_name(st);
 792       if (is_osr_method())
 793         st->print(" @ %d", osr_entry_bci());
 794       if (method()->code_size() > 0)
 795         st->print(" (%d bytes)", method()->code_size());
 796     }
 797   }
 798 }
 799 
 800 
 801 #ifndef PRODUCT
 802 void nmethod::print_nmethod(bool printmethod) {
 803   ttyLocker ttyl;  // keep the following output all in one block
 804   if (xtty != NULL) {
 805     xtty->begin_head("print_nmethod");
 806     xtty->stamp();
 807     xtty->end_head();
 808   }
 809   // print the header part first
 810   print();
 811   // then print the requested information
 812   if (printmethod) {
 813     print_code();
 814     print_pcs();
 815     oop_maps()->print();
 816   }
 817   if (PrintDebugInfo) {
 818     print_scopes();
 819   }
 820   if (PrintRelocations) {
 821     print_relocations();
 822   }
 823   if (PrintDependencies) {
 824     print_dependencies();
 825   }
 826   if (PrintExceptionHandlers) {
 827     print_handler_table();
 828     print_nul_chk_table();
 829   }
 830   if (xtty != NULL) {
 831     xtty->tail("print_nmethod");
 832   }
 833 }
 834 #endif
 835 
 836 
 837 void nmethod::set_version(int v) {
 838   flags.version = v;
 839 }
 840 
 841 
 842 ScopeDesc* nmethod::scope_desc_at(address pc) {
 843   PcDesc* pd = pc_desc_at(pc);
 844   guarantee(pd != NULL, "scope must be present");
 845   return new ScopeDesc(this, pd->scope_decode_offset(),
 846                        pd->obj_decode_offset());
 847 }
 848 
 849 
 850 void nmethod::clear_inline_caches() {
 851   assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
 852   if (is_zombie()) {
 853     return;
 854   }
 855 
 856   RelocIterator iter(this);
 857   while (iter.next()) {
 858     iter.reloc()->clear_inline_cache();
 859   }
 860 }
 861 
 862 
 863 void nmethod::cleanup_inline_caches() {
 864 
 865   assert(SafepointSynchronize::is_at_safepoint() &&
 866         !CompiledIC_lock->is_locked() &&
 867         !Patching_lock->is_locked(), "no threads must be updating the inline caches by them selfs");
 868 
 869   // If the method is not entrant or zombie then a JMP is plastered over the
 870   // first few bytes.  If an oop in the old code was there, that oop
 871   // should not get GC'd.  Skip the first few bytes of oops on
 872   // not-entrant methods.
 873   address low_boundary = verified_entry_point();
 874   if (!is_in_use()) {
 875     low_boundary += NativeJump::instruction_size;
 876     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.
 877     // This means that the low_boundary is going to be a little too high.
 878     // This shouldn't matter, since oops of non-entrant methods are never used.
 879     // In fact, why are we bothering to look at oops in a non-entrant method??
 880   }
 881 
 882   // Find all calls in an nmethod, and clear the ones that points to zombie methods
 883   ResourceMark rm;
 884   RelocIterator iter(this, low_boundary);
 885   while(iter.next()) {
 886     switch(iter.type()) {
 887       case relocInfo::virtual_call_type:
 888       case relocInfo::opt_virtual_call_type: {
 889         CompiledIC *ic = CompiledIC_at(iter.reloc());
 890         // Ok, to lookup references to zombies here
 891         CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination());
 892         if( cb != NULL && cb->is_nmethod() ) {
 893           nmethod* nm = (nmethod*)cb;
 894           // Clean inline caches pointing to both zombie and not_entrant methods
 895           if (!nm->is_in_use()) ic->set_to_clean();
 896         }
 897         break;
 898       }
 899       case relocInfo::static_call_type: {
 900         CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc());
 901         CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination());
 902         if( cb != NULL && cb->is_nmethod() ) {
 903           nmethod* nm = (nmethod*)cb;
 904           // Clean inline caches pointing to both zombie and not_entrant methods
 905           if (!nm->is_in_use()) csc->set_to_clean();
 906         }
 907         break;
 908       }
 909     }
 910   }
 911 }
 912 
 913 void nmethod::mark_as_seen_on_stack() {
 914   assert(is_not_entrant(), "must be a non-entrant method");
 915   set_stack_traversal_mark(NMethodSweeper::traversal_count());
 916 }
 917 
 918 // Tell if a non-entrant method can be converted to a zombie (i.e., there is no activations on the stack)
 919 bool nmethod::can_not_entrant_be_converted() {
 920   assert(is_not_entrant(), "must be a non-entrant method");
 921   assert(SafepointSynchronize::is_at_safepoint(), "must be called during a safepoint");
 922 
 923   // Since the nmethod sweeper only does partial sweep the sweeper's traversal
 924   // count can be greater than the stack traversal count before it hits the
 925   // nmethod for the second time.
 926   return stack_traversal_mark()+1 < NMethodSweeper::traversal_count();
 927 }
 928 
 929 void nmethod::inc_decompile_count() {
 930   // Could be gated by ProfileTraps, but do not bother...
 931   methodOop m = method();
 932   if (m == NULL)  return;
 933   methodDataOop mdo = m->method_data();
 934   if (mdo == NULL)  return;
 935   // There is a benign race here.  See comments in methodDataOop.hpp.
 936   mdo->inc_decompile_count();
 937 }
 938 
 939 void nmethod::make_unloaded(BoolObjectClosure* is_alive, oop cause) {
 940 
 941   post_compiled_method_unload();
 942 
 943   // Since this nmethod is being unloaded, make sure that dependencies
 944   // recorded in instanceKlasses get flushed and pass non-NULL closure to
 945   // indicate that this work is being done during a GC.
 946   assert(Universe::heap()->is_gc_active(), "should only be called during gc");
 947   assert(is_alive != NULL, "Should be non-NULL");
 948   // A non-NULL is_alive closure indicates that this is being called during GC.
 949   flush_dependencies(is_alive);
 950 
 951   // Break cycle between nmethod & method
 952   if (TraceClassUnloading && WizardMode) {
 953     tty->print_cr("[Class unloading: Making nmethod " INTPTR_FORMAT
 954                   " unloadable], methodOop(" INTPTR_FORMAT
 955                   "), cause(" INTPTR_FORMAT ")",
 956                   this, (address)_method, (address)cause);
 957     cause->klass()->print();
 958   }
 959   // If _method is already NULL the methodOop is about to be unloaded,
 960   // so we don't have to break the cycle. Note that it is possible to
 961   // have the methodOop live here, in case we unload the nmethod because
 962   // it is pointing to some oop (other than the methodOop) being unloaded.
 963   if (_method != NULL) {
 964     // OSR methods point to the methodOop, but the methodOop does not
 965     // point back!
 966     if (_method->code() == this) {
 967       _method->clear_code(); // Break a cycle
 968     }
 969     inc_decompile_count();     // Last chance to make a mark on the MDO
 970     _method = NULL;            // Clear the method of this dead nmethod
 971   }
 972   // Make the class unloaded - i.e., change state and notify sweeper
 973   check_safepoint();
 974   if (is_in_use()) {
 975     // Transitioning directly from live to unloaded -- so
 976     // we need to force a cache clean-up; remember this
 977     // for later on.
 978     CodeCache::set_needs_cache_clean(true);
 979   }
 980   flags.state = unloaded;
 981 
 982   // The methodOop is gone at this point
 983   assert(_method == NULL, "Tautology");
 984 
 985   set_link(NULL);
 986   NMethodSweeper::notify(this);
 987 }
 988 
 989 void nmethod::invalidate_osr_method() {
 990   assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod");
 991   if (_entry_bci != InvalidOSREntryBci)
 992     inc_decompile_count();
 993   // Remove from list of active nmethods
 994   if (method() != NULL)
 995     instanceKlass::cast(method()->method_holder())->remove_osr_nmethod(this);
 996   // Set entry as invalid
 997   _entry_bci = InvalidOSREntryBci;
 998 }
 999 
1000 void nmethod::log_state_change(int state) const {
1001   if (LogCompilation) {
1002     if (xtty != NULL) {
1003       ttyLocker ttyl;  // keep the following output all in one block
1004       xtty->begin_elem("make_not_entrant %sthread='" UINTX_FORMAT "'",
1005                        (state == zombie ? "zombie='1' " : ""),
1006                        os::current_thread_id());
1007       log_identity(xtty);
1008       xtty->stamp();
1009       xtty->end_elem();
1010     }
1011   }
1012   if (PrintCompilation) {
1013     print_on(tty, state == zombie ? "made zombie " : "made not entrant ");
1014     tty->cr();
1015   }
1016 }
1017 
1018 // Common functionality for both make_not_entrant and make_zombie
1019 void nmethod::make_not_entrant_or_zombie(int state) {
1020   assert(state == zombie || state == not_entrant, "must be zombie or not_entrant");
1021 
1022   // Code for an on-stack-replacement nmethod is removed when a class gets unloaded.
1023   // They never become zombie/non-entrant, so the nmethod sweeper will never remove
1024   // them. Instead the entry_bci is set to InvalidOSREntryBci, so the osr nmethod
1025   // will never be used anymore. That the nmethods only gets removed when class unloading
1026   // happens, make life much simpler, since the nmethods are not just going to disappear
1027   // out of the blue.
1028   if (is_osr_only_method()) {
1029     if (osr_entry_bci() != InvalidOSREntryBci) {
1030       // only log this once
1031       log_state_change(state);
1032     }
1033     invalidate_osr_method();
1034     return;
1035   }
1036 
1037   // If the method is already zombie or set to the state we want, nothing to do
1038   if (is_zombie() || (state == not_entrant && is_not_entrant())) {
1039     return;
1040   }
1041 
1042   log_state_change(state);
1043 
1044   // Make sure the nmethod is not flushed in case of a safepoint in code below.
1045   nmethodLocker nml(this);
1046 
1047   {
1048     // Enter critical section.  Does not block for safepoint.
1049     MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
1050     // The caller can be calling the method statically or through an inline
1051     // cache call.
1052     if (!is_not_entrant()) {
1053       NativeJump::patch_verified_entry(entry_point(), verified_entry_point(),
1054                   SharedRuntime::get_handle_wrong_method_stub());
1055       assert (NativeJump::instruction_size == nmethod::_zombie_instruction_size, "");
1056     }
1057 
1058     // When the nmethod becomes zombie it is no longer alive so the
1059     // dependencies must be flushed.  nmethods in the not_entrant
1060     // state will be flushed later when the transition to zombie
1061     // happens or they get unloaded.
1062     if (state == zombie) {
1063       assert(SafepointSynchronize::is_at_safepoint(), "must be done at safepoint");
1064       flush_dependencies(NULL);
1065     } else {
1066       assert(state == not_entrant, "other cases may need to be handled differently");
1067     }
1068 
1069     // Change state
1070     flags.state = state;
1071   } // leave critical region under Patching_lock
1072 
1073   if (state == not_entrant) {
1074     Events::log("Make nmethod not entrant " INTPTR_FORMAT, this);
1075   } else {
1076     Events::log("Make nmethod zombie " INTPTR_FORMAT, this);
1077   }
1078 
1079   if (TraceCreateZombies) {
1080     tty->print_cr("nmethod <" INTPTR_FORMAT "> code made %s", this, (state == not_entrant) ? "not entrant" : "zombie");
1081   }
1082 
1083   // Make sweeper aware that there is a zombie method that needs to be removed
1084   NMethodSweeper::notify(this);
1085 
1086   // not_entrant only stuff
1087   if (state == not_entrant) {
1088     mark_as_seen_on_stack();
1089   }
1090 
1091   // It's a true state change, so mark the method as decompiled.
1092   inc_decompile_count();
1093 
1094 
1095   // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload event
1096   // and it hasn't already been reported for this nmethod then report it now.
1097   // (the event may have been reported earilier if the GC marked it for unloading).
1098   if (state == zombie) {
1099 
1100     DTRACE_METHOD_UNLOAD_PROBE(method());
1101 
1102     if (JvmtiExport::should_post_compiled_method_unload() &&
1103         !unload_reported()) {
1104       assert(method() != NULL, "checking");
1105       {
1106         HandleMark hm;
1107         JvmtiExport::post_compiled_method_unload_at_safepoint(
1108             method()->jmethod_id(), code_begin());
1109       }
1110       set_unload_reported();
1111     }
1112   }
1113 
1114 
1115   // Zombie only stuff
1116   if (state == zombie) {
1117     VTune::delete_nmethod(this);
1118   }
1119 
1120   // Check whether method got unloaded at a safepoint before this,
1121   // if so we can skip the flushing steps below
1122   if (method() == NULL) return;
1123 
1124   // Remove nmethod from method.
1125   // We need to check if both the _code and _from_compiled_code_entry_point
1126   // refer to this nmethod because there is a race in setting these two fields
1127   // in methodOop as seen in bugid 4947125.
1128   // If the vep() points to the zombie nmethod, the memory for the nmethod
1129   // could be flushed and the compiler and vtable stubs could still call
1130   // through it.
1131   if (method()->code() == this ||
1132       method()->from_compiled_entry() == verified_entry_point()) {
1133     HandleMark hm;
1134     method()->clear_code();
1135   }
1136 }
1137 
1138 
1139 #ifndef PRODUCT
1140 void nmethod::check_safepoint() {
1141   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1142 }
1143 #endif
1144 
1145 
1146 void nmethod::flush() {
1147   // Note that there are no valid oops in the nmethod anymore.
1148   assert(is_zombie() || (is_osr_method() && is_unloaded()), "must be a zombie method");
1149   assert(is_marked_for_reclamation() || (is_osr_method() && is_unloaded()), "must be marked for reclamation");
1150 
1151   assert (!is_locked_by_vm(), "locked methods shouldn't be flushed");
1152   check_safepoint();
1153 
1154   // completely deallocate this method
1155   EventMark m("flushing nmethod " INTPTR_FORMAT " %s", this, "");
1156   if (PrintMethodFlushing) {
1157     tty->print_cr("*flushing nmethod " INTPTR_FORMAT ". Live blobs: %d", this, CodeCache::nof_blobs());
1158   }
1159 
1160   // We need to deallocate any ExceptionCache data.
1161   // Note that we do not need to grab the nmethod lock for this, it
1162   // better be thread safe if we're disposing of it!
1163   ExceptionCache* ec = exception_cache();
1164   set_exception_cache(NULL);
1165   while(ec != NULL) {
1166     ExceptionCache* next = ec->next();
1167     delete ec;
1168     ec = next;
1169   }
1170 
1171   ((CodeBlob*)(this))->flush();
1172 
1173   CodeCache::free(this);
1174 }
1175 
1176 
1177 //
1178 // Notify all classes this nmethod is dependent on that it is no
1179 // longer dependent. This should only be called in two situations.
1180 // First, when a nmethod transitions to a zombie all dependents need
1181 // to be clear.  Since zombification happens at a safepoint there's no
1182 // synchronization issues.  The second place is a little more tricky.
1183 // During phase 1 of mark sweep class unloading may happen and as a
1184 // result some nmethods may get unloaded.  In this case the flushing
1185 // of dependencies must happen during phase 1 since after GC any
1186 // dependencies in the unloaded nmethod won't be updated, so
1187 // traversing the dependency information in unsafe.  In that case this
1188 // function is called with a non-NULL argument and this function only
1189 // notifies instanceKlasses that are reachable
1190 
1191 void nmethod::flush_dependencies(BoolObjectClosure* is_alive) {
1192   assert(SafepointSynchronize::is_at_safepoint(), "must be done at safepoint");
1193   assert(Universe::heap()->is_gc_active() == (is_alive != NULL),
1194   "is_alive is non-NULL if and only if we are called during GC");
1195   if (!has_flushed_dependencies()) {
1196     set_has_flushed_dependencies();
1197     for (Dependencies::DepStream deps(this); deps.next(); ) {
1198       klassOop klass = deps.context_type();
1199       if (klass == NULL)  continue;  // ignore things like evol_method
1200 
1201       // During GC the is_alive closure is non-NULL, and is used to
1202       // determine liveness of dependees that need to be updated.
1203       if (is_alive == NULL || is_alive->do_object_b(klass)) {
1204         instanceKlass::cast(klass)->remove_dependent_nmethod(this);
1205       }
1206     }
1207   }
1208 }
1209 
1210 
1211 // If this oop is not live, the nmethod can be unloaded.
1212 bool nmethod::can_unload(BoolObjectClosure* is_alive,
1213                          OopClosure* keep_alive,
1214                          oop* root, bool unloading_occurred) {
1215   assert(root != NULL, "just checking");
1216   oop obj = *root;
1217   if (obj == NULL || is_alive->do_object_b(obj)) {
1218       return false;
1219   }
1220   if (obj->is_compiledICHolder()) {
1221     compiledICHolderOop cichk_oop = compiledICHolderOop(obj);
1222     if (is_alive->do_object_b(
1223           cichk_oop->holder_method()->method_holder()) &&
1224         is_alive->do_object_b(cichk_oop->holder_klass())) {
1225       // The oop should be kept alive
1226       keep_alive->do_oop(root);
1227       return false;
1228     }
1229   }
1230   if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) {
1231     // Cannot do this test if verification of the UseParallelOldGC
1232     // code using the PSMarkSweep code is being done.
1233     assert(unloading_occurred, "Inconsistency in unloading");
1234   }
1235   make_unloaded(is_alive, obj);
1236   return true;
1237 }
1238 
1239 // ------------------------------------------------------------------
1240 // post_compiled_method_load_event
1241 // new method for install_code() path
1242 // Transfer information from compilation to jvmti
1243 void nmethod::post_compiled_method_load_event() {
1244 
1245   methodOop moop = method();
1246   HS_DTRACE_PROBE8(hotspot, compiled__method__load,
1247       moop->klass_name()->bytes(),
1248       moop->klass_name()->utf8_length(),
1249       moop->name()->bytes(),
1250       moop->name()->utf8_length(),
1251       moop->signature()->bytes(),
1252       moop->signature()->utf8_length(),
1253       code_begin(), code_size());
1254 
1255   if (JvmtiExport::should_post_compiled_method_load()) {
1256     JvmtiExport::post_compiled_method_load(this);
1257   }
1258 }
1259 
1260 void nmethod::post_compiled_method_unload() {
1261   assert(_method != NULL && !is_unloaded(), "just checking");
1262   DTRACE_METHOD_UNLOAD_PROBE(method());
1263 
1264   // If a JVMTI agent has enabled the CompiledMethodUnload event then
1265   // post the event. Sometime later this nmethod will be made a zombie by
1266   // the sweeper but the methodOop will not be valid at that point.
1267   if (JvmtiExport::should_post_compiled_method_unload()) {
1268     assert(!unload_reported(), "already unloaded");
1269     HandleMark hm;
1270     JvmtiExport::post_compiled_method_unload_at_safepoint(
1271                       method()->jmethod_id(), code_begin());
1272   }
1273 
1274   // The JVMTI CompiledMethodUnload event can be enabled or disabled at
1275   // any time. As the nmethod is being unloaded now we mark it has
1276   // having the unload event reported - this will ensure that we don't
1277   // attempt to report the event in the unlikely scenario where the
1278   // event is enabled at the time the nmethod is made a zombie.
1279   set_unload_reported();
1280 }
1281 
1282 // This is called at the end of the strong tracing/marking phase of a
1283 // GC to unload an nmethod if it contains otherwise unreachable
1284 // oops.
1285 
1286 void nmethod::do_unloading(BoolObjectClosure* is_alive,
1287                            OopClosure* keep_alive, bool unloading_occurred) {
1288   // Make sure the oop's ready to receive visitors
1289   assert(!is_zombie() && !is_unloaded(),
1290          "should not call follow on zombie or unloaded nmethod");
1291 
1292   // If the method is not entrant then a JMP is plastered over the
1293   // first few bytes.  If an oop in the old code was there, that oop
1294   // should not get GC'd.  Skip the first few bytes of oops on
1295   // not-entrant methods.
1296   address low_boundary = verified_entry_point();
1297   if (is_not_entrant()) {
1298     low_boundary += NativeJump::instruction_size;
1299     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.
1300     // (See comment above.)
1301   }
1302 
1303   // The RedefineClasses() API can cause the class unloading invariant
1304   // to no longer be true. See jvmtiExport.hpp for details.
1305   // Also, leave a debugging breadcrumb in local flag.
1306   bool a_class_was_redefined = JvmtiExport::has_redefined_a_class();
1307   if (a_class_was_redefined) {
1308     // This set of the unloading_occurred flag is done before the
1309     // call to post_compiled_method_unload() so that the unloading
1310     // of this nmethod is reported.
1311     unloading_occurred = true;
1312   }
1313 
1314   // Follow methodOop
1315   if (can_unload(is_alive, keep_alive, (oop*)&_method, unloading_occurred)) {
1316     return;
1317   }
1318 
1319   // Exception cache
1320   ExceptionCache* ec = exception_cache();
1321   while (ec != NULL) {
1322     oop* ex_addr = (oop*)ec->exception_type_addr();
1323     oop ex = *ex_addr;
1324     ExceptionCache* next_ec = ec->next();
1325     if (ex != NULL && !is_alive->do_object_b(ex)) {
1326       assert(!ex->is_compiledICHolder(), "Possible error here");
1327       remove_from_exception_cache(ec);
1328     }
1329     ec = next_ec;
1330   }
1331 
1332   // If class unloading occurred we first iterate over all inline caches and
1333   // clear ICs where the cached oop is referring to an unloaded klass or method.
1334   // The remaining live cached oops will be traversed in the relocInfo::oop_type
1335   // iteration below.
1336   if (unloading_occurred) {
1337     RelocIterator iter(this, low_boundary);
1338     while(iter.next()) {
1339       if (iter.type() == relocInfo::virtual_call_type) {
1340         CompiledIC *ic = CompiledIC_at(iter.reloc());
1341         oop ic_oop = ic->cached_oop();
1342         if (ic_oop != NULL && !is_alive->do_object_b(ic_oop)) {
1343           // The only exception is compiledICHolder oops which may
1344           // yet be marked below. (We check this further below).
1345           if (ic_oop->is_compiledICHolder()) {
1346             compiledICHolderOop cichk_oop = compiledICHolderOop(ic_oop);
1347             if (is_alive->do_object_b(
1348                   cichk_oop->holder_method()->method_holder()) &&
1349                 is_alive->do_object_b(cichk_oop->holder_klass())) {
1350               continue;
1351             }
1352           }
1353           ic->set_to_clean();
1354           assert(ic->cached_oop() == NULL, "cached oop in IC should be cleared")
1355         }
1356       }
1357     }
1358   }
1359 
1360   // Compiled code
1361   RelocIterator iter(this, low_boundary);
1362   while (iter.next()) {
1363     if (iter.type() == relocInfo::oop_type) {
1364       oop_Relocation* r = iter.oop_reloc();
1365       // In this loop, we must only traverse those oops directly embedded in
1366       // the code.  Other oops (oop_index>0) are seen as part of scopes_oops.
1367       assert(1 == (r->oop_is_immediate()) +
1368                   (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()),
1369              "oop must be found in exactly one place");
1370       if (r->oop_is_immediate() && r->oop_value() != NULL) {
1371         if (can_unload(is_alive, keep_alive, r->oop_addr(), unloading_occurred)) {
1372           return;
1373         }
1374       }
1375     }
1376   }
1377 
1378 
1379   // Scopes
1380   for (oop* p = oops_begin(); p < oops_end(); p++) {
1381     if (*p == Universe::non_oop_word())  continue;  // skip non-oops
1382     if (can_unload(is_alive, keep_alive, p, unloading_occurred)) {
1383       return;
1384     }
1385   }
1386 
1387 #ifndef PRODUCT
1388   // This nmethod was not unloaded; check below that all CompiledICs
1389   // refer to marked oops.
1390   {
1391     RelocIterator iter(this, low_boundary);
1392     while (iter.next()) {
1393       if (iter.type() == relocInfo::virtual_call_type) {
1394          CompiledIC *ic = CompiledIC_at(iter.reloc());
1395          oop ic_oop = ic->cached_oop();
1396          assert(ic_oop == NULL || is_alive->do_object_b(ic_oop),
1397                 "Found unmarked ic_oop in reachable nmethod");
1398        }
1399     }
1400   }
1401 #endif // !PRODUCT
1402 }
1403 
1404 void nmethod::oops_do(OopClosure* f) {
1405   // make sure the oops ready to receive visitors
1406   assert(!is_zombie() && !is_unloaded(),
1407          "should not call follow on zombie or unloaded nmethod");
1408 
1409   // If the method is not entrant or zombie then a JMP is plastered over the
1410   // first few bytes.  If an oop in the old code was there, that oop
1411   // should not get GC'd.  Skip the first few bytes of oops on
1412   // not-entrant methods.
1413   address low_boundary = verified_entry_point();
1414   if (is_not_entrant()) {
1415     low_boundary += NativeJump::instruction_size;
1416     // %%% Note:  On SPARC we patch only a 4-byte trap, not a full NativeJump.
1417     // (See comment above.)
1418   }
1419 
1420   // Compiled code
1421   f->do_oop((oop*) &_method);
1422   ExceptionCache* ec = exception_cache();
1423   while(ec != NULL) {
1424     f->do_oop((oop*)ec->exception_type_addr());
1425     ec = ec->next();
1426   }
1427 
1428   RelocIterator iter(this, low_boundary);
1429   while (iter.next()) {
1430     if (iter.type() == relocInfo::oop_type ) {
1431       oop_Relocation* r = iter.oop_reloc();
1432       // In this loop, we must only follow those oops directly embedded in
1433       // the code.  Other oops (oop_index>0) are seen as part of scopes_oops.
1434       assert(1 == (r->oop_is_immediate()) + (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()), "oop must be found in exactly one place");
1435       if (r->oop_is_immediate() && r->oop_value() != NULL) {
1436         f->do_oop(r->oop_addr());
1437       }
1438     }
1439   }
1440 
1441   // Scopes
1442   for (oop* p = oops_begin(); p < oops_end(); p++) {
1443     if (*p == Universe::non_oop_word())  continue;  // skip non-oops
1444     f->do_oop(p);
1445   }
1446 }
1447 
1448 // Method that knows how to preserve outgoing arguments at call. This method must be
1449 // called with a frame corresponding to a Java invoke
1450 void nmethod::preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) {
1451   if (!method()->is_native()) {
1452     SimpleScopeDesc ssd(this, fr.pc());
1453     Bytecode_invoke* call = Bytecode_invoke_at(ssd.method(), ssd.bci());
1454     bool is_static = call->is_invokestatic();
1455     symbolOop signature = call->signature();
1456     fr.oops_compiled_arguments_do(signature, is_static, reg_map, f);
1457   }
1458 }
1459 
1460 
1461 oop nmethod::embeddedOop_at(u_char* p) {
1462   RelocIterator iter(this, p, p + oopSize);
1463   while (iter.next())
1464     if (iter.type() == relocInfo::oop_type) {
1465       return iter.oop_reloc()->oop_value();
1466     }
1467   return NULL;
1468 }
1469 
1470 
1471 inline bool includes(void* p, void* from, void* to) {
1472   return from <= p && p < to;
1473 }
1474 
1475 
1476 void nmethod::copy_scopes_pcs(PcDesc* pcs, int count) {
1477   assert(count >= 2, "must be sentinel values, at least");
1478 
1479 #ifdef ASSERT
1480   // must be sorted and unique; we do a binary search in find_pc_desc()
1481   int prev_offset = pcs[0].pc_offset();
1482   assert(prev_offset == PcDesc::lower_offset_limit,
1483          "must start with a sentinel");
1484   for (int i = 1; i < count; i++) {
1485     int this_offset = pcs[i].pc_offset();
1486     assert(this_offset > prev_offset, "offsets must be sorted");
1487     prev_offset = this_offset;
1488   }
1489   assert(prev_offset == PcDesc::upper_offset_limit,
1490          "must end with a sentinel");
1491 #endif //ASSERT
1492 
1493   int size = count * sizeof(PcDesc);
1494   assert(scopes_pcs_size() >= size, "oob");
1495   memcpy(scopes_pcs_begin(), pcs, size);
1496 
1497   // Adjust the final sentinel downward.
1498   PcDesc* last_pc = &scopes_pcs_begin()[count-1];
1499   assert(last_pc->pc_offset() == PcDesc::upper_offset_limit, "sanity");
1500   last_pc->set_pc_offset(instructions_size() + 1);
1501   for (; last_pc + 1 < scopes_pcs_end(); last_pc += 1) {
1502     // Fill any rounding gaps with copies of the last record.
1503     last_pc[1] = last_pc[0];
1504   }
1505   // The following assert could fail if sizeof(PcDesc) is not
1506   // an integral multiple of oopSize (the rounding term).
1507   // If it fails, change the logic to always allocate a multiple
1508   // of sizeof(PcDesc), and fill unused words with copies of *last_pc.
1509   assert(last_pc + 1 == scopes_pcs_end(), "must match exactly");
1510 }
1511 
1512 void nmethod::copy_scopes_data(u_char* buffer, int size) {
1513   assert(scopes_data_size() >= size, "oob");
1514   memcpy(scopes_data_begin(), buffer, size);
1515 }
1516 
1517 
1518 #ifdef ASSERT
1519 static PcDesc* linear_search(nmethod* nm, int pc_offset, bool approximate) {
1520   PcDesc* lower = nm->scopes_pcs_begin();
1521   PcDesc* upper = nm->scopes_pcs_end();
1522   lower += 1; // exclude initial sentinel
1523   PcDesc* res = NULL;
1524   for (PcDesc* p = lower; p < upper; p++) {
1525     NOT_PRODUCT(--nmethod_stats.pc_desc_tests);  // don't count this call to match_desc
1526     if (match_desc(p, pc_offset, approximate)) {
1527       if (res == NULL)
1528         res = p;
1529       else
1530         res = (PcDesc*) badAddress;
1531     }
1532   }
1533   return res;
1534 }
1535 #endif
1536 
1537 
1538 // Finds a PcDesc with real-pc equal to "pc"
1539 PcDesc* nmethod::find_pc_desc_internal(address pc, bool approximate) {
1540   address base_address = instructions_begin();
1541   if ((pc < base_address) ||
1542       (pc - base_address) >= (ptrdiff_t) PcDesc::upper_offset_limit) {
1543     return NULL;  // PC is wildly out of range
1544   }
1545   int pc_offset = (int) (pc - base_address);
1546 
1547   // Check the PcDesc cache if it contains the desired PcDesc
1548   // (This as an almost 100% hit rate.)
1549   PcDesc* res = _pc_desc_cache.find_pc_desc(pc_offset, approximate);
1550   if (res != NULL) {
1551     assert(res == linear_search(this, pc_offset, approximate), "cache ok");
1552     return res;
1553   }
1554 
1555   // Fallback algorithm: quasi-linear search for the PcDesc
1556   // Find the last pc_offset less than the given offset.
1557   // The successor must be the required match, if there is a match at all.
1558   // (Use a fixed radix to avoid expensive affine pointer arithmetic.)
1559   PcDesc* lower = scopes_pcs_begin();
1560   PcDesc* upper = scopes_pcs_end();
1561   upper -= 1; // exclude final sentinel
1562   if (lower >= upper)  return NULL;  // native method; no PcDescs at all
1563 
1564 #define assert_LU_OK \
1565   /* invariant on lower..upper during the following search: */ \
1566   assert(lower->pc_offset() <  pc_offset, "sanity"); \
1567   assert(upper->pc_offset() >= pc_offset, "sanity")
1568   assert_LU_OK;
1569 
1570   // Use the last successful return as a split point.
1571   PcDesc* mid = _pc_desc_cache.last_pc_desc();
1572   NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
1573   if (mid->pc_offset() < pc_offset) {
1574     lower = mid;
1575   } else {
1576     upper = mid;
1577   }
1578 
1579   // Take giant steps at first (4096, then 256, then 16, then 1)
1580   const int LOG2_RADIX = 4 /*smaller steps in debug mode:*/ debug_only(-1);
1581   const int RADIX = (1 << LOG2_RADIX);
1582   for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) {
1583     while ((mid = lower + step) < upper) {
1584       assert_LU_OK;
1585       NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
1586       if (mid->pc_offset() < pc_offset) {
1587         lower = mid;
1588       } else {
1589         upper = mid;
1590         break;
1591       }
1592     }
1593     assert_LU_OK;
1594   }
1595 
1596   // Sneak up on the value with a linear search of length ~16.
1597   while (true) {
1598     assert_LU_OK;
1599     mid = lower + 1;
1600     NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
1601     if (mid->pc_offset() < pc_offset) {
1602       lower = mid;
1603     } else {
1604       upper = mid;
1605       break;
1606     }
1607   }
1608 #undef assert_LU_OK
1609 
1610   if (match_desc(upper, pc_offset, approximate)) {
1611     assert(upper == linear_search(this, pc_offset, approximate), "search ok");
1612     _pc_desc_cache.add_pc_desc(upper);
1613     return upper;
1614   } else {
1615     assert(NULL == linear_search(this, pc_offset, approximate), "search ok");
1616     return NULL;
1617   }
1618 }
1619 
1620 
1621 bool nmethod::check_all_dependencies() {
1622   bool found_check = false;
1623   // wholesale check of all dependencies
1624   for (Dependencies::DepStream deps(this); deps.next(); ) {
1625     if (deps.check_dependency() != NULL) {
1626       found_check = true;
1627       NOT_DEBUG(break);
1628     }
1629   }
1630   return found_check;  // tell caller if we found anything
1631 }
1632 
1633 bool nmethod::check_dependency_on(DepChange& changes) {
1634   // What has happened:
1635   // 1) a new class dependee has been added
1636   // 2) dependee and all its super classes have been marked
1637   bool found_check = false;  // set true if we are upset
1638   for (Dependencies::DepStream deps(this); deps.next(); ) {
1639     // Evaluate only relevant dependencies.
1640     if (deps.spot_check_dependency_at(changes) != NULL) {
1641       found_check = true;
1642       NOT_DEBUG(break);
1643     }
1644   }
1645   return found_check;
1646 }
1647 
1648 bool nmethod::is_evol_dependent_on(klassOop dependee) {
1649   instanceKlass *dependee_ik = instanceKlass::cast(dependee);
1650   objArrayOop dependee_methods = dependee_ik->methods();
1651   for (Dependencies::DepStream deps(this); deps.next(); ) {
1652     if (deps.type() == Dependencies::evol_method) {
1653       methodOop method = deps.method_argument(0);
1654       for (int j = 0; j < dependee_methods->length(); j++) {
1655         if ((methodOop) dependee_methods->obj_at(j) == method) {
1656           // RC_TRACE macro has an embedded ResourceMark
1657           RC_TRACE(0x01000000,
1658             ("Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)",
1659             _method->method_holder()->klass_part()->external_name(),
1660             _method->name()->as_C_string(),
1661             _method->signature()->as_C_string(), compile_id(),
1662             method->method_holder()->klass_part()->external_name(),
1663             method->name()->as_C_string(),
1664             method->signature()->as_C_string()));
1665           if (TraceDependencies || LogCompilation)
1666             deps.log_dependency(dependee);
1667           return true;
1668         }
1669       }
1670     }
1671   }
1672   return false;
1673 }
1674 
1675 // Called from mark_for_deoptimization, when dependee is invalidated.
1676 bool nmethod::is_dependent_on_method(methodOop dependee) {
1677   for (Dependencies::DepStream deps(this); deps.next(); ) {
1678     if (deps.type() != Dependencies::evol_method)
1679       continue;
1680     methodOop method = deps.method_argument(0);
1681     if (method == dependee) return true;
1682   }
1683   return false;
1684 }
1685 
1686 
1687 bool nmethod::is_patchable_at(address instr_addr) {
1688   assert (code_contains(instr_addr), "wrong nmethod used");
1689   if (is_zombie()) {
1690     // a zombie may never be patched
1691     return false;
1692   }
1693   return true;
1694 }
1695 
1696 
1697 address nmethod::continuation_for_implicit_exception(address pc) {
1698   // Exception happened outside inline-cache check code => we are inside
1699   // an active nmethod => use cpc to determine a return address
1700   int exception_offset = pc - instructions_begin();
1701   int cont_offset = ImplicitExceptionTable(this).at( exception_offset );
1702 #ifdef ASSERT
1703   if (cont_offset == 0) {
1704     Thread* thread = ThreadLocalStorage::get_thread_slow();
1705     ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY
1706     HandleMark hm(thread);
1707     ResourceMark rm(thread);
1708     CodeBlob* cb = CodeCache::find_blob(pc);
1709     assert(cb != NULL && cb == this, "");
1710     tty->print_cr("implicit exception happened at " INTPTR_FORMAT, pc);
1711     print();
1712     method()->print_codes();
1713     print_code();
1714     print_pcs();
1715   }
1716 #endif
1717   guarantee(cont_offset != 0, "unhandled implicit exception in compiled code");
1718   return instructions_begin() + cont_offset;
1719 }
1720 
1721 
1722 
1723 void nmethod_init() {
1724   // make sure you didn't forget to adjust the filler fields
1725   assert(sizeof(nmFlags) <= 4,           "nmFlags occupies more than a word");
1726   assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
1727 }
1728 
1729 
1730 //-------------------------------------------------------------------------------------------
1731 
1732 
1733 // QQQ might we make this work from a frame??
1734 nmethodLocker::nmethodLocker(address pc) {
1735   CodeBlob* cb = CodeCache::find_blob(pc);
1736   guarantee(cb != NULL && cb->is_nmethod(), "bad pc for a nmethod found");
1737   _nm = (nmethod*)cb;
1738   lock_nmethod(_nm);
1739 }
1740 
1741 void nmethodLocker::lock_nmethod(nmethod* nm) {
1742   if (nm == NULL)  return;
1743   Atomic::inc(&nm->_lock_count);
1744   guarantee(!nm->is_zombie(), "cannot lock a zombie method");
1745 }
1746 
1747 void nmethodLocker::unlock_nmethod(nmethod* nm) {
1748   if (nm == NULL)  return;
1749   Atomic::dec(&nm->_lock_count);
1750   guarantee(nm->_lock_count >= 0, "unmatched nmethod lock/unlock");
1751 }
1752 
1753 bool nmethod::is_deopt_pc(address pc) {
1754   bool ret =  pc == deopt_handler_begin();
1755   return ret;
1756 }
1757 
1758 
1759 // -----------------------------------------------------------------------------
1760 // Verification
1761 
1762 void nmethod::verify() {
1763 
1764   // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant
1765   // seems odd.
1766 
1767   if( is_zombie() || is_not_entrant() )
1768     return;
1769 
1770   // Make sure all the entry points are correctly aligned for patching.
1771   NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point());
1772 
1773   assert(method()->is_oop(), "must be valid");
1774 
1775   ResourceMark rm;
1776 
1777   if (!CodeCache::contains(this)) {
1778     fatal1("nmethod at " INTPTR_FORMAT " not in zone", this);
1779   }
1780 
1781   if(is_native_method() )
1782     return;
1783 
1784   nmethod* nm = CodeCache::find_nmethod(verified_entry_point());
1785   if (nm != this) {
1786     fatal1("findNMethod did not find this nmethod (" INTPTR_FORMAT ")", this);
1787   }
1788 
1789   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
1790     if (! p->verify(this)) {
1791       tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", this);
1792     }
1793   }
1794 
1795   verify_scopes();
1796 }
1797 
1798 
1799 void nmethod::verify_interrupt_point(address call_site) {
1800   // This code does not work in release mode since
1801   // owns_lock only is available in debug mode.
1802   CompiledIC* ic = NULL;
1803   Thread *cur = Thread::current();
1804   if (CompiledIC_lock->owner() == cur ||
1805       ((cur->is_VM_thread() || cur->is_ConcurrentGC_thread()) &&
1806        SafepointSynchronize::is_at_safepoint())) {
1807     ic = CompiledIC_at(call_site);
1808     CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
1809   } else {
1810     MutexLocker ml_verify (CompiledIC_lock);
1811     ic = CompiledIC_at(call_site);
1812   }
1813   PcDesc* pd = pc_desc_at(ic->end_of_call());
1814   assert(pd != NULL, "PcDesc must exist");
1815   for (ScopeDesc* sd = new ScopeDesc(this, pd->scope_decode_offset(),
1816                                      pd->obj_decode_offset());
1817        !sd->is_top(); sd = sd->sender()) {
1818     sd->verify();
1819   }
1820 }
1821 
1822 void nmethod::verify_scopes() {
1823   if( !method() ) return;       // Runtime stubs have no scope
1824   if (method()->is_native()) return; // Ignore stub methods.
1825   // iterate through all interrupt point
1826   // and verify the debug information is valid.
1827   RelocIterator iter((nmethod*)this);
1828   while (iter.next()) {
1829     address stub = NULL;
1830     switch (iter.type()) {
1831       case relocInfo::virtual_call_type:
1832         verify_interrupt_point(iter.addr());
1833         break;
1834       case relocInfo::opt_virtual_call_type:
1835         stub = iter.opt_virtual_call_reloc()->static_stub();
1836         verify_interrupt_point(iter.addr());
1837         break;
1838       case relocInfo::static_call_type:
1839         stub = iter.static_call_reloc()->static_stub();
1840         //verify_interrupt_point(iter.addr());
1841         break;
1842       case relocInfo::runtime_call_type:
1843         address destination = iter.reloc()->value();
1844         // Right now there is no way to find out which entries support
1845         // an interrupt point.  It would be nice if we had this
1846         // information in a table.
1847         break;
1848     }
1849     assert(stub == NULL || stub_contains(stub), "static call stub outside stub section");
1850   }
1851 }
1852 
1853 
1854 // -----------------------------------------------------------------------------
1855 // Non-product code
1856 #ifndef PRODUCT
1857 
1858 void nmethod::check_store() {
1859   // Make sure all oops in the compiled code are tenured
1860 
1861   RelocIterator iter(this);
1862   while (iter.next()) {
1863     if (iter.type() == relocInfo::oop_type) {
1864       oop_Relocation* reloc = iter.oop_reloc();
1865       oop obj = reloc->oop_value();
1866       if (obj != NULL && !obj->is_perm()) {
1867         fatal("must be permanent oop in compiled code");
1868       }
1869     }
1870   }
1871 }
1872 
1873 
1874 // Printing operations
1875 
1876 void nmethod::print() const {
1877   ResourceMark rm;
1878   ttyLocker ttyl;   // keep the following output all in one block
1879 
1880   tty->print("Compiled ");
1881 
1882   if (is_compiled_by_c1()) {
1883     tty->print("(c1) ");
1884   } else if (is_compiled_by_c2()) {
1885     tty->print("(c2) ");
1886   } else {
1887     assert(is_native_method(), "Who else?");
1888     tty->print("(nm) ");
1889   }
1890 
1891   print_on(tty, "nmethod");
1892   tty->cr();
1893   if (WizardMode) {
1894     tty->print("((nmethod*) "INTPTR_FORMAT ") ", this);
1895     tty->print(" for method " INTPTR_FORMAT , (address)method());
1896     tty->print(" { ");
1897     if (version())        tty->print("v%d ", version());
1898     if (level())          tty->print("l%d ", level());
1899     if (is_in_use())      tty->print("in_use ");
1900     if (is_not_entrant()) tty->print("not_entrant ");
1901     if (is_zombie())      tty->print("zombie ");
1902     if (is_unloaded())    tty->print("unloaded ");
1903     tty->print_cr("}:");
1904   }
1905   if (size              () > 0) tty->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1906                                               (address)this,
1907                                               (address)this + size(),
1908                                               size());
1909   if (relocation_size   () > 0) tty->print_cr(" relocation     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1910                                               relocation_begin(),
1911                                               relocation_end(),
1912                                               relocation_size());
1913   if (code_size         () > 0) tty->print_cr(" main code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1914                                               code_begin(),
1915                                               code_end(),
1916                                               code_size());
1917   if (stub_size         () > 0) tty->print_cr(" stub code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1918                                               stub_begin(),
1919                                               stub_end(),
1920                                               stub_size());
1921   if (consts_size       () > 0) tty->print_cr(" constants      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1922                                               consts_begin(),
1923                                               consts_end(),
1924                                               consts_size());
1925   if (scopes_data_size  () > 0) tty->print_cr(" scopes data    [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1926                                               scopes_data_begin(),
1927                                               scopes_data_end(),
1928                                               scopes_data_size());
1929   if (scopes_pcs_size   () > 0) tty->print_cr(" scopes pcs     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1930                                               scopes_pcs_begin(),
1931                                               scopes_pcs_end(),
1932                                               scopes_pcs_size());
1933   if (dependencies_size () > 0) tty->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1934                                               dependencies_begin(),
1935                                               dependencies_end(),
1936                                               dependencies_size());
1937   if (handler_table_size() > 0) tty->print_cr(" handler table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1938                                               handler_table_begin(),
1939                                               handler_table_end(),
1940                                               handler_table_size());
1941   if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1942                                               nul_chk_table_begin(),
1943                                               nul_chk_table_end(),
1944                                               nul_chk_table_size());
1945   if (oops_size         () > 0) tty->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1946                                               oops_begin(),
1947                                               oops_end(),
1948                                               oops_size());
1949 }
1950 
1951 
1952 void nmethod::print_scopes() {
1953   // Find the first pc desc for all scopes in the code and print it.
1954   ResourceMark rm;
1955   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
1956     if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
1957       continue;
1958 
1959     ScopeDesc* sd = scope_desc_at(p->real_pc(this));
1960     sd->print_on(tty, p);
1961   }
1962 }
1963 
1964 void nmethod::print_dependencies() {
1965   ResourceMark rm;
1966   ttyLocker ttyl;   // keep the following output all in one block
1967   tty->print_cr("Dependencies:");
1968   for (Dependencies::DepStream deps(this); deps.next(); ) {
1969     deps.print_dependency();
1970     klassOop ctxk = deps.context_type();
1971     if (ctxk != NULL) {
1972       Klass* k = Klass::cast(ctxk);
1973       if (k->oop_is_instance() && ((instanceKlass*)k)->is_dependent_nmethod(this)) {
1974         tty->print_cr("   [nmethod<=klass]%s", k->external_name());
1975       }
1976     }
1977     deps.log_dependency();  // put it into the xml log also
1978   }
1979 }
1980 
1981 
1982 void nmethod::print_code() {
1983   HandleMark hm;
1984   ResourceMark m;
1985   Disassembler().decode(this);
1986 }
1987 
1988 
1989 void nmethod::print_relocations() {
1990   ResourceMark m;       // in case methods get printed via the debugger
1991   tty->print_cr("relocations:");
1992   RelocIterator iter(this);
1993   iter.print();
1994   if (UseRelocIndex) {
1995     jint* index_end   = (jint*)relocation_end() - 1;
1996     jint  index_size  = *index_end;
1997     jint* index_start = (jint*)( (address)index_end - index_size );
1998     tty->print_cr("    index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size);
1999     if (index_size > 0) {
2000       jint* ip;
2001       for (ip = index_start; ip+2 <= index_end; ip += 2)
2002         tty->print_cr("  (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT,
2003                       ip[0],
2004                       ip[1],
2005                       header_end()+ip[0],
2006                       relocation_begin()-1+ip[1]);
2007       for (; ip < index_end; ip++)
2008         tty->print_cr("  (%d ?)", ip[0]);
2009       tty->print_cr("          @" INTPTR_FORMAT ": index_size=%d", ip, *ip++);
2010       tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip);
2011     }
2012   }
2013 }
2014 
2015 
2016 void nmethod::print_pcs() {
2017   ResourceMark m;       // in case methods get printed via debugger
2018   tty->print_cr("pc-bytecode offsets:");
2019   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2020     p->print(this);
2021   }
2022 }
2023 
2024 
2025 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) {
2026   RelocIterator iter(this, begin, end);
2027   bool have_one = false;
2028   while (iter.next()) {
2029     have_one = true;
2030     switch (iter.type()) {
2031         case relocInfo::none:                  return "no_reloc";
2032         case relocInfo::oop_type: {
2033           stringStream st;
2034           oop_Relocation* r = iter.oop_reloc();
2035           oop obj = r->oop_value();
2036           st.print("oop(");
2037           if (obj == NULL) st.print("NULL");
2038           else obj->print_value_on(&st);
2039           st.print(")");
2040           return st.as_string();
2041         }
2042         case relocInfo::virtual_call_type:     return "virtual_call";
2043         case relocInfo::opt_virtual_call_type: return "optimized virtual_call";
2044         case relocInfo::static_call_type:      return "static_call";
2045         case relocInfo::static_stub_type:      return "static_stub";
2046         case relocInfo::runtime_call_type:     return "runtime_call";
2047         case relocInfo::external_word_type:    return "external_word";
2048         case relocInfo::internal_word_type:    return "internal_word";
2049         case relocInfo::section_word_type:     return "section_word";
2050         case relocInfo::poll_type:             return "poll";
2051         case relocInfo::poll_return_type:      return "poll_return";
2052         case relocInfo::type_mask:             return "type_bit_mask";
2053     }
2054   }
2055   return have_one ? "other" : NULL;
2056 }
2057 
2058 
2059 // Return a the last scope in (begin..end]
2060 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
2061   PcDesc* p = pc_desc_near(begin+1);
2062   if (p != NULL && p->real_pc(this) <= end) {
2063     return new ScopeDesc(this, p->scope_decode_offset(),
2064                          p->obj_decode_offset());
2065   }
2066   return NULL;
2067 }
2068 
2069 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) {
2070   // First, find an oopmap in (begin, end].
2071   // We use the odd half-closed interval so that oop maps and scope descs
2072   // which are tied to the byte after a call are printed with the call itself.
2073   address base = instructions_begin();
2074   OopMapSet* oms = oop_maps();
2075   if (oms != NULL) {
2076     for (int i = 0, imax = oms->size(); i < imax; i++) {
2077       OopMap* om = oms->at(i);
2078       address pc = base + om->offset();
2079       if (pc > begin) {
2080         if (pc <= end) {
2081           st->fill_to(column);
2082           if (st == tty) {
2083             st->print("; OopMap ");
2084             om->print();
2085             tty->cr();
2086           } else {
2087             st->print_cr("; OopMap #%d offset:%d", i, om->offset());
2088           }
2089         }
2090         break;
2091       }
2092     }
2093   }
2094   ScopeDesc* sd  = scope_desc_in(begin, end);
2095   if (sd != NULL) {
2096     st->fill_to(column);
2097     if (sd->bci() == SynchronizationEntryBCI) {
2098       st->print(";*synchronization entry");
2099     } else {
2100       if (sd->method().is_null()) {
2101         tty->print("method is NULL");
2102       } else if (sd->method()->is_native()) {
2103         tty->print("method is native");
2104       } else {
2105         address bcp  = sd->method()->bcp_from(sd->bci());
2106         Bytecodes::Code bc = Bytecodes::java_code_at(bcp);
2107         st->print(";*%s", Bytecodes::name(bc));
2108         switch (bc) {
2109         case Bytecodes::_invokevirtual:
2110         case Bytecodes::_invokespecial:
2111         case Bytecodes::_invokestatic:
2112         case Bytecodes::_invokeinterface:
2113           {
2114             Bytecode_invoke* invoke = Bytecode_invoke_at(sd->method(), sd->bci());
2115             st->print(" ");
2116             if (invoke->name() != NULL)
2117               invoke->name()->print_symbol_on(st);
2118             else
2119               st->print("<UNKNOWN>");
2120             break;
2121           }
2122         case Bytecodes::_getfield:
2123         case Bytecodes::_putfield:
2124         case Bytecodes::_getstatic:
2125         case Bytecodes::_putstatic:
2126           {
2127             methodHandle sdm = sd->method();
2128             Bytecode_field* field = Bytecode_field_at(sdm(), sdm->bcp_from(sd->bci()));
2129             constantPoolOop sdmc = sdm->constants();
2130             symbolOop name = sdmc->name_ref_at(field->index());
2131             st->print(" ");
2132             if (name != NULL)
2133               name->print_symbol_on(st);
2134             else
2135               st->print("<UNKNOWN>");
2136           }
2137         }
2138       }
2139     }
2140     st->cr();
2141     // Print all scopes
2142     for (;sd != NULL; sd = sd->sender()) {
2143       st->fill_to(column);
2144       st->print("; -");
2145       if (sd->method().is_null()) {
2146         tty->print("method is NULL");
2147       } else {
2148         sd->method()->print_short_name(st);
2149       }
2150       int lineno = sd->method()->line_number_from_bci(sd->bci());
2151       if (lineno != -1) {
2152         st->print("@%d (line %d)", sd->bci(), lineno);
2153       } else {
2154         st->print("@%d", sd->bci());
2155       }
2156       st->cr();
2157     }
2158   }
2159 
2160   // Print relocation information
2161   const char* str = reloc_string_for(begin, end);
2162   if (str != NULL) {
2163     if (sd != NULL) st->cr();
2164     st->fill_to(column);
2165     st->print(";   {%s}", str);
2166   }
2167   int cont_offset = ImplicitExceptionTable(this).at(begin - instructions_begin());
2168   if (cont_offset != 0) {
2169     st->fill_to(column);
2170     st->print("; implicit exception: dispatches to " INTPTR_FORMAT, instructions_begin() + cont_offset);
2171   }
2172 
2173 }
2174 
2175 void nmethod::print_value_on(outputStream* st) const {
2176   print_on(st, "nmethod");
2177 }
2178 
2179 void nmethod::print_calls(outputStream* st) {
2180   RelocIterator iter(this);
2181   while (iter.next()) {
2182     switch (iter.type()) {
2183     case relocInfo::virtual_call_type:
2184     case relocInfo::opt_virtual_call_type: {
2185       VerifyMutexLocker mc(CompiledIC_lock);
2186       CompiledIC_at(iter.reloc())->print();
2187       break;
2188     }
2189     case relocInfo::static_call_type:
2190       st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr());
2191       compiledStaticCall_at(iter.reloc())->print();
2192       break;
2193     }
2194   }
2195 }
2196 
2197 void nmethod::print_handler_table() {
2198   ExceptionHandlerTable(this).print();
2199 }
2200 
2201 void nmethod::print_nul_chk_table() {
2202   ImplicitExceptionTable(this).print(instructions_begin());
2203 }
2204 
2205 void nmethod::print_statistics() {
2206   ttyLocker ttyl;
2207   if (xtty != NULL)  xtty->head("statistics type='nmethod'");
2208   nmethod_stats.print_native_nmethod_stats();
2209   nmethod_stats.print_nmethod_stats();
2210   DebugInformationRecorder::print_statistics();
2211   nmethod_stats.print_pc_stats();
2212   Dependencies::print_statistics();
2213   if (xtty != NULL)  xtty->tail("statistics");
2214 }
2215 
2216 #endif // PRODUCT