blob: e784cf24099096f9a0a885005eaee1c735a71f07 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070019import android.app.IActivityController;
20import android.os.Binder;
21import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.server.am.ActivityManagerService;
Jeff Brown4f8ecd82012-06-18 18:29:13 -070023import com.android.server.power.PowerManagerService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25import android.app.AlarmManager;
26import android.app.PendingIntent;
27import android.content.BroadcastReceiver;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
Jeff Browna4d82042012-10-02 19:11:19 -070032import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Debug;
34import android.os.Handler;
John Michelau11641522013-03-18 18:28:23 -050035import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Message;
37import android.os.Process;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080038import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.SystemClock;
40import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.util.EventLog;
Dan Egnor9bdc94b2010-03-04 14:20:31 -080042import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080043import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Dan Egnor9bdc94b2010-03-04 14:20:31 -080045import java.io.File;
Colin Cross5df1d872012-11-29 11:42:11 -080046import java.io.FileWriter;
47import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import java.util.ArrayList;
49import java.util.Calendar;
50
51/** This class calls its monitor every minute. Killing this process if they don't return **/
52public class Watchdog extends Thread {
53 static final String TAG = "Watchdog";
Joe Onorato43a17652011-04-06 19:22:23 -070054 static final boolean localLOGV = false || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56 // Set this to true to use debug default values.
57 static final boolean DB = false;
58
Christopher Tateecaa7b42010-06-04 14:55:02 -070059 // Set this to true to have the watchdog record kernel thread stacks when it fires
60 static final boolean RECORD_KERNEL_THREADS = true;
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 static final int MONITOR = 2718;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Mathias Agopiancf2317e2011-08-25 17:12:37 -070064 static final int TIME_TO_RESTART = DB ? 15*1000 : 60*1000;
Christopher Tate6ee412d2010-05-28 12:01:56 -070065 static final int TIME_TO_WAIT = TIME_TO_RESTART / 2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 static final int MEMCHECK_DEFAULT_MIN_SCREEN_OFF = DB ? 1*60 : 5*60; // 5 minutes
68 static final int MEMCHECK_DEFAULT_MIN_ALARM = DB ? 1*60 : 3*60; // 3 minutes
69 static final int MEMCHECK_DEFAULT_RECHECK_INTERVAL = DB ? 1*60 : 5*60; // 5 minutes
70
71 static final int REBOOT_DEFAULT_INTERVAL = DB ? 1 : 0; // never force reboot
72 static final int REBOOT_DEFAULT_START_TIME = 3*60*60; // 3:00am
73 static final int REBOOT_DEFAULT_WINDOW = 60*60; // within 1 hour
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 static final String REBOOT_ACTION = "com.android.service.Watchdog.REBOOT";
76
Dianne Hackbornf72467a2012-06-08 17:23:59 -070077 static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
78 "/system/bin/mediaserver",
79 "/system/bin/sdcard",
80 "/system/bin/surfaceflinger"
81 };
82
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 static Watchdog sWatchdog;
84
85 /* This handler will be used to post message back onto the main thread */
86 final Handler mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
88 ContentResolver mResolver;
89 BatteryService mBattery;
90 PowerManagerService mPower;
91 AlarmManagerService mAlarm;
92 ActivityManagerService mActivity;
93 boolean mCompleted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 Monitor mCurrentMonitor;
95
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070097 IActivityController mController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99 final Calendar mCalendar = Calendar.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 int mMinScreenOff = MEMCHECK_DEFAULT_MIN_SCREEN_OFF;
101 int mMinAlarm = MEMCHECK_DEFAULT_MIN_ALARM;
102 boolean mNeedScheduledCheck;
103 PendingIntent mCheckupIntent;
104 PendingIntent mRebootIntent;
105
106 long mBootTime;
107 int mRebootInterval;
108
109 boolean mReqRebootNoWait; // should wait for one interval before reboot?
110 int mReqRebootInterval = -1; // >= 0 if a reboot has been requested
111 int mReqRebootStartTime = -1; // >= 0 if a specific start time has been requested
112 int mReqRebootWindow = -1; // >= 0 if a specific window has been requested
113 int mReqMinScreenOff = -1; // >= 0 if a specific screen off time has been requested
114 int mReqMinNextAlarm = -1; // >= 0 if specific time to next alarm has been requested
115 int mReqRecheckInterval= -1; // >= 0 if a specific recheck interval has been requested
116
117 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * Used for scheduling monitor callbacks and checking memory usage.
119 */
120 final class HeartbeatHandler extends Handler {
John Michelau11641522013-03-18 18:28:23 -0500121 HeartbeatHandler(Looper looper) {
122 super(looper);
123 }
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 @Override
126 public void handleMessage(Message msg) {
127 switch (msg.what) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 case MONITOR: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 // See if we should force a reboot.
130 int rebootInterval = mReqRebootInterval >= 0
Jeff Sharkey4de99362012-09-26 17:58:19 -0700131 ? mReqRebootInterval : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 if (mRebootInterval != rebootInterval) {
133 mRebootInterval = rebootInterval;
134 // We have been running long enough that a reboot can
135 // be considered...
136 checkReboot(false);
137 }
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 final int size = mMonitors.size();
140 for (int i = 0 ; i < size ; i++) {
Michael Wright8fa56f62013-04-01 16:36:05 -0700141 synchronized (Watchdog.this) {
142 mCurrentMonitor = mMonitors.get(i);
143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 mCurrentMonitor.monitor();
145 }
146
147 synchronized (Watchdog.this) {
148 mCompleted = true;
149 mCurrentMonitor = null;
150 }
151 } break;
152 }
153 }
154 }
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 final class RebootReceiver extends BroadcastReceiver {
157 @Override
158 public void onReceive(Context c, Intent intent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800159 if (localLOGV) Slog.v(TAG, "Alarm went off, checking reboot.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 checkReboot(true);
161 }
162 }
163
164 final class RebootRequestReceiver extends BroadcastReceiver {
165 @Override
166 public void onReceive(Context c, Intent intent) {
167 mReqRebootNoWait = intent.getIntExtra("nowait", 0) != 0;
168 mReqRebootInterval = intent.getIntExtra("interval", -1);
169 mReqRebootStartTime = intent.getIntExtra("startTime", -1);
170 mReqRebootWindow = intent.getIntExtra("window", -1);
171 mReqMinScreenOff = intent.getIntExtra("minScreenOff", -1);
172 mReqMinNextAlarm = intent.getIntExtra("minNextAlarm", -1);
173 mReqRecheckInterval = intent.getIntExtra("recheckInterval", -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800174 EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 mReqRebootNoWait ? 1 : 0, mReqRebootInterval,
176 mReqRecheckInterval, mReqRebootStartTime,
177 mReqRebootWindow, mReqMinScreenOff, mReqMinNextAlarm);
178 checkReboot(true);
179 }
180 }
181
182 public interface Monitor {
183 void monitor();
184 }
185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 public static Watchdog getInstance() {
187 if (sWatchdog == null) {
188 sWatchdog = new Watchdog();
189 }
190
191 return sWatchdog;
192 }
193
194 private Watchdog() {
195 super("watchdog");
John Michelau11641522013-03-18 18:28:23 -0500196 // Explicitly bind the HeartbeatHandler to run on the ServerThread, so
197 // that it can't get accidentally bound to another thread.
198 mHandler = new HeartbeatHandler(Looper.getMainLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
200
201 public void init(Context context, BatteryService battery,
202 PowerManagerService power, AlarmManagerService alarm,
203 ActivityManagerService activity) {
204 mResolver = context.getContentResolver();
205 mBattery = battery;
206 mPower = power;
207 mAlarm = alarm;
208 mActivity = activity;
209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 context.registerReceiver(new RebootReceiver(),
211 new IntentFilter(REBOOT_ACTION));
212 mRebootIntent = PendingIntent.getBroadcast(context,
213 0, new Intent(REBOOT_ACTION), 0);
214
215 context.registerReceiver(new RebootRequestReceiver(),
216 new IntentFilter(Intent.ACTION_REBOOT),
217 android.Manifest.permission.REBOOT, null);
218
219 mBootTime = System.currentTimeMillis();
220 }
221
Christopher Tatec27181c2010-06-30 14:41:09 -0700222 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 synchronized (this) {
224 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 }
227 }
228 }
229
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700230 public void setActivityController(IActivityController controller) {
231 synchronized (this) {
232 mController = controller;
233 }
234 }
235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 public void addMonitor(Monitor monitor) {
237 synchronized (this) {
238 if (isAlive()) {
239 throw new RuntimeException("Monitors can't be added while the Watchdog is running");
240 }
241 mMonitors.add(monitor);
242 }
243 }
244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 void checkReboot(boolean fromAlarm) {
246 int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700247 : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 mRebootInterval = rebootInterval;
249 if (rebootInterval <= 0) {
250 // No reboot interval requested.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800251 if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 mAlarm.remove(mRebootIntent);
253 return;
254 }
255
256 long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime
Jeff Sharkey4de99362012-09-26 17:58:19 -0700257 : REBOOT_DEFAULT_START_TIME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow
Jeff Sharkey4de99362012-09-26 17:58:19 -0700259 : REBOOT_DEFAULT_WINDOW) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700261 : MEMCHECK_DEFAULT_RECHECK_INTERVAL) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
263 retrieveBrutalityAmount();
264
265 long realStartTime;
266 long now;
267
268 synchronized (this) {
269 now = System.currentTimeMillis();
270 realStartTime = computeCalendarTime(mCalendar, now,
271 rebootStartTime);
272
273 long rebootIntervalMillis = rebootInterval*24*60*60*1000;
274 if (DB || mReqRebootNoWait ||
275 (now-mBootTime) >= (rebootIntervalMillis-rebootWindowMillis)) {
276 if (fromAlarm && rebootWindowMillis <= 0) {
277 // No reboot window -- just immediately reboot.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800278 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 (int)rebootIntervalMillis, (int)rebootStartTime*1000,
280 (int)rebootWindowMillis, "");
281 rebootSystem("Checkin scheduled forced");
282 return;
283 }
284
285 // Are we within the reboot window?
286 if (now < realStartTime) {
287 // Schedule alarm for next check interval.
288 realStartTime = computeCalendarTime(mCalendar,
289 now, rebootStartTime);
290 } else if (now < (realStartTime+rebootWindowMillis)) {
291 String doit = shouldWeBeBrutalLocked(now);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800292 EventLog.writeEvent(EventLogTags.WATCHDOG_SCHEDULED_REBOOT, now,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 (int)rebootInterval, (int)rebootStartTime*1000,
294 (int)rebootWindowMillis, doit != null ? doit : "");
295 if (doit == null) {
296 rebootSystem("Checked scheduled range");
297 return;
298 }
299
300 // Schedule next alarm either within the window or in the
301 // next interval.
302 if ((now+recheckInterval) >= (realStartTime+rebootWindowMillis)) {
303 realStartTime = computeCalendarTime(mCalendar,
304 now + rebootIntervalMillis, rebootStartTime);
305 } else {
306 realStartTime = now + recheckInterval;
307 }
308 } else {
309 // Schedule alarm for next check interval.
310 realStartTime = computeCalendarTime(mCalendar,
311 now + rebootIntervalMillis, rebootStartTime);
312 }
313 }
314 }
315
Joe Onorato8a9b2202010-02-26 18:56:32 -0800316 if (localLOGV) Slog.v(TAG, "Scheduling next reboot alarm for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 + ((realStartTime-now)/1000/60) + "m from now");
318 mAlarm.remove(mRebootIntent);
319 mAlarm.set(AlarmManager.RTC_WAKEUP, realStartTime, mRebootIntent);
320 }
321
322 /**
323 * Perform a full reboot of the system.
324 */
325 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800326 Slog.i(TAG, "Rebooting system because: " + reason);
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -0800327 PowerManagerService pms = (PowerManagerService) ServiceManager.getService("power");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700328 pms.reboot(false, reason, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330
331 /**
332 * Load the current Gservices settings for when
333 * {@link #shouldWeBeBrutalLocked} will allow the brutality to happen.
334 * Must not be called with the lock held.
335 */
336 void retrieveBrutalityAmount() {
337 mMinScreenOff = (mReqMinScreenOff >= 0 ? mReqMinScreenOff
Jeff Sharkey4de99362012-09-26 17:58:19 -0700338 : MEMCHECK_DEFAULT_MIN_SCREEN_OFF) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm
Jeff Sharkey4de99362012-09-26 17:58:19 -0700340 : MEMCHECK_DEFAULT_MIN_ALARM) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 }
342
343 /**
344 * Determine whether it is a good time to kill, crash, or otherwise
345 * plunder the current situation for the overall long-term benefit of
346 * the world.
347 *
348 * @param curTime The current system time.
349 * @return Returns null if this is a good time, else a String with the
350 * text of why it is not a good time.
351 */
352 String shouldWeBeBrutalLocked(long curTime) {
Jeff Browna4d82042012-10-02 19:11:19 -0700353 if (mBattery == null || !mBattery.isPowered(BatteryManager.BATTERY_PLUGGED_ANY)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 return "battery";
355 }
356
357 if (mMinScreenOff >= 0 && (mPower == null ||
Jeff Brown96307042012-07-27 15:51:34 -0700358 mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 return "screen";
360 }
361
362 if (mMinAlarm >= 0 && (mAlarm == null ||
363 mAlarm.timeToNextAlarm() < mMinAlarm)) {
364 return "alarm";
365 }
366
367 return null;
368 }
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 static long computeCalendarTime(Calendar c, long curTime,
371 long secondsSinceMidnight) {
372
373 // start with now
374 c.setTimeInMillis(curTime);
375
376 int val = (int)secondsSinceMidnight / (60*60);
377 c.set(Calendar.HOUR_OF_DAY, val);
378 secondsSinceMidnight -= val * (60*60);
379 val = (int)secondsSinceMidnight / 60;
380 c.set(Calendar.MINUTE, val);
381 c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60));
382 c.set(Calendar.MILLISECOND, 0);
383
384 long newTime = c.getTimeInMillis();
385 if (newTime < curTime) {
386 // The given time (in seconds since midnight) has already passed for today, so advance
387 // by one day (due to daylight savings, etc., the delta may differ from 24 hours).
388 c.add(Calendar.DAY_OF_MONTH, 1);
389 newTime = c.getTimeInMillis();
390 }
391
392 return newTime;
393 }
394
395 @Override
396 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700397 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 while (true) {
399 mCompleted = false;
400 mHandler.sendEmptyMessage(MONITOR);
401
Michael Wright8fa56f62013-04-01 16:36:05 -0700402
403 final String name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 synchronized (this) {
405 long timeout = TIME_TO_WAIT;
406
407 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
408 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700409 // 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 -0800410 // positive on when to kill things.
411 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700412 while (timeout > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700414 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800416 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800419 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
Michael Wright8fa56f62013-04-01 16:36:05 -0700421 if (mCompleted) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 // The monitors have returned.
Christopher Tate6ee412d2010-05-28 12:01:56 -0700423 waitedHalf = false;
424 continue;
425 }
426
427 if (!waitedHalf) {
428 // We've waited half the deadlock-detection interval. Pull a stack
429 // trace and wait another half.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700430 ArrayList<Integer> pids = new ArrayList<Integer>();
Christopher Tate6ee412d2010-05-28 12:01:56 -0700431 pids.add(Process.myPid());
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700432 ActivityManagerService.dumpStackTraces(true, pids, null, null,
433 NATIVE_STACKS_OF_INTEREST);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700434 waitedHalf = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 continue;
436 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700437
438 name = (mCurrentMonitor != null) ?
439 mCurrentMonitor.getClass().getName() : "null";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 }
441
442 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700443 // First collect stack traces from all threads of the system process.
444 // Then kill this process so that the system will restart.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800445 EventLog.writeEvent(EventLogTags.WATCHDOG, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700447 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800448 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800449 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700450 // Pass !waitedHalf so that just in case we somehow wind up here without having
451 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800452 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700453 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800454
455 // Give some extra time to make sure the stack traces get written.
456 // The system's been hanging for a minute, another second or two won't hurt much.
457 SystemClock.sleep(2000);
458
Christopher Tateecaa7b42010-06-04 14:55:02 -0700459 // Pull our own kernel thread stacks as well if we're configured for that
460 if (RECORD_KERNEL_THREADS) {
461 dumpKernelStackTraces();
462 }
463
Colin Cross5df1d872012-11-29 11:42:11 -0800464 // Trigger the kernel to dump all blocked threads to the kernel log
465 try {
466 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
467 sysrq_trigger.write("w");
468 sysrq_trigger.close();
469 } catch (IOException e) {
470 Slog.e(TAG, "Failed to write to /proc/sysrq-trigger");
471 Slog.e(TAG, e.getMessage());
472 }
473
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800474 // Try to add the error to the dropbox, but assuming that the ActivityManager
475 // itself may be deadlocked. (which has happened, causing this statement to
476 // deadlock and the watchdog as a whole to be ineffective)
477 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
478 public void run() {
479 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700480 "watchdog", null, "system_server", null, null,
481 name, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800482 }
483 };
484 dropboxThread.start();
485 try {
486 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
487 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700489 IActivityController controller;
490 synchronized (this) {
491 controller = mController;
492 }
493 if (controller != null) {
494 Slog.i(TAG, "Reporting stuck state to activity controller");
495 try {
496 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
497 // 1 = keep waiting, -1 = kill system
498 int res = controller.systemNotResponding(name);
499 if (res >= 0) {
500 Slog.i(TAG, "Activity controller requested to coninue to wait");
501 waitedHalf = false;
502 continue;
503 }
504 } catch (RemoteException e) {
505 }
506 }
507
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700508 // Only kill the process if the debugger is not attached.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 if (!Debug.isDebuggerConnected()) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800510 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name);
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700511 Process.killProcess(Process.myPid());
512 System.exit(10);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800513 } else {
514 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700516
517 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 }
519 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700520
521 private File dumpKernelStackTraces() {
522 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
523 if (tracesPath == null || tracesPath.length() == 0) {
524 return null;
525 }
526
527 native_dumpKernelStacks(tracesPath);
528 return new File(tracesPath);
529 }
530
531 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532}