blob: a6fcd024145108bd73c2908f4890449880f66cb3 [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
Orion Hodsona4731bd2020-11-16 14:56:50 +000017#include "dexoptanalyzer.h"
18
David Brazdil89821862019-03-19 13:57:43 +000019#include <iostream>
Calin Juravle36eb3132017-01-13 16:32:38 -080020#include <string>
Vladimir Markoe5125562019-02-06 17:38:26 +000021#include <string_view>
Calin Juravle36eb3132017-01-13 16:32:38 -080022
23#include "android-base/stringprintf.h"
24#include "android-base/strings.h"
Eric Holkc7ac91b2021-02-04 21:44:01 +000025#include "base/compiler_filter.h"
David Sehr891a50e2017-10-27 17:01:07 -070026#include "base/file_utils.h"
Vladimir Markoe5125562019-02-06 17:38:26 +000027#include "base/logging.h" // For InitLogging.
28#include "base/mutex.h"
29#include "base/os.h"
30#include "base/string_view_cpp20.h"
31#include "base/utils.h"
Orion Hodson1da77262021-02-10 14:03:59 +000032#include "class_linker.h"
Calin Juravle20c46442017-09-12 00:54:26 -070033#include "class_loader_context.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file.h"
Orion Hodson1da77262021-02-10 14:03:59 +000035#include "gc/heap.h"
36#include "gc/space/image_space.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080037#include "noop_compiler_callbacks.h"
Orion Hodson0a737622021-02-26 13:10:10 +000038#include "oat.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080039#include "oat_file_assistant.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080040#include "runtime.h"
41#include "thread-inl.h"
Orion Hodson1da77262021-02-10 14:03:59 +000042#include "vdex_file.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080043
44namespace art {
Orion Hodsona4731bd2020-11-16 14:56:50 +000045namespace dexoptanalyzer {
Calin Juravle36eb3132017-01-13 16:32:38 -080046
47static int original_argc;
48static char** original_argv;
49
50static std::string CommandLine() {
51 std::vector<std::string> command;
Andreas Gampe2a487eb2018-11-19 11:41:22 -080052 command.reserve(original_argc);
Calin Juravle36eb3132017-01-13 16:32:38 -080053 for (int i = 0; i < original_argc; ++i) {
54 command.push_back(original_argv[i]);
55 }
56 return android::base::Join(command, ' ');
57}
58
59static void UsageErrorV(const char* fmt, va_list ap) {
60 std::string error;
61 android::base::StringAppendV(&error, fmt, ap);
62 LOG(ERROR) << error;
63}
64
65static void UsageError(const char* fmt, ...) {
66 va_list ap;
67 va_start(ap, fmt);
68 UsageErrorV(fmt, ap);
69 va_end(ap);
70}
71
72NO_RETURN static void Usage(const char *fmt, ...) {
73 va_list ap;
74 va_start(ap, fmt);
75 UsageErrorV(fmt, ap);
76 va_end(ap);
77
78 UsageError("Command: %s", CommandLine().c_str());
79 UsageError(" Performs a dexopt analysis on the given dex file and returns whether or not");
80 UsageError(" the dex file needs to be dexopted.");
81 UsageError("Usage: dexoptanalyzer [options]...");
82 UsageError("");
83 UsageError(" --dex-file=<filename>: the dex file which should be analyzed.");
84 UsageError("");
85 UsageError(" --isa=<string>: the instruction set for which the analysis should be performed.");
86 UsageError("");
87 UsageError(" --compiler-filter=<string>: the target compiler filter to be used as reference");
88 UsageError(" when deciding if the dex file needs to be optimized.");
89 UsageError("");
90 UsageError(" --assume-profile-changed: assumes the profile information has changed");
91 UsageError(" when deciding if the dex file needs to be optimized.");
92 UsageError("");
93 UsageError(" --image=<filename>: optional, the image to be used to decide if the associated");
94 UsageError(" oat file is up to date. Defaults to $ANDROID_ROOT/framework/boot.art.");
95 UsageError(" Example: --image=/system/framework/boot.art");
96 UsageError("");
Vladimir Marko813b9142018-11-29 11:20:07 +000097 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
98 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
99 UsageError(" Use a separate --runtime-arg switch for each argument.");
100 UsageError(" Example: --runtime-arg -Xms256m");
101 UsageError("");
Calin Juravle36eb3132017-01-13 16:32:38 -0800102 UsageError(" --android-data=<directory>: optional, the directory which should be used as");
103 UsageError(" android-data. By default ANDROID_DATA env variable is used.");
104 UsageError("");
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700105 UsageError(" --oat-fd=number: file descriptor of the oat file which should be analyzed");
106 UsageError("");
107 UsageError(" --vdex-fd=number: file descriptor of the vdex file corresponding to the oat file");
108 UsageError("");
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700109 UsageError(" --zip-fd=number: specifies a file descriptor corresponding to the dex file.");
110 UsageError("");
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700111 UsageError(" --downgrade: optional, if the purpose of dexopt is to downgrade the dex file");
112 UsageError(" By default, dexopt considers upgrade case.");
113 UsageError("");
David Brazdil89821862019-03-19 13:57:43 +0000114 UsageError(" --class-loader-context=<string spec>: a string specifying the intended");
115 UsageError(" runtime loading context for the compiled dex files.");
116 UsageError("");
117 UsageError(" --class-loader-context-fds=<fds>: a colon-separated list of file descriptors");
118 UsageError(" for dex files in --class-loader-context. Their order must be the same as");
119 UsageError(" dex files in flattened class loader context.");
120 UsageError("");
121 UsageError(" --flatten-class-loader-context: parse --class-loader-context, flatten it and");
122 UsageError(" print a colon-separated list of its dex files to standard output. Dexopt");
123 UsageError(" needed analysis is not performed when this option is set.");
124 UsageError("");
Orion Hodson1da77262021-02-10 14:03:59 +0000125 UsageError(" --validate-bcp: validates the boot class path files (.art, .oat, .vdex).");
126 UsageError(" Requires --isa and --image options to locate artifacts.");
127 UsageError("");
Calin Juravle36eb3132017-01-13 16:32:38 -0800128 UsageError("Return code:");
129 UsageError(" To make it easier to integrate with the internal tools this command will make");
130 UsageError(" available its result (dexoptNeeded) as the exit/return code. i.e. it will not");
131 UsageError(" return 0 for success and a non zero values for errors as the conventional");
132 UsageError(" commands. The following return codes are possible:");
133 UsageError(" kNoDexOptNeeded = 0");
134 UsageError(" kDex2OatFromScratch = 1");
135 UsageError(" kDex2OatForBootImageOat = 2");
136 UsageError(" kDex2OatForFilterOat = 3");
Vladimir Markoe0669322018-09-03 15:44:54 +0100137 UsageError(" kDex2OatForBootImageOdex = 4");
138 UsageError(" kDex2OatForFilterOdex = 5");
Calin Juravle36eb3132017-01-13 16:32:38 -0800139
140 UsageError(" kErrorInvalidArguments = 101");
141 UsageError(" kErrorCannotCreateRuntime = 102");
142 UsageError(" kErrorUnknownDexOptNeeded = 103");
143 UsageError("");
144
Orion Hodsona4731bd2020-11-16 14:56:50 +0000145 exit(static_cast<int>(ReturnCode::kErrorInvalidArguments));
Calin Juravle36eb3132017-01-13 16:32:38 -0800146}
147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100148class DexoptAnalyzer final {
Calin Juravle36eb3132017-01-13 16:32:38 -0800149 public:
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700150 DexoptAnalyzer() :
David Brazdil89821862019-03-19 13:57:43 +0000151 only_flatten_context_(false),
Greg Kaiser9ca1e102021-02-19 07:50:24 -0800152 only_validate_bcp_(false),
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700153 assume_profile_changed_(false),
154 downgrade_(false) {}
Calin Juravle36eb3132017-01-13 16:32:38 -0800155
156 void ParseArgs(int argc, char **argv) {
157 original_argc = argc;
158 original_argv = argv;
159
David Sehrc431b9d2018-03-02 12:01:51 -0800160 Locks::Init();
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700161 InitLogging(argv, Runtime::Abort);
Calin Juravle36eb3132017-01-13 16:32:38 -0800162 // Skip over the command name.
163 argv++;
164 argc--;
165
166 if (argc == 0) {
167 Usage("No arguments specified");
168 }
169
170 for (int i = 0; i < argc; ++i) {
Vladimir Markoe5125562019-02-06 17:38:26 +0000171 const char* raw_option = argv[i];
172 const std::string_view option(raw_option);
Calin Juravle36eb3132017-01-13 16:32:38 -0800173 if (option == "--assume-profile-changed") {
174 assume_profile_changed_ = true;
Vladimir Markoe5125562019-02-06 17:38:26 +0000175 } else if (StartsWith(option, "--dex-file=")) {
176 dex_file_ = std::string(option.substr(strlen("--dex-file=")));
177 } else if (StartsWith(option, "--compiler-filter=")) {
178 const char* filter_str = raw_option + strlen("--compiler-filter=");
179 if (!CompilerFilter::ParseCompilerFilter(filter_str, &compiler_filter_)) {
180 Usage("Invalid compiler filter '%s'", raw_option);
Calin Juravle36eb3132017-01-13 16:32:38 -0800181 }
Vladimir Markoe5125562019-02-06 17:38:26 +0000182 } else if (StartsWith(option, "--isa=")) {
183 const char* isa_str = raw_option + strlen("--isa=");
184 isa_ = GetInstructionSetFromString(isa_str);
Vladimir Marko33bff252017-11-01 14:35:42 +0000185 if (isa_ == InstructionSet::kNone) {
Vladimir Markoe5125562019-02-06 17:38:26 +0000186 Usage("Invalid isa '%s'", raw_option);
Calin Juravle36eb3132017-01-13 16:32:38 -0800187 }
Vladimir Markoe5125562019-02-06 17:38:26 +0000188 } else if (StartsWith(option, "--image=")) {
189 image_ = std::string(option.substr(strlen("--image=")));
Vladimir Marko813b9142018-11-29 11:20:07 +0000190 } else if (option == "--runtime-arg") {
191 if (i + 1 == argc) {
192 Usage("Missing argument for --runtime-arg\n");
193 }
194 ++i;
195 runtime_args_.push_back(argv[i]);
Vladimir Markoe5125562019-02-06 17:38:26 +0000196 } else if (StartsWith(option, "--android-data=")) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800197 // Overwrite android-data if needed (oat file assistant relies on a valid directory to
198 // compute dalvik-cache folder). This is mostly used in tests.
Vladimir Markoe5125562019-02-06 17:38:26 +0000199 const char* new_android_data = raw_option + strlen("--android-data=");
200 setenv("ANDROID_DATA", new_android_data, 1);
201 } else if (option == "--downgrade") {
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700202 downgrade_ = true;
Vladimir Markoe5125562019-02-06 17:38:26 +0000203 } else if (StartsWith(option, "--oat-fd=")) {
204 oat_fd_ = std::stoi(std::string(option.substr(strlen("--oat-fd="))), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700205 if (oat_fd_ < 0) {
206 Usage("Invalid --oat-fd %d", oat_fd_);
207 }
Vladimir Markoe5125562019-02-06 17:38:26 +0000208 } else if (StartsWith(option, "--vdex-fd=")) {
209 vdex_fd_ = std::stoi(std::string(option.substr(strlen("--vdex-fd="))), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700210 if (vdex_fd_ < 0) {
211 Usage("Invalid --vdex-fd %d", vdex_fd_);
212 }
Vladimir Markoe5125562019-02-06 17:38:26 +0000213 } else if (StartsWith(option, "--zip-fd=")) {
214 zip_fd_ = std::stoi(std::string(option.substr(strlen("--zip-fd="))), nullptr, 0);
215 if (zip_fd_ < 0) {
216 Usage("Invalid --zip-fd %d", zip_fd_);
217 }
218 } else if (StartsWith(option, "--class-loader-context=")) {
219 context_str_ = std::string(option.substr(strlen("--class-loader-context=")));
David Brazdil89821862019-03-19 13:57:43 +0000220 } else if (StartsWith(option, "--class-loader-context-fds=")) {
221 std::string str_context_fds_arg =
222 std::string(option.substr(strlen("--class-loader-context-fds=")));
223 std::vector<std::string> str_fds = android::base::Split(str_context_fds_arg, ":");
224 for (const std::string& str_fd : str_fds) {
225 context_fds_.push_back(std::stoi(str_fd, nullptr, 0));
226 if (context_fds_.back() < 0) {
227 Usage("Invalid --class-loader-context-fds %s", str_context_fds_arg.c_str());
228 }
229 }
230 } else if (option == "--flatten-class-loader-context") {
231 only_flatten_context_ = true;
Orion Hodson1da77262021-02-10 14:03:59 +0000232 } else if (option == "--validate-bcp") {
233 only_validate_bcp_ = true;
Calin Juravle20c46442017-09-12 00:54:26 -0700234 } else {
Vladimir Markoe5125562019-02-06 17:38:26 +0000235 Usage("Unknown argument '%s'", raw_option);
Calin Juravle20c46442017-09-12 00:54:26 -0700236 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800237 }
238
239 if (image_.empty()) {
240 // If we don't receive the image, try to use the default one.
241 // Tests may specify a different image (e.g. core image).
242 std::string error_msg;
243 image_ = GetDefaultBootImageLocation(&error_msg);
244
245 if (image_.empty()) {
246 LOG(ERROR) << error_msg;
247 Usage("--image unspecified and ANDROID_ROOT not set or image file does not exist.");
248 }
249 }
250 }
251
David Brazdil89821862019-03-19 13:57:43 +0000252 bool CreateRuntime() const {
Calin Juravle36eb3132017-01-13 16:32:38 -0800253 RuntimeOptions options;
254 // The image could be custom, so make sure we explicitly pass it.
255 std::string img = "-Ximage:" + image_;
Vladimir Marko813b9142018-11-29 11:20:07 +0000256 options.push_back(std::make_pair(img, nullptr));
Calin Juravle36eb3132017-01-13 16:32:38 -0800257 // The instruction set of the image should match the instruction set we will test.
258 const void* isa_opt = reinterpret_cast<const void*>(GetInstructionSetString(isa_));
259 options.push_back(std::make_pair("imageinstructionset", isa_opt));
Vladimir Marko813b9142018-11-29 11:20:07 +0000260 // Explicit runtime args.
261 for (const char* runtime_arg : runtime_args_) {
262 options.push_back(std::make_pair(runtime_arg, nullptr));
263 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800264 // Disable libsigchain. We don't don't need it to evaluate DexOptNeeded status.
265 options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
266 // Pretend we are a compiler so that we can re-use the same infrastructure to load a different
267 // ISA image and minimize the amount of things that get started.
268 NoopCompilerCallbacks callbacks;
269 options.push_back(std::make_pair("compilercallbacks", &callbacks));
270 // Make sure we don't attempt to relocate. The tool should only retrieve the DexOptNeeded
271 // status and not attempt to relocate the boot image.
272 options.push_back(std::make_pair("-Xnorelocate", nullptr));
273
274 if (!Runtime::Create(options, false)) {
275 LOG(ERROR) << "Unable to initialize runtime";
276 return false;
277 }
278 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
279 // Runtime::Start. Give it away now.
280 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
281
282 return true;
283 }
284
Orion Hodsona4731bd2020-11-16 14:56:50 +0000285 ReturnCode GetDexOptNeeded() const {
Calin Juravle36eb3132017-01-13 16:32:38 -0800286 if (!CreateRuntime()) {
Orion Hodsona4731bd2020-11-16 14:56:50 +0000287 return ReturnCode::kErrorCannotCreateRuntime;
Calin Juravle36eb3132017-01-13 16:32:38 -0800288 }
Andreas Gampe39f44b72017-04-26 22:00:04 -0700289 std::unique_ptr<Runtime> runtime(Runtime::Current());
290
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000291 // Only when the runtime is created can we create the class loader context: the
292 // class loader context will open dex file and use the MemMap global lock that the
293 // runtime owns.
294 std::unique_ptr<ClassLoaderContext> class_loader_context;
295 if (!context_str_.empty()) {
296 class_loader_context = ClassLoaderContext::Create(context_str_);
297 if (class_loader_context == nullptr) {
298 Usage("Invalid --class-loader-context '%s'", context_str_.c_str());
299 }
300 }
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000301 if (class_loader_context != nullptr) {
302 size_t dir_index = dex_file_.rfind('/');
303 std::string classpath_dir = (dir_index != std::string::npos)
304 ? dex_file_.substr(0, dir_index)
305 : "";
306
307 if (!class_loader_context->OpenDexFiles(classpath_dir,
308 context_fds_,
309 /*only_read_checksums=*/ true)) {
310 return ReturnCode::kDex2OatFromScratch;
311 }
312 }
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000313
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700314 std::unique_ptr<OatFileAssistant> oat_file_assistant;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700315 oat_file_assistant = std::make_unique<OatFileAssistant>(dex_file_.c_str(),
316 isa_,
Nicolas Geoffray525fa422021-04-19 07:50:35 +0000317 class_loader_context.get(),
Andreas Gampe9b031f72018-10-04 11:03:34 -0700318 /*load_executable=*/ false,
Orion Hodson094b1cf2021-06-08 09:28:28 +0100319 /*only_load_trusted_executable=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700320 vdex_fd_,
321 oat_fd_,
322 zip_fd_);
Calin Juravle36eb3132017-01-13 16:32:38 -0800323 // Always treat elements of the bootclasspath as up-to-date.
324 // TODO(calin): this check should be in OatFileAssistant.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700325 if (oat_file_assistant->IsInBootClassPath()) {
Orion Hodsona4731bd2020-11-16 14:56:50 +0000326 return ReturnCode::kNoDexOptNeeded;
Calin Juravle36eb3132017-01-13 16:32:38 -0800327 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700328
David Brazdil89821862019-03-19 13:57:43 +0000329 int dexoptNeeded = oat_file_assistant->GetDexOptNeeded(compiler_filter_,
Calin Juravle0a5cad32020-02-14 20:29:26 +0000330 assume_profile_changed_,
331 downgrade_);
Calin Juravle36eb3132017-01-13 16:32:38 -0800332
Orion Hodson1da77262021-02-10 14:03:59 +0000333 // Convert OatFileAssistant codes to dexoptanalyzer codes.
Calin Juravle36eb3132017-01-13 16:32:38 -0800334 switch (dexoptNeeded) {
Orion Hodsona4731bd2020-11-16 14:56:50 +0000335 case OatFileAssistant::kNoDexOptNeeded: return ReturnCode::kNoDexOptNeeded;
336 case OatFileAssistant::kDex2OatFromScratch: return ReturnCode::kDex2OatFromScratch;
337 case OatFileAssistant::kDex2OatForBootImage: return ReturnCode::kDex2OatForBootImageOat;
338 case OatFileAssistant::kDex2OatForFilter: return ReturnCode::kDex2OatForFilterOat;
Calin Juravle36eb3132017-01-13 16:32:38 -0800339
Orion Hodsona4731bd2020-11-16 14:56:50 +0000340 case -OatFileAssistant::kDex2OatForBootImage: return ReturnCode::kDex2OatForBootImageOdex;
341 case -OatFileAssistant::kDex2OatForFilter: return ReturnCode::kDex2OatForFilterOdex;
Calin Juravle36eb3132017-01-13 16:32:38 -0800342 default:
343 LOG(ERROR) << "Unknown dexoptNeeded " << dexoptNeeded;
Orion Hodsona4731bd2020-11-16 14:56:50 +0000344 return ReturnCode::kErrorUnknownDexOptNeeded;
Calin Juravle36eb3132017-01-13 16:32:38 -0800345 }
346 }
347
Orion Hodson1da77262021-02-10 14:03:59 +0000348 // Validates the boot classpath and boot classpath extensions by checking the image checksums,
349 // the oat files and the vdex files.
350 //
351 // Returns `ReturnCode::kNoDexOptNeeded` when all the files are up-to-date,
352 // `ReturnCode::kDex2OatFromScratch` if any of the files are missing or out-of-date, and
353 // `ReturnCode::kErrorCannotCreateRuntime` if the files could not be tested due to problem
354 // creating a runtime.
355 ReturnCode ValidateBcp() const {
356 using ImageSpace = gc::space::ImageSpace;
357
358 if (!CreateRuntime()) {
359 return ReturnCode::kErrorCannotCreateRuntime;
360 }
361 std::unique_ptr<Runtime> runtime(Runtime::Current());
362
363 auto dex_files = ArrayRef<const DexFile* const>(runtime->GetClassLinker()->GetBootClassPath());
364 auto boot_image_spaces = ArrayRef<ImageSpace* const>(runtime->GetHeap()->GetBootImageSpaces());
365 const std::string checksums = ImageSpace::GetBootClassPathChecksums(boot_image_spaces,
366 dex_files);
367
368 std::string error_msg;
369 const std::vector<std::string>& bcp = runtime->GetBootClassPath();
370 const std::vector<std::string>& bcp_locations = runtime->GetBootClassPathLocations();
Victor Hsieh61ffd042021-05-20 15:14:25 -0700371 const std::vector<std::string>& image_locations = runtime->GetImageLocations();
Orion Hodson1da77262021-02-10 14:03:59 +0000372 const std::string bcp_locations_path = android::base::Join(bcp_locations, ':');
373 if (!ImageSpace::VerifyBootClassPathChecksums(checksums,
374 bcp_locations_path,
Victor Hsieh61ffd042021-05-20 15:14:25 -0700375 ArrayRef<const std::string>(image_locations),
Orion Hodson1da77262021-02-10 14:03:59 +0000376 ArrayRef<const std::string>(bcp_locations),
377 ArrayRef<const std::string>(bcp),
378 runtime->GetInstructionSet(),
379 &error_msg)) {
Orion Hodson0a737622021-02-26 13:10:10 +0000380 LOG(INFO) << "Failed to verify boot class path checksums: " << error_msg;
Orion Hodson1da77262021-02-10 14:03:59 +0000381 return ReturnCode::kDex2OatFromScratch;
382 }
383
384 const auto& image_spaces = runtime->GetHeap()->GetBootImageSpaces();
Orion Hodson0a737622021-02-26 13:10:10 +0000385 size_t bcp_component_count = 0;
Orion Hodson1da77262021-02-10 14:03:59 +0000386 for (const auto& image_space : image_spaces) {
Orion Hodson0a737622021-02-26 13:10:10 +0000387 if (!image_space->GetImageHeader().IsValid()) {
388 LOG(INFO) << "Image header is not valid: " << image_space->GetImageFilename();
George Burgess IV27b8cb72021-02-21 15:44:27 -0800389 return ReturnCode::kDex2OatFromScratch;
390 }
Orion Hodson0a737622021-02-26 13:10:10 +0000391 const OatFile* oat_file = image_space->GetOatFile();
392 if (oat_file == nullptr) {
393 const std::string oat_path = ReplaceFileExtension(image_space->GetImageFilename(), "oat");
394 LOG(INFO) << "Oat file missing: " << oat_path;
395 return ReturnCode::kDex2OatFromScratch;
396 }
397 if (!oat_file->GetOatHeader().IsValid() ||
398 !ImageSpace::ValidateOatFile(*oat_file, &error_msg)) {
399 LOG(INFO) << "Oat file is not valid: " << oat_file->GetLocation() << " " << error_msg;
Orion Hodson1da77262021-02-10 14:03:59 +0000400 return ReturnCode::kDex2OatFromScratch;
401 }
402 const VdexFile* vdex_file = oat_file->GetVdexFile();
403 if (vdex_file == nullptr || !vdex_file->IsValid()) {
Orion Hodson0a737622021-02-26 13:10:10 +0000404 LOG(INFO) << "Vdex file is not valid : " << oat_file->GetLocation();
Orion Hodson1da77262021-02-10 14:03:59 +0000405 return ReturnCode::kDex2OatFromScratch;
406 }
Orion Hodson0a737622021-02-26 13:10:10 +0000407 bcp_component_count += image_space->GetComponentCount();
408 }
409
410 // If the number of components encountered in the image spaces does not match the number
411 // of components expected from the boot classpath locations then something is missing.
412 if (bcp_component_count != bcp_locations.size()) {
413 for (size_t i = bcp_component_count; i < bcp_locations.size(); ++i) {
414 LOG(INFO) << "Missing image file for " << bcp_locations[i];
415 }
416 return ReturnCode::kDex2OatFromScratch;
Orion Hodson1da77262021-02-10 14:03:59 +0000417 }
418
419 return ReturnCode::kNoDexOptNeeded;
420 }
421
Orion Hodsona4731bd2020-11-16 14:56:50 +0000422 ReturnCode FlattenClassLoaderContext() const {
David Brazdil89821862019-03-19 13:57:43 +0000423 DCHECK(only_flatten_context_);
424 if (context_str_.empty()) {
Orion Hodsona4731bd2020-11-16 14:56:50 +0000425 return ReturnCode::kErrorInvalidArguments;
David Brazdil89821862019-03-19 13:57:43 +0000426 }
427
428 std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(context_str_);
429 if (context == nullptr) {
430 Usage("Invalid --class-loader-context '%s'", context_str_.c_str());
431 }
432
433 std::cout << context->FlattenDexPaths() << std::flush;
Orion Hodsona4731bd2020-11-16 14:56:50 +0000434 return ReturnCode::kFlattenClassLoaderContextSuccess;
David Brazdil89821862019-03-19 13:57:43 +0000435 }
436
Orion Hodsona4731bd2020-11-16 14:56:50 +0000437 ReturnCode Run() const {
David Brazdil89821862019-03-19 13:57:43 +0000438 if (only_flatten_context_) {
439 return FlattenClassLoaderContext();
Orion Hodson1da77262021-02-10 14:03:59 +0000440 } else if (only_validate_bcp_) {
441 return ValidateBcp();
David Brazdil89821862019-03-19 13:57:43 +0000442 } else {
443 return GetDexOptNeeded();
444 }
445 }
446
Calin Juravle36eb3132017-01-13 16:32:38 -0800447 private:
448 std::string dex_file_;
449 InstructionSet isa_;
450 CompilerFilter::Filter compiler_filter_;
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000451 std::string context_str_;
David Brazdil89821862019-03-19 13:57:43 +0000452 bool only_flatten_context_;
Orion Hodson1da77262021-02-10 14:03:59 +0000453 bool only_validate_bcp_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800454 bool assume_profile_changed_;
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700455 bool downgrade_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800456 std::string image_;
Vladimir Marko813b9142018-11-29 11:20:07 +0000457 std::vector<const char*> runtime_args_;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700458 int oat_fd_ = -1;
459 int vdex_fd_ = -1;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700460 // File descriptor corresponding to apk, dex_file, or zip.
461 int zip_fd_ = -1;
David Brazdil89821862019-03-19 13:57:43 +0000462 std::vector<int> context_fds_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800463};
464
Orion Hodsona4731bd2020-11-16 14:56:50 +0000465static ReturnCode dexoptAnalyze(int argc, char** argv) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800466 DexoptAnalyzer analyzer;
467
468 // Parse arguments. Argument mistakes will lead to exit(kErrorInvalidArguments) in UsageError.
469 analyzer.ParseArgs(argc, argv);
David Brazdil89821862019-03-19 13:57:43 +0000470 return analyzer.Run();
Calin Juravle36eb3132017-01-13 16:32:38 -0800471}
472
Orion Hodsona4731bd2020-11-16 14:56:50 +0000473} // namespace dexoptanalyzer
Calin Juravle36eb3132017-01-13 16:32:38 -0800474} // namespace art
475
476int main(int argc, char **argv) {
Orion Hodsona4731bd2020-11-16 14:56:50 +0000477 art::dexoptanalyzer::ReturnCode return_code = art::dexoptanalyzer::dexoptAnalyze(argc, argv);
478 return static_cast<int>(return_code);
Calin Juravle36eb3132017-01-13 16:32:38 -0800479}