blob: 274121aeeea8e860d8e2cfaf9a310d167eea8f59 [file] [log] [blame]
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016
17#define LOG_TAG "AndroidRuntime"
18//#define LOG_NDEBUG 0
19
20#include <android_runtime/AndroidRuntime.h>
Mathias Agopian07952722009-05-19 19:08:10 -070021#include <binder/IBinder.h>
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -080022#include <binder/IPCThreadState.h>
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/Log.h>
25#include <utils/misc.h>
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/Parcel.h>
Mathias Agopian8ed6beb2009-06-05 01:26:23 -070027#include <utils/StringArray.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <utils/threads.h>
29#include <cutils/properties.h>
30
31#include <SkGraphics.h>
32#include <SkImageDecoder.h>
Mike Reedfc8db532009-04-27 11:43:30 -040033#include <SkImageRef_GlobalPool.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#include "jni.h"
36#include "JNIHelp.h"
37#include "android_util_Binder.h"
38
39#include <stdio.h>
40#include <signal.h>
41#include <sys/stat.h>
42#include <sys/types.h>
43#include <signal.h>
44#include <dirent.h>
45#include <assert.h>
46
47
48using namespace android;
49
50extern void register_BindTest();
51
52extern int register_android_os_Binder(JNIEnv* env);
53extern int register_android_os_Process(JNIEnv* env);
54extern int register_android_graphics_Bitmap(JNIEnv*);
55extern int register_android_graphics_BitmapFactory(JNIEnv*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080056extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057extern int register_android_graphics_Camera(JNIEnv* env);
58extern int register_android_graphics_Graphics(JNIEnv* env);
59extern int register_android_graphics_Interpolator(JNIEnv* env);
60extern int register_android_graphics_LayerRasterizer(JNIEnv*);
61extern int register_android_graphics_MaskFilter(JNIEnv* env);
62extern int register_android_graphics_Movie(JNIEnv* env);
63extern int register_android_graphics_NinePatch(JNIEnv*);
64extern int register_android_graphics_PathEffect(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065extern int register_android_graphics_Shader(JNIEnv* env);
66extern int register_android_graphics_Typeface(JNIEnv* env);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080067extern int register_android_graphics_YuvImage(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
70extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
Jack Palevich1c4907e2009-04-13 16:22:25 -070071extern int register_android_opengl_jni_GLES10(JNIEnv* env);
72extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
73extern int register_android_opengl_jni_GLES11(JNIEnv* env);
74extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
Jack Palevich560814f2009-11-19 16:34:55 +080075extern int register_android_opengl_jni_GLES20(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77extern int register_android_hardware_Camera(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078extern int register_android_hardware_SensorManager(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050079extern int register_android_hardware_UsbDevice(JNIEnv *env);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050080extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050081extern int register_android_hardware_UsbRequest(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83extern int register_android_media_AudioRecord(JNIEnv *env);
84extern int register_android_media_AudioSystem(JNIEnv *env);
85extern int register_android_media_AudioTrack(JNIEnv *env);
86extern int register_android_media_JetPlayer(JNIEnv *env);
87extern int register_android_media_ToneGenerator(JNIEnv *env);
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089extern int register_android_util_FloatMath(JNIEnv* env);
90
91namespace android {
92
93/*
94 * JNI-based registration functions. Note these are properly contained in
95 * namespace android.
96 */
97extern int register_android_content_AssetManager(JNIEnv* env);
98extern int register_android_util_EventLog(JNIEnv* env);
99extern int register_android_util_Log(JNIEnv* env);
100extern int register_android_content_StringBlock(JNIEnv* env);
101extern int register_android_content_XmlBlock(JNIEnv* env);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700102extern int register_android_emoji_EmojiFactory(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103extern int register_android_graphics_Canvas(JNIEnv* env);
104extern int register_android_graphics_ColorFilter(JNIEnv* env);
105extern int register_android_graphics_DrawFilter(JNIEnv* env);
106extern int register_android_graphics_Matrix(JNIEnv* env);
107extern int register_android_graphics_Paint(JNIEnv* env);
108extern int register_android_graphics_Path(JNIEnv* env);
109extern int register_android_graphics_PathMeasure(JNIEnv* env);
110extern int register_android_graphics_Picture(JNIEnv*);
111extern int register_android_graphics_PorterDuff(JNIEnv* env);
112extern int register_android_graphics_Rasterizer(JNIEnv* env);
Jeff Brownfbf09772011-01-16 14:06:57 -0800113extern int register_android_graphics_Region(JNIEnv* env);
Jamie Gennisaa0ce332011-01-06 17:04:26 -0800114extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115extern int register_android_graphics_Xfermode(JNIEnv* env);
116extern int register_android_graphics_PixelFormat(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117extern int register_android_view_Display(JNIEnv* env);
Jeff Brown0a0a1242011-12-02 02:25:22 -0800118extern int register_android_view_DisplayEventReceiver(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);
Romain Guy8f0095c2011-05-02 17:24:22 -0700122extern int register_android_view_TextureView(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123extern int register_android_database_CursorWindow(JNIEnv* env);
Vasu Nori5a03f362009-10-20 15:16:35 -0700124extern int register_android_database_SQLiteCompiledSql(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125extern int register_android_database_SQLiteDatabase(JNIEnv* env);
126extern int register_android_database_SQLiteDebug(JNIEnv* env);
127extern int register_android_database_SQLiteProgram(JNIEnv* env);
128extern int register_android_database_SQLiteQuery(JNIEnv* env);
129extern int register_android_database_SQLiteStatement(JNIEnv* env);
130extern int register_android_debug_JNITest(JNIEnv* env);
131extern int register_android_nio_utils(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132extern int register_android_text_format_Time(JNIEnv* env);
133extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700134extern int register_android_os_MessageQueue(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);
140extern 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_text_KeyCharacterMap(JNIEnv *env);
151extern int register_android_opengl_classes(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152extern int register_android_bluetooth_HeadsetBase(JNIEnv* env);
153extern int register_android_bluetooth_BluetoothAudioGateway(JNIEnv* env);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700154extern int register_android_bluetooth_BluetoothSocket(JNIEnv *env);
Nick Pellybd022f42009-08-14 18:33:38 -0700155extern int register_android_server_BluetoothService(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156extern int register_android_server_BluetoothEventLoop(JNIEnv *env);
157extern int register_android_server_BluetoothA2dpService(JNIEnv* env);
JP Abgrall98a4f7e2011-09-02 15:36:33 -0700158extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
Christopher Tateecaa7b42010-06-04 14:55:02 -0700159extern int register_android_server_Watchdog(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
161extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700162extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700163extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700164extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400165extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Christopher Tate4a627c72011-04-01 14:43:32 -0700166extern int register_android_app_backup_FullBackup(JNIEnv *env);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700167extern int register_android_app_ActivityThread(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700168extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700169extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800170extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700171extern int register_android_view_KeyEvent(JNIEnv* env);
172extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700173extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700174extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700175extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700176extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700177extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700178extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
180static AndroidRuntime* gCurRuntime = NULL;
181
182static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
183{
184 if (jniThrowException(env, exc, msg) != 0)
185 assert(false);
186}
187
188/*
189 * Code written in the Java Programming Language calls here from main().
190 */
191static void com_android_internal_os_RuntimeInit_finishInit(JNIEnv* env, jobject clazz)
192{
193 gCurRuntime->onStarted();
194}
195
196static void com_android_internal_os_RuntimeInit_zygoteInit(JNIEnv* env, jobject clazz)
197{
198 gCurRuntime->onZygoteInit();
199}
200
201static jint com_android_internal_os_RuntimeInit_isComputerOn(JNIEnv* env, jobject clazz)
202{
203 return 1;
204}
205
206static void com_android_internal_os_RuntimeInit_turnComputerOn(JNIEnv* env, jobject clazz)
207{
208}
209
210static jint com_android_internal_os_RuntimeInit_getQwertyKeyboard(JNIEnv* env, jobject clazz)
211{
212 char* value = getenv("qwerty");
213 if (value != NULL && strcmp(value, "true") == 0) {
214 return 1;
215 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 return 0;
218}
219
220/*
221 * JNI registration.
222 */
223static JNINativeMethod gMethods[] = {
224 { "finishInit", "()V",
225 (void*) com_android_internal_os_RuntimeInit_finishInit },
226 { "zygoteInitNative", "()V",
227 (void*) com_android_internal_os_RuntimeInit_zygoteInit },
228 { "isComputerOn", "()I",
229 (void*) com_android_internal_os_RuntimeInit_isComputerOn },
230 { "turnComputerOn", "()V",
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700231 (void*) com_android_internal_os_RuntimeInit_turnComputerOn },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 { "getQwertyKeyboard", "()I",
233 (void*) com_android_internal_os_RuntimeInit_getQwertyKeyboard },
234};
235
236int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
237{
238 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
239 gMethods, NELEM(gMethods));
240}
241
242// ----------------------------------------------------------------------
243
244/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
245
246
247AndroidRuntime::AndroidRuntime()
248{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700249 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 // this sets our preference for 16bit images during decode
251 // in case the src is opaque and 24bit
252 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400253 // This cache is shared between browser native images, and java "purgeable"
254 // bitmaps. This globalpool is for images that do not either use the java
255 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
256 // java call site.
257 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
258 // There is also a global font cache, but its budget is specified in code
259 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 // Pre-allocate enough space to hold a fair number of options.
262 mOptions.setCapacity(20);
263
264 assert(gCurRuntime == NULL); // one per process
265 gCurRuntime = this;
266}
267
268AndroidRuntime::~AndroidRuntime()
269{
270 SkGraphics::Term();
271}
272
273/*
274 * Register native methods using JNI.
275 */
276/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
277 const char* className, const JNINativeMethod* gMethods, int numMethods)
278{
279 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
280}
281
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700282status_t AndroidRuntime::callMain(const char* className,
283 jclass clazz, int argc, const char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284{
285 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 jmethodID methodId;
287
Steve Block5baa3a62011-12-20 16:23:08 +0000288 ALOGD("Calling main entry %s", className);
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700291 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 return UNKNOWN_ERROR;
293 }
294
295 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
296 if (methodId == NULL) {
297 LOGE("ERROR: could not find method %s.main(String[])\n", className);
298 return UNKNOWN_ERROR;
299 }
300
301 /*
302 * We want to call main() with a String array with our arguments in it.
303 * Create an array and populate it.
304 */
305 jclass stringClass;
306 jobjectArray strArray;
307
308 stringClass = env->FindClass("java/lang/String");
309 strArray = env->NewObjectArray(argc, stringClass, NULL);
310
311 for (int i = 0; i < argc; i++) {
312 jstring argStr = env->NewStringUTF(argv[i]);
313 env->SetObjectArrayElement(strArray, i, argStr);
314 }
315
316 env->CallStaticVoidMethod(clazz, methodId, strArray);
317 return NO_ERROR;
318}
319
320/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 * The VM calls this through the "exit" hook.
322 */
323static void runtime_exit(int code)
324{
325 gCurRuntime->onExit(code);
326 exit(code);
327}
328
329/*
330 * The VM calls this through the "vfprintf" hook.
331 *
332 * We ignore "fp" and just write the results to the log file.
333 */
334static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
335{
336 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
337}
338
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800339/**
340 * The VM calls this when mutex contention debugging is enabled to
341 * determine whether or not the blocked thread was a "sensitive thread"
342 * for user responsiveness/smoothess.
343 *
344 * Our policy for this is whether or not we're tracing any StrictMode
345 * events on this thread (which we might've inherited via Binder calls
346 * into us)
347 */
348static bool runtime_isSensitiveThread() {
349 IPCThreadState* state = IPCThreadState::selfOrNull();
350 return state && state->getStrictModePolicy() != 0;
351}
352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353
354/**
355 * Add VM arguments to the to-be-executed VM
356 * Stops at first non '-' argument (also stops at an argument of '--')
357 * Returns the number of args consumed
358 */
359int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
360{
361 int i;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 for (i = 0; i<argc; i++) {
364 if (argv[i][0] != '-') {
365 return i;
366 }
367 if (argv[i][1] == '-' && argv[i][2] == 0) {
368 return i+1;
369 }
370
371 JavaVMOption opt;
372 memset(&opt, 0, sizeof(opt));
373 opt.optionString = (char*)argv[i];
374 mOptions.add(opt);
375 }
376 return i;
377}
378
379static int hasDir(const char* dir)
380{
381 struct stat s;
382 int res = stat(dir, &s);
383 if (res == 0) {
384 return S_ISDIR(s.st_mode);
385 }
386 return 0;
387}
388
389/*
390 * We just want failed write() calls to just return with an error.
391 */
392static void blockSigpipe()
393{
394 sigset_t mask;
395
396 sigemptyset(&mask);
397 sigaddset(&mask, SIGPIPE);
398 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
399 LOGW("WARNING: SIGPIPE not blocked\n");
400}
401
402/*
403 * Read the persistent locale.
404 */
405static void readLocale(char* language, char* region)
406{
407 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 property_get("persist.sys.language", propLang, "");
410 property_get("persist.sys.country", propRegn, "");
411 if (*propLang == 0 && *propRegn == 0) {
412 /* Set to ro properties, default is en_US */
413 property_get("ro.product.locale.language", propLang, "en");
414 property_get("ro.product.locale.region", propRegn, "US");
415 }
416 strncat(language, propLang, 2);
417 strncat(region, propRegn, 2);
Steve Block5baa3a62011-12-20 16:23:08 +0000418 //ALOGD("language=%s region=%s\n", language, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419}
420
Andy McFaddenf70188a2009-03-31 15:52:13 -0700421/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700422 * Parse a property containing space-separated options that should be
423 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
424 *
425 * This will cut up "extraOptsBuf" as we chop it into individual options.
426 *
427 * Adds the strings, if any, to mOptions.
428 */
429void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
430{
431 JavaVMOption opt;
432 char* start;
433 char* end;
434
435 memset(&opt, 0, sizeof(opt));
436 start = extraOptsBuf;
437 while (*start != '\0') {
438 while (*start == ' ') /* skip leading whitespace */
439 start++;
440 if (*start == '\0') /* was trailing ws, bail */
441 break;
442
443 end = start+1;
444 while (*end != ' ' && *end != '\0') /* find end of token */
445 end++;
446 if (*end == ' ')
447 *end++ = '\0'; /* mark end, advance to indicate more */
448
449 opt.optionString = start;
450 mOptions.add(opt);
451 start = end;
452 }
453}
454
455/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700456 * Start the Dalvik Virtual Machine.
457 *
458 * Various arguments, most determined by system properties, are passed in.
459 * The "mOptions" vector is updated.
460 *
461 * Returns 0 on success.
462 */
463int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700465 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 JavaVMInitArgs initArgs;
467 JavaVMOption opt;
468 char propBuf[PROPERTY_VALUE_MAX];
469 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
470 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
471 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
472 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800473 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700474 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800475 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700476 char extraOptsBuf[PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700479 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700481 enum {
482 kEMDefault,
483 kEMIntPortable,
484 kEMIntFast,
485#if defined(WITH_JIT)
486 kEMJitCompiler,
487#endif
488 } executionMode = kEMDefault;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490
491 property_get("dalvik.vm.checkjni", propBuf, "");
492 if (strcmp(propBuf, "true") == 0) {
493 checkJni = true;
494 } else if (strcmp(propBuf, "false") != 0) {
495 /* property is neither true nor false; fall back on kernel parameter */
496 property_get("ro.kernel.android.checkjni", propBuf, "");
497 if (propBuf[0] == '1') {
498 checkJni = true;
499 }
500 }
501
502 property_get("dalvik.vm.execution-mode", propBuf, "");
503 if (strcmp(propBuf, "int:portable") == 0) {
504 executionMode = kEMIntPortable;
505 } else if (strcmp(propBuf, "int:fast") == 0) {
506 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700507#if defined(WITH_JIT)
508 } else if (strcmp(propBuf, "int:jit") == 0) {
509 executionMode = kEMJitCompiler;
510#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512
513 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
514
Andy McFaddene2b23e12009-04-03 11:09:46 -0700515 property_get("dalvik.vm.check-dex-sum", propBuf, "");
516 if (strcmp(propBuf, "true") == 0) {
517 checkDexSum = true;
518 }
519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 property_get("log.redirect-stdio", propBuf, "");
521 if (strcmp(propBuf, "true") == 0) {
522 logStdio = true;
523 }
524
525 strcpy(enableAssertBuf, "-ea:");
526 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
527
528 strcpy(jniOptsBuf, "-Xjniopts:");
529 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 /* route exit() to our handler */
532 opt.extraInfo = (void*) runtime_exit;
533 opt.optionString = "exit";
534 mOptions.add(opt);
535
536 /* route fprintf() to our handler */
537 opt.extraInfo = (void*) runtime_vfprintf;
538 opt.optionString = "vfprintf";
539 mOptions.add(opt);
540
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800541 /* register the framework-specific "is sensitive thread" hook */
542 opt.extraInfo = (void*) runtime_isSensitiveThread;
543 opt.optionString = "sensitiveThread";
544 mOptions.add(opt);
545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 opt.extraInfo = NULL;
547
548 /* enable verbose; standard options are { jni, gc, class } */
549 //options[curOpt++].optionString = "-verbose:jni";
550 opt.optionString = "-verbose:gc";
551 mOptions.add(opt);
552 //options[curOpt++].optionString = "-verbose:class";
553
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800554 /*
555 * The default starting and maximum size of the heap. Larger
556 * values should be specified in a product property override.
557 */
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800558 strcpy(heapstartsizeOptsBuf, "-Xms");
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800559 property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800560 opt.optionString = heapstartsizeOptsBuf;
561 mOptions.add(opt);
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700562 strcpy(heapsizeOptsBuf, "-Xmx");
563 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700564 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 mOptions.add(opt);
566
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800567 strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
568 property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
569 if (heapgrowthlimitOptsBuf[20] != '\0') {
570 opt.optionString = heapgrowthlimitOptsBuf;
571 mOptions.add(opt);
572 }
573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 /*
575 * Enable or disable dexopt features, such as bytecode verification and
576 * calculation of register maps for precise GC.
577 */
578 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
579 if (dexoptFlagsBuf[0] != '\0') {
580 const char* opc;
581 const char* val;
582
583 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
584 if (opc != NULL) {
585 switch (*(opc+2)) {
586 case 'n': val = "-Xverify:none"; break;
587 case 'r': val = "-Xverify:remote"; break;
588 case 'a': val = "-Xverify:all"; break;
589 default: val = NULL; break;
590 }
591
592 if (val != NULL) {
593 opt.optionString = val;
594 mOptions.add(opt);
595 }
596 }
597
598 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
599 if (opc != NULL) {
600 switch (*(opc+2)) {
601 case 'n': val = "-Xdexopt:none"; break;
602 case 'v': val = "-Xdexopt:verified"; break;
603 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700604 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 default: val = NULL; break;
606 }
607
608 if (val != NULL) {
609 opt.optionString = val;
610 mOptions.add(opt);
611 }
612 }
613
614 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
615 if (opc != NULL) {
616 opt.optionString = "-Xgenregmap";
617 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700618
619 /* turn on precise GC while we're at it */
620 opt.optionString = "-Xgc:precise";
621 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623 }
624
625 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 /* use android ADB transport */
627 opt.optionString =
628 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 mOptions.add(opt);
630
Steve Block5baa3a62011-12-20 16:23:08 +0000631 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 if (checkJni) {
633 /* extended JNI checking */
634 opt.optionString = "-Xcheck:jni";
635 mOptions.add(opt);
636
637 /* set a cap on JNI global references */
638 opt.optionString = "-Xjnigreflimit:2000";
639 mOptions.add(opt);
640
641 /* with -Xcheck:jni, this provides a JNI function call trace */
642 //opt.optionString = "-verbose:jni";
643 //mOptions.add(opt);
644 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700645
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700646 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
647 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
648 if (strlen(propBuf) > 0) {
649 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
650 strcat(lockProfThresholdBuf, propBuf);
651 opt.optionString = lockProfThresholdBuf;
652 mOptions.add(opt);
653 }
654
Ben Cheng52b0e732009-06-19 13:31:12 -0700655#if defined(WITH_JIT)
Ben Cheng52b0e732009-06-19 13:31:12 -0700656 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
657 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
658 property_get("dalvik.vm.jit.op", propBuf, "");
659 if (strlen(propBuf) > 0) {
660 strcpy(jitOpBuf, "-Xjitop:");
661 strcat(jitOpBuf, propBuf);
662 opt.optionString = jitOpBuf;
663 mOptions.add(opt);
664 }
665
Ben Cheng52b0e732009-06-19 13:31:12 -0700666 /* Force interpreter-only mode for selected methods */
667 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
668 property_get("dalvik.vm.jit.method", propBuf, "");
669 if (strlen(propBuf) > 0) {
670 strcpy(jitMethodBuf, "-Xjitmethod:");
671 strcat(jitMethodBuf, propBuf);
672 opt.optionString = jitMethodBuf;
673 mOptions.add(opt);
674 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700675#endif
676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 if (executionMode == kEMIntPortable) {
678 opt.optionString = "-Xint:portable";
679 mOptions.add(opt);
680 } else if (executionMode == kEMIntFast) {
681 opt.optionString = "-Xint:fast";
682 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700683#if defined(WITH_JIT)
684 } else if (executionMode == kEMJitCompiler) {
685 opt.optionString = "-Xint:jit";
686 mOptions.add(opt);
687#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700689
690 if (checkDexSum) {
691 /* perform additional DEX checksum tests */
692 opt.optionString = "-Xcheckdexsum";
693 mOptions.add(opt);
694 }
695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 if (logStdio) {
697 /* convert stdout/stderr to log messages */
698 opt.optionString = "-Xlog-stdio";
699 mOptions.add(opt);
700 }
701
702 if (enableAssertBuf[4] != '\0') {
703 /* accept "all" to mean "all classes and packages" */
704 if (strcmp(enableAssertBuf+4, "all") == 0)
705 enableAssertBuf[3] = '\0';
Steve Block6215d3f2012-01-04 20:05:49 +0000706 ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 opt.optionString = enableAssertBuf;
708 mOptions.add(opt);
709 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100710 ALOGV("Assertions disabled\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
713 if (jniOptsBuf[10] != '\0') {
Steve Block6215d3f2012-01-04 20:05:49 +0000714 ALOGI("JNI options: '%s'\n", jniOptsBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 opt.optionString = jniOptsBuf;
716 mOptions.add(opt);
717 }
718
719 if (stackTraceFileBuf[0] != '\0') {
720 static const char* stfOptName = "-Xstacktracefile:";
721
722 stackTraceFile = (char*) malloc(strlen(stfOptName) +
723 strlen(stackTraceFileBuf) +1);
724 strcpy(stackTraceFile, stfOptName);
725 strcat(stackTraceFile, stackTraceFileBuf);
726 opt.optionString = stackTraceFile;
727 mOptions.add(opt);
728 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700729
730 /* extra options; parse this late so it overrides others */
731 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
732 parseExtraOpts(extraOptsBuf);
733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 /* Set the properties for locale */
735 {
736 char langOption[sizeof("-Duser.language=") + 3];
737 char regionOption[sizeof("-Duser.region=") + 3];
738 strcpy(langOption, "-Duser.language=");
739 strcpy(regionOption, "-Duser.region=");
740 readLocale(langOption, regionOption);
741 opt.extraInfo = NULL;
742 opt.optionString = langOption;
743 mOptions.add(opt);
744 opt.optionString = regionOption;
745 mOptions.add(opt);
746 }
747
748 /*
749 * We don't have /tmp on the device, but we often have an SD card. Apps
750 * shouldn't use this, but some test suites might want to exercise it.
751 */
752 opt.optionString = "-Djava.io.tmpdir=/sdcard";
753 mOptions.add(opt);
754
755 initArgs.version = JNI_VERSION_1_4;
756 initArgs.options = mOptions.editArray();
757 initArgs.nOptions = mOptions.size();
758 initArgs.ignoreUnrecognized = JNI_FALSE;
759
760 /*
761 * Initialize the VM.
762 *
763 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
764 * If this call succeeds, the VM is ready, and we can start issuing
765 * JNI calls.
766 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700767 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 LOGE("JNI_CreateJavaVM failed\n");
769 goto bail;
770 }
771
Andy McFaddenf70188a2009-03-31 15:52:13 -0700772 result = 0;
773
774bail:
775 free(stackTraceFile);
776 return result;
777}
778
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700779char* AndroidRuntime::toSlashClassName(const char* className)
780{
781 char* result = strdup(className);
782 for (char* cp = result; *cp != '\0'; cp++) {
783 if (*cp == '.') {
784 *cp = '/';
785 }
786 }
787 return result;
788}
789
Andy McFaddenf70188a2009-03-31 15:52:13 -0700790/*
791 * Start the Android runtime. This involves starting the virtual machine
792 * and calling the "static void main(String[] args)" method in the class
793 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700794 *
795 * Passes the main function two arguments, the class name and the specified
796 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700797 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700798void AndroidRuntime::start(const char* className, const char* options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700799{
Steve Block5baa3a62011-12-20 16:23:08 +0000800 ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700801 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700802
Andy McFaddenf70188a2009-03-31 15:52:13 -0700803 blockSigpipe();
804
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700805 /*
806 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700807 * init.rc anymore, so we print out the boot start event here.
808 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700809 if (strcmp(options, "start-system-server") == 0) {
Andy McFaddenf70188a2009-03-31 15:52:13 -0700810 /* track our progress through the boot sequence */
811 const int LOG_BOOT_PROGRESS_START = 3000;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700812 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
Andy McFaddenf70188a2009-03-31 15:52:13 -0700813 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
814 }
815
816 const char* rootDir = getenv("ANDROID_ROOT");
817 if (rootDir == NULL) {
818 rootDir = "/system";
819 if (!hasDir("/system")) {
820 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700821 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700822 }
823 setenv("ANDROID_ROOT", rootDir, 1);
824 }
825
826 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block5baa3a62011-12-20 16:23:08 +0000827 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700828
829 /* start the virtual machine */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700830 JNIEnv* env;
831 if (startVm(&mJavaVM, &env) != 0) {
832 return;
833 }
834 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 /*
837 * Register android functions.
838 */
839 if (startReg(env) < 0) {
840 LOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700841 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843
844 /*
845 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700846 * At present we have two arguments, the class name and an option string.
847 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 */
849 jclass stringClass;
850 jobjectArray strArray;
851 jstring classNameStr;
Jeff Brownebed7d62011-05-16 17:08:42 -0700852 jstring optionsStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853
854 stringClass = env->FindClass("java/lang/String");
855 assert(stringClass != NULL);
856 strArray = env->NewObjectArray(2, stringClass, NULL);
857 assert(strArray != NULL);
858 classNameStr = env->NewStringUTF(className);
859 assert(classNameStr != NULL);
860 env->SetObjectArrayElement(strArray, 0, classNameStr);
Jeff Brownebed7d62011-05-16 17:08:42 -0700861 optionsStr = env->NewStringUTF(options);
862 env->SetObjectArrayElement(strArray, 1, optionsStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863
864 /*
865 * Start VM. This thread becomes the main thread of the VM, and will
866 * not return until the VM exits.
867 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700868 char* slashClassName = toSlashClassName(className);
869 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 if (startClass == NULL) {
871 LOGE("JavaVM unable to locate class '%s'\n", slashClassName);
872 /* keep going */
873 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700874 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 "([Ljava/lang/String;)V");
876 if (startMeth == NULL) {
877 LOGE("JavaVM unable to find main() in '%s'\n", className);
878 /* keep going */
879 } else {
880 env->CallStaticVoidMethod(startClass, startMeth, strArray);
881
882#if 0
883 if (env->ExceptionCheck())
884 threadExitUncaughtException(env);
885#endif
886 }
887 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700888 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889
Steve Block5baa3a62011-12-20 16:23:08 +0000890 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 if (mJavaVM->DetachCurrentThread() != JNI_OK)
892 LOGW("Warning: unable to detach main thread\n");
893 if (mJavaVM->DestroyJavaVM() != 0)
894 LOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895}
896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897void AndroidRuntime::onExit(int code)
898{
Steve Block71f2cf12011-10-20 11:56:00 +0100899 ALOGV("AndroidRuntime onExit calling exit(%d)", code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 exit(code);
901}
902
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700903void AndroidRuntime::onVmCreated(JNIEnv* env)
904{
905 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
906}
907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908/*
909 * Get the JNIEnv pointer for this thread.
910 *
911 * Returns NULL if the slot wasn't allocated or populated.
912 */
913/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
914{
915 JNIEnv* env;
916 JavaVM* vm = AndroidRuntime::getJavaVM();
917 assert(vm != NULL);
918
919 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
920 return NULL;
921 return env;
922}
923
924/*
925 * Makes the current thread visible to the VM.
926 *
927 * The JNIEnv pointer returned is only valid for the current thread, and
928 * thus must be tucked into thread-local storage.
929 */
930static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
931{
932 JavaVMAttachArgs args;
933 JavaVM* vm;
934 jint result;
935
936 vm = AndroidRuntime::getJavaVM();
937 assert(vm != NULL);
938
939 args.version = JNI_VERSION_1_4;
940 args.name = (char*) threadName;
941 args.group = NULL;
942
943 result = vm->AttachCurrentThread(pEnv, (void*) &args);
944 if (result != JNI_OK)
Steve Block6215d3f2012-01-04 20:05:49 +0000945 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946
947 return result;
948}
949
950/*
951 * Detach the current thread from the set visible to the VM.
952 */
953static int javaDetachThread(void)
954{
955 JavaVM* vm;
956 jint result;
957
958 vm = AndroidRuntime::getJavaVM();
959 assert(vm != NULL);
960
961 result = vm->DetachCurrentThread();
962 if (result != JNI_OK)
963 LOGE("ERROR: thread detach failed\n");
964 return result;
965}
966
967/*
968 * When starting a native thread that will be visible from the VM, we
969 * bounce through this to get the right attach/detach action.
970 * Note that this function calls free(args)
971 */
972/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
973 void* start = ((void**)args)[0];
974 void* userData = ((void **)args)[1];
975 char* name = (char*) ((void **)args)[2]; // we own this storage
976 free(args);
977 JNIEnv* env;
978 int result;
979
980 /* hook us into the VM */
981 if (javaAttachThread(name, &env) != JNI_OK)
982 return -1;
983
984 /* start the thread running */
985 result = (*(android_thread_func_t)start)(userData);
986
987 /* unhook us */
988 javaDetachThread();
989 free(name);
990
991 return result;
992}
993
994/*
995 * This is invoked from androidCreateThreadEtc() via the callback
996 * set with androidSetCreateThreadFunc().
997 *
998 * We need to create the new thread in such a way that it gets hooked
999 * into the VM before it really starts executing.
1000 */
1001/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001002 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 void* userData,
1004 const char* threadName,
1005 int32_t threadPriority,
1006 size_t threadStackSize,
1007 android_thread_id_t* threadId)
1008{
1009 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1010 int result;
1011
1012 assert(threadName != NULL);
1013
1014 args[0] = (void*) entryFunction;
1015 args[1] = userData;
1016 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1017
1018 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1019 threadName, threadPriority, threadStackSize, threadId);
1020 return result;
1021}
1022
1023/*
1024 * Create a thread that is visible from the VM.
1025 *
1026 * This is called from elsewhere in the library.
1027 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001028/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 void (*start)(void *), void* arg)
1030{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001031 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001033 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1034 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035}
1036
1037#if 0
1038static void quickTest(void* arg)
1039{
1040 const char* str = (const char*) arg;
1041
1042 printf("In quickTest: %s\n", str);
1043}
1044#endif
1045
1046#ifdef NDEBUG
1047 #define REG_JNI(name) { name }
1048 struct RegJNIRec {
1049 int (*mProc)(JNIEnv*);
1050 };
1051#else
1052 #define REG_JNI(name) { name, #name }
1053 struct RegJNIRec {
1054 int (*mProc)(JNIEnv*);
1055 const char* mName;
1056 };
1057#endif
1058
1059typedef void (*RegJAMProc)();
1060
1061static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1062{
1063 for (size_t i = 0; i < count; i++) {
1064 if (array[i].mProc(env) < 0) {
1065#ifndef NDEBUG
Steve Block5baa3a62011-12-20 16:23:08 +00001066 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067#endif
1068 return -1;
1069 }
1070 }
1071 return 0;
1072}
1073
1074static void register_jam_procs(const RegJAMProc array[], size_t count)
1075{
1076 for (size_t i = 0; i < count; i++) {
1077 array[i]();
1078 }
1079}
1080
1081static const RegJNIRec gRegJNI[] = {
1082 REG_JNI(register_android_debug_JNITest),
1083 REG_JNI(register_com_android_internal_os_RuntimeInit),
1084 REG_JNI(register_android_os_SystemClock),
1085 REG_JNI(register_android_util_EventLog),
1086 REG_JNI(register_android_util_Log),
1087 REG_JNI(register_android_util_FloatMath),
1088 REG_JNI(register_android_text_format_Time),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 REG_JNI(register_android_content_AssetManager),
1090 REG_JNI(register_android_content_StringBlock),
1091 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001092 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001094 REG_JNI(register_android_text_AndroidBidi),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 REG_JNI(register_android_text_KeyCharacterMap),
1096 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001097 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 REG_JNI(register_android_os_Binder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 REG_JNI(register_android_view_Display),
Jeff Brown0a0a1242011-12-02 02:25:22 -08001100 REG_JNI(register_android_view_DisplayEventReceiver),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 REG_JNI(register_android_nio_utils),
1102 REG_JNI(register_android_graphics_PixelFormat),
1103 REG_JNI(register_android_graphics_Graphics),
Romain Guye4d01122010-06-16 18:44:05 -07001104 REG_JNI(register_android_view_GLES20Canvas),
Romain Guya9582652011-11-10 14:20:10 -08001105 REG_JNI(register_android_view_HardwareRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 REG_JNI(register_android_view_Surface),
Romain Guy8f0095c2011-05-02 17:24:22 -07001107 REG_JNI(register_android_view_TextureView),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1109 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001110 REG_JNI(register_android_opengl_jni_GLES10),
1111 REG_JNI(register_android_opengl_jni_GLES10Ext),
1112 REG_JNI(register_android_opengl_jni_GLES11),
1113 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001114 REG_JNI(register_android_opengl_jni_GLES20),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115
1116 REG_JNI(register_android_graphics_Bitmap),
1117 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001118 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 REG_JNI(register_android_graphics_Camera),
1120 REG_JNI(register_android_graphics_Canvas),
1121 REG_JNI(register_android_graphics_ColorFilter),
1122 REG_JNI(register_android_graphics_DrawFilter),
1123 REG_JNI(register_android_graphics_Interpolator),
1124 REG_JNI(register_android_graphics_LayerRasterizer),
1125 REG_JNI(register_android_graphics_MaskFilter),
1126 REG_JNI(register_android_graphics_Matrix),
1127 REG_JNI(register_android_graphics_Movie),
1128 REG_JNI(register_android_graphics_NinePatch),
1129 REG_JNI(register_android_graphics_Paint),
1130 REG_JNI(register_android_graphics_Path),
1131 REG_JNI(register_android_graphics_PathMeasure),
1132 REG_JNI(register_android_graphics_PathEffect),
1133 REG_JNI(register_android_graphics_Picture),
1134 REG_JNI(register_android_graphics_PorterDuff),
1135 REG_JNI(register_android_graphics_Rasterizer),
1136 REG_JNI(register_android_graphics_Region),
1137 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001138 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 REG_JNI(register_android_graphics_Typeface),
1140 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001141 REG_JNI(register_android_graphics_YuvImage),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142
1143 REG_JNI(register_android_database_CursorWindow),
Vasu Nori5a03f362009-10-20 15:16:35 -07001144 REG_JNI(register_android_database_SQLiteCompiledSql),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 REG_JNI(register_android_database_SQLiteDatabase),
1146 REG_JNI(register_android_database_SQLiteDebug),
1147 REG_JNI(register_android_database_SQLiteProgram),
1148 REG_JNI(register_android_database_SQLiteQuery),
1149 REG_JNI(register_android_database_SQLiteStatement),
1150 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 REG_JNI(register_android_os_FileObserver),
1152 REG_JNI(register_android_os_FileUtils),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001153 REG_JNI(register_android_os_MessageQueue),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 REG_JNI(register_android_os_ParcelFileDescriptor),
1155 REG_JNI(register_android_os_Power),
1156 REG_JNI(register_android_os_StatFs),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 REG_JNI(register_android_os_UEventObserver),
1158 REG_JNI(register_android_net_LocalSocketImpl),
1159 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001160 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 REG_JNI(register_android_net_wifi_WifiManager),
1162 REG_JNI(register_android_os_MemoryFile),
1163 REG_JNI(register_com_android_internal_os_ZygoteInit),
1164 REG_JNI(register_android_hardware_Camera),
1165 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001166 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001167 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001168 REG_JNI(register_android_hardware_UsbRequest),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 REG_JNI(register_android_media_AudioRecord),
1170 REG_JNI(register_android_media_AudioSystem),
1171 REG_JNI(register_android_media_AudioTrack),
1172 REG_JNI(register_android_media_JetPlayer),
1173 REG_JNI(register_android_media_ToneGenerator),
1174
1175 REG_JNI(register_android_opengl_classes),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 REG_JNI(register_android_bluetooth_HeadsetBase),
1177 REG_JNI(register_android_bluetooth_BluetoothAudioGateway),
Nick Pelly0b6955a2009-05-26 19:13:43 -07001178 REG_JNI(register_android_bluetooth_BluetoothSocket),
Nick Pellybd022f42009-08-14 18:33:38 -07001179 REG_JNI(register_android_server_BluetoothService),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 REG_JNI(register_android_server_BluetoothEventLoop),
1181 REG_JNI(register_android_server_BluetoothA2dpService),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001182 REG_JNI(register_android_server_NetworkManagementSocketTagger),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001183 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001185 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001186 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001187 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001188 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001189 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001190 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001191 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001192 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001193 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001194 REG_JNI(register_android_view_KeyEvent),
1195 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001196 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001197 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001198
1199 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001200 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001201
1202 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001203 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204};
1205
1206/*
1207 * Register android native functions with the VM.
1208 */
1209/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1210{
1211 /*
1212 * This hook causes all future threads created in this process to be
1213 * attached to the JavaVM. (This needs to go away in favor of JNI
1214 * Attach calls.)
1215 */
1216 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1217
Steve Block71f2cf12011-10-20 11:56:00 +01001218 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219
1220 /*
1221 * Every "register" function calls one or more things that return
1222 * a local reference (e.g. FindClass). Because we haven't really
1223 * started the VM yet, they're all getting stored in the base frame
1224 * and never released. Use Push/Pop to manage the storage.
1225 */
1226 env->PushLocalFrame(200);
1227
1228 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1229 env->PopLocalFrame(NULL);
1230 return -1;
1231 }
1232 env->PopLocalFrame(NULL);
1233
1234 //createJavaThread("fubar", quickTest, (void*) "hello");
1235
1236 return 0;
1237}
1238
1239AndroidRuntime* AndroidRuntime::getRuntime()
1240{
1241 return gCurRuntime;
1242}
1243
1244/**
1245 * Used by WithFramework to register native functions.
1246 */
1247extern "C"
1248jint Java_com_android_internal_util_WithFramework_registerNatives(
1249 JNIEnv* env, jclass clazz) {
1250 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1251}
1252
1253/**
1254 * Used by LoadClass to register native functions.
1255 */
1256extern "C"
1257jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1258 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1259}
1260
1261} // namespace android