From 28bcb60c855d285db02217789a777209b242607d Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Wed, 15 Mar 2017 16:43:03 +0100 Subject: [PATCH] crash-pipe: Drop all reporting functionality Reporting is handled by crash-stack. Change-Id: I6066c5a414fd2e98efbcc0ddc57f2f967cbfb859 --- src/crash-pipe/crash-pipe.c | 384 +------------------------------------------- 1 file changed, 1 insertion(+), 383 deletions(-) diff --git a/src/crash-pipe/crash-pipe.c b/src/crash-pipe/crash-pipe.c index 0dbea20..4d05ce2 100644 --- a/src/crash-pipe/crash-pipe.c +++ b/src/crash-pipe/crash-pipe.c @@ -28,37 +28,18 @@ #include #include #include -#include #include #include #include #include -#define NELEMS(arr) (sizeof(arr)/sizeof(arr[0])) -#define BUF_SIZE (BUFSIZ) -#define HEXA 16 -#define PERM_LEN 5 -#define ADDR_LEN 16 -#define STR_ANONY "[anony]" -#define STR_ANONY_LEN 8 - enum { OPT_HELP, - OPT_REPORT, OPT_SAVE_CORE, }; -struct addr_node { - long *startaddr; - long *endaddr; - char perm[5]; - char *fpath; - struct addr_node *next; -}; - const struct option opts[] = { { "help", no_argument, 0, OPT_HELP }, - { "report", no_argument, 0, OPT_REPORT }, { "save-core", required_argument, 0, OPT_SAVE_CORE }, { 0, 0, 0, 0 } }; @@ -67,363 +48,7 @@ static char *argv0 = ""; static void usage(void) { - fprintf(stderr, "usage: %s [--help] [--save-core FILE_NAME] [--report] PID UID GID SIGNAL DUMPTIME EXE\n", - argv0); -} - -/* read file to buffer - * - * Returns success only if whole file has been read (requires big - * enough buffer). - */ -static int procfs_read_fileline(const char *pid, const char *filename, char *outbuf, int outsize) -{ - char *path = NULL; - int fd; - int n; - int ret = 0; - - if (!(outsize > 0)) - return 0; - - if (asprintf(&path, "/proc/%s/%s", pid, filename) == -1) - return -ENOMEM; - - fd = open(path, O_RDONLY); - if (fd == -1) { - ret = -errno; - goto err; - } - - /* XXX we are really assuming here that one read is enough */ - ret = read(fd, outbuf, outsize); - if (ret == -1 || ret == outsize /* no place for \0 */) { - ret = -errno; - goto err; - } - - n = ret; - outbuf[n] = 0; - for (; n > 0; --n) { - if (outbuf[n] == '\n') - outbuf[n] = 0; - } - - close(fd); - - free(path); - return ret; - -err: - if (fd >= 0) - close(fd); - free(path); - *outbuf = 0; - return ret; -} - -void print_multiline(char *buf, int buf_size) -{ - int i; - int pos; - - for (pos = i = 0; buf[pos] && pos < buf_size; ++i, pos += strlen(buf + pos) + 1) - printf("%21d: %s\n", i, buf + pos); -} - -static char *fgets_fd(char *str, int len, int fd) -{ - char ch; - register char *cs; - int num = 0; - - cs = str; - while (--len > 0 && (num = read(fd, &ch, 1) > 0)) { - if ((*cs++ = ch) == '\n') - break; - } - *cs = '\0'; - return (num == 0 && cs == str) ? NULL : str; -} - -static void meminfo_report(const char *pidstr) -{ - char infoname[BUF_SIZE]; - char memsize[BUF_SIZE]; - char linebuf[BUF_SIZE]; - char file_path[PATH_MAX]; - int fd; - - printf("\nMemory information\n"); - - if ((fd = open("/proc/meminfo", O_RDONLY)) < 0) { - fprintf(stderr, "[crash-pipe] can't open %s\n", file_path); - } else { - while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) { - sscanf(linebuf, "%s %s %*s", infoname, memsize); - if (strcmp("MemTotal:", infoname) == 0) { - printf("%s %8s KB\n", infoname, memsize); - } else if (strcmp("MemFree:", infoname) == 0) { - printf("%s %8s KB\n", infoname, memsize); - } else if (strcmp("Buffers:", infoname) == 0) { - printf("%s %8s KB\n", infoname, memsize); - } else if (strcmp("Cached:", infoname) == 0) { - printf("%s %8s KB\n", infoname, memsize); - break; - } - } - close(fd); - } - - snprintf(file_path, PATH_MAX, "/proc/%s/status", pidstr); - if ((fd = open(file_path, O_RDONLY)) < 0) { - fprintf(stderr, "[crash-pipe]can't open /proc/meminfo\n"); - } else { - while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) { - sscanf(linebuf, "%s %s %*s", infoname, memsize); - if (strcmp("VmPeak:", infoname) == 0) { - printf("%s %8s KB\n", infoname, - memsize); - } else if (strcmp("VmSize:", infoname) == 0) { - printf("%s %8s KB\n", infoname, - memsize); - } else if (strcmp("VmLck:", infoname) == 0) { - printf("%s %8s KB\n", infoname, - memsize); - } else if (strcmp("VmPin:", infoname) == 0) { - printf("%s %8s KB\n", infoname, - memsize); - } else if (strcmp("VmHWM:", infoname) == 0) { - printf("%s %8s KB\n", - infoname, memsize); - } else if (strcmp("VmRSS:", infoname) == 0) { - printf("%s %8s KB\n", - infoname, memsize); - } else if (strcmp("VmData:", infoname) == 0) { - printf("%s %8s KB\n", - infoname, memsize); - } else if (strcmp("VmStk:", infoname) == 0) { - printf("%s %8s KB\n", - infoname, memsize); - } else if (strcmp("VmExe:", infoname) == 0) { - printf("%s %8s KB\n", - infoname, memsize); - } else if (strcmp("VmLib:", infoname) == 0) { - printf("%s %8s KB\n", - infoname, memsize); - } else if (strcmp("VmPTE:", infoname) == 0) { - printf("%s %8s KB\n", - infoname, memsize); - } else if (strcmp("VmSwap:", infoname) == 0) { - printf("%s %8s KB\n", - infoname, memsize); - break; - } - } - close(fd); - } -} - -static struct addr_node *get_addr_list_from_maps(int fd) -{ - int fpath_len, result; - long *saddr; - long *eaddr; - char perm[PERM_LEN]; - char path[PATH_MAX]; - char addr[ADDR_LEN * 2]; - char linebuf[BUF_SIZE]; - struct addr_node *head = NULL; - struct addr_node *tail = NULL; - struct addr_node *t_node = NULL; - - /* parsing the maps to get executable code address */ - while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) { - memset(path, 0, PATH_MAX); - result = sscanf(linebuf, "%s %s %*s %*s %*s %s ", addr, perm, path); - if (result < 0) - continue; - perm[PERM_LEN - 1] = 0; - /* rwxp */ - if ((perm[2] == 'x' && path[0] == '/') || - (perm[1] == 'w' && path[0] != '/')) - { - /* add addr node to list */ - addr[ADDR_LEN] = 0; - saddr = (long *)strtoul(addr, NULL, HEXA); - /* ffff0000-ffff1000 */ - eaddr = (long *)strtoul(&addr[ADDR_LEN + 1], NULL, HEXA); - /* make node and attach to the list */ - t_node = (struct addr_node *)mmap(0, sizeof(struct addr_node), - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (t_node == NULL) { - fprintf(stderr, "error : mmap\n"); - return NULL; - } - memcpy(t_node->perm, perm, PERM_LEN); - t_node->startaddr = saddr; - t_node->endaddr = eaddr; - t_node->fpath = NULL; - fpath_len = strlen(path); - if (fpath_len > 0) { - t_node->fpath = (char *)mmap(0, fpath_len + 1, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - memset(t_node->fpath, 0, fpath_len + 1); - memcpy(t_node->fpath, path, fpath_len); - } else { - t_node->fpath = (char *)mmap(0, STR_ANONY_LEN, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - memset(t_node->fpath, 0, STR_ANONY_LEN); - memcpy(t_node->fpath, STR_ANONY, STR_ANONY_LEN); - } - t_node->next = NULL; - if (head == NULL) { - head = t_node; - tail = t_node; - } else { - tail->next = t_node; - tail = t_node; - } - } - } - return head; -} - -static void free_all_nodes(struct addr_node *start) -{ - struct addr_node *t_node, *n_node; - int fpath_len; - - if (start == NULL) - return; - t_node = start; - n_node = t_node->next; - while (t_node) { - if (t_node->fpath != NULL) { - fpath_len = strlen(t_node->fpath); - munmap(t_node->fpath, fpath_len + 1); - } - munmap(t_node, sizeof(struct addr_node)); - if (n_node == NULL) - break; - t_node = n_node; - n_node = n_node->next; - } -} - -static void print_node_to_file(struct addr_node *start) -{ - struct addr_node *t_node; - - t_node = start; - printf("\n%s\n", "Maps Information"); - while (t_node) { - if (!strncmp(STR_ANONY, t_node->fpath, STR_ANONY_LEN)) { - t_node = t_node->next; - } else { - printf( "%16lx %16lx %s %s\n", - (unsigned long)t_node->startaddr, - (unsigned long)t_node->endaddr, - t_node->perm, t_node->fpath); - t_node = t_node->next; - } - } - printf("%s\n", "End of Maps Information"); -} - -static void maps_report(const char *pidstr) -{ - char file_path[PATH_MAX]; - struct addr_node *head = NULL; - int fd; - - /* open maps file */ - snprintf(file_path, PATH_MAX, "/proc/%s/maps", pidstr); - - if ((fd = open(file_path, O_RDONLY)) < 0) { - fprintf(stderr, "[crash-pipe]can't open %s\n", file_path); - } else { - /* parsing the maps to get code segment address*/ - head = get_addr_list_from_maps(fd); - close(fd); - } - if (head != NULL) { - /* print maps information */ - print_node_to_file(head); - free_all_nodes(head); - } -} - -static void report(int argc, char *argv[]) -{ - const char *pidstr = argv[0]; - const char *uidstr = argv[1]; - const char *gidstr = argv[2]; - const char *sigstr = argv[3]; - const char *timestr = argv[4]; - const char *exestr = argv[5]; - static const struct { - char *file; - char *desc; - int is_multiline; - } proc_filedesc[] = { - { "comm", "Comm", 0}, - { "cgroup", "CGroup", 0 }, - { "attr/current", "MAC Label", 0 }, - { "oom_score", "OOM Score", 0 }, - { "oom_score_adj", "OOM Score Adj", 0 }, - { "cmdline", "Cmdline", 1 }, - { "environ", "Environment", 1 } - }; - - int i; - int n; - -#define PROC_READ_MAX 16384 /* 4 pages should be enough for any process */ - char proc_readbuf[PROC_READ_MAX]; - char exe_link[PATH_MAX]; - char exe_file[PATH_MAX]; - - snprintf(exe_link, PATH_MAX, "/proc/%s/exe", pidstr); - if (readlink(exe_link, exe_file, PATH_MAX) > 0) { - printf("Executable File Path: %s\n", exe_file); - } - - meminfo_report(pidstr); - - maps_report(pidstr); - - printf(" - passed from kernel -\n" - "%16s: %s\n" - "%16s: %s\n" - "%16s: %s\n" - "%16s: %s\n" - "%16s: %s\n" - "%16s: %s\n\n", - "PID", pidstr, - "UID", uidstr, - "GID", gidstr, - "Signal number", sigstr, - "Timestamp", timestr, - "Executable", exestr); - - printf(" - procfs information -\n"); - - for (i = 0; i < NELEMS(proc_filedesc); ++i) { - n = procfs_read_fileline(pidstr, proc_filedesc[i].file, proc_readbuf, sizeof(proc_readbuf)); - if (n < 0) - snprintf(proc_readbuf, sizeof(proc_readbuf), "Error (%d)", -n); - - if (n < 0 || proc_filedesc[i].is_multiline == 0) - printf("%16s: %s\n", proc_filedesc[i].desc, proc_readbuf); - else { - printf("%16s:\n", proc_filedesc[i].desc); - print_multiline(proc_readbuf, n); - } - } + fprintf(stderr, "usage: %s [--help] --save-core FILE_NAME\n", argv0); } static int copy_fd_simple(int in, int out) @@ -491,11 +116,9 @@ static int save_core(const char *core_path) return r; } - int main(int argc, char *argv[]) { int c; - int opt_report = 0; char *opt_save_core = NULL; int ret = 1; @@ -508,8 +131,6 @@ int main(int argc, char *argv[]) if (c == OPT_HELP) { usage(); exit(EXIT_SUCCESS); - } else if (c == OPT_REPORT) { - opt_report = 1; } else if (c == OPT_SAVE_CORE) { opt_save_core = strdup(optarg); if (!opt_save_core) { @@ -522,9 +143,6 @@ int main(int argc, char *argv[]) argc -= optind; argv += optind; - if (opt_report) - report(argc, argv); - if (opt_save_core) ret = save_core(opt_save_core); -- 2.7.4