blob: f03157f241c1c883564a39e7da758c305626cd79 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07004
Elliott Hughesffe67362011-07-17 12:09:27 -07005#include <cstdio>
6#include <cstdlib>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07007#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -07008
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "class_linker.h"
10#include "heap.h"
11#include "thread.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070012
Carl Shapiro1fb86202011-06-27 17:43:13 -070013namespace art {
14
Carl Shapiro2ed144c2011-07-26 16:52:08 -070015Runtime* Runtime::instance_ = NULL;
16
Carl Shapiro61e019d2011-07-14 16:53:09 -070017Runtime::~Runtime() {
18 // TODO: use a smart pointer instead.
19 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070020 Heap::Destroy();
Carl Shapiro61e019d2011-07-14 16:53:09 -070021 delete thread_list_;
Carl Shapiro4acf4642011-07-26 18:54:13 -070022 // TODO: acquire a static mutex on Runtime to avoid racing.
23 CHECK(instance_ == this);
24 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070025}
26
Elliott Hughesffe67362011-07-17 12:09:27 -070027void Runtime::Abort(const char* file, int line) {
28 // Get any pending output out of the way.
29 fflush(NULL);
30
31 // Many people have difficulty distinguish aborts from crashes,
32 // so be explicit.
33 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
34
35 // TODO: if we support an abort hook, call it here.
36
37 // Perform any platform-specific pre-abort actions.
38 PlatformAbort(file, line);
39
40 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070041 // receive SIGABRT. debuggerd dumps the stack trace of the main
42 // thread, whether or not that was the thread that failed. By
43 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070044 // fault in the current thread, and get a useful log from debuggerd.
45 // We can also trivially tell the difference between a VM crash and
46 // a deliberate abort by looking at the fault address.
47 *reinterpret_cast<char*>(0xdeadd00d) = 38;
48 abort();
49
50 // notreached
51}
52
Carl Shapiro2ed144c2011-07-26 16:52:08 -070053Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
54 // TODO: parse arguments
55 std::vector<DexFile*> boot_class_path; // empty
56 return Runtime::Create(boot_class_path);
57}
58
59Runtime* Runtime::Create(const std::vector<DexFile*>& boot_class_path) {
60 // TODO: acquire a static mutex on Runtime to avoid racing.
61 if (Runtime::instance_ != NULL) {
62 return NULL;
63 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070064 scoped_ptr<Runtime> runtime(new Runtime());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070065 bool success = runtime->Init(boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070066 if (!success) {
67 return NULL;
68 } else {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070069 return Runtime::instance_ = runtime.release();
Carl Shapiro61e019d2011-07-14 16:53:09 -070070 }
71}
72
Carl Shapiro2ed144c2011-07-26 16:52:08 -070073bool Runtime::Init(const std::vector<DexFile*>& boot_class_path) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070074 thread_list_ = ThreadList::Create();
Carl Shapiro69759ea2011-07-21 18:13:35 -070075 Heap::Init(Heap::kStartupSize, Heap::kMaximumSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070076 Thread::Init();
77 Thread* current_thread = Thread::Attach();
78 thread_list_->Register(current_thread);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070079 class_linker_ = ClassLinker::Create(boot_class_path);
Carl Shapiro1fb86202011-06-27 17:43:13 -070080 return true;
81}
82
Carl Shapiro2ed144c2011-07-26 16:52:08 -070083bool Runtime::AttachCurrentThread(const char* name, JniEnvironment** penv) {
84 return Thread::Attach() != NULL;
85}
86
87bool Runtime::AttachCurrentThreadAsDaemon(const char* name,
88 JniEnvironment** penv) {
89 // TODO: do something different for daemon threads.
Ian Rogersb033c752011-07-20 12:22:35 -070090 return Thread::Attach() != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070091}
92
Ian Rogersb033c752011-07-20 12:22:35 -070093bool Runtime::DetachCurrentThread() {
94 LOG(WARNING) << "Unimplemented: Runtime::DetachCurrentThread";
95 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -070096}
97
98} // namespace art