runtime.h revision be759c63c6bb58b76ac71cad2c5a736bd31f374d
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 "heap.h" 16#include "globals.h" 17#include "macros.h" 18#include "stringpiece.h" 19#include "unordered_set.h" 20 21namespace art { 22 23class ClassLinker; 24class DexFile; 25class Heap; 26class InternTable; 27class JavaVMExt; 28class SignalCatcher; 29class String; 30class ThreadList; 31 32class Runtime { 33 public: 34 35 typedef std::vector<std::pair<StringPiece, const void*> > Options; 36 37 class ParsedOptions { 38 public: 39 // returns null if problem parsing and ignore_unrecognized is false 40 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized); 41 42 std::vector<const DexFile*> boot_class_path_; 43 std::vector<const DexFile*> class_path_; 44 const char* boot_image_; 45 std::vector<const char*> images_; 46 bool check_jni_; 47 std::string jni_trace_; 48 size_t heap_initial_size_; 49 size_t heap_maximum_size_; 50 size_t stack_size_; 51 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap); 52 void (*hook_exit_)(jint status); 53 void (*hook_abort_)(); 54 std::tr1::unordered_set<std::string> verbose_; 55 std::vector<std::string> properties_; 56 57 bool IsVerbose(const std::string& key) const { 58 return verbose_.find(key) != verbose_.end(); 59 } 60 61 private: 62 ParsedOptions() {} 63 }; 64 65 // Creates and initializes a new runtime. 66 static Runtime* Create(const Options& options, bool ignore_unrecognized); 67 68 // Starts a runtime, which may cause threads to be started and code to run. 69 void Start(); 70 71 bool IsStarted(); 72 73 static Runtime* Current() { 74 return instance_; 75 } 76 77 // Compiles a dex file. 78 static void Compile(const StringPiece& filename); 79 80 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most 81 // callers should prefer. 82 // This isn't marked ((noreturn)) because then gcc will merge multiple calls 83 // in a single function together. This reduces code size slightly, but means 84 // that the native stack trace we get may point at the wrong call site. 85 static void Abort(const char* file, int line); 86 87 // Attaches the current native thread to the runtime. 88 void AttachCurrentThread(const char* name, bool as_daemon); 89 90 void CallExitHook(jint status); 91 92 // Detaches the current native thread from the runtime. 93 void DetachCurrentThread(); 94 95 void DumpStatistics(std::ostream& os); 96 97 ~Runtime(); 98 99 size_t GetDefaultStackSize() const { 100 return default_stack_size_; 101 } 102 103 ClassLinker* GetClassLinker() const { 104 return class_linker_; 105 } 106 107 InternTable* GetInternTable() const { 108 return intern_table_; 109 } 110 111 JavaVMExt* GetJavaVM() const { 112 return java_vm_; 113 } 114 115 ThreadList* GetThreadList() const { 116 return thread_list_; 117 } 118 119 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const; 120 121 private: 122 static void PlatformAbort(const char*, int); 123 124 Runtime(); 125 126 void BlockSignals(); 127 128 bool Init(const Options& options, bool ignore_unrecognized); 129 void InitLibraries(); 130 void RegisterRuntimeNativeMethods(JNIEnv*); 131 132 // The default stack size for managed threads created by the runtime. 133 size_t default_stack_size_; 134 135 ThreadList* thread_list_; 136 137 InternTable* intern_table_; 138 139 ClassLinker* class_linker_; 140 141 SignalCatcher* signal_catcher_; 142 143 JavaVMExt* java_vm_; 144 145 bool started_; 146 147 // Hooks supported by JNI_CreateJavaVM 148 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap); 149 void (*exit_)(jint status); 150 void (*abort_)(); 151 152 // A pointer to the active runtime or NULL. 153 static Runtime* instance_; 154 155 DISALLOW_COPY_AND_ASSIGN(Runtime); 156}; 157 158} // namespace art 159 160#endif // ART_SRC_RUNTIME_H_ 161