| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/AndroidRuntime.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "AndroidRuntime" |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/IBinder.h> |
| 23 | #include <binder/IServiceManager.h> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | #include <utils/misc.h> |
| Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 26 | #include <binder/Parcel.h> |
| Mathias Agopian | 8ed6beb | 2009-06-05 01:26:23 -0700 | [diff] [blame] | 27 | #include <utils/StringArray.h> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <utils/threads.h> |
| 29 | #include <cutils/properties.h> |
| 30 | |
| 31 | #include <SkGraphics.h> |
| 32 | #include <SkImageDecoder.h> |
| Mike Reed | fc8db53 | 2009-04-27 11:43:30 -0400 | [diff] [blame] | 33 | #include <SkImageRef_GlobalPool.h> |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | #include "jni.h" |
| 36 | #include "JNIHelp.h" |
| 37 | #include "android_util_Binder.h" |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <signal.h> |
| 41 | #include <sys/stat.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <signal.h> |
| 44 | #include <dirent.h> |
| 45 | #include <assert.h> |
| 46 | |
| 47 | |
| 48 | using namespace android; |
| 49 | |
| 50 | extern void register_BindTest(); |
| 51 | |
| 52 | extern int register_android_os_Binder(JNIEnv* env); |
| 53 | extern int register_android_os_Process(JNIEnv* env); |
| 54 | extern int register_android_graphics_Bitmap(JNIEnv*); |
| 55 | extern int register_android_graphics_BitmapFactory(JNIEnv*); |
| 56 | extern int register_android_graphics_Camera(JNIEnv* env); |
| 57 | extern int register_android_graphics_Graphics(JNIEnv* env); |
| 58 | extern int register_android_graphics_Interpolator(JNIEnv* env); |
| 59 | extern int register_android_graphics_LayerRasterizer(JNIEnv*); |
| 60 | extern int register_android_graphics_MaskFilter(JNIEnv* env); |
| 61 | extern int register_android_graphics_Movie(JNIEnv* env); |
| 62 | extern int register_android_graphics_NinePatch(JNIEnv*); |
| 63 | extern int register_android_graphics_PathEffect(JNIEnv* env); |
| 64 | extern int register_android_graphics_Region(JNIEnv* env); |
| 65 | extern int register_android_graphics_Shader(JNIEnv* env); |
| 66 | extern int register_android_graphics_Typeface(JNIEnv* env); |
| 67 | |
| 68 | extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env); |
| 69 | extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env); |
| Jack Palevich | 1c4907e | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 70 | extern int register_android_opengl_jni_GLES10(JNIEnv* env); |
| 71 | extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env); |
| 72 | extern int register_android_opengl_jni_GLES11(JNIEnv* env); |
| 73 | extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
| 75 | extern int register_android_hardware_Camera(JNIEnv *env); |
| 76 | |
| 77 | extern int register_android_hardware_SensorManager(JNIEnv *env); |
| 78 | |
| 79 | extern int register_android_media_AudioRecord(JNIEnv *env); |
| 80 | extern int register_android_media_AudioSystem(JNIEnv *env); |
| 81 | extern int register_android_media_AudioTrack(JNIEnv *env); |
| 82 | extern int register_android_media_JetPlayer(JNIEnv *env); |
| 83 | extern int register_android_media_ToneGenerator(JNIEnv *env); |
| 84 | |
| 85 | extern int register_android_message_digest_sha1(JNIEnv *env); |
| 86 | |
| 87 | extern int register_android_util_FloatMath(JNIEnv* env); |
| 88 | |
| 89 | namespace android { |
| 90 | |
| 91 | /* |
| 92 | * JNI-based registration functions. Note these are properly contained in |
| 93 | * namespace android. |
| 94 | */ |
| 95 | extern int register_android_content_AssetManager(JNIEnv* env); |
| 96 | extern int register_android_util_EventLog(JNIEnv* env); |
| 97 | extern int register_android_util_Log(JNIEnv* env); |
| 98 | extern int register_android_content_StringBlock(JNIEnv* env); |
| 99 | extern int register_android_content_XmlBlock(JNIEnv* env); |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 100 | extern int register_android_emoji_EmojiFactory(JNIEnv* env); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | extern int register_android_graphics_Canvas(JNIEnv* env); |
| 102 | extern int register_android_graphics_ColorFilter(JNIEnv* env); |
| 103 | extern int register_android_graphics_DrawFilter(JNIEnv* env); |
| 104 | extern int register_android_graphics_Matrix(JNIEnv* env); |
| 105 | extern int register_android_graphics_Paint(JNIEnv* env); |
| 106 | extern int register_android_graphics_Path(JNIEnv* env); |
| 107 | extern int register_android_graphics_PathMeasure(JNIEnv* env); |
| 108 | extern int register_android_graphics_Picture(JNIEnv*); |
| 109 | extern int register_android_graphics_PorterDuff(JNIEnv* env); |
| 110 | extern int register_android_graphics_Rasterizer(JNIEnv* env); |
| 111 | extern int register_android_graphics_Xfermode(JNIEnv* env); |
| 112 | extern int register_android_graphics_PixelFormat(JNIEnv* env); |
| 113 | extern int register_com_android_internal_graphics_NativeUtils(JNIEnv *env); |
| 114 | extern int register_android_view_Display(JNIEnv* env); |
| 115 | extern int register_android_view_Surface(JNIEnv* env); |
| 116 | extern int register_android_view_ViewRoot(JNIEnv* env); |
| 117 | extern int register_android_database_CursorWindow(JNIEnv* env); |
| 118 | extern int register_android_database_SQLiteDatabase(JNIEnv* env); |
| 119 | extern int register_android_database_SQLiteDebug(JNIEnv* env); |
| 120 | extern int register_android_database_SQLiteProgram(JNIEnv* env); |
| 121 | extern int register_android_database_SQLiteQuery(JNIEnv* env); |
| 122 | extern int register_android_database_SQLiteStatement(JNIEnv* env); |
| 123 | extern int register_android_debug_JNITest(JNIEnv* env); |
| 124 | extern int register_android_nio_utils(JNIEnv* env); |
| 125 | extern int register_android_pim_EventRecurrence(JNIEnv* env); |
| 126 | extern int register_android_text_format_Time(JNIEnv* env); |
| 127 | extern int register_android_os_Debug(JNIEnv* env); |
| 128 | extern int register_android_os_ParcelFileDescriptor(JNIEnv *env); |
| 129 | extern int register_android_os_Power(JNIEnv *env); |
| 130 | extern int register_android_os_StatFs(JNIEnv *env); |
| 131 | extern int register_android_os_SystemProperties(JNIEnv *env); |
| 132 | extern int register_android_os_Hardware(JNIEnv* env); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | extern int register_android_os_SystemClock(JNIEnv* env); |
| 134 | extern int register_android_os_FileObserver(JNIEnv *env); |
| 135 | extern int register_android_os_FileUtils(JNIEnv *env); |
| 136 | extern int register_android_os_UEventObserver(JNIEnv* env); |
| 137 | extern int register_android_os_MemoryFile(JNIEnv* env); |
| 138 | extern int register_android_net_LocalSocketImpl(JNIEnv* env); |
| 139 | extern int register_android_net_NetworkUtils(JNIEnv* env); |
| 140 | extern int register_android_net_wifi_WifiManager(JNIEnv* env); |
| 141 | extern int register_android_security_Md5MessageDigest(JNIEnv *env); |
| 142 | extern int register_android_text_AndroidCharacter(JNIEnv *env); |
| 143 | extern int register_android_text_KeyCharacterMap(JNIEnv *env); |
| 144 | extern int register_android_opengl_classes(JNIEnv *env); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | extern int register_android_bluetooth_HeadsetBase(JNIEnv* env); |
| 146 | extern int register_android_bluetooth_BluetoothAudioGateway(JNIEnv* env); |
| Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 147 | extern int register_android_bluetooth_BluetoothSocket(JNIEnv *env); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | extern int register_android_bluetooth_ScoSocket(JNIEnv *env); |
| 149 | extern int register_android_server_BluetoothDeviceService(JNIEnv* env); |
| 150 | extern int register_android_server_BluetoothEventLoop(JNIEnv *env); |
| 151 | extern int register_android_server_BluetoothA2dpService(JNIEnv* env); |
| 152 | extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env); |
| 153 | extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env); |
| 154 | extern int register_android_util_Base64(JNIEnv* env); |
| 155 | extern int register_android_location_GpsLocationProvider(JNIEnv* env); |
| Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 156 | extern int register_android_backup_BackupDataInput(JNIEnv *env); |
| Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 157 | extern int register_android_backup_BackupDataOutput(JNIEnv *env); |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 158 | extern int register_android_backup_FileBackupHelper(JNIEnv *env); |
| Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 159 | extern int register_android_backup_RestoreHelperBase(JNIEnv *env); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | |
| 161 | static AndroidRuntime* gCurRuntime = NULL; |
| 162 | |
| 163 | static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL) |
| 164 | { |
| 165 | if (jniThrowException(env, exc, msg) != 0) |
| 166 | assert(false); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Code written in the Java Programming Language calls here from main(). |
| 171 | */ |
| 172 | static void com_android_internal_os_RuntimeInit_finishInit(JNIEnv* env, jobject clazz) |
| 173 | { |
| 174 | gCurRuntime->onStarted(); |
| 175 | } |
| 176 | |
| 177 | static void com_android_internal_os_RuntimeInit_zygoteInit(JNIEnv* env, jobject clazz) |
| 178 | { |
| 179 | gCurRuntime->onZygoteInit(); |
| 180 | } |
| 181 | |
| 182 | static jint com_android_internal_os_RuntimeInit_isComputerOn(JNIEnv* env, jobject clazz) |
| 183 | { |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | static void com_android_internal_os_RuntimeInit_turnComputerOn(JNIEnv* env, jobject clazz) |
| 188 | { |
| 189 | } |
| 190 | |
| 191 | static jint com_android_internal_os_RuntimeInit_getQwertyKeyboard(JNIEnv* env, jobject clazz) |
| 192 | { |
| 193 | char* value = getenv("qwerty"); |
| 194 | if (value != NULL && strcmp(value, "true") == 0) { |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * JNI registration. |
| 203 | */ |
| 204 | static JNINativeMethod gMethods[] = { |
| 205 | { "finishInit", "()V", |
| 206 | (void*) com_android_internal_os_RuntimeInit_finishInit }, |
| 207 | { "zygoteInitNative", "()V", |
| 208 | (void*) com_android_internal_os_RuntimeInit_zygoteInit }, |
| 209 | { "isComputerOn", "()I", |
| 210 | (void*) com_android_internal_os_RuntimeInit_isComputerOn }, |
| 211 | { "turnComputerOn", "()V", |
| 212 | (void*) com_android_internal_os_RuntimeInit_turnComputerOn }, |
| 213 | { "getQwertyKeyboard", "()I", |
| 214 | (void*) com_android_internal_os_RuntimeInit_getQwertyKeyboard }, |
| 215 | }; |
| 216 | |
| 217 | int register_com_android_internal_os_RuntimeInit(JNIEnv* env) |
| 218 | { |
| 219 | return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit", |
| 220 | gMethods, NELEM(gMethods)); |
| 221 | } |
| 222 | |
| 223 | // ---------------------------------------------------------------------- |
| 224 | |
| 225 | /*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL; |
| 226 | |
| 227 | |
| 228 | AndroidRuntime::AndroidRuntime() |
| 229 | { |
| The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 230 | SkGraphics::Init(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | // this sets our preference for 16bit images during decode |
| 232 | // in case the src is opaque and 24bit |
| 233 | SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config); |
| Mike Reed | fc8db53 | 2009-04-27 11:43:30 -0400 | [diff] [blame] | 234 | // This cache is shared between browser native images, and java "purgeable" |
| 235 | // bitmaps. This globalpool is for images that do not either use the java |
| 236 | // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key |
| 237 | // java call site. |
| 238 | SkImageRef_GlobalPool::SetRAMBudget(512 * 1024); |
| 239 | // There is also a global font cache, but its budget is specified in code |
| 240 | // see SkFontHost_android.cpp |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | |
| 242 | // Pre-allocate enough space to hold a fair number of options. |
| 243 | mOptions.setCapacity(20); |
| 244 | |
| 245 | assert(gCurRuntime == NULL); // one per process |
| 246 | gCurRuntime = this; |
| 247 | } |
| 248 | |
| 249 | AndroidRuntime::~AndroidRuntime() |
| 250 | { |
| 251 | SkGraphics::Term(); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Register native methods using JNI. |
| 256 | */ |
| 257 | /*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env, |
| 258 | const char* className, const JNINativeMethod* gMethods, int numMethods) |
| 259 | { |
| 260 | return jniRegisterNativeMethods(env, className, gMethods, numMethods); |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Call a static Java Programming Language function that takes no arguments and returns void. |
| 265 | */ |
| 266 | status_t AndroidRuntime::callStatic(const char* className, const char* methodName) |
| 267 | { |
| 268 | JNIEnv* env; |
| 269 | jclass clazz; |
| 270 | jmethodID methodId; |
| 271 | |
| 272 | env = getJNIEnv(); |
| 273 | if (env == NULL) |
| 274 | return UNKNOWN_ERROR; |
| 275 | |
| 276 | clazz = findClass(env, className); |
| 277 | if (clazz == NULL) { |
| 278 | LOGE("ERROR: could not find class '%s'\n", className); |
| 279 | return UNKNOWN_ERROR; |
| 280 | } |
| 281 | methodId = env->GetStaticMethodID(clazz, methodName, "()V"); |
| 282 | if (methodId == NULL) { |
| 283 | LOGE("ERROR: could not find method %s.%s\n", className, methodName); |
| 284 | return UNKNOWN_ERROR; |
| 285 | } |
| 286 | |
| 287 | env->CallStaticVoidMethod(clazz, methodId); |
| 288 | |
| 289 | return NO_ERROR; |
| 290 | } |
| 291 | |
| 292 | status_t AndroidRuntime::callMain( |
| 293 | const char* className, int argc, const char* const argv[]) |
| 294 | { |
| 295 | JNIEnv* env; |
| 296 | jclass clazz; |
| 297 | jmethodID methodId; |
| 298 | |
| 299 | env = getJNIEnv(); |
| 300 | if (env == NULL) |
| 301 | return UNKNOWN_ERROR; |
| 302 | |
| 303 | clazz = findClass(env, className); |
| 304 | if (clazz == NULL) { |
| 305 | LOGE("ERROR: could not find class '%s'\n", className); |
| 306 | return UNKNOWN_ERROR; |
| 307 | } |
| 308 | |
| 309 | methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V"); |
| 310 | if (methodId == NULL) { |
| 311 | LOGE("ERROR: could not find method %s.main(String[])\n", className); |
| 312 | return UNKNOWN_ERROR; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * We want to call main() with a String array with our arguments in it. |
| 317 | * Create an array and populate it. |
| 318 | */ |
| 319 | jclass stringClass; |
| 320 | jobjectArray strArray; |
| 321 | |
| 322 | stringClass = env->FindClass("java/lang/String"); |
| 323 | strArray = env->NewObjectArray(argc, stringClass, NULL); |
| 324 | |
| 325 | for (int i = 0; i < argc; i++) { |
| 326 | jstring argStr = env->NewStringUTF(argv[i]); |
| 327 | env->SetObjectArrayElement(strArray, i, argStr); |
| 328 | } |
| 329 | |
| 330 | env->CallStaticVoidMethod(clazz, methodId, strArray); |
| 331 | return NO_ERROR; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Find the named class. |
| 336 | */ |
| 337 | jclass AndroidRuntime::findClass(JNIEnv* env, const char* className) |
| 338 | { |
| 339 | char* convName = NULL; |
| 340 | |
| 341 | if (env->ExceptionCheck()) { |
| 342 | LOGE("ERROR: exception pending on entry to findClass()\n"); |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * JNI FindClass uses class names with slashes, but ClassLoader.loadClass |
| 348 | * uses the dotted "binary name" format. We don't need to convert the |
| 349 | * name with the new approach. |
| 350 | */ |
| 351 | #if 0 |
| 352 | /* (convName only created if necessary -- use className) */ |
| 353 | for (char* cp = const_cast<char*>(className); *cp != '\0'; cp++) { |
| 354 | if (*cp == '.') { |
| 355 | if (convName == NULL) { |
| 356 | convName = strdup(className); |
| 357 | cp = convName + (cp-className); |
| 358 | className = convName; |
| 359 | } |
| 360 | *cp = '/'; |
| 361 | } |
| 362 | } |
| 363 | #endif |
| 364 | |
| 365 | /* |
| 366 | * This is a little awkward because the JNI FindClass call uses the |
| 367 | * class loader associated with the native method we're executing in. |
| 368 | * Because this native method is part of a "boot" class, JNI doesn't |
| 369 | * look for the class in CLASSPATH, which unfortunately is a likely |
| 370 | * location for it. (Had we issued the FindClass call before calling |
| 371 | * into the VM -- at which point there isn't a native method frame on |
| 372 | * the stack -- the VM would have checked CLASSPATH. We have to do |
| 373 | * this because we call into Java Programming Language code and |
| 374 | * bounce back out.) |
| 375 | * |
| 376 | * JNI lacks a "find class in a specific class loader" operation, so we |
| 377 | * have to do things the hard way. |
| 378 | */ |
| 379 | jclass cls = NULL; |
| 380 | //cls = env->FindClass(className); |
| 381 | |
| 382 | jclass javaLangClassLoader; |
| 383 | jmethodID getSystemClassLoader, loadClass; |
| 384 | jobject systemClassLoader; |
| 385 | jstring strClassName; |
| 386 | |
| 387 | /* find the "system" class loader; none of this is expected to fail */ |
| 388 | javaLangClassLoader = env->FindClass("java/lang/ClassLoader"); |
| 389 | assert(javaLangClassLoader != NULL); |
| 390 | getSystemClassLoader = env->GetStaticMethodID(javaLangClassLoader, |
| 391 | "getSystemClassLoader", "()Ljava/lang/ClassLoader;"); |
| 392 | loadClass = env->GetMethodID(javaLangClassLoader, |
| 393 | "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); |
| 394 | assert(getSystemClassLoader != NULL && loadClass != NULL); |
| 395 | systemClassLoader = env->CallStaticObjectMethod(javaLangClassLoader, |
| 396 | getSystemClassLoader); |
| 397 | assert(systemClassLoader != NULL); |
| 398 | |
| 399 | /* create an object for the class name string; alloc could fail */ |
| 400 | strClassName = env->NewStringUTF(className); |
| 401 | if (env->ExceptionCheck()) { |
| 402 | LOGE("ERROR: unable to convert '%s' to string\n", className); |
| 403 | goto bail; |
| 404 | } |
| 405 | LOGV("system class loader is %p, loading %p (%s)\n", |
| 406 | systemClassLoader, strClassName, className); |
| 407 | |
| 408 | /* try to find the named class */ |
| 409 | cls = (jclass) env->CallObjectMethod(systemClassLoader, loadClass, |
| 410 | strClassName); |
| 411 | if (env->ExceptionCheck()) { |
| 412 | LOGE("ERROR: unable to load class '%s' from %p\n", |
| 413 | className, systemClassLoader); |
| 414 | cls = NULL; |
| 415 | goto bail; |
| 416 | } |
| 417 | |
| 418 | bail: |
| 419 | free(convName); |
| 420 | return cls; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * The VM calls this through the "exit" hook. |
| 425 | */ |
| 426 | static void runtime_exit(int code) |
| 427 | { |
| 428 | gCurRuntime->onExit(code); |
| 429 | exit(code); |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * The VM calls this through the "vfprintf" hook. |
| 434 | * |
| 435 | * We ignore "fp" and just write the results to the log file. |
| 436 | */ |
| 437 | static void runtime_vfprintf(FILE* fp, const char* format, va_list ap) |
| 438 | { |
| 439 | LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /** |
| 444 | * Add VM arguments to the to-be-executed VM |
| 445 | * Stops at first non '-' argument (also stops at an argument of '--') |
| 446 | * Returns the number of args consumed |
| 447 | */ |
| 448 | int AndroidRuntime::addVmArguments(int argc, const char* const argv[]) |
| 449 | { |
| 450 | int i; |
| 451 | |
| 452 | for (i = 0; i<argc; i++) { |
| 453 | if (argv[i][0] != '-') { |
| 454 | return i; |
| 455 | } |
| 456 | if (argv[i][1] == '-' && argv[i][2] == 0) { |
| 457 | return i+1; |
| 458 | } |
| 459 | |
| 460 | JavaVMOption opt; |
| 461 | memset(&opt, 0, sizeof(opt)); |
| 462 | opt.optionString = (char*)argv[i]; |
| 463 | mOptions.add(opt); |
| 464 | } |
| 465 | return i; |
| 466 | } |
| 467 | |
| 468 | static int hasDir(const char* dir) |
| 469 | { |
| 470 | struct stat s; |
| 471 | int res = stat(dir, &s); |
| 472 | if (res == 0) { |
| 473 | return S_ISDIR(s.st_mode); |
| 474 | } |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * We just want failed write() calls to just return with an error. |
| 480 | */ |
| 481 | static void blockSigpipe() |
| 482 | { |
| 483 | sigset_t mask; |
| 484 | |
| 485 | sigemptyset(&mask); |
| 486 | sigaddset(&mask, SIGPIPE); |
| 487 | if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) |
| 488 | LOGW("WARNING: SIGPIPE not blocked\n"); |
| 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Read the persistent locale. |
| 493 | */ |
| 494 | static void readLocale(char* language, char* region) |
| 495 | { |
| 496 | char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX]; |
| 497 | |
| 498 | property_get("persist.sys.language", propLang, ""); |
| 499 | property_get("persist.sys.country", propRegn, ""); |
| 500 | if (*propLang == 0 && *propRegn == 0) { |
| 501 | /* Set to ro properties, default is en_US */ |
| 502 | property_get("ro.product.locale.language", propLang, "en"); |
| 503 | property_get("ro.product.locale.region", propRegn, "US"); |
| 504 | } |
| 505 | strncat(language, propLang, 2); |
| 506 | strncat(region, propRegn, 2); |
| 507 | //LOGD("language=%s region=%s\n", language, region); |
| 508 | } |
| 509 | |
| Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 510 | /* |
| 511 | * Start the Dalvik Virtual Machine. |
| 512 | * |
| 513 | * Various arguments, most determined by system properties, are passed in. |
| 514 | * The "mOptions" vector is updated. |
| 515 | * |
| 516 | * Returns 0 on success. |
| 517 | */ |
| 518 | int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | { |
| Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 520 | int result = -1; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | JavaVMInitArgs initArgs; |
| 522 | JavaVMOption opt; |
| 523 | char propBuf[PROPERTY_VALUE_MAX]; |
| 524 | char stackTraceFileBuf[PROPERTY_VALUE_MAX]; |
| 525 | char dexoptFlagsBuf[PROPERTY_VALUE_MAX]; |
| 526 | char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX]; |
| 527 | char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX]; |
| 528 | char* stackTraceFile = NULL; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | bool checkJni = false; |
| Andy McFadden | e2b23e1 | 2009-04-03 11:09:46 -0700 | [diff] [blame] | 530 | bool checkDexSum = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | bool logStdio = false; |
| Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame^] | 532 | enum { |
| 533 | kEMDefault, |
| 534 | kEMIntPortable, |
| 535 | kEMIntFast, |
| 536 | #if defined(WITH_JIT) |
| 537 | kEMJitCompiler, |
| 538 | #endif |
| 539 | } executionMode = kEMDefault; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | |
| 542 | property_get("dalvik.vm.checkjni", propBuf, ""); |
| 543 | if (strcmp(propBuf, "true") == 0) { |
| 544 | checkJni = true; |
| 545 | } else if (strcmp(propBuf, "false") != 0) { |
| 546 | /* property is neither true nor false; fall back on kernel parameter */ |
| 547 | property_get("ro.kernel.android.checkjni", propBuf, ""); |
| 548 | if (propBuf[0] == '1') { |
| 549 | checkJni = true; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | property_get("dalvik.vm.execution-mode", propBuf, ""); |
| 554 | if (strcmp(propBuf, "int:portable") == 0) { |
| 555 | executionMode = kEMIntPortable; |
| 556 | } else if (strcmp(propBuf, "int:fast") == 0) { |
| 557 | executionMode = kEMIntFast; |
| Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame^] | 558 | #if defined(WITH_JIT) |
| 559 | } else if (strcmp(propBuf, "int:jit") == 0) { |
| 560 | executionMode = kEMJitCompiler; |
| 561 | #endif |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, ""); |
| 565 | |
| Andy McFadden | e2b23e1 | 2009-04-03 11:09:46 -0700 | [diff] [blame] | 566 | property_get("dalvik.vm.check-dex-sum", propBuf, ""); |
| 567 | if (strcmp(propBuf, "true") == 0) { |
| 568 | checkDexSum = true; |
| 569 | } |
| 570 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | property_get("log.redirect-stdio", propBuf, ""); |
| 572 | if (strcmp(propBuf, "true") == 0) { |
| 573 | logStdio = true; |
| 574 | } |
| 575 | |
| 576 | strcpy(enableAssertBuf, "-ea:"); |
| 577 | property_get("dalvik.vm.enableassertions", enableAssertBuf+4, ""); |
| 578 | |
| 579 | strcpy(jniOptsBuf, "-Xjniopts:"); |
| 580 | property_get("dalvik.vm.jniopts", jniOptsBuf+10, ""); |
| 581 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | /* route exit() to our handler */ |
| 583 | opt.extraInfo = (void*) runtime_exit; |
| 584 | opt.optionString = "exit"; |
| 585 | mOptions.add(opt); |
| 586 | |
| 587 | /* route fprintf() to our handler */ |
| 588 | opt.extraInfo = (void*) runtime_vfprintf; |
| 589 | opt.optionString = "vfprintf"; |
| 590 | mOptions.add(opt); |
| 591 | |
| 592 | opt.extraInfo = NULL; |
| 593 | |
| 594 | /* enable verbose; standard options are { jni, gc, class } */ |
| 595 | //options[curOpt++].optionString = "-verbose:jni"; |
| 596 | opt.optionString = "-verbose:gc"; |
| 597 | mOptions.add(opt); |
| 598 | //options[curOpt++].optionString = "-verbose:class"; |
| 599 | |
| 600 | #ifdef CUSTOM_RUNTIME_HEAP_MAX |
| 601 | #define __make_max_heap_opt(val) #val |
| 602 | #define _make_max_heap_opt(val) "-Xmx" __make_max_heap_opt(val) |
| 603 | opt.optionString = _make_max_heap_opt(CUSTOM_RUNTIME_HEAP_MAX); |
| 604 | #undef __make_max_heap_opt |
| 605 | #undef _make_max_heap_opt |
| 606 | #else |
| 607 | /* limit memory use to 16MB */ |
| 608 | opt.optionString = "-Xmx16m"; |
| 609 | #endif |
| 610 | mOptions.add(opt); |
| 611 | |
| 612 | /* |
| 613 | * Enable or disable dexopt features, such as bytecode verification and |
| 614 | * calculation of register maps for precise GC. |
| 615 | */ |
| 616 | property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, ""); |
| 617 | if (dexoptFlagsBuf[0] != '\0') { |
| 618 | const char* opc; |
| 619 | const char* val; |
| 620 | |
| 621 | opc = strstr(dexoptFlagsBuf, "v="); /* verification */ |
| 622 | if (opc != NULL) { |
| 623 | switch (*(opc+2)) { |
| 624 | case 'n': val = "-Xverify:none"; break; |
| 625 | case 'r': val = "-Xverify:remote"; break; |
| 626 | case 'a': val = "-Xverify:all"; break; |
| 627 | default: val = NULL; break; |
| 628 | } |
| 629 | |
| 630 | if (val != NULL) { |
| 631 | opt.optionString = val; |
| 632 | mOptions.add(opt); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | opc = strstr(dexoptFlagsBuf, "o="); /* optimization */ |
| 637 | if (opc != NULL) { |
| 638 | switch (*(opc+2)) { |
| 639 | case 'n': val = "-Xdexopt:none"; break; |
| 640 | case 'v': val = "-Xdexopt:verified"; break; |
| 641 | case 'a': val = "-Xdexopt:all"; break; |
| 642 | default: val = NULL; break; |
| 643 | } |
| 644 | |
| 645 | if (val != NULL) { |
| 646 | opt.optionString = val; |
| 647 | mOptions.add(opt); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */ |
| 652 | if (opc != NULL) { |
| 653 | opt.optionString = "-Xgenregmap"; |
| 654 | mOptions.add(opt); |
| The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 655 | |
| 656 | /* turn on precise GC while we're at it */ |
| 657 | opt.optionString = "-Xgc:precise"; |
| 658 | mOptions.add(opt); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | /* enable debugging; set suspend=y to pause during VM init */ |
| 663 | #ifdef HAVE_ANDROID_OS |
| 664 | /* use android ADB transport */ |
| 665 | opt.optionString = |
| 666 | "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y"; |
| 667 | #else |
| 668 | /* use TCP socket; address=0 means start at port 8000 and probe up */ |
| 669 | LOGI("Using TCP socket for JDWP\n"); |
| 670 | opt.optionString = |
| 671 | "-agentlib:jdwp=transport=dt_socket,suspend=n,server=y,address=0"; |
| 672 | #endif |
| 673 | mOptions.add(opt); |
| 674 | |
| 675 | char enableDPBuf[sizeof("-Xdeadlockpredict:") + PROPERTY_VALUE_MAX]; |
| 676 | property_get("dalvik.vm.deadlock-predict", propBuf, ""); |
| 677 | if (strlen(propBuf) > 0) { |
| 678 | strcpy(enableDPBuf, "-Xdeadlockpredict:"); |
| 679 | strcat(enableDPBuf, propBuf); |
| 680 | opt.optionString = enableDPBuf; |
| 681 | mOptions.add(opt); |
| 682 | } |
| 683 | |
| 684 | LOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF"); |
| 685 | if (checkJni) { |
| 686 | /* extended JNI checking */ |
| 687 | opt.optionString = "-Xcheck:jni"; |
| 688 | mOptions.add(opt); |
| 689 | |
| 690 | /* set a cap on JNI global references */ |
| 691 | opt.optionString = "-Xjnigreflimit:2000"; |
| 692 | mOptions.add(opt); |
| 693 | |
| 694 | /* with -Xcheck:jni, this provides a JNI function call trace */ |
| 695 | //opt.optionString = "-verbose:jni"; |
| 696 | //mOptions.add(opt); |
| 697 | } |
| Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame^] | 698 | |
| 699 | #if defined(WITH_JIT) |
| 700 | /* Minimal profile threshold to trigger JIT compilation */ |
| 701 | char jitThresholdBuf[sizeof("-Xthreshold:") + PROPERTY_VALUE_MAX]; |
| 702 | property_get("dalvik.vm.jit.threshold", propBuf, ""); |
| 703 | if (strlen(propBuf) > 0) { |
| 704 | strcpy(jitThresholdBuf, "-Xthreshold:"); |
| 705 | strcat(jitThresholdBuf, propBuf); |
| 706 | opt.optionString = jitThresholdBuf; |
| 707 | mOptions.add(opt); |
| 708 | } |
| 709 | |
| 710 | /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */ |
| 711 | char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX]; |
| 712 | property_get("dalvik.vm.jit.op", propBuf, ""); |
| 713 | if (strlen(propBuf) > 0) { |
| 714 | strcpy(jitOpBuf, "-Xjitop:"); |
| 715 | strcat(jitOpBuf, propBuf); |
| 716 | opt.optionString = jitOpBuf; |
| 717 | mOptions.add(opt); |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Reverse the polarity of dalvik.vm.jit.op and force interpreter-only |
| 722 | * for non-selected opcodes. |
| 723 | */ |
| 724 | property_get("dalvik.vm.jit.includeselectedop", propBuf, ""); |
| 725 | if (strlen(propBuf) > 0) { |
| 726 | opt.optionString = "-Xincludeselectedop"; |
| 727 | mOptions.add(opt); |
| 728 | } |
| 729 | |
| 730 | /* Force interpreter-only mode for selected methods */ |
| 731 | char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX]; |
| 732 | property_get("dalvik.vm.jit.method", propBuf, ""); |
| 733 | if (strlen(propBuf) > 0) { |
| 734 | strcpy(jitMethodBuf, "-Xjitmethod:"); |
| 735 | strcat(jitMethodBuf, propBuf); |
| 736 | opt.optionString = jitMethodBuf; |
| 737 | mOptions.add(opt); |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Reverse the polarity of dalvik.vm.jit.method and force interpreter-only |
| 742 | * for non-selected methods. |
| 743 | */ |
| 744 | property_get("dalvik.vm.jit.includeselectedmethod", propBuf, ""); |
| 745 | if (strlen(propBuf) > 0) { |
| 746 | opt.optionString = "-Xincludeselectedmethod"; |
| 747 | mOptions.add(opt); |
| 748 | } |
| 749 | #endif |
| 750 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | if (executionMode == kEMIntPortable) { |
| 752 | opt.optionString = "-Xint:portable"; |
| 753 | mOptions.add(opt); |
| 754 | } else if (executionMode == kEMIntFast) { |
| 755 | opt.optionString = "-Xint:fast"; |
| 756 | mOptions.add(opt); |
| Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame^] | 757 | #if defined(WITH_JIT) |
| 758 | } else if (executionMode == kEMJitCompiler) { |
| 759 | opt.optionString = "-Xint:jit"; |
| 760 | mOptions.add(opt); |
| 761 | #endif |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | } |
| Andy McFadden | e2b23e1 | 2009-04-03 11:09:46 -0700 | [diff] [blame] | 763 | |
| 764 | if (checkDexSum) { |
| 765 | /* perform additional DEX checksum tests */ |
| 766 | opt.optionString = "-Xcheckdexsum"; |
| 767 | mOptions.add(opt); |
| 768 | } |
| 769 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | if (logStdio) { |
| 771 | /* convert stdout/stderr to log messages */ |
| 772 | opt.optionString = "-Xlog-stdio"; |
| 773 | mOptions.add(opt); |
| 774 | } |
| 775 | |
| 776 | if (enableAssertBuf[4] != '\0') { |
| 777 | /* accept "all" to mean "all classes and packages" */ |
| 778 | if (strcmp(enableAssertBuf+4, "all") == 0) |
| 779 | enableAssertBuf[3] = '\0'; |
| 780 | LOGI("Assertions enabled: '%s'\n", enableAssertBuf); |
| 781 | opt.optionString = enableAssertBuf; |
| 782 | mOptions.add(opt); |
| 783 | } else { |
| 784 | LOGV("Assertions disabled\n"); |
| 785 | } |
| 786 | |
| 787 | if (jniOptsBuf[10] != '\0') { |
| 788 | LOGI("JNI options: '%s'\n", jniOptsBuf); |
| 789 | opt.optionString = jniOptsBuf; |
| 790 | mOptions.add(opt); |
| 791 | } |
| 792 | |
| 793 | if (stackTraceFileBuf[0] != '\0') { |
| 794 | static const char* stfOptName = "-Xstacktracefile:"; |
| 795 | |
| 796 | stackTraceFile = (char*) malloc(strlen(stfOptName) + |
| 797 | strlen(stackTraceFileBuf) +1); |
| 798 | strcpy(stackTraceFile, stfOptName); |
| 799 | strcat(stackTraceFile, stackTraceFileBuf); |
| 800 | opt.optionString = stackTraceFile; |
| 801 | mOptions.add(opt); |
| 802 | } |
| 803 | |
| 804 | /* Set the properties for locale */ |
| 805 | { |
| 806 | char langOption[sizeof("-Duser.language=") + 3]; |
| 807 | char regionOption[sizeof("-Duser.region=") + 3]; |
| 808 | strcpy(langOption, "-Duser.language="); |
| 809 | strcpy(regionOption, "-Duser.region="); |
| 810 | readLocale(langOption, regionOption); |
| 811 | opt.extraInfo = NULL; |
| 812 | opt.optionString = langOption; |
| 813 | mOptions.add(opt); |
| 814 | opt.optionString = regionOption; |
| 815 | mOptions.add(opt); |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * We don't have /tmp on the device, but we often have an SD card. Apps |
| 820 | * shouldn't use this, but some test suites might want to exercise it. |
| 821 | */ |
| 822 | opt.optionString = "-Djava.io.tmpdir=/sdcard"; |
| 823 | mOptions.add(opt); |
| 824 | |
| 825 | initArgs.version = JNI_VERSION_1_4; |
| 826 | initArgs.options = mOptions.editArray(); |
| 827 | initArgs.nOptions = mOptions.size(); |
| 828 | initArgs.ignoreUnrecognized = JNI_FALSE; |
| 829 | |
| 830 | /* |
| 831 | * Initialize the VM. |
| 832 | * |
| 833 | * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread. |
| 834 | * If this call succeeds, the VM is ready, and we can start issuing |
| 835 | * JNI calls. |
| 836 | */ |
| Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 837 | if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | LOGE("JNI_CreateJavaVM failed\n"); |
| 839 | goto bail; |
| 840 | } |
| 841 | |
| Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 842 | result = 0; |
| 843 | |
| 844 | bail: |
| 845 | free(stackTraceFile); |
| 846 | return result; |
| 847 | } |
| 848 | |
| 849 | /* |
| 850 | * Start the Android runtime. This involves starting the virtual machine |
| 851 | * and calling the "static void main(String[] args)" method in the class |
| 852 | * named by "className". |
| 853 | */ |
| 854 | void AndroidRuntime::start(const char* className, const bool startSystemServer) |
| 855 | { |
| 856 | LOGD("\n>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<\n"); |
| 857 | |
| 858 | char* slashClassName = NULL; |
| 859 | char* cp; |
| 860 | JNIEnv* env; |
| 861 | |
| 862 | blockSigpipe(); |
| 863 | |
| 864 | /* |
| 865 | * 'startSystemServer == true' means runtime is obslete and not run from |
| 866 | * init.rc anymore, so we print out the boot start event here. |
| 867 | */ |
| 868 | if (startSystemServer) { |
| 869 | /* track our progress through the boot sequence */ |
| 870 | const int LOG_BOOT_PROGRESS_START = 3000; |
| 871 | LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, |
| 872 | ns2ms(systemTime(SYSTEM_TIME_MONOTONIC))); |
| 873 | } |
| 874 | |
| 875 | const char* rootDir = getenv("ANDROID_ROOT"); |
| 876 | if (rootDir == NULL) { |
| 877 | rootDir = "/system"; |
| 878 | if (!hasDir("/system")) { |
| 879 | LOG_FATAL("No root directory specified, and /android does not exist."); |
| 880 | goto bail; |
| 881 | } |
| 882 | setenv("ANDROID_ROOT", rootDir, 1); |
| 883 | } |
| 884 | |
| 885 | //const char* kernelHack = getenv("LD_ASSUME_KERNEL"); |
| 886 | //LOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack); |
| 887 | |
| 888 | /* start the virtual machine */ |
| 889 | if (startVm(&mJavaVM, &env) != 0) |
| 890 | goto bail; |
| 891 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | /* |
| 893 | * Register android functions. |
| 894 | */ |
| 895 | if (startReg(env) < 0) { |
| 896 | LOGE("Unable to register all android natives\n"); |
| 897 | goto bail; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * We want to call main() with a String array with arguments in it. |
| 902 | * At present we only have one argument, the class name. Create an |
| 903 | * array to hold it. |
| 904 | */ |
| 905 | jclass stringClass; |
| 906 | jobjectArray strArray; |
| 907 | jstring classNameStr; |
| 908 | jstring startSystemServerStr; |
| 909 | |
| 910 | stringClass = env->FindClass("java/lang/String"); |
| 911 | assert(stringClass != NULL); |
| 912 | strArray = env->NewObjectArray(2, stringClass, NULL); |
| 913 | assert(strArray != NULL); |
| 914 | classNameStr = env->NewStringUTF(className); |
| 915 | assert(classNameStr != NULL); |
| 916 | env->SetObjectArrayElement(strArray, 0, classNameStr); |
| 917 | startSystemServerStr = env->NewStringUTF(startSystemServer ? |
| 918 | "true" : "false"); |
| 919 | env->SetObjectArrayElement(strArray, 1, startSystemServerStr); |
| 920 | |
| 921 | /* |
| 922 | * Start VM. This thread becomes the main thread of the VM, and will |
| 923 | * not return until the VM exits. |
| 924 | */ |
| 925 | jclass startClass; |
| 926 | jmethodID startMeth; |
| 927 | |
| 928 | slashClassName = strdup(className); |
| 929 | for (cp = slashClassName; *cp != '\0'; cp++) |
| 930 | if (*cp == '.') |
| 931 | *cp = '/'; |
| 932 | |
| 933 | startClass = env->FindClass(slashClassName); |
| 934 | if (startClass == NULL) { |
| 935 | LOGE("JavaVM unable to locate class '%s'\n", slashClassName); |
| 936 | /* keep going */ |
| 937 | } else { |
| 938 | startMeth = env->GetStaticMethodID(startClass, "main", |
| 939 | "([Ljava/lang/String;)V"); |
| 940 | if (startMeth == NULL) { |
| 941 | LOGE("JavaVM unable to find main() in '%s'\n", className); |
| 942 | /* keep going */ |
| 943 | } else { |
| 944 | env->CallStaticVoidMethod(startClass, startMeth, strArray); |
| 945 | |
| 946 | #if 0 |
| 947 | if (env->ExceptionCheck()) |
| 948 | threadExitUncaughtException(env); |
| 949 | #endif |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | LOGD("Shutting down VM\n"); |
| 954 | if (mJavaVM->DetachCurrentThread() != JNI_OK) |
| 955 | LOGW("Warning: unable to detach main thread\n"); |
| 956 | if (mJavaVM->DestroyJavaVM() != 0) |
| 957 | LOGW("Warning: VM did not shut down cleanly\n"); |
| 958 | |
| 959 | bail: |
| 960 | free(slashClassName); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | void AndroidRuntime::start() |
| 964 | { |
| 965 | start("com.android.internal.os.RuntimeInit", |
| 966 | false /* Don't start the system server */); |
| 967 | } |
| 968 | |
| 969 | void AndroidRuntime::onExit(int code) |
| 970 | { |
| 971 | LOGI("AndroidRuntime onExit calling exit(%d)", code); |
| 972 | exit(code); |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Get the JNIEnv pointer for this thread. |
| 977 | * |
| 978 | * Returns NULL if the slot wasn't allocated or populated. |
| 979 | */ |
| 980 | /*static*/ JNIEnv* AndroidRuntime::getJNIEnv() |
| 981 | { |
| 982 | JNIEnv* env; |
| 983 | JavaVM* vm = AndroidRuntime::getJavaVM(); |
| 984 | assert(vm != NULL); |
| 985 | |
| 986 | if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) |
| 987 | return NULL; |
| 988 | return env; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Makes the current thread visible to the VM. |
| 993 | * |
| 994 | * The JNIEnv pointer returned is only valid for the current thread, and |
| 995 | * thus must be tucked into thread-local storage. |
| 996 | */ |
| 997 | static int javaAttachThread(const char* threadName, JNIEnv** pEnv) |
| 998 | { |
| 999 | JavaVMAttachArgs args; |
| 1000 | JavaVM* vm; |
| 1001 | jint result; |
| 1002 | |
| 1003 | vm = AndroidRuntime::getJavaVM(); |
| 1004 | assert(vm != NULL); |
| 1005 | |
| 1006 | args.version = JNI_VERSION_1_4; |
| 1007 | args.name = (char*) threadName; |
| 1008 | args.group = NULL; |
| 1009 | |
| 1010 | result = vm->AttachCurrentThread(pEnv, (void*) &args); |
| 1011 | if (result != JNI_OK) |
| 1012 | LOGE("ERROR: thread attach failed\n"); |
| 1013 | |
| 1014 | return result; |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * Detach the current thread from the set visible to the VM. |
| 1019 | */ |
| 1020 | static int javaDetachThread(void) |
| 1021 | { |
| 1022 | JavaVM* vm; |
| 1023 | jint result; |
| 1024 | |
| 1025 | vm = AndroidRuntime::getJavaVM(); |
| 1026 | assert(vm != NULL); |
| 1027 | |
| 1028 | result = vm->DetachCurrentThread(); |
| 1029 | if (result != JNI_OK) |
| 1030 | LOGE("ERROR: thread detach failed\n"); |
| 1031 | return result; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * When starting a native thread that will be visible from the VM, we |
| 1036 | * bounce through this to get the right attach/detach action. |
| 1037 | * Note that this function calls free(args) |
| 1038 | */ |
| 1039 | /*static*/ int AndroidRuntime::javaThreadShell(void* args) { |
| 1040 | void* start = ((void**)args)[0]; |
| 1041 | void* userData = ((void **)args)[1]; |
| 1042 | char* name = (char*) ((void **)args)[2]; // we own this storage |
| 1043 | free(args); |
| 1044 | JNIEnv* env; |
| 1045 | int result; |
| 1046 | |
| 1047 | /* hook us into the VM */ |
| 1048 | if (javaAttachThread(name, &env) != JNI_OK) |
| 1049 | return -1; |
| 1050 | |
| 1051 | /* start the thread running */ |
| 1052 | result = (*(android_thread_func_t)start)(userData); |
| 1053 | |
| 1054 | /* unhook us */ |
| 1055 | javaDetachThread(); |
| 1056 | free(name); |
| 1057 | |
| 1058 | return result; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
| 1062 | * This is invoked from androidCreateThreadEtc() via the callback |
| 1063 | * set with androidSetCreateThreadFunc(). |
| 1064 | * |
| 1065 | * We need to create the new thread in such a way that it gets hooked |
| 1066 | * into the VM before it really starts executing. |
| 1067 | */ |
| 1068 | /*static*/ int AndroidRuntime::javaCreateThreadEtc( |
| 1069 | android_thread_func_t entryFunction, |
| 1070 | void* userData, |
| 1071 | const char* threadName, |
| 1072 | int32_t threadPriority, |
| 1073 | size_t threadStackSize, |
| 1074 | android_thread_id_t* threadId) |
| 1075 | { |
| 1076 | void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free |
| 1077 | int result; |
| 1078 | |
| 1079 | assert(threadName != NULL); |
| 1080 | |
| 1081 | args[0] = (void*) entryFunction; |
| 1082 | args[1] = userData; |
| 1083 | args[2] = (void*) strdup(threadName); // javaThreadShell must free |
| 1084 | |
| 1085 | result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args, |
| 1086 | threadName, threadPriority, threadStackSize, threadId); |
| 1087 | return result; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * Create a thread that is visible from the VM. |
| 1092 | * |
| 1093 | * This is called from elsewhere in the library. |
| 1094 | */ |
| 1095 | /*static*/ void AndroidRuntime::createJavaThread(const char* name, |
| 1096 | void (*start)(void *), void* arg) |
| 1097 | { |
| 1098 | javaCreateThreadEtc((android_thread_func_t) start, arg, name, |
| 1099 | ANDROID_PRIORITY_DEFAULT, 0, NULL); |
| 1100 | } |
| 1101 | |
| 1102 | #if 0 |
| 1103 | static void quickTest(void* arg) |
| 1104 | { |
| 1105 | const char* str = (const char*) arg; |
| 1106 | |
| 1107 | printf("In quickTest: %s\n", str); |
| 1108 | } |
| 1109 | #endif |
| 1110 | |
| 1111 | #ifdef NDEBUG |
| 1112 | #define REG_JNI(name) { name } |
| 1113 | struct RegJNIRec { |
| 1114 | int (*mProc)(JNIEnv*); |
| 1115 | }; |
| 1116 | #else |
| 1117 | #define REG_JNI(name) { name, #name } |
| 1118 | struct RegJNIRec { |
| 1119 | int (*mProc)(JNIEnv*); |
| 1120 | const char* mName; |
| 1121 | }; |
| 1122 | #endif |
| 1123 | |
| 1124 | typedef void (*RegJAMProc)(); |
| 1125 | |
| 1126 | static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env) |
| 1127 | { |
| 1128 | for (size_t i = 0; i < count; i++) { |
| 1129 | if (array[i].mProc(env) < 0) { |
| 1130 | #ifndef NDEBUG |
| 1131 | LOGD("----------!!! %s failed to load\n", array[i].mName); |
| 1132 | #endif |
| 1133 | return -1; |
| 1134 | } |
| 1135 | } |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | static void register_jam_procs(const RegJAMProc array[], size_t count) |
| 1140 | { |
| 1141 | for (size_t i = 0; i < count; i++) { |
| 1142 | array[i](); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | static const RegJNIRec gRegJNI[] = { |
| 1147 | REG_JNI(register_android_debug_JNITest), |
| 1148 | REG_JNI(register_com_android_internal_os_RuntimeInit), |
| 1149 | REG_JNI(register_android_os_SystemClock), |
| 1150 | REG_JNI(register_android_util_EventLog), |
| 1151 | REG_JNI(register_android_util_Log), |
| 1152 | REG_JNI(register_android_util_FloatMath), |
| 1153 | REG_JNI(register_android_text_format_Time), |
| 1154 | REG_JNI(register_android_pim_EventRecurrence), |
| 1155 | REG_JNI(register_android_content_AssetManager), |
| 1156 | REG_JNI(register_android_content_StringBlock), |
| 1157 | REG_JNI(register_android_content_XmlBlock), |
| The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1158 | REG_JNI(register_android_emoji_EmojiFactory), |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | REG_JNI(register_android_security_Md5MessageDigest), |
| 1160 | REG_JNI(register_android_text_AndroidCharacter), |
| 1161 | REG_JNI(register_android_text_KeyCharacterMap), |
| 1162 | REG_JNI(register_android_os_Process), |
| 1163 | REG_JNI(register_android_os_Binder), |
| 1164 | REG_JNI(register_android_os_Hardware), |
| 1165 | REG_JNI(register_android_view_Display), |
| 1166 | REG_JNI(register_android_nio_utils), |
| 1167 | REG_JNI(register_android_graphics_PixelFormat), |
| 1168 | REG_JNI(register_android_graphics_Graphics), |
| 1169 | REG_JNI(register_android_view_Surface), |
| 1170 | REG_JNI(register_android_view_ViewRoot), |
| 1171 | REG_JNI(register_com_google_android_gles_jni_EGLImpl), |
| 1172 | REG_JNI(register_com_google_android_gles_jni_GLImpl), |
| Jack Palevich | 1c4907e | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 1173 | REG_JNI(register_android_opengl_jni_GLES10), |
| 1174 | REG_JNI(register_android_opengl_jni_GLES10Ext), |
| 1175 | REG_JNI(register_android_opengl_jni_GLES11), |
| 1176 | REG_JNI(register_android_opengl_jni_GLES11Ext), |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1177 | |
| 1178 | REG_JNI(register_android_graphics_Bitmap), |
| 1179 | REG_JNI(register_android_graphics_BitmapFactory), |
| 1180 | REG_JNI(register_android_graphics_Camera), |
| 1181 | REG_JNI(register_android_graphics_Canvas), |
| 1182 | REG_JNI(register_android_graphics_ColorFilter), |
| 1183 | REG_JNI(register_android_graphics_DrawFilter), |
| 1184 | REG_JNI(register_android_graphics_Interpolator), |
| 1185 | REG_JNI(register_android_graphics_LayerRasterizer), |
| 1186 | REG_JNI(register_android_graphics_MaskFilter), |
| 1187 | REG_JNI(register_android_graphics_Matrix), |
| 1188 | REG_JNI(register_android_graphics_Movie), |
| 1189 | REG_JNI(register_android_graphics_NinePatch), |
| 1190 | REG_JNI(register_android_graphics_Paint), |
| 1191 | REG_JNI(register_android_graphics_Path), |
| 1192 | REG_JNI(register_android_graphics_PathMeasure), |
| 1193 | REG_JNI(register_android_graphics_PathEffect), |
| 1194 | REG_JNI(register_android_graphics_Picture), |
| 1195 | REG_JNI(register_android_graphics_PorterDuff), |
| 1196 | REG_JNI(register_android_graphics_Rasterizer), |
| 1197 | REG_JNI(register_android_graphics_Region), |
| 1198 | REG_JNI(register_android_graphics_Shader), |
| 1199 | REG_JNI(register_android_graphics_Typeface), |
| 1200 | REG_JNI(register_android_graphics_Xfermode), |
| 1201 | REG_JNI(register_com_android_internal_graphics_NativeUtils), |
| 1202 | |
| 1203 | REG_JNI(register_android_database_CursorWindow), |
| 1204 | REG_JNI(register_android_database_SQLiteDatabase), |
| 1205 | REG_JNI(register_android_database_SQLiteDebug), |
| 1206 | REG_JNI(register_android_database_SQLiteProgram), |
| 1207 | REG_JNI(register_android_database_SQLiteQuery), |
| 1208 | REG_JNI(register_android_database_SQLiteStatement), |
| 1209 | REG_JNI(register_android_os_Debug), |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | REG_JNI(register_android_os_FileObserver), |
| 1211 | REG_JNI(register_android_os_FileUtils), |
| 1212 | REG_JNI(register_android_os_ParcelFileDescriptor), |
| 1213 | REG_JNI(register_android_os_Power), |
| 1214 | REG_JNI(register_android_os_StatFs), |
| 1215 | REG_JNI(register_android_os_SystemProperties), |
| 1216 | REG_JNI(register_android_os_UEventObserver), |
| 1217 | REG_JNI(register_android_net_LocalSocketImpl), |
| 1218 | REG_JNI(register_android_net_NetworkUtils), |
| 1219 | REG_JNI(register_android_net_wifi_WifiManager), |
| 1220 | REG_JNI(register_android_os_MemoryFile), |
| 1221 | REG_JNI(register_com_android_internal_os_ZygoteInit), |
| 1222 | REG_JNI(register_android_hardware_Camera), |
| 1223 | REG_JNI(register_android_hardware_SensorManager), |
| 1224 | REG_JNI(register_android_media_AudioRecord), |
| 1225 | REG_JNI(register_android_media_AudioSystem), |
| 1226 | REG_JNI(register_android_media_AudioTrack), |
| 1227 | REG_JNI(register_android_media_JetPlayer), |
| 1228 | REG_JNI(register_android_media_ToneGenerator), |
| 1229 | |
| 1230 | REG_JNI(register_android_opengl_classes), |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1231 | REG_JNI(register_android_bluetooth_HeadsetBase), |
| 1232 | REG_JNI(register_android_bluetooth_BluetoothAudioGateway), |
| Nick Pelly | 0b6955a | 2009-05-26 19:13:43 -0700 | [diff] [blame] | 1233 | REG_JNI(register_android_bluetooth_BluetoothSocket), |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | REG_JNI(register_android_bluetooth_ScoSocket), |
| 1235 | REG_JNI(register_android_server_BluetoothDeviceService), |
| 1236 | REG_JNI(register_android_server_BluetoothEventLoop), |
| 1237 | REG_JNI(register_android_server_BluetoothA2dpService), |
| 1238 | REG_JNI(register_android_message_digest_sha1), |
| 1239 | REG_JNI(register_android_ddm_DdmHandleNativeHeap), |
| 1240 | REG_JNI(register_android_util_Base64), |
| 1241 | REG_JNI(register_android_location_GpsLocationProvider), |
| Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 1242 | REG_JNI(register_android_backup_BackupDataInput), |
| Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 1243 | REG_JNI(register_android_backup_BackupDataOutput), |
| Joe Onorato | b1a7ffe | 2009-05-06 18:06:21 -0700 | [diff] [blame] | 1244 | REG_JNI(register_android_backup_FileBackupHelper), |
| Joe Onorato | d2d9ceb | 2009-06-18 13:11:18 -0700 | [diff] [blame] | 1245 | REG_JNI(register_android_backup_RestoreHelperBase), |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | }; |
| 1247 | |
| 1248 | /* |
| 1249 | * Register android native functions with the VM. |
| 1250 | */ |
| 1251 | /*static*/ int AndroidRuntime::startReg(JNIEnv* env) |
| 1252 | { |
| 1253 | /* |
| 1254 | * This hook causes all future threads created in this process to be |
| 1255 | * attached to the JavaVM. (This needs to go away in favor of JNI |
| 1256 | * Attach calls.) |
| 1257 | */ |
| 1258 | androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc); |
| 1259 | |
| 1260 | LOGD("--- registering native functions ---\n"); |
| 1261 | |
| 1262 | /* |
| 1263 | * Every "register" function calls one or more things that return |
| 1264 | * a local reference (e.g. FindClass). Because we haven't really |
| 1265 | * started the VM yet, they're all getting stored in the base frame |
| 1266 | * and never released. Use Push/Pop to manage the storage. |
| 1267 | */ |
| 1268 | env->PushLocalFrame(200); |
| 1269 | |
| 1270 | if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) { |
| 1271 | env->PopLocalFrame(NULL); |
| 1272 | return -1; |
| 1273 | } |
| 1274 | env->PopLocalFrame(NULL); |
| 1275 | |
| 1276 | //createJavaThread("fubar", quickTest, (void*) "hello"); |
| 1277 | |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | AndroidRuntime* AndroidRuntime::getRuntime() |
| 1282 | { |
| 1283 | return gCurRuntime; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * Used by WithFramework to register native functions. |
| 1288 | */ |
| 1289 | extern "C" |
| 1290 | jint Java_com_android_internal_util_WithFramework_registerNatives( |
| 1291 | JNIEnv* env, jclass clazz) { |
| 1292 | return register_jni_procs(gRegJNI, NELEM(gRegJNI), env); |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * Used by LoadClass to register native functions. |
| 1297 | */ |
| 1298 | extern "C" |
| 1299 | jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) { |
| 1300 | return register_jni_procs(gRegJNI, NELEM(gRegJNI), env); |
| 1301 | } |
| 1302 | |
| 1303 | } // namespace android |