xref: /aosp_15_r20/frameworks/native/libs/dumputils/dump_utils.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker #include <set>
17*38e8c45fSAndroid Build Coastguard Worker #include <utility>
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <android-base/file.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <android-base/parseint.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <android-base/properties.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <android-base/strings.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <android/hidl/manager/1.0/IServiceManager.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <dumputils/dump_utils.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <log/log.h>
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker /* list of native processes to include in the native dumps */
30*38e8c45fSAndroid Build Coastguard Worker // This matches the /proc/pid/exe link instead of /proc/pid/cmdline.
31*38e8c45fSAndroid Build Coastguard Worker static const char* native_processes_to_dump[] = {
32*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/audioserver",
33*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/cameraserver",
34*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/drmserver",
35*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/mediadrmserver",
36*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/mediaextractor", // media.extractor
37*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/mediametrics", // media.metrics
38*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/mediaserver",
39*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/mediatranscoding", // media.transcoding
40*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/netd",
41*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/sdcard",
42*38e8c45fSAndroid Build Coastguard Worker         "/apex/com.android.os.statsd/bin/statsd",
43*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/surfaceflinger",
44*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/vehicle_network_service",
45*38e8c45fSAndroid Build Coastguard Worker         "/vendor/bin/hw/android.hardware.media.omx@1.0-service", // media.codec
46*38e8c45fSAndroid Build Coastguard Worker         "/apex/com.android.media.swcodec/bin/mediaswcodec", // media.swcodec
47*38e8c45fSAndroid Build Coastguard Worker         NULL,
48*38e8c45fSAndroid Build Coastguard Worker };
49*38e8c45fSAndroid Build Coastguard Worker 
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker // Native processes to dump on debuggable builds.
52*38e8c45fSAndroid Build Coastguard Worker static const char* debuggable_native_processes_to_dump[] = {
53*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/keystore2",
54*38e8c45fSAndroid Build Coastguard Worker         "/system/bin/vold",
55*38e8c45fSAndroid Build Coastguard Worker         NULL,
56*38e8c45fSAndroid Build Coastguard Worker };
57*38e8c45fSAndroid Build Coastguard Worker 
58*38e8c45fSAndroid Build Coastguard Worker /* list of hidl hal interface to dump containing process during native dumps */
59*38e8c45fSAndroid Build Coastguard Worker static const char* hidl_hal_interfaces_to_dump[] {
60*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IDevicesFactory",
61*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IDevicesFactory",
62*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IDevicesFactory",
63*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IDevicesFactory",
64*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IAudioControl",
65*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IAudioControl",
66*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::ICanBus",
67*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::ICanController",
68*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IEvsCamera",
69*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::ISurroundViewService",
70*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IVehicle",
71*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IBiometricsFace",
72*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IBiometricsFingerprint",
73*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IBluetoothHci",
74*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::ICameraProvider",
75*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IDrmFactory",
76*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IAllocator",
77*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IComposer",
78*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IHealth",
79*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IComponentStore",
80*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IOmx",
81*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IOmxStore",
82*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IDevice",
83*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IPower",
84*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IPowerStats",
85*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::ISensors",
86*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IThermal",
87*38e8c45fSAndroid Build Coastguard Worker         "[email protected]::IVr",
88*38e8c45fSAndroid Build Coastguard Worker         NULL,
89*38e8c45fSAndroid Build Coastguard Worker };
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker /* list of hal interface to dump containing process during native dumps */
92*38e8c45fSAndroid Build Coastguard Worker static const std::vector<std::string> aidl_interfaces_to_dump {
93*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.audio.core.IConfig",
94*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.audio.core.IModule",
95*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.audio.effect.IFactory",
96*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.automotive.audiocontrol.IAudioControl",
97*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.automotive.can.ICanController",
98*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.automotive.evs.IEvsEnumerator",
99*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.automotive.ivn.IIvnAndroidDevice",
100*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.automotive.occupant_awareness.IOccupantAwareness",
101*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.automotive.remoteaccess.IRemoteAccess",
102*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.automotive.vehicle.IVehicle",
103*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.biometrics.face.IBiometricsFace",
104*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.biometrics.fingerprint.IBiometricsFingerprint",
105*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.camera.provider.ICameraProvider",
106*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.drm.IDrmFactory",
107*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.graphics.allocator.IAllocator",
108*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.graphics.composer3.IComposer",
109*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.health.IHealth",
110*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.input.processor.IInputProcessor",
111*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.neuralnetworks.IDevice",
112*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.power.IPower",
113*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.power.stats.IPowerStats",
114*38e8c45fSAndroid Build Coastguard Worker         "android.hardware.sensors.ISensors",
115*38e8c45fSAndroid Build Coastguard Worker };
116*38e8c45fSAndroid Build Coastguard Worker 
117*38e8c45fSAndroid Build Coastguard Worker /* list of extra hal interfaces to dump containing process during native dumps */
118*38e8c45fSAndroid Build Coastguard Worker // This is filled when dumpstate is called.
119*38e8c45fSAndroid Build Coastguard Worker static std::set<std::string> extra_hal_interfaces_to_dump;
120*38e8c45fSAndroid Build Coastguard Worker 
read_extra_hals_to_dump_from_property()121*38e8c45fSAndroid Build Coastguard Worker static void read_extra_hals_to_dump_from_property() {
122*38e8c45fSAndroid Build Coastguard Worker     // extra hals to dump are already filled
123*38e8c45fSAndroid Build Coastguard Worker     if (!extra_hal_interfaces_to_dump.empty()) {
124*38e8c45fSAndroid Build Coastguard Worker         return;
125*38e8c45fSAndroid Build Coastguard Worker     }
126*38e8c45fSAndroid Build Coastguard Worker     std::string value = android::base::GetProperty("ro.dump.hals.extra", "");
127*38e8c45fSAndroid Build Coastguard Worker     std::vector<std::string> tokens = android::base::Split(value, ",");
128*38e8c45fSAndroid Build Coastguard Worker     for (const auto &token : tokens) {
129*38e8c45fSAndroid Build Coastguard Worker         std::string trimmed_token = android::base::Trim(token);
130*38e8c45fSAndroid Build Coastguard Worker         if (trimmed_token.length() == 0) {
131*38e8c45fSAndroid Build Coastguard Worker             continue;
132*38e8c45fSAndroid Build Coastguard Worker         }
133*38e8c45fSAndroid Build Coastguard Worker         extra_hal_interfaces_to_dump.insert(std::move(trimmed_token));
134*38e8c45fSAndroid Build Coastguard Worker     }
135*38e8c45fSAndroid Build Coastguard Worker }
136*38e8c45fSAndroid Build Coastguard Worker 
137*38e8c45fSAndroid Build Coastguard Worker // check if interface is included in either default hal list or extra hal list
should_dump_hal_interface(const std::string & interface)138*38e8c45fSAndroid Build Coastguard Worker bool should_dump_hal_interface(const std::string& interface) {
139*38e8c45fSAndroid Build Coastguard Worker     for (const char** i = hidl_hal_interfaces_to_dump; *i; i++) {
140*38e8c45fSAndroid Build Coastguard Worker         if (interface == *i) {
141*38e8c45fSAndroid Build Coastguard Worker             return true;
142*38e8c45fSAndroid Build Coastguard Worker         }
143*38e8c45fSAndroid Build Coastguard Worker     }
144*38e8c45fSAndroid Build Coastguard Worker     return extra_hal_interfaces_to_dump.find(interface) != extra_hal_interfaces_to_dump.end();
145*38e8c45fSAndroid Build Coastguard Worker }
146*38e8c45fSAndroid Build Coastguard Worker 
should_dump_native_traces(const char * path)147*38e8c45fSAndroid Build Coastguard Worker bool should_dump_native_traces(const char* path) {
148*38e8c45fSAndroid Build Coastguard Worker     for (const char** p = native_processes_to_dump; *p; p++) {
149*38e8c45fSAndroid Build Coastguard Worker         if (!strcmp(*p, path)) {
150*38e8c45fSAndroid Build Coastguard Worker             return true;
151*38e8c45fSAndroid Build Coastguard Worker         }
152*38e8c45fSAndroid Build Coastguard Worker     }
153*38e8c45fSAndroid Build Coastguard Worker 
154*38e8c45fSAndroid Build Coastguard Worker     if (android::base::GetBoolProperty("ro.debuggable", false)) {
155*38e8c45fSAndroid Build Coastguard Worker         for (const char** p = debuggable_native_processes_to_dump; *p; p++) {
156*38e8c45fSAndroid Build Coastguard Worker             if (!strcmp(*p, path)) {
157*38e8c45fSAndroid Build Coastguard Worker                 return true;
158*38e8c45fSAndroid Build Coastguard Worker             }
159*38e8c45fSAndroid Build Coastguard Worker         }
160*38e8c45fSAndroid Build Coastguard Worker     }
161*38e8c45fSAndroid Build Coastguard Worker 
162*38e8c45fSAndroid Build Coastguard Worker     return false;
163*38e8c45fSAndroid Build Coastguard Worker }
164*38e8c45fSAndroid Build Coastguard Worker 
get_interesting_aidl_pids(std::set<int> & pids)165*38e8c45fSAndroid Build Coastguard Worker static void get_interesting_aidl_pids(std::set<int> &pids) {
166*38e8c45fSAndroid Build Coastguard Worker     using ServiceDebugInfo = android::IServiceManager::ServiceDebugInfo;
167*38e8c45fSAndroid Build Coastguard Worker     auto sm = android::defaultServiceManager();
168*38e8c45fSAndroid Build Coastguard Worker     std::vector<ServiceDebugInfo> serviceDebugInfos = sm->getServiceDebugInfo();
169*38e8c45fSAndroid Build Coastguard Worker     for (const auto & serviceDebugInfo : serviceDebugInfos) {
170*38e8c45fSAndroid Build Coastguard Worker         for (const auto &aidl_prefix : aidl_interfaces_to_dump) {
171*38e8c45fSAndroid Build Coastguard Worker             // Check for prefix match with aidl interface to dump
172*38e8c45fSAndroid Build Coastguard Worker             if (serviceDebugInfo.name.rfind(aidl_prefix, 0) == 0) {
173*38e8c45fSAndroid Build Coastguard Worker                 pids.insert(serviceDebugInfo.pid);
174*38e8c45fSAndroid Build Coastguard Worker             }
175*38e8c45fSAndroid Build Coastguard Worker         }
176*38e8c45fSAndroid Build Coastguard Worker     }
177*38e8c45fSAndroid Build Coastguard Worker }
178*38e8c45fSAndroid Build Coastguard Worker 
get_interesting_hidl_pids(std::set<int> & pids)179*38e8c45fSAndroid Build Coastguard Worker static void get_interesting_hidl_pids(std::set<int> &pids) {
180*38e8c45fSAndroid Build Coastguard Worker     using android::hidl::manager::V1_0::IServiceManager;
181*38e8c45fSAndroid Build Coastguard Worker     using android::sp;
182*38e8c45fSAndroid Build Coastguard Worker     using android::hardware::Return;
183*38e8c45fSAndroid Build Coastguard Worker 
184*38e8c45fSAndroid Build Coastguard Worker     sp<IServiceManager> manager = IServiceManager::getService();
185*38e8c45fSAndroid Build Coastguard Worker     read_extra_hals_to_dump_from_property();
186*38e8c45fSAndroid Build Coastguard Worker 
187*38e8c45fSAndroid Build Coastguard Worker     Return<void> ret = manager->debugDump([&](auto& hals) {
188*38e8c45fSAndroid Build Coastguard Worker         for (const auto &info : hals) {
189*38e8c45fSAndroid Build Coastguard Worker             if (info.pid == static_cast<int>(IServiceManager::PidConstant::NO_PID)) {
190*38e8c45fSAndroid Build Coastguard Worker                 continue;
191*38e8c45fSAndroid Build Coastguard Worker             }
192*38e8c45fSAndroid Build Coastguard Worker 
193*38e8c45fSAndroid Build Coastguard Worker             if (should_dump_hal_interface(info.interfaceName)) {
194*38e8c45fSAndroid Build Coastguard Worker                 pids.insert(info.pid);
195*38e8c45fSAndroid Build Coastguard Worker             }
196*38e8c45fSAndroid Build Coastguard Worker         }
197*38e8c45fSAndroid Build Coastguard Worker     });
198*38e8c45fSAndroid Build Coastguard Worker 
199*38e8c45fSAndroid Build Coastguard Worker     if (!ret.isOk()) {
200*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Could not get list of HAL PIDs: %s\n", ret.description().c_str());
201*38e8c45fSAndroid Build Coastguard Worker     }
202*38e8c45fSAndroid Build Coastguard Worker 
203*38e8c45fSAndroid Build Coastguard Worker     return;
204*38e8c45fSAndroid Build Coastguard Worker }
205*38e8c45fSAndroid Build Coastguard Worker 
get_interesting_pids()206*38e8c45fSAndroid Build Coastguard Worker std::set<int> get_interesting_pids() {
207*38e8c45fSAndroid Build Coastguard Worker     std::set<int> interesting_pids;
208*38e8c45fSAndroid Build Coastguard Worker     get_interesting_hidl_pids(interesting_pids);
209*38e8c45fSAndroid Build Coastguard Worker     get_interesting_aidl_pids(interesting_pids);
210*38e8c45fSAndroid Build Coastguard Worker     return interesting_pids;
211*38e8c45fSAndroid Build Coastguard Worker }
212*38e8c45fSAndroid Build Coastguard Worker 
IsZygote(int pid)213*38e8c45fSAndroid Build Coastguard Worker bool IsZygote(int pid) {
214*38e8c45fSAndroid Build Coastguard Worker     std::string cmdline;
215*38e8c45fSAndroid Build Coastguard Worker     if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid),
216*38e8c45fSAndroid Build Coastguard Worker                                          &cmdline)) {
217*38e8c45fSAndroid Build Coastguard Worker         return true;
218*38e8c45fSAndroid Build Coastguard Worker     }
219*38e8c45fSAndroid Build Coastguard Worker 
220*38e8c45fSAndroid Build Coastguard Worker     // cmdline has embedded nulls; only consider argv[0].
221*38e8c45fSAndroid Build Coastguard Worker     cmdline = std::string(cmdline.c_str());
222*38e8c45fSAndroid Build Coastguard Worker 
223*38e8c45fSAndroid Build Coastguard Worker     return cmdline == "zygote" || cmdline == "zygote64" || cmdline == "usap32" ||
224*38e8c45fSAndroid Build Coastguard Worker             cmdline == "usap64" || cmdline == "webview_zygote";
225*38e8c45fSAndroid Build Coastguard Worker }
226*38e8c45fSAndroid Build Coastguard Worker 
IsCached(int pid)227*38e8c45fSAndroid Build Coastguard Worker bool IsCached(int pid) {
228*38e8c45fSAndroid Build Coastguard Worker     std::string oom_score_adj;
229*38e8c45fSAndroid Build Coastguard Worker     if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/oom_score_adj",
230*38e8c45fSAndroid Build Coastguard Worker                                                                      pid),
231*38e8c45fSAndroid Build Coastguard Worker                                          &oom_score_adj)) {
232*38e8c45fSAndroid Build Coastguard Worker         return false;
233*38e8c45fSAndroid Build Coastguard Worker     }
234*38e8c45fSAndroid Build Coastguard Worker     int32_t oom_score_adj_value;
235*38e8c45fSAndroid Build Coastguard Worker     if (!android::base::ParseInt(android::base::Trim(oom_score_adj), &oom_score_adj_value)) {
236*38e8c45fSAndroid Build Coastguard Worker         return false;
237*38e8c45fSAndroid Build Coastguard Worker     }
238*38e8c45fSAndroid Build Coastguard Worker     // An OOM score greater than 900 indicates a cached process.
239*38e8c45fSAndroid Build Coastguard Worker     return oom_score_adj_value >= 900;
240*38e8c45fSAndroid Build Coastguard Worker }
241