blob: a2ec08ed9e3527700e2a12cab3974121ca92dd85 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_RUNTIME_H_
18#define ART_SRC_RUNTIME_H_
19
Elliott Hughesc5f7c912011-08-18 14:00:42 -070020#include <stdio.h>
21
Elliott Hughese27955c2011-08-26 15:21:24 -070022#include <iosfwd>
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080023#include <map>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070024#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -070025#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070026#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027
Elliott Hughesc5f7c912011-08-18 14:00:42 -070028#include <jni.h>
29
Ian Rogersff1ed472011-09-20 13:46:24 -070030#include "constants.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070031#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "globals.h"
33#include "macros.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070034#include "runtime_stats.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035#include "stringpiece.h"
Carl Shapirob5573532011-07-12 18:22:59 -070036
Carl Shapiro1fb86202011-06-27 17:43:13 -070037namespace art {
38
Brian Carlstrom16192862011-09-12 17:50:06 -070039template<class T> class PrimitiveArray;
40typedef PrimitiveArray<int8_t> ByteArray;
Carl Shapiro61e019d2011-07-14 16:53:09 -070041class ClassLinker;
Brian Carlstromaded5f72011-10-07 17:15:04 -070042class ClassLoader;
Carl Shapirofc322c72011-07-27 00:20:01 -070043class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070044class Heap;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070045class InternTable;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070046class JavaVMExt;
Ian Rogersff1ed472011-09-20 13:46:24 -070047class Method;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070048class MonitorList;
Elliott Hughese27955c2011-08-26 15:21:24 -070049class SignalCatcher;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070050class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070051class ThreadList;
jeffhao2692b572011-12-16 15:42:28 -080052class Trace;
Carl Shapiro61e019d2011-07-14 16:53:09 -070053
Carl Shapiro1fb86202011-06-27 17:43:13 -070054class Runtime {
55 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070056
Elliott Hughesf1a5adc2012-02-10 18:09:35 -080057 typedef std::vector<std::pair<std::string, const void*> > Options;
Brian Carlstrom8a436592011-08-15 21:27:23 -070058
59 class ParsedOptions {
60 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070061 // returns null if problem parsing and ignore_unrecognized is false
62 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070063
Brian Carlstroma004aa92012-02-08 18:05:09 -080064 const std::vector<const DexFile*>* boot_class_path_;
65 std::string boot_class_path_string_;
66 std::string class_path_string_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070067 std::string host_prefix_;
Brian Carlstrom223f20f2012-02-04 23:06:55 -080068 std::string image_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070069 bool check_jni_;
Elliott Hughesa0957642011-09-02 14:27:33 -070070 std::string jni_trace_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -080071 bool is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -070072 bool is_zygote_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070073 size_t heap_initial_size_;
74 size_t heap_maximum_size_;
jeffhaoc1160702011-10-27 15:48:45 -070075 size_t heap_growth_limit_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070076 size_t stack_size_;
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070077 size_t jni_globals_max_;
78 size_t lock_profiling_threshold_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -070079 std::string stack_trace_file_;
Elliott Hughesfc861622011-10-17 17:57:47 -070080 bool (*hook_is_sensitive_thread_)();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070081 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
82 void (*hook_exit_)(jint status);
83 void (*hook_abort_)();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070084 std::vector<std::string> properties_;
85
86 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070087 ParsedOptions() {}
Brian Carlstrom8a436592011-08-15 21:27:23 -070088 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070089
Carl Shapiro61e019d2011-07-14 16:53:09 -070090 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070091 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070092
Elliott Hughesd9c67be2012-02-02 19:54:06 -080093 bool IsCompiler() const {
94 return is_compiler_;
95 }
96
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070097 bool IsZygote() const {
98 return is_zygote_;
99 }
100
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700101 const std::string& GetHostPrefix() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700102 DCHECK(!IsStarted());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700103 return host_prefix_;
104 }
105
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700106 // Starts a runtime, which may cause threads to be started and code to run.
107 void Start();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700108
Elliott Hughes6b355752012-01-13 16:49:08 -0800109 bool IsShuttingDown() const;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700110 bool IsStarted() const;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700111
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700112 static Runtime* Current() {
113 return instance_;
114 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700115
Elliott Hughesffe67362011-07-17 12:09:27 -0700116 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
117 // callers should prefer.
118 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
119 // in a single function together. This reduces code size slightly, but means
120 // that the native stack trace we get may point at the wrong call site.
121 static void Abort(const char* file, int line);
122
Carl Shapiro61e019d2011-07-14 16:53:09 -0700123 // Attaches the current native thread to the runtime.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700124 void AttachCurrentThread(const char* name, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700125
Elliott Hughesbf86d042011-08-31 17:53:14 -0700126 void CallExitHook(jint status);
127
Carl Shapiro61e019d2011-07-14 16:53:09 -0700128 // Detaches the current native thread from the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -0700129 void DetachCurrentThread();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700130
Elliott Hughes8daa0922011-09-11 13:46:25 -0700131 void Dump(std::ostream& os);
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800132 void DumpLockHolders(std::ostream& os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700133
Carl Shapiro61e019d2011-07-14 16:53:09 -0700134 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -0700135
Brian Carlstroma004aa92012-02-08 18:05:09 -0800136 const std::string& GetBootClassPathString() const {
137 return boot_class_path_string_;
138 }
139
140 const std::string& GetClassPathString() const {
141 return class_path_string_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700142 }
143
144 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700145 return class_linker_;
146 }
147
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700148 size_t GetDefaultStackSize() const {
149 return default_stack_size_;
150 }
151
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800152 Heap* GetHeap() const {
153 return heap_;
154 }
155
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700156 InternTable* GetInternTable() const {
157 return intern_table_;
158 }
159
Elliott Hughes0af55432011-08-17 18:37:28 -0700160 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700161 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700162 }
163
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700164 const std::vector<std::string>& GetProperties() const {
165 return properties_;
166 }
167
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700168 MonitorList* GetMonitorList() const {
169 return monitor_list_;
170 }
171
Elliott Hughesd92bec42011-09-02 17:04:36 -0700172 ThreadList* GetThreadList() const {
173 return thread_list_;
174 }
175
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700176 const char* GetVersion() const {
177 return "2.0.0";
178 }
179
Elliott Hughes410c0c82011-09-01 17:58:25 -0700180 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700181
Ian Rogers169c9a72011-11-13 20:13:17 -0800182 bool HasJniDlsymLookupStub() const;
183 ByteArray* GetJniDlsymLookupStub() const;
184 void SetJniDlsymLookupStub(ByteArray* jni_stub_array);
Brian Carlstrom16192862011-09-12 17:50:06 -0700185
Brian Carlstrome24fa612011-09-29 00:53:55 -0700186 bool HasAbstractMethodErrorStubArray() const;
187 ByteArray* GetAbstractMethodErrorStubArray() const;
188 void SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array);
Ian Rogersff1ed472011-09-20 13:46:24 -0700189
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700190 enum TrampolineType {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700191 kStaticMethod,
192 kUnknownMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700193 kLastTrampolineMethodType // Value used for iteration
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700194 };
195 static TrampolineType GetTrampolineType(Method* method);
196 bool HasResolutionStubArray(TrampolineType type) const;
197 ByteArray* GetResolutionStubArray(TrampolineType type) const;
198 void SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700199
Ian Rogers19846512012-02-24 11:42:47 -0800200 // Returns a special method to trampoline into runtime resolution
201 Method* CreateResolutionMethod();
202 bool HasResolutionMethod() const;
203 Method* GetResolutionMethod() const;
204 void SetResolutionMethod(Method* method);
205
Ian Rogersff1ed472011-09-20 13:46:24 -0700206 // Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700207 enum CalleeSaveType {
208 kSaveAll,
209 kRefsOnly,
210 kRefsAndArgs,
211 kLastCalleeSaveType // Value used for iteration
212 };
Brian Carlstromae826982011-11-09 01:33:42 -0800213 Method* CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700214 bool HasCalleeSaveMethod(CalleeSaveType type) const;
215 Method* GetCalleeSaveMethod(CalleeSaveType type) const;
216 void SetCalleeSaveMethod(Method* method, CalleeSaveType type);
217
Brian Carlstromae826982011-11-09 01:33:42 -0800218 Method* CreateRefOnlyCalleeSaveMethod(InstructionSet instruction_set);
219 Method* CreateRefAndArgsCalleeSaveMethod(InstructionSet instruction_set);
Ian Rogersff1ed472011-09-20 13:46:24 -0700220
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700221 int32_t GetStat(int kind);
222
223 RuntimeStats* GetStats();
224
225 bool HasStatsEnabled() const {
226 return stats_enabled_;
227 }
228
229 void ResetStats(int kinds);
230
231 void SetStatsEnabled(bool new_state);
232
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700233 void DidForkFromZygote();
234
jeffhao2692b572011-12-16 15:42:28 -0800235 void EnableMethodTracing(Trace* tracer);
236 void DisableMethodTracing();
237 bool IsMethodTracingActive() const;
238 Trace* GetTracer() const;
239
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800240 bool UseCompileTimeClassPath() const {
241 return use_compile_time_class_path_;
242 }
243 const std::vector<const DexFile*>& GetCompileTimeClassPath(const ClassLoader* class_loader);
244 void SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path);
245
Carl Shapirob5573532011-07-12 18:22:59 -0700246 private:
Elliott Hughesffe67362011-07-17 12:09:27 -0700247 static void PlatformAbort(const char*, int);
248
Elliott Hughesdcc24742011-09-07 14:02:44 -0700249 Runtime();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700250
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700251 void BlockSignals();
252
Brian Carlstrom8a436592011-08-15 21:27:23 -0700253 bool Init(const Options& options, bool ignore_unrecognized);
Elliott Hughes038a8062011-09-18 14:12:41 -0700254 void InitNativeMethods();
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800255 void RegisterRuntimeNativeMethods(JNIEnv* env);
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700256
Elliott Hughes85d15452011-09-16 17:33:01 -0700257 void StartDaemonThreads();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700258 void StartSignalCatcher();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700259
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800260 // A pointer to the active runtime or NULL.
261 static Runtime* instance_;
262
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800263 bool is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700264 bool is_zygote_;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700265
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700266 // The host prefix is used during cross compilation. It is removed
267 // from the start of host paths such as:
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800268 // $ANDROID_PRODUCT_OUT/system/framework/boot.oat
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700269 // to produce target paths such as
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -0700270 // /system/framework/boot.oat
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700271 // Similarly it is prepended to target paths to arrive back at a
272 // host past. In both cases this is necessary because image and oat
273 // files embedded expect paths of dependent files (an image points
274 // to an oat file and an oat files to one or more dex files). These
275 // files contain the expected target path.
276 std::string host_prefix_;
277
Brian Carlstroma004aa92012-02-08 18:05:09 -0800278 std::string boot_class_path_string_;
279 std::string class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700280 std::vector<std::string> properties_;
281
Brian Carlstromb765be02011-08-17 23:54:10 -0700282 // The default stack size for managed threads created by the runtime.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700283 size_t default_stack_size_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700284
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800285 Heap* heap_;
286
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700287 MonitorList* monitor_list_;
288
Carl Shapirob5573532011-07-12 18:22:59 -0700289 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700290
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700291 InternTable* intern_table_;
292
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700293 ClassLinker* class_linker_;
294
Elliott Hughese27955c2011-08-26 15:21:24 -0700295 SignalCatcher* signal_catcher_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700296 std::string stack_trace_file_;
Elliott Hughese27955c2011-08-26 15:21:24 -0700297
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700298 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700299
Brian Carlstrom16192862011-09-12 17:50:06 -0700300 ByteArray* jni_stub_array_;
301
Brian Carlstrome24fa612011-09-29 00:53:55 -0700302 ByteArray* abstract_method_error_stub_array_;
303
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700304 ByteArray* resolution_stub_array_[kLastTrampolineMethodType];
Ian Rogersad25ac52011-10-04 19:13:33 -0700305
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700306 Method* callee_save_method_[kLastCalleeSaveType];
Ian Rogersff1ed472011-09-20 13:46:24 -0700307
Ian Rogers19846512012-02-24 11:42:47 -0800308 Method* resolution_method_;
309
Brian Carlstromaded5f72011-10-07 17:15:04 -0700310 // As returned by ClassLoader.getSystemClassLoader()
311 ClassLoader* system_class_loader_;
312
Elliott Hughes6b355752012-01-13 16:49:08 -0800313 bool shutting_down_;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700314 bool started_;
315
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700316 // Hooks supported by JNI_CreateJavaVM
317 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
318 void (*exit_)(jint status);
319 void (*abort_)();
320
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700321 bool stats_enabled_;
322 RuntimeStats stats_;
323
jeffhao2692b572011-12-16 15:42:28 -0800324 Trace* tracer_;
325
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800326 typedef std::map<const ClassLoader*, std::vector<const DexFile*> > CompileTimeClassPaths;
327 CompileTimeClassPaths compile_time_class_paths_;
328 bool use_compile_time_class_path_;
Elliott Hughes131aef82012-01-27 11:53:35 -0800329
Carl Shapiro61e019d2011-07-14 16:53:09 -0700330 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700331};
332
333} // namespace art
334
Carl Shapiro1fb86202011-06-27 17:43:13 -0700335#endif // ART_SRC_RUNTIME_H_