blob: cf766aafa0f1cadfc55d1f61224734eb74ea2e13 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/AndroidRuntime.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "AndroidRuntime"
19//#define LOG_NDEBUG 0
20
21#include <android_runtime/AndroidRuntime.h>
Mathias Agopian07952722009-05-19 19:08:10 -070022#include <binder/IBinder.h>
23#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*);
56extern int register_android_graphics_Camera(JNIEnv* env);
57extern int register_android_graphics_Graphics(JNIEnv* env);
58extern int register_android_graphics_Interpolator(JNIEnv* env);
59extern int register_android_graphics_LayerRasterizer(JNIEnv*);
60extern int register_android_graphics_MaskFilter(JNIEnv* env);
61extern int register_android_graphics_Movie(JNIEnv* env);
62extern int register_android_graphics_NinePatch(JNIEnv*);
63extern int register_android_graphics_PathEffect(JNIEnv* env);
64extern int register_android_graphics_Region(JNIEnv* env);
65extern 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);
78
79extern int register_android_hardware_SensorManager(JNIEnv *env);
80
81extern int register_android_media_AudioRecord(JNIEnv *env);
82extern int register_android_media_AudioSystem(JNIEnv *env);
83extern int register_android_media_AudioTrack(JNIEnv *env);
84extern int register_android_media_JetPlayer(JNIEnv *env);
85extern int register_android_media_ToneGenerator(JNIEnv *env);
86
87extern int register_android_message_digest_sha1(JNIEnv *env);
88
89extern 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);
113extern int register_android_graphics_Xfermode(JNIEnv* env);
114extern int register_android_graphics_PixelFormat(JNIEnv* env);
115extern int register_com_android_internal_graphics_NativeUtils(JNIEnv *env);
116extern int register_android_view_Display(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700117extern int register_android_view_GLES20Canvas(JNIEnv* env);
Romain Guyc6133282010-06-10 16:17:24 -0700118extern int register_android_view_HardwareRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119extern int register_android_view_Surface(JNIEnv* env);
120extern int register_android_view_ViewRoot(JNIEnv* env);
121extern int register_android_database_CursorWindow(JNIEnv* env);
Vasu Nori5a03f362009-10-20 15:16:35 -0700122extern int register_android_database_SQLiteCompiledSql(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123extern int register_android_database_SQLiteDatabase(JNIEnv* env);
124extern int register_android_database_SQLiteDebug(JNIEnv* env);
125extern int register_android_database_SQLiteProgram(JNIEnv* env);
126extern int register_android_database_SQLiteQuery(JNIEnv* env);
127extern int register_android_database_SQLiteStatement(JNIEnv* env);
128extern int register_android_debug_JNITest(JNIEnv* env);
Mike Lockwood755fd612010-05-25 19:08:48 -0400129extern int register_android_media_MtpClient(JNIEnv *env);
130extern int register_android_media_MtpCursor(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131extern int register_android_nio_utils(JNIEnv* env);
132extern int register_android_pim_EventRecurrence(JNIEnv* env);
133extern int register_android_text_format_Time(JNIEnv* env);
134extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700135extern int register_android_os_MessageQueue(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136extern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
137extern int register_android_os_Power(JNIEnv *env);
138extern int register_android_os_StatFs(JNIEnv *env);
139extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140extern int register_android_os_SystemClock(JNIEnv* env);
141extern int register_android_os_FileObserver(JNIEnv *env);
142extern int register_android_os_FileUtils(JNIEnv *env);
143extern int register_android_os_UEventObserver(JNIEnv* env);
144extern int register_android_os_MemoryFile(JNIEnv* env);
145extern int register_android_net_LocalSocketImpl(JNIEnv* env);
146extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700147extern int register_android_net_TrafficStats(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148extern int register_android_net_wifi_WifiManager(JNIEnv* env);
149extern int register_android_security_Md5MessageDigest(JNIEnv *env);
150extern int register_android_text_AndroidCharacter(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800151extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152extern int register_android_text_KeyCharacterMap(JNIEnv *env);
153extern int register_android_opengl_classes(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154extern int register_android_bluetooth_HeadsetBase(JNIEnv* env);
155extern int register_android_bluetooth_BluetoothAudioGateway(JNIEnv* env);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700156extern int register_android_bluetooth_BluetoothSocket(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157extern int register_android_bluetooth_ScoSocket(JNIEnv *env);
Nick Pellybd022f42009-08-14 18:33:38 -0700158extern int register_android_server_BluetoothService(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159extern int register_android_server_BluetoothEventLoop(JNIEnv *env);
160extern int register_android_server_BluetoothA2dpService(JNIEnv* env);
Christopher Tateecaa7b42010-06-04 14:55:02 -0700161extern int register_android_server_Watchdog(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
163extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700164extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700165extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700166extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400167extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700168extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700169extern int register_android_view_InputChannel(JNIEnv* env);
170extern int register_android_view_InputQueue(JNIEnv* env);
171extern int register_android_view_InputTarget(JNIEnv* env);
172extern int register_android_view_KeyEvent(JNIEnv* env);
173extern int register_android_view_MotionEvent(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
175static AndroidRuntime* gCurRuntime = NULL;
176
177static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
178{
179 if (jniThrowException(env, exc, msg) != 0)
180 assert(false);
181}
182
183/*
184 * Code written in the Java Programming Language calls here from main().
185 */
186static void com_android_internal_os_RuntimeInit_finishInit(JNIEnv* env, jobject clazz)
187{
188 gCurRuntime->onStarted();
189}
190
191static void com_android_internal_os_RuntimeInit_zygoteInit(JNIEnv* env, jobject clazz)
192{
193 gCurRuntime->onZygoteInit();
194}
195
196static jint com_android_internal_os_RuntimeInit_isComputerOn(JNIEnv* env, jobject clazz)
197{
198 return 1;
199}
200
201static void com_android_internal_os_RuntimeInit_turnComputerOn(JNIEnv* env, jobject clazz)
202{
203}
204
205static jint com_android_internal_os_RuntimeInit_getQwertyKeyboard(JNIEnv* env, jobject clazz)
206{
207 char* value = getenv("qwerty");
208 if (value != NULL && strcmp(value, "true") == 0) {
209 return 1;
210 }
211
212 return 0;
213}
214
215/*
216 * JNI registration.
217 */
218static JNINativeMethod gMethods[] = {
219 { "finishInit", "()V",
220 (void*) com_android_internal_os_RuntimeInit_finishInit },
221 { "zygoteInitNative", "()V",
222 (void*) com_android_internal_os_RuntimeInit_zygoteInit },
223 { "isComputerOn", "()I",
224 (void*) com_android_internal_os_RuntimeInit_isComputerOn },
225 { "turnComputerOn", "()V",
226 (void*) com_android_internal_os_RuntimeInit_turnComputerOn },
227 { "getQwertyKeyboard", "()I",
228 (void*) com_android_internal_os_RuntimeInit_getQwertyKeyboard },
229};
230
231int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
232{
233 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
234 gMethods, NELEM(gMethods));
235}
236
237// ----------------------------------------------------------------------
238
239/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
240
241
242AndroidRuntime::AndroidRuntime()
243{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700244 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 // this sets our preference for 16bit images during decode
246 // in case the src is opaque and 24bit
247 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400248 // This cache is shared between browser native images, and java "purgeable"
249 // bitmaps. This globalpool is for images that do not either use the java
250 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
251 // java call site.
252 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
253 // There is also a global font cache, but its budget is specified in code
254 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
256 // Pre-allocate enough space to hold a fair number of options.
257 mOptions.setCapacity(20);
258
259 assert(gCurRuntime == NULL); // one per process
260 gCurRuntime = this;
261}
262
263AndroidRuntime::~AndroidRuntime()
264{
265 SkGraphics::Term();
266}
267
268/*
269 * Register native methods using JNI.
270 */
271/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
272 const char* className, const JNINativeMethod* gMethods, int numMethods)
273{
274 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
275}
276
277/*
278 * Call a static Java Programming Language function that takes no arguments and returns void.
279 */
280status_t AndroidRuntime::callStatic(const char* className, const char* methodName)
281{
282 JNIEnv* env;
283 jclass clazz;
284 jmethodID methodId;
285
286 env = getJNIEnv();
287 if (env == NULL)
288 return UNKNOWN_ERROR;
289
290 clazz = findClass(env, className);
291 if (clazz == NULL) {
292 LOGE("ERROR: could not find class '%s'\n", className);
293 return UNKNOWN_ERROR;
294 }
295 methodId = env->GetStaticMethodID(clazz, methodName, "()V");
296 if (methodId == NULL) {
297 LOGE("ERROR: could not find method %s.%s\n", className, methodName);
298 return UNKNOWN_ERROR;
299 }
300
301 env->CallStaticVoidMethod(clazz, methodId);
302
303 return NO_ERROR;
304}
305
306status_t AndroidRuntime::callMain(
307 const char* className, int argc, const char* const argv[])
308{
309 JNIEnv* env;
310 jclass clazz;
311 jmethodID methodId;
312
313 env = getJNIEnv();
314 if (env == NULL)
315 return UNKNOWN_ERROR;
316
317 clazz = findClass(env, className);
318 if (clazz == NULL) {
319 LOGE("ERROR: could not find class '%s'\n", className);
320 return UNKNOWN_ERROR;
321 }
322
323 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
324 if (methodId == NULL) {
325 LOGE("ERROR: could not find method %s.main(String[])\n", className);
326 return UNKNOWN_ERROR;
327 }
328
329 /*
330 * We want to call main() with a String array with our arguments in it.
331 * Create an array and populate it.
332 */
333 jclass stringClass;
334 jobjectArray strArray;
335
336 stringClass = env->FindClass("java/lang/String");
337 strArray = env->NewObjectArray(argc, stringClass, NULL);
338
339 for (int i = 0; i < argc; i++) {
340 jstring argStr = env->NewStringUTF(argv[i]);
341 env->SetObjectArrayElement(strArray, i, argStr);
342 }
343
344 env->CallStaticVoidMethod(clazz, methodId, strArray);
345 return NO_ERROR;
346}
347
348/*
349 * Find the named class.
350 */
351jclass AndroidRuntime::findClass(JNIEnv* env, const char* className)
352{
353 char* convName = NULL;
354
355 if (env->ExceptionCheck()) {
356 LOGE("ERROR: exception pending on entry to findClass()\n");
357 return NULL;
358 }
359
360 /*
361 * JNI FindClass uses class names with slashes, but ClassLoader.loadClass
362 * uses the dotted "binary name" format. We don't need to convert the
363 * name with the new approach.
364 */
365#if 0
366 /* (convName only created if necessary -- use className) */
367 for (char* cp = const_cast<char*>(className); *cp != '\0'; cp++) {
368 if (*cp == '.') {
369 if (convName == NULL) {
370 convName = strdup(className);
371 cp = convName + (cp-className);
372 className = convName;
373 }
374 *cp = '/';
375 }
376 }
377#endif
378
379 /*
380 * This is a little awkward because the JNI FindClass call uses the
381 * class loader associated with the native method we're executing in.
382 * Because this native method is part of a "boot" class, JNI doesn't
383 * look for the class in CLASSPATH, which unfortunately is a likely
384 * location for it. (Had we issued the FindClass call before calling
385 * into the VM -- at which point there isn't a native method frame on
386 * the stack -- the VM would have checked CLASSPATH. We have to do
387 * this because we call into Java Programming Language code and
388 * bounce back out.)
389 *
390 * JNI lacks a "find class in a specific class loader" operation, so we
391 * have to do things the hard way.
392 */
393 jclass cls = NULL;
394 //cls = env->FindClass(className);
395
396 jclass javaLangClassLoader;
397 jmethodID getSystemClassLoader, loadClass;
398 jobject systemClassLoader;
399 jstring strClassName;
400
401 /* find the "system" class loader; none of this is expected to fail */
402 javaLangClassLoader = env->FindClass("java/lang/ClassLoader");
403 assert(javaLangClassLoader != NULL);
404 getSystemClassLoader = env->GetStaticMethodID(javaLangClassLoader,
405 "getSystemClassLoader", "()Ljava/lang/ClassLoader;");
406 loadClass = env->GetMethodID(javaLangClassLoader,
407 "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
408 assert(getSystemClassLoader != NULL && loadClass != NULL);
409 systemClassLoader = env->CallStaticObjectMethod(javaLangClassLoader,
410 getSystemClassLoader);
411 assert(systemClassLoader != NULL);
412
413 /* create an object for the class name string; alloc could fail */
414 strClassName = env->NewStringUTF(className);
415 if (env->ExceptionCheck()) {
416 LOGE("ERROR: unable to convert '%s' to string\n", className);
417 goto bail;
418 }
419 LOGV("system class loader is %p, loading %p (%s)\n",
420 systemClassLoader, strClassName, className);
421
422 /* try to find the named class */
423 cls = (jclass) env->CallObjectMethod(systemClassLoader, loadClass,
424 strClassName);
425 if (env->ExceptionCheck()) {
426 LOGE("ERROR: unable to load class '%s' from %p\n",
427 className, systemClassLoader);
428 cls = NULL;
429 goto bail;
430 }
431
432bail:
433 free(convName);
434 return cls;
435}
436
437/*
438 * The VM calls this through the "exit" hook.
439 */
440static void runtime_exit(int code)
441{
442 gCurRuntime->onExit(code);
443 exit(code);
444}
445
446/*
447 * The VM calls this through the "vfprintf" hook.
448 *
449 * We ignore "fp" and just write the results to the log file.
450 */
451static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
452{
453 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
454}
455
456
457/**
458 * Add VM arguments to the to-be-executed VM
459 * Stops at first non '-' argument (also stops at an argument of '--')
460 * Returns the number of args consumed
461 */
462int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
463{
464 int i;
465
466 for (i = 0; i<argc; i++) {
467 if (argv[i][0] != '-') {
468 return i;
469 }
470 if (argv[i][1] == '-' && argv[i][2] == 0) {
471 return i+1;
472 }
473
474 JavaVMOption opt;
475 memset(&opt, 0, sizeof(opt));
476 opt.optionString = (char*)argv[i];
477 mOptions.add(opt);
478 }
479 return i;
480}
481
482static int hasDir(const char* dir)
483{
484 struct stat s;
485 int res = stat(dir, &s);
486 if (res == 0) {
487 return S_ISDIR(s.st_mode);
488 }
489 return 0;
490}
491
492/*
493 * We just want failed write() calls to just return with an error.
494 */
495static void blockSigpipe()
496{
497 sigset_t mask;
498
499 sigemptyset(&mask);
500 sigaddset(&mask, SIGPIPE);
501 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
502 LOGW("WARNING: SIGPIPE not blocked\n");
503}
504
505/*
506 * Read the persistent locale.
507 */
508static void readLocale(char* language, char* region)
509{
510 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
511
512 property_get("persist.sys.language", propLang, "");
513 property_get("persist.sys.country", propRegn, "");
514 if (*propLang == 0 && *propRegn == 0) {
515 /* Set to ro properties, default is en_US */
516 property_get("ro.product.locale.language", propLang, "en");
517 property_get("ro.product.locale.region", propRegn, "US");
518 }
519 strncat(language, propLang, 2);
520 strncat(region, propRegn, 2);
521 //LOGD("language=%s region=%s\n", language, region);
522}
523
Andy McFaddenf70188a2009-03-31 15:52:13 -0700524/*
525 * Start the Dalvik Virtual Machine.
526 *
527 * Various arguments, most determined by system properties, are passed in.
528 * The "mOptions" vector is updated.
529 *
530 * Returns 0 on success.
531 */
532int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700534 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 JavaVMInitArgs initArgs;
536 JavaVMOption opt;
537 char propBuf[PROPERTY_VALUE_MAX];
538 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
539 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
540 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
541 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700542 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700545 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700547 enum {
548 kEMDefault,
549 kEMIntPortable,
550 kEMIntFast,
551#if defined(WITH_JIT)
552 kEMJitCompiler,
553#endif
554 } executionMode = kEMDefault;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556
557 property_get("dalvik.vm.checkjni", propBuf, "");
558 if (strcmp(propBuf, "true") == 0) {
559 checkJni = true;
560 } else if (strcmp(propBuf, "false") != 0) {
561 /* property is neither true nor false; fall back on kernel parameter */
562 property_get("ro.kernel.android.checkjni", propBuf, "");
563 if (propBuf[0] == '1') {
564 checkJni = true;
565 }
566 }
567
568 property_get("dalvik.vm.execution-mode", propBuf, "");
569 if (strcmp(propBuf, "int:portable") == 0) {
570 executionMode = kEMIntPortable;
571 } else if (strcmp(propBuf, "int:fast") == 0) {
572 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700573#if defined(WITH_JIT)
574 } else if (strcmp(propBuf, "int:jit") == 0) {
575 executionMode = kEMJitCompiler;
576#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578
579 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
580
Andy McFaddene2b23e12009-04-03 11:09:46 -0700581 property_get("dalvik.vm.check-dex-sum", propBuf, "");
582 if (strcmp(propBuf, "true") == 0) {
583 checkDexSum = true;
584 }
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 property_get("log.redirect-stdio", propBuf, "");
587 if (strcmp(propBuf, "true") == 0) {
588 logStdio = true;
589 }
590
591 strcpy(enableAssertBuf, "-ea:");
592 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
593
594 strcpy(jniOptsBuf, "-Xjniopts:");
595 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 /* route exit() to our handler */
598 opt.extraInfo = (void*) runtime_exit;
599 opt.optionString = "exit";
600 mOptions.add(opt);
601
602 /* route fprintf() to our handler */
603 opt.extraInfo = (void*) runtime_vfprintf;
604 opt.optionString = "vfprintf";
605 mOptions.add(opt);
606
607 opt.extraInfo = NULL;
608
609 /* enable verbose; standard options are { jni, gc, class } */
610 //options[curOpt++].optionString = "-verbose:jni";
611 opt.optionString = "-verbose:gc";
612 mOptions.add(opt);
613 //options[curOpt++].optionString = "-verbose:class";
614
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700615 strcpy(heapsizeOptsBuf, "-Xmx");
616 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
617 //LOGI("Heap size: %s", heapsizeOptsBuf);
618 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 mOptions.add(opt);
620
621 /*
622 * Enable or disable dexopt features, such as bytecode verification and
623 * calculation of register maps for precise GC.
624 */
625 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
626 if (dexoptFlagsBuf[0] != '\0') {
627 const char* opc;
628 const char* val;
629
630 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
631 if (opc != NULL) {
632 switch (*(opc+2)) {
633 case 'n': val = "-Xverify:none"; break;
634 case 'r': val = "-Xverify:remote"; break;
635 case 'a': val = "-Xverify:all"; break;
636 default: val = NULL; break;
637 }
638
639 if (val != NULL) {
640 opt.optionString = val;
641 mOptions.add(opt);
642 }
643 }
644
645 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
646 if (opc != NULL) {
647 switch (*(opc+2)) {
648 case 'n': val = "-Xdexopt:none"; break;
649 case 'v': val = "-Xdexopt:verified"; break;
650 case 'a': val = "-Xdexopt:all"; break;
651 default: val = NULL; break;
652 }
653
654 if (val != NULL) {
655 opt.optionString = val;
656 mOptions.add(opt);
657 }
658 }
659
660 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
661 if (opc != NULL) {
662 opt.optionString = "-Xgenregmap";
663 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700664
665 /* turn on precise GC while we're at it */
666 opt.optionString = "-Xgc:precise";
667 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
669 }
670
Barry Hayesc2b32172010-02-02 14:05:09 -0800671 /* enable poisoning of memory of freed objects */
672 property_get("dalvik.vm.gc.overwritefree", propBuf, "false");
673 if (strcmp(propBuf, "true") == 0) {
674 opt.optionString = "-Xgc:overwritefree";
675 mOptions.add(opt);
676 } else if (strcmp(propBuf, "false") != 0) {
677 LOGW("dalvik.vm.gc.overwritefree should be 'true' or 'false'");
678 }
679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 /* enable debugging; set suspend=y to pause during VM init */
681#ifdef HAVE_ANDROID_OS
682 /* use android ADB transport */
683 opt.optionString =
684 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
685#else
686 /* use TCP socket; address=0 means start at port 8000 and probe up */
687 LOGI("Using TCP socket for JDWP\n");
688 opt.optionString =
689 "-agentlib:jdwp=transport=dt_socket,suspend=n,server=y,address=0";
690#endif
691 mOptions.add(opt);
692
693 char enableDPBuf[sizeof("-Xdeadlockpredict:") + PROPERTY_VALUE_MAX];
694 property_get("dalvik.vm.deadlock-predict", propBuf, "");
695 if (strlen(propBuf) > 0) {
696 strcpy(enableDPBuf, "-Xdeadlockpredict:");
697 strcat(enableDPBuf, propBuf);
698 opt.optionString = enableDPBuf;
699 mOptions.add(opt);
700 }
701
702 LOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
703 if (checkJni) {
704 /* extended JNI checking */
705 opt.optionString = "-Xcheck:jni";
706 mOptions.add(opt);
707
708 /* set a cap on JNI global references */
709 opt.optionString = "-Xjnigreflimit:2000";
710 mOptions.add(opt);
711
712 /* with -Xcheck:jni, this provides a JNI function call trace */
713 //opt.optionString = "-verbose:jni";
714 //mOptions.add(opt);
715 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700716
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700717 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
718 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
719 if (strlen(propBuf) > 0) {
720 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
721 strcat(lockProfThresholdBuf, propBuf);
722 opt.optionString = lockProfThresholdBuf;
723 mOptions.add(opt);
724 }
725
Ben Cheng52b0e732009-06-19 13:31:12 -0700726#if defined(WITH_JIT)
727 /* Minimal profile threshold to trigger JIT compilation */
Ben Cheng69c997a2010-03-24 16:15:43 -0700728 char jitThresholdBuf[sizeof("-Xjitthreshold:") + PROPERTY_VALUE_MAX];
Ben Cheng52b0e732009-06-19 13:31:12 -0700729 property_get("dalvik.vm.jit.threshold", propBuf, "");
730 if (strlen(propBuf) > 0) {
Ben Cheng69c997a2010-03-24 16:15:43 -0700731 strcpy(jitThresholdBuf, "-Xjitthreshold:");
Ben Cheng52b0e732009-06-19 13:31:12 -0700732 strcat(jitThresholdBuf, propBuf);
733 opt.optionString = jitThresholdBuf;
734 mOptions.add(opt);
735 }
736
737 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
738 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
739 property_get("dalvik.vm.jit.op", propBuf, "");
740 if (strlen(propBuf) > 0) {
741 strcpy(jitOpBuf, "-Xjitop:");
742 strcat(jitOpBuf, propBuf);
743 opt.optionString = jitOpBuf;
744 mOptions.add(opt);
745 }
746
747 /*
748 * Reverse the polarity of dalvik.vm.jit.op and force interpreter-only
749 * for non-selected opcodes.
750 */
Ben Chenge090f3b2009-06-19 17:13:36 -0700751 property_get("dalvik.vm.jit.includeop", propBuf, "");
Ben Cheng52b0e732009-06-19 13:31:12 -0700752 if (strlen(propBuf) > 0) {
753 opt.optionString = "-Xincludeselectedop";
754 mOptions.add(opt);
755 }
756
757 /* Force interpreter-only mode for selected methods */
758 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
759 property_get("dalvik.vm.jit.method", propBuf, "");
760 if (strlen(propBuf) > 0) {
761 strcpy(jitMethodBuf, "-Xjitmethod:");
762 strcat(jitMethodBuf, propBuf);
763 opt.optionString = jitMethodBuf;
764 mOptions.add(opt);
765 }
766
767 /*
768 * Reverse the polarity of dalvik.vm.jit.method and force interpreter-only
769 * for non-selected methods.
770 */
Ben Chenge090f3b2009-06-19 17:13:36 -0700771 property_get("dalvik.vm.jit.includemethod", propBuf, "");
Ben Cheng52b0e732009-06-19 13:31:12 -0700772 if (strlen(propBuf) > 0) {
773 opt.optionString = "-Xincludeselectedmethod";
774 mOptions.add(opt);
775 }
Ben Cheng0e01fbf2009-07-17 16:03:26 -0700776
777 /*
778 * Enable profile collection on JIT'ed code.
779 */
780 property_get("dalvik.vm.jit.profile", propBuf, "");
781 if (strlen(propBuf) > 0) {
782 opt.optionString = "-Xjitprofile";
783 mOptions.add(opt);
784 }
Ben Chengacc56ec2009-09-11 12:59:53 -0700785
786 /*
787 * Disable optimizations by setting the corresponding bit to 1.
788 */
789 char jitOptBuf[sizeof("-Xjitdisableopt:") + PROPERTY_VALUE_MAX];
790 property_get("dalvik.vm.jit.disableopt", propBuf, "");
791 if (strlen(propBuf) > 0) {
792 strcpy(jitOptBuf, "-Xjitdisableopt:");
793 strcat(jitOptBuf, propBuf);
794 opt.optionString = jitOptBuf;
795 mOptions.add(opt);
796 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700797#endif
798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 if (executionMode == kEMIntPortable) {
800 opt.optionString = "-Xint:portable";
801 mOptions.add(opt);
802 } else if (executionMode == kEMIntFast) {
803 opt.optionString = "-Xint:fast";
804 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700805#if defined(WITH_JIT)
806 } else if (executionMode == kEMJitCompiler) {
807 opt.optionString = "-Xint:jit";
808 mOptions.add(opt);
809#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700811
812 if (checkDexSum) {
813 /* perform additional DEX checksum tests */
814 opt.optionString = "-Xcheckdexsum";
815 mOptions.add(opt);
816 }
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 if (logStdio) {
819 /* convert stdout/stderr to log messages */
820 opt.optionString = "-Xlog-stdio";
821 mOptions.add(opt);
822 }
823
824 if (enableAssertBuf[4] != '\0') {
825 /* accept "all" to mean "all classes and packages" */
826 if (strcmp(enableAssertBuf+4, "all") == 0)
827 enableAssertBuf[3] = '\0';
828 LOGI("Assertions enabled: '%s'\n", enableAssertBuf);
829 opt.optionString = enableAssertBuf;
830 mOptions.add(opt);
831 } else {
832 LOGV("Assertions disabled\n");
833 }
834
835 if (jniOptsBuf[10] != '\0') {
836 LOGI("JNI options: '%s'\n", jniOptsBuf);
837 opt.optionString = jniOptsBuf;
838 mOptions.add(opt);
839 }
840
841 if (stackTraceFileBuf[0] != '\0') {
842 static const char* stfOptName = "-Xstacktracefile:";
843
844 stackTraceFile = (char*) malloc(strlen(stfOptName) +
845 strlen(stackTraceFileBuf) +1);
846 strcpy(stackTraceFile, stfOptName);
847 strcat(stackTraceFile, stackTraceFileBuf);
848 opt.optionString = stackTraceFile;
849 mOptions.add(opt);
850 }
851
852 /* Set the properties for locale */
853 {
854 char langOption[sizeof("-Duser.language=") + 3];
855 char regionOption[sizeof("-Duser.region=") + 3];
856 strcpy(langOption, "-Duser.language=");
857 strcpy(regionOption, "-Duser.region=");
858 readLocale(langOption, regionOption);
859 opt.extraInfo = NULL;
860 opt.optionString = langOption;
861 mOptions.add(opt);
862 opt.optionString = regionOption;
863 mOptions.add(opt);
864 }
865
866 /*
867 * We don't have /tmp on the device, but we often have an SD card. Apps
868 * shouldn't use this, but some test suites might want to exercise it.
869 */
870 opt.optionString = "-Djava.io.tmpdir=/sdcard";
871 mOptions.add(opt);
872
873 initArgs.version = JNI_VERSION_1_4;
874 initArgs.options = mOptions.editArray();
875 initArgs.nOptions = mOptions.size();
876 initArgs.ignoreUnrecognized = JNI_FALSE;
877
878 /*
879 * Initialize the VM.
880 *
881 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
882 * If this call succeeds, the VM is ready, and we can start issuing
883 * JNI calls.
884 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700885 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 LOGE("JNI_CreateJavaVM failed\n");
887 goto bail;
888 }
889
Andy McFaddenf70188a2009-03-31 15:52:13 -0700890 result = 0;
891
892bail:
893 free(stackTraceFile);
894 return result;
895}
896
897/*
898 * Start the Android runtime. This involves starting the virtual machine
899 * and calling the "static void main(String[] args)" method in the class
900 * named by "className".
901 */
902void AndroidRuntime::start(const char* className, const bool startSystemServer)
903{
904 LOGD("\n>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<\n");
905
906 char* slashClassName = NULL;
907 char* cp;
908 JNIEnv* env;
909
910 blockSigpipe();
911
912 /*
913 * 'startSystemServer == true' means runtime is obslete and not run from
914 * init.rc anymore, so we print out the boot start event here.
915 */
916 if (startSystemServer) {
917 /* track our progress through the boot sequence */
918 const int LOG_BOOT_PROGRESS_START = 3000;
919 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
920 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
921 }
922
923 const char* rootDir = getenv("ANDROID_ROOT");
924 if (rootDir == NULL) {
925 rootDir = "/system";
926 if (!hasDir("/system")) {
927 LOG_FATAL("No root directory specified, and /android does not exist.");
928 goto bail;
929 }
930 setenv("ANDROID_ROOT", rootDir, 1);
931 }
932
933 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
934 //LOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
935
936 /* start the virtual machine */
937 if (startVm(&mJavaVM, &env) != 0)
938 goto bail;
939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 /*
941 * Register android functions.
942 */
943 if (startReg(env) < 0) {
944 LOGE("Unable to register all android natives\n");
945 goto bail;
946 }
947
948 /*
949 * We want to call main() with a String array with arguments in it.
950 * At present we only have one argument, the class name. Create an
951 * array to hold it.
952 */
953 jclass stringClass;
954 jobjectArray strArray;
955 jstring classNameStr;
956 jstring startSystemServerStr;
957
958 stringClass = env->FindClass("java/lang/String");
959 assert(stringClass != NULL);
960 strArray = env->NewObjectArray(2, stringClass, NULL);
961 assert(strArray != NULL);
962 classNameStr = env->NewStringUTF(className);
963 assert(classNameStr != NULL);
964 env->SetObjectArrayElement(strArray, 0, classNameStr);
965 startSystemServerStr = env->NewStringUTF(startSystemServer ?
966 "true" : "false");
967 env->SetObjectArrayElement(strArray, 1, startSystemServerStr);
968
969 /*
970 * Start VM. This thread becomes the main thread of the VM, and will
971 * not return until the VM exits.
972 */
973 jclass startClass;
974 jmethodID startMeth;
975
976 slashClassName = strdup(className);
977 for (cp = slashClassName; *cp != '\0'; cp++)
978 if (*cp == '.')
979 *cp = '/';
980
981 startClass = env->FindClass(slashClassName);
982 if (startClass == NULL) {
983 LOGE("JavaVM unable to locate class '%s'\n", slashClassName);
984 /* keep going */
985 } else {
986 startMeth = env->GetStaticMethodID(startClass, "main",
987 "([Ljava/lang/String;)V");
988 if (startMeth == NULL) {
989 LOGE("JavaVM unable to find main() in '%s'\n", className);
990 /* keep going */
991 } else {
992 env->CallStaticVoidMethod(startClass, startMeth, strArray);
993
994#if 0
995 if (env->ExceptionCheck())
996 threadExitUncaughtException(env);
997#endif
998 }
999 }
1000
1001 LOGD("Shutting down VM\n");
1002 if (mJavaVM->DetachCurrentThread() != JNI_OK)
1003 LOGW("Warning: unable to detach main thread\n");
1004 if (mJavaVM->DestroyJavaVM() != 0)
1005 LOGW("Warning: VM did not shut down cleanly\n");
1006
1007bail:
1008 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009}
1010
1011void AndroidRuntime::start()
1012{
1013 start("com.android.internal.os.RuntimeInit",
1014 false /* Don't start the system server */);
1015}
1016
1017void AndroidRuntime::onExit(int code)
1018{
1019 LOGI("AndroidRuntime onExit calling exit(%d)", code);
1020 exit(code);
1021}
1022
1023/*
1024 * Get the JNIEnv pointer for this thread.
1025 *
1026 * Returns NULL if the slot wasn't allocated or populated.
1027 */
1028/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
1029{
1030 JNIEnv* env;
1031 JavaVM* vm = AndroidRuntime::getJavaVM();
1032 assert(vm != NULL);
1033
1034 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
1035 return NULL;
1036 return env;
1037}
1038
1039/*
1040 * Makes the current thread visible to the VM.
1041 *
1042 * The JNIEnv pointer returned is only valid for the current thread, and
1043 * thus must be tucked into thread-local storage.
1044 */
1045static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
1046{
1047 JavaVMAttachArgs args;
1048 JavaVM* vm;
1049 jint result;
1050
1051 vm = AndroidRuntime::getJavaVM();
1052 assert(vm != NULL);
1053
1054 args.version = JNI_VERSION_1_4;
1055 args.name = (char*) threadName;
1056 args.group = NULL;
1057
1058 result = vm->AttachCurrentThread(pEnv, (void*) &args);
1059 if (result != JNI_OK)
Andy McFadden701d9162010-03-01 14:03:13 -08001060 LOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061
1062 return result;
1063}
1064
1065/*
1066 * Detach the current thread from the set visible to the VM.
1067 */
1068static int javaDetachThread(void)
1069{
1070 JavaVM* vm;
1071 jint result;
1072
1073 vm = AndroidRuntime::getJavaVM();
1074 assert(vm != NULL);
1075
1076 result = vm->DetachCurrentThread();
1077 if (result != JNI_OK)
1078 LOGE("ERROR: thread detach failed\n");
1079 return result;
1080}
1081
1082/*
1083 * When starting a native thread that will be visible from the VM, we
1084 * bounce through this to get the right attach/detach action.
1085 * Note that this function calls free(args)
1086 */
1087/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
1088 void* start = ((void**)args)[0];
1089 void* userData = ((void **)args)[1];
1090 char* name = (char*) ((void **)args)[2]; // we own this storage
1091 free(args);
1092 JNIEnv* env;
1093 int result;
1094
1095 /* hook us into the VM */
1096 if (javaAttachThread(name, &env) != JNI_OK)
1097 return -1;
1098
1099 /* start the thread running */
1100 result = (*(android_thread_func_t)start)(userData);
1101
1102 /* unhook us */
1103 javaDetachThread();
1104 free(name);
1105
1106 return result;
1107}
1108
1109/*
1110 * This is invoked from androidCreateThreadEtc() via the callback
1111 * set with androidSetCreateThreadFunc().
1112 *
1113 * We need to create the new thread in such a way that it gets hooked
1114 * into the VM before it really starts executing.
1115 */
1116/*static*/ int AndroidRuntime::javaCreateThreadEtc(
1117 android_thread_func_t entryFunction,
1118 void* userData,
1119 const char* threadName,
1120 int32_t threadPriority,
1121 size_t threadStackSize,
1122 android_thread_id_t* threadId)
1123{
1124 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1125 int result;
1126
1127 assert(threadName != NULL);
1128
1129 args[0] = (void*) entryFunction;
1130 args[1] = userData;
1131 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1132
1133 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1134 threadName, threadPriority, threadStackSize, threadId);
1135 return result;
1136}
1137
1138/*
1139 * Create a thread that is visible from the VM.
1140 *
1141 * This is called from elsewhere in the library.
1142 */
1143/*static*/ void AndroidRuntime::createJavaThread(const char* name,
1144 void (*start)(void *), void* arg)
1145{
1146 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
1147 ANDROID_PRIORITY_DEFAULT, 0, NULL);
1148}
1149
1150#if 0
1151static void quickTest(void* arg)
1152{
1153 const char* str = (const char*) arg;
1154
1155 printf("In quickTest: %s\n", str);
1156}
1157#endif
1158
1159#ifdef NDEBUG
1160 #define REG_JNI(name) { name }
1161 struct RegJNIRec {
1162 int (*mProc)(JNIEnv*);
1163 };
1164#else
1165 #define REG_JNI(name) { name, #name }
1166 struct RegJNIRec {
1167 int (*mProc)(JNIEnv*);
1168 const char* mName;
1169 };
1170#endif
1171
1172typedef void (*RegJAMProc)();
1173
1174static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1175{
1176 for (size_t i = 0; i < count; i++) {
1177 if (array[i].mProc(env) < 0) {
1178#ifndef NDEBUG
1179 LOGD("----------!!! %s failed to load\n", array[i].mName);
1180#endif
1181 return -1;
1182 }
1183 }
1184 return 0;
1185}
1186
1187static void register_jam_procs(const RegJAMProc array[], size_t count)
1188{
1189 for (size_t i = 0; i < count; i++) {
1190 array[i]();
1191 }
1192}
1193
1194static const RegJNIRec gRegJNI[] = {
1195 REG_JNI(register_android_debug_JNITest),
1196 REG_JNI(register_com_android_internal_os_RuntimeInit),
1197 REG_JNI(register_android_os_SystemClock),
1198 REG_JNI(register_android_util_EventLog),
1199 REG_JNI(register_android_util_Log),
1200 REG_JNI(register_android_util_FloatMath),
1201 REG_JNI(register_android_text_format_Time),
1202 REG_JNI(register_android_pim_EventRecurrence),
1203 REG_JNI(register_android_content_AssetManager),
1204 REG_JNI(register_android_content_StringBlock),
1205 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001206 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 REG_JNI(register_android_security_Md5MessageDigest),
1208 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001209 REG_JNI(register_android_text_AndroidBidi),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 REG_JNI(register_android_text_KeyCharacterMap),
1211 REG_JNI(register_android_os_Process),
1212 REG_JNI(register_android_os_Binder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 REG_JNI(register_android_view_Display),
1214 REG_JNI(register_android_nio_utils),
1215 REG_JNI(register_android_graphics_PixelFormat),
1216 REG_JNI(register_android_graphics_Graphics),
Romain Guye4d01122010-06-16 18:44:05 -07001217 REG_JNI(register_android_view_GLES20Canvas),
Romain Guyc6133282010-06-10 16:17:24 -07001218 REG_JNI(register_android_view_HardwareRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 REG_JNI(register_android_view_Surface),
1220 REG_JNI(register_android_view_ViewRoot),
1221 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1222 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001223 REG_JNI(register_android_opengl_jni_GLES10),
1224 REG_JNI(register_android_opengl_jni_GLES10Ext),
1225 REG_JNI(register_android_opengl_jni_GLES11),
1226 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001227 REG_JNI(register_android_opengl_jni_GLES20),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228
1229 REG_JNI(register_android_graphics_Bitmap),
1230 REG_JNI(register_android_graphics_BitmapFactory),
1231 REG_JNI(register_android_graphics_Camera),
1232 REG_JNI(register_android_graphics_Canvas),
1233 REG_JNI(register_android_graphics_ColorFilter),
1234 REG_JNI(register_android_graphics_DrawFilter),
1235 REG_JNI(register_android_graphics_Interpolator),
1236 REG_JNI(register_android_graphics_LayerRasterizer),
1237 REG_JNI(register_android_graphics_MaskFilter),
1238 REG_JNI(register_android_graphics_Matrix),
1239 REG_JNI(register_android_graphics_Movie),
1240 REG_JNI(register_android_graphics_NinePatch),
1241 REG_JNI(register_android_graphics_Paint),
1242 REG_JNI(register_android_graphics_Path),
1243 REG_JNI(register_android_graphics_PathMeasure),
1244 REG_JNI(register_android_graphics_PathEffect),
1245 REG_JNI(register_android_graphics_Picture),
1246 REG_JNI(register_android_graphics_PorterDuff),
1247 REG_JNI(register_android_graphics_Rasterizer),
1248 REG_JNI(register_android_graphics_Region),
1249 REG_JNI(register_android_graphics_Shader),
1250 REG_JNI(register_android_graphics_Typeface),
1251 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001252 REG_JNI(register_android_graphics_YuvImage),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 REG_JNI(register_com_android_internal_graphics_NativeUtils),
1254
1255 REG_JNI(register_android_database_CursorWindow),
Vasu Nori5a03f362009-10-20 15:16:35 -07001256 REG_JNI(register_android_database_SQLiteCompiledSql),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 REG_JNI(register_android_database_SQLiteDatabase),
1258 REG_JNI(register_android_database_SQLiteDebug),
1259 REG_JNI(register_android_database_SQLiteProgram),
1260 REG_JNI(register_android_database_SQLiteQuery),
1261 REG_JNI(register_android_database_SQLiteStatement),
1262 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 REG_JNI(register_android_os_FileObserver),
1264 REG_JNI(register_android_os_FileUtils),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001265 REG_JNI(register_android_os_MessageQueue),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 REG_JNI(register_android_os_ParcelFileDescriptor),
1267 REG_JNI(register_android_os_Power),
1268 REG_JNI(register_android_os_StatFs),
1269 REG_JNI(register_android_os_SystemProperties),
1270 REG_JNI(register_android_os_UEventObserver),
1271 REG_JNI(register_android_net_LocalSocketImpl),
1272 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001273 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 REG_JNI(register_android_net_wifi_WifiManager),
1275 REG_JNI(register_android_os_MemoryFile),
1276 REG_JNI(register_com_android_internal_os_ZygoteInit),
1277 REG_JNI(register_android_hardware_Camera),
1278 REG_JNI(register_android_hardware_SensorManager),
1279 REG_JNI(register_android_media_AudioRecord),
1280 REG_JNI(register_android_media_AudioSystem),
1281 REG_JNI(register_android_media_AudioTrack),
1282 REG_JNI(register_android_media_JetPlayer),
Mike Lockwood755fd612010-05-25 19:08:48 -04001283 REG_JNI(register_android_media_MtpClient),
1284 REG_JNI(register_android_media_MtpCursor),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 REG_JNI(register_android_media_ToneGenerator),
1286
1287 REG_JNI(register_android_opengl_classes),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 REG_JNI(register_android_bluetooth_HeadsetBase),
1289 REG_JNI(register_android_bluetooth_BluetoothAudioGateway),
Nick Pelly0b6955a2009-05-26 19:13:43 -07001290 REG_JNI(register_android_bluetooth_BluetoothSocket),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 REG_JNI(register_android_bluetooth_ScoSocket),
Nick Pellybd022f42009-08-14 18:33:38 -07001292 REG_JNI(register_android_server_BluetoothService),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 REG_JNI(register_android_server_BluetoothEventLoop),
1294 REG_JNI(register_android_server_BluetoothA2dpService),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001295 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 REG_JNI(register_android_message_digest_sha1),
1297 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001298 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001299 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001300 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001301 REG_JNI(register_android_backup_BackupHelperDispatcher),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001302
1303 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001304 REG_JNI(register_android_view_InputChannel),
1305 REG_JNI(register_android_view_InputQueue),
1306 REG_JNI(register_android_view_InputTarget),
1307 REG_JNI(register_android_view_KeyEvent),
1308 REG_JNI(register_android_view_MotionEvent),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309};
1310
1311/*
1312 * Register android native functions with the VM.
1313 */
1314/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1315{
1316 /*
1317 * This hook causes all future threads created in this process to be
1318 * attached to the JavaVM. (This needs to go away in favor of JNI
1319 * Attach calls.)
1320 */
1321 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1322
1323 LOGD("--- registering native functions ---\n");
1324
1325 /*
1326 * Every "register" function calls one or more things that return
1327 * a local reference (e.g. FindClass). Because we haven't really
1328 * started the VM yet, they're all getting stored in the base frame
1329 * and never released. Use Push/Pop to manage the storage.
1330 */
1331 env->PushLocalFrame(200);
1332
1333 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1334 env->PopLocalFrame(NULL);
1335 return -1;
1336 }
1337 env->PopLocalFrame(NULL);
1338
1339 //createJavaThread("fubar", quickTest, (void*) "hello");
1340
1341 return 0;
1342}
1343
1344AndroidRuntime* AndroidRuntime::getRuntime()
1345{
1346 return gCurRuntime;
1347}
1348
1349/**
1350 * Used by WithFramework to register native functions.
1351 */
1352extern "C"
1353jint Java_com_android_internal_util_WithFramework_registerNatives(
1354 JNIEnv* env, jclass clazz) {
1355 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1356}
1357
1358/**
1359 * Used by LoadClass to register native functions.
1360 */
1361extern "C"
1362jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1363 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1364}
1365
1366} // namespace android