Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_RUNTIME_H_ |
| 4 | #define ART_SRC_RUNTIME_H_ |
| 5 | |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 8 | #include <iosfwd> |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 9 | #include <string> |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 10 | #include <utility> |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 11 | #include <vector> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 12 | |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 13 | #include <jni.h> |
| 14 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 15 | #include "heap.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 16 | #include "globals.h" |
| 17 | #include "macros.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 18 | #include "stringpiece.h" |
Brian Carlstrom | 44753c3 | 2011-08-17 22:22:11 -0700 | [diff] [blame] | 19 | #include "unordered_set.h" |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 20 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 23 | template<class T> class PrimitiveArray; |
| 24 | typedef PrimitiveArray<int8_t> ByteArray; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 25 | class ClassLinker; |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 26 | class DexFile; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 27 | class Heap; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 28 | class InternTable; |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 29 | class JavaVMExt; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 30 | class SignalCatcher; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 31 | class String; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 32 | class ThreadList; |
| 33 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 34 | class Runtime { |
| 35 | public: |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 36 | |
| 37 | typedef std::vector<std::pair<StringPiece, const void*> > Options; |
| 38 | |
| 39 | class ParsedOptions { |
| 40 | public: |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 41 | // returns null if problem parsing and ignore_unrecognized is false |
| 42 | static ParsedOptions* Create(const Options& options, bool ignore_unrecognized); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 44 | std::string boot_class_path_string_; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 45 | std::vector<const DexFile*> boot_class_path_; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 46 | std::string class_path_string_; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 47 | std::vector<const DexFile*> class_path_; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 48 | const char* boot_image_; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 49 | std::vector<const char*> images_; |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 50 | bool check_jni_; |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 51 | std::string jni_trace_; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 52 | size_t heap_initial_size_; |
| 53 | size_t heap_maximum_size_; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 54 | size_t stack_size_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 55 | jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap); |
| 56 | void (*hook_exit_)(jint status); |
| 57 | void (*hook_abort_)(); |
Brian Carlstrom | 44753c3 | 2011-08-17 22:22:11 -0700 | [diff] [blame] | 58 | std::tr1::unordered_set<std::string> verbose_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 59 | std::vector<std::string> properties_; |
| 60 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 61 | bool IsVerbose(const std::string& key) const { |
| 62 | return verbose_.find(key) != verbose_.end(); |
| 63 | } |
| 64 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 65 | private: |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 66 | ParsedOptions() {} |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 67 | }; |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 68 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 69 | // Creates and initializes a new runtime. |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 70 | static Runtime* Create(const Options& options, bool ignore_unrecognized); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 71 | |
| 72 | // Starts a runtime, which may cause threads to be started and code to run. |
| 73 | void Start(); |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 74 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 75 | bool IsStarted(); |
| 76 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 77 | static Runtime* Current() { |
| 78 | return instance_; |
| 79 | } |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 80 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 81 | // Compiles a dex file. |
| 82 | static void Compile(const StringPiece& filename); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 83 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 84 | // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most |
| 85 | // callers should prefer. |
| 86 | // This isn't marked ((noreturn)) because then gcc will merge multiple calls |
| 87 | // in a single function together. This reduces code size slightly, but means |
| 88 | // that the native stack trace we get may point at the wrong call site. |
| 89 | static void Abort(const char* file, int line); |
| 90 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 91 | // Attaches the current native thread to the runtime. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 92 | void AttachCurrentThread(const char* name, bool as_daemon); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 94 | void CallExitHook(jint status); |
| 95 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 96 | // Detaches the current native thread from the runtime. |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 97 | void DetachCurrentThread(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 98 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 99 | void Dump(std::ostream& os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 100 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 101 | ~Runtime(); |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 102 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 103 | const std::string& GetBootClassPath() const { |
| 104 | return boot_class_path_; |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ClassLinker* GetClassLinker() const { |
Carl Shapiro | 7a90959 | 2011-07-24 19:21:59 -0700 | [diff] [blame] | 108 | return class_linker_; |
| 109 | } |
| 110 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 111 | const std::string& GetClassPath() const { |
| 112 | return class_path_; |
| 113 | } |
| 114 | |
| 115 | size_t GetDefaultStackSize() const { |
| 116 | return default_stack_size_; |
| 117 | } |
| 118 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 119 | InternTable* GetInternTable() const { |
| 120 | return intern_table_; |
| 121 | } |
| 122 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 123 | JavaVMExt* GetJavaVM() const { |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 124 | return java_vm_; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 127 | const std::vector<std::string>& GetProperties() const { |
| 128 | return properties_; |
| 129 | } |
| 130 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 131 | ThreadList* GetThreadList() const { |
| 132 | return thread_list_; |
| 133 | } |
| 134 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 135 | const char* GetVersion() const { |
| 136 | return "2.0.0"; |
| 137 | } |
| 138 | |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 139 | void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 140 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 141 | bool HasJniStubArray() const { |
| 142 | return jni_stub_array_ != NULL; |
| 143 | } |
| 144 | |
| 145 | ByteArray* GetJniStubArray() const { |
| 146 | CHECK(jni_stub_array_ != NULL); |
| 147 | return jni_stub_array_; |
| 148 | } |
| 149 | |
| 150 | void SetJniStubArray(ByteArray* jni_stub_array) { |
| 151 | CHECK(jni_stub_array != NULL); |
| 152 | CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array); |
| 153 | jni_stub_array_ = jni_stub_array; |
| 154 | } |
| 155 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 156 | private: |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 157 | static void PlatformAbort(const char*, int); |
| 158 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 159 | Runtime(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 160 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 161 | void BlockSignals(); |
| 162 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 163 | bool Init(const Options& options, bool ignore_unrecognized); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 164 | void InitLibraries(); |
| 165 | void RegisterRuntimeNativeMethods(JNIEnv*); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame^] | 166 | void StartDaemonThreads(); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 167 | |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 168 | std::string boot_class_path_; |
| 169 | std::string class_path_; |
| 170 | std::vector<std::string> properties_; |
| 171 | |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 172 | // The default stack size for managed threads created by the runtime. |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 173 | size_t default_stack_size_; |
Brian Carlstrom | b765be0 | 2011-08-17 23:54:10 -0700 | [diff] [blame] | 174 | |
Carl Shapiro | b557353 | 2011-07-12 18:22:59 -0700 | [diff] [blame] | 175 | ThreadList* thread_list_; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 176 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 177 | InternTable* intern_table_; |
| 178 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 179 | ClassLinker* class_linker_; |
| 180 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 181 | SignalCatcher* signal_catcher_; |
| 182 | |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 183 | JavaVMExt* java_vm_; |
Elliott Hughes | f2682d5 | 2011-08-15 16:37:04 -0700 | [diff] [blame] | 184 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 185 | ByteArray* jni_stub_array_; |
| 186 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 187 | bool started_; |
| 188 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 189 | // Hooks supported by JNI_CreateJavaVM |
| 190 | jint (*vfprintf_)(FILE* stream, const char* format, va_list ap); |
| 191 | void (*exit_)(jint status); |
| 192 | void (*abort_)(); |
| 193 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 194 | // A pointer to the active runtime or NULL. |
| 195 | static Runtime* instance_; |
| 196 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 197 | DISALLOW_COPY_AND_ASSIGN(Runtime); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | } // namespace art |
| 201 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 202 | #endif // ART_SRC_RUNTIME_H_ |