Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 1 | /* |
| 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> |
Ryan Prichard | 9949899 | 2024-06-27 22:46:40 -0700 | [diff] [blame] | 17 | #include <utility> |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 18 | |
| 19 | #include <android-base/file.h> |
Kevin Jeon | d0f8a98 | 2023-04-07 14:27:11 -0400 | [diff] [blame] | 20 | #include <android-base/parseint.h> |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 23 | #include <android-base/strings.h> |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 24 | #include <android/hidl/manager/1.0/IServiceManager.h> |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 26 | #include <dumputils/dump_utils.h> |
| 27 | #include <log/log.h> |
| 28 | |
| 29 | /* list of native processes to include in the native dumps */ |
| 30 | // This matches the /proc/pid/exe link instead of /proc/pid/cmdline. |
| 31 | static const char* native_processes_to_dump[] = { |
| 32 | "/system/bin/audioserver", |
| 33 | "/system/bin/cameraserver", |
| 34 | "/system/bin/drmserver", |
| 35 | "/system/bin/mediadrmserver", |
| 36 | "/system/bin/mediaextractor", // media.extractor |
| 37 | "/system/bin/mediametrics", // media.metrics |
| 38 | "/system/bin/mediaserver", |
Chong Zhang | 7920d81 | 2020-10-15 12:06:39 -0700 | [diff] [blame] | 39 | "/system/bin/mediatranscoding", // media.transcoding |
Chalard Jean | bd52694 | 2019-04-04 18:23:04 +0900 | [diff] [blame] | 40 | "/system/bin/netd", |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 41 | "/system/bin/sdcard", |
Howard Ro | 6a446a2 | 2020-02-20 04:57:00 +0000 | [diff] [blame] | 42 | "/apex/com.android.os.statsd/bin/statsd", |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 43 | "/system/bin/surfaceflinger", |
| 44 | "/system/bin/vehicle_network_service", |
| 45 | "/vendor/bin/hw/android.hardware.media.omx@1.0-service", // media.codec |
Chong Zhang | 31f6899 | 2019-03-27 16:08:53 -0700 | [diff] [blame] | 46 | "/apex/com.android.media.swcodec/bin/mediaswcodec", // media.swcodec |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 47 | NULL, |
| 48 | }; |
| 49 | |
Josh Gao | e73b7ee | 2020-01-09 21:54:50 -0800 | [diff] [blame] | 50 | |
| 51 | // Native processes to dump on debuggable builds. |
| 52 | static const char* debuggable_native_processes_to_dump[] = { |
Eric Biggers | e846316 | 2021-05-04 15:03:48 -0700 | [diff] [blame] | 53 | "/system/bin/keystore2", |
Josh Gao | e73b7ee | 2020-01-09 21:54:50 -0800 | [diff] [blame] | 54 | "/system/bin/vold", |
| 55 | NULL, |
| 56 | }; |
| 57 | |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 58 | /* list of hidl hal interface to dump containing process during native dumps */ |
| 59 | static const char* hidl_hal_interfaces_to_dump[] { |
Mikhail Naganov | ac1e32c | 2018-05-01 09:03:40 -0700 | [diff] [blame] | 60 | "android.hardware.audio@4.0::IDevicesFactory", |
Carter Hsu | 723e8cd | 2019-09-11 13:57:58 +0800 | [diff] [blame] | 61 | "android.hardware.audio@5.0::IDevicesFactory", |
Mikhail Naganov | 091e5d8 | 2020-01-27 12:11:51 -0800 | [diff] [blame] | 62 | "android.hardware.audio@6.0::IDevicesFactory", |
Mikhail Naganov | 783022f | 2020-08-06 19:38:38 +0000 | [diff] [blame] | 63 | "android.hardware.audio@7.0::IDevicesFactory", |
Stefano Galarraga | 5101b33 | 2020-03-23 08:49:08 +0000 | [diff] [blame] | 64 | "android.hardware.automotive.audiocontrol@1.0::IAudioControl", |
Hayden Gomes | b11a1b7 | 2020-03-26 14:23:33 -0700 | [diff] [blame] | 65 | "android.hardware.automotive.audiocontrol@2.0::IAudioControl", |
Lakshman Annadorai | 080dff2 | 2023-04-04 13:44:14 -0700 | [diff] [blame] | 66 | "android.hardware.automotive.can@1.0::ICanBus", |
| 67 | "android.hardware.automotive.can@1.0::ICanController", |
Stefano Galarraga | 5101b33 | 2020-03-23 08:49:08 +0000 | [diff] [blame] | 68 | "android.hardware.automotive.evs@1.0::IEvsCamera", |
Lakshman Annadorai | 080dff2 | 2023-04-04 13:44:14 -0700 | [diff] [blame] | 69 | "android.hardware.automotive.sv@1.0::ISurroundViewService", |
Stefano Galarraga | 5101b33 | 2020-03-23 08:49:08 +0000 | [diff] [blame] | 70 | "android.hardware.automotive.vehicle@2.0::IVehicle", |
Kevin Chyn | 3bad810 | 2019-06-19 11:05:53 -0700 | [diff] [blame] | 71 | "android.hardware.biometrics.face@1.0::IBiometricsFace", |
Kris Chen | e93e6c9 | 2020-03-03 17:50:11 +0800 | [diff] [blame] | 72 | "android.hardware.biometrics.fingerprint@2.1::IBiometricsFingerprint", |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 73 | "android.hardware.bluetooth@1.0::IBluetoothHci", |
| 74 | "android.hardware.camera.provider@2.4::ICameraProvider", |
Jeff Tinker | 5fce882 | 2018-04-02 17:24:41 -0700 | [diff] [blame] | 75 | "android.hardware.drm@1.0::IDrmFactory", |
Chia-I Wu | 078dc42 | 2018-08-09 16:04:51 -0700 | [diff] [blame] | 76 | "android.hardware.graphics.allocator@2.0::IAllocator", |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 77 | "android.hardware.graphics.composer@2.1::IComposer", |
Yifan Hong | 77a5617 | 2018-11-27 16:43:49 -0800 | [diff] [blame] | 78 | "android.hardware.health@2.0::IHealth", |
Chong Zhang | 31f6899 | 2019-03-27 16:08:53 -0700 | [diff] [blame] | 79 | "android.hardware.media.c2@1.0::IComponentStore", |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 80 | "android.hardware.media.omx@1.0::IOmx", |
Pawin Vongmasa | b4c85df | 2018-04-18 07:00:30 -0700 | [diff] [blame] | 81 | "android.hardware.media.omx@1.0::IOmxStore", |
Stefano Galarraga | 5101b33 | 2020-03-23 08:49:08 +0000 | [diff] [blame] | 82 | "android.hardware.neuralnetworks@1.0::IDevice", |
Wei Wang | 0773561 | 2019-04-23 14:12:24 -0700 | [diff] [blame] | 83 | "android.hardware.power@1.3::IPower", |
Benjamin Schwartz | 525651b | 2019-04-08 15:26:10 -0700 | [diff] [blame] | 84 | "android.hardware.power.stats@1.0::IPowerStats", |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 85 | "android.hardware.sensors@1.0::ISensors", |
Wei Wang | 0773561 | 2019-04-23 14:12:24 -0700 | [diff] [blame] | 86 | "android.hardware.thermal@2.0::IThermal", |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 87 | "android.hardware.vr@1.0::IVr", |
| 88 | NULL, |
| 89 | }; |
| 90 | |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 91 | /* list of hal interface to dump containing process during native dumps */ |
| 92 | static const std::vector<std::string> aidl_interfaces_to_dump { |
Jaideep Sharma | 51410cc | 2024-01-11 23:03:50 +0530 | [diff] [blame] | 93 | "android.hardware.audio.core.IConfig", |
| 94 | "android.hardware.audio.core.IModule", |
| 95 | "android.hardware.audio.effect.IFactory", |
Steven Moreland | e94be36 | 2022-06-25 01:07:59 +0000 | [diff] [blame] | 96 | "android.hardware.automotive.audiocontrol.IAudioControl", |
Lakshman Annadorai | 080dff2 | 2023-04-04 13:44:14 -0700 | [diff] [blame] | 97 | "android.hardware.automotive.can.ICanController", |
Steven Moreland | e94be36 | 2022-06-25 01:07:59 +0000 | [diff] [blame] | 98 | "android.hardware.automotive.evs.IEvsEnumerator", |
Lakshman Annadorai | 080dff2 | 2023-04-04 13:44:14 -0700 | [diff] [blame] | 99 | "android.hardware.automotive.ivn.IIvnAndroidDevice", |
| 100 | "android.hardware.automotive.occupant_awareness.IOccupantAwareness", |
| 101 | "android.hardware.automotive.remoteaccess.IRemoteAccess", |
| 102 | "android.hardware.automotive.vehicle.IVehicle", |
Steven Moreland | e94be36 | 2022-06-25 01:07:59 +0000 | [diff] [blame] | 103 | "android.hardware.biometrics.face.IBiometricsFace", |
| 104 | "android.hardware.biometrics.fingerprint.IBiometricsFingerprint", |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 105 | "android.hardware.camera.provider.ICameraProvider", |
Steven Moreland | e94be36 | 2022-06-25 01:07:59 +0000 | [diff] [blame] | 106 | "android.hardware.drm.IDrmFactory", |
| 107 | "android.hardware.graphics.allocator.IAllocator", |
| 108 | "android.hardware.graphics.composer3.IComposer", |
| 109 | "android.hardware.health.IHealth", |
Siarhei Vishniakou | 654b7cd | 2022-07-01 13:38:51 -0700 | [diff] [blame] | 110 | "android.hardware.input.processor.IInputProcessor", |
Steven Moreland | e94be36 | 2022-06-25 01:07:59 +0000 | [diff] [blame] | 111 | "android.hardware.neuralnetworks.IDevice", |
| 112 | "android.hardware.power.IPower", |
| 113 | "android.hardware.power.stats.IPowerStats", |
| 114 | "android.hardware.sensors.ISensors", |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 117 | /* list of extra hal interfaces to dump containing process during native dumps */ |
| 118 | // This is filled when dumpstate is called. |
Ryan Prichard | 9949899 | 2024-06-27 22:46:40 -0700 | [diff] [blame] | 119 | static std::set<std::string> extra_hal_interfaces_to_dump; |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 120 | |
| 121 | static void read_extra_hals_to_dump_from_property() { |
| 122 | // extra hals to dump are already filled |
Eric Jeong | 961ba2d | 2020-06-24 11:43:15 -0700 | [diff] [blame] | 123 | if (!extra_hal_interfaces_to_dump.empty()) { |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | std::string value = android::base::GetProperty("ro.dump.hals.extra", ""); |
| 127 | std::vector<std::string> tokens = android::base::Split(value, ","); |
| 128 | for (const auto &token : tokens) { |
| 129 | std::string trimmed_token = android::base::Trim(token); |
| 130 | if (trimmed_token.length() == 0) { |
| 131 | continue; |
| 132 | } |
Ryan Prichard | 9949899 | 2024-06-27 22:46:40 -0700 | [diff] [blame] | 133 | extra_hal_interfaces_to_dump.insert(std::move(trimmed_token)); |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | // check if interface is included in either default hal list or extra hal list |
| 138 | bool should_dump_hal_interface(const std::string& interface) { |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 139 | for (const char** i = hidl_hal_interfaces_to_dump; *i; i++) { |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 140 | if (interface == *i) { |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | } |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 144 | return extra_hal_interfaces_to_dump.find(interface) != extra_hal_interfaces_to_dump.end(); |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool should_dump_native_traces(const char* path) { |
| 148 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 149 | if (!strcmp(*p, path)) { |
| 150 | return true; |
| 151 | } |
| 152 | } |
Josh Gao | e73b7ee | 2020-01-09 21:54:50 -0800 | [diff] [blame] | 153 | |
| 154 | if (android::base::GetBoolProperty("ro.debuggable", false)) { |
| 155 | for (const char** p = debuggable_native_processes_to_dump; *p; p++) { |
| 156 | if (!strcmp(*p, path)) { |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 165 | static void get_interesting_aidl_pids(std::set<int> &pids) { |
| 166 | using ServiceDebugInfo = android::IServiceManager::ServiceDebugInfo; |
| 167 | auto sm = android::defaultServiceManager(); |
| 168 | std::vector<ServiceDebugInfo> serviceDebugInfos = sm->getServiceDebugInfo(); |
| 169 | for (const auto & serviceDebugInfo : serviceDebugInfos) { |
| 170 | for (const auto &aidl_prefix : aidl_interfaces_to_dump) { |
| 171 | // Check for prefix match with aidl interface to dump |
| 172 | if (serviceDebugInfo.name.rfind(aidl_prefix, 0) == 0) { |
| 173 | pids.insert(serviceDebugInfo.pid); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static void get_interesting_hidl_pids(std::set<int> &pids) { |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 180 | using android::hidl::manager::V1_0::IServiceManager; |
| 181 | using android::sp; |
| 182 | using android::hardware::Return; |
| 183 | |
| 184 | sp<IServiceManager> manager = IServiceManager::getService(); |
Eric Jeong | 9b6bcca | 2019-11-08 16:37:13 -0800 | [diff] [blame] | 185 | read_extra_hals_to_dump_from_property(); |
| 186 | |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 187 | Return<void> ret = manager->debugDump([&](auto& hals) { |
| 188 | for (const auto &info : hals) { |
| 189 | if (info.pid == static_cast<int>(IServiceManager::PidConstant::NO_PID)) { |
| 190 | continue; |
| 191 | } |
| 192 | |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 193 | if (should_dump_hal_interface(info.interfaceName)) { |
| 194 | pids.insert(info.pid); |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 195 | } |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 196 | } |
| 197 | }); |
| 198 | |
| 199 | if (!ret.isOk()) { |
| 200 | ALOGE("Could not get list of HAL PIDs: %s\n", ret.description().c_str()); |
| 201 | } |
| 202 | |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
| 206 | std::set<int> get_interesting_pids() { |
| 207 | std::set<int> interesting_pids; |
| 208 | get_interesting_hidl_pids(interesting_pids); |
| 209 | get_interesting_aidl_pids(interesting_pids); |
| 210 | return interesting_pids; |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bool IsZygote(int pid) { |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 214 | std::string cmdline; |
| 215 | if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid), |
| 216 | &cmdline)) { |
| 217 | return true; |
| 218 | } |
| 219 | |
Josh Gao | 33e622a | 2019-05-16 16:05:04 -0700 | [diff] [blame] | 220 | // cmdline has embedded nulls; only consider argv[0]. |
| 221 | cmdline = std::string(cmdline.c_str()); |
| 222 | |
| 223 | return cmdline == "zygote" || cmdline == "zygote64" || cmdline == "usap32" || |
Kevin Jeon | 5e5de12 | 2022-10-15 01:42:59 +0000 | [diff] [blame] | 224 | cmdline == "usap64" || cmdline == "webview_zygote"; |
Kweku Adams | 5ce418d | 2018-02-05 16:43:53 -0800 | [diff] [blame] | 225 | } |
Kevin Jeon | d0f8a98 | 2023-04-07 14:27:11 -0400 | [diff] [blame] | 226 | |
| 227 | bool IsCached(int pid) { |
| 228 | std::string oom_score_adj; |
| 229 | if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/oom_score_adj", |
| 230 | pid), |
| 231 | &oom_score_adj)) { |
| 232 | return false; |
| 233 | } |
| 234 | int32_t oom_score_adj_value; |
| 235 | if (!android::base::ParseInt(android::base::Trim(oom_score_adj), &oom_score_adj_value)) { |
| 236 | return false; |
| 237 | } |
| 238 | // An OOM score greater than 900 indicates a cached process. |
| 239 | return oom_score_adj_value >= 900; |
| 240 | } |