Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "runtime.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 4 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 5 | #include <cstdio> |
| 6 | #include <cstdlib> |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 7 | #include <vector> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 8 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 9 | #include "class_linker.h" |
| 10 | #include "heap.h" |
| 11 | #include "thread.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 12 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 13 | namespace art { |
| 14 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 15 | Runtime* Runtime::instance_ = NULL; |
| 16 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 17 | Runtime::~Runtime() { |
| 18 | // TODO: use a smart pointer instead. |
| 19 | delete class_linker_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 20 | Heap::Destroy(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 21 | delete thread_list_; |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame^] | 22 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 23 | CHECK(instance_ == this); |
| 24 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 27 | void Runtime::Abort(const char* file, int line) { |
| 28 | // Get any pending output out of the way. |
| 29 | fflush(NULL); |
| 30 | |
| 31 | // Many people have difficulty distinguish aborts from crashes, |
| 32 | // so be explicit. |
| 33 | LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting..."; |
| 34 | |
| 35 | // TODO: if we support an abort hook, call it here. |
| 36 | |
| 37 | // Perform any platform-specific pre-abort actions. |
| 38 | PlatformAbort(file, line); |
| 39 | |
| 40 | // If we call abort(3) on a device, all threads in the process |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 41 | // receive SIGABRT. debuggerd dumps the stack trace of the main |
| 42 | // thread, whether or not that was the thread that failed. By |
| 43 | // stuffing a value into a bogus address, we cause a segmentation |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 44 | // fault in the current thread, and get a useful log from debuggerd. |
| 45 | // We can also trivially tell the difference between a VM crash and |
| 46 | // a deliberate abort by looking at the fault address. |
| 47 | *reinterpret_cast<char*>(0xdeadd00d) = 38; |
| 48 | abort(); |
| 49 | |
| 50 | // notreached |
| 51 | } |
| 52 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 53 | Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) { |
| 54 | // TODO: parse arguments |
| 55 | std::vector<DexFile*> boot_class_path; // empty |
| 56 | return Runtime::Create(boot_class_path); |
| 57 | } |
| 58 | |
| 59 | Runtime* Runtime::Create(const std::vector<DexFile*>& boot_class_path) { |
| 60 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 61 | if (Runtime::instance_ != NULL) { |
| 62 | return NULL; |
| 63 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 64 | scoped_ptr<Runtime> runtime(new Runtime()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 65 | bool success = runtime->Init(boot_class_path); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 66 | if (!success) { |
| 67 | return NULL; |
| 68 | } else { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 69 | return Runtime::instance_ = runtime.release(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 73 | bool Runtime::Init(const std::vector<DexFile*>& boot_class_path) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 74 | thread_list_ = ThreadList::Create(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 75 | Heap::Init(Heap::kStartupSize, Heap::kMaximumSize); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 76 | Thread::Init(); |
| 77 | Thread* current_thread = Thread::Attach(); |
| 78 | thread_list_->Register(current_thread); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 79 | class_linker_ = ClassLinker::Create(boot_class_path); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 83 | bool Runtime::AttachCurrentThread(const char* name, JniEnvironment** penv) { |
| 84 | return Thread::Attach() != NULL; |
| 85 | } |
| 86 | |
| 87 | bool Runtime::AttachCurrentThreadAsDaemon(const char* name, |
| 88 | JniEnvironment** penv) { |
| 89 | // TODO: do something different for daemon threads. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 90 | return Thread::Attach() != NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 93 | bool Runtime::DetachCurrentThread() { |
| 94 | LOG(WARNING) << "Unimplemented: Runtime::DetachCurrentThread"; |
| 95 | return true; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace art |