The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | package com.android.internal.os; |
| 18 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Eric Rowe | 35980b2 | 2013-09-03 10:51:15 -0700 | [diff] [blame] | 20 | import android.app.ActivityThread; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 21 | import android.app.ApplicationErrorReport; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 22 | import android.os.Build; |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 23 | import android.os.DeadObjectException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.os.Debug; |
| 25 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.os.SystemProperties; |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 28 | import android.os.Trace; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.util.Log; |
Dianne Hackborn | c9421ba | 2010-03-11 22:23:46 -0800 | [diff] [blame] | 30 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import com.android.internal.logging.AndroidConfig; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 32 | import com.android.server.NetworkManagementSocketTagger; |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 33 | import dalvik.system.RuntimeHooks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import dalvik.system.VMRuntime; |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 35 | import java.lang.reflect.InvocationTargetException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import java.lang.reflect.Method; |
| 37 | import java.lang.reflect.Modifier; |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 38 | import java.util.Objects; |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 39 | import java.util.logging.LogManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Main entry point for runtime initialization. Not for |
| 43 | * public consumption. |
| 44 | * @hide |
| 45 | */ |
| 46 | public class RuntimeInit { |
Andreas Gampe | 76d4fc8 | 2017-02-07 19:44:37 -0800 | [diff] [blame] | 47 | final static String TAG = "AndroidRuntime"; |
| 48 | final static boolean DEBUG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
| 50 | /** true if commonInit() has been called */ |
| 51 | private static boolean initialized; |
| 52 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 53 | private static IBinder mApplicationObject; |
| 54 | |
| 55 | private static volatile boolean mCrashing = false; |
| 56 | |
Jeff Brown | 16f5f5c | 2012-03-15 16:53:55 -0700 | [diff] [blame] | 57 | private static final native void nativeFinishInit(); |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 58 | private static final native void nativeSetExitWithoutCleanup(boolean exitWithoutCleanup); |
Jeff Brown | 16f5f5c | 2012-03-15 16:53:55 -0700 | [diff] [blame] | 59 | |
Mark Salyzyn | 69eb6f5 | 2014-04-09 07:39:15 -0700 | [diff] [blame] | 60 | private static int Clog_e(String tag, String msg, Throwable tr) { |
Andreas Gampe | d888beb | 2016-02-18 14:01:41 -0800 | [diff] [blame] | 61 | return Log.printlns(Log.LOG_ID_CRASH, Log.ERROR, tag, msg, tr); |
Mark Salyzyn | 69eb6f5 | 2014-04-09 07:39:15 -0700 | [diff] [blame] | 62 | } |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /** |
Tobias Thierer | addbf90 | 2016-07-21 15:05:19 +0100 | [diff] [blame] | 65 | * Logs a message when a thread encounters an uncaught exception. By |
| 66 | * default, {@link KillApplicationHandler} will terminate this process later, |
| 67 | * but apps can override that behavior. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | */ |
Tobias Thierer | addbf90 | 2016-07-21 15:05:19 +0100 | [diff] [blame] | 69 | private static class LoggingHandler implements Thread.UncaughtExceptionHandler { |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 70 | public volatile boolean mTriggered = false; |
| 71 | |
Tobias Thierer | addbf90 | 2016-07-21 15:05:19 +0100 | [diff] [blame] | 72 | @Override |
| 73 | public void uncaughtException(Thread t, Throwable e) { |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 74 | mTriggered = true; |
| 75 | |
Tobias Thierer | addbf90 | 2016-07-21 15:05:19 +0100 | [diff] [blame] | 76 | // Don't re-enter if KillApplicationHandler has already run |
| 77 | if (mCrashing) return; |
yuanhuihui | e669ac2 | 2017-03-18 19:34:31 +0800 | [diff] [blame] | 78 | |
| 79 | // mApplicationObject is null for non-zygote java programs (e.g. "am") |
| 80 | // There are also apps running with the system UID. We don't want the |
| 81 | // first clause in either of these two cases, only for system_server. |
| 82 | if (mApplicationObject == null && (Process.SYSTEM_UID == Process.myUid())) { |
Tobias Thierer | addbf90 | 2016-07-21 15:05:19 +0100 | [diff] [blame] | 83 | Clog_e(TAG, "*** FATAL EXCEPTION IN SYSTEM PROCESS: " + t.getName(), e); |
| 84 | } else { |
| 85 | StringBuilder message = new StringBuilder(); |
| 86 | // The "FATAL EXCEPTION" string is still used on Android even though |
| 87 | // apps can set a custom UncaughtExceptionHandler that renders uncaught |
| 88 | // exceptions non-fatal. |
| 89 | message.append("FATAL EXCEPTION: ").append(t.getName()).append("\n"); |
| 90 | final String processName = ActivityThread.currentProcessName(); |
| 91 | if (processName != null) { |
| 92 | message.append("Process: ").append(processName).append(", "); |
| 93 | } |
| 94 | message.append("PID: ").append(Process.myPid()); |
| 95 | Clog_e(TAG, message.toString(), e); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Handle application death from an uncaught exception. The framework |
| 102 | * catches these for the main threads, so this should only matter for |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 103 | * threads created by applications. Before this method runs, the given |
| 104 | * instance of {@link LoggingHandler} should already have logged details |
| 105 | * (and if not it is run first). |
Tobias Thierer | addbf90 | 2016-07-21 15:05:19 +0100 | [diff] [blame] | 106 | */ |
| 107 | private static class KillApplicationHandler implements Thread.UncaughtExceptionHandler { |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 108 | private final LoggingHandler mLoggingHandler; |
| 109 | |
| 110 | /** |
| 111 | * Create a new KillApplicationHandler that follows the given LoggingHandler. |
| 112 | * If {@link #uncaughtException(Thread, Throwable) uncaughtException} is called |
| 113 | * on the created instance without {@code loggingHandler} having been triggered, |
| 114 | * {@link LoggingHandler#uncaughtException(Thread, Throwable) |
| 115 | * loggingHandler.uncaughtException} will be called first. |
| 116 | * |
| 117 | * @param loggingHandler the {@link LoggingHandler} expected to have run before |
| 118 | * this instance's {@link #uncaughtException(Thread, Throwable) uncaughtException} |
| 119 | * is being called. |
| 120 | */ |
| 121 | public KillApplicationHandler(LoggingHandler loggingHandler) { |
| 122 | this.mLoggingHandler = Objects.requireNonNull(loggingHandler); |
| 123 | } |
| 124 | |
| 125 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | public void uncaughtException(Thread t, Throwable e) { |
| 127 | try { |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 128 | ensureLogging(t, e); |
| 129 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 130 | // Don't re-enter -- avoid infinite loops if crash-reporting crashes. |
| 131 | if (mCrashing) return; |
| 132 | mCrashing = true; |
| 133 | |
Andreas Gampe | 4c79fea | 2016-01-28 20:11:41 -0800 | [diff] [blame] | 134 | // Try to end profiling. If a profiler is running at this point, and we kill the |
| 135 | // process (below), the in-memory buffer will be lost. So try to stop, which will |
| 136 | // flush the buffer. (This makes method trace profiling useful to debug crashes.) |
| 137 | if (ActivityThread.currentActivityThread() != null) { |
| 138 | ActivityThread.currentActivityThread().stopProfiling(); |
| 139 | } |
| 140 | |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 141 | // Bring up crash dialog, wait for it to be dismissed |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 142 | ActivityManager.getService().handleApplicationCrash( |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 143 | mApplicationObject, new ApplicationErrorReport.ParcelableCrashInfo(e)); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 144 | } catch (Throwable t2) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 145 | if (t2 instanceof DeadObjectException) { |
| 146 | // System process is dead; ignore |
| 147 | } else { |
| 148 | try { |
| 149 | Clog_e(TAG, "Error reporting crash", t2); |
| 150 | } catch (Throwable t3) { |
| 151 | // Even Clog_e() fails! Oh well. |
| 152 | } |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 153 | } |
| 154 | } finally { |
| 155 | // Try everything to make sure this process goes away. |
| 156 | Process.killProcess(Process.myPid()); |
| 157 | System.exit(10); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Ensures that the logging handler has been triggered. |
| 163 | * |
| 164 | * See b/73380984. This reinstates the pre-O behavior of |
| 165 | * |
| 166 | * {@code thread.getUncaughtExceptionHandler().uncaughtException(thread, e);} |
| 167 | * |
| 168 | * logging the exception (in addition to killing the app). This behavior |
| 169 | * was never documented / guaranteed but helps in diagnostics of apps |
| 170 | * using the pattern. |
| 171 | * |
| 172 | * If this KillApplicationHandler is invoked the "regular" way (by |
| 173 | * {@link Thread#dispatchUncaughtException(Throwable) |
| 174 | * Thread.dispatchUncaughtException} in case of an uncaught exception) |
| 175 | * then the pre-handler (expected to be {@link #mLoggingHandler}) will already |
| 176 | * have run. Otherwise, we manually invoke it here. |
| 177 | */ |
| 178 | private void ensureLogging(Thread t, Throwable e) { |
| 179 | if (!mLoggingHandler.mTriggered) { |
| 180 | try { |
| 181 | mLoggingHandler.uncaughtException(t, e); |
| 182 | } catch (Throwable loggingThrowable) { |
| 183 | // Ignored. |
| 184 | } |
| 185 | } |
| 186 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Andreas Gampe | 76d4fc8 | 2017-02-07 19:44:37 -0800 | [diff] [blame] | 189 | protected static final void commonInit() { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 190 | if (DEBUG) Slog.d(TAG, "Entered RuntimeInit!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
Tobias Thierer | addbf90 | 2016-07-21 15:05:19 +0100 | [diff] [blame] | 192 | /* |
| 193 | * set handlers; these apply to all threads in the VM. Apps can replace |
| 194 | * the default handler, but not the pre handler. |
| 195 | */ |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 196 | LoggingHandler loggingHandler = new LoggingHandler(); |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 197 | RuntimeHooks.setUncaughtExceptionPreHandler(loggingHandler); |
Andreas Gampe | 50fa122 | 2018-04-02 14:00:47 -0700 | [diff] [blame] | 198 | Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | /* |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 201 | * Install a time zone supplier that uses the Android persistent time zone system property. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | */ |
Neil Fuller | a84056a | 2018-07-11 13:59:45 +0100 | [diff] [blame] | 203 | RuntimeHooks.setTimeZoneIdSupplier(() -> SystemProperties.get("persist.sys.timezone")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Sets handler for java.util.logging to use Android log facilities. |
| 207 | * The odd "new instance-and-then-throw-away" is a mirror of how |
| 208 | * the "java.util.logging.config.class" system property works. We |
| 209 | * can't use the system property here since the logger has almost |
| 210 | * certainly already been initialized. |
| 211 | */ |
| 212 | LogManager.getLogManager().reset(); |
| 213 | new AndroidConfig(); |
| 214 | |
| 215 | /* |
Jesse Wilson | d0f80d4 | 2009-09-18 18:06:43 -0700 | [diff] [blame] | 216 | * Sets the default HTTP User-Agent used by HttpURLConnection. |
| 217 | */ |
| 218 | String userAgent = getDefaultUserAgent(); |
| 219 | System.setProperty("http.agent", userAgent); |
| 220 | |
| 221 | /* |
Jesse Wilson | 8568db5 | 2011-06-28 19:06:31 -0700 | [diff] [blame] | 222 | * Wire socket tagging to traffic stats. |
| 223 | */ |
| 224 | NetworkManagementSocketTagger.install(); |
| 225 | |
| 226 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | * If we're running in an emulator launched with "-trace", put the |
| 228 | * VM into emulator trace profiling mode so that the user can hit |
| 229 | * F9/F10 at any time to capture traces. This has performance |
| 230 | * consequences, so it's not something you want to do always. |
| 231 | */ |
| 232 | String trace = SystemProperties.get("ro.kernel.android.tracing"); |
| 233 | if (trace.equals("1")) { |
Dianne Hackborn | c9421ba | 2010-03-11 22:23:46 -0800 | [diff] [blame] | 234 | Slog.i(TAG, "NOTE: emulator trace profiling enabled"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | Debug.enableEmulatorTraceOutput(); |
| 236 | } |
| 237 | |
| 238 | initialized = true; |
| 239 | } |
| 240 | |
| 241 | /** |
Jesse Wilson | d0f80d4 | 2009-09-18 18:06:43 -0700 | [diff] [blame] | 242 | * Returns an HTTP user agent of the form |
| 243 | * "Dalvik/1.1.0 (Linux; U; Android Eclair Build/MASTER)". |
| 244 | */ |
| 245 | private static String getDefaultUserAgent() { |
| 246 | StringBuilder result = new StringBuilder(64); |
| 247 | result.append("Dalvik/"); |
| 248 | result.append(System.getProperty("java.vm.version")); // such as 1.1.0 |
| 249 | result.append(" (Linux; U; Android "); |
| 250 | |
| 251 | String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5" |
| 252 | result.append(version.length() > 0 ? version : "1.0"); |
| 253 | |
| 254 | // add the model for the release build |
| 255 | if ("REL".equals(Build.VERSION.CODENAME)) { |
| 256 | String model = Build.MODEL; |
| 257 | if (model.length() > 0) { |
| 258 | result.append("; "); |
| 259 | result.append(model); |
| 260 | } |
| 261 | } |
| 262 | String id = Build.ID; // "MASTER" or "M4-rc20" |
| 263 | if (id.length() > 0) { |
| 264 | result.append(" Build/"); |
| 265 | result.append(id); |
| 266 | } |
| 267 | result.append(")"); |
| 268 | return result.toString(); |
| 269 | } |
| 270 | |
| 271 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | * Invokes a static "main(argv[]) method on class "className". |
| 273 | * Converts various failing exceptions into RuntimeExceptions, with |
| 274 | * the assumption that they will then cause the VM instance to exit. |
| 275 | * |
| 276 | * @param className Fully-qualified class name |
| 277 | * @param argv Argument vector for main() |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 278 | * @param classLoader the classLoader to load {@className} with |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | */ |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 280 | protected static Runnable findStaticMain(String className, String[] argv, |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 281 | ClassLoader classLoader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | Class<?> cl; |
| 283 | |
| 284 | try { |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 285 | cl = Class.forName(className, true, classLoader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } catch (ClassNotFoundException ex) { |
| 287 | throw new RuntimeException( |
| 288 | "Missing class when invoking static main " + className, |
| 289 | ex); |
| 290 | } |
| 291 | |
| 292 | Method m; |
| 293 | try { |
| 294 | m = cl.getMethod("main", new Class[] { String[].class }); |
| 295 | } catch (NoSuchMethodException ex) { |
| 296 | throw new RuntimeException( |
| 297 | "Missing static main on " + className, ex); |
| 298 | } catch (SecurityException ex) { |
| 299 | throw new RuntimeException( |
| 300 | "Problem getting static main on " + className, ex); |
| 301 | } |
| 302 | |
| 303 | int modifiers = m.getModifiers(); |
| 304 | if (! (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) { |
| 305 | throw new RuntimeException( |
| 306 | "Main method is not public and static on " + className); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * This throw gets caught in ZygoteInit.main(), which responds |
| 311 | * by invoking the exception's run() method. This arrangement |
| 312 | * clears up all the stack frames that were required in setting |
| 313 | * up the process. |
| 314 | */ |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 315 | return new MethodAndArgsCaller(m, argv); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | public static final void main(String[] argv) { |
Piotr Jastrzebski | da74a62 | 2015-02-12 13:55:23 +0000 | [diff] [blame] | 319 | enableDdms(); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 320 | if (argv.length == 2 && argv[1].equals("application")) { |
| 321 | if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting application"); |
| 322 | redirectLogStreams(); |
| 323 | } else { |
| 324 | if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting tool"); |
| 325 | } |
| 326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | commonInit(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | /* |
| 330 | * Now that we're running in interpreted code, call back into native code |
| 331 | * to run the system. |
| 332 | */ |
Jeff Brown | 16f5f5c | 2012-03-15 16:53:55 -0700 | [diff] [blame] | 333 | nativeFinishInit(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 335 | if (DEBUG) Slog.d(TAG, "Leaving RuntimeInit!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | } |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 337 | |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 338 | protected static Runnable applicationInit(int targetSdkVersion, String[] argv, |
| 339 | ClassLoader classLoader) { |
Jeff Brown | 4280c4a | 2012-03-15 17:48:02 -0700 | [diff] [blame] | 340 | // If the application calls System.exit(), terminate the process |
| 341 | // immediately without running any shutdown hooks. It is not possible to |
| 342 | // shutdown an Android application gracefully. Among other things, the |
| 343 | // Android runtime shutdown hooks close the Binder driver, which can cause |
| 344 | // leftover running threads to crash before the process actually exits. |
| 345 | nativeSetExitWithoutCleanup(true); |
| 346 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 347 | // We want to be fairly aggressive about heap utilization, to avoid |
| 348 | // holding on to a lot of memory that isn't needed. |
| 349 | VMRuntime.getRuntime().setTargetHeapUtilization(0.75f); |
Elliott Hughes | e1dfcb7 | 2011-07-08 11:08:07 -0700 | [diff] [blame] | 350 | VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 351 | |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 352 | final Arguments args = new Arguments(argv); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 354 | // The end of of the RuntimeInit event (see #zygoteInit). |
| 355 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | // Remaining arguments are passed to the start class's static main |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 358 | return findStaticMain(args.startClass, args.startArgs, classLoader); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 359 | } |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 360 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 361 | /** |
| 362 | * Redirect System.out and System.err to the Android log. |
| 363 | */ |
| 364 | public static void redirectLogStreams() { |
| 365 | System.out.close(); |
| 366 | System.setOut(new AndroidPrintStream(Log.INFO, "System.out")); |
| 367 | System.err.close(); |
| 368 | System.setErr(new AndroidPrintStream(Log.WARN, "System.err")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | } |
| 370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | /** |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 372 | * Report a serious error in the current process. May or may not cause |
| 373 | * the process to terminate (depends on system settings). |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 374 | * |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 375 | * @param tag to record with the error |
| 376 | * @param t exception describing the error site and conditions |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | */ |
Dianne Hackborn | 5232271 | 2014-08-26 22:47:26 -0700 | [diff] [blame] | 378 | public static void wtf(String tag, Throwable t, boolean system) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 379 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 380 | if (ActivityManager.getService().handleApplicationWtf( |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 381 | mApplicationObject, tag, system, |
| 382 | new ApplicationErrorReport.ParcelableCrashInfo(t))) { |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 383 | // The Activity Manager has already written us off -- now exit. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | Process.killProcess(Process.myPid()); |
| 385 | System.exit(10); |
| 386 | } |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 387 | } catch (Throwable t2) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 388 | if (t2 instanceof DeadObjectException) { |
| 389 | // System process is dead; ignore |
| 390 | } else { |
| 391 | Slog.e(TAG, "Error reporting WTF", t2); |
| 392 | Slog.e(TAG, "Original WTF:", t); |
| 393 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | * Set the object identifying this application/process, for reporting VM |
| 399 | * errors. |
| 400 | */ |
| 401 | public static final void setApplicationObject(IBinder app) { |
| 402 | mApplicationObject = app; |
| 403 | } |
| 404 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 405 | public static final IBinder getApplicationObject() { |
| 406 | return mApplicationObject; |
| 407 | } |
| 408 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | /** |
Piotr Jastrzebski | da74a62 | 2015-02-12 13:55:23 +0000 | [diff] [blame] | 410 | * Enable DDMS. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | */ |
Piotr Jastrzebski | da74a62 | 2015-02-12 13:55:23 +0000 | [diff] [blame] | 412 | static final void enableDdms() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | // Register handlers for DDM messages. |
| 414 | android.ddm.DdmRegister.registerHandlers(); |
| 415 | } |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * Handles argument parsing for args related to the runtime. |
| 419 | * |
| 420 | * Current recognized args: |
| 421 | * <ul> |
| 422 | * <li> <code> [--] <start class name> <args> |
| 423 | * </ul> |
| 424 | */ |
| 425 | static class Arguments { |
| 426 | /** first non-option argument */ |
| 427 | String startClass; |
| 428 | |
| 429 | /** all following arguments */ |
| 430 | String[] startArgs; |
| 431 | |
| 432 | /** |
| 433 | * Constructs instance and parses args |
| 434 | * @param args runtime command-line args |
| 435 | * @throws IllegalArgumentException |
| 436 | */ |
| 437 | Arguments(String args[]) throws IllegalArgumentException { |
| 438 | parseArgs(args); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Parses the commandline arguments intended for the Runtime. |
| 443 | */ |
| 444 | private void parseArgs(String args[]) |
| 445 | throws IllegalArgumentException { |
| 446 | int curArg = 0; |
| 447 | for (; curArg < args.length; curArg++) { |
| 448 | String arg = args[curArg]; |
| 449 | |
| 450 | if (arg.equals("--")) { |
| 451 | curArg++; |
| 452 | break; |
| 453 | } else if (!arg.startsWith("--")) { |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | if (curArg == args.length) { |
| 459 | throw new IllegalArgumentException("Missing classname argument to RuntimeInit!"); |
| 460 | } |
| 461 | |
| 462 | startClass = args[curArg++]; |
| 463 | startArgs = new String[args.length - curArg]; |
| 464 | System.arraycopy(args, curArg, startArgs, 0, startArgs.length); |
| 465 | } |
| 466 | } |
Narayan Kamath | ac0b4be | 2017-07-05 14:45:38 +0100 | [diff] [blame] | 467 | |
| 468 | /** |
| 469 | * Helper class which holds a method and arguments and can call them. This is used as part of |
| 470 | * a trampoline to get rid of the initial process setup stack frames. |
| 471 | */ |
| 472 | static class MethodAndArgsCaller implements Runnable { |
| 473 | /** method to call */ |
| 474 | private final Method mMethod; |
| 475 | |
| 476 | /** argument array */ |
| 477 | private final String[] mArgs; |
| 478 | |
| 479 | public MethodAndArgsCaller(Method method, String[] args) { |
| 480 | mMethod = method; |
| 481 | mArgs = args; |
| 482 | } |
| 483 | |
| 484 | public void run() { |
| 485 | try { |
| 486 | mMethod.invoke(null, new Object[] { mArgs }); |
| 487 | } catch (IllegalAccessException ex) { |
| 488 | throw new RuntimeException(ex); |
| 489 | } catch (InvocationTargetException ex) { |
| 490 | Throwable cause = ex.getCause(); |
| 491 | if (cause instanceof RuntimeException) { |
| 492 | throw (RuntimeException) cause; |
| 493 | } else if (cause instanceof Error) { |
| 494 | throw (Error) cause; |
| 495 | } |
| 496 | throw new RuntimeException(ex); |
| 497 | } |
| 498 | } |
| 499 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | } |