blob: d33dccb8be6d728a054c98b4c1cdb11cd2654416 [file] [log] [blame]
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016
17#define LOG_TAG "AndroidRuntime"
18//#define LOG_NDEBUG 0
19
20#include <android_runtime/AndroidRuntime.h>
Mathias Agopian07952722009-05-19 19:08:10 -070021#include <binder/IBinder.h>
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -080022#include <binder/IPCThreadState.h>
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/Log.h>
25#include <utils/misc.h>
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/Parcel.h>
Mathias Agopian8ed6beb2009-06-05 01:26:23 -070027#include <utils/StringArray.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <utils/threads.h>
29#include <cutils/properties.h>
30
31#include <SkGraphics.h>
32#include <SkImageDecoder.h>
Mike Reedfc8db532009-04-27 11:43:30 -040033#include <SkImageRef_GlobalPool.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#include "jni.h"
36#include "JNIHelp.h"
37#include "android_util_Binder.h"
38
39#include <stdio.h>
40#include <signal.h>
41#include <sys/stat.h>
42#include <sys/types.h>
43#include <signal.h>
44#include <dirent.h>
45#include <assert.h>
46
47
48using namespace android;
49
50extern void register_BindTest();
51
52extern int register_android_os_Binder(JNIEnv* env);
53extern int register_android_os_Process(JNIEnv* env);
54extern int register_android_graphics_Bitmap(JNIEnv*);
55extern int register_android_graphics_BitmapFactory(JNIEnv*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080056extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057extern int register_android_graphics_Camera(JNIEnv* env);
58extern int register_android_graphics_Graphics(JNIEnv* env);
59extern int register_android_graphics_Interpolator(JNIEnv* env);
60extern int register_android_graphics_LayerRasterizer(JNIEnv*);
61extern int register_android_graphics_MaskFilter(JNIEnv* env);
62extern int register_android_graphics_Movie(JNIEnv* env);
63extern int register_android_graphics_NinePatch(JNIEnv*);
64extern int register_android_graphics_PathEffect(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065extern int register_android_graphics_Shader(JNIEnv* env);
66extern int register_android_graphics_Typeface(JNIEnv* env);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080067extern int register_android_graphics_YuvImage(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
70extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
Jack Palevich1c4907e2009-04-13 16:22:25 -070071extern int register_android_opengl_jni_GLES10(JNIEnv* env);
72extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
73extern int register_android_opengl_jni_GLES11(JNIEnv* env);
74extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
Jack Palevich560814f2009-11-19 16:34:55 +080075extern int register_android_opengl_jni_GLES20(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77extern int register_android_hardware_Camera(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078extern int register_android_hardware_SensorManager(JNIEnv *env);
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040079extern int register_android_hardware_SerialPort(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050080extern int register_android_hardware_UsbDevice(JNIEnv *env);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050081extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050082extern int register_android_hardware_UsbRequest(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84extern int register_android_media_AudioRecord(JNIEnv *env);
85extern int register_android_media_AudioSystem(JNIEnv *env);
86extern int register_android_media_AudioTrack(JNIEnv *env);
87extern int register_android_media_JetPlayer(JNIEnv *env);
88extern int register_android_media_ToneGenerator(JNIEnv *env);
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090extern int register_android_util_FloatMath(JNIEnv* env);
91
92namespace android {
93
94/*
95 * JNI-based registration functions. Note these are properly contained in
96 * namespace android.
97 */
98extern int register_android_content_AssetManager(JNIEnv* env);
99extern int register_android_util_EventLog(JNIEnv* env);
100extern int register_android_util_Log(JNIEnv* env);
101extern int register_android_content_StringBlock(JNIEnv* env);
102extern int register_android_content_XmlBlock(JNIEnv* env);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700103extern int register_android_emoji_EmojiFactory(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104extern int register_android_graphics_Canvas(JNIEnv* env);
105extern int register_android_graphics_ColorFilter(JNIEnv* env);
106extern int register_android_graphics_DrawFilter(JNIEnv* env);
107extern int register_android_graphics_Matrix(JNIEnv* env);
108extern int register_android_graphics_Paint(JNIEnv* env);
109extern int register_android_graphics_Path(JNIEnv* env);
110extern int register_android_graphics_PathMeasure(JNIEnv* env);
111extern int register_android_graphics_Picture(JNIEnv*);
112extern int register_android_graphics_PorterDuff(JNIEnv* env);
113extern int register_android_graphics_Rasterizer(JNIEnv* env);
Jeff Brownfbf09772011-01-16 14:06:57 -0800114extern int register_android_graphics_Region(JNIEnv* env);
Jamie Gennisaa0ce332011-01-06 17:04:26 -0800115extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116extern int register_android_graphics_Xfermode(JNIEnv* env);
117extern int register_android_graphics_PixelFormat(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118extern int register_android_view_Display(JNIEnv* env);
Jeff Brown0a0a1242011-12-02 02:25:22 -0800119extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
Chet Haasea1cff502012-02-21 13:43:44 -0800120extern int register_android_view_GLES20DisplayList(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700121extern int register_android_view_GLES20Canvas(JNIEnv* env);
Romain Guya9582652011-11-10 14:20:10 -0800122extern int register_android_view_HardwareRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123extern int register_android_view_Surface(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700124extern int register_android_view_TextureView(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125extern int register_android_database_CursorWindow(JNIEnv* env);
Jeff Browne5360fb2011-10-31 17:48:13 -0700126extern int register_android_database_SQLiteConnection(JNIEnv* env);
127extern int register_android_database_SQLiteGlobal(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128extern int register_android_database_SQLiteDebug(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129extern int register_android_debug_JNITest(JNIEnv* env);
130extern int register_android_nio_utils(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131extern int register_android_text_format_Time(JNIEnv* env);
132extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700133extern int register_android_os_MessageQueue(JNIEnv* env);
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800134extern int register_android_os_Parcel(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135extern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
136extern int register_android_os_Power(JNIEnv *env);
137extern int register_android_os_StatFs(JNIEnv *env);
138extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139extern int register_android_os_SystemClock(JNIEnv* env);
Jeff Brown481c1572012-03-09 14:41:15 -0800140extern int register_android_os_Trace(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141extern 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 Brown46b9ac02010-04-22 18:58:52 -0700170extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800171extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700172extern int register_android_view_KeyEvent(JNIEnv* env);
173extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700174extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700175extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700176extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700177extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700178extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700179extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
181static AndroidRuntime* gCurRuntime = NULL;
182
183static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
184{
185 if (jniThrowException(env, exc, msg) != 0)
186 assert(false);
187}
188
189/*
190 * Code written in the Java Programming Language calls here from main().
191 */
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700192static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193{
194 gCurRuntime->onStarted();
195}
196
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700197static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198{
199 gCurRuntime->onZygoteInit();
200}
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202/*
203 * JNI registration.
204 */
205static JNINativeMethod gMethods[] = {
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700206 { "nativeFinishInit", "()V",
207 (void*) com_android_internal_os_RuntimeInit_nativeFinishInit },
208 { "nativeZygoteInit", "()V",
209 (void*) com_android_internal_os_RuntimeInit_nativeZygoteInit },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210};
211
212int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
213{
214 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
215 gMethods, NELEM(gMethods));
216}
217
218// ----------------------------------------------------------------------
219
220/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
221
222
223AndroidRuntime::AndroidRuntime()
224{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700225 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 // this sets our preference for 16bit images during decode
227 // in case the src is opaque and 24bit
228 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400229 // This cache is shared between browser native images, and java "purgeable"
230 // bitmaps. This globalpool is for images that do not either use the java
231 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
232 // java call site.
233 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
234 // There is also a global font cache, but its budget is specified in code
235 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
237 // Pre-allocate enough space to hold a fair number of options.
238 mOptions.setCapacity(20);
239
240 assert(gCurRuntime == NULL); // one per process
241 gCurRuntime = this;
242}
243
244AndroidRuntime::~AndroidRuntime()
245{
246 SkGraphics::Term();
247}
248
249/*
250 * Register native methods using JNI.
251 */
252/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
253 const char* className, const JNINativeMethod* gMethods, int numMethods)
254{
255 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
256}
257
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700258status_t AndroidRuntime::callMain(const char* className,
259 jclass clazz, int argc, const char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260{
261 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 jmethodID methodId;
263
Steve Block5baa3a62011-12-20 16:23:08 +0000264 ALOGD("Calling main entry %s", className);
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700267 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 return UNKNOWN_ERROR;
269 }
270
271 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
272 if (methodId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000273 ALOGE("ERROR: could not find method %s.main(String[])\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 return UNKNOWN_ERROR;
275 }
276
277 /*
278 * We want to call main() with a String array with our arguments in it.
279 * Create an array and populate it.
280 */
281 jclass stringClass;
282 jobjectArray strArray;
283
284 stringClass = env->FindClass("java/lang/String");
285 strArray = env->NewObjectArray(argc, stringClass, NULL);
286
287 for (int i = 0; i < argc; i++) {
288 jstring argStr = env->NewStringUTF(argv[i]);
289 env->SetObjectArrayElement(strArray, i, argStr);
290 }
291
292 env->CallStaticVoidMethod(clazz, methodId, strArray);
293 return NO_ERROR;
294}
295
296/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 * The VM calls this through the "exit" hook.
298 */
299static void runtime_exit(int code)
300{
301 gCurRuntime->onExit(code);
302 exit(code);
303}
304
305/*
306 * The VM calls this through the "vfprintf" hook.
307 *
308 * We ignore "fp" and just write the results to the log file.
309 */
310static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
311{
312 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
313}
314
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800315/**
316 * The VM calls this when mutex contention debugging is enabled to
317 * determine whether or not the blocked thread was a "sensitive thread"
318 * for user responsiveness/smoothess.
319 *
320 * Our policy for this is whether or not we're tracing any StrictMode
321 * events on this thread (which we might've inherited via Binder calls
322 * into us)
323 */
324static bool runtime_isSensitiveThread() {
325 IPCThreadState* state = IPCThreadState::selfOrNull();
326 return state && state->getStrictModePolicy() != 0;
327}
328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
330/**
331 * Add VM arguments to the to-be-executed VM
332 * Stops at first non '-' argument (also stops at an argument of '--')
333 * Returns the number of args consumed
334 */
335int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
336{
337 int i;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 for (i = 0; i<argc; i++) {
340 if (argv[i][0] != '-') {
341 return i;
342 }
343 if (argv[i][1] == '-' && argv[i][2] == 0) {
344 return i+1;
345 }
346
347 JavaVMOption opt;
348 memset(&opt, 0, sizeof(opt));
349 opt.optionString = (char*)argv[i];
350 mOptions.add(opt);
351 }
352 return i;
353}
354
355static int hasDir(const char* dir)
356{
357 struct stat s;
358 int res = stat(dir, &s);
359 if (res == 0) {
360 return S_ISDIR(s.st_mode);
361 }
362 return 0;
363}
364
365/*
366 * We just want failed write() calls to just return with an error.
367 */
368static void blockSigpipe()
369{
370 sigset_t mask;
371
372 sigemptyset(&mask);
373 sigaddset(&mask, SIGPIPE);
374 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
Steve Block8564c8d2012-01-05 23:22:43 +0000375 ALOGW("WARNING: SIGPIPE not blocked\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376}
377
378/*
379 * Read the persistent locale.
380 */
381static void readLocale(char* language, char* region)
382{
383 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 property_get("persist.sys.language", propLang, "");
386 property_get("persist.sys.country", propRegn, "");
387 if (*propLang == 0 && *propRegn == 0) {
388 /* Set to ro properties, default is en_US */
389 property_get("ro.product.locale.language", propLang, "en");
390 property_get("ro.product.locale.region", propRegn, "US");
391 }
392 strncat(language, propLang, 2);
393 strncat(region, propRegn, 2);
Steve Block5baa3a62011-12-20 16:23:08 +0000394 //ALOGD("language=%s region=%s\n", language, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395}
396
Andy McFaddenf70188a2009-03-31 15:52:13 -0700397/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700398 * Parse a property containing space-separated options that should be
399 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
400 *
401 * This will cut up "extraOptsBuf" as we chop it into individual options.
402 *
403 * Adds the strings, if any, to mOptions.
404 */
405void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
406{
407 JavaVMOption opt;
408 char* start;
409 char* end;
410
411 memset(&opt, 0, sizeof(opt));
412 start = extraOptsBuf;
413 while (*start != '\0') {
414 while (*start == ' ') /* skip leading whitespace */
415 start++;
416 if (*start == '\0') /* was trailing ws, bail */
417 break;
418
419 end = start+1;
420 while (*end != ' ' && *end != '\0') /* find end of token */
421 end++;
422 if (*end == ' ')
423 *end++ = '\0'; /* mark end, advance to indicate more */
424
425 opt.optionString = start;
426 mOptions.add(opt);
427 start = end;
428 }
429}
430
431/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700432 * Start the Dalvik Virtual Machine.
433 *
434 * Various arguments, most determined by system properties, are passed in.
435 * The "mOptions" vector is updated.
436 *
437 * Returns 0 on success.
438 */
439int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700441 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 JavaVMInitArgs initArgs;
443 JavaVMOption opt;
444 char propBuf[PROPERTY_VALUE_MAX];
445 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
446 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
447 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
448 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800449 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700450 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800451 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700452 char extraOptsBuf[PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700455 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700457 enum {
458 kEMDefault,
459 kEMIntPortable,
460 kEMIntFast,
461#if defined(WITH_JIT)
462 kEMJitCompiler,
463#endif
464 } executionMode = kEMDefault;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466
467 property_get("dalvik.vm.checkjni", propBuf, "");
468 if (strcmp(propBuf, "true") == 0) {
469 checkJni = true;
470 } else if (strcmp(propBuf, "false") != 0) {
471 /* property is neither true nor false; fall back on kernel parameter */
472 property_get("ro.kernel.android.checkjni", propBuf, "");
473 if (propBuf[0] == '1') {
474 checkJni = true;
475 }
476 }
477
478 property_get("dalvik.vm.execution-mode", propBuf, "");
479 if (strcmp(propBuf, "int:portable") == 0) {
480 executionMode = kEMIntPortable;
481 } else if (strcmp(propBuf, "int:fast") == 0) {
482 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700483#if defined(WITH_JIT)
484 } else if (strcmp(propBuf, "int:jit") == 0) {
485 executionMode = kEMJitCompiler;
486#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488
489 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
490
Andy McFaddene2b23e12009-04-03 11:09:46 -0700491 property_get("dalvik.vm.check-dex-sum", propBuf, "");
492 if (strcmp(propBuf, "true") == 0) {
493 checkDexSum = true;
494 }
495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 property_get("log.redirect-stdio", propBuf, "");
497 if (strcmp(propBuf, "true") == 0) {
498 logStdio = true;
499 }
500
501 strcpy(enableAssertBuf, "-ea:");
502 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
503
504 strcpy(jniOptsBuf, "-Xjniopts:");
505 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 /* route exit() to our handler */
508 opt.extraInfo = (void*) runtime_exit;
509 opt.optionString = "exit";
510 mOptions.add(opt);
511
512 /* route fprintf() to our handler */
513 opt.extraInfo = (void*) runtime_vfprintf;
514 opt.optionString = "vfprintf";
515 mOptions.add(opt);
516
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800517 /* register the framework-specific "is sensitive thread" hook */
518 opt.extraInfo = (void*) runtime_isSensitiveThread;
519 opt.optionString = "sensitiveThread";
520 mOptions.add(opt);
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 opt.extraInfo = NULL;
523
524 /* enable verbose; standard options are { jni, gc, class } */
525 //options[curOpt++].optionString = "-verbose:jni";
526 opt.optionString = "-verbose:gc";
527 mOptions.add(opt);
528 //options[curOpt++].optionString = "-verbose:class";
529
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800530 /*
531 * The default starting and maximum size of the heap. Larger
532 * values should be specified in a product property override.
533 */
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800534 strcpy(heapstartsizeOptsBuf, "-Xms");
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800535 property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800536 opt.optionString = heapstartsizeOptsBuf;
537 mOptions.add(opt);
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700538 strcpy(heapsizeOptsBuf, "-Xmx");
539 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700540 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 mOptions.add(opt);
542
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800543 strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
544 property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
545 if (heapgrowthlimitOptsBuf[20] != '\0') {
546 opt.optionString = heapgrowthlimitOptsBuf;
547 mOptions.add(opt);
548 }
549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 /*
551 * Enable or disable dexopt features, such as bytecode verification and
552 * calculation of register maps for precise GC.
553 */
554 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
555 if (dexoptFlagsBuf[0] != '\0') {
556 const char* opc;
557 const char* val;
558
559 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
560 if (opc != NULL) {
561 switch (*(opc+2)) {
562 case 'n': val = "-Xverify:none"; break;
563 case 'r': val = "-Xverify:remote"; break;
564 case 'a': val = "-Xverify:all"; break;
565 default: val = NULL; break;
566 }
567
568 if (val != NULL) {
569 opt.optionString = val;
570 mOptions.add(opt);
571 }
572 }
573
574 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
575 if (opc != NULL) {
576 switch (*(opc+2)) {
577 case 'n': val = "-Xdexopt:none"; break;
578 case 'v': val = "-Xdexopt:verified"; break;
579 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700580 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 default: val = NULL; break;
582 }
583
584 if (val != NULL) {
585 opt.optionString = val;
586 mOptions.add(opt);
587 }
588 }
589
590 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
591 if (opc != NULL) {
592 opt.optionString = "-Xgenregmap";
593 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700594
595 /* turn on precise GC while we're at it */
596 opt.optionString = "-Xgc:precise";
597 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 }
599 }
600
601 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 /* use android ADB transport */
603 opt.optionString =
604 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 mOptions.add(opt);
606
Steve Block5baa3a62011-12-20 16:23:08 +0000607 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 if (checkJni) {
609 /* extended JNI checking */
610 opt.optionString = "-Xcheck:jni";
611 mOptions.add(opt);
612
613 /* set a cap on JNI global references */
614 opt.optionString = "-Xjnigreflimit:2000";
615 mOptions.add(opt);
616
617 /* with -Xcheck:jni, this provides a JNI function call trace */
618 //opt.optionString = "-verbose:jni";
619 //mOptions.add(opt);
620 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700621
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700622 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
623 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
624 if (strlen(propBuf) > 0) {
625 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
626 strcat(lockProfThresholdBuf, propBuf);
627 opt.optionString = lockProfThresholdBuf;
628 mOptions.add(opt);
629 }
630
Ben Cheng52b0e732009-06-19 13:31:12 -0700631#if defined(WITH_JIT)
Ben Cheng52b0e732009-06-19 13:31:12 -0700632 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
633 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
634 property_get("dalvik.vm.jit.op", propBuf, "");
635 if (strlen(propBuf) > 0) {
636 strcpy(jitOpBuf, "-Xjitop:");
637 strcat(jitOpBuf, propBuf);
638 opt.optionString = jitOpBuf;
639 mOptions.add(opt);
640 }
641
Ben Cheng52b0e732009-06-19 13:31:12 -0700642 /* Force interpreter-only mode for selected methods */
643 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
644 property_get("dalvik.vm.jit.method", propBuf, "");
645 if (strlen(propBuf) > 0) {
646 strcpy(jitMethodBuf, "-Xjitmethod:");
647 strcat(jitMethodBuf, propBuf);
648 opt.optionString = jitMethodBuf;
649 mOptions.add(opt);
650 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700651#endif
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 if (executionMode == kEMIntPortable) {
654 opt.optionString = "-Xint:portable";
655 mOptions.add(opt);
656 } else if (executionMode == kEMIntFast) {
657 opt.optionString = "-Xint:fast";
658 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700659#if defined(WITH_JIT)
660 } else if (executionMode == kEMJitCompiler) {
661 opt.optionString = "-Xint:jit";
662 mOptions.add(opt);
663#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700665
666 if (checkDexSum) {
667 /* perform additional DEX checksum tests */
668 opt.optionString = "-Xcheckdexsum";
669 mOptions.add(opt);
670 }
671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 if (logStdio) {
673 /* convert stdout/stderr to log messages */
674 opt.optionString = "-Xlog-stdio";
675 mOptions.add(opt);
676 }
677
678 if (enableAssertBuf[4] != '\0') {
679 /* accept "all" to mean "all classes and packages" */
680 if (strcmp(enableAssertBuf+4, "all") == 0)
681 enableAssertBuf[3] = '\0';
Steve Block6215d3f2012-01-04 20:05:49 +0000682 ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 opt.optionString = enableAssertBuf;
684 mOptions.add(opt);
685 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100686 ALOGV("Assertions disabled\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688
689 if (jniOptsBuf[10] != '\0') {
Steve Block6215d3f2012-01-04 20:05:49 +0000690 ALOGI("JNI options: '%s'\n", jniOptsBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 opt.optionString = jniOptsBuf;
692 mOptions.add(opt);
693 }
694
695 if (stackTraceFileBuf[0] != '\0') {
696 static const char* stfOptName = "-Xstacktracefile:";
697
698 stackTraceFile = (char*) malloc(strlen(stfOptName) +
699 strlen(stackTraceFileBuf) +1);
700 strcpy(stackTraceFile, stfOptName);
701 strcat(stackTraceFile, stackTraceFileBuf);
702 opt.optionString = stackTraceFile;
703 mOptions.add(opt);
704 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700705
706 /* extra options; parse this late so it overrides others */
707 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
708 parseExtraOpts(extraOptsBuf);
709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 /* Set the properties for locale */
711 {
712 char langOption[sizeof("-Duser.language=") + 3];
713 char regionOption[sizeof("-Duser.region=") + 3];
714 strcpy(langOption, "-Duser.language=");
715 strcpy(regionOption, "-Duser.region=");
716 readLocale(langOption, regionOption);
717 opt.extraInfo = NULL;
718 opt.optionString = langOption;
719 mOptions.add(opt);
720 opt.optionString = regionOption;
721 mOptions.add(opt);
722 }
723
724 /*
725 * We don't have /tmp on the device, but we often have an SD card. Apps
726 * shouldn't use this, but some test suites might want to exercise it.
727 */
728 opt.optionString = "-Djava.io.tmpdir=/sdcard";
729 mOptions.add(opt);
730
731 initArgs.version = JNI_VERSION_1_4;
732 initArgs.options = mOptions.editArray();
733 initArgs.nOptions = mOptions.size();
734 initArgs.ignoreUnrecognized = JNI_FALSE;
735
736 /*
737 * Initialize the VM.
738 *
739 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
740 * If this call succeeds, the VM is ready, and we can start issuing
741 * JNI calls.
742 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700743 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000744 ALOGE("JNI_CreateJavaVM failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 goto bail;
746 }
747
Andy McFaddenf70188a2009-03-31 15:52:13 -0700748 result = 0;
749
750bail:
751 free(stackTraceFile);
752 return result;
753}
754
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700755char* AndroidRuntime::toSlashClassName(const char* className)
756{
757 char* result = strdup(className);
758 for (char* cp = result; *cp != '\0'; cp++) {
759 if (*cp == '.') {
760 *cp = '/';
761 }
762 }
763 return result;
764}
765
Andy McFaddenf70188a2009-03-31 15:52:13 -0700766/*
767 * Start the Android runtime. This involves starting the virtual machine
768 * and calling the "static void main(String[] args)" method in the class
769 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700770 *
771 * Passes the main function two arguments, the class name and the specified
772 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700773 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700774void AndroidRuntime::start(const char* className, const char* options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700775{
Steve Block5baa3a62011-12-20 16:23:08 +0000776 ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700777 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700778
Andy McFaddenf70188a2009-03-31 15:52:13 -0700779 blockSigpipe();
780
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700781 /*
782 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700783 * init.rc anymore, so we print out the boot start event here.
784 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700785 if (strcmp(options, "start-system-server") == 0) {
Andy McFaddenf70188a2009-03-31 15:52:13 -0700786 /* track our progress through the boot sequence */
787 const int LOG_BOOT_PROGRESS_START = 3000;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700788 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
Andy McFaddenf70188a2009-03-31 15:52:13 -0700789 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
790 }
791
792 const char* rootDir = getenv("ANDROID_ROOT");
793 if (rootDir == NULL) {
794 rootDir = "/system";
795 if (!hasDir("/system")) {
796 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700797 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700798 }
799 setenv("ANDROID_ROOT", rootDir, 1);
800 }
801
802 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block5baa3a62011-12-20 16:23:08 +0000803 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700804
805 /* start the virtual machine */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700806 JNIEnv* env;
807 if (startVm(&mJavaVM, &env) != 0) {
808 return;
809 }
810 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 /*
813 * Register android functions.
814 */
815 if (startReg(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000816 ALOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700817 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
819
820 /*
821 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700822 * At present we have two arguments, the class name and an option string.
823 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 */
825 jclass stringClass;
826 jobjectArray strArray;
827 jstring classNameStr;
Jeff Brownebed7d62011-05-16 17:08:42 -0700828 jstring optionsStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829
830 stringClass = env->FindClass("java/lang/String");
831 assert(stringClass != NULL);
832 strArray = env->NewObjectArray(2, stringClass, NULL);
833 assert(strArray != NULL);
834 classNameStr = env->NewStringUTF(className);
835 assert(classNameStr != NULL);
836 env->SetObjectArrayElement(strArray, 0, classNameStr);
Jeff Brownebed7d62011-05-16 17:08:42 -0700837 optionsStr = env->NewStringUTF(options);
838 env->SetObjectArrayElement(strArray, 1, optionsStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839
840 /*
841 * Start VM. This thread becomes the main thread of the VM, and will
842 * not return until the VM exits.
843 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700844 char* slashClassName = toSlashClassName(className);
845 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 if (startClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000847 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 /* keep going */
849 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700850 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 "([Ljava/lang/String;)V");
852 if (startMeth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000853 ALOGE("JavaVM unable to find main() in '%s'\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 /* keep going */
855 } else {
856 env->CallStaticVoidMethod(startClass, startMeth, strArray);
857
858#if 0
859 if (env->ExceptionCheck())
860 threadExitUncaughtException(env);
861#endif
862 }
863 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700864 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
Steve Block5baa3a62011-12-20 16:23:08 +0000866 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 if (mJavaVM->DetachCurrentThread() != JNI_OK)
Steve Block8564c8d2012-01-05 23:22:43 +0000868 ALOGW("Warning: unable to detach main thread\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 if (mJavaVM->DestroyJavaVM() != 0)
Steve Block8564c8d2012-01-05 23:22:43 +0000870 ALOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871}
872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873void AndroidRuntime::onExit(int code)
874{
Steve Block71f2cf12011-10-20 11:56:00 +0100875 ALOGV("AndroidRuntime onExit calling exit(%d)", code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 exit(code);
877}
878
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700879void AndroidRuntime::onVmCreated(JNIEnv* env)
880{
881 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
882}
883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884/*
885 * Get the JNIEnv pointer for this thread.
886 *
887 * Returns NULL if the slot wasn't allocated or populated.
888 */
889/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
890{
891 JNIEnv* env;
892 JavaVM* vm = AndroidRuntime::getJavaVM();
893 assert(vm != NULL);
894
895 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
896 return NULL;
897 return env;
898}
899
900/*
901 * Makes the current thread visible to the VM.
902 *
903 * The JNIEnv pointer returned is only valid for the current thread, and
904 * thus must be tucked into thread-local storage.
905 */
906static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
907{
908 JavaVMAttachArgs args;
909 JavaVM* vm;
910 jint result;
911
912 vm = AndroidRuntime::getJavaVM();
913 assert(vm != NULL);
914
915 args.version = JNI_VERSION_1_4;
916 args.name = (char*) threadName;
917 args.group = NULL;
918
919 result = vm->AttachCurrentThread(pEnv, (void*) &args);
920 if (result != JNI_OK)
Steve Block6215d3f2012-01-04 20:05:49 +0000921 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922
923 return result;
924}
925
926/*
927 * Detach the current thread from the set visible to the VM.
928 */
929static int javaDetachThread(void)
930{
931 JavaVM* vm;
932 jint result;
933
934 vm = AndroidRuntime::getJavaVM();
935 assert(vm != NULL);
936
937 result = vm->DetachCurrentThread();
938 if (result != JNI_OK)
Steve Block3762c312012-01-06 19:20:56 +0000939 ALOGE("ERROR: thread detach failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 return result;
941}
942
943/*
944 * When starting a native thread that will be visible from the VM, we
945 * bounce through this to get the right attach/detach action.
946 * Note that this function calls free(args)
947 */
948/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
949 void* start = ((void**)args)[0];
950 void* userData = ((void **)args)[1];
951 char* name = (char*) ((void **)args)[2]; // we own this storage
952 free(args);
953 JNIEnv* env;
954 int result;
955
956 /* hook us into the VM */
957 if (javaAttachThread(name, &env) != JNI_OK)
958 return -1;
959
960 /* start the thread running */
961 result = (*(android_thread_func_t)start)(userData);
962
963 /* unhook us */
964 javaDetachThread();
965 free(name);
966
967 return result;
968}
969
970/*
971 * This is invoked from androidCreateThreadEtc() via the callback
972 * set with androidSetCreateThreadFunc().
973 *
974 * We need to create the new thread in such a way that it gets hooked
975 * into the VM before it really starts executing.
976 */
977/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700978 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 void* userData,
980 const char* threadName,
981 int32_t threadPriority,
982 size_t threadStackSize,
983 android_thread_id_t* threadId)
984{
985 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
986 int result;
987
988 assert(threadName != NULL);
989
990 args[0] = (void*) entryFunction;
991 args[1] = userData;
992 args[2] = (void*) strdup(threadName); // javaThreadShell must free
993
994 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
995 threadName, threadPriority, threadStackSize, threadId);
996 return result;
997}
998
999/*
1000 * Create a thread that is visible from the VM.
1001 *
1002 * This is called from elsewhere in the library.
1003 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001004/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 void (*start)(void *), void* arg)
1006{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001007 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001009 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1010 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011}
1012
1013#if 0
1014static void quickTest(void* arg)
1015{
1016 const char* str = (const char*) arg;
1017
1018 printf("In quickTest: %s\n", str);
1019}
1020#endif
1021
1022#ifdef NDEBUG
1023 #define REG_JNI(name) { name }
1024 struct RegJNIRec {
1025 int (*mProc)(JNIEnv*);
1026 };
1027#else
1028 #define REG_JNI(name) { name, #name }
1029 struct RegJNIRec {
1030 int (*mProc)(JNIEnv*);
1031 const char* mName;
1032 };
1033#endif
1034
1035typedef void (*RegJAMProc)();
1036
1037static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1038{
1039 for (size_t i = 0; i < count; i++) {
1040 if (array[i].mProc(env) < 0) {
1041#ifndef NDEBUG
Steve Block5baa3a62011-12-20 16:23:08 +00001042 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043#endif
1044 return -1;
1045 }
1046 }
1047 return 0;
1048}
1049
1050static void register_jam_procs(const RegJAMProc array[], size_t count)
1051{
1052 for (size_t i = 0; i < count; i++) {
1053 array[i]();
1054 }
1055}
1056
1057static const RegJNIRec gRegJNI[] = {
1058 REG_JNI(register_android_debug_JNITest),
1059 REG_JNI(register_com_android_internal_os_RuntimeInit),
1060 REG_JNI(register_android_os_SystemClock),
1061 REG_JNI(register_android_util_EventLog),
1062 REG_JNI(register_android_util_Log),
1063 REG_JNI(register_android_util_FloatMath),
1064 REG_JNI(register_android_text_format_Time),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 REG_JNI(register_android_content_AssetManager),
1066 REG_JNI(register_android_content_StringBlock),
1067 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001068 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001070 REG_JNI(register_android_text_AndroidBidi),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 REG_JNI(register_android_text_KeyCharacterMap),
1072 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001073 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 REG_JNI(register_android_os_Binder),
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001075 REG_JNI(register_android_os_Parcel),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 REG_JNI(register_android_view_Display),
Jeff Brown0a0a1242011-12-02 02:25:22 -08001077 REG_JNI(register_android_view_DisplayEventReceiver),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 REG_JNI(register_android_nio_utils),
1079 REG_JNI(register_android_graphics_PixelFormat),
1080 REG_JNI(register_android_graphics_Graphics),
Chet Haasea1cff502012-02-21 13:43:44 -08001081 REG_JNI(register_android_view_GLES20DisplayList),
Romain Guye4d01122010-06-16 18:44:05 -07001082 REG_JNI(register_android_view_GLES20Canvas),
Romain Guya9582652011-11-10 14:20:10 -08001083 REG_JNI(register_android_view_HardwareRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 REG_JNI(register_android_view_Surface),
Romain Guy8f0095c2011-05-02 17:24:22 -07001085 REG_JNI(register_android_view_TextureView),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1087 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001088 REG_JNI(register_android_opengl_jni_GLES10),
1089 REG_JNI(register_android_opengl_jni_GLES10Ext),
1090 REG_JNI(register_android_opengl_jni_GLES11),
1091 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001092 REG_JNI(register_android_opengl_jni_GLES20),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093
1094 REG_JNI(register_android_graphics_Bitmap),
1095 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001096 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 REG_JNI(register_android_graphics_Camera),
1098 REG_JNI(register_android_graphics_Canvas),
1099 REG_JNI(register_android_graphics_ColorFilter),
1100 REG_JNI(register_android_graphics_DrawFilter),
1101 REG_JNI(register_android_graphics_Interpolator),
1102 REG_JNI(register_android_graphics_LayerRasterizer),
1103 REG_JNI(register_android_graphics_MaskFilter),
1104 REG_JNI(register_android_graphics_Matrix),
1105 REG_JNI(register_android_graphics_Movie),
1106 REG_JNI(register_android_graphics_NinePatch),
1107 REG_JNI(register_android_graphics_Paint),
1108 REG_JNI(register_android_graphics_Path),
1109 REG_JNI(register_android_graphics_PathMeasure),
1110 REG_JNI(register_android_graphics_PathEffect),
1111 REG_JNI(register_android_graphics_Picture),
1112 REG_JNI(register_android_graphics_PorterDuff),
1113 REG_JNI(register_android_graphics_Rasterizer),
1114 REG_JNI(register_android_graphics_Region),
1115 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001116 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 REG_JNI(register_android_graphics_Typeface),
1118 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001119 REG_JNI(register_android_graphics_YuvImage),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120
1121 REG_JNI(register_android_database_CursorWindow),
Jeff Browne5360fb2011-10-31 17:48:13 -07001122 REG_JNI(register_android_database_SQLiteConnection),
1123 REG_JNI(register_android_database_SQLiteGlobal),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 REG_JNI(register_android_database_SQLiteDebug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 REG_JNI(register_android_os_FileObserver),
1127 REG_JNI(register_android_os_FileUtils),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001128 REG_JNI(register_android_os_MessageQueue),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 REG_JNI(register_android_os_ParcelFileDescriptor),
1130 REG_JNI(register_android_os_Power),
1131 REG_JNI(register_android_os_StatFs),
Jeff Brown481c1572012-03-09 14:41:15 -08001132 REG_JNI(register_android_os_Trace),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 REG_JNI(register_android_os_UEventObserver),
1134 REG_JNI(register_android_net_LocalSocketImpl),
1135 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001136 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 REG_JNI(register_android_net_wifi_WifiManager),
1138 REG_JNI(register_android_os_MemoryFile),
1139 REG_JNI(register_com_android_internal_os_ZygoteInit),
1140 REG_JNI(register_android_hardware_Camera),
1141 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -04001142 REG_JNI(register_android_hardware_SerialPort),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001143 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001144 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001145 REG_JNI(register_android_hardware_UsbRequest),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 REG_JNI(register_android_media_AudioRecord),
1147 REG_JNI(register_android_media_AudioSystem),
1148 REG_JNI(register_android_media_AudioTrack),
1149 REG_JNI(register_android_media_JetPlayer),
1150 REG_JNI(register_android_media_ToneGenerator),
1151
1152 REG_JNI(register_android_opengl_classes),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 REG_JNI(register_android_bluetooth_HeadsetBase),
1154 REG_JNI(register_android_bluetooth_BluetoothAudioGateway),
Nick Pelly0b6955a2009-05-26 19:13:43 -07001155 REG_JNI(register_android_bluetooth_BluetoothSocket),
Nick Pellybd022f42009-08-14 18:33:38 -07001156 REG_JNI(register_android_server_BluetoothService),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 REG_JNI(register_android_server_BluetoothEventLoop),
1158 REG_JNI(register_android_server_BluetoothA2dpService),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001159 REG_JNI(register_android_server_NetworkManagementSocketTagger),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001160 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001162 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001163 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001164 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001165 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001166 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001167 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001168 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001169 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001170 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001171 REG_JNI(register_android_view_KeyEvent),
1172 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001173 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001174 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001175
1176 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001177 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001178
1179 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001180 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181};
1182
1183/*
1184 * Register android native functions with the VM.
1185 */
1186/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1187{
1188 /*
1189 * This hook causes all future threads created in this process to be
1190 * attached to the JavaVM. (This needs to go away in favor of JNI
1191 * Attach calls.)
1192 */
1193 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1194
Steve Block71f2cf12011-10-20 11:56:00 +01001195 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196
1197 /*
1198 * Every "register" function calls one or more things that return
1199 * a local reference (e.g. FindClass). Because we haven't really
1200 * started the VM yet, they're all getting stored in the base frame
1201 * and never released. Use Push/Pop to manage the storage.
1202 */
1203 env->PushLocalFrame(200);
1204
1205 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1206 env->PopLocalFrame(NULL);
1207 return -1;
1208 }
1209 env->PopLocalFrame(NULL);
1210
1211 //createJavaThread("fubar", quickTest, (void*) "hello");
1212
1213 return 0;
1214}
1215
1216AndroidRuntime* AndroidRuntime::getRuntime()
1217{
1218 return gCurRuntime;
1219}
1220
1221/**
1222 * Used by WithFramework to register native functions.
1223 */
1224extern "C"
1225jint Java_com_android_internal_util_WithFramework_registerNatives(
1226 JNIEnv* env, jclass clazz) {
1227 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1228}
1229
1230/**
1231 * Used by LoadClass to register native functions.
1232 */
1233extern "C"
1234jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1235 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1236}
1237
1238} // namespace android