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