Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | |
| 17 | #define LOG_TAG "AndroidRuntime" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <android_runtime/AndroidRuntime.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/IBinder.h> |
Brad Fitzpatrick | 0bd5243 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 22 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 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*); |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 56 | extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | extern int register_android_graphics_Camera(JNIEnv* env); |
| 58 | extern int register_android_graphics_Graphics(JNIEnv* env); |
| 59 | extern int register_android_graphics_Interpolator(JNIEnv* env); |
| 60 | extern int register_android_graphics_LayerRasterizer(JNIEnv*); |
| 61 | extern int register_android_graphics_MaskFilter(JNIEnv* env); |
| 62 | extern int register_android_graphics_Movie(JNIEnv* env); |
| 63 | extern int register_android_graphics_NinePatch(JNIEnv*); |
| 64 | extern int register_android_graphics_PathEffect(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | extern int register_android_graphics_Shader(JNIEnv* env); |
| 66 | extern int register_android_graphics_Typeface(JNIEnv* env); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 67 | extern int register_android_graphics_YuvImage(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | |
| 69 | extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env); |
| 70 | extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env); |
Thomas Tafertshofer | 6b1e838 | 2012-07-03 13:37:35 -0700 | [diff] [blame] | 71 | extern int register_android_opengl_jni_EGL14(JNIEnv* env); |
Jack Palevich | 1c4907e | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 72 | extern int register_android_opengl_jni_GLES10(JNIEnv* env); |
| 73 | extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env); |
| 74 | extern int register_android_opengl_jni_GLES11(JNIEnv* env); |
| 75 | extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env); |
Jack Palevich | 560814f | 2009-11-19 16:34:55 +0800 | [diff] [blame] | 76 | extern int register_android_opengl_jni_GLES20(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
| 78 | extern int register_android_hardware_Camera(JNIEnv *env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | extern int register_android_hardware_SensorManager(JNIEnv *env); |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 80 | extern int register_android_hardware_SerialPort(JNIEnv *env); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 81 | extern int register_android_hardware_UsbDevice(JNIEnv *env); |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 82 | extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 83 | extern int register_android_hardware_UsbRequest(JNIEnv *env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | extern int register_android_media_AudioRecord(JNIEnv *env); |
| 86 | extern int register_android_media_AudioSystem(JNIEnv *env); |
| 87 | extern int register_android_media_AudioTrack(JNIEnv *env); |
| 88 | extern int register_android_media_JetPlayer(JNIEnv *env); |
| 89 | extern int register_android_media_ToneGenerator(JNIEnv *env); |
| 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | extern int register_android_util_FloatMath(JNIEnv* env); |
| 92 | |
| 93 | namespace android { |
| 94 | |
| 95 | /* |
| 96 | * JNI-based registration functions. Note these are properly contained in |
| 97 | * namespace android. |
| 98 | */ |
| 99 | extern int register_android_content_AssetManager(JNIEnv* env); |
| 100 | extern int register_android_util_EventLog(JNIEnv* env); |
| 101 | extern int register_android_util_Log(JNIEnv* env); |
| 102 | extern int register_android_content_StringBlock(JNIEnv* env); |
| 103 | extern int register_android_content_XmlBlock(JNIEnv* env); |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 104 | extern int register_android_emoji_EmojiFactory(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | extern int register_android_graphics_Canvas(JNIEnv* env); |
| 106 | extern int register_android_graphics_ColorFilter(JNIEnv* env); |
| 107 | extern int register_android_graphics_DrawFilter(JNIEnv* env); |
| 108 | extern int register_android_graphics_Matrix(JNIEnv* env); |
| 109 | extern int register_android_graphics_Paint(JNIEnv* env); |
| 110 | extern int register_android_graphics_Path(JNIEnv* env); |
| 111 | extern int register_android_graphics_PathMeasure(JNIEnv* env); |
| 112 | extern int register_android_graphics_Picture(JNIEnv*); |
| 113 | extern int register_android_graphics_PorterDuff(JNIEnv* env); |
| 114 | extern int register_android_graphics_Rasterizer(JNIEnv* env); |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 115 | extern int register_android_graphics_Region(JNIEnv* env); |
Jamie Gennis | aa0ce33 | 2011-01-06 17:04:26 -0800 | [diff] [blame] | 116 | extern int register_android_graphics_SurfaceTexture(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | extern int register_android_graphics_Xfermode(JNIEnv* env); |
| 118 | extern int register_android_graphics_PixelFormat(JNIEnv* env); |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 119 | extern int register_android_view_DisplayEventReceiver(JNIEnv* env); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 120 | extern int register_android_view_GLES20DisplayList(JNIEnv* env); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 121 | extern int register_android_view_GLES20Canvas(JNIEnv* env); |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 122 | extern int register_android_view_HardwareRenderer(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | extern int register_android_view_Surface(JNIEnv* env); |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 124 | extern int register_android_view_SurfaceSession(JNIEnv* env); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 125 | extern int register_android_view_TextureView(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | extern int register_android_database_CursorWindow(JNIEnv* env); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 127 | extern int register_android_database_SQLiteConnection(JNIEnv* env); |
| 128 | extern int register_android_database_SQLiteGlobal(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | extern int register_android_database_SQLiteDebug(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | extern int register_android_debug_JNITest(JNIEnv* env); |
| 131 | extern int register_android_nio_utils(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | extern int register_android_text_format_Time(JNIEnv* env); |
| 133 | extern int register_android_os_Debug(JNIEnv* env); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 134 | extern int register_android_os_MessageQueue(JNIEnv* env); |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 135 | extern int register_android_os_Parcel(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | extern int register_android_os_ParcelFileDescriptor(JNIEnv *env); |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 137 | extern int register_android_os_SELinux(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | extern int register_android_os_SystemProperties(JNIEnv *env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | extern int register_android_os_SystemClock(JNIEnv* env); |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 140 | extern int register_android_os_Trace(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | extern int register_android_os_FileObserver(JNIEnv *env); |
| 142 | extern int register_android_os_FileUtils(JNIEnv *env); |
| 143 | extern int register_android_os_UEventObserver(JNIEnv* env); |
| 144 | extern int register_android_os_MemoryFile(JNIEnv* env); |
| 145 | extern int register_android_net_LocalSocketImpl(JNIEnv* env); |
| 146 | extern int register_android_net_NetworkUtils(JNIEnv* env); |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 147 | extern int register_android_net_TrafficStats(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | extern int register_android_net_wifi_WifiManager(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | extern int register_android_text_AndroidCharacter(JNIEnv *env); |
Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 150 | extern int register_android_text_AndroidBidi(JNIEnv *env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | extern int register_android_opengl_classes(JNIEnv *env); |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 152 | extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env); |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 153 | extern int register_android_server_Watchdog(JNIEnv* env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env); |
| 155 | extern int register_com_android_internal_os_ZygoteInit(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 | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 158 | extern int register_android_backup_FileBackupHelperBase(JNIEnv *env); |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 159 | extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env); |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 160 | extern int register_android_app_backup_FullBackup(JNIEnv *env); |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 161 | extern int register_android_app_ActivityThread(JNIEnv *env); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 162 | extern int register_android_app_NativeActivity(JNIEnv *env); |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 163 | extern int register_android_media_RemoteDisplay(JNIEnv *env); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 164 | extern int register_android_view_InputChannel(JNIEnv* env); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 165 | extern int register_android_view_InputDevice(JNIEnv* env); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 166 | extern int register_android_view_InputEventReceiver(JNIEnv* env); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 167 | extern int register_android_view_KeyCharacterMap(JNIEnv *env); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 168 | extern int register_android_view_KeyEvent(JNIEnv* env); |
| 169 | extern int register_android_view_MotionEvent(JNIEnv* env); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 170 | extern int register_android_view_PointerIcon(JNIEnv* env); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 171 | extern int register_android_view_VelocityTracker(JNIEnv* env); |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 172 | extern int register_android_content_res_ObbScanner(JNIEnv* env); |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 173 | extern int register_android_content_res_Configuration(JNIEnv* env); |
Chet Haase | 6e0ecb4 | 2010-11-03 19:41:18 -0700 | [diff] [blame] | 174 | extern int register_android_animation_PropertyValuesHolder(JNIEnv *env); |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 175 | extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
| 177 | static AndroidRuntime* gCurRuntime = NULL; |
| 178 | |
| 179 | static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL) |
| 180 | { |
| 181 | if (jniThrowException(env, exc, msg) != 0) |
| 182 | assert(false); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Code written in the Java Programming Language calls here from main(). |
| 187 | */ |
Jeff Brown | 16f5f5c | 2012-03-15 16:53:55 -0700 | [diff] [blame] | 188 | static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | { |
| 190 | gCurRuntime->onStarted(); |
| 191 | } |
| 192 | |
Jeff Brown | 16f5f5c | 2012-03-15 16:53:55 -0700 | [diff] [blame] | 193 | static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | { |
| 195 | gCurRuntime->onZygoteInit(); |
| 196 | } |
| 197 | |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 198 | static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv* env, |
| 199 | jobject clazz, jboolean exitWithoutCleanup) |
| 200 | { |
| 201 | gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup); |
| 202 | } |
| 203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | /* |
| 205 | * JNI registration. |
| 206 | */ |
| 207 | static JNINativeMethod gMethods[] = { |
Jeff Brown | 16f5f5c | 2012-03-15 16:53:55 -0700 | [diff] [blame] | 208 | { "nativeFinishInit", "()V", |
| 209 | (void*) com_android_internal_os_RuntimeInit_nativeFinishInit }, |
| 210 | { "nativeZygoteInit", "()V", |
| 211 | (void*) com_android_internal_os_RuntimeInit_nativeZygoteInit }, |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 212 | { "nativeSetExitWithoutCleanup", "(Z)V", |
| 213 | (void*) com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | int register_com_android_internal_os_RuntimeInit(JNIEnv* env) |
| 217 | { |
| 218 | return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit", |
| 219 | gMethods, NELEM(gMethods)); |
| 220 | } |
| 221 | |
| 222 | // ---------------------------------------------------------------------- |
| 223 | |
| 224 | /*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL; |
| 225 | |
| 226 | |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 227 | AndroidRuntime::AndroidRuntime() : |
| 228 | mExitWithoutCleanup(false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 263 | status_t AndroidRuntime::callMain(const char* className, |
| 264 | jclass clazz, int argc, const char* const argv[]) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | { |
| 266 | JNIEnv* env; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | jmethodID methodId; |
| 268 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 269 | ALOGD("Calling main entry %s", className); |
Dianne Hackborn | 08e60f2 | 2010-09-01 18:17:17 -0700 | [diff] [blame] | 270 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | env = getJNIEnv(); |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 272 | if (clazz == NULL || env == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | return UNKNOWN_ERROR; |
| 274 | } |
| 275 | |
| 276 | methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V"); |
| 277 | if (methodId == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 278 | ALOGE("ERROR: could not find method %s.main(String[])\n", className); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | return UNKNOWN_ERROR; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * We want to call main() with a String array with our arguments in it. |
| 284 | * Create an array and populate it. |
| 285 | */ |
| 286 | jclass stringClass; |
| 287 | jobjectArray strArray; |
| 288 | |
| 289 | stringClass = env->FindClass("java/lang/String"); |
| 290 | strArray = env->NewObjectArray(argc, stringClass, NULL); |
| 291 | |
| 292 | for (int i = 0; i < argc; i++) { |
| 293 | jstring argStr = env->NewStringUTF(argv[i]); |
| 294 | env->SetObjectArrayElement(strArray, i, argStr); |
| 295 | } |
| 296 | |
| 297 | env->CallStaticVoidMethod(clazz, methodId, strArray); |
| 298 | return NO_ERROR; |
| 299 | } |
| 300 | |
| 301 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | * The VM calls this through the "exit" hook. |
| 303 | */ |
| 304 | static void runtime_exit(int code) |
| 305 | { |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 306 | gCurRuntime->exit(code); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * The VM calls this through the "vfprintf" hook. |
| 311 | * |
| 312 | * We ignore "fp" and just write the results to the log file. |
| 313 | */ |
| 314 | static void runtime_vfprintf(FILE* fp, const char* format, va_list ap) |
| 315 | { |
| 316 | LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap); |
| 317 | } |
| 318 | |
Brad Fitzpatrick | 0bd5243 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 319 | /** |
| 320 | * The VM calls this when mutex contention debugging is enabled to |
| 321 | * determine whether or not the blocked thread was a "sensitive thread" |
| 322 | * for user responsiveness/smoothess. |
| 323 | * |
| 324 | * Our policy for this is whether or not we're tracing any StrictMode |
| 325 | * events on this thread (which we might've inherited via Binder calls |
| 326 | * into us) |
| 327 | */ |
| 328 | static bool runtime_isSensitiveThread() { |
| 329 | IPCThreadState* state = IPCThreadState::selfOrNull(); |
| 330 | return state && state->getStrictModePolicy() != 0; |
| 331 | } |
| 332 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | |
| 334 | /** |
| 335 | * Add VM arguments to the to-be-executed VM |
| 336 | * Stops at first non '-' argument (also stops at an argument of '--') |
| 337 | * Returns the number of args consumed |
| 338 | */ |
| 339 | int AndroidRuntime::addVmArguments(int argc, const char* const argv[]) |
| 340 | { |
| 341 | int i; |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 342 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | for (i = 0; i<argc; i++) { |
| 344 | if (argv[i][0] != '-') { |
| 345 | return i; |
| 346 | } |
| 347 | if (argv[i][1] == '-' && argv[i][2] == 0) { |
| 348 | return i+1; |
| 349 | } |
| 350 | |
| 351 | JavaVMOption opt; |
| 352 | memset(&opt, 0, sizeof(opt)); |
| 353 | opt.optionString = (char*)argv[i]; |
| 354 | mOptions.add(opt); |
| 355 | } |
| 356 | return i; |
| 357 | } |
| 358 | |
| 359 | static int hasDir(const char* dir) |
| 360 | { |
| 361 | struct stat s; |
| 362 | int res = stat(dir, &s); |
| 363 | if (res == 0) { |
| 364 | return S_ISDIR(s.st_mode); |
| 365 | } |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * We just want failed write() calls to just return with an error. |
| 371 | */ |
| 372 | static void blockSigpipe() |
| 373 | { |
| 374 | sigset_t mask; |
| 375 | |
| 376 | sigemptyset(&mask); |
| 377 | sigaddset(&mask, SIGPIPE); |
| 378 | if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 379 | ALOGW("WARNING: SIGPIPE not blocked\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Read the persistent locale. |
| 384 | */ |
| 385 | static void readLocale(char* language, char* region) |
| 386 | { |
| 387 | char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX]; |
Brian Carlstrom | 171ea89 | 2010-08-24 21:27:52 -0700 | [diff] [blame] | 388 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | property_get("persist.sys.language", propLang, ""); |
| 390 | property_get("persist.sys.country", propRegn, ""); |
| 391 | if (*propLang == 0 && *propRegn == 0) { |
| 392 | /* Set to ro properties, default is en_US */ |
| 393 | property_get("ro.product.locale.language", propLang, "en"); |
| 394 | property_get("ro.product.locale.region", propRegn, "US"); |
| 395 | } |
| 396 | strncat(language, propLang, 2); |
| 397 | strncat(region, propRegn, 2); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 398 | //ALOGD("language=%s region=%s\n", language, region); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 401 | /* |
Andy McFadden | e4d81f2 | 2010-07-14 16:02:20 -0700 | [diff] [blame] | 402 | * Parse a property containing space-separated options that should be |
| 403 | * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap". |
| 404 | * |
| 405 | * This will cut up "extraOptsBuf" as we chop it into individual options. |
| 406 | * |
| 407 | * Adds the strings, if any, to mOptions. |
| 408 | */ |
| 409 | void AndroidRuntime::parseExtraOpts(char* extraOptsBuf) |
| 410 | { |
| 411 | JavaVMOption opt; |
| 412 | char* start; |
| 413 | char* end; |
| 414 | |
| 415 | memset(&opt, 0, sizeof(opt)); |
| 416 | start = extraOptsBuf; |
| 417 | while (*start != '\0') { |
| 418 | while (*start == ' ') /* skip leading whitespace */ |
| 419 | start++; |
| 420 | if (*start == '\0') /* was trailing ws, bail */ |
| 421 | break; |
| 422 | |
| 423 | end = start+1; |
| 424 | while (*end != ' ' && *end != '\0') /* find end of token */ |
| 425 | end++; |
| 426 | if (*end == ' ') |
| 427 | *end++ = '\0'; /* mark end, advance to indicate more */ |
| 428 | |
| 429 | opt.optionString = start; |
| 430 | mOptions.add(opt); |
| 431 | start = end; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 436 | * Start the Dalvik Virtual Machine. |
| 437 | * |
| 438 | * Various arguments, most determined by system properties, are passed in. |
| 439 | * The "mOptions" vector is updated. |
| 440 | * |
| 441 | * Returns 0 on success. |
| 442 | */ |
| 443 | int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | { |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 445 | int result = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | JavaVMInitArgs initArgs; |
| 447 | JavaVMOption opt; |
| 448 | char propBuf[PROPERTY_VALUE_MAX]; |
| 449 | char stackTraceFileBuf[PROPERTY_VALUE_MAX]; |
| 450 | char dexoptFlagsBuf[PROPERTY_VALUE_MAX]; |
| 451 | char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX]; |
| 452 | char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX]; |
Carl Shapiro | 38cfa8c | 2010-12-07 16:48:29 -0800 | [diff] [blame] | 453 | char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX]; |
Dianne Hackborn | bdcef70 | 2009-08-18 19:15:36 -0700 | [diff] [blame] | 454 | char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX]; |
Carl Shapiro | 8cdf27c | 2011-01-19 12:33:12 -0800 | [diff] [blame] | 455 | char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX]; |
Ian Rogers | 5325010 | 2012-09-23 16:38:03 -0700 | [diff] [blame] | 456 | char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX]; |
| 457 | char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX]; |
| 458 | char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX]; |
Andy McFadden | e4d81f2 | 2010-07-14 16:02:20 -0700 | [diff] [blame] | 459 | char extraOptsBuf[PROPERTY_VALUE_MAX]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | char* stackTraceFile = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | bool checkJni = false; |
Andy McFadden | e2b23e1 | 2009-04-03 11:09:46 -0700 | [diff] [blame] | 462 | bool checkDexSum = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | bool logStdio = false; |
Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame] | 464 | enum { |
| 465 | kEMDefault, |
| 466 | kEMIntPortable, |
| 467 | kEMIntFast, |
| 468 | #if defined(WITH_JIT) |
| 469 | kEMJitCompiler, |
| 470 | #endif |
| 471 | } executionMode = kEMDefault; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | |
| 474 | property_get("dalvik.vm.checkjni", propBuf, ""); |
| 475 | if (strcmp(propBuf, "true") == 0) { |
| 476 | checkJni = true; |
| 477 | } else if (strcmp(propBuf, "false") != 0) { |
| 478 | /* property is neither true nor false; fall back on kernel parameter */ |
| 479 | property_get("ro.kernel.android.checkjni", propBuf, ""); |
| 480 | if (propBuf[0] == '1') { |
| 481 | checkJni = true; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | property_get("dalvik.vm.execution-mode", propBuf, ""); |
| 486 | if (strcmp(propBuf, "int:portable") == 0) { |
| 487 | executionMode = kEMIntPortable; |
| 488 | } else if (strcmp(propBuf, "int:fast") == 0) { |
| 489 | executionMode = kEMIntFast; |
Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame] | 490 | #if defined(WITH_JIT) |
| 491 | } else if (strcmp(propBuf, "int:jit") == 0) { |
| 492 | executionMode = kEMJitCompiler; |
| 493 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, ""); |
| 497 | |
Andy McFadden | e2b23e1 | 2009-04-03 11:09:46 -0700 | [diff] [blame] | 498 | property_get("dalvik.vm.check-dex-sum", propBuf, ""); |
| 499 | if (strcmp(propBuf, "true") == 0) { |
| 500 | checkDexSum = true; |
| 501 | } |
| 502 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | property_get("log.redirect-stdio", propBuf, ""); |
| 504 | if (strcmp(propBuf, "true") == 0) { |
| 505 | logStdio = true; |
| 506 | } |
| 507 | |
| 508 | strcpy(enableAssertBuf, "-ea:"); |
| 509 | property_get("dalvik.vm.enableassertions", enableAssertBuf+4, ""); |
| 510 | |
| 511 | strcpy(jniOptsBuf, "-Xjniopts:"); |
| 512 | property_get("dalvik.vm.jniopts", jniOptsBuf+10, ""); |
| 513 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | /* route exit() to our handler */ |
| 515 | opt.extraInfo = (void*) runtime_exit; |
| 516 | opt.optionString = "exit"; |
| 517 | mOptions.add(opt); |
| 518 | |
| 519 | /* route fprintf() to our handler */ |
| 520 | opt.extraInfo = (void*) runtime_vfprintf; |
| 521 | opt.optionString = "vfprintf"; |
| 522 | mOptions.add(opt); |
| 523 | |
Brad Fitzpatrick | 0bd5243 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 524 | /* register the framework-specific "is sensitive thread" hook */ |
| 525 | opt.extraInfo = (void*) runtime_isSensitiveThread; |
| 526 | opt.optionString = "sensitiveThread"; |
| 527 | mOptions.add(opt); |
| 528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | opt.extraInfo = NULL; |
| 530 | |
| 531 | /* enable verbose; standard options are { jni, gc, class } */ |
| 532 | //options[curOpt++].optionString = "-verbose:jni"; |
| 533 | opt.optionString = "-verbose:gc"; |
| 534 | mOptions.add(opt); |
| 535 | //options[curOpt++].optionString = "-verbose:class"; |
| 536 | |
Carl Shapiro | 9e4c884 | 2010-12-08 12:14:46 -0800 | [diff] [blame] | 537 | /* |
| 538 | * The default starting and maximum size of the heap. Larger |
| 539 | * values should be specified in a product property override. |
| 540 | */ |
Carl Shapiro | 38cfa8c | 2010-12-07 16:48:29 -0800 | [diff] [blame] | 541 | strcpy(heapstartsizeOptsBuf, "-Xms"); |
Carl Shapiro | 9e4c884 | 2010-12-08 12:14:46 -0800 | [diff] [blame] | 542 | property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m"); |
Carl Shapiro | 38cfa8c | 2010-12-07 16:48:29 -0800 | [diff] [blame] | 543 | opt.optionString = heapstartsizeOptsBuf; |
| 544 | mOptions.add(opt); |
Dianne Hackborn | bdcef70 | 2009-08-18 19:15:36 -0700 | [diff] [blame] | 545 | strcpy(heapsizeOptsBuf, "-Xmx"); |
| 546 | property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m"); |
Dianne Hackborn | bdcef70 | 2009-08-18 19:15:36 -0700 | [diff] [blame] | 547 | opt.optionString = heapsizeOptsBuf; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | mOptions.add(opt); |
| 549 | |
Elliott Hughes | 3f177d7 | 2012-04-12 16:02:56 -0700 | [diff] [blame] | 550 | // Increase the main thread's interpreter stack size for bug 6315322. |
| 551 | opt.optionString = "-XX:mainThreadStackSize=24K"; |
| 552 | mOptions.add(opt); |
| 553 | |
Carl Shapiro | 8cdf27c | 2011-01-19 12:33:12 -0800 | [diff] [blame] | 554 | strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit="); |
| 555 | property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, ""); |
| 556 | if (heapgrowthlimitOptsBuf[20] != '\0') { |
| 557 | opt.optionString = heapgrowthlimitOptsBuf; |
| 558 | mOptions.add(opt); |
| 559 | } |
| 560 | |
Ian Rogers | 5325010 | 2012-09-23 16:38:03 -0700 | [diff] [blame] | 561 | strcpy(heapminfreeOptsBuf, "-XX:HeapMinFree="); |
| 562 | property_get("dalvik.vm.heapminfree", heapminfreeOptsBuf+16, ""); |
| 563 | if (heapminfreeOptsBuf[16] != '\0') { |
| 564 | opt.optionString = heapminfreeOptsBuf; |
| 565 | mOptions.add(opt); |
| 566 | } |
| 567 | |
| 568 | strcpy(heapmaxfreeOptsBuf, "-XX:HeapMaxFree="); |
| 569 | property_get("dalvik.vm.heapmaxfree", heapmaxfreeOptsBuf+16, ""); |
| 570 | if (heapmaxfreeOptsBuf[16] != '\0') { |
| 571 | opt.optionString = heapmaxfreeOptsBuf; |
| 572 | mOptions.add(opt); |
| 573 | } |
| 574 | |
| 575 | strcpy(heaptargetutilizationOptsBuf, "-XX:HeapTargetUtilization="); |
| 576 | property_get("dalvik.vm.heaptargetutilization", heaptargetutilizationOptsBuf+26, ""); |
| 577 | if (heapmaxfreeOptsBuf[26] != '\0') { |
| 578 | opt.optionString = heaptargetutilizationOptsBuf; |
| 579 | mOptions.add(opt); |
| 580 | } |
| 581 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | /* |
| 583 | * Enable or disable dexopt features, such as bytecode verification and |
| 584 | * calculation of register maps for precise GC. |
| 585 | */ |
| 586 | property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, ""); |
| 587 | if (dexoptFlagsBuf[0] != '\0') { |
| 588 | const char* opc; |
| 589 | const char* val; |
| 590 | |
| 591 | opc = strstr(dexoptFlagsBuf, "v="); /* verification */ |
| 592 | if (opc != NULL) { |
| 593 | switch (*(opc+2)) { |
| 594 | case 'n': val = "-Xverify:none"; break; |
| 595 | case 'r': val = "-Xverify:remote"; break; |
| 596 | case 'a': val = "-Xverify:all"; break; |
| 597 | default: val = NULL; break; |
| 598 | } |
| 599 | |
| 600 | if (val != NULL) { |
| 601 | opt.optionString = val; |
| 602 | mOptions.add(opt); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | opc = strstr(dexoptFlagsBuf, "o="); /* optimization */ |
| 607 | if (opc != NULL) { |
| 608 | switch (*(opc+2)) { |
| 609 | case 'n': val = "-Xdexopt:none"; break; |
| 610 | case 'v': val = "-Xdexopt:verified"; break; |
| 611 | case 'a': val = "-Xdexopt:all"; break; |
Brian Carlstrom | 08065b9 | 2011-04-01 15:49:41 -0700 | [diff] [blame] | 612 | case 'f': val = "-Xdexopt:full"; break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | default: val = NULL; break; |
| 614 | } |
| 615 | |
| 616 | if (val != NULL) { |
| 617 | opt.optionString = val; |
| 618 | mOptions.add(opt); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */ |
| 623 | if (opc != NULL) { |
| 624 | opt.optionString = "-Xgenregmap"; |
| 625 | mOptions.add(opt); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 626 | |
| 627 | /* turn on precise GC while we're at it */ |
| 628 | opt.optionString = "-Xgc:precise"; |
| 629 | mOptions.add(opt); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | /* enable debugging; set suspend=y to pause during VM init */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | /* use android ADB transport */ |
| 635 | opt.optionString = |
| 636 | "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | mOptions.add(opt); |
| 638 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 639 | ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | if (checkJni) { |
| 641 | /* extended JNI checking */ |
| 642 | opt.optionString = "-Xcheck:jni"; |
| 643 | mOptions.add(opt); |
| 644 | |
| 645 | /* set a cap on JNI global references */ |
| 646 | opt.optionString = "-Xjnigreflimit:2000"; |
| 647 | mOptions.add(opt); |
| 648 | |
| 649 | /* with -Xcheck:jni, this provides a JNI function call trace */ |
| 650 | //opt.optionString = "-verbose:jni"; |
| 651 | //mOptions.add(opt); |
| 652 | } |
Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame] | 653 | |
Carl Shapiro | d8f3ec6 | 2010-04-12 16:31:59 -0700 | [diff] [blame] | 654 | char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)]; |
| 655 | property_get("dalvik.vm.lockprof.threshold", propBuf, ""); |
| 656 | if (strlen(propBuf) > 0) { |
| 657 | strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:"); |
| 658 | strcat(lockProfThresholdBuf, propBuf); |
| 659 | opt.optionString = lockProfThresholdBuf; |
| 660 | mOptions.add(opt); |
| 661 | } |
| 662 | |
Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame] | 663 | #if defined(WITH_JIT) |
Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame] | 664 | /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */ |
| 665 | char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX]; |
| 666 | property_get("dalvik.vm.jit.op", propBuf, ""); |
| 667 | if (strlen(propBuf) > 0) { |
| 668 | strcpy(jitOpBuf, "-Xjitop:"); |
| 669 | strcat(jitOpBuf, propBuf); |
| 670 | opt.optionString = jitOpBuf; |
| 671 | mOptions.add(opt); |
| 672 | } |
| 673 | |
Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame] | 674 | /* Force interpreter-only mode for selected methods */ |
| 675 | char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX]; |
| 676 | property_get("dalvik.vm.jit.method", propBuf, ""); |
| 677 | if (strlen(propBuf) > 0) { |
| 678 | strcpy(jitMethodBuf, "-Xjitmethod:"); |
| 679 | strcat(jitMethodBuf, propBuf); |
| 680 | opt.optionString = jitMethodBuf; |
| 681 | mOptions.add(opt); |
| 682 | } |
Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame] | 683 | #endif |
| 684 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 685 | if (executionMode == kEMIntPortable) { |
| 686 | opt.optionString = "-Xint:portable"; |
| 687 | mOptions.add(opt); |
| 688 | } else if (executionMode == kEMIntFast) { |
| 689 | opt.optionString = "-Xint:fast"; |
| 690 | mOptions.add(opt); |
Ben Cheng | 52b0e73 | 2009-06-19 13:31:12 -0700 | [diff] [blame] | 691 | #if defined(WITH_JIT) |
| 692 | } else if (executionMode == kEMJitCompiler) { |
| 693 | opt.optionString = "-Xint:jit"; |
| 694 | mOptions.add(opt); |
| 695 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | } |
Andy McFadden | e2b23e1 | 2009-04-03 11:09:46 -0700 | [diff] [blame] | 697 | |
| 698 | if (checkDexSum) { |
| 699 | /* perform additional DEX checksum tests */ |
| 700 | opt.optionString = "-Xcheckdexsum"; |
| 701 | mOptions.add(opt); |
| 702 | } |
| 703 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | if (logStdio) { |
| 705 | /* convert stdout/stderr to log messages */ |
| 706 | opt.optionString = "-Xlog-stdio"; |
| 707 | mOptions.add(opt); |
| 708 | } |
| 709 | |
| 710 | if (enableAssertBuf[4] != '\0') { |
| 711 | /* accept "all" to mean "all classes and packages" */ |
| 712 | if (strcmp(enableAssertBuf+4, "all") == 0) |
| 713 | enableAssertBuf[3] = '\0'; |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 714 | ALOGI("Assertions enabled: '%s'\n", enableAssertBuf); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | opt.optionString = enableAssertBuf; |
| 716 | mOptions.add(opt); |
| 717 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 718 | ALOGV("Assertions disabled\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | if (jniOptsBuf[10] != '\0') { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 722 | ALOGI("JNI options: '%s'\n", jniOptsBuf); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | opt.optionString = jniOptsBuf; |
| 724 | mOptions.add(opt); |
| 725 | } |
| 726 | |
| 727 | if (stackTraceFileBuf[0] != '\0') { |
| 728 | static const char* stfOptName = "-Xstacktracefile:"; |
| 729 | |
| 730 | stackTraceFile = (char*) malloc(strlen(stfOptName) + |
| 731 | strlen(stackTraceFileBuf) +1); |
| 732 | strcpy(stackTraceFile, stfOptName); |
| 733 | strcat(stackTraceFile, stackTraceFileBuf); |
| 734 | opt.optionString = stackTraceFile; |
| 735 | mOptions.add(opt); |
| 736 | } |
Andy McFadden | e4d81f2 | 2010-07-14 16:02:20 -0700 | [diff] [blame] | 737 | |
| 738 | /* extra options; parse this late so it overrides others */ |
| 739 | property_get("dalvik.vm.extra-opts", extraOptsBuf, ""); |
| 740 | parseExtraOpts(extraOptsBuf); |
| 741 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | /* Set the properties for locale */ |
| 743 | { |
| 744 | char langOption[sizeof("-Duser.language=") + 3]; |
| 745 | char regionOption[sizeof("-Duser.region=") + 3]; |
| 746 | strcpy(langOption, "-Duser.language="); |
| 747 | strcpy(regionOption, "-Duser.region="); |
| 748 | readLocale(langOption, regionOption); |
| 749 | opt.extraInfo = NULL; |
| 750 | opt.optionString = langOption; |
| 751 | mOptions.add(opt); |
| 752 | opt.optionString = regionOption; |
| 753 | mOptions.add(opt); |
| 754 | } |
| 755 | |
| 756 | /* |
| 757 | * We don't have /tmp on the device, but we often have an SD card. Apps |
| 758 | * shouldn't use this, but some test suites might want to exercise it. |
| 759 | */ |
| 760 | opt.optionString = "-Djava.io.tmpdir=/sdcard"; |
| 761 | mOptions.add(opt); |
| 762 | |
| 763 | initArgs.version = JNI_VERSION_1_4; |
| 764 | initArgs.options = mOptions.editArray(); |
| 765 | initArgs.nOptions = mOptions.size(); |
| 766 | initArgs.ignoreUnrecognized = JNI_FALSE; |
| 767 | |
| 768 | /* |
| 769 | * Initialize the VM. |
| 770 | * |
| 771 | * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread. |
| 772 | * If this call succeeds, the VM is ready, and we can start issuing |
| 773 | * JNI calls. |
| 774 | */ |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 775 | if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 776 | ALOGE("JNI_CreateJavaVM failed\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | goto bail; |
| 778 | } |
| 779 | |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 780 | result = 0; |
| 781 | |
| 782 | bail: |
| 783 | free(stackTraceFile); |
| 784 | return result; |
| 785 | } |
| 786 | |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 787 | char* AndroidRuntime::toSlashClassName(const char* className) |
| 788 | { |
| 789 | char* result = strdup(className); |
| 790 | for (char* cp = result; *cp != '\0'; cp++) { |
| 791 | if (*cp == '.') { |
| 792 | *cp = '/'; |
| 793 | } |
| 794 | } |
| 795 | return result; |
| 796 | } |
| 797 | |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 798 | /* |
| 799 | * Start the Android runtime. This involves starting the virtual machine |
| 800 | * and calling the "static void main(String[] args)" method in the class |
| 801 | * named by "className". |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 802 | * |
| 803 | * Passes the main function two arguments, the class name and the specified |
| 804 | * options string. |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 805 | */ |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 806 | void AndroidRuntime::start(const char* className, const char* options) |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 807 | { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 808 | ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n", |
Dianne Hackborn | 08e60f2 | 2010-09-01 18:17:17 -0700 | [diff] [blame] | 809 | className != NULL ? className : "(unknown)"); |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 810 | |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 811 | blockSigpipe(); |
| 812 | |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 813 | /* |
| 814 | * 'startSystemServer == true' means runtime is obsolete and not run from |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 815 | * init.rc anymore, so we print out the boot start event here. |
| 816 | */ |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 817 | if (strcmp(options, "start-system-server") == 0) { |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 818 | /* track our progress through the boot sequence */ |
| 819 | const int LOG_BOOT_PROGRESS_START = 3000; |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 820 | LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 821 | ns2ms(systemTime(SYSTEM_TIME_MONOTONIC))); |
| 822 | } |
| 823 | |
| 824 | const char* rootDir = getenv("ANDROID_ROOT"); |
| 825 | if (rootDir == NULL) { |
| 826 | rootDir = "/system"; |
| 827 | if (!hasDir("/system")) { |
| 828 | LOG_FATAL("No root directory specified, and /android does not exist."); |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 829 | return; |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 830 | } |
| 831 | setenv("ANDROID_ROOT", rootDir, 1); |
| 832 | } |
| 833 | |
| 834 | //const char* kernelHack = getenv("LD_ASSUME_KERNEL"); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 835 | //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack); |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 836 | |
| 837 | /* start the virtual machine */ |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 838 | JNIEnv* env; |
| 839 | if (startVm(&mJavaVM, &env) != 0) { |
| 840 | return; |
| 841 | } |
| 842 | onVmCreated(env); |
Andy McFadden | f70188a | 2009-03-31 15:52:13 -0700 | [diff] [blame] | 843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | /* |
| 845 | * Register android functions. |
| 846 | */ |
| 847 | if (startReg(env) < 0) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 848 | ALOGE("Unable to register all android natives\n"); |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 849 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /* |
| 853 | * We want to call main() with a String array with arguments in it. |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 854 | * At present we have two arguments, the class name and an option string. |
| 855 | * Create an array to hold them. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | */ |
| 857 | jclass stringClass; |
| 858 | jobjectArray strArray; |
| 859 | jstring classNameStr; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 860 | jstring optionsStr; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | |
| 862 | stringClass = env->FindClass("java/lang/String"); |
| 863 | assert(stringClass != NULL); |
| 864 | strArray = env->NewObjectArray(2, stringClass, NULL); |
| 865 | assert(strArray != NULL); |
| 866 | classNameStr = env->NewStringUTF(className); |
| 867 | assert(classNameStr != NULL); |
| 868 | env->SetObjectArrayElement(strArray, 0, classNameStr); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 869 | optionsStr = env->NewStringUTF(options); |
| 870 | env->SetObjectArrayElement(strArray, 1, optionsStr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | |
| 872 | /* |
| 873 | * Start VM. This thread becomes the main thread of the VM, and will |
| 874 | * not return until the VM exits. |
| 875 | */ |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 876 | char* slashClassName = toSlashClassName(className); |
| 877 | jclass startClass = env->FindClass(slashClassName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | if (startClass == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 879 | ALOGE("JavaVM unable to locate class '%s'\n", slashClassName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | /* keep going */ |
| 881 | } else { |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 882 | jmethodID startMeth = env->GetStaticMethodID(startClass, "main", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | "([Ljava/lang/String;)V"); |
| 884 | if (startMeth == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 885 | ALOGE("JavaVM unable to find main() in '%s'\n", className); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | /* keep going */ |
| 887 | } else { |
| 888 | env->CallStaticVoidMethod(startClass, startMeth, strArray); |
| 889 | |
| 890 | #if 0 |
| 891 | if (env->ExceptionCheck()) |
| 892 | threadExitUncaughtException(env); |
| 893 | #endif |
| 894 | } |
| 895 | } |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 896 | free(slashClassName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 898 | ALOGD("Shutting down VM\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | if (mJavaVM->DetachCurrentThread() != JNI_OK) |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 900 | ALOGW("Warning: unable to detach main thread\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | if (mJavaVM->DestroyJavaVM() != 0) |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 902 | ALOGW("Warning: VM did not shut down cleanly\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 905 | void AndroidRuntime::exit(int code) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | { |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 907 | if (mExitWithoutCleanup) { |
| 908 | ALOGI("VM exiting with result code %d, cleanup skipped.", code); |
| 909 | ::_exit(code); |
| 910 | } else { |
| 911 | ALOGI("VM exiting with result code %d.", code); |
| 912 | onExit(code); |
| 913 | ::exit(code); |
| 914 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | } |
| 916 | |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 917 | void AndroidRuntime::onVmCreated(JNIEnv* env) |
| 918 | { |
| 919 | // If AndroidRuntime had anything to do here, we'd have done it in 'start'. |
| 920 | } |
| 921 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | /* |
| 923 | * Get the JNIEnv pointer for this thread. |
| 924 | * |
| 925 | * Returns NULL if the slot wasn't allocated or populated. |
| 926 | */ |
| 927 | /*static*/ JNIEnv* AndroidRuntime::getJNIEnv() |
| 928 | { |
| 929 | JNIEnv* env; |
| 930 | JavaVM* vm = AndroidRuntime::getJavaVM(); |
| 931 | assert(vm != NULL); |
| 932 | |
| 933 | if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) |
| 934 | return NULL; |
| 935 | return env; |
| 936 | } |
| 937 | |
| 938 | /* |
| 939 | * Makes the current thread visible to the VM. |
| 940 | * |
| 941 | * The JNIEnv pointer returned is only valid for the current thread, and |
| 942 | * thus must be tucked into thread-local storage. |
| 943 | */ |
| 944 | static int javaAttachThread(const char* threadName, JNIEnv** pEnv) |
| 945 | { |
| 946 | JavaVMAttachArgs args; |
| 947 | JavaVM* vm; |
| 948 | jint result; |
| 949 | |
| 950 | vm = AndroidRuntime::getJavaVM(); |
| 951 | assert(vm != NULL); |
| 952 | |
| 953 | args.version = JNI_VERSION_1_4; |
| 954 | args.name = (char*) threadName; |
| 955 | args.group = NULL; |
| 956 | |
| 957 | result = vm->AttachCurrentThread(pEnv, (void*) &args); |
| 958 | if (result != JNI_OK) |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 959 | ALOGI("NOTE: attach of thread '%s' failed\n", threadName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | |
| 961 | return result; |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | * Detach the current thread from the set visible to the VM. |
| 966 | */ |
| 967 | static int javaDetachThread(void) |
| 968 | { |
| 969 | JavaVM* vm; |
| 970 | jint result; |
| 971 | |
| 972 | vm = AndroidRuntime::getJavaVM(); |
| 973 | assert(vm != NULL); |
| 974 | |
| 975 | result = vm->DetachCurrentThread(); |
| 976 | if (result != JNI_OK) |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 977 | ALOGE("ERROR: thread detach failed\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | return result; |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * When starting a native thread that will be visible from the VM, we |
| 983 | * bounce through this to get the right attach/detach action. |
| 984 | * Note that this function calls free(args) |
| 985 | */ |
| 986 | /*static*/ int AndroidRuntime::javaThreadShell(void* args) { |
| 987 | void* start = ((void**)args)[0]; |
| 988 | void* userData = ((void **)args)[1]; |
| 989 | char* name = (char*) ((void **)args)[2]; // we own this storage |
| 990 | free(args); |
| 991 | JNIEnv* env; |
| 992 | int result; |
| 993 | |
| 994 | /* hook us into the VM */ |
| 995 | if (javaAttachThread(name, &env) != JNI_OK) |
| 996 | return -1; |
| 997 | |
| 998 | /* start the thread running */ |
| 999 | result = (*(android_thread_func_t)start)(userData); |
| 1000 | |
| 1001 | /* unhook us */ |
| 1002 | javaDetachThread(); |
| 1003 | free(name); |
| 1004 | |
| 1005 | return result; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * This is invoked from androidCreateThreadEtc() via the callback |
| 1010 | * set with androidSetCreateThreadFunc(). |
| 1011 | * |
| 1012 | * We need to create the new thread in such a way that it gets hooked |
| 1013 | * into the VM before it really starts executing. |
| 1014 | */ |
| 1015 | /*static*/ int AndroidRuntime::javaCreateThreadEtc( |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 1016 | android_thread_func_t entryFunction, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | void* userData, |
| 1018 | const char* threadName, |
| 1019 | int32_t threadPriority, |
| 1020 | size_t threadStackSize, |
| 1021 | android_thread_id_t* threadId) |
| 1022 | { |
| 1023 | void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free |
| 1024 | int result; |
| 1025 | |
| 1026 | assert(threadName != NULL); |
| 1027 | |
| 1028 | args[0] = (void*) entryFunction; |
| 1029 | args[1] = userData; |
| 1030 | args[2] = (void*) strdup(threadName); // javaThreadShell must free |
| 1031 | |
| 1032 | result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args, |
| 1033 | threadName, threadPriority, threadStackSize, threadId); |
| 1034 | return result; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Create a thread that is visible from the VM. |
| 1039 | * |
| 1040 | * This is called from elsewhere in the library. |
| 1041 | */ |
Mike Lockwood | f602d36 | 2010-06-20 14:28:16 -0700 | [diff] [blame] | 1042 | /*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | void (*start)(void *), void* arg) |
| 1044 | { |
Mike Lockwood | f602d36 | 2010-06-20 14:28:16 -0700 | [diff] [blame] | 1045 | android_thread_id_t threadId = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | javaCreateThreadEtc((android_thread_func_t) start, arg, name, |
Mike Lockwood | f602d36 | 2010-06-20 14:28:16 -0700 | [diff] [blame] | 1047 | ANDROID_PRIORITY_DEFAULT, 0, &threadId); |
| 1048 | return threadId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | #if 0 |
| 1052 | static void quickTest(void* arg) |
| 1053 | { |
| 1054 | const char* str = (const char*) arg; |
| 1055 | |
| 1056 | printf("In quickTest: %s\n", str); |
| 1057 | } |
| 1058 | #endif |
| 1059 | |
| 1060 | #ifdef NDEBUG |
| 1061 | #define REG_JNI(name) { name } |
| 1062 | struct RegJNIRec { |
| 1063 | int (*mProc)(JNIEnv*); |
| 1064 | }; |
| 1065 | #else |
| 1066 | #define REG_JNI(name) { name, #name } |
| 1067 | struct RegJNIRec { |
| 1068 | int (*mProc)(JNIEnv*); |
| 1069 | const char* mName; |
| 1070 | }; |
| 1071 | #endif |
| 1072 | |
| 1073 | typedef void (*RegJAMProc)(); |
| 1074 | |
| 1075 | static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env) |
| 1076 | { |
| 1077 | for (size_t i = 0; i < count; i++) { |
| 1078 | if (array[i].mProc(env) < 0) { |
| 1079 | #ifndef NDEBUG |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1080 | ALOGD("----------!!! %s failed to load\n", array[i].mName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | #endif |
| 1082 | return -1; |
| 1083 | } |
| 1084 | } |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | static void register_jam_procs(const RegJAMProc array[], size_t count) |
| 1089 | { |
| 1090 | for (size_t i = 0; i < count; i++) { |
| 1091 | array[i](); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | static const RegJNIRec gRegJNI[] = { |
| 1096 | REG_JNI(register_android_debug_JNITest), |
| 1097 | REG_JNI(register_com_android_internal_os_RuntimeInit), |
| 1098 | REG_JNI(register_android_os_SystemClock), |
| 1099 | REG_JNI(register_android_util_EventLog), |
| 1100 | REG_JNI(register_android_util_Log), |
| 1101 | REG_JNI(register_android_util_FloatMath), |
| 1102 | REG_JNI(register_android_text_format_Time), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | REG_JNI(register_android_content_AssetManager), |
| 1104 | REG_JNI(register_android_content_StringBlock), |
| 1105 | REG_JNI(register_android_content_XmlBlock), |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 1106 | REG_JNI(register_android_emoji_EmojiFactory), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | REG_JNI(register_android_text_AndroidCharacter), |
Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 1108 | REG_JNI(register_android_text_AndroidBidi), |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1109 | REG_JNI(register_android_view_InputDevice), |
| 1110 | REG_JNI(register_android_view_KeyCharacterMap), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | REG_JNI(register_android_os_Process), |
Andreas Huber | a8079bf | 2010-11-16 14:40:31 -0800 | [diff] [blame] | 1112 | REG_JNI(register_android_os_SystemProperties), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | REG_JNI(register_android_os_Binder), |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1114 | REG_JNI(register_android_os_Parcel), |
Jeff Brown | 0a0a124 | 2011-12-02 02:25:22 -0800 | [diff] [blame] | 1115 | REG_JNI(register_android_view_DisplayEventReceiver), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | REG_JNI(register_android_nio_utils), |
| 1117 | REG_JNI(register_android_graphics_PixelFormat), |
| 1118 | REG_JNI(register_android_graphics_Graphics), |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 1119 | REG_JNI(register_android_view_GLES20DisplayList), |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1120 | REG_JNI(register_android_view_GLES20Canvas), |
Romain Guy | a958265 | 2011-11-10 14:20:10 -0800 | [diff] [blame] | 1121 | REG_JNI(register_android_view_HardwareRenderer), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | REG_JNI(register_android_view_Surface), |
Jeff Brown | 64a55af | 2012-08-26 02:47:39 -0700 | [diff] [blame] | 1123 | REG_JNI(register_android_view_SurfaceSession), |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1124 | REG_JNI(register_android_view_TextureView), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | REG_JNI(register_com_google_android_gles_jni_EGLImpl), |
| 1126 | REG_JNI(register_com_google_android_gles_jni_GLImpl), |
Thomas Tafertshofer | 6b1e838 | 2012-07-03 13:37:35 -0700 | [diff] [blame] | 1127 | REG_JNI(register_android_opengl_jni_EGL14), |
Jack Palevich | 1c4907e | 2009-04-13 16:22:25 -0700 | [diff] [blame] | 1128 | REG_JNI(register_android_opengl_jni_GLES10), |
| 1129 | REG_JNI(register_android_opengl_jni_GLES10Ext), |
| 1130 | REG_JNI(register_android_opengl_jni_GLES11), |
| 1131 | REG_JNI(register_android_opengl_jni_GLES11Ext), |
Jack Palevich | 560814f | 2009-11-19 16:34:55 +0800 | [diff] [blame] | 1132 | REG_JNI(register_android_opengl_jni_GLES20), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | |
| 1134 | REG_JNI(register_android_graphics_Bitmap), |
| 1135 | REG_JNI(register_android_graphics_BitmapFactory), |
Wei-Ta Chen | 6b849e2 | 2010-09-07 17:32:18 +0800 | [diff] [blame] | 1136 | REG_JNI(register_android_graphics_BitmapRegionDecoder), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | REG_JNI(register_android_graphics_Camera), |
| 1138 | REG_JNI(register_android_graphics_Canvas), |
| 1139 | REG_JNI(register_android_graphics_ColorFilter), |
| 1140 | REG_JNI(register_android_graphics_DrawFilter), |
| 1141 | REG_JNI(register_android_graphics_Interpolator), |
| 1142 | REG_JNI(register_android_graphics_LayerRasterizer), |
| 1143 | REG_JNI(register_android_graphics_MaskFilter), |
| 1144 | REG_JNI(register_android_graphics_Matrix), |
| 1145 | REG_JNI(register_android_graphics_Movie), |
| 1146 | REG_JNI(register_android_graphics_NinePatch), |
| 1147 | REG_JNI(register_android_graphics_Paint), |
| 1148 | REG_JNI(register_android_graphics_Path), |
| 1149 | REG_JNI(register_android_graphics_PathMeasure), |
| 1150 | REG_JNI(register_android_graphics_PathEffect), |
| 1151 | REG_JNI(register_android_graphics_Picture), |
| 1152 | REG_JNI(register_android_graphics_PorterDuff), |
| 1153 | REG_JNI(register_android_graphics_Rasterizer), |
| 1154 | REG_JNI(register_android_graphics_Region), |
| 1155 | REG_JNI(register_android_graphics_Shader), |
Jamie Gennis | aa0ce33 | 2011-01-06 17:04:26 -0800 | [diff] [blame] | 1156 | REG_JNI(register_android_graphics_SurfaceTexture), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | REG_JNI(register_android_graphics_Typeface), |
| 1158 | REG_JNI(register_android_graphics_Xfermode), |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 1159 | REG_JNI(register_android_graphics_YuvImage), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | |
| 1161 | REG_JNI(register_android_database_CursorWindow), |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 1162 | REG_JNI(register_android_database_SQLiteConnection), |
| 1163 | REG_JNI(register_android_database_SQLiteGlobal), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | REG_JNI(register_android_database_SQLiteDebug), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | REG_JNI(register_android_os_Debug), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | REG_JNI(register_android_os_FileObserver), |
| 1167 | REG_JNI(register_android_os_FileUtils), |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 1168 | REG_JNI(register_android_os_MessageQueue), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | REG_JNI(register_android_os_ParcelFileDescriptor), |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 1170 | REG_JNI(register_android_os_SELinux), |
Jeff Brown | 481c157 | 2012-03-09 14:41:15 -0800 | [diff] [blame] | 1171 | REG_JNI(register_android_os_Trace), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | REG_JNI(register_android_os_UEventObserver), |
| 1173 | REG_JNI(register_android_net_LocalSocketImpl), |
| 1174 | REG_JNI(register_android_net_NetworkUtils), |
Dan Egnor | 2b4abcd | 2010-04-07 17:30:50 -0700 | [diff] [blame] | 1175 | REG_JNI(register_android_net_TrafficStats), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1176 | REG_JNI(register_android_net_wifi_WifiManager), |
| 1177 | REG_JNI(register_android_os_MemoryFile), |
| 1178 | REG_JNI(register_com_android_internal_os_ZygoteInit), |
| 1179 | REG_JNI(register_android_hardware_Camera), |
| 1180 | REG_JNI(register_android_hardware_SensorManager), |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 1181 | REG_JNI(register_android_hardware_SerialPort), |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 1182 | REG_JNI(register_android_hardware_UsbDevice), |
Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 1183 | REG_JNI(register_android_hardware_UsbDeviceConnection), |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 1184 | REG_JNI(register_android_hardware_UsbRequest), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | REG_JNI(register_android_media_AudioRecord), |
| 1186 | REG_JNI(register_android_media_AudioSystem), |
| 1187 | REG_JNI(register_android_media_AudioTrack), |
| 1188 | REG_JNI(register_android_media_JetPlayer), |
Jeff Brown | cbad976 | 2012-09-04 21:57:59 -0700 | [diff] [blame] | 1189 | REG_JNI(register_android_media_RemoteDisplay), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | REG_JNI(register_android_media_ToneGenerator), |
| 1191 | |
| 1192 | REG_JNI(register_android_opengl_classes), |
JP Abgrall | 98a4f7e | 2011-09-02 15:36:33 -0700 | [diff] [blame] | 1193 | REG_JNI(register_android_server_NetworkManagementSocketTagger), |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 1194 | REG_JNI(register_android_server_Watchdog), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | REG_JNI(register_android_ddm_DdmHandleNativeHeap), |
Joe Onorato | 1cf5874 | 2009-06-12 11:06:24 -0700 | [diff] [blame] | 1196 | REG_JNI(register_android_backup_BackupDataInput), |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 1197 | REG_JNI(register_android_backup_BackupDataOutput), |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 1198 | REG_JNI(register_android_backup_FileBackupHelperBase), |
Joe Onorato | 4ababd9 | 2009-06-25 18:29:18 -0400 | [diff] [blame] | 1199 | REG_JNI(register_android_backup_BackupHelperDispatcher), |
Christopher Tate | 4a627c7 | 2011-04-01 14:43:32 -0700 | [diff] [blame] | 1200 | REG_JNI(register_android_app_backup_FullBackup), |
Chet Haase | 9c1e23b | 2011-03-24 10:51:31 -0700 | [diff] [blame] | 1201 | REG_JNI(register_android_app_ActivityThread), |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1202 | REG_JNI(register_android_app_NativeActivity), |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1203 | REG_JNI(register_android_view_InputChannel), |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 1204 | REG_JNI(register_android_view_InputEventReceiver), |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1205 | REG_JNI(register_android_view_KeyEvent), |
| 1206 | REG_JNI(register_android_view_MotionEvent), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1207 | REG_JNI(register_android_view_PointerIcon), |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 1208 | REG_JNI(register_android_view_VelocityTracker), |
Kenny Root | 02c8730 | 2010-07-01 08:10:18 -0700 | [diff] [blame] | 1209 | |
| 1210 | REG_JNI(register_android_content_res_ObbScanner), |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 1211 | REG_JNI(register_android_content_res_Configuration), |
Chet Haase | 6e0ecb4 | 2010-11-03 19:41:18 -0700 | [diff] [blame] | 1212 | |
| 1213 | REG_JNI(register_android_animation_PropertyValuesHolder), |
Kenny Root | 66269ea | 2011-07-12 14:14:01 -0700 | [diff] [blame] | 1214 | REG_JNI(register_com_android_internal_content_NativeLibraryHelper), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | }; |
| 1216 | |
| 1217 | /* |
| 1218 | * Register android native functions with the VM. |
| 1219 | */ |
| 1220 | /*static*/ int AndroidRuntime::startReg(JNIEnv* env) |
| 1221 | { |
| 1222 | /* |
| 1223 | * This hook causes all future threads created in this process to be |
| 1224 | * attached to the JavaVM. (This needs to go away in favor of JNI |
| 1225 | * Attach calls.) |
| 1226 | */ |
| 1227 | androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc); |
| 1228 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1229 | ALOGV("--- registering native functions ---\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | |
| 1231 | /* |
| 1232 | * Every "register" function calls one or more things that return |
| 1233 | * a local reference (e.g. FindClass). Because we haven't really |
| 1234 | * started the VM yet, they're all getting stored in the base frame |
| 1235 | * and never released. Use Push/Pop to manage the storage. |
| 1236 | */ |
| 1237 | env->PushLocalFrame(200); |
| 1238 | |
| 1239 | if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) { |
| 1240 | env->PopLocalFrame(NULL); |
| 1241 | return -1; |
| 1242 | } |
| 1243 | env->PopLocalFrame(NULL); |
| 1244 | |
| 1245 | //createJavaThread("fubar", quickTest, (void*) "hello"); |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | AndroidRuntime* AndroidRuntime::getRuntime() |
| 1251 | { |
| 1252 | return gCurRuntime; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Used by WithFramework to register native functions. |
| 1257 | */ |
| 1258 | extern "C" |
| 1259 | jint Java_com_android_internal_util_WithFramework_registerNatives( |
| 1260 | JNIEnv* env, jclass clazz) { |
| 1261 | return register_jni_procs(gRegJNI, NELEM(gRegJNI), env); |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * Used by LoadClass to register native functions. |
| 1266 | */ |
| 1267 | extern "C" |
| 1268 | jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) { |
| 1269 | return register_jni_procs(gRegJNI, NELEM(gRegJNI), env); |
| 1270 | } |
| 1271 | |
| 1272 | } // namespace android |