blob: dd1002c4c77ef6982c0ed167c7ee0995673e11b3 [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");
Aart Bik3f382ae2015-11-13 10:06:01 -080043 fprintf(stderr, "%s: [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile]"
Aart Bik69ae54a2015-07-01 14:52:26 -070044 " [-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");
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");
Andreas Gampe5073fed2015-08-10 11:40:25 -070050 fprintf(stderr, " -g : dump 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");
55 fprintf(stderr, " -t : temp file name (defaults to /sdcard/dex-temp-*)\n");
56}
57
58/*
59 * Main driver of the dexdump utility.
60 */
61int dexdumpDriver(int argc, char** argv) {
62 // Art specific set up.
63 InitLogging(argv);
64 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 Bik3f382ae2015-11-13 10:06:01 -080073 const int ic = getopt(argc, argv, "cdefghil:t:o:");
Aart Bik69ae54a2015-07-01 14:52:26 -070074 if (ic < 0) {
75 break; // done
76 }
77 switch (ic) {
78 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 Bik69ae54a2015-07-01 14:52:26 -070087 case 'f': // dump outer file header
88 gOptions.showFileHeaders = true;
89 break;
Andreas Gampe5073fed2015-08-10 11:40:25 -070090 case 'g': // dump cfg
91 gOptions.cfg = true;
92 break;
Aart Bik69ae54a2015-07-01 14:52:26 -070093 case 'h': // dump section headers, i.e. all meta-data
94 gOptions.showSectionHeaders = true;
95 break;
96 case 'i': // continue even if checksum is bad
97 gOptions.ignoreBadChecksum = true;
98 break;
99 case 'l': // layout
100 if (strcmp(optarg, "plain") == 0) {
101 gOptions.outputFormat = OUTPUT_PLAIN;
102 } else if (strcmp(optarg, "xml") == 0) {
103 gOptions.outputFormat = OUTPUT_XML;
104 gOptions.verbose = false;
Aart Bik69ae54a2015-07-01 14:52:26 -0700105 } else {
106 wantUsage = true;
107 }
108 break;
109 case 't': // temp file, used when opening compressed Jar
110 gOptions.tempFileName = optarg;
111 break;
112 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}