Wed, 14 Oct 2020 17:44:48 +0800
Merge
1 /*
2 * Copyright (c) 1997, 2014, 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 */
25 /*
26 * This file has been modified by Loongson Technology in 2015. These
27 * modifications are Copyright (c) 2015 Loongson Technology, and are made
28 * available on the same license terms set forth above.
29 */
31 #include "precompiled.hpp"
32 #include "compiler/abstractCompiler.hpp"
33 #include "compiler/disassembler.hpp"
34 #include "gc_interface/collectedHeap.inline.hpp"
35 #include "interpreter/interpreter.hpp"
36 #include "interpreter/oopMapCache.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "memory/universe.inline.hpp"
39 #include "oops/markOop.hpp"
40 #include "oops/methodData.hpp"
41 #include "oops/method.hpp"
42 #include "oops/oop.inline.hpp"
43 #include "oops/oop.inline2.hpp"
44 #include "prims/methodHandles.hpp"
45 #include "runtime/frame.inline.hpp"
46 #include "runtime/handles.inline.hpp"
47 #include "runtime/javaCalls.hpp"
48 #include "runtime/monitorChunk.hpp"
49 #include "runtime/sharedRuntime.hpp"
50 #include "runtime/signature.hpp"
51 #include "runtime/stubCodeGenerator.hpp"
52 #include "runtime/stubRoutines.hpp"
53 #include "utilities/decoder.hpp"
55 #ifdef TARGET_ARCH_x86
56 # include "nativeInst_x86.hpp"
57 #endif
58 #ifdef TARGET_ARCH_sparc
59 # include "nativeInst_sparc.hpp"
60 #endif
61 #ifdef TARGET_ARCH_zero
62 # include "nativeInst_zero.hpp"
63 #endif
64 #ifdef TARGET_ARCH_arm
65 # include "nativeInst_arm.hpp"
66 #endif
67 #ifdef TARGET_ARCH_ppc
68 # include "nativeInst_ppc.hpp"
69 #endif
70 #ifdef TARGET_ARCH_mips
71 # include "nativeInst_mips.hpp"
72 #endif
75 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
77 RegisterMap::RegisterMap(JavaThread *thread, bool update_map) {
78 _thread = thread;
79 _update_map = update_map;
80 clear();
81 debug_only(_update_for_id = NULL;)
82 #ifndef PRODUCT
83 for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
84 #endif /* PRODUCT */
85 }
87 RegisterMap::RegisterMap(const RegisterMap* map) {
88 assert(map != this, "bad initialization parameter");
89 assert(map != NULL, "RegisterMap must be present");
90 _thread = map->thread();
91 _update_map = map->update_map();
92 _include_argument_oops = map->include_argument_oops();
93 debug_only(_update_for_id = map->_update_for_id;)
94 pd_initialize_from(map);
95 if (update_map()) {
96 for(int i = 0; i < location_valid_size; i++) {
97 LocationValidType bits = !update_map() ? 0 : map->_location_valid[i];
98 _location_valid[i] = bits;
99 // for whichever bits are set, pull in the corresponding map->_location
100 int j = i*location_valid_type_size;
101 while (bits != 0) {
102 if ((bits & 1) != 0) {
103 assert(0 <= j && j < reg_count, "range check");
104 _location[j] = map->_location[j];
105 }
106 bits >>= 1;
107 j += 1;
108 }
109 }
110 }
111 }
113 void RegisterMap::clear() {
114 set_include_argument_oops(true);
115 if (_update_map) {
116 for(int i = 0; i < location_valid_size; i++) {
117 _location_valid[i] = 0;
118 }
119 pd_clear();
120 } else {
121 pd_initialize();
122 }
123 }
125 #ifndef PRODUCT
127 void RegisterMap::print_on(outputStream* st) const {
128 st->print_cr("Register map");
129 for(int i = 0; i < reg_count; i++) {
131 VMReg r = VMRegImpl::as_VMReg(i);
132 intptr_t* src = (intptr_t*) location(r);
133 if (src != NULL) {
135 r->print_on(st);
136 st->print(" [" INTPTR_FORMAT "] = ", src);
137 if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
138 st->print_cr("<misaligned>");
139 } else {
140 st->print_cr(INTPTR_FORMAT, *src);
141 }
142 }
143 }
144 }
146 void RegisterMap::print() const {
147 print_on(tty);
148 }
150 #endif
151 // This returns the pc that if you were in the debugger you'd see. Not
152 // the idealized value in the frame object. This undoes the magic conversion
153 // that happens for deoptimized frames. In addition it makes the value the
154 // hardware would want to see in the native frame. The only user (at this point)
155 // is deoptimization. It likely no one else should ever use it.
157 address frame::raw_pc() const {
158 if (is_deoptimized_frame()) {
159 nmethod* nm = cb()->as_nmethod_or_null();
160 if (nm->is_method_handle_return(pc()))
161 return nm->deopt_mh_handler_begin() - pc_return_offset;
162 else
163 return nm->deopt_handler_begin() - pc_return_offset;
164 } else {
165 return (pc() - pc_return_offset);
166 }
167 }
169 // Change the pc in a frame object. This does not change the actual pc in
170 // actual frame. To do that use patch_pc.
171 //
172 void frame::set_pc(address newpc ) {
173 #ifdef ASSERT
174 if (_cb != NULL && _cb->is_nmethod()) {
175 assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
176 }
177 #endif // ASSERT
179 // Unsafe to use the is_deoptimzed tester after changing pc
180 _deopt_state = unknown;
181 _pc = newpc;
182 _cb = CodeCache::find_blob_unsafe(_pc);
184 }
186 // type testers
187 bool frame::is_ignored_frame() const {
188 return false; // FIXME: some LambdaForm frames should be ignored
189 }
190 bool frame::is_deoptimized_frame() const {
191 assert(_deopt_state != unknown, "not answerable");
192 return _deopt_state == is_deoptimized;
193 }
195 bool frame::is_native_frame() const {
196 return (_cb != NULL &&
197 _cb->is_nmethod() &&
198 ((nmethod*)_cb)->is_native_method());
199 }
201 bool frame::is_java_frame() const {
202 if (is_interpreted_frame()) return true;
203 if (is_compiled_frame()) return true;
204 return false;
205 }
208 bool frame::is_compiled_frame() const {
209 if (_cb != NULL &&
210 _cb->is_nmethod() &&
211 ((nmethod*)_cb)->is_java_method()) {
212 return true;
213 }
214 return false;
215 }
218 bool frame::is_runtime_frame() const {
219 return (_cb != NULL && _cb->is_runtime_stub());
220 }
222 bool frame::is_safepoint_blob_frame() const {
223 return (_cb != NULL && _cb->is_safepoint_stub());
224 }
226 // testers
228 bool frame::is_first_java_frame() const {
229 RegisterMap map(JavaThread::current(), false); // No update
230 frame s;
231 for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));
232 return s.is_first_frame();
233 }
236 bool frame::entry_frame_is_first() const {
237 return entry_frame_call_wrapper()->is_first_frame();
238 }
240 JavaCallWrapper* frame::entry_frame_call_wrapper_if_safe(JavaThread* thread) const {
241 JavaCallWrapper** jcw = entry_frame_call_wrapper_addr();
242 address addr = (address) jcw;
244 // addr must be within the usable part of the stack
245 if (thread->is_in_usable_stack(addr)) {
246 return *jcw;
247 }
249 return NULL;
250 }
252 bool frame::is_entry_frame_valid(JavaThread* thread) const {
253 // Validate the JavaCallWrapper an entry frame must have
254 address jcw = (address)entry_frame_call_wrapper();
255 bool jcw_safe = (jcw < thread->stack_base()) && (jcw > (address)fp()); // less than stack base
256 if (!jcw_safe) {
257 return false;
258 }
260 // Validate sp saved in the java frame anchor
261 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
262 return (jfa->last_Java_sp() > sp());
263 }
265 bool frame::should_be_deoptimized() const {
266 if (_deopt_state == is_deoptimized ||
267 !is_compiled_frame() ) return false;
268 assert(_cb != NULL && _cb->is_nmethod(), "must be an nmethod");
269 nmethod* nm = (nmethod *)_cb;
270 if (TraceDependencies) {
271 tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
272 nm->print_value_on(tty);
273 tty->cr();
274 }
276 if( !nm->is_marked_for_deoptimization() )
277 return false;
279 // If at the return point, then the frame has already been popped, and
280 // only the return needs to be executed. Don't deoptimize here.
281 return !nm->is_at_poll_return(pc());
282 }
284 bool frame::can_be_deoptimized() const {
285 if (!is_compiled_frame()) return false;
286 nmethod* nm = (nmethod*)_cb;
288 if( !nm->can_be_deoptimized() )
289 return false;
291 return !nm->is_at_poll_return(pc());
292 }
294 void frame::deoptimize(JavaThread* thread) {
295 // Schedule deoptimization of an nmethod activation with this frame.
296 assert(_cb != NULL && _cb->is_nmethod(), "must be");
297 nmethod* nm = (nmethod*)_cb;
299 // This is a fix for register window patching race
300 if (NeedsDeoptSuspend && Thread::current() != thread) {
301 assert(SafepointSynchronize::is_at_safepoint(),
302 "patching other threads for deopt may only occur at a safepoint");
304 // It is possible especially with DeoptimizeALot/DeoptimizeRandom that
305 // we could see the frame again and ask for it to be deoptimized since
306 // it might move for a long time. That is harmless and we just ignore it.
307 if (id() == thread->must_deopt_id()) {
308 assert(thread->is_deopt_suspend(), "lost suspension");
309 return;
310 }
312 // We are at a safepoint so the target thread can only be
313 // in 4 states:
314 // blocked - no problem
315 // blocked_trans - no problem (i.e. could have woken up from blocked
316 // during a safepoint).
317 // native - register window pc patching race
318 // native_trans - momentary state
319 //
320 // We could just wait out a thread in native_trans to block.
321 // Then we'd have all the issues that the safepoint code has as to
322 // whether to spin or block. It isn't worth it. Just treat it like
323 // native and be done with it.
324 //
325 // Examine the state of the thread at the start of safepoint since
326 // threads that were in native at the start of the safepoint could
327 // come to a halt during the safepoint, changing the current value
328 // of the safepoint_state.
329 JavaThreadState state = thread->safepoint_state()->orig_thread_state();
330 if (state == _thread_in_native || state == _thread_in_native_trans) {
331 // Since we are at a safepoint the target thread will stop itself
332 // before it can return to java as long as we remain at the safepoint.
333 // Therefore we can put an additional request for the thread to stop
334 // no matter what no (like a suspend). This will cause the thread
335 // to notice it needs to do the deopt on its own once it leaves native.
336 //
337 // The only reason we must do this is because on machine with register
338 // windows we have a race with patching the return address and the
339 // window coming live as the thread returns to the Java code (but still
340 // in native mode) and then blocks. It is only this top most frame
341 // that is at risk. So in truth we could add an additional check to
342 // see if this frame is one that is at risk.
343 RegisterMap map(thread, false);
344 frame at_risk = thread->last_frame().sender(&map);
345 if (id() == at_risk.id()) {
346 thread->set_must_deopt_id(id());
347 thread->set_deopt_suspend();
348 return;
349 }
350 }
351 } // NeedsDeoptSuspend
354 // If the call site is a MethodHandle call site use the MH deopt
355 // handler.
356 address deopt = nm->is_method_handle_return(pc()) ?
357 nm->deopt_mh_handler_begin() :
358 nm->deopt_handler_begin();
360 // Save the original pc before we patch in the new one
361 nm->set_original_pc(this, pc());
362 patch_pc(thread, deopt);
364 #ifdef ASSERT
365 {
366 RegisterMap map(thread, false);
367 frame check = thread->last_frame();
368 while (id() != check.id()) {
369 check = check.sender(&map);
370 }
371 assert(check.is_deoptimized_frame(), "missed deopt");
372 }
373 #endif // ASSERT
374 }
376 frame frame::java_sender() const {
377 RegisterMap map(JavaThread::current(), false);
378 frame s;
379 for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
380 guarantee(s.is_java_frame(), "tried to get caller of first java frame");
381 return s;
382 }
384 frame frame::real_sender(RegisterMap* map) const {
385 frame result = sender(map);
386 while (result.is_runtime_frame() ||
387 result.is_ignored_frame()) {
388 result = result.sender(map);
389 }
390 return result;
391 }
393 // Note: called by profiler - NOT for current thread
394 frame frame::profile_find_Java_sender_frame(JavaThread *thread) {
395 // If we don't recognize this frame, walk back up the stack until we do
396 RegisterMap map(thread, false);
397 frame first_java_frame = frame();
399 // Find the first Java frame on the stack starting with input frame
400 if (is_java_frame()) {
401 // top frame is compiled frame or deoptimized frame
402 first_java_frame = *this;
403 } else if (safe_for_sender(thread)) {
404 for (frame sender_frame = sender(&map);
405 sender_frame.safe_for_sender(thread) && !sender_frame.is_first_frame();
406 sender_frame = sender_frame.sender(&map)) {
407 if (sender_frame.is_java_frame()) {
408 first_java_frame = sender_frame;
409 break;
410 }
411 }
412 }
413 return first_java_frame;
414 }
416 // Interpreter frames
419 void frame::interpreter_frame_set_locals(intptr_t* locs) {
420 assert(is_interpreted_frame(), "Not an interpreted frame");
421 *interpreter_frame_locals_addr() = locs;
422 }
424 Method* frame::interpreter_frame_method() const {
425 assert(is_interpreted_frame(), "interpreted frame expected");
426 Method* m = *interpreter_frame_method_addr();
427 assert(m->is_method(), "not a Method*");
428 return m;
429 }
431 void frame::interpreter_frame_set_method(Method* method) {
432 assert(is_interpreted_frame(), "interpreted frame expected");
433 *interpreter_frame_method_addr() = method;
434 }
436 void frame::interpreter_frame_set_bcx(intptr_t bcx) {
437 assert(is_interpreted_frame(), "Not an interpreted frame");
438 if (ProfileInterpreter) {
439 bool formerly_bci = is_bci(interpreter_frame_bcx());
440 bool is_now_bci = is_bci(bcx);
441 *interpreter_frame_bcx_addr() = bcx;
443 intptr_t mdx = interpreter_frame_mdx();
445 if (mdx != 0) {
446 if (formerly_bci) {
447 if (!is_now_bci) {
448 // The bcx was just converted from bci to bcp.
449 // Convert the mdx in parallel.
450 MethodData* mdo = interpreter_frame_method()->method_data();
451 assert(mdo != NULL, "");
452 int mdi = mdx - 1; // We distinguish valid mdi from zero by adding one.
453 address mdp = mdo->di_to_dp(mdi);
454 interpreter_frame_set_mdx((intptr_t)mdp);
455 }
456 } else {
457 if (is_now_bci) {
458 // The bcx was just converted from bcp to bci.
459 // Convert the mdx in parallel.
460 MethodData* mdo = interpreter_frame_method()->method_data();
461 assert(mdo != NULL, "");
462 int mdi = mdo->dp_to_di((address)mdx);
463 interpreter_frame_set_mdx((intptr_t)mdi + 1); // distinguish valid from 0.
464 }
465 }
466 }
467 } else {
468 *interpreter_frame_bcx_addr() = bcx;
469 }
470 }
472 jint frame::interpreter_frame_bci() const {
473 assert(is_interpreted_frame(), "interpreted frame expected");
474 intptr_t bcx = interpreter_frame_bcx();
475 return is_bci(bcx) ? bcx : interpreter_frame_method()->bci_from((address)bcx);
476 }
478 void frame::interpreter_frame_set_bci(jint bci) {
479 assert(is_interpreted_frame(), "interpreted frame expected");
480 assert(!is_bci(interpreter_frame_bcx()), "should not set bci during GC");
481 interpreter_frame_set_bcx((intptr_t)interpreter_frame_method()->bcp_from(bci));
482 }
484 address frame::interpreter_frame_bcp() const {
485 assert(is_interpreted_frame(), "interpreted frame expected");
486 intptr_t bcx = interpreter_frame_bcx();
487 return is_bci(bcx) ? interpreter_frame_method()->bcp_from(bcx) : (address)bcx;
488 }
490 void frame::interpreter_frame_set_bcp(address bcp) {
491 assert(is_interpreted_frame(), "interpreted frame expected");
492 assert(!is_bci(interpreter_frame_bcx()), "should not set bcp during GC");
493 interpreter_frame_set_bcx((intptr_t)bcp);
494 }
496 void frame::interpreter_frame_set_mdx(intptr_t mdx) {
497 assert(is_interpreted_frame(), "Not an interpreted frame");
498 assert(ProfileInterpreter, "must be profiling interpreter");
499 *interpreter_frame_mdx_addr() = mdx;
500 }
502 address frame::interpreter_frame_mdp() const {
503 assert(ProfileInterpreter, "must be profiling interpreter");
504 assert(is_interpreted_frame(), "interpreted frame expected");
505 intptr_t bcx = interpreter_frame_bcx();
506 intptr_t mdx = interpreter_frame_mdx();
508 assert(!is_bci(bcx), "should not access mdp during GC");
509 return (address)mdx;
510 }
512 void frame::interpreter_frame_set_mdp(address mdp) {
513 assert(is_interpreted_frame(), "interpreted frame expected");
514 if (mdp == NULL) {
515 // Always allow the mdp to be cleared.
516 interpreter_frame_set_mdx((intptr_t)mdp);
517 }
518 intptr_t bcx = interpreter_frame_bcx();
519 assert(!is_bci(bcx), "should not set mdp during GC");
520 interpreter_frame_set_mdx((intptr_t)mdp);
521 }
523 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
524 assert(is_interpreted_frame(), "Not an interpreted frame");
525 #ifdef ASSERT
526 interpreter_frame_verify_monitor(current);
527 #endif
528 BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
529 return next;
530 }
532 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
533 assert(is_interpreted_frame(), "Not an interpreted frame");
534 #ifdef ASSERT
535 // // This verification needs to be checked before being enabled
536 // interpreter_frame_verify_monitor(current);
537 #endif
538 BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
539 return previous;
540 }
542 // Interpreter locals and expression stack locations.
544 intptr_t* frame::interpreter_frame_local_at(int index) const {
545 const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
546 return &((*interpreter_frame_locals_addr())[n]);
547 }
549 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
550 const int i = offset * interpreter_frame_expression_stack_direction();
551 const int n = i * Interpreter::stackElementWords;
552 return &(interpreter_frame_expression_stack()[n]);
553 }
555 jint frame::interpreter_frame_expression_stack_size() const {
556 // Number of elements on the interpreter expression stack
557 // Callers should span by stackElementWords
558 int element_size = Interpreter::stackElementWords;
559 size_t stack_size = 0;
560 if (frame::interpreter_frame_expression_stack_direction() < 0) {
561 stack_size = (interpreter_frame_expression_stack() -
562 interpreter_frame_tos_address() + 1)/element_size;
563 } else {
564 stack_size = (interpreter_frame_tos_address() -
565 interpreter_frame_expression_stack() + 1)/element_size;
566 }
567 assert( stack_size <= (size_t)max_jint, "stack size too big");
568 return ((jint)stack_size);
569 }
572 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
574 const char* frame::print_name() const {
575 if (is_native_frame()) return "Native";
576 if (is_interpreted_frame()) return "Interpreted";
577 if (is_compiled_frame()) {
578 if (is_deoptimized_frame()) return "Deoptimized";
579 return "Compiled";
580 }
581 if (sp() == NULL) return "Empty";
582 return "C";
583 }
585 void frame::print_value_on(outputStream* st, JavaThread *thread) const {
586 NOT_PRODUCT(address begin = pc()-40;)
587 NOT_PRODUCT(address end = NULL;)
589 st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), sp(), unextended_sp());
590 if (sp() != NULL)
591 st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT, fp(), real_fp(), pc());
593 if (StubRoutines::contains(pc())) {
594 st->print_cr(")");
595 st->print("(");
596 StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
597 st->print("~Stub::%s", desc->name());
598 NOT_PRODUCT(begin = desc->begin(); end = desc->end();)
599 } else if (Interpreter::contains(pc())) {
600 st->print_cr(")");
601 st->print("(");
602 InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
603 if (desc != NULL) {
604 st->print("~");
605 desc->print_on(st);
606 NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)
607 } else {
608 st->print("~interpreter");
609 }
610 }
611 st->print_cr(")");
613 if (_cb != NULL) {
614 st->print(" ");
615 _cb->print_value_on(st);
616 st->cr();
617 #ifndef PRODUCT
618 if (end == NULL) {
619 begin = _cb->code_begin();
620 end = _cb->code_end();
621 }
622 #endif
623 }
624 NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)
625 }
628 void frame::print_on(outputStream* st) const {
629 print_value_on(st,NULL);
630 if (is_interpreted_frame()) {
631 interpreter_frame_print_on(st);
632 }
633 }
636 void frame::interpreter_frame_print_on(outputStream* st) const {
637 #ifndef PRODUCT
638 assert(is_interpreted_frame(), "Not an interpreted frame");
639 jint i;
640 for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
641 intptr_t x = *interpreter_frame_local_at(i);
642 st->print(" - local [" INTPTR_FORMAT "]", x);
643 st->fill_to(23);
644 st->print_cr("; #%d", i);
645 }
646 for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
647 intptr_t x = *interpreter_frame_expression_stack_at(i);
648 st->print(" - stack [" INTPTR_FORMAT "]", x);
649 st->fill_to(23);
650 st->print_cr("; #%d", i);
651 }
652 // locks for synchronization
653 for (BasicObjectLock* current = interpreter_frame_monitor_end();
654 current < interpreter_frame_monitor_begin();
655 current = next_monitor_in_interpreter_frame(current)) {
656 st->print(" - obj [");
657 current->obj()->print_value_on(st);
658 st->print_cr("]");
659 st->print(" - lock [");
660 current->lock()->print_on(st);
661 st->print_cr("]");
662 }
663 // monitor
664 st->print_cr(" - monitor[" INTPTR_FORMAT "]", interpreter_frame_monitor_begin());
665 // bcp
666 st->print(" - bcp [" INTPTR_FORMAT "]", interpreter_frame_bcp());
667 st->fill_to(23);
668 st->print_cr("; @%d", interpreter_frame_bci());
669 // locals
670 st->print_cr(" - locals [" INTPTR_FORMAT "]", interpreter_frame_local_at(0));
671 // method
672 st->print(" - method [" INTPTR_FORMAT "]", (address)interpreter_frame_method());
673 st->fill_to(23);
674 st->print("; ");
675 interpreter_frame_method()->print_name(st);
676 st->cr();
677 #endif
678 }
680 // Return whether the frame is in the VM or os indicating a Hotspot problem.
681 // Otherwise, it's likely a bug in the native library that the Java code calls,
682 // hopefully indicating where to submit bugs.
683 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
684 // C/C++ frame
685 bool in_vm = os::address_is_in_vm(pc);
686 st->print(in_vm ? "V" : "C");
688 int offset;
689 bool found;
691 // libname
692 found = os::dll_address_to_library_name(pc, buf, buflen, &offset);
693 if (found) {
694 // skip directory names
695 const char *p1, *p2;
696 p1 = buf;
697 int len = (int)strlen(os::file_separator());
698 while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
699 st->print(" [%s+0x%x]", p1, offset);
700 } else {
701 st->print(" " PTR_FORMAT, pc);
702 }
704 // function name - os::dll_address_to_function_name() may return confusing
705 // names if pc is within jvm.dll or libjvm.so, because JVM only has
706 // JVM_xxxx and a few other symbols in the dynamic symbol table. Do this
707 // only for native libraries.
708 if (!in_vm || Decoder::can_decode_C_frame_in_vm()) {
709 found = os::dll_address_to_function_name(pc, buf, buflen, &offset);
711 if (found) {
712 st->print(" %s+0x%x", buf, offset);
713 }
714 }
715 }
717 // frame::print_on_error() is called by fatal error handler. Notice that we may
718 // crash inside this function if stack frame is corrupted. The fatal error
719 // handler can catch and handle the crash. Here we assume the frame is valid.
720 //
721 // First letter indicates type of the frame:
722 // J: Java frame (compiled)
723 // j: Java frame (interpreted)
724 // V: VM frame (C/C++)
725 // v: Other frames running VM generated code (e.g. stubs, adapters, etc.)
726 // C: C/C++ frame
727 //
728 // We don't need detailed frame type as that in frame::print_name(). "C"
729 // suggests the problem is in user lib; everything else is likely a VM bug.
731 void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {
732 if (_cb != NULL) {
733 if (Interpreter::contains(pc())) {
734 Method* m = this->interpreter_frame_method();
735 if (m != NULL) {
736 m->name_and_sig_as_C_string(buf, buflen);
737 st->print("j %s", buf);
738 st->print("+%d", this->interpreter_frame_bci());
739 } else {
740 st->print("j " PTR_FORMAT, pc());
741 }
742 } else if (StubRoutines::contains(pc())) {
743 StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
744 if (desc != NULL) {
745 st->print("v ~StubRoutines::%s", desc->name());
746 } else {
747 st->print("v ~StubRoutines::" PTR_FORMAT, pc());
748 }
749 } else if (_cb->is_buffer_blob()) {
750 st->print("v ~BufferBlob::%s", ((BufferBlob *)_cb)->name());
751 } else if (_cb->is_nmethod()) {
752 nmethod* nm = (nmethod*)_cb;
753 Method* m = nm->method();
754 if (m != NULL) {
755 m->name_and_sig_as_C_string(buf, buflen);
756 st->print("J %d%s %s %s (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+0x%x]",
757 nm->compile_id(), (nm->is_osr_method() ? "%" : ""),
758 ((nm->compiler() != NULL) ? nm->compiler()->name() : ""),
759 buf, m->code_size(), _pc, _cb->code_begin(), _pc - _cb->code_begin());
760 } else {
761 st->print("J " PTR_FORMAT, pc());
762 }
763 } else if (_cb->is_runtime_stub()) {
764 st->print("v ~RuntimeStub::%s", ((RuntimeStub *)_cb)->name());
765 } else if (_cb->is_deoptimization_stub()) {
766 st->print("v ~DeoptimizationBlob");
767 } else if (_cb->is_exception_stub()) {
768 st->print("v ~ExceptionBlob");
769 } else if (_cb->is_safepoint_stub()) {
770 st->print("v ~SafepointBlob");
771 } else {
772 st->print("v blob " PTR_FORMAT, pc());
773 }
774 } else {
775 print_C_frame(st, buf, buflen, pc());
776 }
777 }
780 /*
781 The interpreter_frame_expression_stack_at method in the case of SPARC needs the
782 max_stack value of the method in order to compute the expression stack address.
783 It uses the Method* in order to get the max_stack value but during GC this
784 Method* value saved on the frame is changed by reverse_and_push and hence cannot
785 be used. So we save the max_stack value in the FrameClosure object and pass it
786 down to the interpreter_frame_expression_stack_at method
787 */
788 class InterpreterFrameClosure : public OffsetClosure {
789 private:
790 frame* _fr;
791 OopClosure* _f;
792 int _max_locals;
793 int _max_stack;
795 public:
796 InterpreterFrameClosure(frame* fr, int max_locals, int max_stack,
797 OopClosure* f) {
798 _fr = fr;
799 _max_locals = max_locals;
800 _max_stack = max_stack;
801 _f = f;
802 }
804 void offset_do(int offset) {
805 oop* addr;
806 if (offset < _max_locals) {
807 addr = (oop*) _fr->interpreter_frame_local_at(offset);
808 assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
809 _f->do_oop(addr);
810 } else {
811 addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
812 // In case of exceptions, the expression stack is invalid and the esp will be reset to express
813 // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
814 bool in_stack;
815 if (frame::interpreter_frame_expression_stack_direction() > 0) {
816 in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
817 } else {
818 in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
819 }
820 if (in_stack) {
821 _f->do_oop(addr);
822 }
823 }
824 }
826 int max_locals() { return _max_locals; }
827 frame* fr() { return _fr; }
828 };
831 class InterpretedArgumentOopFinder: public SignatureInfo {
832 private:
833 OopClosure* _f; // Closure to invoke
834 int _offset; // TOS-relative offset, decremented with each argument
835 bool _has_receiver; // true if the callee has a receiver
836 frame* _fr;
838 void set(int size, BasicType type) {
839 _offset -= size;
840 if (type == T_OBJECT || type == T_ARRAY) oop_offset_do();
841 }
843 void oop_offset_do() {
844 oop* addr;
845 addr = (oop*)_fr->interpreter_frame_tos_at(_offset);
846 _f->do_oop(addr);
847 }
849 public:
850 InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, frame* fr, OopClosure* f) : SignatureInfo(signature), _has_receiver(has_receiver) {
851 // compute size of arguments
852 int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);
853 assert(!fr->is_interpreted_frame() ||
854 args_size <= fr->interpreter_frame_expression_stack_size(),
855 "args cannot be on stack anymore");
856 // initialize InterpretedArgumentOopFinder
857 _f = f;
858 _fr = fr;
859 _offset = args_size;
860 }
862 void oops_do() {
863 if (_has_receiver) {
864 --_offset;
865 oop_offset_do();
866 }
867 iterate_parameters();
868 }
869 };
872 // Entry frame has following form (n arguments)
873 // +-----------+
874 // sp -> | last arg |
875 // +-----------+
876 // : ::: :
877 // +-----------+
878 // (sp+n)->| first arg|
879 // +-----------+
883 // visits and GC's all the arguments in entry frame
884 class EntryFrameOopFinder: public SignatureInfo {
885 private:
886 bool _is_static;
887 int _offset;
888 frame* _fr;
889 OopClosure* _f;
891 void set(int size, BasicType type) {
892 assert (_offset >= 0, "illegal offset");
893 if (type == T_OBJECT || type == T_ARRAY) oop_at_offset_do(_offset);
894 _offset -= size;
895 }
897 void oop_at_offset_do(int offset) {
898 assert (offset >= 0, "illegal offset");
899 oop* addr = (oop*) _fr->entry_frame_argument_at(offset);
900 _f->do_oop(addr);
901 }
903 public:
904 EntryFrameOopFinder(frame* frame, Symbol* signature, bool is_static) : SignatureInfo(signature) {
905 _f = NULL; // will be set later
906 _fr = frame;
907 _is_static = is_static;
908 _offset = ArgumentSizeComputer(signature).size() - 1; // last parameter is at index 0
909 }
911 void arguments_do(OopClosure* f) {
912 _f = f;
913 if (!_is_static) oop_at_offset_do(_offset+1); // do the receiver
914 iterate_parameters();
915 }
917 };
919 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
920 ArgumentSizeComputer asc(signature);
921 int size = asc.size();
922 return (oop *)interpreter_frame_tos_at(size);
923 }
926 void frame::oops_interpreted_do(OopClosure* f, CLDClosure* cld_f,
927 const RegisterMap* map, bool query_oop_map_cache) {
928 assert(is_interpreted_frame(), "Not an interpreted frame");
929 assert(map != NULL, "map must be set");
930 Thread *thread = Thread::current();
931 methodHandle m (thread, interpreter_frame_method());
932 jint bci = interpreter_frame_bci();
934 assert(!Universe::heap()->is_in(m()),
935 "must be valid oop");
936 assert(m->is_method(), "checking frame value");
937 assert((m->is_native() && bci == 0) ||
938 (!m->is_native() && bci >= 0 && bci < m->code_size()),
939 "invalid bci value");
941 // Handle the monitor elements in the activation
942 for (
943 BasicObjectLock* current = interpreter_frame_monitor_end();
944 current < interpreter_frame_monitor_begin();
945 current = next_monitor_in_interpreter_frame(current)
946 ) {
947 #ifdef ASSERT
948 interpreter_frame_verify_monitor(current);
949 #endif
950 current->oops_do(f);
951 }
953 // process fixed part
954 if (cld_f != NULL) {
955 // The method pointer in the frame might be the only path to the method's
956 // klass, and the klass needs to be kept alive while executing. The GCs
957 // don't trace through method pointers, so typically in similar situations
958 // the mirror or the class loader of the klass are installed as a GC root.
959 // To minimze the overhead of doing that here, we ask the GC to pass down a
960 // closure that knows how to keep klasses alive given a ClassLoaderData.
961 cld_f->do_cld(m->method_holder()->class_loader_data());
962 }
964 if (m->is_native() PPC32_ONLY(&& m->is_static())) {
965 f->do_oop(interpreter_frame_temp_oop_addr());
966 }
968 int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
970 Symbol* signature = NULL;
971 bool has_receiver = false;
973 // Process a callee's arguments if we are at a call site
974 // (i.e., if we are at an invoke bytecode)
975 // This is used sometimes for calling into the VM, not for another
976 // interpreted or compiled frame.
977 if (!m->is_native()) {
978 Bytecode_invoke call = Bytecode_invoke_check(m, bci);
979 if (call.is_valid()) {
980 signature = call.signature();
981 has_receiver = call.has_receiver();
982 if (map->include_argument_oops() &&
983 interpreter_frame_expression_stack_size() > 0) {
984 ResourceMark rm(thread); // is this right ???
985 // we are at a call site & the expression stack is not empty
986 // => process callee's arguments
987 //
988 // Note: The expression stack can be empty if an exception
989 // occurred during method resolution/execution. In all
990 // cases we empty the expression stack completely be-
991 // fore handling the exception (the exception handling
992 // code in the interpreter calls a blocking runtime
993 // routine which can cause this code to be executed).
994 // (was bug gri 7/27/98)
995 oops_interpreted_arguments_do(signature, has_receiver, f);
996 }
997 }
998 }
1000 InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
1002 // process locals & expression stack
1003 InterpreterOopMap mask;
1004 if (query_oop_map_cache) {
1005 m->mask_for(bci, &mask);
1006 } else {
1007 OopMapCache::compute_one_oop_map(m, bci, &mask);
1008 }
1009 mask.iterate_oop(&blk);
1010 }
1013 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) {
1014 InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
1015 finder.oops_do();
1016 }
1018 void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* reg_map) {
1019 assert(_cb != NULL, "sanity check");
1020 if (_cb->oop_maps() != NULL) {
1021 OopMapSet::oops_do(this, reg_map, f);
1023 // Preserve potential arguments for a callee. We handle this by dispatching
1024 // on the codeblob. For c2i, we do
1025 if (reg_map->include_argument_oops()) {
1026 _cb->preserve_callee_argument_oops(*this, reg_map, f);
1027 }
1028 }
1029 // In cases where perm gen is collected, GC will want to mark
1030 // oops referenced from nmethods active on thread stacks so as to
1031 // prevent them from being collected. However, this visit should be
1032 // restricted to certain phases of the collection only. The
1033 // closure decides how it wants nmethods to be traced.
1034 if (cf != NULL)
1035 cf->do_code_blob(_cb);
1036 }
1038 class CompiledArgumentOopFinder: public SignatureInfo {
1039 protected:
1040 OopClosure* _f;
1041 int _offset; // the current offset, incremented with each argument
1042 bool _has_receiver; // true if the callee has a receiver
1043 bool _has_appendix; // true if the call has an appendix
1044 frame _fr;
1045 RegisterMap* _reg_map;
1046 int _arg_size;
1047 VMRegPair* _regs; // VMReg list of arguments
1049 void set(int size, BasicType type) {
1050 if (type == T_OBJECT || type == T_ARRAY) handle_oop_offset();
1051 _offset += size;
1052 }
1054 virtual void handle_oop_offset() {
1055 // Extract low order register number from register array.
1056 // In LP64-land, the high-order bits are valid but unhelpful.
1057 VMReg reg = _regs[_offset].first();
1058 oop *loc = _fr.oopmapreg_to_location(reg, _reg_map);
1059 _f->do_oop(loc);
1060 }
1062 public:
1063 CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)
1064 : SignatureInfo(signature) {
1066 // initialize CompiledArgumentOopFinder
1067 _f = f;
1068 _offset = 0;
1069 _has_receiver = has_receiver;
1070 _has_appendix = has_appendix;
1071 _fr = fr;
1072 _reg_map = (RegisterMap*)reg_map;
1073 _arg_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
1075 int arg_size;
1076 _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
1077 assert(arg_size == _arg_size, "wrong arg size");
1078 }
1080 void oops_do() {
1081 if (_has_receiver) {
1082 handle_oop_offset();
1083 _offset++;
1084 }
1085 iterate_parameters();
1086 if (_has_appendix) {
1087 handle_oop_offset();
1088 _offset++;
1089 }
1090 }
1091 };
1093 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix, const RegisterMap* reg_map, OopClosure* f) {
1094 ResourceMark rm;
1095 CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
1096 finder.oops_do();
1097 }
1100 // Get receiver out of callers frame, i.e. find parameter 0 in callers
1101 // frame. Consult ADLC for where parameter 0 is to be found. Then
1102 // check local reg_map for it being a callee-save register or argument
1103 // register, both of which are saved in the local frame. If not found
1104 // there, it must be an in-stack argument of the caller.
1105 // Note: caller.sp() points to callee-arguments
1106 oop frame::retrieve_receiver(RegisterMap* reg_map) {
1107 frame caller = *this;
1109 // First consult the ADLC on where it puts parameter 0 for this signature.
1110 VMReg reg = SharedRuntime::name_for_receiver();
1111 oop* oop_adr = caller.oopmapreg_to_location(reg, reg_map);
1112 if (oop_adr == NULL) {
1113 guarantee(oop_adr != NULL, "bad register save location");
1114 return NULL;
1115 }
1116 oop r = *oop_adr;
1117 assert(Universe::heap()->is_in_or_null(r), err_msg("bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", (void *) r, (void *) r));
1118 return r;
1119 }
1122 oop* frame::oopmapreg_to_location(VMReg reg, const RegisterMap* reg_map) const {
1123 if(reg->is_reg()) {
1124 // If it is passed in a register, it got spilled in the stub frame.
1125 return (oop *)reg_map->location(reg);
1126 } else {
1127 int sp_offset_in_bytes = reg->reg2stack() * VMRegImpl::stack_slot_size;
1128 return (oop*)(((address)unextended_sp()) + sp_offset_in_bytes);
1129 }
1130 }
1132 BasicLock* frame::get_native_monitor() {
1133 nmethod* nm = (nmethod*)_cb;
1134 assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1135 "Should not call this unless it's a native nmethod");
1136 int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1137 assert(byte_offset >= 0, "should not see invalid offset");
1138 return (BasicLock*) &sp()[byte_offset / wordSize];
1139 }
1141 oop frame::get_native_receiver() {
1142 nmethod* nm = (nmethod*)_cb;
1143 assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1144 "Should not call this unless it's a native nmethod");
1145 int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1146 assert(byte_offset >= 0, "should not see invalid offset");
1147 oop owner = ((oop*) sp())[byte_offset / wordSize];
1148 assert( Universe::heap()->is_in(owner), "bad receiver" );
1149 return owner;
1150 }
1152 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) {
1153 assert(map != NULL, "map must be set");
1154 if (map->include_argument_oops()) {
1155 // must collect argument oops, as nobody else is doing it
1156 Thread *thread = Thread::current();
1157 methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1158 EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1159 finder.arguments_do(f);
1160 }
1161 // Traverse the Handle Block saved in the entry frame
1162 entry_frame_call_wrapper()->oops_do(f);
1163 }
1166 void frame::oops_do_internal(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf, RegisterMap* map, bool use_interpreter_oop_map_cache) {
1167 #ifndef PRODUCT
1168 // simulate GC crash here to dump java thread in error report
1169 if (CrashGCForDumpingJavaThread) {
1170 char *t = NULL;
1171 *t = 'c';
1172 }
1173 #endif
1174 if (is_interpreted_frame()) {
1175 oops_interpreted_do(f, cld_f, map, use_interpreter_oop_map_cache);
1176 } else if (is_entry_frame()) {
1177 oops_entry_do(f, map);
1178 } else if (CodeCache::contains(pc())) {
1179 oops_code_blob_do(f, cf, map);
1180 #ifdef SHARK
1181 } else if (is_fake_stub_frame()) {
1182 // nothing to do
1183 #endif // SHARK
1184 } else {
1185 ShouldNotReachHere();
1186 }
1187 }
1189 void frame::nmethods_do(CodeBlobClosure* cf) {
1190 if (_cb != NULL && _cb->is_nmethod()) {
1191 cf->do_code_blob(_cb);
1192 }
1193 }
1196 // call f() on the interpreted Method*s in the stack.
1197 // Have to walk the entire code cache for the compiled frames Yuck.
1198 void frame::metadata_do(void f(Metadata*)) {
1199 if (_cb != NULL && Interpreter::contains(pc())) {
1200 Method* m = this->interpreter_frame_method();
1201 assert(m != NULL, "huh?");
1202 f(m);
1203 }
1204 }
1206 void frame::gc_prologue() {
1207 if (is_interpreted_frame()) {
1208 // set bcx to bci to become Method* position independent during GC
1209 interpreter_frame_set_bcx(interpreter_frame_bci());
1210 }
1211 }
1214 void frame::gc_epilogue() {
1215 if (is_interpreted_frame()) {
1216 // set bcx back to bcp for interpreter
1217 interpreter_frame_set_bcx((intptr_t)interpreter_frame_bcp());
1218 }
1219 // call processor specific epilog function
1220 pd_gc_epilog();
1221 }
1224 # ifdef ENABLE_ZAP_DEAD_LOCALS
1226 void frame::CheckValueClosure::do_oop(oop* p) {
1227 if (CheckOopishValues && Universe::heap()->is_in_reserved(*p)) {
1228 warning("value @ " INTPTR_FORMAT " looks oopish (" INTPTR_FORMAT ") (thread = " INTPTR_FORMAT ")", p, (address)*p, Thread::current());
1229 }
1230 }
1231 frame::CheckValueClosure frame::_check_value;
1234 void frame::CheckOopClosure::do_oop(oop* p) {
1235 if (*p != NULL && !(*p)->is_oop()) {
1236 warning("value @ " INTPTR_FORMAT " should be an oop (" INTPTR_FORMAT ") (thread = " INTPTR_FORMAT ")", p, (address)*p, Thread::current());
1237 }
1238 }
1239 frame::CheckOopClosure frame::_check_oop;
1241 void frame::check_derived_oop(oop* base, oop* derived) {
1242 _check_oop.do_oop(base);
1243 }
1246 void frame::ZapDeadClosure::do_oop(oop* p) {
1247 if (TraceZapDeadLocals) tty->print_cr("zapping @ " INTPTR_FORMAT " containing " INTPTR_FORMAT, p, (address)*p);
1248 *p = cast_to_oop<intptr_t>(0xbabebabe);
1249 }
1250 frame::ZapDeadClosure frame::_zap_dead;
1252 void frame::zap_dead_locals(JavaThread* thread, const RegisterMap* map) {
1253 assert(thread == Thread::current(), "need to synchronize to do this to another thread");
1254 // Tracing - part 1
1255 if (TraceZapDeadLocals) {
1256 ResourceMark rm(thread);
1257 tty->print_cr("--------------------------------------------------------------------------------");
1258 tty->print("Zapping dead locals in ");
1259 print_on(tty);
1260 tty->cr();
1261 }
1262 // Zapping
1263 if (is_entry_frame ()) zap_dead_entry_locals (thread, map);
1264 else if (is_interpreted_frame()) zap_dead_interpreted_locals(thread, map);
1265 else if (is_compiled_frame()) zap_dead_compiled_locals (thread, map);
1267 else
1268 // could be is_runtime_frame
1269 // so remove error: ShouldNotReachHere();
1270 ;
1271 // Tracing - part 2
1272 if (TraceZapDeadLocals) {
1273 tty->cr();
1274 }
1275 }
1278 void frame::zap_dead_interpreted_locals(JavaThread *thread, const RegisterMap* map) {
1279 // get current interpreter 'pc'
1280 assert(is_interpreted_frame(), "Not an interpreted frame");
1281 Method* m = interpreter_frame_method();
1282 int bci = interpreter_frame_bci();
1284 int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
1286 // process dynamic part
1287 InterpreterFrameClosure value_blk(this, max_locals, m->max_stack(),
1288 &_check_value);
1289 InterpreterFrameClosure oop_blk(this, max_locals, m->max_stack(),
1290 &_check_oop );
1291 InterpreterFrameClosure dead_blk(this, max_locals, m->max_stack(),
1292 &_zap_dead );
1294 // get frame map
1295 InterpreterOopMap mask;
1296 m->mask_for(bci, &mask);
1297 mask.iterate_all( &oop_blk, &value_blk, &dead_blk);
1298 }
1301 void frame::zap_dead_compiled_locals(JavaThread* thread, const RegisterMap* reg_map) {
1303 ResourceMark rm(thread);
1304 assert(_cb != NULL, "sanity check");
1305 if (_cb->oop_maps() != NULL) {
1306 OopMapSet::all_do(this, reg_map, &_check_oop, check_derived_oop, &_check_value);
1307 }
1308 }
1311 void frame::zap_dead_entry_locals(JavaThread*, const RegisterMap*) {
1312 if (TraceZapDeadLocals) warning("frame::zap_dead_entry_locals unimplemented");
1313 }
1316 void frame::zap_dead_deoptimized_locals(JavaThread*, const RegisterMap*) {
1317 if (TraceZapDeadLocals) warning("frame::zap_dead_deoptimized_locals unimplemented");
1318 }
1320 # endif // ENABLE_ZAP_DEAD_LOCALS
1322 void frame::verify(const RegisterMap* map) {
1323 // for now make sure receiver type is correct
1324 if (is_interpreted_frame()) {
1325 Method* method = interpreter_frame_method();
1326 guarantee(method->is_method(), "method is wrong in frame::verify");
1327 if (!method->is_static()) {
1328 // fetch the receiver
1329 oop* p = (oop*) interpreter_frame_local_at(0);
1330 // make sure we have the right receiver type
1331 }
1332 }
1333 COMPILER2_PRESENT(assert(DerivedPointerTable::is_empty(), "must be empty before verify");)
1334 oops_do_internal(&VerifyOopClosure::verify_oop, NULL, NULL, (RegisterMap*)map, false);
1335 }
1338 #ifdef ASSERT
1339 bool frame::verify_return_pc(address x) {
1340 if (StubRoutines::returns_to_call_stub(x)) {
1341 return true;
1342 }
1343 if (CodeCache::contains(x)) {
1344 return true;
1345 }
1346 if (Interpreter::contains(x)) {
1347 return true;
1348 }
1349 return false;
1350 }
1351 #endif
1353 #ifdef ASSERT
1354 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1355 assert(is_interpreted_frame(), "Not an interpreted frame");
1356 // verify that the value is in the right part of the frame
1357 address low_mark = (address) interpreter_frame_monitor_end();
1358 address high_mark = (address) interpreter_frame_monitor_begin();
1359 address current = (address) value;
1361 const int monitor_size = frame::interpreter_frame_monitor_size();
1362 guarantee((high_mark - current) % monitor_size == 0 , "Misaligned top of BasicObjectLock*");
1363 guarantee( high_mark > current , "Current BasicObjectLock* higher than high_mark");
1365 guarantee((current - low_mark) % monitor_size == 0 , "Misaligned bottom of BasicObjectLock*");
1366 guarantee( current >= low_mark , "Current BasicObjectLock* below than low_mark");
1367 }
1368 #endif
1370 #ifndef PRODUCT
1371 void frame::describe(FrameValues& values, int frame_no) {
1372 // boundaries: sp and the 'real' frame pointer
1373 values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 1);
1374 intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1376 // print frame info at the highest boundary
1377 intptr_t* info_address = MAX2(sp(), frame_pointer);
1379 if (info_address != frame_pointer) {
1380 // print frame_pointer explicitly if not marked by the frame info
1381 values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1382 }
1384 if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1385 // Label values common to most frames
1386 values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no));
1387 }
1389 if (is_interpreted_frame()) {
1390 Method* m = interpreter_frame_method();
1391 int bci = interpreter_frame_bci();
1393 // Label the method and current bci
1394 values.describe(-1, info_address,
1395 FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 2);
1396 values.describe(-1, info_address,
1397 err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 1);
1398 if (m->max_locals() > 0) {
1399 intptr_t* l0 = interpreter_frame_local_at(0);
1400 intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1401 values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 1);
1402 // Report each local and mark as owned by this frame
1403 for (int l = 0; l < m->max_locals(); l++) {
1404 intptr_t* l0 = interpreter_frame_local_at(l);
1405 values.describe(frame_no, l0, err_msg("local %d", l));
1406 }
1407 }
1409 // Compute the actual expression stack size
1410 InterpreterOopMap mask;
1411 OopMapCache::compute_one_oop_map(m, bci, &mask);
1412 intptr_t* tos = NULL;
1413 // Report each stack element and mark as owned by this frame
1414 for (int e = 0; e < mask.expression_stack_size(); e++) {
1415 tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1416 values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1417 err_msg("stack %d", e));
1418 }
1419 if (tos != NULL) {
1420 values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 1);
1421 }
1422 if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1423 values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1424 values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1425 }
1426 } else if (is_entry_frame()) {
1427 // For now just label the frame
1428 values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1429 } else if (is_compiled_frame()) {
1430 // For now just label the frame
1431 nmethod* nm = cb()->as_nmethod_or_null();
1432 values.describe(-1, info_address,
1433 FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method %s%s", frame_no,
1434 nm, nm->method()->name_and_sig_as_C_string(),
1435 (_deopt_state == is_deoptimized) ?
1436 " (deoptimized)" :
1437 ((_deopt_state == unknown) ? " (state unknown)" : "")),
1438 2);
1439 } else if (is_native_frame()) {
1440 // For now just label the frame
1441 nmethod* nm = cb()->as_nmethod_or_null();
1442 values.describe(-1, info_address,
1443 FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1444 nm, nm->method()->name_and_sig_as_C_string()), 2);
1445 } else {
1446 // provide default info if not handled before
1447 char *info = (char *) "special frame";
1448 if ((_cb != NULL) &&
1449 (_cb->name() != NULL)) {
1450 info = (char *)_cb->name();
1451 }
1452 values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1453 }
1455 // platform dependent additional data
1456 describe_pd(values, frame_no);
1457 }
1459 #endif
1462 //-----------------------------------------------------------------------------------
1463 // StackFrameStream implementation
1465 StackFrameStream::StackFrameStream(JavaThread *thread, bool update) : _reg_map(thread, update) {
1466 assert(thread->has_last_Java_frame(), "sanity check");
1467 _fr = thread->last_frame();
1468 _is_done = false;
1469 }
1472 #ifndef PRODUCT
1474 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1475 FrameValue fv;
1476 fv.location = location;
1477 fv.owner = owner;
1478 fv.priority = priority;
1479 fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1480 strcpy(fv.description, description);
1481 _values.append(fv);
1482 }
1485 #ifdef ASSERT
1486 void FrameValues::validate() {
1487 _values.sort(compare);
1488 bool error = false;
1489 FrameValue prev;
1490 prev.owner = -1;
1491 for (int i = _values.length() - 1; i >= 0; i--) {
1492 FrameValue fv = _values.at(i);
1493 if (fv.owner == -1) continue;
1494 if (prev.owner == -1) {
1495 prev = fv;
1496 continue;
1497 }
1498 if (prev.location == fv.location) {
1499 if (fv.owner != prev.owner) {
1500 tty->print_cr("overlapping storage");
1501 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", prev.location, *prev.location, prev.description);
1502 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", fv.location, *fv.location, fv.description);
1503 error = true;
1504 }
1505 } else {
1506 prev = fv;
1507 }
1508 }
1509 assert(!error, "invalid layout");
1510 }
1511 #endif // ASSERT
1513 void FrameValues::print(JavaThread* thread) {
1514 _values.sort(compare);
1516 // Sometimes values like the fp can be invalid values if the
1517 // register map wasn't updated during the walk. Trim out values
1518 // that aren't actually in the stack of the thread.
1519 int min_index = 0;
1520 int max_index = _values.length() - 1;
1521 intptr_t* v0 = _values.at(min_index).location;
1522 intptr_t* v1 = _values.at(max_index).location;
1524 if (thread == Thread::current()) {
1525 while (!thread->is_in_stack((address)v0)) {
1526 v0 = _values.at(++min_index).location;
1527 }
1528 while (!thread->is_in_stack((address)v1)) {
1529 v1 = _values.at(--max_index).location;
1530 }
1531 } else {
1532 while (!thread->on_local_stack((address)v0)) {
1533 v0 = _values.at(++min_index).location;
1534 }
1535 while (!thread->on_local_stack((address)v1)) {
1536 v1 = _values.at(--max_index).location;
1537 }
1538 }
1539 intptr_t* min = MIN2(v0, v1);
1540 intptr_t* max = MAX2(v0, v1);
1541 intptr_t* cur = max;
1542 intptr_t* last = NULL;
1543 for (int i = max_index; i >= min_index; i--) {
1544 FrameValue fv = _values.at(i);
1545 while (cur > fv.location) {
1546 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, cur, *cur);
1547 cur--;
1548 }
1549 if (last == fv.location) {
1550 const char* spacer = " " LP64_ONLY(" ");
1551 tty->print_cr(" %s %s %s", spacer, spacer, fv.description);
1552 } else {
1553 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", fv.location, *fv.location, fv.description);
1554 last = fv.location;
1555 cur--;
1556 }
1557 }
1558 }
1560 #endif // ndef PRODUCT