blob: f4cf11edb6bacbe2c7460f88129986bae53eaf37 [file] [log] [blame]
Kweku Adams5ce418d2018-02-05 16:43:53 -08001/*
2 * Copyright (C) 2018 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#include <set>
17
18#include <android-base/file.h>
Kevin Jeond0f8a982023-04-07 14:27:11 -040019#include <android-base/parseint.h>
Eric Jeong9b6bcca2019-11-08 16:37:13 -080020#include <android-base/properties.h>
Kweku Adams5ce418d2018-02-05 16:43:53 -080021#include <android-base/stringprintf.h>
Eric Jeong9b6bcca2019-11-08 16:37:13 -080022#include <android-base/strings.h>
Kweku Adams5ce418d2018-02-05 16:43:53 -080023#include <android/hidl/manager/1.0/IServiceManager.h>
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +000024#include <binder/IServiceManager.h>
Kweku Adams5ce418d2018-02-05 16:43:53 -080025#include <dumputils/dump_utils.h>
26#include <log/log.h>
27
28/* list of native processes to include in the native dumps */
29// This matches the /proc/pid/exe link instead of /proc/pid/cmdline.
30static const char* native_processes_to_dump[] = {
31 "/system/bin/audioserver",
32 "/system/bin/cameraserver",
33 "/system/bin/drmserver",
34 "/system/bin/mediadrmserver",
35 "/system/bin/mediaextractor", // media.extractor
36 "/system/bin/mediametrics", // media.metrics
37 "/system/bin/mediaserver",
Chong Zhang7920d812020-10-15 12:06:39 -070038 "/system/bin/mediatranscoding", // media.transcoding
Chalard Jeanbd526942019-04-04 18:23:04 +090039 "/system/bin/netd",
Kweku Adams5ce418d2018-02-05 16:43:53 -080040 "/system/bin/sdcard",
Howard Ro6a446a22020-02-20 04:57:00 +000041 "/apex/com.android.os.statsd/bin/statsd",
Kweku Adams5ce418d2018-02-05 16:43:53 -080042 "/system/bin/surfaceflinger",
43 "/system/bin/vehicle_network_service",
44 "/vendor/bin/hw/android.hardware.media.omx@1.0-service", // media.codec
Chong Zhang31f68992019-03-27 16:08:53 -070045 "/apex/com.android.media.swcodec/bin/mediaswcodec", // media.swcodec
Kweku Adams5ce418d2018-02-05 16:43:53 -080046 NULL,
47};
48
Josh Gaoe73b7ee2020-01-09 21:54:50 -080049
50// Native processes to dump on debuggable builds.
51static const char* debuggable_native_processes_to_dump[] = {
Eric Biggerse8463162021-05-04 15:03:48 -070052 "/system/bin/keystore2",
Josh Gaoe73b7ee2020-01-09 21:54:50 -080053 "/system/bin/vold",
54 NULL,
55};
56
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +000057/* list of hidl hal interface to dump containing process during native dumps */
58static const char* hidl_hal_interfaces_to_dump[] {
Mikhail Naganovac1e32c2018-05-01 09:03:40 -070059 "android.hardware.audio@4.0::IDevicesFactory",
Carter Hsu723e8cd2019-09-11 13:57:58 +080060 "android.hardware.audio@5.0::IDevicesFactory",
Mikhail Naganov091e5d82020-01-27 12:11:51 -080061 "android.hardware.audio@6.0::IDevicesFactory",
Mikhail Naganov783022f2020-08-06 19:38:38 +000062 "android.hardware.audio@7.0::IDevicesFactory",
Stefano Galarraga5101b332020-03-23 08:49:08 +000063 "android.hardware.automotive.audiocontrol@1.0::IAudioControl",
Hayden Gomesb11a1b72020-03-26 14:23:33 -070064 "android.hardware.automotive.audiocontrol@2.0::IAudioControl",
Lakshman Annadorai080dff22023-04-04 13:44:14 -070065 "android.hardware.automotive.can@1.0::ICanBus",
66 "android.hardware.automotive.can@1.0::ICanController",
Stefano Galarraga5101b332020-03-23 08:49:08 +000067 "android.hardware.automotive.evs@1.0::IEvsCamera",
Lakshman Annadorai080dff22023-04-04 13:44:14 -070068 "android.hardware.automotive.sv@1.0::ISurroundViewService",
Stefano Galarraga5101b332020-03-23 08:49:08 +000069 "android.hardware.automotive.vehicle@2.0::IVehicle",
Kevin Chyn3bad8102019-06-19 11:05:53 -070070 "android.hardware.biometrics.face@1.0::IBiometricsFace",
Kris Chene93e6c92020-03-03 17:50:11 +080071 "android.hardware.biometrics.fingerprint@2.1::IBiometricsFingerprint",
Kweku Adams5ce418d2018-02-05 16:43:53 -080072 "android.hardware.bluetooth@1.0::IBluetoothHci",
73 "android.hardware.camera.provider@2.4::ICameraProvider",
Jeff Tinker5fce8822018-04-02 17:24:41 -070074 "android.hardware.drm@1.0::IDrmFactory",
Chia-I Wu078dc422018-08-09 16:04:51 -070075 "android.hardware.graphics.allocator@2.0::IAllocator",
Kweku Adams5ce418d2018-02-05 16:43:53 -080076 "android.hardware.graphics.composer@2.1::IComposer",
Yifan Hong77a56172018-11-27 16:43:49 -080077 "android.hardware.health@2.0::IHealth",
Chong Zhang31f68992019-03-27 16:08:53 -070078 "android.hardware.media.c2@1.0::IComponentStore",
Kweku Adams5ce418d2018-02-05 16:43:53 -080079 "android.hardware.media.omx@1.0::IOmx",
Pawin Vongmasab4c85df2018-04-18 07:00:30 -070080 "android.hardware.media.omx@1.0::IOmxStore",
Stefano Galarraga5101b332020-03-23 08:49:08 +000081 "android.hardware.neuralnetworks@1.0::IDevice",
Wei Wang07735612019-04-23 14:12:24 -070082 "android.hardware.power@1.3::IPower",
Benjamin Schwartz525651b2019-04-08 15:26:10 -070083 "android.hardware.power.stats@1.0::IPowerStats",
Kweku Adams5ce418d2018-02-05 16:43:53 -080084 "android.hardware.sensors@1.0::ISensors",
Wei Wang07735612019-04-23 14:12:24 -070085 "android.hardware.thermal@2.0::IThermal",
Kweku Adams5ce418d2018-02-05 16:43:53 -080086 "android.hardware.vr@1.0::IVr",
87 NULL,
88};
89
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +000090/* list of hal interface to dump containing process during native dumps */
91static const std::vector<std::string> aidl_interfaces_to_dump {
Jaideep Sharma51410cc2024-01-11 23:03:50 +053092 "android.hardware.audio.core.IConfig",
93 "android.hardware.audio.core.IModule",
94 "android.hardware.audio.effect.IFactory",
Steven Morelande94be362022-06-25 01:07:59 +000095 "android.hardware.automotive.audiocontrol.IAudioControl",
Lakshman Annadorai080dff22023-04-04 13:44:14 -070096 "android.hardware.automotive.can.ICanController",
Steven Morelande94be362022-06-25 01:07:59 +000097 "android.hardware.automotive.evs.IEvsEnumerator",
Lakshman Annadorai080dff22023-04-04 13:44:14 -070098 "android.hardware.automotive.ivn.IIvnAndroidDevice",
99 "android.hardware.automotive.occupant_awareness.IOccupantAwareness",
100 "android.hardware.automotive.remoteaccess.IRemoteAccess",
101 "android.hardware.automotive.vehicle.IVehicle",
Steven Morelande94be362022-06-25 01:07:59 +0000102 "android.hardware.biometrics.face.IBiometricsFace",
103 "android.hardware.biometrics.fingerprint.IBiometricsFingerprint",
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000104 "android.hardware.camera.provider.ICameraProvider",
Steven Morelande94be362022-06-25 01:07:59 +0000105 "android.hardware.drm.IDrmFactory",
106 "android.hardware.graphics.allocator.IAllocator",
107 "android.hardware.graphics.composer3.IComposer",
108 "android.hardware.health.IHealth",
Siarhei Vishniakou654b7cd2022-07-01 13:38:51 -0700109 "android.hardware.input.processor.IInputProcessor",
Steven Morelande94be362022-06-25 01:07:59 +0000110 "android.hardware.neuralnetworks.IDevice",
111 "android.hardware.power.IPower",
112 "android.hardware.power.stats.IPowerStats",
113 "android.hardware.sensors.ISensors",
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000114};
115
Eric Jeong9b6bcca2019-11-08 16:37:13 -0800116/* list of extra hal interfaces to dump containing process during native dumps */
117// This is filled when dumpstate is called.
118static std::set<const std::string> extra_hal_interfaces_to_dump;
119
120static void read_extra_hals_to_dump_from_property() {
121 // extra hals to dump are already filled
Eric Jeong961ba2d2020-06-24 11:43:15 -0700122 if (!extra_hal_interfaces_to_dump.empty()) {
Eric Jeong9b6bcca2019-11-08 16:37:13 -0800123 return;
124 }
125 std::string value = android::base::GetProperty("ro.dump.hals.extra", "");
126 std::vector<std::string> tokens = android::base::Split(value, ",");
127 for (const auto &token : tokens) {
128 std::string trimmed_token = android::base::Trim(token);
129 if (trimmed_token.length() == 0) {
130 continue;
131 }
132 extra_hal_interfaces_to_dump.insert(trimmed_token);
133 }
134}
135
136// check if interface is included in either default hal list or extra hal list
137bool should_dump_hal_interface(const std::string& interface) {
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000138 for (const char** i = hidl_hal_interfaces_to_dump; *i; i++) {
Eric Jeong9b6bcca2019-11-08 16:37:13 -0800139 if (interface == *i) {
Kweku Adams5ce418d2018-02-05 16:43:53 -0800140 return true;
141 }
142 }
Eric Jeong9b6bcca2019-11-08 16:37:13 -0800143 return extra_hal_interfaces_to_dump.find(interface) != extra_hal_interfaces_to_dump.end();
Kweku Adams5ce418d2018-02-05 16:43:53 -0800144}
145
146bool should_dump_native_traces(const char* path) {
147 for (const char** p = native_processes_to_dump; *p; p++) {
148 if (!strcmp(*p, path)) {
149 return true;
150 }
151 }
Josh Gaoe73b7ee2020-01-09 21:54:50 -0800152
153 if (android::base::GetBoolProperty("ro.debuggable", false)) {
154 for (const char** p = debuggable_native_processes_to_dump; *p; p++) {
155 if (!strcmp(*p, path)) {
156 return true;
157 }
158 }
159 }
160
Kweku Adams5ce418d2018-02-05 16:43:53 -0800161 return false;
162}
163
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000164static void get_interesting_aidl_pids(std::set<int> &pids) {
165 using ServiceDebugInfo = android::IServiceManager::ServiceDebugInfo;
166 auto sm = android::defaultServiceManager();
167 std::vector<ServiceDebugInfo> serviceDebugInfos = sm->getServiceDebugInfo();
168 for (const auto & serviceDebugInfo : serviceDebugInfos) {
169 for (const auto &aidl_prefix : aidl_interfaces_to_dump) {
170 // Check for prefix match with aidl interface to dump
171 if (serviceDebugInfo.name.rfind(aidl_prefix, 0) == 0) {
172 pids.insert(serviceDebugInfo.pid);
173 }
174 }
175 }
176}
177
178static void get_interesting_hidl_pids(std::set<int> &pids) {
Kweku Adams5ce418d2018-02-05 16:43:53 -0800179 using android::hidl::manager::V1_0::IServiceManager;
180 using android::sp;
181 using android::hardware::Return;
182
183 sp<IServiceManager> manager = IServiceManager::getService();
Eric Jeong9b6bcca2019-11-08 16:37:13 -0800184 read_extra_hals_to_dump_from_property();
185
Kweku Adams5ce418d2018-02-05 16:43:53 -0800186 Return<void> ret = manager->debugDump([&](auto& hals) {
187 for (const auto &info : hals) {
188 if (info.pid == static_cast<int>(IServiceManager::PidConstant::NO_PID)) {
189 continue;
190 }
191
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000192 if (should_dump_hal_interface(info.interfaceName)) {
193 pids.insert(info.pid);
Kweku Adams5ce418d2018-02-05 16:43:53 -0800194 }
Kweku Adams5ce418d2018-02-05 16:43:53 -0800195 }
196 });
197
198 if (!ret.isOk()) {
199 ALOGE("Could not get list of HAL PIDs: %s\n", ret.description().c_str());
200 }
201
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +0000202 return;
203}
204
205std::set<int> get_interesting_pids() {
206 std::set<int> interesting_pids;
207 get_interesting_hidl_pids(interesting_pids);
208 get_interesting_aidl_pids(interesting_pids);
209 return interesting_pids;
Kweku Adams5ce418d2018-02-05 16:43:53 -0800210}
211
212bool IsZygote(int pid) {
Kweku Adams5ce418d2018-02-05 16:43:53 -0800213 std::string cmdline;
214 if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid),
215 &cmdline)) {
216 return true;
217 }
218
Josh Gao33e622a2019-05-16 16:05:04 -0700219 // cmdline has embedded nulls; only consider argv[0].
220 cmdline = std::string(cmdline.c_str());
221
222 return cmdline == "zygote" || cmdline == "zygote64" || cmdline == "usap32" ||
Kevin Jeon5e5de122022-10-15 01:42:59 +0000223 cmdline == "usap64" || cmdline == "webview_zygote";
Kweku Adams5ce418d2018-02-05 16:43:53 -0800224}
Kevin Jeond0f8a982023-04-07 14:27:11 -0400225
226bool IsCached(int pid) {
227 std::string oom_score_adj;
228 if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/oom_score_adj",
229 pid),
230 &oom_score_adj)) {
231 return false;
232 }
233 int32_t oom_score_adj_value;
234 if (!android::base::ParseInt(android::base::Trim(oom_score_adj), &oom_score_adj_value)) {
235 return false;
236 }
237 // An OOM score greater than 900 indicates a cached process.
238 return oom_score_adj_value >= 900;
239}