1 /*
2 * Copyright (c) 1997, 2026, 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 "classfile/moduleEntry.hpp"
26 #include "code/codeCache.hpp"
27 #include "code/scopeDesc.hpp"
28 #include "code/vmreg.inline.hpp"
29 #include "compiler/abstractCompiler.hpp"
30 #include "compiler/disassembler.hpp"
31 #include "compiler/oopMap.hpp"
32 #include "gc/shared/collectedHeap.inline.hpp"
33 #include "interpreter/interpreter.hpp"
34 #include "interpreter/oopMapCache.hpp"
35 #include "logging/log.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "memory/universe.hpp"
38 #include "oops/markWord.hpp"
39 #include "oops/method.inline.hpp"
40 #include "oops/methodData.hpp"
41 #include "oops/oop.inline.hpp"
42 #include "oops/stackChunkOop.inline.hpp"
43 #include "oops/verifyOopClosure.hpp"
44 #include "prims/methodHandles.hpp"
45 #include "runtime/continuation.hpp"
46 #include "runtime/continuationEntry.inline.hpp"
47 #include "runtime/frame.inline.hpp"
48 #include "runtime/handles.inline.hpp"
49 #include "runtime/javaCalls.hpp"
50 #include "runtime/javaThread.hpp"
51 #include "runtime/monitorChunk.hpp"
52 #include "runtime/os.hpp"
53 #include "runtime/safefetch.hpp"
54 #include "runtime/sharedRuntime.hpp"
55 #include "runtime/signature.hpp"
56 #include "runtime/stackValue.hpp"
57 #include "runtime/stubCodeGenerator.hpp"
58 #include "runtime/stubRoutines.hpp"
59 #include "utilities/debug.hpp"
60 #include "utilities/decoder.hpp"
61 #include "utilities/formatBuffer.hpp"
62
63 RegisterMap::RegisterMap(JavaThread *thread, UpdateMap update_map, ProcessFrames process_frames, WalkContinuation walk_cont) {
64 _thread = thread;
65 _update_map = update_map == UpdateMap::include;
66 _process_frames = process_frames == ProcessFrames::include;
67 _walk_cont = walk_cont == WalkContinuation::include;
68 clear();
69 DEBUG_ONLY (_update_for_id = nullptr;)
70 NOT_PRODUCT(_skip_missing = false;)
71 NOT_PRODUCT(_async = false;)
72
73 if (walk_cont == WalkContinuation::include && thread != nullptr && thread->last_continuation() != nullptr) {
74 _chunk = stackChunkHandle(Thread::current()->handle_area()->allocate_null_handle(), true /* dummy */);
75 }
76 _chunk_index = -1;
77
78 #ifndef PRODUCT
79 for (int i = 0; i < reg_count ; i++ ) _location[i] = nullptr;
80 #endif /* PRODUCT */
81 }
82
83 RegisterMap::RegisterMap(oop continuation, UpdateMap update_map) {
84 _thread = nullptr;
85 _update_map = update_map == UpdateMap::include;
86 _process_frames = false;
87 _walk_cont = true;
88 clear();
89 DEBUG_ONLY (_update_for_id = nullptr;)
90 NOT_PRODUCT(_skip_missing = false;)
91 NOT_PRODUCT(_async = false;)
92
93 _chunk = stackChunkHandle(Thread::current()->handle_area()->allocate_null_handle(), true /* dummy */);
94 _chunk_index = -1;
95
96 #ifndef PRODUCT
97 for (int i = 0; i < reg_count ; i++ ) _location[i] = nullptr;
98 #endif /* PRODUCT */
99 }
100
101 RegisterMap::RegisterMap(const RegisterMap* map) {
102 assert(map != this, "bad initialization parameter");
103 assert(map != nullptr, "RegisterMap must be present");
104 _thread = map->thread();
105 _update_map = map->update_map();
106 _process_frames = map->process_frames();
107 _walk_cont = map->_walk_cont;
108 _include_argument_oops = map->include_argument_oops();
109 DEBUG_ONLY (_update_for_id = map->_update_for_id;)
110 NOT_PRODUCT(_skip_missing = map->_skip_missing;)
111 NOT_PRODUCT(_async = map->_async;)
112
113 // only the original RegisterMap's handle lives long enough for StackWalker; this is bound to cause trouble with nested continuations.
114 _chunk = map->_chunk;
115 _chunk_index = map->_chunk_index;
116
117 pd_initialize_from(map);
118 if (update_map()) {
119 for(int i = 0; i < location_valid_size; i++) {
120 LocationValidType bits = map->_location_valid[i];
121 _location_valid[i] = bits;
122 // for whichever bits are set, pull in the corresponding map->_location
123 int j = i*location_valid_type_size;
124 while (bits != 0) {
125 if ((bits & 1) != 0) {
126 assert(0 <= j && j < reg_count, "range check");
127 _location[j] = map->_location[j];
128 }
129 bits >>= 1;
130 j += 1;
131 }
132 }
133 }
134 }
135
136 oop RegisterMap::cont() const {
137 return _chunk() != nullptr ? _chunk()->cont() : (oop)nullptr;
138 }
139
140 void RegisterMap::set_stack_chunk(stackChunkOop chunk) {
141 assert(chunk == nullptr || _walk_cont, "");
142 assert(chunk == nullptr || _chunk.not_null(), "");
143 if (_chunk.is_null()) return;
144 log_trace(continuations)("set_stack_chunk: " INTPTR_FORMAT " this: " INTPTR_FORMAT, p2i((oopDesc*)chunk), p2i(this));
145 _chunk.replace(chunk); // reuse handle. see comment above in the constructor
146 if (chunk == nullptr) {
147 _chunk_index = -1;
148 } else {
149 _chunk_index++;
150 }
151 }
152
153 void RegisterMap::clear() {
154 set_include_argument_oops(true);
155 if (update_map()) {
156 for(int i = 0; i < location_valid_size; i++) {
157 _location_valid[i] = 0;
158 }
159 pd_clear();
160 } else {
161 pd_initialize();
162 }
163 }
164
165 #ifndef PRODUCT
166
167 VMReg RegisterMap::find_register_spilled_here(void* p, intptr_t* sp) {
168 for(int i = 0; i < RegisterMap::reg_count; i++) {
169 VMReg r = VMRegImpl::as_VMReg(i);
170 if (p == location(r, sp)) return r;
171 }
172 return nullptr;
173 }
174
175 void RegisterMap::print_on(outputStream* st) const {
176 st->print_cr("Register map");
177 for(int i = 0; i < reg_count; i++) {
178
179 VMReg r = VMRegImpl::as_VMReg(i);
180 intptr_t* src = (intptr_t*) location(r, nullptr);
181 if (src != nullptr) {
182
183 r->print_on(st);
184 st->print(" [" INTPTR_FORMAT "] = ", p2i(src));
185 if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
186 st->print_cr("<misaligned>");
187 } else {
188 st->print_cr(INTPTR_FORMAT, *src);
189 }
190 }
191 }
192 }
193
194 void RegisterMap::print() const {
195 print_on(tty);
196 }
197
198 #endif
199 // This returns the pc that if you were in the debugger you'd see. Not
200 // the idealized value in the frame object. This undoes the magic conversion
201 // that happens for deoptimized frames. In addition it makes the value the
202 // hardware would want to see in the native frame. The only user (at this point)
203 // is deoptimization. It likely no one else should ever use it.
204
205 address frame::raw_pc() const {
206 if (is_deoptimized_frame()) {
207 nmethod* nm = cb()->as_nmethod_or_null();
208 assert(nm != nullptr, "only nmethod is expected here");
209 return nm->deopt_handler_entry();
210 } else {
211 return pc();
212 }
213 }
214
215 // Change the pc in a frame object. This does not change the actual pc in
216 // actual frame. To do that use patch_pc.
217 //
218 void frame::set_pc(address newpc) {
219 #ifdef ASSERT
220 if (_cb != nullptr && _cb->is_nmethod()) {
221 assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
222 }
223 #endif // ASSERT
224
225 // Unsafe to use the is_deoptimized tester after changing pc
226 _deopt_state = unknown;
227 _pc = newpc;
228 _cb = CodeCache::find_blob(_pc);
229 }
230
231 // This is optimized for intra-blob pc adjustments only.
232 void frame::adjust_pc(address newpc) {
233 assert(_cb != nullptr, "invariant");
234 assert(_cb == CodeCache::find_blob(newpc), "invariant");
235 // Unsafe to use the is_deoptimized tester after changing pc
236 _deopt_state = unknown;
237 _pc = newpc;
238 }
239
240 // type testers
241 bool frame::is_ignored_frame() const {
242 return false; // FIXME: some LambdaForm frames should be ignored
243 }
244
245 bool frame::is_native_frame() const {
246 return (_cb != nullptr &&
247 _cb->is_nmethod() &&
248 ((nmethod*)_cb)->is_native_method());
249 }
250
251 bool frame::is_java_frame() const {
252 if (is_interpreted_frame()) return true;
253 if (is_compiled_frame()) return true;
254 return false;
255 }
256
257 bool frame::is_runtime_frame() const {
258 return (_cb != nullptr && _cb->is_runtime_stub());
259 }
260
261 bool frame::is_safepoint_blob_frame() const {
262 return (_cb != nullptr && _cb->is_safepoint_stub());
263 }
264
265 // testers
266
267 bool frame::is_first_java_frame() const {
268 RegisterMap map(JavaThread::current(),
269 RegisterMap::UpdateMap::skip,
270 RegisterMap::ProcessFrames::include,
271 RegisterMap::WalkContinuation::skip); // No update
272 frame s;
273 for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));
274 return s.is_first_frame();
275 }
276
277 bool frame::is_first_vthread_frame(JavaThread* thread) const {
278 return Continuation::is_continuation_enterSpecial(*this)
279 && Continuation::get_continuation_entry_for_entry_frame(thread, *this)->is_virtual_thread();
280 }
281
282 bool frame::entry_frame_is_first() const {
283 return entry_frame_call_wrapper()->is_first_frame();
284 }
285
286 JavaCallWrapper* frame::entry_frame_call_wrapper_if_safe(JavaThread* thread) const {
287 JavaCallWrapper** jcw = entry_frame_call_wrapper_addr();
288 address addr = (address) jcw;
289
290 // addr must be within the usable part of the stack
291 if (thread->is_in_usable_stack(addr)) {
292 return *jcw;
293 }
294
295 return nullptr;
296 }
297
298 bool frame::is_entry_frame_valid(JavaThread* thread) const {
299 // Validate the JavaCallWrapper an entry frame must have
300 address jcw = (address)entry_frame_call_wrapper();
301 if (!thread->is_in_stack_range_excl(jcw, (address)fp())) {
302 return false;
303 }
304
305 // Validate sp saved in the java frame anchor
306 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
307 return (jfa->last_Java_sp() > sp());
308 }
309
310 Method* frame::safe_interpreter_frame_method() const {
311 Method** m_addr = interpreter_frame_method_addr();
312 if (m_addr == nullptr) {
313 return nullptr;
314 }
315 return (Method*) SafeFetchN((intptr_t*) m_addr, 0);
316 }
317
318 bool frame::should_be_deoptimized() const {
319 if (_deopt_state == is_deoptimized ||
320 !is_compiled_frame() ) return false;
321 assert(_cb != nullptr && _cb->is_nmethod(), "must be an nmethod");
322 nmethod* nm = _cb->as_nmethod();
323 LogTarget(Debug, dependencies) lt;
324 if (lt.is_enabled()) {
325 LogStream ls(<);
326 ls.print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
327 nm->print_value_on(&ls);
328 ls.cr();
329 }
330
331 if( !nm->is_marked_for_deoptimization() )
332 return false;
333
334 // If at the return point, then the frame has already been popped, and
335 // only the return needs to be executed. Don't deoptimize here.
336 return !nm->is_at_poll_return(pc());
337 }
338
339 bool frame::can_be_deoptimized() const {
340 if (!is_compiled_frame()) return false;
341 nmethod* nm = _cb->as_nmethod();
342
343 if(!nm->can_be_deoptimized())
344 return false;
345
346 return !nm->is_at_poll_return(pc());
347 }
348
349 void frame::deoptimize(JavaThread* thread) {
350 assert(thread == nullptr
351 || (thread->frame_anchor()->has_last_Java_frame() &&
352 thread->frame_anchor()->walkable()), "must be");
353 // Schedule deoptimization of an nmethod activation with this frame.
354 assert(_cb != nullptr && _cb->is_nmethod(), "must be");
355
356 // If the call site is a MethodHandle call site use the MH deopt handler.
357 nmethod* nm = _cb->as_nmethod();
358 address deopt = nm->deopt_handler_entry();
359
360 NativePostCallNop* inst = nativePostCallNop_at(pc());
361
362 // Save the original pc before we patch in the new one
363 nm->set_original_pc(this, pc());
364 patch_pc(thread, deopt);
365 assert(is_deoptimized_frame(), "must be");
366
367 #ifdef ASSERT
368 if (thread != nullptr) {
369 frame fr = thread->last_frame();
370 RegisterMap map(thread,
371 RegisterMap::UpdateMap::skip,
372 RegisterMap::ProcessFrames::include,
373 !is_heap_frame() ? RegisterMap::WalkContinuation::skip : RegisterMap::WalkContinuation::include);
374 intptr_t* fr_id = fr.id();
375 while (id() != fr_id) {
376 fr = fr.sender(&map);
377 if (fr.is_heap_frame()) {
378 assert(is_heap_frame(), "");
379 frame derel_fr = map.stack_chunk()->derelativize(fr);
380 fr_id = derel_fr.id();
381 } else {
382 fr_id = fr.id();
383 }
384 }
385 assert(fr.is_deoptimized_frame(), "missed deopt");
386 }
387 #endif // ASSERT
388 }
389
390 void frame::deoptimize(JavaThread* thread, stackChunkOop chunk) {
391 assert(is_heap_frame() && _frame_index >= 0, "wrong frame type");
392
393 // Fast path does not expect deopted frames
394 chunk->force_slow_path();
395
396 frame fr = chunk->derelativize(*this);
397 fr.deoptimize(nullptr);
398
399 // Fix chunk pc if deopted frame is the top one
400 bool is_top = fr.sp() == chunk->sp_address();
401 if (is_top) {
402 chunk->set_pc(fr.raw_pc());
403 }
404 }
405
406 frame frame::java_sender() const {
407 RegisterMap map(JavaThread::current(),
408 RegisterMap::UpdateMap::skip,
409 RegisterMap::ProcessFrames::include,
410 RegisterMap::WalkContinuation::skip);
411 frame s;
412 for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
413 guarantee(s.is_java_frame(), "tried to get caller of first java frame");
414 return s;
415 }
416
417 frame frame::real_sender(RegisterMap* map) const {
418 frame result = sender(map);
419 while (result.is_runtime_frame() ||
420 result.is_ignored_frame()) {
421 result = result.sender(map);
422 }
423 return result;
424 }
425
426 // Interpreter frames
427
428
429 Method* frame::interpreter_frame_method() const {
430 assert(is_interpreted_frame(), "interpreted frame expected");
431 Method* m = *interpreter_frame_method_addr();
432 assert(m->is_method(), "not a Method*");
433 return m;
434 }
435
436 void frame::interpreter_frame_set_method(Method* method) {
437 assert(is_interpreted_frame(), "interpreted frame expected");
438 *interpreter_frame_method_addr() = method;
439 }
440
441 void frame::interpreter_frame_set_mirror(oop mirror) {
442 assert(is_interpreted_frame(), "interpreted frame expected");
443 *interpreter_frame_mirror_addr() = mirror;
444 }
445
446 jint frame::interpreter_frame_bci() const {
447 assert(is_interpreted_frame(), "interpreted frame expected");
448 address bcp = interpreter_frame_bcp();
449 return interpreter_frame_method()->bci_from(bcp);
450 }
451
452 address frame::interpreter_frame_bcp() const {
453 assert(is_interpreted_frame(), "interpreted frame expected");
454 address bcp = (address)*interpreter_frame_bcp_addr();
455 return interpreter_frame_method()->bcp_from(bcp);
456 }
457
458 void frame::interpreter_frame_set_bcp(address bcp) {
459 assert(is_interpreted_frame(), "interpreted frame expected");
460 *interpreter_frame_bcp_addr() = (intptr_t)bcp;
461 }
462
463 address frame::interpreter_frame_mdp() const {
464 assert(ProfileInterpreter, "must be profiling interpreter");
465 assert(is_interpreted_frame(), "interpreted frame expected");
466 return (address)*interpreter_frame_mdp_addr();
467 }
468
469 void frame::interpreter_frame_set_mdp(address mdp) {
470 assert(is_interpreted_frame(), "interpreted frame expected");
471 assert(ProfileInterpreter, "must be profiling interpreter");
472 *interpreter_frame_mdp_addr() = (intptr_t)mdp;
473 }
474
475 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
476 assert(is_interpreted_frame(), "Not an interpreted frame");
477 #ifdef ASSERT
478 interpreter_frame_verify_monitor(current);
479 #endif
480 BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
481 return next;
482 }
483
484 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
485 assert(is_interpreted_frame(), "Not an interpreted frame");
486 #ifdef ASSERT
487 // // This verification needs to be checked before being enabled
488 // interpreter_frame_verify_monitor(current);
489 #endif
490 BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
491 return previous;
492 }
493
494 // Interpreter locals and expression stack locations.
495
496 intptr_t* frame::interpreter_frame_local_at(int index) const {
497 const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
498 intptr_t* first = interpreter_frame_locals();
499 return &(first[n]);
500 }
501
502 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
503 const int i = offset * interpreter_frame_expression_stack_direction();
504 const int n = i * Interpreter::stackElementWords;
505 return &(interpreter_frame_expression_stack()[n]);
506 }
507
508 jint frame::interpreter_frame_expression_stack_size() const {
509 // Number of elements on the interpreter expression stack
510 // Callers should span by stackElementWords
511 int element_size = Interpreter::stackElementWords;
512 size_t stack_size = 0;
513 if (frame::interpreter_frame_expression_stack_direction() < 0) {
514 stack_size = (interpreter_frame_expression_stack() -
515 interpreter_frame_tos_address() + 1)/element_size;
516 } else {
517 stack_size = (interpreter_frame_tos_address() -
518 interpreter_frame_expression_stack() + 1)/element_size;
519 }
520 assert(stack_size <= (size_t)max_jint, "stack size too big");
521 return (jint)stack_size;
522 }
523
524 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
525
526 const char* frame::print_name() const {
527 if (is_native_frame()) return "Native";
528 if (is_interpreted_frame()) return "Interpreted";
529 if (is_compiled_frame()) {
530 if (is_deoptimized_frame()) return "Deoptimized";
531 return "Compiled";
532 }
533 if (sp() == nullptr) return "Empty";
534 return "C";
535 }
536
537 void frame::print_value_on(outputStream* st) const {
538 NOT_PRODUCT(address begin = pc()-40;)
539 NOT_PRODUCT(address end = nullptr;)
540
541 st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), p2i(sp()), p2i(unextended_sp()));
542 if (sp() != nullptr)
543 st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT,
544 p2i(fp()), p2i(real_fp()), p2i(pc()));
545 st->print_cr(")");
546
547 if (StubRoutines::contains(pc())) {
548 StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
549 st->print("~Stub::%s", desc->name());
550 NOT_PRODUCT(begin = desc->begin(); end = desc->end();)
551 } else if (Interpreter::contains(pc())) {
552 InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
553 if (desc != nullptr) {
554 st->print("~");
555 desc->print_on(st);
556 NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)
557 } else {
558 st->print("~interpreter");
559 }
560 }
561
562 #ifndef PRODUCT
563 if (_cb != nullptr) {
564 st->print(" ");
565 _cb->print_value_on(st);
566 if (end == nullptr) {
567 begin = _cb->code_begin();
568 end = _cb->code_end();
569 }
570 }
571 if (WizardMode && Verbose) Disassembler::decode(begin, end);
572 #endif
573 }
574
575 void frame::print_on(outputStream* st) const {
576 print_value_on(st);
577 if (is_interpreted_frame()) {
578 interpreter_frame_print_on(st);
579 }
580 }
581
582 void frame::interpreter_frame_print_on(outputStream* st) const {
583 #ifndef PRODUCT
584 assert(is_interpreted_frame(), "Not an interpreted frame");
585 jint i;
586 for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
587 intptr_t x = *interpreter_frame_local_at(i);
588 st->print(" - local [" INTPTR_FORMAT "]", x);
589 st->fill_to(23);
590 st->print_cr("; #%d", i);
591 }
592 for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
593 intptr_t x = *interpreter_frame_expression_stack_at(i);
594 st->print(" - stack [" INTPTR_FORMAT "]", x);
595 st->fill_to(23);
596 st->print_cr("; #%d", i);
597 }
598 // locks for synchronization
599 for (BasicObjectLock* current = interpreter_frame_monitor_end();
600 current < interpreter_frame_monitor_begin();
601 current = next_monitor_in_interpreter_frame(current)) {
602 st->print(" - obj [%s", current->obj() == nullptr ? "null" : "");
603 oop obj = current->obj();
604 if (obj != nullptr) {
605 if (!is_heap_frame()) {
606 obj->print_value_on(st);
607 } else {
608 // Might be an invalid oop. We don't have the
609 // stackChunk to correct it so just print address.
610 st->print(INTPTR_FORMAT, p2i(obj));
611 }
612 }
613 st->print_cr("]");
614 st->print(" - lock [");
615 if (!is_heap_frame()) {
616 current->lock()->print_on(st, obj);
617 }
618 st->print_cr("]");
619 }
620 // monitor
621 st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin()));
622 // bcp
623 st->print(" - bcp [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp()));
624 st->fill_to(23);
625 st->print_cr("; @%d", interpreter_frame_bci());
626 // locals
627 st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at(0)));
628 // method
629 st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method()));
630 st->fill_to(23);
631 st->print("; ");
632 interpreter_frame_method()->print_name(st);
633 st->cr();
634 #endif
635 }
636
637 // Print whether the frame is in the VM or OS indicating a HotSpot problem.
638 // Otherwise, it's likely a bug in the native library that the Java code calls,
639 // hopefully indicating where to submit bugs.
640 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
641 // C/C++ frame
642 bool in_vm = os::address_is_in_vm(pc);
643 st->print(in_vm ? "V" : "C");
644
645 int offset;
646 bool found;
647
648 if (buf == nullptr || buflen < 1) return;
649 // libname
650 buf[0] = '\0';
651 found = os::dll_address_to_library_name(pc, buf, buflen, &offset);
652 if (found && buf[0] != '\0') {
653 // skip directory names
654 const char *p1, *p2;
655 p1 = buf;
656 int len = (int)strlen(os::file_separator());
657 while ((p2 = strstr(p1, os::file_separator())) != nullptr) p1 = p2 + len;
658 st->print(" [%s+0x%x]", p1, offset);
659 } else {
660 st->print(" " PTR_FORMAT, p2i(pc));
661 }
662
663 found = os::dll_address_to_function_name(pc, buf, buflen, &offset);
664 if (found) {
665 st->print(" %s+0x%x", buf, offset);
666 }
667 }
668
669 // frame::print_on_error() is called by fatal error handler. Notice that we may
670 // crash inside this function if stack frame is corrupted. The fatal error
671 // handler can catch and handle the crash. Here we assume the frame is valid.
672 //
673 // First letter indicates type of the frame:
674 // J: Java frame (compiled)
675 // j: Java frame (interpreted)
676 // V: VM frame (C/C++)
677 // v: Other frames running VM generated code (e.g. stubs, adapters, etc.)
678 // C: C/C++ frame
679 //
680 // We don't need detailed frame type as that in frame::print_name(). "C"
681 // suggests the problem is in user lib; everything else is likely a VM bug.
682
683 void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {
684 if (_cb != nullptr) {
685 if (Interpreter::contains(pc())) {
686 Method* m = this->interpreter_frame_method();
687 if (m != nullptr) {
688 m->name_and_sig_as_C_string(buf, buflen);
689 st->print("j %s", buf);
690 st->print("+%d", this->interpreter_frame_bci());
691 ModuleEntry* module = m->method_holder()->module();
692 if (module->is_named()) {
693 module->name()->as_C_string(buf, buflen);
694 st->print(" %s", buf);
695 if (module->version() != nullptr) {
696 module->version()->as_C_string(buf, buflen);
697 st->print("@%s", buf);
698 }
699 }
700 } else {
701 st->print("j " PTR_FORMAT, p2i(pc()));
702 }
703 } else if (StubRoutines::contains(pc())) {
704 StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
705 if (desc != nullptr) {
706 st->print("v ~StubRoutines::%s " PTR_FORMAT, desc->name(), p2i(pc()));
707 } else {
708 st->print("v ~StubRoutines::" PTR_FORMAT, p2i(pc()));
709 }
710 } else if (_cb->is_buffer_blob()) {
711 st->print("v ~BufferBlob::%s " PTR_FORMAT, ((BufferBlob *)_cb)->name(), p2i(pc()));
712 } else if (_cb->is_nmethod()) {
713 nmethod* nm = _cb->as_nmethod();
714 Method* m = nm->method();
715 if (m != nullptr) {
716 st->print("J %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : ""));
717 st->print(" %s", nm->compiler_name());
718 m->name_and_sig_as_C_string(buf, buflen);
719 st->print(" %s", buf);
720 ModuleEntry* module = m->method_holder()->module();
721 if (module->is_named()) {
722 module->name()->as_C_string(buf, buflen);
723 st->print(" %s", buf);
724 if (module->version() != nullptr) {
725 module->version()->as_C_string(buf, buflen);
726 st->print("@%s", buf);
727 }
728 }
729 st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]",
730 m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin());
731 } else {
732 st->print("J " PTR_FORMAT, p2i(pc()));
733 }
734 } else if (_cb->is_runtime_stub()) {
735 st->print("v ~RuntimeStub::%s " PTR_FORMAT, ((RuntimeStub *)_cb)->name(), p2i(pc()));
736 } else if (_cb->is_deoptimization_stub()) {
737 st->print("v ~DeoptimizationBlob " PTR_FORMAT, p2i(pc()));
738 } else if (_cb->is_exception_stub()) {
739 st->print("v ~ExceptionBlob " PTR_FORMAT, p2i(pc()));
740 } else if (_cb->is_safepoint_stub()) {
741 st->print("v ~SafepointBlob " PTR_FORMAT, p2i(pc()));
742 } else if (_cb->is_adapter_blob()) {
743 st->print("v ~AdapterBlob " PTR_FORMAT, p2i(pc()));
744 } else if (_cb->is_vtable_blob()) {
745 st->print("v ~VtableBlob " PTR_FORMAT, p2i(pc()));
746 } else if (_cb->is_method_handles_adapter_blob()) {
747 st->print("v ~MethodHandlesAdapterBlob " PTR_FORMAT, p2i(pc()));
748 } else if (_cb->is_uncommon_trap_stub()) {
749 st->print("v ~UncommonTrapBlob " PTR_FORMAT, p2i(pc()));
750 } else if (_cb->is_upcall_stub()) {
751 st->print("v ~UpcallStub::%s " PTR_FORMAT, _cb->name(), p2i(pc()));
752 } else {
753 st->print("v blob " PTR_FORMAT, p2i(pc()));
754 }
755 } else {
756 print_C_frame(st, buf, buflen, pc());
757 }
758 }
759
760
761 /*
762 The interpreter_frame_expression_stack_at method in the case of SPARC needs the
763 max_stack value of the method in order to compute the expression stack address.
764 It uses the Method* in order to get the max_stack value but during GC this
765 Method* value saved on the frame is changed by reverse_and_push and hence cannot
766 be used. So we save the max_stack value in the FrameClosure object and pass it
767 down to the interpreter_frame_expression_stack_at method
768 */
769 class InterpreterFrameClosure : public OffsetClosure {
770 private:
771 const frame* _fr;
772 OopClosure* _f;
773 int _max_locals;
774 int _max_stack;
775
776 public:
777 InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack,
778 OopClosure* f) {
779 _fr = fr;
780 _max_locals = max_locals;
781 _max_stack = max_stack;
782 _f = f;
783 }
784
785 void offset_do(int offset) {
786 oop* addr;
787 if (offset < _max_locals) {
788 addr = (oop*) _fr->interpreter_frame_local_at(offset);
789 assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
790 _f->do_oop(addr);
791 } else {
792 addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
793 // In case of exceptions, the expression stack is invalid and the esp will be reset to express
794 // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
795 bool in_stack;
796 if (frame::interpreter_frame_expression_stack_direction() > 0) {
797 in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
798 } else {
799 in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
800 }
801 if (in_stack) {
802 _f->do_oop(addr);
803 }
804 }
805 }
806 };
807
808
809 class InterpretedArgumentOopFinder: public SignatureIterator {
810 private:
811 OopClosure* _f; // Closure to invoke
812 int _offset; // TOS-relative offset, decremented with each argument
813 bool _has_receiver; // true if the callee has a receiver
814 const frame* _fr;
815
816 friend class SignatureIterator; // so do_parameters_on can call do_type
817 void do_type(BasicType type) {
818 _offset -= parameter_type_word_count(type);
819 if (is_reference_type(type)) oop_offset_do();
820 }
821
822 void oop_offset_do() {
823 oop* addr;
824 addr = (oop*)_fr->interpreter_frame_tos_at(_offset);
825 _f->do_oop(addr);
826 }
827
828 public:
829 InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, const frame* fr, OopClosure* f) : SignatureIterator(signature), _has_receiver(has_receiver) {
830 // compute size of arguments
831 int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);
832 assert(!fr->is_interpreted_frame() ||
833 args_size <= fr->interpreter_frame_expression_stack_size(),
834 "args cannot be on stack anymore");
835 // initialize InterpretedArgumentOopFinder
836 _f = f;
837 _fr = fr;
838 _offset = args_size;
839 }
840
841 void oops_do() {
842 if (_has_receiver) {
843 --_offset;
844 oop_offset_do();
845 }
846 do_parameters_on(this);
847 }
848 };
849
850
851 // Entry frame has following form (n arguments)
852 // +-----------+
853 // sp -> | last arg |
854 // +-----------+
855 // : ::: :
856 // +-----------+
857 // (sp+n)->| first arg|
858 // +-----------+
859
860
861
862 // visits and GC's all the arguments in entry frame
863 class EntryFrameOopFinder: public SignatureIterator {
864 private:
865 bool _is_static;
866 int _offset;
867 const frame* _fr;
868 OopClosure* _f;
869
870 friend class SignatureIterator; // so do_parameters_on can call do_type
871 void do_type(BasicType type) {
872 // decrement offset before processing the type
873 _offset -= parameter_type_word_count(type);
874 assert (_offset >= 0, "illegal offset");
875 if (is_reference_type(type)) oop_at_offset_do(_offset);
876 }
877
878 void oop_at_offset_do(int offset) {
879 assert (offset >= 0, "illegal offset");
880 oop* addr = (oop*) _fr->entry_frame_argument_at(offset);
881 _f->do_oop(addr);
882 }
883
884 public:
885 EntryFrameOopFinder(const frame* frame, Symbol* signature, bool is_static) : SignatureIterator(signature) {
886 _f = nullptr; // will be set later
887 _fr = frame;
888 _is_static = is_static;
889 _offset = ArgumentSizeComputer(signature).size(); // pre-decremented down to zero
890 }
891
892 void arguments_do(OopClosure* f) {
893 _f = f;
894 if (!_is_static) oop_at_offset_do(_offset); // do the receiver
895 do_parameters_on(this);
896 }
897
898 };
899
900 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
901 ArgumentSizeComputer asc(signature);
902 int size = asc.size();
903 return (oop *)interpreter_frame_tos_at(size);
904 }
905
906 oop frame::interpreter_callee_receiver(Symbol* signature) {
907 return *interpreter_callee_receiver_addr(signature);
908 }
909
910 template <typename RegisterMapT>
911 void frame::oops_interpreted_do(OopClosure* f, const RegisterMapT* map, bool query_oop_map_cache) const {
912 assert(is_interpreted_frame(), "Not an interpreted frame");
913 Thread *thread = Thread::current();
914 methodHandle m (thread, interpreter_frame_method());
915 jint bci = interpreter_frame_bci();
916
917 assert(!Universe::heap()->is_in(m()),
918 "must be valid oop");
919 assert(m->is_method(), "checking frame value");
920 assert((m->is_native() && bci == 0) ||
921 (!m->is_native() && bci >= 0 && bci < m->code_size()),
922 "invalid bci value");
923
924 // Handle the monitor elements in the activation
925 for (
926 BasicObjectLock* current = interpreter_frame_monitor_end();
927 current < interpreter_frame_monitor_begin();
928 current = next_monitor_in_interpreter_frame(current)
929 ) {
930 #ifdef ASSERT
931 interpreter_frame_verify_monitor(current);
932 #endif
933 current->oops_do(f);
934 }
935
936 if (m->is_native()) {
937 f->do_oop(interpreter_frame_temp_oop_addr());
938 }
939
940 // The method pointer in the frame might be the only path to the method's
941 // klass, and the klass needs to be kept alive while executing. The GCs
942 // don't trace through method pointers, so the mirror of the method's klass
943 // is installed as a GC root.
944 f->do_oop(interpreter_frame_mirror_addr());
945
946 int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
947
948 // Process a callee's arguments if we are at a call site
949 // (i.e., if we are at an invoke bytecode)
950 // This is used sometimes for calling into the VM, not for another
951 // interpreted or compiled frame.
952 if (!m->is_native() && map != nullptr && map->include_argument_oops()) {
953 Bytecode_invoke call = Bytecode_invoke_check(m, bci);
954 if (call.is_valid() && interpreter_frame_expression_stack_size() > 0) {
955 ResourceMark rm(thread); // is this right ???
956 Symbol* signature = call.signature();
957 bool has_receiver = call.has_receiver();
958 // We are at a call site & the expression stack is not empty
959 // so we might have callee arguments we need to process.
960 oops_interpreted_arguments_do(signature, has_receiver, f);
961 }
962 }
963
964 InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
965
966 // process locals & expression stack
967 InterpreterOopMap mask;
968 if (query_oop_map_cache) {
969 m->mask_for(m, bci, &mask);
970 } else {
971 OopMapCache::compute_one_oop_map(m, bci, &mask);
972 }
973 mask.iterate_oop(&blk);
974 }
975
976 template void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const;
977 template void frame::oops_interpreted_do(OopClosure* f, const SmallRegisterMapNoArgs* map, bool query_oop_map_cache) const;
978 template void frame::oops_interpreted_do(OopClosure* f, const SmallRegisterMapWithArgs* map, bool query_oop_map_cache) const;
979
980 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const {
981 InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
982 finder.oops_do();
983 }
984
985 void frame::oops_nmethod_do(OopClosure* f, NMethodClosure* cf, DerivedOopClosure* df, DerivedPointerIterationMode derived_mode, const RegisterMap* reg_map) const {
986 assert(_cb != nullptr, "sanity check");
987 assert((oop_map() == nullptr) == (_cb->oop_maps() == nullptr), "frame and _cb must agree that oopmap is set or not");
988 if (oop_map() != nullptr) {
989 if (df != nullptr) {
990 _oop_map->oops_do(this, reg_map, f, df);
991 } else {
992 _oop_map->oops_do(this, reg_map, f, derived_mode);
993 }
994
995 // Preserve potential arguments for a callee. We handle this by dispatching
996 // on the codeblob. For c2i, we do
997 if (reg_map->include_argument_oops() && _cb->is_nmethod()) {
998 // Only nmethod preserves outgoing arguments at call.
999 _cb->as_nmethod()->preserve_callee_argument_oops(*this, reg_map, f);
1000 }
1001 }
1002 // In cases where perm gen is collected, GC will want to mark
1003 // oops referenced from nmethods active on thread stacks so as to
1004 // prevent them from being collected. However, this visit should be
1005 // restricted to certain phases of the collection only. The
1006 // closure decides how it wants nmethods to be traced.
1007 if (cf != nullptr && _cb->is_nmethod())
1008 cf->do_nmethod(_cb->as_nmethod());
1009 }
1010
1011 class CompiledArgumentOopFinder: public SignatureIterator {
1012 protected:
1013 OopClosure* _f;
1014 int _offset; // the current offset, incremented with each argument
1015 bool _has_receiver; // true if the callee has a receiver
1016 bool _has_appendix; // true if the call has an appendix
1017 frame _fr;
1018 RegisterMap* _reg_map;
1019 int _arg_size;
1020 VMRegPair* _regs; // VMReg list of arguments
1021
1022 friend class SignatureIterator; // so do_parameters_on can call do_type
1023 void do_type(BasicType type) {
1024 if (is_reference_type(type)) handle_oop_offset();
1025 _offset += parameter_type_word_count(type);
1026 }
1027
1028 virtual void handle_oop_offset() {
1029 // Extract low order register number from register array.
1030 // In LP64-land, the high-order bits are valid but unhelpful.
1031 VMReg reg = _regs[_offset].first();
1032 oop *loc = _fr.oopmapreg_to_oop_location(reg, _reg_map);
1033 #ifdef ASSERT
1034 if (loc == nullptr) {
1035 if (_reg_map->should_skip_missing()) {
1036 return;
1037 }
1038 tty->print_cr("Error walking frame oops:");
1039 _fr.print_on(tty);
1040 assert(loc != nullptr, "missing register map entry reg: %d %s loc: " INTPTR_FORMAT, reg->value(), reg->name(), p2i(loc));
1041 }
1042 #endif
1043 _f->do_oop(loc);
1044 }
1045
1046 public:
1047 CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)
1048 : SignatureIterator(signature) {
1049
1050 // initialize CompiledArgumentOopFinder
1051 _f = f;
1052 _offset = 0;
1053 _has_receiver = has_receiver;
1054 _has_appendix = has_appendix;
1055 _fr = fr;
1056 _reg_map = (RegisterMap*)reg_map;
1057 _arg_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
1058
1059 int arg_size;
1060 _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
1061 assert(arg_size == _arg_size, "wrong arg size");
1062 }
1063
1064 void oops_do() {
1065 if (_has_receiver) {
1066 handle_oop_offset();
1067 _offset++;
1068 }
1069 do_parameters_on(this);
1070 if (_has_appendix) {
1071 handle_oop_offset();
1072 _offset++;
1073 }
1074 }
1075 };
1076
1077 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,
1078 const RegisterMap* reg_map, OopClosure* f) const {
1079 // ResourceMark rm;
1080 CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
1081 finder.oops_do();
1082 }
1083
1084 // Get receiver out of callers frame, i.e. find parameter 0 in callers
1085 // frame. Consult ADLC for where parameter 0 is to be found. Then
1086 // check local reg_map for it being a callee-save register or argument
1087 // register, both of which are saved in the local frame. If not found
1088 // there, it must be an in-stack argument of the caller.
1089 // Note: caller.sp() points to callee-arguments
1090 oop frame::retrieve_receiver(RegisterMap* reg_map) {
1091 frame caller = *this;
1092
1093 // First consult the ADLC on where it puts parameter 0 for this signature.
1094 VMReg reg = SharedRuntime::name_for_receiver();
1095 oop* oop_adr = caller.oopmapreg_to_oop_location(reg, reg_map);
1096 if (oop_adr == nullptr) {
1097 guarantee(oop_adr != nullptr, "bad register save location");
1098 return nullptr;
1099 }
1100 oop r = *oop_adr;
1101 assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (%zd)", p2i(r), p2i(r));
1102 return r;
1103 }
1104
1105
1106 BasicLock* frame::get_native_monitor() const {
1107 nmethod* nm = (nmethod*)_cb;
1108 assert(_cb != nullptr && _cb->is_nmethod() && nm->method()->is_native(),
1109 "Should not call this unless it's a native nmethod");
1110 int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1111 assert(byte_offset >= 0, "should not see invalid offset");
1112 return (BasicLock*) &sp()[byte_offset / wordSize];
1113 }
1114
1115 oop frame::get_native_receiver() const {
1116 nmethod* nm = (nmethod*)_cb;
1117 assert(_cb != nullptr && _cb->is_nmethod() && nm->method()->is_native(),
1118 "Should not call this unless it's a native nmethod");
1119 int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1120 assert(byte_offset >= 0, "should not see invalid offset");
1121 oop owner = ((oop*) sp())[byte_offset / wordSize];
1122 assert( Universe::heap()->is_in(owner), "bad receiver" );
1123 return owner;
1124 }
1125
1126 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const {
1127 assert(map != nullptr, "map must be set");
1128 if (map->include_argument_oops()) {
1129 // must collect argument oops, as nobody else is doing it
1130 Thread *thread = Thread::current();
1131 methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1132 EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1133 finder.arguments_do(f);
1134 }
1135 // Traverse the Handle Block saved in the entry frame
1136 entry_frame_call_wrapper()->oops_do(f);
1137 }
1138
1139 void frame::oops_upcall_do(OopClosure* f, const RegisterMap* map) const {
1140 assert(map != nullptr, "map must be set");
1141 if (map->include_argument_oops()) {
1142 // Upcall stubs call a MethodHandle impl method of which only the receiver
1143 // is ever an oop.
1144 // Currently we should not be able to get here, since there are no
1145 // safepoints in the one resolve stub we can get into (handle_wrong_method)
1146 // Leave this here as a trap in case we ever do:
1147 ShouldNotReachHere(); // not implemented
1148 }
1149 _cb->as_upcall_stub()->oops_do(f, *this);
1150 }
1151
1152 void frame::oops_do_internal(OopClosure* f, NMethodClosure* cf,
1153 DerivedOopClosure* df, DerivedPointerIterationMode derived_mode,
1154 const RegisterMap* map, bool use_interpreter_oop_map_cache) const {
1155 #ifndef PRODUCT
1156 // simulate GC crash here to dump java thread in error report
1157 guarantee(!CrashGCForDumpingJavaThread, "");
1158 #endif
1159 if (is_interpreted_frame()) {
1160 oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
1161 } else if (is_entry_frame()) {
1162 oops_entry_do(f, map);
1163 } else if (is_upcall_stub_frame()) {
1164 oops_upcall_do(f, map);
1165 } else if (CodeCache::contains(pc())) {
1166 oops_nmethod_do(f, cf, df, derived_mode, map);
1167 } else {
1168 ShouldNotReachHere();
1169 }
1170 }
1171
1172 void frame::nmethod_do(NMethodClosure* cf) const {
1173 if (_cb != nullptr && _cb->is_nmethod()) {
1174 cf->do_nmethod(_cb->as_nmethod());
1175 }
1176 }
1177
1178
1179 // Call f closure on the interpreted Method*s in the stack.
1180 void frame::metadata_do(MetadataClosure* f) const {
1181 ResourceMark rm;
1182 if (is_interpreted_frame()) {
1183 Method* m = this->interpreter_frame_method();
1184 assert(m != nullptr, "expecting a method in this frame");
1185 f->do_metadata(m);
1186 }
1187 }
1188
1189 void frame::verify(const RegisterMap* map) const {
1190 #ifndef PRODUCT
1191 if (TraceCodeBlobStacks) {
1192 tty->print_cr("*** verify");
1193 print_on(tty);
1194 }
1195 #endif
1196
1197 // for now make sure receiver type is correct
1198 if (is_interpreted_frame()) {
1199 Method* method = interpreter_frame_method();
1200 guarantee(method->is_method(), "method is wrong in frame::verify");
1201 if (!method->is_static()) {
1202 // fetch the receiver
1203 oop* p = (oop*) interpreter_frame_local_at(0);
1204 // make sure we have the right receiver type
1205 }
1206 }
1207 #ifdef COMPILER2
1208 assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1209 #endif // COMPILER2
1210
1211 if (map->update_map()) { // The map has to be up-to-date for the current frame
1212 oops_do_internal(&VerifyOopClosure::verify_oop, nullptr, nullptr, DerivedPointerIterationMode::_ignore, map, false);
1213 }
1214 }
1215
1216
1217 #ifdef ASSERT
1218 bool frame::verify_return_pc(address x) {
1219 #ifdef TARGET_ARCH_aarch64
1220 if (!pauth_ptr_is_raw(x)) {
1221 return false;
1222 }
1223 #endif
1224 if (StubRoutines::returns_to_call_stub(x)) {
1225 return true;
1226 }
1227 if (CodeCache::contains(x)) {
1228 return true;
1229 }
1230 if (Interpreter::contains(x)) {
1231 return true;
1232 }
1233 return false;
1234 }
1235 #endif
1236
1237 #ifdef ASSERT
1238 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1239 assert(is_interpreted_frame(), "Not an interpreted frame");
1240 // verify that the value is in the right part of the frame
1241 address low_mark = (address) interpreter_frame_monitor_end();
1242 address high_mark = (address) interpreter_frame_monitor_begin();
1243 address current = (address) value;
1244
1245 const int monitor_size = frame::interpreter_frame_monitor_size();
1246 guarantee((high_mark - current) % monitor_size == 0 , "Misaligned top of BasicObjectLock*");
1247 guarantee( high_mark > current , "Current BasicObjectLock* higher than high_mark");
1248
1249 guarantee((current - low_mark) % monitor_size == 0 , "Misaligned bottom of BasicObjectLock*");
1250 guarantee( current >= low_mark , "Current BasicObjectLock* below than low_mark");
1251 }
1252 #endif
1253
1254 #ifndef PRODUCT
1255
1256 class FrameValuesOopClosure: public OopClosure, public DerivedOopClosure {
1257 private:
1258 GrowableArray<oop*>* _oops;
1259 GrowableArray<narrowOop*>* _narrow_oops;
1260 GrowableArray<derived_base*>* _base;
1261 GrowableArray<derived_pointer*>* _derived;
1262 NoSafepointVerifier nsv;
1263
1264 public:
1265 FrameValuesOopClosure() {
1266 _oops = new (mtThread) GrowableArray<oop*>(100, mtThread);
1267 _narrow_oops = new (mtThread) GrowableArray<narrowOop*>(100, mtThread);
1268 _base = new (mtThread) GrowableArray<derived_base*>(100, mtThread);
1269 _derived = new (mtThread) GrowableArray<derived_pointer*>(100, mtThread);
1270 }
1271 ~FrameValuesOopClosure() {
1272 delete _oops;
1273 delete _narrow_oops;
1274 delete _base;
1275 delete _derived;
1276 }
1277
1278 virtual void do_oop(oop* p) override { _oops->push(p); }
1279 virtual void do_oop(narrowOop* p) override { _narrow_oops->push(p); }
1280 virtual void do_derived_oop(derived_base* base_loc, derived_pointer* derived_loc) override {
1281 _base->push(base_loc);
1282 _derived->push(derived_loc);
1283 }
1284
1285 void describe(FrameValues& values, int frame_no) {
1286 for (int i = 0; i < _oops->length(); i++) {
1287 oop* p = _oops->at(i);
1288 values.describe(frame_no, (intptr_t*)p, err_msg("oop for #%d", frame_no));
1289 }
1290 for (int i = 0; i < _narrow_oops->length(); i++) {
1291 narrowOop* p = _narrow_oops->at(i);
1292 values.describe(frame_no, (intptr_t*)p, err_msg("narrow oop for #%d", frame_no));
1293 }
1294 assert(_base->length() == _derived->length(), "should be the same");
1295 for (int i = 0; i < _base->length(); i++) {
1296 derived_base* base = _base->at(i);
1297 derived_pointer* derived = _derived->at(i);
1298 values.describe(frame_no, (intptr_t*)derived, err_msg("derived pointer (base: " INTPTR_FORMAT ") for #%d", p2i(base), frame_no));
1299 }
1300 }
1301 };
1302
1303 class FrameValuesOopMapClosure: public OopMapClosure {
1304 private:
1305 const frame* _fr;
1306 const RegisterMap* _reg_map;
1307 FrameValues& _values;
1308 int _frame_no;
1309
1310 public:
1311 FrameValuesOopMapClosure(const frame* fr, const RegisterMap* reg_map, FrameValues& values, int frame_no)
1312 : _fr(fr), _reg_map(reg_map), _values(values), _frame_no(frame_no) {}
1313
1314 virtual void do_value(VMReg reg, OopMapValue::oop_types type) override {
1315 intptr_t* p = (intptr_t*)_fr->oopmapreg_to_location(reg, _reg_map);
1316 if (p != nullptr && (((intptr_t)p & WordAlignmentMask) == 0)) {
1317 const char* type_name = nullptr;
1318 switch(type) {
1319 case OopMapValue::oop_value: type_name = "oop"; break;
1320 case OopMapValue::narrowoop_value: type_name = "narrow oop"; break;
1321 case OopMapValue::callee_saved_value: type_name = "callee-saved"; break;
1322 case OopMapValue::derived_oop_value: type_name = "derived"; break;
1323 // case OopMapValue::live_value: type_name = "live"; break;
1324 default: break;
1325 }
1326 if (type_name != nullptr) {
1327 _values.describe(_frame_no, p, err_msg("%s for #%d", type_name, _frame_no));
1328 }
1329 }
1330 }
1331 };
1332
1333 // callers need a ResourceMark because of name_and_sig_as_C_string() usage,
1334 // RA allocated string is returned to the caller
1335 void frame::describe(FrameValues& values, int frame_no, const RegisterMap* reg_map, bool top) {
1336 // boundaries: sp and the 'real' frame pointer
1337 values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 0);
1338 if (top) {
1339 values.describe(-1, sp() - 1, err_msg("sp[-1] for #%d", frame_no), 0);
1340 values.describe(-1, sp() - 2, err_msg("sp[-2] for #%d", frame_no), 0);
1341 }
1342
1343 intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1344
1345 // print frame info at the highest boundary
1346 intptr_t* info_address = MAX2(sp(), frame_pointer);
1347
1348 if (info_address != frame_pointer) {
1349 // print frame_pointer explicitly if not marked by the frame info
1350 values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1351 }
1352
1353 if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1354 // Label values common to most frames
1355 values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no), 0);
1356 }
1357
1358 if (is_interpreted_frame()) {
1359 Method* m = interpreter_frame_method();
1360 int bci = interpreter_frame_bci();
1361 InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
1362
1363 // Label the method and current bci
1364 values.describe(-1, info_address,
1365 FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 3);
1366 if (desc != nullptr) {
1367 values.describe(-1, info_address, err_msg("- %s codelet: %s",
1368 desc->bytecode() >= 0 ? Bytecodes::name(desc->bytecode()) : "",
1369 desc->description() != nullptr ? desc->description() : "?"), 2);
1370 }
1371 values.describe(-1, info_address,
1372 err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 2);
1373 // return address will be emitted by caller in describe_pd
1374 // values.describe(frame_no, (intptr_t*)sender_pc_addr(), Continuation::is_return_barrier_entry(*sender_pc_addr()) ? "return address (return barrier)" : "return address");
1375
1376 if (m->max_locals() > 0) {
1377 intptr_t* l0 = interpreter_frame_local_at(0);
1378 intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1379 values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 2);
1380 // Report each local and mark as owned by this frame
1381 for (int l = 0; l < m->max_locals(); l++) {
1382 intptr_t* l0 = interpreter_frame_local_at(l);
1383 values.describe(frame_no, l0, err_msg("local %d", l), 1);
1384 }
1385 }
1386
1387 if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1388 values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1389 values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1390 }
1391
1392 // Compute the actual expression stack size
1393 InterpreterOopMap mask;
1394 OopMapCache::compute_one_oop_map(methodHandle(Thread::current(), m), bci, &mask);
1395 intptr_t* tos = nullptr;
1396 // Report each stack element and mark as owned by this frame
1397 for (int e = 0; e < mask.expression_stack_size(); e++) {
1398 tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1399 values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1400 err_msg("stack %d", e), 1);
1401 }
1402 if (tos != nullptr) {
1403 values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 2);
1404 }
1405
1406 if (reg_map != nullptr) {
1407 FrameValuesOopClosure oopsFn;
1408 oops_do(&oopsFn, nullptr, &oopsFn, reg_map);
1409 oopsFn.describe(values, frame_no);
1410 }
1411 } else if (is_entry_frame()) {
1412 // For now just label the frame
1413 values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1414 } else if (is_compiled_frame()) {
1415 // For now just label the frame
1416 nmethod* nm = cb()->as_nmethod();
1417 values.describe(-1, info_address,
1418 FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method J %s%s", frame_no,
1419 p2i(nm),
1420 nm->method()->name_and_sig_as_C_string(),
1421 (_deopt_state == is_deoptimized) ?
1422 " (deoptimized)" :
1423 ((_deopt_state == unknown) ? " (state unknown)" : "")),
1424 3);
1425
1426 { // mark arguments (see nmethod::print_nmethod_labels)
1427 Method* m = nm->method();
1428
1429 int stack_slot_offset = nm->frame_size() * wordSize; // offset, in bytes, to caller sp
1430 int sizeargs = m->size_of_parameters();
1431
1432 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
1433 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
1434 {
1435 int sig_index = 0;
1436 if (!m->is_static()) {
1437 sig_bt[sig_index++] = T_OBJECT; // 'this'
1438 }
1439 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
1440 BasicType t = ss.type();
1441 assert(type2size[t] == 1 || type2size[t] == 2, "size is 1 or 2");
1442 sig_bt[sig_index++] = t;
1443 if (type2size[t] == 2) {
1444 sig_bt[sig_index++] = T_VOID;
1445 }
1446 }
1447 assert(sig_index == sizeargs, "");
1448 }
1449 int stack_arg_slots = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
1450 assert(stack_arg_slots == nm->as_nmethod()->num_stack_arg_slots(false /* rounded */) || nm->is_osr_method(), "");
1451 int out_preserve = SharedRuntime::out_preserve_stack_slots();
1452 int sig_index = 0;
1453 int arg_index = (m->is_static() ? 0 : -1);
1454 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
1455 bool at_this = (arg_index == -1);
1456 bool at_old_sp = false;
1457 BasicType t = (at_this ? T_OBJECT : ss.type());
1458 assert(t == sig_bt[sig_index], "sigs in sync");
1459 VMReg fst = regs[sig_index].first();
1460 if (fst->is_stack()) {
1461 assert(((int)fst->reg2stack()) >= 0, "reg2stack: %d", fst->reg2stack());
1462 int offset = (fst->reg2stack() + out_preserve) * VMRegImpl::stack_slot_size + stack_slot_offset;
1463 intptr_t* stack_address = (intptr_t*)((address)unextended_sp() + offset);
1464 if (at_this) {
1465 values.describe(frame_no, stack_address, err_msg("this for #%d", frame_no), 1);
1466 } else {
1467 values.describe(frame_no, stack_address, err_msg("param %d %s for #%d", arg_index, type2name(t), frame_no), 1);
1468 }
1469 }
1470 sig_index += type2size[t];
1471 arg_index += 1;
1472 if (!at_this) {
1473 ss.next();
1474 }
1475 }
1476 }
1477
1478 if (reg_map != nullptr && is_java_frame()) {
1479 int scope_no = 0;
1480 for (ScopeDesc* scope = nm->scope_desc_at(pc()); scope != nullptr; scope = scope->sender(), scope_no++) {
1481 Method* m = scope->method();
1482 int bci = scope->bci();
1483 values.describe(-1, info_address, err_msg("- #%d scope %s @ %d", scope_no, m->name_and_sig_as_C_string(), bci), 2);
1484
1485 { // mark locals
1486 GrowableArray<ScopeValue*>* scvs = scope->locals();
1487 int scvs_length = scvs != nullptr ? scvs->length() : 0;
1488 for (int i = 0; i < scvs_length; i++) {
1489 intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i));
1490 if (stack_address != nullptr) {
1491 values.describe(frame_no, stack_address, err_msg("local %d for #%d (scope %d)", i, frame_no, scope_no), 1);
1492 }
1493 }
1494 }
1495 { // mark expression stack
1496 GrowableArray<ScopeValue*>* scvs = scope->expressions();
1497 int scvs_length = scvs != nullptr ? scvs->length() : 0;
1498 for (int i = 0; i < scvs_length; i++) {
1499 intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i));
1500 if (stack_address != nullptr) {
1501 values.describe(frame_no, stack_address, err_msg("stack %d for #%d (scope %d)", i, frame_no, scope_no), 1);
1502 }
1503 }
1504 }
1505 }
1506
1507 FrameValuesOopClosure oopsFn;
1508 oops_do(&oopsFn, nullptr, &oopsFn, reg_map);
1509 oopsFn.describe(values, frame_no);
1510
1511 if (oop_map() != nullptr) {
1512 FrameValuesOopMapClosure valuesFn(this, reg_map, values, frame_no);
1513 // also OopMapValue::live_value ??
1514 oop_map()->all_type_do(this, OopMapValue::callee_saved_value, &valuesFn);
1515 }
1516 }
1517 } else if (is_native_frame()) {
1518 // For now just label the frame
1519 nmethod* nm = cb()->as_nmethod_or_null();
1520 values.describe(-1, info_address,
1521 FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1522 p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);
1523 if (nm->method()->is_continuation_enter_intrinsic()) {
1524 ContinuationEntry* ce = Continuation::get_continuation_entry_for_entry_frame(reg_map->thread(), *this); // (ContinuationEntry*)unextended_sp();
1525 ce->describe(values, frame_no);
1526 }
1527 } else {
1528 // provide default info if not handled before
1529 char *info = (char *) "special frame";
1530 if ((_cb != nullptr) &&
1531 (_cb->name() != nullptr)) {
1532 info = (char *)_cb->name();
1533 }
1534 values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1535 }
1536
1537 // platform dependent additional data
1538 describe_pd(values, frame_no);
1539 }
1540
1541 #endif
1542
1543 /**
1544 * Gets the caller frame of `fr` for thread `t`.
1545 *
1546 * @returns an invalid frame (i.e. fr.pc() === 0) if the caller cannot be obtained
1547 */
1548 frame frame::next_frame(frame fr, Thread* t) {
1549 // Compiled code may use EBP register on x86 so it looks like
1550 // non-walkable C frame. Use frame.sender() for java frames.
1551 frame invalid;
1552 if (t != nullptr && t->is_Java_thread()) {
1553 // Catch very first native frame by using stack address.
1554 // For JavaThread stack_base and stack_size should be set.
1555 if (!t->is_in_full_stack((address)(fr.real_fp() + 1))) {
1556 return invalid;
1557 }
1558 if (fr.is_interpreted_frame() || (fr.cb() != nullptr && fr.cb()->frame_size() > 0)) {
1559 RegisterMap map(JavaThread::cast(t),
1560 RegisterMap::UpdateMap::skip,
1561 RegisterMap::ProcessFrames::include,
1562 RegisterMap::WalkContinuation::skip); // No update
1563 return fr.sender(&map);
1564 } else {
1565 // is_first_C_frame() does only simple checks for frame pointer,
1566 // it will pass if java compiled code has a pointer in EBP.
1567 if (os::is_first_C_frame(&fr)) return invalid;
1568 return os::get_sender_for_C_frame(&fr);
1569 }
1570 } else {
1571 if (os::is_first_C_frame(&fr)) return invalid;
1572 return os::get_sender_for_C_frame(&fr);
1573 }
1574 }
1575
1576 #ifndef PRODUCT
1577
1578 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1579 FrameValue fv;
1580 fv.location = location;
1581 fv.owner = owner;
1582 fv.priority = priority;
1583 fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1584 strcpy(fv.description, description);
1585 _values.append(fv);
1586 }
1587
1588
1589 #ifdef ASSERT
1590 void FrameValues::validate() {
1591 _values.sort(compare);
1592 bool error = false;
1593 FrameValue prev;
1594 prev.owner = -1;
1595 for (int i = _values.length() - 1; i >= 0; i--) {
1596 FrameValue fv = _values.at(i);
1597 if (fv.owner == -1) continue;
1598 if (prev.owner == -1) {
1599 prev = fv;
1600 continue;
1601 }
1602 if (prev.location == fv.location) {
1603 if (fv.owner != prev.owner) {
1604 tty->print_cr("overlapping storage");
1605 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(prev.location), *prev.location, prev.description);
1606 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1607 error = true;
1608 }
1609 } else {
1610 prev = fv;
1611 }
1612 }
1613 // if (error) { tty->cr(); print_on(static_cast<JavaThread*>(nullptr), tty); }
1614 assert(!error, "invalid layout");
1615 }
1616 #endif // ASSERT
1617
1618 void FrameValues::print_on(JavaThread* thread, outputStream* st) {
1619 _values.sort(compare);
1620
1621 // Sometimes values like the fp can be invalid values if the
1622 // register map wasn't updated during the walk. Trim out values
1623 // that aren't actually in the stack of the thread.
1624 int min_index = 0;
1625 int max_index = _values.length() - 1;
1626 intptr_t* v0 = _values.at(min_index).location;
1627 intptr_t* v1 = _values.at(max_index).location;
1628
1629 if (thread != nullptr) {
1630 if (thread == Thread::current()) {
1631 while (!thread->is_in_live_stack((address)v0)) v0 = _values.at(++min_index).location;
1632 while (!thread->is_in_live_stack((address)v1)) v1 = _values.at(--max_index).location;
1633 } else {
1634 while (!thread->is_in_full_stack((address)v0)) v0 = _values.at(++min_index).location;
1635 while (!thread->is_in_full_stack((address)v1)) v1 = _values.at(--max_index).location;
1636 }
1637 }
1638
1639 print_on(st, min_index, max_index, v0, v1);
1640 }
1641
1642 void FrameValues::print_on(stackChunkOop chunk, outputStream* st) {
1643 _values.sort(compare);
1644
1645 intptr_t* start = chunk->start_address();
1646 intptr_t* end = chunk->end_address() + 1;
1647
1648 int min_index = 0;
1649 int max_index = _values.length() - 1;
1650 intptr_t* v0 = _values.at(min_index).location;
1651 intptr_t* v1 = _values.at(max_index).location;
1652 while (!(start <= v0 && v0 <= end)) v0 = _values.at(++min_index).location;
1653 while (!(start <= v1 && v1 <= end)) v1 = _values.at(--max_index).location;
1654
1655 print_on(st, min_index, max_index, v0, v1);
1656 }
1657
1658 void FrameValues::print_on(outputStream* st, int min_index, int max_index, intptr_t* v0, intptr_t* v1) {
1659 intptr_t* min = MIN2(v0, v1);
1660 intptr_t* max = MAX2(v0, v1);
1661 intptr_t* cur = max;
1662 intptr_t* last = nullptr;
1663 intptr_t* fp = nullptr;
1664 for (int i = max_index; i >= min_index; i--) {
1665 FrameValue fv = _values.at(i);
1666 while (cur > fv.location) {
1667 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, p2i(cur), *cur);
1668 cur--;
1669 }
1670 if (last == fv.location) {
1671 const char* spacer = " " LP64_ONLY(" ");
1672 st->print_cr(" %s %s %s", spacer, spacer, fv.description);
1673 } else {
1674 if (*fv.description == '#' && isdigit(fv.description[1])) {
1675 // The fv.description string starting with a '#' is the line for the
1676 // saved frame pointer eg. "#10 method java.lang.invoke.LambdaForm..."
1677 // basicaly means frame 10.
1678 fp = fv.location;
1679 }
1680 // To print a fp-relative value:
1681 // 1. The content of *fv.location must be such that we think it's a
1682 // fp-relative number, i.e [-100..100].
1683 // 2. We must have found the frame pointer.
1684 // 3. The line can not be the line for the saved frame pointer.
1685 // 4. Recognize it as being part of the "fixed frame".
1686 if (*fv.location != 0 && *fv.location > -100 && *fv.location < 100
1687 && fp != nullptr && *fv.description != '#'
1688 #if !defined(PPC64)
1689 && (strncmp(fv.description, "interpreter_frame_", 18) == 0 || strstr(fv.description, " method "))
1690 #else // !defined(PPC64)
1691 && (strcmp(fv.description, "sender_sp") == 0 || strcmp(fv.description, "top_frame_sp") == 0 ||
1692 strcmp(fv.description, "esp") == 0 || strcmp(fv.description, "monitors") == 0 ||
1693 strcmp(fv.description, "locals") == 0 || strstr(fv.description, " method "))
1694 #endif //!defined(PPC64)
1695 ) {
1696 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %-32s (relativized: fp%+d)",
1697 p2i(fv.location), p2i(&fp[*fv.location]), fv.description, (int)*fv.location);
1698 } else {
1699 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1700 }
1701 last = fv.location;
1702 cur--;
1703 }
1704 }
1705 }
1706
1707 #endif // ndef PRODUCT