blob: ace543b44cc733c3e81477760782f8ae04d8e718 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 Monitor mCurrentMonitor;
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 int mPhonePid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
95 final Calendar mCalendar = Calendar.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 int mMinScreenOff = MEMCHECK_DEFAULT_MIN_SCREEN_OFF;
97 int mMinAlarm = MEMCHECK_DEFAULT_MIN_ALARM;
98 boolean mNeedScheduledCheck;
99 PendingIntent mCheckupIntent;
100 PendingIntent mRebootIntent;
101
102 long mBootTime;
103 int mRebootInterval;
104
105 boolean mReqRebootNoWait; // should wait for one interval before reboot?
106 int mReqRebootInterval = -1; // >= 0 if a reboot has been requested
107 int mReqRebootStartTime = -1; // >= 0 if a specific start time has been requested
108 int mReqRebootWindow = -1; // >= 0 if a specific window has been requested
109 int mReqMinScreenOff = -1; // >= 0 if a specific screen off time has been requested
110 int mReqMinNextAlarm = -1; // >= 0 if specific time to next alarm has been requested
111 int mReqRecheckInterval= -1; // >= 0 if a specific recheck interval has been requested
112
113 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 * Used for scheduling monitor callbacks and checking memory usage.
115 */
116 final class HeartbeatHandler extends Handler {
John Michelau11641522013-03-18 18:28:23 -0500117 HeartbeatHandler(Looper looper) {
118 super(looper);
119 }
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 @Override
122 public void handleMessage(Message msg) {
123 switch (msg.what) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 case MONITOR: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 // See if we should force a reboot.
126 int rebootInterval = mReqRebootInterval >= 0
Jeff Sharkey4de99362012-09-26 17:58:19 -0700127 ? mReqRebootInterval : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 if (mRebootInterval != rebootInterval) {
129 mRebootInterval = rebootInterval;
130 // We have been running long enough that a reboot can
131 // be considered...
132 checkReboot(false);
133 }
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final int size = mMonitors.size();
136 for (int i = 0 ; i < size ; i++) {
Michael Wright8fa56f62013-04-01 16:36:05 -0700137 synchronized (Watchdog.this) {
138 mCurrentMonitor = mMonitors.get(i);
139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 mCurrentMonitor.monitor();
141 }
142
143 synchronized (Watchdog.this) {
144 mCompleted = true;
145 mCurrentMonitor = null;
146 }
147 } break;
148 }
149 }
150 }
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 final class RebootReceiver extends BroadcastReceiver {
153 @Override
154 public void onReceive(Context c, Intent intent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800155 if (localLOGV) Slog.v(TAG, "Alarm went off, checking reboot.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 checkReboot(true);
157 }
158 }
159
160 final class RebootRequestReceiver extends BroadcastReceiver {
161 @Override
162 public void onReceive(Context c, Intent intent) {
163 mReqRebootNoWait = intent.getIntExtra("nowait", 0) != 0;
164 mReqRebootInterval = intent.getIntExtra("interval", -1);
165 mReqRebootStartTime = intent.getIntExtra("startTime", -1);
166 mReqRebootWindow = intent.getIntExtra("window", -1);
167 mReqMinScreenOff = intent.getIntExtra("minScreenOff", -1);
168 mReqMinNextAlarm = intent.getIntExtra("minNextAlarm", -1);
169 mReqRecheckInterval = intent.getIntExtra("recheckInterval", -1);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800170 EventLog.writeEvent(EventLogTags.WATCHDOG_REQUESTED_REBOOT,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 mReqRebootNoWait ? 1 : 0, mReqRebootInterval,
172 mReqRecheckInterval, mReqRebootStartTime,
173 mReqRebootWindow, mReqMinScreenOff, mReqMinNextAlarm);
174 checkReboot(true);
175 }
176 }
177
178 public interface Monitor {
179 void monitor();
180 }
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 public static Watchdog getInstance() {
183 if (sWatchdog == null) {
184 sWatchdog = new Watchdog();
185 }
186
187 return sWatchdog;
188 }
189
190 private Watchdog() {
191 super("watchdog");
John Michelau11641522013-03-18 18:28:23 -0500192 // Explicitly bind the HeartbeatHandler to run on the ServerThread, so
193 // that it can't get accidentally bound to another thread.
194 mHandler = new HeartbeatHandler(Looper.getMainLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
196
197 public void init(Context context, BatteryService battery,
198 PowerManagerService power, AlarmManagerService alarm,
199 ActivityManagerService activity) {
200 mResolver = context.getContentResolver();
201 mBattery = battery;
202 mPower = power;
203 mAlarm = alarm;
204 mActivity = activity;
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 context.registerReceiver(new RebootReceiver(),
207 new IntentFilter(REBOOT_ACTION));
208 mRebootIntent = PendingIntent.getBroadcast(context,
209 0, new Intent(REBOOT_ACTION), 0);
210
211 context.registerReceiver(new RebootRequestReceiver(),
212 new IntentFilter(Intent.ACTION_REBOOT),
213 android.Manifest.permission.REBOOT, null);
214
215 mBootTime = System.currentTimeMillis();
216 }
217
Christopher Tatec27181c2010-06-30 14:41:09 -0700218 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 synchronized (this) {
220 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 }
223 }
224 }
225
226 public void addMonitor(Monitor monitor) {
227 synchronized (this) {
228 if (isAlive()) {
229 throw new RuntimeException("Monitors can't be added while the Watchdog is running");
230 }
231 mMonitors.add(monitor);
232 }
233 }
234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 void checkReboot(boolean fromAlarm) {
236 int rebootInterval = mReqRebootInterval >= 0 ? mReqRebootInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700237 : REBOOT_DEFAULT_INTERVAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 mRebootInterval = rebootInterval;
239 if (rebootInterval <= 0) {
240 // No reboot interval requested.
Joe Onorato8a9b2202010-02-26 18:56:32 -0800241 if (localLOGV) Slog.v(TAG, "No need to schedule a reboot alarm!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 mAlarm.remove(mRebootIntent);
243 return;
244 }
245
246 long rebootStartTime = mReqRebootStartTime >= 0 ? mReqRebootStartTime
Jeff Sharkey4de99362012-09-26 17:58:19 -0700247 : REBOOT_DEFAULT_START_TIME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 long rebootWindowMillis = (mReqRebootWindow >= 0 ? mReqRebootWindow
Jeff Sharkey4de99362012-09-26 17:58:19 -0700249 : REBOOT_DEFAULT_WINDOW) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 long recheckInterval = (mReqRecheckInterval >= 0 ? mReqRecheckInterval
Jeff Sharkey4de99362012-09-26 17:58:19 -0700251 : MEMCHECK_DEFAULT_RECHECK_INTERVAL) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
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");
Dianne Hackbornc428aae2012-10-03 16:38:22 -0700318 pms.reboot(false, reason, false);
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
Jeff Sharkey4de99362012-09-26 17:58:19 -0700328 : MEMCHECK_DEFAULT_MIN_SCREEN_OFF) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 mMinAlarm = (mReqMinNextAlarm >= 0 ? mReqMinNextAlarm
Jeff Sharkey4de99362012-09-26 17:58:19 -0700330 : MEMCHECK_DEFAULT_MIN_ALARM) * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
332
333 /**
334 * Determine whether it is a good time to kill, crash, or otherwise
335 * plunder the current situation for the overall long-term benefit of
336 * the world.
337 *
338 * @param curTime The current system time.
339 * @return Returns null if this is a good time, else a String with the
340 * text of why it is not a good time.
341 */
342 String shouldWeBeBrutalLocked(long curTime) {
Jeff Browna4d82042012-10-02 19:11:19 -0700343 if (mBattery == null || !mBattery.isPowered(BatteryManager.BATTERY_PLUGGED_ANY)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 return "battery";
345 }
346
347 if (mMinScreenOff >= 0 && (mPower == null ||
Jeff Brown96307042012-07-27 15:51:34 -0700348 mPower.timeSinceScreenWasLastOn() < mMinScreenOff)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 return "screen";
350 }
351
352 if (mMinAlarm >= 0 && (mAlarm == null ||
353 mAlarm.timeToNextAlarm() < mMinAlarm)) {
354 return "alarm";
355 }
356
357 return null;
358 }
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 static long computeCalendarTime(Calendar c, long curTime,
361 long secondsSinceMidnight) {
362
363 // start with now
364 c.setTimeInMillis(curTime);
365
366 int val = (int)secondsSinceMidnight / (60*60);
367 c.set(Calendar.HOUR_OF_DAY, val);
368 secondsSinceMidnight -= val * (60*60);
369 val = (int)secondsSinceMidnight / 60;
370 c.set(Calendar.MINUTE, val);
371 c.set(Calendar.SECOND, (int)secondsSinceMidnight - (val*60));
372 c.set(Calendar.MILLISECOND, 0);
373
374 long newTime = c.getTimeInMillis();
375 if (newTime < curTime) {
376 // The given time (in seconds since midnight) has already passed for today, so advance
377 // by one day (due to daylight savings, etc., the delta may differ from 24 hours).
378 c.add(Calendar.DAY_OF_MONTH, 1);
379 newTime = c.getTimeInMillis();
380 }
381
382 return newTime;
383 }
384
385 @Override
386 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700387 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 while (true) {
Michael Wright8fa56f62013-04-01 16:36:05 -0700389 final String name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 synchronized (this) {
391 long timeout = TIME_TO_WAIT;
Jean-Christophe PINCE1d08e592013-01-21 11:44:40 +0100392 mCompleted = false;
393 mHandler.sendEmptyMessage(MONITOR);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394
395 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
396 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700397 // 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 -0800398 // positive on when to kill things.
399 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700400 while (timeout > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700402 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800404 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
406 timeout = TIME_TO_WAIT - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408
Michael Wright8fa56f62013-04-01 16:36:05 -0700409 if (mCompleted) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 // The monitors have returned.
Christopher Tate6ee412d2010-05-28 12:01:56 -0700411 waitedHalf = false;
412 continue;
413 }
414
415 if (!waitedHalf) {
416 // We've waited half the deadlock-detection interval. Pull a stack
417 // trace and wait another half.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700418 ArrayList<Integer> pids = new ArrayList<Integer>();
Christopher Tate6ee412d2010-05-28 12:01:56 -0700419 pids.add(Process.myPid());
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700420 ActivityManagerService.dumpStackTraces(true, pids, null, null,
421 NATIVE_STACKS_OF_INTEREST);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700422 waitedHalf = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 continue;
424 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700425
426 name = (mCurrentMonitor != null) ?
Michael Wright4da44d12013-04-25 14:44:55 -0700427 mCurrentMonitor.getClass().getName() : "main thread blocked";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429
430 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700431 // First collect stack traces from all threads of the system process.
432 // Then kill this process so that the system will restart.
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800433 EventLog.writeEvent(EventLogTags.WATCHDOG, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -0700435 ArrayList<Integer> pids = new ArrayList<Integer>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800436 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800437 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700438 // Pass !waitedHalf so that just in case we somehow wind up here without having
439 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800440 final File stack = ActivityManagerService.dumpStackTraces(
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700441 !waitedHalf, pids, null, null, NATIVE_STACKS_OF_INTEREST);
Dan Egnor4bded072010-03-11 22:00:47 -0800442
443 // Give some extra time to make sure the stack traces get written.
444 // The system's been hanging for a minute, another second or two won't hurt much.
445 SystemClock.sleep(2000);
446
Christopher Tateecaa7b42010-06-04 14:55:02 -0700447 // Pull our own kernel thread stacks as well if we're configured for that
448 if (RECORD_KERNEL_THREADS) {
449 dumpKernelStackTraces();
450 }
451
Colin Cross5df1d872012-11-29 11:42:11 -0800452 // Trigger the kernel to dump all blocked threads to the kernel log
453 try {
454 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
455 sysrq_trigger.write("w");
456 sysrq_trigger.close();
457 } catch (IOException e) {
458 Slog.e(TAG, "Failed to write to /proc/sysrq-trigger");
459 Slog.e(TAG, e.getMessage());
460 }
461
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800462 // Try to add the error to the dropbox, but assuming that the ActivityManager
463 // itself may be deadlocked. (which has happened, causing this statement to
464 // deadlock and the watchdog as a whole to be ineffective)
465 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
466 public void run() {
467 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700468 "watchdog", null, "system_server", null, null,
469 name, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800470 }
471 };
472 dropboxThread.start();
473 try {
474 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
475 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700477 // Only kill the process if the debugger is not attached.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 if (!Debug.isDebuggerConnected()) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800479 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + name);
Michael Wright56a6c662013-04-30 20:13:07 -0700480 Slog.w(TAG, "Main thread stack trace:");
481 StackTraceElement[] stackTrace = mHandler.getLooper().getThread().getStackTrace();
482 for (StackTraceElement element: stackTrace) {
483 Slog.w(TAG, "\tat " + element);
484 }
485 Slog.w(TAG, "<End of main thread stack trace>");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700486 Process.killProcess(Process.myPid());
487 System.exit(10);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800488 } else {
489 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700491
492 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
494 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700495
496 private File dumpKernelStackTraces() {
497 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
498 if (tracesPath == null || tracesPath.length() == 0) {
499 return null;
500 }
501
502 native_dumpKernelStacks(tracesPath);
503 return new File(tracesPath);
504 }
505
506 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507}