runtime.h revision c33a32bccc4c66ed82ce3a580b16636399385cb4
1// Copyright 2011 Google Inc. All Rights Reserved. 2 3#ifndef ART_SRC_RUNTIME_H_ 4#define ART_SRC_RUNTIME_H_ 5 6#include <stdio.h> 7 8#include <iosfwd> 9#include <string> 10#include <utility> 11#include <vector> 12 13#include <jni.h> 14 15#include "constants.h" 16#include "heap.h" 17#include "globals.h" 18#include "macros.h" 19#include "runtime_stats.h" 20#include "stringpiece.h" 21#include "unordered_set.h" 22 23namespace art { 24 25template<class T> class PrimitiveArray; 26typedef PrimitiveArray<int8_t> ByteArray; 27class ClassLinker; 28class ClassLoader; 29class DexFile; 30class Heap; 31class InternTable; 32class JavaVMExt; 33class Method; 34class MonitorList; 35class SignalCatcher; 36class String; 37class ThreadList; 38 39class Runtime { 40 public: 41 42 typedef std::vector<std::pair<StringPiece, const void*> > Options; 43 44 class ParsedOptions { 45 public: 46 // returns null if problem parsing and ignore_unrecognized is false 47 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized); 48 49 std::string boot_class_path_; 50 std::string class_path_; 51 std::string host_prefix_; 52 std::vector<std::string> images_; 53 bool check_jni_; 54 std::string jni_trace_; 55 bool is_zygote_; 56 size_t heap_initial_size_; 57 size_t heap_maximum_size_; 58 size_t stack_size_; 59 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap); 60 void (*hook_exit_)(jint status); 61 void (*hook_abort_)(); 62 std::tr1::unordered_set<std::string> verbose_; 63 std::vector<std::string> properties_; 64 65 bool IsVerbose(const std::string& key) const { 66 return verbose_.find(key) != verbose_.end(); 67 } 68 69 private: 70 ParsedOptions() {} 71 }; 72 73 // Creates and initializes a new runtime. 74 static Runtime* Create(const Options& options, bool ignore_unrecognized); 75 76 bool IsVerboseStartup() const { 77 return verbose_startup_; 78 } 79 80 const std::string& GetHostPrefix() const { 81 CHECK(!IsStarted()); 82 return host_prefix_; 83 } 84 85 // Starts a runtime, which may cause threads to be started and code to run. 86 void Start(); 87 88 bool IsStarted() const; 89 90 static Runtime* Current() { 91 return instance_; 92 } 93 94 // Compiles a dex file. 95 static void Compile(const StringPiece& filename); 96 97 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most 98 // callers should prefer. 99 // This isn't marked ((noreturn)) because then gcc will merge multiple calls 100 // in a single function together. This reduces code size slightly, but means 101 // that the native stack trace we get may point at the wrong call site. 102 static void Abort(const char* file, int line); 103 104 // Attaches the current native thread to the runtime. 105 void AttachCurrentThread(const char* name, bool as_daemon); 106 107 void CallExitHook(jint status); 108 109 // Detaches the current native thread from the runtime. 110 void DetachCurrentThread(); 111 112 void Dump(std::ostream& os); 113 114 ~Runtime(); 115 116 const std::string& GetBootClassPath() const { 117 return boot_class_path_; 118 } 119 120 ClassLinker* GetClassLinker() const { 121 return class_linker_; 122 } 123 124 const std::string& GetClassPath() const { 125 return class_path_; 126 } 127 128 size_t GetDefaultStackSize() const { 129 return default_stack_size_; 130 } 131 132 InternTable* GetInternTable() const { 133 return intern_table_; 134 } 135 136 JavaVMExt* GetJavaVM() const { 137 return java_vm_; 138 } 139 140 const std::vector<std::string>& GetProperties() const { 141 return properties_; 142 } 143 144 MonitorList* GetMonitorList() const { 145 return monitor_list_; 146 } 147 148 ThreadList* GetThreadList() const { 149 return thread_list_; 150 } 151 152 const char* GetVersion() const { 153 return "2.0.0"; 154 } 155 156 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; 157 158 bool HasJniStubArray() const; 159 ByteArray* GetJniStubArray() const; 160 void SetJniStubArray(ByteArray* jni_stub_array); 161 162 bool HasAbstractMethodErrorStubArray() const; 163 ByteArray* GetAbstractMethodErrorStubArray() const; 164 void SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array); 165 166 enum TrampolineType { 167 kInstanceMethod, 168 kStaticMethod, 169 kUnknownMethod, 170 kLastTrampolineMethodType // Value used for iteration 171 }; 172 static TrampolineType GetTrampolineType(Method* method); 173 bool HasResolutionStubArray(TrampolineType type) const; 174 ByteArray* GetResolutionStubArray(TrampolineType type) const; 175 void SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type); 176 177 // Returns a special method that describes all callee saves being spilled to the stack. 178 enum CalleeSaveType { 179 kSaveAll, 180 kRefsOnly, 181 kRefsAndArgs, 182 kLastCalleeSaveType // Value used for iteration 183 }; 184 Method* CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type); 185 bool HasCalleeSaveMethod(CalleeSaveType type) const; 186 Method* GetCalleeSaveMethod(CalleeSaveType type) const; 187 void SetCalleeSaveMethod(Method* method, CalleeSaveType type); 188 189 Method* CreateRefOnlyCalleeSaveMethod(InstructionSet insns); 190 Method* CreateRefAndArgsCalleeSaveMethod(InstructionSet insns); 191 192 int32_t GetStat(int kind); 193 194 RuntimeStats* GetStats(); 195 196 bool HasStatsEnabled() const { 197 return stats_enabled_; 198 } 199 200 void ResetStats(int kinds); 201 202 void SetStatsEnabled(bool new_state); 203 204 private: 205 static void PlatformAbort(const char*, int); 206 207 Runtime(); 208 209 void BlockSignals(); 210 211 bool Init(const Options& options, bool ignore_unrecognized); 212 void InitNativeMethods(); 213 void RegisterRuntimeNativeMethods(JNIEnv*); 214 void StartDaemonThreads(); 215 216 bool verbose_startup_; 217 bool is_zygote_; 218 219 // The host prefix is used during cross compilation. It is removed 220 // from the start of host paths such as: 221 // $ANDROID_PRODUCT_OUT/system/framework/core.oat 222 // to produce target paths such as 223 // /system/framework/core.oat 224 // Similarly it is prepended to target paths to arrive back at a 225 // host past. In both cases this is necessary because image and oat 226 // files embedded expect paths of dependent files (an image points 227 // to an oat file and an oat files to one or more dex files). These 228 // files contain the expected target path. 229 std::string host_prefix_; 230 231 std::string boot_class_path_; 232 std::string class_path_; 233 std::vector<std::string> properties_; 234 235 // The default stack size for managed threads created by the runtime. 236 size_t default_stack_size_; 237 238 MonitorList* monitor_list_; 239 240 ThreadList* thread_list_; 241 242 InternTable* intern_table_; 243 244 ClassLinker* class_linker_; 245 246 SignalCatcher* signal_catcher_; 247 248 JavaVMExt* java_vm_; 249 250 ByteArray* jni_stub_array_; 251 252 ByteArray* abstract_method_error_stub_array_; 253 254 ByteArray* resolution_stub_array_[kLastTrampolineMethodType]; 255 256 Method* callee_save_method_[kLastCalleeSaveType]; 257 258 // As returned by ClassLoader.getSystemClassLoader() 259 ClassLoader* system_class_loader_; 260 261 bool started_; 262 263 // Hooks supported by JNI_CreateJavaVM 264 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap); 265 void (*exit_)(jint status); 266 void (*abort_)(); 267 268 bool stats_enabled_; 269 RuntimeStats stats_; 270 271 // A pointer to the active runtime or NULL. 272 static Runtime* instance_; 273 274 DISALLOW_COPY_AND_ASSIGN(Runtime); 275}; 276 277} // namespace art 278 279#endif // ART_SRC_RUNTIME_H_ 280