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 (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 (parg->count) { 1527 len = strlen(parg->type->fmttype) + 6; 1528 parg->fmt = kmalloc(len, GFP_KERNEL); 1529 if (!parg->fmt) { 1530 ret = -ENOMEM; 1531 goto fail; 1532 } 1533 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, 1534 parg->count); 1535 } 1536 1537 ret = finalize_fetch_insn(code, parg, type, type ? type - arg : 0, ctx); 1538 if (ret < 0) 1539 goto fail; 1540 1541 for (; code < tmp + FETCH_INSN_MAX; code++) 1542 if (code->op == FETCH_OP_END) 1543 break; 1544 /* Shrink down the code buffer */ 1545 parg->code = kzalloc_objs(*code, code - tmp + 1); 1546 if (!parg->code) 1547 ret = -ENOMEM; 1548 else 1549 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); 1550 1551 fail: 1552 if (ret < 0) { 1553 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) 1554 if (code->op == FETCH_NOP_SYMBOL || 1555 code->op == FETCH_OP_DATA) 1556 kfree(code->data); 1557 } 1558 kfree(tmp); 1559 1560 return ret; 1561 } 1562 1563 /* Return 1 if name is reserved or already used by another argument */ 1564 static int traceprobe_conflict_field_name(const char *name, 1565 struct probe_arg *args, int narg) 1566 { 1567 int i; 1568 1569 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) 1570 if (strcmp(reserved_field_names[i], name) == 0) 1571 return 1; 1572 1573 for (i = 0; i < narg; i++) 1574 if (strcmp(args[i].name, name) == 0) 1575 return 1; 1576 1577 return 0; 1578 } 1579 1580 static char *generate_probe_arg_name(const char *arg, int idx) 1581 { 1582 char *name = NULL; 1583 const char *end; 1584 1585 /* 1586 * If argument name is omitted, try arg as a name (BTF variable) 1587 * or "argN". 1588 */ 1589 if (IS_ENABLED(CONFIG_PROBE_EVENTS_BTF_ARGS)) { 1590 end = strchr(arg, ':'); 1591 if (!end) 1592 end = arg + strlen(arg); 1593 1594 name = kmemdup_nul(arg, end - arg, GFP_KERNEL); 1595 if (!name || !is_good_name(name)) { 1596 kfree(name); 1597 name = NULL; 1598 } 1599 } 1600 1601 if (!name) 1602 name = kasprintf(GFP_KERNEL, "arg%d", idx + 1); 1603 1604 return name; 1605 } 1606 1607 int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg, 1608 struct traceprobe_parse_context *ctx) 1609 { 1610 struct probe_arg *parg = &tp->args[i]; 1611 const char *body; 1612 1613 ctx->tp = tp; 1614 body = strchr(arg, '='); 1615 if (body) { 1616 if (body - arg > MAX_ARG_NAME_LEN) { 1617 trace_probe_log_err(0, ARG_NAME_TOO_LONG); 1618 return -EINVAL; 1619 } else if (body == arg) { 1620 trace_probe_log_err(0, NO_ARG_NAME); 1621 return -EINVAL; 1622 } 1623 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); 1624 body++; 1625 } else { 1626 parg->name = generate_probe_arg_name(arg, i); 1627 body = arg; 1628 } 1629 if (!parg->name) 1630 return -ENOMEM; 1631 1632 if (!is_good_name(parg->name)) { 1633 trace_probe_log_err(0, BAD_ARG_NAME); 1634 return -EINVAL; 1635 } 1636 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { 1637 trace_probe_log_err(0, USED_ARG_NAME); 1638 return -EINVAL; 1639 } 1640 ctx->offset = body - arg; 1641 /* Parse fetch argument */ 1642 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, ctx); 1643 } 1644 1645 void traceprobe_free_probe_arg(struct probe_arg *arg) 1646 { 1647 struct fetch_insn *code = arg->code; 1648 1649 while (code && code->op != FETCH_OP_END) { 1650 if (code->op == FETCH_NOP_SYMBOL || 1651 code->op == FETCH_OP_DATA) 1652 kfree(code->data); 1653 code++; 1654 } 1655 kfree(arg->code); 1656 kfree(arg->name); 1657 kfree(arg->comm); 1658 kfree(arg->fmt); 1659 } 1660 1661 static int argv_has_var_arg(int argc, const char *argv[], int *args_idx, 1662 struct traceprobe_parse_context *ctx) 1663 { 1664 int i, found = 0; 1665 1666 for (i = 0; i < argc; i++) 1667 if (str_has_prefix(argv[i], "$arg")) { 1668 trace_probe_log_set_index(i + 2); 1669 1670 if (!tparg_is_function_entry(ctx->flags) && 1671 !tparg_is_function_return(ctx->flags)) { 1672 trace_probe_log_err(0, NOFENTRY_ARGS); 1673 return -EINVAL; 1674 } 1675 1676 if (isdigit(argv[i][4])) { 1677 found = 1; 1678 continue; 1679 } 1680 1681 if (argv[i][4] != '*') { 1682 trace_probe_log_err(0, BAD_VAR); 1683 return -EINVAL; 1684 } 1685 1686 if (*args_idx >= 0 && *args_idx < argc) { 1687 trace_probe_log_err(0, DOUBLE_ARGS); 1688 return -EINVAL; 1689 } 1690 found = 1; 1691 *args_idx = i; 1692 } 1693 1694 return found; 1695 } 1696 1697 static int sprint_nth_btf_arg(int idx, const char *type, 1698 char *buf, int bufsize, 1699 struct traceprobe_parse_context *ctx) 1700 { 1701 const char *name; 1702 int ret; 1703 1704 if (idx >= ctx->nr_params) { 1705 trace_probe_log_err(0, NO_BTFARG); 1706 return -ENOENT; 1707 } 1708 name = btf_name_by_offset(ctx->btf, ctx->params[idx].name_off); 1709 if (!name) { 1710 trace_probe_log_err(0, NO_BTF_ENTRY); 1711 return -ENOENT; 1712 } 1713 ret = snprintf(buf, bufsize, "%s%s", name, type); 1714 if (ret >= bufsize) { 1715 trace_probe_log_err(0, ARGS_2LONG); 1716 return -E2BIG; 1717 } 1718 return ret; 1719 } 1720 1721 /* Return new_argv which must be freed after use */ 1722 const char **traceprobe_expand_meta_args(int argc, const char *argv[], 1723 int *new_argc, char *buf, int bufsize, 1724 struct traceprobe_parse_context *ctx) 1725 { 1726 const struct btf_param *params = NULL; 1727 int i, j, n, used, ret, args_idx = -1; 1728 const char **new_argv __free(kfree) = NULL; 1729 1730 ret = argv_has_var_arg(argc, argv, &args_idx, ctx); 1731 if (ret < 0) 1732 return ERR_PTR(ret); 1733 1734 if (!ret) { 1735 *new_argc = argc; 1736 return NULL; 1737 } 1738 1739 ret = query_btf_context(ctx); 1740 if (ret < 0 || ctx->nr_params == 0) { 1741 if (args_idx != -1) { 1742 /* $arg* requires BTF info */ 1743 trace_probe_log_err(0, NOSUP_BTFARG); 1744 return (const char **)params; 1745 } 1746 *new_argc = argc; 1747 return NULL; 1748 } 1749 1750 if (args_idx >= 0) 1751 *new_argc = argc + ctx->nr_params - 1; 1752 else 1753 *new_argc = argc; 1754 1755 new_argv = kcalloc(*new_argc, sizeof(char *), GFP_KERNEL); 1756 if (!new_argv) 1757 return ERR_PTR(-ENOMEM); 1758 1759 used = 0; 1760 for (i = 0, j = 0; i < argc; i++) { 1761 trace_probe_log_set_index(i + 2); 1762 if (i == args_idx) { 1763 for (n = 0; n < ctx->nr_params; n++) { 1764 ret = sprint_nth_btf_arg(n, "", buf + used, 1765 bufsize - used, ctx); 1766 if (ret < 0) 1767 return ERR_PTR(ret); 1768 1769 new_argv[j++] = buf + used; 1770 used += ret + 1; 1771 } 1772 continue; 1773 } 1774 1775 if (str_has_prefix(argv[i], "$arg")) { 1776 char *type = NULL; 1777 1778 n = simple_strtoul(argv[i] + 4, &type, 10); 1779 if (type && !(*type == ':' || *type == '\0')) { 1780 trace_probe_log_err(0, BAD_VAR); 1781 return ERR_PTR(-ENOENT); 1782 } 1783 /* Note: $argN starts from $arg1 */ 1784 ret = sprint_nth_btf_arg(n - 1, type, buf + used, 1785 bufsize - used, ctx); 1786 if (ret < 0) 1787 return ERR_PTR(ret); 1788 new_argv[j++] = buf + used; 1789 used += ret + 1; 1790 } else 1791 new_argv[j++] = argv[i]; 1792 } 1793 1794 return_ptr(new_argv); 1795 } 1796 1797 /* @buf: *buf must be equal to NULL. Caller must to free *buf */ 1798 int traceprobe_expand_dentry_args(int argc, const char *argv[], char **buf) 1799 { 1800 int i, used, ret; 1801 const int bufsize = MAX_DENTRY_ARGS_LEN; 1802 char *tmpbuf __free(kfree) = NULL; 1803 1804 if (*buf) 1805 return -EINVAL; 1806 1807 used = 0; 1808 for (i = 0; i < argc; i++) { 1809 char *tmp __free(kfree) = NULL; 1810 char *equal; 1811 size_t arg_len; 1812 1813 if (!glob_match("*:%p[dD]", argv[i])) 1814 continue; 1815 1816 if (!tmpbuf) { 1817 tmpbuf = kmalloc(bufsize, GFP_KERNEL); 1818 if (!tmpbuf) 1819 return -ENOMEM; 1820 } 1821 1822 tmp = kstrdup(argv[i], GFP_KERNEL); 1823 if (!tmp) 1824 return -ENOMEM; 1825 1826 equal = strchr(tmp, '='); 1827 if (equal) 1828 *equal = '\0'; 1829 arg_len = strlen(argv[i]); 1830 tmp[arg_len - 4] = '\0'; 1831 if (argv[i][arg_len - 1] == 'd') 1832 ret = snprintf(tmpbuf + used, bufsize - used, 1833 "%s%s+0x0(+0x%zx(%s)):string", 1834 equal ? tmp : "", equal ? "=" : "", 1835 offsetof(struct dentry, d_name.name), 1836 equal ? equal + 1 : tmp); 1837 else 1838 ret = snprintf(tmpbuf + used, bufsize - used, 1839 "%s%s+0x0(+0x%zx(+0x%zx(%s))):string", 1840 equal ? tmp : "", equal ? "=" : "", 1841 offsetof(struct dentry, d_name.name), 1842 offsetof(struct file, f_path.dentry), 1843 equal ? equal + 1 : tmp); 1844 1845 if (ret >= bufsize - used) 1846 return -ENOMEM; 1847 argv[i] = tmpbuf + used; 1848 used += ret + 1; 1849 } 1850 1851 *buf = no_free_ptr(tmpbuf); 1852 return 0; 1853 } 1854 1855 void traceprobe_finish_parse(struct traceprobe_parse_context *ctx) 1856 { 1857 clear_btf_context(ctx); 1858 } 1859 1860 int traceprobe_update_arg(struct probe_arg *arg) 1861 { 1862 struct fetch_insn *code = arg->code; 1863 long offset; 1864 char *tmp; 1865 char c; 1866 int ret = 0; 1867 1868 while (code && code->op != FETCH_OP_END) { 1869 if (code->op == FETCH_NOP_SYMBOL) { 1870 if (code[1].op != FETCH_OP_IMM) 1871 return -EINVAL; 1872 1873 tmp = strpbrk(code->data, "+-"); 1874 if (tmp) 1875 c = *tmp; 1876 ret = traceprobe_split_symbol_offset(code->data, 1877 &offset); 1878 if (ret) 1879 return ret; 1880 1881 code[1].immediate = 1882 (unsigned long)kallsyms_lookup_name(code->data); 1883 if (tmp) 1884 *tmp = c; 1885 if (!code[1].immediate) 1886 return -ENOENT; 1887 code[1].immediate += offset; 1888 } 1889 code++; 1890 } 1891 return 0; 1892 } 1893 1894 /* When len=0, we just calculate the needed length */ 1895 #define LEN_OR_ZERO (len ? len - pos : 0) 1896 static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, 1897 enum probe_print_type ptype) 1898 { 1899 struct probe_arg *parg; 1900 int i, j; 1901 int pos = 0; 1902 const char *fmt, *arg; 1903 1904 switch (ptype) { 1905 case PROBE_PRINT_NORMAL: 1906 fmt = "(%lx)"; 1907 arg = ", REC->" FIELD_STRING_IP; 1908 break; 1909 case PROBE_PRINT_RETURN: 1910 fmt = "(%lx <- %lx)"; 1911 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; 1912 break; 1913 case PROBE_PRINT_EVENT: 1914 fmt = ""; 1915 arg = ""; 1916 break; 1917 default: 1918 WARN_ON_ONCE(1); 1919 return 0; 1920 } 1921 1922 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); 1923 1924 for (i = 0; i < tp->nr_args; i++) { 1925 parg = tp->args + i; 1926 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); 1927 if (parg->count) { 1928 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", 1929 parg->type->fmt); 1930 for (j = 1; j < parg->count; j++) 1931 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", 1932 parg->type->fmt); 1933 pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); 1934 } else 1935 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", 1936 parg->type->fmt); 1937 } 1938 1939 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg); 1940 1941 for (i = 0; i < tp->nr_args; i++) { 1942 parg = tp->args + i; 1943 if (parg->count) { 1944 if (parg->type->is_string) 1945 fmt = ", __get_str(%s[%d])"; 1946 else 1947 fmt = ", REC->%s[%d]"; 1948 for (j = 0; j < parg->count; j++) 1949 pos += snprintf(buf + pos, LEN_OR_ZERO, 1950 fmt, parg->name, j); 1951 } else { 1952 if (parg->type->is_string) 1953 fmt = ", __get_str(%s)"; 1954 else 1955 fmt = ", REC->%s"; 1956 pos += snprintf(buf + pos, LEN_OR_ZERO, 1957 fmt, parg->name); 1958 } 1959 } 1960 1961 /* return the length of print_fmt */ 1962 return pos; 1963 } 1964 #undef LEN_OR_ZERO 1965 1966 int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype) 1967 { 1968 struct trace_event_call *call = trace_probe_event_call(tp); 1969 int len; 1970 char *print_fmt; 1971 1972 /* First: called with 0 length to calculate the needed length */ 1973 len = __set_print_fmt(tp, NULL, 0, ptype); 1974 print_fmt = kmalloc(len + 1, GFP_KERNEL); 1975 if (!print_fmt) 1976 return -ENOMEM; 1977 1978 /* Second: actually write the @print_fmt */ 1979 __set_print_fmt(tp, print_fmt, len + 1, ptype); 1980 call->print_fmt = print_fmt; 1981 1982 return 0; 1983 } 1984 1985 int traceprobe_define_arg_fields(struct trace_event_call *event_call, 1986 size_t offset, struct trace_probe *tp) 1987 { 1988 int ret, i; 1989 1990 /* Set argument names as fields */ 1991 for (i = 0; i < tp->nr_args; i++) { 1992 struct probe_arg *parg = &tp->args[i]; 1993 const char *fmt = parg->type->fmttype; 1994 int size = parg->type->size; 1995 1996 if (parg->fmt) 1997 fmt = parg->fmt; 1998 if (parg->count) 1999 size *= parg->count; 2000 ret = trace_define_field(event_call, fmt, parg->name, 2001 offset + parg->offset, size, 2002 parg->type->is_signed, 2003 FILTER_OTHER); 2004 if (ret) 2005 return ret; 2006 } 2007 return 0; 2008 } 2009 2010 static void trace_probe_event_free(struct trace_probe_event *tpe) 2011 { 2012 kfree(tpe->class.system); 2013 kfree(tpe->call.name); 2014 kfree(tpe->call.print_fmt); 2015 kfree(tpe); 2016 } 2017 2018 int trace_probe_append(struct trace_probe *tp, struct trace_probe *to) 2019 { 2020 if (trace_probe_has_sibling(tp)) 2021 return -EBUSY; 2022 2023 list_del_init(&tp->list); 2024 trace_probe_event_free(tp->event); 2025 2026 tp->event = to->event; 2027 list_add_tail(&tp->list, trace_probe_probe_list(to)); 2028 2029 return 0; 2030 } 2031 2032 void trace_probe_unlink(struct trace_probe *tp) 2033 { 2034 list_del_init(&tp->list); 2035 if (list_empty(trace_probe_probe_list(tp))) 2036 trace_probe_event_free(tp->event); 2037 tp->event = NULL; 2038 } 2039 2040 void trace_probe_cleanup(struct trace_probe *tp) 2041 { 2042 int i; 2043 2044 for (i = 0; i < tp->nr_args; i++) 2045 traceprobe_free_probe_arg(&tp->args[i]); 2046 2047 if (tp->entry_arg) { 2048 kfree(tp->entry_arg->code); 2049 kfree(tp->entry_arg); 2050 tp->entry_arg = NULL; 2051 } 2052 2053 if (tp->event) 2054 trace_probe_unlink(tp); 2055 } 2056 2057 int trace_probe_init(struct trace_probe *tp, const char *event, 2058 const char *group, bool alloc_filter, int nargs) 2059 { 2060 struct trace_event_call *call; 2061 size_t size = sizeof(struct trace_probe_event); 2062 int ret = 0; 2063 2064 if (!event || !group) 2065 return -EINVAL; 2066 2067 if (alloc_filter) 2068 size += sizeof(struct trace_uprobe_filter); 2069 2070 tp->event = kzalloc(size, GFP_KERNEL); 2071 if (!tp->event) 2072 return -ENOMEM; 2073 2074 INIT_LIST_HEAD(&tp->event->files); 2075 INIT_LIST_HEAD(&tp->event->class.fields); 2076 INIT_LIST_HEAD(&tp->event->probes); 2077 INIT_LIST_HEAD(&tp->list); 2078 list_add(&tp->list, &tp->event->probes); 2079 2080 call = trace_probe_event_call(tp); 2081 call->class = &tp->event->class; 2082 call->name = kstrdup(event, GFP_KERNEL); 2083 if (!call->name) { 2084 ret = -ENOMEM; 2085 goto error; 2086 } 2087 2088 tp->event->class.system = kstrdup(group, GFP_KERNEL); 2089 if (!tp->event->class.system) { 2090 ret = -ENOMEM; 2091 goto error; 2092 } 2093 2094 tp->nr_args = nargs; 2095 /* Make sure pointers in args[] are NULL */ 2096 if (nargs) 2097 memset(tp->args, 0, sizeof(tp->args[0]) * nargs); 2098 2099 return 0; 2100 2101 error: 2102 trace_probe_cleanup(tp); 2103 return ret; 2104 } 2105 2106 static struct trace_event_call * 2107 find_trace_event_call(const char *system, const char *event_name) 2108 { 2109 struct trace_event_call *tp_event; 2110 const char *name; 2111 2112 list_for_each_entry(tp_event, &ftrace_events, list) { 2113 if (!tp_event->class->system || 2114 strcmp(system, tp_event->class->system)) 2115 continue; 2116 name = trace_event_name(tp_event); 2117 if (!name || strcmp(event_name, name)) 2118 continue; 2119 return tp_event; 2120 } 2121 2122 return NULL; 2123 } 2124 2125 int trace_probe_register_event_call(struct trace_probe *tp) 2126 { 2127 struct trace_event_call *call = trace_probe_event_call(tp); 2128 int ret; 2129 2130 lockdep_assert_held(&event_mutex); 2131 2132 if (find_trace_event_call(trace_probe_group_name(tp), 2133 trace_probe_name(tp))) 2134 return -EEXIST; 2135 2136 ret = register_trace_event(&call->event); 2137 if (!ret) 2138 return -ENODEV; 2139 2140 ret = trace_add_event_call(call); 2141 if (ret) 2142 unregister_trace_event(&call->event); 2143 2144 return ret; 2145 } 2146 2147 int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) 2148 { 2149 struct event_file_link *link; 2150 2151 link = kmalloc_obj(*link); 2152 if (!link) 2153 return -ENOMEM; 2154 2155 link->file = file; 2156 INIT_LIST_HEAD(&link->list); 2157 list_add_tail_rcu(&link->list, &tp->event->files); 2158 trace_probe_set_flag(tp, TP_FLAG_TRACE); 2159 return 0; 2160 } 2161 2162 struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, 2163 struct trace_event_file *file) 2164 { 2165 struct event_file_link *link; 2166 2167 trace_probe_for_each_link(link, tp) { 2168 if (link->file == file) 2169 return link; 2170 } 2171 2172 return NULL; 2173 } 2174 2175 int trace_probe_remove_file(struct trace_probe *tp, 2176 struct trace_event_file *file) 2177 { 2178 struct event_file_link *link; 2179 2180 link = trace_probe_get_file_link(tp, file); 2181 if (!link) 2182 return -ENOENT; 2183 2184 list_del_rcu(&link->list); 2185 kvfree_rcu_mightsleep(link); 2186 2187 if (list_empty(&tp->event->files)) 2188 trace_probe_clear_flag(tp, TP_FLAG_TRACE); 2189 2190 return 0; 2191 } 2192 2193 /* 2194 * Return the smallest index of different type argument (start from 1). 2195 * If all argument types and name are same, return 0. 2196 */ 2197 int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b) 2198 { 2199 int i; 2200 2201 /* In case of more arguments */ 2202 if (a->nr_args < b->nr_args) 2203 return a->nr_args + 1; 2204 if (a->nr_args > b->nr_args) 2205 return b->nr_args + 1; 2206 2207 for (i = 0; i < a->nr_args; i++) { 2208 if ((b->nr_args <= i) || 2209 ((a->args[i].type != b->args[i].type) || 2210 (a->args[i].count != b->args[i].count) || 2211 strcmp(a->args[i].name, b->args[i].name))) 2212 return i + 1; 2213 } 2214 2215 return 0; 2216 } 2217 2218 bool trace_probe_match_command_args(struct trace_probe *tp, 2219 int argc, const char **argv) 2220 { 2221 char buf[MAX_ARGSTR_LEN + 1]; 2222 int i; 2223 2224 if (tp->nr_args < argc) 2225 return false; 2226 2227 for (i = 0; i < argc; i++) { 2228 snprintf(buf, sizeof(buf), "%s=%s", 2229 tp->args[i].name, tp->args[i].comm); 2230 if (strcmp(buf, argv[i])) 2231 return false; 2232 } 2233 return true; 2234 } 2235 2236 int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **)) 2237 { 2238 int argc = 0, ret = 0; 2239 char **argv; 2240 2241 argv = argv_split(GFP_KERNEL, raw_command, &argc); 2242 if (!argv) 2243 return -ENOMEM; 2244 2245 if (argc) 2246 ret = createfn(argc, (const char **)argv); 2247 2248 argv_free(argv); 2249 2250 return ret; 2251 } 2252 2253 int trace_probe_print_args(struct trace_seq *s, struct probe_arg *args, int nr_args, 2254 u8 *data, void *field) 2255 { 2256 void *p; 2257 int i, j; 2258 2259 for (i = 0; i < nr_args; i++) { 2260 struct probe_arg *a = args + i; 2261 2262 trace_seq_printf(s, " %s=", a->name); 2263 if (likely(!a->count)) { 2264 if (!a->type->print(s, data + a->offset, field)) 2265 return -ENOMEM; 2266 continue; 2267 } 2268 trace_seq_putc(s, '{'); 2269 p = data + a->offset; 2270 for (j = 0; j < a->count; j++) { 2271 if (!a->type->print(s, p, field)) 2272 return -ENOMEM; 2273 trace_seq_putc(s, j == a->count - 1 ? '}' : ','); 2274 p += a->type->size; 2275 } 2276 } 2277 return 0; 2278 } 2279