blob: 55dab0763b1d6e94a0ce08b6420998f64596ee33 [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 Hughesa0e18062012-04-13 15:59:59 -070020#include <jni.h>
Elliott Hughesc5f7c912011-08-18 14:00:42 -070021#include <stdio.h>
22
Elliott Hughese27955c2011-08-26 15:21:24 -070023#include <iosfwd>
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 Hughesa0e18062012-04-13 15:59:59 -070028#include "globals.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070029#include "heap.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070030#include "instruction_set.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "macros.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070032#include "runtime_stats.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070033#include "safe_map.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "stringpiece.h"
Carl Shapirob5573532011-07-12 18:22:59 -070035
Carl Shapiro1fb86202011-06-27 17:43:13 -070036namespace art {
37
Brian Carlstrom16192862011-09-12 17:50:06 -070038template<class T> class PrimitiveArray;
39typedef PrimitiveArray<int8_t> ByteArray;
Carl Shapiro61e019d2011-07-14 16:53:09 -070040class ClassLinker;
Brian Carlstromaded5f72011-10-07 17:15:04 -070041class ClassLoader;
Carl Shapirofc322c72011-07-27 00:20:01 -070042class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070043class Heap;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070044class InternTable;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070045class JavaVMExt;
Ian Rogersff1ed472011-09-20 13:46:24 -070046class Method;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070047class MonitorList;
Elliott Hughese27955c2011-08-26 15:21:24 -070048class SignalCatcher;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070049class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070050class ThreadList;
jeffhao2692b572011-12-16 15:42:28 -080051class Trace;
Carl Shapiro61e019d2011-07-14 16:53:09 -070052
Carl Shapiro1fb86202011-06-27 17:43:13 -070053class Runtime {
54 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070055
Elliott Hughesf1a5adc2012-02-10 18:09:35 -080056 typedef std::vector<std::pair<std::string, const void*> > Options;
Brian Carlstrom8a436592011-08-15 21:27:23 -070057
58 class ParsedOptions {
59 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070060 // returns null if problem parsing and ignore_unrecognized is false
61 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070062
Brian Carlstroma004aa92012-02-08 18:05:09 -080063 const std::vector<const DexFile*>* boot_class_path_;
64 std::string boot_class_path_string_;
65 std::string class_path_string_;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070066 std::string host_prefix_;
Brian Carlstrom223f20f2012-02-04 23:06:55 -080067 std::string image_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070068 bool check_jni_;
Elliott Hughesa0957642011-09-02 14:27:33 -070069 std::string jni_trace_;
Elliott Hughesd9c67be2012-02-02 19:54:06 -080070 bool is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -070071 bool is_zygote_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070072 size_t heap_initial_size_;
73 size_t heap_maximum_size_;
jeffhaoc1160702011-10-27 15:48:45 -070074 size_t heap_growth_limit_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070075 size_t stack_size_;
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070076 size_t jni_globals_max_;
77 size_t lock_profiling_threshold_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -070078 std::string stack_trace_file_;
jeffhaob5e81852012-03-12 11:15:45 -070079 bool method_trace_;
80 std::string method_trace_file_;
81 size_t method_trace_file_size_;
Elliott Hughesfc861622011-10-17 17:57:47 -070082 bool (*hook_is_sensitive_thread_)();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070083 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
84 void (*hook_exit_)(jint status);
85 void (*hook_abort_)();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070086 std::vector<std::string> properties_;
87
88 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070089 ParsedOptions() {}
Brian Carlstrom8a436592011-08-15 21:27:23 -070090 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070091
Carl Shapiro61e019d2011-07-14 16:53:09 -070092 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070093 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070094
Elliott Hughesd9c67be2012-02-02 19:54:06 -080095 bool IsCompiler() const {
96 return is_compiler_;
97 }
98
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070099 bool IsZygote() const {
100 return is_zygote_;
101 }
102
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700103 const std::string& GetHostPrefix() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700104 DCHECK(!IsStarted());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700105 return host_prefix_;
106 }
107
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700108 // Starts a runtime, which may cause threads to be started and code to run.
109 void Start();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700110
Ian Rogers9af209c2012-06-03 20:50:30 -0700111 bool IsShuttingDown() const {
112 return shutting_down_;
113 }
114
115 bool IsStarted() const {
116 return started_;
117 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700118
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700119 static Runtime* Current() {
120 return instance_;
121 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700122
Elliott Hughesffe67362011-07-17 12:09:27 -0700123 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
124 // callers should prefer.
125 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
126 // in a single function together. This reduces code size slightly, but means
127 // that the native stack trace we get may point at the wrong call site.
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700128 static void Abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700129
Elliott Hughes462c9442012-03-23 18:47:50 -0700130 // Attaches the calling native thread to the runtime.
131 void AttachCurrentThread(const char* thread_name, bool as_daemon, Object* thread_group);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700132
Elliott Hughesbf86d042011-08-31 17:53:14 -0700133 void CallExitHook(jint status);
134
Carl Shapiro61e019d2011-07-14 16:53:09 -0700135 // Detaches the current native thread from the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -0700136 void DetachCurrentThread();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700137
Elliott Hughesc967f782012-04-16 10:23:15 -0700138 void DumpForSigQuit(std::ostream& os);
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800139 void DumpLockHolders(std::ostream& os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700140
Carl Shapiro61e019d2011-07-14 16:53:09 -0700141 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -0700142
Brian Carlstroma004aa92012-02-08 18:05:09 -0800143 const std::string& GetBootClassPathString() const {
144 return boot_class_path_string_;
145 }
146
147 const std::string& GetClassPathString() const {
148 return class_path_string_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700149 }
150
151 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700152 return class_linker_;
153 }
154
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700155 size_t GetDefaultStackSize() const {
156 return default_stack_size_;
157 }
158
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800159 Heap* GetHeap() const {
160 return heap_;
161 }
162
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700163 InternTable* GetInternTable() const {
164 return intern_table_;
165 }
166
Elliott Hughes0af55432011-08-17 18:37:28 -0700167 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700168 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700169 }
170
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700171 const std::vector<std::string>& GetProperties() const {
172 return properties_;
173 }
174
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700175 MonitorList* GetMonitorList() const {
176 return monitor_list_;
177 }
178
Elliott Hughesd92bec42011-09-02 17:04:36 -0700179 ThreadList* GetThreadList() const {
180 return thread_list_;
181 }
182
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700183 const char* GetVersion() const {
184 return "2.0.0";
185 }
186
Elliott Hughes410c0c82011-09-01 17:58:25 -0700187 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700188
Ian Rogers9af209c2012-06-03 20:50:30 -0700189 bool HasJniDlsymLookupStub() const {
190 return jni_stub_array_ != NULL;
191 }
192
193 ByteArray* GetJniDlsymLookupStub() const {
194 CHECK(HasJniDlsymLookupStub());
195 return jni_stub_array_;
196 }
197
Ian Rogers169c9a72011-11-13 20:13:17 -0800198 void SetJniDlsymLookupStub(ByteArray* jni_stub_array);
Brian Carlstrom16192862011-09-12 17:50:06 -0700199
Ian Rogers9af209c2012-06-03 20:50:30 -0700200 bool HasAbstractMethodErrorStubArray() const {
201 return abstract_method_error_stub_array_ != NULL;
202 }
203
204 ByteArray* GetAbstractMethodErrorStubArray() const {
205 CHECK(abstract_method_error_stub_array_ != NULL);
206 return abstract_method_error_stub_array_;
207 }
208
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209 void SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array);
Ian Rogersff1ed472011-09-20 13:46:24 -0700210
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700211 enum TrampolineType {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700212 kStaticMethod,
213 kUnknownMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700214 kLastTrampolineMethodType // Value used for iteration
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700215 };
Ian Rogers9af209c2012-06-03 20:50:30 -0700216
217 bool HasResolutionStubArray(TrampolineType type) const {
218 return resolution_stub_array_[type] != NULL;
219 }
220
221 ByteArray* GetResolutionStubArray(TrampolineType type) const {
222 CHECK(HasResolutionStubArray(type));
223 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
224 return resolution_stub_array_[type];
225 }
226
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700227 void SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700228
Ian Rogers9af209c2012-06-03 20:50:30 -0700229 // Returns a special method that calls into a trampoline for runtime method resolution
230 Method* GetResolutionMethod() const {
231 CHECK(HasResolutionMethod());
232 return resolution_method_;
233 }
234
235 bool HasResolutionMethod() const {
236 return resolution_method_ != NULL;
237 }
238
239 void SetResolutionMethod(Method* method) {
240 resolution_method_ = method;
241 }
242
Ian Rogers19846512012-02-24 11:42:47 -0800243 Method* CreateResolutionMethod();
Ian Rogers19846512012-02-24 11:42:47 -0800244
Ian Rogersff1ed472011-09-20 13:46:24 -0700245 // Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700246 enum CalleeSaveType {
247 kSaveAll,
248 kRefsOnly,
249 kRefsAndArgs,
250 kLastCalleeSaveType // Value used for iteration
251 };
Ian Rogers9af209c2012-06-03 20:50:30 -0700252
253 bool HasCalleeSaveMethod(CalleeSaveType type) const {
254 return callee_save_method_[type] != NULL;
255 }
256
257 Method* GetCalleeSaveMethod(CalleeSaveType type) const {
258 CHECK(HasCalleeSaveMethod(type));
259 return callee_save_method_[type];
260 }
261
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700262 void SetCalleeSaveMethod(Method* method, CalleeSaveType type);
263
Ian Rogers9af209c2012-06-03 20:50:30 -0700264 Method* CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type);
265
Brian Carlstromae826982011-11-09 01:33:42 -0800266 Method* CreateRefOnlyCalleeSaveMethod(InstructionSet instruction_set);
267 Method* CreateRefAndArgsCalleeSaveMethod(InstructionSet instruction_set);
Ian Rogersff1ed472011-09-20 13:46:24 -0700268
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700269 int32_t GetStat(int kind);
270
Ian Rogers9af209c2012-06-03 20:50:30 -0700271 RuntimeStats* GetStats() {
272 return &stats_;
273 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700274
275 bool HasStatsEnabled() const {
276 return stats_enabled_;
277 }
278
279 void ResetStats(int kinds);
280
281 void SetStatsEnabled(bool new_state);
282
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700283 void DidForkFromZygote();
284
jeffhao2692b572011-12-16 15:42:28 -0800285 void EnableMethodTracing(Trace* tracer);
286 void DisableMethodTracing();
Ian Rogers9af209c2012-06-03 20:50:30 -0700287
288 bool IsMethodTracingActive() const {
289 return tracer_ != NULL;
290 }
291
292 Trace* GetTracer() const {
293 CHECK(IsMethodTracingActive());
294 return tracer_;
295 }
jeffhao2692b572011-12-16 15:42:28 -0800296
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800297 bool UseCompileTimeClassPath() const {
298 return use_compile_time_class_path_;
299 }
Ian Rogers9af209c2012-06-03 20:50:30 -0700300
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800301 const std::vector<const DexFile*>& GetCompileTimeClassPath(const ClassLoader* class_loader);
302 void SetCompileTimeClassPath(const ClassLoader* class_loader, std::vector<const DexFile*>& class_path);
303
Carl Shapirob5573532011-07-12 18:22:59 -0700304 private:
Elliott Hughes457005c2012-04-16 13:54:25 -0700305 static void InitPlatformSignalHandlers();
Elliott Hughesffe67362011-07-17 12:09:27 -0700306
Elliott Hughesdcc24742011-09-07 14:02:44 -0700307 Runtime();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700308
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700309 void BlockSignals();
310
Brian Carlstrom8a436592011-08-15 21:27:23 -0700311 bool Init(const Options& options, bool ignore_unrecognized);
Elliott Hughes038a8062011-09-18 14:12:41 -0700312 void InitNativeMethods();
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800313 void RegisterRuntimeNativeMethods(JNIEnv* env);
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700314
Elliott Hughes85d15452011-09-16 17:33:01 -0700315 void StartDaemonThreads();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700316 void StartSignalCatcher();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700317
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800318 // A pointer to the active runtime or NULL.
319 static Runtime* instance_;
320
Elliott Hughesd9c67be2012-02-02 19:54:06 -0800321 bool is_compiler_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700322 bool is_zygote_;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700323
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700324 // The host prefix is used during cross compilation. It is removed
325 // from the start of host paths such as:
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800326 // $ANDROID_PRODUCT_OUT/system/framework/boot.oat
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700327 // to produce target paths such as
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -0700328 // /system/framework/boot.oat
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700329 // Similarly it is prepended to target paths to arrive back at a
330 // host past. In both cases this is necessary because image and oat
331 // files embedded expect paths of dependent files (an image points
332 // to an oat file and an oat files to one or more dex files). These
333 // files contain the expected target path.
334 std::string host_prefix_;
335
Brian Carlstroma004aa92012-02-08 18:05:09 -0800336 std::string boot_class_path_string_;
337 std::string class_path_string_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700338 std::vector<std::string> properties_;
339
Brian Carlstromb765be02011-08-17 23:54:10 -0700340 // The default stack size for managed threads created by the runtime.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700341 size_t default_stack_size_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700342
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800343 Heap* heap_;
344
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700345 MonitorList* monitor_list_;
346
Carl Shapirob5573532011-07-12 18:22:59 -0700347 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700348
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700349 InternTable* intern_table_;
350
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700351 ClassLinker* class_linker_;
352
Elliott Hughese27955c2011-08-26 15:21:24 -0700353 SignalCatcher* signal_catcher_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700354 std::string stack_trace_file_;
Elliott Hughese27955c2011-08-26 15:21:24 -0700355
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700356 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700357
Brian Carlstrom16192862011-09-12 17:50:06 -0700358 ByteArray* jni_stub_array_;
359
Brian Carlstrome24fa612011-09-29 00:53:55 -0700360 ByteArray* abstract_method_error_stub_array_;
361
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700362 ByteArray* resolution_stub_array_[kLastTrampolineMethodType];
Ian Rogersad25ac52011-10-04 19:13:33 -0700363
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700364 Method* callee_save_method_[kLastCalleeSaveType];
Ian Rogersff1ed472011-09-20 13:46:24 -0700365
Ian Rogers19846512012-02-24 11:42:47 -0800366 Method* resolution_method_;
367
Brian Carlstromaded5f72011-10-07 17:15:04 -0700368 // As returned by ClassLoader.getSystemClassLoader()
369 ClassLoader* system_class_loader_;
370
Elliott Hughes6b355752012-01-13 16:49:08 -0800371 bool shutting_down_;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700372 bool started_;
373
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700374 // Hooks supported by JNI_CreateJavaVM
375 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
376 void (*exit_)(jint status);
377 void (*abort_)();
378
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700379 bool stats_enabled_;
380 RuntimeStats stats_;
381
jeffhaob5e81852012-03-12 11:15:45 -0700382 bool method_trace_;
383 std::string method_trace_file_;
384 size_t method_trace_file_size_;
jeffhao2692b572011-12-16 15:42:28 -0800385 Trace* tracer_;
386
Elliott Hughesa0e18062012-04-13 15:59:59 -0700387 typedef SafeMap<const ClassLoader*, std::vector<const DexFile*> > CompileTimeClassPaths;
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800388 CompileTimeClassPaths compile_time_class_paths_;
389 bool use_compile_time_class_path_;
Elliott Hughes131aef82012-01-27 11:53:35 -0800390
Carl Shapiro61e019d2011-07-14 16:53:09 -0700391 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700392};
393
394} // namespace art
395
Carl Shapiro1fb86202011-06-27 17:43:13 -0700396#endif // ART_SRC_RUNTIME_H_