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 
DumpPower(int fd)215 static void DumpPower(int fd) {
216     RunCommandToFd(fd, "Power Stats Times", {"/vendor/bin/sh", "-c",
217                    "echo -n \"Boot: \" && /vendor/bin/uptime -s &&"
218                    "echo -n \"Now: \" && date"});
219     DumpFileToFd(fd, "RPM Stats", "/d/rpm_stats");
220     DumpFileToFd(fd, "Power Management Stats", "/d/rpm_master_stats");
221     DumpFileToFd(fd, "WLAN Power Stats", "/d/wlan0/power_stats");
222 }
223 
DumpTouch(int fd)224 static void DumpTouch(int fd) {
225     if (!access("/sys/android_touch", R_OK)) {
226         DumpFileToFd(fd, "Synaptics touch firmware version",
227                      "/sys/android_touch/vendor");
228         DumpFileToFd(fd, "Synaptics touch firmware config",
229                      "/sys/android_touch/config");
230     }
231     if (!access("/sys/class/input/ftm4_touch", R_OK)) {
232         DumpFileToFd(fd, "STM touch firmware config",
233                      "/sys/class/input/ftm4_touch/version");
234         DumpFileToFd(fd, "STM touch VR Mode",
235                      "/sys/class/input/ftm4_touch/vrmode");
236     }
237 }
238 
239 // Methods from ::android::hardware::dumpstate::V1_0::IDumpstateDevice follow.
dumpstateBoard(const hidl_handle & handle)240 Return<void> DumpstateDevice::dumpstateBoard(const hidl_handle& handle) {
241     // Exit when dump is completed since this is a lazy HAL.
242     addPostCommandTask([]() {
243         exit(0);
244     });
245 
246     if (handle == nullptr || handle->numFds < 1) {
247         ALOGE("no FDs\n");
248         return Void();
249     }
250 
251     int fd = handle->data[0];
252     if (fd < 0) {
253         ALOGE("invalid FD: %d\n", handle->data[0]);
254         return Void();
255     }
256 
257     if (handle->numFds < 2) {
258         ALOGE("no FD for modem\n");
259     }
260     else {
261         int fdModem = handle->data[1];
262         dumpModem(fd, fdModem);
263     }
264     RunCommandToFd(fd, "VENDOR PROPERTIES", {"/vendor/bin/getprop"});
265     DumpFileToFd(fd, "SoC serial number", "/sys/devices/soc0/serial_number");
266     DumpFileToFd(fd, "CPU present", "/sys/devices/system/cpu/present");
267     DumpFileToFd(fd, "CPU online", "/sys/devices/system/cpu/online");
268     DumpFileToFd(fd, "UFS model", "/sys/block/sda/device/model");
269     DumpFileToFd(fd, "UFS rev", "/sys/block/sda/device/rev");
270     DumpFileToFd(fd, "UFS size", "/sys/block/sda/size");
271     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"});
272     DumpFileToFd(fd, "INTERRUPTS", "/proc/interrupts");
273 
274     DumpPower(fd);
275 
276     DumpFileToFd(fd, "LL-Stats", "/d/wlan0/ll_stats");
277     DumpFileToFd(fd, "ICNSS Stats", "/d/icnss/stats");
278     DumpFileToFd(fd, "SMD Log", "/d/ipc_logging/smd/log");
279     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"});
280     DumpFileToFd(fd, "dmabuf info", "/d/dma_buf/bufinfo");
281     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`\""});
282     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"});
283     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"});
284     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"});
285     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"});
286     DumpFileToFd(fd, "MDP xlogs", "/data/vendor/display/mdp_xlog");
287     DumpFileToFd(fd, "TCPM logs", "/d/tcpm/usbpd0");
288     DumpFileToFd(fd, "PD Engine", "/d/pd_engine/usbpd0");
289     DumpFileToFd(fd, "smblib-usb logs", "/d/ipc_logging/smblib/log");
290     DumpFileToFd(fd, "ipc-local-ports", "/d/msm_ipc_router/dump_local_ports");
291     DumpFileToFd(fd, "ipc-servers", "/d/msm_ipc_router/dump_servers");
292     RunCommandToFd(fd, "ipc-logs",
293                    {"/vendor/bin/sh", "-c",
294                     "for f in `ls /d/ipc_logging/*_IPCRTR/log` ; do echo \"------ $f\\n`cat "
295                     "$f`\\n\" ; done"});
296     DumpTouch(fd);
297     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"});
298     DumpFileToFd(fd, "Pixel trace", "/d/tracing/instances/pixel-trace/trace");
299 
300     // Timeout after 3s
301     RunCommandToFd(fd, "QSEE logs", {"/vendor/bin/sh", "-c", "/vendor/bin/timeout 3 cat /d/tzdbg/qsee_log"});
302     RunCommandToFd(fd, "Power supply properties", {"/vendor/bin/sh", "-c", "for f in /sys/class/power_supply/*/uevent ; do echo \"\n------ $f\" ; cat $f ; done"});
303     DumpFileToFd(fd, "Battery cycle count", "/sys/class/power_supply/bms/device/cycle_counts_bins");
304     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"});
305 
306     DumpFileToFd(fd, "WLAN FW Log Symbol Table", "/vendor/firmware/Data.msc");
307 
308     return Void();
309 };
310 
311 }  // namespace implementation
312 }  // namespace V1_0
313 }  // namespace dumpstate
314 }  // namespace hardware
315 }  // namespace android
316