diff options
| author | zy <[email protected]> | 2023-12-12 11:55:44 +0800 |
|---|---|---|
| committer | zy <[email protected]> | 2023-12-12 11:55:44 +0800 |
| commit | 462bdebad6eb2734fd0be4d014bfa2fa67d0ab29 (patch) | |
| tree | 23499c9e763ecbc736d06eedde0335c67d658fb8 /source/ucli/ucli-lib.cc | |
| parent | 636b2df0dd2179969b25d3f62bccd535f219f18e (diff) | |
reformat code
Diffstat (limited to 'source/ucli/ucli-lib.cc')
| -rw-r--r-- | source/ucli/ucli-lib.cc | 387 |
1 files changed, 199 insertions, 188 deletions
diff --git a/source/ucli/ucli-lib.cc b/source/ucli/ucli-lib.cc index e710d86..a9d3e94 100644 --- a/source/ucli/ucli-lib.cc +++ b/source/ucli/ucli-lib.cc @@ -1,13 +1,13 @@ #include <errno.h> #include <fcntl.h> +#include <fstream> #include <stdio.h> #include <sys/ioctl.h> #include <unistd.h> -#include <fstream> #include <assert.h> -#include <stdio_ext.h> #include <ctype.h> +#include <stdio_ext.h> #include <stdlib.h> #include <string.h> @@ -15,21 +15,20 @@ #include "ucli.h" #include "unwind.h" - #define BUF_LEN 4096 -#define WHITESPACE " \t\n\r" +#define WHITESPACE " \t\n\r" #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) -#define streq(a,b) (strcmp((a),(b)) == 0) +#define streq(a, b) (strcmp((a), (b)) == 0) unsigned long run_in_host = 0; using namespace std; class pid_cmdline { - private: +private: std::map<int, std::string> cmdlines; - public: +public: void clear(void); std::string &get_pid_cmdline(int pid); }; @@ -71,14 +70,14 @@ long diag_call_ioctl(unsigned long request, unsigned long arg) { fd = open(DEVICE, O_RDWR, 0); if (fd < 0) { - printf("open %s error,try to open %s\n", DEVICE, DEVICE_BAK); - fd = open(DEVICE_BAK, O_RDWR, 0); - if (fd < 0) { - printf("open %s error!\n", DEVICE_BAK); - return EEXIST; - } else { - printf("open %s success!\n", DEVICE_BAK); - } + printf("open %s error,try to open %s\n", DEVICE, DEVICE_BAK); + fd = open(DEVICE_BAK, O_RDWR, 0); + if (fd < 0) { + printf("open %s error!\n", DEVICE_BAK); + return EEXIST; + } else { + printf("open %s success!\n", DEVICE_BAK); + } } ret = ioctl(fd, request, arg); @@ -94,7 +93,7 @@ err: static int unwind_frame_callback(struct unwind_entry *entry, void *arg) { symbol sym; - std::string symbol; // Use std::string instead of string + std::string symbol; // Use std::string instead of string elf_file file; sym.reset(entry->ip); @@ -135,9 +134,12 @@ void extract_variant_buffer(char *buf, unsigned int len, while (pos < len) { head = (struct diag_variant_buffer_head *)(buf + pos); - if (pos + sizeof(struct diag_variant_buffer_head) >= len) break; - if (head->magic != DIAG_VARIANT_BUFFER_HEAD_MAGIC_SEALED) break; - if (head->len < sizeof(struct diag_variant_buffer_head)) break; + if (pos + sizeof(struct diag_variant_buffer_head) >= len) + break; + if (head->magic != DIAG_VARIANT_BUFFER_HEAD_MAGIC_SEALED) + break; + if (head->len < sizeof(struct diag_variant_buffer_head)) + break; rec = (void *)(buf + pos + sizeof(struct diag_variant_buffer_head)); rec_len = head->len - sizeof(struct diag_variant_buffer_head); @@ -171,29 +173,28 @@ void diag_printf_raw_stack(int pid, int ns_pid, const char *comm, fflush(stdout); } - // 根据 sys_task 取值返回对应的字符串 static const char *sys_task_str(int sys_task) { switch (sys_task) { - case 0: - return "USER_TASK"; - case 1: - return "SYSTEM_TASK"; - case 2: - return "IDLE_TASK"; - default: - return "UNKNOWN_TASK"; + case 0: + return "USER_TASK"; + case 1: + return "SYSTEM_TASK"; + case 2: + return "IDLE_TASK"; + default: + return "UNKNOWN_TASK"; } } static const char *user_mode_str(int user_mode) { switch (user_mode) { - case 1: - return "USER MODE"; - case 0: - return "SYSTEM MODE"; - default: - return "UNKNOWN MODE"; + case 1: + return "USER MODE"; + case 0: + return "SYSTEM MODE"; + default: + return "UNKNOWN MODE"; } } @@ -203,9 +204,9 @@ static const char *user_mode_str(int user_mode) { // #define TASK_UNINTERRUPTIBLE 0x0002 // 等待事件阻塞 不可信号唤醒 // #define __TASK_STOPPED 0x0004 // 暂停执行 // #define __TASK_TRACED 0x0008 //调试状态 -// #define TASK_PARKED 0x0040 // parked 状态,暂停执行 保留在 cpu 但不被调度 -// #define TASK_DEAD 0x0080 // dead -// #define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE) +// #define TASK_PARKED 0x0040 // parked 状态,暂停执行 保留在 cpu +// 但不被调度 #define TASK_DEAD 0x0080 // dead #define +// TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE) // #define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED) // #define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD) // static const char *state_str(int __state){ @@ -215,48 +216,61 @@ static const char *user_mode_str(int user_mode) { #include <string> #include <vector> -#define TASK_RUNNING 0x0000 -#define TASK_INTERRUPTIBLE 0x0001 -#define TASK_UNINTERRUPTIBLE 0x0002 -#define __TASK_STOPPED 0x0004 -#define __TASK_TRACED 0x0008 -#define TASK_PARKED 0x0040 -#define TASK_DEAD 0x0080 -#define TASK_WAKEKILL 0x0100 -#define TASK_NOLOAD 0x0200 -#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE) -#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED) -#define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD) - -std::string state_str(int __state){ - std::vector<std::string> states; - - if (__state == TASK_RUNNING) states.push_back("TASK_RUNNING"); - // if (__state & TASK_RUNNING) states.push_back("TASK_RUNNING"); - if (__state & TASK_INTERRUPTIBLE) states.push_back("TASK_INTERRUPTIBLE"); - if (__state & TASK_UNINTERRUPTIBLE) states.push_back("TASK_UNINTERRUPTIBLE"); - if (__state & __TASK_STOPPED) states.push_back("__TASK_STOPPED"); - if (__state & __TASK_TRACED) states.push_back("__TASK_TRACED"); - if (__state & TASK_PARKED) states.push_back("TASK_PARKED"); - if (__state & TASK_DEAD) states.push_back("TASK_DEAD"); - if (__state == TASK_KILLABLE) states.push_back("TASK_KILLABLE"); - if (__state == TASK_STOPPED) states.push_back("TASK_STOPPED"); - if (__state == TASK_IDLE) states.push_back("TASK_IDLE"); - - std::string result; - for (const auto& state : states) { - if (!result.empty()) result += " | "; - result += state; - } +#define TASK_RUNNING 0x0000 +#define TASK_INTERRUPTIBLE 0x0001 +#define TASK_UNINTERRUPTIBLE 0x0002 +#define __TASK_STOPPED 0x0004 +#define __TASK_TRACED 0x0008 +#define TASK_PARKED 0x0040 +#define TASK_DEAD 0x0080 +#define TASK_WAKEKILL 0x0100 +#define TASK_NOLOAD 0x0200 +#define TASK_KILLABLE (TASK_WAKEKILL | TASK_UNINTERRUPTIBLE) +#define TASK_STOPPED (TASK_WAKEKILL | __TASK_STOPPED) +#define TASK_IDLE (TASK_UNINTERRUPTIBLE | TASK_NOLOAD) + +std::string state_str(int __state) { + std::vector<std::string> states; + + if (__state == TASK_RUNNING) + states.push_back("TASK_RUNNING"); + // if (__state & TASK_RUNNING) states.push_back("TASK_RUNNING"); + if (__state & TASK_INTERRUPTIBLE) + states.push_back("TASK_INTERRUPTIBLE"); + if (__state & TASK_UNINTERRUPTIBLE) + states.push_back("TASK_UNINTERRUPTIBLE"); + if (__state & __TASK_STOPPED) + states.push_back("__TASK_STOPPED"); + if (__state & __TASK_TRACED) + states.push_back("__TASK_TRACED"); + if (__state & TASK_PARKED) + states.push_back("TASK_PARKED"); + if (__state & TASK_DEAD) + states.push_back("TASK_DEAD"); + if (__state == TASK_KILLABLE) + states.push_back("TASK_KILLABLE"); + if (__state == TASK_STOPPED) + states.push_back("TASK_STOPPED"); + if (__state == TASK_IDLE) + states.push_back("TASK_IDLE"); + + std::string result; + for (const auto &state : states) { + if (!result.empty()) + result += " | "; + result += state; + } - return result; + return result; } void printk_task_brief(task_detail *detail) { - printf(" TASK INFO: %s [%s / %s], PID: %d / %d\n", sys_task_str(detail->sys_task) ,detail->cgroup_buf, detail->comm, detail->tgid, - detail->pid); - // printf(" TASK STATE: type: %s, state: %s, state %d\n", sys_task_str(detail->sys_task), state_str(detail->state).c_str(), - // detail->state); + printf(" TASK INFO: %s [%s / %s], PID: %d / %d\n", + sys_task_str(detail->sys_task), detail->cgroup_buf, detail->comm, + detail->tgid, detail->pid); + // printf(" TASK STATE: type: %s, state: %s, state %d\n", + // sys_task_str(detail->sys_task), state_str(detail->state).c_str(), + // detail->state); } void diag_printf_kern_stack(kern_stack_detail *kern_stack) { @@ -284,7 +298,8 @@ void diag_printf_proc_chains(proc_chains_detail *proc_chains) { printf(" PROC CHAINS:\n"); for (i = 0; i < PROCESS_CHAINS_COUNT; i++) { - if (proc_chains->chains[i][0] == 0) break; + if (proc_chains->chains[i][0] == 0) + break; if (proc_chains->full_argv[i] == 0 && detail) { string cmdline = pid_cmdline.get_pid_cmdline(proc_chains->tgid[i]); if (cmdline.length() > 0) @@ -300,155 +315,151 @@ void diag_printf_proc_chains(proc_chains_detail *proc_chains) { } int is_pid_1_has_environ(const char *field) { - bool done = false; - FILE *f = NULL; - int r = 0; - size_t l; + bool done = false; + FILE *f = NULL; + int r = 0; + size_t l; - assert(field); + assert(field); - f = fopen("/proc/1/environ", "re"); - if (!f) - return 0; + f = fopen("/proc/1/environ", "re"); + if (!f) + return 0; - (void) __fsetlocking(f, FSETLOCKING_BYCALLER); + (void)__fsetlocking(f, FSETLOCKING_BYCALLER); - l = strlen(field); + l = strlen(field); - do { - char line[BUF_LEN]; - size_t i; + do { + char line[BUF_LEN]; + size_t i; - for (i = 0; i < sizeof(line)-1; i++) { - int c; + for (i = 0; i < sizeof(line) - 1; i++) { + int c; - c = getc(f); - if ((c == EOF)) { - done = true; - break; - } else if (c == 0) - break; + c = getc(f); + if ((c == EOF)) { + done = true; + break; + } else if (c == 0) + break; - line[i] = c; - } - line[i] = 0; + line[i] = c; + } + line[i] = 0; - if (strneq(line, field, l) && line[l] == '=') { - r = 1; - goto out; - } + if (strneq(line, field, l) && line[l] == '=') { + r = 1; + goto out; + } - } while (!done); + } while (!done); out: - fclose(f); - return r; + fclose(f); + return r; } -enum { - RUN_IN_HOST = 0, - RUN_IN_CONTAINER -}; - -int get_proc_field(const char *filename, const char *pattern, const char *terminator, char **field) { - char status[BUF_LEN] = {0}; - char *t, *f; - size_t len; - int r; +enum { RUN_IN_HOST = 0, RUN_IN_CONTAINER }; - assert(terminator); - assert(filename); - assert(pattern); - assert(field); +int get_proc_field(const char *filename, const char *pattern, + const char *terminator, char **field) { + char status[BUF_LEN] = {0}; + char *t, *f; + size_t len; + int r; - int fd = open(filename, O_RDONLY); - if (fd < 0) - return -errno; + assert(terminator); + assert(filename); + assert(pattern); + assert(field); - r = read(fd, &status, BUF_LEN - 1); - if (r < 0) - return r; + int fd = open(filename, O_RDONLY); + if (fd < 0) + return -errno; - t = status; + r = read(fd, &status, BUF_LEN - 1); + if (r < 0) + return r; - do { - bool pattern_ok; + t = status; - do { - t = strstr(t, pattern); - if (!t) - return -ENOENT; + do { + bool pattern_ok; - /* Check that pattern occurs in beginning of line. */ - pattern_ok = (t == status || t[-1] == '\n'); + do { + t = strstr(t, pattern); + if (!t) + return -ENOENT; - t += strlen(pattern); + /* Check that pattern occurs in beginning of line. */ + pattern_ok = (t == status || t[-1] == '\n'); - } while (!pattern_ok); + t += strlen(pattern); - t += strspn(t, " \t"); - if (!*t) - return -ENOENT; + } while (!pattern_ok); - } while (*t != ':'); + t += strspn(t, " \t"); + if (!*t) + return -ENOENT; - t++; + } while (*t != ':'); + t++; - if (*t) { - t += strspn(t, " \t"); + if (*t) { + t += strspn(t, " \t"); - /* Also skip zeros, because when this is used for - * capabilities, we don't want the zeros. This way the - * same capability set always maps to the same string, - * irrespective of the total capability set size. For - * other numbers it shouldn't matter. */ - t += strspn(t, "0"); - /* Back off one char if there's nothing but whitespace - and zeros */ - if (!*t || isspace(*t)) - t--; - } + /* Also skip zeros, because when this is used for + * capabilities, we don't want the zeros. This way the + * same capability set always maps to the same string, + * irrespective of the total capability set size. For + * other numbers it shouldn't matter. */ + t += strspn(t, "0"); + /* Back off one char if there's nothing but whitespace + and zeros */ + if (!*t || isspace(*t)) + t--; + } - len = strcspn(t, terminator); + len = strcspn(t, terminator); - f = strndup(t, len); - if (!f) - return -ENOMEM; + f = strndup(t, len); + if (!f) + return -ENOMEM; - *field = f; - return 0; + *field = f; + return 0; } static int detect_container_by_pid_2(void) { - char *s = NULL; - int r; - - r = get_proc_field("/proc/2/status", "PPid", WHITESPACE, &s); - if (r >= 0) { - if (streq(s, "0")) - r = RUN_IN_HOST; - else - r = RUN_IN_CONTAINER; - } else if (r == -ENOENT) - r = RUN_IN_CONTAINER; - else { - printf("Failed to read /proc/2/status: %d\n", r); - r = RUN_IN_HOST; - } - - free(s); - return r; + char *s = NULL; + int r; + + r = get_proc_field("/proc/2/status", "PPid", WHITESPACE, &s); + if (r >= 0) { + if (streq(s, "0")) + r = RUN_IN_HOST; + else + r = RUN_IN_CONTAINER; + } else if (r == -ENOENT) + r = RUN_IN_CONTAINER; + else { + printf("Failed to read /proc/2/status: %d\n", r); + r = RUN_IN_HOST; + } + + free(s); + return r; } -int check_in_host(void) -{ - int r; +int check_in_host(void) { + int r; - if (is_pid_1_has_environ("container")) - r = RUN_IN_CONTAINER; - else - r = detect_container_by_pid_2(); + if (is_pid_1_has_environ("container")) + r = RUN_IN_CONTAINER; + else + r = detect_container_by_pid_2(); - return r == RUN_IN_HOST; + return r == RUN_IN_HOST; } |
