ucli no error on vsc

This commit is contained in:
zy
2023-11-24 05:34:51 -05:00
parent 35003bb5be
commit 6aafc05459
3 changed files with 176 additions and 0 deletions

32
source/ucli/ucli-lib.cc Normal file
View File

@@ -0,0 +1,32 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "ucli.h"
/**
* @brief 调用ioctl
*/
long diag_call_ioctl(unsigned long request, unsigned long arg) {
long ret = 0;
int fd;
fd = open(DEVICE, O_RDWR, 0);
if (fd < 0) {
printf("open %s error!\n", DEVICE);
return -EEXIST;
}
ret = ioctl(fd, request, arg);
if (ret < 0) {
printf("call cmd %lx fail, ret is %ld\n", request, ret);
goto err;
}
err:
close(fd);
return ret;
}

119
source/ucli/ucli.cc Normal file
View File

@@ -0,0 +1,119 @@
/**
* various_monitor cli 命令行工具
*/
#include "ucli.h"
#include <getopt.h>
#include <string.h>
#include <cstdio>
static int buffer_extract(void *buf, unsigned int len) {
int *et_type;
variable_monitor_record *vm_record;
variable_monitor_task *tsk_info;
struct load_monitor_cpu_run *cpu_run;
static int seq = 0;
if (len == 0) return 0;
et_type = (int *)buf;
switch (*et_type) {
case 0:
if (len < sizeof(variable_monitor_record)) break;
vm_record = (variable_monitor_record *)buf;
printf("超出阈值:%d\n", vm_record->tv);
for (int i = 0; i < vm_record->threshold_num; i++) {
printf("\t: pid: %d, name: %s, ptr: %p, threshold:%d\n",
vm_record->threshold_record[i].task_id,
vm_record->threshold_record[i]
.name, // Assuming name is a null-terminated string
vm_record->threshold_record[i].ptr,
vm_record->threshold_record[i].threshold);
}
break;
case 1:
if (len < sizeof(variable_monitor_task)) break;
tsk_info = (variable_monitor_task *)buf;
seq++;
printf("##CGROUP:[%s] %d [%03d] 采样命中[%s]\n",
tsk_info->task.cgroup_buf, tsk_info->task.pid, seq,
tsk_info->task.state == 0 ? "R" : "D");
printk_task_brief(&tsk_info->task);
diag_printf_raw_stack(tsk_info->task.tgid, tsk_info->task.container_tgid,
tsk_info->task.comm, &tsk_info->raw_stack);
diag_printf_kern_stack(&tsk_info->kern_stack);
printf("#* 0xffffffffffffff %s (UNKNOWN)\n", tsk_info->task.comm);
diag_printf_proc_chains(&tsk_info->proc_chains);
printf("##\n");
break;
default:
break;
}
return 0;
}
static void do_dump(const char *arg) {
static char variant_buf[50 * 1024 * 1024];
int len;
int ret = 0;
// 参数解析, 未实现/ 源码在 SOURCE/diagnose-tools/params_parse.h |
// params_parse.cc
// struct params_parser parse(arg);
struct diag_ioctl_dump_param dump_param = {
.user_ptr_len = &len,
.user_buf_len = 5 * 1024 * 1024,
.user_buf = variant_buf,
};
ret = diag_call_ioctl(1, (long)&dump_param); // !todo arg -> #define
if (ret == 0) {
buffer_extract(variant_buf, len);
}
}
int main(int argc, char *argv[]) {
static const struct option long_options[] = {
{"help", no_argument, 0, 0},
{"report", no_argument, 0, 0},
// {"pid", required_argument, 0, 0},
{0, 0, 0, 0}};
int c;
if (argc <= 1) {
// do something default
return 0;
}
while (1) {
int option_index = -1;
c = getopt_long_only(argc, argv, "", long_options, &option_index);
if (c == -1) {
break;
}
switch (option_index) {
case 0: // help
// usage_pupil();
break;
case 1: // report
do_dump(optarg ? optarg : "");
break;
default:
// usage_pupil();
break;
}
}
return 0;
}

25
source/ucli/ucli.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef UAPI_H
#define UAPI_H
#include <cstddef> // size_t
#include "../module/monitor_trace.h"
#define DEVICE "/dev/variable_monitor"
struct diag_ioctl_dump_param {
int *user_ptr_len;
size_t user_buf_len;
void *user_buf;
};
long diag_call_ioctl(unsigned long request, unsigned long arg);
// all print fun
void printk_task_brief(task_detail *detail);
void diag_printf_raw_stack(int pid, int ns_pid, const char *comm,
raw_stack_detail *raw_stack);
void diag_printf_kern_stack(kern_stack_detail *kern_stack);
void diag_printf_proc_chains(proc_chains_detail *proc_chains);
#endif /* UAPI_H */