Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 26 | #include <string> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 27 | #include <string.h> |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 29 | #include <sys/inotify.h> |
| 30 | #include <sys/stat.h> |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 31 | #include <sys/sysconf.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 32 | #include <sys/time.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <sys/klog.h> |
| 35 | #include <time.h> |
| 36 | #include <unistd.h> |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 37 | #include <vector> |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 38 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 39 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 40 | #define LOG_TAG "dumpstate" |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 41 | #include <cutils/debugger.h> |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 42 | #include <cutils/log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 43 | #include <cutils/properties.h> |
| 44 | #include <cutils/sockets.h> |
| 45 | #include <private/android_filesystem_config.h> |
| 46 | |
Robert Craig | 9579837 | 2013-04-04 06:33:10 -0400 | [diff] [blame] | 47 | #include <selinux/android.h> |
| 48 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 49 | #include "dumpstate.h" |
| 50 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 51 | static const int64_t NANOS_PER_SEC = 1000000000; |
| 52 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 53 | /* list of native processes to include in the native dumps */ |
| 54 | static const char* native_processes_to_dump[] = { |
Andy Hung | 9609bbd | 2015-12-15 12:42:50 -0800 | [diff] [blame] | 55 | "/system/bin/audioserver", |
Chien-Yu Chen | f5248da | 2016-01-28 14:23:03 -0800 | [diff] [blame] | 56 | "/system/bin/cameraserver", |
James Dong | 1fc4f80 | 2012-09-10 16:08:48 -0700 | [diff] [blame] | 57 | "/system/bin/drmserver", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 58 | "/system/bin/mediaserver", |
| 59 | "/system/bin/sdcard", |
| 60 | "/system/bin/surfaceflinger", |
keunyoung | d907b32 | 2015-10-16 15:21:43 -0700 | [diff] [blame] | 61 | "/system/bin/vehicle_network_service", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 62 | NULL, |
| 63 | }; |
| 64 | |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 65 | DurationReporter::DurationReporter(const char *title) : DurationReporter(title, stdout) {} |
| 66 | |
| 67 | DurationReporter::DurationReporter(const char *title, FILE *out) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 68 | title_ = title; |
| 69 | if (title) { |
| 70 | started_ = DurationReporter::nanotime(); |
| 71 | } |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 72 | out_ = out; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | DurationReporter::~DurationReporter() { |
| 76 | if (title_) { |
| 77 | uint64_t elapsed = DurationReporter::nanotime() - started_; |
| 78 | // Use "Yoda grammar" to make it easier to grep|sort sections. |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 79 | if (out_) { |
| 80 | fprintf(out_, "------ %.3fs was the duration of '%s' ------\n", |
| 81 | (float) elapsed / NANOS_PER_SEC, title_); |
| 82 | } else { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 83 | MYLOGD("Duration of '%s': %.3fs\n", title_, (float) elapsed / NANOS_PER_SEC); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 84 | } |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | uint64_t DurationReporter::DurationReporter::nanotime() { |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 89 | struct timespec ts; |
| 90 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 91 | return (uint64_t) ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 92 | } |
| 93 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 94 | void for_each_userid(void (*func)(int), const char *header) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 95 | ON_DRY_RUN_RETURN(); |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 96 | DIR *d; |
| 97 | struct dirent *de; |
| 98 | |
| 99 | if (header) printf("\n------ %s ------\n", header); |
| 100 | func(0); |
| 101 | |
| 102 | if (!(d = opendir("/data/system/users"))) { |
| 103 | printf("Failed to open /data/system/users (%s)\n", strerror(errno)); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | while ((de = readdir(d))) { |
| 108 | int userid; |
| 109 | if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) { |
| 110 | continue; |
| 111 | } |
| 112 | func(userid); |
| 113 | } |
| 114 | |
| 115 | closedir(d); |
| 116 | } |
| 117 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 118 | static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 119 | DIR *d; |
| 120 | struct dirent *de; |
| 121 | |
| 122 | if (!(d = opendir("/proc"))) { |
| 123 | printf("Failed to open /proc (%s)\n", strerror(errno)); |
| 124 | return; |
| 125 | } |
| 126 | |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 127 | if (header) printf("\n------ %s ------\n", header); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 128 | while ((de = readdir(d))) { |
| 129 | int pid; |
| 130 | int fd; |
| 131 | char cmdpath[255]; |
| 132 | char cmdline[255]; |
| 133 | |
| 134 | if (!(pid = atoi(de->d_name))) { |
| 135 | continue; |
| 136 | } |
| 137 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 138 | memset(cmdline, 0, sizeof(cmdline)); |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 139 | |
| 140 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid); |
| 141 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 142 | TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 143 | close(fd); |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 144 | if (cmdline[0]) { |
| 145 | helper(pid, cmdline, arg); |
| 146 | continue; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // if no cmdline, a kernel thread has comm |
| 151 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid); |
| 152 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 153 | TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4)); |
| 154 | close(fd); |
| 155 | if (cmdline[1]) { |
| 156 | cmdline[0] = '['; |
| 157 | size_t len = strcspn(cmdline, "\f\b\r\n"); |
| 158 | cmdline[len] = ']'; |
| 159 | cmdline[len+1] = '\0'; |
| 160 | } |
| 161 | } |
| 162 | if (!cmdline[0]) { |
| 163 | strcpy(cmdline, "N/A"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 164 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 165 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | closedir(d); |
| 169 | } |
| 170 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 171 | static void for_each_pid_helper(int pid, const char *cmdline, void *arg) { |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 172 | for_each_pid_func *func = (for_each_pid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 173 | func(pid, cmdline); |
| 174 | } |
| 175 | |
| 176 | void for_each_pid(for_each_pid_func func, const char *header) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 177 | ON_DRY_RUN_RETURN(); |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 178 | __for_each_pid(for_each_pid_helper, header, (void *)func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 182 | DIR *d; |
| 183 | struct dirent *de; |
| 184 | char taskpath[255]; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 185 | for_each_tid_func *func = (for_each_tid_func *) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 186 | |
| 187 | sprintf(taskpath, "/proc/%d/task", pid); |
| 188 | |
| 189 | if (!(d = opendir(taskpath))) { |
| 190 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | func(pid, pid, cmdline); |
| 195 | |
| 196 | while ((de = readdir(d))) { |
| 197 | int tid; |
| 198 | int fd; |
| 199 | char commpath[255]; |
| 200 | char comm[255]; |
| 201 | |
| 202 | if (!(tid = atoi(de->d_name))) { |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | if (tid == pid) |
| 207 | continue; |
| 208 | |
| 209 | sprintf(commpath,"/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 210 | memset(comm, 0, sizeof(comm)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 211 | if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 212 | strcpy(comm, "N/A"); |
| 213 | } else { |
| 214 | char *c; |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 215 | TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 216 | close(fd); |
| 217 | |
| 218 | c = strrchr(comm, '\n'); |
| 219 | if (c) { |
| 220 | *c = '\0'; |
| 221 | } |
| 222 | } |
| 223 | func(pid, tid, comm); |
| 224 | } |
| 225 | |
| 226 | closedir(d); |
| 227 | } |
| 228 | |
| 229 | void for_each_tid(for_each_tid_func func, const char *header) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 230 | ON_DRY_RUN_RETURN(); |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 231 | __for_each_pid(for_each_tid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void show_wchan(int pid, int tid, const char *name) { |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 235 | ON_DRY_RUN_RETURN(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 236 | char path[255]; |
| 237 | char buffer[255]; |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 238 | int fd, ret, save_errno; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 239 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 240 | |
| 241 | memset(buffer, 0, sizeof(buffer)); |
| 242 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 243 | sprintf(path, "/proc/%d/wchan", tid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 244 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 245 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 246 | return; |
| 247 | } |
| 248 | |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 249 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 250 | save_errno = errno; |
| 251 | close(fd); |
| 252 | |
| 253 | if (ret < 0) { |
| 254 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 255 | return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 258 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 259 | pid == tid ? 0 : 3, "", name); |
| 260 | |
| 261 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 262 | |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
| 266 | // print time in centiseconds |
| 267 | static void snprcent(char *buffer, size_t len, size_t spc, |
| 268 | unsigned long long time) { |
| 269 | static long hz; // cache discovered hz |
| 270 | |
| 271 | if (hz <= 0) { |
| 272 | hz = sysconf(_SC_CLK_TCK); |
| 273 | if (hz <= 0) { |
| 274 | hz = 1000; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // convert to centiseconds |
| 279 | time = (time * 100 + (hz / 2)) / hz; |
| 280 | |
| 281 | char str[16]; |
| 282 | |
| 283 | snprintf(str, sizeof(str), " %llu.%02u", |
| 284 | time / 100, (unsigned)(time % 100)); |
| 285 | size_t offset = strlen(buffer); |
| 286 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 287 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 288 | } |
| 289 | |
| 290 | // print permille as a percent |
| 291 | static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) { |
| 292 | char str[16]; |
| 293 | |
| 294 | snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10); |
| 295 | size_t offset = strlen(buffer); |
| 296 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 297 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 298 | } |
| 299 | |
| 300 | void show_showtime(int pid, const char *name) { |
| 301 | ON_DRY_RUN_RETURN(); |
| 302 | char path[255]; |
| 303 | char buffer[1023]; |
| 304 | int fd, ret, save_errno; |
| 305 | |
| 306 | memset(buffer, 0, sizeof(buffer)); |
| 307 | |
| 308 | sprintf(path, "/proc/%d/stat", pid); |
| 309 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
| 310 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 315 | save_errno = errno; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 316 | close(fd); |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 317 | |
| 318 | if (ret < 0) { |
| 319 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | // field 14 is utime |
| 324 | // field 15 is stime |
| 325 | // field 42 is iotime |
| 326 | unsigned long long utime = 0, stime = 0, iotime = 0; |
| 327 | if (sscanf(buffer, |
Xia Yang | 60292e5 | 2016-02-16 03:05:18 -0800 | [diff] [blame] | 328 | "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d " |
| 329 | "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d " |
| 330 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " |
| 331 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ", |
Mark Salyzyn | a297c32 | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 332 | &utime, &stime, &iotime) != 3) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | unsigned long long total = utime + stime; |
| 337 | if (!total) { |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | unsigned permille = (iotime * 1000 + (total / 2)) / total; |
| 342 | if (permille > 1000) { |
| 343 | permille = 1000; |
| 344 | } |
| 345 | |
| 346 | // try to beautify and stabilize columns at <80 characters |
| 347 | snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name); |
| 348 | if ((name[0] != '[') || utime) { |
| 349 | snprcent(buffer, sizeof(buffer), 57, utime); |
| 350 | } |
| 351 | snprcent(buffer, sizeof(buffer), 65, stime); |
| 352 | if ((name[0] != '[') || iotime) { |
| 353 | snprcent(buffer, sizeof(buffer), 73, iotime); |
| 354 | } |
| 355 | if (iotime) { |
| 356 | snprdec(buffer, sizeof(buffer), 79, permille); |
| 357 | } |
| 358 | puts(buffer); // adds a trailing newline |
| 359 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 360 | return; |
| 361 | } |
| 362 | |
| 363 | void do_dmesg() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 364 | const char *title = "KERNEL LOG (dmesg)"; |
| 365 | DurationReporter duration_reporter(title); |
| 366 | printf("------ %s ------\n", title); |
| 367 | |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 368 | ON_DRY_RUN_RETURN(); |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 369 | /* Get size of kernel buffer */ |
| 370 | int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 371 | if (size <= 0) { |
| 372 | printf("Unexpected klogctl return value: %d\n\n", size); |
| 373 | return; |
| 374 | } |
| 375 | char *buf = (char *) malloc(size + 1); |
| 376 | if (buf == NULL) { |
| 377 | printf("memory allocation failed\n\n"); |
| 378 | return; |
| 379 | } |
| 380 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 381 | if (retval < 0) { |
| 382 | printf("klogctl failure\n\n"); |
| 383 | free(buf); |
| 384 | return; |
| 385 | } |
| 386 | buf[retval] = '\0'; |
| 387 | printf("%s\n\n", buf); |
| 388 | free(buf); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | void do_showmap(int pid, const char *name) { |
| 393 | char title[255]; |
| 394 | char arg[255]; |
| 395 | |
| 396 | sprintf(title, "SHOW MAP %d (%s)", pid, name); |
| 397 | sprintf(arg, "%d", pid); |
Felipe Leme | 3dba69a | 2016-03-17 14:59:13 -0700 | [diff] [blame] | 398 | run_command(title, 10, SU_PATH, "root", "showmap", "-q", arg, NULL); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 401 | static int _dump_file_from_fd(const char *title, const char *path, int fd) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 402 | if (title) { |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 403 | printf("------ %s (%s", title, path); |
| 404 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 405 | struct stat st; |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 406 | // Only show the modification time of non-device files. |
| 407 | size_t path_len = strlen(path); |
| 408 | if ((path_len < 6 || memcmp(path, "/proc/", 6)) && |
| 409 | (path_len < 5 || memcmp(path, "/sys/", 5)) && |
| 410 | (path_len < 3 || memcmp(path, "/d/", 3)) && |
| 411 | !fstat(fd, &st)) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 412 | char stamp[80]; |
| 413 | time_t mtime = st.st_mtime; |
| 414 | strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); |
| 415 | printf(": %s", stamp); |
| 416 | } |
| 417 | printf(") ------\n"); |
| 418 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 419 | ON_DRY_RUN({ update_progress(WEIGHT_FILE); close(fd); return 0; }); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 420 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 421 | bool newline = false; |
| 422 | fd_set read_set; |
| 423 | struct timeval tm; |
| 424 | while (1) { |
| 425 | FD_ZERO(&read_set); |
| 426 | FD_SET(fd, &read_set); |
| 427 | /* Timeout if no data is read for 30 seconds. */ |
| 428 | tm.tv_sec = 30; |
| 429 | tm.tv_usec = 0; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 430 | uint64_t elapsed = DurationReporter::nanotime(); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 431 | int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm)); |
| 432 | if (ret == -1) { |
| 433 | printf("*** %s: select failed: %s\n", path, strerror(errno)); |
| 434 | newline = true; |
| 435 | break; |
| 436 | } else if (ret == 0) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 437 | elapsed = DurationReporter::nanotime() - elapsed; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 438 | printf("*** %s: Timed out after %.3fs\n", path, |
| 439 | (float) elapsed / NANOS_PER_SEC); |
| 440 | newline = true; |
| 441 | break; |
| 442 | } else { |
| 443 | char buffer[65536]; |
| 444 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 445 | if (bytes_read > 0) { |
| 446 | fwrite(buffer, bytes_read, 1, stdout); |
| 447 | newline = (buffer[bytes_read-1] == '\n'); |
| 448 | } else { |
| 449 | if (bytes_read == -1) { |
| 450 | printf("*** %s: Failed to read from fd: %s", path, strerror(errno)); |
| 451 | newline = true; |
| 452 | } |
| 453 | break; |
| 454 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 455 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 456 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 457 | update_progress(WEIGHT_FILE); |
Elliott Hughes | 997abb6 | 2015-05-15 17:05:40 -0700 | [diff] [blame] | 458 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 459 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 460 | if (!newline) printf("\n"); |
| 461 | if (title) printf("\n"); |
| 462 | return 0; |
| 463 | } |
| 464 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 465 | /* prints the contents of a file */ |
| 466 | int dump_file(const char *title, const char *path) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 467 | DurationReporter duration_reporter(title); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 468 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 469 | if (fd < 0) { |
| 470 | int err = errno; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 471 | printf("*** %s: %s\n", path, strerror(err)); |
| 472 | if (title) printf("\n"); |
| 473 | return -1; |
| 474 | } |
| 475 | return _dump_file_from_fd(title, path, fd); |
| 476 | } |
| 477 | |
Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 478 | int read_file_as_long(const char *path, long int *output) { |
| 479 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 480 | if (fd < 0) { |
| 481 | int err = errno; |
| 482 | MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err)); |
| 483 | return -1; |
| 484 | } |
| 485 | char buffer[50]; |
| 486 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 487 | if (bytes_read == -1) { |
| 488 | MYLOGE("Error reading file %s: %s\n", path, strerror(errno)); |
| 489 | return -2; |
| 490 | } |
| 491 | if (bytes_read == 0) { |
| 492 | MYLOGE("File %s is empty\n", path); |
| 493 | return -3; |
| 494 | } |
| 495 | *output = atoi(buffer); |
| 496 | return 0; |
| 497 | } |
| 498 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 499 | /* calls skip to gate calling dump_from_fd recursively |
| 500 | * in the specified directory. dump_from_fd defaults to |
| 501 | * dump_file_from_fd above when set to NULL. skip defaults |
| 502 | * to false when set to NULL. dump_from_fd will always be |
| 503 | * called with title NULL. |
| 504 | */ |
| 505 | int dump_files(const char *title, const char *dir, |
| 506 | bool (*skip)(const char *path), |
| 507 | int (*dump_from_fd)(const char *title, const char *path, int fd)) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 508 | DurationReporter duration_reporter(title); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 509 | DIR *dirp; |
| 510 | struct dirent *d; |
| 511 | char *newpath = NULL; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 512 | const char *slash = "/"; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 513 | int fd, retval = 0; |
| 514 | |
| 515 | if (title) { |
| 516 | printf("------ %s (%s) ------\n", title, dir); |
| 517 | } |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 518 | ON_DRY_RUN_RETURN(0); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 519 | |
| 520 | if (dir[strlen(dir) - 1] == '/') { |
| 521 | ++slash; |
| 522 | } |
| 523 | dirp = opendir(dir); |
| 524 | if (dirp == NULL) { |
| 525 | retval = -errno; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 526 | MYLOGE("%s: %s\n", dir, strerror(errno)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 527 | return retval; |
| 528 | } |
| 529 | |
| 530 | if (!dump_from_fd) { |
| 531 | dump_from_fd = dump_file_from_fd; |
| 532 | } |
| 533 | for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) { |
| 534 | if ((d->d_name[0] == '.') |
| 535 | && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) |
| 536 | || (d->d_name[1] == '\0'))) { |
| 537 | continue; |
| 538 | } |
| 539 | asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, |
| 540 | (d->d_type == DT_DIR) ? "/" : ""); |
| 541 | if (!newpath) { |
| 542 | retval = -errno; |
| 543 | continue; |
| 544 | } |
| 545 | if (skip && (*skip)(newpath)) { |
| 546 | continue; |
| 547 | } |
| 548 | if (d->d_type == DT_DIR) { |
| 549 | int ret = dump_files(NULL, newpath, skip, dump_from_fd); |
| 550 | if (ret < 0) { |
| 551 | retval = ret; |
| 552 | } |
| 553 | continue; |
| 554 | } |
| 555 | fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 556 | if (fd < 0) { |
| 557 | retval = fd; |
| 558 | printf("*** %s: %s\n", newpath, strerror(errno)); |
| 559 | continue; |
| 560 | } |
| 561 | (*dump_from_fd)(NULL, newpath, fd); |
| 562 | } |
| 563 | closedir(dirp); |
| 564 | if (title) { |
| 565 | printf("\n"); |
| 566 | } |
| 567 | return retval; |
| 568 | } |
| 569 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 570 | /* fd must have been opened with the flag O_NONBLOCK. With this flag set, |
| 571 | * it's possible to avoid issues where opening the file itself can get |
| 572 | * stuck. |
| 573 | */ |
| 574 | int dump_file_from_fd(const char *title, const char *path, int fd) { |
| 575 | int flags = fcntl(fd, F_GETFL); |
| 576 | if (flags == -1) { |
| 577 | printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno)); |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 578 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 579 | return -1; |
| 580 | } else if (!(flags & O_NONBLOCK)) { |
| 581 | printf("*** %s: fd must have O_NONBLOCK set.\n", path); |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 582 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 583 | return -1; |
| 584 | } |
| 585 | return _dump_file_from_fd(title, path, fd); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 588 | bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) { |
| 589 | sigset_t child_mask, old_mask; |
| 590 | sigemptyset(&child_mask); |
| 591 | sigaddset(&child_mask, SIGCHLD); |
| 592 | |
| 593 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { |
| 594 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | struct timespec ts; |
| 599 | ts.tv_sec = timeout_seconds; |
| 600 | ts.tv_nsec = 0; |
| 601 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts)); |
| 602 | int saved_errno = errno; |
| 603 | // Set the signals back the way they were. |
| 604 | if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) { |
| 605 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 606 | if (ret == 0) { |
| 607 | return false; |
| 608 | } |
| 609 | } |
| 610 | if (ret == -1) { |
| 611 | errno = saved_errno; |
| 612 | if (errno == EAGAIN) { |
| 613 | errno = ETIMEDOUT; |
| 614 | } else { |
| 615 | printf("*** sigtimedwait failed: %s\n", strerror(errno)); |
| 616 | } |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | pid_t child_pid = waitpid(pid, status, WNOHANG); |
| 621 | if (child_pid != pid) { |
| 622 | if (child_pid != -1) { |
| 623 | printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid); |
| 624 | } else { |
| 625 | printf("*** waitpid failed: %s\n", strerror(errno)); |
| 626 | } |
| 627 | return false; |
| 628 | } |
| 629 | return true; |
| 630 | } |
| 631 | |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 632 | // TODO: refactor all those commands that convert args |
| 633 | void format_args(const char* command, const char *args[], std::string *string); |
| 634 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 635 | int run_command(const char *title, int timeout_seconds, const char *command, ...) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 636 | DurationReporter duration_reporter(title); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 637 | fflush(stdout); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 638 | |
| 639 | const char *args[1024] = {command}; |
| 640 | size_t arg; |
| 641 | va_list ap; |
| 642 | va_start(ap, command); |
| 643 | if (title) printf("------ %s (%s", title, command); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 644 | bool null_terminated = false; |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 645 | for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { |
| 646 | args[arg] = va_arg(ap, const char *); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 647 | if (args[arg] == nullptr) { |
| 648 | null_terminated = true; |
| 649 | break; |
| 650 | } |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 651 | if (title) printf(" %s", args[arg]); |
| 652 | } |
| 653 | if (title) printf(") ------\n"); |
| 654 | fflush(stdout); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 655 | if (!null_terminated) { |
| 656 | // Fail now, otherwise execvp() call on run_command_always() might hang. |
| 657 | std::string cmd; |
| 658 | format_args(command, args, &cmd); |
| 659 | MYLOGE("skipping command %s because its args were not NULL-terminated", cmd.c_str()); |
| 660 | return -1; |
| 661 | } |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 662 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 663 | ON_DRY_RUN({ update_progress(timeout_seconds); va_end(ap); return 0; }); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 664 | |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 665 | int status = run_command_always(title, false, timeout_seconds, args); |
| 666 | va_end(ap); |
| 667 | return status; |
| 668 | } |
| 669 | |
| 670 | int run_command_as_shell(const char *title, int timeout_seconds, const char *command, ...) { |
| 671 | DurationReporter duration_reporter(title); |
| 672 | fflush(stdout); |
| 673 | |
| 674 | const char *args[1024] = {command}; |
| 675 | size_t arg; |
| 676 | va_list ap; |
| 677 | va_start(ap, command); |
| 678 | if (title) printf("------ %s (%s", title, command); |
| 679 | bool null_terminated = false; |
| 680 | for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { |
| 681 | args[arg] = va_arg(ap, const char *); |
| 682 | if (args[arg] == nullptr) { |
| 683 | null_terminated = true; |
| 684 | break; |
| 685 | } |
| 686 | if (title) printf(" %s", args[arg]); |
| 687 | } |
| 688 | if (title) printf(") ------\n"); |
| 689 | fflush(stdout); |
| 690 | if (!null_terminated) { |
| 691 | // Fail now, otherwise execvp() call on run_command_always() might hang. |
| 692 | std::string cmd; |
| 693 | format_args(command, args, &cmd); |
| 694 | MYLOGE("skipping command %s because its args were not NULL-terminated", cmd.c_str()); |
| 695 | return -1; |
| 696 | } |
| 697 | |
| 698 | ON_DRY_RUN({ update_progress(timeout_seconds); va_end(ap); return 0; }); |
| 699 | |
| 700 | int status = run_command_always(title, true, timeout_seconds, args); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 701 | va_end(ap); |
| 702 | return status; |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /* forks a command and waits for it to finish */ |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 706 | int run_command_always(const char *title, bool drop_root, int timeout_seconds, const char *args[]) { |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 707 | /* TODO: for now we're simplifying the progress calculation by using the timeout as the weight. |
| 708 | * It's a good approximation for most cases, except when calling dumpsys, where its weight |
| 709 | * should be much higher proportionally to its timeout. */ |
| 710 | int weight = timeout_seconds; |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 711 | |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 712 | const char *command = args[0]; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 713 | uint64_t start = DurationReporter::nanotime(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 714 | pid_t pid = fork(); |
| 715 | |
| 716 | /* handle error case */ |
| 717 | if (pid < 0) { |
| 718 | printf("*** fork: %s\n", strerror(errno)); |
| 719 | return pid; |
| 720 | } |
| 721 | |
| 722 | /* handle child case */ |
| 723 | if (pid == 0) { |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 724 | if (drop_root && !drop_root_user()) { |
| 725 | printf("*** could not drop root before running %s: %s\n", command, strerror(errno)); |
| 726 | _exit(-1); |
| 727 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 728 | |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 729 | /* make sure the child dies when dumpstate dies */ |
| 730 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 731 | |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame] | 732 | /* just ignore SIGPIPE, will go down with parent's */ |
| 733 | struct sigaction sigact; |
| 734 | memset(&sigact, 0, sizeof(sigact)); |
| 735 | sigact.sa_handler = SIG_IGN; |
| 736 | sigaction(SIGPIPE, &sigact, NULL); |
| 737 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 738 | execvp(command, (char**) args); |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 739 | // execvp's result will be handled after waitpid_with_timeout() below... |
Felipe Leme | ec72578 | 2016-03-23 11:47:00 -0700 | [diff] [blame^] | 740 | MYLOGD("execvp on command %s returned control (error: %s)", command, strerror(errno)); |
| 741 | fflush(stdout); |
Felipe Leme | b0133cd | 2016-03-23 09:50:11 -0700 | [diff] [blame] | 742 | _exit(-1); // ...but it doesn't hurt to force exit, just in case |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /* handle parent case */ |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 746 | int status; |
| 747 | bool ret = waitpid_with_timeout(pid, timeout_seconds, &status); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 748 | uint64_t elapsed = DurationReporter::nanotime() - start; |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 749 | std::string cmd; // used to log command and its args |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 750 | if (!ret) { |
| 751 | if (errno == ETIMEDOUT) { |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 752 | format_args(command, args, &cmd); |
| 753 | printf("*** command '%s' timed out after %.3fs (killing pid %d)\n", cmd.c_str(), |
| 754 | (float) elapsed / NANOS_PER_SEC, pid); |
| 755 | MYLOGE("command '%s' timed out after %.3fs (killing pid %d)\n", cmd.c_str(), |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 756 | (float) elapsed / NANOS_PER_SEC, pid); |
| 757 | } else { |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 758 | format_args(command, args, &cmd); |
| 759 | printf("*** command '%s': Error after %.4fs (killing pid %d)\n", cmd.c_str(), |
| 760 | (float) elapsed / NANOS_PER_SEC, pid); |
| 761 | MYLOGE("command '%s': Error after %.4fs (killing pid %d)\n", cmd.c_str(), |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 762 | (float) elapsed / NANOS_PER_SEC, pid); |
| 763 | } |
| 764 | kill(pid, SIGTERM); |
| 765 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
| 766 | kill(pid, SIGKILL); |
| 767 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 768 | printf("couldn not kill command '%s' (pid %d) even with SIGKILL.\n", command, pid); |
| 769 | MYLOGE("couldn not kill command '%s' (pid %d) even with SIGKILL.\n", command, pid); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 770 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 771 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 772 | return -1; |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 773 | } else if (status) { |
| 774 | format_args(command, args, &cmd); |
| 775 | printf("*** command '%s' failed: %s\n", cmd.c_str(), strerror(errno)); |
| 776 | MYLOGE("command '%s' failed: %s\n", cmd.c_str(), strerror(errno)); |
| 777 | return -2; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 778 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 779 | |
| 780 | if (WIFSIGNALED(status)) { |
| 781 | printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); |
| 782 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
| 783 | printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); |
| 784 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 785 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 786 | if (weight > 0) { |
| 787 | update_progress(weight); |
| 788 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 789 | return status; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 792 | bool drop_root_user() { |
| 793 | if (getgid() == AID_SHELL && getuid() == AID_SHELL) { |
| 794 | MYLOGD("drop_root_user(): already running as Shell"); |
| 795 | return true; |
| 796 | } |
| 797 | /* ensure we will keep capabilities when we drop root */ |
| 798 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 799 | MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); |
| 800 | return false; |
| 801 | } |
| 802 | |
| 803 | gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, |
| 804 | AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC }; |
| 805 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 806 | MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); |
| 807 | return false; |
| 808 | } |
| 809 | if (setgid(AID_SHELL) != 0) { |
| 810 | MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno)); |
| 811 | return false; |
| 812 | } |
| 813 | if (setuid(AID_SHELL) != 0) { |
| 814 | MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno)); |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | struct __user_cap_header_struct capheader; |
| 819 | struct __user_cap_data_struct capdata[2]; |
| 820 | memset(&capheader, 0, sizeof(capheader)); |
| 821 | memset(&capdata, 0, sizeof(capdata)); |
| 822 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 823 | capheader.pid = 0; |
| 824 | |
| 825 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); |
| 826 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG); |
| 827 | capdata[0].inheritable = 0; |
| 828 | capdata[1].inheritable = 0; |
| 829 | |
| 830 | if (capset(&capheader, &capdata[0]) < 0) { |
| 831 | MYLOGE("capset failed: %s\n", strerror(errno)); |
| 832 | return false; |
| 833 | } |
| 834 | |
| 835 | return true; |
| 836 | } |
| 837 | |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 838 | void send_broadcast(const std::string& action, const std::vector<std::string>& args) { |
| 839 | if (args.size() > 1000) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 840 | MYLOGE("send_broadcast: too many arguments (%d)\n", (int) args.size()); |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 841 | return; |
| 842 | } |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 843 | const char *am_args[1024] = { "/system/bin/am", "broadcast", "--user", "0", "-a", |
| 844 | action.c_str() }; |
| 845 | size_t am_index = 5; // Starts at the index of last initial value above. |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 846 | for (const std::string& arg : args) { |
| 847 | am_args[++am_index] = arg.c_str(); |
| 848 | } |
| 849 | // Always terminate with NULL. |
| 850 | am_args[am_index + 1] = NULL; |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 851 | std::string args_string; |
| 852 | format_args(am_index + 1, am_args, &args_string); |
| 853 | MYLOGD("send_broadcast command: %s\n", args_string.c_str()); |
Felipe Leme | b0133cd | 2016-03-23 09:50:11 -0700 | [diff] [blame] | 854 | run_command_always(NULL, 20, true, am_args); |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 857 | size_t num_props = 0; |
| 858 | static char* props[2000]; |
| 859 | |
| 860 | static void print_prop(const char *key, const char *name, void *user) { |
| 861 | (void) user; |
| 862 | if (num_props < sizeof(props) / sizeof(props[0])) { |
| 863 | char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; |
| 864 | snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); |
| 865 | props[num_props++] = strdup(buf); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | static int compare_prop(const void *a, const void *b) { |
| 870 | return strcmp(*(char * const *) a, *(char * const *) b); |
| 871 | } |
| 872 | |
| 873 | /* prints all the system properties */ |
| 874 | void print_properties() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 875 | const char* title = "SYSTEM PROPERTIES"; |
| 876 | DurationReporter duration_reporter(title); |
| 877 | printf("------ %s ------\n", title); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 878 | ON_DRY_RUN_RETURN(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 879 | size_t i; |
| 880 | num_props = 0; |
| 881 | property_list(print_prop, NULL); |
| 882 | qsort(&props, num_props, sizeof(props[0]), compare_prop); |
| 883 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 884 | for (i = 0; i < num_props; ++i) { |
| 885 | fputs(props[i], stdout); |
| 886 | free(props[i]); |
| 887 | } |
| 888 | printf("\n"); |
| 889 | } |
| 890 | |
| 891 | /* redirect output to a service control socket */ |
| 892 | void redirect_to_socket(FILE *redirect, const char *service) { |
| 893 | int s = android_get_control_socket(service); |
| 894 | if (s < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 895 | MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 896 | exit(1); |
| 897 | } |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 898 | fcntl(s, F_SETFD, FD_CLOEXEC); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 899 | if (listen(s, 4) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 900 | MYLOGE("listen(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 901 | exit(1); |
| 902 | } |
| 903 | |
| 904 | struct sockaddr addr; |
| 905 | socklen_t alen = sizeof(addr); |
| 906 | int fd = accept(s, &addr, &alen); |
| 907 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 908 | MYLOGE("accept(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 909 | exit(1); |
| 910 | } |
| 911 | |
| 912 | fflush(redirect); |
| 913 | dup2(fd, fileno(redirect)); |
| 914 | close(fd); |
| 915 | } |
| 916 | |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 917 | void create_parent_dirs(const char *path) { |
Srinath Sridharan | fdf52d3 | 2016-02-01 15:50:22 -0800 | [diff] [blame] | 918 | char *chp = const_cast<char *> (path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 919 | |
| 920 | /* skip initial slash */ |
| 921 | if (chp[0] == '/') |
| 922 | chp++; |
| 923 | |
| 924 | /* create leading directories, if necessary */ |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 925 | struct stat dir_stat; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 926 | while (chp && chp[0]) { |
| 927 | chp = strchr(chp, '/'); |
| 928 | if (chp) { |
| 929 | *chp = 0; |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 930 | if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 931 | MYLOGI("Creating directory %s\n", path); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 932 | if (mkdir(path, 0770)) { /* drwxrwx--- */ |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 933 | MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 934 | } else if (chown(path, AID_SHELL, AID_SHELL)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 935 | MYLOGE("Unable to change ownership of dir %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 936 | } |
| 937 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 938 | *chp++ = '/'; |
| 939 | } |
| 940 | } |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /* redirect output to a file */ |
| 944 | void redirect_to_file(FILE *redirect, char *path) { |
| 945 | create_parent_dirs(path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 946 | |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 947 | int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 948 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 949 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 950 | MYLOGE("%s: %s\n", path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 951 | exit(1); |
| 952 | } |
| 953 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 954 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 955 | close(fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 958 | static bool should_dump_native_traces(const char* path) { |
| 959 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 960 | if (!strcmp(*p, path)) { |
| 961 | return true; |
| 962 | } |
| 963 | } |
| 964 | return false; |
| 965 | } |
| 966 | |
| 967 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 968 | const char *dump_traces() { |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 969 | DurationReporter duration_reporter("DUMP TRACES", NULL); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 970 | ON_DRY_RUN_RETURN(NULL); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 971 | const char* result = NULL; |
| 972 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 973 | char traces_path[PROPERTY_VALUE_MAX] = ""; |
| 974 | property_get("dalvik.vm.stack-trace-file", traces_path, ""); |
| 975 | if (!traces_path[0]) return NULL; |
| 976 | |
| 977 | /* move the old traces.txt (if any) out of the way temporarily */ |
| 978 | char anr_traces_path[PATH_MAX]; |
| 979 | strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path)); |
| 980 | strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path)); |
| 981 | if (rename(traces_path, anr_traces_path) && errno != ENOENT) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 982 | MYLOGE("rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 983 | return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead |
| 984 | } |
| 985 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 986 | /* create a new, empty traces.txt file to receive stack dumps */ |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 987 | int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 988 | 0666)); /* -rw-rw-rw- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 989 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 990 | MYLOGE("%s: %s\n", traces_path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 991 | return NULL; |
| 992 | } |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 993 | int chmod_ret = fchmod(fd, 0666); |
| 994 | if (chmod_ret < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 995 | MYLOGE("fchmod on %s failed: %s\n", traces_path, strerror(errno)); |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 996 | close(fd); |
| 997 | return NULL; |
| 998 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 999 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1000 | /* Variables below must be initialized before 'goto' statements */ |
| 1001 | int dalvik_found = 0; |
| 1002 | int ifd, wfd = -1; |
| 1003 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1004 | /* walk /proc and kill -QUIT all Dalvik processes */ |
| 1005 | DIR *proc = opendir("/proc"); |
| 1006 | if (proc == NULL) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1007 | MYLOGE("/proc: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1008 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /* use inotify to find when processes are done dumping */ |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1012 | ifd = inotify_init(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1013 | if (ifd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1014 | MYLOGE("inotify_init: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1015 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1018 | wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1019 | if (wfd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1020 | MYLOGE("inotify_add_watch(%s): %s\n", traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1021 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | struct dirent *d; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1025 | while ((d = readdir(proc))) { |
| 1026 | int pid = atoi(d->d_name); |
| 1027 | if (pid <= 0) continue; |
| 1028 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1029 | char path[PATH_MAX]; |
| 1030 | char data[PATH_MAX]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1031 | snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1032 | ssize_t len = readlink(path, data, sizeof(data) - 1); |
| 1033 | if (len <= 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1034 | continue; |
| 1035 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1036 | data[len] = '\0'; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1037 | |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 1038 | if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1039 | /* skip zygote -- it won't dump its stack anyway */ |
| 1040 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1041 | int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1042 | len = read(cfd, data, sizeof(data) - 1); |
| 1043 | close(cfd); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1044 | if (len <= 0) { |
| 1045 | continue; |
| 1046 | } |
| 1047 | data[len] = '\0'; |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 1048 | if (!strncmp(data, "zygote", strlen("zygote"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1049 | continue; |
| 1050 | } |
| 1051 | |
| 1052 | ++dalvik_found; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1053 | uint64_t start = DurationReporter::nanotime(); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1054 | if (kill(pid, SIGQUIT)) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1055 | MYLOGE("kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1056 | continue; |
| 1057 | } |
| 1058 | |
| 1059 | /* wait for the writable-close notification from inotify */ |
| 1060 | struct pollfd pfd = { ifd, POLLIN, 0 }; |
Nick Vaccaro | 85453ec | 2014-04-30 11:19:23 -0700 | [diff] [blame] | 1061 | int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */ |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1062 | if (ret < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1063 | MYLOGE("poll: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1064 | } else if (ret == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1065 | MYLOGE("warning: timed out dumping pid %d\n", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1066 | } else { |
| 1067 | struct inotify_event ie; |
| 1068 | read(ifd, &ie, sizeof(ie)); |
| 1069 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1070 | |
| 1071 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1072 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1073 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1074 | dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1075 | pid, (float)(DurationReporter::nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1076 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1077 | } else if (should_dump_native_traces(data)) { |
| 1078 | /* dump native process if appropriate */ |
| 1079 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1080 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1081 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1082 | static uint16_t timeout_failures = 0; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1083 | uint64_t start = DurationReporter::nanotime(); |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1084 | |
| 1085 | /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */ |
| 1086 | if (timeout_failures == 3) { |
| 1087 | dprintf(fd, "too many stack dump failures, skipping...\n"); |
| 1088 | } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) { |
| 1089 | dprintf(fd, "dumping failed, likely due to a timeout\n"); |
| 1090 | timeout_failures++; |
| 1091 | } else { |
| 1092 | timeout_failures = 0; |
| 1093 | } |
| 1094 | dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1095 | pid, (float)(DurationReporter::nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1096 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1100 | if (dalvik_found == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1101 | MYLOGE("Warning: no Dalvik processes found to dump stacks\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | static char dump_traces_path[PATH_MAX]; |
| 1105 | strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path)); |
| 1106 | strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path)); |
| 1107 | if (rename(traces_path, dump_traces_path)) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1108 | MYLOGE("rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1109 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1110 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1111 | result = dump_traces_path; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1112 | |
| 1113 | /* replace the saved [ANR] traces.txt file */ |
| 1114 | rename(anr_traces_path, traces_path); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1115 | |
| 1116 | error_close_ifd: |
| 1117 | close(ifd); |
| 1118 | error_close_fd: |
| 1119 | close(fd); |
| 1120 | return result; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1123 | void dump_route_tables() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1124 | DurationReporter duration_reporter("DUMP ROUTE TABLES"); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 1125 | ON_DRY_RUN_RETURN(); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1126 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
| 1127 | dump_file("RT_TABLES", RT_TABLES_PATH); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1128 | FILE* fp = fopen(RT_TABLES_PATH, "re"); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1129 | if (!fp) { |
| 1130 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
| 1131 | return; |
| 1132 | } |
| 1133 | char table[16]; |
| 1134 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 1135 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 1136 | // Add a fixed max limit so this doesn't go awry. |
| 1137 | for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) { |
| 1138 | run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL); |
| 1139 | run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL); |
| 1140 | } |
| 1141 | fclose(fp); |
| 1142 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1143 | |
| 1144 | /* overall progress */ |
| 1145 | int progress = 0; |
Felipe Leme | 08b5578 | 2015-12-01 08:40:52 -0800 | [diff] [blame] | 1146 | int do_update_progress = 0; // Set by dumpstate.cpp |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1147 | int weight_total = WEIGHT_TOTAL; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1148 | |
| 1149 | // TODO: make this function thread safe if sections are generated in parallel. |
| 1150 | void update_progress(int delta) { |
| 1151 | if (!do_update_progress) return; |
| 1152 | |
| 1153 | progress += delta; |
| 1154 | |
| 1155 | char key[PROPERTY_KEY_MAX]; |
| 1156 | char value[PROPERTY_VALUE_MAX]; |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1157 | |
| 1158 | // adjusts max on the fly |
| 1159 | if (progress > weight_total) { |
| 1160 | int new_total = weight_total * 1.2; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1161 | MYLOGD("Adjusting total weight from %d to %d\n", weight_total, new_total); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1162 | weight_total = new_total; |
| 1163 | sprintf(key, "dumpstate.%d.max", getpid()); |
| 1164 | sprintf(value, "%d", weight_total); |
| 1165 | int status = property_set(key, value); |
| 1166 | if (status) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1167 | MYLOGE("Could not update max weight by setting system property %s to %s: %d\n", |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1168 | key, value, status); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1172 | sprintf(key, "dumpstate.%d.progress", getpid()); |
| 1173 | sprintf(value, "%d", progress); |
| 1174 | |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1175 | if (progress % 100 == 0) { |
| 1176 | // We don't want to spam logcat, so only log multiples of 100. |
| 1177 | MYLOGD("Setting progress (%s): %s/%d\n", key, value, weight_total); |
| 1178 | } else { |
| 1179 | // stderr is ignored on normal invocations, but useful when calling /system/bin/dumpstate |
| 1180 | // directly for debuggging. |
| 1181 | fprintf(stderr, "Setting progress (%s): %s/%d\n", key, value, weight_total); |
| 1182 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1183 | |
| 1184 | int status = property_set(key, value); |
| 1185 | if (status) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1186 | MYLOGE("Could not update progress by setting system property %s to %s: %d\n", |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1187 | key, value, status); |
| 1188 | } |
| 1189 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1190 | |
Felipe Leme | 3634a1e | 2015-12-09 10:11:47 -0800 | [diff] [blame] | 1191 | void take_screenshot(const std::string& path) { |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1192 | const char *args[] = { "/system/bin/screencap", "-p", path.c_str(), NULL }; |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 1193 | run_command_always(NULL, false, 10, args); |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1194 | } |
Mark Salyzyn | f55d402 | 2015-12-11 07:32:31 -0800 | [diff] [blame] | 1195 | |
Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 1196 | void vibrate(FILE* vibrator, int ms) { |
| 1197 | fprintf(vibrator, "%d\n", ms); |
| 1198 | fflush(vibrator); |
| 1199 | } |
| 1200 | |
| 1201 | bool is_dir(const char* pathname) { |
| 1202 | struct stat info; |
| 1203 | if (stat(pathname, &info) == -1) { |
| 1204 | return false; |
| 1205 | } |
| 1206 | return S_ISDIR(info.st_mode); |
| 1207 | } |
| 1208 | |
| 1209 | time_t get_mtime(int fd, time_t default_mtime) { |
| 1210 | struct stat info; |
| 1211 | if (fstat(fd, &info) == -1) { |
| 1212 | return default_mtime; |
| 1213 | } |
| 1214 | return info.st_mtime; |
| 1215 | } |
| 1216 | |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1217 | void dump_emmc_ecsd(const char *ext_csd_path) { |
| 1218 | static const size_t EXT_CSD_REV = 192; |
| 1219 | static const size_t EXT_PRE_EOL_INFO = 267; |
| 1220 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268; |
| 1221 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269; |
| 1222 | struct hex { |
| 1223 | char str[2]; |
| 1224 | } buffer[512]; |
| 1225 | int fd, ext_csd_rev, ext_pre_eol_info; |
| 1226 | ssize_t bytes_read; |
| 1227 | static const char *ver_str[] = { |
| 1228 | "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0" |
| 1229 | }; |
| 1230 | static const char *eol_str[] = { |
| 1231 | "Undefined", |
| 1232 | "Normal", |
| 1233 | "Warning (consumed 80% of reserve)", |
Mark Salyzyn | 4b45d67 | 2015-12-11 10:41:52 -0800 | [diff] [blame] | 1234 | "Urgent (consumed 90% of reserve)" |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1235 | }; |
| 1236 | |
| 1237 | printf("------ %s Extended CSD ------\n", ext_csd_path); |
| 1238 | |
| 1239 | fd = TEMP_FAILURE_RETRY(open(ext_csd_path, |
| 1240 | O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 1241 | if (fd < 0) { |
| 1242 | printf("*** %s: %s\n\n", ext_csd_path, strerror(errno)); |
| 1243 | return; |
| 1244 | } |
| 1245 | |
| 1246 | bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 1247 | close(fd); |
| 1248 | if (bytes_read < 0) { |
| 1249 | printf("*** %s: %s\n\n", ext_csd_path, strerror(errno)); |
| 1250 | return; |
| 1251 | } |
Mark Salyzyn | 4b45d67 | 2015-12-11 10:41:52 -0800 | [diff] [blame] | 1252 | if (bytes_read < (ssize_t)(EXT_CSD_REV * sizeof(struct hex))) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1253 | printf("*** %s: truncated content %zd\n\n", ext_csd_path, bytes_read); |
| 1254 | return; |
| 1255 | } |
| 1256 | |
| 1257 | ext_csd_rev = 0; |
| 1258 | if (sscanf(buffer[EXT_CSD_REV].str, "%02x", &ext_csd_rev) != 1) { |
| 1259 | printf("*** %s: EXT_CSD_REV parse error \"%.2s\"\n\n", |
| 1260 | ext_csd_path, buffer[EXT_CSD_REV].str); |
| 1261 | return; |
| 1262 | } |
| 1263 | |
| 1264 | printf("rev 1.%d (MMC %s)\n", |
| 1265 | ext_csd_rev, |
| 1266 | (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? |
| 1267 | ver_str[ext_csd_rev] : |
| 1268 | "Unknown"); |
| 1269 | if (ext_csd_rev < 7) { |
| 1270 | printf("\n"); |
| 1271 | return; |
| 1272 | } |
| 1273 | |
Mark Salyzyn | 4b45d67 | 2015-12-11 10:41:52 -0800 | [diff] [blame] | 1274 | if (bytes_read < (ssize_t)(EXT_PRE_EOL_INFO * sizeof(struct hex))) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1275 | printf("*** %s: truncated content %zd\n\n", ext_csd_path, bytes_read); |
| 1276 | return; |
| 1277 | } |
| 1278 | |
| 1279 | ext_pre_eol_info = 0; |
| 1280 | if (sscanf(buffer[EXT_PRE_EOL_INFO].str, "%02x", &ext_pre_eol_info) != 1) { |
| 1281 | printf("*** %s: PRE_EOL_INFO parse error \"%.2s\"\n\n", |
| 1282 | ext_csd_path, buffer[EXT_PRE_EOL_INFO].str); |
| 1283 | return; |
| 1284 | } |
| 1285 | printf("PRE_EOL_INFO %d (MMC %s)\n", |
| 1286 | ext_pre_eol_info, |
| 1287 | eol_str[(ext_pre_eol_info < (int) |
| 1288 | (sizeof(eol_str) / sizeof(eol_str[0]))) ? |
| 1289 | ext_pre_eol_info : 0]); |
| 1290 | |
| 1291 | for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A; |
| 1292 | lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B; |
| 1293 | ++lifetime) { |
| 1294 | int ext_device_life_time_est; |
| 1295 | static const char *est_str[] = { |
| 1296 | "Undefined", |
| 1297 | "0-10% of device lifetime used", |
| 1298 | "10-20% of device lifetime used", |
| 1299 | "20-30% of device lifetime used", |
| 1300 | "30-40% of device lifetime used", |
| 1301 | "40-50% of device lifetime used", |
| 1302 | "50-60% of device lifetime used", |
| 1303 | "60-70% of device lifetime used", |
| 1304 | "70-80% of device lifetime used", |
| 1305 | "80-90% of device lifetime used", |
| 1306 | "90-100% of device lifetime used", |
| 1307 | "Exceeded the maximum estimated device lifetime", |
| 1308 | }; |
| 1309 | |
Mark Salyzyn | 4b45d67 | 2015-12-11 10:41:52 -0800 | [diff] [blame] | 1310 | if (bytes_read < (ssize_t)(lifetime * sizeof(struct hex))) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1311 | printf("*** %s: truncated content %zd\n", ext_csd_path, bytes_read); |
| 1312 | break; |
| 1313 | } |
| 1314 | |
| 1315 | ext_device_life_time_est = 0; |
| 1316 | if (sscanf(buffer[lifetime].str, "%02x", &ext_device_life_time_est) != 1) { |
| 1317 | printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%.2s\"\n", |
| 1318 | ext_csd_path, |
| 1319 | (unsigned)(lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) + 'A', |
| 1320 | buffer[lifetime].str); |
| 1321 | continue; |
| 1322 | } |
| 1323 | printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n", |
| 1324 | (unsigned)(lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) + 'A', |
| 1325 | ext_device_life_time_est, |
| 1326 | est_str[(ext_device_life_time_est < (int) |
| 1327 | (sizeof(est_str) / sizeof(est_str[0]))) ? |
| 1328 | ext_device_life_time_est : 0]); |
| 1329 | } |
| 1330 | |
| 1331 | printf("\n"); |
| 1332 | } |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 1333 | |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 1334 | // TODO: refactor all those commands that convert args |
| 1335 | void format_args(int argc, const char *argv[], std::string *args) { |
| 1336 | LOG_ALWAYS_FATAL_IF(args == nullptr); |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 1337 | for (int i = 0; i < argc; i++) { |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 1338 | args->append(argv[i]); |
| 1339 | if (i < argc -1) { |
| 1340 | args->append(" "); |
| 1341 | } |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 1342 | } |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 1343 | } |
| 1344 | void format_args(const char* command, const char *args[], std::string *string) { |
| 1345 | LOG_ALWAYS_FATAL_IF(args == nullptr || command == nullptr); |
| 1346 | string->append(command); |
| 1347 | if (args[0] == nullptr) return; |
| 1348 | string->append(" "); |
| 1349 | |
| 1350 | for (int arg = 1; arg <= 1000; ++arg) { |
| 1351 | if (args[arg] == nullptr) return; |
| 1352 | string->append(args[arg]); |
| 1353 | if (args[arg+1] != nullptr) { |
| 1354 | string->append(" "); |
| 1355 | } |
| 1356 | } |
| 1357 | MYLOGE("internal error: missing NULL entry on %s", string->c_str()); |
Felipe Leme | 88c7933 | 2016-02-22 11:06:49 -0800 | [diff] [blame] | 1358 | } |