blob: 2929f36d2e50a36e43199c5bde0cf0a6d8b768af [file] [log] [blame]
Igor Murashkin37743352014-11-13 14:38:00 -08001/*
2 * Copyright (C) 2014 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#ifndef ART_CMDLINE_CMDLINE_H_
18#define ART_CMDLINE_CMDLINE_H_
19
20#include <stdio.h>
21#include <stdlib.h>
22
23#include <fstream>
24#include <iostream>
25#include <string>
26
Andreas Gampe46ee31b2016-12-14 10:11:49 -080027#include "android-base/stringprintf.h"
28
David Sehr891a50e2017-10-27 17:01:07 -070029#include "base/file_utils.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030#include "base/logging.h"
Igor Murashkin37743352014-11-13 14:38:00 -080031#include "base/stringpiece.h"
32#include "noop_compiler_callbacks.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080033#include "runtime.h"
Igor Murashkin37743352014-11-13 14:38:00 -080034
35#if !defined(NDEBUG)
36#define DBG_LOG LOG(INFO)
37#else
38#define DBG_LOG LOG(DEBUG)
39#endif
40
41namespace art {
42
43// TODO: Move to <runtime/utils.h> and remove all copies of this function.
44static bool LocationToFilename(const std::string& location, InstructionSet isa,
45 std::string* filename) {
46 bool has_system = false;
47 bool has_cache = false;
48 // image_location = /system/framework/boot.art
49 // system_image_filename = /system/framework/<image_isa>/boot.art
50 std::string system_filename(GetSystemImageFilename(location.c_str(), isa));
51 if (OS::FileExists(system_filename.c_str())) {
52 has_system = true;
53 }
54
55 bool have_android_data = false;
56 bool dalvik_cache_exists = false;
57 bool is_global_cache = false;
58 std::string dalvik_cache;
59 GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache,
60 &have_android_data, &dalvik_cache_exists, &is_global_cache);
61
62 std::string cache_filename;
63 if (have_android_data && dalvik_cache_exists) {
64 // Always set output location even if it does not exist,
65 // so that the caller knows where to create the image.
66 //
67 // image_location = /system/framework/boot.art
68 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
69 std::string error_msg;
70 if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(),
71 &cache_filename, &error_msg)) {
72 has_cache = true;
73 }
74 }
75 if (has_system) {
76 *filename = system_filename;
77 return true;
78 } else if (has_cache) {
79 *filename = cache_filename;
80 return true;
81 } else {
Andreas Gampe8fae4b52017-09-27 20:04:47 -070082 *filename = system_filename;
Igor Murashkin37743352014-11-13 14:38:00 -080083 return false;
84 }
85}
86
Jeff Haodcdc85b2015-12-04 14:06:18 -080087static Runtime* StartRuntime(const char* boot_image_location, InstructionSet instruction_set) {
Igor Murashkin37743352014-11-13 14:38:00 -080088 CHECK(boot_image_location != nullptr);
89
90 RuntimeOptions options;
91
92 // We are more like a compiler than a run-time. We don't want to execute code.
93 {
94 static NoopCompilerCallbacks callbacks;
95 options.push_back(std::make_pair("compilercallbacks", &callbacks));
96 }
97
98 // Boot image location.
99 {
100 std::string boot_image_option;
101 boot_image_option += "-Ximage:";
102 boot_image_option += boot_image_location;
103 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
104 }
105
106 // Instruction set.
107 options.push_back(
108 std::make_pair("imageinstructionset",
109 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
Calin Juravle01aaf6e2015-06-19 22:05:39 +0100110 // None of the command line tools need sig chain. If this changes we'll need
111 // to upgrade this option to a proper parameter.
112 options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
Igor Murashkin37743352014-11-13 14:38:00 -0800113 if (!Runtime::Create(options, false)) {
114 fprintf(stderr, "Failed to create runtime\n");
115 return nullptr;
116 }
117
118 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
119 // give it away now and then switch to a more manageable ScopedObjectAccess.
120 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
121
122 return Runtime::Current();
123}
124
125struct CmdlineArgs {
126 enum ParseStatus {
127 kParseOk, // Parse successful. Do not set the error message.
128 kParseUnknownArgument, // Unknown argument. Do not set the error message.
129 kParseError, // Parse ok, but failed elsewhere. Print the set error message.
130 };
131
132 bool Parse(int argc, char** argv) {
133 // Skip over argv[0].
134 argv++;
135 argc--;
136
137 if (argc == 0) {
138 fprintf(stderr, "No arguments specified\n");
139 PrintUsage();
140 return false;
141 }
142
143 std::string error_msg;
144 for (int i = 0; i < argc; i++) {
145 const StringPiece option(argv[i]);
146 if (option.starts_with("--boot-image=")) {
147 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
148 } else if (option.starts_with("--instruction-set=")) {
149 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
150 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
151 if (instruction_set_ == kNone) {
152 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
153 PrintUsage();
154 return false;
155 }
156 } else if (option.starts_with("--output=")) {
157 output_name_ = option.substr(strlen("--output=")).ToString();
158 const char* filename = output_name_.c_str();
159 out_.reset(new std::ofstream(filename));
160 if (!out_->good()) {
161 fprintf(stderr, "Failed to open output filename %s\n", filename);
162 PrintUsage();
163 return false;
164 }
165 os_ = out_.get();
166 } else {
167 ParseStatus parse_status = ParseCustom(option, &error_msg);
168
169 if (parse_status == kParseUnknownArgument) {
170 fprintf(stderr, "Unknown argument %s\n", option.data());
171 }
172
173 if (parse_status != kParseOk) {
174 fprintf(stderr, "%s\n", error_msg.c_str());
175 PrintUsage();
176 return false;
177 }
178 }
179 }
180
181 DBG_LOG << "will call parse checks";
182
183 {
184 ParseStatus checks_status = ParseChecks(&error_msg);
185 if (checks_status != kParseOk) {
186 fprintf(stderr, "%s\n", error_msg.c_str());
187 PrintUsage();
188 return false;
189 }
190 }
191
192 return true;
193 }
194
195 virtual std::string GetUsage() const {
196 std::string usage;
197
198 usage += // Required.
199 " --boot-image=<file.art>: provide the image location for the boot class path.\n"
200 " Do not include the arch as part of the name, it is added automatically.\n"
201 " Example: --boot-image=/system/framework/boot.art\n"
Kevin Brodsky64fff412015-11-24 14:24:34 +0000202 " (specifies /system/framework/<arch>/boot.art as the image file)\n"
Igor Murashkin37743352014-11-13 14:38:00 -0800203 "\n";
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800204 usage += android::base::StringPrintf( // Optional.
Andreas Gampe57b34292015-01-14 15:45:59 -0800205 " --instruction-set=(arm|arm64|mips|mips64|x86|x86_64): for locating the image\n"
Igor Murashkin37743352014-11-13 14:38:00 -0800206 " file based on the image location set.\n"
207 " Example: --instruction-set=x86\n"
208 " Default: %s\n"
209 "\n",
210 GetInstructionSetString(kRuntimeISA));
211 usage += // Optional.
212 " --output=<file> may be used to send the output to a file.\n"
213 " Example: --output=/tmp/oatdump.txt\n"
214 "\n";
215
216 return usage;
217 }
218
219 // Specified by --boot-image.
220 const char* boot_image_location_ = nullptr;
221 // Specified by --instruction-set.
Andreas Gampe8fae4b52017-09-27 20:04:47 -0700222 InstructionSet instruction_set_ = InstructionSet::kNone;
Igor Murashkin37743352014-11-13 14:38:00 -0800223 // Specified by --output.
224 std::ostream* os_ = &std::cout;
225 std::unique_ptr<std::ofstream> out_; // If something besides cout is used
226 std::string output_name_;
227
228 virtual ~CmdlineArgs() {}
229
Andreas Gampec24f3992014-12-17 20:40:11 -0800230 bool ParseCheckBootImage(std::string* error_msg) {
Igor Murashkin37743352014-11-13 14:38:00 -0800231 if (boot_image_location_ == nullptr) {
232 *error_msg = "--boot-image must be specified";
Andreas Gampec24f3992014-12-17 20:40:11 -0800233 return false;
Igor Murashkin37743352014-11-13 14:38:00 -0800234 }
Andreas Gampe8fae4b52017-09-27 20:04:47 -0700235 if (instruction_set_ == InstructionSet::kNone) {
236 LOG(WARNING) << "No instruction set given, assuming " << GetInstructionSetString(kRuntimeISA);
237 instruction_set_ = kRuntimeISA;
238 }
Igor Murashkin37743352014-11-13 14:38:00 -0800239
240 DBG_LOG << "boot image location: " << boot_image_location_;
241
242 // Checks for --boot-image location.
243 {
244 std::string boot_image_location = boot_image_location_;
Andreas Gampeca620d72016-11-08 08:09:33 -0800245 size_t file_name_idx = boot_image_location.rfind('/');
Igor Murashkin37743352014-11-13 14:38:00 -0800246 if (file_name_idx == std::string::npos) { // Prevent a InsertIsaDirectory check failure.
247 *error_msg = "Boot image location must have a / in it";
Andreas Gampec24f3992014-12-17 20:40:11 -0800248 return false;
Igor Murashkin37743352014-11-13 14:38:00 -0800249 }
250
251 // Don't let image locations with the 'arch' in it through, since it's not a location.
252 // This prevents a common error "Could not create an image space..." when initing the Runtime.
253 if (file_name_idx != std::string::npos) {
254 std::string no_file_name = boot_image_location.substr(0, file_name_idx);
Andreas Gampeca620d72016-11-08 08:09:33 -0800255 size_t ancestor_dirs_idx = no_file_name.rfind('/');
Igor Murashkin37743352014-11-13 14:38:00 -0800256
257 std::string parent_dir_name;
258 if (ancestor_dirs_idx != std::string::npos) {
259 parent_dir_name = no_file_name.substr(ancestor_dirs_idx + 1);
260 } else {
261 parent_dir_name = no_file_name;
262 }
263
264 DBG_LOG << "boot_image_location parent_dir_name was " << parent_dir_name;
265
266 if (GetInstructionSetFromString(parent_dir_name.c_str()) != kNone) {
267 *error_msg = "Do not specify the architecture as part of the boot image location";
Andreas Gampec24f3992014-12-17 20:40:11 -0800268 return false;
Igor Murashkin37743352014-11-13 14:38:00 -0800269 }
270 }
271
272 // Check that the boot image location points to a valid file name.
273 std::string file_name;
274 if (!LocationToFilename(boot_image_location, instruction_set_, &file_name)) {
Andreas Gampe8fae4b52017-09-27 20:04:47 -0700275 *error_msg = android::base::StringPrintf(
276 "No corresponding file for location '%s' (filename '%s') exists",
277 boot_image_location.c_str(),
278 file_name.c_str());
Andreas Gampec24f3992014-12-17 20:40:11 -0800279 return false;
Igor Murashkin37743352014-11-13 14:38:00 -0800280 }
281
282 DBG_LOG << "boot_image_filename does exist: " << file_name;
283 }
284
Andreas Gampec24f3992014-12-17 20:40:11 -0800285 return true;
Igor Murashkin37743352014-11-13 14:38:00 -0800286 }
287
Igor Murashkin37743352014-11-13 14:38:00 -0800288 void PrintUsage() {
289 fprintf(stderr, "%s", GetUsage().c_str());
290 }
Andreas Gampec24f3992014-12-17 20:40:11 -0800291
292 protected:
293 virtual ParseStatus ParseCustom(const StringPiece& option ATTRIBUTE_UNUSED,
294 std::string* error_msg ATTRIBUTE_UNUSED) {
295 return kParseUnknownArgument;
296 }
297
298 virtual ParseStatus ParseChecks(std::string* error_msg ATTRIBUTE_UNUSED) {
299 return kParseOk;
300 }
Igor Murashkin37743352014-11-13 14:38:00 -0800301};
302
303template <typename Args = CmdlineArgs>
304struct CmdlineMain {
305 int Main(int argc, char** argv) {
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700306 InitLogging(argv, Runtime::Abort);
Igor Murashkin37743352014-11-13 14:38:00 -0800307 std::unique_ptr<Args> args = std::unique_ptr<Args>(CreateArguments());
308 args_ = args.get();
309
310 DBG_LOG << "Try to parse";
311
312 if (args_ == nullptr || !args_->Parse(argc, argv)) {
313 return EXIT_FAILURE;
314 }
315
Igor Murashkin37743352014-11-13 14:38:00 -0800316 bool needs_runtime = NeedsRuntime();
Andreas Gampec24f3992014-12-17 20:40:11 -0800317 std::unique_ptr<Runtime> runtime;
318
Igor Murashkin37743352014-11-13 14:38:00 -0800319
320 if (needs_runtime) {
Andreas Gampec24f3992014-12-17 20:40:11 -0800321 std::string error_msg;
322 if (!args_->ParseCheckBootImage(&error_msg)) {
323 fprintf(stderr, "%s\n", error_msg.c_str());
324 args_->PrintUsage();
325 return EXIT_FAILURE;
326 }
327 runtime.reset(CreateRuntime(args.get()));
328 if (runtime == nullptr) {
329 return EXIT_FAILURE;
330 }
Igor Murashkin37743352014-11-13 14:38:00 -0800331 if (!ExecuteWithRuntime(runtime.get())) {
332 return EXIT_FAILURE;
333 }
334 } else {
335 if (!ExecuteWithoutRuntime()) {
336 return EXIT_FAILURE;
337 }
338 }
339
340 if (!ExecuteCommon()) {
341 return EXIT_FAILURE;
342 }
343
344 return EXIT_SUCCESS;
345 }
346
347 // Override this function to create your own arguments.
348 // Usually will want to return a subtype of CmdlineArgs.
349 virtual Args* CreateArguments() {
350 return new Args();
351 }
352
353 // Override this function to do something else with the runtime.
354 virtual bool ExecuteWithRuntime(Runtime* runtime) {
355 CHECK(runtime != nullptr);
356 // Do nothing
357 return true;
358 }
359
360 // Does the code execution need a runtime? Sometimes it doesn't.
361 virtual bool NeedsRuntime() {
362 return true;
363 }
364
365 // Do execution without having created a runtime.
366 virtual bool ExecuteWithoutRuntime() {
367 return true;
368 }
369
370 // Continue execution after ExecuteWith[out]Runtime
371 virtual bool ExecuteCommon() {
372 return true;
373 }
374
375 virtual ~CmdlineMain() {}
376
377 protected:
378 Args* args_ = nullptr;
379
380 private:
Andreas Gampec24f3992014-12-17 20:40:11 -0800381 Runtime* CreateRuntime(CmdlineArgs* args) {
Igor Murashkin37743352014-11-13 14:38:00 -0800382 CHECK(args != nullptr);
383
Andreas Gampec24f3992014-12-17 20:40:11 -0800384 return StartRuntime(args->boot_image_location_, args->instruction_set_);
Igor Murashkin37743352014-11-13 14:38:00 -0800385 }
386};
387} // namespace art
388
389#endif // ART_CMDLINE_CMDLINE_H_