blob: 92850f759073b0e5c41ab3812806785ab3d87cee [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.
David Sehrc431b9d2018-03-02 12:01:51 -080020#include "base/mutex.h"
21#include "base/os.h"
22#include "base/utils.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080023#include "android-base/stringprintf.h"
24#include "android-base/strings.h"
David Sehr891a50e2017-10-27 17:01:07 -070025#include "base/file_utils.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080026#include "compiler_filter.h"
Calin Juravle20c46442017-09-12 00:54:26 -070027#include "class_loader_context.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080029#include "noop_compiler_callbacks.h"
30#include "oat_file_assistant.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080031#include "runtime.h"
32#include "thread-inl.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080033
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,
Vladimir Markoe0669322018-09-03 15:44:54 +010042 kDex2OatForBootImageOdex = 4,
43 kDex2OatForFilterOdex = 5,
Calin Juravle36eb3132017-01-13 16:32:38 -080044
45 kErrorInvalidArguments = 101,
46 kErrorCannotCreateRuntime = 102,
47 kErrorUnknownDexOptNeeded = 103
48};
49
50static int original_argc;
51static char** original_argv;
52
53static std::string CommandLine() {
54 std::vector<std::string> command;
Andreas Gampe2a487eb2018-11-19 11:41:22 -080055 command.reserve(original_argc);
Calin Juravle36eb3132017-01-13 16:32:38 -080056 for (int i = 0; i < original_argc; ++i) {
57 command.push_back(original_argv[i]);
58 }
59 return android::base::Join(command, ' ');
60}
61
62static void UsageErrorV(const char* fmt, va_list ap) {
63 std::string error;
64 android::base::StringAppendV(&error, fmt, ap);
65 LOG(ERROR) << error;
66}
67
68static void UsageError(const char* fmt, ...) {
69 va_list ap;
70 va_start(ap, fmt);
71 UsageErrorV(fmt, ap);
72 va_end(ap);
73}
74
75NO_RETURN static void Usage(const char *fmt, ...) {
76 va_list ap;
77 va_start(ap, fmt);
78 UsageErrorV(fmt, ap);
79 va_end(ap);
80
81 UsageError("Command: %s", CommandLine().c_str());
82 UsageError(" Performs a dexopt analysis on the given dex file and returns whether or not");
83 UsageError(" the dex file needs to be dexopted.");
84 UsageError("Usage: dexoptanalyzer [options]...");
85 UsageError("");
86 UsageError(" --dex-file=<filename>: the dex file which should be analyzed.");
87 UsageError("");
88 UsageError(" --isa=<string>: the instruction set for which the analysis should be performed.");
89 UsageError("");
90 UsageError(" --compiler-filter=<string>: the target compiler filter to be used as reference");
91 UsageError(" when deciding if the dex file needs to be optimized.");
92 UsageError("");
93 UsageError(" --assume-profile-changed: assumes the profile information has changed");
94 UsageError(" when deciding if the dex file needs to be optimized.");
95 UsageError("");
96 UsageError(" --image=<filename>: optional, the image to be used to decide if the associated");
97 UsageError(" oat file is up to date. Defaults to $ANDROID_ROOT/framework/boot.art.");
98 UsageError(" Example: --image=/system/framework/boot.art");
99 UsageError("");
Vladimir Marko813b9142018-11-29 11:20:07 +0000100 UsageError(" --runtime-arg <argument>: used to specify various arguments for the runtime,");
101 UsageError(" such as initial heap size, maximum heap size, and verbose output.");
102 UsageError(" Use a separate --runtime-arg switch for each argument.");
103 UsageError(" Example: --runtime-arg -Xms256m");
104 UsageError("");
Calin Juravle36eb3132017-01-13 16:32:38 -0800105 UsageError(" --android-data=<directory>: optional, the directory which should be used as");
106 UsageError(" android-data. By default ANDROID_DATA env variable is used.");
107 UsageError("");
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700108 UsageError(" --oat-fd=number: file descriptor of the oat file which should be analyzed");
109 UsageError("");
110 UsageError(" --vdex-fd=number: file descriptor of the vdex file corresponding to the oat file");
111 UsageError("");
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700112 UsageError(" --zip-fd=number: specifies a file descriptor corresponding to the dex file.");
113 UsageError("");
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700114 UsageError(" --downgrade: optional, if the purpose of dexopt is to downgrade the dex file");
115 UsageError(" By default, dexopt considers upgrade case.");
116 UsageError("");
Calin Juravle36eb3132017-01-13 16:32:38 -0800117 UsageError("Return code:");
118 UsageError(" To make it easier to integrate with the internal tools this command will make");
119 UsageError(" available its result (dexoptNeeded) as the exit/return code. i.e. it will not");
120 UsageError(" return 0 for success and a non zero values for errors as the conventional");
121 UsageError(" commands. The following return codes are possible:");
122 UsageError(" kNoDexOptNeeded = 0");
123 UsageError(" kDex2OatFromScratch = 1");
124 UsageError(" kDex2OatForBootImageOat = 2");
125 UsageError(" kDex2OatForFilterOat = 3");
Vladimir Markoe0669322018-09-03 15:44:54 +0100126 UsageError(" kDex2OatForBootImageOdex = 4");
127 UsageError(" kDex2OatForFilterOdex = 5");
Calin Juravle36eb3132017-01-13 16:32:38 -0800128
129 UsageError(" kErrorInvalidArguments = 101");
130 UsageError(" kErrorCannotCreateRuntime = 102");
131 UsageError(" kErrorUnknownDexOptNeeded = 103");
132 UsageError("");
133
134 exit(kErrorInvalidArguments);
135}
136
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100137class DexoptAnalyzer final {
Calin Juravle36eb3132017-01-13 16:32:38 -0800138 public:
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700139 DexoptAnalyzer() :
140 assume_profile_changed_(false),
141 downgrade_(false) {}
Calin Juravle36eb3132017-01-13 16:32:38 -0800142
143 void ParseArgs(int argc, char **argv) {
144 original_argc = argc;
145 original_argv = argv;
146
David Sehrc431b9d2018-03-02 12:01:51 -0800147 Locks::Init();
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700148 InitLogging(argv, Runtime::Abort);
Calin Juravle36eb3132017-01-13 16:32:38 -0800149 // Skip over the command name.
150 argv++;
151 argc--;
152
153 if (argc == 0) {
154 Usage("No arguments specified");
155 }
156
157 for (int i = 0; i < argc; ++i) {
158 const StringPiece option(argv[i]);
159 if (option == "--assume-profile-changed") {
160 assume_profile_changed_ = true;
161 } else if (option.starts_with("--dex-file=")) {
162 dex_file_ = option.substr(strlen("--dex-file=")).ToString();
163 } else if (option.starts_with("--compiler-filter=")) {
164 std::string filter_str = option.substr(strlen("--compiler-filter=")).ToString();
165 if (!CompilerFilter::ParseCompilerFilter(filter_str.c_str(), &compiler_filter_)) {
166 Usage("Invalid compiler filter '%s'", option.data());
167 }
168 } else if (option.starts_with("--isa=")) {
169 std::string isa_str = option.substr(strlen("--isa=")).ToString();
170 isa_ = GetInstructionSetFromString(isa_str.c_str());
Vladimir Marko33bff252017-11-01 14:35:42 +0000171 if (isa_ == InstructionSet::kNone) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800172 Usage("Invalid isa '%s'", option.data());
173 }
174 } else if (option.starts_with("--image=")) {
175 image_ = option.substr(strlen("--image=")).ToString();
Vladimir Marko813b9142018-11-29 11:20:07 +0000176 } else if (option == "--runtime-arg") {
177 if (i + 1 == argc) {
178 Usage("Missing argument for --runtime-arg\n");
179 }
180 ++i;
181 runtime_args_.push_back(argv[i]);
Calin Juravle36eb3132017-01-13 16:32:38 -0800182 } else if (option.starts_with("--android-data=")) {
183 // Overwrite android-data if needed (oat file assistant relies on a valid directory to
184 // compute dalvik-cache folder). This is mostly used in tests.
185 std::string new_android_data = option.substr(strlen("--android-data=")).ToString();
186 setenv("ANDROID_DATA", new_android_data.c_str(), 1);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700187 } else if (option.starts_with("--downgrade")) {
188 downgrade_ = true;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700189 } else if (option.starts_with("--oat-fd")) {
190 oat_fd_ = std::stoi(option.substr(strlen("--oat-fd=")).ToString(), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700191 if (oat_fd_ < 0) {
192 Usage("Invalid --oat-fd %d", oat_fd_);
193 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700194 } else if (option.starts_with("--vdex-fd")) {
195 vdex_fd_ = std::stoi(option.substr(strlen("--vdex-fd=")).ToString(), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700196 if (vdex_fd_ < 0) {
197 Usage("Invalid --vdex-fd %d", vdex_fd_);
198 }
199 } else if (option.starts_with("--zip-fd")) {
200 zip_fd_ = std::stoi(option.substr(strlen("--zip-fd=")).ToString(), nullptr, 0);
201 if (zip_fd_ < 0) {
202 Usage("Invalid --zip-fd %d", zip_fd_);
203 }
Calin Juravle20c46442017-09-12 00:54:26 -0700204 } else if (option.starts_with("--class-loader-context=")) {
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000205 context_str_ = option.substr(strlen("--class-loader-context=")).ToString();
Calin Juravle20c46442017-09-12 00:54:26 -0700206 } else {
207 Usage("Unknown argument '%s'", option.data());
208 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800209 }
210
211 if (image_.empty()) {
212 // If we don't receive the image, try to use the default one.
213 // Tests may specify a different image (e.g. core image).
214 std::string error_msg;
215 image_ = GetDefaultBootImageLocation(&error_msg);
216
217 if (image_.empty()) {
218 LOG(ERROR) << error_msg;
219 Usage("--image unspecified and ANDROID_ROOT not set or image file does not exist.");
220 }
221 }
222 }
223
224 bool CreateRuntime() {
225 RuntimeOptions options;
226 // The image could be custom, so make sure we explicitly pass it.
227 std::string img = "-Ximage:" + image_;
Vladimir Marko813b9142018-11-29 11:20:07 +0000228 options.push_back(std::make_pair(img, nullptr));
Calin Juravle36eb3132017-01-13 16:32:38 -0800229 // The instruction set of the image should match the instruction set we will test.
230 const void* isa_opt = reinterpret_cast<const void*>(GetInstructionSetString(isa_));
231 options.push_back(std::make_pair("imageinstructionset", isa_opt));
Vladimir Marko813b9142018-11-29 11:20:07 +0000232 // Explicit runtime args.
233 for (const char* runtime_arg : runtime_args_) {
234 options.push_back(std::make_pair(runtime_arg, nullptr));
235 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800236 // Disable libsigchain. We don't don't need it to evaluate DexOptNeeded status.
237 options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
238 // Pretend we are a compiler so that we can re-use the same infrastructure to load a different
239 // ISA image and minimize the amount of things that get started.
240 NoopCompilerCallbacks callbacks;
241 options.push_back(std::make_pair("compilercallbacks", &callbacks));
242 // Make sure we don't attempt to relocate. The tool should only retrieve the DexOptNeeded
243 // status and not attempt to relocate the boot image.
244 options.push_back(std::make_pair("-Xnorelocate", nullptr));
245
246 if (!Runtime::Create(options, false)) {
247 LOG(ERROR) << "Unable to initialize runtime";
248 return false;
249 }
250 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
251 // Runtime::Start. Give it away now.
252 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
253
254 return true;
255 }
256
257 int GetDexOptNeeded() {
Calin Juravle36eb3132017-01-13 16:32:38 -0800258 if (!CreateRuntime()) {
259 return kErrorCannotCreateRuntime;
260 }
Andreas Gampe39f44b72017-04-26 22:00:04 -0700261 std::unique_ptr<Runtime> runtime(Runtime::Current());
262
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000263 // Only when the runtime is created can we create the class loader context: the
264 // class loader context will open dex file and use the MemMap global lock that the
265 // runtime owns.
266 std::unique_ptr<ClassLoaderContext> class_loader_context;
267 if (!context_str_.empty()) {
268 class_loader_context = ClassLoaderContext::Create(context_str_);
269 if (class_loader_context == nullptr) {
270 Usage("Invalid --class-loader-context '%s'", context_str_.c_str());
271 }
272 }
273
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700274 std::unique_ptr<OatFileAssistant> oat_file_assistant;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700275 oat_file_assistant = std::make_unique<OatFileAssistant>(dex_file_.c_str(),
276 isa_,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700277 /*load_executable=*/ false,
278 /*only_load_system_executable=*/ false,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700279 vdex_fd_,
280 oat_fd_,
281 zip_fd_);
Calin Juravle36eb3132017-01-13 16:32:38 -0800282 // Always treat elements of the bootclasspath as up-to-date.
283 // TODO(calin): this check should be in OatFileAssistant.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700284 if (oat_file_assistant->IsInBootClassPath()) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800285 return kNoDexOptNeeded;
286 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700287
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700288 int dexoptNeeded = oat_file_assistant->GetDexOptNeeded(
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000289 compiler_filter_, assume_profile_changed_, downgrade_, class_loader_context.get());
Calin Juravle36eb3132017-01-13 16:32:38 -0800290
291 // Convert OatFileAssitant codes to dexoptanalyzer codes.
292 switch (dexoptNeeded) {
293 case OatFileAssistant::kNoDexOptNeeded: return kNoDexOptNeeded;
294 case OatFileAssistant::kDex2OatFromScratch: return kDex2OatFromScratch;
295 case OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOat;
296 case OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOat;
Calin Juravle36eb3132017-01-13 16:32:38 -0800297
298 case -OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOdex;
299 case -OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOdex;
Calin Juravle36eb3132017-01-13 16:32:38 -0800300 default:
301 LOG(ERROR) << "Unknown dexoptNeeded " << dexoptNeeded;
302 return kErrorUnknownDexOptNeeded;
303 }
304 }
305
306 private:
307 std::string dex_file_;
308 InstructionSet isa_;
309 CompilerFilter::Filter compiler_filter_;
Nicolas Geoffray35de14b2019-01-10 13:10:36 +0000310 std::string context_str_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800311 bool assume_profile_changed_;
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700312 bool downgrade_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800313 std::string image_;
Vladimir Marko813b9142018-11-29 11:20:07 +0000314 std::vector<const char*> runtime_args_;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700315 int oat_fd_ = -1;
316 int vdex_fd_ = -1;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700317 // File descriptor corresponding to apk, dex_file, or zip.
318 int zip_fd_ = -1;
Calin Juravle36eb3132017-01-13 16:32:38 -0800319};
320
321static int dexoptAnalyze(int argc, char** argv) {
322 DexoptAnalyzer analyzer;
323
324 // Parse arguments. Argument mistakes will lead to exit(kErrorInvalidArguments) in UsageError.
325 analyzer.ParseArgs(argc, argv);
326 return analyzer.GetDexOptNeeded();
327}
328
329} // namespace art
330
331int main(int argc, char **argv) {
332 return art::dexoptAnalyze(argc, argv);
333}