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