blob: 2466f33d1ed3c67a0af40617f7eac1b3f2aed930 [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");
Andreas Gampe5073fed2015-08-10 11:40:25 -070049 fprintf(stderr, " -g : dump CFG for dex\n");
Aart Bik69ae54a2015-07-01 14:52:26 -070050 fprintf(stderr, " -h : display file header details\n");
51 fprintf(stderr, " -i : ignore checksum failures\n");
52 fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
53 fprintf(stderr, " -o : output file name (defaults to stdout)\n");
54 fprintf(stderr, " -t : temp file name (defaults to /sdcard/dex-temp-*)\n");
55}
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) {
Andreas Gampe5073fed2015-08-10 11:40:25 -070072 const int ic = getopt(argc, argv, "cdfghil:t:o:");
Aart Bik69ae54a2015-07-01 14:52:26 -070073 if (ic < 0) {
74 break; // done
75 }
76 switch (ic) {
77 case 'c': // verify the checksum then exit
78 gOptions.checksumOnly = true;
79 break;
80 case 'd': // disassemble Dalvik instructions
81 gOptions.disassemble = true;
82 break;
83 case 'f': // dump outer file header
84 gOptions.showFileHeaders = true;
85 break;
Andreas Gampe5073fed2015-08-10 11:40:25 -070086 case 'g': // dump cfg
87 gOptions.cfg = true;
88 break;
Aart Bik69ae54a2015-07-01 14:52:26 -070089 case 'h': // dump section headers, i.e. all meta-data
90 gOptions.showSectionHeaders = true;
91 break;
92 case 'i': // continue even if checksum is bad
93 gOptions.ignoreBadChecksum = true;
94 break;
95 case 'l': // layout
96 if (strcmp(optarg, "plain") == 0) {
97 gOptions.outputFormat = OUTPUT_PLAIN;
98 } else if (strcmp(optarg, "xml") == 0) {
99 gOptions.outputFormat = OUTPUT_XML;
100 gOptions.verbose = false;
101 gOptions.exportsOnly = true;
102 } else {
103 wantUsage = true;
104 }
105 break;
106 case 't': // temp file, used when opening compressed Jar
107 gOptions.tempFileName = optarg;
108 break;
109 case 'o': // output file
110 gOptions.outputFileName = optarg;
111 break;
112 default:
113 wantUsage = true;
114 break;
Aart Bik4e149602015-07-09 11:45:28 -0700115 } // switch
116 } // while
Aart Bik69ae54a2015-07-01 14:52:26 -0700117
118 // Detect early problems.
119 if (optind == argc) {
120 fprintf(stderr, "%s: no file specified\n", gProgName);
121 wantUsage = true;
122 }
123 if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
124 fprintf(stderr, "Can't specify both -c and -i\n");
125 wantUsage = true;
126 }
127 if (wantUsage) {
128 usage();
129 return 2;
130 }
131
132 // Open alternative output file.
133 if (gOptions.outputFileName) {
134 gOutFile = fopen(gOptions.outputFileName, "w");
135 if (!gOutFile) {
136 fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
137 return 1;
138 }
139 }
140
141 // Process all files supplied on command line.
142 int result = 0;
143 while (optind < argc) {
144 result |= processFile(argv[optind++]);
Aart Bik4e149602015-07-09 11:45:28 -0700145 } // while
Aart Bik69ae54a2015-07-01 14:52:26 -0700146 return result != 0;
147}
148
149} // namespace art
150
151int main(int argc, char** argv) {
152 return art::dexdumpDriver(argc, argv);
153}