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

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

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

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