blob: b2a8ad834dc4531bc7a3ea9b562e5e8573b81a27 [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
19import com.android.server.am.ActivityManagerService;
Jeff Brown4f8ecd82012-06-18 18:29:13 -070020import com.android.server.power.PowerManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22import android.app.AlarmManager;
23import android.app.PendingIntent;
24import android.content.BroadcastReceiver;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
Jeff Browna4d82042012-10-02 19:11:19 -070029import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Debug;
31import android.os.Handler;
32import android.os.Message;
33import 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;
45import java.util.Calendar;
46
47/** This class calls its monitor every minute. Killing this process if they don't return **/
48public class Watchdog extends Thread {
49 static final String TAG = "Watchdog";
Joe Onorato43a17652011-04-06 19:22:23 -070050 static final boolean localLOGV = false || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
52 // Set this to true to use debug default values.
53 static final boolean DB = false;
54
Christopher Tateecaa7b42010-06-04 14:55:02 -070055 // Set this to true to have the watchdog record kernel thread stacks when it fires
56 static final boolean RECORD_KERNEL_THREADS = true;
57
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 static final int MONITOR = 2718;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
Mathias Agopiancf2317e2011-08-25 17:12:37 -070060 static final int TIME_TO_RESTART = DB ? 15*1000 : 60*1000;
Christopher Tate6ee412d2010-05-28 12:01:56 -070061 static final int TIME_TO_WAIT = TIME_TO_RESTART / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 static final int MEMCHECK_DEFAULT_MIN_SCREEN_OFF = DB ? 1*60 : 5*60; // 5 minutes
64 static final int MEMCHECK_DEFAULT_MIN_ALARM = DB ? 1*60 : 3*60; // 3 minutes
65 static final int MEMCHECK_DEFAULT_RECHECK_INTERVAL = DB ? 1*60 : 5*60; // 5 minutes
66
67 static final int REBOOT_DEFAULT_INTERVAL = DB ? 1 : 0; // never force reboot
68 static final int REBOOT_DEFAULT_START_TIME = 3*60*60; // 3:00am
69 static final int REBOOT_DEFAULT_WINDOW = 60*60; // within 1 hour
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 static final String REBOOT_ACTION = "com.android.service.Watchdog.REBOOT";
72
Dianne Hackbornf72467a2012-06-08 17:23:59 -070073 static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
74 "/system/bin/mediaserver",
75 "/system/bin/sdcard",
76 "/system/bin/surfaceflinger"
77 };
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 static Watchdog sWatchdog;
80
81 /* This handler will be used to post message back onto the main thread */
82 final Handler mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
84 ContentResolver mResolver;
85 BatteryService mBattery;
86 PowerManagerService mPower;
87 AlarmManagerService mAlarm;
88 ActivityManagerService mActivity;
89 boolean mCompleted;
90 boolean mForceKillSystem;
91 Monitor mCurrentMonitor;
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 int mPhonePid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95 final Calendar mCalendar = Calendar.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 int mMinScreenOff = MEMCHECK_DEFAULT_MIN_SCREEN_OFF;
97 int mMinAlarm = MEMCHECK_DEFAULT_MIN_ALARM;
98 boolean mNeedScheduledCheck;
99 PendingIntent mCheckupIntent;
100 PendingIntent mRebootIntent;
101
102 long mBootTime;
103 int mRebootInterval;
104
105 boolean mReqRebootNoWait; // should wait for one interval before reboot?
106 int mReqRebootInterval = -1; // >= 0 if a reboot has been requested
107 int mReqRebootStartTime = -1; // >= 0 if a specific start time has been requested
108 int mReqRebootWindow = -1; // >= 0 if a specific window has been requested
109 int mReqMinScreenOff = -1; // >= 0 if a specific screen off time has been requested
110 int mReqMinNextAlarm = -1; // >= 0 if specific time to next alarm has been requested
111 int mReqRecheckInterval= -1; // >= 0 if a specific recheck interval has been requested
112
113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 * Used for scheduling monitor callbacks and checking memory usage.
115 */
116 final class HeartbeatHandler extends Handler {
117 @Override
118 public void handleMessage(Message msg) {
119 switch (msg.what) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 case MONITOR: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 // See if we should force a reboot.
122 int rebootInterval = mReqRebootInterval >= 0
Jeff Sharkey4de99362012-09-26 17:58:19 -0700123 ? mReqRebootInterval : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 if (mRebootInterval != rebootInterval) {
125 mRebootInterval = rebootInterval;
126 // We have been running long enough that a reboot can
127 // be considered...
128 checkReboot(false);
129 }
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final int size = mMonitors.size();
132 for (int i = 0 ; i < size ; i++) {
133 mCurrentMonitor = mMonitors.get(i);
134 mCurrentMonitor.monitor();
135 }
136
137 synchronized (Watchdog.this) {
138 mCompleted = true;
139 mCurrentMonitor = null;
140 }
141 } break;
142 }
143 }
144 }
145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 final class RebootReceiver extends BroadcastReceiver {
147 @Override
148 public void onReceive(Context c, Intent intent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800149 if (localLOGV) Slog.v(TAG, "Alarm went off, checking reboot.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 checkReboot(true);
151 }
152 }
153
154 final class RebootRequestReceiver extends BroadcastReceiver {
155 @Override
156 public void onReceive(Context c, Intent intent) {
157 mReqRebootNoWait = intent.getIntExtra("nowait", 0) != 0;
158 mReqRebootInterval = intent.getIntExtra("interval", -1);
159 mReqRebootStartTime = intent.getIntExtra("startTime", -1);
160 mReqRebootWindow = intent.getIntExtra("window", -1);
161 mReqMinScreenOff = intent.getIntExtra("minScreenOff", -1);
162 mReqMinNextAlarm = intent.getIntExtra("minNextAlarm", -1);
163 mReqRecheckInterval = intent.getIntExtra("recheckInterval", -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800164 EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 mReqRebootNoWait ? 1 : 0, mReqRebootInterval,
166 mReqRecheckInterval, mReqRebootStartTime,
167 mReqRebootWindow, mReqMinScreenOff, mReqMinNextAlarm);
168 checkReboot(true);
169 }
170 }
171
172 public interface Monitor {
173 void monitor();
174 }
175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static Watchdog getInstance() {
177 if (sWatchdog == null) {
178 sWatchdog = new Watchdog();
179 }
180
181 return sWatchdog;
182 }
183
184 private Watchdog() {
185 super("watchdog");
186 mHandler = new HeartbeatHandler();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
188
189 public void init(Context context, BatteryService battery,
190 PowerManagerService power, AlarmManagerService alarm,
191 ActivityManagerService activity) {
192 mResolver = context.getContentResolver();
193 mBattery = battery;
194 mPower = power;
195 mAlarm = alarm;
196 mActivity = activity;
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 context.registerReceiver(new RebootReceiver(),
199 new IntentFilter(REBOOT_ACTION));
200 mRebootIntent = PendingIntent.getBroadcast(context,
201 0, new Intent(REBOOT_ACTION), 0);
202
203 context.registerReceiver(new RebootRequestReceiver(),
204 new IntentFilter(Intent.ACTION_REBOOT),
205 android.Manifest.permission.REBOOT, null);
206
207 mBootTime = System.currentTimeMillis();
208 }
209
Christopher Tatec27181c2010-06-30 14:41:09 -0700210 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 synchronized (this) {
212 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
215 }
216 }
217
218 public void addMonitor(Monitor monitor) {
219 synchronized (this) {
220 if (isAlive()) {
221 throw new RuntimeException("Monitors can't be added while the Watchdog is running");
222 }
223 mMonitors.add(monitor);
224 }
225 }
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 void checkReboot(boolean fromAlarm) {
228 int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700229 : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 mRebootInterval = rebootInterval;
231 if (rebootInterval <= 0) {
232 // No reboot interval requested.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800233 if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 mAlarm.remove(mRebootIntent);
235 return;
236 }
237
238 long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime
Jeff Sharkey4de99362012-09-26 17:58:19 -0700239 : REBOOT_DEFAULT_START_TIME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow
Jeff Sharkey4de99362012-09-26 17:58:19 -0700241 : REBOOT_DEFAULT_WINDOW) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700243 : MEMCHECK_DEFAULT_RECHECK_INTERVAL) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
245 retrieveBrutalityAmount();
246
247 long realStartTime;
248 long now;
249
250 synchronized (this) {
251 now = System.currentTimeMillis();
252 realStartTime = computeCalendarTime(mCalendar, now,
253 rebootStartTime);
254
255 long rebootIntervalMillis = rebootInterval*24*60*60*1000;
256 if (DB || mReqRebootNoWait ||
257 (now-mBootTime) >= (rebootIntervalMillis-rebootWindowMillis)) {
258 if (fromAlarm && rebootWindowMillis <= 0) {
259 // No reboot window -- just immediately reboot.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800260 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 (int)rebootIntervalMillis, (int)rebootStartTime*1000,
262 (int)rebootWindowMillis, "");
263 rebootSystem("Checkin scheduled forced");
264 return;
265 }
266
267 // Are we within the reboot window?
268 if (now < realStartTime) {
269 // Schedule alarm for next check interval.
270 realStartTime = computeCalendarTime(mCalendar,
271 now, rebootStartTime);
272 } else if (now < (realStartTime+rebootWindowMillis)) {
273 String doit = shouldWeBeBrutalLocked(now);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800274 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 (int)rebootInterval, (int)rebootStartTime*1000,
276 (int)rebootWindowMillis, doit != null ? doit : "");
277 if (doit == null) {
278 rebootSystem("Checked scheduled range");
279 return;
280 }
281
282 // Schedule next alarm either within the window or in the
283 // next interval.
284 if ((now+recheckInterval) >= (realStartTime+rebootWindowMillis)) {
285 realStartTime = computeCalendarTime(mCalendar,
286 now + rebootIntervalMillis, rebootStartTime);
287 } else {
288 realStartTime = now + recheckInterval;
289 }
290 } else {
291 // Schedule alarm for next check interval.
292 realStartTime = computeCalendarTime(mCalendar,
293 now + rebootIntervalMillis, rebootStartTime);
294 }
295 }
296 }
297
Joe Onorato8a9b2202010-02-26 18:56:32 -0800298 if (localLOGV) Slog.v(TAG, "Scheduling next reboot alarm for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 + ((realStartTime-now)/1000/60) + "m from now");
300 mAlarm.remove(mRebootIntent);
301 mAlarm.set(AlarmManager.RTC_WAKEUP, realStartTime, mRebootIntent);
302 }
303
304 /**
305 * Perform a full reboot of the system.
306 */
307 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800308 Slog.i(TAG, "Rebooting system because: " + reason);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800309 PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700310 pms.reboot(false, reason, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
312
313 /**
314 * Load the current Gservices settings for when
315 * {@link #shouldWeBeBrutalLocked} will allow the brutality to happen.
316 * Must not be called with the lock held.
317 */
318 void retrieveBrutalityAmount() {
319 mMinScreenOff = (mReqMinScreenOff >= 0 ? mReqMinScreenOff
Jeff Sharkey4de99362012-09-26 17:58:19 -0700320 : MEMCHECK_DEFAULT_MIN_SCREEN_OFF) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm
Jeff Sharkey4de99362012-09-26 17:58:19 -0700322 : MEMCHECK_DEFAULT_MIN_ALARM) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 }
324
325 /**
326 * Determine whether it is a good time to kill, crash, or otherwise
327 * plunder the current situation for the overall long-term benefit of
328 * the world.
329 *
330 * @param curTime The current system time.
331 * @return Returns null if this is a good time, else a String with the
332 * text of why it is not a good time.
333 */
334 String shouldWeBeBrutalLocked(long curTime) {
Jeff Browna4d82042012-10-02 19:11:19 -0700335 if (mBattery == null || !mBattery.isPowered(BatteryManager.BATTERY_PLUGGED_ANY)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 return "battery";
337 }
338
339 if (mMinScreenOff >= 0 && (mPower == null ||
Jeff Brown96307042012-07-27 15:51:34 -0700340 mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 return "screen";
342 }
343
344 if (mMinAlarm >= 0 && (mAlarm == null ||
345 mAlarm.timeToNextAlarm() < mMinAlarm)) {
346 return "alarm";
347 }
348
349 return null;
350 }
351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 static long computeCalendarTime(Calendar c, long curTime,
353 long secondsSinceMidnight) {
354
355 // start with now
356 c.setTimeInMillis(curTime);
357
358 int val = (int)secondsSinceMidnight / (60*60);
359 c.set(Calendar.HOUR_OF_DAY, val);
360 secondsSinceMidnight -= val * (60*60);
361 val = (int)secondsSinceMidnight / 60;
362 c.set(Calendar.MINUTE, val);
363 c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60));
364 c.set(Calendar.MILLISECOND, 0);
365
366 long newTime = c.getTimeInMillis();
367 if (newTime < curTime) {
368 // The given time (in seconds since midnight) has already passed for today, so advance
369 // by one day (due to daylight savings, etc., the delta may differ from 24 hours).
370 c.add(Calendar.DAY_OF_MONTH, 1);
371 newTime = c.getTimeInMillis();
372 }
373
374 return newTime;
375 }
376
377 @Override
378 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700379 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 while (true) {
381 mCompleted = false;
382 mHandler.sendEmptyMessage(MONITOR);
383
384 synchronized (this) {
385 long timeout = TIME_TO_WAIT;
386
387 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
388 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700389 // 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 -0800390 // positive on when to kill things.
391 long start = SystemClock.uptimeMillis();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800392 while (timeout > 0 && !mForceKillSystem) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 try {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800394 wait(timeout); // notifyAll() is called when mForceKillSystem is set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800396 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
398 timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400
401 if (mCompleted && !mForceKillSystem) {
402 // The monitors have returned.
Christopher Tate6ee412d2010-05-28 12:01:56 -0700403 waitedHalf = false;
404 continue;
405 }
406
407 if (!waitedHalf) {
408 // We've waited half the deadlock-detection interval. Pull a stack
409 // trace and wait another half.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700410 ArrayList<Integer> pids = new ArrayList<Integer>();
Christopher Tate6ee412d2010-05-28 12:01:56 -0700411 pids.add(Process.myPid());
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700412 ActivityManagerService.dumpStackTraces(true, pids, null, null,
413 NATIVE_STACKS_OF_INTEREST);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700414 waitedHalf = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 continue;
416 }
417 }
418
419 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700420 // First collect stack traces from all threads of the system process.
421 // Then kill this process so that the system will restart.
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800422
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800423 final String name = (mCurrentMonitor != null) ?
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700424 mCurrentMonitor.getClass().getName() : "null";
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800425 EventLog.writeEvent(EventLogTags.WATCHDOG, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700427 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800428 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800429 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700430 // Pass !waitedHalf so that just in case we somehow wind up here without having
431 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800432 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700433 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800434
435 // Give some extra time to make sure the stack traces get written.
436 // The system's been hanging for a minute, another second or two won't hurt much.
437 SystemClock.sleep(2000);
438
Christopher Tateecaa7b42010-06-04 14:55:02 -0700439 // Pull our own kernel thread stacks as well if we're configured for that
440 if (RECORD_KERNEL_THREADS) {
441 dumpKernelStackTraces();
442 }
443
Colin Cross5df1d872012-11-29 11:42:11 -0800444 // Trigger the kernel to dump all blocked threads to the kernel log
445 try {
446 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
447 sysrq_trigger.write("w");
448 sysrq_trigger.close();
449 } catch (IOException e) {
450 Slog.e(TAG, "Failed to write to /proc/sysrq-trigger");
451 Slog.e(TAG, e.getMessage());
452 }
453
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800454 // Try to add the error to the dropbox, but assuming that the ActivityManager
455 // itself may be deadlocked. (which has happened, causing this statement to
456 // deadlock and the watchdog as a whole to be ineffective)
457 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
458 public void run() {
459 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700460 "watchdog", null, "system_server", null, null,
461 name, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800462 }
463 };
464 dropboxThread.start();
465 try {
466 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
467 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700469 // Only kill the process if the debugger is not attached.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 if (!Debug.isDebuggerConnected()) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800471 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name);
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700472 Process.killProcess(Process.myPid());
473 System.exit(10);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800474 } else {
475 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700477
478 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700481
482 private File dumpKernelStackTraces() {
483 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
484 if (tracesPath == null || tracesPath.length() == 0) {
485 return null;
486 }
487
488 native_dumpKernelStacks(tracesPath);
489 return new File(tracesPath);
490 }
491
492 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493}