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