AndroidRuntime.cpp revision d9feaf49719d58cfc4fad203bd10e75253024fa9
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AndroidRuntime"
18//#define LOG_NDEBUG 0
19
20#include <android_runtime/AndroidRuntime.h>
21#include <binder/IBinder.h>
22#include <binder/IPCThreadState.h>
23#include <binder/IServiceManager.h>
24#include <utils/Log.h>
25#include <utils/misc.h>
26#include <binder/Parcel.h>
27#include <utils/threads.h>
28#include <cutils/properties.h>
29
30#include <SkGraphics.h>
31#include <SkImageDecoder.h>
32#include <SkImageRef_GlobalPool.h>
33
34#include "jni.h"
35#include "JNIHelp.h"
36#include "JniInvocation.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 int register_android_os_Binder(JNIEnv* env);
51extern int register_android_os_Process(JNIEnv* env);
52extern int register_android_graphics_Bitmap(JNIEnv*);
53extern int register_android_graphics_BitmapFactory(JNIEnv*);
54extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
55extern int register_android_graphics_Camera(JNIEnv* env);
56extern int register_android_graphics_CreateJavaOutputStreamAdaptor(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_Shader(JNIEnv* env);
65extern int register_android_graphics_Typeface(JNIEnv* env);
66extern int register_android_graphics_YuvImage(JNIEnv* env);
67
68extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
69extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
70extern int register_android_opengl_jni_EGL14(JNIEnv* env);
71extern int register_android_opengl_jni_EGLExt(JNIEnv* env);
72extern int register_android_opengl_jni_GLES10(JNIEnv* env);
73extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
74extern int register_android_opengl_jni_GLES11(JNIEnv* env);
75extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
76extern int register_android_opengl_jni_GLES20(JNIEnv* env);
77extern int register_android_opengl_jni_GLES30(JNIEnv* env);
78
79extern int register_android_hardware_Camera(JNIEnv *env);
80extern int register_android_hardware_camera2_CameraMetadata(JNIEnv *env);
81extern int register_android_hardware_SensorManager(JNIEnv *env);
82extern int register_android_hardware_SerialPort(JNIEnv *env);
83extern int register_android_hardware_UsbDevice(JNIEnv *env);
84extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
85extern int register_android_hardware_UsbRequest(JNIEnv *env);
86
87extern int register_android_media_AudioRecord(JNIEnv *env);
88extern int register_android_media_AudioSystem(JNIEnv *env);
89extern int register_android_media_AudioTrack(JNIEnv *env);
90extern int register_android_media_JetPlayer(JNIEnv *env);
91extern int register_android_media_ToneGenerator(JNIEnv *env);
92
93extern int register_android_util_FloatMath(JNIEnv* env);
94
95namespace android {
96
97/*
98 * JNI-based registration functions.  Note these are properly contained in
99 * namespace android.
100 */
101extern int register_android_content_AssetManager(JNIEnv* env);
102extern int register_android_util_EventLog(JNIEnv* env);
103extern int register_android_util_Log(JNIEnv* env);
104extern int register_android_content_StringBlock(JNIEnv* env);
105extern int register_android_content_XmlBlock(JNIEnv* env);
106extern int register_android_emoji_EmojiFactory(JNIEnv* env);
107extern int register_android_graphics_Canvas(JNIEnv* env);
108extern int register_android_graphics_ColorFilter(JNIEnv* env);
109extern int register_android_graphics_DrawFilter(JNIEnv* env);
110extern int register_android_graphics_Matrix(JNIEnv* env);
111extern int register_android_graphics_Paint(JNIEnv* env);
112extern int register_android_graphics_Path(JNIEnv* env);
113extern int register_android_graphics_PathMeasure(JNIEnv* env);
114extern int register_android_graphics_Picture(JNIEnv*);
115extern int register_android_graphics_PorterDuff(JNIEnv* env);
116extern int register_android_graphics_Rasterizer(JNIEnv* env);
117extern int register_android_graphics_Region(JNIEnv* env);
118extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
119extern int register_android_graphics_Xfermode(JNIEnv* env);
120extern int register_android_graphics_pdf_PdfDocument(JNIEnv* env);
121extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
122extern int register_android_view_DisplayList(JNIEnv* env);
123extern int register_android_view_GraphicBuffer(JNIEnv* env);
124extern int register_android_view_GLES20Canvas(JNIEnv* env);
125extern int register_android_view_GLRenderer(JNIEnv* env);
126extern int register_android_view_ThreadedRenderer(JNIEnv* env);
127extern int register_android_view_RemoteGLRenderer(JNIEnv* env);
128extern int register_android_view_Surface(JNIEnv* env);
129extern int register_android_view_SurfaceControl(JNIEnv* env);
130extern int register_android_view_SurfaceSession(JNIEnv* env);
131extern int register_android_view_TextureView(JNIEnv* env);
132extern int register_android_database_CursorWindow(JNIEnv* env);
133extern int register_android_database_SQLiteConnection(JNIEnv* env);
134extern int register_android_database_SQLiteGlobal(JNIEnv* env);
135extern int register_android_database_SQLiteDebug(JNIEnv* env);
136extern int register_android_debug_JNITest(JNIEnv* env);
137extern int register_android_nio_utils(JNIEnv* env);
138extern int register_android_text_format_Time(JNIEnv* env);
139extern int register_android_os_Debug(JNIEnv* env);
140extern int register_android_os_MessageQueue(JNIEnv* env);
141extern int register_android_os_Parcel(JNIEnv* env);
142extern int register_android_os_SELinux(JNIEnv* env);
143extern int register_android_os_SystemProperties(JNIEnv *env);
144extern int register_android_os_SystemClock(JNIEnv* env);
145extern int register_android_os_Trace(JNIEnv* env);
146extern int register_android_os_FileObserver(JNIEnv *env);
147extern int register_android_os_UEventObserver(JNIEnv* env);
148extern int register_android_os_MemoryFile(JNIEnv* env);
149extern int register_android_net_LocalSocketImpl(JNIEnv* env);
150extern int register_android_net_NetworkUtils(JNIEnv* env);
151extern int register_android_net_TrafficStats(JNIEnv* env);
152extern int register_android_net_wifi_WifiNative(JNIEnv* env);
153extern int register_android_text_AndroidCharacter(JNIEnv *env);
154extern int register_android_text_AndroidBidi(JNIEnv *env);
155extern int register_android_opengl_classes(JNIEnv *env);
156extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
157extern int register_android_server_Watchdog(JNIEnv* env);
158extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
159extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
160extern int register_android_backup_BackupDataInput(JNIEnv *env);
161extern int register_android_backup_BackupDataOutput(JNIEnv *env);
162extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
163extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
164extern int register_android_app_backup_FullBackup(JNIEnv *env);
165extern int register_android_app_ActivityThread(JNIEnv *env);
166extern int register_android_app_NativeActivity(JNIEnv *env);
167extern int register_android_media_RemoteDisplay(JNIEnv *env);
168extern int register_android_view_InputChannel(JNIEnv* env);
169extern int register_android_view_InputDevice(JNIEnv* env);
170extern int register_android_view_InputEventReceiver(JNIEnv* env);
171extern int register_android_view_InputEventSender(JNIEnv* env);
172extern int register_android_view_InputQueue(JNIEnv* env);
173extern int register_android_view_KeyCharacterMap(JNIEnv *env);
174extern int register_android_view_KeyEvent(JNIEnv* env);
175extern int register_android_view_MotionEvent(JNIEnv* env);
176extern int register_android_view_PointerIcon(JNIEnv* env);
177extern int register_android_view_VelocityTracker(JNIEnv* env);
178extern int register_android_content_res_ObbScanner(JNIEnv* env);
179extern int register_android_content_res_Configuration(JNIEnv* env);
180extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
181extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
182extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env);
183
184static AndroidRuntime* gCurRuntime = NULL;
185
186static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
187{
188    if (jniThrowException(env, exc, msg) != 0)
189        assert(false);
190}
191
192/*
193 * Code written in the Java Programming Language calls here from main().
194 */
195static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz)
196{
197    gCurRuntime->onStarted();
198}
199
200static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
201{
202    gCurRuntime->onZygoteInit();
203}
204
205static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv* env,
206        jobject clazz, jboolean exitWithoutCleanup)
207{
208    gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup);
209}
210
211/*
212 * JNI registration.
213 */
214static JNINativeMethod gMethods[] = {
215    { "nativeFinishInit", "()V",
216        (void*) com_android_internal_os_RuntimeInit_nativeFinishInit },
217    { "nativeZygoteInit", "()V",
218        (void*) com_android_internal_os_RuntimeInit_nativeZygoteInit },
219    { "nativeSetExitWithoutCleanup", "(Z)V",
220        (void*) com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup },
221};
222
223int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
224{
225    return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
226        gMethods, NELEM(gMethods));
227}
228
229// ----------------------------------------------------------------------
230
231/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
232
233
234AndroidRuntime::AndroidRuntime() :
235        mExitWithoutCleanup(false)
236{
237    SkGraphics::Init();
238    // this sets our preference for 16bit images during decode
239    // in case the src is opaque and 24bit
240    SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
241    // This cache is shared between browser native images, and java "purgeable"
242    // bitmaps. This globalpool is for images that do not either use the java
243    // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
244    // java call site.
245    SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
246    // There is also a global font cache, but its budget is specified in code
247    // see SkFontHost_android.cpp
248
249    // Pre-allocate enough space to hold a fair number of options.
250    mOptions.setCapacity(20);
251
252    assert(gCurRuntime == NULL);        // one per process
253    gCurRuntime = this;
254}
255
256AndroidRuntime::~AndroidRuntime()
257{
258    SkGraphics::Term();
259}
260
261/*
262 * Register native methods using JNI.
263 */
264/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
265    const char* className, const JNINativeMethod* gMethods, int numMethods)
266{
267    return jniRegisterNativeMethods(env, className, gMethods, numMethods);
268}
269
270status_t AndroidRuntime::callMain(const char* className,
271    jclass clazz, int argc, const char* const argv[])
272{
273    JNIEnv* env;
274    jmethodID methodId;
275
276    ALOGD("Calling main entry %s", className);
277
278    env = getJNIEnv();
279    if (clazz == NULL || env == NULL) {
280        return UNKNOWN_ERROR;
281    }
282
283    methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
284    if (methodId == NULL) {
285        ALOGE("ERROR: could not find method %s.main(String[])\n", className);
286        return UNKNOWN_ERROR;
287    }
288
289    /*
290     * We want to call main() with a String array with our arguments in it.
291     * Create an array and populate it.
292     */
293    jclass stringClass;
294    jobjectArray strArray;
295
296    stringClass = env->FindClass("java/lang/String");
297    strArray = env->NewObjectArray(argc, stringClass, NULL);
298
299    for (int i = 0; i < argc; i++) {
300        jstring argStr = env->NewStringUTF(argv[i]);
301        env->SetObjectArrayElement(strArray, i, argStr);
302    }
303
304    env->CallStaticVoidMethod(clazz, methodId, strArray);
305    return NO_ERROR;
306}
307
308/*
309 * The VM calls this through the "exit" hook.
310 */
311static void runtime_exit(int code)
312{
313    gCurRuntime->exit(code);
314}
315
316/*
317 * The VM calls this through the "vfprintf" hook.
318 *
319 * We ignore "fp" and just write the results to the log file.
320 */
321static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
322{
323    LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
324}
325
326/**
327 * The VM calls this when mutex contention debugging is enabled to
328 * determine whether or not the blocked thread was a "sensitive thread"
329 * for user responsiveness/smoothess.
330 *
331 * Our policy for this is whether or not we're tracing any StrictMode
332 * events on this thread (which we might've inherited via Binder calls
333 * into us)
334 */
335static bool runtime_isSensitiveThread() {
336    IPCThreadState* state = IPCThreadState::selfOrNull();
337    return state && state->getStrictModePolicy() != 0;
338}
339
340
341/**
342 * Add VM arguments to the to-be-executed VM
343 * Stops at first non '-' argument (also stops at an argument of '--')
344 * Returns the number of args consumed
345 */
346int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
347{
348    int i;
349
350    for (i = 0; i<argc; i++) {
351        if (argv[i][0] != '-') {
352            return i;
353        }
354        if (argv[i][1] == '-' && argv[i][2] == 0) {
355            return i+1;
356        }
357
358        JavaVMOption opt;
359        memset(&opt, 0, sizeof(opt));
360        opt.optionString = (char*)argv[i];
361        mOptions.add(opt);
362    }
363    return i;
364}
365
366static int hasDir(const char* dir)
367{
368    struct stat s;
369    int res = stat(dir, &s);
370    if (res == 0) {
371        return S_ISDIR(s.st_mode);
372    }
373    return 0;
374}
375
376/*
377 * Read the persistent locale.
378 */
379static void readLocale(char* language, char* region)
380{
381    char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
382
383    property_get("persist.sys.language", propLang, "");
384    property_get("persist.sys.country", propRegn, "");
385    if (*propLang == 0 && *propRegn == 0) {
386        /* Set to ro properties, default is en_US */
387        property_get("ro.product.locale.language", propLang, "en");
388        property_get("ro.product.locale.region", propRegn, "US");
389    }
390    strncat(language, propLang, 2);
391    strncat(region, propRegn, 2);
392    //ALOGD("language=%s region=%s\n", language, region);
393}
394
395/*
396 * Parse a property containing space-separated options that should be
397 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
398 *
399 * This will cut up "extraOptsBuf" as we chop it into individual options.
400 *
401 * Adds the strings, if any, to mOptions.
402 */
403void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
404{
405    JavaVMOption opt;
406    char* start;
407    char* end;
408
409    memset(&opt, 0, sizeof(opt));
410    start = extraOptsBuf;
411    while (*start != '\0') {
412        while (*start == ' ')                   /* skip leading whitespace */
413            start++;
414        if (*start == '\0')                     /* was trailing ws, bail */
415            break;
416
417        end = start+1;
418        while (*end != ' ' && *end != '\0')     /* find end of token */
419            end++;
420        if (*end == ' ')
421            *end++ = '\0';          /* mark end, advance to indicate more */
422
423        opt.optionString = start;
424        mOptions.add(opt);
425        start = end;
426    }
427}
428
429/*
430 * Start the Dalvik Virtual Machine.
431 *
432 * Various arguments, most determined by system properties, are passed in.
433 * The "mOptions" vector is updated.
434 *
435 * Returns 0 on success.
436 */
437int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
438{
439    int result = -1;
440    JavaVMInitArgs initArgs;
441    JavaVMOption opt;
442    char propBuf[PROPERTY_VALUE_MAX];
443    char stackTraceFileBuf[PROPERTY_VALUE_MAX];
444    char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
445    char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
446    char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
447    char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
448    char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
449    char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
450    char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
451    char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
452    char gctypeOptsBuf[sizeof("-Xgc:")-1 + PROPERTY_VALUE_MAX];
453    char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
454    char jitcodecachesizeOptsBuf[sizeof("-Xjitcodecachesize:")-1 + PROPERTY_VALUE_MAX];
455    char extraOptsBuf[PROPERTY_VALUE_MAX];
456    char* stackTraceFile = NULL;
457    bool checkJni = false;
458    bool checkDexSum = false;
459    bool logStdio = false;
460    enum {
461      kEMDefault,
462      kEMIntPortable,
463      kEMIntFast,
464      kEMJitCompiler,
465    } executionMode = kEMDefault;
466
467
468    property_get("dalvik.vm.checkjni", propBuf, "");
469    if (strcmp(propBuf, "true") == 0) {
470        checkJni = true;
471    } else if (strcmp(propBuf, "false") != 0) {
472        /* property is neither true nor false; fall back on kernel parameter */
473        property_get("ro.kernel.android.checkjni", propBuf, "");
474        if (propBuf[0] == '1') {
475            checkJni = true;
476        }
477    }
478
479    property_get("dalvik.vm.execution-mode", propBuf, "");
480    if (strcmp(propBuf, "int:portable") == 0) {
481        executionMode = kEMIntPortable;
482    } else if (strcmp(propBuf, "int:fast") == 0) {
483        executionMode = kEMIntFast;
484    } else if (strcmp(propBuf, "int:jit") == 0) {
485        executionMode = kEMJitCompiler;
486    }
487
488    property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
489
490    property_get("dalvik.vm.check-dex-sum", propBuf, "");
491    if (strcmp(propBuf, "true") == 0) {
492        checkDexSum = true;
493    }
494
495    property_get("log.redirect-stdio", propBuf, "");
496    if (strcmp(propBuf, "true") == 0) {
497        logStdio = true;
498    }
499
500    strcpy(enableAssertBuf, "-ea:");
501    property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
502
503    strcpy(jniOptsBuf, "-Xjniopts:");
504    property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
505
506    /* route exit() to our handler */
507    opt.extraInfo = (void*) runtime_exit;
508    opt.optionString = "exit";
509    mOptions.add(opt);
510
511    /* route fprintf() to our handler */
512    opt.extraInfo = (void*) runtime_vfprintf;
513    opt.optionString = "vfprintf";
514    mOptions.add(opt);
515
516    /* register the framework-specific "is sensitive thread" hook */
517    opt.extraInfo = (void*) runtime_isSensitiveThread;
518    opt.optionString = "sensitiveThread";
519    mOptions.add(opt);
520
521    opt.extraInfo = NULL;
522
523    /* enable verbose; standard options are { jni, gc, class } */
524    //options[curOpt++].optionString = "-verbose:jni";
525    opt.optionString = "-verbose:gc";
526    mOptions.add(opt);
527    //options[curOpt++].optionString = "-verbose:class";
528
529    /*
530     * The default starting and maximum size of the heap.  Larger
531     * values should be specified in a product property override.
532     */
533    strcpy(heapstartsizeOptsBuf, "-Xms");
534    property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
535    opt.optionString = heapstartsizeOptsBuf;
536    mOptions.add(opt);
537    strcpy(heapsizeOptsBuf, "-Xmx");
538    property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
539    opt.optionString = heapsizeOptsBuf;
540    mOptions.add(opt);
541
542    // Increase the main thread's interpreter stack size for bug 6315322.
543    opt.optionString = "-XX:mainThreadStackSize=24K";
544    mOptions.add(opt);
545
546    // Set the max jit code cache size.  Note: size of 0 will disable the JIT.
547    strcpy(jitcodecachesizeOptsBuf, "-Xjitcodecachesize:");
548    property_get("dalvik.vm.jit.codecachesize", jitcodecachesizeOptsBuf+19,  NULL);
549    if (jitcodecachesizeOptsBuf[19] != '\0') {
550      opt.optionString = jitcodecachesizeOptsBuf;
551      mOptions.add(opt);
552    }
553
554    strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
555    property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
556    if (heapgrowthlimitOptsBuf[20] != '\0') {
557        opt.optionString = heapgrowthlimitOptsBuf;
558        mOptions.add(opt);
559    }
560
561    strcpy(heapminfreeOptsBuf, "-XX:HeapMinFree=");
562    property_get("dalvik.vm.heapminfree", heapminfreeOptsBuf+16, "");
563    if (heapminfreeOptsBuf[16] != '\0') {
564        opt.optionString = heapminfreeOptsBuf;
565        mOptions.add(opt);
566    }
567
568    strcpy(heapmaxfreeOptsBuf, "-XX:HeapMaxFree=");
569    property_get("dalvik.vm.heapmaxfree", heapmaxfreeOptsBuf+16, "");
570    if (heapmaxfreeOptsBuf[16] != '\0') {
571        opt.optionString = heapmaxfreeOptsBuf;
572        mOptions.add(opt);
573    }
574
575    strcpy(heaptargetutilizationOptsBuf, "-XX:HeapTargetUtilization=");
576    property_get("dalvik.vm.heaptargetutilization", heaptargetutilizationOptsBuf+26, "");
577    if (heaptargetutilizationOptsBuf[26] != '\0') {
578        opt.optionString = heaptargetutilizationOptsBuf;
579        mOptions.add(opt);
580    }
581
582    property_get("ro.config.low_ram", propBuf, "");
583    if (strcmp(propBuf, "true") == 0) {
584      opt.optionString = "-XX:LowMemoryMode";
585      mOptions.add(opt);
586    }
587
588    strcpy(gctypeOptsBuf, "-Xgc:");
589    property_get("dalvik.vm.gctype", gctypeOptsBuf+5, "");
590    if (gctypeOptsBuf[5] != '\0') {
591        opt.optionString = gctypeOptsBuf;
592        mOptions.add(opt);
593    }
594
595    /*
596     * Enable or disable dexopt features, such as bytecode verification and
597     * calculation of register maps for precise GC.
598     */
599    property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
600    if (dexoptFlagsBuf[0] != '\0') {
601        const char* opc;
602        const char* val;
603
604        opc = strstr(dexoptFlagsBuf, "v=");     /* verification */
605        if (opc != NULL) {
606            switch (*(opc+2)) {
607            case 'n':   val = "-Xverify:none";      break;
608            case 'r':   val = "-Xverify:remote";    break;
609            case 'a':   val = "-Xverify:all";       break;
610            default:    val = NULL;                 break;
611            }
612
613            if (val != NULL) {
614                opt.optionString = val;
615                mOptions.add(opt);
616            }
617        }
618
619        opc = strstr(dexoptFlagsBuf, "o=");     /* optimization */
620        if (opc != NULL) {
621            switch (*(opc+2)) {
622            case 'n':   val = "-Xdexopt:none";      break;
623            case 'v':   val = "-Xdexopt:verified";  break;
624            case 'a':   val = "-Xdexopt:all";       break;
625            case 'f':   val = "-Xdexopt:full";      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, "m=y");    /* register map */
636        if (opc != NULL) {
637            opt.optionString = "-Xgenregmap";
638            mOptions.add(opt);
639
640            /* turn on precise GC while we're at it */
641            opt.optionString = "-Xgc:precise";
642            mOptions.add(opt);
643        }
644    }
645
646    /* enable debugging; set suspend=y to pause during VM init */
647    /* use android ADB transport */
648    opt.optionString =
649        "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
650    mOptions.add(opt);
651
652    ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
653    if (checkJni) {
654        /* extended JNI checking */
655        opt.optionString = "-Xcheck:jni";
656        mOptions.add(opt);
657
658        /* set a cap on JNI global references */
659        opt.optionString = "-Xjnigreflimit:2000";
660        mOptions.add(opt);
661
662        /* with -Xcheck:jni, this provides a JNI function call trace */
663        //opt.optionString = "-verbose:jni";
664        //mOptions.add(opt);
665    }
666
667    char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
668    property_get("dalvik.vm.lockprof.threshold", propBuf, "");
669    if (strlen(propBuf) > 0) {
670      strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
671      strcat(lockProfThresholdBuf, propBuf);
672      opt.optionString = lockProfThresholdBuf;
673      mOptions.add(opt);
674    }
675
676    /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
677    char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
678    property_get("dalvik.vm.jit.op", propBuf, "");
679    if (strlen(propBuf) > 0) {
680        strcpy(jitOpBuf, "-Xjitop:");
681        strcat(jitOpBuf, propBuf);
682        opt.optionString = jitOpBuf;
683        mOptions.add(opt);
684    }
685
686    /* Force interpreter-only mode for selected methods */
687    char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
688    property_get("dalvik.vm.jit.method", propBuf, "");
689    if (strlen(propBuf) > 0) {
690        strcpy(jitMethodBuf, "-Xjitmethod:");
691        strcat(jitMethodBuf, propBuf);
692        opt.optionString = jitMethodBuf;
693        mOptions.add(opt);
694    }
695
696    if (executionMode == kEMIntPortable) {
697        opt.optionString = "-Xint:portable";
698        mOptions.add(opt);
699    } else if (executionMode == kEMIntFast) {
700        opt.optionString = "-Xint:fast";
701        mOptions.add(opt);
702    } else if (executionMode == kEMJitCompiler) {
703        opt.optionString = "-Xint:jit";
704        mOptions.add(opt);
705    }
706
707    if (checkDexSum) {
708        /* perform additional DEX checksum tests */
709        opt.optionString = "-Xcheckdexsum";
710        mOptions.add(opt);
711    }
712
713    if (logStdio) {
714        /* convert stdout/stderr to log messages */
715        opt.optionString = "-Xlog-stdio";
716        mOptions.add(opt);
717    }
718
719    if (enableAssertBuf[4] != '\0') {
720        /* accept "all" to mean "all classes and packages" */
721        if (strcmp(enableAssertBuf+4, "all") == 0)
722            enableAssertBuf[3] = '\0';
723        ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
724        opt.optionString = enableAssertBuf;
725        mOptions.add(opt);
726    } else {
727        ALOGV("Assertions disabled\n");
728    }
729
730    if (jniOptsBuf[10] != '\0') {
731        ALOGI("JNI options: '%s'\n", jniOptsBuf);
732        opt.optionString = jniOptsBuf;
733        mOptions.add(opt);
734    }
735
736    if (stackTraceFileBuf[0] != '\0') {
737        static const char* stfOptName = "-Xstacktracefile:";
738
739        stackTraceFile = (char*) malloc(strlen(stfOptName) +
740            strlen(stackTraceFileBuf) +1);
741        strcpy(stackTraceFile, stfOptName);
742        strcat(stackTraceFile, stackTraceFileBuf);
743        opt.optionString = stackTraceFile;
744        mOptions.add(opt);
745    }
746
747    /* extra options; parse this late so it overrides others */
748    property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
749    parseExtraOpts(extraOptsBuf);
750
751    /* Set the properties for locale */
752    {
753        char langOption[sizeof("-Duser.language=") + 3];
754        char regionOption[sizeof("-Duser.region=") + 3];
755        strcpy(langOption, "-Duser.language=");
756        strcpy(regionOption, "-Duser.region=");
757        readLocale(langOption, regionOption);
758        opt.extraInfo = NULL;
759        opt.optionString = langOption;
760        mOptions.add(opt);
761        opt.optionString = regionOption;
762        mOptions.add(opt);
763    }
764
765    /*
766     * We don't have /tmp on the device, but we often have an SD card.  Apps
767     * shouldn't use this, but some test suites might want to exercise it.
768     */
769    opt.optionString = "-Djava.io.tmpdir=/sdcard";
770    mOptions.add(opt);
771
772    initArgs.version = JNI_VERSION_1_4;
773    initArgs.options = mOptions.editArray();
774    initArgs.nOptions = mOptions.size();
775    initArgs.ignoreUnrecognized = JNI_FALSE;
776
777    /*
778     * Initialize the VM.
779     *
780     * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
781     * If this call succeeds, the VM is ready, and we can start issuing
782     * JNI calls.
783     */
784    if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
785        ALOGE("JNI_CreateJavaVM failed\n");
786        goto bail;
787    }
788
789    result = 0;
790
791bail:
792    free(stackTraceFile);
793    return result;
794}
795
796char* AndroidRuntime::toSlashClassName(const char* className)
797{
798    char* result = strdup(className);
799    for (char* cp = result; *cp != '\0'; cp++) {
800        if (*cp == '.') {
801            *cp = '/';
802        }
803    }
804    return result;
805}
806
807/*
808 * Start the Android runtime.  This involves starting the virtual machine
809 * and calling the "static void main(String[] args)" method in the class
810 * named by "className".
811 *
812 * Passes the main function two arguments, the class name and the specified
813 * options string.
814 */
815void AndroidRuntime::start(const char* className, const char* options)
816{
817    ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
818            className != NULL ? className : "(unknown)");
819
820    /*
821     * 'startSystemServer == true' means runtime is obsolete and not run from
822     * init.rc anymore, so we print out the boot start event here.
823     */
824    if (strcmp(options, "start-system-server") == 0) {
825        /* track our progress through the boot sequence */
826        const int LOG_BOOT_PROGRESS_START = 3000;
827        LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
828                       ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
829    }
830
831    const char* rootDir = getenv("ANDROID_ROOT");
832    if (rootDir == NULL) {
833        rootDir = "/system";
834        if (!hasDir("/system")) {
835            LOG_FATAL("No root directory specified, and /android does not exist.");
836            return;
837        }
838        setenv("ANDROID_ROOT", rootDir, 1);
839    }
840
841    //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
842    //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
843
844    /* start the virtual machine */
845    JniInvocation jni_invocation;
846    jni_invocation.Init(NULL);
847    JNIEnv* env;
848    if (startVm(&mJavaVM, &env) != 0) {
849        return;
850    }
851    onVmCreated(env);
852
853    /*
854     * Register android functions.
855     */
856    if (startReg(env) < 0) {
857        ALOGE("Unable to register all android natives\n");
858        return;
859    }
860
861    /*
862     * We want to call main() with a String array with arguments in it.
863     * At present we have two arguments, the class name and an option string.
864     * Create an array to hold them.
865     */
866    jclass stringClass;
867    jobjectArray strArray;
868    jstring classNameStr;
869    jstring optionsStr;
870
871    stringClass = env->FindClass("java/lang/String");
872    assert(stringClass != NULL);
873    strArray = env->NewObjectArray(2, stringClass, NULL);
874    assert(strArray != NULL);
875    classNameStr = env->NewStringUTF(className);
876    assert(classNameStr != NULL);
877    env->SetObjectArrayElement(strArray, 0, classNameStr);
878    optionsStr = env->NewStringUTF(options);
879    env->SetObjectArrayElement(strArray, 1, optionsStr);
880
881    /*
882     * Start VM.  This thread becomes the main thread of the VM, and will
883     * not return until the VM exits.
884     */
885    char* slashClassName = toSlashClassName(className);
886    jclass startClass = env->FindClass(slashClassName);
887    if (startClass == NULL) {
888        ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
889        /* keep going */
890    } else {
891        jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
892            "([Ljava/lang/String;)V");
893        if (startMeth == NULL) {
894            ALOGE("JavaVM unable to find main() in '%s'\n", className);
895            /* keep going */
896        } else {
897            env->CallStaticVoidMethod(startClass, startMeth, strArray);
898
899#if 0
900            if (env->ExceptionCheck())
901                threadExitUncaughtException(env);
902#endif
903        }
904    }
905    free(slashClassName);
906
907    ALOGD("Shutting down VM\n");
908    if (mJavaVM->DetachCurrentThread() != JNI_OK)
909        ALOGW("Warning: unable to detach main thread\n");
910    if (mJavaVM->DestroyJavaVM() != 0)
911        ALOGW("Warning: VM did not shut down cleanly\n");
912}
913
914void AndroidRuntime::exit(int code)
915{
916    if (mExitWithoutCleanup) {
917        ALOGI("VM exiting with result code %d, cleanup skipped.", code);
918        ::_exit(code);
919    } else {
920        ALOGI("VM exiting with result code %d.", code);
921        onExit(code);
922        ::exit(code);
923    }
924}
925
926void AndroidRuntime::onVmCreated(JNIEnv* env)
927{
928    // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
929}
930
931/*
932 * Get the JNIEnv pointer for this thread.
933 *
934 * Returns NULL if the slot wasn't allocated or populated.
935 */
936/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
937{
938    JNIEnv* env;
939    JavaVM* vm = AndroidRuntime::getJavaVM();
940    assert(vm != NULL);
941
942    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
943        return NULL;
944    return env;
945}
946
947/*
948 * Makes the current thread visible to the VM.
949 *
950 * The JNIEnv pointer returned is only valid for the current thread, and
951 * thus must be tucked into thread-local storage.
952 */
953static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
954{
955    JavaVMAttachArgs args;
956    JavaVM* vm;
957    jint result;
958
959    vm = AndroidRuntime::getJavaVM();
960    assert(vm != NULL);
961
962    args.version = JNI_VERSION_1_4;
963    args.name = (char*) threadName;
964    args.group = NULL;
965
966    result = vm->AttachCurrentThread(pEnv, (void*) &args);
967    if (result != JNI_OK)
968        ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
969
970    return result;
971}
972
973/*
974 * Detach the current thread from the set visible to the VM.
975 */
976static int javaDetachThread(void)
977{
978    JavaVM* vm;
979    jint result;
980
981    vm = AndroidRuntime::getJavaVM();
982    assert(vm != NULL);
983
984    result = vm->DetachCurrentThread();
985    if (result != JNI_OK)
986        ALOGE("ERROR: thread detach failed\n");
987    return result;
988}
989
990/*
991 * When starting a native thread that will be visible from the VM, we
992 * bounce through this to get the right attach/detach action.
993 * Note that this function calls free(args)
994 */
995/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
996    void* start = ((void**)args)[0];
997    void* userData = ((void **)args)[1];
998    char* name = (char*) ((void **)args)[2];        // we own this storage
999    free(args);
1000    JNIEnv* env;
1001    int result;
1002
1003    /* hook us into the VM */
1004    if (javaAttachThread(name, &env) != JNI_OK)
1005        return -1;
1006
1007    /* start the thread running */
1008    result = (*(android_thread_func_t)start)(userData);
1009
1010    /* unhook us */
1011    javaDetachThread();
1012    free(name);
1013
1014    return result;
1015}
1016
1017/*
1018 * This is invoked from androidCreateThreadEtc() via the callback
1019 * set with androidSetCreateThreadFunc().
1020 *
1021 * We need to create the new thread in such a way that it gets hooked
1022 * into the VM before it really starts executing.
1023 */
1024/*static*/ int AndroidRuntime::javaCreateThreadEtc(
1025                                android_thread_func_t entryFunction,
1026                                void* userData,
1027                                const char* threadName,
1028                                int32_t threadPriority,
1029                                size_t threadStackSize,
1030                                android_thread_id_t* threadId)
1031{
1032    void** args = (void**) malloc(3 * sizeof(void*));   // javaThreadShell must free
1033    int result;
1034
1035    if (!threadName)
1036        threadName = "unnamed thread";
1037
1038    args[0] = (void*) entryFunction;
1039    args[1] = userData;
1040    args[2] = (void*) strdup(threadName);   // javaThreadShell must free
1041
1042    result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1043        threadName, threadPriority, threadStackSize, threadId);
1044    return result;
1045}
1046
1047/*
1048 * Create a thread that is visible from the VM.
1049 *
1050 * This is called from elsewhere in the library.
1051 */
1052/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
1053    void (*start)(void *), void* arg)
1054{
1055    android_thread_id_t threadId = 0;
1056    javaCreateThreadEtc((android_thread_func_t) start, arg, name,
1057        ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1058    return threadId;
1059}
1060
1061#if 0
1062static void quickTest(void* arg)
1063{
1064    const char* str = (const char*) arg;
1065
1066    printf("In quickTest: %s\n", str);
1067}
1068#endif
1069
1070#ifdef NDEBUG
1071    #define REG_JNI(name)      { name }
1072    struct RegJNIRec {
1073        int (*mProc)(JNIEnv*);
1074    };
1075#else
1076    #define REG_JNI(name)      { name, #name }
1077    struct RegJNIRec {
1078        int (*mProc)(JNIEnv*);
1079        const char* mName;
1080    };
1081#endif
1082
1083typedef void (*RegJAMProc)();
1084
1085static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1086{
1087    for (size_t i = 0; i < count; i++) {
1088        if (array[i].mProc(env) < 0) {
1089#ifndef NDEBUG
1090            ALOGD("----------!!! %s failed to load\n", array[i].mName);
1091#endif
1092            return -1;
1093        }
1094    }
1095    return 0;
1096}
1097
1098static void register_jam_procs(const RegJAMProc array[], size_t count)
1099{
1100    for (size_t i = 0; i < count; i++) {
1101        array[i]();
1102    }
1103}
1104
1105static const RegJNIRec gRegJNI[] = {
1106    REG_JNI(register_android_debug_JNITest),
1107    REG_JNI(register_com_android_internal_os_RuntimeInit),
1108    REG_JNI(register_android_os_SystemClock),
1109    REG_JNI(register_android_util_EventLog),
1110    REG_JNI(register_android_util_Log),
1111    REG_JNI(register_android_util_FloatMath),
1112    REG_JNI(register_android_text_format_Time),
1113    REG_JNI(register_android_content_AssetManager),
1114    REG_JNI(register_android_content_StringBlock),
1115    REG_JNI(register_android_content_XmlBlock),
1116    REG_JNI(register_android_emoji_EmojiFactory),
1117    REG_JNI(register_android_text_AndroidCharacter),
1118    REG_JNI(register_android_text_AndroidBidi),
1119    REG_JNI(register_android_view_InputDevice),
1120    REG_JNI(register_android_view_KeyCharacterMap),
1121    REG_JNI(register_android_os_Process),
1122    REG_JNI(register_android_os_SystemProperties),
1123    REG_JNI(register_android_os_Binder),
1124    REG_JNI(register_android_os_Parcel),
1125    REG_JNI(register_android_nio_utils),
1126    REG_JNI(register_android_graphics_Graphics),
1127    REG_JNI(register_android_view_DisplayEventReceiver),
1128    REG_JNI(register_android_view_DisplayList),
1129    REG_JNI(register_android_view_GraphicBuffer),
1130    REG_JNI(register_android_view_GLES20Canvas),
1131    REG_JNI(register_android_view_GLRenderer),
1132    REG_JNI(register_android_view_ThreadedRenderer),
1133    REG_JNI(register_android_view_RemoteGLRenderer),
1134    REG_JNI(register_android_view_Surface),
1135    REG_JNI(register_android_view_SurfaceControl),
1136    REG_JNI(register_android_view_SurfaceSession),
1137    REG_JNI(register_android_view_TextureView),
1138    REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1139    REG_JNI(register_com_google_android_gles_jni_GLImpl),
1140    REG_JNI(register_android_opengl_jni_EGL14),
1141    REG_JNI(register_android_opengl_jni_EGLExt),
1142    REG_JNI(register_android_opengl_jni_GLES10),
1143    REG_JNI(register_android_opengl_jni_GLES10Ext),
1144    REG_JNI(register_android_opengl_jni_GLES11),
1145    REG_JNI(register_android_opengl_jni_GLES11Ext),
1146    REG_JNI(register_android_opengl_jni_GLES20),
1147    REG_JNI(register_android_opengl_jni_GLES30),
1148
1149    REG_JNI(register_android_graphics_Bitmap),
1150    REG_JNI(register_android_graphics_BitmapFactory),
1151    REG_JNI(register_android_graphics_BitmapRegionDecoder),
1152    REG_JNI(register_android_graphics_Camera),
1153    REG_JNI(register_android_graphics_CreateJavaOutputStreamAdaptor),
1154    REG_JNI(register_android_graphics_Canvas),
1155    REG_JNI(register_android_graphics_ColorFilter),
1156    REG_JNI(register_android_graphics_DrawFilter),
1157    REG_JNI(register_android_graphics_Interpolator),
1158    REG_JNI(register_android_graphics_LayerRasterizer),
1159    REG_JNI(register_android_graphics_MaskFilter),
1160    REG_JNI(register_android_graphics_Matrix),
1161    REG_JNI(register_android_graphics_Movie),
1162    REG_JNI(register_android_graphics_NinePatch),
1163    REG_JNI(register_android_graphics_Paint),
1164    REG_JNI(register_android_graphics_Path),
1165    REG_JNI(register_android_graphics_PathMeasure),
1166    REG_JNI(register_android_graphics_PathEffect),
1167    REG_JNI(register_android_graphics_Picture),
1168    REG_JNI(register_android_graphics_PorterDuff),
1169    REG_JNI(register_android_graphics_Rasterizer),
1170    REG_JNI(register_android_graphics_Region),
1171    REG_JNI(register_android_graphics_Shader),
1172    REG_JNI(register_android_graphics_SurfaceTexture),
1173    REG_JNI(register_android_graphics_Typeface),
1174    REG_JNI(register_android_graphics_Xfermode),
1175    REG_JNI(register_android_graphics_YuvImage),
1176    REG_JNI(register_android_graphics_pdf_PdfDocument),
1177
1178    REG_JNI(register_android_database_CursorWindow),
1179    REG_JNI(register_android_database_SQLiteConnection),
1180    REG_JNI(register_android_database_SQLiteGlobal),
1181    REG_JNI(register_android_database_SQLiteDebug),
1182    REG_JNI(register_android_os_Debug),
1183    REG_JNI(register_android_os_FileObserver),
1184    REG_JNI(register_android_os_MessageQueue),
1185    REG_JNI(register_android_os_SELinux),
1186    REG_JNI(register_android_os_Trace),
1187    REG_JNI(register_android_os_UEventObserver),
1188    REG_JNI(register_android_net_LocalSocketImpl),
1189    REG_JNI(register_android_net_NetworkUtils),
1190    REG_JNI(register_android_net_TrafficStats),
1191    REG_JNI(register_android_net_wifi_WifiNative),
1192    REG_JNI(register_android_os_MemoryFile),
1193    REG_JNI(register_com_android_internal_os_ZygoteInit),
1194    REG_JNI(register_android_hardware_Camera),
1195    REG_JNI(register_android_hardware_camera2_CameraMetadata),
1196    REG_JNI(register_android_hardware_SensorManager),
1197    REG_JNI(register_android_hardware_SerialPort),
1198    REG_JNI(register_android_hardware_UsbDevice),
1199    REG_JNI(register_android_hardware_UsbDeviceConnection),
1200    REG_JNI(register_android_hardware_UsbRequest),
1201    REG_JNI(register_android_media_AudioRecord),
1202    REG_JNI(register_android_media_AudioSystem),
1203    REG_JNI(register_android_media_AudioTrack),
1204    REG_JNI(register_android_media_JetPlayer),
1205    REG_JNI(register_android_media_RemoteDisplay),
1206    REG_JNI(register_android_media_ToneGenerator),
1207
1208    REG_JNI(register_android_opengl_classes),
1209    REG_JNI(register_android_server_NetworkManagementSocketTagger),
1210    REG_JNI(register_android_server_Watchdog),
1211    REG_JNI(register_android_ddm_DdmHandleNativeHeap),
1212    REG_JNI(register_android_backup_BackupDataInput),
1213    REG_JNI(register_android_backup_BackupDataOutput),
1214    REG_JNI(register_android_backup_FileBackupHelperBase),
1215    REG_JNI(register_android_backup_BackupHelperDispatcher),
1216    REG_JNI(register_android_app_backup_FullBackup),
1217    REG_JNI(register_android_app_ActivityThread),
1218    REG_JNI(register_android_app_NativeActivity),
1219    REG_JNI(register_android_view_InputChannel),
1220    REG_JNI(register_android_view_InputEventReceiver),
1221    REG_JNI(register_android_view_InputEventSender),
1222    REG_JNI(register_android_view_InputQueue),
1223    REG_JNI(register_android_view_KeyEvent),
1224    REG_JNI(register_android_view_MotionEvent),
1225    REG_JNI(register_android_view_PointerIcon),
1226    REG_JNI(register_android_view_VelocityTracker),
1227
1228    REG_JNI(register_android_content_res_ObbScanner),
1229    REG_JNI(register_android_content_res_Configuration),
1230
1231    REG_JNI(register_android_animation_PropertyValuesHolder),
1232    REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
1233    REG_JNI(register_com_android_internal_net_NetworkStatsFactory),
1234};
1235
1236/*
1237 * Register android native functions with the VM.
1238 */
1239/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1240{
1241    /*
1242     * This hook causes all future threads created in this process to be
1243     * attached to the JavaVM.  (This needs to go away in favor of JNI
1244     * Attach calls.)
1245     */
1246    androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1247
1248    ALOGV("--- registering native functions ---\n");
1249
1250    /*
1251     * Every "register" function calls one or more things that return
1252     * a local reference (e.g. FindClass).  Because we haven't really
1253     * started the VM yet, they're all getting stored in the base frame
1254     * and never released.  Use Push/Pop to manage the storage.
1255     */
1256    env->PushLocalFrame(200);
1257
1258    if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1259        env->PopLocalFrame(NULL);
1260        return -1;
1261    }
1262    env->PopLocalFrame(NULL);
1263
1264    //createJavaThread("fubar", quickTest, (void*) "hello");
1265
1266    return 0;
1267}
1268
1269AndroidRuntime* AndroidRuntime::getRuntime()
1270{
1271    return gCurRuntime;
1272}
1273
1274/**
1275 * Used by WithFramework to register native functions.
1276 */
1277extern "C"
1278jint Java_com_android_internal_util_WithFramework_registerNatives(
1279        JNIEnv* env, jclass clazz) {
1280    return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1281}
1282
1283/**
1284 * Used by LoadClass to register native functions.
1285 */
1286extern "C"
1287jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1288    return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1289}
1290
1291}   // namespace android
1292