vframe.cpp revision 844:bd02caa94611
1/*
2 * Copyright 1997-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25# include "incls/_precompiled.incl"
26# include "incls/_vframe.cpp.incl"
27
28vframe::vframe(const frame* fr, const RegisterMap* reg_map, JavaThread* thread)
29: _reg_map(reg_map), _thread(thread) {
30  assert(fr != NULL, "must have frame");
31  _fr = *fr;
32}
33
34vframe::vframe(const frame* fr, JavaThread* thread)
35: _reg_map(thread), _thread(thread) {
36  assert(fr != NULL, "must have frame");
37  _fr = *fr;
38}
39
40vframe* vframe::new_vframe(const frame* f, const RegisterMap* reg_map, JavaThread* thread) {
41  // Interpreter frame
42  if (f->is_interpreted_frame()) {
43    return new interpretedVFrame(f, reg_map, thread);
44  }
45
46  // Compiled frame
47  CodeBlob* cb = f->cb();
48  if (cb != NULL) {
49    if (cb->is_nmethod()) {
50      nmethod* nm = (nmethod*)cb;
51      return new compiledVFrame(f, reg_map, thread, nm);
52    }
53
54    if (f->is_runtime_frame()) {
55      // Skip this frame and try again.
56      RegisterMap temp_map = *reg_map;
57      frame s = f->sender(&temp_map);
58      return new_vframe(&s, &temp_map, thread);
59    }
60  }
61
62  // External frame
63  return new externalVFrame(f, reg_map, thread);
64}
65
66vframe* vframe::sender() const {
67  RegisterMap temp_map = *register_map();
68  assert(is_top(), "just checking");
69  if (_fr.is_entry_frame() && _fr.is_first_frame()) return NULL;
70  frame s = _fr.real_sender(&temp_map);
71  if (s.is_first_frame()) return NULL;
72  return vframe::new_vframe(&s, &temp_map, thread());
73}
74
75vframe* vframe::top() const {
76  vframe* vf = (vframe*) this;
77  while (!vf->is_top()) vf = vf->sender();
78  return vf;
79}
80
81
82javaVFrame* vframe::java_sender() const {
83  vframe* f = sender();
84  while (f != NULL) {
85    if (f->is_java_frame()) return javaVFrame::cast(f);
86    f = f->sender();
87  }
88  return NULL;
89}
90
91// ------------- javaVFrame --------------
92
93GrowableArray<MonitorInfo*>* javaVFrame::locked_monitors() {
94  assert(SafepointSynchronize::is_at_safepoint() || JavaThread::current() == thread(),
95         "must be at safepoint or it's a java frame of the current thread");
96
97  GrowableArray<MonitorInfo*>* mons = monitors();
98  GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(mons->length());
99  if (mons->is_empty()) return result;
100
101  bool found_first_monitor = false;
102  ObjectMonitor *pending_monitor = thread()->current_pending_monitor();
103  ObjectMonitor *waiting_monitor = thread()->current_waiting_monitor();
104  oop pending_obj = (pending_monitor != NULL ? (oop) pending_monitor->object() : NULL);
105  oop waiting_obj = (waiting_monitor != NULL ? (oop) waiting_monitor->object() : NULL);
106
107  for (int index = (mons->length()-1); index >= 0; index--) {
108    MonitorInfo* monitor = mons->at(index);
109    if (monitor->eliminated() && is_compiled_frame()) continue; // skip eliminated monitor
110    oop obj = monitor->owner();
111    if (obj == NULL) continue; // skip unowned monitor
112    //
113    // Skip the monitor that the thread is blocked to enter or waiting on
114    //
115    if (!found_first_monitor && (obj == pending_obj || obj == waiting_obj)) {
116      continue;
117    }
118    found_first_monitor = true;
119    result->append(monitor);
120  }
121  return result;
122}
123
124static void print_locked_object_class_name(outputStream* st, Handle obj, const char* lock_state) {
125  if (obj.not_null()) {
126    st->print("\t- %s <" INTPTR_FORMAT "> ", lock_state, (address)obj());
127    if (obj->klass() == SystemDictionary::class_klass()) {
128      klassOop target_klass = java_lang_Class::as_klassOop(obj());
129      st->print_cr("(a java.lang.Class for %s)", instanceKlass::cast(target_klass)->external_name());
130    } else {
131      Klass* k = Klass::cast(obj->klass());
132      st->print_cr("(a %s)", k->external_name());
133    }
134  }
135}
136
137void javaVFrame::print_lock_info_on(outputStream* st, int frame_count) {
138  ResourceMark rm;
139
140  // If this is the first frame, and java.lang.Object.wait(...) then print out the receiver.
141  if (frame_count == 0) {
142    if (method()->name() == vmSymbols::wait_name() &&
143        instanceKlass::cast(method()->method_holder())->name() == vmSymbols::java_lang_Object()) {
144      StackValueCollection* locs = locals();
145      if (!locs->is_empty()) {
146        StackValue* sv = locs->at(0);
147        if (sv->type() == T_OBJECT) {
148          Handle o = locs->at(0)->get_obj();
149          print_locked_object_class_name(st, o, "waiting on");
150        }
151      }
152    } else if (thread()->current_park_blocker() != NULL) {
153      oop obj = thread()->current_park_blocker();
154      Klass* k = Klass::cast(obj->klass());
155      st->print_cr("\t- %s <" INTPTR_FORMAT "> (a %s)", "parking to wait for ", (address)obj, k->external_name());
156    }
157  }
158
159
160  // Print out all monitors that we have locked or are trying to lock
161  GrowableArray<MonitorInfo*>* mons = monitors();
162  if (!mons->is_empty()) {
163    bool found_first_monitor = false;
164    for (int index = (mons->length()-1); index >= 0; index--) {
165      MonitorInfo* monitor = mons->at(index);
166      if (monitor->eliminated() && is_compiled_frame()) { // Eliminated in compiled code
167        if (monitor->owner_is_scalar_replaced()) {
168          Klass* k = Klass::cast(monitor->owner_klass());
169          st->print("\t- eliminated <owner is scalar replaced> (a %s)", k->external_name());
170        } else {
171          oop obj = monitor->owner();
172          if (obj != NULL) {
173            print_locked_object_class_name(st, obj, "eliminated");
174          }
175        }
176        continue;
177      }
178      if (monitor->owner() != NULL) {
179
180        // First, assume we have the monitor locked. If we haven't found an
181        // owned monitor before and this is the first frame, then we need to
182        // see if we have completed the lock or we are blocked trying to
183        // acquire it - we can only be blocked if the monitor is inflated
184
185        const char *lock_state = "locked"; // assume we have the monitor locked
186        if (!found_first_monitor && frame_count == 0) {
187          markOop mark = monitor->owner()->mark();
188          if (mark->has_monitor() &&
189              mark->monitor() == thread()->current_pending_monitor()) {
190            lock_state = "waiting to lock";
191          }
192        }
193
194        found_first_monitor = true;
195        print_locked_object_class_name(st, monitor->owner(), lock_state);
196      }
197    }
198  }
199}
200
201// ------------- interpretedVFrame --------------
202
203u_char* interpretedVFrame::bcp() const {
204  return fr().interpreter_frame_bcp();
205}
206
207void interpretedVFrame::set_bcp(u_char* bcp) {
208  fr().interpreter_frame_set_bcp(bcp);
209}
210
211intptr_t* interpretedVFrame::locals_addr_at(int offset) const {
212  assert(fr().is_interpreted_frame(), "frame should be an interpreted frame");
213  return fr().interpreter_frame_local_at(offset);
214}
215
216
217GrowableArray<MonitorInfo*>* interpretedVFrame::monitors() const {
218  GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(5);
219  for (BasicObjectLock* current = (fr().previous_monitor_in_interpreter_frame(fr().interpreter_frame_monitor_begin()));
220       current >= fr().interpreter_frame_monitor_end();
221       current = fr().previous_monitor_in_interpreter_frame(current)) {
222    result->push(new MonitorInfo(current->obj(), current->lock(), false, false));
223  }
224  return result;
225}
226
227int interpretedVFrame::bci() const {
228  return method()->bci_from(bcp());
229}
230
231methodOop interpretedVFrame::method() const {
232  return fr().interpreter_frame_method();
233}
234
235StackValueCollection* interpretedVFrame::locals() const {
236  int length = method()->max_locals();
237
238  if (method()->is_native()) {
239    // If the method is native, max_locals is not telling the truth.
240    // maxlocals then equals the size of parameters
241    length = method()->size_of_parameters();
242  }
243
244  StackValueCollection* result = new StackValueCollection(length);
245
246  // Get oopmap describing oops and int for current bci
247  if (TaggedStackInterpreter) {
248    for(int i=0; i < length; i++) {
249      // Find stack location
250      intptr_t *addr = locals_addr_at(i);
251
252      // Depending on oop/int put it in the right package
253      StackValue *sv;
254      frame::Tag tag = fr().interpreter_frame_local_tag(i);
255      if (tag == frame::TagReference) {
256        // oop value
257        Handle h(*(oop *)addr);
258        sv = new StackValue(h);
259      } else {
260        // integer
261        sv = new StackValue(*addr);
262      }
263      assert(sv != NULL, "sanity check");
264      result->add(sv);
265    }
266  } else {
267    InterpreterOopMap oop_mask;
268    if (TraceDeoptimization && Verbose) {
269      methodHandle m_h(thread(), method());
270      OopMapCache::compute_one_oop_map(m_h, bci(), &oop_mask);
271    } else {
272      method()->mask_for(bci(), &oop_mask);
273    }
274    // handle locals
275    for(int i=0; i < length; i++) {
276      // Find stack location
277      intptr_t *addr = locals_addr_at(i);
278
279      // Depending on oop/int put it in the right package
280      StackValue *sv;
281      if (oop_mask.is_oop(i)) {
282        // oop value
283        Handle h(*(oop *)addr);
284        sv = new StackValue(h);
285      } else {
286        // integer
287        sv = new StackValue(*addr);
288      }
289      assert(sv != NULL, "sanity check");
290      result->add(sv);
291    }
292  }
293  return result;
294}
295
296void interpretedVFrame::set_locals(StackValueCollection* values) const {
297  if (values == NULL || values->size() == 0) return;
298
299  int length = method()->max_locals();
300  if (method()->is_native()) {
301    // If the method is native, max_locals is not telling the truth.
302    // maxlocals then equals the size of parameters
303    length = method()->size_of_parameters();
304  }
305
306  assert(length == values->size(), "Mismatch between actual stack format and supplied data");
307
308  // handle locals
309  for (int i = 0; i < length; i++) {
310    // Find stack location
311    intptr_t *addr = locals_addr_at(i);
312
313    // Depending on oop/int put it in the right package
314    StackValue *sv = values->at(i);
315    assert(sv != NULL, "sanity check");
316    if (sv->type() == T_OBJECT) {
317      *(oop *) addr = (sv->get_obj())();
318    } else {                   // integer
319      *addr = sv->get_int();
320    }
321  }
322}
323
324StackValueCollection*  interpretedVFrame::expressions() const {
325  int length = fr().interpreter_frame_expression_stack_size();
326  if (method()->is_native()) {
327    // If the method is native, there is no expression stack
328    length = 0;
329  }
330
331  int nof_locals = method()->max_locals();
332  StackValueCollection* result = new StackValueCollection(length);
333
334  if (TaggedStackInterpreter) {
335    // handle expressions
336    for(int i=0; i < length; i++) {
337      // Find stack location
338      intptr_t *addr = fr().interpreter_frame_expression_stack_at(i);
339      frame::Tag tag = fr().interpreter_frame_expression_stack_tag(i);
340
341      // Depending on oop/int put it in the right package
342      StackValue *sv;
343      if (tag == frame::TagReference) {
344        // oop value
345        Handle h(*(oop *)addr);
346        sv = new StackValue(h);
347      } else {
348        // otherwise
349        sv = new StackValue(*addr);
350      }
351      assert(sv != NULL, "sanity check");
352      result->add(sv);
353    }
354  } else {
355    InterpreterOopMap oop_mask;
356    // Get oopmap describing oops and int for current bci
357    if (TraceDeoptimization && Verbose) {
358      methodHandle m_h(method());
359      OopMapCache::compute_one_oop_map(m_h, bci(), &oop_mask);
360    } else {
361      method()->mask_for(bci(), &oop_mask);
362    }
363    // handle expressions
364    for(int i=0; i < length; i++) {
365      // Find stack location
366      intptr_t *addr = fr().interpreter_frame_expression_stack_at(i);
367
368      // Depending on oop/int put it in the right package
369      StackValue *sv;
370      if (oop_mask.is_oop(i + nof_locals)) {
371        // oop value
372        Handle h(*(oop *)addr);
373        sv = new StackValue(h);
374      } else {
375        // integer
376        sv = new StackValue(*addr);
377      }
378      assert(sv != NULL, "sanity check");
379      result->add(sv);
380    }
381  }
382  return result;
383}
384
385
386// ------------- cChunk --------------
387
388entryVFrame::entryVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread)
389: externalVFrame(fr, reg_map, thread) {}
390
391
392void vframeStreamCommon::found_bad_method_frame() {
393  // 6379830 Cut point for an assertion that occasionally fires when
394  // we are using the performance analyzer.
395  // Disable this assert when testing the analyzer with fastdebug.
396  // -XX:SuppressErrorAt=vframe.cpp:XXX (XXX=following line number)
397  assert(false, "invalid bci or invalid scope desc");
398}
399
400// top-frame will be skipped
401vframeStream::vframeStream(JavaThread* thread, frame top_frame,
402  bool stop_at_java_call_stub) : vframeStreamCommon(thread) {
403  _stop_at_java_call_stub = stop_at_java_call_stub;
404
405  // skip top frame, as it may not be at safepoint
406  _frame  = top_frame.sender(&_reg_map);
407  while (!fill_from_frame()) {
408    _frame = _frame.sender(&_reg_map);
409  }
410}
411
412
413// Step back n frames, skip any pseudo frames in between.
414// This function is used in Class.forName, Class.newInstance, Method.Invoke,
415// AccessController.doPrivileged.
416//
417// NOTE that in JDK 1.4 this has been exposed to Java as
418// sun.reflect.Reflection.getCallerClass(), which can be inlined.
419// Inlined versions must match this routine's logic.
420// Native method prefixing logic does not need to match since
421// the method names don't match and inlining will not occur.
422// See, for example,
423// Parse::inline_native_Reflection_getCallerClass in
424// opto/library_call.cpp.
425void vframeStreamCommon::security_get_caller_frame(int depth) {
426  bool use_new_reflection = JDK_Version::is_gte_jdk14x_version() && UseNewReflection;
427
428  while (!at_end()) {
429    if (Universe::reflect_invoke_cache()->is_same_method(method())) {
430      // This is Method.invoke() -- skip it
431    } else if (use_new_reflection &&
432              Klass::cast(method()->method_holder())
433                 ->is_subclass_of(SystemDictionary::reflect_method_accessor_klass())) {
434      // This is an auxilary frame -- skip it
435    } else {
436      // This is non-excluded frame, we need to count it against the depth
437      if (depth-- <= 0) {
438        // we have reached the desired depth, we are done
439        break;
440      }
441    }
442    if (method()->is_prefixed_native()) {
443      skip_prefixed_method_and_wrappers();
444    } else {
445      next();
446    }
447  }
448}
449
450
451void vframeStreamCommon::skip_prefixed_method_and_wrappers() {
452  ResourceMark rm;
453  HandleMark hm;
454
455  int    method_prefix_count = 0;
456  char** method_prefixes = JvmtiExport::get_all_native_method_prefixes(&method_prefix_count);
457  KlassHandle prefixed_klass(method()->method_holder());
458  const char* prefixed_name = method()->name()->as_C_string();
459  size_t prefixed_name_len = strlen(prefixed_name);
460  int prefix_index = method_prefix_count-1;
461
462  while (!at_end()) {
463    next();
464    if (method()->method_holder() != prefixed_klass()) {
465      break; // classes don't match, can't be a wrapper
466    }
467    const char* name = method()->name()->as_C_string();
468    size_t name_len = strlen(name);
469    size_t prefix_len = prefixed_name_len - name_len;
470    if (prefix_len <= 0 || strcmp(name, prefixed_name + prefix_len) != 0) {
471      break; // prefixed name isn't prefixed version of method name, can't be a wrapper
472    }
473    for (; prefix_index >= 0; --prefix_index) {
474      const char* possible_prefix = method_prefixes[prefix_index];
475      size_t possible_prefix_len = strlen(possible_prefix);
476      if (possible_prefix_len == prefix_len &&
477          strncmp(possible_prefix, prefixed_name, prefix_len) == 0) {
478        break; // matching prefix found
479      }
480    }
481    if (prefix_index < 0) {
482      break; // didn't find the prefix, can't be a wrapper
483    }
484    prefixed_name = name;
485    prefixed_name_len = name_len;
486  }
487}
488
489
490void vframeStreamCommon::skip_reflection_related_frames() {
491  while (!at_end() &&
492         (JDK_Version::is_gte_jdk14x_version() && UseNewReflection &&
493          (Klass::cast(method()->method_holder())->is_subclass_of(SystemDictionary::reflect_method_accessor_klass()) ||
494           Klass::cast(method()->method_holder())->is_subclass_of(SystemDictionary::reflect_constructor_accessor_klass())))) {
495    next();
496  }
497}
498
499
500#ifndef PRODUCT
501void vframe::print() {
502  if (WizardMode) _fr.print_value_on(tty,NULL);
503}
504
505
506void vframe::print_value() const {
507  ((vframe*)this)->print();
508}
509
510
511void entryVFrame::print_value() const {
512  ((entryVFrame*)this)->print();
513}
514
515void entryVFrame::print() {
516  vframe::print();
517  tty->print_cr("C Chunk inbetween Java");
518  tty->print_cr("C     link " INTPTR_FORMAT, _fr.link());
519}
520
521
522// ------------- javaVFrame --------------
523
524static void print_stack_values(const char* title, StackValueCollection* values) {
525  if (values->is_empty()) return;
526  tty->print_cr("\t%s:", title);
527  values->print();
528}
529
530
531void javaVFrame::print() {
532  ResourceMark rm;
533  vframe::print();
534  tty->print("\t");
535  method()->print_value();
536  tty->cr();
537  tty->print_cr("\tbci:    %d", bci());
538
539  print_stack_values("locals",      locals());
540  print_stack_values("expressions", expressions());
541
542  GrowableArray<MonitorInfo*>* list = monitors();
543  if (list->is_empty()) return;
544  tty->print_cr("\tmonitor list:");
545  for (int index = (list->length()-1); index >= 0; index--) {
546    MonitorInfo* monitor = list->at(index);
547    tty->print("\t  obj\t");
548    if (monitor->owner_is_scalar_replaced()) {
549      Klass* k = Klass::cast(monitor->owner_klass());
550      tty->print("( is scalar replaced %s)", k->external_name());
551    } else if (monitor->owner() == NULL) {
552      tty->print("( null )");
553    } else {
554      monitor->owner()->print_value();
555      tty->print("(" INTPTR_FORMAT ")", (address)monitor->owner());
556    }
557    if (monitor->eliminated() && is_compiled_frame())
558      tty->print(" ( lock is eliminated )");
559    tty->cr();
560    tty->print("\t  ");
561    monitor->lock()->print_on(tty);
562    tty->cr();
563  }
564}
565
566
567void javaVFrame::print_value() const {
568  methodOop  m = method();
569  klassOop   k = m->method_holder();
570  tty->print_cr("frame( sp=" INTPTR_FORMAT ", unextended_sp=" INTPTR_FORMAT ", fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT ")",
571                _fr.sp(),  _fr.unextended_sp(), _fr.fp(), _fr.pc());
572  tty->print("%s.%s", Klass::cast(k)->internal_name(), m->name()->as_C_string());
573
574  if (!m->is_native()) {
575    symbolOop  source_name = instanceKlass::cast(k)->source_file_name();
576    int        line_number = m->line_number_from_bci(bci());
577    if (source_name != NULL && (line_number != -1)) {
578      tty->print("(%s:%d)", source_name->as_C_string(), line_number);
579    }
580  } else {
581    tty->print("(Native Method)");
582  }
583  // Check frame size and print warning if it looks suspiciously large
584  if (fr().sp() != NULL) {
585    RegisterMap map = *register_map();
586    uint size = fr().frame_size(&map);
587#ifdef _LP64
588    if (size > 8*K) warning("SUSPICIOUSLY LARGE FRAME (%d)", size);
589#else
590    if (size > 4*K) warning("SUSPICIOUSLY LARGE FRAME (%d)", size);
591#endif
592  }
593}
594
595
596bool javaVFrame::structural_compare(javaVFrame* other) {
597  // Check static part
598  if (method() != other->method()) return false;
599  if (bci()    != other->bci())    return false;
600
601  // Check locals
602  StackValueCollection *locs = locals();
603  StackValueCollection *other_locs = other->locals();
604  assert(locs->size() == other_locs->size(), "sanity check");
605  int i;
606  for(i = 0; i < locs->size(); i++) {
607    // it might happen the compiler reports a conflict and
608    // the interpreter reports a bogus int.
609    if (       is_compiled_frame() &&       locs->at(i)->type() == T_CONFLICT) continue;
610    if (other->is_compiled_frame() && other_locs->at(i)->type() == T_CONFLICT) continue;
611
612    if (!locs->at(i)->equal(other_locs->at(i)))
613      return false;
614  }
615
616  // Check expressions
617  StackValueCollection* exprs = expressions();
618  StackValueCollection* other_exprs = other->expressions();
619  assert(exprs->size() == other_exprs->size(), "sanity check");
620  for(i = 0; i < exprs->size(); i++) {
621    if (!exprs->at(i)->equal(other_exprs->at(i)))
622      return false;
623  }
624
625  return true;
626}
627
628
629void javaVFrame::print_activation(int index) const {
630  // frame number and method
631  tty->print("%2d - ", index);
632  ((vframe*)this)->print_value();
633  tty->cr();
634
635  if (WizardMode) {
636    ((vframe*)this)->print();
637    tty->cr();
638  }
639}
640
641
642void javaVFrame::verify() const {
643}
644
645
646void interpretedVFrame::verify() const {
647}
648
649
650// ------------- externalVFrame --------------
651
652void externalVFrame::print() {
653  _fr.print_value_on(tty,NULL);
654}
655
656
657void externalVFrame::print_value() const {
658  ((vframe*)this)->print();
659}
660#endif // PRODUCT
661