xref: /linux-tools/ebpf/libbpf-bootstrap/kprobe_client.c (revision 1009c19dc94f77e7a7c049e186f796e4881717f9)
1*1009c19dSDavid Wang #include <stdio.h>
2*1009c19dSDavid Wang #include <unistd.h>
3*1009c19dSDavid Wang #include <linux/bpf.h>
4*1009c19dSDavid Wang #include <asm/unistd.h>
5*1009c19dSDavid Wang #include <fcntl.h>
6*1009c19dSDavid Wang #include <string.h>
7*1009c19dSDavid Wang 
8*1009c19dSDavid Wang 
sys_bpf(enum bpf_cmd cmd,union bpf_attr * attr,unsigned int size)9*1009c19dSDavid Wang static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
10*1009c19dSDavid Wang 			  unsigned int size)
11*1009c19dSDavid Wang {
12*1009c19dSDavid Wang 	return syscall(__NR_bpf, cmd, attr, size);
13*1009c19dSDavid Wang }
ptr_to_u64(const void * ptr)14*1009c19dSDavid Wang static inline __u64 ptr_to_u64(const void *ptr)
15*1009c19dSDavid Wang {
16*1009c19dSDavid Wang 	return (__u64) (unsigned long) ptr;
17*1009c19dSDavid Wang }
18*1009c19dSDavid Wang 
19*1009c19dSDavid Wang 
get_root_fd()20*1009c19dSDavid Wang int get_root_fd() {
21*1009c19dSDavid Wang     union bpf_attr attr_pin;
22*1009c19dSDavid Wang     int fd, rc;
23*1009c19dSDavid Wang     memset(&attr_pin, 0, sizeof(attr_pin));
24*1009c19dSDavid Wang     attr_pin.pathname = ptr_to_u64("/sys/fs/bpf/ksysread_stats");
25*1009c19dSDavid Wang     fd = sys_bpf(BPF_OBJ_GET, &attr_pin, sizeof(attr_pin));
26*1009c19dSDavid Wang     return fd;
27*1009c19dSDavid Wang }
28*1009c19dSDavid Wang 
29*1009c19dSDavid Wang 
main(int argc,char * argv[])30*1009c19dSDavid Wang int main(int argc, char *argv[]) {
31*1009c19dSDavid Wang     unsigned int vip, ip;
32*1009c19dSDavid Wang     int i, rc, fd;
33*1009c19dSDavid Wang     union bpf_attr attr_elem;
34*1009c19dSDavid Wang     unsigned int key;
35*1009c19dSDavid Wang     unsigned long long value, b=1;
36*1009c19dSDavid Wang     int root_fd = get_root_fd();
37*1009c19dSDavid Wang     if (root_fd < 0) {
38*1009c19dSDavid Wang         perror("fail to open map\n");
39*1009c19dSDavid Wang         return -1;
40*1009c19dSDavid Wang     }
41*1009c19dSDavid Wang     memset(&attr_elem, 0, sizeof(attr_elem));
42*1009c19dSDavid Wang     attr_elem.flags = BPF_ANY;
43*1009c19dSDavid Wang     attr_elem.map_fd = root_fd;
44*1009c19dSDavid Wang     attr_elem.key    = ptr_to_u64(&key);
45*1009c19dSDavid Wang     attr_elem.value    = ptr_to_u64(&value);
46*1009c19dSDavid Wang     for (key=0; key<32; key++) {
47*1009c19dSDavid Wang         rc = sys_bpf(BPF_MAP_LOOKUP_ELEM, &attr_elem, sizeof(attr_elem));
48*1009c19dSDavid Wang         if (rc!=0) {
49*1009c19dSDavid Wang             printf("fail to get read stats %d\n", key);
50*1009c19dSDavid Wang             return 0;
51*1009c19dSDavid Wang         }
52*1009c19dSDavid Wang         // if (value==0) continue;
53*1009c19dSDavid Wang         if (b>=1024*1024) printf("%6lldMB: %-9lld\n", b/1024/1024, value);
54*1009c19dSDavid Wang         else if (b>=1024) printf("%6lldKB: %-9lld\n", b/1024, value);
55*1009c19dSDavid Wang         else printf("%7lldB: %-9lld\n", b, value);
56*1009c19dSDavid Wang         b<<=1;
57*1009c19dSDavid Wang     }
58*1009c19dSDavid Wang     return 0;
59*1009c19dSDavid Wang }
60