blob: 69e61f67eb74ac987ba30e131522aa69ffd82e51 [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[] {
Dianne Hackbornf72467a2012-06-08 17:23:59 -070067 "/system/bin/mediaserver",
68 "/system/bin/sdcard",
69 "/system/bin/surfaceflinger"
70 };
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 static Watchdog sWatchdog;
73
74 /* This handler will be used to post message back onto the main thread */
Wale Ogunwaled7fdd022015-04-13 16:22:38 -070075 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070076 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 ContentResolver mResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070081 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -070082 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -070085 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -070087 public final class HandlerChecker implements Runnable {
88 private final Handler mHandler;
89 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -070090 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070091 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070092 private boolean mCompleted;
93 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -070094 private long mStartTime;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070095
Christopher Tatee6f81cf2013-10-23 17:28:27 -070096 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -070097 mHandler = handler;
98 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -070099 mWaitMax = waitMaxMillis;
100 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700101 }
102
103 public void addMonitor(Monitor monitor) {
104 mMonitors.add(monitor);
105 }
106
107 public void scheduleCheckLocked() {
Jeff Brown6c7b41a2015-02-26 14:43:53 -0800108 if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) {
109 // If the target looper has recently been polling, then
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700110 // there is no reason to enqueue our checker on it since that
111 // is as good as it not being deadlocked. This avoid having
112 // to do a context switch to check the thread. Note that we
113 // only do this if mCheckReboot is false and we have no
114 // monitors, since those would need to be executed at this point.
115 mCompleted = true;
116 return;
117 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700118
119 if (!mCompleted) {
120 // we already have a check in flight, so no need
121 return;
122 }
123
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700124 mCompleted = false;
125 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700126 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700127 mHandler.postAtFrontOfQueue(this);
128 }
129
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700130 public boolean isOverdueLocked() {
131 return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax);
132 }
133
134 public int getCompletionStateLocked() {
135 if (mCompleted) {
136 return COMPLETED;
137 } else {
138 long latency = SystemClock.uptimeMillis() - mStartTime;
139 if (latency < mWaitMax/2) {
140 return WAITING;
141 } else if (latency < mWaitMax) {
142 return WAITED_HALF;
143 }
144 }
145 return OVERDUE;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700146 }
147
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700148 public Thread getThread() {
149 return mHandler.getLooper().getThread();
150 }
151
152 public String getName() {
153 return mName;
154 }
155
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700156 public String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700157 if (mCurrentMonitor == null) {
158 return "Blocked in handler on " + mName + " (" + getThread().getName() + ")";
159 } else {
160 return "Blocked in monitor " + mCurrentMonitor.getClass().getName()
161 + " on " + mName + " (" + getThread().getName() + ")";
162 }
John Michelau11641522013-03-18 18:28:23 -0500163 }
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700166 public void run() {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700167 final int size = mMonitors.size();
168 for (int i = 0 ; i < size ; i++) {
169 synchronized (Watchdog.this) {
170 mCurrentMonitor = mMonitors.get(i);
171 }
172 mCurrentMonitor.monitor();
173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700175 synchronized (Watchdog.this) {
176 mCompleted = true;
177 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 }
179 }
180 }
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 final class RebootRequestReceiver extends BroadcastReceiver {
183 @Override
184 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700185 if (intent.getIntExtra("nowait", 0) != 0) {
186 rebootSystem("Received ACTION_REBOOT broadcast");
187 return;
188 }
189 Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
191 }
192
193 public interface Monitor {
194 void monitor();
195 }
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 public static Watchdog getInstance() {
198 if (sWatchdog == null) {
199 sWatchdog = new Watchdog();
200 }
201
202 return sWatchdog;
203 }
204
205 private Watchdog() {
206 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700207 // Initialize handler checkers for each common thread we want to check. Note
208 // that we are not currently checking the background thread, since it can
209 // potentially hold longer running operations with no guarantees about the timeliness
210 // of operations there.
211
212 // The shared foreground thread is the main checker. It is where we
213 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700214 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
215 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700216 mHandlerCheckers.add(mMonitorChecker);
217 // Add checker for main thread. We only do a quick check since there
218 // can be UI running on the thread.
219 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700220 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700221 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700222 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
223 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700224 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700225 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
226 "i/o thread", DEFAULT_TIMEOUT));
Jeff Brown4ccb8232014-01-16 22:16:42 -0800227 // And the display thread.
228 mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
229 "display thread", DEFAULT_TIMEOUT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 }
231
Adam Lesinski182f73f2013-12-05 16:48:06 -0800232 public void init(Context context, ActivityManagerService activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 mResolver = context.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 mActivity = activity;
235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 context.registerReceiver(new RebootRequestReceiver(),
237 new IntentFilter(Intent.ACTION_REBOOT),
238 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 }
240
Christopher Tatec27181c2010-06-30 14:41:09 -0700241 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 synchronized (this) {
243 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246 }
247 }
248
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700249 public void setActivityController(IActivityController controller) {
250 synchronized (this) {
251 mController = controller;
252 }
253 }
254
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700255 public void setAllowRestart(boolean allowRestart) {
256 synchronized (this) {
257 mAllowRestart = allowRestart;
258 }
259 }
260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 public void addMonitor(Monitor monitor) {
262 synchronized (this) {
263 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700264 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700266 mMonitorChecker.addMonitor(monitor);
267 }
268 }
269
Jeff Brown6f357d32014-01-15 20:40:55 -0800270 public void addThread(Handler thread) {
271 addThread(thread, DEFAULT_TIMEOUT);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700272 }
273
Jeff Brown6f357d32014-01-15 20:40:55 -0800274 public void addThread(Handler thread, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700275 synchronized (this) {
276 if (isAlive()) {
277 throw new RuntimeException("Threads can't be added once the Watchdog is running");
278 }
Jeff Brown6f357d32014-01-15 20:40:55 -0800279 final String name = thread.getLooper().getThread().getName();
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700280 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
282 }
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 /**
285 * Perform a full reboot of the system.
286 */
287 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800288 Slog.i(TAG, "Rebooting system because: " + reason);
Jeff Brown6f357d32014-01-15 20:40:55 -0800289 IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
290 try {
291 pms.reboot(false, reason, false);
292 } catch (RemoteException ex) {
293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 }
295
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700296 private int evaluateCheckerCompletionLocked() {
297 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700298 for (int i=0; i<mHandlerCheckers.size(); i++) {
299 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700300 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700301 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700302 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700303 }
304
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700305 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
306 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700307 for (int i=0; i<mHandlerCheckers.size(); i++) {
308 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700309 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700310 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700311 }
312 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700313 return checkers;
314 }
315
316 private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) {
317 StringBuilder builder = new StringBuilder(128);
318 for (int i=0; i<checkers.size(); i++) {
319 if (builder.length() > 0) {
320 builder.append(", ");
321 }
322 builder.append(checkers.get(i).describeBlockedStateLocked());
323 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700324 return builder.toString();
325 }
326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 @Override
328 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700329 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700331 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700332 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700333 final boolean allowRestart;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700334 int debuggerWasConnected = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700336 long timeout = CHECK_INTERVAL;
337 // Make sure we (re)spin the checkers that have become idle within
338 // this wait-and-check interval
339 for (int i=0; i<mHandlerCheckers.size(); i++) {
340 HandlerChecker hc = mHandlerCheckers.get(i);
341 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700344 if (debuggerWasConnected > 0) {
345 debuggerWasConnected--;
346 }
347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
349 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700350 // 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 -0800351 // positive on when to kill things.
352 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700353 while (timeout > 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700354 if (Debug.isDebuggerConnected()) {
355 debuggerWasConnected = 2;
356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700358 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800360 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700362 if (Debug.isDebuggerConnected()) {
363 debuggerWasConnected = 2;
364 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700365 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700368 final int waitState = evaluateCheckerCompletionLocked();
369 if (waitState == COMPLETED) {
370 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700371 waitedHalf = false;
372 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700373 } else if (waitState == WAITING) {
374 // still waiting but within their configured intervals; back off and recheck
375 continue;
376 } else if (waitState == WAITED_HALF) {
377 if (!waitedHalf) {
378 // We've waited half the deadlock-detection interval. Pull a stack
379 // trace and wait another half.
380 ArrayList<Integer> pids = new ArrayList<Integer>();
381 pids.add(Process.myPid());
382 ActivityManagerService.dumpStackTraces(true, pids, null, null,
383 NATIVE_STACKS_OF_INTEREST);
384 waitedHalf = true;
385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 continue;
387 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700388
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700389 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700390 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700391 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700392 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394
395 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700396 // First collect stack traces from all threads of the system process.
397 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700398 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700400 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800401 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800402 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700403 // Pass !waitedHalf so that just in case we somehow wind up here without having
404 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800405 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700406 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800407
408 // Give some extra time to make sure the stack traces get written.
409 // The system's been hanging for a minute, another second or two won't hurt much.
410 SystemClock.sleep(2000);
411
Christopher Tateecaa7b42010-06-04 14:55:02 -0700412 // Pull our own kernel thread stacks as well if we're configured for that
413 if (RECORD_KERNEL_THREADS) {
414 dumpKernelStackTraces();
415 }
416
Guang Zhu0620c452014-10-29 14:31:48 -0700417 // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log
418 doSysRq('w');
419 doSysRq('l');
Colin Cross5df1d872012-11-29 11:42:11 -0800420
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800421 // Try to add the error to the dropbox, but assuming that the ActivityManager
422 // itself may be deadlocked. (which has happened, causing this statement to
423 // deadlock and the watchdog as a whole to be ineffective)
424 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
425 public void run() {
426 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700427 "watchdog", null, "system_server", null, null,
Jeff Brown7dd2d192013-09-06 15:05:23 -0700428 subject, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800429 }
430 };
431 dropboxThread.start();
432 try {
433 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
434 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700436 IActivityController controller;
437 synchronized (this) {
438 controller = mController;
439 }
440 if (controller != null) {
441 Slog.i(TAG, "Reporting stuck state to activity controller");
442 try {
443 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
444 // 1 = keep waiting, -1 = kill system
Jeff Brown7dd2d192013-09-06 15:05:23 -0700445 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700446 if (res >= 0) {
447 Slog.i(TAG, "Activity controller requested to coninue to wait");
448 waitedHalf = false;
449 continue;
450 }
451 } catch (RemoteException e) {
452 }
453 }
454
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700455 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700456 if (Debug.isDebuggerConnected()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700457 debuggerWasConnected = 2;
458 }
459 if (debuggerWasConnected >= 2) {
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700460 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700461 } else if (debuggerWasConnected > 0) {
462 Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process");
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700463 } else if (!allowRestart) {
464 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
465 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700466 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700467 for (int i=0; i<blockedCheckers.size(); i++) {
468 Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:");
469 StackTraceElement[] stackTrace
470 = blockedCheckers.get(i).getThread().getStackTrace();
471 for (StackTraceElement element: stackTrace) {
472 Slog.w(TAG, " at " + element);
473 }
Michael Wright56a6c662013-04-30 20:13:07 -0700474 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700475 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700476 Process.killProcess(Process.myPid());
477 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700479
480 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 }
482 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700483
Guang Zhu0620c452014-10-29 14:31:48 -0700484 private void doSysRq(char c) {
485 try {
486 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
487 sysrq_trigger.write(c);
488 sysrq_trigger.close();
489 } catch (IOException e) {
490 Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e);
491 }
492 }
493
Christopher Tateecaa7b42010-06-04 14:55:02 -0700494 private File dumpKernelStackTraces() {
495 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
496 if (tracesPath == null || tracesPath.length() == 0) {
497 return null;
498 }
499
500 native_dumpKernelStacks(tracesPath);
501 return new File(tracesPath);
502 }
503
504 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505}