Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v5.13 78 lines 2.0 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2/* Copyright (c) 2021 Google LLC. */ 3 4#include <linux/bpf.h> 5#include <bpf/bpf_helpers.h> 6#include <bpf/bpf_tracing.h> 7 8__u32 pid = 0; 9 10char num_out[64] = {}; 11long num_ret = 0; 12 13char ip_out[64] = {}; 14long ip_ret = 0; 15 16char sym_out[64] = {}; 17long sym_ret = 0; 18 19char addr_out[64] = {}; 20long addr_ret = 0; 21 22char str_out[64] = {}; 23long str_ret = 0; 24 25char over_out[6] = {}; 26long over_ret = 0; 27 28char pad_out[10] = {}; 29long pad_ret = 0; 30 31char noarg_out[64] = {}; 32long noarg_ret = 0; 33 34long nobuf_ret = 0; 35 36extern const void schedule __ksym; 37 38SEC("raw_tp/sys_enter") 39int handler(const void *ctx) 40{ 41 /* Convenient values to pretty-print */ 42 const __u8 ex_ipv4[] = {127, 0, 0, 1}; 43 const __u8 ex_ipv6[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; 44 static const char str1[] = "str1"; 45 static const char longstr[] = "longstr"; 46 47 if ((int)bpf_get_current_pid_tgid() != pid) 48 return 0; 49 50 /* Integer types */ 51 num_ret = BPF_SNPRINTF(num_out, sizeof(num_out), 52 "%d %u %x %li %llu %lX", 53 -8, 9, 150, -424242, 1337, 0xDABBAD00); 54 /* IP addresses */ 55 ip_ret = BPF_SNPRINTF(ip_out, sizeof(ip_out), "%pi4 %pI6", 56 &ex_ipv4, &ex_ipv6); 57 /* Symbol lookup formatting */ 58 sym_ret = BPF_SNPRINTF(sym_out, sizeof(sym_out), "%ps %pS %pB", 59 &schedule, &schedule, &schedule); 60 /* Kernel pointers */ 61 addr_ret = BPF_SNPRINTF(addr_out, sizeof(addr_out), "%pK %px %p", 62 0, 0xFFFF00000ADD4E55, 0xFFFF00000ADD4E55); 63 /* Strings embedding */ 64 str_ret = BPF_SNPRINTF(str_out, sizeof(str_out), "%s %+05s", 65 str1, longstr); 66 /* Overflow */ 67 over_ret = BPF_SNPRINTF(over_out, sizeof(over_out), "%%overflow"); 68 /* Padding of fixed width numbers */ 69 pad_ret = BPF_SNPRINTF(pad_out, sizeof(pad_out), "%5d %0900000X", 4, 4); 70 /* No args */ 71 noarg_ret = BPF_SNPRINTF(noarg_out, sizeof(noarg_out), "simple case"); 72 /* No buffer */ 73 nobuf_ret = BPF_SNPRINTF(NULL, 0, "only interested in length %d", 60); 74 75 return 0; 76} 77 78char _license[] SEC("license") = "GPL";