xref: /aosp_15_r20/frameworks/native/libs/dumputils/dump_utils.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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>
17 #include <utility>
18 
19 #include <android-base/file.h>
20 #include <android-base/parseint.h>
21 #include <android-base/properties.h>
22 #include <android-base/stringprintf.h>
23 #include <android-base/strings.h>
24 #include <android/hidl/manager/1.0/IServiceManager.h>
25 #include <binder/IServiceManager.h>
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",
39         "/system/bin/mediatranscoding", // media.transcoding
40         "/system/bin/netd",
41         "/system/bin/sdcard",
42         "/apex/com.android.os.statsd/bin/statsd",
43         "/system/bin/surfaceflinger",
44         "/system/bin/vehicle_network_service",
45         "/vendor/bin/hw/android.hardware.media.omx@1.0-service", // media.codec
46         "/apex/com.android.media.swcodec/bin/mediaswcodec", // media.swcodec
47         NULL,
48 };
49 
50 
51 // Native processes to dump on debuggable builds.
52 static const char* debuggable_native_processes_to_dump[] = {
53         "/system/bin/keystore2",
54         "/system/bin/vold",
55         NULL,
56 };
57 
58 /* list of hidl hal interface to dump containing process during native dumps */
59 static const char* hidl_hal_interfaces_to_dump[] {
60         "[email protected]::IDevicesFactory",
61         "[email protected]::IDevicesFactory",
62         "[email protected]::IDevicesFactory",
63         "[email protected]::IDevicesFactory",
64         "[email protected]::IAudioControl",
65         "[email protected]::IAudioControl",
66         "[email protected]::ICanBus",
67         "[email protected]::ICanController",
68         "[email protected]::IEvsCamera",
69         "[email protected]::ISurroundViewService",
70         "[email protected]::IVehicle",
71         "[email protected]::IBiometricsFace",
72         "[email protected]::IBiometricsFingerprint",
73         "[email protected]::IBluetoothHci",
74         "[email protected]::ICameraProvider",
75         "[email protected]::IDrmFactory",
76         "[email protected]::IAllocator",
77         "[email protected]::IComposer",
78         "[email protected]::IHealth",
79         "[email protected]::IComponentStore",
80         "[email protected]::IOmx",
81         "[email protected]::IOmxStore",
82         "[email protected]::IDevice",
83         "[email protected]::IPower",
84         "[email protected]::IPowerStats",
85         "[email protected]::ISensors",
86         "[email protected]::IThermal",
87         "[email protected]::IVr",
88         NULL,
89 };
90 
91 /* list of hal interface to dump containing process during native dumps */
92 static const std::vector<std::string> aidl_interfaces_to_dump {
93         "android.hardware.audio.core.IConfig",
94         "android.hardware.audio.core.IModule",
95         "android.hardware.audio.effect.IFactory",
96         "android.hardware.automotive.audiocontrol.IAudioControl",
97         "android.hardware.automotive.can.ICanController",
98         "android.hardware.automotive.evs.IEvsEnumerator",
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",
103         "android.hardware.biometrics.face.IBiometricsFace",
104         "android.hardware.biometrics.fingerprint.IBiometricsFingerprint",
105         "android.hardware.camera.provider.ICameraProvider",
106         "android.hardware.drm.IDrmFactory",
107         "android.hardware.graphics.allocator.IAllocator",
108         "android.hardware.graphics.composer3.IComposer",
109         "android.hardware.health.IHealth",
110         "android.hardware.input.processor.IInputProcessor",
111         "android.hardware.neuralnetworks.IDevice",
112         "android.hardware.power.IPower",
113         "android.hardware.power.stats.IPowerStats",
114         "android.hardware.sensors.ISensors",
115 };
116 
117 /* list of extra hal interfaces to dump containing process during native dumps */
118 // This is filled when dumpstate is called.
119 static std::set<std::string> extra_hal_interfaces_to_dump;
120 
read_extra_hals_to_dump_from_property()121 static void read_extra_hals_to_dump_from_property() {
122     // extra hals to dump are already filled
123     if (!extra_hal_interfaces_to_dump.empty()) {
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         }
133         extra_hal_interfaces_to_dump.insert(std::move(trimmed_token));
134     }
135 }
136 
137 // check if interface is included in either default hal list or extra hal list
should_dump_hal_interface(const std::string & interface)138 bool should_dump_hal_interface(const std::string& interface) {
139     for (const char** i = hidl_hal_interfaces_to_dump; *i; i++) {
140         if (interface == *i) {
141             return true;
142         }
143     }
144     return extra_hal_interfaces_to_dump.find(interface) != extra_hal_interfaces_to_dump.end();
145 }
146 
should_dump_native_traces(const char * path)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     }
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 
162     return false;
163 }
164 
get_interesting_aidl_pids(std::set<int> & pids)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 
get_interesting_hidl_pids(std::set<int> & pids)179 static void get_interesting_hidl_pids(std::set<int> &pids) {
180     using android::hidl::manager::V1_0::IServiceManager;
181     using android::sp;
182     using android::hardware::Return;
183 
184     sp<IServiceManager> manager = IServiceManager::getService();
185     read_extra_hals_to_dump_from_property();
186 
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 
193             if (should_dump_hal_interface(info.interfaceName)) {
194                 pids.insert(info.pid);
195             }
196         }
197     });
198 
199     if (!ret.isOk()) {
200         ALOGE("Could not get list of HAL PIDs: %s\n", ret.description().c_str());
201     }
202 
203     return;
204 }
205 
get_interesting_pids()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;
211 }
212 
IsZygote(int pid)213 bool IsZygote(int pid) {
214     std::string cmdline;
215     if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid),
216                                          &cmdline)) {
217         return true;
218     }
219 
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" ||
224             cmdline == "usap64" || cmdline == "webview_zygote";
225 }
226 
IsCached(int pid)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 }
241