blob: 0b1fa43fa4649be9b25f3c807e44c3f0b7b529e8 [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
2 * Copyright (C) 2008 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#include <dirent.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <limits.h>
21#include <poll.h>
22#include <signal.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/inotify.h>
28#include <sys/stat.h>
Mark Salyzyn0751efa2016-02-05 15:33:17 -080029#include <sys/sysconf.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070030#include <sys/time.h>
31#include <sys/wait.h>
32#include <sys/klog.h>
33#include <time.h>
34#include <unistd.h>
John Michelaue7b6cf12013-03-07 15:35:35 -060035#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070036
Jeff Brownbf7f4922012-06-07 16:40:01 -070037#include <cutils/debugger.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070038#include <cutils/properties.h>
39#include <cutils/sockets.h>
40#include <private/android_filesystem_config.h>
41
Robert Craig95798372013-04-04 06:33:10 -040042#include <selinux/android.h>
43
Colin Crossf45fa6b2012-03-26 12:38:26 -070044#include "dumpstate.h"
45
Jeff Brown1dc94e32014-09-11 14:15:27 -070046static const int64_t NANOS_PER_SEC = 1000000000;
47
Jeff Brownbf7f4922012-06-07 16:40:01 -070048/* list of native processes to include in the native dumps */
49static const char* native_processes_to_dump[] = {
James Dong1fc4f802012-09-10 16:08:48 -070050 "/system/bin/drmserver",
Jeff Brownbf7f4922012-06-07 16:40:01 -070051 "/system/bin/mediaserver",
52 "/system/bin/sdcard",
53 "/system/bin/surfaceflinger",
54 NULL,
55};
56
Christopher Ferris54bcc5f2015-02-10 12:15:01 -080057static uint64_t nanotime() {
58 struct timespec ts;
59 clock_gettime(CLOCK_MONOTONIC, &ts);
60 return (uint64_t)ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec;
61}
62
John Spurlock5ecd4be2014-01-29 14:14:40 -050063void for_each_userid(void (*func)(int), const char *header) {
Felipe Leme68116162015-11-10 20:10:25 -080064 ON_DRY_RUN_RETURN();
John Spurlock5ecd4be2014-01-29 14:14:40 -050065 DIR *d;
66 struct dirent *de;
67
68 if (header) printf("\n------ %s ------\n", header);
69 func(0);
70
71 if (!(d = opendir("/data/system/users"))) {
72 printf("Failed to open /data/system/users (%s)\n", strerror(errno));
73 return;
74 }
75
76 while ((de = readdir(d))) {
77 int userid;
78 if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
79 continue;
80 }
81 func(userid);
82 }
83
84 closedir(d);
85}
86
Colin Cross0c22e8b2012-11-02 15:46:56 -070087static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070088 DIR *d;
89 struct dirent *de;
90
91 if (!(d = opendir("/proc"))) {
92 printf("Failed to open /proc (%s)\n", strerror(errno));
93 return;
94 }
95
96 printf("\n------ %s ------\n", header);
97 while ((de = readdir(d))) {
98 int pid;
99 int fd;
100 char cmdpath[255];
101 char cmdline[255];
102
103 if (!(pid = atoi(de->d_name))) {
104 continue;
105 }
106
Colin Crossf45fa6b2012-03-26 12:38:26 -0700107 memset(cmdline, 0, sizeof(cmdline));
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800108
109 snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid);
110 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
111 TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700112 close(fd);
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800113 if (cmdline[0]) {
114 helper(pid, cmdline, arg);
115 continue;
116 }
117 }
118
119 // if no cmdline, a kernel thread has comm
120 snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid);
121 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
122 TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4));
123 close(fd);
124 if (cmdline[1]) {
125 cmdline[0] = '[';
126 size_t len = strcspn(cmdline, "\f\b\r\n");
127 cmdline[len] = ']';
128 cmdline[len+1] = '\0';
129 }
130 }
131 if (!cmdline[0]) {
132 strcpy(cmdline, "N/A");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700133 }
Colin Cross0c22e8b2012-11-02 15:46:56 -0700134 helper(pid, cmdline, arg);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700135 }
136
137 closedir(d);
138}
139
Colin Cross0c22e8b2012-11-02 15:46:56 -0700140static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
Felipe Leme515eb0d2015-12-14 15:09:56 -0800141 for_each_pid_func *func = (for_each_pid_func*) arg;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700142 func(pid, cmdline);
143}
144
145void for_each_pid(for_each_pid_func func, const char *header) {
Felipe Leme68116162015-11-10 20:10:25 -0800146 ON_DRY_RUN_RETURN();
Felipe Leme515eb0d2015-12-14 15:09:56 -0800147 __for_each_pid(for_each_pid_helper, header, (void *) func);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700148}
149
150static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
151 DIR *d;
152 struct dirent *de;
153 char taskpath[255];
Felipe Leme515eb0d2015-12-14 15:09:56 -0800154 for_each_tid_func *func = (for_each_tid_func*) arg;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700155
156 sprintf(taskpath, "/proc/%d/task", pid);
157
158 if (!(d = opendir(taskpath))) {
159 printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
160 return;
161 }
162
163 func(pid, pid, cmdline);
164
165 while ((de = readdir(d))) {
166 int tid;
167 int fd;
168 char commpath[255];
169 char comm[255];
170
171 if (!(tid = atoi(de->d_name))) {
172 continue;
173 }
174
175 if (tid == pid)
176 continue;
177
178 sprintf(commpath,"/proc/%d/comm", tid);
Colin Cross1493a392012-11-07 11:25:31 -0800179 memset(comm, 0, sizeof(comm));
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700180 if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700181 strcpy(comm, "N/A");
182 } else {
183 char *c;
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800184 TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2));
Colin Cross0c22e8b2012-11-02 15:46:56 -0700185 close(fd);
186
187 c = strrchr(comm, '\n');
188 if (c) {
189 *c = '\0';
190 }
191 }
192 func(pid, tid, comm);
193 }
194
195 closedir(d);
196}
197
198void for_each_tid(for_each_tid_func func, const char *header) {
Felipe Leme68116162015-11-10 20:10:25 -0800199 ON_DRY_RUN_RETURN();
Felipe Leme515eb0d2015-12-14 15:09:56 -0800200 __for_each_pid(for_each_tid_helper, header, (void *) func);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700201}
202
203void show_wchan(int pid, int tid, const char *name) {
Felipe Leme68116162015-11-10 20:10:25 -0800204 ON_DRY_RUN_RETURN();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700205 char path[255];
206 char buffer[255];
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800207 int fd, ret, save_errno;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700208 char name_buffer[255];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700209
210 memset(buffer, 0, sizeof(buffer));
211
Colin Cross0c22e8b2012-11-02 15:46:56 -0700212 sprintf(path, "/proc/%d/wchan", tid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700213 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700214 printf("Failed to open '%s' (%s)\n", path, strerror(errno));
215 return;
216 }
217
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800218 ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
219 save_errno = errno;
220 close(fd);
221
222 if (ret < 0) {
223 printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
224 return;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700225 }
226
Colin Cross0c22e8b2012-11-02 15:46:56 -0700227 snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
228 pid == tid ? 0 : 3, "", name);
229
230 printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700231
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800232 return;
233}
234
235// print time in centiseconds
236static void snprcent(char *buffer, size_t len, size_t spc,
237 unsigned long long time) {
238 static long hz; // cache discovered hz
239
240 if (hz <= 0) {
241 hz = sysconf(_SC_CLK_TCK);
242 if (hz <= 0) {
243 hz = 1000;
244 }
245 }
246
247 // convert to centiseconds
248 time = (time * 100 + (hz / 2)) / hz;
249
250 char str[16];
251
252 snprintf(str, sizeof(str), " %llu.%02u",
253 time / 100, (unsigned)(time % 100));
254 size_t offset = strlen(buffer);
255 snprintf(buffer + offset, (len > offset) ? len - offset : 0,
256 "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
257}
258
259// print permille as a percent
260static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) {
261 char str[16];
262
263 snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10);
264 size_t offset = strlen(buffer);
265 snprintf(buffer + offset, (len > offset) ? len - offset : 0,
266 "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
267}
268
269void show_showtime(int pid, const char *name) {
270 ON_DRY_RUN_RETURN();
271 char path[255];
272 char buffer[1023];
273 int fd, ret, save_errno;
274
275 memset(buffer, 0, sizeof(buffer));
276
277 sprintf(path, "/proc/%d/stat", pid);
278 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
279 printf("Failed to open '%s' (%s)\n", path, strerror(errno));
280 return;
281 }
282
283 ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
284 save_errno = errno;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700285 close(fd);
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800286
287 if (ret < 0) {
288 printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
289 return;
290 }
291
292 // field 14 is utime
293 // field 15 is stime
294 // field 42 is iotime
295 unsigned long long utime = 0, stime = 0, iotime = 0;
296 if (sscanf(buffer,
297 "%*llu %*s %*s %*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld "
298 "%*lld %*lld %llu %llu %*lld %*lld %*lld %*lld %*lld %*lld "
299 "%*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld "
300 "%*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld %*lld %llu ",
301 &utime, &stime, &iotime) != 3) {
302 return;
303 }
304
305 unsigned long long total = utime + stime;
306 if (!total) {
307 return;
308 }
309
310 unsigned permille = (iotime * 1000 + (total / 2)) / total;
311 if (permille > 1000) {
312 permille = 1000;
313 }
314
315 // try to beautify and stabilize columns at <80 characters
316 snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name);
317 if ((name[0] != '[') || utime) {
318 snprcent(buffer, sizeof(buffer), 57, utime);
319 }
320 snprcent(buffer, sizeof(buffer), 65, stime);
321 if ((name[0] != '[') || iotime) {
322 snprcent(buffer, sizeof(buffer), 73, iotime);
323 }
324 if (iotime) {
325 snprdec(buffer, sizeof(buffer), 79, permille);
326 }
327 puts(buffer); // adds a trailing newline
328
Colin Crossf45fa6b2012-03-26 12:38:26 -0700329 return;
330}
331
332void do_dmesg() {
333 printf("------ KERNEL LOG (dmesg) ------\n");
Felipe Leme68116162015-11-10 20:10:25 -0800334 ON_DRY_RUN_RETURN();
Elliott Hughes5f87b312012-09-17 11:43:40 -0700335 /* Get size of kernel buffer */
336 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700337 if (size <= 0) {
338 printf("Unexpected klogctl return value: %d\n\n", size);
339 return;
340 }
341 char *buf = (char *) malloc(size + 1);
342 if (buf == NULL) {
343 printf("memory allocation failed\n\n");
344 return;
345 }
346 int retval = klogctl(KLOG_READ_ALL, buf, size);
347 if (retval < 0) {
348 printf("klogctl failure\n\n");
349 free(buf);
350 return;
351 }
352 buf[retval] = '\0';
353 printf("%s\n\n", buf);
354 free(buf);
355 return;
356}
357
358void do_showmap(int pid, const char *name) {
359 char title[255];
360 char arg[255];
361
362 sprintf(title, "SHOW MAP %d (%s)", pid, name);
363 sprintf(arg, "%d", pid);
364 run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL);
365}
366
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800367static int _dump_file_from_fd(const char *title, const char *path, int fd) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700368 if (title) printf("------ %s (%s", title, path);
369
370 if (title) {
371 struct stat st;
372 if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
373 char stamp[80];
374 time_t mtime = st.st_mtime;
375 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
376 printf(": %s", stamp);
377 }
378 printf(") ------\n");
379 }
380
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800381 bool newline = false;
382 fd_set read_set;
383 struct timeval tm;
384 while (1) {
385 FD_ZERO(&read_set);
386 FD_SET(fd, &read_set);
387 /* Timeout if no data is read for 30 seconds. */
388 tm.tv_sec = 30;
389 tm.tv_usec = 0;
390 uint64_t elapsed = nanotime();
391 int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm));
392 if (ret == -1) {
393 printf("*** %s: select failed: %s\n", path, strerror(errno));
394 newline = true;
395 break;
396 } else if (ret == 0) {
397 elapsed = nanotime() - elapsed;
398 printf("*** %s: Timed out after %.3fs\n", path,
399 (float) elapsed / NANOS_PER_SEC);
400 newline = true;
401 break;
402 } else {
403 char buffer[65536];
404 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
405 if (bytes_read > 0) {
406 fwrite(buffer, bytes_read, 1, stdout);
407 newline = (buffer[bytes_read-1] == '\n');
408 } else {
409 if (bytes_read == -1) {
410 printf("*** %s: Failed to read from fd: %s", path, strerror(errno));
411 newline = true;
412 }
413 break;
414 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700415 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700416 }
Elliott Hughes997abb62015-05-15 17:05:40 -0700417 close(fd);
Christopher Ferris7dc7f322014-07-22 16:08:19 -0700418
Colin Crossf45fa6b2012-03-26 12:38:26 -0700419 if (!newline) printf("\n");
420 if (title) printf("\n");
421 return 0;
422}
423
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800424/* prints the contents of a file */
425int dump_file(const char *title, const char *path) {
Felipe Leme68116162015-11-10 20:10:25 -0800426 if (title) printf("------ %s (%s) ------\n", title, path);
427 ON_DRY_RUN_RETURN(0);
428
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800429 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
430 if (fd < 0) {
431 int err = errno;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800432 printf("*** %s: %s\n", path, strerror(err));
433 if (title) printf("\n");
434 return -1;
435 }
436 return _dump_file_from_fd(title, path, fd);
437}
438
Mark Salyzyn326842f2015-04-30 09:49:41 -0700439/* calls skip to gate calling dump_from_fd recursively
440 * in the specified directory. dump_from_fd defaults to
441 * dump_file_from_fd above when set to NULL. skip defaults
442 * to false when set to NULL. dump_from_fd will always be
443 * called with title NULL.
444 */
445int dump_files(const char *title, const char *dir,
446 bool (*skip)(const char *path),
447 int (*dump_from_fd)(const char *title, const char *path, int fd)) {
448 DIR *dirp;
449 struct dirent *d;
450 char *newpath = NULL;
Felipe Leme515eb0d2015-12-14 15:09:56 -0800451 const char *slash = "/";
Mark Salyzyn326842f2015-04-30 09:49:41 -0700452 int fd, retval = 0;
453
454 if (title) {
455 printf("------ %s (%s) ------\n", title, dir);
456 }
Felipe Leme68116162015-11-10 20:10:25 -0800457 ON_DRY_RUN_RETURN(0);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700458
459 if (dir[strlen(dir) - 1] == '/') {
460 ++slash;
461 }
462 dirp = opendir(dir);
463 if (dirp == NULL) {
464 retval = -errno;
465 fprintf(stderr, "%s: %s\n", dir, strerror(errno));
466 return retval;
467 }
468
469 if (!dump_from_fd) {
470 dump_from_fd = dump_file_from_fd;
471 }
472 for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) {
473 if ((d->d_name[0] == '.')
474 && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
475 || (d->d_name[1] == '\0'))) {
476 continue;
477 }
478 asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
479 (d->d_type == DT_DIR) ? "/" : "");
480 if (!newpath) {
481 retval = -errno;
482 continue;
483 }
484 if (skip && (*skip)(newpath)) {
485 continue;
486 }
487 if (d->d_type == DT_DIR) {
488 int ret = dump_files(NULL, newpath, skip, dump_from_fd);
489 if (ret < 0) {
490 retval = ret;
491 }
492 continue;
493 }
494 fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
495 if (fd < 0) {
496 retval = fd;
497 printf("*** %s: %s\n", newpath, strerror(errno));
498 continue;
499 }
500 (*dump_from_fd)(NULL, newpath, fd);
501 }
502 closedir(dirp);
503 if (title) {
504 printf("\n");
505 }
506 return retval;
507}
508
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800509/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
510 * it's possible to avoid issues where opening the file itself can get
511 * stuck.
512 */
513int dump_file_from_fd(const char *title, const char *path, int fd) {
Felipe Leme68116162015-11-10 20:10:25 -0800514 ON_DRY_RUN_RETURN(0);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800515 int flags = fcntl(fd, F_GETFL);
516 if (flags == -1) {
517 printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
518 return -1;
519 } else if (!(flags & O_NONBLOCK)) {
520 printf("*** %s: fd must have O_NONBLOCK set.\n", path);
521 return -1;
522 }
523 return _dump_file_from_fd(title, path, fd);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700524}
525
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800526bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) {
527 sigset_t child_mask, old_mask;
528 sigemptyset(&child_mask);
529 sigaddset(&child_mask, SIGCHLD);
530
531 if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) {
532 printf("*** sigprocmask failed: %s\n", strerror(errno));
533 return false;
534 }
535
536 struct timespec ts;
537 ts.tv_sec = timeout_seconds;
538 ts.tv_nsec = 0;
539 int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts));
540 int saved_errno = errno;
541 // Set the signals back the way they were.
542 if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) {
543 printf("*** sigprocmask failed: %s\n", strerror(errno));
544 if (ret == 0) {
545 return false;
546 }
547 }
548 if (ret == -1) {
549 errno = saved_errno;
550 if (errno == EAGAIN) {
551 errno = ETIMEDOUT;
552 } else {
553 printf("*** sigtimedwait failed: %s\n", strerror(errno));
554 }
555 return false;
556 }
557
558 pid_t child_pid = waitpid(pid, status, WNOHANG);
559 if (child_pid != pid) {
560 if (child_pid != -1) {
561 printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid);
562 } else {
563 printf("*** waitpid failed: %s\n", strerror(errno));
564 }
565 return false;
566 }
567 return true;
568}
569
Colin Crossf45fa6b2012-03-26 12:38:26 -0700570/* forks a command and waits for it to finish */
571int run_command(const char *title, int timeout_seconds, const char *command, ...) {
572 fflush(stdout);
Felipe Leme68116162015-11-10 20:10:25 -0800573
574 const char *args[1024] = {command};
575 size_t arg;
576 va_list ap;
577 va_start(ap, command);
578 if (title) printf("------ %s (%s", title, command);
579 for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
580 args[arg] = va_arg(ap, const char *);
581 if (args[arg] == NULL) break;
582 if (title) printf(" %s", args[arg]);
583 }
584 if (title) printf(") ------\n");
585 fflush(stdout);
586
587 ON_DRY_RUN_RETURN(0);
588
589 return run_command_always(title, timeout_seconds, args);
590}
591
592/* forks a command and waits for it to finish */
593int run_command_always(const char *title, int timeout_seconds, const char *args[]) {
594 const char *command = args[0];
595
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800596 uint64_t start = nanotime();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700597 pid_t pid = fork();
598
599 /* handle error case */
600 if (pid < 0) {
601 printf("*** fork: %s\n", strerror(errno));
602 return pid;
603 }
604
605 /* handle child case */
606 if (pid == 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700607
John Michelaue7b6cf12013-03-07 15:35:35 -0600608 /* make sure the child dies when dumpstate dies */
609 prctl(PR_SET_PDEATHSIG, SIGKILL);
610
Andres Morales2e671bb2014-08-21 12:38:22 -0700611 /* just ignore SIGPIPE, will go down with parent's */
612 struct sigaction sigact;
613 memset(&sigact, 0, sizeof(sigact));
614 sigact.sa_handler = SIG_IGN;
615 sigaction(SIGPIPE, &sigact, NULL);
616
Colin Crossf45fa6b2012-03-26 12:38:26 -0700617 execvp(command, (char**) args);
618 printf("*** exec(%s): %s\n", command, strerror(errno));
619 fflush(stdout);
620 _exit(-1);
621 }
622
623 /* handle parent case */
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800624 int status;
625 bool ret = waitpid_with_timeout(pid, timeout_seconds, &status);
626 uint64_t elapsed = nanotime() - start;
627 if (!ret) {
628 if (errno == ETIMEDOUT) {
629 printf("*** %s: Timed out after %.3fs (killing pid %d)\n", command,
630 (float) elapsed / NANOS_PER_SEC, pid);
631 } else {
632 printf("*** %s: Error after %.4fs (killing pid %d)\n", command,
633 (float) elapsed / NANOS_PER_SEC, pid);
634 }
635 kill(pid, SIGTERM);
636 if (!waitpid_with_timeout(pid, 5, NULL)) {
637 kill(pid, SIGKILL);
638 if (!waitpid_with_timeout(pid, 5, NULL)) {
639 printf("*** %s: Cannot kill %d even with SIGKILL.\n", command, pid);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700640 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700641 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800642 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700643 }
Christopher Ferris1a9a3382015-01-30 11:00:52 -0800644
645 if (WIFSIGNALED(status)) {
646 printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
647 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
648 printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
649 }
650 if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC);
651
652 return status;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700653}
654
655size_t num_props = 0;
656static char* props[2000];
657
658static void print_prop(const char *key, const char *name, void *user) {
659 (void) user;
660 if (num_props < sizeof(props) / sizeof(props[0])) {
661 char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
662 snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
663 props[num_props++] = strdup(buf);
664 }
665}
666
667static int compare_prop(const void *a, const void *b) {
668 return strcmp(*(char * const *) a, *(char * const *) b);
669}
670
671/* prints all the system properties */
672void print_properties() {
Felipe Leme68116162015-11-10 20:10:25 -0800673 printf("------ SYSTEM PROPERTIES ------\n");
674 ON_DRY_RUN_RETURN();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700675 size_t i;
676 num_props = 0;
677 property_list(print_prop, NULL);
678 qsort(&props, num_props, sizeof(props[0]), compare_prop);
679
Colin Crossf45fa6b2012-03-26 12:38:26 -0700680 for (i = 0; i < num_props; ++i) {
681 fputs(props[i], stdout);
682 free(props[i]);
683 }
684 printf("\n");
685}
686
687/* redirect output to a service control socket */
688void redirect_to_socket(FILE *redirect, const char *service) {
689 int s = android_get_control_socket(service);
690 if (s < 0) {
691 fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
692 exit(1);
693 }
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700694 fcntl(s, F_SETFD, FD_CLOEXEC);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700695 if (listen(s, 4) < 0) {
696 fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
697 exit(1);
698 }
699
700 struct sockaddr addr;
701 socklen_t alen = sizeof(addr);
702 int fd = accept(s, &addr, &alen);
703 if (fd < 0) {
704 fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
705 exit(1);
706 }
707
708 fflush(redirect);
709 dup2(fd, fileno(redirect));
710 close(fd);
711}
712
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800713/* redirect output to a file */
714void redirect_to_file(FILE *redirect, char *path) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700715 char *chp = path;
716
717 /* skip initial slash */
718 if (chp[0] == '/')
719 chp++;
720
721 /* create leading directories, if necessary */
722 while (chp && chp[0]) {
723 chp = strchr(chp, '/');
724 if (chp) {
725 *chp = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700726 mkdir(path, 0770); /* drwxrwx--- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700727 *chp++ = '/';
728 }
729 }
730
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700731 int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800732 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700733 if (fd < 0) {
734 fprintf(stderr, "%s: %s\n", path, strerror(errno));
735 exit(1);
736 }
737
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800738 TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700739 close(fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700740}
741
Jeff Brownbf7f4922012-06-07 16:40:01 -0700742static bool should_dump_native_traces(const char* path) {
743 for (const char** p = native_processes_to_dump; *p; p++) {
744 if (!strcmp(*p, path)) {
745 return true;
746 }
747 }
748 return false;
749}
750
751/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
752const char *dump_traces() {
Felipe Leme68116162015-11-10 20:10:25 -0800753 ON_DRY_RUN_RETURN(NULL);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700754 const char* result = NULL;
755
Colin Crossf45fa6b2012-03-26 12:38:26 -0700756 char traces_path[PROPERTY_VALUE_MAX] = "";
757 property_get("dalvik.vm.stack-trace-file", traces_path, "");
758 if (!traces_path[0]) return NULL;
759
760 /* move the old traces.txt (if any) out of the way temporarily */
761 char anr_traces_path[PATH_MAX];
762 strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
763 strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
764 if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
765 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
766 return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
767 }
768
Colin Crossf45fa6b2012-03-26 12:38:26 -0700769 /* create a new, empty traces.txt file to receive stack dumps */
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700770 int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC,
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800771 0666)); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700772 if (fd < 0) {
773 fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
774 return NULL;
775 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700776 int chmod_ret = fchmod(fd, 0666);
777 if (chmod_ret < 0) {
778 fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
779 close(fd);
780 return NULL;
781 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700782
Felipe Leme515eb0d2015-12-14 15:09:56 -0800783 /* Variables below must be initialized before 'goto' statements */
784 int dalvik_found = 0;
785 int ifd, wfd = -1;
786
Colin Crossf45fa6b2012-03-26 12:38:26 -0700787 /* walk /proc and kill -QUIT all Dalvik processes */
788 DIR *proc = opendir("/proc");
789 if (proc == NULL) {
790 fprintf(stderr, "/proc: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700791 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700792 }
793
794 /* use inotify to find when processes are done dumping */
Felipe Leme515eb0d2015-12-14 15:09:56 -0800795 ifd = inotify_init();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700796 if (ifd < 0) {
797 fprintf(stderr, "inotify_init: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700798 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700799 }
800
Felipe Leme515eb0d2015-12-14 15:09:56 -0800801 wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700802 if (wfd < 0) {
803 fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700804 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700805 }
806
807 struct dirent *d;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700808 while ((d = readdir(proc))) {
809 int pid = atoi(d->d_name);
810 if (pid <= 0) continue;
811
Jeff Brownbf7f4922012-06-07 16:40:01 -0700812 char path[PATH_MAX];
813 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700814 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700815 ssize_t len = readlink(path, data, sizeof(data) - 1);
816 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700817 continue;
818 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700819 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700820
Colin Cross0d6180f2014-07-16 19:00:46 -0700821 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700822 /* skip zygote -- it won't dump its stack anyway */
823 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700824 int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700825 len = read(cfd, data, sizeof(data) - 1);
826 close(cfd);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700827 if (len <= 0) {
828 continue;
829 }
830 data[len] = '\0';
Colin Cross0d6180f2014-07-16 19:00:46 -0700831 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700832 continue;
833 }
834
835 ++dalvik_found;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800836 uint64_t start = nanotime();
Jeff Brownbf7f4922012-06-07 16:40:01 -0700837 if (kill(pid, SIGQUIT)) {
838 fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
839 continue;
840 }
841
842 /* wait for the writable-close notification from inotify */
843 struct pollfd pfd = { ifd, POLLIN, 0 };
Nick Vaccaro85453ec2014-04-30 11:19:23 -0700844 int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */
Jeff Brownbf7f4922012-06-07 16:40:01 -0700845 if (ret < 0) {
846 fprintf(stderr, "poll: %s\n", strerror(errno));
847 } else if (ret == 0) {
848 fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
849 } else {
850 struct inotify_event ie;
851 read(ifd, &ie, sizeof(ie));
852 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700853
854 if (lseek(fd, 0, SEEK_END) < 0) {
855 fprintf(stderr, "lseek: %s\n", strerror(errno));
856 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800857 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700858 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700859 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700860 } else if (should_dump_native_traces(data)) {
861 /* dump native process if appropriate */
862 if (lseek(fd, 0, SEEK_END) < 0) {
863 fprintf(stderr, "lseek: %s\n", strerror(errno));
864 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800865 static uint16_t timeout_failures = 0;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800866 uint64_t start = nanotime();
Christopher Ferris31ef8552015-01-14 13:23:30 -0800867
868 /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */
869 if (timeout_failures == 3) {
870 dprintf(fd, "too many stack dump failures, skipping...\n");
871 } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) {
872 dprintf(fd, "dumping failed, likely due to a timeout\n");
873 timeout_failures++;
874 } else {
875 timeout_failures = 0;
876 }
877 dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n",
Jeff Brown1dc94e32014-09-11 14:15:27 -0700878 pid, (float)(nanotime() - start) / NANOS_PER_SEC);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700879 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700880 }
881 }
882
Colin Crossf45fa6b2012-03-26 12:38:26 -0700883 if (dalvik_found == 0) {
884 fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
885 }
886
887 static char dump_traces_path[PATH_MAX];
888 strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
889 strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
890 if (rename(traces_path, dump_traces_path)) {
891 fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700892 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700893 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700894 result = dump_traces_path;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700895
896 /* replace the saved [ANR] traces.txt file */
897 rename(anr_traces_path, traces_path);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700898
899error_close_ifd:
900 close(ifd);
901error_close_fd:
902 close(fd);
903 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700904}
905
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700906void dump_route_tables() {
Felipe Leme68116162015-11-10 20:10:25 -0800907 ON_DRY_RUN_RETURN();
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700908 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
909 dump_file("RT_TABLES", RT_TABLES_PATH);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700910 FILE* fp = fopen(RT_TABLES_PATH, "re");
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700911 if (!fp) {
912 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
913 return;
914 }
915 char table[16];
916 // Each line has an integer (the table number), a space, and a string (the table name). We only
917 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
918 // Add a fixed max limit so this doesn't go awry.
919 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
920 run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
921 run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
922 }
923 fclose(fp);
924}
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800925
926void dump_emmc_ecsd(const char *ext_csd_path) {
927 static const size_t EXT_CSD_REV = 192;
928 static const size_t EXT_PRE_EOL_INFO = 267;
929 static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268;
930 static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269;
931 struct hex {
932 char str[2];
933 } buffer[512];
934 int fd, ext_csd_rev, ext_pre_eol_info;
935 ssize_t bytes_read;
936 static const char *ver_str[] = {
937 "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0"
938 };
939 static const char *eol_str[] = {
940 "Undefined",
941 "Normal",
942 "Warning (consumed 80% of reserve)",
Mark Salyzyn4b45d672015-12-11 10:41:52 -0800943 "Urgent (consumed 90% of reserve)"
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800944 };
945
946 printf("------ %s Extended CSD ------\n", ext_csd_path);
947
948 fd = TEMP_FAILURE_RETRY(open(ext_csd_path,
949 O_RDONLY | O_NONBLOCK | O_CLOEXEC));
950 if (fd < 0) {
951 printf("*** %s: %s\n\n", ext_csd_path, strerror(errno));
952 return;
953 }
954
955 bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
956 close(fd);
957 if (bytes_read < 0) {
958 printf("*** %s: %s\n\n", ext_csd_path, strerror(errno));
959 return;
960 }
Mark Salyzyn4b45d672015-12-11 10:41:52 -0800961 if (bytes_read < (ssize_t)(EXT_CSD_REV * sizeof(struct hex))) {
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800962 printf("*** %s: truncated content %zd\n\n", ext_csd_path, bytes_read);
963 return;
964 }
965
966 ext_csd_rev = 0;
967 if (sscanf(buffer[EXT_CSD_REV].str, "%02x", &ext_csd_rev) != 1) {
968 printf("*** %s: EXT_CSD_REV parse error \"%.2s\"\n\n",
969 ext_csd_path, buffer[EXT_CSD_REV].str);
970 return;
971 }
972
973 printf("rev 1.%d (MMC %s)\n",
974 ext_csd_rev,
975 (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ?
976 ver_str[ext_csd_rev] :
977 "Unknown");
978 if (ext_csd_rev < 7) {
979 printf("\n");
980 return;
981 }
982
Mark Salyzyn4b45d672015-12-11 10:41:52 -0800983 if (bytes_read < (ssize_t)(EXT_PRE_EOL_INFO * sizeof(struct hex))) {
Mark Salyzyn8c8130e2015-12-09 11:21:28 -0800984 printf("*** %s: truncated content %zd\n\n", ext_csd_path, bytes_read);
985 return;
986 }
987
988 ext_pre_eol_info = 0;
989 if (sscanf(buffer[EXT_PRE_EOL_INFO].str, "%02x", &ext_pre_eol_info) != 1) {
990 printf("*** %s: PRE_EOL_INFO parse error \"%.2s\"\n\n",
991 ext_csd_path, buffer[EXT_PRE_EOL_INFO].str);
992 return;
993 }
994 printf("PRE_EOL_INFO %d (MMC %s)\n",
995 ext_pre_eol_info,
996 eol_str[(ext_pre_eol_info < (int)
997 (sizeof(eol_str) / sizeof(eol_str[0]))) ?
998 ext_pre_eol_info : 0]);
999
1000 for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A;
1001 lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B;
1002 ++lifetime) {
1003 int ext_device_life_time_est;
1004 static const char *est_str[] = {
1005 "Undefined",
1006 "0-10% of device lifetime used",
1007 "10-20% of device lifetime used",
1008 "20-30% of device lifetime used",
1009 "30-40% of device lifetime used",
1010 "40-50% of device lifetime used",
1011 "50-60% of device lifetime used",
1012 "60-70% of device lifetime used",
1013 "70-80% of device lifetime used",
1014 "80-90% of device lifetime used",
1015 "90-100% of device lifetime used",
1016 "Exceeded the maximum estimated device lifetime",
1017 };
1018
Mark Salyzyn4b45d672015-12-11 10:41:52 -08001019 if (bytes_read < (ssize_t)(lifetime * sizeof(struct hex))) {
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001020 printf("*** %s: truncated content %zd\n", ext_csd_path, bytes_read);
1021 break;
1022 }
1023
1024 ext_device_life_time_est = 0;
1025 if (sscanf(buffer[lifetime].str, "%02x", &ext_device_life_time_est) != 1) {
1026 printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%.2s\"\n",
1027 ext_csd_path,
1028 (unsigned)(lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) + 'A',
1029 buffer[lifetime].str);
1030 continue;
1031 }
1032 printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n",
1033 (unsigned)(lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) + 'A',
1034 ext_device_life_time_est,
1035 est_str[(ext_device_life_time_est < (int)
1036 (sizeof(est_str) / sizeof(est_str[0]))) ?
1037 ext_device_life_time_est : 0]);
1038 }
1039
1040 printf("\n");
1041}