blob: eee84cd19337776be664e8dd51c0e47cc00cc5f3 [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_;
22}
23
Elliott Hughesffe67362011-07-17 12:09:27 -070024void Runtime::Abort(const char* file, int line) {
25 // Get any pending output out of the way.
26 fflush(NULL);
27
28 // Many people have difficulty distinguish aborts from crashes,
29 // so be explicit.
30 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
31
32 // TODO: if we support an abort hook, call it here.
33
34 // Perform any platform-specific pre-abort actions.
35 PlatformAbort(file, line);
36
37 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070038 // receive SIGABRT. debuggerd dumps the stack trace of the main
39 // thread, whether or not that was the thread that failed. By
40 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070041 // fault in the current thread, and get a useful log from debuggerd.
42 // We can also trivially tell the difference between a VM crash and
43 // a deliberate abort by looking at the fault address.
44 *reinterpret_cast<char*>(0xdeadd00d) = 38;
45 abort();
46
47 // notreached
48}
49
Carl Shapiro2ed144c2011-07-26 16:52:08 -070050Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
51 // TODO: parse arguments
52 std::vector<DexFile*> boot_class_path; // empty
53 return Runtime::Create(boot_class_path);
54}
55
56Runtime* Runtime::Create(const std::vector<DexFile*>& boot_class_path) {
57 // TODO: acquire a static mutex on Runtime to avoid racing.
58 if (Runtime::instance_ != NULL) {
59 return NULL;
60 }
Carl Shapiro61e019d2011-07-14 16:53:09 -070061 scoped_ptr<Runtime> runtime(new Runtime());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070062 bool success = runtime->Init(boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070063 if (!success) {
64 return NULL;
65 } else {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070066 return Runtime::instance_ = runtime.release();
Carl Shapiro61e019d2011-07-14 16:53:09 -070067 }
68}
69
Carl Shapiro2ed144c2011-07-26 16:52:08 -070070bool Runtime::Init(const std::vector<DexFile*>& boot_class_path) {
Carl Shapiro61e019d2011-07-14 16:53:09 -070071 thread_list_ = ThreadList::Create();
Carl Shapiro69759ea2011-07-21 18:13:35 -070072 Heap::Init(Heap::kStartupSize, Heap::kMaximumSize);
Carl Shapiro61e019d2011-07-14 16:53:09 -070073 Thread::Init();
74 Thread* current_thread = Thread::Attach();
75 thread_list_->Register(current_thread);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070076 class_linker_ = ClassLinker::Create(boot_class_path);
Carl Shapiro1fb86202011-06-27 17:43:13 -070077 return true;
78}
79
Carl Shapiro2ed144c2011-07-26 16:52:08 -070080bool Runtime::AttachCurrentThread(const char* name, JniEnvironment** penv) {
81 return Thread::Attach() != NULL;
82}
83
84bool Runtime::AttachCurrentThreadAsDaemon(const char* name,
85 JniEnvironment** penv) {
86 // TODO: do something different for daemon threads.
Ian Rogersb033c752011-07-20 12:22:35 -070087 return Thread::Attach() != NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070088}
89
Ian Rogersb033c752011-07-20 12:22:35 -070090bool Runtime::DetachCurrentThread() {
91 LOG(WARNING) << "Unimplemented: Runtime::DetachCurrentThread";
92 return true;
Carl Shapiro1fb86202011-06-27 17:43:13 -070093}
94
95} // namespace art