blob: 4c75ab21d6f2b377e84bc128ceb221b7ac780cd7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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.am;
18
riddle_hsu6793fc32015-03-31 11:54:14 +080019import static android.app.ActivityManager.PROCESS_STATE_NONEXISTENT;
Yunfan Chen75157d72018-07-27 14:47:21 +090020
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070021import static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
22import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ANR;
Jorim Jaggi589c5ba2019-07-30 16:50:13 +020023import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_OOM_ADJ;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080024import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
25import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070026import static com.android.server.am.ActivityManagerService.MY_PID;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.app.ActivityManager;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070029import android.app.ApplicationErrorReport;
Jing Ji8055a3a2019-12-17 15:55:33 -080030import android.app.ApplicationExitInfo;
31import android.app.ApplicationExitInfo.Reason;
32import android.app.ApplicationExitInfo.SubReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.app.Dialog;
34import android.app.IApplicationThread;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.ComponentName;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070036import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.ApplicationInfo;
Dianne Hackbornf6729fa2020-01-06 13:12:36 -080038import android.content.pm.ProcessInfo;
Amith Yamasania0a30a12019-01-22 11:38:06 -080039import android.content.pm.ServiceInfo;
Zimuzo972e1cd2019-01-28 16:30:01 +000040import android.content.pm.VersionedPackage;
Dianne Hackborne2515ee2011-04-27 18:52:56 -040041import android.content.res.CompatibilityInfo;
Joe Onorato57190282016-04-20 15:37:49 -070042import android.os.Binder;
Yunfan Chen75157d72018-07-27 14:47:21 +090043import android.os.Debug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.IBinder;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070045import android.os.Message;
Dianne Hackborna0c283e2012-02-09 10:47:01 -080046import android.os.Process;
Joe Onorato57190282016-04-20 15:37:49 -070047import android.os.RemoteException;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070048import android.os.SystemClock;
Yusuke Satocf4b2542015-06-17 00:21:28 -070049import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070050import android.os.UserHandle;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070051import android.provider.Settings;
Yang Lu732d6382018-11-05 07:53:12 -080052import android.server.ServerProtoEnums;
Dianne Hackborn390517b2013-05-30 15:03:32 -070053import android.util.ArrayMap;
Yunfan Chen75157d72018-07-27 14:47:21 +090054import android.util.ArraySet;
55import android.util.DebugUtils;
56import android.util.EventLog;
57import android.util.Slog;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070058import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070059import android.util.TimeUtils;
Yi Jin129fc6c2017-09-28 15:48:38 -070060import android.util.proto.ProtoOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
Yunfan Chen585f2932019-01-29 16:04:45 +090062import com.android.internal.annotations.VisibleForTesting;
Yunfan Chen75157d72018-07-27 14:47:21 +090063import com.android.internal.app.procstats.ProcessState;
64import com.android.internal.app.procstats.ProcessStats;
65import com.android.internal.os.BatteryStatsImpl;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070066import com.android.internal.os.ProcessCpuTracker;
Sudheer Shanka87915d62018-11-06 10:57:35 -080067import com.android.internal.os.Zygote;
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -080068import com.android.internal.util.FrameworkStatsLog;
Simon MacMullen30f04a72020-02-05 10:57:40 +000069import com.android.server.MemoryPressureUtil;
Wale Ogunwale59507092018-10-29 09:00:30 -070070import com.android.server.wm.WindowProcessController;
71import com.android.server.wm.WindowProcessListener;
Yunfan Chen75157d72018-07-27 14:47:21 +090072
Wale Ogunwale51cc98a2018-10-15 10:41:05 -070073import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.io.PrintWriter;
Simon MacMullen46755e22020-02-04 14:03:37 +000075import java.io.StringWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import java.util.ArrayList;
Torne (Richard Coles)466cbe42017-05-31 14:09:14 -040077import java.util.Arrays;
Zimuzo972e1cd2019-01-28 16:30:01 +000078import java.util.List;
Charles Chen173ae782019-11-11 20:39:02 +080079import java.util.function.Consumer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81/**
82 * Full information about a particular process that
83 * is currently running.
84 */
Yunfan Chen585f2932019-01-29 16:04:45 +090085class ProcessRecord implements WindowProcessListener {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080086 private static final String TAG = TAG_WITH_CLASS_NAME ? "ProcessRecord" : TAG_AM;
87
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -070088 private final ActivityManagerService mService; // where we came from
Ryan Mitchell03bf28b2020-04-17 15:53:20 -070089 volatile ApplicationInfo info; // all about the first app in the process
Dianne Hackbornf6729fa2020-01-06 13:12:36 -080090 final ProcessInfo processInfo; // if non-null, process-specific manifest info
Dianne Hackborna0c283e2012-02-09 10:47:01 -080091 final boolean isolated; // true if this is a special isolated process
Martijn Coenen01e719b2018-12-05 16:01:38 +010092 final boolean appZygote; // true if this is forked from the app zygote
Dianne Hackborna0c283e2012-02-09 10:47:01 -080093 final int uid; // uid of process; may be different from 'info' if isolated
94 final int userId; // user of process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 final String processName; // name of the process
96 // List of packages running in the process
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070097 final PackageList pkgList = new PackageList();
98 final class PackageList {
99 final ArrayMap<String, ProcessStats.ProcessStateHolder> mPkgList = new ArrayMap<>();
100
101 ProcessStats.ProcessStateHolder put(String key, ProcessStats.ProcessStateHolder value) {
102 mWindowProcessController.addPackage(key);
103 return mPkgList.put(key, value);
104 }
105
106 void clear() {
107 mPkgList.clear();
108 mWindowProcessController.clearPackageList();
109 }
110
111 int size() {
112 return mPkgList.size();
113 }
114
115 String keyAt(int index) {
116 return mPkgList.keyAt(index);
117 }
118
119 public ProcessStats.ProcessStateHolder valueAt(int index) {
120 return mPkgList.valueAt(index);
121 }
122
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700123 ProcessStats.ProcessStateHolder get(String pkgName) {
124 return mPkgList.get(pkgName);
125 }
126
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700127 boolean containsKey(Object key) {
128 return mPkgList.containsKey(key);
129 }
130 }
131
Dianne Hackborne17b4452018-01-10 13:15:40 -0800132 final ProcessList.ProcStateMemTracker procStateMemTracker
133 = new ProcessList.ProcStateMemTracker();
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700134 UidRecord uidRecord; // overall state of process's uid.
Dianne Hackbornfee756f2014-07-16 17:31:10 -0700135 ArraySet<String> pkgDeps; // additional packages we have a dependency on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 IApplicationThread thread; // the actual proc... may be null only if
137 // 'persistent' is true (in which case we
138 // are in the process of launching the app)
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700139 ProcessState baseProcessTracker;
Dianne Hackborn099bc622014-01-22 13:39:16 -0800140 BatteryStatsImpl.Uid.Proc curProcBatteryStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 int pid; // The process of this application; 0 if none
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700142 String procStatFile; // path to /proc/<pid>/stat
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800143 int[] gids; // The gids this process was launched with
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700144 private String mRequiredAbi;// The ABI this process was launched with
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800145 String instructionSet; // The instruction set this process was launched with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 boolean starting; // True if the process is being started
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800147 long lastActivityTime; // For managing the LRU list
Dianne Hackbornf1cca182013-08-01 10:50:28 -0700148 long lastPssTime; // Last time we retrieved PSS data
149 long nextPssTime; // Next time we want to request PSS data
150 long lastStateTime; // Last time setProcState changed
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700151 long initialIdlePss; // Initial memory pss of process for idle maintenance.
152 long lastPss; // Last computed memory pss.
Thierry Strudelf0a44182016-01-07 18:08:04 -0800153 long lastSwapPss; // Last computed SwapPss.
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700154 long lastCachedPss; // Last computed pss when in cached state.
Thierry Strudelf0a44182016-01-07 18:08:04 -0800155 long lastCachedSwapPss; // Last computed SwapPss when in cached state.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 int maxAdj; // Maximum OOM adjustment for this process
Wale Ogunwale31913b52018-10-13 08:29:31 -0700157 private int mCurRawAdj; // Current OOM unlimited adjustment for this process
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 int setRawAdj; // Last set OOM unlimited adjustment for this process
159 int curAdj; // Current OOM adjustment for this process
160 int setAdj; // Last set OOM adjustment for this process
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700161 int verifiedAdj; // The last adjustment that was verified as actually being set
Hui Yu26969322019-08-21 14:56:35 -0700162 int curCapability; // Current capability flags of this process. For example,
163 // PROCESS_CAPABILITY_FOREGROUND_LOCATION is one capability.
164 int setCapability; // Last set capability flags.
Tim Murray0b1c8aa2018-11-20 10:28:38 -0800165 long lastCompactTime; // The last time that this process was compacted
166 int reqCompactAction; // The most recent compaction action requested for this app.
167 int lastCompactAction; // The most recent compaction action performed for this app.
Marco Ballesiobdaf16e2020-01-23 13:57:00 -0800168 boolean frozen; // True when the process is frozen.
169 long freezeUnfreezeTime; // Last time the app was (un)frozen, 0 for never
Tim Murray7b4b26b2020-02-26 09:20:32 -0800170 boolean shouldNotFreeze; // True if a process has a WPRI binding from an unfrozen process
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700171 private int mCurSchedGroup; // Currently desired scheduling class
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700172 int setSchedGroup; // Last set to background scheduling class
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700173 int trimMemoryLevel; // Last selected memory trimming level
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700174 private int mCurProcState = PROCESS_STATE_NONEXISTENT; // Currently computed process state
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700175 private int mRepProcState = PROCESS_STATE_NONEXISTENT; // Last reported process state
Amith Yamasani08888c02018-12-07 09:38:25 -0800176 private int mCurRawProcState = PROCESS_STATE_NONEXISTENT; // Temp state during computation
riddle_hsu6793fc32015-03-31 11:54:14 +0800177 int setProcState = PROCESS_STATE_NONEXISTENT; // Last set process state in process tracker
178 int pssProcState = PROCESS_STATE_NONEXISTENT; // Currently requesting pss for
Dianne Hackborne17b4452018-01-10 13:15:40 -0800179 int pssStatType; // The type of stat collection that we are currently requesting
Tim Murray33eb07f2016-06-10 10:03:20 -0700180 int savedPriority; // Previous priority value if we're switching to non-SCHED_OTHER
181 int renderThreadTid; // TID for RenderThread
Dianne Hackborna631d562018-11-20 15:58:15 -0800182 ServiceRecord connectionService; // Service that applied current connectionGroup/Importance
183 int connectionGroup; // Last group set by a connection
184 int connectionImportance; // Last importance set by a connection
Dianne Hackborne02c88a2011-10-28 13:58:15 -0700185 boolean serviceb; // Process currently is on the service B list
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700186 boolean serviceHighRam; // We are forcing to service B list due to its RAM use
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700187 boolean notCachedSinceIdle; // Has this process not been in a cached state since last idle?
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700188 private boolean mHasClientActivities; // Are there any client services with activities?
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700189 boolean hasStartedServices; // Are there any started services running in this process?
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700190 private boolean mHasForegroundServices; // Running any services that are foreground?
Amith Yamasania0a30a12019-01-22 11:38:06 -0800191 private int mFgServiceTypes; // Type of foreground service, if there is a foreground service.
Narayan Kamath396ce722019-03-01 16:53:26 +0000192 private int mRepFgServiceTypes; // Last reported foreground service types.
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700193 private boolean mHasForegroundActivities; // Running any activities that are foreground?
Dianne Hackborn684bf342014-04-29 17:56:57 -0700194 boolean repForegroundActivities; // Last reported foreground activities.
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700195 boolean systemNoUi; // This is a system process, but not currently showing UI.
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -0700196 boolean hasShownUi; // Has UI been shown in this process since it was started?
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700197 private boolean mHasTopUi; // Is this process currently showing a non-activity UI that the user
Wale Ogunwaled993a572017-02-05 13:52:09 -0800198 // is interacting with? E.g. The status bar when it is expanded, but
199 // not when it is minimized. When true the
200 // process will be set to use the ProcessList#SCHED_GROUP_TOP_APP
201 // scheduling group to boost performance.
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700202 private boolean mHasOverlayUi; // Is the process currently showing a non-activity UI that
Wale Ogunwaled993a572017-02-05 13:52:09 -0800203 // overlays on-top of activity UIs on screen. E.g. display a window
204 // of type
205 // android.view.WindowManager.LayoutParams#TYPE_APPLICATION_OVERLAY
206 // When true the process will oom adj score will be set to
207 // ProcessList#PERCEPTIBLE_APP_ADJ at minimum to reduce the chance
208 // of the process getting killed.
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100209 boolean runningRemoteAnimation; // Is the process currently running a RemoteAnimation? When true
210 // the process will be set to use the
211 // ProcessList#SCHED_GROUP_TOP_APP scheduling group to boost
212 // performance, as well as oom adj score will be set to
213 // ProcessList#VISIBLE_APP_ADJ at minimum to reduce the chance
214 // of the process getting killed.
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700215 private boolean mPendingUiClean; // Want to clean up resources from showing UI?
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700216 boolean hasAboveClient; // Bound using BIND_ABOVE_CLIENT, so want to be lower
Dianne Hackbornf0f94d12014-03-17 16:04:21 -0700217 boolean treatLikeActivity; // Bound using BIND_TREAT_LIKE_ACTIVITY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 boolean bad; // True if disabled in the bad process list
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700219 boolean killedByAm; // True when proc has been killed by activity manager, not for RAM
Dianne Hackbornd4125632014-09-22 12:52:49 -0700220 boolean killed; // True once we know the process has been killed
Dianne Hackbornc8230512013-07-13 21:32:12 -0700221 boolean procStateChanged; // Keep track of whether we changed 'setAdj'.
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700222 boolean reportedInteraction;// Whether we have told usage stats about it being an interaction
Jeff Sharkeybd91e2f2016-03-22 15:32:31 -0600223 boolean unlocked; // True when proc was started in user unlocked state
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700224 private long mInteractionEventTime; // The time we sent the last interaction event
225 private long mFgInteractionTime; // When we became foreground for interaction purposes
Dianne Hackborn9357b112013-10-03 18:27:48 -0700226 String waitingToKill; // Process is waiting to be killed when in the bg, and reason
Dianne Hackbornf965f402017-05-04 23:27:23 -0700227 Object forcingToImportant; // Token that is forcing this process to be important
Dianne Hackborn906497c2010-05-10 15:57:38 -0700228 int adjSeq; // Sequence id for identifying oom_adj assignment cycles
Tim Murrayb52f0002018-05-02 15:14:35 -0700229 int completedAdjSeq; // Sequence id for identifying oom_adj assignment cycles
230 boolean containsCycle; // Whether this app has encountered a cycle in the most recent update
Dianne Hackborn906497c2010-05-10 15:57:38 -0700231 int lruSeq; // Sequence id for identifying LRU update cycles
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700232 CompatibilityInfo compat; // last used compatibility mode
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -0700233 IBinder.DeathRecipient deathRecipient; // Who is watching for the death.
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700234 private ActiveInstrumentation mInstr; // Set to currently active instrumentation running in
235 // process.
236 private boolean mUsingWrapper; // Set to true when process was launched with a wrapper attached
yangzhenyud509bc92016-08-31 18:26:46 +0800237 final ArraySet<BroadcastRecord> curReceivers = new ArraySet<BroadcastRecord>();// receivers currently running in the app
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700238 private long mWhenUnimportant; // When (uptime) the process last became unimportant
Dianne Hackborn287952c2010-09-22 22:34:31 -0700239 long lastCpuTime; // How long proc has run CPU at last check
240 long curCpuTime; // How long proc has run CPU most recently
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 long lastRequestedGc; // When we last asked the app to do a gc
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700242 long lastLowMemory; // When we last told the app that memory is low
Dianne Hackborn1a405642015-11-04 12:32:45 -0800243 long lastProviderTime; // The last time someone else was using a provider in this process.
Amith Yamasanie5bfeee2018-09-05 18:52:35 -0700244 long lastTopTime; // The last time the process was in the TOP state or greater.
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700245 boolean reportLowMemory; // Set to true when waiting to report low mem
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800246 boolean empty; // Is this an empty background process?
Darryl L Johnson15192d32020-01-15 15:31:42 -0800247 private volatile boolean mCached; // Is this a cached process?
Dianne Hackbornde42bb62009-08-05 12:26:15 -0700248 String adjType; // Debugging: primary thing impacting oom_adj.
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700249 int adjTypeCode; // Debugging: adj code to report to app.
Dianne Hackbornde42bb62009-08-05 12:26:15 -0700250 Object adjSource; // Debugging: option dependent object.
Dianne Hackbornfee756f2014-07-16 17:31:10 -0700251 int adjSourceProcState; // Debugging: proc state of adjSource's process.
Dianne Hackbornde42bb62009-08-05 12:26:15 -0700252 Object adjTarget; // Debugging: target component impacting oom_adj.
Primiano Tucci810c0522014-07-25 18:03:16 +0100253 Runnable crashHandler; // Optional local handler to be invoked in the process crash.
Ricky Wai486d7602020-02-28 16:37:07 +0000254 boolean bindMountPending; // True if Android/obb and Android/data need to be bind mount .
Primiano Tucci810c0522014-07-25 18:03:16 +0100255
Dianne Hackborn8c76d912018-08-23 15:20:05 -0700256 // Cache of last retrieve memory info and uptime, to throttle how frequently
257 // apps can requyest it.
258 Debug.MemoryInfo lastMemInfo;
259 long lastMemInfoTime;
260
Charles Chen173ae782019-11-11 20:39:02 +0800261 // Controller for error dialogs
262 private final ErrorDialogController mDialogController = new ErrorDialogController();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700263 // Controller for driving the process state on the window manager side.
Darryl L Johnsond02044c2020-04-17 11:52:50 -0700264 private final WindowProcessController mWindowProcessController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 // all ServiceRecord running in this process
Darryl L Johnsond02044c2020-04-17 11:52:50 -0700266 private final ArraySet<ServiceRecord> mServices = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 // services that are currently executing code (need to remain foreground).
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700268 final ArraySet<ServiceRecord> executingServices = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 // All ConnectionRecord this process holds
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700270 final ArraySet<ConnectionRecord> connections = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 // all IIntentReceivers that are registered from this process.
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700272 final ArraySet<ReceiverList> receivers = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 // class (String) -> ContentProviderRecord
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700274 final ArrayMap<String, ContentProviderRecord> pubProviders = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 // All ContentProviderRecord process is using
Dianne Hackbornd23e0d62015-05-15 16:36:12 -0700276 final ArrayList<ContentProviderConnection> conProviders = new ArrayList<>();
Michal Karpinskiac116df2018-12-10 17:51:42 +0000277 // A set of tokens that currently contribute to this process being temporarily whitelisted
278 // to start activities even if it's not in the foreground
279 final ArraySet<Binder> mAllowBackgroundActivityStartsTokens = new ArraySet<>();
Michal Karpinskib7daac22019-03-25 10:12:41 +0000280 // a set of UIDs of all bound clients
281 private ArraySet<Integer> mBoundClientUids = new ArraySet<>();
Dianne Hackbornbf36ee22013-07-26 18:24:10 -0700282
Torne (Richard Coles)466cbe42017-05-31 14:09:14 -0400283 String isolatedEntryPoint; // Class to run on start if this is a special isolated process.
284 String[] isolatedEntryPointArgs; // Arguments to pass to isolatedEntryPoint's main().
285
Dianne Hackbornbf36ee22013-07-26 18:24:10 -0700286 boolean execServicesFg; // do we need to be executing services in the foreground?
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700287 private boolean mPersistent;// always keep this application running?
288 private boolean mCrashing; // are we in the process of crashing?
Christopher Tate58d380d2013-03-19 13:10:03 -0700289 boolean forceCrashReport; // suppress normal auto-dismiss of crash dialog & report UI?
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700290 private boolean mNotResponding; // does the app have a not responding dialog?
Riddle Hsuaaef7312019-01-24 19:00:58 +0800291 volatile boolean removed; // Whether this process should be killed and removed from process
292 // list. It is set when the package is force-stopped or the process
293 // has crashed too many times.
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700294 private boolean mDebugging; // was app launched for debugging?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 boolean waitedForDebugger; // has process show wait for debugger dialog?
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700296
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800297 String shortStringName; // caching of toShortString() result.
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700298 String stringName; // caching of toString() result.
Sudheer Shankaf6690102017-10-16 10:20:32 -0700299 boolean pendingStart; // Process start is pending.
300 long startSeq; // Seq no. indicating the latest process start associated with
301 // this process record.
Sudheer Shanka87915d62018-11-06 10:57:35 -0800302 int mountMode; // Indicates how the external storage was mounted for this process.
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 // These reports are generated & stored when an app gets into an error condition.
305 // They will be "null" when all is OK.
306 ActivityManager.ProcessErrorStateInfo crashingReport;
307 ActivityManager.ProcessErrorStateInfo notRespondingReport;
308
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200309 // Who will be notified of the error. This is usually an activity in the
310 // app that installed the package.
311 ComponentName errorReportReceiver;
312
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700313 // Process is currently hosting a backup agent for backup or restore
314 public boolean inFullBackup;
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700315 // App is allowed to manage whitelists such as temporary Power Save mode whitelist.
316 boolean whitelistManager;
Amith Yamasanib5f3b5c2016-05-16 14:23:04 -0700317
Sudheer Shankaf6690102017-10-16 10:20:32 -0700318 // Params used in starting this process.
Martijn Coenene8431c22019-03-28 14:09:38 +0100319 HostingRecord hostingRecord;
Sudheer Shankaf6690102017-10-16 10:20:32 -0700320 String seInfo;
321 long startTime;
322 // This will be same as {@link #uid} usually except for some apps used during factory testing.
323 int startUid;
atrost5ae996f2019-12-11 18:32:48 +0000324 // set of disabled compat changes for the process (all others are enabled)
325 long[] mDisabledCompatChanges;
Sudheer Shankaf6690102017-10-16 10:20:32 -0700326
Jing Jidcc28a02020-03-07 00:28:40 -0800327 long mLastRss; // Last computed memory rss.
328
Jing Ji971da4f2020-01-28 16:34:59 -0800329 // The precede instance of the process, which would exist when the previous process is killed
330 // but not fully dead yet; in this case, the new instance of the process should be held until
331 // this precede instance is fully dead.
332 volatile ProcessRecord mPrecedence;
333 // The succeeding instance of the process, which is going to be started after this process
334 // is killed successfully.
335 volatile ProcessRecord mSuccessor;
336
Jing Ji094c3ef2019-08-27 17:02:09 -0700337 // Cached task info for OomAdjuster
338 private static final int VALUE_INVALID = -1;
339 private static final int VALUE_FALSE = 0;
340 private static final int VALUE_TRUE = 1;
341 private int mCachedHasActivities = VALUE_INVALID;
342 private int mCachedIsHeavyWeight = VALUE_INVALID;
343 private int mCachedHasVisibleActivities = VALUE_INVALID;
344 private int mCachedIsHomeProcess = VALUE_INVALID;
345 private int mCachedIsPreviousProcess = VALUE_INVALID;
346 private int mCachedHasRecentTasks = VALUE_INVALID;
347 private int mCachedIsReceivingBroadcast = VALUE_INVALID;
348 int mCachedAdj = ProcessList.INVALID_ADJ;
349 boolean mCachedForegroundActivities = false;
350 int mCachedProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
351 int mCachedSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
352
Jing Ji619d6442019-11-21 18:26:40 -0800353 boolean mReachable; // Whether or not this process is reachable from given process
354
Martijn Coenene8431c22019-03-28 14:09:38 +0100355 void setStartParams(int startUid, HostingRecord hostingRecord, String seInfo,
Sudheer Shankaf6690102017-10-16 10:20:32 -0700356 long startTime) {
357 this.startUid = startUid;
Martijn Coenene8431c22019-03-28 14:09:38 +0100358 this.hostingRecord = hostingRecord;
Sudheer Shankaf6690102017-10-16 10:20:32 -0700359 this.seInfo = seInfo;
360 this.startTime = startTime;
361 }
362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 void dump(PrintWriter pw, String prefix) {
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700364 final long nowUptime = SystemClock.uptimeMillis();
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700365
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800366 pw.print(prefix); pw.print("user #"); pw.print(userId);
367 pw.print(" uid="); pw.print(info.uid);
368 if (uid != info.uid) {
369 pw.print(" ISOLATED uid="); pw.print(uid);
370 }
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800371 pw.print(" gids={");
372 if (gids != null) {
373 for (int gi=0; gi<gids.length; gi++) {
374 if (gi != 0) pw.print(", ");
375 pw.print(gids[gi]);
376
377 }
378 }
379 pw.println("}");
Dianne Hackborn679925b2020-02-04 17:02:18 -0800380 if (processInfo != null) {
381 pw.print(prefix); pw.println("processInfo:");
382 if (processInfo.deniedPermissions != null) {
383 for (int i = 0; i < processInfo.deniedPermissions.size(); i++) {
384 pw.print(prefix); pw.print(" deny: ");
385 pw.println(processInfo.deniedPermissions.valueAt(i));
386 }
387 }
Evgenii Stepanovd43d1092020-03-16 13:55:42 -0700388 if (processInfo.gwpAsanMode != ApplicationInfo.GWP_ASAN_DEFAULT) {
389 pw.print(prefix); pw.println(" gwpAsanMode=" + processInfo.gwpAsanMode);
Evgenii Stepanov102d3d82020-02-12 16:48:14 -0800390 }
Dianne Hackborn679925b2020-02-04 17:02:18 -0800391 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700392 pw.print(prefix); pw.print("mRequiredAbi="); pw.print(mRequiredAbi);
Dianne Hackbornb3d4cb32015-01-09 09:54:06 -0800393 pw.print(" instructionSet="); pw.println(instructionSet);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700394 if (info.className != null) {
395 pw.print(prefix); pw.print("class="); pw.println(info.className);
396 }
397 if (info.manageSpaceActivityName != null) {
398 pw.print(prefix); pw.print("manageSpaceActivityName=");
399 pw.println(info.manageSpaceActivityName);
400 }
Yunfan Chen75157d72018-07-27 14:47:21 +0900401
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700402 pw.print(prefix); pw.print("dir="); pw.print(info.sourceDir);
403 pw.print(" publicDir="); pw.print(info.publicSourceDir);
404 pw.print(" data="); pw.println(info.dataDir);
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700405 pw.print(prefix); pw.print("packageList={");
406 for (int i=0; i<pkgList.size(); i++) {
407 if (i > 0) pw.print(", ");
408 pw.print(pkgList.keyAt(i));
409 }
410 pw.println("}");
Dianne Hackbornfee756f2014-07-16 17:31:10 -0700411 if (pkgDeps != null) {
412 pw.print(prefix); pw.print("packageDependencies={");
413 for (int i=0; i<pkgDeps.size(); i++) {
414 if (i > 0) pw.print(", ");
415 pw.print(pkgDeps.valueAt(i));
416 }
417 pw.println("}");
418 }
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -0700419 pw.print(prefix); pw.print("compat="); pw.println(compat);
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700420 if (mInstr != null) {
421 pw.print(prefix); pw.print("mInstr="); pw.println(mInstr);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700422 }
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700423 pw.print(prefix); pw.print("thread="); pw.println(thread);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700424 pw.print(prefix); pw.print("pid="); pw.print(pid); pw.print(" starting=");
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700425 pw.println(starting);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700426 pw.print(prefix); pw.print("lastActivityTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700427 TimeUtils.formatDuration(lastActivityTime, nowUptime, pw);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700428 pw.print(" lastPssTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700429 TimeUtils.formatDuration(lastPssTime, nowUptime, pw);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800430 pw.print(" pssStatType="); pw.print(pssStatType);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700431 pw.print(" nextPssTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700432 TimeUtils.formatDuration(nextPssTime, nowUptime, pw);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700433 pw.println();
Dianne Hackborncb6db382020-05-13 17:03:55 -0700434 pw.print(prefix); pw.print("lastPss="); DebugUtils.printSizeValue(pw, lastPss * 1024);
435 pw.print(" lastSwapPss="); DebugUtils.printSizeValue(pw, lastSwapPss * 1024);
436 pw.print(" lastCachedPss="); DebugUtils.printSizeValue(pw, lastCachedPss * 1024);
437 pw.print(" lastCachedSwapPss="); DebugUtils.printSizeValue(pw,
438 lastCachedSwapPss * 1024);
439 pw.print(" lastRss="); DebugUtils.printSizeValue(pw, mLastRss * 1024);
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -0800440 pw.println();
Dianne Hackborne17b4452018-01-10 13:15:40 -0800441 pw.print(prefix); pw.print("procStateMemTracker: ");
442 procStateMemTracker.dumpLine(pw);
Dianne Hackborncb6db382020-05-13 17:03:55 -0700443 pw.print(prefix); pw.print("adjSeq="); pw.print(adjSeq);
444 pw.print(" lruSeq="); pw.println(lruSeq);
445 pw.print(prefix); pw.print("oom adj: max="); pw.print(maxAdj);
446 pw.print(" curRaw="); pw.print(mCurRawAdj);
447 pw.print(" setRaw="); pw.print(setRawAdj);
448 pw.print(" cur="); pw.print(curAdj);
449 pw.print(" set="); pw.println(setAdj);
450 pw.print(prefix); pw.print("lastCompactTime="); pw.print(lastCompactTime);
451 pw.print(" lastCompactAction="); pw.println(lastCompactAction);
452 pw.print(prefix); pw.print("mCurSchedGroup="); pw.print(mCurSchedGroup);
453 pw.print(" setSchedGroup="); pw.print(setSchedGroup);
454 pw.print(" systemNoUi="); pw.print(systemNoUi);
455 pw.print(" trimMemoryLevel="); pw.println(trimMemoryLevel);
456 pw.print(prefix); pw.print("curProcState="); pw.print(getCurProcState());
457 pw.print(" mRepProcState="); pw.print(mRepProcState);
458 pw.print(" pssProcState="); pw.print(pssProcState);
459 pw.print(" setProcState="); pw.print(setProcState);
460 pw.print(" lastStateTime=");
461 TimeUtils.formatDuration(lastStateTime, nowUptime, pw);
462 pw.println();
463 pw.print(prefix); pw.print("curCapability=");
464 ActivityManager.printCapabilitiesFull(pw, curCapability);
465 pw.print(" setCapability=");
466 ActivityManager.printCapabilitiesFull(pw, setCapability);
467 pw.println();
468 if (hasShownUi || mPendingUiClean || hasAboveClient || treatLikeActivity) {
469 pw.print(prefix); pw.print("hasShownUi="); pw.print(hasShownUi);
470 pw.print(" pendingUiClean="); pw.print(mPendingUiClean);
471 pw.print(" hasAboveClient="); pw.print(hasAboveClient);
472 pw.print(" treatLikeActivity="); pw.println(treatLikeActivity);
473 }
Darryl L Johnson72aac102020-01-15 15:16:36 -0800474 pw.print(prefix); pw.print("cached="); pw.print(mCached);
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800475 pw.print(" empty="); pw.println(empty);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700476 if (serviceb) {
477 pw.print(prefix); pw.print("serviceb="); pw.print(serviceb);
478 pw.print(" serviceHighRam="); pw.println(serviceHighRam);
479 }
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700480 if (notCachedSinceIdle) {
481 pw.print(prefix); pw.print("notCachedSinceIdle="); pw.print(notCachedSinceIdle);
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700482 pw.print(" initialIdlePss="); pw.println(initialIdlePss);
Dianne Hackborn35f72be2013-09-16 10:57:39 -0700483 }
Dianne Hackborna631d562018-11-20 15:58:15 -0800484 if (connectionService != null || connectionGroup != 0) {
485 pw.print(prefix); pw.print("connectionGroup="); pw.print(connectionGroup);
486 pw.print(" Importance="); pw.print(connectionImportance);
487 pw.print(" Service="); pw.println(connectionService);
488 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700489 if (hasTopUi() || hasOverlayUi() || runningRemoteAnimation) {
490 pw.print(prefix); pw.print("hasTopUi="); pw.print(hasTopUi());
491 pw.print(" hasOverlayUi="); pw.print(hasOverlayUi());
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100492 pw.print(" runningRemoteAnimation="); pw.println(runningRemoteAnimation);
Dianne Hackborna1baf732017-05-25 15:33:53 -0700493 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700494 if (mHasForegroundServices || forcingToImportant != null) {
495 pw.print(prefix); pw.print("mHasForegroundServices="); pw.print(mHasForegroundServices);
Dianne Hackbornf965f402017-05-04 23:27:23 -0700496 pw.print(" forcingToImportant="); pw.println(forcingToImportant);
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700497 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700498 if (reportedInteraction || mFgInteractionTime != 0) {
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700499 pw.print(prefix); pw.print("reportedInteraction=");
500 pw.print(reportedInteraction);
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700501 if (mInteractionEventTime != 0) {
Dianne Hackbornfcc95a62015-11-02 13:43:29 -0800502 pw.print(" time=");
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700503 TimeUtils.formatDuration(mInteractionEventTime, SystemClock.elapsedRealtime(), pw);
Dianne Hackbornfcc95a62015-11-02 13:43:29 -0800504 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700505 if (mFgInteractionTime != 0) {
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700506 pw.print(" fgInteractionTime=");
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700507 TimeUtils.formatDuration(mFgInteractionTime, SystemClock.elapsedRealtime(), pw);
Dianne Hackbornab4bb9d2015-06-05 18:08:37 -0700508 }
509 pw.println();
510 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700511 if (mPersistent || removed) {
512 pw.print(prefix); pw.print("persistent="); pw.print(mPersistent);
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700513 pw.print(" removed="); pw.println(removed);
514 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700515 if (mHasClientActivities || mHasForegroundActivities || repForegroundActivities) {
516 pw.print(prefix); pw.print("hasClientActivities="); pw.print(mHasClientActivities);
517 pw.print(" foregroundActivities="); pw.print(mHasForegroundActivities);
Dianne Hackborn684bf342014-04-29 17:56:57 -0700518 pw.print(" (rep="); pw.print(repForegroundActivities); pw.println(")");
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700519 }
Dianne Hackborn1a405642015-11-04 12:32:45 -0800520 if (lastProviderTime > 0) {
521 pw.print(prefix); pw.print("lastProviderTime=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700522 TimeUtils.formatDuration(lastProviderTime, nowUptime, pw);
Dianne Hackborn1a405642015-11-04 12:32:45 -0800523 pw.println();
524 }
Amith Yamasanie5bfeee2018-09-05 18:52:35 -0700525 if (lastTopTime > 0) {
526 pw.print(prefix); pw.print("lastTopTime=");
527 TimeUtils.formatDuration(lastTopTime, nowUptime, pw);
528 pw.println();
529 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -0700530 if (hasStartedServices) {
531 pw.print(prefix); pw.print("hasStartedServices="); pw.println(hasStartedServices);
532 }
Sudheer Shankaf6690102017-10-16 10:20:32 -0700533 if (pendingStart) {
534 pw.print(prefix); pw.print("pendingStart="); pw.println(pendingStart);
535 }
536 pw.print(prefix); pw.print("startSeq="); pw.println(startSeq);
Sudheer Shanka87915d62018-11-06 10:57:35 -0800537 pw.print(prefix); pw.print("mountMode="); pw.println(
538 DebugUtils.valueToString(Zygote.class, "MOUNT_EXTERNAL_", mountMode));
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700539 if (setProcState > ActivityManager.PROCESS_STATE_SERVICE) {
Dianne Hackborn287952c2010-09-22 22:34:31 -0700540 pw.print(prefix); pw.print("lastCpuTime="); pw.print(lastCpuTime);
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700541 if (lastCpuTime > 0) {
542 pw.print(" timeUsed=");
543 TimeUtils.formatDuration(curCpuTime - lastCpuTime, pw);
544 }
545 pw.print(" whenUnimportant=");
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700546 TimeUtils.formatDuration(mWhenUnimportant - nowUptime, pw);
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700547 pw.println();
Dianne Hackborn287952c2010-09-22 22:34:31 -0700548 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700549 pw.print(prefix); pw.print("lastRequestedGc=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700550 TimeUtils.formatDuration(lastRequestedGc, nowUptime, pw);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700551 pw.print(" lastLowMemory=");
Dianne Hackbornffca58b2017-05-24 16:15:45 -0700552 TimeUtils.formatDuration(lastLowMemory, nowUptime, pw);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700553 pw.print(" reportLowMemory="); pw.println(reportLowMemory);
Dianne Hackbornd4125632014-09-22 12:52:49 -0700554 if (killed || killedByAm || waitingToKill != null) {
555 pw.print(prefix); pw.print("killed="); pw.print(killed);
556 pw.print(" killedByAm="); pw.print(killedByAm);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700557 pw.print(" waitingToKill="); pw.println(waitingToKill);
Dianne Hackborn149427c2010-04-23 14:20:03 -0700558 }
Charles Chen173ae782019-11-11 20:39:02 +0800559 if (mDebugging || mCrashing || mDialogController.hasCrashDialogs() || mNotResponding
560 || mDialogController.hasAnrDialogs() || bad) {
Wale Ogunwale906f9c62018-07-23 11:23:44 -0700561 pw.print(prefix); pw.print("mDebugging="); pw.print(mDebugging);
Charles Chen173ae782019-11-11 20:39:02 +0800562 pw.print(" mCrashing=" + mCrashing);
563 pw.print(" " + mDialogController.mCrashDialogs);
564 pw.print(" mNotResponding=" + mNotResponding);
565 pw.print(" " + mDialogController.mAnrDialogs);
566 pw.print(" bad=" + bad);
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200567
Charles Chen173ae782019-11-11 20:39:02 +0800568 // mCrashing or mNotResponding is always set before errorReportReceiver
569 if (errorReportReceiver != null) {
570 pw.print(" errorReportReceiver=");
571 pw.print(errorReportReceiver.flattenToShortString());
572 }
573 pw.println();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700574 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700575 if (whitelistManager) {
576 pw.print(prefix); pw.print("whitelistManager="); pw.println(whitelistManager);
577 }
Torne (Richard Coles)466cbe42017-05-31 14:09:14 -0400578 if (isolatedEntryPoint != null || isolatedEntryPointArgs != null) {
579 pw.print(prefix); pw.print("isolatedEntryPoint="); pw.println(isolatedEntryPoint);
580 pw.print(prefix); pw.print("isolatedEntryPointArgs=");
581 pw.println(Arrays.toString(isolatedEntryPointArgs));
582 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700583 mWindowProcessController.dump(pw, prefix);
Darryl L Johnsond02044c2020-04-17 11:52:50 -0700584 if (mServices.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700585 pw.print(prefix); pw.println("Services:");
Darryl L Johnsond02044c2020-04-17 11:52:50 -0700586 for (int i = 0; i < mServices.size(); i++) {
587 pw.print(prefix); pw.print(" - "); pw.println(mServices.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700588 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700589 }
590 if (executingServices.size() > 0) {
Dianne Hackbornbf36ee22013-07-26 18:24:10 -0700591 pw.print(prefix); pw.print("Executing Services (fg=");
592 pw.print(execServicesFg); pw.println(")");
Dianne Hackbornc8230512013-07-13 21:32:12 -0700593 for (int i=0; i<executingServices.size(); i++) {
594 pw.print(prefix); pw.print(" - "); pw.println(executingServices.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700595 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700596 }
597 if (connections.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700598 pw.print(prefix); pw.println("Connections:");
Dianne Hackbornc8230512013-07-13 21:32:12 -0700599 for (int i=0; i<connections.size(); i++) {
600 pw.print(prefix); pw.print(" - "); pw.println(connections.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700601 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700602 }
603 if (pubProviders.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700604 pw.print(prefix); pw.println("Published Providers:");
Dianne Hackborn390517b2013-05-30 15:03:32 -0700605 for (int i=0; i<pubProviders.size(); i++) {
606 pw.print(prefix); pw.print(" - "); pw.println(pubProviders.keyAt(i));
607 pw.print(prefix); pw.print(" -> "); pw.println(pubProviders.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700608 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700609 }
610 if (conProviders.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700611 pw.print(prefix); pw.println("Connected Providers:");
612 for (int i=0; i<conProviders.size(); i++) {
613 pw.print(prefix); pw.print(" - "); pw.println(conProviders.get(i).toShortString());
614 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700615 }
yangzhenyud509bc92016-08-31 18:26:46 +0800616 if (!curReceivers.isEmpty()) {
617 pw.print(prefix); pw.println("Current Receivers:");
618 for (int i=0; i < curReceivers.size(); i++) {
619 pw.print(prefix); pw.print(" - "); pw.println(curReceivers.valueAt(i));
620 }
Dianne Hackbornb12e1352012-09-26 11:39:20 -0700621 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700622 if (receivers.size() > 0) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700623 pw.print(prefix); pw.println("Receivers:");
Dianne Hackbornc8230512013-07-13 21:32:12 -0700624 for (int i=0; i<receivers.size(); i++) {
625 pw.print(prefix); pw.print(" - "); pw.println(receivers.valueAt(i));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700626 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700627 }
Edward Cunninghamde661762019-03-27 15:51:56 +0000628 if (mAllowBackgroundActivityStartsTokens.size() > 0) {
629 pw.print(prefix); pw.println("Background activity start whitelist tokens:");
630 for (int i = 0; i < mAllowBackgroundActivityStartsTokens.size(); i++) {
631 pw.print(prefix); pw.print(" - ");
632 pw.println(mAllowBackgroundActivityStartsTokens.valueAt(i));
633 }
634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700636
Bookatz152623f2018-07-23 15:57:44 -0700637 ProcessRecord(ActivityManagerService _service, ApplicationInfo _info, String _processName,
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700638 int _uid) {
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -0700639 mService = _service;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 info = _info;
Dianne Hackbornfc0839a2020-01-31 11:02:28 -0800641 ProcessInfo procInfo = null;
Dianne Hackbornf6729fa2020-01-06 13:12:36 -0800642 if (_service.mPackageManagerInt != null) {
643 ArrayMap<String, ProcessInfo> processes =
644 _service.mPackageManagerInt.getProcessesForUid(_uid);
Dianne Hackbornfc0839a2020-01-31 11:02:28 -0800645 if (processes != null) {
646 procInfo = processes.get(_processName);
Evgenii Stepanovd43d1092020-03-16 13:55:42 -0700647 if (procInfo != null && procInfo.deniedPermissions == null
648 && procInfo.gwpAsanMode == ApplicationInfo.GWP_ASAN_DEFAULT) {
649 // If this process hasn't asked for permissions to be denied, or for a
650 // non-default GwpAsan mode, then we don't care about it.
Dianne Hackbornfc0839a2020-01-31 11:02:28 -0800651 procInfo = null;
652 }
653 }
Dianne Hackbornf6729fa2020-01-06 13:12:36 -0800654 }
Dianne Hackbornfc0839a2020-01-31 11:02:28 -0800655 processInfo = procInfo;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800656 isolated = _info.uid != _uid;
Martijn Coenen01e719b2018-12-05 16:01:38 +0100657 appZygote = (UserHandle.getAppId(_uid) >= Process.FIRST_APP_ZYGOTE_ISOLATED_UID
658 && UserHandle.getAppId(_uid) <= Process.LAST_APP_ZYGOTE_ISOLATED_UID);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800659 uid = _uid;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700660 userId = UserHandle.getUserId(_uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 processName = _processName;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700662 maxAdj = ProcessList.UNKNOWN_ADJ;
Wale Ogunwale31913b52018-10-13 08:29:31 -0700663 mCurRawAdj = setRawAdj = ProcessList.INVALID_ADJ;
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700664 curAdj = setAdj = verifiedAdj = ProcessList.INVALID_ADJ;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700665 mPersistent = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 removed = false;
Marco Ballesiobdaf16e2020-01-23 13:57:00 -0800667 freezeUnfreezeTime = lastStateTime = lastPssTime = nextPssTime = SystemClock.uptimeMillis();
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700668 mWindowProcessController = new WindowProcessController(
Wale Ogunwale387b34c2018-10-25 19:59:40 -0700669 mService.mActivityTaskManager, info, processName, uid, userId, this, this);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700670 pkgList.put(_info.packageName, new ProcessStats.ProcessStateHolder(_info.longVersionCode));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 }
672
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700673 public void setPid(int _pid) {
674 pid = _pid;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700675 mWindowProcessController.setPid(pid);
Dianne Hackborn4de5a3a2016-06-20 15:20:15 -0700676 procStatFile = null;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800677 shortStringName = null;
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700678 stringName = null;
679 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700680
681 public void makeActive(IApplicationThread _thread, ProcessStatsService tracker) {
682 if (thread == null) {
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700683 final ProcessState origBase = baseProcessTracker;
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700684 if (origBase != null) {
685 origBase.setState(ProcessStats.STATE_NOTHING,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700686 tracker.getMemFactorLocked(), SystemClock.uptimeMillis(), pkgList.mPkgList);
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700687 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -0800688 FrameworkStatsLog.write(FrameworkStatsLog.PROCESS_STATE_CHANGED,
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700689 uid, processName, pkgList.keyAt(ipkg),
690 ActivityManager.processStateAmToProto(ProcessStats.STATE_NOTHING),
691 pkgList.valueAt(ipkg).appVersion);
692 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700693 origBase.makeInactive();
694 }
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800695 baseProcessTracker = tracker.getProcessStateLocked(info.packageName, info.uid,
Patrick Baumannc2def582018-04-04 12:14:15 -0700696 info.longVersionCode, processName);
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700697 baseProcessTracker.makeActive();
698 for (int i=0; i<pkgList.size(); i++) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700699 ProcessStats.ProcessStateHolder holder = pkgList.valueAt(i);
700 if (holder.state != null && holder.state != origBase) {
701 holder.state.makeInactive();
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700702 }
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800703 tracker.updateProcessStateHolderLocked(holder, pkgList.keyAt(i), info.uid,
Patrick Baumannc2def582018-04-04 12:14:15 -0700704 info.longVersionCode, processName);
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700705 if (holder.state != baseProcessTracker) {
706 holder.state.makeActive();
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700707 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700708 }
709 }
710 thread = _thread;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700711 mWindowProcessController.setThread(thread);
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700712 }
713
714 public void makeInactive(ProcessStatsService tracker) {
Dianne Hackborn53459a72013-09-17 17:14:57 -0700715 thread = null;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700716 mWindowProcessController.setThread(null);
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700717 final ProcessState origBase = baseProcessTracker;
Dianne Hackborn53459a72013-09-17 17:14:57 -0700718 if (origBase != null) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700719 if (origBase != null) {
720 origBase.setState(ProcessStats.STATE_NOTHING,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700721 tracker.getMemFactorLocked(), SystemClock.uptimeMillis(), pkgList.mPkgList);
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700722 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -0800723 FrameworkStatsLog.write(FrameworkStatsLog.PROCESS_STATE_CHANGED,
Chenjie Yubd1a28f2018-07-17 14:55:19 -0700724 uid, processName, pkgList.keyAt(ipkg),
725 ActivityManager.processStateAmToProto(ProcessStats.STATE_NOTHING),
726 pkgList.valueAt(ipkg).appVersion);
727 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700728 origBase.makeInactive();
729 }
730 baseProcessTracker = null;
731 for (int i=0; i<pkgList.size(); i++) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700732 ProcessStats.ProcessStateHolder holder = pkgList.valueAt(i);
733 if (holder.state != null && holder.state != origBase) {
734 holder.state.makeInactive();
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700735 }
Dianne Hackborn2aec55a2018-06-26 10:35:35 -0700736 holder.pkg = null;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700737 holder.state = null;
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -0700738 }
739 }
740 }
741
Darryl L Johnsond02044c2020-04-17 11:52:50 -0700742 /**
743 * Records a service as running in the process. Note that this method does not actually start
744 * the service, but records the service as started for bookkeeping.
745 *
746 * @return true if the service was added, false otherwise.
747 */
748 boolean startService(ServiceRecord record) {
749 if (record == null) {
750 return false;
751 }
752 boolean added = mServices.add(record);
753 if (added && record.serviceInfo != null) {
754 mWindowProcessController.onServiceStarted(record.serviceInfo);
755 }
756 return added;
757 }
758
759 /**
760 * Records a service as stopped. Note that like {@link #startService(ServiceRecord)} this method
761 * does not actually stop the service, but records the service as stopped for bookkeeping.
762 *
763 * @return true if the service was removed, false otherwise.
764 */
765 boolean stopService(ServiceRecord record) {
766 return mServices.remove(record);
767 }
768
769 /**
770 * The same as calling {@link #stopService(ServiceRecord)} on all current running services.
771 */
772 void stopAllServices() {
773 mServices.clear();
774 }
775
776 /**
777 * Returns the number of services added with {@link #startService(ServiceRecord)} and not yet
778 * removed by a call to {@link #stopService(ServiceRecord)} or {@link #stopAllServices()}.
779 *
780 * @see #startService(ServiceRecord)
781 * @see #stopService(ServiceRecord)
782 */
783 int numberOfRunningServices() {
784 return mServices.size();
785 }
786
787 /**
788 * Returns the service at the specified {@code index}.
789 *
790 * @see #numberOfRunningServices()
791 */
792 ServiceRecord getRunningServiceAt(int index) {
793 return mServices.valueAt(index);
794 }
795
Darryl L Johnson72aac102020-01-15 15:16:36 -0800796 void setCached(boolean cached) {
Darryl L Johnson15192d32020-01-15 15:31:42 -0800797 if (mCached != cached) {
798 mCached = cached;
799 mWindowProcessController.onProcCachedStateChanged(cached);
800 }
Darryl L Johnson72aac102020-01-15 15:16:36 -0800801 }
802
Darryl L Johnson15192d32020-01-15 15:31:42 -0800803 @Override
804 public boolean isCached() {
Darryl L Johnson72aac102020-01-15 15:16:36 -0800805 return mCached;
806 }
807
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700808 boolean hasActivities() {
809 return mWindowProcessController.hasActivities();
810 }
811
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700812 boolean hasActivitiesOrRecentTasks() {
813 return mWindowProcessController.hasActivitiesOrRecentTasks();
814 }
815
816 boolean hasRecentTasks() {
817 return mWindowProcessController.hasRecentTasks();
818 }
819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 /**
821 * This method returns true if any of the activities within the process record are interesting
822 * to the user. See HistoryRecord.isInterestingToUserLocked()
823 */
824 public boolean isInterestingToUserLocked() {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700825 if (mWindowProcessController.isInterestingToUser()) {
826 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 }
Narayan Kamatha22d9fb2017-04-26 15:14:26 +0100828
Darryl L Johnsond02044c2020-04-17 11:52:50 -0700829 final int servicesSize = mServices.size();
Narayan Kamatha22d9fb2017-04-26 15:14:26 +0100830 for (int i = 0; i < servicesSize; i++) {
Darryl L Johnsond02044c2020-04-17 11:52:50 -0700831 ServiceRecord r = mServices.valueAt(i);
Narayan Kamatha22d9fb2017-04-26 15:14:26 +0100832 if (r.isForeground) {
833 return true;
834 }
835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 return false;
837 }
Wale Ogunwalee23149f2015-03-06 15:39:44 -0800838
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -0700839 public void unlinkDeathRecipient() {
840 if (deathRecipient != null && thread != null) {
841 thread.asBinder().unlinkToDeath(deathRecipient, 0);
842 }
843 deathRecipient = null;
844 }
845
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700846 void updateHasAboveClientLocked() {
847 hasAboveClient = false;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700848 for (int i=connections.size()-1; i>=0; i--) {
849 ConnectionRecord cr = connections.valueAt(i);
850 if ((cr.flags&Context.BIND_ABOVE_CLIENT) != 0) {
851 hasAboveClient = true;
852 break;
Dianne Hackbornc68c9132011-07-29 01:25:18 -0700853 }
854 }
855 }
856
Dianne Hackbornc8230512013-07-13 21:32:12 -0700857 int modifyRawOomAdj(int adj) {
858 if (hasAboveClient) {
859 // If this process has bound to any services with BIND_ABOVE_CLIENT,
860 // then we need to drop its adjustment to be lower than the service's
861 // in order to honor the request. We want to drop it by one adjustment
862 // level... but there is special meaning applied to various levels so
863 // we will skip some of them.
864 if (adj < ProcessList.FOREGROUND_APP_ADJ) {
865 // System process will not get dropped, ever
866 } else if (adj < ProcessList.VISIBLE_APP_ADJ) {
867 adj = ProcessList.VISIBLE_APP_ADJ;
868 } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) {
869 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Amith Yamasani0567ec62019-01-23 11:11:02 -0800870 } else if (adj < ProcessList.PERCEPTIBLE_LOW_APP_ADJ) {
871 adj = ProcessList.PERCEPTIBLE_LOW_APP_ADJ;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700872 } else if (adj < ProcessList.CACHED_APP_MIN_ADJ) {
873 adj = ProcessList.CACHED_APP_MIN_ADJ;
874 } else if (adj < ProcessList.CACHED_APP_MAX_ADJ) {
875 adj++;
876 }
877 }
878 return adj;
879 }
880
Joe Onorato57190282016-04-20 15:37:49 -0700881 void scheduleCrash(String message) {
882 // Checking killedbyAm should keep it from showing the crash dialog if the process
883 // was already dead for a good / normal reason.
884 if (!killedByAm) {
885 if (thread != null) {
886 if (pid == Process.myPid()) {
887 Slog.w(TAG, "scheduleCrash: trying to crash system process!");
888 return;
889 }
890 long ident = Binder.clearCallingIdentity();
891 try {
892 thread.scheduleCrash(message);
893 } catch (RemoteException e) {
894 // If it's already dead our work is done. If it's wedged just kill it.
895 // We won't get the crash dialog or the error reporting.
Jing Ji8055a3a2019-12-17 15:55:33 -0800896 kill("scheduleCrash for '" + message + "' failed",
897 ApplicationExitInfo.REASON_CRASH, true);
Joe Onorato57190282016-04-20 15:37:49 -0700898 } finally {
899 Binder.restoreCallingIdentity(ident);
900 }
901 }
902 }
903 }
904
Jing Ji8055a3a2019-12-17 15:55:33 -0800905 void kill(String reason, @Reason int reasonCode, boolean noisy) {
906 kill(reason, reasonCode, ApplicationExitInfo.SUBREASON_UNKNOWN, noisy);
907 }
908
909 void kill(String reason, @Reason int reasonCode, @SubReason int subReason, boolean noisy) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700910 if (!killedByAm) {
Yusuke Satocf4b2542015-06-17 00:21:28 -0700911 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "kill");
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -0700912 if (mService != null && (noisy || info.uid == mService.mCurOomAdjUid)) {
913 mService.reportUidInfoMessageLocked(TAG,
914 "Killing " + toShortString() + " (adj " + setAdj + "): " + reason,
915 info.uid);
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700916 }
Sudheer Shankaf6690102017-10-16 10:20:32 -0700917 if (pid > 0) {
Jing Ji8055a3a2019-12-17 15:55:33 -0800918 mService.mProcessList.noteAppKill(this, reasonCode, subReason, reason);
Sudheer Shankaf6690102017-10-16 10:20:32 -0700919 EventLog.writeEvent(EventLogTags.AM_KILL, userId, pid, processName, setAdj, reason);
920 Process.killProcessQuiet(pid);
Amith Yamasani98a00922018-08-21 12:50:30 -0400921 ProcessList.killProcessGroup(uid, pid);
Sudheer Shankaf6690102017-10-16 10:20:32 -0700922 } else {
923 pendingStart = false;
924 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700925 if (!mPersistent) {
Dianne Hackbornd4125632014-09-22 12:52:49 -0700926 killed = true;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700927 killedByAm = true;
928 }
Yusuke Satocf4b2542015-06-17 00:21:28 -0700929 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700930 }
931 }
932
Wale Ogunwale51cc98a2018-10-15 10:41:05 -0700933 @Override
Jeffrey Huangcb782852019-12-05 11:28:11 -0800934 public void dumpDebug(ProtoOutputStream proto, long fieldId) {
935 dumpDebug(proto, fieldId, -1);
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800936 }
937
Jeffrey Huangcb782852019-12-05 11:28:11 -0800938 public void dumpDebug(ProtoOutputStream proto, long fieldId, int lruIndex) {
Yi Jin129fc6c2017-09-28 15:48:38 -0700939 long token = proto.start(fieldId);
940 proto.write(ProcessRecordProto.PID, pid);
941 proto.write(ProcessRecordProto.PROCESS_NAME, processName);
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800942 proto.write(ProcessRecordProto.UID, info.uid);
943 if (UserHandle.getAppId(info.uid) >= Process.FIRST_APPLICATION_UID) {
Yi Jin129fc6c2017-09-28 15:48:38 -0700944 proto.write(ProcessRecordProto.USER_ID, userId);
945 proto.write(ProcessRecordProto.APP_ID, UserHandle.getAppId(info.uid));
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800946 }
947 if (uid != info.uid) {
948 proto.write(ProcessRecordProto.ISOLATED_APP_ID, UserHandle.getAppId(uid));
Yi Jin129fc6c2017-09-28 15:48:38 -0700949 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700950 proto.write(ProcessRecordProto.PERSISTENT, mPersistent);
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -0800951 if (lruIndex >= 0) {
952 proto.write(ProcessRecordProto.LRU_INDEX, lruIndex);
953 }
Yi Jin129fc6c2017-09-28 15:48:38 -0700954 proto.end(token);
955 }
956
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800957 public String toShortString() {
958 if (shortStringName != null) {
959 return shortStringName;
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700960 }
961 StringBuilder sb = new StringBuilder(128);
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800962 toShortString(sb);
963 return shortStringName = sb.toString();
964 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700965
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800966 void toShortString(StringBuilder sb) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700967 sb.append(pid);
968 sb.append(':');
969 sb.append(processName);
970 sb.append('/');
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800971 if (info.uid < Process.FIRST_APPLICATION_UID) {
972 sb.append(uid);
973 } else {
974 sb.append('u');
975 sb.append(userId);
Dianne Hackborna4cc2052013-07-08 17:31:25 -0700976 int appId = UserHandle.getAppId(info.uid);
977 if (appId >= Process.FIRST_APPLICATION_UID) {
978 sb.append('a');
979 sb.append(appId - Process.FIRST_APPLICATION_UID);
980 } else {
981 sb.append('s');
982 sb.append(appId);
983 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800984 if (uid != info.uid) {
985 sb.append('i');
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700986 sb.append(UserHandle.getAppId(uid) - Process.FIRST_ISOLATED_UID);
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800987 }
988 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800989 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -0700990
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800991 public String toString() {
992 if (stringName != null) {
993 return stringName;
994 }
995 StringBuilder sb = new StringBuilder(128);
996 sb.append("ProcessRecord{");
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700997 sb.append(Integer.toHexString(System.identityHashCode(this)));
998 sb.append(' ');
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800999 toShortString(sb);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07001000 sb.append('}');
1001 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
Dianne Hackborn9210bc82013-09-05 12:31:16 -07001003
1004 public String makeAdjReason() {
Dianne Hackborn9210bc82013-09-05 12:31:16 -07001005 if (adjSource != null || adjTarget != null) {
Dianne Hackborn8e692572013-09-10 19:06:15 -07001006 StringBuilder sb = new StringBuilder(128);
Dianne Hackborn9210bc82013-09-05 12:31:16 -07001007 sb.append(' ');
1008 if (adjTarget instanceof ComponentName) {
1009 sb.append(((ComponentName)adjTarget).flattenToShortString());
1010 } else if (adjTarget != null) {
1011 sb.append(adjTarget.toString());
1012 } else {
1013 sb.append("{null}");
1014 }
1015 sb.append("<=");
1016 if (adjSource instanceof ProcessRecord) {
1017 sb.append("Proc{");
1018 sb.append(((ProcessRecord)adjSource).toShortString());
1019 sb.append("}");
1020 } else if (adjSource != null) {
1021 sb.append(adjSource.toString());
1022 } else {
1023 sb.append("{null}");
1024 }
Dianne Hackborn8e692572013-09-10 19:06:15 -07001025 return sb.toString();
Dianne Hackborn9210bc82013-09-05 12:31:16 -07001026 }
Dianne Hackborn8e692572013-09-10 19:06:15 -07001027 return null;
Dianne Hackborn9210bc82013-09-05 12:31:16 -07001028 }
1029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 /*
1031 * Return true if package has been added false if not
1032 */
Dianne Hackborn3accca02013-09-20 09:32:11 -07001033 public boolean addPackage(String pkg, long versionCode, ProcessStatsService tracker) {
Dianne Hackborn78a369c2013-06-11 17:10:32 -07001034 if (!pkgList.containsKey(pkg)) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07001035 ProcessStats.ProcessStateHolder holder = new ProcessStats.ProcessStateHolder(
1036 versionCode);
Dianne Hackborn53459a72013-09-17 17:14:57 -07001037 if (baseProcessTracker != null) {
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -08001038 tracker.updateProcessStateHolderLocked(holder, pkg, info.uid, versionCode,
1039 processName);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001040 pkgList.put(pkg, holder);
1041 if (holder.state != baseProcessTracker) {
1042 holder.state.makeActive();
Dianne Hackborn53459a72013-09-17 17:14:57 -07001043 }
1044 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07001045 pkgList.put(pkg, holder);
Dianne Hackborn53459a72013-09-17 17:14:57 -07001046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 return true;
1048 }
1049 return false;
1050 }
Dianne Hackborn78a369c2013-06-11 17:10:32 -07001051
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001052 public int getSetAdjWithServices() {
Dianne Hackborn20cdcee2013-07-10 18:47:04 -07001053 if (setAdj >= ProcessList.CACHED_APP_MIN_ADJ) {
1054 if (hasStartedServices) {
1055 return ProcessList.SERVICE_B_ADJ;
1056 }
Dianne Hackborn2286cdc2013-07-01 19:10:06 -07001057 }
1058 return setAdj;
1059 }
1060
Dianne Hackborna413dc02013-07-12 12:02:55 -07001061 public void forceProcessStateUpTo(int newState) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001062 if (mRepProcState > newState) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001063 mRepProcState = newState;
1064 setCurProcState(newState);
Amith Yamasani08888c02018-12-07 09:38:25 -08001065 setCurRawProcState(newState);
Chenjie Yubd1a28f2018-07-17 14:55:19 -07001066 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -08001067 FrameworkStatsLog.write(FrameworkStatsLog.PROCESS_STATE_CHANGED,
Chenjie Yubd1a28f2018-07-17 14:55:19 -07001068 uid, processName, pkgList.keyAt(ipkg),
1069 ActivityManager.processStateAmToProto(mRepProcState),
1070 pkgList.valueAt(ipkg).appVersion);
1071 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001072 }
1073 }
1074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 /*
1076 * Delete all packages from list except the package indicated in info
1077 */
Dianne Hackbornd2932242013-08-05 18:18:42 -07001078 public void resetPackageList(ProcessStatsService tracker) {
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -07001079 final int N = pkgList.size();
Dianne Hackborne56c2c32013-09-17 10:56:49 -07001080 if (baseProcessTracker != null) {
1081 long now = SystemClock.uptimeMillis();
1082 baseProcessTracker.setState(ProcessStats.STATE_NOTHING,
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001083 tracker.getMemFactorLocked(), now, pkgList.mPkgList);
Chenjie Yubd1a28f2018-07-17 14:55:19 -07001084 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -08001085 FrameworkStatsLog.write(FrameworkStatsLog.PROCESS_STATE_CHANGED,
Chenjie Yubd1a28f2018-07-17 14:55:19 -07001086 uid, processName, pkgList.keyAt(ipkg),
1087 ActivityManager.processStateAmToProto(ProcessStats.STATE_NOTHING),
1088 pkgList.valueAt(ipkg).appVersion);
1089 }
Dianne Hackborne56c2c32013-09-17 10:56:49 -07001090 if (N != 1) {
1091 for (int i=0; i<N; i++) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001092 ProcessStats.ProcessStateHolder holder = pkgList.valueAt(i);
1093 if (holder.state != null && holder.state != baseProcessTracker) {
1094 holder.state.makeInactive();
Dianne Hackborne56c2c32013-09-17 10:56:49 -07001095 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -07001096
Dianne Hackborne56c2c32013-09-17 10:56:49 -07001097 }
1098 pkgList.clear();
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001099 ProcessStats.ProcessStateHolder holder = new ProcessStats.ProcessStateHolder(
Patrick Baumannc2def582018-04-04 12:14:15 -07001100 info.longVersionCode);
Dianne Hackborn2f55e5a2018-11-30 16:31:31 -08001101 tracker.updateProcessStateHolderLocked(holder, info.packageName, info.uid,
Dianne Hackborn2aec55a2018-06-26 10:35:35 -07001102 info.longVersionCode, processName);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07001103 pkgList.put(info.packageName, holder);
Dianne Hackborn2aec55a2018-06-26 10:35:35 -07001104 if (holder.state != baseProcessTracker) {
1105 holder.state.makeActive();
Dianne Hackborne56c2c32013-09-17 10:56:49 -07001106 }
Dianne Hackbornbe4c1d72013-09-09 09:44:05 -07001107 }
Dianne Hackborne56c2c32013-09-17 10:56:49 -07001108 } else if (N != 1) {
Dianne Hackborncfc837f2013-06-27 18:32:07 -07001109 pkgList.clear();
Patrick Baumannc2def582018-04-04 12:14:15 -07001110 pkgList.put(info.packageName, new ProcessStats.ProcessStateHolder(info.longVersionCode));
Dianne Hackborn78a369c2013-06-11 17:10:32 -07001111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
Chong Zhangfdcc4d42015-10-14 16:50:12 -07001113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 public String[] getPackageList() {
1115 int size = pkgList.size();
1116 if (size == 0) {
1117 return null;
1118 }
1119 String list[] = new String[size];
Dianne Hackborn78a369c2013-06-11 17:10:32 -07001120 for (int i=0; i<pkgList.size(); i++) {
1121 list[i] = pkgList.keyAt(i);
1122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 return list;
1124 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001125
Zimuzo972e1cd2019-01-28 16:30:01 +00001126 public List<VersionedPackage> getPackageListWithVersionCode() {
1127 int size = pkgList.size();
1128 if (size == 0) {
1129 return null;
1130 }
1131 List<VersionedPackage> list = new ArrayList<>();
1132 for (int i = 0; i < pkgList.size(); i++) {
1133 list.add(new VersionedPackage(pkgList.keyAt(i), pkgList.valueAt(i).appVersion));
1134 }
1135 return list;
1136 }
1137
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001138 WindowProcessController getWindowProcessController() {
1139 return mWindowProcessController;
1140 }
1141
1142 void setCurrentSchedulingGroup(int curSchedGroup) {
1143 mCurSchedGroup = curSchedGroup;
1144 mWindowProcessController.setCurrentSchedulingGroup(curSchedGroup);
1145 }
1146
1147 int getCurrentSchedulingGroup() {
1148 return mCurSchedGroup;
1149 }
1150
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001151 void setCurProcState(int curProcState) {
1152 mCurProcState = curProcState;
1153 mWindowProcessController.setCurrentProcState(mCurProcState);
1154 }
1155
1156 int getCurProcState() {
1157 return mCurProcState;
1158 }
1159
Amith Yamasani08888c02018-12-07 09:38:25 -08001160 void setCurRawProcState(int curRawProcState) {
1161 mCurRawProcState = curRawProcState;
1162 }
1163
1164 int getCurRawProcState() {
1165 return mCurRawProcState;
1166 }
1167
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001168 void setReportedProcState(int repProcState) {
1169 mRepProcState = repProcState;
Chenjie Yubd1a28f2018-07-17 14:55:19 -07001170 for (int ipkg = pkgList.size() - 1; ipkg >= 0; ipkg--) {
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -08001171 FrameworkStatsLog.write(FrameworkStatsLog.PROCESS_STATE_CHANGED,
Chenjie Yubd1a28f2018-07-17 14:55:19 -07001172 uid, processName, pkgList.keyAt(ipkg),
1173 ActivityManager.processStateAmToProto(mRepProcState),
1174 pkgList.valueAt(ipkg).appVersion);
1175 }
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001176 mWindowProcessController.setReportedProcState(repProcState);
1177 }
1178
1179 int getReportedProcState() {
1180 return mRepProcState;
1181 }
1182
1183 void setCrashing(boolean crashing) {
1184 mCrashing = crashing;
1185 mWindowProcessController.setCrashing(crashing);
1186 }
1187
1188 boolean isCrashing() {
1189 return mCrashing;
1190 }
1191
1192 void setNotResponding(boolean notResponding) {
1193 mNotResponding = notResponding;
1194 mWindowProcessController.setNotResponding(notResponding);
1195 }
1196
1197 boolean isNotResponding() {
1198 return mNotResponding;
1199 }
1200
1201 void setPersistent(boolean persistent) {
1202 mPersistent = persistent;
1203 mWindowProcessController.setPersistent(persistent);
1204 }
1205
1206 boolean isPersistent() {
1207 return mPersistent;
1208 }
1209
1210 public void setRequiredAbi(String requiredAbi) {
1211 mRequiredAbi = requiredAbi;
1212 mWindowProcessController.setRequiredAbi(requiredAbi);
1213 }
1214
1215 String getRequiredAbi() {
1216 return mRequiredAbi;
1217 }
1218
Amith Yamasania0a30a12019-01-22 11:38:06 -08001219 void setHasForegroundServices(boolean hasForegroundServices, int fgServiceTypes) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001220 mHasForegroundServices = hasForegroundServices;
Amith Yamasania0a30a12019-01-22 11:38:06 -08001221 mFgServiceTypes = fgServiceTypes;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001222 mWindowProcessController.setHasForegroundServices(hasForegroundServices);
1223 }
1224
1225 boolean hasForegroundServices() {
1226 return mHasForegroundServices;
1227 }
1228
Amith Yamasania0a30a12019-01-22 11:38:06 -08001229 boolean hasLocationForegroundServices() {
1230 return mHasForegroundServices
1231 && (mFgServiceTypes & ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION) != 0;
1232 }
1233
Hui Yu26969322019-08-21 14:56:35 -07001234 boolean hasLocationCapability() {
1235 return (setCapability & ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION) != 0;
1236 }
1237
Narayan Kamath396ce722019-03-01 16:53:26 +00001238 int getForegroundServiceTypes() {
1239 return mHasForegroundServices ? mFgServiceTypes : 0;
1240 }
1241
1242 int getReportedForegroundServiceTypes() {
1243 return mRepFgServiceTypes;
1244 }
1245
1246 void setReportedForegroundServiceTypes(int foregroundServiceTypes) {
1247 mRepFgServiceTypes = foregroundServiceTypes;
1248 }
1249
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001250 void setHasForegroundActivities(boolean hasForegroundActivities) {
1251 mHasForegroundActivities = hasForegroundActivities;
1252 mWindowProcessController.setHasForegroundActivities(hasForegroundActivities);
1253 }
1254
1255 boolean hasForegroundActivities() {
1256 return mHasForegroundActivities;
1257 }
1258
1259 void setHasClientActivities(boolean hasClientActivities) {
1260 mHasClientActivities = hasClientActivities;
1261 mWindowProcessController.setHasClientActivities(hasClientActivities);
1262 }
1263
1264 boolean hasClientActivities() {
1265 return mHasClientActivities;
1266 }
1267
1268 void setHasTopUi(boolean hasTopUi) {
1269 mHasTopUi = hasTopUi;
1270 mWindowProcessController.setHasTopUi(hasTopUi);
Tim Murrayd33c6f82020-06-19 16:40:47 +00001271 updateTopUiOrRunningRemoteAnim();
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001272 }
1273
1274 boolean hasTopUi() {
1275 return mHasTopUi;
1276 }
1277
1278 void setHasOverlayUi(boolean hasOverlayUi) {
1279 mHasOverlayUi = hasOverlayUi;
1280 mWindowProcessController.setHasOverlayUi(hasOverlayUi);
1281 }
1282
1283 boolean hasOverlayUi() {
1284 return mHasOverlayUi;
1285 }
1286
1287 void setInteractionEventTime(long interactionEventTime) {
1288 mInteractionEventTime = interactionEventTime;
1289 mWindowProcessController.setInteractionEventTime(interactionEventTime);
1290 }
1291
1292 long getInteractionEventTime() {
1293 return mInteractionEventTime;
1294 }
1295
1296 void setFgInteractionTime(long fgInteractionTime) {
1297 mFgInteractionTime = fgInteractionTime;
1298 mWindowProcessController.setFgInteractionTime(fgInteractionTime);
1299 }
1300
1301 long getFgInteractionTime() {
1302 return mFgInteractionTime;
1303 }
1304
1305 void setWhenUnimportant(long whenUnimportant) {
1306 mWhenUnimportant = whenUnimportant;
1307 mWindowProcessController.setWhenUnimportant(whenUnimportant);
1308 }
1309
1310 long getWhenUnimportant() {
1311 return mWhenUnimportant;
1312 }
1313
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001314 void setDebugging(boolean debugging) {
1315 mDebugging = debugging;
1316 mWindowProcessController.setDebugging(debugging);
1317 }
1318
1319 boolean isDebugging() {
1320 return mDebugging;
1321 }
1322
1323 void setUsingWrapper(boolean usingWrapper) {
1324 mUsingWrapper = usingWrapper;
1325 mWindowProcessController.setUsingWrapper(usingWrapper);
1326 }
1327
1328 boolean isUsingWrapper() {
1329 return mUsingWrapper;
1330 }
1331
Michal Karpinskiac116df2018-12-10 17:51:42 +00001332 void addAllowBackgroundActivityStartsToken(Binder entity) {
Michal Karpinskic8aa91b2019-01-10 16:45:59 +00001333 if (entity == null) return;
Michal Karpinskiac116df2018-12-10 17:51:42 +00001334 mAllowBackgroundActivityStartsTokens.add(entity);
1335 mWindowProcessController.setAllowBackgroundActivityStarts(true);
1336 }
1337
1338 void removeAllowBackgroundActivityStartsToken(Binder entity) {
Michal Karpinskic8aa91b2019-01-10 16:45:59 +00001339 if (entity == null) return;
Michal Karpinskiac116df2018-12-10 17:51:42 +00001340 mAllowBackgroundActivityStartsTokens.remove(entity);
1341 mWindowProcessController.setAllowBackgroundActivityStarts(
1342 !mAllowBackgroundActivityStartsTokens.isEmpty());
1343 }
1344
Alan Stokes8473a2c2019-04-29 13:51:18 +01001345 void addBoundClientUid(int clientUid) {
1346 mBoundClientUids.add(clientUid);
Michal Karpinskib7daac22019-03-25 10:12:41 +00001347 mWindowProcessController.setBoundClientUids(mBoundClientUids);
1348 }
1349
1350 void updateBoundClientUids() {
Darryl L Johnsond02044c2020-04-17 11:52:50 -07001351 if (mServices.isEmpty()) {
Michal Karpinskib7daac22019-03-25 10:12:41 +00001352 clearBoundClientUids();
1353 return;
1354 }
1355 // grab a set of clientUids of all connections of all services
1356 ArraySet<Integer> boundClientUids = new ArraySet<>();
Darryl L Johnsond02044c2020-04-17 11:52:50 -07001357 final int serviceCount = mServices.size();
1358 for (int j = 0; j < serviceCount; j++) {
Michal Karpinskib7daac22019-03-25 10:12:41 +00001359 ArrayMap<IBinder, ArrayList<ConnectionRecord>> conns =
Darryl L Johnsond02044c2020-04-17 11:52:50 -07001360 mServices.valueAt(j).getConnections();
Michal Karpinskib7daac22019-03-25 10:12:41 +00001361 final int N = conns.size();
1362 for (int conni = 0; conni < N; conni++) {
1363 ArrayList<ConnectionRecord> c = conns.valueAt(conni);
1364 for (int i = 0; i < c.size(); i++) {
1365 boundClientUids.add(c.get(i).clientUid);
1366 }
1367 }
1368 }
1369 mBoundClientUids = boundClientUids;
1370 mWindowProcessController.setBoundClientUids(mBoundClientUids);
1371 }
1372
1373 void addBoundClientUidsOfNewService(ServiceRecord sr) {
1374 if (sr == null) {
1375 return;
1376 }
1377 ArrayMap<IBinder, ArrayList<ConnectionRecord>> conns = sr.getConnections();
1378 for (int conni = conns.size() - 1; conni >= 0; conni--) {
1379 ArrayList<ConnectionRecord> c = conns.valueAt(conni);
1380 for (int i = 0; i < c.size(); i++) {
1381 mBoundClientUids.add(c.get(i).clientUid);
1382 }
1383 }
1384 mWindowProcessController.setBoundClientUids(mBoundClientUids);
1385 }
1386
1387 void clearBoundClientUids() {
1388 mBoundClientUids.clear();
1389 mWindowProcessController.setBoundClientUids(mBoundClientUids);
1390 }
1391
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001392 void setActiveInstrumentation(ActiveInstrumentation instr) {
1393 mInstr = instr;
Michal Karpinski4bc56492019-01-31 12:07:33 +00001394 boolean isInstrumenting = instr != null;
1395 mWindowProcessController.setInstrumenting(isInstrumenting,
1396 isInstrumenting && instr.mHasBackgroundActivityStartsPermission);
Wale Ogunwale906f9c62018-07-23 11:23:44 -07001397 }
1398
1399 ActiveInstrumentation getActiveInstrumentation() {
1400 return mInstr;
1401 }
1402
Wale Ogunwale31913b52018-10-13 08:29:31 -07001403 void setCurRawAdj(int curRawAdj) {
1404 mCurRawAdj = curRawAdj;
1405 mWindowProcessController.setPerceptible(curRawAdj <= ProcessList.PERCEPTIBLE_APP_ADJ);
1406 }
1407
1408 int getCurRawAdj() {
1409 return mCurRawAdj;
1410 }
1411
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001412 @Override
1413 public void clearProfilerIfNeeded() {
1414 synchronized (mService) {
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08001415 if (mService.mProfileData.getProfileProc() == null
1416 || mService.mProfileData.getProfilerInfo() == null
1417 || mService.mProfileData.getProfileProc() != this) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001418 return;
1419 }
1420 mService.clearProfilerLocked();
1421 }
1422 }
1423
1424 @Override
1425 public void updateServiceConnectionActivities() {
1426 synchronized (mService) {
1427 mService.mServices.updateServiceConnectionActivitiesLocked(this);
1428 }
1429 }
1430
1431 @Override
1432 public void setPendingUiClean(boolean pendingUiClean) {
1433 synchronized (mService) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001434 mPendingUiClean = pendingUiClean;
1435 mWindowProcessController.setPendingUiClean(pendingUiClean);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001436 }
1437 }
1438
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001439 boolean hasPendingUiClean() {
1440 return mPendingUiClean;
1441 }
1442
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001443 @Override
1444 public void setPendingUiCleanAndForceProcessStateUpTo(int newState) {
1445 synchronized (mService) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -07001446 setPendingUiClean(true);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001447 forceProcessStateUpTo(newState);
1448 }
1449 }
1450
1451 @Override
Riddle Hsu17e38422019-04-12 16:55:11 +08001452 public void updateProcessInfo(boolean updateServiceConnectionActivities, boolean activityChange,
1453 boolean updateOomAdj) {
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001454 synchronized (mService) {
1455 if (updateServiceConnectionActivities) {
1456 mService.mServices.updateServiceConnectionActivitiesLocked(this);
1457 }
Riddle Hsu17e38422019-04-12 16:55:11 +08001458 mService.mProcessList.updateLruProcessLocked(this, activityChange, null /* client */);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001459 if (updateOomAdj) {
Jing Ji094c3ef2019-08-27 17:02:09 -07001460 mService.updateOomAdjLocked(this, OomAdjuster.OOM_ADJ_REASON_ACTIVITY);
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001461 }
1462 }
1463 }
1464
1465 @Override
Riddle Hsuaaef7312019-01-24 19:00:58 +08001466 public boolean isRemoved() {
1467 return removed;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -07001468 }
1469
Wale Ogunwale86b74462018-07-02 08:42:43 -07001470 /**
1471 * Returns the total time (in milliseconds) spent executing in both user and system code.
1472 * Safe to call without lock held.
1473 */
Wale Ogunwale9c103022018-10-18 07:44:54 -07001474 @Override
Wale Ogunwale86b74462018-07-02 08:42:43 -07001475 public long getCpuTime() {
1476 return mService.mProcessCpuTracker.getCpuTimeForPid(pid);
1477 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001478
Wale Ogunwale9c103022018-10-18 07:44:54 -07001479 @Override
Riddle Hsu17e38422019-04-12 16:55:11 +08001480 public void onStartActivity(int topProcessState, boolean setProfileProc, String packageName,
1481 long versionCode) {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001482 synchronized (mService) {
1483 waitingToKill = null;
Wale Ogunwale1f5e53d2018-11-05 05:12:46 -08001484 if (setProfileProc) {
1485 mService.mProfileData.setProfileProc(this);
Wale Ogunwale9c103022018-10-18 07:44:54 -07001486 }
Riddle Hsu17e38422019-04-12 16:55:11 +08001487 if (packageName != null) {
1488 addPackage(packageName, versionCode, mService.mProcessStats);
1489 }
Wale Ogunwale9c103022018-10-18 07:44:54 -07001490
Riddle Hsu17e38422019-04-12 16:55:11 +08001491 // Update oom adj first, we don't want the additional states are involved in this round.
1492 updateProcessInfo(false /* updateServiceConnectionActivities */,
1493 true /* activityChange */, true /* updateOomAdj */);
Wale Ogunwale9c103022018-10-18 07:44:54 -07001494 hasShownUi = true;
1495 setPendingUiClean(true);
1496 forceProcessStateUpTo(topProcessState);
Wale Ogunwale9c103022018-10-18 07:44:54 -07001497 }
1498 }
1499
1500 @Override
Jing Ji8055a3a2019-12-17 15:55:33 -08001501 public void appDied(String reason) {
Wale Ogunwale9c103022018-10-18 07:44:54 -07001502 synchronized (mService) {
Jing Ji8055a3a2019-12-17 15:55:33 -08001503 mService.appDiedLocked(this, reason);
Wale Ogunwale9c103022018-10-18 07:44:54 -07001504 }
1505 }
1506
Jorim Jaggi589c5ba2019-07-30 16:50:13 +02001507 @Override
1508 public void setRunningRemoteAnimation(boolean runningRemoteAnimation) {
1509 if (pid == Process.myPid()) {
1510 Slog.wtf(TAG, "system can't run remote animation");
1511 return;
1512 }
1513 synchronized (mService) {
1514 if (this.runningRemoteAnimation == runningRemoteAnimation) {
1515 return;
1516 }
1517 this.runningRemoteAnimation = runningRemoteAnimation;
1518 if (DEBUG_OOM_ADJ) {
1519 Slog.i(TAG, "Setting runningRemoteAnimation=" + runningRemoteAnimation
1520 + " for pid=" + pid);
1521 }
Tim Murrayd33c6f82020-06-19 16:40:47 +00001522 updateTopUiOrRunningRemoteAnim();
Jorim Jaggi589c5ba2019-07-30 16:50:13 +02001523 mService.updateOomAdjLocked(this, true, OomAdjuster.OOM_ADJ_REASON_UI_VISIBILITY);
1524 }
1525 }
1526
Tim Murrayd33c6f82020-06-19 16:40:47 +00001527 void updateTopUiOrRunningRemoteAnim() {
1528 if (runningRemoteAnimation || hasTopUi()) {
1529 mService.addTopUiOrRunningRemoteAnim(this);
1530 } else {
1531 mService.removeTopUiOrRunningRemoteAnim(this);
1532 }
1533 }
1534
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001535 public long getInputDispatchingTimeout() {
1536 return mWindowProcessController.getInputDispatchingTimeout();
1537 }
1538
Yang Lu732d6382018-11-05 07:53:12 -08001539 public int getProcessClassEnum() {
1540 if (pid == MY_PID) {
1541 return ServerProtoEnums.SYSTEM_SERVER;
1542 }
1543 if (info == null) {
1544 return ServerProtoEnums.ERROR_SOURCE_UNKNOWN;
1545 }
1546 return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? ServerProtoEnums.SYSTEM_APP :
1547 ServerProtoEnums.DATA_APP;
1548 }
1549
Yunfan Chen585f2932019-01-29 16:04:45 +09001550 /**
1551 * Unless configured otherwise, swallow ANRs in background processes & kill the process.
1552 * Non-private access is for tests only.
1553 */
1554 @VisibleForTesting
1555 boolean isSilentAnr() {
1556 return !getShowBackground() && !isInterestingForBackgroundTraces();
1557 }
1558
1559 /** Non-private access is for tests only. */
1560 @VisibleForTesting
1561 List<ProcessRecord> getLruProcessList() {
1562 return mService.mProcessList.mLruProcesses;
1563 }
1564
1565 /** Non-private access is for tests only. */
1566 @VisibleForTesting
1567 boolean isMonitorCpuUsage() {
1568 return mService.MONITOR_CPU_USAGE;
1569 }
1570
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001571 void appNotResponding(String activityShortComponentName, ApplicationInfo aInfo,
1572 String parentShortComponentName, WindowProcessController parentProcess,
Riddle Hsuf9a2c04f2020-03-26 20:22:22 +08001573 boolean aboveSystem, String annotation, boolean onlyDumpSelf) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001574 ArrayList<Integer> firstPids = new ArrayList<>(5);
1575 SparseArray<Boolean> lastPids = new SparseArray<>(20);
1576
Jing Ji8055a3a2019-12-17 15:55:33 -08001577 mWindowProcessController.appEarlyNotResponding(annotation, () -> kill("anr",
1578 ApplicationExitInfo.REASON_ANR, true));
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001579
1580 long anrTime = SystemClock.uptimeMillis();
Yunfan Chen585f2932019-01-29 16:04:45 +09001581 if (isMonitorCpuUsage()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001582 mService.updateCpuStatsNow();
1583 }
1584
Riddle Hsuf9a2c04f2020-03-26 20:22:22 +08001585 final boolean isSilentAnr;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001586 synchronized (mService) {
1587 // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
Wale Ogunwale387b34c2018-10-25 19:59:40 -07001588 if (mService.mAtmInternal.isShuttingDown()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001589 Slog.i(TAG, "During shutdown skipping ANR: " + this + " " + annotation);
1590 return;
1591 } else if (isNotResponding()) {
1592 Slog.i(TAG, "Skipping duplicate ANR: " + this + " " + annotation);
1593 return;
1594 } else if (isCrashing()) {
1595 Slog.i(TAG, "Crashing app skipping ANR: " + this + " " + annotation);
1596 return;
1597 } else if (killedByAm) {
1598 Slog.i(TAG, "App already killed by AM skipping ANR: " + this + " " + annotation);
1599 return;
1600 } else if (killed) {
1601 Slog.i(TAG, "Skipping died app ANR: " + this + " " + annotation);
1602 return;
1603 }
1604
1605 // In case we come through here for the same app before completing
1606 // this one, mark as anring now so we will bail out.
1607 setNotResponding(true);
1608
1609 // Log the ANR to the event log.
1610 EventLog.writeEvent(EventLogTags.AM_ANR, userId, pid, processName, info.flags,
1611 annotation);
1612
1613 // Dump thread traces as quickly as we can, starting with "interesting" processes.
1614 firstPids.add(pid);
1615
Riddle Hsuf9a2c04f2020-03-26 20:22:22 +08001616 // Don't dump other PIDs if it's a background ANR or is requested to only dump self.
1617 isSilentAnr = isSilentAnr();
1618 if (!isSilentAnr && !onlyDumpSelf) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001619 int parentPid = pid;
1620 if (parentProcess != null && parentProcess.getPid() > 0) {
1621 parentPid = parentProcess.getPid();
1622 }
1623 if (parentPid != pid) firstPids.add(parentPid);
1624
1625 if (MY_PID != pid && MY_PID != parentPid) firstPids.add(MY_PID);
1626
Yunfan Chen585f2932019-01-29 16:04:45 +09001627 for (int i = getLruProcessList().size() - 1; i >= 0; i--) {
1628 ProcessRecord r = getLruProcessList().get(i);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001629 if (r != null && r.thread != null) {
1630 int myPid = r.pid;
1631 if (myPid > 0 && myPid != pid && myPid != parentPid && myPid != MY_PID) {
1632 if (r.isPersistent()) {
1633 firstPids.add(myPid);
1634 if (DEBUG_ANR) Slog.i(TAG, "Adding persistent proc: " + r);
1635 } else if (r.treatLikeActivity) {
1636 firstPids.add(myPid);
1637 if (DEBUG_ANR) Slog.i(TAG, "Adding likely IME: " + r);
1638 } else {
1639 lastPids.put(myPid, Boolean.TRUE);
1640 if (DEBUG_ANR) Slog.i(TAG, "Adding ANR proc: " + r);
1641 }
1642 }
1643 }
1644 }
1645 }
1646 }
1647
1648 // Log the ANR to the main log.
1649 StringBuilder info = new StringBuilder();
1650 info.setLength(0);
1651 info.append("ANR in ").append(processName);
1652 if (activityShortComponentName != null) {
1653 info.append(" (").append(activityShortComponentName).append(")");
1654 }
1655 info.append("\n");
1656 info.append("PID: ").append(pid).append("\n");
1657 if (annotation != null) {
1658 info.append("Reason: ").append(annotation).append("\n");
1659 }
1660 if (parentShortComponentName != null
1661 && parentShortComponentName.equals(activityShortComponentName)) {
1662 info.append("Parent: ").append(parentShortComponentName).append("\n");
1663 }
1664
Simon MacMullen30f04a72020-02-05 10:57:40 +00001665 StringBuilder report = new StringBuilder();
1666 report.append(MemoryPressureUtil.currentPsiState());
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001667 ProcessCpuTracker processCpuTracker = new ProcessCpuTracker(true);
1668
1669 // don't dump native PIDs for background ANRs unless it is the process of interest
1670 String[] nativeProcs = null;
Riddle Hsuf9a2c04f2020-03-26 20:22:22 +08001671 if (isSilentAnr || onlyDumpSelf) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001672 for (int i = 0; i < NATIVE_STACKS_OF_INTEREST.length; i++) {
1673 if (NATIVE_STACKS_OF_INTEREST[i].equals(processName)) {
1674 nativeProcs = new String[] { processName };
1675 break;
1676 }
1677 }
1678 } else {
1679 nativeProcs = NATIVE_STACKS_OF_INTEREST;
1680 }
1681
1682 int[] pids = nativeProcs == null ? null : Process.getPidsForCommands(nativeProcs);
1683 ArrayList<Integer> nativePids = null;
1684
1685 if (pids != null) {
1686 nativePids = new ArrayList<>(pids.length);
1687 for (int i : pids) {
1688 nativePids.add(i);
1689 }
1690 }
1691
1692 // For background ANRs, don't pass the ProcessCpuTracker to
1693 // avoid spending 1/2 second collecting stats to rank lastPids.
Simon MacMullen46755e22020-02-04 14:03:37 +00001694 StringWriter tracesFileException = new StringWriter();
Jing Ji56006fd2020-03-08 00:32:33 -08001695 // To hold the start and end offset to the ANR trace file respectively.
1696 final long[] offsets = new long[2];
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001697 File tracesFile = ActivityManagerService.dumpStackTraces(firstPids,
Riddle Hsuf9a2c04f2020-03-26 20:22:22 +08001698 isSilentAnr ? null : processCpuTracker, isSilentAnr ? null : lastPids,
Jing Ji56006fd2020-03-08 00:32:33 -08001699 nativePids, tracesFileException, offsets);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001700
Yunfan Chen585f2932019-01-29 16:04:45 +09001701 if (isMonitorCpuUsage()) {
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001702 mService.updateCpuStatsNow();
1703 synchronized (mService.mProcessCpuTracker) {
Simon MacMullen46755e22020-02-04 14:03:37 +00001704 report.append(mService.mProcessCpuTracker.printCurrentState(anrTime));
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001705 }
1706 info.append(processCpuTracker.printCurrentLoad());
Simon MacMullen46755e22020-02-04 14:03:37 +00001707 info.append(report);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001708 }
Simon MacMullen46755e22020-02-04 14:03:37 +00001709 report.append(tracesFileException.getBuffer());
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001710
1711 info.append(processCpuTracker.printCurrentState(anrTime));
1712
1713 Slog.e(TAG, info.toString());
1714 if (tracesFile == null) {
1715 // There is no trace file, so dump (only) the alleged culprit's threads to the log
1716 Process.sendSignal(pid, Process.SIGNAL_QUIT);
Jing Ji56006fd2020-03-08 00:32:33 -08001717 } else if (offsets[1] > 0) {
1718 // We've dumped into the trace file successfully
1719 mService.mProcessList.mAppExitInfoTracker.scheduleLogAnrTrace(
1720 pid, uid, getPackageList(), tracesFile, offsets[0], offsets[1]);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001721 }
1722
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -08001723 FrameworkStatsLog.write(FrameworkStatsLog.ANR_OCCURRED, uid, processName,
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001724 activityShortComponentName == null ? "unknown": activityShortComponentName,
1725 annotation,
1726 (this.info != null) ? (this.info.isInstantApp()
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -08001727 ? FrameworkStatsLog.ANROCCURRED__IS_INSTANT_APP__TRUE
1728 : FrameworkStatsLog.ANROCCURRED__IS_INSTANT_APP__FALSE)
1729 : FrameworkStatsLog.ANROCCURRED__IS_INSTANT_APP__UNAVAILABLE,
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001730 isInterestingToUserLocked()
Muhammad Qureshiedd80fd2020-01-27 17:23:07 -08001731 ? FrameworkStatsLog.ANROCCURRED__FOREGROUND_STATE__FOREGROUND
1732 : FrameworkStatsLog.ANROCCURRED__FOREGROUND_STATE__BACKGROUND,
Yang Lu732d6382018-11-05 07:53:12 -08001733 getProcessClassEnum(),
1734 (this.info != null) ? this.info.packageName : "");
Wale Ogunwale97b2b742018-10-18 08:08:54 -07001735 final ProcessRecord parentPr = parentProcess != null
1736 ? (ProcessRecord) parentProcess.mOwner : null;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001737 mService.addErrorToDropBox("anr", this, processName, activityShortComponentName,
Simon MacMullen46755e22020-02-04 14:03:37 +00001738 parentShortComponentName, parentPr, annotation, report.toString(), tracesFile,
1739 null);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001740
Jing Ji8055a3a2019-12-17 15:55:33 -08001741 if (mWindowProcessController.appNotResponding(info.toString(), () -> kill("anr",
1742 ApplicationExitInfo.REASON_ANR, true),
Wale Ogunwalee2172292018-10-25 10:11:10 -07001743 () -> {
1744 synchronized (mService) {
1745 mService.mServices.scheduleServiceTimeoutLocked(this);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001746 }
Wale Ogunwalee2172292018-10-25 10:11:10 -07001747 })) {
1748 return;
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001749 }
1750
1751 synchronized (mService) {
Yunfan Chen585f2932019-01-29 16:04:45 +09001752 // mBatteryStatsService can be null if the AMS is constructed with injector only. This
1753 // will only happen in tests.
1754 if (mService.mBatteryStatsService != null) {
1755 mService.mBatteryStatsService.noteProcessAnr(processName, uid);
1756 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001757
Hui Yu8e3e4452019-04-11 10:57:51 -07001758 if (isSilentAnr() && !isDebugging()) {
Jing Ji8055a3a2019-12-17 15:55:33 -08001759 kill("bg anr", ApplicationExitInfo.REASON_ANR, true);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001760 return;
1761 }
1762
1763 // Set the app's notResponding state, and look up the errorReportReceiver
1764 makeAppNotRespondingLocked(activityShortComponentName,
1765 annotation != null ? "ANR " + annotation : "ANR", info.toString());
1766
Yunfan Chen585f2932019-01-29 16:04:45 +09001767 // mUiHandler can be null if the AMS is constructed with injector only. This will only
1768 // happen in tests.
1769 if (mService.mUiHandler != null) {
1770 // Bring up the infamous App Not Responding dialog
1771 Message msg = Message.obtain();
1772 msg.what = ActivityManagerService.SHOW_NOT_RESPONDING_UI_MSG;
1773 msg.obj = new AppNotRespondingDialog.Data(this, aInfo, aboveSystem);
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001774
Yunfan Chen585f2932019-01-29 16:04:45 +09001775 mService.mUiHandler.sendMessage(msg);
1776 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001777 }
1778 }
1779
1780 private void makeAppNotRespondingLocked(String activity, String shortMsg, String longMsg) {
1781 setNotResponding(true);
Yunfan Chen585f2932019-01-29 16:04:45 +09001782 // mAppErrors can be null if the AMS is constructed with injector only. This will only
1783 // happen in tests.
1784 if (mService.mAppErrors != null) {
1785 notRespondingReport = mService.mAppErrors.generateProcessError(this,
1786 ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
1787 activity, shortMsg, longMsg, null);
1788 }
Wale Ogunwale51cc98a2018-10-15 10:41:05 -07001789 startAppProblemLocked();
1790 getWindowProcessController().stopFreezingActivities();
1791 }
1792
1793 void startAppProblemLocked() {
1794 // If this app is not running under the current user, then we can't give it a report button
1795 // because that would require launching the report UI under a different user.
1796 errorReportReceiver = null;
1797
1798 for (int userId : mService.mUserController.getCurrentProfileIds()) {
1799 if (this.userId == userId) {
1800 errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
1801 mService.mContext, info.packageName, info.flags);
1802 }
1803 }
1804 mService.skipCurrentReceiverLocked(this);
1805 }
1806
1807 private boolean isInterestingForBackgroundTraces() {
1808 // The system_server is always considered interesting.
1809 if (pid == MY_PID) {
1810 return true;
1811 }
1812
1813 // A package is considered interesting if any of the following is true :
1814 //
1815 // - It's displaying an activity.
1816 // - It's the SystemUI.
1817 // - It has an overlay or a top UI visible.
1818 //
1819 // NOTE: The check whether a given ProcessRecord belongs to the systemui
1820 // process is a bit of a kludge, but the same pattern seems repeated at
1821 // several places in the system server.
1822 return isInterestingToUserLocked() ||
1823 (info != null && "com.android.systemui".equals(info.packageName))
1824 || (hasTopUi() || hasOverlayUi());
1825 }
Yunfan Chen585f2932019-01-29 16:04:45 +09001826
1827 private boolean getShowBackground() {
1828 return Settings.Secure.getInt(mService.mContext.getContentResolver(),
1829 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
1830 }
Jing Ji094c3ef2019-08-27 17:02:09 -07001831
1832 void resetCachedInfo() {
1833 mCachedHasActivities = VALUE_INVALID;
1834 mCachedIsHeavyWeight = VALUE_INVALID;
1835 mCachedHasVisibleActivities = VALUE_INVALID;
1836 mCachedIsHomeProcess = VALUE_INVALID;
1837 mCachedIsPreviousProcess = VALUE_INVALID;
1838 mCachedHasRecentTasks = VALUE_INVALID;
1839 mCachedIsReceivingBroadcast = VALUE_INVALID;
1840 mCachedAdj = ProcessList.INVALID_ADJ;
1841 mCachedForegroundActivities = false;
1842 mCachedProcState = ActivityManager.PROCESS_STATE_CACHED_EMPTY;
1843 mCachedSchedGroup = ProcessList.SCHED_GROUP_BACKGROUND;
1844 }
1845
1846 boolean getCachedHasActivities() {
1847 if (mCachedHasActivities == VALUE_INVALID) {
1848 mCachedHasActivities = getWindowProcessController().hasActivities() ? VALUE_TRUE
1849 : VALUE_FALSE;
1850 }
1851 return mCachedHasActivities == VALUE_TRUE;
1852 }
1853
1854 boolean getCachedIsHeavyWeight() {
1855 if (mCachedIsHeavyWeight == VALUE_INVALID) {
1856 mCachedIsHeavyWeight = mService.mAtmInternal.isHeavyWeightProcess(
1857 getWindowProcessController()) ? VALUE_TRUE : VALUE_FALSE;
1858 }
1859 return mCachedIsHeavyWeight == VALUE_TRUE;
1860 }
1861
1862 boolean getCachedHasVisibleActivities() {
1863 if (mCachedHasVisibleActivities == VALUE_INVALID) {
1864 mCachedHasVisibleActivities = getWindowProcessController().hasVisibleActivities()
1865 ? VALUE_TRUE : VALUE_FALSE;
1866 }
1867 return mCachedHasVisibleActivities == VALUE_TRUE;
1868 }
1869
1870 boolean getCachedIsHomeProcess() {
1871 if (mCachedIsHomeProcess == VALUE_INVALID) {
1872 mCachedIsHomeProcess = getWindowProcessController().isHomeProcess()
1873 ? VALUE_TRUE : VALUE_FALSE;
1874 }
1875 return mCachedIsHomeProcess == VALUE_TRUE;
1876 }
1877
1878 boolean getCachedIsPreviousProcess() {
1879 if (mCachedIsPreviousProcess == VALUE_INVALID) {
1880 mCachedIsPreviousProcess = getWindowProcessController().isPreviousProcess()
1881 ? VALUE_TRUE : VALUE_FALSE;
1882 }
1883 return mCachedIsPreviousProcess == VALUE_TRUE;
1884 }
1885
1886 boolean getCachedHasRecentTasks() {
1887 if (mCachedHasRecentTasks == VALUE_INVALID) {
1888 mCachedHasRecentTasks = getWindowProcessController().hasRecentTasks()
1889 ? VALUE_TRUE : VALUE_FALSE;
1890 }
1891 return mCachedHasRecentTasks == VALUE_TRUE;
1892 }
1893
1894 boolean getCachedIsReceivingBroadcast(ArraySet<BroadcastQueue> tmpQueue) {
1895 if (mCachedIsReceivingBroadcast == VALUE_INVALID) {
1896 tmpQueue.clear();
1897 mCachedIsReceivingBroadcast = mService.isReceivingBroadcastLocked(this, tmpQueue)
1898 ? VALUE_TRUE : VALUE_FALSE;
1899 if (mCachedIsReceivingBroadcast == VALUE_TRUE) {
1900 mCachedSchedGroup = tmpQueue.contains(mService.mFgBroadcastQueue)
1901 ? ProcessList.SCHED_GROUP_DEFAULT : ProcessList.SCHED_GROUP_BACKGROUND;
1902 }
1903 }
1904 return mCachedIsReceivingBroadcast == VALUE_TRUE;
1905 }
1906
1907 void computeOomAdjFromActivitiesIfNecessary(OomAdjuster.ComputeOomAdjWindowCallback callback,
1908 int adj, boolean foregroundActivities, int procState, int schedGroup, int appUid,
1909 int logUid, int processCurTop) {
1910 if (mCachedAdj != ProcessList.INVALID_ADJ) {
1911 return;
1912 }
1913 callback.initialize(this, adj, foregroundActivities, procState, schedGroup, appUid, logUid,
1914 processCurTop);
1915 final int minLayer = getWindowProcessController().computeOomAdjFromActivities(
1916 ProcessList.VISIBLE_APP_LAYER_MAX, callback);
1917
1918 mCachedAdj = callback.adj;
1919 mCachedForegroundActivities = callback.foregroundActivities;
1920 mCachedProcState = callback.procState;
1921 mCachedSchedGroup = callback.schedGroup;
1922
1923 if (mCachedAdj == ProcessList.VISIBLE_APP_ADJ) {
1924 mCachedAdj += minLayer;
1925 }
1926 }
Charles Chen173ae782019-11-11 20:39:02 +08001927
1928 ErrorDialogController getDialogController() {
1929 return mDialogController;
1930 }
1931
1932 /** A controller to generate error dialogs in {@link ProcessRecord} */
1933 class ErrorDialogController {
1934 /** dialogs being displayed due to crash */
1935 private List<AppErrorDialog> mCrashDialogs;
1936 /** dialogs being displayed due to app not responding */
1937 private List<AppNotRespondingDialog> mAnrDialogs;
1938 /** dialogs displayed due to strict mode violation */
1939 private List<StrictModeViolationDialog> mViolationDialogs;
1940 /** current wait for debugger dialog */
1941 private AppWaitingForDebuggerDialog mWaitDialog;
1942
Charles Chen173ae782019-11-11 20:39:02 +08001943 boolean hasCrashDialogs() {
1944 return mCrashDialogs != null;
1945 }
1946
1947 boolean hasAnrDialogs() {
1948 return mAnrDialogs != null;
1949 }
1950
1951 boolean hasViolationDialogs() {
1952 return mViolationDialogs != null;
1953 }
1954
1955 boolean hasDebugWaitingDialog() {
1956 return mWaitDialog != null;
1957 }
1958
1959 void clearAllErrorDialogs() {
1960 clearCrashDialogs();
1961 clearAnrDialogs();
1962 clearViolationDialogs();
1963 clearWaitingDialog();
1964 }
1965
1966 void clearCrashDialogs() {
1967 clearCrashDialogs(true /* needDismiss */);
1968 }
1969
1970 void clearCrashDialogs(boolean needDismiss) {
1971 if (mCrashDialogs == null) {
1972 return;
1973 }
1974 if (needDismiss) {
1975 forAllDialogs(mCrashDialogs, Dialog::dismiss);
1976 }
1977 mCrashDialogs = null;
1978 }
1979
1980 void clearAnrDialogs() {
1981 if (mAnrDialogs == null) {
1982 return;
1983 }
1984 forAllDialogs(mAnrDialogs, Dialog::dismiss);
1985 mAnrDialogs = null;
1986 }
1987
1988 void clearViolationDialogs() {
1989 if (mViolationDialogs == null) {
1990 return;
1991 }
1992 forAllDialogs(mViolationDialogs, Dialog::dismiss);
1993 mViolationDialogs = null;
1994 }
1995
1996 void clearWaitingDialog() {
1997 if (mWaitDialog == null) {
1998 return;
1999 }
2000 mWaitDialog.dismiss();
2001 mWaitDialog = null;
2002 }
2003
Charles Chence7c8072020-02-03 16:10:14 +08002004 void forAllDialogs(List<? extends BaseErrorDialog> dialogs, Consumer<BaseErrorDialog> c) {
Charles Chen173ae782019-11-11 20:39:02 +08002005 for (int i = dialogs.size() - 1; i >= 0; i--) {
2006 c.accept(dialogs.get(i));
2007 }
2008 }
2009
2010 void showCrashDialogs(AppErrorDialog.Data data) {
2011 List<Context> contexts = getDisplayContexts(false /* lastUsedOnly */);
2012 mCrashDialogs = new ArrayList<>();
Charles Chen173ae782019-11-11 20:39:02 +08002013 for (int i = contexts.size() - 1; i >= 0; i--) {
2014 final Context c = contexts.get(i);
2015 mCrashDialogs.add(new AppErrorDialog(c, mService, data));
2016 }
Charles Chence7c8072020-02-03 16:10:14 +08002017 mService.mUiHandler.post(() -> {
2018 List<AppErrorDialog> dialogs;
2019 synchronized (mService) {
2020 dialogs = mCrashDialogs;
2021 }
2022 if (dialogs != null) {
2023 forAllDialogs(dialogs, Dialog::show);
2024 }
2025 });
Charles Chen173ae782019-11-11 20:39:02 +08002026 }
2027
2028 void showAnrDialogs(AppNotRespondingDialog.Data data) {
2029 List<Context> contexts = getDisplayContexts(isSilentAnr() /* lastUsedOnly */);
2030 mAnrDialogs = new ArrayList<>();
Charles Chen173ae782019-11-11 20:39:02 +08002031 for (int i = contexts.size() - 1; i >= 0; i--) {
2032 final Context c = contexts.get(i);
2033 mAnrDialogs.add(new AppNotRespondingDialog(mService, c, data));
2034 }
Charles Chence7c8072020-02-03 16:10:14 +08002035 mService.mUiHandler.post(() -> {
2036 List<AppNotRespondingDialog> dialogs;
2037 synchronized (mService) {
2038 dialogs = mAnrDialogs;
2039 }
2040 if (dialogs != null) {
2041 forAllDialogs(dialogs, Dialog::show);
2042 }
2043 });
Charles Chen173ae782019-11-11 20:39:02 +08002044 }
2045
2046 void showViolationDialogs(AppErrorResult res) {
2047 List<Context> contexts = getDisplayContexts(false /* lastUsedOnly */);
2048 mViolationDialogs = new ArrayList<>();
Charles Chen173ae782019-11-11 20:39:02 +08002049 for (int i = contexts.size() - 1; i >= 0; i--) {
2050 final Context c = contexts.get(i);
2051 mViolationDialogs.add(
2052 new StrictModeViolationDialog(c, mService, res, ProcessRecord.this));
2053 }
Charles Chence7c8072020-02-03 16:10:14 +08002054 mService.mUiHandler.post(() -> {
2055 List<StrictModeViolationDialog> dialogs;
2056 synchronized (mService) {
2057 dialogs = mViolationDialogs;
2058 }
2059 if (dialogs != null) {
2060 forAllDialogs(dialogs, Dialog::show);
2061 }
2062 });
Charles Chen173ae782019-11-11 20:39:02 +08002063 }
2064
2065 void showDebugWaitingDialogs() {
2066 List<Context> contexts = getDisplayContexts(true /* lastUsedOnly */);
2067 final Context c = contexts.get(0);
2068 mWaitDialog = new AppWaitingForDebuggerDialog(mService, c, ProcessRecord.this);
Charles Chence7c8072020-02-03 16:10:14 +08002069
2070 mService.mUiHandler.post(() -> {
2071 Dialog dialog;
2072 synchronized (mService) {
2073 dialog = mWaitDialog;
2074 }
2075 if (dialog != null) {
2076 dialog.show();
2077 }
2078 });
Charles Chen173ae782019-11-11 20:39:02 +08002079 }
2080
2081 /**
2082 * Helper function to collect contexts from crashed app located displays
2083 *
2084 * @param lastUsedOnly Sets to {@code true} to indicate to only get last used context.
2085 * Sets to {@code false} to collect contexts from crashed app located
2086 * displays.
2087 *
2088 * @return display context list
2089 */
2090 private List<Context> getDisplayContexts(boolean lastUsedOnly) {
2091 List<Context> displayContexts = new ArrayList<>();
2092 if (!lastUsedOnly) {
2093 mWindowProcessController.getDisplayContextsWithErrorDialogs(displayContexts);
2094 }
2095 // If there is no foreground window display, fallback to last used display.
2096 if (displayContexts.isEmpty() || lastUsedOnly) {
Riddle Hsu03c86422020-01-17 22:09:17 +08002097 displayContexts.add(mService.mWmInternal != null
2098 ? mService.mWmInternal.getTopFocusedDisplayUiContext()
2099 : mService.mUiContext);
Charles Chen173ae782019-11-11 20:39:02 +08002100 }
2101 return displayContexts;
2102 }
2103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104}