1 /*
2 * Copyright (c) 1997, 2025, 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_begin() - pc_return_offset;
210 } else {
211 return (pc() - pc_return_offset);
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_begin();
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 check = thread->last_frame();
370 if (is_older(check.id())) {
371 RegisterMap map(thread,
372 RegisterMap::UpdateMap::skip,
373 RegisterMap::ProcessFrames::include,
374 RegisterMap::WalkContinuation::skip);
375 while (id() != check.id()) {
376 check = check.sender(&map);
377 }
378 assert(check.is_deoptimized_frame(), "missed deopt");
379 }
380 }
381 #endif // ASSERT
382 }
383
384 frame frame::java_sender() const {
385 RegisterMap map(JavaThread::current(),
386 RegisterMap::UpdateMap::skip,
387 RegisterMap::ProcessFrames::include,
388 RegisterMap::WalkContinuation::skip);
389 frame s;
390 for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
391 guarantee(s.is_java_frame(), "tried to get caller of first java frame");
392 return s;
393 }
394
395 frame frame::real_sender(RegisterMap* map) const {
396 frame result = sender(map);
397 while (result.is_runtime_frame() ||
398 result.is_ignored_frame()) {
399 result = result.sender(map);
400 }
401 return result;
402 }
403
404 // Interpreter frames
405
406
407 Method* frame::interpreter_frame_method() const {
408 assert(is_interpreted_frame(), "interpreted frame expected");
409 Method* m = *interpreter_frame_method_addr();
410 assert(m->is_method(), "not a Method*");
411 return m;
412 }
413
414 void frame::interpreter_frame_set_method(Method* method) {
415 assert(is_interpreted_frame(), "interpreted frame expected");
416 *interpreter_frame_method_addr() = method;
417 }
418
419 void frame::interpreter_frame_set_mirror(oop mirror) {
420 assert(is_interpreted_frame(), "interpreted frame expected");
421 *interpreter_frame_mirror_addr() = mirror;
422 }
423
424 jint frame::interpreter_frame_bci() const {
425 assert(is_interpreted_frame(), "interpreted frame expected");
426 address bcp = interpreter_frame_bcp();
427 return interpreter_frame_method()->bci_from(bcp);
428 }
429
430 address frame::interpreter_frame_bcp() const {
431 assert(is_interpreted_frame(), "interpreted frame expected");
432 address bcp = (address)*interpreter_frame_bcp_addr();
433 return interpreter_frame_method()->bcp_from(bcp);
434 }
435
436 void frame::interpreter_frame_set_bcp(address bcp) {
437 assert(is_interpreted_frame(), "interpreted frame expected");
438 *interpreter_frame_bcp_addr() = (intptr_t)bcp;
439 }
440
441 address frame::interpreter_frame_mdp() const {
442 assert(ProfileInterpreter, "must be profiling interpreter");
443 assert(is_interpreted_frame(), "interpreted frame expected");
444 return (address)*interpreter_frame_mdp_addr();
445 }
446
447 void frame::interpreter_frame_set_mdp(address mdp) {
448 assert(is_interpreted_frame(), "interpreted frame expected");
449 assert(ProfileInterpreter, "must be profiling interpreter");
450 *interpreter_frame_mdp_addr() = (intptr_t)mdp;
451 }
452
453 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
454 assert(is_interpreted_frame(), "Not an interpreted frame");
455 #ifdef ASSERT
456 interpreter_frame_verify_monitor(current);
457 #endif
458 BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
459 return next;
460 }
461
462 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
463 assert(is_interpreted_frame(), "Not an interpreted frame");
464 #ifdef ASSERT
465 // // This verification needs to be checked before being enabled
466 // interpreter_frame_verify_monitor(current);
467 #endif
468 BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
469 return previous;
470 }
471
472 // Interpreter locals and expression stack locations.
473
474 intptr_t* frame::interpreter_frame_local_at(int index) const {
475 const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
476 intptr_t* first = interpreter_frame_locals();
477 return &(first[n]);
478 }
479
480 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
481 const int i = offset * interpreter_frame_expression_stack_direction();
482 const int n = i * Interpreter::stackElementWords;
483 return &(interpreter_frame_expression_stack()[n]);
484 }
485
486 jint frame::interpreter_frame_expression_stack_size() const {
487 // Number of elements on the interpreter expression stack
488 // Callers should span by stackElementWords
489 int element_size = Interpreter::stackElementWords;
490 size_t stack_size = 0;
491 if (frame::interpreter_frame_expression_stack_direction() < 0) {
492 stack_size = (interpreter_frame_expression_stack() -
493 interpreter_frame_tos_address() + 1)/element_size;
494 } else {
495 stack_size = (interpreter_frame_tos_address() -
496 interpreter_frame_expression_stack() + 1)/element_size;
497 }
498 assert(stack_size <= (size_t)max_jint, "stack size too big");
499 return (jint)stack_size;
500 }
501
502 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
503
504 const char* frame::print_name() const {
505 if (is_native_frame()) return "Native";
506 if (is_interpreted_frame()) return "Interpreted";
507 if (is_compiled_frame()) {
508 if (is_deoptimized_frame()) return "Deoptimized";
509 return "Compiled";
510 }
511 if (sp() == nullptr) return "Empty";
512 return "C";
513 }
514
515 void frame::print_value_on(outputStream* st) const {
516 NOT_PRODUCT(address begin = pc()-40;)
517 NOT_PRODUCT(address end = nullptr;)
518
519 st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), p2i(sp()), p2i(unextended_sp()));
520 if (sp() != nullptr)
521 st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT,
522 p2i(fp()), p2i(real_fp()), p2i(pc()));
523 st->print_cr(")");
524
525 if (StubRoutines::contains(pc())) {
526 StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
527 st->print("~Stub::%s", desc->name());
528 NOT_PRODUCT(begin = desc->begin(); end = desc->end();)
529 } else if (Interpreter::contains(pc())) {
530 InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
531 if (desc != nullptr) {
532 st->print("~");
533 desc->print_on(st);
534 NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)
535 } else {
536 st->print("~interpreter");
537 }
538 }
539
540 #ifndef PRODUCT
541 if (_cb != nullptr) {
542 st->print(" ");
543 _cb->print_value_on(st);
544 if (end == nullptr) {
545 begin = _cb->code_begin();
546 end = _cb->code_end();
547 }
548 }
549 if (WizardMode && Verbose) Disassembler::decode(begin, end);
550 #endif
551 }
552
553 void frame::print_on(outputStream* st) const {
554 print_value_on(st);
555 if (is_interpreted_frame()) {
556 interpreter_frame_print_on(st);
557 }
558 }
559
560 void frame::interpreter_frame_print_on(outputStream* st) const {
561 #ifndef PRODUCT
562 assert(is_interpreted_frame(), "Not an interpreted frame");
563 jint i;
564 for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
565 intptr_t x = *interpreter_frame_local_at(i);
566 st->print(" - local [" INTPTR_FORMAT "]", x);
567 st->fill_to(23);
568 st->print_cr("; #%d", i);
569 }
570 for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
571 intptr_t x = *interpreter_frame_expression_stack_at(i);
572 st->print(" - stack [" INTPTR_FORMAT "]", x);
573 st->fill_to(23);
574 st->print_cr("; #%d", i);
575 }
576 // locks for synchronization
577 for (BasicObjectLock* current = interpreter_frame_monitor_end();
578 current < interpreter_frame_monitor_begin();
579 current = next_monitor_in_interpreter_frame(current)) {
580 st->print(" - obj [%s", current->obj() == nullptr ? "null" : "");
581 oop obj = current->obj();
582 if (obj != nullptr) {
583 if (!is_heap_frame()) {
584 obj->print_value_on(st);
585 } else {
586 // Might be an invalid oop. We don't have the
587 // stackChunk to correct it so just print address.
588 st->print(INTPTR_FORMAT, p2i(obj));
589 }
590 }
591 st->print_cr("]");
592 st->print(" - lock [");
593 if (!is_heap_frame()) {
594 current->lock()->print_on(st, obj);
595 }
596 st->print_cr("]");
597 }
598 // monitor
599 st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin()));
600 // bcp
601 st->print(" - bcp [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp()));
602 st->fill_to(23);
603 st->print_cr("; @%d", interpreter_frame_bci());
604 // locals
605 st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at(0)));
606 // method
607 st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method()));
608 st->fill_to(23);
609 st->print("; ");
610 interpreter_frame_method()->print_name(st);
611 st->cr();
612 #endif
613 }
614
615 // Print whether the frame is in the VM or OS indicating a HotSpot problem.
616 // Otherwise, it's likely a bug in the native library that the Java code calls,
617 // hopefully indicating where to submit bugs.
618 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
619 // C/C++ frame
620 bool in_vm = os::address_is_in_vm(pc);
621 st->print(in_vm ? "V" : "C");
622
623 int offset;
624 bool found;
625
626 if (buf == nullptr || buflen < 1) return;
627 // libname
628 buf[0] = '\0';
629 found = os::dll_address_to_library_name(pc, buf, buflen, &offset);
630 if (found && buf[0] != '\0') {
631 // skip directory names
632 const char *p1, *p2;
633 p1 = buf;
634 int len = (int)strlen(os::file_separator());
635 while ((p2 = strstr(p1, os::file_separator())) != nullptr) p1 = p2 + len;
636 st->print(" [%s+0x%x]", p1, offset);
637 } else {
638 st->print(" " PTR_FORMAT, p2i(pc));
639 }
640
641 found = os::dll_address_to_function_name(pc, buf, buflen, &offset);
642 if (found) {
643 st->print(" %s+0x%x", buf, offset);
644 }
645 }
646
647 // frame::print_on_error() is called by fatal error handler. Notice that we may
648 // crash inside this function if stack frame is corrupted. The fatal error
649 // handler can catch and handle the crash. Here we assume the frame is valid.
650 //
651 // First letter indicates type of the frame:
652 // J: Java frame (compiled)
653 // j: Java frame (interpreted)
654 // A: Java frame (AOT compiled and loaded after class initialization)
655 // P: Java frame (AOT compiled and preloaded before class initialization)
656 // V: VM frame (C/C++)
657 // v: Other frames running VM generated code (e.g. stubs, adapters, etc.)
658 // C: C/C++ frame
659 //
660 // We don't need detailed frame type as that in frame::print_name(). "C"
661 // suggests the problem is in user lib; everything else is likely a VM bug.
662
663 void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {
664 if (_cb != nullptr) {
665 if (Interpreter::contains(pc())) {
666 Method* m = this->interpreter_frame_method();
667 if (m != nullptr) {
668 m->name_and_sig_as_C_string(buf, buflen);
669 st->print("j %s", buf);
670 st->print("+%d", this->interpreter_frame_bci());
671 ModuleEntry* module = m->method_holder()->module();
672 if (module->is_named()) {
673 module->name()->as_C_string(buf, buflen);
674 st->print(" %s", buf);
675 if (module->version() != nullptr) {
676 module->version()->as_C_string(buf, buflen);
677 st->print("@%s", buf);
678 }
679 }
680 } else {
681 st->print("j " PTR_FORMAT, p2i(pc()));
682 }
683 } else if (StubRoutines::contains(pc())) {
684 StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
685 if (desc != nullptr) {
686 st->print("v ~StubRoutines::%s " PTR_FORMAT, desc->name(), p2i(pc()));
687 } else {
688 st->print("v ~StubRoutines::" PTR_FORMAT, p2i(pc()));
689 }
690 } else if (_cb->is_buffer_blob()) {
691 st->print("v ~BufferBlob::%s " PTR_FORMAT, ((BufferBlob *)_cb)->name(), p2i(pc()));
692 } else if (_cb->is_nmethod()) {
693 nmethod* nm = _cb->as_nmethod();
694 Method* m = nm->method();
695 if (m != nullptr) {
696 st->print("%s", (nm->preloaded() ? "P" : (nm->is_aot() ? "A" : "J")));
697 st->print(" %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : ""));
698 st->print(" %s", nm->compiler_name());
699 m->name_and_sig_as_C_string(buf, buflen);
700 st->print(" %s", buf);
701 ModuleEntry* module = m->method_holder()->module();
702 if (module->is_named()) {
703 module->name()->as_C_string(buf, buflen);
704 st->print(" %s", buf);
705 if (module->version() != nullptr) {
706 module->version()->as_C_string(buf, buflen);
707 st->print("@%s", buf);
708 }
709 }
710 st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]",
711 m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin());
712 #if INCLUDE_JVMCI
713 const char* jvmciName = nm->jvmci_name();
714 if (jvmciName != nullptr) {
715 st->print(" (%s)", jvmciName);
716 }
717 #endif
718 } else {
719 st->print("J " PTR_FORMAT, p2i(pc()));
720 }
721 } else if (_cb->is_runtime_stub()) {
722 st->print("v ~RuntimeStub::%s " PTR_FORMAT, ((RuntimeStub *)_cb)->name(), p2i(pc()));
723 } else if (_cb->is_deoptimization_stub()) {
724 st->print("v ~DeoptimizationBlob " PTR_FORMAT, p2i(pc()));
725 } else if (_cb->is_exception_stub()) {
726 st->print("v ~ExceptionBlob " PTR_FORMAT, p2i(pc()));
727 } else if (_cb->is_safepoint_stub()) {
728 st->print("v ~SafepointBlob " PTR_FORMAT, p2i(pc()));
729 } else if (_cb->is_adapter_blob()) {
730 st->print("v ~AdapterBlob " PTR_FORMAT, p2i(pc()));
731 } else if (_cb->is_vtable_blob()) {
732 st->print("v ~VtableBlob " PTR_FORMAT, p2i(pc()));
733 } else if (_cb->is_method_handles_adapter_blob()) {
734 st->print("v ~MethodHandlesAdapterBlob " PTR_FORMAT, p2i(pc()));
735 } else if (_cb->is_uncommon_trap_stub()) {
736 st->print("v ~UncommonTrapBlob " PTR_FORMAT, p2i(pc()));
737 } else if (_cb->is_upcall_stub()) {
738 st->print("v ~UpcallStub::%s " PTR_FORMAT, _cb->name(), p2i(pc()));
739 } else {
740 st->print("v blob " PTR_FORMAT, p2i(pc()));
741 }
742 } else {
743 print_C_frame(st, buf, buflen, pc());
744 }
745 }
746
747
748 /*
749 The interpreter_frame_expression_stack_at method in the case of SPARC needs the
750 max_stack value of the method in order to compute the expression stack address.
751 It uses the Method* in order to get the max_stack value but during GC this
752 Method* value saved on the frame is changed by reverse_and_push and hence cannot
753 be used. So we save the max_stack value in the FrameClosure object and pass it
754 down to the interpreter_frame_expression_stack_at method
755 */
756 class InterpreterFrameClosure : public OffsetClosure {
757 private:
758 const frame* _fr;
759 OopClosure* _f;
760 int _max_locals;
761 int _max_stack;
762
763 public:
764 InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack,
765 OopClosure* f) {
766 _fr = fr;
767 _max_locals = max_locals;
768 _max_stack = max_stack;
769 _f = f;
770 }
771
772 void offset_do(int offset) {
773 oop* addr;
774 if (offset < _max_locals) {
775 addr = (oop*) _fr->interpreter_frame_local_at(offset);
776 assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
777 _f->do_oop(addr);
778 } else {
779 addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
780 // In case of exceptions, the expression stack is invalid and the esp will be reset to express
781 // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
782 bool in_stack;
783 if (frame::interpreter_frame_expression_stack_direction() > 0) {
784 in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
785 } else {
786 in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
787 }
788 if (in_stack) {
789 _f->do_oop(addr);
790 }
791 }
792 }
793 };
794
795
796 class InterpretedArgumentOopFinder: public SignatureIterator {
797 private:
798 OopClosure* _f; // Closure to invoke
799 int _offset; // TOS-relative offset, decremented with each argument
800 bool _has_receiver; // true if the callee has a receiver
801 const frame* _fr;
802
803 friend class SignatureIterator; // so do_parameters_on can call do_type
804 void do_type(BasicType type) {
805 _offset -= parameter_type_word_count(type);
806 if (is_reference_type(type)) oop_offset_do();
807 }
808
809 void oop_offset_do() {
810 oop* addr;
811 addr = (oop*)_fr->interpreter_frame_tos_at(_offset);
812 _f->do_oop(addr);
813 }
814
815 public:
816 InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, const frame* fr, OopClosure* f) : SignatureIterator(signature), _has_receiver(has_receiver) {
817 // compute size of arguments
818 int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);
819 assert(!fr->is_interpreted_frame() ||
820 args_size <= fr->interpreter_frame_expression_stack_size(),
821 "args cannot be on stack anymore");
822 // initialize InterpretedArgumentOopFinder
823 _f = f;
824 _fr = fr;
825 _offset = args_size;
826 }
827
828 void oops_do() {
829 if (_has_receiver) {
830 --_offset;
831 oop_offset_do();
832 }
833 do_parameters_on(this);
834 }
835 };
836
837
838 // Entry frame has following form (n arguments)
839 // +-----------+
840 // sp -> | last arg |
841 // +-----------+
842 // : ::: :
843 // +-----------+
844 // (sp+n)->| first arg|
845 // +-----------+
846
847
848
849 // visits and GC's all the arguments in entry frame
850 class EntryFrameOopFinder: public SignatureIterator {
851 private:
852 bool _is_static;
853 int _offset;
854 const frame* _fr;
855 OopClosure* _f;
856
857 friend class SignatureIterator; // so do_parameters_on can call do_type
858 void do_type(BasicType type) {
859 // decrement offset before processing the type
860 _offset -= parameter_type_word_count(type);
861 assert (_offset >= 0, "illegal offset");
862 if (is_reference_type(type)) oop_at_offset_do(_offset);
863 }
864
865 void oop_at_offset_do(int offset) {
866 assert (offset >= 0, "illegal offset");
867 oop* addr = (oop*) _fr->entry_frame_argument_at(offset);
868 _f->do_oop(addr);
869 }
870
871 public:
872 EntryFrameOopFinder(const frame* frame, Symbol* signature, bool is_static) : SignatureIterator(signature) {
873 _f = nullptr; // will be set later
874 _fr = frame;
875 _is_static = is_static;
876 _offset = ArgumentSizeComputer(signature).size(); // pre-decremented down to zero
877 }
878
879 void arguments_do(OopClosure* f) {
880 _f = f;
881 if (!_is_static) oop_at_offset_do(_offset); // do the receiver
882 do_parameters_on(this);
883 }
884
885 };
886
887 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
888 ArgumentSizeComputer asc(signature);
889 int size = asc.size();
890 return (oop *)interpreter_frame_tos_at(size);
891 }
892
893 oop frame::interpreter_callee_receiver(Symbol* signature) {
894 return *interpreter_callee_receiver_addr(signature);
895 }
896
897 template <typename RegisterMapT>
898 void frame::oops_interpreted_do(OopClosure* f, const RegisterMapT* map, bool query_oop_map_cache) const {
899 assert(is_interpreted_frame(), "Not an interpreted frame");
900 Thread *thread = Thread::current();
901 methodHandle m (thread, interpreter_frame_method());
902 jint bci = interpreter_frame_bci();
903
904 assert(!Universe::heap()->is_in(m()),
905 "must be valid oop");
906 assert(m->is_method(), "checking frame value");
907 assert((m->is_native() && bci == 0) ||
908 (!m->is_native() && bci >= 0 && bci < m->code_size()),
909 "invalid bci value");
910
911 // Handle the monitor elements in the activation
912 for (
913 BasicObjectLock* current = interpreter_frame_monitor_end();
914 current < interpreter_frame_monitor_begin();
915 current = next_monitor_in_interpreter_frame(current)
916 ) {
917 #ifdef ASSERT
918 interpreter_frame_verify_monitor(current);
919 #endif
920 current->oops_do(f);
921 }
922
923 if (m->is_native()) {
924 f->do_oop(interpreter_frame_temp_oop_addr());
925 }
926
927 // The method pointer in the frame might be the only path to the method's
928 // klass, and the klass needs to be kept alive while executing. The GCs
929 // don't trace through method pointers, so the mirror of the method's klass
930 // is installed as a GC root.
931 f->do_oop(interpreter_frame_mirror_addr());
932
933 int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
934
935 // Process a callee's arguments if we are at a call site
936 // (i.e., if we are at an invoke bytecode)
937 // This is used sometimes for calling into the VM, not for another
938 // interpreted or compiled frame.
939 if (!m->is_native() && map != nullptr && map->include_argument_oops()) {
940 Bytecode_invoke call = Bytecode_invoke_check(m, bci);
941 if (call.is_valid() && interpreter_frame_expression_stack_size() > 0) {
942 ResourceMark rm(thread); // is this right ???
943 Symbol* signature = call.signature();
944 bool has_receiver = call.has_receiver();
945 // We are at a call site & the expression stack is not empty
946 // so we might have callee arguments we need to process.
947 oops_interpreted_arguments_do(signature, has_receiver, f);
948 }
949 }
950
951 InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
952
953 // process locals & expression stack
954 InterpreterOopMap mask;
955 if (query_oop_map_cache) {
956 m->mask_for(m, bci, &mask);
957 } else {
958 OopMapCache::compute_one_oop_map(m, bci, &mask);
959 }
960 mask.iterate_oop(&blk);
961 }
962
963 template void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const;
964 template void frame::oops_interpreted_do(OopClosure* f, const SmallRegisterMapNoArgs* map, bool query_oop_map_cache) const;
965 template void frame::oops_interpreted_do(OopClosure* f, const SmallRegisterMapWithArgs* map, bool query_oop_map_cache) const;
966
967 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const {
968 InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
969 finder.oops_do();
970 }
971
972 void frame::oops_nmethod_do(OopClosure* f, NMethodClosure* cf, DerivedOopClosure* df, DerivedPointerIterationMode derived_mode, const RegisterMap* reg_map) const {
973 assert(_cb != nullptr, "sanity check");
974 assert((oop_map() == nullptr) == (_cb->oop_maps() == nullptr), "frame and _cb must agree that oopmap is set or not");
975 if (oop_map() != nullptr) {
976 if (df != nullptr) {
977 _oop_map->oops_do(this, reg_map, f, df);
978 } else {
979 _oop_map->oops_do(this, reg_map, f, derived_mode);
980 }
981
982 // Preserve potential arguments for a callee. We handle this by dispatching
983 // on the codeblob. For c2i, we do
984 if (reg_map->include_argument_oops() && _cb->is_nmethod()) {
985 // Only nmethod preserves outgoing arguments at call.
986 _cb->as_nmethod()->preserve_callee_argument_oops(*this, reg_map, f);
987 }
988 }
989 // In cases where perm gen is collected, GC will want to mark
990 // oops referenced from nmethods active on thread stacks so as to
991 // prevent them from being collected. However, this visit should be
992 // restricted to certain phases of the collection only. The
993 // closure decides how it wants nmethods to be traced.
994 if (cf != nullptr && _cb->is_nmethod())
995 cf->do_nmethod(_cb->as_nmethod());
996 }
997
998 class CompiledArgumentOopFinder: public SignatureIterator {
999 protected:
1000 OopClosure* _f;
1001 int _offset; // the current offset, incremented with each argument
1002 bool _has_receiver; // true if the callee has a receiver
1003 bool _has_appendix; // true if the call has an appendix
1004 frame _fr;
1005 RegisterMap* _reg_map;
1006 int _arg_size;
1007 VMRegPair* _regs; // VMReg list of arguments
1008
1009 friend class SignatureIterator; // so do_parameters_on can call do_type
1010 void do_type(BasicType type) {
1011 if (is_reference_type(type)) handle_oop_offset();
1012 _offset += parameter_type_word_count(type);
1013 }
1014
1015 virtual void handle_oop_offset() {
1016 // Extract low order register number from register array.
1017 // In LP64-land, the high-order bits are valid but unhelpful.
1018 VMReg reg = _regs[_offset].first();
1019 oop *loc = _fr.oopmapreg_to_oop_location(reg, _reg_map);
1020 #ifdef ASSERT
1021 if (loc == nullptr) {
1022 if (_reg_map->should_skip_missing()) {
1023 return;
1024 }
1025 tty->print_cr("Error walking frame oops:");
1026 _fr.print_on(tty);
1027 assert(loc != nullptr, "missing register map entry reg: %d %s loc: " INTPTR_FORMAT, reg->value(), reg->name(), p2i(loc));
1028 }
1029 #endif
1030 _f->do_oop(loc);
1031 }
1032
1033 public:
1034 CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)
1035 : SignatureIterator(signature) {
1036
1037 // initialize CompiledArgumentOopFinder
1038 _f = f;
1039 _offset = 0;
1040 _has_receiver = has_receiver;
1041 _has_appendix = has_appendix;
1042 _fr = fr;
1043 _reg_map = (RegisterMap*)reg_map;
1044 _arg_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
1045
1046 int arg_size;
1047 _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
1048 assert(arg_size == _arg_size, "wrong arg size");
1049 }
1050
1051 void oops_do() {
1052 if (_has_receiver) {
1053 handle_oop_offset();
1054 _offset++;
1055 }
1056 do_parameters_on(this);
1057 if (_has_appendix) {
1058 handle_oop_offset();
1059 _offset++;
1060 }
1061 }
1062 };
1063
1064 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,
1065 const RegisterMap* reg_map, OopClosure* f) const {
1066 // ResourceMark rm;
1067 CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
1068 finder.oops_do();
1069 }
1070
1071 // Get receiver out of callers frame, i.e. find parameter 0 in callers
1072 // frame. Consult ADLC for where parameter 0 is to be found. Then
1073 // check local reg_map for it being a callee-save register or argument
1074 // register, both of which are saved in the local frame. If not found
1075 // there, it must be an in-stack argument of the caller.
1076 // Note: caller.sp() points to callee-arguments
1077 oop frame::retrieve_receiver(RegisterMap* reg_map) {
1078 frame caller = *this;
1079
1080 // First consult the ADLC on where it puts parameter 0 for this signature.
1081 VMReg reg = SharedRuntime::name_for_receiver();
1082 oop* oop_adr = caller.oopmapreg_to_oop_location(reg, reg_map);
1083 if (oop_adr == nullptr) {
1084 guarantee(oop_adr != nullptr, "bad register save location");
1085 return nullptr;
1086 }
1087 oop r = *oop_adr;
1088 assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (%zd)", p2i(r), p2i(r));
1089 return r;
1090 }
1091
1092
1093 BasicLock* frame::get_native_monitor() const {
1094 nmethod* nm = (nmethod*)_cb;
1095 assert(_cb != nullptr && _cb->is_nmethod() && nm->method()->is_native(),
1096 "Should not call this unless it's a native nmethod");
1097 int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1098 assert(byte_offset >= 0, "should not see invalid offset");
1099 return (BasicLock*) &sp()[byte_offset / wordSize];
1100 }
1101
1102 oop frame::get_native_receiver() const {
1103 nmethod* nm = (nmethod*)_cb;
1104 assert(_cb != nullptr && _cb->is_nmethod() && nm->method()->is_native(),
1105 "Should not call this unless it's a native nmethod");
1106 int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1107 assert(byte_offset >= 0, "should not see invalid offset");
1108 oop owner = ((oop*) sp())[byte_offset / wordSize];
1109 assert( Universe::heap()->is_in(owner), "bad receiver" );
1110 return owner;
1111 }
1112
1113 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const {
1114 assert(map != nullptr, "map must be set");
1115 if (map->include_argument_oops()) {
1116 // must collect argument oops, as nobody else is doing it
1117 Thread *thread = Thread::current();
1118 methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1119 EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1120 finder.arguments_do(f);
1121 }
1122 // Traverse the Handle Block saved in the entry frame
1123 entry_frame_call_wrapper()->oops_do(f);
1124 }
1125
1126 void frame::oops_upcall_do(OopClosure* f, const RegisterMap* map) const {
1127 assert(map != nullptr, "map must be set");
1128 if (map->include_argument_oops()) {
1129 // Upcall stubs call a MethodHandle impl method of which only the receiver
1130 // is ever an oop.
1131 // Currently we should not be able to get here, since there are no
1132 // safepoints in the one resolve stub we can get into (handle_wrong_method)
1133 // Leave this here as a trap in case we ever do:
1134 ShouldNotReachHere(); // not implemented
1135 }
1136 _cb->as_upcall_stub()->oops_do(f, *this);
1137 }
1138
1139 bool frame::is_deoptimized_frame() const {
1140 assert(_deopt_state != unknown, "not answerable");
1141 if (_deopt_state == is_deoptimized) {
1142 return true;
1143 }
1144
1145 /* This method only checks if the frame is deoptimized
1146 * as in return address being patched.
1147 * It doesn't care if the OP that we return to is a
1148 * deopt instruction */
1149 /*if (_cb != nullptr && _cb->is_nmethod()) {
1150 return NativeDeoptInstruction::is_deopt_at(_pc);
1151 }*/
1152 return false;
1153 }
1154
1155 void frame::oops_do_internal(OopClosure* f, NMethodClosure* cf,
1156 DerivedOopClosure* df, DerivedPointerIterationMode derived_mode,
1157 const RegisterMap* map, bool use_interpreter_oop_map_cache) const {
1158 #ifndef PRODUCT
1159 // simulate GC crash here to dump java thread in error report
1160 guarantee(!CrashGCForDumpingJavaThread, "");
1161 #endif
1162 if (is_interpreted_frame()) {
1163 oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
1164 } else if (is_entry_frame()) {
1165 oops_entry_do(f, map);
1166 } else if (is_upcall_stub_frame()) {
1167 oops_upcall_do(f, map);
1168 } else if (CodeCache::contains(pc())) {
1169 oops_nmethod_do(f, cf, df, derived_mode, map);
1170 } else {
1171 ShouldNotReachHere();
1172 }
1173 }
1174
1175 void frame::nmethod_do(NMethodClosure* cf) const {
1176 if (_cb != nullptr && _cb->is_nmethod()) {
1177 cf->do_nmethod(_cb->as_nmethod());
1178 }
1179 }
1180
1181
1182 // Call f closure on the interpreted Method*s in the stack.
1183 void frame::metadata_do(MetadataClosure* f) const {
1184 ResourceMark rm;
1185 if (is_interpreted_frame()) {
1186 Method* m = this->interpreter_frame_method();
1187 assert(m != nullptr, "expecting a method in this frame");
1188 f->do_metadata(m);
1189 }
1190 }
1191
1192 void frame::verify(const RegisterMap* map) const {
1193 #ifndef PRODUCT
1194 if (TraceCodeBlobStacks) {
1195 tty->print_cr("*** verify");
1196 print_on(tty);
1197 }
1198 #endif
1199
1200 // for now make sure receiver type is correct
1201 if (is_interpreted_frame()) {
1202 Method* method = interpreter_frame_method();
1203 guarantee(method->is_method(), "method is wrong in frame::verify");
1204 if (!method->is_static()) {
1205 // fetch the receiver
1206 oop* p = (oop*) interpreter_frame_local_at(0);
1207 // make sure we have the right receiver type
1208 }
1209 }
1210 #if COMPILER2_OR_JVMCI
1211 assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1212 #endif
1213
1214 if (map->update_map()) { // The map has to be up-to-date for the current frame
1215 oops_do_internal(&VerifyOopClosure::verify_oop, nullptr, nullptr, DerivedPointerIterationMode::_ignore, map, false);
1216 }
1217 }
1218
1219
1220 #ifdef ASSERT
1221 bool frame::verify_return_pc(address x) {
1222 #ifdef TARGET_ARCH_aarch64
1223 if (!pauth_ptr_is_raw(x)) {
1224 return false;
1225 }
1226 #endif
1227 if (StubRoutines::returns_to_call_stub(x)) {
1228 return true;
1229 }
1230 if (CodeCache::contains(x)) {
1231 return true;
1232 }
1233 if (Interpreter::contains(x)) {
1234 return true;
1235 }
1236 return false;
1237 }
1238 #endif
1239
1240 #ifdef ASSERT
1241 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1242 assert(is_interpreted_frame(), "Not an interpreted frame");
1243 // verify that the value is in the right part of the frame
1244 address low_mark = (address) interpreter_frame_monitor_end();
1245 address high_mark = (address) interpreter_frame_monitor_begin();
1246 address current = (address) value;
1247
1248 const int monitor_size = frame::interpreter_frame_monitor_size();
1249 guarantee((high_mark - current) % monitor_size == 0 , "Misaligned top of BasicObjectLock*");
1250 guarantee( high_mark > current , "Current BasicObjectLock* higher than high_mark");
1251
1252 guarantee((current - low_mark) % monitor_size == 0 , "Misaligned bottom of BasicObjectLock*");
1253 guarantee( current >= low_mark , "Current BasicObjectLock* below than low_mark");
1254 }
1255 #endif
1256
1257 #ifndef PRODUCT
1258
1259 // Returns true iff the address p is readable and *(intptr_t*)p != errvalue
1260 extern "C" bool dbg_is_safe(const void* p, intptr_t errvalue);
1261
1262 class FrameValuesOopClosure: public OopClosure, public DerivedOopClosure {
1263 private:
1264 GrowableArray<oop*>* _oops;
1265 GrowableArray<narrowOop*>* _narrow_oops;
1266 GrowableArray<derived_base*>* _base;
1267 GrowableArray<derived_pointer*>* _derived;
1268 NoSafepointVerifier nsv;
1269
1270 public:
1271 FrameValuesOopClosure() {
1272 _oops = new (mtThread) GrowableArray<oop*>(100, mtThread);
1273 _narrow_oops = new (mtThread) GrowableArray<narrowOop*>(100, mtThread);
1274 _base = new (mtThread) GrowableArray<derived_base*>(100, mtThread);
1275 _derived = new (mtThread) GrowableArray<derived_pointer*>(100, mtThread);
1276 }
1277 ~FrameValuesOopClosure() {
1278 delete _oops;
1279 delete _narrow_oops;
1280 delete _base;
1281 delete _derived;
1282 }
1283
1284 virtual void do_oop(oop* p) override { _oops->push(p); }
1285 virtual void do_oop(narrowOop* p) override { _narrow_oops->push(p); }
1286 virtual void do_derived_oop(derived_base* base_loc, derived_pointer* derived_loc) override {
1287 _base->push(base_loc);
1288 _derived->push(derived_loc);
1289 }
1290
1291 bool is_good(oop* p) {
1292 return *p == nullptr || (dbg_is_safe(*p, -1) && dbg_is_safe((*p)->klass(), -1) && oopDesc::is_oop_or_null(*p));
1293 }
1294 void describe(FrameValues& values, int frame_no) {
1295 for (int i = 0; i < _oops->length(); i++) {
1296 oop* p = _oops->at(i);
1297 values.describe(frame_no, (intptr_t*)p, err_msg("oop%s for #%d", is_good(p) ? "" : " (BAD)", frame_no));
1298 }
1299 for (int i = 0; i < _narrow_oops->length(); i++) {
1300 narrowOop* p = _narrow_oops->at(i);
1301 // we can't check for bad compressed oops, as decoding them might crash
1302 values.describe(frame_no, (intptr_t*)p, err_msg("narrow oop for #%d", frame_no));
1303 }
1304 assert(_base->length() == _derived->length(), "should be the same");
1305 for (int i = 0; i < _base->length(); i++) {
1306 derived_base* base = _base->at(i);
1307 derived_pointer* derived = _derived->at(i);
1308 values.describe(frame_no, (intptr_t*)derived, err_msg("derived pointer (base: " INTPTR_FORMAT ") for #%d", p2i(base), frame_no));
1309 }
1310 }
1311 };
1312
1313 class FrameValuesOopMapClosure: public OopMapClosure {
1314 private:
1315 const frame* _fr;
1316 const RegisterMap* _reg_map;
1317 FrameValues& _values;
1318 int _frame_no;
1319
1320 public:
1321 FrameValuesOopMapClosure(const frame* fr, const RegisterMap* reg_map, FrameValues& values, int frame_no)
1322 : _fr(fr), _reg_map(reg_map), _values(values), _frame_no(frame_no) {}
1323
1324 virtual void do_value(VMReg reg, OopMapValue::oop_types type) override {
1325 intptr_t* p = (intptr_t*)_fr->oopmapreg_to_location(reg, _reg_map);
1326 if (p != nullptr && (((intptr_t)p & WordAlignmentMask) == 0)) {
1327 const char* type_name = nullptr;
1328 switch(type) {
1329 case OopMapValue::oop_value: type_name = "oop"; break;
1330 case OopMapValue::narrowoop_value: type_name = "narrow oop"; break;
1331 case OopMapValue::callee_saved_value: type_name = "callee-saved"; break;
1332 case OopMapValue::derived_oop_value: type_name = "derived"; break;
1333 // case OopMapValue::live_value: type_name = "live"; break;
1334 default: break;
1335 }
1336 if (type_name != nullptr) {
1337 _values.describe(_frame_no, p, err_msg("%s for #%d", type_name, _frame_no));
1338 }
1339 }
1340 }
1341 };
1342
1343 // callers need a ResourceMark because of name_and_sig_as_C_string() usage,
1344 // RA allocated string is returned to the caller
1345 void frame::describe(FrameValues& values, int frame_no, const RegisterMap* reg_map, bool top) {
1346 // boundaries: sp and the 'real' frame pointer
1347 values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 0);
1348 if (top) {
1349 values.describe(-1, sp() - 1, err_msg("sp[-1] for #%d", frame_no), 0);
1350 values.describe(-1, sp() - 2, err_msg("sp[-2] for #%d", frame_no), 0);
1351 }
1352
1353 intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1354
1355 // print frame info at the highest boundary
1356 intptr_t* info_address = MAX2(sp(), frame_pointer);
1357
1358 if (info_address != frame_pointer) {
1359 // print frame_pointer explicitly if not marked by the frame info
1360 values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1361 }
1362
1363 if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1364 // Label values common to most frames
1365 values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no), 0);
1366 }
1367
1368 if (is_interpreted_frame()) {
1369 Method* m = interpreter_frame_method();
1370 int bci = interpreter_frame_bci();
1371 InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
1372
1373 // Label the method and current bci
1374 values.describe(-1, info_address,
1375 FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 3);
1376 if (desc != nullptr) {
1377 values.describe(-1, info_address, err_msg("- %s codelet: %s",
1378 desc->bytecode() >= 0 ? Bytecodes::name(desc->bytecode()) : "",
1379 desc->description() != nullptr ? desc->description() : "?"), 2);
1380 }
1381 values.describe(-1, info_address,
1382 err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 2);
1383 // return address will be emitted by caller in describe_pd
1384 // values.describe(frame_no, (intptr_t*)sender_pc_addr(), Continuation::is_return_barrier_entry(*sender_pc_addr()) ? "return address (return barrier)" : "return address");
1385
1386 if (m->max_locals() > 0) {
1387 intptr_t* l0 = interpreter_frame_local_at(0);
1388 intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1389 values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 2);
1390 // Report each local and mark as owned by this frame
1391 for (int l = 0; l < m->max_locals(); l++) {
1392 intptr_t* l0 = interpreter_frame_local_at(l);
1393 values.describe(frame_no, l0, err_msg("local %d", l), 1);
1394 }
1395 }
1396
1397 if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1398 values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1399 values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1400 }
1401
1402 // Compute the actual expression stack size
1403 InterpreterOopMap mask;
1404 OopMapCache::compute_one_oop_map(methodHandle(Thread::current(), m), bci, &mask);
1405 intptr_t* tos = nullptr;
1406 // Report each stack element and mark as owned by this frame
1407 for (int e = 0; e < mask.expression_stack_size(); e++) {
1408 tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1409 values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1410 err_msg("stack %d", e), 1);
1411 }
1412 if (tos != nullptr) {
1413 values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 2);
1414 }
1415
1416 if (reg_map != nullptr) {
1417 FrameValuesOopClosure oopsFn;
1418 oops_do(&oopsFn, nullptr, &oopsFn, reg_map);
1419 oopsFn.describe(values, frame_no);
1420 }
1421 } else if (is_entry_frame()) {
1422 // For now just label the frame
1423 values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1424 } else if (is_compiled_frame()) {
1425 // For now just label the frame
1426 nmethod* nm = cb()->as_nmethod();
1427 values.describe(-1, info_address,
1428 FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method J %s%s", frame_no,
1429 p2i(nm),
1430 nm->method()->name_and_sig_as_C_string(),
1431 (_deopt_state == is_deoptimized) ?
1432 " (deoptimized)" :
1433 ((_deopt_state == unknown) ? " (state unknown)" : "")),
1434 3);
1435
1436 { // mark arguments (see nmethod::print_nmethod_labels)
1437 Method* m = nm->method();
1438
1439 int stack_slot_offset = nm->frame_size() * wordSize; // offset, in bytes, to caller sp
1440 int sizeargs = m->size_of_parameters();
1441
1442 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
1443 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
1444 {
1445 int sig_index = 0;
1446 if (!m->is_static()) {
1447 sig_bt[sig_index++] = T_OBJECT; // 'this'
1448 }
1449 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
1450 BasicType t = ss.type();
1451 assert(type2size[t] == 1 || type2size[t] == 2, "size is 1 or 2");
1452 sig_bt[sig_index++] = t;
1453 if (type2size[t] == 2) {
1454 sig_bt[sig_index++] = T_VOID;
1455 }
1456 }
1457 assert(sig_index == sizeargs, "");
1458 }
1459 int stack_arg_slots = SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
1460 assert(stack_arg_slots == nm->as_nmethod()->num_stack_arg_slots(false /* rounded */) || nm->is_osr_method(), "");
1461 int out_preserve = SharedRuntime::out_preserve_stack_slots();
1462 int sig_index = 0;
1463 int arg_index = (m->is_static() ? 0 : -1);
1464 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
1465 bool at_this = (arg_index == -1);
1466 bool at_old_sp = false;
1467 BasicType t = (at_this ? T_OBJECT : ss.type());
1468 assert(t == sig_bt[sig_index], "sigs in sync");
1469 VMReg fst = regs[sig_index].first();
1470 if (fst->is_stack()) {
1471 assert(((int)fst->reg2stack()) >= 0, "reg2stack: %d", fst->reg2stack());
1472 int offset = (fst->reg2stack() + out_preserve) * VMRegImpl::stack_slot_size + stack_slot_offset;
1473 intptr_t* stack_address = (intptr_t*)((address)unextended_sp() + offset);
1474 if (at_this) {
1475 values.describe(frame_no, stack_address, err_msg("this for #%d", frame_no), 1);
1476 } else {
1477 values.describe(frame_no, stack_address, err_msg("param %d %s for #%d", arg_index, type2name(t), frame_no), 1);
1478 }
1479 }
1480 sig_index += type2size[t];
1481 arg_index += 1;
1482 if (!at_this) {
1483 ss.next();
1484 }
1485 }
1486 }
1487
1488 if (reg_map != nullptr && is_java_frame()) {
1489 int scope_no = 0;
1490 for (ScopeDesc* scope = nm->scope_desc_at(pc()); scope != nullptr; scope = scope->sender(), scope_no++) {
1491 Method* m = scope->method();
1492 int bci = scope->bci();
1493 values.describe(-1, info_address, err_msg("- #%d scope %s @ %d", scope_no, m->name_and_sig_as_C_string(), bci), 2);
1494
1495 { // mark locals
1496 GrowableArray<ScopeValue*>* scvs = scope->locals();
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("local %d for #%d (scope %d)", i, frame_no, scope_no), 1);
1502 }
1503 }
1504 }
1505 { // mark expression stack
1506 GrowableArray<ScopeValue*>* scvs = scope->expressions();
1507 int scvs_length = scvs != nullptr ? scvs->length() : 0;
1508 for (int i = 0; i < scvs_length; i++) {
1509 intptr_t* stack_address = (intptr_t*)StackValue::stack_value_address(this, reg_map, scvs->at(i));
1510 if (stack_address != nullptr) {
1511 values.describe(frame_no, stack_address, err_msg("stack %d for #%d (scope %d)", i, frame_no, scope_no), 1);
1512 }
1513 }
1514 }
1515 }
1516
1517 FrameValuesOopClosure oopsFn;
1518 oops_do(&oopsFn, nullptr, &oopsFn, reg_map);
1519 oopsFn.describe(values, frame_no);
1520
1521 if (oop_map() != nullptr) {
1522 FrameValuesOopMapClosure valuesFn(this, reg_map, values, frame_no);
1523 // also OopMapValue::live_value ??
1524 oop_map()->all_type_do(this, OopMapValue::callee_saved_value, &valuesFn);
1525 }
1526 }
1527 } else if (is_native_frame()) {
1528 // For now just label the frame
1529 nmethod* nm = cb()->as_nmethod_or_null();
1530 values.describe(-1, info_address,
1531 FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1532 p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);
1533 if (nm->method()->is_continuation_enter_intrinsic()) {
1534 ContinuationEntry* ce = Continuation::get_continuation_entry_for_entry_frame(reg_map->thread(), *this); // (ContinuationEntry*)unextended_sp();
1535 ce->describe(values, frame_no);
1536 }
1537 } else {
1538 // provide default info if not handled before
1539 char *info = (char *) "special frame";
1540 if ((_cb != nullptr) &&
1541 (_cb->name() != nullptr)) {
1542 info = (char *)_cb->name();
1543 }
1544 values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1545 }
1546
1547 // platform dependent additional data
1548 describe_pd(values, frame_no);
1549 }
1550
1551 #endif
1552
1553 /**
1554 * Gets the caller frame of `fr` for thread `t`.
1555 *
1556 * @returns an invalid frame (i.e. fr.pc() === 0) if the caller cannot be obtained
1557 */
1558 frame frame::next_frame(frame fr, Thread* t) {
1559 // Compiled code may use EBP register on x86 so it looks like
1560 // non-walkable C frame. Use frame.sender() for java frames.
1561 frame invalid;
1562 if (t != nullptr && t->is_Java_thread()) {
1563 // Catch very first native frame by using stack address.
1564 // For JavaThread stack_base and stack_size should be set.
1565 if (!t->is_in_full_stack((address)(fr.real_fp() + 1))) {
1566 return invalid;
1567 }
1568 if (fr.is_interpreted_frame() || (fr.cb() != nullptr && fr.cb()->frame_size() > 0)) {
1569 RegisterMap map(JavaThread::cast(t),
1570 RegisterMap::UpdateMap::skip,
1571 RegisterMap::ProcessFrames::include,
1572 RegisterMap::WalkContinuation::skip); // No update
1573 return fr.sender(&map);
1574 } else {
1575 // is_first_C_frame() does only simple checks for frame pointer,
1576 // it will pass if java compiled code has a pointer in EBP.
1577 if (os::is_first_C_frame(&fr)) return invalid;
1578 return os::get_sender_for_C_frame(&fr);
1579 }
1580 } else {
1581 if (os::is_first_C_frame(&fr)) return invalid;
1582 return os::get_sender_for_C_frame(&fr);
1583 }
1584 }
1585
1586 #ifndef PRODUCT
1587
1588 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1589 FrameValue fv;
1590 fv.location = location;
1591 fv.owner = owner;
1592 fv.priority = priority;
1593 fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1594 strcpy(fv.description, description);
1595 _values.append(fv);
1596 }
1597
1598
1599 #ifdef ASSERT
1600 void FrameValues::validate() {
1601 _values.sort(compare);
1602 bool error = false;
1603 FrameValue prev;
1604 prev.owner = -1;
1605 for (int i = _values.length() - 1; i >= 0; i--) {
1606 FrameValue fv = _values.at(i);
1607 if (fv.owner == -1) continue;
1608 if (prev.owner == -1) {
1609 prev = fv;
1610 continue;
1611 }
1612 if (prev.location == fv.location) {
1613 if (fv.owner != prev.owner) {
1614 tty->print_cr("overlapping storage");
1615 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(prev.location), *prev.location, prev.description);
1616 tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1617 error = true;
1618 }
1619 } else {
1620 prev = fv;
1621 }
1622 }
1623 // if (error) { tty->cr(); print_on(static_cast<JavaThread*>(nullptr), tty); }
1624 assert(!error, "invalid layout");
1625 }
1626 #endif // ASSERT
1627
1628 void FrameValues::print_on(JavaThread* thread, outputStream* st) {
1629 _values.sort(compare);
1630
1631 // Sometimes values like the fp can be invalid values if the
1632 // register map wasn't updated during the walk. Trim out values
1633 // that aren't actually in the stack of the thread.
1634 int min_index = 0;
1635 int max_index = _values.length() - 1;
1636 intptr_t* v0 = _values.at(min_index).location;
1637 intptr_t* v1 = _values.at(max_index).location;
1638
1639 if (thread != nullptr) {
1640 if (thread == Thread::current()) {
1641 while (!thread->is_in_live_stack((address)v0)) v0 = _values.at(++min_index).location;
1642 while (!thread->is_in_live_stack((address)v1)) v1 = _values.at(--max_index).location;
1643 } else {
1644 while (!thread->is_in_full_stack((address)v0)) v0 = _values.at(++min_index).location;
1645 while (!thread->is_in_full_stack((address)v1)) v1 = _values.at(--max_index).location;
1646 }
1647 }
1648
1649 print_on(st, min_index, max_index, v0, v1);
1650 }
1651
1652 void FrameValues::print_on(stackChunkOop chunk, outputStream* st) {
1653 _values.sort(compare);
1654
1655 intptr_t* start = chunk->start_address();
1656 intptr_t* end = chunk->end_address() + 1;
1657
1658 int min_index = 0;
1659 int max_index = _values.length() - 1;
1660 intptr_t* v0 = _values.at(min_index).location;
1661 intptr_t* v1 = _values.at(max_index).location;
1662 while (!(start <= v0 && v0 <= end)) v0 = _values.at(++min_index).location;
1663 while (!(start <= v1 && v1 <= end)) v1 = _values.at(--max_index).location;
1664
1665 print_on(st, min_index, max_index, v0, v1);
1666 }
1667
1668 void FrameValues::print_on(outputStream* st, int min_index, int max_index, intptr_t* v0, intptr_t* v1) {
1669 intptr_t* min = MIN2(v0, v1);
1670 intptr_t* max = MAX2(v0, v1);
1671 intptr_t* cur = max;
1672 intptr_t* last = nullptr;
1673 intptr_t* fp = nullptr;
1674 for (int i = max_index; i >= min_index; i--) {
1675 FrameValue fv = _values.at(i);
1676 while (cur > fv.location) {
1677 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, p2i(cur), *cur);
1678 cur--;
1679 }
1680 if (last == fv.location) {
1681 const char* spacer = " " LP64_ONLY(" ");
1682 st->print_cr(" %s %s %s", spacer, spacer, fv.description);
1683 } else {
1684 if (*fv.description == '#' && isdigit(fv.description[1])) {
1685 // The fv.description string starting with a '#' is the line for the
1686 // saved frame pointer eg. "#10 method java.lang.invoke.LambdaForm..."
1687 // basicaly means frame 10.
1688 fp = fv.location;
1689 }
1690 // To print a fp-relative value:
1691 // 1. The content of *fv.location must be such that we think it's a
1692 // fp-relative number, i.e [-100..100].
1693 // 2. We must have found the frame pointer.
1694 // 3. The line can not be the line for the saved frame pointer.
1695 // 4. Recognize it as being part of the "fixed frame".
1696 if (*fv.location != 0 && *fv.location > -100 && *fv.location < 100
1697 && fp != nullptr && *fv.description != '#'
1698 #if !defined(PPC64)
1699 && (strncmp(fv.description, "interpreter_frame_", 18) == 0 || strstr(fv.description, " method "))
1700 #else // !defined(PPC64)
1701 && (strcmp(fv.description, "sender_sp") == 0 || strcmp(fv.description, "top_frame_sp") == 0 ||
1702 strcmp(fv.description, "esp") == 0 || strcmp(fv.description, "monitors") == 0 ||
1703 strcmp(fv.description, "locals") == 0 || strstr(fv.description, " method "))
1704 #endif //!defined(PPC64)
1705 ) {
1706 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %-32s (relativized: fp%+d)",
1707 p2i(fv.location), p2i(&fp[*fv.location]), fv.description, (int)*fv.location);
1708 } else {
1709 st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1710 }
1711 last = fv.location;
1712 cur--;
1713 }
1714 }
1715 }
1716
1717 #endif // ndef PRODUCT