blob: 3adaea75785739bb59b37b783db4756248833be9 [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_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070017 Heap::Destroy();
Carl Shapiro61e019d2011-07-14 16:53:09 -070018 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
Carl Shapiro69759ea2011-07-21 18:13:35 -070035 // 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 Hughesffe67362011-07-17 12:09:27 -070038 // 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
Carl Shapiro61e019d2011-07-14 16:53:09 -070047Runtime* Runtime::Create() {
48 scoped_ptr<Runtime> runtime(new Runtime());
49 bool success = runtime->Init();
50 if (!success) {
51 return NULL;
52 } else {
53 return runtime.release();
54 }
55}
56
57bool Runtime::Init() {
58 thread_list_ = ThreadList::Create();
Carl Shapiro69759ea2011-07-21 18:13:35 -070059 Heap::Init(Heap::kStartupSize, Heap::kMaximumSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070060 Thread::Init();
61 Thread* current_thread = Thread::Attach();
62 thread_list_->Register(current_thread);
63 class_linker_ = ClassLinker::Create();
Carl Shapiro1fb86202011-06-27 17:43:13 -070064 return true;
65}
66
Ian Rogersb033c752011-07-20 12:22:35 -070067bool Runtime::AttachCurrentThread() {
68 return Thread::Attach() != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070069}
70
Ian Rogersb033c752011-07-20 12:22:35 -070071bool Runtime::DetachCurrentThread() {
72 LOG(WARNING) << "Unimplemented: Runtime::DetachCurrentThread";
73 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -070074}
75
76} // namespace art