static int tl_panic_handler(struct notifier_block *nb, unsigned long l, void *buf) { unregister_tracepoints(); dump_traces(); return NOTIFY_DONE; }
int main(int argc, char *argv[]) { struct sigaction sigact; int do_add_date = 0; int do_compress = 0; int do_vibrate = 1; char* use_outfile = 0; char* begin_sound = 0; char* end_sound = 0; int use_socket = 0; int do_fb = 0; if (getuid() != 0) { // Old versions of the adb client would call the // dumpstate command directly. Newer clients // call /system/bin/bugreport instead. If we detect // we're being called incorrectly, then exec the // correct program. return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL); } ALOGI("begin\n"); memset(&sigact, 0, sizeof(sigact)); sigact.sa_handler = sigpipe_handler; sigaction(SIGPIPE, &sigact, NULL); /* set as high priority, and protect from OOM killer */ setpriority(PRIO_PROCESS, 0, -20); FILE *oom_adj = fopen("/proc/self/oom_adj", "w"); if (oom_adj) { fputs("-17", oom_adj); fclose(oom_adj); } /* very first thing, collect stack traces from Dalvik and native processes (needs root) */ dump_traces_path = dump_traces(); int c; while ((c = getopt(argc, argv, "b:de:ho:svqzp")) != -1) { switch (c) { case 'b': begin_sound = optarg; break; case 'd': do_add_date = 1; break; case 'e': end_sound = optarg; break; case 'o': use_outfile = optarg; break; case 's': use_socket = 1; break; case 'v': break; // compatibility no-op case 'q': do_vibrate = 0; break; case 'z': do_compress = 6; break; case 'p': do_fb = 1; break; case '?': printf("\n"); case 'h': usage(); exit(1); } } FILE *vibrator = 0; if (do_vibrate) { /* open the vibrator before dropping root */ vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w"); if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC); } /* read /proc/cmdline before dropping root */ FILE *cmdline = fopen("/proc/cmdline", "r"); if (cmdline != NULL) { fgets(cmdline_buf, sizeof(cmdline_buf), cmdline); fclose(cmdline); } if (prctl(PR_SET_KEEPCAPS, 1) < 0) { ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); return -1; } /* switch to non-root user and group */ gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, AID_MOUNT, AID_INET, AID_NET_BW_STATS }; if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); return -1; } if (setgid(AID_SHELL) != 0) { ALOGE("Unable to setgid, aborting: %s\n", strerror(errno)); return -1; } if (setuid(AID_SHELL) != 0) { ALOGE("Unable to setuid, aborting: %s\n", strerror(errno)); return -1; } struct __user_cap_header_struct capheader; struct __user_cap_data_struct capdata[2]; memset(&capheader, 0, sizeof(capheader)); memset(&capdata, 0, sizeof(capdata)); capheader.version = _LINUX_CAPABILITY_VERSION_3; capheader.pid = 0; capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG); capdata[0].inheritable = 0; capdata[1].inheritable = 0; if (capset(&capheader, &capdata[0]) < 0) { ALOGE("capset failed: %s\n", strerror(errno)); return -1; } char path[PATH_MAX], tmp_path[PATH_MAX]; pid_t gzip_pid = -1; if (use_socket) { redirect_to_socket(stdout, "dumpstate"); } else if (use_outfile) { strlcpy(path, use_outfile, sizeof(path)); if (do_add_date) { char date[80]; time_t now = time(NULL); strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now)); strlcat(path, date, sizeof(path)); } if (do_fb) { strlcpy(screenshot_path, path, sizeof(screenshot_path)); strlcat(screenshot_path, ".png", sizeof(screenshot_path)); } strlcat(path, ".txt", sizeof(path)); if (do_compress) strlcat(path, ".gz", sizeof(path)); strlcpy(tmp_path, path, sizeof(tmp_path)); strlcat(tmp_path, ".tmp", sizeof(tmp_path)); gzip_pid = redirect_to_file(stdout, tmp_path, do_compress); } if (begin_sound) { play_sound(begin_sound); } else if (vibrator) { fputs("150", vibrator); fflush(vibrator); } dumpstate(); if (end_sound) { play_sound(end_sound); } else if (vibrator) { int i; for (i = 0; i < 3; i++) { fputs("75\n", vibrator); fflush(vibrator); usleep((75 + 50) * 1000); } fclose(vibrator); } /* wait for gzip to finish, otherwise it might get killed when we exit */ if (gzip_pid > 0) { fclose(stdout); waitpid(gzip_pid, NULL, 0); } /* rename the (now complete) .tmp file to its final location */ if (use_outfile && rename(tmp_path, path)) { fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno)); } ALOGI("done\n"); return 0; }