< prev index next >

src/hotspot/share/code/nmethod.cpp

Print this page
rev 54960 : 8213084: Rework and enhance Print[Opto]Assembly output
Reviewed-by: kvn, thartmann


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"

  27 #include "code/codeCache.hpp"
  28 #include "code/compiledIC.hpp"
  29 #include "code/compiledMethod.inline.hpp"
  30 #include "code/dependencies.hpp"
  31 #include "code/nativeInst.hpp"
  32 #include "code/nmethod.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "compiler/abstractCompiler.hpp"
  35 #include "compiler/compileBroker.hpp"
  36 #include "compiler/compileLog.hpp"
  37 #include "compiler/compilerDirectives.hpp"
  38 #include "compiler/directivesParser.hpp"
  39 #include "compiler/disassembler.hpp"
  40 #include "interpreter/bytecode.hpp"
  41 #include "logging/log.hpp"
  42 #include "logging/logStream.hpp"
  43 #include "memory/allocation.inline.hpp"
  44 #include "memory/resourceArea.hpp"
  45 #include "memory/universe.hpp"
  46 #include "oops/access.inline.hpp"


 439 #if INCLUDE_RTM_OPT
 440   _rtm_state               = NoRTM;
 441 #endif
 442 }
 443 
 444 nmethod* nmethod::new_native_nmethod(const methodHandle& method,
 445   int compile_id,
 446   CodeBuffer *code_buffer,
 447   int vep_offset,
 448   int frame_complete,
 449   int frame_size,
 450   ByteSize basic_lock_owner_sp_offset,
 451   ByteSize basic_lock_sp_offset,
 452   OopMapSet* oop_maps) {
 453   code_buffer->finalize_oop_references(method);
 454   // create nmethod
 455   nmethod* nm = NULL;
 456   {
 457     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 458     int native_nmethod_size = CodeBlob::allocation_size(code_buffer, sizeof(nmethod));

 459     CodeOffsets offsets;
 460     offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
 461     offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
 462     nm = new (native_nmethod_size, CompLevel_none) nmethod(method(), compiler_none, native_nmethod_size,

 463                                             compile_id, &offsets,
 464                                             code_buffer, frame_size,
 465                                             basic_lock_owner_sp_offset,
 466                                             basic_lock_sp_offset, oop_maps);

 467     NOT_PRODUCT(if (nm != NULL)  native_nmethod_stats.note_native_nmethod(nm));
 468   }
 469 
 470   if (nm != NULL) {
 471     // verify nmethod
 472     debug_only(nm->verify();) // might block
 473 
 474     nm->log_new_nmethod();
 475     nm->make_in_use();
 476   }
 477   return nm;
 478 }
 479 
 480 nmethod* nmethod::new_nmethod(const methodHandle& method,
 481   int compile_id,
 482   int entry_bci,
 483   CodeOffsets* offsets,
 484   int orig_pc_offset,
 485   DebugInformationRecorder* debug_info,
 486   Dependencies* dependencies,


 641 
 642     clear_unloading_state();
 643 
 644     Universe::heap()->register_nmethod(this);
 645     debug_only(Universe::heap()->verify_nmethod(this));
 646 
 647     CodeCache::commit(this);
 648   }
 649 
 650   if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) {
 651     ttyLocker ttyl;  // keep the following output all in one block
 652     // This output goes directly to the tty, not the compiler log.
 653     // To enable tools to match it up with the compilation activity,
 654     // be sure to tag this tty output with the compile ID.
 655     if (xtty != NULL) {
 656       xtty->begin_head("print_native_nmethod");
 657       xtty->method(_method);
 658       xtty->stamp();
 659       xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
 660     }
 661     // print the header part first
 662     print();
 663     // then print the requested information
 664     if (PrintNativeNMethods) {

 665       print_code();



 666       if (oop_maps != NULL) {
 667         oop_maps->print();



 668       }



 669     }


 670     if (PrintRelocations) {
 671       print_relocations();

 672     }


 673     if (xtty != NULL) {
 674       xtty->tail("print_native_nmethod");
 675     }
 676   }
 677 }
 678 
 679 void* nmethod::operator new(size_t size, int nmethod_size, int comp_level) throw () {
 680   return CodeCache::allocate(nmethod_size, CodeCache::get_code_blob_type(comp_level));
 681 }
 682 
 683 nmethod::nmethod(
 684   Method* method,
 685   CompilerType type,
 686   int nmethod_size,
 687   int compile_id,
 688   int entry_bci,
 689   CodeOffsets* offsets,
 690   int orig_pc_offset,
 691   DebugInformationRecorder* debug_info,
 692   Dependencies* dependencies,


 729     set_ctable_begin(header_begin() + _consts_offset);
 730 
 731 #if INCLUDE_JVMCI
 732     if (compiler->is_jvmci()) {
 733       // JVMCI might not produce any stub sections
 734       if (offsets->value(CodeOffsets::Exceptions) != -1) {
 735         _exception_offset        = code_offset()          + offsets->value(CodeOffsets::Exceptions);
 736       } else {
 737         _exception_offset = -1;
 738       }
 739       if (offsets->value(CodeOffsets::Deopt) != -1) {
 740         _deopt_handler_begin       = (address) this + code_offset()          + offsets->value(CodeOffsets::Deopt);
 741       } else {
 742         _deopt_handler_begin = NULL;
 743       }
 744       if (offsets->value(CodeOffsets::DeoptMH) != -1) {
 745         _deopt_mh_handler_begin  = (address) this + code_offset()          + offsets->value(CodeOffsets::DeoptMH);
 746       } else {
 747         _deopt_mh_handler_begin = NULL;
 748       }
 749     } else {
 750 #endif

 751     // Exception handler and deopt handler are in the stub section
 752     assert(offsets->value(CodeOffsets::Exceptions) != -1, "must be set");
 753     assert(offsets->value(CodeOffsets::Deopt     ) != -1, "must be set");
 754 
 755     _exception_offset       = _stub_offset          + offsets->value(CodeOffsets::Exceptions);
 756     _deopt_handler_begin    = (address) this + _stub_offset          + offsets->value(CodeOffsets::Deopt);
 757     if (offsets->value(CodeOffsets::DeoptMH) != -1) {
 758       _deopt_mh_handler_begin  = (address) this + _stub_offset          + offsets->value(CodeOffsets::DeoptMH);
 759     } else {
 760       _deopt_mh_handler_begin  = NULL;
 761     }
 762 #if INCLUDE_JVMCI
 763     }
 764 #endif
 765     if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
 766       _unwind_handler_offset = code_offset()         + offsets->value(CodeOffsets::UnwindHandler);
 767     } else {
 768       _unwind_handler_offset = -1;
 769     }
 770 
 771     _oops_offset             = data_offset();
 772     _metadata_offset         = _oops_offset          + align_up(code_buffer->total_oop_size(), oopSize);
 773     int scopes_data_offset   = _metadata_offset      + align_up(code_buffer->total_metadata_size(), wordSize);
 774 
 775     _scopes_pcs_offset       = scopes_data_offset    + align_up(debug_info->data_size       (), oopSize);
 776     _dependencies_offset     = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
 777     _handler_table_offset    = _dependencies_offset  + align_up((int)dependencies->size_in_bytes (), oopSize);
 778     _nul_chk_table_offset    = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
 779 #if INCLUDE_JVMCI
 780     _speculations_offset     = _nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize);
 781     _jvmci_data_offset       = _speculations_offset  + align_up(speculations_len, oopSize);
 782     _nmethod_end_offset      = _jvmci_data_offset    + align_up(jvmci_data_size, oopSize);
 783 #else
 784     _nmethod_end_offset      = _nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize);
 785 #endif
 786     _entry_point             = code_begin()          + offsets->value(CodeOffsets::Entry);
 787     _verified_entry_point    = code_begin()          + offsets->value(CodeOffsets::Verified_Entry);
 788     _osr_entry_point         = code_begin()          + offsets->value(CodeOffsets::OSR_Entry);
 789     _exception_cache         = NULL;
 790 
 791     _scopes_data_begin = (address) this + scopes_data_offset;
 792 
 793     _pc_desc_container.reset_to(scopes_pcs_begin());
 794 
 795     code_buffer->copy_code_and_locs_to(this);
 796     // Copy contents of ScopeDescRecorder to nmethod
 797     code_buffer->copy_values_to(this);
 798     debug_info->copy_to(this);
 799     dependencies->copy_to(this);
 800     clear_unloading_state();
 801 
 802     Universe::heap()->register_nmethod(this);
 803     debug_only(Universe::heap()->verify_nmethod(this));
 804 
 805     CodeCache::commit(this);
 806 
 807     // Copy contents of ExceptionHandlerTable to nmethod
 808     handler_table->copy_to(this);
 809     nul_chk_table->copy_to(this);
 810 


 892     } else {
 893       CompileTask::print(st, this, msg, /*short_form:*/ false);
 894     }
 895   }
 896 }
 897 
 898 void nmethod::maybe_print_nmethod(DirectiveSet* directive) {
 899   bool printnmethods = directive->PrintAssemblyOption || directive->PrintNMethodsOption;
 900   if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) {
 901     print_nmethod(printnmethods);
 902   }
 903 }
 904 
 905 void nmethod::print_nmethod(bool printmethod) {
 906   ttyLocker ttyl;  // keep the following output all in one block
 907   if (xtty != NULL) {
 908     xtty->begin_head("print_nmethod");
 909     xtty->stamp();
 910     xtty->end_head();
 911   }
 912   // print the header part first
 913   print();
 914   // then print the requested information
 915   if (printmethod) {
 916     print_code();



















 917     print_pcs();
 918     if (oop_maps()) {
 919       oop_maps()->print();




 920     }



 921   }



 922   if (printmethod || PrintDebugInfo || CompilerOracle::has_option_string(_method, "PrintDebugInfo")) {
 923     print_scopes();

 924   }
 925   if (printmethod || PrintRelocations || CompilerOracle::has_option_string(_method, "PrintRelocations")) {
 926     print_relocations();

 927   }
 928   if (printmethod || PrintDependencies || CompilerOracle::has_option_string(_method, "PrintDependencies")) {
 929     print_dependencies();

 930   }
 931   if (printmethod || PrintExceptionHandlers) {
 932     print_handler_table();

 933     print_nul_chk_table();

 934   }

 935   if (printmethod) {
 936     print_recorded_oops();

 937     print_recorded_metadata();

 938   }



 939   if (xtty != NULL) {
 940     xtty->tail("print_nmethod");
 941   }
 942 }
 943 
 944 
 945 // Promote one word from an assembly-time handle to a live embedded oop.
 946 inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) {
 947   if (handle == NULL ||
 948       // As a special case, IC oops are initialized to 1 or -1.
 949       handle == (jobject) Universe::non_oop_word()) {
 950     (*dest) = (oop) handle;
 951   } else {
 952     (*dest) = JNIHandles::resolve_non_null(handle);
 953   }
 954 }
 955 
 956 
 957 // Have to have the same name because it's called by a template
 958 void nmethod::copy_values(GrowableArray<jobject>* array) {


2045   }
2046   return true;
2047 }
2048 
2049 
2050 address nmethod::continuation_for_implicit_exception(address pc) {
2051   // Exception happened outside inline-cache check code => we are inside
2052   // an active nmethod => use cpc to determine a return address
2053   int exception_offset = pc - code_begin();
2054   int cont_offset = ImplicitExceptionTable(this).at( exception_offset );
2055 #ifdef ASSERT
2056   if (cont_offset == 0) {
2057     Thread* thread = Thread::current();
2058     ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY
2059     HandleMark hm(thread);
2060     ResourceMark rm(thread);
2061     CodeBlob* cb = CodeCache::find_blob(pc);
2062     assert(cb != NULL && cb == this, "");
2063     ttyLocker ttyl;
2064     tty->print_cr("implicit exception happened at " INTPTR_FORMAT, p2i(pc));
2065     print();

2066     method()->print_codes();
2067     print_code();
2068     print_pcs();
2069   }
2070 #endif
2071   if (cont_offset == 0) {
2072     // Let the normal error handling report the exception
2073     return NULL;
2074   }
2075   return code_begin() + cont_offset;
2076 }
2077 
2078 
2079 
2080 void nmethod_init() {
2081   // make sure you didn't forget to adjust the filler fields
2082   assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
2083 }
2084 
2085 
2086 //-------------------------------------------------------------------------------------------
2087 
2088 
2089 // QQQ might we make this work from a frame??
2090 nmethodLocker::nmethodLocker(address pc) {
2091   CodeBlob* cb = CodeCache::find_blob(pc);
2092   guarantee(cb != NULL && cb->is_compiled(), "bad pc for a nmethod found");
2093   _nm = cb->as_compiled_method();
2094   lock_nmethod(_nm);
2095 }
2096 
2097 // Only JvmtiDeferredEvent::compiled_method_unload_event()
2098 // should pass zombie_ok == true.
2099 void nmethodLocker::lock_nmethod(CompiledMethod* cm, bool zombie_ok) {


2107 void nmethodLocker::unlock_nmethod(CompiledMethod* cm) {
2108   if (cm == NULL)  return;
2109   if (cm->is_aot()) return;  // FIXME: Revisit once _lock_count is added to aot_method
2110   nmethod* nm = cm->as_nmethod();
2111   Atomic::dec(&nm->_lock_count);
2112   assert(nm->_lock_count >= 0, "unmatched nmethod lock/unlock");
2113 }
2114 
2115 
2116 // -----------------------------------------------------------------------------
2117 // Verification
2118 
2119 class VerifyOopsClosure: public OopClosure {
2120   nmethod* _nm;
2121   bool     _ok;
2122 public:
2123   VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
2124   bool ok() { return _ok; }
2125   virtual void do_oop(oop* p) {
2126     if (oopDesc::is_oop_or_null(*p)) return;




2127     if (_ok) {
2128       _nm->print_nmethod(true);
2129       _ok = false;
2130     }
2131     tty->print_cr("*** non-oop " PTR_FORMAT " found at " PTR_FORMAT " (offset %d)",
2132                   p2i(*p), p2i(p), (int)((intptr_t)p - (intptr_t)_nm));
2133   }
2134   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
2135 };
2136 
2137 void nmethod::verify() {
2138 
2139   // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant
2140   // seems odd.
2141 
2142   if (is_zombie() || is_not_entrant() || is_unloaded())
2143     return;
2144 
2145   // Make sure all the entry points are correctly aligned for patching.
2146   NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point());
2147 
2148   // assert(oopDesc::is_oop(method()), "must be valid");
2149 
2150   ResourceMark rm;
2151 
2152   if (!CodeCache::contains(this)) {


2221       case relocInfo::runtime_call_type:
2222       case relocInfo::runtime_call_w_cp_type: {
2223         address destination = iter.reloc()->value();
2224         // Right now there is no way to find out which entries support
2225         // an interrupt point.  It would be nice if we had this
2226         // information in a table.
2227         break;
2228       }
2229       default:
2230         break;
2231     }
2232     assert(stub == NULL || stub_contains(stub), "static call stub outside stub section");
2233   }
2234 }
2235 
2236 
2237 // -----------------------------------------------------------------------------
2238 // Printing operations
2239 
2240 void nmethod::print() const {
2241   ResourceMark rm;
2242   ttyLocker ttyl;   // keep the following output all in one block





2243 
2244   tty->print("Compiled method ");
2245 
2246   if (is_compiled_by_c1()) {
2247     tty->print("(c1) ");
2248   } else if (is_compiled_by_c2()) {
2249     tty->print("(c2) ");
2250   } else if (is_compiled_by_jvmci()) {
2251     tty->print("(JVMCI) ");
2252   } else {
2253     tty->print("(nm) ");
2254   }
2255 
2256   print_on(tty, NULL);
2257 
2258   if (WizardMode) {
2259     tty->print("((nmethod*) " INTPTR_FORMAT ") ", p2i(this));
2260     tty->print(" for method " INTPTR_FORMAT , p2i(method()));
2261     tty->print(" { ");
2262     tty->print_cr("%s ", state());
2263     tty->print_cr("}:");
2264   }
2265   if (size              () > 0) tty->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2266                                               p2i(this),
2267                                               p2i(this) + size(),
2268                                               size());
2269   if (relocation_size   () > 0) tty->print_cr(" relocation     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2270                                               p2i(relocation_begin()),
2271                                               p2i(relocation_end()),
2272                                               relocation_size());
2273   if (consts_size       () > 0) tty->print_cr(" constants      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2274                                               p2i(consts_begin()),
2275                                               p2i(consts_end()),
2276                                               consts_size());
2277   if (insts_size        () > 0) tty->print_cr(" main code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2278                                               p2i(insts_begin()),
2279                                               p2i(insts_end()),
2280                                               insts_size());
2281   if (stub_size         () > 0) tty->print_cr(" stub code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2282                                               p2i(stub_begin()),
2283                                               p2i(stub_end()),
2284                                               stub_size());
2285   if (oops_size         () > 0) tty->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2286                                               p2i(oops_begin()),
2287                                               p2i(oops_end()),
2288                                               oops_size());
2289   if (metadata_size      () > 0) tty->print_cr(" metadata       [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2290                                               p2i(metadata_begin()),
2291                                               p2i(metadata_end()),
2292                                               metadata_size());
2293   if (scopes_data_size  () > 0) tty->print_cr(" scopes data    [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2294                                               p2i(scopes_data_begin()),
2295                                               p2i(scopes_data_end()),
2296                                               scopes_data_size());
2297   if (scopes_pcs_size   () > 0) tty->print_cr(" scopes pcs     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2298                                               p2i(scopes_pcs_begin()),
2299                                               p2i(scopes_pcs_end()),
2300                                               scopes_pcs_size());
2301   if (dependencies_size () > 0) tty->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2302                                               p2i(dependencies_begin()),
2303                                               p2i(dependencies_end()),
2304                                               dependencies_size());
2305   if (handler_table_size() > 0) tty->print_cr(" handler table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2306                                               p2i(handler_table_begin()),
2307                                               p2i(handler_table_end()),
2308                                               handler_table_size());
2309   if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2310                                               p2i(nul_chk_table_begin()),
2311                                               p2i(nul_chk_table_end()),
2312                                               nul_chk_table_size());
2313 #if INCLUDE_JVMCI
2314   if (speculations_size () > 0) tty->print_cr(" speculations   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2315                                               p2i(speculations_begin()),
2316                                               p2i(speculations_end()),
2317                                               speculations_size());
2318   if (jvmci_data_size   () > 0) tty->print_cr(" JVMCI data     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2319                                               p2i(jvmci_data_begin()),
2320                                               p2i(jvmci_data_end()),
2321                                               jvmci_data_size());
2322 #endif
2323 }
2324 
2325 #ifndef PRODUCT
2326 
2327 void nmethod::print_scopes() {
2328   // Find the first pc desc for all scopes in the code and print it.
2329   ResourceMark rm;
2330   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2331     if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
2332       continue;
2333 
2334     ScopeDesc* sd = scope_desc_at(p->real_pc(this));
2335     while (sd != NULL) {
2336       sd->print_on(tty, p);
2337       sd = sd->sender();
2338     }
2339   }
2340 }
2341 


2342 void nmethod::print_dependencies() {
2343   ResourceMark rm;
2344   ttyLocker ttyl;   // keep the following output all in one block
2345   tty->print_cr("Dependencies:");
2346   for (Dependencies::DepStream deps(this); deps.next(); ) {
2347     deps.print_dependency();
2348     Klass* ctxk = deps.context_type();
2349     if (ctxk != NULL) {
2350       if (ctxk->is_instance_klass() && InstanceKlass::cast(ctxk)->is_dependent_nmethod(this)) {
2351         tty->print_cr("   [nmethod<=klass]%s", ctxk->external_name());
2352       }
2353     }
2354     deps.log_dependency();  // put it into the xml log also
2355   }
2356 }




























































2357 











2358 

2359 void nmethod::print_relocations() {
2360   ResourceMark m;       // in case methods get printed via the debugger
2361   tty->print_cr("relocations:");
2362   RelocIterator iter(this);
2363   iter.print();
2364 }

2365 
2366 
2367 void nmethod::print_pcs() {
2368   ResourceMark m;       // in case methods get printed via debugger
2369   tty->print_cr("pc-bytecode offsets:");


2370   for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2371     p->print(this);
2372   }











2373 }
2374 
2375 void nmethod::print_recorded_oops() {
2376   tty->print_cr("Recorded oops:");
2377   for (int i = 0; i < oops_count(); i++) {




2378     oop o = oop_at(i);
2379     tty->print("#%3d: " INTPTR_FORMAT " ", i, p2i(o));
2380     if (o == Universe::non_oop_word()) {
2381       tty->print("non-oop word");


2382     } else {
2383       if (o != NULL) {
2384         o->print_value();
2385       } else {
2386         tty->print_cr("NULL");
2387       }
2388     }
2389     tty->cr();
2390   }



2391 }
2392 
2393 void nmethod::print_recorded_metadata() {
2394   tty->print_cr("Recorded metadata:");
2395   for (int i = 0; i < metadata_count(); i++) {




2396     Metadata* m = metadata_at(i);
2397     tty->print("#%3d: " INTPTR_FORMAT " ", i, p2i(m));
2398     if (m == (Metadata*)Universe::non_oop_word()) {
2399       tty->print("non-metadata word");


2400     } else {
2401       Metadata::print_value_on_maybe_null(tty, m);
2402     }
2403     tty->cr();
2404   }



























































2405 }
2406 
2407 #endif // PRODUCT







































































































































































2408 
2409 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) {
2410   RelocIterator iter(this, begin, end);
2411   bool have_one = false;
2412   while (iter.next()) {
2413     have_one = true;
2414     switch (iter.type()) {
2415         case relocInfo::none:                  return "no_reloc";
2416         case relocInfo::oop_type: {
2417           stringStream st;


2418           oop_Relocation* r = iter.oop_reloc();
2419           oop obj = r->oop_value();
2420           st.print("oop(");
2421           if (obj == NULL) st.print("NULL");
2422           else obj->print_value_on(&st);
2423           st.print(")");
2424           return st.as_string();
2425         }
2426         case relocInfo::metadata_type: {
2427           stringStream st;
2428           metadata_Relocation* r = iter.metadata_reloc();
2429           Metadata* obj = r->metadata_value();
2430           st.print("metadata(");
2431           if (obj == NULL) st.print("NULL");
2432           else obj->print_value_on(&st);
2433           st.print(")");
2434           return st.as_string();
2435         }
2436         case relocInfo::runtime_call_type:
2437         case relocInfo::runtime_call_w_cp_type: {


2499         case relocInfo::type_mask:             return "type_bit_mask";
2500 
2501         default:
2502           break;
2503     }
2504   }
2505   return have_one ? "other" : NULL;
2506 }
2507 
2508 // Return a the last scope in (begin..end]
2509 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
2510   PcDesc* p = pc_desc_near(begin+1);
2511   if (p != NULL && p->real_pc(this) <= end) {
2512     return new ScopeDesc(this, p->scope_decode_offset(),
2513                          p->obj_decode_offset(), p->should_reexecute(), p->rethrow_exception(),
2514                          p->return_oop());
2515   }
2516   return NULL;
2517 }
2518 
2519 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin) const {
2520   if (block_begin == entry_point())             stream->print_cr("[Entry Point]");
2521   if (block_begin == verified_entry_point())    stream->print_cr("[Verified Entry Point]");
2522   if (JVMCI_ONLY(_exception_offset >= 0 &&) block_begin == exception_begin())         stream->print_cr("[Exception Handler]");
2523   if (block_begin == stub_begin())              stream->print_cr("[Stub Code]");
2524   if (JVMCI_ONLY(_deopt_handler_begin != NULL &&) block_begin == deopt_handler_begin())     stream->print_cr("[Deopt Handler Code]");
2525 
2526   if (has_method_handle_invokes())
2527     if (block_begin == deopt_mh_handler_begin())  stream->print_cr("[Deopt MH Handler Code]");
2528 
2529   if (block_begin == consts_begin())            stream->print_cr("[Constants]");











2530 
2531   if (block_begin == entry_point()) {
2532     methodHandle m = method();
2533     if (m.not_null()) {
2534       stream->print("  # ");
2535       m->print_value_on(stream);
2536       stream->cr();
2537     }
2538     if (m.not_null() && !is_osr_method()) {
2539       ResourceMark rm;
2540       int sizeargs = m->size_of_parameters();
2541       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
2542       VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
2543       {
2544         int sig_index = 0;
2545         if (!m->is_static())
2546           sig_bt[sig_index++] = T_OBJECT; // 'this'
2547         for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
2548           BasicType t = ss.type();
2549           sig_bt[sig_index++] = t;


2606         if (at_old_sp) {
2607           stream->print("  (%s of caller)", spname);
2608           did_old_sp = true;
2609         }
2610         stream->cr();
2611         sig_index += type2size[t];
2612         arg_index += 1;
2613         if (!at_this)  ss.next();
2614       }
2615       if (!did_old_sp) {
2616         stream->print("  # ");
2617         stream->move_to(tab1);
2618         stream->print("[%s+0x%x]", spname, stack_slot_offset);
2619         stream->print("  (%s of caller)", spname);
2620         stream->cr();
2621       }
2622     }
2623   }
2624 }
2625 
2626 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) {

















2627   // First, find an oopmap in (begin, end].
2628   // We use the odd half-closed interval so that oop maps and scope descs
2629   // which are tied to the byte after a call are printed with the call itself.
2630   address base = code_begin();
2631   ImmutableOopMapSet* oms = oop_maps();
2632   if (oms != NULL) {
2633     for (int i = 0, imax = oms->count(); i < imax; i++) {
2634       const ImmutableOopMapPair* pair = oms->pair_at(i);
2635       const ImmutableOopMap* om = pair->get_from(oms);
2636       address pc = base + pair->pc_offset();
2637       if (pc > begin) {
2638         if (pc <= end) {
2639           st->move_to(column);
2640           st->print("; ");
2641           om->print_on(st);
2642         }
2643         break;
2644       }
2645     }
2646   }
2647 
2648   // Print any debug info present at this pc.
2649   ScopeDesc* sd  = scope_desc_in(begin, end);
2650   if (sd != NULL) {
2651     st->move_to(column);
2652     if (sd->bci() == SynchronizationEntryBCI) {
2653       st->print(";*synchronization entry");
2654     } else if (sd->bci() == AfterBci) {
2655       st->print(";* method exit (unlocked if synchronized)");
2656     } else if (sd->bci() == UnwindBci) {
2657       st->print(";* unwind (locked if synchronized)");
2658     } else if (sd->bci() == AfterExceptionBci) {
2659       st->print(";* unwind (unlocked if synchronized)");
2660     } else if (sd->bci() == UnknownBci) {
2661       st->print(";* unknown");
2662     } else if (sd->bci() == InvalidFrameStateBci) {
2663       st->print(";* invalid frame state");
2664     } else {
2665       if (sd->method() == NULL) {
2666         st->print("method is NULL");
2667       } else if (sd->method()->is_native()) {
2668         st->print("method is native");
2669       } else {
2670         Bytecodes::Code bc = sd->method()->java_code_at(sd->bci());
2671         st->print(";*%s", Bytecodes::name(bc));


2687         case Bytecodes::_putfield:
2688         case Bytecodes::_getstatic:
2689         case Bytecodes::_putstatic:
2690           {
2691             Bytecode_field field(sd->method(), sd->bci());
2692             st->print(" ");
2693             if (field.name() != NULL)
2694               field.name()->print_symbol_on(st);
2695             else
2696               st->print("<UNKNOWN>");
2697           }
2698         default:
2699           break;
2700         }
2701       }
2702       st->print(" {reexecute=%d rethrow=%d return_oop=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop());
2703     }
2704 
2705     // Print all scopes
2706     for (;sd != NULL; sd = sd->sender()) {
2707       st->move_to(column);
2708       st->print("; -");



2709       if (sd->method() == NULL) {
2710         st->print("method is NULL");
2711       } else {
2712         sd->method()->print_short_name(st);
2713       }
2714       int lineno = sd->method()->line_number_from_bci(sd->bci());
2715       if (lineno != -1) {
2716         st->print("@%d (line %d)", sd->bci(), lineno);
2717       } else {
2718         st->print("@%d", sd->bci());
2719       }
2720       st->cr();
2721     }
2722   }
2723 
2724   // Print relocation information


2725   const char* str = reloc_string_for(begin, end);
2726   if (str != NULL) {
2727     if (sd != NULL) st->cr();
2728     st->move_to(column);
2729     st->print(";   {%s}", str);
2730   }
2731   int cont_offset = ImplicitExceptionTable(this).at(begin - code_begin());
2732   if (cont_offset != 0) {
2733     st->move_to(column);
2734     st->print("; implicit exception: dispatches to " INTPTR_FORMAT, p2i(code_begin() + cont_offset));
2735   }
2736 
2737 }
2738 


2739 class DirectNativeCallWrapper: public NativeCallWrapper {
2740 private:
2741   NativeCall* _call;
2742 
2743 public:
2744   DirectNativeCallWrapper(NativeCall* call) : _call(call) {}
2745 
2746   virtual address destination() const { return _call->destination(); }
2747   virtual address instruction_address() const { return _call->instruction_address(); }
2748   virtual address next_instruction_address() const { return _call->next_instruction_address(); }
2749   virtual address return_address() const { return _call->return_address(); }
2750 
2751   virtual address get_resolve_call_stub(bool is_optimized) const {
2752     if (is_optimized) {
2753       return SharedRuntime::get_resolve_opt_virtual_call_stub();
2754     }
2755     return SharedRuntime::get_resolve_virtual_call_stub();
2756   }
2757 
2758   virtual void set_destination_mt_safe(address dest) {


2825 address nmethod::call_instruction_address(address pc) const {
2826   if (NativeCall::is_call_before(pc)) {
2827     NativeCall *ncall = nativeCall_before(pc);
2828     return ncall->instruction_address();
2829   }
2830   return NULL;
2831 }
2832 
2833 CompiledStaticCall* nmethod::compiledStaticCall_at(Relocation* call_site) const {
2834   return CompiledDirectStaticCall::at(call_site);
2835 }
2836 
2837 CompiledStaticCall* nmethod::compiledStaticCall_at(address call_site) const {
2838   return CompiledDirectStaticCall::at(call_site);
2839 }
2840 
2841 CompiledStaticCall* nmethod::compiledStaticCall_before(address return_addr) const {
2842   return CompiledDirectStaticCall::before(return_addr);
2843 }
2844 
2845 #ifndef PRODUCT
2846 
2847 void nmethod::print_value_on(outputStream* st) const {
2848   st->print("nmethod");
2849   print_on(st, NULL);
2850 }



2851 
2852 void nmethod::print_calls(outputStream* st) {
2853   RelocIterator iter(this);
2854   while (iter.next()) {
2855     switch (iter.type()) {
2856     case relocInfo::virtual_call_type:
2857     case relocInfo::opt_virtual_call_type: {
2858       CompiledICLocker ml_verify(this);
2859       CompiledIC_at(&iter)->print();
2860       break;
2861     }
2862     case relocInfo::static_call_type:
2863       st->print_cr("Static call at " INTPTR_FORMAT, p2i(iter.reloc()->addr()));
2864       CompiledDirectStaticCall::at(iter.reloc())->print();
2865       break;
2866     default:
2867       break;
2868     }
2869   }
2870 }
2871 
2872 void nmethod::print_handler_table() {
2873   ExceptionHandlerTable(this).print();
2874 }
2875 
2876 void nmethod::print_nul_chk_table() {
2877   ImplicitExceptionTable(this).print(code_begin());
2878 }
2879 
2880 void nmethod::print_statistics() {
2881   ttyLocker ttyl;
2882   if (xtty != NULL)  xtty->head("statistics type='nmethod'");
2883   native_nmethod_stats.print_native_nmethod_stats();
2884 #ifdef COMPILER1
2885   c1_java_nmethod_stats.print_nmethod_stats("C1");
2886 #endif
2887 #ifdef COMPILER2
2888   c2_java_nmethod_stats.print_nmethod_stats("C2");
2889 #endif
2890 #if INCLUDE_JVMCI
2891   jvmci_java_nmethod_stats.print_nmethod_stats("JVMCI");
2892 #endif
2893   unknown_java_nmethod_stats.print_nmethod_stats("Unknown");
2894   DebugInformationRecorder::print_statistics();
2895 #ifndef PRODUCT
2896   pc_nmethod_stats.print_pc_stats();
2897 #endif




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "jvm.h"
  27 #include "asm/assembler.inline.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/compiledIC.hpp"
  30 #include "code/compiledMethod.inline.hpp"
  31 #include "code/dependencies.hpp"
  32 #include "code/nativeInst.hpp"
  33 #include "code/nmethod.hpp"
  34 #include "code/scopeDesc.hpp"
  35 #include "compiler/abstractCompiler.hpp"
  36 #include "compiler/compileBroker.hpp"
  37 #include "compiler/compileLog.hpp"
  38 #include "compiler/compilerDirectives.hpp"
  39 #include "compiler/directivesParser.hpp"
  40 #include "compiler/disassembler.hpp"
  41 #include "interpreter/bytecode.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/allocation.inline.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/access.inline.hpp"


 440 #if INCLUDE_RTM_OPT
 441   _rtm_state               = NoRTM;
 442 #endif
 443 }
 444 
 445 nmethod* nmethod::new_native_nmethod(const methodHandle& method,
 446   int compile_id,
 447   CodeBuffer *code_buffer,
 448   int vep_offset,
 449   int frame_complete,
 450   int frame_size,
 451   ByteSize basic_lock_owner_sp_offset,
 452   ByteSize basic_lock_sp_offset,
 453   OopMapSet* oop_maps) {
 454   code_buffer->finalize_oop_references(method);
 455   // create nmethod
 456   nmethod* nm = NULL;
 457   {
 458     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 459     int native_nmethod_size = CodeBlob::allocation_size(code_buffer, sizeof(nmethod));
 460 
 461     CodeOffsets offsets;
 462     offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
 463     offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
 464     nm = new (native_nmethod_size, CompLevel_none)
 465     nmethod(method(), compiler_none, native_nmethod_size,
 466             compile_id, &offsets,
 467             code_buffer, frame_size,
 468             basic_lock_owner_sp_offset,
 469             basic_lock_sp_offset,
 470             oop_maps);
 471     NOT_PRODUCT(if (nm != NULL)  native_nmethod_stats.note_native_nmethod(nm));
 472   }
 473 
 474   if (nm != NULL) {
 475     // verify nmethod
 476     debug_only(nm->verify();) // might block
 477 
 478     nm->log_new_nmethod();
 479     nm->make_in_use();
 480   }
 481   return nm;
 482 }
 483 
 484 nmethod* nmethod::new_nmethod(const methodHandle& method,
 485   int compile_id,
 486   int entry_bci,
 487   CodeOffsets* offsets,
 488   int orig_pc_offset,
 489   DebugInformationRecorder* debug_info,
 490   Dependencies* dependencies,


 645 
 646     clear_unloading_state();
 647 
 648     Universe::heap()->register_nmethod(this);
 649     debug_only(Universe::heap()->verify_nmethod(this));
 650 
 651     CodeCache::commit(this);
 652   }
 653 
 654   if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) {
 655     ttyLocker ttyl;  // keep the following output all in one block
 656     // This output goes directly to the tty, not the compiler log.
 657     // To enable tools to match it up with the compilation activity,
 658     // be sure to tag this tty output with the compile ID.
 659     if (xtty != NULL) {
 660       xtty->begin_head("print_native_nmethod");
 661       xtty->method(_method);
 662       xtty->stamp();
 663       xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
 664     }
 665     // Print the header part, then print the requested information.
 666     // This is both handled in decode2(), called via print_code() -> decode()

 667     if (PrintNativeNMethods) {
 668       tty->print_cr("-------------------------- Assembly (native nmethod) ---------------------------");
 669       print_code();
 670       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 671 #if defined(SUPPORT_DATA_STRUCTS)
 672       if (AbstractDisassembler::show_structs()) {
 673         if (oop_maps != NULL) {
 674           tty->print("oop maps:"); // oop_maps->print_on(tty) outputs a cr() at the beginning
 675           oop_maps->print_on(tty);
 676           tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 677         }
 678       }
 679 #endif
 680     } else {
 681       print(); // print the header part only.
 682     }
 683 #if defined(SUPPORT_DATA_STRUCTS)
 684     if (AbstractDisassembler::show_structs()) {
 685       if (PrintRelocations) {
 686         print_relocations();
 687         tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 688       }
 689     }
 690 #endif
 691     if (xtty != NULL) {
 692       xtty->tail("print_native_nmethod");
 693     }
 694   }
 695 }
 696 
 697 void* nmethod::operator new(size_t size, int nmethod_size, int comp_level) throw () {
 698   return CodeCache::allocate(nmethod_size, CodeCache::get_code_blob_type(comp_level));
 699 }
 700 
 701 nmethod::nmethod(
 702   Method* method,
 703   CompilerType type,
 704   int nmethod_size,
 705   int compile_id,
 706   int entry_bci,
 707   CodeOffsets* offsets,
 708   int orig_pc_offset,
 709   DebugInformationRecorder* debug_info,
 710   Dependencies* dependencies,


 747     set_ctable_begin(header_begin() + _consts_offset);
 748 
 749 #if INCLUDE_JVMCI
 750     if (compiler->is_jvmci()) {
 751       // JVMCI might not produce any stub sections
 752       if (offsets->value(CodeOffsets::Exceptions) != -1) {
 753         _exception_offset        = code_offset()          + offsets->value(CodeOffsets::Exceptions);
 754       } else {
 755         _exception_offset = -1;
 756       }
 757       if (offsets->value(CodeOffsets::Deopt) != -1) {
 758         _deopt_handler_begin       = (address) this + code_offset()          + offsets->value(CodeOffsets::Deopt);
 759       } else {
 760         _deopt_handler_begin = NULL;
 761       }
 762       if (offsets->value(CodeOffsets::DeoptMH) != -1) {
 763         _deopt_mh_handler_begin  = (address) this + code_offset()          + offsets->value(CodeOffsets::DeoptMH);
 764       } else {
 765         _deopt_mh_handler_begin = NULL;
 766       }
 767     } else
 768 #endif
 769     {
 770       // Exception handler and deopt handler are in the stub section
 771       assert(offsets->value(CodeOffsets::Exceptions) != -1, "must be set");
 772       assert(offsets->value(CodeOffsets::Deopt     ) != -1, "must be set");
 773 
 774       _exception_offset       = _stub_offset          + offsets->value(CodeOffsets::Exceptions);
 775       _deopt_handler_begin    = (address) this + _stub_offset          + offsets->value(CodeOffsets::Deopt);
 776       if (offsets->value(CodeOffsets::DeoptMH) != -1) {
 777         _deopt_mh_handler_begin  = (address) this + _stub_offset          + offsets->value(CodeOffsets::DeoptMH);
 778       } else {
 779         _deopt_mh_handler_begin  = NULL;
 780       }

 781     }

 782     if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
 783       _unwind_handler_offset = code_offset()         + offsets->value(CodeOffsets::UnwindHandler);
 784     } else {
 785       _unwind_handler_offset = -1;
 786     }
 787 
 788     _oops_offset             = data_offset();
 789     _metadata_offset         = _oops_offset          + align_up(code_buffer->total_oop_size(), oopSize);
 790     int scopes_data_offset   = _metadata_offset      + align_up(code_buffer->total_metadata_size(), wordSize);
 791 
 792     _scopes_pcs_offset       = scopes_data_offset    + align_up(debug_info->data_size       (), oopSize);
 793     _dependencies_offset     = _scopes_pcs_offset    + adjust_pcs_size(debug_info->pcs_size());
 794     _handler_table_offset    = _dependencies_offset  + align_up((int)dependencies->size_in_bytes (), oopSize);
 795     _nul_chk_table_offset    = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
 796 #if INCLUDE_JVMCI
 797     _speculations_offset     = _nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize);
 798     _jvmci_data_offset       = _speculations_offset  + align_up(speculations_len, oopSize);
 799     _nmethod_end_offset      = _jvmci_data_offset    + align_up(jvmci_data_size, oopSize);
 800 #else
 801     _nmethod_end_offset      = _nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize);
 802 #endif
 803     _entry_point             = code_begin()          + offsets->value(CodeOffsets::Entry);
 804     _verified_entry_point    = code_begin()          + offsets->value(CodeOffsets::Verified_Entry);
 805     _osr_entry_point         = code_begin()          + offsets->value(CodeOffsets::OSR_Entry);
 806     _exception_cache         = NULL;

 807     _scopes_data_begin       = (address) this + scopes_data_offset;
 808 
 809     _pc_desc_container.reset_to(scopes_pcs_begin());
 810 
 811     code_buffer->copy_code_and_locs_to(this);
 812     // Copy contents of ScopeDescRecorder to nmethod
 813     code_buffer->copy_values_to(this);
 814     debug_info->copy_to(this);
 815     dependencies->copy_to(this);
 816     clear_unloading_state();
 817 
 818     Universe::heap()->register_nmethod(this);
 819     debug_only(Universe::heap()->verify_nmethod(this));
 820 
 821     CodeCache::commit(this);
 822 
 823     // Copy contents of ExceptionHandlerTable to nmethod
 824     handler_table->copy_to(this);
 825     nul_chk_table->copy_to(this);
 826 


 908     } else {
 909       CompileTask::print(st, this, msg, /*short_form:*/ false);
 910     }
 911   }
 912 }
 913 
 914 void nmethod::maybe_print_nmethod(DirectiveSet* directive) {
 915   bool printnmethods = directive->PrintAssemblyOption || directive->PrintNMethodsOption;
 916   if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) {
 917     print_nmethod(printnmethods);
 918   }
 919 }
 920 
 921 void nmethod::print_nmethod(bool printmethod) {
 922   ttyLocker ttyl;  // keep the following output all in one block
 923   if (xtty != NULL) {
 924     xtty->begin_head("print_nmethod");
 925     xtty->stamp();
 926     xtty->end_head();
 927   }
 928   // Print the header part, then print the requested information.
 929   // This is both handled in decode2().

 930   if (printmethod) {
 931     HandleMark hm;
 932     ResourceMark m;
 933     if (is_compiled_by_c1()) {
 934       tty->cr();
 935       tty->print_cr("============================= C1-compiled nmethod ==============================");
 936     }
 937     if (is_compiled_by_jvmci()) {
 938       tty->cr();
 939       tty->print_cr("=========================== JVMCI-compiled nmethod =============================");
 940     }
 941     tty->print_cr("----------------------------------- Assembly -----------------------------------");
 942     decode2(tty);
 943 #if defined(SUPPORT_DATA_STRUCTS)
 944     if (AbstractDisassembler::show_structs()) {
 945       // Print the oops from the underlying CodeBlob as well.
 946       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 947       print_oops(tty);
 948       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 949       print_metadata(tty);
 950       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 951       print_pcs();
 952       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 953       if (oop_maps() != NULL) {
 954         tty->print("oop maps:"); // oop_maps()->print_on(tty) outputs a cr() at the beginning
 955         oop_maps()->print_on(tty);
 956         tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 957       }
 958     }
 959 #endif
 960   } else {
 961     print(); // print the header part only.
 962   }
 963 
 964 #if defined(SUPPORT_DATA_STRUCTS)
 965   if (AbstractDisassembler::show_structs()) {
 966     if (printmethod || PrintDebugInfo || CompilerOracle::has_option_string(_method, "PrintDebugInfo")) {
 967       print_scopes();
 968       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 969     }
 970     if (printmethod || PrintRelocations || CompilerOracle::has_option_string(_method, "PrintRelocations")) {
 971       print_relocations();
 972       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 973     }
 974     if (printmethod || PrintDependencies || CompilerOracle::has_option_string(_method, "PrintDependencies")) {
 975       print_dependencies();
 976       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 977     }
 978     if (printmethod || PrintExceptionHandlers) {
 979       print_handler_table();
 980       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 981       print_nul_chk_table();
 982       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 983     }
 984 
 985     if (printmethod) {
 986       print_recorded_oops();
 987       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 988       print_recorded_metadata();
 989       tty->print_cr("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ");
 990     }
 991   }
 992 #endif
 993 
 994   if (xtty != NULL) {
 995     xtty->tail("print_nmethod");
 996   }
 997 }
 998 
 999 
1000 // Promote one word from an assembly-time handle to a live embedded oop.
1001 inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) {
1002   if (handle == NULL ||
1003       // As a special case, IC oops are initialized to 1 or -1.
1004       handle == (jobject) Universe::non_oop_word()) {
1005     (*dest) = (oop) handle;
1006   } else {
1007     (*dest) = JNIHandles::resolve_non_null(handle);
1008   }
1009 }
1010 
1011 
1012 // Have to have the same name because it's called by a template
1013 void nmethod::copy_values(GrowableArray<jobject>* array) {


2100   }
2101   return true;
2102 }
2103 
2104 
2105 address nmethod::continuation_for_implicit_exception(address pc) {
2106   // Exception happened outside inline-cache check code => we are inside
2107   // an active nmethod => use cpc to determine a return address
2108   int exception_offset = pc - code_begin();
2109   int cont_offset = ImplicitExceptionTable(this).at( exception_offset );
2110 #ifdef ASSERT
2111   if (cont_offset == 0) {
2112     Thread* thread = Thread::current();
2113     ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY
2114     HandleMark hm(thread);
2115     ResourceMark rm(thread);
2116     CodeBlob* cb = CodeCache::find_blob(pc);
2117     assert(cb != NULL && cb == this, "");
2118     ttyLocker ttyl;
2119     tty->print_cr("implicit exception happened at " INTPTR_FORMAT, p2i(pc));
2120     // Print all available nmethod info.
2121     print_nmethod(true);
2122     method()->print_codes();


2123   }
2124 #endif
2125   if (cont_offset == 0) {
2126     // Let the normal error handling report the exception
2127     return NULL;
2128   }
2129   return code_begin() + cont_offset;
2130 }
2131 
2132 

2133 void nmethod_init() {
2134   // make sure you didn't forget to adjust the filler fields
2135   assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
2136 }
2137 
2138 
2139 //-------------------------------------------------------------------------------------------
2140 
2141 
2142 // QQQ might we make this work from a frame??
2143 nmethodLocker::nmethodLocker(address pc) {
2144   CodeBlob* cb = CodeCache::find_blob(pc);
2145   guarantee(cb != NULL && cb->is_compiled(), "bad pc for a nmethod found");
2146   _nm = cb->as_compiled_method();
2147   lock_nmethod(_nm);
2148 }
2149 
2150 // Only JvmtiDeferredEvent::compiled_method_unload_event()
2151 // should pass zombie_ok == true.
2152 void nmethodLocker::lock_nmethod(CompiledMethod* cm, bool zombie_ok) {


2160 void nmethodLocker::unlock_nmethod(CompiledMethod* cm) {
2161   if (cm == NULL)  return;
2162   if (cm->is_aot()) return;  // FIXME: Revisit once _lock_count is added to aot_method
2163   nmethod* nm = cm->as_nmethod();
2164   Atomic::dec(&nm->_lock_count);
2165   assert(nm->_lock_count >= 0, "unmatched nmethod lock/unlock");
2166 }
2167 
2168 
2169 // -----------------------------------------------------------------------------
2170 // Verification
2171 
2172 class VerifyOopsClosure: public OopClosure {
2173   nmethod* _nm;
2174   bool     _ok;
2175 public:
2176   VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
2177   bool ok() { return _ok; }
2178   virtual void do_oop(oop* p) {
2179     if (oopDesc::is_oop_or_null(*p)) return;
2180     // Print diagnostic information before calling print_nmethod().
2181     // Assertions therein might prevent call from returning.
2182     tty->print_cr("*** non-oop " PTR_FORMAT " found at " PTR_FORMAT " (offset %d)",
2183                   p2i(*p), p2i(p), (int)((intptr_t)p - (intptr_t)_nm));
2184     if (_ok) {
2185       _nm->print_nmethod(true);
2186       _ok = false;
2187     }


2188   }
2189   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
2190 };
2191 
2192 void nmethod::verify() {
2193 
2194   // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant
2195   // seems odd.
2196 
2197   if (is_zombie() || is_not_entrant() || is_unloaded())
2198     return;
2199 
2200   // Make sure all the entry points are correctly aligned for patching.
2201   NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point());
2202 
2203   // assert(oopDesc::is_oop(method()), "must be valid");
2204 
2205   ResourceMark rm;
2206 
2207   if (!CodeCache::contains(this)) {


2276       case relocInfo::runtime_call_type:
2277       case relocInfo::runtime_call_w_cp_type: {
2278         address destination = iter.reloc()->value();
2279         // Right now there is no way to find out which entries support
2280         // an interrupt point.  It would be nice if we had this
2281         // information in a table.
2282         break;
2283       }
2284       default:
2285         break;
2286     }
2287     assert(stub == NULL || stub_contains(stub), "static call stub outside stub section");
2288   }
2289 }
2290 
2291 
2292 // -----------------------------------------------------------------------------
2293 // Printing operations
2294 
2295 void nmethod::print() const {

2296   ttyLocker ttyl;   // keep the following output all in one block
2297   print(tty);
2298 }
2299 
2300 void nmethod::print(outputStream* st) const {
2301   ResourceMark rm;
2302 
2303   st->print("Compiled method ");
2304 
2305   if (is_compiled_by_c1()) {
2306     st->print("(c1) ");
2307   } else if (is_compiled_by_c2()) {
2308     st->print("(c2) ");
2309   } else if (is_compiled_by_jvmci()) {
2310     st->print("(JVMCI) ");
2311   } else {
2312     st->print("(n/a) ");
2313   }
2314 
2315   print_on(tty, NULL);
2316 
2317   if (WizardMode) {
2318     st->print("((nmethod*) " INTPTR_FORMAT ") ", p2i(this));
2319     st->print(" for method " INTPTR_FORMAT , p2i(method()));
2320     st->print(" { ");
2321     st->print_cr("%s ", state());
2322     st->print_cr("}:");
2323   }
2324   if (size              () > 0) st->print_cr(" total in heap  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2325                                              p2i(this),
2326                                              p2i(this) + size(),
2327                                              size());
2328   if (relocation_size   () > 0) st->print_cr(" relocation     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2329                                              p2i(relocation_begin()),
2330                                              p2i(relocation_end()),
2331                                              relocation_size());
2332   if (consts_size       () > 0) st->print_cr(" constants      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2333                                              p2i(consts_begin()),
2334                                              p2i(consts_end()),
2335                                              consts_size());
2336   if (insts_size        () > 0) st->print_cr(" main code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2337                                              p2i(insts_begin()),
2338                                              p2i(insts_end()),
2339                                              insts_size());
2340   if (stub_size         () > 0) st->print_cr(" stub code      [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2341                                              p2i(stub_begin()),
2342                                              p2i(stub_end()),
2343                                              stub_size());
2344   if (oops_size         () > 0) st->print_cr(" oops           [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2345                                              p2i(oops_begin()),
2346                                              p2i(oops_end()),
2347                                              oops_size());
2348   if (metadata_size     () > 0) st->print_cr(" metadata       [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2349                                              p2i(metadata_begin()),
2350                                              p2i(metadata_end()),
2351                                              metadata_size());
2352   if (scopes_data_size  () > 0) st->print_cr(" scopes data    [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2353                                              p2i(scopes_data_begin()),
2354                                              p2i(scopes_data_end()),
2355                                              scopes_data_size());
2356   if (scopes_pcs_size   () > 0) st->print_cr(" scopes pcs     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2357                                              p2i(scopes_pcs_begin()),
2358                                              p2i(scopes_pcs_end()),
2359                                              scopes_pcs_size());
2360   if (dependencies_size () > 0) st->print_cr(" dependencies   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2361                                              p2i(dependencies_begin()),
2362                                              p2i(dependencies_end()),
2363                                              dependencies_size());
2364   if (handler_table_size() > 0) st->print_cr(" handler table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2365                                              p2i(handler_table_begin()),
2366                                              p2i(handler_table_end()),
2367                                              handler_table_size());
2368   if (nul_chk_table_size() > 0) st->print_cr(" nul chk table  [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2369                                              p2i(nul_chk_table_begin()),
2370                                              p2i(nul_chk_table_end()),
2371                                              nul_chk_table_size());
2372 #if INCLUDE_JVMCI
2373   if (speculations_size () > 0) st->print_cr(" speculations   [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2374                                              p2i(speculations_begin()),
2375                                              p2i(speculations_end()),
2376                                              speculations_size());
2377   if (jvmci_data_size   () > 0) st->print_cr(" JVMCI data     [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
2378                                              p2i(jvmci_data_begin()),
2379                                              p2i(jvmci_data_end()),
2380                                              jvmci_data_size());
2381 #endif
2382 }
2383 
2384 void nmethod::print_code() {
2385   HandleMark hm;
2386   ResourceMark m;
2387   ttyLocker ttyl;
2388   // Call the specialized decode method of this class.
2389   decode(tty);









2390 }
2391 
2392 #ifndef PRODUCT  // called InstanceKlass methods are available only then. Declared as PRODUCT_RETURN
2393 
2394 void nmethod::print_dependencies() {
2395   ResourceMark rm;
2396   ttyLocker ttyl;   // keep the following output all in one block
2397   tty->print_cr("Dependencies:");
2398   for (Dependencies::DepStream deps(this); deps.next(); ) {
2399     deps.print_dependency();
2400     Klass* ctxk = deps.context_type();
2401     if (ctxk != NULL) {
2402       if (ctxk->is_instance_klass() && InstanceKlass::cast(ctxk)->is_dependent_nmethod(this)) {
2403         tty->print_cr("   [nmethod<=klass]%s", ctxk->external_name());
2404       }
2405     }
2406     deps.log_dependency();  // put it into the xml log also
2407   }
2408 }
2409 #endif
2410 
2411 #if defined(SUPPORT_DATA_STRUCTS)
2412 
2413 // Print the oops from the underlying CodeBlob.
2414 void nmethod::print_oops(outputStream* st) {
2415   HandleMark hm;
2416   ResourceMark m;
2417   st->print("Oops:");
2418   if (oops_begin() < oops_end()) {
2419     st->cr();
2420     for (oop* p = oops_begin(); p < oops_end(); p++) {
2421       Disassembler::print_location((unsigned char*)p, (unsigned char*)oops_begin(), (unsigned char*)oops_end(), st, true, false);
2422       st->print(PTR_FORMAT " ", *((uintptr_t*)p));
2423       if (*p == Universe::non_oop_word()) {
2424         st->print_cr("NON_OOP");
2425         continue;  // skip non-oops
2426       }
2427       if (*p == NULL) {
2428         st->print_cr("NULL-oop");
2429         continue;  // skip non-oops
2430       }
2431       (*p)->print_value_on(st);
2432       st->cr();
2433     }
2434   } else {
2435     st->print_cr(" <list empty>");
2436   }
2437 }
2438 
2439 // Print metadata pool.
2440 void nmethod::print_metadata(outputStream* st) {
2441   HandleMark hm;
2442   ResourceMark m;
2443   st->print("Metadata:");
2444   if (metadata_begin() < metadata_end()) {
2445     st->cr();
2446     for (Metadata** p = metadata_begin(); p < metadata_end(); p++) {
2447       Disassembler::print_location((unsigned char*)p, (unsigned char*)metadata_begin(), (unsigned char*)metadata_end(), st, true, false);
2448       st->print(PTR_FORMAT " ", *((uintptr_t*)p));
2449       if (*p && *p != Universe::non_oop_word()) {
2450         (*p)->print_value_on(st);
2451       }
2452       st->cr();
2453     }
2454   } else {
2455     st->print_cr(" <list empty>");
2456   }
2457 }
2458 
2459 #ifndef PRODUCT  // ScopeDesc::print_on() is available only then. Declared as PRODUCT_RETURN
2460 void nmethod::print_scopes_on(outputStream* st) {
2461   // Find the first pc desc for all scopes in the code and print it.
2462   ResourceMark rm;
2463   st->print("scopes:");
2464   if (scopes_pcs_begin() < scopes_pcs_end()) {
2465     st->cr();
2466     for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2467       if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
2468         continue;
2469 
2470       ScopeDesc* sd = scope_desc_at(p->real_pc(this));
2471       while (sd != NULL) {
2472         sd->print_on(st, p);  // print output ends with a newline
2473         sd = sd->sender();
2474       }
2475     }
2476   } else {
2477     st->print_cr(" <list empty>");
2478   }
2479 }
2480 #endif
2481 
2482 #ifndef PRODUCT  // RelocIterator does support printing only then.
2483 void nmethod::print_relocations() {
2484   ResourceMark m;       // in case methods get printed via the debugger
2485   tty->print_cr("relocations:");
2486   RelocIterator iter(this);
2487   iter.print();
2488 }
2489 #endif
2490 
2491 void nmethod::print_pcs_on(outputStream* st) {

2492   ResourceMark m;       // in case methods get printed via debugger
2493   st->print("pc-bytecode offsets:");
2494   if (scopes_pcs_begin() < scopes_pcs_end()) {
2495     st->cr();
2496     for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
2497       p->print_on(st, this);  // print output ends with a newline
2498     }
2499   } else {
2500     st->print_cr(" <list empty>");
2501   }
2502 }
2503 
2504 void nmethod::print_handler_table() {
2505   ExceptionHandlerTable(this).print();
2506 }
2507 
2508 void nmethod::print_nul_chk_table() {
2509   ImplicitExceptionTable(this).print(code_begin());
2510 }
2511 
2512 void nmethod::print_recorded_oops() {
2513   const int n = oops_count();
2514   const int log_n = (n<10) ? 1 : (n<100) ? 2 : (n<1000) ? 3 : (n<10000) ? 4 : 6;
2515   tty->print("Recorded oops:");
2516   if (n > 0) {
2517     tty->cr();
2518     for (int i = 0; i < n; i++) {
2519       oop o = oop_at(i);
2520       tty->print("#%*d: " INTPTR_FORMAT " ", log_n, i, p2i(o));
2521       if (o == (oop)Universe::non_oop_word()) {
2522         tty->print("non-oop word");
2523       } else if (o == NULL) {
2524         tty->print("NULL-oop");
2525       } else {
2526         o->print_value_on(tty);




2527       }
2528       tty->cr();
2529     }
2530   } else {
2531     tty->print_cr(" <list empty>");
2532   }
2533 }
2534 
2535 void nmethod::print_recorded_metadata() {
2536   const int n = metadata_count();
2537   const int log_n = (n<10) ? 1 : (n<100) ? 2 : (n<1000) ? 3 : (n<10000) ? 4 : 6;
2538   tty->print("Recorded metadata:");
2539   if (n > 0) {
2540     tty->cr();
2541     for (int i = 0; i < n; i++) {
2542       Metadata* m = metadata_at(i);
2543       tty->print("#%*d: " INTPTR_FORMAT " ", log_n, i, p2i(m));
2544       if (m == (Metadata*)Universe::non_oop_word()) {
2545         tty->print("non-metadata word");
2546       } else if (m == NULL) {
2547         tty->print("NULL-oop");
2548       } else {
2549         Metadata::print_value_on_maybe_null(tty, m);
2550       }
2551       tty->cr();
2552     }
2553   } else {
2554     tty->print_cr(" <list empty>");
2555   }
2556 }
2557 #endif
2558 
2559 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
2560 
2561 void nmethod::print_constant_pool(outputStream* st) {
2562   //-----------------------------------
2563   //---<  Print the constant pool  >---
2564   //-----------------------------------
2565   int consts_size = this->consts_size();
2566   if ( consts_size > 0 ) {
2567     unsigned char* cstart = this->consts_begin();
2568     unsigned char* cp     = cstart;
2569     unsigned char* cend   = cp + consts_size;
2570     unsigned int   bytes_per_line = 4;
2571     unsigned int   CP_alignment   = 8;
2572     unsigned int   n;
2573 
2574     st->cr();
2575 
2576     //---<  print CP header to make clear what's printed  >---
2577     if( ((uintptr_t)cp&(CP_alignment-1)) == 0 ) {
2578       n = bytes_per_line;
2579       st->print_cr("[Constant Pool]");
2580       Disassembler::print_location(cp, cstart, cend, st, true, true);
2581       Disassembler::print_hexdata(cp, n, st, true);
2582       st->cr();
2583     } else {
2584       n = (uintptr_t)cp&(bytes_per_line-1);
2585       st->print_cr("[Constant Pool (unaligned)]");
2586     }
2587 
2588     //---<  print CP contents, bytes_per_line at a time  >---
2589     while (cp < cend) {
2590       Disassembler::print_location(cp, cstart, cend, st, true, false);
2591       Disassembler::print_hexdata(cp, n, st, false);
2592       cp += n;
2593       n   = bytes_per_line;
2594       st->cr();
2595     }
2596 
2597     //---<  Show potential alignment gap between constant pool and code  >---
2598     cend = code_begin();
2599     if( cp < cend ) {
2600       n = 4;
2601       st->print_cr("[Code entry alignment]");
2602       while (cp < cend) {
2603         Disassembler::print_location(cp, cstart, cend, st, false, false);
2604         cp += n;
2605         st->cr();
2606       }
2607     }
2608   } else {
2609     st->print_cr("[Constant Pool (empty)]");
2610   }
2611   st->cr();
2612 }
2613 
2614 #endif
2615 
2616 // Disassemble this nmethod.
2617 // Print additional debug information, if requested. This could be code
2618 // comments, block comments, profiling counters, etc.
2619 // The undisassembled format is useful no disassembler library is available.
2620 // The resulting hex dump (with markers) can be disassembled later, or on
2621 // another system, when/where a disassembler library is available.
2622 void nmethod::decode2(outputStream* ost) const {
2623 
2624   // Called from frame::back_trace_with_decode without ResourceMark.
2625   ResourceMark rm;
2626 
2627   // Make sure we have a valid stream to print on.
2628   outputStream* st = ost ? ost : tty;
2629 
2630 #if defined(SUPPORT_ABSTRACT_ASSEMBLY) && ! defined(SUPPORT_ASSEMBLY)
2631   const bool use_compressed_format    = true;
2632   const bool compressed_with_comments = use_compressed_format && (AbstractDisassembler::show_comment() ||
2633                                                                   AbstractDisassembler::show_block_comment());
2634 #else
2635   const bool use_compressed_format    = Disassembler::is_abstract();
2636   const bool compressed_with_comments = use_compressed_format && (AbstractDisassembler::show_comment() ||
2637                                                                   AbstractDisassembler::show_block_comment());
2638 #endif
2639 
2640   st->cr();
2641   this->print(st);
2642   st->cr();
2643 
2644 #if defined(SUPPORT_ASSEMBLY)
2645   //----------------------------------
2646   //---<  Print real disassembly  >---
2647   //----------------------------------
2648   if (! use_compressed_format) {
2649     Disassembler::decode(const_cast<nmethod*>(this), st);
2650     return;
2651   }
2652 #endif
2653 
2654 #if defined(SUPPORT_ABSTRACT_ASSEMBLY)
2655 
2656   // Compressed undisassembled disassembly format.
2657   // The following stati are defined/supported:
2658   //   = 0 - currently at bol() position, nothing printed yet on current line.
2659   //   = 1 - currently at position after print_location().
2660   //   > 1 - in the midst of printing instruction stream bytes.
2661   int        compressed_format_idx    = 0;
2662   int        code_comment_column      = 0;
2663   const int  instr_maxlen             = Assembler::instr_maxlen();
2664   const uint tabspacing               = 8;
2665   unsigned char* start = this->code_begin();
2666   unsigned char* p     = this->code_begin();
2667   unsigned char* end   = this->code_end();
2668   unsigned char* pss   = p; // start of a code section (used for offsets)
2669 
2670   if ((start == NULL) || (end == NULL)) {
2671     st->print_cr("PrintAssembly not possible due to uninitialized section pointers");
2672     return;
2673   }
2674 #endif
2675 
2676 #if defined(SUPPORT_ABSTRACT_ASSEMBLY)
2677   //---<  plain abstract disassembly, no comments or anything, just section headers  >---
2678   if (use_compressed_format && ! compressed_with_comments) {
2679     const_cast<nmethod*>(this)->print_constant_pool(st);
2680 
2681     //---<  Open the output (Marker for post-mortem disassembler)  >---
2682     st->print_cr("[MachCode]");
2683     const char* header = NULL;
2684     address p0 = p;
2685     while (p < end) {
2686       address pp = p;
2687       while ((p < end) && (header == NULL)) {
2688         header = nmethod_section_label(p);
2689         pp  = p;
2690         p  += Assembler::instr_len(p);
2691       }
2692       if (pp > p0) {
2693         AbstractDisassembler::decode_range_abstract(p0, pp, start, end, st, Assembler::instr_maxlen());
2694         p0 = pp;
2695         p  = pp;
2696         header = NULL;
2697       } else if (header != NULL) {
2698         st->bol();
2699         st->print_cr("%s", header);
2700         header = NULL;
2701       }
2702     }
2703     //---<  Close the output (Marker for post-mortem disassembler)  >---
2704     st->bol();
2705     st->print_cr("[/MachCode]");
2706     return;
2707   }
2708 #endif
2709 
2710 #if defined(SUPPORT_ABSTRACT_ASSEMBLY)
2711   //---<  abstract disassembly with comments and section headers merged in  >---
2712   if (compressed_with_comments) {
2713     const_cast<nmethod*>(this)->print_constant_pool(st);
2714 
2715     //---<  Open the output (Marker for post-mortem disassembler)  >---
2716     st->print_cr("[MachCode]");
2717     while ((p < end) && (p != NULL)) {
2718       const int instruction_size_in_bytes = Assembler::instr_len(p);
2719 
2720       //---<  Block comments for nmethod. Interrupts instruction stream, if any.  >---
2721       // Outputs a bol() before and a cr() after, but only if a comment is printed.
2722       // Prints nmethod_section_label as well.
2723       if (AbstractDisassembler::show_block_comment()) {
2724         print_block_comment(st, p);
2725         if (st->position() == 0) {
2726           compressed_format_idx = 0;
2727         }
2728       }
2729 
2730       //---<  New location information after line break  >---
2731       if (compressed_format_idx == 0) {
2732         code_comment_column   = Disassembler::print_location(p, pss, end, st, false, false);
2733         compressed_format_idx = 1;
2734       }
2735 
2736       //---<  Code comment for current instruction. Address range [p..(p+len))  >---
2737       unsigned char* p_end = p + (ssize_t)instruction_size_in_bytes;
2738       S390_ONLY(if (p_end > end) p_end = end;) // avoid getting past the end
2739 
2740       if (AbstractDisassembler::show_comment() && const_cast<nmethod*>(this)->has_code_comment(p, p_end)) {
2741         //---<  interrupt instruction byte stream for code comment  >---
2742         if (compressed_format_idx > 1) {
2743           st->cr();  // interrupt byte stream
2744           st->cr();  // add an empty line
2745           code_comment_column = Disassembler::print_location(p, pss, end, st, false, false);
2746         }
2747         const_cast<nmethod*>(this)->print_code_comment_on(st, code_comment_column, p, p_end );
2748         st->bol();
2749         compressed_format_idx = 0;
2750       }
2751 
2752       //---<  New location information after line break  >---
2753       if (compressed_format_idx == 0) {
2754         code_comment_column   = Disassembler::print_location(p, pss, end, st, false, false);
2755         compressed_format_idx = 1;
2756       }
2757 
2758       //---<  Nicely align instructions for readability  >---
2759       if (compressed_format_idx > 1) {
2760         Disassembler::print_delimiter(st);
2761       }
2762 
2763       //---<  Now, finally, print the actual instruction bytes  >---
2764       unsigned char* p0 = p;
2765       p = Disassembler::decode_instruction_abstract(p, st, instruction_size_in_bytes, instr_maxlen);
2766       compressed_format_idx += p - p0;
2767 
2768       if (Disassembler::start_newline(compressed_format_idx-1)) {
2769         st->cr();
2770         compressed_format_idx = 0;
2771       }
2772     }
2773     //---<  Close the output (Marker for post-mortem disassembler)  >---
2774     st->bol();
2775     st->print_cr("[/MachCode]");
2776     return;
2777   }
2778 #endif
2779 }
2780 
2781 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
2782 
2783 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) {
2784   RelocIterator iter(this, begin, end);
2785   bool have_one = false;
2786   while (iter.next()) {
2787     have_one = true;
2788     switch (iter.type()) {
2789         case relocInfo::none:                  return "no_reloc";
2790         case relocInfo::oop_type: {
2791           // Get a non-resizable resource-allocated stringStream.
2792           // Our callees make use of (nested) ResourceMarks.
2793           stringStream st(NEW_RESOURCE_ARRAY(char, 1024), 1024);
2794           oop_Relocation* r = iter.oop_reloc();
2795           oop obj = r->oop_value();
2796           st.print("oop(");
2797           if (obj == NULL) st.print("NULL");
2798           else obj->print_value_on(&st);
2799           st.print(")");
2800           return st.as_string();
2801         }
2802         case relocInfo::metadata_type: {
2803           stringStream st;
2804           metadata_Relocation* r = iter.metadata_reloc();
2805           Metadata* obj = r->metadata_value();
2806           st.print("metadata(");
2807           if (obj == NULL) st.print("NULL");
2808           else obj->print_value_on(&st);
2809           st.print(")");
2810           return st.as_string();
2811         }
2812         case relocInfo::runtime_call_type:
2813         case relocInfo::runtime_call_w_cp_type: {


2875         case relocInfo::type_mask:             return "type_bit_mask";
2876 
2877         default:
2878           break;
2879     }
2880   }
2881   return have_one ? "other" : NULL;
2882 }
2883 
2884 // Return a the last scope in (begin..end]
2885 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
2886   PcDesc* p = pc_desc_near(begin+1);
2887   if (p != NULL && p->real_pc(this) <= end) {
2888     return new ScopeDesc(this, p->scope_decode_offset(),
2889                          p->obj_decode_offset(), p->should_reexecute(), p->rethrow_exception(),
2890                          p->return_oop());
2891   }
2892   return NULL;
2893 }
2894 
2895 const char* nmethod::nmethod_section_label(address pos) const {
2896   const char* label = NULL;
2897   if (pos == code_begin())                                              label = "[Instructions begin]";
2898   if (pos == entry_point())                                             label = "[Entry Point]";
2899   if (pos == verified_entry_point())                                    label = "[Verified Entry Point]";
2900   if (has_method_handle_invokes() && (pos == deopt_mh_handler_begin())) label = "[Deopt MH Handler Code]";
2901   if (pos == consts_begin() && pos != insts_begin())                    label = "[Constants]";
2902   // Check stub_code before checking exception_handler or deopt_handler.
2903   if (pos == this->stub_begin())                                        label = "[Stub Code]";
2904   if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin())           label = "[Exception Handler]";
2905   if (JVMCI_ONLY(_deopt_handler_begin != NULL &&) pos == deopt_handler_begin()) label = "[Deopt Handler Code]";
2906   return label;
2907 }
2908 
2909 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
2910   if (print_section_labels) {
2911     const char* label = nmethod_section_label(block_begin);
2912     if (label != NULL) {
2913       stream->bol();
2914       stream->print_cr("%s", label);
2915     }
2916   }
2917 
2918   if (block_begin == entry_point()) {
2919     methodHandle m = method();
2920     if (m.not_null()) {
2921       stream->print("  # ");
2922       m->print_value_on(stream);
2923       stream->cr();
2924     }
2925     if (m.not_null() && !is_osr_method()) {
2926       ResourceMark rm;
2927       int sizeargs = m->size_of_parameters();
2928       BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
2929       VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
2930       {
2931         int sig_index = 0;
2932         if (!m->is_static())
2933           sig_bt[sig_index++] = T_OBJECT; // 'this'
2934         for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
2935           BasicType t = ss.type();
2936           sig_bt[sig_index++] = t;


2993         if (at_old_sp) {
2994           stream->print("  (%s of caller)", spname);
2995           did_old_sp = true;
2996         }
2997         stream->cr();
2998         sig_index += type2size[t];
2999         arg_index += 1;
3000         if (!at_this)  ss.next();
3001       }
3002       if (!did_old_sp) {
3003         stream->print("  # ");
3004         stream->move_to(tab1);
3005         stream->print("[%s+0x%x]", spname, stack_slot_offset);
3006         stream->print("  (%s of caller)", spname);
3007         stream->cr();
3008       }
3009     }
3010   }
3011 }
3012 
3013 // Returns whether this nmethod has code comments.
3014 bool nmethod::has_code_comment(address begin, address end) {
3015   // scopes?
3016   ScopeDesc* sd  = scope_desc_in(begin, end);
3017   if (sd != NULL) return true;
3018 
3019   // relocations?
3020   const char* str = reloc_string_for(begin, end);
3021   if (str != NULL) return true;
3022 
3023   // implicit exceptions?
3024   int cont_offset = ImplicitExceptionTable(this).at(begin - code_begin());
3025   if (cont_offset != 0) return true;
3026 
3027   return false;
3028 }
3029 
3030 void nmethod::print_code_comment_on(outputStream* st, int column, address begin, address end) {
3031   // First, find an oopmap in (begin, end].
3032   // We use the odd half-closed interval so that oop maps and scope descs
3033   // which are tied to the byte after a call are printed with the call itself.
3034   address base = code_begin();
3035   ImmutableOopMapSet* oms = oop_maps();
3036   if (oms != NULL) {
3037     for (int i = 0, imax = oms->count(); i < imax; i++) {
3038       const ImmutableOopMapPair* pair = oms->pair_at(i);
3039       const ImmutableOopMap* om = pair->get_from(oms);
3040       address pc = base + pair->pc_offset();
3041       if (pc > begin) {
3042         if (pc <= end) {
3043           st->move_to(column, 6, 0);
3044           st->print("; ");
3045           om->print_on(st);
3046         }
3047         break;
3048       }
3049     }
3050   }
3051 
3052   // Print any debug info present at this pc.
3053   ScopeDesc* sd  = scope_desc_in(begin, end);
3054   if (sd != NULL) {
3055     st->move_to(column, 6, 0);
3056     if (sd->bci() == SynchronizationEntryBCI) {
3057       st->print(";*synchronization entry");
3058     } else if (sd->bci() == AfterBci) {
3059       st->print(";* method exit (unlocked if synchronized)");
3060     } else if (sd->bci() == UnwindBci) {
3061       st->print(";* unwind (locked if synchronized)");
3062     } else if (sd->bci() == AfterExceptionBci) {
3063       st->print(";* unwind (unlocked if synchronized)");
3064     } else if (sd->bci() == UnknownBci) {
3065       st->print(";* unknown");
3066     } else if (sd->bci() == InvalidFrameStateBci) {
3067       st->print(";* invalid frame state");
3068     } else {
3069       if (sd->method() == NULL) {
3070         st->print("method is NULL");
3071       } else if (sd->method()->is_native()) {
3072         st->print("method is native");
3073       } else {
3074         Bytecodes::Code bc = sd->method()->java_code_at(sd->bci());
3075         st->print(";*%s", Bytecodes::name(bc));


3091         case Bytecodes::_putfield:
3092         case Bytecodes::_getstatic:
3093         case Bytecodes::_putstatic:
3094           {
3095             Bytecode_field field(sd->method(), sd->bci());
3096             st->print(" ");
3097             if (field.name() != NULL)
3098               field.name()->print_symbol_on(st);
3099             else
3100               st->print("<UNKNOWN>");
3101           }
3102         default:
3103           break;
3104         }
3105       }
3106       st->print(" {reexecute=%d rethrow=%d return_oop=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop());
3107     }
3108 
3109     // Print all scopes
3110     for (;sd != NULL; sd = sd->sender()) {
3111       st->move_to(column, 6, 0);
3112       st->print("; -");
3113       if (sd->should_reexecute()) {
3114         st->print(" (reexecute)");
3115       }
3116       if (sd->method() == NULL) {
3117         st->print("method is NULL");
3118       } else {
3119         sd->method()->print_short_name(st);
3120       }
3121       int lineno = sd->method()->line_number_from_bci(sd->bci());
3122       if (lineno != -1) {
3123         st->print("@%d (line %d)", sd->bci(), lineno);
3124       } else {
3125         st->print("@%d", sd->bci());
3126       }
3127       st->cr();
3128     }
3129   }
3130 
3131   // Print relocation information
3132   // Prevent memory leak: allocating without ResourceMark.
3133   ResourceMark rm;
3134   const char* str = reloc_string_for(begin, end);
3135   if (str != NULL) {
3136     if (sd != NULL) st->cr();
3137     st->move_to(column, 6, 0);
3138     st->print(";   {%s}", str);
3139   }
3140   int cont_offset = ImplicitExceptionTable(this).at(begin - code_begin());
3141   if (cont_offset != 0) {
3142     st->move_to(column, 6, 0);
3143     st->print("; implicit exception: dispatches to " INTPTR_FORMAT, p2i(code_begin() + cont_offset));
3144   }
3145 
3146 }
3147 
3148 #endif
3149 
3150 class DirectNativeCallWrapper: public NativeCallWrapper {
3151 private:
3152   NativeCall* _call;
3153 
3154 public:
3155   DirectNativeCallWrapper(NativeCall* call) : _call(call) {}
3156 
3157   virtual address destination() const { return _call->destination(); }
3158   virtual address instruction_address() const { return _call->instruction_address(); }
3159   virtual address next_instruction_address() const { return _call->next_instruction_address(); }
3160   virtual address return_address() const { return _call->return_address(); }
3161 
3162   virtual address get_resolve_call_stub(bool is_optimized) const {
3163     if (is_optimized) {
3164       return SharedRuntime::get_resolve_opt_virtual_call_stub();
3165     }
3166     return SharedRuntime::get_resolve_virtual_call_stub();
3167   }
3168 
3169   virtual void set_destination_mt_safe(address dest) {


3236 address nmethod::call_instruction_address(address pc) const {
3237   if (NativeCall::is_call_before(pc)) {
3238     NativeCall *ncall = nativeCall_before(pc);
3239     return ncall->instruction_address();
3240   }
3241   return NULL;
3242 }
3243 
3244 CompiledStaticCall* nmethod::compiledStaticCall_at(Relocation* call_site) const {
3245   return CompiledDirectStaticCall::at(call_site);
3246 }
3247 
3248 CompiledStaticCall* nmethod::compiledStaticCall_at(address call_site) const {
3249   return CompiledDirectStaticCall::at(call_site);
3250 }
3251 
3252 CompiledStaticCall* nmethod::compiledStaticCall_before(address return_addr) const {
3253   return CompiledDirectStaticCall::before(return_addr);
3254 }
3255 
3256 #if defined(SUPPORT_DATA_STRUCTS)

3257 void nmethod::print_value_on(outputStream* st) const {
3258   st->print("nmethod");
3259   print_on(st, NULL);
3260 }
3261 #endif
3262 
3263 #ifndef PRODUCT
3264 
3265 void nmethod::print_calls(outputStream* st) {
3266   RelocIterator iter(this);
3267   while (iter.next()) {
3268     switch (iter.type()) {
3269     case relocInfo::virtual_call_type:
3270     case relocInfo::opt_virtual_call_type: {
3271       CompiledICLocker ml_verify(this);
3272       CompiledIC_at(&iter)->print();
3273       break;
3274     }
3275     case relocInfo::static_call_type:
3276       st->print_cr("Static call at " INTPTR_FORMAT, p2i(iter.reloc()->addr()));
3277       CompiledDirectStaticCall::at(iter.reloc())->print();
3278       break;
3279     default:
3280       break;
3281     }
3282   }








3283 }
3284 
3285 void nmethod::print_statistics() {
3286   ttyLocker ttyl;
3287   if (xtty != NULL)  xtty->head("statistics type='nmethod'");
3288   native_nmethod_stats.print_native_nmethod_stats();
3289 #ifdef COMPILER1
3290   c1_java_nmethod_stats.print_nmethod_stats("C1");
3291 #endif
3292 #ifdef COMPILER2
3293   c2_java_nmethod_stats.print_nmethod_stats("C2");
3294 #endif
3295 #if INCLUDE_JVMCI
3296   jvmci_java_nmethod_stats.print_nmethod_stats("JVMCI");
3297 #endif
3298   unknown_java_nmethod_stats.print_nmethod_stats("Unknown");
3299   DebugInformationRecorder::print_statistics();
3300 #ifndef PRODUCT
3301   pc_nmethod_stats.print_pc_stats();
3302 #endif


< prev index next >