1 /*
2 * Copyright 2016 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
17 #define LOG_TAG "dumpstate"
18
19 #include "DumpstateDevice.h"
20
21 #include <android-base/properties.h>
22 #include <android-base/unique_fd.h>
23 #include <cutils/properties.h>
24 #include <hidl/HidlBinderSupport.h>
25
26 #include <log/log.h>
27 #include <string.h>
28
29 #define _SVID_SOURCE
30 #include <dirent.h>
31
32 #include "DumpstateUtil.h"
33
34 #define MODEM_LOG_PREFIX_PROPERTY "ro.radio.log_prefix"
35 #define MODEM_LOG_LOC_PROPERTY "ro.radio.log_loc"
36 #define MODEM_LOGGING_SWITCH "persist.radio.smlog_switch"
37
38 #define DIAG_MDLOG_PERSIST_PROPERTY "persist.sys.modem.diag.mdlog"
39 #define DIAG_MDLOG_PROPERTY "sys.modem.diag.mdlog"
40 #define DIAG_MDLOG_STATUS_PROPERTY "sys.modem.diag.mdlog_on"
41
42 #define DIAG_MDLOG_NUMBER_BUGREPORT "persist.sys.modem.diag.mdlog_br_num"
43
44 using android::os::dumpstate::CommandOptions;
45 using android::os::dumpstate::DumpFileToFd;
46 using android::os::dumpstate::PropertiesHelper;
47 using android::os::dumpstate::RunCommandToFd;
48
49 namespace android {
50 namespace hardware {
51 namespace dumpstate {
52 namespace V1_0 {
53 namespace implementation {
54
55 #define DIAG_LOG_PREFIX "diag_log_"
56
dumpDiagLogs(int fd,std::string srcDir,std::string destDir)57 void DumpstateDevice::dumpDiagLogs(int fd, std::string srcDir, std::string destDir) {
58 struct dirent **dirent_list = NULL;
59 int num_entries = scandir(srcDir.c_str(),
60 &dirent_list,
61 0,
62 (int (*)(const struct dirent **, const struct dirent **)) alphasort);
63 if (!dirent_list) {
64 return;
65 } else if (num_entries <= 0) {
66 return;
67 }
68
69 int maxFileNum = android::base::GetIntProperty(DIAG_MDLOG_NUMBER_BUGREPORT, 100);
70 int copiedFiles = 0;
71
72 for (int i = num_entries - 1; i >= 0; i--) {
73 ALOGD("Found %s\n", dirent_list[i]->d_name);
74
75 if (0 != strncmp(dirent_list[i]->d_name, DIAG_LOG_PREFIX, strlen(DIAG_LOG_PREFIX))) {
76 continue;
77 }
78
79 if ((copiedFiles >= maxFileNum) && (maxFileNum != -1)) {
80 ALOGD("Skipped %s\n", dirent_list[i]->d_name);
81 continue;
82 }
83
84 copiedFiles++;
85
86 CommandOptions options = CommandOptions::WithTimeout(120).Build();
87 std::string srcLogFile = srcDir + "/" + dirent_list[i]->d_name;
88 std::string destLogFile = destDir + "/" + dirent_list[i]->d_name;
89
90 std::string copyCmd= "/vendor/bin/cp " + srcLogFile + " " + destLogFile;
91
92 ALOGD("Copying %s to %s\n", srcLogFile.c_str(), destLogFile.c_str());
93 RunCommandToFd(fd, "CP DIAG LOGS", { "/vendor/bin/sh", "-c", copyCmd.c_str() }, options);
94 }
95
96 while (num_entries--) {
97 free(dirent_list[num_entries]);
98 }
99
100 free(dirent_list);
101 }
102
dumpModem(int fd,int fdModem)103 void DumpstateDevice::dumpModem(int fd, int fdModem)
104 {
105 std::string modemLogDir = android::base::GetProperty(MODEM_LOG_LOC_PROPERTY, "");
106 if (modemLogDir.empty()) {
107 ALOGD("No modem log place is set\n");
108 return;
109 }
110
111 if (!PropertiesHelper::IsUserBuild()) {
112
113 CommandOptions options = CommandOptions::WithTimeout(120).Build();
114
115 RunCommandToFd(fd, "MODEM DIAG SYSTEM PROPERTIES",
116 { "/vendor/bin/sh", "-c", "getprop", "|", "grep", "sys.modem.diag" }, options);
117
118 std::string modemLogAllDir = modemLogDir + "/modem_log";
119 std::string diagLogDir = "/data/vendor/radio/diag_logs/logs";
120 std::vector<std::string> rilAndNetmgrLogs
121 {
122 "/data/vendor/radio/ril_log",
123 "/data/vendor/radio/ril_log_old",
124 "/data/vendor/netmgr/netmgr_log",
125 "/data/vendor/netmgr/netmgr_log_old",
126 "/data/vendor/radio/power_anomaly_data.txt",
127 "/data/vendor/radio/diag_logs/diag_trace.txt",
128 "/data/vendor/radio/diag_logs/diag_trace_old.txt",
129 };
130
131 std::string modemLogMkDirCmd= "/vendor/bin/mkdir -p " + modemLogAllDir;
132 RunCommandToFd(fd, "MKDIR MODEM LOG", { "/vendor/bin/sh", "-c", modemLogMkDirCmd.c_str()}, options);
133
134 bool smlogEnabled = android::base::GetBoolProperty(MODEM_LOGGING_SWITCH, false) &&
135 !access("/vendor/bin/smlog_dump", X_OK);
136
137 bool diagLogEnabled = android::base::GetBoolProperty(DIAG_MDLOG_PERSIST_PROPERTY, false);
138
139 if (smlogEnabled) {
140 RunCommandToFd(fd, "SMLOG DUMP", { "smlog_dump", "-d", "-o", modemLogAllDir.c_str() }, options);
141 } else if (diagLogEnabled) {
142 bool diagLogStarted = android::base::GetBoolProperty(DIAG_MDLOG_STATUS_PROPERTY, false);
143
144 if (diagLogStarted) {
145 android::base::SetProperty(DIAG_MDLOG_PROPERTY, "false");
146 ALOGD("Stopping diag_mdlog...\n");
147 } else {
148 ALOGD("diag_mdlog is not running\n");
149 }
150
151 for (int i = 0; i < 30; i++) {
152 if (!android::base::GetBoolProperty(DIAG_MDLOG_STATUS_PROPERTY, false)) {
153 ALOGD("diag_mdlog exited\n");
154 sleep(1);
155 break;
156 }
157
158 sleep(1);
159 }
160
161 dumpDiagLogs(fd, diagLogDir, modemLogAllDir);
162
163 if (diagLogStarted) {
164 ALOGD("Restarting diag_mdlog...\n");
165 android::base::SetProperty(DIAG_MDLOG_PROPERTY, "true");
166 }
167 }
168
169 for (const auto& logFile : rilAndNetmgrLogs)
170 {
171 std::string copyCmd= "/vendor/bin/cp " + logFile + " " + modemLogAllDir;
172 RunCommandToFd(fd, "CP MODEM LOG", { "/vendor/bin/sh", "-c", copyCmd.c_str() }, options);
173 }
174
175 std::string filePrefix = android::base::GetProperty(MODEM_LOG_PREFIX_PROPERTY, "");
176
177 if (!filePrefix.empty()) {
178 std::string modemLogCombined = modemLogDir + "/" + filePrefix + "all.tar";
179 std::string modemLogTarCmd= "/vendor/bin/tar cvf " + modemLogCombined + " -C " + modemLogAllDir + " .";
180 RunCommandToFd(fd, "TAR LOG", { "/vendor/bin/sh", "-c", modemLogTarCmd.c_str()}, options);
181
182 std::string modemLogPermCmd= "/vendor/bin/chmod a+rw " + modemLogCombined;
183 RunCommandToFd(fd, "CHG PERM", { "/vendor/bin/sh", "-c", modemLogPermCmd.c_str()}, options);
184
185 std::vector<uint8_t> buffer(65536);
186 android::base::unique_fd fdLog(TEMP_FAILURE_RETRY(open(modemLogCombined.c_str(), O_RDONLY | O_CLOEXEC | O_NONBLOCK)));
187
188 if (fdLog >= 0) {
189 while (1) {
190 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fdLog, buffer.data(), buffer.size()));
191
192 if (bytes_read == 0) {
193 break;
194 } else if (bytes_read < 0) {
195 ALOGD("read(%s): %s\n", modemLogCombined.c_str(), strerror(errno));
196 break;
197 }
198
199 ssize_t result = TEMP_FAILURE_RETRY(write(fdModem, buffer.data(), bytes_read));
200
201 if (result != bytes_read) {
202 ALOGD("Failed to write %ld bytes, actually written: %ld", bytes_read, result);
203 break;
204 }
205 }
206 }
207
208 std::string modemLogClearCmd= "/vendor/bin/rm -r " + modemLogAllDir;
209 RunCommandToFd(fd, "RM MODEM DIR", { "/vendor/bin/sh", "-c", modemLogClearCmd.c_str()}, options);
210 RunCommandToFd(fd, "RM LOG", { "/vendor/bin/rm", modemLogCombined.c_str()}, options);
211 }
212 }
213 }
214
DumpTouch(int fd)215 static void DumpTouch(int fd) {
216 if (!access("/sys/android_touch", R_OK)) {
217 DumpFileToFd(fd, "Synaptics touch firmware version",
218 "/sys/android_touch/vendor");
219 DumpFileToFd(fd, "Synaptics touch firmware config",
220 "/sys/android_touch/config");
221 }
222 if (!access("/sys/class/input/ftm4_touch", R_OK)) {
223 DumpFileToFd(fd, "STM touch firmware config",
224 "/sys/class/input/ftm4_touch/version");
225 DumpFileToFd(fd, "STM touch VR Mode",
226 "/sys/class/input/ftm4_touch/vrmode");
227 }
228 }
229
230 // Methods from ::android::hardware::dumpstate::V1_0::IDumpstateDevice follow.
dumpstateBoard(const hidl_handle & handle)231 Return<void> DumpstateDevice::dumpstateBoard(const hidl_handle& handle) {
232 // Exit when dump is completed since this is a lazy HAL.
233 addPostCommandTask([]() {
234 exit(0);
235 });
236
237 if (handle == nullptr || handle->numFds < 1) {
238 ALOGE("no FDs\n");
239 return Void();
240 }
241
242 int fd = handle->data[0];
243 if (fd < 0) {
244 ALOGE("invalid FD: %d\n", handle->data[0]);
245 return Void();
246 }
247
248 if (handle->numFds < 2) {
249 ALOGE("no FD for modem\n");
250 }
251 else {
252 int fdModem = handle->data[1];
253 dumpModem(fd, fdModem);
254 }
255 RunCommandToFd(fd, "VENDOR PROPERTIES", {"/vendor/bin/getprop"});
256 DumpFileToFd(fd, "SoC serial number", "/sys/devices/soc0/serial_number");
257 DumpFileToFd(fd, "CPU present", "/sys/devices/system/cpu/present");
258 DumpFileToFd(fd, "CPU online", "/sys/devices/system/cpu/online");
259 DumpFileToFd(fd, "UFS model", "/sys/block/sda/device/model");
260 DumpFileToFd(fd, "UFS rev", "/sys/block/sda/device/rev");
261 DumpFileToFd(fd, "UFS size", "/sys/block/sda/size");
262 RunCommandToFd(fd, "UFS health", {"/vendor/bin/sh", "-c", "for f in $(find /sys/kernel/debug/ufshcd0 -type f); do if [[ -r $f && -f $f ]]; then echo --- $f; cat $f; fi; done"});
263 DumpFileToFd(fd, "INTERRUPTS", "/proc/interrupts");
264 DumpFileToFd(fd, "RPM Stats", "/d/rpm_stats");
265 DumpFileToFd(fd, "Power Management Stats", "/d/rpm_master_stats");
266 DumpFileToFd(fd, "WLAN Power Stats", "/d/wlan0/power_stats");
267 DumpFileToFd(fd, "LL-Stats", "/d/wlan0/ll_stats");
268 DumpFileToFd(fd, "ICNSS Stats", "/d/icnss/stats");
269 DumpFileToFd(fd, "SMD Log", "/d/ipc_logging/smd/log");
270 RunCommandToFd(fd, "ION HEAPS", {"/vendor/bin/sh", "-c", "for d in $(ls -d /d/ion/*); do for f in $(ls $d); do echo --- $d/$f; cat $d/$f; done; done"});
271 DumpFileToFd(fd, "dmabuf info", "/d/dma_buf/bufinfo");
272 RunCommandToFd(fd, "Easel debug info", {"/vendor/bin/sh", "-c", "for f in `ls /sys/bus/i2c/devices/9-0008/@(*curr|temperature|vbat|total_power)`; do echo \"$f: `cat $f`\" ; done; file=/sys/devices/virtual/misc/mnh_sm/state; echo \"$file: `cat $file`\""});
273 RunCommandToFd(fd, "Temperatures", {"/vendor/bin/sh", "-c", "for f in /sys/class/thermal/thermal* ; do type=`cat $f/type` ; temp=`cat $f/temp` ; echo \"$type: $temp\" ; done"});
274 RunCommandToFd(fd, "Cooling Device Current State", {"/vendor/bin/sh", "-c", "for f in /sys/class/thermal/cooling* ; do type=`cat $f/type` ; temp=`cat $f/cur_state` ; echo \"$type: $temp\" ; done"});
275 RunCommandToFd(fd, "CPU time-in-state", {"/vendor/bin/sh", "-c", "for cpu in /sys/devices/system/cpu/cpu*; do f=$cpu/cpufreq/stats/time_in_state; if [ ! -f $f ]; then continue; fi; echo $f:; cat $f; done"});
276 RunCommandToFd(fd, "CPU cpuidle", {"/vendor/bin/sh", "-c", "for cpu in /sys/devices/system/cpu/cpu*; do for d in $cpu/cpuidle/state*; do if [ ! -d $d ]; then continue; fi; echo \"$d: `cat $d/name` `cat $d/desc` `cat $d/time` `cat $d/usage`\"; done; done"});
277 DumpFileToFd(fd, "MDP xlogs", "/data/vendor/display/mdp_xlog");
278 DumpFileToFd(fd, "TCPM logs", "/d/tcpm/usbpd0");
279 DumpFileToFd(fd, "PD Engine", "/d/pd_engine/usbpd0");
280 DumpFileToFd(fd, "smblib-usb logs", "/d/ipc_logging/smblib/log");
281 DumpFileToFd(fd, "ipc-local-ports", "/d/msm_ipc_router/dump_local_ports");
282 DumpFileToFd(fd, "ipc-servers", "/d/msm_ipc_router/dump_servers");
283 RunCommandToFd(fd, "ipc-logs",
284 {"/vendor/bin/sh", "-c",
285 "for f in `ls /d/ipc_logging/*_IPCRTR/log` ; do echo \"------ $f\\n`cat "
286 "$f`\\n\" ; done"});
287 DumpTouch(fd);
288 RunCommandToFd(fd, "USB Device Descriptors", {"/vendor/bin/sh", "-c", "cd /sys/bus/usb/devices/1-1 && cat product && cat bcdDevice; cat descriptors | od -t x1 -w16 -N96"});
289 DumpFileToFd(fd, "Pixel trace", "/d/tracing/instances/pixel-trace/trace");
290
291 // Timeout after 3s
292 RunCommandToFd(fd, "QSEE logs", {"/vendor/bin/sh", "-c", "/vendor/bin/timeout 3 cat /d/tzdbg/qsee_log"});
293 RunCommandToFd(fd, "Power supply properties", {"/vendor/bin/sh", "-c", "for f in /sys/class/power_supply/*/uevent ; do echo \"\n------ $f\" ; cat $f ; done"});
294 DumpFileToFd(fd, "Battery cycle count", "/sys/class/power_supply/bms/device/cycle_counts_bins");
295 RunCommandToFd(fd, "QCOM FG SRAM", {"/vendor/bin/sh", "-c", "echo 0 > /d/fg/sram/address ; echo 500 > /d/fg/sram/count ; cat /d/fg/sram/data"});
296
297 DumpFileToFd(fd, "WLAN FW Log Symbol Table", "/vendor/firmware/Data.msc");
298
299 return Void();
300 };
301
302 } // namespace implementation
303 } // namespace V1_0
304 } // namespace dumpstate
305 } // namespace hardware
306 } // namespace android
307