-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdexdump.cc
1900 lines (1776 loc) · 67 KB
/
dexdump.cc
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) 2015 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.
*
* Implementation file of the dexdump utility.
*
* This is a re-implementation of the original dexdump utility that was
* based on Dalvik functions in libdex into a new dexdump that is now
* based on Art functions in libart instead. The output is very similar to
* to the original for correct DEX files. Error messages may differ, however.
* Also, ODEX files are no longer supported.
*
* The dexdump tool is intended to mimic objdump. When possible, use
* similar command-line arguments.
*
* Differences between XML output and the "current.xml" file:
* - classes in same package are not all grouped together; nothing is sorted
* - no "deprecated" on fields and methods
* - no parameter names
* - no generic signatures on parameters, e.g. type="java.lang.Class<?>"
* - class shows declared fields and methods; does not show inherited fields
*/
#include "dexdump.h"
#include <inttypes.h>
#include <stdio.h>
#include <memory>
#include <sstream>
#include <vector>
#include "android-base/file.h"
#include "android-base/logging.h"
#include "android-base/stringprintf.h"
#include "dex/class_accessor-inl.h"
#include "dex/code_item_accessors-inl.h"
#include "dex/dex_file-inl.h"
#include "dex/dex_file_exception_helpers.h"
#include "dex/dex_file_loader.h"
#include "dex/dex_file_types.h"
#include "dex/dex_instruction-inl.h"
#include "dexdump_cfg.h"
namespace art {
/*
* Options parsed in main driver.
*/
struct Options gOptions;
/*
* Output file. Defaults to stdout.
*/
FILE* gOutFile = stdout;
/*
* Data types that match the definitions in the VM specification.
*/
using u1 = uint8_t;
using u2 = uint16_t;
using u4 = uint32_t;
using u8 = uint64_t;
using s1 = int8_t;
using s2 = int16_t;
using s4 = int32_t;
using s8 = int64_t;
/*
* Basic information about a field or a method.
*/
struct FieldMethodInfo {
const char* classDescriptor;
const char* name;
const char* signature;
};
/*
* Flags for use with createAccessFlagStr().
*/
enum AccessFor {
kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
};
const int kNumFlags = 18;
/*
* Gets 2 little-endian bytes.
*/
static inline u2 get2LE(unsigned char const* pSrc) {
return pSrc[0] | (pSrc[1] << 8);
}
/*
* Converts a single-character primitive type into human-readable form.
*/
static const char* primitiveTypeLabel(char typeChar) {
switch (typeChar) {
case 'B': return "byte";
case 'C': return "char";
case 'D': return "double";
case 'F': return "float";
case 'I': return "int";
case 'J': return "long";
case 'S': return "short";
case 'V': return "void";
case 'Z': return "boolean";
default: return "UNKNOWN";
} // switch
}
/*
* Converts a type descriptor to human-readable "dotted" form. For
* example, "Ljava/lang/String;" becomes "java.lang.String", and
* "[I" becomes "int[]".
*/
static std::unique_ptr<char[]> descriptorToDot(const char* str) {
int targetLen = strlen(str);
int offset = 0;
// Strip leading [s; will be added to end.
while (targetLen > 1 && str[offset] == '[') {
offset++;
targetLen--;
} // while
const int arrayDepth = offset;
if (targetLen == 1) {
// Primitive type.
str = primitiveTypeLabel(str[offset]);
offset = 0;
targetLen = strlen(str);
} else {
// Account for leading 'L' and trailing ';'.
if (targetLen >= 2 && str[offset] == 'L' &&
str[offset + targetLen - 1] == ';') {
targetLen -= 2;
offset++;
}
}
// Copy class name over.
std::unique_ptr<char[]> newStr(new char[targetLen + arrayDepth * 2 + 1]);
int i = 0;
for (; i < targetLen; i++) {
const char ch = str[offset + i];
newStr[i] = (ch == '/') ? '.' : ch;
} // for
// Add the appropriate number of brackets for arrays.
for (int j = 0; j < arrayDepth; j++) {
newStr[i++] = '[';
newStr[i++] = ']';
} // for
newStr[i] = '\0';
return newStr;
}
/*
* Retrieves the class name portion of a type descriptor.
*/
static std::unique_ptr<char[]> descriptorClassToName(const char* str) {
// Reduce to just the class name prefix.
const char* lastSlash = strrchr(str, '/');
if (lastSlash == nullptr) {
lastSlash = str + 1; // start past 'L'
} else {
lastSlash++; // start past '/'
}
// Copy class name over, trimming trailing ';'.
const int targetLen = strlen(lastSlash);
std::unique_ptr<char[]> newStr(new char[targetLen]);
for (int i = 0; i < targetLen - 1; i++) {
newStr[i] = lastSlash[i];
} // for
newStr[targetLen - 1] = '\0';
return newStr;
}
/*
* Returns string representing the boolean value.
*/
static const char* strBool(bool val) {
return val ? "true" : "false";
}
/*
* Returns a quoted string representing the boolean value.
*/
static const char* quotedBool(bool val) {
return val ? "\"true\"" : "\"false\"";
}
/*
* Returns a quoted string representing the access flags.
*/
static const char* quotedVisibility(u4 accessFlags) {
if (accessFlags & kAccPublic) {
return "\"public\"";
} else if (accessFlags & kAccProtected) {
return "\"protected\"";
} else if (accessFlags & kAccPrivate) {
return "\"private\"";
} else {
return "\"package\"";
}
}
/*
* Counts the number of '1' bits in a word.
*/
static int countOnes(u4 val) {
val = val - ((val >> 1) & 0x55555555);
val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
/*
* Creates a new string with human-readable access flags.
*
* In the base language the access_flags fields are type u2; in Dalvik
* they're u4.
*/
static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
{
"PUBLIC", /* 0x00001 */
"PRIVATE", /* 0x00002 */
"PROTECTED", /* 0x00004 */
"STATIC", /* 0x00008 */
"FINAL", /* 0x00010 */
"?", /* 0x00020 */
"?", /* 0x00040 */
"?", /* 0x00080 */
"?", /* 0x00100 */
"INTERFACE", /* 0x00200 */
"ABSTRACT", /* 0x00400 */
"?", /* 0x00800 */
"SYNTHETIC", /* 0x01000 */
"ANNOTATION", /* 0x02000 */
"ENUM", /* 0x04000 */
"?", /* 0x08000 */
"VERIFIED", /* 0x10000 */
"OPTIMIZED", /* 0x20000 */
}, {
"PUBLIC", /* 0x00001 */
"PRIVATE", /* 0x00002 */
"PROTECTED", /* 0x00004 */
"STATIC", /* 0x00008 */
"FINAL", /* 0x00010 */
"SYNCHRONIZED", /* 0x00020 */
"BRIDGE", /* 0x00040 */
"VARARGS", /* 0x00080 */
"NATIVE", /* 0x00100 */
"?", /* 0x00200 */
"ABSTRACT", /* 0x00400 */
"STRICT", /* 0x00800 */
"SYNTHETIC", /* 0x01000 */
"?", /* 0x02000 */
"?", /* 0x04000 */
"MIRANDA", /* 0x08000 */
"CONSTRUCTOR", /* 0x10000 */
"DECLARED_SYNCHRONIZED", /* 0x20000 */
}, {
"PUBLIC", /* 0x00001 */
"PRIVATE", /* 0x00002 */
"PROTECTED", /* 0x00004 */
"STATIC", /* 0x00008 */
"FINAL", /* 0x00010 */
"?", /* 0x00020 */
"VOLATILE", /* 0x00040 */
"TRANSIENT", /* 0x00080 */
"?", /* 0x00100 */
"?", /* 0x00200 */
"?", /* 0x00400 */
"?", /* 0x00800 */
"SYNTHETIC", /* 0x01000 */
"?", /* 0x02000 */
"ENUM", /* 0x04000 */
"?", /* 0x08000 */
"?", /* 0x10000 */
"?", /* 0x20000 */
},
};
// Allocate enough storage to hold the expected number of strings,
// plus a space between each. We over-allocate, using the longest
// string above as the base metric.
const int kLongest = 21; // The strlen of longest string above.
const int count = countOnes(flags);
char* str;
char* cp;
cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
for (int i = 0; i < kNumFlags; i++) {
if (flags & 0x01) {
const char* accessStr = kAccessStrings[forWhat][i];
const int len = strlen(accessStr);
if (cp != str) {
*cp++ = ' ';
}
memcpy(cp, accessStr, len);
cp += len;
}
flags >>= 1;
} // for
*cp = '\0';
return str;
}
/*
* Copies character data from "data" to "out", converting non-ASCII values
* to fprintf format chars or an ASCII filler ('.' or '?').
*
* The output buffer must be able to hold (2*len)+1 bytes. The result is
* NULL-terminated.
*/
static void asciify(char* out, const unsigned char* data, size_t len) {
for (; len != 0u; --len) {
if (*data < 0x20) {
// Could do more here, but we don't need them yet.
switch (*data) {
case '\0':
*out++ = '\\';
*out++ = '0';
break;
case '\n':
*out++ = '\\';
*out++ = 'n';
break;
default:
*out++ = '.';
break;
} // switch
} else if (*data >= 0x80) {
*out++ = '?';
} else {
*out++ = *data;
}
data++;
} // while
*out = '\0';
}
/*
* Dumps a string value with some escape characters.
*/
static void dumpEscapedString(const char* p) {
fputs("\"", gOutFile);
for (; *p; p++) {
switch (*p) {
case '\\':
fputs("\\\\", gOutFile);
break;
case '\"':
fputs("\\\"", gOutFile);
break;
case '\t':
fputs("\\t", gOutFile);
break;
case '\n':
fputs("\\n", gOutFile);
break;
case '\r':
fputs("\\r", gOutFile);
break;
default:
putc(*p, gOutFile);
} // switch
} // for
fputs("\"", gOutFile);
}
/*
* Dumps a string as an XML attribute value.
*/
static void dumpXmlAttribute(const char* p) {
for (; *p; p++) {
switch (*p) {
case '&':
fputs("&", gOutFile);
break;
case '<':
fputs("<", gOutFile);
break;
case '>':
fputs(">", gOutFile);
break;
case '"':
fputs(""", gOutFile);
break;
case '\t':
fputs("	", gOutFile);
break;
case '\n':
fputs("
", gOutFile);
break;
case '\r':
fputs("
", gOutFile);
break;
default:
putc(*p, gOutFile);
} // switch
} // for
}
/*
* Reads variable width value, possibly sign extended at the last defined byte.
*/
static u8 readVarWidth(const u1** data, u1 arg, bool sign_extend) {
u8 value = 0;
for (u4 i = 0; i <= arg; i++) {
value |= static_cast<u8>(*(*data)++) << (i * 8);
}
if (sign_extend) {
int shift = (7 - arg) * 8;
return (static_cast<s8>(value) << shift) >> shift;
}
return value;
}
/*
* Dumps encoded value.
*/
static void dumpEncodedValue(const DexFile* pDexFile, const u1** data); // forward
static void dumpEncodedValue(const DexFile* pDexFile, const u1** data, u1 type, u1 arg) {
switch (type) {
case DexFile::kDexAnnotationByte:
fprintf(gOutFile, "%" PRId8, static_cast<s1>(readVarWidth(data, arg, false)));
break;
case DexFile::kDexAnnotationShort:
fprintf(gOutFile, "%" PRId16, static_cast<s2>(readVarWidth(data, arg, true)));
break;
case DexFile::kDexAnnotationChar:
fprintf(gOutFile, "%" PRIu16, static_cast<u2>(readVarWidth(data, arg, false)));
break;
case DexFile::kDexAnnotationInt:
fprintf(gOutFile, "%" PRId32, static_cast<s4>(readVarWidth(data, arg, true)));
break;
case DexFile::kDexAnnotationLong:
fprintf(gOutFile, "%" PRId64, static_cast<s8>(readVarWidth(data, arg, true)));
break;
case DexFile::kDexAnnotationFloat: {
// Fill on right.
union {
float f;
u4 data;
} conv;
conv.data = static_cast<u4>(readVarWidth(data, arg, false)) << (3 - arg) * 8;
fprintf(gOutFile, "%g", conv.f);
break;
}
case DexFile::kDexAnnotationDouble: {
// Fill on right.
union {
double d;
u8 data;
} conv;
conv.data = readVarWidth(data, arg, false) << (7 - arg) * 8;
fprintf(gOutFile, "%g", conv.d);
break;
}
case DexFile::kDexAnnotationString: {
const u4 idx = static_cast<u4>(readVarWidth(data, arg, false));
if (gOptions.outputFormat == OUTPUT_PLAIN) {
dumpEscapedString(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
} else {
dumpXmlAttribute(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
}
break;
}
case DexFile::kDexAnnotationType: {
const u4 str_idx = static_cast<u4>(readVarWidth(data, arg, false));
fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(str_idx)), gOutFile);
break;
}
case DexFile::kDexAnnotationField:
case DexFile::kDexAnnotationEnum: {
const u4 field_idx = static_cast<u4>(readVarWidth(data, arg, false));
const dex::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
fputs(pDexFile->StringDataByIdx(pFieldId.name_idx_), gOutFile);
break;
}
case DexFile::kDexAnnotationMethod: {
const u4 method_idx = static_cast<u4>(readVarWidth(data, arg, false));
const dex::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
fputs(pDexFile->StringDataByIdx(pMethodId.name_idx_), gOutFile);
break;
}
case DexFile::kDexAnnotationArray: {
fputc('{', gOutFile);
// Decode and display all elements.
const u4 size = DecodeUnsignedLeb128(data);
for (u4 i = 0; i < size; i++) {
fputc(' ', gOutFile);
dumpEncodedValue(pDexFile, data);
}
fputs(" }", gOutFile);
break;
}
case DexFile::kDexAnnotationAnnotation: {
const u4 type_idx = DecodeUnsignedLeb128(data);
fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(type_idx)), gOutFile);
// Decode and display all name=value pairs.
const u4 size = DecodeUnsignedLeb128(data);
for (u4 i = 0; i < size; i++) {
const u4 name_idx = DecodeUnsignedLeb128(data);
fputc(' ', gOutFile);
fputs(pDexFile->StringDataByIdx(dex::StringIndex(name_idx)), gOutFile);
fputc('=', gOutFile);
dumpEncodedValue(pDexFile, data);
}
break;
}
case DexFile::kDexAnnotationNull:
fputs("null", gOutFile);
break;
case DexFile::kDexAnnotationBoolean:
fputs(strBool(arg), gOutFile);
break;
default:
fputs("????", gOutFile);
break;
} // switch
}
/*
* Dumps encoded value with prefix.
*/
static void dumpEncodedValue(const DexFile* pDexFile, const u1** data) {
const u1 enc = *(*data)++;
dumpEncodedValue(pDexFile, data, enc & 0x1f, enc >> 5);
}
/*
* Dumps the file header.
*/
static void dumpFileHeader(const DexFile* pDexFile) {
const DexFile::Header& pHeader = pDexFile->GetHeader();
char sanitized[sizeof(pHeader.magic_) * 2 + 1];
fprintf(gOutFile, "DEX file header:\n");
asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
fprintf(gOutFile, "magic : '%s'\n", sanitized);
fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
pHeader.signature_[0], pHeader.signature_[1],
pHeader.signature_[DexFile::kSha1DigestSize - 2],
pHeader.signature_[DexFile::kSha1DigestSize - 1]);
fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
fprintf(gOutFile, "link_off : %d (0x%06x)\n",
pHeader.link_off_, pHeader.link_off_);
fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
pHeader.string_ids_off_, pHeader.string_ids_off_);
fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
pHeader.type_ids_off_, pHeader.type_ids_off_);
fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
pHeader.proto_ids_off_, pHeader.proto_ids_off_);
fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
pHeader.field_ids_off_, pHeader.field_ids_off_);
fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
pHeader.method_ids_off_, pHeader.method_ids_off_);
fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
pHeader.class_defs_off_, pHeader.class_defs_off_);
fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
pHeader.data_off_, pHeader.data_off_);
}
/*
* Dumps a class_def_item.
*/
static void dumpClassDef(const DexFile* pDexFile, int idx) {
// General class information.
const dex::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
fprintf(gOutFile, "Class #%d header:\n", idx);
fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_.index_);
fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
pClassDef.access_flags_, pClassDef.access_flags_);
fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_.index_);
fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
pClassDef.interfaces_off_, pClassDef.interfaces_off_);
fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_.index_);
fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
pClassDef.annotations_off_, pClassDef.annotations_off_);
fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
pClassDef.class_data_off_, pClassDef.class_data_off_);
// Fields and methods.
ClassAccessor accessor(*pDexFile, idx);
fprintf(gOutFile, "static_fields_size : %d\n", accessor.NumStaticFields());
fprintf(gOutFile, "instance_fields_size: %d\n", accessor.NumInstanceFields());
fprintf(gOutFile, "direct_methods_size : %d\n", accessor.NumDirectMethods());
fprintf(gOutFile, "virtual_methods_size: %d\n", accessor.NumVirtualMethods());
fprintf(gOutFile, "\n");
}
/**
* Dumps an annotation set item.
*/
static void dumpAnnotationSetItem(const DexFile* pDexFile, const dex::AnnotationSetItem* set_item) {
if (set_item == nullptr || set_item->size_ == 0) {
fputs(" empty-annotation-set\n", gOutFile);
return;
}
for (u4 i = 0; i < set_item->size_; i++) {
const dex::AnnotationItem* annotation = pDexFile->GetAnnotationItem(set_item, i);
if (annotation == nullptr) {
continue;
}
fputs(" ", gOutFile);
switch (annotation->visibility_) {
case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", gOutFile); break;
case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", gOutFile); break;
case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", gOutFile); break;
default: fputs("VISIBILITY_UNKNOWN ", gOutFile); break;
} // switch
// Decode raw bytes in annotation.
const u1* rData = annotation->annotation_;
dumpEncodedValue(pDexFile, &rData, DexFile::kDexAnnotationAnnotation, 0);
fputc('\n', gOutFile);
}
}
/*
* Dumps class annotations.
*/
static void dumpClassAnnotations(const DexFile* pDexFile, int idx) {
const dex::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
const dex::AnnotationsDirectoryItem* dir = pDexFile->GetAnnotationsDirectory(pClassDef);
if (dir == nullptr) {
return; // none
}
fprintf(gOutFile, "Class #%d annotations:\n", idx);
const dex::AnnotationSetItem* class_set_item = pDexFile->GetClassAnnotationSet(dir);
const dex::FieldAnnotationsItem* fields = pDexFile->GetFieldAnnotations(dir);
const dex::MethodAnnotationsItem* methods = pDexFile->GetMethodAnnotations(dir);
const dex::ParameterAnnotationsItem* pars = pDexFile->GetParameterAnnotations(dir);
// Annotations on the class itself.
if (class_set_item != nullptr) {
fprintf(gOutFile, "Annotations on class\n");
dumpAnnotationSetItem(pDexFile, class_set_item);
}
// Annotations on fields.
if (fields != nullptr) {
for (u4 i = 0; i < dir->fields_size_; i++) {
const u4 field_idx = fields[i].field_idx_;
const dex::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
const char* field_name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
fprintf(gOutFile, "Annotations on field #%u '%s'\n", field_idx, field_name);
dumpAnnotationSetItem(pDexFile, pDexFile->GetFieldAnnotationSetItem(fields[i]));
}
}
// Annotations on methods.
if (methods != nullptr) {
for (u4 i = 0; i < dir->methods_size_; i++) {
const u4 method_idx = methods[i].method_idx_;
const dex::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
fprintf(gOutFile, "Annotations on method #%u '%s'\n", method_idx, method_name);
dumpAnnotationSetItem(pDexFile, pDexFile->GetMethodAnnotationSetItem(methods[i]));
}
}
// Annotations on method parameters.
if (pars != nullptr) {
for (u4 i = 0; i < dir->parameters_size_; i++) {
const u4 method_idx = pars[i].method_idx_;
const dex::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
fprintf(gOutFile, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
const dex::AnnotationSetRefList*
list = pDexFile->GetParameterAnnotationSetRefList(&pars[i]);
if (list != nullptr) {
for (u4 j = 0; j < list->size_; j++) {
fprintf(gOutFile, "#%u\n", j);
dumpAnnotationSetItem(pDexFile, pDexFile->GetSetRefItemItem(&list->list_[j]));
}
}
}
}
fputc('\n', gOutFile);
}
/*
* Dumps an interface that a class declares to implement.
*/
static void dumpInterface(const DexFile* pDexFile, const dex::TypeItem& pTypeItem, int i) {
const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
if (gOptions.outputFormat == OUTPUT_PLAIN) {
fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
} else {
std::unique_ptr<char[]> dot(descriptorToDot(interfaceName));
fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dot.get());
}
}
/*
* Dumps the catches table associated with the code.
*/
static void dumpCatches(const DexFile* pDexFile, const dex::CodeItem* pCode) {
CodeItemDataAccessor accessor(*pDexFile, pCode);
const u4 triesSize = accessor.TriesSize();
// No catch table.
if (triesSize == 0) {
fprintf(gOutFile, " catches : (none)\n");
return;
}
// Dump all table entries.
fprintf(gOutFile, " catches : %d\n", triesSize);
for (const dex::TryItem& try_item : accessor.TryItems()) {
const u4 start = try_item.start_addr_;
const u4 end = start + try_item.insn_count_;
fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
for (CatchHandlerIterator it(accessor, try_item); it.HasNext(); it.Next()) {
const dex::TypeIndex tidx = it.GetHandlerTypeIndex();
const char* descriptor = (!tidx.IsValid()) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
} // for
} // for
}
/*
* Helper for dumpInstruction(), which builds the string
* representation for the index in the given instruction.
* Returns a pointer to a buffer of sufficient size.
*/
static std::unique_ptr<char[]> indexString(const DexFile* pDexFile,
const Instruction* pDecInsn,
size_t bufSize) {
std::unique_ptr<char[]> buf(new char[bufSize]);
// Determine index and width of the string.
u4 index = 0;
u2 secondary_index = 0;
u4 width = 4;
switch (Instruction::FormatOf(pDecInsn->Opcode())) {
// SOME NOT SUPPORTED:
// case Instruction::k20bc:
case Instruction::k21c:
case Instruction::k35c:
// case Instruction::k35ms:
case Instruction::k3rc:
// case Instruction::k3rms:
// case Instruction::k35mi:
// case Instruction::k3rmi:
index = pDecInsn->VRegB();
width = 4;
break;
case Instruction::k31c:
index = pDecInsn->VRegB();
width = 8;
break;
case Instruction::k22c:
// case Instruction::k22cs:
index = pDecInsn->VRegC();
width = 4;
break;
case Instruction::k45cc:
case Instruction::k4rcc:
index = pDecInsn->VRegB();
secondary_index = pDecInsn->VRegH();
width = 4;
break;
default:
break;
} // switch
// Determine index type.
size_t outSize = 0;
switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
case Instruction::kIndexUnknown:
// This function should never get called for this type, but do
// something sensible here, just to help with debugging.
outSize = snprintf(buf.get(), bufSize, "<unknown-index>");
break;
case Instruction::kIndexNone:
// This function should never get called for this type, but do
// something sensible here, just to help with debugging.
outSize = snprintf(buf.get(), bufSize, "<no-index>");
break;
case Instruction::kIndexTypeRef:
if (index < pDexFile->GetHeader().type_ids_size_) {
const char* tp = pDexFile->StringByTypeIdx(dex::TypeIndex(index));
outSize = snprintf(buf.get(), bufSize, "%s // type@%0*x", tp, width, index);
} else {
outSize = snprintf(buf.get(), bufSize, "<type?> // type@%0*x", width, index);
}
break;
case Instruction::kIndexStringRef:
if (index < pDexFile->GetHeader().string_ids_size_) {
const char* st = pDexFile->StringDataByIdx(dex::StringIndex(index));
outSize = snprintf(buf.get(), bufSize, "\"%s\" // string@%0*x", st, width, index);
} else {
outSize = snprintf(buf.get(), bufSize, "<string?> // string@%0*x", width, index);
}
break;
case Instruction::kIndexMethodRef:
if (index < pDexFile->GetHeader().method_ids_size_) {
const dex::MethodId& pMethodId = pDexFile->GetMethodId(index);
const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
const Signature signature = pDexFile->GetMethodSignature(pMethodId);
const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // method@%0*x",
backDescriptor, name, signature.ToString().c_str(), width, index);
} else {
outSize = snprintf(buf.get(), bufSize, "<method?> // method@%0*x", width, index);
}
break;
case Instruction::kIndexFieldRef:
if (index < pDexFile->GetHeader().field_ids_size_) {
const dex::FieldId& pFieldId = pDexFile->GetFieldId(index);
const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // field@%0*x",
backDescriptor, name, typeDescriptor, width, index);
} else {
outSize = snprintf(buf.get(), bufSize, "<field?> // field@%0*x", width, index);
}
break;
case Instruction::kIndexVtableOffset:
outSize = snprintf(buf.get(), bufSize, "[%0*x] // vtable #%0*x",
width, index, width, index);
break;
case Instruction::kIndexFieldOffset:
outSize = snprintf(buf.get(), bufSize, "[obj+%0*x]", width, index);
break;
case Instruction::kIndexMethodAndProtoRef: {
std::string method("<method?>");
std::string proto("<proto?>");
if (index < pDexFile->GetHeader().method_ids_size_) {
const dex::MethodId& pMethodId = pDexFile->GetMethodId(index);
const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
const Signature signature = pDexFile->GetMethodSignature(pMethodId);
const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
method = android::base::StringPrintf("%s.%s:%s",
backDescriptor,
name,
signature.ToString().c_str());
}
if (secondary_index < pDexFile->GetHeader().proto_ids_size_) {
const dex::ProtoId& protoId = pDexFile->GetProtoId(dex::ProtoIndex(secondary_index));
const Signature signature = pDexFile->GetProtoSignature(protoId);
proto = signature.ToString();
}
outSize = snprintf(buf.get(), bufSize, "%s, %s // method@%0*x, proto@%0*x",
method.c_str(), proto.c_str(), width, index, width, secondary_index);
break;
}
case Instruction::kIndexCallSiteRef:
// Call site information is too large to detail in disassembly so just output the index.
outSize = snprintf(buf.get(), bufSize, "call_site@%0*x", width, index);
break;
case Instruction::kIndexMethodHandleRef:
// Method handle information is too large to detail in disassembly so just output the index.
outSize = snprintf(buf.get(), bufSize, "method_handle@%0*x", width, index);
break;
case Instruction::kIndexProtoRef:
if (index < pDexFile->GetHeader().proto_ids_size_) {
const dex::ProtoId& protoId = pDexFile->GetProtoId(dex::ProtoIndex(index));
const Signature signature = pDexFile->GetProtoSignature(protoId);
const std::string& proto = signature.ToString();
outSize = snprintf(buf.get(), bufSize, "%s // proto@%0*x", proto.c_str(), width, index);
} else {
outSize = snprintf(buf.get(), bufSize, "<?> // proto@%0*x", width, index);
}
break;
} // switch
if (outSize == 0) {
// The index type has not been handled in the switch above.
outSize = snprintf(buf.get(), bufSize, "<?>");
}
// Determine success of string construction.
if (outSize >= bufSize) {
// The buffer wasn't big enough; retry with computed size. Note: snprintf()
// doesn't count/ the '\0' as part of its returned size, so we add explicit
// space for it here.
return indexString(pDexFile, pDecInsn, outSize + 1);
}
return buf;
}
/*
* Dumps a single instruction.
*/
static void dumpInstruction(const DexFile* pDexFile,
const dex::CodeItem* pCode,
u4 codeOffset, u4 insnIdx, u4 insnWidth,
const Instruction* pDecInsn) {
// Address of instruction (expressed as byte offset).
fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
// Dump (part of) raw bytes.
CodeItemInstructionAccessor accessor(*pDexFile, pCode);
for (u4 i = 0; i < 8; i++) {
if (i < insnWidth) {
if (i == 7) {
fprintf(gOutFile, " ... ");
} else {
// Print 16-bit value in little-endian order.
const u1* bytePtr = (const u1*) &accessor.Insns()[insnIdx + i];
fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
}
} else {
fputs(" ", gOutFile);
}
} // for
// Dump pseudo-instruction or opcode.
if (pDecInsn->Opcode() == Instruction::NOP) {
const u2 instr = get2LE((const u1*) &accessor.Insns()[insnIdx]);
if (instr == Instruction::kPackedSwitchSignature) {
fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
} else if (instr == Instruction::kSparseSwitchSignature) {
fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
} else if (instr == Instruction::kArrayDataSignature) {
fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
} else {
fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
}
} else {
fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
}
// Set up additional argument.
std::unique_ptr<char[]> indexBuf;
if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
indexBuf = indexString(pDexFile, pDecInsn, 200);
}
// Dump the instruction.
//
// NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
//
switch (Instruction::FormatOf(pDecInsn->Opcode())) {
case Instruction::k10x: // op
break;
case Instruction::k12x: // op vA, vB
fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
break;
case Instruction::k11n: // op vA, #+B
fprintf(gOutFile, " v%d, #int %d // #%x",
pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
break;
case Instruction::k11x: // op vAA
fprintf(gOutFile, " v%d", pDecInsn->VRegA());
break;
case Instruction::k10t: // op +AA
case Instruction::k20t: { // op +AAAA
const s4 targ = (s4) pDecInsn->VRegA();
fprintf(gOutFile, " %04x // %c%04x",
insnIdx + targ,
(targ < 0) ? '-' : '+',
(targ < 0) ? -targ : targ);
break;
}
case Instruction::k22x: // op vAA, vBBBB
fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
break;
case Instruction::k21t: { // op vAA, +BBBB
const s4 targ = (s4) pDecInsn->VRegB();
fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
insnIdx + targ,
(targ < 0) ? '-' : '+',
(targ < 0) ? -targ : targ);
break;
}
case Instruction::k21s: // op vAA, #+BBBB