src/os/solaris/launcher/java.c

Fri, 27 Feb 2009 13:27:09 -0800

author
twisti
date
Fri, 27 Feb 2009 13:27:09 -0800
changeset 1040
98cb887364d3
parent 905
ad8c8ca4ab0f
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never

duke@435 1 /*
xdono@905 2 * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 /*
duke@435 26 * Gamma (Hotspot internal engineering test) launcher based on 1.6.0-b28 JDK,
duke@435 27 * search "GAMMA" for gamma specific changes.
duke@435 28 *
duke@435 29 * GAMMA: gamma launcher is much simpler than regular java launcher in that
duke@435 30 * JVM is either statically linked in or it is installed in the
duke@435 31 * same directory where the launcher exists, so we don't have to
duke@435 32 * worry about choosing the right JVM based on command line flag, jar
duke@435 33 * file and/or ergonomics. Intead of removing unused logic from source
duke@435 34 * they are commented out with #ifndef GAMMA, hopefully it'll be easier
duke@435 35 * to maintain this file in sync with regular JDK launcher.
duke@435 36 */
duke@435 37
duke@435 38 /*
duke@435 39 * Shared source for 'java' command line tool.
duke@435 40 *
duke@435 41 * If JAVA_ARGS is defined, then acts as a launcher for applications. For
duke@435 42 * instance, the JDK command line tools such as javac and javadoc (see
duke@435 43 * makefiles for more details) are built with this program. Any arguments
duke@435 44 * prefixed with '-J' will be passed directly to the 'java' command.
duke@435 45 */
duke@435 46
duke@435 47 #ifdef GAMMA
duke@435 48 # ifdef JAVA_ARGS
duke@435 49 # error Do NOT define JAVA_ARGS when building gamma launcher
duke@435 50 # endif
duke@435 51 # if !defined(LINK_INTO_AOUT) && !defined(LINK_INTO_LIBJVM)
duke@435 52 # error Either LINK_INTO_AOUT or LINK_INTO_LIBJVM must be defined
duke@435 53 # endif
duke@435 54 #endif
duke@435 55
duke@435 56 /*
duke@435 57 * One job of the launcher is to remove command line options which the
duke@435 58 * vm does not understand and will not process. These options include
duke@435 59 * options which select which style of vm is run (e.g. -client and
duke@435 60 * -server) as well as options which select the data model to use.
duke@435 61 * Additionally, for tools which invoke an underlying vm "-J-foo"
duke@435 62 * options are turned into "-foo" options to the vm. This option
duke@435 63 * filtering is handled in a number of places in the launcher, some of
duke@435 64 * it in machine-dependent code. In this file, the function
duke@435 65 * CheckJVMType removes vm style options and TranslateDashJArgs
duke@435 66 * removes "-J" prefixes. On unix platforms, the
duke@435 67 * CreateExecutionEnvironment function from the unix java_md.c file
duke@435 68 * processes and removes -d<n> options. However, in case
duke@435 69 * CreateExecutionEnvironment does not need to exec because
duke@435 70 * LD_LIBRARY_PATH is set acceptably and the data model does not need
duke@435 71 * to be changed, ParseArguments will screen out the redundant -d<n>
duke@435 72 * options and prevent them from being passed to the vm; this is done
duke@435 73 * by using the machine-dependent call
duke@435 74 * RemovableMachineDependentOption.
duke@435 75 */
duke@435 76
duke@435 77 #include <stdio.h>
duke@435 78 #include <stdlib.h>
duke@435 79 #include <string.h>
duke@435 80
duke@435 81 #include <jni.h>
duke@435 82 #include "java.h"
duke@435 83
duke@435 84 #ifndef GAMMA
duke@435 85 #include "manifest_info.h"
duke@435 86 #include "version_comp.h"
duke@435 87 #endif
duke@435 88
duke@435 89 #ifndef FULL_VERSION
duke@435 90 #define FULL_VERSION JDK_MAJOR_VERSION "." JDK_MINOR_VERSION
duke@435 91 #endif
duke@435 92
duke@435 93 /*
duke@435 94 * The following environment variable is used to influence the behavior
duke@435 95 * of the jre exec'd through the SelectVersion routine. The command line
duke@435 96 * options which specify the version are not passed to the exec'd version,
duke@435 97 * because that jre may be an older version which wouldn't recognize them.
duke@435 98 * This environment variable is known to this (and later) version and serves
duke@435 99 * to suppress the version selection code. This is not only for efficiency,
duke@435 100 * but also for correctness, since any command line options have been
duke@435 101 * removed which would cause any value found in the manifest to be used.
duke@435 102 * This would be incorrect because the command line options are defined
duke@435 103 * to take precedence.
duke@435 104 *
duke@435 105 * The value associated with this environment variable is the MainClass
duke@435 106 * name from within the executable jar file (if any). This is strictly a
duke@435 107 * performance enhancement to avoid re-reading the jar file manifest.
duke@435 108 *
duke@435 109 * A NOTE TO DEVELOPERS: For performance reasons it is important that
duke@435 110 * the program image remain relatively small until after SelectVersion
duke@435 111 * CreateExecutionEnvironment have finished their possibly recursive
duke@435 112 * processing. Watch everything, but resist all temptations to use Java
duke@435 113 * interfaces.
duke@435 114 */
duke@435 115 #define ENV_ENTRY "_JAVA_VERSION_SET"
duke@435 116
duke@435 117 static jboolean printVersion = JNI_FALSE; /* print and exit */
duke@435 118 static jboolean showVersion = JNI_FALSE; /* print but continue */
duke@435 119 static char *progname;
duke@435 120 jboolean _launcher_debug = JNI_FALSE;
duke@435 121
duke@435 122 /*
duke@435 123 * List of VM options to be specified when the VM is created.
duke@435 124 */
duke@435 125 static JavaVMOption *options;
duke@435 126 static int numOptions, maxOptions;
duke@435 127
duke@435 128 /*
duke@435 129 * Prototypes for functions internal to launcher.
duke@435 130 */
duke@435 131 static void AddOption(char *str, void *info);
duke@435 132 static void SetClassPath(char *s);
duke@435 133 static void SelectVersion(int argc, char **argv, char **main_class);
duke@435 134 static jboolean ParseArguments(int *pargc, char ***pargv, char **pjarfile,
duke@435 135 char **pclassname, int *pret);
duke@435 136 static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
duke@435 137 InvocationFunctions *ifn);
duke@435 138 static jstring NewPlatformString(JNIEnv *env, char *s);
duke@435 139 static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
duke@435 140 static jclass LoadClass(JNIEnv *env, char *name);
duke@435 141 static jstring GetMainClassName(JNIEnv *env, char *jarname);
duke@435 142 static void SetJavaCommandLineProp(char* classname, char* jarfile, int argc, char** argv);
duke@435 143 #ifdef GAMMA
duke@435 144 static void SetJavaLauncherProp(void);
duke@435 145 #endif
duke@435 146
duke@435 147 #ifdef JAVA_ARGS
duke@435 148 static void TranslateDashJArgs(int *pargc, char ***pargv);
duke@435 149 static jboolean AddApplicationOptions(void);
duke@435 150 #endif
duke@435 151
duke@435 152 static void PrintJavaVersion(JNIEnv *env);
duke@435 153 static void PrintUsage(void);
duke@435 154 static jint PrintXUsage(void);
duke@435 155
duke@435 156 static void SetPaths(int argc, char **argv);
duke@435 157
duke@435 158 /* Maximum supported entries from jvm.cfg. */
duke@435 159 #define INIT_MAX_KNOWN_VMS 10
duke@435 160 /* Values for vmdesc.flag */
duke@435 161 #define VM_UNKNOWN -1
duke@435 162 #define VM_KNOWN 0
duke@435 163 #define VM_ALIASED_TO 1
duke@435 164 #define VM_WARN 2
duke@435 165 #define VM_ERROR 3
duke@435 166 #define VM_IF_SERVER_CLASS 4
duke@435 167 #define VM_IGNORE 5
duke@435 168 struct vmdesc {
duke@435 169 char *name;
duke@435 170 int flag;
duke@435 171 char *alias;
duke@435 172 char *server_class;
duke@435 173 };
duke@435 174 static struct vmdesc *knownVMs = NULL;
duke@435 175 static int knownVMsCount = 0;
duke@435 176 static int knownVMsLimit = 0;
duke@435 177
duke@435 178 static void GrowKnownVMs();
duke@435 179 static int KnownVMIndex(const char* name);
duke@435 180 static void FreeKnownVMs();
duke@435 181
duke@435 182 jboolean ServerClassMachine();
duke@435 183
duke@435 184 /* flag which if set suppresses error messages from the launcher */
duke@435 185 static int noExitErrorMessage = 0;
duke@435 186
duke@435 187 /*
duke@435 188 * Entry point.
duke@435 189 */
duke@435 190 int
duke@435 191 main(int argc, char ** argv)
duke@435 192 {
duke@435 193 JavaVM *vm = 0;
duke@435 194 JNIEnv *env = 0;
duke@435 195 char *jarfile = 0;
duke@435 196 char *classname = 0;
duke@435 197 char *s = 0;
duke@435 198 char *main_class = NULL;
duke@435 199 jstring mainClassName;
duke@435 200 jclass mainClass;
duke@435 201 jmethodID mainID;
duke@435 202 jobjectArray mainArgs;
duke@435 203 int ret;
duke@435 204 InvocationFunctions ifn;
duke@435 205 jlong start, end;
duke@435 206 char jrepath[MAXPATHLEN], jvmpath[MAXPATHLEN];
duke@435 207 char ** original_argv = argv;
duke@435 208
duke@435 209 /*
duke@435 210 * Error message to print or display; by default the message will
duke@435 211 * only be displayed in a window.
duke@435 212 */
duke@435 213 char * message = "Fatal exception occurred. Program will exit.";
duke@435 214 jboolean messageDest = JNI_FALSE;
duke@435 215
duke@435 216 if (getenv("_JAVA_LAUNCHER_DEBUG") != 0) {
duke@435 217 _launcher_debug = JNI_TRUE;
duke@435 218 printf("----_JAVA_LAUNCHER_DEBUG----\n");
duke@435 219 }
duke@435 220
duke@435 221 #ifndef GAMMA
duke@435 222 /*
duke@435 223 * Make sure the specified version of the JRE is running.
duke@435 224 *
duke@435 225 * There are three things to note about the SelectVersion() routine:
duke@435 226 * 1) If the version running isn't correct, this routine doesn't
duke@435 227 * return (either the correct version has been exec'd or an error
duke@435 228 * was issued).
duke@435 229 * 2) Argc and Argv in this scope are *not* altered by this routine.
duke@435 230 * It is the responsibility of subsequent code to ignore the
duke@435 231 * arguments handled by this routine.
duke@435 232 * 3) As a side-effect, the variable "main_class" is guaranteed to
duke@435 233 * be set (if it should ever be set). This isn't exactly the
duke@435 234 * poster child for structured programming, but it is a small
duke@435 235 * price to pay for not processing a jar file operand twice.
duke@435 236 * (Note: This side effect has been disabled. See comment on
duke@435 237 * bugid 5030265 below.)
duke@435 238 */
duke@435 239 SelectVersion(argc, argv, &main_class);
duke@435 240 #endif /* ifndef GAMMA */
duke@435 241
duke@435 242 /* copy original argv */
duke@435 243 {
duke@435 244 int i;
duke@435 245 original_argv = (char**)MemAlloc(sizeof(char*)*(argc+1));
duke@435 246 for(i = 0; i < argc+1; i++)
duke@435 247 original_argv[i] = argv[i];
duke@435 248 }
duke@435 249
duke@435 250 CreateExecutionEnvironment(&argc, &argv,
duke@435 251 jrepath, sizeof(jrepath),
duke@435 252 jvmpath, sizeof(jvmpath),
duke@435 253 original_argv);
duke@435 254 ifn.CreateJavaVM = 0;
duke@435 255 ifn.GetDefaultJavaVMInitArgs = 0;
duke@435 256
duke@435 257 if (_launcher_debug)
duke@435 258 start = CounterGet();
duke@435 259 if (!LoadJavaVM(jvmpath, &ifn)) {
duke@435 260 exit(6);
duke@435 261 }
duke@435 262 if (_launcher_debug) {
duke@435 263 end = CounterGet();
duke@435 264 printf("%ld micro seconds to LoadJavaVM\n",
duke@435 265 (long)(jint)Counter2Micros(end-start));
duke@435 266 }
duke@435 267
duke@435 268 #ifdef JAVA_ARGS /* javac, jar and friends. */
duke@435 269 progname = "java";
duke@435 270 #else /* java, oldjava, javaw and friends */
duke@435 271 #ifdef PROGNAME
duke@435 272 progname = PROGNAME;
duke@435 273 #else
duke@435 274 progname = *argv;
duke@435 275 if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) {
duke@435 276 progname = s + 1;
duke@435 277 }
duke@435 278 #endif /* PROGNAME */
duke@435 279 #endif /* JAVA_ARGS */
duke@435 280 ++argv;
duke@435 281 --argc;
duke@435 282
duke@435 283 #ifdef JAVA_ARGS
duke@435 284 /* Preprocess wrapper arguments */
duke@435 285 TranslateDashJArgs(&argc, &argv);
duke@435 286 if (!AddApplicationOptions()) {
duke@435 287 exit(1);
duke@435 288 }
duke@435 289 #endif
duke@435 290
duke@435 291 /* Set default CLASSPATH */
duke@435 292 if ((s = getenv("CLASSPATH")) == 0) {
duke@435 293 s = ".";
duke@435 294 }
duke@435 295 #ifndef JAVA_ARGS
duke@435 296 SetClassPath(s);
duke@435 297 #endif
duke@435 298
duke@435 299 /*
duke@435 300 * Parse command line options; if the return value of
duke@435 301 * ParseArguments is false, the program should exit.
duke@435 302 */
duke@435 303 if (!ParseArguments(&argc, &argv, &jarfile, &classname, &ret)) {
duke@435 304 exit(ret);
duke@435 305 }
duke@435 306
duke@435 307 /* Override class path if -jar flag was specified */
duke@435 308 if (jarfile != 0) {
duke@435 309 SetClassPath(jarfile);
duke@435 310 }
duke@435 311
duke@435 312 /* set the -Dsun.java.command pseudo property */
duke@435 313 SetJavaCommandLineProp(classname, jarfile, argc, argv);
duke@435 314
duke@435 315 #ifdef GAMMA
duke@435 316 /* Set the -Dsun.java.launcher pseudo property */
duke@435 317 SetJavaLauncherProp();
duke@435 318 #endif
duke@435 319
duke@435 320 /*
duke@435 321 * Done with all command line processing and potential re-execs so
duke@435 322 * clean up the environment.
duke@435 323 */
duke@435 324 (void)UnsetEnv(ENV_ENTRY);
duke@435 325
duke@435 326 /* Initialize the virtual machine */
duke@435 327
duke@435 328 if (_launcher_debug)
duke@435 329 start = CounterGet();
duke@435 330 if (!InitializeJVM(&vm, &env, &ifn)) {
duke@435 331 ReportErrorMessage("Could not create the Java virtual machine.",
duke@435 332 JNI_TRUE);
duke@435 333 exit(1);
duke@435 334 }
duke@435 335
duke@435 336 if (printVersion || showVersion) {
duke@435 337 PrintJavaVersion(env);
duke@435 338 if ((*env)->ExceptionOccurred(env)) {
duke@435 339 ReportExceptionDescription(env);
duke@435 340 goto leave;
duke@435 341 }
duke@435 342 if (printVersion) {
duke@435 343 ret = 0;
duke@435 344 message = NULL;
duke@435 345 goto leave;
duke@435 346 }
duke@435 347 if (showVersion) {
duke@435 348 fprintf(stderr, "\n");
duke@435 349 }
duke@435 350 }
duke@435 351
duke@435 352 /* If the user specified neither a class name nor a JAR file */
duke@435 353 if (jarfile == 0 && classname == 0) {
duke@435 354 PrintUsage();
duke@435 355 message = NULL;
duke@435 356 goto leave;
duke@435 357 }
duke@435 358
duke@435 359 #ifndef GAMMA
duke@435 360 FreeKnownVMs(); /* after last possible PrintUsage() */
duke@435 361 #endif
duke@435 362
duke@435 363 if (_launcher_debug) {
duke@435 364 end = CounterGet();
duke@435 365 printf("%ld micro seconds to InitializeJVM\n",
duke@435 366 (long)(jint)Counter2Micros(end-start));
duke@435 367 }
duke@435 368
duke@435 369 /* At this stage, argc/argv have the applications' arguments */
duke@435 370 if (_launcher_debug) {
duke@435 371 int i = 0;
duke@435 372 printf("Main-Class is '%s'\n", classname ? classname : "");
duke@435 373 printf("Apps' argc is %d\n", argc);
duke@435 374 for (; i < argc; i++) {
duke@435 375 printf(" argv[%2d] = '%s'\n", i, argv[i]);
duke@435 376 }
duke@435 377 }
duke@435 378
duke@435 379 ret = 1;
duke@435 380
duke@435 381 /*
duke@435 382 * Get the application's main class.
duke@435 383 *
duke@435 384 * See bugid 5030265. The Main-Class name has already been parsed
duke@435 385 * from the manifest, but not parsed properly for UTF-8 support.
duke@435 386 * Hence the code here ignores the value previously extracted and
duke@435 387 * uses the pre-existing code to reextract the value. This is
duke@435 388 * possibly an end of release cycle expedient. However, it has
duke@435 389 * also been discovered that passing some character sets through
duke@435 390 * the environment has "strange" behavior on some variants of
duke@435 391 * Windows. Hence, maybe the manifest parsing code local to the
duke@435 392 * launcher should never be enhanced.
duke@435 393 *
duke@435 394 * Hence, future work should either:
duke@435 395 * 1) Correct the local parsing code and verify that the
duke@435 396 * Main-Class attribute gets properly passed through
duke@435 397 * all environments,
duke@435 398 * 2) Remove the vestages of maintaining main_class through
duke@435 399 * the environment (and remove these comments).
duke@435 400 */
duke@435 401 if (jarfile != 0) {
duke@435 402 mainClassName = GetMainClassName(env, jarfile);
duke@435 403 if ((*env)->ExceptionOccurred(env)) {
duke@435 404 ReportExceptionDescription(env);
duke@435 405 goto leave;
duke@435 406 }
duke@435 407 if (mainClassName == NULL) {
duke@435 408 const char * format = "Failed to load Main-Class manifest "
duke@435 409 "attribute from\n%s";
duke@435 410 message = (char*)MemAlloc((strlen(format) + strlen(jarfile)) *
duke@435 411 sizeof(char));
duke@435 412 sprintf(message, format, jarfile);
duke@435 413 messageDest = JNI_TRUE;
duke@435 414 goto leave;
duke@435 415 }
duke@435 416 classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);
duke@435 417 if (classname == NULL) {
duke@435 418 ReportExceptionDescription(env);
duke@435 419 goto leave;
duke@435 420 }
duke@435 421 mainClass = LoadClass(env, classname);
twisti@1040 422 if(mainClass == NULL) { /* exception occurred */
duke@435 423 ReportExceptionDescription(env);
duke@435 424 message = "Could not find the main class. Program will exit.";
duke@435 425 goto leave;
duke@435 426 }
duke@435 427 (*env)->ReleaseStringUTFChars(env, mainClassName, classname);
duke@435 428 } else {
duke@435 429 mainClassName = NewPlatformString(env, classname);
duke@435 430 if (mainClassName == NULL) {
duke@435 431 const char * format = "Failed to load Main Class: %s";
duke@435 432 message = (char *)MemAlloc((strlen(format) + strlen(classname)) *
duke@435 433 sizeof(char) );
duke@435 434 sprintf(message, format, classname);
duke@435 435 messageDest = JNI_TRUE;
duke@435 436 goto leave;
duke@435 437 }
duke@435 438 classname = (char *)(*env)->GetStringUTFChars(env, mainClassName, 0);
duke@435 439 if (classname == NULL) {
duke@435 440 ReportExceptionDescription(env);
duke@435 441 goto leave;
duke@435 442 }
duke@435 443 mainClass = LoadClass(env, classname);
twisti@1040 444 if(mainClass == NULL) { /* exception occurred */
duke@435 445 ReportExceptionDescription(env);
duke@435 446 message = "Could not find the main class. Program will exit.";
duke@435 447 goto leave;
duke@435 448 }
duke@435 449 (*env)->ReleaseStringUTFChars(env, mainClassName, classname);
duke@435 450 }
duke@435 451
duke@435 452 /* Get the application's main method */
duke@435 453 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
duke@435 454 "([Ljava/lang/String;)V");
duke@435 455 if (mainID == NULL) {
duke@435 456 if ((*env)->ExceptionOccurred(env)) {
duke@435 457 ReportExceptionDescription(env);
duke@435 458 } else {
duke@435 459 message = "No main method found in specified class.";
duke@435 460 messageDest = JNI_TRUE;
duke@435 461 }
duke@435 462 goto leave;
duke@435 463 }
duke@435 464
duke@435 465 { /* Make sure the main method is public */
duke@435 466 jint mods;
duke@435 467 jmethodID mid;
duke@435 468 jobject obj = (*env)->ToReflectedMethod(env, mainClass,
duke@435 469 mainID, JNI_TRUE);
duke@435 470
duke@435 471 if( obj == NULL) { /* exception occurred */
duke@435 472 ReportExceptionDescription(env);
duke@435 473 goto leave;
duke@435 474 }
duke@435 475
duke@435 476 mid =
duke@435 477 (*env)->GetMethodID(env,
duke@435 478 (*env)->GetObjectClass(env, obj),
duke@435 479 "getModifiers", "()I");
duke@435 480 if ((*env)->ExceptionOccurred(env)) {
duke@435 481 ReportExceptionDescription(env);
duke@435 482 goto leave;
duke@435 483 }
duke@435 484
duke@435 485 mods = (*env)->CallIntMethod(env, obj, mid);
duke@435 486 if ((mods & 1) == 0) { /* if (!Modifier.isPublic(mods)) ... */
duke@435 487 message = "Main method not public.";
duke@435 488 messageDest = JNI_TRUE;
duke@435 489 goto leave;
duke@435 490 }
duke@435 491 }
duke@435 492
duke@435 493 /* Build argument array */
duke@435 494 mainArgs = NewPlatformStringArray(env, argv, argc);
duke@435 495 if (mainArgs == NULL) {
duke@435 496 ReportExceptionDescription(env);
duke@435 497 goto leave;
duke@435 498 }
duke@435 499
duke@435 500 /* Invoke main method. */
duke@435 501 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
duke@435 502
duke@435 503 /*
duke@435 504 * The launcher's exit code (in the absence of calls to
duke@435 505 * System.exit) will be non-zero if main threw an exception.
duke@435 506 */
duke@435 507 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
duke@435 508
duke@435 509 /*
duke@435 510 * Detach the main thread so that it appears to have ended when
duke@435 511 * the application's main method exits. This will invoke the
duke@435 512 * uncaught exception handler machinery if main threw an
duke@435 513 * exception. An uncaught exception handler cannot change the
duke@435 514 * launcher's return code except by calling System.exit.
duke@435 515 */
duke@435 516 if ((*vm)->DetachCurrentThread(vm) != 0) {
duke@435 517 message = "Could not detach main thread.";
duke@435 518 messageDest = JNI_TRUE;
duke@435 519 ret = 1;
duke@435 520 goto leave;
duke@435 521 }
duke@435 522
duke@435 523 message = NULL;
duke@435 524
duke@435 525 leave:
duke@435 526 /*
duke@435 527 * Wait for all non-daemon threads to end, then destroy the VM.
duke@435 528 * This will actually create a trivial new Java waiter thread
duke@435 529 * named "DestroyJavaVM", but this will be seen as a different
duke@435 530 * thread from the one that executed main, even though they are
duke@435 531 * the same C thread. This allows mainThread.join() and
duke@435 532 * mainThread.isAlive() to work as expected.
duke@435 533 */
duke@435 534 (*vm)->DestroyJavaVM(vm);
duke@435 535
duke@435 536 if(message != NULL && !noExitErrorMessage)
duke@435 537 ReportErrorMessage(message, messageDest);
duke@435 538 return ret;
duke@435 539 }
duke@435 540
duke@435 541
duke@435 542 #ifndef GAMMA
duke@435 543 /*
duke@435 544 * Checks the command line options to find which JVM type was
duke@435 545 * specified. If no command line option was given for the JVM type,
duke@435 546 * the default type is used. The environment variable
duke@435 547 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
duke@435 548 * checked as ways of specifying which JVM type to invoke.
duke@435 549 */
duke@435 550 char *
duke@435 551 CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
duke@435 552 int i, argi;
duke@435 553 int argc;
duke@435 554 char **newArgv;
duke@435 555 int newArgvIdx = 0;
duke@435 556 int isVMType;
duke@435 557 int jvmidx = -1;
duke@435 558 char *jvmtype = getenv("JDK_ALTERNATE_VM");
duke@435 559
duke@435 560 argc = *pargc;
duke@435 561
duke@435 562 /* To make things simpler we always copy the argv array */
duke@435 563 newArgv = MemAlloc((argc + 1) * sizeof(char *));
duke@435 564
duke@435 565 /* The program name is always present */
duke@435 566 newArgv[newArgvIdx++] = (*argv)[0];
duke@435 567
duke@435 568 for (argi = 1; argi < argc; argi++) {
duke@435 569 char *arg = (*argv)[argi];
duke@435 570 isVMType = 0;
duke@435 571
duke@435 572 #ifdef JAVA_ARGS
duke@435 573 if (arg[0] != '-') {
duke@435 574 newArgv[newArgvIdx++] = arg;
duke@435 575 continue;
duke@435 576 }
duke@435 577 #else
duke@435 578 if (strcmp(arg, "-classpath") == 0 ||
duke@435 579 strcmp(arg, "-cp") == 0) {
duke@435 580 newArgv[newArgvIdx++] = arg;
duke@435 581 argi++;
duke@435 582 if (argi < argc) {
duke@435 583 newArgv[newArgvIdx++] = (*argv)[argi];
duke@435 584 }
duke@435 585 continue;
duke@435 586 }
duke@435 587 if (arg[0] != '-') break;
duke@435 588 #endif
duke@435 589
duke@435 590 /* Did the user pass an explicit VM type? */
duke@435 591 i = KnownVMIndex(arg);
duke@435 592 if (i >= 0) {
duke@435 593 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
duke@435 594 isVMType = 1;
duke@435 595 *pargc = *pargc - 1;
duke@435 596 }
duke@435 597
duke@435 598 /* Did the user specify an "alternate" VM? */
duke@435 599 else if (strncmp(arg, "-XXaltjvm=", 10) == 0 || strncmp(arg, "-J-XXaltjvm=", 12) == 0) {
duke@435 600 isVMType = 1;
duke@435 601 jvmtype = arg+((arg[1]=='X')? 10 : 12);
duke@435 602 jvmidx = -1;
duke@435 603 }
duke@435 604
duke@435 605 if (!isVMType) {
duke@435 606 newArgv[newArgvIdx++] = arg;
duke@435 607 }
duke@435 608 }
duke@435 609
duke@435 610 /*
duke@435 611 * Finish copying the arguments if we aborted the above loop.
duke@435 612 * NOTE that if we aborted via "break" then we did NOT copy the
duke@435 613 * last argument above, and in addition argi will be less than
duke@435 614 * argc.
duke@435 615 */
duke@435 616 while (argi < argc) {
duke@435 617 newArgv[newArgvIdx++] = (*argv)[argi];
duke@435 618 argi++;
duke@435 619 }
duke@435 620
duke@435 621 /* argv is null-terminated */
duke@435 622 newArgv[newArgvIdx] = 0;
duke@435 623
duke@435 624 /* Copy back argv */
duke@435 625 *argv = newArgv;
duke@435 626 *pargc = newArgvIdx;
duke@435 627
duke@435 628 /* use the default VM type if not specified (no alias processing) */
duke@435 629 if (jvmtype == NULL) {
duke@435 630 char* result = knownVMs[0].name+1;
duke@435 631 /* Use a different VM type if we are on a server class machine? */
duke@435 632 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
duke@435 633 (ServerClassMachine() == JNI_TRUE)) {
duke@435 634 result = knownVMs[0].server_class+1;
duke@435 635 }
duke@435 636 if (_launcher_debug) {
duke@435 637 printf("Default VM: %s\n", result);
duke@435 638 }
duke@435 639 return result;
duke@435 640 }
duke@435 641
duke@435 642 /* if using an alternate VM, no alias processing */
duke@435 643 if (jvmidx < 0)
duke@435 644 return jvmtype;
duke@435 645
duke@435 646 /* Resolve aliases first */
duke@435 647 {
duke@435 648 int loopCount = 0;
duke@435 649 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
duke@435 650 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
duke@435 651
duke@435 652 if (loopCount > knownVMsCount) {
duke@435 653 if (!speculative) {
duke@435 654 ReportErrorMessage("Error: Corrupt jvm.cfg file; cycle in alias list.",
duke@435 655 JNI_TRUE);
duke@435 656 exit(1);
duke@435 657 } else {
duke@435 658 return "ERROR";
duke@435 659 /* break; */
duke@435 660 }
duke@435 661 }
duke@435 662
duke@435 663 if (nextIdx < 0) {
duke@435 664 if (!speculative) {
duke@435 665 ReportErrorMessage2("Error: Unable to resolve VM alias %s",
duke@435 666 knownVMs[jvmidx].alias, JNI_TRUE);
duke@435 667 exit(1);
duke@435 668 } else {
duke@435 669 return "ERROR";
duke@435 670 }
duke@435 671 }
duke@435 672 jvmidx = nextIdx;
duke@435 673 jvmtype = knownVMs[jvmidx].name+1;
duke@435 674 loopCount++;
duke@435 675 }
duke@435 676 }
duke@435 677
duke@435 678 switch (knownVMs[jvmidx].flag) {
duke@435 679 case VM_WARN:
duke@435 680 if (!speculative) {
duke@435 681 fprintf(stderr, "Warning: %s VM not supported; %s VM will be used\n",
duke@435 682 jvmtype, knownVMs[0].name + 1);
duke@435 683 }
duke@435 684 /* fall through */
duke@435 685 case VM_IGNORE:
duke@435 686 jvmtype = knownVMs[jvmidx=0].name + 1;
duke@435 687 /* fall through */
duke@435 688 case VM_KNOWN:
duke@435 689 break;
duke@435 690 case VM_ERROR:
duke@435 691 if (!speculative) {
duke@435 692 ReportErrorMessage2("Error: %s VM not supported", jvmtype, JNI_TRUE);
duke@435 693 exit(1);
duke@435 694 } else {
duke@435 695 return "ERROR";
duke@435 696 }
duke@435 697 }
duke@435 698
duke@435 699 return jvmtype;
duke@435 700 }
duke@435 701 #endif /* ifndef GAMMA */
duke@435 702
duke@435 703 /*
duke@435 704 * Adds a new VM option with the given given name and value.
duke@435 705 */
duke@435 706 static void
duke@435 707 AddOption(char *str, void *info)
duke@435 708 {
duke@435 709 /*
duke@435 710 * Expand options array if needed to accommodate at least one more
duke@435 711 * VM option.
duke@435 712 */
duke@435 713 if (numOptions >= maxOptions) {
duke@435 714 if (options == 0) {
duke@435 715 maxOptions = 4;
duke@435 716 options = MemAlloc(maxOptions * sizeof(JavaVMOption));
duke@435 717 } else {
duke@435 718 JavaVMOption *tmp;
duke@435 719 maxOptions *= 2;
duke@435 720 tmp = MemAlloc(maxOptions * sizeof(JavaVMOption));
duke@435 721 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
duke@435 722 free(options);
duke@435 723 options = tmp;
duke@435 724 }
duke@435 725 }
duke@435 726 options[numOptions].optionString = str;
duke@435 727 options[numOptions++].extraInfo = info;
duke@435 728 }
duke@435 729
duke@435 730 static void
duke@435 731 SetClassPath(char *s)
duke@435 732 {
duke@435 733 char *def = MemAlloc(strlen(s) + 40);
duke@435 734 sprintf(def, "-Djava.class.path=%s", s);
duke@435 735 AddOption(def, NULL);
duke@435 736 }
duke@435 737
duke@435 738 #ifndef GAMMA
duke@435 739 /*
duke@435 740 * The SelectVersion() routine ensures that an appropriate version of
duke@435 741 * the JRE is running. The specification for the appropriate version
duke@435 742 * is obtained from either the manifest of a jar file (preferred) or
duke@435 743 * from command line options.
duke@435 744 */
duke@435 745 static void
duke@435 746 SelectVersion(int argc, char **argv, char **main_class)
duke@435 747 {
duke@435 748 char *arg;
duke@435 749 char **new_argv;
duke@435 750 char **new_argp;
duke@435 751 char *operand;
duke@435 752 char *version = NULL;
duke@435 753 char *jre = NULL;
duke@435 754 int jarflag = 0;
duke@435 755 int restrict_search = -1; /* -1 implies not known */
duke@435 756 manifest_info info;
duke@435 757 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
duke@435 758 char *env_in;
duke@435 759 int res;
duke@435 760
duke@435 761 /*
duke@435 762 * If the version has already been selected, set *main_class
duke@435 763 * with the value passed through the environment (if any) and
duke@435 764 * simply return.
duke@435 765 */
duke@435 766 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
duke@435 767 if (*env_in != '\0')
duke@435 768 *main_class = strdup(env_in);
duke@435 769 return;
duke@435 770 }
duke@435 771
duke@435 772 /*
duke@435 773 * Scan through the arguments for options relevant to multiple JRE
duke@435 774 * support. For reference, the command line syntax is defined as:
duke@435 775 *
duke@435 776 * SYNOPSIS
duke@435 777 * java [options] class [argument...]
duke@435 778 *
duke@435 779 * java [options] -jar file.jar [argument...]
duke@435 780 *
duke@435 781 * As the scan is performed, make a copy of the argument list with
duke@435 782 * the version specification options (new to 1.5) removed, so that
duke@435 783 * a version less than 1.5 can be exec'd.
duke@435 784 */
duke@435 785 new_argv = MemAlloc((argc + 1) * sizeof(char*));
duke@435 786 new_argv[0] = argv[0];
duke@435 787 new_argp = &new_argv[1];
duke@435 788 argc--;
duke@435 789 argv++;
duke@435 790 while ((arg = *argv) != 0 && *arg == '-') {
duke@435 791 if (strncmp(arg, "-version:", 9) == 0) {
duke@435 792 version = arg + 9;
duke@435 793 } else if (strcmp(arg, "-jre-restrict-search") == 0) {
duke@435 794 restrict_search = 1;
duke@435 795 } else if (strcmp(arg, "-no-jre-restrict-search") == 0) {
duke@435 796 restrict_search = 0;
duke@435 797 } else {
duke@435 798 if (strcmp(arg, "-jar") == 0)
duke@435 799 jarflag = 1;
duke@435 800 /* deal with "unfortunate" classpath syntax */
duke@435 801 if ((strcmp(arg, "-classpath") == 0 || strcmp(arg, "-cp") == 0) &&
duke@435 802 (argc >= 2)) {
duke@435 803 *new_argp++ = arg;
duke@435 804 argc--;
duke@435 805 argv++;
duke@435 806 arg = *argv;
duke@435 807 }
duke@435 808 *new_argp++ = arg;
duke@435 809 }
duke@435 810 argc--;
duke@435 811 argv++;
duke@435 812 }
duke@435 813 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
duke@435 814 operand = NULL;
duke@435 815 } else {
duke@435 816 argc--;
duke@435 817 *new_argp++ = operand = *argv++;
duke@435 818 }
duke@435 819 while (argc-- > 0) /* Copy over [argument...] */
duke@435 820 *new_argp++ = *argv++;
duke@435 821 *new_argp = NULL;
duke@435 822
duke@435 823 /*
duke@435 824 * If there is a jar file, read the manifest. If the jarfile can't be
duke@435 825 * read, the manifest can't be read from the jar file, or the manifest
duke@435 826 * is corrupt, issue the appropriate error messages and exit.
duke@435 827 *
duke@435 828 * Even if there isn't a jar file, construct a manifest_info structure
duke@435 829 * containing the command line information. It's a convenient way to carry
duke@435 830 * this data around.
duke@435 831 */
duke@435 832 if (jarflag && operand) {
duke@435 833 if ((res = parse_manifest(operand, &info)) != 0) {
duke@435 834 if (res == -1)
duke@435 835 ReportErrorMessage2("Unable to access jarfile %s",
duke@435 836 operand, JNI_TRUE);
duke@435 837 else
duke@435 838 ReportErrorMessage2("Invalid or corrupt jarfile %s",
duke@435 839 operand, JNI_TRUE);
duke@435 840 exit(1);
duke@435 841 }
duke@435 842 } else {
duke@435 843 info.manifest_version = NULL;
duke@435 844 info.main_class = NULL;
duke@435 845 info.jre_version = NULL;
duke@435 846 info.jre_restrict_search = 0;
duke@435 847 }
duke@435 848
duke@435 849 /*
duke@435 850 * The JRE-Version and JRE-Restrict-Search values (if any) from the
duke@435 851 * manifest are overwritten by any specified on the command line.
duke@435 852 */
duke@435 853 if (version != NULL)
duke@435 854 info.jre_version = version;
duke@435 855 if (restrict_search != -1)
duke@435 856 info.jre_restrict_search = restrict_search;
duke@435 857
duke@435 858 /*
duke@435 859 * "Valid" returns (other than unrecoverable errors) follow. Set
duke@435 860 * main_class as a side-effect of this routine.
duke@435 861 */
duke@435 862 if (info.main_class != NULL)
duke@435 863 *main_class = strdup(info.main_class);
duke@435 864
duke@435 865 /*
duke@435 866 * If no version selection information is found either on the command
duke@435 867 * line or in the manifest, simply return.
duke@435 868 */
duke@435 869 if (info.jre_version == NULL) {
duke@435 870 free_manifest();
duke@435 871 free(new_argv);
duke@435 872 return;
duke@435 873 }
duke@435 874
duke@435 875 /*
duke@435 876 * Check for correct syntax of the version specification (JSR 56).
duke@435 877 */
duke@435 878 if (!valid_version_string(info.jre_version)) {
duke@435 879 ReportErrorMessage2("Syntax error in version specification \"%s\"",
duke@435 880 info.jre_version, JNI_TRUE);
duke@435 881 exit(1);
duke@435 882 }
duke@435 883
duke@435 884 /*
duke@435 885 * Find the appropriate JVM on the system. Just to be as forgiving as
duke@435 886 * possible, if the standard algorithms don't locate an appropriate
duke@435 887 * jre, check to see if the one running will satisfy the requirements.
duke@435 888 * This can happen on systems which haven't been set-up for multiple
duke@435 889 * JRE support.
duke@435 890 */
duke@435 891 jre = LocateJRE(&info);
duke@435 892 if (_launcher_debug)
duke@435 893 printf("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
duke@435 894 (info.jre_version?info.jre_version:"null"),
duke@435 895 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
duke@435 896 if (jre == NULL) {
duke@435 897 if (acceptable_release(FULL_VERSION, info.jre_version)) {
duke@435 898 free_manifest();
duke@435 899 free(new_argv);
duke@435 900 return;
duke@435 901 } else {
duke@435 902 ReportErrorMessage2(
duke@435 903 "Unable to locate JRE meeting specification \"%s\"",
duke@435 904 info.jre_version, JNI_TRUE);
duke@435 905 exit(1);
duke@435 906 }
duke@435 907 }
duke@435 908
duke@435 909 /*
duke@435 910 * If I'm not the chosen one, exec the chosen one. Returning from
duke@435 911 * ExecJRE indicates that I am indeed the chosen one.
duke@435 912 *
duke@435 913 * The private environment variable _JAVA_VERSION_SET is used to
duke@435 914 * prevent the chosen one from re-reading the manifest file and
duke@435 915 * using the values found within to override the (potential) command
duke@435 916 * line flags stripped from argv (because the target may not
duke@435 917 * understand them). Passing the MainClass value is an optimization
duke@435 918 * to avoid locating, expanding and parsing the manifest extra
duke@435 919 * times.
duke@435 920 */
duke@435 921 if (info.main_class != NULL)
duke@435 922 (void)strcat(env_entry, info.main_class);
duke@435 923 (void)putenv(env_entry);
duke@435 924 ExecJRE(jre, new_argv);
duke@435 925 free_manifest();
duke@435 926 free(new_argv);
duke@435 927 return;
duke@435 928 }
duke@435 929 #endif /* ifndef GAMMA */
duke@435 930
duke@435 931 /*
duke@435 932 * Parses command line arguments. Returns JNI_FALSE if launcher
duke@435 933 * should exit without starting vm (e.g. certain version and usage
duke@435 934 * options); returns JNI_TRUE if vm needs to be started to process
duke@435 935 * given options. *pret (the launcher process return value) is set to
duke@435 936 * 0 for a normal exit.
duke@435 937 */
duke@435 938 static jboolean
duke@435 939 ParseArguments(int *pargc, char ***pargv, char **pjarfile,
duke@435 940 char **pclassname, int *pret)
duke@435 941 {
duke@435 942 int argc = *pargc;
duke@435 943 char **argv = *pargv;
duke@435 944 jboolean jarflag = JNI_FALSE;
duke@435 945 char *arg;
duke@435 946
duke@435 947 *pret = 1;
duke@435 948 while ((arg = *argv) != 0 && *arg == '-') {
duke@435 949 argv++; --argc;
duke@435 950 if (strcmp(arg, "-classpath") == 0 || strcmp(arg, "-cp") == 0) {
duke@435 951 if (argc < 1) {
duke@435 952 ReportErrorMessage2("%s requires class path specification",
duke@435 953 arg, JNI_TRUE);
duke@435 954 PrintUsage();
duke@435 955 return JNI_FALSE;
duke@435 956 }
duke@435 957 SetClassPath(*argv);
duke@435 958 argv++; --argc;
duke@435 959 } else if (strcmp(arg, "-jar") == 0) {
duke@435 960 jarflag = JNI_TRUE;
duke@435 961 } else if (strcmp(arg, "-help") == 0 ||
duke@435 962 strcmp(arg, "-h") == 0 ||
duke@435 963 strcmp(arg, "-?") == 0) {
duke@435 964 PrintUsage();
duke@435 965 *pret = 0;
duke@435 966 return JNI_FALSE;
duke@435 967 } else if (strcmp(arg, "-version") == 0) {
duke@435 968 printVersion = JNI_TRUE;
duke@435 969 return JNI_TRUE;
duke@435 970 } else if (strcmp(arg, "-showversion") == 0) {
duke@435 971 showVersion = JNI_TRUE;
duke@435 972 } else if (strcmp(arg, "-X") == 0) {
duke@435 973 *pret = PrintXUsage();
duke@435 974 return JNI_FALSE;
duke@435 975 /*
duke@435 976 * The following case provide backward compatibility with old-style
duke@435 977 * command line options.
duke@435 978 */
duke@435 979 } else if (strcmp(arg, "-fullversion") == 0) {
duke@435 980 fprintf(stderr, "%s full version \"%s\"\n", progname,
duke@435 981 FULL_VERSION);
duke@435 982 *pret = 0;
duke@435 983 return JNI_FALSE;
duke@435 984 } else if (strcmp(arg, "-verbosegc") == 0) {
duke@435 985 AddOption("-verbose:gc", NULL);
duke@435 986 } else if (strcmp(arg, "-t") == 0) {
duke@435 987 AddOption("-Xt", NULL);
duke@435 988 } else if (strcmp(arg, "-tm") == 0) {
duke@435 989 AddOption("-Xtm", NULL);
duke@435 990 } else if (strcmp(arg, "-debug") == 0) {
duke@435 991 AddOption("-Xdebug", NULL);
duke@435 992 } else if (strcmp(arg, "-noclassgc") == 0) {
duke@435 993 AddOption("-Xnoclassgc", NULL);
duke@435 994 } else if (strcmp(arg, "-Xfuture") == 0) {
duke@435 995 AddOption("-Xverify:all", NULL);
duke@435 996 } else if (strcmp(arg, "-verify") == 0) {
duke@435 997 AddOption("-Xverify:all", NULL);
duke@435 998 } else if (strcmp(arg, "-verifyremote") == 0) {
duke@435 999 AddOption("-Xverify:remote", NULL);
duke@435 1000 } else if (strcmp(arg, "-noverify") == 0) {
duke@435 1001 AddOption("-Xverify:none", NULL);
duke@435 1002 } else if (strcmp(arg, "-XXsuppressExitMessage") == 0) {
duke@435 1003 noExitErrorMessage = 1;
duke@435 1004 } else if (strncmp(arg, "-prof", 5) == 0) {
duke@435 1005 char *p = arg + 5;
duke@435 1006 char *tmp = MemAlloc(strlen(arg) + 50);
duke@435 1007 if (*p) {
duke@435 1008 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
duke@435 1009 } else {
duke@435 1010 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
duke@435 1011 }
duke@435 1012 AddOption(tmp, NULL);
duke@435 1013 } else if (strncmp(arg, "-ss", 3) == 0 ||
duke@435 1014 strncmp(arg, "-oss", 4) == 0 ||
duke@435 1015 strncmp(arg, "-ms", 3) == 0 ||
duke@435 1016 strncmp(arg, "-mx", 3) == 0) {
duke@435 1017 char *tmp = MemAlloc(strlen(arg) + 6);
duke@435 1018 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
duke@435 1019 AddOption(tmp, NULL);
duke@435 1020 } else if (strcmp(arg, "-checksource") == 0 ||
duke@435 1021 strcmp(arg, "-cs") == 0 ||
duke@435 1022 strcmp(arg, "-noasyncgc") == 0) {
duke@435 1023 /* No longer supported */
duke@435 1024 fprintf(stderr,
duke@435 1025 "Warning: %s option is no longer supported.\n",
duke@435 1026 arg);
duke@435 1027 } else if (strncmp(arg, "-version:", 9) == 0 ||
duke@435 1028 strcmp(arg, "-no-jre-restrict-search") == 0 ||
duke@435 1029 strcmp(arg, "-jre-restrict-search") == 0) {
duke@435 1030 ; /* Ignore machine independent options already handled */
duke@435 1031 } else if (RemovableMachineDependentOption(arg) ) {
duke@435 1032 ; /* Do not pass option to vm. */
duke@435 1033 }
duke@435 1034 else {
duke@435 1035 AddOption(arg, NULL);
duke@435 1036 }
duke@435 1037 }
duke@435 1038
duke@435 1039 if (--argc >= 0) {
duke@435 1040 if (jarflag) {
duke@435 1041 *pjarfile = *argv++;
duke@435 1042 *pclassname = 0;
duke@435 1043 } else {
duke@435 1044 *pjarfile = 0;
duke@435 1045 *pclassname = *argv++;
duke@435 1046 }
duke@435 1047 *pargc = argc;
duke@435 1048 *pargv = argv;
duke@435 1049 }
duke@435 1050
duke@435 1051 return JNI_TRUE;
duke@435 1052 }
duke@435 1053
duke@435 1054 /*
duke@435 1055 * Initializes the Java Virtual Machine. Also frees options array when
duke@435 1056 * finished.
duke@435 1057 */
duke@435 1058 static jboolean
duke@435 1059 InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
duke@435 1060 {
duke@435 1061 JavaVMInitArgs args;
duke@435 1062 jint r;
duke@435 1063
duke@435 1064 memset(&args, 0, sizeof(args));
duke@435 1065 args.version = JNI_VERSION_1_2;
duke@435 1066 args.nOptions = numOptions;
duke@435 1067 args.options = options;
duke@435 1068 args.ignoreUnrecognized = JNI_FALSE;
duke@435 1069
duke@435 1070 if (_launcher_debug) {
duke@435 1071 int i = 0;
duke@435 1072 printf("JavaVM args:\n ");
duke@435 1073 printf("version 0x%08lx, ", (long)args.version);
duke@435 1074 printf("ignoreUnrecognized is %s, ",
duke@435 1075 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
duke@435 1076 printf("nOptions is %ld\n", (long)args.nOptions);
duke@435 1077 for (i = 0; i < numOptions; i++)
duke@435 1078 printf(" option[%2d] = '%s'\n",
duke@435 1079 i, args.options[i].optionString);
duke@435 1080 }
duke@435 1081
duke@435 1082 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
duke@435 1083 free(options);
duke@435 1084 return r == JNI_OK;
duke@435 1085 }
duke@435 1086
duke@435 1087
duke@435 1088 #define NULL_CHECK0(e) if ((e) == 0) return 0
duke@435 1089 #define NULL_CHECK(e) if ((e) == 0) return
duke@435 1090
duke@435 1091 /*
duke@435 1092 * Returns a pointer to a block of at least 'size' bytes of memory.
duke@435 1093 * Prints error message and exits if the memory could not be allocated.
duke@435 1094 */
duke@435 1095 void *
duke@435 1096 MemAlloc(size_t size)
duke@435 1097 {
duke@435 1098 void *p = malloc(size);
duke@435 1099 if (p == 0) {
duke@435 1100 perror("malloc");
duke@435 1101 exit(1);
duke@435 1102 }
duke@435 1103 return p;
duke@435 1104 }
duke@435 1105
duke@435 1106 static jstring platformEncoding = NULL;
duke@435 1107 static jstring getPlatformEncoding(JNIEnv *env) {
duke@435 1108 if (platformEncoding == NULL) {
duke@435 1109 jstring propname = (*env)->NewStringUTF(env, "sun.jnu.encoding");
duke@435 1110 if (propname) {
duke@435 1111 jclass cls;
duke@435 1112 jmethodID mid;
ksrini@823 1113 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/lang/System"));
duke@435 1114 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
duke@435 1115 env, cls,
duke@435 1116 "getProperty",
duke@435 1117 "(Ljava/lang/String;)Ljava/lang/String;"));
duke@435 1118 platformEncoding = (*env)->CallStaticObjectMethod (
duke@435 1119 env, cls, mid, propname);
duke@435 1120 }
duke@435 1121 }
duke@435 1122 return platformEncoding;
duke@435 1123 }
duke@435 1124
duke@435 1125 static jboolean isEncodingSupported(JNIEnv *env, jstring enc) {
duke@435 1126 jclass cls;
duke@435 1127 jmethodID mid;
ksrini@823 1128 NULL_CHECK0 (cls = FindBootStrapClass(env, "java/nio/charset/Charset"));
duke@435 1129 NULL_CHECK0 (mid = (*env)->GetStaticMethodID(
duke@435 1130 env, cls,
duke@435 1131 "isSupported",
duke@435 1132 "(Ljava/lang/String;)Z"));
duke@435 1133 return (jboolean)(*env)->CallStaticObjectMethod (env, cls, mid, enc);
duke@435 1134 }
duke@435 1135
duke@435 1136 /*
duke@435 1137 * Returns a new Java string object for the specified platform string.
duke@435 1138 */
duke@435 1139 static jstring
duke@435 1140 NewPlatformString(JNIEnv *env, char *s)
duke@435 1141 {
duke@435 1142 int len = (int)strlen(s);
duke@435 1143 jclass cls;
duke@435 1144 jmethodID mid;
duke@435 1145 jbyteArray ary;
duke@435 1146 jstring enc;
duke@435 1147
duke@435 1148 if (s == NULL)
duke@435 1149 return 0;
duke@435 1150 enc = getPlatformEncoding(env);
duke@435 1151
duke@435 1152 ary = (*env)->NewByteArray(env, len);
duke@435 1153 if (ary != 0) {
duke@435 1154 jstring str = 0;
duke@435 1155 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
duke@435 1156 if (!(*env)->ExceptionOccurred(env)) {
duke@435 1157 #ifdef GAMMA
duke@435 1158 /* We support running JVM with older JDK, so here we have to deal */
duke@435 1159 /* with the case that sun.jnu.encoding is undefined (enc == NULL) */
duke@435 1160 if (enc != NULL && isEncodingSupported(env, enc) == JNI_TRUE) {
duke@435 1161 #else
duke@435 1162 if (isEncodingSupported(env, enc) == JNI_TRUE) {
duke@435 1163 #endif
ksrini@823 1164 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke@435 1165 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
duke@435 1166 "([BLjava/lang/String;)V"));
duke@435 1167 str = (*env)->NewObject(env, cls, mid, ary, enc);
duke@435 1168 } else {
duke@435 1169 /*If the encoding specified in sun.jnu.encoding is not
duke@435 1170 endorsed by "Charset.isSupported" we have to fall back
duke@435 1171 to use String(byte[]) explicitly here without specifying
duke@435 1172 the encoding name, in which the StringCoding class will
duke@435 1173 pickup the iso-8859-1 as the fallback converter for us.
duke@435 1174 */
ksrini@823 1175 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke@435 1176 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
duke@435 1177 "([B)V"));
duke@435 1178 str = (*env)->NewObject(env, cls, mid, ary);
duke@435 1179 }
duke@435 1180 (*env)->DeleteLocalRef(env, ary);
duke@435 1181 return str;
duke@435 1182 }
duke@435 1183 }
duke@435 1184 return 0;
duke@435 1185 }
duke@435 1186
duke@435 1187 /*
duke@435 1188 * Returns a new array of Java string objects for the specified
duke@435 1189 * array of platform strings.
duke@435 1190 */
duke@435 1191 static jobjectArray
duke@435 1192 NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
duke@435 1193 {
duke@435 1194 jarray cls;
duke@435 1195 jarray ary;
duke@435 1196 int i;
duke@435 1197
ksrini@823 1198 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke@435 1199 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
duke@435 1200 for (i = 0; i < strc; i++) {
duke@435 1201 jstring str = NewPlatformString(env, *strv++);
duke@435 1202 NULL_CHECK0(str);
duke@435 1203 (*env)->SetObjectArrayElement(env, ary, i, str);
duke@435 1204 (*env)->DeleteLocalRef(env, str);
duke@435 1205 }
duke@435 1206 return ary;
duke@435 1207 }
duke@435 1208
duke@435 1209 /*
duke@435 1210 * Loads a class, convert the '.' to '/'.
duke@435 1211 */
duke@435 1212 static jclass
duke@435 1213 LoadClass(JNIEnv *env, char *name)
duke@435 1214 {
duke@435 1215 char *buf = MemAlloc(strlen(name) + 1);
duke@435 1216 char *s = buf, *t = name, c;
duke@435 1217 jclass cls;
duke@435 1218 jlong start, end;
duke@435 1219
duke@435 1220 if (_launcher_debug)
duke@435 1221 start = CounterGet();
duke@435 1222
duke@435 1223 do {
duke@435 1224 c = *t++;
duke@435 1225 *s++ = (c == '.') ? '/' : c;
duke@435 1226 } while (c != '\0');
ksrini@823 1227 // use the application class loader for the main-class
duke@435 1228 cls = (*env)->FindClass(env, buf);
duke@435 1229 free(buf);
duke@435 1230
duke@435 1231 if (_launcher_debug) {
duke@435 1232 end = CounterGet();
duke@435 1233 printf("%ld micro seconds to load main class\n",
duke@435 1234 (long)(jint)Counter2Micros(end-start));
duke@435 1235 printf("----_JAVA_LAUNCHER_DEBUG----\n");
duke@435 1236 }
duke@435 1237
duke@435 1238 return cls;
duke@435 1239 }
duke@435 1240
duke@435 1241
duke@435 1242 /*
duke@435 1243 * Returns the main class name for the specified jar file.
duke@435 1244 */
duke@435 1245 static jstring
duke@435 1246 GetMainClassName(JNIEnv *env, char *jarname)
duke@435 1247 {
duke@435 1248 #define MAIN_CLASS "Main-Class"
duke@435 1249 jclass cls;
duke@435 1250 jmethodID mid;
duke@435 1251 jobject jar, man, attr;
duke@435 1252 jstring str, result = 0;
duke@435 1253
ksrini@823 1254 NULL_CHECK0(cls = FindBootStrapClass(env, "java/util/jar/JarFile"));
duke@435 1255 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "<init>",
duke@435 1256 "(Ljava/lang/String;)V"));
duke@435 1257 NULL_CHECK0(str = NewPlatformString(env, jarname));
duke@435 1258 NULL_CHECK0(jar = (*env)->NewObject(env, cls, mid, str));
duke@435 1259 NULL_CHECK0(mid = (*env)->GetMethodID(env, cls, "getManifest",
duke@435 1260 "()Ljava/util/jar/Manifest;"));
duke@435 1261 man = (*env)->CallObjectMethod(env, jar, mid);
duke@435 1262 if (man != 0) {
duke@435 1263 NULL_CHECK0(mid = (*env)->GetMethodID(env,
duke@435 1264 (*env)->GetObjectClass(env, man),
duke@435 1265 "getMainAttributes",
duke@435 1266 "()Ljava/util/jar/Attributes;"));
duke@435 1267 attr = (*env)->CallObjectMethod(env, man, mid);
duke@435 1268 if (attr != 0) {
duke@435 1269 NULL_CHECK0(mid = (*env)->GetMethodID(env,
duke@435 1270 (*env)->GetObjectClass(env, attr),
duke@435 1271 "getValue",
duke@435 1272 "(Ljava/lang/String;)Ljava/lang/String;"));
duke@435 1273 NULL_CHECK0(str = NewPlatformString(env, MAIN_CLASS));
duke@435 1274 result = (*env)->CallObjectMethod(env, attr, mid, str);
duke@435 1275 }
duke@435 1276 }
duke@435 1277 return result;
duke@435 1278 }
duke@435 1279
duke@435 1280 #ifdef JAVA_ARGS
duke@435 1281 static char *java_args[] = JAVA_ARGS;
duke@435 1282 static char *app_classpath[] = APP_CLASSPATH;
duke@435 1283
duke@435 1284 /*
duke@435 1285 * For tools convert 'javac -J-ms32m' to 'java -ms32m ...'
duke@435 1286 */
duke@435 1287 static void
duke@435 1288 TranslateDashJArgs(int *pargc, char ***pargv)
duke@435 1289 {
duke@435 1290 const int NUM_ARGS = (sizeof(java_args) / sizeof(char *));
duke@435 1291 int argc = *pargc;
duke@435 1292 char **argv = *pargv;
duke@435 1293 int nargc = argc + NUM_ARGS;
duke@435 1294 char **nargv = MemAlloc((nargc + 1) * sizeof(char *));
duke@435 1295 int i;
duke@435 1296
duke@435 1297 *pargc = nargc;
duke@435 1298 *pargv = nargv;
duke@435 1299
duke@435 1300 /* Copy the VM arguments (i.e. prefixed with -J) */
duke@435 1301 for (i = 0; i < NUM_ARGS; i++) {
duke@435 1302 char *arg = java_args[i];
duke@435 1303 if (arg[0] == '-' && arg[1] == 'J') {
duke@435 1304 *nargv++ = arg + 2;
duke@435 1305 }
duke@435 1306 }
duke@435 1307
duke@435 1308 for (i = 0; i < argc; i++) {
duke@435 1309 char *arg = argv[i];
duke@435 1310 if (arg[0] == '-' && arg[1] == 'J') {
duke@435 1311 if (arg[2] == '\0') {
duke@435 1312 ReportErrorMessage("Error: the -J option should not be "
duke@435 1313 "followed by a space.", JNI_TRUE);
duke@435 1314 exit(1);
duke@435 1315 }
duke@435 1316 *nargv++ = arg + 2;
duke@435 1317 }
duke@435 1318 }
duke@435 1319
duke@435 1320 /* Copy the rest of the arguments */
duke@435 1321 for (i = 0; i < NUM_ARGS; i++) {
duke@435 1322 char *arg = java_args[i];
duke@435 1323 if (arg[0] != '-' || arg[1] != 'J') {
duke@435 1324 *nargv++ = arg;
duke@435 1325 }
duke@435 1326 }
duke@435 1327 for (i = 0; i < argc; i++) {
duke@435 1328 char *arg = argv[i];
duke@435 1329 if (arg[0] != '-' || arg[1] != 'J') {
duke@435 1330 *nargv++ = arg;
duke@435 1331 }
duke@435 1332 }
duke@435 1333 *nargv = 0;
duke@435 1334 }
duke@435 1335
duke@435 1336 /*
duke@435 1337 * For our tools, we try to add 3 VM options:
duke@435 1338 * -Denv.class.path=<envcp>
duke@435 1339 * -Dapplication.home=<apphome>
duke@435 1340 * -Djava.class.path=<appcp>
duke@435 1341 * <envcp> is the user's setting of CLASSPATH -- for instance the user
duke@435 1342 * tells javac where to find binary classes through this environment
duke@435 1343 * variable. Notice that users will be able to compile against our
duke@435 1344 * tools classes (sun.tools.javac.Main) only if they explicitly add
duke@435 1345 * tools.jar to CLASSPATH.
duke@435 1346 * <apphome> is the directory where the application is installed.
duke@435 1347 * <appcp> is the classpath to where our apps' classfiles are.
duke@435 1348 */
duke@435 1349 static jboolean
duke@435 1350 AddApplicationOptions()
duke@435 1351 {
duke@435 1352 const int NUM_APP_CLASSPATH = (sizeof(app_classpath) / sizeof(char *));
duke@435 1353 char *s, *envcp, *appcp, *apphome;
duke@435 1354 char home[MAXPATHLEN]; /* application home */
duke@435 1355 char separator[] = { PATH_SEPARATOR, '\0' };
duke@435 1356 int size, i;
duke@435 1357 int strlenHome;
duke@435 1358
duke@435 1359 s = getenv("CLASSPATH");
duke@435 1360 if (s) {
duke@435 1361 /* 40 for -Denv.class.path= */
duke@435 1362 envcp = (char *)MemAlloc(strlen(s) + 40);
duke@435 1363 sprintf(envcp, "-Denv.class.path=%s", s);
duke@435 1364 AddOption(envcp, NULL);
duke@435 1365 }
duke@435 1366
duke@435 1367 if (!GetApplicationHome(home, sizeof(home))) {
duke@435 1368 ReportErrorMessage("Can't determine application home", JNI_TRUE);
duke@435 1369 return JNI_FALSE;
duke@435 1370 }
duke@435 1371
duke@435 1372 /* 40 for '-Dapplication.home=' */
duke@435 1373 apphome = (char *)MemAlloc(strlen(home) + 40);
duke@435 1374 sprintf(apphome, "-Dapplication.home=%s", home);
duke@435 1375 AddOption(apphome, NULL);
duke@435 1376
duke@435 1377 /* How big is the application's classpath? */
duke@435 1378 size = 40; /* 40: "-Djava.class.path=" */
duke@435 1379 strlenHome = (int)strlen(home);
duke@435 1380 for (i = 0; i < NUM_APP_CLASSPATH; i++) {
duke@435 1381 size += strlenHome + (int)strlen(app_classpath[i]) + 1; /* 1: separator */
duke@435 1382 }
duke@435 1383 appcp = (char *)MemAlloc(size + 1);
duke@435 1384 strcpy(appcp, "-Djava.class.path=");
duke@435 1385 for (i = 0; i < NUM_APP_CLASSPATH; i++) {
duke@435 1386 strcat(appcp, home); /* c:\program files\myapp */
duke@435 1387 strcat(appcp, app_classpath[i]); /* \lib\myapp.jar */
duke@435 1388 strcat(appcp, separator); /* ; */
duke@435 1389 }
duke@435 1390 appcp[strlen(appcp)-1] = '\0'; /* remove trailing path separator */
duke@435 1391 AddOption(appcp, NULL);
duke@435 1392 return JNI_TRUE;
duke@435 1393 }
duke@435 1394 #endif
duke@435 1395
duke@435 1396 /*
duke@435 1397 * inject the -Dsun.java.command pseudo property into the args structure
duke@435 1398 * this pseudo property is used in the HotSpot VM to expose the
duke@435 1399 * Java class name and arguments to the main method to the VM. The
duke@435 1400 * HotSpot VM uses this pseudo property to store the Java class name
duke@435 1401 * (or jar file name) and the arguments to the class's main method
duke@435 1402 * to the instrumentation memory region. The sun.java.command pseudo
duke@435 1403 * property is not exported by HotSpot to the Java layer.
duke@435 1404 */
duke@435 1405 void
duke@435 1406 SetJavaCommandLineProp(char *classname, char *jarfile,
duke@435 1407 int argc, char **argv)
duke@435 1408 {
duke@435 1409
duke@435 1410 int i = 0;
duke@435 1411 size_t len = 0;
duke@435 1412 char* javaCommand = NULL;
duke@435 1413 char* dashDstr = "-Dsun.java.command=";
duke@435 1414
duke@435 1415 if (classname == NULL && jarfile == NULL) {
duke@435 1416 /* unexpected, one of these should be set. just return without
duke@435 1417 * setting the property
duke@435 1418 */
duke@435 1419 return;
duke@435 1420 }
duke@435 1421
duke@435 1422 /* if the class name is not set, then use the jarfile name */
duke@435 1423 if (classname == NULL) {
duke@435 1424 classname = jarfile;
duke@435 1425 }
duke@435 1426
duke@435 1427 /* determine the amount of memory to allocate assuming
duke@435 1428 * the individual components will be space separated
duke@435 1429 */
duke@435 1430 len = strlen(classname);
duke@435 1431 for (i = 0; i < argc; i++) {
duke@435 1432 len += strlen(argv[i]) + 1;
duke@435 1433 }
duke@435 1434
duke@435 1435 /* allocate the memory */
duke@435 1436 javaCommand = (char*) MemAlloc(len + strlen(dashDstr) + 1);
duke@435 1437
duke@435 1438 /* build the -D string */
duke@435 1439 *javaCommand = '\0';
duke@435 1440 strcat(javaCommand, dashDstr);
duke@435 1441 strcat(javaCommand, classname);
duke@435 1442
duke@435 1443 for (i = 0; i < argc; i++) {
duke@435 1444 /* the components of the string are space separated. In
duke@435 1445 * the case of embedded white space, the relationship of
duke@435 1446 * the white space separated components to their true
duke@435 1447 * positional arguments will be ambiguous. This issue may
duke@435 1448 * be addressed in a future release.
duke@435 1449 */
duke@435 1450 strcat(javaCommand, " ");
duke@435 1451 strcat(javaCommand, argv[i]);
duke@435 1452 }
duke@435 1453
duke@435 1454 AddOption(javaCommand, NULL);
duke@435 1455 }
duke@435 1456
duke@435 1457 /*
duke@435 1458 * JVM wants to know launcher type, so tell it.
duke@435 1459 */
duke@435 1460 #ifdef GAMMA
duke@435 1461 void SetJavaLauncherProp() {
duke@435 1462 AddOption("-Dsun.java.launcher=" LAUNCHER_TYPE, NULL);
duke@435 1463 }
duke@435 1464 #endif
duke@435 1465
duke@435 1466 /*
duke@435 1467 * Prints the version information from the java.version and other properties.
duke@435 1468 */
duke@435 1469 static void
duke@435 1470 PrintJavaVersion(JNIEnv *env)
duke@435 1471 {
duke@435 1472 jclass ver;
duke@435 1473 jmethodID print;
duke@435 1474
ksrini@823 1475 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke@435 1476 NULL_CHECK(print = (*env)->GetStaticMethodID(env, ver, "print", "()V"));
duke@435 1477
duke@435 1478 (*env)->CallStaticVoidMethod(env, ver, print);
duke@435 1479 }
duke@435 1480
duke@435 1481 /*
duke@435 1482 * Prints default usage message.
duke@435 1483 */
duke@435 1484 static void
duke@435 1485 PrintUsage(void)
duke@435 1486 {
duke@435 1487 int i;
duke@435 1488
duke@435 1489 fprintf(stdout,
duke@435 1490 "Usage: %s [-options] class [args...]\n"
duke@435 1491 " (to execute a class)\n"
duke@435 1492 " or %s [-options] -jar jarfile [args...]\n"
duke@435 1493 " (to execute a jar file)\n"
duke@435 1494 "\n"
duke@435 1495 "where options include:\n",
duke@435 1496 progname,
duke@435 1497 progname);
duke@435 1498
duke@435 1499 #ifndef GAMMA
duke@435 1500 PrintMachineDependentOptions();
duke@435 1501
duke@435 1502 if ((knownVMs[0].flag == VM_KNOWN) ||
duke@435 1503 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
duke@435 1504 fprintf(stdout, " %s\t to select the \"%s\" VM\n",
duke@435 1505 knownVMs[0].name, knownVMs[0].name+1);
duke@435 1506 }
duke@435 1507 for (i=1; i<knownVMsCount; i++) {
duke@435 1508 if (knownVMs[i].flag == VM_KNOWN)
duke@435 1509 fprintf(stdout, " %s\t to select the \"%s\" VM\n",
duke@435 1510 knownVMs[i].name, knownVMs[i].name+1);
duke@435 1511 }
duke@435 1512 for (i=1; i<knownVMsCount; i++) {
duke@435 1513 if (knownVMs[i].flag == VM_ALIASED_TO)
duke@435 1514 fprintf(stdout, " %s\t is a synonym for "
duke@435 1515 "the \"%s\" VM [deprecated]\n",
duke@435 1516 knownVMs[i].name, knownVMs[i].alias+1);
duke@435 1517 }
duke@435 1518
duke@435 1519 /* The first known VM is the default */
duke@435 1520 {
duke@435 1521 const char* defaultVM = knownVMs[0].name+1;
duke@435 1522 const char* punctuation = ".";
duke@435 1523 const char* reason = "";
duke@435 1524 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
duke@435 1525 (ServerClassMachine() == JNI_TRUE)) {
duke@435 1526 defaultVM = knownVMs[0].server_class+1;
duke@435 1527 punctuation = ", ";
duke@435 1528 reason = "because you are running on a server-class machine.\n";
duke@435 1529 }
duke@435 1530 fprintf(stdout, " The default VM is %s%s\n",
duke@435 1531 defaultVM, punctuation);
duke@435 1532 fprintf(stdout, " %s\n",
duke@435 1533 reason);
duke@435 1534 }
duke@435 1535 #endif /* ifndef GAMMA */
duke@435 1536
duke@435 1537 fprintf(stdout,
duke@435 1538 " -cp <class search path of directories and zip/jar files>\n"
duke@435 1539 " -classpath <class search path of directories and zip/jar files>\n"
duke@435 1540 " A %c separated list of directories, JAR archives,\n"
duke@435 1541 " and ZIP archives to search for class files.\n"
duke@435 1542 " -D<name>=<value>\n"
duke@435 1543 " set a system property\n"
duke@435 1544 " -verbose[:class|gc|jni]\n"
duke@435 1545 " enable verbose output\n"
duke@435 1546 " -version print product version and exit\n"
duke@435 1547 " -version:<value>\n"
duke@435 1548 " require the specified version to run\n"
duke@435 1549 " -showversion print product version and continue\n"
duke@435 1550 " -jre-restrict-search | -jre-no-restrict-search\n"
duke@435 1551 " include/exclude user private JREs in the version search\n"
duke@435 1552 " -? -help print this help message\n"
duke@435 1553 " -X print help on non-standard options\n"
duke@435 1554 " -ea[:<packagename>...|:<classname>]\n"
duke@435 1555 " -enableassertions[:<packagename>...|:<classname>]\n"
duke@435 1556 " enable assertions\n"
duke@435 1557 " -da[:<packagename>...|:<classname>]\n"
duke@435 1558 " -disableassertions[:<packagename>...|:<classname>]\n"
duke@435 1559 " disable assertions\n"
duke@435 1560 " -esa | -enablesystemassertions\n"
duke@435 1561 " enable system assertions\n"
duke@435 1562 " -dsa | -disablesystemassertions\n"
duke@435 1563 " disable system assertions\n"
duke@435 1564 " -agentlib:<libname>[=<options>]\n"
duke@435 1565 " load native agent library <libname>, e.g. -agentlib:hprof\n"
duke@435 1566 " see also, -agentlib:jdwp=help and -agentlib:hprof=help\n"
duke@435 1567 " -agentpath:<pathname>[=<options>]\n"
duke@435 1568 " load native agent library by full pathname\n"
duke@435 1569 " -javaagent:<jarpath>[=<options>]\n"
duke@435 1570 " load Java programming language agent, see java.lang.instrument\n"
duke@435 1571
duke@435 1572 ,PATH_SEPARATOR);
duke@435 1573 }
duke@435 1574
duke@435 1575 /*
duke@435 1576 * Print usage message for -X options.
duke@435 1577 */
duke@435 1578 static jint
duke@435 1579 PrintXUsage(void)
duke@435 1580 {
duke@435 1581 char path[MAXPATHLEN];
duke@435 1582 char buf[128];
duke@435 1583 size_t n;
duke@435 1584 FILE *fp;
duke@435 1585
duke@435 1586 GetXUsagePath(path, sizeof(path));
duke@435 1587 fp = fopen(path, "r");
duke@435 1588 if (fp == 0) {
duke@435 1589 fprintf(stderr, "Can't open %s\n", path);
duke@435 1590 return 1;
duke@435 1591 }
duke@435 1592 while ((n = fread(buf, 1, sizeof(buf), fp)) != 0) {
duke@435 1593 fwrite(buf, 1, n, stdout);
duke@435 1594 }
duke@435 1595 fclose(fp);
duke@435 1596 return 0;
duke@435 1597 }
duke@435 1598
duke@435 1599 #ifndef GAMMA
duke@435 1600
duke@435 1601 /*
duke@435 1602 * Read the jvm.cfg file and fill the knownJVMs[] array.
duke@435 1603 *
duke@435 1604 * The functionality of the jvm.cfg file is subject to change without
duke@435 1605 * notice and the mechanism will be removed in the future.
duke@435 1606 *
duke@435 1607 * The lexical structure of the jvm.cfg file is as follows:
duke@435 1608 *
duke@435 1609 * jvmcfg := { vmLine }
duke@435 1610 * vmLine := knownLine
duke@435 1611 * | aliasLine
duke@435 1612 * | warnLine
duke@435 1613 * | ignoreLine
duke@435 1614 * | errorLine
duke@435 1615 * | predicateLine
duke@435 1616 * | commentLine
duke@435 1617 * knownLine := flag "KNOWN" EOL
duke@435 1618 * warnLine := flag "WARN" EOL
duke@435 1619 * ignoreLine := flag "IGNORE" EOL
duke@435 1620 * errorLine := flag "ERROR" EOL
duke@435 1621 * aliasLine := flag "ALIASED_TO" flag EOL
duke@435 1622 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
duke@435 1623 * commentLine := "#" text EOL
duke@435 1624 * flag := "-" identifier
duke@435 1625 *
duke@435 1626 * The semantics are that when someone specifies a flag on the command line:
duke@435 1627 * - if the flag appears on a knownLine, then the identifier is used as
duke@435 1628 * the name of the directory holding the JVM library (the name of the JVM).
duke@435 1629 * - if the flag appears as the first flag on an aliasLine, the identifier
duke@435 1630 * of the second flag is used as the name of the JVM.
duke@435 1631 * - if the flag appears on a warnLine, the identifier is used as the
duke@435 1632 * name of the JVM, but a warning is generated.
duke@435 1633 * - if the flag appears on an ignoreLine, the identifier is recognized as the
duke@435 1634 * name of a JVM, but the identifier is ignored and the default vm used
duke@435 1635 * - if the flag appears on an errorLine, an error is generated.
duke@435 1636 * - if the flag appears as the first flag on a predicateLine, and
duke@435 1637 * the machine on which you are running passes the predicate indicated,
duke@435 1638 * then the identifier of the second flag is used as the name of the JVM,
duke@435 1639 * otherwise the identifier of the first flag is used as the name of the JVM.
duke@435 1640 * If no flag is given on the command line, the first vmLine of the jvm.cfg
duke@435 1641 * file determines the name of the JVM.
duke@435 1642 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
duke@435 1643 * since they only make sense if someone hasn't specified the name of the
duke@435 1644 * JVM on the command line.
duke@435 1645 *
duke@435 1646 * The intent of the jvm.cfg file is to allow several JVM libraries to
duke@435 1647 * be installed in different subdirectories of a single JRE installation,
duke@435 1648 * for space-savings and convenience in testing.
duke@435 1649 * The intent is explicitly not to provide a full aliasing or predicate
duke@435 1650 * mechanism.
duke@435 1651 */
duke@435 1652 jint
duke@435 1653 ReadKnownVMs(const char *jrepath, char * arch, jboolean speculative)
duke@435 1654 {
duke@435 1655 FILE *jvmCfg;
duke@435 1656 char jvmCfgName[MAXPATHLEN+20];
duke@435 1657 char line[MAXPATHLEN+20];
duke@435 1658 int cnt = 0;
duke@435 1659 int lineno = 0;
duke@435 1660 jlong start, end;
duke@435 1661 int vmType;
duke@435 1662 char *tmpPtr;
duke@435 1663 char *altVMName;
duke@435 1664 char *serverClassVMName;
duke@435 1665 static char *whiteSpace = " \t";
duke@435 1666 if (_launcher_debug) {
duke@435 1667 start = CounterGet();
duke@435 1668 }
duke@435 1669
duke@435 1670 strcpy(jvmCfgName, jrepath);
duke@435 1671 strcat(jvmCfgName, FILESEP "lib" FILESEP);
duke@435 1672 strcat(jvmCfgName, arch);
duke@435 1673 strcat(jvmCfgName, FILESEP "jvm.cfg");
duke@435 1674
duke@435 1675 jvmCfg = fopen(jvmCfgName, "r");
duke@435 1676 if (jvmCfg == NULL) {
duke@435 1677 if (!speculative) {
duke@435 1678 ReportErrorMessage2("Error: could not open `%s'", jvmCfgName,
duke@435 1679 JNI_TRUE);
duke@435 1680 exit(1);
duke@435 1681 } else {
duke@435 1682 return -1;
duke@435 1683 }
duke@435 1684 }
duke@435 1685 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
duke@435 1686 vmType = VM_UNKNOWN;
duke@435 1687 lineno++;
duke@435 1688 if (line[0] == '#')
duke@435 1689 continue;
duke@435 1690 if (line[0] != '-') {
duke@435 1691 fprintf(stderr, "Warning: no leading - on line %d of `%s'\n",
duke@435 1692 lineno, jvmCfgName);
duke@435 1693 }
duke@435 1694 if (cnt >= knownVMsLimit) {
duke@435 1695 GrowKnownVMs(cnt);
duke@435 1696 }
duke@435 1697 line[strlen(line)-1] = '\0'; /* remove trailing newline */
duke@435 1698 tmpPtr = line + strcspn(line, whiteSpace);
duke@435 1699 if (*tmpPtr == 0) {
duke@435 1700 fprintf(stderr, "Warning: missing VM type on line %d of `%s'\n",
duke@435 1701 lineno, jvmCfgName);
duke@435 1702 } else {
duke@435 1703 /* Null-terminate this string for strdup below */
duke@435 1704 *tmpPtr++ = 0;
duke@435 1705 tmpPtr += strspn(tmpPtr, whiteSpace);
duke@435 1706 if (*tmpPtr == 0) {
duke@435 1707 fprintf(stderr, "Warning: missing VM type on line %d of `%s'\n",
duke@435 1708 lineno, jvmCfgName);
duke@435 1709 } else {
duke@435 1710 if (!strncmp(tmpPtr, "KNOWN", strlen("KNOWN"))) {
duke@435 1711 vmType = VM_KNOWN;
duke@435 1712 } else if (!strncmp(tmpPtr, "ALIASED_TO", strlen("ALIASED_TO"))) {
duke@435 1713 tmpPtr += strcspn(tmpPtr, whiteSpace);
duke@435 1714 if (*tmpPtr != 0) {
duke@435 1715 tmpPtr += strspn(tmpPtr, whiteSpace);
duke@435 1716 }
duke@435 1717 if (*tmpPtr == 0) {
duke@435 1718 fprintf(stderr, "Warning: missing VM alias on line %d of `%s'\n",
duke@435 1719 lineno, jvmCfgName);
duke@435 1720 } else {
duke@435 1721 /* Null terminate altVMName */
duke@435 1722 altVMName = tmpPtr;
duke@435 1723 tmpPtr += strcspn(tmpPtr, whiteSpace);
duke@435 1724 *tmpPtr = 0;
duke@435 1725 vmType = VM_ALIASED_TO;
duke@435 1726 }
duke@435 1727 } else if (!strncmp(tmpPtr, "WARN", strlen("WARN"))) {
duke@435 1728 vmType = VM_WARN;
duke@435 1729 } else if (!strncmp(tmpPtr, "IGNORE", strlen("IGNORE"))) {
duke@435 1730 vmType = VM_IGNORE;
duke@435 1731 } else if (!strncmp(tmpPtr, "ERROR", strlen("ERROR"))) {
duke@435 1732 vmType = VM_ERROR;
duke@435 1733 } else if (!strncmp(tmpPtr,
duke@435 1734 "IF_SERVER_CLASS",
duke@435 1735 strlen("IF_SERVER_CLASS"))) {
duke@435 1736 tmpPtr += strcspn(tmpPtr, whiteSpace);
duke@435 1737 if (*tmpPtr != 0) {
duke@435 1738 tmpPtr += strspn(tmpPtr, whiteSpace);
duke@435 1739 }
duke@435 1740 if (*tmpPtr == 0) {
duke@435 1741 fprintf(stderr, "Warning: missing server class VM on line %d of `%s'\n",
duke@435 1742 lineno, jvmCfgName);
duke@435 1743 } else {
duke@435 1744 /* Null terminate server class VM name */
duke@435 1745 serverClassVMName = tmpPtr;
duke@435 1746 tmpPtr += strcspn(tmpPtr, whiteSpace);
duke@435 1747 *tmpPtr = 0;
duke@435 1748 vmType = VM_IF_SERVER_CLASS;
duke@435 1749 }
duke@435 1750 } else {
duke@435 1751 fprintf(stderr, "Warning: unknown VM type on line %d of `%s'\n",
duke@435 1752 lineno, &jvmCfgName[0]);
duke@435 1753 vmType = VM_KNOWN;
duke@435 1754 }
duke@435 1755 }
duke@435 1756 }
duke@435 1757
duke@435 1758 if (_launcher_debug)
duke@435 1759 printf("jvm.cfg[%d] = ->%s<-\n", cnt, line);
duke@435 1760 if (vmType != VM_UNKNOWN) {
duke@435 1761 knownVMs[cnt].name = strdup(line);
duke@435 1762 knownVMs[cnt].flag = vmType;
duke@435 1763 switch (vmType) {
duke@435 1764 default:
duke@435 1765 break;
duke@435 1766 case VM_ALIASED_TO:
duke@435 1767 knownVMs[cnt].alias = strdup(altVMName);
duke@435 1768 if (_launcher_debug) {
duke@435 1769 printf(" name: %s vmType: %s alias: %s\n",
duke@435 1770 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
duke@435 1771 }
duke@435 1772 break;
duke@435 1773 case VM_IF_SERVER_CLASS:
duke@435 1774 knownVMs[cnt].server_class = strdup(serverClassVMName);
duke@435 1775 if (_launcher_debug) {
duke@435 1776 printf(" name: %s vmType: %s server_class: %s\n",
duke@435 1777 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
duke@435 1778 }
duke@435 1779 break;
duke@435 1780 }
duke@435 1781 cnt++;
duke@435 1782 }
duke@435 1783 }
duke@435 1784 fclose(jvmCfg);
duke@435 1785 knownVMsCount = cnt;
duke@435 1786
duke@435 1787 if (_launcher_debug) {
duke@435 1788 end = CounterGet();
duke@435 1789 printf("%ld micro seconds to parse jvm.cfg\n",
duke@435 1790 (long)(jint)Counter2Micros(end-start));
duke@435 1791 }
duke@435 1792
duke@435 1793 return cnt;
duke@435 1794 }
duke@435 1795
duke@435 1796
duke@435 1797 static void
duke@435 1798 GrowKnownVMs(int minimum)
duke@435 1799 {
duke@435 1800 struct vmdesc* newKnownVMs;
duke@435 1801 int newMax;
duke@435 1802
duke@435 1803 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
duke@435 1804 if (newMax <= minimum) {
duke@435 1805 newMax = minimum;
duke@435 1806 }
duke@435 1807 newKnownVMs = (struct vmdesc*) MemAlloc(newMax * sizeof(struct vmdesc));
duke@435 1808 if (knownVMs != NULL) {
duke@435 1809 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
duke@435 1810 }
duke@435 1811 free(knownVMs);
duke@435 1812 knownVMs = newKnownVMs;
duke@435 1813 knownVMsLimit = newMax;
duke@435 1814 }
duke@435 1815
duke@435 1816
duke@435 1817 /* Returns index of VM or -1 if not found */
duke@435 1818 static int
duke@435 1819 KnownVMIndex(const char* name)
duke@435 1820 {
duke@435 1821 int i;
duke@435 1822 if (strncmp(name, "-J", 2) == 0) name += 2;
duke@435 1823 for (i = 0; i < knownVMsCount; i++) {
duke@435 1824 if (!strcmp(name, knownVMs[i].name)) {
duke@435 1825 return i;
duke@435 1826 }
duke@435 1827 }
duke@435 1828 return -1;
duke@435 1829 }
duke@435 1830
duke@435 1831 static void
duke@435 1832 FreeKnownVMs()
duke@435 1833 {
duke@435 1834 int i;
duke@435 1835 for (i = 0; i < knownVMsCount; i++) {
duke@435 1836 free(knownVMs[i].name);
duke@435 1837 knownVMs[i].name = NULL;
duke@435 1838 }
duke@435 1839 free(knownVMs);
duke@435 1840 }
duke@435 1841
duke@435 1842 #endif /* ifndef GAMMA */

mercurial