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