blob: 39c9b9993b263f9c0ede94fa709b2f3e3461a9d1 [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
19#include "android-base/stringprintf.h"
20#include "android-base/strings.h"
David Sehr891a50e2017-10-27 17:01:07 -070021#include "base/file_utils.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080022#include "compiler_filter.h"
Calin Juravle20c46442017-09-12 00:54:26 -070023#include "class_loader_context.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080024#include "dex_file.h"
25#include "noop_compiler_callbacks.h"
26#include "oat_file_assistant.h"
27#include "os.h"
28#include "runtime.h"
29#include "thread-inl.h"
30#include "utils.h"
31
32namespace art {
33
34// See OatFileAssistant docs for the meaning of the valid return codes.
35enum ReturnCodes {
36 kNoDexOptNeeded = 0,
37 kDex2OatFromScratch = 1,
38 kDex2OatForBootImageOat = 2,
39 kDex2OatForFilterOat = 3,
40 kDex2OatForRelocationOat = 4,
41 kDex2OatForBootImageOdex = 5,
42 kDex2OatForFilterOdex = 6,
43 kDex2OatForRelocationOdex = 7,
44
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");
120 UsageError(" kDex2OatForRelocationOat = 4");
121 UsageError(" kDex2OatForBootImageOdex = 5");
122 UsageError(" kDex2OatForFilterOdex = 6");
123 UsageError(" kDex2OatForRelocationOdex = 7");
124
125 UsageError(" kErrorInvalidArguments = 101");
126 UsageError(" kErrorCannotCreateRuntime = 102");
127 UsageError(" kErrorUnknownDexOptNeeded = 103");
128 UsageError("");
129
130 exit(kErrorInvalidArguments);
131}
132
133class DexoptAnalyzer FINAL {
134 public:
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700135 DexoptAnalyzer() :
136 assume_profile_changed_(false),
137 downgrade_(false) {}
Calin Juravle36eb3132017-01-13 16:32:38 -0800138
139 void ParseArgs(int argc, char **argv) {
140 original_argc = argc;
141 original_argv = argv;
142
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700143 InitLogging(argv, Runtime::Abort);
Calin Juravle36eb3132017-01-13 16:32:38 -0800144 // Skip over the command name.
145 argv++;
146 argc--;
147
148 if (argc == 0) {
149 Usage("No arguments specified");
150 }
151
152 for (int i = 0; i < argc; ++i) {
153 const StringPiece option(argv[i]);
154 if (option == "--assume-profile-changed") {
155 assume_profile_changed_ = true;
156 } else if (option.starts_with("--dex-file=")) {
157 dex_file_ = option.substr(strlen("--dex-file=")).ToString();
158 } else if (option.starts_with("--compiler-filter=")) {
159 std::string filter_str = option.substr(strlen("--compiler-filter=")).ToString();
160 if (!CompilerFilter::ParseCompilerFilter(filter_str.c_str(), &compiler_filter_)) {
161 Usage("Invalid compiler filter '%s'", option.data());
162 }
163 } else if (option.starts_with("--isa=")) {
164 std::string isa_str = option.substr(strlen("--isa=")).ToString();
165 isa_ = GetInstructionSetFromString(isa_str.c_str());
Vladimir Marko33bff252017-11-01 14:35:42 +0000166 if (isa_ == InstructionSet::kNone) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800167 Usage("Invalid isa '%s'", option.data());
168 }
169 } else if (option.starts_with("--image=")) {
170 image_ = option.substr(strlen("--image=")).ToString();
171 } else if (option.starts_with("--android-data=")) {
172 // Overwrite android-data if needed (oat file assistant relies on a valid directory to
173 // compute dalvik-cache folder). This is mostly used in tests.
174 std::string new_android_data = option.substr(strlen("--android-data=")).ToString();
175 setenv("ANDROID_DATA", new_android_data.c_str(), 1);
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700176 } else if (option.starts_with("--downgrade")) {
177 downgrade_ = true;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700178 } else if (option.starts_with("--oat-fd")) {
179 oat_fd_ = std::stoi(option.substr(strlen("--oat-fd=")).ToString(), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700180 if (oat_fd_ < 0) {
181 Usage("Invalid --oat-fd %d", oat_fd_);
182 }
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700183 } else if (option.starts_with("--vdex-fd")) {
184 vdex_fd_ = std::stoi(option.substr(strlen("--vdex-fd=")).ToString(), nullptr, 0);
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700185 if (vdex_fd_ < 0) {
186 Usage("Invalid --vdex-fd %d", vdex_fd_);
187 }
188 } else if (option.starts_with("--zip-fd")) {
189 zip_fd_ = std::stoi(option.substr(strlen("--zip-fd=")).ToString(), nullptr, 0);
190 if (zip_fd_ < 0) {
191 Usage("Invalid --zip-fd %d", zip_fd_);
192 }
Calin Juravle20c46442017-09-12 00:54:26 -0700193 } else if (option.starts_with("--class-loader-context=")) {
194 std::string context_str = option.substr(strlen("--class-loader-context=")).ToString();
195 class_loader_context_ = ClassLoaderContext::Create(context_str);
196 if (class_loader_context_ == nullptr) {
197 Usage("Invalid --class-loader-context '%s'", context_str.c_str());
198 }
199 } else {
200 Usage("Unknown argument '%s'", option.data());
201 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800202 }
203
204 if (image_.empty()) {
205 // If we don't receive the image, try to use the default one.
206 // Tests may specify a different image (e.g. core image).
207 std::string error_msg;
208 image_ = GetDefaultBootImageLocation(&error_msg);
209
210 if (image_.empty()) {
211 LOG(ERROR) << error_msg;
212 Usage("--image unspecified and ANDROID_ROOT not set or image file does not exist.");
213 }
214 }
215 }
216
217 bool CreateRuntime() {
218 RuntimeOptions options;
219 // The image could be custom, so make sure we explicitly pass it.
220 std::string img = "-Ximage:" + image_;
221 options.push_back(std::make_pair(img.c_str(), nullptr));
222 // The instruction set of the image should match the instruction set we will test.
223 const void* isa_opt = reinterpret_cast<const void*>(GetInstructionSetString(isa_));
224 options.push_back(std::make_pair("imageinstructionset", isa_opt));
225 // Disable libsigchain. We don't don't need it to evaluate DexOptNeeded status.
226 options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
227 // Pretend we are a compiler so that we can re-use the same infrastructure to load a different
228 // ISA image and minimize the amount of things that get started.
229 NoopCompilerCallbacks callbacks;
230 options.push_back(std::make_pair("compilercallbacks", &callbacks));
231 // Make sure we don't attempt to relocate. The tool should only retrieve the DexOptNeeded
232 // status and not attempt to relocate the boot image.
233 options.push_back(std::make_pair("-Xnorelocate", nullptr));
234
235 if (!Runtime::Create(options, false)) {
236 LOG(ERROR) << "Unable to initialize runtime";
237 return false;
238 }
239 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
240 // Runtime::Start. Give it away now.
241 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
242
243 return true;
244 }
245
246 int GetDexOptNeeded() {
247 // If the file does not exist there's nothing to do.
248 // This is a fast path to avoid creating the runtime (b/34385298).
249 if (!OS::FileExists(dex_file_.c_str())) {
250 return kNoDexOptNeeded;
251 }
252 if (!CreateRuntime()) {
253 return kErrorCannotCreateRuntime;
254 }
Andreas Gampe39f44b72017-04-26 22:00:04 -0700255 std::unique_ptr<Runtime> runtime(Runtime::Current());
256
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700257 std::unique_ptr<OatFileAssistant> oat_file_assistant;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700258 oat_file_assistant = std::make_unique<OatFileAssistant>(dex_file_.c_str(),
259 isa_,
260 false /*load_executable*/,
261 vdex_fd_,
262 oat_fd_,
263 zip_fd_);
Calin Juravle36eb3132017-01-13 16:32:38 -0800264 // Always treat elements of the bootclasspath as up-to-date.
265 // TODO(calin): this check should be in OatFileAssistant.
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700266 if (oat_file_assistant->IsInBootClassPath()) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800267 return kNoDexOptNeeded;
268 }
Calin Juravle44e5efa2017-09-12 00:54:26 -0700269
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700270 int dexoptNeeded = oat_file_assistant->GetDexOptNeeded(
Calin Juravle20c46442017-09-12 00:54:26 -0700271 compiler_filter_, assume_profile_changed_, downgrade_, class_loader_context_.get());
Calin Juravle36eb3132017-01-13 16:32:38 -0800272
273 // Convert OatFileAssitant codes to dexoptanalyzer codes.
274 switch (dexoptNeeded) {
275 case OatFileAssistant::kNoDexOptNeeded: return kNoDexOptNeeded;
276 case OatFileAssistant::kDex2OatFromScratch: return kDex2OatFromScratch;
277 case OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOat;
278 case OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOat;
279 case OatFileAssistant::kDex2OatForRelocation: return kDex2OatForRelocationOat;
280
281 case -OatFileAssistant::kDex2OatForBootImage: return kDex2OatForBootImageOdex;
282 case -OatFileAssistant::kDex2OatForFilter: return kDex2OatForFilterOdex;
283 case -OatFileAssistant::kDex2OatForRelocation: return kDex2OatForRelocationOdex;
284 default:
285 LOG(ERROR) << "Unknown dexoptNeeded " << dexoptNeeded;
286 return kErrorUnknownDexOptNeeded;
287 }
288 }
289
290 private:
291 std::string dex_file_;
292 InstructionSet isa_;
293 CompilerFilter::Filter compiler_filter_;
Calin Juravle20c46442017-09-12 00:54:26 -0700294 std::unique_ptr<ClassLoaderContext> class_loader_context_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800295 bool assume_profile_changed_;
Shubham Ajmerae4e812a2017-05-25 20:09:58 -0700296 bool downgrade_;
Calin Juravle36eb3132017-01-13 16:32:38 -0800297 std::string image_;
Shubham Ajmerab22dea02017-10-04 18:36:41 -0700298 int oat_fd_ = -1;
299 int vdex_fd_ = -1;
Shubham Ajmerac12bf4c2017-10-24 16:59:42 -0700300 // File descriptor corresponding to apk, dex_file, or zip.
301 int zip_fd_ = -1;
Calin Juravle36eb3132017-01-13 16:32:38 -0800302};
303
304static int dexoptAnalyze(int argc, char** argv) {
305 DexoptAnalyzer analyzer;
306
307 // Parse arguments. Argument mistakes will lead to exit(kErrorInvalidArguments) in UsageError.
308 analyzer.ParseArgs(argc, argv);
309 return analyzer.GetDexOptNeeded();
310}
311
312} // namespace art
313
314int main(int argc, char **argv) {
315 return art::dexoptAnalyze(argc, argv);
316}