blob: ca0496c3c263f16945e8de409c356786fcb1e2b9 [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
Ian Rogersff1ed472011-09-20 13:46:24 -070015#include "constants.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070016#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "globals.h"
18#include "macros.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070019#include "runtime_stats.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "stringpiece.h"
Brian Carlstrom44753c32011-08-17 22:22:11 -070021#include "unordered_set.h"
Carl Shapirob5573532011-07-12 18:22:59 -070022
Carl Shapiro1fb86202011-06-27 17:43:13 -070023namespace art {
24
Brian Carlstrom16192862011-09-12 17:50:06 -070025template<class T> class PrimitiveArray;
26typedef PrimitiveArray<int8_t> ByteArray;
Carl Shapiro61e019d2011-07-14 16:53:09 -070027class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070028class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070029class Heap;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070030class InternTable;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070031class JavaVMExt;
Ian Rogersff1ed472011-09-20 13:46:24 -070032class Method;
Elliott Hughese27955c2011-08-26 15:21:24 -070033class SignalCatcher;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070034class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070035class ThreadList;
36
Carl Shapiro1fb86202011-06-27 17:43:13 -070037class Runtime {
38 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070039
40 typedef std::vector<std::pair<StringPiece, const void*> > Options;
41
42 class ParsedOptions {
43 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070044 // returns null if problem parsing and ignore_unrecognized is false
45 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070046
Elliott Hughes7ede61e2011-09-14 18:18:06 -070047 std::string boot_class_path_string_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070048 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070049 std::string class_path_string_;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070050 std::vector<const DexFile*> class_path_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070051 const char* boot_image_;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070052 std::vector<const char*> images_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070053 bool check_jni_;
Elliott Hughesa0957642011-09-02 14:27:33 -070054 std::string jni_trace_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070055 size_t heap_initial_size_;
56 size_t heap_maximum_size_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070057 size_t stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070058 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
59 void (*hook_exit_)(jint status);
60 void (*hook_abort_)();
Brian Carlstrom44753c32011-08-17 22:22:11 -070061 std::tr1::unordered_set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070062 std::vector<std::string> properties_;
63
Elliott Hughesa0957642011-09-02 14:27:33 -070064 bool IsVerbose(const std::string& key) const {
65 return verbose_.find(key) != verbose_.end();
66 }
67
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070068 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070069 ParsedOptions() {}
Brian Carlstrom8a436592011-08-15 21:27:23 -070070 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070071
Carl Shapiro61e019d2011-07-14 16:53:09 -070072 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070073 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070074
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070075 bool IsVerboseStartup() const {
76 return verbose_startup_;
77 }
78
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070079 // Starts a runtime, which may cause threads to be started and code to run.
80 void Start();
Carl Shapiro2ed144c2011-07-26 16:52:08 -070081
Elliott Hughesdcc24742011-09-07 14:02:44 -070082 bool IsStarted();
83
Carl Shapiro2ed144c2011-07-26 16:52:08 -070084 static Runtime* Current() {
85 return instance_;
86 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070087
Carl Shapiro61e019d2011-07-14 16:53:09 -070088 // Compiles a dex file.
89 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070090
Elliott Hughesffe67362011-07-17 12:09:27 -070091 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
92 // callers should prefer.
93 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
94 // in a single function together. This reduces code size slightly, but means
95 // that the native stack trace we get may point at the wrong call site.
96 static void Abort(const char* file, int line);
97
Carl Shapiro61e019d2011-07-14 16:53:09 -070098 // Attaches the current native thread to the runtime.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070099 void AttachCurrentThread(const char* name, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700100
Elliott Hughesbf86d042011-08-31 17:53:14 -0700101 void CallExitHook(jint status);
102
Carl Shapiro61e019d2011-07-14 16:53:09 -0700103 // Detaches the current native thread from the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -0700104 void DetachCurrentThread();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700105
Elliott Hughes8daa0922011-09-11 13:46:25 -0700106 void Dump(std::ostream& os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700107
Carl Shapiro61e019d2011-07-14 16:53:09 -0700108 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -0700109
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700110 const std::string& GetBootClassPath() const {
111 return boot_class_path_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700112 }
113
114 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700115 return class_linker_;
116 }
117
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700118 const std::string& GetClassPath() const {
119 return class_path_;
120 }
121
122 size_t GetDefaultStackSize() const {
123 return default_stack_size_;
124 }
125
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700126 InternTable* GetInternTable() const {
127 return intern_table_;
128 }
129
Elliott Hughes0af55432011-08-17 18:37:28 -0700130 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700131 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700132 }
133
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700134 const std::vector<std::string>& GetProperties() const {
135 return properties_;
136 }
137
Elliott Hughesd92bec42011-09-02 17:04:36 -0700138 ThreadList* GetThreadList() const {
139 return thread_list_;
140 }
141
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700142 const char* GetVersion() const {
143 return "2.0.0";
144 }
145
Elliott Hughes410c0c82011-09-01 17:58:25 -0700146 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700147
Brian Carlstrom16192862011-09-12 17:50:06 -0700148 bool HasJniStubArray() const {
149 return jni_stub_array_ != NULL;
150 }
151
152 ByteArray* GetJniStubArray() const {
153 CHECK(jni_stub_array_ != NULL);
154 return jni_stub_array_;
155 }
156
157 void SetJniStubArray(ByteArray* jni_stub_array) {
158 CHECK(jni_stub_array != NULL);
159 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array);
160 jni_stub_array_ = jni_stub_array;
161 }
162
Ian Rogersff1ed472011-09-20 13:46:24 -0700163 Method* CreateCalleeSaveMethod(InstructionSet insns);
164
165 bool HasCalleeSaveMethod() const {
166 return callee_save_method_ != NULL;
167 }
168
169 // Returns a special method that describes all callee saves being spilled to the stack.
170 Method* GetCalleeSaveMethod() const {
171 return callee_save_method_;
172 }
173
174 void SetCalleeSaveMethod(Method* method) {
175 callee_save_method_ = method;
176 }
177
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700178 int32_t GetStat(int kind);
179
180 RuntimeStats* GetStats();
181
182 bool HasStatsEnabled() const {
183 return stats_enabled_;
184 }
185
186 void ResetStats(int kinds);
187
188 void SetStatsEnabled(bool new_state);
189
Carl Shapirob5573532011-07-12 18:22:59 -0700190 private:
Elliott Hughesffe67362011-07-17 12:09:27 -0700191 static void PlatformAbort(const char*, int);
192
Elliott Hughesdcc24742011-09-07 14:02:44 -0700193 Runtime();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700194
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700195 void BlockSignals();
196
Brian Carlstrom8a436592011-08-15 21:27:23 -0700197 bool Init(const Options& options, bool ignore_unrecognized);
Elliott Hughes038a8062011-09-18 14:12:41 -0700198 void InitNativeMethods();
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700199 void RegisterRuntimeNativeMethods(JNIEnv*);
Elliott Hughes85d15452011-09-16 17:33:01 -0700200 void StartDaemonThreads();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700201
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700202 bool verbose_startup_;
203
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700204 std::string boot_class_path_;
205 std::string class_path_;
206 std::vector<std::string> properties_;
207
Brian Carlstromb765be02011-08-17 23:54:10 -0700208 // The default stack size for managed threads created by the runtime.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700209 size_t default_stack_size_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700210
Carl Shapirob5573532011-07-12 18:22:59 -0700211 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700212
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700213 InternTable* intern_table_;
214
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700215 ClassLinker* class_linker_;
216
Elliott Hughese27955c2011-08-26 15:21:24 -0700217 SignalCatcher* signal_catcher_;
218
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700219 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700220
Brian Carlstrom16192862011-09-12 17:50:06 -0700221 ByteArray* jni_stub_array_;
222
Ian Rogersff1ed472011-09-20 13:46:24 -0700223 Method* callee_save_method_;
224
Elliott Hughesdcc24742011-09-07 14:02:44 -0700225 bool started_;
226
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700227 // Hooks supported by JNI_CreateJavaVM
228 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
229 void (*exit_)(jint status);
230 void (*abort_)();
231
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700232 bool stats_enabled_;
233 RuntimeStats stats_;
234
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700235 // A pointer to the active runtime or NULL.
236 static Runtime* instance_;
237
Carl Shapiro61e019d2011-07-14 16:53:09 -0700238 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700239};
240
241} // namespace art
242
Carl Shapiro1fb86202011-06-27 17:43:13 -0700243#endif // ART_SRC_RUNTIME_H_