blob: 5c032a0bb6d4b61eb8f4c1f2c03828b0ee3970f0 [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
David Sehr9aa352e2016-09-15 18:13:52 -070031#include "base/logging.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070032#include "mem_map.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070033
34namespace art {
35
36static const char* gProgName = "dexdump";
37
38/*
39 * Shows usage.
40 */
41static void usage(void) {
42 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
Aart Bikdce50862016-06-10 16:04:03 -070043 fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile]"
44 " dexfile...\n\n", gProgName);
45 fprintf(stderr, " -a : display annotations\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070046 fprintf(stderr, " -c : verify checksum and exit\n");
47 fprintf(stderr, " -d : disassemble code sections\n");
Aart Bik3f382ae2015-11-13 10:06:01 -080048 fprintf(stderr, " -e : display exported items only\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070049 fprintf(stderr, " -f : display summary information from file header\n");
Aart Bikdce50862016-06-10 16:04:03 -070050 fprintf(stderr, " -g : display CFG for dex\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070051 fprintf(stderr, " -h : display file header details\n");
52 fprintf(stderr, " -i : ignore checksum failures\n");
53 fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
54 fprintf(stderr, " -o : output file name (defaults to stdout)\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070055}
56
57/*
58 * Main driver of the dexdump utility.
59 */
60int dexdumpDriver(int argc, char** argv) {
61 // Art specific set up.
62 InitLogging(argv);
63 MemMap::Init();
64
65 // Reset options.
66 bool wantUsage = false;
67 memset(&gOptions, 0, sizeof(gOptions));
68 gOptions.verbose = true;
69
70 // Parse all arguments.
71 while (1) {
Aart Bikdce50862016-06-10 16:04:03 -070072 const int ic = getopt(argc, argv, "acdefghil:o:");
Aart Bik69ae54a2015-07-01 14:52:26 -070073 if (ic < 0) {
74 break; // done
75 }
76 switch (ic) {
Aart Bikdce50862016-06-10 16:04:03 -070077 case 'a': // display annotations
78 gOptions.showAnnotations = true;
79 break;
Aart Bik69ae54a2015-07-01 14:52:26 -070080 case 'c': // verify the checksum then exit
81 gOptions.checksumOnly = true;
82 break;
83 case 'd': // disassemble Dalvik instructions
84 gOptions.disassemble = true;
85 break;
Aart Bik3f382ae2015-11-13 10:06:01 -080086 case 'e': // exported items only
87 gOptions.exportsOnly = true;
88 break;
Aart Bikdce50862016-06-10 16:04:03 -070089 case 'f': // display outer file header
Aart Bik69ae54a2015-07-01 14:52:26 -070090 gOptions.showFileHeaders = true;
91 break;
Aart Bikdce50862016-06-10 16:04:03 -070092 case 'g': // display cfg
93 gOptions.showCfg = true;
Andreas Gampe5073fed2015-08-10 11:40:25 -070094 break;
Aart Bikdce50862016-06-10 16:04:03 -070095 case 'h': // display section headers, i.e. all meta-data
Aart Bik69ae54a2015-07-01 14:52:26 -070096 gOptions.showSectionHeaders = true;
97 break;
98 case 'i': // continue even if checksum is bad
99 gOptions.ignoreBadChecksum = true;
100 break;
101 case 'l': // layout
102 if (strcmp(optarg, "plain") == 0) {
103 gOptions.outputFormat = OUTPUT_PLAIN;
104 } else if (strcmp(optarg, "xml") == 0) {
105 gOptions.outputFormat = OUTPUT_XML;
106 gOptions.verbose = false;
Aart Bik69ae54a2015-07-01 14:52:26 -0700107 } else {
108 wantUsage = true;
109 }
110 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700111 case 'o': // output file
112 gOptions.outputFileName = optarg;
113 break;
114 default:
115 wantUsage = true;
116 break;
Aart Bik4e149602015-07-09 11:45:28 -0700117 } // switch
118 } // while
Aart Bik69ae54a2015-07-01 14:52:26 -0700119
120 // Detect early problems.
121 if (optind == argc) {
122 fprintf(stderr, "%s: no file specified\n", gProgName);
123 wantUsage = true;
124 }
125 if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
126 fprintf(stderr, "Can't specify both -c and -i\n");
127 wantUsage = true;
128 }
129 if (wantUsage) {
130 usage();
131 return 2;
132 }
133
134 // Open alternative output file.
135 if (gOptions.outputFileName) {
136 gOutFile = fopen(gOptions.outputFileName, "w");
137 if (!gOutFile) {
138 fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
139 return 1;
140 }
141 }
142
143 // Process all files supplied on command line.
144 int result = 0;
145 while (optind < argc) {
146 result |= processFile(argv[optind++]);
Aart Bik4e149602015-07-09 11:45:28 -0700147 } // while
Aart Bik69ae54a2015-07-01 14:52:26 -0700148 return result != 0;
149}
150
151} // namespace art
152
153int main(int argc, char** argv) {
154 return art::dexdumpDriver(argc, argv);
155}