Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/utilities/debug.cpp
32285 views
/*1* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/systemDictionary.hpp"26#include "code/codeCache.hpp"27#include "code/icBuffer.hpp"28#include "code/nmethod.hpp"29#include "code/vtableStubs.hpp"30#include "compiler/compileBroker.hpp"31#include "compiler/disassembler.hpp"32#include "gc_implementation/shared/markSweep.hpp"33#include "gc_interface/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/frame.hpp"42#include "runtime/java.hpp"43#include "runtime/sharedRuntime.hpp"44#include "runtime/stubCodeGenerator.hpp"45#include "runtime/stubRoutines.hpp"46#include "runtime/thread.inline.hpp"47#include "runtime/vframe.hpp"48#include "services/heapDumper.hpp"49#include "utilities/defaultStream.hpp"50#include "utilities/events.hpp"51#include "utilities/top.hpp"52#include "utilities/vmError.hpp"53#ifdef TARGET_OS_FAMILY_linux54# include "os_linux.inline.hpp"55#endif56#ifdef TARGET_OS_FAMILY_solaris57# include "os_solaris.inline.hpp"58#endif59#ifdef TARGET_OS_FAMILY_windows60# include "os_windows.inline.hpp"61#endif62#ifdef TARGET_OS_FAMILY_bsd63# include "os_bsd.inline.hpp"64#endif6566#ifndef ASSERT67# ifdef _DEBUG68// NOTE: don't turn the lines below into a comment -- if you're getting69// a compile error here, change the settings to define ASSERT70ASSERT should be defined when _DEBUG is defined. It is not intended to be used for debugging71functions that do not slow down the system too much and thus can be left in optimized code.72On the other hand, the code should not be included in a production version.73# endif // _DEBUG74#endif // ASSERT757677#ifdef _DEBUG78# ifndef ASSERT79configuration error: ASSERT must be defined in debug version80# endif // ASSERT81#endif // _DEBUG828384#ifdef PRODUCT85# if -defined _DEBUG || -defined ASSERT86configuration error: ASSERT et al. must not be defined in PRODUCT version87# endif88#endif // PRODUCT8990PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC9192FormatBufferResource::FormatBufferResource(const char * format, ...)93: FormatBufferBase((char*)resource_allocate_bytes(RES_BUFSZ)) {94va_list argp;95va_start(argp, format);96jio_vsnprintf(_buf, RES_BUFSZ, format, argp);97va_end(argp);98}99100ATTRIBUTE_PRINTF(1, 2)101void warning(const char* format, ...) {102if (PrintWarnings) {103FILE* const err = defaultStream::error_stream();104jio_fprintf(err, "%s warning: ", VM_Version::vm_name());105va_list ap;106va_start(ap, format);107vfprintf(err, format, ap);108va_end(ap);109fputc('\n', err);110}111if (BreakAtWarning) BREAKPOINT;112}113114#ifndef PRODUCT115116#define is_token_break(ch) (isspace(ch) || (ch) == ',')117118static const char* last_file_name = NULL;119static int last_line_no = -1;120121// assert/guarantee/... may happen very early during VM initialization.122// Don't rely on anything that is initialized by Threads::create_vm(). For123// example, don't use tty.124bool error_is_suppressed(const char* file_name, int line_no) {125// The following 1-element cache requires that passed-in126// file names are always only constant literals.127if (file_name == last_file_name && line_no == last_line_no) return true;128129int file_name_len = (int)strlen(file_name);130char separator = os::file_separator()[0];131const char* base_name = strrchr(file_name, separator);132if (base_name == NULL)133base_name = file_name;134135// scan the SuppressErrorAt option136const char* cp = SuppressErrorAt;137for (;;) {138const char* sfile;139int sfile_len;140int sline;141bool noisy;142while ((*cp) != '\0' && is_token_break(*cp)) cp++;143if ((*cp) == '\0') break;144sfile = cp;145while ((*cp) != '\0' && !is_token_break(*cp) && (*cp) != ':') cp++;146sfile_len = cp - sfile;147if ((*cp) == ':') cp++;148sline = 0;149while ((*cp) != '\0' && isdigit(*cp)) {150sline *= 10;151sline += (*cp) - '0';152cp++;153}154// "file:line!" means the assert suppression is not silent155noisy = ((*cp) == '!');156while ((*cp) != '\0' && !is_token_break(*cp)) cp++;157// match the line158if (sline != 0) {159if (sline != line_no) continue;160}161// match the file162if (sfile_len > 0) {163const char* look = file_name;164const char* look_max = file_name + file_name_len - sfile_len;165const char* foundp;166bool match = false;167while (!match168&& (foundp = strchr(look, sfile[0])) != NULL169&& foundp <= look_max) {170match = true;171for (int i = 1; i < sfile_len; i++) {172if (sfile[i] != foundp[i]) {173match = false;174break;175}176}177look = foundp + 1;178}179if (!match) continue;180}181// got a match!182if (noisy) {183fdStream out(defaultStream::output_fd());184out.print_raw("[error suppressed at ");185out.print_raw(base_name);186char buf[16];187jio_snprintf(buf, sizeof(buf), ":%d]", line_no);188out.print_raw_cr(buf);189} else {190// update 1-element cache for fast silent matches191last_file_name = file_name;192last_line_no = line_no;193}194return true;195}196197if (!is_error_reported()) {198// print a friendly hint:199fdStream out(defaultStream::output_fd());200out.print_raw_cr("# To suppress the following error report, specify this argument");201out.print_raw ("# after -XX: or in .hotspotrc: SuppressErrorAt=");202out.print_raw (base_name);203char buf[16];204jio_snprintf(buf, sizeof(buf), ":%d", line_no);205out.print_raw_cr(buf);206}207return false;208}209210#undef is_token_break211212#else213214// Place-holder for non-existent suppression check:215#define error_is_suppressed(file_name, line_no) (false)216217#endif // !PRODUCT218219void report_vm_error(const char* file, int line, const char* error_msg,220const char* detail_msg)221{222if (Debugging || error_is_suppressed(file, line)) return;223Thread* const thread = ThreadLocalStorage::get_thread_slow();224VMError err(thread, file, line, error_msg, detail_msg);225err.report_and_die();226}227228void report_fatal(const char* file, int line, const char* message)229{230report_vm_error(file, line, "fatal error", message);231}232233void report_vm_out_of_memory(const char* file, int line, size_t size,234VMErrorType vm_err_type, const char* message) {235if (Debugging) return;236237Thread* thread = ThreadLocalStorage::get_thread_slow();238VMError(thread, file, line, size, vm_err_type, message).report_and_die();239240// The UseOSErrorReporting option in report_and_die() may allow a return241// to here. If so then we'll have to figure out how to handle it.242guarantee(false, "report_and_die() should not return here");243}244245void report_should_not_call(const char* file, int line) {246report_vm_error(file, line, "ShouldNotCall()");247}248249void report_should_not_reach_here(const char* file, int line) {250report_vm_error(file, line, "ShouldNotReachHere()");251}252253void report_unimplemented(const char* file, int line) {254report_vm_error(file, line, "Unimplemented()");255}256257void report_untested(const char* file, int line, const char* message) {258#ifndef PRODUCT259warning("Untested: %s in %s: %d\n", message, file, line);260#endif // !PRODUCT261}262263void report_out_of_shared_space(SharedSpaceType shared_space) {264static const char* name[] = {265"native memory for metadata",266"shared read only space",267"shared read write space",268"shared miscellaneous data space",269"shared miscellaneous code space"270};271static const char* flag[] = {272"Metaspace",273"SharedReadOnlySize",274"SharedReadWriteSize",275"SharedMiscDataSize",276"SharedMiscCodeSize"277};278279warning("\nThe %s is not large enough\n"280"to preload requested classes. Use -XX:%s=<size>\n"281"to increase the initial size of %s.\n",282name[shared_space], flag[shared_space], name[shared_space]);283exit(2);284}285286void report_java_out_of_memory(const char* message) {287static jint out_of_memory_reported = 0;288289// A number of threads may attempt to report OutOfMemoryError at around the290// same time. To avoid dumping the heap or executing the data collection291// commands multiple times we just do it once when the first threads reports292// the error.293if (Atomic::cmpxchg(1, &out_of_memory_reported, 0) == 0) {294// create heap dump before OnOutOfMemoryError commands are executed295if (HeapDumpOnOutOfMemoryError) {296tty->print_cr("java.lang.OutOfMemoryError: %s", message);297HeapDumper::dump_heap_from_oome();298}299300if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {301VMError err(message);302err.report_java_out_of_memory();303}304305if (CrashOnOutOfMemoryError) {306tty->print_cr("Aborting due to java.lang.OutOfMemoryError: %s", message);307fatal(err_msg("OutOfMemory encountered: %s", message));308}309310if (ExitOnOutOfMemoryError) {311tty->print_cr("Terminating due to java.lang.OutOfMemoryError: %s", message);312exit(3);313}314}315}316317void report_insufficient_metaspace(size_t required_size) {318warning("\nThe MaxMetaspaceSize of " SIZE_FORMAT " bytes is not large enough.\n"319"Either don't specify the -XX:MaxMetaspaceSize=<size>\n"320"or increase the size to at least " SIZE_FORMAT ".\n",321MaxMetaspaceSize, required_size);322exit(2);323}324325static bool error_reported = false;326327// call this when the VM is dying--it might loosen some asserts328void set_error_reported() {329error_reported = true;330}331332bool is_error_reported() {333return error_reported;334}335336#ifndef PRODUCT337#include <signal.h>338339void test_error_handler() {340uintx test_num = ErrorHandlerTest;341if (test_num == 0) return;342343// If asserts are disabled, use the corresponding guarantee instead.344size_t n = test_num;345NOT_DEBUG(if (n <= 2) n += 2);346347const char* const str = "hello";348const size_t num = (size_t)os::vm_page_size();349350const char* const eol = os::line_separator();351const char* const msg = "this message should be truncated during formatting";352char * const dataPtr = NULL; // bad data pointer353const void (*funcPtr)(void) = (const void(*)()) 0xF; // bad function pointer354355// Keep this in sync with test/runtime/6888954/vmerrors.sh.356switch (n) {357case 1: assert(str == NULL, "expected null");358case 2: assert(num == 1023 && *str == 'X',359err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));360case 3: guarantee(str == NULL, "expected null");361case 4: guarantee(num == 1023 && *str == 'X',362err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));363case 5: fatal("expected null");364case 6: fatal(err_msg("num=" SIZE_FORMAT " str=\"%s\"", num, str));365case 7: fatal(err_msg("%s%s# %s%s# %s%s# %s%s# %s%s# "366"%s%s# %s%s# %s%s# %s%s# %s%s# "367"%s%s# %s%s# %s%s# %s%s# %s",368msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,369msg, eol, msg, eol, msg, eol, msg, eol, msg, eol,370msg, eol, msg, eol, msg, eol, msg, eol, msg));371case 8: vm_exit_out_of_memory(num, OOM_MALLOC_ERROR, "ChunkPool::allocate");372case 9: ShouldNotCallThis();373case 10: ShouldNotReachHere();374case 11: Unimplemented();375// There's no guarantee the bad data pointer will crash us376// so "break" out to the ShouldNotReachHere().377case 12: *dataPtr = '\0'; break;378// There's no guarantee the bad function pointer will crash us379// so "break" out to the ShouldNotReachHere().380case 13: (*funcPtr)(); break;381382default: tty->print_cr("ERROR: %d: unexpected test_num value.", n);383}384ShouldNotReachHere();385}386#endif // !PRODUCT387388// ------ helper functions for debugging go here ------------389390// All debug entries should be wrapped with a stack allocated391// Command object. It makes sure a resource mark is set and392// flushes the logfile to prevent file sharing problems.393394class Command : public StackObj {395private:396ResourceMark rm;397ResetNoHandleMark rnhm;398HandleMark hm;399bool debug_save;400public:401static int level;402Command(const char* str) {403debug_save = Debugging;404Debugging = true;405if (level++ > 0) return;406tty->cr();407tty->print_cr("\"Executing %s\"", str);408}409410~Command() {411tty->flush();412Debugging = debug_save;413level--;414}415};416417int Command::level = 0;418419#ifndef PRODUCT420421extern "C" void blob(CodeBlob* cb) {422Command c("blob");423cb->print();424}425426427extern "C" void dump_vtable(address p) {428Command c("dump_vtable");429Klass* k = (Klass*)p;430InstanceKlass::cast(k)->vtable()->print();431}432433434extern "C" void nm(intptr_t p) {435// Actually we look through all CodeBlobs (the nm name has been kept for backwards compatability)436Command c("nm");437CodeBlob* cb = CodeCache::find_blob((address)p);438if (cb == NULL) {439tty->print_cr("NULL");440} else {441cb->print();442}443}444445446extern "C" void disnm(intptr_t p) {447Command c("disnm");448CodeBlob* cb = CodeCache::find_blob((address) p);449if (cb != NULL) {450nmethod* nm = cb->as_nmethod_or_null();451if (nm != NULL) {452nm->print();453} else {454cb->print();455}456Disassembler::decode(cb);457}458}459460461extern "C" void printnm(intptr_t p) {462char buffer[256];463sprintf(buffer, "printnm: " INTPTR_FORMAT, p);464Command c(buffer);465CodeBlob* cb = CodeCache::find_blob((address) p);466if (cb->is_nmethod()) {467nmethod* nm = (nmethod*)cb;468nm->print_nmethod(true);469}470}471472473extern "C" void universe() {474Command c("universe");475Universe::print();476}477478479extern "C" void verify() {480// try to run a verify on the entire system481// note: this may not be safe if we're not at a safepoint; for debugging,482// this manipulates the safepoint settings to avoid assertion failures483Command c("universe verify");484bool safe = SafepointSynchronize::is_at_safepoint();485if (!safe) {486tty->print_cr("warning: not at safepoint -- verify may fail");487SafepointSynchronize::set_is_at_safepoint();488}489// Ensure Eden top is correct before verification490Universe::heap()->prepare_for_verify();491Universe::verify();492if (!safe) SafepointSynchronize::set_is_not_at_safepoint();493}494495496extern "C" void pp(void* p) {497Command c("pp");498FlagSetting fl(PrintVMMessages, true);499FlagSetting f2(DisplayVMOutput, true);500if (Universe::heap()->is_in(p)) {501oop obj = oop(p);502obj->print();503} else {504tty->print(PTR_FORMAT, p);505}506}507508509// pv: print vm-printable object510extern "C" void pa(intptr_t p) { ((AllocatedObj*) p)->print(); }511extern "C" void findpc(intptr_t x);512513#endif // !PRODUCT514515extern "C" void ps() { // print stack516if (Thread::current() == NULL) return;517Command c("ps");518519520// Prints the stack of the current Java thread521JavaThread* p = JavaThread::active();522tty->print(" for thread: ");523p->print();524tty->cr();525526if (p->has_last_Java_frame()) {527// If the last_Java_fp is set we are in C land and528// can call the standard stack_trace function.529#ifdef PRODUCT530p->print_stack();531} else {532tty->print_cr("Cannot find the last Java frame, printing stack disabled.");533#else // !PRODUCT534p->trace_stack();535} else {536frame f = os::current_frame();537RegisterMap reg_map(p);538f = f.sender(®_map);539tty->print("(guessing starting frame id=%#p based on current fp)\n", f.id());540p->trace_stack_from(vframe::new_vframe(&f, ®_map, p));541pd_ps(f);542#endif // PRODUCT543}544545}546547extern "C" void pfl() {548// print frame layout549Command c("pfl");550JavaThread* p = JavaThread::active();551tty->print(" for thread: ");552p->print();553tty->cr();554if (p->has_last_Java_frame()) {555p->print_frame_layout();556}557}558559#ifndef PRODUCT560561extern "C" void psf() { // print stack frames562{563Command c("psf");564JavaThread* p = JavaThread::active();565tty->print(" for thread: ");566p->print();567tty->cr();568if (p->has_last_Java_frame()) {569p->trace_frames();570}571}572}573574575extern "C" void threads() {576Command c("threads");577Threads::print(false, true);578}579580581extern "C" void psd() {582Command c("psd");583SystemDictionary::print();584}585586587extern "C" void safepoints() {588Command c("safepoints");589SafepointSynchronize::print_state();590}591592#endif // !PRODUCT593594extern "C" void pss() { // print all stacks595if (Thread::current() == NULL) return;596Command c("pss");597Threads::print(true, PRODUCT_ONLY(false) NOT_PRODUCT(true));598}599600#ifndef PRODUCT601602extern "C" void debug() { // to set things up for compiler debugging603Command c("debug");604WizardMode = true;605PrintVMMessages = PrintCompilation = true;606PrintInlining = PrintAssembly = true;607tty->flush();608}609610611extern "C" void ndebug() { // undo debug()612Command c("ndebug");613PrintCompilation = false;614PrintInlining = PrintAssembly = false;615tty->flush();616}617618619extern "C" void flush() {620Command c("flush");621tty->flush();622}623624extern "C" void events() {625Command c("events");626Events::print();627}628629extern "C" Method* findm(intptr_t pc) {630Command c("findm");631nmethod* nm = CodeCache::find_nmethod((address)pc);632return (nm == NULL) ? (Method*)NULL : nm->method();633}634635636extern "C" nmethod* findnm(intptr_t addr) {637Command c("findnm");638return CodeCache::find_nmethod((address)addr);639}640641// Another interface that isn't ambiguous in dbx.642// Can we someday rename the other find to hsfind?643extern "C" void hsfind(intptr_t x) {644Command c("hsfind");645os::print_location(tty, x, false);646}647648649extern "C" void find(intptr_t x) {650Command c("find");651os::print_location(tty, x, false);652}653654655extern "C" void findpc(intptr_t x) {656Command c("findpc");657os::print_location(tty, x, true);658}659660661// Need method pointer to find bcp, when not in permgen.662extern "C" void findbcp(intptr_t method, intptr_t bcp) {663Command c("findbcp");664Method* mh = (Method*)method;665if (!mh->is_native()) {666tty->print_cr("bci_from(%p) = %d; print_codes():",667mh, mh->bci_from(address(bcp)));668mh->print_codes_on(tty);669}670}671672// int versions of all methods to avoid having to type type casts in the debugger673674void pp(intptr_t p) { pp((void*)p); }675void pp(oop p) { pp((void*)p); }676677void help() {678Command c("help");679tty->print_cr("basic");680tty->print_cr(" pp(void* p) - try to make sense of p");681tty->print_cr(" pv(intptr_t p)- ((PrintableResourceObj*) p)->print()");682tty->print_cr(" ps() - print current thread stack");683tty->print_cr(" pss() - print all thread stacks");684tty->print_cr(" pm(int pc) - print Method* given compiled PC");685tty->print_cr(" findm(intptr_t pc) - finds Method*");686tty->print_cr(" find(intptr_t x) - finds & prints nmethod/stub/bytecode/oop based on pointer into it");687tty->print_cr(" pns(void* sp, void* fp, void* pc) - print native (i.e. mixed) stack trace. E.g.");688tty->print_cr(" pns($sp, $rbp, $pc) on Linux/amd64 and Solaris/amd64 or");689tty->print_cr(" pns($sp, $ebp, $pc) on Linux/x86 or");690tty->print_cr(" pns($sp, $fp, $pc) on Linux/AArch64 or");691tty->print_cr(" pns($sp, 0, $pc) on Linux/ppc64 or");692tty->print_cr(" pns($sp + 0x7ff, 0, $pc) on Solaris/SPARC");693tty->print_cr(" - in gdb do 'set overload-resolution off' before calling pns()");694tty->print_cr(" - in dbx do 'frame 1' before calling pns()");695696tty->print_cr("misc.");697tty->print_cr(" flush() - flushes the log file");698tty->print_cr(" events() - dump events from ring buffers");699700701tty->print_cr("compiler debugging");702tty->print_cr(" debug() - to set things up for compiler debugging");703tty->print_cr(" ndebug() - undo debug");704}705706#endif // !PRODUCT707708void print_native_stack(outputStream* st, frame fr, Thread* t, char* buf, int buf_size) {709710// see if it's a valid frame711if (fr.pc()) {712st->print_cr("Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)");713714int count = 0;715while (count++ < StackPrintLimit) {716fr.print_on_error(st, buf, buf_size);717st->cr();718// Compiled code may use EBP register on x86 so it looks like719// non-walkable C frame. Use frame.sender() for java frames.720if (t && t->is_Java_thread()) {721// Catch very first native frame by using stack address.722// For JavaThread stack_base and stack_size should be set.723if (!t->on_local_stack((address)(fr.real_fp() + 1))) {724break;725}726if (fr.is_java_frame() || fr.is_native_frame() || fr.is_runtime_frame()) {727RegisterMap map((JavaThread*)t, false); // No update728fr = fr.sender(&map);729} else {730fr = os::get_sender_for_C_frame(&fr);731}732} else {733// is_first_C_frame() does only simple checks for frame pointer,734// it will pass if java compiled code has a pointer in EBP.735if (os::is_first_C_frame(&fr)) break;736fr = os::get_sender_for_C_frame(&fr);737}738}739740if (count > StackPrintLimit) {741st->print_cr("...<more frames>...");742}743744st->cr();745}746}747748#ifndef PRODUCT749750extern "C" void pns(void* sp, void* fp, void* pc) { // print native stack751Command c("pns");752static char buf[O_BUFLEN];753Thread* t = ThreadLocalStorage::get_thread_slow();754// Call generic frame constructor (certain arguments may be ignored)755frame fr(sp, fp, pc);756print_native_stack(tty, fr, t, buf, sizeof(buf));757}758759#endif // !PRODUCT760761762