blob: 80547882c39cd2f3dd41dc14b841a70b5896ce48 [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
Dianne Hackbornf72467a2012-06-08 17:23:59 -070070 static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
71 "/system/bin/mediaserver",
72 "/system/bin/sdcard",
73 "/system/bin/surfaceflinger"
74 };
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 static Watchdog sWatchdog;
77
78 /* This handler will be used to post message back onto the main thread */
Dianne Hackborn8d044e82013-04-30 17:24:15 -070079 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<HandlerChecker>();
80 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 ContentResolver mResolver;
82 BatteryService mBattery;
83 PowerManagerService mPower;
84 AlarmManagerService mAlarm;
85 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() {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700115 if (mMonitors.size() == 0 && mHandler.getLooper().isIdling()) {
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700116 // If the target looper is or just recently was idling, then
117 // 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
200 public interface Monitor {
201 void monitor();
202 }
203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 public static Watchdog getInstance() {
205 if (sWatchdog == null) {
206 sWatchdog = new Watchdog();
207 }
208
209 return sWatchdog;
210 }
211
212 private Watchdog() {
213 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700214 // Initialize handler checkers for each common thread we want to check. Note
215 // that we are not currently checking the background thread, since it can
216 // potentially hold longer running operations with no guarantees about the timeliness
217 // of operations there.
218
219 // The shared foreground thread is the main checker. It is where we
220 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700221 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
222 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700223 mHandlerCheckers.add(mMonitorChecker);
224 // Add checker for main thread. We only do a quick check since there
225 // can be UI running on the thread.
226 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700227 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700228 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700229 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
230 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700231 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700232 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
233 "i/o thread", DEFAULT_TIMEOUT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 }
235
236 public void init(Context context, BatteryService battery,
237 PowerManagerService power, AlarmManagerService alarm,
238 ActivityManagerService activity) {
239 mResolver = context.getContentResolver();
240 mBattery = battery;
241 mPower = power;
242 mAlarm = alarm;
243 mActivity = activity;
244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 context.registerReceiver(new RebootRequestReceiver(),
246 new IntentFilter(Intent.ACTION_REBOOT),
247 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
249
Christopher Tatec27181c2010-06-30 14:41:09 -0700250 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 synchronized (this) {
252 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
255 }
256 }
257
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700258 public void setActivityController(IActivityController controller) {
259 synchronized (this) {
260 mController = controller;
261 }
262 }
263
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700264 public void setAllowRestart(boolean allowRestart) {
265 synchronized (this) {
266 mAllowRestart = allowRestart;
267 }
268 }
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public void addMonitor(Monitor monitor) {
271 synchronized (this) {
272 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700273 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700275 mMonitorChecker.addMonitor(monitor);
276 }
277 }
278
279 public void addThread(Handler thread, String name) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700280 addThread(thread, name, DEFAULT_TIMEOUT);
281 }
282
283 public void addThread(Handler thread, String name, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700284 synchronized (this) {
285 if (isAlive()) {
286 throw new RuntimeException("Threads can't be added once the Watchdog is running");
287 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700288 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 }
290 }
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 /**
293 * Perform a full reboot of the system.
294 */
295 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800296 Slog.i(TAG, "Rebooting system because: " + reason);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800297 PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700298 pms.reboot(false, reason, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
300
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700301 private int evaluateCheckerCompletionLocked() {
302 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700303 for (int i=0; i<mHandlerCheckers.size(); i++) {
304 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700305 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700306 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700307 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700308 }
309
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700310 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
311 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700312 for (int i=0; i<mHandlerCheckers.size(); i++) {
313 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700314 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700315 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700316 }
317 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700318 return checkers;
319 }
320
321 private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) {
322 StringBuilder builder = new StringBuilder(128);
323 for (int i=0; i<checkers.size(); i++) {
324 if (builder.length() > 0) {
325 builder.append(", ");
326 }
327 builder.append(checkers.get(i).describeBlockedStateLocked());
328 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700329 return builder.toString();
330 }
331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 @Override
333 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700334 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700336 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700337 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700338 final boolean allowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700340 long timeout = CHECK_INTERVAL;
341 // Make sure we (re)spin the checkers that have become idle within
342 // this wait-and-check interval
343 for (int i=0; i<mHandlerCheckers.size(); i++) {
344 HandlerChecker hc = mHandlerCheckers.get(i);
345 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347
348 // 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) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700355 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800357 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700359 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700362 final int waitState = evaluateCheckerCompletionLocked();
363 if (waitState == COMPLETED) {
364 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700365 waitedHalf = false;
366 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700367 } else if (waitState == WAITING) {
368 // still waiting but within their configured intervals; back off and recheck
369 continue;
370 } else if (waitState == WAITED_HALF) {
371 if (!waitedHalf) {
372 // We've waited half the deadlock-detection interval. Pull a stack
373 // trace and wait another half.
374 ArrayList<Integer> pids = new ArrayList<Integer>();
375 pids.add(Process.myPid());
376 ActivityManagerService.dumpStackTraces(true, pids, null, null,
377 NATIVE_STACKS_OF_INTEREST);
378 waitedHalf = true;
379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 continue;
381 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700382
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700383 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700384 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700385 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700386 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
388
389 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700390 // First collect stack traces from all threads of the system process.
391 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700392 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700394 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800395 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800396 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700397 // Pass !waitedHalf so that just in case we somehow wind up here without having
398 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800399 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700400 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800401
402 // Give some extra time to make sure the stack traces get written.
403 // The system's been hanging for a minute, another second or two won't hurt much.
404 SystemClock.sleep(2000);
405
Christopher Tateecaa7b42010-06-04 14:55:02 -0700406 // Pull our own kernel thread stacks as well if we're configured for that
407 if (RECORD_KERNEL_THREADS) {
408 dumpKernelStackTraces();
409 }
410
Colin Cross5df1d872012-11-29 11:42:11 -0800411 // Trigger the kernel to dump all blocked threads to the kernel log
412 try {
413 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
414 sysrq_trigger.write("w");
415 sysrq_trigger.close();
416 } catch (IOException e) {
417 Slog.e(TAG, "Failed to write to /proc/sysrq-trigger");
418 Slog.e(TAG, e.getMessage());
419 }
420
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()) {
457 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
458 } else if (!allowRestart) {
459 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
460 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700461 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700462 for (int i=0; i<blockedCheckers.size(); i++) {
463 Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:");
464 StackTraceElement[] stackTrace
465 = blockedCheckers.get(i).getThread().getStackTrace();
466 for (StackTraceElement element: stackTrace) {
467 Slog.w(TAG, " at " + element);
468 }
Michael Wright56a6c662013-04-30 20:13:07 -0700469 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700470 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700471 Process.killProcess(Process.myPid());
472 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700474
475 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700478
479 private File dumpKernelStackTraces() {
480 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
481 if (tracesPath == null || tracesPath.length() == 0) {
482 return null;
483 }
484
485 native_dumpKernelStacks(tracesPath);
486 return new File(tracesPath);
487 }
488
489 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490}