blob: de9fd33d053b56c8024aa10d7a08e4622b5b0628 [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);
Romain Guye4d01122010-06-16 18:44:05 -0700120extern int register_android_view_GLES20Canvas(JNIEnv* env);
Romain Guya9582652011-11-10 14:20:10 -0800121extern int register_android_view_HardwareRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122extern int register_android_view_Surface(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700123extern int register_android_view_TextureView(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124extern int register_android_database_CursorWindow(JNIEnv* env);
Jeff Browne5360fb2011-10-31 17:48:13 -0700125extern int register_android_database_SQLiteConnection(JNIEnv* env);
126extern int register_android_database_SQLiteGlobal(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127extern int register_android_database_SQLiteDebug(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128extern int register_android_debug_JNITest(JNIEnv* env);
129extern int register_android_nio_utils(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130extern int register_android_text_format_Time(JNIEnv* env);
131extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700132extern int register_android_os_MessageQueue(JNIEnv* env);
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800133extern int register_android_os_Parcel(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134extern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
135extern int register_android_os_Power(JNIEnv *env);
136extern int register_android_os_StatFs(JNIEnv *env);
137extern 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);
139extern int register_android_os_FileObserver(JNIEnv *env);
140extern int register_android_os_FileUtils(JNIEnv *env);
141extern int register_android_os_UEventObserver(JNIEnv* env);
142extern int register_android_os_MemoryFile(JNIEnv* env);
143extern int register_android_net_LocalSocketImpl(JNIEnv* env);
144extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700145extern int register_android_net_TrafficStats(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146extern int register_android_net_wifi_WifiManager(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147extern int register_android_text_AndroidCharacter(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800148extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149extern int register_android_text_KeyCharacterMap(JNIEnv *env);
150extern 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 Brown46b9ac02010-04-22 18:58:52 -0700168extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800169extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700170extern int register_android_view_KeyEvent(JNIEnv* env);
171extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700172extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700173extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700174extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700175extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700176extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700177extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
179static AndroidRuntime* gCurRuntime = NULL;
180
181static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
182{
183 if (jniThrowException(env, exc, msg) != 0)
184 assert(false);
185}
186
187/*
188 * Code written in the Java Programming Language calls here from main().
189 */
190static void com_android_internal_os_RuntimeInit_finishInit(JNIEnv* env, jobject clazz)
191{
192 gCurRuntime->onStarted();
193}
194
195static void com_android_internal_os_RuntimeInit_zygoteInit(JNIEnv* env, jobject clazz)
196{
197 gCurRuntime->onZygoteInit();
198}
199
200static jint com_android_internal_os_RuntimeInit_isComputerOn(JNIEnv* env, jobject clazz)
201{
202 return 1;
203}
204
205static void com_android_internal_os_RuntimeInit_turnComputerOn(JNIEnv* env, jobject clazz)
206{
207}
208
209static jint com_android_internal_os_RuntimeInit_getQwertyKeyboard(JNIEnv* env, jobject clazz)
210{
211 char* value = getenv("qwerty");
212 if (value != NULL && strcmp(value, "true") == 0) {
213 return 1;
214 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 return 0;
217}
218
219/*
220 * JNI registration.
221 */
222static JNINativeMethod gMethods[] = {
223 { "finishInit", "()V",
224 (void*) com_android_internal_os_RuntimeInit_finishInit },
225 { "zygoteInitNative", "()V",
226 (void*) com_android_internal_os_RuntimeInit_zygoteInit },
227 { "isComputerOn", "()I",
228 (void*) com_android_internal_os_RuntimeInit_isComputerOn },
229 { "turnComputerOn", "()V",
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700230 (void*) com_android_internal_os_RuntimeInit_turnComputerOn },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 { "getQwertyKeyboard", "()I",
232 (void*) com_android_internal_os_RuntimeInit_getQwertyKeyboard },
233};
234
235int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
236{
237 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
238 gMethods, NELEM(gMethods));
239}
240
241// ----------------------------------------------------------------------
242
243/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
244
245
246AndroidRuntime::AndroidRuntime()
247{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700248 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 // this sets our preference for 16bit images during decode
250 // in case the src is opaque and 24bit
251 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400252 // This cache is shared between browser native images, and java "purgeable"
253 // bitmaps. This globalpool is for images that do not either use the java
254 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
255 // java call site.
256 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
257 // There is also a global font cache, but its budget is specified in code
258 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
260 // Pre-allocate enough space to hold a fair number of options.
261 mOptions.setCapacity(20);
262
263 assert(gCurRuntime == NULL); // one per process
264 gCurRuntime = this;
265}
266
267AndroidRuntime::~AndroidRuntime()
268{
269 SkGraphics::Term();
270}
271
272/*
273 * Register native methods using JNI.
274 */
275/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
276 const char* className, const JNINativeMethod* gMethods, int numMethods)
277{
278 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
279}
280
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700281status_t AndroidRuntime::callMain(const char* className,
282 jclass clazz, int argc, const char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283{
284 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 jmethodID methodId;
286
Steve Block5baa3a62011-12-20 16:23:08 +0000287 ALOGD("Calling main entry %s", className);
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700290 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 return UNKNOWN_ERROR;
292 }
293
294 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
295 if (methodId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000296 ALOGE("ERROR: could not find method %s.main(String[])\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 return UNKNOWN_ERROR;
298 }
299
300 /*
301 * We want to call main() with a String array with our arguments in it.
302 * Create an array and populate it.
303 */
304 jclass stringClass;
305 jobjectArray strArray;
306
307 stringClass = env->FindClass("java/lang/String");
308 strArray = env->NewObjectArray(argc, stringClass, NULL);
309
310 for (int i = 0; i < argc; i++) {
311 jstring argStr = env->NewStringUTF(argv[i]);
312 env->SetObjectArrayElement(strArray, i, argStr);
313 }
314
315 env->CallStaticVoidMethod(clazz, methodId, strArray);
316 return NO_ERROR;
317}
318
319/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 * The VM calls this through the "exit" hook.
321 */
322static void runtime_exit(int code)
323{
324 gCurRuntime->onExit(code);
325 exit(code);
326}
327
328/*
329 * The VM calls this through the "vfprintf" hook.
330 *
331 * We ignore "fp" and just write the results to the log file.
332 */
333static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
334{
335 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
336}
337
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800338/**
339 * The VM calls this when mutex contention debugging is enabled to
340 * determine whether or not the blocked thread was a "sensitive thread"
341 * for user responsiveness/smoothess.
342 *
343 * Our policy for this is whether or not we're tracing any StrictMode
344 * events on this thread (which we might've inherited via Binder calls
345 * into us)
346 */
347static bool runtime_isSensitiveThread() {
348 IPCThreadState* state = IPCThreadState::selfOrNull();
349 return state && state->getStrictModePolicy() != 0;
350}
351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352
353/**
354 * Add VM arguments to the to-be-executed VM
355 * Stops at first non '-' argument (also stops at an argument of '--')
356 * Returns the number of args consumed
357 */
358int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
359{
360 int i;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 for (i = 0; i<argc; i++) {
363 if (argv[i][0] != '-') {
364 return i;
365 }
366 if (argv[i][1] == '-' && argv[i][2] == 0) {
367 return i+1;
368 }
369
370 JavaVMOption opt;
371 memset(&opt, 0, sizeof(opt));
372 opt.optionString = (char*)argv[i];
373 mOptions.add(opt);
374 }
375 return i;
376}
377
378static int hasDir(const char* dir)
379{
380 struct stat s;
381 int res = stat(dir, &s);
382 if (res == 0) {
383 return S_ISDIR(s.st_mode);
384 }
385 return 0;
386}
387
388/*
389 * We just want failed write() calls to just return with an error.
390 */
391static void blockSigpipe()
392{
393 sigset_t mask;
394
395 sigemptyset(&mask);
396 sigaddset(&mask, SIGPIPE);
397 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
Steve Block8564c8d2012-01-05 23:22:43 +0000398 ALOGW("WARNING: SIGPIPE not blocked\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399}
400
401/*
402 * Read the persistent locale.
403 */
404static void readLocale(char* language, char* region)
405{
406 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 property_get("persist.sys.language", propLang, "");
409 property_get("persist.sys.country", propRegn, "");
410 if (*propLang == 0 && *propRegn == 0) {
411 /* Set to ro properties, default is en_US */
412 property_get("ro.product.locale.language", propLang, "en");
413 property_get("ro.product.locale.region", propRegn, "US");
414 }
415 strncat(language, propLang, 2);
416 strncat(region, propRegn, 2);
Steve Block5baa3a62011-12-20 16:23:08 +0000417 //ALOGD("language=%s region=%s\n", language, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418}
419
Andy McFaddenf70188a2009-03-31 15:52:13 -0700420/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700421 * Parse a property containing space-separated options that should be
422 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
423 *
424 * This will cut up "extraOptsBuf" as we chop it into individual options.
425 *
426 * Adds the strings, if any, to mOptions.
427 */
428void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
429{
430 JavaVMOption opt;
431 char* start;
432 char* end;
433
434 memset(&opt, 0, sizeof(opt));
435 start = extraOptsBuf;
436 while (*start != '\0') {
437 while (*start == ' ') /* skip leading whitespace */
438 start++;
439 if (*start == '\0') /* was trailing ws, bail */
440 break;
441
442 end = start+1;
443 while (*end != ' ' && *end != '\0') /* find end of token */
444 end++;
445 if (*end == ' ')
446 *end++ = '\0'; /* mark end, advance to indicate more */
447
448 opt.optionString = start;
449 mOptions.add(opt);
450 start = end;
451 }
452}
453
454/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700455 * Start the Dalvik Virtual Machine.
456 *
457 * Various arguments, most determined by system properties, are passed in.
458 * The "mOptions" vector is updated.
459 *
460 * Returns 0 on success.
461 */
462int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700464 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 JavaVMInitArgs initArgs;
466 JavaVMOption opt;
467 char propBuf[PROPERTY_VALUE_MAX];
468 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
469 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
470 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
471 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800472 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700473 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800474 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700475 char extraOptsBuf[PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700478 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700480 enum {
481 kEMDefault,
482 kEMIntPortable,
483 kEMIntFast,
484#if defined(WITH_JIT)
485 kEMJitCompiler,
486#endif
487 } executionMode = kEMDefault;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489
490 property_get("dalvik.vm.checkjni", propBuf, "");
491 if (strcmp(propBuf, "true") == 0) {
492 checkJni = true;
493 } else if (strcmp(propBuf, "false") != 0) {
494 /* property is neither true nor false; fall back on kernel parameter */
495 property_get("ro.kernel.android.checkjni", propBuf, "");
496 if (propBuf[0] == '1') {
497 checkJni = true;
498 }
499 }
500
501 property_get("dalvik.vm.execution-mode", propBuf, "");
502 if (strcmp(propBuf, "int:portable") == 0) {
503 executionMode = kEMIntPortable;
504 } else if (strcmp(propBuf, "int:fast") == 0) {
505 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700506#if defined(WITH_JIT)
507 } else if (strcmp(propBuf, "int:jit") == 0) {
508 executionMode = kEMJitCompiler;
509#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 }
511
512 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
513
Andy McFaddene2b23e12009-04-03 11:09:46 -0700514 property_get("dalvik.vm.check-dex-sum", propBuf, "");
515 if (strcmp(propBuf, "true") == 0) {
516 checkDexSum = true;
517 }
518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 property_get("log.redirect-stdio", propBuf, "");
520 if (strcmp(propBuf, "true") == 0) {
521 logStdio = true;
522 }
523
524 strcpy(enableAssertBuf, "-ea:");
525 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
526
527 strcpy(jniOptsBuf, "-Xjniopts:");
528 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 /* route exit() to our handler */
531 opt.extraInfo = (void*) runtime_exit;
532 opt.optionString = "exit";
533 mOptions.add(opt);
534
535 /* route fprintf() to our handler */
536 opt.extraInfo = (void*) runtime_vfprintf;
537 opt.optionString = "vfprintf";
538 mOptions.add(opt);
539
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800540 /* register the framework-specific "is sensitive thread" hook */
541 opt.extraInfo = (void*) runtime_isSensitiveThread;
542 opt.optionString = "sensitiveThread";
543 mOptions.add(opt);
544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 opt.extraInfo = NULL;
546
547 /* enable verbose; standard options are { jni, gc, class } */
548 //options[curOpt++].optionString = "-verbose:jni";
549 opt.optionString = "-verbose:gc";
550 mOptions.add(opt);
551 //options[curOpt++].optionString = "-verbose:class";
552
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800553 /*
554 * The default starting and maximum size of the heap. Larger
555 * values should be specified in a product property override.
556 */
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800557 strcpy(heapstartsizeOptsBuf, "-Xms");
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800558 property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800559 opt.optionString = heapstartsizeOptsBuf;
560 mOptions.add(opt);
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700561 strcpy(heapsizeOptsBuf, "-Xmx");
562 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700563 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 mOptions.add(opt);
565
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800566 strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
567 property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
568 if (heapgrowthlimitOptsBuf[20] != '\0') {
569 opt.optionString = heapgrowthlimitOptsBuf;
570 mOptions.add(opt);
571 }
572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 /*
574 * Enable or disable dexopt features, such as bytecode verification and
575 * calculation of register maps for precise GC.
576 */
577 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
578 if (dexoptFlagsBuf[0] != '\0') {
579 const char* opc;
580 const char* val;
581
582 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
583 if (opc != NULL) {
584 switch (*(opc+2)) {
585 case 'n': val = "-Xverify:none"; break;
586 case 'r': val = "-Xverify:remote"; break;
587 case 'a': val = "-Xverify:all"; break;
588 default: val = NULL; break;
589 }
590
591 if (val != NULL) {
592 opt.optionString = val;
593 mOptions.add(opt);
594 }
595 }
596
597 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
598 if (opc != NULL) {
599 switch (*(opc+2)) {
600 case 'n': val = "-Xdexopt:none"; break;
601 case 'v': val = "-Xdexopt:verified"; break;
602 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700603 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 default: val = NULL; break;
605 }
606
607 if (val != NULL) {
608 opt.optionString = val;
609 mOptions.add(opt);
610 }
611 }
612
613 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
614 if (opc != NULL) {
615 opt.optionString = "-Xgenregmap";
616 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700617
618 /* turn on precise GC while we're at it */
619 opt.optionString = "-Xgc:precise";
620 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622 }
623
624 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 /* use android ADB transport */
626 opt.optionString =
627 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 mOptions.add(opt);
629
Steve Block5baa3a62011-12-20 16:23:08 +0000630 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 if (checkJni) {
632 /* extended JNI checking */
633 opt.optionString = "-Xcheck:jni";
634 mOptions.add(opt);
635
636 /* set a cap on JNI global references */
637 opt.optionString = "-Xjnigreflimit:2000";
638 mOptions.add(opt);
639
640 /* with -Xcheck:jni, this provides a JNI function call trace */
641 //opt.optionString = "-verbose:jni";
642 //mOptions.add(opt);
643 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700644
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700645 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
646 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
647 if (strlen(propBuf) > 0) {
648 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
649 strcat(lockProfThresholdBuf, propBuf);
650 opt.optionString = lockProfThresholdBuf;
651 mOptions.add(opt);
652 }
653
Ben Cheng52b0e732009-06-19 13:31:12 -0700654#if defined(WITH_JIT)
Ben Cheng52b0e732009-06-19 13:31:12 -0700655 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
656 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
657 property_get("dalvik.vm.jit.op", propBuf, "");
658 if (strlen(propBuf) > 0) {
659 strcpy(jitOpBuf, "-Xjitop:");
660 strcat(jitOpBuf, propBuf);
661 opt.optionString = jitOpBuf;
662 mOptions.add(opt);
663 }
664
Ben Cheng52b0e732009-06-19 13:31:12 -0700665 /* Force interpreter-only mode for selected methods */
666 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
667 property_get("dalvik.vm.jit.method", propBuf, "");
668 if (strlen(propBuf) > 0) {
669 strcpy(jitMethodBuf, "-Xjitmethod:");
670 strcat(jitMethodBuf, propBuf);
671 opt.optionString = jitMethodBuf;
672 mOptions.add(opt);
673 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700674#endif
675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 if (executionMode == kEMIntPortable) {
677 opt.optionString = "-Xint:portable";
678 mOptions.add(opt);
679 } else if (executionMode == kEMIntFast) {
680 opt.optionString = "-Xint:fast";
681 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700682#if defined(WITH_JIT)
683 } else if (executionMode == kEMJitCompiler) {
684 opt.optionString = "-Xint:jit";
685 mOptions.add(opt);
686#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700688
689 if (checkDexSum) {
690 /* perform additional DEX checksum tests */
691 opt.optionString = "-Xcheckdexsum";
692 mOptions.add(opt);
693 }
694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 if (logStdio) {
696 /* convert stdout/stderr to log messages */
697 opt.optionString = "-Xlog-stdio";
698 mOptions.add(opt);
699 }
700
701 if (enableAssertBuf[4] != '\0') {
702 /* accept "all" to mean "all classes and packages" */
703 if (strcmp(enableAssertBuf+4, "all") == 0)
704 enableAssertBuf[3] = '\0';
Steve Block6215d3f2012-01-04 20:05:49 +0000705 ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 opt.optionString = enableAssertBuf;
707 mOptions.add(opt);
708 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100709 ALOGV("Assertions disabled\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 }
711
712 if (jniOptsBuf[10] != '\0') {
Steve Block6215d3f2012-01-04 20:05:49 +0000713 ALOGI("JNI options: '%s'\n", jniOptsBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 opt.optionString = jniOptsBuf;
715 mOptions.add(opt);
716 }
717
718 if (stackTraceFileBuf[0] != '\0') {
719 static const char* stfOptName = "-Xstacktracefile:";
720
721 stackTraceFile = (char*) malloc(strlen(stfOptName) +
722 strlen(stackTraceFileBuf) +1);
723 strcpy(stackTraceFile, stfOptName);
724 strcat(stackTraceFile, stackTraceFileBuf);
725 opt.optionString = stackTraceFile;
726 mOptions.add(opt);
727 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700728
729 /* extra options; parse this late so it overrides others */
730 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
731 parseExtraOpts(extraOptsBuf);
732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 /* Set the properties for locale */
734 {
735 char langOption[sizeof("-Duser.language=") + 3];
736 char regionOption[sizeof("-Duser.region=") + 3];
737 strcpy(langOption, "-Duser.language=");
738 strcpy(regionOption, "-Duser.region=");
739 readLocale(langOption, regionOption);
740 opt.extraInfo = NULL;
741 opt.optionString = langOption;
742 mOptions.add(opt);
743 opt.optionString = regionOption;
744 mOptions.add(opt);
745 }
746
747 /*
748 * We don't have /tmp on the device, but we often have an SD card. Apps
749 * shouldn't use this, but some test suites might want to exercise it.
750 */
751 opt.optionString = "-Djava.io.tmpdir=/sdcard";
752 mOptions.add(opt);
753
754 initArgs.version = JNI_VERSION_1_4;
755 initArgs.options = mOptions.editArray();
756 initArgs.nOptions = mOptions.size();
757 initArgs.ignoreUnrecognized = JNI_FALSE;
758
759 /*
760 * Initialize the VM.
761 *
762 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
763 * If this call succeeds, the VM is ready, and we can start issuing
764 * JNI calls.
765 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700766 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000767 ALOGE("JNI_CreateJavaVM failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 goto bail;
769 }
770
Andy McFaddenf70188a2009-03-31 15:52:13 -0700771 result = 0;
772
773bail:
774 free(stackTraceFile);
775 return result;
776}
777
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700778char* AndroidRuntime::toSlashClassName(const char* className)
779{
780 char* result = strdup(className);
781 for (char* cp = result; *cp != '\0'; cp++) {
782 if (*cp == '.') {
783 *cp = '/';
784 }
785 }
786 return result;
787}
788
Andy McFaddenf70188a2009-03-31 15:52:13 -0700789/*
790 * Start the Android runtime. This involves starting the virtual machine
791 * and calling the "static void main(String[] args)" method in the class
792 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700793 *
794 * Passes the main function two arguments, the class name and the specified
795 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700796 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700797void AndroidRuntime::start(const char* className, const char* options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700798{
Steve Block5baa3a62011-12-20 16:23:08 +0000799 ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700800 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700801
Andy McFaddenf70188a2009-03-31 15:52:13 -0700802 blockSigpipe();
803
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700804 /*
805 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700806 * init.rc anymore, so we print out the boot start event here.
807 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700808 if (strcmp(options, "start-system-server") == 0) {
Andy McFaddenf70188a2009-03-31 15:52:13 -0700809 /* track our progress through the boot sequence */
810 const int LOG_BOOT_PROGRESS_START = 3000;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700811 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
Andy McFaddenf70188a2009-03-31 15:52:13 -0700812 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
813 }
814
815 const char* rootDir = getenv("ANDROID_ROOT");
816 if (rootDir == NULL) {
817 rootDir = "/system";
818 if (!hasDir("/system")) {
819 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700820 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700821 }
822 setenv("ANDROID_ROOT", rootDir, 1);
823 }
824
825 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block5baa3a62011-12-20 16:23:08 +0000826 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700827
828 /* start the virtual machine */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700829 JNIEnv* env;
830 if (startVm(&mJavaVM, &env) != 0) {
831 return;
832 }
833 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 /*
836 * Register android functions.
837 */
838 if (startReg(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000839 ALOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700840 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842
843 /*
844 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700845 * At present we have two arguments, the class name and an option string.
846 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 */
848 jclass stringClass;
849 jobjectArray strArray;
850 jstring classNameStr;
Jeff Brownebed7d62011-05-16 17:08:42 -0700851 jstring optionsStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852
853 stringClass = env->FindClass("java/lang/String");
854 assert(stringClass != NULL);
855 strArray = env->NewObjectArray(2, stringClass, NULL);
856 assert(strArray != NULL);
857 classNameStr = env->NewStringUTF(className);
858 assert(classNameStr != NULL);
859 env->SetObjectArrayElement(strArray, 0, classNameStr);
Jeff Brownebed7d62011-05-16 17:08:42 -0700860 optionsStr = env->NewStringUTF(options);
861 env->SetObjectArrayElement(strArray, 1, optionsStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862
863 /*
864 * Start VM. This thread becomes the main thread of the VM, and will
865 * not return until the VM exits.
866 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700867 char* slashClassName = toSlashClassName(className);
868 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 if (startClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000870 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 /* keep going */
872 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700873 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 "([Ljava/lang/String;)V");
875 if (startMeth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000876 ALOGE("JavaVM unable to find main() in '%s'\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 /* keep going */
878 } else {
879 env->CallStaticVoidMethod(startClass, startMeth, strArray);
880
881#if 0
882 if (env->ExceptionCheck())
883 threadExitUncaughtException(env);
884#endif
885 }
886 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700887 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888
Steve Block5baa3a62011-12-20 16:23:08 +0000889 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 if (mJavaVM->DetachCurrentThread() != JNI_OK)
Steve Block8564c8d2012-01-05 23:22:43 +0000891 ALOGW("Warning: unable to detach main thread\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 if (mJavaVM->DestroyJavaVM() != 0)
Steve Block8564c8d2012-01-05 23:22:43 +0000893 ALOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894}
895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896void AndroidRuntime::onExit(int code)
897{
Steve Block71f2cf12011-10-20 11:56:00 +0100898 ALOGV("AndroidRuntime onExit calling exit(%d)", code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 exit(code);
900}
901
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700902void AndroidRuntime::onVmCreated(JNIEnv* env)
903{
904 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
905}
906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907/*
908 * Get the JNIEnv pointer for this thread.
909 *
910 * Returns NULL if the slot wasn't allocated or populated.
911 */
912/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
913{
914 JNIEnv* env;
915 JavaVM* vm = AndroidRuntime::getJavaVM();
916 assert(vm != NULL);
917
918 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
919 return NULL;
920 return env;
921}
922
923/*
924 * Makes the current thread visible to the VM.
925 *
926 * The JNIEnv pointer returned is only valid for the current thread, and
927 * thus must be tucked into thread-local storage.
928 */
929static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
930{
931 JavaVMAttachArgs args;
932 JavaVM* vm;
933 jint result;
934
935 vm = AndroidRuntime::getJavaVM();
936 assert(vm != NULL);
937
938 args.version = JNI_VERSION_1_4;
939 args.name = (char*) threadName;
940 args.group = NULL;
941
942 result = vm->AttachCurrentThread(pEnv, (void*) &args);
943 if (result != JNI_OK)
Steve Block6215d3f2012-01-04 20:05:49 +0000944 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945
946 return result;
947}
948
949/*
950 * Detach the current thread from the set visible to the VM.
951 */
952static int javaDetachThread(void)
953{
954 JavaVM* vm;
955 jint result;
956
957 vm = AndroidRuntime::getJavaVM();
958 assert(vm != NULL);
959
960 result = vm->DetachCurrentThread();
961 if (result != JNI_OK)
Steve Block3762c312012-01-06 19:20:56 +0000962 ALOGE("ERROR: thread detach failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 return result;
964}
965
966/*
967 * When starting a native thread that will be visible from the VM, we
968 * bounce through this to get the right attach/detach action.
969 * Note that this function calls free(args)
970 */
971/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
972 void* start = ((void**)args)[0];
973 void* userData = ((void **)args)[1];
974 char* name = (char*) ((void **)args)[2]; // we own this storage
975 free(args);
976 JNIEnv* env;
977 int result;
978
979 /* hook us into the VM */
980 if (javaAttachThread(name, &env) != JNI_OK)
981 return -1;
982
983 /* start the thread running */
984 result = (*(android_thread_func_t)start)(userData);
985
986 /* unhook us */
987 javaDetachThread();
988 free(name);
989
990 return result;
991}
992
993/*
994 * This is invoked from androidCreateThreadEtc() via the callback
995 * set with androidSetCreateThreadFunc().
996 *
997 * We need to create the new thread in such a way that it gets hooked
998 * into the VM before it really starts executing.
999 */
1000/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001001 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 void* userData,
1003 const char* threadName,
1004 int32_t threadPriority,
1005 size_t threadStackSize,
1006 android_thread_id_t* threadId)
1007{
1008 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1009 int result;
1010
1011 assert(threadName != NULL);
1012
1013 args[0] = (void*) entryFunction;
1014 args[1] = userData;
1015 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1016
1017 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1018 threadName, threadPriority, threadStackSize, threadId);
1019 return result;
1020}
1021
1022/*
1023 * Create a thread that is visible from the VM.
1024 *
1025 * This is called from elsewhere in the library.
1026 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001027/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 void (*start)(void *), void* arg)
1029{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001030 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001032 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1033 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034}
1035
1036#if 0
1037static void quickTest(void* arg)
1038{
1039 const char* str = (const char*) arg;
1040
1041 printf("In quickTest: %s\n", str);
1042}
1043#endif
1044
1045#ifdef NDEBUG
1046 #define REG_JNI(name) { name }
1047 struct RegJNIRec {
1048 int (*mProc)(JNIEnv*);
1049 };
1050#else
1051 #define REG_JNI(name) { name, #name }
1052 struct RegJNIRec {
1053 int (*mProc)(JNIEnv*);
1054 const char* mName;
1055 };
1056#endif
1057
1058typedef void (*RegJAMProc)();
1059
1060static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1061{
1062 for (size_t i = 0; i < count; i++) {
1063 if (array[i].mProc(env) < 0) {
1064#ifndef NDEBUG
Steve Block5baa3a62011-12-20 16:23:08 +00001065 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066#endif
1067 return -1;
1068 }
1069 }
1070 return 0;
1071}
1072
1073static void register_jam_procs(const RegJAMProc array[], size_t count)
1074{
1075 for (size_t i = 0; i < count; i++) {
1076 array[i]();
1077 }
1078}
1079
1080static const RegJNIRec gRegJNI[] = {
1081 REG_JNI(register_android_debug_JNITest),
1082 REG_JNI(register_com_android_internal_os_RuntimeInit),
1083 REG_JNI(register_android_os_SystemClock),
1084 REG_JNI(register_android_util_EventLog),
1085 REG_JNI(register_android_util_Log),
1086 REG_JNI(register_android_util_FloatMath),
1087 REG_JNI(register_android_text_format_Time),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 REG_JNI(register_android_content_AssetManager),
1089 REG_JNI(register_android_content_StringBlock),
1090 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001091 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001093 REG_JNI(register_android_text_AndroidBidi),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 REG_JNI(register_android_text_KeyCharacterMap),
1095 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001096 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 REG_JNI(register_android_os_Binder),
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001098 REG_JNI(register_android_os_Parcel),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 REG_JNI(register_android_view_Display),
Jeff Brown0a0a1242011-12-02 02:25:22 -08001100 REG_JNI(register_android_view_DisplayEventReceiver),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 REG_JNI(register_android_nio_utils),
1102 REG_JNI(register_android_graphics_PixelFormat),
1103 REG_JNI(register_android_graphics_Graphics),
Romain Guye4d01122010-06-16 18:44:05 -07001104 REG_JNI(register_android_view_GLES20Canvas),
Romain Guya9582652011-11-10 14:20:10 -08001105 REG_JNI(register_android_view_HardwareRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 REG_JNI(register_android_view_Surface),
Romain Guy8f0095c2011-05-02 17:24:22 -07001107 REG_JNI(register_android_view_TextureView),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1109 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001110 REG_JNI(register_android_opengl_jni_GLES10),
1111 REG_JNI(register_android_opengl_jni_GLES10Ext),
1112 REG_JNI(register_android_opengl_jni_GLES11),
1113 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001114 REG_JNI(register_android_opengl_jni_GLES20),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115
1116 REG_JNI(register_android_graphics_Bitmap),
1117 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001118 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 REG_JNI(register_android_graphics_Camera),
1120 REG_JNI(register_android_graphics_Canvas),
1121 REG_JNI(register_android_graphics_ColorFilter),
1122 REG_JNI(register_android_graphics_DrawFilter),
1123 REG_JNI(register_android_graphics_Interpolator),
1124 REG_JNI(register_android_graphics_LayerRasterizer),
1125 REG_JNI(register_android_graphics_MaskFilter),
1126 REG_JNI(register_android_graphics_Matrix),
1127 REG_JNI(register_android_graphics_Movie),
1128 REG_JNI(register_android_graphics_NinePatch),
1129 REG_JNI(register_android_graphics_Paint),
1130 REG_JNI(register_android_graphics_Path),
1131 REG_JNI(register_android_graphics_PathMeasure),
1132 REG_JNI(register_android_graphics_PathEffect),
1133 REG_JNI(register_android_graphics_Picture),
1134 REG_JNI(register_android_graphics_PorterDuff),
1135 REG_JNI(register_android_graphics_Rasterizer),
1136 REG_JNI(register_android_graphics_Region),
1137 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001138 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 REG_JNI(register_android_graphics_Typeface),
1140 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001141 REG_JNI(register_android_graphics_YuvImage),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142
1143 REG_JNI(register_android_database_CursorWindow),
Jeff Browne5360fb2011-10-31 17:48:13 -07001144 REG_JNI(register_android_database_SQLiteConnection),
1145 REG_JNI(register_android_database_SQLiteGlobal),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 REG_JNI(register_android_database_SQLiteDebug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 REG_JNI(register_android_os_FileObserver),
1149 REG_JNI(register_android_os_FileUtils),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001150 REG_JNI(register_android_os_MessageQueue),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 REG_JNI(register_android_os_ParcelFileDescriptor),
1152 REG_JNI(register_android_os_Power),
1153 REG_JNI(register_android_os_StatFs),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 REG_JNI(register_android_os_UEventObserver),
1155 REG_JNI(register_android_net_LocalSocketImpl),
1156 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001157 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 REG_JNI(register_android_net_wifi_WifiManager),
1159 REG_JNI(register_android_os_MemoryFile),
1160 REG_JNI(register_com_android_internal_os_ZygoteInit),
1161 REG_JNI(register_android_hardware_Camera),
1162 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -04001163 REG_JNI(register_android_hardware_SerialPort),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001164 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001165 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001166 REG_JNI(register_android_hardware_UsbRequest),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 REG_JNI(register_android_media_AudioRecord),
1168 REG_JNI(register_android_media_AudioSystem),
1169 REG_JNI(register_android_media_AudioTrack),
1170 REG_JNI(register_android_media_JetPlayer),
1171 REG_JNI(register_android_media_ToneGenerator),
1172
1173 REG_JNI(register_android_opengl_classes),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 REG_JNI(register_android_bluetooth_HeadsetBase),
1175 REG_JNI(register_android_bluetooth_BluetoothAudioGateway),
Nick Pelly0b6955a2009-05-26 19:13:43 -07001176 REG_JNI(register_android_bluetooth_BluetoothSocket),
Nick Pellybd022f42009-08-14 18:33:38 -07001177 REG_JNI(register_android_server_BluetoothService),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 REG_JNI(register_android_server_BluetoothEventLoop),
1179 REG_JNI(register_android_server_BluetoothA2dpService),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001180 REG_JNI(register_android_server_NetworkManagementSocketTagger),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001181 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001183 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001184 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001185 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001186 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001187 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001188 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001189 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001190 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001191 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001192 REG_JNI(register_android_view_KeyEvent),
1193 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001194 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001195 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001196
1197 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001198 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001199
1200 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001201 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202};
1203
1204/*
1205 * Register android native functions with the VM.
1206 */
1207/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1208{
1209 /*
1210 * This hook causes all future threads created in this process to be
1211 * attached to the JavaVM. (This needs to go away in favor of JNI
1212 * Attach calls.)
1213 */
1214 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1215
Steve Block71f2cf12011-10-20 11:56:00 +01001216 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217
1218 /*
1219 * Every "register" function calls one or more things that return
1220 * a local reference (e.g. FindClass). Because we haven't really
1221 * started the VM yet, they're all getting stored in the base frame
1222 * and never released. Use Push/Pop to manage the storage.
1223 */
1224 env->PushLocalFrame(200);
1225
1226 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1227 env->PopLocalFrame(NULL);
1228 return -1;
1229 }
1230 env->PopLocalFrame(NULL);
1231
1232 //createJavaThread("fubar", quickTest, (void*) "hello");
1233
1234 return 0;
1235}
1236
1237AndroidRuntime* AndroidRuntime::getRuntime()
1238{
1239 return gCurRuntime;
1240}
1241
1242/**
1243 * Used by WithFramework to register native functions.
1244 */
1245extern "C"
1246jint Java_com_android_internal_util_WithFramework_registerNatives(
1247 JNIEnv* env, jclass clazz) {
1248 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1249}
1250
1251/**
1252 * Used by LoadClass to register native functions.
1253 */
1254extern "C"
1255jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1256 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1257}
1258
1259} // namespace android