blob: 061ff42de60be4a56a39d00aed8f5fab390d78ae [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Steven Moreland6b47c542017-03-21 12:52:16 -070024import android.hidl.manager.V1_0.IServiceManager;
Makoto Onuki99029542018-08-27 17:23:09 -070025import android.os.Binder;
26import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.Debug;
28import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080029import android.os.IPowerManager;
John Michelau11641522013-03-18 18:28:23 -050030import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.Process;
Makoto Onuki99029542018-08-27 17:23:09 -070032import android.os.RemoteException;
Suchi Amalapurapu6ffce2e2010-03-08 14:48:40 -080033import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.SystemClock;
Makoto Onuki99029542018-08-27 17:23:09 -070035import android.system.ErrnoException;
36import android.system.Os;
37import android.system.OsConstants;
38import android.system.StructRlimit;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.util.EventLog;
Dan Egnor9bdc94b2010-03-04 14:20:31 -080040import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080041import android.util.Slog;
Simon MacMullendfc56be2020-01-07 18:31:43 +000042import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Simon MacMullendfc56be2020-01-07 18:31:43 +000044import com.android.internal.os.ProcessCpuTracker;
Makoto Onuki99029542018-08-27 17:23:09 -070045import com.android.internal.os.ZygoteConnectionConstants;
Muhammad Qureshie2b24322020-01-28 10:54:17 -080046import com.android.internal.util.FrameworkStatsLog;
Makoto Onuki99029542018-08-27 17:23:09 -070047import com.android.server.am.ActivityManagerService;
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -080048import com.android.server.wm.SurfaceAnimationThread;
Makoto Onuki99029542018-08-27 17:23:09 -070049
Dan Egnor9bdc94b2010-03-04 14:20:31 -080050import java.io.File;
Colin Cross5df1d872012-11-29 11:42:11 -080051import java.io.FileWriter;
52import java.io.IOException;
Simon MacMullen46755e22020-02-04 14:03:37 +000053import java.io.StringWriter;
Nandana Dutt6647ef52018-07-12 17:02:57 +010054import java.nio.charset.StandardCharsets;
55import java.nio.file.Files;
56import java.nio.file.Path;
57import java.nio.file.Paths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.util.ArrayList;
Steven Moreland6b47c542017-03-21 12:52:16 -070059import java.util.Arrays;
Narayan Kamatha0a28082017-07-31 15:58:59 +010060import java.util.Collections;
Steven Moreland6b47c542017-03-21 12:52:16 -070061import java.util.HashSet;
62import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64/** This class calls its monitor every minute. Killing this process if they don't return **/
65public class Watchdog extends Thread {
66 static final String TAG = "Watchdog";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Makoto Onukie276b442018-08-30 09:38:44 -070068 /** Debug flag. */
Makoto Onuki729c41e2019-05-30 09:47:10 -070069 public static final boolean DEBUG = false;
Makoto Onuki99029542018-08-27 17:23:09 -070070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 // Set this to true to use debug default values.
Zimc0c15412020-03-04 07:29:07 +000072 private static final boolean DB = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073
Andreas Gampe032a9292017-07-21 11:41:00 -070074 // Note 1: Do not lower this value below thirty seconds without tightening the invoke-with
75 // timeout in com.android.internal.os.ZygoteConnection, or wrapped applications
76 // can trigger the watchdog.
77 // Note 2: The debug value is already below the wait time in ZygoteConnection. Wrapped
78 // applications may not work with a debug build. CTS will fail.
Zimc0c15412020-03-04 07:29:07 +000079 private static final long DEFAULT_TIMEOUT = DB ? 10 * 1000 : 60 * 1000;
80 private static final long CHECK_INTERVAL = DEFAULT_TIMEOUT / 2;
Christopher Tatee6f81cf2013-10-23 17:28:27 -070081
82 // These are temporally ordered: larger values as lateness increases
Zimc0c15412020-03-04 07:29:07 +000083 private static final int COMPLETED = 0;
84 private static final int WAITING = 1;
85 private static final int WAITED_HALF = 2;
86 private static final int OVERDUE = 3;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Igor Murashkin44d04aa2013-10-23 10:56:02 -070088 // Which native processes to dump into dropbox's stack traces
89 public static final String[] NATIVE_STACKS_OF_INTEREST = new String[] {
Andy Hung3a64ecb2016-03-09 13:55:58 -080090 "/system/bin/audioserver",
Andy Hung689574a2016-04-13 19:27:43 -070091 "/system/bin/cameraserver",
92 "/system/bin/drmserver",
93 "/system/bin/mediadrmserver",
Dianne Hackbornf72467a2012-06-08 17:23:59 -070094 "/system/bin/mediaserver",
Mike Yuc9855952019-11-19 14:36:02 +080095 "/system/bin/netd",
Dianne Hackbornf72467a2012-06-08 17:23:59 -070096 "/system/bin/sdcard",
Eric Laurent05d4e352016-03-14 18:49:08 -070097 "/system/bin/surfaceflinger",
Jeff Sharkey3e40afb2019-01-02 09:21:24 -070098 "/system/bin/vold",
Andy Hung689574a2016-04-13 19:27:43 -070099 "media.extractor", // system/bin/mediaextractor
Andy Hungb1c4c932018-01-04 12:05:43 -0800100 "media.metrics", // system/bin/mediametrics
Chong Zhange55e6e02017-06-02 10:52:04 -0700101 "media.codec", // vendor/bin/hw/android.hardware.media.omx@1.0-service
Chong Zhangf1f76322019-03-27 16:41:51 -0700102 "media.swcodec", // /apex/com.android.media.swcodec/bin/mediaswcodec
Andreas Gampecf9e79b2016-05-11 18:41:25 -0700103 "com.android.bluetooth", // Bluetooth service
Howard Roadc510f2020-02-20 04:57:00 +0000104 "/apex/com.android.os.statsd/bin/statsd", // Stats daemon
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700105 };
106
Steven Moreland6b47c542017-03-21 12:52:16 -0700107 public static final List<String> HAL_INTERFACES_OF_INTEREST = Arrays.asList(
Hui Yu7e65c3f2019-04-16 17:03:16 -0700108 "android.hardware.audio@2.0::IDevicesFactory",
109 "android.hardware.audio@4.0::IDevicesFactory",
Mikhail Naganov708ea282019-07-10 10:16:32 -0700110 "android.hardware.audio@5.0::IDevicesFactory",
Kevin Rocarddf6e4d52019-09-30 19:54:55 +0100111 "android.hardware.audio@6.0::IDevicesFactory",
Kevin Chyn7733b752019-06-19 13:48:18 -0700112 "android.hardware.biometrics.face@1.0::IBiometricsFace",
Kris Chenf8dc4832020-03-03 15:45:59 +0800113 "android.hardware.biometrics.fingerprint@2.1::IBiometricsFingerprint",
Hui Yu7e65c3f2019-04-16 17:03:16 -0700114 "android.hardware.bluetooth@1.0::IBluetoothHci",
115 "android.hardware.camera.provider@2.4::ICameraProvider",
Sasha Kuznetsov11ed7d92020-02-28 12:55:09 -0800116 "android.hardware.gnss@1.0::IGnss",
Hui Yu7e65c3f2019-04-16 17:03:16 -0700117 "android.hardware.graphics.allocator@2.0::IAllocator",
118 "android.hardware.graphics.composer@2.1::IComposer",
119 "android.hardware.health@2.0::IHealth",
Chong Zhangf1f76322019-03-27 16:41:51 -0700120 "android.hardware.media.c2@1.0::IComponentStore",
Hui Yu7e65c3f2019-04-16 17:03:16 -0700121 "android.hardware.media.omx@1.0::IOmx",
122 "android.hardware.media.omx@1.0::IOmxStore",
Stefano Galarraga219f1e52020-03-18 17:01:42 +0000123 "android.hardware.neuralnetworks@1.0::IDevice",
Benjamin Schwartzf0a81f62019-06-18 17:58:39 -0700124 "android.hardware.power.stats@1.0::IPowerStats",
Hui Yu7e65c3f2019-04-16 17:03:16 -0700125 "android.hardware.sensors@1.0::ISensors",
Kalesh Singh5fe631d2019-07-02 13:51:53 -0700126 "android.hardware.vr@1.0::IVr",
127 "android.system.suspend@1.0::ISystemSuspend"
Steven Moreland6b47c542017-03-21 12:52:16 -0700128 );
129
Zimc0c15412020-03-04 07:29:07 +0000130 private static Watchdog sWatchdog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
132 /* This handler will be used to post message back onto the main thread */
Zimc0c15412020-03-04 07:29:07 +0000133 private final ArrayList<HandlerChecker> mHandlerCheckers = new ArrayList<>();
134 private final HandlerChecker mMonitorChecker;
135 private ActivityManagerService mActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
Zimc0c15412020-03-04 07:29:07 +0000137 private IActivityController mController;
138 private boolean mAllowRestart = true;
139 private final OpenFdMonitor mOpenFdMonitor;
140 private final List<Integer> mInterestingJavaPids = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 /**
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700143 * Used for checking status of handle threads and scheduling monitor callbacks.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 */
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700145 public final class HandlerChecker implements Runnable {
146 private final Handler mHandler;
147 private final String mName;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700148 private final long mWaitMax;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700149 private final ArrayList<Monitor> mMonitors = new ArrayList<Monitor>();
Zimuzob065b152019-04-16 13:33:01 +0100150 private final ArrayList<Monitor> mMonitorQueue = new ArrayList<Monitor>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700151 private boolean mCompleted;
152 private Monitor mCurrentMonitor;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700153 private long mStartTime;
Zimuzo2a050392019-05-09 12:51:33 +0100154 private int mPauseCount;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700155
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700156 HandlerChecker(Handler handler, String name, long waitMaxMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700157 mHandler = handler;
158 mName = name;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700159 mWaitMax = waitMaxMillis;
160 mCompleted = true;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700161 }
162
Makoto Onukie276b442018-08-30 09:38:44 -0700163 void addMonitorLocked(Monitor monitor) {
Zimuzob065b152019-04-16 13:33:01 +0100164 // We don't want to update mMonitors when the Handler is in the middle of checking
165 // all monitors. We will update mMonitors on the next schedule if it is safe
166 mMonitorQueue.add(monitor);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700167 }
168
169 public void scheduleCheckLocked() {
Zimuzob065b152019-04-16 13:33:01 +0100170 if (mCompleted) {
171 // Safe to update monitors in queue, Handler is not in the middle of work
172 mMonitors.addAll(mMonitorQueue);
173 mMonitorQueue.clear();
174 }
Zimuzo2a050392019-05-09 12:51:33 +0100175 if ((mMonitors.size() == 0 && mHandler.getLooper().getQueue().isPolling())
176 || (mPauseCount > 0)) {
177 // Don't schedule until after resume OR
Jeff Brown6c7b41a2015-02-26 14:43:53 -0800178 // If the target looper has recently been polling, then
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700179 // there is no reason to enqueue our checker on it since that
180 // is as good as it not being deadlocked. This avoid having
Zimuzo2a050392019-05-09 12:51:33 +0100181 // to do a context switch to check the thread. Note that we
182 // only do this if we have no monitors since those would need to
183 // be executed at this point.
Dianne Hackbornefa92b22013-05-03 14:11:43 -0700184 mCompleted = true;
185 return;
186 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700187 if (!mCompleted) {
188 // we already have a check in flight, so no need
189 return;
190 }
191
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700192 mCompleted = false;
193 mCurrentMonitor = null;
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700194 mStartTime = SystemClock.uptimeMillis();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700195 mHandler.postAtFrontOfQueue(this);
196 }
197
Makoto Onukie276b442018-08-30 09:38:44 -0700198 boolean isOverdueLocked() {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700199 return (!mCompleted) && (SystemClock.uptimeMillis() > mStartTime + mWaitMax);
200 }
201
202 public int getCompletionStateLocked() {
203 if (mCompleted) {
204 return COMPLETED;
205 } else {
206 long latency = SystemClock.uptimeMillis() - mStartTime;
207 if (latency < mWaitMax/2) {
208 return WAITING;
209 } else if (latency < mWaitMax) {
210 return WAITED_HALF;
211 }
212 }
213 return OVERDUE;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700214 }
215
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700216 public Thread getThread() {
217 return mHandler.getLooper().getThread();
218 }
219
220 public String getName() {
221 return mName;
222 }
223
Makoto Onukie276b442018-08-30 09:38:44 -0700224 String describeBlockedStateLocked() {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700225 if (mCurrentMonitor == null) {
226 return "Blocked in handler on " + mName + " (" + getThread().getName() + ")";
227 } else {
228 return "Blocked in monitor " + mCurrentMonitor.getClass().getName()
229 + " on " + mName + " (" + getThread().getName() + ")";
230 }
John Michelau11641522013-03-18 18:28:23 -0500231 }
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 @Override
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700234 public void run() {
Zimuzob065b152019-04-16 13:33:01 +0100235 // Once we get here, we ensure that mMonitors does not change even if we call
236 // #addMonitorLocked because we first add the new monitors to mMonitorQueue and
237 // move them to mMonitors on the next schedule when mCompleted is true, at which
238 // point we have completed execution of this method.
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700239 final int size = mMonitors.size();
240 for (int i = 0 ; i < size ; i++) {
241 synchronized (Watchdog.this) {
242 mCurrentMonitor = mMonitors.get(i);
243 }
244 mCurrentMonitor.monitor();
245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700247 synchronized (Watchdog.this) {
248 mCompleted = true;
249 mCurrentMonitor = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 }
251 }
Zimuzo2a050392019-05-09 12:51:33 +0100252
253 /** Pause the HandlerChecker. */
254 public void pauseLocked(String reason) {
255 mPauseCount++;
256 // Mark as completed, because there's a chance we called this after the watchog
257 // thread loop called Object#wait after 'WAITED_HALF'. In that case we want to ensure
258 // the next call to #getCompletionStateLocked for this checker returns 'COMPLETED'
259 mCompleted = true;
260 Slog.i(TAG, "Pausing HandlerChecker: " + mName + " for reason: "
261 + reason + ". Pause count: " + mPauseCount);
262 }
263
264 /** Resume the HandlerChecker from the last {@link #pauseLocked}. */
265 public void resumeLocked(String reason) {
266 if (mPauseCount > 0) {
267 mPauseCount--;
268 Slog.i(TAG, "Resuming HandlerChecker: " + mName + " for reason: "
269 + reason + ". Pause count: " + mPauseCount);
270 } else {
271 Slog.wtf(TAG, "Already resumed HandlerChecker: " + mName);
272 }
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 final class RebootRequestReceiver extends BroadcastReceiver {
277 @Override
278 public void onReceive(Context c, Intent intent) {
Dianne Hackbornf6438b12013-05-09 18:53:48 -0700279 if (intent.getIntExtra("nowait", 0) != 0) {
280 rebootSystem("Received ACTION_REBOOT broadcast");
281 return;
282 }
283 Slog.w(TAG, "Unsupported ACTION_REBOOT broadcast: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285 }
286
Wale Ogunwale517daec2015-04-15 10:27:24 -0700287 /** Monitor for checking the availability of binder threads. The monitor will block until
288 * there is a binder thread available to process in coming IPCs to make sure other processes
289 * can still communicate with the service.
290 */
291 private static final class BinderThreadMonitor implements Watchdog.Monitor {
292 @Override
293 public void monitor() {
294 Binder.blockUntilThreadAvailable();
295 }
296 }
297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 public interface Monitor {
299 void monitor();
300 }
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 public static Watchdog getInstance() {
303 if (sWatchdog == null) {
304 sWatchdog = new Watchdog();
305 }
306
307 return sWatchdog;
308 }
309
310 private Watchdog() {
311 super("watchdog");
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700312 // Initialize handler checkers for each common thread we want to check. Note
313 // that we are not currently checking the background thread, since it can
314 // potentially hold longer running operations with no guarantees about the timeliness
315 // of operations there.
316
317 // The shared foreground thread is the main checker. It is where we
318 // will also dispatch monitor checks and do other work.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700319 mMonitorChecker = new HandlerChecker(FgThread.getHandler(),
320 "foreground thread", DEFAULT_TIMEOUT);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700321 mHandlerCheckers.add(mMonitorChecker);
322 // Add checker for main thread. We only do a quick check since there
323 // can be UI running on the thread.
324 mHandlerCheckers.add(new HandlerChecker(new Handler(Looper.getMainLooper()),
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700325 "main thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700326 // Add checker for shared UI thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700327 mHandlerCheckers.add(new HandlerChecker(UiThread.getHandler(),
328 "ui thread", DEFAULT_TIMEOUT));
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700329 // And also check IO thread.
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700330 mHandlerCheckers.add(new HandlerChecker(IoThread.getHandler(),
331 "i/o thread", DEFAULT_TIMEOUT));
Jeff Brown4ccb8232014-01-16 22:16:42 -0800332 // And the display thread.
333 mHandlerCheckers.add(new HandlerChecker(DisplayThread.getHandler(),
334 "display thread", DEFAULT_TIMEOUT));
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -0800335 // And the animation thread.
336 mHandlerCheckers.add(new HandlerChecker(AnimationThread.getHandler(),
337 "animation thread", DEFAULT_TIMEOUT));
338 // And the surface animation thread.
339 mHandlerCheckers.add(new HandlerChecker(SurfaceAnimationThread.getHandler(),
340 "surface animation thread", DEFAULT_TIMEOUT));
Wale Ogunwale517daec2015-04-15 10:27:24 -0700341
342 // Initialize monitor for Binder threads.
343 addMonitor(new BinderThreadMonitor());
Andreas Gampe032a9292017-07-21 11:41:00 -0700344
Narayan Kamatha0a28082017-07-31 15:58:59 +0100345 mOpenFdMonitor = OpenFdMonitor.create();
346
Zimc0c15412020-03-04 07:29:07 +0000347 mInterestingJavaPids.add(Process.myPid());
348
Andreas Gampe032a9292017-07-21 11:41:00 -0700349 // See the notes on DEFAULT_TIMEOUT.
350 assert DB ||
351 DEFAULT_TIMEOUT > ZygoteConnectionConstants.WRAPPED_PID_TIMEOUT_MILLIS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
353
Zimuzob065b152019-04-16 13:33:01 +0100354 /**
355 * Registers a {@link BroadcastReceiver} to listen to reboot broadcasts and trigger reboot.
356 * Should be called during boot after the ActivityManagerService is up and registered
357 * as a system service so it can handle registration of a {@link BroadcastReceiver}.
358 */
Adam Lesinski182f73f2013-12-05 16:48:06 -0800359 public void init(Context context, ActivityManagerService activity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 mActivity = activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 context.registerReceiver(new RebootRequestReceiver(),
362 new IntentFilter(Intent.ACTION_REBOOT),
363 android.Manifest.permission.REBOOT, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
365
Zimc0c15412020-03-04 07:29:07 +0000366 private static boolean isInterestingJavaProcess(String processName) {
Zim9ce0a7c2020-03-04 07:34:27 +0000367 return processName.equals(StorageManagerService.sMediaStoreAuthorityProcessName)
368 || processName.equals("com.android.phone");
Zimc0c15412020-03-04 07:29:07 +0000369 }
370
371 /**
372 * Notifies the watchdog when a Java process with {@code pid} is started.
373 * This process may have its stack trace dumped during an ANR.
374 */
375 public void processStarted(String processName, int pid) {
376 if (isInterestingJavaProcess(processName)) {
377 Slog.i(TAG, "Interesting Java process " + processName + " started. Pid " + pid);
378 synchronized (this) {
379 mInterestingJavaPids.add(pid);
380 }
381 }
382 }
383
384 /**
385 * Notifies the watchdog when a Java process with {@code pid} dies.
386 */
387 public void processDied(String processName, int pid) {
388 if (isInterestingJavaProcess(processName)) {
389 Slog.i(TAG, "Interesting Java process " + processName + " died. Pid " + pid);
390 synchronized (this) {
391 mInterestingJavaPids.remove(Integer.valueOf(pid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 }
393 }
394 }
395
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700396 public void setActivityController(IActivityController controller) {
397 synchronized (this) {
398 mController = controller;
399 }
400 }
401
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700402 public void setAllowRestart(boolean allowRestart) {
403 synchronized (this) {
404 mAllowRestart = allowRestart;
405 }
406 }
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 public void addMonitor(Monitor monitor) {
409 synchronized (this) {
Makoto Onukie276b442018-08-30 09:38:44 -0700410 mMonitorChecker.addMonitorLocked(monitor);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700411 }
412 }
413
Jeff Brown6f357d32014-01-15 20:40:55 -0800414 public void addThread(Handler thread) {
415 addThread(thread, DEFAULT_TIMEOUT);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700416 }
417
Jeff Brown6f357d32014-01-15 20:40:55 -0800418 public void addThread(Handler thread, long timeoutMillis) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700419 synchronized (this) {
Jeff Brown6f357d32014-01-15 20:40:55 -0800420 final String name = thread.getLooper().getThread().getName();
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700421 mHandlerCheckers.add(new HandlerChecker(thread, name, timeoutMillis));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423 }
424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 /**
Zimuzo2a050392019-05-09 12:51:33 +0100426 * Pauses Watchdog action for the currently running thread. Useful before executing long running
427 * operations that could falsely trigger the watchdog. Each call to this will require a matching
428 * call to {@link #resumeWatchingCurrentThread}.
429 *
430 * <p>If the current thread has not been added to the Watchdog, this call is a no-op.
431 *
432 * <p>If the Watchdog is already paused for the current thread, this call adds
433 * adds another pause and will require an additional {@link #resumeCurrentThread} to resume.
434 *
435 * <p>Note: Use with care, as any deadlocks on the current thread will be undetected until all
436 * pauses have been resumed.
437 */
438 public void pauseWatchingCurrentThread(String reason) {
439 synchronized (this) {
440 for (HandlerChecker hc : mHandlerCheckers) {
441 if (Thread.currentThread().equals(hc.getThread())) {
442 hc.pauseLocked(reason);
443 }
444 }
445 }
446 }
447
448 /**
449 * Resumes the last pause from {@link #pauseWatchingCurrentThread} for the currently running
450 * thread.
451 *
452 * <p>If the current thread has not been added to the Watchdog, this call is a no-op.
453 *
454 * <p>If the Watchdog action for the current thread is already resumed, this call logs a wtf.
455 *
456 * <p>If all pauses have been resumed, the Watchdog action is finally resumed, otherwise,
457 * the Watchdog action for the current thread remains paused until resume is called at least
458 * as many times as the calls to pause.
459 */
460 public void resumeWatchingCurrentThread(String reason) {
461 synchronized (this) {
462 for (HandlerChecker hc : mHandlerCheckers) {
463 if (Thread.currentThread().equals(hc.getThread())) {
464 hc.resumeLocked(reason);
465 }
466 }
467 }
468 }
469
470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 * Perform a full reboot of the system.
472 */
473 void rebootSystem(String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800474 Slog.i(TAG, "Rebooting system because: " + reason);
Jeff Brown6f357d32014-01-15 20:40:55 -0800475 IPowerManager pms = (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE);
476 try {
477 pms.reboot(false, reason, false);
478 } catch (RemoteException ex) {
479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 }
481
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700482 private int evaluateCheckerCompletionLocked() {
483 int state = COMPLETED;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700484 for (int i=0; i<mHandlerCheckers.size(); i++) {
485 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700486 state = Math.max(state, hc.getCompletionStateLocked());
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700487 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700488 return state;
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700489 }
490
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700491 private ArrayList<HandlerChecker> getBlockedCheckersLocked() {
492 ArrayList<HandlerChecker> checkers = new ArrayList<HandlerChecker>();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700493 for (int i=0; i<mHandlerCheckers.size(); i++) {
494 HandlerChecker hc = mHandlerCheckers.get(i);
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700495 if (hc.isOverdueLocked()) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700496 checkers.add(hc);
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700497 }
498 }
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700499 return checkers;
500 }
501
Narayan Kamatha0a28082017-07-31 15:58:59 +0100502 private String describeCheckersLocked(List<HandlerChecker> checkers) {
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700503 StringBuilder builder = new StringBuilder(128);
504 for (int i=0; i<checkers.size(); i++) {
505 if (builder.length() > 0) {
506 builder.append(", ");
507 }
508 builder.append(checkers.get(i).describeBlockedStateLocked());
509 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700510 return builder.toString();
511 }
512
Hui Yu7e65c3f2019-04-16 17:03:16 -0700513 private static ArrayList<Integer> getInterestingHalPids() {
Steven Moreland6b47c542017-03-21 12:52:16 -0700514 try {
515 IServiceManager serviceManager = IServiceManager.getService();
516 ArrayList<IServiceManager.InstanceDebugInfo> dump =
517 serviceManager.debugDump();
518 HashSet<Integer> pids = new HashSet<>();
519 for (IServiceManager.InstanceDebugInfo info : dump) {
520 if (info.pid == IServiceManager.PidConstant.NO_PID) {
521 continue;
522 }
523
524 if (!HAL_INTERFACES_OF_INTEREST.contains(info.interfaceName)) {
525 continue;
526 }
527
528 pids.add(info.pid);
529 }
530 return new ArrayList<Integer>(pids);
531 } catch (RemoteException e) {
532 return new ArrayList<Integer>();
533 }
534 }
535
Hui Yu7e65c3f2019-04-16 17:03:16 -0700536 static ArrayList<Integer> getInterestingNativePids() {
Steven Moreland6b47c542017-03-21 12:52:16 -0700537 ArrayList<Integer> pids = getInterestingHalPids();
538
539 int[] nativePids = Process.getPidsForCommands(NATIVE_STACKS_OF_INTEREST);
540 if (nativePids != null) {
541 pids.ensureCapacity(pids.size() + nativePids.length);
542 for (int i : nativePids) {
543 pids.add(i);
544 }
545 }
546
547 return pids;
548 }
549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 @Override
551 public void run() {
Christopher Tate6ee412d2010-05-28 12:01:56 -0700552 boolean waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 while (true) {
Narayan Kamatha0a28082017-07-31 15:58:59 +0100554 final List<HandlerChecker> blockedCheckers;
Jeff Brown7dd2d192013-09-06 15:05:23 -0700555 final String subject;
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700556 final boolean allowRestart;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700557 int debuggerWasConnected = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 synchronized (this) {
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700559 long timeout = CHECK_INTERVAL;
560 // Make sure we (re)spin the checkers that have become idle within
561 // this wait-and-check interval
562 for (int i=0; i<mHandlerCheckers.size(); i++) {
563 HandlerChecker hc = mHandlerCheckers.get(i);
564 hc.scheduleCheckLocked();
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700567 if (debuggerWasConnected > 0) {
568 debuggerWasConnected--;
569 }
570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 // NOTE: We use uptimeMillis() here because we do not want to increment the time we
572 // wait while asleep. If the device is asleep then the thing that we are waiting
Christopher Tate6ee412d2010-05-28 12:01:56 -0700573 // 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 -0800574 // positive on when to kill things.
575 long start = SystemClock.uptimeMillis();
Michael Wright8fa56f62013-04-01 16:36:05 -0700576 while (timeout > 0) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700577 if (Debug.isDebuggerConnected()) {
578 debuggerWasConnected = 2;
579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 try {
Michael Wright8fa56f62013-04-01 16:36:05 -0700581 wait(timeout);
Zimuzob065b152019-04-16 13:33:01 +0100582 // Note: mHandlerCheckers and mMonitorChecker may have changed after waiting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 } catch (InterruptedException e) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800584 Log.wtf(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700586 if (Debug.isDebuggerConnected()) {
587 debuggerWasConnected = 2;
588 }
Christopher Tatee6f81cf2013-10-23 17:28:27 -0700589 timeout = CHECK_INTERVAL - (SystemClock.uptimeMillis() - start);
Dan Egnor9bdc94b2010-03-04 14:20:31 -0800590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591
Narayan Kamatha0a28082017-07-31 15:58:59 +0100592 boolean fdLimitTriggered = false;
593 if (mOpenFdMonitor != null) {
594 fdLimitTriggered = mOpenFdMonitor.monitor();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
Michael Wright8fa56f62013-04-01 16:36:05 -0700596
Narayan Kamatha0a28082017-07-31 15:58:59 +0100597 if (!fdLimitTriggered) {
598 final int waitState = evaluateCheckerCompletionLocked();
599 if (waitState == COMPLETED) {
600 // The monitors have returned; reset
601 waitedHalf = false;
602 continue;
603 } else if (waitState == WAITING) {
604 // still waiting but within their configured intervals; back off and recheck
605 continue;
606 } else if (waitState == WAITED_HALF) {
607 if (!waitedHalf) {
Makoto Onuki729c41e2019-05-30 09:47:10 -0700608 Slog.i(TAG, "WAITED_HALF");
Narayan Kamatha0a28082017-07-31 15:58:59 +0100609 // We've waited half the deadlock-detection interval. Pull a stack
610 // trace and wait another half.
Zimc0c15412020-03-04 07:29:07 +0000611 ArrayList<Integer> pids = new ArrayList<>(mInterestingJavaPids);
Makoto Onukie276b442018-08-30 09:38:44 -0700612 ActivityManagerService.dumpStackTraces(pids, null, null,
Simon MacMullen46755e22020-02-04 14:03:37 +0000613 getInterestingNativePids(), null);
Narayan Kamatha0a28082017-07-31 15:58:59 +0100614 waitedHalf = true;
615 }
616 continue;
617 }
618
619 // something is overdue!
620 blockedCheckers = getBlockedCheckersLocked();
621 subject = describeCheckersLocked(blockedCheckers);
622 } else {
623 blockedCheckers = Collections.emptyList();
624 subject = "Open FD high water mark reached";
625 }
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700626 allowRestart = mAllowRestart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628
629 // If we got here, that means that the system is most likely hung.
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700630 // First collect stack traces from all threads of the system process.
631 // Then kill this process so that the system will restart.
Jeff Brown7dd2d192013-09-06 15:05:23 -0700632 EventLog.writeEvent(EventLogTags.WATCHDOG, subject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633
Zimc0c15412020-03-04 07:29:07 +0000634 ArrayList<Integer> pids = new ArrayList<>(mInterestingJavaPids);
Makoto Onukie276b442018-08-30 09:38:44 -0700635
Simon MacMullendfc56be2020-01-07 18:31:43 +0000636 long anrTime = SystemClock.uptimeMillis();
Simon MacMullen30f04a72020-02-05 10:57:40 +0000637 StringBuilder report = new StringBuilder();
638 report.append(MemoryPressureUtil.currentPsiState());
Simon MacMullendfc56be2020-01-07 18:31:43 +0000639 ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(false);
Simon MacMullen46755e22020-02-04 14:03:37 +0000640 StringWriter tracesFileException = new StringWriter();
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800641 final File stack = ActivityManagerService.dumpStackTraces(
Simon MacMullen46755e22020-02-04 14:03:37 +0000642 pids, processCpuTracker, new SparseArray<>(), getInterestingNativePids(),
643 tracesFileException);
Dan Egnor4bded072010-03-11 22:00:47 -0800644
645 // Give some extra time to make sure the stack traces get written.
646 // The system's been hanging for a minute, another second or two won't hurt much.
Makoto Onukie276b442018-08-30 09:38:44 -0700647 SystemClock.sleep(5000);
Dan Egnor4bded072010-03-11 22:00:47 -0800648
Simon MacMullendfc56be2020-01-07 18:31:43 +0000649 processCpuTracker.update();
Simon MacMullen46755e22020-02-04 14:03:37 +0000650 report.append(processCpuTracker.printCurrentState(anrTime));
651 report.append(tracesFileException.getBuffer());
Simon MacMullendfc56be2020-01-07 18:31:43 +0000652
Guang Zhu0620c452014-10-29 14:31:48 -0700653 // Trigger the kernel to dump all blocked threads, and backtraces on all CPUs to the kernel log
654 doSysRq('w');
655 doSysRq('l');
Colin Cross5df1d872012-11-29 11:42:11 -0800656
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800657 // Try to add the error to the dropbox, but assuming that the ActivityManager
658 // itself may be deadlocked. (which has happened, causing this statement to
659 // deadlock and the watchdog as a whole to be ineffective)
660 Thread dropboxThread = new Thread("watchdogWriteToDropbox") {
661 public void run() {
Amith Yamasanifa7c36892019-05-15 17:32:27 -0700662 // If a watched thread hangs before init() is called, we don't have a
663 // valid mActivity. So we can't log the error to dropbox.
664 if (mActivity != null) {
665 mActivity.addErrorToDropBox(
666 "watchdog", null, "system_server", null, null, null,
Simon MacMullen46755e22020-02-04 14:03:37 +0000667 subject, report.toString(), stack, null);
Amith Yamasanifa7c36892019-05-15 17:32:27 -0700668 }
Muhammad Qureshie2b24322020-01-28 10:54:17 -0800669 FrameworkStatsLog.write(FrameworkStatsLog.SYSTEM_SERVER_WATCHDOG_OCCURRED,
670 subject);
Brad Fitzpatrick9765c722011-01-14 11:28:22 -0800671 }
672 };
673 dropboxThread.start();
674 try {
675 dropboxThread.join(2000); // wait up to 2 seconds for it to return.
676 } catch (InterruptedException ignored) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700678 IActivityController controller;
679 synchronized (this) {
680 controller = mController;
681 }
682 if (controller != null) {
683 Slog.i(TAG, "Reporting stuck state to activity controller");
684 try {
685 Binder.setDumpDisabled("Service dumps disabled due to hung system process.");
686 // 1 = keep waiting, -1 = kill system
Jeff Brown7dd2d192013-09-06 15:05:23 -0700687 int res = controller.systemNotResponding(subject);
Dianne Hackborn5b88a2f2013-05-03 16:25:11 -0700688 if (res >= 0) {
689 Slog.i(TAG, "Activity controller requested to coninue to wait");
690 waitedHalf = false;
691 continue;
692 }
693 } catch (RemoteException e) {
694 }
695 }
696
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700697 // Only kill the process if the debugger is not attached.
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700698 if (Debug.isDebuggerConnected()) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700699 debuggerWasConnected = 2;
700 }
701 if (debuggerWasConnected >= 2) {
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700702 Slog.w(TAG, "Debugger connected: Watchdog is *not* killing the system process");
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700703 } else if (debuggerWasConnected > 0) {
704 Slog.w(TAG, "Debugger was connected: Watchdog is *not* killing the system process");
Dianne Hackborn8bd64df2013-05-06 16:07:26 -0700705 } else if (!allowRestart) {
706 Slog.w(TAG, "Restart not allowed: Watchdog is *not* killing the system process");
707 } else {
Jeff Brown7dd2d192013-09-06 15:05:23 -0700708 Slog.w(TAG, "*** WATCHDOG KILLING SYSTEM PROCESS: " + subject);
Andreas Gampe544416e2018-01-26 11:39:46 -0800709 WatchdogDiagnostics.diagnoseCheckers(blockedCheckers);
Dianne Hackbornfa012b32013-05-10 15:23:28 -0700710 Slog.w(TAG, "*** GOODBYE!");
Jean-Baptiste Queru784827b2012-09-04 13:35:12 -0700711 Process.killProcess(Process.myPid());
712 System.exit(10);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
Christopher Tate6ee412d2010-05-28 12:01:56 -0700714
715 waitedHalf = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717 }
Christopher Tateecaa7b42010-06-04 14:55:02 -0700718
Guang Zhu0620c452014-10-29 14:31:48 -0700719 private void doSysRq(char c) {
720 try {
721 FileWriter sysrq_trigger = new FileWriter("/proc/sysrq-trigger");
722 sysrq_trigger.write(c);
723 sysrq_trigger.close();
724 } catch (IOException e) {
725 Slog.w(TAG, "Failed to write to /proc/sysrq-trigger", e);
726 }
727 }
728
Narayan Kamatha0a28082017-07-31 15:58:59 +0100729 public static final class OpenFdMonitor {
730 /**
731 * Number of FDs below the soft limit that we trigger a runtime restart at. This was
732 * chosen arbitrarily, but will need to be at least 6 in order to have a sufficient number
733 * of FDs in reserve to complete a dump.
734 */
735 private static final int FD_HIGH_WATER_MARK = 12;
736
737 private final File mDumpDir;
738 private final File mFdHighWaterMark;
739
740 public static OpenFdMonitor create() {
741 // Only run the FD monitor on debuggable builds (such as userdebug and eng builds).
742 if (!Build.IS_DEBUGGABLE) {
743 return null;
744 }
745
Narayan Kamatha0a28082017-07-31 15:58:59 +0100746 final StructRlimit rlimit;
747 try {
748 rlimit = android.system.Os.getrlimit(OsConstants.RLIMIT_NOFILE);
749 } catch (ErrnoException errno) {
750 Slog.w(TAG, "Error thrown from getrlimit(RLIMIT_NOFILE)", errno);
751 return null;
752 }
753
754 // The assumption we're making here is that FD numbers are allocated (more or less)
755 // sequentially, which is currently (and historically) true since open is currently
756 // specified to always return the lowest-numbered non-open file descriptor for the
757 // current process.
758 //
759 // We do this to avoid having to enumerate the contents of /proc/self/fd in order to
760 // count the number of descriptors open in the process.
761 final File fdThreshold = new File("/proc/self/fd/" + (rlimit.rlim_cur - FD_HIGH_WATER_MARK));
Elliott Hughes4e4caa72018-03-23 11:06:36 -0700762 return new OpenFdMonitor(new File("/data/anr"), fdThreshold);
Narayan Kamatha0a28082017-07-31 15:58:59 +0100763 }
764
765 OpenFdMonitor(File dumpDir, File fdThreshold) {
766 mDumpDir = dumpDir;
767 mFdHighWaterMark = fdThreshold;
768 }
769
Nandana Dutt6647ef52018-07-12 17:02:57 +0100770 /**
771 * Dumps open file descriptors and their full paths to a temporary file in {@code mDumpDir}.
772 */
Narayan Kamatha0a28082017-07-31 15:58:59 +0100773 private void dumpOpenDescriptors() {
Nandana Dutt6647ef52018-07-12 17:02:57 +0100774 // We cannot exec lsof to get more info about open file descriptors because a newly
775 // forked process will not have the permissions to readlink. Instead list all open
776 // descriptors from /proc/pid/fd and resolve them.
777 List<String> dumpInfo = new ArrayList<>();
778 String fdDirPath = String.format("/proc/%d/fd/", Process.myPid());
779 File[] fds = new File(fdDirPath).listFiles();
780 if (fds == null) {
781 dumpInfo.add("Unable to list " + fdDirPath);
782 } else {
783 for (File f : fds) {
784 String fdSymLink = f.getAbsolutePath();
785 String resolvedPath = "";
786 try {
787 resolvedPath = Os.readlink(fdSymLink);
788 } catch (ErrnoException ex) {
789 resolvedPath = ex.getMessage();
790 }
791 dumpInfo.add(fdSymLink + "\t" + resolvedPath);
792 }
793 }
794
795 // Dump the fds & paths to a temp file.
Narayan Kamatha0a28082017-07-31 15:58:59 +0100796 try {
797 File dumpFile = File.createTempFile("anr_fd_", "", mDumpDir);
Nandana Dutt6647ef52018-07-12 17:02:57 +0100798 Path out = Paths.get(dumpFile.getAbsolutePath());
799 Files.write(out, dumpInfo, StandardCharsets.UTF_8);
800 } catch (IOException ex) {
801 Slog.w(TAG, "Unable to write open descriptors to file: " + ex);
Narayan Kamatha0a28082017-07-31 15:58:59 +0100802 }
803 }
804
805 /**
806 * @return {@code true} if the high water mark was breached and a dump was written,
807 * {@code false} otherwise.
808 */
809 public boolean monitor() {
810 if (mFdHighWaterMark.exists()) {
811 dumpOpenDescriptors();
812 return true;
813 }
814
815 return false;
816 }
817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818}