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 | 0c8c673 | 2011-10-02 16:14:13 -0700 | [diff] [blame^] | 18 | ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz, method_id, JNI_FALSE)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 19 | if (reflected.get() == NULL) { |
| 20 | fprintf(stderr, "Unable to get reflected method\n"); |
| 21 | return false; |
| 22 | } |
| 23 | // We now have a Method instance. We need to call its |
| 24 | // getModifiers() method. |
Elliott Hughes | 0c8c673 | 2011-10-02 16:14:13 -0700 | [diff] [blame^] | 25 | ScopedLocalRef<jclass> method(env, env->FindClass("java/lang/reflect/Method")); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 26 | if (method.get() == NULL) { |
| 27 | fprintf(stderr, "Unable to find class Method\n"); |
| 28 | return false; |
| 29 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 30 | static const int PUBLIC = 0x0001; // java.lang.reflect.Modifiers.PUBLIC |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 31 | jmethodID get_modifiers = env->GetMethodID(method.get(), "getModifiers", "()I"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 32 | if (get_modifiers == NULL) { |
| 33 | fprintf(stderr, "Unable to find reflect.Method.getModifiers\n"); |
| 34 | return false; |
| 35 | } |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 36 | int modifiers = env->CallIntMethod(reflected.get(), get_modifiers); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 37 | if ((modifiers & PUBLIC) == 0) { |
| 38 | return false; |
| 39 | } |
| 40 | return true; |
| 41 | } |
| 42 | |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 43 | static int InvokeMain(JNIEnv* env, int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 44 | // We want to call main() with a String array with our arguments in |
| 45 | // it. Create an array and populate it. Note argv[0] is not |
| 46 | // included. |
Elliott Hughes | 0a96fe0 | 2011-08-19 10:22:04 -0700 | [diff] [blame] | 47 | ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1)); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 48 | if (args.get() == NULL) { |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 49 | env->ExceptionDescribe(); |
| 50 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Find [class].main(String[]). |
| 54 | |
| 55 | // Convert "com.android.Blah" to "com/android/Blah". |
Elliott Hughes | e5b0dc8 | 2011-08-23 09:59:02 -0700 | [diff] [blame] | 56 | std::string class_name(argv[0]); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 57 | std::replace(class_name.begin(), class_name.end(), '.', '/'); |
| 58 | |
Elliott Hughes | 0dab4ec | 2011-08-11 12:19:32 -0700 | [diff] [blame] | 59 | ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str())); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 60 | if (klass.get() == NULL) { |
| 61 | fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str()); |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 62 | env->ExceptionDescribe(); |
| 63 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 66 | jmethodID method = env->GetStaticMethodID(klass.get(), "main", "([Ljava/lang/String;)V"); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 67 | if (method == NULL) { |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 68 | fprintf(stderr, "Unable to find static main(String[]) in '%s'\n", class_name.c_str()); |
| 69 | env->ExceptionDescribe(); |
| 70 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Make sure the method is public. JNI doesn't prevent us from |
| 74 | // calling a private method, so we have to check it explicitly. |
| 75 | if (!IsMethodPublic(env, klass.get(), method)) { |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 76 | fprintf(stderr, "Sorry, main() is not public in '%s'\n", class_name.c_str()); |
| 77 | env->ExceptionDescribe(); |
| 78 | return EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Invoke main(). |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 82 | env->CallStaticVoidMethod(klass.get(), method, args.get()); |
Elliott Hughes | 0c8c673 | 2011-10-02 16:14:13 -0700 | [diff] [blame^] | 83 | |
| 84 | // Check whether there was an uncaught exception. We don't log any uncaught exception here; |
| 85 | // detaching this thread will do that for us, but it will clear the exception (and invalidate |
| 86 | // our JNIEnv), so we need to check here. |
| 87 | return env->ExceptionCheck() ? EXIT_FAILURE : EXIT_SUCCESS; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Parse arguments. Most of it just gets passed through to the VM. |
| 91 | // The JNI spec defines a handful of standard arguments. |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 92 | int main(int argc, char** argv) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 93 | setvbuf(stdout, NULL, _IONBF, 0); |
| 94 | |
| 95 | // Skip over argv[0]. |
| 96 | argv++; |
| 97 | argc--; |
| 98 | |
| 99 | // If we're adding any additional stuff, e.g. function hook specifiers, |
| 100 | // add them to the count here. |
| 101 | // |
| 102 | // We're over-allocating, because this includes the options to the VM |
| 103 | // plus the options to the program. |
| 104 | int option_count = argc; |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 105 | UniquePtr<JavaVMOption[]> options(new JavaVMOption[option_count]()); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 106 | |
| 107 | // Copy options over. Everything up to the name of the class starts |
| 108 | // with a '-' (the function hook stuff is strictly internal). |
| 109 | // |
| 110 | // [Do we need to catch & handle "-jar" here?] |
| 111 | bool need_extra = false; |
| 112 | int curr_opt, arg_idx; |
| 113 | for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) { |
| 114 | if (argv[arg_idx][0] != '-' && !need_extra) { |
| 115 | break; |
| 116 | } |
| 117 | options[curr_opt++].optionString = argv[arg_idx]; |
| 118 | |
| 119 | // Some options require an additional argument. |
| 120 | need_extra = false; |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 121 | if (strcmp(argv[arg_idx], "-classpath") == 0 || strcmp(argv[arg_idx], "-cp") == 0) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 122 | // others? |
| 123 | need_extra = true; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (need_extra) { |
| 128 | fprintf(stderr, "VM requires value after last option flag\n"); |
| 129 | return EXIT_FAILURE; |
| 130 | } |
| 131 | |
| 132 | // Make sure they provided a class name. We do this after VM init |
| 133 | // so that things like "-Xrunjdwp:help" have the opportunity to emit |
| 134 | // a usage statement. |
| 135 | if (arg_idx == argc) { |
| 136 | fprintf(stderr, "Class name required\n"); |
| 137 | return EXIT_FAILURE; |
| 138 | } |
| 139 | |
| 140 | // insert additional internal options here |
| 141 | |
| 142 | DCHECK_LE(curr_opt, option_count); |
| 143 | |
| 144 | JavaVMInitArgs init_args; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 145 | init_args.version = JNI_VERSION_1_6; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 146 | init_args.options = options.get(); |
| 147 | init_args.nOptions = curr_opt; |
| 148 | init_args.ignoreUnrecognized = JNI_FALSE; |
| 149 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 150 | // Start VM. The current thread becomes the main thread of the VM. |
| 151 | JavaVM* vm = NULL; |
| 152 | JNIEnv* env = NULL; |
| 153 | if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) { |
| 154 | fprintf(stderr, "VM init failed (check log file)\n"); |
| 155 | return EXIT_FAILURE; |
| 156 | } |
| 157 | |
Elliott Hughes | d6fe38d | 2011-10-02 11:14:43 -0700 | [diff] [blame] | 158 | int rc = InvokeMain(env, argc - arg_idx, &argv[arg_idx]); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 159 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 160 | if (vm->DetachCurrentThread() != JNI_OK) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 161 | fprintf(stderr, "Warning: unable to detach main thread\n"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 162 | rc = EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 165 | if (vm->DestroyJavaVM() != 0) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 166 | fprintf(stderr, "Warning: VM did not shut down cleanly\n"); |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 167 | rc = EXIT_FAILURE; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 170 | return rc; |
Carl Shapiro | 7b21670 | 2011-06-17 15:09:26 -0700 | [diff] [blame] | 171 | } |