diff --git a/src/share/vm/classfile/vmSymbols.hpp b/src/share/vm/classfile/vmSymbols.hpp index 1e66346eec5111f4dea340ef03a6cfc5318d35b9..428b2940f61c4c0244a6b1ad70405791d19e8225 100644 --- a/src/share/vm/classfile/vmSymbols.hpp +++ b/src/share/vm/classfile/vmSymbols.hpp @@ -517,13 +517,18 @@ template(sun_management_ManagementFactory, "sun/management/ManagementFactory") \ template(sun_management_Sensor, "sun/management/Sensor") \ template(sun_management_Agent, "sun/management/Agent") \ + template(sun_management_DiagnosticCommandImpl, "sun/management/DiagnosticCommandImpl") \ template(sun_management_GarbageCollectorImpl, "sun/management/GarbageCollectorImpl") \ + template(sun_management_ManagementFactoryHelper, "sun/management/ManagementFactoryHelper") \ + template(getDiagnosticCommandMBean_name, "getDiagnosticCommandMBean") \ + template(getDiagnosticCommandMBean_signature, "()Lcom/sun/management/DiagnosticCommandMBean;") \ template(getGcInfoBuilder_name, "getGcInfoBuilder") \ template(getGcInfoBuilder_signature, "()Lsun/management/GcInfoBuilder;") \ template(com_sun_management_GcInfo, "com/sun/management/GcInfo") \ template(com_sun_management_GcInfo_constructor_signature, "(Lsun/management/GcInfoBuilder;JJJ[Ljava/lang/management/MemoryUsage;[Ljava/lang/management/MemoryUsage;[Ljava/lang/Object;)V") \ template(createGCNotification_name, "createGCNotification") \ template(createGCNotification_signature, "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Lcom/sun/management/GcInfo;)V") \ + template(createDiagnosticFrameworkNotification_name, "createDiagnosticFrameworkNotification") \ template(createMemoryPoolMBean_name, "createMemoryPoolMBean") \ template(createMemoryManagerMBean_name, "createMemoryManagerMBean") \ template(createGarbageCollectorMBean_name, "createGarbageCollectorMBean") \ diff --git a/src/share/vm/runtime/serviceThread.cpp b/src/share/vm/runtime/serviceThread.cpp index 3c121e962194b228be0c7f4806aa15ba62980e1b..66d4572522fd0778b8996497b11c66e9ad679298 100644 --- a/src/share/vm/runtime/serviceThread.cpp +++ b/src/share/vm/runtime/serviceThread.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,6 +29,8 @@ #include "runtime/mutexLocker.hpp" #include "prims/jvmtiImpl.hpp" #include "services/gcNotifier.hpp" +#include "services/diagnosticArgument.hpp" +#include "services/diagnosticFramework.hpp" ServiceThread* ServiceThread::_instance = NULL; @@ -83,6 +85,7 @@ void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) { bool sensors_changed = false; bool has_jvmti_events = false; bool has_gc_notification_event = false; + bool has_dcmd_notification_event = false; JvmtiDeferredEvent jvmti_event; { // Need state transition ThreadBlockInVM so that this thread @@ -98,7 +101,8 @@ void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) { MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); while (!(sensors_changed = LowMemoryDetector::has_pending_requests()) && !(has_jvmti_events = JvmtiDeferredEventQueue::has_events()) && - !(has_gc_notification_event = GCNotifier::has_event())) { + !(has_gc_notification_event = GCNotifier::has_event()) && + !(has_dcmd_notification_event = DCmdFactory::has_pending_jmx_notification())) { // wait until one of the sensors has pending requests, or there is a // pending JVMTI event or JMX GC notification to post Service_lock->wait(Mutex::_no_safepoint_check_flag); @@ -120,6 +124,10 @@ void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) { if(has_gc_notification_event) { GCNotifier::sendNotification(CHECK); } + + if(has_dcmd_notification_event) { + DCmdFactory::send_notification(CHECK); + } } } diff --git a/src/share/vm/services/attachListener.cpp b/src/share/vm/services/attachListener.cpp index da93859bb94ddd9a0bcddf88fde1fb26751e9a13..564b20f4b3ba6c7c6ffeb77c8d4fa0490a3c9868 100644 --- a/src/share/vm/services/attachListener.cpp +++ b/src/share/vm/services/attachListener.cpp @@ -157,7 +157,7 @@ static jint jcmd(AttachOperation* op, outputStream* out) { Thread* THREAD = Thread::current(); // All the supplied jcmd arguments are stored as a single // string (op->arg(0)). This is parsed by the Dcmd framework. - DCmd::parse_and_execute(out, op->arg(0), ' ', THREAD); + DCmd::parse_and_execute(DCmd_Source_AttachAPI, out, op->arg(0), ' ', THREAD); if (HAS_PENDING_EXCEPTION) { java_lang_Throwable::print(PENDING_EXCEPTION, out); out->cr(); diff --git a/src/share/vm/services/diagnosticCommand.cpp b/src/share/vm/services/diagnosticCommand.cpp index 5499d9ad2fe065b1e8b742005f3aab64b203e8d6..5deaae0d416f42355d3abeb0385869b592046e37 100644 --- a/src/share/vm/services/diagnosticCommand.cpp +++ b/src/share/vm/services/diagnosticCommand.cpp @@ -34,26 +34,33 @@ void DCmdRegistrant::register_dcmds(){ // Registration of the diagnostic commands - // First boolean argument specifies if the command is enabled - // Second boolean argument specifies if the command is hidden - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); + // First argument specifies which interfaces will export the command + // Second argument specifies if the command is enabled + // Third argument specifies if the command is hidden + uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI + | DCmd_Source_MBean; + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); #if INCLUDE_SERVICES // Heap dumping/inspection supported - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); #endif // INCLUDE_SERVICES - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true, false)); - //Enhanced JMX Agent Support - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true,false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true,false)); - DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(true,false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(full_export, true, false)); + + // Enhanced JMX Agent Support + // These commands won't be exported via the DiagnosticCommandMBean until an + // appropriate permission is created for them + uint32_t jmx_agent_export_flags = DCmd_Source_Internal | DCmd_Source_AttachAPI; + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(jmx_agent_export_flags, true,false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(jmx_agent_export_flags, true,false)); + DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl(jmx_agent_export_flags, true,false)); } @@ -72,29 +79,37 @@ HelpDCmd::HelpDCmd(outputStream* output, bool heap) : DCmdWithParser(output, hea _dcmdparser.add_dcmd_argument(&_cmd); }; -void HelpDCmd::execute(TRAPS) { +void HelpDCmd::execute(DCmdSource source, TRAPS) { if (_all.value()) { - GrowableArray* cmd_list = DCmdFactory::DCmd_list(); + GrowableArray* cmd_list = DCmdFactory::DCmd_list(source); for (int i = 0; i < cmd_list->length(); i++) { - DCmdFactory* factory = DCmdFactory::factory(cmd_list->at(i), + DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i), strlen(cmd_list->at(i))); - if (!factory->is_hidden()) { - output()->print_cr("%s%s", factory->name(), - factory->is_enabled() ? "" : " [disabled]"); - output()->print_cr("\t%s", factory->description()); - output()->cr(); - } + output()->print_cr("%s%s", factory->name(), + factory->is_enabled() ? "" : " [disabled]"); + output()->print_cr("\t%s", factory->description()); + output()->cr(); factory = factory->next(); } } else if (_cmd.has_value()) { DCmd* cmd = NULL; - DCmdFactory* factory = DCmdFactory::factory(_cmd.value(), + DCmdFactory* factory = DCmdFactory::factory(source, _cmd.value(), strlen(_cmd.value())); if (factory != NULL) { output()->print_cr("%s%s", factory->name(), factory->is_enabled() ? "" : " [disabled]"); output()->print_cr(factory->description()); output()->print_cr("\nImpact: %s", factory->impact()); + JavaPermission p = factory->permission(); + if(p._class != NULL) { + if(p._action != NULL) { + output()->print_cr("\nPermission: %s(%s, %s)", + p._class, p._name == NULL ? "null" : p._name, p._action); + } else { + output()->print_cr("\nPermission: %s(%s)", + p._class, p._name == NULL ? "null" : p._name); + } + } output()->cr(); cmd = factory->create_resource_instance(output()); if (cmd != NULL) { @@ -106,14 +121,12 @@ void HelpDCmd::execute(TRAPS) { } } else { output()->print_cr("The following commands are available:"); - GrowableArray* cmd_list = DCmdFactory::DCmd_list(); + GrowableArray* cmd_list = DCmdFactory::DCmd_list(source); for (int i = 0; i < cmd_list->length(); i++) { - DCmdFactory* factory = DCmdFactory::factory(cmd_list->at(i), + DCmdFactory* factory = DCmdFactory::factory(source, cmd_list->at(i), strlen(cmd_list->at(i))); - if (!factory->is_hidden()) { - output()->print_cr("%s%s", factory->name(), - factory->is_enabled() ? "" : " [disabled]"); - } + output()->print_cr("%s%s", factory->name(), + factory->is_enabled() ? "" : " [disabled]"); factory = factory->_next; } output()->print_cr("\nFor more information about a specific command use 'help '."); @@ -131,7 +144,7 @@ int HelpDCmd::num_arguments() { } } -void VersionDCmd::execute(TRAPS) { +void VersionDCmd::execute(DCmdSource source, TRAPS) { output()->print_cr("%s version %s", Abstract_VM_Version::vm_name(), Abstract_VM_Version::vm_release()); JDK_Version jdk_version = JDK_Version::current(); @@ -150,7 +163,7 @@ PrintVMFlagsDCmd::PrintVMFlagsDCmd(outputStream* output, bool heap) : _dcmdparser.add_dcmd_option(&_all); } -void PrintVMFlagsDCmd::execute(TRAPS) { +void PrintVMFlagsDCmd::execute(DCmdSource source, TRAPS) { if (_all.value()) { CommandLineFlags::printFlags(output(), true); } else { @@ -169,7 +182,7 @@ int PrintVMFlagsDCmd::num_arguments() { } } -void PrintSystemPropertiesDCmd::execute(TRAPS) { +void PrintSystemPropertiesDCmd::execute(DCmdSource source, TRAPS) { // load sun.misc.VMSupport Symbol* klass = vmSymbols::sun_misc_VMSupport(); Klass* k = SystemDictionary::resolve_or_fail(klass, true, CHECK); @@ -219,7 +232,7 @@ VMUptimeDCmd::VMUptimeDCmd(outputStream* output, bool heap) : _dcmdparser.add_dcmd_option(&_date); } -void VMUptimeDCmd::execute(TRAPS) { +void VMUptimeDCmd::execute(DCmdSource source, TRAPS) { if (_date.value()) { output()->date_stamp(true, "", ": "); } @@ -239,11 +252,15 @@ int VMUptimeDCmd::num_arguments() { } } -void SystemGCDCmd::execute(TRAPS) { - Universe::heap()->collect(GCCause::_java_lang_system_gc); +void SystemGCDCmd::execute(DCmdSource source, TRAPS) { + if (!DisableExplicitGC) { + Universe::heap()->collect(GCCause::_java_lang_system_gc); + } else { + output()->print_cr("Explicit GC is disabled, no GC has been performed."); + } } -void RunFinalizationDCmd::execute(TRAPS) { +void RunFinalizationDCmd::execute(DCmdSource source, TRAPS) { Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK); instanceKlassHandle klass(THREAD, k); @@ -263,7 +280,7 @@ HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) : _dcmdparser.add_dcmd_argument(&_filename); } -void HeapDumpDCmd::execute(TRAPS) { +void HeapDumpDCmd::execute(DCmdSource source, TRAPS) { // Request a full GC before heap dump if _all is false // This helps reduces the amount of unreachable objects in the dump // and makes it easier to browse. @@ -301,7 +318,7 @@ ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) : _dcmdparser.add_dcmd_option(&_all); } -void ClassHistogramDCmd::execute(TRAPS) { +void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) { VM_GC_HeapInspection heapop(output(), !_all.value() /* request full gc if false */, true /* need_prologue */); @@ -337,7 +354,7 @@ ClassStatsDCmd::ClassStatsDCmd(outputStream* output, bool heap) : _dcmdparser.add_dcmd_argument(&_columns); } -void ClassStatsDCmd::execute(TRAPS) { +void ClassStatsDCmd::execute(DCmdSource source, TRAPS) { if (!UnlockDiagnosticVMOptions) { output()->print_cr("GC.class_stats command requires -XX:+UnlockDiagnosticVMOptions"); return; @@ -384,7 +401,7 @@ ThreadDumpDCmd::ThreadDumpDCmd(outputStream* output, bool heap) : _dcmdparser.add_dcmd_option(&_locks); } -void ThreadDumpDCmd::execute(TRAPS) { +void ThreadDumpDCmd::execute(DCmdSource source, TRAPS) { // thread stacks VM_PrintThreads op1(output(), _locks.value()); VMThread::execute(&op1); @@ -526,7 +543,8 @@ int JMXStartRemoteDCmd::num_arguments() { } } -void JMXStartRemoteDCmd::execute(TRAPS) { + +void JMXStartRemoteDCmd::execute(DCmdSource source, TRAPS) { ResourceMark rm(THREAD); HandleMark hm(THREAD); @@ -593,7 +611,7 @@ JMXStartLocalDCmd::JMXStartLocalDCmd(outputStream *output, bool heap_allocated) // do nothing } -void JMXStartLocalDCmd::execute(TRAPS) { +void JMXStartLocalDCmd::execute(DCmdSource source, TRAPS) { ResourceMark rm(THREAD); HandleMark hm(THREAD); @@ -611,7 +629,7 @@ void JMXStartLocalDCmd::execute(TRAPS) { } -void JMXStopRemoteDCmd::execute(TRAPS) { +void JMXStopRemoteDCmd::execute(DCmdSource source, TRAPS) { ResourceMark rm(THREAD); HandleMark hm(THREAD); diff --git a/src/share/vm/services/diagnosticCommand.hpp b/src/share/vm/services/diagnosticCommand.hpp index ac5d5809d1e0cabc076ca58431d476241d26163a..9c7216177bf7d9274d59826ee1a35d1cc62d614d 100644 --- a/src/share/vm/services/diagnosticCommand.hpp +++ b/src/share/vm/services/diagnosticCommand.hpp @@ -51,7 +51,7 @@ public: } static const char* impact() { return "Low"; } static int num_arguments(); - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; class VersionDCmd : public DCmd { @@ -62,8 +62,13 @@ public: return "Print JVM version information."; } static const char* impact() { return "Low"; } + static const JavaPermission permission() { + JavaPermission p = {"java.util.PropertyPermission", + "java.vm.version", "read"}; + return p; + } static int num_arguments() { return 0; } - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; class CommandLineDCmd : public DCmd { @@ -74,8 +79,13 @@ public: return "Print the command line used to start this VM instance."; } static const char* impact() { return "Low"; } + static const JavaPermission permission() { + JavaPermission p = {"java.lang.management.ManagementPermission", + "monitor", NULL}; + return p; + } static int num_arguments() { return 0; } - virtual void execute(TRAPS) { + virtual void execute(DCmdSource source, TRAPS) { Arguments::print_on(_output); } }; @@ -91,8 +101,13 @@ public: static const char* impact() { return "Low"; } + static const JavaPermission permission() { + JavaPermission p = {"java.util.PropertyPermission", + "*", "read"}; + return p; + } static int num_arguments() { return 0; } - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; // See also: print_flag in attachListener.cpp @@ -108,8 +123,13 @@ public: static const char* impact() { return "Low"; } + static const JavaPermission permission() { + JavaPermission p = {"java.lang.management.ManagementPermission", + "monitor", NULL}; + return p; + } static int num_arguments(); - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; class VMUptimeDCmd : public DCmdWithParser { @@ -125,7 +145,7 @@ public: return "Low"; } static int num_arguments(); - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; class SystemGCDCmd : public DCmd { @@ -139,7 +159,7 @@ public: return "Medium: Depends on Java heap size and content."; } static int num_arguments() { return 0; } - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; class RunFinalizationDCmd : public DCmd { @@ -153,7 +173,7 @@ public: return "Medium: Depends on Java content."; } static int num_arguments() { return 0; } - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; #if INCLUDE_SERVICES // Heap dumping supported @@ -174,8 +194,13 @@ public: return "High: Depends on Java heap size and content. " "Request a full GC unless the '-all' option is specified."; } + static const JavaPermission permission() { + JavaPermission p = {"java.lang.management.ManagementPermission", + "monitor", NULL}; + return p; + } static int num_arguments(); - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; #endif // INCLUDE_SERVICES @@ -194,8 +219,13 @@ public: static const char* impact() { return "High: Depends on Java heap size and content."; } + static const JavaPermission permission() { + JavaPermission p = {"java.lang.management.ManagementPermission", + "monitor", NULL}; + return p; + } static int num_arguments(); - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; class ClassStatsDCmd : public DCmdWithParser { @@ -216,7 +246,7 @@ public: return "High: Depends on Java heap size and content."; } static int num_arguments(); - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; // See also: thread_dump in attachListener.cpp @@ -232,8 +262,13 @@ public: static const char* impact() { return "Medium: Depends on the number of threads."; } + static const JavaPermission permission() { + JavaPermission p = {"java.lang.management.ManagementPermission", + "monitor", NULL}; + return p; + } static int num_arguments(); - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; // Enhanced JMX Agent support @@ -281,7 +316,7 @@ public: static int num_arguments(); - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; @@ -302,7 +337,7 @@ public: return "Start local management agent."; } - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; @@ -321,7 +356,7 @@ public: return "Stop remote management agent."; } - virtual void execute(TRAPS); + virtual void execute(DCmdSource source, TRAPS); }; #endif // SHARE_VM_SERVICES_DIAGNOSTICCOMMAND_HPP diff --git a/src/share/vm/services/diagnosticFramework.cpp b/src/share/vm/services/diagnosticFramework.cpp index 2ae7866f6c631e43142df22e95ceeb2a9a425127..dcc2a21e48a4bc94ff7e3b7d00d4562049c7fee9 100644 --- a/src/share/vm/services/diagnosticFramework.cpp +++ b/src/share/vm/services/diagnosticFramework.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -359,7 +359,7 @@ GrowableArray* DCmdParser::argument_info_array() { while (arg != NULL) { array->append(new DCmdArgumentInfo(arg->name(), arg->description(), arg->type(), arg->default_string(), arg->is_mandatory(), - false, idx)); + false, arg->allow_multiple(), idx)); idx++; arg = arg->next(); } @@ -367,32 +367,42 @@ GrowableArray* DCmdParser::argument_info_array() { while (arg != NULL) { array->append(new DCmdArgumentInfo(arg->name(), arg->description(), arg->type(), arg->default_string(), arg->is_mandatory(), - true)); + true, arg->allow_multiple())); arg = arg->next(); } return array; } DCmdFactory* DCmdFactory::_DCmdFactoryList = NULL; +bool DCmdFactory::_has_pending_jmx_notification = false; -void DCmd::parse_and_execute(outputStream* out, const char* cmdline, - char delim, TRAPS) { +void DCmd::parse_and_execute(DCmdSource source, outputStream* out, + const char* cmdline, char delim, TRAPS) { if (cmdline == NULL) return; // Nothing to do! DCmdIter iter(cmdline, '\n'); + int count = 0; while (iter.has_next()) { + if(source == DCmd_Source_MBean && count > 0) { + // When diagnostic commands are invoked via JMX, each command line + // must contains one and only one command because of the Permission + // checks performed by the DiagnosticCommandMBean + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), + "Invalid syntax"); + } CmdLine line = iter.next(); if (line.is_stop()) { break; } if (line.is_executable()) { - DCmd* command = DCmdFactory::create_local_DCmd(line, out, CHECK); + DCmd* command = DCmdFactory::create_local_DCmd(source, line, out, CHECK); assert(command != NULL, "command error must be handled before this line"); DCmdMark mark(command); command->parse(&line, delim, CHECK); - command->execute(CHECK); + command->execute(source, CHECK); } + count++; } } @@ -420,15 +430,78 @@ GrowableArray* DCmdWithParser::argument_info_array() { return _dcmdparser.argument_info_array(); } +void DCmdFactory::push_jmx_notification_request() { + MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); + _has_pending_jmx_notification = true; + Service_lock->notify_all(); +} + +void DCmdFactory::send_notification(TRAPS) { + DCmdFactory::send_notification_internal(THREAD); + // Clearing pending exception to avoid premature termination of + // the service thread + if (HAS_PENDING_EXCEPTION) { + CLEAR_PENDING_EXCEPTION; + } +} +void DCmdFactory::send_notification_internal(TRAPS) { + ResourceMark rm(THREAD); + HandleMark hm(THREAD); + bool notif = false; + { + MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); + notif = _has_pending_jmx_notification; + _has_pending_jmx_notification = false; + } + if (notif) { + + Klass* k = Management::sun_management_ManagementFactoryHelper_klass(CHECK); + instanceKlassHandle mgmt_factory_helper_klass(THREAD, k); + + JavaValue result(T_OBJECT); + JavaCalls::call_static(&result, + mgmt_factory_helper_klass, + vmSymbols::getDiagnosticCommandMBean_name(), + vmSymbols::getDiagnosticCommandMBean_signature(), + CHECK); + + instanceOop m = (instanceOop) result.get_jobject(); + instanceHandle dcmd_mbean_h(THREAD, m); + + Klass* k2 = Management::sun_management_DiagnosticCommandImpl_klass(CHECK); + instanceKlassHandle dcmd_mbean_klass(THREAD, k2); + + if (!dcmd_mbean_h->is_a(k2)) { + THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), + "ManagementFactory.getDiagnosticCommandMBean didn't return a DiagnosticCommandMBean instance"); + } + + JavaValue result2(T_VOID); + JavaCallArguments args2(dcmd_mbean_h); + + JavaCalls::call_virtual(&result2, + dcmd_mbean_klass, + vmSymbols::createDiagnosticFrameworkNotification_name(), + vmSymbols::void_method_signature(), + &args2, + CHECK); + } +} + Mutex* DCmdFactory::_dcmdFactory_lock = new Mutex(Mutex::leaf, "DCmdFactory", true); +bool DCmdFactory::_send_jmx_notification = false; -DCmdFactory* DCmdFactory::factory(const char* name, size_t len) { +DCmdFactory* DCmdFactory::factory(DCmdSource source, const char* name, size_t len) { MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag); DCmdFactory* factory = _DCmdFactoryList; while (factory != NULL) { if (strlen(factory->name()) == len && strncmp(name, factory->name(), len) == 0) { - return factory; + if(factory->export_flags() & source) { + return factory; + } else { + return NULL; + } } factory = factory->_next; } @@ -439,11 +512,16 @@ int DCmdFactory::register_DCmdFactory(DCmdFactory* factory) { MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag); factory->_next = _DCmdFactoryList; _DCmdFactoryList = factory; + if (_send_jmx_notification && !factory->_hidden + && (factory->_export_flags & DCmd_Source_MBean)) { + DCmdFactory::push_jmx_notification_request(); + } return 0; // Actually, there's no checks for duplicates } -DCmd* DCmdFactory::create_global_DCmd(CmdLine &line, outputStream* out, TRAPS) { - DCmdFactory* f = factory(line.cmd_addr(), line.cmd_len()); +DCmd* DCmdFactory::create_global_DCmd(DCmdSource source, CmdLine &line, + outputStream* out, TRAPS) { + DCmdFactory* f = factory(source, line.cmd_addr(), line.cmd_len()); if (f != NULL) { if (f->is_enabled()) { THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), @@ -455,8 +533,9 @@ DCmd* DCmdFactory::create_global_DCmd(CmdLine &line, outputStream* out, TRAPS) { "Unknown diagnostic command"); } -DCmd* DCmdFactory::create_local_DCmd(CmdLine &line, outputStream* out, TRAPS) { - DCmdFactory* f = factory(line.cmd_addr(), line.cmd_len()); +DCmd* DCmdFactory::create_local_DCmd(DCmdSource source, CmdLine &line, + outputStream* out, TRAPS) { + DCmdFactory* f = factory(source, line.cmd_addr(), line.cmd_len()); if (f != NULL) { if (!f->is_enabled()) { THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), @@ -468,12 +547,12 @@ DCmd* DCmdFactory::create_local_DCmd(CmdLine &line, outputStream* out, TRAPS) { "Unknown diagnostic command"); } -GrowableArray* DCmdFactory::DCmd_list() { +GrowableArray* DCmdFactory::DCmd_list(DCmdSource source) { MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag); GrowableArray* array = new GrowableArray(); DCmdFactory* factory = _DCmdFactoryList; while (factory != NULL) { - if (!factory->is_hidden()) { + if (!factory->is_hidden() && (factory->export_flags() & source)) { array->append(factory->name()); } factory = factory->next(); @@ -481,15 +560,16 @@ GrowableArray* DCmdFactory::DCmd_list() { return array; } -GrowableArray* DCmdFactory::DCmdInfo_list() { +GrowableArray* DCmdFactory::DCmdInfo_list(DCmdSource source ) { MutexLockerEx ml(_dcmdFactory_lock, Mutex::_no_safepoint_check_flag); GrowableArray* array = new GrowableArray(); DCmdFactory* factory = _DCmdFactoryList; while (factory != NULL) { - if (!factory->is_hidden()) { + if (!factory->is_hidden() && (factory->export_flags() & source)) { array->append(new DCmdInfo(factory->name(), factory->description(), factory->impact(), - factory->num_arguments(), factory->is_enabled())); + factory->permission(), factory->num_arguments(), + factory->is_enabled())); } factory = factory->next(); } diff --git a/src/share/vm/services/diagnosticFramework.hpp b/src/share/vm/services/diagnosticFramework.hpp index 08b24e07f3326872e4c9d061d91ec8f1f4486c5a..ca60f53ebc8220b6adfdffdbf97dbdc5eed2cdcb 100644 --- a/src/share/vm/services/diagnosticFramework.hpp +++ b/src/share/vm/services/diagnosticFramework.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -34,6 +34,22 @@ #include "utilities/ostream.hpp" +enum DCmdSource { + DCmd_Source_Internal = 0x01U, // invocation from the JVM + DCmd_Source_AttachAPI = 0x02U, // invocation via the attachAPI + DCmd_Source_MBean = 0x04U // invocation via a MBean +}; + +// Warning: strings referenced by the JavaPermission struct are passed to +// the native part of the JDK. Avoid use of dynamically allocated strings +// that could be de-allocated before the JDK native code had time to +// convert them into Java Strings. +struct JavaPermission { + const char* _class; + const char* _name; + const char* _action; +}; + // CmdLine is the class used to handle a command line containing a single // diagnostic command and its arguments. It provides methods to access the // command name and the beginning of the arguments. The class is also @@ -113,26 +129,30 @@ public: // used to export the description to the JMX interface of the framework. class DCmdInfo : public ResourceObj { protected: - const char* _name; - const char* _description; - const char* _impact; - int _num_arguments; - bool _is_enabled; + const char* _name; /* Name of the diagnostic command */ + const char* _description; /* Short description */ + const char* _impact; /* Impact on the JVM */ + JavaPermission _permission; /* Java Permission required to execute this command if any */ + int _num_arguments; /* Number of supported options or arguments */ + bool _is_enabled; /* True if the diagnostic command can be invoked, false otherwise */ public: DCmdInfo(const char* name, const char* description, const char* impact, + JavaPermission permission, int num_arguments, bool enabled) { this->_name = name; this->_description = description; this->_impact = impact; + this->_permission = permission; this->_num_arguments = num_arguments; this->_is_enabled = enabled; } const char* name() const { return _name; } const char* description() const { return _description; } const char* impact() const { return _impact; } + JavaPermission permission() const { return _permission; } int num_arguments() const { return _num_arguments; } bool is_enabled() const { return _is_enabled; } @@ -144,16 +164,20 @@ public: // framework. class DCmdArgumentInfo : public ResourceObj { protected: - const char* _name; - const char* _description; - const char* _type; - const char* _default_string; - bool _mandatory; - bool _option; - int _position; + const char* _name; /* Option/Argument name*/ + const char* _description; /* Short description */ + const char* _type; /* Type: STRING, BOOLEAN, etc. */ + const char* _default_string; /* Default value in a parsable string */ + bool _mandatory; /* True if the option/argument is mandatory */ + bool _option; /* True if it is an option, false if it is an argument */ + /* (see diagnosticFramework.hpp for option/argument definitions) */ + bool _multiple; /* True is the option can be specified several time */ + int _position; /* Expected position for this argument (this field is */ + /* meaningless for options) */ public: DCmdArgumentInfo(const char* name, const char* description, const char* type, - const char* default_string, bool mandatory, bool option) { + const char* default_string, bool mandatory, bool option, + bool multiple) { this->_name = name; this->_description = description; this->_type = type; @@ -161,11 +185,12 @@ public: this->_option = option; this->_mandatory = mandatory; this->_option = option; + this->_multiple = multiple; this->_position = -1; } DCmdArgumentInfo(const char* name, const char* description, const char* type, const char* default_string, bool mandatory, bool option, - int position) { + bool multiple, int position) { this->_name = name; this->_description = description; this->_type = type; @@ -173,6 +198,7 @@ public: this->_option = option; this->_mandatory = mandatory; this->_option = option; + this->_multiple = multiple; this->_position = position; } const char* name() const { return _name; } @@ -181,11 +207,29 @@ public: const char* default_string() const { return _default_string; } bool is_mandatory() const { return _mandatory; } bool is_option() const { return _option; } + bool is_multiple() const { return _multiple; } int position() const { return _position; } }; // The DCmdParser class can be used to create an argument parser for a // diagnostic command. It is not mandatory to use it to parse arguments. +// The DCmdParser parses a CmdLine instance according to the parameters that +// have been declared by its associated diagnostic command. A parameter can +// either be an option or an argument. Options are identified by the option name +// while arguments are identified by their position in the command line. The +// position of an argument is defined relative to all arguments passed on the +// command line, options are not considered when defining an argument position. +// The generic syntax of a diagnostic command is: +// +// [