1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 3be8ecc57STony Garnock-Jones #include <signal.h> 4f048d548SNamhyung Kim #include <stdio.h> 5f048d548SNamhyung Kim #include <stdlib.h> 6f048d548SNamhyung Kim #include <string.h> 7be8ecc57STony Garnock-Jones #include <sys/types.h> 8f048d548SNamhyung Kim 9f048d548SNamhyung Kim #include <linux/kernel.h> 1013c230abSArnaldo Carvalho de Melo #include <linux/string.h> 117f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 12f048d548SNamhyung Kim 13b3801e79SIan Rogers #include <api/io.h> 14b3801e79SIan Rogers 1586c98cabSNamhyung Kim #include "util/dso.h" 16f048d548SNamhyung Kim #include "util/debug.h" 17a64489c5SJin Yao #include "util/callchain.h" 18b10c78c5SJin Yao #include "util/symbol_conf.h" 19632a5cabSArnaldo Carvalho de Melo #include "srcline.h" 207285cf33SNamhyung Kim #include "string2.h" 2185c116a6SAndi Kleen #include "symbol.h" 22be8ecc57STony Garnock-Jones #include "subcmd/run-command.h" 2385c116a6SAndi Kleen 24a9710ba0SAndi Kleen bool srcline_full_filename; 25a9710ba0SAndi Kleen 265580338dSJin Yao static const char *dso__name(struct dso *dso) 275580338dSJin Yao { 285580338dSJin Yao const char *dso_name; 295580338dSJin Yao 305580338dSJin Yao if (dso->symsrc_filename) 315580338dSJin Yao dso_name = dso->symsrc_filename; 325580338dSJin Yao else 335580338dSJin Yao dso_name = dso->long_name; 345580338dSJin Yao 355580338dSJin Yao if (dso_name[0] == '[') 365580338dSJin Yao return NULL; 375580338dSJin Yao 385580338dSJin Yao if (!strncmp(dso_name, "/tmp/perf-", 10)) 395580338dSJin Yao return NULL; 405580338dSJin Yao 415580338dSJin Yao return dso_name; 425580338dSJin Yao } 435580338dSJin Yao 442be8832fSMilian Wolff static int inline_list__append(struct symbol *symbol, char *srcline, 452be8832fSMilian Wolff struct inline_node *node) 46a64489c5SJin Yao { 47a64489c5SJin Yao struct inline_list *ilist; 48a64489c5SJin Yao 49a64489c5SJin Yao ilist = zalloc(sizeof(*ilist)); 50a64489c5SJin Yao if (ilist == NULL) 51a64489c5SJin Yao return -1; 52a64489c5SJin Yao 53fea0cf84SMilian Wolff ilist->symbol = symbol; 542be8832fSMilian Wolff ilist->srcline = srcline; 55a64489c5SJin Yao 5628071f51SMilian Wolff if (callchain_param.order == ORDER_CALLEE) 57a64489c5SJin Yao list_add_tail(&ilist->list, &node->val); 5828071f51SMilian Wolff else 5928071f51SMilian Wolff list_add(&ilist->list, &node->val); 60a64489c5SJin Yao 61a64489c5SJin Yao return 0; 62a64489c5SJin Yao } 63a64489c5SJin Yao 642be8832fSMilian Wolff /* basename version that takes a const input string */ 652be8832fSMilian Wolff static const char *gnu_basename(const char *path) 662be8832fSMilian Wolff { 672be8832fSMilian Wolff const char *base = strrchr(path, '/'); 682be8832fSMilian Wolff 692be8832fSMilian Wolff return base ? base + 1 : path; 702be8832fSMilian Wolff } 712be8832fSMilian Wolff 722be8832fSMilian Wolff static char *srcline_from_fileline(const char *file, unsigned int line) 732be8832fSMilian Wolff { 742be8832fSMilian Wolff char *srcline; 752be8832fSMilian Wolff 762be8832fSMilian Wolff if (!file) 772be8832fSMilian Wolff return NULL; 782be8832fSMilian Wolff 792be8832fSMilian Wolff if (!srcline_full_filename) 802be8832fSMilian Wolff file = gnu_basename(file); 812be8832fSMilian Wolff 822be8832fSMilian Wolff if (asprintf(&srcline, "%s:%u", file, line) < 0) 832be8832fSMilian Wolff return NULL; 842be8832fSMilian Wolff 852be8832fSMilian Wolff return srcline; 862be8832fSMilian Wolff } 872be8832fSMilian Wolff 887285cf33SNamhyung Kim static struct symbol *new_inline_sym(struct dso *dso, 897285cf33SNamhyung Kim struct symbol *base_sym, 907285cf33SNamhyung Kim const char *funcname) 917285cf33SNamhyung Kim { 927285cf33SNamhyung Kim struct symbol *inline_sym; 937285cf33SNamhyung Kim char *demangled = NULL; 947285cf33SNamhyung Kim 95d4046e8eSMilian Wolff if (!funcname) 96d4046e8eSMilian Wolff funcname = "??"; 97d4046e8eSMilian Wolff 987285cf33SNamhyung Kim if (dso) { 997285cf33SNamhyung Kim demangled = dso__demangle_sym(dso, 0, funcname); 1007285cf33SNamhyung Kim if (demangled) 1017285cf33SNamhyung Kim funcname = demangled; 1027285cf33SNamhyung Kim } 1037285cf33SNamhyung Kim 1047285cf33SNamhyung Kim if (base_sym && strcmp(funcname, base_sym->name) == 0) { 1057285cf33SNamhyung Kim /* reuse the real, existing symbol */ 1067285cf33SNamhyung Kim inline_sym = base_sym; 1077285cf33SNamhyung Kim /* ensure that we don't alias an inlined symbol, which could 1087285cf33SNamhyung Kim * lead to double frees in inline_node__delete 1097285cf33SNamhyung Kim */ 1107285cf33SNamhyung Kim assert(!base_sym->inlined); 1117285cf33SNamhyung Kim } else { 1127285cf33SNamhyung Kim /* create a fake symbol for the inline frame */ 1137285cf33SNamhyung Kim inline_sym = symbol__new(base_sym ? base_sym->start : 0, 1147346195eSHe Kuang base_sym ? (base_sym->end - base_sym->start) : 0, 1157285cf33SNamhyung Kim base_sym ? base_sym->binding : 0, 116af30bffaSArnaldo Carvalho de Melo base_sym ? base_sym->type : 0, 1177285cf33SNamhyung Kim funcname); 1187285cf33SNamhyung Kim if (inline_sym) 1197285cf33SNamhyung Kim inline_sym->inlined = 1; 1207285cf33SNamhyung Kim } 1217285cf33SNamhyung Kim 1227285cf33SNamhyung Kim free(demangled); 1237285cf33SNamhyung Kim 1247285cf33SNamhyung Kim return inline_sym; 1257285cf33SNamhyung Kim } 1267285cf33SNamhyung Kim 127be8ecc57STony Garnock-Jones #define MAX_INLINE_NEST 1024 128be8ecc57STony Garnock-Jones 1292f48fcd8SRoberto Vitillo #ifdef HAVE_LIBBFD_SUPPORT 1302f48fcd8SRoberto Vitillo 1312f48fcd8SRoberto Vitillo /* 1322f48fcd8SRoberto Vitillo * Implement addr2line using libbfd. 1332f48fcd8SRoberto Vitillo */ 1342f48fcd8SRoberto Vitillo #define PACKAGE "perf" 1352f48fcd8SRoberto Vitillo #include <bfd.h> 1362f48fcd8SRoberto Vitillo 1372f48fcd8SRoberto Vitillo struct a2l_data { 1382f48fcd8SRoberto Vitillo const char *input; 139ac931f87SWang Nan u64 addr; 1402f48fcd8SRoberto Vitillo 1412f48fcd8SRoberto Vitillo bool found; 1422f48fcd8SRoberto Vitillo const char *filename; 1432f48fcd8SRoberto Vitillo const char *funcname; 1442f48fcd8SRoberto Vitillo unsigned line; 1452f48fcd8SRoberto Vitillo 1462f48fcd8SRoberto Vitillo bfd *abfd; 1472f48fcd8SRoberto Vitillo asymbol **syms; 1482f48fcd8SRoberto Vitillo }; 1492f48fcd8SRoberto Vitillo 1502f48fcd8SRoberto Vitillo static int bfd_error(const char *string) 1512f48fcd8SRoberto Vitillo { 1522f48fcd8SRoberto Vitillo const char *errmsg; 1532f48fcd8SRoberto Vitillo 1542f48fcd8SRoberto Vitillo errmsg = bfd_errmsg(bfd_get_error()); 1552f48fcd8SRoberto Vitillo fflush(stdout); 1562f48fcd8SRoberto Vitillo 1572f48fcd8SRoberto Vitillo if (string) 1582f48fcd8SRoberto Vitillo pr_debug("%s: %s\n", string, errmsg); 1592f48fcd8SRoberto Vitillo else 1602f48fcd8SRoberto Vitillo pr_debug("%s\n", errmsg); 1612f48fcd8SRoberto Vitillo 1622f48fcd8SRoberto Vitillo return -1; 1632f48fcd8SRoberto Vitillo } 1642f48fcd8SRoberto Vitillo 1652f48fcd8SRoberto Vitillo static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) 1662f48fcd8SRoberto Vitillo { 1672f48fcd8SRoberto Vitillo long storage; 1682f48fcd8SRoberto Vitillo long symcount; 1692f48fcd8SRoberto Vitillo asymbol **syms; 1702f48fcd8SRoberto Vitillo bfd_boolean dynamic = FALSE; 1712f48fcd8SRoberto Vitillo 1722f48fcd8SRoberto Vitillo if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) 1732f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1742f48fcd8SRoberto Vitillo 1752f48fcd8SRoberto Vitillo storage = bfd_get_symtab_upper_bound(abfd); 1762f48fcd8SRoberto Vitillo if (storage == 0L) { 1772f48fcd8SRoberto Vitillo storage = bfd_get_dynamic_symtab_upper_bound(abfd); 1782f48fcd8SRoberto Vitillo dynamic = TRUE; 1792f48fcd8SRoberto Vitillo } 1802f48fcd8SRoberto Vitillo if (storage < 0L) 1812f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1822f48fcd8SRoberto Vitillo 1832f48fcd8SRoberto Vitillo syms = malloc(storage); 1842f48fcd8SRoberto Vitillo if (dynamic) 1852f48fcd8SRoberto Vitillo symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); 1862f48fcd8SRoberto Vitillo else 1872f48fcd8SRoberto Vitillo symcount = bfd_canonicalize_symtab(abfd, syms); 1882f48fcd8SRoberto Vitillo 1892f48fcd8SRoberto Vitillo if (symcount < 0) { 1902f48fcd8SRoberto Vitillo free(syms); 1912f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1922f48fcd8SRoberto Vitillo } 1932f48fcd8SRoberto Vitillo 1942f48fcd8SRoberto Vitillo a2l->syms = syms; 1952f48fcd8SRoberto Vitillo return 0; 1962f48fcd8SRoberto Vitillo } 1972f48fcd8SRoberto Vitillo 1982f48fcd8SRoberto Vitillo static void find_address_in_section(bfd *abfd, asection *section, void *data) 1992f48fcd8SRoberto Vitillo { 2002f48fcd8SRoberto Vitillo bfd_vma pc, vma; 2012f48fcd8SRoberto Vitillo bfd_size_type size; 2022f48fcd8SRoberto Vitillo struct a2l_data *a2l = data; 2030ada120cSChangbin Du flagword flags; 2042f48fcd8SRoberto Vitillo 2052f48fcd8SRoberto Vitillo if (a2l->found) 2062f48fcd8SRoberto Vitillo return; 2072f48fcd8SRoberto Vitillo 2080ada120cSChangbin Du #ifdef bfd_get_section_flags 2090ada120cSChangbin Du flags = bfd_get_section_flags(abfd, section); 2100ada120cSChangbin Du #else 2110ada120cSChangbin Du flags = bfd_section_flags(section); 2120ada120cSChangbin Du #endif 2130ada120cSChangbin Du if ((flags & SEC_ALLOC) == 0) 2142f48fcd8SRoberto Vitillo return; 2152f48fcd8SRoberto Vitillo 2162f48fcd8SRoberto Vitillo pc = a2l->addr; 2170ada120cSChangbin Du #ifdef bfd_get_section_vma 2182f48fcd8SRoberto Vitillo vma = bfd_get_section_vma(abfd, section); 2190ada120cSChangbin Du #else 2200ada120cSChangbin Du vma = bfd_section_vma(section); 2210ada120cSChangbin Du #endif 2220ada120cSChangbin Du #ifdef bfd_get_section_size 2232f48fcd8SRoberto Vitillo size = bfd_get_section_size(section); 2240ada120cSChangbin Du #else 2250ada120cSChangbin Du size = bfd_section_size(section); 2260ada120cSChangbin Du #endif 2272f48fcd8SRoberto Vitillo 2282f48fcd8SRoberto Vitillo if (pc < vma || pc >= vma + size) 2292f48fcd8SRoberto Vitillo return; 2302f48fcd8SRoberto Vitillo 2312f48fcd8SRoberto Vitillo a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, 2322f48fcd8SRoberto Vitillo &a2l->filename, &a2l->funcname, 2332f48fcd8SRoberto Vitillo &a2l->line); 234d964b1cdSMilian Wolff 235d964b1cdSMilian Wolff if (a2l->filename && !strlen(a2l->filename)) 236d964b1cdSMilian Wolff a2l->filename = NULL; 2372f48fcd8SRoberto Vitillo } 2382f48fcd8SRoberto Vitillo 2392f48fcd8SRoberto Vitillo static struct a2l_data *addr2line_init(const char *path) 2402f48fcd8SRoberto Vitillo { 2412f48fcd8SRoberto Vitillo bfd *abfd; 2422f48fcd8SRoberto Vitillo struct a2l_data *a2l = NULL; 2432f48fcd8SRoberto Vitillo 2442f48fcd8SRoberto Vitillo abfd = bfd_openr(path, NULL); 2452f48fcd8SRoberto Vitillo if (abfd == NULL) 2462f48fcd8SRoberto Vitillo return NULL; 2472f48fcd8SRoberto Vitillo 2482f48fcd8SRoberto Vitillo if (!bfd_check_format(abfd, bfd_object)) 2492f48fcd8SRoberto Vitillo goto out; 2502f48fcd8SRoberto Vitillo 2512f48fcd8SRoberto Vitillo a2l = zalloc(sizeof(*a2l)); 2522f48fcd8SRoberto Vitillo if (a2l == NULL) 2532f48fcd8SRoberto Vitillo goto out; 2542f48fcd8SRoberto Vitillo 2552f48fcd8SRoberto Vitillo a2l->abfd = abfd; 2562f48fcd8SRoberto Vitillo a2l->input = strdup(path); 2572f48fcd8SRoberto Vitillo if (a2l->input == NULL) 2582f48fcd8SRoberto Vitillo goto out; 2592f48fcd8SRoberto Vitillo 2602f48fcd8SRoberto Vitillo if (slurp_symtab(abfd, a2l)) 2612f48fcd8SRoberto Vitillo goto out; 2622f48fcd8SRoberto Vitillo 2632f48fcd8SRoberto Vitillo return a2l; 2642f48fcd8SRoberto Vitillo 2652f48fcd8SRoberto Vitillo out: 2662f48fcd8SRoberto Vitillo if (a2l) { 2677d16c634SNamhyung Kim zfree((char **)&a2l->input); 2682f48fcd8SRoberto Vitillo free(a2l); 2692f48fcd8SRoberto Vitillo } 2702f48fcd8SRoberto Vitillo bfd_close(abfd); 2712f48fcd8SRoberto Vitillo return NULL; 2722f48fcd8SRoberto Vitillo } 2732f48fcd8SRoberto Vitillo 2742f48fcd8SRoberto Vitillo static void addr2line_cleanup(struct a2l_data *a2l) 2752f48fcd8SRoberto Vitillo { 2762f48fcd8SRoberto Vitillo if (a2l->abfd) 2772f48fcd8SRoberto Vitillo bfd_close(a2l->abfd); 2787d16c634SNamhyung Kim zfree((char **)&a2l->input); 27974cf249dSArnaldo Carvalho de Melo zfree(&a2l->syms); 2802f48fcd8SRoberto Vitillo free(a2l); 2812f48fcd8SRoberto Vitillo } 2822f48fcd8SRoberto Vitillo 2834d53b9d5SMilian Wolff static int inline_list__append_dso_a2l(struct dso *dso, 284fea0cf84SMilian Wolff struct inline_node *node, 285fea0cf84SMilian Wolff struct symbol *sym) 2864d53b9d5SMilian Wolff { 2874d53b9d5SMilian Wolff struct a2l_data *a2l = dso->a2l; 288fea0cf84SMilian Wolff struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname); 2892be8832fSMilian Wolff char *srcline = NULL; 2904d53b9d5SMilian Wolff 2912be8832fSMilian Wolff if (a2l->filename) 2922be8832fSMilian Wolff srcline = srcline_from_fileline(a2l->filename, a2l->line); 2932be8832fSMilian Wolff 2942be8832fSMilian Wolff return inline_list__append(inline_sym, srcline, node); 2954d53b9d5SMilian Wolff } 2964d53b9d5SMilian Wolff 297ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr, 2982f84b42bSAndi Kleen char **file, unsigned int *line, struct dso *dso, 299fea0cf84SMilian Wolff bool unwind_inlines, struct inline_node *node, 300fea0cf84SMilian Wolff struct symbol *sym) 3012f48fcd8SRoberto Vitillo { 3022f48fcd8SRoberto Vitillo int ret = 0; 303454ff00fSAdrian Hunter struct a2l_data *a2l = dso->a2l; 3042f48fcd8SRoberto Vitillo 305454ff00fSAdrian Hunter if (!a2l) { 306454ff00fSAdrian Hunter dso->a2l = addr2line_init(dso_name); 307454ff00fSAdrian Hunter a2l = dso->a2l; 308454ff00fSAdrian Hunter } 309454ff00fSAdrian Hunter 3102f48fcd8SRoberto Vitillo if (a2l == NULL) { 311b10c78c5SJin Yao if (!symbol_conf.disable_add2line_warn) 3122f48fcd8SRoberto Vitillo pr_warning("addr2line_init failed for %s\n", dso_name); 3132f48fcd8SRoberto Vitillo return 0; 3142f48fcd8SRoberto Vitillo } 3152f48fcd8SRoberto Vitillo 3162f48fcd8SRoberto Vitillo a2l->addr = addr; 317454ff00fSAdrian Hunter a2l->found = false; 318454ff00fSAdrian Hunter 3192f48fcd8SRoberto Vitillo bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); 3202f48fcd8SRoberto Vitillo 321b21cc978SMilian Wolff if (!a2l->found) 322b21cc978SMilian Wolff return 0; 323b21cc978SMilian Wolff 324b21cc978SMilian Wolff if (unwind_inlines) { 3252f84b42bSAndi Kleen int cnt = 0; 3262f84b42bSAndi Kleen 327fea0cf84SMilian Wolff if (node && inline_list__append_dso_a2l(dso, node, sym)) 3284d53b9d5SMilian Wolff return 0; 3294d53b9d5SMilian Wolff 3302f84b42bSAndi Kleen while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, 3312f84b42bSAndi Kleen &a2l->funcname, &a2l->line) && 332a64489c5SJin Yao cnt++ < MAX_INLINE_NEST) { 333a64489c5SJin Yao 334d964b1cdSMilian Wolff if (a2l->filename && !strlen(a2l->filename)) 335d964b1cdSMilian Wolff a2l->filename = NULL; 336d964b1cdSMilian Wolff 337a64489c5SJin Yao if (node != NULL) { 338fea0cf84SMilian Wolff if (inline_list__append_dso_a2l(dso, node, sym)) 339a64489c5SJin Yao return 0; 340b21cc978SMilian Wolff // found at least one inline frame 341b21cc978SMilian Wolff ret = 1; 342a64489c5SJin Yao } 343a64489c5SJin Yao } 3442f84b42bSAndi Kleen } 3452f84b42bSAndi Kleen 346b21cc978SMilian Wolff if (file) { 347b21cc978SMilian Wolff *file = a2l->filename ? strdup(a2l->filename) : NULL; 348b21cc978SMilian Wolff ret = *file ? 1 : 0; 3492f48fcd8SRoberto Vitillo } 3502f48fcd8SRoberto Vitillo 351b21cc978SMilian Wolff if (line) 352b21cc978SMilian Wolff *line = a2l->line; 353b21cc978SMilian Wolff 3542f48fcd8SRoberto Vitillo return ret; 3552f48fcd8SRoberto Vitillo } 3562f48fcd8SRoberto Vitillo 357454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso) 358454ff00fSAdrian Hunter { 359454ff00fSAdrian Hunter struct a2l_data *a2l = dso->a2l; 360454ff00fSAdrian Hunter 361454ff00fSAdrian Hunter if (!a2l) 362454ff00fSAdrian Hunter return; 363454ff00fSAdrian Hunter 364454ff00fSAdrian Hunter addr2line_cleanup(a2l); 365454ff00fSAdrian Hunter 366454ff00fSAdrian Hunter dso->a2l = NULL; 367454ff00fSAdrian Hunter } 368454ff00fSAdrian Hunter 3692f48fcd8SRoberto Vitillo #else /* HAVE_LIBBFD_SUPPORT */ 3702f48fcd8SRoberto Vitillo 3715580338dSJin Yao static int filename_split(char *filename, unsigned int *line_nr) 3725580338dSJin Yao { 3735580338dSJin Yao char *sep; 3745580338dSJin Yao 3755580338dSJin Yao sep = strchr(filename, '\n'); 3765580338dSJin Yao if (sep) 3775580338dSJin Yao *sep = '\0'; 3785580338dSJin Yao 3795580338dSJin Yao if (!strcmp(filename, "??:0")) 3805580338dSJin Yao return 0; 3815580338dSJin Yao 3825580338dSJin Yao sep = strchr(filename, ':'); 3835580338dSJin Yao if (sep) { 3845580338dSJin Yao *sep++ = '\0'; 3855580338dSJin Yao *line_nr = strtoul(sep, NULL, 0); 3865580338dSJin Yao return 1; 3875580338dSJin Yao } 3885580338dSJin Yao 3895580338dSJin Yao return 0; 3905580338dSJin Yao } 3915580338dSJin Yao 392b3801e79SIan Rogers static void addr2line_subprocess_cleanup(struct child_process *a2l) 393f048d548SNamhyung Kim { 394b3801e79SIan Rogers if (a2l->pid != -1) { 395b3801e79SIan Rogers kill(a2l->pid, SIGKILL); 396b3801e79SIan Rogers finish_command(a2l); /* ignore result, we don't care */ 397b3801e79SIan Rogers a2l->pid = -1; 398be8ecc57STony Garnock-Jones } 399be8ecc57STony Garnock-Jones 400be8ecc57STony Garnock-Jones free(a2l); 401be8ecc57STony Garnock-Jones } 402be8ecc57STony Garnock-Jones 403b3801e79SIan Rogers static struct child_process *addr2line_subprocess_init(const char *addr2line_path, 40457594454SIan Rogers const char *binary_path) 405be8ecc57STony Garnock-Jones { 40657594454SIan Rogers const char *argv[] = { 40757594454SIan Rogers addr2line_path ?: "addr2line", 40857594454SIan Rogers "-e", binary_path, 40957594454SIan Rogers "-i", "-f", NULL 41057594454SIan Rogers }; 411b3801e79SIan Rogers struct child_process *a2l = zalloc(sizeof(*a2l)); 412be8ecc57STony Garnock-Jones int start_command_status = 0; 413be8ecc57STony Garnock-Jones 414b3801e79SIan Rogers if (a2l == NULL) { 415b3801e79SIan Rogers pr_err("Failed to allocate memory for addr2line"); 416b3801e79SIan Rogers return NULL; 417b3801e79SIan Rogers } 418be8ecc57STony Garnock-Jones 419b3801e79SIan Rogers a2l->pid = -1; 420b3801e79SIan Rogers a2l->in = -1; 421b3801e79SIan Rogers a2l->out = -1; 422b3801e79SIan Rogers a2l->no_stderr = 1; 423be8ecc57STony Garnock-Jones 424b3801e79SIan Rogers a2l->argv = argv; 425b3801e79SIan Rogers start_command_status = start_command(a2l); 426b3801e79SIan Rogers a2l->argv = NULL; /* it's not used after start_command; avoid dangling pointers */ 427be8ecc57STony Garnock-Jones 428be8ecc57STony Garnock-Jones if (start_command_status != 0) { 42957594454SIan Rogers pr_warning("could not start addr2line (%s) for %s: start_command return code %d\n", 43057594454SIan Rogers addr2line_path, binary_path, start_command_status); 431be8ecc57STony Garnock-Jones addr2line_subprocess_cleanup(a2l); 432be8ecc57STony Garnock-Jones return NULL; 433be8ecc57STony Garnock-Jones } 434be8ecc57STony Garnock-Jones 435b3801e79SIan Rogers return a2l; 436b3801e79SIan Rogers } 437b3801e79SIan Rogers 4382c4b9280SIan Rogers enum a2l_style { 4392c4b9280SIan Rogers BROKEN, 4402c4b9280SIan Rogers GNU_BINUTILS, 4412c4b9280SIan Rogers LLVM, 4422c4b9280SIan Rogers }; 4432c4b9280SIan Rogers 4442c4b9280SIan Rogers static enum a2l_style addr2line_configure(struct child_process *a2l) 4452c4b9280SIan Rogers { 4462c4b9280SIan Rogers static bool cached; 4472c4b9280SIan Rogers static enum a2l_style style; 4482c4b9280SIan Rogers 4492c4b9280SIan Rogers if (!cached) { 4502c4b9280SIan Rogers char buf[128]; 4512c4b9280SIan Rogers struct io io; 4522c4b9280SIan Rogers int ch; 4532c4b9280SIan Rogers 4542c4b9280SIan Rogers if (write(a2l->in, ",\n", 2) != 2) 4552c4b9280SIan Rogers return BROKEN; 4562c4b9280SIan Rogers 4572c4b9280SIan Rogers io__init(&io, a2l->out, buf, sizeof(buf)); 4582c4b9280SIan Rogers ch = io__get_char(&io); 4592c4b9280SIan Rogers if (ch == ',') { 4602c4b9280SIan Rogers style = LLVM; 4612c4b9280SIan Rogers cached = true; 4622c4b9280SIan Rogers } else if (ch == '?') { 4632c4b9280SIan Rogers style = GNU_BINUTILS; 4642c4b9280SIan Rogers cached = true; 4652c4b9280SIan Rogers } else { 4662c4b9280SIan Rogers style = BROKEN; 4672c4b9280SIan Rogers } 4682c4b9280SIan Rogers do { 4692c4b9280SIan Rogers ch = io__get_char(&io); 4702c4b9280SIan Rogers } while (ch > 0 && ch != '\n'); 4712c4b9280SIan Rogers if (style == GNU_BINUTILS) { 4722c4b9280SIan Rogers do { 4732c4b9280SIan Rogers ch = io__get_char(&io); 4742c4b9280SIan Rogers } while (ch > 0 && ch != '\n'); 4752c4b9280SIan Rogers } 476*75a616c6SIan Rogers /* Ignore SIGPIPE in the event addr2line exits. */ 477*75a616c6SIan Rogers signal(SIGPIPE, SIG_IGN); 4782c4b9280SIan Rogers } 4792c4b9280SIan Rogers return style; 4802c4b9280SIan Rogers } 4812c4b9280SIan Rogers 482b3801e79SIan Rogers static int read_addr2line_record(struct io *io, 4832c4b9280SIan Rogers enum a2l_style style, 484be8ecc57STony Garnock-Jones char **function, 485be8ecc57STony Garnock-Jones char **filename, 486be8ecc57STony Garnock-Jones unsigned int *line_nr) 487be8ecc57STony Garnock-Jones { 488be8ecc57STony Garnock-Jones /* 489be8ecc57STony Garnock-Jones * Returns: 490be8ecc57STony Garnock-Jones * -1 ==> error 491be8ecc57STony Garnock-Jones * 0 ==> sentinel (or other ill-formed) record read 492be8ecc57STony Garnock-Jones * 1 ==> a genuine record read 493be8ecc57STony Garnock-Jones */ 494be8ecc57STony Garnock-Jones char *line = NULL; 495be8ecc57STony Garnock-Jones size_t line_len = 0; 496be8ecc57STony Garnock-Jones unsigned int dummy_line_nr = 0; 497be8ecc57STony Garnock-Jones int ret = -1; 498be8ecc57STony Garnock-Jones 499be8ecc57STony Garnock-Jones if (function != NULL) 500be8ecc57STony Garnock-Jones zfree(function); 501be8ecc57STony Garnock-Jones 502be8ecc57STony Garnock-Jones if (filename != NULL) 503be8ecc57STony Garnock-Jones zfree(filename); 504be8ecc57STony Garnock-Jones 505be8ecc57STony Garnock-Jones if (line_nr != NULL) 506be8ecc57STony Garnock-Jones *line_nr = 0; 507be8ecc57STony Garnock-Jones 508b3801e79SIan Rogers if (io__getline(io, &line, &line_len) < 0 || !line_len) 509be8ecc57STony Garnock-Jones goto error; 5102c4b9280SIan Rogers 5112c4b9280SIan Rogers if (style == LLVM && line_len == 2 && line[0] == ',') { 5122c4b9280SIan Rogers zfree(&line); 5132c4b9280SIan Rogers return 0; 5142c4b9280SIan Rogers } 5152c4b9280SIan Rogers 516be8ecc57STony Garnock-Jones if (function != NULL) 517be8ecc57STony Garnock-Jones *function = strdup(strim(line)); 518be8ecc57STony Garnock-Jones 519be8ecc57STony Garnock-Jones zfree(&line); 520be8ecc57STony Garnock-Jones line_len = 0; 521be8ecc57STony Garnock-Jones 522b3801e79SIan Rogers if (io__getline(io, &line, &line_len) < 0 || !line_len) 523be8ecc57STony Garnock-Jones goto error; 524be8ecc57STony Garnock-Jones 5252c4b9280SIan Rogers if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0 && 5262c4b9280SIan Rogers style == GNU_BINUTILS) { 527be8ecc57STony Garnock-Jones ret = 0; 528be8ecc57STony Garnock-Jones goto error; 529be8ecc57STony Garnock-Jones } 530be8ecc57STony Garnock-Jones 531be8ecc57STony Garnock-Jones if (filename != NULL) 532be8ecc57STony Garnock-Jones *filename = strdup(line); 533be8ecc57STony Garnock-Jones 534be8ecc57STony Garnock-Jones zfree(&line); 535be8ecc57STony Garnock-Jones line_len = 0; 536be8ecc57STony Garnock-Jones 537be8ecc57STony Garnock-Jones return 1; 538be8ecc57STony Garnock-Jones 539be8ecc57STony Garnock-Jones error: 540be8ecc57STony Garnock-Jones free(line); 541be8ecc57STony Garnock-Jones if (function != NULL) 542be8ecc57STony Garnock-Jones zfree(function); 543be8ecc57STony Garnock-Jones if (filename != NULL) 544be8ecc57STony Garnock-Jones zfree(filename); 545f048d548SNamhyung Kim return ret; 546f048d548SNamhyung Kim } 547454ff00fSAdrian Hunter 548be8ecc57STony Garnock-Jones static int inline_list__append_record(struct dso *dso, 549be8ecc57STony Garnock-Jones struct inline_node *node, 550be8ecc57STony Garnock-Jones struct symbol *sym, 551be8ecc57STony Garnock-Jones const char *function, 552be8ecc57STony Garnock-Jones const char *filename, 553be8ecc57STony Garnock-Jones unsigned int line_nr) 554454ff00fSAdrian Hunter { 555be8ecc57STony Garnock-Jones struct symbol *inline_sym = new_inline_sym(dso, sym, function); 556be8ecc57STony Garnock-Jones 557be8ecc57STony Garnock-Jones return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node); 558454ff00fSAdrian Hunter } 559454ff00fSAdrian Hunter 560be8ecc57STony Garnock-Jones static int addr2line(const char *dso_name, u64 addr, 561be8ecc57STony Garnock-Jones char **file, unsigned int *line_nr, 562be8ecc57STony Garnock-Jones struct dso *dso, 563be8ecc57STony Garnock-Jones bool unwind_inlines, 564be8ecc57STony Garnock-Jones struct inline_node *node, 565be8ecc57STony Garnock-Jones struct symbol *sym __maybe_unused) 566be8ecc57STony Garnock-Jones { 567b3801e79SIan Rogers struct child_process *a2l = dso->a2l; 568be8ecc57STony Garnock-Jones char *record_function = NULL; 569be8ecc57STony Garnock-Jones char *record_filename = NULL; 570be8ecc57STony Garnock-Jones unsigned int record_line_nr = 0; 571be8ecc57STony Garnock-Jones int record_status = -1; 572be8ecc57STony Garnock-Jones int ret = 0; 573be8ecc57STony Garnock-Jones size_t inline_count = 0; 574b3801e79SIan Rogers int len; 575b3801e79SIan Rogers char buf[128]; 576b3801e79SIan Rogers ssize_t written; 577b3801e79SIan Rogers struct io io; 5782c4b9280SIan Rogers enum a2l_style a2l_style; 579be8ecc57STony Garnock-Jones 580be8ecc57STony Garnock-Jones if (!a2l) { 5813b27222dSNamhyung Kim if (!filename__has_section(dso_name, ".debug_line")) 5823b27222dSNamhyung Kim goto out; 5833b27222dSNamhyung Kim 58457594454SIan Rogers dso->a2l = addr2line_subprocess_init(symbol_conf.addr2line_path, 58557594454SIan Rogers dso_name); 586be8ecc57STony Garnock-Jones a2l = dso->a2l; 587be8ecc57STony Garnock-Jones } 588be8ecc57STony Garnock-Jones 589be8ecc57STony Garnock-Jones if (a2l == NULL) { 590be8ecc57STony Garnock-Jones if (!symbol_conf.disable_add2line_warn) 591be8ecc57STony Garnock-Jones pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name); 592be8ecc57STony Garnock-Jones goto out; 593be8ecc57STony Garnock-Jones } 5942c4b9280SIan Rogers a2l_style = addr2line_configure(a2l); 5952c4b9280SIan Rogers if (a2l_style == BROKEN) { 5962c4b9280SIan Rogers if (!symbol_conf.disable_add2line_warn) 5972c4b9280SIan Rogers pr_warning("%s: addr2line configuration failed\n", __func__); 5982c4b9280SIan Rogers goto out; 5992c4b9280SIan Rogers } 600be8ecc57STony Garnock-Jones 601be8ecc57STony Garnock-Jones /* 602be8ecc57STony Garnock-Jones * Send our request and then *deliberately* send something that can't be interpreted as 603be8ecc57STony Garnock-Jones * a valid address to ask addr2line about (namely, ","). This causes addr2line to first 604be8ecc57STony Garnock-Jones * write out the answer to our request, in an unbounded/unknown number of records, and 6052c4b9280SIan Rogers * then to write out the lines "??" and "??:0", for GNU binutils, or "," for 6062c4b9280SIan Rogers * llvm-addr2line, so that we can detect when it has finished giving us anything 6072c4b9280SIan Rogers * useful. With GNU binutils, we have to be careful about the first record, though, 6082c4b9280SIan Rogers * because it may be genuinely unknown, in which case we'll get two sets of "??"/"??:0" 6092c4b9280SIan Rogers * lines. 610be8ecc57STony Garnock-Jones */ 611b3801e79SIan Rogers len = snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr); 612b3801e79SIan Rogers written = len > 0 ? write(a2l->in, buf, len) : -1; 613b3801e79SIan Rogers if (written != len) { 614d5e33ce0SNamhyung Kim if (!symbol_conf.disable_add2line_warn) 615be8ecc57STony Garnock-Jones pr_warning("%s %s: could not send request\n", __func__, dso_name); 616be8ecc57STony Garnock-Jones goto out; 617be8ecc57STony Garnock-Jones } 618b3801e79SIan Rogers io__init(&io, a2l->out, buf, sizeof(buf)); 619be8ecc57STony Garnock-Jones 6202c4b9280SIan Rogers switch (read_addr2line_record(&io, a2l_style, 6212c4b9280SIan Rogers &record_function, &record_filename, &record_line_nr)) { 622be8ecc57STony Garnock-Jones case -1: 623d5e33ce0SNamhyung Kim if (!symbol_conf.disable_add2line_warn) 624be8ecc57STony Garnock-Jones pr_warning("%s %s: could not read first record\n", __func__, dso_name); 625be8ecc57STony Garnock-Jones goto out; 626be8ecc57STony Garnock-Jones case 0: 627be8ecc57STony Garnock-Jones /* 628be8ecc57STony Garnock-Jones * The first record was invalid, so return failure, but first read another 629be8ecc57STony Garnock-Jones * record, since we asked a junk question and have to clear the answer out. 630be8ecc57STony Garnock-Jones */ 6312c4b9280SIan Rogers switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) { 632be8ecc57STony Garnock-Jones case -1: 633d5e33ce0SNamhyung Kim if (!symbol_conf.disable_add2line_warn) 634d5e33ce0SNamhyung Kim pr_warning("%s %s: could not read delimiter record\n", 635d5e33ce0SNamhyung Kim __func__, dso_name); 636be8ecc57STony Garnock-Jones break; 637be8ecc57STony Garnock-Jones case 0: 638be8ecc57STony Garnock-Jones /* As expected. */ 639be8ecc57STony Garnock-Jones break; 640be8ecc57STony Garnock-Jones default: 641d5e33ce0SNamhyung Kim if (!symbol_conf.disable_add2line_warn) 642be8ecc57STony Garnock-Jones pr_warning("%s %s: unexpected record instead of sentinel", 643be8ecc57STony Garnock-Jones __func__, dso_name); 644be8ecc57STony Garnock-Jones break; 645be8ecc57STony Garnock-Jones } 646be8ecc57STony Garnock-Jones goto out; 647be8ecc57STony Garnock-Jones default: 648be8ecc57STony Garnock-Jones break; 649be8ecc57STony Garnock-Jones } 650be8ecc57STony Garnock-Jones 651be8ecc57STony Garnock-Jones if (file) { 652be8ecc57STony Garnock-Jones *file = strdup(record_filename); 653be8ecc57STony Garnock-Jones ret = 1; 654be8ecc57STony Garnock-Jones } 655be8ecc57STony Garnock-Jones if (line_nr) 656be8ecc57STony Garnock-Jones *line_nr = record_line_nr; 657be8ecc57STony Garnock-Jones 658be8ecc57STony Garnock-Jones if (unwind_inlines) { 659be8ecc57STony Garnock-Jones if (node && inline_list__append_record(dso, node, sym, 660be8ecc57STony Garnock-Jones record_function, 661be8ecc57STony Garnock-Jones record_filename, 662be8ecc57STony Garnock-Jones record_line_nr)) { 663be8ecc57STony Garnock-Jones ret = 0; 664be8ecc57STony Garnock-Jones goto out; 665be8ecc57STony Garnock-Jones } 666be8ecc57STony Garnock-Jones } 667be8ecc57STony Garnock-Jones 668be8ecc57STony Garnock-Jones /* We have to read the records even if we don't care about the inline info. */ 669b3801e79SIan Rogers while ((record_status = read_addr2line_record(&io, 6702c4b9280SIan Rogers a2l_style, 671be8ecc57STony Garnock-Jones &record_function, 672be8ecc57STony Garnock-Jones &record_filename, 673be8ecc57STony Garnock-Jones &record_line_nr)) == 1) { 674be8ecc57STony Garnock-Jones if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) { 675be8ecc57STony Garnock-Jones if (inline_list__append_record(dso, node, sym, 676be8ecc57STony Garnock-Jones record_function, 677be8ecc57STony Garnock-Jones record_filename, 678be8ecc57STony Garnock-Jones record_line_nr)) { 679be8ecc57STony Garnock-Jones ret = 0; 680be8ecc57STony Garnock-Jones goto out; 681be8ecc57STony Garnock-Jones } 682be8ecc57STony Garnock-Jones ret = 1; /* found at least one inline frame */ 683be8ecc57STony Garnock-Jones } 684be8ecc57STony Garnock-Jones } 685be8ecc57STony Garnock-Jones 686be8ecc57STony Garnock-Jones out: 687be8ecc57STony Garnock-Jones free(record_function); 688be8ecc57STony Garnock-Jones free(record_filename); 689be8ecc57STony Garnock-Jones return ret; 690be8ecc57STony Garnock-Jones } 691be8ecc57STony Garnock-Jones 692be8ecc57STony Garnock-Jones void dso__free_a2l(struct dso *dso) 693be8ecc57STony Garnock-Jones { 694b3801e79SIan Rogers struct child_process *a2l = dso->a2l; 695be8ecc57STony Garnock-Jones 696be8ecc57STony Garnock-Jones if (!a2l) 697be8ecc57STony Garnock-Jones return; 698be8ecc57STony Garnock-Jones 699be8ecc57STony Garnock-Jones addr2line_subprocess_cleanup(a2l); 700be8ecc57STony Garnock-Jones 701be8ecc57STony Garnock-Jones dso->a2l = NULL; 702be8ecc57STony Garnock-Jones } 703be8ecc57STony Garnock-Jones 704be8ecc57STony Garnock-Jones #endif /* HAVE_LIBBFD_SUPPORT */ 705be8ecc57STony Garnock-Jones 706a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr, 707be8ecc57STony Garnock-Jones struct dso *dso, struct symbol *sym) 708a64489c5SJin Yao { 709a64489c5SJin Yao struct inline_node *node; 710a64489c5SJin Yao 711a64489c5SJin Yao node = zalloc(sizeof(*node)); 712a64489c5SJin Yao if (node == NULL) { 713a64489c5SJin Yao perror("not enough memory for the inline node"); 714be8ecc57STony Garnock-Jones return NULL; 715a64489c5SJin Yao } 716a64489c5SJin Yao 717a64489c5SJin Yao INIT_LIST_HEAD(&node->val); 718a64489c5SJin Yao node->addr = addr; 719a64489c5SJin Yao 720be8ecc57STony Garnock-Jones addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym); 721a64489c5SJin Yao return node; 722a64489c5SJin Yao } 723a64489c5SJin Yao 724906049c8SAdrian Hunter /* 725906049c8SAdrian Hunter * Number of addr2line failures (without success) before disabling it for that 726906049c8SAdrian Hunter * dso. 727906049c8SAdrian Hunter */ 728906049c8SAdrian Hunter #define A2L_FAIL_LIMIT 123 729906049c8SAdrian Hunter 7302f84b42bSAndi Kleen char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym, 731935f5a9dSJin Yao bool show_sym, bool show_addr, bool unwind_inlines, 732935f5a9dSJin Yao u64 ip) 733f048d548SNamhyung Kim { 734a949fffbSDavid Ahern char *file = NULL; 735a949fffbSDavid Ahern unsigned line = 0; 7362cc9d0efSNamhyung Kim char *srcline; 737bf4414aeSArnaldo Carvalho de Melo const char *dso_name; 738f048d548SNamhyung Kim 7392cc9d0efSNamhyung Kim if (!dso->has_srcline) 74023f0981bSAndi Kleen goto out; 7412cc9d0efSNamhyung Kim 7425580338dSJin Yao dso_name = dso__name(dso); 7435580338dSJin Yao if (dso_name == NULL) 74458d91a00SNamhyung Kim goto out; 74558d91a00SNamhyung Kim 746fea0cf84SMilian Wolff if (!addr2line(dso_name, addr, &file, &line, dso, 747fea0cf84SMilian Wolff unwind_inlines, NULL, sym)) 74858d91a00SNamhyung Kim goto out; 749f048d548SNamhyung Kim 7502be8832fSMilian Wolff srcline = srcline_from_fileline(file, line); 751906049c8SAdrian Hunter free(file); 7522be8832fSMilian Wolff 7532be8832fSMilian Wolff if (!srcline) 754906049c8SAdrian Hunter goto out; 755906049c8SAdrian Hunter 756906049c8SAdrian Hunter dso->a2l_fails = 0; 757f048d548SNamhyung Kim 758f048d548SNamhyung Kim return srcline; 7592cc9d0efSNamhyung Kim 7602cc9d0efSNamhyung Kim out: 761906049c8SAdrian Hunter if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { 7622cc9d0efSNamhyung Kim dso->has_srcline = 0; 763454ff00fSAdrian Hunter dso__free_a2l(dso); 764906049c8SAdrian Hunter } 7655dfa210eSMilian Wolff 7665dfa210eSMilian Wolff if (!show_addr) 7675dfa210eSMilian Wolff return (show_sym && sym) ? 768ea335ef3SNamhyung Kim strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN; 7695dfa210eSMilian Wolff 77085c116a6SAndi Kleen if (sym) { 771ac931f87SWang Nan if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "", 772935f5a9dSJin Yao ip - sym->start) < 0) 77385c116a6SAndi Kleen return SRCLINE_UNKNOWN; 774ac931f87SWang Nan } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0) 7752cc9d0efSNamhyung Kim return SRCLINE_UNKNOWN; 77623f0981bSAndi Kleen return srcline; 777f048d548SNamhyung Kim } 778f048d548SNamhyung Kim 779dd2e18e9SAndi Kleen /* Returns filename and fills in line number in line */ 780dd2e18e9SAndi Kleen char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line) 781dd2e18e9SAndi Kleen { 782dd2e18e9SAndi Kleen char *file = NULL; 783dd2e18e9SAndi Kleen const char *dso_name; 784dd2e18e9SAndi Kleen 785dd2e18e9SAndi Kleen if (!dso->has_srcline) 786dd2e18e9SAndi Kleen goto out; 787dd2e18e9SAndi Kleen 788dd2e18e9SAndi Kleen dso_name = dso__name(dso); 789dd2e18e9SAndi Kleen if (dso_name == NULL) 790dd2e18e9SAndi Kleen goto out; 791dd2e18e9SAndi Kleen 792dd2e18e9SAndi Kleen if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL)) 793dd2e18e9SAndi Kleen goto out; 794dd2e18e9SAndi Kleen 795dd2e18e9SAndi Kleen dso->a2l_fails = 0; 796dd2e18e9SAndi Kleen return file; 797dd2e18e9SAndi Kleen 798dd2e18e9SAndi Kleen out: 799dd2e18e9SAndi Kleen if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { 800dd2e18e9SAndi Kleen dso->has_srcline = 0; 801dd2e18e9SAndi Kleen dso__free_a2l(dso); 802dd2e18e9SAndi Kleen } 803dd2e18e9SAndi Kleen 804dd2e18e9SAndi Kleen return NULL; 805dd2e18e9SAndi Kleen } 806dd2e18e9SAndi Kleen 807f048d548SNamhyung Kim void free_srcline(char *srcline) 808f048d548SNamhyung Kim { 809f048d548SNamhyung Kim if (srcline && strcmp(srcline, SRCLINE_UNKNOWN) != 0) 810f048d548SNamhyung Kim free(srcline); 811f048d548SNamhyung Kim } 8122f84b42bSAndi Kleen 8132f84b42bSAndi Kleen char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym, 814935f5a9dSJin Yao bool show_sym, bool show_addr, u64 ip) 8152f84b42bSAndi Kleen { 816935f5a9dSJin Yao return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip); 8172f84b42bSAndi Kleen } 818a64489c5SJin Yao 81921ac9d54SMilian Wolff struct srcline_node { 82021ac9d54SMilian Wolff u64 addr; 82121ac9d54SMilian Wolff char *srcline; 82221ac9d54SMilian Wolff struct rb_node rb_node; 82321ac9d54SMilian Wolff }; 82421ac9d54SMilian Wolff 82555ecd631SDavidlohr Bueso void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline) 82621ac9d54SMilian Wolff { 82755ecd631SDavidlohr Bueso struct rb_node **p = &tree->rb_root.rb_node; 82821ac9d54SMilian Wolff struct rb_node *parent = NULL; 82921ac9d54SMilian Wolff struct srcline_node *i, *node; 83055ecd631SDavidlohr Bueso bool leftmost = true; 83121ac9d54SMilian Wolff 83221ac9d54SMilian Wolff node = zalloc(sizeof(struct srcline_node)); 83321ac9d54SMilian Wolff if (!node) { 83421ac9d54SMilian Wolff perror("not enough memory for the srcline node"); 83521ac9d54SMilian Wolff return; 83621ac9d54SMilian Wolff } 83721ac9d54SMilian Wolff 83821ac9d54SMilian Wolff node->addr = addr; 83921ac9d54SMilian Wolff node->srcline = srcline; 84021ac9d54SMilian Wolff 84121ac9d54SMilian Wolff while (*p != NULL) { 84221ac9d54SMilian Wolff parent = *p; 84321ac9d54SMilian Wolff i = rb_entry(parent, struct srcline_node, rb_node); 84421ac9d54SMilian Wolff if (addr < i->addr) 84521ac9d54SMilian Wolff p = &(*p)->rb_left; 84655ecd631SDavidlohr Bueso else { 84721ac9d54SMilian Wolff p = &(*p)->rb_right; 84855ecd631SDavidlohr Bueso leftmost = false; 84955ecd631SDavidlohr Bueso } 85021ac9d54SMilian Wolff } 85121ac9d54SMilian Wolff rb_link_node(&node->rb_node, parent, p); 85255ecd631SDavidlohr Bueso rb_insert_color_cached(&node->rb_node, tree, leftmost); 85321ac9d54SMilian Wolff } 85421ac9d54SMilian Wolff 85555ecd631SDavidlohr Bueso char *srcline__tree_find(struct rb_root_cached *tree, u64 addr) 85621ac9d54SMilian Wolff { 85755ecd631SDavidlohr Bueso struct rb_node *n = tree->rb_root.rb_node; 85821ac9d54SMilian Wolff 85921ac9d54SMilian Wolff while (n) { 86021ac9d54SMilian Wolff struct srcline_node *i = rb_entry(n, struct srcline_node, 86121ac9d54SMilian Wolff rb_node); 86221ac9d54SMilian Wolff 86321ac9d54SMilian Wolff if (addr < i->addr) 86421ac9d54SMilian Wolff n = n->rb_left; 86521ac9d54SMilian Wolff else if (addr > i->addr) 86621ac9d54SMilian Wolff n = n->rb_right; 86721ac9d54SMilian Wolff else 86821ac9d54SMilian Wolff return i->srcline; 86921ac9d54SMilian Wolff } 87021ac9d54SMilian Wolff 87121ac9d54SMilian Wolff return NULL; 87221ac9d54SMilian Wolff } 87321ac9d54SMilian Wolff 87455ecd631SDavidlohr Bueso void srcline__tree_delete(struct rb_root_cached *tree) 87521ac9d54SMilian Wolff { 87621ac9d54SMilian Wolff struct srcline_node *pos; 87755ecd631SDavidlohr Bueso struct rb_node *next = rb_first_cached(tree); 87821ac9d54SMilian Wolff 87921ac9d54SMilian Wolff while (next) { 88021ac9d54SMilian Wolff pos = rb_entry(next, struct srcline_node, rb_node); 88121ac9d54SMilian Wolff next = rb_next(&pos->rb_node); 88255ecd631SDavidlohr Bueso rb_erase_cached(&pos->rb_node, tree); 88321ac9d54SMilian Wolff free_srcline(pos->srcline); 88421ac9d54SMilian Wolff zfree(&pos); 88521ac9d54SMilian Wolff } 88621ac9d54SMilian Wolff } 88721ac9d54SMilian Wolff 888fea0cf84SMilian Wolff struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr, 889fea0cf84SMilian Wolff struct symbol *sym) 890a64489c5SJin Yao { 891a64489c5SJin Yao const char *dso_name; 892a64489c5SJin Yao 893a64489c5SJin Yao dso_name = dso__name(dso); 894a64489c5SJin Yao if (dso_name == NULL) 895a64489c5SJin Yao return NULL; 896a64489c5SJin Yao 897fea0cf84SMilian Wolff return addr2inlines(dso_name, addr, dso, sym); 898a64489c5SJin Yao } 899a64489c5SJin Yao 900a64489c5SJin Yao void inline_node__delete(struct inline_node *node) 901a64489c5SJin Yao { 902a64489c5SJin Yao struct inline_list *ilist, *tmp; 903a64489c5SJin Yao 904a64489c5SJin Yao list_for_each_entry_safe(ilist, tmp, &node->val, list) { 905a64489c5SJin Yao list_del_init(&ilist->list); 9062be8832fSMilian Wolff free_srcline(ilist->srcline); 907fea0cf84SMilian Wolff /* only the inlined symbols are owned by the list */ 908fea0cf84SMilian Wolff if (ilist->symbol && ilist->symbol->inlined) 909fea0cf84SMilian Wolff symbol__delete(ilist->symbol); 910a64489c5SJin Yao free(ilist); 911a64489c5SJin Yao } 912a64489c5SJin Yao 913a64489c5SJin Yao free(node); 914a64489c5SJin Yao } 91511ea2515SMilian Wolff 91655ecd631SDavidlohr Bueso void inlines__tree_insert(struct rb_root_cached *tree, 91755ecd631SDavidlohr Bueso struct inline_node *inlines) 91811ea2515SMilian Wolff { 91955ecd631SDavidlohr Bueso struct rb_node **p = &tree->rb_root.rb_node; 92011ea2515SMilian Wolff struct rb_node *parent = NULL; 92111ea2515SMilian Wolff const u64 addr = inlines->addr; 92211ea2515SMilian Wolff struct inline_node *i; 92355ecd631SDavidlohr Bueso bool leftmost = true; 92411ea2515SMilian Wolff 92511ea2515SMilian Wolff while (*p != NULL) { 92611ea2515SMilian Wolff parent = *p; 92711ea2515SMilian Wolff i = rb_entry(parent, struct inline_node, rb_node); 92811ea2515SMilian Wolff if (addr < i->addr) 92911ea2515SMilian Wolff p = &(*p)->rb_left; 93055ecd631SDavidlohr Bueso else { 93111ea2515SMilian Wolff p = &(*p)->rb_right; 93255ecd631SDavidlohr Bueso leftmost = false; 93355ecd631SDavidlohr Bueso } 93411ea2515SMilian Wolff } 93511ea2515SMilian Wolff rb_link_node(&inlines->rb_node, parent, p); 93655ecd631SDavidlohr Bueso rb_insert_color_cached(&inlines->rb_node, tree, leftmost); 93711ea2515SMilian Wolff } 93811ea2515SMilian Wolff 93955ecd631SDavidlohr Bueso struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr) 94011ea2515SMilian Wolff { 94155ecd631SDavidlohr Bueso struct rb_node *n = tree->rb_root.rb_node; 94211ea2515SMilian Wolff 94311ea2515SMilian Wolff while (n) { 94411ea2515SMilian Wolff struct inline_node *i = rb_entry(n, struct inline_node, 94511ea2515SMilian Wolff rb_node); 94611ea2515SMilian Wolff 94711ea2515SMilian Wolff if (addr < i->addr) 94811ea2515SMilian Wolff n = n->rb_left; 94911ea2515SMilian Wolff else if (addr > i->addr) 95011ea2515SMilian Wolff n = n->rb_right; 95111ea2515SMilian Wolff else 95211ea2515SMilian Wolff return i; 95311ea2515SMilian Wolff } 95411ea2515SMilian Wolff 95511ea2515SMilian Wolff return NULL; 95611ea2515SMilian Wolff } 95711ea2515SMilian Wolff 95855ecd631SDavidlohr Bueso void inlines__tree_delete(struct rb_root_cached *tree) 95911ea2515SMilian Wolff { 96011ea2515SMilian Wolff struct inline_node *pos; 96155ecd631SDavidlohr Bueso struct rb_node *next = rb_first_cached(tree); 96211ea2515SMilian Wolff 96311ea2515SMilian Wolff while (next) { 96411ea2515SMilian Wolff pos = rb_entry(next, struct inline_node, rb_node); 96511ea2515SMilian Wolff next = rb_next(&pos->rb_node); 96655ecd631SDavidlohr Bueso rb_erase_cached(&pos->rb_node, tree); 96711ea2515SMilian Wolff inline_node__delete(pos); 96811ea2515SMilian Wolff } 96911ea2515SMilian Wolff } 970