blob: 229135c5c5c76c2824a087e279d9e7e4c1ccf1da [file] [log] [blame]
Carl Shapiro6b6b5f02011-06-21 15:05:09 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003#include <signal.h>
Elliott Hughes90a33692011-08-30 13:27:07 -07004
5#include <algorithm>
6#include <cstdio>
7#include <cstring>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include <string>
Carl Shapiro7b216702011-06-17 15:09:26 -07009
Elliott Hughes90a33692011-08-30 13:27:07 -070010#include "ScopedLocalRef.h"
11#include "UniquePtr.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070012#include "jni.h"
13#include "logging.h"
Elliott Hughes0a96fe02011-08-19 10:22:04 -070014#include "toStringArray.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070015
Carl Shapiro2ed144c2011-07-26 16:52:08 -070016// Determine whether or not the specified method is public.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070017static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) {
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070018 ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz,
19 method_id, JNI_FALSE));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070020 if (reflected.get() == NULL) {
21 fprintf(stderr, "Unable to get reflected method\n");
22 return false;
23 }
24 // We now have a Method instance. We need to call its
25 // getModifiers() method.
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070026 ScopedLocalRef<jclass> method(env,
27 env->FindClass("java/lang/reflect/Method"));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070028 if (method.get() == NULL) {
29 fprintf(stderr, "Unable to find class Method\n");
30 return false;
31 }
32 jmethodID get_modifiers = env->GetMethodID(method.get(),
33 "getModifiers",
34 "()I");
35 if (get_modifiers == NULL) {
36 fprintf(stderr, "Unable to find reflect.Method.getModifiers\n");
37 return false;
38 }
39 static const int PUBLIC = 0x0001; // java.lang.reflect.Modifiers.PUBLIC
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070040#if 0 // CallIntMethod not yet implemented
Elliott Hughesc1674ed2011-08-25 18:09:09 -070041 int modifiers = env->CallIntMethod(reflected.get(), get_modifiers);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070042#else
43 int modifiers = PUBLIC;
44 UNIMPLEMENTED(WARNING) << "assuming main is public...";
45#endif
Carl Shapiro2ed144c2011-07-26 16:52:08 -070046 if ((modifiers & PUBLIC) == 0) {
47 return false;
48 }
49 return true;
50}
51
Elliott Hughesc1674ed2011-08-25 18:09:09 -070052static void InvokeMain(JNIEnv* env, int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053 // We want to call main() with a String array with our arguments in
54 // it. Create an array and populate it. Note argv[0] is not
55 // included.
Elliott Hughes0a96fe02011-08-19 10:22:04 -070056 ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070057 if (args.get() == NULL) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -070058 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070059 }
60
61 // Find [class].main(String[]).
62
63 // Convert "com.android.Blah" to "com/android/Blah".
Elliott Hughese5b0dc82011-08-23 09:59:02 -070064 std::string class_name(argv[0]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070065 std::replace(class_name.begin(), class_name.end(), '.', '/');
66
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070067 ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070068 if (klass.get() == NULL) {
69 fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
Elliott Hughesc1674ed2011-08-25 18:09:09 -070070 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070071 }
72
73 jmethodID method = env->GetStaticMethodID(klass.get(),
74 "main",
75 "([Ljava/lang/String;)V");
76 if (method == NULL) {
77 fprintf(stderr, "Unable to find static main(String[]) in '%s'\n",
78 class_name.c_str());
Elliott Hughesc1674ed2011-08-25 18:09:09 -070079 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070080 }
81
82 // Make sure the method is public. JNI doesn't prevent us from
83 // calling a private method, so we have to check it explicitly.
84 if (!IsMethodPublic(env, klass.get(), method)) {
85 fprintf(stderr, "Sorry, main() is not public\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -070086 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070087 }
88
89 // Invoke main().
Carl Shapiro2ed144c2011-07-26 16:52:08 -070090 env->CallStaticVoidMethod(klass.get(), method, args.get());
Carl Shapiro2ed144c2011-07-26 16:52:08 -070091}
92
93// Parse arguments. Most of it just gets passed through to the VM.
94// The JNI spec defines a handful of standard arguments.
Carl Shapiro7b216702011-06-17 15:09:26 -070095int main(int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070096 setvbuf(stdout, NULL, _IONBF, 0);
97
98 // Skip over argv[0].
99 argv++;
100 argc--;
101
102 // If we're adding any additional stuff, e.g. function hook specifiers,
103 // add them to the count here.
104 //
105 // We're over-allocating, because this includes the options to the VM
106 // plus the options to the program.
107 int option_count = argc;
Elliott Hughes90a33692011-08-30 13:27:07 -0700108 UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]());
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700109
110 // Copy options over. Everything up to the name of the class starts
111 // with a '-' (the function hook stuff is strictly internal).
112 //
113 // [Do we need to catch & handle "-jar" here?]
114 bool need_extra = false;
115 int curr_opt, arg_idx;
116 for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
117 if (argv[arg_idx][0] != '-' && !need_extra) {
118 break;
119 }
120 options[curr_opt++].optionString = argv[arg_idx];
121
122 // Some options require an additional argument.
123 need_extra = false;
124 if (strcmp(argv[arg_idx], "-classpath") == 0 ||
125 strcmp(argv[arg_idx], "-cp") == 0) {
126 // others?
127 need_extra = true;
128 }
129 }
130
131 if (need_extra) {
132 fprintf(stderr, "VM requires value after last option flag\n");
133 return EXIT_FAILURE;
134 }
135
136 // Make sure they provided a class name. We do this after VM init
137 // so that things like "-Xrunjdwp:help" have the opportunity to emit
138 // a usage statement.
139 if (arg_idx == argc) {
140 fprintf(stderr, "Class name required\n");
141 return EXIT_FAILURE;
142 }
143
144 // insert additional internal options here
145
146 DCHECK_LE(curr_opt, option_count);
147
148 JavaVMInitArgs init_args;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700149 init_args.version = JNI_VERSION_1_6;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700150 init_args.options = options.get();
151 init_args.nOptions = curr_opt;
152 init_args.ignoreUnrecognized = JNI_FALSE;
153
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700154 // Start VM. The current thread becomes the main thread of the VM.
155 JavaVM* vm = NULL;
156 JNIEnv* env = NULL;
157 if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
158 fprintf(stderr, "VM init failed (check log file)\n");
159 return EXIT_FAILURE;
160 }
161
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700162 InvokeMain(env, argc - arg_idx, &argv[arg_idx]);
163 int rc = env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS;
164 if (rc == EXIT_FAILURE) {
165 env->ExceptionDescribe();
166 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700167
Elliott Hughesf2682d52011-08-15 16:37:04 -0700168 if (vm->DetachCurrentThread() != JNI_OK) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700169 fprintf(stderr, "Warning: unable to detach main thread\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700170 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700171 }
172
Elliott Hughesf2682d52011-08-15 16:37:04 -0700173 if (vm->DestroyJavaVM() != 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700174 fprintf(stderr, "Warning: VM did not shut down cleanly\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700175 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700176 }
177
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700178 return rc;
Carl Shapiro7b216702011-06-17 15:09:26 -0700179}