summaryrefslogtreecommitdiff
path: root/source/module/monitor_proc.c
blob: 7678e9786ab60fb2cfab48abb49e324dab16782a (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
#include "monitor_proc.h"

#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/types.h>
#include <linux/uaccess.h>

const char* proc_dir = "variable_monitor";
int def_interval_ns = DEFAULT_INTERVAL_NS;
int dump_reset_sec = DEFAULT_DUMP_RESET_SEC;
int sample_all = DEFAULT_SAMPLE_ALL;
int stack_capture_mode = STACK_CAPTURE_AGGREGATE;

static ssize_t read_proc(struct file *file, char __user *buf, size_t count,
                         loff_t *offset, int *var) {
  char temp_buf[32];
  size_t len = sprintf(temp_buf, "%d\n", *var);

  return simple_read_from_buffer(buf, count, offset, temp_buf, len);
}

static ssize_t write_proc(struct file *file, const char __user *buf,
                          size_t count, loff_t *offset, int *var) {
  char temp_buf[32];
  if (count > sizeof(temp_buf) - 1)
    return -EINVAL;

  if (copy_from_user(temp_buf, buf, count))
    return -EFAULT;

  temp_buf[count] = '\0';
  sscanf(temp_buf, "%d", var);

  return count;
}

static ssize_t read_proc_def_interval_ns(struct file *file, char __user *buf,
                                         size_t count, loff_t *offset) {
  return read_proc(file, buf, count, offset, &def_interval_ns);
}

static ssize_t read_proc_dump_reset_sec(struct file *file, char __user *buf,
                                        size_t count, loff_t *offset) {
  return read_proc(file, buf, count, offset, &dump_reset_sec);
}

static ssize_t write_proc_def_interval_ns(struct file *file,
                                          const char __user *buf, size_t count,
                                          loff_t *offset) {
  return write_proc(file, buf, count, offset, &def_interval_ns);
}

static ssize_t write_proc_dump_reset_sec(struct file *file,
                                         const char __user *buf, size_t count,
                                         loff_t *offset) {
  return write_proc(file, buf, count, offset, &dump_reset_sec);
}

static ssize_t read_proc_sample_all(struct file *file, char __user *buf,
                                    size_t count, loff_t *offset) {
  return read_proc(file, buf, count, offset, &sample_all);
}
static ssize_t write_proc_sample_all(struct file *file,
                                     const char __user *buf, size_t count,
                                     loff_t *offset) {
  return write_proc(file, buf, count, offset, &sample_all);
}
static ssize_t read_stack_capture_mode(struct file *file, char __user *buf,
                                       size_t count, loff_t *offset) {
  return read_proc(file, buf, count, offset, &stack_capture_mode);
}
static ssize_t write_stack_capture_mode(struct file *file,
                                        const char __user *buf, size_t count,
                                        loff_t *offset) {
  return write_proc(file, buf, count, offset, &stack_capture_mode);
}

static const struct proc_ops proc_def_interval_ns_ops = {
    .proc_read = read_proc_def_interval_ns,
    .proc_write = write_proc_def_interval_ns,
};

static const struct proc_ops proc_dump_reset_sec_ops = {
    .proc_read = read_proc_dump_reset_sec,
    .proc_write = write_proc_dump_reset_sec,
};

static const struct proc_ops proc_sample_all_ops = {
    .proc_read = read_proc_sample_all,
    .proc_write = write_proc_sample_all,
};

static const struct proc_ops stack_capture_mode_ops = {
    .proc_read = read_stack_capture_mode,
    .proc_write = write_stack_capture_mode,
};

int monitor_proc_init(void) {
  struct proc_dir_entry *dir;

  dir = proc_mkdir(proc_dir, NULL);
  if (!dir) {
    pr_err("variable_monitor: failed to create /proc directory\n");
    return -ENOMEM;
  }

  proc_create("def_interval_ns", 0666, dir, &proc_def_interval_ns_ops);
  proc_create("dump_reset_sec", 0666, dir, &proc_dump_reset_sec_ops);
  proc_create("sample_all", 0666, dir, &proc_sample_all_ops);
  proc_create("stack_capture_mode", 0666, dir, &stack_capture_mode_ops);

  return 0;
}

int monitor_proc_exit(void) {
  // remove_proc_entry("def_interval_ns", NULL);
  // remove_proc_entry("dump_reset_sec", NULL);
  remove_proc_subtree(proc_dir, NULL);
  return 0;
}