blob: 6d11cf3827ed743355b03ec2dea6d5c303b4ea9e [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_RUNTIME_H_
4#define ART_SRC_RUNTIME_H_
5
Elliott Hughesc5f7c912011-08-18 14:00:42 -07006#include <stdio.h>
7
Elliott Hughese27955c2011-08-26 15:21:24 -07008#include <iosfwd>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07009#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -070010#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070011#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012
Elliott Hughesc5f7c912011-08-18 14:00:42 -070013#include <jni.h>
14
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "globals.h"
17#include "macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "stringpiece.h"
Brian Carlstrom44753c32011-08-17 22:22:11 -070019#include "unordered_set.h"
Carl Shapirob5573532011-07-12 18:22:59 -070020
Carl Shapiro1fb86202011-06-27 17:43:13 -070021namespace art {
22
Brian Carlstrom16192862011-09-12 17:50:06 -070023template<class T> class PrimitiveArray;
24typedef PrimitiveArray<int8_t> ByteArray;
Carl Shapiro61e019d2011-07-14 16:53:09 -070025class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070026class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070027class Heap;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070028class InternTable;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070029class JavaVMExt;
Elliott Hughese27955c2011-08-26 15:21:24 -070030class SignalCatcher;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070031class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070032class ThreadList;
33
Carl Shapiro1fb86202011-06-27 17:43:13 -070034class Runtime {
35 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070036
37 typedef std::vector<std::pair<StringPiece, const void*> > Options;
38
39 class ParsedOptions {
40 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070041 // returns null if problem parsing and ignore_unrecognized is false
42 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070043
Elliott Hughes7ede61e2011-09-14 18:18:06 -070044 std::string boot_class_path_string_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070045 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070046 std::string class_path_string_;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070047 std::vector<const DexFile*> class_path_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070048 const char* boot_image_;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070049 std::vector<const char*> images_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070050 bool check_jni_;
Elliott Hughesa0957642011-09-02 14:27:33 -070051 std::string jni_trace_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070052 size_t heap_initial_size_;
53 size_t heap_maximum_size_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070054 size_t stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070055 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
56 void (*hook_exit_)(jint status);
57 void (*hook_abort_)();
Brian Carlstrom44753c32011-08-17 22:22:11 -070058 std::tr1::unordered_set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070059 std::vector<std::string> properties_;
60
Elliott Hughesa0957642011-09-02 14:27:33 -070061 bool IsVerbose(const std::string& key) const {
62 return verbose_.find(key) != verbose_.end();
63 }
64
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070065 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070066 ParsedOptions() {}
Brian Carlstrom8a436592011-08-15 21:27:23 -070067 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070068
Carl Shapiro61e019d2011-07-14 16:53:09 -070069 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070070 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070071
72 // Starts a runtime, which may cause threads to be started and code to run.
73 void Start();
Carl Shapiro2ed144c2011-07-26 16:52:08 -070074
Elliott Hughesdcc24742011-09-07 14:02:44 -070075 bool IsStarted();
76
Carl Shapiro2ed144c2011-07-26 16:52:08 -070077 static Runtime* Current() {
78 return instance_;
79 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070080
Carl Shapiro61e019d2011-07-14 16:53:09 -070081 // Compiles a dex file.
82 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070083
Elliott Hughesffe67362011-07-17 12:09:27 -070084 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
85 // callers should prefer.
86 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
87 // in a single function together. This reduces code size slightly, but means
88 // that the native stack trace we get may point at the wrong call site.
89 static void Abort(const char* file, int line);
90
Carl Shapiro61e019d2011-07-14 16:53:09 -070091 // Attaches the current native thread to the runtime.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070092 void AttachCurrentThread(const char* name, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -070093
Elliott Hughesbf86d042011-08-31 17:53:14 -070094 void CallExitHook(jint status);
95
Carl Shapiro61e019d2011-07-14 16:53:09 -070096 // Detaches the current native thread from the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -070097 void DetachCurrentThread();
Carl Shapiro61e019d2011-07-14 16:53:09 -070098
Elliott Hughes8daa0922011-09-11 13:46:25 -070099 void Dump(std::ostream& os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700100
Carl Shapiro61e019d2011-07-14 16:53:09 -0700101 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -0700102
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700103 const std::string& GetBootClassPath() const {
104 return boot_class_path_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700105 }
106
107 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700108 return class_linker_;
109 }
110
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700111 const std::string& GetClassPath() const {
112 return class_path_;
113 }
114
115 size_t GetDefaultStackSize() const {
116 return default_stack_size_;
117 }
118
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700119 InternTable* GetInternTable() const {
120 return intern_table_;
121 }
122
Elliott Hughes0af55432011-08-17 18:37:28 -0700123 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700124 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700125 }
126
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700127 const std::vector<std::string>& GetProperties() const {
128 return properties_;
129 }
130
Elliott Hughesd92bec42011-09-02 17:04:36 -0700131 ThreadList* GetThreadList() const {
132 return thread_list_;
133 }
134
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700135 const char* GetVersion() const {
136 return "2.0.0";
137 }
138
Elliott Hughes410c0c82011-09-01 17:58:25 -0700139 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700140
Brian Carlstrom16192862011-09-12 17:50:06 -0700141 bool HasJniStubArray() const {
142 return jni_stub_array_ != NULL;
143 }
144
145 ByteArray* GetJniStubArray() const {
146 CHECK(jni_stub_array_ != NULL);
147 return jni_stub_array_;
148 }
149
150 void SetJniStubArray(ByteArray* jni_stub_array) {
151 CHECK(jni_stub_array != NULL);
152 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array);
153 jni_stub_array_ = jni_stub_array;
154 }
155
Carl Shapirob5573532011-07-12 18:22:59 -0700156 private:
Elliott Hughesffe67362011-07-17 12:09:27 -0700157 static void PlatformAbort(const char*, int);
158
Elliott Hughesdcc24742011-09-07 14:02:44 -0700159 Runtime();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700160
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700161 void BlockSignals();
162
Brian Carlstrom8a436592011-08-15 21:27:23 -0700163 bool Init(const Options& options, bool ignore_unrecognized);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700164 void InitLibraries();
165 void RegisterRuntimeNativeMethods(JNIEnv*);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700166 void RunImageClinits();
Elliott Hughes85d15452011-09-16 17:33:01 -0700167 void StartDaemonThreads();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700168
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700169 std::string boot_class_path_;
170 std::string class_path_;
171 std::vector<std::string> properties_;
172
Brian Carlstromb765be02011-08-17 23:54:10 -0700173 // The default stack size for managed threads created by the runtime.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700174 size_t default_stack_size_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700175
Carl Shapirob5573532011-07-12 18:22:59 -0700176 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700177
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700178 InternTable* intern_table_;
179
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700180 ClassLinker* class_linker_;
181
Elliott Hughese27955c2011-08-26 15:21:24 -0700182 SignalCatcher* signal_catcher_;
183
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700184 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700185
Brian Carlstrom16192862011-09-12 17:50:06 -0700186 ByteArray* jni_stub_array_;
187
Elliott Hughesdcc24742011-09-07 14:02:44 -0700188 bool started_;
189
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700190 // Hooks supported by JNI_CreateJavaVM
191 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
192 void (*exit_)(jint status);
193 void (*abort_)();
194
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700195 // A pointer to the active runtime or NULL.
196 static Runtime* instance_;
197
Carl Shapiro61e019d2011-07-14 16:53:09 -0700198 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700199};
200
201} // namespace art
202
Carl Shapiro1fb86202011-06-27 17:43:13 -0700203#endif // ART_SRC_RUNTIME_H_