1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * probe-finder.c : C expression to kprobe event converter 4 * 5 * Written by Masami Hiramatsu <mhiramat@redhat.com> 6 */ 7 8 #include <inttypes.h> 9 #include <sys/utsname.h> 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 #include <fcntl.h> 13 #include <errno.h> 14 #include <stdio.h> 15 #include <unistd.h> 16 #include <stdlib.h> 17 #include <string.h> 18 #include <stdarg.h> 19 #include <dwarf-regs.h> 20 21 #include <linux/bitops.h> 22 #include <linux/zalloc.h> 23 #include "event.h" 24 #include "dso.h" 25 #include "debug.h" 26 #include "intlist.h" 27 #include "strbuf.h" 28 #include "strlist.h" 29 #include "symbol.h" 30 #include "probe-finder.h" 31 #include "probe-file.h" 32 #include "string2.h" 33 34 /* Kprobe tracer basic type is up to u64 */ 35 #define MAX_BASIC_TYPE_BITS 64 36 37 /* Dwarf FL wrappers */ 38 static char *debuginfo_path; /* Currently dummy */ 39 40 static const Dwfl_Callbacks offline_callbacks = { 41 .find_debuginfo = dwfl_standard_find_debuginfo, 42 .debuginfo_path = &debuginfo_path, 43 44 .section_address = dwfl_offline_section_address, 45 46 /* We use this table for core files too. */ 47 .find_elf = dwfl_build_id_find_elf, 48 }; 49 50 /* Get a Dwarf from offline image */ 51 static int debuginfo__init_offline_dwarf(struct debuginfo *dbg, 52 const char *path) 53 { 54 int fd; 55 56 fd = open(path, O_RDONLY); 57 if (fd < 0) 58 return fd; 59 60 dbg->dwfl = dwfl_begin(&offline_callbacks); 61 if (!dbg->dwfl) 62 goto error; 63 64 dwfl_report_begin(dbg->dwfl); 65 dbg->mod = dwfl_report_offline(dbg->dwfl, "", "", fd); 66 if (!dbg->mod) 67 goto error; 68 69 dbg->dbg = dwfl_module_getdwarf(dbg->mod, &dbg->bias); 70 if (!dbg->dbg) 71 goto error; 72 73 dwfl_report_end(dbg->dwfl, NULL, NULL); 74 75 return 0; 76 error: 77 if (dbg->dwfl) 78 dwfl_end(dbg->dwfl); 79 else 80 close(fd); 81 memset(dbg, 0, sizeof(*dbg)); 82 83 return -ENOENT; 84 } 85 86 static struct debuginfo *__debuginfo__new(const char *path) 87 { 88 struct debuginfo *dbg = zalloc(sizeof(*dbg)); 89 if (!dbg) 90 return NULL; 91 92 if (debuginfo__init_offline_dwarf(dbg, path) < 0) 93 zfree(&dbg); 94 if (dbg) 95 pr_debug("Open Debuginfo file: %s\n", path); 96 return dbg; 97 } 98 99 enum dso_binary_type distro_dwarf_types[] = { 100 DSO_BINARY_TYPE__FEDORA_DEBUGINFO, 101 DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, 102 DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, 103 DSO_BINARY_TYPE__BUILDID_DEBUGINFO, 104 DSO_BINARY_TYPE__NOT_FOUND, 105 }; 106 107 struct debuginfo *debuginfo__new(const char *path) 108 { 109 enum dso_binary_type *type; 110 char buf[PATH_MAX], nil = '\0'; 111 struct dso *dso; 112 struct debuginfo *dinfo = NULL; 113 114 /* Try to open distro debuginfo files */ 115 dso = dso__new(path); 116 if (!dso) 117 goto out; 118 119 for (type = distro_dwarf_types; 120 !dinfo && *type != DSO_BINARY_TYPE__NOT_FOUND; 121 type++) { 122 if (dso__read_binary_type_filename(dso, *type, &nil, 123 buf, PATH_MAX) < 0) 124 continue; 125 dinfo = __debuginfo__new(buf); 126 } 127 dso__put(dso); 128 129 out: 130 /* if failed to open all distro debuginfo, open given binary */ 131 return dinfo ? : __debuginfo__new(path); 132 } 133 134 void debuginfo__delete(struct debuginfo *dbg) 135 { 136 if (dbg) { 137 if (dbg->dwfl) 138 dwfl_end(dbg->dwfl); 139 free(dbg); 140 } 141 } 142 143 /* 144 * Probe finder related functions 145 */ 146 147 static struct probe_trace_arg_ref *alloc_trace_arg_ref(long offs) 148 { 149 struct probe_trace_arg_ref *ref; 150 ref = zalloc(sizeof(struct probe_trace_arg_ref)); 151 if (ref != NULL) 152 ref->offset = offs; 153 return ref; 154 } 155 156 /* 157 * Convert a location into trace_arg. 158 * If tvar == NULL, this just checks variable can be converted. 159 * If fentry == true and vr_die is a parameter, do huristic search 160 * for the location fuzzed by function entry mcount. 161 */ 162 static int convert_variable_location(Dwarf_Die *vr_die, Dwarf_Addr addr, 163 Dwarf_Op *fb_ops, Dwarf_Die *sp_die, 164 unsigned int machine, 165 struct probe_trace_arg *tvar) 166 { 167 Dwarf_Attribute attr; 168 Dwarf_Addr tmp = 0; 169 Dwarf_Op *op; 170 size_t nops; 171 unsigned int regn; 172 Dwarf_Word offs = 0; 173 bool ref = false; 174 const char *regs; 175 int ret, ret2 = 0; 176 177 if (dwarf_attr(vr_die, DW_AT_external, &attr) != NULL) 178 goto static_var; 179 180 /* Constant value */ 181 if (dwarf_attr(vr_die, DW_AT_const_value, &attr) && 182 immediate_value_is_supported()) { 183 Dwarf_Sword snum; 184 185 dwarf_formsdata(&attr, &snum); 186 ret = asprintf(&tvar->value, "\\%ld", (long)snum); 187 188 return ret < 0 ? -ENOMEM : 0; 189 } 190 191 /* TODO: handle more than 1 exprs */ 192 if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL) 193 return -EINVAL; /* Broken DIE ? */ 194 if (dwarf_getlocation_addr(&attr, addr, &op, &nops, 1) <= 0) { 195 ret = dwarf_entrypc(sp_die, &tmp); 196 if (ret) 197 return -ENOENT; 198 199 if (probe_conf.show_location_range && 200 (dwarf_tag(vr_die) == DW_TAG_variable)) { 201 ret2 = -ERANGE; 202 } else if (addr != tmp || 203 dwarf_tag(vr_die) != DW_TAG_formal_parameter) { 204 return -ENOENT; 205 } 206 207 ret = dwarf_highpc(sp_die, &tmp); 208 if (ret) 209 return -ENOENT; 210 /* 211 * This is fuzzed by fentry mcount. We try to find the 212 * parameter location at the earliest address. 213 */ 214 for (addr += 1; addr <= tmp; addr++) { 215 if (dwarf_getlocation_addr(&attr, addr, &op, 216 &nops, 1) > 0) 217 goto found; 218 } 219 return -ENOENT; 220 } 221 found: 222 if (nops == 0) 223 /* TODO: Support const_value */ 224 return -ENOENT; 225 226 if (op->atom == DW_OP_addr) { 227 static_var: 228 if (!tvar) 229 return ret2; 230 /* Static variables on memory (not stack), make @varname */ 231 ret = strlen(dwarf_diename(vr_die)); 232 tvar->value = zalloc(ret + 2); 233 if (tvar->value == NULL) 234 return -ENOMEM; 235 snprintf(tvar->value, ret + 2, "@%s", dwarf_diename(vr_die)); 236 tvar->ref = alloc_trace_arg_ref((long)offs); 237 if (tvar->ref == NULL) 238 return -ENOMEM; 239 return ret2; 240 } 241 242 /* If this is based on frame buffer, set the offset */ 243 if (op->atom == DW_OP_fbreg) { 244 if (fb_ops == NULL) 245 return -ENOTSUP; 246 ref = true; 247 offs = op->number; 248 op = &fb_ops[0]; 249 } 250 251 if (op->atom >= DW_OP_breg0 && op->atom <= DW_OP_breg31) { 252 regn = op->atom - DW_OP_breg0; 253 offs += op->number; 254 ref = true; 255 } else if (op->atom >= DW_OP_reg0 && op->atom <= DW_OP_reg31) { 256 regn = op->atom - DW_OP_reg0; 257 } else if (op->atom == DW_OP_bregx) { 258 regn = op->number; 259 offs += op->number2; 260 ref = true; 261 } else if (op->atom == DW_OP_regx) { 262 regn = op->number; 263 } else { 264 pr_debug("DW_OP %x is not supported.\n", op->atom); 265 return -ENOTSUP; 266 } 267 268 if (!tvar) 269 return ret2; 270 271 regs = get_dwarf_regstr(regn, machine); 272 if (!regs) { 273 /* This should be a bug in DWARF or this tool */ 274 pr_warning("Mapping for the register number %u " 275 "missing on this architecture.\n", regn); 276 return -ENOTSUP; 277 } 278 279 tvar->value = strdup(regs); 280 if (tvar->value == NULL) 281 return -ENOMEM; 282 283 if (ref) { 284 tvar->ref = alloc_trace_arg_ref((long)offs); 285 if (tvar->ref == NULL) 286 return -ENOMEM; 287 } 288 return ret2; 289 } 290 291 #define BYTES_TO_BITS(nb) ((nb) * BITS_PER_LONG / sizeof(long)) 292 293 static int convert_variable_type(Dwarf_Die *vr_die, 294 struct probe_trace_arg *tvar, 295 const char *cast, bool user_access) 296 { 297 struct probe_trace_arg_ref **ref_ptr = &tvar->ref; 298 Dwarf_Die type; 299 char buf[16]; 300 char sbuf[STRERR_BUFSIZE]; 301 int bsize, boffs, total; 302 int ret; 303 char prefix; 304 305 /* TODO: check all types */ 306 if (cast && strcmp(cast, "string") != 0 && strcmp(cast, "x") != 0 && 307 strcmp(cast, "s") != 0 && strcmp(cast, "u") != 0) { 308 /* Non string type is OK */ 309 /* and respect signedness/hexadecimal cast */ 310 tvar->type = strdup(cast); 311 return (tvar->type == NULL) ? -ENOMEM : 0; 312 } 313 314 bsize = dwarf_bitsize(vr_die); 315 if (bsize > 0) { 316 /* This is a bitfield */ 317 boffs = dwarf_bitoffset(vr_die); 318 total = dwarf_bytesize(vr_die); 319 if (boffs < 0 || total < 0) 320 return -ENOENT; 321 ret = snprintf(buf, 16, "b%d@%d/%zd", bsize, boffs, 322 BYTES_TO_BITS(total)); 323 goto formatted; 324 } 325 326 if (die_get_real_type(vr_die, &type) == NULL) { 327 pr_warning("Failed to get a type information of %s.\n", 328 dwarf_diename(vr_die)); 329 return -ENOENT; 330 } 331 332 pr_debug("%s type is %s.\n", 333 dwarf_diename(vr_die), dwarf_diename(&type)); 334 335 if (cast && (!strcmp(cast, "string") || !strcmp(cast, "ustring"))) { 336 /* String type */ 337 ret = dwarf_tag(&type); 338 if (ret != DW_TAG_pointer_type && 339 ret != DW_TAG_array_type) { 340 pr_warning("Failed to cast into string: " 341 "%s(%s) is not a pointer nor array.\n", 342 dwarf_diename(vr_die), dwarf_diename(&type)); 343 return -EINVAL; 344 } 345 if (die_get_real_type(&type, &type) == NULL) { 346 pr_warning("Failed to get a type" 347 " information.\n"); 348 return -ENOENT; 349 } 350 if (ret == DW_TAG_pointer_type) { 351 while (*ref_ptr) 352 ref_ptr = &(*ref_ptr)->next; 353 /* Add new reference with offset +0 */ 354 *ref_ptr = zalloc(sizeof(struct probe_trace_arg_ref)); 355 if (*ref_ptr == NULL) { 356 pr_warning("Out of memory error\n"); 357 return -ENOMEM; 358 } 359 (*ref_ptr)->user_access = user_access; 360 } 361 if (!die_compare_name(&type, "char") && 362 !die_compare_name(&type, "unsigned char")) { 363 pr_warning("Failed to cast into string: " 364 "%s is not (unsigned) char *.\n", 365 dwarf_diename(vr_die)); 366 return -EINVAL; 367 } 368 tvar->type = strdup(cast); 369 return (tvar->type == NULL) ? -ENOMEM : 0; 370 } 371 372 if (cast && (strcmp(cast, "u") == 0)) 373 prefix = 'u'; 374 else if (cast && (strcmp(cast, "s") == 0)) 375 prefix = 's'; 376 else if (cast && (strcmp(cast, "x") == 0) && 377 probe_type_is_available(PROBE_TYPE_X)) 378 prefix = 'x'; 379 else 380 prefix = die_is_signed_type(&type) ? 's' : 381 probe_type_is_available(PROBE_TYPE_X) ? 'x' : 'u'; 382 383 ret = dwarf_bytesize(&type); 384 if (ret <= 0) 385 /* No size ... try to use default type */ 386 return 0; 387 ret = BYTES_TO_BITS(ret); 388 389 /* Check the bitwidth */ 390 if (ret > MAX_BASIC_TYPE_BITS) { 391 pr_info("%s exceeds max-bitwidth. Cut down to %d bits.\n", 392 dwarf_diename(&type), MAX_BASIC_TYPE_BITS); 393 ret = MAX_BASIC_TYPE_BITS; 394 } 395 ret = snprintf(buf, 16, "%c%d", prefix, ret); 396 397 formatted: 398 if (ret < 0 || ret >= 16) { 399 if (ret >= 16) 400 ret = -E2BIG; 401 pr_warning("Failed to convert variable type: %s\n", 402 str_error_r(-ret, sbuf, sizeof(sbuf))); 403 return ret; 404 } 405 tvar->type = strdup(buf); 406 if (tvar->type == NULL) 407 return -ENOMEM; 408 return 0; 409 } 410 411 static int convert_variable_fields(Dwarf_Die *vr_die, const char *varname, 412 struct perf_probe_arg_field *field, 413 struct probe_trace_arg_ref **ref_ptr, 414 Dwarf_Die *die_mem, bool user_access) 415 { 416 struct probe_trace_arg_ref *ref = *ref_ptr; 417 Dwarf_Die type; 418 Dwarf_Word offs; 419 int ret, tag; 420 421 pr_debug("converting %s in %s\n", field->name, varname); 422 if (die_get_real_type(vr_die, &type) == NULL) { 423 pr_warning("Failed to get the type of %s.\n", varname); 424 return -ENOENT; 425 } 426 pr_debug2("Var real type: %s (%x)\n", dwarf_diename(&type), 427 (unsigned)dwarf_dieoffset(&type)); 428 tag = dwarf_tag(&type); 429 430 if (field->name[0] == '[' && 431 (tag == DW_TAG_array_type || tag == DW_TAG_pointer_type)) { 432 /* Save original type for next field or type */ 433 memcpy(die_mem, &type, sizeof(*die_mem)); 434 /* Get the type of this array */ 435 if (die_get_real_type(&type, &type) == NULL) { 436 pr_warning("Failed to get the type of %s.\n", varname); 437 return -ENOENT; 438 } 439 pr_debug2("Array real type: %s (%x)\n", dwarf_diename(&type), 440 (unsigned)dwarf_dieoffset(&type)); 441 if (tag == DW_TAG_pointer_type) { 442 ref = zalloc(sizeof(struct probe_trace_arg_ref)); 443 if (ref == NULL) 444 return -ENOMEM; 445 if (*ref_ptr) 446 (*ref_ptr)->next = ref; 447 else 448 *ref_ptr = ref; 449 } 450 ref->offset += dwarf_bytesize(&type) * field->index; 451 ref->user_access = user_access; 452 goto next; 453 } else if (tag == DW_TAG_pointer_type) { 454 /* Check the pointer and dereference */ 455 if (!field->ref) { 456 pr_err("Semantic error: %s must be referred by '->'\n", 457 field->name); 458 return -EINVAL; 459 } 460 /* Get the type pointed by this pointer */ 461 if (die_get_real_type(&type, &type) == NULL) { 462 pr_warning("Failed to get the type of %s.\n", varname); 463 return -ENOENT; 464 } 465 /* Verify it is a data structure */ 466 tag = dwarf_tag(&type); 467 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { 468 pr_warning("%s is not a data structure nor a union.\n", 469 varname); 470 return -EINVAL; 471 } 472 473 ref = zalloc(sizeof(struct probe_trace_arg_ref)); 474 if (ref == NULL) 475 return -ENOMEM; 476 if (*ref_ptr) 477 (*ref_ptr)->next = ref; 478 else 479 *ref_ptr = ref; 480 } else { 481 /* Verify it is a data structure */ 482 if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) { 483 pr_warning("%s is not a data structure nor a union.\n", 484 varname); 485 return -EINVAL; 486 } 487 if (field->name[0] == '[') { 488 pr_err("Semantic error: %s is not a pointer" 489 " nor array.\n", varname); 490 return -EINVAL; 491 } 492 /* While prcessing unnamed field, we don't care about this */ 493 if (field->ref && dwarf_diename(vr_die)) { 494 pr_err("Semantic error: %s must be referred by '.'\n", 495 field->name); 496 return -EINVAL; 497 } 498 if (!ref) { 499 pr_warning("Structure on a register is not " 500 "supported yet.\n"); 501 return -ENOTSUP; 502 } 503 } 504 505 if (die_find_member(&type, field->name, die_mem) == NULL) { 506 pr_warning("%s(type:%s) has no member %s.\n", varname, 507 dwarf_diename(&type), field->name); 508 return -EINVAL; 509 } 510 511 /* Get the offset of the field */ 512 if (tag == DW_TAG_union_type) { 513 offs = 0; 514 } else { 515 ret = die_get_data_member_location(die_mem, &offs); 516 if (ret < 0) { 517 pr_warning("Failed to get the offset of %s.\n", 518 field->name); 519 return ret; 520 } 521 } 522 ref->offset += (long)offs; 523 ref->user_access = user_access; 524 525 /* If this member is unnamed, we need to reuse this field */ 526 if (!dwarf_diename(die_mem)) 527 return convert_variable_fields(die_mem, varname, field, 528 &ref, die_mem, user_access); 529 530 next: 531 /* Converting next field */ 532 if (field->next) 533 return convert_variable_fields(die_mem, field->name, 534 field->next, &ref, die_mem, user_access); 535 else 536 return 0; 537 } 538 539 static void print_var_not_found(const char *varname) 540 { 541 pr_err("Failed to find the location of the '%s' variable at this address.\n" 542 " Perhaps it has been optimized out.\n" 543 " Use -V with the --range option to show '%s' location range.\n", 544 varname, varname); 545 } 546 547 /* Show a variables in kprobe event format */ 548 static int convert_variable(Dwarf_Die *vr_die, struct probe_finder *pf) 549 { 550 Dwarf_Die die_mem; 551 int ret; 552 553 pr_debug("Converting variable %s into trace event.\n", 554 dwarf_diename(vr_die)); 555 556 ret = convert_variable_location(vr_die, pf->addr, pf->fb_ops, 557 &pf->sp_die, pf->machine, pf->tvar); 558 if (ret == -ENOENT && pf->skip_empty_arg) 559 /* This can be found in other place. skip it */ 560 return 0; 561 if (ret == -ENOENT || ret == -EINVAL) { 562 print_var_not_found(pf->pvar->var); 563 } else if (ret == -ENOTSUP) 564 pr_err("Sorry, we don't support this variable location yet.\n"); 565 else if (ret == 0 && pf->pvar->field) { 566 ret = convert_variable_fields(vr_die, pf->pvar->var, 567 pf->pvar->field, &pf->tvar->ref, 568 &die_mem, pf->pvar->user_access); 569 vr_die = &die_mem; 570 } 571 if (ret == 0) 572 ret = convert_variable_type(vr_die, pf->tvar, pf->pvar->type, 573 pf->pvar->user_access); 574 /* *expr will be cached in libdw. Don't free it. */ 575 return ret; 576 } 577 578 /* Find a variable in a scope DIE */ 579 static int find_variable(Dwarf_Die *sc_die, struct probe_finder *pf) 580 { 581 Dwarf_Die vr_die; 582 char *buf, *ptr; 583 int ret = 0; 584 585 /* Copy raw parameters */ 586 if (!is_c_varname(pf->pvar->var)) 587 return copy_to_probe_trace_arg(pf->tvar, pf->pvar); 588 589 if (pf->pvar->name) 590 pf->tvar->name = strdup(pf->pvar->name); 591 else { 592 buf = synthesize_perf_probe_arg(pf->pvar); 593 if (!buf) 594 return -ENOMEM; 595 ptr = strchr(buf, ':'); /* Change type separator to _ */ 596 if (ptr) 597 *ptr = '_'; 598 pf->tvar->name = buf; 599 } 600 if (pf->tvar->name == NULL) 601 return -ENOMEM; 602 603 pr_debug("Searching '%s' variable in context.\n", pf->pvar->var); 604 /* Search child die for local variables and parameters. */ 605 if (!die_find_variable_at(sc_die, pf->pvar->var, pf->addr, &vr_die)) { 606 /* Search again in global variables */ 607 if (!die_find_variable_at(&pf->cu_die, pf->pvar->var, 608 0, &vr_die)) { 609 if (pf->skip_empty_arg) 610 return 0; 611 pr_warning("Failed to find '%s' in this function.\n", 612 pf->pvar->var); 613 ret = -ENOENT; 614 } 615 } 616 if (ret >= 0) 617 ret = convert_variable(&vr_die, pf); 618 619 return ret; 620 } 621 622 /* Convert subprogram DIE to trace point */ 623 static int convert_to_trace_point(Dwarf_Die *sp_die, Dwfl_Module *mod, 624 Dwarf_Addr paddr, bool retprobe, 625 const char *function, 626 struct probe_trace_point *tp) 627 { 628 Dwarf_Addr eaddr; 629 GElf_Sym sym; 630 const char *symbol; 631 632 /* Verify the address is correct */ 633 if (!dwarf_haspc(sp_die, paddr)) { 634 pr_warning("Specified offset is out of %s\n", 635 dwarf_diename(sp_die)); 636 return -EINVAL; 637 } 638 639 /* Try to get actual symbol name from symtab */ 640 symbol = dwfl_module_addrsym(mod, paddr, &sym, NULL); 641 if (!symbol) { 642 pr_warning("Failed to find symbol at 0x%lx\n", 643 (unsigned long)paddr); 644 return -ENOENT; 645 } 646 eaddr = sym.st_value; 647 648 tp->offset = (unsigned long)(paddr - eaddr); 649 tp->address = (unsigned long)paddr; 650 tp->symbol = strdup(symbol); 651 if (!tp->symbol) 652 return -ENOMEM; 653 654 /* Return probe must be on the head of a subprogram */ 655 if (retprobe) { 656 if (eaddr != paddr) { 657 pr_warning("Failed to find \"%s%%return\",\n" 658 " because %s is an inlined function and" 659 " has no return point.\n", function, 660 function); 661 return -EINVAL; 662 } 663 tp->retprobe = true; 664 } 665 666 return 0; 667 } 668 669 /* Call probe_finder callback with scope DIE */ 670 static int call_probe_finder(Dwarf_Die *sc_die, struct probe_finder *pf) 671 { 672 Dwarf_Attribute fb_attr; 673 Dwarf_Frame *frame = NULL; 674 size_t nops; 675 int ret; 676 677 if (!sc_die) { 678 pr_err("Caller must pass a scope DIE. Program error.\n"); 679 return -EINVAL; 680 } 681 682 /* If not a real subprogram, find a real one */ 683 if (!die_is_func_def(sc_die)) { 684 if (!die_find_realfunc(&pf->cu_die, pf->addr, &pf->sp_die)) { 685 if (die_find_tailfunc(&pf->cu_die, pf->addr, &pf->sp_die)) { 686 pr_warning("Ignoring tail call from %s\n", 687 dwarf_diename(&pf->sp_die)); 688 return 0; 689 } else { 690 pr_warning("Failed to find probe point in any " 691 "functions.\n"); 692 return -ENOENT; 693 } 694 } 695 } else 696 memcpy(&pf->sp_die, sc_die, sizeof(Dwarf_Die)); 697 698 /* Get the frame base attribute/ops from subprogram */ 699 dwarf_attr(&pf->sp_die, DW_AT_frame_base, &fb_attr); 700 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); 701 if (ret <= 0 || nops == 0) { 702 pf->fb_ops = NULL; 703 #if _ELFUTILS_PREREQ(0, 142) 704 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && 705 (pf->cfi_eh != NULL || pf->cfi_dbg != NULL)) { 706 if ((dwarf_cfi_addrframe(pf->cfi_eh, pf->addr, &frame) != 0 && 707 (dwarf_cfi_addrframe(pf->cfi_dbg, pf->addr, &frame) != 0)) || 708 dwarf_frame_cfa(frame, &pf->fb_ops, &nops) != 0) { 709 pr_warning("Failed to get call frame on 0x%jx\n", 710 (uintmax_t)pf->addr); 711 free(frame); 712 return -ENOENT; 713 } 714 #endif 715 } 716 717 /* Call finder's callback handler */ 718 ret = pf->callback(sc_die, pf); 719 720 /* Since *pf->fb_ops can be a part of frame. we should free it here. */ 721 free(frame); 722 pf->fb_ops = NULL; 723 724 return ret; 725 } 726 727 struct find_scope_param { 728 const char *function; 729 const char *file; 730 int line; 731 int diff; 732 Dwarf_Die *die_mem; 733 bool found; 734 }; 735 736 static int find_best_scope_cb(Dwarf_Die *fn_die, void *data) 737 { 738 struct find_scope_param *fsp = data; 739 const char *file; 740 int lno; 741 742 /* Skip if declared file name does not match */ 743 if (fsp->file) { 744 file = dwarf_decl_file(fn_die); 745 if (!file || strcmp(fsp->file, file) != 0) 746 return 0; 747 } 748 /* If the function name is given, that's what user expects */ 749 if (fsp->function) { 750 if (die_match_name(fn_die, fsp->function)) { 751 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die)); 752 fsp->found = true; 753 return 1; 754 } 755 } else { 756 /* With the line number, find the nearest declared DIE */ 757 dwarf_decl_line(fn_die, &lno); 758 if (lno < fsp->line && fsp->diff > fsp->line - lno) { 759 /* Keep a candidate and continue */ 760 fsp->diff = fsp->line - lno; 761 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die)); 762 fsp->found = true; 763 } 764 } 765 return 0; 766 } 767 768 /* Return innermost DIE */ 769 static int find_inner_scope_cb(Dwarf_Die *fn_die, void *data) 770 { 771 struct find_scope_param *fsp = data; 772 773 memcpy(fsp->die_mem, fn_die, sizeof(Dwarf_Die)); 774 fsp->found = true; 775 return 1; 776 } 777 778 /* Find an appropriate scope fits to given conditions */ 779 static Dwarf_Die *find_best_scope(struct probe_finder *pf, Dwarf_Die *die_mem) 780 { 781 struct find_scope_param fsp = { 782 .function = pf->pev->point.function, 783 .file = pf->fname, 784 .line = pf->lno, 785 .diff = INT_MAX, 786 .die_mem = die_mem, 787 .found = false, 788 }; 789 int ret; 790 791 ret = cu_walk_functions_at(&pf->cu_die, pf->addr, find_best_scope_cb, 792 &fsp); 793 if (!ret && !fsp.found) 794 cu_walk_functions_at(&pf->cu_die, pf->addr, 795 find_inner_scope_cb, &fsp); 796 797 return fsp.found ? die_mem : NULL; 798 } 799 800 static int verify_representive_line(struct probe_finder *pf, const char *fname, 801 int lineno, Dwarf_Addr addr) 802 { 803 const char *__fname, *__func = NULL; 804 Dwarf_Die die_mem; 805 int __lineno; 806 807 /* Verify line number and address by reverse search */ 808 if (cu_find_lineinfo(&pf->cu_die, addr, &__fname, &__lineno) < 0) 809 return 0; 810 811 pr_debug2("Reversed line: %s:%d\n", __fname, __lineno); 812 if (strcmp(fname, __fname) || lineno == __lineno) 813 return 0; 814 815 pr_warning("This line is sharing the address with other lines.\n"); 816 817 if (pf->pev->point.function) { 818 /* Find best match function name and lines */ 819 pf->addr = addr; 820 if (find_best_scope(pf, &die_mem) 821 && die_match_name(&die_mem, pf->pev->point.function) 822 && dwarf_decl_line(&die_mem, &lineno) == 0) { 823 __func = dwarf_diename(&die_mem); 824 __lineno -= lineno; 825 } 826 } 827 pr_warning("Please try to probe at %s:%d instead.\n", 828 __func ? : __fname, __lineno); 829 830 return -ENOENT; 831 } 832 833 static int probe_point_line_walker(const char *fname, int lineno, 834 Dwarf_Addr addr, void *data) 835 { 836 struct probe_finder *pf = data; 837 Dwarf_Die *sc_die, die_mem; 838 int ret; 839 840 if (lineno != pf->lno || strtailcmp(fname, pf->fname) != 0) 841 return 0; 842 843 if (verify_representive_line(pf, fname, lineno, addr)) 844 return -ENOENT; 845 846 pf->addr = addr; 847 sc_die = find_best_scope(pf, &die_mem); 848 if (!sc_die) { 849 pr_warning("Failed to find scope of probe point.\n"); 850 return -ENOENT; 851 } 852 853 ret = call_probe_finder(sc_die, pf); 854 855 /* Continue if no error, because the line will be in inline function */ 856 return ret < 0 ? ret : 0; 857 } 858 859 /* Find probe point from its line number */ 860 static int find_probe_point_by_line(struct probe_finder *pf) 861 { 862 return die_walk_lines(&pf->cu_die, probe_point_line_walker, pf); 863 } 864 865 /* Find lines which match lazy pattern */ 866 static int find_lazy_match_lines(struct intlist *list, 867 const char *fname, const char *pat) 868 { 869 FILE *fp; 870 char *line = NULL; 871 size_t line_len; 872 ssize_t len; 873 int count = 0, linenum = 1; 874 char sbuf[STRERR_BUFSIZE]; 875 876 fp = fopen(fname, "r"); 877 if (!fp) { 878 pr_warning("Failed to open %s: %s\n", fname, 879 str_error_r(errno, sbuf, sizeof(sbuf))); 880 return -errno; 881 } 882 883 while ((len = getline(&line, &line_len, fp)) > 0) { 884 885 if (line[len - 1] == '\n') 886 line[len - 1] = '\0'; 887 888 if (strlazymatch(line, pat)) { 889 intlist__add(list, linenum); 890 count++; 891 } 892 linenum++; 893 } 894 895 if (ferror(fp)) 896 count = -errno; 897 free(line); 898 fclose(fp); 899 900 if (count == 0) 901 pr_debug("No matched lines found in %s.\n", fname); 902 return count; 903 } 904 905 static int probe_point_lazy_walker(const char *fname, int lineno, 906 Dwarf_Addr addr, void *data) 907 { 908 struct probe_finder *pf = data; 909 Dwarf_Die *sc_die, die_mem; 910 int ret; 911 912 if (!intlist__has_entry(pf->lcache, lineno) || 913 strtailcmp(fname, pf->fname) != 0) 914 return 0; 915 916 pr_debug("Probe line found: line:%d addr:0x%llx\n", 917 lineno, (unsigned long long)addr); 918 pf->addr = addr; 919 pf->lno = lineno; 920 sc_die = find_best_scope(pf, &die_mem); 921 if (!sc_die) { 922 pr_warning("Failed to find scope of probe point.\n"); 923 return -ENOENT; 924 } 925 926 ret = call_probe_finder(sc_die, pf); 927 928 /* 929 * Continue if no error, because the lazy pattern will match 930 * to other lines 931 */ 932 return ret < 0 ? ret : 0; 933 } 934 935 /* Find probe points from lazy pattern */ 936 static int find_probe_point_lazy(Dwarf_Die *sp_die, struct probe_finder *pf) 937 { 938 int ret = 0; 939 char *fpath; 940 941 if (intlist__empty(pf->lcache)) { 942 const char *comp_dir; 943 944 comp_dir = cu_get_comp_dir(&pf->cu_die); 945 ret = get_real_path(pf->fname, comp_dir, &fpath); 946 if (ret < 0) { 947 pr_warning("Failed to find source file path.\n"); 948 return ret; 949 } 950 951 /* Matching lazy line pattern */ 952 ret = find_lazy_match_lines(pf->lcache, fpath, 953 pf->pev->point.lazy_line); 954 free(fpath); 955 if (ret <= 0) 956 return ret; 957 } 958 959 return die_walk_lines(sp_die, probe_point_lazy_walker, pf); 960 } 961 962 static void skip_prologue(Dwarf_Die *sp_die, struct probe_finder *pf) 963 { 964 struct perf_probe_point *pp = &pf->pev->point; 965 966 /* Not uprobe? */ 967 if (!pf->pev->uprobes) 968 return; 969 970 /* Compiled with optimization? */ 971 if (die_is_optimized_target(&pf->cu_die)) 972 return; 973 974 /* Don't know entrypc? */ 975 if (!pf->addr) 976 return; 977 978 /* Only FUNC and FUNC@SRC are eligible. */ 979 if (!pp->function || pp->line || pp->retprobe || pp->lazy_line || 980 pp->offset || pp->abs_address) 981 return; 982 983 /* Not interested in func parameter? */ 984 if (!perf_probe_with_var(pf->pev)) 985 return; 986 987 pr_info("Target program is compiled without optimization. Skipping prologue.\n" 988 "Probe on address 0x%" PRIx64 " to force probing at the function entry.\n\n", 989 pf->addr); 990 991 die_skip_prologue(sp_die, &pf->cu_die, &pf->addr); 992 } 993 994 static int probe_point_inline_cb(Dwarf_Die *in_die, void *data) 995 { 996 struct probe_finder *pf = data; 997 struct perf_probe_point *pp = &pf->pev->point; 998 Dwarf_Addr addr; 999 int ret; 1000 1001 if (pp->lazy_line) 1002 ret = find_probe_point_lazy(in_die, pf); 1003 else { 1004 /* Get probe address */ 1005 if (die_entrypc(in_die, &addr) != 0) { 1006 pr_warning("Failed to get entry address of %s.\n", 1007 dwarf_diename(in_die)); 1008 return -ENOENT; 1009 } 1010 if (addr == 0) { 1011 pr_debug("%s has no valid entry address. skipped.\n", 1012 dwarf_diename(in_die)); 1013 return -ENOENT; 1014 } 1015 pf->addr = addr; 1016 pf->addr += pp->offset; 1017 pr_debug("found inline addr: 0x%jx\n", 1018 (uintmax_t)pf->addr); 1019 1020 ret = call_probe_finder(in_die, pf); 1021 } 1022 1023 return ret; 1024 } 1025 1026 /* Callback parameter with return value for libdw */ 1027 struct dwarf_callback_param { 1028 void *data; 1029 int retval; 1030 }; 1031 1032 /* Search function from function name */ 1033 static int probe_point_search_cb(Dwarf_Die *sp_die, void *data) 1034 { 1035 struct dwarf_callback_param *param = data; 1036 struct probe_finder *pf = param->data; 1037 struct perf_probe_point *pp = &pf->pev->point; 1038 1039 /* Check tag and diename */ 1040 if (!die_is_func_def(sp_die) || 1041 !die_match_name(sp_die, pp->function)) 1042 return DWARF_CB_OK; 1043 1044 /* Check declared file */ 1045 if (pp->file && strtailcmp(pp->file, dwarf_decl_file(sp_die))) 1046 return DWARF_CB_OK; 1047 1048 pr_debug("Matched function: %s [%lx]\n", dwarf_diename(sp_die), 1049 (unsigned long)dwarf_dieoffset(sp_die)); 1050 pf->fname = dwarf_decl_file(sp_die); 1051 if (pp->line) { /* Function relative line */ 1052 dwarf_decl_line(sp_die, &pf->lno); 1053 pf->lno += pp->line; 1054 param->retval = find_probe_point_by_line(pf); 1055 } else if (die_is_func_instance(sp_die)) { 1056 /* Instances always have the entry address */ 1057 die_entrypc(sp_die, &pf->addr); 1058 /* But in some case the entry address is 0 */ 1059 if (pf->addr == 0) { 1060 pr_debug("%s has no entry PC. Skipped\n", 1061 dwarf_diename(sp_die)); 1062 param->retval = 0; 1063 /* Real function */ 1064 } else if (pp->lazy_line) 1065 param->retval = find_probe_point_lazy(sp_die, pf); 1066 else { 1067 skip_prologue(sp_die, pf); 1068 pf->addr += pp->offset; 1069 /* TODO: Check the address in this function */ 1070 param->retval = call_probe_finder(sp_die, pf); 1071 } 1072 } else if (!probe_conf.no_inlines) { 1073 /* Inlined function: search instances */ 1074 param->retval = die_walk_instances(sp_die, 1075 probe_point_inline_cb, (void *)pf); 1076 /* This could be a non-existed inline definition */ 1077 if (param->retval == -ENOENT) 1078 param->retval = 0; 1079 } 1080 1081 /* We need to find other candidates */ 1082 if (strisglob(pp->function) && param->retval >= 0) { 1083 param->retval = 0; /* We have to clear the result */ 1084 return DWARF_CB_OK; 1085 } 1086 1087 return DWARF_CB_ABORT; /* Exit; no same symbol in this CU. */ 1088 } 1089 1090 static int find_probe_point_by_func(struct probe_finder *pf) 1091 { 1092 struct dwarf_callback_param _param = {.data = (void *)pf, 1093 .retval = 0}; 1094 dwarf_getfuncs(&pf->cu_die, probe_point_search_cb, &_param, 0); 1095 return _param.retval; 1096 } 1097 1098 struct pubname_callback_param { 1099 char *function; 1100 char *file; 1101 Dwarf_Die *cu_die; 1102 Dwarf_Die *sp_die; 1103 int found; 1104 }; 1105 1106 static int pubname_search_cb(Dwarf *dbg, Dwarf_Global *gl, void *data) 1107 { 1108 struct pubname_callback_param *param = data; 1109 1110 if (dwarf_offdie(dbg, gl->die_offset, param->sp_die)) { 1111 if (dwarf_tag(param->sp_die) != DW_TAG_subprogram) 1112 return DWARF_CB_OK; 1113 1114 if (die_match_name(param->sp_die, param->function)) { 1115 if (!dwarf_offdie(dbg, gl->cu_offset, param->cu_die)) 1116 return DWARF_CB_OK; 1117 1118 if (param->file && 1119 strtailcmp(param->file, dwarf_decl_file(param->sp_die))) 1120 return DWARF_CB_OK; 1121 1122 param->found = 1; 1123 return DWARF_CB_ABORT; 1124 } 1125 } 1126 1127 return DWARF_CB_OK; 1128 } 1129 1130 static int debuginfo__find_probe_location(struct debuginfo *dbg, 1131 struct probe_finder *pf) 1132 { 1133 struct perf_probe_point *pp = &pf->pev->point; 1134 Dwarf_Off off, noff; 1135 size_t cuhl; 1136 Dwarf_Die *diep; 1137 int ret = 0; 1138 1139 off = 0; 1140 pf->lcache = intlist__new(NULL); 1141 if (!pf->lcache) 1142 return -ENOMEM; 1143 1144 /* Fastpath: lookup by function name from .debug_pubnames section */ 1145 if (pp->function && !strisglob(pp->function)) { 1146 struct pubname_callback_param pubname_param = { 1147 .function = pp->function, 1148 .file = pp->file, 1149 .cu_die = &pf->cu_die, 1150 .sp_die = &pf->sp_die, 1151 .found = 0, 1152 }; 1153 struct dwarf_callback_param probe_param = { 1154 .data = pf, 1155 }; 1156 1157 dwarf_getpubnames(dbg->dbg, pubname_search_cb, 1158 &pubname_param, 0); 1159 if (pubname_param.found) { 1160 ret = probe_point_search_cb(&pf->sp_die, &probe_param); 1161 if (ret) 1162 goto found; 1163 } 1164 } 1165 1166 /* Loop on CUs (Compilation Unit) */ 1167 while (!dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, NULL, NULL, NULL)) { 1168 /* Get the DIE(Debugging Information Entry) of this CU */ 1169 diep = dwarf_offdie(dbg->dbg, off + cuhl, &pf->cu_die); 1170 if (!diep) 1171 continue; 1172 1173 /* Check if target file is included. */ 1174 if (pp->file) 1175 pf->fname = cu_find_realpath(&pf->cu_die, pp->file); 1176 else 1177 pf->fname = NULL; 1178 1179 if (!pp->file || pf->fname) { 1180 if (pp->function) 1181 ret = find_probe_point_by_func(pf); 1182 else if (pp->lazy_line) 1183 ret = find_probe_point_lazy(&pf->cu_die, pf); 1184 else { 1185 pf->lno = pp->line; 1186 ret = find_probe_point_by_line(pf); 1187 } 1188 if (ret < 0) 1189 break; 1190 } 1191 off = noff; 1192 } 1193 1194 found: 1195 intlist__delete(pf->lcache); 1196 pf->lcache = NULL; 1197 1198 return ret; 1199 } 1200 1201 /* Find probe points from debuginfo */ 1202 static int debuginfo__find_probes(struct debuginfo *dbg, 1203 struct probe_finder *pf) 1204 { 1205 int ret = 0; 1206 Elf *elf; 1207 GElf_Ehdr ehdr; 1208 1209 if (pf->cfi_eh || pf->cfi_dbg) 1210 return debuginfo__find_probe_location(dbg, pf); 1211 1212 /* Get the call frame information from this dwarf */ 1213 elf = dwarf_getelf(dbg->dbg); 1214 if (elf == NULL) 1215 return -EINVAL; 1216 1217 if (gelf_getehdr(elf, &ehdr) == NULL) 1218 return -EINVAL; 1219 1220 pf->machine = ehdr.e_machine; 1221 1222 #if _ELFUTILS_PREREQ(0, 142) 1223 do { 1224 GElf_Shdr shdr; 1225 1226 if (elf_section_by_name(elf, &ehdr, &shdr, ".eh_frame", NULL) && 1227 shdr.sh_type == SHT_PROGBITS) 1228 pf->cfi_eh = dwarf_getcfi_elf(elf); 1229 1230 pf->cfi_dbg = dwarf_getcfi(dbg->dbg); 1231 } while (0); 1232 #endif 1233 1234 ret = debuginfo__find_probe_location(dbg, pf); 1235 return ret; 1236 } 1237 1238 struct local_vars_finder { 1239 struct probe_finder *pf; 1240 struct perf_probe_arg *args; 1241 bool vars; 1242 int max_args; 1243 int nargs; 1244 int ret; 1245 }; 1246 1247 /* Collect available variables in this scope */ 1248 static int copy_variables_cb(Dwarf_Die *die_mem, void *data) 1249 { 1250 struct local_vars_finder *vf = data; 1251 struct probe_finder *pf = vf->pf; 1252 int tag; 1253 1254 tag = dwarf_tag(die_mem); 1255 if (tag == DW_TAG_formal_parameter || 1256 (tag == DW_TAG_variable && vf->vars)) { 1257 if (convert_variable_location(die_mem, vf->pf->addr, 1258 vf->pf->fb_ops, &pf->sp_die, 1259 pf->machine, NULL) == 0) { 1260 vf->args[vf->nargs].var = (char *)dwarf_diename(die_mem); 1261 if (vf->args[vf->nargs].var == NULL) { 1262 vf->ret = -ENOMEM; 1263 return DIE_FIND_CB_END; 1264 } 1265 pr_debug(" %s", vf->args[vf->nargs].var); 1266 vf->nargs++; 1267 } 1268 } 1269 1270 if (dwarf_haspc(die_mem, vf->pf->addr)) 1271 return DIE_FIND_CB_CONTINUE; 1272 else 1273 return DIE_FIND_CB_SIBLING; 1274 } 1275 1276 static int expand_probe_args(Dwarf_Die *sc_die, struct probe_finder *pf, 1277 struct perf_probe_arg *args) 1278 { 1279 Dwarf_Die die_mem; 1280 int i; 1281 int n = 0; 1282 struct local_vars_finder vf = {.pf = pf, .args = args, .vars = false, 1283 .max_args = MAX_PROBE_ARGS, .ret = 0}; 1284 1285 for (i = 0; i < pf->pev->nargs; i++) { 1286 /* var never be NULL */ 1287 if (strcmp(pf->pev->args[i].var, PROBE_ARG_VARS) == 0) 1288 vf.vars = true; 1289 else if (strcmp(pf->pev->args[i].var, PROBE_ARG_PARAMS) != 0) { 1290 /* Copy normal argument */ 1291 args[n] = pf->pev->args[i]; 1292 n++; 1293 continue; 1294 } 1295 pr_debug("Expanding %s into:", pf->pev->args[i].var); 1296 vf.nargs = n; 1297 /* Special local variables */ 1298 die_find_child(sc_die, copy_variables_cb, (void *)&vf, 1299 &die_mem); 1300 pr_debug(" (%d)\n", vf.nargs - n); 1301 if (vf.ret < 0) 1302 return vf.ret; 1303 n = vf.nargs; 1304 } 1305 return n; 1306 } 1307 1308 static bool trace_event_finder_overlap(struct trace_event_finder *tf) 1309 { 1310 int i; 1311 1312 for (i = 0; i < tf->ntevs; i++) { 1313 if (tf->pf.addr == tf->tevs[i].point.address) 1314 return true; 1315 } 1316 return false; 1317 } 1318 1319 /* Add a found probe point into trace event list */ 1320 static int add_probe_trace_event(Dwarf_Die *sc_die, struct probe_finder *pf) 1321 { 1322 struct trace_event_finder *tf = 1323 container_of(pf, struct trace_event_finder, pf); 1324 struct perf_probe_point *pp = &pf->pev->point; 1325 struct probe_trace_event *tev; 1326 struct perf_probe_arg *args = NULL; 1327 int ret, i; 1328 1329 /* 1330 * For some reason (e.g. different column assigned to same address) 1331 * This callback can be called with the address which already passed. 1332 * Ignore it first. 1333 */ 1334 if (trace_event_finder_overlap(tf)) 1335 return 0; 1336 1337 /* Check number of tevs */ 1338 if (tf->ntevs == tf->max_tevs) { 1339 pr_warning("Too many( > %d) probe point found.\n", 1340 tf->max_tevs); 1341 return -ERANGE; 1342 } 1343 tev = &tf->tevs[tf->ntevs++]; 1344 1345 /* Trace point should be converted from subprogram DIE */ 1346 ret = convert_to_trace_point(&pf->sp_die, tf->mod, pf->addr, 1347 pp->retprobe, pp->function, &tev->point); 1348 if (ret < 0) 1349 goto end; 1350 1351 tev->point.realname = strdup(dwarf_diename(sc_die)); 1352 if (!tev->point.realname) { 1353 ret = -ENOMEM; 1354 goto end; 1355 } 1356 1357 pr_debug("Probe point found: %s+%lu\n", tev->point.symbol, 1358 tev->point.offset); 1359 1360 /* Expand special probe argument if exist */ 1361 args = zalloc(sizeof(struct perf_probe_arg) * MAX_PROBE_ARGS); 1362 if (args == NULL) { 1363 ret = -ENOMEM; 1364 goto end; 1365 } 1366 1367 ret = expand_probe_args(sc_die, pf, args); 1368 if (ret < 0) 1369 goto end; 1370 1371 tev->nargs = ret; 1372 tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs); 1373 if (tev->args == NULL) { 1374 ret = -ENOMEM; 1375 goto end; 1376 } 1377 1378 /* Find each argument */ 1379 for (i = 0; i < tev->nargs; i++) { 1380 pf->pvar = &args[i]; 1381 pf->tvar = &tev->args[i]; 1382 /* Variable should be found from scope DIE */ 1383 ret = find_variable(sc_die, pf); 1384 if (ret != 0) 1385 break; 1386 } 1387 1388 end: 1389 if (ret) { 1390 clear_probe_trace_event(tev); 1391 tf->ntevs--; 1392 } 1393 free(args); 1394 return ret; 1395 } 1396 1397 static int fill_empty_trace_arg(struct perf_probe_event *pev, 1398 struct probe_trace_event *tevs, int ntevs) 1399 { 1400 char **valp; 1401 char *type; 1402 int i, j, ret; 1403 1404 for (i = 0; i < pev->nargs; i++) { 1405 type = NULL; 1406 for (j = 0; j < ntevs; j++) { 1407 if (tevs[j].args[i].value) { 1408 type = tevs[j].args[i].type; 1409 break; 1410 } 1411 } 1412 if (j == ntevs) { 1413 print_var_not_found(pev->args[i].var); 1414 return -ENOENT; 1415 } 1416 for (j = 0; j < ntevs; j++) { 1417 valp = &tevs[j].args[i].value; 1418 if (*valp) 1419 continue; 1420 1421 ret = asprintf(valp, "\\%lx", probe_conf.magic_num); 1422 if (ret < 0) 1423 return -ENOMEM; 1424 /* Note that type can be NULL */ 1425 if (type) { 1426 tevs[j].args[i].type = strdup(type); 1427 if (!tevs[j].args[i].type) 1428 return -ENOMEM; 1429 } 1430 } 1431 } 1432 return 0; 1433 } 1434 1435 /* Find probe_trace_events specified by perf_probe_event from debuginfo */ 1436 int debuginfo__find_trace_events(struct debuginfo *dbg, 1437 struct perf_probe_event *pev, 1438 struct probe_trace_event **tevs) 1439 { 1440 struct trace_event_finder tf = { 1441 .pf = {.pev = pev, .callback = add_probe_trace_event}, 1442 .max_tevs = probe_conf.max_probes, .mod = dbg->mod}; 1443 int ret, i; 1444 1445 /* Allocate result tevs array */ 1446 *tevs = zalloc(sizeof(struct probe_trace_event) * tf.max_tevs); 1447 if (*tevs == NULL) 1448 return -ENOMEM; 1449 1450 tf.tevs = *tevs; 1451 tf.ntevs = 0; 1452 1453 if (pev->nargs != 0 && immediate_value_is_supported()) 1454 tf.pf.skip_empty_arg = true; 1455 1456 ret = debuginfo__find_probes(dbg, &tf.pf); 1457 if (ret >= 0 && tf.pf.skip_empty_arg) 1458 ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs); 1459 1460 if (ret < 0) { 1461 for (i = 0; i < tf.ntevs; i++) 1462 clear_probe_trace_event(&tf.tevs[i]); 1463 zfree(tevs); 1464 return ret; 1465 } 1466 1467 return (ret < 0) ? ret : tf.ntevs; 1468 } 1469 1470 /* Collect available variables in this scope */ 1471 static int collect_variables_cb(Dwarf_Die *die_mem, void *data) 1472 { 1473 struct available_var_finder *af = data; 1474 struct variable_list *vl; 1475 struct strbuf buf = STRBUF_INIT; 1476 int tag, ret; 1477 1478 vl = &af->vls[af->nvls - 1]; 1479 1480 tag = dwarf_tag(die_mem); 1481 if (tag == DW_TAG_formal_parameter || 1482 tag == DW_TAG_variable) { 1483 ret = convert_variable_location(die_mem, af->pf.addr, 1484 af->pf.fb_ops, &af->pf.sp_die, 1485 af->pf.machine, NULL); 1486 if (ret == 0 || ret == -ERANGE) { 1487 int ret2; 1488 bool externs = !af->child; 1489 1490 if (strbuf_init(&buf, 64) < 0) 1491 goto error; 1492 1493 if (probe_conf.show_location_range) { 1494 if (!externs) 1495 ret2 = strbuf_add(&buf, 1496 ret ? "[INV]\t" : "[VAL]\t", 6); 1497 else 1498 ret2 = strbuf_add(&buf, "[EXT]\t", 6); 1499 if (ret2) 1500 goto error; 1501 } 1502 1503 ret2 = die_get_varname(die_mem, &buf); 1504 1505 if (!ret2 && probe_conf.show_location_range && 1506 !externs) { 1507 if (strbuf_addch(&buf, '\t') < 0) 1508 goto error; 1509 ret2 = die_get_var_range(&af->pf.sp_die, 1510 die_mem, &buf); 1511 } 1512 1513 pr_debug("Add new var: %s\n", buf.buf); 1514 if (ret2 == 0) { 1515 strlist__add(vl->vars, 1516 strbuf_detach(&buf, NULL)); 1517 } 1518 strbuf_release(&buf); 1519 } 1520 } 1521 1522 if (af->child && dwarf_haspc(die_mem, af->pf.addr)) 1523 return DIE_FIND_CB_CONTINUE; 1524 else 1525 return DIE_FIND_CB_SIBLING; 1526 error: 1527 strbuf_release(&buf); 1528 pr_debug("Error in strbuf\n"); 1529 return DIE_FIND_CB_END; 1530 } 1531 1532 static bool available_var_finder_overlap(struct available_var_finder *af) 1533 { 1534 int i; 1535 1536 for (i = 0; i < af->nvls; i++) { 1537 if (af->pf.addr == af->vls[i].point.address) 1538 return true; 1539 } 1540 return false; 1541 1542 } 1543 1544 /* Add a found vars into available variables list */ 1545 static int add_available_vars(Dwarf_Die *sc_die, struct probe_finder *pf) 1546 { 1547 struct available_var_finder *af = 1548 container_of(pf, struct available_var_finder, pf); 1549 struct perf_probe_point *pp = &pf->pev->point; 1550 struct variable_list *vl; 1551 Dwarf_Die die_mem; 1552 int ret; 1553 1554 /* 1555 * For some reason (e.g. different column assigned to same address), 1556 * this callback can be called with the address which already passed. 1557 * Ignore it first. 1558 */ 1559 if (available_var_finder_overlap(af)) 1560 return 0; 1561 1562 /* Check number of tevs */ 1563 if (af->nvls == af->max_vls) { 1564 pr_warning("Too many( > %d) probe point found.\n", af->max_vls); 1565 return -ERANGE; 1566 } 1567 vl = &af->vls[af->nvls++]; 1568 1569 /* Trace point should be converted from subprogram DIE */ 1570 ret = convert_to_trace_point(&pf->sp_die, af->mod, pf->addr, 1571 pp->retprobe, pp->function, &vl->point); 1572 if (ret < 0) 1573 return ret; 1574 1575 pr_debug("Probe point found: %s+%lu\n", vl->point.symbol, 1576 vl->point.offset); 1577 1578 /* Find local variables */ 1579 vl->vars = strlist__new(NULL, NULL); 1580 if (vl->vars == NULL) 1581 return -ENOMEM; 1582 af->child = true; 1583 die_find_child(sc_die, collect_variables_cb, (void *)af, &die_mem); 1584 1585 /* Find external variables */ 1586 if (!probe_conf.show_ext_vars) 1587 goto out; 1588 /* Don't need to search child DIE for external vars. */ 1589 af->child = false; 1590 die_find_child(&pf->cu_die, collect_variables_cb, (void *)af, &die_mem); 1591 1592 out: 1593 if (strlist__empty(vl->vars)) { 1594 strlist__delete(vl->vars); 1595 vl->vars = NULL; 1596 } 1597 1598 return ret; 1599 } 1600 1601 /* 1602 * Find available variables at given probe point 1603 * Return the number of found probe points. Return 0 if there is no 1604 * matched probe point. Return <0 if an error occurs. 1605 */ 1606 int debuginfo__find_available_vars_at(struct debuginfo *dbg, 1607 struct perf_probe_event *pev, 1608 struct variable_list **vls) 1609 { 1610 struct available_var_finder af = { 1611 .pf = {.pev = pev, .callback = add_available_vars}, 1612 .mod = dbg->mod, 1613 .max_vls = probe_conf.max_probes}; 1614 int ret; 1615 1616 /* Allocate result vls array */ 1617 *vls = zalloc(sizeof(struct variable_list) * af.max_vls); 1618 if (*vls == NULL) 1619 return -ENOMEM; 1620 1621 af.vls = *vls; 1622 af.nvls = 0; 1623 1624 ret = debuginfo__find_probes(dbg, &af.pf); 1625 if (ret < 0) { 1626 /* Free vlist for error */ 1627 while (af.nvls--) { 1628 zfree(&af.vls[af.nvls].point.symbol); 1629 strlist__delete(af.vls[af.nvls].vars); 1630 } 1631 zfree(vls); 1632 return ret; 1633 } 1634 1635 return (ret < 0) ? ret : af.nvls; 1636 } 1637 1638 /* For the kernel module, we need a special code to get a DIE */ 1639 int debuginfo__get_text_offset(struct debuginfo *dbg, Dwarf_Addr *offs, 1640 bool adjust_offset) 1641 { 1642 int n, i; 1643 Elf32_Word shndx; 1644 Elf_Scn *scn; 1645 Elf *elf; 1646 GElf_Shdr mem, *shdr; 1647 const char *p; 1648 1649 elf = dwfl_module_getelf(dbg->mod, &dbg->bias); 1650 if (!elf) 1651 return -EINVAL; 1652 1653 /* Get the number of relocations */ 1654 n = dwfl_module_relocations(dbg->mod); 1655 if (n < 0) 1656 return -ENOENT; 1657 /* Search the relocation related .text section */ 1658 for (i = 0; i < n; i++) { 1659 p = dwfl_module_relocation_info(dbg->mod, i, &shndx); 1660 if (strcmp(p, ".text") == 0) { 1661 /* OK, get the section header */ 1662 scn = elf_getscn(elf, shndx); 1663 if (!scn) 1664 return -ENOENT; 1665 shdr = gelf_getshdr(scn, &mem); 1666 if (!shdr) 1667 return -ENOENT; 1668 *offs = shdr->sh_addr; 1669 if (adjust_offset) 1670 *offs -= shdr->sh_offset; 1671 } 1672 } 1673 return 0; 1674 } 1675 1676 /* Reverse search */ 1677 int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr, 1678 struct perf_probe_point *ppt) 1679 { 1680 Dwarf_Die cudie, spdie, indie; 1681 Dwarf_Addr _addr = 0, baseaddr = 0; 1682 const char *fname = NULL, *func = NULL, *basefunc = NULL, *tmp; 1683 int baseline = 0, lineno = 0, ret = 0; 1684 1685 /* We always need to relocate the address for aranges */ 1686 if (debuginfo__get_text_offset(dbg, &baseaddr, false) == 0) 1687 addr += baseaddr; 1688 /* Find cu die */ 1689 if (!dwarf_addrdie(dbg->dbg, (Dwarf_Addr)addr, &cudie)) { 1690 pr_warning("Failed to find debug information for address %lx\n", 1691 addr); 1692 ret = -EINVAL; 1693 goto end; 1694 } 1695 1696 /* Find a corresponding line (filename and lineno) */ 1697 cu_find_lineinfo(&cudie, addr, &fname, &lineno); 1698 /* Don't care whether it failed or not */ 1699 1700 /* Find a corresponding function (name, baseline and baseaddr) */ 1701 if (die_find_realfunc(&cudie, (Dwarf_Addr)addr, &spdie)) { 1702 /* Get function entry information */ 1703 func = basefunc = dwarf_diename(&spdie); 1704 if (!func || 1705 die_entrypc(&spdie, &baseaddr) != 0 || 1706 dwarf_decl_line(&spdie, &baseline) != 0) { 1707 lineno = 0; 1708 goto post; 1709 } 1710 1711 fname = dwarf_decl_file(&spdie); 1712 if (addr == (unsigned long)baseaddr) { 1713 /* Function entry - Relative line number is 0 */ 1714 lineno = baseline; 1715 goto post; 1716 } 1717 1718 /* Track down the inline functions step by step */ 1719 while (die_find_top_inlinefunc(&spdie, (Dwarf_Addr)addr, 1720 &indie)) { 1721 /* There is an inline function */ 1722 if (die_entrypc(&indie, &_addr) == 0 && 1723 _addr == addr) { 1724 /* 1725 * addr is at an inline function entry. 1726 * In this case, lineno should be the call-site 1727 * line number. (overwrite lineinfo) 1728 */ 1729 lineno = die_get_call_lineno(&indie); 1730 fname = die_get_call_file(&indie); 1731 break; 1732 } else { 1733 /* 1734 * addr is in an inline function body. 1735 * Since lineno points one of the lines 1736 * of the inline function, baseline should 1737 * be the entry line of the inline function. 1738 */ 1739 tmp = dwarf_diename(&indie); 1740 if (!tmp || 1741 dwarf_decl_line(&indie, &baseline) != 0) 1742 break; 1743 func = tmp; 1744 spdie = indie; 1745 } 1746 } 1747 /* Verify the lineno and baseline are in a same file */ 1748 tmp = dwarf_decl_file(&spdie); 1749 if (!tmp || strcmp(tmp, fname) != 0) 1750 lineno = 0; 1751 } 1752 1753 post: 1754 /* Make a relative line number or an offset */ 1755 if (lineno) 1756 ppt->line = lineno - baseline; 1757 else if (basefunc) { 1758 ppt->offset = addr - (unsigned long)baseaddr; 1759 func = basefunc; 1760 } 1761 1762 /* Duplicate strings */ 1763 if (func) { 1764 ppt->function = strdup(func); 1765 if (ppt->function == NULL) { 1766 ret = -ENOMEM; 1767 goto end; 1768 } 1769 } 1770 if (fname) { 1771 ppt->file = strdup(fname); 1772 if (ppt->file == NULL) { 1773 zfree(&ppt->function); 1774 ret = -ENOMEM; 1775 goto end; 1776 } 1777 } 1778 end: 1779 if (ret == 0 && (fname || func)) 1780 ret = 1; /* Found a point */ 1781 return ret; 1782 } 1783 1784 /* Add a line and store the src path */ 1785 static int line_range_add_line(const char *src, unsigned int lineno, 1786 struct line_range *lr) 1787 { 1788 /* Copy source path */ 1789 if (!lr->path) { 1790 lr->path = strdup(src); 1791 if (lr->path == NULL) 1792 return -ENOMEM; 1793 } 1794 return intlist__add(lr->line_list, lineno); 1795 } 1796 1797 static int line_range_walk_cb(const char *fname, int lineno, 1798 Dwarf_Addr addr __maybe_unused, 1799 void *data) 1800 { 1801 struct line_finder *lf = data; 1802 const char *__fname; 1803 int __lineno; 1804 int err; 1805 1806 if ((strtailcmp(fname, lf->fname) != 0) || 1807 (lf->lno_s > lineno || lf->lno_e < lineno)) 1808 return 0; 1809 1810 /* Make sure this line can be reversable */ 1811 if (cu_find_lineinfo(&lf->cu_die, addr, &__fname, &__lineno) > 0 1812 && (lineno != __lineno || strcmp(fname, __fname))) 1813 return 0; 1814 1815 err = line_range_add_line(fname, lineno, lf->lr); 1816 if (err < 0 && err != -EEXIST) 1817 return err; 1818 1819 return 0; 1820 } 1821 1822 /* Find line range from its line number */ 1823 static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) 1824 { 1825 int ret; 1826 1827 ret = die_walk_lines(sp_die ?: &lf->cu_die, line_range_walk_cb, lf); 1828 1829 /* Update status */ 1830 if (ret >= 0) 1831 if (!intlist__empty(lf->lr->line_list)) 1832 ret = lf->found = 1; 1833 else 1834 ret = 0; /* Lines are not found */ 1835 else { 1836 zfree(&lf->lr->path); 1837 } 1838 return ret; 1839 } 1840 1841 static int line_range_inline_cb(Dwarf_Die *in_die, void *data) 1842 { 1843 int ret = find_line_range_by_line(in_die, data); 1844 1845 /* 1846 * We have to check all instances of inlined function, because 1847 * some execution paths can be optimized out depends on the 1848 * function argument of instances. However, if an error occurs, 1849 * it should be handled by the caller. 1850 */ 1851 return ret < 0 ? ret : 0; 1852 } 1853 1854 /* Search function definition from function name */ 1855 static int line_range_search_cb(Dwarf_Die *sp_die, void *data) 1856 { 1857 struct dwarf_callback_param *param = data; 1858 struct line_finder *lf = param->data; 1859 struct line_range *lr = lf->lr; 1860 1861 /* Check declared file */ 1862 if (lr->file && strtailcmp(lr->file, dwarf_decl_file(sp_die))) 1863 return DWARF_CB_OK; 1864 1865 if (die_is_func_def(sp_die) && 1866 die_match_name(sp_die, lr->function)) { 1867 lf->fname = dwarf_decl_file(sp_die); 1868 dwarf_decl_line(sp_die, &lr->offset); 1869 pr_debug("fname: %s, lineno:%d\n", lf->fname, lr->offset); 1870 lf->lno_s = lr->offset + lr->start; 1871 if (lf->lno_s < 0) /* Overflow */ 1872 lf->lno_s = INT_MAX; 1873 lf->lno_e = lr->offset + lr->end; 1874 if (lf->lno_e < 0) /* Overflow */ 1875 lf->lno_e = INT_MAX; 1876 pr_debug("New line range: %d to %d\n", lf->lno_s, lf->lno_e); 1877 lr->start = lf->lno_s; 1878 lr->end = lf->lno_e; 1879 if (!die_is_func_instance(sp_die)) 1880 param->retval = die_walk_instances(sp_die, 1881 line_range_inline_cb, lf); 1882 else 1883 param->retval = find_line_range_by_line(sp_die, lf); 1884 return DWARF_CB_ABORT; 1885 } 1886 return DWARF_CB_OK; 1887 } 1888 1889 static int find_line_range_by_func(struct line_finder *lf) 1890 { 1891 struct dwarf_callback_param param = {.data = (void *)lf, .retval = 0}; 1892 dwarf_getfuncs(&lf->cu_die, line_range_search_cb, ¶m, 0); 1893 return param.retval; 1894 } 1895 1896 int debuginfo__find_line_range(struct debuginfo *dbg, struct line_range *lr) 1897 { 1898 struct line_finder lf = {.lr = lr, .found = 0}; 1899 int ret = 0; 1900 Dwarf_Off off = 0, noff; 1901 size_t cuhl; 1902 Dwarf_Die *diep; 1903 const char *comp_dir; 1904 1905 /* Fastpath: lookup by function name from .debug_pubnames section */ 1906 if (lr->function) { 1907 struct pubname_callback_param pubname_param = { 1908 .function = lr->function, .file = lr->file, 1909 .cu_die = &lf.cu_die, .sp_die = &lf.sp_die, .found = 0}; 1910 struct dwarf_callback_param line_range_param = { 1911 .data = (void *)&lf, .retval = 0}; 1912 1913 dwarf_getpubnames(dbg->dbg, pubname_search_cb, 1914 &pubname_param, 0); 1915 if (pubname_param.found) { 1916 line_range_search_cb(&lf.sp_die, &line_range_param); 1917 if (lf.found) 1918 goto found; 1919 } 1920 } 1921 1922 /* Loop on CUs (Compilation Unit) */ 1923 while (!lf.found && ret >= 0) { 1924 if (dwarf_nextcu(dbg->dbg, off, &noff, &cuhl, 1925 NULL, NULL, NULL) != 0) 1926 break; 1927 1928 /* Get the DIE(Debugging Information Entry) of this CU */ 1929 diep = dwarf_offdie(dbg->dbg, off + cuhl, &lf.cu_die); 1930 if (!diep) 1931 continue; 1932 1933 /* Check if target file is included. */ 1934 if (lr->file) 1935 lf.fname = cu_find_realpath(&lf.cu_die, lr->file); 1936 else 1937 lf.fname = 0; 1938 1939 if (!lr->file || lf.fname) { 1940 if (lr->function) 1941 ret = find_line_range_by_func(&lf); 1942 else { 1943 lf.lno_s = lr->start; 1944 lf.lno_e = lr->end; 1945 ret = find_line_range_by_line(NULL, &lf); 1946 } 1947 } 1948 off = noff; 1949 } 1950 1951 found: 1952 /* Store comp_dir */ 1953 if (lf.found) { 1954 comp_dir = cu_get_comp_dir(&lf.cu_die); 1955 if (comp_dir) { 1956 lr->comp_dir = strdup(comp_dir); 1957 if (!lr->comp_dir) 1958 ret = -ENOMEM; 1959 } 1960 } 1961 1962 pr_debug("path: %s\n", lr->path); 1963 return (ret < 0) ? ret : lf.found; 1964 } 1965 1966 /* 1967 * Find a src file from a DWARF tag path. Prepend optional source path prefix 1968 * and chop off leading directories that do not exist. Result is passed back as 1969 * a newly allocated path on success. 1970 * Return 0 if file was found and readable, -errno otherwise. 1971 */ 1972 int get_real_path(const char *raw_path, const char *comp_dir, 1973 char **new_path) 1974 { 1975 const char *prefix = symbol_conf.source_prefix; 1976 1977 if (!prefix) { 1978 if (raw_path[0] != '/' && comp_dir) 1979 /* If not an absolute path, try to use comp_dir */ 1980 prefix = comp_dir; 1981 else { 1982 if (access(raw_path, R_OK) == 0) { 1983 *new_path = strdup(raw_path); 1984 return *new_path ? 0 : -ENOMEM; 1985 } else 1986 return -errno; 1987 } 1988 } 1989 1990 *new_path = malloc((strlen(prefix) + strlen(raw_path) + 2)); 1991 if (!*new_path) 1992 return -ENOMEM; 1993 1994 for (;;) { 1995 sprintf(*new_path, "%s/%s", prefix, raw_path); 1996 1997 if (access(*new_path, R_OK) == 0) 1998 return 0; 1999 2000 if (!symbol_conf.source_prefix) { 2001 /* In case of searching comp_dir, don't retry */ 2002 zfree(new_path); 2003 return -errno; 2004 } 2005 2006 switch (errno) { 2007 case ENAMETOOLONG: 2008 case ENOENT: 2009 case EROFS: 2010 case EFAULT: 2011 raw_path = strchr(++raw_path, '/'); 2012 if (!raw_path) { 2013 zfree(new_path); 2014 return -ENOENT; 2015 } 2016 continue; 2017 2018 default: 2019 zfree(new_path); 2020 return -errno; 2021 } 2022 } 2023 } 2024