src/share/vm/code/nmethod.cpp

Wed, 25 Jun 2014 08:56:57 +0200

author
stefank
date
Wed, 25 Jun 2014 08:56:57 +0200
changeset 6985
c64b6b0c40c8
parent 6983
9717199cb8de
child 6991
882004b9e7e1
permissions
-rw-r--r--

8047326: Consolidate all CompiledIC::CompiledIC implementations and move it to compiledIC.cpp
Reviewed-by: vlivanov, ehelin

duke@435 1 /*
drchase@6680 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "code/codeCache.hpp"
stefank@2314 27 #include "code/compiledIC.hpp"
twisti@3050 28 #include "code/dependencies.hpp"
stefank@2314 29 #include "code/nmethod.hpp"
stefank@2314 30 #include "code/scopeDesc.hpp"
stefank@2314 31 #include "compiler/abstractCompiler.hpp"
twisti@2687 32 #include "compiler/compileBroker.hpp"
stefank@2314 33 #include "compiler/compileLog.hpp"
stefank@2314 34 #include "compiler/compilerOracle.hpp"
stefank@2314 35 #include "compiler/disassembler.hpp"
stefank@2314 36 #include "interpreter/bytecode.hpp"
coleenp@4037 37 #include "oops/methodData.hpp"
stefank@2314 38 #include "prims/jvmtiRedefineClassesTrace.hpp"
kamg@2511 39 #include "prims/jvmtiImpl.hpp"
goetz@6911 40 #include "runtime/orderAccess.inline.hpp"
stefank@2314 41 #include "runtime/sharedRuntime.hpp"
stefank@2314 42 #include "runtime/sweeper.hpp"
stefank@2314 43 #include "utilities/dtrace.hpp"
stefank@2314 44 #include "utilities/events.hpp"
stefank@2314 45 #include "utilities/xmlstream.hpp"
stefank@2314 46 #ifdef SHARK
stefank@2314 47 #include "shark/sharkCompiler.hpp"
stefank@2314 48 #endif
duke@435 49
drchase@6680 50 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 51
duke@435 52 #ifdef DTRACE_ENABLED
duke@435 53
duke@435 54 // Only bother with this argument setup if dtrace is available
duke@435 55
dcubed@3202 56 #ifndef USDT2
duke@435 57 HS_DTRACE_PROBE_DECL8(hotspot, compiled__method__load,
duke@435 58 const char*, int, const char*, int, const char*, int, void*, size_t);
duke@435 59
duke@435 60 HS_DTRACE_PROBE_DECL6(hotspot, compiled__method__unload,
duke@435 61 char*, int, char*, int, char*, int);
duke@435 62
duke@435 63 #define DTRACE_METHOD_UNLOAD_PROBE(method) \
duke@435 64 { \
coleenp@4037 65 Method* m = (method); \
duke@435 66 if (m != NULL) { \
coleenp@2497 67 Symbol* klass_name = m->klass_name(); \
coleenp@2497 68 Symbol* name = m->name(); \
coleenp@2497 69 Symbol* signature = m->signature(); \
duke@435 70 HS_DTRACE_PROBE6(hotspot, compiled__method__unload, \
duke@435 71 klass_name->bytes(), klass_name->utf8_length(), \
duke@435 72 name->bytes(), name->utf8_length(), \
duke@435 73 signature->bytes(), signature->utf8_length()); \
duke@435 74 } \
duke@435 75 }
dcubed@3202 76 #else /* USDT2 */
dcubed@3202 77 #define DTRACE_METHOD_UNLOAD_PROBE(method) \
dcubed@3202 78 { \
coleenp@4037 79 Method* m = (method); \
dcubed@3202 80 if (m != NULL) { \
dcubed@3202 81 Symbol* klass_name = m->klass_name(); \
dcubed@3202 82 Symbol* name = m->name(); \
dcubed@3202 83 Symbol* signature = m->signature(); \
dcubed@3202 84 HOTSPOT_COMPILED_METHOD_UNLOAD( \
dcubed@3202 85 (char *) klass_name->bytes(), klass_name->utf8_length(), \
dcubed@3202 86 (char *) name->bytes(), name->utf8_length(), \
dcubed@3202 87 (char *) signature->bytes(), signature->utf8_length()); \
dcubed@3202 88 } \
dcubed@3202 89 }
dcubed@3202 90 #endif /* USDT2 */
duke@435 91
duke@435 92 #else // ndef DTRACE_ENABLED
duke@435 93
duke@435 94 #define DTRACE_METHOD_UNLOAD_PROBE(method)
duke@435 95
duke@435 96 #endif
duke@435 97
duke@435 98 bool nmethod::is_compiled_by_c1() const {
roland@5629 99 if (compiler() == NULL) {
roland@5629 100 return false;
roland@5629 101 }
duke@435 102 return compiler()->is_c1();
duke@435 103 }
duke@435 104 bool nmethod::is_compiled_by_c2() const {
roland@5629 105 if (compiler() == NULL) {
roland@5629 106 return false;
roland@5629 107 }
duke@435 108 return compiler()->is_c2();
duke@435 109 }
twisti@2047 110 bool nmethod::is_compiled_by_shark() const {
roland@5629 111 if (compiler() == NULL) {
roland@5629 112 return false;
roland@5629 113 }
twisti@2047 114 return compiler()->is_shark();
twisti@2047 115 }
duke@435 116
duke@435 117
duke@435 118
duke@435 119 //---------------------------------------------------------------------------------
duke@435 120 // NMethod statistics
duke@435 121 // They are printed under various flags, including:
duke@435 122 // PrintC1Statistics, PrintOptoStatistics, LogVMOutput, and LogCompilation.
duke@435 123 // (In the latter two cases, they like other stats are printed to the log only.)
duke@435 124
duke@435 125 #ifndef PRODUCT
duke@435 126 // These variables are put into one block to reduce relocations
duke@435 127 // and make it simpler to print from the debugger.
duke@435 128 static
duke@435 129 struct nmethod_stats_struct {
duke@435 130 int nmethod_count;
duke@435 131 int total_size;
duke@435 132 int relocation_size;
twisti@2117 133 int consts_size;
twisti@2103 134 int insts_size;
duke@435 135 int stub_size;
duke@435 136 int scopes_data_size;
duke@435 137 int scopes_pcs_size;
duke@435 138 int dependencies_size;
duke@435 139 int handler_table_size;
duke@435 140 int nul_chk_table_size;
duke@435 141 int oops_size;
duke@435 142
duke@435 143 void note_nmethod(nmethod* nm) {
duke@435 144 nmethod_count += 1;
duke@435 145 total_size += nm->size();
duke@435 146 relocation_size += nm->relocation_size();
twisti@2117 147 consts_size += nm->consts_size();
twisti@2103 148 insts_size += nm->insts_size();
duke@435 149 stub_size += nm->stub_size();
twisti@1918 150 oops_size += nm->oops_size();
duke@435 151 scopes_data_size += nm->scopes_data_size();
duke@435 152 scopes_pcs_size += nm->scopes_pcs_size();
duke@435 153 dependencies_size += nm->dependencies_size();
duke@435 154 handler_table_size += nm->handler_table_size();
duke@435 155 nul_chk_table_size += nm->nul_chk_table_size();
duke@435 156 }
duke@435 157 void print_nmethod_stats() {
duke@435 158 if (nmethod_count == 0) return;
duke@435 159 tty->print_cr("Statistics for %d bytecoded nmethods:", nmethod_count);
duke@435 160 if (total_size != 0) tty->print_cr(" total in heap = %d", total_size);
duke@435 161 if (relocation_size != 0) tty->print_cr(" relocation = %d", relocation_size);
twisti@2117 162 if (consts_size != 0) tty->print_cr(" constants = %d", consts_size);
twisti@2103 163 if (insts_size != 0) tty->print_cr(" main code = %d", insts_size);
duke@435 164 if (stub_size != 0) tty->print_cr(" stub code = %d", stub_size);
twisti@1918 165 if (oops_size != 0) tty->print_cr(" oops = %d", oops_size);
duke@435 166 if (scopes_data_size != 0) tty->print_cr(" scopes data = %d", scopes_data_size);
duke@435 167 if (scopes_pcs_size != 0) tty->print_cr(" scopes pcs = %d", scopes_pcs_size);
duke@435 168 if (dependencies_size != 0) tty->print_cr(" dependencies = %d", dependencies_size);
duke@435 169 if (handler_table_size != 0) tty->print_cr(" handler table = %d", handler_table_size);
duke@435 170 if (nul_chk_table_size != 0) tty->print_cr(" nul chk table = %d", nul_chk_table_size);
duke@435 171 }
duke@435 172
duke@435 173 int native_nmethod_count;
duke@435 174 int native_total_size;
duke@435 175 int native_relocation_size;
twisti@2103 176 int native_insts_size;
duke@435 177 int native_oops_size;
duke@435 178 void note_native_nmethod(nmethod* nm) {
duke@435 179 native_nmethod_count += 1;
duke@435 180 native_total_size += nm->size();
duke@435 181 native_relocation_size += nm->relocation_size();
twisti@2103 182 native_insts_size += nm->insts_size();
duke@435 183 native_oops_size += nm->oops_size();
duke@435 184 }
duke@435 185 void print_native_nmethod_stats() {
duke@435 186 if (native_nmethod_count == 0) return;
duke@435 187 tty->print_cr("Statistics for %d native nmethods:", native_nmethod_count);
duke@435 188 if (native_total_size != 0) tty->print_cr(" N. total size = %d", native_total_size);
duke@435 189 if (native_relocation_size != 0) tty->print_cr(" N. relocation = %d", native_relocation_size);
twisti@2103 190 if (native_insts_size != 0) tty->print_cr(" N. main code = %d", native_insts_size);
duke@435 191 if (native_oops_size != 0) tty->print_cr(" N. oops = %d", native_oops_size);
duke@435 192 }
duke@435 193
duke@435 194 int pc_desc_resets; // number of resets (= number of caches)
duke@435 195 int pc_desc_queries; // queries to nmethod::find_pc_desc
duke@435 196 int pc_desc_approx; // number of those which have approximate true
phh@2621 197 int pc_desc_repeats; // number of _pc_descs[0] hits
duke@435 198 int pc_desc_hits; // number of LRU cache hits
duke@435 199 int pc_desc_tests; // total number of PcDesc examinations
duke@435 200 int pc_desc_searches; // total number of quasi-binary search steps
duke@435 201 int pc_desc_adds; // number of LUR cache insertions
duke@435 202
duke@435 203 void print_pc_stats() {
duke@435 204 tty->print_cr("PcDesc Statistics: %d queries, %.2f comparisons per query",
duke@435 205 pc_desc_queries,
duke@435 206 (double)(pc_desc_tests + pc_desc_searches)
duke@435 207 / pc_desc_queries);
duke@435 208 tty->print_cr(" caches=%d queries=%d/%d, hits=%d+%d, tests=%d+%d, adds=%d",
duke@435 209 pc_desc_resets,
duke@435 210 pc_desc_queries, pc_desc_approx,
duke@435 211 pc_desc_repeats, pc_desc_hits,
duke@435 212 pc_desc_tests, pc_desc_searches, pc_desc_adds);
duke@435 213 }
duke@435 214 } nmethod_stats;
duke@435 215 #endif //PRODUCT
duke@435 216
twisti@2603 217
duke@435 218 //---------------------------------------------------------------------------------
duke@435 219
duke@435 220
duke@435 221 ExceptionCache::ExceptionCache(Handle exception, address pc, address handler) {
duke@435 222 assert(pc != NULL, "Must be non null");
duke@435 223 assert(exception.not_null(), "Must be non null");
duke@435 224 assert(handler != NULL, "Must be non null");
duke@435 225
duke@435 226 _count = 0;
duke@435 227 _exception_type = exception->klass();
duke@435 228 _next = NULL;
duke@435 229
duke@435 230 add_address_and_handler(pc,handler);
duke@435 231 }
duke@435 232
duke@435 233
duke@435 234 address ExceptionCache::match(Handle exception, address pc) {
duke@435 235 assert(pc != NULL,"Must be non null");
duke@435 236 assert(exception.not_null(),"Must be non null");
duke@435 237 if (exception->klass() == exception_type()) {
duke@435 238 return (test_address(pc));
duke@435 239 }
duke@435 240
duke@435 241 return NULL;
duke@435 242 }
duke@435 243
duke@435 244
duke@435 245 bool ExceptionCache::match_exception_with_space(Handle exception) {
duke@435 246 assert(exception.not_null(),"Must be non null");
duke@435 247 if (exception->klass() == exception_type() && count() < cache_size) {
duke@435 248 return true;
duke@435 249 }
duke@435 250 return false;
duke@435 251 }
duke@435 252
duke@435 253
duke@435 254 address ExceptionCache::test_address(address addr) {
duke@435 255 for (int i=0; i<count(); i++) {
duke@435 256 if (pc_at(i) == addr) {
duke@435 257 return handler_at(i);
duke@435 258 }
duke@435 259 }
duke@435 260 return NULL;
duke@435 261 }
duke@435 262
duke@435 263
duke@435 264 bool ExceptionCache::add_address_and_handler(address addr, address handler) {
duke@435 265 if (test_address(addr) == handler) return true;
duke@435 266 if (count() < cache_size) {
duke@435 267 set_pc_at(count(),addr);
duke@435 268 set_handler_at(count(), handler);
duke@435 269 increment_count();
duke@435 270 return true;
duke@435 271 }
duke@435 272 return false;
duke@435 273 }
duke@435 274
duke@435 275
duke@435 276 // private method for handling exception cache
duke@435 277 // These methods are private, and used to manipulate the exception cache
duke@435 278 // directly.
duke@435 279 ExceptionCache* nmethod::exception_cache_entry_for_exception(Handle exception) {
duke@435 280 ExceptionCache* ec = exception_cache();
duke@435 281 while (ec != NULL) {
duke@435 282 if (ec->match_exception_with_space(exception)) {
duke@435 283 return ec;
duke@435 284 }
duke@435 285 ec = ec->next();
duke@435 286 }
duke@435 287 return NULL;
duke@435 288 }
duke@435 289
duke@435 290
duke@435 291 //-----------------------------------------------------------------------------
duke@435 292
duke@435 293
duke@435 294 // Helper used by both find_pc_desc methods.
duke@435 295 static inline bool match_desc(PcDesc* pc, int pc_offset, bool approximate) {
duke@435 296 NOT_PRODUCT(++nmethod_stats.pc_desc_tests);
duke@435 297 if (!approximate)
duke@435 298 return pc->pc_offset() == pc_offset;
duke@435 299 else
duke@435 300 return (pc-1)->pc_offset() < pc_offset && pc_offset <= pc->pc_offset();
duke@435 301 }
duke@435 302
duke@435 303 void PcDescCache::reset_to(PcDesc* initial_pc_desc) {
duke@435 304 if (initial_pc_desc == NULL) {
phh@2621 305 _pc_descs[0] = NULL; // native method; no PcDescs at all
duke@435 306 return;
duke@435 307 }
duke@435 308 NOT_PRODUCT(++nmethod_stats.pc_desc_resets);
duke@435 309 // reset the cache by filling it with benign (non-null) values
duke@435 310 assert(initial_pc_desc->pc_offset() < 0, "must be sentinel");
duke@435 311 for (int i = 0; i < cache_size; i++)
duke@435 312 _pc_descs[i] = initial_pc_desc;
duke@435 313 }
duke@435 314
duke@435 315 PcDesc* PcDescCache::find_pc_desc(int pc_offset, bool approximate) {
duke@435 316 NOT_PRODUCT(++nmethod_stats.pc_desc_queries);
phh@2621 317 NOT_PRODUCT(if (approximate) ++nmethod_stats.pc_desc_approx);
phh@2621 318
phh@2621 319 // Note: one might think that caching the most recently
phh@2621 320 // read value separately would be a win, but one would be
phh@2621 321 // wrong. When many threads are updating it, the cache
phh@2621 322 // line it's in would bounce between caches, negating
phh@2621 323 // any benefit.
duke@435 324
duke@435 325 // In order to prevent race conditions do not load cache elements
duke@435 326 // repeatedly, but use a local copy:
duke@435 327 PcDesc* res;
duke@435 328
phh@2621 329 // Step one: Check the most recently added value.
phh@2621 330 res = _pc_descs[0];
phh@2621 331 if (res == NULL) return NULL; // native method; no PcDescs at all
duke@435 332 if (match_desc(res, pc_offset, approximate)) {
duke@435 333 NOT_PRODUCT(++nmethod_stats.pc_desc_repeats);
duke@435 334 return res;
duke@435 335 }
duke@435 336
phh@2621 337 // Step two: Check the rest of the LRU cache.
phh@2621 338 for (int i = 1; i < cache_size; ++i) {
duke@435 339 res = _pc_descs[i];
phh@2621 340 if (res->pc_offset() < 0) break; // optimization: skip empty cache
duke@435 341 if (match_desc(res, pc_offset, approximate)) {
duke@435 342 NOT_PRODUCT(++nmethod_stats.pc_desc_hits);
duke@435 343 return res;
duke@435 344 }
duke@435 345 }
duke@435 346
duke@435 347 // Report failure.
duke@435 348 return NULL;
duke@435 349 }
duke@435 350
duke@435 351 void PcDescCache::add_pc_desc(PcDesc* pc_desc) {
duke@435 352 NOT_PRODUCT(++nmethod_stats.pc_desc_adds);
phh@2621 353 // Update the LRU cache by shifting pc_desc forward.
duke@435 354 for (int i = 0; i < cache_size; i++) {
duke@435 355 PcDesc* next = _pc_descs[i];
duke@435 356 _pc_descs[i] = pc_desc;
duke@435 357 pc_desc = next;
duke@435 358 }
duke@435 359 }
duke@435 360
duke@435 361 // adjust pcs_size so that it is a multiple of both oopSize and
duke@435 362 // sizeof(PcDesc) (assumes that if sizeof(PcDesc) is not a multiple
duke@435 363 // of oopSize, then 2*sizeof(PcDesc) is)
phh@2621 364 static int adjust_pcs_size(int pcs_size) {
duke@435 365 int nsize = round_to(pcs_size, oopSize);
duke@435 366 if ((nsize % sizeof(PcDesc)) != 0) {
duke@435 367 nsize = pcs_size + sizeof(PcDesc);
duke@435 368 }
phh@2621 369 assert((nsize % oopSize) == 0, "correct alignment");
duke@435 370 return nsize;
duke@435 371 }
duke@435 372
duke@435 373 //-----------------------------------------------------------------------------
duke@435 374
duke@435 375
duke@435 376 void nmethod::add_exception_cache_entry(ExceptionCache* new_entry) {
duke@435 377 assert(ExceptionCache_lock->owned_by_self(),"Must hold the ExceptionCache_lock");
duke@435 378 assert(new_entry != NULL,"Must be non null");
duke@435 379 assert(new_entry->next() == NULL, "Must be null");
duke@435 380
duke@435 381 if (exception_cache() != NULL) {
duke@435 382 new_entry->set_next(exception_cache());
duke@435 383 }
duke@435 384 set_exception_cache(new_entry);
duke@435 385 }
duke@435 386
stefank@6983 387 void nmethod::clean_exception_cache(BoolObjectClosure* is_alive) {
duke@435 388 ExceptionCache* prev = NULL;
duke@435 389 ExceptionCache* curr = exception_cache();
stefank@6983 390
stefank@6983 391 while (curr != NULL) {
stefank@6983 392 ExceptionCache* next = curr->next();
stefank@6983 393
stefank@6983 394 Klass* ex_klass = curr->exception_type();
stefank@6983 395 if (ex_klass != NULL && !ex_klass->is_loader_alive(is_alive)) {
stefank@6983 396 if (prev == NULL) {
stefank@6983 397 set_exception_cache(next);
stefank@6983 398 } else {
stefank@6983 399 prev->set_next(next);
stefank@6983 400 }
stefank@6983 401 delete curr;
stefank@6983 402 // prev stays the same.
stefank@6983 403 } else {
stefank@6983 404 prev = curr;
stefank@6983 405 }
stefank@6983 406
stefank@6983 407 curr = next;
duke@435 408 }
duke@435 409 }
duke@435 410
duke@435 411 // public method for accessing the exception cache
duke@435 412 // These are the public access methods.
duke@435 413 address nmethod::handler_for_exception_and_pc(Handle exception, address pc) {
duke@435 414 // We never grab a lock to read the exception cache, so we may
duke@435 415 // have false negatives. This is okay, as it can only happen during
duke@435 416 // the first few exception lookups for a given nmethod.
duke@435 417 ExceptionCache* ec = exception_cache();
duke@435 418 while (ec != NULL) {
duke@435 419 address ret_val;
duke@435 420 if ((ret_val = ec->match(exception,pc)) != NULL) {
duke@435 421 return ret_val;
duke@435 422 }
duke@435 423 ec = ec->next();
duke@435 424 }
duke@435 425 return NULL;
duke@435 426 }
duke@435 427
duke@435 428
duke@435 429 void nmethod::add_handler_for_exception_and_pc(Handle exception, address pc, address handler) {
duke@435 430 // There are potential race conditions during exception cache updates, so we
duke@435 431 // must own the ExceptionCache_lock before doing ANY modifications. Because
twisti@1040 432 // we don't lock during reads, it is possible to have several threads attempt
duke@435 433 // to update the cache with the same data. We need to check for already inserted
duke@435 434 // copies of the current data before adding it.
duke@435 435
duke@435 436 MutexLocker ml(ExceptionCache_lock);
duke@435 437 ExceptionCache* target_entry = exception_cache_entry_for_exception(exception);
duke@435 438
duke@435 439 if (target_entry == NULL || !target_entry->add_address_and_handler(pc,handler)) {
duke@435 440 target_entry = new ExceptionCache(exception,pc,handler);
duke@435 441 add_exception_cache_entry(target_entry);
duke@435 442 }
duke@435 443 }
duke@435 444
duke@435 445
duke@435 446 //-------------end of code for ExceptionCache--------------
duke@435 447
duke@435 448
duke@435 449 int nmethod::total_size() const {
duke@435 450 return
twisti@2117 451 consts_size() +
twisti@2103 452 insts_size() +
duke@435 453 stub_size() +
duke@435 454 scopes_data_size() +
duke@435 455 scopes_pcs_size() +
duke@435 456 handler_table_size() +
duke@435 457 nul_chk_table_size();
duke@435 458 }
duke@435 459
duke@435 460 const char* nmethod::compile_kind() const {
duke@435 461 if (is_osr_method()) return "osr";
never@1544 462 if (method() != NULL && is_native_method()) return "c2n";
duke@435 463 return NULL;
duke@435 464 }
duke@435 465
never@1999 466 // Fill in default values for various flag fields
never@1999 467 void nmethod::init_defaults() {
kvn@6172 468 _state = in_use;
never@1999 469 _marked_for_reclamation = 0;
never@1999 470 _has_flushed_dependencies = 0;
never@1999 471 _has_unsafe_access = 0;
never@1999 472 _has_method_handle_invokes = 0;
never@3500 473 _lazy_critical_native = 0;
kvn@4103 474 _has_wide_vectors = 0;
never@1999 475 _marked_for_deoptimization = 0;
never@1999 476 _lock_count = 0;
never@1999 477 _stack_traversal_mark = 0;
never@1999 478 _unload_reported = false; // jvmti state
never@1999 479
never@2081 480 #ifdef ASSERT
never@2081 481 _oops_are_stale = false;
never@2081 482 #endif
never@2081 483
never@1999 484 _oops_do_mark_link = NULL;
never@1999 485 _jmethod_id = NULL;
never@1999 486 _osr_link = NULL;
never@1999 487 _scavenge_root_link = NULL;
never@1999 488 _scavenge_root_state = 0;
never@1999 489 _compiler = NULL;
kvn@6429 490 #if INCLUDE_RTM_OPT
kvn@6429 491 _rtm_state = NoRTM;
kvn@6429 492 #endif
never@1999 493 #ifdef HAVE_DTRACE_H
never@1999 494 _trap_offset = 0;
never@1999 495 #endif // def HAVE_DTRACE_H
never@1999 496 }
duke@435 497
duke@435 498 nmethod* nmethod::new_native_nmethod(methodHandle method,
twisti@2687 499 int compile_id,
duke@435 500 CodeBuffer *code_buffer,
duke@435 501 int vep_offset,
duke@435 502 int frame_complete,
duke@435 503 int frame_size,
duke@435 504 ByteSize basic_lock_owner_sp_offset,
duke@435 505 ByteSize basic_lock_sp_offset,
duke@435 506 OopMapSet* oop_maps) {
coleenp@4037 507 code_buffer->finalize_oop_references(method);
duke@435 508 // create nmethod
duke@435 509 nmethod* nm = NULL;
duke@435 510 {
duke@435 511 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@435 512 int native_nmethod_size = allocation_size(code_buffer, sizeof(nmethod));
neliasso@4952 513 CodeOffsets offsets;
neliasso@4952 514 offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
neliasso@4952 515 offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
neliasso@4952 516 nm = new (native_nmethod_size) nmethod(method(), native_nmethod_size,
neliasso@4952 517 compile_id, &offsets,
neliasso@4952 518 code_buffer, frame_size,
neliasso@4952 519 basic_lock_owner_sp_offset,
neliasso@4952 520 basic_lock_sp_offset, oop_maps);
neliasso@4952 521 NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_native_nmethod(nm));
neliasso@4952 522 if (PrintAssembly && nm != NULL) {
neliasso@4952 523 Disassembler::decode(nm);
morris@4864 524 }
duke@435 525 }
duke@435 526 // verify nmethod
duke@435 527 debug_only(if (nm) nm->verify();) // might block
duke@435 528
duke@435 529 if (nm != NULL) {
duke@435 530 nm->log_new_nmethod();
duke@435 531 }
duke@435 532
duke@435 533 return nm;
duke@435 534 }
duke@435 535
kamg@551 536 #ifdef HAVE_DTRACE_H
kamg@551 537 nmethod* nmethod::new_dtrace_nmethod(methodHandle method,
kamg@551 538 CodeBuffer *code_buffer,
kamg@551 539 int vep_offset,
kamg@551 540 int trap_offset,
kamg@551 541 int frame_complete,
kamg@551 542 int frame_size) {
coleenp@4037 543 code_buffer->finalize_oop_references(method);
kamg@551 544 // create nmethod
kamg@551 545 nmethod* nm = NULL;
kamg@551 546 {
kamg@551 547 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
kamg@551 548 int nmethod_size = allocation_size(code_buffer, sizeof(nmethod));
neliasso@4952 549 CodeOffsets offsets;
neliasso@4952 550 offsets.set_value(CodeOffsets::Verified_Entry, vep_offset);
neliasso@4952 551 offsets.set_value(CodeOffsets::Dtrace_trap, trap_offset);
neliasso@4952 552 offsets.set_value(CodeOffsets::Frame_Complete, frame_complete);
kamg@551 553
neliasso@4952 554 nm = new (nmethod_size) nmethod(method(), nmethod_size,
neliasso@4952 555 &offsets, code_buffer, frame_size);
kamg@551 556
neliasso@4952 557 NOT_PRODUCT(if (nm != NULL) nmethod_stats.note_nmethod(nm));
neliasso@4952 558 if (PrintAssembly && nm != NULL) {
neliasso@4952 559 Disassembler::decode(nm);
morris@4864 560 }
kamg@551 561 }
kamg@551 562 // verify nmethod
kamg@551 563 debug_only(if (nm) nm->verify();) // might block
kamg@551 564
kamg@551 565 if (nm != NULL) {
kamg@551 566 nm->log_new_nmethod();
kamg@551 567 }
kamg@551 568
kamg@551 569 return nm;
kamg@551 570 }
kamg@551 571
kamg@551 572 #endif // def HAVE_DTRACE_H
kamg@551 573
duke@435 574 nmethod* nmethod::new_nmethod(methodHandle method,
duke@435 575 int compile_id,
duke@435 576 int entry_bci,
duke@435 577 CodeOffsets* offsets,
duke@435 578 int orig_pc_offset,
duke@435 579 DebugInformationRecorder* debug_info,
duke@435 580 Dependencies* dependencies,
duke@435 581 CodeBuffer* code_buffer, int frame_size,
duke@435 582 OopMapSet* oop_maps,
duke@435 583 ExceptionHandlerTable* handler_table,
duke@435 584 ImplicitExceptionTable* nul_chk_table,
duke@435 585 AbstractCompiler* compiler,
duke@435 586 int comp_level
duke@435 587 )
duke@435 588 {
duke@435 589 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
coleenp@4037 590 code_buffer->finalize_oop_references(method);
duke@435 591 // create nmethod
duke@435 592 nmethod* nm = NULL;
duke@435 593 { MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@435 594 int nmethod_size =
duke@435 595 allocation_size(code_buffer, sizeof(nmethod))
duke@435 596 + adjust_pcs_size(debug_info->pcs_size())
duke@435 597 + round_to(dependencies->size_in_bytes() , oopSize)
duke@435 598 + round_to(handler_table->size_in_bytes(), oopSize)
duke@435 599 + round_to(nul_chk_table->size_in_bytes(), oopSize)
duke@435 600 + round_to(debug_info->data_size() , oopSize);
neliasso@4952 601
neliasso@4952 602 nm = new (nmethod_size)
neliasso@4952 603 nmethod(method(), nmethod_size, compile_id, entry_bci, offsets,
neliasso@4952 604 orig_pc_offset, debug_info, dependencies, code_buffer, frame_size,
neliasso@4952 605 oop_maps,
neliasso@4952 606 handler_table,
neliasso@4952 607 nul_chk_table,
neliasso@4952 608 compiler,
neliasso@4952 609 comp_level);
neliasso@4952 610
duke@435 611 if (nm != NULL) {
duke@435 612 // To make dependency checking during class loading fast, record
duke@435 613 // the nmethod dependencies in the classes it is dependent on.
duke@435 614 // This allows the dependency checking code to simply walk the
duke@435 615 // class hierarchy above the loaded class, checking only nmethods
duke@435 616 // which are dependent on those classes. The slow way is to
duke@435 617 // check every nmethod for dependencies which makes it linear in
duke@435 618 // the number of methods compiled. For applications with a lot
duke@435 619 // classes the slow way is too slow.
duke@435 620 for (Dependencies::DepStream deps(nm); deps.next(); ) {
coleenp@4037 621 Klass* klass = deps.context_type();
neliasso@4952 622 if (klass == NULL) {
neliasso@4952 623 continue; // ignore things like evol_method
neliasso@4952 624 }
duke@435 625
duke@435 626 // record this nmethod as dependent on this klass
coleenp@4037 627 InstanceKlass::cast(klass)->add_dependent_nmethod(nm);
duke@435 628 }
vlivanov@6096 629 NOT_PRODUCT(nmethod_stats.note_nmethod(nm));
goetz@6488 630 if (PrintAssembly || CompilerOracle::has_option_string(method, "PrintAssembly")) {
vlivanov@6096 631 Disassembler::decode(nm);
vlivanov@6096 632 }
neliasso@4952 633 }
duke@435 634 }
vlivanov@6096 635 // Do verification and logging outside CodeCache_lock.
duke@435 636 if (nm != NULL) {
vlivanov@6096 637 // Safepoints in nmethod::verify aren't allowed because nm hasn't been installed yet.
vlivanov@6096 638 DEBUG_ONLY(nm->verify();)
duke@435 639 nm->log_new_nmethod();
duke@435 640 }
duke@435 641 return nm;
duke@435 642 }
duke@435 643
duke@435 644
duke@435 645 // For native wrappers
duke@435 646 nmethod::nmethod(
coleenp@4037 647 Method* method,
duke@435 648 int nmethod_size,
twisti@2687 649 int compile_id,
duke@435 650 CodeOffsets* offsets,
duke@435 651 CodeBuffer* code_buffer,
duke@435 652 int frame_size,
duke@435 653 ByteSize basic_lock_owner_sp_offset,
duke@435 654 ByteSize basic_lock_sp_offset,
duke@435 655 OopMapSet* oop_maps )
duke@435 656 : CodeBlob("native nmethod", code_buffer, sizeof(nmethod),
duke@435 657 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
kamg@2361 658 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
kamg@2361 659 _native_basic_lock_sp_offset(basic_lock_sp_offset)
duke@435 660 {
duke@435 661 {
duke@435 662 debug_only(No_Safepoint_Verifier nsv;)
duke@435 663 assert_locked_or_safepoint(CodeCache_lock);
duke@435 664
never@1999 665 init_defaults();
duke@435 666 _method = method;
duke@435 667 _entry_bci = InvocationEntryBci;
duke@435 668 // We have no exception handler or deopt handler make the
duke@435 669 // values something that will never match a pc like the nmethod vtable entry
duke@435 670 _exception_offset = 0;
duke@435 671 _deoptimize_offset = 0;
twisti@1639 672 _deoptimize_mh_offset = 0;
duke@435 673 _orig_pc_offset = 0;
never@1999 674
twisti@2103 675 _consts_offset = data_offset();
duke@435 676 _stub_offset = data_offset();
twisti@1918 677 _oops_offset = data_offset();
coleenp@4037 678 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize);
coleenp@4037 679 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize);
duke@435 680 _scopes_pcs_offset = _scopes_data_offset;
duke@435 681 _dependencies_offset = _scopes_pcs_offset;
duke@435 682 _handler_table_offset = _dependencies_offset;
duke@435 683 _nul_chk_table_offset = _handler_table_offset;
duke@435 684 _nmethod_end_offset = _nul_chk_table_offset;
twisti@2687 685 _compile_id = compile_id;
duke@435 686 _comp_level = CompLevel_none;
twisti@2103 687 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry);
twisti@2103 688 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry);
duke@435 689 _osr_entry_point = NULL;
duke@435 690 _exception_cache = NULL;
duke@435 691 _pc_desc_cache.reset_to(NULL);
anoll@5792 692 _hotness_counter = NMethodSweeper::hotness_counter_reset_val();
duke@435 693
coleenp@4037 694 code_buffer->copy_values_to(this);
pliden@6905 695 if (ScavengeRootsInCode) {
pliden@6905 696 if (detect_scavenge_root_oops()) {
pliden@6905 697 CodeCache::add_scavenge_root_nmethod(this);
pliden@6905 698 }
johnc@5548 699 Universe::heap()->register_nmethod(this);
never@2658 700 }
jrose@1424 701 debug_only(verify_scavenge_root_oops());
duke@435 702 CodeCache::commit(this);
duke@435 703 }
duke@435 704
duke@435 705 if (PrintNativeNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) {
duke@435 706 ttyLocker ttyl; // keep the following output all in one block
duke@435 707 // This output goes directly to the tty, not the compiler log.
duke@435 708 // To enable tools to match it up with the compilation activity,
duke@435 709 // be sure to tag this tty output with the compile ID.
duke@435 710 if (xtty != NULL) {
duke@435 711 xtty->begin_head("print_native_nmethod");
duke@435 712 xtty->method(_method);
duke@435 713 xtty->stamp();
duke@435 714 xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
duke@435 715 }
duke@435 716 // print the header part first
duke@435 717 print();
duke@435 718 // then print the requested information
duke@435 719 if (PrintNativeNMethods) {
duke@435 720 print_code();
twisti@4101 721 if (oop_maps != NULL) {
twisti@4101 722 oop_maps->print();
twisti@4101 723 }
duke@435 724 }
duke@435 725 if (PrintRelocations) {
duke@435 726 print_relocations();
duke@435 727 }
duke@435 728 if (xtty != NULL) {
duke@435 729 xtty->tail("print_native_nmethod");
duke@435 730 }
duke@435 731 }
duke@435 732 }
duke@435 733
kamg@551 734 // For dtrace wrappers
kamg@551 735 #ifdef HAVE_DTRACE_H
kamg@551 736 nmethod::nmethod(
coleenp@4037 737 Method* method,
kamg@551 738 int nmethod_size,
kamg@551 739 CodeOffsets* offsets,
kamg@551 740 CodeBuffer* code_buffer,
kamg@551 741 int frame_size)
kamg@551 742 : CodeBlob("dtrace nmethod", code_buffer, sizeof(nmethod),
kamg@551 743 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, NULL),
kamg@2361 744 _native_receiver_sp_offset(in_ByteSize(-1)),
kamg@2361 745 _native_basic_lock_sp_offset(in_ByteSize(-1))
kamg@551 746 {
kamg@551 747 {
kamg@551 748 debug_only(No_Safepoint_Verifier nsv;)
kamg@551 749 assert_locked_or_safepoint(CodeCache_lock);
kamg@551 750
never@1999 751 init_defaults();
kamg@551 752 _method = method;
kamg@551 753 _entry_bci = InvocationEntryBci;
kamg@551 754 // We have no exception handler or deopt handler make the
kamg@551 755 // values something that will never match a pc like the nmethod vtable entry
kamg@551 756 _exception_offset = 0;
kamg@551 757 _deoptimize_offset = 0;
twisti@1639 758 _deoptimize_mh_offset = 0;
never@1813 759 _unwind_handler_offset = -1;
kamg@551 760 _trap_offset = offsets->value(CodeOffsets::Dtrace_trap);
kamg@551 761 _orig_pc_offset = 0;
twisti@2103 762 _consts_offset = data_offset();
kamg@551 763 _stub_offset = data_offset();
twisti@1918 764 _oops_offset = data_offset();
coleenp@4037 765 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize);
coleenp@4037 766 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize);
kamg@551 767 _scopes_pcs_offset = _scopes_data_offset;
kamg@551 768 _dependencies_offset = _scopes_pcs_offset;
kamg@551 769 _handler_table_offset = _dependencies_offset;
kamg@551 770 _nul_chk_table_offset = _handler_table_offset;
kamg@551 771 _nmethod_end_offset = _nul_chk_table_offset;
kamg@551 772 _compile_id = 0; // default
kamg@551 773 _comp_level = CompLevel_none;
twisti@2103 774 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry);
twisti@2103 775 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry);
kamg@551 776 _osr_entry_point = NULL;
kamg@551 777 _exception_cache = NULL;
kamg@551 778 _pc_desc_cache.reset_to(NULL);
anoll@5792 779 _hotness_counter = NMethodSweeper::hotness_counter_reset_val();
kamg@551 780
coleenp@4037 781 code_buffer->copy_values_to(this);
pliden@6905 782 if (ScavengeRootsInCode) {
pliden@6905 783 if (detect_scavenge_root_oops()) {
pliden@6905 784 CodeCache::add_scavenge_root_nmethod(this);
pliden@6905 785 }
anoll@6658 786 Universe::heap()->register_nmethod(this);
anoll@6658 787 }
anoll@6658 788 DEBUG_ONLY(verify_scavenge_root_oops();)
kamg@551 789 CodeCache::commit(this);
kamg@551 790 }
kamg@551 791
kamg@551 792 if (PrintNMethods || PrintDebugInfo || PrintRelocations || PrintDependencies) {
kamg@551 793 ttyLocker ttyl; // keep the following output all in one block
kamg@551 794 // This output goes directly to the tty, not the compiler log.
kamg@551 795 // To enable tools to match it up with the compilation activity,
kamg@551 796 // be sure to tag this tty output with the compile ID.
kamg@551 797 if (xtty != NULL) {
kamg@551 798 xtty->begin_head("print_dtrace_nmethod");
kamg@551 799 xtty->method(_method);
kamg@551 800 xtty->stamp();
kamg@551 801 xtty->end_head(" address='" INTPTR_FORMAT "'", (intptr_t) this);
kamg@551 802 }
kamg@551 803 // print the header part first
kamg@551 804 print();
kamg@551 805 // then print the requested information
kamg@551 806 if (PrintNMethods) {
kamg@551 807 print_code();
kamg@551 808 }
kamg@551 809 if (PrintRelocations) {
kamg@551 810 print_relocations();
kamg@551 811 }
kamg@551 812 if (xtty != NULL) {
kamg@551 813 xtty->tail("print_dtrace_nmethod");
kamg@551 814 }
kamg@551 815 }
kamg@551 816 }
kamg@551 817 #endif // def HAVE_DTRACE_H
duke@435 818
coleenp@5614 819 void* nmethod::operator new(size_t size, int nmethod_size) throw() {
neliasso@4952 820 // Not critical, may return null if there is too little continuous memory
neliasso@4952 821 return CodeCache::allocate(nmethod_size);
duke@435 822 }
duke@435 823
duke@435 824 nmethod::nmethod(
coleenp@4037 825 Method* method,
duke@435 826 int nmethod_size,
duke@435 827 int compile_id,
duke@435 828 int entry_bci,
duke@435 829 CodeOffsets* offsets,
duke@435 830 int orig_pc_offset,
duke@435 831 DebugInformationRecorder* debug_info,
duke@435 832 Dependencies* dependencies,
duke@435 833 CodeBuffer *code_buffer,
duke@435 834 int frame_size,
duke@435 835 OopMapSet* oop_maps,
duke@435 836 ExceptionHandlerTable* handler_table,
duke@435 837 ImplicitExceptionTable* nul_chk_table,
duke@435 838 AbstractCompiler* compiler,
duke@435 839 int comp_level
duke@435 840 )
duke@435 841 : CodeBlob("nmethod", code_buffer, sizeof(nmethod),
duke@435 842 nmethod_size, offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps),
kamg@2361 843 _native_receiver_sp_offset(in_ByteSize(-1)),
kamg@2361 844 _native_basic_lock_sp_offset(in_ByteSize(-1))
duke@435 845 {
duke@435 846 assert(debug_info->oop_recorder() == code_buffer->oop_recorder(), "shared OR");
duke@435 847 {
duke@435 848 debug_only(No_Safepoint_Verifier nsv;)
duke@435 849 assert_locked_or_safepoint(CodeCache_lock);
duke@435 850
never@1999 851 init_defaults();
duke@435 852 _method = method;
never@1999 853 _entry_bci = entry_bci;
duke@435 854 _compile_id = compile_id;
duke@435 855 _comp_level = comp_level;
duke@435 856 _compiler = compiler;
duke@435 857 _orig_pc_offset = orig_pc_offset;
anoll@5792 858 _hotness_counter = NMethodSweeper::hotness_counter_reset_val();
twisti@2103 859
twisti@2103 860 // Section offsets
twisti@2117 861 _consts_offset = content_offset() + code_buffer->total_offset_of(code_buffer->consts());
twisti@2117 862 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
duke@435 863
duke@435 864 // Exception handler and deopt handler are in the stub section
twisti@2406 865 assert(offsets->value(CodeOffsets::Exceptions) != -1, "must be set");
twisti@2406 866 assert(offsets->value(CodeOffsets::Deopt ) != -1, "must be set");
twisti@2103 867 _exception_offset = _stub_offset + offsets->value(CodeOffsets::Exceptions);
twisti@2103 868 _deoptimize_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
twisti@2406 869 if (offsets->value(CodeOffsets::DeoptMH) != -1) {
twisti@2117 870 _deoptimize_mh_offset = _stub_offset + offsets->value(CodeOffsets::DeoptMH);
twisti@2117 871 } else {
twisti@2117 872 _deoptimize_mh_offset = -1;
twisti@2117 873 }
never@1813 874 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
twisti@2103 875 _unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
never@1813 876 } else {
twisti@2103 877 _unwind_handler_offset = -1;
never@1813 878 }
twisti@2103 879
twisti@1918 880 _oops_offset = data_offset();
coleenp@4037 881 _metadata_offset = _oops_offset + round_to(code_buffer->total_oop_size(), oopSize);
coleenp@4037 882 _scopes_data_offset = _metadata_offset + round_to(code_buffer->total_metadata_size(), wordSize);
coleenp@4037 883
twisti@1918 884 _scopes_pcs_offset = _scopes_data_offset + round_to(debug_info->data_size (), oopSize);
duke@435 885 _dependencies_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
duke@435 886 _handler_table_offset = _dependencies_offset + round_to(dependencies->size_in_bytes (), oopSize);
duke@435 887 _nul_chk_table_offset = _handler_table_offset + round_to(handler_table->size_in_bytes(), oopSize);
duke@435 888 _nmethod_end_offset = _nul_chk_table_offset + round_to(nul_chk_table->size_in_bytes(), oopSize);
duke@435 889
twisti@2103 890 _entry_point = code_begin() + offsets->value(CodeOffsets::Entry);
twisti@2103 891 _verified_entry_point = code_begin() + offsets->value(CodeOffsets::Verified_Entry);
twisti@2103 892 _osr_entry_point = code_begin() + offsets->value(CodeOffsets::OSR_Entry);
duke@435 893 _exception_cache = NULL;
duke@435 894 _pc_desc_cache.reset_to(scopes_pcs_begin());
duke@435 895
duke@435 896 // Copy contents of ScopeDescRecorder to nmethod
coleenp@4037 897 code_buffer->copy_values_to(this);
duke@435 898 debug_info->copy_to(this);
duke@435 899 dependencies->copy_to(this);
pliden@6905 900 if (ScavengeRootsInCode) {
pliden@6905 901 if (detect_scavenge_root_oops()) {
pliden@6905 902 CodeCache::add_scavenge_root_nmethod(this);
pliden@6905 903 }
johnc@5548 904 Universe::heap()->register_nmethod(this);
jrose@1424 905 }
jrose@1424 906 debug_only(verify_scavenge_root_oops());
duke@435 907
duke@435 908 CodeCache::commit(this);
duke@435 909
duke@435 910 // Copy contents of ExceptionHandlerTable to nmethod
duke@435 911 handler_table->copy_to(this);
duke@435 912 nul_chk_table->copy_to(this);
duke@435 913
duke@435 914 // we use the information of entry points to find out if a method is
duke@435 915 // static or non static
duke@435 916 assert(compiler->is_c2() ||
duke@435 917 _method->is_static() == (entry_point() == _verified_entry_point),
duke@435 918 " entry points must be same for static methods and vice versa");
duke@435 919 }
duke@435 920
jrose@535 921 bool printnmethods = PrintNMethods
jrose@535 922 || CompilerOracle::should_print(_method)
jrose@535 923 || CompilerOracle::has_option_string(_method, "PrintNMethods");
duke@435 924 if (printnmethods || PrintDebugInfo || PrintRelocations || PrintDependencies || PrintExceptionHandlers) {
duke@435 925 print_nmethod(printnmethods);
duke@435 926 }
duke@435 927 }
duke@435 928
duke@435 929
duke@435 930 // Print a short set of xml attributes to identify this nmethod. The
duke@435 931 // output should be embedded in some other element.
duke@435 932 void nmethod::log_identity(xmlStream* log) const {
duke@435 933 log->print(" compile_id='%d'", compile_id());
duke@435 934 const char* nm_kind = compile_kind();
duke@435 935 if (nm_kind != NULL) log->print(" compile_kind='%s'", nm_kind);
duke@435 936 if (compiler() != NULL) {
duke@435 937 log->print(" compiler='%s'", compiler()->name());
duke@435 938 }
iveresov@2138 939 if (TieredCompilation) {
iveresov@2138 940 log->print(" level='%d'", comp_level());
iveresov@2138 941 }
duke@435 942 }
duke@435 943
duke@435 944
duke@435 945 #define LOG_OFFSET(log, name) \
duke@435 946 if ((intptr_t)name##_end() - (intptr_t)name##_begin()) \
duke@435 947 log->print(" " XSTR(name) "_offset='%d'" , \
duke@435 948 (intptr_t)name##_begin() - (intptr_t)this)
duke@435 949
duke@435 950
duke@435 951 void nmethod::log_new_nmethod() const {
duke@435 952 if (LogCompilation && xtty != NULL) {
duke@435 953 ttyLocker ttyl;
duke@435 954 HandleMark hm;
duke@435 955 xtty->begin_elem("nmethod");
duke@435 956 log_identity(xtty);
twisti@2103 957 xtty->print(" entry='" INTPTR_FORMAT "' size='%d'", code_begin(), size());
duke@435 958 xtty->print(" address='" INTPTR_FORMAT "'", (intptr_t) this);
duke@435 959
duke@435 960 LOG_OFFSET(xtty, relocation);
twisti@2117 961 LOG_OFFSET(xtty, consts);
twisti@2103 962 LOG_OFFSET(xtty, insts);
duke@435 963 LOG_OFFSET(xtty, stub);
duke@435 964 LOG_OFFSET(xtty, scopes_data);
duke@435 965 LOG_OFFSET(xtty, scopes_pcs);
duke@435 966 LOG_OFFSET(xtty, dependencies);
duke@435 967 LOG_OFFSET(xtty, handler_table);
duke@435 968 LOG_OFFSET(xtty, nul_chk_table);
duke@435 969 LOG_OFFSET(xtty, oops);
duke@435 970
duke@435 971 xtty->method(method());
duke@435 972 xtty->stamp();
duke@435 973 xtty->end_elem();
duke@435 974 }
duke@435 975 }
duke@435 976
duke@435 977 #undef LOG_OFFSET
duke@435 978
duke@435 979
duke@435 980 // Print out more verbose output usually for a newly created nmethod.
twisti@2687 981 void nmethod::print_on(outputStream* st, const char* msg) const {
duke@435 982 if (st != NULL) {
duke@435 983 ttyLocker ttyl;
twisti@3969 984 if (WizardMode) {
twisti@3969 985 CompileTask::print_compilation(st, this, msg, /*short_form:*/ true);
twisti@3969 986 st->print_cr(" (" INTPTR_FORMAT ")", this);
twisti@3969 987 } else {
twisti@3969 988 CompileTask::print_compilation(st, this, msg, /*short_form:*/ false);
twisti@3969 989 }
duke@435 990 }
duke@435 991 }
duke@435 992
duke@435 993
duke@435 994 void nmethod::print_nmethod(bool printmethod) {
duke@435 995 ttyLocker ttyl; // keep the following output all in one block
duke@435 996 if (xtty != NULL) {
duke@435 997 xtty->begin_head("print_nmethod");
duke@435 998 xtty->stamp();
duke@435 999 xtty->end_head();
duke@435 1000 }
duke@435 1001 // print the header part first
duke@435 1002 print();
duke@435 1003 // then print the requested information
duke@435 1004 if (printmethod) {
duke@435 1005 print_code();
duke@435 1006 print_pcs();
twisti@3969 1007 if (oop_maps()) {
twisti@3969 1008 oop_maps()->print();
twisti@3969 1009 }
duke@435 1010 }
duke@435 1011 if (PrintDebugInfo) {
duke@435 1012 print_scopes();
duke@435 1013 }
duke@435 1014 if (PrintRelocations) {
duke@435 1015 print_relocations();
duke@435 1016 }
duke@435 1017 if (PrintDependencies) {
duke@435 1018 print_dependencies();
duke@435 1019 }
duke@435 1020 if (PrintExceptionHandlers) {
duke@435 1021 print_handler_table();
duke@435 1022 print_nul_chk_table();
duke@435 1023 }
duke@435 1024 if (xtty != NULL) {
duke@435 1025 xtty->tail("print_nmethod");
duke@435 1026 }
duke@435 1027 }
duke@435 1028
duke@435 1029
twisti@1918 1030 // Promote one word from an assembly-time handle to a live embedded oop.
twisti@1918 1031 inline void nmethod::initialize_immediate_oop(oop* dest, jobject handle) {
twisti@1918 1032 if (handle == NULL ||
twisti@1918 1033 // As a special case, IC oops are initialized to 1 or -1.
twisti@1918 1034 handle == (jobject) Universe::non_oop_word()) {
twisti@1918 1035 (*dest) = (oop) handle;
twisti@1918 1036 } else {
twisti@1918 1037 (*dest) = JNIHandles::resolve_non_null(handle);
twisti@1918 1038 }
twisti@1918 1039 }
twisti@1918 1040
twisti@1918 1041
coleenp@4037 1042 // Have to have the same name because it's called by a template
coleenp@4037 1043 void nmethod::copy_values(GrowableArray<jobject>* array) {
twisti@1918 1044 int length = array->length();
coleenp@4037 1045 assert((address)(oops_begin() + length) <= (address)oops_end(), "oops big enough");
twisti@1918 1046 oop* dest = oops_begin();
twisti@1918 1047 for (int index = 0 ; index < length; index++) {
twisti@1918 1048 initialize_immediate_oop(&dest[index], array->at(index));
twisti@1918 1049 }
twisti@1918 1050
twisti@1918 1051 // Now we can fix up all the oops in the code. We need to do this
twisti@1918 1052 // in the code because the assembler uses jobjects as placeholders.
twisti@1918 1053 // The code and relocations have already been initialized by the
twisti@1918 1054 // CodeBlob constructor, so it is valid even at this early point to
twisti@1918 1055 // iterate over relocations and patch the code.
twisti@1918 1056 fix_oop_relocations(NULL, NULL, /*initialize_immediates=*/ true);
twisti@1918 1057 }
twisti@1918 1058
coleenp@4037 1059 void nmethod::copy_values(GrowableArray<Metadata*>* array) {
coleenp@4037 1060 int length = array->length();
coleenp@4037 1061 assert((address)(metadata_begin() + length) <= (address)metadata_end(), "big enough");
coleenp@4037 1062 Metadata** dest = metadata_begin();
coleenp@4037 1063 for (int index = 0 ; index < length; index++) {
coleenp@4037 1064 dest[index] = array->at(index);
coleenp@4037 1065 }
coleenp@4037 1066 }
twisti@1918 1067
twisti@1918 1068 bool nmethod::is_at_poll_return(address pc) {
twisti@1918 1069 RelocIterator iter(this, pc, pc+1);
twisti@1918 1070 while (iter.next()) {
twisti@1918 1071 if (iter.type() == relocInfo::poll_return_type)
twisti@1918 1072 return true;
twisti@1918 1073 }
twisti@1918 1074 return false;
twisti@1918 1075 }
twisti@1918 1076
twisti@1918 1077
twisti@1918 1078 bool nmethod::is_at_poll_or_poll_return(address pc) {
twisti@1918 1079 RelocIterator iter(this, pc, pc+1);
twisti@1918 1080 while (iter.next()) {
twisti@1918 1081 relocInfo::relocType t = iter.type();
twisti@1918 1082 if (t == relocInfo::poll_return_type || t == relocInfo::poll_type)
twisti@1918 1083 return true;
twisti@1918 1084 }
twisti@1918 1085 return false;
twisti@1918 1086 }
twisti@1918 1087
twisti@1918 1088
twisti@1918 1089 void nmethod::fix_oop_relocations(address begin, address end, bool initialize_immediates) {
twisti@1918 1090 // re-patch all oop-bearing instructions, just in case some oops moved
twisti@1918 1091 RelocIterator iter(this, begin, end);
twisti@1918 1092 while (iter.next()) {
twisti@1918 1093 if (iter.type() == relocInfo::oop_type) {
twisti@1918 1094 oop_Relocation* reloc = iter.oop_reloc();
twisti@1918 1095 if (initialize_immediates && reloc->oop_is_immediate()) {
twisti@1918 1096 oop* dest = reloc->oop_addr();
twisti@1918 1097 initialize_immediate_oop(dest, (jobject) *dest);
twisti@1918 1098 }
twisti@1918 1099 // Refresh the oop-related bits of this instruction.
twisti@1918 1100 reloc->fix_oop_relocation();
coleenp@4037 1101 } else if (iter.type() == relocInfo::metadata_type) {
coleenp@4037 1102 metadata_Relocation* reloc = iter.metadata_reloc();
coleenp@4037 1103 reloc->fix_metadata_relocation();
twisti@1918 1104 }
twisti@1918 1105 }
twisti@1918 1106 }
twisti@1918 1107
twisti@1918 1108
never@2657 1109 void nmethod::verify_oop_relocations() {
never@2657 1110 // Ensure sure that the code matches the current oop values
never@2657 1111 RelocIterator iter(this, NULL, NULL);
never@2657 1112 while (iter.next()) {
never@2657 1113 if (iter.type() == relocInfo::oop_type) {
never@2657 1114 oop_Relocation* reloc = iter.oop_reloc();
never@2657 1115 if (!reloc->oop_is_immediate()) {
never@2657 1116 reloc->verify_oop_relocation();
never@2657 1117 }
never@2657 1118 }
never@2657 1119 }
never@2657 1120 }
never@2657 1121
never@2657 1122
duke@435 1123 ScopeDesc* nmethod::scope_desc_at(address pc) {
duke@435 1124 PcDesc* pd = pc_desc_at(pc);
duke@435 1125 guarantee(pd != NULL, "scope must be present");
duke@435 1126 return new ScopeDesc(this, pd->scope_decode_offset(),
kvn@1688 1127 pd->obj_decode_offset(), pd->should_reexecute(),
kvn@1688 1128 pd->return_oop());
duke@435 1129 }
duke@435 1130
duke@435 1131
duke@435 1132 void nmethod::clear_inline_caches() {
duke@435 1133 assert(SafepointSynchronize::is_at_safepoint(), "cleaning of IC's only allowed at safepoint");
duke@435 1134 if (is_zombie()) {
duke@435 1135 return;
duke@435 1136 }
duke@435 1137
duke@435 1138 RelocIterator iter(this);
duke@435 1139 while (iter.next()) {
duke@435 1140 iter.reloc()->clear_inline_cache();
duke@435 1141 }
duke@435 1142 }
duke@435 1143
duke@435 1144
duke@435 1145 void nmethod::cleanup_inline_caches() {
duke@435 1146
never@1893 1147 assert_locked_or_safepoint(CompiledIC_lock);
duke@435 1148
duke@435 1149 // If the method is not entrant or zombie then a JMP is plastered over the
duke@435 1150 // first few bytes. If an oop in the old code was there, that oop
duke@435 1151 // should not get GC'd. Skip the first few bytes of oops on
duke@435 1152 // not-entrant methods.
duke@435 1153 address low_boundary = verified_entry_point();
duke@435 1154 if (!is_in_use()) {
duke@435 1155 low_boundary += NativeJump::instruction_size;
duke@435 1156 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump.
duke@435 1157 // This means that the low_boundary is going to be a little too high.
duke@435 1158 // This shouldn't matter, since oops of non-entrant methods are never used.
duke@435 1159 // In fact, why are we bothering to look at oops in a non-entrant method??
duke@435 1160 }
duke@435 1161
duke@435 1162 // Find all calls in an nmethod, and clear the ones that points to zombie methods
duke@435 1163 ResourceMark rm;
duke@435 1164 RelocIterator iter(this, low_boundary);
duke@435 1165 while(iter.next()) {
duke@435 1166 switch(iter.type()) {
duke@435 1167 case relocInfo::virtual_call_type:
duke@435 1168 case relocInfo::opt_virtual_call_type: {
duke@435 1169 CompiledIC *ic = CompiledIC_at(iter.reloc());
duke@435 1170 // Ok, to lookup references to zombies here
duke@435 1171 CodeBlob *cb = CodeCache::find_blob_unsafe(ic->ic_destination());
duke@435 1172 if( cb != NULL && cb->is_nmethod() ) {
duke@435 1173 nmethod* nm = (nmethod*)cb;
duke@435 1174 // Clean inline caches pointing to both zombie and not_entrant methods
kvn@1637 1175 if (!nm->is_in_use() || (nm->method()->code() != nm)) ic->set_to_clean();
duke@435 1176 }
duke@435 1177 break;
duke@435 1178 }
duke@435 1179 case relocInfo::static_call_type: {
duke@435 1180 CompiledStaticCall *csc = compiledStaticCall_at(iter.reloc());
duke@435 1181 CodeBlob *cb = CodeCache::find_blob_unsafe(csc->destination());
duke@435 1182 if( cb != NULL && cb->is_nmethod() ) {
duke@435 1183 nmethod* nm = (nmethod*)cb;
duke@435 1184 // Clean inline caches pointing to both zombie and not_entrant methods
kvn@1637 1185 if (!nm->is_in_use() || (nm->method()->code() != nm)) csc->set_to_clean();
duke@435 1186 }
duke@435 1187 break;
duke@435 1188 }
duke@435 1189 }
duke@435 1190 }
duke@435 1191 }
duke@435 1192
jrose@1424 1193 // This is a private interface with the sweeper.
duke@435 1194 void nmethod::mark_as_seen_on_stack() {
anoll@5792 1195 assert(is_alive(), "Must be an alive method");
never@1999 1196 // Set the traversal mark to ensure that the sweeper does 2
never@1999 1197 // cleaning passes before moving to zombie.
duke@435 1198 set_stack_traversal_mark(NMethodSweeper::traversal_count());
duke@435 1199 }
duke@435 1200
dcubed@2624 1201 // Tell if a non-entrant method can be converted to a zombie (i.e.,
dcubed@2624 1202 // there are no activations on the stack, not in use by the VM,
dcubed@2624 1203 // and not in use by the ServiceThread)
duke@435 1204 bool nmethod::can_not_entrant_be_converted() {
duke@435 1205 assert(is_not_entrant(), "must be a non-entrant method");
duke@435 1206
duke@435 1207 // Since the nmethod sweeper only does partial sweep the sweeper's traversal
duke@435 1208 // count can be greater than the stack traversal count before it hits the
duke@435 1209 // nmethod for the second time.
dcubed@2624 1210 return stack_traversal_mark()+1 < NMethodSweeper::traversal_count() &&
dcubed@2624 1211 !is_locked_by_vm();
duke@435 1212 }
duke@435 1213
duke@435 1214 void nmethod::inc_decompile_count() {
iveresov@2138 1215 if (!is_compiled_by_c2()) return;
duke@435 1216 // Could be gated by ProfileTraps, but do not bother...
coleenp@4037 1217 Method* m = method();
duke@435 1218 if (m == NULL) return;
coleenp@4037 1219 MethodData* mdo = m->method_data();
duke@435 1220 if (mdo == NULL) return;
coleenp@4037 1221 // There is a benign race here. See comments in methodData.hpp.
duke@435 1222 mdo->inc_decompile_count();
duke@435 1223 }
duke@435 1224
duke@435 1225 void nmethod::make_unloaded(BoolObjectClosure* is_alive, oop cause) {
duke@435 1226
duke@435 1227 post_compiled_method_unload();
duke@435 1228
duke@435 1229 // Since this nmethod is being unloaded, make sure that dependencies
duke@435 1230 // recorded in instanceKlasses get flushed and pass non-NULL closure to
duke@435 1231 // indicate that this work is being done during a GC.
duke@435 1232 assert(Universe::heap()->is_gc_active(), "should only be called during gc");
duke@435 1233 assert(is_alive != NULL, "Should be non-NULL");
duke@435 1234 // A non-NULL is_alive closure indicates that this is being called during GC.
duke@435 1235 flush_dependencies(is_alive);
duke@435 1236
duke@435 1237 // Break cycle between nmethod & method
duke@435 1238 if (TraceClassUnloading && WizardMode) {
duke@435 1239 tty->print_cr("[Class unloading: Making nmethod " INTPTR_FORMAT
coleenp@4037 1240 " unloadable], Method*(" INTPTR_FORMAT
duke@435 1241 "), cause(" INTPTR_FORMAT ")",
duke@435 1242 this, (address)_method, (address)cause);
jrose@1424 1243 if (!Universe::heap()->is_gc_active())
jrose@1424 1244 cause->klass()->print();
duke@435 1245 }
ysr@1376 1246 // Unlink the osr method, so we do not look this up again
ysr@1376 1247 if (is_osr_method()) {
ysr@1376 1248 invalidate_osr_method();
ysr@1376 1249 }
coleenp@4037 1250 // If _method is already NULL the Method* is about to be unloaded,
duke@435 1251 // so we don't have to break the cycle. Note that it is possible to
coleenp@4037 1252 // have the Method* live here, in case we unload the nmethod because
coleenp@4037 1253 // it is pointing to some oop (other than the Method*) being unloaded.
duke@435 1254 if (_method != NULL) {
coleenp@4037 1255 // OSR methods point to the Method*, but the Method* does not
duke@435 1256 // point back!
duke@435 1257 if (_method->code() == this) {
duke@435 1258 _method->clear_code(); // Break a cycle
duke@435 1259 }
duke@435 1260 _method = NULL; // Clear the method of this dead nmethod
duke@435 1261 }
duke@435 1262 // Make the class unloaded - i.e., change state and notify sweeper
never@1893 1263 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
duke@435 1264 if (is_in_use()) {
duke@435 1265 // Transitioning directly from live to unloaded -- so
duke@435 1266 // we need to force a cache clean-up; remember this
duke@435 1267 // for later on.
duke@435 1268 CodeCache::set_needs_cache_clean(true);
duke@435 1269 }
never@1999 1270 _state = unloaded;
duke@435 1271
never@1544 1272 // Log the unloading.
never@1544 1273 log_state_change();
never@1544 1274
coleenp@4037 1275 // The Method* is gone at this point
duke@435 1276 assert(_method == NULL, "Tautology");
duke@435 1277
jrose@1424 1278 set_osr_link(NULL);
jrose@1424 1279 //set_scavenge_root_link(NULL); // done by prune_scavenge_root_nmethods
anoll@6099 1280 NMethodSweeper::report_state_change(this);
duke@435 1281 }
duke@435 1282
duke@435 1283 void nmethod::invalidate_osr_method() {
duke@435 1284 assert(_entry_bci != InvocationEntryBci, "wrong kind of nmethod");
duke@435 1285 // Remove from list of active nmethods
duke@435 1286 if (method() != NULL)
coleenp@4251 1287 method()->method_holder()->remove_osr_nmethod(this);
duke@435 1288 // Set entry as invalid
duke@435 1289 _entry_bci = InvalidOSREntryBci;
duke@435 1290 }
duke@435 1291
never@1544 1292 void nmethod::log_state_change() const {
duke@435 1293 if (LogCompilation) {
duke@435 1294 if (xtty != NULL) {
duke@435 1295 ttyLocker ttyl; // keep the following output all in one block
never@1999 1296 if (_state == unloaded) {
never@1544 1297 xtty->begin_elem("make_unloaded thread='" UINTX_FORMAT "'",
never@1544 1298 os::current_thread_id());
never@1544 1299 } else {
never@1544 1300 xtty->begin_elem("make_not_entrant thread='" UINTX_FORMAT "'%s",
never@1544 1301 os::current_thread_id(),
never@1999 1302 (_state == zombie ? " zombie='1'" : ""));
never@1544 1303 }
duke@435 1304 log_identity(xtty);
duke@435 1305 xtty->stamp();
duke@435 1306 xtty->end_elem();
duke@435 1307 }
duke@435 1308 }
never@1999 1309 if (PrintCompilation && _state != unloaded) {
twisti@2687 1310 print_on(tty, _state == zombie ? "made zombie" : "made not entrant");
duke@435 1311 }
duke@435 1312 }
duke@435 1313
anoll@6099 1314 /**
anoll@6099 1315 * Common functionality for both make_not_entrant and make_zombie
anoll@6099 1316 */
never@1576 1317 bool nmethod::make_not_entrant_or_zombie(unsigned int state) {
duke@435 1318 assert(state == zombie || state == not_entrant, "must be zombie or not_entrant");
dcubed@2624 1319 assert(!is_zombie(), "should not already be a zombie");
duke@435 1320
never@1999 1321 // Make sure neither the nmethod nor the method is flushed in case of a safepoint in code below.
duke@435 1322 nmethodLocker nml(this);
never@1999 1323 methodHandle the_method(method());
never@2081 1324 No_Safepoint_Verifier nsv;
duke@435 1325
johnc@5548 1326 // during patching, depending on the nmethod state we must notify the GC that
johnc@5548 1327 // code has been unloaded, unregistering it. We cannot do this right while
johnc@5548 1328 // holding the Patching_lock because we need to use the CodeCache_lock. This
johnc@5548 1329 // would be prone to deadlocks.
johnc@5548 1330 // This flag is used to remember whether we need to later lock and unregister.
johnc@5548 1331 bool nmethod_needs_unregister = false;
johnc@5548 1332
duke@435 1333 {
never@1544 1334 // invalidate osr nmethod before acquiring the patching lock since
never@1544 1335 // they both acquire leaf locks and we don't want a deadlock.
never@1544 1336 // This logic is equivalent to the logic below for patching the
never@1544 1337 // verified entry point of regular methods.
never@1544 1338 if (is_osr_method()) {
never@1544 1339 // this effectively makes the osr nmethod not entrant
never@1544 1340 invalidate_osr_method();
never@1544 1341 }
never@1544 1342
duke@435 1343 // Enter critical section. Does not block for safepoint.
duke@435 1344 MutexLockerEx pl(Patching_lock, Mutex::_no_safepoint_check_flag);
never@1544 1345
never@1999 1346 if (_state == state) {
never@1544 1347 // another thread already performed this transition so nothing
never@1544 1348 // to do, but return false to indicate this.
never@1544 1349 return false;
never@1544 1350 }
never@1544 1351
duke@435 1352 // The caller can be calling the method statically or through an inline
duke@435 1353 // cache call.
never@1544 1354 if (!is_osr_method() && !is_not_entrant()) {
duke@435 1355 NativeJump::patch_verified_entry(entry_point(), verified_entry_point(),
duke@435 1356 SharedRuntime::get_handle_wrong_method_stub());
duke@435 1357 }
duke@435 1358
never@1999 1359 if (is_in_use()) {
never@1999 1360 // It's a true state change, so mark the method as decompiled.
never@1999 1361 // Do it only for transition from alive.
never@1999 1362 inc_decompile_count();
never@1999 1363 }
kvn@1641 1364
johnc@5548 1365 // If the state is becoming a zombie, signal to unregister the nmethod with
johnc@5548 1366 // the heap.
johnc@5548 1367 // This nmethod may have already been unloaded during a full GC.
johnc@5548 1368 if ((state == zombie) && !is_unloaded()) {
johnc@5548 1369 nmethod_needs_unregister = true;
johnc@5548 1370 }
johnc@5548 1371
anoll@5792 1372 // Must happen before state change. Otherwise we have a race condition in
anoll@5792 1373 // nmethod::can_not_entrant_be_converted(). I.e., a method can immediately
anoll@5792 1374 // transition its state from 'not_entrant' to 'zombie' without having to wait
anoll@5792 1375 // for stack scanning.
anoll@5792 1376 if (state == not_entrant) {
anoll@5792 1377 mark_as_seen_on_stack();
anoll@5792 1378 OrderAccess::storestore();
anoll@5792 1379 }
anoll@5792 1380
duke@435 1381 // Change state
never@1999 1382 _state = state;
never@1544 1383
never@1544 1384 // Log the transition once
never@1544 1385 log_state_change();
never@1544 1386
never@1999 1387 // Remove nmethod from method.
never@1999 1388 // We need to check if both the _code and _from_compiled_code_entry_point
never@1999 1389 // refer to this nmethod because there is a race in setting these two fields
coleenp@4037 1390 // in Method* as seen in bugid 4947125.
never@1999 1391 // If the vep() points to the zombie nmethod, the memory for the nmethod
never@1999 1392 // could be flushed and the compiler and vtable stubs could still call
never@1999 1393 // through it.
never@1999 1394 if (method() != NULL && (method()->code() == this ||
never@1999 1395 method()->from_compiled_entry() == verified_entry_point())) {
never@1999 1396 HandleMark hm;
never@1999 1397 method()->clear_code();
never@1999 1398 }
duke@435 1399 } // leave critical region under Patching_lock
duke@435 1400
never@1893 1401 // When the nmethod becomes zombie it is no longer alive so the
never@1893 1402 // dependencies must be flushed. nmethods in the not_entrant
never@1893 1403 // state will be flushed later when the transition to zombie
never@1893 1404 // happens or they get unloaded.
never@1893 1405 if (state == zombie) {
never@2081 1406 {
never@2081 1407 // Flushing dependecies must be done before any possible
never@2081 1408 // safepoint can sneak in, otherwise the oops used by the
never@2081 1409 // dependency logic could have become stale.
never@2081 1410 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
johnc@5548 1411 if (nmethod_needs_unregister) {
johnc@5548 1412 Universe::heap()->unregister_nmethod(this);
johnc@5548 1413 }
never@2081 1414 flush_dependencies(NULL);
never@2081 1415 }
never@1999 1416
dcubed@2624 1417 // zombie only - if a JVMTI agent has enabled the CompiledMethodUnload
dcubed@2624 1418 // event and it hasn't already been reported for this nmethod then
dcubed@2624 1419 // report it now. The event may have been reported earilier if the GC
dcubed@2624 1420 // marked it for unloading). JvmtiDeferredEventQueue support means
dcubed@2624 1421 // we no longer go to a safepoint here.
dcubed@2624 1422 post_compiled_method_unload();
never@2081 1423
never@2081 1424 #ifdef ASSERT
never@2081 1425 // It's no longer safe to access the oops section since zombie
never@2081 1426 // nmethods aren't scanned for GC.
never@2081 1427 _oops_are_stale = true;
never@2081 1428 #endif
roland@5629 1429 // the Method may be reclaimed by class unloading now that the
roland@5629 1430 // nmethod is in zombie state
roland@5629 1431 set_method(NULL);
never@1893 1432 } else {
never@1893 1433 assert(state == not_entrant, "other cases may need to be handled differently");
never@1893 1434 }
never@1893 1435
duke@435 1436 if (TraceCreateZombies) {
duke@435 1437 tty->print_cr("nmethod <" INTPTR_FORMAT "> code made %s", this, (state == not_entrant) ? "not entrant" : "zombie");
duke@435 1438 }
duke@435 1439
anoll@6099 1440 NMethodSweeper::report_state_change(this);
never@1544 1441 return true;
duke@435 1442 }
duke@435 1443
duke@435 1444 void nmethod::flush() {
duke@435 1445 // Note that there are no valid oops in the nmethod anymore.
duke@435 1446 assert(is_zombie() || (is_osr_method() && is_unloaded()), "must be a zombie method");
duke@435 1447 assert(is_marked_for_reclamation() || (is_osr_method() && is_unloaded()), "must be marked for reclamation");
duke@435 1448
duke@435 1449 assert (!is_locked_by_vm(), "locked methods shouldn't be flushed");
never@1893 1450 assert_locked_or_safepoint(CodeCache_lock);
duke@435 1451
duke@435 1452 // completely deallocate this method
never@3499 1453 Events::log(JavaThread::current(), "flushing nmethod " INTPTR_FORMAT, this);
duke@435 1454 if (PrintMethodFlushing) {
kvn@1637 1455 tty->print_cr("*flushing nmethod %3d/" INTPTR_FORMAT ". Live blobs:" UINT32_FORMAT "/Free CodeCache:" SIZE_FORMAT "Kb",
kvn@1637 1456 _compile_id, this, CodeCache::nof_blobs(), CodeCache::unallocated_capacity()/1024);
duke@435 1457 }
duke@435 1458
duke@435 1459 // We need to deallocate any ExceptionCache data.
duke@435 1460 // Note that we do not need to grab the nmethod lock for this, it
duke@435 1461 // better be thread safe if we're disposing of it!
duke@435 1462 ExceptionCache* ec = exception_cache();
duke@435 1463 set_exception_cache(NULL);
duke@435 1464 while(ec != NULL) {
duke@435 1465 ExceptionCache* next = ec->next();
duke@435 1466 delete ec;
duke@435 1467 ec = next;
duke@435 1468 }
duke@435 1469
jrose@1424 1470 if (on_scavenge_root_list()) {
jrose@1424 1471 CodeCache::drop_scavenge_root_nmethod(this);
jrose@1424 1472 }
jrose@1424 1473
twisti@2047 1474 #ifdef SHARK
twisti@2200 1475 ((SharkCompiler *) compiler())->free_compiled_method(insts_begin());
twisti@2047 1476 #endif // SHARK
twisti@2047 1477
duke@435 1478 ((CodeBlob*)(this))->flush();
duke@435 1479
duke@435 1480 CodeCache::free(this);
duke@435 1481 }
duke@435 1482
duke@435 1483
duke@435 1484 //
duke@435 1485 // Notify all classes this nmethod is dependent on that it is no
duke@435 1486 // longer dependent. This should only be called in two situations.
duke@435 1487 // First, when a nmethod transitions to a zombie all dependents need
duke@435 1488 // to be clear. Since zombification happens at a safepoint there's no
duke@435 1489 // synchronization issues. The second place is a little more tricky.
duke@435 1490 // During phase 1 of mark sweep class unloading may happen and as a
duke@435 1491 // result some nmethods may get unloaded. In this case the flushing
duke@435 1492 // of dependencies must happen during phase 1 since after GC any
duke@435 1493 // dependencies in the unloaded nmethod won't be updated, so
duke@435 1494 // traversing the dependency information in unsafe. In that case this
duke@435 1495 // function is called with a non-NULL argument and this function only
duke@435 1496 // notifies instanceKlasses that are reachable
duke@435 1497
duke@435 1498 void nmethod::flush_dependencies(BoolObjectClosure* is_alive) {
never@1893 1499 assert_locked_or_safepoint(CodeCache_lock);
duke@435 1500 assert(Universe::heap()->is_gc_active() == (is_alive != NULL),
duke@435 1501 "is_alive is non-NULL if and only if we are called during GC");
duke@435 1502 if (!has_flushed_dependencies()) {
duke@435 1503 set_has_flushed_dependencies();
duke@435 1504 for (Dependencies::DepStream deps(this); deps.next(); ) {
coleenp@4037 1505 Klass* klass = deps.context_type();
duke@435 1506 if (klass == NULL) continue; // ignore things like evol_method
duke@435 1507
duke@435 1508 // During GC the is_alive closure is non-NULL, and is used to
duke@435 1509 // determine liveness of dependees that need to be updated.
coleenp@4037 1510 if (is_alive == NULL || klass->is_loader_alive(is_alive)) {
coleenp@4037 1511 InstanceKlass::cast(klass)->remove_dependent_nmethod(this);
duke@435 1512 }
duke@435 1513 }
duke@435 1514 }
duke@435 1515 }
duke@435 1516
duke@435 1517
duke@435 1518 // If this oop is not live, the nmethod can be unloaded.
brutisso@4098 1519 bool nmethod::can_unload(BoolObjectClosure* is_alive, oop* root, bool unloading_occurred) {
duke@435 1520 assert(root != NULL, "just checking");
duke@435 1521 oop obj = *root;
duke@435 1522 if (obj == NULL || is_alive->do_object_b(obj)) {
duke@435 1523 return false;
duke@435 1524 }
coleenp@4037 1525
jrose@1424 1526 // If ScavengeRootsInCode is true, an nmethod might be unloaded
jrose@1424 1527 // simply because one of its constant oops has gone dead.
jrose@1424 1528 // No actual classes need to be unloaded in order for this to occur.
jrose@1424 1529 assert(unloading_occurred || ScavengeRootsInCode, "Inconsistency in unloading");
duke@435 1530 make_unloaded(is_alive, obj);
duke@435 1531 return true;
duke@435 1532 }
duke@435 1533
duke@435 1534 // ------------------------------------------------------------------
duke@435 1535 // post_compiled_method_load_event
duke@435 1536 // new method for install_code() path
duke@435 1537 // Transfer information from compilation to jvmti
duke@435 1538 void nmethod::post_compiled_method_load_event() {
duke@435 1539
coleenp@4037 1540 Method* moop = method();
dcubed@3202 1541 #ifndef USDT2
duke@435 1542 HS_DTRACE_PROBE8(hotspot, compiled__method__load,
duke@435 1543 moop->klass_name()->bytes(),
duke@435 1544 moop->klass_name()->utf8_length(),
duke@435 1545 moop->name()->bytes(),
duke@435 1546 moop->name()->utf8_length(),
duke@435 1547 moop->signature()->bytes(),
duke@435 1548 moop->signature()->utf8_length(),
twisti@2103 1549 insts_begin(), insts_size());
dcubed@3202 1550 #else /* USDT2 */
dcubed@3202 1551 HOTSPOT_COMPILED_METHOD_LOAD(
dcubed@3202 1552 (char *) moop->klass_name()->bytes(),
dcubed@3202 1553 moop->klass_name()->utf8_length(),
dcubed@3202 1554 (char *) moop->name()->bytes(),
dcubed@3202 1555 moop->name()->utf8_length(),
dcubed@3202 1556 (char *) moop->signature()->bytes(),
dcubed@3202 1557 moop->signature()->utf8_length(),
dcubed@3202 1558 insts_begin(), insts_size());
dcubed@3202 1559 #endif /* USDT2 */
duke@435 1560
never@1971 1561 if (JvmtiExport::should_post_compiled_method_load() ||
never@1971 1562 JvmtiExport::should_post_compiled_method_unload()) {
never@1971 1563 get_and_cache_jmethod_id();
never@1971 1564 }
never@1971 1565
duke@435 1566 if (JvmtiExport::should_post_compiled_method_load()) {
kamg@2511 1567 // Let the Service thread (which is a real Java thread) post the event
kamg@2511 1568 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
kamg@2511 1569 JvmtiDeferredEventQueue::enqueue(
kamg@2511 1570 JvmtiDeferredEvent::compiled_method_load_event(this));
duke@435 1571 }
duke@435 1572 }
duke@435 1573
never@1971 1574 jmethodID nmethod::get_and_cache_jmethod_id() {
never@1971 1575 if (_jmethod_id == NULL) {
never@1971 1576 // Cache the jmethod_id since it can no longer be looked up once the
never@1971 1577 // method itself has been marked for unloading.
never@1971 1578 _jmethod_id = method()->jmethod_id();
never@1971 1579 }
never@1971 1580 return _jmethod_id;
never@1971 1581 }
never@1971 1582
duke@435 1583 void nmethod::post_compiled_method_unload() {
never@1932 1584 if (unload_reported()) {
never@1932 1585 // During unloading we transition to unloaded and then to zombie
never@1932 1586 // and the unloading is reported during the first transition.
never@1932 1587 return;
never@1932 1588 }
never@1932 1589
duke@435 1590 assert(_method != NULL && !is_unloaded(), "just checking");
duke@435 1591 DTRACE_METHOD_UNLOAD_PROBE(method());
duke@435 1592
duke@435 1593 // If a JVMTI agent has enabled the CompiledMethodUnload event then
never@1971 1594 // post the event. Sometime later this nmethod will be made a zombie
coleenp@4037 1595 // by the sweeper but the Method* will not be valid at that point.
never@1971 1596 // If the _jmethod_id is null then no load event was ever requested
never@1971 1597 // so don't bother posting the unload. The main reason for this is
coleenp@4037 1598 // that the jmethodID is a weak reference to the Method* so if
never@1971 1599 // it's being unloaded there's no way to look it up since the weak
never@1971 1600 // ref will have been cleared.
never@1971 1601 if (_jmethod_id != NULL && JvmtiExport::should_post_compiled_method_unload()) {
duke@435 1602 assert(!unload_reported(), "already unloaded");
kamg@2511 1603 JvmtiDeferredEvent event =
dcubed@2624 1604 JvmtiDeferredEvent::compiled_method_unload_event(this,
kamg@2511 1605 _jmethod_id, insts_begin());
kamg@2511 1606 if (SafepointSynchronize::is_at_safepoint()) {
kamg@2511 1607 // Don't want to take the queueing lock. Add it as pending and
kamg@2511 1608 // it will get enqueued later.
kamg@2511 1609 JvmtiDeferredEventQueue::add_pending_event(event);
kamg@2511 1610 } else {
kamg@2511 1611 MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag);
kamg@2511 1612 JvmtiDeferredEventQueue::enqueue(event);
kamg@2511 1613 }
duke@435 1614 }
duke@435 1615
duke@435 1616 // The JVMTI CompiledMethodUnload event can be enabled or disabled at
duke@435 1617 // any time. As the nmethod is being unloaded now we mark it has
duke@435 1618 // having the unload event reported - this will ensure that we don't
duke@435 1619 // attempt to report the event in the unlikely scenario where the
duke@435 1620 // event is enabled at the time the nmethod is made a zombie.
duke@435 1621 set_unload_reported();
duke@435 1622 }
duke@435 1623
duke@435 1624 // This is called at the end of the strong tracing/marking phase of a
duke@435 1625 // GC to unload an nmethod if it contains otherwise unreachable
duke@435 1626 // oops.
duke@435 1627
brutisso@4098 1628 void nmethod::do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred) {
duke@435 1629 // Make sure the oop's ready to receive visitors
duke@435 1630 assert(!is_zombie() && !is_unloaded(),
duke@435 1631 "should not call follow on zombie or unloaded nmethod");
duke@435 1632
duke@435 1633 // If the method is not entrant then a JMP is plastered over the
duke@435 1634 // first few bytes. If an oop in the old code was there, that oop
duke@435 1635 // should not get GC'd. Skip the first few bytes of oops on
duke@435 1636 // not-entrant methods.
duke@435 1637 address low_boundary = verified_entry_point();
duke@435 1638 if (is_not_entrant()) {
duke@435 1639 low_boundary += NativeJump::instruction_size;
duke@435 1640 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump.
duke@435 1641 // (See comment above.)
duke@435 1642 }
duke@435 1643
duke@435 1644 // The RedefineClasses() API can cause the class unloading invariant
duke@435 1645 // to no longer be true. See jvmtiExport.hpp for details.
duke@435 1646 // Also, leave a debugging breadcrumb in local flag.
duke@435 1647 bool a_class_was_redefined = JvmtiExport::has_redefined_a_class();
duke@435 1648 if (a_class_was_redefined) {
duke@435 1649 // This set of the unloading_occurred flag is done before the
duke@435 1650 // call to post_compiled_method_unload() so that the unloading
duke@435 1651 // of this nmethod is reported.
duke@435 1652 unloading_occurred = true;
duke@435 1653 }
duke@435 1654
duke@435 1655 // Exception cache
stefank@6983 1656 clean_exception_cache(is_alive);
duke@435 1657
duke@435 1658 // If class unloading occurred we first iterate over all inline caches and
duke@435 1659 // clear ICs where the cached oop is referring to an unloaded klass or method.
duke@435 1660 // The remaining live cached oops will be traversed in the relocInfo::oop_type
duke@435 1661 // iteration below.
duke@435 1662 if (unloading_occurred) {
duke@435 1663 RelocIterator iter(this, low_boundary);
duke@435 1664 while(iter.next()) {
duke@435 1665 if (iter.type() == relocInfo::virtual_call_type) {
duke@435 1666 CompiledIC *ic = CompiledIC_at(iter.reloc());
coleenp@4037 1667 if (ic->is_icholder_call()) {
duke@435 1668 // The only exception is compiledICHolder oops which may
duke@435 1669 // yet be marked below. (We check this further below).
coleenp@4037 1670 CompiledICHolder* cichk_oop = ic->cached_icholder();
coleenp@4037 1671 if (cichk_oop->holder_method()->method_holder()->is_loader_alive(is_alive) &&
coleenp@4037 1672 cichk_oop->holder_klass()->is_loader_alive(is_alive)) {
kvn@6172 1673 continue;
kvn@6172 1674 }
coleenp@4037 1675 } else {
coleenp@4037 1676 Metadata* ic_oop = ic->cached_metadata();
coleenp@4037 1677 if (ic_oop != NULL) {
coleenp@4037 1678 if (ic_oop->is_klass()) {
coleenp@4037 1679 if (((Klass*)ic_oop)->is_loader_alive(is_alive)) {
coleenp@4037 1680 continue;
coleenp@4037 1681 }
coleenp@4037 1682 } else if (ic_oop->is_method()) {
coleenp@4037 1683 if (((Method*)ic_oop)->method_holder()->is_loader_alive(is_alive)) {
coleenp@4037 1684 continue;
coleenp@4037 1685 }
coleenp@4037 1686 } else {
coleenp@4037 1687 ShouldNotReachHere();
coleenp@4037 1688 }
coleenp@4037 1689 }
kvn@6172 1690 }
kvn@6172 1691 ic->set_to_clean();
duke@435 1692 }
duke@435 1693 }
duke@435 1694 }
duke@435 1695
duke@435 1696 // Compiled code
coleenp@4037 1697 {
duke@435 1698 RelocIterator iter(this, low_boundary);
duke@435 1699 while (iter.next()) {
duke@435 1700 if (iter.type() == relocInfo::oop_type) {
duke@435 1701 oop_Relocation* r = iter.oop_reloc();
duke@435 1702 // In this loop, we must only traverse those oops directly embedded in
duke@435 1703 // the code. Other oops (oop_index>0) are seen as part of scopes_oops.
duke@435 1704 assert(1 == (r->oop_is_immediate()) +
duke@435 1705 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()),
duke@435 1706 "oop must be found in exactly one place");
duke@435 1707 if (r->oop_is_immediate() && r->oop_value() != NULL) {
brutisso@4098 1708 if (can_unload(is_alive, r->oop_addr(), unloading_occurred)) {
duke@435 1709 return;
duke@435 1710 }
duke@435 1711 }
duke@435 1712 }
duke@435 1713 }
coleenp@4037 1714 }
duke@435 1715
duke@435 1716
duke@435 1717 // Scopes
duke@435 1718 for (oop* p = oops_begin(); p < oops_end(); p++) {
duke@435 1719 if (*p == Universe::non_oop_word()) continue; // skip non-oops
brutisso@4098 1720 if (can_unload(is_alive, p, unloading_occurred)) {
duke@435 1721 return;
duke@435 1722 }
duke@435 1723 }
duke@435 1724
coleenp@4037 1725 // Ensure that all metadata is still alive
coleenp@4037 1726 verify_metadata_loaders(low_boundary, is_alive);
coleenp@4037 1727 }
coleenp@4037 1728
coleenp@4037 1729 #ifdef ASSERT
coleenp@4037 1730
coleenp@4037 1731 class CheckClass : AllStatic {
coleenp@4037 1732 static BoolObjectClosure* _is_alive;
coleenp@4037 1733
coleenp@4037 1734 // Check class_loader is alive for this bit of metadata.
coleenp@4037 1735 static void check_class(Metadata* md) {
coleenp@4037 1736 Klass* klass = NULL;
coleenp@4037 1737 if (md->is_klass()) {
coleenp@4037 1738 klass = ((Klass*)md);
coleenp@4037 1739 } else if (md->is_method()) {
coleenp@4037 1740 klass = ((Method*)md)->method_holder();
coleenp@4037 1741 } else if (md->is_methodData()) {
coleenp@4037 1742 klass = ((MethodData*)md)->method()->method_holder();
coleenp@4037 1743 } else {
coleenp@4037 1744 md->print();
coleenp@4037 1745 ShouldNotReachHere();
coleenp@4037 1746 }
coleenp@4037 1747 assert(klass->is_loader_alive(_is_alive), "must be alive");
coleenp@4037 1748 }
coleenp@4037 1749 public:
coleenp@4037 1750 static void do_check_class(BoolObjectClosure* is_alive, nmethod* nm) {
coleenp@4037 1751 assert(SafepointSynchronize::is_at_safepoint(), "this is only ok at safepoint");
coleenp@4037 1752 _is_alive = is_alive;
coleenp@4037 1753 nm->metadata_do(check_class);
coleenp@4037 1754 }
coleenp@4037 1755 };
coleenp@4037 1756
coleenp@4037 1757 // This is called during a safepoint so can use static data
coleenp@4037 1758 BoolObjectClosure* CheckClass::_is_alive = NULL;
coleenp@4037 1759 #endif // ASSERT
coleenp@4037 1760
coleenp@4037 1761
coleenp@4037 1762 // Processing of oop references should have been sufficient to keep
coleenp@4037 1763 // all strong references alive. Any weak references should have been
coleenp@4037 1764 // cleared as well. Visit all the metadata and ensure that it's
coleenp@4037 1765 // really alive.
coleenp@4037 1766 void nmethod::verify_metadata_loaders(address low_boundary, BoolObjectClosure* is_alive) {
coleenp@4037 1767 #ifdef ASSERT
duke@435 1768 RelocIterator iter(this, low_boundary);
duke@435 1769 while (iter.next()) {
coleenp@4037 1770 // static_stub_Relocations may have dangling references to
coleenp@4037 1771 // Method*s so trim them out here. Otherwise it looks like
coleenp@4037 1772 // compiled code is maintaining a link to dead metadata.
coleenp@4037 1773 address static_call_addr = NULL;
coleenp@4037 1774 if (iter.type() == relocInfo::opt_virtual_call_type) {
coleenp@4037 1775 CompiledIC* cic = CompiledIC_at(iter.reloc());
coleenp@4037 1776 if (!cic->is_call_to_interpreted()) {
coleenp@4037 1777 static_call_addr = iter.addr();
coleenp@4037 1778 }
coleenp@4037 1779 } else if (iter.type() == relocInfo::static_call_type) {
coleenp@4037 1780 CompiledStaticCall* csc = compiledStaticCall_at(iter.reloc());
coleenp@4037 1781 if (!csc->is_call_to_interpreted()) {
coleenp@4037 1782 static_call_addr = iter.addr();
coleenp@4037 1783 }
coleenp@4037 1784 }
coleenp@4037 1785 if (static_call_addr != NULL) {
coleenp@4037 1786 RelocIterator sciter(this, low_boundary);
coleenp@4037 1787 while (sciter.next()) {
coleenp@4037 1788 if (sciter.type() == relocInfo::static_stub_type &&
coleenp@4037 1789 sciter.static_stub_reloc()->static_call() == static_call_addr) {
coleenp@4037 1790 sciter.static_stub_reloc()->clear_inline_cache();
coleenp@4037 1791 }
coleenp@4037 1792 }
duke@435 1793 }
duke@435 1794 }
coleenp@4037 1795 // Check that the metadata embedded in the nmethod is alive
coleenp@4037 1796 CheckClass::do_check_class(is_alive, this);
coleenp@4037 1797 #endif
duke@435 1798 }
duke@435 1799
coleenp@4037 1800
coleenp@4037 1801 // Iterate over metadata calling this function. Used by RedefineClasses
coleenp@4037 1802 void nmethod::metadata_do(void f(Metadata*)) {
coleenp@4037 1803 address low_boundary = verified_entry_point();
coleenp@4037 1804 if (is_not_entrant()) {
coleenp@4037 1805 low_boundary += NativeJump::instruction_size;
coleenp@4037 1806 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump.
coleenp@4037 1807 // (See comment above.)
coleenp@4037 1808 }
coleenp@4037 1809 {
coleenp@4037 1810 // Visit all immediate references that are embedded in the instruction stream.
coleenp@4037 1811 RelocIterator iter(this, low_boundary);
coleenp@4037 1812 while (iter.next()) {
coleenp@4037 1813 if (iter.type() == relocInfo::metadata_type ) {
coleenp@4037 1814 metadata_Relocation* r = iter.metadata_reloc();
coleenp@4037 1815 // In this lmetadata, we must only follow those metadatas directly embedded in
coleenp@4037 1816 // the code. Other metadatas (oop_index>0) are seen as part of
coleenp@4037 1817 // the metadata section below.
coleenp@4037 1818 assert(1 == (r->metadata_is_immediate()) +
coleenp@4037 1819 (r->metadata_addr() >= metadata_begin() && r->metadata_addr() < metadata_end()),
coleenp@4037 1820 "metadata must be found in exactly one place");
coleenp@4037 1821 if (r->metadata_is_immediate() && r->metadata_value() != NULL) {
coleenp@4037 1822 Metadata* md = r->metadata_value();
coleenp@4037 1823 f(md);
coleenp@4037 1824 }
coleenp@5085 1825 } else if (iter.type() == relocInfo::virtual_call_type) {
coleenp@5085 1826 // Check compiledIC holders associated with this nmethod
coleenp@5085 1827 CompiledIC *ic = CompiledIC_at(iter.reloc());
coleenp@5085 1828 if (ic->is_icholder_call()) {
coleenp@5085 1829 CompiledICHolder* cichk = ic->cached_icholder();
coleenp@5085 1830 f(cichk->holder_method());
coleenp@5085 1831 f(cichk->holder_klass());
coleenp@5085 1832 } else {
coleenp@5085 1833 Metadata* ic_oop = ic->cached_metadata();
coleenp@5085 1834 if (ic_oop != NULL) {
coleenp@5085 1835 f(ic_oop);
coleenp@5085 1836 }
coleenp@5085 1837 }
coleenp@4037 1838 }
coleenp@4037 1839 }
coleenp@4037 1840 }
coleenp@4037 1841
coleenp@4037 1842 // Visit the metadata section
coleenp@4037 1843 for (Metadata** p = metadata_begin(); p < metadata_end(); p++) {
coleenp@4037 1844 if (*p == Universe::non_oop_word() || *p == NULL) continue; // skip non-oops
coleenp@4037 1845 Metadata* md = *p;
coleenp@4037 1846 f(md);
coleenp@4037 1847 }
coleenp@5085 1848
coleenp@4037 1849 // Call function Method*, not embedded in these other places.
coleenp@4037 1850 if (_method != NULL) f(_method);
coleenp@4037 1851 }
coleenp@4037 1852
johnc@5548 1853 void nmethod::oops_do(OopClosure* f, bool allow_zombie) {
duke@435 1854 // make sure the oops ready to receive visitors
johnc@5548 1855 assert(allow_zombie || !is_zombie(), "should not call follow on zombie nmethod");
johnc@5548 1856 assert(!is_unloaded(), "should not call follow on unloaded nmethod");
duke@435 1857
duke@435 1858 // If the method is not entrant or zombie then a JMP is plastered over the
duke@435 1859 // first few bytes. If an oop in the old code was there, that oop
duke@435 1860 // should not get GC'd. Skip the first few bytes of oops on
duke@435 1861 // not-entrant methods.
duke@435 1862 address low_boundary = verified_entry_point();
duke@435 1863 if (is_not_entrant()) {
duke@435 1864 low_boundary += NativeJump::instruction_size;
duke@435 1865 // %%% Note: On SPARC we patch only a 4-byte trap, not a full NativeJump.
duke@435 1866 // (See comment above.)
duke@435 1867 }
duke@435 1868
duke@435 1869 RelocIterator iter(this, low_boundary);
ysr@1376 1870
duke@435 1871 while (iter.next()) {
duke@435 1872 if (iter.type() == relocInfo::oop_type ) {
duke@435 1873 oop_Relocation* r = iter.oop_reloc();
duke@435 1874 // In this loop, we must only follow those oops directly embedded in
duke@435 1875 // the code. Other oops (oop_index>0) are seen as part of scopes_oops.
ysr@1376 1876 assert(1 == (r->oop_is_immediate()) +
ysr@1376 1877 (r->oop_addr() >= oops_begin() && r->oop_addr() < oops_end()),
ysr@1376 1878 "oop must be found in exactly one place");
duke@435 1879 if (r->oop_is_immediate() && r->oop_value() != NULL) {
duke@435 1880 f->do_oop(r->oop_addr());
duke@435 1881 }
duke@435 1882 }
duke@435 1883 }
duke@435 1884
duke@435 1885 // Scopes
jrose@1424 1886 // This includes oop constants not inlined in the code stream.
duke@435 1887 for (oop* p = oops_begin(); p < oops_end(); p++) {
duke@435 1888 if (*p == Universe::non_oop_word()) continue; // skip non-oops
duke@435 1889 f->do_oop(p);
duke@435 1890 }
duke@435 1891 }
duke@435 1892
jrose@1424 1893 #define NMETHOD_SENTINEL ((nmethod*)badAddress)
jrose@1424 1894
jrose@1424 1895 nmethod* volatile nmethod::_oops_do_mark_nmethods;
jrose@1424 1896
jrose@1424 1897 // An nmethod is "marked" if its _mark_link is set non-null.
jrose@1424 1898 // Even if it is the end of the linked list, it will have a non-null link value,
jrose@1424 1899 // as long as it is on the list.
jrose@1424 1900 // This code must be MP safe, because it is used from parallel GC passes.
jrose@1424 1901 bool nmethod::test_set_oops_do_mark() {
jrose@1424 1902 assert(nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called");
jrose@1424 1903 nmethod* observed_mark_link = _oops_do_mark_link;
jrose@1424 1904 if (observed_mark_link == NULL) {
jrose@1424 1905 // Claim this nmethod for this thread to mark.
jrose@1424 1906 observed_mark_link = (nmethod*)
jrose@1424 1907 Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_link, NULL);
jrose@1424 1908 if (observed_mark_link == NULL) {
jrose@1424 1909
jrose@1424 1910 // Atomically append this nmethod (now claimed) to the head of the list:
jrose@1424 1911 nmethod* observed_mark_nmethods = _oops_do_mark_nmethods;
jrose@1424 1912 for (;;) {
jrose@1424 1913 nmethod* required_mark_nmethods = observed_mark_nmethods;
jrose@1424 1914 _oops_do_mark_link = required_mark_nmethods;
jrose@1424 1915 observed_mark_nmethods = (nmethod*)
jrose@1424 1916 Atomic::cmpxchg_ptr(this, &_oops_do_mark_nmethods, required_mark_nmethods);
jrose@1424 1917 if (observed_mark_nmethods == required_mark_nmethods)
jrose@1424 1918 break;
jrose@1424 1919 }
jrose@1424 1920 // Mark was clear when we first saw this guy.
twisti@2687 1921 NOT_PRODUCT(if (TraceScavenge) print_on(tty, "oops_do, mark"));
jrose@1424 1922 return false;
jrose@1424 1923 }
jrose@1424 1924 }
jrose@1424 1925 // On fall through, another racing thread marked this nmethod before we did.
jrose@1424 1926 return true;
jrose@1424 1927 }
jrose@1424 1928
jrose@1424 1929 void nmethod::oops_do_marking_prologue() {
jrose@1424 1930 NOT_PRODUCT(if (TraceScavenge) tty->print_cr("[oops_do_marking_prologue"));
jrose@1424 1931 assert(_oops_do_mark_nmethods == NULL, "must not call oops_do_marking_prologue twice in a row");
jrose@1424 1932 // We use cmpxchg_ptr instead of regular assignment here because the user
jrose@1424 1933 // may fork a bunch of threads, and we need them all to see the same state.
jrose@1424 1934 void* observed = Atomic::cmpxchg_ptr(NMETHOD_SENTINEL, &_oops_do_mark_nmethods, NULL);
jrose@1424 1935 guarantee(observed == NULL, "no races in this sequential code");
jrose@1424 1936 }
jrose@1424 1937
jrose@1424 1938 void nmethod::oops_do_marking_epilogue() {
jrose@1424 1939 assert(_oops_do_mark_nmethods != NULL, "must not call oops_do_marking_epilogue twice in a row");
jrose@1424 1940 nmethod* cur = _oops_do_mark_nmethods;
jrose@1424 1941 while (cur != NMETHOD_SENTINEL) {
jrose@1424 1942 assert(cur != NULL, "not NULL-terminated");
jrose@1424 1943 nmethod* next = cur->_oops_do_mark_link;
jrose@1424 1944 cur->_oops_do_mark_link = NULL;
never@2657 1945 cur->fix_oop_relocations();
twisti@2687 1946 NOT_PRODUCT(if (TraceScavenge) cur->print_on(tty, "oops_do, unmark"));
jrose@1424 1947 cur = next;
jrose@1424 1948 }
jrose@1424 1949 void* required = _oops_do_mark_nmethods;
jrose@1424 1950 void* observed = Atomic::cmpxchg_ptr(NULL, &_oops_do_mark_nmethods, required);
jrose@1424 1951 guarantee(observed == required, "no races in this sequential code");
jrose@1424 1952 NOT_PRODUCT(if (TraceScavenge) tty->print_cr("oops_do_marking_epilogue]"));
jrose@1424 1953 }
jrose@1424 1954
jrose@1424 1955 class DetectScavengeRoot: public OopClosure {
jrose@1424 1956 bool _detected_scavenge_root;
jrose@1424 1957 public:
jrose@1424 1958 DetectScavengeRoot() : _detected_scavenge_root(false)
jrose@1424 1959 { NOT_PRODUCT(_print_nm = NULL); }
jrose@1424 1960 bool detected_scavenge_root() { return _detected_scavenge_root; }
jrose@1424 1961 virtual void do_oop(oop* p) {
jrose@1424 1962 if ((*p) != NULL && (*p)->is_scavengable()) {
jrose@1424 1963 NOT_PRODUCT(maybe_print(p));
jrose@1424 1964 _detected_scavenge_root = true;
jrose@1424 1965 }
jrose@1424 1966 }
jrose@1424 1967 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
jrose@1424 1968
jrose@1424 1969 #ifndef PRODUCT
jrose@1424 1970 nmethod* _print_nm;
jrose@1424 1971 void maybe_print(oop* p) {
jrose@1424 1972 if (_print_nm == NULL) return;
jrose@1424 1973 if (!_detected_scavenge_root) _print_nm->print_on(tty, "new scavenge root");
jmasa@2909 1974 tty->print_cr(""PTR_FORMAT"[offset=%d] detected scavengable oop "PTR_FORMAT" (found at "PTR_FORMAT")",
jrose@1424 1975 _print_nm, (int)((intptr_t)p - (intptr_t)_print_nm),
hseigel@5784 1976 (void *)(*p), (intptr_t)p);
jrose@1424 1977 (*p)->print();
jrose@1424 1978 }
jrose@1424 1979 #endif //PRODUCT
jrose@1424 1980 };
jrose@1424 1981
jrose@1424 1982 bool nmethod::detect_scavenge_root_oops() {
jrose@1424 1983 DetectScavengeRoot detect_scavenge_root;
jrose@1424 1984 NOT_PRODUCT(if (TraceScavenge) detect_scavenge_root._print_nm = this);
jrose@1424 1985 oops_do(&detect_scavenge_root);
jrose@1424 1986 return detect_scavenge_root.detected_scavenge_root();
jrose@1424 1987 }
jrose@1424 1988
duke@435 1989 // Method that knows how to preserve outgoing arguments at call. This method must be
duke@435 1990 // called with a frame corresponding to a Java invoke
duke@435 1991 void nmethod::preserve_callee_argument_oops(frame fr, const RegisterMap *reg_map, OopClosure* f) {
twisti@2047 1992 #ifndef SHARK
duke@435 1993 if (!method()->is_native()) {
duke@435 1994 SimpleScopeDesc ssd(this, fr.pc());
never@2462 1995 Bytecode_invoke call(ssd.method(), ssd.bci());
roland@5222 1996 bool has_receiver = call.has_receiver();
roland@5222 1997 bool has_appendix = call.has_appendix();
coleenp@2497 1998 Symbol* signature = call.signature();
roland@5222 1999 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
duke@435 2000 }
twisti@2047 2001 #endif // !SHARK
duke@435 2002 }
duke@435 2003
duke@435 2004
duke@435 2005 oop nmethod::embeddedOop_at(u_char* p) {
never@2631 2006 RelocIterator iter(this, p, p + 1);
duke@435 2007 while (iter.next())
duke@435 2008 if (iter.type() == relocInfo::oop_type) {
duke@435 2009 return iter.oop_reloc()->oop_value();
duke@435 2010 }
duke@435 2011 return NULL;
duke@435 2012 }
duke@435 2013
duke@435 2014
duke@435 2015 inline bool includes(void* p, void* from, void* to) {
duke@435 2016 return from <= p && p < to;
duke@435 2017 }
duke@435 2018
duke@435 2019
duke@435 2020 void nmethod::copy_scopes_pcs(PcDesc* pcs, int count) {
duke@435 2021 assert(count >= 2, "must be sentinel values, at least");
duke@435 2022
duke@435 2023 #ifdef ASSERT
duke@435 2024 // must be sorted and unique; we do a binary search in find_pc_desc()
duke@435 2025 int prev_offset = pcs[0].pc_offset();
duke@435 2026 assert(prev_offset == PcDesc::lower_offset_limit,
duke@435 2027 "must start with a sentinel");
duke@435 2028 for (int i = 1; i < count; i++) {
duke@435 2029 int this_offset = pcs[i].pc_offset();
duke@435 2030 assert(this_offset > prev_offset, "offsets must be sorted");
duke@435 2031 prev_offset = this_offset;
duke@435 2032 }
duke@435 2033 assert(prev_offset == PcDesc::upper_offset_limit,
duke@435 2034 "must end with a sentinel");
duke@435 2035 #endif //ASSERT
duke@435 2036
twisti@1570 2037 // Search for MethodHandle invokes and tag the nmethod.
twisti@1570 2038 for (int i = 0; i < count; i++) {
twisti@1570 2039 if (pcs[i].is_method_handle_invoke()) {
twisti@1570 2040 set_has_method_handle_invokes(true);
twisti@1570 2041 break;
twisti@1570 2042 }
twisti@1570 2043 }
twisti@2406 2044 assert(has_method_handle_invokes() == (_deoptimize_mh_offset != -1), "must have deopt mh handler");
twisti@1570 2045
duke@435 2046 int size = count * sizeof(PcDesc);
duke@435 2047 assert(scopes_pcs_size() >= size, "oob");
duke@435 2048 memcpy(scopes_pcs_begin(), pcs, size);
duke@435 2049
duke@435 2050 // Adjust the final sentinel downward.
duke@435 2051 PcDesc* last_pc = &scopes_pcs_begin()[count-1];
duke@435 2052 assert(last_pc->pc_offset() == PcDesc::upper_offset_limit, "sanity");
twisti@2103 2053 last_pc->set_pc_offset(content_size() + 1);
duke@435 2054 for (; last_pc + 1 < scopes_pcs_end(); last_pc += 1) {
duke@435 2055 // Fill any rounding gaps with copies of the last record.
duke@435 2056 last_pc[1] = last_pc[0];
duke@435 2057 }
duke@435 2058 // The following assert could fail if sizeof(PcDesc) is not
duke@435 2059 // an integral multiple of oopSize (the rounding term).
duke@435 2060 // If it fails, change the logic to always allocate a multiple
duke@435 2061 // of sizeof(PcDesc), and fill unused words with copies of *last_pc.
duke@435 2062 assert(last_pc + 1 == scopes_pcs_end(), "must match exactly");
duke@435 2063 }
duke@435 2064
duke@435 2065 void nmethod::copy_scopes_data(u_char* buffer, int size) {
duke@435 2066 assert(scopes_data_size() >= size, "oob");
duke@435 2067 memcpy(scopes_data_begin(), buffer, size);
duke@435 2068 }
duke@435 2069
duke@435 2070
duke@435 2071 #ifdef ASSERT
duke@435 2072 static PcDesc* linear_search(nmethod* nm, int pc_offset, bool approximate) {
duke@435 2073 PcDesc* lower = nm->scopes_pcs_begin();
duke@435 2074 PcDesc* upper = nm->scopes_pcs_end();
duke@435 2075 lower += 1; // exclude initial sentinel
duke@435 2076 PcDesc* res = NULL;
duke@435 2077 for (PcDesc* p = lower; p < upper; p++) {
duke@435 2078 NOT_PRODUCT(--nmethod_stats.pc_desc_tests); // don't count this call to match_desc
duke@435 2079 if (match_desc(p, pc_offset, approximate)) {
duke@435 2080 if (res == NULL)
duke@435 2081 res = p;
duke@435 2082 else
duke@435 2083 res = (PcDesc*) badAddress;
duke@435 2084 }
duke@435 2085 }
duke@435 2086 return res;
duke@435 2087 }
duke@435 2088 #endif
duke@435 2089
duke@435 2090
duke@435 2091 // Finds a PcDesc with real-pc equal to "pc"
duke@435 2092 PcDesc* nmethod::find_pc_desc_internal(address pc, bool approximate) {
twisti@2103 2093 address base_address = code_begin();
duke@435 2094 if ((pc < base_address) ||
duke@435 2095 (pc - base_address) >= (ptrdiff_t) PcDesc::upper_offset_limit) {
duke@435 2096 return NULL; // PC is wildly out of range
duke@435 2097 }
duke@435 2098 int pc_offset = (int) (pc - base_address);
duke@435 2099
duke@435 2100 // Check the PcDesc cache if it contains the desired PcDesc
duke@435 2101 // (This as an almost 100% hit rate.)
duke@435 2102 PcDesc* res = _pc_desc_cache.find_pc_desc(pc_offset, approximate);
duke@435 2103 if (res != NULL) {
duke@435 2104 assert(res == linear_search(this, pc_offset, approximate), "cache ok");
duke@435 2105 return res;
duke@435 2106 }
duke@435 2107
duke@435 2108 // Fallback algorithm: quasi-linear search for the PcDesc
duke@435 2109 // Find the last pc_offset less than the given offset.
duke@435 2110 // The successor must be the required match, if there is a match at all.
duke@435 2111 // (Use a fixed radix to avoid expensive affine pointer arithmetic.)
duke@435 2112 PcDesc* lower = scopes_pcs_begin();
duke@435 2113 PcDesc* upper = scopes_pcs_end();
duke@435 2114 upper -= 1; // exclude final sentinel
duke@435 2115 if (lower >= upper) return NULL; // native method; no PcDescs at all
duke@435 2116
duke@435 2117 #define assert_LU_OK \
duke@435 2118 /* invariant on lower..upper during the following search: */ \
duke@435 2119 assert(lower->pc_offset() < pc_offset, "sanity"); \
duke@435 2120 assert(upper->pc_offset() >= pc_offset, "sanity")
duke@435 2121 assert_LU_OK;
duke@435 2122
duke@435 2123 // Use the last successful return as a split point.
duke@435 2124 PcDesc* mid = _pc_desc_cache.last_pc_desc();
duke@435 2125 NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
duke@435 2126 if (mid->pc_offset() < pc_offset) {
duke@435 2127 lower = mid;
duke@435 2128 } else {
duke@435 2129 upper = mid;
duke@435 2130 }
duke@435 2131
duke@435 2132 // Take giant steps at first (4096, then 256, then 16, then 1)
duke@435 2133 const int LOG2_RADIX = 4 /*smaller steps in debug mode:*/ debug_only(-1);
duke@435 2134 const int RADIX = (1 << LOG2_RADIX);
duke@435 2135 for (int step = (1 << (LOG2_RADIX*3)); step > 1; step >>= LOG2_RADIX) {
duke@435 2136 while ((mid = lower + step) < upper) {
duke@435 2137 assert_LU_OK;
duke@435 2138 NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
duke@435 2139 if (mid->pc_offset() < pc_offset) {
duke@435 2140 lower = mid;
duke@435 2141 } else {
duke@435 2142 upper = mid;
duke@435 2143 break;
duke@435 2144 }
duke@435 2145 }
duke@435 2146 assert_LU_OK;
duke@435 2147 }
duke@435 2148
duke@435 2149 // Sneak up on the value with a linear search of length ~16.
duke@435 2150 while (true) {
duke@435 2151 assert_LU_OK;
duke@435 2152 mid = lower + 1;
duke@435 2153 NOT_PRODUCT(++nmethod_stats.pc_desc_searches);
duke@435 2154 if (mid->pc_offset() < pc_offset) {
duke@435 2155 lower = mid;
duke@435 2156 } else {
duke@435 2157 upper = mid;
duke@435 2158 break;
duke@435 2159 }
duke@435 2160 }
duke@435 2161 #undef assert_LU_OK
duke@435 2162
duke@435 2163 if (match_desc(upper, pc_offset, approximate)) {
duke@435 2164 assert(upper == linear_search(this, pc_offset, approximate), "search ok");
duke@435 2165 _pc_desc_cache.add_pc_desc(upper);
duke@435 2166 return upper;
duke@435 2167 } else {
duke@435 2168 assert(NULL == linear_search(this, pc_offset, approximate), "search ok");
duke@435 2169 return NULL;
duke@435 2170 }
duke@435 2171 }
duke@435 2172
duke@435 2173
duke@435 2174 bool nmethod::check_all_dependencies() {
duke@435 2175 bool found_check = false;
duke@435 2176 // wholesale check of all dependencies
duke@435 2177 for (Dependencies::DepStream deps(this); deps.next(); ) {
duke@435 2178 if (deps.check_dependency() != NULL) {
duke@435 2179 found_check = true;
duke@435 2180 NOT_DEBUG(break);
duke@435 2181 }
duke@435 2182 }
duke@435 2183 return found_check; // tell caller if we found anything
duke@435 2184 }
duke@435 2185
duke@435 2186 bool nmethod::check_dependency_on(DepChange& changes) {
duke@435 2187 // What has happened:
duke@435 2188 // 1) a new class dependee has been added
duke@435 2189 // 2) dependee and all its super classes have been marked
duke@435 2190 bool found_check = false; // set true if we are upset
duke@435 2191 for (Dependencies::DepStream deps(this); deps.next(); ) {
duke@435 2192 // Evaluate only relevant dependencies.
duke@435 2193 if (deps.spot_check_dependency_at(changes) != NULL) {
duke@435 2194 found_check = true;
duke@435 2195 NOT_DEBUG(break);
duke@435 2196 }
duke@435 2197 }
duke@435 2198 return found_check;
duke@435 2199 }
duke@435 2200
coleenp@4037 2201 bool nmethod::is_evol_dependent_on(Klass* dependee) {
coleenp@4037 2202 InstanceKlass *dependee_ik = InstanceKlass::cast(dependee);
coleenp@4037 2203 Array<Method*>* dependee_methods = dependee_ik->methods();
duke@435 2204 for (Dependencies::DepStream deps(this); deps.next(); ) {
duke@435 2205 if (deps.type() == Dependencies::evol_method) {
coleenp@4037 2206 Method* method = deps.method_argument(0);
duke@435 2207 for (int j = 0; j < dependee_methods->length(); j++) {
coleenp@4037 2208 if (dependee_methods->at(j) == method) {
duke@435 2209 // RC_TRACE macro has an embedded ResourceMark
duke@435 2210 RC_TRACE(0x01000000,
duke@435 2211 ("Found evol dependency of nmethod %s.%s(%s) compile_id=%d on method %s.%s(%s)",
coleenp@4037 2212 _method->method_holder()->external_name(),
duke@435 2213 _method->name()->as_C_string(),
duke@435 2214 _method->signature()->as_C_string(), compile_id(),
coleenp@4037 2215 method->method_holder()->external_name(),
duke@435 2216 method->name()->as_C_string(),
duke@435 2217 method->signature()->as_C_string()));
duke@435 2218 if (TraceDependencies || LogCompilation)
duke@435 2219 deps.log_dependency(dependee);
duke@435 2220 return true;
duke@435 2221 }
duke@435 2222 }
duke@435 2223 }
duke@435 2224 }
duke@435 2225 return false;
duke@435 2226 }
duke@435 2227
duke@435 2228 // Called from mark_for_deoptimization, when dependee is invalidated.
coleenp@4037 2229 bool nmethod::is_dependent_on_method(Method* dependee) {
duke@435 2230 for (Dependencies::DepStream deps(this); deps.next(); ) {
duke@435 2231 if (deps.type() != Dependencies::evol_method)
duke@435 2232 continue;
coleenp@4037 2233 Method* method = deps.method_argument(0);
duke@435 2234 if (method == dependee) return true;
duke@435 2235 }
duke@435 2236 return false;
duke@435 2237 }
duke@435 2238
duke@435 2239
duke@435 2240 bool nmethod::is_patchable_at(address instr_addr) {
twisti@2103 2241 assert(insts_contains(instr_addr), "wrong nmethod used");
duke@435 2242 if (is_zombie()) {
duke@435 2243 // a zombie may never be patched
duke@435 2244 return false;
duke@435 2245 }
duke@435 2246 return true;
duke@435 2247 }
duke@435 2248
duke@435 2249
duke@435 2250 address nmethod::continuation_for_implicit_exception(address pc) {
duke@435 2251 // Exception happened outside inline-cache check code => we are inside
duke@435 2252 // an active nmethod => use cpc to determine a return address
twisti@2103 2253 int exception_offset = pc - code_begin();
duke@435 2254 int cont_offset = ImplicitExceptionTable(this).at( exception_offset );
duke@435 2255 #ifdef ASSERT
duke@435 2256 if (cont_offset == 0) {
duke@435 2257 Thread* thread = ThreadLocalStorage::get_thread_slow();
duke@435 2258 ResetNoHandleMark rnm; // Might be called from LEAF/QUICK ENTRY
duke@435 2259 HandleMark hm(thread);
duke@435 2260 ResourceMark rm(thread);
duke@435 2261 CodeBlob* cb = CodeCache::find_blob(pc);
duke@435 2262 assert(cb != NULL && cb == this, "");
duke@435 2263 tty->print_cr("implicit exception happened at " INTPTR_FORMAT, pc);
duke@435 2264 print();
duke@435 2265 method()->print_codes();
duke@435 2266 print_code();
duke@435 2267 print_pcs();
duke@435 2268 }
duke@435 2269 #endif
never@1685 2270 if (cont_offset == 0) {
never@1685 2271 // Let the normal error handling report the exception
never@1685 2272 return NULL;
never@1685 2273 }
twisti@2103 2274 return code_begin() + cont_offset;
duke@435 2275 }
duke@435 2276
duke@435 2277
duke@435 2278
duke@435 2279 void nmethod_init() {
duke@435 2280 // make sure you didn't forget to adjust the filler fields
duke@435 2281 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
duke@435 2282 }
duke@435 2283
duke@435 2284
duke@435 2285 //-------------------------------------------------------------------------------------------
duke@435 2286
duke@435 2287
duke@435 2288 // QQQ might we make this work from a frame??
duke@435 2289 nmethodLocker::nmethodLocker(address pc) {
duke@435 2290 CodeBlob* cb = CodeCache::find_blob(pc);
duke@435 2291 guarantee(cb != NULL && cb->is_nmethod(), "bad pc for a nmethod found");
duke@435 2292 _nm = (nmethod*)cb;
duke@435 2293 lock_nmethod(_nm);
duke@435 2294 }
duke@435 2295
dcubed@2624 2296 // Only JvmtiDeferredEvent::compiled_method_unload_event()
dcubed@2624 2297 // should pass zombie_ok == true.
dcubed@2624 2298 void nmethodLocker::lock_nmethod(nmethod* nm, bool zombie_ok) {
duke@435 2299 if (nm == NULL) return;
duke@435 2300 Atomic::inc(&nm->_lock_count);
dcubed@2624 2301 guarantee(zombie_ok || !nm->is_zombie(), "cannot lock a zombie method");
duke@435 2302 }
duke@435 2303
duke@435 2304 void nmethodLocker::unlock_nmethod(nmethod* nm) {
duke@435 2305 if (nm == NULL) return;
duke@435 2306 Atomic::dec(&nm->_lock_count);
duke@435 2307 guarantee(nm->_lock_count >= 0, "unmatched nmethod lock/unlock");
duke@435 2308 }
duke@435 2309
twisti@1639 2310
twisti@1639 2311 // -----------------------------------------------------------------------------
twisti@1639 2312 // nmethod::get_deopt_original_pc
twisti@1639 2313 //
twisti@1639 2314 // Return the original PC for the given PC if:
twisti@1639 2315 // (a) the given PC belongs to a nmethod and
twisti@1639 2316 // (b) it is a deopt PC
twisti@1639 2317 address nmethod::get_deopt_original_pc(const frame* fr) {
twisti@1639 2318 if (fr->cb() == NULL) return NULL;
twisti@1639 2319
twisti@1639 2320 nmethod* nm = fr->cb()->as_nmethod_or_null();
twisti@1639 2321 if (nm != NULL && nm->is_deopt_pc(fr->pc()))
twisti@1639 2322 return nm->get_original_pc(fr);
twisti@1639 2323
twisti@1639 2324 return NULL;
duke@435 2325 }
duke@435 2326
duke@435 2327
duke@435 2328 // -----------------------------------------------------------------------------
twisti@1570 2329 // MethodHandle
twisti@1570 2330
twisti@1570 2331 bool nmethod::is_method_handle_return(address return_pc) {
twisti@1570 2332 if (!has_method_handle_invokes()) return false;
twisti@1570 2333 PcDesc* pd = pc_desc_at(return_pc);
twisti@1570 2334 if (pd == NULL)
twisti@1570 2335 return false;
twisti@1570 2336 return pd->is_method_handle_invoke();
twisti@1570 2337 }
twisti@1570 2338
twisti@1570 2339
twisti@1570 2340 // -----------------------------------------------------------------------------
duke@435 2341 // Verification
duke@435 2342
jrose@1424 2343 class VerifyOopsClosure: public OopClosure {
jrose@1424 2344 nmethod* _nm;
jrose@1424 2345 bool _ok;
jrose@1424 2346 public:
jrose@1424 2347 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
jrose@1424 2348 bool ok() { return _ok; }
jrose@1424 2349 virtual void do_oop(oop* p) {
jrose@1424 2350 if ((*p) == NULL || (*p)->is_oop()) return;
jrose@1424 2351 if (_ok) {
jrose@1424 2352 _nm->print_nmethod(true);
jrose@1424 2353 _ok = false;
jrose@1424 2354 }
jrose@1424 2355 tty->print_cr("*** non-oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)",
hseigel@5784 2356 (void *)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm));
jrose@1424 2357 }
jrose@1424 2358 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
jrose@1424 2359 };
jrose@1424 2360
duke@435 2361 void nmethod::verify() {
duke@435 2362
duke@435 2363 // Hmm. OSR methods can be deopted but not marked as zombie or not_entrant
duke@435 2364 // seems odd.
duke@435 2365
duke@435 2366 if( is_zombie() || is_not_entrant() )
duke@435 2367 return;
duke@435 2368
duke@435 2369 // Make sure all the entry points are correctly aligned for patching.
duke@435 2370 NativeJump::check_verified_entry_alignment(entry_point(), verified_entry_point());
duke@435 2371
coleenp@4037 2372 // assert(method()->is_oop(), "must be valid");
duke@435 2373
duke@435 2374 ResourceMark rm;
duke@435 2375
duke@435 2376 if (!CodeCache::contains(this)) {
jcoomes@1845 2377 fatal(err_msg("nmethod at " INTPTR_FORMAT " not in zone", this));
duke@435 2378 }
duke@435 2379
duke@435 2380 if(is_native_method() )
duke@435 2381 return;
duke@435 2382
duke@435 2383 nmethod* nm = CodeCache::find_nmethod(verified_entry_point());
duke@435 2384 if (nm != this) {
jcoomes@1845 2385 fatal(err_msg("findNMethod did not find this nmethod (" INTPTR_FORMAT ")",
jcoomes@1845 2386 this));
duke@435 2387 }
duke@435 2388
duke@435 2389 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
duke@435 2390 if (! p->verify(this)) {
duke@435 2391 tty->print_cr("\t\tin nmethod at " INTPTR_FORMAT " (pcs)", this);
duke@435 2392 }
duke@435 2393 }
duke@435 2394
jrose@1424 2395 VerifyOopsClosure voc(this);
jrose@1424 2396 oops_do(&voc);
jrose@1424 2397 assert(voc.ok(), "embedded oops must be OK");
jrose@1424 2398 verify_scavenge_root_oops();
jrose@1424 2399
duke@435 2400 verify_scopes();
duke@435 2401 }
duke@435 2402
duke@435 2403
duke@435 2404 void nmethod::verify_interrupt_point(address call_site) {
vlivanov@6096 2405 // Verify IC only when nmethod installation is finished.
kvn@6172 2406 bool is_installed = (method()->code() == this) // nmethod is in state 'in_use' and installed
kvn@6172 2407 || !this->is_in_use(); // nmethod is installed, but not in 'in_use' state
vlivanov@6096 2408 if (is_installed) {
vlivanov@6096 2409 Thread *cur = Thread::current();
vlivanov@6096 2410 if (CompiledIC_lock->owner() == cur ||
vlivanov@6096 2411 ((cur->is_VM_thread() || cur->is_ConcurrentGC_thread()) &&
vlivanov@6096 2412 SafepointSynchronize::is_at_safepoint())) {
vlivanov@6096 2413 CompiledIC_at(this, call_site);
vlivanov@6096 2414 CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
vlivanov@6096 2415 } else {
vlivanov@6096 2416 MutexLocker ml_verify (CompiledIC_lock);
vlivanov@6096 2417 CompiledIC_at(this, call_site);
vlivanov@6096 2418 }
duke@435 2419 }
vlivanov@6096 2420
vlivanov@6096 2421 PcDesc* pd = pc_desc_at(nativeCall_at(call_site)->return_address());
duke@435 2422 assert(pd != NULL, "PcDesc must exist");
duke@435 2423 for (ScopeDesc* sd = new ScopeDesc(this, pd->scope_decode_offset(),
kvn@1688 2424 pd->obj_decode_offset(), pd->should_reexecute(),
kvn@1688 2425 pd->return_oop());
duke@435 2426 !sd->is_top(); sd = sd->sender()) {
duke@435 2427 sd->verify();
duke@435 2428 }
duke@435 2429 }
duke@435 2430
duke@435 2431 void nmethod::verify_scopes() {
duke@435 2432 if( !method() ) return; // Runtime stubs have no scope
duke@435 2433 if (method()->is_native()) return; // Ignore stub methods.
duke@435 2434 // iterate through all interrupt point
duke@435 2435 // and verify the debug information is valid.
duke@435 2436 RelocIterator iter((nmethod*)this);
duke@435 2437 while (iter.next()) {
duke@435 2438 address stub = NULL;
duke@435 2439 switch (iter.type()) {
duke@435 2440 case relocInfo::virtual_call_type:
duke@435 2441 verify_interrupt_point(iter.addr());
duke@435 2442 break;
duke@435 2443 case relocInfo::opt_virtual_call_type:
duke@435 2444 stub = iter.opt_virtual_call_reloc()->static_stub();
duke@435 2445 verify_interrupt_point(iter.addr());
duke@435 2446 break;
duke@435 2447 case relocInfo::static_call_type:
duke@435 2448 stub = iter.static_call_reloc()->static_stub();
duke@435 2449 //verify_interrupt_point(iter.addr());
duke@435 2450 break;
duke@435 2451 case relocInfo::runtime_call_type:
duke@435 2452 address destination = iter.reloc()->value();
duke@435 2453 // Right now there is no way to find out which entries support
duke@435 2454 // an interrupt point. It would be nice if we had this
duke@435 2455 // information in a table.
duke@435 2456 break;
duke@435 2457 }
duke@435 2458 assert(stub == NULL || stub_contains(stub), "static call stub outside stub section");
duke@435 2459 }
duke@435 2460 }
duke@435 2461
duke@435 2462
duke@435 2463 // -----------------------------------------------------------------------------
duke@435 2464 // Non-product code
duke@435 2465 #ifndef PRODUCT
duke@435 2466
jrose@1424 2467 class DebugScavengeRoot: public OopClosure {
jrose@1424 2468 nmethod* _nm;
jrose@1424 2469 bool _ok;
jrose@1424 2470 public:
jrose@1424 2471 DebugScavengeRoot(nmethod* nm) : _nm(nm), _ok(true) { }
jrose@1424 2472 bool ok() { return _ok; }
jrose@1424 2473 virtual void do_oop(oop* p) {
jrose@1424 2474 if ((*p) == NULL || !(*p)->is_scavengable()) return;
jrose@1424 2475 if (_ok) {
jrose@1424 2476 _nm->print_nmethod(true);
jrose@1424 2477 _ok = false;
jrose@1424 2478 }
jmasa@2909 2479 tty->print_cr("*** scavengable oop "PTR_FORMAT" found at "PTR_FORMAT" (offset %d)",
hseigel@5784 2480 (void *)(*p), (intptr_t)p, (int)((intptr_t)p - (intptr_t)_nm));
jrose@1424 2481 (*p)->print();
jrose@1424 2482 }
jrose@1424 2483 virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
jrose@1424 2484 };
duke@435 2485
jrose@1424 2486 void nmethod::verify_scavenge_root_oops() {
jrose@1424 2487 if (!on_scavenge_root_list()) {
jrose@1424 2488 // Actually look inside, to verify the claim that it's clean.
jrose@1424 2489 DebugScavengeRoot debug_scavenge_root(this);
jrose@1424 2490 oops_do(&debug_scavenge_root);
jrose@1424 2491 if (!debug_scavenge_root.ok())
jmasa@2909 2492 fatal("found an unadvertised bad scavengable oop in the code cache");
duke@435 2493 }
jrose@1424 2494 assert(scavenge_root_not_marked(), "");
duke@435 2495 }
duke@435 2496
jrose@535 2497 #endif // PRODUCT
duke@435 2498
duke@435 2499 // Printing operations
duke@435 2500
duke@435 2501 void nmethod::print() const {
duke@435 2502 ResourceMark rm;
duke@435 2503 ttyLocker ttyl; // keep the following output all in one block
duke@435 2504
twisti@2687 2505 tty->print("Compiled method ");
duke@435 2506
duke@435 2507 if (is_compiled_by_c1()) {
duke@435 2508 tty->print("(c1) ");
duke@435 2509 } else if (is_compiled_by_c2()) {
duke@435 2510 tty->print("(c2) ");
twisti@2047 2511 } else if (is_compiled_by_shark()) {
twisti@2047 2512 tty->print("(shark) ");
duke@435 2513 } else {
duke@435 2514 tty->print("(nm) ");
duke@435 2515 }
duke@435 2516
twisti@2687 2517 print_on(tty, NULL);
twisti@2687 2518
duke@435 2519 if (WizardMode) {
duke@435 2520 tty->print("((nmethod*) "INTPTR_FORMAT ") ", this);
duke@435 2521 tty->print(" for method " INTPTR_FORMAT , (address)method());
duke@435 2522 tty->print(" { ");
duke@435 2523 if (is_in_use()) tty->print("in_use ");
duke@435 2524 if (is_not_entrant()) tty->print("not_entrant ");
duke@435 2525 if (is_zombie()) tty->print("zombie ");
duke@435 2526 if (is_unloaded()) tty->print("unloaded ");
jrose@1424 2527 if (on_scavenge_root_list()) tty->print("scavenge_root ");
duke@435 2528 tty->print_cr("}:");
duke@435 2529 }
duke@435 2530 if (size () > 0) tty->print_cr(" total in heap [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
duke@435 2531 (address)this,
duke@435 2532 (address)this + size(),
duke@435 2533 size());
duke@435 2534 if (relocation_size () > 0) tty->print_cr(" relocation [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
duke@435 2535 relocation_begin(),
duke@435 2536 relocation_end(),
duke@435 2537 relocation_size());
twisti@2117 2538 if (consts_size () > 0) tty->print_cr(" constants [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
twisti@2117 2539 consts_begin(),
twisti@2117 2540 consts_end(),
twisti@2117 2541 consts_size());
twisti@2103 2542 if (insts_size () > 0) tty->print_cr(" main code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
twisti@2103 2543 insts_begin(),
twisti@2103 2544 insts_end(),
twisti@2103 2545 insts_size());
duke@435 2546 if (stub_size () > 0) tty->print_cr(" stub code [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
duke@435 2547 stub_begin(),
duke@435 2548 stub_end(),
duke@435 2549 stub_size());
twisti@1918 2550 if (oops_size () > 0) tty->print_cr(" oops [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
twisti@1918 2551 oops_begin(),
twisti@1918 2552 oops_end(),
twisti@1918 2553 oops_size());
coleenp@4037 2554 if (metadata_size () > 0) tty->print_cr(" metadata [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
coleenp@4037 2555 metadata_begin(),
coleenp@4037 2556 metadata_end(),
coleenp@4037 2557 metadata_size());
duke@435 2558 if (scopes_data_size () > 0) tty->print_cr(" scopes data [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
duke@435 2559 scopes_data_begin(),
duke@435 2560 scopes_data_end(),
duke@435 2561 scopes_data_size());
duke@435 2562 if (scopes_pcs_size () > 0) tty->print_cr(" scopes pcs [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
duke@435 2563 scopes_pcs_begin(),
duke@435 2564 scopes_pcs_end(),
duke@435 2565 scopes_pcs_size());
duke@435 2566 if (dependencies_size () > 0) tty->print_cr(" dependencies [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
duke@435 2567 dependencies_begin(),
duke@435 2568 dependencies_end(),
duke@435 2569 dependencies_size());
duke@435 2570 if (handler_table_size() > 0) tty->print_cr(" handler table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
duke@435 2571 handler_table_begin(),
duke@435 2572 handler_table_end(),
duke@435 2573 handler_table_size());
duke@435 2574 if (nul_chk_table_size() > 0) tty->print_cr(" nul chk table [" INTPTR_FORMAT "," INTPTR_FORMAT "] = %d",
duke@435 2575 nul_chk_table_begin(),
duke@435 2576 nul_chk_table_end(),
duke@435 2577 nul_chk_table_size());
duke@435 2578 }
duke@435 2579
jrose@535 2580 void nmethod::print_code() {
jrose@535 2581 HandleMark hm;
jrose@535 2582 ResourceMark m;
jrose@535 2583 Disassembler::decode(this);
jrose@535 2584 }
jrose@535 2585
jrose@535 2586
jrose@535 2587 #ifndef PRODUCT
duke@435 2588
duke@435 2589 void nmethod::print_scopes() {
duke@435 2590 // Find the first pc desc for all scopes in the code and print it.
duke@435 2591 ResourceMark rm;
duke@435 2592 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
duke@435 2593 if (p->scope_decode_offset() == DebugInformationRecorder::serialized_null)
duke@435 2594 continue;
duke@435 2595
duke@435 2596 ScopeDesc* sd = scope_desc_at(p->real_pc(this));
duke@435 2597 sd->print_on(tty, p);
duke@435 2598 }
duke@435 2599 }
duke@435 2600
duke@435 2601 void nmethod::print_dependencies() {
duke@435 2602 ResourceMark rm;
duke@435 2603 ttyLocker ttyl; // keep the following output all in one block
duke@435 2604 tty->print_cr("Dependencies:");
duke@435 2605 for (Dependencies::DepStream deps(this); deps.next(); ) {
duke@435 2606 deps.print_dependency();
coleenp@4037 2607 Klass* ctxk = deps.context_type();
duke@435 2608 if (ctxk != NULL) {
hseigel@4278 2609 if (ctxk->oop_is_instance() && ((InstanceKlass*)ctxk)->is_dependent_nmethod(this)) {
hseigel@4278 2610 tty->print_cr(" [nmethod<=klass]%s", ctxk->external_name());
duke@435 2611 }
duke@435 2612 }
duke@435 2613 deps.log_dependency(); // put it into the xml log also
duke@435 2614 }
duke@435 2615 }
duke@435 2616
duke@435 2617
duke@435 2618 void nmethod::print_relocations() {
duke@435 2619 ResourceMark m; // in case methods get printed via the debugger
duke@435 2620 tty->print_cr("relocations:");
duke@435 2621 RelocIterator iter(this);
duke@435 2622 iter.print();
duke@435 2623 if (UseRelocIndex) {
duke@435 2624 jint* index_end = (jint*)relocation_end() - 1;
duke@435 2625 jint index_size = *index_end;
duke@435 2626 jint* index_start = (jint*)( (address)index_end - index_size );
duke@435 2627 tty->print_cr(" index @" INTPTR_FORMAT ": index_size=%d", index_start, index_size);
duke@435 2628 if (index_size > 0) {
duke@435 2629 jint* ip;
duke@435 2630 for (ip = index_start; ip+2 <= index_end; ip += 2)
duke@435 2631 tty->print_cr(" (%d %d) addr=" INTPTR_FORMAT " @" INTPTR_FORMAT,
duke@435 2632 ip[0],
duke@435 2633 ip[1],
duke@435 2634 header_end()+ip[0],
duke@435 2635 relocation_begin()-1+ip[1]);
duke@435 2636 for (; ip < index_end; ip++)
duke@435 2637 tty->print_cr(" (%d ?)", ip[0]);
ccheung@5259 2638 tty->print_cr(" @" INTPTR_FORMAT ": index_size=%d", ip, *ip);
ccheung@5259 2639 ip++;
duke@435 2640 tty->print_cr("reloc_end @" INTPTR_FORMAT ":", ip);
duke@435 2641 }
duke@435 2642 }
duke@435 2643 }
duke@435 2644
duke@435 2645
duke@435 2646 void nmethod::print_pcs() {
duke@435 2647 ResourceMark m; // in case methods get printed via debugger
duke@435 2648 tty->print_cr("pc-bytecode offsets:");
duke@435 2649 for (PcDesc* p = scopes_pcs_begin(); p < scopes_pcs_end(); p++) {
duke@435 2650 p->print(this);
duke@435 2651 }
duke@435 2652 }
duke@435 2653
jrose@535 2654 #endif // PRODUCT
duke@435 2655
duke@435 2656 const char* nmethod::reloc_string_for(u_char* begin, u_char* end) {
duke@435 2657 RelocIterator iter(this, begin, end);
duke@435 2658 bool have_one = false;
duke@435 2659 while (iter.next()) {
duke@435 2660 have_one = true;
duke@435 2661 switch (iter.type()) {
duke@435 2662 case relocInfo::none: return "no_reloc";
duke@435 2663 case relocInfo::oop_type: {
duke@435 2664 stringStream st;
duke@435 2665 oop_Relocation* r = iter.oop_reloc();
duke@435 2666 oop obj = r->oop_value();
duke@435 2667 st.print("oop(");
duke@435 2668 if (obj == NULL) st.print("NULL");
duke@435 2669 else obj->print_value_on(&st);
duke@435 2670 st.print(")");
duke@435 2671 return st.as_string();
duke@435 2672 }
coleenp@4037 2673 case relocInfo::metadata_type: {
coleenp@4037 2674 stringStream st;
coleenp@4037 2675 metadata_Relocation* r = iter.metadata_reloc();
coleenp@4037 2676 Metadata* obj = r->metadata_value();
coleenp@4037 2677 st.print("metadata(");
coleenp@4037 2678 if (obj == NULL) st.print("NULL");
coleenp@4037 2679 else obj->print_value_on(&st);
coleenp@4037 2680 st.print(")");
coleenp@4037 2681 return st.as_string();
coleenp@4037 2682 }
duke@435 2683 case relocInfo::virtual_call_type: return "virtual_call";
duke@435 2684 case relocInfo::opt_virtual_call_type: return "optimized virtual_call";
duke@435 2685 case relocInfo::static_call_type: return "static_call";
duke@435 2686 case relocInfo::static_stub_type: return "static_stub";
duke@435 2687 case relocInfo::runtime_call_type: return "runtime_call";
duke@435 2688 case relocInfo::external_word_type: return "external_word";
duke@435 2689 case relocInfo::internal_word_type: return "internal_word";
duke@435 2690 case relocInfo::section_word_type: return "section_word";
duke@435 2691 case relocInfo::poll_type: return "poll";
duke@435 2692 case relocInfo::poll_return_type: return "poll_return";
duke@435 2693 case relocInfo::type_mask: return "type_bit_mask";
duke@435 2694 }
duke@435 2695 }
duke@435 2696 return have_one ? "other" : NULL;
duke@435 2697 }
duke@435 2698
duke@435 2699 // Return a the last scope in (begin..end]
duke@435 2700 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
duke@435 2701 PcDesc* p = pc_desc_near(begin+1);
duke@435 2702 if (p != NULL && p->real_pc(this) <= end) {
duke@435 2703 return new ScopeDesc(this, p->scope_decode_offset(),
kvn@1688 2704 p->obj_decode_offset(), p->should_reexecute(),
kvn@1688 2705 p->return_oop());
duke@435 2706 }
duke@435 2707 return NULL;
duke@435 2708 }
duke@435 2709
kvn@4107 2710 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin) const {
jrose@1590 2711 if (block_begin == entry_point()) stream->print_cr("[Entry Point]");
jrose@1590 2712 if (block_begin == verified_entry_point()) stream->print_cr("[Verified Entry Point]");
jrose@1590 2713 if (block_begin == exception_begin()) stream->print_cr("[Exception Handler]");
jrose@1590 2714 if (block_begin == stub_begin()) stream->print_cr("[Stub Code]");
twisti@1639 2715 if (block_begin == deopt_handler_begin()) stream->print_cr("[Deopt Handler Code]");
twisti@2046 2716
twisti@2046 2717 if (has_method_handle_invokes())
twisti@2046 2718 if (block_begin == deopt_mh_handler_begin()) stream->print_cr("[Deopt MH Handler Code]");
twisti@2046 2719
jrose@1590 2720 if (block_begin == consts_begin()) stream->print_cr("[Constants]");
twisti@2046 2721
jrose@1590 2722 if (block_begin == entry_point()) {
jrose@1590 2723 methodHandle m = method();
jrose@1590 2724 if (m.not_null()) {
jrose@1590 2725 stream->print(" # ");
jrose@1590 2726 m->print_value_on(stream);
jrose@1590 2727 stream->cr();
jrose@1590 2728 }
jrose@1590 2729 if (m.not_null() && !is_osr_method()) {
jrose@1590 2730 ResourceMark rm;
jrose@1590 2731 int sizeargs = m->size_of_parameters();
jrose@1590 2732 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
jrose@1590 2733 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
jrose@1590 2734 {
jrose@1590 2735 int sig_index = 0;
jrose@1590 2736 if (!m->is_static())
jrose@1590 2737 sig_bt[sig_index++] = T_OBJECT; // 'this'
jrose@1590 2738 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
jrose@1590 2739 BasicType t = ss.type();
jrose@1590 2740 sig_bt[sig_index++] = t;
jrose@1590 2741 if (type2size[t] == 2) {
jrose@1590 2742 sig_bt[sig_index++] = T_VOID;
jrose@1590 2743 } else {
jrose@1590 2744 assert(type2size[t] == 1, "size is 1 or 2");
jrose@1590 2745 }
jrose@1590 2746 }
jrose@1590 2747 assert(sig_index == sizeargs, "");
jrose@1590 2748 }
jrose@1590 2749 const char* spname = "sp"; // make arch-specific?
jrose@1590 2750 intptr_t out_preserve = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs, false);
jrose@1590 2751 int stack_slot_offset = this->frame_size() * wordSize;
jrose@1590 2752 int tab1 = 14, tab2 = 24;
jrose@1590 2753 int sig_index = 0;
jrose@1590 2754 int arg_index = (m->is_static() ? 0 : -1);
jrose@1590 2755 bool did_old_sp = false;
jrose@1590 2756 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
jrose@1590 2757 bool at_this = (arg_index == -1);
jrose@1590 2758 bool at_old_sp = false;
jrose@1590 2759 BasicType t = (at_this ? T_OBJECT : ss.type());
jrose@1590 2760 assert(t == sig_bt[sig_index], "sigs in sync");
jrose@1590 2761 if (at_this)
jrose@1590 2762 stream->print(" # this: ");
jrose@1590 2763 else
jrose@1590 2764 stream->print(" # parm%d: ", arg_index);
jrose@1590 2765 stream->move_to(tab1);
jrose@1590 2766 VMReg fst = regs[sig_index].first();
jrose@1590 2767 VMReg snd = regs[sig_index].second();
jrose@1590 2768 if (fst->is_reg()) {
jrose@1590 2769 stream->print("%s", fst->name());
jrose@1590 2770 if (snd->is_valid()) {
jrose@1590 2771 stream->print(":%s", snd->name());
jrose@1590 2772 }
jrose@1590 2773 } else if (fst->is_stack()) {
jrose@1590 2774 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
jrose@1590 2775 if (offset == stack_slot_offset) at_old_sp = true;
jrose@1590 2776 stream->print("[%s+0x%x]", spname, offset);
jrose@1590 2777 } else {
jrose@1590 2778 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
jrose@1590 2779 }
jrose@1590 2780 stream->print(" ");
jrose@1590 2781 stream->move_to(tab2);
jrose@1590 2782 stream->print("= ");
jrose@1590 2783 if (at_this) {
jrose@1590 2784 m->method_holder()->print_value_on(stream);
jrose@1590 2785 } else {
jrose@1590 2786 bool did_name = false;
jrose@1590 2787 if (!at_this && ss.is_object()) {
coleenp@2497 2788 Symbol* name = ss.as_symbol_or_null();
jrose@1590 2789 if (name != NULL) {
jrose@1590 2790 name->print_value_on(stream);
jrose@1590 2791 did_name = true;
jrose@1590 2792 }
jrose@1590 2793 }
jrose@1590 2794 if (!did_name)
jrose@1590 2795 stream->print("%s", type2name(t));
jrose@1590 2796 }
jrose@1590 2797 if (at_old_sp) {
jrose@1590 2798 stream->print(" (%s of caller)", spname);
jrose@1590 2799 did_old_sp = true;
jrose@1590 2800 }
jrose@1590 2801 stream->cr();
jrose@1590 2802 sig_index += type2size[t];
jrose@1590 2803 arg_index += 1;
jrose@1590 2804 if (!at_this) ss.next();
jrose@1590 2805 }
jrose@1590 2806 if (!did_old_sp) {
jrose@1590 2807 stream->print(" # ");
jrose@1590 2808 stream->move_to(tab1);
jrose@1590 2809 stream->print("[%s+0x%x]", spname, stack_slot_offset);
jrose@1590 2810 stream->print(" (%s of caller)", spname);
jrose@1590 2811 stream->cr();
jrose@1590 2812 }
jrose@1590 2813 }
jrose@1590 2814 }
jrose@1590 2815 }
jrose@1590 2816
duke@435 2817 void nmethod::print_code_comment_on(outputStream* st, int column, u_char* begin, u_char* end) {
duke@435 2818 // First, find an oopmap in (begin, end].
duke@435 2819 // We use the odd half-closed interval so that oop maps and scope descs
duke@435 2820 // which are tied to the byte after a call are printed with the call itself.
twisti@2103 2821 address base = code_begin();
duke@435 2822 OopMapSet* oms = oop_maps();
duke@435 2823 if (oms != NULL) {
duke@435 2824 for (int i = 0, imax = oms->size(); i < imax; i++) {
duke@435 2825 OopMap* om = oms->at(i);
duke@435 2826 address pc = base + om->offset();
duke@435 2827 if (pc > begin) {
duke@435 2828 if (pc <= end) {
jrose@535 2829 st->move_to(column);
jrose@535 2830 st->print("; ");
jrose@535 2831 om->print_on(st);
duke@435 2832 }
duke@435 2833 break;
duke@435 2834 }
duke@435 2835 }
duke@435 2836 }
jrose@535 2837
jrose@535 2838 // Print any debug info present at this pc.
duke@435 2839 ScopeDesc* sd = scope_desc_in(begin, end);
duke@435 2840 if (sd != NULL) {
jrose@535 2841 st->move_to(column);
duke@435 2842 if (sd->bci() == SynchronizationEntryBCI) {
duke@435 2843 st->print(";*synchronization entry");
duke@435 2844 } else {
coleenp@4037 2845 if (sd->method() == NULL) {
jrose@535 2846 st->print("method is NULL");
duke@435 2847 } else if (sd->method()->is_native()) {
jrose@535 2848 st->print("method is native");
duke@435 2849 } else {
never@2462 2850 Bytecodes::Code bc = sd->method()->java_code_at(sd->bci());
duke@435 2851 st->print(";*%s", Bytecodes::name(bc));
duke@435 2852 switch (bc) {
duke@435 2853 case Bytecodes::_invokevirtual:
duke@435 2854 case Bytecodes::_invokespecial:
duke@435 2855 case Bytecodes::_invokestatic:
duke@435 2856 case Bytecodes::_invokeinterface:
duke@435 2857 {
never@2462 2858 Bytecode_invoke invoke(sd->method(), sd->bci());
duke@435 2859 st->print(" ");
never@2462 2860 if (invoke.name() != NULL)
never@2462 2861 invoke.name()->print_symbol_on(st);
duke@435 2862 else
duke@435 2863 st->print("<UNKNOWN>");
duke@435 2864 break;
duke@435 2865 }
duke@435 2866 case Bytecodes::_getfield:
duke@435 2867 case Bytecodes::_putfield:
duke@435 2868 case Bytecodes::_getstatic:
duke@435 2869 case Bytecodes::_putstatic:
duke@435 2870 {
never@2462 2871 Bytecode_field field(sd->method(), sd->bci());
duke@435 2872 st->print(" ");
never@2462 2873 if (field.name() != NULL)
never@2462 2874 field.name()->print_symbol_on(st);
duke@435 2875 else
duke@435 2876 st->print("<UNKNOWN>");
duke@435 2877 }
duke@435 2878 }
duke@435 2879 }
duke@435 2880 }
jrose@535 2881
duke@435 2882 // Print all scopes
duke@435 2883 for (;sd != NULL; sd = sd->sender()) {
jrose@535 2884 st->move_to(column);
duke@435 2885 st->print("; -");
coleenp@4037 2886 if (sd->method() == NULL) {
jrose@535 2887 st->print("method is NULL");
duke@435 2888 } else {
duke@435 2889 sd->method()->print_short_name(st);
duke@435 2890 }
duke@435 2891 int lineno = sd->method()->line_number_from_bci(sd->bci());
duke@435 2892 if (lineno != -1) {
duke@435 2893 st->print("@%d (line %d)", sd->bci(), lineno);
duke@435 2894 } else {
duke@435 2895 st->print("@%d", sd->bci());
duke@435 2896 }
duke@435 2897 st->cr();
duke@435 2898 }
duke@435 2899 }
duke@435 2900
duke@435 2901 // Print relocation information
duke@435 2902 const char* str = reloc_string_for(begin, end);
duke@435 2903 if (str != NULL) {
duke@435 2904 if (sd != NULL) st->cr();
jrose@535 2905 st->move_to(column);
duke@435 2906 st->print("; {%s}", str);
duke@435 2907 }
twisti@2103 2908 int cont_offset = ImplicitExceptionTable(this).at(begin - code_begin());
duke@435 2909 if (cont_offset != 0) {
jrose@535 2910 st->move_to(column);
twisti@2103 2911 st->print("; implicit exception: dispatches to " INTPTR_FORMAT, code_begin() + cont_offset);
duke@435 2912 }
duke@435 2913
duke@435 2914 }
duke@435 2915
jrose@535 2916 #ifndef PRODUCT
jrose@535 2917
duke@435 2918 void nmethod::print_value_on(outputStream* st) const {
twisti@2687 2919 st->print("nmethod");
twisti@2687 2920 print_on(st, NULL);
duke@435 2921 }
duke@435 2922
duke@435 2923 void nmethod::print_calls(outputStream* st) {
duke@435 2924 RelocIterator iter(this);
duke@435 2925 while (iter.next()) {
duke@435 2926 switch (iter.type()) {
duke@435 2927 case relocInfo::virtual_call_type:
duke@435 2928 case relocInfo::opt_virtual_call_type: {
duke@435 2929 VerifyMutexLocker mc(CompiledIC_lock);
duke@435 2930 CompiledIC_at(iter.reloc())->print();
duke@435 2931 break;
duke@435 2932 }
duke@435 2933 case relocInfo::static_call_type:
duke@435 2934 st->print_cr("Static call at " INTPTR_FORMAT, iter.reloc()->addr());
duke@435 2935 compiledStaticCall_at(iter.reloc())->print();
duke@435 2936 break;
duke@435 2937 }
duke@435 2938 }
duke@435 2939 }
duke@435 2940
duke@435 2941 void nmethod::print_handler_table() {
duke@435 2942 ExceptionHandlerTable(this).print();
duke@435 2943 }
duke@435 2944
duke@435 2945 void nmethod::print_nul_chk_table() {
twisti@2103 2946 ImplicitExceptionTable(this).print(code_begin());
duke@435 2947 }
duke@435 2948
duke@435 2949 void nmethod::print_statistics() {
duke@435 2950 ttyLocker ttyl;
duke@435 2951 if (xtty != NULL) xtty->head("statistics type='nmethod'");
duke@435 2952 nmethod_stats.print_native_nmethod_stats();
duke@435 2953 nmethod_stats.print_nmethod_stats();
duke@435 2954 DebugInformationRecorder::print_statistics();
duke@435 2955 nmethod_stats.print_pc_stats();
duke@435 2956 Dependencies::print_statistics();
duke@435 2957 if (xtty != NULL) xtty->tail("statistics");
duke@435 2958 }
duke@435 2959
duke@435 2960 #endif // PRODUCT

mercurial