blob: 3e7ff04c446b2704a30e6fcf2d4bd826bb42763b [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ksriniac5e02f2012-01-11 08:14:47 -08002 * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
ohair2283b9d2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
duke6e45e102007-12-01 00:00:00 +00008 * particular file as subject to the "Classpath" exception as provided
ohair2283b9d2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
duke6e45e102007-12-01 00:00:00 +000010 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
ohair2283b9d2010-05-25 15:58:33 -070021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
duke6e45e102007-12-01 00:00:00 +000024 */
25
26/*
27 * Shared source for 'java' command line tool.
28 *
29 * If JAVA_ARGS is defined, then acts as a launcher for applications. For
30 * instance, the JDK command line tools such as javac and javadoc (see
31 * makefiles for more details) are built with this program. Any arguments
32 * prefixed with '-J' will be passed directly to the 'java' command.
33 */
34
35/*
36 * One job of the launcher is to remove command line options which the
37 * vm does not understand and will not process. These options include
38 * options which select which style of vm is run (e.g. -client and
39 * -server) as well as options which select the data model to use.
40 * Additionally, for tools which invoke an underlying vm "-J-foo"
41 * options are turned into "-foo" options to the vm. This option
42 * filtering is handled in a number of places in the launcher, some of
43 * it in machine-dependent code. In this file, the function
ksrini11e7f1b2009-11-20 11:01:32 -080044 * CheckJvmType removes vm style options and TranslateApplicationArgs
45 * removes "-J" prefixes. The CreateExecutionEnvironment function processes
46 * and removes -d<n> options. On unix, there is a possibility that the running
47 * data model may not match to the desired data model, in this case an exec is
48 * required to start the desired model. If the data models match, then
49 * ParseArguments will remove the -d<n> flags. If the data models do not match
50 * the CreateExecutionEnviroment will remove the -d<n> flags.
duke6e45e102007-12-01 00:00:00 +000051 */
52
53
54#include "java.h"
55
56/*
57 * A NOTE TO DEVELOPERS: For performance reasons it is important that
58 * the program image remain relatively small until after SelectVersion
59 * CreateExecutionEnvironment have finished their possibly recursive
60 * processing. Watch everything, but resist all temptations to use Java
61 * interfaces.
62 */
63
ksrini7d9872e2011-03-20 08:41:33 -070064/* we always print to stderr */
65#define USE_STDERR JNI_TRUE
66
duke6e45e102007-12-01 00:00:00 +000067static jboolean printVersion = JNI_FALSE; /* print and exit */
68static jboolean showVersion = JNI_FALSE; /* print but continue */
69static jboolean printUsage = JNI_FALSE; /* print and exit*/
70static jboolean printXUsage = JNI_FALSE; /* print and exit*/
ksrini8e20e1c2010-11-23 16:52:39 -080071static char *showSettings = NULL; /* print but continue */
duke6e45e102007-12-01 00:00:00 +000072
73static const char *_program_name;
74static const char *_launcher_name;
75static jboolean _is_java_args = JNI_FALSE;
76static const char *_fVersion;
77static const char *_dVersion;
78static jboolean _wc_enabled = JNI_FALSE;
79static jint _ergo_policy = DEFAULT_POLICY;
80
81/*
82 * Entries for splash screen environment variables.
83 * putenv is performed in SelectVersion. We need
84 * them in memory until UnsetEnv, so they are made static
85 * global instead of auto local.
86 */
87static char* splash_file_entry = NULL;
88static char* splash_jar_entry = NULL;
89
90/*
91 * List of VM options to be specified when the VM is created.
92 */
93static JavaVMOption *options;
94static int numOptions, maxOptions;
95
96/*
97 * Prototypes for functions internal to launcher.
98 */
99static void SetClassPath(const char *s);
100static void SelectVersion(int argc, char **argv, char **main_class);
mchungea290e22011-01-21 09:43:57 -0800101static jboolean ParseArguments(int *pargc, char ***pargv,
102 int *pmode, char **pwhat,
103 int *pret, const char *jrepath);
duke6e45e102007-12-01 00:00:00 +0000104static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
105 InvocationFunctions *ifn);
106static jstring NewPlatformString(JNIEnv *env, char *s);
107static jobjectArray NewPlatformStringArray(JNIEnv *env, char **strv, int strc);
mchungea290e22011-01-21 09:43:57 -0800108static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
duke6e45e102007-12-01 00:00:00 +0000109
110static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
111static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
112static void SetApplicationClassPath(const char**);
113
114static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
115static void PrintUsage(JNIEnv* env, jboolean doXUsage);
ksrini8e20e1c2010-11-23 16:52:39 -0800116static void ShowSettings(JNIEnv* env, char *optString);
duke6e45e102007-12-01 00:00:00 +0000117
118static void SetPaths(int argc, char **argv);
119
120static void DumpState();
121static jboolean RemovableOption(char *option);
122
123/* Maximum supported entries from jvm.cfg. */
124#define INIT_MAX_KNOWN_VMS 10
125
126/* Values for vmdesc.flag */
127enum vmdesc_flag {
128 VM_UNKNOWN = -1,
129 VM_KNOWN,
130 VM_ALIASED_TO,
131 VM_WARN,
132 VM_ERROR,
133 VM_IF_SERVER_CLASS,
134 VM_IGNORE
135};
136
137struct vmdesc {
138 char *name;
139 int flag;
140 char *alias;
141 char *server_class;
142};
143static struct vmdesc *knownVMs = NULL;
144static int knownVMsCount = 0;
145static int knownVMsLimit = 0;
146
147static void GrowKnownVMs();
148static int KnownVMIndex(const char* name);
149static void FreeKnownVMs();
duke6e45e102007-12-01 00:00:00 +0000150static jboolean IsWildCardEnabled();
151
152#define ARG_CHECK(n, f, a) if (n < 1) { \
ksrini0e817162008-08-26 10:21:20 -0700153 JLI_ReportErrorMessage(f, a); \
duke6e45e102007-12-01 00:00:00 +0000154 printUsage = JNI_TRUE; \
155 *pret = 1; \
156 return JNI_TRUE; \
157}
158
159/*
160 * Running Java code in primordial thread caused many problems. We will
161 * create a new thread to invoke JVM. See 6316197 for more information.
162 */
mchungea290e22011-01-21 09:43:57 -0800163static jlong threadStackSize = 0; /* stack size of the new thread */
ksrini6d575f82010-12-23 13:51:30 -0800164static jlong maxHeapSize = 0; /* max heap size */
165static jlong initialHeapSize = 0; /* inital heap size */
duke6e45e102007-12-01 00:00:00 +0000166
duke6e45e102007-12-01 00:00:00 +0000167/*
168 * Entry point.
169 */
170int
171JLI_Launch(int argc, char ** argv, /* main argc, argc */
172 int jargc, const char** jargv, /* java args */
173 int appclassc, const char** appclassv, /* app classpath */
174 const char* fullversion, /* full version defined */
175 const char* dotversion, /* dot version defined */
176 const char* pname, /* program name */
177 const char* lname, /* launcher name */
178 jboolean javaargs, /* JAVA_ARGS */
179 jboolean cpwildcard, /* classpath wildcard*/
180 jboolean javaw, /* windows-only javaw */
181 jint ergo /* ergonomics class policy */
182)
183{
mchungea290e22011-01-21 09:43:57 -0800184 int mode = LM_UNKNOWN;
185 char *what = NULL;
duke6e45e102007-12-01 00:00:00 +0000186 char *cpath = 0;
187 char *main_class = NULL;
188 int ret;
189 InvocationFunctions ifn;
190 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700191 char jvmpath[MAXPATHLEN];
192 char jrepath[MAXPATHLEN];
michaelm5ac8c152012-03-06 20:34:38 +0000193 char jvmcfg[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000194
195 _fVersion = fullversion;
196 _dVersion = dotversion;
197 _launcher_name = lname;
198 _program_name = pname;
199 _is_java_args = javaargs;
200 _wc_enabled = cpwildcard;
201 _ergo_policy = ergo;
202
ksrini52cded22008-03-06 07:51:28 -0800203 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000204 DumpState();
205
206 /*
207 * Make sure the specified version of the JRE is running.
208 *
209 * There are three things to note about the SelectVersion() routine:
210 * 1) If the version running isn't correct, this routine doesn't
211 * return (either the correct version has been exec'd or an error
212 * was issued).
213 * 2) Argc and Argv in this scope are *not* altered by this routine.
214 * It is the responsibility of subsequent code to ignore the
215 * arguments handled by this routine.
216 * 3) As a side-effect, the variable "main_class" is guaranteed to
217 * be set (if it should ever be set). This isn't exactly the
218 * poster child for structured programming, but it is a small
219 * price to pay for not processing a jar file operand twice.
220 * (Note: This side effect has been disabled. See comment on
221 * bugid 5030265 below.)
222 */
223 SelectVersion(argc, argv, &main_class);
224
ksrinie3ec45d2010-07-09 11:04:34 -0700225 if (JLI_IsTraceLauncher()) {
226 int i;
227 printf("Command line args:\n");
228 for (i = 0; i < argc ; i++) {
229 printf("argv[%d] = %s\n", i, argv[i]);
230 }
ksrini9636e2e2011-02-03 15:41:23 -0800231 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrinie3ec45d2010-07-09 11:04:34 -0700232 }
duke6e45e102007-12-01 00:00:00 +0000233
234 CreateExecutionEnvironment(&argc, &argv,
235 jrepath, sizeof(jrepath),
michaelm5ac8c152012-03-06 20:34:38 +0000236 jvmpath, sizeof(jvmpath),
237 jvmcfg, sizeof(jvmcfg));
duke6e45e102007-12-01 00:00:00 +0000238
239 ifn.CreateJavaVM = 0;
240 ifn.GetDefaultJavaVMInitArgs = 0;
241
242 if (JLI_IsTraceLauncher()) {
243 start = CounterGet();
244 }
245
246 if (!LoadJavaVM(jvmpath, &ifn)) {
247 return(6);
248 }
249
250 if (JLI_IsTraceLauncher()) {
251 end = CounterGet();
252 }
253
254 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
255 (long)(jint)Counter2Micros(end-start));
256
257 ++argv;
258 --argc;
259
260 if (IsJavaArgs()) {
261 /* Preprocess wrapper arguments */
262 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
263 if (!AddApplicationOptions(appclassc, appclassv)) {
264 return(1);
265 }
266 } else {
267 /* Set default CLASSPATH */
268 cpath = getenv("CLASSPATH");
269 if (cpath == NULL) {
270 cpath = ".";
271 }
272 SetClassPath(cpath);
273 }
274
mchungea290e22011-01-21 09:43:57 -0800275 /* Parse command line options; if the return value of
276 * ParseArguments is false, the program should exit.
duke6e45e102007-12-01 00:00:00 +0000277 */
mchungea290e22011-01-21 09:43:57 -0800278 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
279 {
duke6e45e102007-12-01 00:00:00 +0000280 return(ret);
281 }
282
283 /* Override class path if -jar flag was specified */
mchungea290e22011-01-21 09:43:57 -0800284 if (mode == LM_JAR) {
285 SetClassPath(what); /* Override class path */
duke6e45e102007-12-01 00:00:00 +0000286 }
287
288 /* set the -Dsun.java.command pseudo property */
mchungea290e22011-01-21 09:43:57 -0800289 SetJavaCommandLineProp(what, argc, argv);
duke6e45e102007-12-01 00:00:00 +0000290
291 /* Set the -Dsun.java.launcher pseudo property */
292 SetJavaLauncherProp();
293
294 /* set the -Dsun.java.launcher.* platform properties */
295 SetJavaLauncherPlatformProps();
296
michaelm5ac8c152012-03-06 20:34:38 +0000297 return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
duke6e45e102007-12-01 00:00:00 +0000298}
ksrinie3ec45d2010-07-09 11:04:34 -0700299/*
300 * Always detach the main thread so that it appears to have ended when
301 * the application's main method exits. This will invoke the
302 * uncaught exception handler machinery if main threw an
303 * exception. An uncaught exception handler cannot change the
304 * launcher's return code except by calling System.exit.
305 *
306 * Wait for all non-daemon threads to end, then destroy the VM.
307 * This will actually create a trivial new Java waiter thread
308 * named "DestroyJavaVM", but this will be seen as a different
309 * thread from the one that executed main, even though they are
310 * the same C thread. This allows mainThread.join() and
311 * mainThread.isAlive() to work as expected.
312 */
313#define LEAVE() \
314 if ((*vm)->DetachCurrentThread(vm) != 0) { \
315 JLI_ReportErrorMessage(JVM_ERROR2); \
316 ret = 1; \
317 } \
318 (*vm)->DestroyJavaVM(vm); \
319 return ret \
duke6e45e102007-12-01 00:00:00 +0000320
ksrini20a64b22008-09-24 15:07:41 -0700321#define CHECK_EXCEPTION_NULL_LEAVE(e) \
322 if ((*env)->ExceptionOccurred(env)) { \
323 JLI_ReportExceptionDescription(env); \
ksrinie3ec45d2010-07-09 11:04:34 -0700324 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700325 } \
326 if ((e) == NULL) { \
327 JLI_ReportErrorMessage(JNI_ERROR); \
ksrinie3ec45d2010-07-09 11:04:34 -0700328 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700329 }
330
331#define CHECK_EXCEPTION_LEAVE(rv) \
332 if ((*env)->ExceptionOccurred(env)) { \
333 JLI_ReportExceptionDescription(env); \
334 ret = (rv); \
ksrinie3ec45d2010-07-09 11:04:34 -0700335 LEAVE(); \
ksrini20a64b22008-09-24 15:07:41 -0700336 }
duke6e45e102007-12-01 00:00:00 +0000337
338int JNICALL
339JavaMain(void * _args)
340{
341 JavaMainArgs *args = (JavaMainArgs *)_args;
342 int argc = args->argc;
343 char **argv = args->argv;
mchungea290e22011-01-21 09:43:57 -0800344 int mode = args->mode;
345 char *what = args->what;
duke6e45e102007-12-01 00:00:00 +0000346 InvocationFunctions ifn = args->ifn;
347
348 JavaVM *vm = 0;
349 JNIEnv *env = 0;
mchungea290e22011-01-21 09:43:57 -0800350 jclass mainClass = NULL;
duke6e45e102007-12-01 00:00:00 +0000351 jmethodID mainID;
352 jobjectArray mainArgs;
353 int ret = 0;
354 jlong start, end;
355
michaelm5ac8c152012-03-06 20:34:38 +0000356 RegisterThread();
357
duke6e45e102007-12-01 00:00:00 +0000358 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000359 start = CounterGet();
360 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700361 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000362 exit(1);
363 }
364
ksrini05fdd5a2012-01-03 08:27:37 -0800365 if (showSettings != NULL) {
366 ShowSettings(env, showSettings);
367 CHECK_EXCEPTION_LEAVE(1);
368 }
369
duke6e45e102007-12-01 00:00:00 +0000370 if (printVersion || showVersion) {
371 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700372 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000373 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700374 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000375 }
376 }
377
378 /* If the user specified neither a class name nor a JAR file */
mchungea290e22011-01-21 09:43:57 -0800379 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
duke6e45e102007-12-01 00:00:00 +0000380 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700381 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700382 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000383 }
384
385 FreeKnownVMs(); /* after last possible PrintUsage() */
386
387 if (JLI_IsTraceLauncher()) {
388 end = CounterGet();
389 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
390 (long)(jint)Counter2Micros(end-start));
391 }
392
mchungea290e22011-01-21 09:43:57 -0800393 /* At this stage, argc/argv have the application's arguments */
duke6e45e102007-12-01 00:00:00 +0000394 if (JLI_IsTraceLauncher()){
395 int i;
mchungea290e22011-01-21 09:43:57 -0800396 printf("%s is '%s'\n", launchModeNames[mode], what);
397 printf("App's argc is %d\n", argc);
duke6e45e102007-12-01 00:00:00 +0000398 for (i=0; i < argc; i++) {
399 printf(" argv[%2d] = '%s'\n", i, argv[i]);
400 }
401 }
402
403 ret = 1;
404
405 /*
406 * Get the application's main class.
407 *
408 * See bugid 5030265. The Main-Class name has already been parsed
409 * from the manifest, but not parsed properly for UTF-8 support.
410 * Hence the code here ignores the value previously extracted and
411 * uses the pre-existing code to reextract the value. This is
412 * possibly an end of release cycle expedient. However, it has
413 * also been discovered that passing some character sets through
414 * the environment has "strange" behavior on some variants of
415 * Windows. Hence, maybe the manifest parsing code local to the
416 * launcher should never be enhanced.
417 *
418 * Hence, future work should either:
419 * 1) Correct the local parsing code and verify that the
420 * Main-Class attribute gets properly passed through
421 * all environments,
422 * 2) Remove the vestages of maintaining main_class through
423 * the environment (and remove these comments).
424 */
mchungea290e22011-01-21 09:43:57 -0800425 mainClass = LoadMainClass(env, mode, what);
ksrini20a64b22008-09-24 15:07:41 -0700426 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
michaelm5ac8c152012-03-06 20:34:38 +0000427 PostJVMInit(env, mainClass, vm);
ksrini20a64b22008-09-24 15:07:41 -0700428 /*
429 * The LoadMainClass not only loads the main class, it will also ensure
430 * that the main method's signature is correct, therefore further checking
431 * is not required. The main method is invoked here so that extraneous java
432 * stacks are not in the application stack trace.
433 */
duke6e45e102007-12-01 00:00:00 +0000434 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
435 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700436 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000437
438 /* Build argument array */
439 mainArgs = NewPlatformStringArray(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700440 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000441
442 /* Invoke main method. */
443 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
444
445 /*
446 * The launcher's exit code (in the absence of calls to
447 * System.exit) will be non-zero if main threw an exception.
448 */
449 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700450 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000451}
452
duke6e45e102007-12-01 00:00:00 +0000453/*
454 * Checks the command line options to find which JVM type was
455 * specified. If no command line option was given for the JVM type,
456 * the default type is used. The environment variable
457 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
458 * checked as ways of specifying which JVM type to invoke.
459 */
460char *
461CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
462 int i, argi;
463 int argc;
464 char **newArgv;
465 int newArgvIdx = 0;
466 int isVMType;
467 int jvmidx = -1;
468 char *jvmtype = getenv("JDK_ALTERNATE_VM");
469
470 argc = *pargc;
471
472 /* To make things simpler we always copy the argv array */
473 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
474
475 /* The program name is always present */
476 newArgv[newArgvIdx++] = (*argv)[0];
477
478 for (argi = 1; argi < argc; argi++) {
479 char *arg = (*argv)[argi];
480 isVMType = 0;
481
482 if (IsJavaArgs()) {
483 if (arg[0] != '-') {
484 newArgv[newArgvIdx++] = arg;
485 continue;
486 }
487 } else {
488 if (JLI_StrCmp(arg, "-classpath") == 0 ||
489 JLI_StrCmp(arg, "-cp") == 0) {
490 newArgv[newArgvIdx++] = arg;
491 argi++;
492 if (argi < argc) {
493 newArgv[newArgvIdx++] = (*argv)[argi];
494 }
495 continue;
496 }
497 if (arg[0] != '-') break;
498 }
499
500 /* Did the user pass an explicit VM type? */
501 i = KnownVMIndex(arg);
502 if (i >= 0) {
503 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
504 isVMType = 1;
505 *pargc = *pargc - 1;
506 }
507
508 /* Did the user specify an "alternate" VM? */
509 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
510 isVMType = 1;
511 jvmtype = arg+((arg[1]=='X')? 10 : 12);
512 jvmidx = -1;
513 }
514
515 if (!isVMType) {
516 newArgv[newArgvIdx++] = arg;
517 }
518 }
519
520 /*
521 * Finish copying the arguments if we aborted the above loop.
522 * NOTE that if we aborted via "break" then we did NOT copy the
523 * last argument above, and in addition argi will be less than
524 * argc.
525 */
526 while (argi < argc) {
527 newArgv[newArgvIdx++] = (*argv)[argi];
528 argi++;
529 }
530
531 /* argv is null-terminated */
532 newArgv[newArgvIdx] = 0;
533
534 /* Copy back argv */
535 *argv = newArgv;
536 *pargc = newArgvIdx;
537
538 /* use the default VM type if not specified (no alias processing) */
539 if (jvmtype == NULL) {
540 char* result = knownVMs[0].name+1;
541 /* Use a different VM type if we are on a server class machine? */
542 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
543 (ServerClassMachine() == JNI_TRUE)) {
544 result = knownVMs[0].server_class+1;
545 }
546 JLI_TraceLauncher("Default VM: %s\n", result);
547 return result;
548 }
549
550 /* if using an alternate VM, no alias processing */
551 if (jvmidx < 0)
552 return jvmtype;
553
554 /* Resolve aliases first */
555 {
556 int loopCount = 0;
557 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
558 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
559
560 if (loopCount > knownVMsCount) {
561 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700562 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000563 exit(1);
564 } else {
565 return "ERROR";
566 /* break; */
567 }
568 }
569
570 if (nextIdx < 0) {
571 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700572 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000573 exit(1);
574 } else {
575 return "ERROR";
576 }
577 }
578 jvmidx = nextIdx;
579 jvmtype = knownVMs[jvmidx].name+1;
580 loopCount++;
581 }
582 }
583
584 switch (knownVMs[jvmidx].flag) {
585 case VM_WARN:
586 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700587 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000588 }
589 /* fall through */
590 case VM_IGNORE:
591 jvmtype = knownVMs[jvmidx=0].name + 1;
592 /* fall through */
593 case VM_KNOWN:
594 break;
595 case VM_ERROR:
596 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700597 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000598 exit(1);
599 } else {
600 return "ERROR";
601 }
602 }
603
604 return jvmtype;
605}
606
607/* copied from HotSpot function "atomll()" */
608static int
ksrini8e20e1c2010-11-23 16:52:39 -0800609parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000610 jlong n = 0;
611 int args_read = sscanf(s, jlong_format_specifier(), &n);
612 if (args_read != 1) {
613 return 0;
614 }
615 while (*s != '\0' && *s >= '0' && *s <= '9') {
616 s++;
617 }
618 // 4705540: illegal if more characters are found after the first non-digit
619 if (JLI_StrLen(s) > 1) {
620 return 0;
621 }
622 switch (*s) {
623 case 'T': case 't':
624 *result = n * GB * KB;
625 return 1;
626 case 'G': case 'g':
627 *result = n * GB;
628 return 1;
629 case 'M': case 'm':
630 *result = n * MB;
631 return 1;
632 case 'K': case 'k':
633 *result = n * KB;
634 return 1;
635 case '\0':
636 *result = n;
637 return 1;
638 default:
639 /* Create JVM with default stack and let VM handle malformed -Xss string*/
640 return 0;
641 }
642}
643
644/*
645 * Adds a new VM option with the given given name and value.
646 */
647void
648AddOption(char *str, void *info)
649{
650 /*
651 * Expand options array if needed to accommodate at least one more
652 * VM option.
653 */
654 if (numOptions >= maxOptions) {
655 if (options == 0) {
656 maxOptions = 4;
657 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
658 } else {
659 JavaVMOption *tmp;
660 maxOptions *= 2;
661 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
662 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
663 JLI_MemFree(options);
664 options = tmp;
665 }
666 }
667 options[numOptions].optionString = str;
668 options[numOptions++].extraInfo = info;
669
670 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800671 jlong tmp;
672 if (parse_size(str + 4, &tmp)) {
673 threadStackSize = tmp;
674 }
675 }
676
677 if (JLI_StrCCmp(str, "-Xmx") == 0) {
678 jlong tmp;
679 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800680 maxHeapSize = tmp;
681 }
682 }
683
684 if (JLI_StrCCmp(str, "-Xms") == 0) {
685 jlong tmp;
686 if (parse_size(str + 4, &tmp)) {
mchungea290e22011-01-21 09:43:57 -0800687 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800688 }
duke6e45e102007-12-01 00:00:00 +0000689 }
690}
691
692static void
693SetClassPath(const char *s)
694{
695 char *def;
martinc0ca3352009-06-22 16:41:27 -0700696 const char *orig = s;
697 static const char format[] = "-Djava.class.path=%s";
ksrinie1a34382012-04-24 10:37:01 -0700698 /*
699 * usually we should not get a null pointer, but there are cases where
700 * we might just get one, in which case we simply ignore it, and let the
701 * caller deal with it
702 */
703 if (s == NULL)
704 return;
duke6e45e102007-12-01 00:00:00 +0000705 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700706 def = JLI_MemAlloc(sizeof(format)
707 - 2 /* strlen("%s") */
708 + JLI_StrLen(s));
709 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000710 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700711 if (s != orig)
712 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000713}
714
715/*
716 * The SelectVersion() routine ensures that an appropriate version of
717 * the JRE is running. The specification for the appropriate version
718 * is obtained from either the manifest of a jar file (preferred) or
719 * from command line options.
720 * The routine also parses splash screen command line options and
721 * passes on their values in private environment variables.
722 */
723static void
724SelectVersion(int argc, char **argv, char **main_class)
725{
726 char *arg;
727 char **new_argv;
728 char **new_argp;
729 char *operand;
730 char *version = NULL;
731 char *jre = NULL;
732 int jarflag = 0;
733 int headlessflag = 0;
734 int restrict_search = -1; /* -1 implies not known */
735 manifest_info info;
736 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
737 char *splash_file_name = NULL;
738 char *splash_jar_name = NULL;
739 char *env_in;
740 int res;
741
742 /*
743 * If the version has already been selected, set *main_class
744 * with the value passed through the environment (if any) and
745 * simply return.
746 */
747 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
748 if (*env_in != '\0')
749 *main_class = JLI_StringDup(env_in);
750 return;
751 }
752
753 /*
754 * Scan through the arguments for options relevant to multiple JRE
755 * support. For reference, the command line syntax is defined as:
756 *
757 * SYNOPSIS
758 * java [options] class [argument...]
759 *
760 * java [options] -jar file.jar [argument...]
761 *
762 * As the scan is performed, make a copy of the argument list with
763 * the version specification options (new to 1.5) removed, so that
764 * a version less than 1.5 can be exec'd.
765 *
766 * Note that due to the syntax of the native Windows interface
767 * CreateProcess(), processing similar to the following exists in
768 * the Windows platform specific routine ExecJRE (in java_md.c).
769 * Changes here should be reproduced there.
770 */
771 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
772 new_argv[0] = argv[0];
773 new_argp = &new_argv[1];
774 argc--;
775 argv++;
776 while ((arg = *argv) != 0 && *arg == '-') {
777 if (JLI_StrCCmp(arg, "-version:") == 0) {
778 version = arg + 9;
779 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
780 restrict_search = 1;
781 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
782 restrict_search = 0;
783 } else {
784 if (JLI_StrCmp(arg, "-jar") == 0)
785 jarflag = 1;
786 /* deal with "unfortunate" classpath syntax */
787 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
788 (argc >= 2)) {
789 *new_argp++ = arg;
790 argc--;
791 argv++;
792 arg = *argv;
793 }
794
795 /*
796 * Checking for headless toolkit option in the some way as AWT does:
797 * "true" means true and any other value means false
798 */
799 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
800 headlessflag = 1;
801 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
802 headlessflag = 0;
803 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
804 splash_file_name = arg+8;
805 }
806 *new_argp++ = arg;
807 }
808 argc--;
809 argv++;
810 }
811 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
812 operand = NULL;
813 } else {
814 argc--;
815 *new_argp++ = operand = *argv++;
816 }
817 while (argc-- > 0) /* Copy over [argument...] */
818 *new_argp++ = *argv++;
819 *new_argp = NULL;
820
821 /*
822 * If there is a jar file, read the manifest. If the jarfile can't be
823 * read, the manifest can't be read from the jar file, or the manifest
824 * is corrupt, issue the appropriate error messages and exit.
825 *
826 * Even if there isn't a jar file, construct a manifest_info structure
827 * containing the command line information. It's a convenient way to carry
828 * this data around.
829 */
830 if (jarflag && operand) {
831 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
832 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700833 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000834 else
ksrini0e817162008-08-26 10:21:20 -0700835 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000836 exit(1);
837 }
838
839 /*
840 * Command line splash screen option should have precedence
841 * over the manifest, so the manifest data is used only if
842 * splash_file_name has not been initialized above during command
843 * line parsing
844 */
845 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
846 splash_file_name = info.splashscreen_image_file_name;
847 splash_jar_name = operand;
848 }
849 } else {
850 info.manifest_version = NULL;
851 info.main_class = NULL;
852 info.jre_version = NULL;
853 info.jre_restrict_search = 0;
854 }
855
856 /*
857 * Passing on splash screen info in environment variables
858 */
859 if (splash_file_name && !headlessflag) {
860 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
861 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
862 JLI_StrCat(splash_file_entry, splash_file_name);
863 putenv(splash_file_entry);
864 }
865 if (splash_jar_name && !headlessflag) {
866 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
867 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
868 JLI_StrCat(splash_jar_entry, splash_jar_name);
869 putenv(splash_jar_entry);
870 }
871
872 /*
873 * The JRE-Version and JRE-Restrict-Search values (if any) from the
874 * manifest are overwritten by any specified on the command line.
875 */
876 if (version != NULL)
877 info.jre_version = version;
878 if (restrict_search != -1)
879 info.jre_restrict_search = restrict_search;
880
881 /*
882 * "Valid" returns (other than unrecoverable errors) follow. Set
883 * main_class as a side-effect of this routine.
884 */
885 if (info.main_class != NULL)
886 *main_class = JLI_StringDup(info.main_class);
887
888 /*
889 * If no version selection information is found either on the command
890 * line or in the manifest, simply return.
891 */
892 if (info.jre_version == NULL) {
893 JLI_FreeManifest();
894 JLI_MemFree(new_argv);
895 return;
896 }
897
898 /*
899 * Check for correct syntax of the version specification (JSR 56).
900 */
901 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700902 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000903 exit(1);
904 }
905
906 /*
907 * Find the appropriate JVM on the system. Just to be as forgiving as
908 * possible, if the standard algorithms don't locate an appropriate
909 * jre, check to see if the one running will satisfy the requirements.
910 * This can happen on systems which haven't been set-up for multiple
911 * JRE support.
912 */
913 jre = LocateJRE(&info);
914 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
915 (info.jre_version?info.jre_version:"null"),
916 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
917
918 if (jre == NULL) {
919 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
920 JLI_FreeManifest();
921 JLI_MemFree(new_argv);
922 return;
923 } else {
ksrini0e817162008-08-26 10:21:20 -0700924 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000925 exit(1);
926 }
927 }
928
929 /*
930 * If I'm not the chosen one, exec the chosen one. Returning from
931 * ExecJRE indicates that I am indeed the chosen one.
932 *
933 * The private environment variable _JAVA_VERSION_SET is used to
934 * prevent the chosen one from re-reading the manifest file and
935 * using the values found within to override the (potential) command
936 * line flags stripped from argv (because the target may not
937 * understand them). Passing the MainClass value is an optimization
938 * to avoid locating, expanding and parsing the manifest extra
939 * times.
940 */
ksrini8760ca92008-09-04 09:43:32 -0700941 if (info.main_class != NULL) {
942 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
943 (void)JLI_StrCat(env_entry, info.main_class);
944 } else {
asaha80ccd422009-04-16 21:08:04 -0700945 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700946 exit(1);
947 }
948 }
duke6e45e102007-12-01 00:00:00 +0000949 (void)putenv(env_entry);
950 ExecJRE(jre, new_argv);
951 JLI_FreeManifest();
952 JLI_MemFree(new_argv);
953 return;
954}
955
956/*
957 * Parses command line arguments. Returns JNI_FALSE if launcher
958 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -0800959 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +0000960 * process return value) is set to 0 for a normal exit.
961 */
962static jboolean
mchungea290e22011-01-21 09:43:57 -0800963ParseArguments(int *pargc, char ***pargv,
964 int *pmode, char **pwhat,
965 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +0000966{
967 int argc = *pargc;
968 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -0800969 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +0000970 char *arg;
971
972 *pret = 0;
973
974 while ((arg = *argv) != 0 && *arg == '-') {
975 argv++; --argc;
976 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
977 ARG_CHECK (argc, ARG_ERROR1, arg);
978 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -0800979 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +0000980 argv++; --argc;
981 } else if (JLI_StrCmp(arg, "-jar") == 0) {
982 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -0800983 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +0000984 } else if (JLI_StrCmp(arg, "-help") == 0 ||
985 JLI_StrCmp(arg, "-h") == 0 ||
986 JLI_StrCmp(arg, "-?") == 0) {
987 printUsage = JNI_TRUE;
988 return JNI_TRUE;
989 } else if (JLI_StrCmp(arg, "-version") == 0) {
990 printVersion = JNI_TRUE;
991 return JNI_TRUE;
992 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
993 showVersion = JNI_TRUE;
994 } else if (JLI_StrCmp(arg, "-X") == 0) {
995 printXUsage = JNI_TRUE;
996 return JNI_TRUE;
997/*
ksrini8e20e1c2010-11-23 16:52:39 -0800998 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
999 * In the latter case, any SUBOPT value not recognized will default to "all"
1000 */
1001 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1002 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1003 showSettings = arg;
ksrini9636e2e2011-02-03 15:41:23 -08001004 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1005 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrini8e20e1c2010-11-23 16:52:39 -08001006/*
duke6e45e102007-12-01 00:00:00 +00001007 * The following case provide backward compatibility with old-style
1008 * command line options.
1009 */
1010 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001011 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001012 return JNI_FALSE;
1013 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1014 AddOption("-verbose:gc", NULL);
1015 } else if (JLI_StrCmp(arg, "-t") == 0) {
1016 AddOption("-Xt", NULL);
1017 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1018 AddOption("-Xtm", NULL);
1019 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1020 AddOption("-Xdebug", NULL);
1021 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1022 AddOption("-Xnoclassgc", NULL);
1023 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1024 AddOption("-Xverify:all", NULL);
1025 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1026 AddOption("-Xverify:all", NULL);
1027 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1028 AddOption("-Xverify:remote", NULL);
1029 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1030 AddOption("-Xverify:none", NULL);
1031 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1032 char *p = arg + 5;
1033 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1034 if (*p) {
1035 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1036 } else {
1037 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1038 }
1039 AddOption(tmp, NULL);
1040 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1041 JLI_StrCCmp(arg, "-oss") == 0 ||
1042 JLI_StrCCmp(arg, "-ms") == 0 ||
1043 JLI_StrCCmp(arg, "-mx") == 0) {
1044 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1045 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1046 AddOption(tmp, NULL);
1047 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1048 JLI_StrCmp(arg, "-cs") == 0 ||
1049 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1050 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001051 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001052 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1053 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1054 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1055 JLI_StrCCmp(arg, "-splash:") == 0) {
1056 ; /* Ignore machine independent options already handled */
michaelm5ac8c152012-03-06 20:34:38 +00001057 } else if (ProcessPlatformOption(arg)) {
1058 ; /* Processing of platform dependent options */
1059 } else if (RemovableOption(arg)) {
duke6e45e102007-12-01 00:00:00 +00001060 ; /* Do not pass option to vm. */
1061 } else {
1062 AddOption(arg, NULL);
1063 }
1064 }
1065
1066 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001067 *pwhat = *argv++;
1068 }
1069
1070 if (*pwhat == NULL) {
1071 *pret = 1;
1072 } else if (mode == LM_UNKNOWN) {
1073 /* default to LM_CLASS if -jar and -cp option are
1074 * not specified */
1075 mode = LM_CLASS;
1076 }
1077
1078 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001079 *pargc = argc;
1080 *pargv = argv;
1081 }
mchungea290e22011-01-21 09:43:57 -08001082
1083 *pmode = mode;
1084
duke6e45e102007-12-01 00:00:00 +00001085 return JNI_TRUE;
1086}
1087
1088/*
1089 * Initializes the Java Virtual Machine. Also frees options array when
1090 * finished.
1091 */
1092static jboolean
1093InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1094{
1095 JavaVMInitArgs args;
1096 jint r;
1097
1098 memset(&args, 0, sizeof(args));
1099 args.version = JNI_VERSION_1_2;
1100 args.nOptions = numOptions;
1101 args.options = options;
1102 args.ignoreUnrecognized = JNI_FALSE;
1103
1104 if (JLI_IsTraceLauncher()) {
1105 int i = 0;
1106 printf("JavaVM args:\n ");
1107 printf("version 0x%08lx, ", (long)args.version);
1108 printf("ignoreUnrecognized is %s, ",
1109 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1110 printf("nOptions is %ld\n", (long)args.nOptions);
1111 for (i = 0; i < numOptions; i++)
1112 printf(" option[%2d] = '%s'\n",
1113 i, args.options[i].optionString);
1114 }
1115
1116 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1117 JLI_MemFree(options);
1118 return r == JNI_OK;
1119}
1120
ksrini7d9872e2011-03-20 08:41:33 -07001121static jclass helperClass = NULL;
1122
1123static jclass
1124GetLauncherHelperClass(JNIEnv *env) {
1125 if (helperClass == NULL) {
1126 NULL_CHECK0(helperClass = FindBootStrapClass(env,
1127 "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001128 }
ksrini7d9872e2011-03-20 08:41:33 -07001129 return helperClass;
duke6e45e102007-12-01 00:00:00 +00001130}
1131
ksrini7d9872e2011-03-20 08:41:33 -07001132static jmethodID makePlatformStringMID = NULL;
duke6e45e102007-12-01 00:00:00 +00001133/*
1134 * Returns a new Java string object for the specified platform string.
1135 */
1136static jstring
1137NewPlatformString(JNIEnv *env, char *s)
1138{
1139 int len = (int)JLI_StrLen(s);
duke6e45e102007-12-01 00:00:00 +00001140 jbyteArray ary;
ksrini7d9872e2011-03-20 08:41:33 -07001141 jclass cls = GetLauncherHelperClass(env);
1142 NULL_CHECK0(cls);
duke6e45e102007-12-01 00:00:00 +00001143 if (s == NULL)
1144 return 0;
duke6e45e102007-12-01 00:00:00 +00001145
1146 ary = (*env)->NewByteArray(env, len);
1147 if (ary != 0) {
1148 jstring str = 0;
1149 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1150 if (!(*env)->ExceptionOccurred(env)) {
ksrini7d9872e2011-03-20 08:41:33 -07001151 if (makePlatformStringMID == NULL) {
1152 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1153 cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
duke6e45e102007-12-01 00:00:00 +00001154 }
ksrini7d9872e2011-03-20 08:41:33 -07001155 str = (*env)->CallStaticObjectMethod(env, cls,
1156 makePlatformStringMID, USE_STDERR, ary);
duke6e45e102007-12-01 00:00:00 +00001157 (*env)->DeleteLocalRef(env, ary);
1158 return str;
1159 }
1160 }
1161 return 0;
1162}
1163
1164/*
1165 * Returns a new array of Java string objects for the specified
1166 * array of platform strings.
1167 */
1168static jobjectArray
1169NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1170{
1171 jarray cls;
1172 jarray ary;
1173 int i;
1174
ksrini20a64b22008-09-24 15:07:41 -07001175 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001176 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1177 for (i = 0; i < strc; i++) {
1178 jstring str = NewPlatformString(env, *strv++);
1179 NULL_CHECK0(str);
1180 (*env)->SetObjectArrayElement(env, ary, i, str);
1181 (*env)->DeleteLocalRef(env, str);
1182 }
1183 return ary;
1184}
1185
1186/*
ksrini20a64b22008-09-24 15:07:41 -07001187 * Loads a class and verifies that the main class is present and it is ok to
1188 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001189 */
1190static jclass
mchungea290e22011-01-21 09:43:57 -08001191LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001192{
ksrini20a64b22008-09-24 15:07:41 -07001193 jmethodID mid;
1194 jstring str;
1195 jobject result;
duke6e45e102007-12-01 00:00:00 +00001196 jlong start, end;
ksrini7d9872e2011-03-20 08:41:33 -07001197 jclass cls = GetLauncherHelperClass(env);
1198 NULL_CHECK0(cls);
ksrini20a64b22008-09-24 15:07:41 -07001199 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001200 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001201 }
ksrini7d9872e2011-03-20 08:41:33 -07001202 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1203 "checkAndLoadMain",
1204 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1205
ksriniac5e02f2012-01-11 08:14:47 -08001206 str = NewPlatformString(env, name);
ksrini7d9872e2011-03-20 08:41:33 -07001207 result = (*env)->CallStaticObjectMethod(env, cls, mid, USE_STDERR, mode, str);
duke6e45e102007-12-01 00:00:00 +00001208
1209 if (JLI_IsTraceLauncher()) {
1210 end = CounterGet();
1211 printf("%ld micro seconds to load main class\n",
1212 (long)(jint)Counter2Micros(end-start));
1213 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1214 }
1215
ksrini20a64b22008-09-24 15:07:41 -07001216 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001217}
1218
duke6e45e102007-12-01 00:00:00 +00001219/*
1220 * For tools, convert command line args thus:
1221 * javac -cp foo:foo/"*" -J-ms32m ...
1222 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1223 *
1224 * Takes 4 parameters, and returns the populated arguments
1225 */
1226static void
1227TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1228{
1229 int argc = *pargc;
1230 char **argv = *pargv;
1231 int nargc = argc + jargc;
1232 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1233 int i;
1234
1235 *pargc = nargc;
1236 *pargv = nargv;
1237
1238 /* Copy the VM arguments (i.e. prefixed with -J) */
1239 for (i = 0; i < jargc; i++) {
1240 const char *arg = jargv[i];
1241 if (arg[0] == '-' && arg[1] == 'J') {
1242 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1243 }
1244 }
1245
1246 for (i = 0; i < argc; i++) {
1247 char *arg = argv[i];
1248 if (arg[0] == '-' && arg[1] == 'J') {
1249 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001250 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001251 exit(1);
1252 }
1253 *nargv++ = arg + 2;
1254 }
1255 }
1256
1257 /* Copy the rest of the arguments */
1258 for (i = 0; i < jargc ; i++) {
1259 const char *arg = jargv[i];
1260 if (arg[0] != '-' || arg[1] != 'J') {
1261 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1262 }
1263 }
1264 for (i = 0; i < argc; i++) {
1265 char *arg = argv[i];
1266 if (arg[0] == '-') {
1267 if (arg[1] == 'J')
1268 continue;
1269 if (IsWildCardEnabled() && arg[1] == 'c'
1270 && (JLI_StrCmp(arg, "-cp") == 0 ||
1271 JLI_StrCmp(arg, "-classpath") == 0)
1272 && i < argc - 1) {
1273 *nargv++ = arg;
1274 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1275 i++;
1276 continue;
1277 }
1278 }
1279 *nargv++ = arg;
1280 }
1281 *nargv = 0;
1282}
1283
1284/*
1285 * For our tools, we try to add 3 VM options:
1286 * -Denv.class.path=<envcp>
1287 * -Dapplication.home=<apphome>
1288 * -Djava.class.path=<appcp>
1289 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1290 * tells javac where to find binary classes through this environment
1291 * variable. Notice that users will be able to compile against our
1292 * tools classes (sun.tools.javac.Main) only if they explicitly add
1293 * tools.jar to CLASSPATH.
1294 * <apphome> is the directory where the application is installed.
1295 * <appcp> is the classpath to where our apps' classfiles are.
1296 */
1297static jboolean
1298AddApplicationOptions(int cpathc, const char **cpathv)
1299{
1300 char *envcp, *appcp, *apphome;
1301 char home[MAXPATHLEN]; /* application home */
1302 char separator[] = { PATH_SEPARATOR, '\0' };
1303 int size, i;
1304
1305 {
1306 const char *s = getenv("CLASSPATH");
1307 if (s) {
1308 s = (char *) JLI_WildcardExpandClasspath(s);
1309 /* 40 for -Denv.class.path= */
1310 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1311 sprintf(envcp, "-Denv.class.path=%s", s);
1312 AddOption(envcp, NULL);
1313 }
1314 }
1315
1316 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001317 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001318 return JNI_FALSE;
1319 }
1320
1321 /* 40 for '-Dapplication.home=' */
1322 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1323 sprintf(apphome, "-Dapplication.home=%s", home);
1324 AddOption(apphome, NULL);
1325
1326 /* How big is the application's classpath? */
1327 size = 40; /* 40: "-Djava.class.path=" */
1328 for (i = 0; i < cpathc; i++) {
1329 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1330 }
1331 appcp = (char *)JLI_MemAlloc(size + 1);
1332 JLI_StrCpy(appcp, "-Djava.class.path=");
1333 for (i = 0; i < cpathc; i++) {
1334 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1335 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1336 JLI_StrCat(appcp, separator); /* ; */
1337 }
1338 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1339 AddOption(appcp, NULL);
1340 return JNI_TRUE;
1341}
1342
1343/*
1344 * inject the -Dsun.java.command pseudo property into the args structure
1345 * this pseudo property is used in the HotSpot VM to expose the
1346 * Java class name and arguments to the main method to the VM. The
1347 * HotSpot VM uses this pseudo property to store the Java class name
1348 * (or jar file name) and the arguments to the class's main method
1349 * to the instrumentation memory region. The sun.java.command pseudo
1350 * property is not exported by HotSpot to the Java layer.
1351 */
1352void
mchungea290e22011-01-21 09:43:57 -08001353SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001354{
1355
1356 int i = 0;
1357 size_t len = 0;
1358 char* javaCommand = NULL;
1359 char* dashDstr = "-Dsun.java.command=";
1360
mchungea290e22011-01-21 09:43:57 -08001361 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001362 /* unexpected, one of these should be set. just return without
1363 * setting the property
1364 */
1365 return;
1366 }
1367
duke6e45e102007-12-01 00:00:00 +00001368 /* determine the amount of memory to allocate assuming
1369 * the individual components will be space separated
1370 */
mchungea290e22011-01-21 09:43:57 -08001371 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001372 for (i = 0; i < argc; i++) {
1373 len += JLI_StrLen(argv[i]) + 1;
1374 }
1375
1376 /* allocate the memory */
1377 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1378
1379 /* build the -D string */
1380 *javaCommand = '\0';
1381 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001382 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001383
1384 for (i = 0; i < argc; i++) {
1385 /* the components of the string are space separated. In
1386 * the case of embedded white space, the relationship of
1387 * the white space separated components to their true
1388 * positional arguments will be ambiguous. This issue may
1389 * be addressed in a future release.
1390 */
1391 JLI_StrCat(javaCommand, " ");
1392 JLI_StrCat(javaCommand, argv[i]);
1393 }
1394
1395 AddOption(javaCommand, NULL);
1396}
1397
1398/*
1399 * JVM would like to know if it's created by a standard Sun launcher, or by
1400 * user native application, the following property indicates the former.
1401 */
mchungea290e22011-01-21 09:43:57 -08001402void
1403SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001404 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1405}
1406
1407/*
1408 * Prints the version information from the java.version and other properties.
1409 */
1410static void
1411PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1412{
1413 jclass ver;
1414 jmethodID print;
1415
ksrini20a64b22008-09-24 15:07:41 -07001416 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001417 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1418 ver,
1419 (extraLF == JNI_TRUE) ? "println" : "print",
1420 "()V"
1421 )
1422 );
1423
1424 (*env)->CallStaticVoidMethod(env, ver, print);
1425}
1426
1427/*
ksrini8e20e1c2010-11-23 16:52:39 -08001428 * Prints all the Java settings, see the java implementation for more details.
1429 */
1430static void
1431ShowSettings(JNIEnv *env, char *optString)
1432{
ksrini8e20e1c2010-11-23 16:52:39 -08001433 jmethodID showSettingsID;
1434 jstring joptString;
ksrini7d9872e2011-03-20 08:41:33 -07001435 jclass cls = GetLauncherHelperClass(env);
1436 NULL_CHECK(cls);
ksrini8e20e1c2010-11-23 16:52:39 -08001437 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001438 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001439 joptString = (*env)->NewStringUTF(env, optString);
1440 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
ksrini7d9872e2011-03-20 08:41:33 -07001441 USE_STDERR,
ksrini8e20e1c2010-11-23 16:52:39 -08001442 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001443 (jlong)initialHeapSize,
1444 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001445 (jlong)threadStackSize,
1446 ServerClassMachine());
1447}
1448
1449/*
ksrini20a64b22008-09-24 15:07:41 -07001450 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001451 */
1452static void
1453PrintUsage(JNIEnv* env, jboolean doXUsage)
1454{
duke6e45e102007-12-01 00:00:00 +00001455 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1456 jstring jprogname, vm1, vm2;
1457 int i;
ksrini7d9872e2011-03-20 08:41:33 -07001458 jclass cls = GetLauncherHelperClass(env);
1459 NULL_CHECK(cls);
duke6e45e102007-12-01 00:00:00 +00001460 if (doXUsage) {
1461 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1462 "printXUsageMessage", "(Z)V"));
ksrini7d9872e2011-03-20 08:41:33 -07001463 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001464 } else {
1465 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1466 "initHelpMessage", "(Ljava/lang/String;)V"));
1467
1468 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1469 "(Ljava/lang/String;Ljava/lang/String;)V"));
1470
1471 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1472 "appendVmSynonymMessage",
1473 "(Ljava/lang/String;Ljava/lang/String;)V"));
1474 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1475 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1476
1477 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1478 "printHelpMessage", "(Z)V"));
1479
1480 jprogname = (*env)->NewStringUTF(env, _program_name);
1481
1482 /* Initialize the usage message with the usual preamble */
1483 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1484
1485
1486 /* Assemble the other variant part of the usage */
1487 if ((knownVMs[0].flag == VM_KNOWN) ||
1488 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1489 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1490 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1491 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1492 }
1493 for (i=1; i<knownVMsCount; i++) {
1494 if (knownVMs[i].flag == VM_KNOWN) {
1495 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1496 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1497 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1498 }
1499 }
1500 for (i=1; i<knownVMsCount; i++) {
1501 if (knownVMs[i].flag == VM_ALIASED_TO) {
1502 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1503 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1504 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1505 }
1506 }
1507
1508 /* The first known VM is the default */
1509 {
1510 jboolean isServerClassMachine = ServerClassMachine();
1511
1512 const char* defaultVM = knownVMs[0].name+1;
1513 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1514 defaultVM = knownVMs[0].server_class+1;
1515 }
1516
1517 vm1 = (*env)->NewStringUTF(env, defaultVM);
1518 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1519 }
1520
1521 /* Complete the usage message and print to stderr*/
ksrini7d9872e2011-03-20 08:41:33 -07001522 (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001523 }
1524 return;
1525}
1526
1527/*
1528 * Read the jvm.cfg file and fill the knownJVMs[] array.
1529 *
1530 * The functionality of the jvm.cfg file is subject to change without
1531 * notice and the mechanism will be removed in the future.
1532 *
1533 * The lexical structure of the jvm.cfg file is as follows:
1534 *
1535 * jvmcfg := { vmLine }
1536 * vmLine := knownLine
1537 * | aliasLine
1538 * | warnLine
1539 * | ignoreLine
1540 * | errorLine
1541 * | predicateLine
1542 * | commentLine
1543 * knownLine := flag "KNOWN" EOL
1544 * warnLine := flag "WARN" EOL
1545 * ignoreLine := flag "IGNORE" EOL
1546 * errorLine := flag "ERROR" EOL
1547 * aliasLine := flag "ALIASED_TO" flag EOL
1548 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1549 * commentLine := "#" text EOL
1550 * flag := "-" identifier
1551 *
1552 * The semantics are that when someone specifies a flag on the command line:
1553 * - if the flag appears on a knownLine, then the identifier is used as
1554 * the name of the directory holding the JVM library (the name of the JVM).
1555 * - if the flag appears as the first flag on an aliasLine, the identifier
1556 * of the second flag is used as the name of the JVM.
1557 * - if the flag appears on a warnLine, the identifier is used as the
1558 * name of the JVM, but a warning is generated.
1559 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1560 * name of a JVM, but the identifier is ignored and the default vm used
1561 * - if the flag appears on an errorLine, an error is generated.
1562 * - if the flag appears as the first flag on a predicateLine, and
1563 * the machine on which you are running passes the predicate indicated,
1564 * then the identifier of the second flag is used as the name of the JVM,
1565 * otherwise the identifier of the first flag is used as the name of the JVM.
1566 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1567 * file determines the name of the JVM.
1568 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1569 * since they only make sense if someone hasn't specified the name of the
1570 * JVM on the command line.
1571 *
1572 * The intent of the jvm.cfg file is to allow several JVM libraries to
1573 * be installed in different subdirectories of a single JRE installation,
1574 * for space-savings and convenience in testing.
1575 * The intent is explicitly not to provide a full aliasing or predicate
1576 * mechanism.
1577 */
1578jint
michaelm5ac8c152012-03-06 20:34:38 +00001579ReadKnownVMs(const char *jvmCfgName, jboolean speculative)
duke6e45e102007-12-01 00:00:00 +00001580{
1581 FILE *jvmCfg;
duke6e45e102007-12-01 00:00:00 +00001582 char line[MAXPATHLEN+20];
1583 int cnt = 0;
1584 int lineno = 0;
1585 jlong start, end;
1586 int vmType;
1587 char *tmpPtr;
1588 char *altVMName = NULL;
1589 char *serverClassVMName = NULL;
1590 static char *whiteSpace = " \t";
1591 if (JLI_IsTraceLauncher()) {
1592 start = CounterGet();
1593 }
duke6e45e102007-12-01 00:00:00 +00001594
1595 jvmCfg = fopen(jvmCfgName, "r");
1596 if (jvmCfg == NULL) {
1597 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001598 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001599 exit(1);
1600 } else {
1601 return -1;
1602 }
1603 }
1604 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1605 vmType = VM_UNKNOWN;
1606 lineno++;
1607 if (line[0] == '#')
1608 continue;
1609 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001610 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001611 }
1612 if (cnt >= knownVMsLimit) {
1613 GrowKnownVMs(cnt);
1614 }
1615 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1616 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1617 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001618 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001619 } else {
1620 /* Null-terminate this string for JLI_StringDup below */
1621 *tmpPtr++ = 0;
1622 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1623 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001624 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001625 } else {
1626 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1627 vmType = VM_KNOWN;
1628 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1629 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1630 if (*tmpPtr != 0) {
1631 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1632 }
1633 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001634 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001635 } else {
1636 /* Null terminate altVMName */
1637 altVMName = tmpPtr;
1638 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1639 *tmpPtr = 0;
1640 vmType = VM_ALIASED_TO;
1641 }
1642 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1643 vmType = VM_WARN;
1644 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1645 vmType = VM_IGNORE;
1646 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1647 vmType = VM_ERROR;
1648 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1649 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1650 if (*tmpPtr != 0) {
1651 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1652 }
1653 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001654 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001655 } else {
1656 /* Null terminate server class VM name */
1657 serverClassVMName = tmpPtr;
1658 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1659 *tmpPtr = 0;
1660 vmType = VM_IF_SERVER_CLASS;
1661 }
1662 } else {
ksrini0e817162008-08-26 10:21:20 -07001663 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001664 vmType = VM_KNOWN;
1665 }
1666 }
1667 }
1668
1669 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1670 if (vmType != VM_UNKNOWN) {
1671 knownVMs[cnt].name = JLI_StringDup(line);
1672 knownVMs[cnt].flag = vmType;
1673 switch (vmType) {
1674 default:
1675 break;
1676 case VM_ALIASED_TO:
1677 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1678 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1679 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1680 break;
1681 case VM_IF_SERVER_CLASS:
1682 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1683 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1684 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1685 break;
1686 }
1687 cnt++;
1688 }
1689 }
1690 fclose(jvmCfg);
1691 knownVMsCount = cnt;
1692
1693 if (JLI_IsTraceLauncher()) {
1694 end = CounterGet();
1695 printf("%ld micro seconds to parse jvm.cfg\n",
1696 (long)(jint)Counter2Micros(end-start));
1697 }
1698
1699 return cnt;
1700}
1701
1702
1703static void
1704GrowKnownVMs(int minimum)
1705{
1706 struct vmdesc* newKnownVMs;
1707 int newMax;
1708
1709 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1710 if (newMax <= minimum) {
1711 newMax = minimum;
1712 }
1713 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1714 if (knownVMs != NULL) {
1715 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1716 }
1717 JLI_MemFree(knownVMs);
1718 knownVMs = newKnownVMs;
1719 knownVMsLimit = newMax;
1720}
1721
1722
1723/* Returns index of VM or -1 if not found */
1724static int
1725KnownVMIndex(const char* name)
1726{
1727 int i;
1728 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1729 for (i = 0; i < knownVMsCount; i++) {
1730 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1731 return i;
1732 }
1733 }
1734 return -1;
1735}
1736
1737static void
1738FreeKnownVMs()
1739{
1740 int i;
1741 for (i = 0; i < knownVMsCount; i++) {
1742 JLI_MemFree(knownVMs[i].name);
1743 knownVMs[i].name = NULL;
1744 }
1745 JLI_MemFree(knownVMs);
1746}
1747
1748
1749/*
1750 * Displays the splash screen according to the jar file name
1751 * and image file names stored in environment variables
1752 */
michaelm5ac8c152012-03-06 20:34:38 +00001753void
duke6e45e102007-12-01 00:00:00 +00001754ShowSplashScreen()
1755{
1756 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1757 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1758 int data_size;
1759 void *image_data;
1760 if (jar_name) {
1761 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1762 if (image_data) {
1763 DoSplashInit();
1764 DoSplashLoadMemory(image_data, data_size);
1765 JLI_MemFree(image_data);
1766 }
1767 } else if (file_name) {
1768 DoSplashInit();
1769 DoSplashLoadFile(file_name);
1770 } else {
1771 return;
1772 }
1773 DoSplashSetFileJarName(file_name, jar_name);
1774
1775 /*
1776 * Done with all command line processing and potential re-execs so
1777 * clean up the environment.
1778 */
1779 (void)UnsetEnv(ENV_ENTRY);
1780 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1781 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1782
1783 JLI_MemFree(splash_jar_entry);
1784 JLI_MemFree(splash_file_entry);
1785
1786}
1787
1788const char*
1789GetDotVersion()
1790{
1791 return _dVersion;
1792}
1793
1794const char*
1795GetFullVersion()
1796{
1797 return _fVersion;
1798}
1799
1800const char*
1801GetProgramName()
1802{
1803 return _program_name;
1804}
1805
1806const char*
1807GetLauncherName()
1808{
1809 return _launcher_name;
1810}
1811
1812jint
1813GetErgoPolicy()
1814{
1815 return _ergo_policy;
1816}
1817
1818jboolean
1819IsJavaArgs()
1820{
1821 return _is_java_args;
1822}
1823
1824static jboolean
1825IsWildCardEnabled()
1826{
1827 return _wc_enabled;
1828}
1829
michaelm5ac8c152012-03-06 20:34:38 +00001830int
1831ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
1832 int argc, char **argv,
mchungea290e22011-01-21 09:43:57 -08001833 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001834{
1835
1836 /*
1837 * If user doesn't specify stack size, check if VM has a preference.
1838 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1839 * return its default stack size through the init args structure.
1840 */
1841 if (threadStackSize == 0) {
1842 struct JDK1_1InitArgs args1_1;
1843 memset((void*)&args1_1, 0, sizeof(args1_1));
1844 args1_1.version = JNI_VERSION_1_1;
1845 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1846 if (args1_1.javaStackSize > 0) {
1847 threadStackSize = args1_1.javaStackSize;
1848 }
1849 }
1850
1851 { /* Create a new thread to create JVM and invoke main method */
1852 JavaMainArgs args;
1853 int rslt;
1854
1855 args.argc = argc;
1856 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001857 args.mode = mode;
1858 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001859 args.ifn = *ifn;
1860
1861 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1862 /* If the caller has deemed there is an error we
1863 * simply return that, otherwise we return the value of
1864 * the callee
1865 */
1866 return (ret != 0) ? ret : rslt;
1867 }
1868}
1869
1870static void
1871DumpState()
1872{
1873 if (!JLI_IsTraceLauncher()) return ;
1874 printf("Launcher state:\n");
1875 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1876 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1877 printf("\tprogram name:%s\n", GetProgramName());
1878 printf("\tlauncher name:%s\n", GetLauncherName());
1879 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1880 printf("\tfullversion:%s\n", GetFullVersion());
1881 printf("\tdotversion:%s\n", GetDotVersion());
1882 printf("\tergo_policy:");
1883 switch(GetErgoPolicy()) {
1884 case NEVER_SERVER_CLASS:
1885 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1886 break;
1887 case ALWAYS_SERVER_CLASS:
1888 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1889 break;
1890 default:
1891 printf("DEFAULT_ERGONOMICS_POLICY\n");
1892 }
1893}
1894
1895/*
1896 * Return JNI_TRUE for an option string that has no effect but should
1897 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1898 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001899 * -d32 or -d64 is passed to a binary with an unmatched data model
1900 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1901 * exec to the proper binary). In the case of when the data model and the
1902 * requested version is matched, an exec would not occur, and these options
1903 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001904 */
1905jboolean
1906RemovableOption(char * option)
1907{
1908 /*
1909 * Unconditionally remove both -d32 and -d64 options since only
1910 * the last such options has an effect; e.g.
1911 * java -d32 -d64 -d32 -version
1912 * is equivalent to
1913 * java -d32 -version
1914 */
1915
1916 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1917 (JLI_StrCCmp(option, "-d64") == 0 ) )
1918 return JNI_TRUE;
1919 else
1920 return JNI_FALSE;
1921}
1922
1923/*
1924 * A utility procedure to always print to stderr
1925 */
1926void
ksrini0e817162008-08-26 10:21:20 -07001927JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001928{
1929 va_list vl;
1930 va_start(vl, fmt);
1931 vfprintf(stderr, fmt, vl);
1932 fprintf(stderr, "\n");
1933 va_end(vl);
1934}