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