blob: 83fb99a734efff28f415121601b6eeaa1500441d [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.
Calin Juravle33083d62017-01-18 15:29:12 -080035#include "jit/profile_compilation_info.h"
David Sehr7629f602016-08-07 16:01:51 -070036#include "mem_map.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) {
David Sehrcdcfde72016-09-26 07:44:04 -070047 fprintf(stderr, "Copyright (C) 2016 The Android Open Source Project\n\n");
48 fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile] [-p profile]"
Jeff Haoec7f1a92017-03-13 16:24:24 -070049 " [-s] [-t] [-v] [-w directory] dexfile...\n\n", kProgramName);
David Sehr7629f602016-08-07 16:01:51 -070050 fprintf(stderr, " -a : display annotations\n");
51 fprintf(stderr, " -b : build dex_ir\n");
52 fprintf(stderr, " -c : verify checksum and exit\n");
53 fprintf(stderr, " -d : disassemble code sections\n");
54 fprintf(stderr, " -e : display exported items only\n");
55 fprintf(stderr, " -f : display summary information from file header\n");
David Sehr7629f602016-08-07 16:01:51 -070056 fprintf(stderr, " -h : display file header details\n");
57 fprintf(stderr, " -i : ignore checksum failures\n");
58 fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
59 fprintf(stderr, " -o : output file name (defaults to stdout)\n");
David Sehrcdcfde72016-09-26 07:44:04 -070060 fprintf(stderr, " -p : profile file name (defaults to no profile)\n");
61 fprintf(stderr, " -s : visualize reference pattern\n");
David Sehr93357492017-03-09 08:02:44 -080062 fprintf(stderr, " -t : display file section sizes\n");
Jeff Haoec7f1a92017-03-13 16:24:24 -070063 fprintf(stderr, " -v : verify output file is canonical to input (IR level comparison)\n");
Jeff Haoa8621002016-10-04 18:13:44 +000064 fprintf(stderr, " -w : output dex directory \n");
Mathieu Chartier5c362202018-01-17 14:52:55 -080065 fprintf(stderr, " -x : compact dex generation level, either 'none' or 'fast'\n");
David Sehr7629f602016-08-07 16:01:51 -070066}
67
68/*
69 * Main driver of the dexlayout utility.
70 */
71int DexlayoutDriver(int argc, char** argv) {
72 // Art specific set up.
Andreas Gampe51d80cc2017-06-21 21:05:13 -070073 InitLogging(argv, Runtime::Abort);
David Sehr7629f602016-08-07 16:01:51 -070074 MemMap::Init();
75
Jeff Haoea7c6292016-11-14 18:10:16 -080076 Options options;
77 options.dump_ = true;
78 options.verbose_ = true;
David Sehr7629f602016-08-07 16:01:51 -070079 bool want_usage = false;
David Sehr7629f602016-08-07 16:01:51 -070080
81 // Parse all arguments.
82 while (1) {
Mathieu Chartier5c362202018-01-17 14:52:55 -080083 const int ic = getopt(argc, argv, "abcdefghil:mo:p:stvw:x:");
David Sehr7629f602016-08-07 16:01:51 -070084 if (ic < 0) {
85 break; // done
86 }
87 switch (ic) {
88 case 'a': // display annotations
Jeff Haoea7c6292016-11-14 18:10:16 -080089 options.show_annotations_ = true;
David Sehr7629f602016-08-07 16:01:51 -070090 break;
91 case 'b': // build dex_ir
Jeff Haoea7c6292016-11-14 18:10:16 -080092 options.build_dex_ir_ = true;
David Sehr7629f602016-08-07 16:01:51 -070093 break;
94 case 'c': // verify the checksum then exit
Jeff Haoea7c6292016-11-14 18:10:16 -080095 options.checksum_only_ = true;
David Sehr7629f602016-08-07 16:01:51 -070096 break;
97 case 'd': // disassemble Dalvik instructions
Jeff Haoea7c6292016-11-14 18:10:16 -080098 options.disassemble_ = true;
David Sehr7629f602016-08-07 16:01:51 -070099 break;
100 case 'e': // exported items only
Jeff Haoea7c6292016-11-14 18:10:16 -0800101 options.exports_only_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700102 break;
103 case 'f': // display outer file header
Jeff Haoea7c6292016-11-14 18:10:16 -0800104 options.show_file_headers_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700105 break;
David Sehr7629f602016-08-07 16:01:51 -0700106 case 'h': // display section headers, i.e. all meta-data
Jeff Haoea7c6292016-11-14 18:10:16 -0800107 options.show_section_headers_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700108 break;
109 case 'i': // continue even if checksum is bad
Jeff Haoea7c6292016-11-14 18:10:16 -0800110 options.ignore_bad_checksum_ = true;
David Sehr7629f602016-08-07 16:01:51 -0700111 break;
112 case 'l': // layout
113 if (strcmp(optarg, "plain") == 0) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800114 options.output_format_ = kOutputPlain;
David Sehr7629f602016-08-07 16:01:51 -0700115 } else if (strcmp(optarg, "xml") == 0) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800116 options.output_format_ = kOutputXml;
117 options.verbose_ = false;
David Sehr7629f602016-08-07 16:01:51 -0700118 } else {
119 want_usage = true;
120 }
121 break;
Jeff Haoea7c6292016-11-14 18:10:16 -0800122 case 'm': // output dex files to a memmap
123 options.output_to_memmap_ = true;
124 break;
David Sehr7629f602016-08-07 16:01:51 -0700125 case 'o': // output file
Jeff Haoea7c6292016-11-14 18:10:16 -0800126 options.output_file_name_ = optarg;
David Sehr7629f602016-08-07 16:01:51 -0700127 break;
David Sehrcdcfde72016-09-26 07:44:04 -0700128 case 'p': // profile file
Jeff Haoea7c6292016-11-14 18:10:16 -0800129 options.profile_file_name_ = optarg;
David Sehrcdcfde72016-09-26 07:44:04 -0700130 break;
131 case 's': // visualize access pattern
Jeff Haoea7c6292016-11-14 18:10:16 -0800132 options.visualize_pattern_ = true;
133 options.verbose_ = false;
David Sehrcdcfde72016-09-26 07:44:04 -0700134 break;
David Sehr93357492017-03-09 08:02:44 -0800135 case 't': // display section statistics
136 options.show_section_statistics_ = true;
137 options.verbose_ = false;
138 break;
Jeff Haoec7f1a92017-03-13 16:24:24 -0700139 case 'v': // verify output
140 options.verify_output_ = true;
141 break;
Jeff Haoa8621002016-10-04 18:13:44 +0000142 case 'w': // output dex files directory
Jeff Haoea7c6292016-11-14 18:10:16 -0800143 options.output_dex_directory_ = optarg;
Jeff Hao3ab96b42016-09-09 18:35:01 -0700144 break;
Mathieu Chartier5c362202018-01-17 14:52:55 -0800145 case 'x': // compact dex level
146 if (strcmp(optarg, "none") == 0) {
147 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
148 } else if (strcmp(optarg, "fast") == 0) {
149 options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelFast;
150 } else {
151 want_usage = true;
152 }
153 break;
David Sehr7629f602016-08-07 16:01:51 -0700154 default:
155 want_usage = true;
156 break;
157 } // switch
158 } // while
159
160 // Detect early problems.
161 if (optind == argc) {
162 fprintf(stderr, "%s: no file specified\n", kProgramName);
163 want_usage = true;
164 }
Jeff Haoea7c6292016-11-14 18:10:16 -0800165 if (options.checksum_only_ && options.ignore_bad_checksum_) {
David Sehr7629f602016-08-07 16:01:51 -0700166 fprintf(stderr, "Can't specify both -c and -i\n");
167 want_usage = true;
168 }
169 if (want_usage) {
170 Usage();
171 return 2;
172 }
173
174 // Open alternative output file.
Jeff Haoea7c6292016-11-14 18:10:16 -0800175 FILE* out_file = stdout;
176 if (options.output_file_name_) {
177 out_file = fopen(options.output_file_name_, "w");
178 if (!out_file) {
179 fprintf(stderr, "Can't open %s\n", options.output_file_name_);
David Sehr7629f602016-08-07 16:01:51 -0700180 return 1;
181 }
182 }
183
David Sehrcdcfde72016-09-26 07:44:04 -0700184 // Open profile file.
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700185 std::unique_ptr<ProfileCompilationInfo> profile_info;
Jeff Haoea7c6292016-11-14 18:10:16 -0800186 if (options.profile_file_name_) {
187 int profile_fd = open(options.profile_file_name_, O_RDONLY);
David Sehrcdcfde72016-09-26 07:44:04 -0700188 if (profile_fd < 0) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800189 fprintf(stderr, "Can't open %s\n", options.profile_file_name_);
David Sehrcdcfde72016-09-26 07:44:04 -0700190 return 1;
191 }
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700192 profile_info.reset(new ProfileCompilationInfo());
Jeff Haoea7c6292016-11-14 18:10:16 -0800193 if (!profile_info->Load(profile_fd)) {
194 fprintf(stderr, "Can't read profile info from %s\n", options.profile_file_name_);
David Sehrcdcfde72016-09-26 07:44:04 -0700195 return 1;
196 }
197 }
198
Jeff Haoea7c6292016-11-14 18:10:16 -0800199 // Create DexLayout instance.
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700200 DexLayout dex_layout(options, profile_info.get(), out_file);
Jeff Haoea7c6292016-11-14 18:10:16 -0800201
David Sehr7629f602016-08-07 16:01:51 -0700202 // Process all files supplied on command line.
203 int result = 0;
204 while (optind < argc) {
Jeff Haoea7c6292016-11-14 18:10:16 -0800205 result |= dex_layout.ProcessFile(argv[optind++]);
David Sehr7629f602016-08-07 16:01:51 -0700206 } // while
Andreas Gampe08ae77f2017-04-26 22:02:33 -0700207
208 if (options.output_file_name_) {
209 CHECK(out_file != nullptr && out_file != stdout);
210 fclose(out_file);
211 }
212
David Sehr7629f602016-08-07 16:01:51 -0700213 return result != 0;
214}
215
216} // namespace art
217
218int main(int argc, char** argv) {
219 return art::DexlayoutDriver(argc, argv);
220}