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