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