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> |
| 7 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 8 | #include "class_linker.h" |
| 9 | #include "heap.h" |
| 10 | #include "thread.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 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_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 17 | Heap::Destroy(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 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 |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 35 | // receive SIGABRT. debuggerd dumps the stack trace of the main |
| 36 | // thread, whether or not that was the thread that failed. By |
| 37 | // stuffing a value into a bogus address, we cause a segmentation |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 38 | // fault in the current thread, and get a useful log from debuggerd. |
| 39 | // We can also trivially tell the difference between a VM crash and |
| 40 | // a deliberate abort by looking at the fault address. |
| 41 | *reinterpret_cast<char*>(0xdeadd00d) = 38; |
| 42 | abort(); |
| 43 | |
| 44 | // notreached |
| 45 | } |
| 46 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 47 | Runtime* Runtime::Create(std::vector<RawDexFile*> boot_class_path) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 48 | scoped_ptr<Runtime> runtime(new Runtime()); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 49 | bool success = runtime->Init(boot_class_path); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 50 | if (!success) { |
| 51 | return NULL; |
| 52 | } else { |
| 53 | return runtime.release(); |
| 54 | } |
| 55 | } |
| 56 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 57 | bool Runtime::Init(std::vector<RawDexFile*> boot_class_path) { |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 58 | thread_list_ = ThreadList::Create(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 59 | Heap::Init(Heap::kStartupSize, Heap::kMaximumSize); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 60 | Thread::Init(); |
| 61 | Thread* current_thread = Thread::Attach(); |
| 62 | thread_list_->Register(current_thread); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame^] | 63 | class_linker_ = ClassLinker::Create(boot_class_path); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 67 | bool Runtime::AttachCurrentThread() { |
| 68 | return Thread::Attach() != NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 71 | bool Runtime::DetachCurrentThread() { |
| 72 | LOG(WARNING) << "Unimplemented: Runtime::DetachCurrentThread"; |
| 73 | return true; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | } // namespace art |