blob: b18fa322bd2b7360d37892439df74f193cbed17e [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.server.am.ActivityManagerService;
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.BroadcastReceiver;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
Steven Moreland6b47c542017-03-21 12:52:16 -070029import android.hidl.manager.V1_0.IServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Debug;
31import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080032import android.os.IPowerManager;
John Michelau11641522013-03-18 18:28:23 -050033import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Process;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080035import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.SystemClock;
37import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.util.EventLog;
Dan Egnor9bdc94b2010-03-04 14:20:31 -080039import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080040import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Dan Egnor9bdc94b2010-03-04 14:20:31 -080042import java.io.File;
Colin Cross5df1d872012-11-29 11:42:11 -080043import java.io.FileWriter;
44import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import java.util.ArrayList;
Steven Moreland6b47c542017-03-21 12:52:16 -070046import java.util.Arrays;
47import java.util.HashSet;
48import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50/** This class calls its monitor every minute. Killing this process if they don't return **/
51public class Watchdog extends Thread {
52 static final String TAG = "Watchdog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 // Set this to true to use debug default values.
55 static final boolean DB = false;
56
Christopher Tateecaa7b42010-06-04 14:55:02 -070057 // 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 Tatee6f81cf2013-10-23 17:28:27 -070060 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 Project9066cfe2009-03-03 19:31:44 -080068
Igor Murashkin44d04aa2013-10-23 10:56:02 -070069 // Which native processes to dump into dropbox's stack traces
70 public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
Andy Hung3a64ecb2016-03-09 13:55:58 -080071 "/system/bin/audioserver",
Andy Hung689574a2016-04-13 19:27:43 -070072 "/system/bin/cameraserver",
73 "/system/bin/drmserver",
74 "/system/bin/mediadrmserver",
Dianne Hackbornf72467a2012-06-08 17:23:59 -070075 "/system/bin/mediaserver",
76 "/system/bin/sdcard",
Eric Laurent05d4e352016-03-14 18:49:08 -070077 "/system/bin/surfaceflinger",
Andy Hung689574a2016-04-13 19:27:43 -070078 "media.extractor", // system/bin/mediaextractor
Chong Zhange55e6e02017-06-02 10:52:04 -070079 "media.codec", // vendor/bin/hw/android.hardware.media.omx@1.0-service
Andreas Gampecf9e79b2016-05-11 18:41:25 -070080 "com.android.bluetooth", // Bluetooth service
Dianne Hackbornf72467a2012-06-08 17:23:59 -070081 };
82
Steven Moreland6b47c542017-03-21 12:52:16 -070083 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 Wu74debcd2017-04-21 11:14:22 -070087 "android.hardware.graphics.composer@2.1::IComposer",
Steven Moreland6b47c542017-03-21 12:52:16 -070088 "android.hardware.vr@1.0::IVr",
89 "android.hardware.media.omx@1.0::IOmx"
90 );
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 static Watchdog sWatchdog;
93
94 /* This handler will be used to post message back onto the main thread */
Wale Ogunwaled7fdd022015-04-13 16:22:38 -070095 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070096 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 ContentResolver mResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700101 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700102 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700105 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700107 public final class HandlerChecker implements Runnable {
108 private final Handler mHandler;
109 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700110 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700111 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700112 private boolean mCompleted;
113 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700114 private long mStartTime;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700115
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700116 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700117 mHandler = handler;
118 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700119 mWaitMax = waitMaxMillis;
120 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700121 }
122
123 public void addMonitor(Monitor monitor) {
124 mMonitors.add(monitor);
125 }
126
127 public void scheduleCheckLocked() {
Jeff Brown6c7b41a2015-02-26 14:43:53 -0800128 if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) {
129 // If the target looper has recently been polling, then
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700130 // 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 Tatee6f81cf2013-10-23 17:28:27 -0700138
139 if (!mCompleted) {
140 // we already have a check in flight, so no need
141 return;
142 }
143
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700144 mCompleted = false;
145 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700146 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700147 mHandler.postAtFrontOfQueue(this);
148 }
149
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700150 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 Hackborn8d044e82013-04-30 17:24:15 -0700166 }
167
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700168 public Thread getThread() {
169 return mHandler.getLooper().getThread();
170 }
171
172 public String getName() {
173 return mName;
174 }
175
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700176 public String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700177 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 Michelau11641522013-03-18 18:28:23 -0500183 }
184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700186 public void run() {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700187 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 Project9066cfe2009-03-03 19:31:44 -0800194
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700195 synchronized (Watchdog.this) {
196 mCompleted = true;
197 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
199 }
200 }
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 final class RebootRequestReceiver extends BroadcastReceiver {
203 @Override
204 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700205 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 Project9066cfe2009-03-03 19:31:44 -0800210 }
211 }
212
Wale Ogunwale517daec2015-04-15 10:27:24 -0700213 /** 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 Project9066cfe2009-03-03 19:31:44 -0800224 public interface Monitor {
225 void monitor();
226 }
227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 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 Hackborn8d044e82013-04-30 17:24:15 -0700238 // 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 Tatee6f81cf2013-10-23 17:28:27 -0700245 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
246 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700247 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 Tatee6f81cf2013-10-23 17:28:27 -0700251 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700252 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700253 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
254 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700255 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700256 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
257 "i/o thread", DEFAULT_TIMEOUT));
Jeff Brown4ccb8232014-01-16 22:16:42 -0800258 // And the display thread.
259 mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
260 "display thread", DEFAULT_TIMEOUT));
Wale Ogunwale517daec2015-04-15 10:27:24 -0700261
262 // Initialize monitor for Binder threads.
263 addMonitor(new BinderThreadMonitor());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265
Adam Lesinski182f73f2013-12-05 16:48:06 -0800266 public void init(Context context, ActivityManagerService activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 mResolver = context.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 mActivity = activity;
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 context.registerReceiver(new RebootRequestReceiver(),
271 new IntentFilter(Intent.ACTION_REBOOT),
272 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
274
Christopher Tatec27181c2010-06-30 14:41:09 -0700275 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 synchronized (this) {
277 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 }
280 }
281 }
282
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700283 public void setActivityController(IActivityController controller) {
284 synchronized (this) {
285 mController = controller;
286 }
287 }
288
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700289 public void setAllowRestart(boolean allowRestart) {
290 synchronized (this) {
291 mAllowRestart = allowRestart;
292 }
293 }
294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 public void addMonitor(Monitor monitor) {
296 synchronized (this) {
297 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700298 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700300 mMonitorChecker.addMonitor(monitor);
301 }
302 }
303
Jeff Brown6f357d32014-01-15 20:40:55 -0800304 public void addThread(Handler thread) {
305 addThread(thread, DEFAULT_TIMEOUT);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700306 }
307
Jeff Brown6f357d32014-01-15 20:40:55 -0800308 public void addThread(Handler thread, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700309 synchronized (this) {
310 if (isAlive()) {
311 throw new RuntimeException("Threads can't be added once the Watchdog is running");
312 }
Jeff Brown6f357d32014-01-15 20:40:55 -0800313 final String name = thread.getLooper().getThread().getName();
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700314 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316 }
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 /**
319 * Perform a full reboot of the system.
320 */
321 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800322 Slog.i(TAG, "Rebooting system because: " + reason);
Jeff Brown6f357d32014-01-15 20:40:55 -0800323 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 Project9066cfe2009-03-03 19:31:44 -0800328 }
329
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700330 private int evaluateCheckerCompletionLocked() {
331 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700332 for (int i=0; i<mHandlerCheckers.size(); i++) {
333 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700334 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700335 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700336 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700337 }
338
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700339 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
340 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700341 for (int i=0; i<mHandlerCheckers.size(); i++) {
342 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700343 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700344 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700345 }
346 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700347 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 Hackborn8d044e82013-04-30 17:24:15 -0700358 return builder.toString();
359 }
360
Steven Moreland6b47c542017-03-21 12:52:16 -0700361 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 Project9066cfe2009-03-03 19:31:44 -0800398 @Override
399 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700400 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700402 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700403 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700404 final boolean allowRestart;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700405 int debuggerWasConnected = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700407 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 Hackborn8d044e82013-04-30 17:24:15 -0700413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700415 if (debuggerWasConnected > 0) {
416 debuggerWasConnected--;
417 }
418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 // 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 Tate6ee412d2010-05-28 12:01:56 -0700421 // 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 -0800422 // positive on when to kill things.
423 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700424 while (timeout > 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700425 if (Debug.isDebuggerConnected()) {
426 debuggerWasConnected = 2;
427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700429 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800431 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700433 if (Debug.isDebuggerConnected()) {
434 debuggerWasConnected = 2;
435 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700436 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700439 final int waitState = evaluateCheckerCompletionLocked();
440 if (waitState == COMPLETED) {
441 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700442 waitedHalf = false;
443 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700444 } 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 Moreland6b47c542017-03-21 12:52:16 -0700454 getInterestingNativePids());
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700455 waitedHalf = true;
456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 continue;
458 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700459
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700460 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700461 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700462 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700463 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465
466 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700467 // First collect stack traces from all threads of the system process.
468 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700469 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
Steven Moreland6b47c542017-03-21 12:52:16 -0700471 ArrayList<Integer> pids = new ArrayList<>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800472 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800473 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700474 // 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 Fitzpatrick9765c722011-01-14 11:28:22 -0800476 final File stack = ActivityManagerService.dumpStackTraces(
Steven Moreland6b47c542017-03-21 12:52:16 -0700477 !waitedHalf, pids, null, null, getInterestingNativePids());
Dan Egnor4bded072010-03-11 22:00:47 -0800478
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 Tateecaa7b42010-06-04 14:55:02 -0700483 // Pull our own kernel thread stacks as well if we're configured for that
484 if (RECORD_KERNEL_THREADS) {
485 dumpKernelStackTraces();
486 }
487
Guang Zhu0620c452014-10-29 14:31:48 -0700488 // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log
489 doSysRq('w');
490 doSysRq('l');
Colin Cross5df1d872012-11-29 11:42:11 -0800491
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800492 // 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 Sharkeya353d262011-10-28 11:12:06 -0700498 "watchdog", null, "system_server", null, null,
Jeff Brown7dd2d192013-09-06 15:05:23 -0700499 subject, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800500 }
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 Project9066cfe2009-03-03 19:31:44 -0800506
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700507 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 Brown7dd2d192013-09-06 15:05:23 -0700516 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700517 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 Queru784827b2012-09-04 13:35:12 -0700526 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700527 if (Debug.isDebuggerConnected()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700528 debuggerWasConnected = 2;
529 }
530 if (debuggerWasConnected >= 2) {
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700531 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700532 } else if (debuggerWasConnected > 0) {
533 Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process");
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700534 } else if (!allowRestart) {
535 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
536 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700537 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700538 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 Wright56a6c662013-04-30 20:13:07 -0700545 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700546 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700547 Process.killProcess(Process.myPid());
548 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700550
551 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
553 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700554
Guang Zhu0620c452014-10-29 14:31:48 -0700555 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 Tateecaa7b42010-06-04 14:55:02 -0700565 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 Project9066cfe2009-03-03 19:31:44 -0800576}