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

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

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ 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 // SPDX-License-Identifier: GPL-2.0-only
  2 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
  3  */
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <signal.h>
  7 #include <unistd.h>
  8 #include <stdbool.h>
  9 #include <string.h>
 10 #include <time.h>
 11 
 12 #include <bpf/bpf.h>
 13 #include <bpf/libbpf.h>
 14 
 15 struct pair {
 16         long long val;
 17         __u64 ip;
 18 };
 19 
 20 static __u64 time_get_ns(void)
 21 {
 22         struct timespec ts;
 23 
 24         clock_gettime(CLOCK_MONOTONIC, &ts);
 25         return ts.tv_sec * 1000000000ull + ts.tv_nsec;
 26 }
 27 
 28 static void print_old_objects(int fd)
 29 {
 30         long long val = time_get_ns();
 31         __u64 key, next_key;
 32         struct pair v;
 33 
 34         key = write(1, "\e[1;1H\e[2J", 11); /* clear screen */
 35 
 36         key = -1;
 37         while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
 38                 bpf_map_lookup_elem(fd, &next_key, &v);
 39                 key = next_key;
 40                 if (val - v.val < 1000000000ll)
 41                         /* object was allocated more then 1 sec ago */
 42                         continue;
 43                 printf("obj 0x%llx is %2lldsec old was allocated at ip %llx\n",
 44                        next_key, (val - v.val) / 1000000000ll, v.ip);
 45         }
 46 }
 47 
 48 int main(int ac, char **argv)
 49 {
 50         struct bpf_link *links[2];
 51         struct bpf_program *prog;
 52         struct bpf_object *obj;
 53         char filename[256];
 54         int map_fd, j = 0;
 55 
 56         snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]);
 57         obj = bpf_object__open_file(filename, NULL);
 58         if (libbpf_get_error(obj)) {
 59                 fprintf(stderr, "ERROR: opening BPF object file failed\n");
 60                 return 0;
 61         }
 62 
 63         /* load BPF program */
 64         if (bpf_object__load(obj)) {
 65                 fprintf(stderr, "ERROR: loading BPF object file failed\n");
 66                 goto cleanup;
 67         }
 68 
 69         map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
 70         if (map_fd < 0) {
 71                 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
 72                 goto cleanup;
 73         }
 74 
 75         bpf_object__for_each_program(prog, obj) {
 76                 links[j] = bpf_program__attach(prog);
 77                 if (libbpf_get_error(links[j])) {
 78                         fprintf(stderr, "ERROR: bpf_program__attach failed\n");
 79                         links[j] = NULL;
 80                         goto cleanup;
 81                 }
 82                 j++;
 83         }
 84 
 85         while (1) {
 86                 print_old_objects(map_fd);
 87                 sleep(1);
 88         }
 89 
 90 cleanup:
 91         for (j--; j >= 0; j--)
 92                 bpf_link__destroy(links[j]);
 93 
 94         bpf_object__close(obj);
 95         return 0;
 96 }
 97 

~ [ 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