blob: 3b6b1608db0091f2002b7f1f0c4c1966840f2d1e [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050extern int register_android_os_Binder(JNIEnv* env);
51extern int register_android_os_Process(JNIEnv* env);
52extern int register_android_graphics_Bitmap(JNIEnv*);
53extern int register_android_graphics_BitmapFactory(JNIEnv*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080054extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055extern int register_android_graphics_Camera(JNIEnv* env);
56extern int register_android_graphics_Graphics(JNIEnv* env);
57extern int register_android_graphics_Interpolator(JNIEnv* env);
58extern int register_android_graphics_LayerRasterizer(JNIEnv*);
59extern int register_android_graphics_MaskFilter(JNIEnv* env);
60extern int register_android_graphics_Movie(JNIEnv* env);
61extern int register_android_graphics_NinePatch(JNIEnv*);
62extern int register_android_graphics_PathEffect(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063extern int register_android_graphics_Shader(JNIEnv* env);
64extern int register_android_graphics_Typeface(JNIEnv* env);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080065extern int register_android_graphics_YuvImage(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
68extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
Thomas Tafertshofer6b1e8382012-07-03 13:37:35 -070069extern int register_android_opengl_jni_EGL14(JNIEnv* env);
Jack Palevich1c4907e2009-04-13 16:22:25 -070070extern int register_android_opengl_jni_GLES10(JNIEnv* env);
71extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
72extern int register_android_opengl_jni_GLES11(JNIEnv* env);
73extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
Jack Palevich560814f2009-11-19 16:34:55 +080074extern int register_android_opengl_jni_GLES20(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
76extern int register_android_hardware_Camera(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077extern int register_android_hardware_SensorManager(JNIEnv *env);
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040078extern int register_android_hardware_SerialPort(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);
Jeff Brown0a0a1242011-12-02 02:25:22 -0800117extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
Chet Haasea1cff502012-02-21 13:43:44 -0800118extern int register_android_view_GLES20DisplayList(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700119extern int register_android_view_GLES20Canvas(JNIEnv* env);
Romain Guya9582652011-11-10 14:20:10 -0800120extern int register_android_view_HardwareRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121extern int register_android_view_Surface(JNIEnv* env);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800122extern int register_android_view_SurfaceControl(JNIEnv* env);
Jeff Brown64a55af2012-08-26 02:47:39 -0700123extern int register_android_view_SurfaceSession(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700124extern int register_android_view_TextureView(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125extern int register_android_database_CursorWindow(JNIEnv* env);
Jeff Browne5360fb2011-10-31 17:48:13 -0700126extern int register_android_database_SQLiteConnection(JNIEnv* env);
127extern int register_android_database_SQLiteGlobal(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128extern int register_android_database_SQLiteDebug(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129extern int register_android_debug_JNITest(JNIEnv* env);
130extern int register_android_nio_utils(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131extern int register_android_text_format_Time(JNIEnv* env);
132extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700133extern int register_android_os_MessageQueue(JNIEnv* env);
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800134extern int register_android_os_Parcel(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135extern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500136extern int register_android_os_SELinux(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138extern int register_android_os_SystemClock(JNIEnv* env);
Jeff Brown481c1572012-03-09 14:41:15 -0800139extern int register_android_os_Trace(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140extern int register_android_os_FileObserver(JNIEnv *env);
141extern int register_android_os_FileUtils(JNIEnv *env);
142extern int register_android_os_UEventObserver(JNIEnv* env);
143extern int register_android_os_MemoryFile(JNIEnv* env);
144extern int register_android_net_LocalSocketImpl(JNIEnv* env);
145extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700146extern int register_android_net_TrafficStats(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147extern int register_android_net_wifi_WifiManager(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148extern int register_android_text_AndroidCharacter(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800149extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150extern int register_android_opengl_classes(JNIEnv *env);
JP Abgrall98a4f7e2011-09-02 15:36:33 -0700151extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
Christopher Tateecaa7b42010-06-04 14:55:02 -0700152extern int register_android_server_Watchdog(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
154extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700155extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700156extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700157extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400158extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Christopher Tate4a627c72011-04-01 14:43:32 -0700159extern int register_android_app_backup_FullBackup(JNIEnv *env);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700160extern int register_android_app_ActivityThread(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700161extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Browncbad9762012-09-04 21:57:59 -0700162extern int register_android_media_RemoteDisplay(JNIEnv *env);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700163extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700164extern int register_android_view_InputDevice(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800165extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brownc28867a2013-03-26 15:42:39 -0700166extern int register_android_view_InputEventSender(JNIEnv* env);
Michael Wrighta44dd262013-04-10 21:12:00 -0700167extern int register_android_view_InputQueue(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700168extern int register_android_view_KeyCharacterMap(JNIEnv *env);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700169extern int register_android_view_KeyEvent(JNIEnv* env);
170extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700171extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700172extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700173extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700174extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700175extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700176extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800177extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
179static AndroidRuntime* gCurRuntime = NULL;
180
181static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
182{
183 if (jniThrowException(env, exc, msg) != 0)
184 assert(false);
185}
186
187/*
188 * Code written in the Java Programming Language calls here from main().
189 */
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700190static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191{
192 gCurRuntime->onStarted();
193}
194
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700195static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196{
197 gCurRuntime->onZygoteInit();
198}
199
Jeff Brown4280c4a2012-03-15 17:48:02 -0700200static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv* env,
201 jobject clazz, jboolean exitWithoutCleanup)
202{
203 gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup);
204}
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206/*
207 * JNI registration.
208 */
209static JNINativeMethod gMethods[] = {
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700210 { "nativeFinishInit", "()V",
211 (void*) com_android_internal_os_RuntimeInit_nativeFinishInit },
212 { "nativeZygoteInit", "()V",
213 (void*) com_android_internal_os_RuntimeInit_nativeZygoteInit },
Jeff Brown4280c4a2012-03-15 17:48:02 -0700214 { "nativeSetExitWithoutCleanup", "(Z)V",
215 (void*) com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216};
217
218int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
219{
220 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
221 gMethods, NELEM(gMethods));
222}
223
224// ----------------------------------------------------------------------
225
226/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
227
228
Jeff Brown4280c4a2012-03-15 17:48:02 -0700229AndroidRuntime::AndroidRuntime() :
230 mExitWithoutCleanup(false)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700232 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 // this sets our preference for 16bit images during decode
234 // in case the src is opaque and 24bit
235 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400236 // This cache is shared between browser native images, and java "purgeable"
237 // bitmaps. This globalpool is for images that do not either use the java
238 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
239 // java call site.
240 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
241 // There is also a global font cache, but its budget is specified in code
242 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
244 // Pre-allocate enough space to hold a fair number of options.
245 mOptions.setCapacity(20);
246
247 assert(gCurRuntime == NULL); // one per process
248 gCurRuntime = this;
249}
250
251AndroidRuntime::~AndroidRuntime()
252{
253 SkGraphics::Term();
254}
255
256/*
257 * Register native methods using JNI.
258 */
259/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
260 const char* className, const JNINativeMethod* gMethods, int numMethods)
261{
262 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
263}
264
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700265status_t AndroidRuntime::callMain(const char* className,
266 jclass clazz, int argc, const char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267{
268 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 jmethodID methodId;
270
Steve Block5baa3a62011-12-20 16:23:08 +0000271 ALOGD("Calling main entry %s", className);
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700274 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 return UNKNOWN_ERROR;
276 }
277
278 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
279 if (methodId == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000280 ALOGE("ERROR: could not find method %s.main(String[])\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 return UNKNOWN_ERROR;
282 }
283
284 /*
285 * We want to call main() with a String array with our arguments in it.
286 * Create an array and populate it.
287 */
288 jclass stringClass;
289 jobjectArray strArray;
290
291 stringClass = env->FindClass("java/lang/String");
292 strArray = env->NewObjectArray(argc, stringClass, NULL);
293
294 for (int i = 0; i < argc; i++) {
295 jstring argStr = env->NewStringUTF(argv[i]);
296 env->SetObjectArrayElement(strArray, i, argStr);
297 }
298
299 env->CallStaticVoidMethod(clazz, methodId, strArray);
300 return NO_ERROR;
301}
302
303/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 * The VM calls this through the "exit" hook.
305 */
306static void runtime_exit(int code)
307{
Jeff Brown4280c4a2012-03-15 17:48:02 -0700308 gCurRuntime->exit(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309}
310
311/*
312 * The VM calls this through the "vfprintf" hook.
313 *
314 * We ignore "fp" and just write the results to the log file.
315 */
316static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
317{
318 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
319}
320
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800321/**
322 * The VM calls this when mutex contention debugging is enabled to
323 * determine whether or not the blocked thread was a "sensitive thread"
324 * for user responsiveness/smoothess.
325 *
326 * Our policy for this is whether or not we're tracing any StrictMode
327 * events on this thread (which we might've inherited via Binder calls
328 * into us)
329 */
330static bool runtime_isSensitiveThread() {
331 IPCThreadState* state = IPCThreadState::selfOrNull();
332 return state && state->getStrictModePolicy() != 0;
333}
334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
336/**
337 * Add VM arguments to the to-be-executed VM
338 * Stops at first non '-' argument (also stops at an argument of '--')
339 * Returns the number of args consumed
340 */
341int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
342{
343 int i;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 for (i = 0; i<argc; i++) {
346 if (argv[i][0] != '-') {
347 return i;
348 }
349 if (argv[i][1] == '-' && argv[i][2] == 0) {
350 return i+1;
351 }
352
353 JavaVMOption opt;
354 memset(&opt, 0, sizeof(opt));
355 opt.optionString = (char*)argv[i];
356 mOptions.add(opt);
357 }
358 return i;
359}
360
361static int hasDir(const char* dir)
362{
363 struct stat s;
364 int res = stat(dir, &s);
365 if (res == 0) {
366 return S_ISDIR(s.st_mode);
367 }
368 return 0;
369}
370
371/*
372 * We just want failed write() calls to just return with an error.
373 */
374static void blockSigpipe()
375{
376 sigset_t mask;
377
378 sigemptyset(&mask);
379 sigaddset(&mask, SIGPIPE);
380 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
Steve Block8564c8d2012-01-05 23:22:43 +0000381 ALOGW("WARNING: SIGPIPE not blocked\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382}
383
384/*
385 * Read the persistent locale.
386 */
387static void readLocale(char* language, char* region)
388{
389 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 property_get("persist.sys.language", propLang, "");
392 property_get("persist.sys.country", propRegn, "");
393 if (*propLang == 0 && *propRegn == 0) {
394 /* Set to ro properties, default is en_US */
395 property_get("ro.product.locale.language", propLang, "en");
396 property_get("ro.product.locale.region", propRegn, "US");
397 }
398 strncat(language, propLang, 2);
399 strncat(region, propRegn, 2);
Steve Block5baa3a62011-12-20 16:23:08 +0000400 //ALOGD("language=%s region=%s\n", language, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401}
402
Andy McFaddenf70188a2009-03-31 15:52:13 -0700403/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700404 * Parse a property containing space-separated options that should be
405 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
406 *
407 * This will cut up "extraOptsBuf" as we chop it into individual options.
408 *
409 * Adds the strings, if any, to mOptions.
410 */
411void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
412{
413 JavaVMOption opt;
414 char* start;
415 char* end;
416
417 memset(&opt, 0, sizeof(opt));
418 start = extraOptsBuf;
419 while (*start != '\0') {
420 while (*start == ' ') /* skip leading whitespace */
421 start++;
422 if (*start == '\0') /* was trailing ws, bail */
423 break;
424
425 end = start+1;
426 while (*end != ' ' && *end != '\0') /* find end of token */
427 end++;
428 if (*end == ' ')
429 *end++ = '\0'; /* mark end, advance to indicate more */
430
431 opt.optionString = start;
432 mOptions.add(opt);
433 start = end;
434 }
435}
436
437/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700438 * Start the Dalvik Virtual Machine.
439 *
440 * Various arguments, most determined by system properties, are passed in.
441 * The "mOptions" vector is updated.
442 *
443 * Returns 0 on success.
444 */
445int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700447 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 JavaVMInitArgs initArgs;
449 JavaVMOption opt;
450 char propBuf[PROPERTY_VALUE_MAX];
451 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
452 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
453 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
454 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800455 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700456 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c32011-01-19 12:33:12 -0800457 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Ian Rogers53250102012-09-23 16:38:03 -0700458 char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
459 char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
460 char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700461 char extraOptsBuf[PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700464 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700466 enum {
467 kEMDefault,
468 kEMIntPortable,
469 kEMIntFast,
Ben Cheng52b0e732009-06-19 13:31:12 -0700470 kEMJitCompiler,
Ben Cheng52b0e732009-06-19 13:31:12 -0700471 } executionMode = kEMDefault;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473
474 property_get("dalvik.vm.checkjni", propBuf, "");
475 if (strcmp(propBuf, "true") == 0) {
476 checkJni = true;
477 } else if (strcmp(propBuf, "false") != 0) {
478 /* property is neither true nor false; fall back on kernel parameter */
479 property_get("ro.kernel.android.checkjni", propBuf, "");
480 if (propBuf[0] == '1') {
481 checkJni = true;
482 }
483 }
484
485 property_get("dalvik.vm.execution-mode", propBuf, "");
486 if (strcmp(propBuf, "int:portable") == 0) {
487 executionMode = kEMIntPortable;
488 } else if (strcmp(propBuf, "int:fast") == 0) {
489 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700490 } else if (strcmp(propBuf, "int:jit") == 0) {
491 executionMode = kEMJitCompiler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 }
493
494 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
495
Andy McFaddene2b23e12009-04-03 11:09:46 -0700496 property_get("dalvik.vm.check-dex-sum", propBuf, "");
497 if (strcmp(propBuf, "true") == 0) {
498 checkDexSum = true;
499 }
500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 property_get("log.redirect-stdio", propBuf, "");
502 if (strcmp(propBuf, "true") == 0) {
503 logStdio = true;
504 }
505
506 strcpy(enableAssertBuf, "-ea:");
507 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
508
509 strcpy(jniOptsBuf, "-Xjniopts:");
510 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 /* route exit() to our handler */
513 opt.extraInfo = (void*) runtime_exit;
514 opt.optionString = "exit";
515 mOptions.add(opt);
516
517 /* route fprintf() to our handler */
518 opt.extraInfo = (void*) runtime_vfprintf;
519 opt.optionString = "vfprintf";
520 mOptions.add(opt);
521
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800522 /* register the framework-specific "is sensitive thread" hook */
523 opt.extraInfo = (void*) runtime_isSensitiveThread;
524 opt.optionString = "sensitiveThread";
525 mOptions.add(opt);
526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 opt.extraInfo = NULL;
528
529 /* enable verbose; standard options are { jni, gc, class } */
530 //options[curOpt++].optionString = "-verbose:jni";
531 opt.optionString = "-verbose:gc";
532 mOptions.add(opt);
533 //options[curOpt++].optionString = "-verbose:class";
534
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800535 /*
536 * The default starting and maximum size of the heap. Larger
537 * values should be specified in a product property override.
538 */
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800539 strcpy(heapstartsizeOptsBuf, "-Xms");
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800540 property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800541 opt.optionString = heapstartsizeOptsBuf;
542 mOptions.add(opt);
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700543 strcpy(heapsizeOptsBuf, "-Xmx");
544 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700545 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 mOptions.add(opt);
547
Elliott Hughes3f177d72012-04-12 16:02:56 -0700548 // Increase the main thread's interpreter stack size for bug 6315322.
549 opt.optionString = "-XX:mainThreadStackSize=24K";
550 mOptions.add(opt);
551
Carl Shapiro8cdf27c32011-01-19 12:33:12 -0800552 strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
553 property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
554 if (heapgrowthlimitOptsBuf[20] != '\0') {
555 opt.optionString = heapgrowthlimitOptsBuf;
556 mOptions.add(opt);
557 }
558
Ian Rogers53250102012-09-23 16:38:03 -0700559 strcpy(heapminfreeOptsBuf, "-XX:HeapMinFree=");
560 property_get("dalvik.vm.heapminfree", heapminfreeOptsBuf+16, "");
561 if (heapminfreeOptsBuf[16] != '\0') {
562 opt.optionString = heapminfreeOptsBuf;
563 mOptions.add(opt);
564 }
565
566 strcpy(heapmaxfreeOptsBuf, "-XX:HeapMaxFree=");
567 property_get("dalvik.vm.heapmaxfree", heapmaxfreeOptsBuf+16, "");
568 if (heapmaxfreeOptsBuf[16] != '\0') {
569 opt.optionString = heapmaxfreeOptsBuf;
570 mOptions.add(opt);
571 }
572
573 strcpy(heaptargetutilizationOptsBuf, "-XX:HeapTargetUtilization=");
574 property_get("dalvik.vm.heaptargetutilization", heaptargetutilizationOptsBuf+26, "");
Ian Rogers7fef2b82012-10-01 14:54:24 -0700575 if (heaptargetutilizationOptsBuf[26] != '\0') {
Ian Rogers53250102012-09-23 16:38:03 -0700576 opt.optionString = heaptargetutilizationOptsBuf;
577 mOptions.add(opt);
578 }
579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 /*
581 * Enable or disable dexopt features, such as bytecode verification and
582 * calculation of register maps for precise GC.
583 */
584 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
585 if (dexoptFlagsBuf[0] != '\0') {
586 const char* opc;
587 const char* val;
588
589 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
590 if (opc != NULL) {
591 switch (*(opc+2)) {
592 case 'n': val = "-Xverify:none"; break;
593 case 'r': val = "-Xverify:remote"; break;
594 case 'a': val = "-Xverify:all"; break;
595 default: val = NULL; break;
596 }
597
598 if (val != NULL) {
599 opt.optionString = val;
600 mOptions.add(opt);
601 }
602 }
603
604 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
605 if (opc != NULL) {
606 switch (*(opc+2)) {
607 case 'n': val = "-Xdexopt:none"; break;
608 case 'v': val = "-Xdexopt:verified"; break;
609 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700610 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 default: val = NULL; break;
612 }
613
614 if (val != NULL) {
615 opt.optionString = val;
616 mOptions.add(opt);
617 }
618 }
619
620 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
621 if (opc != NULL) {
622 opt.optionString = "-Xgenregmap";
623 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700624
625 /* turn on precise GC while we're at it */
626 opt.optionString = "-Xgc:precise";
627 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629 }
630
631 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 /* use android ADB transport */
633 opt.optionString =
634 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 mOptions.add(opt);
636
Steve Block5baa3a62011-12-20 16:23:08 +0000637 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 if (checkJni) {
639 /* extended JNI checking */
640 opt.optionString = "-Xcheck:jni";
641 mOptions.add(opt);
642
643 /* set a cap on JNI global references */
644 opt.optionString = "-Xjnigreflimit:2000";
645 mOptions.add(opt);
646
647 /* with -Xcheck:jni, this provides a JNI function call trace */
648 //opt.optionString = "-verbose:jni";
649 //mOptions.add(opt);
650 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700651
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700652 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
653 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
654 if (strlen(propBuf) > 0) {
655 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
656 strcat(lockProfThresholdBuf, propBuf);
657 opt.optionString = lockProfThresholdBuf;
658 mOptions.add(opt);
659 }
660
Ben Cheng52b0e732009-06-19 13:31:12 -0700661 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
662 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
663 property_get("dalvik.vm.jit.op", propBuf, "");
664 if (strlen(propBuf) > 0) {
665 strcpy(jitOpBuf, "-Xjitop:");
666 strcat(jitOpBuf, propBuf);
667 opt.optionString = jitOpBuf;
668 mOptions.add(opt);
669 }
670
Ben Cheng52b0e732009-06-19 13:31:12 -0700671 /* Force interpreter-only mode for selected methods */
672 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
673 property_get("dalvik.vm.jit.method", propBuf, "");
674 if (strlen(propBuf) > 0) {
675 strcpy(jitMethodBuf, "-Xjitmethod:");
676 strcat(jitMethodBuf, propBuf);
677 opt.optionString = jitMethodBuf;
678 mOptions.add(opt);
679 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 if (executionMode == kEMIntPortable) {
682 opt.optionString = "-Xint:portable";
683 mOptions.add(opt);
684 } else if (executionMode == kEMIntFast) {
685 opt.optionString = "-Xint:fast";
686 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700687 } else if (executionMode == kEMJitCompiler) {
688 opt.optionString = "-Xint:jit";
689 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700691
692 if (checkDexSum) {
693 /* perform additional DEX checksum tests */
694 opt.optionString = "-Xcheckdexsum";
695 mOptions.add(opt);
696 }
697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 if (logStdio) {
699 /* convert stdout/stderr to log messages */
700 opt.optionString = "-Xlog-stdio";
701 mOptions.add(opt);
702 }
703
704 if (enableAssertBuf[4] != '\0') {
705 /* accept "all" to mean "all classes and packages" */
706 if (strcmp(enableAssertBuf+4, "all") == 0)
707 enableAssertBuf[3] = '\0';
Steve Block6215d3f2012-01-04 20:05:49 +0000708 ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 opt.optionString = enableAssertBuf;
710 mOptions.add(opt);
711 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100712 ALOGV("Assertions disabled\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
715 if (jniOptsBuf[10] != '\0') {
Steve Block6215d3f2012-01-04 20:05:49 +0000716 ALOGI("JNI options: '%s'\n", jniOptsBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 opt.optionString = jniOptsBuf;
718 mOptions.add(opt);
719 }
720
721 if (stackTraceFileBuf[0] != '\0') {
722 static const char* stfOptName = "-Xstacktracefile:";
723
724 stackTraceFile = (char*) malloc(strlen(stfOptName) +
725 strlen(stackTraceFileBuf) +1);
726 strcpy(stackTraceFile, stfOptName);
727 strcat(stackTraceFile, stackTraceFileBuf);
728 opt.optionString = stackTraceFile;
729 mOptions.add(opt);
730 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700731
732 /* extra options; parse this late so it overrides others */
733 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
734 parseExtraOpts(extraOptsBuf);
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 /* Set the properties for locale */
737 {
738 char langOption[sizeof("-Duser.language=") + 3];
739 char regionOption[sizeof("-Duser.region=") + 3];
740 strcpy(langOption, "-Duser.language=");
741 strcpy(regionOption, "-Duser.region=");
742 readLocale(langOption, regionOption);
743 opt.extraInfo = NULL;
744 opt.optionString = langOption;
745 mOptions.add(opt);
746 opt.optionString = regionOption;
747 mOptions.add(opt);
748 }
749
750 /*
751 * We don't have /tmp on the device, but we often have an SD card. Apps
752 * shouldn't use this, but some test suites might want to exercise it.
753 */
754 opt.optionString = "-Djava.io.tmpdir=/sdcard";
755 mOptions.add(opt);
756
757 initArgs.version = JNI_VERSION_1_4;
758 initArgs.options = mOptions.editArray();
759 initArgs.nOptions = mOptions.size();
760 initArgs.ignoreUnrecognized = JNI_FALSE;
761
762 /*
763 * Initialize the VM.
764 *
765 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
766 * If this call succeeds, the VM is ready, and we can start issuing
767 * JNI calls.
768 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700769 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000770 ALOGE("JNI_CreateJavaVM failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 goto bail;
772 }
773
Andy McFaddenf70188a2009-03-31 15:52:13 -0700774 result = 0;
775
776bail:
777 free(stackTraceFile);
778 return result;
779}
780
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700781char* AndroidRuntime::toSlashClassName(const char* className)
782{
783 char* result = strdup(className);
784 for (char* cp = result; *cp != '\0'; cp++) {
785 if (*cp == '.') {
786 *cp = '/';
787 }
788 }
789 return result;
790}
791
Andy McFaddenf70188a2009-03-31 15:52:13 -0700792/*
793 * Start the Android runtime. This involves starting the virtual machine
794 * and calling the "static void main(String[] args)" method in the class
795 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700796 *
797 * Passes the main function two arguments, the class name and the specified
798 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700799 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700800void AndroidRuntime::start(const char* className, const char* options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700801{
Steve Block5baa3a62011-12-20 16:23:08 +0000802 ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700803 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700804
Andy McFaddenf70188a2009-03-31 15:52:13 -0700805 blockSigpipe();
806
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700807 /*
808 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700809 * init.rc anymore, so we print out the boot start event here.
810 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700811 if (strcmp(options, "start-system-server") == 0) {
Andy McFaddenf70188a2009-03-31 15:52:13 -0700812 /* track our progress through the boot sequence */
813 const int LOG_BOOT_PROGRESS_START = 3000;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700814 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
Andy McFaddenf70188a2009-03-31 15:52:13 -0700815 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
816 }
817
818 const char* rootDir = getenv("ANDROID_ROOT");
819 if (rootDir == NULL) {
820 rootDir = "/system";
821 if (!hasDir("/system")) {
822 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700823 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700824 }
825 setenv("ANDROID_ROOT", rootDir, 1);
826 }
827
828 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block5baa3a62011-12-20 16:23:08 +0000829 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700830
831 /* start the virtual machine */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700832 JNIEnv* env;
833 if (startVm(&mJavaVM, &env) != 0) {
834 return;
835 }
836 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 /*
839 * Register android functions.
840 */
841 if (startReg(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000842 ALOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700843 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
845
846 /*
847 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700848 * At present we have two arguments, the class name and an option string.
849 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 */
851 jclass stringClass;
852 jobjectArray strArray;
853 jstring classNameStr;
Jeff Brownebed7d62011-05-16 17:08:42 -0700854 jstring optionsStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855
856 stringClass = env->FindClass("java/lang/String");
857 assert(stringClass != NULL);
858 strArray = env->NewObjectArray(2, stringClass, NULL);
859 assert(strArray != NULL);
860 classNameStr = env->NewStringUTF(className);
861 assert(classNameStr != NULL);
862 env->SetObjectArrayElement(strArray, 0, classNameStr);
Jeff Brownebed7d62011-05-16 17:08:42 -0700863 optionsStr = env->NewStringUTF(options);
864 env->SetObjectArrayElement(strArray, 1, optionsStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
866 /*
867 * Start VM. This thread becomes the main thread of the VM, and will
868 * not return until the VM exits.
869 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700870 char* slashClassName = toSlashClassName(className);
871 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 if (startClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000873 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 /* keep going */
875 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700876 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 "([Ljava/lang/String;)V");
878 if (startMeth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000879 ALOGE("JavaVM unable to find main() in '%s'\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 /* keep going */
881 } else {
882 env->CallStaticVoidMethod(startClass, startMeth, strArray);
883
884#if 0
885 if (env->ExceptionCheck())
886 threadExitUncaughtException(env);
887#endif
888 }
889 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700890 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891
Steve Block5baa3a62011-12-20 16:23:08 +0000892 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 if (mJavaVM->DetachCurrentThread() != JNI_OK)
Steve Block8564c8d2012-01-05 23:22:43 +0000894 ALOGW("Warning: unable to detach main thread\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 if (mJavaVM->DestroyJavaVM() != 0)
Steve Block8564c8d2012-01-05 23:22:43 +0000896 ALOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897}
898
Jeff Brown4280c4a2012-03-15 17:48:02 -0700899void AndroidRuntime::exit(int code)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900{
Jeff Brown4280c4a2012-03-15 17:48:02 -0700901 if (mExitWithoutCleanup) {
902 ALOGI("VM exiting with result code %d, cleanup skipped.", code);
903 ::_exit(code);
904 } else {
905 ALOGI("VM exiting with result code %d.", code);
906 onExit(code);
907 ::exit(code);
908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909}
910
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700911void AndroidRuntime::onVmCreated(JNIEnv* env)
912{
913 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
914}
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916/*
917 * Get the JNIEnv pointer for this thread.
918 *
919 * Returns NULL if the slot wasn't allocated or populated.
920 */
921/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
922{
923 JNIEnv* env;
924 JavaVM* vm = AndroidRuntime::getJavaVM();
925 assert(vm != NULL);
926
927 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
928 return NULL;
929 return env;
930}
931
932/*
933 * Makes the current thread visible to the VM.
934 *
935 * The JNIEnv pointer returned is only valid for the current thread, and
936 * thus must be tucked into thread-local storage.
937 */
938static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
939{
940 JavaVMAttachArgs args;
941 JavaVM* vm;
942 jint result;
943
944 vm = AndroidRuntime::getJavaVM();
945 assert(vm != NULL);
946
947 args.version = JNI_VERSION_1_4;
948 args.name = (char*) threadName;
949 args.group = NULL;
950
951 result = vm->AttachCurrentThread(pEnv, (void*) &args);
952 if (result != JNI_OK)
Steve Block6215d3f2012-01-04 20:05:49 +0000953 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954
955 return result;
956}
957
958/*
959 * Detach the current thread from the set visible to the VM.
960 */
961static int javaDetachThread(void)
962{
963 JavaVM* vm;
964 jint result;
965
966 vm = AndroidRuntime::getJavaVM();
967 assert(vm != NULL);
968
969 result = vm->DetachCurrentThread();
970 if (result != JNI_OK)
Steve Block3762c312012-01-06 19:20:56 +0000971 ALOGE("ERROR: thread detach failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 return result;
973}
974
975/*
976 * When starting a native thread that will be visible from the VM, we
977 * bounce through this to get the right attach/detach action.
978 * Note that this function calls free(args)
979 */
980/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
981 void* start = ((void**)args)[0];
982 void* userData = ((void **)args)[1];
983 char* name = (char*) ((void **)args)[2]; // we own this storage
984 free(args);
985 JNIEnv* env;
986 int result;
987
988 /* hook us into the VM */
989 if (javaAttachThread(name, &env) != JNI_OK)
990 return -1;
991
992 /* start the thread running */
993 result = (*(android_thread_func_t)start)(userData);
994
995 /* unhook us */
996 javaDetachThread();
997 free(name);
998
999 return result;
1000}
1001
1002/*
1003 * This is invoked from androidCreateThreadEtc() via the callback
1004 * set with androidSetCreateThreadFunc().
1005 *
1006 * We need to create the new thread in such a way that it gets hooked
1007 * into the VM before it really starts executing.
1008 */
1009/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001010 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 void* userData,
1012 const char* threadName,
1013 int32_t threadPriority,
1014 size_t threadStackSize,
1015 android_thread_id_t* threadId)
1016{
1017 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1018 int result;
1019
1020 assert(threadName != NULL);
1021
1022 args[0] = (void*) entryFunction;
1023 args[1] = userData;
1024 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1025
1026 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1027 threadName, threadPriority, threadStackSize, threadId);
1028 return result;
1029}
1030
1031/*
1032 * Create a thread that is visible from the VM.
1033 *
1034 * This is called from elsewhere in the library.
1035 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001036/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 void (*start)(void *), void* arg)
1038{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001039 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001041 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1042 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043}
1044
1045#if 0
1046static void quickTest(void* arg)
1047{
1048 const char* str = (const char*) arg;
1049
1050 printf("In quickTest: %s\n", str);
1051}
1052#endif
1053
1054#ifdef NDEBUG
1055 #define REG_JNI(name) { name }
1056 struct RegJNIRec {
1057 int (*mProc)(JNIEnv*);
1058 };
1059#else
1060 #define REG_JNI(name) { name, #name }
1061 struct RegJNIRec {
1062 int (*mProc)(JNIEnv*);
1063 const char* mName;
1064 };
1065#endif
1066
1067typedef void (*RegJAMProc)();
1068
1069static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1070{
1071 for (size_t i = 0; i < count; i++) {
1072 if (array[i].mProc(env) < 0) {
1073#ifndef NDEBUG
Steve Block5baa3a62011-12-20 16:23:08 +00001074 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075#endif
1076 return -1;
1077 }
1078 }
1079 return 0;
1080}
1081
1082static void register_jam_procs(const RegJAMProc array[], size_t count)
1083{
1084 for (size_t i = 0; i < count; i++) {
1085 array[i]();
1086 }
1087}
1088
1089static const RegJNIRec gRegJNI[] = {
1090 REG_JNI(register_android_debug_JNITest),
1091 REG_JNI(register_com_android_internal_os_RuntimeInit),
1092 REG_JNI(register_android_os_SystemClock),
1093 REG_JNI(register_android_util_EventLog),
1094 REG_JNI(register_android_util_Log),
1095 REG_JNI(register_android_util_FloatMath),
1096 REG_JNI(register_android_text_format_Time),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 REG_JNI(register_android_content_AssetManager),
1098 REG_JNI(register_android_content_StringBlock),
1099 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001100 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001102 REG_JNI(register_android_text_AndroidBidi),
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001103 REG_JNI(register_android_view_InputDevice),
1104 REG_JNI(register_android_view_KeyCharacterMap),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001106 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 REG_JNI(register_android_os_Binder),
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001108 REG_JNI(register_android_os_Parcel),
Jeff Brown0a0a1242011-12-02 02:25:22 -08001109 REG_JNI(register_android_view_DisplayEventReceiver),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 REG_JNI(register_android_nio_utils),
1111 REG_JNI(register_android_graphics_PixelFormat),
1112 REG_JNI(register_android_graphics_Graphics),
Chet Haasea1cff502012-02-21 13:43:44 -08001113 REG_JNI(register_android_view_GLES20DisplayList),
Romain Guye4d01122010-06-16 18:44:05 -07001114 REG_JNI(register_android_view_GLES20Canvas),
Romain Guya9582652011-11-10 14:20:10 -08001115 REG_JNI(register_android_view_HardwareRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 REG_JNI(register_android_view_Surface),
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001117 REG_JNI(register_android_view_SurfaceControl),
Jeff Brown64a55af2012-08-26 02:47:39 -07001118 REG_JNI(register_android_view_SurfaceSession),
Romain Guy8f0095c2011-05-02 17:24:22 -07001119 REG_JNI(register_android_view_TextureView),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1121 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Thomas Tafertshofer6b1e8382012-07-03 13:37:35 -07001122 REG_JNI(register_android_opengl_jni_EGL14),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001123 REG_JNI(register_android_opengl_jni_GLES10),
1124 REG_JNI(register_android_opengl_jni_GLES10Ext),
1125 REG_JNI(register_android_opengl_jni_GLES11),
1126 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001127 REG_JNI(register_android_opengl_jni_GLES20),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128
1129 REG_JNI(register_android_graphics_Bitmap),
1130 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001131 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 REG_JNI(register_android_graphics_Camera),
1133 REG_JNI(register_android_graphics_Canvas),
1134 REG_JNI(register_android_graphics_ColorFilter),
1135 REG_JNI(register_android_graphics_DrawFilter),
1136 REG_JNI(register_android_graphics_Interpolator),
1137 REG_JNI(register_android_graphics_LayerRasterizer),
1138 REG_JNI(register_android_graphics_MaskFilter),
1139 REG_JNI(register_android_graphics_Matrix),
1140 REG_JNI(register_android_graphics_Movie),
1141 REG_JNI(register_android_graphics_NinePatch),
1142 REG_JNI(register_android_graphics_Paint),
1143 REG_JNI(register_android_graphics_Path),
1144 REG_JNI(register_android_graphics_PathMeasure),
1145 REG_JNI(register_android_graphics_PathEffect),
1146 REG_JNI(register_android_graphics_Picture),
1147 REG_JNI(register_android_graphics_PorterDuff),
1148 REG_JNI(register_android_graphics_Rasterizer),
1149 REG_JNI(register_android_graphics_Region),
1150 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001151 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 REG_JNI(register_android_graphics_Typeface),
1153 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001154 REG_JNI(register_android_graphics_YuvImage),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155
1156 REG_JNI(register_android_database_CursorWindow),
Jeff Browne5360fb2011-10-31 17:48:13 -07001157 REG_JNI(register_android_database_SQLiteConnection),
1158 REG_JNI(register_android_database_SQLiteGlobal),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 REG_JNI(register_android_database_SQLiteDebug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 REG_JNI(register_android_os_FileObserver),
1162 REG_JNI(register_android_os_FileUtils),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001163 REG_JNI(register_android_os_MessageQueue),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 REG_JNI(register_android_os_ParcelFileDescriptor),
Stephen Smalleyc07fca32012-01-13 08:31:39 -05001165 REG_JNI(register_android_os_SELinux),
Jeff Brown481c1572012-03-09 14:41:15 -08001166 REG_JNI(register_android_os_Trace),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 REG_JNI(register_android_os_UEventObserver),
1168 REG_JNI(register_android_net_LocalSocketImpl),
1169 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001170 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 REG_JNI(register_android_net_wifi_WifiManager),
1172 REG_JNI(register_android_os_MemoryFile),
1173 REG_JNI(register_com_android_internal_os_ZygoteInit),
1174 REG_JNI(register_android_hardware_Camera),
1175 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -04001176 REG_JNI(register_android_hardware_SerialPort),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001177 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001178 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001179 REG_JNI(register_android_hardware_UsbRequest),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 REG_JNI(register_android_media_AudioRecord),
1181 REG_JNI(register_android_media_AudioSystem),
1182 REG_JNI(register_android_media_AudioTrack),
1183 REG_JNI(register_android_media_JetPlayer),
Jeff Browncbad9762012-09-04 21:57:59 -07001184 REG_JNI(register_android_media_RemoteDisplay),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 REG_JNI(register_android_media_ToneGenerator),
1186
1187 REG_JNI(register_android_opengl_classes),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001188 REG_JNI(register_android_server_NetworkManagementSocketTagger),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001189 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001191 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001192 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001193 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001194 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001195 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001196 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001197 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001198 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001199 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brownc28867a2013-03-26 15:42:39 -07001200 REG_JNI(register_android_view_InputEventSender),
Michael Wrighta44dd262013-04-10 21:12:00 -07001201 REG_JNI(register_android_view_InputQueue),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001202 REG_JNI(register_android_view_KeyEvent),
1203 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001204 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001205 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001206
1207 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001208 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001209
1210 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001211 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001212 REG_JNI(register_com_android_internal_net_NetworkStatsFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213};
1214
1215/*
1216 * Register android native functions with the VM.
1217 */
1218/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1219{
1220 /*
1221 * This hook causes all future threads created in this process to be
1222 * attached to the JavaVM. (This needs to go away in favor of JNI
1223 * Attach calls.)
1224 */
1225 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1226
Steve Block71f2cf12011-10-20 11:56:00 +01001227 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228
1229 /*
1230 * Every "register" function calls one or more things that return
1231 * a local reference (e.g. FindClass). Because we haven't really
1232 * started the VM yet, they're all getting stored in the base frame
1233 * and never released. Use Push/Pop to manage the storage.
1234 */
1235 env->PushLocalFrame(200);
1236
1237 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1238 env->PopLocalFrame(NULL);
1239 return -1;
1240 }
1241 env->PopLocalFrame(NULL);
1242
1243 //createJavaThread("fubar", quickTest, (void*) "hello");
1244
1245 return 0;
1246}
1247
1248AndroidRuntime* AndroidRuntime::getRuntime()
1249{
1250 return gCurRuntime;
1251}
1252
1253/**
1254 * Used by WithFramework to register native functions.
1255 */
1256extern "C"
1257jint Java_com_android_internal_util_WithFramework_registerNatives(
1258 JNIEnv* env, jclass clazz) {
1259 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1260}
1261
1262/**
1263 * Used by LoadClass to register native functions.
1264 */
1265extern "C"
1266jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1267 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1268}
1269
1270} // namespace android