| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | /* |
| 18 | * Dalvik initialization, shutdown, and command-line argument processing. |
| 19 | */ |
| 20 | #include "Dalvik.h" |
| 21 | #include "test/Test.h" |
| 22 | #include "mterp/Mterp.h" |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <signal.h> |
| 27 | #include <limits.h> |
| 28 | #include <ctype.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #define kMinHeapStartSize (1*1024*1024) |
| 33 | #define kMinHeapSize (2*1024*1024) |
| 34 | #define kMaxHeapSize (1*1024*1024*1024) |
| 35 | |
| 36 | /* |
| 37 | * Register VM-agnostic native methods for system classes. |
| 38 | * |
| 39 | * Currently defined in ../include/nativehelper/AndroidSystemNatives.h |
| 40 | */ |
| 41 | extern int jniRegisterSystemMethods(JNIEnv* env); |
| 42 | |
| 43 | /* fwd */ |
| 44 | static bool registerSystemNatives(JNIEnv* pEnv); |
| 45 | static bool dvmInitJDWP(void); |
| 46 | static bool dvmInitZygote(void); |
| 47 | |
| 48 | |
| 49 | /* global state */ |
| 50 | struct DvmGlobals gDvm; |
| 51 | |
| 52 | /* |
| 53 | * Show usage. |
| 54 | * |
| 55 | * We follow the tradition of unhyphenated compound words. |
| 56 | */ |
| 57 | static void dvmUsage(const char* progName) |
| 58 | { |
| 59 | dvmFprintf(stderr, "%s: [options] class [argument ...]\n", progName); |
| 60 | dvmFprintf(stderr, "%s: [options] -jar file.jar [argument ...]\n",progName); |
| 61 | dvmFprintf(stderr, "\n"); |
| 62 | dvmFprintf(stderr, "The following standard options are recognized:\n"); |
| 63 | dvmFprintf(stderr, " -classpath classpath\n"); |
| 64 | dvmFprintf(stderr, " -Dproperty=value\n"); |
| 65 | dvmFprintf(stderr, " -verbose:tag ('gc', 'jni', or 'class')\n"); |
| 66 | dvmFprintf(stderr, " -ea[:<package name>... |:<class name>]\n"); |
| 67 | dvmFprintf(stderr, " -da[:<package name>... |:<class name>]\n"); |
| 68 | dvmFprintf(stderr, " (-enableassertions, -disableassertions)\n"); |
| 69 | dvmFprintf(stderr, " -esa\n"); |
| 70 | dvmFprintf(stderr, " -dsa\n"); |
| 71 | dvmFprintf(stderr, |
| 72 | " (-enablesystemassertions, -disablesystemassertions)\n"); |
| 73 | dvmFprintf(stderr, " -showversion\n"); |
| 74 | dvmFprintf(stderr, " -help\n"); |
| 75 | dvmFprintf(stderr, "\n"); |
| 76 | dvmFprintf(stderr, "The following extended options are recognized:\n"); |
| 77 | dvmFprintf(stderr, " -Xrunjdwp:<options>\n"); |
| 78 | dvmFprintf(stderr, " -Xbootclasspath:bootclasspath\n"); |
| 79 | dvmFprintf(stderr, " -Xcheck:tag (e.g. 'jni')\n"); |
| 80 | dvmFprintf(stderr, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n"); |
| 81 | dvmFprintf(stderr, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n"); |
| 82 | dvmFprintf(stderr, " -XssN (stack size, >= %dKB, <= %dKB)\n", |
| 83 | kMinStackSize / 1024, kMaxStackSize / 1024); |
| 84 | dvmFprintf(stderr, " -Xverify:{none,remote,all}\n"); |
| 85 | dvmFprintf(stderr, " -Xrs\n"); |
| 86 | dvmFprintf(stderr, |
| 87 | " -Xint (extended to accept ':portable' and ':fast')\n"); |
| 88 | dvmFprintf(stderr, "\n"); |
| 89 | dvmFprintf(stderr, "These are unique to Dalvik:\n"); |
| 90 | dvmFprintf(stderr, " -Xzygote\n"); |
| 91 | dvmFprintf(stderr, " -Xdexopt:{none,verified,all}\n"); |
| 92 | dvmFprintf(stderr, " -Xnoquithandler\n"); |
| 93 | dvmFprintf(stderr, |
| 94 | " -Xjnigreflimit:N (must be multiple of 100, >= 200)\n"); |
| 95 | dvmFprintf(stderr, " -Xjniopts:{warnonly,forcecopy}\n"); |
| 96 | dvmFprintf(stderr, " -Xdeadlockpredict:{off,warn,err,abort}\n"); |
| 97 | dvmFprintf(stderr, " -Xstacktracefile:<filename>\n"); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame^] | 98 | dvmFprintf(stderr, " -Xgc:[no]precise\n"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 99 | dvmFprintf(stderr, " -Xgenregmap\n"); |
| 100 | dvmFprintf(stderr, " -Xcheckdexsum\n"); |
| 101 | dvmFprintf(stderr, "\n"); |
| 102 | dvmFprintf(stderr, "Configured with:" |
| 103 | #ifdef WITH_DEBUGGER |
| 104 | " debugger" |
| 105 | #endif |
| 106 | #ifdef WITH_PROFILER |
| 107 | " profiler" |
| 108 | #endif |
| 109 | #ifdef WITH_MONITOR_TRACKING |
| 110 | " monitor_tracking" |
| 111 | #endif |
| 112 | #ifdef WITH_DEADLOCK_PREDICTION |
| 113 | " deadlock_prediction" |
| 114 | #endif |
| 115 | #ifdef WITH_HPROF |
| 116 | " hprof" |
| 117 | #endif |
| 118 | #ifdef WITH_HPROF_STACK |
| 119 | " hprof_stack" |
| 120 | #endif |
| 121 | #ifdef WITH_HPROF_STACK_UNREACHABLE |
| 122 | " hprof_stack_unreachable" |
| 123 | #endif |
| 124 | #ifdef WITH_ALLOC_LIMITS |
| 125 | " alloc_limits" |
| 126 | #endif |
| 127 | #ifdef WITH_TRACKREF_CHECKS |
| 128 | " trackref_checks" |
| 129 | #endif |
| 130 | #ifdef WITH_INSTR_CHECKS |
| 131 | " instr_checks" |
| 132 | #endif |
| 133 | #ifdef WITH_EXTRA_OBJECT_VALIDATION |
| 134 | " extra_object_validation" |
| 135 | #endif |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame^] | 136 | #ifdef WITH_EXTRA_GC_CHECKS |
| 137 | " extra_gc_checks" |
| 138 | #endif |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 139 | #ifdef WITH_DALVIK_ASSERT |
| 140 | " dalvik_assert" |
| 141 | #endif |
| 142 | #ifdef WITH_JNI_STACK_CHECK |
| 143 | " jni_stack_check" |
| 144 | #endif |
| 145 | #ifdef EASY_GDB |
| 146 | " easy_gdb" |
| 147 | #endif |
| 148 | #ifdef CHECK_MUTEX |
| 149 | " check_mutex" |
| 150 | #endif |
| 151 | #ifdef PROFILE_FIELD_ACCESS |
| 152 | " profile_field_access" |
| 153 | #endif |
| 154 | #ifdef DVM_TRACK_HEAP_MARKING |
| 155 | " track_heap_marking" |
| 156 | #endif |
| 157 | #if DVM_RESOLVER_CACHE == DVM_RC_REDUCING |
| 158 | " resolver_cache_reducing" |
| 159 | #elif DVM_RESOLVER_CACHE == DVM_RC_EXPANDING |
| 160 | " resolver_cache_expanding" |
| 161 | #elif DVM_RESOLVER_CACHE == DVM_RC_NO_CACHE |
| 162 | " resolver_cache_disabled" |
| 163 | #endif |
| 164 | ); |
| 165 | #ifdef DVM_SHOW_EXCEPTION |
| 166 | dvmFprintf(stderr, " show_exception=%d", DVM_SHOW_EXCEPTION); |
| 167 | #endif |
| 168 | dvmFprintf(stderr, "\n\n"); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * Show helpful information on JDWP options. |
| 173 | */ |
| 174 | static void showJdwpHelp(void) |
| 175 | { |
| 176 | dvmFprintf(stderr, |
| 177 | "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"); |
| 178 | dvmFprintf(stderr, |
| 179 | "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n"); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Show version and copyright info. |
| 184 | */ |
| 185 | static void showVersion(void) |
| 186 | { |
| 187 | dvmFprintf(stdout, "DalvikVM version %d.%d.%d\n", |
| 188 | DALVIK_MAJOR_VERSION, DALVIK_MINOR_VERSION, DALVIK_BUG_VERSION); |
| 189 | dvmFprintf(stdout, |
| 190 | "Copyright (C) 2007 The Android Open Source Project\n\n" |
| 191 | "This software is built from source code licensed under the " |
| 192 | "Apache License,\n" |
| 193 | "Version 2.0 (the \"License\"). You may obtain a copy of the " |
| 194 | "License at\n\n" |
| 195 | " http://www.apache.org/licenses/LICENSE-2.0\n\n" |
| 196 | "See the associated NOTICE file for this software for further " |
| 197 | "details.\n"); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify |
| 202 | * memory sizes. [kK] indicates kilobytes, [mM] megabytes, and |
| 203 | * [gG] gigabytes. |
| 204 | * |
| 205 | * "s" should point just past the "-Xm?" part of the string. |
| 206 | * "min" specifies the lowest acceptable value described by "s". |
| 207 | * "div" specifies a divisor, e.g. 1024 if the value must be a multiple |
| 208 | * of 1024. |
| 209 | * |
| 210 | * The spec says the -Xmx and -Xms options must be multiples of 1024. It |
| 211 | * doesn't say anything about -Xss. |
| 212 | * |
| 213 | * Returns 0 (a useless size) if "s" is malformed or specifies a low or |
| 214 | * non-evenly-divisible value. |
| 215 | */ |
| 216 | static unsigned int dvmParseMemOption(const char *s, unsigned int div) |
| 217 | { |
| 218 | /* strtoul accepts a leading [+-], which we don't want, |
| 219 | * so make sure our string starts with a decimal digit. |
| 220 | */ |
| 221 | if (isdigit(*s)) { |
| 222 | const char *s2; |
| 223 | unsigned int val; |
| 224 | |
| 225 | val = (unsigned int)strtoul(s, (char **)&s2, 10); |
| 226 | if (s2 != s) { |
| 227 | /* s2 should be pointing just after the number. |
| 228 | * If this is the end of the string, the user |
| 229 | * has specified a number of bytes. Otherwise, |
| 230 | * there should be exactly one more character |
| 231 | * that specifies a multiplier. |
| 232 | */ |
| 233 | if (*s2 != '\0') { |
| 234 | char c; |
| 235 | |
| 236 | /* The remainder of the string is either a single multiplier |
| 237 | * character, or nothing to indicate that the value is in |
| 238 | * bytes. |
| 239 | */ |
| 240 | c = *s2++; |
| 241 | if (*s2 == '\0') { |
| 242 | unsigned int mul; |
| 243 | |
| 244 | if (c == '\0') { |
| 245 | mul = 1; |
| 246 | } else if (c == 'k' || c == 'K') { |
| 247 | mul = 1024; |
| 248 | } else if (c == 'm' || c == 'M') { |
| 249 | mul = 1024 * 1024; |
| 250 | } else if (c == 'g' || c == 'G') { |
| 251 | mul = 1024 * 1024 * 1024; |
| 252 | } else { |
| 253 | /* Unknown multiplier character. |
| 254 | */ |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | if (val <= UINT_MAX / mul) { |
| 259 | val *= mul; |
| 260 | } else { |
| 261 | /* Clamp to a multiple of 1024. |
| 262 | */ |
| 263 | val = UINT_MAX & ~(1024-1); |
| 264 | } |
| 265 | } else { |
| 266 | /* There's more than one character after the |
| 267 | * numeric part. |
| 268 | */ |
| 269 | return 0; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* The man page says that a -Xm value must be |
| 274 | * a multiple of 1024. |
| 275 | */ |
| 276 | if (val % div == 0) { |
| 277 | return val; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Handle one of the JDWP name/value pairs. |
| 287 | * |
| 288 | * JDWP options are: |
| 289 | * help: if specified, show help message and bail |
| 290 | * transport: may be dt_socket or dt_shmem |
| 291 | * address: for dt_socket, "host:port", or just "port" when listening |
| 292 | * server: if "y", wait for debugger to attach; if "n", attach to debugger |
| 293 | * timeout: how long to wait for debugger to connect / listen |
| 294 | * |
| 295 | * Useful with server=n (these aren't supported yet): |
| 296 | * onthrow=<exception-name>: connect to debugger when exception thrown |
| 297 | * onuncaught=y|n: connect to debugger when uncaught exception thrown |
| 298 | * launch=<command-line>: launch the debugger itself |
| 299 | * |
| 300 | * The "transport" option is required, as is "address" if server=n. |
| 301 | */ |
| 302 | static bool handleJdwpOption(const char* name, const char* value) |
| 303 | { |
| 304 | if (strcmp(name, "transport") == 0) { |
| 305 | if (strcmp(value, "dt_socket") == 0) { |
| 306 | gDvm.jdwpTransport = kJdwpTransportSocket; |
| 307 | } else if (strcmp(value, "dt_android_adb") == 0) { |
| 308 | gDvm.jdwpTransport = kJdwpTransportAndroidAdb; |
| 309 | } else { |
| 310 | LOGE("JDWP transport '%s' not supported\n", value); |
| 311 | return false; |
| 312 | } |
| 313 | } else if (strcmp(name, "server") == 0) { |
| 314 | if (*value == 'n') |
| 315 | gDvm.jdwpServer = false; |
| 316 | else if (*value == 'y') |
| 317 | gDvm.jdwpServer = true; |
| 318 | else { |
| 319 | LOGE("JDWP option 'server' must be 'y' or 'n'\n"); |
| 320 | return false; |
| 321 | } |
| 322 | } else if (strcmp(name, "suspend") == 0) { |
| 323 | if (*value == 'n') |
| 324 | gDvm.jdwpSuspend = false; |
| 325 | else if (*value == 'y') |
| 326 | gDvm.jdwpSuspend = true; |
| 327 | else { |
| 328 | LOGE("JDWP option 'suspend' must be 'y' or 'n'\n"); |
| 329 | return false; |
| 330 | } |
| 331 | } else if (strcmp(name, "address") == 0) { |
| 332 | /* this is either <port> or <host>:<port> */ |
| 333 | const char* colon = strchr(value, ':'); |
| 334 | char* end; |
| 335 | long port; |
| 336 | |
| 337 | if (colon != NULL) { |
| 338 | free(gDvm.jdwpHost); |
| 339 | gDvm.jdwpHost = (char*) malloc(colon - value +1); |
| 340 | strncpy(gDvm.jdwpHost, value, colon - value +1); |
| 341 | gDvm.jdwpHost[colon-value] = '\0'; |
| 342 | value = colon + 1; |
| 343 | } |
| 344 | if (*value == '\0') { |
| 345 | LOGE("JDWP address missing port\n"); |
| 346 | return false; |
| 347 | } |
| 348 | port = strtol(value, &end, 10); |
| 349 | if (*end != '\0') { |
| 350 | LOGE("JDWP address has junk in port field '%s'\n", value); |
| 351 | return false; |
| 352 | } |
| 353 | gDvm.jdwpPort = port; |
| 354 | } else if (strcmp(name, "launch") == 0 || |
| 355 | strcmp(name, "onthrow") == 0 || |
| 356 | strcmp(name, "oncaught") == 0 || |
| 357 | strcmp(name, "timeout") == 0) |
| 358 | { |
| 359 | /* valid but unsupported */ |
| 360 | LOGI("Ignoring JDWP option '%s'='%s'\n", name, value); |
| 361 | } else { |
| 362 | LOGI("Ignoring unrecognized JDWP option '%s'='%s'\n", name, value); |
| 363 | } |
| 364 | |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.: |
| 370 | * "transport=dt_socket,address=8000,server=y,suspend=n" |
| 371 | */ |
| 372 | static bool parseJdwpOptions(const char* str) |
| 373 | { |
| 374 | char* mangle = strdup(str); |
| 375 | char* name = mangle; |
| 376 | bool result = false; |
| 377 | |
| 378 | /* |
| 379 | * Process all of the name=value pairs. |
| 380 | */ |
| 381 | while (true) { |
| 382 | char* value; |
| 383 | char* comma; |
| 384 | |
| 385 | value = strchr(name, '='); |
| 386 | if (value == NULL) { |
| 387 | LOGE("JDWP opts: garbage at '%s'\n", name); |
| 388 | goto bail; |
| 389 | } |
| 390 | |
| 391 | comma = strchr(name, ','); // use name, not value, for safety |
| 392 | if (comma != NULL) { |
| 393 | if (comma < value) { |
| 394 | LOGE("JDWP opts: found comma before '=' in '%s'\n", mangle); |
| 395 | goto bail; |
| 396 | } |
| 397 | *comma = '\0'; |
| 398 | } |
| 399 | |
| 400 | *value++ = '\0'; // stomp the '=' |
| 401 | |
| 402 | if (!handleJdwpOption(name, value)) |
| 403 | goto bail; |
| 404 | |
| 405 | if (comma == NULL) { |
| 406 | /* out of options */ |
| 407 | break; |
| 408 | } |
| 409 | name = comma+1; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Make sure the combination of arguments makes sense. |
| 414 | */ |
| 415 | if (gDvm.jdwpTransport == kJdwpTransportUnknown) { |
| 416 | LOGE("JDWP opts: must specify transport\n"); |
| 417 | goto bail; |
| 418 | } |
| 419 | if (!gDvm.jdwpServer && (gDvm.jdwpHost == NULL || gDvm.jdwpPort == 0)) { |
| 420 | LOGE("JDWP opts: when server=n, must specify host and port\n"); |
| 421 | goto bail; |
| 422 | } |
| 423 | // transport mandatory |
| 424 | // outbound server address |
| 425 | |
| 426 | gDvm.jdwpConfigured = true; |
| 427 | result = true; |
| 428 | |
| 429 | bail: |
| 430 | free(mangle); |
| 431 | return result; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Handle one of the four kinds of assertion arguments. |
| 436 | * |
| 437 | * "pkgOrClass" is the last part of an enable/disable line. For a package |
| 438 | * the arg looks like "-ea:com.google.fubar...", for a class it looks |
| 439 | * like "-ea:com.google.fubar.Wahoo". The string we get starts at the ':'. |
| 440 | * |
| 441 | * For system assertions (-esa/-dsa), "pkgOrClass" is NULL. |
| 442 | * |
| 443 | * Multiple instances of these arguments can be specified, e.g. you can |
| 444 | * enable assertions for a package and then disable them for one class in |
| 445 | * the package. |
| 446 | */ |
| 447 | static bool enableAssertions(const char* pkgOrClass, bool enable) |
| 448 | { |
| 449 | AssertionControl* pCtrl = &gDvm.assertionCtrl[gDvm.assertionCtrlCount++]; |
| 450 | pCtrl->enable = enable; |
| 451 | |
| 452 | if (pkgOrClass == NULL) { |
| 453 | /* enable or disable for all system classes */ |
| 454 | pCtrl->isPackage = false; |
| 455 | pCtrl->pkgOrClass = NULL; |
| 456 | pCtrl->pkgOrClassLen = 0; |
| 457 | } else { |
| 458 | if (*pkgOrClass == '\0') { |
| 459 | /* global enable/disable for all but system */ |
| 460 | pCtrl->isPackage = false; |
| 461 | pCtrl->pkgOrClass = strdup(""); |
| 462 | pCtrl->pkgOrClassLen = 0; |
| 463 | } else { |
| 464 | pCtrl->pkgOrClass = dvmDotToSlash(pkgOrClass+1); // skip ':' |
| 465 | if (pCtrl->pkgOrClass == NULL) { |
| 466 | /* can happen if class name includes an illegal '/' */ |
| 467 | LOGW("Unable to process assertion arg '%s'\n", pkgOrClass); |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | int len = strlen(pCtrl->pkgOrClass); |
| 472 | if (len >= 3 && strcmp(pCtrl->pkgOrClass + len-3, "///") == 0) { |
| 473 | /* mark as package, truncate two of the three slashes */ |
| 474 | pCtrl->isPackage = true; |
| 475 | *(pCtrl->pkgOrClass + len-2) = '\0'; |
| 476 | pCtrl->pkgOrClassLen = len - 2; |
| 477 | } else { |
| 478 | /* just a class */ |
| 479 | pCtrl->isPackage = false; |
| 480 | pCtrl->pkgOrClassLen = len; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * Turn assertions on when requested to do so by the Zygote. |
| 490 | * |
| 491 | * This is a bit sketchy. We can't (easily) go back and fiddle with all |
| 492 | * of the classes that have already been initialized, so this only |
| 493 | * affects classes that have yet to be loaded. If some or all assertions |
| 494 | * have been enabled through some other means, we don't want to mess with |
| 495 | * it here, so we do nothing. Finally, we assume that there's room in |
| 496 | * "assertionCtrl" to hold at least one entry; this is guaranteed by the |
| 497 | * allocator. |
| 498 | * |
| 499 | * This must only be called from the main thread during zygote init. |
| 500 | */ |
| 501 | void dvmLateEnableAssertions(void) |
| 502 | { |
| 503 | if (gDvm.assertionCtrl == NULL) { |
| 504 | LOGD("Not late-enabling assertions: no assertionCtrl array\n"); |
| 505 | return; |
| 506 | } else if (gDvm.assertionCtrlCount != 0) { |
| 507 | LOGD("Not late-enabling assertions: some asserts already configured\n"); |
| 508 | return; |
| 509 | } |
| 510 | LOGD("Late-enabling assertions\n"); |
| 511 | |
| 512 | /* global enable for all but system */ |
| 513 | AssertionControl* pCtrl = gDvm.assertionCtrl; |
| 514 | pCtrl->pkgOrClass = strdup(""); |
| 515 | pCtrl->pkgOrClassLen = 0; |
| 516 | pCtrl->isPackage = false; |
| 517 | pCtrl->enable = true; |
| 518 | gDvm.assertionCtrlCount = 1; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /* |
| 523 | * Release memory associated with the AssertionCtrl array. |
| 524 | */ |
| 525 | static void freeAssertionCtrl(void) |
| 526 | { |
| 527 | int i; |
| 528 | |
| 529 | for (i = 0; i < gDvm.assertionCtrlCount; i++) |
| 530 | free(gDvm.assertionCtrl[i].pkgOrClass); |
| 531 | free(gDvm.assertionCtrl); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | /* |
| 536 | * Process an argument vector full of options. Unlike standard C programs, |
| 537 | * argv[0] does not contain the name of the program. |
| 538 | * |
| 539 | * If "ignoreUnrecognized" is set, we ignore options starting with "-X" or "_" |
| 540 | * that we don't recognize. Otherwise, we return with an error as soon as |
| 541 | * we see anything we can't identify. |
| 542 | * |
| 543 | * Returns 0 on success, -1 on failure, and 1 for the special case of |
| 544 | * "-version" where we want to stop without showing an error message. |
| 545 | */ |
| 546 | static int dvmProcessOptions(int argc, const char* const argv[], |
| 547 | bool ignoreUnrecognized) |
| 548 | { |
| 549 | int i; |
| 550 | |
| 551 | LOGV("VM options (%d):\n", argc); |
| 552 | for (i = 0; i < argc; i++) |
| 553 | LOGV(" %d: '%s'\n", i, argv[i]); |
| 554 | |
| 555 | /* |
| 556 | * Over-allocate AssertionControl array for convenience. If allocated, |
| 557 | * the array must be able to hold at least one entry, so that the |
| 558 | * zygote-time activation can do its business. |
| 559 | */ |
| 560 | assert(gDvm.assertionCtrl == NULL); |
| 561 | if (argc > 0) { |
| 562 | gDvm.assertionCtrl = |
| 563 | (AssertionControl*) malloc(sizeof(AssertionControl) * argc); |
| 564 | if (gDvm.assertionCtrl == NULL) |
| 565 | return -1; |
| 566 | assert(gDvm.assertionCtrlCount == 0); |
| 567 | } |
| 568 | |
| 569 | for (i = 0; i < argc; i++) { |
| 570 | if (strcmp(argv[i], "-help") == 0) { |
| 571 | /* show usage and stop */ |
| 572 | return -1; |
| 573 | |
| 574 | } else if (strcmp(argv[i], "-version") == 0) { |
| 575 | /* show version and stop */ |
| 576 | showVersion(); |
| 577 | return 1; |
| 578 | } else if (strcmp(argv[i], "-showversion") == 0) { |
| 579 | /* show version and continue */ |
| 580 | showVersion(); |
| 581 | |
| 582 | } else if (strcmp(argv[i], "-classpath") == 0 || |
| 583 | strcmp(argv[i], "-cp") == 0) |
| 584 | { |
| 585 | /* set classpath */ |
| 586 | if (i == argc-1) { |
| 587 | dvmFprintf(stderr, "Missing classpath path list\n"); |
| 588 | return -1; |
| 589 | } |
| 590 | free(gDvm.classPathStr); /* in case we have compiled-in default */ |
| 591 | gDvm.classPathStr = strdup(argv[++i]); |
| 592 | |
| 593 | } else if (strncmp(argv[i], "-Xbootclasspath:", |
| 594 | sizeof("-Xbootclasspath:")-1) == 0) |
| 595 | { |
| 596 | /* set bootclasspath */ |
| 597 | const char* path = argv[i] + sizeof("-Xbootclasspath:")-1; |
| 598 | |
| 599 | if (*path == '\0') { |
| 600 | dvmFprintf(stderr, "Missing bootclasspath path list\n"); |
| 601 | return -1; |
| 602 | } |
| 603 | free(gDvm.bootClassPathStr); |
| 604 | gDvm.bootClassPathStr = strdup(path); |
| 605 | |
| 606 | /* |
| 607 | * TODO: support -Xbootclasspath/a and /p, which append or |
| 608 | * prepend to the default bootclasspath. We set the default |
| 609 | * path earlier. |
| 610 | */ |
| 611 | |
| 612 | } else if (strncmp(argv[i], "-D", 2) == 0) { |
| 613 | /* set property */ |
| 614 | dvmAddCommandLineProperty(argv[i] + 2); |
| 615 | |
| 616 | } else if (strcmp(argv[i], "-jar") == 0) { |
| 617 | // TODO: handle this; name of jar should be in argv[i+1] |
| 618 | dvmFprintf(stderr, "-jar not yet handled\n"); |
| 619 | assert(false); |
| 620 | |
| 621 | } else if (strncmp(argv[i], "-Xms", 4) == 0) { |
| 622 | unsigned int val = dvmParseMemOption(argv[i]+4, 1024); |
| 623 | if (val != 0) { |
| 624 | if (val >= kMinHeapStartSize && val <= kMaxHeapSize) { |
| 625 | gDvm.heapSizeStart = val; |
| 626 | } else { |
| 627 | dvmFprintf(stderr, |
| 628 | "Invalid -Xms '%s', range is %dKB to %dKB\n", |
| 629 | argv[i], kMinHeapStartSize/1024, kMaxHeapSize/1024); |
| 630 | return -1; |
| 631 | } |
| 632 | } else { |
| 633 | dvmFprintf(stderr, "Invalid -Xms option '%s'\n", argv[i]); |
| 634 | return -1; |
| 635 | } |
| 636 | } else if (strncmp(argv[i], "-Xmx", 4) == 0) { |
| 637 | unsigned int val = dvmParseMemOption(argv[i]+4, 1024); |
| 638 | if (val != 0) { |
| 639 | if (val >= kMinHeapSize && val <= kMaxHeapSize) { |
| 640 | gDvm.heapSizeMax = val; |
| 641 | } else { |
| 642 | dvmFprintf(stderr, |
| 643 | "Invalid -Xmx '%s', range is %dKB to %dKB\n", |
| 644 | argv[i], kMinHeapSize/1024, kMaxHeapSize/1024); |
| 645 | return -1; |
| 646 | } |
| 647 | } else { |
| 648 | dvmFprintf(stderr, "Invalid -Xmx option '%s'\n", argv[i]); |
| 649 | return -1; |
| 650 | } |
| 651 | } else if (strncmp(argv[i], "-Xss", 4) == 0) { |
| 652 | unsigned int val = dvmParseMemOption(argv[i]+4, 1); |
| 653 | if (val != 0) { |
| 654 | if (val >= kMinStackSize && val <= kMaxStackSize) { |
| 655 | gDvm.stackSize = val; |
| 656 | } else { |
| 657 | dvmFprintf(stderr, "Invalid -Xss '%s', range is %d to %d\n", |
| 658 | argv[i], kMinStackSize, kMaxStackSize); |
| 659 | return -1; |
| 660 | } |
| 661 | } else { |
| 662 | dvmFprintf(stderr, "Invalid -Xss option '%s'\n", argv[i]); |
| 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | } else if (strcmp(argv[i], "-verbose") == 0 || |
| 667 | strcmp(argv[i], "-verbose:class") == 0) |
| 668 | { |
| 669 | // JNI spec says "-verbose:gc,class" is valid, but cmd line |
| 670 | // doesn't work that way; may want to support. |
| 671 | gDvm.verboseClass = true; |
| 672 | } else if (strcmp(argv[i], "-verbose:jni") == 0) { |
| 673 | gDvm.verboseJni = true; |
| 674 | } else if (strcmp(argv[i], "-verbose:gc") == 0) { |
| 675 | gDvm.verboseGc = true; |
| 676 | |
| 677 | } else if (strncmp(argv[i], "-enableassertions", 17) == 0) { |
| 678 | enableAssertions(argv[i] + 17, true); |
| 679 | } else if (strncmp(argv[i], "-ea", 3) == 0) { |
| 680 | enableAssertions(argv[i] + 3, true); |
| 681 | } else if (strncmp(argv[i], "-disableassertions", 18) == 0) { |
| 682 | enableAssertions(argv[i] + 18, false); |
| 683 | } else if (strncmp(argv[i], "-da", 3) == 0) { |
| 684 | enableAssertions(argv[i] + 3, false); |
| 685 | } else if (strcmp(argv[i], "-enablesystemassertions") == 0 || |
| 686 | strcmp(argv[i], "-esa") == 0) |
| 687 | { |
| 688 | enableAssertions(NULL, true); |
| 689 | } else if (strcmp(argv[i], "-disablesystemassertions") == 0 || |
| 690 | strcmp(argv[i], "-dsa") == 0) |
| 691 | { |
| 692 | enableAssertions(NULL, false); |
| 693 | |
| 694 | } else if (strncmp(argv[i], "-Xcheck:jni", 11) == 0) { |
| 695 | /* nothing to do now -- was handled during JNI init */ |
| 696 | |
| 697 | } else if (strcmp(argv[i], "-Xdebug") == 0) { |
| 698 | /* accept but ignore */ |
| 699 | |
| 700 | } else if (strncmp(argv[i], "-Xrunjdwp:", 10) == 0 || |
| 701 | strncmp(argv[i], "-agentlib:jdwp=", 15) == 0) |
| 702 | { |
| 703 | const char* tail; |
| 704 | bool result = false; |
| 705 | |
| 706 | if (argv[i][1] == 'X') |
| 707 | tail = argv[i] + 10; |
| 708 | else |
| 709 | tail = argv[i] + 15; |
| 710 | |
| 711 | if (strncmp(tail, "help", 4) == 0 || !parseJdwpOptions(tail)) { |
| 712 | showJdwpHelp(); |
| 713 | return 1; |
| 714 | } |
| 715 | } else if (strcmp(argv[i], "-Xrs") == 0) { |
| 716 | gDvm.reduceSignals = true; |
| 717 | } else if (strcmp(argv[i], "-Xnoquithandler") == 0) { |
| 718 | /* disables SIGQUIT handler thread while still blocking SIGQUIT */ |
| 719 | /* (useful if we don't want thread but system still signals us) */ |
| 720 | gDvm.noQuitHandler = true; |
| 721 | } else if (strcmp(argv[i], "-Xzygote") == 0) { |
| 722 | gDvm.zygote = true; |
| 723 | } else if (strncmp(argv[i], "-Xdexopt:", 9) == 0) { |
| 724 | if (strcmp(argv[i] + 9, "none") == 0) |
| 725 | gDvm.dexOptMode = OPTIMIZE_MODE_NONE; |
| 726 | else if (strcmp(argv[i] + 9, "verified") == 0) |
| 727 | gDvm.dexOptMode = OPTIMIZE_MODE_VERIFIED; |
| 728 | else if (strcmp(argv[i] + 9, "all") == 0) |
| 729 | gDvm.dexOptMode = OPTIMIZE_MODE_ALL; |
| 730 | else { |
| 731 | dvmFprintf(stderr, "Unrecognized dexopt option '%s'\n",argv[i]); |
| 732 | return -1; |
| 733 | } |
| 734 | } else if (strncmp(argv[i], "-Xverify:", 9) == 0) { |
| 735 | if (strcmp(argv[i] + 9, "none") == 0) |
| 736 | gDvm.classVerifyMode = VERIFY_MODE_NONE; |
| 737 | else if (strcmp(argv[i] + 9, "remote") == 0) |
| 738 | gDvm.classVerifyMode = VERIFY_MODE_REMOTE; |
| 739 | else if (strcmp(argv[i] + 9, "all") == 0) |
| 740 | gDvm.classVerifyMode = VERIFY_MODE_ALL; |
| 741 | else { |
| 742 | dvmFprintf(stderr, "Unrecognized verify option '%s'\n",argv[i]); |
| 743 | return -1; |
| 744 | } |
| 745 | } else if (strncmp(argv[i], "-Xjnigreflimit:", 15) == 0) { |
| 746 | int lim = atoi(argv[i] + 15); |
| 747 | if (lim < 200 || (lim % 100) != 0) { |
| 748 | dvmFprintf(stderr, "Bad value for -Xjnigreflimit: '%s'\n", |
| 749 | argv[i]+15); |
| 750 | return -1; |
| 751 | } |
| 752 | gDvm.jniGrefLimit = lim; |
| 753 | |
| 754 | } else if (strcmp(argv[i], "-Xlog-stdio") == 0) { |
| 755 | gDvm.logStdio = true; |
| 756 | |
| 757 | } else if (strncmp(argv[i], "-Xint", 5) == 0) { |
| 758 | if (argv[i][5] == ':') { |
| 759 | if (strcmp(argv[i] + 6, "portable") == 0) |
| 760 | gDvm.executionMode = kExecutionModeInterpPortable; |
| 761 | else if (strcmp(argv[i] + 6, "fast") == 0) |
| 762 | gDvm.executionMode = kExecutionModeInterpFast; |
| 763 | else { |
| 764 | dvmFprintf(stderr, |
| 765 | "Warning: Unrecognized interpreter mode %s\n",argv[i]); |
| 766 | /* keep going */ |
| 767 | } |
| 768 | } else { |
| 769 | /* disable JIT -- nothing to do here for now */ |
| 770 | } |
| 771 | |
| 772 | } else if (strncmp(argv[i], "-Xdeadlockpredict:", 18) == 0) { |
| 773 | #ifdef WITH_DEADLOCK_PREDICTION |
| 774 | if (strcmp(argv[i] + 18, "off") == 0) |
| 775 | gDvm.deadlockPredictMode = kDPOff; |
| 776 | else if (strcmp(argv[i] + 18, "warn") == 0) |
| 777 | gDvm.deadlockPredictMode = kDPWarn; |
| 778 | else if (strcmp(argv[i] + 18, "err") == 0) |
| 779 | gDvm.deadlockPredictMode = kDPErr; |
| 780 | else if (strcmp(argv[i] + 18, "abort") == 0) |
| 781 | gDvm.deadlockPredictMode = kDPAbort; |
| 782 | else { |
| 783 | dvmFprintf(stderr, "Bad value for -Xdeadlockpredict"); |
| 784 | return -1; |
| 785 | } |
| 786 | if (gDvm.deadlockPredictMode != kDPOff) |
| 787 | LOGD("Deadlock prediction enabled (%s)\n", argv[i]+18); |
| 788 | #endif |
| 789 | |
| 790 | } else if (strncmp(argv[i], "-Xstacktracefile:", 17) == 0) { |
| 791 | gDvm.stackTraceFile = strdup(argv[i]+17); |
| 792 | |
| 793 | } else if (strcmp(argv[i], "-Xgenregmap") == 0) { |
| 794 | gDvm.generateRegisterMaps = true; |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame^] | 795 | LOGD("Register maps will be generated during verification\n"); |
| 796 | |
| 797 | } else if (strncmp(argv[i], "-Xgc:", 5) == 0) { |
| 798 | if (strcmp(argv[i] + 5, "precise") == 0) |
| 799 | gDvm.preciseGc = true; |
| 800 | else if (strcmp(argv[i] + 5, "noprecise") == 0) |
| 801 | gDvm.preciseGc = false; |
| 802 | else { |
| 803 | dvmFprintf(stderr, "Bad value for -Xgc"); |
| 804 | return -1; |
| 805 | } |
| 806 | LOGD("Precise GC configured %s\n", gDvm.preciseGc ? "ON" : "OFF"); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 807 | |
| 808 | } else if (strcmp(argv[i], "-Xcheckdexsum") == 0) { |
| 809 | gDvm.verifyDexChecksum = true; |
| 810 | |
| 811 | } else { |
| 812 | if (!ignoreUnrecognized) { |
| 813 | dvmFprintf(stderr, "Unrecognized option '%s'\n", argv[i]); |
| 814 | return -1; |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (gDvm.heapSizeStart > gDvm.heapSizeMax) { |
| 820 | dvmFprintf(stderr, "Heap start size must be <= heap max size\n"); |
| 821 | return -1; |
| 822 | } |
| 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Set defaults for fields altered or modified by arguments. |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame^] | 829 | * |
| 830 | * Globals are initialized to 0 (a/k/a NULL or false). |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 831 | */ |
| 832 | static void setCommandLineDefaults() |
| 833 | { |
| 834 | const char* envStr; |
| 835 | |
| 836 | envStr = getenv("CLASSPATH"); |
| 837 | if (envStr != NULL) |
| 838 | gDvm.classPathStr = strdup(envStr); |
| 839 | else |
| 840 | gDvm.classPathStr = strdup("."); |
| 841 | envStr = getenv("BOOTCLASSPATH"); |
| 842 | if (envStr != NULL) |
| 843 | gDvm.bootClassPathStr = strdup(envStr); |
| 844 | else |
| 845 | gDvm.bootClassPathStr = strdup("."); |
| 846 | |
| 847 | /* Defaults overridden by -Xms and -Xmx. |
| 848 | * TODO: base these on a system or application-specific default |
| 849 | */ |
| 850 | gDvm.heapSizeStart = 2 * 1024 * 1024; // Spec says 16MB; too big for us. |
| 851 | gDvm.heapSizeMax = 16 * 1024 * 1024; // Spec says 75% physical mem |
| 852 | gDvm.stackSize = kDefaultStackSize; |
| 853 | |
| 854 | /* gDvm.jdwpSuspend = true; */ |
| 855 | |
| 856 | /* allowed unless zygote config doesn't allow it */ |
| 857 | gDvm.jdwpAllowed = true; |
| 858 | |
| 859 | /* default verification and optimization modes */ |
| 860 | gDvm.classVerifyMode = VERIFY_MODE_ALL; |
| 861 | gDvm.dexOptMode = OPTIMIZE_MODE_VERIFIED; |
| 862 | |
| 863 | /* |
| 864 | * Default execution mode. |
| 865 | * |
| 866 | * This should probably interact with the mterp code somehow, e.g. if |
| 867 | * we know we're using the "desktop" build we should probably be |
| 868 | * using "portable" rather than "fast". |
| 869 | */ |
| 870 | gDvm.executionMode = kExecutionModeInterpFast; |
| 871 | } |
| 872 | |
| 873 | |
| 874 | /* |
| 875 | * Handle a SIGBUS, which frequently occurs because somebody replaced an |
| 876 | * optimized DEX file out from under us. |
| 877 | */ |
| 878 | static void busCatcher(int signum, siginfo_t* info, void* context) |
| 879 | { |
| 880 | void* addr = info->si_addr; |
| 881 | |
| 882 | LOGE("Caught a SIGBUS (%d), addr=%p\n", signum, addr); |
| 883 | |
| 884 | /* |
| 885 | * If we return at this point the SIGBUS just keeps happening, so we |
| 886 | * remove the signal handler and allow it to kill us. TODO: restore |
| 887 | * the original, which points to a debuggerd stub; if we don't then |
| 888 | * debuggerd won't be notified. |
| 889 | */ |
| 890 | signal(SIGBUS, SIG_DFL); |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * Configure signals. We need to block SIGQUIT so that the signal only |
| 895 | * reaches the dump-stack-trace thread. |
| 896 | * |
| 897 | * This can be disabled with the "-Xrs" flag. |
| 898 | */ |
| 899 | static void blockSignals() |
| 900 | { |
| 901 | sigset_t mask; |
| 902 | int cc; |
| 903 | |
| 904 | sigemptyset(&mask); |
| 905 | sigaddset(&mask, SIGQUIT); |
| 906 | sigaddset(&mask, SIGUSR1); // used to initiate heap dump |
| 907 | //sigaddset(&mask, SIGPIPE); |
| 908 | cc = sigprocmask(SIG_BLOCK, &mask, NULL); |
| 909 | assert(cc == 0); |
| 910 | |
| 911 | if (false) { |
| 912 | /* TODO: save the old sigaction in a global */ |
| 913 | struct sigaction sa; |
| 914 | memset(&sa, 0, sizeof(sa)); |
| 915 | sa.sa_sigaction = busCatcher; |
| 916 | sa.sa_flags = SA_SIGINFO; |
| 917 | cc = sigaction(SIGBUS, &sa, NULL); |
| 918 | assert(cc == 0); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * VM initialization. Pass in any options provided on the command line. |
| 924 | * Do not pass in the class name or the options for the class. |
| 925 | * |
| 926 | * Returns 0 on success. |
| 927 | */ |
| 928 | int dvmStartup(int argc, const char* const argv[], bool ignoreUnrecognized, |
| 929 | JNIEnv* pEnv) |
| 930 | { |
| 931 | int i, cc; |
| 932 | |
| 933 | assert(gDvm.initializing); |
| 934 | |
| 935 | LOGV("VM init args (%d):\n", argc); |
| 936 | for (i = 0; i < argc; i++) |
| 937 | LOGV(" %d: '%s'\n", i, argv[i]); |
| 938 | |
| 939 | setCommandLineDefaults(); |
| 940 | |
| 941 | /* prep properties storage */ |
| 942 | if (!dvmPropertiesStartup(argc)) |
| 943 | goto fail; |
| 944 | |
| 945 | /* |
| 946 | * Process the option flags (if any). |
| 947 | */ |
| 948 | cc = dvmProcessOptions(argc, argv, ignoreUnrecognized); |
| 949 | if (cc != 0) { |
| 950 | if (cc < 0) { |
| 951 | dvmFprintf(stderr, "\n"); |
| 952 | dvmUsage("dalvikvm"); |
| 953 | } |
| 954 | goto fail; |
| 955 | } |
| 956 | |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame^] | 957 | #if WITH_EXTRA_GC_CHECKS > 1 |
| 958 | /* only "portable" interp has the extra goodies */ |
| 959 | if (gDvm.executionMode != kExecutionModeInterpPortable) { |
| 960 | LOGI("Switching to 'portable' interpreter for GC checks\n"); |
| 961 | gDvm.executionMode = kExecutionModeInterpPortable; |
| 962 | } |
| 963 | #endif |
| 964 | |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 965 | /* configure signal handling */ |
| 966 | if (!gDvm.reduceSignals) |
| 967 | blockSignals(); |
| 968 | |
| 969 | /* mterp setup */ |
| 970 | LOGV("Using executionMode %d\n", gDvm.executionMode); |
| 971 | dvmCheckAsmConstants(); |
| 972 | |
| 973 | /* |
| 974 | * Initialize components. |
| 975 | */ |
| 976 | if (!dvmAllocTrackerStartup()) |
| 977 | goto fail; |
| 978 | if (!dvmGcStartup()) |
| 979 | goto fail; |
| 980 | if (!dvmThreadStartup()) |
| 981 | goto fail; |
| 982 | if (!dvmInlineNativeStartup()) |
| 983 | goto fail; |
| 984 | if (!dvmVerificationStartup()) |
| 985 | goto fail; |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame^] | 986 | if (!dvmRegisterMapStartup()) |
| 987 | goto fail; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 988 | if (!dvmInstanceofStartup()) |
| 989 | goto fail; |
| 990 | if (!dvmClassStartup()) |
| 991 | goto fail; |
| 992 | if (!dvmThreadObjStartup()) |
| 993 | goto fail; |
| 994 | if (!dvmExceptionStartup()) |
| 995 | goto fail; |
| 996 | if (!dvmStringInternStartup()) |
| 997 | goto fail; |
| 998 | if (!dvmNativeStartup()) |
| 999 | goto fail; |
| 1000 | if (!dvmInternalNativeStartup()) |
| 1001 | goto fail; |
| 1002 | if (!dvmJniStartup()) |
| 1003 | goto fail; |
| 1004 | if (!dvmReflectStartup()) |
| 1005 | goto fail; |
| 1006 | #ifdef WITH_PROFILER |
| 1007 | if (!dvmProfilingStartup()) |
| 1008 | goto fail; |
| 1009 | #endif |
| 1010 | |
| 1011 | /* make sure we got these [can this go away?] */ |
| 1012 | assert(gDvm.classJavaLangClass != NULL); |
| 1013 | assert(gDvm.classJavaLangObject != NULL); |
| 1014 | //assert(gDvm.classJavaLangString != NULL); |
| 1015 | assert(gDvm.classJavaLangThread != NULL); |
| 1016 | assert(gDvm.classJavaLangVMThread != NULL); |
| 1017 | assert(gDvm.classJavaLangThreadGroup != NULL); |
| 1018 | |
| 1019 | /* |
| 1020 | * Make sure these exist. If they don't, we can return a failure out |
| 1021 | * of main and nip the whole thing in the bud. |
| 1022 | */ |
| 1023 | static const char* earlyClasses[] = { |
| 1024 | "Ljava/lang/InternalError;", |
| 1025 | "Ljava/lang/StackOverflowError;", |
| 1026 | "Ljava/lang/UnsatisfiedLinkError;", |
| 1027 | "Ljava/lang/NoClassDefFoundError;", |
| 1028 | NULL |
| 1029 | }; |
| 1030 | const char** pClassName; |
| 1031 | for (pClassName = earlyClasses; *pClassName != NULL; pClassName++) { |
| 1032 | if (dvmFindSystemClassNoInit(*pClassName) == NULL) |
| 1033 | goto fail; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * Miscellaneous class library validation. |
| 1038 | */ |
| 1039 | if (!dvmValidateBoxClasses()) |
| 1040 | goto fail; |
| 1041 | |
| 1042 | /* |
| 1043 | * Do the last bits of Thread struct initialization we need to allow |
| 1044 | * JNI calls to work. |
| 1045 | */ |
| 1046 | if (!dvmPrepMainForJni(pEnv)) |
| 1047 | goto fail; |
| 1048 | |
| 1049 | /* |
| 1050 | * Register the system native methods, which are registered through JNI. |
| 1051 | */ |
| 1052 | if (!registerSystemNatives(pEnv)) |
| 1053 | goto fail; |
| 1054 | |
| 1055 | /* |
| 1056 | * Do some "late" initialization for the memory allocator. This may |
| 1057 | * allocate storage and initialize classes. |
| 1058 | */ |
| 1059 | if (!dvmGcLateInit()) |
| 1060 | goto fail; |
| 1061 | |
| 1062 | /* |
| 1063 | * At this point, the VM is in a pretty good state. Finish prep on |
| 1064 | * the main thread (specifically, create a java.lang.Thread object to go |
| 1065 | * along with our Thread struct). Note we will probably be executing |
| 1066 | * some interpreted class initializer code in here. |
| 1067 | */ |
| 1068 | if (!dvmPrepMainThread()) |
| 1069 | goto fail; |
| 1070 | |
| 1071 | /* general debugging setup */ |
| 1072 | if (!dvmDebuggerStartup()) |
| 1073 | goto fail; |
| 1074 | |
| 1075 | /* |
| 1076 | * Init for either zygote mode or non-zygote mode. The key difference |
| 1077 | * is that we don't start any additional threads in Zygote mode. |
| 1078 | */ |
| 1079 | if (gDvm.zygote) { |
| 1080 | if (!dvmInitZygote()) |
| 1081 | goto fail; |
| 1082 | } else { |
| 1083 | if (!dvmInitAfterZygote()) |
| 1084 | goto fail; |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | #ifndef NDEBUG |
| 1089 | dvmTestHash(); |
| 1090 | #endif |
| 1091 | |
| 1092 | assert(!dvmCheckException(dvmThreadSelf())); |
| 1093 | gDvm.initExceptionCount = 0; |
| 1094 | |
| 1095 | return 0; |
| 1096 | |
| 1097 | fail: |
| 1098 | dvmShutdown(); |
| 1099 | return 1; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Register java.* natives from our class libraries. We need to do |
| 1104 | * this after we're ready for JNI registration calls, but before we |
| 1105 | * do any class initialization. |
| 1106 | * |
| 1107 | * If we get this wrong, we will blow up in the ThreadGroup class init if |
| 1108 | * interpreted code makes any reference to System. It will likely do this |
| 1109 | * since it wants to do some java.io.File setup (e.g. for static in/out/err). |
| 1110 | * |
| 1111 | * We need to have gDvm.initializing raised here so that JNI FindClass |
| 1112 | * won't try to use the system/application class loader. |
| 1113 | */ |
| 1114 | static bool registerSystemNatives(JNIEnv* pEnv) |
| 1115 | { |
| 1116 | Thread* self; |
| 1117 | |
| 1118 | /* main thread is always first in list */ |
| 1119 | self = gDvm.threadList; |
| 1120 | |
| 1121 | /* must set this before allowing JNI-based method registration */ |
| 1122 | self->status = THREAD_NATIVE; |
| 1123 | |
| 1124 | if (jniRegisterSystemMethods(pEnv) < 0) { |
| 1125 | LOGW("jniRegisterSystemMethods failed\n"); |
| 1126 | return false; |
| 1127 | } |
| 1128 | |
| 1129 | /* back to run mode */ |
| 1130 | self->status = THREAD_RUNNING; |
| 1131 | |
| 1132 | return true; |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | /* |
| 1137 | * Do zygote-mode-only initialization. |
| 1138 | */ |
| 1139 | static bool dvmInitZygote(void) |
| 1140 | { |
| 1141 | /* zygote goes into its own process group */ |
| 1142 | setpgid(0,0); |
| 1143 | |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * Do non-zygote-mode initialization. This is done during VM init for |
| 1149 | * standard startup, or after a "zygote fork" when creating a new process. |
| 1150 | */ |
| 1151 | bool dvmInitAfterZygote(void) |
| 1152 | { |
| 1153 | u8 startHeap, startQuit, startJdwp; |
| 1154 | u8 endHeap, endQuit, endJdwp; |
| 1155 | |
| 1156 | startHeap = dvmGetRelativeTimeUsec(); |
| 1157 | |
| 1158 | /* |
| 1159 | * Post-zygote heap initialization, including starting |
| 1160 | * the HeapWorker thread. |
| 1161 | */ |
| 1162 | if (!dvmGcStartupAfterZygote()) |
| 1163 | return false; |
| 1164 | |
| 1165 | endHeap = dvmGetRelativeTimeUsec(); |
| 1166 | startQuit = dvmGetRelativeTimeUsec(); |
| 1167 | |
| 1168 | /* start signal catcher thread that dumps stacks on SIGQUIT */ |
| 1169 | if (!gDvm.reduceSignals && !gDvm.noQuitHandler) { |
| 1170 | if (!dvmSignalCatcherStartup()) |
| 1171 | return false; |
| 1172 | } |
| 1173 | |
| 1174 | /* start stdout/stderr copier, if requested */ |
| 1175 | if (gDvm.logStdio) { |
| 1176 | if (!dvmStdioConverterStartup()) |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | endQuit = dvmGetRelativeTimeUsec(); |
| 1181 | startJdwp = dvmGetRelativeTimeUsec(); |
| 1182 | |
| 1183 | /* |
| 1184 | * Start JDWP thread. If the command-line debugger flags specified |
| 1185 | * "suspend=y", this will pause the VM. We probably want this to |
| 1186 | * come last. |
| 1187 | */ |
| 1188 | if (!dvmInitJDWP()) { |
| 1189 | LOGD("JDWP init failed; continuing anyway\n"); |
| 1190 | } |
| 1191 | |
| 1192 | endJdwp = dvmGetRelativeTimeUsec(); |
| 1193 | |
| 1194 | LOGV("thread-start heap=%d quit=%d jdwp=%d total=%d usec\n", |
| 1195 | (int)(endHeap-startHeap), (int)(endQuit-startQuit), |
| 1196 | (int)(endJdwp-startJdwp), (int)(endJdwp-startHeap)); |
| 1197 | |
| 1198 | return true; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
| 1202 | * Prepare for a connection to a JDWP-compliant debugger. |
| 1203 | * |
| 1204 | * Note this needs to happen fairly late in the startup process, because |
| 1205 | * we need to have all of the java.* native methods registered (which in |
| 1206 | * turn requires JNI to be fully prepped). |
| 1207 | * |
| 1208 | * There are several ways to initialize: |
| 1209 | * server=n |
| 1210 | * We immediately try to connect to host:port. Bail on failure. On |
| 1211 | * success, send VM_START (suspending the VM if "suspend=y"). |
| 1212 | * server=y suspend=n |
| 1213 | * Passively listen for a debugger to connect. Return immediately. |
| 1214 | * server=y suspend=y |
| 1215 | * Wait until debugger connects. Send VM_START ASAP, suspending the |
| 1216 | * VM after the message is sent. |
| 1217 | * |
| 1218 | * This gets more complicated with a nonzero value for "timeout". |
| 1219 | */ |
| 1220 | static bool dvmInitJDWP(void) |
| 1221 | { |
| 1222 | assert(!gDvm.zygote); |
| 1223 | |
| 1224 | #ifndef WITH_DEBUGGER |
| 1225 | LOGI("Debugger support not compiled into VM\n"); |
| 1226 | return false; |
| 1227 | #endif |
| 1228 | |
| 1229 | /* |
| 1230 | * Init JDWP if the debugger is enabled. This may connect out to a |
| 1231 | * debugger, passively listen for a debugger, or block waiting for a |
| 1232 | * debugger. |
| 1233 | */ |
| 1234 | if (gDvm.jdwpAllowed && gDvm.jdwpConfigured) { |
| 1235 | JdwpStartupParams params; |
| 1236 | |
| 1237 | if (gDvm.jdwpHost != NULL) { |
| 1238 | if (strlen(gDvm.jdwpHost) >= sizeof(params.host)-1) { |
| 1239 | LOGE("ERROR: hostname too long: '%s'\n", gDvm.jdwpHost); |
| 1240 | return false; |
| 1241 | } |
| 1242 | strcpy(params.host, gDvm.jdwpHost); |
| 1243 | } else { |
| 1244 | params.host[0] = '\0'; |
| 1245 | } |
| 1246 | params.transport = gDvm.jdwpTransport; |
| 1247 | params.server = gDvm.jdwpServer; |
| 1248 | params.suspend = gDvm.jdwpSuspend; |
| 1249 | params.port = gDvm.jdwpPort; |
| 1250 | |
| 1251 | gDvm.jdwpState = dvmJdwpStartup(¶ms); |
| 1252 | if (gDvm.jdwpState == NULL) { |
| 1253 | LOGW("WARNING: debugger thread failed to initialize\n"); |
| 1254 | /* TODO: ignore? fail? need to mimic "expected" behavior */ |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * If a debugger has already attached, send the "welcome" message. This |
| 1260 | * may cause us to suspend all threads. |
| 1261 | */ |
| 1262 | if (dvmJdwpIsActive(gDvm.jdwpState)) { |
| 1263 | //dvmChangeStatus(NULL, THREAD_RUNNING); |
| 1264 | if (!dvmJdwpPostVMStart(gDvm.jdwpState, gDvm.jdwpSuspend)) { |
| 1265 | LOGW("WARNING: failed to post 'start' message to debugger\n"); |
| 1266 | /* keep going */ |
| 1267 | } |
| 1268 | //dvmChangeStatus(NULL, THREAD_NATIVE); |
| 1269 | } |
| 1270 | |
| 1271 | return true; |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * An alternative to JNI_CreateJavaVM/dvmStartup that does the first bit |
| 1276 | * of initialization and then returns with "initializing" still set. (Used |
| 1277 | * by DexOpt command-line utility.) |
| 1278 | * |
| 1279 | * Attempting to use JNI or internal natives will fail. It's best if |
| 1280 | * no bytecode gets executed, which means no <clinit>, which means no |
| 1281 | * exception-throwing. We check the "initializing" flag anyway when |
| 1282 | * throwing an exception, so we can insert some code that avoids chucking |
| 1283 | * an exception when we're optimizing stuff. |
| 1284 | * |
| 1285 | * Returns 0 on success. |
| 1286 | */ |
| 1287 | int dvmPrepForDexOpt(const char* bootClassPath, DexOptimizerMode dexOptMode, |
| 1288 | DexClassVerifyMode verifyMode, int dexoptFlags) |
| 1289 | { |
| 1290 | gDvm.initializing = true; |
| 1291 | gDvm.optimizing = true; |
| 1292 | |
| 1293 | /* configure signal handling */ |
| 1294 | blockSignals(); |
| 1295 | |
| 1296 | /* set some defaults */ |
| 1297 | setCommandLineDefaults(); |
| 1298 | free(gDvm.bootClassPathStr); |
| 1299 | gDvm.bootClassPathStr = strdup(bootClassPath); |
| 1300 | |
| 1301 | /* set opt/verify modes */ |
| 1302 | gDvm.dexOptMode = dexOptMode; |
| 1303 | gDvm.classVerifyMode = verifyMode; |
| 1304 | gDvm.generateRegisterMaps = (dexoptFlags & DEXOPT_GEN_REGISTER_MAPS) != 0; |
| 1305 | |
| 1306 | /* |
| 1307 | * Initialize the heap, some basic thread control mutexes, and |
| 1308 | * get the bootclasspath prepped. |
| 1309 | * |
| 1310 | * We can't load any classes yet because we may not yet have a source |
| 1311 | * for things like java.lang.Object and java.lang.Class. |
| 1312 | */ |
| 1313 | if (!dvmGcStartup()) |
| 1314 | goto fail; |
| 1315 | if (!dvmThreadStartup()) |
| 1316 | goto fail; |
| 1317 | if (!dvmInlineNativeStartup()) |
| 1318 | goto fail; |
| 1319 | if (!dvmVerificationStartup()) |
| 1320 | goto fail; |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame^] | 1321 | if (!dvmRegisterMapStartup()) |
| 1322 | goto fail; |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1323 | if (!dvmInstanceofStartup()) |
| 1324 | goto fail; |
| 1325 | if (!dvmClassStartup()) |
| 1326 | goto fail; |
| 1327 | |
| 1328 | /* |
| 1329 | * We leave gDvm.initializing set to "true" so that, if we're not |
| 1330 | * able to process the "core" classes, we don't go into a death-spin |
| 1331 | * trying to throw a "class not found" exception. |
| 1332 | */ |
| 1333 | |
| 1334 | return 0; |
| 1335 | |
| 1336 | fail: |
| 1337 | dvmShutdown(); |
| 1338 | return 1; |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | /* |
| 1343 | * All threads have stopped. Finish the shutdown procedure. |
| 1344 | * |
| 1345 | * We can also be called if startup fails partway through, so be prepared |
| 1346 | * to deal with partially initialized data. |
| 1347 | * |
| 1348 | * Free any storage allocated in gGlobals. |
| 1349 | * |
| 1350 | * We can't dlclose() shared libs we've loaded, because it's possible a |
| 1351 | * thread not associated with the VM is running code in one. |
| 1352 | * |
| 1353 | * This is called from the JNI DestroyJavaVM function, which can be |
| 1354 | * called from any thread. (In practice, this will usually run in the |
| 1355 | * same thread that started the VM, a/k/a the main thread, but we don't |
| 1356 | * want to assume that.) |
| 1357 | */ |
| 1358 | void dvmShutdown(void) |
| 1359 | { |
| 1360 | LOGV("VM shutting down\n"); |
| 1361 | |
| 1362 | if (CALC_CACHE_STATS) |
| 1363 | dvmDumpAtomicCacheStats(gDvm.instanceofCache); |
| 1364 | |
| 1365 | /* |
| 1366 | * Stop our internal threads. |
| 1367 | */ |
| 1368 | dvmHeapWorkerShutdown(); |
| 1369 | |
| 1370 | if (gDvm.jdwpState != NULL) |
| 1371 | dvmJdwpShutdown(gDvm.jdwpState); |
| 1372 | free(gDvm.jdwpHost); |
| 1373 | gDvm.jdwpHost = NULL; |
| 1374 | free(gDvm.stackTraceFile); |
| 1375 | gDvm.stackTraceFile = NULL; |
| 1376 | |
| 1377 | /* tell signal catcher to shut down if it was started */ |
| 1378 | dvmSignalCatcherShutdown(); |
| 1379 | |
| 1380 | /* shut down stdout/stderr conversion */ |
| 1381 | dvmStdioConverterShutdown(); |
| 1382 | |
| 1383 | /* |
| 1384 | * Kill any daemon threads that still exist. Actively-running threads |
| 1385 | * are likely to crash the process if they continue to execute while |
| 1386 | * the VM shuts down. |
| 1387 | */ |
| 1388 | dvmSlayDaemons(); |
| 1389 | |
| 1390 | LOGD("VM cleaning up\n"); |
| 1391 | |
| 1392 | dvmDebuggerShutdown(); |
| 1393 | dvmReflectShutdown(); |
| 1394 | #ifdef WITH_PROFILER |
| 1395 | dvmProfilingShutdown(); |
| 1396 | #endif |
| 1397 | dvmJniShutdown(); |
| 1398 | dvmStringInternShutdown(); |
| 1399 | dvmExceptionShutdown(); |
| 1400 | dvmThreadShutdown(); |
| 1401 | dvmClassShutdown(); |
| 1402 | dvmVerificationShutdown(); |
| The Android Open Source Project | 9940988 | 2009-03-18 22:20:24 -0700 | [diff] [blame^] | 1403 | dvmRegisterMapShutdown(); |
| The Android Open Source Project | f6c3871 | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1404 | dvmInstanceofShutdown(); |
| 1405 | dvmInlineNativeShutdown(); |
| 1406 | dvmGcShutdown(); |
| 1407 | dvmAllocTrackerShutdown(); |
| 1408 | dvmPropertiesShutdown(); |
| 1409 | |
| 1410 | /* these must happen AFTER dvmClassShutdown has walked through class data */ |
| 1411 | dvmNativeShutdown(); |
| 1412 | dvmInternalNativeShutdown(); |
| 1413 | |
| 1414 | free(gDvm.bootClassPathStr); |
| 1415 | free(gDvm.classPathStr); |
| 1416 | |
| 1417 | freeAssertionCtrl(); |
| 1418 | |
| 1419 | /* |
| 1420 | * We want valgrind to report anything we forget to free as "definitely |
| 1421 | * lost". If there's a pointer in the global chunk, it would be reported |
| 1422 | * as "still reachable". Erasing the memory fixes this. |
| 1423 | * |
| 1424 | * This must be erased to zero if we want to restart the VM within this |
| 1425 | * process. |
| 1426 | */ |
| 1427 | memset(&gDvm, 0xcd, sizeof(gDvm)); |
| 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | /* |
| 1432 | * fprintf() wrapper that calls through the JNI-specified vfprintf hook if |
| 1433 | * one was specified. |
| 1434 | */ |
| 1435 | int dvmFprintf(FILE* fp, const char* format, ...) |
| 1436 | { |
| 1437 | va_list args; |
| 1438 | int result; |
| 1439 | |
| 1440 | va_start(args, format); |
| 1441 | if (gDvm.vfprintfHook != NULL) |
| 1442 | result = (*gDvm.vfprintfHook)(fp, format, args); |
| 1443 | else |
| 1444 | result = vfprintf(fp, format, args); |
| 1445 | va_end(args); |
| 1446 | |
| 1447 | return result; |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Abort the VM. We get here on fatal errors. Try very hard not to use |
| 1452 | * this; whenever possible, return an error to somebody responsible. |
| 1453 | */ |
| 1454 | void dvmAbort(void) |
| 1455 | { |
| 1456 | LOGE("VM aborting\n"); |
| 1457 | |
| 1458 | fflush(NULL); // flush all open file buffers |
| 1459 | |
| 1460 | /* JNI-supplied abort hook gets right of first refusal */ |
| 1461 | if (gDvm.abortHook != NULL) |
| 1462 | (*gDvm.abortHook)(); |
| 1463 | |
| 1464 | /* |
| 1465 | * If we call abort(), all threads in the process receives a SIBABRT. |
| 1466 | * debuggerd dumps the stack trace of the main thread, whether or not |
| 1467 | * that was the thread that failed. |
| 1468 | * |
| 1469 | * By stuffing a value into a bogus address, we cause a segmentation |
| 1470 | * fault in the current thread, and get a useful log from debuggerd. |
| 1471 | * We can also trivially tell the difference between a VM crash and |
| 1472 | * a deliberate abort by looking at the fault address. |
| 1473 | */ |
| 1474 | *((char*)0xdeadd00d) = 38; |
| 1475 | abort(); |
| 1476 | |
| 1477 | /* notreached */ |
| 1478 | } |