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