blob: 74cae3c961051894f77b82b555c1499b16ab6087 [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"
David Sehrf57589f2016-10-17 10:09:33 -070032#include "runtime.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070033#include "mem_map.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070034
35namespace art {
36
37static const char* gProgName = "dexdump";
38
39/*
40 * Shows usage.
41 */
42static void usage(void) {
43 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
Aart Bikdce50862016-06-10 16:04:03 -070044 fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile]"
45 " dexfile...\n\n", gProgName);
46 fprintf(stderr, " -a : display annotations\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070047 fprintf(stderr, " -c : verify checksum and exit\n");
48 fprintf(stderr, " -d : disassemble code sections\n");
Aart Bik3f382ae2015-11-13 10:06:01 -080049 fprintf(stderr, " -e : display exported items only\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070050 fprintf(stderr, " -f : display summary information from file header\n");
Aart Bikdce50862016-06-10 16:04:03 -070051 fprintf(stderr, " -g : display CFG for dex\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070052 fprintf(stderr, " -h : display file header details\n");
53 fprintf(stderr, " -i : ignore checksum failures\n");
54 fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
55 fprintf(stderr, " -o : output file name (defaults to stdout)\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070056}
57
58/*
59 * Main driver of the dexdump utility.
60 */
61int dexdumpDriver(int argc, char** argv) {
62 // Art specific set up.
David Sehrf57589f2016-10-17 10:09:33 -070063 InitLogging(argv, Runtime::Aborter);
Aart Bik69ae54a2015-07-01 14:52:26 -070064 MemMap::Init();
65
66 // Reset options.
67 bool wantUsage = false;
68 memset(&gOptions, 0, sizeof(gOptions));
69 gOptions.verbose = true;
70
71 // Parse all arguments.
72 while (1) {
Aart Bikdce50862016-06-10 16:04:03 -070073 const int ic = getopt(argc, argv, "acdefghil:o:");
Aart Bik69ae54a2015-07-01 14:52:26 -070074 if (ic < 0) {
75 break; // done
76 }
77 switch (ic) {
Aart Bikdce50862016-06-10 16:04:03 -070078 case 'a': // display annotations
79 gOptions.showAnnotations = true;
80 break;
Aart Bik69ae54a2015-07-01 14:52:26 -070081 case 'c': // verify the checksum then exit
82 gOptions.checksumOnly = true;
83 break;
84 case 'd': // disassemble Dalvik instructions
85 gOptions.disassemble = true;
86 break;
Aart Bik3f382ae2015-11-13 10:06:01 -080087 case 'e': // exported items only
88 gOptions.exportsOnly = true;
89 break;
Aart Bikdce50862016-06-10 16:04:03 -070090 case 'f': // display outer file header
Aart Bik69ae54a2015-07-01 14:52:26 -070091 gOptions.showFileHeaders = true;
92 break;
Aart Bikdce50862016-06-10 16:04:03 -070093 case 'g': // display cfg
94 gOptions.showCfg = true;
Andreas Gampe5073fed2015-08-10 11:40:25 -070095 break;
Aart Bikdce50862016-06-10 16:04:03 -070096 case 'h': // display section headers, i.e. all meta-data
Aart Bik69ae54a2015-07-01 14:52:26 -070097 gOptions.showSectionHeaders = true;
98 break;
99 case 'i': // continue even if checksum is bad
100 gOptions.ignoreBadChecksum = true;
101 break;
102 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;
Aart Bik69ae54a2015-07-01 14:52:26 -0700112 case 'o': // output file
113 gOptions.outputFileName = optarg;
114 break;
115 default:
116 wantUsage = true;
117 break;
Aart Bik4e149602015-07-09 11:45:28 -0700118 } // switch
119 } // while
Aart Bik69ae54a2015-07-01 14:52:26 -0700120
121 // Detect early problems.
122 if (optind == argc) {
123 fprintf(stderr, "%s: no file specified\n", gProgName);
124 wantUsage = true;
125 }
126 if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
127 fprintf(stderr, "Can't specify both -c and -i\n");
128 wantUsage = true;
129 }
130 if (wantUsage) {
131 usage();
132 return 2;
133 }
134
135 // Open alternative output file.
136 if (gOptions.outputFileName) {
137 gOutFile = fopen(gOptions.outputFileName, "w");
138 if (!gOutFile) {
139 fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
140 return 1;
141 }
142 }
143
144 // Process all files supplied on command line.
145 int result = 0;
146 while (optind < argc) {
147 result |= processFile(argv[optind++]);
Aart Bik4e149602015-07-09 11:45:28 -0700148 } // while
Aart Bik69ae54a2015-07-01 14:52:26 -0700149 return result != 0;
150}
151
152} // namespace art
153
154int main(int argc, char** argv) {
155 return art::dexdumpDriver(argc, argv);
156}