The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 19 | import android.app.IActivityController; |
| 20 | import android.os.Binder; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 21 | import android.os.Build; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 23 | import android.system.ErrnoException; |
| 24 | import android.system.OsConstants; |
| 25 | import android.system.StructRlimit; |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 26 | import com.android.internal.os.ZygoteConnectionConstants; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import com.android.server.am.ActivityManagerService; |
| 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
| 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.IntentFilter; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 34 | import android.hidl.manager.V1_0.IServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.Debug; |
| 36 | import android.os.Handler; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 37 | import android.os.IPowerManager; |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 38 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.os.Process; |
Suchi Amalapurapu | 6ffce2e | 2010-03-08 14:48:40 -0800 | [diff] [blame] | 40 | import android.os.ServiceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.os.SystemClock; |
| 42 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.util.EventLog; |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 44 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 45 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 47 | import java.io.File; |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 48 | import java.io.FileWriter; |
| 49 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import java.util.ArrayList; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 51 | import java.util.Arrays; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 52 | import java.util.Collections; |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 53 | import java.util.HashSet; |
| 54 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
| 56 | /** This class calls its monitor every minute. Killing this process if they don't return **/ |
| 57 | public class Watchdog extends Thread { |
| 58 | static final String TAG = "Watchdog"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | |
| 60 | // Set this to true to use debug default values. |
| 61 | static final boolean DB = false; |
| 62 | |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 63 | // Note 1: Do not lower this value below thirty seconds without tightening the invoke-with |
| 64 | // timeout in com.android.internal.os.ZygoteConnection, or wrapped applications |
| 65 | // can trigger the watchdog. |
| 66 | // Note 2: The debug value is already below the wait time in ZygoteConnection. Wrapped |
| 67 | // applications may not work with a debug build. CTS will fail. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 68 | static final long DEFAULT_TIMEOUT = DB ? 10*1000 : 60*1000; |
| 69 | static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2; |
| 70 | |
| 71 | // These are temporally ordered: larger values as lateness increases |
| 72 | static final int COMPLETED = 0; |
| 73 | static final int WAITING = 1; |
| 74 | static final int WAITED_HALF = 2; |
| 75 | static final int OVERDUE = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
Igor Murashkin | 44d04aa | 2013-10-23 10:56:02 -0700 | [diff] [blame] | 77 | // Which native processes to dump into dropbox's stack traces |
| 78 | public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] { |
Andy Hung | 3a64ecb | 2016-03-09 13:55:58 -0800 | [diff] [blame] | 79 | "/system/bin/audioserver", |
Andy Hung | 689574a | 2016-04-13 19:27:43 -0700 | [diff] [blame] | 80 | "/system/bin/cameraserver", |
| 81 | "/system/bin/drmserver", |
| 82 | "/system/bin/mediadrmserver", |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 83 | "/system/bin/mediaserver", |
| 84 | "/system/bin/sdcard", |
Eric Laurent | 05d4e35 | 2016-03-14 18:49:08 -0700 | [diff] [blame] | 85 | "/system/bin/surfaceflinger", |
Andy Hung | 689574a | 2016-04-13 19:27:43 -0700 | [diff] [blame] | 86 | "media.extractor", // system/bin/mediaextractor |
Andy Hung | b1c4c93 | 2018-01-04 12:05:43 -0800 | [diff] [blame] | 87 | "media.metrics", // system/bin/mediametrics |
Chong Zhang | e55e6e0 | 2017-06-02 10:52:04 -0700 | [diff] [blame] | 88 | "media.codec", // vendor/bin/hw/android.hardware.media.omx@1.0-service |
Andreas Gampe | cf9e79b | 2016-05-11 18:41:25 -0700 | [diff] [blame] | 89 | "com.android.bluetooth", // Bluetooth service |
Andreas Gampe | e54440c | 2018-01-24 17:55:17 -0800 | [diff] [blame] | 90 | "statsd", // Stats daemon |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 93 | public static final List<String> HAL_INTERFACES_OF_INTEREST = Arrays.asList( |
| 94 | "android.hardware.audio@2.0::IDevicesFactory", |
| 95 | "android.hardware.bluetooth@1.0::IBluetoothHci", |
| 96 | "android.hardware.camera.provider@2.4::ICameraProvider", |
Chia-I Wu | 74debcd | 2017-04-21 11:14:22 -0700 | [diff] [blame] | 97 | "android.hardware.graphics.composer@2.1::IComposer", |
Peng Xu | 102122fb | 2017-07-11 21:12:11 -0700 | [diff] [blame] | 98 | "android.hardware.media.omx@1.0::IOmx", |
| 99 | "android.hardware.sensors@1.0::ISensors", |
| 100 | "android.hardware.vr@1.0::IVr" |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 101 | ); |
| 102 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | static Watchdog sWatchdog; |
| 104 | |
| 105 | /* This handler will be used to post message back onto the main thread */ |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 106 | final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 107 | final HandlerChecker mMonitorChecker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | ContentResolver mResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | ActivityManagerService mActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | int mPhonePid; |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 112 | IActivityController mController; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 113 | boolean mAllowRestart = true; |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 114 | final OpenFdMonitor mOpenFdMonitor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | /** |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 117 | * Used for checking status of handle threads and scheduling monitor callbacks. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | */ |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 119 | public final class HandlerChecker implements Runnable { |
| 120 | private final Handler mHandler; |
| 121 | private final String mName; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 122 | private final long mWaitMax; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 123 | private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 124 | private boolean mCompleted; |
| 125 | private Monitor mCurrentMonitor; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 126 | private long mStartTime; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 127 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 128 | HandlerChecker(Handler handler, String name, long waitMaxMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 129 | mHandler = handler; |
| 130 | mName = name; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 131 | mWaitMax = waitMaxMillis; |
| 132 | mCompleted = true; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public void addMonitor(Monitor monitor) { |
| 136 | mMonitors.add(monitor); |
| 137 | } |
| 138 | |
| 139 | public void scheduleCheckLocked() { |
Jeff Brown | 6c7b41a | 2015-02-26 14:43:53 -0800 | [diff] [blame] | 140 | if (mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling()) { |
| 141 | // If the target looper has recently been polling, then |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 142 | // there is no reason to enqueue our checker on it since that |
| 143 | // is as good as it not being deadlocked. This avoid having |
| 144 | // to do a context switch to check the thread. Note that we |
| 145 | // only do this if mCheckReboot is false and we have no |
| 146 | // monitors, since those would need to be executed at this point. |
| 147 | mCompleted = true; |
| 148 | return; |
| 149 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 150 | |
| 151 | if (!mCompleted) { |
| 152 | // we already have a check in flight, so no need |
| 153 | return; |
| 154 | } |
| 155 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 156 | mCompleted = false; |
| 157 | mCurrentMonitor = null; |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 158 | mStartTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 159 | mHandler.postAtFrontOfQueue(this); |
| 160 | } |
| 161 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 162 | public boolean isOverdueLocked() { |
| 163 | return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax); |
| 164 | } |
| 165 | |
| 166 | public int getCompletionStateLocked() { |
| 167 | if (mCompleted) { |
| 168 | return COMPLETED; |
| 169 | } else { |
| 170 | long latency = SystemClock.uptimeMillis() - mStartTime; |
| 171 | if (latency < mWaitMax/2) { |
| 172 | return WAITING; |
| 173 | } else if (latency < mWaitMax) { |
| 174 | return WAITED_HALF; |
| 175 | } |
| 176 | } |
| 177 | return OVERDUE; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 180 | public Thread getThread() { |
| 181 | return mHandler.getLooper().getThread(); |
| 182 | } |
| 183 | |
| 184 | public String getName() { |
| 185 | return mName; |
| 186 | } |
| 187 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 188 | public String describeBlockedStateLocked() { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 189 | if (mCurrentMonitor == null) { |
| 190 | return "Blocked in handler on " + mName + " (" + getThread().getName() + ")"; |
| 191 | } else { |
| 192 | return "Blocked in monitor " + mCurrentMonitor.getClass().getName() |
| 193 | + " on " + mName + " (" + getThread().getName() + ")"; |
| 194 | } |
John Michelau | 1164152 | 2013-03-18 18:28:23 -0500 | [diff] [blame] | 195 | } |
| 196 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | @Override |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 198 | public void run() { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 199 | final int size = mMonitors.size(); |
| 200 | for (int i = 0 ; i < size ; i++) { |
| 201 | synchronized (Watchdog.this) { |
| 202 | mCurrentMonitor = mMonitors.get(i); |
| 203 | } |
| 204 | mCurrentMonitor.monitor(); |
| 205 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 207 | synchronized (Watchdog.this) { |
| 208 | mCompleted = true; |
| 209 | mCurrentMonitor = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | final class RebootRequestReceiver extends BroadcastReceiver { |
| 215 | @Override |
| 216 | public void onReceive(Context c, Intent intent) { |
Dianne Hackborn | f6438b1 | 2013-05-09 18:53:48 -0700 | [diff] [blame] | 217 | if (intent.getIntExtra("nowait", 0) != 0) { |
| 218 | rebootSystem("Received ACTION_REBOOT broadcast"); |
| 219 | return; |
| 220 | } |
| 221 | Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Wale Ogunwale | 517daec | 2015-04-15 10:27:24 -0700 | [diff] [blame] | 225 | /** Monitor for checking the availability of binder threads. The monitor will block until |
| 226 | * there is a binder thread available to process in coming IPCs to make sure other processes |
| 227 | * can still communicate with the service. |
| 228 | */ |
| 229 | private static final class BinderThreadMonitor implements Watchdog.Monitor { |
| 230 | @Override |
| 231 | public void monitor() { |
| 232 | Binder.blockUntilThreadAvailable(); |
| 233 | } |
| 234 | } |
| 235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | public interface Monitor { |
| 237 | void monitor(); |
| 238 | } |
| 239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | public static Watchdog getInstance() { |
| 241 | if (sWatchdog == null) { |
| 242 | sWatchdog = new Watchdog(); |
| 243 | } |
| 244 | |
| 245 | return sWatchdog; |
| 246 | } |
| 247 | |
| 248 | private Watchdog() { |
| 249 | super("watchdog"); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 250 | // Initialize handler checkers for each common thread we want to check. Note |
| 251 | // that we are not currently checking the background thread, since it can |
| 252 | // potentially hold longer running operations with no guarantees about the timeliness |
| 253 | // of operations there. |
| 254 | |
| 255 | // The shared foreground thread is the main checker. It is where we |
| 256 | // will also dispatch monitor checks and do other work. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 257 | mMonitorChecker = new HandlerChecker(FgThread.getHandler(), |
| 258 | "foreground thread", DEFAULT_TIMEOUT); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 259 | mHandlerCheckers.add(mMonitorChecker); |
| 260 | // Add checker for main thread. We only do a quick check since there |
| 261 | // can be UI running on the thread. |
| 262 | mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()), |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 263 | "main thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 264 | // Add checker for shared UI thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 265 | mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(), |
| 266 | "ui thread", DEFAULT_TIMEOUT)); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 267 | // And also check IO thread. |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 268 | mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(), |
| 269 | "i/o thread", DEFAULT_TIMEOUT)); |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 270 | // And the display thread. |
| 271 | mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(), |
| 272 | "display thread", DEFAULT_TIMEOUT)); |
Wale Ogunwale | 517daec | 2015-04-15 10:27:24 -0700 | [diff] [blame] | 273 | |
| 274 | // Initialize monitor for Binder threads. |
| 275 | addMonitor(new BinderThreadMonitor()); |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 276 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 277 | mOpenFdMonitor = OpenFdMonitor.create(); |
| 278 | |
Andreas Gampe | 032a929 | 2017-07-21 11:41:00 -0700 | [diff] [blame] | 279 | // See the notes on DEFAULT_TIMEOUT. |
| 280 | assert DB || |
| 281 | DEFAULT_TIMEOUT > ZygoteConnectionConstants.WRAPPED_PID_TIMEOUT_MILLIS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 284 | public void init(Context context, ActivityManagerService activity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | mResolver = context.getContentResolver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | mActivity = activity; |
| 287 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | context.registerReceiver(new RebootRequestReceiver(), |
| 289 | new IntentFilter(Intent.ACTION_REBOOT), |
| 290 | android.Manifest.permission.REBOOT, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Christopher Tate | c27181c | 2010-06-30 14:41:09 -0700 | [diff] [blame] | 293 | public void processStarted(String name, int pid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | synchronized (this) { |
| 295 | if ("com.android.phone".equals(name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | mPhonePid = pid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 301 | public void setActivityController(IActivityController controller) { |
| 302 | synchronized (this) { |
| 303 | mController = controller; |
| 304 | } |
| 305 | } |
| 306 | |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 307 | public void setAllowRestart(boolean allowRestart) { |
| 308 | synchronized (this) { |
| 309 | mAllowRestart = allowRestart; |
| 310 | } |
| 311 | } |
| 312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | public void addMonitor(Monitor monitor) { |
| 314 | synchronized (this) { |
| 315 | if (isAlive()) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 316 | throw new RuntimeException("Monitors can't be added once the Watchdog is running"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 318 | mMonitorChecker.addMonitor(monitor); |
| 319 | } |
| 320 | } |
| 321 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 322 | public void addThread(Handler thread) { |
| 323 | addThread(thread, DEFAULT_TIMEOUT); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 326 | public void addThread(Handler thread, long timeoutMillis) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 327 | synchronized (this) { |
| 328 | if (isAlive()) { |
| 329 | throw new RuntimeException("Threads can't be added once the Watchdog is running"); |
| 330 | } |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 331 | final String name = thread.getLooper().getThread().getName(); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 332 | mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | /** |
| 337 | * Perform a full reboot of the system. |
| 338 | */ |
| 339 | void rebootSystem(String reason) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 340 | Slog.i(TAG, "Rebooting system because: " + reason); |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 341 | IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE); |
| 342 | try { |
| 343 | pms.reboot(false, reason, false); |
| 344 | } catch (RemoteException ex) { |
| 345 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 348 | private int evaluateCheckerCompletionLocked() { |
| 349 | int state = COMPLETED; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 350 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 351 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 352 | state = Math.max(state, hc.getCompletionStateLocked()); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 353 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 354 | return state; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 357 | private ArrayList<HandlerChecker> getBlockedCheckersLocked() { |
| 358 | ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 359 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 360 | HandlerChecker hc = mHandlerCheckers.get(i); |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 361 | if (hc.isOverdueLocked()) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 362 | checkers.add(hc); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 365 | return checkers; |
| 366 | } |
| 367 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 368 | private String describeCheckersLocked(List<HandlerChecker> checkers) { |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 369 | StringBuilder builder = new StringBuilder(128); |
| 370 | for (int i=0; i<checkers.size(); i++) { |
| 371 | if (builder.length() > 0) { |
| 372 | builder.append(", "); |
| 373 | } |
| 374 | builder.append(checkers.get(i).describeBlockedStateLocked()); |
| 375 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 376 | return builder.toString(); |
| 377 | } |
| 378 | |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 379 | private ArrayList<Integer> getInterestingHalPids() { |
| 380 | try { |
| 381 | IServiceManager serviceManager = IServiceManager.getService(); |
| 382 | ArrayList<IServiceManager.InstanceDebugInfo> dump = |
| 383 | serviceManager.debugDump(); |
| 384 | HashSet<Integer> pids = new HashSet<>(); |
| 385 | for (IServiceManager.InstanceDebugInfo info : dump) { |
| 386 | if (info.pid == IServiceManager.PidConstant.NO_PID) { |
| 387 | continue; |
| 388 | } |
| 389 | |
| 390 | if (!HAL_INTERFACES_OF_INTEREST.contains(info.interfaceName)) { |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | pids.add(info.pid); |
| 395 | } |
| 396 | return new ArrayList<Integer>(pids); |
| 397 | } catch (RemoteException e) { |
| 398 | return new ArrayList<Integer>(); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | private ArrayList<Integer> getInterestingNativePids() { |
| 403 | ArrayList<Integer> pids = getInterestingHalPids(); |
| 404 | |
| 405 | int[] nativePids = Process.getPidsForCommands(NATIVE_STACKS_OF_INTEREST); |
| 406 | if (nativePids != null) { |
| 407 | pids.ensureCapacity(pids.size() + nativePids.length); |
| 408 | for (int i : nativePids) { |
| 409 | pids.add(i); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return pids; |
| 414 | } |
| 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | @Override |
| 417 | public void run() { |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 418 | boolean waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | while (true) { |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 420 | final List<HandlerChecker> blockedCheckers; |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 421 | final String subject; |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 422 | final boolean allowRestart; |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 423 | int debuggerWasConnected = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | synchronized (this) { |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 425 | long timeout = CHECK_INTERVAL; |
| 426 | // Make sure we (re)spin the checkers that have become idle within |
| 427 | // this wait-and-check interval |
| 428 | for (int i=0; i<mHandlerCheckers.size(); i++) { |
| 429 | HandlerChecker hc = mHandlerCheckers.get(i); |
| 430 | hc.scheduleCheckLocked(); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 431 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 433 | if (debuggerWasConnected > 0) { |
| 434 | debuggerWasConnected--; |
| 435 | } |
| 436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | // NOTE: We use uptimeMillis() here because we do not want to increment the time we |
| 438 | // wait while asleep. If the device is asleep then the thing that we are waiting |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 439 | // to timeout on is asleep as well and won't have a chance to run, causing a false |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | // positive on when to kill things. |
| 441 | long start = SystemClock.uptimeMillis(); |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 442 | while (timeout > 0) { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 443 | if (Debug.isDebuggerConnected()) { |
| 444 | debuggerWasConnected = 2; |
| 445 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | try { |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 447 | wait(timeout); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | } catch (InterruptedException e) { |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 449 | Log.wtf(TAG, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | } |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 451 | if (Debug.isDebuggerConnected()) { |
| 452 | debuggerWasConnected = 2; |
| 453 | } |
Christopher Tate | e6f81cf | 2013-10-23 17:28:27 -0700 | [diff] [blame] | 454 | timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 455 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 457 | boolean fdLimitTriggered = false; |
| 458 | if (mOpenFdMonitor != null) { |
| 459 | fdLimitTriggered = mOpenFdMonitor.monitor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | } |
Michael Wright | 8fa56f6 | 2013-04-01 16:36:05 -0700 | [diff] [blame] | 461 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 462 | if (!fdLimitTriggered) { |
| 463 | final int waitState = evaluateCheckerCompletionLocked(); |
| 464 | if (waitState == COMPLETED) { |
| 465 | // The monitors have returned; reset |
| 466 | waitedHalf = false; |
| 467 | continue; |
| 468 | } else if (waitState == WAITING) { |
| 469 | // still waiting but within their configured intervals; back off and recheck |
| 470 | continue; |
| 471 | } else if (waitState == WAITED_HALF) { |
| 472 | if (!waitedHalf) { |
| 473 | // We've waited half the deadlock-detection interval. Pull a stack |
| 474 | // trace and wait another half. |
| 475 | ArrayList<Integer> pids = new ArrayList<Integer>(); |
| 476 | pids.add(Process.myPid()); |
| 477 | ActivityManagerService.dumpStackTraces(true, pids, null, null, |
| 478 | getInterestingNativePids()); |
| 479 | waitedHalf = true; |
| 480 | } |
| 481 | continue; |
| 482 | } |
| 483 | |
| 484 | // something is overdue! |
| 485 | blockedCheckers = getBlockedCheckersLocked(); |
| 486 | subject = describeCheckersLocked(blockedCheckers); |
| 487 | } else { |
| 488 | blockedCheckers = Collections.emptyList(); |
| 489 | subject = "Open FD high water mark reached"; |
| 490 | } |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 491 | allowRestart = mAllowRestart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // If we got here, that means that the system is most likely hung. |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 495 | // First collect stack traces from all threads of the system process. |
| 496 | // Then kill this process so that the system will restart. |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 497 | EventLog.writeEvent(EventLogTags.WATCHDOG, subject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 499 | ArrayList<Integer> pids = new ArrayList<>(); |
Dan Egnor | 9bdc94b | 2010-03-04 14:20:31 -0800 | [diff] [blame] | 500 | pids.add(Process.myPid()); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 501 | if (mPhonePid > 0) pids.add(mPhonePid); |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 502 | // Pass !waitedHalf so that just in case we somehow wind up here without having |
| 503 | // dumped the halfway stacks, we properly re-initialize the trace file. |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 504 | final File stack = ActivityManagerService.dumpStackTraces( |
Steven Moreland | 6b47c54 | 2017-03-21 12:52:16 -0700 | [diff] [blame] | 505 | !waitedHalf, pids, null, null, getInterestingNativePids()); |
Dan Egnor | 4bded07 | 2010-03-11 22:00:47 -0800 | [diff] [blame] | 506 | |
| 507 | // Give some extra time to make sure the stack traces get written. |
| 508 | // The system's been hanging for a minute, another second or two won't hurt much. |
| 509 | SystemClock.sleep(2000); |
| 510 | |
Guang Zhu | 0620c45 | 2014-10-29 14:31:48 -0700 | [diff] [blame] | 511 | // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log |
| 512 | doSysRq('w'); |
| 513 | doSysRq('l'); |
Colin Cross | 5df1d87 | 2012-11-29 11:42:11 -0800 | [diff] [blame] | 514 | |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 515 | // Try to add the error to the dropbox, but assuming that the ActivityManager |
| 516 | // itself may be deadlocked. (which has happened, causing this statement to |
| 517 | // deadlock and the watchdog as a whole to be ineffective) |
| 518 | Thread dropboxThread = new Thread("watchdogWriteToDropbox") { |
| 519 | public void run() { |
| 520 | mActivity.addErrorToDropBox( |
Jeff Sharkey | a353d26 | 2011-10-28 11:12:06 -0700 | [diff] [blame] | 521 | "watchdog", null, "system_server", null, null, |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 522 | subject, null, stack, null); |
Brad Fitzpatrick | 9765c72 | 2011-01-14 11:28:22 -0800 | [diff] [blame] | 523 | } |
| 524 | }; |
| 525 | dropboxThread.start(); |
| 526 | try { |
| 527 | dropboxThread.join(2000); // wait up to 2 seconds for it to return. |
| 528 | } catch (InterruptedException ignored) {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 530 | IActivityController controller; |
| 531 | synchronized (this) { |
| 532 | controller = mController; |
| 533 | } |
| 534 | if (controller != null) { |
| 535 | Slog.i(TAG, "Reporting stuck state to activity controller"); |
| 536 | try { |
| 537 | Binder.setDumpDisabled("Service dumps disabled due to hung system process."); |
| 538 | // 1 = keep waiting, -1 = kill system |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 539 | int res = controller.systemNotResponding(subject); |
Dianne Hackborn | 5b88a2f | 2013-05-03 16:25:11 -0700 | [diff] [blame] | 540 | if (res >= 0) { |
| 541 | Slog.i(TAG, "Activity controller requested to coninue to wait"); |
| 542 | waitedHalf = false; |
| 543 | continue; |
| 544 | } |
| 545 | } catch (RemoteException e) { |
| 546 | } |
| 547 | } |
| 548 | |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 549 | // Only kill the process if the debugger is not attached. |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 550 | if (Debug.isDebuggerConnected()) { |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 551 | debuggerWasConnected = 2; |
| 552 | } |
| 553 | if (debuggerWasConnected >= 2) { |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 554 | Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process"); |
Dianne Hackborn | 89ad456 | 2014-08-24 16:45:38 -0700 | [diff] [blame] | 555 | } else if (debuggerWasConnected > 0) { |
| 556 | Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process"); |
Dianne Hackborn | 8bd64df | 2013-05-06 16:07:26 -0700 | [diff] [blame] | 557 | } else if (!allowRestart) { |
| 558 | Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process"); |
| 559 | } else { |
Jeff Brown | 7dd2d19 | 2013-09-06 15:05:23 -0700 | [diff] [blame] | 560 | Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject); |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 561 | for (int i=0; i<blockedCheckers.size(); i++) { |
| 562 | Slog.w(TAG, blockedCheckers.get(i).getName() + " stack trace:"); |
| 563 | StackTraceElement[] stackTrace |
| 564 | = blockedCheckers.get(i).getThread().getStackTrace(); |
| 565 | for (StackTraceElement element: stackTrace) { |
| 566 | Slog.w(TAG, " at " + element); |
| 567 | } |
Michael Wright | 56a6c66 | 2013-04-30 20:13:07 -0700 | [diff] [blame] | 568 | } |
Dianne Hackborn | fa012b3 | 2013-05-10 15:23:28 -0700 | [diff] [blame] | 569 | Slog.w(TAG, "*** GOODBYE!"); |
Jean-Baptiste Queru | 784827b | 2012-09-04 13:35:12 -0700 | [diff] [blame] | 570 | Process.killProcess(Process.myPid()); |
| 571 | System.exit(10); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | } |
Christopher Tate | 6ee412d | 2010-05-28 12:01:56 -0700 | [diff] [blame] | 573 | |
| 574 | waitedHalf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | } |
| 576 | } |
Christopher Tate | ecaa7b4 | 2010-06-04 14:55:02 -0700 | [diff] [blame] | 577 | |
Guang Zhu | 0620c45 | 2014-10-29 14:31:48 -0700 | [diff] [blame] | 578 | private void doSysRq(char c) { |
| 579 | try { |
| 580 | FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger"); |
| 581 | sysrq_trigger.write(c); |
| 582 | sysrq_trigger.close(); |
| 583 | } catch (IOException e) { |
| 584 | Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e); |
| 585 | } |
| 586 | } |
| 587 | |
Narayan Kamath | a0a2808 | 2017-07-31 15:58:59 +0100 | [diff] [blame] | 588 | public static final class OpenFdMonitor { |
| 589 | /** |
| 590 | * Number of FDs below the soft limit that we trigger a runtime restart at. This was |
| 591 | * chosen arbitrarily, but will need to be at least 6 in order to have a sufficient number |
| 592 | * of FDs in reserve to complete a dump. |
| 593 | */ |
| 594 | private static final int FD_HIGH_WATER_MARK = 12; |
| 595 | |
| 596 | private final File mDumpDir; |
| 597 | private final File mFdHighWaterMark; |
| 598 | |
| 599 | public static OpenFdMonitor create() { |
| 600 | // Only run the FD monitor on debuggable builds (such as userdebug and eng builds). |
| 601 | if (!Build.IS_DEBUGGABLE) { |
| 602 | return null; |
| 603 | } |
| 604 | |
| 605 | // Don't run the FD monitor on builds that have a global ANR trace file. We're using |
| 606 | // the ANR trace directory as a quick hack in order to get these traces in bugreports |
| 607 | // and we wouldn't want to overwrite something important. |
| 608 | final String dumpDirStr = SystemProperties.get("dalvik.vm.stack-trace-dir", ""); |
| 609 | if (dumpDirStr.isEmpty()) { |
| 610 | return null; |
| 611 | } |
| 612 | |
| 613 | final StructRlimit rlimit; |
| 614 | try { |
| 615 | rlimit = android.system.Os.getrlimit(OsConstants.RLIMIT_NOFILE); |
| 616 | } catch (ErrnoException errno) { |
| 617 | Slog.w(TAG, "Error thrown from getrlimit(RLIMIT_NOFILE)", errno); |
| 618 | return null; |
| 619 | } |
| 620 | |
| 621 | // The assumption we're making here is that FD numbers are allocated (more or less) |
| 622 | // sequentially, which is currently (and historically) true since open is currently |
| 623 | // specified to always return the lowest-numbered non-open file descriptor for the |
| 624 | // current process. |
| 625 | // |
| 626 | // We do this to avoid having to enumerate the contents of /proc/self/fd in order to |
| 627 | // count the number of descriptors open in the process. |
| 628 | final File fdThreshold = new File("/proc/self/fd/" + (rlimit.rlim_cur - FD_HIGH_WATER_MARK)); |
| 629 | return new OpenFdMonitor(new File(dumpDirStr), fdThreshold); |
| 630 | } |
| 631 | |
| 632 | OpenFdMonitor(File dumpDir, File fdThreshold) { |
| 633 | mDumpDir = dumpDir; |
| 634 | mFdHighWaterMark = fdThreshold; |
| 635 | } |
| 636 | |
| 637 | private void dumpOpenDescriptors() { |
| 638 | try { |
| 639 | File dumpFile = File.createTempFile("anr_fd_", "", mDumpDir); |
| 640 | java.lang.Process proc = new ProcessBuilder() |
| 641 | .command("/system/bin/lsof", "-p", String.valueOf(Process.myPid())) |
| 642 | .redirectErrorStream(true) |
| 643 | .redirectOutput(dumpFile) |
| 644 | .start(); |
| 645 | |
| 646 | int returnCode = proc.waitFor(); |
| 647 | if (returnCode != 0) { |
| 648 | Slog.w(TAG, "Unable to dump open descriptors, lsof return code: " |
| 649 | + returnCode); |
| 650 | dumpFile.delete(); |
| 651 | } |
| 652 | } catch (IOException | InterruptedException ex) { |
| 653 | Slog.w(TAG, "Unable to dump open descriptors: " + ex); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * @return {@code true} if the high water mark was breached and a dump was written, |
| 659 | * {@code false} otherwise. |
| 660 | */ |
| 661 | public boolean monitor() { |
| 662 | if (mFdHighWaterMark.exists()) { |
| 663 | dumpOpenDescriptors(); |
| 664 | return true; |
| 665 | } |
| 666 | |
| 667 | return false; |
| 668 | } |
| 669 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | } |