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