blob: 913779a5497569b26bd8218f003eb46af9d4ce62 [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"
Carl Shapirob5573532011-07-12 18:22:59 -070021
Carl Shapiro1fb86202011-06-27 17:43:13 -070022namespace art {
23
Brian Carlstrom16192862011-09-12 17:50:06 -070024template<class T> class PrimitiveArray;
25typedef PrimitiveArray<int8_t> ByteArray;
Carl Shapiro61e019d2011-07-14 16:53:09 -070026class ClassLinker;
Brian Carlstromaded5f72011-10-07 17:15:04 -070027class ClassLoader;
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 Hughesc33a32b2011-10-11 18:18:07 -070033class MonitorList;
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;
jeffhao2692b572011-12-16 15:42:28 -080037class Trace;
Carl Shapiro61e019d2011-07-14 16:53:09 -070038
Carl Shapiro1fb86202011-06-27 17:43:13 -070039class Runtime {
40 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070041
42 typedef std::vector<std::pair<StringPiece, const void*> > Options;
43
44 class ParsedOptions {
45 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070046 // returns null if problem parsing and ignore_unrecognized is false
47 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070048
Brian Carlstrom58ae9412011-10-04 00:56:06 -070049 std::string boot_class_path_;
50 std::string class_path_;
51 std::string host_prefix_;
52 std::vector<std::string> images_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070053 bool check_jni_;
Elliott Hughesa0957642011-09-02 14:27:33 -070054 std::string jni_trace_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -070055 bool is_zygote_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070056 size_t heap_initial_size_;
57 size_t heap_maximum_size_;
jeffhaoc1160702011-10-27 15:48:45 -070058 size_t heap_growth_limit_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070059 size_t stack_size_;
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070060 size_t jni_globals_max_;
61 size_t lock_profiling_threshold_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -070062 std::string stack_trace_file_;
Elliott Hughesfc861622011-10-17 17:57:47 -070063 bool (*hook_is_sensitive_thread_)();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070064 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
65 void (*hook_exit_)(jint status);
66 void (*hook_abort_)();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070067 std::vector<std::string> properties_;
68
69 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070070 ParsedOptions() {}
Brian Carlstrom8a436592011-08-15 21:27:23 -070071 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070072
Carl Shapiro61e019d2011-07-14 16:53:09 -070073 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070074 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070075
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070076 bool IsZygote() const {
77 return is_zygote_;
78 }
79
Brian Carlstrom58ae9412011-10-04 00:56:06 -070080 const std::string& GetHostPrefix() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -070081 DCHECK(!IsStarted());
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 return host_prefix_;
83 }
84
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070085 // Starts a runtime, which may cause threads to be started and code to run.
86 void Start();
Carl Shapiro2ed144c2011-07-26 16:52:08 -070087
Elliott Hughes6b355752012-01-13 16:49:08 -080088 bool IsShuttingDown() const;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070089 bool IsStarted() const;
Elliott Hughesdcc24742011-09-07 14:02:44 -070090
Carl Shapiro2ed144c2011-07-26 16:52:08 -070091 static Runtime* Current() {
92 return instance_;
93 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070094
Carl Shapiro61e019d2011-07-14 16:53:09 -070095 // Compiles a dex file.
96 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070097
Elliott Hughesffe67362011-07-17 12:09:27 -070098 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
99 // callers should prefer.
100 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
101 // in a single function together. This reduces code size slightly, but means
102 // that the native stack trace we get may point at the wrong call site.
103 static void Abort(const char* file, int line);
104
Carl Shapiro61e019d2011-07-14 16:53:09 -0700105 // Attaches the current native thread to the runtime.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700106 void AttachCurrentThread(const char* name, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700107
Elliott Hughesbf86d042011-08-31 17:53:14 -0700108 void CallExitHook(jint status);
109
Carl Shapiro61e019d2011-07-14 16:53:09 -0700110 // Detaches the current native thread from the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -0700111 void DetachCurrentThread();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700112
Elliott Hughes8daa0922011-09-11 13:46:25 -0700113 void Dump(std::ostream& os);
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800114 void DumpLockHolders(std::ostream& os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700115
Carl Shapiro61e019d2011-07-14 16:53:09 -0700116 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -0700117
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700118 const std::string& GetBootClassPath() const {
119 return boot_class_path_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700120 }
121
122 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700123 return class_linker_;
124 }
125
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700126 const std::string& GetClassPath() const {
127 return class_path_;
128 }
129
130 size_t GetDefaultStackSize() const {
131 return default_stack_size_;
132 }
133
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700134 InternTable* GetInternTable() const {
135 return intern_table_;
136 }
137
Elliott Hughes0af55432011-08-17 18:37:28 -0700138 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700139 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700140 }
141
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700142 const std::vector<std::string>& GetProperties() const {
143 return properties_;
144 }
145
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700146 MonitorList* GetMonitorList() const {
147 return monitor_list_;
148 }
149
Elliott Hughesd92bec42011-09-02 17:04:36 -0700150 ThreadList* GetThreadList() const {
151 return thread_list_;
152 }
153
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700154 const char* GetVersion() const {
155 return "2.0.0";
156 }
157
Elliott Hughes410c0c82011-09-01 17:58:25 -0700158 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700159
Ian Rogers169c9a72011-11-13 20:13:17 -0800160 bool HasJniDlsymLookupStub() const;
161 ByteArray* GetJniDlsymLookupStub() const;
162 void SetJniDlsymLookupStub(ByteArray* jni_stub_array);
Brian Carlstrom16192862011-09-12 17:50:06 -0700163
Brian Carlstrome24fa612011-09-29 00:53:55 -0700164 bool HasAbstractMethodErrorStubArray() const;
165 ByteArray* GetAbstractMethodErrorStubArray() const;
166 void SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array);
Ian Rogersff1ed472011-09-20 13:46:24 -0700167
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700168 enum TrampolineType {
169 kInstanceMethod,
170 kStaticMethod,
171 kUnknownMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700172 kLastTrampolineMethodType // Value used for iteration
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700173 };
174 static TrampolineType GetTrampolineType(Method* method);
175 bool HasResolutionStubArray(TrampolineType type) const;
176 ByteArray* GetResolutionStubArray(TrampolineType type) const;
177 void SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700178
Ian Rogersff1ed472011-09-20 13:46:24 -0700179 // Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700180 enum CalleeSaveType {
181 kSaveAll,
182 kRefsOnly,
183 kRefsAndArgs,
184 kLastCalleeSaveType // Value used for iteration
185 };
Brian Carlstromae826982011-11-09 01:33:42 -0800186 Method* CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700187 bool HasCalleeSaveMethod(CalleeSaveType type) const;
188 Method* GetCalleeSaveMethod(CalleeSaveType type) const;
189 void SetCalleeSaveMethod(Method* method, CalleeSaveType type);
190
Brian Carlstromae826982011-11-09 01:33:42 -0800191 Method* CreateRefOnlyCalleeSaveMethod(InstructionSet instruction_set);
192 Method* CreateRefAndArgsCalleeSaveMethod(InstructionSet instruction_set);
Ian Rogersff1ed472011-09-20 13:46:24 -0700193
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700194 int32_t GetStat(int kind);
195
196 RuntimeStats* GetStats();
197
198 bool HasStatsEnabled() const {
199 return stats_enabled_;
200 }
201
202 void ResetStats(int kinds);
203
204 void SetStatsEnabled(bool new_state);
205
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700206 void DidForkFromZygote();
207
jeffhao2692b572011-12-16 15:42:28 -0800208 void EnableMethodTracing(Trace* tracer);
209 void DisableMethodTracing();
210 bool IsMethodTracingActive() const;
211 Trace* GetTracer() const;
212
Carl Shapirob5573532011-07-12 18:22:59 -0700213 private:
Elliott Hughesffe67362011-07-17 12:09:27 -0700214 static void PlatformAbort(const char*, int);
215
Elliott Hughesdcc24742011-09-07 14:02:44 -0700216 Runtime();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700217
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700218 void BlockSignals();
219
Brian Carlstrom8a436592011-08-15 21:27:23 -0700220 bool Init(const Options& options, bool ignore_unrecognized);
Elliott Hughes038a8062011-09-18 14:12:41 -0700221 void InitNativeMethods();
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700222 void RegisterRuntimeNativeMethods(JNIEnv*);
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700223
Elliott Hughes85d15452011-09-16 17:33:01 -0700224 void StartDaemonThreads();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700225 void StartSignalCatcher();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700226
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700227 bool is_zygote_;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700228
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700229 // The host prefix is used during cross compilation. It is removed
230 // from the start of host paths such as:
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800231 // $ANDROID_PRODUCT_OUT/system/framework/boot.oat
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700232 // to produce target paths such as
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -0700233 // /system/framework/boot.oat
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700234 // Similarly it is prepended to target paths to arrive back at a
235 // host past. In both cases this is necessary because image and oat
236 // files embedded expect paths of dependent files (an image points
237 // to an oat file and an oat files to one or more dex files). These
238 // files contain the expected target path.
239 std::string host_prefix_;
240
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700241 std::string boot_class_path_;
242 std::string class_path_;
243 std::vector<std::string> properties_;
244
Brian Carlstromb765be02011-08-17 23:54:10 -0700245 // The default stack size for managed threads created by the runtime.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700246 size_t default_stack_size_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700247
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700248 MonitorList* monitor_list_;
249
Carl Shapirob5573532011-07-12 18:22:59 -0700250 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700251
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700252 InternTable* intern_table_;
253
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700254 ClassLinker* class_linker_;
255
Elliott Hughese27955c2011-08-26 15:21:24 -0700256 SignalCatcher* signal_catcher_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700257 std::string stack_trace_file_;
Elliott Hughese27955c2011-08-26 15:21:24 -0700258
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700259 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700260
Brian Carlstrom16192862011-09-12 17:50:06 -0700261 ByteArray* jni_stub_array_;
262
Brian Carlstrome24fa612011-09-29 00:53:55 -0700263 ByteArray* abstract_method_error_stub_array_;
264
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700265 ByteArray* resolution_stub_array_[kLastTrampolineMethodType];
Ian Rogersad25ac52011-10-04 19:13:33 -0700266
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700267 Method* callee_save_method_[kLastCalleeSaveType];
Ian Rogersff1ed472011-09-20 13:46:24 -0700268
Brian Carlstromaded5f72011-10-07 17:15:04 -0700269 // As returned by ClassLoader.getSystemClassLoader()
270 ClassLoader* system_class_loader_;
271
Elliott Hughes6b355752012-01-13 16:49:08 -0800272 bool shutting_down_;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700273 bool started_;
274
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700275 // Hooks supported by JNI_CreateJavaVM
276 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
277 void (*exit_)(jint status);
278 void (*abort_)();
279
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700280 bool stats_enabled_;
281 RuntimeStats stats_;
282
jeffhao2692b572011-12-16 15:42:28 -0800283 Trace* tracer_;
284
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700285 // A pointer to the active runtime or NULL.
286 static Runtime* instance_;
287
Carl Shapiro61e019d2011-07-14 16:53:09 -0700288 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700289};
290
291} // namespace art
292
Carl Shapiro1fb86202011-06-27 17:43:13 -0700293#endif // ART_SRC_RUNTIME_H_