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