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/stringprintf.h>
20 #include <android/hidl/manager/1.0/IServiceManager.h>
21 #include <dumputils/dump_utils.h>
22 #include <log/log.h>
23
24 /* list of native processes to include in the native dumps */
25 // This matches the /proc/pid/exe link instead of /proc/pid/cmdline.
26 static const char* native_processes_to_dump[] = {
27 "/system/bin/audioserver",
28 "/system/bin/cameraserver",
29 "/system/bin/drmserver",
30 "/system/bin/mediadrmserver",
31 "/system/bin/mediaextractor", // media.extractor
32 "/system/bin/mediametrics", // media.metrics
33 "/system/bin/mediaserver",
34 "/system/bin/netd",
35 "/system/bin/vold",
36 "/system/bin/sdcard",
37 "/system/bin/statsd",
38 "/system/bin/surfaceflinger",
39 "/system/bin/vehicle_network_service",
40 "/vendor/bin/hw/android.hardware.media.omx@1.0-service", // media.codec
41 "/apex/com.android.media.swcodec/bin/mediaswcodec", // media.swcodec
42 NULL,
43 };
44
45 /* list of hal interface to dump containing process during native dumps */
46 static const char* hal_interfaces_to_dump[] {
47 "android.hardware.audio@2.0::IDevicesFactory",
48 "android.hardware.audio@4.0::IDevicesFactory",
49 "android.hardware.bluetooth@1.0::IBluetoothHci",
50 "android.hardware.camera.provider@2.4::ICameraProvider",
51 "android.hardware.drm@1.0::IDrmFactory",
52 "android.hardware.graphics.allocator@2.0::IAllocator",
53 "android.hardware.graphics.composer@2.1::IComposer",
54 "android.hardware.health@2.0::IHealth",
55 "android.hardware.media.c2@1.0::IComponentStore",
56 "android.hardware.media.omx@1.0::IOmx",
57 "android.hardware.media.omx@1.0::IOmxStore",
58 "android.hardware.power@1.3::IPower",
59 "android.hardware.power.stats@1.0::IPowerStats",
60 "android.hardware.sensors@1.0::ISensors",
61 "android.hardware.thermal@2.0::IThermal",
62 "android.hardware.vr@1.0::IVr",
63 NULL,
64 };
65
should_dump_hal_interface(const char * interface)66 bool should_dump_hal_interface(const char* interface) {
67 for (const char** i = hal_interfaces_to_dump; *i; i++) {
68 if (!strcmp(*i, interface)) {
69 return true;
70 }
71 }
72 return false;
73 }
74
should_dump_native_traces(const char * path)75 bool should_dump_native_traces(const char* path) {
76 for (const char** p = native_processes_to_dump; *p; p++) {
77 if (!strcmp(*p, path)) {
78 return true;
79 }
80 }
81 return false;
82 }
83
get_interesting_hal_pids()84 std::set<int> get_interesting_hal_pids() {
85 using android::hidl::manager::V1_0::IServiceManager;
86 using android::sp;
87 using android::hardware::Return;
88
89 sp<IServiceManager> manager = IServiceManager::getService();
90 std::set<int> pids;
91
92 Return<void> ret = manager->debugDump([&](auto& hals) {
93 for (const auto &info : hals) {
94 if (info.pid == static_cast<int>(IServiceManager::PidConstant::NO_PID)) {
95 continue;
96 }
97
98 if (!should_dump_hal_interface(info.interfaceName.c_str())) {
99 continue;
100 }
101
102 pids.insert(info.pid);
103 }
104 });
105
106 if (!ret.isOk()) {
107 ALOGE("Could not get list of HAL PIDs: %s\n", ret.description().c_str());
108 }
109
110 return pids; // whether it was okay or not
111 }
112
IsZygote(int pid)113 bool IsZygote(int pid) {
114 std::string cmdline;
115 if (!android::base::ReadFileToString(android::base::StringPrintf("/proc/%d/cmdline", pid),
116 &cmdline)) {
117 return true;
118 }
119
120 // cmdline has embedded nulls; only consider argv[0].
121 cmdline = std::string(cmdline.c_str());
122
123 return cmdline == "zygote" || cmdline == "zygote64" || cmdline == "usap32" ||
124 cmdline == "usap64";
125 }
126