• Wang Nan's avatar
    perf trace: Print content of bpf-output event · 1d6c9407
    Wang Nan authored
    With this patch the contend of BPF output event is printed by
    'perf trace'. For example:
    
     # ./perf trace -a --ev bpf-output/no-inherit,name=evt/ \
                       --ev ./test_bpf_trace.c/map:channel.event=evt/ \
                       usleep 100000
      ...
        1.787 ( 0.004 ms): usleep/3832 nanosleep(rqtp: 0x7ffc78b18980                                        ) ...
        1.787 (         ): evt:Raise a BPF event!..)
        1.788 (         ): perf_bpf_probe:func_begin:(ffffffff810e97d0))
      ...
      101.866 (87.038 ms): gmain/1654 poll(ufds: 0x7f57a80008c0, nfds: 2, timeout_msecs: 1000               ) ...
      101.866 (         ): evt:Raise a BPF event!..)
      101.867 (         ): perf_bpf_probe:func_end:(ffffffff810e97d0 <- ffffffff81796173))
      101.869 (100.087 ms): usleep/3832  ... [continued]: nanosleep()) = 0
      ...
    
     (There is an extra ')' at the end of several lines. However, it is
      another problem, unrelated to this commit.)
    
    Where test_bpf_trace.c is:
    
      /************************ BEGIN **************************/
      #include <uapi/linux/bpf.h>
      struct bpf_map_def {
            unsigned int type;
            unsigned int key_size;
            unsigned int value_size;
            unsigned int max_entries;
      };
      #define SEC(NAME) __attribute__((section(NAME), used))
      static u64 (*ktime_get_ns)(void) =
            (void *)BPF_FUNC_ktime_get_ns;
      static int (*trace_printk)(const char *fmt, int fmt_size, ...) =
            (void *)BPF_FUNC_trace_printk;
      static int (*get_smp_processor_id)(void) =
            (void *)BPF_FUNC_get_smp_processor_id;
      static int (*perf_event_output)(void *, struct bpf_map_def *, int, void *, unsigned long) =
            (void *)BPF_FUNC_perf_event_output;
    
      struct bpf_map_def SEC("maps") channel = {
            .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
            .key_size = sizeof(int),
            .value_size = sizeof(u32),
            .max_entries = __NR_CPUS__,
      };
    
      static inline int __attribute__((always_inline))
      func(void *ctx, int type)
      {
    	char output_str[] = "Raise a BPF event!";
    	char err_str[] = "BAD %d\n";
    	int err;
    
            err = perf_event_output(ctx, &channel, get_smp_processor_id(),
    			        &output_str, sizeof(output_str));
    	if (err)
    		trace_printk(err_str, sizeof(err_str), err);
            return 1;
      }
      SEC("func_begin=sys_nanosleep")
      int func_begin(void *ctx) {return func(ctx, 1);}
      SEC("func_end=sys_nanosleep%return")
      int func_end(void *ctx) { return func(ctx, 2);}
      char _license[] SEC("license") = "GPL";
      int _version SEC("version") = LINUX_VERSION_CODE;
      /************************* END ***************************/
    Signed-off-by: default avatarWang Nan <wangnan0@huawei.com>
    Cc: Alexei Starovoitov <ast@kernel.org>
    Cc: Jiri Olsa <jolsa@kernel.org>
    Cc: Li Zefan <lizefan@huawei.com>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: pi3orama@163.com
    Link: http://lkml.kernel.org/r/1456479154-136027-8-git-send-email-wangnan0@huawei.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
    1d6c9407
builtin-trace.c 87.5 KB