Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "src/runtime.h" |
| 4 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 5 | #include <cstdio> |
| 6 | #include <cstdlib> |
| 7 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 8 | #include "src/class_linker.h" |
| 9 | #include "src/heap.h" |
| 10 | #include "src/thread.h" |
| 11 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 12 | namespace art { |
| 13 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 14 | Runtime::~Runtime() { |
| 15 | // TODO: use a smart pointer instead. |
| 16 | delete class_linker_; |
| 17 | delete heap_; |
| 18 | delete thread_list_; |
| 19 | } |
| 20 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 21 | void Runtime::Abort(const char* file, int line) { |
| 22 | // Get any pending output out of the way. |
| 23 | fflush(NULL); |
| 24 | |
| 25 | // Many people have difficulty distinguish aborts from crashes, |
| 26 | // so be explicit. |
| 27 | LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting..."; |
| 28 | |
| 29 | // TODO: if we support an abort hook, call it here. |
| 30 | |
| 31 | // Perform any platform-specific pre-abort actions. |
| 32 | PlatformAbort(file, line); |
| 33 | |
| 34 | // If we call abort(3) on a device, all threads in the process |
| 35 | // receive SIBABRT. |
| 36 | // debuggerd dumps the stack trace of the main thread, whether or not |
| 37 | // that was the thread that failed. |
| 38 | // By stuffing a value into a bogus address, we cause a segmentation |
| 39 | // fault in the current thread, and get a useful log from debuggerd. |
| 40 | // We can also trivially tell the difference between a VM crash and |
| 41 | // a deliberate abort by looking at the fault address. |
| 42 | *reinterpret_cast<char*>(0xdeadd00d) = 38; |
| 43 | abort(); |
| 44 | |
| 45 | // notreached |
| 46 | } |
| 47 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 48 | Runtime* Runtime::Create() { |
| 49 | scoped_ptr<Runtime> runtime(new Runtime()); |
| 50 | bool success = runtime->Init(); |
| 51 | if (!success) { |
| 52 | return NULL; |
| 53 | } else { |
| 54 | return runtime.release(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | bool Runtime::Init() { |
| 59 | thread_list_ = ThreadList::Create(); |
| 60 | heap_ = Heap::Create(); |
| 61 | Thread::Init(); |
| 62 | Thread* current_thread = Thread::Attach(); |
| 63 | thread_list_->Register(current_thread); |
| 64 | class_linker_ = ClassLinker::Create(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 68 | bool Runtime::AttachCurrentThread() { |
| 69 | return Thread::Attach() != NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 72 | bool Runtime::DetachCurrentThread() { |
| 73 | LOG(WARNING) << "Unimplemented: Runtime::DetachCurrentThread"; |
| 74 | return true; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | } // namespace art |