blob: 1f4105fdca9896766b294f94111cc9f055663967 [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>
32
33#include "jni.h"
34#include "JNIHelp.h"
Brian Carlstrom9f8203a2013-06-19 13:49:36 -070035#include "JniInvocation.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include "android_util_Binder.h"
37
38#include <stdio.h>
39#include <signal.h>
40#include <sys/stat.h>
41#include <sys/types.h>
42#include <signal.h>
43#include <dirent.h>
44#include <assert.h>
45
46
47using namespace android;
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049extern int register_android_os_Binder(JNIEnv* env);
50extern int register_android_os_Process(JNIEnv* env);
51extern int register_android_graphics_Bitmap(JNIEnv*);
52extern int register_android_graphics_BitmapFactory(JNIEnv*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080053extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054extern int register_android_graphics_Camera(JNIEnv* env);
Leon Scroggins IIId0d7eaf2013-09-06 16:46:57 -040055extern int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056extern int register_android_graphics_Graphics(JNIEnv* env);
57extern int register_android_graphics_Interpolator(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058extern int register_android_graphics_MaskFilter(JNIEnv* env);
59extern int register_android_graphics_Movie(JNIEnv* env);
60extern int register_android_graphics_NinePatch(JNIEnv*);
61extern int register_android_graphics_PathEffect(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062extern int register_android_graphics_Shader(JNIEnv* env);
63extern int register_android_graphics_Typeface(JNIEnv* env);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080064extern int register_android_graphics_YuvImage(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
67extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
Thomas Tafertshofer6b1e8382012-07-03 13:37:35 -070068extern int register_android_opengl_jni_EGL14(JNIEnv* env);
Jesse Hall237c2b82013-05-06 11:36:57 -070069extern int register_android_opengl_jni_EGLExt(JNIEnv* env);
Jack Palevich1c4907e2009-04-13 16:22:25 -070070extern int register_android_opengl_jni_GLES10(JNIEnv* env);
71extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
72extern int register_android_opengl_jni_GLES11(JNIEnv* env);
73extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
Jack Palevich560814f2009-11-19 16:34:55 +080074extern int register_android_opengl_jni_GLES20(JNIEnv* env);
Jesse Halld877efe2013-04-29 15:59:35 -070075extern int register_android_opengl_jni_GLES30(JNIEnv* env);
Jesse Hall7ab63ac2014-05-19 15:13:41 -070076extern int register_android_opengl_jni_GLES31(JNIEnv* env);
77extern int register_android_opengl_jni_GLES31Ext(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);
Ruben Brunkfeb50af2014-05-09 19:58:49 -070081extern int register_android_hardware_camera2_legacy_LegacyCameraDevice(JNIEnv *env);
Eino-Ville Talvalae1f57d62014-05-29 17:17:07 -070082extern int register_android_hardware_camera2_legacy_PerfMeasurement(JNIEnv *env);
Ruben Brunkb6079002014-05-22 12:33:54 -070083extern int register_android_hardware_camera2_DngCreator(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084extern int register_android_hardware_SensorManager(JNIEnv *env);
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040085extern int register_android_hardware_SerialPort(JNIEnv *env);
Eric Laurent60b62bc2014-04-18 17:50:49 -070086extern int register_android_hardware_SoundTrigger(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050087extern int register_android_hardware_UsbDevice(JNIEnv *env);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050088extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050089extern int register_android_hardware_UsbRequest(JNIEnv *env);
destradaaa4fa3b52014-07-09 10:46:39 -070090extern int register_android_hardware_location_ActivityRecognitionHardware(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92extern int register_android_media_AudioRecord(JNIEnv *env);
93extern int register_android_media_AudioSystem(JNIEnv *env);
94extern int register_android_media_AudioTrack(JNIEnv *env);
95extern int register_android_media_JetPlayer(JNIEnv *env);
96extern int register_android_media_ToneGenerator(JNIEnv *env);
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098extern int register_android_util_FloatMath(JNIEnv* env);
99
100namespace android {
101
102/*
103 * JNI-based registration functions. Note these are properly contained in
104 * namespace android.
105 */
106extern int register_android_content_AssetManager(JNIEnv* env);
107extern int register_android_util_EventLog(JNIEnv* env);
108extern int register_android_util_Log(JNIEnv* env);
109extern int register_android_content_StringBlock(JNIEnv* env);
110extern int register_android_content_XmlBlock(JNIEnv* env);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700111extern int register_android_emoji_EmojiFactory(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112extern int register_android_graphics_Canvas(JNIEnv* env);
John Reck52244ff2014-05-01 21:27:37 -0700113extern int register_android_graphics_CanvasProperty(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114extern int register_android_graphics_ColorFilter(JNIEnv* env);
115extern int register_android_graphics_DrawFilter(JNIEnv* env);
Raph Levien1a73f732014-01-30 16:06:28 -0800116extern int register_android_graphics_FontFamily(JNIEnv* env);
Mike Reed4c9355c2014-05-07 11:48:37 -0400117extern int register_android_graphics_LayerRasterizer(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118extern int register_android_graphics_Matrix(JNIEnv* env);
119extern int register_android_graphics_Paint(JNIEnv* env);
120extern int register_android_graphics_Path(JNIEnv* env);
121extern int register_android_graphics_PathMeasure(JNIEnv* env);
122extern int register_android_graphics_Picture(JNIEnv*);
123extern int register_android_graphics_PorterDuff(JNIEnv* env);
124extern int register_android_graphics_Rasterizer(JNIEnv* env);
Jeff Brownfbf09772011-01-16 14:06:57 -0800125extern int register_android_graphics_Region(JNIEnv* env);
Jamie Gennisaa0ce332011-01-06 17:04:26 -0800126extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127extern int register_android_graphics_Xfermode(JNIEnv* env);
Svetoslav6811f4e2013-09-18 15:58:28 -0700128extern int register_android_graphics_pdf_PdfDocument(JNIEnv* env);
Svetoslav62ce3322014-09-04 21:17:17 -0700129extern int register_android_graphics_pdf_PdfEditor(JNIEnv* env);
Svetoslav29617692014-04-24 18:40:42 -0700130extern int register_android_graphics_pdf_PdfRenderer(JNIEnv* env);
Jeff Brown0a0a1242011-12-02 02:25:22 -0800131extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
John Reckf666ad72014-03-14 16:24:57 -0700132extern int register_android_view_RenderNode(JNIEnv* env);
John Recke45b1fd2014-04-15 09:50:16 -0700133extern int register_android_view_RenderNodeAnimator(JNIEnv* env);
Romain Guy3b748a42013-04-17 18:54:38 -0700134extern int register_android_view_GraphicBuffer(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700135extern int register_android_view_GLES20Canvas(JNIEnv* env);
John Reck04fc5832014-02-05 16:38:25 -0800136extern int register_android_view_HardwareLayer(JNIEnv* env);
John Reckcec24ae2013-11-05 13:27:50 -0800137extern int register_android_view_ThreadedRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138extern int register_android_view_Surface(JNIEnv* env);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800139extern int register_android_view_SurfaceControl(JNIEnv* env);
Jeff Brown64a55af2012-08-26 02:47:39 -0700140extern int register_android_view_SurfaceSession(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700141extern int register_android_view_TextureView(JNIEnv* env);
John Reck315c3292014-05-09 19:21:04 -0700142extern int register_com_android_internal_view_animation_NativeInterpolatorFactoryHelper(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143extern int register_android_database_CursorWindow(JNIEnv* env);
Jeff Browne5360fb2011-10-31 17:48:13 -0700144extern int register_android_database_SQLiteConnection(JNIEnv* env);
145extern int register_android_database_SQLiteGlobal(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146extern int register_android_database_SQLiteDebug(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147extern int register_android_nio_utils(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700149extern int register_android_os_MessageQueue(JNIEnv* env);
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800150extern int register_android_os_Parcel(JNIEnv* env);
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500151extern int register_android_os_SELinux(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153extern int register_android_os_SystemClock(JNIEnv* env);
Jeff Brown481c1572012-03-09 14:41:15 -0800154extern int register_android_os_Trace(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155extern int register_android_os_FileObserver(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156extern int register_android_os_UEventObserver(JNIEnv* env);
157extern int register_android_os_MemoryFile(JNIEnv* env);
158extern int register_android_net_LocalSocketImpl(JNIEnv* env);
159extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700160extern int register_android_net_TrafficStats(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161extern int register_android_text_AndroidCharacter(JNIEnv *env);
Anish Athalye88b5b0b2014-06-24 14:39:43 -0700162extern int register_android_text_StaticLayout(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800163extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164extern int register_android_opengl_classes(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
Narayan Kamath29564cd2014-08-07 10:57:40 +0100166extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700168extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700169extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700170extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400171extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Christopher Tate4a627c72011-04-01 14:43:32 -0700172extern int register_android_app_backup_FullBackup(JNIEnv *env);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700173extern int register_android_app_ActivityThread(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700174extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Browncbad9762012-09-04 21:57:59 -0700175extern int register_android_media_RemoteDisplay(JNIEnv *env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700176extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700177extern int register_android_view_InputDevice(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800178extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brownc28867a2013-03-26 15:42:39 -0700179extern int register_android_view_InputEventSender(JNIEnv* env);
Michael Wrighta44dd262013-04-10 21:12:00 -0700180extern int register_android_view_InputQueue(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700181extern int register_android_view_KeyCharacterMap(JNIEnv *env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700182extern int register_android_view_KeyEvent(JNIEnv* env);
183extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700184extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700185extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700186extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700187extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700188extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700189extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800190extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env);
Narayan Kamath973b4662014-03-31 13:41:26 +0100191extern int register_com_android_internal_os_Zygote(JNIEnv *env);
John Reck9fa40712014-05-09 15:26:59 -0700192extern int register_com_android_internal_util_VirtualRefBasePtr(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
194static AndroidRuntime* gCurRuntime = NULL;
195
196static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
197{
198 if (jniThrowException(env, exc, msg) != 0)
199 assert(false);
200}
201
202/*
203 * Code written in the Java Programming Language calls here from main().
204 */
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700205static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206{
207 gCurRuntime->onStarted();
208}
209
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700210static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211{
212 gCurRuntime->onZygoteInit();
213}
214
Jeff Brown4280c4a2012-03-15 17:48:02 -0700215static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv* env,
216 jobject clazz, jboolean exitWithoutCleanup)
217{
218 gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup);
219}
220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221/*
222 * JNI registration.
223 */
224static JNINativeMethod gMethods[] = {
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700225 { "nativeFinishInit", "()V",
226 (void*) com_android_internal_os_RuntimeInit_nativeFinishInit },
227 { "nativeZygoteInit", "()V",
228 (void*) com_android_internal_os_RuntimeInit_nativeZygoteInit },
Jeff Brown4280c4a2012-03-15 17:48:02 -0700229 { "nativeSetExitWithoutCleanup", "(Z)V",
230 (void*) com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231};
232
233int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
234{
235 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
236 gMethods, NELEM(gMethods));
237}
238
239// ----------------------------------------------------------------------
240
Andreas Huber9ae000c2014-02-13 17:22:33 +0000241/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
242
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000243AndroidRuntime::AndroidRuntime(char* argBlockStart, const size_t argBlockLength) :
244 mExitWithoutCleanup(false),
245 mArgBlockStart(argBlockStart),
246 mArgBlockLength(argBlockLength)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700248 SkGraphics::Init();
Mike Reedfc8db532009-04-27 11:43:30 -0400249 // There is also a global font cache, but its budget is specified in code
250 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
252 // Pre-allocate enough space to hold a fair number of options.
253 mOptions.setCapacity(20);
254
255 assert(gCurRuntime == NULL); // one per process
256 gCurRuntime = this;
257}
258
259AndroidRuntime::~AndroidRuntime()
260{
261 SkGraphics::Term();
262}
263
Andreas Huber9ae000c2014-02-13 17:22:33 +0000264/*
265 * Register native methods using JNI.
266 */
267/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
268 const char* className, const JNINativeMethod* gMethods, int numMethods)
269{
270 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
271}
272
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000273void AndroidRuntime::setArgv0(const char* argv0) {
Jeff Brown00c0cd42014-09-10 16:48:46 -0700274 memset(mArgBlockStart, 0, mArgBlockLength);
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000275 strlcpy(mArgBlockStart, argv0, mArgBlockLength);
276}
277
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100278status_t AndroidRuntime::callMain(const String8& className, jclass clazz,
279 const Vector<String8>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280{
281 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 jmethodID methodId;
283
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100284 ALOGD("Calling main entry %s", className.string());
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700287 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 return UNKNOWN_ERROR;
289 }
290
291 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
292 if (methodId == NULL) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100293 ALOGE("ERROR: could not find method %s.main(String[])\n", className.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 return UNKNOWN_ERROR;
295 }
296
297 /*
298 * We want to call main() with a String array with our arguments in it.
299 * Create an array and populate it.
300 */
301 jclass stringClass;
302 jobjectArray strArray;
303
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100304 const size_t numArgs = args.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 stringClass = env->FindClass("java/lang/String");
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100306 strArray = env->NewObjectArray(numArgs, stringClass, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100308 for (size_t i = 0; i < numArgs; i++) {
309 jstring argStr = env->NewStringUTF(args[i].string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 env->SetObjectArrayElement(strArray, i, argStr);
311 }
312
313 env->CallStaticVoidMethod(clazz, methodId, strArray);
314 return NO_ERROR;
315}
316
317/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * The VM calls this through the "exit" hook.
319 */
320static void runtime_exit(int code)
321{
Jeff Brown4280c4a2012-03-15 17:48:02 -0700322 gCurRuntime->exit(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323}
324
325/*
326 * The VM calls this through the "vfprintf" hook.
327 *
328 * We ignore "fp" and just write the results to the log file.
329 */
330static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
331{
332 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
333}
334
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800335/**
336 * The VM calls this when mutex contention debugging is enabled to
337 * determine whether or not the blocked thread was a "sensitive thread"
338 * for user responsiveness/smoothess.
339 *
340 * Our policy for this is whether or not we're tracing any StrictMode
341 * events on this thread (which we might've inherited via Binder calls
342 * into us)
343 */
344static bool runtime_isSensitiveThread() {
345 IPCThreadState* state = IPCThreadState::selfOrNull();
346 return state && state->getStrictModePolicy() != 0;
347}
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349static int hasDir(const char* dir)
350{
351 struct stat s;
352 int res = stat(dir, &s);
353 if (res == 0) {
354 return S_ISDIR(s.st_mode);
355 }
356 return 0;
357}
358
359/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 * Read the persistent locale.
361 */
362static void readLocale(char* language, char* region)
363{
364 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 property_get("persist.sys.language", propLang, "");
367 property_get("persist.sys.country", propRegn, "");
368 if (*propLang == 0 && *propRegn == 0) {
369 /* Set to ro properties, default is en_US */
370 property_get("ro.product.locale.language", propLang, "en");
371 property_get("ro.product.locale.region", propRegn, "US");
372 }
Narayan Kamath37151b52014-07-01 12:44:51 +0100373 strncat(language, propLang, 3);
374 strncat(region, propRegn, 3);
Steve Block5baa3a62011-12-20 16:23:08 +0000375 //ALOGD("language=%s region=%s\n", language, region);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376}
377
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700378void AndroidRuntime::addOption(const char* optionString, void* extraInfo)
379{
380 JavaVMOption opt;
381 opt.optionString = optionString;
382 opt.extraInfo = extraInfo;
383 mOptions.add(opt);
384}
385
Andy McFaddenf70188a2009-03-31 15:52:13 -0700386/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700387 * Parse a property containing space-separated options that should be
388 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
389 *
390 * This will cut up "extraOptsBuf" as we chop it into individual options.
391 *
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800392 * If "quotingArg" is non-null, it is passed before each extra option in mOptions.
393 *
Andy McFaddene4d81f22010-07-14 16:02:20 -0700394 * Adds the strings, if any, to mOptions.
395 */
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800396void AndroidRuntime::parseExtraOpts(char* extraOptsBuf, const char* quotingArg)
Andy McFaddene4d81f22010-07-14 16:02:20 -0700397{
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800398 char* start = extraOptsBuf;
399 char* end = NULL;
Andy McFaddene4d81f22010-07-14 16:02:20 -0700400 while (*start != '\0') {
401 while (*start == ' ') /* skip leading whitespace */
402 start++;
403 if (*start == '\0') /* was trailing ws, bail */
404 break;
405
406 end = start+1;
407 while (*end != ' ' && *end != '\0') /* find end of token */
408 end++;
409 if (*end == ' ')
410 *end++ = '\0'; /* mark end, advance to indicate more */
411
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800412 if (quotingArg != NULL) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700413 addOption(quotingArg);
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800414 }
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700415 addOption(start);
Andy McFaddene4d81f22010-07-14 16:02:20 -0700416 start = end;
417 }
418}
419
420/*
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700421 * Reads a "property" into "buffer" with a default of "defaultArg". If
422 * the property is non-empty, it is treated as a runtime option such
423 * as "-Xmx32m".
424 *
425 * The "runtimeArg" is a prefix for the option such as "-Xms" or "-Xmx".
426 *
427 * If an argument is found, it is added to mOptions.
428 *
429 * If an option is found, it is added to mOptions and true is
430 * returned. Otherwise false is returned.
431 */
432bool AndroidRuntime::parseRuntimeOption(const char* property,
433 char* buffer,
434 const char* runtimeArg,
435 const char* defaultArg)
436{
437 strcpy(buffer, runtimeArg);
438 size_t runtimeArgLen = strlen(runtimeArg);
439 property_get(property, buffer+runtimeArgLen, defaultArg);
440 if (buffer[runtimeArgLen] == '\0') {
441 return false;
442 }
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700443 addOption(buffer);
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700444 return true;
445}
446
447/*
448 * Reads a "property" into "buffer". If the property is non-empty, it
Brian Carlstrom3fbfbb42014-07-28 19:13:28 -0700449 * is treated as a dex2oat compiler option that should be
450 * passed as a quoted option, e.g. "-Ximage-compiler-option --compiler-filter=verify-none".
451 *
452 * The "compilerArg" is a prefix for the option such as "--compiler-filter=".
453 *
454 * The "quotingArg" should be "-Ximage-compiler-option" or "-Xcompiler-option".
455 *
456 * If an option is found, it is added to mOptions and true is
457 * returned. Otherwise false is returned.
458 */
459bool AndroidRuntime::parseCompilerOption(const char* property,
460 char* buffer,
461 const char* compilerArg,
462 const char* quotingArg)
463{
464 strcpy(buffer, compilerArg);
465 size_t compilerArgLen = strlen(compilerArg);
466 property_get(property, buffer+compilerArgLen, "");
467 if (buffer[compilerArgLen] == '\0') {
468 return false;
469 }
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700470 addOption(quotingArg);
471 addOption(buffer);
Brian Carlstrom3fbfbb42014-07-28 19:13:28 -0700472 return true;
473}
474
475/*
476 * Reads a "property" into "buffer". If the property is non-empty, it
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700477 * is treated as a dex2oat compiler runtime option that should be
478 * passed as a quoted option, e.g. "-Ximage-compiler-option
479 * --runtime-arg -Ximage-compiler-option -Xmx32m".
480 *
481 * The "runtimeArg" is a prefix for the option such as "-Xms" or "-Xmx".
482 *
483 * The "quotingArg" should be "-Ximage-compiler-option" or "-Xcompiler-option".
484 *
485 * If an option is found, it is added to mOptions and true is
486 * returned. Otherwise false is returned.
487 */
488bool AndroidRuntime::parseCompilerRuntimeOption(const char* property,
489 char* buffer,
490 const char* runtimeArg,
491 const char* quotingArg)
492{
493 strcpy(buffer, runtimeArg);
494 size_t runtimeArgLen = strlen(runtimeArg);
495 property_get(property, buffer+runtimeArgLen, "");
496 if (buffer[runtimeArgLen] == '\0') {
497 return false;
498 }
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700499 addOption(quotingArg);
500 addOption("--runtime-arg");
501 addOption(quotingArg);
502 addOption(buffer);
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700503 return true;
504}
505
506/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700507 * Start the Dalvik Virtual Machine.
508 *
509 * Various arguments, most determined by system properties, are passed in.
510 * The "mOptions" vector is updated.
511 *
Dave Allison07a1e232014-03-14 08:54:33 -0700512 * CAUTION: when adding options in here, be careful not to put the
513 * char buffer inside a nested scope. Adding the buffer to the
514 * options using mOptions.add() does not copy the buffer, so if the
515 * buffer goes out of scope the option may be overwritten. It's best
516 * to put the buffer at the top of the function so that it is more
517 * unlikely that someone will surround it in a scope at a later time
518 * and thus introduce a bug.
519 *
Andy McFaddenf70188a2009-03-31 15:52:13 -0700520 * Returns 0 on success.
521 */
522int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700524 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 JavaVMInitArgs initArgs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 char propBuf[PROPERTY_VALUE_MAX];
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700527 char stackTraceFileBuf[sizeof("-Xstacktracefile:")-1 + PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
529 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
530 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800531 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700532 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800533 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Ian Rogers53250102012-09-23 16:38:03 -0700534 char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
535 char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
Mathieu Chartier7e4fdec2013-12-02 15:46:51 -0800536 char gctypeOptsBuf[sizeof("-Xgc:")-1 + PROPERTY_VALUE_MAX];
Mathieu Chartierc01936a2014-05-14 15:44:49 -0700537 char backgroundgcOptsBuf[sizeof("-XX:BackgroundGC=")-1 + PROPERTY_VALUE_MAX];
Ian Rogers53250102012-09-23 16:38:03 -0700538 char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
buzbeeb63de6d2013-05-24 13:14:08 -0700539 char jitcodecachesizeOptsBuf[sizeof("-Xjitcodecachesize:")-1 + PROPERTY_VALUE_MAX];
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800540 char dalvikVmLibBuf[PROPERTY_VALUE_MAX];
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700541 char dex2oatXmsImageFlagsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
542 char dex2oatXmxImageFlagsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
543 char dex2oatXmsFlagsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
544 char dex2oatXmxFlagsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Brian Carlstrom3fbfbb42014-07-28 19:13:28 -0700545 char dex2oatCompilerFilterBuf[sizeof("--compiler-filter=")-1 + PROPERTY_VALUE_MAX];
546 char dex2oatImageCompilerFilterBuf[sizeof("--compiler-filter=")-1 + PROPERTY_VALUE_MAX];
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800547 char dex2oatFlagsBuf[PROPERTY_VALUE_MAX];
548 char dex2oatImageFlagsBuf[PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700549 char extraOptsBuf[PROPERTY_VALUE_MAX];
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700550 char voldDecryptBuf[PROPERTY_VALUE_MAX];
Ben Cheng52b0e732009-06-19 13:31:12 -0700551 enum {
552 kEMDefault,
553 kEMIntPortable,
554 kEMIntFast,
Ben Cheng52b0e732009-06-19 13:31:12 -0700555 kEMJitCompiler,
Ben Cheng52b0e732009-06-19 13:31:12 -0700556 } executionMode = kEMDefault;
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700557 char profilePeriod[sizeof("-Xprofile-period:")-1 + PROPERTY_VALUE_MAX];
558 char profileDuration[sizeof("-Xprofile-duration:")-1 + PROPERTY_VALUE_MAX];
559 char profileInterval[sizeof("-Xprofile-interval:")-1 + PROPERTY_VALUE_MAX];
560 char profileBackoff[sizeof("-Xprofile-backoff:")-1 + PROPERTY_VALUE_MAX];
561 char profileTopKThreshold[sizeof("-Xprofile-top-k-threshold:")-1 + PROPERTY_VALUE_MAX];
562 char profileTopKChangeThreshold[sizeof("-Xprofile-top-k-change-threshold:")-1 +
563 PROPERTY_VALUE_MAX];
564 char profileType[sizeof("-Xprofile-type:")-1 + PROPERTY_VALUE_MAX];
565 char profileMaxStackDepth[sizeof("-Xprofile-max-stack-depth:")-1 + PROPERTY_VALUE_MAX];
Dave Allison07a1e232014-03-14 08:54:33 -0700566 char langOption[sizeof("-Duser.language=") + 3];
567 char regionOption[sizeof("-Duser.region=") + 3];
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700568 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:")-1 + PROPERTY_VALUE_MAX];
569 char jitOpBuf[sizeof("-Xjitop:")-1 + PROPERTY_VALUE_MAX];
570 char jitMethodBuf[sizeof("-Xjitmethod:")-1 + PROPERTY_VALUE_MAX];
Calin Juravle01db9162014-08-07 14:45:53 +0100571 char nativeBridgeLibrary[sizeof("-XX:NativeBridge=") + PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700573 bool checkJni = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 property_get("dalvik.vm.checkjni", propBuf, "");
575 if (strcmp(propBuf, "true") == 0) {
576 checkJni = true;
577 } else if (strcmp(propBuf, "false") != 0) {
578 /* property is neither true nor false; fall back on kernel parameter */
579 property_get("ro.kernel.android.checkjni", propBuf, "");
580 if (propBuf[0] == '1') {
581 checkJni = true;
582 }
583 }
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700584 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
585 if (checkJni) {
586 /* extended JNI checking */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700587 addOption("-Xcheck:jni");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700588
589 /* set a cap on JNI global references */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700590 addOption("-Xjnigreflimit:2000");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700591
592 /* with -Xcheck:jni, this provides a JNI function call trace */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700593 //addOption("-verbose:jni");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595
596 property_get("dalvik.vm.execution-mode", propBuf, "");
597 if (strcmp(propBuf, "int:portable") == 0) {
598 executionMode = kEMIntPortable;
599 } else if (strcmp(propBuf, "int:fast") == 0) {
600 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700601 } else if (strcmp(propBuf, "int:jit") == 0) {
602 executionMode = kEMJitCompiler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700605 parseRuntimeOption("dalvik.vm.stack-trace-file", stackTraceFileBuf, "-Xstacktracefile:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606
Andy McFaddene2b23e12009-04-03 11:09:46 -0700607 property_get("dalvik.vm.check-dex-sum", propBuf, "");
608 if (strcmp(propBuf, "true") == 0) {
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700609 /* perform additional DEX checksum tests */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700610 addOption("-Xcheckdexsum");
Andy McFaddene2b23e12009-04-03 11:09:46 -0700611 }
612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 property_get("log.redirect-stdio", propBuf, "");
614 if (strcmp(propBuf, "true") == 0) {
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700615 /* convert stdout/stderr to log messages */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700616 addOption("-Xlog-stdio");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
618
619 strcpy(enableAssertBuf, "-ea:");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700620 property_get("dalvik.vm.enableassertions", enableAssertBuf+sizeof("-ea:")-1, "");
621 if (enableAssertBuf[sizeof("-ea:")-1] != '\0') {
622 /* accept "all" to mean "all classes and packages" */
623 if (strcmp(enableAssertBuf+sizeof("-ea:")-1, "all") == 0)
624 enableAssertBuf[3] = '\0'; // truncate to "-ea"
625 ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700626 addOption(enableAssertBuf);
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700627 } else {
628 ALOGV("Assertions disabled\n");
629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
631 strcpy(jniOptsBuf, "-Xjniopts:");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700632 if (parseRuntimeOption("dalvik.vm.jniopts", jniOptsBuf, "-Xjniopts:")) {
633 ALOGI("JNI options: '%s'\n", jniOptsBuf);
634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 /* route exit() to our handler */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700637 addOption("exit", (void*) runtime_exit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638
639 /* route fprintf() to our handler */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700640 addOption("vfprintf", (void*) runtime_vfprintf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800642 /* register the framework-specific "is sensitive thread" hook */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700643 addOption("sensitiveThread", (void*) runtime_isSensitiveThread);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644
645 /* enable verbose; standard options are { jni, gc, class } */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700646 //addOption("-verbose:jni");
647 addOption("-verbose:gc");
648 //addOption("-verbose:class");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800650 /*
651 * The default starting and maximum size of the heap. Larger
652 * values should be specified in a product property override.
653 */
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700654 parseRuntimeOption("dalvik.vm.heapstartsize", heapstartsizeOptsBuf, "-Xms", "4m");
655 parseRuntimeOption("dalvik.vm.heapsize", heapsizeOptsBuf, "-Xmx", "16m");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656
Elliott Hughes3f177d72012-04-12 16:02:56 -0700657 // Increase the main thread's interpreter stack size for bug 6315322.
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700658 addOption("-XX:mainThreadStackSize=24K");
Elliott Hughes3f177d72012-04-12 16:02:56 -0700659
buzbeeb63de6d2013-05-24 13:14:08 -0700660 // Set the max jit code cache size. Note: size of 0 will disable the JIT.
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700661 parseRuntimeOption("dalvik.vm.jit.codecachesize",
662 jitcodecachesizeOptsBuf,
663 "-Xjitcodecachesize:");
buzbeeb63de6d2013-05-24 13:14:08 -0700664
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700665 parseRuntimeOption("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
666 parseRuntimeOption("dalvik.vm.heapminfree", heapminfreeOptsBuf, "-XX:HeapMinFree=");
667 parseRuntimeOption("dalvik.vm.heapmaxfree", heapmaxfreeOptsBuf, "-XX:HeapMaxFree=");
668 parseRuntimeOption("dalvik.vm.heaptargetutilization",
669 heaptargetutilizationOptsBuf,
670 "-XX:HeapTargetUtilization=");
Ian Rogers53250102012-09-23 16:38:03 -0700671
Mathieu Chartiere6c22412013-08-20 17:16:03 -0700672 property_get("ro.config.low_ram", propBuf, "");
673 if (strcmp(propBuf, "true") == 0) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700674 addOption("-XX:LowMemoryMode");
Mathieu Chartiere6c22412013-08-20 17:16:03 -0700675 }
676
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700677 parseRuntimeOption("dalvik.vm.gctype", gctypeOptsBuf, "-Xgc:");
678 parseRuntimeOption("dalvik.vm.backgroundgctype", backgroundgcOptsBuf, "-XX:BackgroundGC=");
Mathieu Chartierc01936a2014-05-14 15:44:49 -0700679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 /*
681 * Enable or disable dexopt features, such as bytecode verification and
682 * calculation of register maps for precise GC.
683 */
684 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
685 if (dexoptFlagsBuf[0] != '\0') {
686 const char* opc;
687 const char* val;
688
689 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
690 if (opc != NULL) {
691 switch (*(opc+2)) {
692 case 'n': val = "-Xverify:none"; break;
693 case 'r': val = "-Xverify:remote"; break;
694 case 'a': val = "-Xverify:all"; break;
695 default: val = NULL; break;
696 }
697
698 if (val != NULL) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700699 addOption(val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 }
701 }
702
703 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
704 if (opc != NULL) {
705 switch (*(opc+2)) {
706 case 'n': val = "-Xdexopt:none"; break;
707 case 'v': val = "-Xdexopt:verified"; break;
708 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700709 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 default: val = NULL; break;
711 }
712
713 if (val != NULL) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700714 addOption(val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716 }
717
718 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
719 if (opc != NULL) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700720 addOption("-Xgenregmap");
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700721
722 /* turn on precise GC while we're at it */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700723 addOption("-Xgc:precise");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 }
725 }
726
727 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 /* use android ADB transport */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700729 addOption("-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700731 parseRuntimeOption("dalvik.vm.lockprof.threshold",
732 lockProfThresholdBuf,
733 "-Xlockprofthreshold:");
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700734
Ben Cheng52b0e732009-06-19 13:31:12 -0700735 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700736 parseRuntimeOption("dalvik.vm.jit.op", jitOpBuf, "-Xjitop:");
Ben Cheng52b0e732009-06-19 13:31:12 -0700737
Ben Cheng52b0e732009-06-19 13:31:12 -0700738 /* Force interpreter-only mode for selected methods */
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700739 parseRuntimeOption("dalvik.vm.jit.method", jitMethodBuf, "-Xjitmethod:");
Ben Cheng52b0e732009-06-19 13:31:12 -0700740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 if (executionMode == kEMIntPortable) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700742 addOption("-Xint:portable");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 } else if (executionMode == kEMIntFast) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700744 addOption("-Xint:fast");
Ben Cheng52b0e732009-06-19 13:31:12 -0700745 } else if (executionMode == kEMJitCompiler) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700746 addOption("-Xint:jit");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700748
Brian Carlstrom98ba7bd2014-03-10 10:20:01 -0700749 // libart tolerates libdvm flags, but not vice versa, so only pass some options if libart.
Brian Carlstrom3c1ff822014-05-28 14:31:47 -0700750 property_get("persist.sys.dalvik.vm.lib.2", dalvikVmLibBuf, "libart.so");
Brian Carlstrom98ba7bd2014-03-10 10:20:01 -0700751 bool libart = (strncmp(dalvikVmLibBuf, "libart", 6) == 0);
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800752
Brian Carlstrom98ba7bd2014-03-10 10:20:01 -0700753 if (libart) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700754 // If we booting without the real /data, don't spend time compiling.
755 property_get("vold.decrypt", voldDecryptBuf, "");
756 bool skip_compilation = ((strcmp(voldDecryptBuf, "trigger_restart_min_framework") == 0) ||
757 (strcmp(voldDecryptBuf, "1") == 0));
758
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800759 // Extra options for boot.art/boot.oat image generation.
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700760 parseCompilerRuntimeOption("dalvik.vm.image-dex2oat-Xms", dex2oatXmsImageFlagsBuf,
761 "-Xms", "-Ximage-compiler-option");
762 parseCompilerRuntimeOption("dalvik.vm.image-dex2oat-Xmx", dex2oatXmxImageFlagsBuf,
763 "-Xmx", "-Ximage-compiler-option");
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700764 if (skip_compilation) {
765 addOption("-Ximage-compiler-option");
766 addOption("--compiler-filter=verify-none");
767 } else {
768 parseCompilerOption("dalvik.vm.image-dex2oat-filter", dex2oatImageCompilerFilterBuf,
769 "--compiler-filter=", "-Ximage-compiler-option");
770 }
Brian Carlstromce5bbbe2014-07-30 15:13:17 -0700771 addOption("-Ximage-compiler-option");
772 addOption("--image-classes-zip=/system/framework/framework.jar");
773 addOption("-Ximage-compiler-option");
774 addOption("--image-classes=preloaded-classes");
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800775 property_get("dalvik.vm.image-dex2oat-flags", dex2oatImageFlagsBuf, "");
776 parseExtraOpts(dex2oatImageFlagsBuf, "-Ximage-compiler-option");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700777
778 // Extra options for DexClassLoader.
779 parseCompilerRuntimeOption("dalvik.vm.dex2oat-Xms", dex2oatXmsFlagsBuf,
780 "-Xms", "-Xcompiler-option");
781 parseCompilerRuntimeOption("dalvik.vm.dex2oat-Xmx", dex2oatXmxFlagsBuf,
782 "-Xmx", "-Xcompiler-option");
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700783 if (skip_compilation) {
784 addOption("-Xcompiler-option");
Brian Carlstromdf6c83f2014-08-15 09:31:27 -0700785 addOption("--compiler-filter=verify-none");
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700786 } else {
787 parseCompilerOption("dalvik.vm.dex2oat-filter", dex2oatCompilerFilterBuf,
788 "--compiler-filter=", "-Xcompiler-option");
789 }
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700790 property_get("dalvik.vm.dex2oat-flags", dex2oatFlagsBuf, "");
791 parseExtraOpts(dex2oatFlagsBuf, "-Xcompiler-option");
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700792
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800793 }
794
Andy McFaddene4d81f22010-07-14 16:02:20 -0700795 /* extra options; parse this late so it overrides others */
796 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800797 parseExtraOpts(extraOptsBuf, NULL);
Andy McFaddene4d81f22010-07-14 16:02:20 -0700798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 /* Set the properties for locale */
800 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 strcpy(langOption, "-Duser.language=");
802 strcpy(regionOption, "-Duser.region=");
803 readLocale(langOption, regionOption);
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700804 addOption(langOption);
805 addOption(regionOption);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807
Dave Allison0efbd9a2014-01-30 14:19:51 -0800808 /*
809 * Set profiler options
810 */
Brian Carlstrom98ba7bd2014-03-10 10:20:01 -0700811 if (libart) {
Calin Juravlede99f082014-06-06 16:00:53 +0100812 // Whether or not the profiler should be enabled.
Calin Juravle9828d062014-06-02 16:45:13 +0100813 property_get("dalvik.vm.profiler", propBuf, "0");
814 if (propBuf[0] == '1') {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700815 addOption("-Xenable-profiler");
Calin Juravle9828d062014-06-02 16:45:13 +0100816 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800817
Calin Juravle3f967912014-06-10 14:52:24 +0100818 // Whether the profile should start upon app startup or be delayed by some random offset
819 // (in seconds) that is bound between 0 and a fixed value.
Calin Juravle994cd282014-06-11 18:21:41 +0100820 property_get("dalvik.vm.profile.start-immed", propBuf, "0");
Calin Juravle9828d062014-06-02 16:45:13 +0100821 if (propBuf[0] == '1') {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700822 addOption("-Xprofile-start-immediately");
Calin Juravle9828d062014-06-02 16:45:13 +0100823 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800824
Calin Juravle9828d062014-06-02 16:45:13 +0100825 // Number of seconds during profile runs.
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700826 parseRuntimeOption("dalvik.vm.profile.period-secs", profilePeriod, "-Xprofile-period:");
Dave Allison0efbd9a2014-01-30 14:19:51 -0800827
Calin Juravle9828d062014-06-02 16:45:13 +0100828 // Length of each profile run (seconds).
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700829 parseRuntimeOption("dalvik.vm.profile.duration-secs",
830 profileDuration,
831 "-Xprofile-duration:");
Dave Allison0efbd9a2014-01-30 14:19:51 -0800832
Calin Juravle9828d062014-06-02 16:45:13 +0100833 // Polling interval during profile run (microseconds).
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700834 parseRuntimeOption("dalvik.vm.profile.interval-us", profileInterval, "-Xprofile-interval:");
Calin Juravle9828d062014-06-02 16:45:13 +0100835
836 // Coefficient for period backoff. The the period is multiplied
837 // by this value after each profile run.
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700838 parseRuntimeOption("dalvik.vm.profile.backoff-coeff", profileBackoff, "-Xprofile-backoff:");
Calin Juravle9828d062014-06-02 16:45:13 +0100839
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700840 // Top K% of samples that are considered relevant when
841 // deciding if the app should be recompiled.
842 parseRuntimeOption("dalvik.vm.profile.top-k-thr",
843 profileTopKThreshold,
844 "-Xprofile-top-k-threshold:");
Calin Juravle9828d062014-06-02 16:45:13 +0100845
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700846 // The threshold after which a change in the structure of the
847 // top K% profiled samples becomes significant and triggers
848 // recompilation. A change in profile is considered
849 // significant if X% (top-k-change-threshold) of the top K%
850 // (top-k-threshold property) samples has changed.
851 parseRuntimeOption("dalvik.vm.profile.top-k-ch-thr",
852 profileTopKChangeThreshold,
853 "-Xprofile-top-k-change-threshold:");
Wei Jin54fc18b2014-06-09 16:29:54 -0700854
855 // Type of profile data.
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700856 parseRuntimeOption("dalvik.vm.profiler.type", profileType, "-Xprofile-type:");
Wei Jin54fc18b2014-06-09 16:29:54 -0700857
858 // Depth of bounded stack data
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700859 parseRuntimeOption("dalvik.vm.profile.max-stack-depth",
860 profileMaxStackDepth,
861 "-Xprofile-max-stack-depth:");
Dave Allison0efbd9a2014-01-30 14:19:51 -0800862
Calin Juravlebe20ed42014-08-26 23:00:11 +0100863 // Native bridge library. "0" means that native bridge is disabled.
864 property_get("ro.dalvik.vm.native.bridge", propBuf, "");
865 if (propBuf[0] == '\0') {
866 ALOGW("ro.dalvik.vm.native.bridge is not expected to be empty");
867 } else if (strcmp(propBuf, "0") != 0) {
868 snprintf(nativeBridgeLibrary, sizeof("-XX:NativeBridge=") + PROPERTY_VALUE_MAX,
869 "-XX:NativeBridge=%s", propBuf);
870 addOption(nativeBridgeLibrary);
871 }
872 }
Calin Juravle01db9162014-08-07 14:45:53 +0100873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 initArgs.version = JNI_VERSION_1_4;
875 initArgs.options = mOptions.editArray();
876 initArgs.nOptions = mOptions.size();
877 initArgs.ignoreUnrecognized = JNI_FALSE;
878
879 /*
880 * Initialize the VM.
881 *
882 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
883 * If this call succeeds, the VM is ready, and we can start issuing
884 * JNI calls.
885 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700886 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000887 ALOGE("JNI_CreateJavaVM failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 goto bail;
889 }
890
Andy McFaddenf70188a2009-03-31 15:52:13 -0700891 result = 0;
892
893bail:
Andy McFaddenf70188a2009-03-31 15:52:13 -0700894 return result;
895}
896
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700897char* AndroidRuntime::toSlashClassName(const char* className)
898{
899 char* result = strdup(className);
900 for (char* cp = result; *cp != '\0'; cp++) {
901 if (*cp == '.') {
902 *cp = '/';
903 }
904 }
905 return result;
906}
907
Mike Lockwood9ee5e7e2014-09-08 16:15:21 -0700908/** Create a Java string from an ASCII or Latin-1 string */
909jstring AndroidRuntime::NewStringLatin1(JNIEnv* env, const char* bytes) {
910 if (!bytes) return NULL;
911 int length = strlen(bytes);
912 jchar* buffer = (jchar *)alloca(length * sizeof(jchar));
913 if (!buffer) return NULL;
914 jchar* chp = buffer;
915 for (int i = 0; i < length; i++) {
916 *chp++ = *bytes++;
917 }
918 return env->NewString(buffer, length);
919}
920
921
Andy McFaddenf70188a2009-03-31 15:52:13 -0700922/*
923 * Start the Android runtime. This involves starting the virtual machine
924 * and calling the "static void main(String[] args)" method in the class
925 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700926 *
927 * Passes the main function two arguments, the class name and the specified
928 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700929 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100930void AndroidRuntime::start(const char* className, const Vector<String8>& options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700931{
Steve Block5baa3a62011-12-20 16:23:08 +0000932 ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700933 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700934
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100935 static const String8 startSystemServer("start-system-server");
936
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700937 /*
938 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700939 * init.rc anymore, so we print out the boot start event here.
940 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100941 for (size_t i = 0; i < options.size(); ++i) {
942 if (options[i] == startSystemServer) {
943 /* track our progress through the boot sequence */
944 const int LOG_BOOT_PROGRESS_START = 3000;
945 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
946 }
Andy McFaddenf70188a2009-03-31 15:52:13 -0700947 }
948
949 const char* rootDir = getenv("ANDROID_ROOT");
950 if (rootDir == NULL) {
951 rootDir = "/system";
952 if (!hasDir("/system")) {
953 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700954 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700955 }
956 setenv("ANDROID_ROOT", rootDir, 1);
957 }
958
959 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block5baa3a62011-12-20 16:23:08 +0000960 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700961
962 /* start the virtual machine */
Brian Carlstrom9f8203a2013-06-19 13:49:36 -0700963 JniInvocation jni_invocation;
964 jni_invocation.Init(NULL);
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700965 JNIEnv* env;
966 if (startVm(&mJavaVM, &env) != 0) {
967 return;
968 }
969 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 /*
972 * Register android functions.
973 */
974 if (startReg(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000975 ALOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700976 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 }
978
979 /*
980 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700981 * At present we have two arguments, the class name and an option string.
982 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 */
984 jclass stringClass;
985 jobjectArray strArray;
986 jstring classNameStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987
988 stringClass = env->FindClass("java/lang/String");
989 assert(stringClass != NULL);
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100990 strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 assert(strArray != NULL);
992 classNameStr = env->NewStringUTF(className);
993 assert(classNameStr != NULL);
994 env->SetObjectArrayElement(strArray, 0, classNameStr);
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100995
996 for (size_t i = 0; i < options.size(); ++i) {
997 jstring optionsStr = env->NewStringUTF(options.itemAt(i).string());
998 assert(optionsStr != NULL);
999 env->SetObjectArrayElement(strArray, i + 1, optionsStr);
1000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001
1002 /*
1003 * Start VM. This thread becomes the main thread of the VM, and will
1004 * not return until the VM exits.
1005 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001006 char* slashClassName = toSlashClassName(className);
1007 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 if (startClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001009 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 /* keep going */
1011 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001012 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 "([Ljava/lang/String;)V");
1014 if (startMeth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001015 ALOGE("JavaVM unable to find main() in '%s'\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 /* keep going */
1017 } else {
1018 env->CallStaticVoidMethod(startClass, startMeth, strArray);
1019
1020#if 0
1021 if (env->ExceptionCheck())
1022 threadExitUncaughtException(env);
1023#endif
1024 }
1025 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001026 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027
Steve Block5baa3a62011-12-20 16:23:08 +00001028 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 if (mJavaVM->DetachCurrentThread() != JNI_OK)
Steve Block8564c8d2012-01-05 23:22:43 +00001030 ALOGW("Warning: unable to detach main thread\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 if (mJavaVM->DestroyJavaVM() != 0)
Steve Block8564c8d2012-01-05 23:22:43 +00001032 ALOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033}
1034
Jeff Brown4280c4a2012-03-15 17:48:02 -07001035void AndroidRuntime::exit(int code)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036{
Jeff Brown4280c4a2012-03-15 17:48:02 -07001037 if (mExitWithoutCleanup) {
1038 ALOGI("VM exiting with result code %d, cleanup skipped.", code);
1039 ::_exit(code);
1040 } else {
1041 ALOGI("VM exiting with result code %d.", code);
1042 onExit(code);
1043 ::exit(code);
1044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045}
1046
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001047void AndroidRuntime::onVmCreated(JNIEnv* env)
1048{
1049 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
1050}
1051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052/*
Andreas Huber9ae000c2014-02-13 17:22:33 +00001053 * Get the JNIEnv pointer for this thread.
1054 *
1055 * Returns NULL if the slot wasn't allocated or populated.
1056 */
1057/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
1058{
1059 JNIEnv* env;
1060 JavaVM* vm = AndroidRuntime::getJavaVM();
1061 assert(vm != NULL);
1062
1063 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
1064 return NULL;
1065 return env;
1066}
1067
1068/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 * Makes the current thread visible to the VM.
1070 *
1071 * The JNIEnv pointer returned is only valid for the current thread, and
1072 * thus must be tucked into thread-local storage.
1073 */
1074static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
1075{
1076 JavaVMAttachArgs args;
1077 JavaVM* vm;
1078 jint result;
1079
1080 vm = AndroidRuntime::getJavaVM();
1081 assert(vm != NULL);
1082
1083 args.version = JNI_VERSION_1_4;
1084 args.name = (char*) threadName;
1085 args.group = NULL;
1086
1087 result = vm->AttachCurrentThread(pEnv, (void*) &args);
1088 if (result != JNI_OK)
Steve Block6215d3f2012-01-04 20:05:49 +00001089 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090
1091 return result;
1092}
1093
1094/*
1095 * Detach the current thread from the set visible to the VM.
1096 */
1097static int javaDetachThread(void)
1098{
1099 JavaVM* vm;
1100 jint result;
1101
1102 vm = AndroidRuntime::getJavaVM();
1103 assert(vm != NULL);
1104
1105 result = vm->DetachCurrentThread();
1106 if (result != JNI_OK)
Steve Block3762c312012-01-06 19:20:56 +00001107 ALOGE("ERROR: thread detach failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 return result;
1109}
1110
1111/*
1112 * When starting a native thread that will be visible from the VM, we
1113 * bounce through this to get the right attach/detach action.
1114 * Note that this function calls free(args)
1115 */
1116/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
1117 void* start = ((void**)args)[0];
1118 void* userData = ((void **)args)[1];
1119 char* name = (char*) ((void **)args)[2]; // we own this storage
1120 free(args);
1121 JNIEnv* env;
1122 int result;
1123
1124 /* hook us into the VM */
1125 if (javaAttachThread(name, &env) != JNI_OK)
1126 return -1;
1127
1128 /* start the thread running */
1129 result = (*(android_thread_func_t)start)(userData);
1130
1131 /* unhook us */
1132 javaDetachThread();
1133 free(name);
1134
1135 return result;
1136}
1137
1138/*
1139 * This is invoked from androidCreateThreadEtc() via the callback
1140 * set with androidSetCreateThreadFunc().
1141 *
1142 * We need to create the new thread in such a way that it gets hooked
1143 * into the VM before it really starts executing.
1144 */
1145/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001146 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 void* userData,
1148 const char* threadName,
1149 int32_t threadPriority,
1150 size_t threadStackSize,
1151 android_thread_id_t* threadId)
1152{
1153 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1154 int result;
1155
Mike Lockwood32f2e622013-10-10 10:11:27 -07001156 if (!threadName)
1157 threadName = "unnamed thread";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158
1159 args[0] = (void*) entryFunction;
1160 args[1] = userData;
1161 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1162
1163 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1164 threadName, threadPriority, threadStackSize, threadId);
1165 return result;
1166}
1167
1168/*
1169 * Create a thread that is visible from the VM.
1170 *
1171 * This is called from elsewhere in the library.
1172 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001173/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 void (*start)(void *), void* arg)
1175{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001176 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001178 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1179 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180}
1181
1182#if 0
1183static void quickTest(void* arg)
1184{
1185 const char* str = (const char*) arg;
1186
1187 printf("In quickTest: %s\n", str);
1188}
1189#endif
1190
1191#ifdef NDEBUG
1192 #define REG_JNI(name) { name }
1193 struct RegJNIRec {
1194 int (*mProc)(JNIEnv*);
1195 };
1196#else
1197 #define REG_JNI(name) { name, #name }
1198 struct RegJNIRec {
1199 int (*mProc)(JNIEnv*);
1200 const char* mName;
1201 };
1202#endif
1203
1204typedef void (*RegJAMProc)();
1205
1206static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1207{
1208 for (size_t i = 0; i < count; i++) {
1209 if (array[i].mProc(env) < 0) {
1210#ifndef NDEBUG
Steve Block5baa3a62011-12-20 16:23:08 +00001211 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212#endif
1213 return -1;
1214 }
1215 }
1216 return 0;
1217}
1218
1219static void register_jam_procs(const RegJAMProc array[], size_t count)
1220{
1221 for (size_t i = 0; i < count; i++) {
1222 array[i]();
1223 }
1224}
1225
1226static const RegJNIRec gRegJNI[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 REG_JNI(register_com_android_internal_os_RuntimeInit),
1228 REG_JNI(register_android_os_SystemClock),
1229 REG_JNI(register_android_util_EventLog),
1230 REG_JNI(register_android_util_Log),
1231 REG_JNI(register_android_util_FloatMath),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 REG_JNI(register_android_content_AssetManager),
1233 REG_JNI(register_android_content_StringBlock),
1234 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001235 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 REG_JNI(register_android_text_AndroidCharacter),
Anish Athalye88b5b0b2014-06-24 14:39:43 -07001237 REG_JNI(register_android_text_StaticLayout),
Doug Feltdae8e942010-02-24 14:33:15 -08001238 REG_JNI(register_android_text_AndroidBidi),
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001239 REG_JNI(register_android_view_InputDevice),
1240 REG_JNI(register_android_view_KeyCharacterMap),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001242 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 REG_JNI(register_android_os_Binder),
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001244 REG_JNI(register_android_os_Parcel),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 REG_JNI(register_android_nio_utils),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 REG_JNI(register_android_graphics_Graphics),
Chris Craik54389792013-12-20 13:28:11 -08001247 REG_JNI(register_android_view_DisplayEventReceiver),
John Reckf666ad72014-03-14 16:24:57 -07001248 REG_JNI(register_android_view_RenderNode),
John Recke45b1fd2014-04-15 09:50:16 -07001249 REG_JNI(register_android_view_RenderNodeAnimator),
Romain Guy3b748a42013-04-17 18:54:38 -07001250 REG_JNI(register_android_view_GraphicBuffer),
Romain Guye4d01122010-06-16 18:44:05 -07001251 REG_JNI(register_android_view_GLES20Canvas),
John Reck04fc5832014-02-05 16:38:25 -08001252 REG_JNI(register_android_view_HardwareLayer),
John Reckcec24ae2013-11-05 13:27:50 -08001253 REG_JNI(register_android_view_ThreadedRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 REG_JNI(register_android_view_Surface),
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001255 REG_JNI(register_android_view_SurfaceControl),
Jeff Brown64a55af2012-08-26 02:47:39 -07001256 REG_JNI(register_android_view_SurfaceSession),
Romain Guy8f0095c2011-05-02 17:24:22 -07001257 REG_JNI(register_android_view_TextureView),
John Reck315c3292014-05-09 19:21:04 -07001258 REG_JNI(register_com_android_internal_view_animation_NativeInterpolatorFactoryHelper),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1260 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Thomas Tafertshofer6b1e8382012-07-03 13:37:35 -07001261 REG_JNI(register_android_opengl_jni_EGL14),
Jesse Hall237c2b82013-05-06 11:36:57 -07001262 REG_JNI(register_android_opengl_jni_EGLExt),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001263 REG_JNI(register_android_opengl_jni_GLES10),
1264 REG_JNI(register_android_opengl_jni_GLES10Ext),
1265 REG_JNI(register_android_opengl_jni_GLES11),
1266 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001267 REG_JNI(register_android_opengl_jni_GLES20),
Jesse Halld877efe2013-04-29 15:59:35 -07001268 REG_JNI(register_android_opengl_jni_GLES30),
Jesse Hall7ab63ac2014-05-19 15:13:41 -07001269 REG_JNI(register_android_opengl_jni_GLES31),
1270 REG_JNI(register_android_opengl_jni_GLES31Ext),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271
1272 REG_JNI(register_android_graphics_Bitmap),
1273 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001274 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 REG_JNI(register_android_graphics_Camera),
Leon Scroggins IIId0d7eaf2013-09-06 16:46:57 -04001276 REG_JNI(register_android_graphics_CreateJavaOutputStreamAdaptor),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 REG_JNI(register_android_graphics_Canvas),
John Reck52244ff2014-05-01 21:27:37 -07001278 REG_JNI(register_android_graphics_CanvasProperty),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 REG_JNI(register_android_graphics_ColorFilter),
1280 REG_JNI(register_android_graphics_DrawFilter),
Raph Levien1a73f732014-01-30 16:06:28 -08001281 REG_JNI(register_android_graphics_FontFamily),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 REG_JNI(register_android_graphics_Interpolator),
1283 REG_JNI(register_android_graphics_LayerRasterizer),
1284 REG_JNI(register_android_graphics_MaskFilter),
1285 REG_JNI(register_android_graphics_Matrix),
1286 REG_JNI(register_android_graphics_Movie),
1287 REG_JNI(register_android_graphics_NinePatch),
1288 REG_JNI(register_android_graphics_Paint),
1289 REG_JNI(register_android_graphics_Path),
1290 REG_JNI(register_android_graphics_PathMeasure),
1291 REG_JNI(register_android_graphics_PathEffect),
1292 REG_JNI(register_android_graphics_Picture),
1293 REG_JNI(register_android_graphics_PorterDuff),
1294 REG_JNI(register_android_graphics_Rasterizer),
1295 REG_JNI(register_android_graphics_Region),
1296 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001297 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 REG_JNI(register_android_graphics_Typeface),
1299 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001300 REG_JNI(register_android_graphics_YuvImage),
Svetoslav6811f4e2013-09-18 15:58:28 -07001301 REG_JNI(register_android_graphics_pdf_PdfDocument),
Svetoslav62ce3322014-09-04 21:17:17 -07001302 REG_JNI(register_android_graphics_pdf_PdfEditor),
Svetoslav29617692014-04-24 18:40:42 -07001303 REG_JNI(register_android_graphics_pdf_PdfRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304
1305 REG_JNI(register_android_database_CursorWindow),
Jeff Browne5360fb2011-10-31 17:48:13 -07001306 REG_JNI(register_android_database_SQLiteConnection),
1307 REG_JNI(register_android_database_SQLiteGlobal),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 REG_JNI(register_android_database_SQLiteDebug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 REG_JNI(register_android_os_FileObserver),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001311 REG_JNI(register_android_os_MessageQueue),
Stephen Smalleyc07fca32012-01-13 08:31:39 -05001312 REG_JNI(register_android_os_SELinux),
Jeff Brown481c1572012-03-09 14:41:15 -08001313 REG_JNI(register_android_os_Trace),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 REG_JNI(register_android_os_UEventObserver),
1315 REG_JNI(register_android_net_LocalSocketImpl),
1316 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001317 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 REG_JNI(register_android_os_MemoryFile),
1319 REG_JNI(register_com_android_internal_os_ZygoteInit),
Narayan Kamath973b4662014-03-31 13:41:26 +01001320 REG_JNI(register_com_android_internal_os_Zygote),
John Reck9fa40712014-05-09 15:26:59 -07001321 REG_JNI(register_com_android_internal_util_VirtualRefBasePtr),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 REG_JNI(register_android_hardware_Camera),
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -07001323 REG_JNI(register_android_hardware_camera2_CameraMetadata),
Ruben Brunkfeb50af2014-05-09 19:58:49 -07001324 REG_JNI(register_android_hardware_camera2_legacy_LegacyCameraDevice),
Eino-Ville Talvalae1f57d62014-05-29 17:17:07 -07001325 REG_JNI(register_android_hardware_camera2_legacy_PerfMeasurement),
Ruben Brunkb6079002014-05-22 12:33:54 -07001326 REG_JNI(register_android_hardware_camera2_DngCreator),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -04001328 REG_JNI(register_android_hardware_SerialPort),
Eric Laurent60b62bc2014-04-18 17:50:49 -07001329 REG_JNI(register_android_hardware_SoundTrigger),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001330 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001331 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001332 REG_JNI(register_android_hardware_UsbRequest),
destradaaa4fa3b52014-07-09 10:46:39 -07001333 REG_JNI(register_android_hardware_location_ActivityRecognitionHardware),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 REG_JNI(register_android_media_AudioRecord),
1335 REG_JNI(register_android_media_AudioSystem),
1336 REG_JNI(register_android_media_AudioTrack),
1337 REG_JNI(register_android_media_JetPlayer),
Jeff Browncbad9762012-09-04 21:57:59 -07001338 REG_JNI(register_android_media_RemoteDisplay),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 REG_JNI(register_android_media_ToneGenerator),
1340
1341 REG_JNI(register_android_opengl_classes),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001342 REG_JNI(register_android_server_NetworkManagementSocketTagger),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001344 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001345 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001346 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001347 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001348 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001349 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001350 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001351 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001352 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brownc28867a2013-03-26 15:42:39 -07001353 REG_JNI(register_android_view_InputEventSender),
Michael Wrighta44dd262013-04-10 21:12:00 -07001354 REG_JNI(register_android_view_InputQueue),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001355 REG_JNI(register_android_view_KeyEvent),
1356 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001357 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001358 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001359
1360 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001361 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001362
1363 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001364 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001365 REG_JNI(register_com_android_internal_net_NetworkStatsFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366};
1367
1368/*
1369 * Register android native functions with the VM.
1370 */
1371/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1372{
1373 /*
1374 * This hook causes all future threads created in this process to be
1375 * attached to the JavaVM. (This needs to go away in favor of JNI
1376 * Attach calls.)
1377 */
1378 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1379
Steve Block71f2cf12011-10-20 11:56:00 +01001380 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
1382 /*
1383 * Every "register" function calls one or more things that return
1384 * a local reference (e.g. FindClass). Because we haven't really
1385 * started the VM yet, they're all getting stored in the base frame
1386 * and never released. Use Push/Pop to manage the storage.
1387 */
1388 env->PushLocalFrame(200);
1389
1390 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1391 env->PopLocalFrame(NULL);
1392 return -1;
1393 }
1394 env->PopLocalFrame(NULL);
1395
1396 //createJavaThread("fubar", quickTest, (void*) "hello");
1397
1398 return 0;
1399}
1400
1401AndroidRuntime* AndroidRuntime::getRuntime()
1402{
1403 return gCurRuntime;
1404}
1405
1406/**
1407 * Used by WithFramework to register native functions.
1408 */
1409extern "C"
1410jint Java_com_android_internal_util_WithFramework_registerNatives(
1411 JNIEnv* env, jclass clazz) {
1412 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1413}
1414
1415/**
1416 * Used by LoadClass to register native functions.
1417 */
1418extern "C"
1419jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1420 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1421}
1422
1423} // namespace android