blob: 8694b3c304f9f7cadc4fc9470cf9599894b7b5ab [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 Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "globals.h"
16#include "macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "stringpiece.h"
Brian Carlstrom44753c32011-08-17 22:22:11 -070018#include "unordered_set.h"
Carl Shapirob5573532011-07-12 18:22:59 -070019
Carl Shapiro1fb86202011-06-27 17:43:13 -070020namespace art {
21
Carl Shapiro61e019d2011-07-14 16:53:09 -070022class ClassLinker;
Carl Shapirofc322c72011-07-27 00:20:01 -070023class DexFile;
Carl Shapiro61e019d2011-07-14 16:53:09 -070024class Heap;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070025class JavaVMExt;
Elliott Hughese27955c2011-08-26 15:21:24 -070026class SignalCatcher;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070027class String;
Carl Shapiro61e019d2011-07-14 16:53:09 -070028class ThreadList;
29
Carl Shapiro1fb86202011-06-27 17:43:13 -070030class Runtime {
31 public:
Brian Carlstrom8a436592011-08-15 21:27:23 -070032
33 typedef std::vector<std::pair<StringPiece, const void*> > Options;
34
35 class ParsedOptions {
36 public:
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070037 // returns null if problem parsing and ignore_unrecognized is false
38 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070039
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -070040 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070041 const char* boot_image_;
Elliott Hughes515a5bc2011-08-17 11:08:34 -070042 bool check_jni_;
Brian Carlstrom8a436592011-08-15 21:27:23 -070043 size_t heap_initial_size_;
44 size_t heap_maximum_size_;
Brian Carlstromf734cf52011-08-17 16:28:14 -070045 size_t stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070046 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap);
47 void (*hook_exit_)(jint status);
48 void (*hook_abort_)();
Brian Carlstrom44753c32011-08-17 22:22:11 -070049 std::tr1::unordered_set<std::string> verbose_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070050 std::vector<std::string> properties_;
51
52 private:
53 ParsedOptions() {};
Brian Carlstrom8a436592011-08-15 21:27:23 -070054 };
Carl Shapiro2ed144c2011-07-26 16:52:08 -070055
Carl Shapiro61e019d2011-07-14 16:53:09 -070056 // Creates and initializes a new runtime.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070057 static Runtime* Create(const Options& options, bool ignore_unrecognized);
Brian Carlstrom8a436592011-08-15 21:27:23 -070058 static Runtime* Create(const std::vector<const DexFile*>& boot_class_path);
Carl Shapiro2ed144c2011-07-26 16:52:08 -070059
60 static Runtime* Current() {
61 return instance_;
62 }
Carl Shapiro1fb86202011-06-27 17:43:13 -070063
Carl Shapiro61e019d2011-07-14 16:53:09 -070064 // Compiles a dex file.
65 static void Compile(const StringPiece& filename);
Carl Shapirob5573532011-07-12 18:22:59 -070066
Elliott Hughesffe67362011-07-17 12:09:27 -070067 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most
68 // callers should prefer.
69 // This isn't marked ((noreturn)) because then gcc will merge multiple calls
70 // in a single function together. This reduces code size slightly, but means
71 // that the native stack trace we get may point at the wrong call site.
72 static void Abort(const char* file, int line);
73
Carl Shapiro61e019d2011-07-14 16:53:09 -070074 // Attaches the current native thread to the runtime.
Elliott Hughes75770752011-08-24 17:52:38 -070075 bool AttachCurrentThread(const char* name, JNIEnv** jni_env, bool as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -070076
77 // Detaches the current native thread from the runtime.
78 bool DetachCurrentThread();
79
Elliott Hughese27955c2011-08-26 15:21:24 -070080 void DumpStatistics(std::ostream& os);
81
Carl Shapiro61e019d2011-07-14 16:53:09 -070082 ~Runtime();
Carl Shapirob5573532011-07-12 18:22:59 -070083
Brian Carlstromb765be02011-08-17 23:54:10 -070084 size_t GetStackSize() const {
85 return stack_size_;
86 }
87
88 ClassLinker* GetClassLinker() const {
Carl Shapiro7a909592011-07-24 19:21:59 -070089 return class_linker_;
90 }
91
Elliott Hughes0af55432011-08-17 18:37:28 -070092 JavaVMExt* GetJavaVM() const {
Elliott Hughesc5f7c912011-08-18 14:00:42 -070093 return java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -070094 }
95
Carl Shapirob5573532011-07-12 18:22:59 -070096 private:
Elliott Hughesffe67362011-07-17 12:09:27 -070097 static void PlatformAbort(const char*, int);
98
Brian Carlstromb765be02011-08-17 23:54:10 -070099 Runtime() : stack_size_(0), thread_list_(NULL), class_linker_(NULL) {}
Carl Shapiro61e019d2011-07-14 16:53:09 -0700100
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700101 void BlockSignals();
102
Brian Carlstrom8a436592011-08-15 21:27:23 -0700103 bool Init(const Options& options, bool ignore_unrecognized);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700104
Brian Carlstromb765be02011-08-17 23:54:10 -0700105 // The default stack size for managed threads created by the runtime.
106 size_t stack_size_;
107
Carl Shapirob5573532011-07-12 18:22:59 -0700108 ThreadList* thread_list_;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700109
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700110 ClassLinker* class_linker_;
111
Elliott Hughese27955c2011-08-26 15:21:24 -0700112 SignalCatcher* signal_catcher_;
113
Elliott Hughesc5f7c912011-08-18 14:00:42 -0700114 JavaVMExt* java_vm_;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700115
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700116 // Hooks supported by JNI_CreateJavaVM
117 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap);
118 void (*exit_)(jint status);
119 void (*abort_)();
120
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700121 // A pointer to the active runtime or NULL.
122 static Runtime* instance_;
123
Carl Shapiro61e019d2011-07-14 16:53:09 -0700124 DISALLOW_COPY_AND_ASSIGN(Runtime);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700125};
126
127} // namespace art
128
Carl Shapiro1fb86202011-06-27 17:43:13 -0700129#endif // ART_SRC_RUNTIME_H_