blob: 80f89fc938ada325b370cd53f289de6feff4469d [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;
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.BroadcastReceiver;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
Steven Moreland6b47c542017-03-21 12:52:16 -070029import android.hidl.manager.V1_0.IServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Debug;
31import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080032import android.os.IPowerManager;
John Michelau11641522013-03-18 18:28:23 -050033import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import 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;
Steven Moreland6b47c542017-03-21 12:52:16 -070046import java.util.Arrays;
47import java.util.HashSet;
48import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50/** This class calls its monitor every minute. Killing this process if they don't return **/
51public class Watchdog extends Thread {
52 static final String TAG = "Watchdog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
54 // Set this to true to use debug default values.
55 static final boolean DB = false;
56
Christopher Tateecaa7b42010-06-04 14:55:02 -070057 // Set this to true to have the watchdog record kernel thread stacks when it fires
58 static final boolean RECORD_KERNEL_THREADS = true;
59
Christopher Tatee6f81cf2013-10-23 17:28:27 -070060 static final long DEFAULT_TIMEOUT = DB ? 10*1000 : 60*1000;
61 static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2;
62
63 // These are temporally ordered: larger values as lateness increases
64 static final int COMPLETED = 0;
65 static final int WAITING = 1;
66 static final int WAITED_HALF = 2;
67 static final int OVERDUE = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Igor Murashkin44d04aa2013-10-23 10:56:02 -070069 // Which native processes to dump into dropbox's stack traces
70 public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
Andy Hung3a64ecb2016-03-09 13:55:58 -080071 "/system/bin/audioserver",
Andy Hung689574a2016-04-13 19:27:43 -070072 "/system/bin/cameraserver",
73 "/system/bin/drmserver",
74 "/system/bin/mediadrmserver",
Dianne Hackbornf72467a2012-06-08 17:23:59 -070075 "/system/bin/mediaserver",
76 "/system/bin/sdcard",
Eric Laurent05d4e352016-03-14 18:49:08 -070077 "/system/bin/surfaceflinger",
Andy Hung689574a2016-04-13 19:27:43 -070078 "media.extractor", // system/bin/mediaextractor
Andreas Gampecf9e79b2016-05-11 18:41:25 -070079 "com.android.bluetooth", // Bluetooth service
Dianne Hackbornf72467a2012-06-08 17:23:59 -070080 };
81
Steven Moreland6b47c542017-03-21 12:52:16 -070082 public static final List<String> HAL_INTERFACES_OF_INTEREST = Arrays.asList(
83 "android.hardware.audio@2.0::IDevicesFactory",
84 "android.hardware.bluetooth@1.0::IBluetoothHci",
85 "android.hardware.camera.provider@2.4::ICameraProvider",
86 "android.hardware.vr@1.0::IVr",
87 "android.hardware.media.omx@1.0::IOmx"
88 );
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 static Watchdog sWatchdog;
91
92 /* This handler will be used to post message back onto the main thread */
Wale Ogunwaled7fdd022015-04-13 16:22:38 -070093 final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -070094 final HandlerChecker mMonitorChecker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 ContentResolver mResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 int mPhonePid;
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -070099 IActivityController mController;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700100 boolean mAllowRestart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700103 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700105 public final class HandlerChecker implements Runnable {
106 private final Handler mHandler;
107 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700108 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700109 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700110 private boolean mCompleted;
111 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700112 private long mStartTime;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700113
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700114 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700115 mHandler = handler;
116 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700117 mWaitMax = waitMaxMillis;
118 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700119 }
120
121 public void addMonitor(Monitor monitor) {
122 mMonitors.add(monitor);
123 }
124
125 public void scheduleCheckLocked() {
Jeff Brown6c7b41a2015-02-26 14:43:53 -0800126 if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) {
127 // If the target looper has recently been polling, then
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700128 // there is no reason to enqueue our checker on it since that
129 // is as good as it not being deadlocked. This avoid having
130 // to do a context switch to check the thread. Note that we
131 // only do this if mCheckReboot is false and we have no
132 // monitors, since those would need to be executed at this point.
133 mCompleted = true;
134 return;
135 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700136
137 if (!mCompleted) {
138 // we already have a check in flight, so no need
139 return;
140 }
141
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700142 mCompleted = false;
143 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700144 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700145 mHandler.postAtFrontOfQueue(this);
146 }
147
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700148 public boolean isOverdueLocked() {
149 return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax);
150 }
151
152 public int getCompletionStateLocked() {
153 if (mCompleted) {
154 return COMPLETED;
155 } else {
156 long latency = SystemClock.uptimeMillis() - mStartTime;
157 if (latency < mWaitMax/2) {
158 return WAITING;
159 } else if (latency < mWaitMax) {
160 return WAITED_HALF;
161 }
162 }
163 return OVERDUE;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700164 }
165
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700166 public Thread getThread() {
167 return mHandler.getLooper().getThread();
168 }
169
170 public String getName() {
171 return mName;
172 }
173
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700174 public String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700175 if (mCurrentMonitor == null) {
176 return "Blocked in handler on " + mName + " (" + getThread().getName() + ")";
177 } else {
178 return "Blocked in monitor " + mCurrentMonitor.getClass().getName()
179 + " on " + mName + " (" + getThread().getName() + ")";
180 }
John Michelau11641522013-03-18 18:28:23 -0500181 }
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700184 public void run() {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700185 final int size = mMonitors.size();
186 for (int i = 0 ; i < size ; i++) {
187 synchronized (Watchdog.this) {
188 mCurrentMonitor = mMonitors.get(i);
189 }
190 mCurrentMonitor.monitor();
191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700193 synchronized (Watchdog.this) {
194 mCompleted = true;
195 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 }
197 }
198 }
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 final class RebootRequestReceiver extends BroadcastReceiver {
201 @Override
202 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700203 if (intent.getIntExtra("nowait", 0) != 0) {
204 rebootSystem("Received ACTION_REBOOT broadcast");
205 return;
206 }
207 Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 }
209 }
210
Wale Ogunwale517daec2015-04-15 10:27:24 -0700211 /** Monitor for checking the availability of binder threads. The monitor will block until
212 * there is a binder thread available to process in coming IPCs to make sure other processes
213 * can still communicate with the service.
214 */
215 private static final class BinderThreadMonitor implements Watchdog.Monitor {
216 @Override
217 public void monitor() {
218 Binder.blockUntilThreadAvailable();
219 }
220 }
221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 public interface Monitor {
223 void monitor();
224 }
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 public static Watchdog getInstance() {
227 if (sWatchdog == null) {
228 sWatchdog = new Watchdog();
229 }
230
231 return sWatchdog;
232 }
233
234 private Watchdog() {
235 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700236 // Initialize handler checkers for each common thread we want to check. Note
237 // that we are not currently checking the background thread, since it can
238 // potentially hold longer running operations with no guarantees about the timeliness
239 // of operations there.
240
241 // The shared foreground thread is the main checker. It is where we
242 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700243 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
244 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700245 mHandlerCheckers.add(mMonitorChecker);
246 // Add checker for main thread. We only do a quick check since there
247 // can be UI running on the thread.
248 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700249 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700250 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700251 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
252 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700253 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700254 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
255 "i/o thread", DEFAULT_TIMEOUT));
Jeff Brown4ccb8232014-01-16 22:16:42 -0800256 // And the display thread.
257 mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
258 "display thread", DEFAULT_TIMEOUT));
Wale Ogunwale517daec2015-04-15 10:27:24 -0700259
260 // Initialize monitor for Binder threads.
261 addMonitor(new BinderThreadMonitor());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263
Adam Lesinski182f73f2013-12-05 16:48:06 -0800264 public void init(Context context, ActivityManagerService activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mResolver = context.getContentResolver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 mActivity = activity;
267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 context.registerReceiver(new RebootRequestReceiver(),
269 new IntentFilter(Intent.ACTION_REBOOT),
270 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 }
272
Christopher Tatec27181c2010-06-30 14:41:09 -0700273 public void processStarted(String name, int pid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 synchronized (this) {
275 if ("com.android.phone".equals(name)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 mPhonePid = pid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 }
278 }
279 }
280
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700281 public void setActivityController(IActivityController controller) {
282 synchronized (this) {
283 mController = controller;
284 }
285 }
286
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700287 public void setAllowRestart(boolean allowRestart) {
288 synchronized (this) {
289 mAllowRestart = allowRestart;
290 }
291 }
292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 public void addMonitor(Monitor monitor) {
294 synchronized (this) {
295 if (isAlive()) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700296 throw new RuntimeException("Monitors can't be added once the Watchdog is running");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700298 mMonitorChecker.addMonitor(monitor);
299 }
300 }
301
Jeff Brown6f357d32014-01-15 20:40:55 -0800302 public void addThread(Handler thread) {
303 addThread(thread, DEFAULT_TIMEOUT);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700304 }
305
Jeff Brown6f357d32014-01-15 20:40:55 -0800306 public void addThread(Handler thread, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700307 synchronized (this) {
308 if (isAlive()) {
309 throw new RuntimeException("Threads can't be added once the Watchdog is running");
310 }
Jeff Brown6f357d32014-01-15 20:40:55 -0800311 final String name = thread.getLooper().getThread().getName();
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700312 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
314 }
315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 /**
317 * Perform a full reboot of the system.
318 */
319 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800320 Slog.i(TAG, "Rebooting system because: " + reason);
Jeff Brown6f357d32014-01-15 20:40:55 -0800321 IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
322 try {
323 pms.reboot(false, reason, false);
324 } catch (RemoteException ex) {
325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700328 private int evaluateCheckerCompletionLocked() {
329 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700330 for (int i=0; i<mHandlerCheckers.size(); i++) {
331 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700332 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700333 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700334 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700335 }
336
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700337 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
338 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700339 for (int i=0; i<mHandlerCheckers.size(); i++) {
340 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700341 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700342 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700343 }
344 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700345 return checkers;
346 }
347
348 private String describeCheckersLocked(ArrayList<HandlerChecker> checkers) {
349 StringBuilder builder = new StringBuilder(128);
350 for (int i=0; i<checkers.size(); i++) {
351 if (builder.length() > 0) {
352 builder.append(", ");
353 }
354 builder.append(checkers.get(i).describeBlockedStateLocked());
355 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700356 return builder.toString();
357 }
358
Steven Moreland6b47c542017-03-21 12:52:16 -0700359 private ArrayList<Integer> getInterestingHalPids() {
360 try {
361 IServiceManager serviceManager = IServiceManager.getService();
362 ArrayList<IServiceManager.InstanceDebugInfo> dump =
363 serviceManager.debugDump();
364 HashSet<Integer> pids = new HashSet<>();
365 for (IServiceManager.InstanceDebugInfo info : dump) {
366 if (info.pid == IServiceManager.PidConstant.NO_PID) {
367 continue;
368 }
369
370 if (!HAL_INTERFACES_OF_INTEREST.contains(info.interfaceName)) {
371 continue;
372 }
373
374 pids.add(info.pid);
375 }
376 return new ArrayList<Integer>(pids);
377 } catch (RemoteException e) {
378 return new ArrayList<Integer>();
379 }
380 }
381
382 private ArrayList<Integer> getInterestingNativePids() {
383 ArrayList<Integer> pids = getInterestingHalPids();
384
385 int[] nativePids = Process.getPidsForCommands(NATIVE_STACKS_OF_INTEREST);
386 if (nativePids != null) {
387 pids.ensureCapacity(pids.size() + nativePids.length);
388 for (int i : nativePids) {
389 pids.add(i);
390 }
391 }
392
393 return pids;
394 }
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 @Override
397 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700398 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 while (true) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700400 final ArrayList<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700401 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700402 final boolean allowRestart;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700403 int debuggerWasConnected = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700405 long timeout = CHECK_INTERVAL;
406 // Make sure we (re)spin the checkers that have become idle within
407 // this wait-and-check interval
408 for (int i=0; i<mHandlerCheckers.size(); i++) {
409 HandlerChecker hc = mHandlerCheckers.get(i);
410 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700413 if (debuggerWasConnected > 0) {
414 debuggerWasConnected--;
415 }
416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
418 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700419 // 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 -0800420 // positive on when to kill things.
421 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700422 while (timeout > 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700423 if (Debug.isDebuggerConnected()) {
424 debuggerWasConnected = 2;
425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700427 wait(timeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800429 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700431 if (Debug.isDebuggerConnected()) {
432 debuggerWasConnected = 2;
433 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700434 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700437 final int waitState = evaluateCheckerCompletionLocked();
438 if (waitState == COMPLETED) {
439 // The monitors have returned; reset
Christopher Tate6ee412d2010-05-28 12:01:56 -0700440 waitedHalf = false;
441 continue;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700442 } else if (waitState == WAITING) {
443 // still waiting but within their configured intervals; back off and recheck
444 continue;
445 } else if (waitState == WAITED_HALF) {
446 if (!waitedHalf) {
447 // We've waited half the deadlock-detection interval. Pull a stack
448 // trace and wait another half.
449 ArrayList<Integer> pids = new ArrayList<Integer>();
450 pids.add(Process.myPid());
451 ActivityManagerService.dumpStackTraces(true, pids, null, null,
Steven Moreland6b47c542017-03-21 12:52:16 -0700452 getInterestingNativePids());
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700453 waitedHalf = true;
454 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 continue;
456 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700457
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700458 // something is overdue!
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700459 blockedCheckers = getBlockedCheckersLocked();
Jeff Brown7dd2d192013-09-06 15:05:23 -0700460 subject = describeCheckersLocked(blockedCheckers);
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700461 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463
464 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700465 // First collect stack traces from all threads of the system process.
466 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700467 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468
Steven Moreland6b47c542017-03-21 12:52:16 -0700469 ArrayList<Integer> pids = new ArrayList<>();
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800470 pids.add(Process.myPid());
Dan Egnor4bded072010-03-11 22:00:47 -0800471 if (mPhonePid > 0) pids.add(mPhonePid);
Christopher Tate6ee412d2010-05-28 12:01:56 -0700472 // Pass !waitedHalf so that just in case we somehow wind up here without having
473 // dumped the halfway stacks, we properly re-initialize the trace file.
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800474 final File stack = ActivityManagerService.dumpStackTraces(
Steven Moreland6b47c542017-03-21 12:52:16 -0700475 !waitedHalf, pids, null, null, getInterestingNativePids());
Dan Egnor4bded072010-03-11 22:00:47 -0800476
477 // Give some extra time to make sure the stack traces get written.
478 // The system's been hanging for a minute, another second or two won't hurt much.
479 SystemClock.sleep(2000);
480
Christopher Tateecaa7b42010-06-04 14:55:02 -0700481 // Pull our own kernel thread stacks as well if we're configured for that
482 if (RECORD_KERNEL_THREADS) {
483 dumpKernelStackTraces();
484 }
485
Guang Zhu0620c452014-10-29 14:31:48 -0700486 // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log
487 doSysRq('w');
488 doSysRq('l');
Colin Cross5df1d872012-11-29 11:42:11 -0800489
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800490 // Try to add the error to the dropbox, but assuming that the ActivityManager
491 // itself may be deadlocked. (which has happened, causing this statement to
492 // deadlock and the watchdog as a whole to be ineffective)
493 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
494 public void run() {
495 mActivity.addErrorToDropBox(
Jeff Sharkeya353d262011-10-28 11:12:06 -0700496 "watchdog", null, "system_server", null, null,
Jeff Brown7dd2d192013-09-06 15:05:23 -0700497 subject, null, stack, null);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800498 }
499 };
500 dropboxThread.start();
501 try {
502 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
503 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700505 IActivityController controller;
506 synchronized (this) {
507 controller = mController;
508 }
509 if (controller != null) {
510 Slog.i(TAG, "Reporting stuck state to activity controller");
511 try {
512 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
513 // 1 = keep waiting, -1 = kill system
Jeff Brown7dd2d192013-09-06 15:05:23 -0700514 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700515 if (res >= 0) {
516 Slog.i(TAG, "Activity controller requested to coninue to wait");
517 waitedHalf = false;
518 continue;
519 }
520 } catch (RemoteException e) {
521 }
522 }
523
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700524 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700525 if (Debug.isDebuggerConnected()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700526 debuggerWasConnected = 2;
527 }
528 if (debuggerWasConnected >= 2) {
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700529 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700530 } else if (debuggerWasConnected > 0) {
531 Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process");
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700532 } else if (!allowRestart) {
533 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
534 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700535 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700536 for (int i=0; i<blockedCheckers.size(); i++) {
537 Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:");
538 StackTraceElement[] stackTrace
539 = blockedCheckers.get(i).getThread().getStackTrace();
540 for (StackTraceElement element: stackTrace) {
541 Slog.w(TAG, " at " + element);
542 }
Michael Wright56a6c662013-04-30 20:13:07 -0700543 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700544 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700545 Process.killProcess(Process.myPid());
546 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700548
549 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700552
Guang Zhu0620c452014-10-29 14:31:48 -0700553 private void doSysRq(char c) {
554 try {
555 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
556 sysrq_trigger.write(c);
557 sysrq_trigger.close();
558 } catch (IOException e) {
559 Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e);
560 }
561 }
562
Christopher Tateecaa7b42010-06-04 14:55:02 -0700563 private File dumpKernelStackTraces() {
564 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
565 if (tracesPath == null || tracesPath.length() == 0) {
566 return null;
567 }
568
569 native_dumpKernelStacks(tracesPath);
570 return new File(tracesPath);
571 }
572
573 private native void native_dumpKernelStacks(String tracesPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574}