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