From 7c2d199979222eabb14e8207acd5187cf291431a Mon Sep 17 00:00:00 2001 From: Sberm <1007273067@qq.com> Date: Fri, 22 Mar 2024 23:13:21 +0800 Subject: [PATCH] Minor? --- Makefile | 2 +- src/mem.bpf.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/mem.h | 26 ++++++++++++++++++ src/record.c | 65 ++++++++++++++++++++++++++++++++++++++------ src/record.h | 5 ++++ src/stat.bpf.c | 1 + 6 files changed, 163 insertions(+), 10 deletions(-) create mode 100644 src/mem.bpf.c create mode 100644 src/mem.h diff --git a/Makefile b/Makefile index da20b71..f6e827d 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \ # \_ sberf # bpf.c文件 -BPF_FILE_ := record stat +BPF_FILE_ := record stat mem BPF_FILE := $(addsuffix .bpf.c, $(BPF_FILE_)) SKEL := $(patsubst %.bpf.c, %.skel.h,$(BPF_FILE)) SKEL_BUILT := $(addprefix $(SKEL_DIR)/,$(SKEL)) diff --git a/src/mem.bpf.c b/src/mem.bpf.c new file mode 100644 index 0000000..16e7cbb --- /dev/null +++ b/src/mem.bpf.c @@ -0,0 +1,74 @@ +/*-*- coding:utf-8 -*-│ +│vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2024 Howard Chu │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ + +#include "vmlinux.h" +#include "mem.h" +#include "bpf_util.h" +#include "util.h" + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; + +// specific pid +volatile bool spec_pid = 0; +volatile pid_t pids[MAX_PID] = {0}; + +struct { + __uint(type, BPF_MAP_TYPE_STACK_TRACE); + __type(key, u32); +} stack_map SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, u32); + __type(value, u32); + __uint(max_entries, MAX_ENTRIES); +} mem_usage SEC(".maps"); + +SEC("kprobe") +int mem_profile(void *ctx) +{ + u64 id = bpf_get_current_pid_tgid(); + u32 pid = id >> 32; + + // if to trace only specific pids + if (spec_pid) { + int i; + for (i = 0;i < ARRAY_LEN(pids); i++) { + if (pids[i] == 0) + return 0; + if (pids[i] == pid) + break; + } + /* didn't match through the whole pids array */ + if (i == ARRAY_LEN(pids)) + return 0; + } + + bpf_printk("mem triggered"); + /*u64 zero = 0;*/ + // u64* cnt = bpf_map_lookup_insert(&stat_cnt, &zero, &zero); + + /*if (cnt)*/ + /*__sync_fetch_and_add(cnt, 1);*/ + /*else {*/ + /*bpf_printk("Failed to look up stack sample");*/ + /*return -1;*/ + /*}*/ + return 0; +} diff --git a/src/mem.h b/src/mem.h new file mode 100644 index 0000000..8b9a387 --- /dev/null +++ b/src/mem.h @@ -0,0 +1,26 @@ +/*-*- coding:utf-8 -*-│ +│vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2024 Howard Chu │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ + +#ifndef MEM_H +#define MEM_H + +#define MAX_ENTRIES 10240 +#define MAX_PID 1024 + +#endif diff --git a/src/record.c b/src/record.c index 6462949..9cd7b0f 100644 --- a/src/record.c +++ b/src/record.c @@ -38,6 +38,7 @@ #include "util.h" #include "record.skel.h" #include "stat.skel.h" +#include "mem.skel.h" #include "record.h" #include "stack.h" #include "sym.h" @@ -61,7 +62,7 @@ static struct { char event_names_str[512]; } env = { .freq = 1, - .sample_freq = 3999, + .sample_freq = 69, .no_plot = 0, .pids = "\0", .all_p = 0, @@ -69,14 +70,11 @@ static struct { .event_names_str = "\0", }; -int record_syscall(int argc, char** argv, int index); -int record_tracepoint(int argc, char** argv, int index); -int record_pid(int argc, char** argv, int index); - static struct func_struct record_func[] = { {"-s", record_syscall}, {"-t", record_tracepoint}, {"-p", record_pid}, + {"-m", record_mem}, }; static struct env_struct record_env[] = { @@ -91,6 +89,15 @@ static struct env_struct event_env[] = { {"-p", 1, &env.pids}, {"-s", 1, &env.event_names_str}, {"-t", 1, &env.event_names_str}, + {"-rt", 1, &env.event_names_str}, +}; + +static struct env_struct mem_env[] = { + {"-f", 0, &env.sample_freq}, + {"-np", 4, &env.no_plot}, + {"-a", 4, &env.all_p}, + {"-p", 1, &env.pids}, + {"-fn", 1, &env.svg_file_name}, }; static void signalHandler(int signum) @@ -166,7 +173,7 @@ void print_stack(struct bpf_map *stack_map, struct bpf_map *sample) printf("Collected %d samples\n", sample_num_total); } -int split_env_str() { +int split_event_str() { char *token; size_t index = 0; token = strtok(env.event_names_str, ","); @@ -241,7 +248,7 @@ int record_syscall(int argc, char** argv, int cur) parse_opts_env(argc, argv, cur, event_env, ARRAY_LEN(event_env)); - int event_num = split_env_str(); + int event_num = split_event_str(); printf("recording events: "); for (int i = 0;i < event_num; i++) @@ -289,7 +296,7 @@ int record_tracepoint(int argc, char** argv, int cur) parse_opts_env(argc, argv, cur, event_env, ARRAY_LEN(event_env)); - int event_num = split_env_str(); + int event_num = split_event_str(); printf("recording events: "); for (int i = 0;i < event_num; i++) @@ -353,7 +360,6 @@ int record_pid(int argc, char** argv, int cur) pid_t *pids = skel->bss->pids; size_t num_of_pids = split(env.pids, pids); skel->bss->spec_pid = !env.all_p; // if to record all process(all_p = 1), specific pid(spec_pid) is 0 - /* sberf record 1001 is also legal */ if (!env.all_p && strlen(env.pids) == 0) { @@ -458,3 +464,44 @@ int record_pid(int argc, char** argv, int cur) return err; } +int record_mem(int argc, char** argv, int cur) +{ + struct mem_bpf *skel; + int err = 0; + + parse_opts_env(argc, argv, cur, mem_env, ARRAY_LEN(record_env)); + + skel = mem_bpf__open(); + if (!skel) { + fprintf(stderr, "Failed to open and load record's BPF skeleton\n"); + return 1; + } + + /* pids to trace */ + pid_t *pids = skel->bss->pids; + size_t num_of_pids = split(env.pids, pids); + skel->bss->spec_pid = !env.all_p; // if to record all process(all_p = 1), specific pid(spec_pid) is 0 + + struct bpf_link *link = NULL; + link = bpf_program__attach_ksyscall(skel->progs.mem_profile, "mmap", NULL); + + err = mem_bpf__load(skel); + if (err) { + fprintf(stderr, "Failed to load and verify BPF skeleton\n"); + goto cleanup; + } + + err = mem_bpf__attach(skel); + if (err) { + fprintf(stderr, "Failed to attach BPF skeleton\n"); + goto cleanup; + } + + + sleep(100); + +cleanup: + mem_bpf__destroy(skel); + return err; + return 0; +} diff --git a/src/record.h b/src/record.h index a920c37..a6c4b81 100644 --- a/src/record.h +++ b/src/record.h @@ -32,4 +32,9 @@ struct key_t { char comm[TASK_COMM_LEN]; }; +int record_syscall(int argc, char** argv, int index); +int record_tracepoint(int argc, char** argv, int index); +int record_pid(int argc, char** argv, int index); +int record_mem(int argc, char** argv, int cur); + #endif diff --git a/src/stat.bpf.c b/src/stat.bpf.c index 3adefa2..72c916f 100644 --- a/src/stat.bpf.c +++ b/src/stat.bpf.c @@ -50,6 +50,7 @@ int stat_tracepoint(void *ctx) SEC("ksyscall") int stat_ksyscall(void *ctx) { + bpf_printk("triggered"); u64 zero = 0; u64* cnt = bpf_map_lookup_insert(&stat_cnt, &zero, &zero); if (cnt)