blob: 49a788940983fb11487cebd4e033ff603cd6b490 [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;
30import 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
47/** This class calls its monitor every minute. Killing this process if they don't return **/
48public class Watchdog extends Thread {
49 static final String TAG = "Watchdog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
51 // Set this to true to use debug default values.
52 static final boolean DB = false;
53
Christopher Tateecaa7b42010-06-04 14:55:02 -070054 // Set this to true to have the watchdog record kernel thread stacks when it fires
55 static final boolean RECORD_KERNEL_THREADS = true;
56
Andreas Gampe27497c62017-07-21 11:41:00 -070057 // Note 1: Do not lower this value below thirty seconds without tightening the invoke-with
58 // timeout in com.android.internal.os.ZygoteConnection, or wrapped applications
59 // can trigger the watchdog.
60 // Note 2: The debug value is already below the wait time in ZygoteConnection. Wrapped
61 // applications may not work with a debug build. CTS will fail.
Christopher Tatee6f81cf2013-10-23 17:28:27 -070062 static final long DEFAULT_TIMEOUT = DB ? 10*1000 : 60*1000;
63 static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2;
64
65 // These are temporally ordered: larger values as lateness increases
66 static final int COMPLETED = 0;
67 static final int WAITING = 1;
68 static final int WAITED_HALF = 2;
69 static final int OVERDUE = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Igor Murashkin44d04aa2013-10-23 10:56:02 -070071 // Which native processes to dump into dropbox's stack traces
72 public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
Andy Hung3a64ecb2016-03-09 13:55:58 -080073 "/system/bin/audioserver",
Andy Hung689574a2016-04-13 19:27:43 -070074 "/system/bin/cameraserver",
75 "/system/bin/drmserver",
76 "/system/bin/mediadrmserver",
Dianne Hackbornf72467a2012-06-08 17:23:59 -070077 "/system/bin/mediaserver",
78 "/system/bin/sdcard",
Eric Laurent05d4e352016-03-14 18:49:08 -070079 "/system/bin/surfaceflinger",
Andy Hung689574a2016-04-13 19:27:43 -070080 "media.codec", // system/bin/mediacodec
81 "media.extractor", // system/bin/mediaextractor
Andreas Gampecf9e79b2016-05-11 18:41:25 -070082 "com.android.bluetooth", // Bluetooth service
Dianne Hackbornf72467a2012-06-08 17:23:59 -070083 };
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 static Watchdog sWatchdog;
86
87 /* This handler will be used to post message back onto the main thread */
Wale Ogunwaled7fdd022015-04-13 16:22:38 -070088 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070089 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 ContentResolver mResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070094 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -070095 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -070098 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700100 public final class HandlerChecker implements Runnable {
101 private final Handler mHandler;
102 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700103 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700104 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700105 private boolean mCompleted;
106 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700107 private long mStartTime;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700108
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700109 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700110 mHandler = handler;
111 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700112 mWaitMax = waitMaxMillis;
113 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700114 }
115
116 public void addMonitor(Monitor monitor) {
117 mMonitors.add(monitor);
118 }
119
120 public void scheduleCheckLocked() {
Jeff Brown6c7b41a2015-02-26 14:43:53 -0800121 if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) {
122 // If the target looper has recently been polling, then
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700123 // there is no reason to enqueue our checker on it since that
124 // is as good as it not being deadlocked. This avoid having
125 // to do a context switch to check the thread. Note that we
126 // only do this if mCheckReboot is false and we have no
127 // monitors, since those would need to be executed at this point.
128 mCompleted = true;
129 return;
130 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700131
132 if (!mCompleted) {
133 // we already have a check in flight, so no need
134 return;
135 }
136
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700137 mCompleted = false;
138 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700139 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700140 mHandler.postAtFrontOfQueue(this);
141 }
142
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700143 public boolean isOverdueLocked() {
144 return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax);
145 }
146
147 public int getCompletionStateLocked() {
148 if (mCompleted) {
149 return COMPLETED;
150 } else {
151 long latency = SystemClock.uptimeMillis() - mStartTime;
152 if (latency < mWaitMax/2) {
153 return WAITING;
154 } else if (latency < mWaitMax) {
155 return WAITED_HALF;
156 }
157 }
158 return OVERDUE;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700159 }
160
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700161 public Thread getThread() {
162 return mHandler.getLooper().getThread();
163 }
164
165 public String getName() {
166 return mName;
167 }
168
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700169 public String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700170 if (mCurrentMonitor == null) {
171 return "Blocked in handler on " + mName + " (" + getThread().getName() + ")";
172 } else {
173 return "Blocked in monitor " + mCurrentMonitor.getClass().getName()
174 + " on " + mName + " (" + getThread().getName() + ")";
175 }
John Michelau11641522013-03-18 18:28:23 -0500176 }
177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700179 public void run() {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700180 final int size = mMonitors.size();
181 for (int i = 0 ; i < size ; i++) {
182 synchronized (Watchdog.this) {
183 mCurrentMonitor = mMonitors.get(i);
184 }
185 mCurrentMonitor.monitor();
186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700188 synchronized (Watchdog.this) {
189 mCompleted = true;
190 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
192 }
193 }
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 final class RebootRequestReceiver extends BroadcastReceiver {
196 @Override
197 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700198 if (intent.getIntExtra("nowait", 0) != 0) {
199 rebootSystem("Received ACTION_REBOOT broadcast");
200 return;
201 }
202 Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
204 }
205
Wale Ogunwale517daec2015-04-15 10:27:24 -0700206 /** Monitor for checking the availability of binder threads. The monitor will block until
207 * there is a binder thread available to process in coming IPCs to make sure other processes
208 * can still communicate with the service.
209 */
210 private static final class BinderThreadMonitor implements Watchdog.Monitor {
211 @Override
212 public void monitor() {
213 Binder.blockUntilThreadAvailable();
214 }
215 }
216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 public interface Monitor {
218 void monitor();
219 }
220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static Watchdog getInstance() {
222 if (sWatchdog == null) {
223 sWatchdog = new Watchdog();
224 }
225
226 return sWatchdog;
227 }
228
229 private Watchdog() {
230 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700231 // Initialize handler checkers for each common thread we want to check. Note
232 // that we are not currently checking the background thread, since it can
233 // potentially hold longer running operations with no guarantees about the timeliness
234 // of operations there.
235
236 // The shared foreground thread is the main checker. It is where we
237 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700238 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
239 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700240 mHandlerCheckers.add(mMonitorChecker);
241 // Add checker for main thread. We only do a quick check since there
242 // can be UI running on the thread.
243 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700244 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700245 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700246 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
247 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700248 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700249 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
250 "i/o thread", DEFAULT_TIMEOUT));
Jeff Brown4ccb8232014-01-16 22:16:42 -0800251 // And the display thread.
252 mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
253 "display thread", DEFAULT_TIMEOUT));
Wale Ogunwale517daec2015-04-15 10:27:24 -0700254
255 // Initialize monitor for Binder threads.
256 addMonitor(new BinderThreadMonitor());
Andreas Gampe27497c62017-07-21 11:41:00 -0700257
258 // See the notes on DEFAULT_TIMEOUT.
259 assert DB ||
260 DEFAULT_TIMEOUT > ZygoteConnectionConstants.WRAPPED_PID_TIMEOUT_MILLIS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 }
262
Adam Lesinski182f73f2013-12-05 16:48:06 -0800263 public void init(Context context, ActivityManagerService activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 mResolver = context.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mActivity = activity;
266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 context.registerReceiver(new RebootRequestReceiver(),
268 new IntentFilter(Intent.ACTION_REBOOT),
269 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271
Christopher Tatec27181c2010-06-30 14:41:09 -0700272 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 synchronized (this) {
274 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277 }
278 }
279
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700280 public void setActivityController(IActivityController controller) {
281 synchronized (this) {
282 mController = controller;
283 }
284 }
285
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700286 public void setAllowRestart(boolean allowRestart) {
287 synchronized (this) {
288 mAllowRestart = allowRestart;
289 }
290 }
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 public void addMonitor(Monitor monitor) {
293 synchronized (this) {
294 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700295 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700297 mMonitorChecker.addMonitor(monitor);
298 }
299 }
300
Jeff Brown6f357d32014-01-15 20:40:55 -0800301 public void addThread(Handler thread) {
302 addThread(thread, DEFAULT_TIMEOUT);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700303 }
304
Jeff Brown6f357d32014-01-15 20:40:55 -0800305 public void addThread(Handler thread, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700306 synchronized (this) {
307 if (isAlive()) {
308 throw new RuntimeException("Threads can't be added once the Watchdog is running");
309 }
Jeff Brown6f357d32014-01-15 20:40:55 -0800310 final String name = thread.getLooper().getThread().getName();
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700311 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 }
313 }
314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 /**
316 * Perform a full reboot of the system.
317 */
318 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800319 Slog.i(TAG, "Rebooting system because: " + reason);
Jeff Brown6f357d32014-01-15 20:40:55 -0800320 IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
321 try {
322 pms.reboot(false, reason, false);
323 } catch (RemoteException ex) {
324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
326
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700327 private int evaluateCheckerCompletionLocked() {
328 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700329 for (int i=0; i<mHandlerCheckers.size(); i++) {
330 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700331 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700332 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700333 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700334 }
335
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700336 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
337 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700338 for (int i=0; i<mHandlerCheckers.size(); i++) {
339 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700340 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700341 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700342 }
343 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700344 return checkers;
345 }
346
347 private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) {
348 StringBuilder builder = new StringBuilder(128);
349 for (int i=0; i<checkers.size(); i++) {
350 if (builder.length() > 0) {
351 builder.append(", ");
352 }
353 builder.append(checkers.get(i).describeBlockedStateLocked());
354 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700355 return builder.toString();
356 }
357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 @Override
359 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700360 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700362 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700363 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700364 final boolean allowRestart;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700365 int debuggerWasConnected = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700367 long timeout = CHECK_INTERVAL;
368 // Make sure we (re)spin the checkers that have become idle within
369 // this wait-and-check interval
370 for (int i=0; i<mHandlerCheckers.size(); i++) {
371 HandlerChecker hc = mHandlerCheckers.get(i);
372 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700375 if (debuggerWasConnected > 0) {
376 debuggerWasConnected--;
377 }
378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
380 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700381 // 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 -0800382 // positive on when to kill things.
383 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700384 while (timeout > 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700385 if (Debug.isDebuggerConnected()) {
386 debuggerWasConnected = 2;
387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700389 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800391 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700393 if (Debug.isDebuggerConnected()) {
394 debuggerWasConnected = 2;
395 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700396 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700399 final int waitState = evaluateCheckerCompletionLocked();
400 if (waitState == COMPLETED) {
401 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700402 waitedHalf = false;
403 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700404 } else if (waitState == WAITING) {
405 // still waiting but within their configured intervals; back off and recheck
406 continue;
407 } else if (waitState == WAITED_HALF) {
408 if (!waitedHalf) {
409 // We've waited half the deadlock-detection interval. Pull a stack
410 // trace and wait another half.
411 ArrayList<Integer> pids = new ArrayList<Integer>();
412 pids.add(Process.myPid());
413 ActivityManagerService.dumpStackTraces(true, pids, null, null,
414 NATIVE_STACKS_OF_INTEREST);
415 waitedHalf = true;
416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 continue;
418 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700419
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700420 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700421 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700422 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700423 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425
426 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700427 // First collect stack traces from all threads of the system process.
428 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700429 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700431 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800432 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800433 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700434 // Pass !waitedHalf so that just in case we somehow wind up here without having
435 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800436 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700437 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800438
439 // Give some extra time to make sure the stack traces get written.
440 // The system's been hanging for a minute, another second or two won't hurt much.
441 SystemClock.sleep(2000);
442
Christopher Tateecaa7b42010-06-04 14:55:02 -0700443 // Pull our own kernel thread stacks as well if we're configured for that
444 if (RECORD_KERNEL_THREADS) {
445 dumpKernelStackTraces();
446 }
447
Guang Zhu0620c452014-10-29 14:31:48 -0700448 // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log
449 doSysRq('w');
450 doSysRq('l');
Colin Cross5df1d872012-11-29 11:42:11 -0800451
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800452 // Try to add the error to the dropbox, but assuming that the ActivityManager
453 // itself may be deadlocked. (which has happened, causing this statement to
454 // deadlock and the watchdog as a whole to be ineffective)
455 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
456 public void run() {
457 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700458 "watchdog", null, "system_server", null, null,
Jeff Brown7dd2d192013-09-06 15:05:23 -0700459 subject, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800460 }
461 };
462 dropboxThread.start();
463 try {
464 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
465 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700467 IActivityController controller;
468 synchronized (this) {
469 controller = mController;
470 }
471 if (controller != null) {
472 Slog.i(TAG, "Reporting stuck state to activity controller");
473 try {
474 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
475 // 1 = keep waiting, -1 = kill system
Jeff Brown7dd2d192013-09-06 15:05:23 -0700476 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700477 if (res >= 0) {
478 Slog.i(TAG, "Activity controller requested to coninue to wait");
479 waitedHalf = false;
480 continue;
481 }
482 } catch (RemoteException e) {
483 }
484 }
485
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700486 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700487 if (Debug.isDebuggerConnected()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700488 debuggerWasConnected = 2;
489 }
490 if (debuggerWasConnected >= 2) {
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700491 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700492 } else if (debuggerWasConnected > 0) {
493 Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process");
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700494 } else if (!allowRestart) {
495 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
496 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700497 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700498 for (int i=0; i<blockedCheckers.size(); i++) {
499 Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:");
500 StackTraceElement[] stackTrace
501 = blockedCheckers.get(i).getThread().getStackTrace();
502 for (StackTraceElement element: stackTrace) {
503 Slog.w(TAG, " at " + element);
504 }
Michael Wright56a6c662013-04-30 20:13:07 -0700505 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700506 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700507 Process.killProcess(Process.myPid());
508 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700510
511 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 }
513 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700514
Guang Zhu0620c452014-10-29 14:31:48 -0700515 private void doSysRq(char c) {
516 try {
517 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
518 sysrq_trigger.write(c);
519 sysrq_trigger.close();
520 } catch (IOException e) {
521 Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e);
522 }
523 }
524
Christopher Tateecaa7b42010-06-04 14:55:02 -0700525 private File dumpKernelStackTraces() {
526 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
527 if (tracesPath == null || tracesPath.length() == 0) {
528 return null;
529 }
530
531 native_dumpKernelStacks(tracesPath);
532 return new File(tracesPath);
533 }
534
535 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536}