Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * bpf-loader.c |
| 3 | * |
| 4 | * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> |
| 5 | * Copyright (C) 2015 Huawei Inc. |
| 6 | */ |
| 7 | |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 8 | #include <linux/bpf.h> |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 9 | #include <bpf/libbpf.h> |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 10 | #include <bpf/bpf.h> |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 11 | #include <linux/err.h> |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 12 | #include <linux/string.h> |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 13 | #include "perf.h" |
| 14 | #include "debug.h" |
| 15 | #include "bpf-loader.h" |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 16 | #include "bpf-prologue.h" |
| 17 | #include "llvm-utils.h" |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 18 | #include "probe-event.h" |
| 19 | #include "probe-finder.h" // for MAX_PROBES |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 20 | #include "parse-events.h" |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 21 | #include "llvm-utils.h" |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 22 | |
| 23 | #define DEFINE_PRINT_FN(name, level) \ |
| 24 | static int libbpf_##name(const char *fmt, ...) \ |
| 25 | { \ |
| 26 | va_list args; \ |
| 27 | int ret; \ |
| 28 | \ |
| 29 | va_start(args, fmt); \ |
| 30 | ret = veprintf(level, verbose, pr_fmt(fmt), args);\ |
| 31 | va_end(args); \ |
| 32 | return ret; \ |
| 33 | } |
| 34 | |
Wang Nan | 7a01194 | 2015-11-03 10:44:43 +0000 | [diff] [blame] | 35 | DEFINE_PRINT_FN(warning, 1) |
| 36 | DEFINE_PRINT_FN(info, 1) |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 37 | DEFINE_PRINT_FN(debug, 1) |
| 38 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 39 | struct bpf_prog_priv { |
| 40 | struct perf_probe_event pev; |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 41 | bool need_prologue; |
| 42 | struct bpf_insn *insns_buf; |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 43 | int nr_types; |
| 44 | int *type_mapping; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Wang Nan | ba1fae4 | 2015-11-06 13:49:43 +0000 | [diff] [blame] | 47 | static bool libbpf_initialized; |
| 48 | |
| 49 | struct bpf_object * |
| 50 | bpf__prepare_load_buffer(void *obj_buf, size_t obj_buf_sz, const char *name) |
| 51 | { |
| 52 | struct bpf_object *obj; |
| 53 | |
| 54 | if (!libbpf_initialized) { |
| 55 | libbpf_set_print(libbpf_warning, |
| 56 | libbpf_info, |
| 57 | libbpf_debug); |
| 58 | libbpf_initialized = true; |
| 59 | } |
| 60 | |
| 61 | obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, name); |
| 62 | if (IS_ERR(obj)) { |
| 63 | pr_debug("bpf: failed to load buffer\n"); |
| 64 | return ERR_PTR(-EINVAL); |
| 65 | } |
| 66 | |
| 67 | return obj; |
| 68 | } |
| 69 | |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 70 | struct bpf_object *bpf__prepare_load(const char *filename, bool source) |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 71 | { |
| 72 | struct bpf_object *obj; |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 73 | |
| 74 | if (!libbpf_initialized) { |
| 75 | libbpf_set_print(libbpf_warning, |
| 76 | libbpf_info, |
| 77 | libbpf_debug); |
| 78 | libbpf_initialized = true; |
| 79 | } |
| 80 | |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 81 | if (source) { |
| 82 | int err; |
| 83 | void *obj_buf; |
| 84 | size_t obj_buf_sz; |
| 85 | |
| 86 | err = llvm__compile_bpf(filename, &obj_buf, &obj_buf_sz); |
| 87 | if (err) |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 88 | return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE); |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 89 | obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename); |
| 90 | free(obj_buf); |
| 91 | } else |
| 92 | obj = bpf_object__open(filename); |
| 93 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 94 | if (IS_ERR(obj)) { |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 95 | pr_debug("bpf: failed to load %s\n", filename); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 96 | return obj; |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | return obj; |
| 100 | } |
| 101 | |
| 102 | void bpf__clear(void) |
| 103 | { |
| 104 | struct bpf_object *obj, *tmp; |
| 105 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 106 | bpf_object__for_each_safe(obj, tmp) { |
| 107 | bpf__unprobe(obj); |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 108 | bpf_object__close(obj); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | static void |
Wang Nan | 80cdce7 | 2016-02-19 11:43:51 +0000 | [diff] [blame] | 113 | clear_prog_priv(struct bpf_program *prog __maybe_unused, |
| 114 | void *_priv) |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 115 | { |
| 116 | struct bpf_prog_priv *priv = _priv; |
| 117 | |
| 118 | cleanup_perf_probe_events(&priv->pev, 1); |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 119 | zfree(&priv->insns_buf); |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 120 | zfree(&priv->type_mapping); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 121 | free(priv); |
| 122 | } |
| 123 | |
| 124 | static int |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 125 | prog_config__exec(const char *value, struct perf_probe_event *pev) |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 126 | { |
| 127 | pev->uprobes = true; |
| 128 | pev->target = strdup(value); |
| 129 | if (!pev->target) |
| 130 | return -ENOMEM; |
| 131 | return 0; |
| 132 | } |
| 133 | |
Wang Nan | 5dbd16c | 2015-11-16 12:10:06 +0000 | [diff] [blame] | 134 | static int |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 135 | prog_config__module(const char *value, struct perf_probe_event *pev) |
Wang Nan | 5dbd16c | 2015-11-16 12:10:06 +0000 | [diff] [blame] | 136 | { |
| 137 | pev->uprobes = false; |
| 138 | pev->target = strdup(value); |
| 139 | if (!pev->target) |
| 140 | return -ENOMEM; |
| 141 | return 0; |
| 142 | } |
| 143 | |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 144 | static int |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 145 | prog_config__bool(const char *value, bool *pbool, bool invert) |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 146 | { |
| 147 | int err; |
| 148 | bool bool_value; |
| 149 | |
| 150 | if (!pbool) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | err = strtobool(value, &bool_value); |
| 154 | if (err) |
| 155 | return err; |
| 156 | |
| 157 | *pbool = invert ? !bool_value : bool_value; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 162 | prog_config__inlines(const char *value, |
| 163 | struct perf_probe_event *pev __maybe_unused) |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 164 | { |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 165 | return prog_config__bool(value, &probe_conf.no_inlines, true); |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static int |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 169 | prog_config__force(const char *value, |
| 170 | struct perf_probe_event *pev __maybe_unused) |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 171 | { |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 172 | return prog_config__bool(value, &probe_conf.force_add, false); |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 175 | static struct { |
| 176 | const char *key; |
| 177 | const char *usage; |
| 178 | const char *desc; |
| 179 | int (*func)(const char *, struct perf_probe_event *); |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 180 | } bpf_prog_config_terms[] = { |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 181 | { |
| 182 | .key = "exec", |
| 183 | .usage = "exec=<full path of file>", |
| 184 | .desc = "Set uprobe target", |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 185 | .func = prog_config__exec, |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 186 | }, |
Wang Nan | 5dbd16c | 2015-11-16 12:10:06 +0000 | [diff] [blame] | 187 | { |
| 188 | .key = "module", |
| 189 | .usage = "module=<module name> ", |
| 190 | .desc = "Set kprobe module", |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 191 | .func = prog_config__module, |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 192 | }, |
| 193 | { |
| 194 | .key = "inlines", |
| 195 | .usage = "inlines=[yes|no] ", |
| 196 | .desc = "Probe at inline symbol", |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 197 | .func = prog_config__inlines, |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 198 | }, |
| 199 | { |
| 200 | .key = "force", |
| 201 | .usage = "force=[yes|no] ", |
| 202 | .desc = "Forcibly add events with existing name", |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 203 | .func = prog_config__force, |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 204 | }, |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static int |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 208 | do_prog_config(const char *key, const char *value, |
| 209 | struct perf_probe_event *pev) |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 210 | { |
| 211 | unsigned int i; |
| 212 | |
| 213 | pr_debug("config bpf program: %s=%s\n", key, value); |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 214 | for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++) |
| 215 | if (strcmp(key, bpf_prog_config_terms[i].key) == 0) |
| 216 | return bpf_prog_config_terms[i].func(value, pev); |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 217 | |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 218 | pr_debug("BPF: ERROR: invalid program config option: %s=%s\n", |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 219 | key, value); |
| 220 | |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 221 | pr_debug("\nHint: Valid options are:\n"); |
| 222 | for (i = 0; i < ARRAY_SIZE(bpf_prog_config_terms); i++) |
| 223 | pr_debug("\t%s:\t%s\n", bpf_prog_config_terms[i].usage, |
| 224 | bpf_prog_config_terms[i].desc); |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 225 | pr_debug("\n"); |
| 226 | |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 227 | return -BPF_LOADER_ERRNO__PROGCONF_TERM; |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static const char * |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 231 | parse_prog_config_kvpair(const char *config_str, struct perf_probe_event *pev) |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 232 | { |
| 233 | char *text = strdup(config_str); |
| 234 | char *sep, *line; |
| 235 | const char *main_str = NULL; |
| 236 | int err = 0; |
| 237 | |
| 238 | if (!text) { |
| 239 | pr_debug("No enough memory: dup config_str failed\n"); |
| 240 | return ERR_PTR(-ENOMEM); |
| 241 | } |
| 242 | |
| 243 | line = text; |
| 244 | while ((sep = strchr(line, ';'))) { |
| 245 | char *equ; |
| 246 | |
| 247 | *sep = '\0'; |
| 248 | equ = strchr(line, '='); |
| 249 | if (!equ) { |
| 250 | pr_warning("WARNING: invalid config in BPF object: %s\n", |
| 251 | line); |
| 252 | pr_warning("\tShould be 'key=value'.\n"); |
| 253 | goto nextline; |
| 254 | } |
| 255 | *equ = '\0'; |
| 256 | |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 257 | err = do_prog_config(line, equ + 1, pev); |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 258 | if (err) |
| 259 | break; |
| 260 | nextline: |
| 261 | line = sep + 1; |
| 262 | } |
| 263 | |
| 264 | if (!err) |
| 265 | main_str = config_str + (line - text); |
| 266 | free(text); |
| 267 | |
| 268 | return err ? ERR_PTR(err) : main_str; |
| 269 | } |
| 270 | |
| 271 | static int |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 272 | parse_prog_config(const char *config_str, struct perf_probe_event *pev) |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 273 | { |
| 274 | int err; |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 275 | const char *main_str = parse_prog_config_kvpair(config_str, pev); |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 276 | |
| 277 | if (IS_ERR(main_str)) |
| 278 | return PTR_ERR(main_str); |
| 279 | |
| 280 | err = parse_perf_probe_command(main_str, pev); |
| 281 | if (err < 0) { |
| 282 | pr_debug("bpf: '%s' is not a valid config string\n", |
| 283 | config_str); |
| 284 | /* parse failed, don't need clear pev. */ |
| 285 | return -BPF_LOADER_ERRNO__CONFIG; |
| 286 | } |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 291 | config_bpf_program(struct bpf_program *prog) |
| 292 | { |
| 293 | struct perf_probe_event *pev = NULL; |
| 294 | struct bpf_prog_priv *priv = NULL; |
| 295 | const char *config_str; |
| 296 | int err; |
| 297 | |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 298 | /* Initialize per-program probing setting */ |
| 299 | probe_conf.no_inlines = false; |
| 300 | probe_conf.force_add = false; |
| 301 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 302 | config_str = bpf_program__title(prog, false); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 303 | if (IS_ERR(config_str)) { |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 304 | pr_debug("bpf: unable to get title for program\n"); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 305 | return PTR_ERR(config_str); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | priv = calloc(sizeof(*priv), 1); |
| 309 | if (!priv) { |
| 310 | pr_debug("bpf: failed to alloc priv\n"); |
| 311 | return -ENOMEM; |
| 312 | } |
| 313 | pev = &priv->pev; |
| 314 | |
| 315 | pr_debug("bpf: config program '%s'\n", config_str); |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 316 | err = parse_prog_config(config_str, pev); |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 317 | if (err) |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 318 | goto errout; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 319 | |
| 320 | if (pev->group && strcmp(pev->group, PERF_BPF_PROBE_GROUP)) { |
| 321 | pr_debug("bpf: '%s': group for event is set and not '%s'.\n", |
| 322 | config_str, PERF_BPF_PROBE_GROUP); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 323 | err = -BPF_LOADER_ERRNO__GROUP; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 324 | goto errout; |
| 325 | } else if (!pev->group) |
| 326 | pev->group = strdup(PERF_BPF_PROBE_GROUP); |
| 327 | |
| 328 | if (!pev->group) { |
| 329 | pr_debug("bpf: strdup failed\n"); |
| 330 | err = -ENOMEM; |
| 331 | goto errout; |
| 332 | } |
| 333 | |
| 334 | if (!pev->event) { |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 335 | pr_debug("bpf: '%s': event name is missing. Section name should be 'key=value'\n", |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 336 | config_str); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 337 | err = -BPF_LOADER_ERRNO__EVENTNAME; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 338 | goto errout; |
| 339 | } |
| 340 | pr_debug("bpf: config '%s' is ok\n", config_str); |
| 341 | |
Wang Nan | 80cdce7 | 2016-02-19 11:43:51 +0000 | [diff] [blame] | 342 | err = bpf_program__set_private(prog, priv, clear_prog_priv); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 343 | if (err) { |
| 344 | pr_debug("Failed to set priv for program '%s'\n", config_str); |
| 345 | goto errout; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | |
| 350 | errout: |
| 351 | if (pev) |
| 352 | clear_perf_probe_event(pev); |
| 353 | free(priv); |
| 354 | return err; |
| 355 | } |
| 356 | |
| 357 | static int bpf__prepare_probe(void) |
| 358 | { |
| 359 | static int err = 0; |
| 360 | static bool initialized = false; |
| 361 | |
| 362 | /* |
| 363 | * Make err static, so if init failed the first, bpf__prepare_probe() |
| 364 | * fails each time without calling init_probe_symbol_maps multiple |
| 365 | * times. |
| 366 | */ |
| 367 | if (initialized) |
| 368 | return err; |
| 369 | |
| 370 | initialized = true; |
| 371 | err = init_probe_symbol_maps(false); |
| 372 | if (err < 0) |
| 373 | pr_debug("Failed to init_probe_symbol_maps\n"); |
| 374 | probe_conf.max_probes = MAX_PROBES; |
| 375 | return err; |
| 376 | } |
| 377 | |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 378 | static int |
| 379 | preproc_gen_prologue(struct bpf_program *prog, int n, |
| 380 | struct bpf_insn *orig_insns, int orig_insns_cnt, |
| 381 | struct bpf_prog_prep_result *res) |
| 382 | { |
| 383 | struct probe_trace_event *tev; |
| 384 | struct perf_probe_event *pev; |
| 385 | struct bpf_prog_priv *priv; |
| 386 | struct bpf_insn *buf; |
| 387 | size_t prologue_cnt = 0; |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 388 | int i, err; |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 389 | |
| 390 | err = bpf_program__get_private(prog, (void **)&priv); |
| 391 | if (err || !priv) |
| 392 | goto errout; |
| 393 | |
| 394 | pev = &priv->pev; |
| 395 | |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 396 | if (n < 0 || n >= priv->nr_types) |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 397 | goto errout; |
| 398 | |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 399 | /* Find a tev belongs to that type */ |
| 400 | for (i = 0; i < pev->ntevs; i++) { |
| 401 | if (priv->type_mapping[i] == n) |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | if (i >= pev->ntevs) { |
| 406 | pr_debug("Internal error: prologue type %d not found\n", n); |
| 407 | return -BPF_LOADER_ERRNO__PROLOGUE; |
| 408 | } |
| 409 | |
| 410 | tev = &pev->tevs[i]; |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 411 | |
| 412 | buf = priv->insns_buf; |
| 413 | err = bpf__gen_prologue(tev->args, tev->nargs, |
| 414 | buf, &prologue_cnt, |
| 415 | BPF_MAXINSNS - orig_insns_cnt); |
| 416 | if (err) { |
| 417 | const char *title; |
| 418 | |
| 419 | title = bpf_program__title(prog, false); |
| 420 | if (!title) |
| 421 | title = "[unknown]"; |
| 422 | |
| 423 | pr_debug("Failed to generate prologue for program %s\n", |
| 424 | title); |
| 425 | return err; |
| 426 | } |
| 427 | |
| 428 | memcpy(&buf[prologue_cnt], orig_insns, |
| 429 | sizeof(struct bpf_insn) * orig_insns_cnt); |
| 430 | |
| 431 | res->new_insn_ptr = buf; |
| 432 | res->new_insn_cnt = prologue_cnt + orig_insns_cnt; |
| 433 | res->pfd = NULL; |
| 434 | return 0; |
| 435 | |
| 436 | errout: |
| 437 | pr_debug("Internal error in preproc_gen_prologue\n"); |
| 438 | return -BPF_LOADER_ERRNO__PROLOGUE; |
| 439 | } |
| 440 | |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 441 | /* |
| 442 | * compare_tev_args is reflexive, transitive and antisymmetric. |
| 443 | * I can proof it but this margin is too narrow to contain. |
| 444 | */ |
| 445 | static int compare_tev_args(const void *ptev1, const void *ptev2) |
| 446 | { |
| 447 | int i, ret; |
| 448 | const struct probe_trace_event *tev1 = |
| 449 | *(const struct probe_trace_event **)ptev1; |
| 450 | const struct probe_trace_event *tev2 = |
| 451 | *(const struct probe_trace_event **)ptev2; |
| 452 | |
| 453 | ret = tev2->nargs - tev1->nargs; |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | |
| 457 | for (i = 0; i < tev1->nargs; i++) { |
| 458 | struct probe_trace_arg *arg1, *arg2; |
| 459 | struct probe_trace_arg_ref *ref1, *ref2; |
| 460 | |
| 461 | arg1 = &tev1->args[i]; |
| 462 | arg2 = &tev2->args[i]; |
| 463 | |
| 464 | ret = strcmp(arg1->value, arg2->value); |
| 465 | if (ret) |
| 466 | return ret; |
| 467 | |
| 468 | ref1 = arg1->ref; |
| 469 | ref2 = arg2->ref; |
| 470 | |
| 471 | while (ref1 && ref2) { |
| 472 | ret = ref2->offset - ref1->offset; |
| 473 | if (ret) |
| 474 | return ret; |
| 475 | |
| 476 | ref1 = ref1->next; |
| 477 | ref2 = ref2->next; |
| 478 | } |
| 479 | |
| 480 | if (ref1 || ref2) |
| 481 | return ref2 ? 1 : -1; |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Assign a type number to each tevs in a pev. |
| 489 | * mapping is an array with same slots as tevs in that pev. |
| 490 | * nr_types will be set to number of types. |
| 491 | */ |
| 492 | static int map_prologue(struct perf_probe_event *pev, int *mapping, |
| 493 | int *nr_types) |
| 494 | { |
| 495 | int i, type = 0; |
| 496 | struct probe_trace_event **ptevs; |
| 497 | |
| 498 | size_t array_sz = sizeof(*ptevs) * pev->ntevs; |
| 499 | |
| 500 | ptevs = malloc(array_sz); |
| 501 | if (!ptevs) { |
| 502 | pr_debug("No ehough memory: alloc ptevs failed\n"); |
| 503 | return -ENOMEM; |
| 504 | } |
| 505 | |
| 506 | pr_debug("In map_prologue, ntevs=%d\n", pev->ntevs); |
| 507 | for (i = 0; i < pev->ntevs; i++) |
| 508 | ptevs[i] = &pev->tevs[i]; |
| 509 | |
| 510 | qsort(ptevs, pev->ntevs, sizeof(*ptevs), |
| 511 | compare_tev_args); |
| 512 | |
| 513 | for (i = 0; i < pev->ntevs; i++) { |
| 514 | int n; |
| 515 | |
| 516 | n = ptevs[i] - pev->tevs; |
| 517 | if (i == 0) { |
| 518 | mapping[n] = type; |
| 519 | pr_debug("mapping[%d]=%d\n", n, type); |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | if (compare_tev_args(ptevs + i, ptevs + i - 1) == 0) |
| 524 | mapping[n] = type; |
| 525 | else |
| 526 | mapping[n] = ++type; |
| 527 | |
| 528 | pr_debug("mapping[%d]=%d\n", n, mapping[n]); |
| 529 | } |
| 530 | free(ptevs); |
| 531 | *nr_types = type + 1; |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 536 | static int hook_load_preprocessor(struct bpf_program *prog) |
| 537 | { |
| 538 | struct perf_probe_event *pev; |
| 539 | struct bpf_prog_priv *priv; |
| 540 | bool need_prologue = false; |
| 541 | int err, i; |
| 542 | |
| 543 | err = bpf_program__get_private(prog, (void **)&priv); |
| 544 | if (err || !priv) { |
| 545 | pr_debug("Internal error when hook preprocessor\n"); |
| 546 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 547 | } |
| 548 | |
| 549 | pev = &priv->pev; |
| 550 | for (i = 0; i < pev->ntevs; i++) { |
| 551 | struct probe_trace_event *tev = &pev->tevs[i]; |
| 552 | |
| 553 | if (tev->nargs > 0) { |
| 554 | need_prologue = true; |
| 555 | break; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Since all tevs don't have argument, we don't need generate |
| 561 | * prologue. |
| 562 | */ |
| 563 | if (!need_prologue) { |
| 564 | priv->need_prologue = false; |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | priv->need_prologue = true; |
| 569 | priv->insns_buf = malloc(sizeof(struct bpf_insn) * BPF_MAXINSNS); |
| 570 | if (!priv->insns_buf) { |
| 571 | pr_debug("No enough memory: alloc insns_buf failed\n"); |
| 572 | return -ENOMEM; |
| 573 | } |
| 574 | |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 575 | priv->type_mapping = malloc(sizeof(int) * pev->ntevs); |
| 576 | if (!priv->type_mapping) { |
| 577 | pr_debug("No enough memory: alloc type_mapping failed\n"); |
| 578 | return -ENOMEM; |
| 579 | } |
| 580 | memset(priv->type_mapping, -1, |
| 581 | sizeof(int) * pev->ntevs); |
| 582 | |
| 583 | err = map_prologue(pev, priv->type_mapping, &priv->nr_types); |
| 584 | if (err) |
| 585 | return err; |
| 586 | |
| 587 | err = bpf_program__set_prep(prog, priv->nr_types, |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 588 | preproc_gen_prologue); |
| 589 | return err; |
| 590 | } |
| 591 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 592 | int bpf__probe(struct bpf_object *obj) |
| 593 | { |
| 594 | int err = 0; |
| 595 | struct bpf_program *prog; |
| 596 | struct bpf_prog_priv *priv; |
| 597 | struct perf_probe_event *pev; |
| 598 | |
| 599 | err = bpf__prepare_probe(); |
| 600 | if (err) { |
| 601 | pr_debug("bpf__prepare_probe failed\n"); |
| 602 | return err; |
| 603 | } |
| 604 | |
| 605 | bpf_object__for_each_program(prog, obj) { |
| 606 | err = config_bpf_program(prog); |
| 607 | if (err) |
| 608 | goto out; |
| 609 | |
| 610 | err = bpf_program__get_private(prog, (void **)&priv); |
| 611 | if (err || !priv) |
| 612 | goto out; |
| 613 | pev = &priv->pev; |
| 614 | |
| 615 | err = convert_perf_probe_events(pev, 1); |
| 616 | if (err < 0) { |
| 617 | pr_debug("bpf_probe: failed to convert perf probe events"); |
| 618 | goto out; |
| 619 | } |
| 620 | |
| 621 | err = apply_perf_probe_events(pev, 1); |
| 622 | if (err < 0) { |
| 623 | pr_debug("bpf_probe: failed to apply perf probe events"); |
| 624 | goto out; |
| 625 | } |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 626 | |
| 627 | /* |
| 628 | * After probing, let's consider prologue, which |
| 629 | * adds program fetcher to BPF programs. |
| 630 | * |
| 631 | * hook_load_preprocessorr() hooks pre-processor |
| 632 | * to bpf_program, let it generate prologue |
| 633 | * dynamically during loading. |
| 634 | */ |
| 635 | err = hook_load_preprocessor(prog); |
| 636 | if (err) |
| 637 | goto out; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 638 | } |
| 639 | out: |
| 640 | return err < 0 ? err : 0; |
| 641 | } |
| 642 | |
| 643 | #define EVENTS_WRITE_BUFSIZE 4096 |
| 644 | int bpf__unprobe(struct bpf_object *obj) |
| 645 | { |
| 646 | int err, ret = 0; |
| 647 | struct bpf_program *prog; |
| 648 | struct bpf_prog_priv *priv; |
| 649 | |
| 650 | bpf_object__for_each_program(prog, obj) { |
| 651 | int i; |
| 652 | |
| 653 | err = bpf_program__get_private(prog, (void **)&priv); |
| 654 | if (err || !priv) |
| 655 | continue; |
| 656 | |
| 657 | for (i = 0; i < priv->pev.ntevs; i++) { |
| 658 | struct probe_trace_event *tev = &priv->pev.tevs[i]; |
| 659 | char name_buf[EVENTS_WRITE_BUFSIZE]; |
| 660 | struct strfilter *delfilter; |
| 661 | |
| 662 | snprintf(name_buf, EVENTS_WRITE_BUFSIZE, |
| 663 | "%s:%s", tev->group, tev->event); |
| 664 | name_buf[EVENTS_WRITE_BUFSIZE - 1] = '\0'; |
| 665 | |
| 666 | delfilter = strfilter__new(name_buf, NULL); |
| 667 | if (!delfilter) { |
| 668 | pr_debug("Failed to create filter for unprobing\n"); |
| 669 | ret = -ENOMEM; |
| 670 | continue; |
| 671 | } |
| 672 | |
| 673 | err = del_perf_probe_events(delfilter); |
| 674 | strfilter__delete(delfilter); |
| 675 | if (err) { |
| 676 | pr_debug("Failed to delete %s\n", name_buf); |
| 677 | ret = err; |
| 678 | continue; |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | return ret; |
| 683 | } |
| 684 | |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 685 | int bpf__load(struct bpf_object *obj) |
| 686 | { |
| 687 | int err; |
| 688 | |
| 689 | err = bpf_object__load(obj); |
| 690 | if (err) { |
| 691 | pr_debug("bpf: load objects failed\n"); |
| 692 | return err; |
| 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 697 | int bpf__foreach_tev(struct bpf_object *obj, |
| 698 | bpf_prog_iter_callback_t func, |
| 699 | void *arg) |
| 700 | { |
| 701 | struct bpf_program *prog; |
| 702 | int err; |
| 703 | |
| 704 | bpf_object__for_each_program(prog, obj) { |
| 705 | struct probe_trace_event *tev; |
| 706 | struct perf_probe_event *pev; |
| 707 | struct bpf_prog_priv *priv; |
| 708 | int i, fd; |
| 709 | |
| 710 | err = bpf_program__get_private(prog, |
| 711 | (void **)&priv); |
| 712 | if (err || !priv) { |
| 713 | pr_debug("bpf: failed to get private field\n"); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 714 | return -BPF_LOADER_ERRNO__INTERNAL; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | pev = &priv->pev; |
| 718 | for (i = 0; i < pev->ntevs; i++) { |
| 719 | tev = &pev->tevs[i]; |
| 720 | |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 721 | if (priv->need_prologue) { |
| 722 | int type = priv->type_mapping[i]; |
| 723 | |
| 724 | fd = bpf_program__nth_fd(prog, type); |
| 725 | } else { |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 726 | fd = bpf_program__fd(prog); |
Wang Nan | d35b328 | 2015-11-17 08:32:47 +0000 | [diff] [blame] | 727 | } |
Wang Nan | a08357d | 2015-11-16 12:10:13 +0000 | [diff] [blame] | 728 | |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 729 | if (fd < 0) { |
| 730 | pr_debug("bpf: failed to get file descriptor\n"); |
| 731 | return fd; |
| 732 | } |
| 733 | |
| 734 | err = (*func)(tev, fd, arg); |
| 735 | if (err) { |
| 736 | pr_debug("bpf: call back failed, stop iterate\n"); |
| 737 | return err; |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | return 0; |
| 742 | } |
| 743 | |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 744 | enum bpf_map_op_type { |
| 745 | BPF_MAP_OP_SET_VALUE, |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 746 | BPF_MAP_OP_SET_EVSEL, |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 747 | }; |
| 748 | |
| 749 | enum bpf_map_key_type { |
| 750 | BPF_MAP_KEY_ALL, |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 751 | BPF_MAP_KEY_RANGES, |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 752 | }; |
| 753 | |
| 754 | struct bpf_map_op { |
| 755 | struct list_head list; |
| 756 | enum bpf_map_op_type op_type; |
| 757 | enum bpf_map_key_type key_type; |
| 758 | union { |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 759 | struct parse_events_array array; |
| 760 | } k; |
| 761 | union { |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 762 | u64 value; |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 763 | struct perf_evsel *evsel; |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 764 | } v; |
| 765 | }; |
| 766 | |
| 767 | struct bpf_map_priv { |
| 768 | struct list_head ops_list; |
| 769 | }; |
| 770 | |
| 771 | static void |
| 772 | bpf_map_op__delete(struct bpf_map_op *op) |
| 773 | { |
| 774 | if (!list_empty(&op->list)) |
| 775 | list_del(&op->list); |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 776 | if (op->key_type == BPF_MAP_KEY_RANGES) |
| 777 | parse_events__clear_array(&op->k.array); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 778 | free(op); |
| 779 | } |
| 780 | |
| 781 | static void |
| 782 | bpf_map_priv__purge(struct bpf_map_priv *priv) |
| 783 | { |
| 784 | struct bpf_map_op *pos, *n; |
| 785 | |
| 786 | list_for_each_entry_safe(pos, n, &priv->ops_list, list) { |
| 787 | list_del_init(&pos->list); |
| 788 | bpf_map_op__delete(pos); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | static void |
| 793 | bpf_map_priv__clear(struct bpf_map *map __maybe_unused, |
| 794 | void *_priv) |
| 795 | { |
| 796 | struct bpf_map_priv *priv = _priv; |
| 797 | |
| 798 | bpf_map_priv__purge(priv); |
| 799 | free(priv); |
| 800 | } |
| 801 | |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 802 | static int |
| 803 | bpf_map_op_setkey(struct bpf_map_op *op, struct parse_events_term *term) |
| 804 | { |
| 805 | op->key_type = BPF_MAP_KEY_ALL; |
| 806 | if (!term) |
| 807 | return 0; |
| 808 | |
| 809 | if (term->array.nr_ranges) { |
| 810 | size_t memsz = term->array.nr_ranges * |
| 811 | sizeof(op->k.array.ranges[0]); |
| 812 | |
| 813 | op->k.array.ranges = memdup(term->array.ranges, memsz); |
| 814 | if (!op->k.array.ranges) { |
| 815 | pr_debug("No enough memory to alloc indices for map\n"); |
| 816 | return -ENOMEM; |
| 817 | } |
| 818 | op->key_type = BPF_MAP_KEY_RANGES; |
| 819 | op->k.array.nr_ranges = term->array.nr_ranges; |
| 820 | } |
| 821 | return 0; |
| 822 | } |
| 823 | |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 824 | static struct bpf_map_op * |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 825 | bpf_map_op__new(struct parse_events_term *term) |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 826 | { |
| 827 | struct bpf_map_op *op; |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 828 | int err; |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 829 | |
| 830 | op = zalloc(sizeof(*op)); |
| 831 | if (!op) { |
| 832 | pr_debug("Failed to alloc bpf_map_op\n"); |
| 833 | return ERR_PTR(-ENOMEM); |
| 834 | } |
| 835 | INIT_LIST_HEAD(&op->list); |
| 836 | |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 837 | err = bpf_map_op_setkey(op, term); |
| 838 | if (err) { |
| 839 | free(op); |
| 840 | return ERR_PTR(err); |
| 841 | } |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 842 | return op; |
| 843 | } |
| 844 | |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 845 | static struct bpf_map_op * |
| 846 | bpf_map_op__clone(struct bpf_map_op *op) |
| 847 | { |
| 848 | struct bpf_map_op *newop; |
| 849 | |
| 850 | newop = memdup(op, sizeof(*op)); |
| 851 | if (!newop) { |
| 852 | pr_debug("Failed to alloc bpf_map_op\n"); |
| 853 | return NULL; |
| 854 | } |
| 855 | |
| 856 | INIT_LIST_HEAD(&newop->list); |
| 857 | if (op->key_type == BPF_MAP_KEY_RANGES) { |
| 858 | size_t memsz = op->k.array.nr_ranges * |
| 859 | sizeof(op->k.array.ranges[0]); |
| 860 | |
| 861 | newop->k.array.ranges = memdup(op->k.array.ranges, memsz); |
| 862 | if (!newop->k.array.ranges) { |
| 863 | pr_debug("Failed to alloc indices for map\n"); |
| 864 | free(newop); |
| 865 | return NULL; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | return newop; |
| 870 | } |
| 871 | |
| 872 | static struct bpf_map_priv * |
| 873 | bpf_map_priv__clone(struct bpf_map_priv *priv) |
| 874 | { |
| 875 | struct bpf_map_priv *newpriv; |
| 876 | struct bpf_map_op *pos, *newop; |
| 877 | |
| 878 | newpriv = zalloc(sizeof(*newpriv)); |
| 879 | if (!newpriv) { |
| 880 | pr_debug("No enough memory to alloc map private\n"); |
| 881 | return NULL; |
| 882 | } |
| 883 | INIT_LIST_HEAD(&newpriv->ops_list); |
| 884 | |
| 885 | list_for_each_entry(pos, &priv->ops_list, list) { |
| 886 | newop = bpf_map_op__clone(pos); |
| 887 | if (!newop) { |
| 888 | bpf_map_priv__purge(newpriv); |
| 889 | return NULL; |
| 890 | } |
| 891 | list_add_tail(&newop->list, &newpriv->ops_list); |
| 892 | } |
| 893 | |
| 894 | return newpriv; |
| 895 | } |
| 896 | |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 897 | static int |
| 898 | bpf_map__add_op(struct bpf_map *map, struct bpf_map_op *op) |
| 899 | { |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 900 | const char *map_name = bpf_map__name(map); |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 901 | struct bpf_map_priv *priv = bpf_map__priv(map); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 902 | |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 903 | if (IS_ERR(priv)) { |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 904 | pr_debug("Failed to get private from map %s\n", map_name); |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 905 | return PTR_ERR(priv); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | if (!priv) { |
| 909 | priv = zalloc(sizeof(*priv)); |
| 910 | if (!priv) { |
| 911 | pr_debug("No enough memory to alloc map private\n"); |
| 912 | return -ENOMEM; |
| 913 | } |
| 914 | INIT_LIST_HEAD(&priv->ops_list); |
| 915 | |
| 916 | if (bpf_map__set_private(map, priv, bpf_map_priv__clear)) { |
| 917 | free(priv); |
| 918 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | list_add_tail(&op->list, &priv->ops_list); |
| 923 | return 0; |
| 924 | } |
| 925 | |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 926 | static struct bpf_map_op * |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 927 | bpf_map__add_newop(struct bpf_map *map, struct parse_events_term *term) |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 928 | { |
| 929 | struct bpf_map_op *op; |
| 930 | int err; |
| 931 | |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 932 | op = bpf_map_op__new(term); |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 933 | if (IS_ERR(op)) |
| 934 | return op; |
| 935 | |
| 936 | err = bpf_map__add_op(map, op); |
| 937 | if (err) { |
| 938 | bpf_map_op__delete(op); |
| 939 | return ERR_PTR(err); |
| 940 | } |
| 941 | return op; |
| 942 | } |
| 943 | |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 944 | static int |
| 945 | __bpf_map__config_value(struct bpf_map *map, |
| 946 | struct parse_events_term *term) |
| 947 | { |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 948 | struct bpf_map_op *op; |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 949 | const char *map_name = bpf_map__name(map); |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 950 | const struct bpf_map_def *def = bpf_map__def(map); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 951 | |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 952 | if (IS_ERR(def)) { |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 953 | pr_debug("Unable to get map definition from '%s'\n", |
| 954 | map_name); |
| 955 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 956 | } |
| 957 | |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 958 | if (def->type != BPF_MAP_TYPE_ARRAY) { |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 959 | pr_debug("Map %s type is not BPF_MAP_TYPE_ARRAY\n", |
| 960 | map_name); |
| 961 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE; |
| 962 | } |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 963 | if (def->key_size < sizeof(unsigned int)) { |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 964 | pr_debug("Map %s has incorrect key size\n", map_name); |
| 965 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_KEYSIZE; |
| 966 | } |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 967 | switch (def->value_size) { |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 968 | case 1: |
| 969 | case 2: |
| 970 | case 4: |
| 971 | case 8: |
| 972 | break; |
| 973 | default: |
| 974 | pr_debug("Map %s has incorrect value size\n", map_name); |
| 975 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE; |
| 976 | } |
| 977 | |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 978 | op = bpf_map__add_newop(map, term); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 979 | if (IS_ERR(op)) |
| 980 | return PTR_ERR(op); |
| 981 | op->op_type = BPF_MAP_OP_SET_VALUE; |
| 982 | op->v.value = term->val.num; |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 983 | return 0; |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | static int |
| 987 | bpf_map__config_value(struct bpf_map *map, |
| 988 | struct parse_events_term *term, |
| 989 | struct perf_evlist *evlist __maybe_unused) |
| 990 | { |
| 991 | if (!term->err_val) { |
| 992 | pr_debug("Config value not set\n"); |
| 993 | return -BPF_LOADER_ERRNO__OBJCONF_CONF; |
| 994 | } |
| 995 | |
| 996 | if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) { |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 997 | pr_debug("ERROR: wrong value type for 'value'\n"); |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 998 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE; |
| 999 | } |
| 1000 | |
| 1001 | return __bpf_map__config_value(map, term); |
| 1002 | } |
| 1003 | |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1004 | static int |
| 1005 | __bpf_map__config_event(struct bpf_map *map, |
| 1006 | struct parse_events_term *term, |
| 1007 | struct perf_evlist *evlist) |
| 1008 | { |
| 1009 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1010 | const struct bpf_map_def *def; |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1011 | struct bpf_map_op *op; |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 1012 | const char *map_name = bpf_map__name(map); |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1013 | |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1014 | evsel = perf_evlist__find_evsel_by_str(evlist, term->val.str); |
| 1015 | if (!evsel) { |
| 1016 | pr_debug("Event (for '%s') '%s' doesn't exist\n", |
| 1017 | map_name, term->val.str); |
| 1018 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_NOEVT; |
| 1019 | } |
| 1020 | |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1021 | def = bpf_map__def(map); |
| 1022 | if (IS_ERR(def)) { |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1023 | pr_debug("Unable to get map definition from '%s'\n", |
| 1024 | map_name); |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1025 | return PTR_ERR(def); |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * No need to check key_size and value_size: |
| 1030 | * kernel has already checked them. |
| 1031 | */ |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1032 | if (def->type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) { |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1033 | pr_debug("Map %s type is not BPF_MAP_TYPE_PERF_EVENT_ARRAY\n", |
| 1034 | map_name); |
| 1035 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE; |
| 1036 | } |
| 1037 | |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1038 | op = bpf_map__add_newop(map, term); |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1039 | if (IS_ERR(op)) |
| 1040 | return PTR_ERR(op); |
| 1041 | op->op_type = BPF_MAP_OP_SET_EVSEL; |
| 1042 | op->v.evsel = evsel; |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | static int |
| 1047 | bpf_map__config_event(struct bpf_map *map, |
| 1048 | struct parse_events_term *term, |
| 1049 | struct perf_evlist *evlist) |
| 1050 | { |
| 1051 | if (!term->err_val) { |
| 1052 | pr_debug("Config value not set\n"); |
| 1053 | return -BPF_LOADER_ERRNO__OBJCONF_CONF; |
| 1054 | } |
| 1055 | |
| 1056 | if (term->type_val != PARSE_EVENTS__TERM_TYPE_STR) { |
| 1057 | pr_debug("ERROR: wrong value type for 'event'\n"); |
| 1058 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE; |
| 1059 | } |
| 1060 | |
| 1061 | return __bpf_map__config_event(map, term, evlist); |
| 1062 | } |
| 1063 | |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 1064 | struct bpf_obj_config__map_func { |
| 1065 | const char *config_opt; |
| 1066 | int (*config_func)(struct bpf_map *, struct parse_events_term *, |
| 1067 | struct perf_evlist *); |
| 1068 | }; |
| 1069 | |
| 1070 | struct bpf_obj_config__map_func bpf_obj_config__map_funcs[] = { |
| 1071 | {"value", bpf_map__config_value}, |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1072 | {"event", bpf_map__config_event}, |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 1073 | }; |
| 1074 | |
| 1075 | static int |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1076 | config_map_indices_range_check(struct parse_events_term *term, |
| 1077 | struct bpf_map *map, |
| 1078 | const char *map_name) |
| 1079 | { |
| 1080 | struct parse_events_array *array = &term->array; |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1081 | const struct bpf_map_def *def; |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1082 | unsigned int i; |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1083 | |
| 1084 | if (!array->nr_ranges) |
| 1085 | return 0; |
| 1086 | if (!array->ranges) { |
| 1087 | pr_debug("ERROR: map %s: array->nr_ranges is %d but range array is NULL\n", |
| 1088 | map_name, (int)array->nr_ranges); |
| 1089 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 1090 | } |
| 1091 | |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1092 | def = bpf_map__def(map); |
| 1093 | if (IS_ERR(def)) { |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1094 | pr_debug("ERROR: Unable to get map definition from '%s'\n", |
| 1095 | map_name); |
| 1096 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 1097 | } |
| 1098 | |
| 1099 | for (i = 0; i < array->nr_ranges; i++) { |
| 1100 | unsigned int start = array->ranges[i].start; |
| 1101 | size_t length = array->ranges[i].length; |
| 1102 | unsigned int idx = start + length - 1; |
| 1103 | |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1104 | if (idx >= def->max_entries) { |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1105 | pr_debug("ERROR: index %d too large\n", idx); |
| 1106 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_IDX2BIG; |
| 1107 | } |
| 1108 | } |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static int |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 1113 | bpf__obj_config_map(struct bpf_object *obj, |
| 1114 | struct parse_events_term *term, |
| 1115 | struct perf_evlist *evlist, |
| 1116 | int *key_scan_pos) |
| 1117 | { |
| 1118 | /* key is "map:<mapname>.<config opt>" */ |
| 1119 | char *map_name = strdup(term->config + sizeof("map:") - 1); |
| 1120 | struct bpf_map *map; |
| 1121 | int err = -BPF_LOADER_ERRNO__OBJCONF_OPT; |
| 1122 | char *map_opt; |
| 1123 | size_t i; |
| 1124 | |
| 1125 | if (!map_name) |
| 1126 | return -ENOMEM; |
| 1127 | |
| 1128 | map_opt = strchr(map_name, '.'); |
| 1129 | if (!map_opt) { |
| 1130 | pr_debug("ERROR: Invalid map config: %s\n", map_name); |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
| 1134 | *map_opt++ = '\0'; |
| 1135 | if (*map_opt == '\0') { |
| 1136 | pr_debug("ERROR: Invalid map option: %s\n", term->config); |
| 1137 | goto out; |
| 1138 | } |
| 1139 | |
| 1140 | map = bpf_object__get_map_by_name(obj, map_name); |
| 1141 | if (!map) { |
| 1142 | pr_debug("ERROR: Map %s doesn't exist\n", map_name); |
| 1143 | err = -BPF_LOADER_ERRNO__OBJCONF_MAP_NOTEXIST; |
| 1144 | goto out; |
| 1145 | } |
| 1146 | |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1147 | *key_scan_pos += strlen(map_opt); |
| 1148 | err = config_map_indices_range_check(term, map, map_name); |
| 1149 | if (err) |
| 1150 | goto out; |
| 1151 | *key_scan_pos -= strlen(map_opt); |
| 1152 | |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 1153 | for (i = 0; i < ARRAY_SIZE(bpf_obj_config__map_funcs); i++) { |
| 1154 | struct bpf_obj_config__map_func *func = |
| 1155 | &bpf_obj_config__map_funcs[i]; |
| 1156 | |
| 1157 | if (strcmp(map_opt, func->config_opt) == 0) { |
| 1158 | err = func->config_func(map, term, evlist); |
| 1159 | goto out; |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | pr_debug("ERROR: Invalid map config option '%s'\n", map_opt); |
| 1164 | err = -BPF_LOADER_ERRNO__OBJCONF_MAP_OPT; |
| 1165 | out: |
| 1166 | free(map_name); |
| 1167 | if (!err) |
| 1168 | key_scan_pos += strlen(map_opt); |
| 1169 | return err; |
| 1170 | } |
| 1171 | |
| 1172 | int bpf__config_obj(struct bpf_object *obj, |
| 1173 | struct parse_events_term *term, |
| 1174 | struct perf_evlist *evlist, |
| 1175 | int *error_pos) |
| 1176 | { |
| 1177 | int key_scan_pos = 0; |
| 1178 | int err; |
| 1179 | |
| 1180 | if (!obj || !term || !term->config) |
| 1181 | return -EINVAL; |
| 1182 | |
| 1183 | if (!prefixcmp(term->config, "map:")) { |
| 1184 | key_scan_pos = sizeof("map:") - 1; |
| 1185 | err = bpf__obj_config_map(obj, term, evlist, &key_scan_pos); |
| 1186 | goto out; |
| 1187 | } |
| 1188 | err = -BPF_LOADER_ERRNO__OBJCONF_OPT; |
| 1189 | out: |
| 1190 | if (error_pos) |
| 1191 | *error_pos = key_scan_pos; |
| 1192 | return err; |
| 1193 | |
| 1194 | } |
| 1195 | |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1196 | typedef int (*map_config_func_t)(const char *name, int map_fd, |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1197 | const struct bpf_map_def *pdef, |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1198 | struct bpf_map_op *op, |
| 1199 | void *pkey, void *arg); |
| 1200 | |
| 1201 | static int |
| 1202 | foreach_key_array_all(map_config_func_t func, |
| 1203 | void *arg, const char *name, |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1204 | int map_fd, const struct bpf_map_def *pdef, |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1205 | struct bpf_map_op *op) |
| 1206 | { |
| 1207 | unsigned int i; |
| 1208 | int err; |
| 1209 | |
| 1210 | for (i = 0; i < pdef->max_entries; i++) { |
| 1211 | err = func(name, map_fd, pdef, op, &i, arg); |
| 1212 | if (err) { |
| 1213 | pr_debug("ERROR: failed to insert value to %s[%u]\n", |
| 1214 | name, i); |
| 1215 | return err; |
| 1216 | } |
| 1217 | } |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | static int |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1222 | foreach_key_array_ranges(map_config_func_t func, void *arg, |
| 1223 | const char *name, int map_fd, |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1224 | const struct bpf_map_def *pdef, |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1225 | struct bpf_map_op *op) |
| 1226 | { |
| 1227 | unsigned int i, j; |
| 1228 | int err; |
| 1229 | |
| 1230 | for (i = 0; i < op->k.array.nr_ranges; i++) { |
| 1231 | unsigned int start = op->k.array.ranges[i].start; |
| 1232 | size_t length = op->k.array.ranges[i].length; |
| 1233 | |
| 1234 | for (j = 0; j < length; j++) { |
| 1235 | unsigned int idx = start + j; |
| 1236 | |
| 1237 | err = func(name, map_fd, pdef, op, &idx, arg); |
| 1238 | if (err) { |
| 1239 | pr_debug("ERROR: failed to insert value to %s[%u]\n", |
| 1240 | name, idx); |
| 1241 | return err; |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | static int |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1249 | bpf_map_config_foreach_key(struct bpf_map *map, |
| 1250 | map_config_func_t func, |
| 1251 | void *arg) |
| 1252 | { |
| 1253 | int err, map_fd; |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1254 | struct bpf_map_op *op; |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1255 | const struct bpf_map_def *def; |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 1256 | const char *name = bpf_map__name(map); |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 1257 | struct bpf_map_priv *priv = bpf_map__priv(map); |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1258 | |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 1259 | if (IS_ERR(priv)) { |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1260 | pr_debug("ERROR: failed to get private from map %s\n", name); |
| 1261 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 1262 | } |
| 1263 | if (!priv || list_empty(&priv->ops_list)) { |
| 1264 | pr_debug("INFO: nothing to config for map %s\n", name); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1268 | def = bpf_map__def(map); |
| 1269 | if (IS_ERR(def)) { |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1270 | pr_debug("ERROR: failed to get definition from map %s\n", name); |
| 1271 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 1272 | } |
| 1273 | map_fd = bpf_map__get_fd(map); |
| 1274 | if (map_fd < 0) { |
| 1275 | pr_debug("ERROR: failed to get fd from map %s\n", name); |
| 1276 | return map_fd; |
| 1277 | } |
| 1278 | |
| 1279 | list_for_each_entry(op, &priv->ops_list, list) { |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1280 | switch (def->type) { |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1281 | case BPF_MAP_TYPE_ARRAY: |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1282 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1283 | switch (op->key_type) { |
| 1284 | case BPF_MAP_KEY_ALL: |
| 1285 | err = foreach_key_array_all(func, arg, name, |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1286 | map_fd, def, op); |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1287 | break; |
| 1288 | case BPF_MAP_KEY_RANGES: |
| 1289 | err = foreach_key_array_ranges(func, arg, name, |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1290 | map_fd, def, |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1291 | op); |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1292 | break; |
| 1293 | default: |
| 1294 | pr_debug("ERROR: keytype for map '%s' invalid\n", |
| 1295 | name); |
| 1296 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 1297 | } |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1298 | if (err) |
| 1299 | return err; |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1300 | break; |
| 1301 | default: |
| 1302 | pr_debug("ERROR: type of '%s' incorrect\n", name); |
| 1303 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | static int |
| 1311 | apply_config_value_for_key(int map_fd, void *pkey, |
| 1312 | size_t val_size, u64 val) |
| 1313 | { |
| 1314 | int err = 0; |
| 1315 | |
| 1316 | switch (val_size) { |
| 1317 | case 1: { |
| 1318 | u8 _val = (u8)(val); |
| 1319 | err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY); |
| 1320 | break; |
| 1321 | } |
| 1322 | case 2: { |
| 1323 | u16 _val = (u16)(val); |
| 1324 | err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY); |
| 1325 | break; |
| 1326 | } |
| 1327 | case 4: { |
| 1328 | u32 _val = (u32)(val); |
| 1329 | err = bpf_map_update_elem(map_fd, pkey, &_val, BPF_ANY); |
| 1330 | break; |
| 1331 | } |
| 1332 | case 8: { |
| 1333 | err = bpf_map_update_elem(map_fd, pkey, &val, BPF_ANY); |
| 1334 | break; |
| 1335 | } |
| 1336 | default: |
| 1337 | pr_debug("ERROR: invalid value size\n"); |
| 1338 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUESIZE; |
| 1339 | } |
| 1340 | if (err && errno) |
| 1341 | err = -errno; |
| 1342 | return err; |
| 1343 | } |
| 1344 | |
| 1345 | static int |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1346 | apply_config_evsel_for_key(const char *name, int map_fd, void *pkey, |
| 1347 | struct perf_evsel *evsel) |
| 1348 | { |
| 1349 | struct xyarray *xy = evsel->fd; |
| 1350 | struct perf_event_attr *attr; |
| 1351 | unsigned int key, events; |
| 1352 | bool check_pass = false; |
| 1353 | int *evt_fd; |
| 1354 | int err; |
| 1355 | |
| 1356 | if (!xy) { |
| 1357 | pr_debug("ERROR: evsel not ready for map %s\n", name); |
| 1358 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 1359 | } |
| 1360 | |
| 1361 | if (xy->row_size / xy->entry_size != 1) { |
| 1362 | pr_debug("ERROR: Dimension of target event is incorrect for map %s\n", |
| 1363 | name); |
| 1364 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM; |
| 1365 | } |
| 1366 | |
| 1367 | attr = &evsel->attr; |
| 1368 | if (attr->inherit) { |
| 1369 | pr_debug("ERROR: Can't put inherit event into map %s\n", name); |
| 1370 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH; |
| 1371 | } |
| 1372 | |
Wang Nan | 03e0a7d | 2016-02-22 09:10:37 +0000 | [diff] [blame] | 1373 | if (perf_evsel__is_bpf_output(evsel)) |
| 1374 | check_pass = true; |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1375 | if (attr->type == PERF_TYPE_RAW) |
| 1376 | check_pass = true; |
| 1377 | if (attr->type == PERF_TYPE_HARDWARE) |
| 1378 | check_pass = true; |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1379 | if (!check_pass) { |
| 1380 | pr_debug("ERROR: Event type is wrong for map %s\n", name); |
| 1381 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE; |
| 1382 | } |
| 1383 | |
| 1384 | events = xy->entries / (xy->row_size / xy->entry_size); |
| 1385 | key = *((unsigned int *)pkey); |
| 1386 | if (key >= events) { |
| 1387 | pr_debug("ERROR: there is no event %d for map %s\n", |
| 1388 | key, name); |
| 1389 | return -BPF_LOADER_ERRNO__OBJCONF_MAP_MAPSIZE; |
| 1390 | } |
| 1391 | evt_fd = xyarray__entry(xy, key, 0); |
| 1392 | err = bpf_map_update_elem(map_fd, pkey, evt_fd, BPF_ANY); |
| 1393 | if (err && errno) |
| 1394 | err = -errno; |
| 1395 | return err; |
| 1396 | } |
| 1397 | |
| 1398 | static int |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1399 | apply_obj_config_map_for_key(const char *name, int map_fd, |
Arnaldo Carvalho de Melo | 53897a7 | 2016-06-02 14:21:06 -0300 | [diff] [blame^] | 1400 | const struct bpf_map_def *pdef, |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1401 | struct bpf_map_op *op, |
| 1402 | void *pkey, void *arg __maybe_unused) |
| 1403 | { |
| 1404 | int err; |
| 1405 | |
| 1406 | switch (op->op_type) { |
| 1407 | case BPF_MAP_OP_SET_VALUE: |
| 1408 | err = apply_config_value_for_key(map_fd, pkey, |
| 1409 | pdef->value_size, |
| 1410 | op->v.value); |
| 1411 | break; |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1412 | case BPF_MAP_OP_SET_EVSEL: |
| 1413 | err = apply_config_evsel_for_key(name, map_fd, pkey, |
| 1414 | op->v.evsel); |
| 1415 | break; |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1416 | default: |
| 1417 | pr_debug("ERROR: unknown value type for '%s'\n", name); |
| 1418 | err = -BPF_LOADER_ERRNO__INTERNAL; |
| 1419 | } |
| 1420 | return err; |
| 1421 | } |
| 1422 | |
| 1423 | static int |
| 1424 | apply_obj_config_map(struct bpf_map *map) |
| 1425 | { |
| 1426 | return bpf_map_config_foreach_key(map, |
| 1427 | apply_obj_config_map_for_key, |
| 1428 | NULL); |
| 1429 | } |
| 1430 | |
| 1431 | static int |
| 1432 | apply_obj_config_object(struct bpf_object *obj) |
| 1433 | { |
| 1434 | struct bpf_map *map; |
| 1435 | int err; |
| 1436 | |
| 1437 | bpf_map__for_each(map, obj) { |
| 1438 | err = apply_obj_config_map(map); |
| 1439 | if (err) |
| 1440 | return err; |
| 1441 | } |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | int bpf__apply_obj_config(void) |
| 1446 | { |
| 1447 | struct bpf_object *obj, *tmp; |
| 1448 | int err; |
| 1449 | |
| 1450 | bpf_object__for_each_safe(obj, tmp) { |
| 1451 | err = apply_obj_config_object(obj); |
| 1452 | if (err) |
| 1453 | return err; |
| 1454 | } |
| 1455 | |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1459 | #define bpf__for_each_map(pos, obj, objtmp) \ |
| 1460 | bpf_object__for_each_safe(obj, objtmp) \ |
| 1461 | bpf_map__for_each(pos, obj) |
| 1462 | |
| 1463 | #define bpf__for_each_stdout_map(pos, obj, objtmp) \ |
| 1464 | bpf__for_each_map(pos, obj, objtmp) \ |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 1465 | if (bpf_map__name(pos) && \ |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1466 | (strcmp("__bpf_stdout__", \ |
Arnaldo Carvalho de Melo | 009ad5d | 2016-06-02 11:02:05 -0300 | [diff] [blame] | 1467 | bpf_map__name(pos)) == 0)) |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1468 | |
| 1469 | int bpf__setup_stdout(struct perf_evlist *evlist __maybe_unused) |
| 1470 | { |
| 1471 | struct bpf_map_priv *tmpl_priv = NULL; |
| 1472 | struct bpf_object *obj, *tmp; |
Wang Nan | 72c0809 | 2016-04-08 15:07:25 +0000 | [diff] [blame] | 1473 | struct perf_evsel *evsel = NULL; |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1474 | struct bpf_map *map; |
| 1475 | int err; |
| 1476 | bool need_init = false; |
| 1477 | |
| 1478 | bpf__for_each_stdout_map(map, obj, tmp) { |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 1479 | struct bpf_map_priv *priv = bpf_map__priv(map); |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1480 | |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 1481 | if (IS_ERR(priv)) |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1482 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 1483 | |
| 1484 | /* |
| 1485 | * No need to check map type: type should have been |
| 1486 | * verified by kernel. |
| 1487 | */ |
| 1488 | if (!need_init && !priv) |
| 1489 | need_init = !priv; |
| 1490 | if (!tmpl_priv && priv) |
| 1491 | tmpl_priv = priv; |
| 1492 | } |
| 1493 | |
| 1494 | if (!need_init) |
| 1495 | return 0; |
| 1496 | |
Wang Nan | 72c0809 | 2016-04-08 15:07:25 +0000 | [diff] [blame] | 1497 | if (!tmpl_priv) { |
| 1498 | err = parse_events(evlist, "bpf-output/no-inherit=1,name=__bpf_stdout__/", |
| 1499 | NULL); |
| 1500 | if (err) { |
| 1501 | pr_debug("ERROR: failed to create bpf-output event\n"); |
| 1502 | return -err; |
| 1503 | } |
| 1504 | |
| 1505 | evsel = perf_evlist__last(evlist); |
| 1506 | } |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1507 | |
| 1508 | bpf__for_each_stdout_map(map, obj, tmp) { |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 1509 | struct bpf_map_priv *priv = bpf_map__priv(map); |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1510 | |
Arnaldo Carvalho de Melo | b4cbfa5 | 2016-06-02 10:51:59 -0300 | [diff] [blame] | 1511 | if (IS_ERR(priv)) |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1512 | return -BPF_LOADER_ERRNO__INTERNAL; |
| 1513 | if (priv) |
| 1514 | continue; |
| 1515 | |
Wang Nan | 72c0809 | 2016-04-08 15:07:25 +0000 | [diff] [blame] | 1516 | if (tmpl_priv) { |
| 1517 | priv = bpf_map_priv__clone(tmpl_priv); |
| 1518 | if (!priv) |
| 1519 | return -ENOMEM; |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1520 | |
Wang Nan | 72c0809 | 2016-04-08 15:07:25 +0000 | [diff] [blame] | 1521 | err = bpf_map__set_private(map, priv, bpf_map_priv__clear); |
| 1522 | if (err) { |
| 1523 | bpf_map_priv__clear(map, priv); |
| 1524 | return err; |
| 1525 | } |
| 1526 | } else if (evsel) { |
| 1527 | struct bpf_map_op *op; |
| 1528 | |
| 1529 | op = bpf_map__add_newop(map, NULL); |
| 1530 | if (IS_ERR(op)) |
| 1531 | return PTR_ERR(op); |
| 1532 | op->op_type = BPF_MAP_OP_SET_EVSEL; |
| 1533 | op->v.evsel = evsel; |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 1540 | #define ERRNO_OFFSET(e) ((e) - __BPF_LOADER_ERRNO__START) |
| 1541 | #define ERRCODE_OFFSET(c) ERRNO_OFFSET(BPF_LOADER_ERRNO__##c) |
| 1542 | #define NR_ERRNO (__BPF_LOADER_ERRNO__END - __BPF_LOADER_ERRNO__START) |
| 1543 | |
| 1544 | static const char *bpf_loader_strerror_table[NR_ERRNO] = { |
| 1545 | [ERRCODE_OFFSET(CONFIG)] = "Invalid config string", |
| 1546 | [ERRCODE_OFFSET(GROUP)] = "Invalid group name", |
| 1547 | [ERRCODE_OFFSET(EVENTNAME)] = "No event name found in config string", |
| 1548 | [ERRCODE_OFFSET(INTERNAL)] = "BPF loader internal error", |
| 1549 | [ERRCODE_OFFSET(COMPILE)] = "Error when compiling BPF scriptlet", |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 1550 | [ERRCODE_OFFSET(PROGCONF_TERM)] = "Invalid program config term in config string", |
He Kuang | bfc077b | 2015-11-16 12:10:12 +0000 | [diff] [blame] | 1551 | [ERRCODE_OFFSET(PROLOGUE)] = "Failed to generate prologue", |
| 1552 | [ERRCODE_OFFSET(PROLOGUE2BIG)] = "Prologue too big for program", |
| 1553 | [ERRCODE_OFFSET(PROLOGUEOOB)] = "Offset out of bound for prologue", |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 1554 | [ERRCODE_OFFSET(OBJCONF_OPT)] = "Invalid object config option", |
| 1555 | [ERRCODE_OFFSET(OBJCONF_CONF)] = "Config value not set (missing '=')", |
| 1556 | [ERRCODE_OFFSET(OBJCONF_MAP_OPT)] = "Invalid object map config option", |
| 1557 | [ERRCODE_OFFSET(OBJCONF_MAP_NOTEXIST)] = "Target map doesn't exist", |
| 1558 | [ERRCODE_OFFSET(OBJCONF_MAP_VALUE)] = "Incorrect value type for map", |
| 1559 | [ERRCODE_OFFSET(OBJCONF_MAP_TYPE)] = "Incorrect map type", |
| 1560 | [ERRCODE_OFFSET(OBJCONF_MAP_KEYSIZE)] = "Incorrect map key size", |
| 1561 | [ERRCODE_OFFSET(OBJCONF_MAP_VALUESIZE)] = "Incorrect map value size", |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1562 | [ERRCODE_OFFSET(OBJCONF_MAP_NOEVT)] = "Event not found for map setting", |
| 1563 | [ERRCODE_OFFSET(OBJCONF_MAP_MAPSIZE)] = "Invalid map size for event setting", |
| 1564 | [ERRCODE_OFFSET(OBJCONF_MAP_EVTDIM)] = "Event dimension too large", |
| 1565 | [ERRCODE_OFFSET(OBJCONF_MAP_EVTINH)] = "Doesn't support inherit event", |
| 1566 | [ERRCODE_OFFSET(OBJCONF_MAP_EVTTYPE)] = "Wrong event type for map", |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 1567 | [ERRCODE_OFFSET(OBJCONF_MAP_IDX2BIG)] = "Index too large", |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 1568 | }; |
| 1569 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1570 | static int |
| 1571 | bpf_loader_strerror(int err, char *buf, size_t size) |
| 1572 | { |
| 1573 | char sbuf[STRERR_BUFSIZE]; |
| 1574 | const char *msg; |
| 1575 | |
| 1576 | if (!buf || !size) |
| 1577 | return -1; |
| 1578 | |
| 1579 | err = err > 0 ? err : -err; |
| 1580 | |
| 1581 | if (err >= __LIBBPF_ERRNO__START) |
| 1582 | return libbpf_strerror(err, buf, size); |
| 1583 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 1584 | if (err >= __BPF_LOADER_ERRNO__START && err < __BPF_LOADER_ERRNO__END) { |
| 1585 | msg = bpf_loader_strerror_table[ERRNO_OFFSET(err)]; |
| 1586 | snprintf(buf, size, "%s", msg); |
| 1587 | buf[size - 1] = '\0'; |
| 1588 | return 0; |
| 1589 | } |
| 1590 | |
| 1591 | if (err >= __BPF_LOADER_ERRNO__END) |
| 1592 | snprintf(buf, size, "Unknown bpf loader error %d", err); |
| 1593 | else |
| 1594 | snprintf(buf, size, "%s", |
| 1595 | strerror_r(err, sbuf, sizeof(sbuf))); |
| 1596 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1597 | buf[size - 1] = '\0'; |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 1598 | return -1; |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 1601 | #define bpf__strerror_head(err, buf, size) \ |
| 1602 | char sbuf[STRERR_BUFSIZE], *emsg;\ |
| 1603 | if (!size)\ |
| 1604 | return 0;\ |
| 1605 | if (err < 0)\ |
| 1606 | err = -err;\ |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 1607 | bpf_loader_strerror(err, sbuf, sizeof(sbuf));\ |
| 1608 | emsg = sbuf;\ |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 1609 | switch (err) {\ |
| 1610 | default:\ |
| 1611 | scnprintf(buf, size, "%s", emsg);\ |
| 1612 | break; |
| 1613 | |
| 1614 | #define bpf__strerror_entry(val, fmt...)\ |
| 1615 | case val: {\ |
| 1616 | scnprintf(buf, size, fmt);\ |
| 1617 | break;\ |
| 1618 | } |
| 1619 | |
| 1620 | #define bpf__strerror_end(buf, size)\ |
| 1621 | }\ |
| 1622 | buf[size - 1] = '\0'; |
| 1623 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 1624 | int bpf__strerror_prepare_load(const char *filename, bool source, |
| 1625 | int err, char *buf, size_t size) |
| 1626 | { |
| 1627 | size_t n; |
| 1628 | int ret; |
| 1629 | |
| 1630 | n = snprintf(buf, size, "Failed to load %s%s: ", |
| 1631 | filename, source ? " from source" : ""); |
| 1632 | if (n >= size) { |
| 1633 | buf[size - 1] = '\0'; |
| 1634 | return 0; |
| 1635 | } |
| 1636 | buf += n; |
| 1637 | size -= n; |
| 1638 | |
| 1639 | ret = bpf_loader_strerror(err, buf, size); |
| 1640 | buf[size - 1] = '\0'; |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 1644 | int bpf__strerror_probe(struct bpf_object *obj __maybe_unused, |
| 1645 | int err, char *buf, size_t size) |
| 1646 | { |
| 1647 | bpf__strerror_head(err, buf, size); |
Wang Nan | 0bb9349 | 2015-11-27 08:47:37 +0000 | [diff] [blame] | 1648 | case BPF_LOADER_ERRNO__PROGCONF_TERM: { |
Wang Nan | 361f2b1 | 2015-11-16 12:10:05 +0000 | [diff] [blame] | 1649 | scnprintf(buf, size, "%s (add -v to see detail)", emsg); |
| 1650 | break; |
| 1651 | } |
Wang Nan | 03e01f5 | 2015-11-16 12:10:08 +0000 | [diff] [blame] | 1652 | bpf__strerror_entry(EEXIST, "Probe point exist. Try 'perf probe -d \"*\"' and set 'force=yes'"); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 1653 | bpf__strerror_entry(EACCES, "You need to be root"); |
| 1654 | bpf__strerror_entry(EPERM, "You need to be root, and /proc/sys/kernel/kptr_restrict should be 0"); |
| 1655 | bpf__strerror_entry(ENOENT, "You need to check probing points in BPF file"); |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 1656 | bpf__strerror_end(buf, size); |
| 1657 | return 0; |
Wang Nan | 69d262a | 2015-10-14 12:41:13 +0000 | [diff] [blame] | 1658 | } |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 1659 | |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 1660 | int bpf__strerror_load(struct bpf_object *obj, |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 1661 | int err, char *buf, size_t size) |
| 1662 | { |
| 1663 | bpf__strerror_head(err, buf, size); |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 1664 | case LIBBPF_ERRNO__KVER: { |
| 1665 | unsigned int obj_kver = bpf_object__get_kversion(obj); |
| 1666 | unsigned int real_kver; |
| 1667 | |
| 1668 | if (fetch_kernel_version(&real_kver, NULL, 0)) { |
| 1669 | scnprintf(buf, size, "Unable to fetch kernel version"); |
| 1670 | break; |
| 1671 | } |
| 1672 | |
| 1673 | if (obj_kver != real_kver) { |
| 1674 | scnprintf(buf, size, |
| 1675 | "'version' ("KVER_FMT") doesn't match running kernel ("KVER_FMT")", |
| 1676 | KVER_PARAM(obj_kver), |
| 1677 | KVER_PARAM(real_kver)); |
| 1678 | break; |
| 1679 | } |
| 1680 | |
| 1681 | scnprintf(buf, size, "Failed to load program for unknown reason"); |
| 1682 | break; |
| 1683 | } |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 1684 | bpf__strerror_end(buf, size); |
| 1685 | return 0; |
| 1686 | } |
Wang Nan | 066dacb | 2016-02-22 09:10:30 +0000 | [diff] [blame] | 1687 | |
| 1688 | int bpf__strerror_config_obj(struct bpf_object *obj __maybe_unused, |
| 1689 | struct parse_events_term *term __maybe_unused, |
| 1690 | struct perf_evlist *evlist __maybe_unused, |
| 1691 | int *error_pos __maybe_unused, int err, |
| 1692 | char *buf, size_t size) |
| 1693 | { |
| 1694 | bpf__strerror_head(err, buf, size); |
| 1695 | bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_TYPE, |
| 1696 | "Can't use this config term with this map type"); |
| 1697 | bpf__strerror_end(buf, size); |
| 1698 | return 0; |
| 1699 | } |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1700 | |
| 1701 | int bpf__strerror_apply_obj_config(int err, char *buf, size_t size) |
| 1702 | { |
| 1703 | bpf__strerror_head(err, buf, size); |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 1704 | bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTDIM, |
| 1705 | "Cannot set event to BPF map in multi-thread tracing"); |
| 1706 | bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTINH, |
| 1707 | "%s (Hint: use -i to turn off inherit)", emsg); |
| 1708 | bpf__strerror_entry(BPF_LOADER_ERRNO__OBJCONF_MAP_EVTTYPE, |
| 1709 | "Can only put raw, hardware and BPF output event into a BPF map"); |
Wang Nan | 8690a2a | 2016-02-22 09:10:32 +0000 | [diff] [blame] | 1710 | bpf__strerror_end(buf, size); |
| 1711 | return 0; |
| 1712 | } |
Wang Nan | d788857 | 2016-04-08 15:07:24 +0000 | [diff] [blame] | 1713 | |
| 1714 | int bpf__strerror_setup_stdout(struct perf_evlist *evlist __maybe_unused, |
| 1715 | int err, char *buf, size_t size) |
| 1716 | { |
| 1717 | bpf__strerror_head(err, buf, size); |
| 1718 | bpf__strerror_end(buf, size); |
| 1719 | return 0; |
| 1720 | } |