at commit afb1ecfe
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Common code for probe-based Dynamic events. |
| 4 | * |
| 5 | * This code was copied from kernel/trace/trace_kprobe.c written by |
| 6 | * Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> |
| 7 | * |
| 8 | * Updates to make this generic: |
| 9 | * Copyright (C) IBM Corporation, 2010-2011 |
| 10 | * Author: Srikar Dronamraju |
| 11 | */ |
| 12 | #define pr_fmt(fmt) "trace_probe: " fmt |
| 13 | |
| 14 | #include <linux/bpf.h> |
| 15 | #include <linux/fs.h> |
| 16 | |
| 17 | #include "trace_btf.h" |
| 18 | #include "trace_probe.h" |
| 19 | |
| 20 | #undef C |
| 21 | #define C(a, b) b |
| 22 | |
| 23 | static const char *trace_probe_err_text[] = { ERRORS }; |
| 24 | |
| 25 | static const char *reserved_field_names[] = { |
| 26 | "common_type", |
| 27 | "common_flags", |
| 28 | "common_preempt_count", |
| 29 | "common_pid", |
| 30 | "common_tgid", |
| 31 | FIELD_STRING_IP, |
| 32 | FIELD_STRING_RETIP, |
| 33 | FIELD_STRING_FUNC, |
| 34 | }; |
| 35 | |
| 36 | /* Printing in basic type function template */ |
| 37 | #define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \ |
| 38 | int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\ |
| 39 | { \ |
| 40 | trace_seq_printf(s, fmt, *(type *)data); \ |
| 41 | return !trace_seq_has_overflowed(s); \ |
| 42 | } \ |
| 43 | const char PRINT_TYPE_FMT_NAME(tname)[] = fmt; |
| 44 | |
| 45 | DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u") |
| 46 | DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u") |
| 47 | DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u") |
| 48 | DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu") |
| 49 | DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d") |
| 50 | DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d") |
| 51 | DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d") |
| 52 | DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld") |
| 53 | DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x") |
| 54 | DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x") |
| 55 | DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x") |
| 56 | DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx") |
| 57 | DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'") |
| 58 | |
| 59 | int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent) |
| 60 | { |
| 61 | trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data); |
| 62 | return !trace_seq_has_overflowed(s); |
| 63 | } |
| 64 | const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS"; |
| 65 | |
| 66 | /* Print type function for string type */ |
| 67 | int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent) |
| 68 | { |
| 69 | int len = *(u32 *)data >> 16; |
| 70 | |
| 71 | if (!len) |
| 72 | trace_seq_puts(s, FAULT_STRING); |
| 73 | else |
| 74 | trace_seq_printf(s, "\"%s\"", |
| 75 | (const char *)get_loc_data(data, ent)); |
| 76 | return !trace_seq_has_overflowed(s); |
| 77 | } |
| 78 | |
| 79 | const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; |
| 80 | |
| 81 | /* Fetch type information table */ |
| 82 | static const struct fetch_type probe_fetch_types[] = { |
| 83 | /* Special types */ |
| 84 | __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 1, |
| 85 | "__data_loc char[]"), |
| 86 | __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 1, |
| 87 | "__data_loc char[]"), |
| 88 | __ASSIGN_FETCH_TYPE("symstr", string, string, sizeof(u32), 1, 1, |
| 89 | "__data_loc char[]"), |
| 90 | /* Basic types */ |
| 91 | ASSIGN_FETCH_TYPE(u8, u8, 0), |
| 92 | ASSIGN_FETCH_TYPE(u16, u16, 0), |
| 93 | ASSIGN_FETCH_TYPE(u32, u32, 0), |
| 94 | ASSIGN_FETCH_TYPE(u64, u64, 0), |
| 95 | ASSIGN_FETCH_TYPE(s8, u8, 1), |
| 96 | ASSIGN_FETCH_TYPE(s16, u16, 1), |
| 97 | ASSIGN_FETCH_TYPE(s32, u32, 1), |
| 98 | ASSIGN_FETCH_TYPE(s64, u64, 1), |
| 99 | ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0), |
| 100 | ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0), |
| 101 | ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0), |
| 102 | ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0), |
| 103 | ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8, 0), |
| 104 | ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0), |
| 105 | |
| 106 | ASSIGN_FETCH_TYPE_END |
| 107 | }; |
| 108 | |
| 109 | static const struct fetch_type *find_fetch_type(const char *type, unsigned long flags) |
| 110 | { |
| 111 | int i; |
| 112 | |
| 113 | /* Reject the symbol/symstr for uprobes */ |
| 114 | if (type && (flags & TPARG_FL_USER) && |
| 115 | (!strcmp(type, "symbol") || !strcmp(type, "symstr"))) |
| 116 | return NULL; |
| 117 | |
| 118 | if (!type) |
| 119 | type = DEFAULT_FETCH_TYPE_STR; |
| 120 | |
| 121 | /* Special case: bitfield */ |
| 122 | if (*type == 'b') { |
| 123 | unsigned long bs; |
| 124 | |
| 125 | type = strchr(type, '/'); |
| 126 | if (!type) |
| 127 | goto fail; |
| 128 | |
| 129 | type++; |
| 130 | if (kstrtoul(type, 0, &bs)) |
| 131 | goto fail; |
| 132 | |
| 133 | switch (bs) { |
| 134 | case 8: |
| 135 | return find_fetch_type("u8", flags); |
| 136 | case 16: |
| 137 | return find_fetch_type("u16", flags); |
| 138 | case 32: |
| 139 | return find_fetch_type("u32", flags); |
| 140 | case 64: |
| 141 | return find_fetch_type("u64", flags); |
| 142 | default: |
| 143 | goto fail; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | for (i = 0; probe_fetch_types[i].name; i++) { |
| 148 | if (strcmp(type, probe_fetch_types[i].name) == 0) |
| 149 | return &probe_fetch_types[i]; |
| 150 | } |
| 151 | |
| 152 | fail: |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | static struct trace_probe_log trace_probe_log; |
| 157 | extern struct mutex dyn_event_ops_mutex; |
| 158 | |
| 159 | const char *trace_probe_log_init(const char *subsystem, int argc, const char **argv) |
| 160 | { |
| 161 | lockdep_assert_held(&dyn_event_ops_mutex); |
| 162 | |
| 163 | trace_probe_log.subsystem = subsystem; |
| 164 | trace_probe_log.argc = argc; |
| 165 | trace_probe_log.argv = argv; |
| 166 | trace_probe_log.index = 0; |
| 167 | return subsystem; |
| 168 | } |
| 169 | |
| 170 | void trace_probe_log_clear(void) |
| 171 | { |
| 172 | lockdep_assert_held(&dyn_event_ops_mutex); |
| 173 | |
| 174 | memset(&trace_probe_log, 0, sizeof(trace_probe_log)); |
| 175 | } |
| 176 | |
| 177 | void trace_probe_log_set_index(int index) |
| 178 | { |
| 179 | lockdep_assert_held(&dyn_event_ops_mutex); |
| 180 | |
| 181 | trace_probe_log.index = index; |
| 182 | } |
| 183 | |
| 184 | void __trace_probe_log_err(int offset, int err_type) |
| 185 | { |
| 186 | char *command, *p; |
| 187 | int i, len = 0, pos = 0; |
| 188 | |
| 189 | lockdep_assert_held(&dyn_event_ops_mutex); |
| 190 | |
| 191 | if (!trace_probe_log.argv || !trace_probe_log.argc) |
| 192 | return; |
| 193 | |
| 194 | /* Recalculate the length and allocate buffer */ |
| 195 | for (i = 0; i < trace_probe_log.argc; i++) { |
| 196 | if (i == trace_probe_log.index) |
| 197 | pos = len; |
| 198 | len += strlen(trace_probe_log.argv[i]) + 1; |
| 199 | } |
| 200 | command = kzalloc(len, GFP_KERNEL); |
| 201 | if (!command) |
| 202 | return; |
| 203 | |
| 204 | if (trace_probe_log.index >= trace_probe_log.argc) { |
| 205 | /** |
| 206 | * Set the error position is next to the last arg + space. |
| 207 | * Note that len includes the terminal null and the cursor |
| 208 | * appears at pos + 1. |
| 209 | */ |
| 210 | pos = len; |
| 211 | offset = 0; |
| 212 | } |
| 213 | |
| 214 | /* And make a command string from argv array */ |
| 215 | p = command; |
| 216 | for (i = 0; i < trace_probe_log.argc; i++) { |
| 217 | len = strlen(trace_probe_log.argv[i]); |
| 218 | memcpy(p, trace_probe_log.argv[i], len); |
| 219 | p[len] = ' '; |
| 220 | p += len + 1; |
| 221 | } |
| 222 | *(p - 1) = '\0'; |
| 223 | |
| 224 | tracing_log_err(NULL, trace_probe_log.subsystem, command, |
| 225 | trace_probe_err_text, err_type, pos + offset); |
| 226 | |
| 227 | kfree(command); |
| 228 | } |
| 229 | |
| 230 | /* Split symbol and offset. */ |
| 231 | int traceprobe_split_symbol_offset(char *symbol, long *offset) |
| 232 | { |
| 233 | char *tmp; |
| 234 | int ret; |
| 235 | |
| 236 | if (!offset) |
| 237 | return -EINVAL; |
| 238 | |
| 239 | tmp = strpbrk(symbol, "+-"); |
| 240 | if (tmp) { |
| 241 | ret = kstrtol(tmp, 0, offset); |
| 242 | if (ret) |
| 243 | return ret; |
| 244 | *tmp = '\0'; |
| 245 | } else |
| 246 | *offset = 0; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * traceprobe_parse_event_name() - Parse a string into group and event names |
| 253 | * @pevent: A pointer to the string to be parsed. |
| 254 | * @pgroup: A pointer to the group name. |
| 255 | * @buf: A buffer to store the parsed group name. |
| 256 | * @offset: The offset of the string in the original user command, for logging. |
| 257 | * |
| 258 | * This parses a string with the format `[GROUP/][EVENT]` or `[GROUP.][EVENT]` |
| 259 | * (either GROUP or EVENT or both must be specified). |
| 260 | * Since the parsed group name is stored in @buf, the caller must ensure @buf |
| 261 | * is at least MAX_EVENT_NAME_LEN bytes. |
| 262 | * |
| 263 | * Return: 0 on success, or -EINVAL on failure. |
| 264 | * |
| 265 | * If success, *@pevent is updated to point to the event name part of the |
| 266 | * original string, or NULL if there is no event name. |
| 267 | * Also, *@pgroup is updated to point to the parsed group which is stored |
| 268 | * in @buf, or NULL if there is no group name. |
| 269 | */ |
| 270 | int traceprobe_parse_event_name(const char **pevent, const char **pgroup, |
| 271 | char *buf, int offset) |
| 272 | { |
| 273 | const char *slash, *event = *pevent; |
| 274 | int len; |
| 275 | |
| 276 | slash = strchr(event, '/'); |
| 277 | if (!slash) |
| 278 | slash = strchr(event, '.'); |
| 279 | |
| 280 | if (slash) { |
| 281 | if (slash == event) { |
| 282 | trace_probe_log_err(offset, NO_GROUP_NAME); |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | if (slash - event + 1 > MAX_EVENT_NAME_LEN) { |
| 286 | trace_probe_log_err(offset, GROUP_TOO_LONG); |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | strscpy(buf, event, slash - event + 1); |
| 290 | if (!is_good_system_name(buf)) { |
| 291 | trace_probe_log_err(offset, BAD_GROUP_NAME); |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | *pgroup = buf; |
| 295 | *pevent = slash + 1; |
| 296 | offset += slash - event + 1; |
| 297 | event = *pevent; |
| 298 | } |
| 299 | len = strlen(event); |
| 300 | if (len == 0) { |
| 301 | if (slash) { |
| 302 | *pevent = NULL; |
| 303 | return 0; |
| 304 | } |
| 305 | trace_probe_log_err(offset, NO_EVENT_NAME); |
| 306 | return -EINVAL; |
| 307 | } else if (len >= MAX_EVENT_NAME_LEN) { |
| 308 | trace_probe_log_err(offset, EVENT_TOO_LONG); |
| 309 | return -EINVAL; |
| 310 | } |
| 311 | if (!is_good_name(event)) { |
| 312 | trace_probe_log_err(offset, BAD_EVENT_NAME); |
| 313 | return -EINVAL; |
| 314 | } |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static int parse_trace_event_arg(char *arg, struct fetch_insn *code, |
| 319 | struct traceprobe_parse_context *ctx) |
| 320 | { |
| 321 | struct ftrace_event_field *field; |
| 322 | struct list_head *head; |
| 323 | |
| 324 | head = trace_get_fields(ctx->event); |
| 325 | list_for_each_entry(field, head, link) { |
| 326 | if (!strcmp(arg, field->name)) { |
| 327 | code->op = FETCH_OP_TP_ARG; |
| 328 | code->data = field; |
| 329 | return 0; |
| 330 | } |
| 331 | } |
| 332 | return -ENOENT; |
| 333 | } |
| 334 | |
| 335 | static int parse_trace_event(char *arg, struct fetch_insn *code, |
| 336 | struct traceprobe_parse_context *ctx) |
| 337 | { |
| 338 | int ret; |
| 339 | |
| 340 | if (code->data) |
| 341 | return -EFAULT; |
| 342 | ret = parse_trace_event_arg(arg, code, ctx); |
| 343 | if (!ret) |
| 344 | return 0; |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | /* this_cpu_* parser */ |
| 349 | #define THIS_CPU_PTR_PREFIX "this_cpu_ptr(" |
| 350 | #define THIS_CPU_READ_PREFIX "this_cpu_read(" |
| 351 | #define THIS_CPU_PTR_LEN (sizeof(THIS_CPU_PTR_PREFIX) - 1) |
| 352 | #define THIS_CPU_READ_LEN (sizeof(THIS_CPU_READ_PREFIX) - 1) |
| 353 | |
| 354 | static int |
| 355 | parse_probe_arg(char *arg, const struct fetch_type *type, |
| 356 | struct fetch_insn **pcode, struct fetch_insn *end, |
| 357 | struct traceprobe_parse_context *ctx); |
| 358 | |
| 359 | static int parse_this_cpu(char *arg, struct traceprobe_parse_context *ctx) |
| 360 | { |
| 361 | bool is_read = false; |
| 362 | char *tmp; |
| 363 | |
| 364 | /* |
| 365 | * This is only for kernel probes, excluding eprobe, because per-cpu |
| 366 | * pointer should not be recorded by events. |
| 367 | */ |
| 368 | if (!(ctx->flags & TPARG_FL_KERNEL) || |
| 369 | (ctx->flags & TPARG_FL_TEVENT)) { |
| 370 | trace_probe_log_err(ctx->offset, NOSUP_PERCPU); |
| 371 | return -EINVAL; |
| 372 | } |
| 373 | if (str_has_prefix(arg, THIS_CPU_PTR_PREFIX)) { |
| 374 | arg += THIS_CPU_PTR_LEN; |
| 375 | ctx->offset += THIS_CPU_PTR_LEN; |
| 376 | } else if (str_has_prefix(arg, THIS_CPU_READ_PREFIX)) { |
| 377 | arg += THIS_CPU_READ_LEN; |
| 378 | ctx->offset += THIS_CPU_READ_LEN; |
| 379 | is_read = true; |
| 380 | } else { |
| 381 | trace_probe_log_err(ctx->offset, BAD_FETCH_ARG); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
| 385 | tmp = strrchr(arg, ')'); |
| 386 | if (!tmp) { |
| 387 | trace_probe_log_err(ctx->offset + strlen(arg), |
| 388 | DEREF_OPEN_BRACE); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | *tmp = '\0'; |
| 392 | |
| 393 | ctx->stack[ctx->depth].type = STATE_DEREF; |
| 394 | ctx->stack[ctx->depth].deref.deref = FETCH_OP_CPU_PTR; |
| 395 | ctx->stack[ctx->depth].deref.offset = 0; |
| 396 | ctx->stack[ctx->depth].deref.cur_offs = ctx->offset; |
| 397 | ctx->stack[ctx->depth].deref.inner_arg = arg; |
| 398 | ctx->stack[ctx->depth].deref.is_cpu_read = is_read; |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS |
| 403 | |
| 404 | static u32 btf_type_int(const struct btf_type *t) |
| 405 | { |
| 406 | return *(u32 *)(t + 1); |
| 407 | } |
| 408 | |
| 409 | static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type) |
| 410 | { |
| 411 | const struct btf_type *real_type; |
| 412 | u32 intdata; |
| 413 | |
| 414 | real_type = btf_type_skip_modifiers(btf, type->type, NULL); |
| 415 | if (!real_type) |
| 416 | return false; |
| 417 | |
| 418 | if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT) |
| 419 | return false; |
| 420 | |
| 421 | intdata = btf_type_int(real_type); |
| 422 | return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) |
| 423 | && BTF_INT_BITS(intdata) == 8; |
| 424 | } |
| 425 | |
| 426 | static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type) |
| 427 | { |
| 428 | const struct btf_type *real_type; |
| 429 | const struct btf_array *array; |
| 430 | u32 intdata; |
| 431 | |
| 432 | if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY) |
| 433 | return false; |
| 434 | |
| 435 | array = (const struct btf_array *)(type + 1); |
| 436 | |
| 437 | real_type = btf_type_skip_modifiers(btf, array->type, NULL); |
| 438 | |
| 439 | intdata = btf_type_int(real_type); |
| 440 | return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) |
| 441 | && BTF_INT_BITS(intdata) == 8; |
| 442 | } |
| 443 | |
| 444 | static struct btf *ctx_btf(struct traceprobe_parse_context *ctx) |
| 445 | { |
| 446 | return ctx->struct_btf ? : ctx->btf; |
| 447 | } |
| 448 | |
| 449 | static int check_prepare_btf_string_fetch(char *typename, |
| 450 | struct fetch_insn **pcode, |
| 451 | struct traceprobe_parse_context *ctx) |
| 452 | { |
| 453 | struct btf *btf = ctx_btf(ctx); |
| 454 | |
| 455 | if (!btf || !ctx->last_type) |
| 456 | return 0; |
| 457 | |
| 458 | /* char [] does not need any change. */ |
| 459 | if (btf_type_is_char_array(btf, ctx->last_type)) |
| 460 | return 0; |
| 461 | |
| 462 | /* char * requires dereference the pointer. */ |
| 463 | if (btf_type_is_char_ptr(btf, ctx->last_type)) { |
| 464 | struct fetch_insn *code = *pcode + 1; |
| 465 | |
| 466 | if (code->op == FETCH_OP_END) { |
| 467 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 468 | return -E2BIG; |
| 469 | } |
| 470 | if (typename[0] == 'u') |
| 471 | code->op = FETCH_OP_UDEREF; |
| 472 | else |
| 473 | code->op = FETCH_OP_DEREF; |
| 474 | code->offset = 0; |
| 475 | *pcode = code; |
| 476 | return 0; |
| 477 | } |
| 478 | /* Other types are not available for string */ |
| 479 | trace_probe_log_err(ctx->offset, BAD_TYPE4STR); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | static const char *fetch_type_from_btf_type(struct btf *btf, |
| 484 | const struct btf_type *type, |
| 485 | struct traceprobe_parse_context *ctx) |
| 486 | { |
| 487 | u32 intdata; |
| 488 | |
| 489 | /* TODO: const char * could be converted as a string */ |
| 490 | switch (BTF_INFO_KIND(type->info)) { |
| 491 | case BTF_KIND_ENUM: |
| 492 | /* enum is "int", so convert to "s32" */ |
| 493 | return "s32"; |
| 494 | case BTF_KIND_ENUM64: |
| 495 | return "s64"; |
| 496 | case BTF_KIND_PTR: |
| 497 | /* pointer will be converted to "x??" */ |
| 498 | return IS_ENABLED(CONFIG_64BIT) ? "x64" : "x32"; |
| 499 | case BTF_KIND_INT: |
| 500 | intdata = btf_type_int(type); |
| 501 | if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) { |
| 502 | switch (BTF_INT_BITS(intdata)) { |
| 503 | case 8: |
| 504 | return "s8"; |
| 505 | case 16: |
| 506 | return "s16"; |
| 507 | case 32: |
| 508 | return "s32"; |
| 509 | case 64: |
| 510 | return "s64"; |
| 511 | } |
| 512 | } else { /* unsigned */ |
| 513 | switch (BTF_INT_BITS(intdata)) { |
| 514 | case 8: |
| 515 | return "u8"; |
| 516 | case 16: |
| 517 | return "u16"; |
| 518 | case 32: |
| 519 | return "u32"; |
| 520 | case 64: |
| 521 | return "u64"; |
| 522 | } |
| 523 | /* bitfield, size is encoded in the type */ |
| 524 | ctx->last_bitsize = BTF_INT_BITS(intdata); |
| 525 | ctx->last_bitoffs += BTF_INT_OFFSET(intdata); |
| 526 | return "u64"; |
| 527 | } |
| 528 | } |
| 529 | /* TODO: support other types */ |
| 530 | |
| 531 | return NULL; |
| 532 | } |
| 533 | |
| 534 | static int query_btf_context(struct traceprobe_parse_context *ctx) |
| 535 | { |
| 536 | const struct btf_param *param; |
| 537 | const struct btf_type *type; |
| 538 | struct btf *btf; |
| 539 | s32 nr; |
| 540 | |
| 541 | if (ctx->btf) |
| 542 | return 0; |
| 543 | |
| 544 | if (!ctx->funcname) |
| 545 | return -EINVAL; |
| 546 | |
| 547 | type = btf_find_func_proto(ctx->funcname, &btf); |
| 548 | if (!type) |
| 549 | return -ENOENT; |
| 550 | |
| 551 | ctx->btf = btf; |
| 552 | ctx->proto = type; |
| 553 | |
| 554 | /* ctx->params is optional, since func(void) will not have params. */ |
| 555 | nr = 0; |
| 556 | param = btf_get_func_param(type, &nr); |
| 557 | if (!IS_ERR_OR_NULL(param)) { |
| 558 | /* Hide the first 'data' argument of tracepoint */ |
| 559 | if (ctx->flags & TPARG_FL_TPOINT) { |
| 560 | nr--; |
| 561 | param++; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | if (nr > 0) { |
| 566 | ctx->nr_params = nr; |
| 567 | ctx->params = param; |
| 568 | } else { |
| 569 | ctx->nr_params = 0; |
| 570 | ctx->params = NULL; |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static void clear_struct_btf(struct traceprobe_parse_context *ctx) |
| 577 | { |
| 578 | if (ctx->struct_btf) { |
| 579 | btf_put(ctx->struct_btf); |
| 580 | ctx->struct_btf = NULL; |
| 581 | ctx->last_struct = NULL; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static void clear_btf_context(struct traceprobe_parse_context *ctx) |
| 586 | { |
| 587 | if (ctx->btf) { |
| 588 | btf_put(ctx->btf); |
| 589 | ctx->btf = NULL; |
| 590 | ctx->proto = NULL; |
| 591 | ctx->params = NULL; |
| 592 | ctx->nr_params = 0; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /* Return 1 if the field separator is arrow operator ('->') */ |
| 597 | static int split_next_field(char *varname, char **next_field, |
| 598 | struct traceprobe_parse_context *ctx) |
| 599 | { |
| 600 | char *field; |
| 601 | int ret = 0; |
| 602 | |
| 603 | field = strpbrk(varname, ".-"); |
| 604 | if (field) { |
| 605 | if (field[0] == '-' && field[1] == '>') { |
| 606 | field[0] = '\0'; |
| 607 | field += 2; |
| 608 | ret = 1; |
| 609 | } else if (field[0] == '.') { |
| 610 | field[0] = '\0'; |
| 611 | field += 1; |
| 612 | } else { |
| 613 | trace_probe_log_err(ctx->offset + field - varname, BAD_HYPHEN); |
| 614 | return -EINVAL; |
| 615 | } |
| 616 | *next_field = field; |
| 617 | } |
| 618 | |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | /* Inner loop for solving dot operator ('.'). Return bit-offset of the given field */ |
| 623 | static int get_bitoffset_of_field(char **pfieldname, const struct btf_type **ptype, |
| 624 | struct traceprobe_parse_context *ctx) |
| 625 | { |
| 626 | const struct btf_type *type = *ptype; |
| 627 | const struct btf_member *field; |
| 628 | struct btf *btf = ctx_btf(ctx); |
| 629 | char *fieldname = *pfieldname; |
| 630 | int bitoffs = 0; |
| 631 | u32 anon_offs; |
| 632 | char *next; |
| 633 | int is_ptr; |
| 634 | |
| 635 | do { |
| 636 | next = NULL; |
| 637 | is_ptr = split_next_field(fieldname, &next, ctx); |
| 638 | if (is_ptr < 0) |
| 639 | return is_ptr; |
| 640 | |
| 641 | anon_offs = 0; |
| 642 | field = btf_find_struct_member(btf, type, fieldname, |
| 643 | &anon_offs); |
| 644 | if (IS_ERR(field)) { |
| 645 | trace_probe_log_err(ctx->offset, BAD_BTF_TID); |
| 646 | return PTR_ERR(field); |
| 647 | } |
| 648 | if (!field) { |
| 649 | trace_probe_log_err(ctx->offset, NO_BTF_FIELD); |
| 650 | return -ENOENT; |
| 651 | } |
| 652 | /* Add anonymous structure/union offset */ |
| 653 | bitoffs += anon_offs; |
| 654 | |
| 655 | /* Accumulate the bit-offsets of the dot-connected fields */ |
| 656 | if (btf_type_kflag(type)) { |
| 657 | bitoffs += BTF_MEMBER_BIT_OFFSET(field->offset); |
| 658 | ctx->last_bitsize = BTF_MEMBER_BITFIELD_SIZE(field->offset); |
| 659 | } else { |
| 660 | bitoffs += field->offset; |
| 661 | ctx->last_bitsize = 0; |
| 662 | } |
| 663 | |
| 664 | type = btf_type_skip_modifiers(btf, field->type, NULL); |
| 665 | if (!type) { |
| 666 | trace_probe_log_err(ctx->offset, BAD_BTF_TID); |
| 667 | return -EINVAL; |
| 668 | } |
| 669 | |
| 670 | if (next) |
| 671 | ctx->offset += next - fieldname; |
| 672 | fieldname = next; |
| 673 | } while (!is_ptr && fieldname); |
| 674 | |
| 675 | *pfieldname = fieldname; |
| 676 | *ptype = type; |
| 677 | |
| 678 | return bitoffs; |
| 679 | } |
| 680 | /* |
| 681 | * Parse the field of data structure. The @type must be a pointer type |
| 682 | * pointing the target data structure type. |
| 683 | */ |
| 684 | static int parse_btf_field(char *fieldname, const struct btf_type *type, |
| 685 | struct fetch_insn **pcode, struct fetch_insn *end, |
| 686 | struct traceprobe_parse_context *ctx) |
| 687 | { |
| 688 | struct fetch_insn *code = *pcode; |
| 689 | struct btf *btf = ctx_btf(ctx); |
| 690 | bool is_first_field = true; |
| 691 | int bitoffs; |
| 692 | |
| 693 | do { |
| 694 | /* For the first field of typecast, @type will be the target structure type. */ |
| 695 | if (!(is_first_field && ctx->struct_btf)) { |
| 696 | /* Outer loop for solving arrow operator ('->') */ |
| 697 | if (BTF_INFO_KIND(type->info) != BTF_KIND_PTR) { |
| 698 | trace_probe_log_err(ctx->offset, NO_PTR_STRCT); |
| 699 | return -EINVAL; |
| 700 | } |
| 701 | |
| 702 | /* Convert a struct pointer type to a struct type */ |
| 703 | type = btf_type_skip_modifiers(btf, type->type, NULL); |
| 704 | if (!type) { |
| 705 | trace_probe_log_err(ctx->offset, BAD_BTF_TID); |
| 706 | return -EINVAL; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | bitoffs = get_bitoffset_of_field(&fieldname, &type, ctx); |
| 711 | if (bitoffs < 0) |
| 712 | return bitoffs; |
| 713 | if (++code == end) { |
| 714 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | code->op = FETCH_OP_DEREF; /* TODO: user deref support */ |
| 718 | code->offset = bitoffs / 8; |
| 719 | if (is_first_field && ctx->struct_btf) { |
| 720 | /* The first field can be typecasted with field option. */ |
| 721 | code->offset -= ctx->prefix_byteoffs; |
| 722 | } |
| 723 | *pcode = code; |
| 724 | |
| 725 | ctx->last_bitoffs = bitoffs % 8; |
| 726 | ctx->last_type = type; |
| 727 | is_first_field = false; |
| 728 | } while (fieldname); |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static int __store_entry_arg(struct trace_probe *tp, int argnum); |
| 734 | |
| 735 | static int parse_btf_arg(char *varname, |
| 736 | struct fetch_insn **pcode, struct fetch_insn *end, |
| 737 | struct traceprobe_parse_context *ctx) |
| 738 | { |
| 739 | struct fetch_insn *code = *pcode; |
| 740 | const struct btf_param *params; |
| 741 | const struct btf_type *type; |
| 742 | char *field = NULL; |
| 743 | int i, is_ptr, ret; |
| 744 | u32 tid; |
| 745 | |
| 746 | /* Note: field is not separated at this point, so check prefix. */ |
| 747 | if (!str_has_prefix(varname, "$current") && |
| 748 | !ctx->funcname && !(ctx->flags & TPARG_FL_TEVENT)) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | is_ptr = split_next_field(varname, &field, ctx); |
| 752 | if (is_ptr < 0) |
| 753 | return is_ptr; |
| 754 | if (!is_ptr && field) { |
| 755 | /* dot-connected field on an argument is not supported. */ |
| 756 | trace_probe_log_err(ctx->offset + field - varname, |
| 757 | NOSUP_DAT_ARG); |
| 758 | return -EOPNOTSUPP; |
| 759 | } |
| 760 | |
| 761 | if (!strcmp(varname, "$current")) { |
| 762 | code->op = FETCH_OP_CURRENT; |
| 763 | /* If no typecast is specified for $current, use task_struct by default */ |
| 764 | ret = bpf_find_btf_id("task_struct", BTF_KIND_STRUCT, &ctx->struct_btf); |
| 765 | if (ret < 0) { |
| 766 | trace_probe_log_err(ctx->offset, NO_BTF_ENTRY); |
| 767 | return -ENOENT; |
| 768 | } |
| 769 | tid = (u32)ret; |
| 770 | type = ctx->last_struct = |
| 771 | btf_type_skip_modifiers(ctx->struct_btf, tid, NULL); |
| 772 | goto found_type; |
| 773 | } |
| 774 | |
| 775 | if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) { |
| 776 | code->op = FETCH_OP_RETVAL; |
| 777 | /* Check whether the function return type is not void, even with typecast. */ |
| 778 | if (query_btf_context(ctx) == 0) { |
| 779 | if (ctx->proto->type == 0) { |
| 780 | trace_probe_log_err(ctx->offset, NO_RETVAL); |
| 781 | return -ENOENT; |
| 782 | } |
| 783 | tid = ctx->proto->type; |
| 784 | goto found; |
| 785 | } |
| 786 | if (field) { |
| 787 | trace_probe_log_err(ctx->offset + field - varname, |
| 788 | NO_BTF_ENTRY); |
| 789 | return -ENOENT; |
| 790 | } |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | if (!ctx->btf) { |
| 795 | ret = query_btf_context(ctx); |
| 796 | if (ret < 0 || ctx->nr_params == 0) { |
| 797 | trace_probe_log_err(ctx->offset, NO_BTF_ENTRY); |
| 798 | return -ENOENT; |
| 799 | } |
| 800 | } |
| 801 | params = ctx->params; |
| 802 | |
| 803 | for (i = 0; i < ctx->nr_params; i++) { |
| 804 | const char *name = btf_name_by_offset(ctx->btf, params[i].name_off); |
| 805 | |
| 806 | if (name && !strcmp(name, varname)) { |
| 807 | if (tparg_is_function_entry(ctx->flags)) { |
| 808 | code->op = FETCH_OP_ARG; |
| 809 | if (ctx->flags & TPARG_FL_TPOINT) |
| 810 | code->param = i + 1; |
| 811 | else |
| 812 | code->param = i; |
| 813 | } else if (tparg_is_function_return(ctx->flags)) { |
| 814 | code->op = FETCH_OP_EDATA; |
| 815 | ret = __store_entry_arg(ctx->tp, i); |
| 816 | if (ret < 0) { |
| 817 | /* internal error */ |
| 818 | return ret; |
| 819 | } |
| 820 | code->offset = ret; |
| 821 | } |
| 822 | tid = params[i].type; |
| 823 | goto found; |
| 824 | } |
| 825 | } |
| 826 | trace_probe_log_err(ctx->offset, NO_BTFARG); |
| 827 | return -ENOENT; |
| 828 | |
| 829 | found: |
| 830 | type = btf_type_skip_modifiers(ctx->btf, tid, NULL); |
| 831 | found_type: |
| 832 | if (!type) { |
| 833 | trace_probe_log_err(ctx->offset, BAD_BTF_TID); |
| 834 | return -EINVAL; |
| 835 | } |
| 836 | /* Initialize the last type information */ |
| 837 | ctx->last_type = type; |
| 838 | ctx->last_bitoffs = 0; |
| 839 | ctx->last_bitsize = 0; |
| 840 | if (field) { |
| 841 | ctx->offset += field - varname; |
| 842 | return parse_btf_field(field, type, pcode, end, ctx); |
| 843 | } |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | static const struct fetch_type *find_fetch_type_from_btf_type( |
| 848 | struct traceprobe_parse_context *ctx) |
| 849 | { |
| 850 | struct btf *btf = ctx_btf(ctx); |
| 851 | const char *typestr = NULL; |
| 852 | |
| 853 | if (btf && ctx->last_type) |
| 854 | typestr = fetch_type_from_btf_type(btf, ctx->last_type, ctx); |
| 855 | |
| 856 | return find_fetch_type(typestr, ctx->flags); |
| 857 | } |
| 858 | |
| 859 | static int parse_btf_bitfield(struct fetch_insn **pcode, |
| 860 | struct traceprobe_parse_context *ctx) |
| 861 | { |
| 862 | struct fetch_insn *code = *pcode; |
| 863 | |
| 864 | if ((ctx->last_bitsize % 8 == 0) && ctx->last_bitoffs == 0) |
| 865 | return 0; |
| 866 | |
| 867 | code++; |
| 868 | if (code->op != FETCH_OP_NOP) { |
| 869 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | *pcode = code; |
| 873 | |
| 874 | code->op = FETCH_OP_MOD_BF; |
| 875 | code->lshift = 64 - (ctx->last_bitsize + ctx->last_bitoffs); |
| 876 | code->rshift = 64 - ctx->last_bitsize; |
| 877 | code->basesize = 64 / 8; |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static int query_btf_struct(const char *sname, struct traceprobe_parse_context *ctx) |
| 882 | { |
| 883 | struct btf *btf = NULL; |
| 884 | int id; |
| 885 | |
| 886 | /* A struct_btf should only be used by a single argument */ |
| 887 | if (WARN_ON_ONCE(ctx->struct_btf)) { |
| 888 | btf_put(ctx->struct_btf); |
| 889 | ctx->struct_btf = NULL; |
| 890 | } |
| 891 | |
| 892 | id = bpf_find_btf_id(sname, BTF_KIND_STRUCT, &btf); |
| 893 | if (id < 0) |
| 894 | return id; |
| 895 | ctx->struct_btf = btf; |
| 896 | ctx->last_struct = btf_type_by_id(ctx->struct_btf, id); |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | static int parse_btf_casttype(char *casttype, struct traceprobe_parse_context *ctx) |
| 901 | { |
| 902 | char *field; |
| 903 | int ret; |
| 904 | |
| 905 | /* Field option - evaluated later. */ |
| 906 | field = strchr(casttype, ','); |
| 907 | if (field) |
| 908 | *field++ = '\0'; |
| 909 | |
| 910 | ret = query_btf_struct(casttype, ctx); |
| 911 | if (ret < 0) { |
| 912 | trace_probe_log_err(ctx->offset, NO_PTR_STRCT); |
| 913 | return -EINVAL; |
| 914 | } |
| 915 | |
| 916 | if (field) { |
| 917 | struct btf_type *type = (struct btf_type *)ctx->last_struct; |
| 918 | |
| 919 | ctx->offset += field - casttype; |
| 920 | ret = get_bitoffset_of_field(&field, &ctx->last_struct, ctx); |
| 921 | if (ret < 0) |
| 922 | return ret; |
| 923 | if (ret % 8) { |
| 924 | trace_probe_log_err(ctx->offset, TYPECAST_NOT_ALIGNED); |
| 925 | return -EINVAL; |
| 926 | } |
| 927 | if (field != NULL) { |
| 928 | /* this means @field skips an arrow operator ("->"). */ |
| 929 | trace_probe_log_err(ctx->offset - 2, TYPECAST_BAD_ARROW); |
| 930 | return -EINVAL; |
| 931 | } |
| 932 | ctx->prefix_byteoffs = ret / 8; |
| 933 | /* Restore the original struct type (overwritten by get_bitoffset_of_field) */ |
| 934 | ctx->last_struct = type; |
| 935 | } |
| 936 | |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | /* Find the matching closing parenthesis for a given opening parenthesis. */ |
| 941 | static char *find_matched_close_paren(char *s) |
| 942 | { |
| 943 | char *p = s; |
| 944 | int count = 0; |
| 945 | |
| 946 | while (*p) { |
| 947 | if (*p == '(') |
| 948 | count++; |
| 949 | else if (*p == ')') { |
| 950 | if (--count == 0) |
| 951 | return p; |
| 952 | } |
| 953 | p++; |
| 954 | } |
| 955 | return NULL; |
| 956 | } |
| 957 | |
| 958 | static int handle_typecast(char *arg, struct traceprobe_parse_context *ctx) |
| 959 | { |
| 960 | int orig_offset = ctx->offset; |
| 961 | char *close; |
| 962 | char *tmp; |
| 963 | char *fieldname; |
| 964 | |
| 965 | if (!(tparg_is_event_probe(ctx->flags) || |
| 966 | tparg_is_function_entry(ctx->flags) || |
| 967 | tparg_is_function_return(ctx->flags))) { |
| 968 | trace_probe_log_err(ctx->offset, NOSUP_BTFARG); |
| 969 | return -EOPNOTSUPP; |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * Always consider the token after typecast as a nested call |
| 974 | * For example: (STRUCT)VAR->FIELD and (STRUCT)(VAR)->FIELD are same. |
| 975 | * VAR is solved in the nested call. |
| 976 | */ |
| 977 | tmp = strchr(arg, ')'); |
| 978 | if (!tmp) { |
| 979 | trace_probe_log_err(ctx->offset + strlen(arg), |
| 980 | DEREF_OPEN_BRACE); |
| 981 | return -EINVAL; |
| 982 | } |
| 983 | *tmp++ = '\0'; |
| 984 | |
| 985 | ctx->offset += tmp - arg; |
| 986 | if (*tmp == '(') { |
| 987 | close = find_matched_close_paren(tmp); |
| 988 | |
| 989 | if (!close) { |
| 990 | trace_probe_log_err(ctx->offset, DEREF_OPEN_BRACE); |
| 991 | return -EINVAL; |
| 992 | } |
| 993 | /* We expect a field access for typecast */ |
| 994 | if (close[1] != '-' || close[2] != '>') { |
| 995 | trace_probe_log_err(ctx->offset + close - tmp + 1, |
| 996 | TYPECAST_REQ_FIELD); |
| 997 | return -EINVAL; |
| 998 | } |
| 999 | /* Skip '(' */ |
| 1000 | ctx->offset += 1; |
| 1001 | tmp++; |
| 1002 | } else if (*tmp == '+' || *tmp == '-' || |
| 1003 | str_has_prefix(tmp, THIS_CPU_PTR_PREFIX) || |
| 1004 | str_has_prefix(tmp, THIS_CPU_READ_PREFIX)) { |
| 1005 | /* Dereference can have another field access inside it. */ |
| 1006 | char *open = strchr(tmp + 1, '('); |
| 1007 | |
| 1008 | if (!open) { |
| 1009 | trace_probe_log_err(ctx->offset, |
| 1010 | DEREF_NEED_BRACE); |
| 1011 | return -EINVAL; |
| 1012 | } |
| 1013 | close = find_matched_close_paren(open); |
| 1014 | if (!close) { |
| 1015 | trace_probe_log_err(ctx->offset + strlen(tmp), |
| 1016 | DEREF_OPEN_BRACE); |
| 1017 | return -EINVAL; |
| 1018 | } |
| 1019 | close++; |
| 1020 | /* We expect a field access for typecast */ |
| 1021 | if (close[0] != '-' || close[1] != '>') { |
| 1022 | trace_probe_log_err(ctx->offset + close - tmp, |
| 1023 | TYPECAST_REQ_FIELD); |
| 1024 | return -EINVAL; |
| 1025 | } |
| 1026 | } else { |
| 1027 | if (tmp[0] == '@') { |
| 1028 | /* @sym+offset is not allowed without parenthesized */ |
| 1029 | close = strpbrk(tmp, "+-"); |
| 1030 | if (close && isdigit(close[1])) { |
| 1031 | trace_probe_log_err(ctx->offset, |
| 1032 | TYPECAST_SYM_OFFSET); |
| 1033 | return -EINVAL; |
| 1034 | } |
| 1035 | } |
| 1036 | /* Inner variable name */ |
| 1037 | close = strchr(tmp, '-'); |
| 1038 | if (!close || close[1] != '>') { |
| 1039 | trace_probe_log_err(ctx->offset + strlen(tmp), |
| 1040 | TYPECAST_REQ_FIELD); |
| 1041 | return -EINVAL; |
| 1042 | } |
| 1043 | } |
| 1044 | *close = '\0'; |
| 1045 | |
| 1046 | /* Let fieldname point the field name. */ |
| 1047 | if (close[1] == '-') |
| 1048 | fieldname = close + 3; /* Skip "->" after closing parenthesis */ |
| 1049 | else |
| 1050 | fieldname = close + 2; /* Skip ">" after inner variable name */ |
| 1051 | |
| 1052 | ctx->stack[ctx->depth].type = STATE_TYPECAST; |
| 1053 | ctx->stack[ctx->depth].typecast.casttype = arg + 1; |
| 1054 | ctx->stack[ctx->depth].typecast.fieldname = fieldname; |
| 1055 | ctx->stack[ctx->depth].typecast.orig_offset = orig_offset; |
| 1056 | ctx->stack[ctx->depth].typecast.field_offset_diff = fieldname - arg; |
| 1057 | ctx->stack[ctx->depth].typecast.inner_arg = tmp; |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | #else /* !CONFIG_PROBE_EVENTS_BTF_ARGS */ |
| 1062 | |
| 1063 | static void clear_struct_btf(struct traceprobe_parse_context *ctx) |
| 1064 | { |
| 1065 | ctx->struct_btf = NULL; |
| 1066 | } |
| 1067 | |
| 1068 | static void clear_btf_context(struct traceprobe_parse_context *ctx) |
| 1069 | { |
| 1070 | ctx->btf = NULL; |
| 1071 | } |
| 1072 | |
| 1073 | static int query_btf_context(struct traceprobe_parse_context *ctx) |
| 1074 | { |
| 1075 | return -EOPNOTSUPP; |
| 1076 | } |
| 1077 | |
| 1078 | static int parse_btf_arg(char *varname, |
| 1079 | struct fetch_insn **pcode, struct fetch_insn *end, |
| 1080 | struct traceprobe_parse_context *ctx) |
| 1081 | { |
| 1082 | trace_probe_log_err(ctx->offset, NOSUP_BTFARG); |
| 1083 | return -EOPNOTSUPP; |
| 1084 | } |
| 1085 | |
| 1086 | static int parse_btf_bitfield(struct fetch_insn **pcode, |
| 1087 | struct traceprobe_parse_context *ctx) |
| 1088 | { |
| 1089 | trace_probe_log_err(ctx->offset, NOSUP_BTFARG); |
| 1090 | return -EOPNOTSUPP; |
| 1091 | } |
| 1092 | |
| 1093 | #define find_fetch_type_from_btf_type(ctx) \ |
| 1094 | find_fetch_type(NULL, ctx->flags) |
| 1095 | |
| 1096 | static int check_prepare_btf_string_fetch(char *typename, |
| 1097 | struct fetch_insn **pcode, |
| 1098 | struct traceprobe_parse_context *ctx) |
| 1099 | { |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | static int parse_btf_casttype(char *casttype, |
| 1104 | struct traceprobe_parse_context *ctx) |
| 1105 | { |
| 1106 | return -EOPNOTSUPP; |
| 1107 | } |
| 1108 | |
| 1109 | static int parse_btf_field(char *fieldname, const struct btf_type *type, |
| 1110 | struct fetch_insn **pcode, struct fetch_insn *end, |
| 1111 | struct traceprobe_parse_context *ctx) |
| 1112 | { |
| 1113 | return -EOPNOTSUPP; |
| 1114 | } |
| 1115 | |
| 1116 | static int handle_typecast(char *arg, struct traceprobe_parse_context *ctx) |
| 1117 | { |
| 1118 | trace_probe_log_err(ctx->offset, NOSUP_BTFARG); |
| 1119 | return -EOPNOTSUPP; |
| 1120 | } |
| 1121 | |
| 1122 | #endif /* CONFIG_PROBE_EVENTS_BTF_ARGS */ |
| 1123 | |
| 1124 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
| 1125 | |
| 1126 | static void store_entry_arg_at(struct fetch_insn *code, int argnum, int offset) |
| 1127 | { |
| 1128 | code[0].op = FETCH_OP_ARG; |
| 1129 | code[0].param = argnum; |
| 1130 | code[1].op = FETCH_OP_ST_EDATA; |
| 1131 | code[1].offset = offset; |
| 1132 | } |
| 1133 | |
| 1134 | static int get_entry_arg_max_offset(struct probe_entry_arg *earg) |
| 1135 | { |
| 1136 | int i, max_offset = 0; |
| 1137 | |
| 1138 | /* |
| 1139 | * earg->code[] array has an operation sequence which is run in |
| 1140 | * the entry handler. |
| 1141 | * The sequence stopped by FETCH_OP_END and each data stored in |
| 1142 | * the entry data buffer by FETCH_OP_ST_EDATA. The FETCH_OP_ST_EDATA |
| 1143 | * stores the data at the data buffer + its offset, and all data are |
| 1144 | * "unsigned long" size. The offset must be increased when a data is |
| 1145 | * stored. Thus we need to find the last FETCH_OP_ST_EDATA in the |
| 1146 | * code array. |
| 1147 | */ |
| 1148 | for (i = 0; i < earg->size - 1 && earg->code[i].op != FETCH_OP_END; i++) { |
| 1149 | if (earg->code[i].op == FETCH_OP_ST_EDATA) |
| 1150 | if (earg->code[i].offset > max_offset) |
| 1151 | max_offset = earg->code[i].offset; |
| 1152 | } |
| 1153 | return max_offset; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Add the entry code to store the 'argnum'th parameter and return the offset |
| 1158 | * in the entry data buffer where the data will be stored. |
| 1159 | */ |
| 1160 | static int __store_entry_arg(struct trace_probe *tp, int argnum) |
| 1161 | { |
| 1162 | struct probe_entry_arg *earg = tp->entry_arg; |
| 1163 | int i, offset, last_offset = 0; |
| 1164 | |
| 1165 | if (!earg) { |
| 1166 | earg = kzalloc_flex(*earg, code, 2 * tp->nr_args + 1); |
| 1167 | if (!earg) |
| 1168 | return -ENOMEM; |
| 1169 | earg->size = 2 * tp->nr_args + 1; |
| 1170 | /* Fill the code buffer with 'end' to simplify it */ |
| 1171 | for (i = 0; i < earg->size; i++) |
| 1172 | earg->code[i].op = FETCH_OP_END; |
| 1173 | tp->entry_arg = earg; |
| 1174 | store_entry_arg_at(earg->code, argnum, 0); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | /* |
| 1179 | * NOTE: if anyone change the following rule, please rewrite this. |
| 1180 | * The entry code array is filled with the pair of |
| 1181 | * |
| 1182 | * [FETCH_OP_ARG(argnum)] |
| 1183 | * [FETCH_OP_ST_EDATA(offset of entry data buffer)] |
| 1184 | * |
| 1185 | * and the rest of entries are filled with [FETCH_OP_END]. |
| 1186 | * The offset should be incremented, thus the last pair should |
| 1187 | * have the largest offset. |
| 1188 | */ |
| 1189 | |
| 1190 | /* Search the offset for the sprcified argnum. */ |
| 1191 | for (i = 0; i < earg->size - 1 && earg->code[i].op != FETCH_OP_END; i += 2) { |
| 1192 | if (WARN_ON_ONCE(earg->code[i].op != FETCH_OP_ARG)) |
| 1193 | return -EINVAL; |
| 1194 | |
| 1195 | if (earg->code[i].param != argnum) |
| 1196 | continue; |
| 1197 | |
| 1198 | if (WARN_ON_ONCE(earg->code[i + 1].op != FETCH_OP_ST_EDATA)) |
| 1199 | return -EINVAL; |
| 1200 | |
| 1201 | return earg->code[i + 1].offset; |
| 1202 | } |
| 1203 | /* Not found, append new entry if possible. */ |
| 1204 | if (i >= earg->size - 1) |
| 1205 | return -ENOSPC; |
| 1206 | |
| 1207 | /* The last entry must have the largest offset. */ |
| 1208 | if (i != 0) { |
| 1209 | if (WARN_ON_ONCE(earg->code[i - 1].op != FETCH_OP_ST_EDATA)) |
| 1210 | return -EINVAL; |
| 1211 | last_offset = earg->code[i - 1].offset; |
| 1212 | } |
| 1213 | |
| 1214 | offset = last_offset + sizeof(unsigned long); |
| 1215 | store_entry_arg_at(&earg->code[i], argnum, offset); |
| 1216 | return offset; |
| 1217 | } |
| 1218 | |
| 1219 | int traceprobe_get_entry_data_size(struct trace_probe *tp) |
| 1220 | { |
| 1221 | struct probe_entry_arg *earg = tp->entry_arg; |
| 1222 | |
| 1223 | if (!earg) |
| 1224 | return 0; |
| 1225 | |
| 1226 | return get_entry_arg_max_offset(earg) + sizeof(unsigned long); |
| 1227 | } |
| 1228 | |
| 1229 | void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs) |
| 1230 | { |
| 1231 | struct probe_entry_arg *earg = tp->entry_arg; |
| 1232 | unsigned long val = 0; |
| 1233 | int i; |
| 1234 | |
| 1235 | if (!earg) |
| 1236 | return; |
| 1237 | |
| 1238 | for (i = 0; i < earg->size; i++) { |
| 1239 | struct fetch_insn *code = &earg->code[i]; |
| 1240 | |
| 1241 | switch (code->op) { |
| 1242 | case FETCH_OP_ARG: |
| 1243 | val = regs_get_kernel_argument(regs, code->param); |
| 1244 | break; |
| 1245 | case FETCH_OP_ST_EDATA: |
| 1246 | *(unsigned long *)((u8 *)edata + code->offset) = val; |
| 1247 | break; |
| 1248 | case FETCH_OP_END: |
| 1249 | goto end; |
| 1250 | default: |
| 1251 | break; |
| 1252 | } |
| 1253 | } |
| 1254 | end: |
| 1255 | return; |
| 1256 | } |
| 1257 | NOKPROBE_SYMBOL(store_trace_entry_data) |
| 1258 | #endif |
| 1259 | |
| 1260 | #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) |
| 1261 | |
| 1262 | static int parse_probe_var_retval(char *orig_arg, |
| 1263 | struct fetch_insn **pcode, |
| 1264 | struct fetch_insn *end, |
| 1265 | struct traceprobe_parse_context *ctx) |
| 1266 | { |
| 1267 | struct fetch_insn *code = *pcode; |
| 1268 | |
| 1269 | if (!(ctx->flags & TPARG_FL_RETURN)) { |
| 1270 | trace_probe_log_err(ctx->offset, RETVAL_ON_PROBE); |
| 1271 | return -EINVAL; |
| 1272 | } |
| 1273 | if (!(ctx->flags & TPARG_FL_KERNEL) || |
| 1274 | !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) { |
| 1275 | code->op = FETCH_OP_RETVAL; |
| 1276 | return 0; |
| 1277 | } |
| 1278 | return parse_btf_arg(orig_arg, pcode, end, ctx); |
| 1279 | } |
| 1280 | |
| 1281 | static int parse_probe_var_stack(char *arg, int len, struct fetch_insn *code, |
| 1282 | struct traceprobe_parse_context *ctx) |
| 1283 | { |
| 1284 | unsigned long param; |
| 1285 | int ret; |
| 1286 | |
| 1287 | if (arg[len] == '\0') { |
| 1288 | code->op = FETCH_OP_STACKP; |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | if (isdigit(arg[len])) { |
| 1293 | ret = kstrtoul(arg + len, 10, ¶m); |
| 1294 | if (ret) { |
| 1295 | trace_probe_log_err(ctx->offset, BAD_VAR); |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | if ((ctx->flags & TPARG_FL_KERNEL) && |
| 1300 | param > PARAM_MAX_STACK) { |
| 1301 | trace_probe_log_err(ctx->offset, BAD_STACK_NUM); |
| 1302 | return -EINVAL; |
| 1303 | } |
| 1304 | code->op = FETCH_OP_STACK; |
| 1305 | code->param = (unsigned int)param; |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | trace_probe_log_err(ctx->offset, BAD_VAR); |
| 1310 | return -EINVAL; |
| 1311 | } |
| 1312 | |
| 1313 | static int parse_probe_var_current(char *orig_arg, char *arg, |
| 1314 | struct fetch_insn **pcode, |
| 1315 | struct fetch_insn *end, |
| 1316 | struct traceprobe_parse_context *ctx) |
| 1317 | { |
| 1318 | struct fetch_insn *code = *pcode; |
| 1319 | |
| 1320 | /* $current is only supported by kernel probe. */ |
| 1321 | if (!(ctx->flags & TPARG_FL_KERNEL)) { |
| 1322 | trace_probe_log_err(ctx->offset, BAD_VAR); |
| 1323 | return -EINVAL; |
| 1324 | } |
| 1325 | arg += strlen("current"); |
| 1326 | if (*arg == '-' && IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) |
| 1327 | return parse_btf_arg(orig_arg, pcode, end, ctx); |
| 1328 | |
| 1329 | if (*arg != '\0') { |
| 1330 | trace_probe_log_err(ctx->offset, BAD_VAR); |
| 1331 | return -EINVAL; |
| 1332 | } |
| 1333 | |
| 1334 | code->op = FETCH_OP_CURRENT; |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API |
| 1339 | static int parse_probe_var_arg(char *arg, int len, struct fetch_insn *code, |
| 1340 | struct traceprobe_parse_context *ctx) |
| 1341 | { |
| 1342 | unsigned long param; |
| 1343 | int ret; |
| 1344 | |
| 1345 | ret = kstrtoul(arg + len, 10, ¶m); |
| 1346 | if (ret) { |
| 1347 | trace_probe_log_err(ctx->offset, BAD_VAR); |
| 1348 | return ret; |
| 1349 | } |
| 1350 | |
| 1351 | if (!param || param > PARAM_MAX_STACK) { |
| 1352 | trace_probe_log_err(ctx->offset, BAD_ARG_NUM); |
| 1353 | return -EINVAL; |
| 1354 | } |
| 1355 | param--; /* argN starts from 1, but internal arg[N] starts from 0 */ |
| 1356 | |
| 1357 | if (tparg_is_function_entry(ctx->flags)) { |
| 1358 | code->op = FETCH_OP_ARG; |
| 1359 | code->param = (unsigned int)param; |
| 1360 | /* |
| 1361 | * The tracepoint probe will probe a stub function, and the |
| 1362 | * first parameter of the stub is a dummy and should be ignored. |
| 1363 | */ |
| 1364 | if (ctx->flags & TPARG_FL_TPOINT) |
| 1365 | code->param++; |
| 1366 | } else if (tparg_is_function_return(ctx->flags)) { |
| 1367 | /* function entry argument access from return probe */ |
| 1368 | ret = __store_entry_arg(ctx->tp, param); |
| 1369 | if (ret < 0) /* This error should be an internal error */ |
| 1370 | return ret; |
| 1371 | |
| 1372 | code->op = FETCH_OP_EDATA; |
| 1373 | code->offset = ret; |
| 1374 | } else { |
| 1375 | trace_probe_log_err(ctx->offset, NOFENTRY_ARGS); |
| 1376 | return -EINVAL; |
| 1377 | } |
| 1378 | return 0; |
| 1379 | } |
| 1380 | #else |
| 1381 | static int parse_probe_var_arg(char *arg, int len, struct fetch_insn *code, |
| 1382 | struct traceprobe_parse_context *ctx) |
| 1383 | { |
| 1384 | trace_probe_log_err(ctx->offset, BAD_VAR); |
| 1385 | return -EINVAL; |
| 1386 | } |
| 1387 | #endif |
| 1388 | |
| 1389 | /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */ |
| 1390 | static int parse_probe_vars(char *orig_arg, const struct fetch_type *t, |
| 1391 | struct fetch_insn **pcode, |
| 1392 | struct fetch_insn *end, |
| 1393 | struct traceprobe_parse_context *ctx) |
| 1394 | { |
| 1395 | struct fetch_insn *code = *pcode; |
| 1396 | char *arg = orig_arg + 1; |
| 1397 | int len, ret; |
| 1398 | |
| 1399 | if (ctx->flags & TPARG_FL_TEVENT) { |
| 1400 | ret = parse_trace_event(arg, code, ctx); |
| 1401 | if (!ret) |
| 1402 | return 0; |
| 1403 | } |
| 1404 | |
| 1405 | if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) { |
| 1406 | code->op = FETCH_OP_COMM; |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | /* eprobe only support event fields or '$comm'. */ |
| 1411 | if (ctx->flags & TPARG_FL_TEVENT) |
| 1412 | goto inval; |
| 1413 | |
| 1414 | if (str_has_prefix(arg, "retval")) |
| 1415 | return parse_probe_var_retval(orig_arg, pcode, end, ctx); |
| 1416 | |
| 1417 | len = str_has_prefix(arg, "stack"); |
| 1418 | if (len) |
| 1419 | return parse_probe_var_stack(arg, len, code, ctx); |
| 1420 | |
| 1421 | /* $current returns the address of the current task_struct. */ |
| 1422 | if (str_has_prefix(arg, "current")) |
| 1423 | return parse_probe_var_current(orig_arg, arg, pcode, end, ctx); |
| 1424 | |
| 1425 | len = str_has_prefix(arg, "arg"); |
| 1426 | if (len) |
| 1427 | return parse_probe_var_arg(arg, len, code, ctx); |
| 1428 | |
| 1429 | inval: |
| 1430 | trace_probe_log_err(ctx->offset, BAD_VAR); |
| 1431 | return -EINVAL; |
| 1432 | } |
| 1433 | |
| 1434 | static int str_to_immediate(char *str, unsigned long *imm) |
| 1435 | { |
| 1436 | if (isdigit(str[0])) |
| 1437 | return kstrtoul(str, 0, imm); |
| 1438 | else if (str[0] == '-') |
| 1439 | return kstrtol(str, 0, (long *)imm); |
| 1440 | else if (str[0] == '+') |
| 1441 | return kstrtol(str + 1, 0, (long *)imm); |
| 1442 | return -EINVAL; |
| 1443 | } |
| 1444 | |
| 1445 | static int __parse_imm_string(char *str, char **pbuf, int offs) |
| 1446 | { |
| 1447 | size_t len = strlen(str); |
| 1448 | |
| 1449 | if (!len || str[len - 1] != '"') { |
| 1450 | trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE); |
| 1451 | return -EINVAL; |
| 1452 | } |
| 1453 | *pbuf = kstrndup(str, len - 1, GFP_KERNEL); |
| 1454 | if (!*pbuf) |
| 1455 | return -ENOMEM; |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
| 1459 | static int parse_probe_arg_register(char *arg, struct fetch_insn *code, |
| 1460 | struct traceprobe_parse_context *ctx) |
| 1461 | { |
| 1462 | int ret; |
| 1463 | |
| 1464 | if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) { |
| 1465 | /* eprobe and fprobe do not handle registers */ |
| 1466 | trace_probe_log_err(ctx->offset, BAD_VAR); |
| 1467 | return -EINVAL; |
| 1468 | } |
| 1469 | ret = regs_query_register_offset(arg + 1); |
| 1470 | if (ret >= 0) { |
| 1471 | code->op = FETCH_OP_REG; |
| 1472 | code->param = (unsigned int)ret; |
| 1473 | return 0; |
| 1474 | } |
| 1475 | trace_probe_log_err(ctx->offset, BAD_REG_NAME); |
| 1476 | return -EINVAL; |
| 1477 | } |
| 1478 | |
| 1479 | static int parse_probe_arg_mem_symbol(char *arg, struct fetch_insn **pcode, |
| 1480 | struct fetch_insn *end, |
| 1481 | struct traceprobe_parse_context *ctx) |
| 1482 | { |
| 1483 | struct fetch_insn *code = *pcode; |
| 1484 | unsigned long param; |
| 1485 | long offset = 0; |
| 1486 | int ret; |
| 1487 | |
| 1488 | if (isdigit(arg[1])) { |
| 1489 | ret = kstrtoul(arg + 1, 0, ¶m); |
| 1490 | if (ret) { |
| 1491 | trace_probe_log_err(ctx->offset, BAD_MEM_ADDR); |
| 1492 | return ret; |
| 1493 | } |
| 1494 | /* load address */ |
| 1495 | code->op = FETCH_OP_IMM; |
| 1496 | code->immediate = param; |
| 1497 | } else if (arg[1] == '+') { |
| 1498 | /* Kernel probes do not support file offsets */ |
| 1499 | if (ctx->flags & TPARG_FL_KERNEL) { |
| 1500 | trace_probe_log_err(ctx->offset, FILE_ON_KPROBE); |
| 1501 | return -EINVAL; |
| 1502 | } |
| 1503 | ret = kstrtol(arg + 2, 0, &offset); |
| 1504 | if (ret) { |
| 1505 | trace_probe_log_err(ctx->offset, BAD_FILE_OFFS); |
| 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | code->op = FETCH_OP_FOFFS; |
| 1510 | code->immediate = (unsigned long)offset; |
| 1511 | offset = 0; |
| 1512 | } else { |
| 1513 | /* uprobes don't support symbols */ |
| 1514 | if (!(ctx->flags & TPARG_FL_KERNEL)) { |
| 1515 | trace_probe_log_err(ctx->offset, SYM_ON_UPROBE); |
| 1516 | return -EINVAL; |
| 1517 | } |
| 1518 | /* Preserve symbol for updating */ |
| 1519 | code->op = FETCH_NOP_SYMBOL; |
| 1520 | code->data = kstrdup(arg + 1, GFP_KERNEL); |
| 1521 | if (!code->data) |
| 1522 | return -ENOMEM; |
| 1523 | if (++code == end) { |
| 1524 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 1525 | return -EINVAL; |
| 1526 | } |
| 1527 | code->op = FETCH_OP_IMM; |
| 1528 | code->immediate = 0; |
| 1529 | } |
| 1530 | /* These are fetching from memory */ |
| 1531 | if (++code == end) { |
| 1532 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 1533 | return -EINVAL; |
| 1534 | } |
| 1535 | *pcode = code; |
| 1536 | code->op = FETCH_OP_DEREF; |
| 1537 | code->offset = offset; |
| 1538 | return 0; |
| 1539 | } |
| 1540 | |
| 1541 | static int parse_probe_arg_deref(char *arg, struct traceprobe_parse_context *ctx) |
| 1542 | { |
| 1543 | int deref = FETCH_OP_DEREF; |
| 1544 | long offset = 0; |
| 1545 | char *tmp; |
| 1546 | int ret; |
| 1547 | |
| 1548 | if (arg[1] == 'u') { |
| 1549 | deref = FETCH_OP_UDEREF; |
| 1550 | arg[1] = arg[0]; |
| 1551 | arg++; |
| 1552 | } |
| 1553 | if (arg[0] == '+') |
| 1554 | arg++; /* Skip '+', because kstrtol() rejects it. */ |
| 1555 | tmp = strchr(arg, '('); |
| 1556 | if (!tmp) { |
| 1557 | trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE); |
| 1558 | return -EINVAL; |
| 1559 | } |
| 1560 | *tmp = '\0'; |
| 1561 | ret = kstrtol(arg, 0, &offset); |
| 1562 | if (ret) { |
| 1563 | trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS); |
| 1564 | return ret; |
| 1565 | } |
| 1566 | ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); |
| 1567 | arg = tmp + 1; |
| 1568 | |
| 1569 | tmp = strrchr(arg, ')'); |
| 1570 | if (!tmp) { |
| 1571 | trace_probe_log_err(ctx->offset + strlen(arg), |
| 1572 | DEREF_OPEN_BRACE); |
| 1573 | return -EINVAL; |
| 1574 | } |
| 1575 | *tmp = '\0'; |
| 1576 | |
| 1577 | ctx->stack[ctx->depth].type = STATE_DEREF; |
| 1578 | ctx->stack[ctx->depth].deref.deref = deref; |
| 1579 | ctx->stack[ctx->depth].deref.offset = offset; |
| 1580 | ctx->stack[ctx->depth].deref.cur_offs = ctx->offset; |
| 1581 | ctx->stack[ctx->depth].deref.inner_arg = arg; |
| 1582 | ctx->stack[ctx->depth].deref.is_cpu_read = false; |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | static int parse_probe_arg_imm(char *arg, struct fetch_insn *code, |
| 1587 | struct traceprobe_parse_context *ctx) |
| 1588 | { |
| 1589 | char *tmp; |
| 1590 | int ret; |
| 1591 | |
| 1592 | if (arg[1] == '"') { /* Immediate string */ |
| 1593 | ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2); |
| 1594 | if (ret) |
| 1595 | return ret; |
| 1596 | code->op = FETCH_OP_IMMSTR; |
| 1597 | code->data = tmp; |
| 1598 | } else { |
| 1599 | ret = str_to_immediate(arg + 1, &code->immediate); |
| 1600 | if (ret) { |
| 1601 | trace_probe_log_err(ctx->offset + 1, BAD_IMM); |
| 1602 | return ret; |
| 1603 | } |
| 1604 | code->op = FETCH_OP_IMM; |
| 1605 | } |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
| 1609 | static int parse_probe_arg_default(char *arg, struct fetch_insn **pcode, |
| 1610 | struct fetch_insn *end, |
| 1611 | struct traceprobe_parse_context *ctx) |
| 1612 | { |
| 1613 | int ret; |
| 1614 | |
| 1615 | if (isalpha(arg[0]) || arg[0] == '_') { |
| 1616 | /* BTF variable or event field */ |
| 1617 | if (ctx->flags & TPARG_FL_TEVENT) { |
| 1618 | ret = parse_trace_event(arg, *pcode, ctx); |
| 1619 | if (ret < 0) { |
| 1620 | trace_probe_log_err(ctx->offset, NO_EVENT_FIELD); |
| 1621 | return -EINVAL; |
| 1622 | } |
| 1623 | return 0; |
| 1624 | } |
| 1625 | if (!tparg_is_function_entry(ctx->flags) && |
| 1626 | !tparg_is_function_return(ctx->flags)) { |
| 1627 | trace_probe_log_err(ctx->offset, NOSUP_BTFARG); |
| 1628 | return -EINVAL; |
| 1629 | } |
| 1630 | return parse_btf_arg(arg, pcode, end, ctx); |
| 1631 | } |
| 1632 | |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | static int parse_probe_arg_nested(char **parg, struct traceprobe_parse_context *ctx) |
| 1637 | { |
| 1638 | char *arg = *parg; |
| 1639 | int ret; |
| 1640 | |
| 1641 | while (true) { |
| 1642 | /* Determine if this is a nested argument */ |
| 1643 | if (arg[0] != '+' && arg[0] != '-' && arg[0] != '(' && |
| 1644 | !str_has_prefix(arg, THIS_CPU_PTR_PREFIX) && |
| 1645 | !str_has_prefix(arg, THIS_CPU_READ_PREFIX)) |
| 1646 | break; |
| 1647 | |
| 1648 | /* If nested, check the maximum depth limit */ |
| 1649 | if (ctx->depth >= TRACEPROBE_MAX_NESTED_LEVEL) { |
| 1650 | trace_probe_log_err(ctx->offset, TOO_MANY_NESTED); |
| 1651 | return -E2BIG; |
| 1652 | } |
| 1653 | |
| 1654 | /* Perform the actual parsing subroutine calls */ |
| 1655 | switch (arg[0]) { |
| 1656 | case '+': |
| 1657 | case '-': |
| 1658 | ret = parse_probe_arg_deref(arg, ctx); |
| 1659 | if (ret) |
| 1660 | return ret; |
| 1661 | arg = ctx->stack[ctx->depth].deref.inner_arg; |
| 1662 | break; |
| 1663 | case '(': |
| 1664 | ret = handle_typecast(arg, ctx); |
| 1665 | if (ret) |
| 1666 | return ret; |
| 1667 | arg = ctx->stack[ctx->depth].typecast.inner_arg; |
| 1668 | break; |
| 1669 | default: |
| 1670 | ret = parse_this_cpu(arg, ctx); |
| 1671 | if (ret) |
| 1672 | return ret; |
| 1673 | arg = ctx->stack[ctx->depth].deref.inner_arg; |
| 1674 | break; |
| 1675 | } |
| 1676 | ctx->depth++; |
| 1677 | } |
| 1678 | |
| 1679 | *parg = arg; |
| 1680 | return 0; |
| 1681 | } |
| 1682 | |
| 1683 | static int parse_probe_arg_leaf(char *arg, const struct fetch_type *type, |
| 1684 | struct fetch_insn **pcode, struct fetch_insn *end, |
| 1685 | struct traceprobe_parse_context *ctx) |
| 1686 | { |
| 1687 | struct fetch_insn *code = *pcode; |
| 1688 | int ret; |
| 1689 | |
| 1690 | switch (arg[0]) { |
| 1691 | case '$': |
| 1692 | ret = parse_probe_vars(arg, type, pcode, end, ctx); |
| 1693 | break; |
| 1694 | case '%': /* named register */ |
| 1695 | ret = parse_probe_arg_register(arg, code, ctx); |
| 1696 | break; |
| 1697 | case '@': /* memory, file-offset or symbol */ |
| 1698 | ret = parse_probe_arg_mem_symbol(arg, pcode, end, ctx); |
| 1699 | break; |
| 1700 | case '\\': /* Immediate value */ |
| 1701 | ret = parse_probe_arg_imm(arg, code, ctx); |
| 1702 | break; |
| 1703 | default: |
| 1704 | ret = parse_probe_arg_default(arg, pcode, end, ctx); |
| 1705 | break; |
| 1706 | } |
| 1707 | |
| 1708 | if (ret) |
| 1709 | return ret; |
| 1710 | |
| 1711 | if (code->op == FETCH_OP_NOP) { |
| 1712 | /* Parsed, but do not find fetch method */ |
| 1713 | trace_probe_log_err(ctx->offset, BAD_FETCH_ARG); |
| 1714 | return -EINVAL; |
| 1715 | } |
| 1716 | |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | static int unwind_parse_states(struct fetch_insn **pcode, struct fetch_insn *end, |
| 1721 | struct traceprobe_parse_context *ctx) |
| 1722 | { |
| 1723 | struct parse_state *state; |
| 1724 | struct fetch_insn *code; |
| 1725 | int ret; |
| 1726 | |
| 1727 | while (ctx->depth > 0) { |
| 1728 | ctx->depth--; |
| 1729 | state = &ctx->stack[ctx->depth]; |
| 1730 | |
| 1731 | if (state->type == STATE_DEREF) { |
| 1732 | code = *pcode; |
| 1733 | ctx->offset = state->deref.cur_offs; |
| 1734 | if (code->op == FETCH_OP_COMM || code->op == FETCH_OP_IMMSTR) { |
| 1735 | trace_probe_log_err(ctx->offset, COMM_CANT_DEREF); |
| 1736 | return -EINVAL; |
| 1737 | } |
| 1738 | |
| 1739 | if (!(state->deref.deref == FETCH_OP_CPU_PTR && |
| 1740 | *state->deref.inner_arg == '@')) { |
| 1741 | code++; |
| 1742 | if (code == end) { |
| 1743 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 1744 | return -EINVAL; |
| 1745 | } |
| 1746 | } |
| 1747 | *pcode = code; |
| 1748 | |
| 1749 | code->op = state->deref.deref; |
| 1750 | code->offset = state->deref.offset; |
| 1751 | ctx->last_type = NULL; |
| 1752 | |
| 1753 | if (state->deref.is_cpu_read) { |
| 1754 | code = *pcode; |
| 1755 | code++; |
| 1756 | if (code == end) { |
| 1757 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 1758 | return -EINVAL; |
| 1759 | } |
| 1760 | code->op = FETCH_OP_DEREF; |
| 1761 | code->offset = 0; |
| 1762 | *pcode = code; |
| 1763 | } |
| 1764 | } else if (state->type == STATE_TYPECAST) { |
| 1765 | clear_struct_btf(ctx); |
| 1766 | |
| 1767 | /* resolve the typecast struct name */ |
| 1768 | ctx->offset = state->typecast.orig_offset + 1; /* for the '(' */ |
| 1769 | ret = parse_btf_casttype(state->typecast.casttype, ctx); |
| 1770 | if (ret < 0) |
| 1771 | return ret; |
| 1772 | |
| 1773 | ctx->offset = state->typecast.orig_offset + |
| 1774 | state->typecast.field_offset_diff; |
| 1775 | ret = parse_btf_field(state->typecast.fieldname, |
| 1776 | ctx->last_struct, pcode, |
| 1777 | end, ctx); |
| 1778 | ctx->prefix_byteoffs = 0; |
| 1779 | if (ret < 0) |
| 1780 | return ret; |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
| 1787 | /* Loop-based (non-recursive) argument parser */ |
| 1788 | static int |
| 1789 | parse_probe_arg(char *arg, const struct fetch_type *type, |
| 1790 | struct fetch_insn **pcode, struct fetch_insn *end, |
| 1791 | struct traceprobe_parse_context *ctx) |
| 1792 | { |
| 1793 | int ret; |
| 1794 | |
| 1795 | ctx->depth = 0; |
| 1796 | |
| 1797 | ret = parse_probe_arg_nested(&arg, ctx); |
| 1798 | if (ret) |
| 1799 | return ret; |
| 1800 | |
| 1801 | ret = parse_probe_arg_leaf(arg, type, pcode, end, ctx); |
| 1802 | if (ret) |
| 1803 | return ret; |
| 1804 | |
| 1805 | return unwind_parse_states(pcode, end, ctx); |
| 1806 | } |
| 1807 | |
| 1808 | /* Bitfield type needs to be parsed into a fetch function */ |
| 1809 | static int __parse_bitfield_probe_arg(const char *bf, |
| 1810 | const struct fetch_type *t, |
| 1811 | struct fetch_insn **pcode) |
| 1812 | { |
| 1813 | struct fetch_insn *code = *pcode; |
| 1814 | unsigned long bw, bo; |
| 1815 | char *tail; |
| 1816 | |
| 1817 | if (*bf != 'b') |
| 1818 | return 0; |
| 1819 | |
| 1820 | bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ |
| 1821 | |
| 1822 | if (bw == 0 || *tail != '@') |
| 1823 | return -EINVAL; |
| 1824 | |
| 1825 | bf = tail + 1; |
| 1826 | bo = simple_strtoul(bf, &tail, 0); |
| 1827 | |
| 1828 | if (tail == bf || *tail != '/') |
| 1829 | return -EINVAL; |
| 1830 | code++; |
| 1831 | if (code->op != FETCH_OP_NOP) |
| 1832 | return -EINVAL; |
| 1833 | *pcode = code; |
| 1834 | |
| 1835 | code->op = FETCH_OP_MOD_BF; |
| 1836 | code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); |
| 1837 | code->rshift = BYTES_TO_BITS(t->size) - bw; |
| 1838 | code->basesize = t->size; |
| 1839 | |
| 1840 | return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; |
| 1841 | } |
| 1842 | |
| 1843 | /* Split type part from @arg and return it. */ |
| 1844 | static char *parse_probe_arg_type(char *arg, struct probe_arg *parg, |
| 1845 | struct traceprobe_parse_context *ctx) |
| 1846 | { |
| 1847 | char *t = NULL, *t2, *t3; |
| 1848 | int offs; |
| 1849 | |
| 1850 | t = strchr(arg, ':'); |
| 1851 | if (t) { |
| 1852 | *t++ = '\0'; |
| 1853 | t2 = strchr(t, '['); |
| 1854 | if (t2) { |
| 1855 | *t2++ = '\0'; |
| 1856 | t3 = strchr(t2, ']'); |
| 1857 | if (!t3) { |
| 1858 | offs = t2 + strlen(t2) - arg; |
| 1859 | |
| 1860 | trace_probe_log_err(ctx->offset + offs, |
| 1861 | ARRAY_NO_CLOSE); |
| 1862 | return ERR_PTR(-EINVAL); |
| 1863 | } else if (t3[1] != '\0') { |
| 1864 | trace_probe_log_err(ctx->offset + t3 + 1 - arg, |
| 1865 | BAD_ARRAY_SUFFIX); |
| 1866 | return ERR_PTR(-EINVAL); |
| 1867 | } |
| 1868 | *t3 = '\0'; |
| 1869 | if (kstrtouint(t2, 0, &parg->count) || !parg->count) { |
| 1870 | trace_probe_log_err(ctx->offset + t2 - arg, |
| 1871 | BAD_ARRAY_NUM); |
| 1872 | return ERR_PTR(-EINVAL); |
| 1873 | } |
| 1874 | if (parg->count > MAX_ARRAY_LEN) { |
| 1875 | trace_probe_log_err(ctx->offset + t2 - arg, |
| 1876 | ARRAY_TOO_BIG); |
| 1877 | return ERR_PTR(-EINVAL); |
| 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | offs = t ? t - arg : 0; |
| 1882 | |
| 1883 | /* |
| 1884 | * Since $comm and immediate string can not be dereferenced, |
| 1885 | * we can find those by strcmp. But ignore for eprobes. |
| 1886 | */ |
| 1887 | if (!(ctx->flags & TPARG_FL_TEVENT) && |
| 1888 | (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 || |
| 1889 | strncmp(arg, "\\\"", 2) == 0)) { |
| 1890 | /* The type of $comm must be "string", and not an array type. */ |
| 1891 | if (parg->count || (t && strcmp(t, "string"))) { |
| 1892 | trace_probe_log_err(ctx->offset + offs, NEED_STRING_TYPE); |
| 1893 | return ERR_PTR(-EINVAL); |
| 1894 | } |
| 1895 | parg->type = find_fetch_type("string", ctx->flags); |
| 1896 | } else |
| 1897 | parg->type = find_fetch_type(t, ctx->flags); |
| 1898 | |
| 1899 | if (!parg->type) { |
| 1900 | trace_probe_log_err(ctx->offset + offs, BAD_TYPE); |
| 1901 | return ERR_PTR(-EINVAL); |
| 1902 | } |
| 1903 | |
| 1904 | return t; |
| 1905 | } |
| 1906 | |
| 1907 | /* After parsing, adjust the fetch_insn according to the probe_arg */ |
| 1908 | static int finalize_fetch_insn(struct fetch_insn *code, |
| 1909 | struct probe_arg *parg, |
| 1910 | char *type, |
| 1911 | int type_offset, |
| 1912 | struct traceprobe_parse_context *ctx) |
| 1913 | { |
| 1914 | struct fetch_insn *scode; |
| 1915 | int ret; |
| 1916 | |
| 1917 | /* Store operation */ |
| 1918 | if (parg->type->is_string) { |
| 1919 | /* Check bad combination of the type and the last fetch_insn. */ |
| 1920 | if (!strcmp(parg->type->name, "symstr")) { |
| 1921 | if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK && |
| 1922 | code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG && |
| 1923 | code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) { |
| 1924 | trace_probe_log_err(ctx->offset + type_offset, |
| 1925 | BAD_SYMSTRING); |
| 1926 | return -EINVAL; |
| 1927 | } |
| 1928 | } else { |
| 1929 | if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF && |
| 1930 | code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM && |
| 1931 | code->op != FETCH_OP_IMMSTR && code->op != FETCH_OP_TP_ARG) { |
| 1932 | trace_probe_log_err(ctx->offset + type_offset, |
| 1933 | BAD_STRING); |
| 1934 | return -EINVAL; |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | if (!strcmp(parg->type->name, "symstr") || |
| 1939 | (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM || |
| 1940 | code->op == FETCH_OP_IMMSTR) || code->op == FETCH_OP_TP_ARG || |
| 1941 | parg->count) { |
| 1942 | /* |
| 1943 | * IMM, DATA and COMM is pointing actual address, those |
| 1944 | * must be kept, and if parg->count != 0, this is an |
| 1945 | * array of string pointers instead of string address |
| 1946 | * itself. |
| 1947 | * For the symstr, it doesn't need to dereference, thus |
| 1948 | * it just get the value. |
| 1949 | */ |
| 1950 | code++; |
| 1951 | if (code->op != FETCH_OP_NOP) { |
| 1952 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 1953 | return -EINVAL; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | /* If op == DEREF, replace it with STRING */ |
| 1958 | if (!strcmp(parg->type->name, "ustring") || |
| 1959 | code->op == FETCH_OP_UDEREF) |
| 1960 | code->op = FETCH_OP_ST_USTRING; |
| 1961 | else if (!strcmp(parg->type->name, "symstr")) |
| 1962 | code->op = FETCH_OP_ST_SYMSTR; |
| 1963 | else |
| 1964 | code->op = FETCH_OP_ST_STRING; |
| 1965 | code->size = parg->type->size; |
| 1966 | parg->dynamic = true; |
| 1967 | } else if (code->op == FETCH_OP_DEREF) { |
| 1968 | code->op = FETCH_OP_ST_MEM; |
| 1969 | code->size = parg->type->size; |
| 1970 | } else if (code->op == FETCH_OP_UDEREF) { |
| 1971 | code->op = FETCH_OP_ST_UMEM; |
| 1972 | code->size = parg->type->size; |
| 1973 | } else { |
| 1974 | code++; |
| 1975 | if (code->op != FETCH_OP_NOP) { |
| 1976 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 1977 | return -E2BIG; |
| 1978 | } |
| 1979 | code->op = FETCH_OP_ST_RAW; |
| 1980 | code->size = parg->type->size; |
| 1981 | } |
| 1982 | |
| 1983 | /* Save storing fetch_insn. */ |
| 1984 | scode = code; |
| 1985 | |
| 1986 | /* Modify operation */ |
| 1987 | if (type != NULL) { |
| 1988 | /* Bitfield needs a special fetch_insn. */ |
| 1989 | ret = __parse_bitfield_probe_arg(type, parg->type, &code); |
| 1990 | if (ret) { |
| 1991 | trace_probe_log_err(ctx->offset + type_offset, BAD_BITFIELD); |
| 1992 | return ret; |
| 1993 | } |
| 1994 | } else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && |
| 1995 | ctx->last_type) { |
| 1996 | /* If user not specified the type, try parsing BTF bitfield. */ |
| 1997 | ret = parse_btf_bitfield(&code, ctx); |
| 1998 | if (ret) |
| 1999 | return ret; |
| 2000 | } |
| 2001 | |
| 2002 | /* Loop(Array) operation */ |
| 2003 | if (parg->count) { |
| 2004 | if (scode->op != FETCH_OP_ST_MEM && |
| 2005 | scode->op != FETCH_OP_ST_STRING && |
| 2006 | scode->op != FETCH_OP_ST_USTRING) { |
| 2007 | trace_probe_log_err(ctx->offset + type_offset, BAD_STRING); |
| 2008 | return -EINVAL; |
| 2009 | } |
| 2010 | code++; |
| 2011 | if (code->op != FETCH_OP_NOP) { |
| 2012 | trace_probe_log_err(ctx->offset, TOO_MANY_OPS); |
| 2013 | return -E2BIG; |
| 2014 | } |
| 2015 | code->op = FETCH_OP_LP_ARRAY; |
| 2016 | code->param = parg->count; |
| 2017 | } |
| 2018 | |
| 2019 | /* Finalize the fetch_insn array. */ |
| 2020 | code++; |
| 2021 | code->op = FETCH_OP_END; |
| 2022 | |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
| 2026 | /* String length checking wrapper */ |
| 2027 | static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, |
| 2028 | struct probe_arg *parg, |
| 2029 | struct traceprobe_parse_context *ctx) |
| 2030 | { |
| 2031 | struct fetch_insn *code, *tmp = NULL; |
| 2032 | char *type, *arg __free(kfree) = NULL; |
| 2033 | int ret, len; |
| 2034 | |
| 2035 | len = strlen(argv); |
| 2036 | if (len > MAX_ARGSTR_LEN) { |
| 2037 | trace_probe_log_err(ctx->offset, ARG_TOO_LONG); |
| 2038 | return -E2BIG; |
| 2039 | } else if (len == 0) { |
| 2040 | trace_probe_log_err(ctx->offset, NO_ARG_BODY); |
| 2041 | return -EINVAL; |
| 2042 | } |
| 2043 | |
| 2044 | arg = kstrdup(argv, GFP_KERNEL); |
| 2045 | if (!arg) |
| 2046 | return -ENOMEM; |
| 2047 | |
| 2048 | parg->comm = kstrdup(arg, GFP_KERNEL); |
| 2049 | if (!parg->comm) |
| 2050 | return -ENOMEM; |
| 2051 | |
| 2052 | type = parse_probe_arg_type(arg, parg, ctx); |
| 2053 | if (IS_ERR(type)) |
| 2054 | return PTR_ERR(type); |
| 2055 | |
| 2056 | code = tmp = kzalloc_objs(*code, FETCH_INSN_MAX); |
| 2057 | if (!code) |
| 2058 | return -ENOMEM; |
| 2059 | code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; |
| 2060 | |
| 2061 | ctx->last_type = NULL; |
| 2062 | ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], |
| 2063 | ctx); |
| 2064 | if (ret < 0) |
| 2065 | goto fail; |
| 2066 | /* Update storing type if BTF is available */ |
| 2067 | if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && |
| 2068 | ctx->last_type) { |
| 2069 | if (!type) { |
| 2070 | parg->type = find_fetch_type_from_btf_type(ctx); |
| 2071 | } else if (strstr(type, "string")) { |
| 2072 | ret = check_prepare_btf_string_fetch(type, &code, ctx); |
| 2073 | if (ret) |
| 2074 | goto fail; |
| 2075 | } |
| 2076 | } |
| 2077 | parg->offset = *size; |
| 2078 | *size += parg->type->size * (parg->count ?: 1); |
| 2079 | |
| 2080 | if (*size > MAX_PROBE_EVENT_SIZE) { |
| 2081 | ret = -E2BIG; |
| 2082 | trace_probe_log_err(ctx->offset, EVENT_TOO_BIG); |
| 2083 | goto fail; |
| 2084 | } |
| 2085 | |
| 2086 | if (parg->count) { |
| 2087 | len = strlen(parg->type->fmttype) + 6; |
| 2088 | parg->fmt = kmalloc(len, GFP_KERNEL); |
| 2089 | if (!parg->fmt) { |
| 2090 | ret = -ENOMEM; |
| 2091 | goto fail; |
| 2092 | } |
| 2093 | snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, |
| 2094 | parg->count); |
| 2095 | } |
| 2096 | |
| 2097 | ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx); |
| 2098 | if (ret < 0) |
| 2099 | goto fail; |
| 2100 | |
| 2101 | for (; code < tmp + FETCH_INSN_MAX; code++) |
| 2102 | if (code->op == FETCH_OP_END) |
| 2103 | break; |
| 2104 | /* Shrink down the code buffer */ |
| 2105 | parg->code = kzalloc_objs(*code, code - tmp + 1); |
| 2106 | if (!parg->code) |
| 2107 | ret = -ENOMEM; |
| 2108 | else |
| 2109 | memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); |
| 2110 | |
| 2111 | fail: |
| 2112 | if (ret < 0) { |
| 2113 | for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) |
| 2114 | if (code->op == FETCH_NOP_SYMBOL || |
| 2115 | code->op == FETCH_OP_IMMSTR) |
| 2116 | kfree(code->data); |
| 2117 | } |
| 2118 | kfree(tmp); |
| 2119 | |
| 2120 | /* struct_btf should not be passed to other arguments */ |
| 2121 | clear_struct_btf(ctx); |
| 2122 | |
| 2123 | return ret; |
| 2124 | } |
| 2125 | |
| 2126 | /* Return 1 if name is reserved or already used by another argument */ |
| 2127 | static int traceprobe_conflict_field_name(const char *name, |
| 2128 | struct probe_arg *args, int narg) |
| 2129 | { |
| 2130 | int i; |
| 2131 | |
| 2132 | for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) |
| 2133 | if (strcmp(reserved_field_names[i], name) == 0) |
| 2134 | return 1; |
| 2135 | |
| 2136 | for (i = 0; i < narg; i++) |
| 2137 | if (strcmp(args[i].name, name) == 0) |
| 2138 | return 1; |
| 2139 | |
| 2140 | return 0; |
| 2141 | } |
| 2142 | |
| 2143 | static char *generate_probe_arg_name(const char *arg, int idx) |
| 2144 | { |
| 2145 | char *name = NULL; |
| 2146 | const char *end; |
| 2147 | |
| 2148 | /* |
| 2149 | * If argument name is omitted, try arg as a name (BTF variable) |
| 2150 | * or "argN". |
| 2151 | */ |
| 2152 | if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) { |
| 2153 | end = strchr(arg, ':'); |
| 2154 | if (!end) |
| 2155 | end = arg + strlen(arg); |
| 2156 | |
| 2157 | name = kmemdup_nul(arg, end - arg, GFP_KERNEL); |
| 2158 | if (!name || !is_good_name(name)) { |
| 2159 | kfree(name); |
| 2160 | name = NULL; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | if (!name) |
| 2165 | name = kasprintf(GFP_KERNEL, "arg%d", idx + 1); |
| 2166 | |
| 2167 | return name; |
| 2168 | } |
| 2169 | |
| 2170 | int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg, |
| 2171 | struct traceprobe_parse_context *ctx) |
| 2172 | { |
| 2173 | struct probe_arg *parg = &tp->args[i]; |
| 2174 | const char *body; |
| 2175 | |
| 2176 | ctx->tp = tp; |
| 2177 | body = strchr(arg, '='); |
| 2178 | if (body) { |
| 2179 | if (body - arg > MAX_ARG_NAME_LEN) { |
| 2180 | trace_probe_log_err(0, ARG_NAME_TOO_LONG); |
| 2181 | return -EINVAL; |
| 2182 | } else if (body == arg) { |
| 2183 | trace_probe_log_err(0, NO_ARG_NAME); |
| 2184 | return -EINVAL; |
| 2185 | } |
| 2186 | parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); |
| 2187 | body++; |
| 2188 | } else { |
| 2189 | parg->name = generate_probe_arg_name(arg, i); |
| 2190 | body = arg; |
| 2191 | } |
| 2192 | if (!parg->name) |
| 2193 | return -ENOMEM; |
| 2194 | |
| 2195 | if (!is_good_name(parg->name)) { |
| 2196 | trace_probe_log_err(0, BAD_ARG_NAME); |
| 2197 | return -EINVAL; |
| 2198 | } |
| 2199 | if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { |
| 2200 | trace_probe_log_err(0, USED_ARG_NAME); |
| 2201 | return -EINVAL; |
| 2202 | } |
| 2203 | ctx->offset = body - arg; |
| 2204 | /* Parse fetch argument */ |
| 2205 | return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx); |
| 2206 | } |
| 2207 | |
| 2208 | void traceprobe_free_probe_arg(struct probe_arg *arg) |
| 2209 | { |
| 2210 | struct fetch_insn *code = arg->code; |
| 2211 | |
| 2212 | while (code && code->op != FETCH_OP_END) { |
| 2213 | if (code->op == FETCH_NOP_SYMBOL || |
| 2214 | code->op == FETCH_OP_IMMSTR) |
| 2215 | kfree(code->data); |
| 2216 | code++; |
| 2217 | } |
| 2218 | kfree(arg->code); |
| 2219 | kfree(arg->name); |
| 2220 | kfree(arg->comm); |
| 2221 | kfree(arg->fmt); |
| 2222 | } |
| 2223 | |
| 2224 | static int argv_has_var_arg(int argc, const char *argv[], int *args_idx, |
| 2225 | struct traceprobe_parse_context *ctx) |
| 2226 | { |
| 2227 | int i, found = 0; |
| 2228 | |
| 2229 | for (i = 0; i < argc; i++) |
| 2230 | if (str_has_prefix(argv[i], "$arg")) { |
| 2231 | trace_probe_log_set_index(i + 2); |
| 2232 | |
| 2233 | if (!tparg_is_function_entry(ctx->flags) && |
| 2234 | !tparg_is_function_return(ctx->flags)) { |
| 2235 | trace_probe_log_err(0, NOFENTRY_ARGS); |
| 2236 | return -EINVAL; |
| 2237 | } |
| 2238 | |
| 2239 | if (isdigit(argv[i][4])) { |
| 2240 | found = 1; |
| 2241 | continue; |
| 2242 | } |
| 2243 | |
| 2244 | if (argv[i][4] != '*') { |
| 2245 | trace_probe_log_err(0, BAD_VAR); |
| 2246 | return -EINVAL; |
| 2247 | } |
| 2248 | |
| 2249 | if (*args_idx >= 0 && *args_idx < argc) { |
| 2250 | trace_probe_log_err(0, DOUBLE_ARGS); |
| 2251 | return -EINVAL; |
| 2252 | } |
| 2253 | found = 1; |
| 2254 | *args_idx = i; |
| 2255 | } |
| 2256 | |
| 2257 | return found; |
| 2258 | } |
| 2259 | |
| 2260 | static int sprint_nth_btf_arg(int idx, const char *type, |
| 2261 | char *buf, int bufsize, |
| 2262 | struct traceprobe_parse_context *ctx) |
| 2263 | { |
| 2264 | const char *name; |
| 2265 | int ret; |
| 2266 | |
| 2267 | if (idx >= ctx->nr_params) { |
| 2268 | trace_probe_log_err(0, NO_BTFARG); |
| 2269 | return -ENOENT; |
| 2270 | } |
| 2271 | name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off); |
| 2272 | if (!name) { |
| 2273 | trace_probe_log_err(0, NO_BTF_ENTRY); |
| 2274 | return -ENOENT; |
| 2275 | } |
| 2276 | ret = snprintf(buf, bufsize, "%s%s", name, type); |
| 2277 | if (ret >= bufsize) { |
| 2278 | trace_probe_log_err(0, ARGS_2LONG); |
| 2279 | return -E2BIG; |
| 2280 | } |
| 2281 | return ret; |
| 2282 | } |
| 2283 | |
| 2284 | /* Return new_argv which must be freed after use */ |
| 2285 | const char **traceprobe_expand_meta_args(int argc, const char *argv[], |
| 2286 | int *new_argc, char *buf, int bufsize, |
| 2287 | struct traceprobe_parse_context *ctx) |
| 2288 | { |
| 2289 | const struct btf_param *params = NULL; |
| 2290 | int i, j, n, used, ret, args_idx = -1; |
| 2291 | const char **new_argv __free(kfree) = NULL; |
| 2292 | |
| 2293 | ret = argv_has_var_arg(argc, argv, &args_idx, ctx); |
| 2294 | if (ret < 0) |
| 2295 | return ERR_PTR(ret); |
| 2296 | |
| 2297 | if (!ret) { |
| 2298 | *new_argc = argc; |
| 2299 | return NULL; |
| 2300 | } |
| 2301 | |
| 2302 | ret = query_btf_context(ctx); |
| 2303 | if (ret < 0 || ctx->nr_params == 0) { |
| 2304 | if (args_idx != -1) { |
| 2305 | /* $arg* requires BTF info */ |
| 2306 | trace_probe_log_err(0, NOSUP_BTFARG); |
| 2307 | return (const char **)params; |
| 2308 | } |
| 2309 | *new_argc = argc; |
| 2310 | return NULL; |
| 2311 | } |
| 2312 | |
| 2313 | if (args_idx >= 0) |
| 2314 | *new_argc = argc + ctx->nr_params - 1; |
| 2315 | else |
| 2316 | *new_argc = argc; |
| 2317 | |
| 2318 | new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL); |
| 2319 | if (!new_argv) |
| 2320 | return ERR_PTR(-ENOMEM); |
| 2321 | |
| 2322 | used = 0; |
| 2323 | for (i = 0, j = 0; i < argc; i++) { |
| 2324 | trace_probe_log_set_index(i + 2); |
| 2325 | if (i == args_idx) { |
| 2326 | for (n = 0; n < ctx->nr_params; n++) { |
| 2327 | ret = sprint_nth_btf_arg(n, "", buf + used, |
| 2328 | bufsize - used, ctx); |
| 2329 | if (ret < 0) |
| 2330 | return ERR_PTR(ret); |
| 2331 | |
| 2332 | new_argv[j++] = buf + used; |
| 2333 | used += ret + 1; |
| 2334 | } |
| 2335 | continue; |
| 2336 | } |
| 2337 | |
| 2338 | if (str_has_prefix(argv[i], "$arg")) { |
| 2339 | char *type = NULL; |
| 2340 | |
| 2341 | n = simple_strtoul(argv[i] + 4, &type, 10); |
| 2342 | if (type && !(*type == ':' || *type == '\0')) { |
| 2343 | trace_probe_log_err(0, BAD_VAR); |
| 2344 | return ERR_PTR(-ENOENT); |
| 2345 | } |
| 2346 | /* Note: $argN starts from $arg1, so $arg0 is invalid. */ |
| 2347 | if (n == 0) { |
| 2348 | trace_probe_log_err(0, BAD_ARG_NUM); |
| 2349 | return ERR_PTR(-EINVAL); |
| 2350 | } |
| 2351 | ret = sprint_nth_btf_arg(n - 1, type, buf + used, |
| 2352 | bufsize - used, ctx); |
| 2353 | if (ret < 0) |
| 2354 | return ERR_PTR(ret); |
| 2355 | new_argv[j++] = buf + used; |
| 2356 | used += ret + 1; |
| 2357 | } else |
| 2358 | new_argv[j++] = argv[i]; |
| 2359 | } |
| 2360 | |
| 2361 | return_ptr(new_argv); |
| 2362 | } |
| 2363 | |
| 2364 | /* @buf: *buf must be equal to NULL. Caller must to free *buf */ |
| 2365 | int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf) |
| 2366 | { |
| 2367 | int i, used, ret; |
| 2368 | const int bufsize = MAX_DENTRY_ARGS_LEN; |
| 2369 | char *tmpbuf __free(kfree) = NULL; |
| 2370 | |
| 2371 | if (*buf) |
| 2372 | return -EINVAL; |
| 2373 | |
| 2374 | used = 0; |
| 2375 | for (i = 0; i < argc; i++) { |
| 2376 | char *tmp __free(kfree) = NULL; |
| 2377 | char *equal; |
| 2378 | size_t arg_len; |
| 2379 | |
| 2380 | if (!glob_match("*:%p[dD]", argv[i])) |
| 2381 | continue; |
| 2382 | |
| 2383 | if (!tmpbuf) { |
| 2384 | tmpbuf = kmalloc(bufsize, GFP_KERNEL); |
| 2385 | if (!tmpbuf) |
| 2386 | return -ENOMEM; |
| 2387 | } |
| 2388 | |
| 2389 | tmp = kstrdup(argv[i], GFP_KERNEL); |
| 2390 | if (!tmp) |
| 2391 | return -ENOMEM; |
| 2392 | |
| 2393 | equal = strchr(tmp, '='); |
| 2394 | if (equal) |
| 2395 | *equal = '\0'; |
| 2396 | arg_len = strlen(argv[i]); |
| 2397 | tmp[arg_len - 4] = '\0'; |
| 2398 | if (argv[i][arg_len - 1] == 'd') |
| 2399 | ret = snprintf(tmpbuf + used, bufsize - used, |
| 2400 | "%s%s+0x0(+0x%zx(%s)):string", |
| 2401 | equal ? tmp : "", equal ? "=" : "", |
| 2402 | offsetof(struct dentry, d_name.name), |
| 2403 | equal ? equal + 1 : tmp); |
| 2404 | else |
| 2405 | ret = snprintf(tmpbuf + used, bufsize - used, |
| 2406 | "%s%s+0x0(+0x%zx(+0x%zx(%s))):string", |
| 2407 | equal ? tmp : "", equal ? "=" : "", |
| 2408 | offsetof(struct dentry, d_name.name), |
| 2409 | offsetof(struct file, f_path.dentry), |
| 2410 | equal ? equal + 1 : tmp); |
| 2411 | |
| 2412 | if (ret >= bufsize - used) |
| 2413 | return -ENOMEM; |
| 2414 | argv[i] = tmpbuf + used; |
| 2415 | used += ret + 1; |
| 2416 | } |
| 2417 | |
| 2418 | *buf = no_free_ptr(tmpbuf); |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
| 2422 | void traceprobe_finish_parse(struct traceprobe_parse_context *ctx) |
| 2423 | { |
| 2424 | clear_btf_context(ctx); |
| 2425 | } |
| 2426 | |
| 2427 | int traceprobe_update_arg(struct probe_arg *arg) |
| 2428 | { |
| 2429 | struct fetch_insn *code = arg->code; |
| 2430 | long offset; |
| 2431 | char *tmp; |
| 2432 | char c; |
| 2433 | int ret = 0; |
| 2434 | |
| 2435 | while (code && code->op != FETCH_OP_END) { |
| 2436 | if (code->op == FETCH_NOP_SYMBOL) { |
| 2437 | if (code[1].op != FETCH_OP_IMM) |
| 2438 | return -EINVAL; |
| 2439 | |
| 2440 | tmp = strpbrk(code->data, "+-"); |
| 2441 | if (tmp) |
| 2442 | c = *tmp; |
| 2443 | ret = traceprobe_split_symbol_offset(code->data, |
| 2444 | &offset); |
| 2445 | if (ret) |
| 2446 | return ret; |
| 2447 | |
| 2448 | code[1].immediate = |
| 2449 | (unsigned long)kallsyms_lookup_name(code->data); |
| 2450 | if (tmp) |
| 2451 | *tmp = c; |
| 2452 | if (!code[1].immediate) |
| 2453 | return -ENOENT; |
| 2454 | code[1].immediate += offset; |
| 2455 | } |
| 2456 | code++; |
| 2457 | } |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
| 2461 | /* When len=0, we just calculate the needed length */ |
| 2462 | #define LEN_OR_ZERO (len > pos ? len - pos : 0) |
| 2463 | static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, |
| 2464 | enum probe_print_type ptype) |
| 2465 | { |
| 2466 | struct probe_arg *parg; |
| 2467 | int i, j; |
| 2468 | int pos = 0; |
| 2469 | const char *fmt, *arg; |
| 2470 | |
| 2471 | switch (ptype) { |
| 2472 | case PROBE_PRINT_NORMAL: |
| 2473 | fmt = "(%lx)"; |
| 2474 | arg = ", REC->" FIELD_STRING_IP; |
| 2475 | break; |
| 2476 | case PROBE_PRINT_RETURN: |
| 2477 | fmt = "(%lx <- %lx)"; |
| 2478 | arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; |
| 2479 | break; |
| 2480 | case PROBE_PRINT_EVENT: |
| 2481 | fmt = ""; |
| 2482 | arg = ""; |
| 2483 | break; |
| 2484 | default: |
| 2485 | WARN_ON_ONCE(1); |
| 2486 | return 0; |
| 2487 | } |
| 2488 | |
| 2489 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); |
| 2490 | |
| 2491 | for (i = 0; i < tp->nr_args; i++) { |
| 2492 | parg = tp->args + i; |
| 2493 | pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); |
| 2494 | if (parg->count) { |
| 2495 | pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", |
| 2496 | parg->type->fmt); |
| 2497 | for (j = 1; j < parg->count; j++) |
| 2498 | pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", |
| 2499 | parg->type->fmt); |
| 2500 | pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); |
| 2501 | } else |
| 2502 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", |
| 2503 | parg->type->fmt); |
| 2504 | } |
| 2505 | |
| 2506 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg); |
| 2507 | |
| 2508 | for (i = 0; i < tp->nr_args; i++) { |
| 2509 | parg = tp->args + i; |
| 2510 | if (parg->count) { |
| 2511 | if (parg->type->is_string) |
| 2512 | fmt = ", __get_str(%s[%d])"; |
| 2513 | else |
| 2514 | fmt = ", REC->%s[%d]"; |
| 2515 | for (j = 0; j < parg->count; j++) |
| 2516 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 2517 | fmt, parg->name, j); |
| 2518 | } else { |
| 2519 | if (parg->type->is_string) |
| 2520 | fmt = ", __get_str(%s)"; |
| 2521 | else |
| 2522 | fmt = ", REC->%s"; |
| 2523 | pos += snprintf(buf + pos, LEN_OR_ZERO, |
| 2524 | fmt, parg->name); |
| 2525 | } |
| 2526 | } |
| 2527 | |
| 2528 | /* return the length of print_fmt */ |
| 2529 | return pos; |
| 2530 | } |
| 2531 | #undef LEN_OR_ZERO |
| 2532 | |
| 2533 | int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype) |
| 2534 | { |
| 2535 | struct trace_event_call *call = trace_probe_event_call(tp); |
| 2536 | int len; |
| 2537 | char *print_fmt; |
| 2538 | |
| 2539 | /* First: called with 0 length to calculate the needed length */ |
| 2540 | len = __set_print_fmt(tp, NULL, 0, ptype); |
| 2541 | print_fmt = kmalloc(len + 1, GFP_KERNEL); |
| 2542 | if (!print_fmt) |
| 2543 | return -ENOMEM; |
| 2544 | |
| 2545 | /* Second: actually write the @print_fmt */ |
| 2546 | __set_print_fmt(tp, print_fmt, len + 1, ptype); |
| 2547 | call->print_fmt = print_fmt; |
| 2548 | |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
| 2552 | int traceprobe_define_arg_fields(struct trace_event_call *event_call, |
| 2553 | size_t offset, struct trace_probe *tp) |
| 2554 | { |
| 2555 | int ret, i; |
| 2556 | |
| 2557 | /* Set argument names as fields */ |
| 2558 | for (i = 0; i < tp->nr_args; i++) { |
| 2559 | struct probe_arg *parg = &tp->args[i]; |
| 2560 | const char *fmt = parg->type->fmttype; |
| 2561 | int size = parg->type->size; |
| 2562 | |
| 2563 | if (parg->fmt) |
| 2564 | fmt = parg->fmt; |
| 2565 | if (parg->count) |
| 2566 | size *= parg->count; |
| 2567 | ret = trace_define_field(event_call, fmt, parg->name, |
| 2568 | offset + parg->offset, size, |
| 2569 | parg->type->is_signed, |
| 2570 | FILTER_OTHER); |
| 2571 | if (ret) |
| 2572 | return ret; |
| 2573 | } |
| 2574 | return 0; |
| 2575 | } |
| 2576 | |
| 2577 | static void trace_probe_event_free(struct trace_probe_event *tpe) |
| 2578 | { |
| 2579 | kfree(tpe->class.system); |
| 2580 | kfree(tpe->call.name); |
| 2581 | kfree(tpe->call.print_fmt); |
| 2582 | kfree(tpe); |
| 2583 | } |
| 2584 | |
| 2585 | int trace_probe_append(struct trace_probe *tp, struct trace_probe *to) |
| 2586 | { |
| 2587 | if (trace_probe_has_sibling(tp)) |
| 2588 | return -EBUSY; |
| 2589 | |
| 2590 | list_del_init(&tp->list); |
| 2591 | trace_probe_event_free(tp->event); |
| 2592 | |
| 2593 | tp->event = to->event; |
| 2594 | list_add_tail(&tp->list, trace_probe_probe_list(to)); |
| 2595 | |
| 2596 | return 0; |
| 2597 | } |
| 2598 | |
| 2599 | void trace_probe_unlink(struct trace_probe *tp) |
| 2600 | { |
| 2601 | list_del_init(&tp->list); |
| 2602 | if (list_empty(trace_probe_probe_list(tp))) |
| 2603 | trace_probe_event_free(tp->event); |
| 2604 | tp->event = NULL; |
| 2605 | } |
| 2606 | |
| 2607 | void trace_probe_cleanup(struct trace_probe *tp) |
| 2608 | { |
| 2609 | int i; |
| 2610 | |
| 2611 | for (i = 0; i < tp->nr_args; i++) |
| 2612 | traceprobe_free_probe_arg(&tp->args[i]); |
| 2613 | |
| 2614 | if (tp->entry_arg) { |
| 2615 | kfree(tp->entry_arg); |
| 2616 | tp->entry_arg = NULL; |
| 2617 | } |
| 2618 | |
| 2619 | if (tp->event) |
| 2620 | trace_probe_unlink(tp); |
| 2621 | } |
| 2622 | |
| 2623 | int trace_probe_init(struct trace_probe *tp, const char *event, |
| 2624 | const char *group, bool alloc_filter, int nargs) |
| 2625 | { |
| 2626 | struct trace_event_call *call; |
| 2627 | size_t size = sizeof(struct trace_probe_event); |
| 2628 | int ret = 0; |
| 2629 | |
| 2630 | if (!event || !group) |
| 2631 | return -EINVAL; |
| 2632 | |
| 2633 | if (alloc_filter) |
| 2634 | size += sizeof(struct trace_uprobe_filter); |
| 2635 | |
| 2636 | tp->event = kzalloc(size, GFP_KERNEL); |
| 2637 | if (!tp->event) |
| 2638 | return -ENOMEM; |
| 2639 | |
| 2640 | INIT_LIST_HEAD(&tp->event->files); |
| 2641 | INIT_LIST_HEAD(&tp->event->class.fields); |
| 2642 | INIT_LIST_HEAD(&tp->event->probes); |
| 2643 | INIT_LIST_HEAD(&tp->list); |
| 2644 | list_add(&tp->list, &tp->event->probes); |
| 2645 | |
| 2646 | call = trace_probe_event_call(tp); |
| 2647 | call->class = &tp->event->class; |
| 2648 | call->name = kstrdup(event, GFP_KERNEL); |
| 2649 | if (!call->name) { |
| 2650 | ret = -ENOMEM; |
| 2651 | goto error; |
| 2652 | } |
| 2653 | |
| 2654 | tp->event->class.system = kstrdup(group, GFP_KERNEL); |
| 2655 | if (!tp->event->class.system) { |
| 2656 | ret = -ENOMEM; |
| 2657 | goto error; |
| 2658 | } |
| 2659 | |
| 2660 | tp->nr_args = nargs; |
| 2661 | /* Make sure pointers in args[] are NULL */ |
| 2662 | if (nargs) |
| 2663 | memset(tp->args, 0, sizeof(tp->args[0]) * nargs); |
| 2664 | |
| 2665 | return 0; |
| 2666 | |
| 2667 | error: |
| 2668 | trace_probe_cleanup(tp); |
| 2669 | return ret; |
| 2670 | } |
| 2671 | |
| 2672 | static struct trace_event_call * |
| 2673 | find_trace_event_call(const char *system, const char *event_name) |
| 2674 | { |
| 2675 | struct trace_event_call *tp_event; |
| 2676 | const char *name; |
| 2677 | |
| 2678 | list_for_each_entry(tp_event, &ftrace_events, list) { |
| 2679 | if (!tp_event->class->system || |
| 2680 | strcmp(system, tp_event->class->system)) |
| 2681 | continue; |
| 2682 | name = trace_event_name(tp_event); |
| 2683 | if (!name || strcmp(event_name, name)) |
| 2684 | continue; |
| 2685 | return tp_event; |
| 2686 | } |
| 2687 | |
| 2688 | return NULL; |
| 2689 | } |
| 2690 | |
| 2691 | int trace_probe_register_event_call(struct trace_probe *tp) |
| 2692 | { |
| 2693 | struct trace_event_call *call = trace_probe_event_call(tp); |
| 2694 | int ret; |
| 2695 | |
| 2696 | lockdep_assert_held(&event_mutex); |
| 2697 | |
| 2698 | if (find_trace_event_call(trace_probe_group_name(tp), |
| 2699 | trace_probe_name(tp))) |
| 2700 | return -EEXIST; |
| 2701 | |
| 2702 | ret = register_trace_event(&call->event); |
| 2703 | if (!ret) |
| 2704 | return -ENODEV; |
| 2705 | |
| 2706 | ret = trace_add_event_call(call); |
| 2707 | if (ret) |
| 2708 | unregister_trace_event(&call->event); |
| 2709 | |
| 2710 | return ret; |
| 2711 | } |
| 2712 | |
| 2713 | int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) |
| 2714 | { |
| 2715 | struct event_file_link *link; |
| 2716 | |
| 2717 | link = kmalloc_obj(*link); |
| 2718 | if (!link) |
| 2719 | return -ENOMEM; |
| 2720 | |
| 2721 | link->file = file; |
| 2722 | INIT_LIST_HEAD(&link->list); |
| 2723 | list_add_tail_rcu(&link->list, &tp->event->files); |
| 2724 | trace_probe_set_flag(tp, TP_FLAG_TRACE); |
| 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, |
| 2729 | struct trace_event_file *file) |
| 2730 | { |
| 2731 | struct event_file_link *link; |
| 2732 | |
| 2733 | trace_probe_for_each_link(link, tp) { |
| 2734 | if (link->file == file) |
| 2735 | return link; |
| 2736 | } |
| 2737 | |
| 2738 | return NULL; |
| 2739 | } |
| 2740 | |
| 2741 | int trace_probe_remove_file(struct trace_probe *tp, |
| 2742 | struct trace_event_file *file) |
| 2743 | { |
| 2744 | struct event_file_link *link; |
| 2745 | |
| 2746 | link = trace_probe_get_file_link(tp, file); |
| 2747 | if (!link) |
| 2748 | return -ENOENT; |
| 2749 | |
| 2750 | list_del_rcu(&link->list); |
| 2751 | kvfree_rcu_mightsleep(link); |
| 2752 | |
| 2753 | if (list_empty(&tp->event->files)) |
| 2754 | trace_probe_clear_flag(tp, TP_FLAG_TRACE); |
| 2755 | |
| 2756 | return 0; |
| 2757 | } |
| 2758 | |
| 2759 | /* |
| 2760 | * Return the smallest index of different type argument (start from 1). |
| 2761 | * If all argument types and name are same, return 0. |
| 2762 | */ |
| 2763 | int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b) |
| 2764 | { |
| 2765 | int i; |
| 2766 | |
| 2767 | /* In case of more arguments */ |
| 2768 | if (a->nr_args < b->nr_args) |
| 2769 | return a->nr_args + 1; |
| 2770 | if (a->nr_args > b->nr_args) |
| 2771 | return b->nr_args + 1; |
| 2772 | |
| 2773 | for (i = 0; i < a->nr_args; i++) { |
| 2774 | if ((a->args[i].type != b->args[i].type) || |
| 2775 | (a->args[i].count != b->args[i].count) || |
| 2776 | strcmp(a->args[i].name, b->args[i].name)) |
| 2777 | return i + 1; |
| 2778 | } |
| 2779 | |
| 2780 | return 0; |
| 2781 | } |
| 2782 | |
| 2783 | bool trace_probe_match_command_args(struct trace_probe *tp, |
| 2784 | int argc, const char **argv) |
| 2785 | { |
| 2786 | int i; |
| 2787 | |
| 2788 | if (tp->nr_args < argc) |
| 2789 | return false; |
| 2790 | |
| 2791 | for (i = 0; i < argc; i++) { |
| 2792 | int len = strlen(tp->args[i].name); |
| 2793 | |
| 2794 | if (strncmp(argv[i], tp->args[i].name, len) || |
| 2795 | argv[i][len] != '=' || |
| 2796 | strcmp(argv[i] + len + 1, tp->args[i].comm)) |
| 2797 | return false; |
| 2798 | } |
| 2799 | return true; |
| 2800 | } |
| 2801 | |
| 2802 | int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **)) |
| 2803 | { |
| 2804 | int argc = 0, ret = 0; |
| 2805 | char **argv; |
| 2806 | |
| 2807 | argv = argv_split(GFP_KERNEL, raw_command, &argc); |
| 2808 | if (!argv) |
| 2809 | return -ENOMEM; |
| 2810 | |
| 2811 | if (argc) |
| 2812 | ret = createfn(argc, (const char **)argv); |
| 2813 | |
| 2814 | argv_free(argv); |
| 2815 | |
| 2816 | return ret; |
| 2817 | } |
| 2818 | |
| 2819 | int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args, |
| 2820 | u8 *data, void *field) |
| 2821 | { |
| 2822 | void *p; |
| 2823 | int i, j; |
| 2824 | |
| 2825 | for (i = 0; i < nr_args; i++) { |
| 2826 | struct probe_arg *a = args + i; |
| 2827 | |
| 2828 | trace_seq_printf(s, " %s=", a->name); |
| 2829 | if (likely(!a->count)) { |
| 2830 | if (!a->type->print(s, data + a->offset, field)) |
| 2831 | return -ENOMEM; |
| 2832 | continue; |
| 2833 | } |
| 2834 | trace_seq_putc(s, '{'); |
| 2835 | p = data + a->offset; |
| 2836 | for (j = 0; j < a->count; j++) { |
| 2837 | if (!a->type->print(s, p, field)) |
| 2838 | return -ENOMEM; |
| 2839 | trace_seq_putc(s, j == a->count - 1 ? '}' : ','); |
| 2840 | p += a->type->size; |
| 2841 | } |
| 2842 | } |
| 2843 | return 0; |
| 2844 | } |
| 2845 | |
| 2846 | #ifdef CONFIG_PROBE_EVENTS_DUMP_FETCHARG |
| 2847 | |
| 2848 | struct fetch_op_decode { |
| 2849 | const char *name; |
| 2850 | void (*decode)(struct seq_file *m, struct fetch_insn *insn); |
| 2851 | }; |
| 2852 | |
| 2853 | static const struct fetch_op_decode fetch_op_decode[]; |
| 2854 | |
| 2855 | static void fetcharg_decode_none(struct seq_file *m, struct fetch_insn *insn) |
| 2856 | { |
| 2857 | seq_puts(m, fetch_op_decode[insn->op].name); |
| 2858 | } |
| 2859 | |
| 2860 | static void fetcharg_decode_param(struct seq_file *m, struct fetch_insn *insn) |
| 2861 | { |
| 2862 | seq_printf(m, "%s(%u)", fetch_op_decode[insn->op].name, insn->param); |
| 2863 | } |
| 2864 | |
| 2865 | static void fetcharg_decode_imm(struct seq_file *m, struct fetch_insn *insn) |
| 2866 | { |
| 2867 | seq_printf(m, "%s(0x%lx)", fetch_op_decode[insn->op].name, insn->immediate); |
| 2868 | } |
| 2869 | |
| 2870 | static void fetcharg_decode_string(struct seq_file *m, struct fetch_insn *insn) |
| 2871 | { |
| 2872 | seq_printf(m, "%s(%s)", fetch_op_decode[insn->op].name, (char *)insn->data); |
| 2873 | } |
| 2874 | |
| 2875 | static void fetcharg_decode_symbol(struct seq_file *m, struct fetch_insn *insn) |
| 2876 | { |
| 2877 | seq_printf(m, "%s(%s)", fetch_op_decode[insn->op].name, (char *)insn->data); |
| 2878 | } |
| 2879 | |
| 2880 | static void fetcharg_decode_offset(struct seq_file *m, struct fetch_insn *insn) |
| 2881 | { |
| 2882 | seq_printf(m, "%s(offset=%d)", fetch_op_decode[insn->op].name, insn->offset); |
| 2883 | } |
| 2884 | |
| 2885 | static void fetcharg_decode_store(struct seq_file *m, struct fetch_insn *insn) |
| 2886 | { |
| 2887 | if (insn->op == FETCH_OP_ST_RAW) |
| 2888 | seq_printf(m, "%s(size=%u)", fetch_op_decode[insn->op].name, insn->size); |
| 2889 | else |
| 2890 | seq_printf(m, "%s(offset=%d,size=%u)", fetch_op_decode[insn->op].name, |
| 2891 | insn->offset, insn->size); |
| 2892 | } |
| 2893 | |
| 2894 | static void fetcharg_decode_bf(struct seq_file *m, struct fetch_insn *insn) |
| 2895 | { |
| 2896 | seq_printf(m, "%s(basesize=%u,lshift=%u,rshift=%u)", |
| 2897 | fetch_op_decode[insn->op].name, insn->basesize, insn->lshift, insn->rshift); |
| 2898 | } |
| 2899 | |
| 2900 | static void fetcharg_decode_tp_arg(struct seq_file *m, struct fetch_insn *insn) |
| 2901 | { |
| 2902 | struct ftrace_event_field *field = insn->data; |
| 2903 | |
| 2904 | seq_printf(m, "%s(%s)", fetch_op_decode[insn->op].name, field->name); |
| 2905 | } |
| 2906 | |
| 2907 | #define FETCH_OP(opname, decode_fn) \ |
| 2908 | [FETCH_OP_##opname] = { .name = #opname, .decode = fetcharg_decode_##decode_fn } |
| 2909 | |
| 2910 | static const struct fetch_op_decode fetch_op_decode[] = FETCH_OP_LIST; |
| 2911 | #undef FETCH_OP |
| 2912 | |
| 2913 | static void trace_probe_dump_arg(struct seq_file *m, struct probe_arg *parg) |
| 2914 | { |
| 2915 | int i; |
| 2916 | |
| 2917 | seq_printf(m, "# %s: ", parg->name); |
| 2918 | for (i = 0; i < FETCH_INSN_MAX; i++) { |
| 2919 | struct fetch_insn *insn = parg->code + i; |
| 2920 | |
| 2921 | if (insn->op >= ARRAY_SIZE(fetch_op_decode) || !fetch_op_decode[insn->op].decode) |
| 2922 | seq_printf(m, "unknown(%d)", insn->op); |
| 2923 | else |
| 2924 | fetch_op_decode[insn->op].decode(m, insn); |
| 2925 | |
| 2926 | if (insn->op == FETCH_OP_END) |
| 2927 | break; |
| 2928 | seq_puts(m, " -> "); |
| 2929 | } |
| 2930 | seq_putc(m, '\n'); |
| 2931 | } |
| 2932 | |
| 2933 | void trace_probe_dump_args(struct seq_file *m, struct trace_probe *tp) |
| 2934 | { |
| 2935 | int i; |
| 2936 | |
| 2937 | for (i = 0; i < tp->nr_args; i++) |
| 2938 | trace_probe_dump_arg(m, &tp->args[i]); |
| 2939 | } |
| 2940 | #endif /* CONFIG_PROBE_EVENTS_DUMP_FETCHARG */ |