blob: 11888ff4705ce4697dff5b87c1366834fb6a48a2 [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",
Dianne Hackbornf72467a2012-06-08 17:23:59 -070068 "/system/bin/mediaserver",
69 "/system/bin/sdcard",
70 "/system/bin/surfaceflinger"
71 };
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 static Watchdog sWatchdog;
74
75 /* This handler will be used to post message back onto the main thread */
Wale Ogunwaled7fdd022015-04-13 16:22:38 -070076 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070077 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 ContentResolver mResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070082 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -070083 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -070086 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -070088 public final class HandlerChecker implements Runnable {
89 private final Handler mHandler;
90 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -070091 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070092 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070093 private boolean mCompleted;
94 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -070095 private long mStartTime;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070096
Christopher Tatee6f81cf2013-10-23 17:28:27 -070097 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -070098 mHandler = handler;
99 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700100 mWaitMax = waitMaxMillis;
101 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700102 }
103
104 public void addMonitor(Monitor monitor) {
105 mMonitors.add(monitor);
106 }
107
108 public void scheduleCheckLocked() {
Jeff Brown6c7b41a2015-02-26 14:43:53 -0800109 if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) {
110 // If the target looper has recently been polling, then
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700111 // there is no reason to enqueue our checker on it since that
112 // is as good as it not being deadlocked. This avoid having
113 // to do a context switch to check the thread. Note that we
114 // only do this if mCheckReboot is false and we have no
115 // monitors, since those would need to be executed at this point.
116 mCompleted = true;
117 return;
118 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700119
120 if (!mCompleted) {
121 // we already have a check in flight, so no need
122 return;
123 }
124
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700125 mCompleted = false;
126 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700127 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700128 mHandler.postAtFrontOfQueue(this);
129 }
130
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700131 public boolean isOverdueLocked() {
132 return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax);
133 }
134
135 public int getCompletionStateLocked() {
136 if (mCompleted) {
137 return COMPLETED;
138 } else {
139 long latency = SystemClock.uptimeMillis() - mStartTime;
140 if (latency < mWaitMax/2) {
141 return WAITING;
142 } else if (latency < mWaitMax) {
143 return WAITED_HALF;
144 }
145 }
146 return OVERDUE;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700147 }
148
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700149 public Thread getThread() {
150 return mHandler.getLooper().getThread();
151 }
152
153 public String getName() {
154 return mName;
155 }
156
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700157 public String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700158 if (mCurrentMonitor == null) {
159 return "Blocked in handler on " + mName + " (" + getThread().getName() + ")";
160 } else {
161 return "Blocked in monitor " + mCurrentMonitor.getClass().getName()
162 + " on " + mName + " (" + getThread().getName() + ")";
163 }
John Michelau11641522013-03-18 18:28:23 -0500164 }
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700167 public void run() {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700168 final int size = mMonitors.size();
169 for (int i = 0 ; i < size ; i++) {
170 synchronized (Watchdog.this) {
171 mCurrentMonitor = mMonitors.get(i);
172 }
173 mCurrentMonitor.monitor();
174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700176 synchronized (Watchdog.this) {
177 mCompleted = true;
178 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
180 }
181 }
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 final class RebootRequestReceiver extends BroadcastReceiver {
184 @Override
185 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700186 if (intent.getIntExtra("nowait", 0) != 0) {
187 rebootSystem("Received ACTION_REBOOT broadcast");
188 return;
189 }
190 Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
192 }
193
Wale Ogunwale517daec2015-04-15 10:27:24 -0700194 /** Monitor for checking the availability of binder threads. The monitor will block until
195 * there is a binder thread available to process in coming IPCs to make sure other processes
196 * can still communicate with the service.
197 */
198 private static final class BinderThreadMonitor implements Watchdog.Monitor {
199 @Override
200 public void monitor() {
201 Binder.blockUntilThreadAvailable();
202 }
203 }
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public interface Monitor {
206 void monitor();
207 }
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 public static Watchdog getInstance() {
210 if (sWatchdog == null) {
211 sWatchdog = new Watchdog();
212 }
213
214 return sWatchdog;
215 }
216
217 private Watchdog() {
218 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700219 // Initialize handler checkers for each common thread we want to check. Note
220 // that we are not currently checking the background thread, since it can
221 // potentially hold longer running operations with no guarantees about the timeliness
222 // of operations there.
223
224 // The shared foreground thread is the main checker. It is where we
225 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700226 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
227 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700228 mHandlerCheckers.add(mMonitorChecker);
229 // Add checker for main thread. We only do a quick check since there
230 // can be UI running on the thread.
231 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700232 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700233 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700234 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
235 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700236 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700237 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
238 "i/o thread", DEFAULT_TIMEOUT));
Jeff Brown4ccb8232014-01-16 22:16:42 -0800239 // And the display thread.
240 mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
241 "display thread", DEFAULT_TIMEOUT));
Wale Ogunwale517daec2015-04-15 10:27:24 -0700242
243 // Initialize monitor for Binder threads.
244 addMonitor(new BinderThreadMonitor());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246
Adam Lesinski182f73f2013-12-05 16:48:06 -0800247 public void init(Context context, ActivityManagerService activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 mResolver = context.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 mActivity = activity;
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 context.registerReceiver(new RebootRequestReceiver(),
252 new IntentFilter(Intent.ACTION_REBOOT),
253 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
255
Christopher Tatec27181c2010-06-30 14:41:09 -0700256 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 synchronized (this) {
258 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
261 }
262 }
263
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700264 public void setActivityController(IActivityController controller) {
265 synchronized (this) {
266 mController = controller;
267 }
268 }
269
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700270 public void setAllowRestart(boolean allowRestart) {
271 synchronized (this) {
272 mAllowRestart = allowRestart;
273 }
274 }
275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 public void addMonitor(Monitor monitor) {
277 synchronized (this) {
278 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700279 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700281 mMonitorChecker.addMonitor(monitor);
282 }
283 }
284
Jeff Brown6f357d32014-01-15 20:40:55 -0800285 public void addThread(Handler thread) {
286 addThread(thread, DEFAULT_TIMEOUT);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700287 }
288
Jeff Brown6f357d32014-01-15 20:40:55 -0800289 public void addThread(Handler thread, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700290 synchronized (this) {
291 if (isAlive()) {
292 throw new RuntimeException("Threads can't be added once the Watchdog is running");
293 }
Jeff Brown6f357d32014-01-15 20:40:55 -0800294 final String name = thread.getLooper().getThread().getName();
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700295 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
297 }
298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Perform a full reboot of the system.
301 */
302 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800303 Slog.i(TAG, "Rebooting system because: " + reason);
Jeff Brown6f357d32014-01-15 20:40:55 -0800304 IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
305 try {
306 pms.reboot(false, reason, false);
307 } catch (RemoteException ex) {
308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 }
310
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700311 private int evaluateCheckerCompletionLocked() {
312 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700313 for (int i=0; i<mHandlerCheckers.size(); i++) {
314 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700315 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700316 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700317 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700318 }
319
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700320 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
321 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700322 for (int i=0; i<mHandlerCheckers.size(); i++) {
323 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700324 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700325 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700326 }
327 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700328 return checkers;
329 }
330
331 private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) {
332 StringBuilder builder = new StringBuilder(128);
333 for (int i=0; i<checkers.size(); i++) {
334 if (builder.length() > 0) {
335 builder.append(", ");
336 }
337 builder.append(checkers.get(i).describeBlockedStateLocked());
338 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700339 return builder.toString();
340 }
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 @Override
343 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700344 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700346 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700347 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700348 final boolean allowRestart;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700349 int debuggerWasConnected = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700351 long timeout = CHECK_INTERVAL;
352 // Make sure we (re)spin the checkers that have become idle within
353 // this wait-and-check interval
354 for (int i=0; i<mHandlerCheckers.size(); i++) {
355 HandlerChecker hc = mHandlerCheckers.get(i);
356 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700359 if (debuggerWasConnected > 0) {
360 debuggerWasConnected--;
361 }
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
364 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700365 // 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 -0800366 // positive on when to kill things.
367 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700368 while (timeout > 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700369 if (Debug.isDebuggerConnected()) {
370 debuggerWasConnected = 2;
371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700373 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800375 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700377 if (Debug.isDebuggerConnected()) {
378 debuggerWasConnected = 2;
379 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700380 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700383 final int waitState = evaluateCheckerCompletionLocked();
384 if (waitState == COMPLETED) {
385 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700386 waitedHalf = false;
387 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700388 } else if (waitState == WAITING) {
389 // still waiting but within their configured intervals; back off and recheck
390 continue;
391 } else if (waitState == WAITED_HALF) {
392 if (!waitedHalf) {
393 // We've waited half the deadlock-detection interval. Pull a stack
394 // trace and wait another half.
395 ArrayList<Integer> pids = new ArrayList<Integer>();
396 pids.add(Process.myPid());
397 ActivityManagerService.dumpStackTraces(true, pids, null, null,
398 NATIVE_STACKS_OF_INTEREST);
399 waitedHalf = true;
400 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 continue;
402 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700403
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700404 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700405 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700406 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700407 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 }
409
410 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700411 // First collect stack traces from all threads of the system process.
412 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700413 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700415 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800416 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800417 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700418 // Pass !waitedHalf so that just in case we somehow wind up here without having
419 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800420 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700421 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800422
423 // Give some extra time to make sure the stack traces get written.
424 // The system's been hanging for a minute, another second or two won't hurt much.
425 SystemClock.sleep(2000);
426
Christopher Tateecaa7b42010-06-04 14:55:02 -0700427 // Pull our own kernel thread stacks as well if we're configured for that
428 if (RECORD_KERNEL_THREADS) {
429 dumpKernelStackTraces();
430 }
431
Guang Zhu0620c452014-10-29 14:31:48 -0700432 // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log
433 doSysRq('w');
434 doSysRq('l');
Colin Cross5df1d872012-11-29 11:42:11 -0800435
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800436 // Try to add the error to the dropbox, but assuming that the ActivityManager
437 // itself may be deadlocked. (which has happened, causing this statement to
438 // deadlock and the watchdog as a whole to be ineffective)
439 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
440 public void run() {
441 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700442 "watchdog", null, "system_server", null, null,
Jeff Brown7dd2d192013-09-06 15:05:23 -0700443 subject, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800444 }
445 };
446 dropboxThread.start();
447 try {
448 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
449 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700451 IActivityController controller;
452 synchronized (this) {
453 controller = mController;
454 }
455 if (controller != null) {
456 Slog.i(TAG, "Reporting stuck state to activity controller");
457 try {
458 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
459 // 1 = keep waiting, -1 = kill system
Jeff Brown7dd2d192013-09-06 15:05:23 -0700460 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700461 if (res >= 0) {
462 Slog.i(TAG, "Activity controller requested to coninue to wait");
463 waitedHalf = false;
464 continue;
465 }
466 } catch (RemoteException e) {
467 }
468 }
469
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700470 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700471 if (Debug.isDebuggerConnected()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700472 debuggerWasConnected = 2;
473 }
474 if (debuggerWasConnected >= 2) {
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700475 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700476 } else if (debuggerWasConnected > 0) {
477 Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process");
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700478 } else if (!allowRestart) {
479 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
480 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700481 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700482 for (int i=0; i<blockedCheckers.size(); i++) {
483 Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:");
484 StackTraceElement[] stackTrace
485 = blockedCheckers.get(i).getThread().getStackTrace();
486 for (StackTraceElement element: stackTrace) {
487 Slog.w(TAG, " at " + element);
488 }
Michael Wright56a6c662013-04-30 20:13:07 -0700489 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700490 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700491 Process.killProcess(Process.myPid());
492 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700494
495 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700498
Guang Zhu0620c452014-10-29 14:31:48 -0700499 private void doSysRq(char c) {
500 try {
501 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
502 sysrq_trigger.write(c);
503 sysrq_trigger.close();
504 } catch (IOException e) {
505 Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e);
506 }
507 }
508
Christopher Tateecaa7b42010-06-04 14:55:02 -0700509 private File dumpKernelStackTraces() {
510 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
511 if (tracesPath == null || tracesPath.length() == 0) {
512 return null;
513 }
514
515 native_dumpKernelStacks(tracesPath);
516 return new File(tracesPath);
517 }
518
519 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520}