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