blob: d826303f4f8c92ab6ef71ba7db34c9358ea97194 [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 -080098namespace android {
99
100/*
101 * JNI-based registration functions. Note these are properly contained in
102 * namespace android.
103 */
104extern int register_android_content_AssetManager(JNIEnv* env);
105extern int register_android_util_EventLog(JNIEnv* env);
106extern int register_android_util_Log(JNIEnv* env);
107extern int register_android_content_StringBlock(JNIEnv* env);
108extern int register_android_content_XmlBlock(JNIEnv* env);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700109extern int register_android_emoji_EmojiFactory(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110extern int register_android_graphics_Canvas(JNIEnv* env);
John Reck52244ff2014-05-01 21:27:37 -0700111extern int register_android_graphics_CanvasProperty(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112extern int register_android_graphics_ColorFilter(JNIEnv* env);
113extern int register_android_graphics_DrawFilter(JNIEnv* env);
Raph Levien1a73f7322014-01-30 16:06:28 -0800114extern int register_android_graphics_FontFamily(JNIEnv* env);
Mike Reed4c9355c2014-05-07 11:48:37 -0400115extern int register_android_graphics_LayerRasterizer(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116extern int register_android_graphics_Matrix(JNIEnv* env);
117extern int register_android_graphics_Paint(JNIEnv* env);
118extern int register_android_graphics_Path(JNIEnv* env);
119extern int register_android_graphics_PathMeasure(JNIEnv* env);
120extern int register_android_graphics_Picture(JNIEnv*);
121extern int register_android_graphics_PorterDuff(JNIEnv* env);
122extern int register_android_graphics_Rasterizer(JNIEnv* env);
Jeff Brownfbf09772011-01-16 14:06:57 -0800123extern int register_android_graphics_Region(JNIEnv* env);
Jamie Gennisaa0ce332011-01-06 17:04:26 -0800124extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125extern int register_android_graphics_Xfermode(JNIEnv* env);
Svetoslav6811f4e2013-09-18 15:58:28 -0700126extern int register_android_graphics_pdf_PdfDocument(JNIEnv* env);
Svetoslav62ce3322014-09-04 21:17:17 -0700127extern int register_android_graphics_pdf_PdfEditor(JNIEnv* env);
Svetoslav29617692014-04-24 18:40:42 -0700128extern int register_android_graphics_pdf_PdfRenderer(JNIEnv* env);
Jeff Brown0a0a1242011-12-02 02:25:22 -0800129extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
John Reckf666ad72014-03-14 16:24:57 -0700130extern int register_android_view_RenderNode(JNIEnv* env);
John Recke45b1fd2014-04-15 09:50:16 -0700131extern int register_android_view_RenderNodeAnimator(JNIEnv* env);
Romain Guy3b748a42013-04-17 18:54:38 -0700132extern int register_android_view_GraphicBuffer(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700133extern int register_android_view_GLES20Canvas(JNIEnv* env);
John Reck04fc5832014-02-05 16:38:25 -0800134extern int register_android_view_HardwareLayer(JNIEnv* env);
John Reckcec24ae2013-11-05 13:27:50 -0800135extern int register_android_view_ThreadedRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136extern int register_android_view_Surface(JNIEnv* env);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800137extern int register_android_view_SurfaceControl(JNIEnv* env);
Jeff Brown64a55af2012-08-26 02:47:39 -0700138extern int register_android_view_SurfaceSession(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700139extern int register_android_view_TextureView(JNIEnv* env);
John Reck315c3292014-05-09 19:21:04 -0700140extern int register_com_android_internal_view_animation_NativeInterpolatorFactoryHelper(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141extern int register_android_database_CursorWindow(JNIEnv* env);
Jeff Browne5360fb2011-10-31 17:48:13 -0700142extern int register_android_database_SQLiteConnection(JNIEnv* env);
143extern int register_android_database_SQLiteGlobal(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144extern int register_android_database_SQLiteDebug(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145extern int register_android_nio_utils(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700147extern int register_android_os_MessageQueue(JNIEnv* env);
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800148extern int register_android_os_Parcel(JNIEnv* env);
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500149extern int register_android_os_SELinux(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151extern int register_android_os_SystemClock(JNIEnv* env);
Jeff Brown481c1572012-03-09 14:41:15 -0800152extern int register_android_os_Trace(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153extern int register_android_os_FileObserver(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154extern int register_android_os_UEventObserver(JNIEnv* env);
155extern int register_android_os_MemoryFile(JNIEnv* env);
156extern int register_android_net_LocalSocketImpl(JNIEnv* env);
157extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700158extern int register_android_net_TrafficStats(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159extern int register_android_text_AndroidCharacter(JNIEnv *env);
Anish Athalye88b5b0b2014-06-24 14:39:43 -0700160extern int register_android_text_StaticLayout(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800161extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162extern int register_android_opengl_classes(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
Narayan Kamath29564cd2014-08-07 10:57:40 +0100164extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700165extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700166extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700167extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400168extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Christopher Tate4a627c72011-04-01 14:43:32 -0700169extern int register_android_app_backup_FullBackup(JNIEnv *env);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700170extern int register_android_app_ActivityThread(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700171extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Browncbad9762012-09-04 21:57:59 -0700172extern int register_android_media_RemoteDisplay(JNIEnv *env);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700173extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700174extern int register_android_view_InputDevice(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800175extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brownc28867a2013-03-26 15:42:39 -0700176extern int register_android_view_InputEventSender(JNIEnv* env);
Michael Wrighta44dd262013-04-10 21:12:00 -0700177extern int register_android_view_InputQueue(JNIEnv* env);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700178extern int register_android_view_KeyCharacterMap(JNIEnv *env);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700179extern int register_android_view_KeyEvent(JNIEnv* env);
180extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700181extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700182extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700183extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700184extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700185extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700186extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -0800187extern int register_com_android_internal_net_NetworkStatsFactory(JNIEnv *env);
Narayan Kamath973b4662014-03-31 13:41:26 +0100188extern int register_com_android_internal_os_Zygote(JNIEnv *env);
John Reck9fa40712014-05-09 15:26:59 -0700189extern int register_com_android_internal_util_VirtualRefBasePtr(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
191static AndroidRuntime* gCurRuntime = NULL;
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193/*
194 * Code written in the Java Programming Language calls here from main().
195 */
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700196static void com_android_internal_os_RuntimeInit_nativeFinishInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197{
198 gCurRuntime->onStarted();
199}
200
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700201static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202{
203 gCurRuntime->onZygoteInit();
204}
205
Jeff Brown4280c4a2012-03-15 17:48:02 -0700206static void com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup(JNIEnv* env,
207 jobject clazz, jboolean exitWithoutCleanup)
208{
209 gCurRuntime->setExitWithoutCleanup(exitWithoutCleanup);
210}
211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212/*
213 * JNI registration.
214 */
215static JNINativeMethod gMethods[] = {
Jeff Brown16f5f5c2012-03-15 16:53:55 -0700216 { "nativeFinishInit", "()V",
217 (void*) com_android_internal_os_RuntimeInit_nativeFinishInit },
218 { "nativeZygoteInit", "()V",
219 (void*) com_android_internal_os_RuntimeInit_nativeZygoteInit },
Jeff Brown4280c4a2012-03-15 17:48:02 -0700220 { "nativeSetExitWithoutCleanup", "(Z)V",
221 (void*) com_android_internal_os_RuntimeInit_nativeSetExitWithoutCleanup },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222};
223
224int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
225{
226 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
227 gMethods, NELEM(gMethods));
228}
229
230// ----------------------------------------------------------------------
231
Andreas Huber9ae000c2014-02-13 17:22:33 +0000232/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
233
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000234AndroidRuntime::AndroidRuntime(char* argBlockStart, const size_t argBlockLength) :
235 mExitWithoutCleanup(false),
236 mArgBlockStart(argBlockStart),
237 mArgBlockLength(argBlockLength)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700239 SkGraphics::Init();
Mike Reedfc8db532009-04-27 11:43:30 -0400240 // There is also a global font cache, but its budget is specified in code
241 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
243 // Pre-allocate enough space to hold a fair number of options.
244 mOptions.setCapacity(20);
245
246 assert(gCurRuntime == NULL); // one per process
247 gCurRuntime = this;
248}
249
250AndroidRuntime::~AndroidRuntime()
251{
252 SkGraphics::Term();
253}
254
Andreas Huber9ae000c2014-02-13 17:22:33 +0000255/*
256 * Register native methods using JNI.
257 */
258/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
259 const char* className, const JNINativeMethod* gMethods, int numMethods)
260{
261 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
262}
263
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000264void AndroidRuntime::setArgv0(const char* argv0) {
Jeff Brown00c0cd42014-09-10 16:48:46 -0700265 memset(mArgBlockStart, 0, mArgBlockLength);
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000266 strlcpy(mArgBlockStart, argv0, mArgBlockLength);
267}
268
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100269status_t AndroidRuntime::callMain(const String8& className, jclass clazz,
270 const Vector<String8>& args)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271{
272 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 jmethodID methodId;
274
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100275 ALOGD("Calling main entry %s", className.string());
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700278 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 return UNKNOWN_ERROR;
280 }
281
282 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
283 if (methodId == NULL) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100284 ALOGE("ERROR: could not find method %s.main(String[])\n", className.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 return UNKNOWN_ERROR;
286 }
287
288 /*
289 * We want to call main() with a String array with our arguments in it.
290 * Create an array and populate it.
291 */
292 jclass stringClass;
293 jobjectArray strArray;
294
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100295 const size_t numArgs = args.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 stringClass = env->FindClass("java/lang/String");
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100297 strArray = env->NewObjectArray(numArgs, stringClass, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100299 for (size_t i = 0; i < numArgs; i++) {
300 jstring argStr = env->NewStringUTF(args[i].string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 env->SetObjectArrayElement(strArray, i, argStr);
302 }
303
304 env->CallStaticVoidMethod(clazz, methodId, strArray);
305 return NO_ERROR;
306}
307
308/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 * The VM calls this through the "exit" hook.
310 */
311static void runtime_exit(int code)
312{
Jeff Brown4280c4a2012-03-15 17:48:02 -0700313 gCurRuntime->exit(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314}
315
316/*
317 * The VM calls this through the "vfprintf" hook.
318 *
319 * We ignore "fp" and just write the results to the log file.
320 */
321static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
322{
323 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
324}
325
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800326/**
327 * The VM calls this when mutex contention debugging is enabled to
328 * determine whether or not the blocked thread was a "sensitive thread"
329 * for user responsiveness/smoothess.
330 *
331 * Our policy for this is whether or not we're tracing any StrictMode
332 * events on this thread (which we might've inherited via Binder calls
333 * into us)
334 */
335static bool runtime_isSensitiveThread() {
336 IPCThreadState* state = IPCThreadState::selfOrNull();
337 return state && state->getStrictModePolicy() != 0;
338}
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340static int hasDir(const char* dir)
341{
342 struct stat s;
343 int res = stat(dir, &s);
344 if (res == 0) {
345 return S_ISDIR(s.st_mode);
346 }
347 return 0;
348}
349
Andreas Gampe995c62d2014-11-14 16:15:17 -0800350static bool hasFile(const char* file) {
351 struct stat s;
352 int res = stat(file, &s);
353 if (res == 0) {
354 return S_ISREG(s.st_mode);
355 }
356 return false;
357}
358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359/*
Narayan Kamathd30dbb82015-01-15 14:48:15 +0000360 * Read the persistent locale. Attempts to read to persist.sys.locale
361 * and falls back to the default locale (ro.product.locale) if
362 * persist.sys.locale is empty.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 */
Narayan Kamathd30dbb82015-01-15 14:48:15 +0000364static void readLocale(char* locale)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365{
Narayan Kamathd30dbb82015-01-15 14:48:15 +0000366 // Allocate 4 extra bytes because we might read a property into
367 // this array at offset 4.
368 char propLocale[PROPERTY_VALUE_MAX + 4];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700369
Narayan Kamathd30dbb82015-01-15 14:48:15 +0000370 property_get("persist.sys.locale", propLocale, "");
371 if (propLocale[0] == 0) {
372 property_get("ro.product.locale", propLocale, "");
373
374 if (propLocale[0] == 0) {
375 // If persist.sys.locale and ro.product.locale are missing,
376 // construct a locale value from the individual locale components.
377 property_get("ro.product.locale.language", propLocale, "en");
378
379 // The language code is either two or three chars in length. If it
380 // isn't 2 chars long, assume three. Anything else is an error
381 // anyway.
382 const int offset = (propLocale[2] == 0) ? 2 : 3;
383 propLocale[offset] = '-';
384
385 property_get("ro.product.locale.region", propLocale + offset + 1, "US");
386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
Narayan Kamathd30dbb82015-01-15 14:48:15 +0000388
389 strncat(locale, propLocale, PROPERTY_VALUE_MAX);
390 // ALOGD("[DEBUG] locale=%s", locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391}
392
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700393void AndroidRuntime::addOption(const char* optionString, void* extraInfo)
394{
395 JavaVMOption opt;
396 opt.optionString = optionString;
397 opt.extraInfo = extraInfo;
398 mOptions.add(opt);
399}
400
Andy McFaddenf70188a2009-03-31 15:52:13 -0700401/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700402 * Parse a property containing space-separated options that should be
403 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
404 *
405 * This will cut up "extraOptsBuf" as we chop it into individual options.
406 *
Brian Carlstrom3beff1e02014-02-28 23:27:22 -0800407 * If "quotingArg" is non-null, it is passed before each extra option in mOptions.
408 *
Andy McFaddene4d81f22010-07-14 16:02:20 -0700409 * Adds the strings, if any, to mOptions.
410 */
Brian Carlstrom3beff1e02014-02-28 23:27:22 -0800411void AndroidRuntime::parseExtraOpts(char* extraOptsBuf, const char* quotingArg)
Andy McFaddene4d81f22010-07-14 16:02:20 -0700412{
Brian Carlstrom3beff1e02014-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
Brian Carlstrom3beff1e02014-02-28 23:27:22 -0800427 if (quotingArg != NULL) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700428 addOption(quotingArg);
Brian Carlstrom3beff1e02014-02-28 23:27:22 -0800429 }
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700430 addOption(start);
Andy McFaddene4d81f22010-07-14 16:02:20 -0700431 start = end;
432 }
433}
434
435/*
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700436 * Reads a "property" into "buffer" with a default of "defaultArg". If
437 * the property is non-empty, it is treated as a runtime option such
438 * as "-Xmx32m".
439 *
440 * The "runtimeArg" is a prefix for the option such as "-Xms" or "-Xmx".
441 *
442 * If an argument is found, it is added to mOptions.
443 *
444 * If an option is found, it is added to mOptions and true is
445 * returned. Otherwise false is returned.
446 */
447bool AndroidRuntime::parseRuntimeOption(const char* property,
448 char* buffer,
449 const char* runtimeArg,
450 const char* defaultArg)
451{
452 strcpy(buffer, runtimeArg);
453 size_t runtimeArgLen = strlen(runtimeArg);
454 property_get(property, buffer+runtimeArgLen, defaultArg);
455 if (buffer[runtimeArgLen] == '\0') {
456 return false;
457 }
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700458 addOption(buffer);
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700459 return true;
460}
461
462/*
463 * Reads a "property" into "buffer". If the property is non-empty, it
Brian Carlstrom3fbfbb42014-07-28 19:13:28 -0700464 * is treated as a dex2oat compiler option that should be
465 * passed as a quoted option, e.g. "-Ximage-compiler-option --compiler-filter=verify-none".
466 *
467 * The "compilerArg" is a prefix for the option such as "--compiler-filter=".
468 *
469 * The "quotingArg" should be "-Ximage-compiler-option" or "-Xcompiler-option".
470 *
471 * If an option is found, it is added to mOptions and true is
472 * returned. Otherwise false is returned.
473 */
474bool AndroidRuntime::parseCompilerOption(const char* property,
475 char* buffer,
476 const char* compilerArg,
477 const char* quotingArg)
478{
479 strcpy(buffer, compilerArg);
480 size_t compilerArgLen = strlen(compilerArg);
481 property_get(property, buffer+compilerArgLen, "");
482 if (buffer[compilerArgLen] == '\0') {
483 return false;
484 }
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700485 addOption(quotingArg);
486 addOption(buffer);
Brian Carlstrom3fbfbb42014-07-28 19:13:28 -0700487 return true;
488}
489
490/*
491 * Reads a "property" into "buffer". If the property is non-empty, it
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700492 * is treated as a dex2oat compiler runtime option that should be
493 * passed as a quoted option, e.g. "-Ximage-compiler-option
494 * --runtime-arg -Ximage-compiler-option -Xmx32m".
495 *
496 * The "runtimeArg" is a prefix for the option such as "-Xms" or "-Xmx".
497 *
498 * The "quotingArg" should be "-Ximage-compiler-option" or "-Xcompiler-option".
499 *
500 * If an option is found, it is added to mOptions and true is
501 * returned. Otherwise false is returned.
502 */
503bool AndroidRuntime::parseCompilerRuntimeOption(const char* property,
504 char* buffer,
505 const char* runtimeArg,
506 const char* quotingArg)
507{
508 strcpy(buffer, runtimeArg);
509 size_t runtimeArgLen = strlen(runtimeArg);
510 property_get(property, buffer+runtimeArgLen, "");
511 if (buffer[runtimeArgLen] == '\0') {
512 return false;
513 }
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700514 addOption(quotingArg);
515 addOption("--runtime-arg");
516 addOption(quotingArg);
517 addOption(buffer);
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700518 return true;
519}
520
521/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700522 * Start the Dalvik Virtual Machine.
523 *
524 * Various arguments, most determined by system properties, are passed in.
525 * The "mOptions" vector is updated.
526 *
Dave Allison07a1e232014-03-14 08:54:33 -0700527 * CAUTION: when adding options in here, be careful not to put the
528 * char buffer inside a nested scope. Adding the buffer to the
529 * options using mOptions.add() does not copy the buffer, so if the
530 * buffer goes out of scope the option may be overwritten. It's best
531 * to put the buffer at the top of the function so that it is more
532 * unlikely that someone will surround it in a scope at a later time
533 * and thus introduce a bug.
534 *
Andy McFaddenf70188a2009-03-31 15:52:13 -0700535 * Returns 0 on success.
536 */
537int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 JavaVMInitArgs initArgs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 char propBuf[PROPERTY_VALUE_MAX];
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700541 char stackTraceFileBuf[sizeof("-Xstacktracefile:")-1 + PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800543 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700544 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c32011-01-19 12:33:12 -0800545 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Ian Rogers53250102012-09-23 16:38:03 -0700546 char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
547 char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
Mathieu Chartier6909c0e2015-03-05 17:00:41 -0800548 char usejitOptsBuf[sizeof("-Xusejit:")-1 + PROPERTY_VALUE_MAX];
549 char jitcodecachesizeOptsBuf[sizeof("-Xjitcodecachesize:")-1 + PROPERTY_VALUE_MAX];
550 char jitthresholdOptsBuf[sizeof("-Xjitthreshold:")-1 + PROPERTY_VALUE_MAX];
Mathieu Chartier7e4fdec2013-12-02 15:46:51 -0800551 char gctypeOptsBuf[sizeof("-Xgc:")-1 + PROPERTY_VALUE_MAX];
Mathieu Chartierc01936a2014-05-14 15:44:49 -0700552 char backgroundgcOptsBuf[sizeof("-XX:BackgroundGC=")-1 + PROPERTY_VALUE_MAX];
Ian Rogers53250102012-09-23 16:38:03 -0700553 char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
Andreas Gampee324ba02015-03-31 16:15:17 -0700554 char cachePruneBuf[sizeof("-Xzygote-max-boot-retry=")-1 + PROPERTY_VALUE_MAX];
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700555 char dex2oatXmsImageFlagsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
556 char dex2oatXmxImageFlagsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
557 char dex2oatXmsFlagsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
558 char dex2oatXmxFlagsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Brian Carlstrom3fbfbb42014-07-28 19:13:28 -0700559 char dex2oatCompilerFilterBuf[sizeof("--compiler-filter=")-1 + PROPERTY_VALUE_MAX];
560 char dex2oatImageCompilerFilterBuf[sizeof("--compiler-filter=")-1 + PROPERTY_VALUE_MAX];
Andreas Gampee0352382015-03-30 18:43:39 -0700561 char dex2oatThreadsBuf[sizeof("-j")-1 + PROPERTY_VALUE_MAX];
562 char dex2oatThreadsImageBuf[sizeof("-j")-1 + PROPERTY_VALUE_MAX];
Andreas Gampe605cca12015-04-02 23:12:56 -0700563 char dex2oat_isa_variant_key[PROPERTY_KEY_MAX];
564 char dex2oat_isa_variant[sizeof("--instruction-set-variant=") -1 + PROPERTY_VALUE_MAX];
565 char dex2oat_isa_features_key[PROPERTY_KEY_MAX];
566 char dex2oat_isa_features[sizeof("--instruction-set-features=") -1 + PROPERTY_VALUE_MAX];
Brian Carlstrom3beff1e02014-02-28 23:27:22 -0800567 char dex2oatFlagsBuf[PROPERTY_VALUE_MAX];
568 char dex2oatImageFlagsBuf[PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700569 char extraOptsBuf[PROPERTY_VALUE_MAX];
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700570 char voldDecryptBuf[PROPERTY_VALUE_MAX];
Ben Cheng52b0e732009-06-19 13:31:12 -0700571 enum {
572 kEMDefault,
573 kEMIntPortable,
574 kEMIntFast,
Ben Cheng52b0e732009-06-19 13:31:12 -0700575 kEMJitCompiler,
Ben Cheng52b0e732009-06-19 13:31:12 -0700576 } executionMode = kEMDefault;
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700577 char profilePeriod[sizeof("-Xprofile-period:")-1 + PROPERTY_VALUE_MAX];
578 char profileDuration[sizeof("-Xprofile-duration:")-1 + PROPERTY_VALUE_MAX];
579 char profileInterval[sizeof("-Xprofile-interval:")-1 + PROPERTY_VALUE_MAX];
580 char profileBackoff[sizeof("-Xprofile-backoff:")-1 + PROPERTY_VALUE_MAX];
581 char profileTopKThreshold[sizeof("-Xprofile-top-k-threshold:")-1 + PROPERTY_VALUE_MAX];
582 char profileTopKChangeThreshold[sizeof("-Xprofile-top-k-change-threshold:")-1 +
583 PROPERTY_VALUE_MAX];
584 char profileType[sizeof("-Xprofile-type:")-1 + PROPERTY_VALUE_MAX];
585 char profileMaxStackDepth[sizeof("-Xprofile-max-stack-depth:")-1 + PROPERTY_VALUE_MAX];
Narayan Kamathd30dbb82015-01-15 14:48:15 +0000586 char localeOption[sizeof("-Duser.locale=") + PROPERTY_VALUE_MAX];
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700587 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:")-1 + PROPERTY_VALUE_MAX];
Calin Juravle01db9162014-08-07 14:45:53 +0100588 char nativeBridgeLibrary[sizeof("-XX:NativeBridge=") + PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700590 bool checkJni = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 property_get("dalvik.vm.checkjni", propBuf, "");
592 if (strcmp(propBuf, "true") == 0) {
593 checkJni = true;
594 } else if (strcmp(propBuf, "false") != 0) {
595 /* property is neither true nor false; fall back on kernel parameter */
596 property_get("ro.kernel.android.checkjni", propBuf, "");
597 if (propBuf[0] == '1') {
598 checkJni = true;
599 }
600 }
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700601 ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
602 if (checkJni) {
603 /* extended JNI checking */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700604 addOption("-Xcheck:jni");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700605
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700606 /* with -Xcheck:jni, this provides a JNI function call trace */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700607 //addOption("-verbose:jni");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 property_get("dalvik.vm.execution-mode", propBuf, "");
611 if (strcmp(propBuf, "int:portable") == 0) {
612 executionMode = kEMIntPortable;
613 } else if (strcmp(propBuf, "int:fast") == 0) {
614 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700615 } else if (strcmp(propBuf, "int:jit") == 0) {
616 executionMode = kEMJitCompiler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 }
618
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700619 parseRuntimeOption("dalvik.vm.stack-trace-file", stackTraceFileBuf, "-Xstacktracefile:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 strcpy(jniOptsBuf, "-Xjniopts:");
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700622 if (parseRuntimeOption("dalvik.vm.jniopts", jniOptsBuf, "-Xjniopts:")) {
623 ALOGI("JNI options: '%s'\n", jniOptsBuf);
624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 /* route exit() to our handler */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700627 addOption("exit", (void*) runtime_exit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
629 /* route fprintf() to our handler */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700630 addOption("vfprintf", (void*) runtime_vfprintf);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800632 /* register the framework-specific "is sensitive thread" hook */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700633 addOption("sensitiveThread", (void*) runtime_isSensitiveThread);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
635 /* enable verbose; standard options are { jni, gc, class } */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700636 //addOption("-verbose:jni");
637 addOption("-verbose:gc");
638 //addOption("-verbose:class");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800640 /*
641 * The default starting and maximum size of the heap. Larger
642 * values should be specified in a product property override.
643 */
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700644 parseRuntimeOption("dalvik.vm.heapstartsize", heapstartsizeOptsBuf, "-Xms", "4m");
645 parseRuntimeOption("dalvik.vm.heapsize", heapsizeOptsBuf, "-Xmx", "16m");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700647 parseRuntimeOption("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
648 parseRuntimeOption("dalvik.vm.heapminfree", heapminfreeOptsBuf, "-XX:HeapMinFree=");
649 parseRuntimeOption("dalvik.vm.heapmaxfree", heapmaxfreeOptsBuf, "-XX:HeapMaxFree=");
650 parseRuntimeOption("dalvik.vm.heaptargetutilization",
651 heaptargetutilizationOptsBuf,
652 "-XX:HeapTargetUtilization=");
Ian Rogers53250102012-09-23 16:38:03 -0700653
Mathieu Chartier6909c0e2015-03-05 17:00:41 -0800654 /*
655 * JIT related options.
656 */
Mathieu Chartier1fb7aab2015-03-18 18:54:36 -0700657 parseRuntimeOption("dalvik.vm.usejit", usejitOptsBuf, "-Xusejit:");
658 parseRuntimeOption("dalvik.vm.jitcodecachesize", jitcodecachesizeOptsBuf, "-Xjitcodecachesize:");
659 parseRuntimeOption("dalvik.vm.jitthreshold", jitthresholdOptsBuf, "-Xjitthreshold:");
Mathieu Chartier6909c0e2015-03-05 17:00:41 -0800660
Mathieu Chartiere6c22412013-08-20 17:16:03 -0700661 property_get("ro.config.low_ram", propBuf, "");
662 if (strcmp(propBuf, "true") == 0) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700663 addOption("-XX:LowMemoryMode");
Mathieu Chartiere6c22412013-08-20 17:16:03 -0700664 }
665
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700666 parseRuntimeOption("dalvik.vm.gctype", gctypeOptsBuf, "-Xgc:");
667 parseRuntimeOption("dalvik.vm.backgroundgctype", backgroundgcOptsBuf, "-XX:BackgroundGC=");
Mathieu Chartierc01936a2014-05-14 15:44:49 -0700668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 /* use android ADB transport */
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700671 addOption("-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700673 parseRuntimeOption("dalvik.vm.lockprof.threshold",
674 lockProfThresholdBuf,
675 "-Xlockprofthreshold:");
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 if (executionMode == kEMIntPortable) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700678 addOption("-Xint:portable");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 } else if (executionMode == kEMIntFast) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700680 addOption("-Xint:fast");
Ben Cheng52b0e732009-06-19 13:31:12 -0700681 } else if (executionMode == kEMJitCompiler) {
Brian Carlstrom0d8fb012014-07-30 12:11:41 -0700682 addOption("-Xint:jit");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700684
Andreas Gampe08b2d582014-12-18 14:39:22 -0800685 // If we are booting without the real /data, don't spend time compiling.
686 property_get("vold.decrypt", voldDecryptBuf, "");
687 bool skip_compilation = ((strcmp(voldDecryptBuf, "trigger_restart_min_framework") == 0) ||
688 (strcmp(voldDecryptBuf, "1") == 0));
Brian Carlstrom3beff1e02014-02-28 23:27:22 -0800689
Andreas Gampe08b2d582014-12-18 14:39:22 -0800690 // Extra options for boot.art/boot.oat image generation.
691 parseCompilerRuntimeOption("dalvik.vm.image-dex2oat-Xms", dex2oatXmsImageFlagsBuf,
692 "-Xms", "-Ximage-compiler-option");
693 parseCompilerRuntimeOption("dalvik.vm.image-dex2oat-Xmx", dex2oatXmxImageFlagsBuf,
694 "-Xmx", "-Ximage-compiler-option");
695 if (skip_compilation) {
Brian Carlstromce5bbbe2014-07-30 15:13:17 -0700696 addOption("-Ximage-compiler-option");
Andreas Gampe08b2d582014-12-18 14:39:22 -0800697 addOption("--compiler-filter=verify-none");
698 } else {
699 parseCompilerOption("dalvik.vm.image-dex2oat-filter", dex2oatImageCompilerFilterBuf,
700 "--compiler-filter=", "-Ximage-compiler-option");
Brian Carlstrom3beff1e02014-02-28 23:27:22 -0800701 }
702
Andreas Gampe08b2d582014-12-18 14:39:22 -0800703 // Make sure there is a preloaded-classes file.
704 if (!hasFile("/system/etc/preloaded-classes")) {
705 ALOGE("Missing preloaded-classes file, /system/etc/preloaded-classes not found: %s\n",
706 strerror(errno));
Andreas Gampe605cca12015-04-02 23:12:56 -0700707 return -1;
Andreas Gampe08b2d582014-12-18 14:39:22 -0800708 }
709 addOption("-Ximage-compiler-option");
710 addOption("--image-classes=/system/etc/preloaded-classes");
711
712 // If there is a compiled-classes file, push it.
713 if (hasFile("/system/etc/compiled-classes")) {
714 addOption("-Ximage-compiler-option");
715 addOption("--compiled-classes=/system/etc/compiled-classes");
716 }
717
718 property_get("dalvik.vm.image-dex2oat-flags", dex2oatImageFlagsBuf, "");
719 parseExtraOpts(dex2oatImageFlagsBuf, "-Ximage-compiler-option");
720
721 // Extra options for DexClassLoader.
722 parseCompilerRuntimeOption("dalvik.vm.dex2oat-Xms", dex2oatXmsFlagsBuf,
723 "-Xms", "-Xcompiler-option");
724 parseCompilerRuntimeOption("dalvik.vm.dex2oat-Xmx", dex2oatXmxFlagsBuf,
725 "-Xmx", "-Xcompiler-option");
726 if (skip_compilation) {
727 addOption("-Xcompiler-option");
728 addOption("--compiler-filter=verify-none");
neo.chae155118f2015-01-07 15:32:15 +0900729
730 // We skip compilation when a minimal runtime is brought up for decryption. In that case
731 // /data is temporarily backed by a tmpfs, which is usually small.
732 // If the system image contains prebuilts, they will be relocated into the tmpfs. In this
733 // specific situation it is acceptable to *not* relocate and run out of the prebuilts
734 // directly instead.
735 addOption("--runtime-arg");
736 addOption("-Xnorelocate");
Andreas Gampe08b2d582014-12-18 14:39:22 -0800737 } else {
738 parseCompilerOption("dalvik.vm.dex2oat-filter", dex2oatCompilerFilterBuf,
739 "--compiler-filter=", "-Xcompiler-option");
740 }
Andreas Gampee0352382015-03-30 18:43:39 -0700741 parseCompilerOption("dalvik.vm.dex2oat-threads", dex2oatThreadsBuf, "-j", "-Xcompiler-option");
742 parseCompilerOption("dalvik.vm.image-dex2oat-threads", dex2oatThreadsImageBuf, "-j",
743 "-Ximage-compiler-option");
Andreas Gampe605cca12015-04-02 23:12:56 -0700744
745 // The runtime will compile a boot image, when necessary, not using installd. Thus, we need to
746 // pass the instruction-set-features/variant as an image-compiler-option.
747 // TODO: Find a better way for the instruction-set.
748#if defined(__arm__)
749 constexpr const char* instruction_set = "arm";
750#elif defined(__aarch64__)
751 constexpr const char* instruction_set = "arm64";
752#elif defined(__mips__) && !defined(__LP64__)
753 constexpr const char* instruction_set = "mips";
754#elif defined(__mips__) && defined(__LP64__)
755 constexpr const char* instruction_set = "mips64";
756#elif defined(__i386__)
757 constexpr const char* instruction_set = "x86";
758#elif defined(__x86_64__)
759 constexpr const char* instruction_set = "x86_64";
760#else
761 constexpr const char* instruction_set = "unknown";
762#endif
763 // Note: it is OK to reuse the buffer, as the values are exactly the same between
764 // * compiler-option, used for runtime compilation (DexClassLoader)
765 // * image-compiler-option, used for boot-image compilation on device
766
767 // Copy the variant.
768 sprintf(dex2oat_isa_variant_key, "dalvik.vm.isa.%s.variant", instruction_set);
769 parseCompilerOption(dex2oat_isa_variant_key, dex2oat_isa_variant,
770 "--instruction-set-variant=", "-Ximage-compiler-option");
771 parseCompilerOption(dex2oat_isa_variant_key, dex2oat_isa_variant,
772 "--instruction-set-variant=", "-Xcompiler-option");
773 // Copy the features.
774 sprintf(dex2oat_isa_features_key, "dalvik.vm.isa.%s.features", instruction_set);
775 parseCompilerOption(dex2oat_isa_features_key, dex2oat_isa_features,
776 "--instruction-set-features=", "-Ximage-compiler-option");
777 parseCompilerOption(dex2oat_isa_features_key, dex2oat_isa_features,
778 "--instruction-set-features=", "-Xcompiler-option");
779
780
Andreas Gampe08b2d582014-12-18 14:39:22 -0800781 property_get("dalvik.vm.dex2oat-flags", dex2oatFlagsBuf, "");
782 parseExtraOpts(dex2oatFlagsBuf, "-Xcompiler-option");
783
Andy McFaddene4d81f22010-07-14 16:02:20 -0700784 /* extra options; parse this late so it overrides others */
785 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
Brian Carlstrom3beff1e02014-02-28 23:27:22 -0800786 parseExtraOpts(extraOptsBuf, NULL);
Andy McFaddene4d81f22010-07-14 16:02:20 -0700787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 /* Set the properties for locale */
789 {
Narayan Kamathd30dbb82015-01-15 14:48:15 +0000790 strcpy(localeOption, "-Duser.locale=");
791 readLocale(localeOption);
792 addOption(localeOption);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
794
Dave Allison0efbd9a2014-01-30 14:19:51 -0800795 /*
796 * Set profiler options
797 */
Andreas Gampe08b2d582014-12-18 14:39:22 -0800798 // Whether or not the profiler should be enabled.
799 property_get("dalvik.vm.profiler", propBuf, "0");
800 if (propBuf[0] == '1') {
801 addOption("-Xenable-profiler");
802 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800803
Andreas Gampe08b2d582014-12-18 14:39:22 -0800804 // Whether the profile should start upon app startup or be delayed by some random offset
805 // (in seconds) that is bound between 0 and a fixed value.
806 property_get("dalvik.vm.profile.start-immed", propBuf, "0");
807 if (propBuf[0] == '1') {
808 addOption("-Xprofile-start-immediately");
809 }
Dave Allison0efbd9a2014-01-30 14:19:51 -0800810
Andreas Gampe08b2d582014-12-18 14:39:22 -0800811 // Number of seconds during profile runs.
812 parseRuntimeOption("dalvik.vm.profile.period-secs", profilePeriod, "-Xprofile-period:");
Dave Allison0efbd9a2014-01-30 14:19:51 -0800813
Andreas Gampe08b2d582014-12-18 14:39:22 -0800814 // Length of each profile run (seconds).
815 parseRuntimeOption("dalvik.vm.profile.duration-secs",
816 profileDuration,
817 "-Xprofile-duration:");
Dave Allison0efbd9a2014-01-30 14:19:51 -0800818
Andreas Gampe08b2d582014-12-18 14:39:22 -0800819 // Polling interval during profile run (microseconds).
820 parseRuntimeOption("dalvik.vm.profile.interval-us", profileInterval, "-Xprofile-interval:");
Calin Juravle9828d062014-06-02 16:45:13 +0100821
Andreas Gampe08b2d582014-12-18 14:39:22 -0800822 // Coefficient for period backoff. The the period is multiplied
823 // by this value after each profile run.
824 parseRuntimeOption("dalvik.vm.profile.backoff-coeff", profileBackoff, "-Xprofile-backoff:");
Calin Juravle9828d062014-06-02 16:45:13 +0100825
Andreas Gampe08b2d582014-12-18 14:39:22 -0800826 // Top K% of samples that are considered relevant when
827 // deciding if the app should be recompiled.
828 parseRuntimeOption("dalvik.vm.profile.top-k-thr",
829 profileTopKThreshold,
830 "-Xprofile-top-k-threshold:");
Calin Juravle9828d062014-06-02 16:45:13 +0100831
Andreas Gampe08b2d582014-12-18 14:39:22 -0800832 // The threshold after which a change in the structure of the
833 // top K% profiled samples becomes significant and triggers
834 // recompilation. A change in profile is considered
835 // significant if X% (top-k-change-threshold) of the top K%
836 // (top-k-threshold property) samples has changed.
837 parseRuntimeOption("dalvik.vm.profile.top-k-ch-thr",
838 profileTopKChangeThreshold,
839 "-Xprofile-top-k-change-threshold:");
Wei Jin54fc18b2014-06-09 16:29:54 -0700840
Andreas Gampe08b2d582014-12-18 14:39:22 -0800841 // Type of profile data.
842 parseRuntimeOption("dalvik.vm.profiler.type", profileType, "-Xprofile-type:");
Wei Jin54fc18b2014-06-09 16:29:54 -0700843
Andreas Gampe08b2d582014-12-18 14:39:22 -0800844 // Depth of bounded stack data
845 parseRuntimeOption("dalvik.vm.profile.stack-depth",
846 profileMaxStackDepth,
847 "-Xprofile-max-stack-depth:");
Dave Allison0efbd9a2014-01-30 14:19:51 -0800848
Andreas Gampe08b2d582014-12-18 14:39:22 -0800849 // Native bridge library. "0" means that native bridge is disabled.
850 property_get("ro.dalvik.vm.native.bridge", propBuf, "");
851 if (propBuf[0] == '\0') {
852 ALOGW("ro.dalvik.vm.native.bridge is not expected to be empty");
853 } else if (strcmp(propBuf, "0") != 0) {
854 snprintf(nativeBridgeLibrary, sizeof("-XX:NativeBridge=") + PROPERTY_VALUE_MAX,
855 "-XX:NativeBridge=%s", propBuf);
856 addOption(nativeBridgeLibrary);
Calin Juravlebe20ed42014-08-26 23:00:11 +0100857 }
Calin Juravle01db9162014-08-07 14:45:53 +0100858
Andreas Gampee324ba02015-03-31 16:15:17 -0700859 // Dalvik-cache pruning counter.
860 parseRuntimeOption("dalvik.vm.zygote.max-boot-retry", cachePruneBuf,
861 "-Xzygote-max-boot-retry=");
862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 initArgs.version = JNI_VERSION_1_4;
864 initArgs.options = mOptions.editArray();
865 initArgs.nOptions = mOptions.size();
866 initArgs.ignoreUnrecognized = JNI_FALSE;
867
868 /*
869 * Initialize the VM.
870 *
871 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
872 * If this call succeeds, the VM is ready, and we can start issuing
873 * JNI calls.
874 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700875 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000876 ALOGE("JNI_CreateJavaVM failed\n");
Andreas Gampe605cca12015-04-02 23:12:56 -0700877 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
Andreas Gampe605cca12015-04-02 23:12:56 -0700880 return 0;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700881}
882
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700883char* AndroidRuntime::toSlashClassName(const char* className)
884{
885 char* result = strdup(className);
886 for (char* cp = result; *cp != '\0'; cp++) {
887 if (*cp == '.') {
888 *cp = '/';
889 }
890 }
891 return result;
892}
893
Mike Lockwood9ee5e7e2014-09-08 16:15:21 -0700894/** Create a Java string from an ASCII or Latin-1 string */
895jstring AndroidRuntime::NewStringLatin1(JNIEnv* env, const char* bytes) {
896 if (!bytes) return NULL;
897 int length = strlen(bytes);
898 jchar* buffer = (jchar *)alloca(length * sizeof(jchar));
899 if (!buffer) return NULL;
900 jchar* chp = buffer;
901 for (int i = 0; i < length; i++) {
902 *chp++ = *bytes++;
903 }
904 return env->NewString(buffer, length);
905}
906
907
Andy McFaddenf70188a2009-03-31 15:52:13 -0700908/*
909 * Start the Android runtime. This involves starting the virtual machine
910 * and calling the "static void main(String[] args)" method in the class
911 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700912 *
913 * Passes the main function two arguments, the class name and the specified
914 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700915 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100916void AndroidRuntime::start(const char* className, const Vector<String8>& options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700917{
Dianne Hackborn8e5aafe2014-10-27 18:04:10 -0700918 ALOGD(">>>>>> START %s uid %d <<<<<<\n",
919 className != NULL ? className : "(unknown)", getuid());
Andy McFaddenf70188a2009-03-31 15:52:13 -0700920
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100921 static const String8 startSystemServer("start-system-server");
922
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700923 /*
924 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700925 * init.rc anymore, so we print out the boot start event here.
926 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100927 for (size_t i = 0; i < options.size(); ++i) {
928 if (options[i] == startSystemServer) {
929 /* track our progress through the boot sequence */
930 const int LOG_BOOT_PROGRESS_START = 3000;
931 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
932 }
Andy McFaddenf70188a2009-03-31 15:52:13 -0700933 }
934
935 const char* rootDir = getenv("ANDROID_ROOT");
936 if (rootDir == NULL) {
937 rootDir = "/system";
938 if (!hasDir("/system")) {
939 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700940 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700941 }
942 setenv("ANDROID_ROOT", rootDir, 1);
943 }
944
945 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
Steve Block5baa3a62011-12-20 16:23:08 +0000946 //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700947
948 /* start the virtual machine */
Brian Carlstrom9f8203a2013-06-19 13:49:36 -0700949 JniInvocation jni_invocation;
950 jni_invocation.Init(NULL);
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700951 JNIEnv* env;
952 if (startVm(&mJavaVM, &env) != 0) {
953 return;
954 }
955 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 /*
958 * Register android functions.
959 */
960 if (startReg(env) < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000961 ALOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700962 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964
965 /*
966 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700967 * At present we have two arguments, the class name and an option string.
968 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 */
970 jclass stringClass;
971 jobjectArray strArray;
972 jstring classNameStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973
974 stringClass = env->FindClass("java/lang/String");
975 assert(stringClass != NULL);
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100976 strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 assert(strArray != NULL);
978 classNameStr = env->NewStringUTF(className);
979 assert(classNameStr != NULL);
980 env->SetObjectArrayElement(strArray, 0, classNameStr);
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100981
982 for (size_t i = 0; i < options.size(); ++i) {
983 jstring optionsStr = env->NewStringUTF(options.itemAt(i).string());
984 assert(optionsStr != NULL);
985 env->SetObjectArrayElement(strArray, i + 1, optionsStr);
986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987
988 /*
989 * Start VM. This thread becomes the main thread of the VM, and will
990 * not return until the VM exits.
991 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700992 char* slashClassName = toSlashClassName(className);
993 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (startClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000995 ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 /* keep going */
997 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700998 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 "([Ljava/lang/String;)V");
1000 if (startMeth == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001001 ALOGE("JavaVM unable to find main() in '%s'\n", className);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 /* keep going */
1003 } else {
1004 env->CallStaticVoidMethod(startClass, startMeth, strArray);
1005
1006#if 0
1007 if (env->ExceptionCheck())
1008 threadExitUncaughtException(env);
1009#endif
1010 }
1011 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001012 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013
Steve Block5baa3a62011-12-20 16:23:08 +00001014 ALOGD("Shutting down VM\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 if (mJavaVM->DetachCurrentThread() != JNI_OK)
Steve Block8564c8d2012-01-05 23:22:43 +00001016 ALOGW("Warning: unable to detach main thread\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 if (mJavaVM->DestroyJavaVM() != 0)
Steve Block8564c8d2012-01-05 23:22:43 +00001018 ALOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019}
1020
Jeff Brown4280c4a2012-03-15 17:48:02 -07001021void AndroidRuntime::exit(int code)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022{
Jeff Brown4280c4a2012-03-15 17:48:02 -07001023 if (mExitWithoutCleanup) {
1024 ALOGI("VM exiting with result code %d, cleanup skipped.", code);
1025 ::_exit(code);
1026 } else {
1027 ALOGI("VM exiting with result code %d.", code);
1028 onExit(code);
1029 ::exit(code);
1030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031}
1032
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001033void AndroidRuntime::onVmCreated(JNIEnv* env)
1034{
1035 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
1036}
1037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038/*
Andreas Huber9ae000c2014-02-13 17:22:33 +00001039 * Get the JNIEnv pointer for this thread.
1040 *
1041 * Returns NULL if the slot wasn't allocated or populated.
1042 */
1043/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
1044{
1045 JNIEnv* env;
1046 JavaVM* vm = AndroidRuntime::getJavaVM();
1047 assert(vm != NULL);
1048
1049 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
1050 return NULL;
1051 return env;
1052}
1053
1054/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 * Makes the current thread visible to the VM.
1056 *
1057 * The JNIEnv pointer returned is only valid for the current thread, and
1058 * thus must be tucked into thread-local storage.
1059 */
1060static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
1061{
1062 JavaVMAttachArgs args;
1063 JavaVM* vm;
1064 jint result;
1065
1066 vm = AndroidRuntime::getJavaVM();
1067 assert(vm != NULL);
1068
1069 args.version = JNI_VERSION_1_4;
1070 args.name = (char*) threadName;
1071 args.group = NULL;
1072
1073 result = vm->AttachCurrentThread(pEnv, (void*) &args);
1074 if (result != JNI_OK)
Steve Block6215d3f2012-01-04 20:05:49 +00001075 ALOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076
1077 return result;
1078}
1079
1080/*
1081 * Detach the current thread from the set visible to the VM.
1082 */
1083static int javaDetachThread(void)
1084{
1085 JavaVM* vm;
1086 jint result;
1087
1088 vm = AndroidRuntime::getJavaVM();
1089 assert(vm != NULL);
1090
1091 result = vm->DetachCurrentThread();
1092 if (result != JNI_OK)
Steve Block3762c312012-01-06 19:20:56 +00001093 ALOGE("ERROR: thread detach failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 return result;
1095}
1096
1097/*
1098 * When starting a native thread that will be visible from the VM, we
1099 * bounce through this to get the right attach/detach action.
1100 * Note that this function calls free(args)
1101 */
1102/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
1103 void* start = ((void**)args)[0];
1104 void* userData = ((void **)args)[1];
1105 char* name = (char*) ((void **)args)[2]; // we own this storage
1106 free(args);
1107 JNIEnv* env;
1108 int result;
1109
1110 /* hook us into the VM */
1111 if (javaAttachThread(name, &env) != JNI_OK)
1112 return -1;
1113
1114 /* start the thread running */
1115 result = (*(android_thread_func_t)start)(userData);
1116
1117 /* unhook us */
1118 javaDetachThread();
1119 free(name);
1120
1121 return result;
1122}
1123
1124/*
1125 * This is invoked from androidCreateThreadEtc() via the callback
1126 * set with androidSetCreateThreadFunc().
1127 *
1128 * We need to create the new thread in such a way that it gets hooked
1129 * into the VM before it really starts executing.
1130 */
1131/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001132 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 void* userData,
1134 const char* threadName,
1135 int32_t threadPriority,
1136 size_t threadStackSize,
1137 android_thread_id_t* threadId)
1138{
1139 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1140 int result;
1141
Mike Lockwood32f2e622013-10-10 10:11:27 -07001142 if (!threadName)
1143 threadName = "unnamed thread";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144
1145 args[0] = (void*) entryFunction;
1146 args[1] = userData;
1147 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1148
1149 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1150 threadName, threadPriority, threadStackSize, threadId);
1151 return result;
1152}
1153
1154/*
1155 * Create a thread that is visible from the VM.
1156 *
1157 * This is called from elsewhere in the library.
1158 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001159/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 void (*start)(void *), void* arg)
1161{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001162 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001164 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1165 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166}
1167
1168#if 0
1169static void quickTest(void* arg)
1170{
1171 const char* str = (const char*) arg;
1172
1173 printf("In quickTest: %s\n", str);
1174}
1175#endif
1176
1177#ifdef NDEBUG
1178 #define REG_JNI(name) { name }
1179 struct RegJNIRec {
1180 int (*mProc)(JNIEnv*);
1181 };
1182#else
1183 #define REG_JNI(name) { name, #name }
1184 struct RegJNIRec {
1185 int (*mProc)(JNIEnv*);
1186 const char* mName;
1187 };
1188#endif
1189
1190typedef void (*RegJAMProc)();
1191
1192static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1193{
1194 for (size_t i = 0; i < count; i++) {
1195 if (array[i].mProc(env) < 0) {
1196#ifndef NDEBUG
Steve Block5baa3a62011-12-20 16:23:08 +00001197 ALOGD("----------!!! %s failed to load\n", array[i].mName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198#endif
1199 return -1;
1200 }
1201 }
1202 return 0;
1203}
1204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205static const RegJNIRec gRegJNI[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 REG_JNI(register_com_android_internal_os_RuntimeInit),
1207 REG_JNI(register_android_os_SystemClock),
1208 REG_JNI(register_android_util_EventLog),
1209 REG_JNI(register_android_util_Log),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 REG_JNI(register_android_content_AssetManager),
1211 REG_JNI(register_android_content_StringBlock),
1212 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001213 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 REG_JNI(register_android_text_AndroidCharacter),
Anish Athalye88b5b0b2014-06-24 14:39:43 -07001215 REG_JNI(register_android_text_StaticLayout),
Doug Feltdae8e942010-02-24 14:33:15 -08001216 REG_JNI(register_android_text_AndroidBidi),
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001217 REG_JNI(register_android_view_InputDevice),
1218 REG_JNI(register_android_view_KeyCharacterMap),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001220 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 REG_JNI(register_android_os_Binder),
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001222 REG_JNI(register_android_os_Parcel),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 REG_JNI(register_android_nio_utils),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 REG_JNI(register_android_graphics_Graphics),
Chris Craik54389792013-12-20 13:28:11 -08001225 REG_JNI(register_android_view_DisplayEventReceiver),
John Reckf666ad72014-03-14 16:24:57 -07001226 REG_JNI(register_android_view_RenderNode),
John Recke45b1fd2014-04-15 09:50:16 -07001227 REG_JNI(register_android_view_RenderNodeAnimator),
Romain Guy3b748a42013-04-17 18:54:38 -07001228 REG_JNI(register_android_view_GraphicBuffer),
Romain Guye4d01122010-06-16 18:44:05 -07001229 REG_JNI(register_android_view_GLES20Canvas),
John Reck04fc5832014-02-05 16:38:25 -08001230 REG_JNI(register_android_view_HardwareLayer),
John Reckcec24ae2013-11-05 13:27:50 -08001231 REG_JNI(register_android_view_ThreadedRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 REG_JNI(register_android_view_Surface),
Mathias Agopian3866f0d2013-02-11 22:08:48 -08001233 REG_JNI(register_android_view_SurfaceControl),
Jeff Brown64a55af2012-08-26 02:47:39 -07001234 REG_JNI(register_android_view_SurfaceSession),
Romain Guy8f0095c2011-05-02 17:24:22 -07001235 REG_JNI(register_android_view_TextureView),
John Reck315c3292014-05-09 19:21:04 -07001236 REG_JNI(register_com_android_internal_view_animation_NativeInterpolatorFactoryHelper),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1238 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Thomas Tafertshofer6b1e8382012-07-03 13:37:35 -07001239 REG_JNI(register_android_opengl_jni_EGL14),
Jesse Hall237c2b82013-05-06 11:36:57 -07001240 REG_JNI(register_android_opengl_jni_EGLExt),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001241 REG_JNI(register_android_opengl_jni_GLES10),
1242 REG_JNI(register_android_opengl_jni_GLES10Ext),
1243 REG_JNI(register_android_opengl_jni_GLES11),
1244 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001245 REG_JNI(register_android_opengl_jni_GLES20),
Jesse Halld877efe2013-04-29 15:59:35 -07001246 REG_JNI(register_android_opengl_jni_GLES30),
Jesse Hall7ab63ac2014-05-19 15:13:41 -07001247 REG_JNI(register_android_opengl_jni_GLES31),
1248 REG_JNI(register_android_opengl_jni_GLES31Ext),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249
1250 REG_JNI(register_android_graphics_Bitmap),
1251 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001252 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 REG_JNI(register_android_graphics_Camera),
Leon Scroggins IIId0d7eaf2013-09-06 16:46:57 -04001254 REG_JNI(register_android_graphics_CreateJavaOutputStreamAdaptor),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 REG_JNI(register_android_graphics_Canvas),
John Reck52244ff2014-05-01 21:27:37 -07001256 REG_JNI(register_android_graphics_CanvasProperty),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 REG_JNI(register_android_graphics_ColorFilter),
1258 REG_JNI(register_android_graphics_DrawFilter),
Raph Levien1a73f7322014-01-30 16:06:28 -08001259 REG_JNI(register_android_graphics_FontFamily),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 REG_JNI(register_android_graphics_Interpolator),
1261 REG_JNI(register_android_graphics_LayerRasterizer),
1262 REG_JNI(register_android_graphics_MaskFilter),
1263 REG_JNI(register_android_graphics_Matrix),
1264 REG_JNI(register_android_graphics_Movie),
1265 REG_JNI(register_android_graphics_NinePatch),
1266 REG_JNI(register_android_graphics_Paint),
1267 REG_JNI(register_android_graphics_Path),
1268 REG_JNI(register_android_graphics_PathMeasure),
1269 REG_JNI(register_android_graphics_PathEffect),
1270 REG_JNI(register_android_graphics_Picture),
1271 REG_JNI(register_android_graphics_PorterDuff),
1272 REG_JNI(register_android_graphics_Rasterizer),
1273 REG_JNI(register_android_graphics_Region),
1274 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001275 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 REG_JNI(register_android_graphics_Typeface),
1277 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001278 REG_JNI(register_android_graphics_YuvImage),
Svetoslav6811f4e2013-09-18 15:58:28 -07001279 REG_JNI(register_android_graphics_pdf_PdfDocument),
Svetoslav62ce3322014-09-04 21:17:17 -07001280 REG_JNI(register_android_graphics_pdf_PdfEditor),
Svetoslav29617692014-04-24 18:40:42 -07001281 REG_JNI(register_android_graphics_pdf_PdfRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282
1283 REG_JNI(register_android_database_CursorWindow),
Jeff Browne5360fb2011-10-31 17:48:13 -07001284 REG_JNI(register_android_database_SQLiteConnection),
1285 REG_JNI(register_android_database_SQLiteGlobal),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 REG_JNI(register_android_database_SQLiteDebug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 REG_JNI(register_android_os_FileObserver),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001289 REG_JNI(register_android_os_MessageQueue),
Stephen Smalleyc07fca32012-01-13 08:31:39 -05001290 REG_JNI(register_android_os_SELinux),
Jeff Brown481c1572012-03-09 14:41:15 -08001291 REG_JNI(register_android_os_Trace),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 REG_JNI(register_android_os_UEventObserver),
1293 REG_JNI(register_android_net_LocalSocketImpl),
1294 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001295 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 REG_JNI(register_android_os_MemoryFile),
Narayan Kamath973b4662014-03-31 13:41:26 +01001297 REG_JNI(register_com_android_internal_os_Zygote),
John Reck9fa40712014-05-09 15:26:59 -07001298 REG_JNI(register_com_android_internal_util_VirtualRefBasePtr),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 REG_JNI(register_android_hardware_Camera),
Eino-Ville Talvala2f1a2e42013-07-25 17:12:05 -07001300 REG_JNI(register_android_hardware_camera2_CameraMetadata),
Ruben Brunkfeb50af2014-05-09 19:58:49 -07001301 REG_JNI(register_android_hardware_camera2_legacy_LegacyCameraDevice),
Eino-Ville Talvalae1f57d62014-05-29 17:17:07 -07001302 REG_JNI(register_android_hardware_camera2_legacy_PerfMeasurement),
Ruben Brunkb6079002014-05-22 12:33:54 -07001303 REG_JNI(register_android_hardware_camera2_DngCreator),
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),
Eric Laurent60b62bc2014-04-18 17:50:49 -07001306 REG_JNI(register_android_hardware_SoundTrigger),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001307 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001308 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001309 REG_JNI(register_android_hardware_UsbRequest),
destradaaa4fa3b52014-07-09 10:46:39 -07001310 REG_JNI(register_android_hardware_location_ActivityRecognitionHardware),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 REG_JNI(register_android_media_AudioRecord),
1312 REG_JNI(register_android_media_AudioSystem),
1313 REG_JNI(register_android_media_AudioTrack),
1314 REG_JNI(register_android_media_JetPlayer),
Jeff Browncbad9762012-09-04 21:57:59 -07001315 REG_JNI(register_android_media_RemoteDisplay),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 REG_JNI(register_android_media_ToneGenerator),
1317
1318 REG_JNI(register_android_opengl_classes),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001319 REG_JNI(register_android_server_NetworkManagementSocketTagger),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001321 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001322 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001323 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001324 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001325 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001326 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001327 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001328 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001329 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brownc28867a2013-03-26 15:42:39 -07001330 REG_JNI(register_android_view_InputEventSender),
Michael Wrighta44dd262013-04-10 21:12:00 -07001331 REG_JNI(register_android_view_InputQueue),
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001332 REG_JNI(register_android_view_KeyEvent),
1333 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001334 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001335 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001336
1337 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001338 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001339
1340 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001341 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
Jeff Sharkey9a2c2a62013-01-14 16:48:51 -08001342 REG_JNI(register_com_android_internal_net_NetworkStatsFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343};
1344
1345/*
1346 * Register android native functions with the VM.
1347 */
1348/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1349{
1350 /*
1351 * This hook causes all future threads created in this process to be
1352 * attached to the JavaVM. (This needs to go away in favor of JNI
1353 * Attach calls.)
1354 */
1355 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1356
Steve Block71f2cf12011-10-20 11:56:00 +01001357 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358
1359 /*
1360 * Every "register" function calls one or more things that return
1361 * a local reference (e.g. FindClass). Because we haven't really
1362 * started the VM yet, they're all getting stored in the base frame
1363 * and never released. Use Push/Pop to manage the storage.
1364 */
1365 env->PushLocalFrame(200);
1366
1367 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1368 env->PopLocalFrame(NULL);
1369 return -1;
1370 }
1371 env->PopLocalFrame(NULL);
1372
1373 //createJavaThread("fubar", quickTest, (void*) "hello");
1374
1375 return 0;
1376}
1377
1378AndroidRuntime* AndroidRuntime::getRuntime()
1379{
1380 return gCurRuntime;
1381}
1382
1383/**
1384 * Used by WithFramework to register native functions.
1385 */
1386extern "C"
1387jint Java_com_android_internal_util_WithFramework_registerNatives(
1388 JNIEnv* env, jclass clazz) {
1389 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1390}
1391
1392/**
1393 * Used by LoadClass to register native functions.
1394 */
1395extern "C"
1396jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1397 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1398}
1399
1400} // namespace android