1 /* 2 * Copyright (c) 1997, 2023, 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 #ifndef SHARE_RUNTIME_FRAME_HPP 26 #define SHARE_RUNTIME_FRAME_HPP 27 28 #include "code/vmregTypes.hpp" 29 #include "compiler/oopMap.hpp" 30 #include "oops/oopsHierarchy.hpp" 31 #include "runtime/basicLock.hpp" 32 #include "runtime/monitorChunk.hpp" 33 #include "utilities/growableArray.hpp" 34 #include "utilities/macros.hpp" 35 #ifdef ZERO 36 # include "stack_zero.hpp" 37 #endif 38 39 typedef class BytecodeInterpreter* interpreterState; 40 41 class CodeBlob; 42 class CompiledMethod; 43 class FrameValues; 44 class InterpreterOopMap; 45 class JavaCallWrapper; 46 class Method; 47 class methodHandle; 48 class RegisterMap; 49 class vframeArray; 50 51 enum class DerivedPointerIterationMode { 52 _with_table, 53 _directly, 54 _ignore 55 }; 56 57 // A frame represents a physical stack frame (an activation). Frames 58 // can be C or Java frames, and the Java frames can be interpreted or 59 // compiled. In contrast, vframes represent source-level activations, 60 // so that one physical frame can correspond to multiple source level 61 // frames because of inlining. 62 63 class frame { 64 private: 65 // Instance variables: 66 union { 67 intptr_t* _sp; // stack pointer (from Thread::last_Java_sp) 68 int _offset_sp; // used by frames in stack chunks 69 }; 70 address _pc; // program counter (the next instruction after the call) 71 mutable CodeBlob* _cb; // CodeBlob that "owns" pc 72 mutable const ImmutableOopMap* _oop_map; // oop map, for compiled/stubs frames only 73 enum deopt_state { 74 not_deoptimized, 75 is_deoptimized, 76 unknown 77 }; 78 79 deopt_state _deopt_state; 80 81 // Do internal pointers in interpreter frames use absolute adddresses or relative (to fp)? 82 // Frames in stack chunks are on the Java heap and use relative addressing; on the stack 83 // they use absolute addressing 84 bool _on_heap; // This frame represents a frame on the heap. 85 DEBUG_ONLY(int _frame_index;) // the frame index in a stack chunk; -1 when on a thread stack 86 87 // We use different assertions to allow for intermediate states (e.g. during thawing or relativizing the frame) 88 void assert_on_heap() const { assert(is_heap_frame(), "Using offset with a non-chunk frame"); } 89 void assert_offset() const { assert(_frame_index >= 0, "Using offset with a non-chunk frame"); assert_on_heap(); } 90 void assert_absolute() const { assert(_frame_index == -1, "Using absolute addresses with a chunk frame"); } 91 92 public: 93 // Constructors 94 frame(); 95 96 explicit frame(bool dummy) {} // no initialization 97 98 explicit frame(intptr_t* sp); 99 100 #ifndef PRODUCT 101 // This is a generic constructor which is only used by pns() in debug.cpp. 102 // pns (i.e. print native stack) uses this constructor to create a starting 103 // frame for stack walking. The implementation of this constructor is platform 104 // dependent (i.e. SPARC doesn't need an 'fp' argument an will ignore it) but 105 // we want to keep the signature generic because pns() is shared code. 106 frame(void* sp, void* fp, void* pc); 107 #endif 108 109 // Accessors 110 111 // pc: Returns the pc at which this frame will continue normally. 112 // It must point at the beginning of the next instruction to execute. 113 address pc() const { return _pc; } 114 115 // This returns the pc that if you were in the debugger you'd see. Not 116 // the idealized value in the frame object. This undoes the magic conversion 117 // that happens for deoptimized frames. In addition it makes the value the 118 // hardware would want to see in the native frame. The only user (at this point) 119 // is deoptimization. It likely no one else should ever use it. 120 address raw_pc() const; 121 122 void set_pc(address newpc); 123 124 intptr_t* sp() const { assert_absolute(); return _sp; } 125 void set_sp( intptr_t* newsp ) { _sp = newsp; } 126 127 int offset_sp() const { assert_offset(); return _offset_sp; } 128 void set_offset_sp( int newsp ) { assert_on_heap(); _offset_sp = newsp; } 129 130 int frame_index() const { 131 #ifdef ASSERT 132 return _frame_index; 133 #else 134 return -1; 135 #endif 136 } 137 void set_frame_index( int index ) { 138 #ifdef ASSERT 139 _frame_index = index; 140 #endif 141 } 142 143 static int sender_sp_ret_address_offset(); 144 145 CodeBlob* cb() const { return _cb; } 146 inline CodeBlob* get_cb() const; 147 // inline void set_cb(CodeBlob* cb); 148 149 const ImmutableOopMap* oop_map() const { 150 if (_oop_map == nullptr) { 151 _oop_map = get_oop_map(); 152 } 153 return _oop_map; 154 } 155 156 // patching operations 157 void patch_pc(Thread* thread, address pc); 158 159 // Every frame needs to return a unique id which distinguishes it from all other frames. 160 // For sparc and ia32 use sp. ia64 can have memory frames that are empty so multiple frames 161 // will have identical sp values. For ia64 the bsp (fp) value will serve. No real frame 162 // should have an id() of null so it is a distinguishing value for an unmatchable frame. 163 // We also have relationals which allow comparing a frame to anoth frame's id() allow 164 // us to distinguish younger (more recent activation) from older (less recent activations) 165 // A null id is only valid when comparing for equality. 166 167 intptr_t* id(void) const; 168 bool is_younger(intptr_t* id) const; 169 bool is_older(intptr_t* id) const; 170 171 // testers 172 173 // Compares for strict equality. Rarely used or needed. 174 // It can return a different result than f1.id() == f2.id() 175 bool equal(frame other) const; 176 177 // type testers 178 bool is_empty() const { return _pc == nullptr; } 179 bool is_interpreted_frame() const; 180 bool is_java_frame() const; 181 bool is_entry_frame() const; // Java frame called from C? 182 bool is_stub_frame() const; 183 bool is_ignored_frame() const; 184 bool is_native_frame() const; 185 bool is_runtime_frame() const; 186 bool is_compiled_frame() const; 187 bool is_safepoint_blob_frame() const; 188 bool is_deoptimized_frame() const; 189 bool is_upcall_stub_frame() const; 190 bool is_heap_frame() const { return _on_heap; } 191 192 // testers 193 bool is_first_frame() const; // oldest frame? (has no sender) 194 bool is_first_java_frame() const; // same for Java frame 195 bool is_first_vthread_frame(JavaThread* thread) const; 196 197 bool is_interpreted_frame_valid(JavaThread* thread) const; // performs sanity checks on interpreted frames. 198 199 // is this frame doing a call using the compiled calling convention? 200 bool is_compiled_caller() const { 201 return is_compiled_frame() || is_upcall_stub_frame(); 202 } 203 204 // tells whether this frame is marked for deoptimization 205 bool should_be_deoptimized() const; 206 207 // tells whether this frame can be deoptimized 208 bool can_be_deoptimized() const; 209 210 // the frame size in machine words 211 inline int frame_size() const; 212 213 // the size, in words, of stack-passed arguments 214 inline int compiled_frame_stack_argsize() const; 215 216 inline void interpreted_frame_oop_map(InterpreterOopMap* mask) const; 217 218 // returns the sending frame 219 inline frame sender(RegisterMap* map) const; 220 221 bool safe_for_sender(JavaThread *thread); 222 223 // returns the sender, but skips conversion frames 224 frame real_sender(RegisterMap* map) const; 225 226 // returns the sending Java frame, skipping any intermediate C frames 227 // NB: receiver must not be first frame 228 frame java_sender() const; 229 230 private: 231 // Helper methods for better factored code in frame::sender 232 inline frame sender_for_compiled_frame(RegisterMap* map) const; 233 frame sender_for_entry_frame(RegisterMap* map) const; 234 frame sender_for_interpreter_frame(RegisterMap* map) const; 235 frame sender_for_upcall_stub_frame(RegisterMap* map) const; 236 237 bool is_entry_frame_valid(JavaThread* thread) const; 238 239 // All frames: 240 241 // A low-level interface for vframes: 242 243 public: 244 245 intptr_t* addr_at(int index) const { return &fp()[index]; } 246 intptr_t at_absolute(int index) const { return *addr_at(index); } 247 // Interpreter frames in continuation stacks are on the heap, and internal addresses are relative to fp. 248 intptr_t at_relative(int index) const { return (intptr_t)(fp() + fp()[index]); } 249 250 intptr_t at(int index) const { 251 return _on_heap ? at_relative(index) : at_absolute(index); 252 } 253 254 public: 255 // Link (i.e., the pointer to the previous frame) 256 // might crash if the frame has no parent 257 intptr_t* link() const; 258 259 // Link (i.e., the pointer to the previous frame) or null if the link cannot be accessed 260 intptr_t* link_or_null() const; 261 262 // Return address 263 address sender_pc() const; 264 265 // Support for deoptimization 266 void deoptimize(JavaThread* thread); 267 268 // The frame's original SP, before any extension by an interpreted callee; 269 // used for packing debug info into vframeArray objects and vframeArray lookup. 270 intptr_t* unextended_sp() const; 271 void set_unextended_sp(intptr_t* value); 272 273 int offset_unextended_sp() const; 274 void set_offset_unextended_sp(int value); 275 276 // returns the stack pointer of the calling frame 277 intptr_t* sender_sp() const; 278 279 // Returns the real 'frame pointer' for the current frame. 280 // This is the value expected by the platform ABI when it defines a 281 // frame pointer register. It may differ from the effective value of 282 // the FP register when that register is used in the JVM for other 283 // purposes (like compiled frames on some platforms). 284 // On other platforms, it is defined so that the stack area used by 285 // this frame goes from real_fp() to sp(). 286 intptr_t* real_fp() const; 287 288 // Deoptimization info, if needed (platform dependent). 289 // Stored in the initial_info field of the unroll info, to be used by 290 // the platform dependent deoptimization blobs. 291 intptr_t *initial_deoptimization_info(); 292 293 // Interpreter frames: 294 295 private: 296 intptr_t* interpreter_frame_locals() const; 297 intptr_t* interpreter_frame_bcp_addr() const; 298 intptr_t* interpreter_frame_mdp_addr() const; 299 300 public: 301 // Locals 302 303 // The _at version returns a pointer because the address is used for GC. 304 intptr_t* interpreter_frame_local_at(int index) const; 305 306 void interpreter_frame_set_locals(intptr_t* locs); 307 308 // byte code index 309 jint interpreter_frame_bci() const; 310 311 // byte code pointer 312 address interpreter_frame_bcp() const; 313 void interpreter_frame_set_bcp(address bcp); 314 315 // method data pointer 316 address interpreter_frame_mdp() const; 317 void interpreter_frame_set_mdp(address dp); 318 319 // Find receiver out of caller's (compiled) argument list 320 oop retrieve_receiver(RegisterMap *reg_map); 321 322 // Return the monitor owner and BasicLock for compiled synchronized 323 // native methods. Used by JVMTI's GetLocalInstance method 324 // (via VM_GetReceiver) to retrieve the receiver from a native wrapper frame. 325 BasicLock* get_native_monitor(); 326 oop get_native_receiver(); 327 328 // Find receiver for an invoke when arguments are just pushed on stack (i.e., callee stack-frame is 329 // not setup) 330 oop interpreter_callee_receiver(Symbol* signature); 331 332 333 oop* interpreter_callee_receiver_addr(Symbol* signature); 334 335 336 // expression stack (may go up or down, direction == 1 or -1) 337 public: 338 intptr_t* interpreter_frame_expression_stack() const; 339 340 // The _at version returns a pointer because the address is used for GC. 341 intptr_t* interpreter_frame_expression_stack_at(jint offset) const; 342 343 // top of expression stack 344 intptr_t* interpreter_frame_tos_at(jint offset) const; 345 intptr_t* interpreter_frame_tos_address() const; 346 347 348 jint interpreter_frame_expression_stack_size() const; 349 350 intptr_t* interpreter_frame_sender_sp() const; 351 352 // template based interpreter deoptimization support 353 void set_interpreter_frame_sender_sp(intptr_t* sender_sp); 354 void interpreter_frame_set_monitor_end(BasicObjectLock* value); 355 356 // Address of the temp oop in the frame. Needed as GC root. 357 oop* interpreter_frame_temp_oop_addr() const; 358 359 // BasicObjectLocks: 360 // 361 // interpreter_frame_monitor_begin is higher in memory than interpreter_frame_monitor_end 362 // Interpreter_frame_monitor_begin points to one element beyond the oldest one, 363 // interpreter_frame_monitor_end points to the youngest one, or if there are none, 364 // it points to one beyond where the first element will be. 365 // interpreter_frame_monitor_size reports the allocation size of a monitor in the interpreter stack. 366 // this value is >= BasicObjectLock::size(), and may be rounded up 367 368 BasicObjectLock* interpreter_frame_monitor_begin() const; 369 BasicObjectLock* interpreter_frame_monitor_end() const; 370 BasicObjectLock* next_monitor_in_interpreter_frame(BasicObjectLock* current) const; 371 BasicObjectLock* previous_monitor_in_interpreter_frame(BasicObjectLock* current) const; 372 static int interpreter_frame_monitor_size(); 373 374 void interpreter_frame_verify_monitor(BasicObjectLock* value) const; 375 376 // Return/result value from this interpreter frame 377 // If the method return type is T_OBJECT or T_ARRAY populates oop_result 378 // For other (non-T_VOID) the appropriate field in the jvalue is populated 379 // with the result value. 380 // Should only be called when at method exit when the method is not 381 // exiting due to an exception. 382 BasicType interpreter_frame_result(oop* oop_result, jvalue* value_result); 383 384 public: 385 // Method & constant pool cache 386 Method* interpreter_frame_method() const; 387 void interpreter_frame_set_method(Method* method); 388 Method** interpreter_frame_method_addr() const; 389 ConstantPoolCache** interpreter_frame_cache_addr() const; 390 oop* interpreter_frame_mirror_addr() const; 391 392 void interpreter_frame_set_mirror(oop mirror); 393 394 public: 395 // Entry frames 396 JavaCallWrapper* entry_frame_call_wrapper() const { return *entry_frame_call_wrapper_addr(); } 397 JavaCallWrapper* entry_frame_call_wrapper_if_safe(JavaThread* thread) const; 398 JavaCallWrapper** entry_frame_call_wrapper_addr() const; 399 intptr_t* entry_frame_argument_at(int offset) const; 400 401 // tells whether there is another chunk of Delta stack above 402 bool entry_frame_is_first() const; 403 bool upcall_stub_frame_is_first() const; 404 405 // Safepoints 406 407 public: 408 oop saved_oop_result(RegisterMap* map) const; 409 void set_saved_oop_result(RegisterMap* map, oop obj); 410 411 // For debugging 412 private: 413 const char* print_name() const; 414 415 void describe_pd(FrameValues& values, int frame_no); 416 417 public: 418 void print_value() const { print_value_on(tty,nullptr); } 419 void print_value_on(outputStream* st, JavaThread *thread) const; 420 void print_on(outputStream* st) const; 421 void interpreter_frame_print_on(outputStream* st) const; 422 void print_on_error(outputStream* st, char* buf, int buflen, bool verbose = false) const; 423 static void print_C_frame(outputStream* st, char* buf, int buflen, address pc); 424 425 // Add annotated descriptions of memory locations belonging to this frame to values 426 void describe(FrameValues& values, int frame_no, const RegisterMap* reg_map=nullptr); 427 428 // Conversion from a VMReg to physical stack location 429 template <typename RegisterMapT> 430 address oopmapreg_to_location(VMReg reg, const RegisterMapT* reg_map) const; 431 template <typename RegisterMapT> 432 oop* oopmapreg_to_oop_location(VMReg reg, const RegisterMapT* reg_map) const; 433 434 // Oops-do's 435 void oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix, const RegisterMap* reg_map, OopClosure* f) const; 436 void oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache = true) const; 437 void buffered_values_interpreted_do(BufferedValueClosure* f); 438 439 private: 440 void oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const; 441 442 // Iteration of oops 443 void oops_do_internal(OopClosure* f, CodeBlobClosure* cf, 444 DerivedOopClosure* df, DerivedPointerIterationMode derived_mode, 445 const RegisterMap* map, bool use_interpreter_oop_map_cache) const; 446 447 void oops_entry_do(OopClosure* f, const RegisterMap* map) const; 448 void oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, 449 DerivedOopClosure* df, DerivedPointerIterationMode derived_mode, 450 const RegisterMap* map) const; 451 public: 452 // Memory management 453 void oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map) { 454 #if COMPILER2_OR_JVMCI 455 DerivedPointerIterationMode dpim = DerivedPointerTable::is_active() ? 456 DerivedPointerIterationMode::_with_table : 457 DerivedPointerIterationMode::_ignore; 458 #else 459 DerivedPointerIterationMode dpim = DerivedPointerIterationMode::_ignore;; 460 #endif 461 oops_do_internal(f, cf, nullptr, dpim, map, true); 462 } 463 464 void oops_do(OopClosure* f, CodeBlobClosure* cf, DerivedOopClosure* df, const RegisterMap* map) { 465 oops_do_internal(f, cf, df, DerivedPointerIterationMode::_ignore, map, true); 466 } 467 468 void oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map, 469 DerivedPointerIterationMode derived_mode) const { 470 oops_do_internal(f, cf, nullptr, derived_mode, map, true); 471 } 472 473 void nmethods_do(CodeBlobClosure* cf) const; 474 475 // RedefineClasses support for finding live interpreted methods on the stack 476 void metadata_do(MetadataClosure* f) const; 477 478 // Verification 479 void verify(const RegisterMap* map) const; 480 static bool verify_return_pc(address x); 481 // Usage: 482 // assert(frame::verify_return_pc(return_address), "must be a return pc"); 483 484 #include CPU_HEADER(frame) 485 486 }; 487 488 #ifndef PRODUCT 489 // A simple class to describe a location on the stack 490 class FrameValue { 491 public: 492 intptr_t* location; 493 char* description; 494 int owner; 495 int priority; 496 497 FrameValue() { 498 location = nullptr; 499 description = nullptr; 500 owner = -1; 501 priority = 0; 502 } 503 }; 504 505 506 // A collection of described stack values that can print a symbolic 507 // description of the stack memory. Interpreter frame values can be 508 // in the caller frames so all the values are collected first and then 509 // sorted before being printed. 510 class FrameValues { 511 private: 512 GrowableArray<FrameValue> _values; 513 514 static int compare(FrameValue* a, FrameValue* b) { 515 if (a->location == b->location) { 516 return a->priority - b->priority; 517 } 518 return a->location - b->location; 519 } 520 521 void print_on(outputStream* out, int min_index, int max_index, intptr_t* v0, intptr_t* v1, 522 bool on_heap = false); 523 524 public: 525 // Used by frame functions to describe locations. 526 void describe(int owner, intptr_t* location, const char* description, int priority = 0); 527 528 #ifdef ASSERT 529 void validate(); 530 #endif 531 void print(JavaThread* thread) { print_on(thread, tty); } 532 void print_on(JavaThread* thread, outputStream* out); 533 void print(stackChunkOop chunk) { print_on(chunk, tty); } 534 void print_on(stackChunkOop chunk, outputStream* out); 535 }; 536 537 #endif 538 539 540 #endif // SHARE_RUNTIME_FRAME_HPP