hotspot/src/share/vm/code/nmethod.cpp

Print this page
rev 611 : Merge
   1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)nmethod.cpp  1.371 08/02/29 12:46:11 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_nmethod.cpp.incl"
  30 
  31 #ifdef DTRACE_ENABLED
  32 
  33 
  34 // Only bother with this argument setup if dtrace is available
  35 
  36 HS_DTRACE_PROBE_DECL8(hotspot, compiled__method__load, 
  37   const char*, int, const char*, int, const char*, int, void*, size_t);
  38 
  39 HS_DTRACE_PROBE_DECL6(hotspot, compiled__method__unload, 
  40   char*, int, char*, int, char*, int);
  41 
  42 #define DTRACE_METHOD_UNLOAD_PROBE(method)                                \
  43   {                                                                       \
  44     methodOop m = (method);                                               \
  45     if (m != NULL) {                                                      \
  46       symbolOop klass_name = m->klass_name();                             \
  47       symbolOop name = m->name();                                         \
  48       symbolOop signature = m->signature();                               \
  49       HS_DTRACE_PROBE6(hotspot, compiled__method__unload,                 \
  50         klass_name->bytes(), klass_name->utf8_length(),                   \
  51         name->bytes(), name->utf8_length(),                               \
  52         signature->bytes(), signature->utf8_length());                    \
  53     }                                                                     \


 424   return NULL;
 425 }
 426 
 427 // %%% This variable is no longer used?
 428 int nmethod::_zombie_instruction_size = NativeJump::instruction_size;
 429 
 430 
 431 nmethod* nmethod::new_native_nmethod(methodHandle method,
 432   CodeBuffer *code_buffer, 
 433   int vep_offset, 
 434   int frame_complete, 
 435   int frame_size, 
 436   ByteSize basic_lock_owner_sp_offset,
 437   ByteSize basic_lock_sp_offset,
 438   OopMapSet* oop_maps) {
 439   // create nmethod
 440   nmethod* nm = NULL;
 441   {
 442     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 443     int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod));
 444     const int dummy = -1;               // Flag to force proper "operator new"
 445     CodeOffsets offsets;
 446     offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
 447     offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
 448     nm = new (native_nmethod_size)
 449       nmethod(method(), native_nmethod_size, &offsets,
 450               code_buffer, frame_size,
 451               basic_lock_owner_sp_offset, basic_lock_sp_offset,
 452               oop_maps);
 453     NOT_PRODUCT(if (nm != NULL)  nmethod_stats.note_native_nmethod(nm));
 454     if (PrintAssembly && nm != NULL)
 455       Disassembler::decode(nm);
 456   }
 457   // verify nmethod
 458   debug_only(if (nm) nm->verify();) // might block
 459 
 460   if (nm != NULL) {
 461     nm->log_new_nmethod();
 462   }
 463 
 464   return nm;
 465 }
 466 



































 467 nmethod* nmethod::new_nmethod(methodHandle method,
 468   int compile_id,
 469   int entry_bci,
 470   CodeOffsets* offsets,
 471   int orig_pc_offset,
 472   DebugInformationRecorder* debug_info, 
 473   Dependencies* dependencies,
 474   CodeBuffer* code_buffer, int frame_size, 
 475   OopMapSet* oop_maps, 
 476   ExceptionHandlerTable* handler_table, 
 477   ImplicitExceptionTable* nul_chk_table,
 478   AbstractCompiler* compiler,
 479   int comp_level
 480 )
 481 {
 482   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
 483   // create nmethod
 484   nmethod* nm = NULL;
 485   { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 486     int nmethod_size =


 544   OopMapSet* oop_maps )
 545   : CodeBlob("native nmethod", code_buffer, sizeof(nmethod),
 546              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
 547   _compiled_synchronized_native_basic_lock_owner_sp_offset(basic_lock_owner_sp_offset),
 548   _compiled_synchronized_native_basic_lock_sp_offset(basic_lock_sp_offset)
 549 {
 550   {
 551     debug_only(No_Safepoint_Verifier nsv;)
 552     assert_locked_or_safepoint(CodeCache_lock);
 553 
 554     NOT_PRODUCT(_has_debug_info = false; )
 555     _method                  = method;
 556     _entry_bci               = InvocationEntryBci;
 557     _link                    = NULL;
 558     _compiler                = NULL;
 559     // We have no exception handler or deopt handler make the
 560     // values something that will never match a pc like the nmethod vtable entry
 561     _exception_offset        = 0;
 562     _deoptimize_offset       = 0;
 563     _orig_pc_offset          = 0;



 564     _stub_offset             = data_offset();
 565     _consts_offset           = data_offset();
 566     _scopes_data_offset      = data_offset();
 567     _scopes_pcs_offset       = _scopes_data_offset;
 568     _dependencies_offset     = _scopes_pcs_offset;
 569     _handler_table_offset    = _dependencies_offset;
 570     _nul_chk_table_offset    = _handler_table_offset;
 571     _nmethod_end_offset      = _nul_chk_table_offset;
 572     _compile_id              = 0;  // default
 573     _comp_level              = CompLevel_none;
 574     _entry_point             = instructions_begin();
 575     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
 576     _osr_entry_point         = NULL;
 577     _exception_cache         = NULL;
 578     _pc_desc_cache.reset_to(NULL);
 579 
 580     flags.clear();
 581     flags.state              = alive;
 582     _markedForDeoptimization = 0;
 583 


 601       xtty->stamp();
 602       xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
 603     }
 604     // print the header part first
 605     print();
 606     // then print the requested information
 607     if (PrintNativeNMethods) {
 608       print_code();
 609       oop_maps->print();
 610     }
 611     if (PrintRelocations) {
 612       print_relocations();
 613     }
 614     if (xtty != NULL) {
 615       xtty->tail("print_native_nmethod");
 616     }
 617   }
 618   Events::log("Create nmethod " INTPTR_FORMAT, this);
 619 }
 620 




















































































 621 
 622 void* nmethod::operator new(size_t size, int nmethod_size) {  
 623   // Always leave some room in the CodeCache for I2C/C2I adapters  
 624   if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) return NULL;
 625   return CodeCache::allocate(nmethod_size);    
 626 }
 627 
 628 
 629 nmethod::nmethod(
 630   methodOop method,
 631   int nmethod_size,
 632   int compile_id,
 633   int entry_bci,
 634   CodeOffsets* offsets,
 635   int orig_pc_offset,  
 636   DebugInformationRecorder* debug_info,
 637   Dependencies* dependencies,
 638   CodeBuffer *code_buffer,
 639   int frame_size,
 640   OopMapSet* oop_maps,


 644   int comp_level
 645   )
 646   : CodeBlob("nmethod", code_buffer, sizeof(nmethod),
 647              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
 648   _compiled_synchronized_native_basic_lock_owner_sp_offset(in_ByteSize(-1)),
 649   _compiled_synchronized_native_basic_lock_sp_offset(in_ByteSize(-1))
 650 {
 651   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
 652   {
 653     debug_only(No_Safepoint_Verifier nsv;)
 654     assert_locked_or_safepoint(CodeCache_lock);
 655 
 656     NOT_PRODUCT(_has_debug_info = false; )
 657     _method                  = method;
 658     _compile_id              = compile_id;
 659     _comp_level              = comp_level;
 660     _entry_bci               = entry_bci;
 661     _link                    = NULL;
 662     _compiler                = compiler;
 663     _orig_pc_offset          = orig_pc_offset;



 664     _stub_offset             = instructions_offset() + code_buffer->total_offset_of(code_buffer->stubs()->start());
 665 
 666     // Exception handler and deopt handler are in the stub section
 667     _exception_offset        = _stub_offset + offsets->value(CodeOffsets::Exceptions);
 668     _deoptimize_offset       = _stub_offset + offsets->value(CodeOffsets::Deopt);
 669     _consts_offset           = instructions_offset() + code_buffer->total_offset_of(code_buffer->consts()->start());
 670     _scopes_data_offset      = data_offset();
 671     _scopes_pcs_offset       = _scopes_data_offset   + round_to(debug_info->data_size         (), oopSize);
 672     _dependencies_offset     = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
 673     _handler_table_offset    = _dependencies_offset  + round_to(dependencies->size_in_bytes (), oopSize);
 674     _nul_chk_table_offset    = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize);
 675     _nmethod_end_offset      = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize);
 676  
 677     _entry_point             = instructions_begin();
 678     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
 679     _osr_entry_point         = instructions_begin() + offsets->value(CodeOffsets::OSR_Entry);
 680     _exception_cache         = NULL;
 681     _pc_desc_cache.reset_to(scopes_pcs_begin());
 682 
 683     flags.clear();


 693     code_buffer->copy_oops_to(this);
 694     debug_info->copy_to(this);
 695     dependencies->copy_to(this);
 696     debug_only(check_store();)
 697 
 698     CodeCache::commit(this);
 699   
 700     VTune::create_nmethod(this);
 701 
 702     // Copy contents of ExceptionHandlerTable to nmethod
 703     handler_table->copy_to(this);
 704     nul_chk_table->copy_to(this);
 705 
 706     // we use the information of entry points to find out if a method is
 707     // static or non static
 708     assert(compiler->is_c2() || 
 709            _method->is_static() == (entry_point() == _verified_entry_point),
 710            " entry points must be same for static methods and vice versa");
 711   }
 712 
 713   bool printnmethods = PrintNMethods || CompilerOracle::has_option_string(_method, "PrintNMethods");


 714   if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) {
 715     print_nmethod(printnmethods);
 716   }
 717 
 718   // Note: Do not verify in here as the CodeCache_lock is
 719   //       taken which would conflict with the CompiledIC_lock
 720   //       which taken during the verification of call sites.
 721   //       (was bug - gri 10/25/99)
 722 
 723   Events::log("Create nmethod " INTPTR_FORMAT, this);
 724 }
 725 
 726 
 727 // Print a short set of xml attributes to identify this nmethod.  The
 728 // output should be embedded in some other element.
 729 void nmethod::log_identity(xmlStream* log) const {
 730   log->print(" compile_id='%d'", compile_id());
 731   const char* nm_kind = compile_kind();
 732   if (nm_kind != NULL)  log->print(" compile_kind='%s'", nm_kind);
 733   if (compiler() != NULL) {


 784               compile_id(),
 785               is_osr_method() ? '%' :
 786               method() != NULL &&
 787               is_native_method() ? 'n' : ' ',
 788               title);
 789 #ifdef TIERED
 790     st->print(" (%d) ", comp_level());
 791 #endif // TIERED
 792     if (WizardMode) st->print(" (" INTPTR_FORMAT ")", this);
 793     if (method() != NULL) {
 794       method()->print_short_name(st);
 795       if (is_osr_method())
 796         st->print(" @ %d", osr_entry_bci());
 797       if (method()->code_size() > 0)
 798         st->print(" (%d bytes)", method()->code_size());
 799     }
 800   }
 801 }
 802 
 803 
 804 #ifndef PRODUCT
 805 void nmethod::print_nmethod(bool printmethod) {
 806   ttyLocker ttyl;  // keep the following output all in one block
 807   if (xtty != NULL) {
 808     xtty->begin_head("print_nmethod");
 809     xtty->stamp();
 810     xtty->end_head();
 811   }
 812   // print the header part first
 813   print();
 814   // then print the requested information
 815   if (printmethod) {
 816     print_code();
 817     print_pcs();
 818     oop_maps()->print();
 819   }
 820   if (PrintDebugInfo) {
 821     print_scopes();
 822   }
 823   if (PrintRelocations) {
 824     print_relocations();
 825   }
 826   if (PrintDependencies) {
 827     print_dependencies();
 828   }
 829   if (PrintExceptionHandlers) {
 830     print_handler_table();
 831     print_nul_chk_table();
 832   }
 833   if (xtty != NULL) {
 834     xtty->tail("print_nmethod");
 835   }
 836 }
 837 #endif
 838 
 839 
 840 void nmethod::set_version(int v) {
 841   flags.version = v;
 842 }
 843 
 844 
 845 ScopeDesc* nmethod::scope_desc_at(address pc) {
 846   PcDesc* pd = pc_desc_at(pc);
 847   guarantee(pd != NULL, "scope must be present");
 848   return new ScopeDesc(this, pd->scope_decode_offset(),
 849                        pd->obj_decode_offset());
 850 }
 851 
 852 
 853 void nmethod::clear_inline_caches() {  
 854   assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
 855   if (is_zombie()) {
 856     return;
 857   }


1213 
1214 // If this oop is not live, the nmethod can be unloaded.
1215 bool nmethod::can_unload(BoolObjectClosure* is_alive,
1216                          OopClosure* keep_alive,
1217                          oop* root, bool unloading_occurred) {
1218   assert(root != NULL, "just checking");
1219   oop obj = *root;
1220   if (obj == NULL || is_alive->do_object_b(obj)) {
1221       return false;
1222   }
1223   if (obj->is_compiledICHolder()) {
1224     compiledICHolderOop cichk_oop = compiledICHolderOop(obj);
1225     if (is_alive->do_object_b(
1226           cichk_oop->holder_method()->method_holder()) &&
1227         is_alive->do_object_b(cichk_oop->holder_klass())) {
1228       // The oop should be kept alive
1229       keep_alive->do_oop(root);
1230       return false;
1231     }
1232   }
1233   if (!UseParallelOldGC || !VerifyParallelOldWithMarkSweep) {
1234     // Cannot do this test if verification of the UseParallelOldGC
1235     // code using the PSMarkSweep code is being done.
1236     assert(unloading_occurred, "Inconsistency in unloading");
1237   }
1238   make_unloaded(is_alive, obj);
1239   return true;
1240 }
1241 
1242 // ------------------------------------------------------------------
1243 // post_compiled_method_load_event
1244 // new method for install_code() path
1245 // Transfer information from compilation to jvmti
1246 void nmethod::post_compiled_method_load_event() {
1247 
1248   methodOop moop = method();
1249   HS_DTRACE_PROBE8(hotspot, compiled__method__load, 
1250       moop->klass_name()->bytes(), 
1251       moop->klass_name()->utf8_length(),
1252       moop->name()->bytes(), 
1253       moop->name()->utf8_length(),
1254       moop->signature()->bytes(), 
1255       moop->signature()->utf8_length(),
1256       code_begin(), code_size());
1257 


1856 
1857 // -----------------------------------------------------------------------------
1858 // Non-product code
1859 #ifndef PRODUCT
1860 
1861 void nmethod::check_store() {
1862   // Make sure all oops in the compiled code are tenured
1863 
1864   RelocIterator iter(this);
1865   while (iter.next()) {
1866     if (iter.type() == relocInfo::oop_type) {
1867       oop_Relocation* reloc = iter.oop_reloc();
1868       oop obj = reloc->oop_value();
1869       if (obj != NULL && !obj->is_perm()) {
1870         fatal("must be permanent oop in compiled code");
1871       }
1872     }
1873   }
1874 }
1875 

1876 
1877 // Printing operations
1878 
1879 void nmethod::print() const {
1880   ResourceMark rm;
1881   ttyLocker ttyl;   // keep the following output all in one block
1882 
1883   tty->print("Compiled ");
1884 
1885   if (is_compiled_by_c1()) {
1886     tty->print("(c1) ");
1887   } else if (is_compiled_by_c2()) {
1888     tty->print("(c2) ");
1889   } else {
1890     assert(is_native_method(), "Who else?");
1891     tty->print("(nm) ");
1892   }
1893 
1894   print_on(tty, "nmethod");
1895   tty->cr();
1896   if (WizardMode) {
1897     tty->print("((nmethod*) "INTPTR_FORMAT ") ", this);
1898     tty->print(" for method " INTPTR_FORMAT , (address)method());
1899     tty->print(" { ");
1900     if (version())        tty->print("v%d ", version());
1901     if (level())          tty->print("l%d ", level());
1902     if (is_in_use())      tty->print("in_use ");
1903     if (is_not_entrant()) tty->print("not_entrant ");
1904     if (is_zombie())      tty->print("zombie ");
1905     if (is_unloaded())    tty->print("unloaded ");
1906     tty->print_cr("}:");  
1907   }
1908   if (size              () > 0) tty->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1909                                               (address)this,
1910                                               (address)this + size(),


1934                                               scopes_pcs_end(),
1935                                               scopes_pcs_size());
1936   if (dependencies_size () > 0) tty->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1937                                               dependencies_begin(),
1938                                               dependencies_end(),
1939                                               dependencies_size());
1940   if (handler_table_size() > 0) tty->print_cr(" handler table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1941                                               handler_table_begin(),
1942                                               handler_table_end(),
1943                                               handler_table_size());
1944   if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1945                                               nul_chk_table_begin(),
1946                                               nul_chk_table_end(),
1947                                               nul_chk_table_size());
1948   if (oops_size         () > 0) tty->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
1949                                               oops_begin(),
1950                                               oops_end(),
1951                                               oops_size());
1952 }
1953 








1954 
1955 void nmethod::print_scopes() {
1956   // Find the first pc desc for all scopes in the code and print it.
1957   ResourceMark rm;
1958   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
1959     if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
1960       continue;
1961 
1962     ScopeDesc* sd = scope_desc_at(p->real_pc(this));
1963     sd->print_on(tty, p);
1964   }
1965 }
1966 
1967 void nmethod::print_dependencies() {
1968   ResourceMark rm;
1969   ttyLocker ttyl;   // keep the following output all in one block
1970   tty->print_cr("Dependencies:");
1971   for (Dependencies::DepStream deps(this); deps.next(); ) {
1972     deps.print_dependency();
1973     klassOop ctxk = deps.context_type();
1974     if (ctxk != NULL) {
1975       Klass* k = Klass::cast(ctxk);
1976       if (k->oop_is_instance() && ((instanceKlass*)k)->is_dependent_nmethod(this)) {
1977         tty->print_cr("   [nmethod<=klass]%s", k->external_name());
1978       }
1979     }
1980     deps.log_dependency();  // put it into the xml log also
1981   }
1982 }
1983 
1984 
1985 void nmethod::print_code() {
1986   HandleMark hm;
1987   ResourceMark m;
1988   Disassembler().decode(this);
1989 }
1990 
1991 
1992 void nmethod::print_relocations() {
1993   ResourceMark m;       // in case methods get printed via the debugger
1994   tty->print_cr("relocations:");
1995   RelocIterator iter(this);
1996   iter.print();
1997   if (UseRelocIndex) {
1998     jint* index_end   = (jint*)relocation_end() - 1;
1999     jint  index_size  = *index_end;
2000     jint* index_start = (jint*)( (address)index_end - index_size );
2001     tty->print_cr("    index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size);
2002     if (index_size > 0) {
2003       jint* ip;
2004       for (ip = index_start; ip+2 <= index_end; ip += 2)
2005         tty->print_cr("  (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT,
2006                       ip[0],
2007                       ip[1],
2008                       header_end()+ip[0],
2009                       relocation_begin()-1+ip[1]);
2010       for (; ip < index_end; ip++)
2011         tty->print_cr("  (%d ?)", ip[0]);
2012       tty->print_cr("          @" INTPTR_FORMAT ": index_size=%d", ip, *ip++);
2013       tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip);
2014     }
2015   }
2016 }
2017 
2018 
2019 void nmethod::print_pcs() {
2020   ResourceMark m;       // in case methods get printed via debugger
2021   tty->print_cr("pc-bytecode offsets:");
2022   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2023     p->print(this);
2024   }
2025 }
2026 

2027 
2028 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) {
2029   RelocIterator iter(this, begin, end);
2030   bool have_one = false;
2031   while (iter.next()) {
2032     have_one = true;
2033     switch (iter.type()) {
2034         case relocInfo::none:                  return "no_reloc";
2035         case relocInfo::oop_type: {
2036           stringStream st;
2037           oop_Relocation* r = iter.oop_reloc();
2038           oop obj = r->oop_value();
2039           st.print("oop(");
2040           if (obj == NULL) st.print("NULL");
2041           else obj->print_value_on(&st);
2042           st.print(")");
2043           return st.as_string();
2044         }
2045         case relocInfo::virtual_call_type:     return "virtual_call";
2046         case relocInfo::opt_virtual_call_type: return "optimized virtual_call";
2047         case relocInfo::static_call_type:      return "static_call";
2048         case relocInfo::static_stub_type:      return "static_stub";        
2049         case relocInfo::runtime_call_type:     return "runtime_call";
2050         case relocInfo::external_word_type:    return "external_word";
2051         case relocInfo::internal_word_type:    return "internal_word";
2052         case relocInfo::section_word_type:     return "section_word";
2053         case relocInfo::poll_type:             return "poll";
2054         case relocInfo::poll_return_type:      return "poll_return";
2055         case relocInfo::type_mask:             return "type_bit_mask";
2056     }
2057   }
2058   return have_one ? "other" : NULL;
2059 }
2060 
2061 
2062 // Return a the last scope in (begin..end]
2063 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
2064   PcDesc* p = pc_desc_near(begin+1);
2065   if (p != NULL && p->real_pc(this) <= end) {
2066     return new ScopeDesc(this, p->scope_decode_offset(),
2067                          p->obj_decode_offset());
2068   }
2069   return NULL;
2070 }
2071 
2072 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) {
2073   // First, find an oopmap in (begin, end].
2074   // We use the odd half-closed interval so that oop maps and scope descs
2075   // which are tied to the byte after a call are printed with the call itself.
2076   address base = instructions_begin();
2077   OopMapSet* oms = oop_maps();
2078   if (oms != NULL) {
2079     for (int i = 0, imax = oms->size(); i < imax; i++) {
2080       OopMap* om = oms->at(i);
2081       address pc = base + om->offset();
2082       if (pc > begin) {
2083         if (pc <= end) {
2084           st->fill_to(column);
2085           if (st == tty) {
2086             st->print("; OopMap ");
2087             om->print();
2088             tty->cr();
2089           } else {
2090             st->print_cr("; OopMap #%d offset:%d", i, om->offset());
2091           }
2092         }
2093         break;
2094       }
2095     }
2096   }


2097   ScopeDesc* sd  = scope_desc_in(begin, end);  
2098   if (sd != NULL) {
2099     st->fill_to(column);
2100     if (sd->bci() == SynchronizationEntryBCI) {
2101       st->print(";*synchronization entry");
2102     } else {
2103       if (sd->method().is_null()) {
2104         tty->print("method is NULL");
2105       } else if (sd->method()->is_native()) {
2106         tty->print("method is native");
2107       } else {
2108         address bcp  = sd->method()->bcp_from(sd->bci());
2109         Bytecodes::Code bc = Bytecodes::java_code_at(bcp);
2110         st->print(";*%s", Bytecodes::name(bc));
2111         switch (bc) {
2112         case Bytecodes::_invokevirtual:
2113         case Bytecodes::_invokespecial:
2114         case Bytecodes::_invokestatic:
2115         case Bytecodes::_invokeinterface:
2116           {
2117             Bytecode_invoke* invoke = Bytecode_invoke_at(sd->method(), sd->bci());
2118             st->print(" ");
2119             if (invoke->name() != NULL)
2120               invoke->name()->print_symbol_on(st);
2121             else
2122               st->print("<UNKNOWN>");
2123             break;
2124           }
2125         case Bytecodes::_getfield:
2126         case Bytecodes::_putfield:
2127         case Bytecodes::_getstatic:
2128         case Bytecodes::_putstatic:
2129           {
2130             methodHandle sdm = sd->method();
2131             Bytecode_field* field = Bytecode_field_at(sdm(), sdm->bcp_from(sd->bci()));
2132             constantPoolOop sdmc = sdm->constants();
2133             symbolOop name = sdmc->name_ref_at(field->index());
2134             st->print(" ");
2135             if (name != NULL)
2136               name->print_symbol_on(st);
2137             else
2138               st->print("<UNKNOWN>");
2139           }
2140         }
2141       }
2142     }
2143     st->cr();
2144     // Print all scopes
2145     for (;sd != NULL; sd = sd->sender()) {
2146       st->fill_to(column);
2147       st->print("; -");
2148       if (sd->method().is_null()) {
2149         tty->print("method is NULL");
2150       } else {
2151         sd->method()->print_short_name(st);
2152       }
2153       int lineno = sd->method()->line_number_from_bci(sd->bci());
2154       if (lineno != -1) {
2155         st->print("@%d (line %d)", sd->bci(), lineno);
2156       } else {
2157         st->print("@%d", sd->bci());
2158       }
2159       st->cr();
2160     }
2161   }
2162 
2163   // Print relocation information
2164   const char* str = reloc_string_for(begin, end);
2165   if (str != NULL) {
2166     if (sd != NULL) st->cr();
2167     st->fill_to(column);
2168     st->print(";   {%s}", str);
2169   }
2170   int cont_offset = ImplicitExceptionTable(this).at(begin - instructions_begin());
2171   if (cont_offset != 0) {
2172     st->fill_to(column);
2173     st->print("; implicit exception: dispatches to " INTPTR_FORMAT, instructions_begin() + cont_offset);
2174   }
2175 
2176 }
2177 


2178 void nmethod::print_value_on(outputStream* st) const {
2179   print_on(st, "nmethod");
2180 }
2181 
2182 void nmethod::print_calls(outputStream* st) {
2183   RelocIterator iter(this);
2184   while (iter.next()) {
2185     switch (iter.type()) {
2186     case relocInfo::virtual_call_type:
2187     case relocInfo::opt_virtual_call_type: {
2188       VerifyMutexLocker mc(CompiledIC_lock);
2189       CompiledIC_at(iter.reloc())->print();
2190       break;
2191     }
2192     case relocInfo::static_call_type:
2193       st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr());
2194       compiledStaticCall_at(iter.reloc())->print();
2195       break;
2196     }
2197   }


   1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)nmethod.cpp  1.371 08/02/29 12:46:11 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_nmethod.cpp.incl"
  30 
  31 #ifdef DTRACE_ENABLED
  32 

  33 // Only bother with this argument setup if dtrace is available
  34 
  35 HS_DTRACE_PROBE_DECL8(hotspot, compiled__method__load, 
  36   const char*, int, const char*, int, const char*, int, void*, size_t);
  37 
  38 HS_DTRACE_PROBE_DECL6(hotspot, compiled__method__unload, 
  39   char*, int, char*, int, char*, int);
  40 
  41 #define DTRACE_METHOD_UNLOAD_PROBE(method)                                \
  42   {                                                                       \
  43     methodOop m = (method);                                               \
  44     if (m != NULL) {                                                      \
  45       symbolOop klass_name = m->klass_name();                             \
  46       symbolOop name = m->name();                                         \
  47       symbolOop signature = m->signature();                               \
  48       HS_DTRACE_PROBE6(hotspot, compiled__method__unload,                 \
  49         klass_name->bytes(), klass_name->utf8_length(),                   \
  50         name->bytes(), name->utf8_length(),                               \
  51         signature->bytes(), signature->utf8_length());                    \
  52     }                                                                     \


 423   return NULL;
 424 }
 425 
 426 // %%% This variable is no longer used?
 427 int nmethod::_zombie_instruction_size = NativeJump::instruction_size;
 428 
 429 
 430 nmethod* nmethod::new_native_nmethod(methodHandle method,
 431   CodeBuffer *code_buffer, 
 432   int vep_offset, 
 433   int frame_complete, 
 434   int frame_size, 
 435   ByteSize basic_lock_owner_sp_offset,
 436   ByteSize basic_lock_sp_offset,
 437   OopMapSet* oop_maps) {
 438   // create nmethod
 439   nmethod* nm = NULL;
 440   {
 441     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 442     int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod));

 443     CodeOffsets offsets;
 444     offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
 445     offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
 446     nm = new (native_nmethod_size)
 447       nmethod(method(), native_nmethod_size, &offsets,
 448               code_buffer, frame_size,
 449               basic_lock_owner_sp_offset, basic_lock_sp_offset,
 450               oop_maps);
 451     NOT_PRODUCT(if (nm != NULL)  nmethod_stats.note_native_nmethod(nm));
 452     if (PrintAssembly && nm != NULL)
 453       Disassembler::decode(nm);
 454   }
 455   // verify nmethod
 456   debug_only(if (nm) nm->verify();) // might block
 457 
 458   if (nm != NULL) {
 459     nm->log_new_nmethod();
 460   }
 461 
 462   return nm;
 463 }
 464 
 465 #ifdef HAVE_DTRACE_H
 466 nmethod* nmethod::new_dtrace_nmethod(methodHandle method,
 467                                      CodeBuffer *code_buffer,
 468                                      int vep_offset,
 469                                      int trap_offset,
 470                                      int frame_complete,
 471                                      int frame_size) {
 472   // create nmethod
 473   nmethod* nm = NULL;
 474   {
 475     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 476     int nmethod_size = allocation_size(code_buffer, sizeof(nmethod));
 477     CodeOffsets offsets;
 478     offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
 479     offsets.set_value(CodeOffsets::Dtrace_trap, trap_offset);
 480     offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
 481 
 482     nm = new (nmethod_size) nmethod(method(), nmethod_size, &offsets, code_buffer, frame_size);
 483 
 484     NOT_PRODUCT(if (nm != NULL)  nmethod_stats.note_nmethod(nm));
 485     if (PrintAssembly && nm != NULL)
 486       Disassembler::decode(nm);
 487   }
 488   // verify nmethod
 489   debug_only(if (nm) nm->verify();) // might block
 490 
 491   if (nm != NULL) {
 492     nm->log_new_nmethod();
 493   }
 494 
 495   return nm;
 496 }
 497 
 498 #endif // def HAVE_DTRACE_H
 499 
 500 nmethod* nmethod::new_nmethod(methodHandle method,
 501   int compile_id,
 502   int entry_bci,
 503   CodeOffsets* offsets,
 504   int orig_pc_offset,
 505   DebugInformationRecorder* debug_info, 
 506   Dependencies* dependencies,
 507   CodeBuffer* code_buffer, int frame_size, 
 508   OopMapSet* oop_maps, 
 509   ExceptionHandlerTable* handler_table, 
 510   ImplicitExceptionTable* nul_chk_table,
 511   AbstractCompiler* compiler,
 512   int comp_level
 513 )
 514 {
 515   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
 516   // create nmethod
 517   nmethod* nm = NULL;
 518   { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 519     int nmethod_size =


 577   OopMapSet* oop_maps )
 578   : CodeBlob("native nmethod", code_buffer, sizeof(nmethod),
 579              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
 580   _compiled_synchronized_native_basic_lock_owner_sp_offset(basic_lock_owner_sp_offset),
 581   _compiled_synchronized_native_basic_lock_sp_offset(basic_lock_sp_offset)
 582 {
 583   {
 584     debug_only(No_Safepoint_Verifier nsv;)
 585     assert_locked_or_safepoint(CodeCache_lock);
 586 
 587     NOT_PRODUCT(_has_debug_info = false; )
 588     _method                  = method;
 589     _entry_bci               = InvocationEntryBci;
 590     _link                    = NULL;
 591     _compiler                = NULL;
 592     // We have no exception handler or deopt handler make the
 593     // values something that will never match a pc like the nmethod vtable entry
 594     _exception_offset        = 0;
 595     _deoptimize_offset       = 0;
 596     _orig_pc_offset          = 0;
 597 #ifdef HAVE_DTRACE_H
 598     _trap_offset             = 0;
 599 #endif // def HAVE_DTRACE_H
 600     _stub_offset             = data_offset();
 601     _consts_offset           = data_offset();
 602     _scopes_data_offset      = data_offset();
 603     _scopes_pcs_offset       = _scopes_data_offset;
 604     _dependencies_offset     = _scopes_pcs_offset;
 605     _handler_table_offset    = _dependencies_offset;
 606     _nul_chk_table_offset    = _handler_table_offset;
 607     _nmethod_end_offset      = _nul_chk_table_offset;
 608     _compile_id              = 0;  // default
 609     _comp_level              = CompLevel_none;
 610     _entry_point             = instructions_begin();
 611     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
 612     _osr_entry_point         = NULL;
 613     _exception_cache         = NULL;
 614     _pc_desc_cache.reset_to(NULL);
 615 
 616     flags.clear();
 617     flags.state              = alive;
 618     _markedForDeoptimization = 0;
 619 


 637       xtty->stamp();
 638       xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
 639     }
 640     // print the header part first
 641     print();
 642     // then print the requested information
 643     if (PrintNativeNMethods) {
 644       print_code();
 645       oop_maps->print();
 646     }
 647     if (PrintRelocations) {
 648       print_relocations();
 649     }
 650     if (xtty != NULL) {
 651       xtty->tail("print_native_nmethod");
 652     }
 653   }
 654   Events::log("Create nmethod " INTPTR_FORMAT, this);
 655 }
 656 
 657 // For dtrace wrappers
 658 #ifdef HAVE_DTRACE_H
 659 nmethod::nmethod(
 660   methodOop method,
 661   int nmethod_size,
 662   CodeOffsets* offsets,
 663   CodeBuffer* code_buffer,
 664   int frame_size)
 665   : CodeBlob("dtrace nmethod", code_buffer, sizeof(nmethod),
 666              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, NULL),
 667   _compiled_synchronized_native_basic_lock_owner_sp_offset(in_ByteSize(-1)),
 668   _compiled_synchronized_native_basic_lock_sp_offset(in_ByteSize(-1))
 669 {
 670   {
 671     debug_only(No_Safepoint_Verifier nsv;)
 672     assert_locked_or_safepoint(CodeCache_lock);
 673 
 674     NOT_PRODUCT(_has_debug_info = false; )
 675     _method                  = method;
 676     _entry_bci               = InvocationEntryBci;
 677     _link                    = NULL;
 678     _compiler                = NULL;
 679     // We have no exception handler or deopt handler make the
 680     // values something that will never match a pc like the nmethod vtable entry
 681     _exception_offset        = 0;
 682     _deoptimize_offset       = 0;
 683     _trap_offset             = offsets->value(CodeOffsets::Dtrace_trap);
 684     _orig_pc_offset          = 0;
 685     _stub_offset             = data_offset();
 686     _consts_offset           = data_offset();
 687     _scopes_data_offset      = data_offset();
 688     _scopes_pcs_offset       = _scopes_data_offset;
 689     _dependencies_offset     = _scopes_pcs_offset;
 690     _handler_table_offset    = _dependencies_offset;
 691     _nul_chk_table_offset    = _handler_table_offset;
 692     _nmethod_end_offset      = _nul_chk_table_offset;
 693     _compile_id              = 0;  // default
 694     _comp_level              = CompLevel_none;
 695     _entry_point             = instructions_begin();
 696     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
 697     _osr_entry_point         = NULL;
 698     _exception_cache         = NULL;
 699     _pc_desc_cache.reset_to(NULL);
 700 
 701     flags.clear();
 702     flags.state              = alive;
 703     _markedForDeoptimization = 0;
 704 
 705     _lock_count = 0;
 706     _stack_traversal_mark    = 0;
 707 
 708     code_buffer->copy_oops_to(this);
 709     debug_only(check_store();)
 710     CodeCache::commit(this);
 711     VTune::create_nmethod(this);
 712   }
 713 
 714   if (PrintNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) {
 715     ttyLocker ttyl;  // keep the following output all in one block
 716     // This output goes directly to the tty, not the compiler log.
 717     // To enable tools to match it up with the compilation activity,
 718     // be sure to tag this tty output with the compile ID.
 719     if (xtty != NULL) {
 720       xtty->begin_head("print_dtrace_nmethod");
 721       xtty->method(_method);
 722       xtty->stamp();
 723       xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
 724     }
 725     // print the header part first
 726     print();
 727     // then print the requested information
 728     if (PrintNMethods) {
 729       print_code();
 730     }
 731     if (PrintRelocations) {
 732       print_relocations();
 733     }
 734     if (xtty != NULL) {
 735       xtty->tail("print_dtrace_nmethod");
 736     }
 737   }
 738   Events::log("Create nmethod " INTPTR_FORMAT, this);
 739 }
 740 #endif // def HAVE_DTRACE_H
 741 
 742 void* nmethod::operator new(size_t size, int nmethod_size) {  
 743   // Always leave some room in the CodeCache for I2C/C2I adapters  
 744   if (CodeCache::unallocated_capacity() < CodeCacheMinimumFreeSpace) return NULL;
 745   return CodeCache::allocate(nmethod_size);    
 746 }
 747 
 748 
 749 nmethod::nmethod(
 750   methodOop method,
 751   int nmethod_size,
 752   int compile_id,
 753   int entry_bci,
 754   CodeOffsets* offsets,
 755   int orig_pc_offset,  
 756   DebugInformationRecorder* debug_info,
 757   Dependencies* dependencies,
 758   CodeBuffer *code_buffer,
 759   int frame_size,
 760   OopMapSet* oop_maps,


 764   int comp_level
 765   )
 766   : CodeBlob("nmethod", code_buffer, sizeof(nmethod),
 767              nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
 768   _compiled_synchronized_native_basic_lock_owner_sp_offset(in_ByteSize(-1)),
 769   _compiled_synchronized_native_basic_lock_sp_offset(in_ByteSize(-1))
 770 {
 771   assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
 772   {
 773     debug_only(No_Safepoint_Verifier nsv;)
 774     assert_locked_or_safepoint(CodeCache_lock);
 775 
 776     NOT_PRODUCT(_has_debug_info = false; )
 777     _method                  = method;
 778     _compile_id              = compile_id;
 779     _comp_level              = comp_level;
 780     _entry_bci               = entry_bci;
 781     _link                    = NULL;
 782     _compiler                = compiler;
 783     _orig_pc_offset          = orig_pc_offset;
 784 #ifdef HAVE_DTRACE_H
 785     _trap_offset             = 0;
 786 #endif // def HAVE_DTRACE_H
 787     _stub_offset             = instructions_offset() + code_buffer->total_offset_of(code_buffer->stubs()->start());
 788 
 789     // Exception handler and deopt handler are in the stub section
 790     _exception_offset        = _stub_offset + offsets->value(CodeOffsets::Exceptions);
 791     _deoptimize_offset       = _stub_offset + offsets->value(CodeOffsets::Deopt);
 792     _consts_offset           = instructions_offset() + code_buffer->total_offset_of(code_buffer->consts()->start());
 793     _scopes_data_offset      = data_offset();
 794     _scopes_pcs_offset       = _scopes_data_offset   + round_to(debug_info->data_size         (), oopSize);
 795     _dependencies_offset     = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
 796     _handler_table_offset    = _dependencies_offset  + round_to(dependencies->size_in_bytes (), oopSize);
 797     _nul_chk_table_offset    = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize);
 798     _nmethod_end_offset      = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize);
 799  
 800     _entry_point             = instructions_begin();
 801     _verified_entry_point    = instructions_begin() + offsets->value(CodeOffsets::Verified_Entry);
 802     _osr_entry_point         = instructions_begin() + offsets->value(CodeOffsets::OSR_Entry);
 803     _exception_cache         = NULL;
 804     _pc_desc_cache.reset_to(scopes_pcs_begin());
 805 
 806     flags.clear();


 816     code_buffer->copy_oops_to(this);
 817     debug_info->copy_to(this);
 818     dependencies->copy_to(this);
 819     debug_only(check_store();)
 820 
 821     CodeCache::commit(this);
 822   
 823     VTune::create_nmethod(this);
 824 
 825     // Copy contents of ExceptionHandlerTable to nmethod
 826     handler_table->copy_to(this);
 827     nul_chk_table->copy_to(this);
 828 
 829     // we use the information of entry points to find out if a method is
 830     // static or non static
 831     assert(compiler->is_c2() || 
 832            _method->is_static() == (entry_point() == _verified_entry_point),
 833            " entry points must be same for static methods and vice versa");
 834   }
 835 
 836   bool printnmethods = PrintNMethods
 837     || CompilerOracle::should_print(_method)
 838     || CompilerOracle::has_option_string(_method, "PrintNMethods");
 839   if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) {
 840     print_nmethod(printnmethods);
 841   }
 842 
 843   // Note: Do not verify in here as the CodeCache_lock is
 844   //       taken which would conflict with the CompiledIC_lock
 845   //       which taken during the verification of call sites.
 846   //       (was bug - gri 10/25/99)
 847 
 848   Events::log("Create nmethod " INTPTR_FORMAT, this);
 849 }
 850 
 851 
 852 // Print a short set of xml attributes to identify this nmethod.  The
 853 // output should be embedded in some other element.
 854 void nmethod::log_identity(xmlStream* log) const {
 855   log->print(" compile_id='%d'", compile_id());
 856   const char* nm_kind = compile_kind();
 857   if (nm_kind != NULL)  log->print(" compile_kind='%s'", nm_kind);
 858   if (compiler() != NULL) {


 909               compile_id(),
 910               is_osr_method() ? '%' :
 911               method() != NULL &&
 912               is_native_method() ? 'n' : ' ',
 913               title);
 914 #ifdef TIERED
 915     st->print(" (%d) ", comp_level());
 916 #endif // TIERED
 917     if (WizardMode) st->print(" (" INTPTR_FORMAT ")", this);
 918     if (method() != NULL) {
 919       method()->print_short_name(st);
 920       if (is_osr_method())
 921         st->print(" @ %d", osr_entry_bci());
 922       if (method()->code_size() > 0)
 923         st->print(" (%d bytes)", method()->code_size());
 924     }
 925   }
 926 }
 927 
 928 

 929 void nmethod::print_nmethod(bool printmethod) {
 930   ttyLocker ttyl;  // keep the following output all in one block
 931   if (xtty != NULL) {
 932     xtty->begin_head("print_nmethod");
 933     xtty->stamp();
 934     xtty->end_head();
 935   }
 936   // print the header part first
 937   print();
 938   // then print the requested information
 939   if (printmethod) {
 940     print_code();
 941     print_pcs();
 942     oop_maps()->print();
 943   }
 944   if (PrintDebugInfo) {
 945     print_scopes();
 946   }
 947   if (PrintRelocations) {
 948     print_relocations();
 949   }
 950   if (PrintDependencies) {
 951     print_dependencies();
 952   }
 953   if (PrintExceptionHandlers) {
 954     print_handler_table();
 955     print_nul_chk_table();
 956   }
 957   if (xtty != NULL) {
 958     xtty->tail("print_nmethod");
 959   }
 960 }

 961 
 962 
 963 void nmethod::set_version(int v) {
 964   flags.version = v;
 965 }
 966 
 967 
 968 ScopeDesc* nmethod::scope_desc_at(address pc) {
 969   PcDesc* pd = pc_desc_at(pc);
 970   guarantee(pd != NULL, "scope must be present");
 971   return new ScopeDesc(this, pd->scope_decode_offset(),
 972                        pd->obj_decode_offset());
 973 }
 974 
 975 
 976 void nmethod::clear_inline_caches() {  
 977   assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
 978   if (is_zombie()) {
 979     return;
 980   }


1336 
1337 // If this oop is not live, the nmethod can be unloaded.
1338 bool nmethod::can_unload(BoolObjectClosure* is_alive,
1339                          OopClosure* keep_alive,
1340                          oop* root, bool unloading_occurred) {
1341   assert(root != NULL, "just checking");
1342   oop obj = *root;
1343   if (obj == NULL || is_alive->do_object_b(obj)) {
1344       return false;
1345   }
1346   if (obj->is_compiledICHolder()) {
1347     compiledICHolderOop cichk_oop = compiledICHolderOop(obj);
1348     if (is_alive->do_object_b(
1349           cichk_oop->holder_method()->method_holder()) &&
1350         is_alive->do_object_b(cichk_oop->holder_klass())) {
1351       // The oop should be kept alive
1352       keep_alive->do_oop(root);
1353       return false;
1354     }
1355   }



1356   assert(unloading_occurred, "Inconsistency in unloading");

1357   make_unloaded(is_alive, obj);
1358   return true;
1359 }
1360 
1361 // ------------------------------------------------------------------
1362 // post_compiled_method_load_event
1363 // new method for install_code() path
1364 // Transfer information from compilation to jvmti
1365 void nmethod::post_compiled_method_load_event() {
1366 
1367   methodOop moop = method();
1368   HS_DTRACE_PROBE8(hotspot, compiled__method__load, 
1369       moop->klass_name()->bytes(), 
1370       moop->klass_name()->utf8_length(),
1371       moop->name()->bytes(), 
1372       moop->name()->utf8_length(),
1373       moop->signature()->bytes(), 
1374       moop->signature()->utf8_length(),
1375       code_begin(), code_size());
1376 


1975 
1976 // -----------------------------------------------------------------------------
1977 // Non-product code
1978 #ifndef PRODUCT
1979 
1980 void nmethod::check_store() {
1981   // Make sure all oops in the compiled code are tenured
1982 
1983   RelocIterator iter(this);
1984   while (iter.next()) {
1985     if (iter.type() == relocInfo::oop_type) {
1986       oop_Relocation* reloc = iter.oop_reloc();
1987       oop obj = reloc->oop_value();
1988       if (obj != NULL && !obj->is_perm()) {
1989         fatal("must be permanent oop in compiled code");
1990       }
1991     }
1992   }
1993 }
1994 
1995 #endif // PRODUCT
1996 
1997 // Printing operations
1998 
1999 void nmethod::print() const {
2000   ResourceMark rm;
2001   ttyLocker ttyl;   // keep the following output all in one block
2002 
2003   tty->print("Compiled ");
2004 
2005   if (is_compiled_by_c1()) {
2006     tty->print("(c1) ");
2007   } else if (is_compiled_by_c2()) {
2008     tty->print("(c2) ");
2009   } else {

2010     tty->print("(nm) ");
2011   }
2012 
2013   print_on(tty, "nmethod");
2014   tty->cr();
2015   if (WizardMode) {
2016     tty->print("((nmethod*) "INTPTR_FORMAT ") ", this);
2017     tty->print(" for method " INTPTR_FORMAT , (address)method());
2018     tty->print(" { ");
2019     if (version())        tty->print("v%d ", version());
2020     if (level())          tty->print("l%d ", level());
2021     if (is_in_use())      tty->print("in_use ");
2022     if (is_not_entrant()) tty->print("not_entrant ");
2023     if (is_zombie())      tty->print("zombie ");
2024     if (is_unloaded())    tty->print("unloaded ");
2025     tty->print_cr("}:");  
2026   }
2027   if (size              () > 0) tty->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2028                                               (address)this,
2029                                               (address)this + size(),


2053                                               scopes_pcs_end(),
2054                                               scopes_pcs_size());
2055   if (dependencies_size () > 0) tty->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2056                                               dependencies_begin(),
2057                                               dependencies_end(),
2058                                               dependencies_size());
2059   if (handler_table_size() > 0) tty->print_cr(" handler table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2060                                               handler_table_begin(),
2061                                               handler_table_end(),
2062                                               handler_table_size());
2063   if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2064                                               nul_chk_table_begin(),
2065                                               nul_chk_table_end(),
2066                                               nul_chk_table_size());
2067   if (oops_size         () > 0) tty->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2068                                               oops_begin(),
2069                                               oops_end(),
2070                                               oops_size());
2071 }
2072 
2073 void nmethod::print_code() {
2074   HandleMark hm;
2075   ResourceMark m;
2076   Disassembler::decode(this);
2077 }
2078 
2079 
2080 #ifndef PRODUCT
2081 
2082 void nmethod::print_scopes() {
2083   // Find the first pc desc for all scopes in the code and print it.
2084   ResourceMark rm;
2085   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2086     if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
2087       continue;
2088 
2089     ScopeDesc* sd = scope_desc_at(p->real_pc(this));
2090     sd->print_on(tty, p);
2091   }
2092 }
2093 
2094 void nmethod::print_dependencies() {
2095   ResourceMark rm;
2096   ttyLocker ttyl;   // keep the following output all in one block
2097   tty->print_cr("Dependencies:");
2098   for (Dependencies::DepStream deps(this); deps.next(); ) {
2099     deps.print_dependency();
2100     klassOop ctxk = deps.context_type();
2101     if (ctxk != NULL) {
2102       Klass* k = Klass::cast(ctxk);
2103       if (k->oop_is_instance() && ((instanceKlass*)k)->is_dependent_nmethod(this)) {
2104         tty->print_cr("   [nmethod<=klass]%s", k->external_name());
2105       }
2106     }
2107     deps.log_dependency();  // put it into the xml log also
2108   }
2109 }
2110 
2111 







2112 void nmethod::print_relocations() {
2113   ResourceMark m;       // in case methods get printed via the debugger
2114   tty->print_cr("relocations:");
2115   RelocIterator iter(this);
2116   iter.print();
2117   if (UseRelocIndex) {
2118     jint* index_end   = (jint*)relocation_end() - 1;
2119     jint  index_size  = *index_end;
2120     jint* index_start = (jint*)( (address)index_end - index_size );
2121     tty->print_cr("    index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size);
2122     if (index_size > 0) {
2123       jint* ip;
2124       for (ip = index_start; ip+2 <= index_end; ip += 2)
2125         tty->print_cr("  (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT,
2126                       ip[0],
2127                       ip[1],
2128                       header_end()+ip[0],
2129                       relocation_begin()-1+ip[1]);
2130       for (; ip < index_end; ip++)
2131         tty->print_cr("  (%d ?)", ip[0]);
2132       tty->print_cr("          @" INTPTR_FORMAT ": index_size=%d", ip, *ip++);
2133       tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip);
2134     }
2135   }
2136 }
2137 
2138 
2139 void nmethod::print_pcs() {
2140   ResourceMark m;       // in case methods get printed via debugger
2141   tty->print_cr("pc-bytecode offsets:");
2142   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2143     p->print(this);
2144   }
2145 }
2146 
2147 #endif // PRODUCT
2148 
2149 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) {
2150   RelocIterator iter(this, begin, end);
2151   bool have_one = false;
2152   while (iter.next()) {
2153     have_one = true;
2154     switch (iter.type()) {
2155         case relocInfo::none:                  return "no_reloc";
2156         case relocInfo::oop_type: {
2157           stringStream st;
2158           oop_Relocation* r = iter.oop_reloc();
2159           oop obj = r->oop_value();
2160           st.print("oop(");
2161           if (obj == NULL) st.print("NULL");
2162           else obj->print_value_on(&st);
2163           st.print(")");
2164           return st.as_string();
2165         }
2166         case relocInfo::virtual_call_type:     return "virtual_call";
2167         case relocInfo::opt_virtual_call_type: return "optimized virtual_call";
2168         case relocInfo::static_call_type:      return "static_call";
2169         case relocInfo::static_stub_type:      return "static_stub";        
2170         case relocInfo::runtime_call_type:     return "runtime_call";
2171         case relocInfo::external_word_type:    return "external_word";
2172         case relocInfo::internal_word_type:    return "internal_word";
2173         case relocInfo::section_word_type:     return "section_word";
2174         case relocInfo::poll_type:             return "poll";
2175         case relocInfo::poll_return_type:      return "poll_return";
2176         case relocInfo::type_mask:             return "type_bit_mask";
2177     }
2178   }
2179   return have_one ? "other" : NULL;
2180 }
2181 

2182 // Return a the last scope in (begin..end]
2183 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
2184   PcDesc* p = pc_desc_near(begin+1);
2185   if (p != NULL && p->real_pc(this) <= end) {
2186     return new ScopeDesc(this, p->scope_decode_offset(),
2187                          p->obj_decode_offset());
2188   }
2189   return NULL;
2190 }
2191 
2192 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) {
2193   // First, find an oopmap in (begin, end].
2194   // We use the odd half-closed interval so that oop maps and scope descs
2195   // which are tied to the byte after a call are printed with the call itself.
2196   address base = instructions_begin();
2197   OopMapSet* oms = oop_maps();
2198   if (oms != NULL) {
2199     for (int i = 0, imax = oms->size(); i < imax; i++) {
2200       OopMap* om = oms->at(i);
2201       address pc = base + om->offset();
2202       if (pc > begin) {
2203         if (pc <= end) {
2204           st->move_to(column);
2205           st->print("; ");
2206           om->print_on(st);





2207         }
2208         break;
2209       }
2210     }
2211   }
2212 
2213   // Print any debug info present at this pc.
2214   ScopeDesc* sd  = scope_desc_in(begin, end);
2215   if (sd != NULL) {
2216     st->move_to(column);
2217     if (sd->bci() == SynchronizationEntryBCI) {
2218       st->print(";*synchronization entry");
2219     } else {
2220       if (sd->method().is_null()) {
2221         st->print("method is NULL");
2222       } else if (sd->method()->is_native()) {
2223         st->print("method is native");
2224       } else {
2225         address bcp  = sd->method()->bcp_from(sd->bci());
2226         Bytecodes::Code bc = Bytecodes::java_code_at(bcp);
2227         st->print(";*%s", Bytecodes::name(bc));
2228         switch (bc) {
2229         case Bytecodes::_invokevirtual:
2230         case Bytecodes::_invokespecial:
2231         case Bytecodes::_invokestatic:
2232         case Bytecodes::_invokeinterface:
2233           {
2234             Bytecode_invoke* invoke = Bytecode_invoke_at(sd->method(), sd->bci());
2235             st->print(" ");
2236             if (invoke->name() != NULL)
2237               invoke->name()->print_symbol_on(st);
2238             else
2239               st->print("<UNKNOWN>");
2240             break;
2241           }
2242         case Bytecodes::_getfield:
2243         case Bytecodes::_putfield:
2244         case Bytecodes::_getstatic:
2245         case Bytecodes::_putstatic:
2246           {
2247             methodHandle sdm = sd->method();
2248             Bytecode_field* field = Bytecode_field_at(sdm(), sdm->bcp_from(sd->bci()));
2249             constantPoolOop sdmc = sdm->constants();
2250             symbolOop name = sdmc->name_ref_at(field->index());
2251             st->print(" ");
2252             if (name != NULL)
2253               name->print_symbol_on(st);
2254             else
2255               st->print("<UNKNOWN>");
2256           }
2257         }
2258       }
2259     }
2260 
2261     // Print all scopes
2262     for (;sd != NULL; sd = sd->sender()) {
2263       st->move_to(column);
2264       st->print("; -");
2265       if (sd->method().is_null()) {
2266         st->print("method is NULL");
2267       } else {
2268         sd->method()->print_short_name(st);
2269       }
2270       int lineno = sd->method()->line_number_from_bci(sd->bci());
2271       if (lineno != -1) {
2272         st->print("@%d (line %d)", sd->bci(), lineno);
2273       } else {
2274         st->print("@%d", sd->bci());
2275       }
2276       st->cr();
2277     }
2278   }
2279 
2280   // Print relocation information
2281   const char* str = reloc_string_for(begin, end);
2282   if (str != NULL) {
2283     if (sd != NULL) st->cr();
2284     st->move_to(column);
2285     st->print(";   {%s}", str);
2286   }
2287   int cont_offset = ImplicitExceptionTable(this).at(begin - instructions_begin());
2288   if (cont_offset != 0) {
2289     st->move_to(column);
2290     st->print("; implicit exception: dispatches to " INTPTR_FORMAT, instructions_begin() + cont_offset);
2291   }
2292 
2293 }
2294 
2295 #ifndef PRODUCT
2296 
2297 void nmethod::print_value_on(outputStream* st) const {
2298   print_on(st, "nmethod");
2299 }
2300 
2301 void nmethod::print_calls(outputStream* st) {
2302   RelocIterator iter(this);
2303   while (iter.next()) {
2304     switch (iter.type()) {
2305     case relocInfo::virtual_call_type:
2306     case relocInfo::opt_virtual_call_type: {
2307       VerifyMutexLocker mc(CompiledIC_lock);
2308       CompiledIC_at(iter.reloc())->print();
2309       break;
2310     }
2311     case relocInfo::static_call_type:
2312       st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr());
2313       compiledStaticCall_at(iter.reloc())->print();
2314       break;
2315     }
2316   }