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