blob: 756f8793c93568760303159ae03472fd8e8d819d [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
20 * based on Art functions in libart instead. The output is identical to
21 * 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 "mem_map.h"
32#include "runtime.h"
33
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");
43 fprintf(stderr, "%s: [-c] [-d] [-f] [-h] [-i] [-l layout] [-o outfile]"
44 " [-t tempfile] dexfile...\n", gProgName);
45 fprintf(stderr, "\n");
46 fprintf(stderr, " -c : verify checksum and exit\n");
47 fprintf(stderr, " -d : disassemble code sections\n");
48 fprintf(stderr, " -f : display summary information from file header\n");
49 fprintf(stderr, " -h : display file header details\n");
50 fprintf(stderr, " -i : ignore checksum failures\n");
51 fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
52 fprintf(stderr, " -o : output file name (defaults to stdout)\n");
53 fprintf(stderr, " -t : temp file name (defaults to /sdcard/dex-temp-*)\n");
54}
55
56/*
57 * Main driver of the dexdump utility.
58 */
59int dexdumpDriver(int argc, char** argv) {
60 // Art specific set up.
61 InitLogging(argv);
62 MemMap::Init();
63
64 // Reset options.
65 bool wantUsage = false;
66 memset(&gOptions, 0, sizeof(gOptions));
67 gOptions.verbose = true;
68
69 // Parse all arguments.
70 while (1) {
71 const int ic = getopt(argc, argv, "cdfhil:t:o:");
72 if (ic < 0) {
73 break; // done
74 }
75 switch (ic) {
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 'f': // dump outer file header
83 gOptions.showFileHeaders = true;
84 break;
85 case 'h': // dump section headers, i.e. all meta-data
86 gOptions.showSectionHeaders = true;
87 break;
88 case 'i': // continue even if checksum is bad
89 gOptions.ignoreBadChecksum = true;
90 break;
91 case 'l': // layout
92 if (strcmp(optarg, "plain") == 0) {
93 gOptions.outputFormat = OUTPUT_PLAIN;
94 } else if (strcmp(optarg, "xml") == 0) {
95 gOptions.outputFormat = OUTPUT_XML;
96 gOptions.verbose = false;
97 gOptions.exportsOnly = true;
98 } else {
99 wantUsage = true;
100 }
101 break;
102 case 't': // temp file, used when opening compressed Jar
103 gOptions.tempFileName = optarg;
104 break;
105 case 'o': // output file
106 gOptions.outputFileName = optarg;
107 break;
108 default:
109 wantUsage = true;
110 break;
111 }
112 }
113
114 // Detect early problems.
115 if (optind == argc) {
116 fprintf(stderr, "%s: no file specified\n", gProgName);
117 wantUsage = true;
118 }
119 if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
120 fprintf(stderr, "Can't specify both -c and -i\n");
121 wantUsage = true;
122 }
123 if (wantUsage) {
124 usage();
125 return 2;
126 }
127
128 // Open alternative output file.
129 if (gOptions.outputFileName) {
130 gOutFile = fopen(gOptions.outputFileName, "w");
131 if (!gOutFile) {
132 fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
133 return 1;
134 }
135 }
136
137 // Process all files supplied on command line.
138 int result = 0;
139 while (optind < argc) {
140 result |= processFile(argv[optind++]);
141 }
142 return result != 0;
143}
144
145} // namespace art
146
147int main(int argc, char** argv) {
148 return art::dexdumpDriver(argc, argv);
149}