Reputation: 1077
I have a server written in Java and we start and stop the JVM for it under native code. I'm having a problem that is specific to that environment, so I want to start the JVM with remote debugging turned on.
Here are my JVM options:
char** vmargs = NULL;
int vmargc = 4;
{
// Configure the VM Args for the JVM
//Log( "vmargs: %d\n",vma-2);
vmargs = new char*[vmargc];
vmargs[0] = new char[1024];
vmargs[1] = new char[1024];
vmargs[2] = new char[1024];
vmargs[3] = new char[1024];
_snprintf_s(vmargs[0], 1024,_TRUNCATE, "-Djava.library.path=%s\\bin\\lib", root.c_str());
_snprintf_s(vmargs[1], 1024,_TRUNCATE, "-Djava.class.path=%s\\bin\\%s", root.c_str(), JarName);
_snprintf_s(vmargs[2], 1024,_TRUNCATE, "-Xrs");
_snprintf_s(vmargs[3], 1024,_TRUNCATE, "-Xdebug -Xrunjdwp:transport=dt_socket,address=8989,server=y,suspend=y");
Log( "vmarg %d:%s.\n",0,vmargs[0]);
Log( "vmarg %d:%s.\n",1,vmargs[1]);
Log( "vmarg %d:%s.\n",2,vmargs[2]);
Log( "vmarg %d:%s.\n",3,vmargs[3]);
}
As you can see I'm using "-Xdebug -Xrunjdwp:transport=dt_socket,address=8989,server=y,suspend=y
". These options are feed into the JVM by:
int optNum = 0;
for (int g=0; g<vmargc; ++g) {
options[optNum++].optionString = vmargs[g];
Log("Added vmarg: %s\n", options[g].optionString);
}
vm_args.version = JNI_VERSION_1_4; //JNI Version 1.4 and above
vm_args.options = options;
vm_args.nOptions = optNum;
vm_args.ignoreUnrecognized = JNI_TRUE;
//Create the JVM
res = createJVM(&vm, (void **)&env, &vm_args);
Unfortunately the JVM neither waits for the debugger to attach or accepts connections on port 8989 (connection refused). I've tried other ports, but it simply seems that the JVM is ignoring my options.
The JVM is starting the server correctly, I can connect a client to it.
Any thoughts as to what I'm doing wrong?
Upvotes: 1
Views: 850
Reputation: 1077
Figured it out. Each argument must have its char* array entry or it confuses the JVM. The corrected code reads:
char** vmargs = NULL;
int vmargc = 5;
{
// Configure the VM Args for the JVM
vmargs = new char*[vmargc];
vmargs[0] = new char[1024];
vmargs[1] = new char[1024];
vmargs[2] = new char[1024];
vmargs[3] = new char[1024];
vmargs[4] = new char[1024];
_snprintf_s(vmargs[0], 1024,_TRUNCATE, "-Djava.library.path=%s\\bin\\lib", root.c_str());
_snprintf_s(vmargs[1], 1024,_TRUNCATE, "-Djava.class.path=%s\\bin\\%s", root.c_str(), JarName);
_snprintf_s(vmargs[2], 1024,_TRUNCATE, "-Xrs");
_snprintf_s(vmargs[3], 1024,_TRUNCATE, "-Xdebug");
_snprintf_s(vmargs[4], 1024,_TRUNCATE, "-Xrunjdwp:transport=dt_socket,address=8989,server=y,suspend=y");
Log( "vmarg %d:%s.\n",0,vmargs[0]);
Log( "vmarg %d:%s.\n",1,vmargs[1]);
Log( "vmarg %d:%s.\n",2,vmargs[2]);
Log( "vmarg %d:%s.\n",3,vmargs[3]);
Log( "vmarg %d:%s.\n",4,vmargs[4]);
}
Separating -Xdebug from -Xrunjdwp allows the JVM to recognize both of them. The JVM was not parsing them into two options when they shared a char*, but thought they were one option.
Upvotes: 1