Alexei Starovoitov | e3edfde | 2016-04-06 18:43:31 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2016 Facebook |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | */ |
| 7 | #include <linux/version.h> |
| 8 | #include <linux/ptrace.h> |
Yafang Shao | d068144 | 2022-01-19 18:08:33 -0800 | [diff] [blame] | 9 | #include <linux/sched.h> |
Alexei Starovoitov | e3edfde | 2016-04-06 18:43:31 -0700 | [diff] [blame] | 10 | #include <uapi/linux/bpf.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 11 | #include <bpf/bpf_helpers.h> |
| 12 | #include <bpf/bpf_tracing.h> |
Alexei Starovoitov | e3edfde | 2016-04-06 18:43:31 -0700 | [diff] [blame] | 13 | |
Ilya Leoshkevich | e4d9c23 | 2020-07-20 13:48:06 +0200 | [diff] [blame] | 14 | #define _(P) \ |
| 15 | ({ \ |
| 16 | typeof(P) val = 0; \ |
| 17 | bpf_probe_read_kernel(&val, sizeof(val), &(P)); \ |
| 18 | val; \ |
| 19 | }) |
Alexei Starovoitov | e3edfde | 2016-04-06 18:43:31 -0700 | [diff] [blame] | 20 | |
| 21 | SEC("kprobe/__set_task_comm") |
| 22 | int prog(struct pt_regs *ctx) |
| 23 | { |
| 24 | struct signal_struct *signal; |
| 25 | struct task_struct *tsk; |
Yafang Shao | d068144 | 2022-01-19 18:08:33 -0800 | [diff] [blame] | 26 | char oldcomm[TASK_COMM_LEN] = {}; |
| 27 | char newcomm[TASK_COMM_LEN] = {}; |
Alexei Starovoitov | e3edfde | 2016-04-06 18:43:31 -0700 | [diff] [blame] | 28 | u16 oom_score_adj; |
| 29 | u32 pid; |
| 30 | |
| 31 | tsk = (void *)PT_REGS_PARM1(ctx); |
| 32 | |
| 33 | pid = _(tsk->pid); |
Yafang Shao | d068144 | 2022-01-19 18:08:33 -0800 | [diff] [blame] | 34 | bpf_probe_read_kernel_str(oldcomm, sizeof(oldcomm), &tsk->comm); |
| 35 | bpf_probe_read_kernel_str(newcomm, sizeof(newcomm), |
| 36 | (void *)PT_REGS_PARM2(ctx)); |
Alexei Starovoitov | e3edfde | 2016-04-06 18:43:31 -0700 | [diff] [blame] | 37 | signal = _(tsk->signal); |
| 38 | oom_score_adj = _(signal->oom_score_adj); |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | SEC("kprobe/urandom_read") |
| 43 | int prog2(struct pt_regs *ctx) |
| 44 | { |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | char _license[] SEC("license") = "GPL"; |
| 49 | u32 _version SEC("version") = LINUX_VERSION_CODE; |