blob: 73c532676b3c6bc9485c15a6185348c813ee8881 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ksrini07e328d2013-05-07 13:15:28 -07002 * Copyright (c) 1995, 2013, 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);
mchungea290e22011-01-21 09:43:57 -0800107static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
ksrini6b41ee82012-11-19 19:49:38 -0800108static jclass GetApplicationClass(JNIEnv *env);
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
ksrini07e328d2013-05-07 13:15:28 -0700152#define ARG_CHECK(AC_arg_count, AC_failure_message, AC_questionable_arg) \
153 do { \
154 if (AC_arg_count < 1) { \
155 JLI_ReportErrorMessage(AC_failure_message, AC_questionable_arg); \
156 printUsage = JNI_TRUE; \
157 *pret = 1; \
158 return JNI_TRUE; \
159 } \
160 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000161
162/*
163 * Running Java code in primordial thread caused many problems. We will
164 * create a new thread to invoke JVM. See 6316197 for more information.
165 */
ksrinie97d4002012-07-31 06:14:28 -0700166static jlong threadStackSize = 0; /* stack size of the new thread */
ksrini6d575f82010-12-23 13:51:30 -0800167static jlong maxHeapSize = 0; /* max heap size */
168static jlong initialHeapSize = 0; /* inital heap size */
duke6e45e102007-12-01 00:00:00 +0000169
duke6e45e102007-12-01 00:00:00 +0000170/*
171 * Entry point.
172 */
173int
174JLI_Launch(int argc, char ** argv, /* main argc, argc */
175 int jargc, const char** jargv, /* java args */
176 int appclassc, const char** appclassv, /* app classpath */
177 const char* fullversion, /* full version defined */
178 const char* dotversion, /* dot version defined */
179 const char* pname, /* program name */
180 const char* lname, /* launcher name */
181 jboolean javaargs, /* JAVA_ARGS */
182 jboolean cpwildcard, /* classpath wildcard*/
183 jboolean javaw, /* windows-only javaw */
184 jint ergo /* ergonomics class policy */
185)
186{
mchungea290e22011-01-21 09:43:57 -0800187 int mode = LM_UNKNOWN;
188 char *what = NULL;
duke6e45e102007-12-01 00:00:00 +0000189 char *cpath = 0;
190 char *main_class = NULL;
191 int ret;
192 InvocationFunctions ifn;
193 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700194 char jvmpath[MAXPATHLEN];
195 char jrepath[MAXPATHLEN];
michaelm5ac8c152012-03-06 20:34:38 +0000196 char jvmcfg[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000197
198 _fVersion = fullversion;
199 _dVersion = dotversion;
200 _launcher_name = lname;
201 _program_name = pname;
202 _is_java_args = javaargs;
203 _wc_enabled = cpwildcard;
204 _ergo_policy = ergo;
205
ksrini52cded22008-03-06 07:51:28 -0800206 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000207 DumpState();
ksrinie97d4002012-07-31 06:14:28 -0700208 if (JLI_IsTraceLauncher()) {
209 int i;
210 printf("Command line args:\n");
211 for (i = 0; i < argc ; i++) {
212 printf("argv[%d] = %s\n", i, argv[i]);
213 }
214 AddOption("-Dsun.java.launcher.diag=true", NULL);
215 }
duke6e45e102007-12-01 00:00:00 +0000216
217 /*
218 * Make sure the specified version of the JRE is running.
219 *
220 * There are three things to note about the SelectVersion() routine:
221 * 1) If the version running isn't correct, this routine doesn't
222 * return (either the correct version has been exec'd or an error
223 * was issued).
224 * 2) Argc and Argv in this scope are *not* altered by this routine.
225 * It is the responsibility of subsequent code to ignore the
226 * arguments handled by this routine.
227 * 3) As a side-effect, the variable "main_class" is guaranteed to
228 * be set (if it should ever be set). This isn't exactly the
229 * poster child for structured programming, but it is a small
230 * price to pay for not processing a jar file operand twice.
231 * (Note: This side effect has been disabled. See comment on
232 * bugid 5030265 below.)
233 */
234 SelectVersion(argc, argv, &main_class);
235
duke6e45e102007-12-01 00:00:00 +0000236 CreateExecutionEnvironment(&argc, &argv,
237 jrepath, sizeof(jrepath),
michaelm5ac8c152012-03-06 20:34:38 +0000238 jvmpath, sizeof(jvmpath),
239 jvmcfg, sizeof(jvmcfg));
duke6e45e102007-12-01 00:00:00 +0000240
241 ifn.CreateJavaVM = 0;
242 ifn.GetDefaultJavaVMInitArgs = 0;
243
244 if (JLI_IsTraceLauncher()) {
245 start = CounterGet();
246 }
247
248 if (!LoadJavaVM(jvmpath, &ifn)) {
249 return(6);
250 }
251
252 if (JLI_IsTraceLauncher()) {
253 end = CounterGet();
254 }
255
256 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
257 (long)(jint)Counter2Micros(end-start));
258
259 ++argv;
260 --argc;
261
262 if (IsJavaArgs()) {
263 /* Preprocess wrapper arguments */
264 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
265 if (!AddApplicationOptions(appclassc, appclassv)) {
266 return(1);
267 }
268 } else {
269 /* Set default CLASSPATH */
270 cpath = getenv("CLASSPATH");
271 if (cpath == NULL) {
272 cpath = ".";
273 }
274 SetClassPath(cpath);
275 }
276
mchungea290e22011-01-21 09:43:57 -0800277 /* Parse command line options; if the return value of
278 * ParseArguments is false, the program should exit.
duke6e45e102007-12-01 00:00:00 +0000279 */
mchungea290e22011-01-21 09:43:57 -0800280 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
281 {
duke6e45e102007-12-01 00:00:00 +0000282 return(ret);
283 }
284
285 /* Override class path if -jar flag was specified */
mchungea290e22011-01-21 09:43:57 -0800286 if (mode == LM_JAR) {
287 SetClassPath(what); /* Override class path */
duke6e45e102007-12-01 00:00:00 +0000288 }
289
290 /* set the -Dsun.java.command pseudo property */
mchungea290e22011-01-21 09:43:57 -0800291 SetJavaCommandLineProp(what, argc, argv);
duke6e45e102007-12-01 00:00:00 +0000292
293 /* Set the -Dsun.java.launcher pseudo property */
294 SetJavaLauncherProp();
295
296 /* set the -Dsun.java.launcher.* platform properties */
297 SetJavaLauncherPlatformProps();
298
michaelm5ac8c152012-03-06 20:34:38 +0000299 return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
duke6e45e102007-12-01 00:00:00 +0000300}
ksrinie3ec45d2010-07-09 11:04:34 -0700301/*
302 * Always detach the main thread so that it appears to have ended when
303 * the application's main method exits. This will invoke the
304 * uncaught exception handler machinery if main threw an
305 * exception. An uncaught exception handler cannot change the
306 * launcher's return code except by calling System.exit.
307 *
308 * Wait for all non-daemon threads to end, then destroy the VM.
309 * This will actually create a trivial new Java waiter thread
310 * named "DestroyJavaVM", but this will be seen as a different
311 * thread from the one that executed main, even though they are
312 * the same C thread. This allows mainThread.join() and
313 * mainThread.isAlive() to work as expected.
314 */
315#define LEAVE() \
ksrini07e328d2013-05-07 13:15:28 -0700316 do { \
317 if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \
318 JLI_ReportErrorMessage(JVM_ERROR2); \
319 ret = 1; \
320 } \
321 if (JNI_TRUE) { \
322 (*vm)->DestroyJavaVM(vm); \
323 return ret; \
324 } \
325 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000326
ksrini07e328d2013-05-07 13:15:28 -0700327#define CHECK_EXCEPTION_NULL_LEAVE(CENL_exception) \
328 do { \
329 if ((*env)->ExceptionOccurred(env)) { \
330 JLI_ReportExceptionDescription(env); \
331 LEAVE(); \
332 } \
333 if ((CENL_exception) == NULL) { \
334 JLI_ReportErrorMessage(JNI_ERROR); \
335 LEAVE(); \
336 } \
337 } while (JNI_FALSE)
ksrini20a64b22008-09-24 15:07:41 -0700338
ksrini07e328d2013-05-07 13:15:28 -0700339#define CHECK_EXCEPTION_LEAVE(CEL_return_value) \
340 do { \
341 if ((*env)->ExceptionOccurred(env)) { \
342 JLI_ReportExceptionDescription(env); \
343 ret = (CEL_return_value); \
344 LEAVE(); \
345 } \
346 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000347
348int JNICALL
349JavaMain(void * _args)
350{
351 JavaMainArgs *args = (JavaMainArgs *)_args;
352 int argc = args->argc;
353 char **argv = args->argv;
mchungea290e22011-01-21 09:43:57 -0800354 int mode = args->mode;
355 char *what = args->what;
duke6e45e102007-12-01 00:00:00 +0000356 InvocationFunctions ifn = args->ifn;
357
358 JavaVM *vm = 0;
359 JNIEnv *env = 0;
mchungea290e22011-01-21 09:43:57 -0800360 jclass mainClass = NULL;
ksrini6b41ee82012-11-19 19:49:38 -0800361 jclass appClass = NULL; // actual application class being launched
duke6e45e102007-12-01 00:00:00 +0000362 jmethodID mainID;
363 jobjectArray mainArgs;
364 int ret = 0;
365 jlong start, end;
366
michaelm5ac8c152012-03-06 20:34:38 +0000367 RegisterThread();
368
duke6e45e102007-12-01 00:00:00 +0000369 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000370 start = CounterGet();
371 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700372 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000373 exit(1);
374 }
375
ksrini05fdd5a2012-01-03 08:27:37 -0800376 if (showSettings != NULL) {
377 ShowSettings(env, showSettings);
378 CHECK_EXCEPTION_LEAVE(1);
379 }
380
duke6e45e102007-12-01 00:00:00 +0000381 if (printVersion || showVersion) {
382 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700383 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000384 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700385 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000386 }
387 }
388
389 /* If the user specified neither a class name nor a JAR file */
mchungea290e22011-01-21 09:43:57 -0800390 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
duke6e45e102007-12-01 00:00:00 +0000391 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700392 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700393 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000394 }
395
396 FreeKnownVMs(); /* after last possible PrintUsage() */
397
398 if (JLI_IsTraceLauncher()) {
399 end = CounterGet();
400 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
401 (long)(jint)Counter2Micros(end-start));
402 }
403
mchungea290e22011-01-21 09:43:57 -0800404 /* At this stage, argc/argv have the application's arguments */
duke6e45e102007-12-01 00:00:00 +0000405 if (JLI_IsTraceLauncher()){
406 int i;
mchungea290e22011-01-21 09:43:57 -0800407 printf("%s is '%s'\n", launchModeNames[mode], what);
408 printf("App's argc is %d\n", argc);
duke6e45e102007-12-01 00:00:00 +0000409 for (i=0; i < argc; i++) {
410 printf(" argv[%2d] = '%s'\n", i, argv[i]);
411 }
412 }
413
414 ret = 1;
415
416 /*
417 * Get the application's main class.
418 *
419 * See bugid 5030265. The Main-Class name has already been parsed
420 * from the manifest, but not parsed properly for UTF-8 support.
421 * Hence the code here ignores the value previously extracted and
422 * uses the pre-existing code to reextract the value. This is
423 * possibly an end of release cycle expedient. However, it has
424 * also been discovered that passing some character sets through
425 * the environment has "strange" behavior on some variants of
426 * Windows. Hence, maybe the manifest parsing code local to the
427 * launcher should never be enhanced.
428 *
429 * Hence, future work should either:
430 * 1) Correct the local parsing code and verify that the
431 * Main-Class attribute gets properly passed through
432 * all environments,
433 * 2) Remove the vestages of maintaining main_class through
434 * the environment (and remove these comments).
ksrini6b41ee82012-11-19 19:49:38 -0800435 *
436 * This method also correctly handles launching existing JavaFX
437 * applications that may or may not have a Main-Class manifest entry.
duke6e45e102007-12-01 00:00:00 +0000438 */
mchungea290e22011-01-21 09:43:57 -0800439 mainClass = LoadMainClass(env, mode, what);
ksrini20a64b22008-09-24 15:07:41 -0700440 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
ksrini6b41ee82012-11-19 19:49:38 -0800441 /*
442 * In some cases when launching an application that needs a helper, e.g., a
443 * JavaFX application with no main method, the mainClass will not be the
444 * applications own main class but rather a helper class. To keep things
445 * consistent in the UI we need to track and report the application main class.
446 */
447 appClass = GetApplicationClass(env);
ksrini07e328d2013-05-07 13:15:28 -0700448 NULL_CHECK_RETURN_VALUE(appClass, -1);
ksrini6b41ee82012-11-19 19:49:38 -0800449 /*
450 * PostJVMInit uses the class name as the application name for GUI purposes,
451 * for example, on OSX this sets the application name in the menu bar for
452 * both SWT and JavaFX. So we'll pass the actual application class here
453 * instead of mainClass as that may be a launcher or helper class instead
454 * of the application class.
455 */
456 PostJVMInit(env, appClass, vm);
ksrini20a64b22008-09-24 15:07:41 -0700457 /*
458 * The LoadMainClass not only loads the main class, it will also ensure
459 * that the main method's signature is correct, therefore further checking
460 * is not required. The main method is invoked here so that extraneous java
461 * stacks are not in the application stack trace.
462 */
duke6e45e102007-12-01 00:00:00 +0000463 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
464 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700465 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000466
ksrinie97d4002012-07-31 06:14:28 -0700467 /* Build platform specific argument array */
468 mainArgs = CreateApplicationArgs(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700469 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000470
471 /* Invoke main method. */
472 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
473
474 /*
475 * The launcher's exit code (in the absence of calls to
476 * System.exit) will be non-zero if main threw an exception.
477 */
478 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700479 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000480}
481
duke6e45e102007-12-01 00:00:00 +0000482/*
483 * Checks the command line options to find which JVM type was
484 * specified. If no command line option was given for the JVM type,
485 * the default type is used. The environment variable
486 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
487 * checked as ways of specifying which JVM type to invoke.
488 */
489char *
490CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
491 int i, argi;
492 int argc;
493 char **newArgv;
494 int newArgvIdx = 0;
495 int isVMType;
496 int jvmidx = -1;
497 char *jvmtype = getenv("JDK_ALTERNATE_VM");
498
499 argc = *pargc;
500
501 /* To make things simpler we always copy the argv array */
502 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
503
504 /* The program name is always present */
505 newArgv[newArgvIdx++] = (*argv)[0];
506
507 for (argi = 1; argi < argc; argi++) {
508 char *arg = (*argv)[argi];
509 isVMType = 0;
510
511 if (IsJavaArgs()) {
512 if (arg[0] != '-') {
513 newArgv[newArgvIdx++] = arg;
514 continue;
515 }
516 } else {
517 if (JLI_StrCmp(arg, "-classpath") == 0 ||
518 JLI_StrCmp(arg, "-cp") == 0) {
519 newArgv[newArgvIdx++] = arg;
520 argi++;
521 if (argi < argc) {
522 newArgv[newArgvIdx++] = (*argv)[argi];
523 }
524 continue;
525 }
526 if (arg[0] != '-') break;
527 }
528
529 /* Did the user pass an explicit VM type? */
530 i = KnownVMIndex(arg);
531 if (i >= 0) {
532 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
533 isVMType = 1;
534 *pargc = *pargc - 1;
535 }
536
537 /* Did the user specify an "alternate" VM? */
538 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
539 isVMType = 1;
540 jvmtype = arg+((arg[1]=='X')? 10 : 12);
541 jvmidx = -1;
542 }
543
544 if (!isVMType) {
545 newArgv[newArgvIdx++] = arg;
546 }
547 }
548
549 /*
550 * Finish copying the arguments if we aborted the above loop.
551 * NOTE that if we aborted via "break" then we did NOT copy the
552 * last argument above, and in addition argi will be less than
553 * argc.
554 */
555 while (argi < argc) {
556 newArgv[newArgvIdx++] = (*argv)[argi];
557 argi++;
558 }
559
560 /* argv is null-terminated */
561 newArgv[newArgvIdx] = 0;
562
563 /* Copy back argv */
564 *argv = newArgv;
565 *pargc = newArgvIdx;
566
567 /* use the default VM type if not specified (no alias processing) */
568 if (jvmtype == NULL) {
569 char* result = knownVMs[0].name+1;
570 /* Use a different VM type if we are on a server class machine? */
571 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
572 (ServerClassMachine() == JNI_TRUE)) {
573 result = knownVMs[0].server_class+1;
574 }
575 JLI_TraceLauncher("Default VM: %s\n", result);
576 return result;
577 }
578
579 /* if using an alternate VM, no alias processing */
580 if (jvmidx < 0)
581 return jvmtype;
582
583 /* Resolve aliases first */
584 {
585 int loopCount = 0;
586 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
587 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
588
589 if (loopCount > knownVMsCount) {
590 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700591 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000592 exit(1);
593 } else {
594 return "ERROR";
595 /* break; */
596 }
597 }
598
599 if (nextIdx < 0) {
600 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700601 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000602 exit(1);
603 } else {
604 return "ERROR";
605 }
606 }
607 jvmidx = nextIdx;
608 jvmtype = knownVMs[jvmidx].name+1;
609 loopCount++;
610 }
611 }
612
613 switch (knownVMs[jvmidx].flag) {
614 case VM_WARN:
615 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700616 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000617 }
618 /* fall through */
619 case VM_IGNORE:
620 jvmtype = knownVMs[jvmidx=0].name + 1;
621 /* fall through */
622 case VM_KNOWN:
623 break;
624 case VM_ERROR:
625 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700626 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000627 exit(1);
628 } else {
629 return "ERROR";
630 }
631 }
632
633 return jvmtype;
634}
635
636/* copied from HotSpot function "atomll()" */
637static int
ksrini8e20e1c2010-11-23 16:52:39 -0800638parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000639 jlong n = 0;
640 int args_read = sscanf(s, jlong_format_specifier(), &n);
641 if (args_read != 1) {
642 return 0;
643 }
644 while (*s != '\0' && *s >= '0' && *s <= '9') {
645 s++;
646 }
647 // 4705540: illegal if more characters are found after the first non-digit
648 if (JLI_StrLen(s) > 1) {
649 return 0;
650 }
651 switch (*s) {
652 case 'T': case 't':
653 *result = n * GB * KB;
654 return 1;
655 case 'G': case 'g':
656 *result = n * GB;
657 return 1;
658 case 'M': case 'm':
659 *result = n * MB;
660 return 1;
661 case 'K': case 'k':
662 *result = n * KB;
663 return 1;
664 case '\0':
665 *result = n;
666 return 1;
667 default:
668 /* Create JVM with default stack and let VM handle malformed -Xss string*/
669 return 0;
670 }
671}
672
673/*
674 * Adds a new VM option with the given given name and value.
675 */
676void
677AddOption(char *str, void *info)
678{
679 /*
680 * Expand options array if needed to accommodate at least one more
681 * VM option.
682 */
683 if (numOptions >= maxOptions) {
684 if (options == 0) {
685 maxOptions = 4;
686 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
687 } else {
688 JavaVMOption *tmp;
689 maxOptions *= 2;
690 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
691 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
692 JLI_MemFree(options);
693 options = tmp;
694 }
695 }
696 options[numOptions].optionString = str;
697 options[numOptions++].extraInfo = info;
698
699 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800700 jlong tmp;
701 if (parse_size(str + 4, &tmp)) {
702 threadStackSize = tmp;
703 }
704 }
705
706 if (JLI_StrCCmp(str, "-Xmx") == 0) {
707 jlong tmp;
708 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800709 maxHeapSize = tmp;
710 }
711 }
712
713 if (JLI_StrCCmp(str, "-Xms") == 0) {
714 jlong tmp;
715 if (parse_size(str + 4, &tmp)) {
mchungea290e22011-01-21 09:43:57 -0800716 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800717 }
duke6e45e102007-12-01 00:00:00 +0000718 }
719}
720
721static void
722SetClassPath(const char *s)
723{
724 char *def;
martinc0ca3352009-06-22 16:41:27 -0700725 const char *orig = s;
726 static const char format[] = "-Djava.class.path=%s";
ksrinie1a34382012-04-24 10:37:01 -0700727 /*
728 * usually we should not get a null pointer, but there are cases where
729 * we might just get one, in which case we simply ignore it, and let the
730 * caller deal with it
731 */
732 if (s == NULL)
733 return;
duke6e45e102007-12-01 00:00:00 +0000734 s = JLI_WildcardExpandClasspath(s);
martinc0ca3352009-06-22 16:41:27 -0700735 def = JLI_MemAlloc(sizeof(format)
736 - 2 /* strlen("%s") */
737 + JLI_StrLen(s));
738 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000739 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700740 if (s != orig)
741 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000742}
743
744/*
745 * The SelectVersion() routine ensures that an appropriate version of
746 * the JRE is running. The specification for the appropriate version
747 * is obtained from either the manifest of a jar file (preferred) or
748 * from command line options.
749 * The routine also parses splash screen command line options and
750 * passes on their values in private environment variables.
751 */
752static void
753SelectVersion(int argc, char **argv, char **main_class)
754{
755 char *arg;
756 char **new_argv;
757 char **new_argp;
758 char *operand;
759 char *version = NULL;
760 char *jre = NULL;
761 int jarflag = 0;
762 int headlessflag = 0;
763 int restrict_search = -1; /* -1 implies not known */
764 manifest_info info;
765 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
766 char *splash_file_name = NULL;
767 char *splash_jar_name = NULL;
768 char *env_in;
769 int res;
770
771 /*
772 * If the version has already been selected, set *main_class
773 * with the value passed through the environment (if any) and
774 * simply return.
775 */
776 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
777 if (*env_in != '\0')
778 *main_class = JLI_StringDup(env_in);
779 return;
780 }
781
782 /*
783 * Scan through the arguments for options relevant to multiple JRE
784 * support. For reference, the command line syntax is defined as:
785 *
786 * SYNOPSIS
787 * java [options] class [argument...]
788 *
789 * java [options] -jar file.jar [argument...]
790 *
791 * As the scan is performed, make a copy of the argument list with
792 * the version specification options (new to 1.5) removed, so that
793 * a version less than 1.5 can be exec'd.
794 *
795 * Note that due to the syntax of the native Windows interface
796 * CreateProcess(), processing similar to the following exists in
797 * the Windows platform specific routine ExecJRE (in java_md.c).
798 * Changes here should be reproduced there.
799 */
800 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
801 new_argv[0] = argv[0];
802 new_argp = &new_argv[1];
803 argc--;
804 argv++;
805 while ((arg = *argv) != 0 && *arg == '-') {
806 if (JLI_StrCCmp(arg, "-version:") == 0) {
807 version = arg + 9;
808 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
809 restrict_search = 1;
810 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
811 restrict_search = 0;
812 } else {
813 if (JLI_StrCmp(arg, "-jar") == 0)
814 jarflag = 1;
815 /* deal with "unfortunate" classpath syntax */
816 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
817 (argc >= 2)) {
818 *new_argp++ = arg;
819 argc--;
820 argv++;
821 arg = *argv;
822 }
823
824 /*
825 * Checking for headless toolkit option in the some way as AWT does:
826 * "true" means true and any other value means false
827 */
828 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
829 headlessflag = 1;
830 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
831 headlessflag = 0;
832 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
833 splash_file_name = arg+8;
834 }
835 *new_argp++ = arg;
836 }
837 argc--;
838 argv++;
839 }
840 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
841 operand = NULL;
842 } else {
843 argc--;
844 *new_argp++ = operand = *argv++;
845 }
846 while (argc-- > 0) /* Copy over [argument...] */
847 *new_argp++ = *argv++;
848 *new_argp = NULL;
849
850 /*
851 * If there is a jar file, read the manifest. If the jarfile can't be
852 * read, the manifest can't be read from the jar file, or the manifest
853 * is corrupt, issue the appropriate error messages and exit.
854 *
855 * Even if there isn't a jar file, construct a manifest_info structure
856 * containing the command line information. It's a convenient way to carry
857 * this data around.
858 */
859 if (jarflag && operand) {
860 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
861 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700862 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000863 else
ksrini0e817162008-08-26 10:21:20 -0700864 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000865 exit(1);
866 }
867
868 /*
869 * Command line splash screen option should have precedence
870 * over the manifest, so the manifest data is used only if
871 * splash_file_name has not been initialized above during command
872 * line parsing
873 */
874 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
875 splash_file_name = info.splashscreen_image_file_name;
876 splash_jar_name = operand;
877 }
878 } else {
879 info.manifest_version = NULL;
880 info.main_class = NULL;
881 info.jre_version = NULL;
882 info.jre_restrict_search = 0;
883 }
884
885 /*
886 * Passing on splash screen info in environment variables
887 */
888 if (splash_file_name && !headlessflag) {
889 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
890 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
891 JLI_StrCat(splash_file_entry, splash_file_name);
892 putenv(splash_file_entry);
893 }
894 if (splash_jar_name && !headlessflag) {
895 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
896 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
897 JLI_StrCat(splash_jar_entry, splash_jar_name);
898 putenv(splash_jar_entry);
899 }
900
901 /*
902 * The JRE-Version and JRE-Restrict-Search values (if any) from the
903 * manifest are overwritten by any specified on the command line.
904 */
905 if (version != NULL)
906 info.jre_version = version;
907 if (restrict_search != -1)
908 info.jre_restrict_search = restrict_search;
909
910 /*
911 * "Valid" returns (other than unrecoverable errors) follow. Set
912 * main_class as a side-effect of this routine.
913 */
914 if (info.main_class != NULL)
915 *main_class = JLI_StringDup(info.main_class);
916
917 /*
918 * If no version selection information is found either on the command
919 * line or in the manifest, simply return.
920 */
921 if (info.jre_version == NULL) {
922 JLI_FreeManifest();
923 JLI_MemFree(new_argv);
924 return;
925 }
926
927 /*
928 * Check for correct syntax of the version specification (JSR 56).
929 */
930 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -0700931 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000932 exit(1);
933 }
934
935 /*
936 * Find the appropriate JVM on the system. Just to be as forgiving as
937 * possible, if the standard algorithms don't locate an appropriate
938 * jre, check to see if the one running will satisfy the requirements.
939 * This can happen on systems which haven't been set-up for multiple
940 * JRE support.
941 */
942 jre = LocateJRE(&info);
943 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
944 (info.jre_version?info.jre_version:"null"),
945 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
946
947 if (jre == NULL) {
948 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
949 JLI_FreeManifest();
950 JLI_MemFree(new_argv);
951 return;
952 } else {
ksrini0e817162008-08-26 10:21:20 -0700953 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +0000954 exit(1);
955 }
956 }
957
958 /*
959 * If I'm not the chosen one, exec the chosen one. Returning from
960 * ExecJRE indicates that I am indeed the chosen one.
961 *
962 * The private environment variable _JAVA_VERSION_SET is used to
963 * prevent the chosen one from re-reading the manifest file and
964 * using the values found within to override the (potential) command
965 * line flags stripped from argv (because the target may not
966 * understand them). Passing the MainClass value is an optimization
967 * to avoid locating, expanding and parsing the manifest extra
968 * times.
969 */
ksrini8760ca92008-09-04 09:43:32 -0700970 if (info.main_class != NULL) {
971 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
972 (void)JLI_StrCat(env_entry, info.main_class);
973 } else {
asaha80ccd422009-04-16 21:08:04 -0700974 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -0700975 exit(1);
976 }
977 }
duke6e45e102007-12-01 00:00:00 +0000978 (void)putenv(env_entry);
979 ExecJRE(jre, new_argv);
980 JLI_FreeManifest();
981 JLI_MemFree(new_argv);
982 return;
983}
984
985/*
986 * Parses command line arguments. Returns JNI_FALSE if launcher
987 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -0800988 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +0000989 * process return value) is set to 0 for a normal exit.
990 */
991static jboolean
mchungea290e22011-01-21 09:43:57 -0800992ParseArguments(int *pargc, char ***pargv,
993 int *pmode, char **pwhat,
994 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +0000995{
996 int argc = *pargc;
997 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -0800998 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +0000999 char *arg;
1000
1001 *pret = 0;
1002
1003 while ((arg = *argv) != 0 && *arg == '-') {
1004 argv++; --argc;
1005 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
1006 ARG_CHECK (argc, ARG_ERROR1, arg);
1007 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -08001008 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +00001009 argv++; --argc;
1010 } else if (JLI_StrCmp(arg, "-jar") == 0) {
1011 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -08001012 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +00001013 } else if (JLI_StrCmp(arg, "-help") == 0 ||
1014 JLI_StrCmp(arg, "-h") == 0 ||
1015 JLI_StrCmp(arg, "-?") == 0) {
1016 printUsage = JNI_TRUE;
1017 return JNI_TRUE;
1018 } else if (JLI_StrCmp(arg, "-version") == 0) {
1019 printVersion = JNI_TRUE;
1020 return JNI_TRUE;
1021 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1022 showVersion = JNI_TRUE;
1023 } else if (JLI_StrCmp(arg, "-X") == 0) {
1024 printXUsage = JNI_TRUE;
1025 return JNI_TRUE;
1026/*
ksrini8e20e1c2010-11-23 16:52:39 -08001027 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1028 * In the latter case, any SUBOPT value not recognized will default to "all"
1029 */
1030 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1031 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1032 showSettings = arg;
ksrini9636e2e2011-02-03 15:41:23 -08001033 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1034 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrini8e20e1c2010-11-23 16:52:39 -08001035/*
duke6e45e102007-12-01 00:00:00 +00001036 * The following case provide backward compatibility with old-style
1037 * command line options.
1038 */
1039 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001040 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001041 return JNI_FALSE;
1042 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1043 AddOption("-verbose:gc", NULL);
1044 } else if (JLI_StrCmp(arg, "-t") == 0) {
1045 AddOption("-Xt", NULL);
1046 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1047 AddOption("-Xtm", NULL);
1048 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1049 AddOption("-Xdebug", NULL);
1050 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1051 AddOption("-Xnoclassgc", NULL);
1052 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1053 AddOption("-Xverify:all", NULL);
1054 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1055 AddOption("-Xverify:all", NULL);
1056 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1057 AddOption("-Xverify:remote", NULL);
1058 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1059 AddOption("-Xverify:none", NULL);
1060 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1061 char *p = arg + 5;
1062 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1063 if (*p) {
1064 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1065 } else {
1066 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1067 }
1068 AddOption(tmp, NULL);
1069 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1070 JLI_StrCCmp(arg, "-oss") == 0 ||
1071 JLI_StrCCmp(arg, "-ms") == 0 ||
1072 JLI_StrCCmp(arg, "-mx") == 0) {
1073 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1074 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1075 AddOption(tmp, NULL);
1076 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1077 JLI_StrCmp(arg, "-cs") == 0 ||
1078 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1079 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001080 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001081 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1082 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1083 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1084 JLI_StrCCmp(arg, "-splash:") == 0) {
1085 ; /* Ignore machine independent options already handled */
michaelm5ac8c152012-03-06 20:34:38 +00001086 } else if (ProcessPlatformOption(arg)) {
1087 ; /* Processing of platform dependent options */
1088 } else if (RemovableOption(arg)) {
duke6e45e102007-12-01 00:00:00 +00001089 ; /* Do not pass option to vm. */
1090 } else {
1091 AddOption(arg, NULL);
1092 }
1093 }
1094
1095 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001096 *pwhat = *argv++;
1097 }
1098
1099 if (*pwhat == NULL) {
1100 *pret = 1;
1101 } else if (mode == LM_UNKNOWN) {
1102 /* default to LM_CLASS if -jar and -cp option are
1103 * not specified */
1104 mode = LM_CLASS;
1105 }
1106
1107 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001108 *pargc = argc;
1109 *pargv = argv;
1110 }
mchungea290e22011-01-21 09:43:57 -08001111
1112 *pmode = mode;
1113
duke6e45e102007-12-01 00:00:00 +00001114 return JNI_TRUE;
1115}
1116
1117/*
1118 * Initializes the Java Virtual Machine. Also frees options array when
1119 * finished.
1120 */
1121static jboolean
1122InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1123{
1124 JavaVMInitArgs args;
1125 jint r;
1126
1127 memset(&args, 0, sizeof(args));
1128 args.version = JNI_VERSION_1_2;
1129 args.nOptions = numOptions;
1130 args.options = options;
1131 args.ignoreUnrecognized = JNI_FALSE;
1132
1133 if (JLI_IsTraceLauncher()) {
1134 int i = 0;
1135 printf("JavaVM args:\n ");
1136 printf("version 0x%08lx, ", (long)args.version);
1137 printf("ignoreUnrecognized is %s, ",
1138 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1139 printf("nOptions is %ld\n", (long)args.nOptions);
1140 for (i = 0; i < numOptions; i++)
1141 printf(" option[%2d] = '%s'\n",
1142 i, args.options[i].optionString);
1143 }
1144
1145 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1146 JLI_MemFree(options);
1147 return r == JNI_OK;
1148}
1149
ksrini7d9872e2011-03-20 08:41:33 -07001150static jclass helperClass = NULL;
1151
ksrinie97d4002012-07-31 06:14:28 -07001152jclass
1153GetLauncherHelperClass(JNIEnv *env)
1154{
ksrini7d9872e2011-03-20 08:41:33 -07001155 if (helperClass == NULL) {
1156 NULL_CHECK0(helperClass = FindBootStrapClass(env,
1157 "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001158 }
ksrini7d9872e2011-03-20 08:41:33 -07001159 return helperClass;
duke6e45e102007-12-01 00:00:00 +00001160}
1161
ksrini7d9872e2011-03-20 08:41:33 -07001162static jmethodID makePlatformStringMID = NULL;
duke6e45e102007-12-01 00:00:00 +00001163/*
1164 * Returns a new Java string object for the specified platform string.
1165 */
1166static jstring
1167NewPlatformString(JNIEnv *env, char *s)
1168{
1169 int len = (int)JLI_StrLen(s);
duke6e45e102007-12-01 00:00:00 +00001170 jbyteArray ary;
ksrini7d9872e2011-03-20 08:41:33 -07001171 jclass cls = GetLauncherHelperClass(env);
1172 NULL_CHECK0(cls);
duke6e45e102007-12-01 00:00:00 +00001173 if (s == NULL)
1174 return 0;
duke6e45e102007-12-01 00:00:00 +00001175
1176 ary = (*env)->NewByteArray(env, len);
1177 if (ary != 0) {
1178 jstring str = 0;
1179 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1180 if (!(*env)->ExceptionOccurred(env)) {
ksrini7d9872e2011-03-20 08:41:33 -07001181 if (makePlatformStringMID == NULL) {
1182 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1183 cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
duke6e45e102007-12-01 00:00:00 +00001184 }
ksrini7d9872e2011-03-20 08:41:33 -07001185 str = (*env)->CallStaticObjectMethod(env, cls,
1186 makePlatformStringMID, USE_STDERR, ary);
duke6e45e102007-12-01 00:00:00 +00001187 (*env)->DeleteLocalRef(env, ary);
1188 return str;
1189 }
1190 }
1191 return 0;
1192}
1193
1194/*
1195 * Returns a new array of Java string objects for the specified
1196 * array of platform strings.
1197 */
ksrinie97d4002012-07-31 06:14:28 -07001198jobjectArray
duke6e45e102007-12-01 00:00:00 +00001199NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1200{
1201 jarray cls;
1202 jarray ary;
1203 int i;
1204
ksrini20a64b22008-09-24 15:07:41 -07001205 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001206 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1207 for (i = 0; i < strc; i++) {
1208 jstring str = NewPlatformString(env, *strv++);
1209 NULL_CHECK0(str);
1210 (*env)->SetObjectArrayElement(env, ary, i, str);
1211 (*env)->DeleteLocalRef(env, str);
1212 }
1213 return ary;
1214}
1215
1216/*
ksrini20a64b22008-09-24 15:07:41 -07001217 * Loads a class and verifies that the main class is present and it is ok to
1218 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001219 */
1220static jclass
mchungea290e22011-01-21 09:43:57 -08001221LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001222{
ksrini20a64b22008-09-24 15:07:41 -07001223 jmethodID mid;
1224 jstring str;
1225 jobject result;
duke6e45e102007-12-01 00:00:00 +00001226 jlong start, end;
ksrini7d9872e2011-03-20 08:41:33 -07001227 jclass cls = GetLauncherHelperClass(env);
1228 NULL_CHECK0(cls);
ksrini20a64b22008-09-24 15:07:41 -07001229 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001230 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001231 }
ksrini7d9872e2011-03-20 08:41:33 -07001232 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1233 "checkAndLoadMain",
1234 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1235
ksriniac5e02f2012-01-11 08:14:47 -08001236 str = NewPlatformString(env, name);
ksrini7d9872e2011-03-20 08:41:33 -07001237 result = (*env)->CallStaticObjectMethod(env, cls, mid, USE_STDERR, mode, str);
duke6e45e102007-12-01 00:00:00 +00001238
1239 if (JLI_IsTraceLauncher()) {
1240 end = CounterGet();
1241 printf("%ld micro seconds to load main class\n",
1242 (long)(jint)Counter2Micros(end-start));
ksrinie97d4002012-07-31 06:14:28 -07001243 printf("----%s----\n", JLDEBUG_ENV_ENTRY);
duke6e45e102007-12-01 00:00:00 +00001244 }
1245
ksrini20a64b22008-09-24 15:07:41 -07001246 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001247}
1248
ksrini6b41ee82012-11-19 19:49:38 -08001249static jclass
1250GetApplicationClass(JNIEnv *env)
1251{
1252 jmethodID mid;
1253 jobject result;
1254 jclass cls = GetLauncherHelperClass(env);
1255 NULL_CHECK0(cls);
1256 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1257 "getApplicationClass",
1258 "()Ljava/lang/Class;"));
1259
1260 return (*env)->CallStaticObjectMethod(env, cls, mid);
1261}
1262
duke6e45e102007-12-01 00:00:00 +00001263/*
1264 * For tools, convert command line args thus:
1265 * javac -cp foo:foo/"*" -J-ms32m ...
1266 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1267 *
1268 * Takes 4 parameters, and returns the populated arguments
1269 */
1270static void
1271TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1272{
1273 int argc = *pargc;
1274 char **argv = *pargv;
1275 int nargc = argc + jargc;
1276 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1277 int i;
1278
1279 *pargc = nargc;
1280 *pargv = nargv;
1281
1282 /* Copy the VM arguments (i.e. prefixed with -J) */
1283 for (i = 0; i < jargc; i++) {
1284 const char *arg = jargv[i];
1285 if (arg[0] == '-' && arg[1] == 'J') {
1286 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1287 }
1288 }
1289
1290 for (i = 0; i < argc; i++) {
1291 char *arg = argv[i];
1292 if (arg[0] == '-' && arg[1] == 'J') {
1293 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001294 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001295 exit(1);
1296 }
1297 *nargv++ = arg + 2;
1298 }
1299 }
1300
1301 /* Copy the rest of the arguments */
1302 for (i = 0; i < jargc ; i++) {
1303 const char *arg = jargv[i];
1304 if (arg[0] != '-' || arg[1] != 'J') {
1305 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1306 }
1307 }
1308 for (i = 0; i < argc; i++) {
1309 char *arg = argv[i];
1310 if (arg[0] == '-') {
1311 if (arg[1] == 'J')
1312 continue;
1313 if (IsWildCardEnabled() && arg[1] == 'c'
1314 && (JLI_StrCmp(arg, "-cp") == 0 ||
1315 JLI_StrCmp(arg, "-classpath") == 0)
1316 && i < argc - 1) {
1317 *nargv++ = arg;
1318 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1319 i++;
1320 continue;
1321 }
1322 }
1323 *nargv++ = arg;
1324 }
1325 *nargv = 0;
1326}
1327
1328/*
1329 * For our tools, we try to add 3 VM options:
1330 * -Denv.class.path=<envcp>
1331 * -Dapplication.home=<apphome>
1332 * -Djava.class.path=<appcp>
1333 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1334 * tells javac where to find binary classes through this environment
1335 * variable. Notice that users will be able to compile against our
1336 * tools classes (sun.tools.javac.Main) only if they explicitly add
1337 * tools.jar to CLASSPATH.
1338 * <apphome> is the directory where the application is installed.
1339 * <appcp> is the classpath to where our apps' classfiles are.
1340 */
1341static jboolean
1342AddApplicationOptions(int cpathc, const char **cpathv)
1343{
1344 char *envcp, *appcp, *apphome;
1345 char home[MAXPATHLEN]; /* application home */
1346 char separator[] = { PATH_SEPARATOR, '\0' };
1347 int size, i;
1348
1349 {
1350 const char *s = getenv("CLASSPATH");
1351 if (s) {
1352 s = (char *) JLI_WildcardExpandClasspath(s);
1353 /* 40 for -Denv.class.path= */
1354 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1355 sprintf(envcp, "-Denv.class.path=%s", s);
1356 AddOption(envcp, NULL);
1357 }
1358 }
1359
1360 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001361 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001362 return JNI_FALSE;
1363 }
1364
1365 /* 40 for '-Dapplication.home=' */
1366 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1367 sprintf(apphome, "-Dapplication.home=%s", home);
1368 AddOption(apphome, NULL);
1369
1370 /* How big is the application's classpath? */
1371 size = 40; /* 40: "-Djava.class.path=" */
1372 for (i = 0; i < cpathc; i++) {
1373 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1374 }
1375 appcp = (char *)JLI_MemAlloc(size + 1);
1376 JLI_StrCpy(appcp, "-Djava.class.path=");
1377 for (i = 0; i < cpathc; i++) {
1378 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1379 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1380 JLI_StrCat(appcp, separator); /* ; */
1381 }
1382 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1383 AddOption(appcp, NULL);
1384 return JNI_TRUE;
1385}
1386
1387/*
1388 * inject the -Dsun.java.command pseudo property into the args structure
1389 * this pseudo property is used in the HotSpot VM to expose the
1390 * Java class name and arguments to the main method to the VM. The
1391 * HotSpot VM uses this pseudo property to store the Java class name
1392 * (or jar file name) and the arguments to the class's main method
1393 * to the instrumentation memory region. The sun.java.command pseudo
1394 * property is not exported by HotSpot to the Java layer.
1395 */
1396void
mchungea290e22011-01-21 09:43:57 -08001397SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001398{
1399
1400 int i = 0;
1401 size_t len = 0;
1402 char* javaCommand = NULL;
1403 char* dashDstr = "-Dsun.java.command=";
1404
mchungea290e22011-01-21 09:43:57 -08001405 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001406 /* unexpected, one of these should be set. just return without
1407 * setting the property
1408 */
1409 return;
1410 }
1411
duke6e45e102007-12-01 00:00:00 +00001412 /* determine the amount of memory to allocate assuming
1413 * the individual components will be space separated
1414 */
mchungea290e22011-01-21 09:43:57 -08001415 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001416 for (i = 0; i < argc; i++) {
1417 len += JLI_StrLen(argv[i]) + 1;
1418 }
1419
1420 /* allocate the memory */
1421 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1422
1423 /* build the -D string */
1424 *javaCommand = '\0';
1425 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001426 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001427
1428 for (i = 0; i < argc; i++) {
1429 /* the components of the string are space separated. In
1430 * the case of embedded white space, the relationship of
1431 * the white space separated components to their true
1432 * positional arguments will be ambiguous. This issue may
1433 * be addressed in a future release.
1434 */
1435 JLI_StrCat(javaCommand, " ");
1436 JLI_StrCat(javaCommand, argv[i]);
1437 }
1438
1439 AddOption(javaCommand, NULL);
1440}
1441
1442/*
1443 * JVM would like to know if it's created by a standard Sun launcher, or by
1444 * user native application, the following property indicates the former.
1445 */
mchungea290e22011-01-21 09:43:57 -08001446void
1447SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001448 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1449}
1450
1451/*
1452 * Prints the version information from the java.version and other properties.
1453 */
1454static void
1455PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1456{
1457 jclass ver;
1458 jmethodID print;
1459
ksrini20a64b22008-09-24 15:07:41 -07001460 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001461 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1462 ver,
1463 (extraLF == JNI_TRUE) ? "println" : "print",
1464 "()V"
1465 )
1466 );
1467
1468 (*env)->CallStaticVoidMethod(env, ver, print);
1469}
1470
1471/*
ksrini8e20e1c2010-11-23 16:52:39 -08001472 * Prints all the Java settings, see the java implementation for more details.
1473 */
1474static void
1475ShowSettings(JNIEnv *env, char *optString)
1476{
ksrini8e20e1c2010-11-23 16:52:39 -08001477 jmethodID showSettingsID;
1478 jstring joptString;
ksrini7d9872e2011-03-20 08:41:33 -07001479 jclass cls = GetLauncherHelperClass(env);
1480 NULL_CHECK(cls);
ksrini8e20e1c2010-11-23 16:52:39 -08001481 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001482 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001483 joptString = (*env)->NewStringUTF(env, optString);
1484 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
ksrini7d9872e2011-03-20 08:41:33 -07001485 USE_STDERR,
ksrini8e20e1c2010-11-23 16:52:39 -08001486 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001487 (jlong)initialHeapSize,
1488 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001489 (jlong)threadStackSize,
1490 ServerClassMachine());
1491}
1492
1493/*
ksrini20a64b22008-09-24 15:07:41 -07001494 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001495 */
1496static void
1497PrintUsage(JNIEnv* env, jboolean doXUsage)
1498{
duke6e45e102007-12-01 00:00:00 +00001499 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1500 jstring jprogname, vm1, vm2;
1501 int i;
ksrini7d9872e2011-03-20 08:41:33 -07001502 jclass cls = GetLauncherHelperClass(env);
1503 NULL_CHECK(cls);
duke6e45e102007-12-01 00:00:00 +00001504 if (doXUsage) {
1505 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1506 "printXUsageMessage", "(Z)V"));
ksrini7d9872e2011-03-20 08:41:33 -07001507 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001508 } else {
1509 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1510 "initHelpMessage", "(Ljava/lang/String;)V"));
1511
1512 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1513 "(Ljava/lang/String;Ljava/lang/String;)V"));
1514
1515 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1516 "appendVmSynonymMessage",
1517 "(Ljava/lang/String;Ljava/lang/String;)V"));
1518 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1519 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1520
1521 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1522 "printHelpMessage", "(Z)V"));
1523
1524 jprogname = (*env)->NewStringUTF(env, _program_name);
1525
1526 /* Initialize the usage message with the usual preamble */
1527 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1528
1529
1530 /* Assemble the other variant part of the usage */
1531 if ((knownVMs[0].flag == VM_KNOWN) ||
1532 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1533 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1534 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1535 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1536 }
1537 for (i=1; i<knownVMsCount; i++) {
1538 if (knownVMs[i].flag == VM_KNOWN) {
1539 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1540 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1541 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1542 }
1543 }
1544 for (i=1; i<knownVMsCount; i++) {
1545 if (knownVMs[i].flag == VM_ALIASED_TO) {
1546 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1547 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1548 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1549 }
1550 }
1551
1552 /* The first known VM is the default */
1553 {
1554 jboolean isServerClassMachine = ServerClassMachine();
1555
1556 const char* defaultVM = knownVMs[0].name+1;
1557 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1558 defaultVM = knownVMs[0].server_class+1;
1559 }
1560
1561 vm1 = (*env)->NewStringUTF(env, defaultVM);
1562 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1563 }
1564
1565 /* Complete the usage message and print to stderr*/
ksrini7d9872e2011-03-20 08:41:33 -07001566 (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001567 }
1568 return;
1569}
1570
1571/*
1572 * Read the jvm.cfg file and fill the knownJVMs[] array.
1573 *
1574 * The functionality of the jvm.cfg file is subject to change without
1575 * notice and the mechanism will be removed in the future.
1576 *
1577 * The lexical structure of the jvm.cfg file is as follows:
1578 *
1579 * jvmcfg := { vmLine }
1580 * vmLine := knownLine
1581 * | aliasLine
1582 * | warnLine
1583 * | ignoreLine
1584 * | errorLine
1585 * | predicateLine
1586 * | commentLine
1587 * knownLine := flag "KNOWN" EOL
1588 * warnLine := flag "WARN" EOL
1589 * ignoreLine := flag "IGNORE" EOL
1590 * errorLine := flag "ERROR" EOL
1591 * aliasLine := flag "ALIASED_TO" flag EOL
1592 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1593 * commentLine := "#" text EOL
1594 * flag := "-" identifier
1595 *
1596 * The semantics are that when someone specifies a flag on the command line:
1597 * - if the flag appears on a knownLine, then the identifier is used as
1598 * the name of the directory holding the JVM library (the name of the JVM).
1599 * - if the flag appears as the first flag on an aliasLine, the identifier
1600 * of the second flag is used as the name of the JVM.
1601 * - if the flag appears on a warnLine, the identifier is used as the
1602 * name of the JVM, but a warning is generated.
1603 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1604 * name of a JVM, but the identifier is ignored and the default vm used
1605 * - if the flag appears on an errorLine, an error is generated.
1606 * - if the flag appears as the first flag on a predicateLine, and
1607 * the machine on which you are running passes the predicate indicated,
1608 * then the identifier of the second flag is used as the name of the JVM,
1609 * otherwise the identifier of the first flag is used as the name of the JVM.
1610 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1611 * file determines the name of the JVM.
1612 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1613 * since they only make sense if someone hasn't specified the name of the
1614 * JVM on the command line.
1615 *
1616 * The intent of the jvm.cfg file is to allow several JVM libraries to
1617 * be installed in different subdirectories of a single JRE installation,
1618 * for space-savings and convenience in testing.
1619 * The intent is explicitly not to provide a full aliasing or predicate
1620 * mechanism.
1621 */
1622jint
michaelm5ac8c152012-03-06 20:34:38 +00001623ReadKnownVMs(const char *jvmCfgName, jboolean speculative)
duke6e45e102007-12-01 00:00:00 +00001624{
1625 FILE *jvmCfg;
duke6e45e102007-12-01 00:00:00 +00001626 char line[MAXPATHLEN+20];
1627 int cnt = 0;
1628 int lineno = 0;
1629 jlong start, end;
1630 int vmType;
1631 char *tmpPtr;
1632 char *altVMName = NULL;
1633 char *serverClassVMName = NULL;
1634 static char *whiteSpace = " \t";
1635 if (JLI_IsTraceLauncher()) {
1636 start = CounterGet();
1637 }
duke6e45e102007-12-01 00:00:00 +00001638
1639 jvmCfg = fopen(jvmCfgName, "r");
1640 if (jvmCfg == NULL) {
1641 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001642 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001643 exit(1);
1644 } else {
1645 return -1;
1646 }
1647 }
1648 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1649 vmType = VM_UNKNOWN;
1650 lineno++;
1651 if (line[0] == '#')
1652 continue;
1653 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001654 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001655 }
1656 if (cnt >= knownVMsLimit) {
1657 GrowKnownVMs(cnt);
1658 }
1659 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1660 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1661 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001662 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001663 } else {
1664 /* Null-terminate this string for JLI_StringDup below */
1665 *tmpPtr++ = 0;
1666 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1667 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001668 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001669 } else {
1670 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1671 vmType = VM_KNOWN;
1672 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1673 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1674 if (*tmpPtr != 0) {
1675 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1676 }
1677 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001678 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001679 } else {
1680 /* Null terminate altVMName */
1681 altVMName = tmpPtr;
1682 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1683 *tmpPtr = 0;
1684 vmType = VM_ALIASED_TO;
1685 }
1686 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1687 vmType = VM_WARN;
1688 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1689 vmType = VM_IGNORE;
1690 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1691 vmType = VM_ERROR;
1692 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1693 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1694 if (*tmpPtr != 0) {
1695 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1696 }
1697 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001698 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001699 } else {
1700 /* Null terminate server class VM name */
1701 serverClassVMName = tmpPtr;
1702 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1703 *tmpPtr = 0;
1704 vmType = VM_IF_SERVER_CLASS;
1705 }
1706 } else {
ksrini0e817162008-08-26 10:21:20 -07001707 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001708 vmType = VM_KNOWN;
1709 }
1710 }
1711 }
1712
1713 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1714 if (vmType != VM_UNKNOWN) {
1715 knownVMs[cnt].name = JLI_StringDup(line);
1716 knownVMs[cnt].flag = vmType;
1717 switch (vmType) {
1718 default:
1719 break;
1720 case VM_ALIASED_TO:
1721 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1722 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1723 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1724 break;
1725 case VM_IF_SERVER_CLASS:
1726 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1727 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1728 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1729 break;
1730 }
1731 cnt++;
1732 }
1733 }
1734 fclose(jvmCfg);
1735 knownVMsCount = cnt;
1736
1737 if (JLI_IsTraceLauncher()) {
1738 end = CounterGet();
1739 printf("%ld micro seconds to parse jvm.cfg\n",
1740 (long)(jint)Counter2Micros(end-start));
1741 }
1742
1743 return cnt;
1744}
1745
1746
1747static void
1748GrowKnownVMs(int minimum)
1749{
1750 struct vmdesc* newKnownVMs;
1751 int newMax;
1752
1753 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1754 if (newMax <= minimum) {
1755 newMax = minimum;
1756 }
1757 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1758 if (knownVMs != NULL) {
1759 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1760 }
1761 JLI_MemFree(knownVMs);
1762 knownVMs = newKnownVMs;
1763 knownVMsLimit = newMax;
1764}
1765
1766
1767/* Returns index of VM or -1 if not found */
1768static int
1769KnownVMIndex(const char* name)
1770{
1771 int i;
1772 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1773 for (i = 0; i < knownVMsCount; i++) {
1774 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1775 return i;
1776 }
1777 }
1778 return -1;
1779}
1780
1781static void
1782FreeKnownVMs()
1783{
1784 int i;
1785 for (i = 0; i < knownVMsCount; i++) {
1786 JLI_MemFree(knownVMs[i].name);
1787 knownVMs[i].name = NULL;
1788 }
1789 JLI_MemFree(knownVMs);
1790}
1791
duke6e45e102007-12-01 00:00:00 +00001792/*
1793 * Displays the splash screen according to the jar file name
1794 * and image file names stored in environment variables
1795 */
michaelm5ac8c152012-03-06 20:34:38 +00001796void
duke6e45e102007-12-01 00:00:00 +00001797ShowSplashScreen()
1798{
1799 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1800 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1801 int data_size;
1802 void *image_data;
1803 if (jar_name) {
1804 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1805 if (image_data) {
1806 DoSplashInit();
1807 DoSplashLoadMemory(image_data, data_size);
1808 JLI_MemFree(image_data);
1809 }
1810 } else if (file_name) {
1811 DoSplashInit();
1812 DoSplashLoadFile(file_name);
1813 } else {
1814 return;
1815 }
1816 DoSplashSetFileJarName(file_name, jar_name);
1817
1818 /*
1819 * Done with all command line processing and potential re-execs so
1820 * clean up the environment.
1821 */
1822 (void)UnsetEnv(ENV_ENTRY);
1823 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1824 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1825
1826 JLI_MemFree(splash_jar_entry);
1827 JLI_MemFree(splash_file_entry);
1828
1829}
1830
1831const char*
1832GetDotVersion()
1833{
1834 return _dVersion;
1835}
1836
1837const char*
1838GetFullVersion()
1839{
1840 return _fVersion;
1841}
1842
1843const char*
1844GetProgramName()
1845{
1846 return _program_name;
1847}
1848
1849const char*
1850GetLauncherName()
1851{
1852 return _launcher_name;
1853}
1854
1855jint
1856GetErgoPolicy()
1857{
1858 return _ergo_policy;
1859}
1860
1861jboolean
1862IsJavaArgs()
1863{
1864 return _is_java_args;
1865}
1866
1867static jboolean
1868IsWildCardEnabled()
1869{
1870 return _wc_enabled;
1871}
1872
michaelm5ac8c152012-03-06 20:34:38 +00001873int
1874ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
1875 int argc, char **argv,
mchungea290e22011-01-21 09:43:57 -08001876 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001877{
1878
1879 /*
1880 * If user doesn't specify stack size, check if VM has a preference.
1881 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1882 * return its default stack size through the init args structure.
1883 */
1884 if (threadStackSize == 0) {
1885 struct JDK1_1InitArgs args1_1;
1886 memset((void*)&args1_1, 0, sizeof(args1_1));
1887 args1_1.version = JNI_VERSION_1_1;
1888 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1889 if (args1_1.javaStackSize > 0) {
1890 threadStackSize = args1_1.javaStackSize;
1891 }
1892 }
1893
1894 { /* Create a new thread to create JVM and invoke main method */
1895 JavaMainArgs args;
1896 int rslt;
1897
1898 args.argc = argc;
1899 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001900 args.mode = mode;
1901 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001902 args.ifn = *ifn;
1903
1904 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1905 /* If the caller has deemed there is an error we
1906 * simply return that, otherwise we return the value of
1907 * the callee
1908 */
1909 return (ret != 0) ? ret : rslt;
1910 }
1911}
1912
1913static void
1914DumpState()
1915{
1916 if (!JLI_IsTraceLauncher()) return ;
1917 printf("Launcher state:\n");
1918 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1919 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1920 printf("\tprogram name:%s\n", GetProgramName());
1921 printf("\tlauncher name:%s\n", GetLauncherName());
1922 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1923 printf("\tfullversion:%s\n", GetFullVersion());
1924 printf("\tdotversion:%s\n", GetDotVersion());
1925 printf("\tergo_policy:");
1926 switch(GetErgoPolicy()) {
1927 case NEVER_SERVER_CLASS:
1928 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1929 break;
1930 case ALWAYS_SERVER_CLASS:
1931 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
1932 break;
1933 default:
1934 printf("DEFAULT_ERGONOMICS_POLICY\n");
1935 }
1936}
1937
1938/*
1939 * Return JNI_TRUE for an option string that has no effect but should
1940 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1941 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08001942 * -d32 or -d64 is passed to a binary with an unmatched data model
1943 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
1944 * exec to the proper binary). In the case of when the data model and the
1945 * requested version is matched, an exec would not occur, and these options
1946 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00001947 */
1948jboolean
1949RemovableOption(char * option)
1950{
1951 /*
1952 * Unconditionally remove both -d32 and -d64 options since only
1953 * the last such options has an effect; e.g.
1954 * java -d32 -d64 -d32 -version
1955 * is equivalent to
1956 * java -d32 -version
1957 */
1958
1959 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
1960 (JLI_StrCCmp(option, "-d64") == 0 ) )
1961 return JNI_TRUE;
1962 else
1963 return JNI_FALSE;
1964}
1965
1966/*
1967 * A utility procedure to always print to stderr
1968 */
1969void
ksrini0e817162008-08-26 10:21:20 -07001970JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00001971{
1972 va_list vl;
1973 va_start(vl, fmt);
1974 vfprintf(stderr, fmt, vl);
1975 fprintf(stderr, "\n");
1976 va_end(vl);
1977}