blob: be021ea48ab8025c73b04ea90b4bf2e80e0eda1d [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;
29import android.os.Debug;
30import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080031import android.os.IPowerManager;
John Michelau11641522013-03-18 18:28:23 -050032import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Process;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080034import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.SystemClock;
36import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.util.EventLog;
Dan Egnor9bdc94b2010-03-04 14:20:31 -080038import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080039import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Dan Egnor9bdc94b2010-03-04 14:20:31 -080041import java.io.File;
Colin Cross5df1d872012-11-29 11:42:11 -080042import java.io.FileWriter;
43import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46/** This class calls its monitor every minute. Killing this process if they don't return **/
47public class Watchdog extends Thread {
48 static final String TAG = "Watchdog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50 // Set this to true to use debug default values.
51 static final boolean DB = false;
52
Christopher Tateecaa7b42010-06-04 14:55:02 -070053 // 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 Tatee6f81cf2013-10-23 17:28:27 -070056 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 Project9066cfe2009-03-03 19:31:44 -080064
Igor Murashkin44d04aa2013-10-23 10:56:02 -070065 // Which native processes to dump into dropbox's stack traces
66 public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
Andy Hung3a64ecb2016-03-09 13:55:58 -080067 "/system/bin/audioserver",
Andy Hung689574a2016-04-13 19:27:43 -070068 "/system/bin/cameraserver",
69 "/system/bin/drmserver",
70 "/system/bin/mediadrmserver",
Dianne Hackbornf72467a2012-06-08 17:23:59 -070071 "/system/bin/mediaserver",
72 "/system/bin/sdcard",
Eric Laurent05d4e352016-03-14 18:49:08 -070073 "/system/bin/surfaceflinger",
Andy Hung689574a2016-04-13 19:27:43 -070074 "media.codec", // system/bin/mediacodec
75 "media.extractor", // system/bin/mediaextractor
Andreas Gampecf9e79b2016-05-11 18:41:25 -070076 "com.android.bluetooth", // Bluetooth service
Dianne Hackbornf72467a2012-06-08 17:23:59 -070077 };
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 static Watchdog sWatchdog;
80
81 /* This handler will be used to post message back onto the main thread */
Wale Ogunwaled7fdd022015-04-13 16:22:38 -070082 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070083 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 ContentResolver mResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070088 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -070089 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -070092 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -070094 public final class HandlerChecker implements Runnable {
95 private final Handler mHandler;
96 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -070097 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070098 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070099 private boolean mCompleted;
100 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700101 private long mStartTime;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700102
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700103 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700104 mHandler = handler;
105 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700106 mWaitMax = waitMaxMillis;
107 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700108 }
109
110 public void addMonitor(Monitor monitor) {
111 mMonitors.add(monitor);
112 }
113
114 public void scheduleCheckLocked() {
Jeff Brown6c7b41a2015-02-26 14:43:53 -0800115 if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) {
116 // If the target looper has recently been polling, then
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700117 // there is no reason to enqueue our checker on it since that
118 // is as good as it not being deadlocked. This avoid having
119 // to do a context switch to check the thread. Note that we
120 // only do this if mCheckReboot is false and we have no
121 // monitors, since those would need to be executed at this point.
122 mCompleted = true;
123 return;
124 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700125
126 if (!mCompleted) {
127 // we already have a check in flight, so no need
128 return;
129 }
130
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700131 mCompleted = false;
132 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700133 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700134 mHandler.postAtFrontOfQueue(this);
135 }
136
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700137 public boolean isOverdueLocked() {
138 return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax);
139 }
140
141 public int getCompletionStateLocked() {
142 if (mCompleted) {
143 return COMPLETED;
144 } else {
145 long latency = SystemClock.uptimeMillis() - mStartTime;
146 if (latency < mWaitMax/2) {
147 return WAITING;
148 } else if (latency < mWaitMax) {
149 return WAITED_HALF;
150 }
151 }
152 return OVERDUE;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700153 }
154
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700155 public Thread getThread() {
156 return mHandler.getLooper().getThread();
157 }
158
159 public String getName() {
160 return mName;
161 }
162
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700163 public String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700164 if (mCurrentMonitor == null) {
165 return "Blocked in handler on " + mName + " (" + getThread().getName() + ")";
166 } else {
167 return "Blocked in monitor " + mCurrentMonitor.getClass().getName()
168 + " on " + mName + " (" + getThread().getName() + ")";
169 }
John Michelau11641522013-03-18 18:28:23 -0500170 }
171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700173 public void run() {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700174 final int size = mMonitors.size();
175 for (int i = 0 ; i < size ; i++) {
176 synchronized (Watchdog.this) {
177 mCurrentMonitor = mMonitors.get(i);
178 }
179 mCurrentMonitor.monitor();
180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700182 synchronized (Watchdog.this) {
183 mCompleted = true;
184 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 }
186 }
187 }
188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 final class RebootRequestReceiver extends BroadcastReceiver {
190 @Override
191 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700192 if (intent.getIntExtra("nowait", 0) != 0) {
193 rebootSystem("Received ACTION_REBOOT broadcast");
194 return;
195 }
196 Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 }
198 }
199
Wale Ogunwale517daec2015-04-15 10:27:24 -0700200 /** Monitor for checking the availability of binder threads. The monitor will block until
201 * there is a binder thread available to process in coming IPCs to make sure other processes
202 * can still communicate with the service.
203 */
204 private static final class BinderThreadMonitor implements Watchdog.Monitor {
205 @Override
206 public void monitor() {
207 Binder.blockUntilThreadAvailable();
208 }
209 }
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public interface Monitor {
212 void monitor();
213 }
214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 public static Watchdog getInstance() {
216 if (sWatchdog == null) {
217 sWatchdog = new Watchdog();
218 }
219
220 return sWatchdog;
221 }
222
223 private Watchdog() {
224 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700225 // Initialize handler checkers for each common thread we want to check. Note
226 // that we are not currently checking the background thread, since it can
227 // potentially hold longer running operations with no guarantees about the timeliness
228 // of operations there.
229
230 // The shared foreground thread is the main checker. It is where we
231 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700232 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
233 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700234 mHandlerCheckers.add(mMonitorChecker);
235 // Add checker for main thread. We only do a quick check since there
236 // can be UI running on the thread.
237 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700238 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700239 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700240 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
241 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700242 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700243 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
244 "i/o thread", DEFAULT_TIMEOUT));
Jeff Brown4ccb8232014-01-16 22:16:42 -0800245 // And the display thread.
246 mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
247 "display thread", DEFAULT_TIMEOUT));
Wale Ogunwale517daec2015-04-15 10:27:24 -0700248
249 // Initialize monitor for Binder threads.
250 addMonitor(new BinderThreadMonitor());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 }
252
Adam Lesinski182f73f2013-12-05 16:48:06 -0800253 public void init(Context context, ActivityManagerService activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 mResolver = context.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 mActivity = activity;
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 context.registerReceiver(new RebootRequestReceiver(),
258 new IntentFilter(Intent.ACTION_REBOOT),
259 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
261
Christopher Tatec27181c2010-06-30 14:41:09 -0700262 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 synchronized (this) {
264 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
267 }
268 }
269
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700270 public void setActivityController(IActivityController controller) {
271 synchronized (this) {
272 mController = controller;
273 }
274 }
275
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700276 public void setAllowRestart(boolean allowRestart) {
277 synchronized (this) {
278 mAllowRestart = allowRestart;
279 }
280 }
281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 public void addMonitor(Monitor monitor) {
283 synchronized (this) {
284 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700285 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700287 mMonitorChecker.addMonitor(monitor);
288 }
289 }
290
Jeff Brown6f357d32014-01-15 20:40:55 -0800291 public void addThread(Handler thread) {
292 addThread(thread, DEFAULT_TIMEOUT);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700293 }
294
Jeff Brown6f357d32014-01-15 20:40:55 -0800295 public void addThread(Handler thread, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700296 synchronized (this) {
297 if (isAlive()) {
298 throw new RuntimeException("Threads can't be added once the Watchdog is running");
299 }
Jeff Brown6f357d32014-01-15 20:40:55 -0800300 final String name = thread.getLooper().getThread().getName();
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700301 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 }
304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 /**
306 * Perform a full reboot of the system.
307 */
308 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800309 Slog.i(TAG, "Rebooting system because: " + reason);
Jeff Brown6f357d32014-01-15 20:40:55 -0800310 IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
311 try {
312 pms.reboot(false, reason, false);
313 } catch (RemoteException ex) {
314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700317 private int evaluateCheckerCompletionLocked() {
318 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700319 for (int i=0; i<mHandlerCheckers.size(); i++) {
320 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700321 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700322 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700323 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700324 }
325
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700326 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
327 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700328 for (int i=0; i<mHandlerCheckers.size(); i++) {
329 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700330 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700331 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700332 }
333 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700334 return checkers;
335 }
336
337 private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) {
338 StringBuilder builder = new StringBuilder(128);
339 for (int i=0; i<checkers.size(); i++) {
340 if (builder.length() > 0) {
341 builder.append(", ");
342 }
343 builder.append(checkers.get(i).describeBlockedStateLocked());
344 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700345 return builder.toString();
346 }
347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 @Override
349 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700350 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700352 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700353 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700354 final boolean allowRestart;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700355 int debuggerWasConnected = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700357 long timeout = CHECK_INTERVAL;
358 // Make sure we (re)spin the checkers that have become idle within
359 // this wait-and-check interval
360 for (int i=0; i<mHandlerCheckers.size(); i++) {
361 HandlerChecker hc = mHandlerCheckers.get(i);
362 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700365 if (debuggerWasConnected > 0) {
366 debuggerWasConnected--;
367 }
368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
370 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700371 // 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 -0800372 // positive on when to kill things.
373 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700374 while (timeout > 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700375 if (Debug.isDebuggerConnected()) {
376 debuggerWasConnected = 2;
377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700379 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800381 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700383 if (Debug.isDebuggerConnected()) {
384 debuggerWasConnected = 2;
385 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700386 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700389 final int waitState = evaluateCheckerCompletionLocked();
390 if (waitState == COMPLETED) {
391 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700392 waitedHalf = false;
393 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700394 } else if (waitState == WAITING) {
395 // still waiting but within their configured intervals; back off and recheck
396 continue;
397 } else if (waitState == WAITED_HALF) {
398 if (!waitedHalf) {
399 // We've waited half the deadlock-detection interval. Pull a stack
400 // trace and wait another half.
401 ArrayList<Integer> pids = new ArrayList<Integer>();
402 pids.add(Process.myPid());
403 ActivityManagerService.dumpStackTraces(true, pids, null, null,
404 NATIVE_STACKS_OF_INTEREST);
405 waitedHalf = true;
406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 continue;
408 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700409
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700410 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700411 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700412 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700413 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415
416 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700417 // First collect stack traces from all threads of the system process.
418 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700419 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700421 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800422 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800423 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700424 // Pass !waitedHalf so that just in case we somehow wind up here without having
425 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800426 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700427 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800428
429 // Give some extra time to make sure the stack traces get written.
430 // The system's been hanging for a minute, another second or two won't hurt much.
431 SystemClock.sleep(2000);
432
Christopher Tateecaa7b42010-06-04 14:55:02 -0700433 // Pull our own kernel thread stacks as well if we're configured for that
434 if (RECORD_KERNEL_THREADS) {
435 dumpKernelStackTraces();
436 }
437
Guang Zhu0620c452014-10-29 14:31:48 -0700438 // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log
439 doSysRq('w');
440 doSysRq('l');
Colin Cross5df1d872012-11-29 11:42:11 -0800441
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800442 // Try to add the error to the dropbox, but assuming that the ActivityManager
443 // itself may be deadlocked. (which has happened, causing this statement to
444 // deadlock and the watchdog as a whole to be ineffective)
445 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
446 public void run() {
447 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700448 "watchdog", null, "system_server", null, null,
Jeff Brown7dd2d192013-09-06 15:05:23 -0700449 subject, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800450 }
451 };
452 dropboxThread.start();
453 try {
454 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
455 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700457 IActivityController controller;
458 synchronized (this) {
459 controller = mController;
460 }
461 if (controller != null) {
462 Slog.i(TAG, "Reporting stuck state to activity controller");
463 try {
464 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
465 // 1 = keep waiting, -1 = kill system
Jeff Brown7dd2d192013-09-06 15:05:23 -0700466 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700467 if (res >= 0) {
468 Slog.i(TAG, "Activity controller requested to coninue to wait");
469 waitedHalf = false;
470 continue;
471 }
472 } catch (RemoteException e) {
473 }
474 }
475
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700476 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700477 if (Debug.isDebuggerConnected()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700478 debuggerWasConnected = 2;
479 }
480 if (debuggerWasConnected >= 2) {
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700481 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700482 } else if (debuggerWasConnected > 0) {
483 Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process");
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700484 } else if (!allowRestart) {
485 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
486 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700487 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700488 for (int i=0; i<blockedCheckers.size(); i++) {
489 Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:");
490 StackTraceElement[] stackTrace
491 = blockedCheckers.get(i).getThread().getStackTrace();
492 for (StackTraceElement element: stackTrace) {
493 Slog.w(TAG, " at " + element);
494 }
Michael Wright56a6c662013-04-30 20:13:07 -0700495 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700496 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700497 Process.killProcess(Process.myPid());
498 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700500
501 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700504
Guang Zhu0620c452014-10-29 14:31:48 -0700505 private void doSysRq(char c) {
506 try {
507 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
508 sysrq_trigger.write(c);
509 sysrq_trigger.close();
510 } catch (IOException e) {
511 Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e);
512 }
513 }
514
Christopher Tateecaa7b42010-06-04 14:55:02 -0700515 private File dumpKernelStackTraces() {
516 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
517 if (tracesPath == null || tracesPath.length() == 0) {
518 return null;
519 }
520
521 native_dumpKernelStacks(tracesPath);
522 return new File(tracesPath);
523 }
524
525 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526}