blob: 3afee0db39f5ff60d1d152ec30d1a182466bdfc0 [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";
Ian Zerny19df0e92022-02-02 16:51:02 +010042 LOG(ERROR) << gProgName << ": [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-j] [-l layout] [-n]"
43 " [-o outfile] dexfile...\n";
Andreas Gampe221d9812018-01-22 17:48:56 -080044 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'";
Ian Zerny19df0e92022-02-02 16:51:02 +010054 LOG(ERROR) << " -n : don't display debug information";
Andreas Gampe221d9812018-01-22 17:48:56 -080055 LOG(ERROR) << " -o : output file name (defaults to stdout)";
Aart Bik69ae54a2015-07-01 14:52:26 -070056}
57
58/*
59 * Main driver of the dexdump utility.
60 */
61int dexdumpDriver(int argc, char** argv) {
Aart Bik69ae54a2015-07-01 14:52:26 -070062 // Reset options.
63 bool wantUsage = false;
64 memset(&gOptions, 0, sizeof(gOptions));
65 gOptions.verbose = true;
Ian Zerny19df0e92022-02-02 16:51:02 +010066 gOptions.showDebugInfo = true;
Aart Bik69ae54a2015-07-01 14:52:26 -070067
68 // Parse all arguments.
Andreas Gampe70dfb692018-09-18 16:50:18 -070069 while (true) {
Ian Zerny19df0e92022-02-02 16:51:02 +010070 const int ic = getopt(argc, argv, "acdefghijl:no:");
Aart Bik69ae54a2015-07-01 14:52:26 -070071 if (ic < 0) {
72 break; // done
73 }
74 switch (ic) {
Aart Bikdce50862016-06-10 16:04:03 -070075 case 'a': // display annotations
76 gOptions.showAnnotations = true;
77 break;
Aart Bik69ae54a2015-07-01 14:52:26 -070078 case 'c': // verify the checksum then exit
79 gOptions.checksumOnly = true;
80 break;
81 case 'd': // disassemble Dalvik instructions
82 gOptions.disassemble = true;
83 break;
Aart Bik3f382ae2015-11-13 10:06:01 -080084 case 'e': // exported items only
85 gOptions.exportsOnly = true;
86 break;
Aart Bikdce50862016-06-10 16:04:03 -070087 case 'f': // display outer file header
Aart Bik69ae54a2015-07-01 14:52:26 -070088 gOptions.showFileHeaders = true;
89 break;
Aart Bikdce50862016-06-10 16:04:03 -070090 case 'g': // display cfg
91 gOptions.showCfg = true;
Andreas Gampe5073fed2015-08-10 11:40:25 -070092 break;
Aart Bikdce50862016-06-10 16:04:03 -070093 case 'h': // display section headers, i.e. all meta-data
Aart Bik69ae54a2015-07-01 14:52:26 -070094 gOptions.showSectionHeaders = true;
95 break;
96 case 'i': // continue even if checksum is bad
97 gOptions.ignoreBadChecksum = true;
98 break;
Nicolas Geoffrayc1d8caa2018-02-27 10:15:14 +000099 case 'j': // disable dex file verification
100 gOptions.disableVerifier = true;
101 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700102 case 'l': // layout
103 if (strcmp(optarg, "plain") == 0) {
104 gOptions.outputFormat = OUTPUT_PLAIN;
105 } else if (strcmp(optarg, "xml") == 0) {
106 gOptions.outputFormat = OUTPUT_XML;
107 gOptions.verbose = false;
Aart Bik69ae54a2015-07-01 14:52:26 -0700108 } else {
109 wantUsage = true;
110 }
111 break;
Ian Zerny19df0e92022-02-02 16:51:02 +0100112 case 'n': // don't display debug information
113 gOptions.showDebugInfo = false;
114 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700115 case 'o': // output file
116 gOptions.outputFileName = optarg;
117 break;
118 default:
119 wantUsage = true;
120 break;
Aart Bik4e149602015-07-09 11:45:28 -0700121 } // switch
122 } // while
Aart Bik69ae54a2015-07-01 14:52:26 -0700123
124 // Detect early problems.
125 if (optind == argc) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800126 LOG(ERROR) << "No file specified";
Aart Bik69ae54a2015-07-01 14:52:26 -0700127 wantUsage = true;
128 }
129 if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800130 LOG(ERROR) << "Can't specify both -c and -i";
Aart Bik69ae54a2015-07-01 14:52:26 -0700131 wantUsage = true;
132 }
133 if (wantUsage) {
134 usage();
135 return 2;
136 }
137
138 // Open alternative output file.
139 if (gOptions.outputFileName) {
140 gOutFile = fopen(gOptions.outputFileName, "w");
141 if (!gOutFile) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800142 PLOG(ERROR) << "Can't open " << gOptions.outputFileName;
Aart Bik69ae54a2015-07-01 14:52:26 -0700143 return 1;
144 }
145 }
146
147 // Process all files supplied on command line.
148 int result = 0;
149 while (optind < argc) {
150 result |= processFile(argv[optind++]);
Aart Bik4e149602015-07-09 11:45:28 -0700151 } // while
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700152 return result != 0 ? 1 : 0;
Aart Bik69ae54a2015-07-01 14:52:26 -0700153}
154
155} // namespace art
156
157int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800158 // Output all logging to stderr.
159 android::base::SetLogger(android::base::StderrLogger);
160
Aart Bik69ae54a2015-07-01 14:52:26 -0700161 return art::dexdumpDriver(argc, argv);
162}