1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * event probes 4 * 5 * Part of this code was copied from kernel/trace/trace_kprobe.c written by 6 * Masami Hiramatsu <mhiramat@kernel.org> 7 * 8 * Copyright (C) 2021, VMware Inc, Steven Rostedt <rostedt@goodmis.org> 9 * Copyright (C) 2021, VMware Inc, Tzvetomir Stoyanov tz.stoyanov@gmail.com> 10 * 11 */ 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/ftrace.h> 15 16 #include "trace_dynevent.h" 17 #include "trace_probe.h" 18 #include "trace_probe_tmpl.h" 19 #include "trace_probe_kernel.h" 20 21 #define EPROBE_EVENT_SYSTEM "eprobes" 22 23 struct trace_eprobe { 24 /* tracepoint system */ 25 const char *event_system; 26 27 /* tracepoint event */ 28 const char *event_name; 29 30 /* filter string for the tracepoint */ 31 char *filter_str; 32 33 struct trace_event_call *event; 34 35 struct dyn_event devent; 36 struct trace_probe tp; 37 }; 38 39 struct eprobe_data { 40 struct trace_event_file *file; 41 struct trace_eprobe *ep; 42 }; 43 44 static int __trace_eprobe_create(int argc, const char *argv[]); 45 46 static void trace_event_probe_cleanup(struct trace_eprobe *ep) 47 { 48 if (!ep) 49 return; 50 trace_probe_cleanup(&ep->tp); 51 kfree(ep->event_name); 52 kfree(ep->event_system); 53 if (ep->event) 54 trace_event_put_ref(ep->event); 55 kfree(ep->filter_str); 56 kfree(ep); 57 } 58 59 static struct trace_eprobe *to_trace_eprobe(struct dyn_event *ev) 60 { 61 return container_of(ev, struct trace_eprobe, devent); 62 } 63 64 static int eprobe_dyn_event_create(const char *raw_command) 65 { 66 return trace_probe_create(raw_command, __trace_eprobe_create); 67 } 68 69 static int eprobe_dyn_event_show(struct seq_file *m, struct dyn_event *ev) 70 { 71 struct trace_eprobe *ep = to_trace_eprobe(ev); 72 int i; 73 74 seq_printf(m, "e:%s/%s", trace_probe_group_name(&ep->tp), 75 trace_probe_name(&ep->tp)); 76 seq_printf(m, " %s.%s", ep->event_system, ep->event_name); 77 78 for (i = 0; i < ep->tp.nr_args; i++) 79 seq_printf(m, " %s=%s", ep->tp.args[i].name, ep->tp.args[i].comm); 80 seq_putc(m, '\n'); 81 82 return 0; 83 } 84 85 static int unregister_trace_eprobe(struct trace_eprobe *ep) 86 { 87 /* If other probes are on the event, just unregister eprobe */ 88 if (trace_probe_has_sibling(&ep->tp)) 89 goto unreg; 90 91 /* Enabled event can not be unregistered */ 92 if (trace_probe_is_enabled(&ep->tp)) 93 return -EBUSY; 94 95 /* Will fail if probe is being used by ftrace or perf */ 96 if (trace_probe_unregister_event_call(&ep->tp)) 97 return -EBUSY; 98 99 unreg: 100 dyn_event_remove(&ep->devent); 101 trace_probe_unlink(&ep->tp); 102 103 return 0; 104 } 105 106 static int eprobe_dyn_event_release(struct dyn_event *ev) 107 { 108 struct trace_eprobe *ep = to_trace_eprobe(ev); 109 int ret = unregister_trace_eprobe(ep); 110 111 if (!ret) 112 trace_event_probe_cleanup(ep); 113 return ret; 114 } 115 116 static bool eprobe_dyn_event_is_busy(struct dyn_event *ev) 117 { 118 struct trace_eprobe *ep = to_trace_eprobe(ev); 119 120 return trace_probe_is_enabled(&ep->tp); 121 } 122 123 static bool eprobe_dyn_event_match(const char *system, const char *event, 124 int argc, const char **argv, struct dyn_event *ev) 125 { 126 struct trace_eprobe *ep = to_trace_eprobe(ev); 127 const char *slash; 128 129 /* 130 * We match the following: 131 * event only - match all eprobes with event name 132 * system and event only - match all system/event probes 133 * system only - match all system probes 134 * 135 * The below has the above satisfied with more arguments: 136 * 137 * attached system/event - If the arg has the system and event 138 * the probe is attached to, match 139 * probes with the attachment. 140 * 141 * If any more args are given, then it requires a full match. 142 */ 143 144 /* 145 * If system exists, but this probe is not part of that system 146 * do not match. 147 */ 148 if (system && strcmp(trace_probe_group_name(&ep->tp), system) != 0) 149 return false; 150 151 /* Must match the event name */ 152 if (event[0] != '\0' && strcmp(trace_probe_name(&ep->tp), event) != 0) 153 return false; 154 155 /* No arguments match all */ 156 if (argc < 1) 157 return true; 158 159 /* First argument is the system/event the probe is attached to */ 160 161 slash = strchr(argv[0], '/'); 162 if (!slash) 163 slash = strchr(argv[0], '.'); 164 if (!slash) 165 return false; 166 167 if (strncmp(ep->event_system, argv[0], slash - argv[0])) 168 return false; 169 if (strcmp(ep->event_name, slash + 1)) 170 return false; 171 172 argc--; 173 argv++; 174 175 /* If there are no other args, then match */ 176 if (argc < 1) 177 return true; 178 179 return trace_probe_match_command_args(&ep->tp, argc, argv); 180 } 181 182 static struct dyn_event_operations eprobe_dyn_event_ops = { 183 .create = eprobe_dyn_event_create, 184 .show = eprobe_dyn_event_show, 185 .is_busy = eprobe_dyn_event_is_busy, 186 .free = eprobe_dyn_event_release, 187 .match = eprobe_dyn_event_match, 188 }; 189 190 static struct trace_eprobe *alloc_event_probe(const char *group, 191 const char *this_event, 192 struct trace_event_call *event, 193 int nargs) 194 { 195 struct trace_eprobe *ep; 196 const char *event_name; 197 const char *sys_name; 198 int ret = -ENOMEM; 199 200 if (!event) 201 return ERR_PTR(-ENODEV); 202 203 sys_name = event->class->system; 204 event_name = trace_event_name(event); 205 206 ep = kzalloc(struct_size(ep, tp.args, nargs), GFP_KERNEL); 207 if (!ep) { 208 trace_event_put_ref(event); 209 goto error; 210 } 211 ep->event = event; 212 ep->event_name = kstrdup(event_name, GFP_KERNEL); 213 if (!ep->event_name) 214 goto error; 215 ep->event_system = kstrdup(sys_name, GFP_KERNEL); 216 if (!ep->event_system) 217 goto error; 218 219 ret = trace_probe_init(&ep->tp, this_event, group, false); 220 if (ret < 0) 221 goto error; 222 223 dyn_event_init(&ep->devent, &eprobe_dyn_event_ops); 224 return ep; 225 error: 226 trace_event_probe_cleanup(ep); 227 return ERR_PTR(ret); 228 } 229 230 static int eprobe_event_define_fields(struct trace_event_call *event_call) 231 { 232 struct eprobe_trace_entry_head field; 233 struct trace_probe *tp; 234 235 tp = trace_probe_primary_from_call(event_call); 236 if (WARN_ON_ONCE(!tp)) 237 return -ENOENT; 238 239 return traceprobe_define_arg_fields(event_call, sizeof(field), tp); 240 } 241 242 static struct trace_event_fields eprobe_fields_array[] = { 243 { .type = TRACE_FUNCTION_TYPE, 244 .define_fields = eprobe_event_define_fields }, 245 {} 246 }; 247 248 /* Event entry printers */ 249 static enum print_line_t 250 print_eprobe_event(struct trace_iterator *iter, int flags, 251 struct trace_event *event) 252 { 253 struct eprobe_trace_entry_head *field; 254 struct trace_event_call *pevent; 255 struct trace_event *probed_event; 256 struct trace_seq *s = &iter->seq; 257 struct trace_eprobe *ep; 258 struct trace_probe *tp; 259 unsigned int type; 260 261 field = (struct eprobe_trace_entry_head *)iter->ent; 262 tp = trace_probe_primary_from_call( 263 container_of(event, struct trace_event_call, event)); 264 if (WARN_ON_ONCE(!tp)) 265 goto out; 266 267 ep = container_of(tp, struct trace_eprobe, tp); 268 type = ep->event->event.type; 269 270 trace_seq_printf(s, "%s: (", trace_probe_name(tp)); 271 272 probed_event = ftrace_find_event(type); 273 if (probed_event) { 274 pevent = container_of(probed_event, struct trace_event_call, event); 275 trace_seq_printf(s, "%s.%s", pevent->class->system, 276 trace_event_name(pevent)); 277 } else { 278 trace_seq_printf(s, "%u", type); 279 } 280 281 trace_seq_putc(s, ')'); 282 283 if (trace_probe_print_args(s, tp->args, tp->nr_args, 284 (u8 *)&field[1], field) < 0) 285 goto out; 286 287 trace_seq_putc(s, '\n'); 288 out: 289 return trace_handle_return(s); 290 } 291 292 static nokprobe_inline unsigned long 293 get_event_field(struct fetch_insn *code, void *rec) 294 { 295 struct ftrace_event_field *field = code->data; 296 unsigned long val; 297 void *addr; 298 299 addr = rec + field->offset; 300 301 if (is_string_field(field)) { 302 switch (field->filter_type) { 303 case FILTER_DYN_STRING: 304 val = (unsigned long)(rec + (*(unsigned int *)addr & 0xffff)); 305 break; 306 case FILTER_RDYN_STRING: 307 val = (unsigned long)(addr + (*(unsigned int *)addr & 0xffff)); 308 break; 309 case FILTER_STATIC_STRING: 310 val = (unsigned long)addr; 311 break; 312 case FILTER_PTR_STRING: 313 val = (unsigned long)(*(char *)addr); 314 break; 315 default: 316 WARN_ON_ONCE(1); 317 return 0; 318 } 319 return val; 320 } 321 322 switch (field->size) { 323 case 1: 324 if (field->is_signed) 325 val = *(char *)addr; 326 else 327 val = *(unsigned char *)addr; 328 break; 329 case 2: 330 if (field->is_signed) 331 val = *(short *)addr; 332 else 333 val = *(unsigned short *)addr; 334 break; 335 case 4: 336 if (field->is_signed) 337 val = *(int *)addr; 338 else 339 val = *(unsigned int *)addr; 340 break; 341 default: 342 if (field->is_signed) 343 val = *(long *)addr; 344 else 345 val = *(unsigned long *)addr; 346 break; 347 } 348 return val; 349 } 350 351 static int get_eprobe_size(struct trace_probe *tp, void *rec) 352 { 353 struct fetch_insn *code; 354 struct probe_arg *arg; 355 int i, len, ret = 0; 356 357 for (i = 0; i < tp->nr_args; i++) { 358 arg = tp->args + i; 359 if (arg->dynamic) { 360 unsigned long val; 361 362 code = arg->code; 363 retry: 364 switch (code->op) { 365 case FETCH_OP_TP_ARG: 366 val = get_event_field(code, rec); 367 break; 368 case FETCH_NOP_SYMBOL: /* Ignore a place holder */ 369 code++; 370 goto retry; 371 default: 372 if (process_common_fetch_insn(code, &val) < 0) 373 continue; 374 } 375 code++; 376 len = process_fetch_insn_bottom(code, val, NULL, NULL); 377 if (len > 0) 378 ret += len; 379 } 380 } 381 382 return ret; 383 } 384 385 /* Kprobe specific fetch functions */ 386 387 /* Note that we don't verify it, since the code does not come from user space */ 388 static int 389 process_fetch_insn(struct fetch_insn *code, void *rec, void *dest, 390 void *base) 391 { 392 unsigned long val; 393 int ret; 394 395 retry: 396 switch (code->op) { 397 case FETCH_OP_TP_ARG: 398 val = get_event_field(code, rec); 399 break; 400 case FETCH_NOP_SYMBOL: /* Ignore a place holder */ 401 code++; 402 goto retry; 403 default: 404 ret = process_common_fetch_insn(code, &val); 405 if (ret < 0) 406 return ret; 407 } 408 code++; 409 return process_fetch_insn_bottom(code, val, dest, base); 410 } 411 NOKPROBE_SYMBOL(process_fetch_insn) 412 413 /* eprobe handler */ 414 static inline void 415 __eprobe_trace_func(struct eprobe_data *edata, void *rec) 416 { 417 struct eprobe_trace_entry_head *entry; 418 struct trace_event_call *call = trace_probe_event_call(&edata->ep->tp); 419 struct trace_event_buffer fbuffer; 420 int dsize; 421 422 if (WARN_ON_ONCE(call != edata->file->event_call)) 423 return; 424 425 if (trace_trigger_soft_disabled(edata->file)) 426 return; 427 428 dsize = get_eprobe_size(&edata->ep->tp, rec); 429 430 entry = trace_event_buffer_reserve(&fbuffer, edata->file, 431 sizeof(*entry) + edata->ep->tp.size + dsize); 432 433 if (!entry) 434 return; 435 436 entry = fbuffer.entry = ring_buffer_event_data(fbuffer.event); 437 store_trace_args(&entry[1], &edata->ep->tp, rec, sizeof(*entry), dsize); 438 439 trace_event_buffer_commit(&fbuffer); 440 } 441 442 /* 443 * The event probe implementation uses event triggers to get access to 444 * the event it is attached to, but is not an actual trigger. The below 445 * functions are just stubs to fulfill what is needed to use the trigger 446 * infrastructure. 447 */ 448 static int eprobe_trigger_init(struct event_trigger_data *data) 449 { 450 return 0; 451 } 452 453 static void eprobe_trigger_free(struct event_trigger_data *data) 454 { 455 456 } 457 458 static int eprobe_trigger_print(struct seq_file *m, 459 struct event_trigger_data *data) 460 { 461 /* Do not print eprobe event triggers */ 462 return 0; 463 } 464 465 static void eprobe_trigger_func(struct event_trigger_data *data, 466 struct trace_buffer *buffer, void *rec, 467 struct ring_buffer_event *rbe) 468 { 469 struct eprobe_data *edata = data->private_data; 470 471 if (unlikely(!rec)) 472 return; 473 474 __eprobe_trace_func(edata, rec); 475 } 476 477 static struct event_trigger_ops eprobe_trigger_ops = { 478 .trigger = eprobe_trigger_func, 479 .print = eprobe_trigger_print, 480 .init = eprobe_trigger_init, 481 .free = eprobe_trigger_free, 482 }; 483 484 static int eprobe_trigger_cmd_parse(struct event_command *cmd_ops, 485 struct trace_event_file *file, 486 char *glob, char *cmd, 487 char *param_and_filter) 488 { 489 return -1; 490 } 491 492 static int eprobe_trigger_reg_func(char *glob, 493 struct event_trigger_data *data, 494 struct trace_event_file *file) 495 { 496 return -1; 497 } 498 499 static void eprobe_trigger_unreg_func(char *glob, 500 struct event_trigger_data *data, 501 struct trace_event_file *file) 502 { 503 504 } 505 506 static struct event_trigger_ops *eprobe_trigger_get_ops(char *cmd, 507 char *param) 508 { 509 return &eprobe_trigger_ops; 510 } 511 512 static struct event_command event_trigger_cmd = { 513 .name = "eprobe", 514 .trigger_type = ETT_EVENT_EPROBE, 515 .flags = EVENT_CMD_FL_NEEDS_REC, 516 .parse = eprobe_trigger_cmd_parse, 517 .reg = eprobe_trigger_reg_func, 518 .unreg = eprobe_trigger_unreg_func, 519 .unreg_all = NULL, 520 .get_trigger_ops = eprobe_trigger_get_ops, 521 .set_filter = NULL, 522 }; 523 524 static struct event_trigger_data * 525 new_eprobe_trigger(struct trace_eprobe *ep, struct trace_event_file *file) 526 { 527 struct event_trigger_data *trigger; 528 struct event_filter *filter = NULL; 529 struct eprobe_data *edata; 530 int ret; 531 532 edata = kzalloc(sizeof(*edata), GFP_KERNEL); 533 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL); 534 if (!trigger || !edata) { 535 ret = -ENOMEM; 536 goto error; 537 } 538 539 trigger->flags = EVENT_TRIGGER_FL_PROBE; 540 trigger->count = -1; 541 trigger->ops = &eprobe_trigger_ops; 542 543 /* 544 * EVENT PROBE triggers are not registered as commands with 545 * register_event_command(), as they are not controlled by the user 546 * from the trigger file 547 */ 548 trigger->cmd_ops = &event_trigger_cmd; 549 550 INIT_LIST_HEAD(&trigger->list); 551 552 if (ep->filter_str) { 553 ret = create_event_filter(file->tr, ep->event, 554 ep->filter_str, false, &filter); 555 if (ret) 556 goto error; 557 } 558 RCU_INIT_POINTER(trigger->filter, filter); 559 560 edata->file = file; 561 edata->ep = ep; 562 trigger->private_data = edata; 563 564 return trigger; 565 error: 566 free_event_filter(filter); 567 kfree(edata); 568 kfree(trigger); 569 return ERR_PTR(ret); 570 } 571 572 static int enable_eprobe(struct trace_eprobe *ep, 573 struct trace_event_file *eprobe_file) 574 { 575 struct event_trigger_data *trigger; 576 struct trace_event_file *file; 577 struct trace_array *tr = eprobe_file->tr; 578 579 file = find_event_file(tr, ep->event_system, ep->event_name); 580 if (!file) 581 return -ENOENT; 582 trigger = new_eprobe_trigger(ep, eprobe_file); 583 if (IS_ERR(trigger)) 584 return PTR_ERR(trigger); 585 586 list_add_tail_rcu(&trigger->list, &file->triggers); 587 588 trace_event_trigger_enable_disable(file, 1); 589 update_cond_flag(file); 590 591 return 0; 592 } 593 594 static struct trace_event_functions eprobe_funcs = { 595 .trace = print_eprobe_event 596 }; 597 598 static int disable_eprobe(struct trace_eprobe *ep, 599 struct trace_array *tr) 600 { 601 struct event_trigger_data *trigger = NULL, *iter; 602 struct trace_event_file *file; 603 struct event_filter *filter; 604 struct eprobe_data *edata; 605 606 file = find_event_file(tr, ep->event_system, ep->event_name); 607 if (!file) 608 return -ENOENT; 609 610 list_for_each_entry(iter, &file->triggers, list) { 611 if (!(iter->flags & EVENT_TRIGGER_FL_PROBE)) 612 continue; 613 edata = iter->private_data; 614 if (edata->ep == ep) { 615 trigger = iter; 616 break; 617 } 618 } 619 if (!trigger) 620 return -ENODEV; 621 622 list_del_rcu(&trigger->list); 623 624 trace_event_trigger_enable_disable(file, 0); 625 update_cond_flag(file); 626 627 /* Make sure nothing is using the edata or trigger */ 628 tracepoint_synchronize_unregister(); 629 630 filter = rcu_access_pointer(trigger->filter); 631 632 if (filter) 633 free_event_filter(filter); 634 kfree(edata); 635 kfree(trigger); 636 637 return 0; 638 } 639 640 static int enable_trace_eprobe(struct trace_event_call *call, 641 struct trace_event_file *file) 642 { 643 struct trace_probe *pos, *tp; 644 struct trace_eprobe *ep; 645 bool enabled; 646 int ret = 0; 647 648 tp = trace_probe_primary_from_call(call); 649 if (WARN_ON_ONCE(!tp)) 650 return -ENODEV; 651 enabled = trace_probe_is_enabled(tp); 652 653 /* This also changes "enabled" state */ 654 if (file) { 655 ret = trace_probe_add_file(tp, file); 656 if (ret) 657 return ret; 658 } else 659 trace_probe_set_flag(tp, TP_FLAG_PROFILE); 660 661 if (enabled) 662 return 0; 663 664 list_for_each_entry(pos, trace_probe_probe_list(tp), list) { 665 ep = container_of(pos, struct trace_eprobe, tp); 666 ret = enable_eprobe(ep, file); 667 if (ret) 668 break; 669 enabled = true; 670 } 671 672 if (ret) { 673 /* Failed to enable one of them. Roll back all */ 674 if (enabled) 675 disable_eprobe(ep, file->tr); 676 if (file) 677 trace_probe_remove_file(tp, file); 678 else 679 trace_probe_clear_flag(tp, TP_FLAG_PROFILE); 680 } 681 682 return ret; 683 } 684 685 static int disable_trace_eprobe(struct trace_event_call *call, 686 struct trace_event_file *file) 687 { 688 struct trace_probe *pos, *tp; 689 struct trace_eprobe *ep; 690 691 tp = trace_probe_primary_from_call(call); 692 if (WARN_ON_ONCE(!tp)) 693 return -ENODEV; 694 695 if (file) { 696 if (!trace_probe_get_file_link(tp, file)) 697 return -ENOENT; 698 if (!trace_probe_has_single_file(tp)) 699 goto out; 700 trace_probe_clear_flag(tp, TP_FLAG_TRACE); 701 } else 702 trace_probe_clear_flag(tp, TP_FLAG_PROFILE); 703 704 if (!trace_probe_is_enabled(tp)) { 705 list_for_each_entry(pos, trace_probe_probe_list(tp), list) { 706 ep = container_of(pos, struct trace_eprobe, tp); 707 disable_eprobe(ep, file->tr); 708 } 709 } 710 711 out: 712 if (file) 713 /* 714 * Synchronization is done in below function. For perf event, 715 * file == NULL and perf_trace_event_unreg() calls 716 * tracepoint_synchronize_unregister() to ensure synchronize 717 * event. We don't need to care about it. 718 */ 719 trace_probe_remove_file(tp, file); 720 721 return 0; 722 } 723 724 static int eprobe_register(struct trace_event_call *event, 725 enum trace_reg type, void *data) 726 { 727 struct trace_event_file *file = data; 728 729 switch (type) { 730 case TRACE_REG_REGISTER: 731 return enable_trace_eprobe(event, file); 732 case TRACE_REG_UNREGISTER: 733 return disable_trace_eprobe(event, file); 734 #ifdef CONFIG_PERF_EVENTS 735 case TRACE_REG_PERF_REGISTER: 736 case TRACE_REG_PERF_UNREGISTER: 737 case TRACE_REG_PERF_OPEN: 738 case TRACE_REG_PERF_CLOSE: 739 case TRACE_REG_PERF_ADD: 740 case TRACE_REG_PERF_DEL: 741 return 0; 742 #endif 743 } 744 return 0; 745 } 746 747 static inline void init_trace_eprobe_call(struct trace_eprobe *ep) 748 { 749 struct trace_event_call *call = trace_probe_event_call(&ep->tp); 750 751 call->flags = TRACE_EVENT_FL_EPROBE; 752 call->event.funcs = &eprobe_funcs; 753 call->class->fields_array = eprobe_fields_array; 754 call->class->reg = eprobe_register; 755 } 756 757 static struct trace_event_call * 758 find_and_get_event(const char *system, const char *event_name) 759 { 760 struct trace_event_call *tp_event; 761 const char *name; 762 763 list_for_each_entry(tp_event, &ftrace_events, list) { 764 /* Skip other probes and ftrace events */ 765 if (tp_event->flags & 766 (TRACE_EVENT_FL_IGNORE_ENABLE | 767 TRACE_EVENT_FL_KPROBE | 768 TRACE_EVENT_FL_UPROBE | 769 TRACE_EVENT_FL_EPROBE)) 770 continue; 771 if (!tp_event->class->system || 772 strcmp(system, tp_event->class->system)) 773 continue; 774 name = trace_event_name(tp_event); 775 if (!name || strcmp(event_name, name)) 776 continue; 777 if (!trace_event_try_get_ref(tp_event)) { 778 return NULL; 779 break; 780 } 781 return tp_event; 782 break; 783 } 784 return NULL; 785 } 786 787 static int trace_eprobe_tp_update_arg(struct trace_eprobe *ep, const char *argv[], int i) 788 { 789 struct traceprobe_parse_context ctx = { 790 .event = ep->event, 791 .flags = TPARG_FL_KERNEL | TPARG_FL_TEVENT, 792 }; 793 int ret; 794 795 ret = traceprobe_parse_probe_arg(&ep->tp, i, argv[i], &ctx); 796 if (ret) 797 return ret; 798 799 /* Handle symbols "@" */ 800 if (!ret) 801 ret = traceprobe_update_arg(&ep->tp.args[i]); 802 803 return ret; 804 } 805 806 static int trace_eprobe_parse_filter(struct trace_eprobe *ep, int argc, const char *argv[]) 807 { 808 struct event_filter *dummy = NULL; 809 int i, ret, len = 0; 810 char *p; 811 812 if (argc == 0) { 813 trace_probe_log_err(0, NO_EP_FILTER); 814 return -EINVAL; 815 } 816 817 /* Recover the filter string */ 818 for (i = 0; i < argc; i++) 819 len += strlen(argv[i]) + 1; 820 821 ep->filter_str = kzalloc(len, GFP_KERNEL); 822 if (!ep->filter_str) 823 return -ENOMEM; 824 825 p = ep->filter_str; 826 for (i = 0; i < argc; i++) { 827 if (i) 828 ret = snprintf(p, len, " %s", argv[i]); 829 else 830 ret = snprintf(p, len, "%s", argv[i]); 831 p += ret; 832 len -= ret; 833 } 834 835 /* 836 * Ensure the filter string can be parsed correctly. Note, this 837 * filter string is for the original event, not for the eprobe. 838 */ 839 ret = create_event_filter(top_trace_array(), ep->event, ep->filter_str, 840 true, &dummy); 841 free_event_filter(dummy); 842 if (ret) 843 goto error; 844 845 return 0; 846 error: 847 kfree(ep->filter_str); 848 ep->filter_str = NULL; 849 return ret; 850 } 851 852 static int __trace_eprobe_create(int argc, const char *argv[]) 853 { 854 /* 855 * Argument syntax: 856 * e[:[GRP/][ENAME]] SYSTEM.EVENT [FETCHARGS] [if FILTER] 857 * Fetch args (no space): 858 * <name>=$<field>[:TYPE] 859 */ 860 const char *event = NULL, *group = EPROBE_EVENT_SYSTEM; 861 const char *sys_event = NULL, *sys_name = NULL; 862 struct trace_event_call *event_call; 863 struct trace_eprobe *ep = NULL; 864 char buf1[MAX_EVENT_NAME_LEN]; 865 char buf2[MAX_EVENT_NAME_LEN]; 866 char gbuf[MAX_EVENT_NAME_LEN]; 867 int ret = 0, filter_idx = 0; 868 int i, filter_cnt; 869 870 if (argc < 2 || argv[0][0] != 'e') 871 return -ECANCELED; 872 873 trace_probe_log_init("event_probe", argc, argv); 874 875 event = strchr(&argv[0][1], ':'); 876 if (event) { 877 event++; 878 ret = traceprobe_parse_event_name(&event, &group, gbuf, 879 event - argv[0]); 880 if (ret) 881 goto parse_error; 882 } 883 884 trace_probe_log_set_index(1); 885 sys_event = argv[1]; 886 ret = traceprobe_parse_event_name(&sys_event, &sys_name, buf2, 0); 887 if (ret || !sys_event || !sys_name) { 888 trace_probe_log_err(0, NO_EVENT_INFO); 889 goto parse_error; 890 } 891 892 if (!event) { 893 strscpy(buf1, sys_event, MAX_EVENT_NAME_LEN); 894 event = buf1; 895 } 896 897 for (i = 2; i < argc; i++) { 898 if (!strcmp(argv[i], "if")) { 899 filter_idx = i + 1; 900 filter_cnt = argc - filter_idx; 901 argc = i; 902 break; 903 } 904 } 905 906 mutex_lock(&event_mutex); 907 event_call = find_and_get_event(sys_name, sys_event); 908 ep = alloc_event_probe(group, event, event_call, argc - 2); 909 mutex_unlock(&event_mutex); 910 911 if (IS_ERR(ep)) { 912 ret = PTR_ERR(ep); 913 if (ret == -ENODEV) 914 trace_probe_log_err(0, BAD_ATTACH_EVENT); 915 /* This must return -ENOMEM or missing event, else there is a bug */ 916 WARN_ON_ONCE(ret != -ENOMEM && ret != -ENODEV); 917 ep = NULL; 918 goto error; 919 } 920 921 if (filter_idx) { 922 trace_probe_log_set_index(filter_idx); 923 ret = trace_eprobe_parse_filter(ep, filter_cnt, argv + filter_idx); 924 if (ret) 925 goto parse_error; 926 } else 927 ep->filter_str = NULL; 928 929 argc -= 2; argv += 2; 930 /* parse arguments */ 931 for (i = 0; i < argc && i < MAX_TRACE_ARGS; i++) { 932 trace_probe_log_set_index(i + 2); 933 ret = trace_eprobe_tp_update_arg(ep, argv, i); 934 if (ret) 935 goto error; 936 } 937 ret = traceprobe_set_print_fmt(&ep->tp, PROBE_PRINT_EVENT); 938 if (ret < 0) 939 goto error; 940 init_trace_eprobe_call(ep); 941 mutex_lock(&event_mutex); 942 ret = trace_probe_register_event_call(&ep->tp); 943 if (ret) { 944 if (ret == -EEXIST) { 945 trace_probe_log_set_index(0); 946 trace_probe_log_err(0, EVENT_EXIST); 947 } 948 mutex_unlock(&event_mutex); 949 goto error; 950 } 951 ret = dyn_event_add(&ep->devent, &ep->tp.event->call); 952 mutex_unlock(&event_mutex); 953 return ret; 954 parse_error: 955 ret = -EINVAL; 956 error: 957 trace_event_probe_cleanup(ep); 958 return ret; 959 } 960 961 /* 962 * Register dynevent at core_initcall. This allows kernel to setup eprobe 963 * events in postcore_initcall without tracefs. 964 */ 965 static __init int trace_events_eprobe_init_early(void) 966 { 967 int err = 0; 968 969 err = dyn_event_register(&eprobe_dyn_event_ops); 970 if (err) 971 pr_warn("Could not register eprobe_dyn_event_ops\n"); 972 973 return err; 974 } 975 core_initcall(trace_events_eprobe_init_early); 976