blob: e17f42d9f415abaefdd08394016479a22d099d33 [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;
Jeff Brown4f8ecd82012-06-18 18:29:13 -070023import com.android.server.power.PowerManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25import android.app.AlarmManager;
26import android.app.PendingIntent;
27import android.content.BroadcastReceiver;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
Jeff Browna4d82042012-10-02 19:11:19 -070032import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Debug;
34import android.os.Handler;
John Michelau11641522013-03-18 18:28:23 -050035import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Process;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080037import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.SystemClock;
39import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.util.EventLog;
Dan Egnor9bdc94b2010-03-04 14:20:31 -080041import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080042import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Dan Egnor9bdc94b2010-03-04 14:20:31 -080044import java.io.File;
Colin Cross5df1d872012-11-29 11:42:11 -080045import java.io.FileWriter;
46import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.util.ArrayList;
48import java.util.Calendar;
49
50/** This class calls its monitor every minute. Killing this process if they don't return **/
51public class Watchdog extends Thread {
52 static final String TAG = "Watchdog";
Joe Onorato43a17652011-04-06 19:22:23 -070053 static final boolean localLOGV = false || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55 // Set this to true to use debug default values.
56 static final boolean DB = false;
57
Christopher Tateecaa7b42010-06-04 14:55:02 -070058 // Set this to true to have the watchdog record kernel thread stacks when it fires
59 static final boolean RECORD_KERNEL_THREADS = true;
60
Christopher Tatee6f81cf2013-10-23 17:28:27 -070061 static final long DEFAULT_TIMEOUT = DB ? 10*1000 : 60*1000;
62 static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2;
63
64 // These are temporally ordered: larger values as lateness increases
65 static final int COMPLETED = 0;
66 static final int WAITING = 1;
67 static final int WAITED_HALF = 2;
68 static final int OVERDUE = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Igor Murashkin44d04aa2013-10-23 10:56:02 -070070 // Which native processes to dump into dropbox's stack traces
71 public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
Dianne Hackbornf72467a2012-06-08 17:23:59 -070072 "/system/bin/mediaserver",
73 "/system/bin/sdcard",
74 "/system/bin/surfaceflinger"
75 };
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 static Watchdog sWatchdog;
78
79 /* This handler will be used to post message back onto the main thread */
Dianne Hackborn8d044e82013-04-30 17:24:15 -070080 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<HandlerChecker>();
81 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 ContentResolver mResolver;
83 BatteryService mBattery;
84 PowerManagerService mPower;
85 AlarmManagerService mAlarm;
86 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070089 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -070090 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -070093 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -070095 public final class HandlerChecker implements Runnable {
96 private final Handler mHandler;
97 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -070098 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070099 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700100 private boolean mCompleted;
101 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700102 private long mStartTime;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700103
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700104 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700105 mHandler = handler;
106 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700107 mWaitMax = waitMaxMillis;
108 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700109 }
110
111 public void addMonitor(Monitor monitor) {
112 mMonitors.add(monitor);
113 }
114
115 public void scheduleCheckLocked() {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700116 if (mMonitors.size() == 0 && mHandler.getLooper().isIdling()) {
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700117 // If the target looper is or just recently was idling, then
118 // there is no reason to enqueue our checker on it since that
119 // is as good as it not being deadlocked. This avoid having
120 // to do a context switch to check the thread. Note that we
121 // only do this if mCheckReboot is false and we have no
122 // monitors, since those would need to be executed at this point.
123 mCompleted = true;
124 return;
125 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700126
127 if (!mCompleted) {
128 // we already have a check in flight, so no need
129 return;
130 }
131
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700132 mCompleted = false;
133 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700134 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700135 mHandler.postAtFrontOfQueue(this);
136 }
137
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700138 public boolean isOverdueLocked() {
139 return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax);
140 }
141
142 public int getCompletionStateLocked() {
143 if (mCompleted) {
144 return COMPLETED;
145 } else {
146 long latency = SystemClock.uptimeMillis() - mStartTime;
147 if (latency < mWaitMax/2) {
148 return WAITING;
149 } else if (latency < mWaitMax) {
150 return WAITED_HALF;
151 }
152 }
153 return OVERDUE;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700154 }
155
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700156 public Thread getThread() {
157 return mHandler.getLooper().getThread();
158 }
159
160 public String getName() {
161 return mName;
162 }
163
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700164 public String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700165 if (mCurrentMonitor == null) {
166 return "Blocked in handler on " + mName + " (" + getThread().getName() + ")";
167 } else {
168 return "Blocked in monitor " + mCurrentMonitor.getClass().getName()
169 + " on " + mName + " (" + getThread().getName() + ")";
170 }
John Michelau11641522013-03-18 18:28:23 -0500171 }
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700174 public void run() {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700175 final int size = mMonitors.size();
176 for (int i = 0 ; i < size ; i++) {
177 synchronized (Watchdog.this) {
178 mCurrentMonitor = mMonitors.get(i);
179 }
180 mCurrentMonitor.monitor();
181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700183 synchronized (Watchdog.this) {
184 mCompleted = true;
185 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 }
187 }
188 }
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 final class RebootRequestReceiver extends BroadcastReceiver {
191 @Override
192 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700193 if (intent.getIntExtra("nowait", 0) != 0) {
194 rebootSystem("Received ACTION_REBOOT broadcast");
195 return;
196 }
197 Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
199 }
200
201 public interface Monitor {
202 void monitor();
203 }
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public static Watchdog getInstance() {
206 if (sWatchdog == null) {
207 sWatchdog = new Watchdog();
208 }
209
210 return sWatchdog;
211 }
212
213 private Watchdog() {
214 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700215 // Initialize handler checkers for each common thread we want to check. Note
216 // that we are not currently checking the background thread, since it can
217 // potentially hold longer running operations with no guarantees about the timeliness
218 // of operations there.
219
220 // The shared foreground thread is the main checker. It is where we
221 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700222 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
223 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700224 mHandlerCheckers.add(mMonitorChecker);
225 // Add checker for main thread. We only do a quick check since there
226 // can be UI running on the thread.
227 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700228 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700229 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700230 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
231 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700232 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700233 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
234 "i/o thread", DEFAULT_TIMEOUT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 }
236
237 public void init(Context context, BatteryService battery,
238 PowerManagerService power, AlarmManagerService alarm,
239 ActivityManagerService activity) {
240 mResolver = context.getContentResolver();
241 mBattery = battery;
242 mPower = power;
243 mAlarm = alarm;
244 mActivity = activity;
245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 context.registerReceiver(new RebootRequestReceiver(),
247 new IntentFilter(Intent.ACTION_REBOOT),
248 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250
Christopher Tatec27181c2010-06-30 14:41:09 -0700251 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 synchronized (this) {
253 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
256 }
257 }
258
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700259 public void setActivityController(IActivityController controller) {
260 synchronized (this) {
261 mController = controller;
262 }
263 }
264
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700265 public void setAllowRestart(boolean allowRestart) {
266 synchronized (this) {
267 mAllowRestart = allowRestart;
268 }
269 }
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public void addMonitor(Monitor monitor) {
272 synchronized (this) {
273 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700274 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700276 mMonitorChecker.addMonitor(monitor);
277 }
278 }
279
280 public void addThread(Handler thread, String name) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700281 addThread(thread, name, DEFAULT_TIMEOUT);
282 }
283
284 public void addThread(Handler thread, String name, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700285 synchronized (this) {
286 if (isAlive()) {
287 throw new RuntimeException("Threads can't be added once the Watchdog is running");
288 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700289 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 }
291 }
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 /**
294 * Perform a full reboot of the system.
295 */
296 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800297 Slog.i(TAG, "Rebooting system because: " + reason);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800298 PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700299 pms.reboot(false, reason, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 }
301
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700302 private int evaluateCheckerCompletionLocked() {
303 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700304 for (int i=0; i<mHandlerCheckers.size(); i++) {
305 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700306 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700307 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700308 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700309 }
310
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700311 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
312 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
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 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700316 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700317 }
318 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700319 return checkers;
320 }
321
322 private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) {
323 StringBuilder builder = new StringBuilder(128);
324 for (int i=0; i<checkers.size(); i++) {
325 if (builder.length() > 0) {
326 builder.append(", ");
327 }
328 builder.append(checkers.get(i).describeBlockedStateLocked());
329 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700330 return builder.toString();
331 }
332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 @Override
334 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700335 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700337 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700338 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700339 final boolean allowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700341 long timeout = CHECK_INTERVAL;
342 // Make sure we (re)spin the checkers that have become idle within
343 // this wait-and-check interval
344 for (int i=0; i<mHandlerCheckers.size(); i++) {
345 HandlerChecker hc = mHandlerCheckers.get(i);
346 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348
349 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
350 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700351 // 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 -0800352 // positive on when to kill things.
353 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700354 while (timeout > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700356 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800358 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700360 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700363 final int waitState = evaluateCheckerCompletionLocked();
364 if (waitState == COMPLETED) {
365 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700366 waitedHalf = false;
367 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700368 } else if (waitState == WAITING) {
369 // still waiting but within their configured intervals; back off and recheck
370 continue;
371 } else if (waitState == WAITED_HALF) {
372 if (!waitedHalf) {
373 // We've waited half the deadlock-detection interval. Pull a stack
374 // trace and wait another half.
375 ArrayList<Integer> pids = new ArrayList<Integer>();
376 pids.add(Process.myPid());
377 ActivityManagerService.dumpStackTraces(true, pids, null, null,
378 NATIVE_STACKS_OF_INTEREST);
379 waitedHalf = true;
380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 continue;
382 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700383
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700384 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700385 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700386 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700387 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
389
390 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700391 // First collect stack traces from all threads of the system process.
392 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700393 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700395 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800396 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800397 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700398 // Pass !waitedHalf so that just in case we somehow wind up here without having
399 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800400 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700401 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800402
403 // Give some extra time to make sure the stack traces get written.
404 // The system's been hanging for a minute, another second or two won't hurt much.
405 SystemClock.sleep(2000);
406
Christopher Tateecaa7b42010-06-04 14:55:02 -0700407 // Pull our own kernel thread stacks as well if we're configured for that
408 if (RECORD_KERNEL_THREADS) {
409 dumpKernelStackTraces();
410 }
411
Colin Cross5df1d872012-11-29 11:42:11 -0800412 // Trigger the kernel to dump all blocked threads to the kernel log
413 try {
414 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
415 sysrq_trigger.write("w");
416 sysrq_trigger.close();
417 } catch (IOException e) {
418 Slog.e(TAG, "Failed to write to /proc/sysrq-trigger");
419 Slog.e(TAG, e.getMessage());
420 }
421
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800422 // Try to add the error to the dropbox, but assuming that the ActivityManager
423 // itself may be deadlocked. (which has happened, causing this statement to
424 // deadlock and the watchdog as a whole to be ineffective)
425 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
426 public void run() {
427 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700428 "watchdog", null, "system_server", null, null,
Jeff Brown7dd2d192013-09-06 15:05:23 -0700429 subject, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800430 }
431 };
432 dropboxThread.start();
433 try {
434 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
435 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700437 IActivityController controller;
438 synchronized (this) {
439 controller = mController;
440 }
441 if (controller != null) {
442 Slog.i(TAG, "Reporting stuck state to activity controller");
443 try {
444 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
445 // 1 = keep waiting, -1 = kill system
Jeff Brown7dd2d192013-09-06 15:05:23 -0700446 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700447 if (res >= 0) {
448 Slog.i(TAG, "Activity controller requested to coninue to wait");
449 waitedHalf = false;
450 continue;
451 }
452 } catch (RemoteException e) {
453 }
454 }
455
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700456 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700457 if (Debug.isDebuggerConnected()) {
458 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
459 } else if (!allowRestart) {
460 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
461 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700462 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700463 for (int i=0; i<blockedCheckers.size(); i++) {
464 Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:");
465 StackTraceElement[] stackTrace
466 = blockedCheckers.get(i).getThread().getStackTrace();
467 for (StackTraceElement element: stackTrace) {
468 Slog.w(TAG, " at " + element);
469 }
Michael Wright56a6c662013-04-30 20:13:07 -0700470 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700471 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700472 Process.killProcess(Process.myPid());
473 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700475
476 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700479
480 private File dumpKernelStackTraces() {
481 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
482 if (tracesPath == null || tracesPath.length() == 0) {
483 return null;
484 }
485
486 native_dumpKernelStacks(tracesPath);
487 return new File(tracesPath);
488 }
489
490 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491}