blob: a61901b62c7208c743cc0bcdce52d028da1bee67 [file] [log] [blame]
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016
17#define LOG_TAG "AndroidRuntime"
18//#define LOG_NDEBUG 0
19
20#include <android_runtime/AndroidRuntime.h>
Mathias Agopian07952722009-05-19 19:08:10 -070021#include <binder/IBinder.h>
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -080022#include <binder/IPCThreadState.h>
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/Log.h>
25#include <utils/misc.h>
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/Parcel.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <utils/threads.h>
28#include <cutils/properties.h>
29
30#include <SkGraphics.h>
31#include <SkImageDecoder.h>
Mike Reedfc8db532009-04-27 11:43:30 -040032#include <SkImageRef_GlobalPool.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34#include "jni.h"
35#include "JNIHelp.h"
Brian Carlstrom9f8203a2013-06-19 13:49:36 -070036#include "JniInvocation.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050extern 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*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080054extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055extern int register_android_graphics_Camera(JNIEnv* env);
Leon Scroggins IIId0d7eaf2013-09-06 16:46:57 -040056extern int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057extern 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064extern int register_android_graphics_Shader(JNIEnv* env);
65extern int register_android_graphics_Typeface(JNIEnv* env);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080066extern int register_android_graphics_YuvImage(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
69extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
Thomas Tafertshofer6b1e8382012-07-03 13:37:35 -070070extern int register_android_opengl_jni_EGL14(JNIEnv* env);
Jesse Hall237c2b82013-05-06 11:36:57 -070071extern int register_android_opengl_jni_EGLExt(JNIEnv* env);
Jack Palevich1c4907e2009-04-13 16:22:25 -070072extern 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);
Jack Palevich560814f2009-11-19 16:34:55 +080076extern int register_android_opengl_jni_GLES20(JNIEnv* env);
Jesse Halld877efe2013-04-29 15:59:35 -070077extern int register_android_opengl_jni_GLES30(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
79extern int register_android_hardware_Camera(JNIEnv *env);
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -070080extern int register_android_hardware_camera2_CameraMetadata(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081extern int register_android_hardware_SensorManager(JNIEnv *env);
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040082extern int register_android_hardware_SerialPort(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050083extern int register_android_hardware_UsbDevice(JNIEnv *env);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050084extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050085extern int register_android_hardware_UsbRequest(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093extern 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);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700106extern int register_android_emoji_EmojiFactory(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107extern 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);
Jeff Brownfbf09772011-01-16 14:06:57 -0800117extern int register_android_graphics_Region(JNIEnv* env);
Jamie Gennisaa0ce332011-01-06 17:04:26 -0800118extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119extern int register_android_graphics_Xfermode(JNIEnv* env);
Svetoslav6811f4e2013-09-18 15:58:28 -0700120extern int register_android_graphics_pdf_PdfDocument(JNIEnv* env);
Jeff Brown0a0a1242011-12-02 02:25:22 -0800121extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
Romain Guy3b748a42013-04-17 18:54:38 -0700122extern int register_android_view_GraphicBuffer(JNIEnv* env);
Chet Haasea1cff502012-02-21 13:43:44 -0800123extern int register_android_view_GLES20DisplayList(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700124extern int register_android_view_GLES20Canvas(JNIEnv* env);
Romain Guya9582652011-11-10 14:20:10 -0800125extern int register_android_view_HardwareRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126extern int register_android_view_Surface(JNIEnv* env);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800127extern int register_android_view_SurfaceControl(JNIEnv* env);
Jeff Brown64a55af2012-08-26 02:47:39 -0700128extern int register_android_view_SurfaceSession(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700129extern int register_android_view_TextureView(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130extern int register_android_database_CursorWindow(JNIEnv* env);
Jeff Browne5360fb2011-10-31 17:48:13 -0700131extern int register_android_database_SQLiteConnection(JNIEnv* env);
132extern int register_android_database_SQLiteGlobal(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133extern int register_android_database_SQLiteDebug(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134extern int register_android_nio_utils(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135extern int register_android_text_format_Time(JNIEnv* env);
136extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700137extern int register_android_os_MessageQueue(JNIEnv* env);
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800138extern int register_android_os_Parcel(JNIEnv* env);
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500139extern int register_android_os_SELinux(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141extern int register_android_os_SystemClock(JNIEnv* env);
Jeff Brown481c1572012-03-09 14:41:15 -0800142extern int register_android_os_Trace(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143extern int register_android_os_FileObserver(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144extern int register_android_os_UEventObserver(JNIEnv* env);
145extern int register_android_os_MemoryFile(JNIEnv* env);
146extern int register_android_net_LocalSocketImpl(JNIEnv* env);
147extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700148extern int register_android_net_TrafficStats(JNIEnv* env);
Elliott Hughesa11d5742013-08-09 14:59:39 -0700149extern int register_android_net_wifi_WifiNative(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150extern int register_android_text_AndroidCharacter(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800151extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152extern int register_android_opengl_classes(JNIEnv *env);
JP Abgrall98a4f7e2011-09-02 15:36:33 -0700153extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
Christopher Tateecaa7b42010-06-04 14:55:02 -0700154extern int register_android_server_Watchdog(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
156extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700157extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700158extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700159extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400160extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Christopher Tate4a627c72011-04-01 14:43:32 -0700161extern int register_android_app_backup_FullBackup(JNIEnv *env);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700162extern int register_android_app_ActivityThread(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700163extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Browncbad9762012-09-04 21:57:59 -0700164extern int register_android_media_RemoteDisplay(JNIEnv *env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700165extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700166extern int register_android_view_InputDevice(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800167extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brownc28867a2013-03-26 15:42:39 -0700168extern int register_android_view_InputEventSender(JNIEnv* env);
Michael Wrighta44dd262013-04-10 21:12:00 -0700169extern int register_android_view_InputQueue(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700170extern int register_android_view_KeyCharacterMap(JNIEnv *env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700171extern int register_android_view_KeyEvent(JNIEnv* env);
172extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700173extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700174extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700175extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700176extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700177extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700178extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800179extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env);
Narayan Kamath973b4662014-03-31 13:41:26 +0100180extern int register_com_android_internal_os_Zygote(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
182static AndroidRuntime* gCurRuntime = NULL;
183
184static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
185{
186 if (jniThrowException(env, exc, msg) != 0)
187 assert(false);
188}
189
190/*
191 * Code written in the Java Programming Language calls here from main().
192 */
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700193static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194{
195 gCurRuntime->onStarted();
196}
197
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700198static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199{
200 gCurRuntime->onZygoteInit();
201}
202
Jeff Brown4280c4a2012-03-15 17:48:02 -0700203static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv* env,
204 jobject clazz, jboolean exitWithoutCleanup)
205{
206 gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup);
207}
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209/*
210 * JNI registration.
211 */
212static JNINativeMethod gMethods[] = {
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700213 { "nativeFinishInit", "()V",
214 (void*) com_android_internal_os_RuntimeInit_nativeFinishInit },
215 { "nativeZygoteInit", "()V",
216 (void*) com_android_internal_os_RuntimeInit_nativeZygoteInit },
Jeff Brown4280c4a2012-03-15 17:48:02 -0700217 { "nativeSetExitWithoutCleanup", "(Z)V",
218 (void*) com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219};
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
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000231AndroidRuntime::AndroidRuntime(char* argBlockStart, const size_t argBlockLength) :
232 mExitWithoutCleanup(false),
233 mArgBlockStart(argBlockStart),
234 mArgBlockLength(argBlockLength)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700236 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 // this sets our preference for 16bit images during decode
238 // in case the src is opaque and 24bit
239 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400240 // This cache is shared between browser native images, and java "purgeable"
241 // bitmaps. This globalpool is for images that do not either use the java
242 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
243 // java call site.
244 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
245 // There is also a global font cache, but its budget is specified in code
246 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
248 // Pre-allocate enough space to hold a fair number of options.
249 mOptions.setCapacity(20);
250
251 assert(gCurRuntime == NULL); // one per process
252 gCurRuntime = this;
253}
254
255AndroidRuntime::~AndroidRuntime()
256{
257 SkGraphics::Term();
258}
259
260/*
261 * Register native methods using JNI.
262 */
263/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
264 const char* className, const JNINativeMethod* gMethods, int numMethods)
265{
266 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
267}
268
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000269void AndroidRuntime::setArgv0(const char* argv0) {
270 strlcpy(mArgBlockStart, argv0, mArgBlockLength);
271}
272
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100273status_t AndroidRuntime::callMain(const String8& className, jclass clazz,
274 const Vector<String8>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275{
276 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 jmethodID methodId;
278
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100279 ALOGD("Calling main entry %s", className.string());
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700282 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 return UNKNOWN_ERROR;
284 }
285
286 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
287 if (methodId == NULL) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100288 ALOGE("ERROR: could not find method %s.main(String[])\n", className.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 return UNKNOWN_ERROR;
290 }
291
292 /*
293 * We want to call main() with a String array with our arguments in it.
294 * Create an array and populate it.
295 */
296 jclass stringClass;
297 jobjectArray strArray;
298
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100299 const size_t numArgs = args.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 stringClass = env->FindClass("java/lang/String");
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100301 strArray = env->NewObjectArray(numArgs, stringClass, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100303 for (size_t i = 0; i < numArgs; i++) {
304 jstring argStr = env->NewStringUTF(args[i].string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 env->SetObjectArrayElement(strArray, i, argStr);
306 }
307
308 env->CallStaticVoidMethod(clazz, methodId, strArray);
309 return NO_ERROR;
310}
311
312/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 * The VM calls this through the "exit" hook.
314 */
315static void runtime_exit(int code)
316{
Jeff Brown4280c4a2012-03-15 17:48:02 -0700317 gCurRuntime->exit(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318}
319
320/*
321 * The VM calls this through the "vfprintf" hook.
322 *
323 * We ignore "fp" and just write the results to the log file.
324 */
325static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
326{
327 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
328}
329
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800330/**
331 * The VM calls this when mutex contention debugging is enabled to
332 * determine whether or not the blocked thread was a "sensitive thread"
333 * for user responsiveness/smoothess.
334 *
335 * Our policy for this is whether or not we're tracing any StrictMode
336 * events on this thread (which we might've inherited via Binder calls
337 * into us)
338 */
339static bool runtime_isSensitiveThread() {
340 IPCThreadState* state = IPCThreadState::selfOrNull();
341 return state && state->getStrictModePolicy() != 0;
342}
343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344
345/**
346 * Add VM arguments to the to-be-executed VM
347 * Stops at first non '-' argument (also stops at an argument of '--')
348 * Returns the number of args consumed
349 */
350int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
351{
352 int i;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 for (i = 0; i<argc; i++) {
355 if (argv[i][0] != '-') {
356 return i;
357 }
358 if (argv[i][1] == '-' && argv[i][2] == 0) {
359 return i+1;
360 }
361
362 JavaVMOption opt;
363 memset(&opt, 0, sizeof(opt));
364 opt.optionString = (char*)argv[i];
365 mOptions.add(opt);
366 }
367 return i;
368}
369
370static int hasDir(const char* dir)
371{
372 struct stat s;
373 int res = stat(dir, &s);
374 if (res == 0) {
375 return S_ISDIR(s.st_mode);
376 }
377 return 0;
378}
379
380/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * Read the persistent locale.
382 */
383static void readLocale(char* language, char* region)
384{
385 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 property_get("persist.sys.language", propLang, "");
388 property_get("persist.sys.country", propRegn, "");
389 if (*propLang == 0 && *propRegn == 0) {
390 /* Set to ro properties, default is en_US */
391 property_get("ro.product.locale.language", propLang, "en");
392 property_get("ro.product.locale.region", propRegn, "US");
393 }
394 strncat(language, propLang, 2);
395 strncat(region, propRegn, 2);
Steve Block5baa3a62011-12-20 16:23:08 +0000396 //ALOGD("language=%s region=%s\n", language, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397}
398
Andy McFaddenf70188a2009-03-31 15:52:13 -0700399/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700400 * Parse a property containing space-separated options that should be
401 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
402 *
403 * This will cut up "extraOptsBuf" as we chop it into individual options.
404 *
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800405 * If "quotingArg" is non-null, it is passed before each extra option in mOptions.
406 *
Andy McFaddene4d81f22010-07-14 16:02:20 -0700407 * Adds the strings, if any, to mOptions.
408 */
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800409void AndroidRuntime::parseExtraOpts(char* extraOptsBuf, const char* quotingArg)
Andy McFaddene4d81f22010-07-14 16:02:20 -0700410{
411 JavaVMOption opt;
Andy McFaddene4d81f22010-07-14 16:02:20 -0700412 memset(&opt, 0, sizeof(opt));
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800413 char* start = extraOptsBuf;
414 char* end = NULL;
Andy McFaddene4d81f22010-07-14 16:02:20 -0700415 while (*start != '\0') {
416 while (*start == ' ') /* skip leading whitespace */
417 start++;
418 if (*start == '\0') /* was trailing ws, bail */
419 break;
420
421 end = start+1;
422 while (*end != ' ' && *end != '\0') /* find end of token */
423 end++;
424 if (*end == ' ')
425 *end++ = '\0'; /* mark end, advance to indicate more */
426
427 opt.optionString = start;
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800428 if (quotingArg != NULL) {
429 JavaVMOption quotingOpt;
430 quotingOpt.optionString = quotingArg;
431 mOptions.add(quotingOpt);
432 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700433 mOptions.add(opt);
434 start = end;
435 }
436}
437
438/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700439 * Start the Dalvik Virtual Machine.
440 *
441 * Various arguments, most determined by system properties, are passed in.
442 * The "mOptions" vector is updated.
443 *
Dave Allison07a1e232014-03-14 08:54:33 -0700444 * CAUTION: when adding options in here, be careful not to put the
445 * char buffer inside a nested scope. Adding the buffer to the
446 * options using mOptions.add() does not copy the buffer, so if the
447 * buffer goes out of scope the option may be overwritten. It's best
448 * to put the buffer at the top of the function so that it is more
449 * unlikely that someone will surround it in a scope at a later time
450 * and thus introduce a bug.
451 *
Andy McFaddenf70188a2009-03-31 15:52:13 -0700452 * Returns 0 on success.
453 */
454int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700456 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 JavaVMInitArgs initArgs;
458 JavaVMOption opt;
459 char propBuf[PROPERTY_VALUE_MAX];
460 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
461 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
462 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
463 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800464 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700465 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800466 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Ian Rogers53250102012-09-23 16:38:03 -0700467 char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
468 char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
Mathieu Chartier7e4fdec2013-12-02 15:46:51 -0800469 char gctypeOptsBuf[sizeof("-Xgc:")-1 + PROPERTY_VALUE_MAX];
Mathieu Chartierc01936a2014-05-14 15:44:49 -0700470 char backgroundgcOptsBuf[sizeof("-XX:BackgroundGC=")-1 + PROPERTY_VALUE_MAX];
Ian Rogers53250102012-09-23 16:38:03 -0700471 char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
buzbeeb63de6d2013-05-24 13:14:08 -0700472 char jitcodecachesizeOptsBuf[sizeof("-Xjitcodecachesize:")-1 + PROPERTY_VALUE_MAX];
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800473 char dalvikVmLibBuf[PROPERTY_VALUE_MAX];
474 char dex2oatFlagsBuf[PROPERTY_VALUE_MAX];
475 char dex2oatImageFlagsBuf[PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700476 char extraOptsBuf[PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700479 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700481 enum {
482 kEMDefault,
483 kEMIntPortable,
484 kEMIntFast,
Ben Cheng52b0e732009-06-19 13:31:12 -0700485 kEMJitCompiler,
Ben Cheng52b0e732009-06-19 13:31:12 -0700486 } executionMode = kEMDefault;
Dave Allison07a1e232014-03-14 08:54:33 -0700487 char profile_period[sizeof("-Xprofile-period:") + PROPERTY_VALUE_MAX];
488 char profile_duration[sizeof("-Xprofile-duration:") + PROPERTY_VALUE_MAX];
489 char profile_interval[sizeof("-Xprofile-interval:") + PROPERTY_VALUE_MAX];
490 char profile_backoff[sizeof("-Xprofile-backoff:") + PROPERTY_VALUE_MAX];
Calin Juravlee279cd62014-06-19 18:51:47 +0100491 char profile_top_k_threshold[sizeof("-Xprofile-top-k-threshold:") + PROPERTY_VALUE_MAX];
492 char profile_top_k_change_threshold[sizeof("-Xprofile-top-k-change-threshold:") + PROPERTY_VALUE_MAX];
Dave Allison07a1e232014-03-14 08:54:33 -0700493 char langOption[sizeof("-Duser.language=") + 3];
494 char regionOption[sizeof("-Duser.region=") + 3];
495 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
496 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
497 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
499 property_get("dalvik.vm.checkjni", propBuf, "");
500 if (strcmp(propBuf, "true") == 0) {
501 checkJni = true;
502 } else if (strcmp(propBuf, "false") != 0) {
503 /* property is neither true nor false; fall back on kernel parameter */
504 property_get("ro.kernel.android.checkjni", propBuf, "");
505 if (propBuf[0] == '1') {
506 checkJni = true;
507 }
508 }
509
510 property_get("dalvik.vm.execution-mode", propBuf, "");
511 if (strcmp(propBuf, "int:portable") == 0) {
512 executionMode = kEMIntPortable;
513 } else if (strcmp(propBuf, "int:fast") == 0) {
514 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700515 } else if (strcmp(propBuf, "int:jit") == 0) {
516 executionMode = kEMJitCompiler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 }
518
519 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
520
Andy McFaddene2b23e12009-04-03 11:09:46 -0700521 property_get("dalvik.vm.check-dex-sum", propBuf, "");
522 if (strcmp(propBuf, "true") == 0) {
523 checkDexSum = true;
524 }
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 property_get("log.redirect-stdio", propBuf, "");
527 if (strcmp(propBuf, "true") == 0) {
528 logStdio = true;
529 }
530
531 strcpy(enableAssertBuf, "-ea:");
532 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
533
534 strcpy(jniOptsBuf, "-Xjniopts:");
535 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 /* route exit() to our handler */
538 opt.extraInfo = (void*) runtime_exit;
539 opt.optionString = "exit";
540 mOptions.add(opt);
541
542 /* route fprintf() to our handler */
543 opt.extraInfo = (void*) runtime_vfprintf;
544 opt.optionString = "vfprintf";
545 mOptions.add(opt);
546
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800547 /* register the framework-specific "is sensitive thread" hook */
548 opt.extraInfo = (void*) runtime_isSensitiveThread;
549 opt.optionString = "sensitiveThread";
550 mOptions.add(opt);
551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 opt.extraInfo = NULL;
553
554 /* enable verbose; standard options are { jni, gc, class } */
555 //options[curOpt++].optionString = "-verbose:jni";
556 opt.optionString = "-verbose:gc";
557 mOptions.add(opt);
558 //options[curOpt++].optionString = "-verbose:class";
559
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800560 /*
561 * The default starting and maximum size of the heap. Larger
562 * values should be specified in a product property override.
563 */
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800564 strcpy(heapstartsizeOptsBuf, "-Xms");
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800565 property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800566 opt.optionString = heapstartsizeOptsBuf;
567 mOptions.add(opt);
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700568 strcpy(heapsizeOptsBuf, "-Xmx");
569 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700570 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 mOptions.add(opt);
572
Elliott Hughes3f177d72012-04-12 16:02:56 -0700573 // Increase the main thread's interpreter stack size for bug 6315322.
574 opt.optionString = "-XX:mainThreadStackSize=24K";
575 mOptions.add(opt);
576
buzbeeb63de6d2013-05-24 13:14:08 -0700577 // Set the max jit code cache size. Note: size of 0 will disable the JIT.
578 strcpy(jitcodecachesizeOptsBuf, "-Xjitcodecachesize:");
579 property_get("dalvik.vm.jit.codecachesize", jitcodecachesizeOptsBuf+19, NULL);
580 if (jitcodecachesizeOptsBuf[19] != '\0') {
581 opt.optionString = jitcodecachesizeOptsBuf;
582 mOptions.add(opt);
583 }
584
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800585 strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
586 property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
587 if (heapgrowthlimitOptsBuf[20] != '\0') {
588 opt.optionString = heapgrowthlimitOptsBuf;
589 mOptions.add(opt);
590 }
591
Ian Rogers53250102012-09-23 16:38:03 -0700592 strcpy(heapminfreeOptsBuf, "-XX:HeapMinFree=");
593 property_get("dalvik.vm.heapminfree", heapminfreeOptsBuf+16, "");
594 if (heapminfreeOptsBuf[16] != '\0') {
595 opt.optionString = heapminfreeOptsBuf;
596 mOptions.add(opt);
597 }
598
599 strcpy(heapmaxfreeOptsBuf, "-XX:HeapMaxFree=");
600 property_get("dalvik.vm.heapmaxfree", heapmaxfreeOptsBuf+16, "");
601 if (heapmaxfreeOptsBuf[16] != '\0') {
602 opt.optionString = heapmaxfreeOptsBuf;
603 mOptions.add(opt);
604 }
605
606 strcpy(heaptargetutilizationOptsBuf, "-XX:HeapTargetUtilization=");
607 property_get("dalvik.vm.heaptargetutilization", heaptargetutilizationOptsBuf+26, "");
Ian Rogers7fef2b82012-10-01 14:54:24 -0700608 if (heaptargetutilizationOptsBuf[26] != '\0') {
Ian Rogers53250102012-09-23 16:38:03 -0700609 opt.optionString = heaptargetutilizationOptsBuf;
610 mOptions.add(opt);
611 }
612
Mathieu Chartiere6c22412013-08-20 17:16:03 -0700613 property_get("ro.config.low_ram", propBuf, "");
614 if (strcmp(propBuf, "true") == 0) {
615 opt.optionString = "-XX:LowMemoryMode";
616 mOptions.add(opt);
617 }
618
Mathieu Chartier7e4fdec2013-12-02 15:46:51 -0800619 strcpy(gctypeOptsBuf, "-Xgc:");
620 property_get("dalvik.vm.gctype", gctypeOptsBuf+5, "");
621 if (gctypeOptsBuf[5] != '\0') {
622 opt.optionString = gctypeOptsBuf;
623 mOptions.add(opt);
624 }
625
Mathieu Chartierc01936a2014-05-14 15:44:49 -0700626 strcpy(backgroundgcOptsBuf, "-XX:BackgroundGC=");
627 property_get("dalvik.vm.backgroundgctype", backgroundgcOptsBuf+sizeof("-XX:BackgroundGC=")-1, "");
628 if (backgroundgcOptsBuf[sizeof("-XX:BackgroundGC=")-1] != '\0') {
629 opt.optionString = backgroundgcOptsBuf;
630 mOptions.add(opt);
631 }
632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 /*
634 * Enable or disable dexopt features, such as bytecode verification and
635 * calculation of register maps for precise GC.
636 */
637 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
638 if (dexoptFlagsBuf[0] != '\0') {
639 const char* opc;
640 const char* val;
641
642 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
643 if (opc != NULL) {
644 switch (*(opc+2)) {
645 case 'n': val = "-Xverify:none"; break;
646 case 'r': val = "-Xverify:remote"; break;
647 case 'a': val = "-Xverify:all"; break;
648 default: val = NULL; break;
649 }
650
651 if (val != NULL) {
652 opt.optionString = val;
653 mOptions.add(opt);
654 }
655 }
656
657 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
658 if (opc != NULL) {
659 switch (*(opc+2)) {
660 case 'n': val = "-Xdexopt:none"; break;
661 case 'v': val = "-Xdexopt:verified"; break;
662 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700663 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 default: val = NULL; break;
665 }
666
667 if (val != NULL) {
668 opt.optionString = val;
669 mOptions.add(opt);
670 }
671 }
672
673 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
674 if (opc != NULL) {
675 opt.optionString = "-Xgenregmap";
676 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700677
678 /* turn on precise GC while we're at it */
679 opt.optionString = "-Xgc:precise";
680 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682 }
683
684 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 /* use android ADB transport */
686 opt.optionString =
687 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 mOptions.add(opt);
689
Steve Block5baa3a62011-12-20 16:23:08 +0000690 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 if (checkJni) {
692 /* extended JNI checking */
693 opt.optionString = "-Xcheck:jni";
694 mOptions.add(opt);
695
696 /* set a cap on JNI global references */
697 opt.optionString = "-Xjnigreflimit:2000";
698 mOptions.add(opt);
699
700 /* with -Xcheck:jni, this provides a JNI function call trace */
701 //opt.optionString = "-verbose:jni";
702 //mOptions.add(opt);
703 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700704
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700705 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
706 if (strlen(propBuf) > 0) {
707 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
708 strcat(lockProfThresholdBuf, propBuf);
709 opt.optionString = lockProfThresholdBuf;
710 mOptions.add(opt);
711 }
712
Ben Cheng52b0e732009-06-19 13:31:12 -0700713 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
Ben Cheng52b0e732009-06-19 13:31:12 -0700714 property_get("dalvik.vm.jit.op", propBuf, "");
715 if (strlen(propBuf) > 0) {
716 strcpy(jitOpBuf, "-Xjitop:");
717 strcat(jitOpBuf, propBuf);
718 opt.optionString = jitOpBuf;
719 mOptions.add(opt);
720 }
721
Ben Cheng52b0e732009-06-19 13:31:12 -0700722 /* Force interpreter-only mode for selected methods */
Ben Cheng52b0e732009-06-19 13:31:12 -0700723 property_get("dalvik.vm.jit.method", propBuf, "");
724 if (strlen(propBuf) > 0) {
725 strcpy(jitMethodBuf, "-Xjitmethod:");
726 strcat(jitMethodBuf, propBuf);
727 opt.optionString = jitMethodBuf;
728 mOptions.add(opt);
729 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 if (executionMode == kEMIntPortable) {
732 opt.optionString = "-Xint:portable";
733 mOptions.add(opt);
734 } else if (executionMode == kEMIntFast) {
735 opt.optionString = "-Xint:fast";
736 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700737 } else if (executionMode == kEMJitCompiler) {
738 opt.optionString = "-Xint:jit";
739 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700741
742 if (checkDexSum) {
743 /* perform additional DEX checksum tests */
744 opt.optionString = "-Xcheckdexsum";
745 mOptions.add(opt);
746 }
747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 if (logStdio) {
749 /* convert stdout/stderr to log messages */
750 opt.optionString = "-Xlog-stdio";
751 mOptions.add(opt);
752 }
753
754 if (enableAssertBuf[4] != '\0') {
755 /* accept "all" to mean "all classes and packages" */
756 if (strcmp(enableAssertBuf+4, "all") == 0)
757 enableAssertBuf[3] = '\0';
Steve Block6215d3f2012-01-04 20:05:49 +0000758 ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 opt.optionString = enableAssertBuf;
760 mOptions.add(opt);
761 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100762 ALOGV("Assertions disabled\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764
765 if (jniOptsBuf[10] != '\0') {
Steve Block6215d3f2012-01-04 20:05:49 +0000766 ALOGI("JNI options: '%s'\n", jniOptsBuf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 opt.optionString = jniOptsBuf;
768 mOptions.add(opt);
769 }
770
771 if (stackTraceFileBuf[0] != '\0') {
772 static const char* stfOptName = "-Xstacktracefile:";
773
774 stackTraceFile = (char*) malloc(strlen(stfOptName) +
775 strlen(stackTraceFileBuf) +1);
776 strcpy(stackTraceFile, stfOptName);
777 strcat(stackTraceFile, stackTraceFileBuf);
778 opt.optionString = stackTraceFile;
779 mOptions.add(opt);
780 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700781
Brian Carlstrom98ba7bd2014-03-10 10:20:01 -0700782 // libart tolerates libdvm flags, but not vice versa, so only pass some options if libart.
Brian Carlstrom5bc93862014-05-28 14:31:47 -0700783 property_get("persist.sys.dalvik.vm.lib.2", dalvikVmLibBuf, "libart.so");
Brian Carlstrom98ba7bd2014-03-10 10:20:01 -0700784 bool libart = (strncmp(dalvikVmLibBuf, "libart", 6) == 0);
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800785
Brian Carlstrom98ba7bd2014-03-10 10:20:01 -0700786 if (libart) {
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800787 // Extra options for DexClassLoader.
788 property_get("dalvik.vm.dex2oat-flags", dex2oatFlagsBuf, "");
789 parseExtraOpts(dex2oatFlagsBuf, "-Xcompiler-option");
790
791 // Extra options for boot.art/boot.oat image generation.
792 property_get("dalvik.vm.image-dex2oat-flags", dex2oatImageFlagsBuf, "");
793 parseExtraOpts(dex2oatImageFlagsBuf, "-Ximage-compiler-option");
794 }
795
Andy McFaddene4d81f22010-07-14 16:02:20 -0700796 /* extra options; parse this late so it overrides others */
797 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800798 parseExtraOpts(extraOptsBuf, NULL);
Andy McFaddene4d81f22010-07-14 16:02:20 -0700799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 /* Set the properties for locale */
801 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 strcpy(langOption, "-Duser.language=");
803 strcpy(regionOption, "-Duser.region=");
804 readLocale(langOption, regionOption);
805 opt.extraInfo = NULL;
806 opt.optionString = langOption;
807 mOptions.add(opt);
808 opt.optionString = regionOption;
809 mOptions.add(opt);
810 }
811
Dave Allison0efbd9a2014-01-30 14:19:51 -0800812 /*
813 * Set profiler options
814 */
Brian Carlstrom98ba7bd2014-03-10 10:20:01 -0700815 if (libart) {
Calin Juravlede99f082014-06-06 16:00:53 +0100816 // Whether or not the profiler should be enabled.
Calin Juravle9828d062014-06-02 16:45:13 +0100817 property_get("dalvik.vm.profiler", propBuf, "0");
818 if (propBuf[0] == '1') {
819 opt.optionString = "-Xenable-profiler";
820 mOptions.add(opt);
821 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800822
Calin Juravle3f967912014-06-10 14:52:24 +0100823 // Whether the profile should start upon app startup or be delayed by some random offset
824 // (in seconds) that is bound between 0 and a fixed value.
Calin Juravle994cd282014-06-11 18:21:41 +0100825 property_get("dalvik.vm.profile.start-immed", propBuf, "0");
Calin Juravle9828d062014-06-02 16:45:13 +0100826 if (propBuf[0] == '1') {
827 opt.optionString = "-Xprofile-start-immediately";
828 mOptions.add(opt);
829 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800830
Calin Juravle9828d062014-06-02 16:45:13 +0100831 // Number of seconds during profile runs.
832 strcpy(profile_period, "-Xprofile-period:");
833 if (property_get("dalvik.vm.profile.period-secs", profile_period+17, NULL) > 0) {
834 opt.optionString = profile_period;
835 mOptions.add(opt);
836 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800837
Calin Juravle9828d062014-06-02 16:45:13 +0100838 // Length of each profile run (seconds).
839 strcpy(profile_duration, "-Xprofile-duration:");
840 if (property_get("dalvik.vm.profile.duration-secs", profile_duration+19, NULL) > 0) {
841 opt.optionString = profile_duration;
842 mOptions.add(opt);
843 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800844
Calin Juravle9828d062014-06-02 16:45:13 +0100845 // Polling interval during profile run (microseconds).
846 strcpy(profile_interval, "-Xprofile-interval:");
847 if (property_get("dalvik.vm.profile.interval-us", profile_interval+19, NULL) > 0) {
848 opt.optionString = profile_interval;
849 mOptions.add(opt);
850 }
851
852 // Coefficient for period backoff. The the period is multiplied
853 // by this value after each profile run.
854 strcpy(profile_backoff, "-Xprofile-backoff:");
855 if (property_get("dalvik.vm.profile.backoff-coeff", profile_backoff+18, NULL) > 0) {
856 opt.optionString = profile_backoff;
857 mOptions.add(opt);
858 }
859
860 // Top K% of samples that are considered relevant when deciding if the app should be recompiled.
861 strcpy(profile_top_k_threshold, "-Xprofile-top-k-threshold:");
862 if (property_get("dalvik.vm.profile.top-k-thr", profile_top_k_threshold+26, NULL) > 0) {
863 opt.optionString = profile_top_k_threshold;
864 mOptions.add(opt);
865 }
866
867 // The threshold after which a change in the structure of the top K% profiled samples becomes significant
868 // and triggers recompilation. A change in profile is considered significant if X% (top-k-change-threshold)
869 // of the top K% (top-k-threshold property) samples has changed.
870 strcpy(profile_top_k_change_threshold, "-Xprofile-top-k-change-threshold:");
871 if (property_get("dalvik.vm.profile.top-k-ch-thr", profile_top_k_change_threshold+33, NULL) > 0) {
872 opt.optionString = profile_top_k_change_threshold;
873 mOptions.add(opt);
874 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800875 }
876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 initArgs.version = JNI_VERSION_1_4;
878 initArgs.options = mOptions.editArray();
879 initArgs.nOptions = mOptions.size();
880 initArgs.ignoreUnrecognized = JNI_FALSE;
881
882 /*
883 * Initialize the VM.
884 *
885 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
886 * If this call succeeds, the VM is ready, and we can start issuing
887 * JNI calls.
888 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700889 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000890 ALOGE("JNI_CreateJavaVM failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 goto bail;
892 }
893
Andy McFaddenf70188a2009-03-31 15:52:13 -0700894 result = 0;
895
896bail:
897 free(stackTraceFile);
898 return result;
899}
900
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700901char* AndroidRuntime::toSlashClassName(const char* className)
902{
903 char* result = strdup(className);
904 for (char* cp = result; *cp != '\0'; cp++) {
905 if (*cp == '.') {
906 *cp = '/';
907 }
908 }
909 return result;
910}
911
Andy McFaddenf70188a2009-03-31 15:52:13 -0700912/*
913 * Start the Android runtime. This involves starting the virtual machine
914 * and calling the "static void main(String[] args)" method in the class
915 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700916 *
917 * Passes the main function two arguments, the class name and the specified
918 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700919 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100920void AndroidRuntime::start(const char* className, const Vector<String8>& options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700921{
Steve Block5baa3a62011-12-20 16:23:08 +0000922 ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700923 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700924
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100925 static const String8 startSystemServer("start-system-server");
926
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700927 /*
928 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700929 * init.rc anymore, so we print out the boot start event here.
930 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100931 for (size_t i = 0; i < options.size(); ++i) {
932 if (options[i] == startSystemServer) {
933 /* track our progress through the boot sequence */
934 const int LOG_BOOT_PROGRESS_START = 3000;
935 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
936 }
Andy McFaddenf70188a2009-03-31 15:52:13 -0700937 }
938
939 const char* rootDir = getenv("ANDROID_ROOT");
940 if (rootDir == NULL) {
941 rootDir = "/system";
942 if (!hasDir("/system")) {
943 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700944 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700945 }
946 setenv("ANDROID_ROOT", rootDir, 1);
947 }
948
949 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block5baa3a62011-12-20 16:23:08 +0000950 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700951
952 /* start the virtual machine */
Brian Carlstrom9f8203a2013-06-19 13:49:36 -0700953 JniInvocation jni_invocation;
954 jni_invocation.Init(NULL);
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700955 JNIEnv* env;
956 if (startVm(&mJavaVM, &env) != 0) {
957 return;
958 }
959 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 /*
962 * Register android functions.
963 */
964 if (startReg(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000965 ALOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700966 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968
969 /*
970 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700971 * At present we have two arguments, the class name and an option string.
972 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 */
974 jclass stringClass;
975 jobjectArray strArray;
976 jstring classNameStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977
978 stringClass = env->FindClass("java/lang/String");
979 assert(stringClass != NULL);
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100980 strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 assert(strArray != NULL);
982 classNameStr = env->NewStringUTF(className);
983 assert(classNameStr != NULL);
984 env->SetObjectArrayElement(strArray, 0, classNameStr);
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100985
986 for (size_t i = 0; i < options.size(); ++i) {
987 jstring optionsStr = env->NewStringUTF(options.itemAt(i).string());
988 assert(optionsStr != NULL);
989 env->SetObjectArrayElement(strArray, i + 1, optionsStr);
990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991
992 /*
993 * Start VM. This thread becomes the main thread of the VM, and will
994 * not return until the VM exits.
995 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700996 char* slashClassName = toSlashClassName(className);
997 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 if (startClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000999 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 /* keep going */
1001 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001002 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 "([Ljava/lang/String;)V");
1004 if (startMeth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001005 ALOGE("JavaVM unable to find main() in '%s'\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 /* keep going */
1007 } else {
1008 env->CallStaticVoidMethod(startClass, startMeth, strArray);
1009
1010#if 0
1011 if (env->ExceptionCheck())
1012 threadExitUncaughtException(env);
1013#endif
1014 }
1015 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001016 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017
Steve Block5baa3a62011-12-20 16:23:08 +00001018 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 if (mJavaVM->DetachCurrentThread() != JNI_OK)
Steve Block8564c8d2012-01-05 23:22:43 +00001020 ALOGW("Warning: unable to detach main thread\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 if (mJavaVM->DestroyJavaVM() != 0)
Steve Block8564c8d2012-01-05 23:22:43 +00001022 ALOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023}
1024
Jeff Brown4280c4a2012-03-15 17:48:02 -07001025void AndroidRuntime::exit(int code)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026{
Jeff Brown4280c4a2012-03-15 17:48:02 -07001027 if (mExitWithoutCleanup) {
1028 ALOGI("VM exiting with result code %d, cleanup skipped.", code);
1029 ::_exit(code);
1030 } else {
1031 ALOGI("VM exiting with result code %d.", code);
1032 onExit(code);
1033 ::exit(code);
1034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035}
1036
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001037void AndroidRuntime::onVmCreated(JNIEnv* env)
1038{
1039 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
1040}
1041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042/*
1043 * Get the JNIEnv pointer for this thread.
1044 *
1045 * Returns NULL if the slot wasn't allocated or populated.
1046 */
1047/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
1048{
1049 JNIEnv* env;
1050 JavaVM* vm = AndroidRuntime::getJavaVM();
1051 assert(vm != NULL);
1052
1053 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
1054 return NULL;
1055 return env;
1056}
1057
1058/*
1059 * Makes the current thread visible to the VM.
1060 *
1061 * The JNIEnv pointer returned is only valid for the current thread, and
1062 * thus must be tucked into thread-local storage.
1063 */
1064static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
1065{
1066 JavaVMAttachArgs args;
1067 JavaVM* vm;
1068 jint result;
1069
1070 vm = AndroidRuntime::getJavaVM();
1071 assert(vm != NULL);
1072
1073 args.version = JNI_VERSION_1_4;
1074 args.name = (char*) threadName;
1075 args.group = NULL;
1076
1077 result = vm->AttachCurrentThread(pEnv, (void*) &args);
1078 if (result != JNI_OK)
Steve Block6215d3f2012-01-04 20:05:49 +00001079 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080
1081 return result;
1082}
1083
1084/*
1085 * Detach the current thread from the set visible to the VM.
1086 */
1087static int javaDetachThread(void)
1088{
1089 JavaVM* vm;
1090 jint result;
1091
1092 vm = AndroidRuntime::getJavaVM();
1093 assert(vm != NULL);
1094
1095 result = vm->DetachCurrentThread();
1096 if (result != JNI_OK)
Steve Block3762c312012-01-06 19:20:56 +00001097 ALOGE("ERROR: thread detach failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 return result;
1099}
1100
1101/*
1102 * When starting a native thread that will be visible from the VM, we
1103 * bounce through this to get the right attach/detach action.
1104 * Note that this function calls free(args)
1105 */
1106/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
1107 void* start = ((void**)args)[0];
1108 void* userData = ((void **)args)[1];
1109 char* name = (char*) ((void **)args)[2]; // we own this storage
1110 free(args);
1111 JNIEnv* env;
1112 int result;
1113
1114 /* hook us into the VM */
1115 if (javaAttachThread(name, &env) != JNI_OK)
1116 return -1;
1117
1118 /* start the thread running */
1119 result = (*(android_thread_func_t)start)(userData);
1120
1121 /* unhook us */
1122 javaDetachThread();
1123 free(name);
1124
1125 return result;
1126}
1127
1128/*
1129 * This is invoked from androidCreateThreadEtc() via the callback
1130 * set with androidSetCreateThreadFunc().
1131 *
1132 * We need to create the new thread in such a way that it gets hooked
1133 * into the VM before it really starts executing.
1134 */
1135/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001136 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 void* userData,
1138 const char* threadName,
1139 int32_t threadPriority,
1140 size_t threadStackSize,
1141 android_thread_id_t* threadId)
1142{
1143 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1144 int result;
1145
1146 assert(threadName != NULL);
1147
1148 args[0] = (void*) entryFunction;
1149 args[1] = userData;
1150 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1151
1152 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1153 threadName, threadPriority, threadStackSize, threadId);
1154 return result;
1155}
1156
1157/*
1158 * Create a thread that is visible from the VM.
1159 *
1160 * This is called from elsewhere in the library.
1161 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001162/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 void (*start)(void *), void* arg)
1164{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001165 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001167 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1168 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169}
1170
1171#if 0
1172static void quickTest(void* arg)
1173{
1174 const char* str = (const char*) arg;
1175
1176 printf("In quickTest: %s\n", str);
1177}
1178#endif
1179
1180#ifdef NDEBUG
1181 #define REG_JNI(name) { name }
1182 struct RegJNIRec {
1183 int (*mProc)(JNIEnv*);
1184 };
1185#else
1186 #define REG_JNI(name) { name, #name }
1187 struct RegJNIRec {
1188 int (*mProc)(JNIEnv*);
1189 const char* mName;
1190 };
1191#endif
1192
1193typedef void (*RegJAMProc)();
1194
1195static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1196{
1197 for (size_t i = 0; i < count; i++) {
1198 if (array[i].mProc(env) < 0) {
1199#ifndef NDEBUG
Steve Block5baa3a62011-12-20 16:23:08 +00001200 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201#endif
1202 return -1;
1203 }
1204 }
1205 return 0;
1206}
1207
1208static void register_jam_procs(const RegJAMProc array[], size_t count)
1209{
1210 for (size_t i = 0; i < count; i++) {
1211 array[i]();
1212 }
1213}
1214
1215static const RegJNIRec gRegJNI[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 REG_JNI(register_com_android_internal_os_RuntimeInit),
1217 REG_JNI(register_android_os_SystemClock),
1218 REG_JNI(register_android_util_EventLog),
1219 REG_JNI(register_android_util_Log),
1220 REG_JNI(register_android_util_FloatMath),
1221 REG_JNI(register_android_text_format_Time),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 REG_JNI(register_android_content_AssetManager),
1223 REG_JNI(register_android_content_StringBlock),
1224 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001225 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001227 REG_JNI(register_android_text_AndroidBidi),
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001228 REG_JNI(register_android_view_InputDevice),
1229 REG_JNI(register_android_view_KeyCharacterMap),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001231 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 REG_JNI(register_android_os_Binder),
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001233 REG_JNI(register_android_os_Parcel),
Jeff Brown0a0a1242011-12-02 02:25:22 -08001234 REG_JNI(register_android_view_DisplayEventReceiver),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 REG_JNI(register_android_nio_utils),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 REG_JNI(register_android_graphics_Graphics),
Romain Guy3b748a42013-04-17 18:54:38 -07001237 REG_JNI(register_android_view_GraphicBuffer),
Chet Haasea1cff502012-02-21 13:43:44 -08001238 REG_JNI(register_android_view_GLES20DisplayList),
Romain Guye4d01122010-06-16 18:44:05 -07001239 REG_JNI(register_android_view_GLES20Canvas),
Romain Guya9582652011-11-10 14:20:10 -08001240 REG_JNI(register_android_view_HardwareRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 REG_JNI(register_android_view_Surface),
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001242 REG_JNI(register_android_view_SurfaceControl),
Jeff Brown64a55af2012-08-26 02:47:39 -07001243 REG_JNI(register_android_view_SurfaceSession),
Romain Guy8f0095c2011-05-02 17:24:22 -07001244 REG_JNI(register_android_view_TextureView),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1246 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Thomas Tafertshofer6b1e8382012-07-03 13:37:35 -07001247 REG_JNI(register_android_opengl_jni_EGL14),
Jesse Hall237c2b82013-05-06 11:36:57 -07001248 REG_JNI(register_android_opengl_jni_EGLExt),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001249 REG_JNI(register_android_opengl_jni_GLES10),
1250 REG_JNI(register_android_opengl_jni_GLES10Ext),
1251 REG_JNI(register_android_opengl_jni_GLES11),
1252 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001253 REG_JNI(register_android_opengl_jni_GLES20),
Jesse Halld877efe2013-04-29 15:59:35 -07001254 REG_JNI(register_android_opengl_jni_GLES30),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255
1256 REG_JNI(register_android_graphics_Bitmap),
1257 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001258 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 REG_JNI(register_android_graphics_Camera),
Leon Scroggins IIId0d7eaf2013-09-06 16:46:57 -04001260 REG_JNI(register_android_graphics_CreateJavaOutputStreamAdaptor),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 REG_JNI(register_android_graphics_Canvas),
1262 REG_JNI(register_android_graphics_ColorFilter),
1263 REG_JNI(register_android_graphics_DrawFilter),
1264 REG_JNI(register_android_graphics_Interpolator),
1265 REG_JNI(register_android_graphics_LayerRasterizer),
1266 REG_JNI(register_android_graphics_MaskFilter),
1267 REG_JNI(register_android_graphics_Matrix),
1268 REG_JNI(register_android_graphics_Movie),
1269 REG_JNI(register_android_graphics_NinePatch),
1270 REG_JNI(register_android_graphics_Paint),
1271 REG_JNI(register_android_graphics_Path),
1272 REG_JNI(register_android_graphics_PathMeasure),
1273 REG_JNI(register_android_graphics_PathEffect),
1274 REG_JNI(register_android_graphics_Picture),
1275 REG_JNI(register_android_graphics_PorterDuff),
1276 REG_JNI(register_android_graphics_Rasterizer),
1277 REG_JNI(register_android_graphics_Region),
1278 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001279 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 REG_JNI(register_android_graphics_Typeface),
1281 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001282 REG_JNI(register_android_graphics_YuvImage),
Svetoslav6811f4e2013-09-18 15:58:28 -07001283 REG_JNI(register_android_graphics_pdf_PdfDocument),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284
1285 REG_JNI(register_android_database_CursorWindow),
Jeff Browne5360fb2011-10-31 17:48:13 -07001286 REG_JNI(register_android_database_SQLiteConnection),
1287 REG_JNI(register_android_database_SQLiteGlobal),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 REG_JNI(register_android_database_SQLiteDebug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 REG_JNI(register_android_os_FileObserver),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001291 REG_JNI(register_android_os_MessageQueue),
Stephen Smalleyc07fca32012-01-13 08:31:39 -05001292 REG_JNI(register_android_os_SELinux),
Jeff Brown481c1572012-03-09 14:41:15 -08001293 REG_JNI(register_android_os_Trace),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 REG_JNI(register_android_os_UEventObserver),
1295 REG_JNI(register_android_net_LocalSocketImpl),
1296 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001297 REG_JNI(register_android_net_TrafficStats),
Elliott Hughesa11d5742013-08-09 14:59:39 -07001298 REG_JNI(register_android_net_wifi_WifiNative),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 REG_JNI(register_android_os_MemoryFile),
1300 REG_JNI(register_com_android_internal_os_ZygoteInit),
Narayan Kamath973b4662014-03-31 13:41:26 +01001301 REG_JNI(register_com_android_internal_os_Zygote),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 REG_JNI(register_android_hardware_Camera),
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -07001303 REG_JNI(register_android_hardware_camera2_CameraMetadata),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -04001305 REG_JNI(register_android_hardware_SerialPort),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001306 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001307 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001308 REG_JNI(register_android_hardware_UsbRequest),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 REG_JNI(register_android_media_AudioRecord),
1310 REG_JNI(register_android_media_AudioSystem),
1311 REG_JNI(register_android_media_AudioTrack),
1312 REG_JNI(register_android_media_JetPlayer),
Jeff Browncbad9762012-09-04 21:57:59 -07001313 REG_JNI(register_android_media_RemoteDisplay),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 REG_JNI(register_android_media_ToneGenerator),
1315
1316 REG_JNI(register_android_opengl_classes),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001317 REG_JNI(register_android_server_NetworkManagementSocketTagger),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001318 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001320 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001321 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001322 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001323 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001324 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001325 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001326 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001327 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001328 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brownc28867a2013-03-26 15:42:39 -07001329 REG_JNI(register_android_view_InputEventSender),
Michael Wrighta44dd262013-04-10 21:12:00 -07001330 REG_JNI(register_android_view_InputQueue),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001331 REG_JNI(register_android_view_KeyEvent),
1332 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001333 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001334 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001335
1336 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001337 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001338
1339 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001340 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001341 REG_JNI(register_com_android_internal_net_NetworkStatsFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342};
1343
1344/*
1345 * Register android native functions with the VM.
1346 */
1347/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1348{
1349 /*
1350 * This hook causes all future threads created in this process to be
1351 * attached to the JavaVM. (This needs to go away in favor of JNI
1352 * Attach calls.)
1353 */
1354 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1355
Steve Block71f2cf12011-10-20 11:56:00 +01001356 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357
1358 /*
1359 * Every "register" function calls one or more things that return
1360 * a local reference (e.g. FindClass). Because we haven't really
1361 * started the VM yet, they're all getting stored in the base frame
1362 * and never released. Use Push/Pop to manage the storage.
1363 */
1364 env->PushLocalFrame(200);
1365
1366 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1367 env->PopLocalFrame(NULL);
1368 return -1;
1369 }
1370 env->PopLocalFrame(NULL);
1371
1372 //createJavaThread("fubar", quickTest, (void*) "hello");
1373
1374 return 0;
1375}
1376
1377AndroidRuntime* AndroidRuntime::getRuntime()
1378{
1379 return gCurRuntime;
1380}
1381
1382/**
1383 * Used by WithFramework to register native functions.
1384 */
1385extern "C"
1386jint Java_com_android_internal_util_WithFramework_registerNatives(
1387 JNIEnv* env, jclass clazz) {
1388 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1389}
1390
1391/**
1392 * Used by LoadClass to register native functions.
1393 */
1394extern "C"
1395jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1396 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1397}
1398
1399} // namespace android