1 /*
  2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "classfile/javaClasses.inline.hpp"
 27 #include "classfile/javaThreadStatus.hpp"
 28 #include "classfile/vmClasses.hpp"
 29 #include "classfile/vmSymbols.hpp"
 30 #include "code/codeCache.hpp"
 31 #include "code/debugInfoRec.hpp"
 32 #include "code/nmethod.hpp"
 33 #include "code/pcDesc.hpp"
 34 #include "code/scopeDesc.hpp"
 35 #include "interpreter/interpreter.hpp"
 36 #include "interpreter/oopMapCache.hpp"
 37 #include "memory/resourceArea.hpp"
 38 #include "oops/instanceKlass.hpp"
 39 #include "oops/method.inline.hpp"
 40 #include "oops/oop.inline.hpp"
 41 #include "oops/stackChunkOop.hpp"
 42 #include "prims/jvmtiExport.hpp"
 43 #include "runtime/frame.inline.hpp"
 44 #include "runtime/globals.hpp"
 45 #include "runtime/handles.inline.hpp"
 46 #include "runtime/javaThread.inline.hpp"
 47 #include "runtime/objectMonitor.hpp"
 48 #include "runtime/objectMonitor.inline.hpp"
 49 #include "runtime/osThread.hpp"
 50 #include "runtime/signature.hpp"
 51 #include "runtime/stackFrameStream.inline.hpp"
 52 #include "runtime/stubRoutines.hpp"
 53 #include "runtime/synchronizer.inline.hpp"
 54 #include "runtime/vframe.inline.hpp"
 55 #include "runtime/vframeArray.hpp"
 56 #include "runtime/vframe_hp.hpp"
 57 
 58 vframe::vframe(const frame* fr, const RegisterMap* reg_map, JavaThread* thread)
 59 : _reg_map(reg_map), _thread(thread),
 60   _chunk(Thread::current(), reg_map->stack_chunk()()) {
 61   assert(fr != nullptr, "must have frame");
 62   _fr = *fr;
 63 }
 64 
 65 vframe* vframe::new_vframe(const frame* f, const RegisterMap* reg_map, JavaThread* thread) {
 66   // Interpreter frame
 67   if (f->is_interpreted_frame()) {
 68     return new interpretedVFrame(f, reg_map, thread);
 69   }
 70 
 71   // Compiled frame
 72   CodeBlob* cb = f->cb();
 73   if (cb != nullptr) {
 74     if (cb->is_nmethod()) {
 75       nmethod* nm = cb->as_nmethod();
 76       return new compiledVFrame(f, reg_map, thread, nm);
 77     }
 78 
 79     if (f->is_runtime_frame()) {
 80       // Skip this frame and try again.
 81       RegisterMap temp_map = *reg_map;
 82       frame s = f->sender(&temp_map);
 83       return new_vframe(&s, &temp_map, thread);
 84     }
 85   }
 86 
 87   // Entry frame
 88   if (f->is_entry_frame()) {
 89     return new entryVFrame(f, reg_map, thread);
 90   }
 91 
 92   // External frame
 93   return new externalVFrame(f, reg_map, thread);
 94 }
 95 
 96 vframe* vframe::sender() const {
 97   RegisterMap temp_map = *register_map();
 98   assert(is_top(), "just checking");
 99   if (_fr.is_empty()) return nullptr;
100   if (_fr.is_entry_frame() && _fr.is_first_frame()) return nullptr;
101   frame s = _fr.real_sender(&temp_map);
102   if (s.is_first_frame()) return nullptr;
103   return vframe::new_vframe(&s, &temp_map, thread());
104 }
105 
106 bool vframe::is_vthread_entry() const {
107   return _fr.is_first_vthread_frame(register_map()->thread());
108 }
109 
110 javaVFrame* vframe::java_sender() const {
111   vframe* f = sender();
112   while (f != nullptr) {
113     if (f->is_vthread_entry()) break;
114     if (f->is_java_frame() && !javaVFrame::cast(f)->method()->is_continuation_enter_intrinsic())
115       return javaVFrame::cast(f);
116     f = f->sender();
117   }
118   return nullptr;
119 }
120 
121 // ------------- javaVFrame --------------
122 
123 GrowableArray<MonitorInfo*>* javaVFrame::locked_monitors() {
124   assert(SafepointSynchronize::is_at_safepoint() || JavaThread::current() == thread(),
125          "must be at safepoint or it's a java frame of the current thread");
126 
127   GrowableArray<MonitorInfo*>* mons = monitors();
128   GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(mons->length());
129   if (mons->is_empty()) return result;
130 
131   bool found_first_monitor = false;
132   // The ObjectMonitor* can't be async deflated since we are either
133   // at a safepoint or the calling thread is operating on itself so
134   // it cannot exit the ObjectMonitor so it remains busy.
135   ObjectMonitor *waiting_monitor = thread()->current_waiting_monitor();
136   ObjectMonitor *pending_monitor = nullptr;
137   if (waiting_monitor == nullptr) {
138     pending_monitor = thread()->current_pending_monitor();
139   }
140   oop pending_obj = (pending_monitor != nullptr ? pending_monitor->object() : (oop) nullptr);
141   oop waiting_obj = (waiting_monitor != nullptr ? waiting_monitor->object() : (oop) nullptr);
142 
143   for (int index = (mons->length()-1); index >= 0; index--) {
144     MonitorInfo* monitor = mons->at(index);
145     if (monitor->eliminated() && is_compiled_frame()) continue; // skip eliminated monitor
146     oop obj = monitor->owner();
147     if (obj == nullptr) continue; // skip unowned monitor
148     //
149     // Skip the monitor that the thread is blocked to enter or waiting on
150     //
151     if (!found_first_monitor && (obj == pending_obj || obj == waiting_obj)) {
152       continue;
153     }
154     found_first_monitor = true;
155     result->append(monitor);
156   }
157   return result;
158 }
159 
160 void javaVFrame::print_locked_object_class_name(outputStream* st, Handle obj, const char* lock_state) {
161   if (obj.not_null()) {
162     st->print("\t- %s <" INTPTR_FORMAT "> ", lock_state, p2i(obj()));
163     if (obj->klass() == vmClasses::Class_klass()) {
164       st->print_cr("(a java.lang.Class for %s)", java_lang_Class::as_external_name(obj()));
165     } else {
166       Klass* k = obj->klass();
167       st->print_cr("(a %s)", k->external_name());
168     }
169   }
170 }
171 
172 void javaVFrame::print_lock_info_on(outputStream* st, int frame_count) {
173   Thread* current = Thread::current();
174   ResourceMark rm(current);
175   HandleMark hm(current);
176 
177   // If this is the first frame and it is java.lang.Object.wait(...)
178   // then print out the receiver. Locals are not always available,
179   // e.g., compiled native frames have no scope so there are no locals.
180   if (frame_count == 0) {
181     if (method()->name() == vmSymbols::wait_name() &&
182         method()->method_holder()->name() == vmSymbols::java_lang_Object()) {
183       const char *wait_state = "waiting on"; // assume we are waiting
184       // If earlier in the output we reported java.lang.Thread.State ==
185       // "WAITING (on object monitor)" and now we report "waiting on", then
186       // we are still waiting for notification or timeout. Otherwise if
187       // we earlier reported java.lang.Thread.State == "BLOCKED (on object
188       // monitor)", then we are actually waiting to re-lock the monitor.
189       StackValueCollection* locs = locals();
190       if (!locs->is_empty()) {
191         StackValue* sv = locs->at(0);
192         if (sv->type() == T_OBJECT) {
193           Handle o = locs->at(0)->get_obj();
194           if (java_lang_Thread::get_thread_status(thread()->threadObj()) ==
195                                 JavaThreadStatus::BLOCKED_ON_MONITOR_ENTER) {
196             wait_state = "waiting to re-lock in wait()";
197           }
198           print_locked_object_class_name(st, o, wait_state);
199         }
200       } else {
201         st->print_cr("\t- %s <no object reference available>", wait_state);
202       }
203     } else if (thread()->current_park_blocker() != nullptr) {
204       oop obj = thread()->current_park_blocker();
205       Klass* k = obj->klass();
206       st->print_cr("\t- %s <" INTPTR_FORMAT "> (a %s)", "parking to wait for ", p2i(obj), k->external_name());
207     }
208     else if (thread()->osthread()->get_state() == OBJECT_WAIT) {
209       // We are waiting on an Object monitor but Object.wait() isn't the
210       // top-frame, so we should be waiting on a Class initialization monitor.
211       InstanceKlass* k = thread()->class_to_be_initialized();
212       if (k != nullptr) {
213         st->print_cr("\t- waiting on the Class initialization monitor for %s", k->external_name());
214       }
215     }
216   }
217 
218   // Print out all monitors that we have locked, or are trying to lock,
219   // including re-locking after being notified or timing out in a wait().
220   GrowableArray<MonitorInfo*>* mons = monitors();
221   if (!mons->is_empty()) {
222     bool found_first_monitor = false;
223     for (int index = (mons->length()-1); index >= 0; index--) {
224       MonitorInfo* monitor = mons->at(index);
225       if (monitor->eliminated() && is_compiled_frame()) { // Eliminated in compiled code
226         if (monitor->owner_is_scalar_replaced()) {
227           Klass* k = java_lang_Class::as_Klass(monitor->owner_klass());
228           st->print_cr("\t- eliminated <owner is scalar replaced> (a %s)", k->external_name());
229         } else {
230           Handle obj(current, monitor->owner());
231           if (obj() != nullptr) {
232             print_locked_object_class_name(st, obj, "eliminated");
233           }
234         }
235         continue;
236       }
237       if (monitor->owner() != nullptr) {
238         // the monitor is associated with an object, i.e., it is locked
239 
240         const char *lock_state = "locked"; // assume we have the monitor locked
241         if (!found_first_monitor && frame_count == 0) {
242           // If this is the first frame and we haven't found an owned
243           // monitor before, then we need to see if we have completed
244           // the lock or if we are blocked trying to acquire it. Only
245           // an inflated monitor that is first on the monitor list in
246           // the first frame can block us on a monitor enter.
247           markWord mark = monitor->owner()->mark();
248           // The first stage of async deflation does not affect any field
249           // used by this comparison so the ObjectMonitor* is usable here.
250           if (mark.has_monitor()) {
251             ObjectMonitor* mon = ObjectSynchronizer::read_monitor(current, monitor->owner(), mark);
252             if (// if the monitor is null we must be in the process of locking
253                 mon == nullptr ||
254                 // we have marked ourself as pending on this monitor
255                 mon == thread()->current_pending_monitor() ||
256                 // we are not the owner of this monitor
257                 !mon->is_entered(thread())) {
258               lock_state = "waiting to lock";
259             }
260           }
261         }
262         print_locked_object_class_name(st, Handle(current, monitor->owner()), lock_state);
263 
264         found_first_monitor = true;
265       }
266     }
267   }
268 }
269 
270 // ------------- interpretedVFrame --------------
271 
272 u_char* interpretedVFrame::bcp() const {
273   return stack_chunk() == nullptr ? fr().interpreter_frame_bcp() : stack_chunk()->interpreter_frame_bcp(fr());
274 }
275 
276 intptr_t* interpretedVFrame::locals_addr_at(int offset) const {
277   assert(stack_chunk() == nullptr, "Not supported for heap frames"); // unsupported for now because seems to be unused
278   assert(fr().is_interpreted_frame(), "frame should be an interpreted frame");
279   return fr().interpreter_frame_local_at(offset);
280 }
281 
282 GrowableArray<MonitorInfo*>* interpretedVFrame::monitors() const {
283   GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(5);
284   if (stack_chunk() == nullptr) { // no monitors in continuations
285     for (BasicObjectLock* current = (fr().previous_monitor_in_interpreter_frame(fr().interpreter_frame_monitor_begin()));
286         current >= fr().interpreter_frame_monitor_end();
287         current = fr().previous_monitor_in_interpreter_frame(current)) {
288       result->push(new MonitorInfo(current->obj(), current->lock(), false, false));
289     }
290   }
291   return result;
292 }
293 
294 int interpretedVFrame::bci() const {
295   return method()->bci_from(bcp());
296 }
297 
298 Method* interpretedVFrame::method() const {
299   return stack_chunk() == nullptr ? fr().interpreter_frame_method() : stack_chunk()->interpreter_frame_method(fr());
300 }
301 
302 static StackValue* create_stack_value_from_oop_map(const InterpreterOopMap& oop_mask,
303                                                    int index,
304                                                    const intptr_t* const addr,
305                                                    stackChunkOop chunk) {
306 
307   assert(index >= 0 && index < oop_mask.number_of_entries(), "invariant");
308 
309   // categorize using oop_mask
310   if (oop_mask.is_oop(index)) {
311     return StackValue::create_stack_value_from_oop_location(chunk, (void*)addr);
312   }
313   // value (integer) "v"
314   return new StackValue(addr != nullptr ? *addr : 0);
315 }
316 
317 static bool is_in_expression_stack(const frame& fr, const intptr_t* const addr) {
318   assert(addr != nullptr, "invariant");
319 
320   // Ensure to be 'inside' the expression stack (i.e., addr >= sp for Intel).
321   // In case of exceptions, the expression stack is invalid and the sp
322   // will be reset to express this condition.
323   if (frame::interpreter_frame_expression_stack_direction() > 0) {
324     return addr <= fr.interpreter_frame_tos_address();
325   }
326 
327   return addr >= fr.interpreter_frame_tos_address();
328 }
329 
330 static void stack_locals(StackValueCollection* result,
331                          int length,
332                          const InterpreterOopMap& oop_mask,
333                          const frame& fr,
334                          const stackChunkOop chunk) {
335 
336   assert(result != nullptr, "invariant");
337 
338   for (int i = 0; i < length; ++i) {
339     const intptr_t* addr;
340     if (chunk == nullptr) {
341       addr = fr.interpreter_frame_local_at(i);
342       assert(addr >= fr.sp(), "must be inside the frame");
343     } else {
344       addr = chunk->interpreter_frame_local_at(fr, i);
345     }
346     assert(addr != nullptr, "invariant");
347 
348     StackValue* const sv = create_stack_value_from_oop_map(oop_mask, i, addr, chunk);
349     assert(sv != nullptr, "sanity check");
350 
351     result->add(sv);
352   }
353 }
354 
355 static void stack_expressions(StackValueCollection* result,
356                               int length,
357                               int max_locals,
358                               const InterpreterOopMap& oop_mask,
359                               const frame& fr,
360                               const stackChunkOop chunk) {
361 
362   assert(result != nullptr, "invariant");
363 
364   for (int i = 0; i < length; ++i) {
365     const intptr_t* addr;
366     if (chunk == nullptr) {
367       addr = fr.interpreter_frame_expression_stack_at(i);
368       assert(addr != nullptr, "invariant");
369       if (!is_in_expression_stack(fr, addr)) {
370         // Need to ensure no bogus escapes.
371         addr = nullptr;
372       }
373     } else {
374       addr = chunk->interpreter_frame_expression_stack_at(fr, i);
375     }
376 
377     StackValue* const sv = create_stack_value_from_oop_map(oop_mask,
378                                                            i + max_locals,
379                                                            addr,
380                                                            chunk);
381     assert(sv != nullptr, "sanity check");
382 
383     result->add(sv);
384   }
385 }
386 
387 StackValueCollection* interpretedVFrame::locals() const {
388   return stack_data(false);
389 }
390 
391 StackValueCollection* interpretedVFrame::expressions() const {
392   return stack_data(true);
393 }
394 
395 /*
396  * Worker routine for fetching references and/or values
397  * for a particular bci in the interpretedVFrame.
398  *
399  * Returns data for either "locals" or "expressions",
400  * using bci relative oop_map (oop_mask) information.
401  *
402  * @param expressions  bool switch controlling what data to return
403                        (false == locals / true == expression)
404  *
405  */
406 StackValueCollection* interpretedVFrame::stack_data(bool expressions) const {
407 
408   InterpreterOopMap oop_mask;
409   method()->mask_for(bci(), &oop_mask);
410   const int mask_len = oop_mask.number_of_entries();
411 
412   // If the method is native, method()->max_locals() is not telling the truth.
413   // For our purposes, max locals instead equals the size of parameters.
414   const int max_locals = method()->is_native() ?
415     method()->size_of_parameters() : method()->max_locals();
416 
417   assert(mask_len >= max_locals, "invariant");
418 
419   const int length = expressions ? mask_len - max_locals : max_locals;
420   assert(length >= 0, "invariant");
421 
422   StackValueCollection* const result = new StackValueCollection(length);
423 
424   if (0 == length) {
425     return result;
426   }
427 
428   if (expressions) {
429     stack_expressions(result, length, max_locals, oop_mask, fr(), stack_chunk());
430   } else {
431     stack_locals(result, length, oop_mask, fr(), stack_chunk());
432   }
433 
434   assert(length == result->size(), "invariant");
435 
436   return result;
437 }
438 
439 void interpretedVFrame::set_locals(StackValueCollection* values) const {
440   if (values == nullptr || values->size() == 0) return;
441 
442   // If the method is native, max_locals is not telling the truth.
443   // maxlocals then equals the size of parameters
444   const int max_locals = method()->is_native() ?
445     method()->size_of_parameters() : method()->max_locals();
446 
447   assert(max_locals == values->size(), "Mismatch between actual stack format and supplied data");
448 
449   // handle locals
450   for (int i = 0; i < max_locals; i++) {
451     // Find stack location
452     intptr_t *addr = locals_addr_at(i);
453 
454     // Depending on oop/int put it in the right package
455     const StackValue* const sv = values->at(i);
456     assert(sv != nullptr, "sanity check");
457     if (sv->type() == T_OBJECT) {
458       *(oop *) addr = (sv->get_obj())();
459     } else {                   // integer
460       *addr = sv->get_intptr();
461     }
462   }
463 }
464 
465 // ------------- cChunk --------------
466 
467 entryVFrame::entryVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread)
468 : externalVFrame(fr, reg_map, thread) {}
469 
470 MonitorInfo::MonitorInfo(oop owner, BasicLock* lock, bool eliminated, bool owner_is_scalar_replaced) {
471   Thread* thread = Thread::current();
472   if (!owner_is_scalar_replaced) {
473     _owner = Handle(thread, owner);
474     _owner_klass = Handle();
475   } else {
476     assert(eliminated, "monitor should be eliminated for scalar replaced object");
477     _owner = Handle();
478     _owner_klass = Handle(thread, owner);
479   }
480   _lock = lock;
481   _eliminated = eliminated;
482   _owner_is_scalar_replaced = owner_is_scalar_replaced;
483 }
484 
485 #ifdef ASSERT
486 void vframeStreamCommon::found_bad_method_frame() const {
487   // 6379830 Cut point for an assertion that occasionally fires when
488   // we are using the performance analyzer.
489   // Disable this when testing the analyzer with fastdebug.
490   fatal("invalid bci or invalid scope desc");
491 }
492 #endif
493 
494 vframeStream::vframeStream(JavaThread* thread, Handle continuation_scope, bool stop_at_java_call_stub)
495  : vframeStreamCommon(RegisterMap(thread,
496                                   RegisterMap::UpdateMap::include,
497                                   RegisterMap::ProcessFrames::include,
498                                   RegisterMap::WalkContinuation::include)) {
499 
500   _stop_at_java_call_stub = stop_at_java_call_stub;
501   _continuation_scope = continuation_scope;
502 
503   if (!thread->has_last_Java_frame()) {
504     _mode = at_end_mode;
505     return;
506   }
507 
508   _frame = _thread->last_frame();
509   _cont_entry = _thread->last_continuation();
510   while (!fill_from_frame()) {
511     _frame = _frame.sender(&_reg_map);
512   }
513 }
514 
515 vframeStream::vframeStream(oop continuation, Handle continuation_scope)
516  : vframeStreamCommon(RegisterMap(continuation, RegisterMap::UpdateMap::include)) {
517 
518   _stop_at_java_call_stub = false;
519   _continuation_scope = continuation_scope;
520 
521   if (!Continuation::has_last_Java_frame(continuation, &_frame, &_reg_map)) {
522     _mode = at_end_mode;
523     return;
524   }
525 
526   // _chunk = _reg_map.stack_chunk();
527   while (!fill_from_frame()) {
528     _frame = _frame.sender(&_reg_map);
529   }
530 }
531 
532 
533 // Step back n frames, skip any pseudo frames in between.
534 // This function is used in Class.forName, Class.newInstance, Method.Invoke,
535 // AccessController.doPrivileged.
536 void vframeStreamCommon::security_get_caller_frame(int depth) {
537   assert(depth >= 0, "invalid depth: %d", depth);
538   for (int n = 0; !at_end(); security_next()) {
539     if (!method()->is_ignored_by_security_stack_walk()) {
540       if (n == depth) {
541         // We have reached the desired depth; return.
542         return;
543       }
544       n++;  // this is a non-skipped frame; count it against the depth
545     }
546   }
547   // NOTE: At this point there were not enough frames on the stack
548   // to walk to depth.  Callers of this method have to check for at_end.
549 }
550 
551 
552 void vframeStreamCommon::security_next() {
553   if (method()->is_prefixed_native()) {
554     skip_prefixed_method_and_wrappers();  // calls next()
555   } else {
556     next();
557   }
558 }
559 
560 
561 void vframeStreamCommon::skip_prefixed_method_and_wrappers() {
562   ResourceMark rm;
563 
564   int    method_prefix_count = 0;
565   char** method_prefixes = JvmtiExport::get_all_native_method_prefixes(&method_prefix_count);
566   Klass* prefixed_klass = method()->method_holder();
567   const char* prefixed_name = method()->name()->as_C_string();
568   size_t prefixed_name_len = strlen(prefixed_name);
569   int prefix_index = method_prefix_count-1;
570 
571   while (!at_end()) {
572     next();
573     if (method()->method_holder() != prefixed_klass) {
574       break; // classes don't match, can't be a wrapper
575     }
576     const char* name = method()->name()->as_C_string();
577     size_t name_len = strlen(name);
578     size_t prefix_len = prefixed_name_len - name_len;
579     if (prefix_len <= 0 || strcmp(name, prefixed_name + prefix_len) != 0) {
580       break; // prefixed name isn't prefixed version of method name, can't be a wrapper
581     }
582     for (; prefix_index >= 0; --prefix_index) {
583       const char* possible_prefix = method_prefixes[prefix_index];
584       size_t possible_prefix_len = strlen(possible_prefix);
585       if (possible_prefix_len == prefix_len &&
586           strncmp(possible_prefix, prefixed_name, prefix_len) == 0) {
587         break; // matching prefix found
588       }
589     }
590     if (prefix_index < 0) {
591       break; // didn't find the prefix, can't be a wrapper
592     }
593     prefixed_name = name;
594     prefixed_name_len = name_len;
595   }
596 }
597 
598 javaVFrame* vframeStreamCommon::asJavaVFrame() {
599   javaVFrame* result = nullptr;
600   // FIXME, need to re-do JDK-8271140 and check is_native_frame?
601   if (_mode == compiled_mode && _frame.is_compiled_frame()) {
602     assert(_frame.is_compiled_frame() || _frame.is_native_frame(), "expected compiled Java frame");
603     guarantee(_reg_map.update_map(), "");
604 
605     compiledVFrame* cvf = compiledVFrame::cast(vframe::new_vframe(&_frame, &_reg_map, _thread));
606 
607     guarantee(cvf->cb() == cb(), "wrong code blob");
608 
609     cvf = cvf->at_scope(_decode_offset, _vframe_id); // get the same scope as this stream
610 
611     guarantee(cvf->scope()->decode_offset() == _decode_offset, "wrong scope");
612     guarantee(cvf->scope()->sender_decode_offset() == _sender_decode_offset, "wrong scope");
613     guarantee(cvf->vframe_id() == _vframe_id, "wrong vframe");
614 
615     result = cvf;
616   } else {
617     result = javaVFrame::cast(vframe::new_vframe(&_frame, &_reg_map, _thread));
618   }
619   assert(result->method() == method(), "wrong method");
620   return result;
621 }
622 
623 #ifndef PRODUCT
624 void vframe::print(outputStream* output) {
625   if (WizardMode) _fr.print_value_on(output);
626 }
627 
628 void vframe::print_value(outputStream* output) const {
629   ((vframe*)this)->print(output);
630 }
631 
632 
633 void entryVFrame::print_value(outputStream* output) const {
634   ((entryVFrame*)this)->print(output);
635 }
636 
637 void entryVFrame::print(outputStream* output) {
638   vframe::print(output);
639   output->print_cr("C Chunk in between Java");
640   output->print_cr("C     link " INTPTR_FORMAT, p2i(_fr.link()));
641 }
642 
643 
644 // ------------- javaVFrame --------------
645 
646 static void print_stack_values(outputStream* output, const char* title, StackValueCollection* values) {
647   if (values->is_empty()) return;
648   output->print_cr("\t%s:", title);
649   values->print();
650 }
651 
652 
653 void javaVFrame::print(outputStream* output) {
654   Thread* current_thread = Thread::current();
655   ResourceMark rm(current_thread);
656   HandleMark hm(current_thread);
657 
658   vframe::print(output);
659   output->print("\t");
660   method()->print_value();
661   output->cr();
662   output->print_cr("\tbci:    %d", bci());
663 
664   print_stack_values(output, "locals",      locals());
665   print_stack_values(output, "expressions", expressions());
666 
667   GrowableArray<MonitorInfo*>* list = monitors();
668   if (list->is_empty()) return;
669   output->print_cr("\tmonitor list:");
670   for (int index = (list->length()-1); index >= 0; index--) {
671     MonitorInfo* monitor = list->at(index);
672     output->print("\t  obj\t");
673     if (monitor->owner_is_scalar_replaced()) {
674       Klass* k = java_lang_Class::as_Klass(monitor->owner_klass());
675       output->print("( is scalar replaced %s)", k->external_name());
676     } else if (monitor->owner() == nullptr) {
677       output->print("( null )");
678     } else {
679       monitor->owner()->print_value();
680       output->print("(owner=" INTPTR_FORMAT ")", p2i(monitor->owner()));
681     }
682     if (monitor->eliminated()) {
683       if(is_compiled_frame()) {
684         output->print(" ( lock is eliminated in compiled frame )");
685       } else {
686         output->print(" ( lock is eliminated, frame not compiled )");
687       }
688     }
689     output->cr();
690     output->print("\t  ");
691     monitor->lock()->print_on(output, monitor->owner());
692     output->cr();
693   }
694 }
695 
696 
697 void javaVFrame::print_value(outputStream* output) const {
698   Method*    m = method();
699   InstanceKlass*     k = m->method_holder();
700   output->print_cr("frame( sp=" INTPTR_FORMAT ", unextended_sp=" INTPTR_FORMAT ", fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT ")",
701                 p2i(_fr.sp()),  p2i(_fr.unextended_sp()), p2i(_fr.fp()), p2i(_fr.pc()));
702   output->print("%s.%s", k->internal_name(), m->name()->as_C_string());
703 
704   if (!m->is_native()) {
705     Symbol*  source_name = k->source_file_name();
706     int        line_number = m->line_number_from_bci(bci());
707     if (source_name != nullptr && (line_number != -1)) {
708       output->print("(%s:%d)", source_name->as_C_string(), line_number);
709     }
710   } else {
711     output->print("(Native Method)");
712   }
713   // Check frame size and print warning if it looks suspiciously large
714   if (fr().sp() != nullptr) {
715     RegisterMap map = *register_map();
716     uint size = fr().frame_size();
717 #ifdef _LP64
718     if (size > 8*K) warning("SUSPICIOUSLY LARGE FRAME (%d)", size);
719 #else
720     if (size > 4*K) warning("SUSPICIOUSLY LARGE FRAME (%d)", size);
721 #endif
722   }
723 }
724 
725 void javaVFrame::print_activation(int index, outputStream* output) const {
726   // frame number and method
727   output->print("%2d - ", index);
728   ((vframe*)this)->print_value();
729   output->cr();
730 
731   if (WizardMode) {
732     ((vframe*)this)->print();
733     output->cr();
734   }
735 }
736 
737 // ------------- externalVFrame --------------
738 
739 void externalVFrame::print(outputStream* output) {
740   _fr.print_value_on(output);
741 }
742 
743 void externalVFrame::print_value(outputStream* output) const {
744   ((vframe*)this)->print(output);
745 }
746 #endif // PRODUCT