blob: 10bb673d1483db0ca9b728162c5d5be7f08abf0f [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;
55 for (int i = 0; i < original_argc; ++i) {
56 command.push_back(original_argv[i]);
57 }
58 return android::base::Join(command, ' ');
59}
60
61static void UsageErrorV(const char* fmt, va_list ap) {
62 std::string error;
63 android::base::StringAppendV(&error, fmt, ap);
64 LOG(ERROR) << error;
65}
66
67static void UsageError(const char* fmt, ...) {
68 va_list ap;
69 va_start(ap, fmt);
70 UsageErrorV(fmt, ap);
71 va_end(ap);
72}
73
74NO_RETURN static void Usage(const char *fmt, ...) {
75 va_list ap;
76 va_start(ap, fmt);
77 UsageErrorV(fmt, ap);
78 va_end(ap);
79
80 UsageError("Command: %s", CommandLine().c_str());
81 UsageError(" Performs a dexopt analysis on the given dex file and returns whether or not");
82 UsageError(" the dex file needs to be dexopted.");
83 UsageError("Usage: dexoptanalyzer [options]...");
84 UsageError("");
85 UsageError(" --dex-file=<filename>: the dex file which should be analyzed.");
86 UsageError("");
87 UsageError(" --isa=<string>: the instruction set for which the analysis should be performed.");
88 UsageError("");
89 UsageError(" --compiler-filter=<string>: the target compiler filter to be used as reference");
90 UsageError(" when deciding if the dex file needs to be optimized.");
91 UsageError("");
92 UsageError(" --assume-profile-changed: assumes the profile information has changed");
93 UsageError(" when deciding if the dex file needs to be optimized.");
94 UsageError("");
95 UsageError(" --image=<filename>: optional, the image to be used to decide if the associated");
96 UsageError(" oat file is up to date. Defaults to $ANDROID_ROOT/framework/boot.art.");
97 UsageError(" Example: --image=/system/framework/boot.art");
98 UsageError("");
99 UsageError(" --android-data=<directory>: optional, the directory which should be used as");
100 UsageError(" android-data. By default ANDROID_DATA env variable is used.");
101 UsageError("");
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700102 UsageError(" --oat-fd=number: file descriptor of the oat file which should be analyzed");
103 UsageError("");
104 UsageError(" --vdex-fd=number: file descriptor of the vdex file corresponding to the oat file");
105 UsageError("");
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700106 UsageError(" --zip-fd=number: specifies a file descriptor corresponding to the dex file.");
107 UsageError("");
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700108 UsageError(" --downgrade: optional, if the purpose of dexopt is to downgrade the dex file");
109 UsageError(" By default, dexopt considers upgrade case.");
110 UsageError("");
Calin Juravle36eb3132017-01-13 16:32:38 -0800111 UsageError("Return code:");
112 UsageError(" To make it easier to integrate with the internal tools this command will make");
113 UsageError(" available its result (dexoptNeeded) as the exit/return code. i.e. it will not");
114 UsageError(" return 0 for success and a non zero values for errors as the conventional");
115 UsageError(" commands. The following return codes are possible:");
116 UsageError(" kNoDexOptNeeded = 0");
117 UsageError(" kDex2OatFromScratch = 1");
118 UsageError(" kDex2OatForBootImageOat = 2");
119 UsageError(" kDex2OatForFilterOat = 3");
Vladimir Markoe0669322018-09-03 15:44:54 +0100120 UsageError(" kDex2OatForBootImageOdex = 4");
121 UsageError(" kDex2OatForFilterOdex = 5");
Calin Juravle36eb3132017-01-13 16:32:38 -0800122
123 UsageError(" kErrorInvalidArguments = 101");
124 UsageError(" kErrorCannotCreateRuntime = 102");
125 UsageError(" kErrorUnknownDexOptNeeded = 103");
126 UsageError("");
127
128 exit(kErrorInvalidArguments);
129}
130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100131class DexoptAnalyzer final {
Calin Juravle36eb3132017-01-13 16:32:38 -0800132 public:
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700133 DexoptAnalyzer() :
134 assume_profile_changed_(false),
135 downgrade_(false) {}
Calin Juravle36eb3132017-01-13 16:32:38 -0800136
137 void ParseArgs(int argc, char **argv) {
138 original_argc = argc;
139 original_argv = argv;
140
David Sehrc431b9d2018-03-02 12:01:51 -0800141 Locks::Init();
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700142 InitLogging(argv, Runtime::Abort);
Calin Juravle36eb3132017-01-13 16:32:38 -0800143 // Skip over the command name.
144 argv++;
145 argc--;
146
147 if (argc == 0) {
148 Usage("No arguments specified");
149 }
150
151 for (int i = 0; i < argc; ++i) {
152 const StringPiece option(argv[i]);
153 if (option == "--assume-profile-changed") {
154 assume_profile_changed_ = true;
155 } else if (option.starts_with("--dex-file=")) {
156 dex_file_ = option.substr(strlen("--dex-file=")).ToString();
157 } else if (option.starts_with("--compiler-filter=")) {
158 std::string filter_str = option.substr(strlen("--compiler-filter=")).ToString();
159 if (!CompilerFilter::ParseCompilerFilter(filter_str.c_str(), &compiler_filter_)) {
160 Usage("Invalid compiler filter '%s'", option.data());
161 }
162 } else if (option.starts_with("--isa=")) {
163 std::string isa_str = option.substr(strlen("--isa=")).ToString();
164 isa_ = GetInstructionSetFromString(isa_str.c_str());
Vladimir Marko33bff252017-11-01 14:35:42 +0000165 if (isa_ == InstructionSet::kNone) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800166 Usage("Invalid isa '%s'", option.data());
167 }
168 } else if (option.starts_with("--image=")) {
169 image_ = option.substr(strlen("--image=")).ToString();
170 } else if (option.starts_with("--android-data=")) {
171 // Overwrite android-data if needed (oat file assistant relies on a valid directory to
172 // compute dalvik-cache folder). This is mostly used in tests.
173 std::string new_android_data = option.substr(strlen("--android-data=")).ToString();
174 setenv("ANDROID_DATA", new_android_data.c_str(), 1);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700175 } else if (option.starts_with("--downgrade")) {
176 downgrade_ = true;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700177 } else if (option.starts_with("--oat-fd")) {
178 oat_fd_ = std::stoi(option.substr(strlen("--oat-fd=")).ToString(), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700179 if (oat_fd_ < 0) {
180 Usage("Invalid --oat-fd %d", oat_fd_);
181 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700182 } else if (option.starts_with("--vdex-fd")) {
183 vdex_fd_ = std::stoi(option.substr(strlen("--vdex-fd=")).ToString(), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700184 if (vdex_fd_ < 0) {
185 Usage("Invalid --vdex-fd %d", vdex_fd_);
186 }
187 } else if (option.starts_with("--zip-fd")) {
188 zip_fd_ = std::stoi(option.substr(strlen("--zip-fd=")).ToString(), nullptr, 0);
189 if (zip_fd_ < 0) {
190 Usage("Invalid --zip-fd %d", zip_fd_);
191 }
Calin Juravle20c46442017-09-12 00:54:26 -0700192 } else if (option.starts_with("--class-loader-context=")) {
193 std::string context_str = option.substr(strlen("--class-loader-context=")).ToString();
194 class_loader_context_ = ClassLoaderContext::Create(context_str);
195 if (class_loader_context_ == nullptr) {
196 Usage("Invalid --class-loader-context '%s'", context_str.c_str());
197 }
198 } else {
199 Usage("Unknown argument '%s'", option.data());
200 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800201 }
202
203 if (image_.empty()) {
204 // If we don't receive the image, try to use the default one.
205 // Tests may specify a different image (e.g. core image).
206 std::string error_msg;
207 image_ = GetDefaultBootImageLocation(&error_msg);
208
209 if (image_.empty()) {
210 LOG(ERROR) << error_msg;
211 Usage("--image unspecified and ANDROID_ROOT not set or image file does not exist.");
212 }
213 }
214 }
215
216 bool CreateRuntime() {
217 RuntimeOptions options;
218 // The image could be custom, so make sure we explicitly pass it.
219 std::string img = "-Ximage:" + image_;
220 options.push_back(std::make_pair(img.c_str(), nullptr));
221 // The instruction set of the image should match the instruction set we will test.
222 const void* isa_opt = reinterpret_cast<const void*>(GetInstructionSetString(isa_));
223 options.push_back(std::make_pair("imageinstructionset", isa_opt));
224 // Disable libsigchain. We don't don't need it to evaluate DexOptNeeded status.
225 options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
226 // Pretend we are a compiler so that we can re-use the same infrastructure to load a different
227 // ISA image and minimize the amount of things that get started.
228 NoopCompilerCallbacks callbacks;
229 options.push_back(std::make_pair("compilercallbacks", &callbacks));
230 // Make sure we don't attempt to relocate. The tool should only retrieve the DexOptNeeded
231 // status and not attempt to relocate the boot image.
232 options.push_back(std::make_pair("-Xnorelocate", nullptr));
233
234 if (!Runtime::Create(options, false)) {
235 LOG(ERROR) << "Unable to initialize runtime";
236 return false;
237 }
238 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
239 // Runtime::Start. Give it away now.
240 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
241
242 return true;
243 }
244
245 int GetDexOptNeeded() {
Calin Juravle36eb3132017-01-13 16:32:38 -0800246 if (!CreateRuntime()) {
247 return kErrorCannotCreateRuntime;
248 }
Andreas Gampe39f44b72017-04-26 22:00:04 -0700249 std::unique_ptr<Runtime> runtime(Runtime::Current());
250
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700251 std::unique_ptr<OatFileAssistant> oat_file_assistant;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700252 oat_file_assistant = std::make_unique<OatFileAssistant>(dex_file_.c_str(),
253 isa_,
254 false /*load_executable*/,
Nicolas Geoffray29742602017-12-14 10:09:03 +0000255 false /*only_load_system_executable*/,
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700256 vdex_fd_,
257 oat_fd_,
258 zip_fd_);
Calin Juravle36eb3132017-01-13 16:32:38 -0800259 // Always treat elements of the bootclasspath as up-to-date.
260 // TODO(calin): this check should be in OatFileAssistant.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700261 if (oat_file_assistant->IsInBootClassPath()) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800262 return kNoDexOptNeeded;
263 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700264
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700265 int dexoptNeeded = oat_file_assistant->GetDexOptNeeded(
Calin Juravle20c46442017-09-12 00:54:26 -0700266 compiler_filter_, assume_profile_changed_, downgrade_, class_loader_context_.get());
Calin Juravle36eb3132017-01-13 16:32:38 -0800267
268 // Convert OatFileAssitant codes to dexoptanalyzer codes.
269 switch (dexoptNeeded) {
270 case OatFileAssistant::kNoDexOptNeeded: return kNoDexOptNeeded;
271 case OatFileAssistant::kDex2OatFromScratch: return kDex2OatFromScratch;
272 case OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOat;
273 case OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOat;
Calin Juravle36eb3132017-01-13 16:32:38 -0800274
275 case -OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOdex;
276 case -OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOdex;
Calin Juravle36eb3132017-01-13 16:32:38 -0800277 default:
278 LOG(ERROR) << "Unknown dexoptNeeded " << dexoptNeeded;
279 return kErrorUnknownDexOptNeeded;
280 }
281 }
282
283 private:
284 std::string dex_file_;
285 InstructionSet isa_;
286 CompilerFilter::Filter compiler_filter_;
Calin Juravle20c46442017-09-12 00:54:26 -0700287 std::unique_ptr<ClassLoaderContext> class_loader_context_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800288 bool assume_profile_changed_;
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700289 bool downgrade_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800290 std::string image_;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700291 int oat_fd_ = -1;
292 int vdex_fd_ = -1;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700293 // File descriptor corresponding to apk, dex_file, or zip.
294 int zip_fd_ = -1;
Calin Juravle36eb3132017-01-13 16:32:38 -0800295};
296
297static int dexoptAnalyze(int argc, char** argv) {
298 DexoptAnalyzer analyzer;
299
300 // Parse arguments. Argument mistakes will lead to exit(kErrorInvalidArguments) in UsageError.
301 analyzer.ParseArgs(argc, argv);
302 return analyzer.GetDexOptNeeded();
303}
304
305} // namespace art
306
307int main(int argc, char **argv) {
308 return art::dexoptAnalyze(argc, argv);
309}