2012-03-26 19:38:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/klog.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2013-03-07 21:35:35 +00:00
|
|
|
#include <sys/prctl.h>
|
2012-03-26 19:38:26 +00:00
|
|
|
|
2012-06-07 23:40:01 +00:00
|
|
|
#include <cutils/debugger.h>
|
2012-03-26 19:38:26 +00:00
|
|
|
#include <cutils/properties.h>
|
|
|
|
#include <cutils/sockets.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
2013-04-04 10:33:10 +00:00
|
|
|
#include <selinux/android.h>
|
|
|
|
|
2012-03-26 19:38:26 +00:00
|
|
|
#include "dumpstate.h"
|
|
|
|
|
2014-09-11 21:15:27 +00:00
|
|
|
static const int64_t NANOS_PER_SEC = 1000000000;
|
|
|
|
|
2012-06-07 23:40:01 +00:00
|
|
|
/* list of native processes to include in the native dumps */
|
|
|
|
static const char* native_processes_to_dump[] = {
|
2012-09-10 23:08:48 +00:00
|
|
|
"/system/bin/drmserver",
|
2012-06-07 23:40:01 +00:00
|
|
|
"/system/bin/mediaserver",
|
|
|
|
"/system/bin/sdcard",
|
|
|
|
"/system/bin/surfaceflinger",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2014-01-29 19:14:40 +00:00
|
|
|
void for_each_userid(void (*func)(int), const char *header) {
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
if (header) printf("\n------ %s ------\n", header);
|
|
|
|
func(0);
|
|
|
|
|
|
|
|
if (!(d = opendir("/data/system/users"))) {
|
|
|
|
printf("Failed to open /data/system/users (%s)\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((de = readdir(d))) {
|
|
|
|
int userid;
|
|
|
|
if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
func(userid);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
|
2012-11-02 22:46:56 +00:00
|
|
|
static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
|
2012-03-26 19:38:26 +00:00
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
if (!(d = opendir("/proc"))) {
|
|
|
|
printf("Failed to open /proc (%s)\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n------ %s ------\n", header);
|
|
|
|
while ((de = readdir(d))) {
|
|
|
|
int pid;
|
|
|
|
int fd;
|
|
|
|
char cmdpath[255];
|
|
|
|
char cmdline[255];
|
|
|
|
|
|
|
|
if (!(pid = atoi(de->d_name))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(cmdpath,"/proc/%d/cmdline", pid);
|
|
|
|
memset(cmdline, 0, sizeof(cmdline));
|
|
|
|
if ((fd = open(cmdpath, O_RDONLY)) < 0) {
|
|
|
|
strcpy(cmdline, "N/A");
|
|
|
|
} else {
|
2012-11-02 22:46:56 +00:00
|
|
|
read(fd, cmdline, sizeof(cmdline) - 1);
|
2012-03-26 19:38:26 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2012-11-02 22:46:56 +00:00
|
|
|
helper(pid, cmdline, arg);
|
2012-03-26 19:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
|
2012-11-02 22:46:56 +00:00
|
|
|
static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
|
|
|
|
for_each_pid_func *func = arg;
|
|
|
|
func(pid, cmdline);
|
|
|
|
}
|
|
|
|
|
|
|
|
void for_each_pid(for_each_pid_func func, const char *header) {
|
|
|
|
__for_each_pid(for_each_pid_helper, header, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
|
|
|
|
DIR *d;
|
|
|
|
struct dirent *de;
|
|
|
|
char taskpath[255];
|
|
|
|
for_each_tid_func *func = arg;
|
|
|
|
|
|
|
|
sprintf(taskpath, "/proc/%d/task", pid);
|
|
|
|
|
|
|
|
if (!(d = opendir(taskpath))) {
|
|
|
|
printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
func(pid, pid, cmdline);
|
|
|
|
|
|
|
|
while ((de = readdir(d))) {
|
|
|
|
int tid;
|
|
|
|
int fd;
|
|
|
|
char commpath[255];
|
|
|
|
char comm[255];
|
|
|
|
|
|
|
|
if (!(tid = atoi(de->d_name))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tid == pid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sprintf(commpath,"/proc/%d/comm", tid);
|
2012-11-07 19:25:31 +00:00
|
|
|
memset(comm, 0, sizeof(comm));
|
2012-11-02 22:46:56 +00:00
|
|
|
if ((fd = open(commpath, O_RDONLY)) < 0) {
|
|
|
|
strcpy(comm, "N/A");
|
|
|
|
} else {
|
|
|
|
char *c;
|
|
|
|
read(fd, comm, sizeof(comm) - 1);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
c = strrchr(comm, '\n');
|
|
|
|
if (c) {
|
|
|
|
*c = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func(pid, tid, comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void for_each_tid(for_each_tid_func func, const char *header) {
|
|
|
|
__for_each_pid(for_each_tid_helper, header, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_wchan(int pid, int tid, const char *name) {
|
2012-03-26 19:38:26 +00:00
|
|
|
char path[255];
|
|
|
|
char buffer[255];
|
|
|
|
int fd;
|
2012-11-02 22:46:56 +00:00
|
|
|
char name_buffer[255];
|
2012-03-26 19:38:26 +00:00
|
|
|
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
2012-11-02 22:46:56 +00:00
|
|
|
sprintf(path, "/proc/%d/wchan", tid);
|
2012-03-26 19:38:26 +00:00
|
|
|
if ((fd = open(path, O_RDONLY)) < 0) {
|
|
|
|
printf("Failed to open '%s' (%s)\n", path, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read(fd, buffer, sizeof(buffer)) < 0) {
|
|
|
|
printf("Failed to read '%s' (%s)\n", path, strerror(errno));
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2012-11-02 22:46:56 +00:00
|
|
|
snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
|
|
|
|
pid == tid ? 0 : 3, "", name);
|
|
|
|
|
|
|
|
printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
|
2012-03-26 19:38:26 +00:00
|
|
|
|
|
|
|
out_close:
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-29 19:14:40 +00:00
|
|
|
void do_dump_settings(int userid) {
|
|
|
|
char title[255];
|
|
|
|
char dbpath[255];
|
|
|
|
char sql[255];
|
|
|
|
sprintf(title, "SYSTEM SETTINGS (user %d)", userid);
|
|
|
|
if (userid == 0) {
|
|
|
|
strcpy(dbpath, "/data/data/com.android.providers.settings/databases/settings.db");
|
|
|
|
strcpy(sql, "pragma user_version; select * from system; select * from secure; select * from global;");
|
|
|
|
} else {
|
|
|
|
sprintf(dbpath, "/data/system/users/%d/settings.db", userid);
|
|
|
|
strcpy(sql, "pragma user_version; select * from system; select * from secure;");
|
|
|
|
}
|
|
|
|
run_command(title, 20, SU_PATH, "root", "sqlite3", dbpath, sql, NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-26 19:38:26 +00:00
|
|
|
void do_dmesg() {
|
|
|
|
printf("------ KERNEL LOG (dmesg) ------\n");
|
2012-09-17 18:43:40 +00:00
|
|
|
/* Get size of kernel buffer */
|
|
|
|
int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
|
2012-03-26 19:38:26 +00:00
|
|
|
if (size <= 0) {
|
|
|
|
printf("Unexpected klogctl return value: %d\n\n", size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *buf = (char *) malloc(size + 1);
|
|
|
|
if (buf == NULL) {
|
|
|
|
printf("memory allocation failed\n\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int retval = klogctl(KLOG_READ_ALL, buf, size);
|
|
|
|
if (retval < 0) {
|
|
|
|
printf("klogctl failure\n\n");
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[retval] = '\0';
|
|
|
|
printf("%s\n\n", buf);
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_showmap(int pid, const char *name) {
|
|
|
|
char title[255];
|
|
|
|
char arg[255];
|
|
|
|
|
|
|
|
sprintf(title, "SHOW MAP %d (%s)", pid, name);
|
|
|
|
sprintf(arg, "%d", pid);
|
|
|
|
run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prints the contents of a file */
|
2014-07-22 23:08:19 +00:00
|
|
|
int dump_file(const char *title, const char *path) {
|
2012-03-26 19:38:26 +00:00
|
|
|
int fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
int err = errno;
|
|
|
|
if (title) printf("------ %s (%s) ------\n", title, path);
|
|
|
|
printf("*** %s: %s\n", path, strerror(err));
|
|
|
|
if (title) printf("\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-22 23:08:19 +00:00
|
|
|
return dump_file_from_fd(title, path, fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dump_file_from_fd(const char *title, const char *path, int fd) {
|
|
|
|
char buffer[32768];
|
2012-03-26 19:38:26 +00:00
|
|
|
|
|
|
|
if (title) printf("------ %s (%s", title, path);
|
|
|
|
|
|
|
|
if (title) {
|
|
|
|
struct stat st;
|
|
|
|
if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
|
|
|
|
char stamp[80];
|
|
|
|
time_t mtime = st.st_mtime;
|
|
|
|
strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
|
|
|
|
printf(": %s", stamp);
|
|
|
|
}
|
|
|
|
printf(") ------\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int newline = 0;
|
|
|
|
for (;;) {
|
|
|
|
int ret = read(fd, buffer, sizeof(buffer));
|
|
|
|
if (ret > 0) {
|
|
|
|
newline = (buffer[ret - 1] == '\n');
|
|
|
|
ret = fwrite(buffer, ret, 1, stdout);
|
|
|
|
}
|
|
|
|
if (ret <= 0) break;
|
|
|
|
}
|
|
|
|
close(fd);
|
2014-07-22 23:08:19 +00:00
|
|
|
|
2012-03-26 19:38:26 +00:00
|
|
|
if (!newline) printf("\n");
|
|
|
|
if (title) printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-11 21:15:27 +00:00
|
|
|
static int64_t nanotime() {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return (int64_t)ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec;
|
|
|
|
}
|
|
|
|
|
2012-03-26 19:38:26 +00:00
|
|
|
/* forks a command and waits for it to finish */
|
|
|
|
int run_command(const char *title, int timeout_seconds, const char *command, ...) {
|
|
|
|
fflush(stdout);
|
2014-09-11 21:15:27 +00:00
|
|
|
int64_t start = nanotime();
|
2012-03-26 19:38:26 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
|
|
|
|
/* handle error case */
|
|
|
|
if (pid < 0) {
|
|
|
|
printf("*** fork: %s\n", strerror(errno));
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle child case */
|
|
|
|
if (pid == 0) {
|
|
|
|
const char *args[1024] = {command};
|
|
|
|
size_t arg;
|
|
|
|
|
2013-03-07 21:35:35 +00:00
|
|
|
/* make sure the child dies when dumpstate dies */
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGKILL);
|
|
|
|
|
2014-08-21 19:38:22 +00:00
|
|
|
/* just ignore SIGPIPE, will go down with parent's */
|
|
|
|
struct sigaction sigact;
|
|
|
|
memset(&sigact, 0, sizeof(sigact));
|
|
|
|
sigact.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &sigact, NULL);
|
|
|
|
|
2012-03-26 19:38:26 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, command);
|
|
|
|
if (title) printf("------ %s (%s", title, command);
|
|
|
|
for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
|
|
|
|
args[arg] = va_arg(ap, const char *);
|
|
|
|
if (args[arg] == NULL) break;
|
|
|
|
if (title) printf(" %s", args[arg]);
|
|
|
|
}
|
|
|
|
if (title) printf(") ------\n");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
execvp(command, (char**) args);
|
|
|
|
printf("*** exec(%s): %s\n", command, strerror(errno));
|
|
|
|
fflush(stdout);
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle parent case */
|
|
|
|
for (;;) {
|
|
|
|
int status;
|
|
|
|
pid_t p = waitpid(pid, &status, WNOHANG);
|
2014-09-11 21:15:27 +00:00
|
|
|
int64_t elapsed = nanotime() - start;
|
2012-03-26 19:38:26 +00:00
|
|
|
if (p == pid) {
|
|
|
|
if (WIFSIGNALED(status)) {
|
|
|
|
printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
|
|
|
|
} else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
|
|
|
|
printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
|
|
|
|
}
|
2014-09-11 21:15:27 +00:00
|
|
|
if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC);
|
2012-03-26 19:38:26 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-09-11 21:15:27 +00:00
|
|
|
if (timeout_seconds && elapsed / NANOS_PER_SEC > timeout_seconds) {
|
2014-09-09 20:10:49 +00:00
|
|
|
printf("*** %s: Timed out after %ds (killing pid %d)\n", command, (int) elapsed, pid);
|
2012-03-26 19:38:26 +00:00
|
|
|
kill(pid, SIGTERM);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(100000); // poll every 0.1 sec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t num_props = 0;
|
|
|
|
static char* props[2000];
|
|
|
|
|
|
|
|
static void print_prop(const char *key, const char *name, void *user) {
|
|
|
|
(void) user;
|
|
|
|
if (num_props < sizeof(props) / sizeof(props[0])) {
|
|
|
|
char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
|
|
|
|
snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
|
|
|
|
props[num_props++] = strdup(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int compare_prop(const void *a, const void *b) {
|
|
|
|
return strcmp(*(char * const *) a, *(char * const *) b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prints all the system properties */
|
|
|
|
void print_properties() {
|
|
|
|
size_t i;
|
|
|
|
num_props = 0;
|
|
|
|
property_list(print_prop, NULL);
|
|
|
|
qsort(&props, num_props, sizeof(props[0]), compare_prop);
|
|
|
|
|
|
|
|
printf("------ SYSTEM PROPERTIES ------\n");
|
|
|
|
for (i = 0; i < num_props; ++i) {
|
|
|
|
fputs(props[i], stdout);
|
|
|
|
free(props[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* redirect output to a service control socket */
|
|
|
|
void redirect_to_socket(FILE *redirect, const char *service) {
|
|
|
|
int s = android_get_control_socket(service);
|
|
|
|
if (s < 0) {
|
|
|
|
fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (listen(s, 4) < 0) {
|
|
|
|
fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr addr;
|
|
|
|
socklen_t alen = sizeof(addr);
|
|
|
|
int fd = accept(s, &addr, &alen);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(redirect);
|
|
|
|
dup2(fd, fileno(redirect));
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* redirect output to a file, optionally gzipping; returns gzip pid (or -1) */
|
|
|
|
pid_t redirect_to_file(FILE *redirect, char *path, int gzip_level) {
|
|
|
|
char *chp = path;
|
|
|
|
|
|
|
|
/* skip initial slash */
|
|
|
|
if (chp[0] == '/')
|
|
|
|
chp++;
|
|
|
|
|
|
|
|
/* create leading directories, if necessary */
|
|
|
|
while (chp && chp[0]) {
|
|
|
|
chp = strchr(chp, '/');
|
|
|
|
if (chp) {
|
|
|
|
*chp = 0;
|
2013-03-13 22:45:50 +00:00
|
|
|
mkdir(path, 0770); /* drwxrwx--- */
|
2012-03-26 19:38:26 +00:00
|
|
|
*chp++ = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", path, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t gzip_pid = -1;
|
|
|
|
if (gzip_level > 0) {
|
|
|
|
int fds[2];
|
|
|
|
if (pipe(fds)) {
|
|
|
|
fprintf(stderr, "pipe: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(redirect);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
gzip_pid = fork();
|
|
|
|
if (gzip_pid < 0) {
|
|
|
|
fprintf(stderr, "fork: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gzip_pid == 0) {
|
|
|
|
dup2(fds[0], STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
|
|
|
|
|
|
|
char level[10];
|
|
|
|
snprintf(level, sizeof(level), "-%d", gzip_level);
|
|
|
|
execlp("gzip", "gzip", level, NULL);
|
|
|
|
fprintf(stderr, "exec(gzip): %s\n", strerror(errno));
|
|
|
|
_exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
close(fds[0]);
|
|
|
|
fd = fds[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
dup2(fd, fileno(redirect));
|
|
|
|
close(fd);
|
|
|
|
return gzip_pid;
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:40:01 +00:00
|
|
|
static bool should_dump_native_traces(const char* path) {
|
|
|
|
for (const char** p = native_processes_to_dump; *p; p++) {
|
|
|
|
if (!strcmp(*p, path)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
|
|
|
|
const char *dump_traces() {
|
|
|
|
const char* result = NULL;
|
|
|
|
|
2012-03-26 19:38:26 +00:00
|
|
|
char traces_path[PROPERTY_VALUE_MAX] = "";
|
|
|
|
property_get("dalvik.vm.stack-trace-file", traces_path, "");
|
|
|
|
if (!traces_path[0]) return NULL;
|
|
|
|
|
|
|
|
/* move the old traces.txt (if any) out of the way temporarily */
|
|
|
|
char anr_traces_path[PATH_MAX];
|
|
|
|
strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
|
|
|
|
strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
|
|
|
|
if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
|
|
|
|
fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
|
|
|
|
return NULL; // Can't rename old traces.txt -- no permission? -- leave it alone instead
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make the directory if necessary */
|
|
|
|
char anr_traces_dir[PATH_MAX];
|
|
|
|
strlcpy(anr_traces_dir, traces_path, sizeof(anr_traces_dir));
|
|
|
|
char *slash = strrchr(anr_traces_dir, '/');
|
|
|
|
if (slash != NULL) {
|
|
|
|
*slash = '\0';
|
|
|
|
if (!mkdir(anr_traces_dir, 0775)) {
|
|
|
|
chown(anr_traces_dir, AID_SYSTEM, AID_SYSTEM);
|
2012-04-06 16:31:28 +00:00
|
|
|
chmod(anr_traces_dir, 0775);
|
2014-02-07 14:16:46 +00:00
|
|
|
if (selinux_android_restorecon(anr_traces_dir, 0) == -1) {
|
2013-04-04 10:33:10 +00:00
|
|
|
fprintf(stderr, "restorecon failed for %s: %s\n", anr_traces_dir, strerror(errno));
|
|
|
|
}
|
2012-03-26 19:38:26 +00:00
|
|
|
} else if (errno != EEXIST) {
|
|
|
|
fprintf(stderr, "mkdir(%s): %s\n", anr_traces_dir, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a new, empty traces.txt file to receive stack dumps */
|
2012-04-06 16:53:45 +00:00
|
|
|
int fd = open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
|
2012-03-26 19:38:26 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-04-06 16:31:28 +00:00
|
|
|
int chmod_ret = fchmod(fd, 0666);
|
|
|
|
if (chmod_ret < 0) {
|
|
|
|
fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-26 19:38:26 +00:00
|
|
|
|
|
|
|
/* walk /proc and kill -QUIT all Dalvik processes */
|
|
|
|
DIR *proc = opendir("/proc");
|
|
|
|
if (proc == NULL) {
|
|
|
|
fprintf(stderr, "/proc: %s\n", strerror(errno));
|
2012-06-07 23:40:01 +00:00
|
|
|
goto error_close_fd;
|
2012-03-26 19:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use inotify to find when processes are done dumping */
|
|
|
|
int ifd = inotify_init();
|
|
|
|
if (ifd < 0) {
|
|
|
|
fprintf(stderr, "inotify_init: %s\n", strerror(errno));
|
2012-06-07 23:40:01 +00:00
|
|
|
goto error_close_fd;
|
2012-03-26 19:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
|
|
|
|
if (wfd < 0) {
|
|
|
|
fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
|
2012-06-07 23:40:01 +00:00
|
|
|
goto error_close_ifd;
|
2012-03-26 19:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *d;
|
|
|
|
int dalvik_found = 0;
|
|
|
|
while ((d = readdir(proc))) {
|
|
|
|
int pid = atoi(d->d_name);
|
|
|
|
if (pid <= 0) continue;
|
|
|
|
|
2012-06-07 23:40:01 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
char data[PATH_MAX];
|
2012-03-26 19:38:26 +00:00
|
|
|
snprintf(path, sizeof(path), "/proc/%d/exe", pid);
|
2012-06-07 23:40:01 +00:00
|
|
|
ssize_t len = readlink(path, data, sizeof(data) - 1);
|
|
|
|
if (len <= 0) {
|
2012-03-26 19:38:26 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-06-07 23:40:01 +00:00
|
|
|
data[len] = '\0';
|
2012-03-26 19:38:26 +00:00
|
|
|
|
2014-07-17 02:00:46 +00:00
|
|
|
if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
|
2012-06-07 23:40:01 +00:00
|
|
|
/* skip zygote -- it won't dump its stack anyway */
|
|
|
|
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
|
2014-09-11 21:15:27 +00:00
|
|
|
int cfd = open(path, O_RDONLY);
|
|
|
|
len = read(cfd, data, sizeof(data) - 1);
|
|
|
|
close(cfd);
|
2012-06-07 23:40:01 +00:00
|
|
|
if (len <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
data[len] = '\0';
|
2014-07-17 02:00:46 +00:00
|
|
|
if (!strncmp(data, "zygote", strlen("zygote"))) {
|
2012-06-07 23:40:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++dalvik_found;
|
2014-09-11 21:15:27 +00:00
|
|
|
int64_t start = nanotime();
|
2012-06-07 23:40:01 +00:00
|
|
|
if (kill(pid, SIGQUIT)) {
|
|
|
|
fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for the writable-close notification from inotify */
|
|
|
|
struct pollfd pfd = { ifd, POLLIN, 0 };
|
2014-04-30 18:19:23 +00:00
|
|
|
int ret = poll(&pfd, 1, 5000); /* 5 sec timeout */
|
2012-06-07 23:40:01 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "poll: %s\n", strerror(errno));
|
|
|
|
} else if (ret == 0) {
|
|
|
|
fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
|
|
|
|
} else {
|
|
|
|
struct inotify_event ie;
|
|
|
|
read(ifd, &ie, sizeof(ie));
|
|
|
|
}
|
2014-09-11 21:15:27 +00:00
|
|
|
|
|
|
|
if (lseek(fd, 0, SEEK_END) < 0) {
|
|
|
|
fprintf(stderr, "lseek: %s\n", strerror(errno));
|
|
|
|
} else {
|
|
|
|
snprintf(data, sizeof(data), "[dump dalvik stack %d: %.3fs elapsed]\n",
|
|
|
|
pid, (float)(nanotime() - start) / NANOS_PER_SEC);
|
|
|
|
write(fd, data, strlen(data));
|
|
|
|
}
|
2012-06-07 23:40:01 +00:00
|
|
|
} else if (should_dump_native_traces(data)) {
|
|
|
|
/* dump native process if appropriate */
|
|
|
|
if (lseek(fd, 0, SEEK_END) < 0) {
|
|
|
|
fprintf(stderr, "lseek: %s\n", strerror(errno));
|
|
|
|
} else {
|
2014-09-11 21:15:27 +00:00
|
|
|
int64_t start = nanotime();
|
2012-06-07 23:40:01 +00:00
|
|
|
dump_backtrace_to_file(pid, fd);
|
2014-09-11 21:15:27 +00:00
|
|
|
snprintf(data, sizeof(data), "[dump native stack %d: %.3fs elapsed]\n",
|
|
|
|
pid, (float)(nanotime() - start) / NANOS_PER_SEC);
|
|
|
|
write(fd, data, strlen(data));
|
2012-06-07 23:40:01 +00:00
|
|
|
}
|
2012-03-26 19:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dalvik_found == 0) {
|
|
|
|
fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static char dump_traces_path[PATH_MAX];
|
|
|
|
strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
|
|
|
|
strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
|
|
|
|
if (rename(traces_path, dump_traces_path)) {
|
|
|
|
fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
|
2012-06-07 23:40:01 +00:00
|
|
|
goto error_close_ifd;
|
2012-03-26 19:38:26 +00:00
|
|
|
}
|
2012-06-07 23:40:01 +00:00
|
|
|
result = dump_traces_path;
|
2012-03-26 19:38:26 +00:00
|
|
|
|
|
|
|
/* replace the saved [ANR] traces.txt file */
|
|
|
|
rename(anr_traces_path, traces_path);
|
2012-06-07 23:40:01 +00:00
|
|
|
|
|
|
|
error_close_ifd:
|
|
|
|
close(ifd);
|
|
|
|
error_close_fd:
|
|
|
|
close(fd);
|
|
|
|
return result;
|
2012-03-26 19:38:26 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 22:40:55 +00:00
|
|
|
void dump_route_tables() {
|
|
|
|
const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
|
|
|
|
dump_file("RT_TABLES", RT_TABLES_PATH);
|
|
|
|
FILE* fp = fopen(RT_TABLES_PATH, "r");
|
|
|
|
if (!fp) {
|
|
|
|
printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char table[16];
|
|
|
|
// Each line has an integer (the table number), a space, and a string (the table name). We only
|
|
|
|
// need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
|
|
|
|
// Add a fixed max limit so this doesn't go awry.
|
|
|
|
for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
|
|
|
|
run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
|
|
|
|
run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|