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/properties.h>
20 #include <android-base/stringprintf.h>
21 #include <android-base/strings.h>
22 #include <android/hidl/manager/1.0/IServiceManager.h>
23 #include <binder/IServiceManager.h>
24 #include <dumputils/dump_utils.h>
25 #include <log/log.h>
26
27 /* list of native processes to include in the native dumps */
28 // This matches the /proc/pid/exe link instead of /proc/pid/cmdline.
29 static const char* native_processes_to_dump[] = {
30 "/system/bin/audioserver",
31 "/system/bin/cameraserver",
32 "/system/bin/drmserver",
33 "/system/bin/mediadrmserver",
34 "/system/bin/mediaextractor", // media.extractor
35 "/system/bin/mediametrics", // media.metrics
36 "/system/bin/mediaserver",
37 "/system/bin/mediatranscoding", // media.transcoding
38 "/system/bin/netd",
39 "/system/bin/sdcard",
40 "/apex/com.android.os.statsd/bin/statsd",
41 "/system/bin/surfaceflinger",
42 "/system/bin/vehicle_network_service",
43 "/vendor/bin/hw/android.hardware.media.omx@1.0-service", // media.codec
44 "/apex/com.android.media.swcodec/bin/mediaswcodec", // media.swcodec
45 NULL,
46 };
47
48
49 // Native processes to dump on debuggable builds.
50 static const char* debuggable_native_processes_to_dump[] = {
51 "/system/bin/keystore2",
52 "/system/bin/vold",
53 NULL,
54 };
55
56 /* list of hidl hal interface to dump containing process during native dumps */
57 static const char* hidl_hal_interfaces_to_dump[] {
58 "android.hardware.audio@4.0::IDevicesFactory",
59 "android.hardware.audio@5.0::IDevicesFactory",
60 "android.hardware.audio@6.0::IDevicesFactory",
61 "android.hardware.audio@7.0::IDevicesFactory",
62 "android.hardware.automotive.audiocontrol@1.0::IAudioControl",
63 "android.hardware.automotive.audiocontrol@2.0::IAudioControl",
64 "android.hardware.automotive.evs@1.0::IEvsCamera",
65 "android.hardware.automotive.vehicle@2.0::IVehicle",
66 "android.hardware.biometrics.face@1.0::IBiometricsFace",
67 "android.hardware.biometrics.fingerprint@2.1::IBiometricsFingerprint",
68 "android.hardware.bluetooth@1.0::IBluetoothHci",
69 "android.hardware.camera.provider@2.4::ICameraProvider",
70 "android.hardware.drm@1.0::IDrmFactory",
71 "android.hardware.graphics.allocator@2.0::IAllocator",
72 "android.hardware.graphics.composer@2.1::IComposer",
73 "android.hardware.health@2.0::IHealth",
74 "android.hardware.media.c2@1.0::IComponentStore",
75 "android.hardware.media.omx@1.0::IOmx",
76 "android.hardware.media.omx@1.0::IOmxStore",
77 "android.hardware.neuralnetworks@1.0::IDevice",
78 "android.hardware.power@1.3::IPower",
79 "android.hardware.power.stats@1.0::IPowerStats",
80 "android.hardware.sensors@1.0::ISensors",
81 "android.hardware.thermal@2.0::IThermal",
82 "android.hardware.vr@1.0::IVr",
83 NULL,
84 };
85
86 /* list of hal interface to dump containing process during native dumps */
87 static const std::vector<std::string> aidl_interfaces_to_dump {
88 "android.hardware.camera.provider.ICameraProvider",
89 "android.hardware.input.processor.IInputProcessor",
90 };
91
92 /* list of extra hal interfaces to dump containing process during native dumps */
93 // This is filled when dumpstate is called.
94 static std::set<const std::string> extra_hal_interfaces_to_dump;
95
read_extra_hals_to_dump_from_property()96 static void read_extra_hals_to_dump_from_property() {
97 // extra hals to dump are already filled
98 if (!extra_hal_interfaces_to_dump.empty()) {
99 return;
100 }
101 std::string value = android::base::GetProperty("ro.dump.hals.extra", "");
102 std::vector<std::string> tokens = android::base::Split(value, ",");
103 for (const auto &token : tokens) {
104 std::string trimmed_token = android::base::Trim(token);
105 if (trimmed_token.length() == 0) {
106 continue;
107 }
108 extra_hal_interfaces_to_dump.insert(trimmed_token);
109 }
110 }
111
112 // check if interface is included in either default hal list or extra hal list
should_dump_hal_interface(const std::string & interface)113 bool should_dump_hal_interface(const std::string& interface) {
114 for (const char** i = hidl_hal_interfaces_to_dump; *i; i++) {
115 if (interface == *i) {
116 return true;
117 }
118 }
119 return extra_hal_interfaces_to_dump.find(interface) != extra_hal_interfaces_to_dump.end();
120 }
121
should_dump_native_traces(const char * path)122 bool should_dump_native_traces(const char* path) {
123 for (const char** p = native_processes_to_dump; *p; p++) {
124 if (!strcmp(*p, path)) {
125 return true;
126 }
127 }
128
129 if (android::base::GetBoolProperty("ro.debuggable", false)) {
130 for (const char** p = debuggable_native_processes_to_dump; *p; p++) {
131 if (!strcmp(*p, path)) {
132 return true;
133 }
134 }
135 }
136
137 return false;
138 }
139
get_interesting_aidl_pids(std::set<int> & pids)140 static void get_interesting_aidl_pids(std::set<int> &pids) {
141 using ServiceDebugInfo = android::IServiceManager::ServiceDebugInfo;
142 auto sm = android::defaultServiceManager();
143 std::vector<ServiceDebugInfo> serviceDebugInfos = sm->getServiceDebugInfo();
144 for (const auto & serviceDebugInfo : serviceDebugInfos) {
145 for (const auto &aidl_prefix : aidl_interfaces_to_dump) {
146 // Check for prefix match with aidl interface to dump
147 if (serviceDebugInfo.name.rfind(aidl_prefix, 0) == 0) {
148 pids.insert(serviceDebugInfo.pid);
149 }
150 }
151 }
152 }
153
get_interesting_hidl_pids(std::set<int> & pids)154 static void get_interesting_hidl_pids(std::set<int> &pids) {
155 using android::hidl::manager::V1_0::IServiceManager;
156 using android::sp;
157 using android::hardware::Return;
158
159 sp<IServiceManager> manager = IServiceManager::getService();
160 read_extra_hals_to_dump_from_property();
161
162 Return<void> ret = manager->debugDump([&](auto& hals) {
163 for (const auto &info : hals) {
164 if (info.pid == static_cast<int>(IServiceManager::PidConstant::NO_PID)) {
165 continue;
166 }
167
168 if (should_dump_hal_interface(info.interfaceName)) {
169 pids.insert(info.pid);
170 }
171 }
172 });
173
174 if (!ret.isOk()) {
175 ALOGE("Could not get list of HAL PIDs: %s\n", ret.description().c_str());
176 }
177
178 return;
179 }
180
get_interesting_pids()181 std::set<int> get_interesting_pids() {
182 std::set<int> interesting_pids;
183 get_interesting_hidl_pids(interesting_pids);
184 get_interesting_aidl_pids(interesting_pids);
185 return interesting_pids;
186 }
187
IsZygote(int pid)188 bool IsZygote(int pid) {
189 std::string cmdline;
190 if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid),
191 &cmdline)) {
192 return true;
193 }
194
195 // cmdline has embedded nulls; only consider argv[0].
196 cmdline = std::string(cmdline.c_str());
197
198 return cmdline == "zygote" || cmdline == "zygote64" || cmdline == "usap32" ||
199 cmdline == "usap64";
200 }
201