blob: ec5edf4065d26ee25a3f9460ee14bad7bd2fdebc [file] [log] [blame]
David Sehr7629f602016-08-07 16:01:51 -07001/*
2 * Copyright (C) 2016 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 dexlayout utility.
17 *
18 * This is a tool to read dex files into an internal representation,
19 * reorganize the representation, and emit dex files with a better
20 * file layout.
21 */
22
23#include "dexlayout.h"
24
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
28
David Sehr9aa352e2016-09-15 18:13:52 -070029#include "base/logging.h"
David Sehr7629f602016-08-07 16:01:51 -070030#include "mem_map.h"
David Sehr7629f602016-08-07 16:01:51 -070031
32namespace art {
33
34static const char* kProgramName = "dexlayout";
35
36/*
37 * Shows usage.
38 */
39static void Usage(void) {
40 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
Jeff Hao3ab96b42016-09-09 18:35:01 -070041 fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile] [-w]"
David Sehr7629f602016-08-07 16:01:51 -070042 " dexfile...\n\n", kProgramName);
43 fprintf(stderr, " -a : display annotations\n");
44 fprintf(stderr, " -b : build dex_ir\n");
45 fprintf(stderr, " -c : verify checksum and exit\n");
46 fprintf(stderr, " -d : disassemble code sections\n");
47 fprintf(stderr, " -e : display exported items only\n");
48 fprintf(stderr, " -f : display summary information from file header\n");
49 fprintf(stderr, " -g : display CFG for dex\n");
50 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");
Jeff Hao3ab96b42016-09-09 18:35:01 -070054 fprintf(stderr, " -w : output dex files\n");
David Sehr7629f602016-08-07 16:01:51 -070055}
56
57/*
58 * Main driver of the dexlayout utility.
59 */
60int DexlayoutDriver(int argc, char** argv) {
61 // Art specific set up.
62 InitLogging(argv);
63 MemMap::Init();
64
65 // Reset options.
66 bool want_usage = false;
67 memset(&options_, 0, sizeof(options_));
68 options_.verbose_ = true;
69
70 // Parse all arguments.
71 while (1) {
Jeff Hao3ab96b42016-09-09 18:35:01 -070072 const int ic = getopt(argc, argv, "abcdefghil:o:w");
David Sehr7629f602016-08-07 16:01:51 -070073 if (ic < 0) {
74 break; // done
75 }
76 switch (ic) {
77 case 'a': // display annotations
78 options_.show_annotations_ = true;
79 break;
80 case 'b': // build dex_ir
81 options_.build_dex_ir_ = true;
82 break;
83 case 'c': // verify the checksum then exit
84 options_.checksum_only_ = true;
85 break;
86 case 'd': // disassemble Dalvik instructions
87 options_.disassemble_ = true;
88 break;
89 case 'e': // exported items only
90 options_.exports_only_ = true;
91 break;
92 case 'f': // display outer file header
93 options_.show_file_headers_ = true;
94 break;
95 case 'g': // display cfg
96 options_.show_cfg_ = true;
97 break;
98 case 'h': // display section headers, i.e. all meta-data
99 options_.show_section_headers_ = true;
100 break;
101 case 'i': // continue even if checksum is bad
102 options_.ignore_bad_checksum_ = true;
103 break;
104 case 'l': // layout
105 if (strcmp(optarg, "plain") == 0) {
106 options_.output_format_ = kOutputPlain;
107 } else if (strcmp(optarg, "xml") == 0) {
108 options_.output_format_ = kOutputXml;
109 options_.verbose_ = false;
110 } else {
111 want_usage = true;
112 }
113 break;
114 case 'o': // output file
115 options_.output_file_name_ = optarg;
116 break;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700117 case 'w': // output dex files
118 options_.output_dex_files_ = true;
119 break;
David Sehr7629f602016-08-07 16:01:51 -0700120 default:
121 want_usage = true;
122 break;
123 } // switch
124 } // while
125
126 // Detect early problems.
127 if (optind == argc) {
128 fprintf(stderr, "%s: no file specified\n", kProgramName);
129 want_usage = true;
130 }
131 if (options_.checksum_only_ && options_.ignore_bad_checksum_) {
132 fprintf(stderr, "Can't specify both -c and -i\n");
133 want_usage = true;
134 }
135 if (want_usage) {
136 Usage();
137 return 2;
138 }
139
140 // Open alternative output file.
141 if (options_.output_file_name_) {
142 out_file_ = fopen(options_.output_file_name_, "w");
143 if (!out_file_) {
144 fprintf(stderr, "Can't open %s\n", options_.output_file_name_);
145 return 1;
146 }
147 }
148
149 // Process all files supplied on command line.
150 int result = 0;
151 while (optind < argc) {
152 result |= ProcessFile(argv[optind++]);
153 } // while
154 return result != 0;
155}
156
157} // namespace art
158
159int main(int argc, char** argv) {
160 return art::DexlayoutDriver(argc, argv);
161}