blob: e376a0bc0001e209b04d37bb2e15b6c2a271c87d [file] [log] [blame]
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001/*
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 Project9066cfe2009-03-03 19:31:44 -080016
17#define LOG_TAG "AndroidRuntime"
18//#define LOG_NDEBUG 0
19
20#include <android_runtime/AndroidRuntime.h>
Mathias Agopian07952722009-05-19 19:08:10 -070021#include <binder/IBinder.h>
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -080022#include <binder/IPCThreadState.h>
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/Log.h>
25#include <utils/misc.h>
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/Parcel.h>
Mathias Agopian8ed6beb2009-06-05 01:26:23 -070027#include <utils/StringArray.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <utils/threads.h>
29#include <cutils/properties.h>
30
31#include <SkGraphics.h>
32#include <SkImageDecoder.h>
Mike Reedfc8db532009-04-27 11:43:30 -040033#include <SkImageRef_GlobalPool.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
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
48using namespace android;
49
50extern void register_BindTest();
51
52extern int register_android_os_Binder(JNIEnv* env);
53extern int register_android_os_Process(JNIEnv* env);
54extern int register_android_graphics_Bitmap(JNIEnv*);
55extern int register_android_graphics_BitmapFactory(JNIEnv*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080056extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057extern int register_android_graphics_Camera(JNIEnv* env);
58extern int register_android_graphics_Graphics(JNIEnv* env);
59extern int register_android_graphics_Interpolator(JNIEnv* env);
60extern int register_android_graphics_LayerRasterizer(JNIEnv*);
61extern int register_android_graphics_MaskFilter(JNIEnv* env);
62extern int register_android_graphics_Movie(JNIEnv* env);
63extern int register_android_graphics_NinePatch(JNIEnv*);
64extern int register_android_graphics_PathEffect(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065extern int register_android_graphics_Shader(JNIEnv* env);
66extern int register_android_graphics_Typeface(JNIEnv* env);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080067extern int register_android_graphics_YuvImage(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
70extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
Jack Palevich1c4907e2009-04-13 16:22:25 -070071extern int register_android_opengl_jni_GLES10(JNIEnv* env);
72extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
73extern int register_android_opengl_jni_GLES11(JNIEnv* env);
74extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
Jack Palevich560814f2009-11-19 16:34:55 +080075extern int register_android_opengl_jni_GLES20(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77extern int register_android_hardware_Camera(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078extern int register_android_hardware_SensorManager(JNIEnv *env);
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040079extern int register_android_hardware_SerialPort(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050080extern int register_android_hardware_UsbDevice(JNIEnv *env);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050081extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050082extern int register_android_hardware_UsbRequest(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84extern int register_android_media_AudioRecord(JNIEnv *env);
85extern int register_android_media_AudioSystem(JNIEnv *env);
86extern int register_android_media_AudioTrack(JNIEnv *env);
87extern int register_android_media_JetPlayer(JNIEnv *env);
88extern int register_android_media_ToneGenerator(JNIEnv *env);
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090extern int register_android_util_FloatMath(JNIEnv* env);
91
92namespace android {
93
94/*
95 * JNI-based registration functions. Note these are properly contained in
96 * namespace android.
97 */
98extern int register_android_content_AssetManager(JNIEnv* env);
99extern int register_android_util_EventLog(JNIEnv* env);
100extern int register_android_util_Log(JNIEnv* env);
101extern int register_android_content_StringBlock(JNIEnv* env);
102extern int register_android_content_XmlBlock(JNIEnv* env);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700103extern int register_android_emoji_EmojiFactory(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104extern int register_android_graphics_Canvas(JNIEnv* env);
105extern int register_android_graphics_ColorFilter(JNIEnv* env);
106extern int register_android_graphics_DrawFilter(JNIEnv* env);
107extern int register_android_graphics_Matrix(JNIEnv* env);
108extern int register_android_graphics_Paint(JNIEnv* env);
109extern int register_android_graphics_Path(JNIEnv* env);
110extern int register_android_graphics_PathMeasure(JNIEnv* env);
111extern int register_android_graphics_Picture(JNIEnv*);
112extern int register_android_graphics_PorterDuff(JNIEnv* env);
113extern int register_android_graphics_Rasterizer(JNIEnv* env);
Jeff Brownfbf09772011-01-16 14:06:57 -0800114extern int register_android_graphics_Region(JNIEnv* env);
Jamie Gennisaa0ce332011-01-06 17:04:26 -0800115extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116extern int register_android_graphics_Xfermode(JNIEnv* env);
117extern int register_android_graphics_PixelFormat(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118extern int register_android_view_Display(JNIEnv* env);
Jeff Brown0a0a1242011-12-02 02:25:22 -0800119extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
Chet Haasea1cff502012-02-21 13:43:44 -0800120extern int register_android_view_GLES20DisplayList(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700121extern int register_android_view_GLES20Canvas(JNIEnv* env);
Romain Guya9582652011-11-10 14:20:10 -0800122extern int register_android_view_HardwareRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123extern int register_android_view_Surface(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700124extern int register_android_view_TextureView(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125extern int register_android_database_CursorWindow(JNIEnv* env);
Jeff Browne5360fb2011-10-31 17:48:13 -0700126extern int register_android_database_SQLiteConnection(JNIEnv* env);
127extern int register_android_database_SQLiteGlobal(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128extern int register_android_database_SQLiteDebug(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129extern int register_android_debug_JNITest(JNIEnv* env);
130extern int register_android_nio_utils(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131extern int register_android_text_format_Time(JNIEnv* env);
132extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700133extern int register_android_os_MessageQueue(JNIEnv* env);
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800134extern int register_android_os_Parcel(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135extern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500136extern int register_android_os_SELinux(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138extern int register_android_os_SystemClock(JNIEnv* env);
Jeff Brown481c1572012-03-09 14:41:15 -0800139extern int register_android_os_Trace(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140extern int register_android_os_FileObserver(JNIEnv *env);
141extern int register_android_os_FileUtils(JNIEnv *env);
142extern int register_android_os_UEventObserver(JNIEnv* env);
143extern int register_android_os_MemoryFile(JNIEnv* env);
144extern int register_android_net_LocalSocketImpl(JNIEnv* env);
145extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700146extern int register_android_net_TrafficStats(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147extern int register_android_net_wifi_WifiManager(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148extern int register_android_text_AndroidCharacter(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800149extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150extern int register_android_opengl_classes(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151extern int register_android_bluetooth_HeadsetBase(JNIEnv* env);
152extern int register_android_bluetooth_BluetoothAudioGateway(JNIEnv* env);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700153extern int register_android_bluetooth_BluetoothSocket(JNIEnv *env);
Nick Pellybd022f42009-08-14 18:33:38 -0700154extern int register_android_server_BluetoothService(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155extern int register_android_server_BluetoothEventLoop(JNIEnv *env);
156extern int register_android_server_BluetoothA2dpService(JNIEnv* env);
JP Abgrall98a4f7e2011-09-02 15:36:33 -0700157extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
Christopher Tateecaa7b42010-06-04 14:55:02 -0700158extern int register_android_server_Watchdog(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
160extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700161extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700162extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700163extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400164extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Christopher Tate4a627c72011-04-01 14:43:32 -0700165extern int register_android_app_backup_FullBackup(JNIEnv *env);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700166extern int register_android_app_ActivityThread(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700167extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700168extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700169extern int register_android_view_InputDevice(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800170extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700171extern int register_android_view_KeyCharacterMap(JNIEnv *env);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700172extern int register_android_view_KeyEvent(JNIEnv* env);
173extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700174extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700175extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700176extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700177extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700178extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700179extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
181static AndroidRuntime* gCurRuntime = NULL;
182
183static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
184{
185 if (jniThrowException(env, exc, msg) != 0)
186 assert(false);
187}
188
189/*
190 * Code written in the Java Programming Language calls here from main().
191 */
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700192static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193{
194 gCurRuntime->onStarted();
195}
196
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700197static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198{
199 gCurRuntime->onZygoteInit();
200}
201
Jeff Brown4280c4a2012-03-15 17:48:02 -0700202static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv* env,
203 jobject clazz, jboolean exitWithoutCleanup)
204{
205 gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup);
206}
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208/*
209 * JNI registration.
210 */
211static JNINativeMethod gMethods[] = {
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700212 { "nativeFinishInit", "()V",
213 (void*) com_android_internal_os_RuntimeInit_nativeFinishInit },
214 { "nativeZygoteInit", "()V",
215 (void*) com_android_internal_os_RuntimeInit_nativeZygoteInit },
Jeff Brown4280c4a2012-03-15 17:48:02 -0700216 { "nativeSetExitWithoutCleanup", "(Z)V",
217 (void*) com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218};
219
220int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
221{
222 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
223 gMethods, NELEM(gMethods));
224}
225
226// ----------------------------------------------------------------------
227
228/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
229
230
Jeff Brown4280c4a2012-03-15 17:48:02 -0700231AndroidRuntime::AndroidRuntime() :
232 mExitWithoutCleanup(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700234 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 // this sets our preference for 16bit images during decode
236 // in case the src is opaque and 24bit
237 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400238 // This cache is shared between browser native images, and java "purgeable"
239 // bitmaps. This globalpool is for images that do not either use the java
240 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
241 // java call site.
242 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
243 // There is also a global font cache, but its budget is specified in code
244 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
246 // Pre-allocate enough space to hold a fair number of options.
247 mOptions.setCapacity(20);
248
249 assert(gCurRuntime == NULL); // one per process
250 gCurRuntime = this;
251}
252
253AndroidRuntime::~AndroidRuntime()
254{
255 SkGraphics::Term();
256}
257
258/*
259 * Register native methods using JNI.
260 */
261/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
262 const char* className, const JNINativeMethod* gMethods, int numMethods)
263{
264 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
265}
266
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700267status_t AndroidRuntime::callMain(const char* className,
268 jclass clazz, int argc, const char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269{
270 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 jmethodID methodId;
272
Steve Block5baa3a62011-12-20 16:23:08 +0000273 ALOGD("Calling main entry %s", className);
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700276 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 return UNKNOWN_ERROR;
278 }
279
280 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
281 if (methodId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000282 ALOGE("ERROR: could not find method %s.main(String[])\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 return UNKNOWN_ERROR;
284 }
285
286 /*
287 * We want to call main() with a String array with our arguments in it.
288 * Create an array and populate it.
289 */
290 jclass stringClass;
291 jobjectArray strArray;
292
293 stringClass = env->FindClass("java/lang/String");
294 strArray = env->NewObjectArray(argc, stringClass, NULL);
295
296 for (int i = 0; i < argc; i++) {
297 jstring argStr = env->NewStringUTF(argv[i]);
298 env->SetObjectArrayElement(strArray, i, argStr);
299 }
300
301 env->CallStaticVoidMethod(clazz, methodId, strArray);
302 return NO_ERROR;
303}
304
305/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * The VM calls this through the "exit" hook.
307 */
308static void runtime_exit(int code)
309{
Jeff Brown4280c4a2012-03-15 17:48:02 -0700310 gCurRuntime->exit(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311}
312
313/*
314 * The VM calls this through the "vfprintf" hook.
315 *
316 * We ignore "fp" and just write the results to the log file.
317 */
318static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
319{
320 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
321}
322
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800323/**
324 * The VM calls this when mutex contention debugging is enabled to
325 * determine whether or not the blocked thread was a "sensitive thread"
326 * for user responsiveness/smoothess.
327 *
328 * Our policy for this is whether or not we're tracing any StrictMode
329 * events on this thread (which we might've inherited via Binder calls
330 * into us)
331 */
332static bool runtime_isSensitiveThread() {
333 IPCThreadState* state = IPCThreadState::selfOrNull();
334 return state && state->getStrictModePolicy() != 0;
335}
336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
338/**
339 * Add VM arguments to the to-be-executed VM
340 * Stops at first non '-' argument (also stops at an argument of '--')
341 * Returns the number of args consumed
342 */
343int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
344{
345 int i;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 for (i = 0; i<argc; i++) {
348 if (argv[i][0] != '-') {
349 return i;
350 }
351 if (argv[i][1] == '-' && argv[i][2] == 0) {
352 return i+1;
353 }
354
355 JavaVMOption opt;
356 memset(&opt, 0, sizeof(opt));
357 opt.optionString = (char*)argv[i];
358 mOptions.add(opt);
359 }
360 return i;
361}
362
363static int hasDir(const char* dir)
364{
365 struct stat s;
366 int res = stat(dir, &s);
367 if (res == 0) {
368 return S_ISDIR(s.st_mode);
369 }
370 return 0;
371}
372
373/*
374 * We just want failed write() calls to just return with an error.
375 */
376static void blockSigpipe()
377{
378 sigset_t mask;
379
380 sigemptyset(&mask);
381 sigaddset(&mask, SIGPIPE);
382 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
Steve Block8564c8d2012-01-05 23:22:43 +0000383 ALOGW("WARNING: SIGPIPE not blocked\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384}
385
386/*
387 * Read the persistent locale.
388 */
389static void readLocale(char* language, char* region)
390{
391 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 property_get("persist.sys.language", propLang, "");
394 property_get("persist.sys.country", propRegn, "");
395 if (*propLang == 0 && *propRegn == 0) {
396 /* Set to ro properties, default is en_US */
397 property_get("ro.product.locale.language", propLang, "en");
398 property_get("ro.product.locale.region", propRegn, "US");
399 }
400 strncat(language, propLang, 2);
401 strncat(region, propRegn, 2);
Steve Block5baa3a62011-12-20 16:23:08 +0000402 //ALOGD("language=%s region=%s\n", language, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403}
404
Andy McFaddenf70188a2009-03-31 15:52:13 -0700405/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700406 * Parse a property containing space-separated options that should be
407 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
408 *
409 * This will cut up "extraOptsBuf" as we chop it into individual options.
410 *
411 * Adds the strings, if any, to mOptions.
412 */
413void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
414{
415 JavaVMOption opt;
416 char* start;
417 char* end;
418
419 memset(&opt, 0, sizeof(opt));
420 start = extraOptsBuf;
421 while (*start != '\0') {
422 while (*start == ' ') /* skip leading whitespace */
423 start++;
424 if (*start == '\0') /* was trailing ws, bail */
425 break;
426
427 end = start+1;
428 while (*end != ' ' && *end != '\0') /* find end of token */
429 end++;
430 if (*end == ' ')
431 *end++ = '\0'; /* mark end, advance to indicate more */
432
433 opt.optionString = start;
434 mOptions.add(opt);
435 start = end;
436 }
437}
438
439/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700440 * Start the Dalvik Virtual Machine.
441 *
442 * Various arguments, most determined by system properties, are passed in.
443 * The "mOptions" vector is updated.
444 *
445 * Returns 0 on success.
446 */
447int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700449 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 JavaVMInitArgs initArgs;
451 JavaVMOption opt;
452 char propBuf[PROPERTY_VALUE_MAX];
453 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
454 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
455 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
456 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800457 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700458 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c32011-01-19 12:33:12 -0800459 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700460 char extraOptsBuf[PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700463 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700465 enum {
466 kEMDefault,
467 kEMIntPortable,
468 kEMIntFast,
Ben Cheng52b0e732009-06-19 13:31:12 -0700469 kEMJitCompiler,
Ben Cheng52b0e732009-06-19 13:31:12 -0700470 } executionMode = kEMDefault;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472
473 property_get("dalvik.vm.checkjni", propBuf, "");
474 if (strcmp(propBuf, "true") == 0) {
475 checkJni = true;
476 } else if (strcmp(propBuf, "false") != 0) {
477 /* property is neither true nor false; fall back on kernel parameter */
478 property_get("ro.kernel.android.checkjni", propBuf, "");
479 if (propBuf[0] == '1') {
480 checkJni = true;
481 }
482 }
483
484 property_get("dalvik.vm.execution-mode", propBuf, "");
485 if (strcmp(propBuf, "int:portable") == 0) {
486 executionMode = kEMIntPortable;
487 } else if (strcmp(propBuf, "int:fast") == 0) {
488 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700489 } else if (strcmp(propBuf, "int:jit") == 0) {
490 executionMode = kEMJitCompiler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
492
493 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
494
Andy McFaddene2b23e12009-04-03 11:09:46 -0700495 property_get("dalvik.vm.check-dex-sum", propBuf, "");
496 if (strcmp(propBuf, "true") == 0) {
497 checkDexSum = true;
498 }
499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 property_get("log.redirect-stdio", propBuf, "");
501 if (strcmp(propBuf, "true") == 0) {
502 logStdio = true;
503 }
504
505 strcpy(enableAssertBuf, "-ea:");
506 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
507
508 strcpy(jniOptsBuf, "-Xjniopts:");
509 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 /* route exit() to our handler */
512 opt.extraInfo = (void*) runtime_exit;
513 opt.optionString = "exit";
514 mOptions.add(opt);
515
516 /* route fprintf() to our handler */
517 opt.extraInfo = (void*) runtime_vfprintf;
518 opt.optionString = "vfprintf";
519 mOptions.add(opt);
520
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800521 /* register the framework-specific "is sensitive thread" hook */
522 opt.extraInfo = (void*) runtime_isSensitiveThread;
523 opt.optionString = "sensitiveThread";
524 mOptions.add(opt);
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 opt.extraInfo = NULL;
527
528 /* enable verbose; standard options are { jni, gc, class } */
529 //options[curOpt++].optionString = "-verbose:jni";
530 opt.optionString = "-verbose:gc";
531 mOptions.add(opt);
532 //options[curOpt++].optionString = "-verbose:class";
533
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800534 /*
535 * The default starting and maximum size of the heap. Larger
536 * values should be specified in a product property override.
537 */
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800538 strcpy(heapstartsizeOptsBuf, "-Xms");
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800539 property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800540 opt.optionString = heapstartsizeOptsBuf;
541 mOptions.add(opt);
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700542 strcpy(heapsizeOptsBuf, "-Xmx");
543 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700544 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 mOptions.add(opt);
546
Elliott Hughes3f177d72012-04-12 16:02:56 -0700547 // Increase the main thread's interpreter stack size for bug 6315322.
548 opt.optionString = "-XX:mainThreadStackSize=24K";
549 mOptions.add(opt);
550
Carl Shapiro8cdf27c32011-01-19 12:33:12 -0800551 strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
552 property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
553 if (heapgrowthlimitOptsBuf[20] != '\0') {
554 opt.optionString = heapgrowthlimitOptsBuf;
555 mOptions.add(opt);
556 }
557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 /*
559 * Enable or disable dexopt features, such as bytecode verification and
560 * calculation of register maps for precise GC.
561 */
562 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
563 if (dexoptFlagsBuf[0] != '\0') {
564 const char* opc;
565 const char* val;
566
567 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
568 if (opc != NULL) {
569 switch (*(opc+2)) {
570 case 'n': val = "-Xverify:none"; break;
571 case 'r': val = "-Xverify:remote"; break;
572 case 'a': val = "-Xverify:all"; break;
573 default: val = NULL; break;
574 }
575
576 if (val != NULL) {
577 opt.optionString = val;
578 mOptions.add(opt);
579 }
580 }
581
582 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
583 if (opc != NULL) {
584 switch (*(opc+2)) {
585 case 'n': val = "-Xdexopt:none"; break;
586 case 'v': val = "-Xdexopt:verified"; break;
587 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700588 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 default: val = NULL; break;
590 }
591
592 if (val != NULL) {
593 opt.optionString = val;
594 mOptions.add(opt);
595 }
596 }
597
598 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
599 if (opc != NULL) {
600 opt.optionString = "-Xgenregmap";
601 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700602
603 /* turn on precise GC while we're at it */
604 opt.optionString = "-Xgc:precise";
605 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
607 }
608
609 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 /* use android ADB transport */
611 opt.optionString =
612 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 mOptions.add(opt);
614
Steve Block5baa3a62011-12-20 16:23:08 +0000615 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 if (checkJni) {
617 /* extended JNI checking */
618 opt.optionString = "-Xcheck:jni";
619 mOptions.add(opt);
620
621 /* set a cap on JNI global references */
622 opt.optionString = "-Xjnigreflimit:2000";
623 mOptions.add(opt);
624
625 /* with -Xcheck:jni, this provides a JNI function call trace */
626 //opt.optionString = "-verbose:jni";
627 //mOptions.add(opt);
628 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700629
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700630 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
631 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
632 if (strlen(propBuf) > 0) {
633 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
634 strcat(lockProfThresholdBuf, propBuf);
635 opt.optionString = lockProfThresholdBuf;
636 mOptions.add(opt);
637 }
638
Ben Cheng52b0e732009-06-19 13:31:12 -0700639 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
640 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
641 property_get("dalvik.vm.jit.op", propBuf, "");
642 if (strlen(propBuf) > 0) {
643 strcpy(jitOpBuf, "-Xjitop:");
644 strcat(jitOpBuf, propBuf);
645 opt.optionString = jitOpBuf;
646 mOptions.add(opt);
647 }
648
Ben Cheng52b0e732009-06-19 13:31:12 -0700649 /* Force interpreter-only mode for selected methods */
650 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
651 property_get("dalvik.vm.jit.method", propBuf, "");
652 if (strlen(propBuf) > 0) {
653 strcpy(jitMethodBuf, "-Xjitmethod:");
654 strcat(jitMethodBuf, propBuf);
655 opt.optionString = jitMethodBuf;
656 mOptions.add(opt);
657 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 if (executionMode == kEMIntPortable) {
660 opt.optionString = "-Xint:portable";
661 mOptions.add(opt);
662 } else if (executionMode == kEMIntFast) {
663 opt.optionString = "-Xint:fast";
664 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700665 } else if (executionMode == kEMJitCompiler) {
666 opt.optionString = "-Xint:jit";
667 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700669
670 if (checkDexSum) {
671 /* perform additional DEX checksum tests */
672 opt.optionString = "-Xcheckdexsum";
673 mOptions.add(opt);
674 }
675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 if (logStdio) {
677 /* convert stdout/stderr to log messages */
678 opt.optionString = "-Xlog-stdio";
679 mOptions.add(opt);
680 }
681
682 if (enableAssertBuf[4] != '\0') {
683 /* accept "all" to mean "all classes and packages" */
684 if (strcmp(enableAssertBuf+4, "all") == 0)
685 enableAssertBuf[3] = '\0';
Steve Block6215d3f2012-01-04 20:05:49 +0000686 ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 opt.optionString = enableAssertBuf;
688 mOptions.add(opt);
689 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100690 ALOGV("Assertions disabled\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692
693 if (jniOptsBuf[10] != '\0') {
Steve Block6215d3f2012-01-04 20:05:49 +0000694 ALOGI("JNI options: '%s'\n", jniOptsBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 opt.optionString = jniOptsBuf;
696 mOptions.add(opt);
697 }
698
699 if (stackTraceFileBuf[0] != '\0') {
700 static const char* stfOptName = "-Xstacktracefile:";
701
702 stackTraceFile = (char*) malloc(strlen(stfOptName) +
703 strlen(stackTraceFileBuf) +1);
704 strcpy(stackTraceFile, stfOptName);
705 strcat(stackTraceFile, stackTraceFileBuf);
706 opt.optionString = stackTraceFile;
707 mOptions.add(opt);
708 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700709
710 /* extra options; parse this late so it overrides others */
711 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
712 parseExtraOpts(extraOptsBuf);
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 /* Set the properties for locale */
715 {
716 char langOption[sizeof("-Duser.language=") + 3];
717 char regionOption[sizeof("-Duser.region=") + 3];
718 strcpy(langOption, "-Duser.language=");
719 strcpy(regionOption, "-Duser.region=");
720 readLocale(langOption, regionOption);
721 opt.extraInfo = NULL;
722 opt.optionString = langOption;
723 mOptions.add(opt);
724 opt.optionString = regionOption;
725 mOptions.add(opt);
726 }
727
728 /*
729 * We don't have /tmp on the device, but we often have an SD card. Apps
730 * shouldn't use this, but some test suites might want to exercise it.
731 */
732 opt.optionString = "-Djava.io.tmpdir=/sdcard";
733 mOptions.add(opt);
734
735 initArgs.version = JNI_VERSION_1_4;
736 initArgs.options = mOptions.editArray();
737 initArgs.nOptions = mOptions.size();
738 initArgs.ignoreUnrecognized = JNI_FALSE;
739
740 /*
741 * Initialize the VM.
742 *
743 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
744 * If this call succeeds, the VM is ready, and we can start issuing
745 * JNI calls.
746 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700747 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000748 ALOGE("JNI_CreateJavaVM failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 goto bail;
750 }
751
Andy McFaddenf70188a2009-03-31 15:52:13 -0700752 result = 0;
753
754bail:
755 free(stackTraceFile);
756 return result;
757}
758
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700759char* AndroidRuntime::toSlashClassName(const char* className)
760{
761 char* result = strdup(className);
762 for (char* cp = result; *cp != '\0'; cp++) {
763 if (*cp == '.') {
764 *cp = '/';
765 }
766 }
767 return result;
768}
769
Andy McFaddenf70188a2009-03-31 15:52:13 -0700770/*
771 * Start the Android runtime. This involves starting the virtual machine
772 * and calling the "static void main(String[] args)" method in the class
773 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700774 *
775 * Passes the main function two arguments, the class name and the specified
776 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700777 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700778void AndroidRuntime::start(const char* className, const char* options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700779{
Steve Block5baa3a62011-12-20 16:23:08 +0000780 ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700781 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700782
Andy McFaddenf70188a2009-03-31 15:52:13 -0700783 blockSigpipe();
784
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700785 /*
786 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700787 * init.rc anymore, so we print out the boot start event here.
788 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700789 if (strcmp(options, "start-system-server") == 0) {
Andy McFaddenf70188a2009-03-31 15:52:13 -0700790 /* track our progress through the boot sequence */
791 const int LOG_BOOT_PROGRESS_START = 3000;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700792 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
Andy McFaddenf70188a2009-03-31 15:52:13 -0700793 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
794 }
795
796 const char* rootDir = getenv("ANDROID_ROOT");
797 if (rootDir == NULL) {
798 rootDir = "/system";
799 if (!hasDir("/system")) {
800 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700801 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700802 }
803 setenv("ANDROID_ROOT", rootDir, 1);
804 }
805
806 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block5baa3a62011-12-20 16:23:08 +0000807 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700808
809 /* start the virtual machine */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700810 JNIEnv* env;
811 if (startVm(&mJavaVM, &env) != 0) {
812 return;
813 }
814 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 /*
817 * Register android functions.
818 */
819 if (startReg(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000820 ALOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700821 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
823
824 /*
825 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700826 * At present we have two arguments, the class name and an option string.
827 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 */
829 jclass stringClass;
830 jobjectArray strArray;
831 jstring classNameStr;
Jeff Brownebed7d62011-05-16 17:08:42 -0700832 jstring optionsStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833
834 stringClass = env->FindClass("java/lang/String");
835 assert(stringClass != NULL);
836 strArray = env->NewObjectArray(2, stringClass, NULL);
837 assert(strArray != NULL);
838 classNameStr = env->NewStringUTF(className);
839 assert(classNameStr != NULL);
840 env->SetObjectArrayElement(strArray, 0, classNameStr);
Jeff Brownebed7d62011-05-16 17:08:42 -0700841 optionsStr = env->NewStringUTF(options);
842 env->SetObjectArrayElement(strArray, 1, optionsStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843
844 /*
845 * Start VM. This thread becomes the main thread of the VM, and will
846 * not return until the VM exits.
847 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700848 char* slashClassName = toSlashClassName(className);
849 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 if (startClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000851 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 /* keep going */
853 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700854 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 "([Ljava/lang/String;)V");
856 if (startMeth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000857 ALOGE("JavaVM unable to find main() in '%s'\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 /* keep going */
859 } else {
860 env->CallStaticVoidMethod(startClass, startMeth, strArray);
861
862#if 0
863 if (env->ExceptionCheck())
864 threadExitUncaughtException(env);
865#endif
866 }
867 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700868 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869
Steve Block5baa3a62011-12-20 16:23:08 +0000870 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 if (mJavaVM->DetachCurrentThread() != JNI_OK)
Steve Block8564c8d2012-01-05 23:22:43 +0000872 ALOGW("Warning: unable to detach main thread\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 if (mJavaVM->DestroyJavaVM() != 0)
Steve Block8564c8d2012-01-05 23:22:43 +0000874 ALOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875}
876
Jeff Brown4280c4a2012-03-15 17:48:02 -0700877void AndroidRuntime::exit(int code)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878{
Jeff Brown4280c4a2012-03-15 17:48:02 -0700879 if (mExitWithoutCleanup) {
880 ALOGI("VM exiting with result code %d, cleanup skipped.", code);
881 ::_exit(code);
882 } else {
883 ALOGI("VM exiting with result code %d.", code);
884 onExit(code);
885 ::exit(code);
886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887}
888
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700889void AndroidRuntime::onVmCreated(JNIEnv* env)
890{
891 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
892}
893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894/*
895 * Get the JNIEnv pointer for this thread.
896 *
897 * Returns NULL if the slot wasn't allocated or populated.
898 */
899/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
900{
901 JNIEnv* env;
902 JavaVM* vm = AndroidRuntime::getJavaVM();
903 assert(vm != NULL);
904
905 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
906 return NULL;
907 return env;
908}
909
910/*
911 * Makes the current thread visible to the VM.
912 *
913 * The JNIEnv pointer returned is only valid for the current thread, and
914 * thus must be tucked into thread-local storage.
915 */
916static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
917{
918 JavaVMAttachArgs args;
919 JavaVM* vm;
920 jint result;
921
922 vm = AndroidRuntime::getJavaVM();
923 assert(vm != NULL);
924
925 args.version = JNI_VERSION_1_4;
926 args.name = (char*) threadName;
927 args.group = NULL;
928
929 result = vm->AttachCurrentThread(pEnv, (void*) &args);
930 if (result != JNI_OK)
Steve Block6215d3f2012-01-04 20:05:49 +0000931 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932
933 return result;
934}
935
936/*
937 * Detach the current thread from the set visible to the VM.
938 */
939static int javaDetachThread(void)
940{
941 JavaVM* vm;
942 jint result;
943
944 vm = AndroidRuntime::getJavaVM();
945 assert(vm != NULL);
946
947 result = vm->DetachCurrentThread();
948 if (result != JNI_OK)
Steve Block3762c312012-01-06 19:20:56 +0000949 ALOGE("ERROR: thread detach failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 return result;
951}
952
953/*
954 * When starting a native thread that will be visible from the VM, we
955 * bounce through this to get the right attach/detach action.
956 * Note that this function calls free(args)
957 */
958/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
959 void* start = ((void**)args)[0];
960 void* userData = ((void **)args)[1];
961 char* name = (char*) ((void **)args)[2]; // we own this storage
962 free(args);
963 JNIEnv* env;
964 int result;
965
966 /* hook us into the VM */
967 if (javaAttachThread(name, &env) != JNI_OK)
968 return -1;
969
970 /* start the thread running */
971 result = (*(android_thread_func_t)start)(userData);
972
973 /* unhook us */
974 javaDetachThread();
975 free(name);
976
977 return result;
978}
979
980/*
981 * This is invoked from androidCreateThreadEtc() via the callback
982 * set with androidSetCreateThreadFunc().
983 *
984 * We need to create the new thread in such a way that it gets hooked
985 * into the VM before it really starts executing.
986 */
987/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700988 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 void* userData,
990 const char* threadName,
991 int32_t threadPriority,
992 size_t threadStackSize,
993 android_thread_id_t* threadId)
994{
995 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
996 int result;
997
998 assert(threadName != NULL);
999
1000 args[0] = (void*) entryFunction;
1001 args[1] = userData;
1002 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1003
1004 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1005 threadName, threadPriority, threadStackSize, threadId);
1006 return result;
1007}
1008
1009/*
1010 * Create a thread that is visible from the VM.
1011 *
1012 * This is called from elsewhere in the library.
1013 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001014/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 void (*start)(void *), void* arg)
1016{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001017 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001019 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1020 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021}
1022
1023#if 0
1024static void quickTest(void* arg)
1025{
1026 const char* str = (const char*) arg;
1027
1028 printf("In quickTest: %s\n", str);
1029}
1030#endif
1031
1032#ifdef NDEBUG
1033 #define REG_JNI(name) { name }
1034 struct RegJNIRec {
1035 int (*mProc)(JNIEnv*);
1036 };
1037#else
1038 #define REG_JNI(name) { name, #name }
1039 struct RegJNIRec {
1040 int (*mProc)(JNIEnv*);
1041 const char* mName;
1042 };
1043#endif
1044
1045typedef void (*RegJAMProc)();
1046
1047static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1048{
1049 for (size_t i = 0; i < count; i++) {
1050 if (array[i].mProc(env) < 0) {
1051#ifndef NDEBUG
Steve Block5baa3a62011-12-20 16:23:08 +00001052 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053#endif
1054 return -1;
1055 }
1056 }
1057 return 0;
1058}
1059
1060static void register_jam_procs(const RegJAMProc array[], size_t count)
1061{
1062 for (size_t i = 0; i < count; i++) {
1063 array[i]();
1064 }
1065}
1066
1067static const RegJNIRec gRegJNI[] = {
1068 REG_JNI(register_android_debug_JNITest),
1069 REG_JNI(register_com_android_internal_os_RuntimeInit),
1070 REG_JNI(register_android_os_SystemClock),
1071 REG_JNI(register_android_util_EventLog),
1072 REG_JNI(register_android_util_Log),
1073 REG_JNI(register_android_util_FloatMath),
1074 REG_JNI(register_android_text_format_Time),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 REG_JNI(register_android_content_AssetManager),
1076 REG_JNI(register_android_content_StringBlock),
1077 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001078 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001080 REG_JNI(register_android_text_AndroidBidi),
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001081 REG_JNI(register_android_view_InputDevice),
1082 REG_JNI(register_android_view_KeyCharacterMap),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001084 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 REG_JNI(register_android_os_Binder),
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001086 REG_JNI(register_android_os_Parcel),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 REG_JNI(register_android_view_Display),
Jeff Brown0a0a1242011-12-02 02:25:22 -08001088 REG_JNI(register_android_view_DisplayEventReceiver),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 REG_JNI(register_android_nio_utils),
1090 REG_JNI(register_android_graphics_PixelFormat),
1091 REG_JNI(register_android_graphics_Graphics),
Chet Haasea1cff502012-02-21 13:43:44 -08001092 REG_JNI(register_android_view_GLES20DisplayList),
Romain Guye4d01122010-06-16 18:44:05 -07001093 REG_JNI(register_android_view_GLES20Canvas),
Romain Guya9582652011-11-10 14:20:10 -08001094 REG_JNI(register_android_view_HardwareRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 REG_JNI(register_android_view_Surface),
Romain Guy8f0095c2011-05-02 17:24:22 -07001096 REG_JNI(register_android_view_TextureView),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1098 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001099 REG_JNI(register_android_opengl_jni_GLES10),
1100 REG_JNI(register_android_opengl_jni_GLES10Ext),
1101 REG_JNI(register_android_opengl_jni_GLES11),
1102 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001103 REG_JNI(register_android_opengl_jni_GLES20),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104
1105 REG_JNI(register_android_graphics_Bitmap),
1106 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001107 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 REG_JNI(register_android_graphics_Camera),
1109 REG_JNI(register_android_graphics_Canvas),
1110 REG_JNI(register_android_graphics_ColorFilter),
1111 REG_JNI(register_android_graphics_DrawFilter),
1112 REG_JNI(register_android_graphics_Interpolator),
1113 REG_JNI(register_android_graphics_LayerRasterizer),
1114 REG_JNI(register_android_graphics_MaskFilter),
1115 REG_JNI(register_android_graphics_Matrix),
1116 REG_JNI(register_android_graphics_Movie),
1117 REG_JNI(register_android_graphics_NinePatch),
1118 REG_JNI(register_android_graphics_Paint),
1119 REG_JNI(register_android_graphics_Path),
1120 REG_JNI(register_android_graphics_PathMeasure),
1121 REG_JNI(register_android_graphics_PathEffect),
1122 REG_JNI(register_android_graphics_Picture),
1123 REG_JNI(register_android_graphics_PorterDuff),
1124 REG_JNI(register_android_graphics_Rasterizer),
1125 REG_JNI(register_android_graphics_Region),
1126 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001127 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 REG_JNI(register_android_graphics_Typeface),
1129 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001130 REG_JNI(register_android_graphics_YuvImage),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131
1132 REG_JNI(register_android_database_CursorWindow),
Jeff Browne5360fb2011-10-31 17:48:13 -07001133 REG_JNI(register_android_database_SQLiteConnection),
1134 REG_JNI(register_android_database_SQLiteGlobal),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 REG_JNI(register_android_database_SQLiteDebug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 REG_JNI(register_android_os_FileObserver),
1138 REG_JNI(register_android_os_FileUtils),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001139 REG_JNI(register_android_os_MessageQueue),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 REG_JNI(register_android_os_ParcelFileDescriptor),
Stephen Smalleyc07fca32012-01-13 08:31:39 -05001141 REG_JNI(register_android_os_SELinux),
Jeff Brown481c1572012-03-09 14:41:15 -08001142 REG_JNI(register_android_os_Trace),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 REG_JNI(register_android_os_UEventObserver),
1144 REG_JNI(register_android_net_LocalSocketImpl),
1145 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001146 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 REG_JNI(register_android_net_wifi_WifiManager),
1148 REG_JNI(register_android_os_MemoryFile),
1149 REG_JNI(register_com_android_internal_os_ZygoteInit),
1150 REG_JNI(register_android_hardware_Camera),
1151 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -04001152 REG_JNI(register_android_hardware_SerialPort),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001153 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001154 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001155 REG_JNI(register_android_hardware_UsbRequest),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 REG_JNI(register_android_media_AudioRecord),
1157 REG_JNI(register_android_media_AudioSystem),
1158 REG_JNI(register_android_media_AudioTrack),
1159 REG_JNI(register_android_media_JetPlayer),
1160 REG_JNI(register_android_media_ToneGenerator),
1161
1162 REG_JNI(register_android_opengl_classes),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 REG_JNI(register_android_bluetooth_HeadsetBase),
1164 REG_JNI(register_android_bluetooth_BluetoothAudioGateway),
Nick Pelly0b6955a2009-05-26 19:13:43 -07001165 REG_JNI(register_android_bluetooth_BluetoothSocket),
Nick Pellybd022f42009-08-14 18:33:38 -07001166 REG_JNI(register_android_server_BluetoothService),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 REG_JNI(register_android_server_BluetoothEventLoop),
1168 REG_JNI(register_android_server_BluetoothA2dpService),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001169 REG_JNI(register_android_server_NetworkManagementSocketTagger),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001170 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001172 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001173 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001174 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001175 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001176 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001177 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001178 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001179 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001180 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001181 REG_JNI(register_android_view_KeyEvent),
1182 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001183 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001184 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001185
1186 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001187 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001188
1189 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001190 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191};
1192
1193/*
1194 * Register android native functions with the VM.
1195 */
1196/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1197{
1198 /*
1199 * This hook causes all future threads created in this process to be
1200 * attached to the JavaVM. (This needs to go away in favor of JNI
1201 * Attach calls.)
1202 */
1203 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1204
Steve Block71f2cf12011-10-20 11:56:00 +01001205 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206
1207 /*
1208 * Every "register" function calls one or more things that return
1209 * a local reference (e.g. FindClass). Because we haven't really
1210 * started the VM yet, they're all getting stored in the base frame
1211 * and never released. Use Push/Pop to manage the storage.
1212 */
1213 env->PushLocalFrame(200);
1214
1215 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1216 env->PopLocalFrame(NULL);
1217 return -1;
1218 }
1219 env->PopLocalFrame(NULL);
1220
1221 //createJavaThread("fubar", quickTest, (void*) "hello");
1222
1223 return 0;
1224}
1225
1226AndroidRuntime* AndroidRuntime::getRuntime()
1227{
1228 return gCurRuntime;
1229}
1230
1231/**
1232 * Used by WithFramework to register native functions.
1233 */
1234extern "C"
1235jint Java_com_android_internal_util_WithFramework_registerNatives(
1236 JNIEnv* env, jclass clazz) {
1237 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1238}
1239
1240/**
1241 * Used by LoadClass to register native functions.
1242 */
1243extern "C"
1244jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1245 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1246}
1247
1248} // namespace android