trace_probe.c (28513B)
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 "trace_probe.h" 15 16#undef C 17#define C(a, b) b 18 19static const char *trace_probe_err_text[] = { ERRORS }; 20 21static const char *reserved_field_names[] = { 22 "common_type", 23 "common_flags", 24 "common_preempt_count", 25 "common_pid", 26 "common_tgid", 27 FIELD_STRING_IP, 28 FIELD_STRING_RETIP, 29 FIELD_STRING_FUNC, 30}; 31 32/* Printing in basic type function template */ 33#define DEFINE_BASIC_PRINT_TYPE_FUNC(tname, type, fmt) \ 34int PRINT_TYPE_FUNC_NAME(tname)(struct trace_seq *s, void *data, void *ent)\ 35{ \ 36 trace_seq_printf(s, fmt, *(type *)data); \ 37 return !trace_seq_has_overflowed(s); \ 38} \ 39const char PRINT_TYPE_FMT_NAME(tname)[] = fmt; 40 41DEFINE_BASIC_PRINT_TYPE_FUNC(u8, u8, "%u") 42DEFINE_BASIC_PRINT_TYPE_FUNC(u16, u16, "%u") 43DEFINE_BASIC_PRINT_TYPE_FUNC(u32, u32, "%u") 44DEFINE_BASIC_PRINT_TYPE_FUNC(u64, u64, "%Lu") 45DEFINE_BASIC_PRINT_TYPE_FUNC(s8, s8, "%d") 46DEFINE_BASIC_PRINT_TYPE_FUNC(s16, s16, "%d") 47DEFINE_BASIC_PRINT_TYPE_FUNC(s32, s32, "%d") 48DEFINE_BASIC_PRINT_TYPE_FUNC(s64, s64, "%Ld") 49DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x") 50DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x") 51DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x") 52DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx") 53 54int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent) 55{ 56 trace_seq_printf(s, "%pS", (void *)*(unsigned long *)data); 57 return !trace_seq_has_overflowed(s); 58} 59const char PRINT_TYPE_FMT_NAME(symbol)[] = "%pS"; 60 61/* Print type function for string type */ 62int PRINT_TYPE_FUNC_NAME(string)(struct trace_seq *s, void *data, void *ent) 63{ 64 int len = *(u32 *)data >> 16; 65 66 if (!len) 67 trace_seq_puts(s, "(fault)"); 68 else 69 trace_seq_printf(s, "\"%s\"", 70 (const char *)get_loc_data(data, ent)); 71 return !trace_seq_has_overflowed(s); 72} 73 74const char PRINT_TYPE_FMT_NAME(string)[] = "\\\"%s\\\""; 75 76/* Fetch type information table */ 77static const struct fetch_type probe_fetch_types[] = { 78 /* Special types */ 79 __ASSIGN_FETCH_TYPE("string", string, string, sizeof(u32), 1, 80 "__data_loc char[]"), 81 __ASSIGN_FETCH_TYPE("ustring", string, string, sizeof(u32), 1, 82 "__data_loc char[]"), 83 /* Basic types */ 84 ASSIGN_FETCH_TYPE(u8, u8, 0), 85 ASSIGN_FETCH_TYPE(u16, u16, 0), 86 ASSIGN_FETCH_TYPE(u32, u32, 0), 87 ASSIGN_FETCH_TYPE(u64, u64, 0), 88 ASSIGN_FETCH_TYPE(s8, u8, 1), 89 ASSIGN_FETCH_TYPE(s16, u16, 1), 90 ASSIGN_FETCH_TYPE(s32, u32, 1), 91 ASSIGN_FETCH_TYPE(s64, u64, 1), 92 ASSIGN_FETCH_TYPE_ALIAS(x8, u8, u8, 0), 93 ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0), 94 ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0), 95 ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0), 96 ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0), 97 98 ASSIGN_FETCH_TYPE_END 99}; 100 101static const struct fetch_type *find_fetch_type(const char *type) 102{ 103 int i; 104 105 if (!type) 106 type = DEFAULT_FETCH_TYPE_STR; 107 108 /* Special case: bitfield */ 109 if (*type == 'b') { 110 unsigned long bs; 111 112 type = strchr(type, '/'); 113 if (!type) 114 goto fail; 115 116 type++; 117 if (kstrtoul(type, 0, &bs)) 118 goto fail; 119 120 switch (bs) { 121 case 8: 122 return find_fetch_type("u8"); 123 case 16: 124 return find_fetch_type("u16"); 125 case 32: 126 return find_fetch_type("u32"); 127 case 64: 128 return find_fetch_type("u64"); 129 default: 130 goto fail; 131 } 132 } 133 134 for (i = 0; probe_fetch_types[i].name; i++) { 135 if (strcmp(type, probe_fetch_types[i].name) == 0) 136 return &probe_fetch_types[i]; 137 } 138 139fail: 140 return NULL; 141} 142 143static struct trace_probe_log trace_probe_log; 144 145void trace_probe_log_init(const char *subsystem, int argc, const char **argv) 146{ 147 trace_probe_log.subsystem = subsystem; 148 trace_probe_log.argc = argc; 149 trace_probe_log.argv = argv; 150 trace_probe_log.index = 0; 151} 152 153void trace_probe_log_clear(void) 154{ 155 memset(&trace_probe_log, 0, sizeof(trace_probe_log)); 156} 157 158void trace_probe_log_set_index(int index) 159{ 160 trace_probe_log.index = index; 161} 162 163void __trace_probe_log_err(int offset, int err_type) 164{ 165 char *command, *p; 166 int i, len = 0, pos = 0; 167 168 if (!trace_probe_log.argv) 169 return; 170 171 /* Recalculate the length and allocate buffer */ 172 for (i = 0; i < trace_probe_log.argc; i++) { 173 if (i == trace_probe_log.index) 174 pos = len; 175 len += strlen(trace_probe_log.argv[i]) + 1; 176 } 177 command = kzalloc(len, GFP_KERNEL); 178 if (!command) 179 return; 180 181 if (trace_probe_log.index >= trace_probe_log.argc) { 182 /** 183 * Set the error position is next to the last arg + space. 184 * Note that len includes the terminal null and the cursor 185 * appears at pos + 1. 186 */ 187 pos = len; 188 offset = 0; 189 } 190 191 /* And make a command string from argv array */ 192 p = command; 193 for (i = 0; i < trace_probe_log.argc; i++) { 194 len = strlen(trace_probe_log.argv[i]); 195 strcpy(p, trace_probe_log.argv[i]); 196 p[len] = ' '; 197 p += len + 1; 198 } 199 *(p - 1) = '\0'; 200 201 tracing_log_err(NULL, trace_probe_log.subsystem, command, 202 trace_probe_err_text, err_type, pos + offset); 203 204 kfree(command); 205} 206 207/* Split symbol and offset. */ 208int traceprobe_split_symbol_offset(char *symbol, long *offset) 209{ 210 char *tmp; 211 int ret; 212 213 if (!offset) 214 return -EINVAL; 215 216 tmp = strpbrk(symbol, "+-"); 217 if (tmp) { 218 ret = kstrtol(tmp, 0, offset); 219 if (ret) 220 return ret; 221 *tmp = '\0'; 222 } else 223 *offset = 0; 224 225 return 0; 226} 227 228/* @buf must has MAX_EVENT_NAME_LEN size */ 229int traceprobe_parse_event_name(const char **pevent, const char **pgroup, 230 char *buf, int offset) 231{ 232 const char *slash, *event = *pevent; 233 int len; 234 235 slash = strchr(event, '/'); 236 if (!slash) 237 slash = strchr(event, '.'); 238 239 if (slash) { 240 if (slash == event) { 241 trace_probe_log_err(offset, NO_GROUP_NAME); 242 return -EINVAL; 243 } 244 if (slash - event + 1 > MAX_EVENT_NAME_LEN) { 245 trace_probe_log_err(offset, GROUP_TOO_LONG); 246 return -EINVAL; 247 } 248 strlcpy(buf, event, slash - event + 1); 249 if (!is_good_name(buf)) { 250 trace_probe_log_err(offset, BAD_GROUP_NAME); 251 return -EINVAL; 252 } 253 *pgroup = buf; 254 *pevent = slash + 1; 255 offset += slash - event + 1; 256 event = *pevent; 257 } 258 len = strlen(event); 259 if (len == 0) { 260 trace_probe_log_err(offset, NO_EVENT_NAME); 261 return -EINVAL; 262 } else if (len > MAX_EVENT_NAME_LEN) { 263 trace_probe_log_err(offset, EVENT_TOO_LONG); 264 return -EINVAL; 265 } 266 if (!is_good_name(event)) { 267 trace_probe_log_err(offset, BAD_EVENT_NAME); 268 return -EINVAL; 269 } 270 return 0; 271} 272 273#define PARAM_MAX_STACK (THREAD_SIZE / sizeof(unsigned long)) 274 275static int parse_probe_vars(char *arg, const struct fetch_type *t, 276 struct fetch_insn *code, unsigned int flags, int offs) 277{ 278 unsigned long param; 279 int ret = 0; 280 int len; 281 282 if (strcmp(arg, "retval") == 0) { 283 if (flags & TPARG_FL_RETURN) { 284 code->op = FETCH_OP_RETVAL; 285 } else { 286 trace_probe_log_err(offs, RETVAL_ON_PROBE); 287 ret = -EINVAL; 288 } 289 } else if ((len = str_has_prefix(arg, "stack"))) { 290 if (arg[len] == '\0') { 291 code->op = FETCH_OP_STACKP; 292 } else if (isdigit(arg[len])) { 293 ret = kstrtoul(arg + len, 10, ¶m); 294 if (ret) { 295 goto inval_var; 296 } else if ((flags & TPARG_FL_KERNEL) && 297 param > PARAM_MAX_STACK) { 298 trace_probe_log_err(offs, BAD_STACK_NUM); 299 ret = -EINVAL; 300 } else { 301 code->op = FETCH_OP_STACK; 302 code->param = (unsigned int)param; 303 } 304 } else 305 goto inval_var; 306 } else if (strcmp(arg, "comm") == 0) { 307 code->op = FETCH_OP_COMM; 308#ifdef CONFIG_HAVE_FUNCTION_ARG_ACCESS_API 309 } else if (((flags & TPARG_FL_MASK) == 310 (TPARG_FL_KERNEL | TPARG_FL_FENTRY)) && 311 (len = str_has_prefix(arg, "arg"))) { 312 ret = kstrtoul(arg + len, 10, ¶m); 313 if (ret) { 314 goto inval_var; 315 } else if (!param || param > PARAM_MAX_STACK) { 316 trace_probe_log_err(offs, BAD_ARG_NUM); 317 return -EINVAL; 318 } 319 code->op = FETCH_OP_ARG; 320 code->param = (unsigned int)param - 1; 321#endif 322 } else if (flags & TPARG_FL_TPOINT) { 323 if (code->data) 324 return -EFAULT; 325 code->data = kstrdup(arg, GFP_KERNEL); 326 if (!code->data) 327 return -ENOMEM; 328 code->op = FETCH_OP_TP_ARG; 329 } else 330 goto inval_var; 331 332 return ret; 333 334inval_var: 335 trace_probe_log_err(offs, BAD_VAR); 336 return -EINVAL; 337} 338 339static int str_to_immediate(char *str, unsigned long *imm) 340{ 341 if (isdigit(str[0])) 342 return kstrtoul(str, 0, imm); 343 else if (str[0] == '-') 344 return kstrtol(str, 0, (long *)imm); 345 else if (str[0] == '+') 346 return kstrtol(str + 1, 0, (long *)imm); 347 return -EINVAL; 348} 349 350static int __parse_imm_string(char *str, char **pbuf, int offs) 351{ 352 size_t len = strlen(str); 353 354 if (str[len - 1] != '"') { 355 trace_probe_log_err(offs + len, IMMSTR_NO_CLOSE); 356 return -EINVAL; 357 } 358 *pbuf = kstrndup(str, len - 1, GFP_KERNEL); 359 if (!*pbuf) 360 return -ENOMEM; 361 return 0; 362} 363 364/* Recursive argument parser */ 365static int 366parse_probe_arg(char *arg, const struct fetch_type *type, 367 struct fetch_insn **pcode, struct fetch_insn *end, 368 unsigned int flags, int offs) 369{ 370 struct fetch_insn *code = *pcode; 371 unsigned long param; 372 int deref = FETCH_OP_DEREF; 373 long offset = 0; 374 char *tmp; 375 int ret = 0; 376 377 switch (arg[0]) { 378 case '$': 379 ret = parse_probe_vars(arg + 1, type, code, flags, offs); 380 break; 381 382 case '%': /* named register */ 383 ret = regs_query_register_offset(arg + 1); 384 if (ret >= 0) { 385 code->op = FETCH_OP_REG; 386 code->param = (unsigned int)ret; 387 ret = 0; 388 } else 389 trace_probe_log_err(offs, BAD_REG_NAME); 390 break; 391 392 case '@': /* memory, file-offset or symbol */ 393 if (isdigit(arg[1])) { 394 ret = kstrtoul(arg + 1, 0, ¶m); 395 if (ret) { 396 trace_probe_log_err(offs, BAD_MEM_ADDR); 397 break; 398 } 399 /* load address */ 400 code->op = FETCH_OP_IMM; 401 code->immediate = param; 402 } else if (arg[1] == '+') { 403 /* kprobes don't support file offsets */ 404 if (flags & TPARG_FL_KERNEL) { 405 trace_probe_log_err(offs, FILE_ON_KPROBE); 406 return -EINVAL; 407 } 408 ret = kstrtol(arg + 2, 0, &offset); 409 if (ret) { 410 trace_probe_log_err(offs, BAD_FILE_OFFS); 411 break; 412 } 413 414 code->op = FETCH_OP_FOFFS; 415 code->immediate = (unsigned long)offset; // imm64? 416 } else { 417 /* uprobes don't support symbols */ 418 if (!(flags & TPARG_FL_KERNEL)) { 419 trace_probe_log_err(offs, SYM_ON_UPROBE); 420 return -EINVAL; 421 } 422 /* Preserve symbol for updating */ 423 code->op = FETCH_NOP_SYMBOL; 424 code->data = kstrdup(arg + 1, GFP_KERNEL); 425 if (!code->data) 426 return -ENOMEM; 427 if (++code == end) { 428 trace_probe_log_err(offs, TOO_MANY_OPS); 429 return -EINVAL; 430 } 431 code->op = FETCH_OP_IMM; 432 code->immediate = 0; 433 } 434 /* These are fetching from memory */ 435 if (++code == end) { 436 trace_probe_log_err(offs, TOO_MANY_OPS); 437 return -EINVAL; 438 } 439 *pcode = code; 440 code->op = FETCH_OP_DEREF; 441 code->offset = offset; 442 break; 443 444 case '+': /* deref memory */ 445 case '-': 446 if (arg[1] == 'u') { 447 deref = FETCH_OP_UDEREF; 448 arg[1] = arg[0]; 449 arg++; 450 } 451 if (arg[0] == '+') 452 arg++; /* Skip '+', because kstrtol() rejects it. */ 453 tmp = strchr(arg, '('); 454 if (!tmp) { 455 trace_probe_log_err(offs, DEREF_NEED_BRACE); 456 return -EINVAL; 457 } 458 *tmp = '\0'; 459 ret = kstrtol(arg, 0, &offset); 460 if (ret) { 461 trace_probe_log_err(offs, BAD_DEREF_OFFS); 462 break; 463 } 464 offs += (tmp + 1 - arg) + (arg[0] != '-' ? 1 : 0); 465 arg = tmp + 1; 466 tmp = strrchr(arg, ')'); 467 if (!tmp) { 468 trace_probe_log_err(offs + strlen(arg), 469 DEREF_OPEN_BRACE); 470 return -EINVAL; 471 } else { 472 const struct fetch_type *t2 = find_fetch_type(NULL); 473 474 *tmp = '\0'; 475 ret = parse_probe_arg(arg, t2, &code, end, flags, offs); 476 if (ret) 477 break; 478 if (code->op == FETCH_OP_COMM || 479 code->op == FETCH_OP_DATA) { 480 trace_probe_log_err(offs, COMM_CANT_DEREF); 481 return -EINVAL; 482 } 483 if (++code == end) { 484 trace_probe_log_err(offs, TOO_MANY_OPS); 485 return -EINVAL; 486 } 487 *pcode = code; 488 489 code->op = deref; 490 code->offset = offset; 491 } 492 break; 493 case '\\': /* Immediate value */ 494 if (arg[1] == '"') { /* Immediate string */ 495 ret = __parse_imm_string(arg + 2, &tmp, offs + 2); 496 if (ret) 497 break; 498 code->op = FETCH_OP_DATA; 499 code->data = tmp; 500 } else { 501 ret = str_to_immediate(arg + 1, &code->immediate); 502 if (ret) 503 trace_probe_log_err(offs + 1, BAD_IMM); 504 else 505 code->op = FETCH_OP_IMM; 506 } 507 break; 508 } 509 if (!ret && code->op == FETCH_OP_NOP) { 510 /* Parsed, but do not find fetch method */ 511 trace_probe_log_err(offs, BAD_FETCH_ARG); 512 ret = -EINVAL; 513 } 514 return ret; 515} 516 517#define BYTES_TO_BITS(nb) ((BITS_PER_LONG * (nb)) / sizeof(long)) 518 519/* Bitfield type needs to be parsed into a fetch function */ 520static int __parse_bitfield_probe_arg(const char *bf, 521 const struct fetch_type *t, 522 struct fetch_insn **pcode) 523{ 524 struct fetch_insn *code = *pcode; 525 unsigned long bw, bo; 526 char *tail; 527 528 if (*bf != 'b') 529 return 0; 530 531 bw = simple_strtoul(bf + 1, &tail, 0); /* Use simple one */ 532 533 if (bw == 0 || *tail != '@') 534 return -EINVAL; 535 536 bf = tail + 1; 537 bo = simple_strtoul(bf, &tail, 0); 538 539 if (tail == bf || *tail != '/') 540 return -EINVAL; 541 code++; 542 if (code->op != FETCH_OP_NOP) 543 return -EINVAL; 544 *pcode = code; 545 546 code->op = FETCH_OP_MOD_BF; 547 code->lshift = BYTES_TO_BITS(t->size) - (bw + bo); 548 code->rshift = BYTES_TO_BITS(t->size) - bw; 549 code->basesize = t->size; 550 551 return (BYTES_TO_BITS(t->size) < (bw + bo)) ? -EINVAL : 0; 552} 553 554/* String length checking wrapper */ 555static int traceprobe_parse_probe_arg_body(const char *argv, ssize_t *size, 556 struct probe_arg *parg, unsigned int flags, int offset) 557{ 558 struct fetch_insn *code, *scode, *tmp = NULL; 559 char *t, *t2, *t3; 560 char *arg; 561 int ret, len; 562 563 arg = kstrdup(argv, GFP_KERNEL); 564 if (!arg) 565 return -ENOMEM; 566 567 ret = -EINVAL; 568 len = strlen(arg); 569 if (len > MAX_ARGSTR_LEN) { 570 trace_probe_log_err(offset, ARG_TOO_LONG); 571 goto out; 572 } else if (len == 0) { 573 trace_probe_log_err(offset, NO_ARG_BODY); 574 goto out; 575 } 576 577 ret = -ENOMEM; 578 parg->comm = kstrdup(arg, GFP_KERNEL); 579 if (!parg->comm) 580 goto out; 581 582 ret = -EINVAL; 583 t = strchr(arg, ':'); 584 if (t) { 585 *t = '\0'; 586 t2 = strchr(++t, '['); 587 if (t2) { 588 *t2++ = '\0'; 589 t3 = strchr(t2, ']'); 590 if (!t3) { 591 offset += t2 + strlen(t2) - arg; 592 trace_probe_log_err(offset, 593 ARRAY_NO_CLOSE); 594 goto out; 595 } else if (t3[1] != '\0') { 596 trace_probe_log_err(offset + t3 + 1 - arg, 597 BAD_ARRAY_SUFFIX); 598 goto out; 599 } 600 *t3 = '\0'; 601 if (kstrtouint(t2, 0, &parg->count) || !parg->count) { 602 trace_probe_log_err(offset + t2 - arg, 603 BAD_ARRAY_NUM); 604 goto out; 605 } 606 if (parg->count > MAX_ARRAY_LEN) { 607 trace_probe_log_err(offset + t2 - arg, 608 ARRAY_TOO_BIG); 609 goto out; 610 } 611 } 612 } 613 614 /* 615 * Since $comm and immediate string can not be dereferenced, 616 * we can find those by strcmp. 617 */ 618 if (strcmp(arg, "$comm") == 0 || strncmp(arg, "\\\"", 2) == 0) { 619 /* The type of $comm must be "string", and not an array. */ 620 if (parg->count || (t && strcmp(t, "string"))) 621 goto out; 622 parg->type = find_fetch_type("string"); 623 } else 624 parg->type = find_fetch_type(t); 625 if (!parg->type) { 626 trace_probe_log_err(offset + (t ? (t - arg) : 0), BAD_TYPE); 627 goto out; 628 } 629 parg->offset = *size; 630 *size += parg->type->size * (parg->count ?: 1); 631 632 ret = -ENOMEM; 633 if (parg->count) { 634 len = strlen(parg->type->fmttype) + 6; 635 parg->fmt = kmalloc(len, GFP_KERNEL); 636 if (!parg->fmt) 637 goto out; 638 snprintf(parg->fmt, len, "%s[%d]", parg->type->fmttype, 639 parg->count); 640 } 641 642 code = tmp = kcalloc(FETCH_INSN_MAX, sizeof(*code), GFP_KERNEL); 643 if (!code) 644 goto out; 645 code[FETCH_INSN_MAX - 1].op = FETCH_OP_END; 646 647 ret = parse_probe_arg(arg, parg->type, &code, &code[FETCH_INSN_MAX - 1], 648 flags, offset); 649 if (ret) 650 goto fail; 651 652 ret = -EINVAL; 653 /* Store operation */ 654 if (!strcmp(parg->type->name, "string") || 655 !strcmp(parg->type->name, "ustring")) { 656 if (code->op != FETCH_OP_DEREF && code->op != FETCH_OP_UDEREF && 657 code->op != FETCH_OP_IMM && code->op != FETCH_OP_COMM && 658 code->op != FETCH_OP_DATA && code->op != FETCH_OP_TP_ARG) { 659 trace_probe_log_err(offset + (t ? (t - arg) : 0), 660 BAD_STRING); 661 goto fail; 662 } 663 if ((code->op == FETCH_OP_IMM || code->op == FETCH_OP_COMM || 664 code->op == FETCH_OP_DATA) || code->op == FETCH_OP_TP_ARG || 665 parg->count) { 666 /* 667 * IMM, DATA and COMM is pointing actual address, those 668 * must be kept, and if parg->count != 0, this is an 669 * array of string pointers instead of string address 670 * itself. 671 */ 672 code++; 673 if (code->op != FETCH_OP_NOP) { 674 trace_probe_log_err(offset, TOO_MANY_OPS); 675 goto fail; 676 } 677 } 678 /* If op == DEREF, replace it with STRING */ 679 if (!strcmp(parg->type->name, "ustring") || 680 code->op == FETCH_OP_UDEREF) 681 code->op = FETCH_OP_ST_USTRING; 682 else 683 code->op = FETCH_OP_ST_STRING; 684 code->size = parg->type->size; 685 parg->dynamic = true; 686 } else if (code->op == FETCH_OP_DEREF) { 687 code->op = FETCH_OP_ST_MEM; 688 code->size = parg->type->size; 689 } else if (code->op == FETCH_OP_UDEREF) { 690 code->op = FETCH_OP_ST_UMEM; 691 code->size = parg->type->size; 692 } else { 693 code++; 694 if (code->op != FETCH_OP_NOP) { 695 trace_probe_log_err(offset, TOO_MANY_OPS); 696 goto fail; 697 } 698 code->op = FETCH_OP_ST_RAW; 699 code->size = parg->type->size; 700 } 701 scode = code; 702 /* Modify operation */ 703 if (t != NULL) { 704 ret = __parse_bitfield_probe_arg(t, parg->type, &code); 705 if (ret) { 706 trace_probe_log_err(offset + t - arg, BAD_BITFIELD); 707 goto fail; 708 } 709 } 710 ret = -EINVAL; 711 /* Loop(Array) operation */ 712 if (parg->count) { 713 if (scode->op != FETCH_OP_ST_MEM && 714 scode->op != FETCH_OP_ST_STRING && 715 scode->op != FETCH_OP_ST_USTRING) { 716 trace_probe_log_err(offset + (t ? (t - arg) : 0), 717 BAD_STRING); 718 goto fail; 719 } 720 code++; 721 if (code->op != FETCH_OP_NOP) { 722 trace_probe_log_err(offset, TOO_MANY_OPS); 723 goto fail; 724 } 725 code->op = FETCH_OP_LP_ARRAY; 726 code->param = parg->count; 727 } 728 code++; 729 code->op = FETCH_OP_END; 730 731 ret = 0; 732 /* Shrink down the code buffer */ 733 parg->code = kcalloc(code - tmp + 1, sizeof(*code), GFP_KERNEL); 734 if (!parg->code) 735 ret = -ENOMEM; 736 else 737 memcpy(parg->code, tmp, sizeof(*code) * (code - tmp + 1)); 738 739fail: 740 if (ret) { 741 for (code = tmp; code < tmp + FETCH_INSN_MAX; code++) 742 if (code->op == FETCH_NOP_SYMBOL || 743 code->op == FETCH_OP_DATA) 744 kfree(code->data); 745 } 746 kfree(tmp); 747out: 748 kfree(arg); 749 750 return ret; 751} 752 753/* Return 1 if name is reserved or already used by another argument */ 754static int traceprobe_conflict_field_name(const char *name, 755 struct probe_arg *args, int narg) 756{ 757 int i; 758 759 for (i = 0; i < ARRAY_SIZE(reserved_field_names); i++) 760 if (strcmp(reserved_field_names[i], name) == 0) 761 return 1; 762 763 for (i = 0; i < narg; i++) 764 if (strcmp(args[i].name, name) == 0) 765 return 1; 766 767 return 0; 768} 769 770int traceprobe_parse_probe_arg(struct trace_probe *tp, int i, const char *arg, 771 unsigned int flags) 772{ 773 struct probe_arg *parg = &tp->args[i]; 774 const char *body; 775 776 /* Increment count for freeing args in error case */ 777 tp->nr_args++; 778 779 body = strchr(arg, '='); 780 if (body) { 781 if (body - arg > MAX_ARG_NAME_LEN) { 782 trace_probe_log_err(0, ARG_NAME_TOO_LONG); 783 return -EINVAL; 784 } else if (body == arg) { 785 trace_probe_log_err(0, NO_ARG_NAME); 786 return -EINVAL; 787 } 788 parg->name = kmemdup_nul(arg, body - arg, GFP_KERNEL); 789 body++; 790 } else { 791 /* If argument name is omitted, set "argN" */ 792 parg->name = kasprintf(GFP_KERNEL, "arg%d", i + 1); 793 body = arg; 794 } 795 if (!parg->name) 796 return -ENOMEM; 797 798 if (!is_good_name(parg->name)) { 799 trace_probe_log_err(0, BAD_ARG_NAME); 800 return -EINVAL; 801 } 802 if (traceprobe_conflict_field_name(parg->name, tp->args, i)) { 803 trace_probe_log_err(0, USED_ARG_NAME); 804 return -EINVAL; 805 } 806 /* Parse fetch argument */ 807 return traceprobe_parse_probe_arg_body(body, &tp->size, parg, flags, 808 body - arg); 809} 810 811void traceprobe_free_probe_arg(struct probe_arg *arg) 812{ 813 struct fetch_insn *code = arg->code; 814 815 while (code && code->op != FETCH_OP_END) { 816 if (code->op == FETCH_NOP_SYMBOL || 817 code->op == FETCH_OP_DATA) 818 kfree(code->data); 819 code++; 820 } 821 kfree(arg->code); 822 kfree(arg->name); 823 kfree(arg->comm); 824 kfree(arg->fmt); 825} 826 827int traceprobe_update_arg(struct probe_arg *arg) 828{ 829 struct fetch_insn *code = arg->code; 830 long offset; 831 char *tmp; 832 char c; 833 int ret = 0; 834 835 while (code && code->op != FETCH_OP_END) { 836 if (code->op == FETCH_NOP_SYMBOL) { 837 if (code[1].op != FETCH_OP_IMM) 838 return -EINVAL; 839 840 tmp = strpbrk(code->data, "+-"); 841 if (tmp) 842 c = *tmp; 843 ret = traceprobe_split_symbol_offset(code->data, 844 &offset); 845 if (ret) 846 return ret; 847 848 code[1].immediate = 849 (unsigned long)kallsyms_lookup_name(code->data); 850 if (tmp) 851 *tmp = c; 852 if (!code[1].immediate) 853 return -ENOENT; 854 code[1].immediate += offset; 855 } 856 code++; 857 } 858 return 0; 859} 860 861/* When len=0, we just calculate the needed length */ 862#define LEN_OR_ZERO (len ? len - pos : 0) 863static int __set_print_fmt(struct trace_probe *tp, char *buf, int len, 864 enum probe_print_type ptype) 865{ 866 struct probe_arg *parg; 867 int i, j; 868 int pos = 0; 869 const char *fmt, *arg; 870 871 switch (ptype) { 872 case PROBE_PRINT_NORMAL: 873 fmt = "(%lx)"; 874 arg = ", REC->" FIELD_STRING_IP; 875 break; 876 case PROBE_PRINT_RETURN: 877 fmt = "(%lx <- %lx)"; 878 arg = ", REC->" FIELD_STRING_FUNC ", REC->" FIELD_STRING_RETIP; 879 break; 880 case PROBE_PRINT_EVENT: 881 fmt = ""; 882 arg = ""; 883 break; 884 default: 885 WARN_ON_ONCE(1); 886 return 0; 887 } 888 889 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", fmt); 890 891 for (i = 0; i < tp->nr_args; i++) { 892 parg = tp->args + i; 893 pos += snprintf(buf + pos, LEN_OR_ZERO, " %s=", parg->name); 894 if (parg->count) { 895 pos += snprintf(buf + pos, LEN_OR_ZERO, "{%s", 896 parg->type->fmt); 897 for (j = 1; j < parg->count; j++) 898 pos += snprintf(buf + pos, LEN_OR_ZERO, ",%s", 899 parg->type->fmt); 900 pos += snprintf(buf + pos, LEN_OR_ZERO, "}"); 901 } else 902 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s", 903 parg->type->fmt); 904 } 905 906 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"%s", arg); 907 908 for (i = 0; i < tp->nr_args; i++) { 909 parg = tp->args + i; 910 if (parg->count) { 911 if ((strcmp(parg->type->name, "string") == 0) || 912 (strcmp(parg->type->name, "ustring") == 0)) 913 fmt = ", __get_str(%s[%d])"; 914 else 915 fmt = ", REC->%s[%d]"; 916 for (j = 0; j < parg->count; j++) 917 pos += snprintf(buf + pos, LEN_OR_ZERO, 918 fmt, parg->name, j); 919 } else { 920 if ((strcmp(parg->type->name, "string") == 0) || 921 (strcmp(parg->type->name, "ustring") == 0)) 922 fmt = ", __get_str(%s)"; 923 else 924 fmt = ", REC->%s"; 925 pos += snprintf(buf + pos, LEN_OR_ZERO, 926 fmt, parg->name); 927 } 928 } 929 930 /* return the length of print_fmt */ 931 return pos; 932} 933#undef LEN_OR_ZERO 934 935int traceprobe_set_print_fmt(struct trace_probe *tp, enum probe_print_type ptype) 936{ 937 struct trace_event_call *call = trace_probe_event_call(tp); 938 int len; 939 char *print_fmt; 940 941 /* First: called with 0 length to calculate the needed length */ 942 len = __set_print_fmt(tp, NULL, 0, ptype); 943 print_fmt = kmalloc(len + 1, GFP_KERNEL); 944 if (!print_fmt) 945 return -ENOMEM; 946 947 /* Second: actually write the @print_fmt */ 948 __set_print_fmt(tp, print_fmt, len + 1, ptype); 949 call->print_fmt = print_fmt; 950 951 return 0; 952} 953 954int traceprobe_define_arg_fields(struct trace_event_call *event_call, 955 size_t offset, struct trace_probe *tp) 956{ 957 int ret, i; 958 959 /* Set argument names as fields */ 960 for (i = 0; i < tp->nr_args; i++) { 961 struct probe_arg *parg = &tp->args[i]; 962 const char *fmt = parg->type->fmttype; 963 int size = parg->type->size; 964 965 if (parg->fmt) 966 fmt = parg->fmt; 967 if (parg->count) 968 size *= parg->count; 969 ret = trace_define_field(event_call, fmt, parg->name, 970 offset + parg->offset, size, 971 parg->type->is_signed, 972 FILTER_OTHER); 973 if (ret) 974 return ret; 975 } 976 return 0; 977} 978 979static void trace_probe_event_free(struct trace_probe_event *tpe) 980{ 981 kfree(tpe->class.system); 982 kfree(tpe->call.name); 983 kfree(tpe->call.print_fmt); 984 kfree(tpe); 985} 986 987int trace_probe_append(struct trace_probe *tp, struct trace_probe *to) 988{ 989 if (trace_probe_has_sibling(tp)) 990 return -EBUSY; 991 992 list_del_init(&tp->list); 993 trace_probe_event_free(tp->event); 994 995 tp->event = to->event; 996 list_add_tail(&tp->list, trace_probe_probe_list(to)); 997 998 return 0; 999} 1000 1001void trace_probe_unlink(struct trace_probe *tp) 1002{ 1003 list_del_init(&tp->list); 1004 if (list_empty(trace_probe_probe_list(tp))) 1005 trace_probe_event_free(tp->event); 1006 tp->event = NULL; 1007} 1008 1009void trace_probe_cleanup(struct trace_probe *tp) 1010{ 1011 int i; 1012 1013 for (i = 0; i < tp->nr_args; i++) 1014 traceprobe_free_probe_arg(&tp->args[i]); 1015 1016 if (tp->event) 1017 trace_probe_unlink(tp); 1018} 1019 1020int trace_probe_init(struct trace_probe *tp, const char *event, 1021 const char *group, bool alloc_filter) 1022{ 1023 struct trace_event_call *call; 1024 size_t size = sizeof(struct trace_probe_event); 1025 int ret = 0; 1026 1027 if (!event || !group) 1028 return -EINVAL; 1029 1030 if (alloc_filter) 1031 size += sizeof(struct trace_uprobe_filter); 1032 1033 tp->event = kzalloc(size, GFP_KERNEL); 1034 if (!tp->event) 1035 return -ENOMEM; 1036 1037 INIT_LIST_HEAD(&tp->event->files); 1038 INIT_LIST_HEAD(&tp->event->class.fields); 1039 INIT_LIST_HEAD(&tp->event->probes); 1040 INIT_LIST_HEAD(&tp->list); 1041 list_add(&tp->list, &tp->event->probes); 1042 1043 call = trace_probe_event_call(tp); 1044 call->class = &tp->event->class; 1045 call->name = kstrdup(event, GFP_KERNEL); 1046 if (!call->name) { 1047 ret = -ENOMEM; 1048 goto error; 1049 } 1050 1051 tp->event->class.system = kstrdup(group, GFP_KERNEL); 1052 if (!tp->event->class.system) { 1053 ret = -ENOMEM; 1054 goto error; 1055 } 1056 1057 return 0; 1058 1059error: 1060 trace_probe_cleanup(tp); 1061 return ret; 1062} 1063 1064static struct trace_event_call * 1065find_trace_event_call(const char *system, const char *event_name) 1066{ 1067 struct trace_event_call *tp_event; 1068 const char *name; 1069 1070 list_for_each_entry(tp_event, &ftrace_events, list) { 1071 if (!tp_event->class->system || 1072 strcmp(system, tp_event->class->system)) 1073 continue; 1074 name = trace_event_name(tp_event); 1075 if (!name || strcmp(event_name, name)) 1076 continue; 1077 return tp_event; 1078 } 1079 1080 return NULL; 1081} 1082 1083int trace_probe_register_event_call(struct trace_probe *tp) 1084{ 1085 struct trace_event_call *call = trace_probe_event_call(tp); 1086 int ret; 1087 1088 lockdep_assert_held(&event_mutex); 1089 1090 if (find_trace_event_call(trace_probe_group_name(tp), 1091 trace_probe_name(tp))) 1092 return -EEXIST; 1093 1094 ret = register_trace_event(&call->event); 1095 if (!ret) 1096 return -ENODEV; 1097 1098 ret = trace_add_event_call(call); 1099 if (ret) 1100 unregister_trace_event(&call->event); 1101 1102 return ret; 1103} 1104 1105int trace_probe_add_file(struct trace_probe *tp, struct trace_event_file *file) 1106{ 1107 struct event_file_link *link; 1108 1109 link = kmalloc(sizeof(*link), GFP_KERNEL); 1110 if (!link) 1111 return -ENOMEM; 1112 1113 link->file = file; 1114 INIT_LIST_HEAD(&link->list); 1115 list_add_tail_rcu(&link->list, &tp->event->files); 1116 trace_probe_set_flag(tp, TP_FLAG_TRACE); 1117 return 0; 1118} 1119 1120struct event_file_link *trace_probe_get_file_link(struct trace_probe *tp, 1121 struct trace_event_file *file) 1122{ 1123 struct event_file_link *link; 1124 1125 trace_probe_for_each_link(link, tp) { 1126 if (link->file == file) 1127 return link; 1128 } 1129 1130 return NULL; 1131} 1132 1133int trace_probe_remove_file(struct trace_probe *tp, 1134 struct trace_event_file *file) 1135{ 1136 struct event_file_link *link; 1137 1138 link = trace_probe_get_file_link(tp, file); 1139 if (!link) 1140 return -ENOENT; 1141 1142 list_del_rcu(&link->list); 1143 kvfree_rcu(link); 1144 1145 if (list_empty(&tp->event->files)) 1146 trace_probe_clear_flag(tp, TP_FLAG_TRACE); 1147 1148 return 0; 1149} 1150 1151/* 1152 * Return the smallest index of different type argument (start from 1). 1153 * If all argument types and name are same, return 0. 1154 */ 1155int trace_probe_compare_arg_type(struct trace_probe *a, struct trace_probe *b) 1156{ 1157 int i; 1158 1159 /* In case of more arguments */ 1160 if (a->nr_args < b->nr_args) 1161 return a->nr_args + 1; 1162 if (a->nr_args > b->nr_args) 1163 return b->nr_args + 1; 1164 1165 for (i = 0; i < a->nr_args; i++) { 1166 if ((b->nr_args <= i) || 1167 ((a->args[i].type != b->args[i].type) || 1168 (a->args[i].count != b->args[i].count) || 1169 strcmp(a->args[i].name, b->args[i].name))) 1170 return i + 1; 1171 } 1172 1173 return 0; 1174} 1175 1176bool trace_probe_match_command_args(struct trace_probe *tp, 1177 int argc, const char **argv) 1178{ 1179 char buf[MAX_ARGSTR_LEN + 1]; 1180 int i; 1181 1182 if (tp->nr_args < argc) 1183 return false; 1184 1185 for (i = 0; i < argc; i++) { 1186 snprintf(buf, sizeof(buf), "%s=%s", 1187 tp->args[i].name, tp->args[i].comm); 1188 if (strcmp(buf, argv[i])) 1189 return false; 1190 } 1191 return true; 1192} 1193 1194int trace_probe_create(const char *raw_command, int (*createfn)(int, const char **)) 1195{ 1196 int argc = 0, ret = 0; 1197 char **argv; 1198 1199 argv = argv_split(GFP_KERNEL, raw_command, &argc); 1200 if (!argv) 1201 return -ENOMEM; 1202 1203 if (argc) 1204 ret = createfn(argc, (const char **)argv); 1205 1206 argv_free(argv); 1207 1208 return ret; 1209}