blob: 185c1420ab4ff86e02396889adf193b93610d3fc [file] [log] [blame]
Jeff Haoec7f1a92017-03-13 16:24:24 -07001 /*
David Sehr7629f602016-08-07 16:01:51 -07002 * 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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include <fcntl.h>
David Sehr7629f602016-08-07 16:01:51 -070026#include <stdio.h>
27#include <string.h>
David Sehrcdcfde72016-09-26 07:44:04 -070028#include <sys/stat.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070029#include <sys/types.h>
30#include <unistd.h>
David Sehr7629f602016-08-07 16:01:51 -070031
Andreas Gampe57943812017-12-06 21:39:13 -080032#include <android-base/logging.h>
33
34#include "base/logging.h" // For InitLogging.
David Sehrc75f0af2018-04-05 11:02:03 -070035#include "base/mem_map.h"
David Sehrd96bcb32018-04-23 08:14:19 -070036#include "profile/profile_compilation_info.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "runtime.h"
David Sehr7629f602016-08-07 16:01:51 -070038
39namespace art {
40
41static const char* kProgramName = "dexlayout";
42
43/*
44 * Shows usage.
45 */
46static void Usage(void) {
Andreas Gampe221d9812018-01-22 17:48:56 -080047 LOG(ERROR) << "Copyright (C) 2016 The Android Open Source Project\n";
48 LOG(ERROR) << kProgramName
49 << ": [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile] [-p profile]"
50 " [-s] [-t] [-v] [-w directory] dexfile...\n";
51 LOG(ERROR) << " -a : display annotations";
52 LOG(ERROR) << " -b : build dex_ir";
53 LOG(ERROR) << " -c : verify checksum and exit";
54 LOG(ERROR) << " -d : disassemble code sections";
55 LOG(ERROR) << " -e : display exported items only";
56 LOG(ERROR) << " -f : display summary information from file header";
57 LOG(ERROR) << " -h : display file header details";
58 LOG(ERROR) << " -i : ignore checksum failures";
59 LOG(ERROR) << " -l : output layout, either 'plain' or 'xml'";
60 LOG(ERROR) << " -o : output file name (defaults to stdout)";
61 LOG(ERROR) << " -p : profile file name (defaults to no profile)";
62 LOG(ERROR) << " -s : visualize reference pattern";
63 LOG(ERROR) << " -t : display file section sizes";
64 LOG(ERROR) << " -v : verify output file is canonical to input (IR level comparison)";
65 LOG(ERROR) << " -w : output dex directory";
66 LOG(ERROR) << " -x : compact dex generation level, either 'none' or 'fast'";
David Sehr7629f602016-08-07 16:01:51 -070067}
68
69/*
70 * Main driver of the dexlayout utility.
71 */
72int DexlayoutDriver(int argc, char** argv) {
73 // Art specific set up.
Andreas Gampe51d80cc2017-06-21 21:05:13 -070074 InitLogging(argv, Runtime::Abort);
David Sehr7629f602016-08-07 16:01:51 -070075 MemMap::Init();
76
Jeff Haoea7c6292016-11-14 18:10:16 -080077 Options options;
78 options.dump_ = true;
79 options.verbose_ = true;
David Sehr7629f602016-08-07 16:01:51 -070080 bool want_usage = false;
David Sehr7629f602016-08-07 16:01:51 -070081
82 // Parse all arguments.
83 while (1) {
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -080084 const int ic = getopt(argc, argv, "abcdefghil:o:p:stvw:x:");
David Sehr7629f602016-08-07 16:01:51 -070085 if (ic < 0) {
86 break; // done
87 }
88 switch (ic) {
89 case 'a': // display annotations
Jeff Haoea7c6292016-11-14 18:10:16 -080090 options.show_annotations_ = true;
David Sehr7629f602016-08-07 16:01:51 -070091 break;
92 case 'b': // build dex_ir
Jeff Haoea7c6292016-11-14 18:10:16 -080093 options.build_dex_ir_ = true;
David Sehr7629f602016-08-07 16:01:51 -070094 break;
95 case 'c': // verify the checksum then exit
Jeff Haoea7c6292016-11-14 18:10:16 -080096 options.checksum_only_ = true;
David Sehr7629f602016-08-07 16:01:51 -070097 break;
98 case 'd': // disassemble Dalvik instructions
Jeff Haoea7c6292016-11-14 18:10:16 -080099 options.disassemble_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700100 break;
101 case 'e': // exported items only
Jeff Haoea7c6292016-11-14 18:10:16 -0800102 options.exports_only_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700103 break;
104 case 'f': // display outer file header
Jeff Haoea7c6292016-11-14 18:10:16 -0800105 options.show_file_headers_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700106 break;
David Sehr7629f602016-08-07 16:01:51 -0700107 case 'h': // display section headers, i.e. all meta-data
Jeff Haoea7c6292016-11-14 18:10:16 -0800108 options.show_section_headers_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700109 break;
110 case 'i': // continue even if checksum is bad
Jeff Haoea7c6292016-11-14 18:10:16 -0800111 options.ignore_bad_checksum_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700112 break;
113 case 'l': // layout
114 if (strcmp(optarg, "plain") == 0) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800115 options.output_format_ = kOutputPlain;
David Sehr7629f602016-08-07 16:01:51 -0700116 } else if (strcmp(optarg, "xml") == 0) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800117 options.output_format_ = kOutputXml;
118 options.verbose_ = false;
David Sehr7629f602016-08-07 16:01:51 -0700119 } else {
120 want_usage = true;
121 }
122 break;
123 case 'o': // output file
Jeff Haoea7c6292016-11-14 18:10:16 -0800124 options.output_file_name_ = optarg;
David Sehr7629f602016-08-07 16:01:51 -0700125 break;
David Sehrcdcfde72016-09-26 07:44:04 -0700126 case 'p': // profile file
Jeff Haoea7c6292016-11-14 18:10:16 -0800127 options.profile_file_name_ = optarg;
David Sehrcdcfde72016-09-26 07:44:04 -0700128 break;
129 case 's': // visualize access pattern
Jeff Haoea7c6292016-11-14 18:10:16 -0800130 options.visualize_pattern_ = true;
131 options.verbose_ = false;
David Sehrcdcfde72016-09-26 07:44:04 -0700132 break;
David Sehr93357492017-03-09 08:02:44 -0800133 case 't': // display section statistics
134 options.show_section_statistics_ = true;
135 options.verbose_ = false;
136 break;
Jeff Haoec7f1a92017-03-13 16:24:24 -0700137 case 'v': // verify output
138 options.verify_output_ = true;
139 break;
Jeff Haoa8621002016-10-04 18:13:44 +0000140 case 'w': // output dex files directory
Jeff Haoea7c6292016-11-14 18:10:16 -0800141 options.output_dex_directory_ = optarg;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700142 break;
Mathieu Chartier5c362202018-01-17 14:52:55 -0800143 case 'x': // compact dex level
144 if (strcmp(optarg, "none") == 0) {
145 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
146 } else if (strcmp(optarg, "fast") == 0) {
147 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelFast;
148 } else {
149 want_usage = true;
150 }
151 break;
David Sehr7629f602016-08-07 16:01:51 -0700152 default:
153 want_usage = true;
154 break;
155 } // switch
156 } // while
157
158 // Detect early problems.
159 if (optind == argc) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800160 LOG(ERROR) << "no file specified";
David Sehr7629f602016-08-07 16:01:51 -0700161 want_usage = true;
162 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800163 if (options.checksum_only_ && options.ignore_bad_checksum_) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800164 LOG(ERROR) << "Can't specify both -c and -i";
David Sehr7629f602016-08-07 16:01:51 -0700165 want_usage = true;
166 }
167 if (want_usage) {
168 Usage();
169 return 2;
170 }
171
172 // Open alternative output file.
Jeff Haoea7c6292016-11-14 18:10:16 -0800173 FILE* out_file = stdout;
174 if (options.output_file_name_) {
175 out_file = fopen(options.output_file_name_, "w");
176 if (!out_file) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800177 PLOG(ERROR) << "Can't open " << options.output_file_name_;
David Sehr7629f602016-08-07 16:01:51 -0700178 return 1;
179 }
180 }
181
David Sehrcdcfde72016-09-26 07:44:04 -0700182 // Open profile file.
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700183 std::unique_ptr<ProfileCompilationInfo> profile_info;
Jeff Haoea7c6292016-11-14 18:10:16 -0800184 if (options.profile_file_name_) {
185 int profile_fd = open(options.profile_file_name_, O_RDONLY);
David Sehrcdcfde72016-09-26 07:44:04 -0700186 if (profile_fd < 0) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800187 PLOG(ERROR) << "Can't open " << options.profile_file_name_;
David Sehrcdcfde72016-09-26 07:44:04 -0700188 return 1;
189 }
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700190 profile_info.reset(new ProfileCompilationInfo());
Jeff Haoea7c6292016-11-14 18:10:16 -0800191 if (!profile_info->Load(profile_fd)) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800192 LOG(ERROR) << "Can't read profile info from " << options.profile_file_name_;
David Sehrcdcfde72016-09-26 07:44:04 -0700193 return 1;
194 }
195 }
196
Jeff Haoea7c6292016-11-14 18:10:16 -0800197 // Create DexLayout instance.
Mathieu Chartiere6b6ff82018-01-19 18:58:34 -0800198 DexLayout dex_layout(options, profile_info.get(), out_file, /*header*/ nullptr);
Jeff Haoea7c6292016-11-14 18:10:16 -0800199
David Sehr7629f602016-08-07 16:01:51 -0700200 // Process all files supplied on command line.
201 int result = 0;
202 while (optind < argc) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800203 result |= dex_layout.ProcessFile(argv[optind++]);
David Sehr7629f602016-08-07 16:01:51 -0700204 } // while
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700205
206 if (options.output_file_name_) {
207 CHECK(out_file != nullptr && out_file != stdout);
208 fclose(out_file);
209 }
210
David Sehr7629f602016-08-07 16:01:51 -0700211 return result != 0;
212}
213
214} // namespace art
215
216int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800217 // Output all logging to stderr.
218 android::base::SetLogger(android::base::StderrLogger);
219
David Sehr7629f602016-08-07 16:01:51 -0700220 return art::DexlayoutDriver(argc, argv);
221}