blob: 8b2b71c8afe66fb148bf0022dfda2b37fb005f05 [file] [log] [blame]
Aart Bik69ae54a2015-07-01 14:52:26 -07001/*
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
Aart Bik37d6a3b2016-06-21 18:30:10 -070020 * 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.
Aart Bik69ae54a2015-07-01 14:52:26 -070022 * 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
Andreas Gampe221d9812018-01-22 17:48:56 -080031#include <android-base/logging.h>
32
Aart Bik69ae54a2015-07-01 14:52:26 -070033namespace art {
34
35static const char* gProgName = "dexdump";
36
37/*
38 * Shows usage.
39 */
Andreas Gampe70dfb692018-09-18 16:50:18 -070040static void usage() {
Andreas Gampe221d9812018-01-22 17:48:56 -080041 LOG(ERROR) << "Copyright (C) 2007 The Android Open Source Project\n";
Nicolas Geoffrayc1d8caa2018-02-27 10:15:14 +000042 LOG(ERROR) << gProgName << ": [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-j] [-l layout] [-o outfile]"
Andreas Gampe221d9812018-01-22 17:48:56 -080043 " 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";
Nicolas Geoffrayc1d8caa2018-02-27 10:15:14 +000052 LOG(ERROR) << " -j : disable dex file verification";
Andreas Gampe221d9812018-01-22 17:48:56 -080053 LOG(ERROR) << " -l : output layout, either 'plain' or 'xml'";
54 LOG(ERROR) << " -o : output file name (defaults to stdout)";
Aart Bik69ae54a2015-07-01 14:52:26 -070055}
56
57/*
58 * Main driver of the dexdump utility.
59 */
60int dexdumpDriver(int argc, char** argv) {
Aart Bik69ae54a2015-07-01 14:52:26 -070061 // Reset options.
62 bool wantUsage = false;
63 memset(&gOptions, 0, sizeof(gOptions));
64 gOptions.verbose = true;
65
66 // Parse all arguments.
Andreas Gampe70dfb692018-09-18 16:50:18 -070067 while (true) {
Nicolas Geoffrayc1d8caa2018-02-27 10:15:14 +000068 const int ic = getopt(argc, argv, "acdefghijl:o:");
Aart Bik69ae54a2015-07-01 14:52:26 -070069 if (ic < 0) {
70 break; // done
71 }
72 switch (ic) {
Aart Bikdce50862016-06-10 16:04:03 -070073 case 'a': // display annotations
74 gOptions.showAnnotations = true;
75 break;
Aart Bik69ae54a2015-07-01 14:52:26 -070076 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;
Aart Bik3f382ae2015-11-13 10:06:01 -080082 case 'e': // exported items only
83 gOptions.exportsOnly = true;
84 break;
Aart Bikdce50862016-06-10 16:04:03 -070085 case 'f': // display outer file header
Aart Bik69ae54a2015-07-01 14:52:26 -070086 gOptions.showFileHeaders = true;
87 break;
Aart Bikdce50862016-06-10 16:04:03 -070088 case 'g': // display cfg
89 gOptions.showCfg = true;
Andreas Gampe5073fed2015-08-10 11:40:25 -070090 break;
Aart Bikdce50862016-06-10 16:04:03 -070091 case 'h': // display section headers, i.e. all meta-data
Aart Bik69ae54a2015-07-01 14:52:26 -070092 gOptions.showSectionHeaders = true;
93 break;
94 case 'i': // continue even if checksum is bad
95 gOptions.ignoreBadChecksum = true;
96 break;
Nicolas Geoffrayc1d8caa2018-02-27 10:15:14 +000097 case 'j': // disable dex file verification
98 gOptions.disableVerifier = true;
99 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700100 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;
Aart Bik69ae54a2015-07-01 14:52:26 -0700106 } else {
107 wantUsage = true;
108 }
109 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700110 case 'o': // output file
111 gOptions.outputFileName = optarg;
112 break;
113 default:
114 wantUsage = true;
115 break;
Aart Bik4e149602015-07-09 11:45:28 -0700116 } // switch
117 } // while
Aart Bik69ae54a2015-07-01 14:52:26 -0700118
119 // Detect early problems.
120 if (optind == argc) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800121 LOG(ERROR) << "No file specified";
Aart Bik69ae54a2015-07-01 14:52:26 -0700122 wantUsage = true;
123 }
124 if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800125 LOG(ERROR) << "Can't specify both -c and -i";
Aart Bik69ae54a2015-07-01 14:52:26 -0700126 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) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800137 PLOG(ERROR) << "Can't open " << gOptions.outputFileName;
Aart Bik69ae54a2015-07-01 14:52:26 -0700138 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++]);
Aart Bik4e149602015-07-09 11:45:28 -0700146 } // while
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700147 return result != 0 ? 1 : 0;
Aart Bik69ae54a2015-07-01 14:52:26 -0700148}
149
150} // namespace art
151
152int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800153 // Output all logging to stderr.
154 android::base::SetLogger(android::base::StderrLogger);
155
Aart Bik69ae54a2015-07-01 14:52:26 -0700156 return art::dexdumpDriver(argc, argv);
157}