debug.cpp 24.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include "precompiled.hpp"
#include "classfile/systemDictionary.hpp"
#include "code/codeCache.hpp"
#include "code/icBuffer.hpp"
#include "code/nmethod.hpp"
#include "code/vtableStubs.hpp"
#include "compiler/compileBroker.hpp"
#include "compiler/disassembler.hpp"
#include "gc_implementation/shared/markSweep.hpp"
#include "gc_interface/collectedHeap.hpp"
#include "interpreter/bytecodeHistogram.hpp"
#include "interpreter/interpreter.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/oop.inline.hpp"
#include "prims/privilegedStack.hpp"
#include "runtime/arguments.hpp"
#include "runtime/frame.hpp"
#include "runtime/java.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/stubCodeGenerator.hpp"
#include "runtime/stubRoutines.hpp"
#include "runtime/vframe.hpp"
#include "services/heapDumper.hpp"
#include "utilities/defaultStream.hpp"
#include "utilities/events.hpp"
#include "utilities/top.hpp"
#include "utilities/vmError.hpp"
#ifdef TARGET_OS_FAMILY_linux
# include "os_linux.inline.hpp"
# include "thread_linux.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_solaris
# include "os_solaris.inline.hpp"
# include "thread_solaris.inline.hpp"
#endif
#ifdef TARGET_OS_FAMILY_windows
# include "os_windows.inline.hpp"
# include "thread_windows.inline.hpp"
#endif
N
never 已提交
65 66 67 68
#ifdef TARGET_OS_FAMILY_bsd
# include "os_bsd.inline.hpp"
# include "thread_bsd.inline.hpp"
#endif
D
duke 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

#ifndef ASSERT
#  ifdef _DEBUG
   // NOTE: don't turn the lines below into a comment -- if you're getting
   // a compile error here, change the settings to define ASSERT
   ASSERT should be defined when _DEBUG is defined.  It is not intended to be used for debugging
   functions that do not slow down the system too much and thus can be left in optimized code.
   On the other hand, the code should not be included in a production version.
#  endif // _DEBUG
#endif // ASSERT


#ifdef _DEBUG
#  ifndef ASSERT
     configuration error: ASSERT must be defined in debug version
#  endif // ASSERT
#endif // _DEBUG


#ifdef PRODUCT
#  if -defined _DEBUG || -defined ASSERT
     configuration error: ASSERT et al. must not be defined in PRODUCT version
#  endif
#endif // PRODUCT

94 95 96 97 98 99 100
FormatBufferResource::FormatBufferResource(const char * format, ...)
  : FormatBufferBase((char*)resource_allocate_bytes(RES_BUFSZ)) {
  va_list argp;
  va_start(argp, format);
  jio_vsnprintf(_buf, RES_BUFSZ, format, argp);
  va_end(argp);
}
D
duke 已提交
101 102

void warning(const char* format, ...) {
103
  if (PrintWarnings) {
104 105
    FILE* const err = defaultStream::error_stream();
    jio_fprintf(err, "%s warning: ", VM_Version::vm_name());
106 107
    va_list ap;
    va_start(ap, format);
108
    vfprintf(err, format, ap);
109
    va_end(ap);
110
    fputc('\n', err);
111
  }
D
duke 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124
  if (BreakAtWarning) BREAKPOINT;
}

#ifndef PRODUCT

#define is_token_break(ch) (isspace(ch) || (ch) == ',')

static const char* last_file_name = NULL;
static int         last_line_no   = -1;

// assert/guarantee/... may happen very early during VM initialization.
// Don't rely on anything that is initialized by Threads::create_vm(). For
// example, don't use tty.
125
bool error_is_suppressed(const char* file_name, int line_no) {
D
duke 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  // The following 1-element cache requires that passed-in
  // file names are always only constant literals.
  if (file_name == last_file_name && line_no == last_line_no)  return true;

  int file_name_len = (int)strlen(file_name);
  char separator = os::file_separator()[0];
  const char* base_name = strrchr(file_name, separator);
  if (base_name == NULL)
    base_name = file_name;

  // scan the SuppressErrorAt option
  const char* cp = SuppressErrorAt;
  for (;;) {
    const char* sfile;
    int sfile_len;
    int sline;
    bool noisy;
    while ((*cp) != '\0' && is_token_break(*cp))  cp++;
    if ((*cp) == '\0')  break;
    sfile = cp;
    while ((*cp) != '\0' && !is_token_break(*cp) && (*cp) != ':')  cp++;
    sfile_len = cp - sfile;
    if ((*cp) == ':')  cp++;
    sline = 0;
    while ((*cp) != '\0' && isdigit(*cp)) {
      sline *= 10;
      sline += (*cp) - '0';
      cp++;
    }
    // "file:line!" means the assert suppression is not silent
    noisy = ((*cp) == '!');
    while ((*cp) != '\0' && !is_token_break(*cp))  cp++;
    // match the line
    if (sline != 0) {
      if (sline != line_no)  continue;
    }
    // match the file
    if (sfile_len > 0) {
      const char* look = file_name;
      const char* look_max = file_name + file_name_len - sfile_len;
      const char* foundp;
      bool match = false;
      while (!match
             && (foundp = strchr(look, sfile[0])) != NULL
             && foundp <= look_max) {
        match = true;
        for (int i = 1; i < sfile_len; i++) {
          if (sfile[i] != foundp[i]) {
            match = false;
            break;
          }
        }
        look = foundp + 1;
      }
      if (!match)  continue;
    }
    // got a match!
    if (noisy) {
      fdStream out(defaultStream::output_fd());
      out.print_raw("[error suppressed at ");
      out.print_raw(base_name);
      char buf[16];
      jio_snprintf(buf, sizeof(buf), ":%d]", line_no);
      out.print_raw_cr(buf);
    } else {
      // update 1-element cache for fast silent matches
      last_file_name = file_name;
      last_line_no   = line_no;
    }
    return true;
  }

  if (!is_error_reported()) {
    // print a friendly hint:
    fdStream out(defaultStream::output_fd());
    out.print_raw_cr("# To suppress the following error report, specify this argument");
    out.print_raw   ("# after -XX: or in .hotspotrc:  SuppressErrorAt=");
    out.print_raw   (base_name);
    char buf[16];
    jio_snprintf(buf, sizeof(buf), ":%d", line_no);
    out.print_raw_cr(buf);
  }
  return false;
}

#undef is_token_break

#else

// Place-holder for non-existent suppression check:
216
#define error_is_suppressed(file_name, line_no) (false)
D
duke 已提交
217

D
dcubed 已提交
218
#endif // !PRODUCT
D
duke 已提交
219

220 221 222 223 224 225
void report_vm_error(const char* file, int line, const char* error_msg,
                     const char* detail_msg)
{
  if (Debugging || error_is_suppressed(file, line)) return;
  Thread* const thread = ThreadLocalStorage::get_thread_slow();
  VMError err(thread, file, line, error_msg, detail_msg);
D
duke 已提交
226 227 228
  err.report_and_die();
}

229 230 231
void report_fatal(const char* file, int line, const char* message)
{
  report_vm_error(file, line, "fatal error", message);
D
duke 已提交
232 233 234 235 236
}

// Used by report_vm_out_of_memory to detect recursion.
static jint _exiting_out_of_mem = 0;

237 238
void report_vm_out_of_memory(const char* file, int line, size_t size,
                             const char* message) {
239
  if (Debugging) return;
D
duke 已提交
240 241 242 243 244 245 246 247 248 249 250

  // We try to gather additional information for the first out of memory
  // error only; gathering additional data might cause an allocation and a
  // recursive out_of_memory condition.

  const jint exiting = 1;
  // If we succeed in changing the value, we're the first one in.
  bool first_time_here = Atomic::xchg(exiting, &_exiting_out_of_mem) != exiting;

  if (first_time_here) {
    Thread* thread = ThreadLocalStorage::get_thread_slow();
251
    VMError(thread, file, line, size, message).report_and_die();
D
duke 已提交
252
  }
253 254 255

  // Dump core and abort
  vm_abort(true);
D
duke 已提交
256 257
}

258 259
void report_should_not_call(const char* file, int line) {
  report_vm_error(file, line, "ShouldNotCall()");
D
duke 已提交
260 261
}

262 263
void report_should_not_reach_here(const char* file, int line) {
  report_vm_error(file, line, "ShouldNotReachHere()");
D
duke 已提交
264 265
}

266 267 268 269
void report_should_not_reach_here2(const char* file, int line, const char* message) {
  report_vm_error(file, line, "ShouldNotReachHere()", message);
}

270 271
void report_unimplemented(const char* file, int line) {
  report_vm_error(file, line, "Unimplemented()");
D
duke 已提交
272 273
}

274
void report_untested(const char* file, int line, const char* message) {
D
duke 已提交
275
#ifndef PRODUCT
276
  warning("Untested: %s in %s: %d\n", message, file, line);
D
dcubed 已提交
277
#endif // !PRODUCT
D
duke 已提交
278 279
}

280 281
void report_out_of_shared_space(SharedSpaceType shared_space) {
  static const char* name[] = {
282
    "native memory for metadata",
283 284 285 286 287
    "shared read only space",
    "shared read write space",
    "shared miscellaneous data space"
  };
  static const char* flag[] = {
288
    "Metaspace",
289 290 291 292 293 294 295 296 297 298 299 300
    "SharedReadOnlySize",
    "SharedReadWriteSize",
    "SharedMiscDataSize"
  };

   warning("\nThe %s is not large enough\n"
           "to preload requested classes. Use -XX:%s=\n"
           "to increase the initial size of %s.\n",
           name[shared_space], flag[shared_space], name[shared_space]);
   exit(2);
}

D
duke 已提交
301 302 303 304 305 306 307 308 309 310 311
void report_java_out_of_memory(const char* message) {
  static jint out_of_memory_reported = 0;

  // A number of threads may attempt to report OutOfMemoryError at around the
  // same time. To avoid dumping the heap or executing the data collection
  // commands multiple times we just do it once when the first threads reports
  // the error.
  if (Atomic::cmpxchg(1, &out_of_memory_reported, 0) == 0) {
    // create heap dump before OnOutOfMemoryError commands are executed
    if (HeapDumpOnOutOfMemoryError) {
      tty->print_cr("java.lang.OutOfMemoryError: %s", message);
312
      HeapDumper::dump_heap_from_oome();
D
duke 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    }

    if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
      VMError err(message);
      err.report_java_out_of_memory();
    }
  }
}

static bool error_reported = false;

// call this when the VM is dying--it might loosen some asserts
void set_error_reported() {
  error_reported = true;
}

bool is_error_reported() {
    return error_reported;
}

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
#ifndef PRODUCT
#include <signal.h>

void test_error_handler(size_t test_num)
{
  if (test_num == 0) return;

  // If asserts are disabled, use the corresponding guarantee instead.
  size_t n = test_num;
  NOT_DEBUG(if (n <= 2) n += 2);

  const char* const str = "hello";
  const size_t      num = (size_t)os::vm_page_size();

  const char* const eol = os::line_separator();
  const char* const msg = "this message should be truncated during formatting";

  // Keep this in sync with test/runtime/6888954/vmerrors.sh.
  switch (n) {
    case  1: assert(str == NULL, "expected null");
    case  2: assert(num == 1023 && *str == 'X',
                    err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));
    case  3: guarantee(str == NULL, "expected null");
    case  4: guarantee(num == 1023 && *str == 'X',
                       err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));
    case  5: fatal("expected null");
    case  6: fatal(err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));
    case  7: fatal(err_msg("%s%s#    %s%s#    %s%s#    %s%s#    %s%s#    "
                           "%s%s#    %s%s#    %s%s#    %s%s#    %s%s#    "
                           "%s%s#    %s%s#    %s%s#    %s%s#    %s",
                           msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
                           msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
                           msg, eol, msg, eol, msg, eol, msg, eol, msg));
    case  8: vm_exit_out_of_memory(num, "ChunkPool::allocate");
    case  9: ShouldNotCallThis();
    case 10: ShouldNotReachHere();
    case 11: Unimplemented();
    // This is last because it does not generate an hs_err* file on Windows.
    case 12: os::signal_raise(SIGSEGV);

    default: ShouldNotReachHere();
  }
}
D
dcubed 已提交
376
#endif // !PRODUCT
377

D
duke 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
// ------ helper functions for debugging go here ------------

// All debug entries should be wrapped with a stack allocated
// Command object. It makes sure a resource mark is set and
// flushes the logfile to prevent file sharing problems.

class Command : public StackObj {
 private:
  ResourceMark rm;
  ResetNoHandleMark rnhm;
  HandleMark   hm;
  bool debug_save;
 public:
  static int level;
  Command(const char* str) {
    debug_save = Debugging;
    Debugging = true;
    if (level++ > 0)  return;
    tty->cr();
    tty->print_cr("\"Executing %s\"", str);
  }

D
dcubed 已提交
400 401 402 403 404
  ~Command() {
        tty->flush();
        Debugging = debug_save;
        level--;
  }
D
duke 已提交
405 406 407 408
};

int Command::level = 0;

D
dcubed 已提交
409 410
#ifndef PRODUCT

D
duke 已提交
411 412 413 414 415 416 417 418
extern "C" void blob(CodeBlob* cb) {
  Command c("blob");
  cb->print();
}


extern "C" void dump_vtable(address p) {
  Command c("dump_vtable");
419 420
  Klass* k = (Klass*)p;
  InstanceKlass::cast(k)->vtable()->print();
D
duke 已提交
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
}


extern "C" void nm(intptr_t p) {
  // Actually we look through all CodeBlobs (the nm name has been kept for backwards compatability)
  Command c("nm");
  CodeBlob* cb = CodeCache::find_blob((address)p);
  if (cb == NULL) {
    tty->print_cr("NULL");
  } else {
    cb->print();
  }
}


extern "C" void disnm(intptr_t p) {
  Command c("disnm");
  CodeBlob* cb = CodeCache::find_blob((address) p);
439 440 441 442 443 444 445 446
  nmethod* nm = cb->as_nmethod_or_null();
  if (nm) {
    nm->print();
    Disassembler::decode(nm);
  } else {
    cb->print();
    Disassembler::decode(cb);
  }
D
duke 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
}


extern "C" void printnm(intptr_t p) {
  char buffer[256];
  sprintf(buffer, "printnm: " INTPTR_FORMAT, p);
  Command c(buffer);
  CodeBlob* cb = CodeCache::find_blob((address) p);
  if (cb->is_nmethod()) {
    nmethod* nm = (nmethod*)cb;
    nm->print_nmethod(true);
  }
}


extern "C" void universe() {
  Command c("universe");
  Universe::print();
}


extern "C" void verify() {
  // try to run a verify on the entire system
  // note: this may not be safe if we're not at a safepoint; for debugging,
  // this manipulates the safepoint settings to avoid assertion failures
  Command c("universe verify");
  bool safe = SafepointSynchronize::is_at_safepoint();
  if (!safe) {
    tty->print_cr("warning: not at safepoint -- verify may fail");
    SafepointSynchronize::set_is_at_safepoint();
  }
  // Ensure Eden top is correct before verification
  Universe::heap()->prepare_for_verify();
480
  Universe::verify();
D
duke 已提交
481 482 483 484 485 486 487
  if (!safe) SafepointSynchronize::set_is_not_at_safepoint();
}


extern "C" void pp(void* p) {
  Command c("pp");
  FlagSetting fl(PrintVMMessages, true);
488
  FlagSetting f2(DisplayVMOutput, true);
D
duke 已提交
489 490 491 492
  if (Universe::heap()->is_in(p)) {
    oop obj = oop(p);
    obj->print();
  } else {
D
dcubed 已提交
493
    tty->print(PTR_FORMAT, p);
D
duke 已提交
494 495 496 497 498 499 500 501
  }
}


// pv: print vm-printable object
extern "C" void pa(intptr_t p)   { ((AllocatedObj*) p)->print(); }
extern "C" void findpc(intptr_t x);

D
dcubed 已提交
502 503
#endif // !PRODUCT

D
duke 已提交
504
extern "C" void ps() { // print stack
D
dcubed 已提交
505
  if (Thread::current() == NULL) return;
D
duke 已提交
506 507 508 509 510 511 512 513 514 515 516 517
  Command c("ps");


  // Prints the stack of the current Java thread
  JavaThread* p = JavaThread::active();
  tty->print(" for thread: ");
  p->print();
  tty->cr();

  if (p->has_last_Java_frame()) {
    // If the last_Java_fp is set we are in C land and
    // can call the standard stack_trace function.
D
dcubed 已提交
518 519 520 521 522
#ifdef PRODUCT
    p->print_stack();
  } else {
    tty->print_cr("Cannot find the last Java frame, printing stack disabled.");
#else // !PRODUCT
D
duke 已提交
523 524 525 526 527 528 529 530
    p->trace_stack();
  } else {
    frame f = os::current_frame();
    RegisterMap reg_map(p);
    f = f.sender(&reg_map);
    tty->print("(guessing starting frame id=%#p based on current fp)\n", f.id());
    p->trace_stack_from(vframe::new_vframe(&f, &reg_map, p));
  pd_ps(f);
D
dcubed 已提交
531
#endif // PRODUCT
D
duke 已提交
532 533 534 535
  }

}

536 537 538 539 540 541 542 543 544 545 546
extern "C" void pfl() {
  // print frame layout
  Command c("pfl");
  JavaThread* p = JavaThread::active();
  tty->print(" for thread: ");
  p->print();
  tty->cr();
  if (p->has_last_Java_frame()) {
    p->print_frame_layout();
  }
}
D
duke 已提交
547

D
dcubed 已提交
548 549
#ifndef PRODUCT

D
duke 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
extern "C" void psf() { // print stack frames
  {
    Command c("psf");
    JavaThread* p = JavaThread::active();
    tty->print(" for thread: ");
    p->print();
    tty->cr();
    if (p->has_last_Java_frame()) {
      p->trace_frames();
    }
  }
}


extern "C" void threads() {
  Command c("threads");
  Threads::print(false, true);
}


extern "C" void psd() {
  Command c("psd");
  SystemDictionary::print();
}


extern "C" void safepoints() {
  Command c("safepoints");
  SafepointSynchronize::print_state();
}

D
dcubed 已提交
581
#endif // !PRODUCT
D
duke 已提交
582 583

extern "C" void pss() { // print all stacks
D
dcubed 已提交
584
  if (Thread::current() == NULL) return;
D
duke 已提交
585
  Command c("pss");
D
dcubed 已提交
586
  Threads::print(true, PRODUCT_ONLY(false) NOT_PRODUCT(true));
D
duke 已提交
587 588
}

D
dcubed 已提交
589
#ifndef PRODUCT
D
duke 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

extern "C" void debug() {               // to set things up for compiler debugging
  Command c("debug");
  WizardMode = true;
  PrintVMMessages = PrintCompilation = true;
  PrintInlining = PrintAssembly = true;
  tty->flush();
}


extern "C" void ndebug() {              // undo debug()
  Command c("ndebug");
  PrintCompilation = false;
  PrintInlining = PrintAssembly = false;
  tty->flush();
}


extern "C" void flush()  {
  Command c("flush");
  tty->flush();
}

N
never 已提交
613 614 615 616
extern "C" void events() {
  Command c("events");
  Events::print();
}
D
duke 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

// Given a heap address that was valid before the most recent GC, if
// the oop that used to contain it is still live, prints the new
// location of the oop and the address. Useful for tracking down
// certain kinds of naked oop and oop map bugs.
extern "C" void pnl(intptr_t old_heap_addr) {
  // Print New Location of old heap address
  Command c("pnl");
#ifndef VALIDATE_MARK_SWEEP
  tty->print_cr("Requires build with VALIDATE_MARK_SWEEP defined (debug build) and RecordMarkSweepCompaction enabled");
#else
  MarkSweep::print_new_location_of_heap_address((HeapWord*) old_heap_addr);
#endif
}


633
extern "C" Method* findm(intptr_t pc) {
D
duke 已提交
634 635
  Command c("findm");
  nmethod* nm = CodeCache::find_nmethod((address)pc);
636
  return (nm == NULL) ? (Method*)NULL : nm->method();
D
duke 已提交
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
}


extern "C" nmethod* findnm(intptr_t addr) {
  Command c("findnm");
  return  CodeCache::find_nmethod((address)addr);
}

static address same_page(address x, address y) {
  intptr_t page_bits = -os::vm_page_size();
  if ((intptr_t(x) & page_bits) == (intptr_t(y) & page_bits)) {
    return x;
  } else if (x > y) {
    return (address)(intptr_t(y) | ~page_bits) + 1;
  } else {
    return (address)(intptr_t(y) & page_bits);
  }
}


// Another interface that isn't ambiguous in dbx.
// Can we someday rename the other find to hsfind?
extern "C" void hsfind(intptr_t x) {
  Command c("hsfind");
661
  os::print_location(tty, x, false);
D
duke 已提交
662 663 664 665 666
}


extern "C" void find(intptr_t x) {
  Command c("find");
667
  os::print_location(tty, x, false);
D
duke 已提交
668 669 670 671 672
}


extern "C" void findpc(intptr_t x) {
  Command c("findpc");
673
  os::print_location(tty, x, true);
D
duke 已提交
674 675 676
}


677 678 679 680 681 682 683 684 685 686 687
// Need method pointer to find bcp, when not in permgen.
extern "C" void findbcp(intptr_t method, intptr_t bcp) {
  Command c("findbcp");
  Method* mh = (Method*)method;
  if (!mh->is_native()) {
    tty->print_cr("bci_from(%p) = %d; print_codes():",
                        mh, mh->bci_from(address(bcp)));
    mh->print_codes_on(tty);
  }
}

D
duke 已提交
688 689 690 691 692 693 694 695 696 697 698 699
// int versions of all methods to avoid having to type type casts in the debugger

void pp(intptr_t p)          { pp((void*)p); }
void pp(oop p)               { pp((void*)p); }

void help() {
  Command c("help");
  tty->print_cr("basic");
  tty->print_cr("  pp(void* p)   - try to make sense of p");
  tty->print_cr("  pv(intptr_t p)- ((PrintableResourceObj*) p)->print()");
  tty->print_cr("  ps()          - print current thread stack");
  tty->print_cr("  pss()         - print all thread stacks");
700 701
  tty->print_cr("  pm(int pc)    - print Method* given compiled PC");
  tty->print_cr("  findm(intptr_t pc) - finds Method*");
D
duke 已提交
702 703 704 705
  tty->print_cr("  find(intptr_t x)   - finds & prints nmethod/stub/bytecode/oop based on pointer into it");

  tty->print_cr("misc.");
  tty->print_cr("  flush()       - flushes the log file");
N
never 已提交
706
  tty->print_cr("  events()      - dump events from ring buffers");
D
duke 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747


  tty->print_cr("compiler debugging");
  tty->print_cr("  debug()       - to set things up for compiler debugging");
  tty->print_cr("  ndebug()      - undo debug");
}

#if 0

// BobV's command parser for debugging on windows when nothing else works.

enum CommandID {
  CMDID_HELP,
  CMDID_QUIT,
  CMDID_HSFIND,
  CMDID_PSS,
  CMDID_PS,
  CMDID_PSF,
  CMDID_FINDM,
  CMDID_FINDNM,
  CMDID_PP,
  CMDID_BPT,
  CMDID_EXIT,
  CMDID_VERIFY,
  CMDID_THREADS,
  CMDID_ILLEGAL = 99
};

struct CommandParser {
   char *name;
   CommandID code;
   char *description;
};

struct CommandParser CommandList[] = {
  (char *)"help", CMDID_HELP, "  Dump this list",
  (char *)"quit", CMDID_QUIT, "  Return from this routine",
  (char *)"hsfind", CMDID_HSFIND, "Perform an hsfind on an address",
  (char *)"ps", CMDID_PS, "    Print Current Thread Stack Trace",
  (char *)"pss", CMDID_PSS, "   Print All Thread Stack Trace",
  (char *)"psf", CMDID_PSF, "   Print All Stack Frames",
748
  (char *)"findm", CMDID_FINDM, " Find a Method* from a PC",
D
duke 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
  (char *)"findnm", CMDID_FINDNM, "Find an nmethod from a PC",
  (char *)"pp", CMDID_PP, "    Find out something about a pointer",
  (char *)"break", CMDID_BPT, " Execute a breakpoint",
  (char *)"exitvm", CMDID_EXIT, "Exit the VM",
  (char *)"verify", CMDID_VERIFY, "Perform a Heap Verify",
  (char *)"thread", CMDID_THREADS, "Dump Info on all Threads",
  (char *)0, CMDID_ILLEGAL
};


// get_debug_command()
//
// Read a command from standard input.
// This is useful when you have a debugger
// which doesn't support calling into functions.
//
void get_debug_command()
{
  ssize_t count;
  int i,j;
  bool gotcommand;
  intptr_t addr;
  char buffer[256];
  nmethod *nm;
773
  Method* m;
D
duke 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

  tty->print_cr("You have entered the diagnostic command interpreter");
  tty->print("The supported commands are:\n");
  for ( i=0; ; i++ ) {
    if ( CommandList[i].code == CMDID_ILLEGAL )
      break;
    tty->print_cr("  %s \n", CommandList[i].name );
  }

  while ( 1 ) {
    gotcommand = false;
    tty->print("Please enter a command: ");
    count = scanf("%s", buffer) ;
    if ( count >=0 ) {
      for ( i=0; ; i++ ) {
        if ( CommandList[i].code == CMDID_ILLEGAL ) {
          if (!gotcommand) tty->print("Invalid command, please try again\n");
          break;
        }
        if ( strcmp(buffer, CommandList[i].name) == 0 ) {
          gotcommand = true;
          switch ( CommandList[i].code ) {
              case CMDID_PS:
                ps();
                break;
              case CMDID_PSS:
                pss();
                break;
              case CMDID_PSF:
                psf();
                break;
              case CMDID_FINDM:
                tty->print("Please enter the hex addr to pass to findm: ");
                scanf("%I64X", &addr);
808
                m = (Method*)findm(addr);
D
duke 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
                tty->print("findm(0x%I64X) returned 0x%I64X\n", addr, m);
                break;
              case CMDID_FINDNM:
                tty->print("Please enter the hex addr to pass to findnm: ");
                scanf("%I64X", &addr);
                nm = (nmethod*)findnm(addr);
                tty->print("findnm(0x%I64X) returned 0x%I64X\n", addr, nm);
                break;
              case CMDID_PP:
                tty->print("Please enter the hex addr to pass to pp: ");
                scanf("%I64X", &addr);
                pp((void*)addr);
                break;
              case CMDID_EXIT:
                exit(0);
              case CMDID_HELP:
                tty->print("Here are the supported commands: ");
                for ( j=0; ; j++ ) {
                  if ( CommandList[j].code == CMDID_ILLEGAL )
                    break;
                  tty->print_cr("  %s --  %s\n", CommandList[j].name,
                                                 CommandList[j].description );
                }
                break;
              case CMDID_QUIT:
                return;
                break;
              case CMDID_BPT:
                BREAKPOINT;
                break;
              case CMDID_VERIFY:
                verify();;
                break;
              case CMDID_THREADS:
                threads();;
                break;
              case CMDID_HSFIND:
                tty->print("Please enter the hex addr to pass to hsfind: ");
                scanf("%I64X", &addr);
                tty->print("Calling hsfind(0x%I64X)\n", addr);
                hsfind(addr);
                break;
              default:
              case CMDID_ILLEGAL:
                break;
          }
        }
      }
    }
  }
}
#endif

D
dcubed 已提交
862
#endif // !PRODUCT