The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.server; |
| 18 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 19 | import android.app.IActivityController; |
| 20 | import android.os.Binder; |
| 21 | import android.os.RemoteException; |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame^] | 22 | import com.android.internal.os.ZygoteConnectionConstants; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import com.android.server.am.ActivityManagerService; |
| 24 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.IntentFilter; |
| 30 | import android.os.Debug; |
| 31 | import android.os.Handler; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 32 | import android.os.IPowerManager; |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 33 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.Process; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 35 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.SystemClock; |
| 37 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.util.EventLog; |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 39 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 40 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 42 | import java.io.File; |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 43 | import java.io.FileWriter; |
| 44 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
| 47 | /** This class calls its monitor every minute. Killing this process if they don't return **/ |
| 48 | public class Watchdog extends Thread { |
| 49 | static final String TAG = "Watchdog"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | // Set this to true to use debug default values. |
| 52 | static final boolean DB = false; |
| 53 | |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 54 | // Set this to true to have the watchdog record kernel thread stacks when it fires |
| 55 | static final boolean RECORD_KERNEL_THREADS = true; |
| 56 | |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame^] | 57 | // Note 1: Do not lower this value below thirty seconds without tightening the invoke-with |
| 58 | // timeout in com.android.internal.os.ZygoteConnection, or wrapped applications |
| 59 | // can trigger the watchdog. |
| 60 | // Note 2: The debug value is already below the wait time in ZygoteConnection. Wrapped |
| 61 | // applications may not work with a debug build. CTS will fail. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 62 | static final long DEFAULT_TIMEOUT = DB ? 10*1000 : 60*1000; |
| 63 | static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2; |
| 64 | |
| 65 | // These are temporally ordered: larger values as lateness increases |
| 66 | static final int COMPLETED = 0; |
| 67 | static final int WAITING = 1; |
| 68 | static final int WAITED_HALF = 2; |
| 69 | static final int OVERDUE = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Igor Murashkin | 44d04aa | 2013-10-23 10:56:02 -0700 | [diff] [blame] | 71 | // Which native processes to dump into dropbox's stack traces |
| 72 | public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] { |
Andy Hung | 3a64ecb | 2016-03-09 13:55:58 -0800 | [diff] [blame] | 73 | "/system/bin/audioserver", |
Andy Hung | 689574a | 2016-04-13 19:27:43 -0700 | [diff] [blame] | 74 | "/system/bin/cameraserver", |
| 75 | "/system/bin/drmserver", |
| 76 | "/system/bin/mediadrmserver", |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 77 | "/system/bin/mediaserver", |
| 78 | "/system/bin/sdcard", |
Eric Laurent | 05d4e35 | 2016-03-14 18:49:08 -0700 | [diff] [blame] | 79 | "/system/bin/surfaceflinger", |
Andy Hung | 689574a | 2016-04-13 19:27:43 -0700 | [diff] [blame] | 80 | "media.codec", // system/bin/mediacodec |
| 81 | "media.extractor", // system/bin/mediaextractor |
Andreas Gampe | cf9e79b | 2016-05-11 18:41:25 -0700 | [diff] [blame] | 82 | "com.android.bluetooth", // Bluetooth service |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | static Watchdog sWatchdog; |
| 86 | |
| 87 | /* This handler will be used to post message back onto the main thread */ |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 88 | final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 89 | final HandlerChecker mMonitorChecker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | ContentResolver mResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | ActivityManagerService mActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | int mPhonePid; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 94 | IActivityController mController; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 95 | boolean mAllowRestart = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | /** |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 98 | * Used for checking status of handle threads and scheduling monitor callbacks. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 100 | public final class HandlerChecker implements Runnable { |
| 101 | private final Handler mHandler; |
| 102 | private final String mName; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 103 | private final long mWaitMax; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 104 | private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 105 | private boolean mCompleted; |
| 106 | private Monitor mCurrentMonitor; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 107 | private long mStartTime; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 108 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 109 | HandlerChecker(Handler handler, String name, long waitMaxMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 110 | mHandler = handler; |
| 111 | mName = name; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 112 | mWaitMax = waitMaxMillis; |
| 113 | mCompleted = true; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public void addMonitor(Monitor monitor) { |
| 117 | mMonitors.add(monitor); |
| 118 | } |
| 119 | |
| 120 | public void scheduleCheckLocked() { |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 121 | if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) { |
| 122 | // If the target looper has recently been polling, then |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 123 | // there is no reason to enqueue our checker on it since that |
| 124 | // is as good as it not being deadlocked. This avoid having |
| 125 | // to do a context switch to check the thread. Note that we |
| 126 | // only do this if mCheckReboot is false and we have no |
| 127 | // monitors, since those would need to be executed at this point. |
| 128 | mCompleted = true; |
| 129 | return; |
| 130 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 131 | |
| 132 | if (!mCompleted) { |
| 133 | // we already have a check in flight, so no need |
| 134 | return; |
| 135 | } |
| 136 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 137 | mCompleted = false; |
| 138 | mCurrentMonitor = null; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 139 | mStartTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 140 | mHandler.postAtFrontOfQueue(this); |
| 141 | } |
| 142 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 143 | public boolean isOverdueLocked() { |
| 144 | return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax); |
| 145 | } |
| 146 | |
| 147 | public int getCompletionStateLocked() { |
| 148 | if (mCompleted) { |
| 149 | return COMPLETED; |
| 150 | } else { |
| 151 | long latency = SystemClock.uptimeMillis() - mStartTime; |
| 152 | if (latency < mWaitMax/2) { |
| 153 | return WAITING; |
| 154 | } else if (latency < mWaitMax) { |
| 155 | return WAITED_HALF; |
| 156 | } |
| 157 | } |
| 158 | return OVERDUE; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 161 | public Thread getThread() { |
| 162 | return mHandler.getLooper().getThread(); |
| 163 | } |
| 164 | |
| 165 | public String getName() { |
| 166 | return mName; |
| 167 | } |
| 168 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 169 | public String describeBlockedStateLocked() { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 170 | if (mCurrentMonitor == null) { |
| 171 | return "Blocked in handler on " + mName + " (" + getThread().getName() + ")"; |
| 172 | } else { |
| 173 | return "Blocked in monitor " + mCurrentMonitor.getClass().getName() |
| 174 | + " on " + mName + " (" + getThread().getName() + ")"; |
| 175 | } |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 176 | } |
| 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | @Override |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 179 | public void run() { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 180 | final int size = mMonitors.size(); |
| 181 | for (int i = 0 ; i < size ; i++) { |
| 182 | synchronized (Watchdog.this) { |
| 183 | mCurrentMonitor = mMonitors.get(i); |
| 184 | } |
| 185 | mCurrentMonitor.monitor(); |
| 186 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 188 | synchronized (Watchdog.this) { |
| 189 | mCompleted = true; |
| 190 | mCurrentMonitor = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | final class RebootRequestReceiver extends BroadcastReceiver { |
| 196 | @Override |
| 197 | public void onReceive(Context c, Intent intent) { |
Dianne Hackborn | f6438b1 | 2013-05-09 18:53:48 -0700 | [diff] [blame] | 198 | if (intent.getIntExtra("nowait", 0) != 0) { |
| 199 | rebootSystem("Received ACTION_REBOOT broadcast"); |
| 200 | return; |
| 201 | } |
| 202 | Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Wale Ogunwale | 517daec | 2015-04-15 10:27:24 -0700 | [diff] [blame] | 206 | /** Monitor for checking the availability of binder threads. The monitor will block until |
| 207 | * there is a binder thread available to process in coming IPCs to make sure other processes |
| 208 | * can still communicate with the service. |
| 209 | */ |
| 210 | private static final class BinderThreadMonitor implements Watchdog.Monitor { |
| 211 | @Override |
| 212 | public void monitor() { |
| 213 | Binder.blockUntilThreadAvailable(); |
| 214 | } |
| 215 | } |
| 216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | public interface Monitor { |
| 218 | void monitor(); |
| 219 | } |
| 220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | public static Watchdog getInstance() { |
| 222 | if (sWatchdog == null) { |
| 223 | sWatchdog = new Watchdog(); |
| 224 | } |
| 225 | |
| 226 | return sWatchdog; |
| 227 | } |
| 228 | |
| 229 | private Watchdog() { |
| 230 | super("watchdog"); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 231 | // Initialize handler checkers for each common thread we want to check. Note |
| 232 | // that we are not currently checking the background thread, since it can |
| 233 | // potentially hold longer running operations with no guarantees about the timeliness |
| 234 | // of operations there. |
| 235 | |
| 236 | // The shared foreground thread is the main checker. It is where we |
| 237 | // will also dispatch monitor checks and do other work. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 238 | mMonitorChecker = new HandlerChecker(FgThread.getHandler(), |
| 239 | "foreground thread", DEFAULT_TIMEOUT); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 240 | mHandlerCheckers.add(mMonitorChecker); |
| 241 | // Add checker for main thread. We only do a quick check since there |
| 242 | // can be UI running on the thread. |
| 243 | mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()), |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 244 | "main thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 245 | // Add checker for shared UI thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 246 | mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(), |
| 247 | "ui thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 248 | // And also check IO thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 249 | mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(), |
| 250 | "i/o thread", DEFAULT_TIMEOUT)); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 251 | // And the display thread. |
| 252 | mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(), |
| 253 | "display thread", DEFAULT_TIMEOUT)); |
Wale Ogunwale | 517daec | 2015-04-15 10:27:24 -0700 | [diff] [blame] | 254 | |
| 255 | // Initialize monitor for Binder threads. |
| 256 | addMonitor(new BinderThreadMonitor()); |
Andreas Gampe | 27497c6 | 2017-07-21 11:41:00 -0700 | [diff] [blame^] | 257 | |
| 258 | // See the notes on DEFAULT_TIMEOUT. |
| 259 | assert DB || |
| 260 | DEFAULT_TIMEOUT > ZygoteConnectionConstants.WRAPPED_PID_TIMEOUT_MILLIS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 263 | public void init(Context context, ActivityManagerService activity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | mResolver = context.getContentResolver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | mActivity = activity; |
| 266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | context.registerReceiver(new RebootRequestReceiver(), |
| 268 | new IntentFilter(Intent.ACTION_REBOOT), |
| 269 | android.Manifest.permission.REBOOT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Christopher Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 272 | public void processStarted(String name, int pid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | synchronized (this) { |
| 274 | if ("com.android.phone".equals(name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | mPhonePid = pid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 280 | public void setActivityController(IActivityController controller) { |
| 281 | synchronized (this) { |
| 282 | mController = controller; |
| 283 | } |
| 284 | } |
| 285 | |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 286 | public void setAllowRestart(boolean allowRestart) { |
| 287 | synchronized (this) { |
| 288 | mAllowRestart = allowRestart; |
| 289 | } |
| 290 | } |
| 291 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | public void addMonitor(Monitor monitor) { |
| 293 | synchronized (this) { |
| 294 | if (isAlive()) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 295 | throw new RuntimeException("Monitors can't be added once the Watchdog is running"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 297 | mMonitorChecker.addMonitor(monitor); |
| 298 | } |
| 299 | } |
| 300 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 301 | public void addThread(Handler thread) { |
| 302 | addThread(thread, DEFAULT_TIMEOUT); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 305 | public void addThread(Handler thread, long timeoutMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 306 | synchronized (this) { |
| 307 | if (isAlive()) { |
| 308 | throw new RuntimeException("Threads can't be added once the Watchdog is running"); |
| 309 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 310 | final String name = thread.getLooper().getThread().getName(); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 311 | mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | /** |
| 316 | * Perform a full reboot of the system. |
| 317 | */ |
| 318 | void rebootSystem(String reason) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 319 | Slog.i(TAG, "Rebooting system because: " + reason); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 320 | IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE); |
| 321 | try { |
| 322 | pms.reboot(false, reason, false); |
| 323 | } catch (RemoteException ex) { |
| 324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 327 | private int evaluateCheckerCompletionLocked() { |
| 328 | int state = COMPLETED; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 329 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 330 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 331 | state = Math.max(state, hc.getCompletionStateLocked()); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 332 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 333 | return state; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 336 | private ArrayList<HandlerChecker> getBlockedCheckersLocked() { |
| 337 | ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 338 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 339 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 340 | if (hc.isOverdueLocked()) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 341 | checkers.add(hc); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 344 | return checkers; |
| 345 | } |
| 346 | |
| 347 | private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) { |
| 348 | StringBuilder builder = new StringBuilder(128); |
| 349 | for (int i=0; i<checkers.size(); i++) { |
| 350 | if (builder.length() > 0) { |
| 351 | builder.append(", "); |
| 352 | } |
| 353 | builder.append(checkers.get(i).describeBlockedStateLocked()); |
| 354 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 355 | return builder.toString(); |
| 356 | } |
| 357 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | @Override |
| 359 | public void run() { |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 360 | boolean waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | while (true) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 362 | final ArrayList<HandlerChecker> blockedCheckers; |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 363 | final String subject; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 364 | final boolean allowRestart; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 365 | int debuggerWasConnected = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | synchronized (this) { |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 367 | long timeout = CHECK_INTERVAL; |
| 368 | // Make sure we (re)spin the checkers that have become idle within |
| 369 | // this wait-and-check interval |
| 370 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 371 | HandlerChecker hc = mHandlerCheckers.get(i); |
| 372 | hc.scheduleCheckLocked(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 373 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 375 | if (debuggerWasConnected > 0) { |
| 376 | debuggerWasConnected--; |
| 377 | } |
| 378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | // NOTE: We use uptimeMillis() here because we do not want to increment the time we |
| 380 | // wait while asleep. If the device is asleep then the thing that we are waiting |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 381 | // to timeout on is asleep as well and won't have a chance to run, causing a false |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | // positive on when to kill things. |
| 383 | long start = SystemClock.uptimeMillis(); |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 384 | while (timeout > 0) { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 385 | if (Debug.isDebuggerConnected()) { |
| 386 | debuggerWasConnected = 2; |
| 387 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | try { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 389 | wait(timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | } catch (InterruptedException e) { |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 391 | Log.wtf(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 393 | if (Debug.isDebuggerConnected()) { |
| 394 | debuggerWasConnected = 2; |
| 395 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 396 | timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 399 | final int waitState = evaluateCheckerCompletionLocked(); |
| 400 | if (waitState == COMPLETED) { |
| 401 | // The monitors have returned; reset |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 402 | waitedHalf = false; |
| 403 | continue; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 404 | } else if (waitState == WAITING) { |
| 405 | // still waiting but within their configured intervals; back off and recheck |
| 406 | continue; |
| 407 | } else if (waitState == WAITED_HALF) { |
| 408 | if (!waitedHalf) { |
| 409 | // We've waited half the deadlock-detection interval. Pull a stack |
| 410 | // trace and wait another half. |
| 411 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
| 412 | pids.add(Process.myPid()); |
| 413 | ActivityManagerService.dumpStackTraces(true, pids, null, null, |
| 414 | NATIVE_STACKS_OF_INTEREST); |
| 415 | waitedHalf = true; |
| 416 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | continue; |
| 418 | } |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 419 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 420 | // something is overdue! |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 421 | blockedCheckers = getBlockedCheckersLocked(); |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 422 | subject = describeCheckersLocked(blockedCheckers); |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 423 | allowRestart = mAllowRestart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // If we got here, that means that the system is most likely hung. |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 427 | // First collect stack traces from all threads of the system process. |
| 428 | // Then kill this process so that the system will restart. |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 429 | EventLog.writeEvent(EventLogTags.WATCHDOG, subject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | |
Dianne Hackborn | 6b1afeb | 2010-08-31 15:40:21 -0700 | [diff] [blame] | 431 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 432 | pids.add(Process.myPid()); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 433 | if (mPhonePid > 0) pids.add(mPhonePid); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 434 | // Pass !waitedHalf so that just in case we somehow wind up here without having |
| 435 | // dumped the halfway stacks, we properly re-initialize the trace file. |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 436 | final File stack = ActivityManagerService.dumpStackTraces( |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 437 | !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 438 | |
| 439 | // Give some extra time to make sure the stack traces get written. |
| 440 | // The system's been hanging for a minute, another second or two won't hurt much. |
| 441 | SystemClock.sleep(2000); |
| 442 | |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 443 | // Pull our own kernel thread stacks as well if we're configured for that |
| 444 | if (RECORD_KERNEL_THREADS) { |
| 445 | dumpKernelStackTraces(); |
| 446 | } |
| 447 | |
Guang Zhu | 0620c45 | 2014-10-29 14:31:48 -0700 | [diff] [blame] | 448 | // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log |
| 449 | doSysRq('w'); |
| 450 | doSysRq('l'); |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 451 | |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 452 | // Try to add the error to the dropbox, but assuming that the ActivityManager |
| 453 | // itself may be deadlocked. (which has happened, causing this statement to |
| 454 | // deadlock and the watchdog as a whole to be ineffective) |
| 455 | Thread dropboxThread = new Thread("watchdogWriteToDropbox") { |
| 456 | public void run() { |
| 457 | mActivity.addErrorToDropBox( |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 458 | "watchdog", null, "system_server", null, null, |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 459 | subject, null, stack, null); |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 460 | } |
| 461 | }; |
| 462 | dropboxThread.start(); |
| 463 | try { |
| 464 | dropboxThread.join(2000); // wait up to 2 seconds for it to return. |
| 465 | } catch (InterruptedException ignored) {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 467 | IActivityController controller; |
| 468 | synchronized (this) { |
| 469 | controller = mController; |
| 470 | } |
| 471 | if (controller != null) { |
| 472 | Slog.i(TAG, "Reporting stuck state to activity controller"); |
| 473 | try { |
| 474 | Binder.setDumpDisabled("Service dumps disabled due to hung system process."); |
| 475 | // 1 = keep waiting, -1 = kill system |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 476 | int res = controller.systemNotResponding(subject); |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 477 | if (res >= 0) { |
| 478 | Slog.i(TAG, "Activity controller requested to coninue to wait"); |
| 479 | waitedHalf = false; |
| 480 | continue; |
| 481 | } |
| 482 | } catch (RemoteException e) { |
| 483 | } |
| 484 | } |
| 485 | |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 486 | // Only kill the process if the debugger is not attached. |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 487 | if (Debug.isDebuggerConnected()) { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 488 | debuggerWasConnected = 2; |
| 489 | } |
| 490 | if (debuggerWasConnected >= 2) { |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 491 | Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process"); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 492 | } else if (debuggerWasConnected > 0) { |
| 493 | Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process"); |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 494 | } else if (!allowRestart) { |
| 495 | Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process"); |
| 496 | } else { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 497 | Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject); |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 498 | for (int i=0; i<blockedCheckers.size(); i++) { |
| 499 | Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:"); |
| 500 | StackTraceElement[] stackTrace |
| 501 | = blockedCheckers.get(i).getThread().getStackTrace(); |
| 502 | for (StackTraceElement element: stackTrace) { |
| 503 | Slog.w(TAG, " at " + element); |
| 504 | } |
Michael Wright | 56a6c66 | 2013-04-30 20:13:07 -0700 | [diff] [blame] | 505 | } |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 506 | Slog.w(TAG, "*** GOODBYE!"); |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 507 | Process.killProcess(Process.myPid()); |
| 508 | System.exit(10); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | } |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 510 | |
| 511 | waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | } |
| 513 | } |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 514 | |
Guang Zhu | 0620c45 | 2014-10-29 14:31:48 -0700 | [diff] [blame] | 515 | private void doSysRq(char c) { |
| 516 | try { |
| 517 | FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger"); |
| 518 | sysrq_trigger.write(c); |
| 519 | sysrq_trigger.close(); |
| 520 | } catch (IOException e) { |
| 521 | Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e); |
| 522 | } |
| 523 | } |
| 524 | |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 525 | private File dumpKernelStackTraces() { |
| 526 | String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null); |
| 527 | if (tracesPath == null || tracesPath.length() == 0) { |
| 528 | return null; |
| 529 | } |
| 530 | |
| 531 | native_dumpKernelStacks(tracesPath); |
| 532 | return new File(tracesPath); |
| 533 | } |
| 534 | |
| 535 | private native void native_dumpKernelStacks(String tracesPath); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | } |