blob: 28af9ddf581d74a2004477f839067782d86e2031 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "src/runtime.h"
4
Elliott Hughesffe67362011-07-17 12:09:27 -07005#include <cstdio>
6#include <cstdlib>
7
Carl Shapiro61e019d2011-07-14 16:53:09 -07008#include "src/class_linker.h"
9#include "src/heap.h"
10#include "src/thread.h"
11
Carl Shapiro1fb86202011-06-27 17:43:13 -070012namespace art {
13
Carl Shapiro61e019d2011-07-14 16:53:09 -070014Runtime::~Runtime() {
15 // TODO: use a smart pointer instead.
16 delete class_linker_;
17 delete heap_;
18 delete thread_list_;
19}
20
Elliott Hughesffe67362011-07-17 12:09:27 -070021void 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 Shapiro61e019d2011-07-14 16:53:09 -070048Runtime* 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
58bool 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 Shapiro1fb86202011-06-27 17:43:13 -070065 return true;
66}
67
Ian Rogersb033c752011-07-20 12:22:35 -070068bool Runtime::AttachCurrentThread() {
69 return Thread::Attach() != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070070}
71
Ian Rogersb033c752011-07-20 12:22:35 -070072bool Runtime::DetachCurrentThread() {
73 LOG(WARNING) << "Unimplemented: Runtime::DetachCurrentThread";
74 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -070075}
76
77} // namespace art