| 1 |
/* |
| 2 |
* Copyright (C) 2022 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.server.art; |
| 18 |
|
| 19 |
import static com.android.server.art.DexUseManagerLocal.DexLoader; |
| 20 |
import static com.android.server.art.DexUseManagerLocal.SecondaryDexInfo; |
| 21 |
import static com.android.server.art.model.DexoptStatus.DexContainerFileDexoptStatus; |
| 22 |
|
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.os.Build; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.os.ServiceSpecificException; |
| 27 |
import android.util.Log; |
| 28 |
|
| 29 |
import androidx.annotation.RequiresApi; |
| 30 |
|
| 31 |
import com.android.internal.annotations.VisibleForTesting; |
| 32 |
import com.android.server.LocalManagerRegistry; |
| 33 |
import com.android.server.pm.PackageManagerLocal; |
| 34 |
import com.android.server.pm.pkg.PackageState; |
| 35 |
|
| 36 |
import dalvik.system.VMRuntime; |
| 37 |
|
| 38 |
import java.io.PrintWriter; |
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.Comparator; |
| 41 |
import java.util.LinkedHashMap; |
| 42 |
import java.util.List; |
| 43 |
import java.util.Map; |
| 44 |
import java.util.Objects; |
| 45 |
import java.util.Set; |
| 46 |
import java.util.TreeMap; |
| 47 |
import java.util.function.Function; |
| 48 |
import java.util.stream.Collectors; |
| 49 |
|
| 50 |
/** |
| 51 |
* A helper class to handle dump. |
| 52 |
* |
| 53 |
* @hide |
| 54 |
*/ |
| 55 |
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) |
| 56 |
public class DumpHelper { |
| 57 |
private static final String TAG = ArtManagerLocal.TAG; |
| 58 |
|
| 59 |
@NonNull private final Injector mInjector; |
| 60 |
|
| 61 |
public DumpHelper(@NonNull ArtManagerLocal artManagerLocal) { |
| 62 |
this(new Injector(artManagerLocal)); |
| 63 |
} |
| 64 |
|
| 65 |
@VisibleForTesting |
| 66 |
public DumpHelper(@NonNull Injector injector) { |
| 67 |
mInjector = injector; |
| 68 |
} |
| 69 |
|
| 70 |
/** Handles {@link ArtManagerLocal#dump(PrintWriter, PackageManagerLocal.FilteredSnapshot)}. */ |
| 71 |
public void dump( |
| 72 |
@NonNull PrintWriter pw, @NonNull PackageManagerLocal.FilteredSnapshot snapshot) { |
| 73 |
snapshot.getPackageStates() |
| 74 |
.values() |
| 75 |
.stream() |
| 76 |
.sorted(Comparator.comparing(PackageState::getPackageName)) |
| 77 |
.forEach(pkgState -> dumpPackage(pw, snapshot, pkgState)); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Handles {@link |
| 82 |
* ArtManagerLocal#dumpPackage(PrintWriter, PackageManagerLocal.FilteredSnapshot, String)}. |
| 83 |
*/ |
| 84 |
public void dumpPackage(@NonNull PrintWriter pw, |
| 85 |
@NonNull PackageManagerLocal.FilteredSnapshot snapshot, |
| 86 |
@NonNull PackageState pkgState) { |
| 87 |
// An APEX has a uid of -1. |
| 88 |
// TODO(b/256637152): Consider using `isApex` instead. |
| 89 |
if (pkgState.getAppId() <= 0 || pkgState.getAndroidPackage() == null) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
var ipw = new IndentingPrintWriter(pw); |
| 94 |
|
| 95 |
String packageName = pkgState.getPackageName(); |
| 96 |
ipw.printf("[%s]\n", packageName); |
| 97 |
|
| 98 |
List<DexContainerFileDexoptStatus> statuses = |
| 99 |
mInjector.getArtManagerLocal() |
| 100 |
.getDexoptStatus(snapshot, packageName) |
| 101 |
.getDexContainerFileDexoptStatuses(); |
| 102 |
Map<String, SecondaryDexInfo> secondaryDexInfoByDexPath = |
| 103 |
mInjector.getDexUseManager() |
| 104 |
.getSecondaryDexInfo(packageName) |
| 105 |
.stream() |
| 106 |
.collect(Collectors.toMap(SecondaryDexInfo::dexPath, Function.identity())); |
| 107 |
|
| 108 |
// Use LinkedHashMap to keep the order. They are ordered by their split indexes. |
| 109 |
var primaryStatusesByDexPath = |
| 110 |
new LinkedHashMap<String, List<DexContainerFileDexoptStatus>>(); |
| 111 |
// Use TreeMap to force lexicographical order. |
| 112 |
var secondaryStatusesByDexPath = new TreeMap<String, List<DexContainerFileDexoptStatus>>(); |
| 113 |
for (DexContainerFileDexoptStatus fileStatus : statuses) { |
| 114 |
if (fileStatus.isPrimaryDex()) { |
| 115 |
primaryStatusesByDexPath |
| 116 |
.computeIfAbsent(fileStatus.getDexContainerFile(), k -> new ArrayList<>()) |
| 117 |
.add(fileStatus); |
| 118 |
} else if (secondaryDexInfoByDexPath.containsKey(fileStatus.getDexContainerFile())) { |
| 119 |
// The condition above is false only if a change occurs between |
| 120 |
// `getDexoptStatus` and `getSecondaryDexInfo`, which is an edge case. |
| 121 |
secondaryStatusesByDexPath |
| 122 |
.computeIfAbsent(fileStatus.getDexContainerFile(), k -> new ArrayList<>()) |
| 123 |
.add(fileStatus); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
ipw.increaseIndent(); |
| 128 |
for (List<DexContainerFileDexoptStatus> fileStatuses : primaryStatusesByDexPath.values()) { |
| 129 |
dumpPrimaryDex(ipw, snapshot, fileStatuses, packageName); |
| 130 |
} |
| 131 |
if (!secondaryStatusesByDexPath.isEmpty()) { |
| 132 |
ipw.println("known secondary dex files:"); |
| 133 |
ipw.increaseIndent(); |
| 134 |
for (Map.Entry<String, List<DexContainerFileDexoptStatus>> entry : |
| 135 |
secondaryStatusesByDexPath.entrySet()) { |
| 136 |
dumpSecondaryDex(ipw, snapshot, entry.getValue(), packageName, |
| 137 |
secondaryDexInfoByDexPath.get(entry.getKey())); |
| 138 |
} |
| 139 |
ipw.decreaseIndent(); |
| 140 |
} |
| 141 |
ipw.decreaseIndent(); |
| 142 |
} |
| 143 |
|
| 144 |
private void dumpPrimaryDex(@NonNull IndentingPrintWriter ipw, |
| 145 |
@NonNull PackageManagerLocal.FilteredSnapshot snapshot, |
| 146 |
List<DexContainerFileDexoptStatus> fileStatuses, @NonNull String packageName) { |
| 147 |
String dexPath = fileStatuses.get(0).getDexContainerFile(); |
| 148 |
ipw.printf("path: %s\n", dexPath); |
| 149 |
ipw.increaseIndent(); |
| 150 |
dumpFileStatuses(ipw, fileStatuses); |
| 151 |
dumpUsedByOtherApps(ipw, snapshot, |
| 152 |
mInjector.getDexUseManager().getPrimaryDexLoaders(packageName, dexPath), |
| 153 |
packageName); |
| 154 |
ipw.decreaseIndent(); |
| 155 |
} |
| 156 |
|
| 157 |
private void dumpSecondaryDex(@NonNull IndentingPrintWriter ipw, |
| 158 |
@NonNull PackageManagerLocal.FilteredSnapshot snapshot, |
| 159 |
List<DexContainerFileDexoptStatus> fileStatuses, @NonNull String packageName, |
| 160 |
@NonNull SecondaryDexInfo info) { |
| 161 |
String dexPath = fileStatuses.get(0).getDexContainerFile(); |
| 162 |
@FileVisibility int visibility = getDexFileVisibility(dexPath); |
| 163 |
ipw.println(dexPath |
| 164 |
+ (visibility == FileVisibility.NOT_FOUND |
| 165 |
? " (removed)" |
| 166 |
: (visibility == FileVisibility.OTHER_READABLE ? " (public)" |
| 167 |
: ""))); |
| 168 |
ipw.increaseIndent(); |
| 169 |
dumpFileStatuses(ipw, fileStatuses); |
| 170 |
ipw.printf("class loader context: %s\n", info.displayClassLoaderContext()); |
| 171 |
TreeMap<DexLoader, String> classLoaderContexts = |
| 172 |
info.loaders().stream().collect(Collectors.toMap(loader |
| 173 |
-> loader, |
| 174 |
loader |
| 175 |
-> mInjector.getDexUseManager().getSecondaryClassLoaderContext( |
| 176 |
packageName, dexPath, loader), |
| 177 |
(a, b) -> a, TreeMap::new)); |
| 178 |
// We should print all class loader contexts even if `info.displayClassLoaderContext()` is |
| 179 |
// not `VARYING_CLASS_LOADER_CONTEXTS`. This is because `info.displayClassLoaderContext()` |
| 180 |
// may show the only supported class loader context while other apps have unsupported ones. |
| 181 |
if (classLoaderContexts.values().stream().distinct().count() >= 2) { |
| 182 |
ipw.increaseIndent(); |
| 183 |
for (var entry : classLoaderContexts.entrySet()) { |
| 184 |
// entry.getValue() may be null due to a race, but it's an edge case. |
| 185 |
ipw.printf("%s: %s\n", entry.getKey(), entry.getValue()); |
| 186 |
} |
| 187 |
ipw.decreaseIndent(); |
| 188 |
} |
| 189 |
dumpUsedByOtherApps(ipw, snapshot, info.loaders(), packageName); |
| 190 |
ipw.decreaseIndent(); |
| 191 |
} |
| 192 |
|
| 193 |
private void dumpFileStatuses( |
| 194 |
@NonNull IndentingPrintWriter ipw, List<DexContainerFileDexoptStatus> fileStatuses) { |
| 195 |
for (DexContainerFileDexoptStatus fileStatus : fileStatuses) { |
| 196 |
ipw.printf("%s: [status=%s] [reason=%s]%s\n", |
| 197 |
VMRuntime.getInstructionSet(fileStatus.getAbi()), |
| 198 |
fileStatus.getCompilerFilter(), fileStatus.getCompilationReason(), |
| 199 |
fileStatus.isPrimaryAbi() ? " [primary-abi]" : ""); |
| 200 |
ipw.increaseIndent(); |
| 201 |
ipw.printf("[location is %s]\n", fileStatus.getLocationDebugString()); |
| 202 |
ipw.decreaseIndent(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
private void dumpUsedByOtherApps(@NonNull IndentingPrintWriter ipw, |
| 207 |
@NonNull PackageManagerLocal.FilteredSnapshot snapshot, |
| 208 |
@NonNull Set<DexLoader> dexLoaders, @NonNull String packageName) { |
| 209 |
List<DexLoader> otherApps = |
| 210 |
dexLoaders.stream() |
| 211 |
.filter(loader -> DexUseManagerLocal.isLoaderOtherApp(loader, packageName)) |
| 212 |
.collect(Collectors.toList()); |
| 213 |
if (!otherApps.isEmpty()) { |
| 214 |
ipw.printf("used by other apps: [%s]\n", |
| 215 |
otherApps.stream() |
| 216 |
.sorted() |
| 217 |
.map(loader -> getLoaderState(snapshot, loader)) |
| 218 |
.collect(Collectors.joining(", "))); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
@NonNull |
| 223 |
private String getLoaderState( |
| 224 |
@NonNull PackageManagerLocal.FilteredSnapshot snapshot, @NonNull DexLoader loader) { |
| 225 |
var result = new StringBuilder(loader.toString()); |
| 226 |
PackageState loadingPkgState = snapshot.getPackageState(loader.loadingPackageName()); |
| 227 |
if (loadingPkgState == null) { |
| 228 |
// This can happen because the information held by DexUseManagerLocal can be outdated. |
| 229 |
// We don't want to clean up the entry at this point because we don't want the dump |
| 230 |
// operation to have an side effect. |
| 231 |
result.append(" (removed)"); |
| 232 |
return result.toString(); |
| 233 |
} |
| 234 |
Utils.Abi abi = Utils.getPrimaryAbi(loadingPkgState); |
| 235 |
result.append(String.format(" (isa=%s)", abi.isa())); |
| 236 |
return result.toString(); |
| 237 |
} |
| 238 |
|
| 239 |
private @FileVisibility int getDexFileVisibility(@NonNull String dexPath) { |
| 240 |
try { |
| 241 |
return mInjector.getArtd().getDexFileVisibility(dexPath); |
| 242 |
} catch (ServiceSpecificException | RemoteException e) { |
| 243 |
Log.e(TAG, "Failed to get visibility of " + dexPath, e); |
| 244 |
return FileVisibility.NOT_FOUND; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/** Injector pattern for testing purpose. */ |
| 249 |
@VisibleForTesting |
| 250 |
public static class Injector { |
| 251 |
@NonNull private final ArtManagerLocal mArtManagerLocal; |
| 252 |
|
| 253 |
Injector(@NonNull ArtManagerLocal artManagerLocal) { |
| 254 |
mArtManagerLocal = artManagerLocal; |
| 255 |
} |
| 256 |
|
| 257 |
@NonNull |
| 258 |
public ArtManagerLocal getArtManagerLocal() { |
| 259 |
return mArtManagerLocal; |
| 260 |
} |
| 261 |
|
| 262 |
@NonNull |
| 263 |
public DexUseManagerLocal getDexUseManager() { |
| 264 |
return Objects.requireNonNull( |
| 265 |
LocalManagerRegistry.getManager(DexUseManagerLocal.class)); |
| 266 |
} |
| 267 |
|
| 268 |
@NonNull |
| 269 |
public IArtd getArtd() { |
| 270 |
return Utils.getArtd(); |
| 271 |
} |
| 272 |
} |
| 273 |
} |