1 /*
   2  * Copyright (c) 1997, 2016, 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/systemDictionary.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/icBuffer.hpp"
  29 #include "code/nmethod.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "gc/shared/collectedHeap.hpp"
  34 #include "interpreter/bytecodeHistogram.hpp"
  35 #include "interpreter/interpreter.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "memory/universe.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "prims/privilegedStack.hpp"
  40 #include "runtime/arguments.hpp"
  41 #include "runtime/atomic.inline.hpp"
  42 #include "runtime/frame.hpp"
  43 #include "runtime/java.hpp"
  44 #include "runtime/os.hpp"
  45 #include "runtime/sharedRuntime.hpp"
  46 #include "runtime/stubCodeGenerator.hpp"
  47 #include "runtime/stubRoutines.hpp"
  48 #include "runtime/thread.inline.hpp"
  49 #include "runtime/vframe.hpp"
  50 #include "runtime/vm_version.hpp"
  51 #include "services/heapDumper.hpp"
  52 #include "utilities/defaultStream.hpp"
  53 #include "utilities/events.hpp"
  54 #include "utilities/macros.hpp"
  55 #include "utilities/vmError.hpp"
  56 
  57 #if INCLUDE_TRACE
  58 #include "trace/tracing.hpp"
  59 #endif
  60 
  61 #ifndef ASSERT
  62 #  ifdef _DEBUG
  63    // NOTE: don't turn the lines below into a comment -- if you're getting
  64    // a compile error here, change the settings to define ASSERT
  65    ASSERT should be defined when _DEBUG is defined.  It is not intended to be used for debugging
  66    functions that do not slow down the system too much and thus can be left in optimized code.
  67    On the other hand, the code should not be included in a production version.
  68 #  endif // _DEBUG
  69 #endif // ASSERT
  70 
  71 
  72 #ifdef _DEBUG
  73 #  ifndef ASSERT
  74      configuration error: ASSERT must be defined in debug version
  75 #  endif // ASSERT
  76 #endif // _DEBUG
  77 
  78 
  79 #ifdef PRODUCT
  80 #  if -defined _DEBUG || -defined ASSERT
  81      configuration error: ASSERT et al. must not be defined in PRODUCT version
  82 #  endif
  83 #endif // PRODUCT
  84 
  85 FormatBufferResource::FormatBufferResource(const char * format, ...)
  86   : FormatBufferBase((char*)resource_allocate_bytes(FormatBufferBase::BufferSize)) {
  87   va_list argp;
  88   va_start(argp, format);
  89   jio_vsnprintf(_buf, FormatBufferBase::BufferSize, format, argp);
  90   va_end(argp);
  91 }
  92 
  93 ATTRIBUTE_PRINTF(1, 2)
  94 void warning(const char* format, ...) {
  95   if (PrintWarnings) {
  96     FILE* const err = defaultStream::error_stream();
  97     jio_fprintf(err, "%s warning: ", VM_Version::vm_name());
  98     va_list ap;
  99     va_start(ap, format);
 100     vfprintf(err, format, ap);
 101     va_end(ap);
 102     fputc('\n', err);
 103   }
 104   if (BreakAtWarning) BREAKPOINT;
 105 }
 106 
 107 #ifndef PRODUCT
 108 
 109 #define is_token_break(ch) (isspace(ch) || (ch) == ',')
 110 
 111 static const char* last_file_name = NULL;
 112 static int         last_line_no   = -1;
 113 
 114 // assert/guarantee/... may happen very early during VM initialization.
 115 // Don't rely on anything that is initialized by Threads::create_vm(). For
 116 // example, don't use tty.
 117 bool error_is_suppressed(const char* file_name, int line_no) {
 118   // The following 1-element cache requires that passed-in
 119   // file names are always only constant literals.
 120   if (file_name == last_file_name && line_no == last_line_no)  return true;
 121 
 122   int file_name_len = (int)strlen(file_name);
 123   char separator = os::file_separator()[0];
 124   const char* base_name = strrchr(file_name, separator);
 125   if (base_name == NULL)
 126     base_name = file_name;
 127 
 128   // scan the SuppressErrorAt option
 129   const char* cp = SuppressErrorAt;
 130   for (;;) {
 131     const char* sfile;
 132     int sfile_len;
 133     int sline;
 134     bool noisy;
 135     while ((*cp) != '\0' && is_token_break(*cp))  cp++;
 136     if ((*cp) == '\0')  break;
 137     sfile = cp;
 138     while ((*cp) != '\0' && !is_token_break(*cp) && (*cp) != ':')  cp++;
 139     sfile_len = cp - sfile;
 140     if ((*cp) == ':')  cp++;
 141     sline = 0;
 142     while ((*cp) != '\0' && isdigit(*cp)) {
 143       sline *= 10;
 144       sline += (*cp) - '0';
 145       cp++;
 146     }
 147     // "file:line!" means the assert suppression is not silent
 148     noisy = ((*cp) == '!');
 149     while ((*cp) != '\0' && !is_token_break(*cp))  cp++;
 150     // match the line
 151     if (sline != 0) {
 152       if (sline != line_no)  continue;
 153     }
 154     // match the file
 155     if (sfile_len > 0) {
 156       const char* look = file_name;
 157       const char* look_max = file_name + file_name_len - sfile_len;
 158       const char* foundp;
 159       bool match = false;
 160       while (!match
 161              && (foundp = strchr(look, sfile[0])) != NULL
 162              && foundp <= look_max) {
 163         match = true;
 164         for (int i = 1; i < sfile_len; i++) {
 165           if (sfile[i] != foundp[i]) {
 166             match = false;
 167             break;
 168           }
 169         }
 170         look = foundp + 1;
 171       }
 172       if (!match)  continue;
 173     }
 174     // got a match!
 175     if (noisy) {
 176       fdStream out(defaultStream::output_fd());
 177       out.print_raw("[error suppressed at ");
 178       out.print_raw(base_name);
 179       char buf[16];
 180       jio_snprintf(buf, sizeof(buf), ":%d]", line_no);
 181       out.print_raw_cr(buf);
 182     } else {
 183       // update 1-element cache for fast silent matches
 184       last_file_name = file_name;
 185       last_line_no   = line_no;
 186     }
 187     return true;
 188   }
 189 
 190   if (!is_error_reported()) {
 191     // print a friendly hint:
 192     fdStream out(defaultStream::output_fd());
 193     out.print_raw_cr("# To suppress the following error report, specify this argument");
 194     out.print_raw   ("# after -XX: or in .hotspotrc:  SuppressErrorAt=");
 195     out.print_raw   (base_name);
 196     char buf[16];
 197     jio_snprintf(buf, sizeof(buf), ":%d", line_no);
 198     out.print_raw_cr(buf);
 199   }
 200   return false;
 201 }
 202 
 203 #undef is_token_break
 204 
 205 #else
 206 
 207 // Place-holder for non-existent suppression check:
 208 #define error_is_suppressed(file_name, line_no) (false)
 209 
 210 #endif // !PRODUCT
 211 
 212 void report_vm_error(const char* file, int line, const char* error_msg)
 213 {
 214   report_vm_error(file, line, error_msg, "%s", "");
 215 }
 216 
 217 void report_vm_error(const char* file, int line, const char* error_msg, const char* detail_fmt, ...)
 218 {
 219   if (Debugging || error_is_suppressed(file, line)) return;
 220   va_list detail_args;
 221   va_start(detail_args, detail_fmt);
 222   VMError::report_and_die(Thread::current_or_null(), file, line, error_msg, detail_fmt, detail_args);
 223   va_end(detail_args);
 224 }
 225 
 226 void report_vm_status_error(const char* file, int line, const char* error_msg,
 227                             int status, const char* detail) {
 228   report_vm_error(file, line, error_msg, "error %s(%d), %s", os::errno_name(status), status, detail);
 229 }
 230 
 231 void report_fatal(const char* file, int line, const char* detail_fmt, ...)
 232 {
 233   if (Debugging || error_is_suppressed(file, line)) return;
 234   va_list detail_args;
 235   va_start(detail_args, detail_fmt);
 236   VMError::report_and_die(Thread::current_or_null(), file, line, "fatal error", detail_fmt, detail_args);
 237   va_end(detail_args);
 238 }
 239 
 240 void report_vm_out_of_memory(const char* file, int line, size_t size,
 241                              VMErrorType vm_err_type, const char* detail_fmt, ...) {
 242   if (Debugging) return;
 243   va_list detail_args;
 244   va_start(detail_args, detail_fmt);
 245   VMError::report_and_die(Thread::current_or_null(), file, line, size, vm_err_type, detail_fmt, detail_args);
 246   va_end(detail_args);
 247 
 248   // The UseOSErrorReporting option in report_and_die() may allow a return
 249   // to here. If so then we'll have to figure out how to handle it.
 250   guarantee(false, "report_and_die() should not return here");
 251 }
 252 
 253 void report_should_not_call(const char* file, int line) {
 254   report_vm_error(file, line, "ShouldNotCall()");
 255 }
 256 
 257 void report_should_not_reach_here(const char* file, int line) {
 258   report_vm_error(file, line, "ShouldNotReachHere()");
 259 }
 260 
 261 void report_unimplemented(const char* file, int line) {
 262   report_vm_error(file, line, "Unimplemented()");
 263 }
 264 
 265 void report_untested(const char* file, int line, const char* message) {
 266 #ifndef PRODUCT
 267   warning("Untested: %s in %s: %d\n", message, file, line);
 268 #endif // !PRODUCT
 269 }
 270 
 271 void report_out_of_shared_space(SharedSpaceType shared_space) {
 272   static const char* name[] = {
 273     "shared read only space",
 274     "shared read write space",
 275     "shared miscellaneous data space",
 276     "shared miscellaneous code space"
 277   };
 278   static const char* flag[] = {
 279     "SharedReadOnlySize",
 280     "SharedReadWriteSize",
 281     "SharedMiscDataSize",
 282     "SharedMiscCodeSize"
 283   };
 284 
 285    warning("\nThe %s is not large enough\n"
 286            "to preload requested classes. Use -XX:%s=<size>\n"
 287            "to increase the initial size of %s.\n",
 288            name[shared_space], flag[shared_space], name[shared_space]);
 289    exit(2);
 290 }
 291 
 292 static void notify_tracing() {
 293 #if INCLUDE_TRACE
 294   Tracing::on_vm_error(true);
 295 #endif
 296 }
 297 
 298 void report_insufficient_metaspace(size_t required_size) {
 299   warning("\nThe MaxMetaspaceSize of " SIZE_FORMAT " bytes is not large enough.\n"
 300           "Either don't specify the -XX:MaxMetaspaceSize=<size>\n"
 301           "or increase the size to at least " SIZE_FORMAT ".\n",
 302           MaxMetaspaceSize, required_size);
 303   exit(2);
 304 }
 305 
 306 void report_java_out_of_memory(const char* message) {
 307   static jint out_of_memory_reported = 0;
 308 
 309   // A number of threads may attempt to report OutOfMemoryError at around the
 310   // same time. To avoid dumping the heap or executing the data collection
 311   // commands multiple times we just do it once when the first threads reports
 312   // the error.
 313   if (Atomic::cmpxchg(1, &out_of_memory_reported, 0) == 0) {
 314     // create heap dump before OnOutOfMemoryError commands are executed
 315     if (HeapDumpOnOutOfMemoryError) {
 316       tty->print_cr("java.lang.OutOfMemoryError: %s", message);
 317       HeapDumper::dump_heap_from_oome();
 318     }
 319 
 320     notify_tracing();
 321 
 322     if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
 323       VMError::report_java_out_of_memory(message);
 324     }
 325 
 326     if (CrashOnOutOfMemoryError) {
 327       tty->print_cr("Aborting due to java.lang.OutOfMemoryError: %s", message);
 328       fatal("OutOfMemory encountered: %s", message);
 329     }
 330 
 331     if (ExitOnOutOfMemoryError) {
 332       tty->print_cr("Terminating due to java.lang.OutOfMemoryError: %s", message);
 333       os::exit(3);
 334     }
 335   }
 336 }
 337 
 338 static bool error_reported = false;
 339 
 340 // call this when the VM is dying--it might loosen some asserts
 341 void set_error_reported() {
 342   error_reported = true;
 343 }
 344 
 345 bool is_error_reported() {
 346     return error_reported;
 347 }
 348 
 349 #ifndef PRODUCT
 350 #include <signal.h>
 351 
 352 typedef void (*voidfun_t)();
 353 // Crash with an authentic sigfpe
 354 static void crash_with_sigfpe() {
 355   // generate a native synchronous SIGFPE where possible;
 356   // if that did not cause a signal (e.g. on ppc), just
 357   // raise the signal.
 358   volatile int x = 0;
 359   volatile int y = 1/x;
 360 #ifndef _WIN32
 361   // OSX implements raise(sig) incorrectly so we need to
 362   // explicitly target the current thread
 363   pthread_kill(pthread_self(), SIGFPE);
 364 #endif
 365 } // end: crash_with_sigfpe
 366 
 367 // crash with sigsegv at non-null address.
 368 static void crash_with_segfault() {
 369 
 370   char* const crash_addr = (char*) get_segfault_address();
 371   *crash_addr = 'X';
 372 
 373 } // end: crash_with_segfault
 374 
 375 // returns an address which is guaranteed to generate a SIGSEGV on read,
 376 // for test purposes, which is not NULL and contains bits in every word
 377 void* get_segfault_address() {
 378   return (void*)
 379 #ifdef _LP64
 380     0xABC0000000000ABCULL;
 381 #else
 382     0x00000ABC;
 383 #endif
 384 }
 385 
 386 void test_error_handler() {
 387   controlled_crash(ErrorHandlerTest);
 388 }
 389 
 390 void controlled_crash(int how) {
 391   if (how == 0) return;
 392 
 393   // If asserts are disabled, use the corresponding guarantee instead.
 394   NOT_DEBUG(if (how <= 2) how += 2);
 395 
 396   const char* const str = "hello";
 397   const size_t      num = (size_t)os::vm_page_size();
 398 
 399   const char* const eol = os::line_separator();
 400   const char* const msg = "this message should be truncated during formatting";
 401   char * const dataPtr = NULL;  // bad data pointer
 402   const void (*funcPtr)(void) = (const void(*)()) 0xF;  // bad function pointer
 403 
 404   // Keep this in sync with test/runtime/ErrorHandling/ErrorHandler.java
 405   switch (how) {
 406     case  1: vmassert(str == NULL, "expected null");
 407     case  2: vmassert(num == 1023 && *str == 'X',
 408                       "num=" SIZE_FORMAT " str=\"%s\"", num, str);
 409     case  3: guarantee(str == NULL, "expected null");
 410     case  4: guarantee(num == 1023 && *str == 'X',
 411                        "num=" SIZE_FORMAT " str=\"%s\"", num, str);
 412     case  5: fatal("expected null");
 413     case  6: fatal("num=" SIZE_FORMAT " str=\"%s\"", num, str);
 414     case  7: fatal("%s%s#    %s%s#    %s%s#    %s%s#    %s%s#    "
 415                    "%s%s#    %s%s#    %s%s#    %s%s#    %s%s#    "
 416                    "%s%s#    %s%s#    %s%s#    %s%s#    %s",
 417                    msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
 418                    msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
 419                    msg, eol, msg, eol, msg, eol, msg, eol, msg);
 420     case  8: vm_exit_out_of_memory(num, OOM_MALLOC_ERROR, "ChunkPool::allocate");
 421     case  9: ShouldNotCallThis();
 422     case 10: ShouldNotReachHere();
 423     case 11: Unimplemented();
 424     // There's no guarantee the bad data pointer will crash us
 425     // so "break" out to the ShouldNotReachHere().
 426     case 12: *dataPtr = '\0'; break;
 427     // There's no guarantee the bad function pointer will crash us
 428     // so "break" out to the ShouldNotReachHere().
 429     case 13: (*funcPtr)(); break;
 430     case 14: crash_with_segfault(); break;
 431     case 15: crash_with_sigfpe(); break;
 432 
 433     default: tty->print_cr("ERROR: %d: unexpected test_num value.", how);
 434   }
 435   ShouldNotReachHere();
 436 }
 437 #endif // !PRODUCT
 438 
 439 // ------ helper functions for debugging go here ------------
 440 
 441 // All debug entries should be wrapped with a stack allocated
 442 // Command object. It makes sure a resource mark is set and
 443 // flushes the logfile to prevent file sharing problems.
 444 
 445 class Command : public StackObj {
 446  private:
 447   ResourceMark rm;
 448   ResetNoHandleMark rnhm;
 449   HandleMark   hm;
 450   bool debug_save;
 451  public:
 452   static int level;
 453   Command(const char* str) {
 454     debug_save = Debugging;
 455     Debugging = true;
 456     if (level++ > 0)  return;
 457     tty->cr();
 458     tty->print_cr("\"Executing %s\"", str);
 459   }
 460 
 461   ~Command() {
 462         tty->flush();
 463         Debugging = debug_save;
 464         level--;
 465   }
 466 };
 467 
 468 int Command::level = 0;
 469 
 470 #ifndef PRODUCT
 471 
 472 extern "C" void blob(CodeBlob* cb) {
 473   Command c("blob");
 474   cb->print();
 475 }
 476 
 477 
 478 extern "C" void dump_vtable(address p) {
 479   Command c("dump_vtable");
 480   Klass* k = (Klass*)p;
 481   k->vtable()->print();
 482 }
 483 
 484 
 485 extern "C" void nm(intptr_t p) {
 486   // Actually we look through all CodeBlobs (the nm name has been kept for backwards compatability)
 487   Command c("nm");
 488   CodeBlob* cb = CodeCache::find_blob((address)p);
 489   if (cb == NULL) {
 490     tty->print_cr("NULL");
 491   } else {
 492     cb->print();
 493   }
 494 }
 495 
 496 
 497 extern "C" void disnm(intptr_t p) {
 498   Command c("disnm");
 499   CodeBlob* cb = CodeCache::find_blob((address) p);
 500   if (cb != NULL) {
 501     nmethod* nm = cb->as_nmethod_or_null();
 502     if (nm != NULL) {
 503       nm->print();
 504     } else {
 505       cb->print();
 506     }
 507     Disassembler::decode(cb);
 508   }
 509 }
 510 
 511 
 512 extern "C" void printnm(intptr_t p) {
 513   char buffer[256];
 514   sprintf(buffer, "printnm: " INTPTR_FORMAT, p);
 515   Command c(buffer);
 516   CodeBlob* cb = CodeCache::find_blob((address) p);
 517   if (cb->is_nmethod()) {
 518     nmethod* nm = (nmethod*)cb;
 519     nm->print_nmethod(true);
 520   }
 521 }
 522 
 523 
 524 extern "C" void universe() {
 525   Command c("universe");
 526   Universe::print_on(tty);
 527 }
 528 
 529 
 530 extern "C" void verify() {
 531   // try to run a verify on the entire system
 532   // note: this may not be safe if we're not at a safepoint; for debugging,
 533   // this manipulates the safepoint settings to avoid assertion failures
 534   Command c("universe verify");
 535   bool safe = SafepointSynchronize::is_at_safepoint();
 536   if (!safe) {
 537     tty->print_cr("warning: not at safepoint -- verify may fail");
 538     SafepointSynchronize::set_is_at_safepoint();
 539   }
 540   // Ensure Eden top is correct before verification
 541   Universe::heap()->prepare_for_verify();
 542   Universe::verify();
 543   if (!safe) SafepointSynchronize::set_is_not_at_safepoint();
 544 }
 545 
 546 
 547 extern "C" void pp(void* p) {
 548   Command c("pp");
 549   FlagSetting fl(PrintVMMessages, true);
 550   FlagSetting f2(DisplayVMOutput, true);
 551   if (Universe::heap()->is_in(p)) {
 552     oop obj = oop(p);
 553     obj->print();
 554   } else {
 555     tty->print(PTR_FORMAT, p2i(p));
 556   }
 557 }
 558 
 559 
 560 // pv: print vm-printable object
 561 extern "C" void pa(intptr_t p)   { ((AllocatedObj*) p)->print(); }
 562 extern "C" void findpc(intptr_t x);
 563 
 564 #endif // !PRODUCT
 565 
 566 extern "C" void ps() { // print stack
 567   if (Thread::current_or_null() == NULL) return;
 568   Command c("ps");
 569 
 570 
 571   // Prints the stack of the current Java thread
 572   JavaThread* p = JavaThread::active();
 573   tty->print(" for thread: ");
 574   p->print();
 575   tty->cr();
 576 
 577   if (p->has_last_Java_frame()) {
 578     // If the last_Java_fp is set we are in C land and
 579     // can call the standard stack_trace function.
 580 #ifdef PRODUCT
 581     p->print_stack();
 582   } else {
 583     tty->print_cr("Cannot find the last Java frame, printing stack disabled.");
 584 #else // !PRODUCT
 585     p->trace_stack();
 586   } else {
 587     frame f = os::current_frame();
 588     RegisterMap reg_map(p);
 589     f = f.sender(&reg_map);
 590     tty->print("(guessing starting frame id=" PTR_FORMAT " based on current fp)\n", p2i(f.id()));
 591     p->trace_stack_from(vframe::new_vframe(&f, &reg_map, p));
 592   pd_ps(f);
 593 #endif // PRODUCT
 594   }
 595 
 596 }
 597 
 598 extern "C" void pfl() {
 599   // print frame layout
 600   Command c("pfl");
 601   JavaThread* p = JavaThread::active();
 602   tty->print(" for thread: ");
 603   p->print();
 604   tty->cr();
 605   if (p->has_last_Java_frame()) {
 606     p->print_frame_layout();
 607   }
 608 }
 609 
 610 #ifndef PRODUCT
 611 
 612 extern "C" void psf() { // print stack frames
 613   {
 614     Command c("psf");
 615     JavaThread* p = JavaThread::active();
 616     tty->print(" for thread: ");
 617     p->print();
 618     tty->cr();
 619     if (p->has_last_Java_frame()) {
 620       p->trace_frames();
 621     }
 622   }
 623 }
 624 
 625 
 626 extern "C" void threads() {
 627   Command c("threads");
 628   Threads::print(false, true);
 629 }
 630 
 631 
 632 extern "C" void psd() {
 633   Command c("psd");
 634   SystemDictionary::print();
 635 }
 636 
 637 
 638 extern "C" void safepoints() {
 639   Command c("safepoints");
 640   SafepointSynchronize::print_state();
 641 }
 642 
 643 #endif // !PRODUCT
 644 
 645 extern "C" void pss() { // print all stacks
 646   if (Thread::current_or_null() == NULL) return;
 647   Command c("pss");
 648   Threads::print(true, PRODUCT_ONLY(false) NOT_PRODUCT(true));
 649 }
 650 
 651 #ifndef PRODUCT
 652 
 653 extern "C" void debug() {               // to set things up for compiler debugging
 654   Command c("debug");
 655   WizardMode = true;
 656   PrintVMMessages = PrintCompilation = true;
 657   PrintInlining = PrintAssembly = true;
 658   tty->flush();
 659 }
 660 
 661 
 662 extern "C" void ndebug() {              // undo debug()
 663   Command c("ndebug");
 664   PrintCompilation = false;
 665   PrintInlining = PrintAssembly = false;
 666   tty->flush();
 667 }
 668 
 669 
 670 extern "C" void flush()  {
 671   Command c("flush");
 672   tty->flush();
 673 }
 674 
 675 extern "C" void events() {
 676   Command c("events");
 677   Events::print();
 678 }
 679 
 680 extern "C" Method* findm(intptr_t pc) {
 681   Command c("findm");
 682   nmethod* nm = CodeCache::find_nmethod((address)pc);
 683   return (nm == NULL) ? (Method*)NULL : nm->method();
 684 }
 685 
 686 
 687 extern "C" nmethod* findnm(intptr_t addr) {
 688   Command c("findnm");
 689   return  CodeCache::find_nmethod((address)addr);
 690 }
 691 
 692 // Another interface that isn't ambiguous in dbx.
 693 // Can we someday rename the other find to hsfind?
 694 extern "C" void hsfind(intptr_t x) {
 695   Command c("hsfind");
 696   os::print_location(tty, x, false);
 697 }
 698 
 699 
 700 extern "C" void find(intptr_t x) {
 701   Command c("find");
 702   os::print_location(tty, x, false);
 703 }
 704 
 705 
 706 extern "C" void findpc(intptr_t x) {
 707   Command c("findpc");
 708   os::print_location(tty, x, true);
 709 }
 710 
 711 
 712 // Need method pointer to find bcp, when not in permgen.
 713 extern "C" void findbcp(intptr_t method, intptr_t bcp) {
 714   Command c("findbcp");
 715   Method* mh = (Method*)method;
 716   if (!mh->is_native()) {
 717     tty->print_cr("bci_from(%p) = %d; print_codes():",
 718                         mh, mh->bci_from(address(bcp)));
 719     mh->print_codes_on(tty);
 720   }
 721 }
 722 
 723 // int versions of all methods to avoid having to type type casts in the debugger
 724 
 725 void pp(intptr_t p)          { pp((void*)p); }
 726 void pp(oop p)               { pp((void*)p); }
 727 
 728 void help() {
 729   Command c("help");
 730   tty->print_cr("basic");
 731   tty->print_cr("  pp(void* p)   - try to make sense of p");
 732   tty->print_cr("  pv(intptr_t p)- ((PrintableResourceObj*) p)->print()");
 733   tty->print_cr("  ps()          - print current thread stack");
 734   tty->print_cr("  pss()         - print all thread stacks");
 735   tty->print_cr("  pm(int pc)    - print Method* given compiled PC");
 736   tty->print_cr("  findm(intptr_t pc) - finds Method*");
 737   tty->print_cr("  find(intptr_t x)   - finds & prints nmethod/stub/bytecode/oop based on pointer into it");
 738   tty->print_cr("  pns(void* sp, void* fp, void* pc)  - print native (i.e. mixed) stack trace. E.g.");
 739   tty->print_cr("                   pns($sp, $rbp, $pc) on Linux/amd64 and Solaris/amd64 or");
 740   tty->print_cr("                   pns($sp, $ebp, $pc) on Linux/x86 or");
 741   tty->print_cr("                   pns($sp, 0, $pc)    on Linux/ppc64 or");
 742   tty->print_cr("                   pns($sp + 0x7ff, 0, $pc) on Solaris/SPARC");
 743   tty->print_cr("                 - in gdb do 'set overload-resolution off' before calling pns()");
 744   tty->print_cr("                 - in dbx do 'frame 1' before calling pns()");
 745 
 746   tty->print_cr("misc.");
 747   tty->print_cr("  flush()       - flushes the log file");
 748   tty->print_cr("  events()      - dump events from ring buffers");
 749 
 750 
 751   tty->print_cr("compiler debugging");
 752   tty->print_cr("  debug()       - to set things up for compiler debugging");
 753   tty->print_cr("  ndebug()      - undo debug");
 754 }
 755 
 756 #endif // !PRODUCT
 757 
 758 void print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size) {
 759 
 760   // see if it's a valid frame
 761   if (fr.pc()) {
 762     st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)");
 763 
 764     int count = 0;
 765     while (count++ < StackPrintLimit) {
 766       fr.print_on_error(st, buf, buf_size);
 767       st->cr();
 768       // Compiled code may use EBP register on x86 so it looks like
 769       // non-walkable C frame. Use frame.sender() for java frames.
 770       if (t && t->is_Java_thread()) {
 771         // Catch very first native frame by using stack address.
 772         // For JavaThread stack_base and stack_size should be set.
 773         if (!t->on_local_stack((address)(fr.real_fp() + 1))) {
 774           break;
 775         }
 776         if (fr.is_java_frame() || fr.is_native_frame() || fr.is_runtime_frame()) {
 777           RegisterMap map((JavaThread*)t, false); // No update
 778           fr = fr.sender(&map);
 779         } else {
 780           fr = os::get_sender_for_C_frame(&fr);
 781         }
 782       } else {
 783         // is_first_C_frame() does only simple checks for frame pointer,
 784         // it will pass if java compiled code has a pointer in EBP.
 785         if (os::is_first_C_frame(&fr)) break;
 786         fr = os::get_sender_for_C_frame(&fr);
 787       }
 788     }
 789 
 790     if (count > StackPrintLimit) {
 791       st->print_cr("...<more frames>...");
 792     }
 793 
 794     st->cr();
 795   }
 796 }
 797 
 798 #ifndef PRODUCT
 799 
 800 extern "C" void pns(void* sp, void* fp, void* pc) { // print native stack
 801   Command c("pns");
 802   static char buf[O_BUFLEN];
 803   Thread* t = Thread::current_or_null();
 804   // Call generic frame constructor (certain arguments may be ignored)
 805   frame fr(sp, fp, pc);
 806   print_native_stack(tty, fr, t, buf, sizeof(buf));
 807 }
 808 
 809 #endif // !PRODUCT
 810 
 811 //////////////////////////////////////////////////////////////////////////////
 812 // Test multiple STATIC_ASSERT forms in various scopes.
 813 
 814 #ifndef PRODUCT
 815 
 816 // namespace scope
 817 STATIC_ASSERT(true);
 818 STATIC_ASSERT(true);
 819 STATIC_ASSERT(1 == 1);
 820 STATIC_ASSERT(0 == 0);
 821 
 822 void test_multiple_static_assert_forms_in_function_scope() {
 823   STATIC_ASSERT(true);
 824   STATIC_ASSERT(true);
 825   STATIC_ASSERT(0 == 0);
 826   STATIC_ASSERT(1 == 1);
 827 }
 828 
 829 // class scope
 830 struct TestMultipleStaticAssertFormsInClassScope {
 831   STATIC_ASSERT(true);
 832   STATIC_ASSERT(true);
 833   STATIC_ASSERT(0 == 0);
 834   STATIC_ASSERT(1 == 1);
 835 };
 836 
 837 #endif // !PRODUCT