blob: 313325817e0a9f4f5c4bd7fa1d9e309856ca306e [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";
duke6e45e102007-12-01 00:00:00 +0000698 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700699 def = JLI_MemAlloc(sizeof(format)
700 - 2 /* strlen("%s") */
701 + JLI_StrLen(s));
702 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000703 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700704 if (s != orig)
705 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000706}
707
708/*
709 * The SelectVersion() routine ensures that an appropriate version of
710 * the JRE is running. The specification for the appropriate version
711 * is obtained from either the manifest of a jar file (preferred) or
712 * from command line options.
713 * The routine also parses splash screen command line options and
714 * passes on their values in private environment variables.
715 */
716static void
717SelectVersion(int argc, char **argv, char **main_class)
718{
719 char *arg;
720 char **new_argv;
721 char **new_argp;
722 char *operand;
723 char *version = NULL;
724 char *jre = NULL;
725 int jarflag = 0;
726 int headlessflag = 0;
727 int restrict_search = -1; /* -1 implies not known */
728 manifest_info info;
729 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
730 char *splash_file_name = NULL;
731 char *splash_jar_name = NULL;
732 char *env_in;
733 int res;
734
735 /*
736 * If the version has already been selected, set *main_class
737 * with the value passed through the environment (if any) and
738 * simply return.
739 */
740 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
741 if (*env_in != '\0')
742 *main_class = JLI_StringDup(env_in);
743 return;
744 }
745
746 /*
747 * Scan through the arguments for options relevant to multiple JRE
748 * support. For reference, the command line syntax is defined as:
749 *
750 * SYNOPSIS
751 * java [options] class [argument...]
752 *
753 * java [options] -jar file.jar [argument...]
754 *
755 * As the scan is performed, make a copy of the argument list with
756 * the version specification options (new to 1.5) removed, so that
757 * a version less than 1.5 can be exec'd.
758 *
759 * Note that due to the syntax of the native Windows interface
760 * CreateProcess(), processing similar to the following exists in
761 * the Windows platform specific routine ExecJRE (in java_md.c).
762 * Changes here should be reproduced there.
763 */
764 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
765 new_argv[0] = argv[0];
766 new_argp = &new_argv[1];
767 argc--;
768 argv++;
769 while ((arg = *argv) != 0 && *arg == '-') {
770 if (JLI_StrCCmp(arg, "-version:") == 0) {
771 version = arg + 9;
772 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
773 restrict_search = 1;
774 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
775 restrict_search = 0;
776 } else {
777 if (JLI_StrCmp(arg, "-jar") == 0)
778 jarflag = 1;
779 /* deal with "unfortunate" classpath syntax */
780 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
781 (argc >= 2)) {
782 *new_argp++ = arg;
783 argc--;
784 argv++;
785 arg = *argv;
786 }
787
788 /*
789 * Checking for headless toolkit option in the some way as AWT does:
790 * "true" means true and any other value means false
791 */
792 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
793 headlessflag = 1;
794 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
795 headlessflag = 0;
796 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
797 splash_file_name = arg+8;
798 }
799 *new_argp++ = arg;
800 }
801 argc--;
802 argv++;
803 }
804 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
805 operand = NULL;
806 } else {
807 argc--;
808 *new_argp++ = operand = *argv++;
809 }
810 while (argc-- > 0) /* Copy over [argument...] */
811 *new_argp++ = *argv++;
812 *new_argp = NULL;
813
814 /*
815 * If there is a jar file, read the manifest. If the jarfile can't be
816 * read, the manifest can't be read from the jar file, or the manifest
817 * is corrupt, issue the appropriate error messages and exit.
818 *
819 * Even if there isn't a jar file, construct a manifest_info structure
820 * containing the command line information. It's a convenient way to carry
821 * this data around.
822 */
823 if (jarflag && operand) {
824 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
825 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700826 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000827 else
ksrini0e817162008-08-26 10:21:20 -0700828 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000829 exit(1);
830 }
831
832 /*
833 * Command line splash screen option should have precedence
834 * over the manifest, so the manifest data is used only if
835 * splash_file_name has not been initialized above during command
836 * line parsing
837 */
838 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
839 splash_file_name = info.splashscreen_image_file_name;
840 splash_jar_name = operand;
841 }
842 } else {
843 info.manifest_version = NULL;
844 info.main_class = NULL;
845 info.jre_version = NULL;
846 info.jre_restrict_search = 0;
847 }
848
849 /*
850 * Passing on splash screen info in environment variables
851 */
852 if (splash_file_name && !headlessflag) {
853 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
854 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
855 JLI_StrCat(splash_file_entry, splash_file_name);
856 putenv(splash_file_entry);
857 }
858 if (splash_jar_name && !headlessflag) {
859 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
860 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
861 JLI_StrCat(splash_jar_entry, splash_jar_name);
862 putenv(splash_jar_entry);
863 }
864
865 /*
866 * The JRE-Version and JRE-Restrict-Search values (if any) from the
867 * manifest are overwritten by any specified on the command line.
868 */
869 if (version != NULL)
870 info.jre_version = version;
871 if (restrict_search != -1)
872 info.jre_restrict_search = restrict_search;
873
874 /*
875 * "Valid" returns (other than unrecoverable errors) follow. Set
876 * main_class as a side-effect of this routine.
877 */
878 if (info.main_class != NULL)
879 *main_class = JLI_StringDup(info.main_class);
880
881 /*
882 * If no version selection information is found either on the command
883 * line or in the manifest, simply return.
884 */
885 if (info.jre_version == NULL) {
886 JLI_FreeManifest();
887 JLI_MemFree(new_argv);
888 return;
889 }
890
891 /*
892 * Check for correct syntax of the version specification (JSR 56).
893 */
894 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700895 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000896 exit(1);
897 }
898
899 /*
900 * Find the appropriate JVM on the system. Just to be as forgiving as
901 * possible, if the standard algorithms don't locate an appropriate
902 * jre, check to see if the one running will satisfy the requirements.
903 * This can happen on systems which haven't been set-up for multiple
904 * JRE support.
905 */
906 jre = LocateJRE(&info);
907 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
908 (info.jre_version?info.jre_version:"null"),
909 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
910
911 if (jre == NULL) {
912 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
913 JLI_FreeManifest();
914 JLI_MemFree(new_argv);
915 return;
916 } else {
ksrini0e817162008-08-26 10:21:20 -0700917 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000918 exit(1);
919 }
920 }
921
922 /*
923 * If I'm not the chosen one, exec the chosen one. Returning from
924 * ExecJRE indicates that I am indeed the chosen one.
925 *
926 * The private environment variable _JAVA_VERSION_SET is used to
927 * prevent the chosen one from re-reading the manifest file and
928 * using the values found within to override the (potential) command
929 * line flags stripped from argv (because the target may not
930 * understand them). Passing the MainClass value is an optimization
931 * to avoid locating, expanding and parsing the manifest extra
932 * times.
933 */
ksrini8760ca92008-09-04 09:43:32 -0700934 if (info.main_class != NULL) {
935 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
936 (void)JLI_StrCat(env_entry, info.main_class);
937 } else {
asaha80ccd422009-04-16 21:08:04 -0700938 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700939 exit(1);
940 }
941 }
duke6e45e102007-12-01 00:00:00 +0000942 (void)putenv(env_entry);
943 ExecJRE(jre, new_argv);
944 JLI_FreeManifest();
945 JLI_MemFree(new_argv);
946 return;
947}
948
949/*
950 * Parses command line arguments. Returns JNI_FALSE if launcher
951 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -0800952 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +0000953 * process return value) is set to 0 for a normal exit.
954 */
955static jboolean
mchungea290e22011-01-21 09:43:57 -0800956ParseArguments(int *pargc, char ***pargv,
957 int *pmode, char **pwhat,
958 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +0000959{
960 int argc = *pargc;
961 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -0800962 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +0000963 char *arg;
964
965 *pret = 0;
966
967 while ((arg = *argv) != 0 && *arg == '-') {
968 argv++; --argc;
969 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
970 ARG_CHECK (argc, ARG_ERROR1, arg);
971 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -0800972 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +0000973 argv++; --argc;
974 } else if (JLI_StrCmp(arg, "-jar") == 0) {
975 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -0800976 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +0000977 } else if (JLI_StrCmp(arg, "-help") == 0 ||
978 JLI_StrCmp(arg, "-h") == 0 ||
979 JLI_StrCmp(arg, "-?") == 0) {
980 printUsage = JNI_TRUE;
981 return JNI_TRUE;
982 } else if (JLI_StrCmp(arg, "-version") == 0) {
983 printVersion = JNI_TRUE;
984 return JNI_TRUE;
985 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
986 showVersion = JNI_TRUE;
987 } else if (JLI_StrCmp(arg, "-X") == 0) {
988 printXUsage = JNI_TRUE;
989 return JNI_TRUE;
990/*
ksrini8e20e1c2010-11-23 16:52:39 -0800991 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
992 * In the latter case, any SUBOPT value not recognized will default to "all"
993 */
994 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
995 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
996 showSettings = arg;
ksrini9636e2e2011-02-03 15:41:23 -0800997 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
998 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrini8e20e1c2010-11-23 16:52:39 -0800999/*
duke6e45e102007-12-01 00:00:00 +00001000 * The following case provide backward compatibility with old-style
1001 * command line options.
1002 */
1003 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001004 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001005 return JNI_FALSE;
1006 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1007 AddOption("-verbose:gc", NULL);
1008 } else if (JLI_StrCmp(arg, "-t") == 0) {
1009 AddOption("-Xt", NULL);
1010 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1011 AddOption("-Xtm", NULL);
1012 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1013 AddOption("-Xdebug", NULL);
1014 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1015 AddOption("-Xnoclassgc", NULL);
1016 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1017 AddOption("-Xverify:all", NULL);
1018 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1019 AddOption("-Xverify:all", NULL);
1020 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1021 AddOption("-Xverify:remote", NULL);
1022 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1023 AddOption("-Xverify:none", NULL);
1024 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1025 char *p = arg + 5;
1026 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1027 if (*p) {
1028 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1029 } else {
1030 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1031 }
1032 AddOption(tmp, NULL);
1033 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1034 JLI_StrCCmp(arg, "-oss") == 0 ||
1035 JLI_StrCCmp(arg, "-ms") == 0 ||
1036 JLI_StrCCmp(arg, "-mx") == 0) {
1037 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1038 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1039 AddOption(tmp, NULL);
1040 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1041 JLI_StrCmp(arg, "-cs") == 0 ||
1042 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1043 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001044 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001045 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1046 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1047 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1048 JLI_StrCCmp(arg, "-splash:") == 0) {
1049 ; /* Ignore machine independent options already handled */
michaelm5ac8c152012-03-06 20:34:38 +00001050 } else if (ProcessPlatformOption(arg)) {
1051 ; /* Processing of platform dependent options */
1052 } else if (RemovableOption(arg)) {
duke6e45e102007-12-01 00:00:00 +00001053 ; /* Do not pass option to vm. */
1054 } else {
1055 AddOption(arg, NULL);
1056 }
1057 }
1058
1059 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001060 *pwhat = *argv++;
1061 }
1062
1063 if (*pwhat == NULL) {
1064 *pret = 1;
1065 } else if (mode == LM_UNKNOWN) {
1066 /* default to LM_CLASS if -jar and -cp option are
1067 * not specified */
1068 mode = LM_CLASS;
1069 }
1070
1071 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001072 *pargc = argc;
1073 *pargv = argv;
1074 }
mchungea290e22011-01-21 09:43:57 -08001075
1076 *pmode = mode;
1077
duke6e45e102007-12-01 00:00:00 +00001078 return JNI_TRUE;
1079}
1080
1081/*
1082 * Initializes the Java Virtual Machine. Also frees options array when
1083 * finished.
1084 */
1085static jboolean
1086InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1087{
1088 JavaVMInitArgs args;
1089 jint r;
1090
1091 memset(&args, 0, sizeof(args));
1092 args.version = JNI_VERSION_1_2;
1093 args.nOptions = numOptions;
1094 args.options = options;
1095 args.ignoreUnrecognized = JNI_FALSE;
1096
1097 if (JLI_IsTraceLauncher()) {
1098 int i = 0;
1099 printf("JavaVM args:\n ");
1100 printf("version 0x%08lx, ", (long)args.version);
1101 printf("ignoreUnrecognized is %s, ",
1102 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1103 printf("nOptions is %ld\n", (long)args.nOptions);
1104 for (i = 0; i < numOptions; i++)
1105 printf(" option[%2d] = '%s'\n",
1106 i, args.options[i].optionString);
1107 }
1108
1109 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1110 JLI_MemFree(options);
1111 return r == JNI_OK;
1112}
1113
ksrini7d9872e2011-03-20 08:41:33 -07001114static jclass helperClass = NULL;
1115
1116static jclass
1117GetLauncherHelperClass(JNIEnv *env) {
1118 if (helperClass == NULL) {
1119 NULL_CHECK0(helperClass = FindBootStrapClass(env,
1120 "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001121 }
ksrini7d9872e2011-03-20 08:41:33 -07001122 return helperClass;
duke6e45e102007-12-01 00:00:00 +00001123}
1124
ksrini7d9872e2011-03-20 08:41:33 -07001125static jmethodID makePlatformStringMID = NULL;
duke6e45e102007-12-01 00:00:00 +00001126/*
1127 * Returns a new Java string object for the specified platform string.
1128 */
1129static jstring
1130NewPlatformString(JNIEnv *env, char *s)
1131{
1132 int len = (int)JLI_StrLen(s);
duke6e45e102007-12-01 00:00:00 +00001133 jbyteArray ary;
ksrini7d9872e2011-03-20 08:41:33 -07001134 jclass cls = GetLauncherHelperClass(env);
1135 NULL_CHECK0(cls);
duke6e45e102007-12-01 00:00:00 +00001136 if (s == NULL)
1137 return 0;
duke6e45e102007-12-01 00:00:00 +00001138
1139 ary = (*env)->NewByteArray(env, len);
1140 if (ary != 0) {
1141 jstring str = 0;
1142 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1143 if (!(*env)->ExceptionOccurred(env)) {
ksrini7d9872e2011-03-20 08:41:33 -07001144 if (makePlatformStringMID == NULL) {
1145 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1146 cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
duke6e45e102007-12-01 00:00:00 +00001147 }
ksrini7d9872e2011-03-20 08:41:33 -07001148 str = (*env)->CallStaticObjectMethod(env, cls,
1149 makePlatformStringMID, USE_STDERR, ary);
duke6e45e102007-12-01 00:00:00 +00001150 (*env)->DeleteLocalRef(env, ary);
1151 return str;
1152 }
1153 }
1154 return 0;
1155}
1156
1157/*
1158 * Returns a new array of Java string objects for the specified
1159 * array of platform strings.
1160 */
1161static jobjectArray
1162NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1163{
1164 jarray cls;
1165 jarray ary;
1166 int i;
1167
ksrini20a64b22008-09-24 15:07:41 -07001168 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001169 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1170 for (i = 0; i < strc; i++) {
1171 jstring str = NewPlatformString(env, *strv++);
1172 NULL_CHECK0(str);
1173 (*env)->SetObjectArrayElement(env, ary, i, str);
1174 (*env)->DeleteLocalRef(env, str);
1175 }
1176 return ary;
1177}
1178
1179/*
ksrini20a64b22008-09-24 15:07:41 -07001180 * Loads a class and verifies that the main class is present and it is ok to
1181 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001182 */
1183static jclass
mchungea290e22011-01-21 09:43:57 -08001184LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001185{
ksrini20a64b22008-09-24 15:07:41 -07001186 jmethodID mid;
1187 jstring str;
1188 jobject result;
duke6e45e102007-12-01 00:00:00 +00001189 jlong start, end;
ksrini7d9872e2011-03-20 08:41:33 -07001190 jclass cls = GetLauncherHelperClass(env);
1191 NULL_CHECK0(cls);
ksrini20a64b22008-09-24 15:07:41 -07001192 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001193 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001194 }
ksrini7d9872e2011-03-20 08:41:33 -07001195 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1196 "checkAndLoadMain",
1197 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1198
ksriniac5e02f2012-01-11 08:14:47 -08001199 str = NewPlatformString(env, name);
ksrini7d9872e2011-03-20 08:41:33 -07001200 result = (*env)->CallStaticObjectMethod(env, cls, mid, USE_STDERR, mode, str);
duke6e45e102007-12-01 00:00:00 +00001201
1202 if (JLI_IsTraceLauncher()) {
1203 end = CounterGet();
1204 printf("%ld micro seconds to load main class\n",
1205 (long)(jint)Counter2Micros(end-start));
1206 printf("----_JAVA_LAUNCHER_DEBUG----\n");
1207 }
1208
ksrini20a64b22008-09-24 15:07:41 -07001209 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001210}
1211
duke6e45e102007-12-01 00:00:00 +00001212/*
1213 * For tools, convert command line args thus:
1214 * javac -cp foo:foo/"*" -J-ms32m ...
1215 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1216 *
1217 * Takes 4 parameters, and returns the populated arguments
1218 */
1219static void
1220TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1221{
1222 int argc = *pargc;
1223 char **argv = *pargv;
1224 int nargc = argc + jargc;
1225 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1226 int i;
1227
1228 *pargc = nargc;
1229 *pargv = nargv;
1230
1231 /* Copy the VM arguments (i.e. prefixed with -J) */
1232 for (i = 0; i < jargc; i++) {
1233 const char *arg = jargv[i];
1234 if (arg[0] == '-' && arg[1] == 'J') {
1235 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1236 }
1237 }
1238
1239 for (i = 0; i < argc; i++) {
1240 char *arg = argv[i];
1241 if (arg[0] == '-' && arg[1] == 'J') {
1242 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001243 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001244 exit(1);
1245 }
1246 *nargv++ = arg + 2;
1247 }
1248 }
1249
1250 /* Copy the rest of the arguments */
1251 for (i = 0; i < jargc ; i++) {
1252 const char *arg = jargv[i];
1253 if (arg[0] != '-' || arg[1] != 'J') {
1254 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1255 }
1256 }
1257 for (i = 0; i < argc; i++) {
1258 char *arg = argv[i];
1259 if (arg[0] == '-') {
1260 if (arg[1] == 'J')
1261 continue;
1262 if (IsWildCardEnabled() && arg[1] == 'c'
1263 && (JLI_StrCmp(arg, "-cp") == 0 ||
1264 JLI_StrCmp(arg, "-classpath") == 0)
1265 && i < argc - 1) {
1266 *nargv++ = arg;
1267 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1268 i++;
1269 continue;
1270 }
1271 }
1272 *nargv++ = arg;
1273 }
1274 *nargv = 0;
1275}
1276
1277/*
1278 * For our tools, we try to add 3 VM options:
1279 * -Denv.class.path=<envcp>
1280 * -Dapplication.home=<apphome>
1281 * -Djava.class.path=<appcp>
1282 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1283 * tells javac where to find binary classes through this environment
1284 * variable. Notice that users will be able to compile against our
1285 * tools classes (sun.tools.javac.Main) only if they explicitly add
1286 * tools.jar to CLASSPATH.
1287 * <apphome> is the directory where the application is installed.
1288 * <appcp> is the classpath to where our apps' classfiles are.
1289 */
1290static jboolean
1291AddApplicationOptions(int cpathc, const char **cpathv)
1292{
1293 char *envcp, *appcp, *apphome;
1294 char home[MAXPATHLEN]; /* application home */
1295 char separator[] = { PATH_SEPARATOR, '\0' };
1296 int size, i;
1297
1298 {
1299 const char *s = getenv("CLASSPATH");
1300 if (s) {
1301 s = (char *) JLI_WildcardExpandClasspath(s);
1302 /* 40 for -Denv.class.path= */
1303 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1304 sprintf(envcp, "-Denv.class.path=%s", s);
1305 AddOption(envcp, NULL);
1306 }
1307 }
1308
1309 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001310 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001311 return JNI_FALSE;
1312 }
1313
1314 /* 40 for '-Dapplication.home=' */
1315 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1316 sprintf(apphome, "-Dapplication.home=%s", home);
1317 AddOption(apphome, NULL);
1318
1319 /* How big is the application's classpath? */
1320 size = 40; /* 40: "-Djava.class.path=" */
1321 for (i = 0; i < cpathc; i++) {
1322 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1323 }
1324 appcp = (char *)JLI_MemAlloc(size + 1);
1325 JLI_StrCpy(appcp, "-Djava.class.path=");
1326 for (i = 0; i < cpathc; i++) {
1327 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1328 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1329 JLI_StrCat(appcp, separator); /* ; */
1330 }
1331 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1332 AddOption(appcp, NULL);
1333 return JNI_TRUE;
1334}
1335
1336/*
1337 * inject the -Dsun.java.command pseudo property into the args structure
1338 * this pseudo property is used in the HotSpot VM to expose the
1339 * Java class name and arguments to the main method to the VM. The
1340 * HotSpot VM uses this pseudo property to store the Java class name
1341 * (or jar file name) and the arguments to the class's main method
1342 * to the instrumentation memory region. The sun.java.command pseudo
1343 * property is not exported by HotSpot to the Java layer.
1344 */
1345void
mchungea290e22011-01-21 09:43:57 -08001346SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001347{
1348
1349 int i = 0;
1350 size_t len = 0;
1351 char* javaCommand = NULL;
1352 char* dashDstr = "-Dsun.java.command=";
1353
mchungea290e22011-01-21 09:43:57 -08001354 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001355 /* unexpected, one of these should be set. just return without
1356 * setting the property
1357 */
1358 return;
1359 }
1360
duke6e45e102007-12-01 00:00:00 +00001361 /* determine the amount of memory to allocate assuming
1362 * the individual components will be space separated
1363 */
mchungea290e22011-01-21 09:43:57 -08001364 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001365 for (i = 0; i < argc; i++) {
1366 len += JLI_StrLen(argv[i]) + 1;
1367 }
1368
1369 /* allocate the memory */
1370 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1371
1372 /* build the -D string */
1373 *javaCommand = '\0';
1374 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001375 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001376
1377 for (i = 0; i < argc; i++) {
1378 /* the components of the string are space separated. In
1379 * the case of embedded white space, the relationship of
1380 * the white space separated components to their true
1381 * positional arguments will be ambiguous. This issue may
1382 * be addressed in a future release.
1383 */
1384 JLI_StrCat(javaCommand, " ");
1385 JLI_StrCat(javaCommand, argv[i]);
1386 }
1387
1388 AddOption(javaCommand, NULL);
1389}
1390
1391/*
1392 * JVM would like to know if it's created by a standard Sun launcher, or by
1393 * user native application, the following property indicates the former.
1394 */
mchungea290e22011-01-21 09:43:57 -08001395void
1396SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001397 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1398}
1399
1400/*
1401 * Prints the version information from the java.version and other properties.
1402 */
1403static void
1404PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1405{
1406 jclass ver;
1407 jmethodID print;
1408
ksrini20a64b22008-09-24 15:07:41 -07001409 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001410 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1411 ver,
1412 (extraLF == JNI_TRUE) ? "println" : "print",
1413 "()V"
1414 )
1415 );
1416
1417 (*env)->CallStaticVoidMethod(env, ver, print);
1418}
1419
1420/*
ksrini8e20e1c2010-11-23 16:52:39 -08001421 * Prints all the Java settings, see the java implementation for more details.
1422 */
1423static void
1424ShowSettings(JNIEnv *env, char *optString)
1425{
ksrini8e20e1c2010-11-23 16:52:39 -08001426 jmethodID showSettingsID;
1427 jstring joptString;
ksrini7d9872e2011-03-20 08:41:33 -07001428 jclass cls = GetLauncherHelperClass(env);
1429 NULL_CHECK(cls);
ksrini8e20e1c2010-11-23 16:52:39 -08001430 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001431 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001432 joptString = (*env)->NewStringUTF(env, optString);
1433 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
ksrini7d9872e2011-03-20 08:41:33 -07001434 USE_STDERR,
ksrini8e20e1c2010-11-23 16:52:39 -08001435 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001436 (jlong)initialHeapSize,
1437 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001438 (jlong)threadStackSize,
1439 ServerClassMachine());
1440}
1441
1442/*
ksrini20a64b22008-09-24 15:07:41 -07001443 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001444 */
1445static void
1446PrintUsage(JNIEnv* env, jboolean doXUsage)
1447{
duke6e45e102007-12-01 00:00:00 +00001448 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1449 jstring jprogname, vm1, vm2;
1450 int i;
ksrini7d9872e2011-03-20 08:41:33 -07001451 jclass cls = GetLauncherHelperClass(env);
1452 NULL_CHECK(cls);
duke6e45e102007-12-01 00:00:00 +00001453 if (doXUsage) {
1454 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1455 "printXUsageMessage", "(Z)V"));
ksrini7d9872e2011-03-20 08:41:33 -07001456 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001457 } else {
1458 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1459 "initHelpMessage", "(Ljava/lang/String;)V"));
1460
1461 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1462 "(Ljava/lang/String;Ljava/lang/String;)V"));
1463
1464 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1465 "appendVmSynonymMessage",
1466 "(Ljava/lang/String;Ljava/lang/String;)V"));
1467 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1468 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1469
1470 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1471 "printHelpMessage", "(Z)V"));
1472
1473 jprogname = (*env)->NewStringUTF(env, _program_name);
1474
1475 /* Initialize the usage message with the usual preamble */
1476 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1477
1478
1479 /* Assemble the other variant part of the usage */
1480 if ((knownVMs[0].flag == VM_KNOWN) ||
1481 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1482 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1483 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1484 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1485 }
1486 for (i=1; i<knownVMsCount; i++) {
1487 if (knownVMs[i].flag == VM_KNOWN) {
1488 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1489 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1490 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1491 }
1492 }
1493 for (i=1; i<knownVMsCount; i++) {
1494 if (knownVMs[i].flag == VM_ALIASED_TO) {
1495 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1496 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1497 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1498 }
1499 }
1500
1501 /* The first known VM is the default */
1502 {
1503 jboolean isServerClassMachine = ServerClassMachine();
1504
1505 const char* defaultVM = knownVMs[0].name+1;
1506 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1507 defaultVM = knownVMs[0].server_class+1;
1508 }
1509
1510 vm1 = (*env)->NewStringUTF(env, defaultVM);
1511 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1512 }
1513
1514 /* Complete the usage message and print to stderr*/
ksrini7d9872e2011-03-20 08:41:33 -07001515 (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001516 }
1517 return;
1518}
1519
1520/*
1521 * Read the jvm.cfg file and fill the knownJVMs[] array.
1522 *
1523 * The functionality of the jvm.cfg file is subject to change without
1524 * notice and the mechanism will be removed in the future.
1525 *
1526 * The lexical structure of the jvm.cfg file is as follows:
1527 *
1528 * jvmcfg := { vmLine }
1529 * vmLine := knownLine
1530 * | aliasLine
1531 * | warnLine
1532 * | ignoreLine
1533 * | errorLine
1534 * | predicateLine
1535 * | commentLine
1536 * knownLine := flag "KNOWN" EOL
1537 * warnLine := flag "WARN" EOL
1538 * ignoreLine := flag "IGNORE" EOL
1539 * errorLine := flag "ERROR" EOL
1540 * aliasLine := flag "ALIASED_TO" flag EOL
1541 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1542 * commentLine := "#" text EOL
1543 * flag := "-" identifier
1544 *
1545 * The semantics are that when someone specifies a flag on the command line:
1546 * - if the flag appears on a knownLine, then the identifier is used as
1547 * the name of the directory holding the JVM library (the name of the JVM).
1548 * - if the flag appears as the first flag on an aliasLine, the identifier
1549 * of the second flag is used as the name of the JVM.
1550 * - if the flag appears on a warnLine, the identifier is used as the
1551 * name of the JVM, but a warning is generated.
1552 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1553 * name of a JVM, but the identifier is ignored and the default vm used
1554 * - if the flag appears on an errorLine, an error is generated.
1555 * - if the flag appears as the first flag on a predicateLine, and
1556 * the machine on which you are running passes the predicate indicated,
1557 * then the identifier of the second flag is used as the name of the JVM,
1558 * otherwise the identifier of the first flag is used as the name of the JVM.
1559 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1560 * file determines the name of the JVM.
1561 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1562 * since they only make sense if someone hasn't specified the name of the
1563 * JVM on the command line.
1564 *
1565 * The intent of the jvm.cfg file is to allow several JVM libraries to
1566 * be installed in different subdirectories of a single JRE installation,
1567 * for space-savings and convenience in testing.
1568 * The intent is explicitly not to provide a full aliasing or predicate
1569 * mechanism.
1570 */
1571jint
michaelm5ac8c152012-03-06 20:34:38 +00001572ReadKnownVMs(const char *jvmCfgName, jboolean speculative)
duke6e45e102007-12-01 00:00:00 +00001573{
1574 FILE *jvmCfg;
duke6e45e102007-12-01 00:00:00 +00001575 char line[MAXPATHLEN+20];
1576 int cnt = 0;
1577 int lineno = 0;
1578 jlong start, end;
1579 int vmType;
1580 char *tmpPtr;
1581 char *altVMName = NULL;
1582 char *serverClassVMName = NULL;
1583 static char *whiteSpace = " \t";
1584 if (JLI_IsTraceLauncher()) {
1585 start = CounterGet();
1586 }
duke6e45e102007-12-01 00:00:00 +00001587
1588 jvmCfg = fopen(jvmCfgName, "r");
1589 if (jvmCfg == NULL) {
1590 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001591 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001592 exit(1);
1593 } else {
1594 return -1;
1595 }
1596 }
1597 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1598 vmType = VM_UNKNOWN;
1599 lineno++;
1600 if (line[0] == '#')
1601 continue;
1602 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001603 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001604 }
1605 if (cnt >= knownVMsLimit) {
1606 GrowKnownVMs(cnt);
1607 }
1608 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1609 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1610 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001611 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001612 } else {
1613 /* Null-terminate this string for JLI_StringDup below */
1614 *tmpPtr++ = 0;
1615 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1616 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001617 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001618 } else {
1619 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1620 vmType = VM_KNOWN;
1621 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1622 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1623 if (*tmpPtr != 0) {
1624 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1625 }
1626 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001627 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001628 } else {
1629 /* Null terminate altVMName */
1630 altVMName = tmpPtr;
1631 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1632 *tmpPtr = 0;
1633 vmType = VM_ALIASED_TO;
1634 }
1635 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1636 vmType = VM_WARN;
1637 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1638 vmType = VM_IGNORE;
1639 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1640 vmType = VM_ERROR;
1641 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1642 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1643 if (*tmpPtr != 0) {
1644 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1645 }
1646 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001647 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001648 } else {
1649 /* Null terminate server class VM name */
1650 serverClassVMName = tmpPtr;
1651 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1652 *tmpPtr = 0;
1653 vmType = VM_IF_SERVER_CLASS;
1654 }
1655 } else {
ksrini0e817162008-08-26 10:21:20 -07001656 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001657 vmType = VM_KNOWN;
1658 }
1659 }
1660 }
1661
1662 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1663 if (vmType != VM_UNKNOWN) {
1664 knownVMs[cnt].name = JLI_StringDup(line);
1665 knownVMs[cnt].flag = vmType;
1666 switch (vmType) {
1667 default:
1668 break;
1669 case VM_ALIASED_TO:
1670 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1671 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1672 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1673 break;
1674 case VM_IF_SERVER_CLASS:
1675 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1676 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1677 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1678 break;
1679 }
1680 cnt++;
1681 }
1682 }
1683 fclose(jvmCfg);
1684 knownVMsCount = cnt;
1685
1686 if (JLI_IsTraceLauncher()) {
1687 end = CounterGet();
1688 printf("%ld micro seconds to parse jvm.cfg\n",
1689 (long)(jint)Counter2Micros(end-start));
1690 }
1691
1692 return cnt;
1693}
1694
1695
1696static void
1697GrowKnownVMs(int minimum)
1698{
1699 struct vmdesc* newKnownVMs;
1700 int newMax;
1701
1702 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1703 if (newMax <= minimum) {
1704 newMax = minimum;
1705 }
1706 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1707 if (knownVMs != NULL) {
1708 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1709 }
1710 JLI_MemFree(knownVMs);
1711 knownVMs = newKnownVMs;
1712 knownVMsLimit = newMax;
1713}
1714
1715
1716/* Returns index of VM or -1 if not found */
1717static int
1718KnownVMIndex(const char* name)
1719{
1720 int i;
1721 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1722 for (i = 0; i < knownVMsCount; i++) {
1723 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1724 return i;
1725 }
1726 }
1727 return -1;
1728}
1729
1730static void
1731FreeKnownVMs()
1732{
1733 int i;
1734 for (i = 0; i < knownVMsCount; i++) {
1735 JLI_MemFree(knownVMs[i].name);
1736 knownVMs[i].name = NULL;
1737 }
1738 JLI_MemFree(knownVMs);
1739}
1740
1741
1742/*
1743 * Displays the splash screen according to the jar file name
1744 * and image file names stored in environment variables
1745 */
michaelm5ac8c152012-03-06 20:34:38 +00001746void
duke6e45e102007-12-01 00:00:00 +00001747ShowSplashScreen()
1748{
1749 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1750 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1751 int data_size;
1752 void *image_data;
1753 if (jar_name) {
1754 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1755 if (image_data) {
1756 DoSplashInit();
1757 DoSplashLoadMemory(image_data, data_size);
1758 JLI_MemFree(image_data);
1759 }
1760 } else if (file_name) {
1761 DoSplashInit();
1762 DoSplashLoadFile(file_name);
1763 } else {
1764 return;
1765 }
1766 DoSplashSetFileJarName(file_name, jar_name);
1767
1768 /*
1769 * Done with all command line processing and potential re-execs so
1770 * clean up the environment.
1771 */
1772 (void)UnsetEnv(ENV_ENTRY);
1773 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1774 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1775
1776 JLI_MemFree(splash_jar_entry);
1777 JLI_MemFree(splash_file_entry);
1778
1779}
1780
1781const char*
1782GetDotVersion()
1783{
1784 return _dVersion;
1785}
1786
1787const char*
1788GetFullVersion()
1789{
1790 return _fVersion;
1791}
1792
1793const char*
1794GetProgramName()
1795{
1796 return _program_name;
1797}
1798
1799const char*
1800GetLauncherName()
1801{
1802 return _launcher_name;
1803}
1804
1805jint
1806GetErgoPolicy()
1807{
1808 return _ergo_policy;
1809}
1810
1811jboolean
1812IsJavaArgs()
1813{
1814 return _is_java_args;
1815}
1816
1817static jboolean
1818IsWildCardEnabled()
1819{
1820 return _wc_enabled;
1821}
1822
michaelm5ac8c152012-03-06 20:34:38 +00001823int
1824ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
1825 int argc, char **argv,
mchungea290e22011-01-21 09:43:57 -08001826 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001827{
1828
1829 /*
1830 * If user doesn't specify stack size, check if VM has a preference.
1831 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1832 * return its default stack size through the init args structure.
1833 */
1834 if (threadStackSize == 0) {
1835 struct JDK1_1InitArgs args1_1;
1836 memset((void*)&args1_1, 0, sizeof(args1_1));
1837 args1_1.version = JNI_VERSION_1_1;
1838 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1839 if (args1_1.javaStackSize > 0) {
1840 threadStackSize = args1_1.javaStackSize;
1841 }
1842 }
1843
1844 { /* Create a new thread to create JVM and invoke main method */
1845 JavaMainArgs args;
1846 int rslt;
1847
1848 args.argc = argc;
1849 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001850 args.mode = mode;
1851 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001852 args.ifn = *ifn;
1853
1854 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1855 /* If the caller has deemed there is an error we
1856 * simply return that, otherwise we return the value of
1857 * the callee
1858 */
1859 return (ret != 0) ? ret : rslt;
1860 }
1861}
1862
1863static void
1864DumpState()
1865{
1866 if (!JLI_IsTraceLauncher()) return ;
1867 printf("Launcher state:\n");
1868 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1869 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1870 printf("\tprogram name:%s\n", GetProgramName());
1871 printf("\tlauncher name:%s\n", GetLauncherName());
1872 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1873 printf("\tfullversion:%s\n", GetFullVersion());
1874 printf("\tdotversion:%s\n", GetDotVersion());
1875 printf("\tergo_policy:");
1876 switch(GetErgoPolicy()) {
1877 case NEVER_SERVER_CLASS:
1878 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1879 break;
1880 case ALWAYS_SERVER_CLASS:
1881 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1882 break;
1883 default:
1884 printf("DEFAULT_ERGONOMICS_POLICY\n");
1885 }
1886}
1887
1888/*
1889 * Return JNI_TRUE for an option string that has no effect but should
1890 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1891 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001892 * -d32 or -d64 is passed to a binary with an unmatched data model
1893 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1894 * exec to the proper binary). In the case of when the data model and the
1895 * requested version is matched, an exec would not occur, and these options
1896 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001897 */
1898jboolean
1899RemovableOption(char * option)
1900{
1901 /*
1902 * Unconditionally remove both -d32 and -d64 options since only
1903 * the last such options has an effect; e.g.
1904 * java -d32 -d64 -d32 -version
1905 * is equivalent to
1906 * java -d32 -version
1907 */
1908
1909 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1910 (JLI_StrCCmp(option, "-d64") == 0 ) )
1911 return JNI_TRUE;
1912 else
1913 return JNI_FALSE;
1914}
1915
1916/*
1917 * A utility procedure to always print to stderr
1918 */
1919void
ksrini0e817162008-08-26 10:21:20 -07001920JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001921{
1922 va_list vl;
1923 va_start(vl, fmt);
1924 vfprintf(stderr, fmt, vl);
1925 fprintf(stderr, "\n");
1926 va_end(vl);
1927}