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