OSDN Git Service

Merge "Implement common super type in reference type propagation." am: a4d1b6438f...
[android-x86/art.git] / cmdline / cmdline.h
1 /*
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
38 namespace art {
39
40 // TODO: Move to <runtime/utils.h> and remove all copies of this function.
41 static 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
83 static 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   // None of the command line tools need sig chain. If this changes we'll need
108   // to upgrade this option to a proper parameter.
109   options.push_back(std::make_pair("-Xno-sig-chain", nullptr));
110   if (!Runtime::Create(options, false)) {
111     fprintf(stderr, "Failed to create runtime\n");
112     return nullptr;
113   }
114
115   // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
116   // give it away now and then switch to a more manageable ScopedObjectAccess.
117   Thread::Current()->TransitionFromRunnableToSuspended(kNative);
118
119   return Runtime::Current();
120 }
121
122 struct CmdlineArgs {
123   enum ParseStatus {
124     kParseOk,               // Parse successful. Do not set the error message.
125     kParseUnknownArgument,  // Unknown argument. Do not set the error message.
126     kParseError,            // Parse ok, but failed elsewhere. Print the set error message.
127   };
128
129   bool Parse(int argc, char** argv) {
130     // Skip over argv[0].
131     argv++;
132     argc--;
133
134     if (argc == 0) {
135       fprintf(stderr, "No arguments specified\n");
136       PrintUsage();
137       return false;
138     }
139
140     std::string error_msg;
141     for (int i = 0; i < argc; i++) {
142       const StringPiece option(argv[i]);
143       if (option.starts_with("--boot-image=")) {
144         boot_image_location_ = option.substr(strlen("--boot-image=")).data();
145       } else if (option.starts_with("--instruction-set=")) {
146         StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
147         instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
148         if (instruction_set_ == kNone) {
149           fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
150           PrintUsage();
151           return false;
152         }
153       } else if (option.starts_with("--output=")) {
154         output_name_ = option.substr(strlen("--output=")).ToString();
155         const char* filename = output_name_.c_str();
156         out_.reset(new std::ofstream(filename));
157         if (!out_->good()) {
158           fprintf(stderr, "Failed to open output filename %s\n", filename);
159           PrintUsage();
160           return false;
161         }
162         os_ = out_.get();
163       } else {
164         ParseStatus parse_status = ParseCustom(option, &error_msg);
165
166         if (parse_status == kParseUnknownArgument) {
167           fprintf(stderr, "Unknown argument %s\n", option.data());
168         }
169
170         if (parse_status != kParseOk) {
171           fprintf(stderr, "%s\n", error_msg.c_str());
172           PrintUsage();
173           return false;
174         }
175       }
176     }
177
178     DBG_LOG << "will call parse checks";
179
180     {
181       ParseStatus checks_status = ParseChecks(&error_msg);
182       if (checks_status != kParseOk) {
183           fprintf(stderr, "%s\n", error_msg.c_str());
184           PrintUsage();
185           return false;
186       }
187     }
188
189     return true;
190   }
191
192   virtual std::string GetUsage() const {
193     std::string usage;
194
195     usage +=  // Required.
196         "  --boot-image=<file.art>: provide the image location for the boot class path.\n"
197         "      Do not include the arch as part of the name, it is added automatically.\n"
198         "      Example: --boot-image=/system/framework/boot.art\n"
199         "               (specifies /system/framework/<arch>/boot.art as the image file)\n"
200         "\n";
201     usage += StringPrintf(  // Optional.
202         "  --instruction-set=(arm|arm64|mips|mips64|x86|x86_64): for locating the image\n"
203         "      file based on the image location set.\n"
204         "      Example: --instruction-set=x86\n"
205         "      Default: %s\n"
206         "\n",
207         GetInstructionSetString(kRuntimeISA));
208     usage +=  // Optional.
209         "  --output=<file> may be used to send the output to a file.\n"
210         "      Example: --output=/tmp/oatdump.txt\n"
211         "\n";
212
213     return usage;
214   }
215
216   // Specified by --boot-image.
217   const char* boot_image_location_ = nullptr;
218   // Specified by --instruction-set.
219   InstructionSet instruction_set_ = kRuntimeISA;
220   // Specified by --output.
221   std::ostream* os_ = &std::cout;
222   std::unique_ptr<std::ofstream> out_;  // If something besides cout is used
223   std::string output_name_;
224
225   virtual ~CmdlineArgs() {}
226
227   bool ParseCheckBootImage(std::string* error_msg) {
228     if (boot_image_location_ == nullptr) {
229       *error_msg = "--boot-image must be specified";
230       return false;
231     }
232
233     DBG_LOG << "boot image location: " << boot_image_location_;
234
235     // Checks for --boot-image location.
236     {
237       std::string boot_image_location = boot_image_location_;
238       size_t file_name_idx = boot_image_location.rfind("/");
239       if (file_name_idx == std::string::npos) {  // Prevent a InsertIsaDirectory check failure.
240         *error_msg = "Boot image location must have a / in it";
241         return false;
242       }
243
244       // Don't let image locations with the 'arch' in it through, since it's not a location.
245       // This prevents a common error "Could not create an image space..." when initing the Runtime.
246       if (file_name_idx != std::string::npos) {
247         std::string no_file_name = boot_image_location.substr(0, file_name_idx);
248         size_t ancestor_dirs_idx = no_file_name.rfind("/");
249
250         std::string parent_dir_name;
251         if (ancestor_dirs_idx != std::string::npos) {
252           parent_dir_name = no_file_name.substr(ancestor_dirs_idx + 1);
253         } else {
254           parent_dir_name = no_file_name;
255         }
256
257         DBG_LOG << "boot_image_location parent_dir_name was " << parent_dir_name;
258
259         if (GetInstructionSetFromString(parent_dir_name.c_str()) != kNone) {
260           *error_msg = "Do not specify the architecture as part of the boot image location";
261           return false;
262         }
263       }
264
265       // Check that the boot image location points to a valid file name.
266       std::string file_name;
267       if (!LocationToFilename(boot_image_location, instruction_set_, &file_name)) {
268         *error_msg = StringPrintf("No corresponding file for location '%s' exists",
269                                   file_name.c_str());
270         return false;
271       }
272
273       DBG_LOG << "boot_image_filename does exist: " << file_name;
274     }
275
276     return true;
277   }
278
279   void PrintUsage() {
280     fprintf(stderr, "%s", GetUsage().c_str());
281   }
282
283  protected:
284   virtual ParseStatus ParseCustom(const StringPiece& option ATTRIBUTE_UNUSED,
285                                   std::string* error_msg ATTRIBUTE_UNUSED) {
286     return kParseUnknownArgument;
287   }
288
289   virtual ParseStatus ParseChecks(std::string* error_msg ATTRIBUTE_UNUSED) {
290     return kParseOk;
291   }
292 };
293
294 template <typename Args = CmdlineArgs>
295 struct CmdlineMain {
296   int Main(int argc, char** argv) {
297     InitLogging(argv);
298     std::unique_ptr<Args> args = std::unique_ptr<Args>(CreateArguments());
299     args_ = args.get();
300
301     DBG_LOG << "Try to parse";
302
303     if (args_ == nullptr || !args_->Parse(argc, argv)) {
304       return EXIT_FAILURE;
305     }
306
307     bool needs_runtime = NeedsRuntime();
308     std::unique_ptr<Runtime> runtime;
309
310
311     if (needs_runtime) {
312       std::string error_msg;
313       if (!args_->ParseCheckBootImage(&error_msg)) {
314         fprintf(stderr, "%s\n", error_msg.c_str());
315         args_->PrintUsage();
316         return EXIT_FAILURE;
317       }
318       runtime.reset(CreateRuntime(args.get()));
319       if (runtime == nullptr) {
320         return EXIT_FAILURE;
321       }
322       if (!ExecuteWithRuntime(runtime.get())) {
323         return EXIT_FAILURE;
324       }
325     } else {
326       if (!ExecuteWithoutRuntime()) {
327         return EXIT_FAILURE;
328       }
329     }
330
331     if (!ExecuteCommon()) {
332       return EXIT_FAILURE;
333     }
334
335     return EXIT_SUCCESS;
336   }
337
338   // Override this function to create your own arguments.
339   // Usually will want to return a subtype of CmdlineArgs.
340   virtual Args* CreateArguments() {
341     return new Args();
342   }
343
344   // Override this function to do something else with the runtime.
345   virtual bool ExecuteWithRuntime(Runtime* runtime) {
346     CHECK(runtime != nullptr);
347     // Do nothing
348     return true;
349   }
350
351   // Does the code execution need a runtime? Sometimes it doesn't.
352   virtual bool NeedsRuntime() {
353     return true;
354   }
355
356   // Do execution without having created a runtime.
357   virtual bool ExecuteWithoutRuntime() {
358     return true;
359   }
360
361   // Continue execution after ExecuteWith[out]Runtime
362   virtual bool ExecuteCommon() {
363     return true;
364   }
365
366   virtual ~CmdlineMain() {}
367
368  protected:
369   Args* args_ = nullptr;
370
371  private:
372   Runtime* CreateRuntime(CmdlineArgs* args) {
373     CHECK(args != nullptr);
374
375     return StartRuntime(args->boot_image_location_, args->instruction_set_);
376   }
377 };
378 }  // namespace art
379
380 #endif  // ART_CMDLINE_CMDLINE_H_