blob: 988c612767ec289393936ca9078f078eb1c43d67 [file] [log] [blame]
Calin Juravle36eb3132017-01-13 16:32:38 -08001/*
2 * Copyright (C) 2017 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
17#include <string>
Vladimir Markoe5125562019-02-06 17:38:26 +000018#include <string_view>
Calin Juravle36eb3132017-01-13 16:32:38 -080019
20#include "android-base/stringprintf.h"
21#include "android-base/strings.h"
David Sehr891a50e2017-10-27 17:01:07 -070022#include "base/file_utils.h"
Vladimir Markoe5125562019-02-06 17:38:26 +000023#include "base/logging.h" // For InitLogging.
24#include "base/mutex.h"
25#include "base/os.h"
26#include "base/string_view_cpp20.h"
27#include "base/utils.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080028#include "compiler_filter.h"
Calin Juravle20c46442017-09-12 00:54:26 -070029#include "class_loader_context.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080031#include "noop_compiler_callbacks.h"
32#include "oat_file_assistant.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080033#include "runtime.h"
34#include "thread-inl.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080035
36namespace art {
37
38// See OatFileAssistant docs for the meaning of the valid return codes.
39enum ReturnCodes {
40 kNoDexOptNeeded = 0,
41 kDex2OatFromScratch = 1,
42 kDex2OatForBootImageOat = 2,
43 kDex2OatForFilterOat = 3,
Vladimir Markoe0669322018-09-03 15:44:54 +010044 kDex2OatForBootImageOdex = 4,
45 kDex2OatForFilterOdex = 5,
Calin Juravle36eb3132017-01-13 16:32:38 -080046
47 kErrorInvalidArguments = 101,
48 kErrorCannotCreateRuntime = 102,
49 kErrorUnknownDexOptNeeded = 103
50};
51
52static int original_argc;
53static char** original_argv;
54
55static std::string CommandLine() {
56 std::vector<std::string> command;
Andreas Gampe2a487eb2018-11-19 11:41:22 -080057 command.reserve(original_argc);
Calin Juravle36eb3132017-01-13 16:32:38 -080058 for (int i = 0; i < original_argc; ++i) {
59 command.push_back(original_argv[i]);
60 }
61 return android::base::Join(command, ' ');
62}
63
64static void UsageErrorV(const char* fmt, va_list ap) {
65 std::string error;
66 android::base::StringAppendV(&error, fmt, ap);
67 LOG(ERROR) << error;
68}
69
70static void UsageError(const char* fmt, ...) {
71 va_list ap;
72 va_start(ap, fmt);
73 UsageErrorV(fmt, ap);
74 va_end(ap);
75}
76
77NO_RETURN static void Usage(const char *fmt, ...) {
78 va_list ap;
79 va_start(ap, fmt);
80 UsageErrorV(fmt, ap);
81 va_end(ap);
82
83 UsageError("Command: %s", CommandLine().c_str());
84 UsageError(" Performs a dexopt analysis on the given dex file and returns whether or not");
85 UsageError(" the dex file needs to be dexopted.");
86 UsageError("Usage: dexoptanalyzer [options]...");
87 UsageError("");
88 UsageError(" --dex-file=<filename>: the dex file which should be analyzed.");
89 UsageError("");
90 UsageError(" --isa=<string>: the instruction set for which the analysis should be performed.");
91 UsageError("");
92 UsageError(" --compiler-filter=<string>: the target compiler filter to be used as reference");
93 UsageError(" when deciding if the dex file needs to be optimized.");
94 UsageError("");
95 UsageError(" --assume-profile-changed: assumes the profile information has changed");
96 UsageError(" when deciding if the dex file needs to be optimized.");
97 UsageError("");
98 UsageError(" --image=<filename>: optional, the image to be used to decide if the associated");
99 UsageError(" oat file is up to date. Defaults to $ANDROID_ROOT/framework/boot.art.");
100 UsageError(" Example: --image=/system/framework/boot.art");
101 UsageError("");
Vladimir Marko813b9142018-11-29 11:20:07 +0000102 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
103 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
104 UsageError(" Use a separate --runtime-arg switch for each argument.");
105 UsageError(" Example: --runtime-arg -Xms256m");
106 UsageError("");
Calin Juravle36eb3132017-01-13 16:32:38 -0800107 UsageError(" --android-data=<directory>: optional, the directory which should be used as");
108 UsageError(" android-data. By default ANDROID_DATA env variable is used.");
109 UsageError("");
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700110 UsageError(" --oat-fd=number: file descriptor of the oat file which should be analyzed");
111 UsageError("");
112 UsageError(" --vdex-fd=number: file descriptor of the vdex file corresponding to the oat file");
113 UsageError("");
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700114 UsageError(" --zip-fd=number: specifies a file descriptor corresponding to the dex file.");
115 UsageError("");
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700116 UsageError(" --downgrade: optional, if the purpose of dexopt is to downgrade the dex file");
117 UsageError(" By default, dexopt considers upgrade case.");
118 UsageError("");
Calin Juravle36eb3132017-01-13 16:32:38 -0800119 UsageError("Return code:");
120 UsageError(" To make it easier to integrate with the internal tools this command will make");
121 UsageError(" available its result (dexoptNeeded) as the exit/return code. i.e. it will not");
122 UsageError(" return 0 for success and a non zero values for errors as the conventional");
123 UsageError(" commands. The following return codes are possible:");
124 UsageError(" kNoDexOptNeeded = 0");
125 UsageError(" kDex2OatFromScratch = 1");
126 UsageError(" kDex2OatForBootImageOat = 2");
127 UsageError(" kDex2OatForFilterOat = 3");
Vladimir Markoe0669322018-09-03 15:44:54 +0100128 UsageError(" kDex2OatForBootImageOdex = 4");
129 UsageError(" kDex2OatForFilterOdex = 5");
Calin Juravle36eb3132017-01-13 16:32:38 -0800130
131 UsageError(" kErrorInvalidArguments = 101");
132 UsageError(" kErrorCannotCreateRuntime = 102");
133 UsageError(" kErrorUnknownDexOptNeeded = 103");
134 UsageError("");
135
136 exit(kErrorInvalidArguments);
137}
138
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100139class DexoptAnalyzer final {
Calin Juravle36eb3132017-01-13 16:32:38 -0800140 public:
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700141 DexoptAnalyzer() :
142 assume_profile_changed_(false),
143 downgrade_(false) {}
Calin Juravle36eb3132017-01-13 16:32:38 -0800144
145 void ParseArgs(int argc, char **argv) {
146 original_argc = argc;
147 original_argv = argv;
148
David Sehrc431b9d2018-03-02 12:01:51 -0800149 Locks::Init();
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700150 InitLogging(argv, Runtime::Abort);
Calin Juravle36eb3132017-01-13 16:32:38 -0800151 // Skip over the command name.
152 argv++;
153 argc--;
154
155 if (argc == 0) {
156 Usage("No arguments specified");
157 }
158
159 for (int i = 0; i < argc; ++i) {
Vladimir Markoe5125562019-02-06 17:38:26 +0000160 const char* raw_option = argv[i];
161 const std::string_view option(raw_option);
Calin Juravle36eb3132017-01-13 16:32:38 -0800162 if (option == "--assume-profile-changed") {
163 assume_profile_changed_ = true;
Vladimir Markoe5125562019-02-06 17:38:26 +0000164 } else if (StartsWith(option, "--dex-file=")) {
165 dex_file_ = std::string(option.substr(strlen("--dex-file=")));
166 } else if (StartsWith(option, "--compiler-filter=")) {
167 const char* filter_str = raw_option + strlen("--compiler-filter=");
168 if (!CompilerFilter::ParseCompilerFilter(filter_str, &compiler_filter_)) {
169 Usage("Invalid compiler filter '%s'", raw_option);
Calin Juravle36eb3132017-01-13 16:32:38 -0800170 }
Vladimir Markoe5125562019-02-06 17:38:26 +0000171 } else if (StartsWith(option, "--isa=")) {
172 const char* isa_str = raw_option + strlen("--isa=");
173 isa_ = GetInstructionSetFromString(isa_str);
Vladimir Marko33bff252017-11-01 14:35:42 +0000174 if (isa_ == InstructionSet::kNone) {
Vladimir Markoe5125562019-02-06 17:38:26 +0000175 Usage("Invalid isa '%s'", raw_option);
Calin Juravle36eb3132017-01-13 16:32:38 -0800176 }
Vladimir Markoe5125562019-02-06 17:38:26 +0000177 } else if (StartsWith(option, "--image=")) {
178 image_ = std::string(option.substr(strlen("--image=")));
Vladimir Marko813b9142018-11-29 11:20:07 +0000179 } else if (option == "--runtime-arg") {
180 if (i + 1 == argc) {
181 Usage("Missing argument for --runtime-arg\n");
182 }
183 ++i;
184 runtime_args_.push_back(argv[i]);
Vladimir Markoe5125562019-02-06 17:38:26 +0000185 } else if (StartsWith(option, "--android-data=")) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800186 // Overwrite android-data if needed (oat file assistant relies on a valid directory to
187 // compute dalvik-cache folder). This is mostly used in tests.
Vladimir Markoe5125562019-02-06 17:38:26 +0000188 const char* new_android_data = raw_option + strlen("--android-data=");
189 setenv("ANDROID_DATA", new_android_data, 1);
190 } else if (option == "--downgrade") {
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700191 downgrade_ = true;
Vladimir Markoe5125562019-02-06 17:38:26 +0000192 } else if (StartsWith(option, "--oat-fd=")) {
193 oat_fd_ = std::stoi(std::string(option.substr(strlen("--oat-fd="))), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700194 if (oat_fd_ < 0) {
195 Usage("Invalid --oat-fd %d", oat_fd_);
196 }
Vladimir Markoe5125562019-02-06 17:38:26 +0000197 } else if (StartsWith(option, "--vdex-fd=")) {
198 vdex_fd_ = std::stoi(std::string(option.substr(strlen("--vdex-fd="))), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700199 if (vdex_fd_ < 0) {
200 Usage("Invalid --vdex-fd %d", vdex_fd_);
201 }
Vladimir Markoe5125562019-02-06 17:38:26 +0000202 } else if (StartsWith(option, "--zip-fd=")) {
203 zip_fd_ = std::stoi(std::string(option.substr(strlen("--zip-fd="))), nullptr, 0);
204 if (zip_fd_ < 0) {
205 Usage("Invalid --zip-fd %d", zip_fd_);
206 }
207 } else if (StartsWith(option, "--class-loader-context=")) {
208 context_str_ = std::string(option.substr(strlen("--class-loader-context=")));
Calin Juravle20c46442017-09-12 00:54:26 -0700209 } else {
Vladimir Markoe5125562019-02-06 17:38:26 +0000210 Usage("Unknown argument '%s'", raw_option);
Calin Juravle20c46442017-09-12 00:54:26 -0700211 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800212 }
213
214 if (image_.empty()) {
215 // If we don't receive the image, try to use the default one.
216 // Tests may specify a different image (e.g. core image).
217 std::string error_msg;
218 image_ = GetDefaultBootImageLocation(&error_msg);
219
220 if (image_.empty()) {
221 LOG(ERROR) << error_msg;
222 Usage("--image unspecified and ANDROID_ROOT not set or image file does not exist.");
223 }
224 }
225 }
226
227 bool CreateRuntime() {
228 RuntimeOptions options;
229 // The image could be custom, so make sure we explicitly pass it.
230 std::string img = "-Ximage:" + image_;
Vladimir Marko813b9142018-11-29 11:20:07 +0000231 options.push_back(std::make_pair(img, nullptr));
Calin Juravle36eb3132017-01-13 16:32:38 -0800232 // The instruction set of the image should match the instruction set we will test.
233 const void* isa_opt = reinterpret_cast<const void*>(GetInstructionSetString(isa_));
234 options.push_back(std::make_pair("imageinstructionset", isa_opt));
Vladimir Marko813b9142018-11-29 11:20:07 +0000235 // Explicit runtime args.
236 for (const char* runtime_arg : runtime_args_) {
237 options.push_back(std::make_pair(runtime_arg, nullptr));
238 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800239 // Disable libsigchain. We don't don't need it to evaluate DexOptNeeded status.
240 options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
241 // Pretend we are a compiler so that we can re-use the same infrastructure to load a different
242 // ISA image and minimize the amount of things that get started.
243 NoopCompilerCallbacks callbacks;
244 options.push_back(std::make_pair("compilercallbacks", &callbacks));
245 // Make sure we don't attempt to relocate. The tool should only retrieve the DexOptNeeded
246 // status and not attempt to relocate the boot image.
247 options.push_back(std::make_pair("-Xnorelocate", nullptr));
248
249 if (!Runtime::Create(options, false)) {
250 LOG(ERROR) << "Unable to initialize runtime";
251 return false;
252 }
253 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
254 // Runtime::Start. Give it away now.
255 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
256
257 return true;
258 }
259
260 int GetDexOptNeeded() {
Calin Juravle36eb3132017-01-13 16:32:38 -0800261 if (!CreateRuntime()) {
262 return kErrorCannotCreateRuntime;
263 }
Andreas Gampe39f44b72017-04-26 22:00:04 -0700264 std::unique_ptr<Runtime> runtime(Runtime::Current());
265
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000266 // Only when the runtime is created can we create the class loader context: the
267 // class loader context will open dex file and use the MemMap global lock that the
268 // runtime owns.
269 std::unique_ptr<ClassLoaderContext> class_loader_context;
270 if (!context_str_.empty()) {
271 class_loader_context = ClassLoaderContext::Create(context_str_);
272 if (class_loader_context == nullptr) {
273 Usage("Invalid --class-loader-context '%s'", context_str_.c_str());
274 }
275 }
276
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700277 std::unique_ptr<OatFileAssistant> oat_file_assistant;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700278 oat_file_assistant = std::make_unique<OatFileAssistant>(dex_file_.c_str(),
279 isa_,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700280 /*load_executable=*/ false,
281 /*only_load_system_executable=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700282 vdex_fd_,
283 oat_fd_,
284 zip_fd_);
Calin Juravle36eb3132017-01-13 16:32:38 -0800285 // Always treat elements of the bootclasspath as up-to-date.
286 // TODO(calin): this check should be in OatFileAssistant.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700287 if (oat_file_assistant->IsInBootClassPath()) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800288 return kNoDexOptNeeded;
289 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700290
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700291 int dexoptNeeded = oat_file_assistant->GetDexOptNeeded(
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000292 compiler_filter_, assume_profile_changed_, downgrade_, class_loader_context.get());
Calin Juravle36eb3132017-01-13 16:32:38 -0800293
294 // Convert OatFileAssitant codes to dexoptanalyzer codes.
295 switch (dexoptNeeded) {
296 case OatFileAssistant::kNoDexOptNeeded: return kNoDexOptNeeded;
297 case OatFileAssistant::kDex2OatFromScratch: return kDex2OatFromScratch;
298 case OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOat;
299 case OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOat;
Calin Juravle36eb3132017-01-13 16:32:38 -0800300
301 case -OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOdex;
302 case -OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOdex;
Calin Juravle36eb3132017-01-13 16:32:38 -0800303 default:
304 LOG(ERROR) << "Unknown dexoptNeeded " << dexoptNeeded;
305 return kErrorUnknownDexOptNeeded;
306 }
307 }
308
309 private:
310 std::string dex_file_;
311 InstructionSet isa_;
312 CompilerFilter::Filter compiler_filter_;
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000313 std::string context_str_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800314 bool assume_profile_changed_;
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700315 bool downgrade_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800316 std::string image_;
Vladimir Marko813b9142018-11-29 11:20:07 +0000317 std::vector<const char*> runtime_args_;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700318 int oat_fd_ = -1;
319 int vdex_fd_ = -1;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700320 // File descriptor corresponding to apk, dex_file, or zip.
321 int zip_fd_ = -1;
Calin Juravle36eb3132017-01-13 16:32:38 -0800322};
323
324static int dexoptAnalyze(int argc, char** argv) {
325 DexoptAnalyzer analyzer;
326
327 // Parse arguments. Argument mistakes will lead to exit(kErrorInvalidArguments) in UsageError.
328 analyzer.ParseArgs(argc, argv);
329 return analyzer.GetDexOptNeeded();
330}
331
332} // namespace art
333
334int main(int argc, char **argv) {
335 return art::dexoptAnalyze(argc, argv);
336}