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