blob: 462bb81dd3a61ac67c84dab5bf3c81a9dd5ce029 [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;
Brian Carlstromaded5f72011-10-07 17:15:04 -070028class ClassLoader;
Carl Shapirofc322c72011-07-27 00:20:01 -070029class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070030class Heap;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070031class InternTable;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070032class JavaVMExt;
Ian Rogersff1ed472011-09-20 13:46:24 -070033class Method;
Elliott Hughese27955c2011-08-26 15:21:24 -070034class SignalCatcher;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070035class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070036class ThreadList;
37
Carl Shapiro1fb86202011-06-27 17:43:13 -070038class Runtime {
39 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070040
41 typedef std::vector<std::pair<StringPiece, const void*> > Options;
42
43 class ParsedOptions {
44 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070045 // returns null if problem parsing and ignore_unrecognized is false
46 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070047
Brian Carlstrom58ae9412011-10-04 00:56:06 -070048 std::string boot_class_path_;
49 std::string class_path_;
50 std::string host_prefix_;
51 std::vector<std::string> images_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070052 bool check_jni_;
Elliott Hughesa0957642011-09-02 14:27:33 -070053 std::string jni_trace_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -070054 bool is_zygote_;
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 Carlstrom58ae9412011-10-04 00:56:06 -070079 const std::string& GetHostPrefix() const {
80 CHECK(!IsStarted());
81 return host_prefix_;
82 }
83
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070084 // Starts a runtime, which may cause threads to be started and code to run.
85 void Start();
Carl Shapiro2ed144c2011-07-26 16:52:08 -070086
Brian Carlstrom58ae9412011-10-04 00:56:06 -070087 bool IsStarted() const;
Elliott Hughesdcc24742011-09-07 14:02:44 -070088
Carl Shapiro2ed144c2011-07-26 16:52:08 -070089 static Runtime* Current() {
90 return instance_;
91 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070092
Carl Shapiro61e019d2011-07-14 16:53:09 -070093 // Compiles a dex file.
94 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070095
Elliott Hughesffe67362011-07-17 12:09:27 -070096 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
97 // callers should prefer.
98 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
99 // in a single function together. This reduces code size slightly, but means
100 // that the native stack trace we get may point at the wrong call site.
101 static void Abort(const char* file, int line);
102
Carl Shapiro61e019d2011-07-14 16:53:09 -0700103 // Attaches the current native thread to the runtime.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700104 void AttachCurrentThread(const char* name, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700105
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106 void CallExitHook(jint status);
107
Carl Shapiro61e019d2011-07-14 16:53:09 -0700108 // Detaches the current native thread from the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -0700109 void DetachCurrentThread();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700110
Elliott Hughes8daa0922011-09-11 13:46:25 -0700111 void Dump(std::ostream& os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700112
Carl Shapiro61e019d2011-07-14 16:53:09 -0700113 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -0700114
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700115 const std::string& GetBootClassPath() const {
116 return boot_class_path_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700117 }
118
119 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700120 return class_linker_;
121 }
122
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700123 const std::string& GetClassPath() const {
124 return class_path_;
125 }
126
127 size_t GetDefaultStackSize() const {
128 return default_stack_size_;
129 }
130
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700131 InternTable* GetInternTable() const {
132 return intern_table_;
133 }
134
Elliott Hughes0af55432011-08-17 18:37:28 -0700135 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700136 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700137 }
138
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700139 const std::vector<std::string>& GetProperties() const {
140 return properties_;
141 }
142
Elliott Hughesd92bec42011-09-02 17:04:36 -0700143 ThreadList* GetThreadList() const {
144 return thread_list_;
145 }
146
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700147 const char* GetVersion() const {
148 return "2.0.0";
149 }
150
Elliott Hughes410c0c82011-09-01 17:58:25 -0700151 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700152
Brian Carlstrome24fa612011-09-29 00:53:55 -0700153 bool HasJniStubArray() const;
154 ByteArray* GetJniStubArray() const;
155 void SetJniStubArray(ByteArray* jni_stub_array);
Brian Carlstrom16192862011-09-12 17:50:06 -0700156
Brian Carlstrome24fa612011-09-29 00:53:55 -0700157 bool HasAbstractMethodErrorStubArray() const;
158 ByteArray* GetAbstractMethodErrorStubArray() const;
159 void SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array);
Ian Rogersff1ed472011-09-20 13:46:24 -0700160
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700161 enum TrampolineType {
162 kInstanceMethod,
163 kStaticMethod,
164 kUnknownMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700165 kLastTrampolineMethodType // Value used for iteration
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700166 };
167 static TrampolineType GetTrampolineType(Method* method);
168 bool HasResolutionStubArray(TrampolineType type) const;
169 ByteArray* GetResolutionStubArray(TrampolineType type) const;
170 void SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700171
Ian Rogersff1ed472011-09-20 13:46:24 -0700172 // Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700173 enum CalleeSaveType {
174 kSaveAll,
175 kRefsOnly,
176 kRefsAndArgs,
177 kLastCalleeSaveType // Value used for iteration
178 };
179 Method* CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type);
180 bool HasCalleeSaveMethod(CalleeSaveType type) const;
181 Method* GetCalleeSaveMethod(CalleeSaveType type) const;
182 void SetCalleeSaveMethod(Method* method, CalleeSaveType type);
183
184 Method* CreateRefOnlyCalleeSaveMethod(InstructionSet insns);
185 Method* CreateRefAndArgsCalleeSaveMethod(InstructionSet insns);
Ian Rogersff1ed472011-09-20 13:46:24 -0700186
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700187 int32_t GetStat(int kind);
188
189 RuntimeStats* GetStats();
190
191 bool HasStatsEnabled() const {
192 return stats_enabled_;
193 }
194
195 void ResetStats(int kinds);
196
197 void SetStatsEnabled(bool new_state);
198
Carl Shapirob5573532011-07-12 18:22:59 -0700199 private:
Elliott Hughesffe67362011-07-17 12:09:27 -0700200 static void PlatformAbort(const char*, int);
201
Elliott Hughesdcc24742011-09-07 14:02:44 -0700202 Runtime();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700203
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700204 void BlockSignals();
205
Brian Carlstrom8a436592011-08-15 21:27:23 -0700206 bool Init(const Options& options, bool ignore_unrecognized);
Elliott Hughes038a8062011-09-18 14:12:41 -0700207 void InitNativeMethods();
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700208 void RegisterRuntimeNativeMethods(JNIEnv*);
Elliott Hughes85d15452011-09-16 17:33:01 -0700209 void StartDaemonThreads();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700210
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700211 bool verbose_startup_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700212 bool is_zygote_;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700213
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700214 // The host prefix is used during cross compilation. It is removed
215 // from the start of host paths such as:
216 // $ANDROID_PRODUCT_OUT/system/framework/core.oat
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700217 // to produce target paths such as
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700218 // /system/framework/core.oat
219 // Similarly it is prepended to target paths to arrive back at a
220 // host past. In both cases this is necessary because image and oat
221 // files embedded expect paths of dependent files (an image points
222 // to an oat file and an oat files to one or more dex files). These
223 // files contain the expected target path.
224 std::string host_prefix_;
225
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700226 std::string boot_class_path_;
227 std::string class_path_;
228 std::vector<std::string> properties_;
229
Brian Carlstromb765be02011-08-17 23:54:10 -0700230 // The default stack size for managed threads created by the runtime.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700231 size_t default_stack_size_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700232
Carl Shapirob5573532011-07-12 18:22:59 -0700233 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700234
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700235 InternTable* intern_table_;
236
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700237 ClassLinker* class_linker_;
238
Elliott Hughese27955c2011-08-26 15:21:24 -0700239 SignalCatcher* signal_catcher_;
240
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700241 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700242
Brian Carlstrom16192862011-09-12 17:50:06 -0700243 ByteArray* jni_stub_array_;
244
Brian Carlstrome24fa612011-09-29 00:53:55 -0700245 ByteArray* abstract_method_error_stub_array_;
246
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700247 ByteArray* resolution_stub_array_[kLastTrampolineMethodType];
Ian Rogersad25ac52011-10-04 19:13:33 -0700248
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700249 Method* callee_save_method_[kLastCalleeSaveType];
Ian Rogersff1ed472011-09-20 13:46:24 -0700250
Brian Carlstromaded5f72011-10-07 17:15:04 -0700251 // As returned by ClassLoader.getSystemClassLoader()
252 ClassLoader* system_class_loader_;
253
Elliott Hughesdcc24742011-09-07 14:02:44 -0700254 bool started_;
255
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700256 // Hooks supported by JNI_CreateJavaVM
257 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
258 void (*exit_)(jint status);
259 void (*abort_)();
260
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700261 bool stats_enabled_;
262 RuntimeStats stats_;
263
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700264 // A pointer to the active runtime or NULL.
265 static Runtime* instance_;
266
Carl Shapiro61e019d2011-07-14 16:53:09 -0700267 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700268};
269
270} // namespace art
271
Carl Shapiro1fb86202011-06-27 17:43:13 -0700272#endif // ART_SRC_RUNTIME_H_