blob: 2c5038eb7ce52ac7cc690141094633e4f1b35c67 [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;
rikard dahlman9211b132012-08-28 16:12:38 +020029import android.os.Build;
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;
37import android.provider.Settings;
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;
rikard dahlman9211b132012-08-28 16:12:38 +020043import 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 {
118 @Override
119 public void handleMessage(Message msg) {
120 switch (msg.what) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 case MONITOR: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 // See if we should force a reboot.
123 int rebootInterval = mReqRebootInterval >= 0
Doug Zongkerf6888892010-01-06 16:38:14 -0800124 ? mReqRebootInterval : Settings.Secure.getInt(
125 mResolver, Settings.Secure.REBOOT_INTERVAL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 REBOOT_DEFAULT_INTERVAL);
127 if (mRebootInterval != rebootInterval) {
128 mRebootInterval = rebootInterval;
129 // We have been running long enough that a reboot can
130 // be considered...
131 checkReboot(false);
132 }
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 final int size = mMonitors.size();
135 for (int i = 0 ; i < size ; i++) {
136 mCurrentMonitor = mMonitors.get(i);
137 mCurrentMonitor.monitor();
138 }
139
140 synchronized (Watchdog.this) {
141 mCompleted = true;
142 mCurrentMonitor = null;
143 }
144 } break;
145 }
146 }
147 }
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final class RebootReceiver extends BroadcastReceiver {
150 @Override
151 public void onReceive(Context c, Intent intent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800152 if (localLOGV) Slog.v(TAG, "Alarm went off, checking reboot.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 checkReboot(true);
154 }
155 }
156
157 final class RebootRequestReceiver extends BroadcastReceiver {
158 @Override
159 public void onReceive(Context c, Intent intent) {
160 mReqRebootNoWait = intent.getIntExtra("nowait", 0) != 0;
161 mReqRebootInterval = intent.getIntExtra("interval", -1);
162 mReqRebootStartTime = intent.getIntExtra("startTime", -1);
163 mReqRebootWindow = intent.getIntExtra("window", -1);
164 mReqMinScreenOff = intent.getIntExtra("minScreenOff", -1);
165 mReqMinNextAlarm = intent.getIntExtra("minNextAlarm", -1);
166 mReqRecheckInterval = intent.getIntExtra("recheckInterval", -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800167 EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 mReqRebootNoWait ? 1 : 0, mReqRebootInterval,
169 mReqRecheckInterval, mReqRebootStartTime,
170 mReqRebootWindow, mReqMinScreenOff, mReqMinNextAlarm);
171 checkReboot(true);
172 }
173 }
174
175 public interface Monitor {
176 void monitor();
177 }
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 public static Watchdog getInstance() {
180 if (sWatchdog == null) {
181 sWatchdog = new Watchdog();
182 }
183
184 return sWatchdog;
185 }
186
187 private Watchdog() {
188 super("watchdog");
189 mHandler = new HeartbeatHandler();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 }
191
192 public void init(Context context, BatteryService battery,
193 PowerManagerService power, AlarmManagerService alarm,
194 ActivityManagerService activity) {
195 mResolver = context.getContentResolver();
196 mBattery = battery;
197 mPower = power;
198 mAlarm = alarm;
199 mActivity = activity;
200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 context.registerReceiver(new RebootReceiver(),
202 new IntentFilter(REBOOT_ACTION));
203 mRebootIntent = PendingIntent.getBroadcast(context,
204 0, new Intent(REBOOT_ACTION), 0);
205
206 context.registerReceiver(new RebootRequestReceiver(),
207 new IntentFilter(Intent.ACTION_REBOOT),
208 android.Manifest.permission.REBOOT, null);
209
210 mBootTime = System.currentTimeMillis();
211 }
212
Christopher Tatec27181c2010-06-30 14:41:09 -0700213 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 synchronized (this) {
215 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 }
218 }
219 }
220
221 public void addMonitor(Monitor monitor) {
222 synchronized (this) {
223 if (isAlive()) {
224 throw new RuntimeException("Monitors can't be added while the Watchdog is running");
225 }
226 mMonitors.add(monitor);
227 }
228 }
229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 void checkReboot(boolean fromAlarm) {
231 int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval
Doug Zongkerf6888892010-01-06 16:38:14 -0800232 : Settings.Secure.getInt(
233 mResolver, Settings.Secure.REBOOT_INTERVAL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 REBOOT_DEFAULT_INTERVAL);
235 mRebootInterval = rebootInterval;
236 if (rebootInterval <= 0) {
237 // No reboot interval requested.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800238 if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 mAlarm.remove(mRebootIntent);
240 return;
241 }
242
243 long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime
Doug Zongkerf6888892010-01-06 16:38:14 -0800244 : Settings.Secure.getLong(
245 mResolver, Settings.Secure.REBOOT_START_TIME,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 REBOOT_DEFAULT_START_TIME);
247 long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow
Doug Zongkerf6888892010-01-06 16:38:14 -0800248 : Settings.Secure.getLong(
249 mResolver, Settings.Secure.REBOOT_WINDOW,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 REBOOT_DEFAULT_WINDOW)) * 1000;
251 long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval
Doug Zongkerf6888892010-01-06 16:38:14 -0800252 : Settings.Secure.getLong(
253 mResolver, Settings.Secure.MEMCHECK_RECHECK_INTERVAL,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 MEMCHECK_DEFAULT_RECHECK_INTERVAL)) * 1000;
255
256 retrieveBrutalityAmount();
257
258 long realStartTime;
259 long now;
260
261 synchronized (this) {
262 now = System.currentTimeMillis();
263 realStartTime = computeCalendarTime(mCalendar, now,
264 rebootStartTime);
265
266 long rebootIntervalMillis = rebootInterval*24*60*60*1000;
267 if (DB || mReqRebootNoWait ||
268 (now-mBootTime) >= (rebootIntervalMillis-rebootWindowMillis)) {
269 if (fromAlarm && rebootWindowMillis <= 0) {
270 // No reboot window -- just immediately reboot.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800271 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 (int)rebootIntervalMillis, (int)rebootStartTime*1000,
273 (int)rebootWindowMillis, "");
274 rebootSystem("Checkin scheduled forced");
275 return;
276 }
277
278 // Are we within the reboot window?
279 if (now < realStartTime) {
280 // Schedule alarm for next check interval.
281 realStartTime = computeCalendarTime(mCalendar,
282 now, rebootStartTime);
283 } else if (now < (realStartTime+rebootWindowMillis)) {
284 String doit = shouldWeBeBrutalLocked(now);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800285 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 (int)rebootInterval, (int)rebootStartTime*1000,
287 (int)rebootWindowMillis, doit != null ? doit : "");
288 if (doit == null) {
289 rebootSystem("Checked scheduled range");
290 return;
291 }
292
293 // Schedule next alarm either within the window or in the
294 // next interval.
295 if ((now+recheckInterval) >= (realStartTime+rebootWindowMillis)) {
296 realStartTime = computeCalendarTime(mCalendar,
297 now + rebootIntervalMillis, rebootStartTime);
298 } else {
299 realStartTime = now + recheckInterval;
300 }
301 } else {
302 // Schedule alarm for next check interval.
303 realStartTime = computeCalendarTime(mCalendar,
304 now + rebootIntervalMillis, rebootStartTime);
305 }
306 }
307 }
308
Joe Onorato8a9b2202010-02-26 18:56:32 -0800309 if (localLOGV) Slog.v(TAG, "Scheduling next reboot alarm for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 + ((realStartTime-now)/1000/60) + "m from now");
311 mAlarm.remove(mRebootIntent);
312 mAlarm.set(AlarmManager.RTC_WAKEUP, realStartTime, mRebootIntent);
313 }
314
315 /**
316 * Perform a full reboot of the system.
317 */
318 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800319 Slog.i(TAG, "Rebooting system because: " + reason);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800320 PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
321 pms.reboot(reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 }
323
324 /**
325 * Load the current Gservices settings for when
326 * {@link #shouldWeBeBrutalLocked} will allow the brutality to happen.
327 * Must not be called with the lock held.
328 */
329 void retrieveBrutalityAmount() {
330 mMinScreenOff = (mReqMinScreenOff >= 0 ? mReqMinScreenOff
Doug Zongkerf6888892010-01-06 16:38:14 -0800331 : Settings.Secure.getInt(
332 mResolver, Settings.Secure.MEMCHECK_MIN_SCREEN_OFF,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 MEMCHECK_DEFAULT_MIN_SCREEN_OFF)) * 1000;
334 mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm
Doug Zongkerf6888892010-01-06 16:38:14 -0800335 : Settings.Secure.getInt(
336 mResolver, Settings.Secure.MEMCHECK_MIN_ALARM,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 MEMCHECK_DEFAULT_MIN_ALARM)) * 1000;
338 }
339
340 /**
341 * Determine whether it is a good time to kill, crash, or otherwise
342 * plunder the current situation for the overall long-term benefit of
343 * the world.
344 *
345 * @param curTime The current system time.
346 * @return Returns null if this is a good time, else a String with the
347 * text of why it is not a good time.
348 */
349 String shouldWeBeBrutalLocked(long curTime) {
350 if (mBattery == null || !mBattery.isPowered()) {
351 return "battery";
352 }
353
354 if (mMinScreenOff >= 0 && (mPower == null ||
Jeff Brown96307042012-07-27 15:51:34 -0700355 mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 return "screen";
357 }
358
359 if (mMinAlarm >= 0 && (mAlarm == null ||
360 mAlarm.timeToNextAlarm() < mMinAlarm)) {
361 return "alarm";
362 }
363
364 return null;
365 }
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 static long computeCalendarTime(Calendar c, long curTime,
368 long secondsSinceMidnight) {
369
370 // start with now
371 c.setTimeInMillis(curTime);
372
373 int val = (int)secondsSinceMidnight / (60*60);
374 c.set(Calendar.HOUR_OF_DAY, val);
375 secondsSinceMidnight -= val * (60*60);
376 val = (int)secondsSinceMidnight / 60;
377 c.set(Calendar.MINUTE, val);
378 c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60));
379 c.set(Calendar.MILLISECOND, 0);
380
381 long newTime = c.getTimeInMillis();
382 if (newTime < curTime) {
383 // The given time (in seconds since midnight) has already passed for today, so advance
384 // by one day (due to daylight savings, etc., the delta may differ from 24 hours).
385 c.add(Calendar.DAY_OF_MONTH, 1);
386 newTime = c.getTimeInMillis();
387 }
388
389 return newTime;
390 }
391
392 @Override
393 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700394 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 while (true) {
396 mCompleted = false;
397 mHandler.sendEmptyMessage(MONITOR);
398
399 synchronized (this) {
400 long timeout = TIME_TO_WAIT;
401
402 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
403 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700404 // 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 -0800405 // positive on when to kill things.
406 long start = SystemClock.uptimeMillis();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800407 while (timeout > 0 && !mForceKillSystem) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 try {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800409 wait(timeout); // notifyAll() is called when mForceKillSystem is set
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800411 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
413 timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415
416 if (mCompleted && !mForceKillSystem) {
417 // The monitors have returned.
Christopher Tate6ee412d2010-05-28 12:01:56 -0700418 waitedHalf = false;
419 continue;
420 }
421
422 if (!waitedHalf) {
423 // We've waited half the deadlock-detection interval. Pull a stack
424 // trace and wait another half.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700425 ArrayList<Integer> pids = new ArrayList<Integer>();
Christopher Tate6ee412d2010-05-28 12:01:56 -0700426 pids.add(Process.myPid());
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700427 ActivityManagerService.dumpStackTraces(true, pids, null, null,
428 NATIVE_STACKS_OF_INTEREST);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700429 waitedHalf = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 continue;
431 }
432 }
433
434 // If we got here, that means that the system is most likely hung.
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800435
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800436 final String name = (mCurrentMonitor != null) ?
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700437 mCurrentMonitor.getClass().getName() : "null";
rikard dahlman9211b132012-08-28 16:12:38 +0200438 Slog.w(TAG, "WATCHDOG PROBLEM IN SYSTEM SERVER: " + name);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800439 EventLog.writeEvent(EventLogTags.WATCHDOG, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700441 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800442 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800443 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700444 // Pass !waitedHalf so that just in case we somehow wind up here without having
445 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800446 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700447 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800448
449 // Give some extra time to make sure the stack traces get written.
450 // The system's been hanging for a minute, another second or two won't hurt much.
451 SystemClock.sleep(2000);
452
Christopher Tateecaa7b42010-06-04 14:55:02 -0700453 // Pull our own kernel thread stacks as well if we're configured for that
454 if (RECORD_KERNEL_THREADS) {
455 dumpKernelStackTraces();
456 }
457
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800458 // Try to add the error to the dropbox, but assuming that the ActivityManager
459 // itself may be deadlocked. (which has happened, causing this statement to
460 // deadlock and the watchdog as a whole to be ineffective)
461 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
462 public void run() {
463 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700464 "watchdog", null, "system_server", null, null,
465 name, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800466 }
467 };
468 dropboxThread.start();
469 try {
470 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
471 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472
rikard dahlman9211b132012-08-28 16:12:38 +0200473 // Only kill/crash the process if the debugger is not attached.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 if (!Debug.isDebuggerConnected()) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800475 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name);
rikard dahlman9211b132012-08-28 16:12:38 +0200476 if (!Build.TYPE.equals("user")) {
477 forceCrashDump();
478 } else {
479 Process.killProcess(Process.myPid());
480 System.exit(10);
481 }
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800482 } else {
483 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700485
486 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 }
488 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700489
rikard dahlman9211b132012-08-28 16:12:38 +0200490 private void forceCrashDump() {
491 /* Sync file system to flash the data which is written just before the
492 * crash.
493 */
494 java.lang.Process p = null;
495 try {
496 p = Runtime.getRuntime().exec("sync");
497 if (p != null) {
498 // It is not necessary to check the exit code, here.
499 // 'sync' command always succeeds, and this function returns 0.
500 p.waitFor();
501 } else {
502 Slog.e(TAG, "Failed to execute 'sync' command. (no process handle)");
503 }
504 } catch (Exception e) {
505 // This code is an emergency path to crash MUT. The system already
506 // caused fatal error, and then calls this function to create a
507 // crash dump. This function must run the code below to force a
508 // crash, even if the sync command failed.
509 // Therefore, ignore all exceptions, here.
510 Slog.e(TAG, "Failed to execute 'sync' command prior to forcing crash: " + e);
511 } finally {
512 if (p != null) {
513 p.destroy();
514 }
515 }
516
517 FileWriter out = null;
518 try {
519 out = new FileWriter("/proc/sysrq-trigger");
520 out.write("c");
521 } catch (IOException e) {
522 Slog.e(TAG, "Failed to write to sysrq-trigger while triggering crash: " + e);
523 } finally {
524 if (out != null) {
525 try {
526 out.close();
527 } catch (IOException e) {
528 Slog.e(TAG, "Failed to close sysrq-trigger while triggering crash: " + e);
529 }
530 }
531 }
532 }
533
Christopher Tateecaa7b42010-06-04 14:55:02 -0700534 private File dumpKernelStackTraces() {
535 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
536 if (tracesPath == null || tracesPath.length() == 0) {
537 return null;
538 }
539
540 native_dumpKernelStacks(tracesPath);
541 return new File(tracesPath);
542 }
543
544 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545}