blob: c5b4a076a04c2050c64c6daeca8d25010593adc5 [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;
29import android.os.Debug;
30import android.os.Handler;
31import android.os.Message;
32import android.os.Process;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080033import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.SystemClock;
35import android.os.SystemProperties;
36import android.provider.Settings;
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import java.util.ArrayList;
43import java.util.Calendar;
44
45/** This class calls its monitor every minute. Killing this process if they don't return **/
46public class Watchdog extends Thread {
47 static final String TAG = "Watchdog";
Joe Onorato43a17652011-04-06 19:22:23 -070048 static final boolean localLOGV = false || false;
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 static final int MONITOR = 2718;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Mathias Agopiancf2317e2011-08-25 17:12:37 -070058 static final int TIME_TO_RESTART = DB ? 15*1000 : 60*1000;
Christopher Tate6ee412d2010-05-28 12:01:56 -070059 static final int TIME_TO_WAIT = TIME_TO_RESTART / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 static final int MEMCHECK_DEFAULT_MIN_SCREEN_OFF = DB ? 1*60 : 5*60; // 5 minutes
62 static final int MEMCHECK_DEFAULT_MIN_ALARM = DB ? 1*60 : 3*60; // 3 minutes
63 static final int MEMCHECK_DEFAULT_RECHECK_INTERVAL = DB ? 1*60 : 5*60; // 5 minutes
64
65 static final int REBOOT_DEFAULT_INTERVAL = DB ? 1 : 0; // never force reboot
66 static final int REBOOT_DEFAULT_START_TIME = 3*60*60; // 3:00am
67 static final int REBOOT_DEFAULT_WINDOW = 60*60; // within 1 hour
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 static final String REBOOT_ACTION = "com.android.service.Watchdog.REBOOT";
70
Dianne Hackbornf72467a2012-06-08 17:23:59 -070071 static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
72 "/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 */
80 final Handler mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
82 ContentResolver mResolver;
83 BatteryService mBattery;
84 PowerManagerService mPower;
85 AlarmManagerService mAlarm;
86 ActivityManagerService mActivity;
87 boolean mCompleted;
88 boolean mForceKillSystem;
89 Monitor mCurrentMonitor;
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 int mPhonePid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 final Calendar mCalendar = Calendar.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 int mMinScreenOff = MEMCHECK_DEFAULT_MIN_SCREEN_OFF;
95 int mMinAlarm = MEMCHECK_DEFAULT_MIN_ALARM;
96 boolean mNeedScheduledCheck;
97 PendingIntent mCheckupIntent;
98 PendingIntent mRebootIntent;
99
100 long mBootTime;
101 int mRebootInterval;
102
103 boolean mReqRebootNoWait; // should wait for one interval before reboot?
104 int mReqRebootInterval = -1; // >= 0 if a reboot has been requested
105 int mReqRebootStartTime = -1; // >= 0 if a specific start time has been requested
106 int mReqRebootWindow = -1; // >= 0 if a specific window has been requested
107 int mReqMinScreenOff = -1; // >= 0 if a specific screen off time has been requested
108 int mReqMinNextAlarm = -1; // >= 0 if specific time to next alarm has been requested
109 int mReqRecheckInterval= -1; // >= 0 if a specific recheck interval has been requested
110
111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * Used for scheduling monitor callbacks and checking memory usage.
113 */
114 final class HeartbeatHandler extends Handler {
115 @Override
116 public void handleMessage(Message msg) {
117 switch (msg.what) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 case MONITOR: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 // See if we should force a reboot.
120 int rebootInterval = mReqRebootInterval >= 0
Doug Zongkerf6888892010-01-06 16:38:14 -0800121 ? mReqRebootInterval : Settings.Secure.getInt(
122 mResolver, Settings.Secure.REBOOT_INTERVAL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 REBOOT_DEFAULT_INTERVAL);
124 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
Doug Zongkerf6888892010-01-06 16:38:14 -0800229 : Settings.Secure.getInt(
230 mResolver, Settings.Secure.REBOOT_INTERVAL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 REBOOT_DEFAULT_INTERVAL);
232 mRebootInterval = rebootInterval;
233 if (rebootInterval <= 0) {
234 // No reboot interval requested.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800235 if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 mAlarm.remove(mRebootIntent);
237 return;
238 }
239
240 long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime
Doug Zongkerf6888892010-01-06 16:38:14 -0800241 : Settings.Secure.getLong(
242 mResolver, Settings.Secure.REBOOT_START_TIME,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 REBOOT_DEFAULT_START_TIME);
244 long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow
Doug Zongkerf6888892010-01-06 16:38:14 -0800245 : Settings.Secure.getLong(
246 mResolver, Settings.Secure.REBOOT_WINDOW,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 REBOOT_DEFAULT_WINDOW)) * 1000;
248 long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval
Doug Zongkerf6888892010-01-06 16:38:14 -0800249 : Settings.Secure.getLong(
250 mResolver, Settings.Secure.MEMCHECK_RECHECK_INTERVAL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 MEMCHECK_DEFAULT_RECHECK_INTERVAL)) * 1000;
252
253 retrieveBrutalityAmount();
254
255 long realStartTime;
256 long now;
257
258 synchronized (this) {
259 now = System.currentTimeMillis();
260 realStartTime = computeCalendarTime(mCalendar, now,
261 rebootStartTime);
262
263 long rebootIntervalMillis = rebootInterval*24*60*60*1000;
264 if (DB || mReqRebootNoWait ||
265 (now-mBootTime) >= (rebootIntervalMillis-rebootWindowMillis)) {
266 if (fromAlarm && rebootWindowMillis <= 0) {
267 // No reboot window -- just immediately reboot.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800268 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 (int)rebootIntervalMillis, (int)rebootStartTime*1000,
270 (int)rebootWindowMillis, "");
271 rebootSystem("Checkin scheduled forced");
272 return;
273 }
274
275 // Are we within the reboot window?
276 if (now < realStartTime) {
277 // Schedule alarm for next check interval.
278 realStartTime = computeCalendarTime(mCalendar,
279 now, rebootStartTime);
280 } else if (now < (realStartTime+rebootWindowMillis)) {
281 String doit = shouldWeBeBrutalLocked(now);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800282 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 (int)rebootInterval, (int)rebootStartTime*1000,
284 (int)rebootWindowMillis, doit != null ? doit : "");
285 if (doit == null) {
286 rebootSystem("Checked scheduled range");
287 return;
288 }
289
290 // Schedule next alarm either within the window or in the
291 // next interval.
292 if ((now+recheckInterval) >= (realStartTime+rebootWindowMillis)) {
293 realStartTime = computeCalendarTime(mCalendar,
294 now + rebootIntervalMillis, rebootStartTime);
295 } else {
296 realStartTime = now + recheckInterval;
297 }
298 } else {
299 // Schedule alarm for next check interval.
300 realStartTime = computeCalendarTime(mCalendar,
301 now + rebootIntervalMillis, rebootStartTime);
302 }
303 }
304 }
305
Joe Onorato8a9b2202010-02-26 18:56:32 -0800306 if (localLOGV) Slog.v(TAG, "Scheduling next reboot alarm for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 + ((realStartTime-now)/1000/60) + "m from now");
308 mAlarm.remove(mRebootIntent);
309 mAlarm.set(AlarmManager.RTC_WAKEUP, realStartTime, mRebootIntent);
310 }
311
312 /**
313 * Perform a full reboot of the system.
314 */
315 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800316 Slog.i(TAG, "Rebooting system because: " + reason);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800317 PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
318 pms.reboot(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 }
320
321 /**
322 * Load the current Gservices settings for when
323 * {@link #shouldWeBeBrutalLocked} will allow the brutality to happen.
324 * Must not be called with the lock held.
325 */
326 void retrieveBrutalityAmount() {
327 mMinScreenOff = (mReqMinScreenOff >= 0 ? mReqMinScreenOff
Doug Zongkerf6888892010-01-06 16:38:14 -0800328 : Settings.Secure.getInt(
329 mResolver, Settings.Secure.MEMCHECK_MIN_SCREEN_OFF,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 MEMCHECK_DEFAULT_MIN_SCREEN_OFF)) * 1000;
331 mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm
Doug Zongkerf6888892010-01-06 16:38:14 -0800332 : Settings.Secure.getInt(
333 mResolver, Settings.Secure.MEMCHECK_MIN_ALARM,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 MEMCHECK_DEFAULT_MIN_ALARM)) * 1000;
335 }
336
337 /**
338 * Determine whether it is a good time to kill, crash, or otherwise
339 * plunder the current situation for the overall long-term benefit of
340 * the world.
341 *
342 * @param curTime The current system time.
343 * @return Returns null if this is a good time, else a String with the
344 * text of why it is not a good time.
345 */
346 String shouldWeBeBrutalLocked(long curTime) {
347 if (mBattery == null || !mBattery.isPowered()) {
348 return "battery";
349 }
350
351 if (mMinScreenOff >= 0 && (mPower == null ||
352 mPower.timeSinceScreenOn() < mMinScreenOff)) {
353 return "screen";
354 }
355
356 if (mMinAlarm >= 0 && (mAlarm == null ||
357 mAlarm.timeToNextAlarm() < mMinAlarm)) {
358 return "alarm";
359 }
360
361 return null;
362 }
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 static long computeCalendarTime(Calendar c, long curTime,
365 long secondsSinceMidnight) {
366
367 // start with now
368 c.setTimeInMillis(curTime);
369
370 int val = (int)secondsSinceMidnight / (60*60);
371 c.set(Calendar.HOUR_OF_DAY, val);
372 secondsSinceMidnight -= val * (60*60);
373 val = (int)secondsSinceMidnight / 60;
374 c.set(Calendar.MINUTE, val);
375 c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60));
376 c.set(Calendar.MILLISECOND, 0);
377
378 long newTime = c.getTimeInMillis();
379 if (newTime < curTime) {
380 // The given time (in seconds since midnight) has already passed for today, so advance
381 // by one day (due to daylight savings, etc., the delta may differ from 24 hours).
382 c.add(Calendar.DAY_OF_MONTH, 1);
383 newTime = c.getTimeInMillis();
384 }
385
386 return newTime;
387 }
388
389 @Override
390 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700391 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 while (true) {
393 mCompleted = false;
394 mHandler.sendEmptyMessage(MONITOR);
395
396 synchronized (this) {
397 long timeout = TIME_TO_WAIT;
398
399 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
400 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700401 // 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 -0800402 // positive on when to kill things.
403 long start = SystemClock.uptimeMillis();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800404 while (timeout > 0 && !mForceKillSystem) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 try {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800406 wait(timeout); // notifyAll() is called when mForceKillSystem is set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800408 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410 timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412
413 if (mCompleted && !mForceKillSystem) {
414 // The monitors have returned.
Christopher Tate6ee412d2010-05-28 12:01:56 -0700415 waitedHalf = false;
416 continue;
417 }
418
419 if (!waitedHalf) {
420 // We've waited half the deadlock-detection interval. Pull a stack
421 // trace and wait another half.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700422 ArrayList<Integer> pids = new ArrayList<Integer>();
Christopher Tate6ee412d2010-05-28 12:01:56 -0700423 pids.add(Process.myPid());
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700424 ActivityManagerService.dumpStackTraces(true, pids, null, null,
425 NATIVE_STACKS_OF_INTEREST);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700426 waitedHalf = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 continue;
428 }
429 }
430
431 // If we got here, that means that the system is most likely hung.
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800432 // First collect stack traces from all threads of the system process.
433 // Then kill this process so that the system will restart.
434
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800435 final String name = (mCurrentMonitor != null) ?
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700436 mCurrentMonitor.getClass().getName() : "null";
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800437 EventLog.writeEvent(EventLogTags.WATCHDOG, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700439 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800440 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800441 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700442 // Pass !waitedHalf so that just in case we somehow wind up here without having
443 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800444 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700445 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800446
447 // Give some extra time to make sure the stack traces get written.
448 // The system's been hanging for a minute, another second or two won't hurt much.
449 SystemClock.sleep(2000);
450
Christopher Tateecaa7b42010-06-04 14:55:02 -0700451 // Pull our own kernel thread stacks as well if we're configured for that
452 if (RECORD_KERNEL_THREADS) {
453 dumpKernelStackTraces();
454 }
455
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800456 // Try to add the error to the dropbox, but assuming that the ActivityManager
457 // itself may be deadlocked. (which has happened, causing this statement to
458 // deadlock and the watchdog as a whole to be ineffective)
459 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
460 public void run() {
461 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700462 "watchdog", null, "system_server", null, null,
463 name, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800464 }
465 };
466 dropboxThread.start();
467 try {
468 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
469 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470
471 // Only kill the process if the debugger is not attached.
472 if (!Debug.isDebuggerConnected()) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800473 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 Process.killProcess(Process.myPid());
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800475 System.exit(10);
476 } else {
477 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
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
484 private File dumpKernelStackTraces() {
485 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
486 if (tracesPath == null || tracesPath.length() == 0) {
487 return null;
488 }
489
490 native_dumpKernelStacks(tracesPath);
491 return new File(tracesPath);
492 }
493
494 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495}