blob: 2203fba325d0b2483783af29a14bb8d323c9fbcf [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>
David Sehrcdcfde72016-09-26 07:44:04 -070028#include <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
David Sehr7629f602016-08-07 16:01:51 -070031
David Sehr9aa352e2016-09-15 18:13:52 -070032#include "base/logging.h"
David Sehrcdcfde72016-09-26 07:44:04 -070033#include "jit/offline_profiling_info.h"
David Sehrf57589f2016-10-17 10:09:33 -070034#include "runtime.h"
David Sehr7629f602016-08-07 16:01:51 -070035#include "mem_map.h"
David Sehr7629f602016-08-07 16:01:51 -070036
37namespace art {
38
39static const char* kProgramName = "dexlayout";
40
41/*
42 * Shows usage.
43 */
44static void Usage(void) {
David Sehrcdcfde72016-09-26 07:44:04 -070045 fprintf(stderr, "Copyright (C) 2016 The Android Open Source Project\n\n");
46 fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile] [-p profile]"
Jeff Haoa8621002016-10-04 18:13:44 +000047 " [-s] [-w directory] dexfile...\n\n", kProgramName);
David Sehr7629f602016-08-07 16:01:51 -070048 fprintf(stderr, " -a : display annotations\n");
49 fprintf(stderr, " -b : build dex_ir\n");
50 fprintf(stderr, " -c : verify checksum and exit\n");
51 fprintf(stderr, " -d : disassemble code sections\n");
52 fprintf(stderr, " -e : display exported items only\n");
53 fprintf(stderr, " -f : display summary information from file header\n");
54 fprintf(stderr, " -g : display CFG for dex\n");
55 fprintf(stderr, " -h : display file header details\n");
56 fprintf(stderr, " -i : ignore checksum failures\n");
57 fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
58 fprintf(stderr, " -o : output file name (defaults to stdout)\n");
David Sehrcdcfde72016-09-26 07:44:04 -070059 fprintf(stderr, " -p : profile file name (defaults to no profile)\n");
60 fprintf(stderr, " -s : visualize reference pattern\n");
Jeff Haoa8621002016-10-04 18:13:44 +000061 fprintf(stderr, " -w : output dex directory \n");
David Sehr7629f602016-08-07 16:01:51 -070062}
63
64/*
65 * Main driver of the dexlayout utility.
66 */
67int DexlayoutDriver(int argc, char** argv) {
68 // Art specific set up.
David Sehrf57589f2016-10-17 10:09:33 -070069 InitLogging(argv, Runtime::Aborter);
David Sehr7629f602016-08-07 16:01:51 -070070 MemMap::Init();
71
72 // Reset options.
73 bool want_usage = false;
74 memset(&options_, 0, sizeof(options_));
75 options_.verbose_ = true;
76
77 // Parse all arguments.
78 while (1) {
Jeff Haoa8621002016-10-04 18:13:44 +000079 const int ic = getopt(argc, argv, "abcdefghil:o:p:sw:");
David Sehr7629f602016-08-07 16:01:51 -070080 if (ic < 0) {
81 break; // done
82 }
83 switch (ic) {
84 case 'a': // display annotations
85 options_.show_annotations_ = true;
86 break;
87 case 'b': // build dex_ir
88 options_.build_dex_ir_ = true;
89 break;
90 case 'c': // verify the checksum then exit
91 options_.checksum_only_ = true;
92 break;
93 case 'd': // disassemble Dalvik instructions
94 options_.disassemble_ = true;
95 break;
96 case 'e': // exported items only
97 options_.exports_only_ = true;
98 break;
99 case 'f': // display outer file header
100 options_.show_file_headers_ = true;
101 break;
102 case 'g': // display cfg
103 options_.show_cfg_ = true;
104 break;
105 case 'h': // display section headers, i.e. all meta-data
106 options_.show_section_headers_ = true;
107 break;
108 case 'i': // continue even if checksum is bad
109 options_.ignore_bad_checksum_ = true;
110 break;
111 case 'l': // layout
112 if (strcmp(optarg, "plain") == 0) {
113 options_.output_format_ = kOutputPlain;
114 } else if (strcmp(optarg, "xml") == 0) {
115 options_.output_format_ = kOutputXml;
116 options_.verbose_ = false;
117 } else {
118 want_usage = true;
119 }
120 break;
121 case 'o': // output file
122 options_.output_file_name_ = optarg;
123 break;
David Sehrcdcfde72016-09-26 07:44:04 -0700124 case 'p': // profile file
125 options_.profile_file_name_ = optarg;
126 break;
127 case 's': // visualize access pattern
128 options_.visualize_pattern_ = true;
129 options_.verbose_ = false;
130 break;
Jeff Haoa8621002016-10-04 18:13:44 +0000131 case 'w': // output dex files directory
132 options_.output_dex_directory_ = optarg;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700133 break;
David Sehr7629f602016-08-07 16:01:51 -0700134 default:
135 want_usage = true;
136 break;
137 } // switch
138 } // while
139
140 // Detect early problems.
141 if (optind == argc) {
142 fprintf(stderr, "%s: no file specified\n", kProgramName);
143 want_usage = true;
144 }
145 if (options_.checksum_only_ && options_.ignore_bad_checksum_) {
146 fprintf(stderr, "Can't specify both -c and -i\n");
147 want_usage = true;
148 }
149 if (want_usage) {
150 Usage();
151 return 2;
152 }
153
154 // Open alternative output file.
155 if (options_.output_file_name_) {
156 out_file_ = fopen(options_.output_file_name_, "w");
157 if (!out_file_) {
158 fprintf(stderr, "Can't open %s\n", options_.output_file_name_);
159 return 1;
160 }
161 }
162
David Sehrcdcfde72016-09-26 07:44:04 -0700163 // Open profile file.
164 if (options_.profile_file_name_) {
165 int profile_fd = open(options_.profile_file_name_, O_RDONLY);
166 if (profile_fd < 0) {
167 fprintf(stderr, "Can't open %s\n", options_.profile_file_name_);
168 return 1;
169 }
170 profile_info_ = new ProfileCompilationInfo();
171 if (!profile_info_->Load(profile_fd)) {
172 fprintf(stderr, "Can't read profile info from %s\n", options_.profile_file_name_);
173 return 1;
174 }
175 }
176
David Sehr7629f602016-08-07 16:01:51 -0700177 // Process all files supplied on command line.
178 int result = 0;
179 while (optind < argc) {
180 result |= ProcessFile(argv[optind++]);
181 } // while
182 return result != 0;
183}
184
185} // namespace art
186
187int main(int argc, char** argv) {
188 return art::DexlayoutDriver(argc, argv);
189}