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