blob: 17097f172843e474e17b4d050fcb3c757b364244 [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
David Sehr9aa352e2016-09-15 18:13:52 -070032#include "base/logging.h"
Calin Juravle33083d62017-01-18 15:29:12 -080033#include "jit/profile_compilation_info.h"
David Sehr7629f602016-08-07 16:01:51 -070034#include "mem_map.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "runtime.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 Haoec7f1a92017-03-13 16:24:24 -070047 " [-s] [-t] [-v] [-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");
David Sehr7629f602016-08-07 16:01:51 -070054 fprintf(stderr, " -h : display file header details\n");
55 fprintf(stderr, " -i : ignore checksum failures\n");
56 fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
57 fprintf(stderr, " -o : output file name (defaults to stdout)\n");
David Sehrcdcfde72016-09-26 07:44:04 -070058 fprintf(stderr, " -p : profile file name (defaults to no profile)\n");
59 fprintf(stderr, " -s : visualize reference pattern\n");
David Sehr93357492017-03-09 08:02:44 -080060 fprintf(stderr, " -t : display file section sizes\n");
Jeff Haoec7f1a92017-03-13 16:24:24 -070061 fprintf(stderr, " -v : verify output file is canonical to input (IR level comparison)\n");
Jeff Haoa8621002016-10-04 18:13:44 +000062 fprintf(stderr, " -w : output dex directory \n");
David Sehr7629f602016-08-07 16:01:51 -070063}
64
65/*
66 * Main driver of the dexlayout utility.
67 */
68int DexlayoutDriver(int argc, char** argv) {
69 // Art specific set up.
Andreas Gampe51d80cc2017-06-21 21:05:13 -070070 InitLogging(argv, Runtime::Abort);
David Sehr7629f602016-08-07 16:01:51 -070071 MemMap::Init();
72
Jeff Haoea7c6292016-11-14 18:10:16 -080073 Options options;
74 options.dump_ = true;
75 options.verbose_ = true;
David Sehr7629f602016-08-07 16:01:51 -070076 bool want_usage = false;
David Sehr7629f602016-08-07 16:01:51 -070077
78 // Parse all arguments.
79 while (1) {
Jeff Haoec7f1a92017-03-13 16:24:24 -070080 const int ic = getopt(argc, argv, "abcdefghil:mo:p:stvw:");
David Sehr7629f602016-08-07 16:01:51 -070081 if (ic < 0) {
82 break; // done
83 }
84 switch (ic) {
85 case 'a': // display annotations
Jeff Haoea7c6292016-11-14 18:10:16 -080086 options.show_annotations_ = true;
David Sehr7629f602016-08-07 16:01:51 -070087 break;
88 case 'b': // build dex_ir
Jeff Haoea7c6292016-11-14 18:10:16 -080089 options.build_dex_ir_ = true;
David Sehr7629f602016-08-07 16:01:51 -070090 break;
91 case 'c': // verify the checksum then exit
Jeff Haoea7c6292016-11-14 18:10:16 -080092 options.checksum_only_ = true;
David Sehr7629f602016-08-07 16:01:51 -070093 break;
94 case 'd': // disassemble Dalvik instructions
Jeff Haoea7c6292016-11-14 18:10:16 -080095 options.disassemble_ = true;
David Sehr7629f602016-08-07 16:01:51 -070096 break;
97 case 'e': // exported items only
Jeff Haoea7c6292016-11-14 18:10:16 -080098 options.exports_only_ = true;
David Sehr7629f602016-08-07 16:01:51 -070099 break;
100 case 'f': // display outer file header
Jeff Haoea7c6292016-11-14 18:10:16 -0800101 options.show_file_headers_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700102 break;
David Sehr7629f602016-08-07 16:01:51 -0700103 case 'h': // display section headers, i.e. all meta-data
Jeff Haoea7c6292016-11-14 18:10:16 -0800104 options.show_section_headers_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700105 break;
106 case 'i': // continue even if checksum is bad
Jeff Haoea7c6292016-11-14 18:10:16 -0800107 options.ignore_bad_checksum_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700108 break;
109 case 'l': // layout
110 if (strcmp(optarg, "plain") == 0) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800111 options.output_format_ = kOutputPlain;
David Sehr7629f602016-08-07 16:01:51 -0700112 } else if (strcmp(optarg, "xml") == 0) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800113 options.output_format_ = kOutputXml;
114 options.verbose_ = false;
David Sehr7629f602016-08-07 16:01:51 -0700115 } else {
116 want_usage = true;
117 }
118 break;
Jeff Haoea7c6292016-11-14 18:10:16 -0800119 case 'm': // output dex files to a memmap
120 options.output_to_memmap_ = true;
121 break;
David Sehr7629f602016-08-07 16:01:51 -0700122 case 'o': // output file
Jeff Haoea7c6292016-11-14 18:10:16 -0800123 options.output_file_name_ = optarg;
David Sehr7629f602016-08-07 16:01:51 -0700124 break;
David Sehrcdcfde72016-09-26 07:44:04 -0700125 case 'p': // profile file
Jeff Haoea7c6292016-11-14 18:10:16 -0800126 options.profile_file_name_ = optarg;
David Sehrcdcfde72016-09-26 07:44:04 -0700127 break;
128 case 's': // visualize access pattern
Jeff Haoea7c6292016-11-14 18:10:16 -0800129 options.visualize_pattern_ = true;
130 options.verbose_ = false;
David Sehrcdcfde72016-09-26 07:44:04 -0700131 break;
David Sehr93357492017-03-09 08:02:44 -0800132 case 't': // display section statistics
133 options.show_section_statistics_ = true;
134 options.verbose_ = false;
135 break;
Jeff Haoec7f1a92017-03-13 16:24:24 -0700136 case 'v': // verify output
137 options.verify_output_ = true;
138 break;
Jeff Haoa8621002016-10-04 18:13:44 +0000139 case 'w': // output dex files directory
Jeff Haoea7c6292016-11-14 18:10:16 -0800140 options.output_dex_directory_ = optarg;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700141 break;
David Sehr7629f602016-08-07 16:01:51 -0700142 default:
143 want_usage = true;
144 break;
145 } // switch
146 } // while
147
148 // Detect early problems.
149 if (optind == argc) {
150 fprintf(stderr, "%s: no file specified\n", kProgramName);
151 want_usage = true;
152 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800153 if (options.checksum_only_ && options.ignore_bad_checksum_) {
David Sehr7629f602016-08-07 16:01:51 -0700154 fprintf(stderr, "Can't specify both -c and -i\n");
155 want_usage = true;
156 }
157 if (want_usage) {
158 Usage();
159 return 2;
160 }
161
162 // Open alternative output file.
Jeff Haoea7c6292016-11-14 18:10:16 -0800163 FILE* out_file = stdout;
164 if (options.output_file_name_) {
165 out_file = fopen(options.output_file_name_, "w");
166 if (!out_file) {
167 fprintf(stderr, "Can't open %s\n", options.output_file_name_);
David Sehr7629f602016-08-07 16:01:51 -0700168 return 1;
169 }
170 }
171
David Sehrcdcfde72016-09-26 07:44:04 -0700172 // Open profile file.
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700173 std::unique_ptr<ProfileCompilationInfo> profile_info;
Jeff Haoea7c6292016-11-14 18:10:16 -0800174 if (options.profile_file_name_) {
175 int profile_fd = open(options.profile_file_name_, O_RDONLY);
David Sehrcdcfde72016-09-26 07:44:04 -0700176 if (profile_fd < 0) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800177 fprintf(stderr, "Can't open %s\n", options.profile_file_name_);
David Sehrcdcfde72016-09-26 07:44:04 -0700178 return 1;
179 }
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700180 profile_info.reset(new ProfileCompilationInfo());
Jeff Haoea7c6292016-11-14 18:10:16 -0800181 if (!profile_info->Load(profile_fd)) {
182 fprintf(stderr, "Can't read profile info from %s\n", options.profile_file_name_);
David Sehrcdcfde72016-09-26 07:44:04 -0700183 return 1;
184 }
185 }
186
Jeff Haoea7c6292016-11-14 18:10:16 -0800187 // Create DexLayout instance.
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700188 DexLayout dex_layout(options, profile_info.get(), out_file);
Jeff Haoea7c6292016-11-14 18:10:16 -0800189
David Sehr7629f602016-08-07 16:01:51 -0700190 // Process all files supplied on command line.
191 int result = 0;
192 while (optind < argc) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800193 result |= dex_layout.ProcessFile(argv[optind++]);
David Sehr7629f602016-08-07 16:01:51 -0700194 } // while
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700195
196 if (options.output_file_name_) {
197 CHECK(out_file != nullptr && out_file != stdout);
198 fclose(out_file);
199 }
200
David Sehr7629f602016-08-07 16:01:51 -0700201 return result != 0;
202}
203
204} // namespace art
205
206int main(int argc, char** argv) {
207 return art::DexlayoutDriver(argc, argv);
208}