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 #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.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_compiled()) {
 75       CompiledMethod* nm = (CompiledMethod*)cb;
 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() == CONDVAR_WAIT) {
209       // We are waiting on the native class initialization monitor.
210       InstanceKlass* k = thread()->class_to_be_initialized();
211       if (k != nullptr) {
212         st->print_cr("\t- waiting on the Class initialization monitor for %s", k->external_name());
213       }
214     }
215   }
216 
217   // Print out all monitors that we have locked, or are trying to lock,
218   // including re-locking after being notified or timing out in a wait().
219   GrowableArray<MonitorInfo*>* mons = monitors();
220   if (!mons->is_empty()) {
221     bool found_first_monitor = false;
222     for (int index = (mons->length()-1); index >= 0; index--) {
223       MonitorInfo* monitor = mons->at(index);
224       if (monitor->eliminated() && is_compiled_frame()) { // Eliminated in compiled code
225         if (monitor->owner_is_scalar_replaced()) {
226           Klass* k = java_lang_Class::as_Klass(monitor->owner_klass());
227           st->print_cr("\t- eliminated <owner is scalar replaced> (a %s)", k->external_name());
228         } else {
229           Handle obj(current, monitor->owner());
230           if (obj() != nullptr) {
231             print_locked_object_class_name(st, obj, "eliminated");
232           }
233         }
234         continue;
235       }
236       if (monitor->owner() != nullptr) {
237         // the monitor is associated with an object, i.e., it is locked
238 
239         const char *lock_state = "locked"; // assume we have the monitor locked
240         if (!found_first_monitor && frame_count == 0) {
241           // If this is the first frame and we haven't found an owned
242           // monitor before, then we need to see if we have completed
243           // the lock or if we are blocked trying to acquire it. Only
244           // an inflated monitor that is first on the monitor list in
245           // the first frame can block us on a monitor enter.
246           markWord mark = monitor->owner()->mark();
247           // The first stage of async deflation does not affect any field
248           // used by this comparison so the ObjectMonitor* is usable here.
249           if (mark.has_monitor() &&
250               ( // we have marked ourself as pending on this monitor
251                 mark.monitor() == thread()->current_pending_monitor() ||
252                 // we are not the owner of this monitor
253                 !mark.monitor()->is_entered(thread())
254               )) {
255             lock_state = "waiting to lock";
256           }
257         }
258         print_locked_object_class_name(st, Handle(current, monitor->owner()), lock_state);
259 
260         found_first_monitor = true;
261       }
262     }
263   }
264 }
265 
266 // ------------- interpretedVFrame --------------
267 
268 u_char* interpretedVFrame::bcp() const {
269   return stack_chunk() == nullptr ? fr().interpreter_frame_bcp() : stack_chunk()->interpreter_frame_bcp(fr());
270 }
271 
272 intptr_t* interpretedVFrame::locals_addr_at(int offset) const {
273   assert(stack_chunk() == nullptr, "Not supported for heap frames"); // unsupported for now because seems to be unused
274   assert(fr().is_interpreted_frame(), "frame should be an interpreted frame");
275   return fr().interpreter_frame_local_at(offset);
276 }
277 
278 GrowableArray<MonitorInfo*>* interpretedVFrame::monitors() const {
279   bool heap_frame = stack_chunk() != nullptr;
280   frame f = !heap_frame ? _fr : stack_chunk()->derelativize(_fr);
281   GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(5);
282   for (BasicObjectLock* current = (f.previous_monitor_in_interpreter_frame(f.interpreter_frame_monitor_begin()));
283       current >= f.interpreter_frame_monitor_end();
284       current = f.previous_monitor_in_interpreter_frame(current)) {
285       oop owner = !heap_frame ? current->obj() : StackValue::create_stack_value_from_oop_location(stack_chunk(), (void*)current->obj_adr())->get_obj()();
286     result->push(new MonitorInfo(owner, current->lock(), false, false));
287   }
288   return result;
289 }
290 
291 int interpretedVFrame::bci() const {
292   return method()->bci_from(bcp());
293 }
294 
295 Method* interpretedVFrame::method() const {
296   return stack_chunk() == nullptr ? fr().interpreter_frame_method() : stack_chunk()->interpreter_frame_method(fr());
297 }
298 
299 static StackValue* create_stack_value_from_oop_map(const InterpreterOopMap& oop_mask,
300                                                    int index,
301                                                    const intptr_t* const addr,
302                                                    stackChunkOop chunk) {
303 
304   assert(index >= 0 && index < oop_mask.number_of_entries(), "invariant");
305 
306   // categorize using oop_mask
307   if (oop_mask.is_oop(index)) {
308     return StackValue::create_stack_value_from_oop_location(chunk, (void*)addr);
309   }
310   // value (integer) "v"
311   return new StackValue(addr != nullptr ? *addr : 0);
312 }
313 
314 static bool is_in_expression_stack(const frame& fr, const intptr_t* const addr) {
315   assert(addr != nullptr, "invariant");
316 
317   // Ensure to be 'inside' the expression stack (i.e., addr >= sp for Intel).
318   // In case of exceptions, the expression stack is invalid and the sp
319   // will be reset to express this condition.
320   if (frame::interpreter_frame_expression_stack_direction() > 0) {
321     return addr <= fr.interpreter_frame_tos_address();
322   }
323 
324   return addr >= fr.interpreter_frame_tos_address();
325 }
326 
327 static void stack_locals(StackValueCollection* result,
328                          int length,
329                          const InterpreterOopMap& oop_mask,
330                          const frame& fr,
331                          const stackChunkOop chunk) {
332 
333   assert(result != nullptr, "invariant");
334 
335   for (int i = 0; i < length; ++i) {
336     const intptr_t* addr;
337     if (chunk == nullptr) {
338       addr = fr.interpreter_frame_local_at(i);
339       assert(addr >= fr.sp(), "must be inside the frame");
340     } else {
341       addr = chunk->interpreter_frame_local_at(fr, i);
342     }
343     assert(addr != nullptr, "invariant");
344 
345     StackValue* const sv = create_stack_value_from_oop_map(oop_mask, i, addr, chunk);
346     assert(sv != nullptr, "sanity check");
347 
348     result->add(sv);
349   }
350 }
351 
352 static void stack_expressions(StackValueCollection* result,
353                               int length,
354                               int max_locals,
355                               const InterpreterOopMap& oop_mask,
356                               const frame& fr,
357                               const stackChunkOop chunk) {
358 
359   assert(result != nullptr, "invariant");
360 
361   for (int i = 0; i < length; ++i) {
362     const intptr_t* addr;
363     if (chunk == nullptr) {
364       addr = fr.interpreter_frame_expression_stack_at(i);
365       assert(addr != nullptr, "invariant");
366       if (!is_in_expression_stack(fr, addr)) {
367         // Need to ensure no bogus escapes.
368         addr = nullptr;
369       }
370     } else {
371       addr = chunk->interpreter_frame_expression_stack_at(fr, i);
372     }
373 
374     StackValue* const sv = create_stack_value_from_oop_map(oop_mask,
375                                                            i + max_locals,
376                                                            addr,
377                                                            chunk);
378     assert(sv != nullptr, "sanity check");
379 
380     result->add(sv);
381   }
382 }
383 
384 StackValueCollection* interpretedVFrame::locals() const {
385   return stack_data(false);
386 }
387 
388 StackValueCollection* interpretedVFrame::expressions() const {
389   return stack_data(true);
390 }
391 
392 /*
393  * Worker routine for fetching references and/or values
394  * for a particular bci in the interpretedVFrame.
395  *
396  * Returns data for either "locals" or "expressions",
397  * using bci relative oop_map (oop_mask) information.
398  *
399  * @param expressions  bool switch controlling what data to return
400                        (false == locals / true == expression)
401  *
402  */
403 StackValueCollection* interpretedVFrame::stack_data(bool expressions) const {
404 
405   InterpreterOopMap oop_mask;
406   method()->mask_for(bci(), &oop_mask);
407   const int mask_len = oop_mask.number_of_entries();
408 
409   // If the method is native, method()->max_locals() is not telling the truth.
410   // For our purposes, max locals instead equals the size of parameters.
411   const int max_locals = method()->is_native() ?
412     method()->size_of_parameters() : method()->max_locals();
413 
414   assert(mask_len >= max_locals, "invariant");
415 
416   const int length = expressions ? mask_len - max_locals : max_locals;
417   assert(length >= 0, "invariant");
418 
419   StackValueCollection* const result = new StackValueCollection(length);
420 
421   if (0 == length) {
422     return result;
423   }
424 
425   if (expressions) {
426     stack_expressions(result, length, max_locals, oop_mask, fr(), stack_chunk());
427   } else {
428     stack_locals(result, length, oop_mask, fr(), stack_chunk());
429   }
430 
431   assert(length == result->size(), "invariant");
432 
433   return result;
434 }
435 
436 void interpretedVFrame::set_locals(StackValueCollection* values) const {
437   if (values == nullptr || values->size() == 0) return;
438 
439   // If the method is native, max_locals is not telling the truth.
440   // maxlocals then equals the size of parameters
441   const int max_locals = method()->is_native() ?
442     method()->size_of_parameters() : method()->max_locals();
443 
444   assert(max_locals == values->size(), "Mismatch between actual stack format and supplied data");
445 
446   // handle locals
447   for (int i = 0; i < max_locals; i++) {
448     // Find stack location
449     intptr_t *addr = locals_addr_at(i);
450 
451     // Depending on oop/int put it in the right package
452     const StackValue* const sv = values->at(i);
453     assert(sv != nullptr, "sanity check");
454     if (sv->type() == T_OBJECT) {
455       *(oop *) addr = (sv->get_obj())();
456     } else {                   // integer
457       *addr = sv->get_intptr();
458     }
459   }
460 }
461 
462 // ------------- cChunk --------------
463 
464 entryVFrame::entryVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread)
465 : externalVFrame(fr, reg_map, thread) {}
466 
467 MonitorInfo::MonitorInfo(oop owner, BasicLock* lock, bool eliminated, bool owner_is_scalar_replaced) {
468   Thread* thread = Thread::current();
469   if (!owner_is_scalar_replaced) {
470     _owner = Handle(thread, owner);
471     _owner_klass = Handle();
472   } else {
473     assert(eliminated, "monitor should be eliminated for scalar replaced object");
474     _owner = Handle();
475     _owner_klass = Handle(thread, owner);
476   }
477   _lock = lock;
478   _eliminated = eliminated;
479   _owner_is_scalar_replaced = owner_is_scalar_replaced;
480 }
481 
482 #ifdef ASSERT
483 void vframeStreamCommon::found_bad_method_frame() const {
484   // 6379830 Cut point for an assertion that occasionally fires when
485   // we are using the performance analyzer.
486   // Disable this when testing the analyzer with fastdebug.
487   fatal("invalid bci or invalid scope desc");
488 }
489 #endif
490 
491 vframeStream::vframeStream(JavaThread* thread, Handle continuation_scope, bool stop_at_java_call_stub)
492  : vframeStreamCommon(RegisterMap(thread,
493                                   RegisterMap::UpdateMap::include,
494                                   RegisterMap::ProcessFrames::include,
495                                   RegisterMap::WalkContinuation::include)) {
496 
497   _stop_at_java_call_stub = stop_at_java_call_stub;
498   _continuation_scope = continuation_scope;
499 
500   if (!thread->has_last_Java_frame()) {
501     _mode = at_end_mode;
502     return;
503   }
504 
505   _frame = _thread->last_frame();
506   _cont_entry = _thread->last_continuation();
507   while (!fill_from_frame()) {
508     _frame = _frame.sender(&_reg_map);
509   }
510 }
511 
512 vframeStream::vframeStream(oop continuation, Handle continuation_scope)
513  : vframeStreamCommon(RegisterMap(continuation, RegisterMap::UpdateMap::include)) {
514 
515   _stop_at_java_call_stub = false;
516   _continuation_scope = continuation_scope;
517 
518   if (!Continuation::has_last_Java_frame(continuation, &_frame, &_reg_map)) {
519     _mode = at_end_mode;
520     return;
521   }
522 
523   // _chunk = _reg_map.stack_chunk();
524   while (!fill_from_frame()) {
525     _frame = _frame.sender(&_reg_map);
526   }
527 }
528 
529 
530 // Step back n frames, skip any pseudo frames in between.
531 // This function is used in Class.forName, Class.newInstance, Method.Invoke,
532 // AccessController.doPrivileged.
533 void vframeStreamCommon::security_get_caller_frame(int depth) {
534   assert(depth >= 0, "invalid depth: %d", depth);
535   for (int n = 0; !at_end(); security_next()) {
536     if (!method()->is_ignored_by_security_stack_walk()) {
537       if (n == depth) {
538         // We have reached the desired depth; return.
539         return;
540       }
541       n++;  // this is a non-skipped frame; count it against the depth
542     }
543   }
544   // NOTE: At this point there were not enough frames on the stack
545   // to walk to depth.  Callers of this method have to check for at_end.
546 }
547 
548 
549 void vframeStreamCommon::security_next() {
550   if (method()->is_prefixed_native()) {
551     skip_prefixed_method_and_wrappers();  // calls next()
552   } else {
553     next();
554   }
555 }
556 
557 
558 void vframeStreamCommon::skip_prefixed_method_and_wrappers() {
559   ResourceMark rm;
560 
561   int    method_prefix_count = 0;
562   char** method_prefixes = JvmtiExport::get_all_native_method_prefixes(&method_prefix_count);
563   Klass* prefixed_klass = method()->method_holder();
564   const char* prefixed_name = method()->name()->as_C_string();
565   size_t prefixed_name_len = strlen(prefixed_name);
566   int prefix_index = method_prefix_count-1;
567 
568   while (!at_end()) {
569     next();
570     if (method()->method_holder() != prefixed_klass) {
571       break; // classes don't match, can't be a wrapper
572     }
573     const char* name = method()->name()->as_C_string();
574     size_t name_len = strlen(name);
575     size_t prefix_len = prefixed_name_len - name_len;
576     if (prefix_len <= 0 || strcmp(name, prefixed_name + prefix_len) != 0) {
577       break; // prefixed name isn't prefixed version of method name, can't be a wrapper
578     }
579     for (; prefix_index >= 0; --prefix_index) {
580       const char* possible_prefix = method_prefixes[prefix_index];
581       size_t possible_prefix_len = strlen(possible_prefix);
582       if (possible_prefix_len == prefix_len &&
583           strncmp(possible_prefix, prefixed_name, prefix_len) == 0) {
584         break; // matching prefix found
585       }
586     }
587     if (prefix_index < 0) {
588       break; // didn't find the prefix, can't be a wrapper
589     }
590     prefixed_name = name;
591     prefixed_name_len = name_len;
592   }
593 }
594 
595 javaVFrame* vframeStreamCommon::asJavaVFrame() {
596   javaVFrame* result = nullptr;
597   // FIXME, need to re-do JDK-8271140 and check is_native_frame?
598   if (_mode == compiled_mode && _frame.is_compiled_frame()) {
599     assert(_frame.is_compiled_frame() || _frame.is_native_frame(), "expected compiled Java frame");
600     guarantee(_reg_map.update_map(), "");
601 
602     compiledVFrame* cvf = compiledVFrame::cast(vframe::new_vframe(&_frame, &_reg_map, _thread));
603 
604     guarantee(cvf->cb() == cb(), "wrong code blob");
605 
606     cvf = cvf->at_scope(_decode_offset, _vframe_id); // get the same scope as this stream
607 
608     guarantee(cvf->scope()->decode_offset() == _decode_offset, "wrong scope");
609     guarantee(cvf->scope()->sender_decode_offset() == _sender_decode_offset, "wrong scope");
610     guarantee(cvf->vframe_id() == _vframe_id, "wrong vframe");
611 
612     result = cvf;
613   } else {
614     result = javaVFrame::cast(vframe::new_vframe(&_frame, &_reg_map, _thread));
615   }
616   assert(result->method() == method(), "wrong method");
617   return result;
618 }
619 
620 #ifndef PRODUCT
621 void vframe::print() {
622   if (WizardMode) _fr.print_value_on(tty,nullptr);
623 }
624 
625 void vframe::print_value() const {
626   ((vframe*)this)->print();
627 }
628 
629 
630 void entryVFrame::print_value() const {
631   ((entryVFrame*)this)->print();
632 }
633 
634 void entryVFrame::print() {
635   vframe::print();
636   tty->print_cr("C Chunk in between Java");
637   tty->print_cr("C     link " INTPTR_FORMAT, p2i(_fr.link()));
638 }
639 
640 
641 // ------------- javaVFrame --------------
642 
643 static void print_stack_values(const char* title, StackValueCollection* values) {
644   if (values->is_empty()) return;
645   tty->print_cr("\t%s:", title);
646   values->print();
647 }
648 
649 
650 void javaVFrame::print() {
651   Thread* current_thread = Thread::current();
652   ResourceMark rm(current_thread);
653   HandleMark hm(current_thread);
654 
655   vframe::print();
656   tty->print("\t");
657   method()->print_value();
658   tty->cr();
659   tty->print_cr("\tbci:    %d", bci());
660 
661   print_stack_values("locals",      locals());
662   print_stack_values("expressions", expressions());
663 
664   GrowableArray<MonitorInfo*>* list = monitors();
665   if (list->is_empty()) return;
666   tty->print_cr("\tmonitor list:");
667   for (int index = (list->length()-1); index >= 0; index--) {
668     MonitorInfo* monitor = list->at(index);
669     tty->print("\t  obj\t");
670     if (monitor->owner_is_scalar_replaced()) {
671       Klass* k = java_lang_Class::as_Klass(monitor->owner_klass());
672       tty->print("( is scalar replaced %s)", k->external_name());
673     } else if (monitor->owner() == nullptr) {
674       tty->print("( null )");
675     } else {
676       monitor->owner()->print_value();
677       tty->print("(owner=" INTPTR_FORMAT ")", p2i(monitor->owner()));
678     }
679     if (monitor->eliminated()) {
680       if(is_compiled_frame()) {
681         tty->print(" ( lock is eliminated in compiled frame )");
682       } else {
683         tty->print(" ( lock is eliminated, frame not compiled )");
684       }
685     }
686     tty->cr();
687     tty->print("\t  ");
688     monitor->lock()->print_on(tty, monitor->owner());
689     tty->cr();
690   }
691 }
692 
693 
694 void javaVFrame::print_value() const {
695   Method*    m = method();
696   InstanceKlass*     k = m->method_holder();
697   tty->print_cr("frame( sp=" INTPTR_FORMAT ", unextended_sp=" INTPTR_FORMAT ", fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT ")",
698                 p2i(_fr.sp()),  p2i(_fr.unextended_sp()), p2i(_fr.fp()), p2i(_fr.pc()));
699   tty->print("%s.%s", k->internal_name(), m->name()->as_C_string());
700 
701   if (!m->is_native()) {
702     Symbol*  source_name = k->source_file_name();
703     int        line_number = m->line_number_from_bci(bci());
704     if (source_name != nullptr && (line_number != -1)) {
705       tty->print("(%s:%d)", source_name->as_C_string(), line_number);
706     }
707   } else {
708     tty->print("(Native Method)");
709   }
710   // Check frame size and print warning if it looks suspiciously large
711   if (fr().sp() != nullptr) {
712     RegisterMap map = *register_map();
713     uint size = fr().frame_size();
714 #ifdef _LP64
715     if (size > 8*K) warning("SUSPICIOUSLY LARGE FRAME (%d)", size);
716 #else
717     if (size > 4*K) warning("SUSPICIOUSLY LARGE FRAME (%d)", size);
718 #endif
719   }
720 }
721 
722 void javaVFrame::print_activation(int index) const {
723   // frame number and method
724   tty->print("%2d - ", index);
725   ((vframe*)this)->print_value();
726   tty->cr();
727 
728   if (WizardMode) {
729     ((vframe*)this)->print();
730     tty->cr();
731   }
732 }
733 
734 // ------------- externalVFrame --------------
735 
736 void externalVFrame::print() {
737   _fr.print_value_on(tty,nullptr);
738 }
739 
740 void externalVFrame::print_value() const {
741   ((vframe*)this)->print();
742 }
743 #endif // PRODUCT