8214892: Delayed starting of debugging via jcmd

Reviewed-by: cjplummer, clanger
This commit is contained in:
Ralf Schmelter 2018-12-12 11:34:08 +00:00 committed by Christoph Langer
parent 7223ed2205
commit 8c4f51f666
4 changed files with 242 additions and 1 deletions
src
hotspot/share/services
jdk.jdwp.agent/share/native/libjdwp
test/jdk/com/sun/jdi

@ -37,6 +37,7 @@
#include "runtime/fieldDescriptor.inline.hpp"
#include "runtime/flags/jvmFlag.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/os.hpp"
#include "services/diagnosticArgument.hpp"
@ -123,6 +124,11 @@ void DCmdRegistrant::register_dcmds(){
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStopRemoteDCmd>(jmx_agent_export_flags, true,false));
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JMXStatusDCmd>(jmx_agent_export_flags, true,false));
// Debug on cmd (only makes sense with JVMTI since the agentlib needs it).
#if INCLUDE_JVMTI
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<DebugOnCmdStartDCmd>(full_export, true, false));
#endif // INCLUDE_JVMTI
}
#ifndef HAVE_EXTRA_DCMD
@ -1054,3 +1060,43 @@ void TouchedMethodsDCmd::execute(DCmdSource source, TRAPS) {
int TouchedMethodsDCmd::num_arguments() {
return 0;
}
#if INCLUDE_JVMTI
extern "C" typedef char const* (JNICALL *debugInit_startDebuggingViaCommandPtr)(JNIEnv* env, jthread thread, char const** transport_name,
char const** address, jboolean* first_start);
static debugInit_startDebuggingViaCommandPtr dvc_start_ptr = NULL;
DebugOnCmdStartDCmd::DebugOnCmdStartDCmd(outputStream* output, bool heap) : DCmdWithParser(output, heap) {
}
void DebugOnCmdStartDCmd::execute(DCmdSource source, TRAPS) {
char const* transport = NULL;
char const* addr = NULL;
jboolean is_first_start = JNI_FALSE;
JavaThread* thread = (JavaThread*) THREAD;
jthread jt = JNIHandles::make_local(thread->threadObj());
ThreadToNativeFromVM ttn(thread);
const char *error = "Could not find jdwp agent.";
if (!dvc_start_ptr) {
for (AgentLibrary* agent = Arguments::agents(); agent != NULL; agent = agent->next()) {
if ((strcmp("jdwp", agent->name()) == 0) && (dvc_start_ptr == NULL)) {
char const* func = "debugInit_startDebuggingViaCommand";
dvc_start_ptr = (debugInit_startDebuggingViaCommandPtr) os::find_agent_function(agent, false, &func, 1);
}
}
}
if (dvc_start_ptr) {
error = dvc_start_ptr(thread->jni_environment(), jt, &transport, &addr, &is_first_start);
}
if (error != NULL) {
output()->print_cr("Debugging has not been started: %s", error);
} else {
output()->print_cr(is_first_start ? "Debugging has been started." : "Debugging is already active.");
output()->print_cr("Transport : %s", transport ? transport : "#unknown");
output()->print_cr("Address : %s", addr ? addr : "#unknown");
}
}
#endif // INCLUDE_JVMTI

@ -867,4 +867,26 @@ public:
virtual void execute(DCmdSource source, TRAPS);
};
#if INCLUDE_JVMTI
class DebugOnCmdStartDCmd : public DCmdWithParser {
public:
DebugOnCmdStartDCmd(outputStream* output, bool heap);
static const char* name() {
return "VM.start_java_debugging";
}
static const char* description() {
return "Starts up the Java debugging if the jdwp agentlib was enabled with the option onjcmd=y.";
}
static const char* impact() {
return "High: Switches the VM into Java debug mode.";
}
static const JavaPermission permission() {
JavaPermission p = { "java.lang.management.ManagementPermission", "monitor", NULL };
return p;
}
static int num_arguments() { return 0; }
virtual void execute(DCmdSource source, TRAPS);
};
#endif // INCLUDE_JVMTI
#endif // SHARE_VM_SERVICES_DIAGNOSTICCOMMAND_HPP

@ -82,6 +82,9 @@ static unsigned logflags = 0; /* Log flags */
static char *names; /* strings derived from OnLoad options */
static jboolean allowStartViaJcmd = JNI_FALSE; /* if true we allow the debugging to be started via a jcmd */
static jboolean startedViaJcmd = JNI_FALSE; /* if false, we have not yet started debugging via a jcmd */
/*
* Elements of the transports bag
*/
@ -870,6 +873,7 @@ printUsage(void)
"launch=<command line> run debugger on event none\n"
"onthrow=<exception name> debug on throw none\n"
"onuncaught=y|n debug on any uncaught? n\n"
"onjcmd=y|n start debug via jcmd? n\n"
"timeout=<timeout value> for listen/attach in milliseconds n\n"
"mutf8=y|n output modified utf-8 n\n"
"quiet=y|n control over terminal messages n\n"));
@ -1010,6 +1014,7 @@ parseOptions(char *options)
int length;
char *str;
char *errmsg;
jboolean onJcmd = JNI_FALSE;
/* Set defaults */
gdata->assertOn = DEFAULT_ASSERT_ON;
@ -1229,6 +1234,10 @@ parseOptions(char *options)
if ( !get_boolean(&str, &useStandardAlloc) ) {
goto syntax_error;
}
} else if (strcmp(buf, "onjcmd") == 0) {
if (!get_boolean(&str, &onJcmd)) {
goto syntax_error;
}
} else {
goto syntax_error;
}
@ -1254,7 +1263,6 @@ parseOptions(char *options)
goto bad_option_with_errmsg;
}
if (!isServer) {
jboolean specified = bagEnumerateOver(transports, checkAddress, NULL);
if (!specified) {
@ -1280,6 +1288,20 @@ parseOptions(char *options)
}
}
if (onJcmd) {
if (launchOnInit != NULL) {
errmsg = "Cannot combine onjcmd and launch suboptions";
goto bad_option_with_errmsg;
}
if (!isServer) {
errmsg = "Can only use onjcmd with server=y";
goto bad_option_with_errmsg;
}
suspendOnInit = JNI_FALSE;
initOnStartup = JNI_FALSE;
allowStartViaJcmd = JNI_TRUE;
}
return JNI_TRUE;
syntax_error:
@ -1348,3 +1370,45 @@ debugInit_exit(jvmtiError error, const char *msg)
// Last chance to die, this kills the entire process.
forceExit(EXIT_JVMTI_ERROR);
}
static jboolean getFirstTransport(void *item, void *arg)
{
TransportSpec** store = arg;
*store = item;
return JNI_FALSE; /* Want the first */
}
/* Call to start up debugging. */
JNIEXPORT char const* JNICALL debugInit_startDebuggingViaCommand(JNIEnv* env, jthread thread, char const** transport_name,
char const** address, jboolean* first_start) {
jboolean is_first_start = JNI_FALSE;
TransportSpec* spec = NULL;
if (!vmInitialized) {
return "Not yet initialized. Try again later.";
}
if (!allowStartViaJcmd) {
return "Starting debugging via jcmd was not enabled via the onjcmd option of the jdwp agent.";
}
if (!startedViaJcmd) {
startedViaJcmd = JNI_TRUE;
is_first_start = JNI_TRUE;
initialize(env, thread, EI_VM_INIT);
}
bagEnumerateOver(transports, getFirstTransport, &spec);
if ((spec != NULL) && (transport_name != NULL) && (address != NULL)) {
*transport_name = spec->name;
*address = spec->address;
}
if (first_start != NULL) {
*first_start = is_first_start;
}
return NULL;
}

@ -0,0 +1,109 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, SAP SE. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8214892
* @summary Test that the onjcmd option of the jdwp agent works.
*
* @author Ralf Schmelter
*
* @library /test/lib
* @run compile --add-exports java.base/jdk.internal.vm=ALL-UNNAMED -g OnJcmdTest.java
* @run main/othervm --add-exports java.base/jdk.internal.vm=ALL-UNNAMED -agentlib:jdwp=transport=dt_socket,address=localhost:0,onjcmd=y,server=y OnJcmdTest
*/
import java.lang.reflect.Method;
import java.util.Properties;
import jdk.internal.vm.VMSupport;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
public class OnJcmdTest {
private static String getListenerAddress() throws Exception {
Properties props = VMSupport.getAgentProperties();
return props.getProperty("sun.jdwp.listenerAddress", null);
}
public static void main(String[] args) throws Throwable {
// First check if we get the expected errors.
OutputAnalyzer output = ProcessTools.executeTestJvm(
"-agentlib:jdwp=transport=dt_socket,address=any,onjcmd=y");
output.shouldContain("Can only use onjcmd with server=y");
output.shouldHaveExitValue(1);
output = ProcessTools.executeTestJvm(
"-agentlib:jdwp=transport=dt_socket,address=any,onjcmd=y,onthrow=a,launch=a");
output.shouldContain("Cannot combine onjcmd and launch suboptions");
output.shouldHaveExitValue(1);
// Make sure debugging is not yet started.
String prop = getListenerAddress();
if (prop != null) {
throw new RuntimeException("Listener address was set to " + prop);
}
// Now start it (test that it is OK to do this more than once).
for (int i = 0; i < 3; ++i) {
String jcmd = JDKToolFinder.getJDKTool("jcmd");
output = ProcessTools.executeProcess(jcmd,
Long.toString(ProcessTools.getProcessId()),
"VM.start_java_debugging");
String exp_str = i == 0 ? "Debugging has been started." :
"Debugging is already active.";
output.shouldContain(exp_str);
output.shouldContain("Transport : dt_socket");
output.shouldHaveExitValue(0);
}
// Now the property should be set, as the jdwp agent waits for a
// connection.
long t1 = System.currentTimeMillis();
long t2 = t1;
while(t2 - t1 < 4000) {
prop = getListenerAddress();
if (prop != null) {
if (prop.equals("localhost:0")) {
throw new RuntimeException("Port was not expanded");
} else if (!prop.startsWith("dt_socket:")) {
throw new RuntimeException("Invalid transport prop " + prop);
}
return;
}
Thread.sleep(50);
t2 = System.currentTimeMillis();
}
throw new RuntimeException("Debugging backend didn't start");
}
}