blob: dda58231bbab5bd55fc2444dd2e8b5e36d85e69f [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>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070023#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -070024#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070025#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026
Elliott Hughesc5f7c912011-08-18 14:00:42 -070027#include <jni.h>
28
Ian Rogersff1ed472011-09-20 13:46:24 -070029#include "constants.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070030#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "globals.h"
32#include "macros.h"
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070033#include "runtime_stats.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
56 typedef std::vector<std::pair<StringPiece, const void*> > Options;
57
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 Carlstrom58ae9412011-10-04 00:56:06 -070063 std::string boot_class_path_;
64 std::string class_path_;
65 std::string host_prefix_;
66 std::vector<std::string> images_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070067 bool check_jni_;
Elliott Hughesa0957642011-09-02 14:27:33 -070068 std::string jni_trace_;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -070069 bool is_zygote_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070070 size_t heap_initial_size_;
71 size_t heap_maximum_size_;
jeffhaoc1160702011-10-27 15:48:45 -070072 size_t heap_growth_limit_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070073 size_t stack_size_;
Elliott Hughesbb1e8f02011-10-18 14:14:25 -070074 size_t jni_globals_max_;
75 size_t lock_profiling_threshold_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -070076 std::string stack_trace_file_;
Elliott Hughesfc861622011-10-17 17:57:47 -070077 bool (*hook_is_sensitive_thread_)();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070078 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
79 void (*hook_exit_)(jint status);
80 void (*hook_abort_)();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070081 std::vector<std::string> properties_;
82
83 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070084 ParsedOptions() {}
Brian Carlstrom8a436592011-08-15 21:27:23 -070085 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070086
Carl Shapiro61e019d2011-07-14 16:53:09 -070087 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070088 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070089
Brian Carlstromcaabb1b2011-10-11 18:09:13 -070090 bool IsZygote() const {
91 return is_zygote_;
92 }
93
Brian Carlstrom58ae9412011-10-04 00:56:06 -070094 const std::string& GetHostPrefix() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -070095 DCHECK(!IsStarted());
Brian Carlstrom58ae9412011-10-04 00:56:06 -070096 return host_prefix_;
97 }
98
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070099 // Starts a runtime, which may cause threads to be started and code to run.
100 void Start();
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700101
Elliott Hughes6b355752012-01-13 16:49:08 -0800102 bool IsShuttingDown() const;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700103 bool IsStarted() const;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700104
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700105 static Runtime* Current() {
106 return instance_;
107 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700108
Carl Shapiro61e019d2011-07-14 16:53:09 -0700109 // Compiles a dex file.
110 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -0700111
Elliott Hughesffe67362011-07-17 12:09:27 -0700112 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
113 // callers should prefer.
114 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
115 // in a single function together. This reduces code size slightly, but means
116 // that the native stack trace we get may point at the wrong call site.
117 static void Abort(const char* file, int line);
118
Carl Shapiro61e019d2011-07-14 16:53:09 -0700119 // Attaches the current native thread to the runtime.
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700120 void AttachCurrentThread(const char* name, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700121
Elliott Hughesbf86d042011-08-31 17:53:14 -0700122 void CallExitHook(jint status);
123
Carl Shapiro61e019d2011-07-14 16:53:09 -0700124 // Detaches the current native thread from the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -0700125 void DetachCurrentThread();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700126
Elliott Hughes8daa0922011-09-11 13:46:25 -0700127 void Dump(std::ostream& os);
Elliott Hughes21a5bf22011-12-07 14:35:20 -0800128 void DumpLockHolders(std::ostream& os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700129
Carl Shapiro61e019d2011-07-14 16:53:09 -0700130 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -0700131
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700132 const std::string& GetBootClassPath() const {
133 return boot_class_path_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700134 }
135
136 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700137 return class_linker_;
138 }
139
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700140 const std::string& GetClassPath() const {
141 return class_path_;
142 }
143
144 size_t GetDefaultStackSize() const {
145 return default_stack_size_;
146 }
147
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700148 InternTable* GetInternTable() const {
149 return intern_table_;
150 }
151
Elliott Hughes0af55432011-08-17 18:37:28 -0700152 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700153 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700154 }
155
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700156 const std::vector<std::string>& GetProperties() const {
157 return properties_;
158 }
159
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700160 MonitorList* GetMonitorList() const {
161 return monitor_list_;
162 }
163
Elliott Hughesd92bec42011-09-02 17:04:36 -0700164 ThreadList* GetThreadList() const {
165 return thread_list_;
166 }
167
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700168 const char* GetVersion() const {
169 return "2.0.0";
170 }
171
Elliott Hughes410c0c82011-09-01 17:58:25 -0700172 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700173
Ian Rogers169c9a72011-11-13 20:13:17 -0800174 bool HasJniDlsymLookupStub() const;
175 ByteArray* GetJniDlsymLookupStub() const;
176 void SetJniDlsymLookupStub(ByteArray* jni_stub_array);
Brian Carlstrom16192862011-09-12 17:50:06 -0700177
Brian Carlstrome24fa612011-09-29 00:53:55 -0700178 bool HasAbstractMethodErrorStubArray() const;
179 ByteArray* GetAbstractMethodErrorStubArray() const;
180 void SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array);
Ian Rogersff1ed472011-09-20 13:46:24 -0700181
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700182 enum TrampolineType {
183 kInstanceMethod,
184 kStaticMethod,
185 kUnknownMethod,
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700186 kLastTrampolineMethodType // Value used for iteration
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700187 };
188 static TrampolineType GetTrampolineType(Method* method);
189 bool HasResolutionStubArray(TrampolineType type) const;
190 ByteArray* GetResolutionStubArray(TrampolineType type) const;
191 void SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type);
Ian Rogersad25ac52011-10-04 19:13:33 -0700192
Ian Rogersff1ed472011-09-20 13:46:24 -0700193 // Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700194 enum CalleeSaveType {
195 kSaveAll,
196 kRefsOnly,
197 kRefsAndArgs,
198 kLastCalleeSaveType // Value used for iteration
199 };
Brian Carlstromae826982011-11-09 01:33:42 -0800200 Method* CreateCalleeSaveMethod(InstructionSet instruction_set, CalleeSaveType type);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700201 bool HasCalleeSaveMethod(CalleeSaveType type) const;
202 Method* GetCalleeSaveMethod(CalleeSaveType type) const;
203 void SetCalleeSaveMethod(Method* method, CalleeSaveType type);
204
Brian Carlstromae826982011-11-09 01:33:42 -0800205 Method* CreateRefOnlyCalleeSaveMethod(InstructionSet instruction_set);
206 Method* CreateRefAndArgsCalleeSaveMethod(InstructionSet instruction_set);
Ian Rogersff1ed472011-09-20 13:46:24 -0700207
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700208 int32_t GetStat(int kind);
209
210 RuntimeStats* GetStats();
211
212 bool HasStatsEnabled() const {
213 return stats_enabled_;
214 }
215
216 void ResetStats(int kinds);
217
218 void SetStatsEnabled(bool new_state);
219
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700220 void DidForkFromZygote();
221
jeffhao2692b572011-12-16 15:42:28 -0800222 void EnableMethodTracing(Trace* tracer);
223 void DisableMethodTracing();
224 bool IsMethodTracingActive() const;
225 Trace* GetTracer() const;
226
Carl Shapirob5573532011-07-12 18:22:59 -0700227 private:
Elliott Hughesffe67362011-07-17 12:09:27 -0700228 static void PlatformAbort(const char*, int);
229
Elliott Hughesdcc24742011-09-07 14:02:44 -0700230 Runtime();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700231
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700232 void BlockSignals();
233
Brian Carlstrom8a436592011-08-15 21:27:23 -0700234 bool Init(const Options& options, bool ignore_unrecognized);
Elliott Hughes038a8062011-09-18 14:12:41 -0700235 void InitNativeMethods();
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800236 void RegisterRuntimeNativeMethods(JNIEnv* env);
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700237
Elliott Hughes85d15452011-09-16 17:33:01 -0700238 void StartDaemonThreads();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700239 void StartSignalCatcher();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700240
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700241 bool is_zygote_;
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700242
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700243 // The host prefix is used during cross compilation. It is removed
244 // from the start of host paths such as:
Brian Carlstrom29e7ac72011-12-05 23:42:57 -0800245 // $ANDROID_PRODUCT_OUT/system/framework/boot.oat
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700246 // to produce target paths such as
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -0700247 // /system/framework/boot.oat
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700248 // Similarly it is prepended to target paths to arrive back at a
249 // host past. In both cases this is necessary because image and oat
250 // files embedded expect paths of dependent files (an image points
251 // to an oat file and an oat files to one or more dex files). These
252 // files contain the expected target path.
253 std::string host_prefix_;
254
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700255 std::string boot_class_path_;
256 std::string class_path_;
257 std::vector<std::string> properties_;
258
Brian Carlstromb765be02011-08-17 23:54:10 -0700259 // The default stack size for managed threads created by the runtime.
Elliott Hughesbe759c62011-09-08 19:38:21 -0700260 size_t default_stack_size_;
Brian Carlstromb765be02011-08-17 23:54:10 -0700261
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700262 MonitorList* monitor_list_;
263
Carl Shapirob5573532011-07-12 18:22:59 -0700264 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700265
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700266 InternTable* intern_table_;
267
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700268 ClassLinker* class_linker_;
269
Elliott Hughese27955c2011-08-26 15:21:24 -0700270 SignalCatcher* signal_catcher_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700271 std::string stack_trace_file_;
Elliott Hughese27955c2011-08-26 15:21:24 -0700272
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700273 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700274
Brian Carlstrom16192862011-09-12 17:50:06 -0700275 ByteArray* jni_stub_array_;
276
Brian Carlstrome24fa612011-09-29 00:53:55 -0700277 ByteArray* abstract_method_error_stub_array_;
278
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700279 ByteArray* resolution_stub_array_[kLastTrampolineMethodType];
Ian Rogersad25ac52011-10-04 19:13:33 -0700280
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700281 Method* callee_save_method_[kLastCalleeSaveType];
Ian Rogersff1ed472011-09-20 13:46:24 -0700282
Brian Carlstromaded5f72011-10-07 17:15:04 -0700283 // As returned by ClassLoader.getSystemClassLoader()
284 ClassLoader* system_class_loader_;
285
Elliott Hughes6b355752012-01-13 16:49:08 -0800286 bool shutting_down_;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700287 bool started_;
288
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700289 // Hooks supported by JNI_CreateJavaVM
290 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
291 void (*exit_)(jint status);
292 void (*abort_)();
293
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700294 bool stats_enabled_;
295 RuntimeStats stats_;
296
jeffhao2692b572011-12-16 15:42:28 -0800297 Trace* tracer_;
298
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700299 // A pointer to the active runtime or NULL.
300 static Runtime* instance_;
301
Elliott Hughes131aef82012-01-27 11:53:35 -0800302 static Mutex abort_lock_;
303
Carl Shapiro61e019d2011-07-14 16:53:09 -0700304 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700305};
306
307} // namespace art
308
Carl Shapiro1fb86202011-06-27 17:43:13 -0700309#endif // ART_SRC_RUNTIME_H_