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> |
| 26 | #include <string.h> |
| 27 | #include <sys/inotify.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <sys/klog.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 34 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 35 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 36 | #include <cutils/debugger.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> |
| 38 | #include <cutils/sockets.h> |
| 39 | #include <private/android_filesystem_config.h> |
| 40 | |
Robert Craig | 9579837 | 2013-04-04 06:33:10 -0400 | [diff] [blame] | 41 | #include <selinux/android.h> |
| 42 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 43 | #include "dumpstate.h" |
| 44 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 45 | /* list of native processes to include in the native dumps */ |
| 46 | static const char* native_processes_to_dump[] = { |
James Dong | 1fc4f80 | 2012-09-10 16:08:48 -0700 | [diff] [blame] | 47 | "/system/bin/drmserver", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 48 | "/system/bin/mediaserver", |
| 49 | "/system/bin/sdcard", |
| 50 | "/system/bin/surfaceflinger", |
| 51 | NULL, |
| 52 | }; |
| 53 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 54 | void for_each_userid(void (*func)(int), const char *header) { |
| 55 | DIR *d; |
| 56 | struct dirent *de; |
| 57 | |
| 58 | if (header) printf("\n------ %s ------\n", header); |
| 59 | func(0); |
| 60 | |
| 61 | if (!(d = opendir("/data/system/users"))) { |
| 62 | printf("Failed to open /data/system/users (%s)\n", strerror(errno)); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | while ((de = readdir(d))) { |
| 67 | int userid; |
| 68 | if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) { |
| 69 | continue; |
| 70 | } |
| 71 | func(userid); |
| 72 | } |
| 73 | |
| 74 | closedir(d); |
| 75 | } |
| 76 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 77 | 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] | 78 | DIR *d; |
| 79 | struct dirent *de; |
| 80 | |
| 81 | if (!(d = opendir("/proc"))) { |
| 82 | printf("Failed to open /proc (%s)\n", strerror(errno)); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | printf("\n------ %s ------\n", header); |
| 87 | while ((de = readdir(d))) { |
| 88 | int pid; |
| 89 | int fd; |
| 90 | char cmdpath[255]; |
| 91 | char cmdline[255]; |
| 92 | |
| 93 | if (!(pid = atoi(de->d_name))) { |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | sprintf(cmdpath,"/proc/%d/cmdline", pid); |
| 98 | memset(cmdline, 0, sizeof(cmdline)); |
| 99 | if ((fd = open(cmdpath, O_RDONLY)) < 0) { |
| 100 | strcpy(cmdline, "N/A"); |
| 101 | } else { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 102 | read(fd, cmdline, sizeof(cmdline) - 1); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 103 | close(fd); |
| 104 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 105 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | closedir(d); |
| 109 | } |
| 110 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 111 | static void for_each_pid_helper(int pid, const char *cmdline, void *arg) { |
| 112 | for_each_pid_func *func = arg; |
| 113 | func(pid, cmdline); |
| 114 | } |
| 115 | |
| 116 | void for_each_pid(for_each_pid_func func, const char *header) { |
| 117 | __for_each_pid(for_each_pid_helper, header, func); |
| 118 | } |
| 119 | |
| 120 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 121 | DIR *d; |
| 122 | struct dirent *de; |
| 123 | char taskpath[255]; |
| 124 | for_each_tid_func *func = arg; |
| 125 | |
| 126 | sprintf(taskpath, "/proc/%d/task", pid); |
| 127 | |
| 128 | if (!(d = opendir(taskpath))) { |
| 129 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | func(pid, pid, cmdline); |
| 134 | |
| 135 | while ((de = readdir(d))) { |
| 136 | int tid; |
| 137 | int fd; |
| 138 | char commpath[255]; |
| 139 | char comm[255]; |
| 140 | |
| 141 | if (!(tid = atoi(de->d_name))) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | if (tid == pid) |
| 146 | continue; |
| 147 | |
| 148 | sprintf(commpath,"/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 149 | memset(comm, 0, sizeof(comm)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 150 | if ((fd = open(commpath, O_RDONLY)) < 0) { |
| 151 | strcpy(comm, "N/A"); |
| 152 | } else { |
| 153 | char *c; |
| 154 | read(fd, comm, sizeof(comm) - 1); |
| 155 | close(fd); |
| 156 | |
| 157 | c = strrchr(comm, '\n'); |
| 158 | if (c) { |
| 159 | *c = '\0'; |
| 160 | } |
| 161 | } |
| 162 | func(pid, tid, comm); |
| 163 | } |
| 164 | |
| 165 | closedir(d); |
| 166 | } |
| 167 | |
| 168 | void for_each_tid(for_each_tid_func func, const char *header) { |
| 169 | __for_each_pid(for_each_tid_helper, header, func); |
| 170 | } |
| 171 | |
| 172 | void show_wchan(int pid, int tid, const char *name) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 173 | char path[255]; |
| 174 | char buffer[255]; |
| 175 | int fd; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 176 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 177 | |
| 178 | memset(buffer, 0, sizeof(buffer)); |
| 179 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 180 | sprintf(path, "/proc/%d/wchan", tid); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 181 | if ((fd = open(path, O_RDONLY)) < 0) { |
| 182 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (read(fd, buffer, sizeof(buffer)) < 0) { |
| 187 | printf("Failed to read '%s' (%s)\n", path, strerror(errno)); |
| 188 | goto out_close; |
| 189 | } |
| 190 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 191 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 192 | pid == tid ? 0 : 3, "", name); |
| 193 | |
| 194 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 195 | |
| 196 | out_close: |
| 197 | close(fd); |
| 198 | return; |
| 199 | } |
| 200 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 201 | void do_dump_settings(int userid) { |
| 202 | char title[255]; |
| 203 | char dbpath[255]; |
| 204 | char sql[255]; |
| 205 | sprintf(title, "SYSTEM SETTINGS (user %d)", userid); |
| 206 | if (userid == 0) { |
| 207 | strcpy(dbpath, "/data/data/com.android.providers.settings/databases/settings.db"); |
| 208 | strcpy(sql, "pragma user_version; select * from system; select * from secure; select * from global;"); |
| 209 | } else { |
| 210 | sprintf(dbpath, "/data/system/users/%d/settings.db", userid); |
| 211 | strcpy(sql, "pragma user_version; select * from system; select * from secure;"); |
| 212 | } |
| 213 | run_command(title, 20, SU_PATH, "root", "sqlite3", dbpath, sql, NULL); |
| 214 | return; |
| 215 | } |
| 216 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 217 | void do_dmesg() { |
| 218 | printf("------ KERNEL LOG (dmesg) ------\n"); |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 219 | /* Get size of kernel buffer */ |
| 220 | int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 221 | if (size <= 0) { |
| 222 | printf("Unexpected klogctl return value: %d\n\n", size); |
| 223 | return; |
| 224 | } |
| 225 | char *buf = (char *) malloc(size + 1); |
| 226 | if (buf == NULL) { |
| 227 | printf("memory allocation failed\n\n"); |
| 228 | return; |
| 229 | } |
| 230 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 231 | if (retval < 0) { |
| 232 | printf("klogctl failure\n\n"); |
| 233 | free(buf); |
| 234 | return; |
| 235 | } |
| 236 | buf[retval] = '\0'; |
| 237 | printf("%s\n\n", buf); |
| 238 | free(buf); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | void do_showmap(int pid, const char *name) { |
| 243 | char title[255]; |
| 244 | char arg[255]; |
| 245 | |
| 246 | sprintf(title, "SHOW MAP %d (%s)", pid, name); |
| 247 | sprintf(arg, "%d", pid); |
| 248 | run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL); |
| 249 | } |
| 250 | |
| 251 | /* prints the contents of a file */ |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 252 | int dump_file(const char *title, const char *path) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 253 | int fd = open(path, O_RDONLY); |
| 254 | if (fd < 0) { |
| 255 | int err = errno; |
| 256 | if (title) printf("------ %s (%s) ------\n", title, path); |
| 257 | printf("*** %s: %s\n", path, strerror(err)); |
| 258 | if (title) printf("\n"); |
| 259 | return -1; |
| 260 | } |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 261 | return dump_file_from_fd(title, path, fd); |
| 262 | } |
| 263 | |
| 264 | int dump_file_from_fd(const char *title, const char *path, int fd) { |
| 265 | char buffer[32768]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 266 | |
| 267 | if (title) printf("------ %s (%s", title, path); |
| 268 | |
| 269 | if (title) { |
| 270 | struct stat st; |
| 271 | if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) { |
| 272 | char stamp[80]; |
| 273 | time_t mtime = st.st_mtime; |
| 274 | strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); |
| 275 | printf(": %s", stamp); |
| 276 | } |
| 277 | printf(") ------\n"); |
| 278 | } |
| 279 | |
| 280 | int newline = 0; |
| 281 | for (;;) { |
| 282 | int ret = read(fd, buffer, sizeof(buffer)); |
| 283 | if (ret > 0) { |
| 284 | newline = (buffer[ret - 1] == '\n'); |
| 285 | ret = fwrite(buffer, ret, 1, stdout); |
| 286 | } |
| 287 | if (ret <= 0) break; |
| 288 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 289 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 290 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 291 | if (!newline) printf("\n"); |
| 292 | if (title) printf("\n"); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* forks a command and waits for it to finish */ |
| 297 | int run_command(const char *title, int timeout_seconds, const char *command, ...) { |
| 298 | fflush(stdout); |
| 299 | clock_t start = clock(); |
| 300 | pid_t pid = fork(); |
| 301 | |
| 302 | /* handle error case */ |
| 303 | if (pid < 0) { |
| 304 | printf("*** fork: %s\n", strerror(errno)); |
| 305 | return pid; |
| 306 | } |
| 307 | |
| 308 | /* handle child case */ |
| 309 | if (pid == 0) { |
| 310 | const char *args[1024] = {command}; |
| 311 | size_t arg; |
| 312 | |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 313 | /* make sure the child dies when dumpstate dies */ |
| 314 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 315 | |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame^] | 316 | /* just ignore SIGPIPE, will go down with parent's */ |
| 317 | struct sigaction sigact; |
| 318 | memset(&sigact, 0, sizeof(sigact)); |
| 319 | sigact.sa_handler = SIG_IGN; |
| 320 | sigaction(SIGPIPE, &sigact, NULL); |
| 321 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 322 | va_list ap; |
| 323 | va_start(ap, command); |
| 324 | if (title) printf("------ %s (%s", title, command); |
| 325 | for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) { |
| 326 | args[arg] = va_arg(ap, const char *); |
| 327 | if (args[arg] == NULL) break; |
| 328 | if (title) printf(" %s", args[arg]); |
| 329 | } |
| 330 | if (title) printf(") ------\n"); |
| 331 | fflush(stdout); |
| 332 | |
| 333 | execvp(command, (char**) args); |
| 334 | printf("*** exec(%s): %s\n", command, strerror(errno)); |
| 335 | fflush(stdout); |
| 336 | _exit(-1); |
| 337 | } |
| 338 | |
| 339 | /* handle parent case */ |
| 340 | for (;;) { |
| 341 | int status; |
| 342 | pid_t p = waitpid(pid, &status, WNOHANG); |
| 343 | float elapsed = (float) (clock() - start) / CLOCKS_PER_SEC; |
| 344 | if (p == pid) { |
| 345 | if (WIFSIGNALED(status)) { |
| 346 | printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); |
| 347 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
| 348 | printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); |
| 349 | } |
| 350 | if (title) printf("[%s: %.1fs elapsed]\n\n", command, elapsed); |
| 351 | return status; |
| 352 | } |
| 353 | |
| 354 | if (timeout_seconds && elapsed > timeout_seconds) { |
| 355 | printf("*** %s: Timed out after %.1fs (killing pid %d)\n", command, elapsed, pid); |
| 356 | kill(pid, SIGTERM); |
| 357 | return -1; |
| 358 | } |
| 359 | |
| 360 | usleep(100000); // poll every 0.1 sec |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | size_t num_props = 0; |
| 365 | static char* props[2000]; |
| 366 | |
| 367 | static void print_prop(const char *key, const char *name, void *user) { |
| 368 | (void) user; |
| 369 | if (num_props < sizeof(props) / sizeof(props[0])) { |
| 370 | char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; |
| 371 | snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); |
| 372 | props[num_props++] = strdup(buf); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | static int compare_prop(const void *a, const void *b) { |
| 377 | return strcmp(*(char * const *) a, *(char * const *) b); |
| 378 | } |
| 379 | |
| 380 | /* prints all the system properties */ |
| 381 | void print_properties() { |
| 382 | size_t i; |
| 383 | num_props = 0; |
| 384 | property_list(print_prop, NULL); |
| 385 | qsort(&props, num_props, sizeof(props[0]), compare_prop); |
| 386 | |
| 387 | printf("------ SYSTEM PROPERTIES ------\n"); |
| 388 | for (i = 0; i < num_props; ++i) { |
| 389 | fputs(props[i], stdout); |
| 390 | free(props[i]); |
| 391 | } |
| 392 | printf("\n"); |
| 393 | } |
| 394 | |
| 395 | /* redirect output to a service control socket */ |
| 396 | void redirect_to_socket(FILE *redirect, const char *service) { |
| 397 | int s = android_get_control_socket(service); |
| 398 | if (s < 0) { |
| 399 | fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno)); |
| 400 | exit(1); |
| 401 | } |
| 402 | if (listen(s, 4) < 0) { |
| 403 | fprintf(stderr, "listen(control socket): %s\n", strerror(errno)); |
| 404 | exit(1); |
| 405 | } |
| 406 | |
| 407 | struct sockaddr addr; |
| 408 | socklen_t alen = sizeof(addr); |
| 409 | int fd = accept(s, &addr, &alen); |
| 410 | if (fd < 0) { |
| 411 | fprintf(stderr, "accept(control socket): %s\n", strerror(errno)); |
| 412 | exit(1); |
| 413 | } |
| 414 | |
| 415 | fflush(redirect); |
| 416 | dup2(fd, fileno(redirect)); |
| 417 | close(fd); |
| 418 | } |
| 419 | |
| 420 | /* redirect output to a file, optionally gzipping; returns gzip pid (or -1) */ |
| 421 | pid_t redirect_to_file(FILE *redirect, char *path, int gzip_level) { |
| 422 | char *chp = path; |
| 423 | |
| 424 | /* skip initial slash */ |
| 425 | if (chp[0] == '/') |
| 426 | chp++; |
| 427 | |
| 428 | /* create leading directories, if necessary */ |
| 429 | while (chp && chp[0]) { |
| 430 | chp = strchr(chp, '/'); |
| 431 | if (chp) { |
| 432 | *chp = 0; |
Jeff Sharkey | 27f9e6d | 2013-03-13 15:45:50 -0700 | [diff] [blame] | 433 | mkdir(path, 0770); /* drwxrwx--- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 434 | *chp++ = '/'; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 439 | if (fd < 0) { |
| 440 | fprintf(stderr, "%s: %s\n", path, strerror(errno)); |
| 441 | exit(1); |
| 442 | } |
| 443 | |
| 444 | pid_t gzip_pid = -1; |
| 445 | if (gzip_level > 0) { |
| 446 | int fds[2]; |
| 447 | if (pipe(fds)) { |
| 448 | fprintf(stderr, "pipe: %s\n", strerror(errno)); |
| 449 | exit(1); |
| 450 | } |
| 451 | |
| 452 | fflush(redirect); |
| 453 | fflush(stdout); |
| 454 | |
| 455 | gzip_pid = fork(); |
| 456 | if (gzip_pid < 0) { |
| 457 | fprintf(stderr, "fork: %s\n", strerror(errno)); |
| 458 | exit(1); |
| 459 | } |
| 460 | |
| 461 | if (gzip_pid == 0) { |
| 462 | dup2(fds[0], STDIN_FILENO); |
| 463 | dup2(fd, STDOUT_FILENO); |
| 464 | |
| 465 | close(fd); |
| 466 | close(fds[0]); |
| 467 | close(fds[1]); |
| 468 | |
| 469 | char level[10]; |
| 470 | snprintf(level, sizeof(level), "-%d", gzip_level); |
| 471 | execlp("gzip", "gzip", level, NULL); |
| 472 | fprintf(stderr, "exec(gzip): %s\n", strerror(errno)); |
| 473 | _exit(-1); |
| 474 | } |
| 475 | |
| 476 | close(fd); |
| 477 | close(fds[0]); |
| 478 | fd = fds[1]; |
| 479 | } |
| 480 | |
| 481 | dup2(fd, fileno(redirect)); |
| 482 | close(fd); |
| 483 | return gzip_pid; |
| 484 | } |
| 485 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 486 | static bool should_dump_native_traces(const char* path) { |
| 487 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 488 | if (!strcmp(*p, path)) { |
| 489 | return true; |
| 490 | } |
| 491 | } |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 496 | const char *dump_traces() { |
| 497 | const char* result = NULL; |
| 498 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 499 | char traces_path[PROPERTY_VALUE_MAX] = ""; |
| 500 | property_get("dalvik.vm.stack-trace-file", traces_path, ""); |
| 501 | if (!traces_path[0]) return NULL; |
| 502 | |
| 503 | /* move the old traces.txt (if any) out of the way temporarily */ |
| 504 | char anr_traces_path[PATH_MAX]; |
| 505 | strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path)); |
| 506 | strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path)); |
| 507 | if (rename(traces_path, anr_traces_path) && errno != ENOENT) { |
| 508 | fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno)); |
| 509 | return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead |
| 510 | } |
| 511 | |
| 512 | /* make the directory if necessary */ |
| 513 | char anr_traces_dir[PATH_MAX]; |
| 514 | strlcpy(anr_traces_dir, traces_path, sizeof(anr_traces_dir)); |
| 515 | char *slash = strrchr(anr_traces_dir, '/'); |
| 516 | if (slash != NULL) { |
| 517 | *slash = '\0'; |
| 518 | if (!mkdir(anr_traces_dir, 0775)) { |
| 519 | chown(anr_traces_dir, AID_SYSTEM, AID_SYSTEM); |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 520 | chmod(anr_traces_dir, 0775); |
Stephen Smalley | 2628820 | 2014-02-07 09:16:46 -0500 | [diff] [blame] | 521 | if (selinux_android_restorecon(anr_traces_dir, 0) == -1) { |
Robert Craig | 9579837 | 2013-04-04 06:33:10 -0400 | [diff] [blame] | 522 | fprintf(stderr, "restorecon failed for %s: %s\n", anr_traces_dir, strerror(errno)); |
| 523 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 524 | } else if (errno != EEXIST) { |
| 525 | fprintf(stderr, "mkdir(%s): %s\n", anr_traces_dir, strerror(errno)); |
| 526 | return NULL; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* create a new, empty traces.txt file to receive stack dumps */ |
Nick Kralevich | d51820e | 2012-04-06 09:53:45 -0700 | [diff] [blame] | 531 | int fd = open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 0666); /* -rw-rw-rw- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 532 | if (fd < 0) { |
| 533 | fprintf(stderr, "%s: %s\n", traces_path, strerror(errno)); |
| 534 | return NULL; |
| 535 | } |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 536 | int chmod_ret = fchmod(fd, 0666); |
| 537 | if (chmod_ret < 0) { |
| 538 | fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno)); |
| 539 | close(fd); |
| 540 | return NULL; |
| 541 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 542 | |
| 543 | /* walk /proc and kill -QUIT all Dalvik processes */ |
| 544 | DIR *proc = opendir("/proc"); |
| 545 | if (proc == NULL) { |
| 546 | fprintf(stderr, "/proc: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 547 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* use inotify to find when processes are done dumping */ |
| 551 | int ifd = inotify_init(); |
| 552 | if (ifd < 0) { |
| 553 | fprintf(stderr, "inotify_init: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 554 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | int wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE); |
| 558 | if (wfd < 0) { |
| 559 | fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 560 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | struct dirent *d; |
| 564 | int dalvik_found = 0; |
| 565 | while ((d = readdir(proc))) { |
| 566 | int pid = atoi(d->d_name); |
| 567 | if (pid <= 0) continue; |
| 568 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 569 | char path[PATH_MAX]; |
| 570 | char data[PATH_MAX]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 571 | snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 572 | ssize_t len = readlink(path, data, sizeof(data) - 1); |
| 573 | if (len <= 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 574 | continue; |
| 575 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 576 | data[len] = '\0'; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 577 | |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 578 | if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 579 | /* skip zygote -- it won't dump its stack anyway */ |
| 580 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 581 | int fd = open(path, O_RDONLY); |
| 582 | len = read(fd, data, sizeof(data) - 1); |
| 583 | close(fd); |
| 584 | if (len <= 0) { |
| 585 | continue; |
| 586 | } |
| 587 | data[len] = '\0'; |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 588 | if (!strncmp(data, "zygote", strlen("zygote"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 589 | continue; |
| 590 | } |
| 591 | |
| 592 | ++dalvik_found; |
| 593 | if (kill(pid, SIGQUIT)) { |
| 594 | fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); |
| 595 | continue; |
| 596 | } |
| 597 | |
| 598 | /* wait for the writable-close notification from inotify */ |
| 599 | struct pollfd pfd = { ifd, POLLIN, 0 }; |
Nick Vaccaro | 85453ec | 2014-04-30 11:19:23 -0700 | [diff] [blame] | 600 | int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */ |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 601 | if (ret < 0) { |
| 602 | fprintf(stderr, "poll: %s\n", strerror(errno)); |
| 603 | } else if (ret == 0) { |
| 604 | fprintf(stderr, "warning: timed out dumping pid %d\n", pid); |
| 605 | } else { |
| 606 | struct inotify_event ie; |
| 607 | read(ifd, &ie, sizeof(ie)); |
| 608 | } |
| 609 | } else if (should_dump_native_traces(data)) { |
| 610 | /* dump native process if appropriate */ |
| 611 | if (lseek(fd, 0, SEEK_END) < 0) { |
| 612 | fprintf(stderr, "lseek: %s\n", strerror(errno)); |
| 613 | } else { |
| 614 | dump_backtrace_to_file(pid, fd); |
| 615 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 619 | if (dalvik_found == 0) { |
| 620 | fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n"); |
| 621 | } |
| 622 | |
| 623 | static char dump_traces_path[PATH_MAX]; |
| 624 | strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path)); |
| 625 | strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path)); |
| 626 | if (rename(traces_path, dump_traces_path)) { |
| 627 | fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 628 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 629 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 630 | result = dump_traces_path; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 631 | |
| 632 | /* replace the saved [ANR] traces.txt file */ |
| 633 | rename(anr_traces_path, traces_path); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 634 | |
| 635 | error_close_ifd: |
| 636 | close(ifd); |
| 637 | error_close_fd: |
| 638 | close(fd); |
| 639 | return result; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | void play_sound(const char* path) { |
| 643 | run_command(NULL, 5, "/system/bin/stagefright", "-o", "-a", path, NULL); |
| 644 | } |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 645 | |
| 646 | void dump_route_tables() { |
| 647 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
| 648 | dump_file("RT_TABLES", RT_TABLES_PATH); |
| 649 | FILE* fp = fopen(RT_TABLES_PATH, "r"); |
| 650 | if (!fp) { |
| 651 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
| 652 | return; |
| 653 | } |
| 654 | char table[16]; |
| 655 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 656 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 657 | // Add a fixed max limit so this doesn't go awry. |
| 658 | for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) { |
| 659 | run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL); |
| 660 | run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL); |
| 661 | } |
| 662 | fclose(fp); |
| 663 | } |