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) 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 #ifdef CONFIG_PROBE_EVENTS_BTF_ARGS 336 337 static u32 btf_type_int(const struct btf_type *t) 338 { 339 return *(u32 *)(t + 1); 340 } 341 342 static bool btf_type_is_char_ptr(struct btf *btf, const struct btf_type *type) 343 { 344 const struct btf_type *real_type; 345 u32 intdata; 346 s32 tid; 347 348 real_type = btf_type_skip_modifiers(btf, type->type, &tid); 349 if (!real_type) 350 return false; 351 352 if (BTF_INFO_KIND(real_type->info) != BTF_KIND_INT) 353 return false; 354 355 intdata = btf_type_int(real_type); 356 return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) 357 && BTF_INT_BITS(intdata) == 8; 358 } 359 360 static bool btf_type_is_char_array(struct btf *btf, const struct btf_type *type) 361 { 362 const struct btf_type *real_type; 363 const struct btf_array *array; 364 u32 intdata; 365 s32 tid; 366 367 if (BTF_INFO_KIND(type->info) != BTF_KIND_ARRAY) 368 return false; 369 370 array = (const struct btf_array *)(type + 1); 371 372 real_type = btf_type_skip_modifiers(btf, array->type, &tid); 373 374 intdata = btf_type_int(real_type); 375 return !(BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) 376 && BTF_INT_BITS(intdata) == 8; 377 } 378 379 static int check_prepare_btf_string_fetch(char *typename, 380 struct fetch_insn **pcode, 381 struct traceprobe_parse_context *ctx) 382 { 383 struct btf *btf = ctx->btf; 384 385 if (!btf || !ctx->last_type) 386 return 0; 387 388 /* char [] does not need any change. */ 389 if (btf_type_is_char_array(btf, ctx->last_type)) 390 return 0; 391 392 /* char * requires dereference the pointer. */ 393 if (btf_type_is_char_ptr(btf, ctx->last_type)) { 394 struct fetch_insn *code = *pcode + 1; 395 396 if (code->op == FETCH_OP_END) { 397 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 398 return -E2BIG; 399 } 400 if (typename[0] == 'u') 401 code->op = FETCH_OP_UDEREF; 402 else 403 code->op = FETCH_OP_DEREF; 404 code->offset = 0; 405 *pcode = code; 406 return 0; 407 } 408 /* Other types are not available for string */ 409 trace_probe_log_err(ctx->offset, BAD_TYPE4STR); 410 return -EINVAL; 411 } 412 413 static const char *fetch_type_from_btf_type(struct btf *btf, 414 const struct btf_type *type, 415 struct traceprobe_parse_context *ctx) 416 { 417 u32 intdata; 418 419 /* TODO: const char * could be converted as a string */ 420 switch (BTF_INFO_KIND(type->info)) { 421 case BTF_KIND_ENUM: 422 /* enum is "int", so convert to "s32" */ 423 return "s32"; 424 case BTF_KIND_ENUM64: 425 return "s64"; 426 case BTF_KIND_PTR: 427 /* pointer will be converted to "x??" */ 428 if (IS_ENABLED(CONFIG_64BIT)) 429 return "x64"; 430 else 431 return "x32"; 432 case BTF_KIND_INT: 433 intdata = btf_type_int(type); 434 if (BTF_INT_ENCODING(intdata) & BTF_INT_SIGNED) { 435 switch (BTF_INT_BITS(intdata)) { 436 case 8: 437 return "s8"; 438 case 16: 439 return "s16"; 440 case 32: 441 return "s32"; 442 case 64: 443 return "s64"; 444 } 445 } else { /* unsigned */ 446 switch (BTF_INT_BITS(intdata)) { 447 case 8: 448 return "u8"; 449 case 16: 450 return "u16"; 451 case 32: 452 return "u32"; 453 case 64: 454 return "u64"; 455 } 456 /* bitfield, size is encoded in the type */ 457 ctx->last_bitsize = BTF_INT_BITS(intdata); 458 ctx->last_bitoffs += BTF_INT_OFFSET(intdata); 459 return "u64"; 460 } 461 } 462 /* TODO: support other types */ 463 464 return NULL; 465 } 466 467 static int query_btf_context(struct traceprobe_parse_context *ctx) 468 { 469 const struct btf_param *param; 470 const struct btf_type *type; 471 struct btf *btf; 472 s32 nr; 473 474 if (ctx->btf) 475 return 0; 476 477 if (!ctx->funcname) 478 return -EINVAL; 479 480 type = btf_find_func_proto(ctx->funcname, &btf); 481 if (!type) 482 return -ENOENT; 483 484 ctx->btf = btf; 485 ctx->proto = type; 486 487 /* ctx->params is optional, since func(void) will not have params. */ 488 nr = 0; 489 param = btf_get_func_param(type, &nr); 490 if (!IS_ERR_OR_NULL(param)) { 491 /* Hide the first 'data' argument of tracepoint */ 492 if (ctx->flags & TPARG_FL_TPOINT) { 493 nr--; 494 param++; 495 } 496 } 497 498 if (nr > 0) { 499 ctx->nr_params = nr; 500 ctx->params = param; 501 } else { 502 ctx->nr_params = 0; 503 ctx->params = NULL; 504 } 505 506 return 0; 507 } 508 509 static void clear_btf_context(struct traceprobe_parse_context *ctx) 510 { 511 if (ctx->btf) { 512 btf_put(ctx->btf); 513 ctx->btf = NULL; 514 ctx->proto = NULL; 515 ctx->params = NULL; 516 ctx->nr_params = 0; 517 } 518 } 519 520 /* Return 1 if the field separator is arrow operator ('->') */ 521 static int split_next_field(char *varname, char **next_field, 522 struct traceprobe_parse_context *ctx) 523 { 524 char *field; 525 int ret = 0; 526 527 field = strpbrk(varname, ".-"); 528 if (field) { 529 if (field[0] == '-' && field[1] == '>') { 530 field[0] = '\0'; 531 field += 2; 532 ret = 1; 533 } else if (field[0] == '.') { 534 field[0] = '\0'; 535 field += 1; 536 } else { 537 trace_probe_log_err(ctx->offset + field - varname, BAD_HYPHEN); 538 return -EINVAL; 539 } 540 *next_field = field; 541 } 542 543 return ret; 544 } 545 546 /* 547 * Parse the field of data structure. The @type must be a pointer type 548 * pointing the target data structure type. 549 */ 550 static int parse_btf_field(char *fieldname, const struct btf_type *type, 551 struct fetch_insn **pcode, struct fetch_insn *end, 552 struct traceprobe_parse_context *ctx) 553 { 554 struct fetch_insn *code = *pcode; 555 const struct btf_member *field; 556 u32 bitoffs, anon_offs; 557 char *next; 558 int is_ptr; 559 s32 tid; 560 561 do { 562 /* Outer loop for solving arrow operator ('->') */ 563 if (BTF_INFO_KIND(type->info) != BTF_KIND_PTR) { 564 trace_probe_log_err(ctx->offset, NO_PTR_STRCT); 565 return -EINVAL; 566 } 567 /* Convert a struct pointer type to a struct type */ 568 type = btf_type_skip_modifiers(ctx->btf, type->type, &tid); 569 if (!type) { 570 trace_probe_log_err(ctx->offset, BAD_BTF_TID); 571 return -EINVAL; 572 } 573 574 bitoffs = 0; 575 do { 576 /* Inner loop for solving dot operator ('.') */ 577 next = NULL; 578 is_ptr = split_next_field(fieldname, &next, ctx); 579 if (is_ptr < 0) 580 return is_ptr; 581 582 anon_offs = 0; 583 field = btf_find_struct_member(ctx->btf, type, fieldname, 584 &anon_offs); 585 if (IS_ERR(field)) { 586 trace_probe_log_err(ctx->offset, BAD_BTF_TID); 587 return PTR_ERR(field); 588 } 589 if (!field) { 590 trace_probe_log_err(ctx->offset, NO_BTF_FIELD); 591 return -ENOENT; 592 } 593 /* Add anonymous structure/union offset */ 594 bitoffs += anon_offs; 595 596 /* Accumulate the bit-offsets of the dot-connected fields */ 597 if (btf_type_kflag(type)) { 598 bitoffs += BTF_MEMBER_BIT_OFFSET(field->offset); 599 ctx->last_bitsize = BTF_MEMBER_BITFIELD_SIZE(field->offset); 600 } else { 601 bitoffs += field->offset; 602 ctx->last_bitsize = 0; 603 } 604 605 type = btf_type_skip_modifiers(ctx->btf, field->type, &tid); 606 if (!type) { 607 trace_probe_log_err(ctx->offset, BAD_BTF_TID); 608 return -EINVAL; 609 } 610 611 ctx->offset += next - fieldname; 612 fieldname = next; 613 } while (!is_ptr && fieldname); 614 615 if (++code == end) { 616 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 617 return -EINVAL; 618 } 619 code->op = FETCH_OP_DEREF; /* TODO: user deref support */ 620 code->offset = bitoffs / 8; 621 *pcode = code; 622 623 ctx->last_bitoffs = bitoffs % 8; 624 ctx->last_type = type; 625 } while (fieldname); 626 627 return 0; 628 } 629 630 static int __store_entry_arg(struct trace_probe *tp, int argnum); 631 632 static int parse_btf_arg(char *varname, 633 struct fetch_insn **pcode, struct fetch_insn *end, 634 struct traceprobe_parse_context *ctx) 635 { 636 struct fetch_insn *code = *pcode; 637 const struct btf_param *params; 638 const struct btf_type *type; 639 char *field = NULL; 640 int i, is_ptr, ret; 641 u32 tid; 642 643 if (WARN_ON_ONCE(!ctx->funcname)) 644 return -EINVAL; 645 646 is_ptr = split_next_field(varname, &field, ctx); 647 if (is_ptr < 0) 648 return is_ptr; 649 if (!is_ptr && field) { 650 /* dot-connected field on an argument is not supported. */ 651 trace_probe_log_err(ctx->offset + field - varname, 652 NOSUP_DAT_ARG); 653 return -EOPNOTSUPP; 654 } 655 656 if (ctx->flags & TPARG_FL_RETURN && !strcmp(varname, "$retval")) { 657 code->op = FETCH_OP_RETVAL; 658 /* Check whether the function return type is not void */ 659 if (query_btf_context(ctx) == 0) { 660 if (ctx->proto->type == 0) { 661 trace_probe_log_err(ctx->offset, NO_RETVAL); 662 return -ENOENT; 663 } 664 tid = ctx->proto->type; 665 goto found; 666 } 667 if (field) { 668 trace_probe_log_err(ctx->offset + field - varname, 669 NO_BTF_ENTRY); 670 return -ENOENT; 671 } 672 return 0; 673 } 674 675 if (!ctx->btf) { 676 ret = query_btf_context(ctx); 677 if (ret < 0 || ctx->nr_params == 0) { 678 trace_probe_log_err(ctx->offset, NO_BTF_ENTRY); 679 return -ENOENT; 680 } 681 } 682 params = ctx->params; 683 684 for (i = 0; i < ctx->nr_params; i++) { 685 const char *name = btf_name_by_offset(ctx->btf, params[i].name_off); 686 687 if (name && !strcmp(name, varname)) { 688 if (tparg_is_function_entry(ctx->flags)) { 689 code->op = FETCH_OP_ARG; 690 if (ctx->flags & TPARG_FL_TPOINT) 691 code->param = i + 1; 692 else 693 code->param = i; 694 } else if (tparg_is_function_return(ctx->flags)) { 695 code->op = FETCH_OP_EDATA; 696 ret = __store_entry_arg(ctx->tp, i); 697 if (ret < 0) { 698 /* internal error */ 699 return ret; 700 } 701 code->offset = ret; 702 } 703 tid = params[i].type; 704 goto found; 705 } 706 } 707 trace_probe_log_err(ctx->offset, NO_BTFARG); 708 return -ENOENT; 709 710 found: 711 type = btf_type_skip_modifiers(ctx->btf, tid, &tid); 712 if (!type) { 713 trace_probe_log_err(ctx->offset, BAD_BTF_TID); 714 return -EINVAL; 715 } 716 /* Initialize the last type information */ 717 ctx->last_type = type; 718 ctx->last_bitoffs = 0; 719 ctx->last_bitsize = 0; 720 if (field) { 721 ctx->offset += field - varname; 722 return parse_btf_field(field, type, pcode, end, ctx); 723 } 724 return 0; 725 } 726 727 static const struct fetch_type *find_fetch_type_from_btf_type( 728 struct traceprobe_parse_context *ctx) 729 { 730 struct btf *btf = ctx->btf; 731 const char *typestr = NULL; 732 733 if (btf && ctx->last_type) 734 typestr = fetch_type_from_btf_type(btf, ctx->last_type, ctx); 735 736 return find_fetch_type(typestr, ctx->flags); 737 } 738 739 static int parse_btf_bitfield(struct fetch_insn **pcode, 740 struct traceprobe_parse_context *ctx) 741 { 742 struct fetch_insn *code = *pcode; 743 744 if ((ctx->last_bitsize % 8 == 0) && ctx->last_bitoffs == 0) 745 return 0; 746 747 code++; 748 if (code->op != FETCH_OP_NOP) { 749 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 750 return -EINVAL; 751 } 752 *pcode = code; 753 754 code->op = FETCH_OP_MOD_BF; 755 code->lshift = 64 - (ctx->last_bitsize + ctx->last_bitoffs); 756 code->rshift = 64 - ctx->last_bitsize; 757 code->basesize = 64 / 8; 758 return 0; 759 } 760 761 #else 762 static void clear_btf_context(struct traceprobe_parse_context *ctx) 763 { 764 ctx->btf = NULL; 765 } 766 767 static int query_btf_context(struct traceprobe_parse_context *ctx) 768 { 769 return -EOPNOTSUPP; 770 } 771 772 static int parse_btf_arg(char *varname, 773 struct fetch_insn **pcode, struct fetch_insn *end, 774 struct traceprobe_parse_context *ctx) 775 { 776 trace_probe_log_err(ctx->offset, NOSUP_BTFARG); 777 return -EOPNOTSUPP; 778 } 779 780 static int parse_btf_bitfield(struct fetch_insn **pcode, 781 struct traceprobe_parse_context *ctx) 782 { 783 trace_probe_log_err(ctx->offset, NOSUP_BTFARG); 784 return -EOPNOTSUPP; 785 } 786 787 #define find_fetch_type_from_btf_type(ctx) \ 788 find_fetch_type(NULL, ctx->flags) 789 790 static int check_prepare_btf_string_fetch(char *typename, 791 struct fetch_insn **pcode, 792 struct traceprobe_parse_context *ctx) 793 { 794 return 0; 795 } 796 797 #endif 798 799 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API 800 801 static void store_entry_arg_at(struct fetch_insn *code, int argnum, int offset) 802 { 803 code[0].op = FETCH_OP_ARG; 804 code[0].param = argnum; 805 code[1].op = FETCH_OP_ST_EDATA; 806 code[1].offset = offset; 807 } 808 809 static int get_entry_arg_max_offset(struct probe_entry_arg *earg) 810 { 811 int i, max_offset = 0; 812 813 /* 814 * earg->code[] array has an operation sequence which is run in 815 * the entry handler. 816 * The sequence stopped by FETCH_OP_END and each data stored in 817 * the entry data buffer by FETCH_OP_ST_EDATA. The FETCH_OP_ST_EDATA 818 * stores the data at the data buffer + its offset, and all data are 819 * "unsigned long" size. The offset must be increased when a data is 820 * stored. Thus we need to find the last FETCH_OP_ST_EDATA in the 821 * code array. 822 */ 823 for (i = 0; i < earg->size - 1 && earg->code[i].op != FETCH_OP_END; i++) { 824 if (earg->code[i].op == FETCH_OP_ST_EDATA) 825 if (earg->code[i].offset > max_offset) 826 max_offset = earg->code[i].offset; 827 } 828 return max_offset; 829 } 830 831 /* 832 * Add the entry code to store the 'argnum'th parameter and return the offset 833 * in the entry data buffer where the data will be stored. 834 */ 835 static int __store_entry_arg(struct trace_probe *tp, int argnum) 836 { 837 struct probe_entry_arg *earg = tp->entry_arg; 838 int i, offset, last_offset = 0; 839 840 if (!earg) { 841 earg = kzalloc_obj(*tp->entry_arg); 842 if (!earg) 843 return -ENOMEM; 844 earg->size = 2 * tp->nr_args + 1; 845 earg->code = kzalloc_objs(struct fetch_insn, earg->size); 846 if (!earg->code) { 847 kfree(earg); 848 return -ENOMEM; 849 } 850 /* Fill the code buffer with 'end' to simplify it */ 851 for (i = 0; i < earg->size; i++) 852 earg->code[i].op = FETCH_OP_END; 853 tp->entry_arg = earg; 854 store_entry_arg_at(earg->code, argnum, 0); 855 return 0; 856 } 857 858 /* 859 * NOTE: if anyone change the following rule, please rewrite this. 860 * The entry code array is filled with the pair of 861 * 862 * [FETCH_OP_ARG(argnum)] 863 * [FETCH_OP_ST_EDATA(offset of entry data buffer)] 864 * 865 * and the rest of entries are filled with [FETCH_OP_END]. 866 * The offset should be incremented, thus the last pair should 867 * have the largest offset. 868 */ 869 870 /* Search the offset for the sprcified argnum. */ 871 for (i = 0; i < earg->size - 1 && earg->code[i].op != FETCH_OP_END; i += 2) { 872 if (WARN_ON_ONCE(earg->code[i].op != FETCH_OP_ARG)) 873 return -EINVAL; 874 875 if (earg->code[i].param != argnum) 876 continue; 877 878 if (WARN_ON_ONCE(earg->code[i + 1].op != FETCH_OP_ST_EDATA)) 879 return -EINVAL; 880 881 return earg->code[i + 1].offset; 882 } 883 /* Not found, append new entry if possible. */ 884 if (i >= earg->size - 1) 885 return -ENOSPC; 886 887 /* The last entry must have the largest offset. */ 888 if (i != 0) { 889 if (WARN_ON_ONCE(earg->code[i - 1].op != FETCH_OP_ST_EDATA)) 890 return -EINVAL; 891 last_offset = earg->code[i - 1].offset; 892 } 893 894 offset = last_offset + sizeof(unsigned long); 895 store_entry_arg_at(&earg->code[i], argnum, offset); 896 return offset; 897 } 898 899 int traceprobe_get_entry_data_size(struct trace_probe *tp) 900 { 901 struct probe_entry_arg *earg = tp->entry_arg; 902 903 if (!earg) 904 return 0; 905 906 return get_entry_arg_max_offset(earg) + sizeof(unsigned long); 907 } 908 909 void store_trace_entry_data(void *edata, struct trace_probe *tp, struct pt_regs *regs) 910 { 911 struct probe_entry_arg *earg = tp->entry_arg; 912 unsigned long val = 0; 913 int i; 914 915 if (!earg) 916 return; 917 918 for (i = 0; i < earg->size; i++) { 919 struct fetch_insn *code = &earg->code[i]; 920 921 switch (code->op) { 922 case FETCH_OP_ARG: 923 val = regs_get_kernel_argument(regs, code->param); 924 break; 925 case FETCH_OP_ST_EDATA: 926 *(unsigned long *)((unsigned long)edata + code->offset) = val; 927 break; 928 case FETCH_OP_END: 929 goto end; 930 default: 931 break; 932 } 933 } 934 end: 935 return; 936 } 937 NOKPROBE_SYMBOL(store_trace_entry_data) 938 #endif 939 940 #define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) 941 942 /* Parse $vars. @orig_arg points '$', which syncs to @ctx->offset */ 943 static int parse_probe_vars(char *orig_arg, const struct fetch_type *t, 944 struct fetch_insn **pcode, 945 struct fetch_insn *end, 946 struct traceprobe_parse_context *ctx) 947 { 948 struct fetch_insn *code = *pcode; 949 int err = TP_ERR_BAD_VAR; 950 char *arg = orig_arg + 1; 951 unsigned long param; 952 int ret = 0; 953 int len; 954 955 if (ctx->flags & TPARG_FL_TEVENT) { 956 if (code->data) 957 return -EFAULT; 958 ret = parse_trace_event_arg(arg, code, ctx); 959 if (!ret) 960 return 0; 961 if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) { 962 code->op = FETCH_OP_COMM; 963 return 0; 964 } 965 goto inval; 966 } 967 968 if (str_has_prefix(arg, "retval")) { 969 if (!(ctx->flags & TPARG_FL_RETURN)) { 970 err = TP_ERR_RETVAL_ON_PROBE; 971 goto inval; 972 } 973 if (!(ctx->flags & TPARG_FL_KERNEL) || 974 !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) { 975 code->op = FETCH_OP_RETVAL; 976 return 0; 977 } 978 return parse_btf_arg(orig_arg, pcode, end, ctx); 979 } 980 981 len = str_has_prefix(arg, "stack"); 982 if (len) { 983 984 if (arg[len] == '\0') { 985 code->op = FETCH_OP_STACKP; 986 return 0; 987 } 988 989 if (isdigit(arg[len])) { 990 ret = kstrtoul(arg + len, 10, ¶m); 991 if (ret) 992 goto inval; 993 994 if ((ctx->flags & TPARG_FL_KERNEL) && 995 param > PARAM_MAX_STACK) { 996 err = TP_ERR_BAD_STACK_NUM; 997 goto inval; 998 } 999 code->op = FETCH_OP_STACK; 1000 code->param = (unsigned int)param; 1001 return 0; 1002 } 1003 goto inval; 1004 } 1005 1006 if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) { 1007 code->op = FETCH_OP_COMM; 1008 return 0; 1009 } 1010 1011 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API 1012 len = str_has_prefix(arg, "arg"); 1013 if (len) { 1014 ret = kstrtoul(arg + len, 10, ¶m); 1015 if (ret) 1016 goto inval; 1017 1018 if (!param || param > PARAM_MAX_STACK) { 1019 err = TP_ERR_BAD_ARG_NUM; 1020 goto inval; 1021 } 1022 param--; /* argN starts from 1, but internal arg[N] starts from 0 */ 1023 1024 if (tparg_is_function_entry(ctx->flags)) { 1025 code->op = FETCH_OP_ARG; 1026 code->param = (unsigned int)param; 1027 /* 1028 * The tracepoint probe will probe a stub function, and the 1029 * first parameter of the stub is a dummy and should be ignored. 1030 */ 1031 if (ctx->flags & TPARG_FL_TPOINT) 1032 code->param++; 1033 } else if (tparg_is_function_return(ctx->flags)) { 1034 /* function entry argument access from return probe */ 1035 ret = __store_entry_arg(ctx->tp, param); 1036 if (ret < 0) /* This error should be an internal error */ 1037 return ret; 1038 1039 code->op = FETCH_OP_EDATA; 1040 code->offset = ret; 1041 } else { 1042 err = TP_ERR_NOFENTRY_ARGS; 1043 goto inval; 1044 } 1045 return 0; 1046 } 1047 #endif 1048 1049 inval: 1050 __trace_probe_log_err(ctx->offset, err); 1051 return -EINVAL; 1052 } 1053 1054 static int str_to_immediate(char *str, unsigned long *imm) 1055 { 1056 if (isdigit(str[0])) 1057 return kstrtoul(str, 0, imm); 1058 else if (str[0] == '-') 1059 return kstrtol(str, 0, (long *)imm); 1060 else if (str[0] == '+') 1061 return kstrtol(str + 1, 0, (long *)imm); 1062 return -EINVAL; 1063 } 1064 1065 static int __parse_imm_string(char *str, char **pbuf, int offs) 1066 { 1067 size_t len = strlen(str); 1068 1069 if (!len || str[len - 1] != '"') { 1070 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE); 1071 return -EINVAL; 1072 } 1073 *pbuf = kstrndup(str, len - 1, GFP_KERNEL); 1074 if (!*pbuf) 1075 return -ENOMEM; 1076 return 0; 1077 } 1078 1079 /* Recursive argument parser */ 1080 static int 1081 parse_probe_arg(char *arg, const struct fetch_type *type, 1082 struct fetch_insn **pcode, struct fetch_insn *end, 1083 struct traceprobe_parse_context *ctx) 1084 { 1085 struct fetch_insn *code = *pcode; 1086 unsigned long param; 1087 int deref = FETCH_OP_DEREF; 1088 long offset = 0; 1089 char *tmp; 1090 int ret = 0; 1091 1092 switch (arg[0]) { 1093 case '$': 1094 ret = parse_probe_vars(arg, type, pcode, end, ctx); 1095 break; 1096 1097 case '%': /* named register */ 1098 if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) { 1099 /* eprobe and fprobe do not handle registers */ 1100 trace_probe_log_err(ctx->offset, BAD_VAR); 1101 break; 1102 } 1103 ret = regs_query_register_offset(arg + 1); 1104 if (ret >= 0) { 1105 code->op = FETCH_OP_REG; 1106 code->param = (unsigned int)ret; 1107 ret = 0; 1108 } else 1109 trace_probe_log_err(ctx->offset, BAD_REG_NAME); 1110 break; 1111 1112 case '@': /* memory, file-offset or symbol */ 1113 if (isdigit(arg[1])) { 1114 ret = kstrtoul(arg + 1, 0, ¶m); 1115 if (ret) { 1116 trace_probe_log_err(ctx->offset, BAD_MEM_ADDR); 1117 break; 1118 } 1119 /* load address */ 1120 code->op = FETCH_OP_IMM; 1121 code->immediate = param; 1122 } else if (arg[1] == '+') { 1123 /* kprobes don't support file offsets */ 1124 if (ctx->flags & TPARG_FL_KERNEL) { 1125 trace_probe_log_err(ctx->offset, FILE_ON_KPROBE); 1126 return -EINVAL; 1127 } 1128 ret = kstrtol(arg + 2, 0, &offset); 1129 if (ret) { 1130 trace_probe_log_err(ctx->offset, BAD_FILE_OFFS); 1131 break; 1132 } 1133 1134 code->op = FETCH_OP_FOFFS; 1135 code->immediate = (unsigned long)offset; // imm64? 1136 } else { 1137 /* uprobes don't support symbols */ 1138 if (!(ctx->flags & TPARG_FL_KERNEL)) { 1139 trace_probe_log_err(ctx->offset, SYM_ON_UPROBE); 1140 return -EINVAL; 1141 } 1142 /* Preserve symbol for updating */ 1143 code->op = FETCH_NOP_SYMBOL; 1144 code->data = kstrdup(arg + 1, GFP_KERNEL); 1145 if (!code->data) 1146 return -ENOMEM; 1147 if (++code == end) { 1148 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1149 return -EINVAL; 1150 } 1151 code->op = FETCH_OP_IMM; 1152 code->immediate = 0; 1153 } 1154 /* These are fetching from memory */ 1155 if (++code == end) { 1156 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1157 return -EINVAL; 1158 } 1159 *pcode = code; 1160 code->op = FETCH_OP_DEREF; 1161 code->offset = offset; 1162 break; 1163 1164 case '+': /* deref memory */ 1165 case '-': 1166 if (arg[1] == 'u') { 1167 deref = FETCH_OP_UDEREF; 1168 arg[1] = arg[0]; 1169 arg++; 1170 } 1171 if (arg[0] == '+') 1172 arg++; /* Skip '+', because kstrtol() rejects it. */ 1173 tmp = strchr(arg, '('); 1174 if (!tmp) { 1175 trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE); 1176 return -EINVAL; 1177 } 1178 *tmp = '\0'; 1179 ret = kstrtol(arg, 0, &offset); 1180 if (ret) { 1181 trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS); 1182 break; 1183 } 1184 ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); 1185 arg = tmp + 1; 1186 tmp = strrchr(arg, ')'); 1187 if (!tmp) { 1188 trace_probe_log_err(ctx->offset + strlen(arg), 1189 DEREF_OPEN_BRACE); 1190 return -EINVAL; 1191 } else { 1192 const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags); 1193 int cur_offs = ctx->offset; 1194 1195 *tmp = '\0'; 1196 ret = parse_probe_arg(arg, t2, &code, end, ctx); 1197 if (ret) 1198 break; 1199 ctx->offset = cur_offs; 1200 if (code->op == FETCH_OP_COMM || 1201 code->op == FETCH_OP_DATA) { 1202 trace_probe_log_err(ctx->offset, COMM_CANT_DEREF); 1203 return -EINVAL; 1204 } 1205 if (++code == end) { 1206 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1207 return -EINVAL; 1208 } 1209 *pcode = code; 1210 1211 code->op = deref; 1212 code->offset = offset; 1213 /* Reset the last type if used */ 1214 ctx->last_type = NULL; 1215 } 1216 break; 1217 case '\\': /* Immediate value */ 1218 if (arg[1] == '"') { /* Immediate string */ 1219 ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2); 1220 if (ret) 1221 break; 1222 code->op = FETCH_OP_DATA; 1223 code->data = tmp; 1224 } else { 1225 ret = str_to_immediate(arg + 1, &code->immediate); 1226 if (ret) 1227 trace_probe_log_err(ctx->offset + 1, BAD_IMM); 1228 else 1229 code->op = FETCH_OP_IMM; 1230 } 1231 break; 1232 default: 1233 if (isalpha(arg[0]) || arg[0] == '_') { /* BTF variable */ 1234 if (!tparg_is_function_entry(ctx->flags) && 1235 !tparg_is_function_return(ctx->flags)) { 1236 trace_probe_log_err(ctx->offset, NOSUP_BTFARG); 1237 return -EINVAL; 1238 } 1239 ret = parse_btf_arg(arg, pcode, end, ctx); 1240 break; 1241 } 1242 } 1243 if (!ret && code->op == FETCH_OP_NOP) { 1244 /* Parsed, but do not find fetch method */ 1245 trace_probe_log_err(ctx->offset, BAD_FETCH_ARG); 1246 ret = -EINVAL; 1247 } 1248 return ret; 1249 } 1250 1251 /* Bitfield type needs to be parsed into a fetch function */ 1252 static int __parse_bitfield_probe_arg(const char *bf, 1253 const struct fetch_type *t, 1254 struct fetch_insn **pcode) 1255 { 1256 struct fetch_insn *code = *pcode; 1257 unsigned long bw, bo; 1258 char *tail; 1259 1260 if (*bf != 'b') 1261 return 0; 1262 1263 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ 1264 1265 if (bw == 0 || *tail != '@') 1266 return -EINVAL; 1267 1268 bf = tail + 1; 1269 bo = simple_strtoul(bf, &tail, 0); 1270 1271 if (tail == bf || *tail != '/') 1272 return -EINVAL; 1273 code++; 1274 if (code->op != FETCH_OP_NOP) 1275 return -EINVAL; 1276 *pcode = code; 1277 1278 code->op = FETCH_OP_MOD_BF; 1279 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); 1280 code->rshift = BYTES_TO_BITS(t->size) - bw; 1281 code->basesize = t->size; 1282 1283 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; 1284 } 1285 1286 /* Split type part from @arg and return it. */ 1287 static char *parse_probe_arg_type(char *arg, struct probe_arg *parg, 1288 struct traceprobe_parse_context *ctx) 1289 { 1290 char *t = NULL, *t2, *t3; 1291 int offs; 1292 1293 t = strchr(arg, ':'); 1294 if (t) { 1295 *t++ = '\0'; 1296 t2 = strchr(t, '['); 1297 if (t2) { 1298 *t2++ = '\0'; 1299 t3 = strchr(t2, ']'); 1300 if (!t3) { 1301 offs = t2 + strlen(t2) - arg; 1302 1303 trace_probe_log_err(ctx->offset + offs, 1304 ARRAY_NO_CLOSE); 1305 return ERR_PTR(-EINVAL); 1306 } else if (t3[1] != '\0') { 1307 trace_probe_log_err(ctx->offset + t3 + 1 - arg, 1308 BAD_ARRAY_SUFFIX); 1309 return ERR_PTR(-EINVAL); 1310 } 1311 *t3 = '\0'; 1312 if (kstrtouint(t2, 0, &parg->count) || !parg->count) { 1313 trace_probe_log_err(ctx->offset + t2 - arg, 1314 BAD_ARRAY_NUM); 1315 return ERR_PTR(-EINVAL); 1316 } 1317 if (parg->count > MAX_ARRAY_LEN) { 1318 trace_probe_log_err(ctx->offset + t2 - arg, 1319 ARRAY_TOO_BIG); 1320 return ERR_PTR(-EINVAL); 1321 } 1322 } 1323 } 1324 offs = t ? t - arg : 0; 1325 1326 /* 1327 * Since $comm and immediate string can not be dereferenced, 1328 * we can find those by strcmp. But ignore for eprobes. 1329 */ 1330 if (!(ctx->flags & TPARG_FL_TEVENT) && 1331 (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 || 1332 strncmp(arg, "\\\"", 2) == 0)) { 1333 /* The type of $comm must be "string", and not an array type. */ 1334 if (parg->count || (t && strcmp(t, "string"))) { 1335 trace_probe_log_err(ctx->offset + offs, NEED_STRING_TYPE); 1336 return ERR_PTR(-EINVAL); 1337 } 1338 parg->type = find_fetch_type("string", ctx->flags); 1339 } else 1340 parg->type = find_fetch_type(t, ctx->flags); 1341 1342 if (!parg->type) { 1343 trace_probe_log_err(ctx->offset + offs, BAD_TYPE); 1344 return ERR_PTR(-EINVAL); 1345 } 1346 1347 return t; 1348 } 1349 1350 /* After parsing, adjust the fetch_insn according to the probe_arg */ 1351 static int finalize_fetch_insn(struct fetch_insn *code, 1352 struct probe_arg *parg, 1353 char *type, 1354 int type_offset, 1355 struct traceprobe_parse_context *ctx) 1356 { 1357 struct fetch_insn *scode; 1358 int ret; 1359 1360 /* Store operation */ 1361 if (parg->type->is_string) { 1362 /* Check bad combination of the type and the last fetch_insn. */ 1363 if (!strcmp(parg->type->name, "symstr")) { 1364 if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK && 1365 code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG && 1366 code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) { 1367 trace_probe_log_err(ctx->offset + type_offset, 1368 BAD_SYMSTRING); 1369 return -EINVAL; 1370 } 1371 } else { 1372 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF && 1373 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM && 1374 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) { 1375 trace_probe_log_err(ctx->offset + type_offset, 1376 BAD_STRING); 1377 return -EINVAL; 1378 } 1379 } 1380 1381 if (!strcmp(parg->type->name, "symstr") || 1382 (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM || 1383 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG || 1384 parg->count) { 1385 /* 1386 * IMM, DATA and COMM is pointing actual address, those 1387 * must be kept, and if parg->count != 0, this is an 1388 * array of string pointers instead of string address 1389 * itself. 1390 * For the symstr, it doesn't need to dereference, thus 1391 * it just get the value. 1392 */ 1393 code++; 1394 if (code->op != FETCH_OP_NOP) { 1395 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1396 return -EINVAL; 1397 } 1398 } 1399 1400 /* If op == DEREF, replace it with STRING */ 1401 if (!strcmp(parg->type->name, "ustring") || 1402 code->op == FETCH_OP_UDEREF) 1403 code->op = FETCH_OP_ST_USTRING; 1404 else if (!strcmp(parg->type->name, "symstr")) 1405 code->op = FETCH_OP_ST_SYMSTR; 1406 else 1407 code->op = FETCH_OP_ST_STRING; 1408 code->size = parg->type->size; 1409 parg->dynamic = true; 1410 } else if (code->op == FETCH_OP_DEREF) { 1411 code->op = FETCH_OP_ST_MEM; 1412 code->size = parg->type->size; 1413 } else if (code->op == FETCH_OP_UDEREF) { 1414 code->op = FETCH_OP_ST_UMEM; 1415 code->size = parg->type->size; 1416 } else { 1417 code++; 1418 if (code->op != FETCH_OP_NOP) { 1419 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1420 return -E2BIG; 1421 } 1422 code->op = FETCH_OP_ST_RAW; 1423 code->size = parg->type->size; 1424 } 1425 1426 /* Save storing fetch_insn. */ 1427 scode = code; 1428 1429 /* Modify operation */ 1430 if (type != NULL) { 1431 /* Bitfield needs a special fetch_insn. */ 1432 ret = __parse_bitfield_probe_arg(type, parg->type, &code); 1433 if (ret) { 1434 trace_probe_log_err(ctx->offset + type_offset, BAD_BITFIELD); 1435 return ret; 1436 } 1437 } else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && 1438 ctx->last_type) { 1439 /* If user not specified the type, try parsing BTF bitfield. */ 1440 ret = parse_btf_bitfield(&code, ctx); 1441 if (ret) 1442 return ret; 1443 } 1444 1445 /* Loop(Array) operation */ 1446 if (parg->count) { 1447 if (scode->op != FETCH_OP_ST_MEM && 1448 scode->op != FETCH_OP_ST_STRING && 1449 scode->op != FETCH_OP_ST_USTRING) { 1450 trace_probe_log_err(ctx->offset + type_offset, BAD_STRING); 1451 return -EINVAL; 1452 } 1453 code++; 1454 if (code->op != FETCH_OP_NOP) { 1455 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1456 return -E2BIG; 1457 } 1458 code->op = FETCH_OP_LP_ARRAY; 1459 code->param = parg->count; 1460 } 1461 1462 /* Finalize the fetch_insn array. */ 1463 code++; 1464 code->op = FETCH_OP_END; 1465 1466 return 0; 1467 } 1468 1469 /* String length checking wrapper */ 1470 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, 1471 struct probe_arg *parg, 1472 struct traceprobe_parse_context *ctx) 1473 { 1474 struct fetch_insn *code, *tmp = NULL; 1475 char *type, *arg __free(kfree) = NULL; 1476 int ret, len; 1477 1478 len = strlen(argv); 1479 if (len > MAX_ARGSTR_LEN) { 1480 trace_probe_log_err(ctx->offset, ARG_TOO_LONG); 1481 return -E2BIG; 1482 } else if (len == 0) { 1483 trace_probe_log_err(ctx->offset, NO_ARG_BODY); 1484 return -EINVAL; 1485 } 1486 1487 arg = kstrdup(argv, GFP_KERNEL); 1488 if (!arg) 1489 return -ENOMEM; 1490 1491 parg->comm = kstrdup(arg, GFP_KERNEL); 1492 if (!parg->comm) 1493 return -ENOMEM; 1494 1495 type = parse_probe_arg_type(arg, parg, ctx); 1496 if (IS_ERR(type)) 1497 return PTR_ERR(type); 1498 1499 code = tmp = kzalloc_objs(*code, FETCH_INSN_MAX); 1500 if (!code) 1501 return -ENOMEM; 1502 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; 1503 1504 ctx->last_type = NULL; 1505 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], 1506 ctx); 1507 if (ret < 0) 1508 goto fail; 1509 1510 /* Update storing type if BTF is available */ 1511 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && 1512 ctx->last_type) { 1513 if (!type) { 1514 parg->type = find_fetch_type_from_btf_type(ctx); 1515 } else if (strstr(type, "string")) { 1516 ret = check_prepare_btf_string_fetch(type, &code, ctx); 1517 if (ret) 1518 goto fail; 1519 } 1520 } 1521 parg->offset = *size; 1522 *size += parg->type->size * (parg->count ?: 1); 1523 1524 if (*size > MAX_PROBE_EVENT_SIZE) { 1525 ret = -E2BIG; 1526 trace_probe_log_err(ctx->offset, EVENT_TOO_BIG); 1527 goto fail; 1528 } 1529 1530 if (parg->count) { 1531 len = strlen(parg->type->fmttype) + 6; 1532 parg->fmt = kmalloc(len, GFP_KERNEL); 1533 if (!parg->fmt) { 1534 ret = -ENOMEM; 1535 goto fail; 1536 } 1537 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, 1538 parg->count); 1539 } 1540 1541 ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx); 1542 if (ret < 0) 1543 goto fail; 1544 1545 for (; code < tmp + FETCH_INSN_MAX; code++) 1546 if (code->op == FETCH_OP_END) 1547 break; 1548 /* Shrink down the code buffer */ 1549 parg->code = kzalloc_objs(*code, code - tmp + 1); 1550 if (!parg->code) 1551 ret = -ENOMEM; 1552 else 1553 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); 1554 1555 fail: 1556 if (ret < 0) { 1557 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) 1558 if (code->op == FETCH_NOP_SYMBOL || 1559 code->op == FETCH_OP_DATA) 1560 kfree(code->data); 1561 } 1562 kfree(tmp); 1563 1564 return ret; 1565 } 1566 1567 /* Return 1 if name is reserved or already used by another argument */ 1568 static int traceprobe_conflict_field_name(const char *name, 1569 struct probe_arg *args, int narg) 1570 { 1571 int i; 1572 1573 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) 1574 if (strcmp(reserved_field_names[i], name) == 0) 1575 return 1; 1576 1577 for (i = 0; i < narg; i++) 1578 if (strcmp(args[i].name, name) == 0) 1579 return 1; 1580 1581 return 0; 1582 } 1583 1584 static char *generate_probe_arg_name(const char *arg, int idx) 1585 { 1586 char *name = NULL; 1587 const char *end; 1588 1589 /* 1590 * If argument name is omitted, try arg as a name (BTF variable) 1591 * or "argN". 1592 */ 1593 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) { 1594 end = strchr(arg, ':'); 1595 if (!end) 1596 end = arg + strlen(arg); 1597 1598 name = kmemdup_nul(arg, end - arg, GFP_KERNEL); 1599 if (!name || !is_good_name(name)) { 1600 kfree(name); 1601 name = NULL; 1602 } 1603 } 1604 1605 if (!name) 1606 name = kasprintf(GFP_KERNEL, "arg%d", idx + 1); 1607 1608 return name; 1609 } 1610 1611 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg, 1612 struct traceprobe_parse_context *ctx) 1613 { 1614 struct probe_arg *parg = &tp->args[i]; 1615 const char *body; 1616 1617 ctx->tp = tp; 1618 body = strchr(arg, '='); 1619 if (body) { 1620 if (body - arg > MAX_ARG_NAME_LEN) { 1621 trace_probe_log_err(0, ARG_NAME_TOO_LONG); 1622 return -EINVAL; 1623 } else if (body == arg) { 1624 trace_probe_log_err(0, NO_ARG_NAME); 1625 return -EINVAL; 1626 } 1627 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); 1628 body++; 1629 } else { 1630 parg->name = generate_probe_arg_name(arg, i); 1631 body = arg; 1632 } 1633 if (!parg->name) 1634 return -ENOMEM; 1635 1636 if (!is_good_name(parg->name)) { 1637 trace_probe_log_err(0, BAD_ARG_NAME); 1638 return -EINVAL; 1639 } 1640 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { 1641 trace_probe_log_err(0, USED_ARG_NAME); 1642 return -EINVAL; 1643 } 1644 ctx->offset = body - arg; 1645 /* Parse fetch argument */ 1646 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx); 1647 } 1648 1649 void traceprobe_free_probe_arg(struct probe_arg *arg) 1650 { 1651 struct fetch_insn *code = arg->code; 1652 1653 while (code && code->op != FETCH_OP_END) { 1654 if (code->op == FETCH_NOP_SYMBOL || 1655 code->op == FETCH_OP_DATA) 1656 kfree(code->data); 1657 code++; 1658 } 1659 kfree(arg->code); 1660 kfree(arg->name); 1661 kfree(arg->comm); 1662 kfree(arg->fmt); 1663 } 1664 1665 static int argv_has_var_arg(int argc, const char *argv[], int *args_idx, 1666 struct traceprobe_parse_context *ctx) 1667 { 1668 int i, found = 0; 1669 1670 for (i = 0; i < argc; i++) 1671 if (str_has_prefix(argv[i], "$arg")) { 1672 trace_probe_log_set_index(i + 2); 1673 1674 if (!tparg_is_function_entry(ctx->flags) && 1675 !tparg_is_function_return(ctx->flags)) { 1676 trace_probe_log_err(0, NOFENTRY_ARGS); 1677 return -EINVAL; 1678 } 1679 1680 if (isdigit(argv[i][4])) { 1681 found = 1; 1682 continue; 1683 } 1684 1685 if (argv[i][4] != '*') { 1686 trace_probe_log_err(0, BAD_VAR); 1687 return -EINVAL; 1688 } 1689 1690 if (*args_idx >= 0 && *args_idx < argc) { 1691 trace_probe_log_err(0, DOUBLE_ARGS); 1692 return -EINVAL; 1693 } 1694 found = 1; 1695 *args_idx = i; 1696 } 1697 1698 return found; 1699 } 1700 1701 static int sprint_nth_btf_arg(int idx, const char *type, 1702 char *buf, int bufsize, 1703 struct traceprobe_parse_context *ctx) 1704 { 1705 const char *name; 1706 int ret; 1707 1708 if (idx >= ctx->nr_params) { 1709 trace_probe_log_err(0, NO_BTFARG); 1710 return -ENOENT; 1711 } 1712 name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off); 1713 if (!name) { 1714 trace_probe_log_err(0, NO_BTF_ENTRY); 1715 return -ENOENT; 1716 } 1717 ret = snprintf(buf, bufsize, "%s%s", name, type); 1718 if (ret >= bufsize) { 1719 trace_probe_log_err(0, ARGS_2LONG); 1720 return -E2BIG; 1721 } 1722 return ret; 1723 } 1724 1725 /* Return new_argv which must be freed after use */ 1726 const char **traceprobe_expand_meta_args(int argc, const char *argv[], 1727 int *new_argc, char *buf, int bufsize, 1728 struct traceprobe_parse_context *ctx) 1729 { 1730 const struct btf_param *params = NULL; 1731 int i, j, n, used, ret, args_idx = -1; 1732 const char **new_argv __free(kfree) = NULL; 1733 1734 ret = argv_has_var_arg(argc, argv, &args_idx, ctx); 1735 if (ret < 0) 1736 return ERR_PTR(ret); 1737 1738 if (!ret) { 1739 *new_argc = argc; 1740 return NULL; 1741 } 1742 1743 ret = query_btf_context(ctx); 1744 if (ret < 0 || ctx->nr_params == 0) { 1745 if (args_idx != -1) { 1746 /* $arg* requires BTF info */ 1747 trace_probe_log_err(0, NOSUP_BTFARG); 1748 return (const char **)params; 1749 } 1750 *new_argc = argc; 1751 return NULL; 1752 } 1753 1754 if (args_idx >= 0) 1755 *new_argc = argc + ctx->nr_params - 1; 1756 else 1757 *new_argc = argc; 1758 1759 new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL); 1760 if (!new_argv) 1761 return ERR_PTR(-ENOMEM); 1762 1763 used = 0; 1764 for (i = 0, j = 0; i < argc; i++) { 1765 trace_probe_log_set_index(i + 2); 1766 if (i == args_idx) { 1767 for (n = 0; n < ctx->nr_params; n++) { 1768 ret = sprint_nth_btf_arg(n, "", buf + used, 1769 bufsize - used, ctx); 1770 if (ret < 0) 1771 return ERR_PTR(ret); 1772 1773 new_argv[j++] = buf + used; 1774 used += ret + 1; 1775 } 1776 continue; 1777 } 1778 1779 if (str_has_prefix(argv[i], "$arg")) { 1780 char *type = NULL; 1781 1782 n = simple_strtoul(argv[i] + 4, &type, 10); 1783 if (type && !(*type == ':' || *type == '\0')) { 1784 trace_probe_log_err(0, BAD_VAR); 1785 return ERR_PTR(-ENOENT); 1786 } 1787 /* Note: $argN starts from $arg1 */ 1788 ret = sprint_nth_btf_arg(n - 1, type, buf + used, 1789 bufsize - used, ctx); 1790 if (ret < 0) 1791 return ERR_PTR(ret); 1792 new_argv[j++] = buf + used; 1793 used += ret + 1; 1794 } else 1795 new_argv[j++] = argv[i]; 1796 } 1797 1798 return_ptr(new_argv); 1799 } 1800 1801 /* @buf: *buf must be equal to NULL. Caller must to free *buf */ 1802 int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf) 1803 { 1804 int i, used, ret; 1805 const int bufsize = MAX_DENTRY_ARGS_LEN; 1806 char *tmpbuf __free(kfree) = NULL; 1807 1808 if (*buf) 1809 return -EINVAL; 1810 1811 used = 0; 1812 for (i = 0; i < argc; i++) { 1813 char *tmp __free(kfree) = NULL; 1814 char *equal; 1815 size_t arg_len; 1816 1817 if (!glob_match("*:%p[dD]", argv[i])) 1818 continue; 1819 1820 if (!tmpbuf) { 1821 tmpbuf = kmalloc(bufsize, GFP_KERNEL); 1822 if (!tmpbuf) 1823 return -ENOMEM; 1824 } 1825 1826 tmp = kstrdup(argv[i], GFP_KERNEL); 1827 if (!tmp) 1828 return -ENOMEM; 1829 1830 equal = strchr(tmp, '='); 1831 if (equal) 1832 *equal = '\0'; 1833 arg_len = strlen(argv[i]); 1834 tmp[arg_len - 4] = '\0'; 1835 if (argv[i][arg_len - 1] == 'd') 1836 ret = snprintf(tmpbuf + used, bufsize - used, 1837 "%s%s+0x0(+0x%zx(%s)):string", 1838 equal ? tmp : "", equal ? "=" : "", 1839 offsetof(struct dentry, d_name.name), 1840 equal ? equal + 1 : tmp); 1841 else 1842 ret = snprintf(tmpbuf + used, bufsize - used, 1843 "%s%s+0x0(+0x%zx(+0x%zx(%s))):string", 1844 equal ? tmp : "", equal ? "=" : "", 1845 offsetof(struct dentry, d_name.name), 1846 offsetof(struct file, f_path.dentry), 1847 equal ? equal + 1 : tmp); 1848 1849 if (ret >= bufsize - used) 1850 return -ENOMEM; 1851 argv[i] = tmpbuf + used; 1852 used += ret + 1; 1853 } 1854 1855 *buf = no_free_ptr(tmpbuf); 1856 return 0; 1857 } 1858 1859 void traceprobe_finish_parse(struct traceprobe_parse_context *ctx) 1860 { 1861 clear_btf_context(ctx); 1862 } 1863 1864 int traceprobe_update_arg(struct probe_arg *arg) 1865 { 1866 struct fetch_insn *code = arg->code; 1867 long offset; 1868 char *tmp; 1869 char c; 1870 int ret = 0; 1871 1872 while (code && code->op != FETCH_OP_END) { 1873 if (code->op == FETCH_NOP_SYMBOL) { 1874 if (code[1].op != FETCH_OP_IMM) 1875 return -EINVAL; 1876 1877 tmp = strpbrk(code->data, "+-"); 1878 if (tmp) 1879 c = *tmp; 1880 ret = traceprobe_split_symbol_offset(code->data, 1881 &offset); 1882 if (ret) 1883 return ret; 1884 1885 code[1].immediate = 1886 (unsigned long)kallsyms_lookup_name(code->data); 1887 if (tmp) 1888 *tmp = c; 1889 if (!code[1].immediate) 1890 return -ENOENT; 1891 code[1].immediate += offset; 1892 } 1893 code++; 1894 } 1895 return 0; 1896 } 1897 1898 /* When len=0, we just calculate the needed length */ 1899 #define LEN_OR_ZERO (len ? len - pos : 0) 1900 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, 1901 enum probe_print_type ptype) 1902 { 1903 struct probe_arg *parg; 1904 int i, j; 1905 int pos = 0; 1906 const char *fmt, *arg; 1907 1908 switch (ptype) { 1909 case PROBE_PRINT_NORMAL: 1910 fmt = "(%lx)"; 1911 arg = ", REC->" FIELD_STRING_IP; 1912 break; 1913 case PROBE_PRINT_RETURN: 1914 fmt = "(%lx <- %lx)"; 1915 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; 1916 break; 1917 case PROBE_PRINT_EVENT: 1918 fmt = ""; 1919 arg = ""; 1920 break; 1921 default: 1922 WARN_ON_ONCE(1); 1923 return 0; 1924 } 1925 1926 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); 1927 1928 for (i = 0; i < tp->nr_args; i++) { 1929 parg = tp->args + i; 1930 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); 1931 if (parg->count) { 1932 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", 1933 parg->type->fmt); 1934 for (j = 1; j < parg->count; j++) 1935 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", 1936 parg->type->fmt); 1937 pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); 1938 } else 1939 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", 1940 parg->type->fmt); 1941 } 1942 1943 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg); 1944 1945 for (i = 0; i < tp->nr_args; i++) { 1946 parg = tp->args + i; 1947 if (parg->count) { 1948 if (parg->type->is_string) 1949 fmt = ", __get_str(%s[%d])"; 1950 else 1951 fmt = ", REC->%s[%d]"; 1952 for (j = 0; j < parg->count; j++) 1953 pos += snprintf(buf + pos, LEN_OR_ZERO, 1954 fmt, parg->name, j); 1955 } else { 1956 if (parg->type->is_string) 1957 fmt = ", __get_str(%s)"; 1958 else 1959 fmt = ", REC->%s"; 1960 pos += snprintf(buf + pos, LEN_OR_ZERO, 1961 fmt, parg->name); 1962 } 1963 } 1964 1965 /* return the length of print_fmt */ 1966 return pos; 1967 } 1968 #undef LEN_OR_ZERO 1969 1970 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype) 1971 { 1972 struct trace_event_call *call = trace_probe_event_call(tp); 1973 int len; 1974 char *print_fmt; 1975 1976 /* First: called with 0 length to calculate the needed length */ 1977 len = __set_print_fmt(tp, NULL, 0, ptype); 1978 print_fmt = kmalloc(len + 1, GFP_KERNEL); 1979 if (!print_fmt) 1980 return -ENOMEM; 1981 1982 /* Second: actually write the @print_fmt */ 1983 __set_print_fmt(tp, print_fmt, len + 1, ptype); 1984 call->print_fmt = print_fmt; 1985 1986 return 0; 1987 } 1988 1989 int traceprobe_define_arg_fields(struct trace_event_call *event_call, 1990 size_t offset, struct trace_probe *tp) 1991 { 1992 int ret, i; 1993 1994 /* Set argument names as fields */ 1995 for (i = 0; i < tp->nr_args; i++) { 1996 struct probe_arg *parg = &tp->args[i]; 1997 const char *fmt = parg->type->fmttype; 1998 int size = parg->type->size; 1999 2000 if (parg->fmt) 2001 fmt = parg->fmt; 2002 if (parg->count) 2003 size *= parg->count; 2004 ret = trace_define_field(event_call, fmt, parg->name, 2005 offset + parg->offset, size, 2006 parg->type->is_signed, 2007 FILTER_OTHER); 2008 if (ret) 2009 return ret; 2010 } 2011 return 0; 2012 } 2013 2014 static void trace_probe_event_free(struct trace_probe_event *tpe) 2015 { 2016 kfree(tpe->class.system); 2017 kfree(tpe->call.name); 2018 kfree(tpe->call.print_fmt); 2019 kfree(tpe); 2020 } 2021 2022 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to) 2023 { 2024 if (trace_probe_has_sibling(tp)) 2025 return -EBUSY; 2026 2027 list_del_init(&tp->list); 2028 trace_probe_event_free(tp->event); 2029 2030 tp->event = to->event; 2031 list_add_tail(&tp->list, trace_probe_probe_list(to)); 2032 2033 return 0; 2034 } 2035 2036 void trace_probe_unlink(struct trace_probe *tp) 2037 { 2038 list_del_init(&tp->list); 2039 if (list_empty(trace_probe_probe_list(tp))) 2040 trace_probe_event_free(tp->event); 2041 tp->event = NULL; 2042 } 2043 2044 void trace_probe_cleanup(struct trace_probe *tp) 2045 { 2046 int i; 2047 2048 for (i = 0; i < tp->nr_args; i++) 2049 traceprobe_free_probe_arg(&tp->args[i]); 2050 2051 if (tp->entry_arg) { 2052 kfree(tp->entry_arg->code); 2053 kfree(tp->entry_arg); 2054 tp->entry_arg = NULL; 2055 } 2056 2057 if (tp->event) 2058 trace_probe_unlink(tp); 2059 } 2060 2061 int trace_probe_init(struct trace_probe *tp, const char *event, 2062 const char *group, bool alloc_filter, int nargs) 2063 { 2064 struct trace_event_call *call; 2065 size_t size = sizeof(struct trace_probe_event); 2066 int ret = 0; 2067 2068 if (!event || !group) 2069 return -EINVAL; 2070 2071 if (alloc_filter) 2072 size += sizeof(struct trace_uprobe_filter); 2073 2074 tp->event = kzalloc(size, GFP_KERNEL); 2075 if (!tp->event) 2076 return -ENOMEM; 2077 2078 INIT_LIST_HEAD(&tp->event->files); 2079 INIT_LIST_HEAD(&tp->event->class.fields); 2080 INIT_LIST_HEAD(&tp->event->probes); 2081 INIT_LIST_HEAD(&tp->list); 2082 list_add(&tp->list, &tp->event->probes); 2083 2084 call = trace_probe_event_call(tp); 2085 call->class = &tp->event->class; 2086 call->name = kstrdup(event, GFP_KERNEL); 2087 if (!call->name) { 2088 ret = -ENOMEM; 2089 goto error; 2090 } 2091 2092 tp->event->class.system = kstrdup(group, GFP_KERNEL); 2093 if (!tp->event->class.system) { 2094 ret = -ENOMEM; 2095 goto error; 2096 } 2097 2098 tp->nr_args = nargs; 2099 /* Make sure pointers in args[] are NULL */ 2100 if (nargs) 2101 memset(tp->args, 0, sizeof(tp->args[0]) * nargs); 2102 2103 return 0; 2104 2105 error: 2106 trace_probe_cleanup(tp); 2107 return ret; 2108 } 2109 2110 static struct trace_event_call * 2111 find_trace_event_call(const char *system, const char *event_name) 2112 { 2113 struct trace_event_call *tp_event; 2114 const char *name; 2115 2116 list_for_each_entry(tp_event, &ftrace_events, list) { 2117 if (!tp_event->class->system || 2118 strcmp(system, tp_event->class->system)) 2119 continue; 2120 name = trace_event_name(tp_event); 2121 if (!name || strcmp(event_name, name)) 2122 continue; 2123 return tp_event; 2124 } 2125 2126 return NULL; 2127 } 2128 2129 int trace_probe_register_event_call(struct trace_probe *tp) 2130 { 2131 struct trace_event_call *call = trace_probe_event_call(tp); 2132 int ret; 2133 2134 lockdep_assert_held(&event_mutex); 2135 2136 if (find_trace_event_call(trace_probe_group_name(tp), 2137 trace_probe_name(tp))) 2138 return -EEXIST; 2139 2140 ret = register_trace_event(&call->event); 2141 if (!ret) 2142 return -ENODEV; 2143 2144 ret = trace_add_event_call(call); 2145 if (ret) 2146 unregister_trace_event(&call->event); 2147 2148 return ret; 2149 } 2150 2151 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) 2152 { 2153 struct event_file_link *link; 2154 2155 link = kmalloc_obj(*link); 2156 if (!link) 2157 return -ENOMEM; 2158 2159 link->file = file; 2160 INIT_LIST_HEAD(&link->list); 2161 list_add_tail_rcu(&link->list, &tp->event->files); 2162 trace_probe_set_flag(tp, TP_FLAG_TRACE); 2163 return 0; 2164 } 2165 2166 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, 2167 struct trace_event_file *file) 2168 { 2169 struct event_file_link *link; 2170 2171 trace_probe_for_each_link(link, tp) { 2172 if (link->file == file) 2173 return link; 2174 } 2175 2176 return NULL; 2177 } 2178 2179 int trace_probe_remove_file(struct trace_probe *tp, 2180 struct trace_event_file *file) 2181 { 2182 struct event_file_link *link; 2183 2184 link = trace_probe_get_file_link(tp, file); 2185 if (!link) 2186 return -ENOENT; 2187 2188 list_del_rcu(&link->list); 2189 kvfree_rcu_mightsleep(link); 2190 2191 if (list_empty(&tp->event->files)) 2192 trace_probe_clear_flag(tp, TP_FLAG_TRACE); 2193 2194 return 0; 2195 } 2196 2197 /* 2198 * Return the smallest index of different type argument (start from 1). 2199 * If all argument types and name are same, return 0. 2200 */ 2201 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b) 2202 { 2203 int i; 2204 2205 /* In case of more arguments */ 2206 if (a->nr_args < b->nr_args) 2207 return a->nr_args + 1; 2208 if (a->nr_args > b->nr_args) 2209 return b->nr_args + 1; 2210 2211 for (i = 0; i < a->nr_args; i++) { 2212 if ((b->nr_args <= i) || 2213 ((a->args[i].type != b->args[i].type) || 2214 (a->args[i].count != b->args[i].count) || 2215 strcmp(a->args[i].name, b->args[i].name))) 2216 return i + 1; 2217 } 2218 2219 return 0; 2220 } 2221 2222 bool trace_probe_match_command_args(struct trace_probe *tp, 2223 int argc, const char **argv) 2224 { 2225 char buf[MAX_ARGSTR_LEN + 1]; 2226 int i; 2227 2228 if (tp->nr_args < argc) 2229 return false; 2230 2231 for (i = 0; i < argc; i++) { 2232 snprintf(buf, sizeof(buf), "%s=%s", 2233 tp->args[i].name, tp->args[i].comm); 2234 if (strcmp(buf, argv[i])) 2235 return false; 2236 } 2237 return true; 2238 } 2239 2240 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **)) 2241 { 2242 int argc = 0, ret = 0; 2243 char **argv; 2244 2245 argv = argv_split(GFP_KERNEL, raw_command, &argc); 2246 if (!argv) 2247 return -ENOMEM; 2248 2249 if (argc) 2250 ret = createfn(argc, (const char **)argv); 2251 2252 argv_free(argv); 2253 2254 return ret; 2255 } 2256 2257 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args, 2258 u8 *data, void *field) 2259 { 2260 void *p; 2261 int i, j; 2262 2263 for (i = 0; i < nr_args; i++) { 2264 struct probe_arg *a = args + i; 2265 2266 trace_seq_printf(s, " %s=", a->name); 2267 if (likely(!a->count)) { 2268 if (!a->type->print(s, data + a->offset, field)) 2269 return -ENOMEM; 2270 continue; 2271 } 2272 trace_seq_putc(s, '{'); 2273 p = data + a->offset; 2274 for (j = 0; j < a->count; j++) { 2275 if (!a->type->print(s, p, field)) 2276 return -ENOMEM; 2277 trace_seq_putc(s, j == a->count - 1 ? '}' : ','); 2278 p += a->type->size; 2279 } 2280 } 2281 return 0; 2282 } 2283