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

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

Version: ~ [ linux-5.18 ] ~ [ linux-5.17.9 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.41 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.117 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.195 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.244 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.280 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.315 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ 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) 2016 Facebook
  3  */
  4 #include <stdio.h>
  5 #include <unistd.h>
  6 #include <stdlib.h>
  7 #include <signal.h>
  8 #include <linux/bpf.h>
  9 #include <string.h>
 10 #include <linux/perf_event.h>
 11 #include <errno.h>
 12 #include <assert.h>
 13 #include <stdbool.h>
 14 #include <sys/resource.h>
 15 #include "libbpf.h"
 16 #include "bpf_load.h"
 17 #include "trace_helpers.h"
 18 
 19 #define PRINT_RAW_ADDR 0
 20 
 21 static void print_ksym(__u64 addr)
 22 {
 23         struct ksym *sym;
 24 
 25         if (!addr)
 26                 return;
 27         sym = ksym_search(addr);
 28         if (!sym) {
 29                 printf("ksym not found. Is kallsyms loaded?\n");
 30                 return;
 31         }
 32 
 33         if (PRINT_RAW_ADDR)
 34                 printf("%s/%llx;", sym->name, addr);
 35         else
 36                 printf("%s;", sym->name);
 37 }
 38 
 39 #define TASK_COMM_LEN 16
 40 
 41 struct key_t {
 42         char waker[TASK_COMM_LEN];
 43         char target[TASK_COMM_LEN];
 44         __u32 wret;
 45         __u32 tret;
 46 };
 47 
 48 static void print_stack(struct key_t *key, __u64 count)
 49 {
 50         __u64 ip[PERF_MAX_STACK_DEPTH] = {};
 51         static bool warned;
 52         int i;
 53 
 54         printf("%s;", key->target);
 55         if (bpf_map_lookup_elem(map_fd[3], &key->tret, ip) != 0) {
 56                 printf("---;");
 57         } else {
 58                 for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
 59                         print_ksym(ip[i]);
 60         }
 61         printf("-;");
 62         if (bpf_map_lookup_elem(map_fd[3], &key->wret, ip) != 0) {
 63                 printf("---;");
 64         } else {
 65                 for (i = 0; i < PERF_MAX_STACK_DEPTH; i++)
 66                         print_ksym(ip[i]);
 67         }
 68         printf(";%s %lld\n", key->waker, count);
 69 
 70         if ((key->tret == -EEXIST || key->wret == -EEXIST) && !warned) {
 71                 printf("stackmap collisions seen. Consider increasing size\n");
 72                 warned = true;
 73         } else if (((int)(key->tret) < 0 || (int)(key->wret) < 0)) {
 74                 printf("err stackid %d %d\n", key->tret, key->wret);
 75         }
 76 }
 77 
 78 static void print_stacks(int fd)
 79 {
 80         struct key_t key = {}, next_key;
 81         __u64 value;
 82 
 83         while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
 84                 bpf_map_lookup_elem(fd, &next_key, &value);
 85                 print_stack(&next_key, value);
 86                 key = next_key;
 87         }
 88 }
 89 
 90 static void int_exit(int sig)
 91 {
 92         print_stacks(map_fd[0]);
 93         exit(0);
 94 }
 95 
 96 int main(int argc, char **argv)
 97 {
 98         struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
 99         char filename[256];
100         int delay = 1;
101 
102         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
103         setrlimit(RLIMIT_MEMLOCK, &r);
104 
105         signal(SIGINT, int_exit);
106         signal(SIGTERM, int_exit);
107 
108         if (load_kallsyms()) {
109                 printf("failed to process /proc/kallsyms\n");
110                 return 2;
111         }
112 
113         if (load_bpf_file(filename)) {
114                 printf("%s", bpf_log_buf);
115                 return 1;
116         }
117 
118         if (argc > 1)
119                 delay = atoi(argv[1]);
120         sleep(delay);
121         print_stacks(map_fd[0]);
122 
123         return 0;
124 }
125 

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