blob: 1663106964dc9661e0760c82eb466227ddddca00 [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;
John Michelau11641522013-03-18 18:28:23 -050032import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Message;
34import android.os.Process;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080035import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.SystemClock;
37import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.util.EventLog;
Dan Egnor9bdc94b2010-03-04 14:20:31 -080039import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080040import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Dan Egnor9bdc94b2010-03-04 14:20:31 -080042import java.io.File;
Colin Cross5df1d872012-11-29 11:42:11 -080043import java.io.FileWriter;
44import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import java.util.ArrayList;
46import java.util.Calendar;
47
48/** This class calls its monitor every minute. Killing this process if they don't return **/
49public class Watchdog extends Thread {
50 static final String TAG = "Watchdog";
Joe Onorato43a17652011-04-06 19:22:23 -070051 static final boolean localLOGV = false || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53 // Set this to true to use debug default values.
54 static final boolean DB = false;
55
Christopher Tateecaa7b42010-06-04 14:55:02 -070056 // Set this to true to have the watchdog record kernel thread stacks when it fires
57 static final boolean RECORD_KERNEL_THREADS = true;
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 static final int MONITOR = 2718;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
Mathias Agopiancf2317e2011-08-25 17:12:37 -070061 static final int TIME_TO_RESTART = DB ? 15*1000 : 60*1000;
Christopher Tate6ee412d2010-05-28 12:01:56 -070062 static final int TIME_TO_WAIT = TIME_TO_RESTART / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 static final int MEMCHECK_DEFAULT_MIN_SCREEN_OFF = DB ? 1*60 : 5*60; // 5 minutes
65 static final int MEMCHECK_DEFAULT_MIN_ALARM = DB ? 1*60 : 3*60; // 3 minutes
66 static final int MEMCHECK_DEFAULT_RECHECK_INTERVAL = DB ? 1*60 : 5*60; // 5 minutes
67
68 static final int REBOOT_DEFAULT_INTERVAL = DB ? 1 : 0; // never force reboot
69 static final int REBOOT_DEFAULT_START_TIME = 3*60*60; // 3:00am
70 static final int REBOOT_DEFAULT_WINDOW = 60*60; // within 1 hour
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 static final String REBOOT_ACTION = "com.android.service.Watchdog.REBOOT";
73
Dianne Hackbornf72467a2012-06-08 17:23:59 -070074 static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
75 "/system/bin/mediaserver",
76 "/system/bin/sdcard",
77 "/system/bin/surfaceflinger"
78 };
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 static Watchdog sWatchdog;
81
82 /* This handler will be used to post message back onto the main thread */
83 final Handler mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
85 ContentResolver mResolver;
86 BatteryService mBattery;
87 PowerManagerService mPower;
88 AlarmManagerService mAlarm;
89 ActivityManagerService mActivity;
90 boolean mCompleted;
91 boolean mForceKillSystem;
92 Monitor mCurrentMonitor;
93
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 int mPhonePid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
96 final Calendar mCalendar = Calendar.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 int mMinScreenOff = MEMCHECK_DEFAULT_MIN_SCREEN_OFF;
98 int mMinAlarm = MEMCHECK_DEFAULT_MIN_ALARM;
99 boolean mNeedScheduledCheck;
100 PendingIntent mCheckupIntent;
101 PendingIntent mRebootIntent;
102
103 long mBootTime;
104 int mRebootInterval;
105
106 boolean mReqRebootNoWait; // should wait for one interval before reboot?
107 int mReqRebootInterval = -1; // >= 0 if a reboot has been requested
108 int mReqRebootStartTime = -1; // >= 0 if a specific start time has been requested
109 int mReqRebootWindow = -1; // >= 0 if a specific window has been requested
110 int mReqMinScreenOff = -1; // >= 0 if a specific screen off time has been requested
111 int mReqMinNextAlarm = -1; // >= 0 if specific time to next alarm has been requested
112 int mReqRecheckInterval= -1; // >= 0 if a specific recheck interval has been requested
113
114 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 * Used for scheduling monitor callbacks and checking memory usage.
116 */
117 final class HeartbeatHandler extends Handler {
John Michelau11641522013-03-18 18:28:23 -0500118 HeartbeatHandler(Looper looper) {
119 super(looper);
120 }
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 @Override
123 public void handleMessage(Message msg) {
124 switch (msg.what) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 case MONITOR: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 // See if we should force a reboot.
127 int rebootInterval = mReqRebootInterval >= 0
Jeff Sharkey4de99362012-09-26 17:58:19 -0700128 ? mReqRebootInterval : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 if (mRebootInterval != rebootInterval) {
130 mRebootInterval = rebootInterval;
131 // We have been running long enough that a reboot can
132 // be considered...
133 checkReboot(false);
134 }
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 final int size = mMonitors.size();
137 for (int i = 0 ; i < size ; i++) {
138 mCurrentMonitor = mMonitors.get(i);
139 mCurrentMonitor.monitor();
140 }
141
142 synchronized (Watchdog.this) {
143 mCompleted = true;
144 mCurrentMonitor = null;
145 }
146 } break;
147 }
148 }
149 }
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 final class RebootReceiver extends BroadcastReceiver {
152 @Override
153 public void onReceive(Context c, Intent intent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800154 if (localLOGV) Slog.v(TAG, "Alarm went off, checking reboot.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 checkReboot(true);
156 }
157 }
158
159 final class RebootRequestReceiver extends BroadcastReceiver {
160 @Override
161 public void onReceive(Context c, Intent intent) {
162 mReqRebootNoWait = intent.getIntExtra("nowait", 0) != 0;
163 mReqRebootInterval = intent.getIntExtra("interval", -1);
164 mReqRebootStartTime = intent.getIntExtra("startTime", -1);
165 mReqRebootWindow = intent.getIntExtra("window", -1);
166 mReqMinScreenOff = intent.getIntExtra("minScreenOff", -1);
167 mReqMinNextAlarm = intent.getIntExtra("minNextAlarm", -1);
168 mReqRecheckInterval = intent.getIntExtra("recheckInterval", -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800169 EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 mReqRebootNoWait ? 1 : 0, mReqRebootInterval,
171 mReqRecheckInterval, mReqRebootStartTime,
172 mReqRebootWindow, mReqMinScreenOff, mReqMinNextAlarm);
173 checkReboot(true);
174 }
175 }
176
177 public interface Monitor {
178 void monitor();
179 }
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 public static Watchdog getInstance() {
182 if (sWatchdog == null) {
183 sWatchdog = new Watchdog();
184 }
185
186 return sWatchdog;
187 }
188
189 private Watchdog() {
190 super("watchdog");
John Michelau11641522013-03-18 18:28:23 -0500191 // Explicitly bind the HeartbeatHandler to run on the ServerThread, so
192 // that it can't get accidentally bound to another thread.
193 mHandler = new HeartbeatHandler(Looper.getMainLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 }
195
196 public void init(Context context, BatteryService battery,
197 PowerManagerService power, AlarmManagerService alarm,
198 ActivityManagerService activity) {
199 mResolver = context.getContentResolver();
200 mBattery = battery;
201 mPower = power;
202 mAlarm = alarm;
203 mActivity = activity;
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 context.registerReceiver(new RebootReceiver(),
206 new IntentFilter(REBOOT_ACTION));
207 mRebootIntent = PendingIntent.getBroadcast(context,
208 0, new Intent(REBOOT_ACTION), 0);
209
210 context.registerReceiver(new RebootRequestReceiver(),
211 new IntentFilter(Intent.ACTION_REBOOT),
212 android.Manifest.permission.REBOOT, null);
213
214 mBootTime = System.currentTimeMillis();
215 }
216
Christopher Tatec27181c2010-06-30 14:41:09 -0700217 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 synchronized (this) {
219 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222 }
223 }
224
225 public void addMonitor(Monitor monitor) {
226 synchronized (this) {
227 if (isAlive()) {
228 throw new RuntimeException("Monitors can't be added while the Watchdog is running");
229 }
230 mMonitors.add(monitor);
231 }
232 }
233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 void checkReboot(boolean fromAlarm) {
235 int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700236 : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 mRebootInterval = rebootInterval;
238 if (rebootInterval <= 0) {
239 // No reboot interval requested.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800240 if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 mAlarm.remove(mRebootIntent);
242 return;
243 }
244
245 long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime
Jeff Sharkey4de99362012-09-26 17:58:19 -0700246 : REBOOT_DEFAULT_START_TIME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow
Jeff Sharkey4de99362012-09-26 17:58:19 -0700248 : REBOOT_DEFAULT_WINDOW) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700250 : MEMCHECK_DEFAULT_RECHECK_INTERVAL) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
252 retrieveBrutalityAmount();
253
254 long realStartTime;
255 long now;
256
257 synchronized (this) {
258 now = System.currentTimeMillis();
259 realStartTime = computeCalendarTime(mCalendar, now,
260 rebootStartTime);
261
262 long rebootIntervalMillis = rebootInterval*24*60*60*1000;
263 if (DB || mReqRebootNoWait ||
264 (now-mBootTime) >= (rebootIntervalMillis-rebootWindowMillis)) {
265 if (fromAlarm && rebootWindowMillis <= 0) {
266 // No reboot window -- just immediately reboot.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800267 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 (int)rebootIntervalMillis, (int)rebootStartTime*1000,
269 (int)rebootWindowMillis, "");
270 rebootSystem("Checkin scheduled forced");
271 return;
272 }
273
274 // Are we within the reboot window?
275 if (now < realStartTime) {
276 // Schedule alarm for next check interval.
277 realStartTime = computeCalendarTime(mCalendar,
278 now, rebootStartTime);
279 } else if (now < (realStartTime+rebootWindowMillis)) {
280 String doit = shouldWeBeBrutalLocked(now);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800281 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 (int)rebootInterval, (int)rebootStartTime*1000,
283 (int)rebootWindowMillis, doit != null ? doit : "");
284 if (doit == null) {
285 rebootSystem("Checked scheduled range");
286 return;
287 }
288
289 // Schedule next alarm either within the window or in the
290 // next interval.
291 if ((now+recheckInterval) >= (realStartTime+rebootWindowMillis)) {
292 realStartTime = computeCalendarTime(mCalendar,
293 now + rebootIntervalMillis, rebootStartTime);
294 } else {
295 realStartTime = now + recheckInterval;
296 }
297 } else {
298 // Schedule alarm for next check interval.
299 realStartTime = computeCalendarTime(mCalendar,
300 now + rebootIntervalMillis, rebootStartTime);
301 }
302 }
303 }
304
Joe Onorato8a9b2202010-02-26 18:56:32 -0800305 if (localLOGV) Slog.v(TAG, "Scheduling next reboot alarm for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 + ((realStartTime-now)/1000/60) + "m from now");
307 mAlarm.remove(mRebootIntent);
308 mAlarm.set(AlarmManager.RTC_WAKEUP, realStartTime, mRebootIntent);
309 }
310
311 /**
312 * Perform a full reboot of the system.
313 */
314 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800315 Slog.i(TAG, "Rebooting system because: " + reason);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800316 PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700317 pms.reboot(false, reason, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
319
320 /**
321 * Load the current Gservices settings for when
322 * {@link #shouldWeBeBrutalLocked} will allow the brutality to happen.
323 * Must not be called with the lock held.
324 */
325 void retrieveBrutalityAmount() {
326 mMinScreenOff = (mReqMinScreenOff >= 0 ? mReqMinScreenOff
Jeff Sharkey4de99362012-09-26 17:58:19 -0700327 : MEMCHECK_DEFAULT_MIN_SCREEN_OFF) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm
Jeff Sharkey4de99362012-09-26 17:58:19 -0700329 : MEMCHECK_DEFAULT_MIN_ALARM) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
331
332 /**
333 * Determine whether it is a good time to kill, crash, or otherwise
334 * plunder the current situation for the overall long-term benefit of
335 * the world.
336 *
337 * @param curTime The current system time.
338 * @return Returns null if this is a good time, else a String with the
339 * text of why it is not a good time.
340 */
341 String shouldWeBeBrutalLocked(long curTime) {
Jeff Browna4d82042012-10-02 19:11:19 -0700342 if (mBattery == null || !mBattery.isPowered(BatteryManager.BATTERY_PLUGGED_ANY)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 return "battery";
344 }
345
346 if (mMinScreenOff >= 0 && (mPower == null ||
Jeff Brown96307042012-07-27 15:51:34 -0700347 mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 return "screen";
349 }
350
351 if (mMinAlarm >= 0 && (mAlarm == null ||
352 mAlarm.timeToNextAlarm() < mMinAlarm)) {
353 return "alarm";
354 }
355
356 return null;
357 }
358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 static long computeCalendarTime(Calendar c, long curTime,
360 long secondsSinceMidnight) {
361
362 // start with now
363 c.setTimeInMillis(curTime);
364
365 int val = (int)secondsSinceMidnight / (60*60);
366 c.set(Calendar.HOUR_OF_DAY, val);
367 secondsSinceMidnight -= val * (60*60);
368 val = (int)secondsSinceMidnight / 60;
369 c.set(Calendar.MINUTE, val);
370 c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60));
371 c.set(Calendar.MILLISECOND, 0);
372
373 long newTime = c.getTimeInMillis();
374 if (newTime < curTime) {
375 // The given time (in seconds since midnight) has already passed for today, so advance
376 // by one day (due to daylight savings, etc., the delta may differ from 24 hours).
377 c.add(Calendar.DAY_OF_MONTH, 1);
378 newTime = c.getTimeInMillis();
379 }
380
381 return newTime;
382 }
383
384 @Override
385 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700386 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 while (true) {
388 mCompleted = false;
389 mHandler.sendEmptyMessage(MONITOR);
390
391 synchronized (this) {
392 long timeout = TIME_TO_WAIT;
393
394 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
395 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700396 // 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 -0800397 // positive on when to kill things.
398 long start = SystemClock.uptimeMillis();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800399 while (timeout > 0 && !mForceKillSystem) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 try {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800401 wait(timeout); // notifyAll() is called when mForceKillSystem is set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800403 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 }
405 timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407
408 if (mCompleted && !mForceKillSystem) {
409 // The monitors have returned.
Christopher Tate6ee412d2010-05-28 12:01:56 -0700410 waitedHalf = false;
411 continue;
412 }
413
414 if (!waitedHalf) {
415 // We've waited half the deadlock-detection interval. Pull a stack
416 // trace and wait another half.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700417 ArrayList<Integer> pids = new ArrayList<Integer>();
Christopher Tate6ee412d2010-05-28 12:01:56 -0700418 pids.add(Process.myPid());
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700419 ActivityManagerService.dumpStackTraces(true, pids, null, null,
420 NATIVE_STACKS_OF_INTEREST);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700421 waitedHalf = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 continue;
423 }
424 }
425
426 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700427 // First collect stack traces from all threads of the system process.
428 // Then kill this process so that the system will restart.
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800429
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800430 final String name = (mCurrentMonitor != null) ?
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700431 mCurrentMonitor.getClass().getName() : "null";
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800432 EventLog.writeEvent(EventLogTags.WATCHDOG, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700434 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800435 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800436 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700437 // Pass !waitedHalf so that just in case we somehow wind up here without having
438 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800439 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700440 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800441
442 // Give some extra time to make sure the stack traces get written.
443 // The system's been hanging for a minute, another second or two won't hurt much.
444 SystemClock.sleep(2000);
445
Christopher Tateecaa7b42010-06-04 14:55:02 -0700446 // Pull our own kernel thread stacks as well if we're configured for that
447 if (RECORD_KERNEL_THREADS) {
448 dumpKernelStackTraces();
449 }
450
Colin Cross5df1d872012-11-29 11:42:11 -0800451 // Trigger the kernel to dump all blocked threads to the kernel log
452 try {
453 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
454 sysrq_trigger.write("w");
455 sysrq_trigger.close();
456 } catch (IOException e) {
457 Slog.e(TAG, "Failed to write to /proc/sysrq-trigger");
458 Slog.e(TAG, e.getMessage());
459 }
460
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800461 // Try to add the error to the dropbox, but assuming that the ActivityManager
462 // itself may be deadlocked. (which has happened, causing this statement to
463 // deadlock and the watchdog as a whole to be ineffective)
464 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
465 public void run() {
466 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700467 "watchdog", null, "system_server", null, null,
468 name, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800469 }
470 };
471 dropboxThread.start();
472 try {
473 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
474 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700476 // Only kill the process if the debugger is not attached.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 if (!Debug.isDebuggerConnected()) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800478 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name);
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700479 Process.killProcess(Process.myPid());
480 System.exit(10);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800481 } else {
482 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700484
485 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 }
487 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700488
489 private File dumpKernelStackTraces() {
490 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
491 if (tracesPath == null || tracesPath.length() == 0) {
492 return null;
493 }
494
495 native_dumpKernelStacks(tracesPath);
496 return new File(tracesPath);
497 }
498
499 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500}