1 /*
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 
34 namespace art {
35 
36 static const char* gProgName = "dexdump";
37 
38 /*
39  * Shows usage.
40  */
usage(void)41 static void usage(void) {
42   fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
43   fprintf(stderr, "%s: [-c] [-d] [-e] [-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, " -e : display exported items only\n");
49   fprintf(stderr, " -f : display summary information from file header\n");
50   fprintf(stderr, " -g : dump CFG for dex\n");
51   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  */
dexdumpDriver(int argc,char ** argv)61 int 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) {
73     const int ic = getopt(argc, argv, "cdefghil:t:o:");
74     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;
84       case 'e':  // exported items only
85         gOptions.exportsOnly = true;
86         break;
87       case 'f':  // dump outer file header
88         gOptions.showFileHeaders = true;
89         break;
90       case 'g':  // dump cfg
91         gOptions.cfg = true;
92         break;
93       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;
105         } 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;
118     }  // switch
119   }  // while
120 
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++]);
148   }  // while
149   return result != 0;
150 }
151 
152 }  // namespace art
153 
main(int argc,char ** argv)154 int main(int argc, char** argv) {
155   return art::dexdumpDriver(argc, argv);
156 }
157