blob: c15594a80e4686b0881b37957074c494423ef614 [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
27#include "runtime.h"
28#include "base/stringpiece.h"
29#include "noop_compiler_callbacks.h"
30#include "base/logging.h"
31
32#if !defined(NDEBUG)
33#define DBG_LOG LOG(INFO)
34#else
35#define DBG_LOG LOG(DEBUG)
36#endif
37
38namespace art {
39
40// TODO: Move to <runtime/utils.h> and remove all copies of this function.
41static bool LocationToFilename(const std::string& location, InstructionSet isa,
42 std::string* filename) {
43 bool has_system = false;
44 bool has_cache = false;
45 // image_location = /system/framework/boot.art
46 // system_image_filename = /system/framework/<image_isa>/boot.art
47 std::string system_filename(GetSystemImageFilename(location.c_str(), isa));
48 if (OS::FileExists(system_filename.c_str())) {
49 has_system = true;
50 }
51
52 bool have_android_data = false;
53 bool dalvik_cache_exists = false;
54 bool is_global_cache = false;
55 std::string dalvik_cache;
56 GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache,
57 &have_android_data, &dalvik_cache_exists, &is_global_cache);
58
59 std::string cache_filename;
60 if (have_android_data && dalvik_cache_exists) {
61 // Always set output location even if it does not exist,
62 // so that the caller knows where to create the image.
63 //
64 // image_location = /system/framework/boot.art
65 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
66 std::string error_msg;
67 if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(),
68 &cache_filename, &error_msg)) {
69 has_cache = true;
70 }
71 }
72 if (has_system) {
73 *filename = system_filename;
74 return true;
75 } else if (has_cache) {
76 *filename = cache_filename;
77 return true;
78 } else {
79 return false;
80 }
81}
82
83static Runtime* StartRuntime(const char* boot_image_location,
84 InstructionSet instruction_set) {
85 CHECK(boot_image_location != nullptr);
86
87 RuntimeOptions options;
88
89 // We are more like a compiler than a run-time. We don't want to execute code.
90 {
91 static NoopCompilerCallbacks callbacks;
92 options.push_back(std::make_pair("compilercallbacks", &callbacks));
93 }
94
95 // Boot image location.
96 {
97 std::string boot_image_option;
98 boot_image_option += "-Ximage:";
99 boot_image_option += boot_image_location;
100 options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
101 }
102
103 // Instruction set.
104 options.push_back(
105 std::make_pair("imageinstructionset",
106 reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
107
108 if (!Runtime::Create(options, false)) {
109 fprintf(stderr, "Failed to create runtime\n");
110 return nullptr;
111 }
112
113 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
114 // give it away now and then switch to a more manageable ScopedObjectAccess.
115 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
116
117 return Runtime::Current();
118}
119
120struct CmdlineArgs {
121 enum ParseStatus {
122 kParseOk, // Parse successful. Do not set the error message.
123 kParseUnknownArgument, // Unknown argument. Do not set the error message.
124 kParseError, // Parse ok, but failed elsewhere. Print the set error message.
125 };
126
127 bool Parse(int argc, char** argv) {
128 // Skip over argv[0].
129 argv++;
130 argc--;
131
132 if (argc == 0) {
133 fprintf(stderr, "No arguments specified\n");
134 PrintUsage();
135 return false;
136 }
137
138 std::string error_msg;
139 for (int i = 0; i < argc; i++) {
140 const StringPiece option(argv[i]);
141 if (option.starts_with("--boot-image=")) {
142 boot_image_location_ = option.substr(strlen("--boot-image=")).data();
143 } else if (option.starts_with("--instruction-set=")) {
144 StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
145 instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
146 if (instruction_set_ == kNone) {
147 fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
148 PrintUsage();
149 return false;
150 }
151 } else if (option.starts_with("--output=")) {
152 output_name_ = option.substr(strlen("--output=")).ToString();
153 const char* filename = output_name_.c_str();
154 out_.reset(new std::ofstream(filename));
155 if (!out_->good()) {
156 fprintf(stderr, "Failed to open output filename %s\n", filename);
157 PrintUsage();
158 return false;
159 }
160 os_ = out_.get();
161 } else {
162 ParseStatus parse_status = ParseCustom(option, &error_msg);
163
164 if (parse_status == kParseUnknownArgument) {
165 fprintf(stderr, "Unknown argument %s\n", option.data());
166 }
167
168 if (parse_status != kParseOk) {
169 fprintf(stderr, "%s\n", error_msg.c_str());
170 PrintUsage();
171 return false;
172 }
173 }
174 }
175
176 DBG_LOG << "will call parse checks";
177
178 {
179 ParseStatus checks_status = ParseChecks(&error_msg);
180 if (checks_status != kParseOk) {
181 fprintf(stderr, "%s\n", error_msg.c_str());
182 PrintUsage();
183 return false;
184 }
185 }
186
187 return true;
188 }
189
190 virtual std::string GetUsage() const {
191 std::string usage;
192
193 usage += // Required.
194 " --boot-image=<file.art>: provide the image location for the boot class path.\n"
195 " Do not include the arch as part of the name, it is added automatically.\n"
196 " Example: --boot-image=/system/framework/boot.art\n"
197 "\n";
198 usage += StringPrintf( // Optional.
199 " --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image\n"
200 " file based on the image location set.\n"
201 " Example: --instruction-set=x86\n"
202 " Default: %s\n"
203 "\n",
204 GetInstructionSetString(kRuntimeISA));
205 usage += // Optional.
206 " --output=<file> may be used to send the output to a file.\n"
207 " Example: --output=/tmp/oatdump.txt\n"
208 "\n";
209
210 return usage;
211 }
212
213 // Specified by --boot-image.
214 const char* boot_image_location_ = nullptr;
215 // Specified by --instruction-set.
216 InstructionSet instruction_set_ = kRuntimeISA;
217 // Specified by --output.
218 std::ostream* os_ = &std::cout;
219 std::unique_ptr<std::ofstream> out_; // If something besides cout is used
220 std::string output_name_;
221
222 virtual ~CmdlineArgs() {}
223
224 protected:
225 virtual ParseStatus ParseCustom(const StringPiece& option, std::string* error_msg) {
226 UNUSED(option);
227 UNUSED(error_msg);
228
229 return kParseUnknownArgument;
230 }
231
232 virtual ParseStatus ParseChecks(std::string* error_msg) {
233 if (boot_image_location_ == nullptr) {
234 *error_msg = "--boot-image must be specified";
235 return kParseError;
236 }
237
238 DBG_LOG << "boot image location: " << boot_image_location_;
239
240 // Checks for --boot-image location.
241 {
242 std::string boot_image_location = boot_image_location_;
243 size_t file_name_idx = boot_image_location.rfind("/");
244 if (file_name_idx == std::string::npos) { // Prevent a InsertIsaDirectory check failure.
245 *error_msg = "Boot image location must have a / in it";
246 return kParseError;
247 }
248
249 // Don't let image locations with the 'arch' in it through, since it's not a location.
250 // This prevents a common error "Could not create an image space..." when initing the Runtime.
251 if (file_name_idx != std::string::npos) {
252 std::string no_file_name = boot_image_location.substr(0, file_name_idx);
253 size_t ancestor_dirs_idx = no_file_name.rfind("/");
254
255 std::string parent_dir_name;
256 if (ancestor_dirs_idx != std::string::npos) {
257 parent_dir_name = no_file_name.substr(ancestor_dirs_idx + 1);
258 } else {
259 parent_dir_name = no_file_name;
260 }
261
262 DBG_LOG << "boot_image_location parent_dir_name was " << parent_dir_name;
263
264 if (GetInstructionSetFromString(parent_dir_name.c_str()) != kNone) {
265 *error_msg = "Do not specify the architecture as part of the boot image location";
266 return kParseError;
267 }
268 }
269
270 // Check that the boot image location points to a valid file name.
271 std::string file_name;
272 if (!LocationToFilename(boot_image_location, instruction_set_, &file_name)) {
273 *error_msg = StringPrintf("No corresponding file for location '%s' exists",
274 file_name.c_str());
275 return kParseError;
276 }
277
278 DBG_LOG << "boot_image_filename does exist: " << file_name;
279 }
280
281 return kParseOk;
282 }
283
284 private:
285 void PrintUsage() {
286 fprintf(stderr, "%s", GetUsage().c_str());
287 }
288};
289
290template <typename Args = CmdlineArgs>
291struct CmdlineMain {
292 int Main(int argc, char** argv) {
293 InitLogging(argv);
294 std::unique_ptr<Args> args = std::unique_ptr<Args>(CreateArguments());
295 args_ = args.get();
296
297 DBG_LOG << "Try to parse";
298
299 if (args_ == nullptr || !args_->Parse(argc, argv)) {
300 return EXIT_FAILURE;
301 }
302
303 std::unique_ptr<Runtime> runtime = CreateRuntime(args.get());
304 if (runtime == nullptr) {
305 return EXIT_FAILURE;
306 }
307
308 bool needs_runtime = NeedsRuntime();
309
310 if (needs_runtime) {
311 if (!ExecuteWithRuntime(runtime.get())) {
312 return EXIT_FAILURE;
313 }
314 } else {
315 if (!ExecuteWithoutRuntime()) {
316 return EXIT_FAILURE;
317 }
318 }
319
320 if (!ExecuteCommon()) {
321 return EXIT_FAILURE;
322 }
323
324 return EXIT_SUCCESS;
325 }
326
327 // Override this function to create your own arguments.
328 // Usually will want to return a subtype of CmdlineArgs.
329 virtual Args* CreateArguments() {
330 return new Args();
331 }
332
333 // Override this function to do something else with the runtime.
334 virtual bool ExecuteWithRuntime(Runtime* runtime) {
335 CHECK(runtime != nullptr);
336 // Do nothing
337 return true;
338 }
339
340 // Does the code execution need a runtime? Sometimes it doesn't.
341 virtual bool NeedsRuntime() {
342 return true;
343 }
344
345 // Do execution without having created a runtime.
346 virtual bool ExecuteWithoutRuntime() {
347 return true;
348 }
349
350 // Continue execution after ExecuteWith[out]Runtime
351 virtual bool ExecuteCommon() {
352 return true;
353 }
354
355 virtual ~CmdlineMain() {}
356
357 protected:
358 Args* args_ = nullptr;
359
360 private:
361 std::unique_ptr<Runtime> CreateRuntime(CmdlineArgs* args) {
362 CHECK(args != nullptr);
363
364 return std::unique_ptr<Runtime>(StartRuntime(args->boot_image_location_,
365 args->instruction_set_));
366 }
367};
368} // namespace art
369
370#endif // ART_CMDLINE_CMDLINE_H_