8178380: Module system implementation refresh (5/2017)
authoralanb
Thu, 04 May 2017 07:26:28 +0000
changeset 44993 f61bcd80ec1f
parent 44741 c5de7263722b
child 44994 1c32d3f0daf0
8178380: Module system implementation refresh (5/2017) Reviewed-by: lfoltan, hseigel, mchung, sspitsyn Contributed-by: alan.bateman@oracle.com, serguei.spitsyn@oracle.com
hotspot/src/share/vm/classfile/moduleEntry.cpp
hotspot/src/share/vm/classfile/modules.cpp
hotspot/src/share/vm/classfile/packageEntry.cpp
hotspot/src/share/vm/classfile/vmSymbols.hpp
hotspot/src/share/vm/logging/logTag.hpp
hotspot/src/share/vm/oops/instanceKlass.cpp
hotspot/src/share/vm/prims/jvmti.xml
hotspot/src/share/vm/prims/jvmtiExport.cpp
hotspot/src/share/vm/prims/jvmtiExport.hpp
hotspot/src/share/vm/runtime/arguments.cpp
hotspot/src/share/vm/runtime/globals.hpp
hotspot/src/share/vm/runtime/thread.cpp
hotspot/src/share/vm/services/attachListener.cpp
hotspot/src/share/vm/services/diagnosticCommand.cpp
hotspot/test/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java
hotspot/test/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/RedefineClassTest.java
hotspot/test/compiler/profiling/spectrapredefineclass/Launcher.java
hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Launcher.java
hotspot/test/runtime/CommandLine/OptionsValidation/TestOptionsWithRangesDynamic.java
hotspot/test/runtime/Metaspace/DefineClass.java
hotspot/test/runtime/logging/ModulesTest.java
hotspot/test/runtime/logging/StartupTimeTest.java
hotspot/test/runtime/modules/JVMAddModuleExports.java
hotspot/test/runtime/modules/JVMAddModulePackage.java
hotspot/test/runtime/modules/JVMDefineModule.java
hotspot/test/runtime/modules/ModuleStress/ModuleStress.java
hotspot/test/runtime/modules/ModuleStress/ModuleStressGC.java
--- a/hotspot/src/share/vm/classfile/moduleEntry.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/classfile/moduleEntry.cpp	Thu May 04 07:26:28 2017 +0000
@@ -158,10 +158,10 @@
       loader_data() != m_loader_data &&
       !m_loader_data->is_builtin_class_loader_data()) {
     _must_walk_reads = true;
-    if (log_is_enabled(Trace, modules)) {
+    if (log_is_enabled(Trace, module)) {
       ResourceMark rm;
-      log_trace(modules)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked",
-                         (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
+      log_trace(module)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked",
+                        (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
     }
   }
 }
@@ -180,10 +180,10 @@
     // on the remaining live modules on the reads list.
     _must_walk_reads = false;
 
-    if (log_is_enabled(Trace, modules)) {
+    if (log_is_enabled(Trace, module)) {
       ResourceMark rm;
-      log_trace(modules)("ModuleEntry::purge_reads(): module %s reads list being walked",
-                         (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
+      log_trace(module)("ModuleEntry::purge_reads(): module %s reads list being walked",
+                        (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE);
     }
 
     // Go backwards because this removes entries that are dead.
@@ -236,8 +236,11 @@
       m = m->next();
 
       ResourceMark rm;
-      log_debug(modules)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ?
-                         to_remove->name()->as_C_string() : UNNAMED_MODULE);
+      if (to_remove->name() != NULL) {
+        log_info(module, unload)("unloading module %s", to_remove->name()->as_C_string());
+      }
+      log_debug(module)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ?
+                        to_remove->name()->as_C_string() : UNNAMED_MODULE);
 
       // Clean out the C heap allocated reads list first before freeing the entry
       to_remove->delete_reads();
@@ -315,9 +318,9 @@
 
   if (ClassLoader::is_in_patch_mod_entries(name)) {
     entry->set_is_patched();
-    if (log_is_enabled(Trace, modules, patch)) {
+    if (log_is_enabled(Trace, module, patch)) {
       ResourceMark rm;
-      log_trace(modules, patch)("Marked module %s as patched from --patch-module", name->as_C_string());
+      log_trace(module, patch)("Marked module %s as patched from --patch-module", name->as_C_string());
     }
   }
 
--- a/hotspot/src/share/vm/classfile/modules.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/classfile/modules.cpp	Thu May 04 07:26:28 2017 +0000
@@ -237,16 +237,18 @@
   // Patch any previously loaded class's module field with java.base's java.lang.Module.
   ModuleEntryTable::patch_javabase_entries(module_handle);
 
-  log_debug(modules)("define_javabase_module(): Definition of module: "
-                     JAVA_BASE_NAME ", version: %s, location: %s, package #: %d",
-                     module_version != NULL ? module_version : "NULL",
-                     module_location != NULL ? module_location : "NULL",
-                     pkg_list->length());
+  log_info(module, load)(JAVA_BASE_NAME " location: %s",
+                         module_location != NULL ? module_location : "NULL");
+  log_debug(module)("define_javabase_module(): Definition of module: "
+                    JAVA_BASE_NAME ", version: %s, location: %s, package #: %d",
+                    module_version != NULL ? module_version : "NULL",
+                    module_location != NULL ? module_location : "NULL",
+                    pkg_list->length());
 
   // packages defined to java.base
   for (int x = 0; x < pkg_list->length(); x++) {
-    log_trace(modules)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME,
-                       (pkg_list->at(x))->as_C_string());
+    log_trace(module)("define_javabase_module(): creation of package %s for module " JAVA_BASE_NAME,
+                      (pkg_list->at(x))->as_C_string());
   }
 }
 
@@ -438,23 +440,24 @@
       throw_dup_pkg_exception(module_name, existing_pkg, CHECK);
   }
 
-  if (log_is_enabled(Debug, modules)) {
-    outputStream* logst = Log(modules)::debug_stream();
+  log_info(module, load)("%s location: %s", module_name,
+                         module_location != NULL ? module_location : "NULL");
+  if (log_is_enabled(Debug, module)) {
+    outputStream* logst = Log(module)::debug_stream();
     logst->print("define_module(): creation of module: %s, version: %s, location: %s, ",
                  module_name, module_version != NULL ? module_version : "NULL",
                  module_location != NULL ? module_location : "NULL");
     loader_data->print_value_on(logst);
     logst->print_cr(", package #: %d", pkg_list->length());
     for (int y = 0; y < pkg_list->length(); y++) {
-      log_trace(modules)("define_module(): creation of package %s for module %s",
-                         (pkg_list->at(y))->as_C_string(), module_name);
+      log_trace(module)("define_module(): creation of package %s for module %s",
+                        (pkg_list->at(y))->as_C_string(), module_name);
     }
   }
 
   // If the module is defined to the boot loader and an exploded build is being
   // used, prepend <java.home>/modules/modules_name, if it exists, to the system boot class path.
   if (loader == NULL &&
-      !Universe::is_module_initialized() &&
       !ClassLoader::has_jrt_entry()) {
     ClassLoader::add_to_exploded_build_list(module_symbol, CHECK);
   }
@@ -487,7 +490,7 @@
   }
   Handle h_loader = Handle(THREAD, loader);
 
-  log_debug(modules)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
+  log_debug(module)("set_bootloader_unnamed_module(): recording unnamed module for boot loader");
 
   // Ensure the boot loader's PackageEntryTable has been created
   ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK);
@@ -545,10 +548,10 @@
                       from_module_entry->name()->as_C_string()));
   }
 
-  log_debug(modules)("add_module_exports(): package %s in module %s is exported to module %s",
-                     package_entry->name()->as_C_string(),
-                     from_module_entry->name()->as_C_string(),
-                     to_module_entry == NULL ? "NULL" :
+  log_debug(module)("add_module_exports(): package %s in module %s is exported to module %s",
+                    package_entry->name()->as_C_string(),
+                    from_module_entry->name()->as_C_string(),
+                    to_module_entry == NULL ? "NULL" :
                       to_module_entry->is_named() ?
                         to_module_entry->name()->as_C_string() : UNNAMED_MODULE);
 
@@ -592,12 +595,12 @@
   }
 
   ResourceMark rm(THREAD);
-  log_debug(modules)("add_reads_module(): Adding read from module %s to module %s",
-                     from_module_entry->is_named() ?
-                     from_module_entry->name()->as_C_string() : UNNAMED_MODULE,
-                     to_module_entry == NULL ? "all unnamed" :
-                       (to_module_entry->is_named() ?
-                        to_module_entry->name()->as_C_string() : UNNAMED_MODULE));
+  log_debug(module)("add_reads_module(): Adding read from module %s to module %s",
+                    from_module_entry->is_named() ?
+                    from_module_entry->name()->as_C_string() : UNNAMED_MODULE,
+                    to_module_entry == NULL ? "all unnamed" :
+                      (to_module_entry->is_named() ?
+                       to_module_entry->name()->as_C_string() : UNNAMED_MODULE));
 
   // if modules are the same or if from_module is unnamed then no need to add the read.
   if (from_module_entry != to_module_entry && from_module_entry->is_named()) {
@@ -616,7 +619,7 @@
   }
   oop mirror = JNIHandles::resolve_non_null(clazz);
   if (mirror == NULL) {
-    log_debug(modules)("get_module(): no mirror, returning NULL");
+    log_debug(module)("get_module(): no mirror, returning NULL");
     return NULL;
   }
   if (!java_lang_Class::is_instance(mirror)) {
@@ -629,9 +632,9 @@
   assert(module != NULL, "java.lang.Class module field not set");
   assert(java_lang_Module::is_instance(module), "module is not an instance of type java.lang.Module");
 
-  if (log_is_enabled(Debug, modules)) {
+  if (log_is_enabled(Debug, module)) {
     ResourceMark rm(THREAD);
-    outputStream* logst = Log(modules)::debug_stream();
+    outputStream* logst = Log(module)::debug_stream();
     Klass* klass = java_lang_Class::as_Klass(mirror);
     oop module_name = java_lang_Module::name(module);
     if (module_name != NULL) {
@@ -764,8 +767,8 @@
     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), message);
   }
 
-  log_debug(modules)("add_module_package(): Adding package %s to module %s",
-                     package_name, module_entry->name()->as_C_string());
+  log_debug(module)("add_module_package(): Adding package %s to module %s",
+                    package_name, module_entry->name()->as_C_string());
 
   TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK);
   PackageEntryTable* package_table = loader_data->packages();
@@ -820,8 +823,8 @@
                         module_entry->name()->as_C_string()));
     }
 
-    log_debug(modules)("add_module_exports_to_all_unnamed(): package %s in module"
-                       " %s is exported to all unnamed modules",
+    log_debug(module)("add_module_exports_to_all_unnamed(): package %s in module"
+                      " %s is exported to all unnamed modules",
                        package_entry->name()->as_C_string(),
                        module_entry->name()->as_C_string());
 
--- a/hotspot/src/share/vm/classfile/packageEntry.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/classfile/packageEntry.cpp	Thu May 04 07:26:28 2017 +0000
@@ -77,13 +77,13 @@
       (this_pkg_mod == NULL || this_pkg_mod->loader_data() != m_loader_data) &&
       !m_loader_data->is_builtin_class_loader_data()) {
     _must_walk_exports = true;
-    if (log_is_enabled(Trace, modules)) {
+    if (log_is_enabled(Trace, module)) {
       ResourceMark rm;
       assert(name() != NULL, "PackageEntry without a valid name");
-      log_trace(modules)("PackageEntry::set_export_walk_required(): package %s defined in module %s, exports list must be walked",
-                         name()->as_C_string(),
-                         (this_pkg_mod == NULL || this_pkg_mod->name() == NULL) ?
-                           UNNAMED_MODULE : this_pkg_mod->name()->as_C_string());
+      log_trace(module)("PackageEntry::set_export_walk_required(): package %s defined in module %s, exports list must be walked",
+                        name()->as_C_string(),
+                        (this_pkg_mod == NULL || this_pkg_mod->name() == NULL) ?
+                          UNNAMED_MODULE : this_pkg_mod->name()->as_C_string());
     }
   }
 }
@@ -132,13 +132,13 @@
     // on the remaining live modules on the exports list.
     _must_walk_exports = false;
 
-    if (log_is_enabled(Trace, modules)) {
+    if (log_is_enabled(Trace, module)) {
       ResourceMark rm;
       assert(name() != NULL, "PackageEntry without a valid name");
       ModuleEntry* pkg_mod = module();
-      log_trace(modules)("PackageEntry::purge_qualified_exports(): package %s defined in module %s, exports list being walked",
-                         name()->as_C_string(),
-                         (pkg_mod == NULL || pkg_mod->name() == NULL) ? UNNAMED_MODULE : pkg_mod->name()->as_C_string());
+      log_trace(module)("PackageEntry::purge_qualified_exports(): package %s defined in module %s, exports list being walked",
+                        name()->as_C_string(),
+                        (pkg_mod == NULL || pkg_mod->name() == NULL) ? UNNAMED_MODULE : pkg_mod->name()->as_C_string());
     }
 
     // Go backwards because this removes entries that are dead.
--- a/hotspot/src/share/vm/classfile/vmSymbols.hpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/classfile/vmSymbols.hpp	Thu May 04 07:26:28 2017 +0000
@@ -650,6 +650,8 @@
   template(addUses_signature,                          "(Ljava/lang/Module;Ljava/lang/Class;)V")                  \
   template(addProvides_name,                           "addProvides")                                             \
   template(addProvides_signature,                      "(Ljava/lang/Module;Ljava/lang/Class;Ljava/lang/Class;)V") \
+  template(loadModule_name,                            "loadModule")                                              \
+  template(loadModule_signature,                       "(Ljava/lang/String;)Ljava/lang/Module;")                  \
   template(transformedByAgent_name,                    "transformedByAgent")                                      \
   template(transformedByAgent_signature,               "(Ljava/lang/Module;)V")                                   \
   template(appendToClassPathForInstrumentation_name,   "appendToClassPathForInstrumentation")                     \
--- a/hotspot/src/share/vm/logging/logTag.hpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/logging/logTag.hpp	Thu May 04 07:26:28 2017 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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
@@ -82,7 +82,7 @@
   LOG_TAG(metadata) \
   LOG_TAG(metaspace) \
   LOG_TAG(mmu) \
-  LOG_TAG(modules) \
+  LOG_TAG(module) \
   LOG_TAG(monitorinflation) \
   LOG_TAG(monitormismatch) \
   LOG_TAG(nmethod) \
--- a/hotspot/src/share/vm/oops/instanceKlass.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/oops/instanceKlass.cpp	Thu May 04 07:26:28 2017 +0000
@@ -2331,21 +2331,21 @@
              name()->as_C_string(), loader_data->loader_name());
     }
 
-    if (log_is_enabled(Debug, modules)) {
+    if (log_is_enabled(Debug, module)) {
       ResourceMark rm;
       ModuleEntry* m = _package_entry->module();
-      log_trace(modules)("Setting package: class: %s, package: %s, loader: %s, module: %s",
-                         external_name(),
-                         pkg_name->as_C_string(),
-                         loader_data->loader_name(),
-                         (m->is_named() ? m->name()->as_C_string() : UNNAMED_MODULE));
+      log_trace(module)("Setting package: class: %s, package: %s, loader: %s, module: %s",
+                        external_name(),
+                        pkg_name->as_C_string(),
+                        loader_data->loader_name(),
+                        (m->is_named() ? m->name()->as_C_string() : UNNAMED_MODULE));
     }
   } else {
     ResourceMark rm;
-    log_trace(modules)("Setting package: class: %s, package: unnamed, loader: %s, module: %s",
-                       external_name(),
-                       (loader_data != NULL) ? loader_data->loader_name() : "NULL",
-                       UNNAMED_MODULE);
+    log_trace(module)("Setting package: class: %s, package: unnamed, loader: %s, module: %s",
+                      external_name(),
+                      (loader_data != NULL) ? loader_data->loader_name() : "NULL",
+                      UNNAMED_MODULE);
   }
 }
 
--- a/hotspot/src/share/vm/prims/jvmti.xml	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/prims/jvmti.xml	Thu May 04 07:26:28 2017 +0000
@@ -6814,7 +6814,9 @@
         <functionlink id="AddModuleReads"/>, <functionlink id="AddModuleExports"/>,
         <functionlink id="AddModuleOpens"/>, <functionlink id="AddModuleUses"/>,
         and <functionlink id="AddModuleProvides"/>. If a module is not modifiable
-        then the module can not be updated with these functions.
+        then the module can not be updated with these functions. The result of
+        this function is always <code>JNI_TRUE</code> when called to determine
+        if an unnamed module is modifiable.
       </description>
       <origin>new</origin>
       <capabilities>
--- a/hotspot/src/share/vm/prims/jvmtiExport.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/prims/jvmtiExport.cpp	Thu May 04 07:26:28 2017 +0000
@@ -54,7 +54,6 @@
 #include "runtime/os.inline.hpp"
 #include "runtime/thread.inline.hpp"
 #include "runtime/vframe.hpp"
-#include "services/attachListener.hpp"
 #include "services/serviceUtil.hpp"
 #include "utilities/macros.hpp"
 #if INCLUDE_ALL_GCS
@@ -2479,15 +2478,6 @@
   typedef jint (JNICALL *OnAttachEntry_t)(JavaVM*, char *, void *);
 }
 
-jint JvmtiExport::load_agent_library(AttachOperation* op, outputStream* st) {
-  // get agent name and options
-  const char* agent = op->arg(0);
-  const char* absParam = op->arg(1);
-  const char* options = op->arg(2);
-
-  return load_agent_library(agent, absParam, options, st);
-}
-
 jint JvmtiExport::load_agent_library(const char *agent, const char *absParam,
                                      const char *options, outputStream* st) {
   char ebuf[1024];
--- a/hotspot/src/share/vm/prims/jvmtiExport.hpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/prims/jvmtiExport.hpp	Thu May 04 07:26:28 2017 +0000
@@ -45,7 +45,6 @@
 class JvmtiManageCapabilities;
 class JvmtiEnv;
 class JvmtiThreadState;
-class AttachOperation;
 
 #define JVMTI_SUPPORT_FLAG(key)                                           \
   private:                                                                \
@@ -396,7 +395,6 @@
 #if INCLUDE_SERVICES
   // attach support
   static jint load_agent_library(const char *agent, const char *absParam, const char *options, outputStream* out) NOT_JVMTI_RETURN_(JNI_ERR);
-  static jint load_agent_library(AttachOperation* op, outputStream* out) NOT_JVMTI_RETURN_(JNI_ERR);
 #endif
 
   // SetNativeMethodPrefix support
--- a/hotspot/src/share/vm/runtime/arguments.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/runtime/arguments.cpp	Thu May 04 07:26:28 2017 +0000
@@ -2837,11 +2837,14 @@
         build_jvm_args(option->optionString);
     }
 
-    // -verbose:[class/gc/jni]
+    // -verbose:[class/module/gc/jni]
     if (match_option(option, "-verbose", &tail)) {
       if (!strcmp(tail, ":class") || !strcmp(tail, "")) {
         LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(class, load));
         LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(class, unload));
+      } else if (!strcmp(tail, ":module")) {
+        LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(module, load));
+        LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(module, unload));
       } else if (!strcmp(tail, ":gc")) {
         LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(gc));
       } else if (!strcmp(tail, ":jni")) {
--- a/hotspot/src/share/vm/runtime/globals.hpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/runtime/globals.hpp	Thu May 04 07:26:28 2017 +0000
@@ -3874,6 +3874,9 @@
   product(bool, StartAttachListener, false,                                 \
           "Always start Attach Listener at VM startup")                     \
                                                                             \
+  product(bool, EnableDynamicAgentLoading, true,                            \
+          "Allow tools to load agents with the attach mechanism")           \
+                                                                            \
   manageable(bool, PrintConcurrentLocks, false,                             \
           "Print java.util.concurrent locks in thread dump")                \
                                                                             \
--- a/hotspot/src/share/vm/runtime/thread.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/runtime/thread.cpp	Thu May 04 07:26:28 2017 +0000
@@ -3404,7 +3404,7 @@
 //
 //     After phase 2, The VM will begin search classes from -Xbootclasspath/a.
 static void call_initPhase2(TRAPS) {
-  TraceTime timer("Phase2 initialization", TRACETIME_LOG(Info, modules, startuptime));
+  TraceTime timer("Phase2 initialization", TRACETIME_LOG(Info, module, startuptime));
 
   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_System(), true, CHECK);
   instanceKlassHandle klass (THREAD, k);
--- a/hotspot/src/share/vm/services/attachListener.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/services/attachListener.cpp	Thu May 04 07:26:28 2017 +0000
@@ -100,6 +100,36 @@
   return JNI_OK;
 }
 
+// Implementation of "load" command.
+static jint load_agent(AttachOperation* op, outputStream* out) {
+  // get agent name and options
+  const char* agent = op->arg(0);
+  const char* absParam = op->arg(1);
+  const char* options = op->arg(2);
+
+  // If loading a java agent then need to ensure that the java.instrument module is loaded
+  if (strcmp(agent, "instrument") == 0) {
+    Thread* THREAD = Thread::current();
+    ResourceMark rm(THREAD);
+    HandleMark hm(THREAD);
+    JavaValue result(T_OBJECT);
+    Handle h_module_name = java_lang_String::create_from_str("java.instrument", THREAD);
+    JavaCalls::call_static(&result,
+                           SystemDictionary::module_Modules_klass(),
+                           vmSymbols::loadModule_name(),
+                           vmSymbols::loadModule_signature(),
+                           h_module_name,
+                           THREAD);
+    if (HAS_PENDING_EXCEPTION) {
+      java_lang_Throwable::print(PENDING_EXCEPTION, out);
+      CLEAR_PENDING_EXCEPTION;
+      return JNI_ERR;
+    }
+  }
+
+  return JvmtiExport::load_agent_library(agent, absParam, options, out);
+}
+
 // Implementation of "properties" command.
 // See also: PrintSystemPropertiesDCmd class
 static jint get_system_properties(AttachOperation* op, outputStream* out) {
@@ -282,7 +312,7 @@
   { "agentProperties",  get_agent_properties },
   { "datadump",         data_dump },
   { "dumpheap",         dump_heap },
-  { "load",             JvmtiExport::load_agent_library },
+  { "load",             load_agent },
   { "properties",       get_system_properties },
   { "threaddump",       thread_dump },
   { "inspectheap",      heap_inspection },
@@ -321,6 +351,10 @@
     // handle special detachall operation
     if (strcmp(op->name(), AttachOperation::detachall_operation_name()) == 0) {
       AttachListener::detachall();
+    } else if (!EnableDynamicAgentLoading && strcmp(op->name(), "load") == 0) {
+      st.print("Dynamic agent loading is not enabled. "
+               "Use -XX:+EnableDynamicAgentLoading to launch target VM.");
+      res = JNI_ERR;
     } else {
       // find the function to dispatch too
       AttachOperationFunctionInfo* info = NULL;
--- a/hotspot/src/share/vm/services/diagnosticCommand.cpp	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/src/share/vm/services/diagnosticCommand.cpp	Thu May 04 07:26:28 2017 +0000
@@ -42,6 +42,21 @@
 #include "utilities/macros.hpp"
 #include "oops/objArrayOop.inline.hpp"
 
+
+static void loadAgentModule(TRAPS) {
+  ResourceMark rm(THREAD);
+  HandleMark hm(THREAD);
+
+  JavaValue result(T_OBJECT);
+  Handle h_module_name = java_lang_String::create_from_str("jdk.management.agent", CHECK);
+  JavaCalls::call_static(&result,
+                         SystemDictionary::module_Modules_klass(),
+                         vmSymbols::loadModule_name(),
+                         vmSymbols::loadModule_signature(),
+                         h_module_name,
+                         THREAD);
+}
+
 void DCmdRegistrant::register_dcmds(){
   // Registration of the diagnostic commands
   // First argument specifies which interfaces will export the command
@@ -753,6 +768,7 @@
     // the remote management server.
     // throw java.lang.NoSuchMethodError if the method doesn't exist
 
+    loadAgentModule(CHECK);
     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_agent_Agent(), loader, Handle(), true, CHECK);
     instanceKlassHandle ik (THREAD, k);
@@ -826,6 +842,7 @@
     // the local management server
     // throw java.lang.NoSuchMethodError if method doesn't exist
 
+    loadAgentModule(CHECK);
     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_agent_Agent(), loader, Handle(), true, CHECK);
     instanceKlassHandle ik (THREAD, k);
@@ -843,6 +860,7 @@
     // management server
     // throw java.lang.NoSuchMethodError if method doesn't exist
 
+    loadAgentModule(CHECK);
     Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
     Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_agent_Agent(), loader, Handle(), true, CHECK);
     instanceKlassHandle ik (THREAD, k);
@@ -864,6 +882,7 @@
   // invoke getManagementAgentStatus() method to generate the status info
   // throw java.lang.NoSuchMethodError if method doesn't exist
 
+  loadAgentModule(CHECK);
   Handle loader = Handle(THREAD, SystemDictionary::java_system_loader());
   Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::jdk_internal_agent_Agent(), loader, Handle(), true, CHECK);
   instanceKlassHandle ik (THREAD, k);
--- a/hotspot/test/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/compiler/jsr292/RedefineMethodUsedByMultipleMethodHandles.java	Thu May 04 07:26:28 2017 +0000
@@ -32,7 +32,7 @@
  *          java.management
  *          jdk.attach
  *
- * @run main/othervm compiler.jsr292.RedefineMethodUsedByMultipleMethodHandles
+ * @run main/othervm -Djdk.attach.allowAttachSelf compiler.jsr292.RedefineMethodUsedByMultipleMethodHandles
  */
 
 package compiler.jsr292;
--- a/hotspot/test/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/RedefineClassTest.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/RedefineClassTest.java	Thu May 04 07:26:28 2017 +0000
@@ -29,7 +29,8 @@
  *          jdk.internal.vm.ci/jdk.vm.ci.runtime
  *          jdk.attach
  *          java.base/jdk.internal.misc
- * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.vm.ci.runtime.test.RedefineClassTest
+ * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -Djdk.attach.allowAttachSelf
+ *                   jdk.vm.ci.runtime.test.RedefineClassTest
  */
 
 package jdk.vm.ci.runtime.test;
--- a/hotspot/test/compiler/profiling/spectrapredefineclass/Launcher.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/compiler/profiling/spectrapredefineclass/Launcher.java	Thu May 04 07:26:28 2017 +0000
@@ -33,7 +33,7 @@
  * @run driver compiler.profiling.spectrapredefineclass.Launcher
  * @run main/othervm -XX:-TieredCompilation -XX:-BackgroundCompilation
  *                   -XX:-UseOnStackReplacement -XX:TypeProfileLevel=222
- *                   -XX:ReservedCodeCacheSize=3M
+ *                   -XX:ReservedCodeCacheSize=3M -Djdk.attach.allowAttachSelf
  *                   compiler.profiling.spectrapredefineclass.Agent
  */
 
--- a/hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Launcher.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Launcher.java	Thu May 04 07:26:28 2017 +0000
@@ -36,7 +36,7 @@
  * @run driver compiler.profiling.spectrapredefineclass_classloaders.Launcher
  * @run main/othervm -XX:-TieredCompilation -XX:-BackgroundCompilation
  *                   -XX:-UseOnStackReplacement -XX:TypeProfileLevel=222
- *                   -XX:ReservedCodeCacheSize=3M
+ *                   -XX:ReservedCodeCacheSize=3M -Djdk.attach.allowAttachSelf
  *                   compiler.profiling.spectrapredefineclass_classloaders.Agent
  */
 
--- a/hotspot/test/runtime/CommandLine/OptionsValidation/TestOptionsWithRangesDynamic.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/CommandLine/OptionsValidation/TestOptionsWithRangesDynamic.java	Thu May 04 07:26:28 2017 +0000
@@ -28,7 +28,7 @@
  * @modules java.base/jdk.internal.misc
  *          jdk.attach/sun.tools.attach
  *          java.management
- * @run main/othervm -XX:MinHeapFreeRatio=0 -XX:MaxHeapFreeRatio=100 TestOptionsWithRangesDynamic
+ * @run main/othervm -XX:MinHeapFreeRatio=0 -XX:MaxHeapFreeRatio=100 -Djdk.attach.allowAttachSelf TestOptionsWithRangesDynamic
  */
 
 import java.util.List;
--- a/hotspot/test/runtime/Metaspace/DefineClass.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/Metaspace/DefineClass.java	Thu May 04 07:26:28 2017 +0000
@@ -41,8 +41,8 @@
  * @run main/othervm -XX:+UnlockDiagnosticVMOptions
                      -XX:-UnsyncloadClass -XX:-AllowParallelDefineClass
                      test.DefineClass defineClassParallel
- * @run main/othervm test.DefineClass redefineClass
- * @run main/othervm test.DefineClass redefineClassWithError
+ * @run main/othervm -Djdk.attach.allowAttachSelf test.DefineClass redefineClass
+ * @run main/othervm -Djdk.attach.allowAttachSelf test.DefineClass redefineClassWithError
  * @author volker.simonis@gmail.com
  */
 
--- a/hotspot/test/runtime/logging/ModulesTest.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/logging/ModulesTest.java	Thu May 04 07:26:28 2017 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @summary modules=debug should have logging from statements in the code
+ * @summary -Xlog:module should emit logging output
  * @library /test/lib
  * @modules java.base/jdk.internal.misc
  *          java.management
@@ -35,9 +35,16 @@
 
 public class ModulesTest {
     public static void main(String[] args) throws Exception {
-        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
-            "-Xlog:modules=trace", "-version");
-        OutputAnalyzer output = new OutputAnalyzer(pb.start());
+        testModuleTrace("-Xlog:module=trace", "-version");
+        testModuleLoad("-Xlog:module+load", "-version");
+        testModuleUnload("-Xlog:module+unload", "-version");
+
+        // same as -Xlog:module+load -Xlog:module+unload
+        testModuleLoad("-verbose:module", "-version");
+    }
+
+    static void testModuleTrace(String... args) throws Exception {
+        OutputAnalyzer output = run(args);
         output.shouldContain("define_javabase_module(): Definition of module:");
         output.shouldContain("define_javabase_module(): creation of package");
         output.shouldContain("define_module(): creation of module");
@@ -48,5 +55,22 @@
         output.shouldContain("Setting package: class:");
         output.shouldHaveExitValue(0);
     }
+
+    static void testModuleLoad(String... args) throws Exception {
+        OutputAnalyzer output = run(args);
+        output.shouldContain("java.base location:");
+        output.shouldContain("java.management location:");
+        output.shouldHaveExitValue(0);
+    }
+
+    static void testModuleUnload(String... args) throws Exception {
+        OutputAnalyzer output = run(args);
+        output.shouldHaveExitValue(0);
+    }
+
+    static OutputAnalyzer run(String... args) throws Exception {
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
+        return new OutputAnalyzer(pb.start());
+    }
 }
 
--- a/hotspot/test/runtime/logging/StartupTimeTest.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/logging/StartupTimeTest.java	Thu May 04 07:26:28 2017 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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
@@ -57,7 +57,7 @@
 
     static void analyzeModulesOutputOff(ProcessBuilder pb) throws Exception {
         OutputAnalyzer output = new OutputAnalyzer(pb.start());
-        output.shouldNotContain("[modules,startuptime]");
+        output.shouldNotContain("[module,startuptime]");
         output.shouldHaveExitValue(0);
     }
 
@@ -70,11 +70,11 @@
                                                    InnerClass.class.getName());
         analyzeOutputOff(pb);
 
-        pb = ProcessTools.createJavaProcessBuilder("-Xlog:startuptime+modules",
+        pb = ProcessTools.createJavaProcessBuilder("-Xlog:startuptime+module",
                                                    InnerClass.class.getName());
         analyzeModulesOutputOn(pb);
 
-        pb = ProcessTools.createJavaProcessBuilder("-Xlog:startuptime+modules=off",
+        pb = ProcessTools.createJavaProcessBuilder("-Xlog:startuptime+module=off",
                                                    InnerClass.class.getName());
         analyzeModulesOutputOff(pb);
     }
--- a/hotspot/test/runtime/modules/JVMAddModuleExports.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/modules/JVMAddModuleExports.java	Thu May 04 07:26:28 2017 +0000
@@ -41,12 +41,12 @@
         MyClassLoader to_cl = new MyClassLoader();
         Module from_module, to_module;
 
-        from_module = ModuleHelper.ModuleObject("from_module", from_cl, new String[] { "mypackage", "this/package" });
+        from_module = ModuleHelper.ModuleObject("from_module", from_cl, new String[] { "mypackage", "x/apackage" });
         assertNotNull(from_module, "Module should not be null");
-        ModuleHelper.DefineModule(from_module, "9.0", "from_module/here", new String[] { "mypackage", "this/package" });
-        to_module = ModuleHelper.ModuleObject("to_module", to_cl, new String[] { "yourpackage", "that/package" });
+        ModuleHelper.DefineModule(from_module, "9.0", "from_module/here", new String[] { "mypackage", "x/apackage" });
+        to_module = ModuleHelper.ModuleObject("to_module", to_cl, new String[] { "yourpackage", "that/apackage" });
         assertNotNull(to_module, "Module should not be null");
-        ModuleHelper.DefineModule(to_module, "9.0", "to_module/here", new String[] { "yourpackage", "that/package" });
+        ModuleHelper.DefineModule(to_module, "9.0", "to_module/here", new String[] { "yourpackage", "that/apackage" });
 
         // Null from_module argument, expect an NPE
         try {
@@ -117,19 +117,19 @@
 
         // Export a package, that is not in from_module, to from_module
         try {
-            ModuleHelper.AddModuleExports(from_module, "that/package", from_module);
+            ModuleHelper.AddModuleExports(from_module, "that/apackage", from_module);
             throw new RuntimeException("Failed to get the expected IAE");
         } catch(IllegalArgumentException e) {
             // Expected
         }
 
         // Export the same package twice to the same module
-        ModuleHelper.AddModuleExports(from_module, "this/package", to_module);
-        ModuleHelper.AddModuleExports(from_module, "this/package", to_module);
+        ModuleHelper.AddModuleExports(from_module, "x/apackage", to_module);
+        ModuleHelper.AddModuleExports(from_module, "x/apackage", to_module);
 
         // Export a package, using '.' instead of '/'
         try {
-            ModuleHelper.AddModuleExports(from_module, "this.package", to_module);
+            ModuleHelper.AddModuleExports(from_module, "x.apackage", to_module);
             throw new RuntimeException("Failed to get the expected IAE");
         } catch(IllegalArgumentException e) {
             // Expected
@@ -137,8 +137,8 @@
 
         // Export a package to the unnamed module and then to a specific module.
         // The qualified export should be ignored.
-        ModuleHelper.AddModuleExportsToAll(to_module, "that/package");
-        ModuleHelper.AddModuleExports(to_module, "that/package", from_module);
+        ModuleHelper.AddModuleExportsToAll(to_module, "that/apackage");
+        ModuleHelper.AddModuleExports(to_module, "that/apackage", from_module);
     }
 
     static class MyClassLoader extends ClassLoader { }
--- a/hotspot/test/runtime/modules/JVMAddModulePackage.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/modules/JVMAddModulePackage.java	Thu May 04 07:26:28 2017 +0000
@@ -49,16 +49,16 @@
         module_two = ModuleHelper.ModuleObject("module_two", cl1, new String[] { "yourpackage" });
         assertNotNull(module_two, "Module should not be null");
         ModuleHelper.DefineModule(module_two, "9.0", "module_two/here", new String[] { "yourpackage" });
-        module_three = ModuleHelper.ModuleObject("module_three", cl3, new String[] { "package/num3" });
+        module_three = ModuleHelper.ModuleObject("module_three", cl3, new String[] { "apackage/num3" });
         assertNotNull(module_three, "Module should not be null");
-        ModuleHelper.DefineModule(module_three, "9.0", "module_three/here", new String[] { "package/num3" });
+        ModuleHelper.DefineModule(module_three, "9.0", "module_three/here", new String[] { "apackage/num3" });
 
         // Simple call
         ModuleHelper.AddModulePackage(module_one, "new_package");
 
         // Add a package and export it
-        ModuleHelper.AddModulePackage(module_one, "package/num3");
-        ModuleHelper.AddModuleExportsToAll(module_one, "package/num3");
+        ModuleHelper.AddModulePackage(module_one, "apackage/num3");
+        ModuleHelper.AddModuleExportsToAll(module_one, "apackage/num3");
 
         // Null module argument, expect an NPE
         try {
@@ -94,7 +94,7 @@
 
         // Invalid package name, expect an IAE
         try {
-            ModuleHelper.AddModulePackage(module_one, "your.package");
+            ModuleHelper.AddModulePackage(module_one, "your.apackage");
             throw new RuntimeException("Failed to get the expected IAE");
         } catch(IllegalArgumentException e) {
             // Expected
@@ -102,7 +102,7 @@
 
         // Invalid package name, expect an IAE
         try {
-            ModuleHelper.AddModulePackage(module_one, ";your/package");
+            ModuleHelper.AddModulePackage(module_one, ";your/apackage");
             throw new RuntimeException("Failed to get the expected IAE");
         } catch(IllegalArgumentException e) {
             // Expected
--- a/hotspot/test/runtime/modules/JVMDefineModule.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/modules/JVMDefineModule.java	Thu May 04 07:26:28 2017 +0000
@@ -207,10 +207,10 @@
         ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { });
 
         // Invalid package name, expect an IAE
-        m = ModuleHelper.ModuleObject("moduleFive", cl, new String[] { "your.package" });
+        m = ModuleHelper.ModuleObject("moduleFive", cl, new String[] { "your.apackage" });
         try {
-            ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "your.package" });
-            throw new RuntimeException("Failed to get expected IAE for your.package");
+            ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "your.apackage" });
+            throw new RuntimeException("Failed to get expected IAE for your.apackage");
         } catch(IllegalArgumentException e) {
             if (!e.getMessage().contains("Invalid package name")) {
               throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage());
@@ -220,8 +220,8 @@
         // Invalid package name, expect an IAE
         m = ModuleHelper.ModuleObject("moduleSix", cl, new String[] { "foo" }); // Name irrelevant
         try {
-            ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { ";your/package" });
-            throw new RuntimeException("Failed to get expected IAE for ;your.package");
+            ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { ";your/apackage" });
+            throw new RuntimeException("Failed to get expected IAE for ;your.apackage");
         } catch(IllegalArgumentException e) {
             if (!e.getMessage().contains("Invalid package name")) {
               throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage());
--- a/hotspot/test/runtime/modules/ModuleStress/ModuleStress.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/modules/ModuleStress/ModuleStress.java	Thu May 04 07:26:28 2017 +0000
@@ -53,7 +53,7 @@
         //   those loaders never die.
         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
              "-Xbootclasspath/a:.",
-             "-Xlog:modules=trace",
+             "-Xlog:module=trace",
              "-version");
 
         OutputAnalyzer oa = new OutputAnalyzer(pb.start());
@@ -88,7 +88,7 @@
         //   the same loader and thus have the exact same life cycle.
         pb = ProcessTools.createJavaProcessBuilder(
              "-Xbootclasspath/a:.",
-             "-Xlog:modules=trace",
+             "-Xlog:module=trace",
              "ModuleSameCLMain");
 
         oa = new OutputAnalyzer(pb.start());
@@ -102,7 +102,7 @@
         //   class loaders which could die and thus be unloaded.
         pb = ProcessTools.createJavaProcessBuilder(
              "-Xbootclasspath/a:.",
-             "-Xlog:modules=trace",
+             "-Xlog:module=trace",
              "ModuleNonBuiltinCLMain");
 
         oa = new OutputAnalyzer(pb.start());
@@ -120,7 +120,7 @@
         pb = ProcessTools.createJavaProcessBuilder(
              "-Djava.system.class.loader=CustomSystemClassLoader",
              "-Xbootclasspath/a:.",
-             "-Xlog:modules=trace",
+             "-Xlog:module=trace",
              "ModuleNonBuiltinCLMain");
 
         oa = new OutputAnalyzer(pb.start());
--- a/hotspot/test/runtime/modules/ModuleStress/ModuleStressGC.java	Sat Apr 22 12:05:20 2017 +0200
+++ b/hotspot/test/runtime/modules/ModuleStress/ModuleStressGC.java	Thu May 04 07:26:28 2017 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2017, 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
@@ -73,7 +73,7 @@
         // test's, defined to module jdk.translet, export list at
         // GC safepoints.
         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
-            "-Xlog:modules=trace",
+            "-Xlog:module=trace",
             "-p", MODS_DIR.toString(),
             "-m", "jdk.test/test.MainGC");
         OutputAnalyzer oa = new OutputAnalyzer(pb.start());