1 /*
2 * Copyright (C) 2015 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 * Main driver of the dexdump utility.
17 *
18 * This is a re-implementation of the original dexdump utility that was
19 * based on Dalvik functions in libdex into a new dexdump that is now
20 * based on Art functions in libart instead. The output is very similar to
21 * to the original for correct DEX files. Error messages may differ, however.
22 * Also, ODEX files are no longer supported.
23 */
24
25 #include "dexdump.h"
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <android-base/logging.h>
32
33 namespace art {
34
35 static const char* gProgName = "dexdump";
36
37 /*
38 * Shows usage.
39 */
usage()40 static void usage() {
41 LOG(ERROR) << "Copyright (C) 2007 The Android Open Source Project\n";
42 LOG(ERROR) << gProgName << ": [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-j] [-l layout] [-o outfile]"
43 " dexfile...\n";
44 LOG(ERROR) << " -a : display annotations";
45 LOG(ERROR) << " -c : verify checksum and exit";
46 LOG(ERROR) << " -d : disassemble code sections";
47 LOG(ERROR) << " -e : display exported items only";
48 LOG(ERROR) << " -f : display summary information from file header";
49 LOG(ERROR) << " -g : display CFG for dex";
50 LOG(ERROR) << " -h : display file header details";
51 LOG(ERROR) << " -i : ignore checksum failures";
52 LOG(ERROR) << " -j : disable dex file verification";
53 LOG(ERROR) << " -l : output layout, either 'plain' or 'xml'";
54 LOG(ERROR) << " -o : output file name (defaults to stdout)";
55 }
56
57 /*
58 * Main driver of the dexdump utility.
59 */
dexdumpDriver(int argc,char ** argv)60 int dexdumpDriver(int argc, char** argv) {
61 // Reset options.
62 bool wantUsage = false;
63 memset(&gOptions, 0, sizeof(gOptions));
64 gOptions.verbose = true;
65
66 // Parse all arguments.
67 while (true) {
68 const int ic = getopt(argc, argv, "acdefghijl:o:");
69 if (ic < 0) {
70 break; // done
71 }
72 switch (ic) {
73 case 'a': // display annotations
74 gOptions.showAnnotations = true;
75 break;
76 case 'c': // verify the checksum then exit
77 gOptions.checksumOnly = true;
78 break;
79 case 'd': // disassemble Dalvik instructions
80 gOptions.disassemble = true;
81 break;
82 case 'e': // exported items only
83 gOptions.exportsOnly = true;
84 break;
85 case 'f': // display outer file header
86 gOptions.showFileHeaders = true;
87 break;
88 case 'g': // display cfg
89 gOptions.showCfg = true;
90 break;
91 case 'h': // display section headers, i.e. all meta-data
92 gOptions.showSectionHeaders = true;
93 break;
94 case 'i': // continue even if checksum is bad
95 gOptions.ignoreBadChecksum = true;
96 break;
97 case 'j': // disable dex file verification
98 gOptions.disableVerifier = true;
99 break;
100 case 'l': // layout
101 if (strcmp(optarg, "plain") == 0) {
102 gOptions.outputFormat = OUTPUT_PLAIN;
103 } else if (strcmp(optarg, "xml") == 0) {
104 gOptions.outputFormat = OUTPUT_XML;
105 gOptions.verbose = false;
106 } else {
107 wantUsage = true;
108 }
109 break;
110 case 'o': // output file
111 gOptions.outputFileName = optarg;
112 break;
113 default:
114 wantUsage = true;
115 break;
116 } // switch
117 } // while
118
119 // Detect early problems.
120 if (optind == argc) {
121 LOG(ERROR) << "No file specified";
122 wantUsage = true;
123 }
124 if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
125 LOG(ERROR) << "Can't specify both -c and -i";
126 wantUsage = true;
127 }
128 if (wantUsage) {
129 usage();
130 return 2;
131 }
132
133 // Open alternative output file.
134 if (gOptions.outputFileName) {
135 gOutFile = fopen(gOptions.outputFileName, "w");
136 if (!gOutFile) {
137 PLOG(ERROR) << "Can't open " << gOptions.outputFileName;
138 return 1;
139 }
140 }
141
142 // Process all files supplied on command line.
143 int result = 0;
144 while (optind < argc) {
145 result |= processFile(argv[optind++]);
146 } // while
147 return result != 0 ? 1 : 0;
148 }
149
150 } // namespace art
151
main(int argc,char ** argv)152 int main(int argc, char** argv) {
153 // Output all logging to stderr.
154 android::base::SetLogger(android::base::StderrLogger);
155
156 return art::dexdumpDriver(argc, argv);
157 }
158