This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
mirrored from https://android.googlesource.com/platform/dalvik.git
-
Notifications
You must be signed in to change notification settings - Fork 263
/
Init.c
1693 lines (1507 loc) · 52.5 KB
/
Init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Dalvik initialization, shutdown, and command-line argument processing.
*/
#include "Dalvik.h"
#include "test/Test.h"
#include "mterp/Mterp.h"
#include "Hash.h"
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <limits.h>
#include <ctype.h>
#include <sys/wait.h>
#include <unistd.h>
#define kMinHeapStartSize (1*1024*1024)
#define kMinHeapSize (2*1024*1024)
#define kMaxHeapSize (1*1024*1024*1024)
/*
* Register VM-agnostic native methods for system classes.
*
* Currently defined in ../include/nativehelper/AndroidSystemNatives.h
*/
extern int jniRegisterSystemMethods(JNIEnv* env);
/* fwd */
static bool registerSystemNatives(JNIEnv* pEnv);
static bool dvmInitJDWP(void);
static bool dvmInitZygote(void);
/* global state */
struct DvmGlobals gDvm;
/* JIT-specific global state */
#if defined(WITH_JIT)
struct DvmJitGlobals gDvmJit;
#endif
/*
* Show usage.
*
* We follow the tradition of unhyphenated compound words.
*/
static void dvmUsage(const char* progName)
{
dvmFprintf(stderr, "%s: [options] class [argument ...]\n", progName);
dvmFprintf(stderr, "%s: [options] -jar file.jar [argument ...]\n",progName);
dvmFprintf(stderr, "\n");
dvmFprintf(stderr, "The following standard options are recognized:\n");
dvmFprintf(stderr, " -classpath classpath\n");
dvmFprintf(stderr, " -Dproperty=value\n");
dvmFprintf(stderr, " -verbose:tag ('gc', 'jni', or 'class')\n");
dvmFprintf(stderr, " -ea[:<package name>... |:<class name>]\n");
dvmFprintf(stderr, " -da[:<package name>... |:<class name>]\n");
dvmFprintf(stderr, " (-enableassertions, -disableassertions)\n");
dvmFprintf(stderr, " -esa\n");
dvmFprintf(stderr, " -dsa\n");
dvmFprintf(stderr,
" (-enablesystemassertions, -disablesystemassertions)\n");
dvmFprintf(stderr, " -showversion\n");
dvmFprintf(stderr, " -help\n");
dvmFprintf(stderr, "\n");
dvmFprintf(stderr, "The following extended options are recognized:\n");
dvmFprintf(stderr, " -Xrunjdwp:<options>\n");
dvmFprintf(stderr, " -Xbootclasspath:bootclasspath\n");
dvmFprintf(stderr, " -Xcheck:tag (e.g. 'jni')\n");
dvmFprintf(stderr, " -XmsN (min heap, must be multiple of 1K, >= 1MB)\n");
dvmFprintf(stderr, " -XmxN (max heap, must be multiple of 1K, >= 2MB)\n");
dvmFprintf(stderr, " -XssN (stack size, >= %dKB, <= %dKB)\n",
kMinStackSize / 1024, kMaxStackSize / 1024);
dvmFprintf(stderr, " -Xverify:{none,remote,all}\n");
dvmFprintf(stderr, " -Xrs\n");
#if defined(WITH_JIT)
dvmFprintf(stderr,
" -Xint (extended to accept ':portable', ':fast' and ':jit')\n");
#else
dvmFprintf(stderr,
" -Xint (extended to accept ':portable' and ':fast')\n");
#endif
dvmFprintf(stderr, "\n");
dvmFprintf(stderr, "These are unique to Dalvik:\n");
dvmFprintf(stderr, " -Xzygote\n");
dvmFprintf(stderr, " -Xdexopt:{none,verified,all}\n");
dvmFprintf(stderr, " -Xnoquithandler\n");
dvmFprintf(stderr,
" -Xjnigreflimit:N (must be multiple of 100, >= 200)\n");
dvmFprintf(stderr, " -Xjniopts:{warnonly,forcecopy}\n");
dvmFprintf(stderr, " -Xdeadlockpredict:{off,warn,err,abort}\n");
dvmFprintf(stderr, " -Xstacktracefile:<filename>\n");
dvmFprintf(stderr, " -Xgc:[no]precise\n");
dvmFprintf(stderr, " -Xgenregmap\n");
dvmFprintf(stderr, " -Xcheckdexsum\n");
#if defined(WITH_JIT)
dvmFprintf(stderr, " -Xincludeselectedop\n");
dvmFprintf(stderr, " -Xjitop:hexopvalue[-endvalue]"
"[,hexopvalue[-endvalue]]*\n");
dvmFprintf(stderr, " -Xincludeselectedmethod\n");
dvmFprintf(stderr, " -Xjitthreshold:decimalvalue\n");
dvmFprintf(stderr, " -Xjitblocking\n");
dvmFprintf(stderr, " -Xjitmethod:signature[,signature]* "
"(eg Ljava/lang/String\\;replace)\n");
dvmFprintf(stderr, " -Xjitcheckcg\n");
dvmFprintf(stderr, " -Xjitverbose\n");
dvmFprintf(stderr, " -Xjitprofile\n");
dvmFprintf(stderr, " -Xjitdisableopt\n");
#endif
dvmFprintf(stderr, "\n");
dvmFprintf(stderr, "Configured with:"
#ifdef WITH_DEBUGGER
" debugger"
#endif
#ifdef WITH_PROFILER
" profiler"
#endif
#ifdef WITH_MONITOR_TRACKING
" monitor_tracking"
#endif
#ifdef WITH_DEADLOCK_PREDICTION
" deadlock_prediction"
#endif
#ifdef WITH_HPROF
" hprof"
#endif
#ifdef WITH_HPROF_STACK
" hprof_stack"
#endif
#ifdef WITH_HPROF_STACK_UNREACHABLE
" hprof_stack_unreachable"
#endif
#ifdef WITH_ALLOC_LIMITS
" alloc_limits"
#endif
#ifdef WITH_TRACKREF_CHECKS
" trackref_checks"
#endif
#ifdef WITH_INSTR_CHECKS
" instr_checks"
#endif
#ifdef WITH_EXTRA_OBJECT_VALIDATION
" extra_object_validation"
#endif
#ifdef WITH_EXTRA_GC_CHECKS
" extra_gc_checks"
#endif
#ifdef WITH_DALVIK_ASSERT
" dalvik_assert"
#endif
#ifdef WITH_JNI_STACK_CHECK
" jni_stack_check"
#endif
#ifdef EASY_GDB
" easy_gdb"
#endif
#ifdef CHECK_MUTEX
" check_mutex"
#endif
#ifdef PROFILE_FIELD_ACCESS
" profile_field_access"
#endif
#ifdef DVM_TRACK_HEAP_MARKING
" track_heap_marking"
#endif
#if DVM_RESOLVER_CACHE == DVM_RC_REDUCING
" resolver_cache_reducing"
#elif DVM_RESOLVER_CACHE == DVM_RC_EXPANDING
" resolver_cache_expanding"
#elif DVM_RESOLVER_CACHE == DVM_RC_NO_CACHE
" resolver_cache_disabled"
#endif
#if defined(WITH_JIT)
" jit"
#endif
#if defined(WITH_SELF_VERIFICATION)
" self_verification"
#endif
);
#ifdef DVM_SHOW_EXCEPTION
dvmFprintf(stderr, " show_exception=%d", DVM_SHOW_EXCEPTION);
#endif
dvmFprintf(stderr, "\n\n");
}
/*
* Show helpful information on JDWP options.
*/
static void showJdwpHelp(void)
{
dvmFprintf(stderr,
"Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n");
dvmFprintf(stderr,
"Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n\n");
}
/*
* Show version and copyright info.
*/
static void showVersion(void)
{
dvmFprintf(stdout, "DalvikVM version %d.%d.%d\n",
DALVIK_MAJOR_VERSION, DALVIK_MINOR_VERSION, DALVIK_BUG_VERSION);
dvmFprintf(stdout,
"Copyright (C) 2007 The Android Open Source Project\n\n"
"This software is built from source code licensed under the "
"Apache License,\n"
"Version 2.0 (the \"License\"). You may obtain a copy of the "
"License at\n\n"
" http://www.apache.org/licenses/LICENSE-2.0\n\n"
"See the associated NOTICE file for this software for further "
"details.\n");
}
/*
* Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
* memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
* [gG] gigabytes.
*
* "s" should point just past the "-Xm?" part of the string.
* "min" specifies the lowest acceptable value described by "s".
* "div" specifies a divisor, e.g. 1024 if the value must be a multiple
* of 1024.
*
* The spec says the -Xmx and -Xms options must be multiples of 1024. It
* doesn't say anything about -Xss.
*
* Returns 0 (a useless size) if "s" is malformed or specifies a low or
* non-evenly-divisible value.
*/
static unsigned int dvmParseMemOption(const char *s, unsigned int div)
{
/* strtoul accepts a leading [+-], which we don't want,
* so make sure our string starts with a decimal digit.
*/
if (isdigit(*s)) {
const char *s2;
unsigned int val;
val = (unsigned int)strtoul(s, (char **)&s2, 10);
if (s2 != s) {
/* s2 should be pointing just after the number.
* If this is the end of the string, the user
* has specified a number of bytes. Otherwise,
* there should be exactly one more character
* that specifies a multiplier.
*/
if (*s2 != '\0') {
char c;
/* The remainder of the string is either a single multiplier
* character, or nothing to indicate that the value is in
* bytes.
*/
c = *s2++;
if (*s2 == '\0') {
unsigned int mul;
if (c == '\0') {
mul = 1;
} else if (c == 'k' || c == 'K') {
mul = 1024;
} else if (c == 'm' || c == 'M') {
mul = 1024 * 1024;
} else if (c == 'g' || c == 'G') {
mul = 1024 * 1024 * 1024;
} else {
/* Unknown multiplier character.
*/
return 0;
}
if (val <= UINT_MAX / mul) {
val *= mul;
} else {
/* Clamp to a multiple of 1024.
*/
val = UINT_MAX & ~(1024-1);
}
} else {
/* There's more than one character after the
* numeric part.
*/
return 0;
}
}
/* The man page says that a -Xm value must be
* a multiple of 1024.
*/
if (val % div == 0) {
return val;
}
}
}
return 0;
}
/*
* Handle one of the JDWP name/value pairs.
*
* JDWP options are:
* help: if specified, show help message and bail
* transport: may be dt_socket or dt_shmem
* address: for dt_socket, "host:port", or just "port" when listening
* server: if "y", wait for debugger to attach; if "n", attach to debugger
* timeout: how long to wait for debugger to connect / listen
*
* Useful with server=n (these aren't supported yet):
* onthrow=<exception-name>: connect to debugger when exception thrown
* onuncaught=y|n: connect to debugger when uncaught exception thrown
* launch=<command-line>: launch the debugger itself
*
* The "transport" option is required, as is "address" if server=n.
*/
static bool handleJdwpOption(const char* name, const char* value)
{
if (strcmp(name, "transport") == 0) {
if (strcmp(value, "dt_socket") == 0) {
gDvm.jdwpTransport = kJdwpTransportSocket;
} else if (strcmp(value, "dt_android_adb") == 0) {
gDvm.jdwpTransport = kJdwpTransportAndroidAdb;
} else {
LOGE("JDWP transport '%s' not supported\n", value);
return false;
}
} else if (strcmp(name, "server") == 0) {
if (*value == 'n')
gDvm.jdwpServer = false;
else if (*value == 'y')
gDvm.jdwpServer = true;
else {
LOGE("JDWP option 'server' must be 'y' or 'n'\n");
return false;
}
} else if (strcmp(name, "suspend") == 0) {
if (*value == 'n')
gDvm.jdwpSuspend = false;
else if (*value == 'y')
gDvm.jdwpSuspend = true;
else {
LOGE("JDWP option 'suspend' must be 'y' or 'n'\n");
return false;
}
} else if (strcmp(name, "address") == 0) {
/* this is either <port> or <host>:<port> */
const char* colon = strchr(value, ':');
char* end;
long port;
if (colon != NULL) {
free(gDvm.jdwpHost);
gDvm.jdwpHost = (char*) malloc(colon - value +1);
strncpy(gDvm.jdwpHost, value, colon - value +1);
gDvm.jdwpHost[colon-value] = '\0';
value = colon + 1;
}
if (*value == '\0') {
LOGE("JDWP address missing port\n");
return false;
}
port = strtol(value, &end, 10);
if (*end != '\0') {
LOGE("JDWP address has junk in port field '%s'\n", value);
return false;
}
gDvm.jdwpPort = port;
} else if (strcmp(name, "launch") == 0 ||
strcmp(name, "onthrow") == 0 ||
strcmp(name, "oncaught") == 0 ||
strcmp(name, "timeout") == 0)
{
/* valid but unsupported */
LOGI("Ignoring JDWP option '%s'='%s'\n", name, value);
} else {
LOGI("Ignoring unrecognized JDWP option '%s'='%s'\n", name, value);
}
return true;
}
/*
* Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
* "transport=dt_socket,address=8000,server=y,suspend=n"
*/
static bool parseJdwpOptions(const char* str)
{
char* mangle = strdup(str);
char* name = mangle;
bool result = false;
/*
* Process all of the name=value pairs.
*/
while (true) {
char* value;
char* comma;
value = strchr(name, '=');
if (value == NULL) {
LOGE("JDWP opts: garbage at '%s'\n", name);
goto bail;
}
comma = strchr(name, ','); // use name, not value, for safety
if (comma != NULL) {
if (comma < value) {
LOGE("JDWP opts: found comma before '=' in '%s'\n", mangle);
goto bail;
}
*comma = '\0';
}
*value++ = '\0'; // stomp the '='
if (!handleJdwpOption(name, value))
goto bail;
if (comma == NULL) {
/* out of options */
break;
}
name = comma+1;
}
/*
* Make sure the combination of arguments makes sense.
*/
if (gDvm.jdwpTransport == kJdwpTransportUnknown) {
LOGE("JDWP opts: must specify transport\n");
goto bail;
}
if (!gDvm.jdwpServer && (gDvm.jdwpHost == NULL || gDvm.jdwpPort == 0)) {
LOGE("JDWP opts: when server=n, must specify host and port\n");
goto bail;
}
// transport mandatory
// outbound server address
gDvm.jdwpConfigured = true;
result = true;
bail:
free(mangle);
return result;
}
/*
* Handle one of the four kinds of assertion arguments.
*
* "pkgOrClass" is the last part of an enable/disable line. For a package
* the arg looks like "-ea:com.google.fubar...", for a class it looks
* like "-ea:com.google.fubar.Wahoo". The string we get starts at the ':'.
*
* For system assertions (-esa/-dsa), "pkgOrClass" is NULL.
*
* Multiple instances of these arguments can be specified, e.g. you can
* enable assertions for a package and then disable them for one class in
* the package.
*/
static bool enableAssertions(const char* pkgOrClass, bool enable)
{
AssertionControl* pCtrl = &gDvm.assertionCtrl[gDvm.assertionCtrlCount++];
pCtrl->enable = enable;
if (pkgOrClass == NULL) {
/* enable or disable for all system classes */
pCtrl->isPackage = false;
pCtrl->pkgOrClass = NULL;
pCtrl->pkgOrClassLen = 0;
} else {
if (*pkgOrClass == '\0') {
/* global enable/disable for all but system */
pCtrl->isPackage = false;
pCtrl->pkgOrClass = strdup("");
pCtrl->pkgOrClassLen = 0;
} else {
pCtrl->pkgOrClass = dvmDotToSlash(pkgOrClass+1); // skip ':'
if (pCtrl->pkgOrClass == NULL) {
/* can happen if class name includes an illegal '/' */
LOGW("Unable to process assertion arg '%s'\n", pkgOrClass);
return false;
}
int len = strlen(pCtrl->pkgOrClass);
if (len >= 3 && strcmp(pCtrl->pkgOrClass + len-3, "///") == 0) {
/* mark as package, truncate two of the three slashes */
pCtrl->isPackage = true;
*(pCtrl->pkgOrClass + len-2) = '\0';
pCtrl->pkgOrClassLen = len - 2;
} else {
/* just a class */
pCtrl->isPackage = false;
pCtrl->pkgOrClassLen = len;
}
}
}
return true;
}
/*
* Turn assertions on when requested to do so by the Zygote.
*
* This is a bit sketchy. We can't (easily) go back and fiddle with all
* of the classes that have already been initialized, so this only
* affects classes that have yet to be loaded. If some or all assertions
* have been enabled through some other means, we don't want to mess with
* it here, so we do nothing. Finally, we assume that there's room in
* "assertionCtrl" to hold at least one entry; this is guaranteed by the
* allocator.
*
* This must only be called from the main thread during zygote init.
*/
void dvmLateEnableAssertions(void)
{
if (gDvm.assertionCtrl == NULL) {
LOGD("Not late-enabling assertions: no assertionCtrl array\n");
return;
} else if (gDvm.assertionCtrlCount != 0) {
LOGD("Not late-enabling assertions: some asserts already configured\n");
return;
}
LOGD("Late-enabling assertions\n");
/* global enable for all but system */
AssertionControl* pCtrl = gDvm.assertionCtrl;
pCtrl->pkgOrClass = strdup("");
pCtrl->pkgOrClassLen = 0;
pCtrl->isPackage = false;
pCtrl->enable = true;
gDvm.assertionCtrlCount = 1;
}
/*
* Release memory associated with the AssertionCtrl array.
*/
static void freeAssertionCtrl(void)
{
int i;
for (i = 0; i < gDvm.assertionCtrlCount; i++)
free(gDvm.assertionCtrl[i].pkgOrClass);
free(gDvm.assertionCtrl);
}
#if defined(WITH_JIT)
/* Parse -Xjitop to selectively turn on/off certain opcodes for JIT */
static void processXjitop(const char *opt)
{
if (opt[7] == ':') {
const char *startPtr = &opt[8];
char *endPtr = NULL;
do {
long startValue, endValue;
startValue = strtol(startPtr, &endPtr, 16);
if (startPtr != endPtr) {
/* Just in case value is out of range */
startValue &= 0xff;
if (*endPtr == '-') {
endValue = strtol(endPtr+1, &endPtr, 16);
endValue &= 0xff;
} else {
endValue = startValue;
}
for (; startValue <= endValue; startValue++) {
LOGW("Dalvik opcode %x is selected for debugging",
(unsigned int) startValue);
/* Mark the corresponding bit to 1 */
gDvmJit.opList[startValue >> 3] |=
1 << (startValue & 0x7);
}
if (*endPtr == 0) {
break;
}
startPtr = endPtr + 1;
continue;
} else {
if (*endPtr != 0) {
dvmFprintf(stderr,
"Warning: Unrecognized opcode value substring "
"%s\n", endPtr);
}
break;
}
} while (1);
} else {
int i;
for (i = 0; i < 32; i++) {
gDvmJit.opList[i] = 0xff;
}
dvmFprintf(stderr, "Warning: select all opcodes\n");
}
}
/* Parse -Xjitmethod to selectively turn on/off certain methods for JIT */
static void processXjitmethod(const char *opt)
{
char *buf = strdup(&opt[12]);
char *start, *end;
gDvmJit.methodTable = dvmHashTableCreate(8, NULL);
start = buf;
/*
* Break comma-separated method signatures and enter them into the hash
* table individually.
*/
do {
int hashValue;
end = strchr(start, ',');
if (end) {
*end = 0;
}
hashValue = dvmComputeUtf8Hash(start);
dvmHashTableLookup(gDvmJit.methodTable, hashValue,
strdup(start),
(HashCompareFunc) strcmp, true);
if (end) {
start = end + 1;
} else {
break;
}
} while (1);
free(buf);
}
#endif
/*
* Process an argument vector full of options. Unlike standard C programs,
* argv[0] does not contain the name of the program.
*
* If "ignoreUnrecognized" is set, we ignore options starting with "-X" or "_"
* that we don't recognize. Otherwise, we return with an error as soon as
* we see anything we can't identify.
*
* Returns 0 on success, -1 on failure, and 1 for the special case of
* "-version" where we want to stop without showing an error message.
*/
static int dvmProcessOptions(int argc, const char* const argv[],
bool ignoreUnrecognized)
{
int i;
LOGV("VM options (%d):\n", argc);
for (i = 0; i < argc; i++)
LOGV(" %d: '%s'\n", i, argv[i]);
/*
* Over-allocate AssertionControl array for convenience. If allocated,
* the array must be able to hold at least one entry, so that the
* zygote-time activation can do its business.
*/
assert(gDvm.assertionCtrl == NULL);
if (argc > 0) {
gDvm.assertionCtrl =
(AssertionControl*) malloc(sizeof(AssertionControl) * argc);
if (gDvm.assertionCtrl == NULL)
return -1;
assert(gDvm.assertionCtrlCount == 0);
}
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "-help") == 0) {
/* show usage and stop */
return -1;
} else if (strcmp(argv[i], "-version") == 0) {
/* show version and stop */
showVersion();
return 1;
} else if (strcmp(argv[i], "-showversion") == 0) {
/* show version and continue */
showVersion();
} else if (strcmp(argv[i], "-classpath") == 0 ||
strcmp(argv[i], "-cp") == 0)
{
/* set classpath */
if (i == argc-1) {
dvmFprintf(stderr, "Missing classpath path list\n");
return -1;
}
free(gDvm.classPathStr); /* in case we have compiled-in default */
gDvm.classPathStr = strdup(argv[++i]);
} else if (strncmp(argv[i], "-Xbootclasspath:",
sizeof("-Xbootclasspath:")-1) == 0)
{
/* set bootclasspath */
const char* path = argv[i] + sizeof("-Xbootclasspath:")-1;
if (*path == '\0') {
dvmFprintf(stderr, "Missing bootclasspath path list\n");
return -1;
}
free(gDvm.bootClassPathStr);
gDvm.bootClassPathStr = strdup(path);
/*
* TODO: support -Xbootclasspath/a and /p, which append or
* prepend to the default bootclasspath. We set the default
* path earlier.
*/
} else if (strncmp(argv[i], "-D", 2) == 0) {
/* set property */
dvmAddCommandLineProperty(argv[i] + 2);
} else if (strcmp(argv[i], "-jar") == 0) {
// TODO: handle this; name of jar should be in argv[i+1]
dvmFprintf(stderr, "-jar not yet handled\n");
assert(false);
} else if (strncmp(argv[i], "-Xms", 4) == 0) {
unsigned int val = dvmParseMemOption(argv[i]+4, 1024);
if (val != 0) {
if (val >= kMinHeapStartSize && val <= kMaxHeapSize) {
gDvm.heapSizeStart = val;
} else {
dvmFprintf(stderr,
"Invalid -Xms '%s', range is %dKB to %dKB\n",
argv[i], kMinHeapStartSize/1024, kMaxHeapSize/1024);
return -1;
}
} else {
dvmFprintf(stderr, "Invalid -Xms option '%s'\n", argv[i]);
return -1;
}
} else if (strncmp(argv[i], "-Xmx", 4) == 0) {
unsigned int val = dvmParseMemOption(argv[i]+4, 1024);
if (val != 0) {
if (val >= kMinHeapSize && val <= kMaxHeapSize) {
gDvm.heapSizeMax = val;
} else {
dvmFprintf(stderr,
"Invalid -Xmx '%s', range is %dKB to %dKB\n",
argv[i], kMinHeapSize/1024, kMaxHeapSize/1024);
return -1;
}
} else {
dvmFprintf(stderr, "Invalid -Xmx option '%s'\n", argv[i]);
return -1;
}
} else if (strncmp(argv[i], "-Xss", 4) == 0) {
unsigned int val = dvmParseMemOption(argv[i]+4, 1);
if (val != 0) {
if (val >= kMinStackSize && val <= kMaxStackSize) {
gDvm.stackSize = val;
} else {
dvmFprintf(stderr, "Invalid -Xss '%s', range is %d to %d\n",
argv[i], kMinStackSize, kMaxStackSize);
return -1;
}
} else {
dvmFprintf(stderr, "Invalid -Xss option '%s'\n", argv[i]);
return -1;
}
} else if (strcmp(argv[i], "-verbose") == 0 ||
strcmp(argv[i], "-verbose:class") == 0)
{
// JNI spec says "-verbose:gc,class" is valid, but cmd line
// doesn't work that way; may want to support.
gDvm.verboseClass = true;
} else if (strcmp(argv[i], "-verbose:jni") == 0) {
gDvm.verboseJni = true;
} else if (strcmp(argv[i], "-verbose:gc") == 0) {
gDvm.verboseGc = true;
} else if (strcmp(argv[i], "-verbose:shutdown") == 0) {
gDvm.verboseShutdown = true;
} else if (strncmp(argv[i], "-enableassertions", 17) == 0) {
enableAssertions(argv[i] + 17, true);
} else if (strncmp(argv[i], "-ea", 3) == 0) {
enableAssertions(argv[i] + 3, true);
} else if (strncmp(argv[i], "-disableassertions", 18) == 0) {
enableAssertions(argv[i] + 18, false);
} else if (strncmp(argv[i], "-da", 3) == 0) {
enableAssertions(argv[i] + 3, false);
} else if (strcmp(argv[i], "-enablesystemassertions") == 0 ||
strcmp(argv[i], "-esa") == 0)
{
enableAssertions(NULL, true);
} else if (strcmp(argv[i], "-disablesystemassertions") == 0 ||
strcmp(argv[i], "-dsa") == 0)
{
enableAssertions(NULL, false);
} else if (strncmp(argv[i], "-Xcheck:jni", 11) == 0) {
/* nothing to do now -- was handled during JNI init */
} else if (strcmp(argv[i], "-Xdebug") == 0) {
/* accept but ignore */
} else if (strncmp(argv[i], "-Xrunjdwp:", 10) == 0 ||
strncmp(argv[i], "-agentlib:jdwp=", 15) == 0)
{
const char* tail;
bool result = false;
if (argv[i][1] == 'X')
tail = argv[i] + 10;
else
tail = argv[i] + 15;
if (strncmp(tail, "help", 4) == 0 || !parseJdwpOptions(tail)) {
showJdwpHelp();
return 1;
}
} else if (strcmp(argv[i], "-Xrs") == 0) {
gDvm.reduceSignals = true;
} else if (strcmp(argv[i], "-Xnoquithandler") == 0) {
/* disables SIGQUIT handler thread while still blocking SIGQUIT */
/* (useful if we don't want thread but system still signals us) */
gDvm.noQuitHandler = true;
} else if (strcmp(argv[i], "-Xzygote") == 0) {
gDvm.zygote = true;
#if defined(WITH_JIT)
gDvmJit.runningInAndroidFramework = true;
#endif
} else if (strncmp(argv[i], "-Xdexopt:", 9) == 0) {
if (strcmp(argv[i] + 9, "none") == 0)
gDvm.dexOptMode = OPTIMIZE_MODE_NONE;
else if (strcmp(argv[i] + 9, "verified") == 0)
gDvm.dexOptMode = OPTIMIZE_MODE_VERIFIED;
else if (strcmp(argv[i] + 9, "all") == 0)
gDvm.dexOptMode = OPTIMIZE_MODE_ALL;
else {
dvmFprintf(stderr, "Unrecognized dexopt option '%s'\n",argv[i]);
return -1;
}
} else if (strncmp(argv[i], "-Xverify:", 9) == 0) {
if (strcmp(argv[i] + 9, "none") == 0)
gDvm.classVerifyMode = VERIFY_MODE_NONE;
else if (strcmp(argv[i] + 9, "remote") == 0)
gDvm.classVerifyMode = VERIFY_MODE_REMOTE;
else if (strcmp(argv[i] + 9, "all") == 0)
gDvm.classVerifyMode = VERIFY_MODE_ALL;
else {
dvmFprintf(stderr, "Unrecognized verify option '%s'\n",argv[i]);
return -1;
}
} else if (strncmp(argv[i], "-Xjnigreflimit:", 15) == 0) {
int lim = atoi(argv[i] + 15);
if (lim < 200 || (lim % 100) != 0) {
dvmFprintf(stderr, "Bad value for -Xjnigreflimit: '%s'\n",
argv[i]+15);
return -1;
}
gDvm.jniGrefLimit = lim;
} else if (strcmp(argv[i], "-Xlog-stdio") == 0) {
gDvm.logStdio = true;
} else if (strncmp(argv[i], "-Xint", 5) == 0) {
if (argv[i][5] == ':') {
if (strcmp(argv[i] + 6, "portable") == 0)
gDvm.executionMode = kExecutionModeInterpPortable;
else if (strcmp(argv[i] + 6, "fast") == 0)
gDvm.executionMode = kExecutionModeInterpFast;
#ifdef WITH_JIT
else if (strcmp(argv[i] + 6, "jit") == 0)
gDvm.executionMode = kExecutionModeJit;
#endif
else {
dvmFprintf(stderr,
"Warning: Unrecognized interpreter mode %s\n",argv[i]);
/* keep going */
}
} else {
/* disable JIT -- nothing to do here for now */
}
} else if (strncmp(argv[i], "-Xlockprofthreshold:", 20) == 0) {
gDvm.lockProfThreshold = atoi(argv[i] + 20);
#ifdef WITH_JIT
} else if (strncmp(argv[i], "-Xjitop", 7) == 0) {
processXjitop(argv[i]);
} else if (strncmp(argv[i], "-Xjitmethod", 11) == 0) {
processXjitmethod(argv[i]);
} else if (strncmp(argv[i], "-Xjitblocking", 13) == 0) {
gDvmJit.blockingMode = true;
} else if (strncmp(argv[i], "-Xjitthreshold:", 15) == 0) {
gDvmJit.threshold = atoi(argv[i] + 15);
} else if (strncmp(argv[i], "-Xincludeselectedop", 19) == 0) {
gDvmJit.includeSelectedOp = true;
} else if (strncmp(argv[i], "-Xincludeselectedmethod", 23) == 0) {
gDvmJit.includeSelectedMethod = true;
} else if (strncmp(argv[i], "-Xjitcheckcg", 12) == 0) {
gDvmJit.checkCallGraph = true;
/* Need to enable blocking mode due to stack crawling */
gDvmJit.blockingMode = true;
} else if (strncmp(argv[i], "-Xjitverbose", 12) == 0) {
gDvmJit.printMe = true;
} else if (strncmp(argv[i], "-Xjitprofile", 12) == 0) {
gDvmJit.profile = true;
} else if (strncmp(argv[i], "-Xjitdisableopt", 15) == 0) {
/* Disable selected optimizations */
if (argv[i][15] == ':') {
sscanf(argv[i] + 16, "%x", &gDvmJit.disableOpt);
/* Disable all optimizations */
} else {
gDvmJit.disableOpt = -1;
}
#endif
} else if (strncmp(argv[i], "-Xdeadlockpredict:", 18) == 0) {
#ifdef WITH_DEADLOCK_PREDICTION
if (strcmp(argv[i] + 18, "off") == 0)
gDvm.deadlockPredictMode = kDPOff;
else if (strcmp(argv[i] + 18, "warn") == 0)
gDvm.deadlockPredictMode = kDPWarn;
else if (strcmp(argv[i] + 18, "err") == 0)
gDvm.deadlockPredictMode = kDPErr;
else if (strcmp(argv[i] + 18, "abort") == 0)
gDvm.deadlockPredictMode = kDPAbort;
else {
dvmFprintf(stderr, "Bad value for -Xdeadlockpredict");
return -1;
}
if (gDvm.deadlockPredictMode != kDPOff)
LOGD("Deadlock prediction enabled (%s)\n", argv[i]+18);
#endif
} else if (strncmp(argv[i], "-Xstacktracefile:", 17) == 0) {
gDvm.stackTraceFile = strdup(argv[i]+17);
} else if (strcmp(argv[i], "-Xgenregmap") == 0) {
gDvm.generateRegisterMaps = true;
LOGV("Register maps will be generated during verification\n");
} else if (strncmp(argv[i], "-Xgc:", 5) == 0) {
if (strcmp(argv[i] + 5, "precise") == 0)
gDvm.preciseGc = true;
else if (strcmp(argv[i] + 5, "noprecise") == 0)
gDvm.preciseGc = false;
else {
dvmFprintf(stderr, "Bad value for -Xgc");
return -1;
}
LOGV("Precise GC configured %s\n", gDvm.preciseGc ? "ON" : "OFF");
} else if (strcmp(argv[i], "-Xcheckdexsum") == 0) {
gDvm.verifyDexChecksum = true;
} else {
if (!ignoreUnrecognized) {
dvmFprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
return -1;
}
}
}
if (gDvm.heapSizeStart > gDvm.heapSizeMax) {
dvmFprintf(stderr, "Heap start size must be <= heap max size\n");
return -1;
}
return 0;
}
/*
* Set defaults for fields altered or modified by arguments.
*
* Globals are initialized to 0 (a/k/a NULL or false).
*/
static void setCommandLineDefaults()
{
const char* envStr;