OSDN Git Service

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