• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define __STDC_LIMIT_MACROS
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <signal.h>
24 #include <limits.h>
25 #include <ctype.h>
26 #include <sys/mount.h>
27 #include <sys/wait.h>
28 #include <linux/fs.h>
29 #include <cutils/fs.h>
30 #include <unistd.h>
31 
32 #include "Dalvik.h"
33 #include "test/Test.h"
34 #include "mterp/Mterp.h"
35 #include "Hash.h"
36 
37 #if defined(WITH_JIT)
38 #include "compiler/codegen/Optimizer.h"
39 #endif
40 
41 #define kMinHeapStartSize   (1*1024*1024)
42 #define kMinHeapSize        (2*1024*1024)
43 #define kMaxHeapSize        (1*1024*1024*1024)
44 
45 /*
46  * Register VM-agnostic native methods for system classes.
47  */
48 extern int jniRegisterSystemMethods(JNIEnv* env);
49 
50 /* fwd */
51 static bool registerSystemNatives(JNIEnv* pEnv);
52 static bool initJdwp();
53 static bool initZygote();
54 
55 
56 /* global state */
57 struct DvmGlobals gDvm;
58 struct DvmJniGlobals gDvmJni;
59 
60 /* JIT-specific global state */
61 #if defined(WITH_JIT)
62 struct DvmJitGlobals gDvmJit;
63 
64 #if defined(WITH_JIT_TUNING)
65 /*
66  * Track the number of hits in the inline cache for predicted chaining.
67  * Use an ugly global variable here since it is accessed in assembly code.
68  */
69 int gDvmICHitCount;
70 #endif
71 
72 #endif
73 
74 /*
75  * Show usage.
76  *
77  * We follow the tradition of unhyphenated compound words.
78  */
usage(const char * progName)79 static void usage(const char* progName)
80 {
81     dvmFprintf(stderr, "%s: [options] class [argument ...]\n", progName);
82     dvmFprintf(stderr, "%s: [options] -jar file.jar [argument ...]\n",progName);
83     dvmFprintf(stderr, "\n");
84     dvmFprintf(stderr, "The following standard options are recognized:\n");
85     dvmFprintf(stderr, "  -classpath classpath\n");
86     dvmFprintf(stderr, "  -Dproperty=value\n");
87     dvmFprintf(stderr, "  -verbose:tag  ('gc', 'jni', or 'class')\n");
88     dvmFprintf(stderr, "  -ea[:<package name>... |:<class name>]\n");
89     dvmFprintf(stderr, "  -da[:<package name>... |:<class name>]\n");
90     dvmFprintf(stderr, "   (-enableassertions, -disableassertions)\n");
91     dvmFprintf(stderr, "  -esa\n");
92     dvmFprintf(stderr, "  -dsa\n");
93     dvmFprintf(stderr,
94                 "   (-enablesystemassertions, -disablesystemassertions)\n");
95     dvmFprintf(stderr, "  -showversion\n");
96     dvmFprintf(stderr, "  -help\n");
97     dvmFprintf(stderr, "\n");
98     dvmFprintf(stderr, "The following extended options are recognized:\n");
99     dvmFprintf(stderr, "  -Xrunjdwp:<options>\n");
100     dvmFprintf(stderr, "  -Xbootclasspath:bootclasspath\n");
101     dvmFprintf(stderr, "  -Xcheck:tag  (e.g. 'jni')\n");
102     dvmFprintf(stderr, "  -XmsN  (min heap, must be multiple of 1K, >= 1MB)\n");
103     dvmFprintf(stderr, "  -XmxN  (max heap, must be multiple of 1K, >= 2MB)\n");
104     dvmFprintf(stderr, "  -XssN  (stack size, >= %dKB, <= %dKB)\n",
105         kMinStackSize / 1024, kMaxStackSize / 1024);
106     dvmFprintf(stderr, "  -Xverify:{none,remote,all}\n");
107     dvmFprintf(stderr, "  -Xrs\n");
108 #if defined(WITH_JIT)
109     dvmFprintf(stderr,
110                 "  -Xint  (extended to accept ':portable', ':fast' and ':jit')\n");
111 #else
112     dvmFprintf(stderr,
113                 "  -Xint  (extended to accept ':portable' and ':fast')\n");
114 #endif
115     dvmFprintf(stderr, "\n");
116     dvmFprintf(stderr, "These are unique to Dalvik:\n");
117     dvmFprintf(stderr, "  -Xzygote\n");
118     dvmFprintf(stderr, "  -Xdexopt:{none,verified,all,full}\n");
119     dvmFprintf(stderr, "  -Xnoquithandler\n");
120     dvmFprintf(stderr,
121                 "  -Xjnigreflimit:N  (must be multiple of 100, >= 200)\n");
122     dvmFprintf(stderr, "  -Xjniopts:{warnonly,forcecopy}\n");
123     dvmFprintf(stderr, "  -Xjnitrace:substring (eg NativeClass or nativeMethod)\n");
124     dvmFprintf(stderr, "  -Xstacktracefile:<filename>\n");
125     dvmFprintf(stderr, "  -Xgc:[no]precise\n");
126     dvmFprintf(stderr, "  -Xgc:[no]preverify\n");
127     dvmFprintf(stderr, "  -Xgc:[no]postverify\n");
128     dvmFprintf(stderr, "  -Xgc:[no]concurrent\n");
129     dvmFprintf(stderr, "  -Xgc:[no]verifycardtable\n");
130     dvmFprintf(stderr, "  -XX:+DisableExplicitGC\n");
131     dvmFprintf(stderr, "  -X[no]genregmap\n");
132     dvmFprintf(stderr, "  -Xverifyopt:[no]checkmon\n");
133     dvmFprintf(stderr, "  -Xcheckdexsum\n");
134 #if defined(WITH_JIT)
135     dvmFprintf(stderr, "  -Xincludeselectedop\n");
136     dvmFprintf(stderr, "  -Xjitop:hexopvalue[-endvalue]"
137                        "[,hexopvalue[-endvalue]]*\n");
138     dvmFprintf(stderr, "  -Xincludeselectedmethod\n");
139     dvmFprintf(stderr, "  -Xjitthreshold:decimalvalue\n");
140     dvmFprintf(stderr, "  -Xjitblocking\n");
141     dvmFprintf(stderr, "  -Xjitmethod:signature[,signature]* "
142                        "(eg Ljava/lang/String\\;replace)\n");
143     dvmFprintf(stderr, "  -Xjitclass:classname[,classname]*\n");
144     dvmFprintf(stderr, "  -Xjitoffset:offset[,offset]\n");
145     dvmFprintf(stderr, "  -Xjitconfig:filename\n");
146     dvmFprintf(stderr, "  -Xjitcheckcg\n");
147     dvmFprintf(stderr, "  -Xjitverbose\n");
148     dvmFprintf(stderr, "  -Xjitprofile\n");
149     dvmFprintf(stderr, "  -Xjitdisableopt\n");
150     dvmFprintf(stderr, "  -Xjitsuspendpoll\n");
151 #endif
152     dvmFprintf(stderr, "\n");
153     dvmFprintf(stderr, "Configured with:"
154         " debugger"
155         " profiler"
156         " hprof"
157 #ifdef WITH_TRACKREF_CHECKS
158         " trackref_checks"
159 #endif
160 #ifdef WITH_INSTR_CHECKS
161         " instr_checks"
162 #endif
163 #ifdef WITH_EXTRA_OBJECT_VALIDATION
164         " extra_object_validation"
165 #endif
166 #ifdef WITH_EXTRA_GC_CHECKS
167         " extra_gc_checks"
168 #endif
169 #if !defined(NDEBUG) && defined(WITH_DALVIK_ASSERT)
170         " dalvik_assert"
171 #endif
172 #ifdef WITH_JNI_STACK_CHECK
173         " jni_stack_check"
174 #endif
175 #ifdef EASY_GDB
176         " easy_gdb"
177 #endif
178 #ifdef CHECK_MUTEX
179         " check_mutex"
180 #endif
181 #if defined(WITH_JIT)
182         " jit(" ARCH_VARIANT ")"
183 #endif
184 #if defined(WITH_SELF_VERIFICATION)
185         " self_verification"
186 #endif
187 #if ANDROID_SMP != 0
188         " smp"
189 #endif
190     );
191 #ifdef DVM_SHOW_EXCEPTION
192     dvmFprintf(stderr, " show_exception=%d", DVM_SHOW_EXCEPTION);
193 #endif
194     dvmFprintf(stderr, "\n\n");
195 }
196 
197 /*
198  * Show helpful information on JDWP options.
199  */
showJdwpHelp()200 static void showJdwpHelp()
201 {
202     dvmFprintf(stderr,
203         "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n");
204     dvmFprintf(stderr,
205         "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
206 }
207 
208 /*
209  * Show version and copyright info.
210  */
showVersion()211 static void showVersion()
212 {
213     dvmFprintf(stdout, "DalvikVM version %d.%d.%d\n",
214         DALVIK_MAJOR_VERSION, DALVIK_MINOR_VERSION, DALVIK_BUG_VERSION);
215     dvmFprintf(stdout,
216         "Copyright (C) 2007 The Android Open Source Project\n\n"
217         "This software is built from source code licensed under the "
218         "Apache License,\n"
219         "Version 2.0 (the \"License\"). You may obtain a copy of the "
220         "License at\n\n"
221         "     http://www.apache.org/licenses/LICENSE-2.0\n\n"
222         "See the associated NOTICE file for this software for further "
223         "details.\n");
224 }
225 
226 /*
227  * Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
228  * memory sizes.  [kK] indicates kilobytes, [mM] megabytes, and
229  * [gG] gigabytes.
230  *
231  * "s" should point just past the "-Xm?" part of the string.
232  * "min" specifies the lowest acceptable value described by "s".
233  * "div" specifies a divisor, e.g. 1024 if the value must be a multiple
234  * of 1024.
235  *
236  * The spec says the -Xmx and -Xms options must be multiples of 1024.  It
237  * doesn't say anything about -Xss.
238  *
239  * Returns 0 (a useless size) if "s" is malformed or specifies a low or
240  * non-evenly-divisible value.
241  */
parseMemOption(const char * s,size_t div)242 static size_t parseMemOption(const char* s, size_t div)
243 {
244     /* strtoul accepts a leading [+-], which we don't want,
245      * so make sure our string starts with a decimal digit.
246      */
247     if (isdigit(*s)) {
248         const char* s2;
249         size_t val;
250 
251         val = strtoul(s, (char* *)&s2, 10);
252         if (s2 != s) {
253             /* s2 should be pointing just after the number.
254              * If this is the end of the string, the user
255              * has specified a number of bytes.  Otherwise,
256              * there should be exactly one more character
257              * that specifies a multiplier.
258              */
259             if (*s2 != '\0') {
260                 char c;
261 
262                 /* The remainder of the string is either a single multiplier
263                  * character, or nothing to indicate that the value is in
264                  * bytes.
265                  */
266                 c = *s2++;
267                 if (*s2 == '\0') {
268                     size_t mul;
269 
270                     if (c == '\0') {
271                         mul = 1;
272                     } else if (c == 'k' || c == 'K') {
273                         mul = 1024;
274                     } else if (c == 'm' || c == 'M') {
275                         mul = 1024 * 1024;
276                     } else if (c == 'g' || c == 'G') {
277                         mul = 1024 * 1024 * 1024;
278                     } else {
279                         /* Unknown multiplier character.
280                          */
281                         return 0;
282                     }
283 
284                     if (val <= SIZE_MAX / mul) {
285                         val *= mul;
286                     } else {
287                         /* Clamp to a multiple of 1024.
288                          */
289                         val = SIZE_MAX & ~(1024-1);
290                     }
291                 } else {
292                     /* There's more than one character after the
293                      * numeric part.
294                      */
295                     return 0;
296                 }
297             }
298 
299             /* The man page says that a -Xm value must be
300              * a multiple of 1024.
301              */
302             if (val % div == 0) {
303                 return val;
304             }
305         }
306     }
307 
308     return 0;
309 }
310 
311 /*
312  * Handle one of the JDWP name/value pairs.
313  *
314  * JDWP options are:
315  *  help: if specified, show help message and bail
316  *  transport: may be dt_socket or dt_shmem
317  *  address: for dt_socket, "host:port", or just "port" when listening
318  *  server: if "y", wait for debugger to attach; if "n", attach to debugger
319  *  timeout: how long to wait for debugger to connect / listen
320  *
321  * Useful with server=n (these aren't supported yet):
322  *  onthrow=<exception-name>: connect to debugger when exception thrown
323  *  onuncaught=y|n: connect to debugger when uncaught exception thrown
324  *  launch=<command-line>: launch the debugger itself
325  *
326  * The "transport" option is required, as is "address" if server=n.
327  */
handleJdwpOption(const char * name,const char * value)328 static bool handleJdwpOption(const char* name, const char* value)
329 {
330     if (strcmp(name, "transport") == 0) {
331         if (strcmp(value, "dt_socket") == 0) {
332             gDvm.jdwpTransport = kJdwpTransportSocket;
333         } else if (strcmp(value, "dt_android_adb") == 0) {
334             gDvm.jdwpTransport = kJdwpTransportAndroidAdb;
335         } else {
336             ALOGE("JDWP transport '%s' not supported", value);
337             return false;
338         }
339     } else if (strcmp(name, "server") == 0) {
340         if (*value == 'n')
341             gDvm.jdwpServer = false;
342         else if (*value == 'y')
343             gDvm.jdwpServer = true;
344         else {
345             ALOGE("JDWP option 'server' must be 'y' or 'n'");
346             return false;
347         }
348     } else if (strcmp(name, "suspend") == 0) {
349         if (*value == 'n')
350             gDvm.jdwpSuspend = false;
351         else if (*value == 'y')
352             gDvm.jdwpSuspend = true;
353         else {
354             ALOGE("JDWP option 'suspend' must be 'y' or 'n'");
355             return false;
356         }
357     } else if (strcmp(name, "address") == 0) {
358         /* this is either <port> or <host>:<port> */
359         const char* colon = strchr(value, ':');
360         char* end;
361         long port;
362 
363         if (colon != NULL) {
364             free(gDvm.jdwpHost);
365             gDvm.jdwpHost = (char*) malloc(colon - value +1);
366             strncpy(gDvm.jdwpHost, value, colon - value +1);
367             gDvm.jdwpHost[colon-value] = '\0';
368             value = colon + 1;
369         }
370         if (*value == '\0') {
371             ALOGE("JDWP address missing port");
372             return false;
373         }
374         port = strtol(value, &end, 10);
375         if (*end != '\0') {
376             ALOGE("JDWP address has junk in port field '%s'", value);
377             return false;
378         }
379         gDvm.jdwpPort = port;
380     } else if (strcmp(name, "launch") == 0 ||
381                strcmp(name, "onthrow") == 0 ||
382                strcmp(name, "oncaught") == 0 ||
383                strcmp(name, "timeout") == 0)
384     {
385         /* valid but unsupported */
386         ALOGI("Ignoring JDWP option '%s'='%s'", name, value);
387     } else {
388         ALOGI("Ignoring unrecognized JDWP option '%s'='%s'", name, value);
389     }
390 
391     return true;
392 }
393 
394 /*
395  * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
396  * "transport=dt_socket,address=8000,server=y,suspend=n"
397  */
parseJdwpOptions(const char * str)398 static bool parseJdwpOptions(const char* str)
399 {
400     char* mangle = strdup(str);
401     char* name = mangle;
402     bool result = false;
403 
404     /*
405      * Process all of the name=value pairs.
406      */
407     while (true) {
408         char* value;
409         char* comma;
410 
411         value = strchr(name, '=');
412         if (value == NULL) {
413             ALOGE("JDWP opts: garbage at '%s'", name);
414             goto bail;
415         }
416 
417         comma = strchr(name, ',');      // use name, not value, for safety
418         if (comma != NULL) {
419             if (comma < value) {
420                 ALOGE("JDWP opts: found comma before '=' in '%s'", mangle);
421                 goto bail;
422             }
423             *comma = '\0';
424         }
425 
426         *value++ = '\0';        // stomp the '='
427 
428         if (!handleJdwpOption(name, value))
429             goto bail;
430 
431         if (comma == NULL) {
432             /* out of options */
433             break;
434         }
435         name = comma+1;
436     }
437 
438     /*
439      * Make sure the combination of arguments makes sense.
440      */
441     if (gDvm.jdwpTransport == kJdwpTransportUnknown) {
442         ALOGE("JDWP opts: must specify transport");
443         goto bail;
444     }
445     if (!gDvm.jdwpServer && (gDvm.jdwpHost == NULL || gDvm.jdwpPort == 0)) {
446         ALOGE("JDWP opts: when server=n, must specify host and port");
447         goto bail;
448     }
449     // transport mandatory
450     // outbound server address
451 
452     gDvm.jdwpConfigured = true;
453     result = true;
454 
455 bail:
456     free(mangle);
457     return result;
458 }
459 
460 /*
461  * Handle one of the four kinds of assertion arguments.
462  *
463  * "pkgOrClass" is the last part of an enable/disable line.  For a package
464  * the arg looks like "-ea:com.google.fubar...", for a class it looks
465  * like "-ea:com.google.fubar.Wahoo".  The string we get starts at the ':'.
466  *
467  * For system assertions (-esa/-dsa), "pkgOrClass" is NULL.
468  *
469  * Multiple instances of these arguments can be specified, e.g. you can
470  * enable assertions for a package and then disable them for one class in
471  * the package.
472  */
enableAssertions(const char * pkgOrClass,bool enable)473 static bool enableAssertions(const char* pkgOrClass, bool enable)
474 {
475     AssertionControl* pCtrl = &gDvm.assertionCtrl[gDvm.assertionCtrlCount++];
476     pCtrl->enable = enable;
477 
478     if (pkgOrClass == NULL) {
479         /* enable or disable for all system classes */
480         pCtrl->isPackage = false;
481         pCtrl->pkgOrClass = NULL;
482         pCtrl->pkgOrClassLen = 0;
483     } else {
484         if (*pkgOrClass == '\0') {
485             /* global enable/disable for all but system */
486             pCtrl->isPackage = false;
487             pCtrl->pkgOrClass = strdup("");
488             pCtrl->pkgOrClassLen = 0;
489         } else {
490             pCtrl->pkgOrClass = dvmDotToSlash(pkgOrClass+1);    // skip ':'
491             if (pCtrl->pkgOrClass == NULL) {
492                 /* can happen if class name includes an illegal '/' */
493                 ALOGW("Unable to process assertion arg '%s'", pkgOrClass);
494                 return false;
495             }
496 
497             int len = strlen(pCtrl->pkgOrClass);
498             if (len >= 3 && strcmp(pCtrl->pkgOrClass + len-3, "///") == 0) {
499                 /* mark as package, truncate two of the three slashes */
500                 pCtrl->isPackage = true;
501                 *(pCtrl->pkgOrClass + len-2) = '\0';
502                 pCtrl->pkgOrClassLen = len - 2;
503             } else {
504                 /* just a class */
505                 pCtrl->isPackage = false;
506                 pCtrl->pkgOrClassLen = len;
507             }
508         }
509     }
510 
511     return true;
512 }
513 
514 /*
515  * Turn assertions on when requested to do so by the Zygote.
516  *
517  * This is a bit sketchy.  We can't (easily) go back and fiddle with all
518  * of the classes that have already been initialized, so this only
519  * affects classes that have yet to be loaded.  If some or all assertions
520  * have been enabled through some other means, we don't want to mess with
521  * it here, so we do nothing.  Finally, we assume that there's room in
522  * "assertionCtrl" to hold at least one entry; this is guaranteed by the
523  * allocator.
524  *
525  * This must only be called from the main thread during zygote init.
526  */
dvmLateEnableAssertions()527 void dvmLateEnableAssertions()
528 {
529     if (gDvm.assertionCtrl == NULL) {
530         ALOGD("Not late-enabling assertions: no assertionCtrl array");
531         return;
532     } else if (gDvm.assertionCtrlCount != 0) {
533         ALOGD("Not late-enabling assertions: some asserts already configured");
534         return;
535     }
536     ALOGD("Late-enabling assertions");
537 
538     /* global enable for all but system */
539     AssertionControl* pCtrl = gDvm.assertionCtrl;
540     pCtrl->pkgOrClass = strdup("");
541     pCtrl->pkgOrClassLen = 0;
542     pCtrl->isPackage = false;
543     pCtrl->enable = true;
544     gDvm.assertionCtrlCount = 1;
545 }
546 
547 
548 /*
549  * Release memory associated with the AssertionCtrl array.
550  */
freeAssertionCtrl()551 static void freeAssertionCtrl()
552 {
553     int i;
554 
555     for (i = 0; i < gDvm.assertionCtrlCount; i++)
556         free(gDvm.assertionCtrl[i].pkgOrClass);
557     free(gDvm.assertionCtrl);
558 }
559 
560 #if defined(WITH_JIT)
561 /* Parse -Xjitop to selectively turn on/off certain opcodes for JIT */
processXjitop(const char * opt)562 static void processXjitop(const char* opt)
563 {
564     if (opt[7] == ':') {
565         const char* startPtr = &opt[8];
566         char* endPtr = NULL;
567 
568         do {
569             long startValue, endValue;
570 
571             startValue = strtol(startPtr, &endPtr, 16);
572             if (startPtr != endPtr) {
573                 /* Just in case value is out of range */
574                 startValue %= kNumPackedOpcodes;
575 
576                 if (*endPtr == '-') {
577                     endValue = strtol(endPtr+1, &endPtr, 16);
578                     endValue %= kNumPackedOpcodes;
579                 } else {
580                     endValue = startValue;
581                 }
582 
583                 for (; startValue <= endValue; startValue++) {
584                     ALOGW("Dalvik opcode %x is selected for debugging",
585                          (unsigned int) startValue);
586                     /* Mark the corresponding bit to 1 */
587                     gDvmJit.opList[startValue >> 3] |= 1 << (startValue & 0x7);
588                 }
589 
590                 if (*endPtr == 0) {
591                     break;
592                 }
593 
594                 startPtr = endPtr + 1;
595 
596                 continue;
597             } else {
598                 if (*endPtr != 0) {
599                     dvmFprintf(stderr,
600                         "Warning: Unrecognized opcode value substring "
601                         "%s\n", endPtr);
602                 }
603                 break;
604             }
605         } while (1);
606     } else {
607         int i;
608         for (i = 0; i < (kNumPackedOpcodes+7)/8; i++) {
609             gDvmJit.opList[i] = 0xff;
610         }
611         dvmFprintf(stderr, "Warning: select all opcodes\n");
612     }
613 }
614 
615 /* Parse -Xjitoffset to selectively turn on/off traces with certain offsets for JIT */
processXjitoffset(const char * opt)616 static void processXjitoffset(const char* opt) {
617     gDvmJit.num_entries_pcTable = 0;
618     char* buf = strdup(opt);
619     char* start, *end;
620     start = buf;
621     int idx = 0;
622     do {
623         end = strchr(start, ',');
624         if (end) {
625             *end = 0;
626         }
627 
628         dvmFprintf(stderr, "processXjitoffset start = %s\n", start);
629         char* tmp = strdup(start);
630         gDvmJit.pcTable[idx++] = atoi(tmp);
631         free(tmp);
632         if (idx >= COMPILER_PC_OFFSET_SIZE) {
633             dvmFprintf(stderr, "processXjitoffset: ignore entries beyond %d\n", COMPILER_PC_OFFSET_SIZE);
634             break;
635         }
636         if (end) {
637             start = end + 1;
638         } else {
639             break;
640         }
641     } while (1);
642     gDvmJit.num_entries_pcTable = idx;
643     free(buf);
644 }
645 
646 /* Parse -Xjitmethod to selectively turn on/off certain methods for JIT */
processXjitmethod(const char * opt,bool isMethod)647 static void processXjitmethod(const char* opt, bool isMethod) {
648     char* buf = strdup(opt);
649 
650     if (isMethod && gDvmJit.methodTable == NULL) {
651         gDvmJit.methodTable = dvmHashTableCreate(8, NULL);
652     }
653     if (!isMethod && gDvmJit.classTable == NULL) {
654         gDvmJit.classTable = dvmHashTableCreate(8, NULL);
655     }
656 
657     char* start = buf;
658     char* end;
659     /*
660      * Break comma-separated method signatures and enter them into the hash
661      * table individually.
662      */
663     do {
664         int hashValue;
665 
666         end = strchr(start, ',');
667         if (end) {
668             *end = 0;
669         }
670 
671         hashValue = dvmComputeUtf8Hash(start);
672         dvmHashTableLookup(isMethod ? gDvmJit.methodTable : gDvmJit.classTable,
673                            hashValue, strdup(start), (HashCompareFunc) strcmp, true);
674 
675         if (end) {
676             start = end + 1;
677         } else {
678             break;
679         }
680     } while (1);
681     free(buf);
682 }
683 
684 /* The format of jit_config.list:
685    EXCLUDE or INCLUDE
686    CLASS
687    prefix1 ...
688    METHOD
689    prefix 1 ...
690    OFFSET
691    index ... //each pair is a range, if pcOff falls into a range, JIT
692 */
processXjitconfig(const char * opt)693 static int processXjitconfig(const char* opt) {
694    FILE* fp = fopen(opt, "r");
695    if (fp == NULL) {
696        return -1;
697    }
698 
699    char fLine[500];
700    bool startClass = false, startMethod = false, startOffset = false;
701    gDvmJit.num_entries_pcTable = 0;
702    int idx = 0;
703 
704    while (fgets(fLine, 500, fp) != NULL) {
705        char* curLine = strtok(fLine, " \t\r\n");
706        /* handles keyword CLASS, METHOD, INCLUDE, EXCLUDE */
707        if (!strncmp(curLine, "CLASS", 5)) {
708            startClass = true;
709            startMethod = false;
710            startOffset = false;
711            continue;
712        }
713        if (!strncmp(curLine, "METHOD", 6)) {
714            startMethod = true;
715            startClass = false;
716            startOffset = false;
717            continue;
718        }
719        if (!strncmp(curLine, "OFFSET", 6)) {
720            startOffset = true;
721            startMethod = false;
722            startClass = false;
723            continue;
724        }
725        if (!strncmp(curLine, "EXCLUDE", 7)) {
726           gDvmJit.includeSelectedMethod = false;
727           continue;
728        }
729        if (!strncmp(curLine, "INCLUDE", 7)) {
730           gDvmJit.includeSelectedMethod = true;
731           continue;
732        }
733        if (!startMethod && !startClass && !startOffset) {
734          continue;
735        }
736 
737         int hashValue = dvmComputeUtf8Hash(curLine);
738         if (startMethod) {
739             if (gDvmJit.methodTable == NULL) {
740                 gDvmJit.methodTable = dvmHashTableCreate(8, NULL);
741             }
742             dvmHashTableLookup(gDvmJit.methodTable, hashValue,
743                                strdup(curLine),
744                                (HashCompareFunc) strcmp, true);
745         } else if (startClass) {
746             if (gDvmJit.classTable == NULL) {
747                 gDvmJit.classTable = dvmHashTableCreate(8, NULL);
748             }
749             dvmHashTableLookup(gDvmJit.classTable, hashValue,
750                                strdup(curLine),
751                                (HashCompareFunc) strcmp, true);
752         } else if (startOffset) {
753            int tmpInt = atoi(curLine);
754            gDvmJit.pcTable[idx++] = tmpInt;
755            if (idx >= COMPILER_PC_OFFSET_SIZE) {
756                printf("processXjitoffset: ignore entries beyond %d\n", COMPILER_PC_OFFSET_SIZE);
757                break;
758            }
759         }
760    }
761    gDvmJit.num_entries_pcTable = idx;
762    fclose(fp);
763    return 0;
764 }
765 #endif
766 
767 /*
768  * Process an argument vector full of options.  Unlike standard C programs,
769  * argv[0] does not contain the name of the program.
770  *
771  * If "ignoreUnrecognized" is set, we ignore options starting with "-X" or "_"
772  * that we don't recognize.  Otherwise, we return with an error as soon as
773  * we see anything we can't identify.
774  *
775  * Returns 0 on success, -1 on failure, and 1 for the special case of
776  * "-version" where we want to stop without showing an error message.
777  */
processOptions(int argc,const char * const argv[],bool ignoreUnrecognized)778 static int processOptions(int argc, const char* const argv[],
779     bool ignoreUnrecognized)
780 {
781     int i;
782 
783     ALOGV("VM options (%d):", argc);
784     for (i = 0; i < argc; i++)
785         ALOGV("  %d: '%s'", i, argv[i]);
786 
787     /*
788      * Over-allocate AssertionControl array for convenience.  If allocated,
789      * the array must be able to hold at least one entry, so that the
790      * zygote-time activation can do its business.
791      */
792     assert(gDvm.assertionCtrl == NULL);
793     if (argc > 0) {
794         gDvm.assertionCtrl =
795             (AssertionControl*) malloc(sizeof(AssertionControl) * argc);
796         if (gDvm.assertionCtrl == NULL)
797             return -1;
798         assert(gDvm.assertionCtrlCount == 0);
799     }
800 
801     for (i = 0; i < argc; i++) {
802         if (strcmp(argv[i], "-help") == 0) {
803             /* show usage and stop */
804             return -1;
805 
806         } else if (strcmp(argv[i], "-version") == 0) {
807             /* show version and stop */
808             showVersion();
809             return 1;
810         } else if (strcmp(argv[i], "-showversion") == 0) {
811             /* show version and continue */
812             showVersion();
813 
814         } else if (strcmp(argv[i], "-classpath") == 0 ||
815                    strcmp(argv[i], "-cp") == 0)
816         {
817             /* set classpath */
818             if (i == argc-1) {
819                 dvmFprintf(stderr, "Missing classpath path list\n");
820                 return -1;
821             }
822             free(gDvm.classPathStr); /* in case we have compiled-in default */
823             gDvm.classPathStr = strdup(argv[++i]);
824 
825         } else if (strncmp(argv[i], "-Xbootclasspath:",
826                 sizeof("-Xbootclasspath:")-1) == 0)
827         {
828             /* set bootclasspath */
829             const char* path = argv[i] + sizeof("-Xbootclasspath:")-1;
830 
831             if (*path == '\0') {
832                 dvmFprintf(stderr, "Missing bootclasspath path list\n");
833                 return -1;
834             }
835             free(gDvm.bootClassPathStr);
836             gDvm.bootClassPathStr = strdup(path);
837 
838         } else if (strncmp(argv[i], "-Xbootclasspath/a:",
839                 sizeof("-Xbootclasspath/a:")-1) == 0) {
840             const char* appPath = argv[i] + sizeof("-Xbootclasspath/a:")-1;
841 
842             if (*(appPath) == '\0') {
843                 dvmFprintf(stderr, "Missing appending bootclasspath path list\n");
844                 return -1;
845             }
846             char* allPath;
847 
848             if (asprintf(&allPath, "%s:%s", gDvm.bootClassPathStr, appPath) < 0) {
849                 dvmFprintf(stderr, "Can't append to bootclasspath path list\n");
850                 return -1;
851             }
852             free(gDvm.bootClassPathStr);
853             gDvm.bootClassPathStr = allPath;
854 
855         } else if (strncmp(argv[i], "-Xbootclasspath/p:",
856                 sizeof("-Xbootclasspath/p:")-1) == 0) {
857             const char* prePath = argv[i] + sizeof("-Xbootclasspath/p:")-1;
858 
859             if (*(prePath) == '\0') {
860                 dvmFprintf(stderr, "Missing prepending bootclasspath path list\n");
861                 return -1;
862             }
863             char* allPath;
864 
865             if (asprintf(&allPath, "%s:%s", prePath, gDvm.bootClassPathStr) < 0) {
866                 dvmFprintf(stderr, "Can't prepend to bootclasspath path list\n");
867                 return -1;
868             }
869             free(gDvm.bootClassPathStr);
870             gDvm.bootClassPathStr = allPath;
871 
872         } else if (strncmp(argv[i], "-D", 2) == 0) {
873             /* Properties are handled in managed code. We just check syntax. */
874             if (strchr(argv[i], '=') == NULL) {
875                 dvmFprintf(stderr, "Bad system property setting: \"%s\"\n",
876                     argv[i]);
877                 return -1;
878             }
879             gDvm.properties->push_back(argv[i] + 2);
880 
881         } else if (strcmp(argv[i], "-jar") == 0) {
882             // TODO: handle this; name of jar should be in argv[i+1]
883             dvmFprintf(stderr, "-jar not yet handled\n");
884             assert(false);
885 
886         } else if (strncmp(argv[i], "-Xms", 4) == 0) {
887             size_t val = parseMemOption(argv[i]+4, 1024);
888             if (val != 0) {
889                 if (val >= kMinHeapStartSize && val <= kMaxHeapSize) {
890                     gDvm.heapStartingSize = val;
891                 } else {
892                     dvmFprintf(stderr,
893                         "Invalid -Xms '%s', range is %dKB to %dKB\n",
894                         argv[i], kMinHeapStartSize/1024, kMaxHeapSize/1024);
895                     return -1;
896                 }
897             } else {
898                 dvmFprintf(stderr, "Invalid -Xms option '%s'\n", argv[i]);
899                 return -1;
900             }
901         } else if (strncmp(argv[i], "-Xmx", 4) == 0) {
902             size_t val = parseMemOption(argv[i]+4, 1024);
903             if (val != 0) {
904                 if (val >= kMinHeapSize && val <= kMaxHeapSize) {
905                     gDvm.heapMaximumSize = val;
906                 } else {
907                     dvmFprintf(stderr,
908                         "Invalid -Xmx '%s', range is %dKB to %dKB\n",
909                         argv[i], kMinHeapSize/1024, kMaxHeapSize/1024);
910                     return -1;
911                 }
912             } else {
913                 dvmFprintf(stderr, "Invalid -Xmx option '%s'\n", argv[i]);
914                 return -1;
915             }
916         } else if (strncmp(argv[i], "-XX:HeapGrowthLimit=", 20) == 0) {
917             size_t val = parseMemOption(argv[i] + 20, 1024);
918             if (val != 0) {
919                 gDvm.heapGrowthLimit = val;
920             } else {
921                 dvmFprintf(stderr, "Invalid -XX:HeapGrowthLimit option '%s'\n", argv[i]);
922                 return -1;
923             }
924         } else if (strncmp(argv[i], "-XX:HeapMinFree=", 16) == 0) {
925             size_t val = parseMemOption(argv[i] + 16, 1024);
926             if (val != 0) {
927                 gDvm.heapMinFree = val;
928             } else {
929                 dvmFprintf(stderr, "Invalid -XX:HeapMinFree option '%s'\n", argv[i]);
930                 return -1;
931             }
932         } else if (strncmp(argv[i], "-XX:HeapMaxFree=", 16) == 0) {
933             size_t val = parseMemOption(argv[i] + 16, 1024);
934             if (val != 0) {
935                 gDvm.heapMaxFree = val;
936             } else {
937                 dvmFprintf(stderr, "Invalid -XX:HeapMaxFree option '%s'\n", argv[i]);
938                 return -1;
939             }
940         } else if (strncmp(argv[i], "-XX:HeapTargetUtilization=", 26) == 0) {
941             const char* start = argv[i] + 26;
942             const char* end = start;
943             double val = strtod(start, const_cast<char**>(&end));
944             // Ensure that we have a value, there was no cruft after it and it
945             // satisfies a sensible range.
946             bool sane_val = (start != end) && (end[0] == '\0') &&
947                 (val >= 0.1) && (val <= 0.9);
948             if (sane_val) {
949                 gDvm.heapTargetUtilization = val;
950             } else {
951                 dvmFprintf(stderr, "Invalid -XX:HeapTargetUtilization option '%s'\n", argv[i]);
952                 return -1;
953             }
954         } else if (strncmp(argv[i], "-Xss", 4) == 0) {
955             size_t val = parseMemOption(argv[i]+4, 1);
956             if (val != 0) {
957                 if (val >= kMinStackSize && val <= kMaxStackSize) {
958                     gDvm.stackSize = val;
959                     if (val > gDvm.mainThreadStackSize) {
960                         gDvm.mainThreadStackSize = val;
961                     }
962                 } else {
963                     dvmFprintf(stderr, "Invalid -Xss '%s', range is %d to %d\n",
964                         argv[i], kMinStackSize, kMaxStackSize);
965                     return -1;
966                 }
967             } else {
968                 dvmFprintf(stderr, "Invalid -Xss option '%s'\n", argv[i]);
969                 return -1;
970             }
971 
972         } else if (strncmp(argv[i], "-XX:mainThreadStackSize=", strlen("-XX:mainThreadStackSize=")) == 0) {
973             size_t val = parseMemOption(argv[i] + strlen("-XX:mainThreadStackSize="), 1);
974             if (val != 0) {
975                 if (val >= kMinStackSize && val <= kMaxStackSize) {
976                     gDvm.mainThreadStackSize = val;
977                 } else {
978                     dvmFprintf(stderr, "Invalid -XX:mainThreadStackSize '%s', range is %d to %d\n",
979                                argv[i], kMinStackSize, kMaxStackSize);
980                     return -1;
981                 }
982             } else {
983                 dvmFprintf(stderr, "Invalid -XX:mainThreadStackSize option '%s'\n", argv[i]);
984                 return -1;
985             }
986 
987         } else if (strncmp(argv[i], "-XX:+DisableExplicitGC", 22) == 0) {
988             gDvm.disableExplicitGc = true;
989         } else if (strcmp(argv[i], "-verbose") == 0 ||
990             strcmp(argv[i], "-verbose:class") == 0)
991         {
992             // JNI spec says "-verbose:gc,class" is valid, but cmd line
993             // doesn't work that way; may want to support.
994             gDvm.verboseClass = true;
995         } else if (strcmp(argv[i], "-verbose:jni") == 0) {
996             gDvm.verboseJni = true;
997         } else if (strcmp(argv[i], "-verbose:gc") == 0) {
998             gDvm.verboseGc = true;
999         } else if (strcmp(argv[i], "-verbose:shutdown") == 0) {
1000             gDvm.verboseShutdown = true;
1001 
1002         } else if (strncmp(argv[i], "-enableassertions", 17) == 0) {
1003             enableAssertions(argv[i] + 17, true);
1004         } else if (strncmp(argv[i], "-ea", 3) == 0) {
1005             enableAssertions(argv[i] + 3, true);
1006         } else if (strncmp(argv[i], "-disableassertions", 18) == 0) {
1007             enableAssertions(argv[i] + 18, false);
1008         } else if (strncmp(argv[i], "-da", 3) == 0) {
1009             enableAssertions(argv[i] + 3, false);
1010         } else if (strcmp(argv[i], "-enablesystemassertions") == 0 ||
1011                    strcmp(argv[i], "-esa") == 0)
1012         {
1013             enableAssertions(NULL, true);
1014         } else if (strcmp(argv[i], "-disablesystemassertions") == 0 ||
1015                    strcmp(argv[i], "-dsa") == 0)
1016         {
1017             enableAssertions(NULL, false);
1018 
1019         } else if (strncmp(argv[i], "-Xcheck:jni", 11) == 0) {
1020             /* nothing to do now -- was handled during JNI init */
1021 
1022         } else if (strcmp(argv[i], "-Xdebug") == 0) {
1023             /* accept but ignore */
1024 
1025         } else if (strncmp(argv[i], "-Xrunjdwp:", 10) == 0 ||
1026             strncmp(argv[i], "-agentlib:jdwp=", 15) == 0)
1027         {
1028             const char* tail;
1029 
1030             if (argv[i][1] == 'X')
1031                 tail = argv[i] + 10;
1032             else
1033                 tail = argv[i] + 15;
1034 
1035             if (strncmp(tail, "help", 4) == 0 || !parseJdwpOptions(tail)) {
1036                 showJdwpHelp();
1037                 return 1;
1038             }
1039         } else if (strcmp(argv[i], "-Xrs") == 0) {
1040             gDvm.reduceSignals = true;
1041         } else if (strcmp(argv[i], "-Xnoquithandler") == 0) {
1042             /* disables SIGQUIT handler thread while still blocking SIGQUIT */
1043             /* (useful if we don't want thread but system still signals us) */
1044             gDvm.noQuitHandler = true;
1045         } else if (strcmp(argv[i], "-Xzygote") == 0) {
1046             gDvm.zygote = true;
1047 #if defined(WITH_JIT)
1048             gDvmJit.runningInAndroidFramework = true;
1049 #endif
1050         } else if (strncmp(argv[i], "-Xdexopt:", 9) == 0) {
1051             if (strcmp(argv[i] + 9, "none") == 0)
1052                 gDvm.dexOptMode = OPTIMIZE_MODE_NONE;
1053             else if (strcmp(argv[i] + 9, "verified") == 0)
1054                 gDvm.dexOptMode = OPTIMIZE_MODE_VERIFIED;
1055             else if (strcmp(argv[i] + 9, "all") == 0)
1056                 gDvm.dexOptMode = OPTIMIZE_MODE_ALL;
1057             else if (strcmp(argv[i] + 9, "full") == 0)
1058                 gDvm.dexOptMode = OPTIMIZE_MODE_FULL;
1059             else {
1060                 dvmFprintf(stderr, "Unrecognized dexopt option '%s'\n",argv[i]);
1061                 return -1;
1062             }
1063         } else if (strncmp(argv[i], "-Xverify:", 9) == 0) {
1064             if (strcmp(argv[i] + 9, "none") == 0)
1065                 gDvm.classVerifyMode = VERIFY_MODE_NONE;
1066             else if (strcmp(argv[i] + 9, "remote") == 0)
1067                 gDvm.classVerifyMode = VERIFY_MODE_REMOTE;
1068             else if (strcmp(argv[i] + 9, "all") == 0)
1069                 gDvm.classVerifyMode = VERIFY_MODE_ALL;
1070             else {
1071                 dvmFprintf(stderr, "Unrecognized verify option '%s'\n",argv[i]);
1072                 return -1;
1073             }
1074         } else if (strncmp(argv[i], "-Xjnigreflimit:", 15) == 0) {
1075             int lim = atoi(argv[i] + 15);
1076             if (lim < 200 || (lim % 100) != 0) {
1077                 dvmFprintf(stderr, "Bad value for -Xjnigreflimit: '%s'\n",
1078                     argv[i]+15);
1079                 return -1;
1080             }
1081             gDvm.jniGrefLimit = lim;
1082         } else if (strncmp(argv[i], "-Xjnitrace:", 11) == 0) {
1083             gDvm.jniTrace = strdup(argv[i] + 11);
1084         } else if (strcmp(argv[i], "-Xlog-stdio") == 0) {
1085             gDvm.logStdio = true;
1086 
1087         } else if (strncmp(argv[i], "-Xint", 5) == 0) {
1088             if (argv[i][5] == ':') {
1089                 if (strcmp(argv[i] + 6, "portable") == 0)
1090                     gDvm.executionMode = kExecutionModeInterpPortable;
1091                 else if (strcmp(argv[i] + 6, "fast") == 0)
1092                     gDvm.executionMode = kExecutionModeInterpFast;
1093 #ifdef WITH_JIT
1094                 else if (strcmp(argv[i] + 6, "jit") == 0)
1095                     gDvm.executionMode = kExecutionModeJit;
1096 #endif
1097                 else {
1098                     dvmFprintf(stderr,
1099                         "Warning: Unrecognized interpreter mode %s\n",argv[i]);
1100                     /* keep going */
1101                 }
1102             } else {
1103                 /* disable JIT if it was enabled by default */
1104                 gDvm.executionMode = kExecutionModeInterpFast;
1105             }
1106 
1107         } else if (strncmp(argv[i], "-Xlockprofthreshold:", 20) == 0) {
1108             gDvm.lockProfThreshold = atoi(argv[i] + 20);
1109 
1110 #ifdef WITH_JIT
1111         } else if (strncmp(argv[i], "-Xjitop", 7) == 0) {
1112             processXjitop(argv[i]);
1113         } else if (strncmp(argv[i], "-Xjitmethod:", 12) == 0) {
1114             processXjitmethod(argv[i] + strlen("-Xjitmethod:"), true);
1115         } else if (strncmp(argv[i], "-Xjitclass:", 11) == 0) {
1116             processXjitmethod(argv[i] + strlen("-Xjitclass:"), false);
1117         } else if (strncmp(argv[i], "-Xjitoffset:", 12) == 0) {
1118             processXjitoffset(argv[i] + strlen("-Xjitoffset:"));
1119         } else if (strncmp(argv[i], "-Xjitconfig:", 12) == 0) {
1120             processXjitconfig(argv[i] + strlen("-Xjitconfig:"));
1121         } else if (strncmp(argv[i], "-Xjitblocking", 13) == 0) {
1122           gDvmJit.blockingMode = true;
1123         } else if (strncmp(argv[i], "-Xjitthreshold:", 15) == 0) {
1124           gDvmJit.threshold = atoi(argv[i] + 15);
1125         } else if (strncmp(argv[i], "-Xincludeselectedop", 19) == 0) {
1126           gDvmJit.includeSelectedOp = true;
1127         } else if (strncmp(argv[i], "-Xincludeselectedmethod", 23) == 0) {
1128           gDvmJit.includeSelectedMethod = true;
1129         } else if (strncmp(argv[i], "-Xjitcheckcg", 12) == 0) {
1130           gDvmJit.checkCallGraph = true;
1131           /* Need to enable blocking mode due to stack crawling */
1132           gDvmJit.blockingMode = true;
1133         } else if (strncmp(argv[i], "-Xjitdumpbin", 12) == 0) {
1134           gDvmJit.printBinary = true;
1135         } else if (strncmp(argv[i], "-Xjitverbose", 12) == 0) {
1136           gDvmJit.printMe = true;
1137         } else if (strncmp(argv[i], "-Xjitprofile", 12) == 0) {
1138           gDvmJit.profileMode = kTraceProfilingContinuous;
1139         } else if (strncmp(argv[i], "-Xjitdisableopt", 15) == 0) {
1140           /* Disable selected optimizations */
1141           if (argv[i][15] == ':') {
1142               sscanf(argv[i] + 16, "%x", &gDvmJit.disableOpt);
1143           /* Disable all optimizations */
1144           } else {
1145               gDvmJit.disableOpt = -1;
1146           }
1147         } else if (strncmp(argv[i], "-Xjitsuspendpoll", 16) == 0) {
1148           gDvmJit.genSuspendPoll = true;
1149 #endif
1150 
1151         } else if (strncmp(argv[i], "-Xstacktracefile:", 17) == 0) {
1152             gDvm.stackTraceFile = strdup(argv[i]+17);
1153 
1154         } else if (strcmp(argv[i], "-Xgenregmap") == 0) {
1155             gDvm.generateRegisterMaps = true;
1156         } else if (strcmp(argv[i], "-Xnogenregmap") == 0) {
1157             gDvm.generateRegisterMaps = false;
1158 
1159         } else if (strcmp(argv[i], "Xverifyopt:checkmon") == 0) {
1160             gDvm.monitorVerification = true;
1161         } else if (strcmp(argv[i], "Xverifyopt:nocheckmon") == 0) {
1162             gDvm.monitorVerification = false;
1163 
1164         } else if (strncmp(argv[i], "-Xgc:", 5) == 0) {
1165             if (strcmp(argv[i] + 5, "precise") == 0)
1166                 gDvm.preciseGc = true;
1167             else if (strcmp(argv[i] + 5, "noprecise") == 0)
1168                 gDvm.preciseGc = false;
1169             else if (strcmp(argv[i] + 5, "preverify") == 0)
1170                 gDvm.preVerify = true;
1171             else if (strcmp(argv[i] + 5, "nopreverify") == 0)
1172                 gDvm.preVerify = false;
1173             else if (strcmp(argv[i] + 5, "postverify") == 0)
1174                 gDvm.postVerify = true;
1175             else if (strcmp(argv[i] + 5, "nopostverify") == 0)
1176                 gDvm.postVerify = false;
1177             else if (strcmp(argv[i] + 5, "concurrent") == 0)
1178                 gDvm.concurrentMarkSweep = true;
1179             else if (strcmp(argv[i] + 5, "noconcurrent") == 0)
1180                 gDvm.concurrentMarkSweep = false;
1181             else if (strcmp(argv[i] + 5, "verifycardtable") == 0)
1182                 gDvm.verifyCardTable = true;
1183             else if (strcmp(argv[i] + 5, "noverifycardtable") == 0)
1184                 gDvm.verifyCardTable = false;
1185             else {
1186                 dvmFprintf(stderr, "Bad value for -Xgc");
1187                 return -1;
1188             }
1189             ALOGV("Precise GC configured %s", gDvm.preciseGc ? "ON" : "OFF");
1190 
1191         } else if (strcmp(argv[i], "-Xcheckdexsum") == 0) {
1192             gDvm.verifyDexChecksum = true;
1193 
1194         } else if (strcmp(argv[i], "-Xprofile:threadcpuclock") == 0) {
1195             gDvm.profilerClockSource = kProfilerClockSourceThreadCpu;
1196         } else if (strcmp(argv[i], "-Xprofile:wallclock") == 0) {
1197             gDvm.profilerClockSource = kProfilerClockSourceWall;
1198         } else if (strcmp(argv[i], "-Xprofile:dualclock") == 0) {
1199             gDvm.profilerClockSource = kProfilerClockSourceDual;
1200 
1201         } else {
1202             if (!ignoreUnrecognized) {
1203                 dvmFprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
1204                 return -1;
1205             }
1206         }
1207     }
1208 
1209     return 0;
1210 }
1211 
1212 /*
1213  * Set defaults for fields altered or modified by arguments.
1214  *
1215  * Globals are initialized to 0 (a/k/a NULL or false).
1216  */
setCommandLineDefaults()1217 static void setCommandLineDefaults()
1218 {
1219     const char* envStr = getenv("CLASSPATH");
1220     if (envStr != NULL) {
1221         gDvm.classPathStr = strdup(envStr);
1222     } else {
1223         gDvm.classPathStr = strdup(".");
1224     }
1225     envStr = getenv("BOOTCLASSPATH");
1226     if (envStr != NULL) {
1227         gDvm.bootClassPathStr = strdup(envStr);
1228     } else {
1229         gDvm.bootClassPathStr = strdup(".");
1230     }
1231 
1232     gDvm.properties = new std::vector<std::string>();
1233 
1234     /* Defaults overridden by -Xms and -Xmx.
1235      * TODO: base these on a system or application-specific default
1236      */
1237     gDvm.heapStartingSize = 2 * 1024 * 1024;  // Spec says 16MB; too big for us.
1238     gDvm.heapMaximumSize = 16 * 1024 * 1024;  // Spec says 75% physical mem
1239     gDvm.heapGrowthLimit = 0;  // 0 means no growth limit
1240     gDvm.stackSize = kDefaultStackSize;
1241     gDvm.mainThreadStackSize = kDefaultStackSize;
1242     // When the heap is less than the maximum or growth limited size,
1243     // fix the free portion of the heap. The utilization is the ratio
1244     // of live to free memory, 0.5 implies half the heap is available
1245     // to allocate into before a GC occurs. Min free and max free
1246     // force the free memory to never be smaller than min free or
1247     // larger than max free.
1248     gDvm.heapTargetUtilization = 0.5;
1249     gDvm.heapMaxFree = 2 * 1024 * 1024;
1250     gDvm.heapMinFree = gDvm.heapMaxFree / 4;
1251 
1252     gDvm.concurrentMarkSweep = true;
1253 
1254     /* gDvm.jdwpSuspend = true; */
1255 
1256     /* allowed unless zygote config doesn't allow it */
1257     gDvm.jdwpAllowed = true;
1258 
1259     /* default verification and optimization modes */
1260     gDvm.classVerifyMode = VERIFY_MODE_ALL;
1261     gDvm.dexOptMode = OPTIMIZE_MODE_VERIFIED;
1262     gDvm.monitorVerification = false;
1263     gDvm.generateRegisterMaps = true;
1264     gDvm.registerMapMode = kRegisterMapModeTypePrecise;
1265 
1266     /*
1267      * Default execution mode.
1268      *
1269      * This should probably interact with the mterp code somehow, e.g. if
1270      * we know we're using the "desktop" build we should probably be
1271      * using "portable" rather than "fast".
1272      */
1273 #if defined(WITH_JIT)
1274     gDvm.executionMode = kExecutionModeJit;
1275     gDvmJit.num_entries_pcTable = 0;
1276     gDvmJit.includeSelectedMethod = false;
1277     gDvmJit.includeSelectedOffset = false;
1278     gDvmJit.methodTable = NULL;
1279     gDvmJit.classTable = NULL;
1280 
1281     gDvm.constInit = false;
1282     gDvm.commonInit = false;
1283 #else
1284     gDvm.executionMode = kExecutionModeInterpFast;
1285 #endif
1286 
1287     /*
1288      * SMP support is a compile-time define, but we may want to have
1289      * dexopt target a differently-configured device.
1290      */
1291     gDvm.dexOptForSmp = (ANDROID_SMP != 0);
1292 
1293     /*
1294      * Default profiler configuration.
1295      */
1296     gDvm.profilerClockSource = kProfilerClockSourceDual;
1297 }
1298 
1299 
1300 /*
1301  * Handle a SIGBUS, which frequently occurs because somebody replaced an
1302  * optimized DEX file out from under us.
1303  */
busCatcher(int signum,siginfo_t * info,void * context)1304 static void busCatcher(int signum, siginfo_t* info, void* context)
1305 {
1306     void* addr = info->si_addr;
1307 
1308     ALOGE("Caught a SIGBUS (%d), addr=%p", signum, addr);
1309 
1310     /*
1311      * If we return at this point the SIGBUS just keeps happening, so we
1312      * remove the signal handler and allow it to kill us.  TODO: restore
1313      * the original, which points to a debuggerd stub; if we don't then
1314      * debuggerd won't be notified.
1315      */
1316     signal(SIGBUS, SIG_DFL);
1317 }
1318 
1319 /*
1320  * Configure signals.  We need to block SIGQUIT so that the signal only
1321  * reaches the dump-stack-trace thread.
1322  *
1323  * This can be disabled with the "-Xrs" flag.
1324  */
blockSignals()1325 static void blockSignals()
1326 {
1327     sigset_t mask;
1328     int cc;
1329 
1330     sigemptyset(&mask);
1331     sigaddset(&mask, SIGQUIT);
1332     sigaddset(&mask, SIGUSR1);      // used to initiate heap dump
1333 #if defined(WITH_JIT) && defined(WITH_JIT_TUNING)
1334     sigaddset(&mask, SIGUSR2);      // used to investigate JIT internals
1335 #endif
1336     //sigaddset(&mask, SIGPIPE);
1337     cc = sigprocmask(SIG_BLOCK, &mask, NULL);
1338     assert(cc == 0);
1339 
1340     if (false) {
1341         /* TODO: save the old sigaction in a global */
1342         struct sigaction sa;
1343         memset(&sa, 0, sizeof(sa));
1344         sa.sa_sigaction = busCatcher;
1345         sa.sa_flags = SA_SIGINFO;
1346         cc = sigaction(SIGBUS, &sa, NULL);
1347         assert(cc == 0);
1348     }
1349 }
1350 
1351 class ScopedShutdown {
1352 public:
ScopedShutdown()1353     ScopedShutdown() : armed_(true) {
1354     }
1355 
~ScopedShutdown()1356     ~ScopedShutdown() {
1357         if (armed_) {
1358             dvmShutdown();
1359         }
1360     }
1361 
disarm()1362     void disarm() {
1363         armed_ = false;
1364     }
1365 
1366 private:
1367     bool armed_;
1368 };
1369 
1370 /*
1371  * VM initialization.  Pass in any options provided on the command line.
1372  * Do not pass in the class name or the options for the class.
1373  *
1374  * Returns 0 on success.
1375  */
dvmStartup(int argc,const char * const argv[],bool ignoreUnrecognized,JNIEnv * pEnv)1376 std::string dvmStartup(int argc, const char* const argv[],
1377         bool ignoreUnrecognized, JNIEnv* pEnv)
1378 {
1379     ScopedShutdown scopedShutdown;
1380 
1381     assert(gDvm.initializing);
1382 
1383     ALOGV("VM init args (%d):", argc);
1384     for (int i = 0; i < argc; i++) {
1385         ALOGV("  %d: '%s'", i, argv[i]);
1386     }
1387     setCommandLineDefaults();
1388 
1389     /*
1390      * Process the option flags (if any).
1391      */
1392     int cc = processOptions(argc, argv, ignoreUnrecognized);
1393     if (cc != 0) {
1394         if (cc < 0) {
1395             dvmFprintf(stderr, "\n");
1396             usage("dalvikvm");
1397         }
1398         return "syntax error";
1399     }
1400 
1401 #if WITH_EXTRA_GC_CHECKS > 1
1402     /* only "portable" interp has the extra goodies */
1403     if (gDvm.executionMode != kExecutionModeInterpPortable) {
1404         ALOGI("Switching to 'portable' interpreter for GC checks");
1405         gDvm.executionMode = kExecutionModeInterpPortable;
1406     }
1407 #endif
1408 
1409     /* Configure group scheduling capabilities */
1410     if (!access("/dev/cpuctl/tasks", F_OK)) {
1411         ALOGV("Using kernel group scheduling");
1412         gDvm.kernelGroupScheduling = 1;
1413     } else {
1414         ALOGV("Using kernel scheduler policies");
1415     }
1416 
1417     /* configure signal handling */
1418     if (!gDvm.reduceSignals)
1419         blockSignals();
1420 
1421     /* verify system page size */
1422     if (sysconf(_SC_PAGESIZE) != SYSTEM_PAGE_SIZE) {
1423         return StringPrintf("expected page size %d, got %d",
1424                 SYSTEM_PAGE_SIZE, (int) sysconf(_SC_PAGESIZE));
1425     }
1426 
1427     /* mterp setup */
1428     ALOGV("Using executionMode %d", gDvm.executionMode);
1429     dvmCheckAsmConstants();
1430 
1431     /*
1432      * Initialize components.
1433      */
1434     dvmQuasiAtomicsStartup();
1435     if (!dvmAllocTrackerStartup()) {
1436         return "dvmAllocTrackerStartup failed";
1437     }
1438     if (!dvmGcStartup()) {
1439         return "dvmGcStartup failed";
1440     }
1441     if (!dvmThreadStartup()) {
1442         return "dvmThreadStartup failed";
1443     }
1444     if (!dvmInlineNativeStartup()) {
1445         return "dvmInlineNativeStartup";
1446     }
1447     if (!dvmRegisterMapStartup()) {
1448         return "dvmRegisterMapStartup failed";
1449     }
1450     if (!dvmInstanceofStartup()) {
1451         return "dvmInstanceofStartup failed";
1452     }
1453     if (!dvmClassStartup()) {
1454         return "dvmClassStartup failed";
1455     }
1456 
1457     /*
1458      * At this point, the system is guaranteed to be sufficiently
1459      * initialized that we can look up classes and class members. This
1460      * call populates the gDvm instance with all the class and member
1461      * references that the VM wants to use directly.
1462      */
1463     if (!dvmFindRequiredClassesAndMembers()) {
1464         return "dvmFindRequiredClassesAndMembers failed";
1465     }
1466 
1467     if (!dvmStringInternStartup()) {
1468         return "dvmStringInternStartup failed";
1469     }
1470     if (!dvmNativeStartup()) {
1471         return "dvmNativeStartup failed";
1472     }
1473     if (!dvmInternalNativeStartup()) {
1474         return "dvmInternalNativeStartup failed";
1475     }
1476     if (!dvmJniStartup()) {
1477         return "dvmJniStartup failed";
1478     }
1479     if (!dvmProfilingStartup()) {
1480         return "dvmProfilingStartup failed";
1481     }
1482 
1483     /*
1484      * Create a table of methods for which we will substitute an "inline"
1485      * version for performance.
1486      */
1487     if (!dvmCreateInlineSubsTable()) {
1488         return "dvmCreateInlineSubsTable failed";
1489     }
1490 
1491     /*
1492      * Miscellaneous class library validation.
1493      */
1494     if (!dvmValidateBoxClasses()) {
1495         return "dvmValidateBoxClasses failed";
1496     }
1497 
1498     /*
1499      * Do the last bits of Thread struct initialization we need to allow
1500      * JNI calls to work.
1501      */
1502     if (!dvmPrepMainForJni(pEnv)) {
1503         return "dvmPrepMainForJni failed";
1504     }
1505 
1506     /*
1507      * Explicitly initialize java.lang.Class.  This doesn't happen
1508      * automatically because it's allocated specially (it's an instance
1509      * of itself).  Must happen before registration of system natives,
1510      * which make some calls that throw assertions if the classes they
1511      * operate on aren't initialized.
1512      */
1513     if (!dvmInitClass(gDvm.classJavaLangClass)) {
1514         return "couldn't initialized java.lang.Class";
1515     }
1516 
1517     /*
1518      * Register the system native methods, which are registered through JNI.
1519      */
1520     if (!registerSystemNatives(pEnv)) {
1521         return "couldn't register system natives";
1522     }
1523 
1524     /*
1525      * Do some "late" initialization for the memory allocator.  This may
1526      * allocate storage and initialize classes.
1527      */
1528     if (!dvmCreateStockExceptions()) {
1529         return "dvmCreateStockExceptions failed";
1530     }
1531 
1532     /*
1533      * At this point, the VM is in a pretty good state.  Finish prep on
1534      * the main thread (specifically, create a java.lang.Thread object to go
1535      * along with our Thread struct).  Note we will probably be executing
1536      * some interpreted class initializer code in here.
1537      */
1538     if (!dvmPrepMainThread()) {
1539         return "dvmPrepMainThread failed";
1540     }
1541 
1542     /*
1543      * Make sure we haven't accumulated any tracked references.  The main
1544      * thread should be starting with a clean slate.
1545      */
1546     if (dvmReferenceTableEntries(&dvmThreadSelf()->internalLocalRefTable) != 0)
1547     {
1548         ALOGW("Warning: tracked references remain post-initialization");
1549         dvmDumpReferenceTable(&dvmThreadSelf()->internalLocalRefTable, "MAIN");
1550     }
1551 
1552     /* general debugging setup */
1553     if (!dvmDebuggerStartup()) {
1554         return "dvmDebuggerStartup failed";
1555     }
1556 
1557     if (!dvmGcStartupClasses()) {
1558         return "dvmGcStartupClasses failed";
1559     }
1560 
1561     /*
1562      * Init for either zygote mode or non-zygote mode.  The key difference
1563      * is that we don't start any additional threads in Zygote mode.
1564      */
1565     if (gDvm.zygote) {
1566         if (!initZygote()) {
1567             return "initZygote failed";
1568         }
1569     } else {
1570         if (!dvmInitAfterZygote()) {
1571             return "dvmInitAfterZygote failed";
1572         }
1573     }
1574 
1575 
1576 #ifndef NDEBUG
1577     if (!dvmTestHash())
1578         ALOGE("dvmTestHash FAILED");
1579     if (false /*noisy!*/ && !dvmTestIndirectRefTable())
1580         ALOGE("dvmTestIndirectRefTable FAILED");
1581 #endif
1582 
1583     if (dvmCheckException(dvmThreadSelf())) {
1584         dvmLogExceptionStackTrace();
1585         return "Exception pending at end of VM initialization";
1586     }
1587 
1588     scopedShutdown.disarm();
1589     return "";
1590 }
1591 
loadJniLibrary(const char * name)1592 static void loadJniLibrary(const char* name) {
1593     std::string mappedName(StringPrintf(OS_SHARED_LIB_FORMAT_STR, name));
1594     char* reason = NULL;
1595     if (!dvmLoadNativeCode(mappedName.c_str(), NULL, &reason)) {
1596         ALOGE("dvmLoadNativeCode failed for \"%s\": %s", name, reason);
1597         dvmAbort();
1598     }
1599 }
1600 
1601 /*
1602  * Register java.* natives from our class libraries.  We need to do
1603  * this after we're ready for JNI registration calls, but before we
1604  * do any class initialization.
1605  *
1606  * If we get this wrong, we will blow up in the ThreadGroup class init if
1607  * interpreted code makes any reference to System.  It will likely do this
1608  * since it wants to do some java.io.File setup (e.g. for static in/out/err).
1609  *
1610  * We need to have gDvm.initializing raised here so that JNI FindClass
1611  * won't try to use the system/application class loader.
1612  */
registerSystemNatives(JNIEnv * pEnv)1613 static bool registerSystemNatives(JNIEnv* pEnv)
1614 {
1615     // Main thread is always first in list.
1616     Thread* self = gDvm.threadList;
1617 
1618     // Must set this before allowing JNI-based method registration.
1619     self->status = THREAD_NATIVE;
1620 
1621     // Most JNI libraries can just use System.loadLibrary, but you can't
1622     // if you're the library that implements System.loadLibrary!
1623     loadJniLibrary("javacore");
1624     loadJniLibrary("nativehelper");
1625 
1626     // Back to run mode.
1627     self->status = THREAD_RUNNING;
1628 
1629     return true;
1630 }
1631 
1632 
1633 /*
1634  * Do zygote-mode-only initialization.
1635  */
initZygote()1636 static bool initZygote()
1637 {
1638     /* zygote goes into its own process group */
1639     setpgid(0,0);
1640 
1641     // See storage config details at http://source.android.com/tech/storage/
1642     // Create private mount namespace shared by all children
1643     if (unshare(CLONE_NEWNS) == -1) {
1644         SLOGE("Failed to unshare(): %s", strerror(errno));
1645         return -1;
1646     }
1647 
1648     // Mark rootfs as being a slave so that changes from default
1649     // namespace only flow into our children.
1650     if (mount("rootfs", "/", NULL, (MS_SLAVE | MS_REC), NULL) == -1) {
1651         SLOGE("Failed to mount() rootfs as MS_SLAVE: %s", strerror(errno));
1652         return -1;
1653     }
1654 
1655     // Create a staging tmpfs that is shared by our children; they will
1656     // bind mount storage into their respective private namespaces, which
1657     // are isolated from each other.
1658     const char* target_base = getenv("EMULATED_STORAGE_TARGET");
1659     if (target_base != NULL) {
1660         if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV,
1661                 "uid=0,gid=1028,mode=0050") == -1) {
1662             SLOGE("Failed to mount tmpfs to %s: %s", target_base, strerror(errno));
1663             return -1;
1664         }
1665     }
1666 
1667     return true;
1668 }
1669 
1670 /*
1671  * Do non-zygote-mode initialization.  This is done during VM init for
1672  * standard startup, or after a "zygote fork" when creating a new process.
1673  */
dvmInitAfterZygote()1674 bool dvmInitAfterZygote()
1675 {
1676     u8 startHeap, startQuit, startJdwp;
1677     u8 endHeap, endQuit, endJdwp;
1678 
1679     startHeap = dvmGetRelativeTimeUsec();
1680 
1681     /*
1682      * Post-zygote heap initialization, including starting
1683      * the HeapWorker thread.
1684      */
1685     if (!dvmGcStartupAfterZygote())
1686         return false;
1687 
1688     endHeap = dvmGetRelativeTimeUsec();
1689     startQuit = dvmGetRelativeTimeUsec();
1690 
1691     /* start signal catcher thread that dumps stacks on SIGQUIT */
1692     if (!gDvm.reduceSignals && !gDvm.noQuitHandler) {
1693         if (!dvmSignalCatcherStartup())
1694             return false;
1695     }
1696 
1697     /* start stdout/stderr copier, if requested */
1698     if (gDvm.logStdio) {
1699         if (!dvmStdioConverterStartup())
1700             return false;
1701     }
1702 
1703     endQuit = dvmGetRelativeTimeUsec();
1704     startJdwp = dvmGetRelativeTimeUsec();
1705 
1706     /*
1707      * Start JDWP thread.  If the command-line debugger flags specified
1708      * "suspend=y", this will pause the VM.  We probably want this to
1709      * come last.
1710      */
1711     if (!initJdwp()) {
1712         ALOGD("JDWP init failed; continuing anyway");
1713     }
1714 
1715     endJdwp = dvmGetRelativeTimeUsec();
1716 
1717     ALOGV("thread-start heap=%d quit=%d jdwp=%d total=%d usec",
1718         (int)(endHeap-startHeap), (int)(endQuit-startQuit),
1719         (int)(endJdwp-startJdwp), (int)(endJdwp-startHeap));
1720 
1721 #ifdef WITH_JIT
1722     if (gDvm.executionMode == kExecutionModeJit) {
1723         if (!dvmCompilerStartup())
1724             return false;
1725     }
1726 #endif
1727 
1728     return true;
1729 }
1730 
1731 /*
1732  * Prepare for a connection to a JDWP-compliant debugger.
1733  *
1734  * Note this needs to happen fairly late in the startup process, because
1735  * we need to have all of the java.* native methods registered (which in
1736  * turn requires JNI to be fully prepped).
1737  *
1738  * There are several ways to initialize:
1739  *   server=n
1740  *     We immediately try to connect to host:port.  Bail on failure.  On
1741  *     success, send VM_START (suspending the VM if "suspend=y").
1742  *   server=y suspend=n
1743  *     Passively listen for a debugger to connect.  Return immediately.
1744  *   server=y suspend=y
1745  *     Wait until debugger connects.  Send VM_START ASAP, suspending the
1746  *     VM after the message is sent.
1747  *
1748  * This gets more complicated with a nonzero value for "timeout".
1749  */
initJdwp()1750 static bool initJdwp()
1751 {
1752     assert(!gDvm.zygote);
1753 
1754     /*
1755      * Init JDWP if the debugger is enabled.  This may connect out to a
1756      * debugger, passively listen for a debugger, or block waiting for a
1757      * debugger.
1758      */
1759     if (gDvm.jdwpAllowed && gDvm.jdwpConfigured) {
1760         JdwpStartupParams params;
1761 
1762         if (gDvm.jdwpHost != NULL) {
1763             if (strlen(gDvm.jdwpHost) >= sizeof(params.host)-1) {
1764                 ALOGE("ERROR: hostname too long: '%s'", gDvm.jdwpHost);
1765                 return false;
1766             }
1767             strcpy(params.host, gDvm.jdwpHost);
1768         } else {
1769             params.host[0] = '\0';
1770         }
1771         params.transport = gDvm.jdwpTransport;
1772         params.server = gDvm.jdwpServer;
1773         params.suspend = gDvm.jdwpSuspend;
1774         params.port = gDvm.jdwpPort;
1775 
1776         gDvm.jdwpState = dvmJdwpStartup(&params);
1777         if (gDvm.jdwpState == NULL) {
1778             ALOGW("WARNING: debugger thread failed to initialize");
1779             /* TODO: ignore? fail? need to mimic "expected" behavior */
1780         }
1781     }
1782 
1783     /*
1784      * If a debugger has already attached, send the "welcome" message.  This
1785      * may cause us to suspend all threads.
1786      */
1787     if (dvmJdwpIsActive(gDvm.jdwpState)) {
1788         //dvmChangeStatus(NULL, THREAD_RUNNING);
1789         if (!dvmJdwpPostVMStart(gDvm.jdwpState, gDvm.jdwpSuspend)) {
1790             ALOGW("WARNING: failed to post 'start' message to debugger");
1791             /* keep going */
1792         }
1793         //dvmChangeStatus(NULL, THREAD_NATIVE);
1794     }
1795 
1796     return true;
1797 }
1798 
1799 /*
1800  * An alternative to JNI_CreateJavaVM/dvmStartup that does the first bit
1801  * of initialization and then returns with "initializing" still set.  (Used
1802  * by DexOpt command-line utility.)
1803  *
1804  * Attempting to use JNI or internal natives will fail.  It's best
1805  * if no bytecode gets executed, which means no <clinit>, which means
1806  * no exception-throwing.  (In practice we need to initialize Class and
1807  * Object, and probably some exception classes.)
1808  *
1809  * Returns 0 on success.
1810  */
dvmPrepForDexOpt(const char * bootClassPath,DexOptimizerMode dexOptMode,DexClassVerifyMode verifyMode,int dexoptFlags)1811 int dvmPrepForDexOpt(const char* bootClassPath, DexOptimizerMode dexOptMode,
1812     DexClassVerifyMode verifyMode, int dexoptFlags)
1813 {
1814     gDvm.initializing = true;
1815     gDvm.optimizing = true;
1816 
1817     /* configure signal handling */
1818     blockSignals();
1819 
1820     /* set some defaults */
1821     setCommandLineDefaults();
1822     free(gDvm.bootClassPathStr);
1823     gDvm.bootClassPathStr = strdup(bootClassPath);
1824 
1825     /* set opt/verify modes */
1826     gDvm.dexOptMode = dexOptMode;
1827     gDvm.classVerifyMode = verifyMode;
1828     gDvm.generateRegisterMaps = (dexoptFlags & DEXOPT_GEN_REGISTER_MAPS) != 0;
1829     if (dexoptFlags & DEXOPT_SMP) {
1830         assert((dexoptFlags & DEXOPT_UNIPROCESSOR) == 0);
1831         gDvm.dexOptForSmp = true;
1832     } else if (dexoptFlags & DEXOPT_UNIPROCESSOR) {
1833         gDvm.dexOptForSmp = false;
1834     } else {
1835         gDvm.dexOptForSmp = (ANDROID_SMP != 0);
1836     }
1837 
1838     /*
1839      * Initialize the heap, some basic thread control mutexes, and
1840      * get the bootclasspath prepped.
1841      *
1842      * We can't load any classes yet because we may not yet have a source
1843      * for things like java.lang.Object and java.lang.Class.
1844      */
1845     if (!dvmGcStartup())
1846         goto fail;
1847     if (!dvmThreadStartup())
1848         goto fail;
1849     if (!dvmInlineNativeStartup())
1850         goto fail;
1851     if (!dvmRegisterMapStartup())
1852         goto fail;
1853     if (!dvmInstanceofStartup())
1854         goto fail;
1855     if (!dvmClassStartup())
1856         goto fail;
1857 
1858     /*
1859      * We leave gDvm.initializing set to "true" so that, if we're not
1860      * able to process the "core" classes, we don't go into a death-spin
1861      * trying to throw a "class not found" exception.
1862      */
1863 
1864     return 0;
1865 
1866 fail:
1867     dvmShutdown();
1868     return 1;
1869 }
1870 
1871 
1872 /*
1873  * All threads have stopped.  Finish the shutdown procedure.
1874  *
1875  * We can also be called if startup fails partway through, so be prepared
1876  * to deal with partially initialized data.
1877  *
1878  * Free any storage allocated in gGlobals.
1879  *
1880  * We can't dlclose() shared libs we've loaded, because it's possible a
1881  * thread not associated with the VM is running code in one.
1882  *
1883  * This is called from the JNI DestroyJavaVM function, which can be
1884  * called from any thread.  (In practice, this will usually run in the
1885  * same thread that started the VM, a/k/a the main thread, but we don't
1886  * want to assume that.)
1887  */
dvmShutdown()1888 void dvmShutdown()
1889 {
1890     ALOGV("VM shutting down");
1891 
1892     if (CALC_CACHE_STATS)
1893         dvmDumpAtomicCacheStats(gDvm.instanceofCache);
1894 
1895     /*
1896      * Stop our internal threads.
1897      */
1898     dvmGcThreadShutdown();
1899 
1900     if (gDvm.jdwpState != NULL)
1901         dvmJdwpShutdown(gDvm.jdwpState);
1902     free(gDvm.jdwpHost);
1903     gDvm.jdwpHost = NULL;
1904     free(gDvm.jniTrace);
1905     gDvm.jniTrace = NULL;
1906     free(gDvm.stackTraceFile);
1907     gDvm.stackTraceFile = NULL;
1908 
1909     /* tell signal catcher to shut down if it was started */
1910     dvmSignalCatcherShutdown();
1911 
1912     /* shut down stdout/stderr conversion */
1913     dvmStdioConverterShutdown();
1914 
1915 #ifdef WITH_JIT
1916     if (gDvm.executionMode == kExecutionModeJit) {
1917         /* shut down the compiler thread */
1918         dvmCompilerShutdown();
1919     }
1920 #endif
1921 
1922     /*
1923      * Kill any daemon threads that still exist.  Actively-running threads
1924      * are likely to crash the process if they continue to execute while
1925      * the VM shuts down.
1926      */
1927     dvmSlayDaemons();
1928 
1929     if (gDvm.verboseShutdown)
1930         ALOGD("VM cleaning up");
1931 
1932     dvmDebuggerShutdown();
1933     dvmProfilingShutdown();
1934     dvmJniShutdown();
1935     dvmStringInternShutdown();
1936     dvmThreadShutdown();
1937     dvmClassShutdown();
1938     dvmRegisterMapShutdown();
1939     dvmInstanceofShutdown();
1940     dvmInlineNativeShutdown();
1941     dvmGcShutdown();
1942     dvmAllocTrackerShutdown();
1943 
1944     /* these must happen AFTER dvmClassShutdown has walked through class data */
1945     dvmNativeShutdown();
1946     dvmInternalNativeShutdown();
1947 
1948     dvmFreeInlineSubsTable();
1949 
1950     free(gDvm.bootClassPathStr);
1951     free(gDvm.classPathStr);
1952     delete gDvm.properties;
1953 
1954     freeAssertionCtrl();
1955 
1956     dvmQuasiAtomicsShutdown();
1957 
1958     /*
1959      * We want valgrind to report anything we forget to free as "definitely
1960      * lost".  If there's a pointer in the global chunk, it would be reported
1961      * as "still reachable".  Erasing the memory fixes this.
1962      *
1963      * This must be erased to zero if we want to restart the VM within this
1964      * process.
1965      */
1966     memset(&gDvm, 0xcd, sizeof(gDvm));
1967 }
1968 
1969 
1970 /*
1971  * fprintf() wrapper that calls through the JNI-specified vfprintf hook if
1972  * one was specified.
1973  */
dvmFprintf(FILE * fp,const char * format,...)1974 int dvmFprintf(FILE* fp, const char* format, ...)
1975 {
1976     va_list args;
1977     int result;
1978 
1979     va_start(args, format);
1980     if (gDvm.vfprintfHook != NULL)
1981         result = (*gDvm.vfprintfHook)(fp, format, args);
1982     else
1983         result = vfprintf(fp, format, args);
1984     va_end(args);
1985 
1986     return result;
1987 }
1988 
1989 #ifdef __GLIBC__
1990 #include <execinfo.h>
1991 /*
1992  * glibc-only stack dump function.  Requires link with "--export-dynamic".
1993  *
1994  * TODO: move this into libs/cutils and make it work for all platforms.
1995  */
dvmPrintNativeBackTrace()1996 void dvmPrintNativeBackTrace()
1997 {
1998     size_t MAX_STACK_FRAMES = 64;
1999     void* stackFrames[MAX_STACK_FRAMES];
2000     size_t frameCount = backtrace(stackFrames, MAX_STACK_FRAMES);
2001 
2002     /*
2003      * TODO: in practice, we may find that we should use backtrace_symbols_fd
2004      * to avoid allocation, rather than use our own custom formatting.
2005      */
2006     char** strings = backtrace_symbols(stackFrames, frameCount);
2007     if (strings == NULL) {
2008         ALOGE("backtrace_symbols failed: %s", strerror(errno));
2009         return;
2010     }
2011 
2012     size_t i;
2013     for (i = 0; i < frameCount; ++i) {
2014         ALOGW("#%-2d %s", i, strings[i]);
2015     }
2016     free(strings);
2017 }
2018 #else
dvmPrintNativeBackTrace()2019 void dvmPrintNativeBackTrace() {
2020     /* Hopefully, you're on an Android device and debuggerd will do this. */
2021 }
2022 #endif
2023 
2024 /*
2025  * Abort the VM.  We get here on fatal errors.  Try very hard not to use
2026  * this; whenever possible, return an error to somebody responsible.
2027  */
dvmAbort()2028 void dvmAbort()
2029 {
2030     /*
2031      * Leave gDvm.lastMessage on the stack frame which can be decoded in the
2032      * tombstone file. This is for situations where we only have tombstone files
2033      * but no logs (ie b/5372634).
2034      *
2035      * For example, in the tombstone file you usually see this:
2036      *
2037      *   #00  pc 00050ef2  /system/lib/libdvm.so (dvmAbort)
2038      *   #01  pc 00077670  /system/lib/libdvm.so (_Z15dvmClassStartupv)
2039      *     :
2040      *
2041      * stack:
2042      *     :
2043      * #00 beed2658  00000000
2044      *     beed265c  7379732f
2045      *     beed2660  2f6d6574
2046      *     beed2664  6d617266
2047      *     beed2668  726f7765
2048      *     beed266c  6f632f6b
2049      *     beed2670  6a2e6572
2050      *     beed2674  00007261
2051      *     beed2678  00000000
2052      *
2053      * The ascii values between beed265c and beed2674 belongs to messageBuffer
2054      * and it can be decoded as "/system/framework/core.jar".
2055      */
2056     const int messageLength = 512;
2057     char messageBuffer[messageLength] = {0};
2058     int result = 0;
2059 
2060     snprintf(messageBuffer, messageLength, "%s", gDvm.lastMessage);
2061 
2062     /* So that messageBuffer[] looks like useful stuff to the compiler */
2063     for (int i = 0; i < messageLength && messageBuffer[i]; i++) {
2064         result += messageBuffer[i];
2065     }
2066 
2067     ALOGE("VM aborting");
2068 
2069     fflush(NULL);       // flush all open file buffers
2070 
2071     /* JNI-supplied abort hook gets right of first refusal */
2072     if (gDvm.abortHook != NULL)
2073         (*gDvm.abortHook)();
2074 
2075     /*
2076      * On the device, debuggerd will give us a stack trace.
2077      * On the host, we have to help ourselves.
2078      */
2079     dvmPrintNativeBackTrace();
2080 
2081     /*
2082      * If we call abort(), all threads in the process receives a SIBABRT.
2083      * debuggerd dumps the stack trace of the main thread, whether or not
2084      * that was the thread that failed.
2085      *
2086      * By stuffing a value into a bogus address, we cause a segmentation
2087      * fault in the current thread, and get a useful log from debuggerd.
2088      * We can also trivially tell the difference between a VM crash and
2089      * a deliberate abort by looking at the fault address.
2090      */
2091     *((char*)0xdeadd00d) = result;
2092     abort();
2093 
2094     /* notreached */
2095 }
2096