1 /*
2  * Copyright 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 
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 #include <hidl/HidlSupport.h>
26 
27 #include <log/log.h>
28 #include <pthread.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 
32 #define _SVID_SOURCE
33 #include <dirent.h>
34 
35 #include "DumpstateUtil.h"
36 
37 #define MODEM_LOG_PREFIX_PROPERTY "ro.vendor.radio.log_prefix"
38 #define MODEM_LOG_LOC_PROPERTY "ro.vendor.radio.log_loc"
39 #define MODEM_LOGGING_SWITCH "persist.vendor.radio.smlog_switch"
40 
41 #define DIAG_MDLOG_PERSIST_PROPERTY "persist.vendor.sys.modem.diag.mdlog"
42 #define DIAG_MDLOG_PROPERTY "vendor.sys.modem.diag.mdlog"
43 #define DIAG_MDLOG_STATUS_PROPERTY "vendor.sys.modem.diag.mdlog_on"
44 
45 #define DIAG_MDLOG_NUMBER_BUGREPORT "persist.vendor.sys.modem.diag.mdlog_br_num"
46 
47 #define TCPDUMP_NUMBER_BUGREPORT "persist.vendor.tcpdump.log.br_num"
48 #define TCPDUMP_PERSIST_PROPERTY "persist.vendor.tcpdump.log.alwayson"
49 
50 #define MODEM_EFS_DUMP_PROPERTY "vendor.sys.modem.diag.efsdump"
51 
52 #define VENDOR_VERBOSE_LOGGING_ENABLED_PROPERTY "persist.vendor.verbose_logging_enabled"
53 
54 using android::os::dumpstate::CommandOptions;
55 using android::os::dumpstate::DumpFileToFd;
56 using android::os::dumpstate::PropertiesHelper;
57 using android::os::dumpstate::RunCommandToFd;
58 
59 namespace android {
60 namespace hardware {
61 namespace dumpstate {
62 namespace V1_1 {
63 namespace implementation {
64 
65 #define DIAG_LOG_PREFIX "diag_log_"
66 #define TCPDUMP_LOG_PREFIX "tcpdump"
67 #define EXTENDED_LOG_PREFIX "extended_log_"
68 
69 #define BUFSIZE 65536
copyFile(std::string srcFile,std::string destFile)70 static void copyFile(std::string srcFile, std::string destFile) {
71     uint8_t buffer[BUFSIZE];
72     ssize_t size;
73 
74     int fdSrc = open(srcFile.c_str(), O_RDONLY);
75     if (fdSrc < 0) {
76         ALOGD("Failed to open source file %s\n", srcFile.c_str());
77         return;
78     }
79 
80     int fdDest = open(destFile.c_str(), O_WRONLY | O_CREAT, 0666);
81     if (fdDest < 0) {
82         ALOGD("Failed to open destination file %s\n", destFile.c_str());
83         close(fdSrc);
84         return;
85     }
86 
87     ALOGD("Copying %s to %s\n", srcFile.c_str(), destFile.c_str());
88     while ((size = TEMP_FAILURE_RETRY(read(fdSrc, buffer, BUFSIZE))) > 0) {
89         TEMP_FAILURE_RETRY(write(fdDest, buffer, size));
90     }
91 
92     close(fdDest);
93     close(fdSrc);
94 }
95 
96 struct PosixTarHeader {
97     char name[100];               /*   0 */
98     char mode[8];                 /* 100 */
99     char uid[8];                  /* 108 */
100     char gid[8];                  /* 116 */
101     char size[12];                /* 124 */
102     char mtime[12];               /* 136 */
103     char chksum[8];               /* 148 */
104     char typeflag;                /* 156 */
105     char linkname[100];           /* 157 */
106     char magic[6];                /* 257 */
107     char version[2];              /* 259 */
108     char uname[32];               /* 265 */
109     char gname[32];               /* 297 */
110     char devmajor[8];             /* 329 */
111     char devminor[8];             /* 337 */
112     char prefix[155];             /* 345 */
113     char pad[12];                 /* 500 */
114 };
115 
tarCheckSum(PosixTarHeader * header)116 static unsigned int tarCheckSum(PosixTarHeader *header) {
117     unsigned int sum = 0;
118     char *p = (char *)header;
119     char *q = p + sizeof(PosixTarHeader);
120     for (int i = 0; i < 8; i++) {
121         header->chksum[i] = ' ';
122     }
123     while (p < q) {
124         sum += *p++ & 0xff;
125     }
126     return sum;
127 }
128 
tarHeader(PosixTarHeader * header,char * fileName,ssize_t fileSize)129 static PosixTarHeader *tarHeader(PosixTarHeader *header, char *fileName, ssize_t fileSize) {
130     memset(header, 0, sizeof(PosixTarHeader));
131     strcpy(header->name, fileName);
132     sprintf(header->mode, "%07o", 0600);
133     sprintf(header->size, "%011llo", (long long unsigned int)fileSize);
134     sprintf(header->mtime, "%011o", 0);
135     header->typeflag = '0';
136     strcpy(header->magic, "ustar");
137     strcpy(header->version, " ");
138     sprintf(header->chksum, "%06o", tarCheckSum(header));
139     return header;
140 }
141 
createTarFile(std::string tarFile,std::string srcDir)142 static void createTarFile(std::string tarFile, std::string srcDir) {
143     uint8_t buffer[BUFSIZE];
144     struct dirent *dirent;
145     struct stat st;
146     PosixTarHeader header;
147     ssize_t size, tarLen = 0, blockSize = sizeof(PosixTarHeader);
148     char padding = '\0';
149 
150     DIR *dirp = opendir(srcDir.c_str());
151     if (dirp == NULL) {
152         ALOGD("Unable to open folder %s\n", srcDir.c_str());
153         return;
154     }
155 
156     int fdTar = open(tarFile.c_str(), O_WRONLY | O_CREAT, 0666);
157     if (fdTar < 0) {
158         ALOGD("Unable to open file %s\n", tarFile.c_str());
159         closedir(dirp);
160         return;
161     }
162 
163     ALOGD("Creating tar file %s\n", tarFile.c_str());
164     while ((dirent = readdir(dirp)) != NULL) {
165         if (dirent->d_name[0] == '.') {
166             continue;
167         }
168 
169         std::string path = srcDir + "/" + dirent->d_name;
170         int fd = open(path.c_str(), O_RDONLY);
171         if (fd < 0) {
172             ALOGD("Unable to open file %s\n", path.c_str());
173             continue;
174         }
175         fstat(fd, &st);
176 
177         if (TEMP_FAILURE_RETRY(write(fdTar, tarHeader(
178             &header, dirent->d_name, st.st_size), blockSize)) <= 0) {
179             ALOGD("Error while writing file %s, errno=%d\n", tarFile.c_str(), errno);
180             close(fd);
181             continue;
182         }
183         tarLen += blockSize;
184 
185         while ((size = TEMP_FAILURE_RETRY(read(fd, buffer, BUFSIZE))) > 0) {
186             write(fdTar, buffer, size);
187             tarLen += size;
188         }
189 
190         while (tarLen % blockSize != 0) {
191             write(fdTar, &padding, 1);
192             tarLen++;
193         }
194         close(fd);
195     }
196     close(fdTar);
197     closedir(dirp);
198 }
199 
dumpLogs(int fd,std::string srcDir,std::string destDir,int maxFileNum,const char * logPrefix)200 static void dumpLogs(int fd, std::string srcDir, std::string destDir,
201                      int maxFileNum, const char *logPrefix) {
202     (void) fd;
203     struct dirent **dirent_list = NULL;
204     int num_entries = scandir(srcDir.c_str(),
205                               &dirent_list,
206                               0,
207                               (int (*)(const struct dirent **, const struct dirent **)) alphasort);
208 
209     if (!dirent_list) {
210         return;
211     } else if (num_entries <= 0) {
212         return;
213     }
214 
215     int copiedFiles = 0;
216 
217     for (int i = num_entries - 1; i >= 0; i--) {
218         ALOGD("Found %s\n", dirent_list[i]->d_name);
219 
220         if (0 != strncmp(dirent_list[i]->d_name, logPrefix, strlen(logPrefix))) {
221             continue;
222         }
223 
224         if ((copiedFiles >= maxFileNum) && (maxFileNum != -1)) {
225             ALOGD("Skipped %s\n", dirent_list[i]->d_name);
226             continue;
227         }
228 
229         copiedFiles++;
230 
231         std::string srcLogFile = srcDir + "/" + dirent_list[i]->d_name;
232         std::string destLogFile = destDir + "/" + dirent_list[i]->d_name;
233         copyFile(srcLogFile, destLogFile);
234     }
235 
236     while (num_entries--) {
237         free(dirent_list[num_entries]);
238     }
239 
240     free(dirent_list);
241 }
242 
dumpModemThread(void * data)243 static void *dumpModemThread(void *data)
244 {
245     long fdModem = (long)data;
246 
247     ALOGD("dumpModemThread started\n");
248 
249     std::string modemLogDir = android::base::GetProperty(MODEM_LOG_LOC_PROPERTY, "");
250     if (modemLogDir.empty()) {
251         ALOGD("No modem log place is set");
252         return NULL;
253     }
254 
255     std::string filePrefix = android::base::GetProperty(MODEM_LOG_PREFIX_PROPERTY, "");
256 
257     if (filePrefix.empty()) {
258         ALOGD("Modem log prefix is not set");
259         return NULL;
260     }
261 
262     bool diagLogEnabled = android::base::GetBoolProperty(DIAG_MDLOG_PERSIST_PROPERTY, false);
263     bool diagLogStarted = android::base::GetBoolProperty(DIAG_MDLOG_STATUS_PROPERTY, false);
264 
265     if (diagLogEnabled) {
266         if (diagLogStarted) {
267             android::base::SetProperty(DIAG_MDLOG_PROPERTY, "false");
268             ALOGD("Stopping diag_mdlog...\n");
269             if (android::base::WaitForProperty(DIAG_MDLOG_STATUS_PROPERTY, "false", std::chrono::seconds(10))) {
270                 ALOGD("diag_mdlog exited");
271             } else {
272                 ALOGE("Waited mdlog timeout after 10 second");
273             }
274         } else {
275             ALOGD("diag_mdlog is not running");
276         }
277     }
278 
279     sleep(1);
280     ALOGD("Waited modem for 1 second to flush logs");
281 
282     const std::string modemLogCombined = modemLogDir + "/" + filePrefix + "all.tar";
283     const std::string modemLogAllDir = modemLogDir + "/modem_log";
284 
285     RunCommandToFd(STDOUT_FILENO, "MKDIR MODEM LOG", {"/vendor/bin/mkdir", "-p", modemLogAllDir.c_str()}, CommandOptions::WithTimeout(2).Build());
286 
287     const std::string diagLogDir = "/data/vendor/radio/diag_logs/logs";
288     const std::string diagPoweronLogPath = "/data/vendor/radio/diag_logs/logs/diag_poweron_log.qmdl";
289 
290     if (diagLogEnabled) {
291         dumpLogs(STDOUT_FILENO, diagLogDir, modemLogAllDir, android::base::GetIntProperty(DIAG_MDLOG_NUMBER_BUGREPORT, 100), DIAG_LOG_PREFIX);
292 
293         if (diagLogStarted) {
294             ALOGD("Restarting diag_mdlog...");
295             android::base::SetProperty(DIAG_MDLOG_PROPERTY, "true");
296         }
297     }
298     copyFile(diagPoweronLogPath, modemLogAllDir + "/" + basename(diagPoweronLogPath.c_str()));
299 
300     if (!PropertiesHelper::IsUserBuild()) {
301         char cmd[256] = { 0 };
302 
303         android::base::SetProperty(MODEM_EFS_DUMP_PROPERTY, "true");
304 
305         const std::string tcpdumpLogDir = "/data/vendor/tcpdump_logger/logs";
306         const std::string extendedLogDir = "/data/vendor/radio/extended_logs";
307         const std::vector <std::string> rilAndNetmgrLogs
308             {
309                 "/data/vendor/radio/haldebug_ril0",
310                 "/data/vendor/radio/haldebug_ril1",
311                 "/data/vendor/radio/ril_log0",
312                 "/data/vendor/radio/ril_log0_old",
313                 "/data/vendor/radio/ril_log1",
314                 "/data/vendor/radio/ril_log1_old",
315                 "/data/vendor/radio/qmi_fw_log",
316                 "/data/vendor/radio/qmi_fw_log_old",
317                 "/data/vendor/radio/imsdatadaemon_log",
318                 "/data/vendor/radio/imsdatadaemon_log_old",
319                 "/data/vendor/netmgr/netmgr_log",
320                 "/data/vendor/netmgr/netmgr_log_old",
321                 "/data/vendor/radio/omadm_logs.txt",
322                 "/data/vendor/radio/power_anomaly_data.txt",
323                 "/data/vendor/radio/diag_logs/diag_trace.txt",
324                 "/data/vendor/radio/diag_logs/diag_trace_old.txt",
325                 "/data/vendor/radio/metrics_data",
326                 "/data/vendor/ssrlog/ssr_log.txt",
327                 "/data/vendor/ssrlog/ssr_log_old.txt",
328                 "/data/vendor/rfs/mpss/modem_efs",
329                 "/sys/kernel/debug/ipa/ipa_statistics_msg"
330             };
331 
332         bool tcpdumpEnabled = android::base::GetBoolProperty(TCPDUMP_PERSIST_PROPERTY, false);
333         if (tcpdumpEnabled) {
334             dumpLogs(STDOUT_FILENO, tcpdumpLogDir, modemLogAllDir, android::base::GetIntProperty(TCPDUMP_NUMBER_BUGREPORT, 5), TCPDUMP_LOG_PREFIX);
335         }
336 
337         for (const auto& logFile : rilAndNetmgrLogs) {
338             copyFile(logFile, modemLogAllDir + "/" + basename(logFile.c_str()));
339         }
340 
341         //Dump IPA log
342         snprintf(cmd, sizeof(cmd),
343                 "cat /d/ipc_logging/ipa/log > %s/ipa_log",
344                 modemLogAllDir.c_str());
345         RunCommandToFd(STDOUT_FILENO, "Dump IPA log", {"/vendor/bin/sh", "-c", cmd});
346 
347         dumpLogs(STDOUT_FILENO, extendedLogDir, modemLogAllDir, 100, EXTENDED_LOG_PREFIX);
348         android::base::SetProperty(MODEM_EFS_DUMP_PROPERTY, "false");
349     }
350 
351     createTarFile(modemLogCombined, modemLogAllDir);
352 
353     std::vector<uint8_t> buffer(65536);
354     android::base::unique_fd fdLog(TEMP_FAILURE_RETRY(open(modemLogCombined.c_str(), O_RDONLY | O_CLOEXEC | O_NONBLOCK)));
355 
356     if (fdLog >= 0) {
357         while (1) {
358             ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fdLog, buffer.data(), buffer.size()));
359 
360             if (bytes_read == 0) {
361                 break;
362             } else if (bytes_read < 0) {
363                 ALOGD("read(%s): %s\n", modemLogCombined.c_str(), strerror(errno));
364                 break;
365             }
366 
367             ssize_t result = TEMP_FAILURE_RETRY(write(fdModem, buffer.data(), bytes_read));
368 
369             if (result != bytes_read) {
370                 ALOGD("Failed to write %ld bytes, actually written: %ld", bytes_read, result);
371                 break;
372             }
373         }
374     }
375 
376     RunCommandToFd(STDOUT_FILENO, "RM MODEM DIR", { "/vendor/bin/rm", "-r", modemLogAllDir.c_str()}, CommandOptions::WithTimeout(2).Build());
377     RunCommandToFd(STDOUT_FILENO, "RM LOG", { "/vendor/bin/rm", modemLogCombined.c_str()}, CommandOptions::WithTimeout(2).Build());
378 
379     ALOGD("dumpModemThread finished\n");
380 
381     return NULL;
382 }
383 
DumpTouch(int fd)384 static void DumpTouch(int fd) {
385     const char touch_spi_path[] = "/sys/devices/virtual/sec/tsp";
386     char cmd[256];
387 
388     if (!access(touch_spi_path, R_OK)) {
389         //Enable: force touch active
390         snprintf(cmd, sizeof(cmd),
391                  "echo %s > %s/cmd && cat %s/cmd_result",
392                  "force_touch_active,1",
393                  touch_spi_path, touch_spi_path);
394         RunCommandToFd(fd, "Force Touch Active", {"/vendor/bin/sh", "-c", cmd});
395 
396         //Change data format from portrait to landscape
397         snprintf(cmd, sizeof(cmd),
398                  "echo %s > %s/cmd && cat %s/cmd_result",
399                  "set_print_format,1",
400                  touch_spi_path, touch_spi_path);
401         RunCommandToFd(fd, "Print Format", {"/vendor/bin/sh", "-c", cmd});
402 
403         //Firmware info
404         snprintf(cmd, sizeof(cmd), "%s/fw_version", touch_spi_path);
405         DumpFileToFd(fd, "LSI firmware version", cmd);
406 
407         //Touch status
408         snprintf(cmd, sizeof(cmd), "%s/status", touch_spi_path);
409         DumpFileToFd(fd, "LSI touch status", cmd);
410 
411         //Calibration info
412         snprintf(cmd, sizeof(cmd),
413                  "echo %s > %s/cmd && cat %s/cmd_result",
414                  "get_mis_cal_info",
415                  touch_spi_path, touch_spi_path);
416         RunCommandToFd(fd, "Calibration info", {"/vendor/bin/sh", "-c", cmd});
417 
418         //Mutual strength
419         snprintf(cmd, sizeof(cmd),
420                  "echo %s > %s/cmd && cat %s/cmd_result",
421                  "run_delta_read_all",
422                  touch_spi_path, touch_spi_path);
423         RunCommandToFd(fd, "Mutual Strength", {"/vendor/bin/sh", "-c", cmd});
424 
425         //Self strength
426         snprintf(cmd, sizeof(cmd),
427                  "echo %s > %s/cmd && cat %s/cmd_result",
428                  "run_self_delta_read_all",
429                  touch_spi_path, touch_spi_path);
430         RunCommandToFd(fd, "Self Strength", {"/vendor/bin/sh", "-c", cmd});
431 
432         //Raw cap
433         snprintf(cmd, sizeof(cmd),
434                  "echo %s > %s/cmd && cat %s/cmd_result",
435                  "run_rawcap_read_all",
436                  touch_spi_path, touch_spi_path);
437         RunCommandToFd(fd, "Mutual Raw Cap", {"/vendor/bin/sh", "-c", cmd});
438 
439         //Self raw cap
440         snprintf(cmd, sizeof(cmd),
441                  "echo %s > %s/cmd && cat %s/cmd_result",
442                  "run_self_rawcap_read_all",
443                  touch_spi_path, touch_spi_path);
444         RunCommandToFd(fd, "Self Raw Cap", {"/vendor/bin/sh", "-c", cmd});
445 
446         //TYPE_OFFSET_DATA_SEC
447         snprintf(cmd, sizeof(cmd),
448                  "echo %s > %s/cmd && cat %s/cmd_result",
449                  "run_rawdata_read_type,19",
450                  touch_spi_path, touch_spi_path);
451         RunCommandToFd(fd, "TYPE_OFFSET_DATA_SEC", {"/vendor/bin/sh", "-c", cmd});
452 
453         //TYPE_AMBIENT_DATA
454         snprintf(cmd, sizeof(cmd),
455                  "echo %s > %s/cmd && cat %s/cmd_result",
456                  "run_rawdata_read_type,3",
457                  touch_spi_path, touch_spi_path);
458         RunCommandToFd(fd, "TYPE_AMBIENT_DATA", {"/vendor/bin/sh", "-c", cmd});
459 
460         //TYPE_DECODED_DATA
461         snprintf(cmd, sizeof(cmd),
462                  "echo %s > %s/cmd && cat %s/cmd_result",
463                  "run_rawdata_read_type,5",
464                  touch_spi_path, touch_spi_path);
465         RunCommandToFd(fd, "TYPE_DECODED_DATA", {"/vendor/bin/sh", "-c", cmd});
466 
467         //TYPE_NOI_P2P_MIN
468         snprintf(cmd, sizeof(cmd),
469                  "echo %s > %s/cmd && cat %s/cmd_result",
470                  "run_rawdata_read_type,30",
471                  touch_spi_path, touch_spi_path);
472         RunCommandToFd(fd, "TYPE_NOI_P2P_MIN", {"/vendor/bin/sh", "-c", cmd});
473 
474         //TYPE_NOI_P2P_MAX
475         snprintf(cmd, sizeof(cmd),
476                  "echo %s > %s/cmd && cat %s/cmd_result",
477                  "run_rawdata_read_type,31",
478                  touch_spi_path, touch_spi_path);
479         RunCommandToFd(fd, "TYPE_NOI_P2P_MAX", {"/vendor/bin/sh", "-c", cmd});
480 
481         //Change data format back to default(portrait)
482         snprintf(cmd, sizeof(cmd),
483                  "echo %s > %s/cmd && cat %s/cmd_result",
484                  "set_print_format,0",
485                  touch_spi_path, touch_spi_path);
486         RunCommandToFd(fd, "Print Format", {"/vendor/bin/sh", "-c", cmd});
487 
488 
489         //Disable: force touch active
490         snprintf(cmd, sizeof(cmd),
491                  "echo %s > %s/cmd && cat %s/cmd_result",
492                  "force_touch_active,0",
493                  touch_spi_path, touch_spi_path);
494         RunCommandToFd(fd, "Force Touch Active", {"/vendor/bin/sh", "-c", cmd});
495     }
496 
497 }
498 
DumpDisplay(int fd)499 static void DumpDisplay(int fd) {
500     DumpFileToFd(fd, "PANEL VENDOR NAME", "/sys/class/panel_info/panel0/panel_vendor_name");
501     DumpFileToFd(fd, "PANEL SN", "/sys/class/panel_info/panel0/serial_number");
502     DumpFileToFd(fd, "PANEL EXTRA INFO", "/sys/class/panel_info/panel0/panel_extinfo");
503 }
504 
DumpSensorLog(int fd)505 static void DumpSensorLog(int fd) {
506     const std::string logPath = "/data/vendor/sensors/log/sensor_log.txt";
507     const std::string lastlogPath = "/data/vendor/sensors/log/sensor_lastlog.txt";
508 
509     if (!access(logPath.c_str(), R_OK)) {
510         DumpFileToFd(fd, "sensor log", logPath);
511     }
512     if (!access(lastlogPath.c_str(), R_OK)) {
513         DumpFileToFd(fd, "sensor lastlog", lastlogPath);
514     }
515 }
516 
DumpF2FS(int fd)517 static void DumpF2FS(int fd) {
518     DumpFileToFd(fd, "F2FS", "/sys/kernel/debug/f2fs/status");
519     DumpFileToFd(fd, "F2FS", "/dev/fscklogs/fsck");
520     RunCommandToFd(fd, "F2FS - fsck time (ms)", {"/vendor/bin/sh", "-c", "getprop ro.boottime.init.fsck.data"});
521     RunCommandToFd(fd, "F2FS - checkpoint=disable time (ms)", {"/vendor/bin/sh", "-c", "getprop ro.boottime.init.mount.data"});
522 }
523 
DumpUFS(int fd)524 static void DumpUFS(int fd) {
525     DumpFileToFd(fd, "UFS model", "/sys/block/sda/device/model");
526     DumpFileToFd(fd, "UFS rev", "/sys/block/sda/device/rev");
527     DumpFileToFd(fd, "UFS size", "/sys/block/sda/size");
528 
529     DumpFileToFd(fd, "UFS Slow IO Read", "/dev/sys/block/bootdevice/slowio_read_cnt");
530     DumpFileToFd(fd, "UFS Slow IO Write", "/dev/sys/block/bootdevice/slowio_write_cnt");
531     DumpFileToFd(fd, "UFS Slow IO Unmap", "/dev/sys/block/bootdevice/slowio_unmap_cnt");
532     DumpFileToFd(fd, "UFS Slow IO Sync", "//dev/sys/block/bootdevice/slowio_sync_cnt");
533 
534     RunCommandToFd(fd, "UFS err_stats", {"/vendor/bin/sh", "-c",
535                        "path=\"/dev/sys/block/bootdevice/err_stats\"; "
536                        "for node in `ls $path/err_*`; do "
537                        "printf \"%s:%d\\n\" $(basename $node) $(cat $node); done;"});
538 
539     RunCommandToFd(fd, "UFS io_stats", {"/vendor/bin/sh", "-c",
540                        "path=\"/dev/sys/block/bootdevice/io_stats\"; "
541                        "printf \"\\t\\t%-10s %-10s %-10s %-10s %-10s %-10s\\n\" "
542                        "ReadCnt ReadBytes WriteCnt WriteBytes RWCnt RWBytes; "
543                        "str=$(cat $path/*_start); arr=($str); "
544                        "printf \"Started: \\t%-10s %-10s %-10s %-10s %-10s %-10s\\n\" "
545                        "${arr[1]} ${arr[0]} ${arr[5]} ${arr[4]} ${arr[3]} ${arr[2]}; "
546                        "str=$(cat $path/*_complete); arr=($str); "
547                        "printf \"Completed: \\t%-10s %-10s %-10s %-10s %-10s %-10s\\n\" "
548                        "${arr[1]} ${arr[0]} ${arr[5]} ${arr[4]} ${arr[3]} ${arr[2]}; "
549                        "str=$(cat $path/*_maxdiff); arr=($str); "
550                        "printf \"MaxDiff: \\t%-10s %-10s %-10s %-10s %-10s %-10s\\n\\n\" "
551                        "${arr[1]} ${arr[0]} ${arr[5]} ${arr[4]} ${arr[3]} ${arr[2]}; "});
552 
553     RunCommandToFd(fd, "UFS req_stats", {"/vendor/bin/sh", "-c",
554                        "path=\"/dev/sys/block/bootdevice/req_stats\"; "
555                        "printf \"\\t%-10s %-10s %-10s %-10s %-10s %-10s %-10s\\n\" "
556                        "All Write Read Read\\(urg\\) Write\\(urg\\) Flush Discard; "
557                        "str=$(cat $path/*_min); arr=($str); "
558                        "printf \"Min:\\t%-10s %-10s %-10s %-10s %-10s %-10s %-10s\\n\" "
559                        "${arr[0]} ${arr[3]} ${arr[6]} ${arr[4]} ${arr[5]} ${arr[2]} ${arr[1]}; "
560                        "str=$(cat $path/*_max); arr=($str); "
561                        "printf \"Max:\\t%-10s %-10s %-10s %-10s %-10s %-10s %-10s\\n\" "
562                        "${arr[0]} ${arr[3]} ${arr[6]} ${arr[4]} ${arr[5]} ${arr[2]} ${arr[1]}; "
563                        "str=$(cat $path/*_avg); arr=($str); "
564                        "printf \"Avg.:\\t%-10s %-10s %-10s %-10s %-10s %-10s %-10s\\n\" "
565                        "${arr[0]} ${arr[3]} ${arr[6]} ${arr[4]} ${arr[5]} ${arr[2]} ${arr[1]}; "
566                        "str=$(cat $path/*_sum); arr=($str); "
567                        "printf \"Count:\\t%-10s %-10s %-10s %-10s %-10s %-10s %-10s\\n\\n\" "
568                        "${arr[0]} ${arr[3]} ${arr[6]} ${arr[4]} ${arr[5]} ${arr[2]} ${arr[1]};"});
569 
570     std::string ufs_health = "for f in $(find /dev/sys/block/bootdevice/health_descriptor -type f); do if [[ -r $f && -f $f ]]; then echo --- $f; cat $f; echo ''; fi; done";
571     RunCommandToFd(fd, "UFS health", {"/vendor/bin/sh", "-c", ufs_health.c_str()});
572 }
573 
574 // Methods from ::android::hardware::dumpstate::V1_0::IDumpstateDevice follow.
dumpstateBoard(const hidl_handle & handle)575 Return<void> DumpstateDevice::dumpstateBoard(const hidl_handle& handle) {
576     // Ignore return value, just return an empty status.
577     dumpstateBoard_1_1(handle, DumpstateMode::DEFAULT, 30 * 1000 /* timeoutMillis */);
578     return Void();
579 }
580 
581 // Methods from ::android::hardware::dumpstate::V1_1::IDumpstateDevice follow.
dumpstateBoard_1_1(const hidl_handle & handle,const DumpstateMode mode,const uint64_t timeoutMillis)582 Return<DumpstateStatus> DumpstateDevice::dumpstateBoard_1_1(const hidl_handle& handle,
583                                                             const DumpstateMode mode,
584                                                             const uint64_t timeoutMillis) {
585     // Unused arguments.
586     (void) timeoutMillis;
587 
588     // Exit when dump is completed since this is a lazy HAL.
589     addPostCommandTask([]() {
590         exit(0);
591     });
592 
593     if (handle == nullptr || handle->numFds < 1) {
594         ALOGE("no FDs\n");
595         return DumpstateStatus::ILLEGAL_ARGUMENT;
596     }
597 
598     int fd = handle->data[0];
599     if (fd < 0) {
600         ALOGE("invalid FD: %d\n", handle->data[0]);
601         return DumpstateStatus::ILLEGAL_ARGUMENT;
602     }
603 
604     bool isModeValid = false;
605     for (const auto dumpstateMode : hidl_enum_range<DumpstateMode>()) {
606         if (mode == dumpstateMode) {
607             isModeValid = true;
608             break;
609         }
610     }
611     if (!isModeValid) {
612         ALOGE("Invalid mode: %d\n", mode);
613         return DumpstateStatus::ILLEGAL_ARGUMENT;
614     } else if (mode == DumpstateMode::WEAR) {
615         // We aren't a Wear device.
616         ALOGE("Unsupported mode: %d\n", mode);
617         return DumpstateStatus::UNSUPPORTED_MODE;
618     }
619 
620     RunCommandToFd(fd, "Notify modem", {"/vendor/bin/modem_svc", "-s"}, CommandOptions::WithTimeout(1).Build());
621 
622     pthread_t modemThreadHandle = 0;
623     if (getVerboseLoggingEnabled()) {
624         ALOGD("Verbose logging is enabled.\n");
625         if (handle->numFds < 2) {
626             ALOGE("no FD for modem\n");
627         } else {
628             int fdModem = handle->data[1];
629             if (pthread_create(&modemThreadHandle, NULL, dumpModemThread, (void *)((long)fdModem)) != 0) {
630                 ALOGE("could not create thread for dumpModem\n");
631             }
632         }
633     }
634 
635     RunCommandToFd(fd, "VENDOR PROPERTIES", {"/vendor/bin/getprop"});
636     DumpFileToFd(fd, "SoC serial number", "/sys/devices/soc0/serial_number");
637     DumpFileToFd(fd, "CPU present", "/sys/devices/system/cpu/present");
638     DumpFileToFd(fd, "CPU online", "/sys/devices/system/cpu/online");
639     DumpFileToFd(fd, "Bootloader Log", "/proc/bldrlog");
640     DumpTouch(fd);
641     DumpDisplay(fd);
642 
643     DumpF2FS(fd);
644     DumpUFS(fd);
645 
646     DumpSensorLog(fd);
647 
648     DumpFileToFd(fd, "INTERRUPTS", "/proc/interrupts");
649     DumpFileToFd(fd, "Sleep Stats", "/sys/power/system_sleep/stats");
650     DumpFileToFd(fd, "Power Management Stats", "/sys/power/rpmh_stats/master_stats");
651     DumpFileToFd(fd, "WLAN Power Stats", "/sys/kernel/wlan/power_stats");
652     DumpFileToFd(fd, "LL-Stats", "/d/wlan0/ll_stats");
653     DumpFileToFd(fd, "WLAN Connect Info", "/d/wlan0/connect_info");
654     DumpFileToFd(fd, "WLAN Offload Info", "/d/wlan0/offload_info");
655     DumpFileToFd(fd, "WLAN Roaming Stats", "/d/wlan0/roam_stats");
656     DumpFileToFd(fd, "ICNSS Stats", "/d/icnss/stats");
657     DumpFileToFd(fd, "SMD Log", "/d/ipc_logging/smd/log");
658     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"});
659     DumpFileToFd(fd, "dmabuf info", "/d/dma_buf/bufinfo");
660     DumpFileToFd(fd, "dmabuf process info", "/d/dma_buf/dmaprocs");
661     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"});
662     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"});
663     RunCommandToFd(fd, "Cooling Device Time in State", {"/vendor/bin/sh", "-c", "for f in /sys/class/thermal/cooling* ; do type=`cat $f/type` ; temp=`cat $f/stats/time_in_state_ms` ; echo \"$type:\n$temp\" ; done"});
664     RunCommandToFd(fd, "Cooling Device Trans Table", {"/vendor/bin/sh", "-c", "for f in /sys/class/thermal/cooling* ; do type=`cat $f/type` ; temp=`cat $f/stats/trans_table` ; echo \"$type:\n$temp\" ; done"});
665     RunCommandToFd(
666         fd, "LMH info",
667         {"/vendor/bin/sh", "-c",
668          "for f in /sys/bus/platform/drivers/msm_lmh_dcvs/*qcom,limits-dcvs@*/lmh_freq_limit; do "
669          "state=`cat $f` ; echo \"$f: $state\" ; done"});
670     RunCommandToFd(
671         fd, "CPU MAX FREQ info",
672         {"/vendor/bin/sh", "-c",
673          "for f in /sys/devices/system/cpu/cpufreq/policy*/scaling_max_freq; do "
674          "max_freq=`cat $f` ; echo \"$f: $max_freq\" ; done"});
675     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"});
676     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"});
677     RunCommandToFd(fd, "Airbrush debug info", {"/vendor/bin/sh", "-c", "for f in `ls /sys/devices/platform/soc/c84000.i2c/i2c-4/4-0066/@(*curr|temperature|vbat|total_power)`; do echo \"$f: `cat $f`\" ; done; file=/d/airbrush/airbrush_sm/chip_state; echo \"$file: `cat $file`\""});
678     DumpFileToFd(fd, "TCPM logs", "/d/usb/tcpm-usbpd0");
679     DumpFileToFd(fd, "TCPM logs", "/dev/logbuffer_tcpm");
680     DumpFileToFd(fd, "PD Engine", "/dev/logbuffer_usbpd");
681     DumpFileToFd(fd, "PPS", "/dev/logbuffer_pps");
682     DumpFileToFd(fd, "BMS", "/dev/logbuffer_ssoc");
683     DumpFileToFd(fd, "smblib", "/dev/logbuffer_smblib");
684     DumpFileToFd(fd, "WLC logs", "/dev/logbuffer_wireless");
685     DumpFileToFd(fd, "RTX logs", "/dev/logbuffer_rtx");
686     DumpFileToFd(fd, "TTF", "/dev/logbuffer_ttf");
687     DumpFileToFd(fd, "TTF details", "/sys/class/power_supply/battery/ttf_details");
688     DumpFileToFd(fd, "TTF stats", "/sys/class/power_supply/battery/ttf_stats");
689     DumpFileToFd(fd, "aacr_state", "/sys/class/power_supply/battery/aacr_state");
690     DumpFileToFd(fd, "ipc-local-ports", "/d/msm_ipc_router/dump_local_ports");
691     RunCommandToFd(fd, "TRICKLE-DEFEND Config", {"/vendor/bin/sh", "-c", " cd /sys/devices/platform/soc/soc:google,battery/power_supply/battery/; echo \"bd_trickle_enable: `cat bd_trickle_enable`\"; echo \"bd_trickle_cnt: `cat bd_trickle_cnt`\";  echo \"bd_trickle_recharge_soc: `cat bd_trickle_recharge_soc`\";  echo \"bd_trickle_dry_run: `cat bd_trickle_dry_run`\";  echo \"bd_trickle_reset_sec: `cat bd_trickle_reset_sec`\""});
692     RunCommandToFd(fd, "DWELL-DEFEND Config", {"/vendor/bin/sh", "-c", " cd /sys/devices/platform/soc/soc:google,charger/; for f in `ls charge_s*` ; do echo \"$f: `cat $f`\" ; done"});
693     RunCommandToFd(fd, "TEMP-DEFEND Config", {"/vendor/bin/sh", "-c", " cd /sys/devices/platform/soc/soc:google,charger/; for f in `ls bd_*` ; do echo \"$f: `cat $f`\" ; done"});
694     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"});
695     RunCommandToFd(fd, "Power supply properties", {"/vendor/bin/sh", "-c", "for f in `ls /sys/class/power_supply/*/uevent` ; do echo \"------ $f\\n`cat $f`\\n\" ; done"});
696     RunCommandToFd(fd, "PMIC Votables", {"/vendor/bin/sh", "-c", "cat /sys/kernel/debug/pmic-votable/*/status"});
697 
698     if (!PropertiesHelper::IsUserBuild()) {
699         RunCommandToFd(fd, "Google Charger", {"/vendor/bin/sh", "-c", "cd /d/google_charger/; for f in `ls pps_*` ; do echo \"$f: `cat $f`\" ; done"});
700         RunCommandToFd(fd, "Google Battery", {"/vendor/bin/sh", "-c", "cd /d/google_battery/; for f in `ls ssoc_*` ; do echo \"$f: `cat $f`\" ; done"});
701         DumpFileToFd(fd, "Charging table dump", "/d/google_battery/chg_raw_profile");
702     }
703 
704     RunCommandToFd(fd, "Battery EEPROM", {"/vendor/bin/sh", "-c", "xxd /sys/devices/platform/soc/98c000.i2c/i2c-1/1-0050/1-00500/nvmem"});
705     DumpFileToFd(fd, "WLC VER", "/sys/devices/platform/soc/98c000.i2c/i2c-1/1-003b/version");
706     DumpFileToFd(fd, "WLC STATUS", "/sys/devices/platform/soc/98c000.i2c/i2c-1/1-003b/status");
707 
708     RunCommandToFd(fd, "eSIM Status", {"/vendor/bin/sh", "-c", "od -t x1 /sys/firmware/devicetree/base/chosen/cdt/cdb2/esim"});
709     DumpFileToFd(fd, "Modem Stat", "/data/vendor/modem_stat/debug.txt");
710     DumpFileToFd(fd, "Pixel trace", "/d/tracing/instances/pixel-trace/trace");
711 
712     // Slower dump put later in case stuck the rest of dump
713     // Timeout after 3s as TZ log missing EOF
714     RunCommandToFd(fd, "QSEE logs", {"/vendor/bin/sh", "-c", "/vendor/bin/timeout 3 cat /d/tzdbg/qsee_log"});
715 
716     // Citadel info
717     RunCommandToFd(fd, "Citadel VERSION", {"/vendor/bin/hw/citadel_updater", "-lv"});
718     RunCommandToFd(fd, "Citadel STATS", {"/vendor/bin/hw/citadel_updater", "--stats"});
719     RunCommandToFd(fd, "Citadel BOARDID", {"/vendor/bin/hw/citadel_updater", "--board_id"});
720 
721     // Dump various events in WiFi data path
722     DumpFileToFd(fd, "WLAN DP Trace", "/d/wlan/dpt_stats/dump_set_dpt_logs");
723 
724     // Keep this at the end as very long on not for humans
725     DumpFileToFd(fd, "WLAN FW Log Symbol Table", "/vendor/firmware/Data.msc");
726 
727     // Dump camera profiler log
728     RunCommandToFd(fd, "Camera Profiler Logs", {"/vendor/bin/sh", "-c", "for f in /data/vendor/camera/profiler/camx_*; do echo [$f]; cat \"$f\";done"});
729 
730     // Dump fastrpc dma buffer size
731     DumpFileToFd(fd, "Fastrpc dma buffer", "/sys/kernel/fastrpc/total_dma_kb");
732 
733     // Dump page owner
734     DumpFileToFd(fd, "Page Owner", "/sys/kernel/debug/page_owner");
735     if (modemThreadHandle) {
736         pthread_join(modemThreadHandle, NULL);
737     }
738 
739     return DumpstateStatus::OK;
740 }
741 
setVerboseLoggingEnabled(const bool enable)742 Return<void> DumpstateDevice::setVerboseLoggingEnabled(const bool enable) {
743     android::base::SetProperty(VENDOR_VERBOSE_LOGGING_ENABLED_PROPERTY, enable ? "true" : "false");
744     return Void();
745 }
746 
getVerboseLoggingEnabled()747 Return<bool> DumpstateDevice::getVerboseLoggingEnabled() {
748     return android::base::GetBoolProperty(VENDOR_VERBOSE_LOGGING_ENABLED_PROPERTY, false);
749 }
750 
751 }  // namespace implementation
752 }  // namespace V1_1
753 }  // namespace dumpstate
754 }  // namespace hardware
755 }  // namespace android
756