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