Brenden Blanco | 246b942 | 2015-06-05 11:15:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 PLUMgrid, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 17 | #include <arpa/inet.h> |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 18 | #include <errno.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 20 | #include <linux/bpf.h> |
| 21 | #include <linux/if_packet.h> |
| 22 | #include <linux/pkt_cls.h> |
| 23 | #include <linux/perf_event.h> |
| 24 | #include <linux/rtnetlink.h> |
| 25 | #include <linux/unistd.h> |
| 26 | #include <linux/version.h> |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 27 | #include <net/ethernet.h> |
| 28 | #include <net/if.h> |
Brenden Blanco | bb7200c | 2015-06-04 18:01:42 -0700 | [diff] [blame] | 29 | #include <stdio.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <sys/ioctl.h> |
Brenden Blanco | bb7200c | 2015-06-04 18:01:42 -0700 | [diff] [blame] | 33 | #include <unistd.h> |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 34 | |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 35 | #include "libbpf.h" |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 36 | #include "perf_reader.h" |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 37 | |
Brenden Blanco | f275d3d | 2015-07-06 23:41:23 -0700 | [diff] [blame] | 38 | // TODO: remove these defines when linux-libc-dev exports them properly |
| 39 | |
| 40 | #ifndef __NR_bpf |
| 41 | #define __NR_bpf 321 |
| 42 | #endif |
| 43 | |
| 44 | #ifndef SO_ATTACH_BPF |
| 45 | #define SO_ATTACH_BPF 50 |
| 46 | #endif |
| 47 | |
| 48 | #ifndef PERF_EVENT_IOC_SET_BPF |
| 49 | #define PERF_EVENT_IOC_SET_BPF _IOW('$', 8, __u32) |
| 50 | #endif |
| 51 | |
| 52 | #ifndef PERF_FLAG_FD_CLOEXEC |
| 53 | #define PERF_FLAG_FD_CLOEXEC (1UL << 3) |
| 54 | #endif |
| 55 | |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 56 | static __u64 ptr_to_u64(void *ptr) |
| 57 | { |
| 58 | return (__u64) (unsigned long) ptr; |
| 59 | } |
| 60 | |
| 61 | int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, int max_entries) |
| 62 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 63 | union bpf_attr attr; |
| 64 | memset(&attr, 0, sizeof(attr)); |
| 65 | attr.map_type = map_type; |
| 66 | attr.key_size = key_size; |
| 67 | attr.value_size = value_size; |
| 68 | attr.max_entries = max_entries; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 69 | |
| 70 | return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); |
| 71 | } |
| 72 | |
| 73 | int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags) |
| 74 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 75 | union bpf_attr attr; |
| 76 | memset(&attr, 0, sizeof(attr)); |
| 77 | attr.map_fd = fd; |
| 78 | attr.key = ptr_to_u64(key); |
| 79 | attr.value = ptr_to_u64(value); |
| 80 | attr.flags = flags; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 81 | |
| 82 | return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); |
| 83 | } |
| 84 | |
| 85 | int bpf_lookup_elem(int fd, void *key, void *value) |
| 86 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 87 | union bpf_attr attr; |
| 88 | memset(&attr, 0, sizeof(attr)); |
| 89 | attr.map_fd = fd; |
| 90 | attr.key = ptr_to_u64(key); |
| 91 | attr.value = ptr_to_u64(value); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 92 | |
| 93 | return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); |
| 94 | } |
| 95 | |
| 96 | int bpf_delete_elem(int fd, void *key) |
| 97 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 98 | union bpf_attr attr; |
| 99 | memset(&attr, 0, sizeof(attr)); |
| 100 | attr.map_fd = fd; |
| 101 | attr.key = ptr_to_u64(key); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 102 | |
| 103 | return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); |
| 104 | } |
| 105 | |
| 106 | int bpf_get_next_key(int fd, void *key, void *next_key) |
| 107 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 108 | union bpf_attr attr; |
| 109 | memset(&attr, 0, sizeof(attr)); |
| 110 | attr.map_fd = fd; |
| 111 | attr.key = ptr_to_u64(key); |
| 112 | attr.next_key = ptr_to_u64(next_key); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 113 | |
| 114 | return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); |
| 115 | } |
| 116 | |
| 117 | #define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u)) |
| 118 | |
| 119 | char bpf_log_buf[LOG_BUF_SIZE]; |
| 120 | |
| 121 | int bpf_prog_load(enum bpf_prog_type prog_type, |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 122 | const struct bpf_insn *insns, int prog_len, |
Brenden Blanco | 759029f | 2015-07-29 15:47:51 -0700 | [diff] [blame] | 123 | const char *license, unsigned kern_version, |
| 124 | char *log_buf, unsigned log_buf_size) |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 125 | { |
Brenden Blanco | fdc027c | 2015-09-03 11:49:54 -0700 | [diff] [blame] | 126 | union bpf_attr attr; |
| 127 | memset(&attr, 0, sizeof(attr)); |
| 128 | attr.prog_type = prog_type; |
| 129 | attr.insns = ptr_to_u64((void *) insns); |
| 130 | attr.insn_cnt = prog_len / sizeof(struct bpf_insn); |
| 131 | attr.license = ptr_to_u64((void *) license); |
| 132 | attr.log_buf = ptr_to_u64(log_buf); |
| 133 | attr.log_size = log_buf_size; |
| 134 | attr.log_level = log_buf ? 1 : 0; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 135 | |
Brenden Blanco | 7009b55 | 2015-05-26 11:48:17 -0700 | [diff] [blame] | 136 | attr.kern_version = kern_version; |
Brenden Blanco | 81a783a | 2015-08-24 23:42:42 -0700 | [diff] [blame] | 137 | if (log_buf) |
| 138 | log_buf[0] = 0; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 139 | |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 140 | int ret = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); |
Brenden Blanco | 81a783a | 2015-08-24 23:42:42 -0700 | [diff] [blame] | 141 | if (ret < 0 && !log_buf) { |
| 142 | // caller did not specify log_buf but failure should be printed, |
| 143 | // so call recursively and print the result to stderr |
| 144 | bpf_prog_load(prog_type, insns, prog_len, license, kern_version, |
| 145 | bpf_log_buf, LOG_BUF_SIZE); |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 146 | fprintf(stderr, "bpf: %s\n%s\n", strerror(errno), bpf_log_buf); |
| 147 | } |
| 148 | return ret; |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int bpf_open_raw_sock(const char *name) |
| 152 | { |
| 153 | struct sockaddr_ll sll; |
| 154 | int sock; |
| 155 | |
| 156 | sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL)); |
| 157 | if (sock < 0) { |
| 158 | printf("cannot create raw socket\n"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | memset(&sll, 0, sizeof(sll)); |
| 163 | sll.sll_family = AF_PACKET; |
| 164 | sll.sll_ifindex = if_nametoindex(name); |
| 165 | sll.sll_protocol = htons(ETH_P_ALL); |
| 166 | if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) { |
| 167 | printf("bind to %s: %s\n", name, strerror(errno)); |
| 168 | close(sock); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | return sock; |
| 173 | } |
| 174 | |
| 175 | int bpf_attach_socket(int sock, int prog) { |
Brenden Blanco | af95673 | 2015-06-09 13:58:42 -0700 | [diff] [blame] | 176 | return setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog, sizeof(prog)); |
Brenden Blanco | a94bd93 | 2015-04-26 00:56:42 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 179 | static int bpf_attach_tracing_event(int progfd, const char *event_path, |
| 180 | struct perf_reader *reader, int pid, int cpu, int group_fd) { |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 181 | int efd = -1, pfd; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 182 | ssize_t bytes; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 183 | char buf[256]; |
| 184 | struct perf_event_attr attr = {}; |
| 185 | |
| 186 | snprintf(buf, sizeof(buf), "%s/id", event_path); |
| 187 | efd = open(buf, O_RDONLY, 0); |
| 188 | if (efd < 0) { |
| 189 | fprintf(stderr, "open(%s): %s\n", buf, strerror(errno)); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 190 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | bytes = read(efd, buf, sizeof(buf)); |
| 194 | if (bytes <= 0 || bytes >= sizeof(buf)) { |
| 195 | fprintf(stderr, "read(%s): %s\n", buf, strerror(errno)); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 196 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 197 | } |
| 198 | buf[bytes] = '\0'; |
| 199 | attr.config = strtol(buf, NULL, 0); |
| 200 | attr.type = PERF_TYPE_TRACEPOINT; |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 201 | attr.sample_type = PERF_SAMPLE_RAW | PERF_SAMPLE_CALLCHAIN; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 202 | attr.sample_period = 1; |
| 203 | attr.wakeup_events = 1; |
| 204 | pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, group_fd, PERF_FLAG_FD_CLOEXEC); |
| 205 | if (pfd < 0) { |
| 206 | perror("perf_event_open"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 207 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 208 | } |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 209 | perf_reader_set_fd(reader, pfd); |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 210 | |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 211 | if (perf_reader_mmap(reader, attr.type, attr.sample_type) < 0) |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 212 | goto error; |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 213 | |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 214 | if (ioctl(pfd, PERF_EVENT_IOC_SET_BPF, progfd) < 0) { |
| 215 | perror("ioctl(PERF_EVENT_IOC_SET_BPF)"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 216 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 217 | } |
| 218 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 219 | perror("ioctl(PERF_EVENT_IOC_ENABLE)"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 220 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 223 | return 0; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 224 | |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 225 | error: |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 226 | if (efd >= 0) |
| 227 | close(efd); |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 228 | |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 229 | return -1; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 232 | void * bpf_attach_kprobe(int progfd, const char *event, |
| 233 | const char *event_desc, pid_t pid, |
| 234 | int cpu, int group_fd, perf_reader_cb cb, |
| 235 | void *cb_cookie) { |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 236 | int kfd = -1; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 237 | char buf[256]; |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 238 | struct perf_reader *reader = NULL; |
| 239 | |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 240 | reader = perf_reader_new(cb, NULL, cb_cookie); |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 241 | if (!reader) |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 242 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 243 | |
| 244 | kfd = open("/sys/kernel/debug/tracing/kprobe_events", O_WRONLY | O_APPEND, 0); |
| 245 | if (kfd < 0) { |
| 246 | perror("open(kprobe_events)"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 247 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | if (write(kfd, event_desc, strlen(event_desc)) < 0) { |
Brenden Blanco | 7e71aef | 2015-09-09 18:28:21 -0700 | [diff] [blame] | 251 | fprintf(stderr, "write of \"%s\" into kprobe_events failed: %s\n", event_desc, strerror(errno)); |
| 252 | if (errno == EINVAL) |
| 253 | fprintf(stderr, "check dmesg output for possible cause\n"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 254 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | snprintf(buf, sizeof(buf), "/sys/kernel/debug/tracing/events/kprobes/%s", event); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 258 | if (bpf_attach_tracing_event(progfd, buf, reader, pid, cpu, group_fd) < 0) |
| 259 | goto error; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 260 | |
Brenden Blanco | 8207d10 | 2015-09-25 13:58:30 -0700 | [diff] [blame] | 261 | return reader; |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 262 | |
| 263 | error: |
| 264 | if (kfd >= 0) |
| 265 | close(kfd); |
| 266 | if (reader) |
| 267 | perf_reader_free(reader); |
| 268 | |
| 269 | return NULL; |
Brenden Blanco | cd5cb41 | 2015-04-26 09:41:58 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 272 | int bpf_detach_kprobe(const char *event_desc) { |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 273 | int kfd = -1; |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 274 | |
| 275 | kfd = open("/sys/kernel/debug/tracing/kprobe_events", O_WRONLY | O_APPEND, 0); |
| 276 | if (kfd < 0) { |
| 277 | perror("open(kprobe_events)"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 278 | goto error; |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (write(kfd, event_desc, strlen(event_desc)) < 0) { |
| 282 | perror("write(kprobe_events)"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 283 | goto error; |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 284 | } |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 285 | |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 286 | return 0; |
| 287 | |
| 288 | error: |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 289 | if (kfd >= 0) |
| 290 | close(kfd); |
| 291 | |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 292 | return -1; |
Brenden Blanco | 839dd27 | 2015-06-11 12:35:55 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 295 | void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb, void *cb_cookie, int pid, int cpu) { |
| 296 | int pfd; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 297 | struct perf_event_attr attr = {}; |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 298 | struct perf_reader *reader = NULL; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 299 | |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 300 | reader = perf_reader_new(NULL, raw_cb, cb_cookie); |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 301 | if (!reader) |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 302 | goto error; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 303 | |
| 304 | attr.config = PERF_COUNT_SW_BPF_OUTPUT; |
| 305 | attr.type = PERF_TYPE_SOFTWARE; |
| 306 | attr.sample_type = PERF_SAMPLE_RAW; |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 307 | attr.sample_period = 1; |
| 308 | attr.wakeup_events = 1; |
| 309 | pfd = syscall(__NR_perf_event_open, &attr, pid, cpu, -1, PERF_FLAG_FD_CLOEXEC); |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 310 | if (pfd < 0) { |
| 311 | perror("perf_event_open"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 312 | goto error; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 313 | } |
| 314 | perf_reader_set_fd(reader, pfd); |
| 315 | |
| 316 | if (perf_reader_mmap(reader, attr.type, attr.sample_type) < 0) |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 317 | goto error; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 318 | |
| 319 | if (ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) < 0) { |
| 320 | perror("ioctl(PERF_EVENT_IOC_ENABLE)"); |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 321 | goto error; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | return reader; |
Brenden Blanco | 7598249 | 2015-11-06 10:43:05 -0800 | [diff] [blame^] | 325 | |
| 326 | error: |
| 327 | if (reader) |
| 328 | perf_reader_free(reader); |
| 329 | |
| 330 | return NULL; |
Brenden Blanco | d0daf6a | 2015-11-05 23:31:22 -0800 | [diff] [blame] | 331 | } |