blob: ed77d9aead08732f0f71207d40db77a515a39005 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//
18
19#ifndef _RUNTIME_ANDROID_RUNTIME_H
20#define _RUNTIME_ANDROID_RUNTIME_H
21
22#include <utils/Errors.h>
Mathias Agopian07952722009-05-19 19:08:10 -070023#include <binder/IBinder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/String8.h>
25#include <utils/String16.h>
26#include <utils/Vector.h>
27#include <utils/threads.h>
28#include <pthread.h>
29#include <nativehelper/jni.h>
30
31
32namespace android {
Mike Lockwood81ea83d2010-06-30 17:49:41 -040033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034class AndroidRuntime
35{
36public:
Narayan Kamatha23fcd72014-03-28 13:39:21 +000037 AndroidRuntime(char* argBlockStart, size_t argBlockSize);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 virtual ~AndroidRuntime();
39
Jeff Brownebed7d62011-05-16 17:08:42 -070040 enum StartMode {
41 Zygote,
42 SystemServer,
43 Application,
44 Tool,
45 };
46
Dmitriy Filchenkof5b6e552016-07-18 16:00:35 -070047 void setArgv0(const char* argv0, bool setProcName = false);
Jeff Brown00c0cd42014-09-10 16:48:46 -070048 void addOption(const char* optionString, void* extra_info = NULL);
Narayan Kamatha23fcd72014-03-28 13:39:21 +000049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 /**
51 * Register a set of methods in the specified class.
52 */
53 static int registerNativeMethods(JNIEnv* env,
54 const char* className, const JNINativeMethod* gMethods, int numMethods);
55
56 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 * Call a class's static main method with the given arguments,
58 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010059 status_t callMain(const String8& className, jclass clazz, const Vector<String8>& args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 /**
Elliott Hughesa3804cf2011-04-11 16:50:19 -070062 * Find a class, with the input either of the form
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 * "package/class" or "package.class".
64 */
65 static jclass findClass(JNIEnv* env, const char* className);
66
Sebastien Hertz7a09b832015-08-10 18:55:34 +020067 void start(const char *classname, const Vector<String8>& options, bool zygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Jeff Brown4280c4a2012-03-15 17:48:02 -070069 void exit(int code);
70
71 void setExitWithoutCleanup(bool exitWithoutCleanup) {
72 mExitWithoutCleanup = exitWithoutCleanup;
73 }
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 static AndroidRuntime* getRuntime();
Elliott Hughesa3804cf2011-04-11 16:50:19 -070076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 /**
Elliott Hughesd195e5a2011-04-13 15:39:37 -070078 * This gets called after the VM has been created, but before we
79 * run any code. Override it to make any FindClass calls that need
80 * to use CLASSPATH.
81 */
82 virtual void onVmCreated(JNIEnv* env);
83
84 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * This gets called after the JavaVM has initialized. Override it
86 * with the system's native entry point.
87 */
88 virtual void onStarted() = 0;
89
90 /**
91 * This gets called after the JavaVM has initialized after a Zygote
92 * fork. Override it to initialize threads, etc. Upon return, the
93 * correct static main will be invoked.
94 */
Jeff Brown4280c4a2012-03-15 17:48:02 -070095 virtual void onZygoteInit() { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97 /**
Jeff Brown4280c4a2012-03-15 17:48:02 -070098 * Called when the Java application exits to perform additional cleanup actions
99 * before the process is terminated.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 */
Jeff Brown4280c4a2012-03-15 17:48:02 -0700101 virtual void onExit(int code) { }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
103 /** create a new thread that is visible from Java */
Mike Lockwoodf602d362010-06-20 14:28:16 -0700104 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 void* arg);
106
107 /** return a pointer to the VM running in this process */
108 static JavaVM* getJavaVM() { return mJavaVM; }
109
110 /** return a pointer to the JNIEnv pointer for this thread */
111 static JNIEnv* getJNIEnv();
112
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700113 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
114 static char* toSlashClassName(const char* className);
115
Mike Lockwood9ee5e7e2014-09-08 16:15:21 -0700116 /** Create a Java string from an ASCII or Latin-1 string */
117 static jstring NewStringLatin1(JNIEnv* env, const char* bytes);
118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119private:
120 static int startReg(JNIEnv* env);
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700121 bool parseRuntimeOption(const char* property,
122 char* buffer,
123 const char* runtimeArg,
124 const char* defaultArg = "");
Brian Carlstrom3fbfbb42014-07-28 19:13:28 -0700125 bool parseCompilerOption(const char* property,
126 char* buffer,
127 const char* compilerArg,
128 const char* quotingArg);
Brian Carlstrom6d77eb92014-07-08 10:40:59 -0700129 bool parseCompilerRuntimeOption(const char* property,
130 char* buffer,
131 const char* runtimeArg,
132 const char* quotingArg);
Brian Carlstrom3beff1e2014-02-28 23:27:22 -0800133 void parseExtraOpts(char* extraOptsBuf, const char* quotingArg);
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200134 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv, bool zygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
136 Vector<JavaVMOption> mOptions;
Jeff Brown4280c4a2012-03-15 17:48:02 -0700137 bool mExitWithoutCleanup;
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000138 char* const mArgBlockStart;
139 const size_t mArgBlockLength;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
141 /* JNI JavaVM pointer */
142 static JavaVM* mJavaVM;
143
144 /*
145 * Thread creation helpers.
146 */
147 static int javaCreateThreadEtc(
Elliott Hughesa3804cf2011-04-11 16:50:19 -0700148 android_thread_func_t entryFunction,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 void* userData,
150 const char* threadName,
151 int32_t threadPriority,
152 size_t threadStackSize,
153 android_thread_id_t* threadId);
154 static int javaThreadShell(void* args);
155};
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157}
158
159#endif