7022037: Pause when exiting if debugger is attached on windows
authorsla
Mon, 28 Feb 2011 14:19:52 +0100
changeset 8476 7e34c2d4cf9b
parent 8475 c2b97a27943a
child 8477 e6f1e62b9e23
child 8478 d89e2f60ba40
7022037: Pause when exiting if debugger is attached on windows Reviewed-by: dsamersoff, kamg, hosterda
hotspot/src/os/linux/vm/os_linux.cpp
hotspot/src/os/posix/vm/os_posix.cpp
hotspot/src/os/solaris/vm/os_solaris.cpp
hotspot/src/os/windows/vm/os_windows.cpp
hotspot/src/share/vm/runtime/arguments.cpp
hotspot/src/share/vm/runtime/arguments.hpp
hotspot/src/share/vm/runtime/globals.hpp
hotspot/src/share/vm/runtime/java.cpp
hotspot/src/share/vm/runtime/os.hpp
hotspot/src/share/vm/runtime/thread.cpp
hotspot/src/share/vm/utilities/vmError.cpp
--- a/hotspot/src/os/linux/vm/os_linux.cpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/os/linux/vm/os_linux.cpp	Mon Feb 28 14:19:52 2011 +0100
@@ -2213,7 +2213,7 @@
   if (rp == NULL)
     return;
 
-  if (strcmp(Arguments::sun_java_launcher(), "gamma") == 0) {
+  if (Arguments::created_by_gamma_launcher()) {
     // Support for the gamma launcher.  Typical value for buf is
     // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".  If "/jre/lib/" appears at
     // the right place in the string, then assume we are installed in a JDK and
--- a/hotspot/src/os/posix/vm/os_posix.cpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/os/posix/vm/os_posix.cpp	Mon Feb 28 14:19:52 2011 +0100
@@ -59,3 +59,12 @@
   VMError::report_coredump_status(buffer, success);
 }
 
+bool os::is_debugger_attached() {
+  // not implemented
+  return false;
+}
+
+void os::wait_for_keypress_at_exit(void) {
+  // don't do anything on posix platforms
+  return;
+}
--- a/hotspot/src/os/solaris/vm/os_solaris.cpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/os/solaris/vm/os_solaris.cpp	Mon Feb 28 14:19:52 2011 +0100
@@ -2507,7 +2507,7 @@
   assert(ret != 0, "cannot locate libjvm");
   realpath((char *)dlinfo.dli_fname, buf);
 
-  if (strcmp(Arguments::sun_java_launcher(), "gamma") == 0) {
+  if (Arguments::created_by_gamma_launcher()) {
     // Support for the gamma launcher.  Typical value for buf is
     // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so".  If "/jre/lib/" appears at
     // the right place in the string, then assume we are installed in a JDK and
--- a/hotspot/src/os/windows/vm/os_windows.cpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/os/windows/vm/os_windows.cpp	Mon Feb 28 14:19:52 2011 +0100
@@ -1788,7 +1788,7 @@
   }
 
   buf[0] = '\0';
-  if (strcmp(Arguments::sun_java_launcher(), "gamma") == 0) {
+  if (Arguments::created_by_gamma_launcher()) {
      // Support for the gamma launcher. Check for an
      // JAVA_HOME environment variable
      // and fix up the path so it looks like
@@ -3418,6 +3418,19 @@
 }
 
 
+bool os::is_debugger_attached() {
+  return IsDebuggerPresent() ? true : false;
+}
+
+
+void os::wait_for_keypress_at_exit(void) {
+  if (PauseAtExit) {
+    fprintf(stderr, "Press any key to continue...\n");
+    fgetc(stdin);
+  }
+}
+
+
 int os::message_box(const char* title, const char* message) {
   int result = MessageBox(NULL, message, title,
                           MB_YESNO | MB_ICONERROR | MB_SYSTEMMODAL | MB_DEFAULT_DESKTOP_ONLY);
--- a/hotspot/src/share/vm/runtime/arguments.cpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/share/vm/runtime/arguments.cpp	Mon Feb 28 14:19:52 2011 +0100
@@ -78,6 +78,7 @@
 const char*  Arguments::_java_vendor_url_bug    = DEFAULT_VENDOR_URL_BUG;
 const char*  Arguments::_sun_java_launcher      = DEFAULT_JAVA_LAUNCHER;
 int    Arguments::_sun_java_launcher_pid        = -1;
+bool   Arguments::_created_by_gamma_launcher    = false;
 
 // These parameters are reset in method parse_vm_init_args(JavaVMInitArgs*)
 bool   Arguments::_AlwaysCompileLoopMethods     = AlwaysCompileLoopMethods;
@@ -1656,6 +1657,9 @@
 
 void Arguments::process_java_launcher_argument(const char* launcher, void* extra_info) {
   _sun_java_launcher = strdup(launcher);
+  if (strcmp("gamma", _sun_java_launcher) == 0) {
+    _created_by_gamma_launcher = true;
+  }
 }
 
 bool Arguments::created_by_java_launcher() {
@@ -1663,6 +1667,10 @@
   return strcmp(DEFAULT_JAVA_LAUNCHER, _sun_java_launcher) != 0;
 }
 
+bool Arguments::created_by_gamma_launcher() {
+  return _created_by_gamma_launcher;
+}
+
 //===========================================================================================================
 // Parsing of main arguments
 
@@ -3155,6 +3163,16 @@
     }
   }
 
+  // set PauseAtExit if the gamma launcher was used and a debugger is attached
+  // but only if not already set on the commandline
+  if (Arguments::created_by_gamma_launcher() && os::is_debugger_attached()) {
+    bool set = false;
+    CommandLineFlags::wasSetOnCmdline("PauseAtExit", &set);
+    if (!set) {
+      FLAG_SET_DEFAULT(PauseAtExit, true);
+    }
+  }
+
   return JNI_OK;
 }
 
--- a/hotspot/src/share/vm/runtime/arguments.hpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/share/vm/runtime/arguments.hpp	Mon Feb 28 14:19:52 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, 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
@@ -257,6 +257,9 @@
   // sun.java.launcher.pid, private property
   static int    _sun_java_launcher_pid;
 
+  // was this VM created by the gamma launcher
+  static bool   _created_by_gamma_launcher;
+
   // Option flags
   static bool   _has_profile;
   static bool   _has_alloc_profile;
@@ -444,6 +447,8 @@
   static const char* sun_java_launcher()    { return _sun_java_launcher; }
   // Was VM created by a Java launcher?
   static bool created_by_java_launcher();
+  // Was VM created by the gamma Java launcher?
+  static bool created_by_gamma_launcher();
   // -Dsun.java.launcher.pid
   static int sun_java_launcher_pid()        { return _sun_java_launcher_pid; }
 
--- a/hotspot/src/share/vm/runtime/globals.hpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/share/vm/runtime/globals.hpp	Mon Feb 28 14:19:52 2011 +0100
@@ -3730,6 +3730,9 @@
           "The file to create and for whose removal to await when pausing " \
           "at startup. (default: ./vm.paused.<pid>)")                       \
                                                                             \
+  diagnostic(bool, PauseAtExit, false,                                      \
+          "Pause and wait for keypress on exit if a debugger is attached")  \
+                                                                            \
   product(bool, ExtendedDTraceProbes,    false,                             \
           "Enable performance-impacting dtrace probes")                     \
                                                                             \
--- a/hotspot/src/share/vm/runtime/java.cpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/share/vm/runtime/java.cpp	Mon Feb 28 14:19:52 2011 +0100
@@ -551,6 +551,7 @@
 
 void vm_direct_exit(int code) {
   notify_vm_shutdown();
+  os::wait_for_keypress_at_exit();
   ::exit(code);
 }
 
@@ -577,11 +578,13 @@
 void vm_shutdown()
 {
   vm_perform_shutdown_actions();
+  os::wait_for_keypress_at_exit();
   os::shutdown();
 }
 
 void vm_abort(bool dump_core) {
   vm_perform_shutdown_actions();
+  os::wait_for_keypress_at_exit();
   os::abort(dump_core);
   ShouldNotReachHere();
 }
--- a/hotspot/src/share/vm/runtime/os.hpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/share/vm/runtime/os.hpp	Mon Feb 28 14:19:52 2011 +0100
@@ -492,6 +492,12 @@
   static void print_location(outputStream* st, intptr_t x, bool verbose = false);
   static size_t lasterror(char *buf, size_t len);
 
+  // Determines whether the calling process is being debugged by a user-mode debugger.
+  static bool is_debugger_attached();
+
+  // wait for a key press if PauseAtExit is set
+  static void wait_for_keypress_at_exit(void);
+
   // The following two functions are used by fatal error handler to trace
   // native (C) frames. They are not part of frame.hpp/frame.cpp because
   // frame.hpp/cpp assume thread is JavaThread, and also because different
--- a/hotspot/src/share/vm/runtime/thread.cpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/share/vm/runtime/thread.cpp	Mon Feb 28 14:19:52 2011 +0100
@@ -3644,6 +3644,7 @@
   if (ShowMessageBoxOnError && is_error_reported()) {
     os::infinite_sleep();
   }
+  os::wait_for_keypress_at_exit();
 
   if (JDK_Version::is_jdk12x_version()) {
     // We are the last thread running, so check if finalizers should be run.
--- a/hotspot/src/share/vm/utilities/vmError.cpp	Sat Feb 26 13:33:23 2011 -0500
+++ b/hotspot/src/share/vm/utilities/vmError.cpp	Mon Feb 28 14:19:52 2011 +0100
@@ -802,7 +802,7 @@
     first_error_tid = mytid;
     set_error_reported();
 
-    if (ShowMessageBoxOnError) {
+    if (ShowMessageBoxOnError || PauseAtExit) {
       show_message_box(buffer, sizeof(buffer));
 
       // User has asked JVM to abort. Reset ShowMessageBoxOnError so the