blob: eead2dcf837fac45711b1566ab5234c382299949 [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>
18
Andreas Gampe39b378c2017-12-07 15:44:13 -080019#include "base/logging.h" // For InitLogging.
Calin Juravle36eb3132017-01-13 16:32:38 -080020#include "android-base/stringprintf.h"
21#include "android-base/strings.h"
David Sehr891a50e2017-10-27 17:01:07 -070022#include "base/file_utils.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080023#include "base/logging.h" // For InitLogging.
Calin Juravle36eb3132017-01-13 16:32:38 -080024#include "compiler_filter.h"
Calin Juravle20c46442017-09-12 00:54:26 -070025#include "class_loader_context.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080026#include "dex_file.h"
27#include "noop_compiler_callbacks.h"
28#include "oat_file_assistant.h"
29#include "os.h"
30#include "runtime.h"
31#include "thread-inl.h"
32#include "utils.h"
33
34namespace art {
35
36// See OatFileAssistant docs for the meaning of the valid return codes.
37enum ReturnCodes {
38 kNoDexOptNeeded = 0,
39 kDex2OatFromScratch = 1,
40 kDex2OatForBootImageOat = 2,
41 kDex2OatForFilterOat = 3,
42 kDex2OatForRelocationOat = 4,
43 kDex2OatForBootImageOdex = 5,
44 kDex2OatForFilterOdex = 6,
45 kDex2OatForRelocationOdex = 7,
46
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;
57 for (int i = 0; i < original_argc; ++i) {
58 command.push_back(original_argv[i]);
59 }
60 return android::base::Join(command, ' ');
61}
62
63static void UsageErrorV(const char* fmt, va_list ap) {
64 std::string error;
65 android::base::StringAppendV(&error, fmt, ap);
66 LOG(ERROR) << error;
67}
68
69static void UsageError(const char* fmt, ...) {
70 va_list ap;
71 va_start(ap, fmt);
72 UsageErrorV(fmt, ap);
73 va_end(ap);
74}
75
76NO_RETURN static void Usage(const char *fmt, ...) {
77 va_list ap;
78 va_start(ap, fmt);
79 UsageErrorV(fmt, ap);
80 va_end(ap);
81
82 UsageError("Command: %s", CommandLine().c_str());
83 UsageError(" Performs a dexopt analysis on the given dex file and returns whether or not");
84 UsageError(" the dex file needs to be dexopted.");
85 UsageError("Usage: dexoptanalyzer [options]...");
86 UsageError("");
87 UsageError(" --dex-file=<filename>: the dex file which should be analyzed.");
88 UsageError("");
89 UsageError(" --isa=<string>: the instruction set for which the analysis should be performed.");
90 UsageError("");
91 UsageError(" --compiler-filter=<string>: the target compiler filter to be used as reference");
92 UsageError(" when deciding if the dex file needs to be optimized.");
93 UsageError("");
94 UsageError(" --assume-profile-changed: assumes the profile information has changed");
95 UsageError(" when deciding if the dex file needs to be optimized.");
96 UsageError("");
97 UsageError(" --image=<filename>: optional, the image to be used to decide if the associated");
98 UsageError(" oat file is up to date. Defaults to $ANDROID_ROOT/framework/boot.art.");
99 UsageError(" Example: --image=/system/framework/boot.art");
100 UsageError("");
101 UsageError(" --android-data=<directory>: optional, the directory which should be used as");
102 UsageError(" android-data. By default ANDROID_DATA env variable is used.");
103 UsageError("");
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700104 UsageError(" --oat-fd=number: file descriptor of the oat file which should be analyzed");
105 UsageError("");
106 UsageError(" --vdex-fd=number: file descriptor of the vdex file corresponding to the oat file");
107 UsageError("");
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700108 UsageError(" --zip-fd=number: specifies a file descriptor corresponding to the dex file.");
109 UsageError("");
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700110 UsageError(" --downgrade: optional, if the purpose of dexopt is to downgrade the dex file");
111 UsageError(" By default, dexopt considers upgrade case.");
112 UsageError("");
Calin Juravle36eb3132017-01-13 16:32:38 -0800113 UsageError("Return code:");
114 UsageError(" To make it easier to integrate with the internal tools this command will make");
115 UsageError(" available its result (dexoptNeeded) as the exit/return code. i.e. it will not");
116 UsageError(" return 0 for success and a non zero values for errors as the conventional");
117 UsageError(" commands. The following return codes are possible:");
118 UsageError(" kNoDexOptNeeded = 0");
119 UsageError(" kDex2OatFromScratch = 1");
120 UsageError(" kDex2OatForBootImageOat = 2");
121 UsageError(" kDex2OatForFilterOat = 3");
122 UsageError(" kDex2OatForRelocationOat = 4");
123 UsageError(" kDex2OatForBootImageOdex = 5");
124 UsageError(" kDex2OatForFilterOdex = 6");
125 UsageError(" kDex2OatForRelocationOdex = 7");
126
127 UsageError(" kErrorInvalidArguments = 101");
128 UsageError(" kErrorCannotCreateRuntime = 102");
129 UsageError(" kErrorUnknownDexOptNeeded = 103");
130 UsageError("");
131
132 exit(kErrorInvalidArguments);
133}
134
135class DexoptAnalyzer FINAL {
136 public:
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700137 DexoptAnalyzer() :
138 assume_profile_changed_(false),
139 downgrade_(false) {}
Calin Juravle36eb3132017-01-13 16:32:38 -0800140
141 void ParseArgs(int argc, char **argv) {
142 original_argc = argc;
143 original_argv = argv;
144
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700145 InitLogging(argv, Runtime::Abort);
Calin Juravle36eb3132017-01-13 16:32:38 -0800146 // Skip over the command name.
147 argv++;
148 argc--;
149
150 if (argc == 0) {
151 Usage("No arguments specified");
152 }
153
154 for (int i = 0; i < argc; ++i) {
155 const StringPiece option(argv[i]);
156 if (option == "--assume-profile-changed") {
157 assume_profile_changed_ = true;
158 } else if (option.starts_with("--dex-file=")) {
159 dex_file_ = option.substr(strlen("--dex-file=")).ToString();
160 } else if (option.starts_with("--compiler-filter=")) {
161 std::string filter_str = option.substr(strlen("--compiler-filter=")).ToString();
162 if (!CompilerFilter::ParseCompilerFilter(filter_str.c_str(), &compiler_filter_)) {
163 Usage("Invalid compiler filter '%s'", option.data());
164 }
165 } else if (option.starts_with("--isa=")) {
166 std::string isa_str = option.substr(strlen("--isa=")).ToString();
167 isa_ = GetInstructionSetFromString(isa_str.c_str());
Vladimir Marko33bff252017-11-01 14:35:42 +0000168 if (isa_ == InstructionSet::kNone) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800169 Usage("Invalid isa '%s'", option.data());
170 }
171 } else if (option.starts_with("--image=")) {
172 image_ = option.substr(strlen("--image=")).ToString();
173 } else if (option.starts_with("--android-data=")) {
174 // Overwrite android-data if needed (oat file assistant relies on a valid directory to
175 // compute dalvik-cache folder). This is mostly used in tests.
176 std::string new_android_data = option.substr(strlen("--android-data=")).ToString();
177 setenv("ANDROID_DATA", new_android_data.c_str(), 1);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700178 } else if (option.starts_with("--downgrade")) {
179 downgrade_ = true;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700180 } else if (option.starts_with("--oat-fd")) {
181 oat_fd_ = std::stoi(option.substr(strlen("--oat-fd=")).ToString(), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700182 if (oat_fd_ < 0) {
183 Usage("Invalid --oat-fd %d", oat_fd_);
184 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700185 } else if (option.starts_with("--vdex-fd")) {
186 vdex_fd_ = std::stoi(option.substr(strlen("--vdex-fd=")).ToString(), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700187 if (vdex_fd_ < 0) {
188 Usage("Invalid --vdex-fd %d", vdex_fd_);
189 }
190 } else if (option.starts_with("--zip-fd")) {
191 zip_fd_ = std::stoi(option.substr(strlen("--zip-fd=")).ToString(), nullptr, 0);
192 if (zip_fd_ < 0) {
193 Usage("Invalid --zip-fd %d", zip_fd_);
194 }
Calin Juravle20c46442017-09-12 00:54:26 -0700195 } else if (option.starts_with("--class-loader-context=")) {
196 std::string context_str = option.substr(strlen("--class-loader-context=")).ToString();
197 class_loader_context_ = ClassLoaderContext::Create(context_str);
198 if (class_loader_context_ == nullptr) {
199 Usage("Invalid --class-loader-context '%s'", context_str.c_str());
200 }
201 } else {
202 Usage("Unknown argument '%s'", option.data());
203 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800204 }
205
206 if (image_.empty()) {
207 // If we don't receive the image, try to use the default one.
208 // Tests may specify a different image (e.g. core image).
209 std::string error_msg;
210 image_ = GetDefaultBootImageLocation(&error_msg);
211
212 if (image_.empty()) {
213 LOG(ERROR) << error_msg;
214 Usage("--image unspecified and ANDROID_ROOT not set or image file does not exist.");
215 }
216 }
217 }
218
219 bool CreateRuntime() {
220 RuntimeOptions options;
221 // The image could be custom, so make sure we explicitly pass it.
222 std::string img = "-Ximage:" + image_;
223 options.push_back(std::make_pair(img.c_str(), nullptr));
224 // The instruction set of the image should match the instruction set we will test.
225 const void* isa_opt = reinterpret_cast<const void*>(GetInstructionSetString(isa_));
226 options.push_back(std::make_pair("imageinstructionset", isa_opt));
227 // Disable libsigchain. We don't don't need it to evaluate DexOptNeeded status.
228 options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
229 // Pretend we are a compiler so that we can re-use the same infrastructure to load a different
230 // ISA image and minimize the amount of things that get started.
231 NoopCompilerCallbacks callbacks;
232 options.push_back(std::make_pair("compilercallbacks", &callbacks));
233 // Make sure we don't attempt to relocate. The tool should only retrieve the DexOptNeeded
234 // status and not attempt to relocate the boot image.
235 options.push_back(std::make_pair("-Xnorelocate", nullptr));
236
237 if (!Runtime::Create(options, false)) {
238 LOG(ERROR) << "Unable to initialize runtime";
239 return false;
240 }
241 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
242 // Runtime::Start. Give it away now.
243 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
244
245 return true;
246 }
247
248 int GetDexOptNeeded() {
249 // If the file does not exist there's nothing to do.
250 // This is a fast path to avoid creating the runtime (b/34385298).
251 if (!OS::FileExists(dex_file_.c_str())) {
252 return kNoDexOptNeeded;
253 }
254 if (!CreateRuntime()) {
255 return kErrorCannotCreateRuntime;
256 }
Andreas Gampe39f44b72017-04-26 22:00:04 -0700257 std::unique_ptr<Runtime> runtime(Runtime::Current());
258
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700259 std::unique_ptr<OatFileAssistant> oat_file_assistant;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700260 oat_file_assistant = std::make_unique<OatFileAssistant>(dex_file_.c_str(),
261 isa_,
262 false /*load_executable*/,
263 vdex_fd_,
264 oat_fd_,
265 zip_fd_);
Calin Juravle36eb3132017-01-13 16:32:38 -0800266 // Always treat elements of the bootclasspath as up-to-date.
267 // TODO(calin): this check should be in OatFileAssistant.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700268 if (oat_file_assistant->IsInBootClassPath()) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800269 return kNoDexOptNeeded;
270 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700271
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700272 int dexoptNeeded = oat_file_assistant->GetDexOptNeeded(
Calin Juravle20c46442017-09-12 00:54:26 -0700273 compiler_filter_, assume_profile_changed_, downgrade_, class_loader_context_.get());
Calin Juravle36eb3132017-01-13 16:32:38 -0800274
275 // Convert OatFileAssitant codes to dexoptanalyzer codes.
276 switch (dexoptNeeded) {
277 case OatFileAssistant::kNoDexOptNeeded: return kNoDexOptNeeded;
278 case OatFileAssistant::kDex2OatFromScratch: return kDex2OatFromScratch;
279 case OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOat;
280 case OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOat;
281 case OatFileAssistant::kDex2OatForRelocation: return kDex2OatForRelocationOat;
282
283 case -OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOdex;
284 case -OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOdex;
285 case -OatFileAssistant::kDex2OatForRelocation: return kDex2OatForRelocationOdex;
286 default:
287 LOG(ERROR) << "Unknown dexoptNeeded " << dexoptNeeded;
288 return kErrorUnknownDexOptNeeded;
289 }
290 }
291
292 private:
293 std::string dex_file_;
294 InstructionSet isa_;
295 CompilerFilter::Filter compiler_filter_;
Calin Juravle20c46442017-09-12 00:54:26 -0700296 std::unique_ptr<ClassLoaderContext> class_loader_context_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800297 bool assume_profile_changed_;
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700298 bool downgrade_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800299 std::string image_;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700300 int oat_fd_ = -1;
301 int vdex_fd_ = -1;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700302 // File descriptor corresponding to apk, dex_file, or zip.
303 int zip_fd_ = -1;
Calin Juravle36eb3132017-01-13 16:32:38 -0800304};
305
306static int dexoptAnalyze(int argc, char** argv) {
307 DexoptAnalyzer analyzer;
308
309 // Parse arguments. Argument mistakes will lead to exit(kErrorInvalidArguments) in UsageError.
310 analyzer.ParseArgs(argc, argv);
311 return analyzer.GetDexOptNeeded();
312}
313
314} // namespace art
315
316int main(int argc, char **argv) {
317 return art::dexoptAnalyze(argc, argv);
318}