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