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