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 26922db21dSArnaldo Carvalho de Melo char *srcline__unknown = (char *)"??:0"; 27922db21dSArnaldo Carvalho de Melo 285580338dSJin Yao static const char *dso__name(struct dso *dso) 295580338dSJin Yao { 305580338dSJin Yao const char *dso_name; 315580338dSJin Yao 325580338dSJin Yao if (dso->symsrc_filename) 335580338dSJin Yao dso_name = dso->symsrc_filename; 345580338dSJin Yao else 355580338dSJin Yao dso_name = dso->long_name; 365580338dSJin Yao 375580338dSJin Yao if (dso_name[0] == '[') 385580338dSJin Yao return NULL; 395580338dSJin Yao 405580338dSJin Yao if (!strncmp(dso_name, "/tmp/perf-", 10)) 415580338dSJin Yao return NULL; 425580338dSJin Yao 435580338dSJin Yao return dso_name; 445580338dSJin Yao } 455580338dSJin Yao 462be8832fSMilian Wolff static int inline_list__append(struct symbol *symbol, char *srcline, 472be8832fSMilian Wolff struct inline_node *node) 48a64489c5SJin Yao { 49a64489c5SJin Yao struct inline_list *ilist; 50a64489c5SJin Yao 51a64489c5SJin Yao ilist = zalloc(sizeof(*ilist)); 52a64489c5SJin Yao if (ilist == NULL) 53a64489c5SJin Yao return -1; 54a64489c5SJin Yao 55fea0cf84SMilian Wolff ilist->symbol = symbol; 562be8832fSMilian Wolff ilist->srcline = srcline; 57a64489c5SJin Yao 5828071f51SMilian Wolff if (callchain_param.order == ORDER_CALLEE) 59a64489c5SJin Yao list_add_tail(&ilist->list, &node->val); 6028071f51SMilian Wolff else 6128071f51SMilian Wolff list_add(&ilist->list, &node->val); 62a64489c5SJin Yao 63a64489c5SJin Yao return 0; 64a64489c5SJin Yao } 65a64489c5SJin Yao 662be8832fSMilian Wolff /* basename version that takes a const input string */ 672be8832fSMilian Wolff static const char *gnu_basename(const char *path) 682be8832fSMilian Wolff { 692be8832fSMilian Wolff const char *base = strrchr(path, '/'); 702be8832fSMilian Wolff 712be8832fSMilian Wolff return base ? base + 1 : path; 722be8832fSMilian Wolff } 732be8832fSMilian Wolff 742be8832fSMilian Wolff static char *srcline_from_fileline(const char *file, unsigned int line) 752be8832fSMilian Wolff { 762be8832fSMilian Wolff char *srcline; 772be8832fSMilian Wolff 782be8832fSMilian Wolff if (!file) 792be8832fSMilian Wolff return NULL; 802be8832fSMilian Wolff 812be8832fSMilian Wolff if (!srcline_full_filename) 822be8832fSMilian Wolff file = gnu_basename(file); 832be8832fSMilian Wolff 842be8832fSMilian Wolff if (asprintf(&srcline, "%s:%u", file, line) < 0) 852be8832fSMilian Wolff return NULL; 862be8832fSMilian Wolff 872be8832fSMilian Wolff return srcline; 882be8832fSMilian Wolff } 892be8832fSMilian Wolff 907285cf33SNamhyung Kim static struct symbol *new_inline_sym(struct dso *dso, 917285cf33SNamhyung Kim struct symbol *base_sym, 927285cf33SNamhyung Kim const char *funcname) 937285cf33SNamhyung Kim { 947285cf33SNamhyung Kim struct symbol *inline_sym; 957285cf33SNamhyung Kim char *demangled = NULL; 967285cf33SNamhyung Kim 97d4046e8eSMilian Wolff if (!funcname) 98d4046e8eSMilian Wolff funcname = "??"; 99d4046e8eSMilian Wolff 1007285cf33SNamhyung Kim if (dso) { 1017285cf33SNamhyung Kim demangled = dso__demangle_sym(dso, 0, funcname); 1027285cf33SNamhyung Kim if (demangled) 1037285cf33SNamhyung Kim funcname = demangled; 1047285cf33SNamhyung Kim } 1057285cf33SNamhyung Kim 1067285cf33SNamhyung Kim if (base_sym && strcmp(funcname, base_sym->name) == 0) { 1077285cf33SNamhyung Kim /* reuse the real, existing symbol */ 1087285cf33SNamhyung Kim inline_sym = base_sym; 1097285cf33SNamhyung Kim /* ensure that we don't alias an inlined symbol, which could 1107285cf33SNamhyung Kim * lead to double frees in inline_node__delete 1117285cf33SNamhyung Kim */ 1127285cf33SNamhyung Kim assert(!base_sym->inlined); 1137285cf33SNamhyung Kim } else { 1147285cf33SNamhyung Kim /* create a fake symbol for the inline frame */ 1157285cf33SNamhyung Kim inline_sym = symbol__new(base_sym ? base_sym->start : 0, 1167346195eSHe Kuang base_sym ? (base_sym->end - base_sym->start) : 0, 1177285cf33SNamhyung Kim base_sym ? base_sym->binding : 0, 118af30bffaSArnaldo Carvalho de Melo base_sym ? base_sym->type : 0, 1197285cf33SNamhyung Kim funcname); 1207285cf33SNamhyung Kim if (inline_sym) 1217285cf33SNamhyung Kim inline_sym->inlined = 1; 1227285cf33SNamhyung Kim } 1237285cf33SNamhyung Kim 1247285cf33SNamhyung Kim free(demangled); 1257285cf33SNamhyung Kim 1267285cf33SNamhyung Kim return inline_sym; 1277285cf33SNamhyung Kim } 1287285cf33SNamhyung Kim 129be8ecc57STony Garnock-Jones #define MAX_INLINE_NEST 1024 130be8ecc57STony Garnock-Jones 1312f48fcd8SRoberto Vitillo #ifdef HAVE_LIBBFD_SUPPORT 1322f48fcd8SRoberto Vitillo 1332f48fcd8SRoberto Vitillo /* 1342f48fcd8SRoberto Vitillo * Implement addr2line using libbfd. 1352f48fcd8SRoberto Vitillo */ 1362f48fcd8SRoberto Vitillo #define PACKAGE "perf" 1372f48fcd8SRoberto Vitillo #include <bfd.h> 1382f48fcd8SRoberto Vitillo 1392f48fcd8SRoberto Vitillo struct a2l_data { 1402f48fcd8SRoberto Vitillo const char *input; 141ac931f87SWang Nan u64 addr; 1422f48fcd8SRoberto Vitillo 1432f48fcd8SRoberto Vitillo bool found; 1442f48fcd8SRoberto Vitillo const char *filename; 1452f48fcd8SRoberto Vitillo const char *funcname; 1462f48fcd8SRoberto Vitillo unsigned line; 1472f48fcd8SRoberto Vitillo 1482f48fcd8SRoberto Vitillo bfd *abfd; 1492f48fcd8SRoberto Vitillo asymbol **syms; 1502f48fcd8SRoberto Vitillo }; 1512f48fcd8SRoberto Vitillo 1522f48fcd8SRoberto Vitillo static int bfd_error(const char *string) 1532f48fcd8SRoberto Vitillo { 1542f48fcd8SRoberto Vitillo const char *errmsg; 1552f48fcd8SRoberto Vitillo 1562f48fcd8SRoberto Vitillo errmsg = bfd_errmsg(bfd_get_error()); 1572f48fcd8SRoberto Vitillo fflush(stdout); 1582f48fcd8SRoberto Vitillo 1592f48fcd8SRoberto Vitillo if (string) 1602f48fcd8SRoberto Vitillo pr_debug("%s: %s\n", string, errmsg); 1612f48fcd8SRoberto Vitillo else 1622f48fcd8SRoberto Vitillo pr_debug("%s\n", errmsg); 1632f48fcd8SRoberto Vitillo 1642f48fcd8SRoberto Vitillo return -1; 1652f48fcd8SRoberto Vitillo } 1662f48fcd8SRoberto Vitillo 1672f48fcd8SRoberto Vitillo static int slurp_symtab(bfd *abfd, struct a2l_data *a2l) 1682f48fcd8SRoberto Vitillo { 1692f48fcd8SRoberto Vitillo long storage; 1702f48fcd8SRoberto Vitillo long symcount; 1712f48fcd8SRoberto Vitillo asymbol **syms; 1722f48fcd8SRoberto Vitillo bfd_boolean dynamic = FALSE; 1732f48fcd8SRoberto Vitillo 1742f48fcd8SRoberto Vitillo if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0) 1752f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1762f48fcd8SRoberto Vitillo 1772f48fcd8SRoberto Vitillo storage = bfd_get_symtab_upper_bound(abfd); 1782f48fcd8SRoberto Vitillo if (storage == 0L) { 1792f48fcd8SRoberto Vitillo storage = bfd_get_dynamic_symtab_upper_bound(abfd); 1802f48fcd8SRoberto Vitillo dynamic = TRUE; 1812f48fcd8SRoberto Vitillo } 1822f48fcd8SRoberto Vitillo if (storage < 0L) 1832f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1842f48fcd8SRoberto Vitillo 1852f48fcd8SRoberto Vitillo syms = malloc(storage); 1862f48fcd8SRoberto Vitillo if (dynamic) 1872f48fcd8SRoberto Vitillo symcount = bfd_canonicalize_dynamic_symtab(abfd, syms); 1882f48fcd8SRoberto Vitillo else 1892f48fcd8SRoberto Vitillo symcount = bfd_canonicalize_symtab(abfd, syms); 1902f48fcd8SRoberto Vitillo 1912f48fcd8SRoberto Vitillo if (symcount < 0) { 1922f48fcd8SRoberto Vitillo free(syms); 1932f48fcd8SRoberto Vitillo return bfd_error(bfd_get_filename(abfd)); 1942f48fcd8SRoberto Vitillo } 1952f48fcd8SRoberto Vitillo 1962f48fcd8SRoberto Vitillo a2l->syms = syms; 1972f48fcd8SRoberto Vitillo return 0; 1982f48fcd8SRoberto Vitillo } 1992f48fcd8SRoberto Vitillo 2002f48fcd8SRoberto Vitillo static void find_address_in_section(bfd *abfd, asection *section, void *data) 2012f48fcd8SRoberto Vitillo { 2022f48fcd8SRoberto Vitillo bfd_vma pc, vma; 2032f48fcd8SRoberto Vitillo bfd_size_type size; 2042f48fcd8SRoberto Vitillo struct a2l_data *a2l = data; 2050ada120cSChangbin Du flagword flags; 2062f48fcd8SRoberto Vitillo 2072f48fcd8SRoberto Vitillo if (a2l->found) 2082f48fcd8SRoberto Vitillo return; 2092f48fcd8SRoberto Vitillo 2100ada120cSChangbin Du #ifdef bfd_get_section_flags 2110ada120cSChangbin Du flags = bfd_get_section_flags(abfd, section); 2120ada120cSChangbin Du #else 2130ada120cSChangbin Du flags = bfd_section_flags(section); 2140ada120cSChangbin Du #endif 2150ada120cSChangbin Du if ((flags & SEC_ALLOC) == 0) 2162f48fcd8SRoberto Vitillo return; 2172f48fcd8SRoberto Vitillo 2182f48fcd8SRoberto Vitillo pc = a2l->addr; 2190ada120cSChangbin Du #ifdef bfd_get_section_vma 2202f48fcd8SRoberto Vitillo vma = bfd_get_section_vma(abfd, section); 2210ada120cSChangbin Du #else 2220ada120cSChangbin Du vma = bfd_section_vma(section); 2230ada120cSChangbin Du #endif 2240ada120cSChangbin Du #ifdef bfd_get_section_size 2252f48fcd8SRoberto Vitillo size = bfd_get_section_size(section); 2260ada120cSChangbin Du #else 2270ada120cSChangbin Du size = bfd_section_size(section); 2280ada120cSChangbin Du #endif 2292f48fcd8SRoberto Vitillo 2302f48fcd8SRoberto Vitillo if (pc < vma || pc >= vma + size) 2312f48fcd8SRoberto Vitillo return; 2322f48fcd8SRoberto Vitillo 2332f48fcd8SRoberto Vitillo a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma, 2342f48fcd8SRoberto Vitillo &a2l->filename, &a2l->funcname, 2352f48fcd8SRoberto Vitillo &a2l->line); 236d964b1cdSMilian Wolff 237d964b1cdSMilian Wolff if (a2l->filename && !strlen(a2l->filename)) 238d964b1cdSMilian Wolff a2l->filename = NULL; 2392f48fcd8SRoberto Vitillo } 2402f48fcd8SRoberto Vitillo 2412f48fcd8SRoberto Vitillo static struct a2l_data *addr2line_init(const char *path) 2422f48fcd8SRoberto Vitillo { 2432f48fcd8SRoberto Vitillo bfd *abfd; 2442f48fcd8SRoberto Vitillo struct a2l_data *a2l = NULL; 2452f48fcd8SRoberto Vitillo 2462f48fcd8SRoberto Vitillo abfd = bfd_openr(path, NULL); 2472f48fcd8SRoberto Vitillo if (abfd == NULL) 2482f48fcd8SRoberto Vitillo return NULL; 2492f48fcd8SRoberto Vitillo 2502f48fcd8SRoberto Vitillo if (!bfd_check_format(abfd, bfd_object)) 2512f48fcd8SRoberto Vitillo goto out; 2522f48fcd8SRoberto Vitillo 2532f48fcd8SRoberto Vitillo a2l = zalloc(sizeof(*a2l)); 2542f48fcd8SRoberto Vitillo if (a2l == NULL) 2552f48fcd8SRoberto Vitillo goto out; 2562f48fcd8SRoberto Vitillo 2572f48fcd8SRoberto Vitillo a2l->abfd = abfd; 2582f48fcd8SRoberto Vitillo a2l->input = strdup(path); 2592f48fcd8SRoberto Vitillo if (a2l->input == NULL) 2602f48fcd8SRoberto Vitillo goto out; 2612f48fcd8SRoberto Vitillo 2622f48fcd8SRoberto Vitillo if (slurp_symtab(abfd, a2l)) 2632f48fcd8SRoberto Vitillo goto out; 2642f48fcd8SRoberto Vitillo 2652f48fcd8SRoberto Vitillo return a2l; 2662f48fcd8SRoberto Vitillo 2672f48fcd8SRoberto Vitillo out: 2682f48fcd8SRoberto Vitillo if (a2l) { 2697d16c634SNamhyung Kim zfree((char **)&a2l->input); 2702f48fcd8SRoberto Vitillo free(a2l); 2712f48fcd8SRoberto Vitillo } 2722f48fcd8SRoberto Vitillo bfd_close(abfd); 2732f48fcd8SRoberto Vitillo return NULL; 2742f48fcd8SRoberto Vitillo } 2752f48fcd8SRoberto Vitillo 2762f48fcd8SRoberto Vitillo static void addr2line_cleanup(struct a2l_data *a2l) 2772f48fcd8SRoberto Vitillo { 2782f48fcd8SRoberto Vitillo if (a2l->abfd) 2792f48fcd8SRoberto Vitillo bfd_close(a2l->abfd); 2807d16c634SNamhyung Kim zfree((char **)&a2l->input); 28174cf249dSArnaldo Carvalho de Melo zfree(&a2l->syms); 2822f48fcd8SRoberto Vitillo free(a2l); 2832f48fcd8SRoberto Vitillo } 2842f48fcd8SRoberto Vitillo 2854d53b9d5SMilian Wolff static int inline_list__append_dso_a2l(struct dso *dso, 286fea0cf84SMilian Wolff struct inline_node *node, 287fea0cf84SMilian Wolff struct symbol *sym) 2884d53b9d5SMilian Wolff { 2894d53b9d5SMilian Wolff struct a2l_data *a2l = dso->a2l; 290fea0cf84SMilian Wolff struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname); 2912be8832fSMilian Wolff char *srcline = NULL; 2924d53b9d5SMilian Wolff 2932be8832fSMilian Wolff if (a2l->filename) 2942be8832fSMilian Wolff srcline = srcline_from_fileline(a2l->filename, a2l->line); 2952be8832fSMilian Wolff 2962be8832fSMilian Wolff return inline_list__append(inline_sym, srcline, node); 2974d53b9d5SMilian Wolff } 2984d53b9d5SMilian Wolff 299ac931f87SWang Nan static int addr2line(const char *dso_name, u64 addr, 3002f84b42bSAndi Kleen char **file, unsigned int *line, struct dso *dso, 301fea0cf84SMilian Wolff bool unwind_inlines, struct inline_node *node, 302fea0cf84SMilian Wolff struct symbol *sym) 3032f48fcd8SRoberto Vitillo { 3042f48fcd8SRoberto Vitillo int ret = 0; 305454ff00fSAdrian Hunter struct a2l_data *a2l = dso->a2l; 3062f48fcd8SRoberto Vitillo 307454ff00fSAdrian Hunter if (!a2l) { 308454ff00fSAdrian Hunter dso->a2l = addr2line_init(dso_name); 309454ff00fSAdrian Hunter a2l = dso->a2l; 310454ff00fSAdrian Hunter } 311454ff00fSAdrian Hunter 3122f48fcd8SRoberto Vitillo if (a2l == NULL) { 313b10c78c5SJin Yao if (!symbol_conf.disable_add2line_warn) 3142f48fcd8SRoberto Vitillo pr_warning("addr2line_init failed for %s\n", dso_name); 3152f48fcd8SRoberto Vitillo return 0; 3162f48fcd8SRoberto Vitillo } 3172f48fcd8SRoberto Vitillo 3182f48fcd8SRoberto Vitillo a2l->addr = addr; 319454ff00fSAdrian Hunter a2l->found = false; 320454ff00fSAdrian Hunter 3212f48fcd8SRoberto Vitillo bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l); 3222f48fcd8SRoberto Vitillo 323b21cc978SMilian Wolff if (!a2l->found) 324b21cc978SMilian Wolff return 0; 325b21cc978SMilian Wolff 326b21cc978SMilian Wolff if (unwind_inlines) { 3272f84b42bSAndi Kleen int cnt = 0; 3282f84b42bSAndi Kleen 329fea0cf84SMilian Wolff if (node && inline_list__append_dso_a2l(dso, node, sym)) 3304d53b9d5SMilian Wolff return 0; 3314d53b9d5SMilian Wolff 3322f84b42bSAndi Kleen while (bfd_find_inliner_info(a2l->abfd, &a2l->filename, 3332f84b42bSAndi Kleen &a2l->funcname, &a2l->line) && 334a64489c5SJin Yao cnt++ < MAX_INLINE_NEST) { 335a64489c5SJin Yao 336d964b1cdSMilian Wolff if (a2l->filename && !strlen(a2l->filename)) 337d964b1cdSMilian Wolff a2l->filename = NULL; 338d964b1cdSMilian Wolff 339a64489c5SJin Yao if (node != NULL) { 340fea0cf84SMilian Wolff if (inline_list__append_dso_a2l(dso, node, sym)) 341a64489c5SJin Yao return 0; 342b21cc978SMilian Wolff // found at least one inline frame 343b21cc978SMilian Wolff ret = 1; 344a64489c5SJin Yao } 345a64489c5SJin Yao } 3462f84b42bSAndi Kleen } 3472f84b42bSAndi Kleen 348b21cc978SMilian Wolff if (file) { 349b21cc978SMilian Wolff *file = a2l->filename ? strdup(a2l->filename) : NULL; 350b21cc978SMilian Wolff ret = *file ? 1 : 0; 3512f48fcd8SRoberto Vitillo } 3522f48fcd8SRoberto Vitillo 353b21cc978SMilian Wolff if (line) 354b21cc978SMilian Wolff *line = a2l->line; 355b21cc978SMilian Wolff 3562f48fcd8SRoberto Vitillo return ret; 3572f48fcd8SRoberto Vitillo } 3582f48fcd8SRoberto Vitillo 359454ff00fSAdrian Hunter void dso__free_a2l(struct dso *dso) 360454ff00fSAdrian Hunter { 361454ff00fSAdrian Hunter struct a2l_data *a2l = dso->a2l; 362454ff00fSAdrian Hunter 363454ff00fSAdrian Hunter if (!a2l) 364454ff00fSAdrian Hunter return; 365454ff00fSAdrian Hunter 366454ff00fSAdrian Hunter addr2line_cleanup(a2l); 367454ff00fSAdrian Hunter 368454ff00fSAdrian Hunter dso->a2l = NULL; 369454ff00fSAdrian Hunter } 370454ff00fSAdrian Hunter 3712f48fcd8SRoberto Vitillo #else /* HAVE_LIBBFD_SUPPORT */ 3722f48fcd8SRoberto Vitillo 3735580338dSJin Yao static int filename_split(char *filename, unsigned int *line_nr) 3745580338dSJin Yao { 3755580338dSJin Yao char *sep; 3765580338dSJin Yao 3775580338dSJin Yao sep = strchr(filename, '\n'); 3785580338dSJin Yao if (sep) 3795580338dSJin Yao *sep = '\0'; 3805580338dSJin Yao 3815580338dSJin Yao if (!strcmp(filename, "??:0")) 3825580338dSJin Yao return 0; 3835580338dSJin Yao 3845580338dSJin Yao sep = strchr(filename, ':'); 3855580338dSJin Yao if (sep) { 3865580338dSJin Yao *sep++ = '\0'; 3875580338dSJin Yao *line_nr = strtoul(sep, NULL, 0); 3885580338dSJin Yao return 1; 3895580338dSJin Yao } 3905580338dSJin Yao 3915580338dSJin Yao return 0; 3925580338dSJin Yao } 3935580338dSJin Yao 394b3801e79SIan Rogers static void addr2line_subprocess_cleanup(struct child_process *a2l) 395f048d548SNamhyung Kim { 396b3801e79SIan Rogers if (a2l->pid != -1) { 397b3801e79SIan Rogers kill(a2l->pid, SIGKILL); 398b3801e79SIan Rogers finish_command(a2l); /* ignore result, we don't care */ 399b3801e79SIan Rogers a2l->pid = -1; 400be8ecc57STony Garnock-Jones } 401be8ecc57STony Garnock-Jones 402be8ecc57STony Garnock-Jones free(a2l); 403be8ecc57STony Garnock-Jones } 404be8ecc57STony Garnock-Jones 405b3801e79SIan Rogers static struct child_process *addr2line_subprocess_init(const char *addr2line_path, 40657594454SIan Rogers const char *binary_path) 407be8ecc57STony Garnock-Jones { 40857594454SIan Rogers const char *argv[] = { 40957594454SIan Rogers addr2line_path ?: "addr2line", 41057594454SIan Rogers "-e", binary_path, 41157594454SIan Rogers "-i", "-f", NULL 41257594454SIan Rogers }; 413b3801e79SIan Rogers struct child_process *a2l = zalloc(sizeof(*a2l)); 414be8ecc57STony Garnock-Jones int start_command_status = 0; 415be8ecc57STony Garnock-Jones 416b3801e79SIan Rogers if (a2l == NULL) { 417b3801e79SIan Rogers pr_err("Failed to allocate memory for addr2line"); 418b3801e79SIan Rogers return NULL; 419b3801e79SIan Rogers } 420be8ecc57STony Garnock-Jones 421b3801e79SIan Rogers a2l->pid = -1; 422b3801e79SIan Rogers a2l->in = -1; 423b3801e79SIan Rogers a2l->out = -1; 424b3801e79SIan Rogers a2l->no_stderr = 1; 425be8ecc57STony Garnock-Jones 426b3801e79SIan Rogers a2l->argv = argv; 427b3801e79SIan Rogers start_command_status = start_command(a2l); 428b3801e79SIan Rogers a2l->argv = NULL; /* it's not used after start_command; avoid dangling pointers */ 429be8ecc57STony Garnock-Jones 430be8ecc57STony Garnock-Jones if (start_command_status != 0) { 43157594454SIan Rogers pr_warning("could not start addr2line (%s) for %s: start_command return code %d\n", 43257594454SIan Rogers addr2line_path, binary_path, start_command_status); 433be8ecc57STony Garnock-Jones addr2line_subprocess_cleanup(a2l); 434be8ecc57STony Garnock-Jones return NULL; 435be8ecc57STony Garnock-Jones } 436be8ecc57STony Garnock-Jones 437b3801e79SIan Rogers return a2l; 438b3801e79SIan Rogers } 439b3801e79SIan Rogers 4402c4b9280SIan Rogers enum a2l_style { 4412c4b9280SIan Rogers BROKEN, 4422c4b9280SIan Rogers GNU_BINUTILS, 4432c4b9280SIan Rogers LLVM, 4442c4b9280SIan Rogers }; 4452c4b9280SIan Rogers 446*c7a0023aSIan Rogers static enum a2l_style addr2line_configure(struct child_process *a2l, const char *dso_name) 4472c4b9280SIan Rogers { 4482c4b9280SIan Rogers static bool cached; 4492c4b9280SIan Rogers static enum a2l_style style; 4502c4b9280SIan Rogers 4512c4b9280SIan Rogers if (!cached) { 4522c4b9280SIan Rogers char buf[128]; 4532c4b9280SIan Rogers struct io io; 4542c4b9280SIan Rogers int ch; 455*c7a0023aSIan Rogers int lines; 4562c4b9280SIan Rogers 4572c4b9280SIan Rogers if (write(a2l->in, ",\n", 2) != 2) 4582c4b9280SIan Rogers return BROKEN; 4592c4b9280SIan Rogers 4602c4b9280SIan Rogers io__init(&io, a2l->out, buf, sizeof(buf)); 4612c4b9280SIan Rogers ch = io__get_char(&io); 4622c4b9280SIan Rogers if (ch == ',') { 4632c4b9280SIan Rogers style = LLVM; 4642c4b9280SIan Rogers cached = true; 465*c7a0023aSIan Rogers lines = 1; 4662c4b9280SIan Rogers } else if (ch == '?') { 4672c4b9280SIan Rogers style = GNU_BINUTILS; 4682c4b9280SIan Rogers cached = true; 469*c7a0023aSIan Rogers lines = 2; 4702c4b9280SIan Rogers } else { 471*c7a0023aSIan Rogers if (!symbol_conf.disable_add2line_warn) { 472*c7a0023aSIan Rogers char *output = NULL; 473*c7a0023aSIan Rogers size_t output_len; 474*c7a0023aSIan Rogers 475*c7a0023aSIan Rogers io__getline(&io, &output, &output_len); 476*c7a0023aSIan Rogers pr_warning("%s %s: addr2line configuration failed\n", 477*c7a0023aSIan Rogers __func__, dso_name); 478*c7a0023aSIan Rogers pr_warning("\t%c%s", ch, output); 4792c4b9280SIan Rogers } 480*c7a0023aSIan Rogers return BROKEN; 481*c7a0023aSIan Rogers } 482*c7a0023aSIan Rogers while (lines) { 4832c4b9280SIan Rogers ch = io__get_char(&io); 484*c7a0023aSIan Rogers if (ch <= 0) 485*c7a0023aSIan Rogers break; 486*c7a0023aSIan Rogers if (ch == '\n') 487*c7a0023aSIan Rogers lines--; 4882c4b9280SIan Rogers } 48975a616c6SIan Rogers /* Ignore SIGPIPE in the event addr2line exits. */ 49075a616c6SIan Rogers signal(SIGPIPE, SIG_IGN); 4912c4b9280SIan Rogers } 4922c4b9280SIan Rogers return style; 4932c4b9280SIan Rogers } 4942c4b9280SIan Rogers 495b3801e79SIan Rogers static int read_addr2line_record(struct io *io, 4962c4b9280SIan Rogers enum a2l_style style, 497be8ecc57STony Garnock-Jones char **function, 498be8ecc57STony Garnock-Jones char **filename, 499be8ecc57STony Garnock-Jones unsigned int *line_nr) 500be8ecc57STony Garnock-Jones { 501be8ecc57STony Garnock-Jones /* 502be8ecc57STony Garnock-Jones * Returns: 503be8ecc57STony Garnock-Jones * -1 ==> error 504be8ecc57STony Garnock-Jones * 0 ==> sentinel (or other ill-formed) record read 505be8ecc57STony Garnock-Jones * 1 ==> a genuine record read 506be8ecc57STony Garnock-Jones */ 507be8ecc57STony Garnock-Jones char *line = NULL; 508be8ecc57STony Garnock-Jones size_t line_len = 0; 509be8ecc57STony Garnock-Jones unsigned int dummy_line_nr = 0; 510be8ecc57STony Garnock-Jones int ret = -1; 511be8ecc57STony Garnock-Jones 512be8ecc57STony Garnock-Jones if (function != NULL) 513be8ecc57STony Garnock-Jones zfree(function); 514be8ecc57STony Garnock-Jones 515be8ecc57STony Garnock-Jones if (filename != NULL) 516be8ecc57STony Garnock-Jones zfree(filename); 517be8ecc57STony Garnock-Jones 518be8ecc57STony Garnock-Jones if (line_nr != NULL) 519be8ecc57STony Garnock-Jones *line_nr = 0; 520be8ecc57STony Garnock-Jones 521b3801e79SIan Rogers if (io__getline(io, &line, &line_len) < 0 || !line_len) 522be8ecc57STony Garnock-Jones goto error; 5232c4b9280SIan Rogers 5242c4b9280SIan Rogers if (style == LLVM && line_len == 2 && line[0] == ',') { 5252c4b9280SIan Rogers zfree(&line); 5262c4b9280SIan Rogers return 0; 5272c4b9280SIan Rogers } 5282c4b9280SIan Rogers 529be8ecc57STony Garnock-Jones if (function != NULL) 530be8ecc57STony Garnock-Jones *function = strdup(strim(line)); 531be8ecc57STony Garnock-Jones 532be8ecc57STony Garnock-Jones zfree(&line); 533be8ecc57STony Garnock-Jones line_len = 0; 534be8ecc57STony Garnock-Jones 535b3801e79SIan Rogers if (io__getline(io, &line, &line_len) < 0 || !line_len) 536be8ecc57STony Garnock-Jones goto error; 537be8ecc57STony Garnock-Jones 5382c4b9280SIan Rogers if (filename_split(line, line_nr == NULL ? &dummy_line_nr : line_nr) == 0 && 5392c4b9280SIan Rogers style == GNU_BINUTILS) { 540be8ecc57STony Garnock-Jones ret = 0; 541be8ecc57STony Garnock-Jones goto error; 542be8ecc57STony Garnock-Jones } 543be8ecc57STony Garnock-Jones 544be8ecc57STony Garnock-Jones if (filename != NULL) 545be8ecc57STony Garnock-Jones *filename = strdup(line); 546be8ecc57STony Garnock-Jones 547be8ecc57STony Garnock-Jones zfree(&line); 548be8ecc57STony Garnock-Jones line_len = 0; 549be8ecc57STony Garnock-Jones 550be8ecc57STony Garnock-Jones return 1; 551be8ecc57STony Garnock-Jones 552be8ecc57STony Garnock-Jones error: 553be8ecc57STony Garnock-Jones free(line); 554be8ecc57STony Garnock-Jones if (function != NULL) 555be8ecc57STony Garnock-Jones zfree(function); 556be8ecc57STony Garnock-Jones if (filename != NULL) 557be8ecc57STony Garnock-Jones zfree(filename); 558f048d548SNamhyung Kim return ret; 559f048d548SNamhyung Kim } 560454ff00fSAdrian Hunter 561be8ecc57STony Garnock-Jones static int inline_list__append_record(struct dso *dso, 562be8ecc57STony Garnock-Jones struct inline_node *node, 563be8ecc57STony Garnock-Jones struct symbol *sym, 564be8ecc57STony Garnock-Jones const char *function, 565be8ecc57STony Garnock-Jones const char *filename, 566be8ecc57STony Garnock-Jones unsigned int line_nr) 567454ff00fSAdrian Hunter { 568be8ecc57STony Garnock-Jones struct symbol *inline_sym = new_inline_sym(dso, sym, function); 569be8ecc57STony Garnock-Jones 570be8ecc57STony Garnock-Jones return inline_list__append(inline_sym, srcline_from_fileline(filename, line_nr), node); 571454ff00fSAdrian Hunter } 572454ff00fSAdrian Hunter 573be8ecc57STony Garnock-Jones static int addr2line(const char *dso_name, u64 addr, 574be8ecc57STony Garnock-Jones char **file, unsigned int *line_nr, 575be8ecc57STony Garnock-Jones struct dso *dso, 576be8ecc57STony Garnock-Jones bool unwind_inlines, 577be8ecc57STony Garnock-Jones struct inline_node *node, 578be8ecc57STony Garnock-Jones struct symbol *sym __maybe_unused) 579be8ecc57STony Garnock-Jones { 580b3801e79SIan Rogers struct child_process *a2l = dso->a2l; 581be8ecc57STony Garnock-Jones char *record_function = NULL; 582be8ecc57STony Garnock-Jones char *record_filename = NULL; 583be8ecc57STony Garnock-Jones unsigned int record_line_nr = 0; 584be8ecc57STony Garnock-Jones int record_status = -1; 585be8ecc57STony Garnock-Jones int ret = 0; 586be8ecc57STony Garnock-Jones size_t inline_count = 0; 587b3801e79SIan Rogers int len; 588b3801e79SIan Rogers char buf[128]; 589b3801e79SIan Rogers ssize_t written; 590b3801e79SIan Rogers struct io io; 5912c4b9280SIan Rogers enum a2l_style a2l_style; 592be8ecc57STony Garnock-Jones 593be8ecc57STony Garnock-Jones if (!a2l) { 5943b27222dSNamhyung Kim if (!filename__has_section(dso_name, ".debug_line")) 5953b27222dSNamhyung Kim goto out; 5963b27222dSNamhyung Kim 59757594454SIan Rogers dso->a2l = addr2line_subprocess_init(symbol_conf.addr2line_path, 59857594454SIan Rogers dso_name); 599be8ecc57STony Garnock-Jones a2l = dso->a2l; 600be8ecc57STony Garnock-Jones } 601be8ecc57STony Garnock-Jones 602be8ecc57STony Garnock-Jones if (a2l == NULL) { 603be8ecc57STony Garnock-Jones if (!symbol_conf.disable_add2line_warn) 604be8ecc57STony Garnock-Jones pr_warning("%s %s: addr2line_subprocess_init failed\n", __func__, dso_name); 605be8ecc57STony Garnock-Jones goto out; 606be8ecc57STony Garnock-Jones } 607*c7a0023aSIan Rogers a2l_style = addr2line_configure(a2l, dso_name); 608*c7a0023aSIan Rogers if (a2l_style == BROKEN) 6092c4b9280SIan Rogers goto out; 610be8ecc57STony Garnock-Jones 611be8ecc57STony Garnock-Jones /* 612be8ecc57STony Garnock-Jones * Send our request and then *deliberately* send something that can't be interpreted as 613be8ecc57STony Garnock-Jones * a valid address to ask addr2line about (namely, ","). This causes addr2line to first 614be8ecc57STony Garnock-Jones * write out the answer to our request, in an unbounded/unknown number of records, and 6152c4b9280SIan Rogers * then to write out the lines "??" and "??:0", for GNU binutils, or "," for 6162c4b9280SIan Rogers * llvm-addr2line, so that we can detect when it has finished giving us anything 6172c4b9280SIan Rogers * useful. With GNU binutils, we have to be careful about the first record, though, 6182c4b9280SIan Rogers * because it may be genuinely unknown, in which case we'll get two sets of "??"/"??:0" 6192c4b9280SIan Rogers * lines. 620be8ecc57STony Garnock-Jones */ 621b3801e79SIan Rogers len = snprintf(buf, sizeof(buf), "%016"PRIx64"\n,\n", addr); 622b3801e79SIan Rogers written = len > 0 ? write(a2l->in, buf, len) : -1; 623b3801e79SIan Rogers if (written != len) { 624d5e33ce0SNamhyung Kim if (!symbol_conf.disable_add2line_warn) 625be8ecc57STony Garnock-Jones pr_warning("%s %s: could not send request\n", __func__, dso_name); 626be8ecc57STony Garnock-Jones goto out; 627be8ecc57STony Garnock-Jones } 628b3801e79SIan Rogers io__init(&io, a2l->out, buf, sizeof(buf)); 629be8ecc57STony Garnock-Jones 6302c4b9280SIan Rogers switch (read_addr2line_record(&io, a2l_style, 6312c4b9280SIan Rogers &record_function, &record_filename, &record_line_nr)) { 632be8ecc57STony Garnock-Jones case -1: 633d5e33ce0SNamhyung Kim if (!symbol_conf.disable_add2line_warn) 634be8ecc57STony Garnock-Jones pr_warning("%s %s: could not read first record\n", __func__, dso_name); 635be8ecc57STony Garnock-Jones goto out; 636be8ecc57STony Garnock-Jones case 0: 637be8ecc57STony Garnock-Jones /* 638be8ecc57STony Garnock-Jones * The first record was invalid, so return failure, but first read another 639be8ecc57STony Garnock-Jones * record, since we asked a junk question and have to clear the answer out. 640be8ecc57STony Garnock-Jones */ 6412c4b9280SIan Rogers switch (read_addr2line_record(&io, a2l_style, NULL, NULL, NULL)) { 642be8ecc57STony Garnock-Jones case -1: 643d5e33ce0SNamhyung Kim if (!symbol_conf.disable_add2line_warn) 644d5e33ce0SNamhyung Kim pr_warning("%s %s: could not read delimiter record\n", 645d5e33ce0SNamhyung Kim __func__, dso_name); 646be8ecc57STony Garnock-Jones break; 647be8ecc57STony Garnock-Jones case 0: 648be8ecc57STony Garnock-Jones /* As expected. */ 649be8ecc57STony Garnock-Jones break; 650be8ecc57STony Garnock-Jones default: 651d5e33ce0SNamhyung Kim if (!symbol_conf.disable_add2line_warn) 652be8ecc57STony Garnock-Jones pr_warning("%s %s: unexpected record instead of sentinel", 653be8ecc57STony Garnock-Jones __func__, dso_name); 654be8ecc57STony Garnock-Jones break; 655be8ecc57STony Garnock-Jones } 656be8ecc57STony Garnock-Jones goto out; 657be8ecc57STony Garnock-Jones default: 658be8ecc57STony Garnock-Jones break; 659be8ecc57STony Garnock-Jones } 660be8ecc57STony Garnock-Jones 661be8ecc57STony Garnock-Jones if (file) { 662be8ecc57STony Garnock-Jones *file = strdup(record_filename); 663be8ecc57STony Garnock-Jones ret = 1; 664be8ecc57STony Garnock-Jones } 665be8ecc57STony Garnock-Jones if (line_nr) 666be8ecc57STony Garnock-Jones *line_nr = record_line_nr; 667be8ecc57STony Garnock-Jones 668be8ecc57STony Garnock-Jones if (unwind_inlines) { 669be8ecc57STony Garnock-Jones if (node && inline_list__append_record(dso, node, sym, 670be8ecc57STony Garnock-Jones record_function, 671be8ecc57STony Garnock-Jones record_filename, 672be8ecc57STony Garnock-Jones record_line_nr)) { 673be8ecc57STony Garnock-Jones ret = 0; 674be8ecc57STony Garnock-Jones goto out; 675be8ecc57STony Garnock-Jones } 676be8ecc57STony Garnock-Jones } 677be8ecc57STony Garnock-Jones 678be8ecc57STony Garnock-Jones /* We have to read the records even if we don't care about the inline info. */ 679b3801e79SIan Rogers while ((record_status = read_addr2line_record(&io, 6802c4b9280SIan Rogers a2l_style, 681be8ecc57STony Garnock-Jones &record_function, 682be8ecc57STony Garnock-Jones &record_filename, 683be8ecc57STony Garnock-Jones &record_line_nr)) == 1) { 684be8ecc57STony Garnock-Jones if (unwind_inlines && node && inline_count++ < MAX_INLINE_NEST) { 685be8ecc57STony Garnock-Jones if (inline_list__append_record(dso, node, sym, 686be8ecc57STony Garnock-Jones record_function, 687be8ecc57STony Garnock-Jones record_filename, 688be8ecc57STony Garnock-Jones record_line_nr)) { 689be8ecc57STony Garnock-Jones ret = 0; 690be8ecc57STony Garnock-Jones goto out; 691be8ecc57STony Garnock-Jones } 692be8ecc57STony Garnock-Jones ret = 1; /* found at least one inline frame */ 693be8ecc57STony Garnock-Jones } 694be8ecc57STony Garnock-Jones } 695be8ecc57STony Garnock-Jones 696be8ecc57STony Garnock-Jones out: 697be8ecc57STony Garnock-Jones free(record_function); 698be8ecc57STony Garnock-Jones free(record_filename); 699be8ecc57STony Garnock-Jones return ret; 700be8ecc57STony Garnock-Jones } 701be8ecc57STony Garnock-Jones 702be8ecc57STony Garnock-Jones void dso__free_a2l(struct dso *dso) 703be8ecc57STony Garnock-Jones { 704b3801e79SIan Rogers struct child_process *a2l = dso->a2l; 705be8ecc57STony Garnock-Jones 706be8ecc57STony Garnock-Jones if (!a2l) 707be8ecc57STony Garnock-Jones return; 708be8ecc57STony Garnock-Jones 709be8ecc57STony Garnock-Jones addr2line_subprocess_cleanup(a2l); 710be8ecc57STony Garnock-Jones 711be8ecc57STony Garnock-Jones dso->a2l = NULL; 712be8ecc57STony Garnock-Jones } 713be8ecc57STony Garnock-Jones 714be8ecc57STony Garnock-Jones #endif /* HAVE_LIBBFD_SUPPORT */ 715be8ecc57STony Garnock-Jones 716a64489c5SJin Yao static struct inline_node *addr2inlines(const char *dso_name, u64 addr, 717be8ecc57STony Garnock-Jones struct dso *dso, struct symbol *sym) 718a64489c5SJin Yao { 719a64489c5SJin Yao struct inline_node *node; 720a64489c5SJin Yao 721a64489c5SJin Yao node = zalloc(sizeof(*node)); 722a64489c5SJin Yao if (node == NULL) { 723a64489c5SJin Yao perror("not enough memory for the inline node"); 724be8ecc57STony Garnock-Jones return NULL; 725a64489c5SJin Yao } 726a64489c5SJin Yao 727a64489c5SJin Yao INIT_LIST_HEAD(&node->val); 728a64489c5SJin Yao node->addr = addr; 729a64489c5SJin Yao 730be8ecc57STony Garnock-Jones addr2line(dso_name, addr, NULL, NULL, dso, true, node, sym); 731a64489c5SJin Yao return node; 732a64489c5SJin Yao } 733a64489c5SJin Yao 734906049c8SAdrian Hunter /* 735906049c8SAdrian Hunter * Number of addr2line failures (without success) before disabling it for that 736906049c8SAdrian Hunter * dso. 737906049c8SAdrian Hunter */ 738906049c8SAdrian Hunter #define A2L_FAIL_LIMIT 123 739906049c8SAdrian Hunter 7402f84b42bSAndi Kleen char *__get_srcline(struct dso *dso, u64 addr, struct symbol *sym, 741935f5a9dSJin Yao bool show_sym, bool show_addr, bool unwind_inlines, 742935f5a9dSJin Yao u64 ip) 743f048d548SNamhyung Kim { 744a949fffbSDavid Ahern char *file = NULL; 745a949fffbSDavid Ahern unsigned line = 0; 7462cc9d0efSNamhyung Kim char *srcline; 747bf4414aeSArnaldo Carvalho de Melo const char *dso_name; 748f048d548SNamhyung Kim 7492cc9d0efSNamhyung Kim if (!dso->has_srcline) 75023f0981bSAndi Kleen goto out; 7512cc9d0efSNamhyung Kim 7525580338dSJin Yao dso_name = dso__name(dso); 7535580338dSJin Yao if (dso_name == NULL) 75458d91a00SNamhyung Kim goto out; 75558d91a00SNamhyung Kim 756fea0cf84SMilian Wolff if (!addr2line(dso_name, addr, &file, &line, dso, 757fea0cf84SMilian Wolff unwind_inlines, NULL, sym)) 75858d91a00SNamhyung Kim goto out; 759f048d548SNamhyung Kim 7602be8832fSMilian Wolff srcline = srcline_from_fileline(file, line); 761906049c8SAdrian Hunter free(file); 7622be8832fSMilian Wolff 7632be8832fSMilian Wolff if (!srcline) 764906049c8SAdrian Hunter goto out; 765906049c8SAdrian Hunter 766906049c8SAdrian Hunter dso->a2l_fails = 0; 767f048d548SNamhyung Kim 768f048d548SNamhyung Kim return srcline; 7692cc9d0efSNamhyung Kim 7702cc9d0efSNamhyung Kim out: 771906049c8SAdrian Hunter if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { 7722cc9d0efSNamhyung Kim dso->has_srcline = 0; 773454ff00fSAdrian Hunter dso__free_a2l(dso); 774906049c8SAdrian Hunter } 7755dfa210eSMilian Wolff 7765dfa210eSMilian Wolff if (!show_addr) 7775dfa210eSMilian Wolff return (show_sym && sym) ? 778ea335ef3SNamhyung Kim strndup(sym->name, sym->namelen) : SRCLINE_UNKNOWN; 7795dfa210eSMilian Wolff 78085c116a6SAndi Kleen if (sym) { 781ac931f87SWang Nan if (asprintf(&srcline, "%s+%" PRIu64, show_sym ? sym->name : "", 782935f5a9dSJin Yao ip - sym->start) < 0) 78385c116a6SAndi Kleen return SRCLINE_UNKNOWN; 784ac931f87SWang Nan } else if (asprintf(&srcline, "%s[%" PRIx64 "]", dso->short_name, addr) < 0) 7852cc9d0efSNamhyung Kim return SRCLINE_UNKNOWN; 78623f0981bSAndi Kleen return srcline; 787f048d548SNamhyung Kim } 788f048d548SNamhyung Kim 789dd2e18e9SAndi Kleen /* Returns filename and fills in line number in line */ 790dd2e18e9SAndi Kleen char *get_srcline_split(struct dso *dso, u64 addr, unsigned *line) 791dd2e18e9SAndi Kleen { 792dd2e18e9SAndi Kleen char *file = NULL; 793dd2e18e9SAndi Kleen const char *dso_name; 794dd2e18e9SAndi Kleen 795dd2e18e9SAndi Kleen if (!dso->has_srcline) 796dd2e18e9SAndi Kleen goto out; 797dd2e18e9SAndi Kleen 798dd2e18e9SAndi Kleen dso_name = dso__name(dso); 799dd2e18e9SAndi Kleen if (dso_name == NULL) 800dd2e18e9SAndi Kleen goto out; 801dd2e18e9SAndi Kleen 802dd2e18e9SAndi Kleen if (!addr2line(dso_name, addr, &file, line, dso, true, NULL, NULL)) 803dd2e18e9SAndi Kleen goto out; 804dd2e18e9SAndi Kleen 805dd2e18e9SAndi Kleen dso->a2l_fails = 0; 806dd2e18e9SAndi Kleen return file; 807dd2e18e9SAndi Kleen 808dd2e18e9SAndi Kleen out: 809dd2e18e9SAndi Kleen if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) { 810dd2e18e9SAndi Kleen dso->has_srcline = 0; 811dd2e18e9SAndi Kleen dso__free_a2l(dso); 812dd2e18e9SAndi Kleen } 813dd2e18e9SAndi Kleen 814dd2e18e9SAndi Kleen return NULL; 815dd2e18e9SAndi Kleen } 816dd2e18e9SAndi Kleen 817625db36eSIan Rogers void zfree_srcline(char **srcline) 818f048d548SNamhyung Kim { 819625db36eSIan Rogers if (*srcline == NULL) 820625db36eSIan Rogers return; 821625db36eSIan Rogers 822922db21dSArnaldo Carvalho de Melo if (*srcline != SRCLINE_UNKNOWN) 823625db36eSIan Rogers free(*srcline); 824625db36eSIan Rogers 825625db36eSIan Rogers *srcline = NULL; 826f048d548SNamhyung Kim } 8272f84b42bSAndi Kleen 8282f84b42bSAndi Kleen char *get_srcline(struct dso *dso, u64 addr, struct symbol *sym, 829935f5a9dSJin Yao bool show_sym, bool show_addr, u64 ip) 8302f84b42bSAndi Kleen { 831935f5a9dSJin Yao return __get_srcline(dso, addr, sym, show_sym, show_addr, false, ip); 8322f84b42bSAndi Kleen } 833a64489c5SJin Yao 83421ac9d54SMilian Wolff struct srcline_node { 83521ac9d54SMilian Wolff u64 addr; 83621ac9d54SMilian Wolff char *srcline; 83721ac9d54SMilian Wolff struct rb_node rb_node; 83821ac9d54SMilian Wolff }; 83921ac9d54SMilian Wolff 84055ecd631SDavidlohr Bueso void srcline__tree_insert(struct rb_root_cached *tree, u64 addr, char *srcline) 84121ac9d54SMilian Wolff { 84255ecd631SDavidlohr Bueso struct rb_node **p = &tree->rb_root.rb_node; 84321ac9d54SMilian Wolff struct rb_node *parent = NULL; 84421ac9d54SMilian Wolff struct srcline_node *i, *node; 84555ecd631SDavidlohr Bueso bool leftmost = true; 84621ac9d54SMilian Wolff 84721ac9d54SMilian Wolff node = zalloc(sizeof(struct srcline_node)); 84821ac9d54SMilian Wolff if (!node) { 84921ac9d54SMilian Wolff perror("not enough memory for the srcline node"); 85021ac9d54SMilian Wolff return; 85121ac9d54SMilian Wolff } 85221ac9d54SMilian Wolff 85321ac9d54SMilian Wolff node->addr = addr; 85421ac9d54SMilian Wolff node->srcline = srcline; 85521ac9d54SMilian Wolff 85621ac9d54SMilian Wolff while (*p != NULL) { 85721ac9d54SMilian Wolff parent = *p; 85821ac9d54SMilian Wolff i = rb_entry(parent, struct srcline_node, rb_node); 85921ac9d54SMilian Wolff if (addr < i->addr) 86021ac9d54SMilian Wolff p = &(*p)->rb_left; 86155ecd631SDavidlohr Bueso else { 86221ac9d54SMilian Wolff p = &(*p)->rb_right; 86355ecd631SDavidlohr Bueso leftmost = false; 86455ecd631SDavidlohr Bueso } 86521ac9d54SMilian Wolff } 86621ac9d54SMilian Wolff rb_link_node(&node->rb_node, parent, p); 86755ecd631SDavidlohr Bueso rb_insert_color_cached(&node->rb_node, tree, leftmost); 86821ac9d54SMilian Wolff } 86921ac9d54SMilian Wolff 87055ecd631SDavidlohr Bueso char *srcline__tree_find(struct rb_root_cached *tree, u64 addr) 87121ac9d54SMilian Wolff { 87255ecd631SDavidlohr Bueso struct rb_node *n = tree->rb_root.rb_node; 87321ac9d54SMilian Wolff 87421ac9d54SMilian Wolff while (n) { 87521ac9d54SMilian Wolff struct srcline_node *i = rb_entry(n, struct srcline_node, 87621ac9d54SMilian Wolff rb_node); 87721ac9d54SMilian Wolff 87821ac9d54SMilian Wolff if (addr < i->addr) 87921ac9d54SMilian Wolff n = n->rb_left; 88021ac9d54SMilian Wolff else if (addr > i->addr) 88121ac9d54SMilian Wolff n = n->rb_right; 88221ac9d54SMilian Wolff else 88321ac9d54SMilian Wolff return i->srcline; 88421ac9d54SMilian Wolff } 88521ac9d54SMilian Wolff 88621ac9d54SMilian Wolff return NULL; 88721ac9d54SMilian Wolff } 88821ac9d54SMilian Wolff 88955ecd631SDavidlohr Bueso void srcline__tree_delete(struct rb_root_cached *tree) 89021ac9d54SMilian Wolff { 89121ac9d54SMilian Wolff struct srcline_node *pos; 89255ecd631SDavidlohr Bueso struct rb_node *next = rb_first_cached(tree); 89321ac9d54SMilian Wolff 89421ac9d54SMilian Wolff while (next) { 89521ac9d54SMilian Wolff pos = rb_entry(next, struct srcline_node, rb_node); 89621ac9d54SMilian Wolff next = rb_next(&pos->rb_node); 89755ecd631SDavidlohr Bueso rb_erase_cached(&pos->rb_node, tree); 898625db36eSIan Rogers zfree_srcline(&pos->srcline); 89921ac9d54SMilian Wolff zfree(&pos); 90021ac9d54SMilian Wolff } 90121ac9d54SMilian Wolff } 90221ac9d54SMilian Wolff 903fea0cf84SMilian Wolff struct inline_node *dso__parse_addr_inlines(struct dso *dso, u64 addr, 904fea0cf84SMilian Wolff struct symbol *sym) 905a64489c5SJin Yao { 906a64489c5SJin Yao const char *dso_name; 907a64489c5SJin Yao 908a64489c5SJin Yao dso_name = dso__name(dso); 909a64489c5SJin Yao if (dso_name == NULL) 910a64489c5SJin Yao return NULL; 911a64489c5SJin Yao 912fea0cf84SMilian Wolff return addr2inlines(dso_name, addr, dso, sym); 913a64489c5SJin Yao } 914a64489c5SJin Yao 915a64489c5SJin Yao void inline_node__delete(struct inline_node *node) 916a64489c5SJin Yao { 917a64489c5SJin Yao struct inline_list *ilist, *tmp; 918a64489c5SJin Yao 919a64489c5SJin Yao list_for_each_entry_safe(ilist, tmp, &node->val, list) { 920a64489c5SJin Yao list_del_init(&ilist->list); 921625db36eSIan Rogers zfree_srcline(&ilist->srcline); 922fea0cf84SMilian Wolff /* only the inlined symbols are owned by the list */ 923fea0cf84SMilian Wolff if (ilist->symbol && ilist->symbol->inlined) 924fea0cf84SMilian Wolff symbol__delete(ilist->symbol); 925a64489c5SJin Yao free(ilist); 926a64489c5SJin Yao } 927a64489c5SJin Yao 928a64489c5SJin Yao free(node); 929a64489c5SJin Yao } 93011ea2515SMilian Wolff 93155ecd631SDavidlohr Bueso void inlines__tree_insert(struct rb_root_cached *tree, 93255ecd631SDavidlohr Bueso struct inline_node *inlines) 93311ea2515SMilian Wolff { 93455ecd631SDavidlohr Bueso struct rb_node **p = &tree->rb_root.rb_node; 93511ea2515SMilian Wolff struct rb_node *parent = NULL; 93611ea2515SMilian Wolff const u64 addr = inlines->addr; 93711ea2515SMilian Wolff struct inline_node *i; 93855ecd631SDavidlohr Bueso bool leftmost = true; 93911ea2515SMilian Wolff 94011ea2515SMilian Wolff while (*p != NULL) { 94111ea2515SMilian Wolff parent = *p; 94211ea2515SMilian Wolff i = rb_entry(parent, struct inline_node, rb_node); 94311ea2515SMilian Wolff if (addr < i->addr) 94411ea2515SMilian Wolff p = &(*p)->rb_left; 94555ecd631SDavidlohr Bueso else { 94611ea2515SMilian Wolff p = &(*p)->rb_right; 94755ecd631SDavidlohr Bueso leftmost = false; 94855ecd631SDavidlohr Bueso } 94911ea2515SMilian Wolff } 95011ea2515SMilian Wolff rb_link_node(&inlines->rb_node, parent, p); 95155ecd631SDavidlohr Bueso rb_insert_color_cached(&inlines->rb_node, tree, leftmost); 95211ea2515SMilian Wolff } 95311ea2515SMilian Wolff 95455ecd631SDavidlohr Bueso struct inline_node *inlines__tree_find(struct rb_root_cached *tree, u64 addr) 95511ea2515SMilian Wolff { 95655ecd631SDavidlohr Bueso struct rb_node *n = tree->rb_root.rb_node; 95711ea2515SMilian Wolff 95811ea2515SMilian Wolff while (n) { 95911ea2515SMilian Wolff struct inline_node *i = rb_entry(n, struct inline_node, 96011ea2515SMilian Wolff rb_node); 96111ea2515SMilian Wolff 96211ea2515SMilian Wolff if (addr < i->addr) 96311ea2515SMilian Wolff n = n->rb_left; 96411ea2515SMilian Wolff else if (addr > i->addr) 96511ea2515SMilian Wolff n = n->rb_right; 96611ea2515SMilian Wolff else 96711ea2515SMilian Wolff return i; 96811ea2515SMilian Wolff } 96911ea2515SMilian Wolff 97011ea2515SMilian Wolff return NULL; 97111ea2515SMilian Wolff } 97211ea2515SMilian Wolff 97355ecd631SDavidlohr Bueso void inlines__tree_delete(struct rb_root_cached *tree) 97411ea2515SMilian Wolff { 97511ea2515SMilian Wolff struct inline_node *pos; 97655ecd631SDavidlohr Bueso struct rb_node *next = rb_first_cached(tree); 97711ea2515SMilian Wolff 97811ea2515SMilian Wolff while (next) { 97911ea2515SMilian Wolff pos = rb_entry(next, struct inline_node, rb_node); 98011ea2515SMilian Wolff next = rb_next(&pos->rb_node); 98155ecd631SDavidlohr Bueso rb_erase_cached(&pos->rb_node, tree); 98211ea2515SMilian Wolff inline_node__delete(pos); 98311ea2515SMilian Wolff } 98411ea2515SMilian Wolff } 985