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 /* backward compatibility */ 966 ctx->offset = 0; 967 goto inval; 968 } 969 970 if (str_has_prefix(arg, "retval")) { 971 if (!(ctx->flags & TPARG_FL_RETURN)) { 972 err = TP_ERR_RETVAL_ON_PROBE; 973 goto inval; 974 } 975 if (!(ctx->flags & TPARG_FL_KERNEL) || 976 !IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) { 977 code->op = FETCH_OP_RETVAL; 978 return 0; 979 } 980 return parse_btf_arg(orig_arg, pcode, end, ctx); 981 } 982 983 len = str_has_prefix(arg, "stack"); 984 if (len) { 985 986 if (arg[len] == '\0') { 987 code->op = FETCH_OP_STACKP; 988 return 0; 989 } 990 991 if (isdigit(arg[len])) { 992 ret = kstrtoul(arg + len, 10, ¶m); 993 if (ret) 994 goto inval; 995 996 if ((ctx->flags & TPARG_FL_KERNEL) && 997 param > PARAM_MAX_STACK) { 998 err = TP_ERR_BAD_STACK_NUM; 999 goto inval; 1000 } 1001 code->op = FETCH_OP_STACK; 1002 code->param = (unsigned int)param; 1003 return 0; 1004 } 1005 goto inval; 1006 } 1007 1008 if (strcmp(arg, "comm") == 0 || strcmp(arg, "COMM") == 0) { 1009 code->op = FETCH_OP_COMM; 1010 return 0; 1011 } 1012 1013 #ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API 1014 len = str_has_prefix(arg, "arg"); 1015 if (len) { 1016 ret = kstrtoul(arg + len, 10, ¶m); 1017 if (ret) 1018 goto inval; 1019 1020 if (!param || param > PARAM_MAX_STACK) { 1021 err = TP_ERR_BAD_ARG_NUM; 1022 goto inval; 1023 } 1024 param--; /* argN starts from 1, but internal arg[N] starts from 0 */ 1025 1026 if (tparg_is_function_entry(ctx->flags)) { 1027 code->op = FETCH_OP_ARG; 1028 code->param = (unsigned int)param; 1029 /* 1030 * The tracepoint probe will probe a stub function, and the 1031 * first parameter of the stub is a dummy and should be ignored. 1032 */ 1033 if (ctx->flags & TPARG_FL_TPOINT) 1034 code->param++; 1035 } else if (tparg_is_function_return(ctx->flags)) { 1036 /* function entry argument access from return probe */ 1037 ret = __store_entry_arg(ctx->tp, param); 1038 if (ret < 0) /* This error should be an internal error */ 1039 return ret; 1040 1041 code->op = FETCH_OP_EDATA; 1042 code->offset = ret; 1043 } else { 1044 err = TP_ERR_NOFENTRY_ARGS; 1045 goto inval; 1046 } 1047 return 0; 1048 } 1049 #endif 1050 1051 inval: 1052 __trace_probe_log_err(ctx->offset, err); 1053 return -EINVAL; 1054 } 1055 1056 static int str_to_immediate(char *str, unsigned long *imm) 1057 { 1058 if (isdigit(str[0])) 1059 return kstrtoul(str, 0, imm); 1060 else if (str[0] == '-') 1061 return kstrtol(str, 0, (long *)imm); 1062 else if (str[0] == '+') 1063 return kstrtol(str + 1, 0, (long *)imm); 1064 return -EINVAL; 1065 } 1066 1067 static int __parse_imm_string(char *str, char **pbuf, int offs) 1068 { 1069 size_t len = strlen(str); 1070 1071 if (!len || str[len - 1] != '"') { 1072 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE); 1073 return -EINVAL; 1074 } 1075 *pbuf = kstrndup(str, len - 1, GFP_KERNEL); 1076 if (!*pbuf) 1077 return -ENOMEM; 1078 return 0; 1079 } 1080 1081 /* Recursive argument parser */ 1082 static int 1083 parse_probe_arg(char *arg, const struct fetch_type *type, 1084 struct fetch_insn **pcode, struct fetch_insn *end, 1085 struct traceprobe_parse_context *ctx) 1086 { 1087 struct fetch_insn *code = *pcode; 1088 unsigned long param; 1089 int deref = FETCH_OP_DEREF; 1090 long offset = 0; 1091 char *tmp; 1092 int ret = 0; 1093 1094 switch (arg[0]) { 1095 case '$': 1096 ret = parse_probe_vars(arg, type, pcode, end, ctx); 1097 break; 1098 1099 case '%': /* named register */ 1100 if (ctx->flags & (TPARG_FL_TEVENT | TPARG_FL_FPROBE)) { 1101 /* eprobe and fprobe do not handle registers */ 1102 trace_probe_log_err(ctx->offset, BAD_VAR); 1103 break; 1104 } 1105 ret = regs_query_register_offset(arg + 1); 1106 if (ret >= 0) { 1107 code->op = FETCH_OP_REG; 1108 code->param = (unsigned int)ret; 1109 ret = 0; 1110 } else 1111 trace_probe_log_err(ctx->offset, BAD_REG_NAME); 1112 break; 1113 1114 case '@': /* memory, file-offset or symbol */ 1115 if (isdigit(arg[1])) { 1116 ret = kstrtoul(arg + 1, 0, ¶m); 1117 if (ret) { 1118 trace_probe_log_err(ctx->offset, BAD_MEM_ADDR); 1119 break; 1120 } 1121 /* load address */ 1122 code->op = FETCH_OP_IMM; 1123 code->immediate = param; 1124 } else if (arg[1] == '+') { 1125 /* kprobes don't support file offsets */ 1126 if (ctx->flags & TPARG_FL_KERNEL) { 1127 trace_probe_log_err(ctx->offset, FILE_ON_KPROBE); 1128 return -EINVAL; 1129 } 1130 ret = kstrtol(arg + 2, 0, &offset); 1131 if (ret) { 1132 trace_probe_log_err(ctx->offset, BAD_FILE_OFFS); 1133 break; 1134 } 1135 1136 code->op = FETCH_OP_FOFFS; 1137 code->immediate = (unsigned long)offset; // imm64? 1138 } else { 1139 /* uprobes don't support symbols */ 1140 if (!(ctx->flags & TPARG_FL_KERNEL)) { 1141 trace_probe_log_err(ctx->offset, SYM_ON_UPROBE); 1142 return -EINVAL; 1143 } 1144 /* Preserve symbol for updating */ 1145 code->op = FETCH_NOP_SYMBOL; 1146 code->data = kstrdup(arg + 1, GFP_KERNEL); 1147 if (!code->data) 1148 return -ENOMEM; 1149 if (++code == end) { 1150 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1151 return -EINVAL; 1152 } 1153 code->op = FETCH_OP_IMM; 1154 code->immediate = 0; 1155 } 1156 /* These are fetching from memory */ 1157 if (++code == end) { 1158 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1159 return -EINVAL; 1160 } 1161 *pcode = code; 1162 code->op = FETCH_OP_DEREF; 1163 code->offset = offset; 1164 break; 1165 1166 case '+': /* deref memory */ 1167 case '-': 1168 if (arg[1] == 'u') { 1169 deref = FETCH_OP_UDEREF; 1170 arg[1] = arg[0]; 1171 arg++; 1172 } 1173 if (arg[0] == '+') 1174 arg++; /* Skip '+', because kstrtol() rejects it. */ 1175 tmp = strchr(arg, '('); 1176 if (!tmp) { 1177 trace_probe_log_err(ctx->offset, DEREF_NEED_BRACE); 1178 return -EINVAL; 1179 } 1180 *tmp = '\0'; 1181 ret = kstrtol(arg, 0, &offset); 1182 if (ret) { 1183 trace_probe_log_err(ctx->offset, BAD_DEREF_OFFS); 1184 break; 1185 } 1186 ctx->offset += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); 1187 arg = tmp + 1; 1188 tmp = strrchr(arg, ')'); 1189 if (!tmp) { 1190 trace_probe_log_err(ctx->offset + strlen(arg), 1191 DEREF_OPEN_BRACE); 1192 return -EINVAL; 1193 } else { 1194 const struct fetch_type *t2 = find_fetch_type(NULL, ctx->flags); 1195 int cur_offs = ctx->offset; 1196 1197 *tmp = '\0'; 1198 ret = parse_probe_arg(arg, t2, &code, end, ctx); 1199 if (ret) 1200 break; 1201 ctx->offset = cur_offs; 1202 if (code->op == FETCH_OP_COMM || 1203 code->op == FETCH_OP_DATA) { 1204 trace_probe_log_err(ctx->offset, COMM_CANT_DEREF); 1205 return -EINVAL; 1206 } 1207 if (++code == end) { 1208 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1209 return -EINVAL; 1210 } 1211 *pcode = code; 1212 1213 code->op = deref; 1214 code->offset = offset; 1215 /* Reset the last type if used */ 1216 ctx->last_type = NULL; 1217 } 1218 break; 1219 case '\\': /* Immediate value */ 1220 if (arg[1] == '"') { /* Immediate string */ 1221 ret = __parse_imm_string(arg + 2, &tmp, ctx->offset + 2); 1222 if (ret) 1223 break; 1224 code->op = FETCH_OP_DATA; 1225 code->data = tmp; 1226 } else { 1227 ret = str_to_immediate(arg + 1, &code->immediate); 1228 if (ret) 1229 trace_probe_log_err(ctx->offset + 1, BAD_IMM); 1230 else 1231 code->op = FETCH_OP_IMM; 1232 } 1233 break; 1234 default: 1235 if (isalpha(arg[0]) || arg[0] == '_') { /* BTF variable */ 1236 if (!tparg_is_function_entry(ctx->flags) && 1237 !tparg_is_function_return(ctx->flags)) { 1238 trace_probe_log_err(ctx->offset, NOSUP_BTFARG); 1239 return -EINVAL; 1240 } 1241 ret = parse_btf_arg(arg, pcode, end, ctx); 1242 break; 1243 } 1244 } 1245 if (!ret && code->op == FETCH_OP_NOP) { 1246 /* Parsed, but do not find fetch method */ 1247 trace_probe_log_err(ctx->offset, BAD_FETCH_ARG); 1248 ret = -EINVAL; 1249 } 1250 return ret; 1251 } 1252 1253 /* Bitfield type needs to be parsed into a fetch function */ 1254 static int __parse_bitfield_probe_arg(const char *bf, 1255 const struct fetch_type *t, 1256 struct fetch_insn **pcode) 1257 { 1258 struct fetch_insn *code = *pcode; 1259 unsigned long bw, bo; 1260 char *tail; 1261 1262 if (*bf != 'b') 1263 return 0; 1264 1265 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ 1266 1267 if (bw == 0 || *tail != '@') 1268 return -EINVAL; 1269 1270 bf = tail + 1; 1271 bo = simple_strtoul(bf, &tail, 0); 1272 1273 if (tail == bf || *tail != '/') 1274 return -EINVAL; 1275 code++; 1276 if (code->op != FETCH_OP_NOP) 1277 return -EINVAL; 1278 *pcode = code; 1279 1280 code->op = FETCH_OP_MOD_BF; 1281 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); 1282 code->rshift = BYTES_TO_BITS(t->size) - bw; 1283 code->basesize = t->size; 1284 1285 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; 1286 } 1287 1288 /* Split type part from @arg and return it. */ 1289 static char *parse_probe_arg_type(char *arg, struct probe_arg *parg, 1290 struct traceprobe_parse_context *ctx) 1291 { 1292 char *t = NULL, *t2, *t3; 1293 int offs; 1294 1295 t = strchr(arg, ':'); 1296 if (t) { 1297 *t++ = '\0'; 1298 t2 = strchr(t, '['); 1299 if (t2) { 1300 *t2++ = '\0'; 1301 t3 = strchr(t2, ']'); 1302 if (!t3) { 1303 offs = t2 + strlen(t2) - arg; 1304 1305 trace_probe_log_err(ctx->offset + offs, 1306 ARRAY_NO_CLOSE); 1307 return ERR_PTR(-EINVAL); 1308 } else if (t3[1] != '\0') { 1309 trace_probe_log_err(ctx->offset + t3 + 1 - arg, 1310 BAD_ARRAY_SUFFIX); 1311 return ERR_PTR(-EINVAL); 1312 } 1313 *t3 = '\0'; 1314 if (kstrtouint(t2, 0, &parg->count) || !parg->count) { 1315 trace_probe_log_err(ctx->offset + t2 - arg, 1316 BAD_ARRAY_NUM); 1317 return ERR_PTR(-EINVAL); 1318 } 1319 if (parg->count > MAX_ARRAY_LEN) { 1320 trace_probe_log_err(ctx->offset + t2 - arg, 1321 ARRAY_TOO_BIG); 1322 return ERR_PTR(-EINVAL); 1323 } 1324 } 1325 } 1326 offs = t ? t - arg : 0; 1327 1328 /* 1329 * Since $comm and immediate string can not be dereferenced, 1330 * we can find those by strcmp. But ignore for eprobes. 1331 */ 1332 if (!(ctx->flags & TPARG_FL_TEVENT) && 1333 (strcmp(arg, "$comm") == 0 || strcmp(arg, "$COMM") == 0 || 1334 strncmp(arg, "\\\"", 2) == 0)) { 1335 /* The type of $comm must be "string", and not an array type. */ 1336 if (parg->count || (t && strcmp(t, "string"))) { 1337 trace_probe_log_err(ctx->offset + offs, NEED_STRING_TYPE); 1338 return ERR_PTR(-EINVAL); 1339 } 1340 parg->type = find_fetch_type("string", ctx->flags); 1341 } else 1342 parg->type = find_fetch_type(t, ctx->flags); 1343 1344 if (!parg->type) { 1345 trace_probe_log_err(ctx->offset + offs, BAD_TYPE); 1346 return ERR_PTR(-EINVAL); 1347 } 1348 1349 return t; 1350 } 1351 1352 /* After parsing, adjust the fetch_insn according to the probe_arg */ 1353 static int finalize_fetch_insn(struct fetch_insn *code, 1354 struct probe_arg *parg, 1355 char *type, 1356 int type_offset, 1357 struct traceprobe_parse_context *ctx) 1358 { 1359 struct fetch_insn *scode; 1360 int ret; 1361 1362 /* Store operation */ 1363 if (parg->type->is_string) { 1364 /* Check bad combination of the type and the last fetch_insn. */ 1365 if (!strcmp(parg->type->name, "symstr")) { 1366 if (code->op != FETCH_OP_REG && code->op != FETCH_OP_STACK && 1367 code->op != FETCH_OP_RETVAL && code->op != FETCH_OP_ARG && 1368 code->op != FETCH_OP_DEREF && code->op != FETCH_OP_TP_ARG) { 1369 trace_probe_log_err(ctx->offset + type_offset, 1370 BAD_SYMSTRING); 1371 return -EINVAL; 1372 } 1373 } else { 1374 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF && 1375 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM && 1376 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) { 1377 trace_probe_log_err(ctx->offset + type_offset, 1378 BAD_STRING); 1379 return -EINVAL; 1380 } 1381 } 1382 1383 if (!strcmp(parg->type->name, "symstr") || 1384 (code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM || 1385 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG || 1386 parg->count) { 1387 /* 1388 * IMM, DATA and COMM is pointing actual address, those 1389 * must be kept, and if parg->count != 0, this is an 1390 * array of string pointers instead of string address 1391 * itself. 1392 * For the symstr, it doesn't need to dereference, thus 1393 * it just get the value. 1394 */ 1395 code++; 1396 if (code->op != FETCH_OP_NOP) { 1397 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1398 return -EINVAL; 1399 } 1400 } 1401 1402 /* If op == DEREF, replace it with STRING */ 1403 if (!strcmp(parg->type->name, "ustring") || 1404 code->op == FETCH_OP_UDEREF) 1405 code->op = FETCH_OP_ST_USTRING; 1406 else if (!strcmp(parg->type->name, "symstr")) 1407 code->op = FETCH_OP_ST_SYMSTR; 1408 else 1409 code->op = FETCH_OP_ST_STRING; 1410 code->size = parg->type->size; 1411 parg->dynamic = true; 1412 } else if (code->op == FETCH_OP_DEREF) { 1413 code->op = FETCH_OP_ST_MEM; 1414 code->size = parg->type->size; 1415 } else if (code->op == FETCH_OP_UDEREF) { 1416 code->op = FETCH_OP_ST_UMEM; 1417 code->size = parg->type->size; 1418 } else { 1419 code++; 1420 if (code->op != FETCH_OP_NOP) { 1421 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1422 return -E2BIG; 1423 } 1424 code->op = FETCH_OP_ST_RAW; 1425 code->size = parg->type->size; 1426 } 1427 1428 /* Save storing fetch_insn. */ 1429 scode = code; 1430 1431 /* Modify operation */ 1432 if (type != NULL) { 1433 /* Bitfield needs a special fetch_insn. */ 1434 ret = __parse_bitfield_probe_arg(type, parg->type, &code); 1435 if (ret) { 1436 trace_probe_log_err(ctx->offset + type_offset, BAD_BITFIELD); 1437 return ret; 1438 } 1439 } else if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && 1440 ctx->last_type) { 1441 /* If user not specified the type, try parsing BTF bitfield. */ 1442 ret = parse_btf_bitfield(&code, ctx); 1443 if (ret) 1444 return ret; 1445 } 1446 1447 /* Loop(Array) operation */ 1448 if (parg->count) { 1449 if (scode->op != FETCH_OP_ST_MEM && 1450 scode->op != FETCH_OP_ST_STRING && 1451 scode->op != FETCH_OP_ST_USTRING) { 1452 trace_probe_log_err(ctx->offset + type_offset, BAD_STRING); 1453 return -EINVAL; 1454 } 1455 code++; 1456 if (code->op != FETCH_OP_NOP) { 1457 trace_probe_log_err(ctx->offset, TOO_MANY_OPS); 1458 return -E2BIG; 1459 } 1460 code->op = FETCH_OP_LP_ARRAY; 1461 code->param = parg->count; 1462 } 1463 1464 /* Finalize the fetch_insn array. */ 1465 code++; 1466 code->op = FETCH_OP_END; 1467 1468 return 0; 1469 } 1470 1471 /* String length checking wrapper */ 1472 static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, 1473 struct probe_arg *parg, 1474 struct traceprobe_parse_context *ctx) 1475 { 1476 struct fetch_insn *code, *tmp = NULL; 1477 char *type, *arg __free(kfree) = NULL; 1478 int ret, len; 1479 1480 len = strlen(argv); 1481 if (len > MAX_ARGSTR_LEN) { 1482 trace_probe_log_err(ctx->offset, ARG_TOO_LONG); 1483 return -E2BIG; 1484 } else if (len == 0) { 1485 trace_probe_log_err(ctx->offset, NO_ARG_BODY); 1486 return -EINVAL; 1487 } 1488 1489 arg = kstrdup(argv, GFP_KERNEL); 1490 if (!arg) 1491 return -ENOMEM; 1492 1493 parg->comm = kstrdup(arg, GFP_KERNEL); 1494 if (!parg->comm) 1495 return -ENOMEM; 1496 1497 type = parse_probe_arg_type(arg, parg, ctx); 1498 if (IS_ERR(type)) 1499 return PTR_ERR(type); 1500 1501 code = tmp = kzalloc_objs(*code, FETCH_INSN_MAX); 1502 if (!code) 1503 return -ENOMEM; 1504 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; 1505 1506 ctx->last_type = NULL; 1507 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], 1508 ctx); 1509 if (ret < 0) 1510 goto fail; 1511 1512 /* Update storing type if BTF is available */ 1513 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS) && 1514 ctx->last_type) { 1515 if (!type) { 1516 parg->type = find_fetch_type_from_btf_type(ctx); 1517 } else if (strstr(type, "string")) { 1518 ret = check_prepare_btf_string_fetch(type, &code, ctx); 1519 if (ret) 1520 goto fail; 1521 } 1522 } 1523 parg->offset = *size; 1524 *size += parg->type->size * (parg->count ?: 1); 1525 1526 if (*size > MAX_PROBE_EVENT_SIZE) { 1527 ret = -E2BIG; 1528 trace_probe_log_err(ctx->offset, EVENT_TOO_BIG); 1529 goto fail; 1530 } 1531 1532 if (parg->count) { 1533 len = strlen(parg->type->fmttype) + 6; 1534 parg->fmt = kmalloc(len, GFP_KERNEL); 1535 if (!parg->fmt) { 1536 ret = -ENOMEM; 1537 goto fail; 1538 } 1539 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, 1540 parg->count); 1541 } 1542 1543 ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx); 1544 if (ret < 0) 1545 goto fail; 1546 1547 for (; code < tmp + FETCH_INSN_MAX; code++) 1548 if (code->op == FETCH_OP_END) 1549 break; 1550 /* Shrink down the code buffer */ 1551 parg->code = kzalloc_objs(*code, code - tmp + 1); 1552 if (!parg->code) 1553 ret = -ENOMEM; 1554 else 1555 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); 1556 1557 fail: 1558 if (ret < 0) { 1559 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) 1560 if (code->op == FETCH_NOP_SYMBOL || 1561 code->op == FETCH_OP_DATA) 1562 kfree(code->data); 1563 } 1564 kfree(tmp); 1565 1566 return ret; 1567 } 1568 1569 /* Return 1 if name is reserved or already used by another argument */ 1570 static int traceprobe_conflict_field_name(const char *name, 1571 struct probe_arg *args, int narg) 1572 { 1573 int i; 1574 1575 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) 1576 if (strcmp(reserved_field_names[i], name) == 0) 1577 return 1; 1578 1579 for (i = 0; i < narg; i++) 1580 if (strcmp(args[i].name, name) == 0) 1581 return 1; 1582 1583 return 0; 1584 } 1585 1586 static char *generate_probe_arg_name(const char *arg, int idx) 1587 { 1588 char *name = NULL; 1589 const char *end; 1590 1591 /* 1592 * If argument name is omitted, try arg as a name (BTF variable) 1593 * or "argN". 1594 */ 1595 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) { 1596 end = strchr(arg, ':'); 1597 if (!end) 1598 end = arg + strlen(arg); 1599 1600 name = kmemdup_nul(arg, end - arg, GFP_KERNEL); 1601 if (!name || !is_good_name(name)) { 1602 kfree(name); 1603 name = NULL; 1604 } 1605 } 1606 1607 if (!name) 1608 name = kasprintf(GFP_KERNEL, "arg%d", idx + 1); 1609 1610 return name; 1611 } 1612 1613 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg, 1614 struct traceprobe_parse_context *ctx) 1615 { 1616 struct probe_arg *parg = &tp->args[i]; 1617 const char *body; 1618 1619 ctx->tp = tp; 1620 body = strchr(arg, '='); 1621 if (body) { 1622 if (body - arg > MAX_ARG_NAME_LEN) { 1623 trace_probe_log_err(0, ARG_NAME_TOO_LONG); 1624 return -EINVAL; 1625 } else if (body == arg) { 1626 trace_probe_log_err(0, NO_ARG_NAME); 1627 return -EINVAL; 1628 } 1629 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); 1630 body++; 1631 } else { 1632 parg->name = generate_probe_arg_name(arg, i); 1633 body = arg; 1634 } 1635 if (!parg->name) 1636 return -ENOMEM; 1637 1638 if (!is_good_name(parg->name)) { 1639 trace_probe_log_err(0, BAD_ARG_NAME); 1640 return -EINVAL; 1641 } 1642 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { 1643 trace_probe_log_err(0, USED_ARG_NAME); 1644 return -EINVAL; 1645 } 1646 ctx->offset = body - arg; 1647 /* Parse fetch argument */ 1648 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx); 1649 } 1650 1651 void traceprobe_free_probe_arg(struct probe_arg *arg) 1652 { 1653 struct fetch_insn *code = arg->code; 1654 1655 while (code && code->op != FETCH_OP_END) { 1656 if (code->op == FETCH_NOP_SYMBOL || 1657 code->op == FETCH_OP_DATA) 1658 kfree(code->data); 1659 code++; 1660 } 1661 kfree(arg->code); 1662 kfree(arg->name); 1663 kfree(arg->comm); 1664 kfree(arg->fmt); 1665 } 1666 1667 static int argv_has_var_arg(int argc, const char *argv[], int *args_idx, 1668 struct traceprobe_parse_context *ctx) 1669 { 1670 int i, found = 0; 1671 1672 for (i = 0; i < argc; i++) 1673 if (str_has_prefix(argv[i], "$arg")) { 1674 trace_probe_log_set_index(i + 2); 1675 1676 if (!tparg_is_function_entry(ctx->flags) && 1677 !tparg_is_function_return(ctx->flags)) { 1678 trace_probe_log_err(0, NOFENTRY_ARGS); 1679 return -EINVAL; 1680 } 1681 1682 if (isdigit(argv[i][4])) { 1683 found = 1; 1684 continue; 1685 } 1686 1687 if (argv[i][4] != '*') { 1688 trace_probe_log_err(0, BAD_VAR); 1689 return -EINVAL; 1690 } 1691 1692 if (*args_idx >= 0 && *args_idx < argc) { 1693 trace_probe_log_err(0, DOUBLE_ARGS); 1694 return -EINVAL; 1695 } 1696 found = 1; 1697 *args_idx = i; 1698 } 1699 1700 return found; 1701 } 1702 1703 static int sprint_nth_btf_arg(int idx, const char *type, 1704 char *buf, int bufsize, 1705 struct traceprobe_parse_context *ctx) 1706 { 1707 const char *name; 1708 int ret; 1709 1710 if (idx >= ctx->nr_params) { 1711 trace_probe_log_err(0, NO_BTFARG); 1712 return -ENOENT; 1713 } 1714 name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off); 1715 if (!name) { 1716 trace_probe_log_err(0, NO_BTF_ENTRY); 1717 return -ENOENT; 1718 } 1719 ret = snprintf(buf, bufsize, "%s%s", name, type); 1720 if (ret >= bufsize) { 1721 trace_probe_log_err(0, ARGS_2LONG); 1722 return -E2BIG; 1723 } 1724 return ret; 1725 } 1726 1727 /* Return new_argv which must be freed after use */ 1728 const char **traceprobe_expand_meta_args(int argc, const char *argv[], 1729 int *new_argc, char *buf, int bufsize, 1730 struct traceprobe_parse_context *ctx) 1731 { 1732 const struct btf_param *params = NULL; 1733 int i, j, n, used, ret, args_idx = -1; 1734 const char **new_argv __free(kfree) = NULL; 1735 1736 ret = argv_has_var_arg(argc, argv, &args_idx, ctx); 1737 if (ret < 0) 1738 return ERR_PTR(ret); 1739 1740 if (!ret) { 1741 *new_argc = argc; 1742 return NULL; 1743 } 1744 1745 ret = query_btf_context(ctx); 1746 if (ret < 0 || ctx->nr_params == 0) { 1747 if (args_idx != -1) { 1748 /* $arg* requires BTF info */ 1749 trace_probe_log_err(0, NOSUP_BTFARG); 1750 return (const char **)params; 1751 } 1752 *new_argc = argc; 1753 return NULL; 1754 } 1755 1756 if (args_idx >= 0) 1757 *new_argc = argc + ctx->nr_params - 1; 1758 else 1759 *new_argc = argc; 1760 1761 new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL); 1762 if (!new_argv) 1763 return ERR_PTR(-ENOMEM); 1764 1765 used = 0; 1766 for (i = 0, j = 0; i < argc; i++) { 1767 trace_probe_log_set_index(i + 2); 1768 if (i == args_idx) { 1769 for (n = 0; n < ctx->nr_params; n++) { 1770 ret = sprint_nth_btf_arg(n, "", buf + used, 1771 bufsize - used, ctx); 1772 if (ret < 0) 1773 return ERR_PTR(ret); 1774 1775 new_argv[j++] = buf + used; 1776 used += ret + 1; 1777 } 1778 continue; 1779 } 1780 1781 if (str_has_prefix(argv[i], "$arg")) { 1782 char *type = NULL; 1783 1784 n = simple_strtoul(argv[i] + 4, &type, 10); 1785 if (type && !(*type == ':' || *type == '\0')) { 1786 trace_probe_log_err(0, BAD_VAR); 1787 return ERR_PTR(-ENOENT); 1788 } 1789 /* Note: $argN starts from $arg1 */ 1790 ret = sprint_nth_btf_arg(n - 1, type, buf + used, 1791 bufsize - used, ctx); 1792 if (ret < 0) 1793 return ERR_PTR(ret); 1794 new_argv[j++] = buf + used; 1795 used += ret + 1; 1796 } else 1797 new_argv[j++] = argv[i]; 1798 } 1799 1800 return_ptr(new_argv); 1801 } 1802 1803 /* @buf: *buf must be equal to NULL. Caller must to free *buf */ 1804 int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf) 1805 { 1806 int i, used, ret; 1807 const int bufsize = MAX_DENTRY_ARGS_LEN; 1808 char *tmpbuf __free(kfree) = NULL; 1809 1810 if (*buf) 1811 return -EINVAL; 1812 1813 used = 0; 1814 for (i = 0; i < argc; i++) { 1815 char *tmp __free(kfree) = NULL; 1816 char *equal; 1817 size_t arg_len; 1818 1819 if (!glob_match("*:%p[dD]", argv[i])) 1820 continue; 1821 1822 if (!tmpbuf) { 1823 tmpbuf = kmalloc(bufsize, GFP_KERNEL); 1824 if (!tmpbuf) 1825 return -ENOMEM; 1826 } 1827 1828 tmp = kstrdup(argv[i], GFP_KERNEL); 1829 if (!tmp) 1830 return -ENOMEM; 1831 1832 equal = strchr(tmp, '='); 1833 if (equal) 1834 *equal = '\0'; 1835 arg_len = strlen(argv[i]); 1836 tmp[arg_len - 4] = '\0'; 1837 if (argv[i][arg_len - 1] == 'd') 1838 ret = snprintf(tmpbuf + used, bufsize - used, 1839 "%s%s+0x0(+0x%zx(%s)):string", 1840 equal ? tmp : "", equal ? "=" : "", 1841 offsetof(struct dentry, d_name.name), 1842 equal ? equal + 1 : tmp); 1843 else 1844 ret = snprintf(tmpbuf + used, bufsize - used, 1845 "%s%s+0x0(+0x%zx(+0x%zx(%s))):string", 1846 equal ? tmp : "", equal ? "=" : "", 1847 offsetof(struct dentry, d_name.name), 1848 offsetof(struct file, f_path.dentry), 1849 equal ? equal + 1 : tmp); 1850 1851 if (ret >= bufsize - used) 1852 return -ENOMEM; 1853 argv[i] = tmpbuf + used; 1854 used += ret + 1; 1855 } 1856 1857 *buf = no_free_ptr(tmpbuf); 1858 return 0; 1859 } 1860 1861 void traceprobe_finish_parse(struct traceprobe_parse_context *ctx) 1862 { 1863 clear_btf_context(ctx); 1864 } 1865 1866 int traceprobe_update_arg(struct probe_arg *arg) 1867 { 1868 struct fetch_insn *code = arg->code; 1869 long offset; 1870 char *tmp; 1871 char c; 1872 int ret = 0; 1873 1874 while (code && code->op != FETCH_OP_END) { 1875 if (code->op == FETCH_NOP_SYMBOL) { 1876 if (code[1].op != FETCH_OP_IMM) 1877 return -EINVAL; 1878 1879 tmp = strpbrk(code->data, "+-"); 1880 if (tmp) 1881 c = *tmp; 1882 ret = traceprobe_split_symbol_offset(code->data, 1883 &offset); 1884 if (ret) 1885 return ret; 1886 1887 code[1].immediate = 1888 (unsigned long)kallsyms_lookup_name(code->data); 1889 if (tmp) 1890 *tmp = c; 1891 if (!code[1].immediate) 1892 return -ENOENT; 1893 code[1].immediate += offset; 1894 } 1895 code++; 1896 } 1897 return 0; 1898 } 1899 1900 /* When len=0, we just calculate the needed length */ 1901 #define LEN_OR_ZERO (len ? len - pos : 0) 1902 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, 1903 enum probe_print_type ptype) 1904 { 1905 struct probe_arg *parg; 1906 int i, j; 1907 int pos = 0; 1908 const char *fmt, *arg; 1909 1910 switch (ptype) { 1911 case PROBE_PRINT_NORMAL: 1912 fmt = "(%lx)"; 1913 arg = ", REC->" FIELD_STRING_IP; 1914 break; 1915 case PROBE_PRINT_RETURN: 1916 fmt = "(%lx <- %lx)"; 1917 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; 1918 break; 1919 case PROBE_PRINT_EVENT: 1920 fmt = ""; 1921 arg = ""; 1922 break; 1923 default: 1924 WARN_ON_ONCE(1); 1925 return 0; 1926 } 1927 1928 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); 1929 1930 for (i = 0; i < tp->nr_args; i++) { 1931 parg = tp->args + i; 1932 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); 1933 if (parg->count) { 1934 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", 1935 parg->type->fmt); 1936 for (j = 1; j < parg->count; j++) 1937 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", 1938 parg->type->fmt); 1939 pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); 1940 } else 1941 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", 1942 parg->type->fmt); 1943 } 1944 1945 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg); 1946 1947 for (i = 0; i < tp->nr_args; i++) { 1948 parg = tp->args + i; 1949 if (parg->count) { 1950 if (parg->type->is_string) 1951 fmt = ", __get_str(%s[%d])"; 1952 else 1953 fmt = ", REC->%s[%d]"; 1954 for (j = 0; j < parg->count; j++) 1955 pos += snprintf(buf + pos, LEN_OR_ZERO, 1956 fmt, parg->name, j); 1957 } else { 1958 if (parg->type->is_string) 1959 fmt = ", __get_str(%s)"; 1960 else 1961 fmt = ", REC->%s"; 1962 pos += snprintf(buf + pos, LEN_OR_ZERO, 1963 fmt, parg->name); 1964 } 1965 } 1966 1967 /* return the length of print_fmt */ 1968 return pos; 1969 } 1970 #undef LEN_OR_ZERO 1971 1972 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype) 1973 { 1974 struct trace_event_call *call = trace_probe_event_call(tp); 1975 int len; 1976 char *print_fmt; 1977 1978 /* First: called with 0 length to calculate the needed length */ 1979 len = __set_print_fmt(tp, NULL, 0, ptype); 1980 print_fmt = kmalloc(len + 1, GFP_KERNEL); 1981 if (!print_fmt) 1982 return -ENOMEM; 1983 1984 /* Second: actually write the @print_fmt */ 1985 __set_print_fmt(tp, print_fmt, len + 1, ptype); 1986 call->print_fmt = print_fmt; 1987 1988 return 0; 1989 } 1990 1991 int traceprobe_define_arg_fields(struct trace_event_call *event_call, 1992 size_t offset, struct trace_probe *tp) 1993 { 1994 int ret, i; 1995 1996 /* Set argument names as fields */ 1997 for (i = 0; i < tp->nr_args; i++) { 1998 struct probe_arg *parg = &tp->args[i]; 1999 const char *fmt = parg->type->fmttype; 2000 int size = parg->type->size; 2001 2002 if (parg->fmt) 2003 fmt = parg->fmt; 2004 if (parg->count) 2005 size *= parg->count; 2006 ret = trace_define_field(event_call, fmt, parg->name, 2007 offset + parg->offset, size, 2008 parg->type->is_signed, 2009 FILTER_OTHER); 2010 if (ret) 2011 return ret; 2012 } 2013 return 0; 2014 } 2015 2016 static void trace_probe_event_free(struct trace_probe_event *tpe) 2017 { 2018 kfree(tpe->class.system); 2019 kfree(tpe->call.name); 2020 kfree(tpe->call.print_fmt); 2021 kfree(tpe); 2022 } 2023 2024 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to) 2025 { 2026 if (trace_probe_has_sibling(tp)) 2027 return -EBUSY; 2028 2029 list_del_init(&tp->list); 2030 trace_probe_event_free(tp->event); 2031 2032 tp->event = to->event; 2033 list_add_tail(&tp->list, trace_probe_probe_list(to)); 2034 2035 return 0; 2036 } 2037 2038 void trace_probe_unlink(struct trace_probe *tp) 2039 { 2040 list_del_init(&tp->list); 2041 if (list_empty(trace_probe_probe_list(tp))) 2042 trace_probe_event_free(tp->event); 2043 tp->event = NULL; 2044 } 2045 2046 void trace_probe_cleanup(struct trace_probe *tp) 2047 { 2048 int i; 2049 2050 for (i = 0; i < tp->nr_args; i++) 2051 traceprobe_free_probe_arg(&tp->args[i]); 2052 2053 if (tp->entry_arg) { 2054 kfree(tp->entry_arg->code); 2055 kfree(tp->entry_arg); 2056 tp->entry_arg = NULL; 2057 } 2058 2059 if (tp->event) 2060 trace_probe_unlink(tp); 2061 } 2062 2063 int trace_probe_init(struct trace_probe *tp, const char *event, 2064 const char *group, bool alloc_filter, int nargs) 2065 { 2066 struct trace_event_call *call; 2067 size_t size = sizeof(struct trace_probe_event); 2068 int ret = 0; 2069 2070 if (!event || !group) 2071 return -EINVAL; 2072 2073 if (alloc_filter) 2074 size += sizeof(struct trace_uprobe_filter); 2075 2076 tp->event = kzalloc(size, GFP_KERNEL); 2077 if (!tp->event) 2078 return -ENOMEM; 2079 2080 INIT_LIST_HEAD(&tp->event->files); 2081 INIT_LIST_HEAD(&tp->event->class.fields); 2082 INIT_LIST_HEAD(&tp->event->probes); 2083 INIT_LIST_HEAD(&tp->list); 2084 list_add(&tp->list, &tp->event->probes); 2085 2086 call = trace_probe_event_call(tp); 2087 call->class = &tp->event->class; 2088 call->name = kstrdup(event, GFP_KERNEL); 2089 if (!call->name) { 2090 ret = -ENOMEM; 2091 goto error; 2092 } 2093 2094 tp->event->class.system = kstrdup(group, GFP_KERNEL); 2095 if (!tp->event->class.system) { 2096 ret = -ENOMEM; 2097 goto error; 2098 } 2099 2100 tp->nr_args = nargs; 2101 /* Make sure pointers in args[] are NULL */ 2102 if (nargs) 2103 memset(tp->args, 0, sizeof(tp->args[0]) * nargs); 2104 2105 return 0; 2106 2107 error: 2108 trace_probe_cleanup(tp); 2109 return ret; 2110 } 2111 2112 static struct trace_event_call * 2113 find_trace_event_call(const char *system, const char *event_name) 2114 { 2115 struct trace_event_call *tp_event; 2116 const char *name; 2117 2118 list_for_each_entry(tp_event, &ftrace_events, list) { 2119 if (!tp_event->class->system || 2120 strcmp(system, tp_event->class->system)) 2121 continue; 2122 name = trace_event_name(tp_event); 2123 if (!name || strcmp(event_name, name)) 2124 continue; 2125 return tp_event; 2126 } 2127 2128 return NULL; 2129 } 2130 2131 int trace_probe_register_event_call(struct trace_probe *tp) 2132 { 2133 struct trace_event_call *call = trace_probe_event_call(tp); 2134 int ret; 2135 2136 lockdep_assert_held(&event_mutex); 2137 2138 if (find_trace_event_call(trace_probe_group_name(tp), 2139 trace_probe_name(tp))) 2140 return -EEXIST; 2141 2142 ret = register_trace_event(&call->event); 2143 if (!ret) 2144 return -ENODEV; 2145 2146 ret = trace_add_event_call(call); 2147 if (ret) 2148 unregister_trace_event(&call->event); 2149 2150 return ret; 2151 } 2152 2153 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) 2154 { 2155 struct event_file_link *link; 2156 2157 link = kmalloc_obj(*link); 2158 if (!link) 2159 return -ENOMEM; 2160 2161 link->file = file; 2162 INIT_LIST_HEAD(&link->list); 2163 list_add_tail_rcu(&link->list, &tp->event->files); 2164 trace_probe_set_flag(tp, TP_FLAG_TRACE); 2165 return 0; 2166 } 2167 2168 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, 2169 struct trace_event_file *file) 2170 { 2171 struct event_file_link *link; 2172 2173 trace_probe_for_each_link(link, tp) { 2174 if (link->file == file) 2175 return link; 2176 } 2177 2178 return NULL; 2179 } 2180 2181 int trace_probe_remove_file(struct trace_probe *tp, 2182 struct trace_event_file *file) 2183 { 2184 struct event_file_link *link; 2185 2186 link = trace_probe_get_file_link(tp, file); 2187 if (!link) 2188 return -ENOENT; 2189 2190 list_del_rcu(&link->list); 2191 kvfree_rcu_mightsleep(link); 2192 2193 if (list_empty(&tp->event->files)) 2194 trace_probe_clear_flag(tp, TP_FLAG_TRACE); 2195 2196 return 0; 2197 } 2198 2199 /* 2200 * Return the smallest index of different type argument (start from 1). 2201 * If all argument types and name are same, return 0. 2202 */ 2203 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b) 2204 { 2205 int i; 2206 2207 /* In case of more arguments */ 2208 if (a->nr_args < b->nr_args) 2209 return a->nr_args + 1; 2210 if (a->nr_args > b->nr_args) 2211 return b->nr_args + 1; 2212 2213 for (i = 0; i < a->nr_args; i++) { 2214 if ((b->nr_args <= i) || 2215 ((a->args[i].type != b->args[i].type) || 2216 (a->args[i].count != b->args[i].count) || 2217 strcmp(a->args[i].name, b->args[i].name))) 2218 return i + 1; 2219 } 2220 2221 return 0; 2222 } 2223 2224 bool trace_probe_match_command_args(struct trace_probe *tp, 2225 int argc, const char **argv) 2226 { 2227 char buf[MAX_ARGSTR_LEN + 1]; 2228 int i; 2229 2230 if (tp->nr_args < argc) 2231 return false; 2232 2233 for (i = 0; i < argc; i++) { 2234 snprintf(buf, sizeof(buf), "%s=%s", 2235 tp->args[i].name, tp->args[i].comm); 2236 if (strcmp(buf, argv[i])) 2237 return false; 2238 } 2239 return true; 2240 } 2241 2242 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **)) 2243 { 2244 int argc = 0, ret = 0; 2245 char **argv; 2246 2247 argv = argv_split(GFP_KERNEL, raw_command, &argc); 2248 if (!argv) 2249 return -ENOMEM; 2250 2251 if (argc) 2252 ret = createfn(argc, (const char **)argv); 2253 2254 argv_free(argv); 2255 2256 return ret; 2257 } 2258 2259 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args, 2260 u8 *data, void *field) 2261 { 2262 void *p; 2263 int i, j; 2264 2265 for (i = 0; i < nr_args; i++) { 2266 struct probe_arg *a = args + i; 2267 2268 trace_seq_printf(s, " %s=", a->name); 2269 if (likely(!a->count)) { 2270 if (!a->type->print(s, data + a->offset, field)) 2271 return -ENOMEM; 2272 continue; 2273 } 2274 trace_seq_putc(s, '{'); 2275 p = data + a->offset; 2276 for (j = 0; j < a->count; j++) { 2277 if (!a->type->print(s, p, field)) 2278 return -ENOMEM; 2279 trace_seq_putc(s, j == a->count - 1 ? '}' : ','); 2280 p += a->type->size; 2281 } 2282 } 2283 return 0; 2284 } 2285