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