src/share/vm/utilities/debug.cpp

Mon, 03 Jan 2011 14:09:11 -0500

author
coleenp
date
Mon, 03 Jan 2011 14:09:11 -0500
changeset 2418
36c186bcc085
parent 2350
2f644f85485d
child 2497
3582bf76420e
permissions
-rw-r--r--

6302804: Hotspot VM dies ungraceful death when C heap is exhausted in various places.
Summary: enhance the error reporting mechanism to help user to fix the problem rather than making it look like a VM error.
Reviewed-by: kvn, kamg

duke@435 1 /*
bobv@2036 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/systemDictionary.hpp"
stefank@2314 27 #include "code/codeCache.hpp"
stefank@2314 28 #include "code/icBuffer.hpp"
stefank@2314 29 #include "code/nmethod.hpp"
stefank@2314 30 #include "code/vtableStubs.hpp"
stefank@2314 31 #include "compiler/compileBroker.hpp"
stefank@2314 32 #include "compiler/disassembler.hpp"
stefank@2314 33 #include "gc_implementation/shared/markSweep.hpp"
stefank@2314 34 #include "gc_interface/collectedHeap.hpp"
stefank@2314 35 #include "interpreter/bytecodeHistogram.hpp"
stefank@2314 36 #include "interpreter/interpreter.hpp"
stefank@2314 37 #include "memory/resourceArea.hpp"
stefank@2314 38 #include "memory/universe.hpp"
stefank@2314 39 #include "oops/oop.inline.hpp"
stefank@2314 40 #include "prims/privilegedStack.hpp"
stefank@2314 41 #include "runtime/arguments.hpp"
stefank@2314 42 #include "runtime/frame.hpp"
stefank@2314 43 #include "runtime/java.hpp"
stefank@2314 44 #include "runtime/sharedRuntime.hpp"
stefank@2314 45 #include "runtime/stubCodeGenerator.hpp"
stefank@2314 46 #include "runtime/stubRoutines.hpp"
stefank@2314 47 #include "runtime/vframe.hpp"
stefank@2314 48 #include "services/heapDumper.hpp"
stefank@2314 49 #include "utilities/defaultStream.hpp"
stefank@2314 50 #include "utilities/events.hpp"
stefank@2314 51 #include "utilities/top.hpp"
stefank@2314 52 #include "utilities/vmError.hpp"
stefank@2314 53 #ifdef TARGET_OS_FAMILY_linux
stefank@2314 54 # include "os_linux.inline.hpp"
stefank@2314 55 # include "thread_linux.inline.hpp"
stefank@2314 56 #endif
stefank@2314 57 #ifdef TARGET_OS_FAMILY_solaris
stefank@2314 58 # include "os_solaris.inline.hpp"
stefank@2314 59 # include "thread_solaris.inline.hpp"
stefank@2314 60 #endif
stefank@2314 61 #ifdef TARGET_OS_FAMILY_windows
stefank@2314 62 # include "os_windows.inline.hpp"
stefank@2314 63 # include "thread_windows.inline.hpp"
stefank@2314 64 #endif
duke@435 65
duke@435 66 #ifndef ASSERT
duke@435 67 # ifdef _DEBUG
duke@435 68 // NOTE: don't turn the lines below into a comment -- if you're getting
duke@435 69 // a compile error here, change the settings to define ASSERT
duke@435 70 ASSERT should be defined when _DEBUG is defined. It is not intended to be used for debugging
duke@435 71 functions that do not slow down the system too much and thus can be left in optimized code.
duke@435 72 On the other hand, the code should not be included in a production version.
duke@435 73 # endif // _DEBUG
duke@435 74 #endif // ASSERT
duke@435 75
duke@435 76
duke@435 77 #ifdef _DEBUG
duke@435 78 # ifndef ASSERT
duke@435 79 configuration error: ASSERT must be defined in debug version
duke@435 80 # endif // ASSERT
duke@435 81 #endif // _DEBUG
duke@435 82
duke@435 83
duke@435 84 #ifdef PRODUCT
duke@435 85 # if -defined _DEBUG || -defined ASSERT
duke@435 86 configuration error: ASSERT et al. must not be defined in PRODUCT version
duke@435 87 # endif
duke@435 88 #endif // PRODUCT
duke@435 89
duke@435 90
duke@435 91 void warning(const char* format, ...) {
kamg@2225 92 if (PrintWarnings) {
kamg@2225 93 // In case error happens before init or during shutdown
kamg@2225 94 if (tty == NULL) ostream_init();
duke@435 95
kamg@2225 96 tty->print("%s warning: ", VM_Version::vm_name());
kamg@2225 97 va_list ap;
kamg@2225 98 va_start(ap, format);
kamg@2225 99 tty->vprint_cr(format, ap);
kamg@2225 100 va_end(ap);
kamg@2225 101 }
duke@435 102 if (BreakAtWarning) BREAKPOINT;
duke@435 103 }
duke@435 104
duke@435 105 #ifndef PRODUCT
duke@435 106
duke@435 107 #define is_token_break(ch) (isspace(ch) || (ch) == ',')
duke@435 108
duke@435 109 static const char* last_file_name = NULL;
duke@435 110 static int last_line_no = -1;
duke@435 111
duke@435 112 // assert/guarantee/... may happen very early during VM initialization.
duke@435 113 // Don't rely on anything that is initialized by Threads::create_vm(). For
duke@435 114 // example, don't use tty.
jcoomes@1845 115 bool error_is_suppressed(const char* file_name, int line_no) {
duke@435 116 // The following 1-element cache requires that passed-in
duke@435 117 // file names are always only constant literals.
duke@435 118 if (file_name == last_file_name && line_no == last_line_no) return true;
duke@435 119
duke@435 120 int file_name_len = (int)strlen(file_name);
duke@435 121 char separator = os::file_separator()[0];
duke@435 122 const char* base_name = strrchr(file_name, separator);
duke@435 123 if (base_name == NULL)
duke@435 124 base_name = file_name;
duke@435 125
duke@435 126 // scan the SuppressErrorAt option
duke@435 127 const char* cp = SuppressErrorAt;
duke@435 128 for (;;) {
duke@435 129 const char* sfile;
duke@435 130 int sfile_len;
duke@435 131 int sline;
duke@435 132 bool noisy;
duke@435 133 while ((*cp) != '\0' && is_token_break(*cp)) cp++;
duke@435 134 if ((*cp) == '\0') break;
duke@435 135 sfile = cp;
duke@435 136 while ((*cp) != '\0' && !is_token_break(*cp) && (*cp) != ':') cp++;
duke@435 137 sfile_len = cp - sfile;
duke@435 138 if ((*cp) == ':') cp++;
duke@435 139 sline = 0;
duke@435 140 while ((*cp) != '\0' && isdigit(*cp)) {
duke@435 141 sline *= 10;
duke@435 142 sline += (*cp) - '0';
duke@435 143 cp++;
duke@435 144 }
duke@435 145 // "file:line!" means the assert suppression is not silent
duke@435 146 noisy = ((*cp) == '!');
duke@435 147 while ((*cp) != '\0' && !is_token_break(*cp)) cp++;
duke@435 148 // match the line
duke@435 149 if (sline != 0) {
duke@435 150 if (sline != line_no) continue;
duke@435 151 }
duke@435 152 // match the file
duke@435 153 if (sfile_len > 0) {
duke@435 154 const char* look = file_name;
duke@435 155 const char* look_max = file_name + file_name_len - sfile_len;
duke@435 156 const char* foundp;
duke@435 157 bool match = false;
duke@435 158 while (!match
duke@435 159 && (foundp = strchr(look, sfile[0])) != NULL
duke@435 160 && foundp <= look_max) {
duke@435 161 match = true;
duke@435 162 for (int i = 1; i < sfile_len; i++) {
duke@435 163 if (sfile[i] != foundp[i]) {
duke@435 164 match = false;
duke@435 165 break;
duke@435 166 }
duke@435 167 }
duke@435 168 look = foundp + 1;
duke@435 169 }
duke@435 170 if (!match) continue;
duke@435 171 }
duke@435 172 // got a match!
duke@435 173 if (noisy) {
duke@435 174 fdStream out(defaultStream::output_fd());
duke@435 175 out.print_raw("[error suppressed at ");
duke@435 176 out.print_raw(base_name);
duke@435 177 char buf[16];
duke@435 178 jio_snprintf(buf, sizeof(buf), ":%d]", line_no);
duke@435 179 out.print_raw_cr(buf);
duke@435 180 } else {
duke@435 181 // update 1-element cache for fast silent matches
duke@435 182 last_file_name = file_name;
duke@435 183 last_line_no = line_no;
duke@435 184 }
duke@435 185 return true;
duke@435 186 }
duke@435 187
duke@435 188 if (!is_error_reported()) {
duke@435 189 // print a friendly hint:
duke@435 190 fdStream out(defaultStream::output_fd());
duke@435 191 out.print_raw_cr("# To suppress the following error report, specify this argument");
duke@435 192 out.print_raw ("# after -XX: or in .hotspotrc: SuppressErrorAt=");
duke@435 193 out.print_raw (base_name);
duke@435 194 char buf[16];
duke@435 195 jio_snprintf(buf, sizeof(buf), ":%d", line_no);
duke@435 196 out.print_raw_cr(buf);
duke@435 197 }
duke@435 198 return false;
duke@435 199 }
duke@435 200
duke@435 201 #undef is_token_break
duke@435 202
duke@435 203 #else
duke@435 204
duke@435 205 // Place-holder for non-existent suppression check:
jcoomes@1845 206 #define error_is_suppressed(file_name, line_no) (false)
duke@435 207
duke@435 208 #endif //PRODUCT
duke@435 209
jcoomes@1845 210 void report_vm_error(const char* file, int line, const char* error_msg,
jcoomes@1845 211 const char* detail_msg)
jcoomes@1845 212 {
jcoomes@1845 213 if (Debugging || error_is_suppressed(file, line)) return;
jcoomes@1845 214 Thread* const thread = ThreadLocalStorage::get_thread_slow();
jcoomes@1845 215 VMError err(thread, file, line, error_msg, detail_msg);
duke@435 216 err.report_and_die();
duke@435 217 }
duke@435 218
jcoomes@1845 219 void report_fatal(const char* file, int line, const char* message)
jcoomes@1845 220 {
jcoomes@1845 221 report_vm_error(file, line, "fatal error", message);
duke@435 222 }
duke@435 223
duke@435 224 // Used by report_vm_out_of_memory to detect recursion.
duke@435 225 static jint _exiting_out_of_mem = 0;
duke@435 226
jcoomes@1845 227 void report_vm_out_of_memory(const char* file, int line, size_t size,
jcoomes@1845 228 const char* message) {
coleenp@2418 229 if (Debugging) return;
duke@435 230
duke@435 231 // We try to gather additional information for the first out of memory
duke@435 232 // error only; gathering additional data might cause an allocation and a
duke@435 233 // recursive out_of_memory condition.
duke@435 234
duke@435 235 const jint exiting = 1;
duke@435 236 // If we succeed in changing the value, we're the first one in.
duke@435 237 bool first_time_here = Atomic::xchg(exiting, &_exiting_out_of_mem) != exiting;
duke@435 238
duke@435 239 if (first_time_here) {
duke@435 240 Thread* thread = ThreadLocalStorage::get_thread_slow();
jcoomes@1845 241 VMError(thread, file, line, size, message).report_and_die();
duke@435 242 }
poonam@662 243
poonam@662 244 // Dump core and abort
poonam@662 245 vm_abort(true);
duke@435 246 }
duke@435 247
jcoomes@1845 248 void report_should_not_call(const char* file, int line) {
jcoomes@1845 249 report_vm_error(file, line, "ShouldNotCall()");
duke@435 250 }
duke@435 251
jcoomes@1845 252 void report_should_not_reach_here(const char* file, int line) {
jcoomes@1845 253 report_vm_error(file, line, "ShouldNotReachHere()");
duke@435 254 }
duke@435 255
jcoomes@1845 256 void report_unimplemented(const char* file, int line) {
jcoomes@1845 257 report_vm_error(file, line, "Unimplemented()");
duke@435 258 }
duke@435 259
jcoomes@1845 260 void report_untested(const char* file, int line, const char* message) {
duke@435 261 #ifndef PRODUCT
jcoomes@1845 262 warning("Untested: %s in %s: %d\n", message, file, line);
duke@435 263 #endif // PRODUCT
duke@435 264 }
duke@435 265
duke@435 266 void report_java_out_of_memory(const char* message) {
duke@435 267 static jint out_of_memory_reported = 0;
duke@435 268
duke@435 269 // A number of threads may attempt to report OutOfMemoryError at around the
duke@435 270 // same time. To avoid dumping the heap or executing the data collection
duke@435 271 // commands multiple times we just do it once when the first threads reports
duke@435 272 // the error.
duke@435 273 if (Atomic::cmpxchg(1, &out_of_memory_reported, 0) == 0) {
duke@435 274 // create heap dump before OnOutOfMemoryError commands are executed
duke@435 275 if (HeapDumpOnOutOfMemoryError) {
duke@435 276 tty->print_cr("java.lang.OutOfMemoryError: %s", message);
thurka@2130 277 HeapDumper::dump_heap_from_oome();
duke@435 278 }
duke@435 279
duke@435 280 if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
duke@435 281 VMError err(message);
duke@435 282 err.report_java_out_of_memory();
duke@435 283 }
duke@435 284 }
duke@435 285 }
duke@435 286
duke@435 287
duke@435 288 extern "C" void ps();
duke@435 289
duke@435 290 static bool error_reported = false;
duke@435 291
duke@435 292 // call this when the VM is dying--it might loosen some asserts
duke@435 293 void set_error_reported() {
duke@435 294 error_reported = true;
duke@435 295 }
duke@435 296
duke@435 297 bool is_error_reported() {
duke@435 298 return error_reported;
duke@435 299 }
duke@435 300
jcoomes@1845 301 #ifndef PRODUCT
jcoomes@1845 302 #include <signal.h>
jcoomes@1845 303
jcoomes@1845 304 void test_error_handler(size_t test_num)
jcoomes@1845 305 {
jcoomes@1845 306 if (test_num == 0) return;
jcoomes@1845 307
jcoomes@1845 308 // If asserts are disabled, use the corresponding guarantee instead.
jcoomes@1845 309 size_t n = test_num;
jcoomes@1845 310 NOT_DEBUG(if (n <= 2) n += 2);
jcoomes@1845 311
jcoomes@1845 312 const char* const str = "hello";
jcoomes@1845 313 const size_t num = (size_t)os::vm_page_size();
jcoomes@1845 314
jcoomes@1845 315 const char* const eol = os::line_separator();
jcoomes@1845 316 const char* const msg = "this message should be truncated during formatting";
jcoomes@1845 317
jcoomes@1845 318 // Keep this in sync with test/runtime/6888954/vmerrors.sh.
jcoomes@1845 319 switch (n) {
jcoomes@1845 320 case 1: assert(str == NULL, "expected null");
jcoomes@1845 321 case 2: assert(num == 1023 && *str == 'X',
jcoomes@1845 322 err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));
jcoomes@1845 323 case 3: guarantee(str == NULL, "expected null");
jcoomes@1845 324 case 4: guarantee(num == 1023 && *str == 'X',
jcoomes@1845 325 err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));
jcoomes@1845 326 case 5: fatal("expected null");
jcoomes@1845 327 case 6: fatal(err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));
jcoomes@1845 328 case 7: fatal(err_msg("%s%s# %s%s# %s%s# %s%s# %s%s# "
jcoomes@1845 329 "%s%s# %s%s# %s%s# %s%s# %s%s# "
jcoomes@1845 330 "%s%s# %s%s# %s%s# %s%s# %s",
jcoomes@1845 331 msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
jcoomes@1845 332 msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,
jcoomes@1845 333 msg, eol, msg, eol, msg, eol, msg, eol, msg));
jcoomes@1845 334 case 8: vm_exit_out_of_memory(num, "ChunkPool::allocate");
jcoomes@1845 335 case 9: ShouldNotCallThis();
jcoomes@1845 336 case 10: ShouldNotReachHere();
jcoomes@1845 337 case 11: Unimplemented();
jcoomes@1845 338 // This is last because it does not generate an hs_err* file on Windows.
jcoomes@1845 339 case 12: os::signal_raise(SIGSEGV);
jcoomes@1845 340
jcoomes@1845 341 default: ShouldNotReachHere();
jcoomes@1845 342 }
jcoomes@1845 343 }
jcoomes@1845 344 #endif // #ifndef PRODUCT
jcoomes@1845 345
duke@435 346 // ------ helper functions for debugging go here ------------
duke@435 347
duke@435 348 #ifndef PRODUCT
duke@435 349 // All debug entries should be wrapped with a stack allocated
duke@435 350 // Command object. It makes sure a resource mark is set and
duke@435 351 // flushes the logfile to prevent file sharing problems.
duke@435 352
duke@435 353 class Command : public StackObj {
duke@435 354 private:
duke@435 355 ResourceMark rm;
duke@435 356 ResetNoHandleMark rnhm;
duke@435 357 HandleMark hm;
duke@435 358 bool debug_save;
duke@435 359 public:
duke@435 360 static int level;
duke@435 361 Command(const char* str) {
duke@435 362 debug_save = Debugging;
duke@435 363 Debugging = true;
duke@435 364 if (level++ > 0) return;
duke@435 365 tty->cr();
duke@435 366 tty->print_cr("\"Executing %s\"", str);
duke@435 367 }
duke@435 368
duke@435 369 ~Command() { tty->flush(); Debugging = debug_save; level--; }
duke@435 370 };
duke@435 371
duke@435 372 int Command::level = 0;
duke@435 373
duke@435 374 extern "C" void blob(CodeBlob* cb) {
duke@435 375 Command c("blob");
duke@435 376 cb->print();
duke@435 377 }
duke@435 378
duke@435 379
duke@435 380 extern "C" void dump_vtable(address p) {
duke@435 381 Command c("dump_vtable");
duke@435 382 klassOop k = (klassOop)p;
duke@435 383 instanceKlass::cast(k)->vtable()->print();
duke@435 384 }
duke@435 385
duke@435 386
duke@435 387 extern "C" void nm(intptr_t p) {
duke@435 388 // Actually we look through all CodeBlobs (the nm name has been kept for backwards compatability)
duke@435 389 Command c("nm");
duke@435 390 CodeBlob* cb = CodeCache::find_blob((address)p);
duke@435 391 if (cb == NULL) {
duke@435 392 tty->print_cr("NULL");
duke@435 393 } else {
duke@435 394 cb->print();
duke@435 395 }
duke@435 396 }
duke@435 397
duke@435 398
duke@435 399 extern "C" void disnm(intptr_t p) {
duke@435 400 Command c("disnm");
duke@435 401 CodeBlob* cb = CodeCache::find_blob((address) p);
twisti@2350 402 nmethod* nm = cb->as_nmethod_or_null();
twisti@2350 403 if (nm) {
twisti@2350 404 nm->print();
twisti@2350 405 Disassembler::decode(nm);
twisti@2350 406 } else {
twisti@2350 407 cb->print();
twisti@2350 408 Disassembler::decode(cb);
twisti@2350 409 }
duke@435 410 }
duke@435 411
duke@435 412
duke@435 413 extern "C" void printnm(intptr_t p) {
duke@435 414 char buffer[256];
duke@435 415 sprintf(buffer, "printnm: " INTPTR_FORMAT, p);
duke@435 416 Command c(buffer);
duke@435 417 CodeBlob* cb = CodeCache::find_blob((address) p);
duke@435 418 if (cb->is_nmethod()) {
duke@435 419 nmethod* nm = (nmethod*)cb;
duke@435 420 nm->print_nmethod(true);
duke@435 421 }
duke@435 422 }
duke@435 423
duke@435 424
duke@435 425 extern "C" void universe() {
duke@435 426 Command c("universe");
duke@435 427 Universe::print();
duke@435 428 }
duke@435 429
duke@435 430
duke@435 431 extern "C" void verify() {
duke@435 432 // try to run a verify on the entire system
duke@435 433 // note: this may not be safe if we're not at a safepoint; for debugging,
duke@435 434 // this manipulates the safepoint settings to avoid assertion failures
duke@435 435 Command c("universe verify");
duke@435 436 bool safe = SafepointSynchronize::is_at_safepoint();
duke@435 437 if (!safe) {
duke@435 438 tty->print_cr("warning: not at safepoint -- verify may fail");
duke@435 439 SafepointSynchronize::set_is_at_safepoint();
duke@435 440 }
duke@435 441 // Ensure Eden top is correct before verification
duke@435 442 Universe::heap()->prepare_for_verify();
duke@435 443 Universe::verify(true);
duke@435 444 if (!safe) SafepointSynchronize::set_is_not_at_safepoint();
duke@435 445 }
duke@435 446
duke@435 447
duke@435 448 extern "C" void pp(void* p) {
duke@435 449 Command c("pp");
duke@435 450 FlagSetting fl(PrintVMMessages, true);
duke@435 451 if (Universe::heap()->is_in(p)) {
duke@435 452 oop obj = oop(p);
duke@435 453 obj->print();
duke@435 454 } else {
duke@435 455 tty->print("%#p", p);
duke@435 456 }
duke@435 457 }
duke@435 458
duke@435 459
duke@435 460 // pv: print vm-printable object
duke@435 461 extern "C" void pa(intptr_t p) { ((AllocatedObj*) p)->print(); }
duke@435 462 extern "C" void findpc(intptr_t x);
duke@435 463
duke@435 464 extern "C" void ps() { // print stack
duke@435 465 Command c("ps");
duke@435 466
duke@435 467
duke@435 468 // Prints the stack of the current Java thread
duke@435 469 JavaThread* p = JavaThread::active();
duke@435 470 tty->print(" for thread: ");
duke@435 471 p->print();
duke@435 472 tty->cr();
duke@435 473
duke@435 474 if (p->has_last_Java_frame()) {
duke@435 475 // If the last_Java_fp is set we are in C land and
duke@435 476 // can call the standard stack_trace function.
duke@435 477 p->trace_stack();
duke@435 478 } else {
duke@435 479 frame f = os::current_frame();
duke@435 480 RegisterMap reg_map(p);
duke@435 481 f = f.sender(&reg_map);
duke@435 482 tty->print("(guessing starting frame id=%#p based on current fp)\n", f.id());
duke@435 483 p->trace_stack_from(vframe::new_vframe(&f, &reg_map, p));
duke@435 484 pd_ps(f);
duke@435 485 }
duke@435 486
duke@435 487 }
duke@435 488
duke@435 489
duke@435 490 extern "C" void psf() { // print stack frames
duke@435 491 {
duke@435 492 Command c("psf");
duke@435 493 JavaThread* p = JavaThread::active();
duke@435 494 tty->print(" for thread: ");
duke@435 495 p->print();
duke@435 496 tty->cr();
duke@435 497 if (p->has_last_Java_frame()) {
duke@435 498 p->trace_frames();
duke@435 499 }
duke@435 500 }
duke@435 501 }
duke@435 502
duke@435 503
duke@435 504 extern "C" void threads() {
duke@435 505 Command c("threads");
duke@435 506 Threads::print(false, true);
duke@435 507 }
duke@435 508
duke@435 509
duke@435 510 extern "C" void psd() {
duke@435 511 Command c("psd");
duke@435 512 SystemDictionary::print();
duke@435 513 }
duke@435 514
duke@435 515
duke@435 516 extern "C" void safepoints() {
duke@435 517 Command c("safepoints");
duke@435 518 SafepointSynchronize::print_state();
duke@435 519 }
duke@435 520
duke@435 521
duke@435 522 extern "C" void pss() { // print all stacks
duke@435 523 Command c("pss");
duke@435 524 Threads::print(true, true);
duke@435 525 }
duke@435 526
duke@435 527
duke@435 528 extern "C" void debug() { // to set things up for compiler debugging
duke@435 529 Command c("debug");
duke@435 530 WizardMode = true;
duke@435 531 PrintVMMessages = PrintCompilation = true;
duke@435 532 PrintInlining = PrintAssembly = true;
duke@435 533 tty->flush();
duke@435 534 }
duke@435 535
duke@435 536
duke@435 537 extern "C" void ndebug() { // undo debug()
duke@435 538 Command c("ndebug");
duke@435 539 PrintCompilation = false;
duke@435 540 PrintInlining = PrintAssembly = false;
duke@435 541 tty->flush();
duke@435 542 }
duke@435 543
duke@435 544
duke@435 545 extern "C" void flush() {
duke@435 546 Command c("flush");
duke@435 547 tty->flush();
duke@435 548 }
duke@435 549
duke@435 550
duke@435 551 extern "C" void events() {
duke@435 552 Command c("events");
duke@435 553 Events::print_last(tty, 50);
duke@435 554 }
duke@435 555
duke@435 556
duke@435 557 extern "C" void nevents(int n) {
duke@435 558 Command c("events");
duke@435 559 Events::print_last(tty, n);
duke@435 560 }
duke@435 561
duke@435 562
duke@435 563 // Given a heap address that was valid before the most recent GC, if
duke@435 564 // the oop that used to contain it is still live, prints the new
duke@435 565 // location of the oop and the address. Useful for tracking down
duke@435 566 // certain kinds of naked oop and oop map bugs.
duke@435 567 extern "C" void pnl(intptr_t old_heap_addr) {
duke@435 568 // Print New Location of old heap address
duke@435 569 Command c("pnl");
duke@435 570 #ifndef VALIDATE_MARK_SWEEP
duke@435 571 tty->print_cr("Requires build with VALIDATE_MARK_SWEEP defined (debug build) and RecordMarkSweepCompaction enabled");
duke@435 572 #else
duke@435 573 MarkSweep::print_new_location_of_heap_address((HeapWord*) old_heap_addr);
duke@435 574 #endif
duke@435 575 }
duke@435 576
duke@435 577
duke@435 578 extern "C" methodOop findm(intptr_t pc) {
duke@435 579 Command c("findm");
duke@435 580 nmethod* nm = CodeCache::find_nmethod((address)pc);
duke@435 581 return (nm == NULL) ? (methodOop)NULL : nm->method();
duke@435 582 }
duke@435 583
duke@435 584
duke@435 585 extern "C" nmethod* findnm(intptr_t addr) {
duke@435 586 Command c("findnm");
duke@435 587 return CodeCache::find_nmethod((address)addr);
duke@435 588 }
duke@435 589
duke@435 590 static address same_page(address x, address y) {
duke@435 591 intptr_t page_bits = -os::vm_page_size();
duke@435 592 if ((intptr_t(x) & page_bits) == (intptr_t(y) & page_bits)) {
duke@435 593 return x;
duke@435 594 } else if (x > y) {
duke@435 595 return (address)(intptr_t(y) | ~page_bits) + 1;
duke@435 596 } else {
duke@435 597 return (address)(intptr_t(y) & page_bits);
duke@435 598 }
duke@435 599 }
duke@435 600
duke@435 601 class LookForRefInGenClosure : public OopsInGenClosure {
duke@435 602 public:
duke@435 603 oop target;
duke@435 604 void do_oop(oop* o) {
duke@435 605 if (o != NULL && *o == target) {
ysr@777 606 tty->print_cr(INTPTR_FORMAT, o);
duke@435 607 }
duke@435 608 }
coleenp@548 609 void do_oop(narrowOop* o) { ShouldNotReachHere(); }
duke@435 610 };
duke@435 611
duke@435 612
duke@435 613 class LookForRefInObjectClosure : public ObjectClosure {
duke@435 614 private:
duke@435 615 LookForRefInGenClosure look_in_object;
duke@435 616 public:
duke@435 617 LookForRefInObjectClosure(oop target) { look_in_object.target = target; }
duke@435 618 void do_object(oop obj) {
duke@435 619 obj->oop_iterate(&look_in_object);
duke@435 620 }
duke@435 621 };
duke@435 622
duke@435 623
duke@435 624 static void findref(intptr_t x) {
ysr@777 625 CollectedHeap *ch = Universe::heap();
duke@435 626 LookForRefInGenClosure lookFor;
duke@435 627 lookFor.target = (oop) x;
duke@435 628 LookForRefInObjectClosure look_in_object((oop) x);
duke@435 629
duke@435 630 tty->print_cr("Searching heap:");
ysr@777 631 ch->object_iterate(&look_in_object);
duke@435 632
duke@435 633 tty->print_cr("Searching strong roots:");
duke@435 634 Universe::oops_do(&lookFor, false);
duke@435 635 JNIHandles::oops_do(&lookFor); // Global (strong) JNI handles
jrose@1424 636 Threads::oops_do(&lookFor, NULL);
duke@435 637 ObjectSynchronizer::oops_do(&lookFor);
duke@435 638 //FlatProfiler::oops_do(&lookFor);
duke@435 639 SystemDictionary::oops_do(&lookFor);
duke@435 640
jrose@1424 641 tty->print_cr("Searching code cache:");
jrose@1424 642 CodeCache::oops_do(&lookFor);
jrose@1424 643
duke@435 644 tty->print_cr("Done.");
duke@435 645 }
duke@435 646
duke@435 647 class FindClassObjectClosure: public ObjectClosure {
duke@435 648 private:
duke@435 649 const char* _target;
duke@435 650 public:
duke@435 651 FindClassObjectClosure(const char name[]) { _target = name; }
duke@435 652
duke@435 653 virtual void do_object(oop obj) {
duke@435 654 if (obj->is_klass()) {
duke@435 655 Klass* k = klassOop(obj)->klass_part();
duke@435 656 if (k->name() != NULL) {
duke@435 657 ResourceMark rm;
duke@435 658 const char* ext = k->external_name();
duke@435 659 if ( strcmp(_target, ext) == 0 ) {
duke@435 660 tty->print_cr("Found " INTPTR_FORMAT, obj);
duke@435 661 obj->print();
duke@435 662 }
duke@435 663 }
duke@435 664 }
duke@435 665 }
duke@435 666 };
duke@435 667
duke@435 668 //
duke@435 669 extern "C" void findclass(const char name[]) {
duke@435 670 Command c("findclass");
duke@435 671 if (name != NULL) {
duke@435 672 tty->print_cr("Finding class %s -> ", name);
duke@435 673 FindClassObjectClosure srch(name);
duke@435 674 Universe::heap()->permanent_object_iterate(&srch);
duke@435 675 }
duke@435 676 }
duke@435 677
duke@435 678 // Another interface that isn't ambiguous in dbx.
duke@435 679 // Can we someday rename the other find to hsfind?
duke@435 680 extern "C" void hsfind(intptr_t x) {
duke@435 681 Command c("hsfind");
bobv@2036 682 os::print_location(tty, x, false);
duke@435 683 }
duke@435 684
duke@435 685
duke@435 686 extern "C" void hsfindref(intptr_t x) {
duke@435 687 Command c("hsfindref");
duke@435 688 findref(x);
duke@435 689 }
duke@435 690
duke@435 691 extern "C" void find(intptr_t x) {
duke@435 692 Command c("find");
bobv@2036 693 os::print_location(tty, x, false);
duke@435 694 }
duke@435 695
duke@435 696
duke@435 697 extern "C" void findpc(intptr_t x) {
duke@435 698 Command c("findpc");
bobv@2036 699 os::print_location(tty, x, true);
duke@435 700 }
duke@435 701
duke@435 702
duke@435 703 // int versions of all methods to avoid having to type type casts in the debugger
duke@435 704
duke@435 705 void pp(intptr_t p) { pp((void*)p); }
duke@435 706 void pp(oop p) { pp((void*)p); }
duke@435 707
duke@435 708 void help() {
duke@435 709 Command c("help");
duke@435 710 tty->print_cr("basic");
duke@435 711 tty->print_cr(" pp(void* p) - try to make sense of p");
duke@435 712 tty->print_cr(" pv(intptr_t p)- ((PrintableResourceObj*) p)->print()");
duke@435 713 tty->print_cr(" ps() - print current thread stack");
duke@435 714 tty->print_cr(" pss() - print all thread stacks");
duke@435 715 tty->print_cr(" pm(int pc) - print methodOop given compiled PC");
duke@435 716 tty->print_cr(" findm(intptr_t pc) - finds methodOop");
duke@435 717 tty->print_cr(" find(intptr_t x) - finds & prints nmethod/stub/bytecode/oop based on pointer into it");
duke@435 718
duke@435 719 tty->print_cr("misc.");
duke@435 720 tty->print_cr(" flush() - flushes the log file");
duke@435 721 tty->print_cr(" events() - dump last 50 events");
duke@435 722
duke@435 723
duke@435 724 tty->print_cr("compiler debugging");
duke@435 725 tty->print_cr(" debug() - to set things up for compiler debugging");
duke@435 726 tty->print_cr(" ndebug() - undo debug");
duke@435 727 }
duke@435 728
duke@435 729 #if 0
duke@435 730
duke@435 731 // BobV's command parser for debugging on windows when nothing else works.
duke@435 732
duke@435 733 enum CommandID {
duke@435 734 CMDID_HELP,
duke@435 735 CMDID_QUIT,
duke@435 736 CMDID_HSFIND,
duke@435 737 CMDID_PSS,
duke@435 738 CMDID_PS,
duke@435 739 CMDID_PSF,
duke@435 740 CMDID_FINDM,
duke@435 741 CMDID_FINDNM,
duke@435 742 CMDID_PP,
duke@435 743 CMDID_BPT,
duke@435 744 CMDID_EXIT,
duke@435 745 CMDID_VERIFY,
duke@435 746 CMDID_THREADS,
duke@435 747 CMDID_ILLEGAL = 99
duke@435 748 };
duke@435 749
duke@435 750 struct CommandParser {
duke@435 751 char *name;
duke@435 752 CommandID code;
duke@435 753 char *description;
duke@435 754 };
duke@435 755
duke@435 756 struct CommandParser CommandList[] = {
duke@435 757 (char *)"help", CMDID_HELP, " Dump this list",
duke@435 758 (char *)"quit", CMDID_QUIT, " Return from this routine",
duke@435 759 (char *)"hsfind", CMDID_HSFIND, "Perform an hsfind on an address",
duke@435 760 (char *)"ps", CMDID_PS, " Print Current Thread Stack Trace",
duke@435 761 (char *)"pss", CMDID_PSS, " Print All Thread Stack Trace",
duke@435 762 (char *)"psf", CMDID_PSF, " Print All Stack Frames",
duke@435 763 (char *)"findm", CMDID_FINDM, " Find a methodOop from a PC",
duke@435 764 (char *)"findnm", CMDID_FINDNM, "Find an nmethod from a PC",
duke@435 765 (char *)"pp", CMDID_PP, " Find out something about a pointer",
duke@435 766 (char *)"break", CMDID_BPT, " Execute a breakpoint",
duke@435 767 (char *)"exitvm", CMDID_EXIT, "Exit the VM",
duke@435 768 (char *)"verify", CMDID_VERIFY, "Perform a Heap Verify",
duke@435 769 (char *)"thread", CMDID_THREADS, "Dump Info on all Threads",
duke@435 770 (char *)0, CMDID_ILLEGAL
duke@435 771 };
duke@435 772
duke@435 773
duke@435 774 // get_debug_command()
duke@435 775 //
duke@435 776 // Read a command from standard input.
duke@435 777 // This is useful when you have a debugger
duke@435 778 // which doesn't support calling into functions.
duke@435 779 //
duke@435 780 void get_debug_command()
duke@435 781 {
duke@435 782 ssize_t count;
duke@435 783 int i,j;
duke@435 784 bool gotcommand;
duke@435 785 intptr_t addr;
duke@435 786 char buffer[256];
duke@435 787 nmethod *nm;
duke@435 788 methodOop m;
duke@435 789
duke@435 790 tty->print_cr("You have entered the diagnostic command interpreter");
duke@435 791 tty->print("The supported commands are:\n");
duke@435 792 for ( i=0; ; i++ ) {
duke@435 793 if ( CommandList[i].code == CMDID_ILLEGAL )
duke@435 794 break;
duke@435 795 tty->print_cr(" %s \n", CommandList[i].name );
duke@435 796 }
duke@435 797
duke@435 798 while ( 1 ) {
duke@435 799 gotcommand = false;
duke@435 800 tty->print("Please enter a command: ");
duke@435 801 count = scanf("%s", buffer) ;
duke@435 802 if ( count >=0 ) {
duke@435 803 for ( i=0; ; i++ ) {
duke@435 804 if ( CommandList[i].code == CMDID_ILLEGAL ) {
duke@435 805 if (!gotcommand) tty->print("Invalid command, please try again\n");
duke@435 806 break;
duke@435 807 }
duke@435 808 if ( strcmp(buffer, CommandList[i].name) == 0 ) {
duke@435 809 gotcommand = true;
duke@435 810 switch ( CommandList[i].code ) {
duke@435 811 case CMDID_PS:
duke@435 812 ps();
duke@435 813 break;
duke@435 814 case CMDID_PSS:
duke@435 815 pss();
duke@435 816 break;
duke@435 817 case CMDID_PSF:
duke@435 818 psf();
duke@435 819 break;
duke@435 820 case CMDID_FINDM:
duke@435 821 tty->print("Please enter the hex addr to pass to findm: ");
duke@435 822 scanf("%I64X", &addr);
duke@435 823 m = (methodOop)findm(addr);
duke@435 824 tty->print("findm(0x%I64X) returned 0x%I64X\n", addr, m);
duke@435 825 break;
duke@435 826 case CMDID_FINDNM:
duke@435 827 tty->print("Please enter the hex addr to pass to findnm: ");
duke@435 828 scanf("%I64X", &addr);
duke@435 829 nm = (nmethod*)findnm(addr);
duke@435 830 tty->print("findnm(0x%I64X) returned 0x%I64X\n", addr, nm);
duke@435 831 break;
duke@435 832 case CMDID_PP:
duke@435 833 tty->print("Please enter the hex addr to pass to pp: ");
duke@435 834 scanf("%I64X", &addr);
duke@435 835 pp((void*)addr);
duke@435 836 break;
duke@435 837 case CMDID_EXIT:
duke@435 838 exit(0);
duke@435 839 case CMDID_HELP:
duke@435 840 tty->print("Here are the supported commands: ");
duke@435 841 for ( j=0; ; j++ ) {
duke@435 842 if ( CommandList[j].code == CMDID_ILLEGAL )
duke@435 843 break;
duke@435 844 tty->print_cr(" %s -- %s\n", CommandList[j].name,
duke@435 845 CommandList[j].description );
duke@435 846 }
duke@435 847 break;
duke@435 848 case CMDID_QUIT:
duke@435 849 return;
duke@435 850 break;
duke@435 851 case CMDID_BPT:
duke@435 852 BREAKPOINT;
duke@435 853 break;
duke@435 854 case CMDID_VERIFY:
duke@435 855 verify();;
duke@435 856 break;
duke@435 857 case CMDID_THREADS:
duke@435 858 threads();;
duke@435 859 break;
duke@435 860 case CMDID_HSFIND:
duke@435 861 tty->print("Please enter the hex addr to pass to hsfind: ");
duke@435 862 scanf("%I64X", &addr);
duke@435 863 tty->print("Calling hsfind(0x%I64X)\n", addr);
duke@435 864 hsfind(addr);
duke@435 865 break;
duke@435 866 default:
duke@435 867 case CMDID_ILLEGAL:
duke@435 868 break;
duke@435 869 }
duke@435 870 }
duke@435 871 }
duke@435 872 }
duke@435 873 }
duke@435 874 }
duke@435 875 #endif
duke@435 876
duke@435 877 #endif // PRODUCT

mercurial