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