blob: 8a6ffe3b51454ac38448291f23a72ef2822d2923 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_RUNTIME_H_
4#define ART_SRC_RUNTIME_H_
5
Elliott Hughesc5f7c912011-08-18 14:00:42 -07006#include <stdio.h>
7
Elliott Hughese27955c2011-08-26 15:21:24 -07008#include <iosfwd>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -07009#include <string>
Carl Shapirofc322c72011-07-27 00:20:01 -070010#include <utility>
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070011#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012
Elliott Hughesc5f7c912011-08-18 14:00:42 -070013#include <jni.h>
14
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "globals.h"
17#include "macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070018#include "stringpiece.h"
Brian Carlstrom44753c32011-08-17 22:22:11 -070019#include "unordered_set.h"
Carl Shapirob5573532011-07-12 18:22:59 -070020
Carl Shapiro1fb86202011-06-27 17:43:13 -070021namespace art {
22
Carl Shapiro61e019d2011-07-14 16:53:09 -070023class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070024class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070025class Heap;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070026class InternTable;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070027class JavaVMExt;
Elliott Hughese27955c2011-08-26 15:21:24 -070028class SignalCatcher;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070029class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070030class ThreadList;
31
Carl Shapiro1fb86202011-06-27 17:43:13 -070032class Runtime {
33 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070034
35 typedef std::vector<std::pair<StringPiece, const void*> > Options;
36
37 class ParsedOptions {
38 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070039 // returns null if problem parsing and ignore_unrecognized is false
40 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070041
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070042 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070043 std::vector<const DexFile*> class_path_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070044 const char* boot_image_;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070045 std::vector<const char*> images_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070046 bool check_jni_;
Elliott Hughesa0957642011-09-02 14:27:33 -070047 std::string jni_trace_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070048 size_t heap_initial_size_;
49 size_t heap_maximum_size_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070050 size_t stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070051 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
52 void (*hook_exit_)(jint status);
53 void (*hook_abort_)();
Brian Carlstrom44753c32011-08-17 22:22:11 -070054 std::tr1::unordered_set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070055 std::vector<std::string> properties_;
56
Elliott Hughesa0957642011-09-02 14:27:33 -070057 bool IsVerbose(const std::string& key) const {
58 return verbose_.find(key) != verbose_.end();
59 }
60
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070061 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070062 ParsedOptions() {}
Brian Carlstrom8a436592011-08-15 21:27:23 -070063 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070064
Carl Shapiro61e019d2011-07-14 16:53:09 -070065 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070066 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070067
68 // Starts a runtime, which may cause threads to be started and code to run.
69 void Start();
Carl Shapiro2ed144c2011-07-26 16:52:08 -070070
71 static Runtime* Current() {
72 return instance_;
73 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070074
Carl Shapiro61e019d2011-07-14 16:53:09 -070075 // Compiles a dex file.
76 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070077
Elliott Hughesffe67362011-07-17 12:09:27 -070078 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
79 // callers should prefer.
80 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
81 // in a single function together. This reduces code size slightly, but means
82 // that the native stack trace we get may point at the wrong call site.
83 static void Abort(const char* file, int line);
84
Carl Shapiro61e019d2011-07-14 16:53:09 -070085 // Attaches the current native thread to the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -070086 void AttachCurrentThread(const char* name, JNIEnv** jni_env, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -070087
Elliott Hughesbf86d042011-08-31 17:53:14 -070088 void CallExitHook(jint status);
89
Carl Shapiro61e019d2011-07-14 16:53:09 -070090 // Detaches the current native thread from the runtime.
Elliott Hughesd92bec42011-09-02 17:04:36 -070091 void DetachCurrentThread();
Carl Shapiro61e019d2011-07-14 16:53:09 -070092
Elliott Hughese27955c2011-08-26 15:21:24 -070093 void DumpStatistics(std::ostream& os);
94
Carl Shapiro61e019d2011-07-14 16:53:09 -070095 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070096
Brian Carlstromb765be02011-08-17 23:54:10 -070097 size_t GetStackSize() const {
98 return stack_size_;
99 }
100
101 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -0700102 return class_linker_;
103 }
104
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700105 InternTable* GetInternTable() const {
106 return intern_table_;
107 }
108
Elliott Hughes0af55432011-08-17 18:37:28 -0700109 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700110 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700111 }
112
Elliott Hughesd92bec42011-09-02 17:04:36 -0700113 ThreadList* GetThreadList() const {
114 return thread_list_;
115 }
116
Elliott Hughes410c0c82011-09-01 17:58:25 -0700117 void VisitRoots(Heap::RootVisitor* visitor, void* arg) const;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700118
Carl Shapirob5573532011-07-12 18:22:59 -0700119 private:
Elliott Hughesffe67362011-07-17 12:09:27 -0700120 static void PlatformAbort(const char*, int);
121
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700122 Runtime() : stack_size_(0), thread_list_(NULL), intern_table_(NULL), class_linker_(NULL),
123 signal_catcher_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -0700124
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700125 void BlockSignals();
126
Brian Carlstrom8a436592011-08-15 21:27:23 -0700127 bool Init(const Options& options, bool ignore_unrecognized);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700128 void InitLibraries();
129 void RegisterRuntimeNativeMethods(JNIEnv*);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700130
Brian Carlstromb765be02011-08-17 23:54:10 -0700131 // The default stack size for managed threads created by the runtime.
132 size_t stack_size_;
133
Carl Shapirob5573532011-07-12 18:22:59 -0700134 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700135
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700136 InternTable* intern_table_;
137
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700138 ClassLinker* class_linker_;
139
Elliott Hughese27955c2011-08-26 15:21:24 -0700140 SignalCatcher* signal_catcher_;
141
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700142 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700143
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700144 // Hooks supported by JNI_CreateJavaVM
145 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
146 void (*exit_)(jint status);
147 void (*abort_)();
148
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700149 // A pointer to the active runtime or NULL.
150 static Runtime* instance_;
151
Carl Shapiro61e019d2011-07-14 16:53:09 -0700152 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700153};
154
155} // namespace art
156
Carl Shapiro1fb86202011-06-27 17:43:13 -0700157#endif // ART_SRC_RUNTIME_H_