~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/trace_event_kern.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/ptrace.h>
  8 #include <uapi/linux/bpf.h>
  9 #include <uapi/linux/bpf_perf_event.h>
 10 #include <uapi/linux/perf_event.h>
 11 #include <bpf/bpf_helpers.h>
 12 #include <bpf/bpf_tracing.h>
 13 
 14 struct key_t {
 15         char comm[TASK_COMM_LEN];
 16         u32 kernstack;
 17         u32 userstack;
 18 };
 19 
 20 struct {
 21         __uint(type, BPF_MAP_TYPE_HASH);
 22         __type(key, struct key_t);
 23         __type(value, u64);
 24         __uint(max_entries, 10000);
 25 } counts SEC(".maps");
 26 
 27 struct {
 28         __uint(type, BPF_MAP_TYPE_STACK_TRACE);
 29         __uint(key_size, sizeof(u32));
 30         __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
 31         __uint(max_entries, 10000);
 32 } stackmap SEC(".maps");
 33 
 34 #define KERN_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
 35 #define USER_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK)
 36 
 37 SEC("perf_event")
 38 int bpf_prog1(struct bpf_perf_event_data *ctx)
 39 {
 40         char time_fmt1[] = "Time Enabled: %llu, Time Running: %llu";
 41         char time_fmt2[] = "Get Time Failed, ErrCode: %d";
 42         char addr_fmt[] = "Address recorded on event: %llx";
 43         char fmt[] = "CPU-%d period %lld ip %llx";
 44         u32 cpu = bpf_get_smp_processor_id();
 45         struct bpf_perf_event_value value_buf;
 46         struct key_t key;
 47         u64 *val, one = 1;
 48         int ret;
 49 
 50         if (ctx->sample_period < 10000)
 51                 /* ignore warmup */
 52                 return 0;
 53         bpf_get_current_comm(&key.comm, sizeof(key.comm));
 54         key.kernstack = bpf_get_stackid(ctx, &stackmap, KERN_STACKID_FLAGS);
 55         key.userstack = bpf_get_stackid(ctx, &stackmap, USER_STACKID_FLAGS);
 56         if ((int)key.kernstack < 0 && (int)key.userstack < 0) {
 57                 bpf_trace_printk(fmt, sizeof(fmt), cpu, ctx->sample_period,
 58                                  PT_REGS_IP(&ctx->regs));
 59                 return 0;
 60         }
 61 
 62         ret = bpf_perf_prog_read_value(ctx, (void *)&value_buf, sizeof(struct bpf_perf_event_value));
 63         if (!ret)
 64           bpf_trace_printk(time_fmt1, sizeof(time_fmt1), value_buf.enabled, value_buf.running);
 65         else
 66           bpf_trace_printk(time_fmt2, sizeof(time_fmt2), ret);
 67 
 68         if (ctx->addr != 0)
 69           bpf_trace_printk(addr_fmt, sizeof(addr_fmt), ctx->addr);
 70 
 71         val = bpf_map_lookup_elem(&counts, &key);
 72         if (val)
 73                 (*val)++;
 74         else
 75                 bpf_map_update_elem(&counts, &key, &one, BPF_NOEXIST);
 76         return 0;
 77 }
 78 
 79 char _license[] SEC("license") = "GPL";
 80 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php