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