Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 3 | #include <signal.h> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 4 | |
| 5 | #include <algorithm> |
| 6 | #include <cstdio> |
| 7 | #include <cstring> |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 8 | #include <string> |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 9 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 10 | #include "ScopedLocalRef.h" |
| 11 | #include "UniquePtr.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 12 | #include "jni.h" |
| 13 | #include "logging.h" |
Elliott Hughes | 0a96fe0 | 2011-08-19 10:22:04 -0700 | [diff] [blame] | 14 | #include "toStringArray.h" |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 15 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 16 | // Determine whether or not the specified method is public. |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 17 | static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) { |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 18 | ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz, |
| 19 | method_id, JNI_FALSE)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 20 | 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 Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 26 | ScopedLocalRef<jclass> method(env, |
| 27 | env->FindClass("java/lang/reflect/Method")); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 28 | 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 Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 40 | #if 0 // CallIntMethod not yet implemented |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 41 | int modifiers = env->CallIntMethod(reflected.get(), get_modifiers); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 42 | #else |
| 43 | int modifiers = PUBLIC; |
| 44 | UNIMPLEMENTED(WARNING) << "assuming main is public..."; |
| 45 | #endif |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 46 | if ((modifiers & PUBLIC) == 0) { |
| 47 | return false; |
| 48 | } |
| 49 | return true; |
| 50 | } |
| 51 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 52 | static void InvokeMain(JNIEnv* env, int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 53 | // 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 Hughes | 0a96fe0 | 2011-08-19 10:22:04 -0700 | [diff] [blame] | 56 | ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 57 | if (args.get() == NULL) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 58 | return; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Find [class].main(String[]). |
| 62 | |
| 63 | // Convert "com.android.Blah" to "com/android/Blah". |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 64 | std::string class_name(argv[0]); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 65 | std::replace(class_name.begin(), class_name.end(), '.', '/'); |
| 66 | |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 67 | ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str())); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 68 | if (klass.get() == NULL) { |
| 69 | fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str()); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 70 | return; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 71 | } |
| 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 Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 79 | return; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 80 | } |
| 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 Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 86 | return; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Invoke main(). |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 90 | env->CallStaticVoidMethod(klass.get(), method, args.get()); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 91 | } |
| 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 Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 95 | int main(int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 96 | 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 Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame^] | 108 | UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]()); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 109 | |
| 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 Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 149 | init_args.version = JNI_VERSION_1_6; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 150 | init_args.options = options.get(); |
| 151 | init_args.nOptions = curr_opt; |
| 152 | init_args.ignoreUnrecognized = JNI_FALSE; |
| 153 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 154 | // 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 Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 162 | 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 Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 167 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 168 | if (vm->DetachCurrentThread() != JNI_OK) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 169 | fprintf(stderr, "Warning: unable to detach main thread\n"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 170 | rc = EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 173 | if (vm->DestroyJavaVM() != 0) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 174 | fprintf(stderr, "Warning: VM did not shut down cleanly\n"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 175 | rc = EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 178 | return rc; |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 179 | } |