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

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/stacktrace_map.c

Version: ~ [ linux-5.2-rc1 ] ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <test_progs.h>
  3 
  4 void test_stacktrace_map(void)
  5 {
  6         int control_map_fd, stackid_hmap_fd, stackmap_fd, stack_amap_fd;
  7         const char *file = "./test_stacktrace_map.o";
  8         int bytes, efd, err, pmu_fd, prog_fd, stack_trace_len;
  9         struct perf_event_attr attr = {};
 10         __u32 key, val, duration = 0;
 11         struct bpf_object *obj;
 12         char buf[256];
 13 
 14         err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
 15         if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
 16                 return;
 17 
 18         /* Get the ID for the sched/sched_switch tracepoint */
 19         snprintf(buf, sizeof(buf),
 20                  "/sys/kernel/debug/tracing/events/sched/sched_switch/id");
 21         efd = open(buf, O_RDONLY, 0);
 22         if (CHECK(efd < 0, "open", "err %d errno %d\n", efd, errno))
 23                 goto close_prog;
 24 
 25         bytes = read(efd, buf, sizeof(buf));
 26         close(efd);
 27         if (bytes <= 0 || bytes >= sizeof(buf))
 28                 goto close_prog;
 29 
 30         /* Open the perf event and attach bpf progrram */
 31         attr.config = strtol(buf, NULL, 0);
 32         attr.type = PERF_TYPE_TRACEPOINT;
 33         attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN;
 34         attr.sample_period = 1;
 35         attr.wakeup_events = 1;
 36         pmu_fd = syscall(__NR_perf_event_open, &attr, -1 /* pid */,
 37                          0 /* cpu 0 */, -1 /* group id */,
 38                          0 /* flags */);
 39         if (CHECK(pmu_fd < 0, "perf_event_open", "err %d errno %d\n",
 40                   pmu_fd, errno))
 41                 goto close_prog;
 42 
 43         err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
 44         if (err)
 45                 goto disable_pmu;
 46 
 47         err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
 48         if (err)
 49                 goto disable_pmu;
 50 
 51         /* find map fds */
 52         control_map_fd = bpf_find_map(__func__, obj, "control_map");
 53         if (control_map_fd < 0)
 54                 goto disable_pmu;
 55 
 56         stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap");
 57         if (stackid_hmap_fd < 0)
 58                 goto disable_pmu;
 59 
 60         stackmap_fd = bpf_find_map(__func__, obj, "stackmap");
 61         if (stackmap_fd < 0)
 62                 goto disable_pmu;
 63 
 64         stack_amap_fd = bpf_find_map(__func__, obj, "stack_amap");
 65         if (stack_amap_fd < 0)
 66                 goto disable_pmu;
 67 
 68         /* give some time for bpf program run */
 69         sleep(1);
 70 
 71         /* disable stack trace collection */
 72         key = 0;
 73         val = 1;
 74         bpf_map_update_elem(control_map_fd, &key, &val, 0);
 75 
 76         /* for every element in stackid_hmap, we can find a corresponding one
 77          * in stackmap, and vise versa.
 78          */
 79         err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
 80         if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
 81                   "err %d errno %d\n", err, errno))
 82                 goto disable_pmu_noerr;
 83 
 84         err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
 85         if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
 86                   "err %d errno %d\n", err, errno))
 87                 goto disable_pmu_noerr;
 88 
 89         stack_trace_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
 90         err = compare_stack_ips(stackmap_fd, stack_amap_fd, stack_trace_len);
 91         if (CHECK(err, "compare_stack_ips stackmap vs. stack_amap",
 92                   "err %d errno %d\n", err, errno))
 93                 goto disable_pmu_noerr;
 94 
 95         goto disable_pmu_noerr;
 96 disable_pmu:
 97         error_cnt++;
 98 disable_pmu_noerr:
 99         ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE);
100         close(pmu_fd);
101 close_prog:
102         bpf_object__close(obj);
103 }
104 

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

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | 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.

osdn.jp