blob: 1c0d7cf280f25ed1b79f4b838cd6b3e6919b521d [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 Lockwoode7d511e2010-12-30 13:39:37 -050079extern int register_android_hardware_UsbDevice(JNIEnv *env);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050080extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050081extern int register_android_hardware_UsbRequest(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83extern int register_android_media_AudioRecord(JNIEnv *env);
84extern int register_android_media_AudioSystem(JNIEnv *env);
85extern int register_android_media_AudioTrack(JNIEnv *env);
86extern int register_android_media_JetPlayer(JNIEnv *env);
87extern int register_android_media_ToneGenerator(JNIEnv *env);
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089extern int register_android_util_FloatMath(JNIEnv* env);
90
91namespace android {
92
93/*
94 * JNI-based registration functions. Note these are properly contained in
95 * namespace android.
96 */
97extern int register_android_content_AssetManager(JNIEnv* env);
98extern int register_android_util_EventLog(JNIEnv* env);
99extern int register_android_util_Log(JNIEnv* env);
100extern int register_android_content_StringBlock(JNIEnv* env);
101extern int register_android_content_XmlBlock(JNIEnv* env);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700102extern int register_android_emoji_EmojiFactory(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103extern int register_android_graphics_Canvas(JNIEnv* env);
104extern int register_android_graphics_ColorFilter(JNIEnv* env);
105extern int register_android_graphics_DrawFilter(JNIEnv* env);
106extern int register_android_graphics_Matrix(JNIEnv* env);
107extern int register_android_graphics_Paint(JNIEnv* env);
108extern int register_android_graphics_Path(JNIEnv* env);
109extern int register_android_graphics_PathMeasure(JNIEnv* env);
110extern int register_android_graphics_Picture(JNIEnv*);
111extern int register_android_graphics_PorterDuff(JNIEnv* env);
112extern int register_android_graphics_Rasterizer(JNIEnv* env);
Jeff Brownfbf09772011-01-16 14:06:57 -0800113extern int register_android_graphics_Region(JNIEnv* env);
Jamie Gennisaa0ce332011-01-06 17:04:26 -0800114extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115extern int register_android_graphics_Xfermode(JNIEnv* env);
116extern int register_android_graphics_PixelFormat(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117extern int register_android_view_Display(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700118extern int register_android_view_GLES20Canvas(JNIEnv* env);
Romain Guya9582652011-11-10 14:20:10 -0800119extern int register_android_view_HardwareRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120extern int register_android_view_Surface(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700121extern int register_android_view_TextureView(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122extern int register_android_database_CursorWindow(JNIEnv* env);
Vasu Nori5a03f362009-10-20 15:16:35 -0700123extern int register_android_database_SQLiteCompiledSql(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124extern int register_android_database_SQLiteDatabase(JNIEnv* env);
125extern int register_android_database_SQLiteDebug(JNIEnv* env);
126extern int register_android_database_SQLiteProgram(JNIEnv* env);
127extern int register_android_database_SQLiteQuery(JNIEnv* env);
128extern int register_android_database_SQLiteStatement(JNIEnv* env);
129extern int register_android_debug_JNITest(JNIEnv* env);
130extern int register_android_nio_utils(JNIEnv* env);
Nick Pellycd0e8392010-10-13 17:25:24 -0700131extern int register_android_nfc_NdefMessage(JNIEnv *env);
132extern int register_android_nfc_NdefRecord(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133extern int register_android_text_format_Time(JNIEnv* env);
134extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700135extern int register_android_os_MessageQueue(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136extern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
137extern int register_android_os_Power(JNIEnv *env);
138extern int register_android_os_StatFs(JNIEnv *env);
139extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140extern int register_android_os_SystemClock(JNIEnv* env);
141extern int register_android_os_FileObserver(JNIEnv *env);
142extern int register_android_os_FileUtils(JNIEnv *env);
143extern int register_android_os_UEventObserver(JNIEnv* env);
144extern int register_android_os_MemoryFile(JNIEnv* env);
145extern int register_android_net_LocalSocketImpl(JNIEnv* env);
146extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700147extern int register_android_net_TrafficStats(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148extern int register_android_net_wifi_WifiManager(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149extern int register_android_text_AndroidCharacter(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800150extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151extern int register_android_text_KeyCharacterMap(JNIEnv *env);
152extern int register_android_opengl_classes(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153extern int register_android_bluetooth_HeadsetBase(JNIEnv* env);
154extern int register_android_bluetooth_BluetoothAudioGateway(JNIEnv* env);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700155extern int register_android_bluetooth_BluetoothSocket(JNIEnv *env);
Nick Pellybd022f42009-08-14 18:33:38 -0700156extern int register_android_server_BluetoothService(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157extern int register_android_server_BluetoothEventLoop(JNIEnv *env);
158extern int register_android_server_BluetoothA2dpService(JNIEnv* env);
JP Abgrall98a4f7e2011-09-02 15:36:33 -0700159extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
Christopher Tateecaa7b42010-06-04 14:55:02 -0700160extern int register_android_server_Watchdog(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
162extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700163extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700164extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700165extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400166extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Christopher Tate4a627c72011-04-01 14:43:32 -0700167extern int register_android_app_backup_FullBackup(JNIEnv *env);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700168extern int register_android_app_ActivityThread(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700169extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700170extern int register_android_view_InputChannel(JNIEnv* env);
171extern int register_android_view_InputQueue(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 */
192static void com_android_internal_os_RuntimeInit_finishInit(JNIEnv* env, jobject clazz)
193{
194 gCurRuntime->onStarted();
195}
196
197static void com_android_internal_os_RuntimeInit_zygoteInit(JNIEnv* env, jobject clazz)
198{
199 gCurRuntime->onZygoteInit();
200}
201
202static jint com_android_internal_os_RuntimeInit_isComputerOn(JNIEnv* env, jobject clazz)
203{
204 return 1;
205}
206
207static void com_android_internal_os_RuntimeInit_turnComputerOn(JNIEnv* env, jobject clazz)
208{
209}
210
211static jint com_android_internal_os_RuntimeInit_getQwertyKeyboard(JNIEnv* env, jobject clazz)
212{
213 char* value = getenv("qwerty");
214 if (value != NULL && strcmp(value, "true") == 0) {
215 return 1;
216 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 return 0;
219}
220
221/*
222 * JNI registration.
223 */
224static JNINativeMethod gMethods[] = {
225 { "finishInit", "()V",
226 (void*) com_android_internal_os_RuntimeInit_finishInit },
227 { "zygoteInitNative", "()V",
228 (void*) com_android_internal_os_RuntimeInit_zygoteInit },
229 { "isComputerOn", "()I",
230 (void*) com_android_internal_os_RuntimeInit_isComputerOn },
231 { "turnComputerOn", "()V",
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700232 (void*) com_android_internal_os_RuntimeInit_turnComputerOn },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 { "getQwertyKeyboard", "()I",
234 (void*) com_android_internal_os_RuntimeInit_getQwertyKeyboard },
235};
236
237int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
238{
239 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
240 gMethods, NELEM(gMethods));
241}
242
243// ----------------------------------------------------------------------
244
245/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
246
247
248AndroidRuntime::AndroidRuntime()
249{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700250 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 // this sets our preference for 16bit images during decode
252 // in case the src is opaque and 24bit
253 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400254 // This cache is shared between browser native images, and java "purgeable"
255 // bitmaps. This globalpool is for images that do not either use the java
256 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
257 // java call site.
258 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
259 // There is also a global font cache, but its budget is specified in code
260 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
262 // Pre-allocate enough space to hold a fair number of options.
263 mOptions.setCapacity(20);
264
265 assert(gCurRuntime == NULL); // one per process
266 gCurRuntime = this;
267}
268
269AndroidRuntime::~AndroidRuntime()
270{
271 SkGraphics::Term();
272}
273
274/*
275 * Register native methods using JNI.
276 */
277/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
278 const char* className, const JNINativeMethod* gMethods, int numMethods)
279{
280 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
281}
282
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700283status_t AndroidRuntime::callMain(const char* className,
284 jclass clazz, int argc, const char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285{
286 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 jmethodID methodId;
288
Steve Block1afd5ba2011-12-20 16:23:08 +0000289 ALOGD("Calling main entry %s", className);
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700292 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 return UNKNOWN_ERROR;
294 }
295
296 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
297 if (methodId == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000298 ALOGE("ERROR: could not find method %s.main(String[])\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 return UNKNOWN_ERROR;
300 }
301
302 /*
303 * We want to call main() with a String array with our arguments in it.
304 * Create an array and populate it.
305 */
306 jclass stringClass;
307 jobjectArray strArray;
308
309 stringClass = env->FindClass("java/lang/String");
310 strArray = env->NewObjectArray(argc, stringClass, NULL);
311
312 for (int i = 0; i < argc; i++) {
313 jstring argStr = env->NewStringUTF(argv[i]);
314 env->SetObjectArrayElement(strArray, i, argStr);
315 }
316
317 env->CallStaticVoidMethod(clazz, methodId, strArray);
318 return NO_ERROR;
319}
320
321/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 * The VM calls this through the "exit" hook.
323 */
324static void runtime_exit(int code)
325{
326 gCurRuntime->onExit(code);
327 exit(code);
328}
329
330/*
331 * The VM calls this through the "vfprintf" hook.
332 *
333 * We ignore "fp" and just write the results to the log file.
334 */
335static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
336{
337 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
338}
339
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800340/**
341 * The VM calls this when mutex contention debugging is enabled to
342 * determine whether or not the blocked thread was a "sensitive thread"
343 * for user responsiveness/smoothess.
344 *
345 * Our policy for this is whether or not we're tracing any StrictMode
346 * events on this thread (which we might've inherited via Binder calls
347 * into us)
348 */
349static bool runtime_isSensitiveThread() {
350 IPCThreadState* state = IPCThreadState::selfOrNull();
351 return state && state->getStrictModePolicy() != 0;
352}
353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354
355/**
356 * Add VM arguments to the to-be-executed VM
357 * Stops at first non '-' argument (also stops at an argument of '--')
358 * Returns the number of args consumed
359 */
360int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
361{
362 int i;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 for (i = 0; i<argc; i++) {
365 if (argv[i][0] != '-') {
366 return i;
367 }
368 if (argv[i][1] == '-' && argv[i][2] == 0) {
369 return i+1;
370 }
371
372 JavaVMOption opt;
373 memset(&opt, 0, sizeof(opt));
374 opt.optionString = (char*)argv[i];
375 mOptions.add(opt);
376 }
377 return i;
378}
379
380static int hasDir(const char* dir)
381{
382 struct stat s;
383 int res = stat(dir, &s);
384 if (res == 0) {
385 return S_ISDIR(s.st_mode);
386 }
387 return 0;
388}
389
390/*
391 * We just want failed write() calls to just return with an error.
392 */
393static void blockSigpipe()
394{
395 sigset_t mask;
396
397 sigemptyset(&mask);
398 sigaddset(&mask, SIGPIPE);
399 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
Steve Blocka51f0e72012-01-05 23:22:43 +0000400 ALOGW("WARNING: SIGPIPE not blocked\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401}
402
403/*
404 * Read the persistent locale.
405 */
406static void readLocale(char* language, char* region)
407{
408 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 property_get("persist.sys.language", propLang, "");
411 property_get("persist.sys.country", propRegn, "");
412 if (*propLang == 0 && *propRegn == 0) {
413 /* Set to ro properties, default is en_US */
414 property_get("ro.product.locale.language", propLang, "en");
415 property_get("ro.product.locale.region", propRegn, "US");
416 }
417 strncat(language, propLang, 2);
418 strncat(region, propRegn, 2);
Steve Block1afd5ba2011-12-20 16:23:08 +0000419 //ALOGD("language=%s region=%s\n", language, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420}
421
Andy McFaddenf70188a2009-03-31 15:52:13 -0700422/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700423 * Parse a property containing space-separated options that should be
424 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
425 *
426 * This will cut up "extraOptsBuf" as we chop it into individual options.
427 *
428 * Adds the strings, if any, to mOptions.
429 */
430void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
431{
432 JavaVMOption opt;
433 char* start;
434 char* end;
435
436 memset(&opt, 0, sizeof(opt));
437 start = extraOptsBuf;
438 while (*start != '\0') {
439 while (*start == ' ') /* skip leading whitespace */
440 start++;
441 if (*start == '\0') /* was trailing ws, bail */
442 break;
443
444 end = start+1;
445 while (*end != ' ' && *end != '\0') /* find end of token */
446 end++;
447 if (*end == ' ')
448 *end++ = '\0'; /* mark end, advance to indicate more */
449
450 opt.optionString = start;
451 mOptions.add(opt);
452 start = end;
453 }
454}
455
456/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700457 * Start the Dalvik Virtual Machine.
458 *
459 * Various arguments, most determined by system properties, are passed in.
460 * The "mOptions" vector is updated.
461 *
462 * Returns 0 on success.
463 */
464int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700466 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 JavaVMInitArgs initArgs;
468 JavaVMOption opt;
469 char propBuf[PROPERTY_VALUE_MAX];
470 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
471 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
472 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
473 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800474 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700475 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c32011-01-19 12:33:12 -0800476 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700477 char extraOptsBuf[PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700480 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700482 enum {
483 kEMDefault,
484 kEMIntPortable,
485 kEMIntFast,
486#if defined(WITH_JIT)
487 kEMJitCompiler,
488#endif
489 } executionMode = kEMDefault;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491
492 property_get("dalvik.vm.checkjni", propBuf, "");
493 if (strcmp(propBuf, "true") == 0) {
494 checkJni = true;
495 } else if (strcmp(propBuf, "false") != 0) {
496 /* property is neither true nor false; fall back on kernel parameter */
497 property_get("ro.kernel.android.checkjni", propBuf, "");
498 if (propBuf[0] == '1') {
499 checkJni = true;
500 }
501 }
502
503 property_get("dalvik.vm.execution-mode", propBuf, "");
504 if (strcmp(propBuf, "int:portable") == 0) {
505 executionMode = kEMIntPortable;
506 } else if (strcmp(propBuf, "int:fast") == 0) {
507 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700508#if defined(WITH_JIT)
509 } else if (strcmp(propBuf, "int:jit") == 0) {
510 executionMode = kEMJitCompiler;
511#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 }
513
514 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
515
Andy McFaddene2b23e12009-04-03 11:09:46 -0700516 property_get("dalvik.vm.check-dex-sum", propBuf, "");
517 if (strcmp(propBuf, "true") == 0) {
518 checkDexSum = true;
519 }
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 property_get("log.redirect-stdio", propBuf, "");
522 if (strcmp(propBuf, "true") == 0) {
523 logStdio = true;
524 }
525
526 strcpy(enableAssertBuf, "-ea:");
527 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
528
529 strcpy(jniOptsBuf, "-Xjniopts:");
530 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 /* route exit() to our handler */
533 opt.extraInfo = (void*) runtime_exit;
534 opt.optionString = "exit";
535 mOptions.add(opt);
536
537 /* route fprintf() to our handler */
538 opt.extraInfo = (void*) runtime_vfprintf;
539 opt.optionString = "vfprintf";
540 mOptions.add(opt);
541
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800542 /* register the framework-specific "is sensitive thread" hook */
543 opt.extraInfo = (void*) runtime_isSensitiveThread;
544 opt.optionString = "sensitiveThread";
545 mOptions.add(opt);
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 opt.extraInfo = NULL;
548
549 /* enable verbose; standard options are { jni, gc, class } */
550 //options[curOpt++].optionString = "-verbose:jni";
551 opt.optionString = "-verbose:gc";
552 mOptions.add(opt);
553 //options[curOpt++].optionString = "-verbose:class";
554
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800555 /*
556 * The default starting and maximum size of the heap. Larger
557 * values should be specified in a product property override.
558 */
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800559 strcpy(heapstartsizeOptsBuf, "-Xms");
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800560 property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800561 opt.optionString = heapstartsizeOptsBuf;
562 mOptions.add(opt);
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700563 strcpy(heapsizeOptsBuf, "-Xmx");
564 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700565 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 mOptions.add(opt);
567
Carl Shapiro8cdf27c32011-01-19 12:33:12 -0800568 strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
569 property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
570 if (heapgrowthlimitOptsBuf[20] != '\0') {
571 opt.optionString = heapgrowthlimitOptsBuf;
572 mOptions.add(opt);
573 }
574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 /*
576 * Enable or disable dexopt features, such as bytecode verification and
577 * calculation of register maps for precise GC.
578 */
579 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
580 if (dexoptFlagsBuf[0] != '\0') {
581 const char* opc;
582 const char* val;
583
584 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
585 if (opc != NULL) {
586 switch (*(opc+2)) {
587 case 'n': val = "-Xverify:none"; break;
588 case 'r': val = "-Xverify:remote"; break;
589 case 'a': val = "-Xverify:all"; break;
590 default: val = NULL; break;
591 }
592
593 if (val != NULL) {
594 opt.optionString = val;
595 mOptions.add(opt);
596 }
597 }
598
599 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
600 if (opc != NULL) {
601 switch (*(opc+2)) {
602 case 'n': val = "-Xdexopt:none"; break;
603 case 'v': val = "-Xdexopt:verified"; break;
604 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700605 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 default: val = NULL; break;
607 }
608
609 if (val != NULL) {
610 opt.optionString = val;
611 mOptions.add(opt);
612 }
613 }
614
615 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
616 if (opc != NULL) {
617 opt.optionString = "-Xgenregmap";
618 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700619
620 /* turn on precise GC while we're at it */
621 opt.optionString = "-Xgc:precise";
622 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
624 }
625
626 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 /* use android ADB transport */
628 opt.optionString =
629 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 mOptions.add(opt);
631
Steve Block1afd5ba2011-12-20 16:23:08 +0000632 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 if (checkJni) {
634 /* extended JNI checking */
635 opt.optionString = "-Xcheck:jni";
636 mOptions.add(opt);
637
638 /* set a cap on JNI global references */
639 opt.optionString = "-Xjnigreflimit:2000";
640 mOptions.add(opt);
641
642 /* with -Xcheck:jni, this provides a JNI function call trace */
643 //opt.optionString = "-verbose:jni";
644 //mOptions.add(opt);
645 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700646
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700647 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
648 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
649 if (strlen(propBuf) > 0) {
650 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
651 strcat(lockProfThresholdBuf, propBuf);
652 opt.optionString = lockProfThresholdBuf;
653 mOptions.add(opt);
654 }
655
Ben Cheng52b0e732009-06-19 13:31:12 -0700656#if defined(WITH_JIT)
Ben Cheng52b0e732009-06-19 13:31:12 -0700657 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
658 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
659 property_get("dalvik.vm.jit.op", propBuf, "");
660 if (strlen(propBuf) > 0) {
661 strcpy(jitOpBuf, "-Xjitop:");
662 strcat(jitOpBuf, propBuf);
663 opt.optionString = jitOpBuf;
664 mOptions.add(opt);
665 }
666
Ben Cheng52b0e732009-06-19 13:31:12 -0700667 /* Force interpreter-only mode for selected methods */
668 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
669 property_get("dalvik.vm.jit.method", propBuf, "");
670 if (strlen(propBuf) > 0) {
671 strcpy(jitMethodBuf, "-Xjitmethod:");
672 strcat(jitMethodBuf, propBuf);
673 opt.optionString = jitMethodBuf;
674 mOptions.add(opt);
675 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700676#endif
677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 if (executionMode == kEMIntPortable) {
679 opt.optionString = "-Xint:portable";
680 mOptions.add(opt);
681 } else if (executionMode == kEMIntFast) {
682 opt.optionString = "-Xint:fast";
683 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700684#if defined(WITH_JIT)
685 } else if (executionMode == kEMJitCompiler) {
686 opt.optionString = "-Xint:jit";
687 mOptions.add(opt);
688#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700690
691 if (checkDexSum) {
692 /* perform additional DEX checksum tests */
693 opt.optionString = "-Xcheckdexsum";
694 mOptions.add(opt);
695 }
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 if (logStdio) {
698 /* convert stdout/stderr to log messages */
699 opt.optionString = "-Xlog-stdio";
700 mOptions.add(opt);
701 }
702
703 if (enableAssertBuf[4] != '\0') {
704 /* accept "all" to mean "all classes and packages" */
705 if (strcmp(enableAssertBuf+4, "all") == 0)
706 enableAssertBuf[3] = '\0';
Steve Block933e8562012-01-04 20:05:49 +0000707 ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 opt.optionString = enableAssertBuf;
709 mOptions.add(opt);
710 } else {
Steve Block06ade6a2011-10-20 11:56:00 +0100711 ALOGV("Assertions disabled\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
713
714 if (jniOptsBuf[10] != '\0') {
Steve Block933e8562012-01-04 20:05:49 +0000715 ALOGI("JNI options: '%s'\n", jniOptsBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 opt.optionString = jniOptsBuf;
717 mOptions.add(opt);
718 }
719
720 if (stackTraceFileBuf[0] != '\0') {
721 static const char* stfOptName = "-Xstacktracefile:";
722
723 stackTraceFile = (char*) malloc(strlen(stfOptName) +
724 strlen(stackTraceFileBuf) +1);
725 strcpy(stackTraceFile, stfOptName);
726 strcat(stackTraceFile, stackTraceFileBuf);
727 opt.optionString = stackTraceFile;
728 mOptions.add(opt);
729 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700730
731 /* extra options; parse this late so it overrides others */
732 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
733 parseExtraOpts(extraOptsBuf);
734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 /* Set the properties for locale */
736 {
737 char langOption[sizeof("-Duser.language=") + 3];
738 char regionOption[sizeof("-Duser.region=") + 3];
739 strcpy(langOption, "-Duser.language=");
740 strcpy(regionOption, "-Duser.region=");
741 readLocale(langOption, regionOption);
742 opt.extraInfo = NULL;
743 opt.optionString = langOption;
744 mOptions.add(opt);
745 opt.optionString = regionOption;
746 mOptions.add(opt);
747 }
748
749 /*
750 * We don't have /tmp on the device, but we often have an SD card. Apps
751 * shouldn't use this, but some test suites might want to exercise it.
752 */
753 opt.optionString = "-Djava.io.tmpdir=/sdcard";
754 mOptions.add(opt);
755
756 initArgs.version = JNI_VERSION_1_4;
757 initArgs.options = mOptions.editArray();
758 initArgs.nOptions = mOptions.size();
759 initArgs.ignoreUnrecognized = JNI_FALSE;
760
761 /*
762 * Initialize the VM.
763 *
764 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
765 * If this call succeeds, the VM is ready, and we can start issuing
766 * JNI calls.
767 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700768 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000769 ALOGE("JNI_CreateJavaVM failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 goto bail;
771 }
772
Andy McFaddenf70188a2009-03-31 15:52:13 -0700773 result = 0;
774
775bail:
776 free(stackTraceFile);
777 return result;
778}
779
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700780char* AndroidRuntime::toSlashClassName(const char* className)
781{
782 char* result = strdup(className);
783 for (char* cp = result; *cp != '\0'; cp++) {
784 if (*cp == '.') {
785 *cp = '/';
786 }
787 }
788 return result;
789}
790
Andy McFaddenf70188a2009-03-31 15:52:13 -0700791/*
792 * Start the Android runtime. This involves starting the virtual machine
793 * and calling the "static void main(String[] args)" method in the class
794 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700795 *
796 * Passes the main function two arguments, the class name and the specified
797 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700798 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700799void AndroidRuntime::start(const char* className, const char* options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700800{
Steve Block1afd5ba2011-12-20 16:23:08 +0000801 ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700802 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700803
Andy McFaddenf70188a2009-03-31 15:52:13 -0700804 blockSigpipe();
805
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700806 /*
807 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700808 * init.rc anymore, so we print out the boot start event here.
809 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700810 if (strcmp(options, "start-system-server") == 0) {
Andy McFaddenf70188a2009-03-31 15:52:13 -0700811 /* track our progress through the boot sequence */
812 const int LOG_BOOT_PROGRESS_START = 3000;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700813 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
Andy McFaddenf70188a2009-03-31 15:52:13 -0700814 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
815 }
816
817 const char* rootDir = getenv("ANDROID_ROOT");
818 if (rootDir == NULL) {
819 rootDir = "/system";
820 if (!hasDir("/system")) {
821 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700822 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700823 }
824 setenv("ANDROID_ROOT", rootDir, 1);
825 }
826
827 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block1afd5ba2011-12-20 16:23:08 +0000828 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700829
830 /* start the virtual machine */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700831 JNIEnv* env;
832 if (startVm(&mJavaVM, &env) != 0) {
833 return;
834 }
835 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 /*
838 * Register android functions.
839 */
840 if (startReg(env) < 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000841 ALOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700842 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
844
845 /*
846 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700847 * At present we have two arguments, the class name and an option string.
848 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 */
850 jclass stringClass;
851 jobjectArray strArray;
852 jstring classNameStr;
Jeff Brownebed7d62011-05-16 17:08:42 -0700853 jstring optionsStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854
855 stringClass = env->FindClass("java/lang/String");
856 assert(stringClass != NULL);
857 strArray = env->NewObjectArray(2, stringClass, NULL);
858 assert(strArray != NULL);
859 classNameStr = env->NewStringUTF(className);
860 assert(classNameStr != NULL);
861 env->SetObjectArrayElement(strArray, 0, classNameStr);
Jeff Brownebed7d62011-05-16 17:08:42 -0700862 optionsStr = env->NewStringUTF(options);
863 env->SetObjectArrayElement(strArray, 1, optionsStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864
865 /*
866 * Start VM. This thread becomes the main thread of the VM, and will
867 * not return until the VM exits.
868 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700869 char* slashClassName = toSlashClassName(className);
870 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 if (startClass == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000872 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 /* keep going */
874 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700875 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 "([Ljava/lang/String;)V");
877 if (startMeth == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000878 ALOGE("JavaVM unable to find main() in '%s'\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 /* keep going */
880 } else {
881 env->CallStaticVoidMethod(startClass, startMeth, strArray);
882
883#if 0
884 if (env->ExceptionCheck())
885 threadExitUncaughtException(env);
886#endif
887 }
888 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700889 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890
Steve Block1afd5ba2011-12-20 16:23:08 +0000891 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 if (mJavaVM->DetachCurrentThread() != JNI_OK)
Steve Blocka51f0e72012-01-05 23:22:43 +0000893 ALOGW("Warning: unable to detach main thread\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 if (mJavaVM->DestroyJavaVM() != 0)
Steve Blocka51f0e72012-01-05 23:22:43 +0000895 ALOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896}
897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898void AndroidRuntime::onExit(int code)
899{
Steve Block06ade6a2011-10-20 11:56:00 +0100900 ALOGV("AndroidRuntime onExit calling exit(%d)", code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 exit(code);
902}
903
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700904void AndroidRuntime::onVmCreated(JNIEnv* env)
905{
906 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
907}
908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909/*
910 * Get the JNIEnv pointer for this thread.
911 *
912 * Returns NULL if the slot wasn't allocated or populated.
913 */
914/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
915{
916 JNIEnv* env;
917 JavaVM* vm = AndroidRuntime::getJavaVM();
918 assert(vm != NULL);
919
920 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
921 return NULL;
922 return env;
923}
924
925/*
926 * Makes the current thread visible to the VM.
927 *
928 * The JNIEnv pointer returned is only valid for the current thread, and
929 * thus must be tucked into thread-local storage.
930 */
931static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
932{
933 JavaVMAttachArgs args;
934 JavaVM* vm;
935 jint result;
936
937 vm = AndroidRuntime::getJavaVM();
938 assert(vm != NULL);
939
940 args.version = JNI_VERSION_1_4;
941 args.name = (char*) threadName;
942 args.group = NULL;
943
944 result = vm->AttachCurrentThread(pEnv, (void*) &args);
945 if (result != JNI_OK)
Steve Block933e8562012-01-04 20:05:49 +0000946 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947
948 return result;
949}
950
951/*
952 * Detach the current thread from the set visible to the VM.
953 */
954static int javaDetachThread(void)
955{
956 JavaVM* vm;
957 jint result;
958
959 vm = AndroidRuntime::getJavaVM();
960 assert(vm != NULL);
961
962 result = vm->DetachCurrentThread();
963 if (result != JNI_OK)
Steve Blockc6aacce2012-01-06 19:20:56 +0000964 ALOGE("ERROR: thread detach failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 return result;
966}
967
968/*
969 * When starting a native thread that will be visible from the VM, we
970 * bounce through this to get the right attach/detach action.
971 * Note that this function calls free(args)
972 */
973/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
974 void* start = ((void**)args)[0];
975 void* userData = ((void **)args)[1];
976 char* name = (char*) ((void **)args)[2]; // we own this storage
977 free(args);
978 JNIEnv* env;
979 int result;
980
981 /* hook us into the VM */
982 if (javaAttachThread(name, &env) != JNI_OK)
983 return -1;
984
985 /* start the thread running */
986 result = (*(android_thread_func_t)start)(userData);
987
988 /* unhook us */
989 javaDetachThread();
990 free(name);
991
992 return result;
993}
994
995/*
996 * This is invoked from androidCreateThreadEtc() via the callback
997 * set with androidSetCreateThreadFunc().
998 *
999 * We need to create the new thread in such a way that it gets hooked
1000 * into the VM before it really starts executing.
1001 */
1002/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001003 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 void* userData,
1005 const char* threadName,
1006 int32_t threadPriority,
1007 size_t threadStackSize,
1008 android_thread_id_t* threadId)
1009{
1010 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1011 int result;
1012
1013 assert(threadName != NULL);
1014
1015 args[0] = (void*) entryFunction;
1016 args[1] = userData;
1017 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1018
1019 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1020 threadName, threadPriority, threadStackSize, threadId);
1021 return result;
1022}
1023
1024/*
1025 * Create a thread that is visible from the VM.
1026 *
1027 * This is called from elsewhere in the library.
1028 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001029/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 void (*start)(void *), void* arg)
1031{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001032 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001034 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1035 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036}
1037
1038#if 0
1039static void quickTest(void* arg)
1040{
1041 const char* str = (const char*) arg;
1042
1043 printf("In quickTest: %s\n", str);
1044}
1045#endif
1046
1047#ifdef NDEBUG
1048 #define REG_JNI(name) { name }
1049 struct RegJNIRec {
1050 int (*mProc)(JNIEnv*);
1051 };
1052#else
1053 #define REG_JNI(name) { name, #name }
1054 struct RegJNIRec {
1055 int (*mProc)(JNIEnv*);
1056 const char* mName;
1057 };
1058#endif
1059
1060typedef void (*RegJAMProc)();
1061
1062static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1063{
1064 for (size_t i = 0; i < count; i++) {
1065 if (array[i].mProc(env) < 0) {
1066#ifndef NDEBUG
Steve Block1afd5ba2011-12-20 16:23:08 +00001067 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068#endif
1069 return -1;
1070 }
1071 }
1072 return 0;
1073}
1074
1075static void register_jam_procs(const RegJAMProc array[], size_t count)
1076{
1077 for (size_t i = 0; i < count; i++) {
1078 array[i]();
1079 }
1080}
1081
1082static const RegJNIRec gRegJNI[] = {
1083 REG_JNI(register_android_debug_JNITest),
1084 REG_JNI(register_com_android_internal_os_RuntimeInit),
1085 REG_JNI(register_android_os_SystemClock),
1086 REG_JNI(register_android_util_EventLog),
1087 REG_JNI(register_android_util_Log),
1088 REG_JNI(register_android_util_FloatMath),
1089 REG_JNI(register_android_text_format_Time),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 REG_JNI(register_android_content_AssetManager),
1091 REG_JNI(register_android_content_StringBlock),
1092 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001093 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001095 REG_JNI(register_android_text_AndroidBidi),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 REG_JNI(register_android_text_KeyCharacterMap),
1097 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001098 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 REG_JNI(register_android_os_Binder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 REG_JNI(register_android_view_Display),
1101 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),
Vasu Nori5a03f362009-10-20 15:16:35 -07001144 REG_JNI(register_android_database_SQLiteCompiledSql),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 REG_JNI(register_android_database_SQLiteDatabase),
1146 REG_JNI(register_android_database_SQLiteDebug),
1147 REG_JNI(register_android_database_SQLiteProgram),
1148 REG_JNI(register_android_database_SQLiteQuery),
1149 REG_JNI(register_android_database_SQLiteStatement),
1150 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 REG_JNI(register_android_os_FileObserver),
1152 REG_JNI(register_android_os_FileUtils),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001153 REG_JNI(register_android_os_MessageQueue),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 REG_JNI(register_android_os_ParcelFileDescriptor),
1155 REG_JNI(register_android_os_Power),
1156 REG_JNI(register_android_os_StatFs),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 REG_JNI(register_android_os_UEventObserver),
1158 REG_JNI(register_android_net_LocalSocketImpl),
1159 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001160 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 REG_JNI(register_android_net_wifi_WifiManager),
Nick Pellycd0e8392010-10-13 17:25:24 -07001162 REG_JNI(register_android_nfc_NdefMessage),
1163 REG_JNI(register_android_nfc_NdefRecord),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 REG_JNI(register_android_os_MemoryFile),
1165 REG_JNI(register_com_android_internal_os_ZygoteInit),
1166 REG_JNI(register_android_hardware_Camera),
1167 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001168 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001169 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001170 REG_JNI(register_android_hardware_UsbRequest),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 REG_JNI(register_android_media_AudioRecord),
1172 REG_JNI(register_android_media_AudioSystem),
1173 REG_JNI(register_android_media_AudioTrack),
1174 REG_JNI(register_android_media_JetPlayer),
1175 REG_JNI(register_android_media_ToneGenerator),
1176
1177 REG_JNI(register_android_opengl_classes),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 REG_JNI(register_android_bluetooth_HeadsetBase),
1179 REG_JNI(register_android_bluetooth_BluetoothAudioGateway),
Nick Pelly0b6955a2009-05-26 19:13:43 -07001180 REG_JNI(register_android_bluetooth_BluetoothSocket),
Nick Pellybd022f42009-08-14 18:33:38 -07001181 REG_JNI(register_android_server_BluetoothService),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 REG_JNI(register_android_server_BluetoothEventLoop),
1183 REG_JNI(register_android_server_BluetoothA2dpService),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001184 REG_JNI(register_android_server_NetworkManagementSocketTagger),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001185 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001187 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001188 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001189 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001190 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001191 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001192 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001193 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001194 REG_JNI(register_android_view_InputChannel),
1195 REG_JNI(register_android_view_InputQueue),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001196 REG_JNI(register_android_view_KeyEvent),
1197 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001198 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001199 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001200
1201 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001202 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001203
1204 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001205 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206};
1207
1208/*
1209 * Register android native functions with the VM.
1210 */
1211/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1212{
1213 /*
1214 * This hook causes all future threads created in this process to be
1215 * attached to the JavaVM. (This needs to go away in favor of JNI
1216 * Attach calls.)
1217 */
1218 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1219
Steve Block06ade6a2011-10-20 11:56:00 +01001220 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221
1222 /*
1223 * Every "register" function calls one or more things that return
1224 * a local reference (e.g. FindClass). Because we haven't really
1225 * started the VM yet, they're all getting stored in the base frame
1226 * and never released. Use Push/Pop to manage the storage.
1227 */
1228 env->PushLocalFrame(200);
1229
1230 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1231 env->PopLocalFrame(NULL);
1232 return -1;
1233 }
1234 env->PopLocalFrame(NULL);
1235
1236 //createJavaThread("fubar", quickTest, (void*) "hello");
1237
1238 return 0;
1239}
1240
1241AndroidRuntime* AndroidRuntime::getRuntime()
1242{
1243 return gCurRuntime;
1244}
1245
1246/**
1247 * Used by WithFramework to register native functions.
1248 */
1249extern "C"
1250jint Java_com_android_internal_util_WithFramework_registerNatives(
1251 JNIEnv* env, jclass clazz) {
1252 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1253}
1254
1255/**
1256 * Used by LoadClass to register native functions.
1257 */
1258extern "C"
1259jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1260 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1261}
1262
1263} // namespace android