blob: 8db7b248a6be3e80ce6856e91188e9c5e2bac536 [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>
Mathias Agopian8ed6beb2009-06-05 01:26:23 -070027#include <utils/StringArray.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <utils/threads.h>
29#include <cutils/properties.h>
30
31#include <SkGraphics.h>
32#include <SkImageDecoder.h>
Mike Reedfc8db532009-04-27 11:43:30 -040033#include <SkImageRef_GlobalPool.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#include "jni.h"
36#include "JNIHelp.h"
37#include "android_util_Binder.h"
38
39#include <stdio.h>
40#include <signal.h>
41#include <sys/stat.h>
42#include <sys/types.h>
43#include <signal.h>
44#include <dirent.h>
45#include <assert.h>
46
47
48using namespace android;
49
50extern void register_BindTest();
51
52extern int register_android_os_Binder(JNIEnv* env);
53extern int register_android_os_Process(JNIEnv* env);
54extern int register_android_graphics_Bitmap(JNIEnv*);
55extern int register_android_graphics_BitmapFactory(JNIEnv*);
Wei-Ta Chen6b849e22010-09-07 17:32:18 +080056extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057extern int register_android_graphics_Camera(JNIEnv* env);
58extern int register_android_graphics_Graphics(JNIEnv* env);
59extern int register_android_graphics_Interpolator(JNIEnv* env);
60extern int register_android_graphics_LayerRasterizer(JNIEnv*);
61extern int register_android_graphics_MaskFilter(JNIEnv* env);
62extern int register_android_graphics_Movie(JNIEnv* env);
63extern int register_android_graphics_NinePatch(JNIEnv*);
64extern int register_android_graphics_PathEffect(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065extern int register_android_graphics_Shader(JNIEnv* env);
66extern int register_android_graphics_Typeface(JNIEnv* env);
Wei-Ta Chenbca2d612009-11-30 17:52:05 +080067extern int register_android_graphics_YuvImage(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69extern int register_com_google_android_gles_jni_EGLImpl(JNIEnv* env);
70extern int register_com_google_android_gles_jni_GLImpl(JNIEnv* env);
Jack Palevich1c4907e2009-04-13 16:22:25 -070071extern int register_android_opengl_jni_GLES10(JNIEnv* env);
72extern int register_android_opengl_jni_GLES10Ext(JNIEnv* env);
73extern int register_android_opengl_jni_GLES11(JNIEnv* env);
74extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env);
Jack Palevich560814f2009-11-19 16:34:55 +080075extern int register_android_opengl_jni_GLES20(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77extern int register_android_hardware_Camera(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078extern int register_android_hardware_SensorManager(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050079extern int register_android_hardware_UsbDevice(JNIEnv *env);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050080extern int register_android_hardware_UsbDeviceConnection(JNIEnv *env);
Mike Lockwoode7d511e2010-12-30 13:39:37 -050081extern int register_android_hardware_UsbRequest(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83extern int register_android_media_AudioRecord(JNIEnv *env);
84extern int register_android_media_AudioSystem(JNIEnv *env);
85extern int register_android_media_AudioTrack(JNIEnv *env);
86extern int register_android_media_JetPlayer(JNIEnv *env);
87extern int register_android_media_ToneGenerator(JNIEnv *env);
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089extern int register_android_util_FloatMath(JNIEnv* env);
90
91namespace android {
92
93/*
94 * JNI-based registration functions. Note these are properly contained in
95 * namespace android.
96 */
97extern int register_android_content_AssetManager(JNIEnv* env);
98extern int register_android_util_EventLog(JNIEnv* env);
99extern int register_android_util_Log(JNIEnv* env);
100extern int register_android_content_StringBlock(JNIEnv* env);
101extern int register_android_content_XmlBlock(JNIEnv* env);
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700102extern int register_android_emoji_EmojiFactory(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103extern int register_android_graphics_Canvas(JNIEnv* env);
104extern int register_android_graphics_ColorFilter(JNIEnv* env);
105extern int register_android_graphics_DrawFilter(JNIEnv* env);
106extern int register_android_graphics_Matrix(JNIEnv* env);
107extern int register_android_graphics_Paint(JNIEnv* env);
108extern int register_android_graphics_Path(JNIEnv* env);
109extern int register_android_graphics_PathMeasure(JNIEnv* env);
110extern int register_android_graphics_Picture(JNIEnv*);
111extern int register_android_graphics_PorterDuff(JNIEnv* env);
112extern int register_android_graphics_Rasterizer(JNIEnv* env);
Jeff Brownfbf09772011-01-16 14:06:57 -0800113extern int register_android_graphics_Region(JNIEnv* env);
Jamie Gennisaa0ce332011-01-06 17:04:26 -0800114extern int register_android_graphics_SurfaceTexture(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115extern int register_android_graphics_Xfermode(JNIEnv* env);
116extern int register_android_graphics_PixelFormat(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117extern int register_android_view_Display(JNIEnv* env);
Jeff Brown0a0a1242011-12-02 02:25:22 -0800118extern int register_android_view_DisplayEventReceiver(JNIEnv* env);
Romain Guye4d01122010-06-16 18:44:05 -0700119extern int register_android_view_GLES20Canvas(JNIEnv* env);
Romain Guya9582652011-11-10 14:20:10 -0800120extern int register_android_view_HardwareRenderer(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121extern int register_android_view_Surface(JNIEnv* env);
Romain Guy8f0095c2011-05-02 17:24:22 -0700122extern int register_android_view_TextureView(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123extern int register_android_database_CursorWindow(JNIEnv* env);
Vasu Nori5a03f362009-10-20 15:16:35 -0700124extern int register_android_database_SQLiteCompiledSql(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125extern int register_android_database_SQLiteDatabase(JNIEnv* env);
126extern int register_android_database_SQLiteDebug(JNIEnv* env);
127extern int register_android_database_SQLiteProgram(JNIEnv* env);
128extern int register_android_database_SQLiteQuery(JNIEnv* env);
129extern int register_android_database_SQLiteStatement(JNIEnv* env);
130extern int register_android_debug_JNITest(JNIEnv* env);
131extern int register_android_nio_utils(JNIEnv* env);
Nick Pellycd0e8392010-10-13 17:25:24 -0700132extern int register_android_nfc_NdefMessage(JNIEnv *env);
133extern int register_android_nfc_NdefRecord(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134extern int register_android_text_format_Time(JNIEnv* env);
135extern int register_android_os_Debug(JNIEnv* env);
Christopher Tatefa9e7c02010-05-06 12:07:10 -0700136extern int register_android_os_MessageQueue(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137extern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
138extern int register_android_os_Power(JNIEnv *env);
139extern int register_android_os_StatFs(JNIEnv *env);
140extern int register_android_os_SystemProperties(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141extern int register_android_os_SystemClock(JNIEnv* env);
142extern int register_android_os_FileObserver(JNIEnv *env);
143extern int register_android_os_FileUtils(JNIEnv *env);
144extern int register_android_os_UEventObserver(JNIEnv* env);
145extern int register_android_os_MemoryFile(JNIEnv* env);
146extern int register_android_net_LocalSocketImpl(JNIEnv* env);
147extern int register_android_net_NetworkUtils(JNIEnv* env);
Dan Egnor2b4abcd2010-04-07 17:30:50 -0700148extern int register_android_net_TrafficStats(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149extern int register_android_net_wifi_WifiManager(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150extern int register_android_text_AndroidCharacter(JNIEnv *env);
Doug Feltdae8e942010-02-24 14:33:15 -0800151extern int register_android_text_AndroidBidi(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152extern int register_android_text_KeyCharacterMap(JNIEnv *env);
153extern int register_android_opengl_classes(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154extern int register_android_bluetooth_HeadsetBase(JNIEnv* env);
155extern int register_android_bluetooth_BluetoothAudioGateway(JNIEnv* env);
Nick Pelly0b6955a2009-05-26 19:13:43 -0700156extern int register_android_bluetooth_BluetoothSocket(JNIEnv *env);
Nick Pellybd022f42009-08-14 18:33:38 -0700157extern int register_android_server_BluetoothService(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158extern int register_android_server_BluetoothEventLoop(JNIEnv *env);
159extern int register_android_server_BluetoothA2dpService(JNIEnv* env);
JP Abgrall98a4f7e2011-09-02 15:36:33 -0700160extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
Christopher Tateecaa7b42010-06-04 14:55:02 -0700161extern int register_android_server_Watchdog(JNIEnv* env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
163extern int register_com_android_internal_os_ZygoteInit(JNIEnv* env);
Joe Onorato1cf58742009-06-12 11:06:24 -0700164extern int register_android_backup_BackupDataInput(JNIEnv *env);
Joe Onoratod2110db2009-05-19 13:41:21 -0700165extern int register_android_backup_BackupDataOutput(JNIEnv *env);
Joe Onorato06290a42009-06-18 20:10:37 -0700166extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
Joe Onorato4ababd92009-06-25 18:29:18 -0400167extern int register_android_backup_BackupHelperDispatcher(JNIEnv *env);
Christopher Tate4a627c72011-04-01 14:43:32 -0700168extern int register_android_app_backup_FullBackup(JNIEnv *env);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700169extern int register_android_app_ActivityThread(JNIEnv *env);
Dianne Hackborn69969e42010-05-04 11:40:40 -0700170extern int register_android_app_NativeActivity(JNIEnv *env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700171extern int register_android_view_InputChannel(JNIEnv* env);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800172extern int register_android_view_InputEventReceiver(JNIEnv* env);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700173extern int register_android_view_KeyEvent(JNIEnv* env);
174extern int register_android_view_MotionEvent(JNIEnv* env);
Jeff Brown2352b972011-04-12 22:39:53 -0700175extern int register_android_view_PointerIcon(JNIEnv* env);
Jeff Brown2ed24622011-03-14 19:39:54 -0700176extern int register_android_view_VelocityTracker(JNIEnv* env);
Kenny Root02c87302010-07-01 08:10:18 -0700177extern int register_android_content_res_ObbScanner(JNIEnv* env);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700178extern int register_android_content_res_Configuration(JNIEnv* env);
Chet Haase6e0ecb42010-11-03 19:41:18 -0700179extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
Kenny Root66269ea2011-07-12 14:14:01 -0700180extern int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
182static AndroidRuntime* gCurRuntime = NULL;
183
184static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
185{
186 if (jniThrowException(env, exc, msg) != 0)
187 assert(false);
188}
189
190/*
191 * Code written in the Java Programming Language calls here from main().
192 */
193static void com_android_internal_os_RuntimeInit_finishInit(JNIEnv* env, jobject clazz)
194{
195 gCurRuntime->onStarted();
196}
197
198static void com_android_internal_os_RuntimeInit_zygoteInit(JNIEnv* env, jobject clazz)
199{
200 gCurRuntime->onZygoteInit();
201}
202
203static jint com_android_internal_os_RuntimeInit_isComputerOn(JNIEnv* env, jobject clazz)
204{
205 return 1;
206}
207
208static void com_android_internal_os_RuntimeInit_turnComputerOn(JNIEnv* env, jobject clazz)
209{
210}
211
212static jint com_android_internal_os_RuntimeInit_getQwertyKeyboard(JNIEnv* env, jobject clazz)
213{
214 char* value = getenv("qwerty");
215 if (value != NULL && strcmp(value, "true") == 0) {
216 return 1;
217 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 return 0;
220}
221
222/*
223 * JNI registration.
224 */
225static JNINativeMethod gMethods[] = {
226 { "finishInit", "()V",
227 (void*) com_android_internal_os_RuntimeInit_finishInit },
228 { "zygoteInitNative", "()V",
229 (void*) com_android_internal_os_RuntimeInit_zygoteInit },
230 { "isComputerOn", "()I",
231 (void*) com_android_internal_os_RuntimeInit_isComputerOn },
232 { "turnComputerOn", "()V",
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700233 (void*) com_android_internal_os_RuntimeInit_turnComputerOn },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 { "getQwertyKeyboard", "()I",
235 (void*) com_android_internal_os_RuntimeInit_getQwertyKeyboard },
236};
237
238int register_com_android_internal_os_RuntimeInit(JNIEnv* env)
239{
240 return jniRegisterNativeMethods(env, "com/android/internal/os/RuntimeInit",
241 gMethods, NELEM(gMethods));
242}
243
244// ----------------------------------------------------------------------
245
246/*static*/ JavaVM* AndroidRuntime::mJavaVM = NULL;
247
248
249AndroidRuntime::AndroidRuntime()
250{
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700251 SkGraphics::Init();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 // this sets our preference for 16bit images during decode
253 // in case the src is opaque and 24bit
254 SkImageDecoder::SetDeviceConfig(SkBitmap::kRGB_565_Config);
Mike Reedfc8db532009-04-27 11:43:30 -0400255 // This cache is shared between browser native images, and java "purgeable"
256 // bitmaps. This globalpool is for images that do not either use the java
257 // heap, or are not backed by ashmem. See BitmapFactory.cpp for the key
258 // java call site.
259 SkImageRef_GlobalPool::SetRAMBudget(512 * 1024);
260 // There is also a global font cache, but its budget is specified in code
261 // see SkFontHost_android.cpp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 // Pre-allocate enough space to hold a fair number of options.
264 mOptions.setCapacity(20);
265
266 assert(gCurRuntime == NULL); // one per process
267 gCurRuntime = this;
268}
269
270AndroidRuntime::~AndroidRuntime()
271{
272 SkGraphics::Term();
273}
274
275/*
276 * Register native methods using JNI.
277 */
278/*static*/ int AndroidRuntime::registerNativeMethods(JNIEnv* env,
279 const char* className, const JNINativeMethod* gMethods, int numMethods)
280{
281 return jniRegisterNativeMethods(env, className, gMethods, numMethods);
282}
283
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700284status_t AndroidRuntime::callMain(const char* className,
285 jclass clazz, int argc, const char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286{
287 JNIEnv* env;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 jmethodID methodId;
289
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700290 LOGD("Calling main entry %s", className);
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 env = getJNIEnv();
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700293 if (clazz == NULL || env == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 return UNKNOWN_ERROR;
295 }
296
297 methodId = env->GetStaticMethodID(clazz, "main", "([Ljava/lang/String;)V");
298 if (methodId == NULL) {
299 LOGE("ERROR: could not find method %s.main(String[])\n", className);
300 return UNKNOWN_ERROR;
301 }
302
303 /*
304 * We want to call main() with a String array with our arguments in it.
305 * Create an array and populate it.
306 */
307 jclass stringClass;
308 jobjectArray strArray;
309
310 stringClass = env->FindClass("java/lang/String");
311 strArray = env->NewObjectArray(argc, stringClass, NULL);
312
313 for (int i = 0; i < argc; i++) {
314 jstring argStr = env->NewStringUTF(argv[i]);
315 env->SetObjectArrayElement(strArray, i, argStr);
316 }
317
318 env->CallStaticVoidMethod(clazz, methodId, strArray);
319 return NO_ERROR;
320}
321
322/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 * The VM calls this through the "exit" hook.
324 */
325static void runtime_exit(int code)
326{
327 gCurRuntime->onExit(code);
328 exit(code);
329}
330
331/*
332 * The VM calls this through the "vfprintf" hook.
333 *
334 * We ignore "fp" and just write the results to the log file.
335 */
336static void runtime_vfprintf(FILE* fp, const char* format, va_list ap)
337{
338 LOG_PRI_VA(ANDROID_LOG_INFO, "vm-printf", format, ap);
339}
340
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800341/**
342 * The VM calls this when mutex contention debugging is enabled to
343 * determine whether or not the blocked thread was a "sensitive thread"
344 * for user responsiveness/smoothess.
345 *
346 * Our policy for this is whether or not we're tracing any StrictMode
347 * events on this thread (which we might've inherited via Binder calls
348 * into us)
349 */
350static bool runtime_isSensitiveThread() {
351 IPCThreadState* state = IPCThreadState::selfOrNull();
352 return state && state->getStrictModePolicy() != 0;
353}
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355
356/**
357 * Add VM arguments to the to-be-executed VM
358 * Stops at first non '-' argument (also stops at an argument of '--')
359 * Returns the number of args consumed
360 */
361int AndroidRuntime::addVmArguments(int argc, const char* const argv[])
362{
363 int i;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 for (i = 0; i<argc; i++) {
366 if (argv[i][0] != '-') {
367 return i;
368 }
369 if (argv[i][1] == '-' && argv[i][2] == 0) {
370 return i+1;
371 }
372
373 JavaVMOption opt;
374 memset(&opt, 0, sizeof(opt));
375 opt.optionString = (char*)argv[i];
376 mOptions.add(opt);
377 }
378 return i;
379}
380
381static int hasDir(const char* dir)
382{
383 struct stat s;
384 int res = stat(dir, &s);
385 if (res == 0) {
386 return S_ISDIR(s.st_mode);
387 }
388 return 0;
389}
390
391/*
392 * We just want failed write() calls to just return with an error.
393 */
394static void blockSigpipe()
395{
396 sigset_t mask;
397
398 sigemptyset(&mask);
399 sigaddset(&mask, SIGPIPE);
400 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0)
401 LOGW("WARNING: SIGPIPE not blocked\n");
402}
403
404/*
405 * Read the persistent locale.
406 */
407static void readLocale(char* language, char* region)
408{
409 char propLang[PROPERTY_VALUE_MAX], propRegn[PROPERTY_VALUE_MAX];
Brian Carlstrom171ea892010-08-24 21:27:52 -0700410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 property_get("persist.sys.language", propLang, "");
412 property_get("persist.sys.country", propRegn, "");
413 if (*propLang == 0 && *propRegn == 0) {
414 /* Set to ro properties, default is en_US */
415 property_get("ro.product.locale.language", propLang, "en");
416 property_get("ro.product.locale.region", propRegn, "US");
417 }
418 strncat(language, propLang, 2);
419 strncat(region, propRegn, 2);
420 //LOGD("language=%s region=%s\n", language, region);
421}
422
Andy McFaddenf70188a2009-03-31 15:52:13 -0700423/*
Andy McFaddene4d81f22010-07-14 16:02:20 -0700424 * Parse a property containing space-separated options that should be
425 * passed directly to the VM, e.g. "-Xmx32m -verbose:gc -Xregenmap".
426 *
427 * This will cut up "extraOptsBuf" as we chop it into individual options.
428 *
429 * Adds the strings, if any, to mOptions.
430 */
431void AndroidRuntime::parseExtraOpts(char* extraOptsBuf)
432{
433 JavaVMOption opt;
434 char* start;
435 char* end;
436
437 memset(&opt, 0, sizeof(opt));
438 start = extraOptsBuf;
439 while (*start != '\0') {
440 while (*start == ' ') /* skip leading whitespace */
441 start++;
442 if (*start == '\0') /* was trailing ws, bail */
443 break;
444
445 end = start+1;
446 while (*end != ' ' && *end != '\0') /* find end of token */
447 end++;
448 if (*end == ' ')
449 *end++ = '\0'; /* mark end, advance to indicate more */
450
451 opt.optionString = start;
452 mOptions.add(opt);
453 start = end;
454 }
455}
456
457/*
Andy McFaddenf70188a2009-03-31 15:52:13 -0700458 * Start the Dalvik Virtual Machine.
459 *
460 * Various arguments, most determined by system properties, are passed in.
461 * The "mOptions" vector is updated.
462 *
463 * Returns 0 on success.
464 */
465int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466{
Andy McFaddenf70188a2009-03-31 15:52:13 -0700467 int result = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 JavaVMInitArgs initArgs;
469 JavaVMOption opt;
470 char propBuf[PROPERTY_VALUE_MAX];
471 char stackTraceFileBuf[PROPERTY_VALUE_MAX];
472 char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
473 char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
474 char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800475 char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700476 char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800477 char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
Andy McFaddene4d81f22010-07-14 16:02:20 -0700478 char extraOptsBuf[PROPERTY_VALUE_MAX];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 char* stackTraceFile = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 bool checkJni = false;
Andy McFaddene2b23e12009-04-03 11:09:46 -0700481 bool checkDexSum = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 bool logStdio = false;
Ben Cheng52b0e732009-06-19 13:31:12 -0700483 enum {
484 kEMDefault,
485 kEMIntPortable,
486 kEMIntFast,
487#if defined(WITH_JIT)
488 kEMJitCompiler,
489#endif
490 } executionMode = kEMDefault;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
493 property_get("dalvik.vm.checkjni", propBuf, "");
494 if (strcmp(propBuf, "true") == 0) {
495 checkJni = true;
496 } else if (strcmp(propBuf, "false") != 0) {
497 /* property is neither true nor false; fall back on kernel parameter */
498 property_get("ro.kernel.android.checkjni", propBuf, "");
499 if (propBuf[0] == '1') {
500 checkJni = true;
501 }
502 }
503
504 property_get("dalvik.vm.execution-mode", propBuf, "");
505 if (strcmp(propBuf, "int:portable") == 0) {
506 executionMode = kEMIntPortable;
507 } else if (strcmp(propBuf, "int:fast") == 0) {
508 executionMode = kEMIntFast;
Ben Cheng52b0e732009-06-19 13:31:12 -0700509#if defined(WITH_JIT)
510 } else if (strcmp(propBuf, "int:jit") == 0) {
511 executionMode = kEMJitCompiler;
512#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
515 property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
516
Andy McFaddene2b23e12009-04-03 11:09:46 -0700517 property_get("dalvik.vm.check-dex-sum", propBuf, "");
518 if (strcmp(propBuf, "true") == 0) {
519 checkDexSum = true;
520 }
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 property_get("log.redirect-stdio", propBuf, "");
523 if (strcmp(propBuf, "true") == 0) {
524 logStdio = true;
525 }
526
527 strcpy(enableAssertBuf, "-ea:");
528 property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
529
530 strcpy(jniOptsBuf, "-Xjniopts:");
531 property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 /* route exit() to our handler */
534 opt.extraInfo = (void*) runtime_exit;
535 opt.optionString = "exit";
536 mOptions.add(opt);
537
538 /* route fprintf() to our handler */
539 opt.extraInfo = (void*) runtime_vfprintf;
540 opt.optionString = "vfprintf";
541 mOptions.add(opt);
542
Brad Fitzpatrick0bd52432010-12-13 16:52:35 -0800543 /* register the framework-specific "is sensitive thread" hook */
544 opt.extraInfo = (void*) runtime_isSensitiveThread;
545 opt.optionString = "sensitiveThread";
546 mOptions.add(opt);
547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 opt.extraInfo = NULL;
549
550 /* enable verbose; standard options are { jni, gc, class } */
551 //options[curOpt++].optionString = "-verbose:jni";
552 opt.optionString = "-verbose:gc";
553 mOptions.add(opt);
554 //options[curOpt++].optionString = "-verbose:class";
555
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800556 /*
557 * The default starting and maximum size of the heap. Larger
558 * values should be specified in a product property override.
559 */
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800560 strcpy(heapstartsizeOptsBuf, "-Xms");
Carl Shapiro9e4c8842010-12-08 12:14:46 -0800561 property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
Carl Shapiro38cfa8c2010-12-07 16:48:29 -0800562 opt.optionString = heapstartsizeOptsBuf;
563 mOptions.add(opt);
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700564 strcpy(heapsizeOptsBuf, "-Xmx");
565 property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
Dianne Hackbornbdcef702009-08-18 19:15:36 -0700566 opt.optionString = heapsizeOptsBuf;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 mOptions.add(opt);
568
Carl Shapiro8cdf27c2011-01-19 12:33:12 -0800569 strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
570 property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
571 if (heapgrowthlimitOptsBuf[20] != '\0') {
572 opt.optionString = heapgrowthlimitOptsBuf;
573 mOptions.add(opt);
574 }
575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 /*
577 * Enable or disable dexopt features, such as bytecode verification and
578 * calculation of register maps for precise GC.
579 */
580 property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
581 if (dexoptFlagsBuf[0] != '\0') {
582 const char* opc;
583 const char* val;
584
585 opc = strstr(dexoptFlagsBuf, "v="); /* verification */
586 if (opc != NULL) {
587 switch (*(opc+2)) {
588 case 'n': val = "-Xverify:none"; break;
589 case 'r': val = "-Xverify:remote"; break;
590 case 'a': val = "-Xverify:all"; break;
591 default: val = NULL; break;
592 }
593
594 if (val != NULL) {
595 opt.optionString = val;
596 mOptions.add(opt);
597 }
598 }
599
600 opc = strstr(dexoptFlagsBuf, "o="); /* optimization */
601 if (opc != NULL) {
602 switch (*(opc+2)) {
603 case 'n': val = "-Xdexopt:none"; break;
604 case 'v': val = "-Xdexopt:verified"; break;
605 case 'a': val = "-Xdexopt:all"; break;
Brian Carlstrom08065b92011-04-01 15:49:41 -0700606 case 'f': val = "-Xdexopt:full"; break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 default: val = NULL; break;
608 }
609
610 if (val != NULL) {
611 opt.optionString = val;
612 mOptions.add(opt);
613 }
614 }
615
616 opc = strstr(dexoptFlagsBuf, "m=y"); /* register map */
617 if (opc != NULL) {
618 opt.optionString = "-Xgenregmap";
619 mOptions.add(opt);
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700620
621 /* turn on precise GC while we're at it */
622 opt.optionString = "-Xgc:precise";
623 mOptions.add(opt);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 }
625 }
626
627 /* enable debugging; set suspend=y to pause during VM init */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 /* use android ADB transport */
629 opt.optionString =
630 "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 mOptions.add(opt);
632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 LOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
634 if (checkJni) {
635 /* extended JNI checking */
636 opt.optionString = "-Xcheck:jni";
637 mOptions.add(opt);
638
639 /* set a cap on JNI global references */
640 opt.optionString = "-Xjnigreflimit:2000";
641 mOptions.add(opt);
642
643 /* with -Xcheck:jni, this provides a JNI function call trace */
644 //opt.optionString = "-verbose:jni";
645 //mOptions.add(opt);
646 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700647
Carl Shapirod8f3ec62010-04-12 16:31:59 -0700648 char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
649 property_get("dalvik.vm.lockprof.threshold", propBuf, "");
650 if (strlen(propBuf) > 0) {
651 strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
652 strcat(lockProfThresholdBuf, propBuf);
653 opt.optionString = lockProfThresholdBuf;
654 mOptions.add(opt);
655 }
656
Ben Cheng52b0e732009-06-19 13:31:12 -0700657#if defined(WITH_JIT)
Ben Cheng52b0e732009-06-19 13:31:12 -0700658 /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
659 char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
660 property_get("dalvik.vm.jit.op", propBuf, "");
661 if (strlen(propBuf) > 0) {
662 strcpy(jitOpBuf, "-Xjitop:");
663 strcat(jitOpBuf, propBuf);
664 opt.optionString = jitOpBuf;
665 mOptions.add(opt);
666 }
667
Ben Cheng52b0e732009-06-19 13:31:12 -0700668 /* Force interpreter-only mode for selected methods */
669 char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
670 property_get("dalvik.vm.jit.method", propBuf, "");
671 if (strlen(propBuf) > 0) {
672 strcpy(jitMethodBuf, "-Xjitmethod:");
673 strcat(jitMethodBuf, propBuf);
674 opt.optionString = jitMethodBuf;
675 mOptions.add(opt);
676 }
Ben Cheng52b0e732009-06-19 13:31:12 -0700677#endif
678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 if (executionMode == kEMIntPortable) {
680 opt.optionString = "-Xint:portable";
681 mOptions.add(opt);
682 } else if (executionMode == kEMIntFast) {
683 opt.optionString = "-Xint:fast";
684 mOptions.add(opt);
Ben Cheng52b0e732009-06-19 13:31:12 -0700685#if defined(WITH_JIT)
686 } else if (executionMode == kEMJitCompiler) {
687 opt.optionString = "-Xint:jit";
688 mOptions.add(opt);
689#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
Andy McFaddene2b23e12009-04-03 11:09:46 -0700691
692 if (checkDexSum) {
693 /* perform additional DEX checksum tests */
694 opt.optionString = "-Xcheckdexsum";
695 mOptions.add(opt);
696 }
697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 if (logStdio) {
699 /* convert stdout/stderr to log messages */
700 opt.optionString = "-Xlog-stdio";
701 mOptions.add(opt);
702 }
703
704 if (enableAssertBuf[4] != '\0') {
705 /* accept "all" to mean "all classes and packages" */
706 if (strcmp(enableAssertBuf+4, "all") == 0)
707 enableAssertBuf[3] = '\0';
708 LOGI("Assertions enabled: '%s'\n", enableAssertBuf);
709 opt.optionString = enableAssertBuf;
710 mOptions.add(opt);
711 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100712 ALOGV("Assertions disabled\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
715 if (jniOptsBuf[10] != '\0') {
716 LOGI("JNI options: '%s'\n", jniOptsBuf);
717 opt.optionString = jniOptsBuf;
718 mOptions.add(opt);
719 }
720
721 if (stackTraceFileBuf[0] != '\0') {
722 static const char* stfOptName = "-Xstacktracefile:";
723
724 stackTraceFile = (char*) malloc(strlen(stfOptName) +
725 strlen(stackTraceFileBuf) +1);
726 strcpy(stackTraceFile, stfOptName);
727 strcat(stackTraceFile, stackTraceFileBuf);
728 opt.optionString = stackTraceFile;
729 mOptions.add(opt);
730 }
Andy McFaddene4d81f22010-07-14 16:02:20 -0700731
732 /* extra options; parse this late so it overrides others */
733 property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
734 parseExtraOpts(extraOptsBuf);
735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 /* Set the properties for locale */
737 {
738 char langOption[sizeof("-Duser.language=") + 3];
739 char regionOption[sizeof("-Duser.region=") + 3];
740 strcpy(langOption, "-Duser.language=");
741 strcpy(regionOption, "-Duser.region=");
742 readLocale(langOption, regionOption);
743 opt.extraInfo = NULL;
744 opt.optionString = langOption;
745 mOptions.add(opt);
746 opt.optionString = regionOption;
747 mOptions.add(opt);
748 }
749
750 /*
751 * We don't have /tmp on the device, but we often have an SD card. Apps
752 * shouldn't use this, but some test suites might want to exercise it.
753 */
754 opt.optionString = "-Djava.io.tmpdir=/sdcard";
755 mOptions.add(opt);
756
757 initArgs.version = JNI_VERSION_1_4;
758 initArgs.options = mOptions.editArray();
759 initArgs.nOptions = mOptions.size();
760 initArgs.ignoreUnrecognized = JNI_FALSE;
761
762 /*
763 * Initialize the VM.
764 *
765 * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
766 * If this call succeeds, the VM is ready, and we can start issuing
767 * JNI calls.
768 */
Andy McFaddenf70188a2009-03-31 15:52:13 -0700769 if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 LOGE("JNI_CreateJavaVM failed\n");
771 goto bail;
772 }
773
Andy McFaddenf70188a2009-03-31 15:52:13 -0700774 result = 0;
775
776bail:
777 free(stackTraceFile);
778 return result;
779}
780
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700781char* AndroidRuntime::toSlashClassName(const char* className)
782{
783 char* result = strdup(className);
784 for (char* cp = result; *cp != '\0'; cp++) {
785 if (*cp == '.') {
786 *cp = '/';
787 }
788 }
789 return result;
790}
791
Andy McFaddenf70188a2009-03-31 15:52:13 -0700792/*
793 * Start the Android runtime. This involves starting the virtual machine
794 * and calling the "static void main(String[] args)" method in the class
795 * named by "className".
Jeff Brownebed7d62011-05-16 17:08:42 -0700796 *
797 * Passes the main function two arguments, the class name and the specified
798 * options string.
Andy McFaddenf70188a2009-03-31 15:52:13 -0700799 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700800void AndroidRuntime::start(const char* className, const char* options)
Andy McFaddenf70188a2009-03-31 15:52:13 -0700801{
Dianne Hackborn08e60f22010-09-01 18:17:17 -0700802 LOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
803 className != NULL ? className : "(unknown)");
Andy McFaddenf70188a2009-03-31 15:52:13 -0700804
Andy McFaddenf70188a2009-03-31 15:52:13 -0700805 blockSigpipe();
806
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700807 /*
808 * 'startSystemServer == true' means runtime is obsolete and not run from
Andy McFaddenf70188a2009-03-31 15:52:13 -0700809 * init.rc anymore, so we print out the boot start event here.
810 */
Jeff Brownebed7d62011-05-16 17:08:42 -0700811 if (strcmp(options, "start-system-server") == 0) {
Andy McFaddenf70188a2009-03-31 15:52:13 -0700812 /* track our progress through the boot sequence */
813 const int LOG_BOOT_PROGRESS_START = 3000;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700814 LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
Andy McFaddenf70188a2009-03-31 15:52:13 -0700815 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
816 }
817
818 const char* rootDir = getenv("ANDROID_ROOT");
819 if (rootDir == NULL) {
820 rootDir = "/system";
821 if (!hasDir("/system")) {
822 LOG_FATAL("No root directory specified, and /android does not exist.");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700823 return;
Andy McFaddenf70188a2009-03-31 15:52:13 -0700824 }
825 setenv("ANDROID_ROOT", rootDir, 1);
826 }
827
828 //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
829 //LOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
830
831 /* start the virtual machine */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700832 JNIEnv* env;
833 if (startVm(&mJavaVM, &env) != 0) {
834 return;
835 }
836 onVmCreated(env);
Andy McFaddenf70188a2009-03-31 15:52:13 -0700837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 /*
839 * Register android functions.
840 */
841 if (startReg(env) < 0) {
842 LOGE("Unable to register all android natives\n");
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700843 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
845
846 /*
847 * We want to call main() with a String array with arguments in it.
Jeff Brownebed7d62011-05-16 17:08:42 -0700848 * At present we have two arguments, the class name and an option string.
849 * Create an array to hold them.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 */
851 jclass stringClass;
852 jobjectArray strArray;
853 jstring classNameStr;
Jeff Brownebed7d62011-05-16 17:08:42 -0700854 jstring optionsStr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855
856 stringClass = env->FindClass("java/lang/String");
857 assert(stringClass != NULL);
858 strArray = env->NewObjectArray(2, stringClass, NULL);
859 assert(strArray != NULL);
860 classNameStr = env->NewStringUTF(className);
861 assert(classNameStr != NULL);
862 env->SetObjectArrayElement(strArray, 0, classNameStr);
Jeff Brownebed7d62011-05-16 17:08:42 -0700863 optionsStr = env->NewStringUTF(options);
864 env->SetObjectArrayElement(strArray, 1, optionsStr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865
866 /*
867 * Start VM. This thread becomes the main thread of the VM, and will
868 * not return until the VM exits.
869 */
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700870 char* slashClassName = toSlashClassName(className);
871 jclass startClass = env->FindClass(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 if (startClass == NULL) {
873 LOGE("JavaVM unable to locate class '%s'\n", slashClassName);
874 /* keep going */
875 } else {
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700876 jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 "([Ljava/lang/String;)V");
878 if (startMeth == NULL) {
879 LOGE("JavaVM unable to find main() in '%s'\n", className);
880 /* keep going */
881 } else {
882 env->CallStaticVoidMethod(startClass, startMeth, strArray);
883
884#if 0
885 if (env->ExceptionCheck())
886 threadExitUncaughtException(env);
887#endif
888 }
889 }
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700890 free(slashClassName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891
892 LOGD("Shutting down VM\n");
893 if (mJavaVM->DetachCurrentThread() != JNI_OK)
894 LOGW("Warning: unable to detach main thread\n");
895 if (mJavaVM->DestroyJavaVM() != 0)
896 LOGW("Warning: VM did not shut down cleanly\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897}
898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899void AndroidRuntime::onExit(int code)
900{
Steve Block71f2cf12011-10-20 11:56:00 +0100901 ALOGV("AndroidRuntime onExit calling exit(%d)", code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 exit(code);
903}
904
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700905void AndroidRuntime::onVmCreated(JNIEnv* env)
906{
907 // If AndroidRuntime had anything to do here, we'd have done it in 'start'.
908}
909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910/*
911 * Get the JNIEnv pointer for this thread.
912 *
913 * Returns NULL if the slot wasn't allocated or populated.
914 */
915/*static*/ JNIEnv* AndroidRuntime::getJNIEnv()
916{
917 JNIEnv* env;
918 JavaVM* vm = AndroidRuntime::getJavaVM();
919 assert(vm != NULL);
920
921 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
922 return NULL;
923 return env;
924}
925
926/*
927 * Makes the current thread visible to the VM.
928 *
929 * The JNIEnv pointer returned is only valid for the current thread, and
930 * thus must be tucked into thread-local storage.
931 */
932static int javaAttachThread(const char* threadName, JNIEnv** pEnv)
933{
934 JavaVMAttachArgs args;
935 JavaVM* vm;
936 jint result;
937
938 vm = AndroidRuntime::getJavaVM();
939 assert(vm != NULL);
940
941 args.version = JNI_VERSION_1_4;
942 args.name = (char*) threadName;
943 args.group = NULL;
944
945 result = vm->AttachCurrentThread(pEnv, (void*) &args);
946 if (result != JNI_OK)
Andy McFadden701d9162010-03-01 14:03:13 -0800947 LOGI("NOTE: attach of thread '%s' failed\n", threadName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948
949 return result;
950}
951
952/*
953 * Detach the current thread from the set visible to the VM.
954 */
955static int javaDetachThread(void)
956{
957 JavaVM* vm;
958 jint result;
959
960 vm = AndroidRuntime::getJavaVM();
961 assert(vm != NULL);
962
963 result = vm->DetachCurrentThread();
964 if (result != JNI_OK)
965 LOGE("ERROR: thread detach failed\n");
966 return result;
967}
968
969/*
970 * When starting a native thread that will be visible from the VM, we
971 * bounce through this to get the right attach/detach action.
972 * Note that this function calls free(args)
973 */
974/*static*/ int AndroidRuntime::javaThreadShell(void* args) {
975 void* start = ((void**)args)[0];
976 void* userData = ((void **)args)[1];
977 char* name = (char*) ((void **)args)[2]; // we own this storage
978 free(args);
979 JNIEnv* env;
980 int result;
981
982 /* hook us into the VM */
983 if (javaAttachThread(name, &env) != JNI_OK)
984 return -1;
985
986 /* start the thread running */
987 result = (*(android_thread_func_t)start)(userData);
988
989 /* unhook us */
990 javaDetachThread();
991 free(name);
992
993 return result;
994}
995
996/*
997 * This is invoked from androidCreateThreadEtc() via the callback
998 * set with androidSetCreateThreadFunc().
999 *
1000 * We need to create the new thread in such a way that it gets hooked
1001 * into the VM before it really starts executing.
1002 */
1003/*static*/ int AndroidRuntime::javaCreateThreadEtc(
Elliott Hughesd195e5a2011-04-13 15:39:37 -07001004 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 void* userData,
1006 const char* threadName,
1007 int32_t threadPriority,
1008 size_t threadStackSize,
1009 android_thread_id_t* threadId)
1010{
1011 void** args = (void**) malloc(3 * sizeof(void*)); // javaThreadShell must free
1012 int result;
1013
1014 assert(threadName != NULL);
1015
1016 args[0] = (void*) entryFunction;
1017 args[1] = userData;
1018 args[2] = (void*) strdup(threadName); // javaThreadShell must free
1019
1020 result = androidCreateRawThreadEtc(AndroidRuntime::javaThreadShell, args,
1021 threadName, threadPriority, threadStackSize, threadId);
1022 return result;
1023}
1024
1025/*
1026 * Create a thread that is visible from the VM.
1027 *
1028 * This is called from elsewhere in the library.
1029 */
Mike Lockwoodf602d362010-06-20 14:28:16 -07001030/*static*/ android_thread_id_t AndroidRuntime::createJavaThread(const char* name,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 void (*start)(void *), void* arg)
1032{
Mike Lockwoodf602d362010-06-20 14:28:16 -07001033 android_thread_id_t threadId = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 javaCreateThreadEtc((android_thread_func_t) start, arg, name,
Mike Lockwoodf602d362010-06-20 14:28:16 -07001035 ANDROID_PRIORITY_DEFAULT, 0, &threadId);
1036 return threadId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037}
1038
1039#if 0
1040static void quickTest(void* arg)
1041{
1042 const char* str = (const char*) arg;
1043
1044 printf("In quickTest: %s\n", str);
1045}
1046#endif
1047
1048#ifdef NDEBUG
1049 #define REG_JNI(name) { name }
1050 struct RegJNIRec {
1051 int (*mProc)(JNIEnv*);
1052 };
1053#else
1054 #define REG_JNI(name) { name, #name }
1055 struct RegJNIRec {
1056 int (*mProc)(JNIEnv*);
1057 const char* mName;
1058 };
1059#endif
1060
1061typedef void (*RegJAMProc)();
1062
1063static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
1064{
1065 for (size_t i = 0; i < count; i++) {
1066 if (array[i].mProc(env) < 0) {
1067#ifndef NDEBUG
1068 LOGD("----------!!! %s failed to load\n", array[i].mName);
1069#endif
1070 return -1;
1071 }
1072 }
1073 return 0;
1074}
1075
1076static void register_jam_procs(const RegJAMProc array[], size_t count)
1077{
1078 for (size_t i = 0; i < count; i++) {
1079 array[i]();
1080 }
1081}
1082
1083static const RegJNIRec gRegJNI[] = {
1084 REG_JNI(register_android_debug_JNITest),
1085 REG_JNI(register_com_android_internal_os_RuntimeInit),
1086 REG_JNI(register_android_os_SystemClock),
1087 REG_JNI(register_android_util_EventLog),
1088 REG_JNI(register_android_util_Log),
1089 REG_JNI(register_android_util_FloatMath),
1090 REG_JNI(register_android_text_format_Time),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 REG_JNI(register_android_content_AssetManager),
1092 REG_JNI(register_android_content_StringBlock),
1093 REG_JNI(register_android_content_XmlBlock),
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07001094 REG_JNI(register_android_emoji_EmojiFactory),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 REG_JNI(register_android_text_AndroidCharacter),
Doug Feltdae8e942010-02-24 14:33:15 -08001096 REG_JNI(register_android_text_AndroidBidi),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 REG_JNI(register_android_text_KeyCharacterMap),
1098 REG_JNI(register_android_os_Process),
Andreas Hubera8079bf2010-11-16 14:40:31 -08001099 REG_JNI(register_android_os_SystemProperties),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 REG_JNI(register_android_os_Binder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 REG_JNI(register_android_view_Display),
Jeff Brown0a0a1242011-12-02 02:25:22 -08001102 REG_JNI(register_android_view_DisplayEventReceiver),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 REG_JNI(register_android_nio_utils),
1104 REG_JNI(register_android_graphics_PixelFormat),
1105 REG_JNI(register_android_graphics_Graphics),
Romain Guye4d01122010-06-16 18:44:05 -07001106 REG_JNI(register_android_view_GLES20Canvas),
Romain Guya9582652011-11-10 14:20:10 -08001107 REG_JNI(register_android_view_HardwareRenderer),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 REG_JNI(register_android_view_Surface),
Romain Guy8f0095c2011-05-02 17:24:22 -07001109 REG_JNI(register_android_view_TextureView),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 REG_JNI(register_com_google_android_gles_jni_EGLImpl),
1111 REG_JNI(register_com_google_android_gles_jni_GLImpl),
Jack Palevich1c4907e2009-04-13 16:22:25 -07001112 REG_JNI(register_android_opengl_jni_GLES10),
1113 REG_JNI(register_android_opengl_jni_GLES10Ext),
1114 REG_JNI(register_android_opengl_jni_GLES11),
1115 REG_JNI(register_android_opengl_jni_GLES11Ext),
Jack Palevich560814f2009-11-19 16:34:55 +08001116 REG_JNI(register_android_opengl_jni_GLES20),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117
1118 REG_JNI(register_android_graphics_Bitmap),
1119 REG_JNI(register_android_graphics_BitmapFactory),
Wei-Ta Chen6b849e22010-09-07 17:32:18 +08001120 REG_JNI(register_android_graphics_BitmapRegionDecoder),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 REG_JNI(register_android_graphics_Camera),
1122 REG_JNI(register_android_graphics_Canvas),
1123 REG_JNI(register_android_graphics_ColorFilter),
1124 REG_JNI(register_android_graphics_DrawFilter),
1125 REG_JNI(register_android_graphics_Interpolator),
1126 REG_JNI(register_android_graphics_LayerRasterizer),
1127 REG_JNI(register_android_graphics_MaskFilter),
1128 REG_JNI(register_android_graphics_Matrix),
1129 REG_JNI(register_android_graphics_Movie),
1130 REG_JNI(register_android_graphics_NinePatch),
1131 REG_JNI(register_android_graphics_Paint),
1132 REG_JNI(register_android_graphics_Path),
1133 REG_JNI(register_android_graphics_PathMeasure),
1134 REG_JNI(register_android_graphics_PathEffect),
1135 REG_JNI(register_android_graphics_Picture),
1136 REG_JNI(register_android_graphics_PorterDuff),
1137 REG_JNI(register_android_graphics_Rasterizer),
1138 REG_JNI(register_android_graphics_Region),
1139 REG_JNI(register_android_graphics_Shader),
Jamie Gennisaa0ce332011-01-06 17:04:26 -08001140 REG_JNI(register_android_graphics_SurfaceTexture),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 REG_JNI(register_android_graphics_Typeface),
1142 REG_JNI(register_android_graphics_Xfermode),
Wei-Ta Chenbca2d612009-11-30 17:52:05 +08001143 REG_JNI(register_android_graphics_YuvImage),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144
1145 REG_JNI(register_android_database_CursorWindow),
Vasu Nori5a03f362009-10-20 15:16:35 -07001146 REG_JNI(register_android_database_SQLiteCompiledSql),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 REG_JNI(register_android_database_SQLiteDatabase),
1148 REG_JNI(register_android_database_SQLiteDebug),
1149 REG_JNI(register_android_database_SQLiteProgram),
1150 REG_JNI(register_android_database_SQLiteQuery),
1151 REG_JNI(register_android_database_SQLiteStatement),
1152 REG_JNI(register_android_os_Debug),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 REG_JNI(register_android_os_FileObserver),
1154 REG_JNI(register_android_os_FileUtils),
Christopher Tatefa9e7c02010-05-06 12:07:10 -07001155 REG_JNI(register_android_os_MessageQueue),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 REG_JNI(register_android_os_ParcelFileDescriptor),
1157 REG_JNI(register_android_os_Power),
1158 REG_JNI(register_android_os_StatFs),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 REG_JNI(register_android_os_UEventObserver),
1160 REG_JNI(register_android_net_LocalSocketImpl),
1161 REG_JNI(register_android_net_NetworkUtils),
Dan Egnor2b4abcd2010-04-07 17:30:50 -07001162 REG_JNI(register_android_net_TrafficStats),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 REG_JNI(register_android_net_wifi_WifiManager),
Nick Pellycd0e8392010-10-13 17:25:24 -07001164 REG_JNI(register_android_nfc_NdefMessage),
1165 REG_JNI(register_android_nfc_NdefRecord),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 REG_JNI(register_android_os_MemoryFile),
1167 REG_JNI(register_com_android_internal_os_ZygoteInit),
1168 REG_JNI(register_android_hardware_Camera),
1169 REG_JNI(register_android_hardware_SensorManager),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001170 REG_JNI(register_android_hardware_UsbDevice),
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001171 REG_JNI(register_android_hardware_UsbDeviceConnection),
Mike Lockwoode7d511e2010-12-30 13:39:37 -05001172 REG_JNI(register_android_hardware_UsbRequest),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 REG_JNI(register_android_media_AudioRecord),
1174 REG_JNI(register_android_media_AudioSystem),
1175 REG_JNI(register_android_media_AudioTrack),
1176 REG_JNI(register_android_media_JetPlayer),
1177 REG_JNI(register_android_media_ToneGenerator),
1178
1179 REG_JNI(register_android_opengl_classes),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 REG_JNI(register_android_bluetooth_HeadsetBase),
1181 REG_JNI(register_android_bluetooth_BluetoothAudioGateway),
Nick Pelly0b6955a2009-05-26 19:13:43 -07001182 REG_JNI(register_android_bluetooth_BluetoothSocket),
Nick Pellybd022f42009-08-14 18:33:38 -07001183 REG_JNI(register_android_server_BluetoothService),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 REG_JNI(register_android_server_BluetoothEventLoop),
1185 REG_JNI(register_android_server_BluetoothA2dpService),
JP Abgrall98a4f7e2011-09-02 15:36:33 -07001186 REG_JNI(register_android_server_NetworkManagementSocketTagger),
Christopher Tateecaa7b42010-06-04 14:55:02 -07001187 REG_JNI(register_android_server_Watchdog),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 REG_JNI(register_android_ddm_DdmHandleNativeHeap),
Joe Onorato1cf58742009-06-12 11:06:24 -07001189 REG_JNI(register_android_backup_BackupDataInput),
Joe Onoratod2110db2009-05-19 13:41:21 -07001190 REG_JNI(register_android_backup_BackupDataOutput),
Joe Onorato06290a42009-06-18 20:10:37 -07001191 REG_JNI(register_android_backup_FileBackupHelperBase),
Joe Onorato4ababd92009-06-25 18:29:18 -04001192 REG_JNI(register_android_backup_BackupHelperDispatcher),
Christopher Tate4a627c72011-04-01 14:43:32 -07001193 REG_JNI(register_android_app_backup_FullBackup),
Chet Haase9c1e23b2011-03-24 10:51:31 -07001194 REG_JNI(register_android_app_ActivityThread),
Dianne Hackborn69969e42010-05-04 11:40:40 -07001195 REG_JNI(register_android_app_NativeActivity),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001196 REG_JNI(register_android_view_InputChannel),
Jeff Brown32cbc38552011-12-01 14:01:49 -08001197 REG_JNI(register_android_view_InputEventReceiver),
Jeff Brown46b9ac02010-04-22 18:58:52 -07001198 REG_JNI(register_android_view_KeyEvent),
1199 REG_JNI(register_android_view_MotionEvent),
Jeff Brown2352b972011-04-12 22:39:53 -07001200 REG_JNI(register_android_view_PointerIcon),
Jeff Brown2ed24622011-03-14 19:39:54 -07001201 REG_JNI(register_android_view_VelocityTracker),
Kenny Root02c87302010-07-01 08:10:18 -07001202
1203 REG_JNI(register_android_content_res_ObbScanner),
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -07001204 REG_JNI(register_android_content_res_Configuration),
Chet Haase6e0ecb42010-11-03 19:41:18 -07001205
1206 REG_JNI(register_android_animation_PropertyValuesHolder),
Kenny Root66269ea2011-07-12 14:14:01 -07001207 REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208};
1209
1210/*
1211 * Register android native functions with the VM.
1212 */
1213/*static*/ int AndroidRuntime::startReg(JNIEnv* env)
1214{
1215 /*
1216 * This hook causes all future threads created in this process to be
1217 * attached to the JavaVM. (This needs to go away in favor of JNI
1218 * Attach calls.)
1219 */
1220 androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
1221
Steve Block71f2cf12011-10-20 11:56:00 +01001222 ALOGV("--- registering native functions ---\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223
1224 /*
1225 * Every "register" function calls one or more things that return
1226 * a local reference (e.g. FindClass). Because we haven't really
1227 * started the VM yet, they're all getting stored in the base frame
1228 * and never released. Use Push/Pop to manage the storage.
1229 */
1230 env->PushLocalFrame(200);
1231
1232 if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
1233 env->PopLocalFrame(NULL);
1234 return -1;
1235 }
1236 env->PopLocalFrame(NULL);
1237
1238 //createJavaThread("fubar", quickTest, (void*) "hello");
1239
1240 return 0;
1241}
1242
1243AndroidRuntime* AndroidRuntime::getRuntime()
1244{
1245 return gCurRuntime;
1246}
1247
1248/**
1249 * Used by WithFramework to register native functions.
1250 */
1251extern "C"
1252jint Java_com_android_internal_util_WithFramework_registerNatives(
1253 JNIEnv* env, jclass clazz) {
1254 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1255}
1256
1257/**
1258 * Used by LoadClass to register native functions.
1259 */
1260extern "C"
1261jint Java_LoadClass_registerNatives(JNIEnv* env, jclass clazz) {
1262 return register_jni_procs(gRegJNI, NELEM(gRegJNI), env);
1263}
1264
1265} // namespace android