summaryrefslogtreecommitdiff
path: root/source/ucli/ucli.h
blob: db123765cc659665529bbd164f53f0d4cf187aca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef UAPI_H
#define UAPI_H

#include <asm/ptrace.h> // struct pt_regs
#include <sys/types.h>

#include <cstddef> // size_t
#include <stdlib.h>
#include <string>

// ioctl
#define IOCTL_WATCH_VARIABLE 0
#define IOCTL_DUMP_LOG 1
#define IOCTL_MAGIC_NUMBER 'k'
#define IOCTL_PID _IOWR(IOCTL_MAGIC_NUMBER, 2, int)
#define IOCTL_TGID _IOWR(IOCTL_MAGIC_NUMBER, 3, int)
#define IOCTL_DUMP_LOG_SA _IOWR(IOCTL_MAGIC_NUMBER, 4, int)

// dump type
#define VARIABLE_MONITOR_RECORD_TYPE 0x0
#define VARIABLE_MONITOR_TASK_TYPE 0x1
#define VARIABLE_MONITOR_TASK_TYPE_SYSTEM 0x2

#define DEVICE "/dev/variable_monitor"
#define DEVICE_BAK "/host/dev/variable_monitor"

#define CGROUP_NAME_LEN 32 // max length of cgroup name
#define TASK_COMM_LEN 16   // max length of task name

#define BACKTRACE_DEPTH 30 // max depth of backtrace

#define PROCESS_CHAINS_COUNT 10 // max count of process chains
#define PROCESS_ARGV_LEN 128    // max length of process argv

#define MAX_NAME_LEN (127)       // max name length
#define TIMER_MAX_WATCH_NUM (32) // A timer max watch number at once time

#define VARIABLE_MONITOR_BUFFER_SIZE 256 * 1024 * 1024 // 256MB
#define STAND_ALONE_BUFFER_SIZE 50 * 1024 * 1024       // 50 MB

#define DIAG_USER_STACK_SIZE (16 * 1024)

extern unsigned long run_in_host;

typedef struct {
  pid_t task_id;               // current process id
  char name[MAX_NAME_LEN + 1]; // name
  void *ptr;                   // virtual address
  long long threshold;         // threshold value
  long long true_value;        // target true value
} threshold;

typedef struct {
  int et_type;
  unsigned long id;
  unsigned long long tv;
  int threshold_over_count;
  threshold threshold_record[TIMER_MAX_WATCH_NUM];
} variable_monitor_record;

typedef struct {
  char cgroup_buf[CGROUP_NAME_LEN];
  char cgroup_cpuset[CGROUP_NAME_LEN];
  int pid;
  int tgid;
  int container_pid;
  int container_tgid;
  long state;
  int task_type;
  unsigned long syscallno;
  /**
   * 0->user 1->sys 2->idle
   */
  unsigned long sys_task;
  /**
   * 1->user mode 0->sys mode -1->unknown
   */
  unsigned long user_mode;
  char comm[TASK_COMM_LEN];
} task_detail;

typedef struct {
  unsigned long stack[BACKTRACE_DEPTH];
} kern_stack_detail;

// typedef struct {
//   struct pt_regs regs;
//   unsigned long ip;
//   unsigned long bp;
//   unsigned long sp;
//   unsigned long stack[BACKTRACE_DEPTH];
// } user_stack_detail;

typedef struct {
  struct pt_regs regs;
  unsigned long ip;
  unsigned long bp;
  unsigned long sp;
  unsigned long stack_size;
  unsigned long stack[DIAG_USER_STACK_SIZE / sizeof(unsigned long)];
} raw_stack_detail;

typedef struct {
  unsigned int full_argv[PROCESS_CHAINS_COUNT];        //
  char chains[PROCESS_CHAINS_COUNT][PROCESS_ARGV_LEN]; // process chains argv
  unsigned int tgid[PROCESS_CHAINS_COUNT];             // process chains tgid
} proc_chains_detail;

// most important struct
typedef struct {
  int et_type;
  unsigned long id;
  unsigned long long tv;
  task_detail task; // brief
  // user_stack_detail user_stack;    // user stack
  kern_stack_detail kern_stack;   // kernel stack
  proc_chains_detail proc_chains; // process chains argv
  raw_stack_detail raw_stack;
} variable_monitor_task;

typedef struct {
  int et_type;
  unsigned long id;
  unsigned long long tv;
  task_detail task;             // brief
  kern_stack_detail kern_stack; // kernel stack
} variable_monitor_task_system;

#define DIAG_VARIANT_BUFFER_HEAD_MAGIC_SEALED 197612031122
#define DIAG_VARIANT_BUFFER_HEAD_MAGIC_UNSEALED 197612031234

struct diag_variant_buffer_head {
  unsigned long magic;
  unsigned long len;
};
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);

void extract_variant_buffer(char *buf, unsigned int len,
                            int (*func)(void *, unsigned int, void *),
                            void *arg);

extern unsigned long run_in_host;
int check_in_host(void);

// 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 */