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