blob: 6a81d3211c9d8619c139e508d074dc11c3810465 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070019import android.app.IActivityController;
20import android.os.Binder;
21import android.os.RemoteException;
Andreas Gampe27497c62017-07-21 11:41:00 -070022import com.android.internal.os.ZygoteConnectionConstants;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import com.android.server.am.ActivityManagerService;
24
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.BroadcastReceiver;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
Steven Moreland6b47c542017-03-21 12:52:16 -070030import android.hidl.manager.V1_0.IServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.Debug;
32import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080033import android.os.IPowerManager;
John Michelau11641522013-03-18 18:28:23 -050034import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Process;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080036import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.SystemClock;
38import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.util.EventLog;
Dan Egnor9bdc94b2010-03-04 14:20:31 -080040import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080041import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Dan Egnor9bdc94b2010-03-04 14:20:31 -080043import java.io.File;
Colin Cross5df1d872012-11-29 11:42:11 -080044import java.io.FileWriter;
45import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.util.ArrayList;
Steven Moreland6b47c542017-03-21 12:52:16 -070047import java.util.Arrays;
48import java.util.HashSet;
49import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51/** This class calls its monitor every minute. Killing this process if they don't return **/
52public class Watchdog extends Thread {
53 static final String TAG = "Watchdog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55 // Set this to true to use debug default values.
56 static final boolean DB = false;
57
Christopher Tateecaa7b42010-06-04 14:55:02 -070058 // Set this to true to have the watchdog record kernel thread stacks when it fires
59 static final boolean RECORD_KERNEL_THREADS = true;
60
Andreas Gampe27497c62017-07-21 11:41:00 -070061 // Note 1: Do not lower this value below thirty seconds without tightening the invoke-with
62 // timeout in com.android.internal.os.ZygoteConnection, or wrapped applications
63 // can trigger the watchdog.
64 // Note 2: The debug value is already below the wait time in ZygoteConnection. Wrapped
65 // applications may not work with a debug build. CTS will fail.
Christopher Tatee6f81cf2013-10-23 17:28:27 -070066 static final long DEFAULT_TIMEOUT = DB ? 10*1000 : 60*1000;
67 static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2;
68
69 // These are temporally ordered: larger values as lateness increases
70 static final int COMPLETED = 0;
71 static final int WAITING = 1;
72 static final int WAITED_HALF = 2;
73 static final int OVERDUE = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
Igor Murashkin44d04aa2013-10-23 10:56:02 -070075 // Which native processes to dump into dropbox's stack traces
76 public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
Andy Hung3a64ecb2016-03-09 13:55:58 -080077 "/system/bin/audioserver",
Andy Hung689574a2016-04-13 19:27:43 -070078 "/system/bin/cameraserver",
79 "/system/bin/drmserver",
80 "/system/bin/mediadrmserver",
Dianne Hackbornf72467a2012-06-08 17:23:59 -070081 "/system/bin/mediaserver",
82 "/system/bin/sdcard",
Eric Laurent05d4e352016-03-14 18:49:08 -070083 "/system/bin/surfaceflinger",
Andy Hung689574a2016-04-13 19:27:43 -070084 "media.extractor", // system/bin/mediaextractor
Chong Zhange55e6e02017-06-02 10:52:04 -070085 "media.codec", // vendor/bin/hw/android.hardware.media.omx@1.0-service
Andreas Gampecf9e79b2016-05-11 18:41:25 -070086 "com.android.bluetooth", // Bluetooth service
Dianne Hackbornf72467a2012-06-08 17:23:59 -070087 };
88
Steven Moreland6b47c542017-03-21 12:52:16 -070089 public static final List<String> HAL_INTERFACES_OF_INTEREST = Arrays.asList(
90 "android.hardware.audio@2.0::IDevicesFactory",
91 "android.hardware.bluetooth@1.0::IBluetoothHci",
92 "android.hardware.camera.provider@2.4::ICameraProvider",
Chia-I Wu74debcd2017-04-21 11:14:22 -070093 "android.hardware.graphics.composer@2.1::IComposer",
Peng Xu102122fb2017-07-11 21:12:11 -070094 "android.hardware.media.omx@1.0::IOmx",
95 "android.hardware.sensors@1.0::ISensors",
96 "android.hardware.vr@1.0::IVr"
Steven Moreland6b47c542017-03-21 12:52:16 -070097 );
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 static Watchdog sWatchdog;
100
101 /* This handler will be used to post message back onto the main thread */
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700102 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700103 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 ContentResolver mResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700108 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700109 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700112 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700114 public final class HandlerChecker implements Runnable {
115 private final Handler mHandler;
116 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700117 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700118 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700119 private boolean mCompleted;
120 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700121 private long mStartTime;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700122
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700123 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700124 mHandler = handler;
125 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700126 mWaitMax = waitMaxMillis;
127 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700128 }
129
130 public void addMonitor(Monitor monitor) {
131 mMonitors.add(monitor);
132 }
133
134 public void scheduleCheckLocked() {
Jeff Brown6c7b41a2015-02-26 14:43:53 -0800135 if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) {
136 // If the target looper has recently been polling, then
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700137 // there is no reason to enqueue our checker on it since that
138 // is as good as it not being deadlocked. This avoid having
139 // to do a context switch to check the thread. Note that we
140 // only do this if mCheckReboot is false and we have no
141 // monitors, since those would need to be executed at this point.
142 mCompleted = true;
143 return;
144 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700145
146 if (!mCompleted) {
147 // we already have a check in flight, so no need
148 return;
149 }
150
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700151 mCompleted = false;
152 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700153 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700154 mHandler.postAtFrontOfQueue(this);
155 }
156
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700157 public boolean isOverdueLocked() {
158 return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax);
159 }
160
161 public int getCompletionStateLocked() {
162 if (mCompleted) {
163 return COMPLETED;
164 } else {
165 long latency = SystemClock.uptimeMillis() - mStartTime;
166 if (latency < mWaitMax/2) {
167 return WAITING;
168 } else if (latency < mWaitMax) {
169 return WAITED_HALF;
170 }
171 }
172 return OVERDUE;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700173 }
174
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700175 public Thread getThread() {
176 return mHandler.getLooper().getThread();
177 }
178
179 public String getName() {
180 return mName;
181 }
182
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700183 public String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700184 if (mCurrentMonitor == null) {
185 return "Blocked in handler on " + mName + " (" + getThread().getName() + ")";
186 } else {
187 return "Blocked in monitor " + mCurrentMonitor.getClass().getName()
188 + " on " + mName + " (" + getThread().getName() + ")";
189 }
John Michelau11641522013-03-18 18:28:23 -0500190 }
191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700193 public void run() {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700194 final int size = mMonitors.size();
195 for (int i = 0 ; i < size ; i++) {
196 synchronized (Watchdog.this) {
197 mCurrentMonitor = mMonitors.get(i);
198 }
199 mCurrentMonitor.monitor();
200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700202 synchronized (Watchdog.this) {
203 mCompleted = true;
204 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206 }
207 }
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 final class RebootRequestReceiver extends BroadcastReceiver {
210 @Override
211 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700212 if (intent.getIntExtra("nowait", 0) != 0) {
213 rebootSystem("Received ACTION_REBOOT broadcast");
214 return;
215 }
216 Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 }
218 }
219
Wale Ogunwale517daec2015-04-15 10:27:24 -0700220 /** Monitor for checking the availability of binder threads. The monitor will block until
221 * there is a binder thread available to process in coming IPCs to make sure other processes
222 * can still communicate with the service.
223 */
224 private static final class BinderThreadMonitor implements Watchdog.Monitor {
225 @Override
226 public void monitor() {
227 Binder.blockUntilThreadAvailable();
228 }
229 }
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 public interface Monitor {
232 void monitor();
233 }
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 public static Watchdog getInstance() {
236 if (sWatchdog == null) {
237 sWatchdog = new Watchdog();
238 }
239
240 return sWatchdog;
241 }
242
243 private Watchdog() {
244 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700245 // Initialize handler checkers for each common thread we want to check. Note
246 // that we are not currently checking the background thread, since it can
247 // potentially hold longer running operations with no guarantees about the timeliness
248 // of operations there.
249
250 // The shared foreground thread is the main checker. It is where we
251 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700252 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
253 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700254 mHandlerCheckers.add(mMonitorChecker);
255 // Add checker for main thread. We only do a quick check since there
256 // can be UI running on the thread.
257 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700258 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700259 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700260 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
261 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700262 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700263 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
264 "i/o thread", DEFAULT_TIMEOUT));
Jeff Brown4ccb8232014-01-16 22:16:42 -0800265 // And the display thread.
266 mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
267 "display thread", DEFAULT_TIMEOUT));
Wale Ogunwale517daec2015-04-15 10:27:24 -0700268
269 // Initialize monitor for Binder threads.
270 addMonitor(new BinderThreadMonitor());
Andreas Gampe27497c62017-07-21 11:41:00 -0700271
272 // See the notes on DEFAULT_TIMEOUT.
273 assert DB ||
274 DEFAULT_TIMEOUT > ZygoteConnectionConstants.WRAPPED_PID_TIMEOUT_MILLIS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276
Adam Lesinski182f73f2013-12-05 16:48:06 -0800277 public void init(Context context, ActivityManagerService activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 mResolver = context.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 mActivity = activity;
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 context.registerReceiver(new RebootRequestReceiver(),
282 new IntentFilter(Intent.ACTION_REBOOT),
283 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285
Christopher Tatec27181c2010-06-30 14:41:09 -0700286 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 synchronized (this) {
288 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 }
291 }
292 }
293
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700294 public void setActivityController(IActivityController controller) {
295 synchronized (this) {
296 mController = controller;
297 }
298 }
299
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700300 public void setAllowRestart(boolean allowRestart) {
301 synchronized (this) {
302 mAllowRestart = allowRestart;
303 }
304 }
305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 public void addMonitor(Monitor monitor) {
307 synchronized (this) {
308 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700309 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700311 mMonitorChecker.addMonitor(monitor);
312 }
313 }
314
Jeff Brown6f357d32014-01-15 20:40:55 -0800315 public void addThread(Handler thread) {
316 addThread(thread, DEFAULT_TIMEOUT);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700317 }
318
Jeff Brown6f357d32014-01-15 20:40:55 -0800319 public void addThread(Handler thread, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700320 synchronized (this) {
321 if (isAlive()) {
322 throw new RuntimeException("Threads can't be added once the Watchdog is running");
323 }
Jeff Brown6f357d32014-01-15 20:40:55 -0800324 final String name = thread.getLooper().getThread().getName();
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700325 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327 }
328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 /**
330 * Perform a full reboot of the system.
331 */
332 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800333 Slog.i(TAG, "Rebooting system because: " + reason);
Jeff Brown6f357d32014-01-15 20:40:55 -0800334 IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
335 try {
336 pms.reboot(false, reason, false);
337 } catch (RemoteException ex) {
338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
340
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700341 private int evaluateCheckerCompletionLocked() {
342 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700343 for (int i=0; i<mHandlerCheckers.size(); i++) {
344 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700345 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700346 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700347 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700348 }
349
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700350 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
351 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700352 for (int i=0; i<mHandlerCheckers.size(); i++) {
353 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700354 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700355 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700356 }
357 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700358 return checkers;
359 }
360
361 private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) {
362 StringBuilder builder = new StringBuilder(128);
363 for (int i=0; i<checkers.size(); i++) {
364 if (builder.length() > 0) {
365 builder.append(", ");
366 }
367 builder.append(checkers.get(i).describeBlockedStateLocked());
368 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700369 return builder.toString();
370 }
371
Steven Moreland6b47c542017-03-21 12:52:16 -0700372 private ArrayList<Integer> getInterestingHalPids() {
373 try {
374 IServiceManager serviceManager = IServiceManager.getService();
375 ArrayList<IServiceManager.InstanceDebugInfo> dump =
376 serviceManager.debugDump();
377 HashSet<Integer> pids = new HashSet<>();
378 for (IServiceManager.InstanceDebugInfo info : dump) {
379 if (info.pid == IServiceManager.PidConstant.NO_PID) {
380 continue;
381 }
382
383 if (!HAL_INTERFACES_OF_INTEREST.contains(info.interfaceName)) {
384 continue;
385 }
386
387 pids.add(info.pid);
388 }
389 return new ArrayList<Integer>(pids);
390 } catch (RemoteException e) {
391 return new ArrayList<Integer>();
392 }
393 }
394
395 private ArrayList<Integer> getInterestingNativePids() {
396 ArrayList<Integer> pids = getInterestingHalPids();
397
398 int[] nativePids = Process.getPidsForCommands(NATIVE_STACKS_OF_INTEREST);
399 if (nativePids != null) {
400 pids.ensureCapacity(pids.size() + nativePids.length);
401 for (int i : nativePids) {
402 pids.add(i);
403 }
404 }
405
406 return pids;
407 }
408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 @Override
410 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700411 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700413 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700414 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700415 final boolean allowRestart;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700416 int debuggerWasConnected = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700418 long timeout = CHECK_INTERVAL;
419 // Make sure we (re)spin the checkers that have become idle within
420 // this wait-and-check interval
421 for (int i=0; i<mHandlerCheckers.size(); i++) {
422 HandlerChecker hc = mHandlerCheckers.get(i);
423 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700426 if (debuggerWasConnected > 0) {
427 debuggerWasConnected--;
428 }
429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
431 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700432 // to timeout on is asleep as well and won't have a chance to run, causing a false
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 // positive on when to kill things.
434 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700435 while (timeout > 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700436 if (Debug.isDebuggerConnected()) {
437 debuggerWasConnected = 2;
438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700440 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800442 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700444 if (Debug.isDebuggerConnected()) {
445 debuggerWasConnected = 2;
446 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700447 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700450 final int waitState = evaluateCheckerCompletionLocked();
451 if (waitState == COMPLETED) {
452 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700453 waitedHalf = false;
454 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700455 } else if (waitState == WAITING) {
456 // still waiting but within their configured intervals; back off and recheck
457 continue;
458 } else if (waitState == WAITED_HALF) {
459 if (!waitedHalf) {
460 // We've waited half the deadlock-detection interval. Pull a stack
461 // trace and wait another half.
462 ArrayList<Integer> pids = new ArrayList<Integer>();
463 pids.add(Process.myPid());
464 ActivityManagerService.dumpStackTraces(true, pids, null, null,
Steven Moreland6b47c542017-03-21 12:52:16 -0700465 getInterestingNativePids());
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700466 waitedHalf = true;
467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 continue;
469 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700470
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700471 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700472 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700473 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700474 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
476
477 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700478 // First collect stack traces from all threads of the system process.
479 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700480 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481
Steven Moreland6b47c542017-03-21 12:52:16 -0700482 ArrayList<Integer> pids = new ArrayList<>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800483 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800484 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700485 // Pass !waitedHalf so that just in case we somehow wind up here without having
486 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800487 final File stack = ActivityManagerService.dumpStackTraces(
Steven Moreland6b47c542017-03-21 12:52:16 -0700488 !waitedHalf, pids, null, null, getInterestingNativePids());
Dan Egnor4bded072010-03-11 22:00:47 -0800489
490 // Give some extra time to make sure the stack traces get written.
491 // The system's been hanging for a minute, another second or two won't hurt much.
492 SystemClock.sleep(2000);
493
Christopher Tateecaa7b42010-06-04 14:55:02 -0700494 // Pull our own kernel thread stacks as well if we're configured for that
495 if (RECORD_KERNEL_THREADS) {
496 dumpKernelStackTraces();
497 }
498
Guang Zhu0620c452014-10-29 14:31:48 -0700499 // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log
500 doSysRq('w');
501 doSysRq('l');
Colin Cross5df1d872012-11-29 11:42:11 -0800502
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800503 // Try to add the error to the dropbox, but assuming that the ActivityManager
504 // itself may be deadlocked. (which has happened, causing this statement to
505 // deadlock and the watchdog as a whole to be ineffective)
506 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
507 public void run() {
508 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700509 "watchdog", null, "system_server", null, null,
Jeff Brown7dd2d192013-09-06 15:05:23 -0700510 subject, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800511 }
512 };
513 dropboxThread.start();
514 try {
515 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
516 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700518 IActivityController controller;
519 synchronized (this) {
520 controller = mController;
521 }
522 if (controller != null) {
523 Slog.i(TAG, "Reporting stuck state to activity controller");
524 try {
525 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
526 // 1 = keep waiting, -1 = kill system
Jeff Brown7dd2d192013-09-06 15:05:23 -0700527 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700528 if (res >= 0) {
529 Slog.i(TAG, "Activity controller requested to coninue to wait");
530 waitedHalf = false;
531 continue;
532 }
533 } catch (RemoteException e) {
534 }
535 }
536
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700537 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700538 if (Debug.isDebuggerConnected()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700539 debuggerWasConnected = 2;
540 }
541 if (debuggerWasConnected >= 2) {
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700542 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700543 } else if (debuggerWasConnected > 0) {
544 Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process");
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700545 } else if (!allowRestart) {
546 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
547 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700548 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700549 for (int i=0; i<blockedCheckers.size(); i++) {
550 Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:");
551 StackTraceElement[] stackTrace
552 = blockedCheckers.get(i).getThread().getStackTrace();
553 for (StackTraceElement element: stackTrace) {
554 Slog.w(TAG, " at " + element);
555 }
Michael Wright56a6c662013-04-30 20:13:07 -0700556 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700557 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700558 Process.killProcess(Process.myPid());
559 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700561
562 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700565
Guang Zhu0620c452014-10-29 14:31:48 -0700566 private void doSysRq(char c) {
567 try {
568 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
569 sysrq_trigger.write(c);
570 sysrq_trigger.close();
571 } catch (IOException e) {
572 Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e);
573 }
574 }
575
Christopher Tateecaa7b42010-06-04 14:55:02 -0700576 private File dumpKernelStackTraces() {
577 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
578 if (tracesPath == null || tracesPath.length() == 0) {
579 return null;
580 }
581
582 native_dumpKernelStacks(tracesPath);
583 return new File(tracesPath);
584 }
585
586 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587}