blob: eb5c3319ab013a4e19cf5caea572cdf6bc816858 [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);
ksrini2d05c7d2014-08-05 19:29:00 -0700101static void SetJvmEnvironment(int argc, char **argv);
mchungea290e22011-01-21 09:43:57 -0800102static jboolean ParseArguments(int *pargc, char ***pargv,
103 int *pmode, char **pwhat,
104 int *pret, const char *jrepath);
duke6e45e102007-12-01 00:00:00 +0000105static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
106 InvocationFunctions *ifn);
107static jstring NewPlatformString(JNIEnv *env, char *s);
mchungea290e22011-01-21 09:43:57 -0800108static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
ksrini6b41ee82012-11-19 19:49:38 -0800109static jclass GetApplicationClass(JNIEnv *env);
duke6e45e102007-12-01 00:00:00 +0000110
111static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
112static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
113static void SetApplicationClassPath(const char**);
114
115static void PrintJavaVersion(JNIEnv *env, jboolean extraLF);
116static void PrintUsage(JNIEnv* env, jboolean doXUsage);
ksrini8e20e1c2010-11-23 16:52:39 -0800117static void ShowSettings(JNIEnv* env, char *optString);
duke6e45e102007-12-01 00:00:00 +0000118
119static void SetPaths(int argc, char **argv);
120
121static void DumpState();
122static jboolean RemovableOption(char *option);
123
124/* Maximum supported entries from jvm.cfg. */
125#define INIT_MAX_KNOWN_VMS 10
126
127/* Values for vmdesc.flag */
128enum vmdesc_flag {
129 VM_UNKNOWN = -1,
130 VM_KNOWN,
131 VM_ALIASED_TO,
132 VM_WARN,
133 VM_ERROR,
134 VM_IF_SERVER_CLASS,
135 VM_IGNORE
136};
137
138struct vmdesc {
139 char *name;
140 int flag;
141 char *alias;
142 char *server_class;
143};
144static struct vmdesc *knownVMs = NULL;
145static int knownVMsCount = 0;
146static int knownVMsLimit = 0;
147
148static void GrowKnownVMs();
149static int KnownVMIndex(const char* name);
150static void FreeKnownVMs();
duke6e45e102007-12-01 00:00:00 +0000151static jboolean IsWildCardEnabled();
152
ksrini07e328d2013-05-07 13:15:28 -0700153#define ARG_CHECK(AC_arg_count, AC_failure_message, AC_questionable_arg) \
154 do { \
155 if (AC_arg_count < 1) { \
156 JLI_ReportErrorMessage(AC_failure_message, AC_questionable_arg); \
157 printUsage = JNI_TRUE; \
158 *pret = 1; \
159 return JNI_TRUE; \
160 } \
161 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000162
163/*
164 * Running Java code in primordial thread caused many problems. We will
165 * create a new thread to invoke JVM. See 6316197 for more information.
166 */
ksrinie97d4002012-07-31 06:14:28 -0700167static jlong threadStackSize = 0; /* stack size of the new thread */
ksrini6d575f82010-12-23 13:51:30 -0800168static jlong maxHeapSize = 0; /* max heap size */
169static jlong initialHeapSize = 0; /* inital heap size */
duke6e45e102007-12-01 00:00:00 +0000170
duke6e45e102007-12-01 00:00:00 +0000171/*
172 * Entry point.
173 */
174int
175JLI_Launch(int argc, char ** argv, /* main argc, argc */
176 int jargc, const char** jargv, /* java args */
177 int appclassc, const char** appclassv, /* app classpath */
178 const char* fullversion, /* full version defined */
179 const char* dotversion, /* dot version defined */
180 const char* pname, /* program name */
181 const char* lname, /* launcher name */
182 jboolean javaargs, /* JAVA_ARGS */
183 jboolean cpwildcard, /* classpath wildcard*/
184 jboolean javaw, /* windows-only javaw */
185 jint ergo /* ergonomics class policy */
186)
187{
mchungea290e22011-01-21 09:43:57 -0800188 int mode = LM_UNKNOWN;
189 char *what = NULL;
duke6e45e102007-12-01 00:00:00 +0000190 char *cpath = 0;
191 char *main_class = NULL;
192 int ret;
193 InvocationFunctions ifn;
194 jlong start, end;
ksrinie3ec45d2010-07-09 11:04:34 -0700195 char jvmpath[MAXPATHLEN];
196 char jrepath[MAXPATHLEN];
michaelm5ac8c152012-03-06 20:34:38 +0000197 char jvmcfg[MAXPATHLEN];
duke6e45e102007-12-01 00:00:00 +0000198
199 _fVersion = fullversion;
200 _dVersion = dotversion;
201 _launcher_name = lname;
202 _program_name = pname;
203 _is_java_args = javaargs;
204 _wc_enabled = cpwildcard;
205 _ergo_policy = ergo;
206
ksrini52cded22008-03-06 07:51:28 -0800207 InitLauncher(javaw);
duke6e45e102007-12-01 00:00:00 +0000208 DumpState();
ksrinie97d4002012-07-31 06:14:28 -0700209 if (JLI_IsTraceLauncher()) {
210 int i;
211 printf("Command line args:\n");
212 for (i = 0; i < argc ; i++) {
213 printf("argv[%d] = %s\n", i, argv[i]);
214 }
215 AddOption("-Dsun.java.launcher.diag=true", NULL);
216 }
duke6e45e102007-12-01 00:00:00 +0000217
218 /*
219 * Make sure the specified version of the JRE is running.
220 *
221 * There are three things to note about the SelectVersion() routine:
222 * 1) If the version running isn't correct, this routine doesn't
223 * return (either the correct version has been exec'd or an error
224 * was issued).
225 * 2) Argc and Argv in this scope are *not* altered by this routine.
226 * It is the responsibility of subsequent code to ignore the
227 * arguments handled by this routine.
228 * 3) As a side-effect, the variable "main_class" is guaranteed to
229 * be set (if it should ever be set). This isn't exactly the
230 * poster child for structured programming, but it is a small
231 * price to pay for not processing a jar file operand twice.
232 * (Note: This side effect has been disabled. See comment on
233 * bugid 5030265 below.)
234 */
235 SelectVersion(argc, argv, &main_class);
236
duke6e45e102007-12-01 00:00:00 +0000237 CreateExecutionEnvironment(&argc, &argv,
238 jrepath, sizeof(jrepath),
michaelm5ac8c152012-03-06 20:34:38 +0000239 jvmpath, sizeof(jvmpath),
240 jvmcfg, sizeof(jvmcfg));
duke6e45e102007-12-01 00:00:00 +0000241
ksrini2d05c7d2014-08-05 19:29:00 -0700242 if (!IsJavaArgs()) {
243 SetJvmEnvironment(argc,argv);
244 }
245
duke6e45e102007-12-01 00:00:00 +0000246 ifn.CreateJavaVM = 0;
247 ifn.GetDefaultJavaVMInitArgs = 0;
248
249 if (JLI_IsTraceLauncher()) {
250 start = CounterGet();
251 }
252
253 if (!LoadJavaVM(jvmpath, &ifn)) {
254 return(6);
255 }
256
257 if (JLI_IsTraceLauncher()) {
258 end = CounterGet();
259 }
260
261 JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n",
262 (long)(jint)Counter2Micros(end-start));
263
264 ++argv;
265 --argc;
266
267 if (IsJavaArgs()) {
268 /* Preprocess wrapper arguments */
269 TranslateApplicationArgs(jargc, jargv, &argc, &argv);
270 if (!AddApplicationOptions(appclassc, appclassv)) {
271 return(1);
272 }
273 } else {
274 /* Set default CLASSPATH */
275 cpath = getenv("CLASSPATH");
276 if (cpath == NULL) {
277 cpath = ".";
278 }
279 SetClassPath(cpath);
280 }
281
mchungea290e22011-01-21 09:43:57 -0800282 /* Parse command line options; if the return value of
283 * ParseArguments is false, the program should exit.
duke6e45e102007-12-01 00:00:00 +0000284 */
mchungea290e22011-01-21 09:43:57 -0800285 if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath))
286 {
duke6e45e102007-12-01 00:00:00 +0000287 return(ret);
288 }
289
290 /* Override class path if -jar flag was specified */
mchungea290e22011-01-21 09:43:57 -0800291 if (mode == LM_JAR) {
292 SetClassPath(what); /* Override class path */
duke6e45e102007-12-01 00:00:00 +0000293 }
294
295 /* set the -Dsun.java.command pseudo property */
mchungea290e22011-01-21 09:43:57 -0800296 SetJavaCommandLineProp(what, argc, argv);
duke6e45e102007-12-01 00:00:00 +0000297
298 /* Set the -Dsun.java.launcher pseudo property */
299 SetJavaLauncherProp();
300
301 /* set the -Dsun.java.launcher.* platform properties */
302 SetJavaLauncherPlatformProps();
303
michaelm5ac8c152012-03-06 20:34:38 +0000304 return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
duke6e45e102007-12-01 00:00:00 +0000305}
ksrinie3ec45d2010-07-09 11:04:34 -0700306/*
307 * Always detach the main thread so that it appears to have ended when
308 * the application's main method exits. This will invoke the
309 * uncaught exception handler machinery if main threw an
310 * exception. An uncaught exception handler cannot change the
311 * launcher's return code except by calling System.exit.
312 *
313 * Wait for all non-daemon threads to end, then destroy the VM.
314 * This will actually create a trivial new Java waiter thread
315 * named "DestroyJavaVM", but this will be seen as a different
316 * thread from the one that executed main, even though they are
317 * the same C thread. This allows mainThread.join() and
318 * mainThread.isAlive() to work as expected.
319 */
320#define LEAVE() \
ksrini07e328d2013-05-07 13:15:28 -0700321 do { \
322 if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \
323 JLI_ReportErrorMessage(JVM_ERROR2); \
324 ret = 1; \
325 } \
326 if (JNI_TRUE) { \
327 (*vm)->DestroyJavaVM(vm); \
328 return ret; \
329 } \
330 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000331
ksrini07e328d2013-05-07 13:15:28 -0700332#define CHECK_EXCEPTION_NULL_LEAVE(CENL_exception) \
333 do { \
334 if ((*env)->ExceptionOccurred(env)) { \
335 JLI_ReportExceptionDescription(env); \
336 LEAVE(); \
337 } \
338 if ((CENL_exception) == NULL) { \
339 JLI_ReportErrorMessage(JNI_ERROR); \
340 LEAVE(); \
341 } \
342 } while (JNI_FALSE)
ksrini20a64b22008-09-24 15:07:41 -0700343
ksrini07e328d2013-05-07 13:15:28 -0700344#define CHECK_EXCEPTION_LEAVE(CEL_return_value) \
345 do { \
346 if ((*env)->ExceptionOccurred(env)) { \
347 JLI_ReportExceptionDescription(env); \
348 ret = (CEL_return_value); \
349 LEAVE(); \
350 } \
351 } while (JNI_FALSE)
duke6e45e102007-12-01 00:00:00 +0000352
353int JNICALL
354JavaMain(void * _args)
355{
356 JavaMainArgs *args = (JavaMainArgs *)_args;
357 int argc = args->argc;
358 char **argv = args->argv;
mchungea290e22011-01-21 09:43:57 -0800359 int mode = args->mode;
360 char *what = args->what;
duke6e45e102007-12-01 00:00:00 +0000361 InvocationFunctions ifn = args->ifn;
362
363 JavaVM *vm = 0;
364 JNIEnv *env = 0;
mchungea290e22011-01-21 09:43:57 -0800365 jclass mainClass = NULL;
ksrini6b41ee82012-11-19 19:49:38 -0800366 jclass appClass = NULL; // actual application class being launched
duke6e45e102007-12-01 00:00:00 +0000367 jmethodID mainID;
368 jobjectArray mainArgs;
369 int ret = 0;
370 jlong start, end;
371
michaelm5ac8c152012-03-06 20:34:38 +0000372 RegisterThread();
373
duke6e45e102007-12-01 00:00:00 +0000374 /* Initialize the virtual machine */
duke6e45e102007-12-01 00:00:00 +0000375 start = CounterGet();
376 if (!InitializeJVM(&vm, &env, &ifn)) {
ksrini0e817162008-08-26 10:21:20 -0700377 JLI_ReportErrorMessage(JVM_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000378 exit(1);
379 }
380
ksrini05fdd5a2012-01-03 08:27:37 -0800381 if (showSettings != NULL) {
382 ShowSettings(env, showSettings);
383 CHECK_EXCEPTION_LEAVE(1);
384 }
385
duke6e45e102007-12-01 00:00:00 +0000386 if (printVersion || showVersion) {
387 PrintJavaVersion(env, showVersion);
ksrini20a64b22008-09-24 15:07:41 -0700388 CHECK_EXCEPTION_LEAVE(0);
duke6e45e102007-12-01 00:00:00 +0000389 if (printVersion) {
ksrinie3ec45d2010-07-09 11:04:34 -0700390 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000391 }
392 }
393
394 /* If the user specified neither a class name nor a JAR file */
mchungea290e22011-01-21 09:43:57 -0800395 if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
duke6e45e102007-12-01 00:00:00 +0000396 PrintUsage(env, printXUsage);
ksrini20a64b22008-09-24 15:07:41 -0700397 CHECK_EXCEPTION_LEAVE(1);
ksrinie3ec45d2010-07-09 11:04:34 -0700398 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000399 }
400
401 FreeKnownVMs(); /* after last possible PrintUsage() */
402
403 if (JLI_IsTraceLauncher()) {
404 end = CounterGet();
405 JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n",
406 (long)(jint)Counter2Micros(end-start));
407 }
408
mchungea290e22011-01-21 09:43:57 -0800409 /* At this stage, argc/argv have the application's arguments */
duke6e45e102007-12-01 00:00:00 +0000410 if (JLI_IsTraceLauncher()){
411 int i;
mchungea290e22011-01-21 09:43:57 -0800412 printf("%s is '%s'\n", launchModeNames[mode], what);
413 printf("App's argc is %d\n", argc);
duke6e45e102007-12-01 00:00:00 +0000414 for (i=0; i < argc; i++) {
415 printf(" argv[%2d] = '%s'\n", i, argv[i]);
416 }
417 }
418
419 ret = 1;
420
421 /*
422 * Get the application's main class.
423 *
424 * See bugid 5030265. The Main-Class name has already been parsed
425 * from the manifest, but not parsed properly for UTF-8 support.
426 * Hence the code here ignores the value previously extracted and
427 * uses the pre-existing code to reextract the value. This is
428 * possibly an end of release cycle expedient. However, it has
429 * also been discovered that passing some character sets through
430 * the environment has "strange" behavior on some variants of
431 * Windows. Hence, maybe the manifest parsing code local to the
432 * launcher should never be enhanced.
433 *
434 * Hence, future work should either:
435 * 1) Correct the local parsing code and verify that the
436 * Main-Class attribute gets properly passed through
437 * all environments,
438 * 2) Remove the vestages of maintaining main_class through
439 * the environment (and remove these comments).
ksrini6b41ee82012-11-19 19:49:38 -0800440 *
441 * This method also correctly handles launching existing JavaFX
442 * applications that may or may not have a Main-Class manifest entry.
duke6e45e102007-12-01 00:00:00 +0000443 */
mchungea290e22011-01-21 09:43:57 -0800444 mainClass = LoadMainClass(env, mode, what);
ksrini20a64b22008-09-24 15:07:41 -0700445 CHECK_EXCEPTION_NULL_LEAVE(mainClass);
ksrini6b41ee82012-11-19 19:49:38 -0800446 /*
447 * In some cases when launching an application that needs a helper, e.g., a
448 * JavaFX application with no main method, the mainClass will not be the
449 * applications own main class but rather a helper class. To keep things
450 * consistent in the UI we need to track and report the application main class.
451 */
452 appClass = GetApplicationClass(env);
ksrini07e328d2013-05-07 13:15:28 -0700453 NULL_CHECK_RETURN_VALUE(appClass, -1);
ksrini6b41ee82012-11-19 19:49:38 -0800454 /*
455 * PostJVMInit uses the class name as the application name for GUI purposes,
456 * for example, on OSX this sets the application name in the menu bar for
457 * both SWT and JavaFX. So we'll pass the actual application class here
458 * instead of mainClass as that may be a launcher or helper class instead
459 * of the application class.
460 */
461 PostJVMInit(env, appClass, vm);
ksrini20a64b22008-09-24 15:07:41 -0700462 /*
463 * The LoadMainClass not only loads the main class, it will also ensure
464 * that the main method's signature is correct, therefore further checking
465 * is not required. The main method is invoked here so that extraneous java
466 * stacks are not in the application stack trace.
467 */
duke6e45e102007-12-01 00:00:00 +0000468 mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
469 "([Ljava/lang/String;)V");
ksrini20a64b22008-09-24 15:07:41 -0700470 CHECK_EXCEPTION_NULL_LEAVE(mainID);
duke6e45e102007-12-01 00:00:00 +0000471
ksrinie97d4002012-07-31 06:14:28 -0700472 /* Build platform specific argument array */
473 mainArgs = CreateApplicationArgs(env, argv, argc);
ksrini20a64b22008-09-24 15:07:41 -0700474 CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
duke6e45e102007-12-01 00:00:00 +0000475
476 /* Invoke main method. */
477 (*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
478
479 /*
480 * The launcher's exit code (in the absence of calls to
481 * System.exit) will be non-zero if main threw an exception.
482 */
483 ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
ksrinie3ec45d2010-07-09 11:04:34 -0700484 LEAVE();
duke6e45e102007-12-01 00:00:00 +0000485}
486
duke6e45e102007-12-01 00:00:00 +0000487/*
488 * Checks the command line options to find which JVM type was
489 * specified. If no command line option was given for the JVM type,
490 * the default type is used. The environment variable
491 * JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
492 * checked as ways of specifying which JVM type to invoke.
493 */
494char *
495CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
496 int i, argi;
497 int argc;
498 char **newArgv;
499 int newArgvIdx = 0;
500 int isVMType;
501 int jvmidx = -1;
502 char *jvmtype = getenv("JDK_ALTERNATE_VM");
503
504 argc = *pargc;
505
506 /* To make things simpler we always copy the argv array */
507 newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
508
509 /* The program name is always present */
510 newArgv[newArgvIdx++] = (*argv)[0];
511
512 for (argi = 1; argi < argc; argi++) {
513 char *arg = (*argv)[argi];
514 isVMType = 0;
515
516 if (IsJavaArgs()) {
517 if (arg[0] != '-') {
518 newArgv[newArgvIdx++] = arg;
519 continue;
520 }
521 } else {
522 if (JLI_StrCmp(arg, "-classpath") == 0 ||
523 JLI_StrCmp(arg, "-cp") == 0) {
524 newArgv[newArgvIdx++] = arg;
525 argi++;
526 if (argi < argc) {
527 newArgv[newArgvIdx++] = (*argv)[argi];
528 }
529 continue;
530 }
531 if (arg[0] != '-') break;
532 }
533
534 /* Did the user pass an explicit VM type? */
535 i = KnownVMIndex(arg);
536 if (i >= 0) {
537 jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
538 isVMType = 1;
539 *pargc = *pargc - 1;
540 }
541
542 /* Did the user specify an "alternate" VM? */
543 else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
544 isVMType = 1;
545 jvmtype = arg+((arg[1]=='X')? 10 : 12);
546 jvmidx = -1;
547 }
548
549 if (!isVMType) {
550 newArgv[newArgvIdx++] = arg;
551 }
552 }
553
554 /*
555 * Finish copying the arguments if we aborted the above loop.
556 * NOTE that if we aborted via "break" then we did NOT copy the
557 * last argument above, and in addition argi will be less than
558 * argc.
559 */
560 while (argi < argc) {
561 newArgv[newArgvIdx++] = (*argv)[argi];
562 argi++;
563 }
564
565 /* argv is null-terminated */
566 newArgv[newArgvIdx] = 0;
567
568 /* Copy back argv */
569 *argv = newArgv;
570 *pargc = newArgvIdx;
571
572 /* use the default VM type if not specified (no alias processing) */
573 if (jvmtype == NULL) {
574 char* result = knownVMs[0].name+1;
575 /* Use a different VM type if we are on a server class machine? */
576 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) &&
577 (ServerClassMachine() == JNI_TRUE)) {
578 result = knownVMs[0].server_class+1;
579 }
580 JLI_TraceLauncher("Default VM: %s\n", result);
581 return result;
582 }
583
584 /* if using an alternate VM, no alias processing */
585 if (jvmidx < 0)
586 return jvmtype;
587
588 /* Resolve aliases first */
589 {
590 int loopCount = 0;
591 while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
592 int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
593
594 if (loopCount > knownVMsCount) {
595 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700596 JLI_ReportErrorMessage(CFG_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000597 exit(1);
598 } else {
599 return "ERROR";
600 /* break; */
601 }
602 }
603
604 if (nextIdx < 0) {
605 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700606 JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
duke6e45e102007-12-01 00:00:00 +0000607 exit(1);
608 } else {
609 return "ERROR";
610 }
611 }
612 jvmidx = nextIdx;
613 jvmtype = knownVMs[jvmidx].name+1;
614 loopCount++;
615 }
616 }
617
618 switch (knownVMs[jvmidx].flag) {
619 case VM_WARN:
620 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700621 JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
duke6e45e102007-12-01 00:00:00 +0000622 }
623 /* fall through */
624 case VM_IGNORE:
625 jvmtype = knownVMs[jvmidx=0].name + 1;
626 /* fall through */
627 case VM_KNOWN:
628 break;
629 case VM_ERROR:
630 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -0700631 JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
duke6e45e102007-12-01 00:00:00 +0000632 exit(1);
633 } else {
634 return "ERROR";
635 }
636 }
637
638 return jvmtype;
639}
640
ksrini2d05c7d2014-08-05 19:29:00 -0700641/*
642 * static void SetJvmEnvironment(int argc, char **argv);
643 * Is called just before the JVM is loaded. We can set env variables
644 * that are consumed by the JVM. This function is non-destructive,
645 * leaving the arg list intact. The first use is for the JVM flag
646 * -XX:NativeMemoryTracking=value.
647 */
648static void
649SetJvmEnvironment(int argc, char **argv) {
650
651 static const char* NMT_Env_Name = "NMT_LEVEL_";
652
653 int i;
654 for (i = 0; i < argc; i++) {
655 /*
656 * The following case checks for "-XX:NativeMemoryTracking=value".
657 * If value is non null, an environmental variable set to this value
658 * will be created to be used by the JVM.
659 * The argument is passed to the JVM, which will check validity.
660 * The JVM is responsible for removing the env variable.
661 */
662 char *arg = argv[i];
663 if (JLI_StrCCmp(arg, "-XX:NativeMemoryTracking=") == 0) {
664 int retval;
665 // get what follows this parameter, include "="
666 size_t pnlen = JLI_StrLen("-XX:NativeMemoryTracking=");
667 if (JLI_StrLen(arg) > pnlen) {
668 char* value = arg + pnlen;
669 size_t pbuflen = pnlen + JLI_StrLen(value) + 10; // 10 max pid digits
670
671 /*
672 * ensures that malloc successful
673 * DONT JLI_MemFree() pbuf. JLI_PutEnv() uses system call
674 * that could store the address.
675 */
676 char * pbuf = (char*)JLI_MemAlloc(pbuflen);
677
678 JLI_Snprintf(pbuf, pbuflen, "%s%d=%s", NMT_Env_Name, JLI_GetPid(), value);
679 retval = JLI_PutEnv(pbuf);
680 if (JLI_IsTraceLauncher()) {
681 char* envName;
682 char* envBuf;
683
684 // ensures that malloc successful
685 envName = (char*)JLI_MemAlloc(pbuflen);
686 JLI_Snprintf(envName, pbuflen, "%s%d", NMT_Env_Name, JLI_GetPid());
687
688 printf("TRACER_MARKER: NativeMemoryTracking: env var is %s\n",envName);
689 printf("TRACER_MARKER: NativeMemoryTracking: putenv arg %s\n",pbuf);
690 envBuf = getenv(envName);
691 printf("TRACER_MARKER: NativeMemoryTracking: got value %s\n",envBuf);
692 free(envName);
693 }
694
695 }
696
697 }
698
699 }
700}
701
duke6e45e102007-12-01 00:00:00 +0000702/* copied from HotSpot function "atomll()" */
703static int
ksrini8e20e1c2010-11-23 16:52:39 -0800704parse_size(const char *s, jlong *result) {
duke6e45e102007-12-01 00:00:00 +0000705 jlong n = 0;
706 int args_read = sscanf(s, jlong_format_specifier(), &n);
707 if (args_read != 1) {
708 return 0;
709 }
710 while (*s != '\0' && *s >= '0' && *s <= '9') {
711 s++;
712 }
713 // 4705540: illegal if more characters are found after the first non-digit
714 if (JLI_StrLen(s) > 1) {
715 return 0;
716 }
717 switch (*s) {
718 case 'T': case 't':
719 *result = n * GB * KB;
720 return 1;
721 case 'G': case 'g':
722 *result = n * GB;
723 return 1;
724 case 'M': case 'm':
725 *result = n * MB;
726 return 1;
727 case 'K': case 'k':
728 *result = n * KB;
729 return 1;
730 case '\0':
731 *result = n;
732 return 1;
733 default:
734 /* Create JVM with default stack and let VM handle malformed -Xss string*/
735 return 0;
736 }
737}
738
739/*
740 * Adds a new VM option with the given given name and value.
741 */
742void
743AddOption(char *str, void *info)
744{
745 /*
746 * Expand options array if needed to accommodate at least one more
747 * VM option.
748 */
749 if (numOptions >= maxOptions) {
750 if (options == 0) {
751 maxOptions = 4;
752 options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
753 } else {
754 JavaVMOption *tmp;
755 maxOptions *= 2;
756 tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
757 memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
758 JLI_MemFree(options);
759 options = tmp;
760 }
761 }
762 options[numOptions].optionString = str;
763 options[numOptions++].extraInfo = info;
764
765 if (JLI_StrCCmp(str, "-Xss") == 0) {
ksrini8e20e1c2010-11-23 16:52:39 -0800766 jlong tmp;
767 if (parse_size(str + 4, &tmp)) {
768 threadStackSize = tmp;
769 }
770 }
771
772 if (JLI_StrCCmp(str, "-Xmx") == 0) {
773 jlong tmp;
774 if (parse_size(str + 4, &tmp)) {
ksrini6d575f82010-12-23 13:51:30 -0800775 maxHeapSize = tmp;
776 }
777 }
778
779 if (JLI_StrCCmp(str, "-Xms") == 0) {
780 jlong tmp;
781 if (parse_size(str + 4, &tmp)) {
mchungea290e22011-01-21 09:43:57 -0800782 initialHeapSize = tmp;
ksrini8e20e1c2010-11-23 16:52:39 -0800783 }
duke6e45e102007-12-01 00:00:00 +0000784 }
785}
786
787static void
788SetClassPath(const char *s)
789{
790 char *def;
martinc0ca3352009-06-22 16:41:27 -0700791 const char *orig = s;
792 static const char format[] = "-Djava.class.path=%s";
ksrinie1a34382012-04-24 10:37:01 -0700793 /*
794 * usually we should not get a null pointer, but there are cases where
795 * we might just get one, in which case we simply ignore it, and let the
796 * caller deal with it
797 */
798 if (s == NULL)
799 return;
duke6e45e102007-12-01 00:00:00 +0000800 s = JLI_WildcardExpandClasspath(s);
kizune0fc19542014-05-23 20:46:44 +0400801 if (sizeof(format) - 2 + JLI_StrLen(s) < JLI_StrLen(s))
802 // s is corrupted after wildcard expansion
803 return;
martinc0ca3352009-06-22 16:41:27 -0700804 def = JLI_MemAlloc(sizeof(format)
805 - 2 /* strlen("%s") */
806 + JLI_StrLen(s));
807 sprintf(def, format, s);
duke6e45e102007-12-01 00:00:00 +0000808 AddOption(def, NULL);
martinc0ca3352009-06-22 16:41:27 -0700809 if (s != orig)
810 JLI_MemFree((char *) s);
duke6e45e102007-12-01 00:00:00 +0000811}
812
813/*
814 * The SelectVersion() routine ensures that an appropriate version of
815 * the JRE is running. The specification for the appropriate version
816 * is obtained from either the manifest of a jar file (preferred) or
817 * from command line options.
818 * The routine also parses splash screen command line options and
819 * passes on their values in private environment variables.
820 */
821static void
822SelectVersion(int argc, char **argv, char **main_class)
823{
824 char *arg;
825 char **new_argv;
826 char **new_argp;
827 char *operand;
828 char *version = NULL;
829 char *jre = NULL;
830 int jarflag = 0;
831 int headlessflag = 0;
832 int restrict_search = -1; /* -1 implies not known */
833 manifest_info info;
834 char env_entry[MAXNAMELEN + 24] = ENV_ENTRY "=";
835 char *splash_file_name = NULL;
836 char *splash_jar_name = NULL;
837 char *env_in;
838 int res;
839
840 /*
841 * If the version has already been selected, set *main_class
842 * with the value passed through the environment (if any) and
843 * simply return.
844 */
845 if ((env_in = getenv(ENV_ENTRY)) != NULL) {
846 if (*env_in != '\0')
847 *main_class = JLI_StringDup(env_in);
848 return;
849 }
850
851 /*
852 * Scan through the arguments for options relevant to multiple JRE
853 * support. For reference, the command line syntax is defined as:
854 *
855 * SYNOPSIS
856 * java [options] class [argument...]
857 *
858 * java [options] -jar file.jar [argument...]
859 *
860 * As the scan is performed, make a copy of the argument list with
861 * the version specification options (new to 1.5) removed, so that
862 * a version less than 1.5 can be exec'd.
863 *
864 * Note that due to the syntax of the native Windows interface
865 * CreateProcess(), processing similar to the following exists in
866 * the Windows platform specific routine ExecJRE (in java_md.c).
867 * Changes here should be reproduced there.
868 */
869 new_argv = JLI_MemAlloc((argc + 1) * sizeof(char*));
870 new_argv[0] = argv[0];
871 new_argp = &new_argv[1];
872 argc--;
873 argv++;
874 while ((arg = *argv) != 0 && *arg == '-') {
875 if (JLI_StrCCmp(arg, "-version:") == 0) {
876 version = arg + 9;
877 } else if (JLI_StrCmp(arg, "-jre-restrict-search") == 0) {
878 restrict_search = 1;
879 } else if (JLI_StrCmp(arg, "-no-jre-restrict-search") == 0) {
880 restrict_search = 0;
881 } else {
882 if (JLI_StrCmp(arg, "-jar") == 0)
883 jarflag = 1;
884 /* deal with "unfortunate" classpath syntax */
885 if ((JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) &&
886 (argc >= 2)) {
887 *new_argp++ = arg;
888 argc--;
889 argv++;
890 arg = *argv;
891 }
892
893 /*
894 * Checking for headless toolkit option in the some way as AWT does:
895 * "true" means true and any other value means false
896 */
897 if (JLI_StrCmp(arg, "-Djava.awt.headless=true") == 0) {
898 headlessflag = 1;
899 } else if (JLI_StrCCmp(arg, "-Djava.awt.headless=") == 0) {
900 headlessflag = 0;
901 } else if (JLI_StrCCmp(arg, "-splash:") == 0) {
902 splash_file_name = arg+8;
903 }
904 *new_argp++ = arg;
905 }
906 argc--;
907 argv++;
908 }
909 if (argc <= 0) { /* No operand? Possibly legit with -[full]version */
910 operand = NULL;
911 } else {
912 argc--;
913 *new_argp++ = operand = *argv++;
914 }
915 while (argc-- > 0) /* Copy over [argument...] */
916 *new_argp++ = *argv++;
917 *new_argp = NULL;
918
919 /*
920 * If there is a jar file, read the manifest. If the jarfile can't be
921 * read, the manifest can't be read from the jar file, or the manifest
922 * is corrupt, issue the appropriate error messages and exit.
923 *
924 * Even if there isn't a jar file, construct a manifest_info structure
925 * containing the command line information. It's a convenient way to carry
926 * this data around.
927 */
928 if (jarflag && operand) {
929 if ((res = JLI_ParseManifest(operand, &info)) != 0) {
930 if (res == -1)
ksrini0e817162008-08-26 10:21:20 -0700931 JLI_ReportErrorMessage(JAR_ERROR2, operand);
duke6e45e102007-12-01 00:00:00 +0000932 else
ksrini0e817162008-08-26 10:21:20 -0700933 JLI_ReportErrorMessage(JAR_ERROR3, operand);
duke6e45e102007-12-01 00:00:00 +0000934 exit(1);
935 }
936
937 /*
938 * Command line splash screen option should have precedence
939 * over the manifest, so the manifest data is used only if
940 * splash_file_name has not been initialized above during command
941 * line parsing
942 */
943 if (!headlessflag && !splash_file_name && info.splashscreen_image_file_name) {
944 splash_file_name = info.splashscreen_image_file_name;
945 splash_jar_name = operand;
946 }
947 } else {
948 info.manifest_version = NULL;
949 info.main_class = NULL;
950 info.jre_version = NULL;
951 info.jre_restrict_search = 0;
952 }
953
954 /*
955 * Passing on splash screen info in environment variables
956 */
957 if (splash_file_name && !headlessflag) {
958 char* splash_file_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_FILE_ENV_ENTRY "=")+JLI_StrLen(splash_file_name)+1);
959 JLI_StrCpy(splash_file_entry, SPLASH_FILE_ENV_ENTRY "=");
960 JLI_StrCat(splash_file_entry, splash_file_name);
961 putenv(splash_file_entry);
962 }
963 if (splash_jar_name && !headlessflag) {
964 char* splash_jar_entry = JLI_MemAlloc(JLI_StrLen(SPLASH_JAR_ENV_ENTRY "=")+JLI_StrLen(splash_jar_name)+1);
965 JLI_StrCpy(splash_jar_entry, SPLASH_JAR_ENV_ENTRY "=");
966 JLI_StrCat(splash_jar_entry, splash_jar_name);
967 putenv(splash_jar_entry);
968 }
969
970 /*
971 * The JRE-Version and JRE-Restrict-Search values (if any) from the
972 * manifest are overwritten by any specified on the command line.
973 */
974 if (version != NULL)
975 info.jre_version = version;
976 if (restrict_search != -1)
977 info.jre_restrict_search = restrict_search;
978
979 /*
980 * "Valid" returns (other than unrecoverable errors) follow. Set
981 * main_class as a side-effect of this routine.
982 */
983 if (info.main_class != NULL)
984 *main_class = JLI_StringDup(info.main_class);
985
986 /*
987 * If no version selection information is found either on the command
988 * line or in the manifest, simply return.
989 */
990 if (info.jre_version == NULL) {
991 JLI_FreeManifest();
992 JLI_MemFree(new_argv);
993 return;
994 }
995
996 /*
997 * Check for correct syntax of the version specification (JSR 56).
998 */
999 if (!JLI_ValidVersionString(info.jre_version)) {
ksrini0e817162008-08-26 10:21:20 -07001000 JLI_ReportErrorMessage(SPC_ERROR1, info.jre_version);
duke6e45e102007-12-01 00:00:00 +00001001 exit(1);
1002 }
1003
1004 /*
1005 * Find the appropriate JVM on the system. Just to be as forgiving as
1006 * possible, if the standard algorithms don't locate an appropriate
1007 * jre, check to see if the one running will satisfy the requirements.
1008 * This can happen on systems which haven't been set-up for multiple
1009 * JRE support.
1010 */
1011 jre = LocateJRE(&info);
1012 JLI_TraceLauncher("JRE-Version = %s, JRE-Restrict-Search = %s Selected = %s\n",
1013 (info.jre_version?info.jre_version:"null"),
1014 (info.jre_restrict_search?"true":"false"), (jre?jre:"null"));
1015
1016 if (jre == NULL) {
1017 if (JLI_AcceptableRelease(GetFullVersion(), info.jre_version)) {
1018 JLI_FreeManifest();
1019 JLI_MemFree(new_argv);
1020 return;
1021 } else {
ksrini0e817162008-08-26 10:21:20 -07001022 JLI_ReportErrorMessage(CFG_ERROR4, info.jre_version);
duke6e45e102007-12-01 00:00:00 +00001023 exit(1);
1024 }
1025 }
1026
1027 /*
1028 * If I'm not the chosen one, exec the chosen one. Returning from
1029 * ExecJRE indicates that I am indeed the chosen one.
1030 *
1031 * The private environment variable _JAVA_VERSION_SET is used to
1032 * prevent the chosen one from re-reading the manifest file and
1033 * using the values found within to override the (potential) command
1034 * line flags stripped from argv (because the target may not
1035 * understand them). Passing the MainClass value is an optimization
1036 * to avoid locating, expanding and parsing the manifest extra
1037 * times.
1038 */
ksrini8760ca92008-09-04 09:43:32 -07001039 if (info.main_class != NULL) {
1040 if (JLI_StrLen(info.main_class) <= MAXNAMELEN) {
1041 (void)JLI_StrCat(env_entry, info.main_class);
1042 } else {
asaha80ccd422009-04-16 21:08:04 -07001043 JLI_ReportErrorMessage(CLS_ERROR5, MAXNAMELEN);
ksrini8760ca92008-09-04 09:43:32 -07001044 exit(1);
1045 }
1046 }
duke6e45e102007-12-01 00:00:00 +00001047 (void)putenv(env_entry);
1048 ExecJRE(jre, new_argv);
1049 JLI_FreeManifest();
1050 JLI_MemFree(new_argv);
1051 return;
1052}
1053
1054/*
1055 * Parses command line arguments. Returns JNI_FALSE if launcher
1056 * should exit without starting vm, returns JNI_TRUE if vm needs
mchungea290e22011-01-21 09:43:57 -08001057 * to be started to process given options. *pret (the launcher
duke6e45e102007-12-01 00:00:00 +00001058 * process return value) is set to 0 for a normal exit.
1059 */
1060static jboolean
mchungea290e22011-01-21 09:43:57 -08001061ParseArguments(int *pargc, char ***pargv,
1062 int *pmode, char **pwhat,
1063 int *pret, const char *jrepath)
duke6e45e102007-12-01 00:00:00 +00001064{
1065 int argc = *pargc;
1066 char **argv = *pargv;
mchungea290e22011-01-21 09:43:57 -08001067 int mode = LM_UNKNOWN;
duke6e45e102007-12-01 00:00:00 +00001068 char *arg;
1069
1070 *pret = 0;
1071
1072 while ((arg = *argv) != 0 && *arg == '-') {
1073 argv++; --argc;
1074 if (JLI_StrCmp(arg, "-classpath") == 0 || JLI_StrCmp(arg, "-cp") == 0) {
1075 ARG_CHECK (argc, ARG_ERROR1, arg);
1076 SetClassPath(*argv);
mchungea290e22011-01-21 09:43:57 -08001077 mode = LM_CLASS;
duke6e45e102007-12-01 00:00:00 +00001078 argv++; --argc;
1079 } else if (JLI_StrCmp(arg, "-jar") == 0) {
1080 ARG_CHECK (argc, ARG_ERROR2, arg);
mchungea290e22011-01-21 09:43:57 -08001081 mode = LM_JAR;
duke6e45e102007-12-01 00:00:00 +00001082 } else if (JLI_StrCmp(arg, "-help") == 0 ||
1083 JLI_StrCmp(arg, "-h") == 0 ||
1084 JLI_StrCmp(arg, "-?") == 0) {
1085 printUsage = JNI_TRUE;
1086 return JNI_TRUE;
1087 } else if (JLI_StrCmp(arg, "-version") == 0) {
1088 printVersion = JNI_TRUE;
1089 return JNI_TRUE;
1090 } else if (JLI_StrCmp(arg, "-showversion") == 0) {
1091 showVersion = JNI_TRUE;
1092 } else if (JLI_StrCmp(arg, "-X") == 0) {
1093 printXUsage = JNI_TRUE;
1094 return JNI_TRUE;
1095/*
ksrini8e20e1c2010-11-23 16:52:39 -08001096 * The following case checks for -XshowSettings OR -XshowSetting:SUBOPT.
1097 * In the latter case, any SUBOPT value not recognized will default to "all"
1098 */
1099 } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
1100 JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
1101 showSettings = arg;
ksrini9636e2e2011-02-03 15:41:23 -08001102 } else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
1103 AddOption("-Dsun.java.launcher.diag=true", NULL);
ksrini8e20e1c2010-11-23 16:52:39 -08001104/*
duke6e45e102007-12-01 00:00:00 +00001105 * The following case provide backward compatibility with old-style
1106 * command line options.
1107 */
1108 } else if (JLI_StrCmp(arg, "-fullversion") == 0) {
ksrini0e817162008-08-26 10:21:20 -07001109 JLI_ReportMessage("%s full version \"%s\"", _launcher_name, GetFullVersion());
duke6e45e102007-12-01 00:00:00 +00001110 return JNI_FALSE;
1111 } else if (JLI_StrCmp(arg, "-verbosegc") == 0) {
1112 AddOption("-verbose:gc", NULL);
1113 } else if (JLI_StrCmp(arg, "-t") == 0) {
1114 AddOption("-Xt", NULL);
1115 } else if (JLI_StrCmp(arg, "-tm") == 0) {
1116 AddOption("-Xtm", NULL);
1117 } else if (JLI_StrCmp(arg, "-debug") == 0) {
1118 AddOption("-Xdebug", NULL);
1119 } else if (JLI_StrCmp(arg, "-noclassgc") == 0) {
1120 AddOption("-Xnoclassgc", NULL);
1121 } else if (JLI_StrCmp(arg, "-Xfuture") == 0) {
1122 AddOption("-Xverify:all", NULL);
1123 } else if (JLI_StrCmp(arg, "-verify") == 0) {
1124 AddOption("-Xverify:all", NULL);
1125 } else if (JLI_StrCmp(arg, "-verifyremote") == 0) {
1126 AddOption("-Xverify:remote", NULL);
1127 } else if (JLI_StrCmp(arg, "-noverify") == 0) {
1128 AddOption("-Xverify:none", NULL);
1129 } else if (JLI_StrCCmp(arg, "-prof") == 0) {
1130 char *p = arg + 5;
1131 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 50);
1132 if (*p) {
1133 sprintf(tmp, "-Xrunhprof:cpu=old,file=%s", p + 1);
1134 } else {
1135 sprintf(tmp, "-Xrunhprof:cpu=old,file=java.prof");
1136 }
1137 AddOption(tmp, NULL);
1138 } else if (JLI_StrCCmp(arg, "-ss") == 0 ||
1139 JLI_StrCCmp(arg, "-oss") == 0 ||
1140 JLI_StrCCmp(arg, "-ms") == 0 ||
1141 JLI_StrCCmp(arg, "-mx") == 0) {
1142 char *tmp = JLI_MemAlloc(JLI_StrLen(arg) + 6);
1143 sprintf(tmp, "-X%s", arg + 1); /* skip '-' */
1144 AddOption(tmp, NULL);
1145 } else if (JLI_StrCmp(arg, "-checksource") == 0 ||
1146 JLI_StrCmp(arg, "-cs") == 0 ||
1147 JLI_StrCmp(arg, "-noasyncgc") == 0) {
1148 /* No longer supported */
ksrini0e817162008-08-26 10:21:20 -07001149 JLI_ReportErrorMessage(ARG_WARN, arg);
duke6e45e102007-12-01 00:00:00 +00001150 } else if (JLI_StrCCmp(arg, "-version:") == 0 ||
1151 JLI_StrCmp(arg, "-no-jre-restrict-search") == 0 ||
1152 JLI_StrCmp(arg, "-jre-restrict-search") == 0 ||
1153 JLI_StrCCmp(arg, "-splash:") == 0) {
1154 ; /* Ignore machine independent options already handled */
michaelm5ac8c152012-03-06 20:34:38 +00001155 } else if (ProcessPlatformOption(arg)) {
1156 ; /* Processing of platform dependent options */
1157 } else if (RemovableOption(arg)) {
duke6e45e102007-12-01 00:00:00 +00001158 ; /* Do not pass option to vm. */
1159 } else {
1160 AddOption(arg, NULL);
1161 }
1162 }
1163
1164 if (--argc >= 0) {
mchungea290e22011-01-21 09:43:57 -08001165 *pwhat = *argv++;
1166 }
1167
1168 if (*pwhat == NULL) {
1169 *pret = 1;
1170 } else if (mode == LM_UNKNOWN) {
1171 /* default to LM_CLASS if -jar and -cp option are
1172 * not specified */
1173 mode = LM_CLASS;
1174 }
1175
1176 if (argc >= 0) {
duke6e45e102007-12-01 00:00:00 +00001177 *pargc = argc;
1178 *pargv = argv;
1179 }
mchungea290e22011-01-21 09:43:57 -08001180
1181 *pmode = mode;
1182
duke6e45e102007-12-01 00:00:00 +00001183 return JNI_TRUE;
1184}
1185
1186/*
1187 * Initializes the Java Virtual Machine. Also frees options array when
1188 * finished.
1189 */
1190static jboolean
1191InitializeJVM(JavaVM **pvm, JNIEnv **penv, InvocationFunctions *ifn)
1192{
1193 JavaVMInitArgs args;
1194 jint r;
1195
1196 memset(&args, 0, sizeof(args));
1197 args.version = JNI_VERSION_1_2;
1198 args.nOptions = numOptions;
1199 args.options = options;
1200 args.ignoreUnrecognized = JNI_FALSE;
1201
1202 if (JLI_IsTraceLauncher()) {
1203 int i = 0;
1204 printf("JavaVM args:\n ");
1205 printf("version 0x%08lx, ", (long)args.version);
1206 printf("ignoreUnrecognized is %s, ",
1207 args.ignoreUnrecognized ? "JNI_TRUE" : "JNI_FALSE");
1208 printf("nOptions is %ld\n", (long)args.nOptions);
1209 for (i = 0; i < numOptions; i++)
1210 printf(" option[%2d] = '%s'\n",
1211 i, args.options[i].optionString);
1212 }
1213
1214 r = ifn->CreateJavaVM(pvm, (void **)penv, &args);
1215 JLI_MemFree(options);
1216 return r == JNI_OK;
1217}
1218
ksrini7d9872e2011-03-20 08:41:33 -07001219static jclass helperClass = NULL;
1220
ksrinie97d4002012-07-31 06:14:28 -07001221jclass
1222GetLauncherHelperClass(JNIEnv *env)
1223{
ksrini7d9872e2011-03-20 08:41:33 -07001224 if (helperClass == NULL) {
1225 NULL_CHECK0(helperClass = FindBootStrapClass(env,
1226 "sun/launcher/LauncherHelper"));
duke6e45e102007-12-01 00:00:00 +00001227 }
ksrini7d9872e2011-03-20 08:41:33 -07001228 return helperClass;
duke6e45e102007-12-01 00:00:00 +00001229}
1230
ksrini7d9872e2011-03-20 08:41:33 -07001231static jmethodID makePlatformStringMID = NULL;
duke6e45e102007-12-01 00:00:00 +00001232/*
1233 * Returns a new Java string object for the specified platform string.
1234 */
1235static jstring
1236NewPlatformString(JNIEnv *env, char *s)
1237{
1238 int len = (int)JLI_StrLen(s);
duke6e45e102007-12-01 00:00:00 +00001239 jbyteArray ary;
ksrini7d9872e2011-03-20 08:41:33 -07001240 jclass cls = GetLauncherHelperClass(env);
1241 NULL_CHECK0(cls);
duke6e45e102007-12-01 00:00:00 +00001242 if (s == NULL)
1243 return 0;
duke6e45e102007-12-01 00:00:00 +00001244
1245 ary = (*env)->NewByteArray(env, len);
1246 if (ary != 0) {
1247 jstring str = 0;
1248 (*env)->SetByteArrayRegion(env, ary, 0, len, (jbyte *)s);
1249 if (!(*env)->ExceptionOccurred(env)) {
ksrini7d9872e2011-03-20 08:41:33 -07001250 if (makePlatformStringMID == NULL) {
1251 NULL_CHECK0(makePlatformStringMID = (*env)->GetStaticMethodID(env,
1252 cls, "makePlatformString", "(Z[B)Ljava/lang/String;"));
duke6e45e102007-12-01 00:00:00 +00001253 }
ksrini7d9872e2011-03-20 08:41:33 -07001254 str = (*env)->CallStaticObjectMethod(env, cls,
1255 makePlatformStringMID, USE_STDERR, ary);
duke6e45e102007-12-01 00:00:00 +00001256 (*env)->DeleteLocalRef(env, ary);
1257 return str;
1258 }
1259 }
1260 return 0;
1261}
1262
1263/*
1264 * Returns a new array of Java string objects for the specified
1265 * array of platform strings.
1266 */
ksrinie97d4002012-07-31 06:14:28 -07001267jobjectArray
duke6e45e102007-12-01 00:00:00 +00001268NewPlatformStringArray(JNIEnv *env, char **strv, int strc)
1269{
1270 jarray cls;
1271 jarray ary;
1272 int i;
1273
ksrini20a64b22008-09-24 15:07:41 -07001274 NULL_CHECK0(cls = FindBootStrapClass(env, "java/lang/String"));
duke6e45e102007-12-01 00:00:00 +00001275 NULL_CHECK0(ary = (*env)->NewObjectArray(env, strc, cls, 0));
1276 for (i = 0; i < strc; i++) {
1277 jstring str = NewPlatformString(env, *strv++);
1278 NULL_CHECK0(str);
1279 (*env)->SetObjectArrayElement(env, ary, i, str);
1280 (*env)->DeleteLocalRef(env, str);
1281 }
1282 return ary;
1283}
1284
1285/*
ksrini20a64b22008-09-24 15:07:41 -07001286 * Loads a class and verifies that the main class is present and it is ok to
1287 * call it for more details refer to the java implementation.
duke6e45e102007-12-01 00:00:00 +00001288 */
1289static jclass
mchungea290e22011-01-21 09:43:57 -08001290LoadMainClass(JNIEnv *env, int mode, char *name)
duke6e45e102007-12-01 00:00:00 +00001291{
ksrini20a64b22008-09-24 15:07:41 -07001292 jmethodID mid;
1293 jstring str;
1294 jobject result;
duke6e45e102007-12-01 00:00:00 +00001295 jlong start, end;
ksrini7d9872e2011-03-20 08:41:33 -07001296 jclass cls = GetLauncherHelperClass(env);
1297 NULL_CHECK0(cls);
ksrini20a64b22008-09-24 15:07:41 -07001298 if (JLI_IsTraceLauncher()) {
duke6e45e102007-12-01 00:00:00 +00001299 start = CounterGet();
ksrini20a64b22008-09-24 15:07:41 -07001300 }
ksrini7d9872e2011-03-20 08:41:33 -07001301 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1302 "checkAndLoadMain",
1303 "(ZILjava/lang/String;)Ljava/lang/Class;"));
1304
ksriniac5e02f2012-01-11 08:14:47 -08001305 str = NewPlatformString(env, name);
ksrini7d9872e2011-03-20 08:41:33 -07001306 result = (*env)->CallStaticObjectMethod(env, cls, mid, USE_STDERR, mode, str);
duke6e45e102007-12-01 00:00:00 +00001307
1308 if (JLI_IsTraceLauncher()) {
1309 end = CounterGet();
1310 printf("%ld micro seconds to load main class\n",
1311 (long)(jint)Counter2Micros(end-start));
ksrinie97d4002012-07-31 06:14:28 -07001312 printf("----%s----\n", JLDEBUG_ENV_ENTRY);
duke6e45e102007-12-01 00:00:00 +00001313 }
1314
ksrini20a64b22008-09-24 15:07:41 -07001315 return (jclass)result;
duke6e45e102007-12-01 00:00:00 +00001316}
1317
ksrini6b41ee82012-11-19 19:49:38 -08001318static jclass
1319GetApplicationClass(JNIEnv *env)
1320{
1321 jmethodID mid;
1322 jobject result;
1323 jclass cls = GetLauncherHelperClass(env);
1324 NULL_CHECK0(cls);
1325 NULL_CHECK0(mid = (*env)->GetStaticMethodID(env, cls,
1326 "getApplicationClass",
1327 "()Ljava/lang/Class;"));
1328
1329 return (*env)->CallStaticObjectMethod(env, cls, mid);
1330}
1331
duke6e45e102007-12-01 00:00:00 +00001332/*
1333 * For tools, convert command line args thus:
1334 * javac -cp foo:foo/"*" -J-ms32m ...
1335 * java -ms32m -cp JLI_WildcardExpandClasspath(foo:foo/"*") ...
1336 *
1337 * Takes 4 parameters, and returns the populated arguments
1338 */
1339static void
1340TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv)
1341{
1342 int argc = *pargc;
1343 char **argv = *pargv;
1344 int nargc = argc + jargc;
1345 char **nargv = JLI_MemAlloc((nargc + 1) * sizeof(char *));
1346 int i;
1347
1348 *pargc = nargc;
1349 *pargv = nargv;
1350
1351 /* Copy the VM arguments (i.e. prefixed with -J) */
1352 for (i = 0; i < jargc; i++) {
1353 const char *arg = jargv[i];
1354 if (arg[0] == '-' && arg[1] == 'J') {
1355 *nargv++ = ((arg + 2) == NULL) ? NULL : JLI_StringDup(arg + 2);
1356 }
1357 }
1358
1359 for (i = 0; i < argc; i++) {
1360 char *arg = argv[i];
1361 if (arg[0] == '-' && arg[1] == 'J') {
1362 if (arg[2] == '\0') {
ksrini0e817162008-08-26 10:21:20 -07001363 JLI_ReportErrorMessage(ARG_ERROR3);
duke6e45e102007-12-01 00:00:00 +00001364 exit(1);
1365 }
1366 *nargv++ = arg + 2;
1367 }
1368 }
1369
1370 /* Copy the rest of the arguments */
1371 for (i = 0; i < jargc ; i++) {
1372 const char *arg = jargv[i];
1373 if (arg[0] != '-' || arg[1] != 'J') {
1374 *nargv++ = (arg == NULL) ? NULL : JLI_StringDup(arg);
1375 }
1376 }
1377 for (i = 0; i < argc; i++) {
1378 char *arg = argv[i];
1379 if (arg[0] == '-') {
1380 if (arg[1] == 'J')
1381 continue;
1382 if (IsWildCardEnabled() && arg[1] == 'c'
1383 && (JLI_StrCmp(arg, "-cp") == 0 ||
1384 JLI_StrCmp(arg, "-classpath") == 0)
1385 && i < argc - 1) {
1386 *nargv++ = arg;
1387 *nargv++ = (char *) JLI_WildcardExpandClasspath(argv[i+1]);
1388 i++;
1389 continue;
1390 }
1391 }
1392 *nargv++ = arg;
1393 }
1394 *nargv = 0;
1395}
1396
1397/*
1398 * For our tools, we try to add 3 VM options:
1399 * -Denv.class.path=<envcp>
1400 * -Dapplication.home=<apphome>
1401 * -Djava.class.path=<appcp>
1402 * <envcp> is the user's setting of CLASSPATH -- for instance the user
1403 * tells javac where to find binary classes through this environment
1404 * variable. Notice that users will be able to compile against our
1405 * tools classes (sun.tools.javac.Main) only if they explicitly add
1406 * tools.jar to CLASSPATH.
1407 * <apphome> is the directory where the application is installed.
1408 * <appcp> is the classpath to where our apps' classfiles are.
1409 */
1410static jboolean
1411AddApplicationOptions(int cpathc, const char **cpathv)
1412{
1413 char *envcp, *appcp, *apphome;
1414 char home[MAXPATHLEN]; /* application home */
1415 char separator[] = { PATH_SEPARATOR, '\0' };
1416 int size, i;
1417
1418 {
1419 const char *s = getenv("CLASSPATH");
1420 if (s) {
1421 s = (char *) JLI_WildcardExpandClasspath(s);
1422 /* 40 for -Denv.class.path= */
kizune0fc19542014-05-23 20:46:44 +04001423 if (JLI_StrLen(s) + 40 > JLI_StrLen(s)) { // Safeguard from overflow
1424 envcp = (char *)JLI_MemAlloc(JLI_StrLen(s) + 40);
1425 sprintf(envcp, "-Denv.class.path=%s", s);
1426 AddOption(envcp, NULL);
1427 }
duke6e45e102007-12-01 00:00:00 +00001428 }
1429 }
1430
1431 if (!GetApplicationHome(home, sizeof(home))) {
ksrini0e817162008-08-26 10:21:20 -07001432 JLI_ReportErrorMessage(CFG_ERROR5);
duke6e45e102007-12-01 00:00:00 +00001433 return JNI_FALSE;
1434 }
1435
1436 /* 40 for '-Dapplication.home=' */
1437 apphome = (char *)JLI_MemAlloc(JLI_StrLen(home) + 40);
1438 sprintf(apphome, "-Dapplication.home=%s", home);
1439 AddOption(apphome, NULL);
1440
1441 /* How big is the application's classpath? */
1442 size = 40; /* 40: "-Djava.class.path=" */
1443 for (i = 0; i < cpathc; i++) {
1444 size += (int)JLI_StrLen(home) + (int)JLI_StrLen(cpathv[i]) + 1; /* 1: separator */
1445 }
1446 appcp = (char *)JLI_MemAlloc(size + 1);
1447 JLI_StrCpy(appcp, "-Djava.class.path=");
1448 for (i = 0; i < cpathc; i++) {
1449 JLI_StrCat(appcp, home); /* c:\program files\myapp */
1450 JLI_StrCat(appcp, cpathv[i]); /* \lib\myapp.jar */
1451 JLI_StrCat(appcp, separator); /* ; */
1452 }
1453 appcp[JLI_StrLen(appcp)-1] = '\0'; /* remove trailing path separator */
1454 AddOption(appcp, NULL);
1455 return JNI_TRUE;
1456}
1457
1458/*
1459 * inject the -Dsun.java.command pseudo property into the args structure
1460 * this pseudo property is used in the HotSpot VM to expose the
1461 * Java class name and arguments to the main method to the VM. The
1462 * HotSpot VM uses this pseudo property to store the Java class name
1463 * (or jar file name) and the arguments to the class's main method
1464 * to the instrumentation memory region. The sun.java.command pseudo
1465 * property is not exported by HotSpot to the Java layer.
1466 */
1467void
mchungea290e22011-01-21 09:43:57 -08001468SetJavaCommandLineProp(char *what, int argc, char **argv)
duke6e45e102007-12-01 00:00:00 +00001469{
1470
1471 int i = 0;
1472 size_t len = 0;
1473 char* javaCommand = NULL;
1474 char* dashDstr = "-Dsun.java.command=";
1475
mchungea290e22011-01-21 09:43:57 -08001476 if (what == NULL) {
duke6e45e102007-12-01 00:00:00 +00001477 /* unexpected, one of these should be set. just return without
1478 * setting the property
1479 */
1480 return;
1481 }
1482
duke6e45e102007-12-01 00:00:00 +00001483 /* determine the amount of memory to allocate assuming
1484 * the individual components will be space separated
1485 */
mchungea290e22011-01-21 09:43:57 -08001486 len = JLI_StrLen(what);
duke6e45e102007-12-01 00:00:00 +00001487 for (i = 0; i < argc; i++) {
1488 len += JLI_StrLen(argv[i]) + 1;
1489 }
1490
1491 /* allocate the memory */
1492 javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);
1493
1494 /* build the -D string */
1495 *javaCommand = '\0';
1496 JLI_StrCat(javaCommand, dashDstr);
mchungea290e22011-01-21 09:43:57 -08001497 JLI_StrCat(javaCommand, what);
duke6e45e102007-12-01 00:00:00 +00001498
1499 for (i = 0; i < argc; i++) {
1500 /* the components of the string are space separated. In
1501 * the case of embedded white space, the relationship of
1502 * the white space separated components to their true
1503 * positional arguments will be ambiguous. This issue may
1504 * be addressed in a future release.
1505 */
1506 JLI_StrCat(javaCommand, " ");
1507 JLI_StrCat(javaCommand, argv[i]);
1508 }
1509
1510 AddOption(javaCommand, NULL);
1511}
1512
1513/*
1514 * JVM would like to know if it's created by a standard Sun launcher, or by
1515 * user native application, the following property indicates the former.
1516 */
mchungea290e22011-01-21 09:43:57 -08001517void
1518SetJavaLauncherProp() {
duke6e45e102007-12-01 00:00:00 +00001519 AddOption("-Dsun.java.launcher=SUN_STANDARD", NULL);
1520}
1521
1522/*
1523 * Prints the version information from the java.version and other properties.
1524 */
1525static void
1526PrintJavaVersion(JNIEnv *env, jboolean extraLF)
1527{
1528 jclass ver;
1529 jmethodID print;
1530
ksrini20a64b22008-09-24 15:07:41 -07001531 NULL_CHECK(ver = FindBootStrapClass(env, "sun/misc/Version"));
duke6e45e102007-12-01 00:00:00 +00001532 NULL_CHECK(print = (*env)->GetStaticMethodID(env,
1533 ver,
1534 (extraLF == JNI_TRUE) ? "println" : "print",
1535 "()V"
1536 )
1537 );
1538
1539 (*env)->CallStaticVoidMethod(env, ver, print);
1540}
1541
1542/*
ksrini8e20e1c2010-11-23 16:52:39 -08001543 * Prints all the Java settings, see the java implementation for more details.
1544 */
1545static void
1546ShowSettings(JNIEnv *env, char *optString)
1547{
ksrini8e20e1c2010-11-23 16:52:39 -08001548 jmethodID showSettingsID;
1549 jstring joptString;
ksrini7d9872e2011-03-20 08:41:33 -07001550 jclass cls = GetLauncherHelperClass(env);
1551 NULL_CHECK(cls);
ksrini8e20e1c2010-11-23 16:52:39 -08001552 NULL_CHECK(showSettingsID = (*env)->GetStaticMethodID(env, cls,
ksrini6d575f82010-12-23 13:51:30 -08001553 "showSettings", "(ZLjava/lang/String;JJJZ)V"));
ksrini8e20e1c2010-11-23 16:52:39 -08001554 joptString = (*env)->NewStringUTF(env, optString);
1555 (*env)->CallStaticVoidMethod(env, cls, showSettingsID,
ksrini7d9872e2011-03-20 08:41:33 -07001556 USE_STDERR,
ksrini8e20e1c2010-11-23 16:52:39 -08001557 joptString,
ksrini6d575f82010-12-23 13:51:30 -08001558 (jlong)initialHeapSize,
1559 (jlong)maxHeapSize,
ksrini8e20e1c2010-11-23 16:52:39 -08001560 (jlong)threadStackSize,
1561 ServerClassMachine());
1562}
1563
1564/*
ksrini20a64b22008-09-24 15:07:41 -07001565 * Prints default usage or the Xusage message, see sun.launcher.LauncherHelper.java
duke6e45e102007-12-01 00:00:00 +00001566 */
1567static void
1568PrintUsage(JNIEnv* env, jboolean doXUsage)
1569{
duke6e45e102007-12-01 00:00:00 +00001570 jmethodID initHelp, vmSelect, vmSynonym, vmErgo, printHelp, printXUsageMessage;
1571 jstring jprogname, vm1, vm2;
1572 int i;
ksrini7d9872e2011-03-20 08:41:33 -07001573 jclass cls = GetLauncherHelperClass(env);
1574 NULL_CHECK(cls);
duke6e45e102007-12-01 00:00:00 +00001575 if (doXUsage) {
1576 NULL_CHECK(printXUsageMessage = (*env)->GetStaticMethodID(env, cls,
1577 "printXUsageMessage", "(Z)V"));
ksrini7d9872e2011-03-20 08:41:33 -07001578 (*env)->CallStaticVoidMethod(env, cls, printXUsageMessage, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001579 } else {
1580 NULL_CHECK(initHelp = (*env)->GetStaticMethodID(env, cls,
1581 "initHelpMessage", "(Ljava/lang/String;)V"));
1582
1583 NULL_CHECK(vmSelect = (*env)->GetStaticMethodID(env, cls, "appendVmSelectMessage",
1584 "(Ljava/lang/String;Ljava/lang/String;)V"));
1585
1586 NULL_CHECK(vmSynonym = (*env)->GetStaticMethodID(env, cls,
1587 "appendVmSynonymMessage",
1588 "(Ljava/lang/String;Ljava/lang/String;)V"));
1589 NULL_CHECK(vmErgo = (*env)->GetStaticMethodID(env, cls,
1590 "appendVmErgoMessage", "(ZLjava/lang/String;)V"));
1591
1592 NULL_CHECK(printHelp = (*env)->GetStaticMethodID(env, cls,
1593 "printHelpMessage", "(Z)V"));
1594
1595 jprogname = (*env)->NewStringUTF(env, _program_name);
1596
1597 /* Initialize the usage message with the usual preamble */
1598 (*env)->CallStaticVoidMethod(env, cls, initHelp, jprogname);
1599
1600
1601 /* Assemble the other variant part of the usage */
1602 if ((knownVMs[0].flag == VM_KNOWN) ||
1603 (knownVMs[0].flag == VM_IF_SERVER_CLASS)) {
1604 vm1 = (*env)->NewStringUTF(env, knownVMs[0].name);
1605 vm2 = (*env)->NewStringUTF(env, knownVMs[0].name+1);
1606 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1607 }
1608 for (i=1; i<knownVMsCount; i++) {
1609 if (knownVMs[i].flag == VM_KNOWN) {
1610 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1611 vm2 = (*env)->NewStringUTF(env, knownVMs[i].name+1);
1612 (*env)->CallStaticVoidMethod(env, cls, vmSelect, vm1, vm2);
1613 }
1614 }
1615 for (i=1; i<knownVMsCount; i++) {
1616 if (knownVMs[i].flag == VM_ALIASED_TO) {
1617 vm1 = (*env)->NewStringUTF(env, knownVMs[i].name);
1618 vm2 = (*env)->NewStringUTF(env, knownVMs[i].alias+1);
1619 (*env)->CallStaticVoidMethod(env, cls, vmSynonym, vm1, vm2);
1620 }
1621 }
1622
1623 /* The first known VM is the default */
1624 {
1625 jboolean isServerClassMachine = ServerClassMachine();
1626
1627 const char* defaultVM = knownVMs[0].name+1;
1628 if ((knownVMs[0].flag == VM_IF_SERVER_CLASS) && isServerClassMachine) {
1629 defaultVM = knownVMs[0].server_class+1;
1630 }
1631
1632 vm1 = (*env)->NewStringUTF(env, defaultVM);
1633 (*env)->CallStaticVoidMethod(env, cls, vmErgo, isServerClassMachine, vm1);
1634 }
1635
1636 /* Complete the usage message and print to stderr*/
ksrini7d9872e2011-03-20 08:41:33 -07001637 (*env)->CallStaticVoidMethod(env, cls, printHelp, USE_STDERR);
duke6e45e102007-12-01 00:00:00 +00001638 }
1639 return;
1640}
1641
1642/*
1643 * Read the jvm.cfg file and fill the knownJVMs[] array.
1644 *
1645 * The functionality of the jvm.cfg file is subject to change without
1646 * notice and the mechanism will be removed in the future.
1647 *
1648 * The lexical structure of the jvm.cfg file is as follows:
1649 *
1650 * jvmcfg := { vmLine }
1651 * vmLine := knownLine
1652 * | aliasLine
1653 * | warnLine
1654 * | ignoreLine
1655 * | errorLine
1656 * | predicateLine
1657 * | commentLine
1658 * knownLine := flag "KNOWN" EOL
1659 * warnLine := flag "WARN" EOL
1660 * ignoreLine := flag "IGNORE" EOL
1661 * errorLine := flag "ERROR" EOL
1662 * aliasLine := flag "ALIASED_TO" flag EOL
1663 * predicateLine := flag "IF_SERVER_CLASS" flag EOL
1664 * commentLine := "#" text EOL
1665 * flag := "-" identifier
1666 *
1667 * The semantics are that when someone specifies a flag on the command line:
1668 * - if the flag appears on a knownLine, then the identifier is used as
1669 * the name of the directory holding the JVM library (the name of the JVM).
1670 * - if the flag appears as the first flag on an aliasLine, the identifier
1671 * of the second flag is used as the name of the JVM.
1672 * - if the flag appears on a warnLine, the identifier is used as the
1673 * name of the JVM, but a warning is generated.
1674 * - if the flag appears on an ignoreLine, the identifier is recognized as the
1675 * name of a JVM, but the identifier is ignored and the default vm used
1676 * - if the flag appears on an errorLine, an error is generated.
1677 * - if the flag appears as the first flag on a predicateLine, and
1678 * the machine on which you are running passes the predicate indicated,
1679 * then the identifier of the second flag is used as the name of the JVM,
1680 * otherwise the identifier of the first flag is used as the name of the JVM.
1681 * If no flag is given on the command line, the first vmLine of the jvm.cfg
1682 * file determines the name of the JVM.
1683 * PredicateLines are only interpreted on first vmLine of a jvm.cfg file,
1684 * since they only make sense if someone hasn't specified the name of the
1685 * JVM on the command line.
1686 *
1687 * The intent of the jvm.cfg file is to allow several JVM libraries to
1688 * be installed in different subdirectories of a single JRE installation,
1689 * for space-savings and convenience in testing.
1690 * The intent is explicitly not to provide a full aliasing or predicate
1691 * mechanism.
1692 */
1693jint
michaelm5ac8c152012-03-06 20:34:38 +00001694ReadKnownVMs(const char *jvmCfgName, jboolean speculative)
duke6e45e102007-12-01 00:00:00 +00001695{
1696 FILE *jvmCfg;
duke6e45e102007-12-01 00:00:00 +00001697 char line[MAXPATHLEN+20];
1698 int cnt = 0;
1699 int lineno = 0;
1700 jlong start, end;
1701 int vmType;
1702 char *tmpPtr;
1703 char *altVMName = NULL;
1704 char *serverClassVMName = NULL;
1705 static char *whiteSpace = " \t";
1706 if (JLI_IsTraceLauncher()) {
1707 start = CounterGet();
1708 }
duke6e45e102007-12-01 00:00:00 +00001709
1710 jvmCfg = fopen(jvmCfgName, "r");
1711 if (jvmCfg == NULL) {
1712 if (!speculative) {
ksrini0e817162008-08-26 10:21:20 -07001713 JLI_ReportErrorMessage(CFG_ERROR6, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001714 exit(1);
1715 } else {
1716 return -1;
1717 }
1718 }
1719 while (fgets(line, sizeof(line), jvmCfg) != NULL) {
1720 vmType = VM_UNKNOWN;
1721 lineno++;
1722 if (line[0] == '#')
1723 continue;
1724 if (line[0] != '-') {
ksrini0e817162008-08-26 10:21:20 -07001725 JLI_ReportErrorMessage(CFG_WARN2, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001726 }
1727 if (cnt >= knownVMsLimit) {
1728 GrowKnownVMs(cnt);
1729 }
1730 line[JLI_StrLen(line)-1] = '\0'; /* remove trailing newline */
1731 tmpPtr = line + JLI_StrCSpn(line, whiteSpace);
1732 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001733 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001734 } else {
1735 /* Null-terminate this string for JLI_StringDup below */
1736 *tmpPtr++ = 0;
1737 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1738 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001739 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001740 } else {
1741 if (!JLI_StrCCmp(tmpPtr, "KNOWN")) {
1742 vmType = VM_KNOWN;
1743 } else if (!JLI_StrCCmp(tmpPtr, "ALIASED_TO")) {
1744 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1745 if (*tmpPtr != 0) {
1746 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1747 }
1748 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001749 JLI_ReportErrorMessage(CFG_WARN3, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001750 } else {
1751 /* Null terminate altVMName */
1752 altVMName = tmpPtr;
1753 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1754 *tmpPtr = 0;
1755 vmType = VM_ALIASED_TO;
1756 }
1757 } else if (!JLI_StrCCmp(tmpPtr, "WARN")) {
1758 vmType = VM_WARN;
1759 } else if (!JLI_StrCCmp(tmpPtr, "IGNORE")) {
1760 vmType = VM_IGNORE;
1761 } else if (!JLI_StrCCmp(tmpPtr, "ERROR")) {
1762 vmType = VM_ERROR;
1763 } else if (!JLI_StrCCmp(tmpPtr, "IF_SERVER_CLASS")) {
1764 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1765 if (*tmpPtr != 0) {
1766 tmpPtr += JLI_StrSpn(tmpPtr, whiteSpace);
1767 }
1768 if (*tmpPtr == 0) {
ksrini0e817162008-08-26 10:21:20 -07001769 JLI_ReportErrorMessage(CFG_WARN4, lineno, jvmCfgName);
duke6e45e102007-12-01 00:00:00 +00001770 } else {
1771 /* Null terminate server class VM name */
1772 serverClassVMName = tmpPtr;
1773 tmpPtr += JLI_StrCSpn(tmpPtr, whiteSpace);
1774 *tmpPtr = 0;
1775 vmType = VM_IF_SERVER_CLASS;
1776 }
1777 } else {
ksrini0e817162008-08-26 10:21:20 -07001778 JLI_ReportErrorMessage(CFG_WARN5, lineno, &jvmCfgName[0]);
duke6e45e102007-12-01 00:00:00 +00001779 vmType = VM_KNOWN;
1780 }
1781 }
1782 }
1783
1784 JLI_TraceLauncher("jvm.cfg[%d] = ->%s<-\n", cnt, line);
1785 if (vmType != VM_UNKNOWN) {
1786 knownVMs[cnt].name = JLI_StringDup(line);
1787 knownVMs[cnt].flag = vmType;
1788 switch (vmType) {
1789 default:
1790 break;
1791 case VM_ALIASED_TO:
1792 knownVMs[cnt].alias = JLI_StringDup(altVMName);
1793 JLI_TraceLauncher(" name: %s vmType: %s alias: %s\n",
1794 knownVMs[cnt].name, "VM_ALIASED_TO", knownVMs[cnt].alias);
1795 break;
1796 case VM_IF_SERVER_CLASS:
1797 knownVMs[cnt].server_class = JLI_StringDup(serverClassVMName);
1798 JLI_TraceLauncher(" name: %s vmType: %s server_class: %s\n",
1799 knownVMs[cnt].name, "VM_IF_SERVER_CLASS", knownVMs[cnt].server_class);
1800 break;
1801 }
1802 cnt++;
1803 }
1804 }
1805 fclose(jvmCfg);
1806 knownVMsCount = cnt;
1807
1808 if (JLI_IsTraceLauncher()) {
1809 end = CounterGet();
1810 printf("%ld micro seconds to parse jvm.cfg\n",
1811 (long)(jint)Counter2Micros(end-start));
1812 }
1813
1814 return cnt;
1815}
1816
1817
1818static void
1819GrowKnownVMs(int minimum)
1820{
1821 struct vmdesc* newKnownVMs;
1822 int newMax;
1823
1824 newMax = (knownVMsLimit == 0 ? INIT_MAX_KNOWN_VMS : (2 * knownVMsLimit));
1825 if (newMax <= minimum) {
1826 newMax = minimum;
1827 }
1828 newKnownVMs = (struct vmdesc*) JLI_MemAlloc(newMax * sizeof(struct vmdesc));
1829 if (knownVMs != NULL) {
1830 memcpy(newKnownVMs, knownVMs, knownVMsLimit * sizeof(struct vmdesc));
1831 }
1832 JLI_MemFree(knownVMs);
1833 knownVMs = newKnownVMs;
1834 knownVMsLimit = newMax;
1835}
1836
1837
1838/* Returns index of VM or -1 if not found */
1839static int
1840KnownVMIndex(const char* name)
1841{
1842 int i;
1843 if (JLI_StrCCmp(name, "-J") == 0) name += 2;
1844 for (i = 0; i < knownVMsCount; i++) {
1845 if (!JLI_StrCmp(name, knownVMs[i].name)) {
1846 return i;
1847 }
1848 }
1849 return -1;
1850}
1851
1852static void
1853FreeKnownVMs()
1854{
1855 int i;
1856 for (i = 0; i < knownVMsCount; i++) {
1857 JLI_MemFree(knownVMs[i].name);
1858 knownVMs[i].name = NULL;
1859 }
1860 JLI_MemFree(knownVMs);
1861}
1862
duke6e45e102007-12-01 00:00:00 +00001863/*
1864 * Displays the splash screen according to the jar file name
1865 * and image file names stored in environment variables
1866 */
michaelm5ac8c152012-03-06 20:34:38 +00001867void
duke6e45e102007-12-01 00:00:00 +00001868ShowSplashScreen()
1869{
1870 const char *jar_name = getenv(SPLASH_JAR_ENV_ENTRY);
1871 const char *file_name = getenv(SPLASH_FILE_ENV_ENTRY);
1872 int data_size;
1873 void *image_data;
1874 if (jar_name) {
1875 image_data = JLI_JarUnpackFile(jar_name, file_name, &data_size);
1876 if (image_data) {
1877 DoSplashInit();
1878 DoSplashLoadMemory(image_data, data_size);
1879 JLI_MemFree(image_data);
1880 }
1881 } else if (file_name) {
1882 DoSplashInit();
1883 DoSplashLoadFile(file_name);
1884 } else {
1885 return;
1886 }
1887 DoSplashSetFileJarName(file_name, jar_name);
1888
1889 /*
1890 * Done with all command line processing and potential re-execs so
1891 * clean up the environment.
1892 */
1893 (void)UnsetEnv(ENV_ENTRY);
1894 (void)UnsetEnv(SPLASH_FILE_ENV_ENTRY);
1895 (void)UnsetEnv(SPLASH_JAR_ENV_ENTRY);
1896
1897 JLI_MemFree(splash_jar_entry);
1898 JLI_MemFree(splash_file_entry);
1899
1900}
1901
1902const char*
1903GetDotVersion()
1904{
1905 return _dVersion;
1906}
1907
1908const char*
1909GetFullVersion()
1910{
1911 return _fVersion;
1912}
1913
1914const char*
1915GetProgramName()
1916{
1917 return _program_name;
1918}
1919
1920const char*
1921GetLauncherName()
1922{
1923 return _launcher_name;
1924}
1925
1926jint
1927GetErgoPolicy()
1928{
1929 return _ergo_policy;
1930}
1931
1932jboolean
1933IsJavaArgs()
1934{
1935 return _is_java_args;
1936}
1937
1938static jboolean
1939IsWildCardEnabled()
1940{
1941 return _wc_enabled;
1942}
1943
michaelm5ac8c152012-03-06 20:34:38 +00001944int
1945ContinueInNewThread(InvocationFunctions* ifn, jlong threadStackSize,
1946 int argc, char **argv,
mchungea290e22011-01-21 09:43:57 -08001947 int mode, char *what, int ret)
duke6e45e102007-12-01 00:00:00 +00001948{
1949
1950 /*
1951 * If user doesn't specify stack size, check if VM has a preference.
1952 * Note that HotSpot no longer supports JNI_VERSION_1_1 but it will
1953 * return its default stack size through the init args structure.
1954 */
1955 if (threadStackSize == 0) {
1956 struct JDK1_1InitArgs args1_1;
1957 memset((void*)&args1_1, 0, sizeof(args1_1));
1958 args1_1.version = JNI_VERSION_1_1;
1959 ifn->GetDefaultJavaVMInitArgs(&args1_1); /* ignore return value */
1960 if (args1_1.javaStackSize > 0) {
1961 threadStackSize = args1_1.javaStackSize;
1962 }
1963 }
1964
1965 { /* Create a new thread to create JVM and invoke main method */
1966 JavaMainArgs args;
1967 int rslt;
1968
1969 args.argc = argc;
1970 args.argv = argv;
mchungea290e22011-01-21 09:43:57 -08001971 args.mode = mode;
1972 args.what = what;
duke6e45e102007-12-01 00:00:00 +00001973 args.ifn = *ifn;
1974
1975 rslt = ContinueInNewThread0(JavaMain, threadStackSize, (void*)&args);
1976 /* If the caller has deemed there is an error we
1977 * simply return that, otherwise we return the value of
1978 * the callee
1979 */
1980 return (ret != 0) ? ret : rslt;
1981 }
1982}
1983
1984static void
1985DumpState()
1986{
1987 if (!JLI_IsTraceLauncher()) return ;
1988 printf("Launcher state:\n");
1989 printf("\tdebug:%s\n", (JLI_IsTraceLauncher() == JNI_TRUE) ? "on" : "off");
1990 printf("\tjavargs:%s\n", (_is_java_args == JNI_TRUE) ? "on" : "off");
1991 printf("\tprogram name:%s\n", GetProgramName());
1992 printf("\tlauncher name:%s\n", GetLauncherName());
1993 printf("\tjavaw:%s\n", (IsJavaw() == JNI_TRUE) ? "on" : "off");
1994 printf("\tfullversion:%s\n", GetFullVersion());
1995 printf("\tdotversion:%s\n", GetDotVersion());
1996 printf("\tergo_policy:");
1997 switch(GetErgoPolicy()) {
1998 case NEVER_SERVER_CLASS:
1999 printf("NEVER_ACT_AS_A_SERVER_CLASS_MACHINE\n");
2000 break;
2001 case ALWAYS_SERVER_CLASS:
2002 printf("ALWAYS_ACT_AS_A_SERVER_CLASS_MACHINE\n");
2003 break;
2004 default:
2005 printf("DEFAULT_ERGONOMICS_POLICY\n");
2006 }
2007}
2008
2009/*
2010 * Return JNI_TRUE for an option string that has no effect but should
2011 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
2012 * Solaris SPARC, this screening needs to be done if:
ksrini11e7f1b2009-11-20 11:01:32 -08002013 * -d32 or -d64 is passed to a binary with an unmatched data model
2014 * (the exec in CreateExecutionEnvironment removes -d<n> options and points the
2015 * exec to the proper binary). In the case of when the data model and the
2016 * requested version is matched, an exec would not occur, and these options
2017 * were erroneously passed to the vm.
duke6e45e102007-12-01 00:00:00 +00002018 */
2019jboolean
2020RemovableOption(char * option)
2021{
2022 /*
2023 * Unconditionally remove both -d32 and -d64 options since only
2024 * the last such options has an effect; e.g.
2025 * java -d32 -d64 -d32 -version
2026 * is equivalent to
2027 * java -d32 -version
2028 */
2029
2030 if( (JLI_StrCCmp(option, "-d32") == 0 ) ||
2031 (JLI_StrCCmp(option, "-d64") == 0 ) )
2032 return JNI_TRUE;
2033 else
2034 return JNI_FALSE;
2035}
2036
2037/*
2038 * A utility procedure to always print to stderr
2039 */
2040void
ksrini0e817162008-08-26 10:21:20 -07002041JLI_ReportMessage(const char* fmt, ...)
duke6e45e102007-12-01 00:00:00 +00002042{
2043 va_list vl;
2044 va_start(vl, fmt);
2045 vfprintf(stderr, fmt, vl);
2046 fprintf(stderr, "\n");
2047 va_end(vl);
2048}