blob: 3730b43688423ef3efd6684008a8b2309d737c3f [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 }
Brian Carlstrom78128a62011-09-15 17:21:19 -070032 static const int PUBLIC = 0x0001; // java.lang.reflect.Modifiers.PUBLIC
Carl Shapiro2ed144c2011-07-26 16:52:08 -070033 jmethodID get_modifiers = env->GetMethodID(method.get(),
34 "getModifiers",
35 "()I");
36 if (get_modifiers == NULL) {
37 fprintf(stderr, "Unable to find reflect.Method.getModifiers\n");
38 return false;
39 }
Elliott Hughesc1674ed2011-08-25 18:09:09 -070040 int modifiers = env->CallIntMethod(reflected.get(), get_modifiers);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070041 if ((modifiers & PUBLIC) == 0) {
42 return false;
43 }
44 return true;
45}
46
Elliott Hughesc1674ed2011-08-25 18:09:09 -070047static void InvokeMain(JNIEnv* env, int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070048 // We want to call main() with a String array with our arguments in
49 // it. Create an array and populate it. Note argv[0] is not
50 // included.
Elliott Hughes0a96fe02011-08-19 10:22:04 -070051 ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070052 if (args.get() == NULL) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -070053 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070054 }
55
56 // Find [class].main(String[]).
57
58 // Convert "com.android.Blah" to "com/android/Blah".
Elliott Hughese5b0dc82011-08-23 09:59:02 -070059 std::string class_name(argv[0]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070060 std::replace(class_name.begin(), class_name.end(), '.', '/');
61
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070062 ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070063 if (klass.get() == NULL) {
64 fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
Elliott Hughesc1674ed2011-08-25 18:09:09 -070065 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070066 }
67
68 jmethodID method = env->GetStaticMethodID(klass.get(),
69 "main",
70 "([Ljava/lang/String;)V");
71 if (method == NULL) {
72 fprintf(stderr, "Unable to find static main(String[]) in '%s'\n",
73 class_name.c_str());
Elliott Hughesc1674ed2011-08-25 18:09:09 -070074 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070075 }
76
77 // Make sure the method is public. JNI doesn't prevent us from
78 // calling a private method, so we have to check it explicitly.
79 if (!IsMethodPublic(env, klass.get(), method)) {
80 fprintf(stderr, "Sorry, main() is not public\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -070081 return;
Carl Shapiro2ed144c2011-07-26 16:52:08 -070082 }
83
84 // Invoke main().
Carl Shapiro2ed144c2011-07-26 16:52:08 -070085 env->CallStaticVoidMethod(klass.get(), method, args.get());
Carl Shapiro2ed144c2011-07-26 16:52:08 -070086}
87
88// Parse arguments. Most of it just gets passed through to the VM.
89// The JNI spec defines a handful of standard arguments.
Carl Shapiro7b216702011-06-17 15:09:26 -070090int main(int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070091 setvbuf(stdout, NULL, _IONBF, 0);
92
93 // Skip over argv[0].
94 argv++;
95 argc--;
96
97 // If we're adding any additional stuff, e.g. function hook specifiers,
98 // add them to the count here.
99 //
100 // We're over-allocating, because this includes the options to the VM
101 // plus the options to the program.
102 int option_count = argc;
Elliott Hughes90a33692011-08-30 13:27:07 -0700103 UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]());
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700104
105 // Copy options over. Everything up to the name of the class starts
106 // with a '-' (the function hook stuff is strictly internal).
107 //
108 // [Do we need to catch & handle "-jar" here?]
109 bool need_extra = false;
110 int curr_opt, arg_idx;
111 for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
112 if (argv[arg_idx][0] != '-' && !need_extra) {
113 break;
114 }
115 options[curr_opt++].optionString = argv[arg_idx];
116
117 // Some options require an additional argument.
118 need_extra = false;
119 if (strcmp(argv[arg_idx], "-classpath") == 0 ||
120 strcmp(argv[arg_idx], "-cp") == 0) {
121 // others?
122 need_extra = true;
123 }
124 }
125
126 if (need_extra) {
127 fprintf(stderr, "VM requires value after last option flag\n");
128 return EXIT_FAILURE;
129 }
130
131 // Make sure they provided a class name. We do this after VM init
132 // so that things like "-Xrunjdwp:help" have the opportunity to emit
133 // a usage statement.
134 if (arg_idx == argc) {
135 fprintf(stderr, "Class name required\n");
136 return EXIT_FAILURE;
137 }
138
139 // insert additional internal options here
140
141 DCHECK_LE(curr_opt, option_count);
142
143 JavaVMInitArgs init_args;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700144 init_args.version = JNI_VERSION_1_6;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700145 init_args.options = options.get();
146 init_args.nOptions = curr_opt;
147 init_args.ignoreUnrecognized = JNI_FALSE;
148
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700149 // Start VM. The current thread becomes the main thread of the VM.
150 JavaVM* vm = NULL;
151 JNIEnv* env = NULL;
152 if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
153 fprintf(stderr, "VM init failed (check log file)\n");
154 return EXIT_FAILURE;
155 }
156
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700157 InvokeMain(env, argc - arg_idx, &argv[arg_idx]);
158 int rc = env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS;
159 if (rc == EXIT_FAILURE) {
160 env->ExceptionDescribe();
161 }
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700162
Elliott Hughesf2682d52011-08-15 16:37:04 -0700163 if (vm->DetachCurrentThread() != JNI_OK) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700164 fprintf(stderr, "Warning: unable to detach main thread\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700165 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700166 }
167
Elliott Hughesf2682d52011-08-15 16:37:04 -0700168 if (vm->DestroyJavaVM() != 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700169 fprintf(stderr, "Warning: VM did not shut down cleanly\n");
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700170 rc = EXIT_FAILURE;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700171 }
172
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700173 return rc;
Carl Shapiro7b216702011-06-17 15:09:26 -0700174}