blob: 40db8145c0c11395d3a92f513e163d176d8b66fc [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 Hughesc5f7c912011-08-18 14:00:42 -070026class JavaVMExt;
Elliott Hughese27955c2011-08-26 15:21:24 -070027class SignalCatcher;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070028class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070029class ThreadList;
30
Carl Shapiro1fb86202011-06-27 17:43:13 -070031class Runtime {
32 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070033
34 typedef std::vector<std::pair<StringPiece, const void*> > Options;
35
36 class ParsedOptions {
37 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070038 // returns null if problem parsing and ignore_unrecognized is false
39 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070040
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070041 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070042 const char* boot_image_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070043 bool check_jni_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070044 size_t heap_initial_size_;
45 size_t heap_maximum_size_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070046 size_t stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070047 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
48 void (*hook_exit_)(jint status);
49 void (*hook_abort_)();
Brian Carlstrom44753c32011-08-17 22:22:11 -070050 std::tr1::unordered_set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070051 std::vector<std::string> properties_;
52
53 private:
54 ParsedOptions() {};
Brian Carlstrom8a436592011-08-15 21:27:23 -070055 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070056
Carl Shapiro61e019d2011-07-14 16:53:09 -070057 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070058 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070059 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070060
61 static Runtime* Current() {
62 return instance_;
63 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070064
Carl Shapiro61e019d2011-07-14 16:53:09 -070065 // Compiles a dex file.
66 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070067
Elliott Hughesffe67362011-07-17 12:09:27 -070068 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
69 // callers should prefer.
70 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
71 // in a single function together. This reduces code size slightly, but means
72 // that the native stack trace we get may point at the wrong call site.
73 static void Abort(const char* file, int line);
74
Carl Shapiro61e019d2011-07-14 16:53:09 -070075 // Attaches the current native thread to the runtime.
Elliott Hughes75770752011-08-24 17:52:38 -070076 bool AttachCurrentThread(const char* name, JNIEnv** jni_env, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -070077
78 // Detaches the current native thread from the runtime.
79 bool DetachCurrentThread();
80
Elliott Hughese27955c2011-08-26 15:21:24 -070081 void DumpStatistics(std::ostream& os);
82
Carl Shapiro61e019d2011-07-14 16:53:09 -070083 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070084
Brian Carlstromb765be02011-08-17 23:54:10 -070085 size_t GetStackSize() const {
86 return stack_size_;
87 }
88
89 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -070090 return class_linker_;
91 }
92
Elliott Hughes0af55432011-08-17 18:37:28 -070093 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070094 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -070095 }
96
Brian Carlstrom1f870082011-08-23 16:02:11 -070097 void VisitRoots(Heap::RootVistor* root_visitor, void* arg) const;
98
Carl Shapirob5573532011-07-12 18:22:59 -070099 private:
Elliott Hughesffe67362011-07-17 12:09:27 -0700100 static void PlatformAbort(const char*, int);
101
Brian Carlstromb765be02011-08-17 23:54:10 -0700102 Runtime() : stack_size_(0), thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -0700103
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700104 void BlockSignals();
105
Brian Carlstrom8a436592011-08-15 21:27:23 -0700106 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700107
Brian Carlstromb765be02011-08-17 23:54:10 -0700108 // The default stack size for managed threads created by the runtime.
109 size_t stack_size_;
110
Carl Shapirob5573532011-07-12 18:22:59 -0700111 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700112
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700113 ClassLinker* class_linker_;
114
Elliott Hughese27955c2011-08-26 15:21:24 -0700115 SignalCatcher* signal_catcher_;
116
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700117 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700118
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700119 // Hooks supported by JNI_CreateJavaVM
120 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
121 void (*exit_)(jint status);
122 void (*abort_)();
123
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700124 // A pointer to the active runtime or NULL.
125 static Runtime* instance_;
126
Carl Shapiro61e019d2011-07-14 16:53:09 -0700127 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700128};
129
130} // namespace art
131
Carl Shapiro1fb86202011-06-27 17:43:13 -0700132#endif // ART_SRC_RUNTIME_H_