1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 25aab621bSArnaldo Carvalho de Melo #include <dirent.h> 35aab621bSArnaldo Carvalho de Melo #include <errno.h> 45aab621bSArnaldo Carvalho de Melo #include <stdlib.h> 55aab621bSArnaldo Carvalho de Melo #include <stdio.h> 65aab621bSArnaldo Carvalho de Melo #include <string.h> 78859aedeSIgor Lubashev #include <linux/capability.h> 8877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h> 9e9814df8SArnaldo Carvalho de Melo #include <linux/mman.h> 108520a98dSArnaldo Carvalho de Melo #include <linux/string.h> 112a1292cbSAndi Kleen #include <linux/time64.h> 125aab621bSArnaldo Carvalho de Melo #include <sys/types.h> 135aab621bSArnaldo Carvalho de Melo #include <sys/stat.h> 145aab621bSArnaldo Carvalho de Melo #include <sys/param.h> 155aab621bSArnaldo Carvalho de Melo #include <fcntl.h> 165aab621bSArnaldo Carvalho de Melo #include <unistd.h> 179486aa38SArnaldo Carvalho de Melo #include <inttypes.h> 18b01141f4SArnaldo Carvalho de Melo #include "annotate.h" 19b36f19d5SArnaldo Carvalho de Melo #include "build-id.h" 208859aedeSIgor Lubashev #include "cap.h" 21fac583fdSArnaldo Carvalho de Melo #include "dso.h" 22fb71c86cSArnaldo Carvalho de Melo #include "util.h" // lsdir() 238a6c5b26SArnaldo Carvalho de Melo #include "debug.h" 248859aedeSIgor Lubashev #include "event.h" 2569d2591aSArnaldo Carvalho de Melo #include "machine.h" 261101f69aSArnaldo Carvalho de Melo #include "map.h" 2786470930SIngo Molnar #include "symbol.h" 28d3300a3cSArnaldo Carvalho de Melo #include "map_symbol.h" 29d3300a3cSArnaldo Carvalho de Melo #include "mem-events.h" 30b1d1b094SArnaldo Carvalho de Melo #include "symsrc.h" 315aab621bSArnaldo Carvalho de Melo #include "strlist.h" 32e03eaa40SDavid Ahern #include "intlist.h" 33843ff37bSKrister Johansen #include "namespaces.h" 340a7e6d1bSNamhyung Kim #include "header.h" 359a3993d4SArnaldo Carvalho de Melo #include "path.h" 363052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h> 377f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 3886470930SIngo Molnar 3986470930SIngo Molnar #include <elf.h> 40f1617b40SArnaldo Carvalho de Melo #include <limits.h> 41c506c96bSArnaldo Carvalho de Melo #include <symbol/kallsyms.h> 42439d473bSArnaldo Carvalho de Melo #include <sys/utsname.h> 432cdbc46dSPeter Zijlstra 44be39db9fSArnaldo Carvalho de Melo static int dso__load_kernel_sym(struct dso *dso, struct map *map); 45be39db9fSArnaldo Carvalho de Melo static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map); 46608c34deSArnaldo Carvalho de Melo static bool symbol__is_idle(const char *name); 47608c34deSArnaldo Carvalho de Melo 483f067dcaSArnaldo Carvalho de Melo int vmlinux_path__nr_entries; 493f067dcaSArnaldo Carvalho de Melo char **vmlinux_path; 50439d473bSArnaldo Carvalho de Melo 5175be6cf4SArnaldo Carvalho de Melo struct symbol_conf symbol_conf = { 5252bab886SAndi Kleen .nanosecs = false, 53b32d133aSArnaldo Carvalho de Melo .use_modules = true, 54b32d133aSArnaldo Carvalho de Melo .try_vmlinux_path = true, 55328ccdacSNamhyung Kim .demangle = true, 56763122adSAvi Kivity .demangle_kernel = false, 57e511db5eSNamhyung Kim .cumulate_callchain = true, 582a1292cbSAndi Kleen .time_quantum = 100 * NSEC_PER_MSEC, /* 100ms */ 59c8302367SJiri Olsa .show_hist_headers = true, 60ec5761eaSDavid Ahern .symfs = "", 611e9abf8bSNamhyung Kim .event_group = true, 62d8a88dd2SMilian Wolff .inline_name = true, 634968ac8fSAndi Kleen .res_sample = 0, 64b32d133aSArnaldo Carvalho de Melo }; 65b32d133aSArnaldo Carvalho de Melo 6644f24cb3SJiri Olsa static enum dso_binary_type binary_type_symtab[] = { 6744f24cb3SJiri Olsa DSO_BINARY_TYPE__KALLSYMS, 6844f24cb3SJiri Olsa DSO_BINARY_TYPE__GUEST_KALLSYMS, 6944f24cb3SJiri Olsa DSO_BINARY_TYPE__JAVA_JIT, 7044f24cb3SJiri Olsa DSO_BINARY_TYPE__DEBUGLINK, 7144f24cb3SJiri Olsa DSO_BINARY_TYPE__BUILD_ID_CACHE, 72d2396999SKrister Johansen DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO, 7344f24cb3SJiri Olsa DSO_BINARY_TYPE__FEDORA_DEBUGINFO, 7444f24cb3SJiri Olsa DSO_BINARY_TYPE__UBUNTU_DEBUGINFO, 7544f24cb3SJiri Olsa DSO_BINARY_TYPE__BUILDID_DEBUGINFO, 7644f24cb3SJiri Olsa DSO_BINARY_TYPE__SYSTEM_PATH_DSO, 7744f24cb3SJiri Olsa DSO_BINARY_TYPE__GUEST_KMODULE, 78c00c48fcSNamhyung Kim DSO_BINARY_TYPE__GUEST_KMODULE_COMP, 7944f24cb3SJiri Olsa DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE, 80c00c48fcSNamhyung Kim DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP, 819cd00941SRicardo Ribalda Delgado DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO, 8285afd355SAdrian Hunter DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO, 8344f24cb3SJiri Olsa DSO_BINARY_TYPE__NOT_FOUND, 8444f24cb3SJiri Olsa }; 8544f24cb3SJiri Olsa 86028df767SJiri Olsa #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab) 8744f24cb3SJiri Olsa 883183f8caSArnaldo Carvalho de Melo static bool symbol_type__filter(char symbol_type) 896893d4eeSArnaldo Carvalho de Melo { 9031877908SAnton Blanchard symbol_type = toupper(symbol_type); 912be732c0SArnaldo Carvalho de Melo return symbol_type == 'T' || symbol_type == 'W' || symbol_type == 'D' || symbol_type == 'B'; 926893d4eeSArnaldo Carvalho de Melo } 936893d4eeSArnaldo Carvalho de Melo 94694bf407SAnton Blanchard static int prefix_underscores_count(const char *str) 95694bf407SAnton Blanchard { 96694bf407SAnton Blanchard const char *tail = str; 97694bf407SAnton Blanchard 98694bf407SAnton Blanchard while (*tail == '_') 99694bf407SAnton Blanchard tail++; 100694bf407SAnton Blanchard 101694bf407SAnton Blanchard return tail - str; 102694bf407SAnton Blanchard } 103694bf407SAnton Blanchard 104b9c0a649SThomas Richter void __weak arch__symbols__fixup_end(struct symbol *p, struct symbol *c) 105b9c0a649SThomas Richter { 106b9c0a649SThomas Richter p->end = c->start; 107b9c0a649SThomas Richter } 108b9c0a649SThomas Richter 1094b3a2716SMasami Hiramatsu const char * __weak arch__normalize_symbol_name(const char *name) 1104b3a2716SMasami Hiramatsu { 1114b3a2716SMasami Hiramatsu return name; 1124b3a2716SMasami Hiramatsu } 1134b3a2716SMasami Hiramatsu 114d8040645SPaul Clarke int __weak arch__compare_symbol_names(const char *namea, const char *nameb) 115d8040645SPaul Clarke { 116d8040645SPaul Clarke return strcmp(namea, nameb); 117d8040645SPaul Clarke } 118d8040645SPaul Clarke 119d8040645SPaul Clarke int __weak arch__compare_symbol_names_n(const char *namea, const char *nameb, 120d8040645SPaul Clarke unsigned int n) 121d8040645SPaul Clarke { 122d8040645SPaul Clarke return strncmp(namea, nameb, n); 123d8040645SPaul Clarke } 124d8040645SPaul Clarke 125fb6d5942SNaveen N. Rao int __weak arch__choose_best_symbol(struct symbol *syma, 126fb6d5942SNaveen N. Rao struct symbol *symb __maybe_unused) 127fb6d5942SNaveen N. Rao { 128fb6d5942SNaveen N. Rao /* Avoid "SyS" kernel syscall aliases */ 129fb6d5942SNaveen N. Rao if (strlen(syma->name) >= 3 && !strncmp(syma->name, "SyS", 3)) 130fb6d5942SNaveen N. Rao return SYMBOL_B; 131fb6d5942SNaveen N. Rao if (strlen(syma->name) >= 10 && !strncmp(syma->name, "compat_SyS", 10)) 132fb6d5942SNaveen N. Rao return SYMBOL_B; 133fb6d5942SNaveen N. Rao 134fb6d5942SNaveen N. Rao return SYMBOL_A; 135fb6d5942SNaveen N. Rao } 136694bf407SAnton Blanchard 137694bf407SAnton Blanchard static int choose_best_symbol(struct symbol *syma, struct symbol *symb) 138694bf407SAnton Blanchard { 139694bf407SAnton Blanchard s64 a; 140694bf407SAnton Blanchard s64 b; 1413445432bSAdrian Hunter size_t na, nb; 142694bf407SAnton Blanchard 143694bf407SAnton Blanchard /* Prefer a symbol with non zero length */ 144694bf407SAnton Blanchard a = syma->end - syma->start; 145694bf407SAnton Blanchard b = symb->end - symb->start; 146694bf407SAnton Blanchard if ((b == 0) && (a > 0)) 147694bf407SAnton Blanchard return SYMBOL_A; 148694bf407SAnton Blanchard else if ((a == 0) && (b > 0)) 149694bf407SAnton Blanchard return SYMBOL_B; 150694bf407SAnton Blanchard 151694bf407SAnton Blanchard /* Prefer a non weak symbol over a weak one */ 152694bf407SAnton Blanchard a = syma->binding == STB_WEAK; 153694bf407SAnton Blanchard b = symb->binding == STB_WEAK; 154694bf407SAnton Blanchard if (b && !a) 155694bf407SAnton Blanchard return SYMBOL_A; 156694bf407SAnton Blanchard if (a && !b) 157694bf407SAnton Blanchard return SYMBOL_B; 158694bf407SAnton Blanchard 159694bf407SAnton Blanchard /* Prefer a global symbol over a non global one */ 160694bf407SAnton Blanchard a = syma->binding == STB_GLOBAL; 161694bf407SAnton Blanchard b = symb->binding == STB_GLOBAL; 162694bf407SAnton Blanchard if (a && !b) 163694bf407SAnton Blanchard return SYMBOL_A; 164694bf407SAnton Blanchard if (b && !a) 165694bf407SAnton Blanchard return SYMBOL_B; 166694bf407SAnton Blanchard 167694bf407SAnton Blanchard /* Prefer a symbol with less underscores */ 168694bf407SAnton Blanchard a = prefix_underscores_count(syma->name); 169694bf407SAnton Blanchard b = prefix_underscores_count(symb->name); 170694bf407SAnton Blanchard if (b > a) 171694bf407SAnton Blanchard return SYMBOL_A; 172694bf407SAnton Blanchard else if (a > b) 173694bf407SAnton Blanchard return SYMBOL_B; 174694bf407SAnton Blanchard 1753445432bSAdrian Hunter /* Choose the symbol with the longest name */ 1763445432bSAdrian Hunter na = strlen(syma->name); 1773445432bSAdrian Hunter nb = strlen(symb->name); 1783445432bSAdrian Hunter if (na > nb) 179694bf407SAnton Blanchard return SYMBOL_A; 1803445432bSAdrian Hunter else if (na < nb) 181694bf407SAnton Blanchard return SYMBOL_B; 1823445432bSAdrian Hunter 183fb6d5942SNaveen N. Rao return arch__choose_best_symbol(syma, symb); 184694bf407SAnton Blanchard } 185694bf407SAnton Blanchard 1867137ff50SDavidlohr Bueso void symbols__fixup_duplicate(struct rb_root_cached *symbols) 187694bf407SAnton Blanchard { 188694bf407SAnton Blanchard struct rb_node *nd; 189694bf407SAnton Blanchard struct symbol *curr, *next; 190694bf407SAnton Blanchard 191c97b40e4SArnaldo Carvalho de Melo if (symbol_conf.allow_aliases) 192c97b40e4SArnaldo Carvalho de Melo return; 193c97b40e4SArnaldo Carvalho de Melo 1947137ff50SDavidlohr Bueso nd = rb_first_cached(symbols); 195694bf407SAnton Blanchard 196694bf407SAnton Blanchard while (nd) { 197694bf407SAnton Blanchard curr = rb_entry(nd, struct symbol, rb_node); 198694bf407SAnton Blanchard again: 199694bf407SAnton Blanchard nd = rb_next(&curr->rb_node); 200694bf407SAnton Blanchard next = rb_entry(nd, struct symbol, rb_node); 201694bf407SAnton Blanchard 202694bf407SAnton Blanchard if (!nd) 203694bf407SAnton Blanchard break; 204694bf407SAnton Blanchard 205694bf407SAnton Blanchard if (curr->start != next->start) 206694bf407SAnton Blanchard continue; 207694bf407SAnton Blanchard 208694bf407SAnton Blanchard if (choose_best_symbol(curr, next) == SYMBOL_A) { 2097137ff50SDavidlohr Bueso rb_erase_cached(&next->rb_node, symbols); 210d4f74eb8SChenggang Qin symbol__delete(next); 211694bf407SAnton Blanchard goto again; 212694bf407SAnton Blanchard } else { 213694bf407SAnton Blanchard nd = rb_next(&curr->rb_node); 2147137ff50SDavidlohr Bueso rb_erase_cached(&curr->rb_node, symbols); 215d4f74eb8SChenggang Qin symbol__delete(curr); 216694bf407SAnton Blanchard } 217694bf407SAnton Blanchard } 218694bf407SAnton Blanchard } 219694bf407SAnton Blanchard 2207137ff50SDavidlohr Bueso void symbols__fixup_end(struct rb_root_cached *symbols) 221af427bf5SArnaldo Carvalho de Melo { 2227137ff50SDavidlohr Bueso struct rb_node *nd, *prevnd = rb_first_cached(symbols); 2232e538c4aSArnaldo Carvalho de Melo struct symbol *curr, *prev; 224af427bf5SArnaldo Carvalho de Melo 225af427bf5SArnaldo Carvalho de Melo if (prevnd == NULL) 226af427bf5SArnaldo Carvalho de Melo return; 227af427bf5SArnaldo Carvalho de Melo 2282e538c4aSArnaldo Carvalho de Melo curr = rb_entry(prevnd, struct symbol, rb_node); 2292e538c4aSArnaldo Carvalho de Melo 230af427bf5SArnaldo Carvalho de Melo for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { 2312e538c4aSArnaldo Carvalho de Melo prev = curr; 2322e538c4aSArnaldo Carvalho de Melo curr = rb_entry(nd, struct symbol, rb_node); 233af427bf5SArnaldo Carvalho de Melo 2343b01a413SArnaldo Carvalho de Melo if (prev->end == prev->start && prev->end != curr->start) 235b9c0a649SThomas Richter arch__symbols__fixup_end(prev, curr); 236af427bf5SArnaldo Carvalho de Melo } 237af427bf5SArnaldo Carvalho de Melo 2382e538c4aSArnaldo Carvalho de Melo /* Last entry */ 2392e538c4aSArnaldo Carvalho de Melo if (curr->end == curr->start) 240e7ede72aSDaniel Borkmann curr->end = roundup(curr->start, 4096) + 4096; 2412e538c4aSArnaldo Carvalho de Melo } 2422e538c4aSArnaldo Carvalho de Melo 2439a29ceeeSArnaldo Carvalho de Melo void maps__fixup_end(struct maps *maps) 244af427bf5SArnaldo Carvalho de Melo { 2458efc4f05SArnaldo Carvalho de Melo struct map *prev = NULL, *curr; 246af427bf5SArnaldo Carvalho de Melo 2470a7c74eaSArnaldo Carvalho de Melo down_write(&maps->lock); 2486a2ffcddSArnaldo Carvalho de Melo 2498efc4f05SArnaldo Carvalho de Melo maps__for_each_entry(maps, curr) { 2508efc4f05SArnaldo Carvalho de Melo if (prev != NULL && !prev->end) 2518efc4f05SArnaldo Carvalho de Melo prev->end = curr->start; 252af427bf5SArnaldo Carvalho de Melo 2538efc4f05SArnaldo Carvalho de Melo prev = curr; 2542e538c4aSArnaldo Carvalho de Melo } 25590c83218SArnaldo Carvalho de Melo 25690c83218SArnaldo Carvalho de Melo /* 25790c83218SArnaldo Carvalho de Melo * We still haven't the actual symbols, so guess the 25890c83218SArnaldo Carvalho de Melo * last map final address. 25990c83218SArnaldo Carvalho de Melo */ 2608efc4f05SArnaldo Carvalho de Melo if (curr && !curr->end) 2619d1faba5SIan Munsie curr->end = ~0ULL; 2626a2ffcddSArnaldo Carvalho de Melo 2630a7c74eaSArnaldo Carvalho de Melo up_write(&maps->lock); 264af427bf5SArnaldo Carvalho de Melo } 265af427bf5SArnaldo Carvalho de Melo 266af30bffaSArnaldo Carvalho de Melo struct symbol *symbol__new(u64 start, u64 len, u8 binding, u8 type, const char *name) 26786470930SIngo Molnar { 26886470930SIngo Molnar size_t namelen = strlen(name) + 1; 269aeafcbafSArnaldo Carvalho de Melo struct symbol *sym = calloc(1, (symbol_conf.priv_size + 270aeafcbafSArnaldo Carvalho de Melo sizeof(*sym) + namelen)); 271aeafcbafSArnaldo Carvalho de Melo if (sym == NULL) 27286470930SIngo Molnar return NULL; 27386470930SIngo Molnar 274b01141f4SArnaldo Carvalho de Melo if (symbol_conf.priv_size) { 275b01141f4SArnaldo Carvalho de Melo if (symbol_conf.init_annotation) { 276b01141f4SArnaldo Carvalho de Melo struct annotation *notes = (void *)sym; 277b01141f4SArnaldo Carvalho de Melo pthread_mutex_init(¬es->lock, NULL); 278b01141f4SArnaldo Carvalho de Melo } 279aeafcbafSArnaldo Carvalho de Melo sym = ((void *)sym) + symbol_conf.priv_size; 280b01141f4SArnaldo Carvalho de Melo } 28136479484SArnaldo Carvalho de Melo 282aeafcbafSArnaldo Carvalho de Melo sym->start = start; 2832c241bd3SArnaldo Carvalho de Melo sym->end = len ? start + len : start; 284af30bffaSArnaldo Carvalho de Melo sym->type = type; 285aeafcbafSArnaldo Carvalho de Melo sym->binding = binding; 286aeafcbafSArnaldo Carvalho de Melo sym->namelen = namelen - 1; 287e4204992SArnaldo Carvalho de Melo 288aeafcbafSArnaldo Carvalho de Melo pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n", 289aeafcbafSArnaldo Carvalho de Melo __func__, name, start, sym->end); 290aeafcbafSArnaldo Carvalho de Melo memcpy(sym->name, name, namelen); 291e4204992SArnaldo Carvalho de Melo 292aeafcbafSArnaldo Carvalho de Melo return sym; 29386470930SIngo Molnar } 29486470930SIngo Molnar 295aeafcbafSArnaldo Carvalho de Melo void symbol__delete(struct symbol *sym) 29686470930SIngo Molnar { 297aeafcbafSArnaldo Carvalho de Melo free(((void *)sym) - symbol_conf.priv_size); 29886470930SIngo Molnar } 29986470930SIngo Molnar 3007137ff50SDavidlohr Bueso void symbols__delete(struct rb_root_cached *symbols) 30186470930SIngo Molnar { 30286470930SIngo Molnar struct symbol *pos; 3037137ff50SDavidlohr Bueso struct rb_node *next = rb_first_cached(symbols); 30486470930SIngo Molnar 30586470930SIngo Molnar while (next) { 30686470930SIngo Molnar pos = rb_entry(next, struct symbol, rb_node); 30786470930SIngo Molnar next = rb_next(&pos->rb_node); 3087137ff50SDavidlohr Bueso rb_erase_cached(&pos->rb_node, symbols); 30900a192b3SArnaldo Carvalho de Melo symbol__delete(pos); 31086470930SIngo Molnar } 31186470930SIngo Molnar } 31286470930SIngo Molnar 3137137ff50SDavidlohr Bueso void __symbols__insert(struct rb_root_cached *symbols, 3147137ff50SDavidlohr Bueso struct symbol *sym, bool kernel) 31586470930SIngo Molnar { 3167137ff50SDavidlohr Bueso struct rb_node **p = &symbols->rb_root.rb_node; 31786470930SIngo Molnar struct rb_node *parent = NULL; 3189cffa8d5SPaul Mackerras const u64 ip = sym->start; 31986470930SIngo Molnar struct symbol *s; 3207137ff50SDavidlohr Bueso bool leftmost = true; 32186470930SIngo Molnar 322608c34deSArnaldo Carvalho de Melo if (kernel) { 323608c34deSArnaldo Carvalho de Melo const char *name = sym->name; 324608c34deSArnaldo Carvalho de Melo /* 325608c34deSArnaldo Carvalho de Melo * ppc64 uses function descriptors and appends a '.' to the 326608c34deSArnaldo Carvalho de Melo * start of every instruction address. Remove it. 327608c34deSArnaldo Carvalho de Melo */ 328608c34deSArnaldo Carvalho de Melo if (name[0] == '.') 329608c34deSArnaldo Carvalho de Melo name++; 330608c34deSArnaldo Carvalho de Melo sym->idle = symbol__is_idle(name); 331608c34deSArnaldo Carvalho de Melo } 332608c34deSArnaldo Carvalho de Melo 33386470930SIngo Molnar while (*p != NULL) { 33486470930SIngo Molnar parent = *p; 33586470930SIngo Molnar s = rb_entry(parent, struct symbol, rb_node); 33686470930SIngo Molnar if (ip < s->start) 33786470930SIngo Molnar p = &(*p)->rb_left; 3387137ff50SDavidlohr Bueso else { 33986470930SIngo Molnar p = &(*p)->rb_right; 3407137ff50SDavidlohr Bueso leftmost = false; 3417137ff50SDavidlohr Bueso } 34286470930SIngo Molnar } 34386470930SIngo Molnar rb_link_node(&sym->rb_node, parent, p); 3447137ff50SDavidlohr Bueso rb_insert_color_cached(&sym->rb_node, symbols, leftmost); 34586470930SIngo Molnar } 34686470930SIngo Molnar 3477137ff50SDavidlohr Bueso void symbols__insert(struct rb_root_cached *symbols, struct symbol *sym) 348608c34deSArnaldo Carvalho de Melo { 349608c34deSArnaldo Carvalho de Melo __symbols__insert(symbols, sym, false); 350608c34deSArnaldo Carvalho de Melo } 351608c34deSArnaldo Carvalho de Melo 3527137ff50SDavidlohr Bueso static struct symbol *symbols__find(struct rb_root_cached *symbols, u64 ip) 35386470930SIngo Molnar { 35486470930SIngo Molnar struct rb_node *n; 35586470930SIngo Molnar 356aeafcbafSArnaldo Carvalho de Melo if (symbols == NULL) 35786470930SIngo Molnar return NULL; 35886470930SIngo Molnar 3597137ff50SDavidlohr Bueso n = symbols->rb_root.rb_node; 36086470930SIngo Molnar 36186470930SIngo Molnar while (n) { 36286470930SIngo Molnar struct symbol *s = rb_entry(n, struct symbol, rb_node); 36386470930SIngo Molnar 36486470930SIngo Molnar if (ip < s->start) 36586470930SIngo Molnar n = n->rb_left; 3669c7b37cdSChris Phlipot else if (ip > s->end || (ip == s->end && ip != s->start)) 36786470930SIngo Molnar n = n->rb_right; 36886470930SIngo Molnar else 36986470930SIngo Molnar return s; 37086470930SIngo Molnar } 37186470930SIngo Molnar 37286470930SIngo Molnar return NULL; 37386470930SIngo Molnar } 37486470930SIngo Molnar 3757137ff50SDavidlohr Bueso static struct symbol *symbols__first(struct rb_root_cached *symbols) 3768e0cf965SAdrian Hunter { 3777137ff50SDavidlohr Bueso struct rb_node *n = rb_first_cached(symbols); 3788e0cf965SAdrian Hunter 3798e0cf965SAdrian Hunter if (n) 3808e0cf965SAdrian Hunter return rb_entry(n, struct symbol, rb_node); 3818e0cf965SAdrian Hunter 3828e0cf965SAdrian Hunter return NULL; 3838e0cf965SAdrian Hunter } 3848e0cf965SAdrian Hunter 3857137ff50SDavidlohr Bueso static struct symbol *symbols__last(struct rb_root_cached *symbols) 386cd67f99fSAdrian Hunter { 3877137ff50SDavidlohr Bueso struct rb_node *n = rb_last(&symbols->rb_root); 388cd67f99fSAdrian Hunter 389cd67f99fSAdrian Hunter if (n) 390cd67f99fSAdrian Hunter return rb_entry(n, struct symbol, rb_node); 391cd67f99fSAdrian Hunter 392cd67f99fSAdrian Hunter return NULL; 393cd67f99fSAdrian Hunter } 394cd67f99fSAdrian Hunter 3959c00a81bSAdrian Hunter static struct symbol *symbols__next(struct symbol *sym) 3969c00a81bSAdrian Hunter { 3979c00a81bSAdrian Hunter struct rb_node *n = rb_next(&sym->rb_node); 3989c00a81bSAdrian Hunter 3999c00a81bSAdrian Hunter if (n) 4009c00a81bSAdrian Hunter return rb_entry(n, struct symbol, rb_node); 4019c00a81bSAdrian Hunter 4029c00a81bSAdrian Hunter return NULL; 4039c00a81bSAdrian Hunter } 4049c00a81bSAdrian Hunter 4057137ff50SDavidlohr Bueso static void symbols__insert_by_name(struct rb_root_cached *symbols, struct symbol *sym) 40679406cd7SArnaldo Carvalho de Melo { 4077137ff50SDavidlohr Bueso struct rb_node **p = &symbols->rb_root.rb_node; 40879406cd7SArnaldo Carvalho de Melo struct rb_node *parent = NULL; 40902a9d037SRabin Vincent struct symbol_name_rb_node *symn, *s; 4107137ff50SDavidlohr Bueso bool leftmost = true; 41102a9d037SRabin Vincent 41202a9d037SRabin Vincent symn = container_of(sym, struct symbol_name_rb_node, sym); 41379406cd7SArnaldo Carvalho de Melo 41479406cd7SArnaldo Carvalho de Melo while (*p != NULL) { 41579406cd7SArnaldo Carvalho de Melo parent = *p; 41679406cd7SArnaldo Carvalho de Melo s = rb_entry(parent, struct symbol_name_rb_node, rb_node); 41779406cd7SArnaldo Carvalho de Melo if (strcmp(sym->name, s->sym.name) < 0) 41879406cd7SArnaldo Carvalho de Melo p = &(*p)->rb_left; 4197137ff50SDavidlohr Bueso else { 42079406cd7SArnaldo Carvalho de Melo p = &(*p)->rb_right; 4217137ff50SDavidlohr Bueso leftmost = false; 4227137ff50SDavidlohr Bueso } 42379406cd7SArnaldo Carvalho de Melo } 42479406cd7SArnaldo Carvalho de Melo rb_link_node(&symn->rb_node, parent, p); 4257137ff50SDavidlohr Bueso rb_insert_color_cached(&symn->rb_node, symbols, leftmost); 42679406cd7SArnaldo Carvalho de Melo } 42779406cd7SArnaldo Carvalho de Melo 4287137ff50SDavidlohr Bueso static void symbols__sort_by_name(struct rb_root_cached *symbols, 4297137ff50SDavidlohr Bueso struct rb_root_cached *source) 43079406cd7SArnaldo Carvalho de Melo { 43179406cd7SArnaldo Carvalho de Melo struct rb_node *nd; 43279406cd7SArnaldo Carvalho de Melo 4337137ff50SDavidlohr Bueso for (nd = rb_first_cached(source); nd; nd = rb_next(nd)) { 43479406cd7SArnaldo Carvalho de Melo struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 435aeafcbafSArnaldo Carvalho de Melo symbols__insert_by_name(symbols, pos); 43679406cd7SArnaldo Carvalho de Melo } 43779406cd7SArnaldo Carvalho de Melo } 43879406cd7SArnaldo Carvalho de Melo 439d8040645SPaul Clarke int symbol__match_symbol_name(const char *name, const char *str, 440d8040645SPaul Clarke enum symbol_tag_include includes) 441d8040645SPaul Clarke { 442d8040645SPaul Clarke const char *versioning; 443d8040645SPaul Clarke 444d8040645SPaul Clarke if (includes == SYMBOL_TAG_INCLUDE__DEFAULT_ONLY && 445d8040645SPaul Clarke (versioning = strstr(name, "@@"))) { 446d8040645SPaul Clarke int len = strlen(str); 447d8040645SPaul Clarke 448d8040645SPaul Clarke if (len < versioning - name) 449d8040645SPaul Clarke len = versioning - name; 450d8040645SPaul Clarke 451d8040645SPaul Clarke return arch__compare_symbol_names_n(name, str, len); 452d8040645SPaul Clarke } else 453d8040645SPaul Clarke return arch__compare_symbol_names(name, str); 454d8040645SPaul Clarke } 455d8040645SPaul Clarke 4567137ff50SDavidlohr Bueso static struct symbol *symbols__find_by_name(struct rb_root_cached *symbols, 457d8040645SPaul Clarke const char *name, 458d8040645SPaul Clarke enum symbol_tag_include includes) 45979406cd7SArnaldo Carvalho de Melo { 46079406cd7SArnaldo Carvalho de Melo struct rb_node *n; 4615bcaaca3SMartin Liška struct symbol_name_rb_node *s = NULL; 46279406cd7SArnaldo Carvalho de Melo 463aeafcbafSArnaldo Carvalho de Melo if (symbols == NULL) 46479406cd7SArnaldo Carvalho de Melo return NULL; 46579406cd7SArnaldo Carvalho de Melo 4667137ff50SDavidlohr Bueso n = symbols->rb_root.rb_node; 46779406cd7SArnaldo Carvalho de Melo 46879406cd7SArnaldo Carvalho de Melo while (n) { 46979406cd7SArnaldo Carvalho de Melo int cmp; 47079406cd7SArnaldo Carvalho de Melo 47179406cd7SArnaldo Carvalho de Melo s = rb_entry(n, struct symbol_name_rb_node, rb_node); 472d8040645SPaul Clarke cmp = symbol__match_symbol_name(s->sym.name, name, includes); 47379406cd7SArnaldo Carvalho de Melo 474d8040645SPaul Clarke if (cmp > 0) 47579406cd7SArnaldo Carvalho de Melo n = n->rb_left; 476d8040645SPaul Clarke else if (cmp < 0) 47779406cd7SArnaldo Carvalho de Melo n = n->rb_right; 47879406cd7SArnaldo Carvalho de Melo else 479de480999SNamhyung Kim break; 48079406cd7SArnaldo Carvalho de Melo } 48179406cd7SArnaldo Carvalho de Melo 482de480999SNamhyung Kim if (n == NULL) 48379406cd7SArnaldo Carvalho de Melo return NULL; 484de480999SNamhyung Kim 485d8040645SPaul Clarke if (includes != SYMBOL_TAG_INCLUDE__DEFAULT_ONLY) 486de480999SNamhyung Kim /* return first symbol that has same name (if any) */ 487de480999SNamhyung Kim for (n = rb_prev(n); n; n = rb_prev(n)) { 488de480999SNamhyung Kim struct symbol_name_rb_node *tmp; 489de480999SNamhyung Kim 490de480999SNamhyung Kim tmp = rb_entry(n, struct symbol_name_rb_node, rb_node); 491031b84c4SNaveen N. Rao if (arch__compare_symbol_names(tmp->sym.name, s->sym.name)) 492de480999SNamhyung Kim break; 493de480999SNamhyung Kim 494de480999SNamhyung Kim s = tmp; 495de480999SNamhyung Kim } 496de480999SNamhyung Kim 497de480999SNamhyung Kim return &s->sym; 49879406cd7SArnaldo Carvalho de Melo } 49979406cd7SArnaldo Carvalho de Melo 500c0b4dffbSArnaldo Carvalho de Melo void dso__reset_find_symbol_cache(struct dso *dso) 501c0b4dffbSArnaldo Carvalho de Melo { 5023183f8caSArnaldo Carvalho de Melo dso->last_find_result.addr = 0; 5033183f8caSArnaldo Carvalho de Melo dso->last_find_result.symbol = NULL; 504c0b4dffbSArnaldo Carvalho de Melo } 505c0b4dffbSArnaldo Carvalho de Melo 5063183f8caSArnaldo Carvalho de Melo void dso__insert_symbol(struct dso *dso, struct symbol *sym) 507ae93a6c7SChris Phlipot { 5083183f8caSArnaldo Carvalho de Melo __symbols__insert(&dso->symbols, sym, dso->kernel); 509ae93a6c7SChris Phlipot 510ae93a6c7SChris Phlipot /* update the symbol cache if necessary */ 5113183f8caSArnaldo Carvalho de Melo if (dso->last_find_result.addr >= sym->start && 5123183f8caSArnaldo Carvalho de Melo (dso->last_find_result.addr < sym->end || 513ae93a6c7SChris Phlipot sym->start == sym->end)) { 5143183f8caSArnaldo Carvalho de Melo dso->last_find_result.symbol = sym; 515ae93a6c7SChris Phlipot } 516ae93a6c7SChris Phlipot } 517ae93a6c7SChris Phlipot 5183183f8caSArnaldo Carvalho de Melo struct symbol *dso__find_symbol(struct dso *dso, u64 addr) 519fcf1203aSArnaldo Carvalho de Melo { 5203183f8caSArnaldo Carvalho de Melo if (dso->last_find_result.addr != addr || dso->last_find_result.symbol == NULL) { 5213183f8caSArnaldo Carvalho de Melo dso->last_find_result.addr = addr; 5223183f8caSArnaldo Carvalho de Melo dso->last_find_result.symbol = symbols__find(&dso->symbols, addr); 523b685ac22SArnaldo Carvalho de Melo } 524b685ac22SArnaldo Carvalho de Melo 5253183f8caSArnaldo Carvalho de Melo return dso->last_find_result.symbol; 5268e0cf965SAdrian Hunter } 5278e0cf965SAdrian Hunter 5285cf88a63SArnaldo Carvalho de Melo struct symbol *dso__first_symbol(struct dso *dso) 5295cf88a63SArnaldo Carvalho de Melo { 5303183f8caSArnaldo Carvalho de Melo return symbols__first(&dso->symbols); 531cd67f99fSAdrian Hunter } 532cd67f99fSAdrian Hunter 5335cf88a63SArnaldo Carvalho de Melo struct symbol *dso__last_symbol(struct dso *dso) 5345cf88a63SArnaldo Carvalho de Melo { 5353183f8caSArnaldo Carvalho de Melo return symbols__last(&dso->symbols); 5365cf88a63SArnaldo Carvalho de Melo } 5375cf88a63SArnaldo Carvalho de Melo 5389c00a81bSAdrian Hunter struct symbol *dso__next_symbol(struct symbol *sym) 5399c00a81bSAdrian Hunter { 5409c00a81bSAdrian Hunter return symbols__next(sym); 5419c00a81bSAdrian Hunter } 5429c00a81bSAdrian Hunter 54318bd7264SArnaldo Carvalho de Melo struct symbol *symbol__next_by_name(struct symbol *sym) 54418bd7264SArnaldo Carvalho de Melo { 54518bd7264SArnaldo Carvalho de Melo struct symbol_name_rb_node *s = container_of(sym, struct symbol_name_rb_node, sym); 54618bd7264SArnaldo Carvalho de Melo struct rb_node *n = rb_next(&s->rb_node); 54718bd7264SArnaldo Carvalho de Melo 54818bd7264SArnaldo Carvalho de Melo return n ? &rb_entry(n, struct symbol_name_rb_node, rb_node)->sym : NULL; 54918bd7264SArnaldo Carvalho de Melo } 55018bd7264SArnaldo Carvalho de Melo 55118bd7264SArnaldo Carvalho de Melo /* 552af07eeb0SArnaldo Carvalho de Melo * Returns first symbol that matched with @name. 55318bd7264SArnaldo Carvalho de Melo */ 5543183f8caSArnaldo Carvalho de Melo struct symbol *dso__find_symbol_by_name(struct dso *dso, const char *name) 55579406cd7SArnaldo Carvalho de Melo { 5563183f8caSArnaldo Carvalho de Melo struct symbol *s = symbols__find_by_name(&dso->symbol_names, name, 557d8040645SPaul Clarke SYMBOL_TAG_INCLUDE__NONE); 558d8040645SPaul Clarke if (!s) 5593183f8caSArnaldo Carvalho de Melo s = symbols__find_by_name(&dso->symbol_names, name, 560d8040645SPaul Clarke SYMBOL_TAG_INCLUDE__DEFAULT_ONLY); 561d8040645SPaul Clarke return s; 56279406cd7SArnaldo Carvalho de Melo } 56379406cd7SArnaldo Carvalho de Melo 5643183f8caSArnaldo Carvalho de Melo void dso__sort_by_name(struct dso *dso) 56579406cd7SArnaldo Carvalho de Melo { 5663183f8caSArnaldo Carvalho de Melo dso__set_sorted_by_name(dso); 5673183f8caSArnaldo Carvalho de Melo return symbols__sort_by_name(&dso->symbol_names, &dso->symbols); 56879406cd7SArnaldo Carvalho de Melo } 56979406cd7SArnaldo Carvalho de Melo 57032add10fSIan Rogers /* 57132add10fSIan Rogers * While we find nice hex chars, build a long_val. 57232add10fSIan Rogers * Return number of chars processed. 57332add10fSIan Rogers */ 57432add10fSIan Rogers static int hex2u64(const char *ptr, u64 *long_val) 57532add10fSIan Rogers { 57632add10fSIan Rogers char *p; 57732add10fSIan Rogers 57832add10fSIan Rogers *long_val = strtoull(ptr, &p, 16); 57932add10fSIan Rogers 58032add10fSIan Rogers return p - ptr; 58132add10fSIan Rogers } 58232add10fSIan Rogers 58332add10fSIan Rogers 584316d70d6SAdrian Hunter int modules__parse(const char *filename, void *arg, 585316d70d6SAdrian Hunter int (*process_module)(void *arg, const char *name, 5869ad4652bSThomas Richter u64 start, u64 size)) 587316d70d6SAdrian Hunter { 588316d70d6SAdrian Hunter char *line = NULL; 589316d70d6SAdrian Hunter size_t n; 590316d70d6SAdrian Hunter FILE *file; 591316d70d6SAdrian Hunter int err = 0; 592316d70d6SAdrian Hunter 593316d70d6SAdrian Hunter file = fopen(filename, "r"); 594316d70d6SAdrian Hunter if (file == NULL) 595316d70d6SAdrian Hunter return -1; 596316d70d6SAdrian Hunter 597316d70d6SAdrian Hunter while (1) { 598316d70d6SAdrian Hunter char name[PATH_MAX]; 5999ad4652bSThomas Richter u64 start, size; 6009ad4652bSThomas Richter char *sep, *endptr; 601316d70d6SAdrian Hunter ssize_t line_len; 602316d70d6SAdrian Hunter 603316d70d6SAdrian Hunter line_len = getline(&line, &n, file); 604316d70d6SAdrian Hunter if (line_len < 0) { 605316d70d6SAdrian Hunter if (feof(file)) 606316d70d6SAdrian Hunter break; 607316d70d6SAdrian Hunter err = -1; 608316d70d6SAdrian Hunter goto out; 609316d70d6SAdrian Hunter } 610316d70d6SAdrian Hunter 611316d70d6SAdrian Hunter if (!line) { 612316d70d6SAdrian Hunter err = -1; 613316d70d6SAdrian Hunter goto out; 614316d70d6SAdrian Hunter } 615316d70d6SAdrian Hunter 616316d70d6SAdrian Hunter line[--line_len] = '\0'; /* \n */ 617316d70d6SAdrian Hunter 618316d70d6SAdrian Hunter sep = strrchr(line, 'x'); 619316d70d6SAdrian Hunter if (sep == NULL) 620316d70d6SAdrian Hunter continue; 621316d70d6SAdrian Hunter 622316d70d6SAdrian Hunter hex2u64(sep + 1, &start); 623316d70d6SAdrian Hunter 624316d70d6SAdrian Hunter sep = strchr(line, ' '); 625316d70d6SAdrian Hunter if (sep == NULL) 626316d70d6SAdrian Hunter continue; 627316d70d6SAdrian Hunter 628316d70d6SAdrian Hunter *sep = '\0'; 629316d70d6SAdrian Hunter 630316d70d6SAdrian Hunter scnprintf(name, sizeof(name), "[%s]", line); 631316d70d6SAdrian Hunter 6329ad4652bSThomas Richter size = strtoul(sep + 1, &endptr, 0); 6339ad4652bSThomas Richter if (*endptr != ' ' && *endptr != '\t') 6349ad4652bSThomas Richter continue; 6359ad4652bSThomas Richter 6369ad4652bSThomas Richter err = process_module(arg, name, start, size); 637316d70d6SAdrian Hunter if (err) 638316d70d6SAdrian Hunter break; 639316d70d6SAdrian Hunter } 640316d70d6SAdrian Hunter out: 641316d70d6SAdrian Hunter free(line); 642316d70d6SAdrian Hunter fclose(file); 643316d70d6SAdrian Hunter return err; 644316d70d6SAdrian Hunter } 645316d70d6SAdrian Hunter 646e7110b9fSArnaldo Carvalho de Melo /* 647e7110b9fSArnaldo Carvalho de Melo * These are symbols in the kernel image, so make sure that 648e7110b9fSArnaldo Carvalho de Melo * sym is from a kernel DSO. 649e7110b9fSArnaldo Carvalho de Melo */ 650608c34deSArnaldo Carvalho de Melo static bool symbol__is_idle(const char *name) 65182d1deb0SDavid Ahern { 65282d1deb0SDavid Ahern const char * const idle_symbols[] = { 6530e71459aSKim Phillips "acpi_idle_do_entry", 6540e71459aSKim Phillips "acpi_processor_ffh_cstate_enter", 655549aff77SArnaldo Carvalho de Melo "arch_cpu_idle", 65682d1deb0SDavid Ahern "cpu_idle", 657e0336ed6SArnaldo Carvalho de Melo "cpu_startup_entry", 6580e71459aSKim Phillips "idle_cpu", 65982d1deb0SDavid Ahern "intel_idle", 66082d1deb0SDavid Ahern "default_idle", 66182d1deb0SDavid Ahern "native_safe_halt", 66282d1deb0SDavid Ahern "enter_idle", 66382d1deb0SDavid Ahern "exit_idle", 66482d1deb0SDavid Ahern "mwait_idle", 66582d1deb0SDavid Ahern "mwait_idle_with_hints", 666783abbd4SArnaldo Carvalho de Melo "mwait_idle_with_hints.constprop.0", 66782d1deb0SDavid Ahern "poll_idle", 66882d1deb0SDavid Ahern "ppc64_runlatch_off", 66982d1deb0SDavid Ahern "pseries_dedicated_idle_sleep", 67019bf119cSSven Schnelle "psw_idle", 67119bf119cSSven Schnelle "psw_idle_exit", 67282d1deb0SDavid Ahern NULL 67382d1deb0SDavid Ahern }; 67482d1deb0SDavid Ahern int i; 675bc5f15beSKim Phillips static struct strlist *idle_symbols_list; 67682d1deb0SDavid Ahern 677bc5f15beSKim Phillips if (idle_symbols_list) 678bc5f15beSKim Phillips return strlist__has_entry(idle_symbols_list, name); 67982d1deb0SDavid Ahern 680bc5f15beSKim Phillips idle_symbols_list = strlist__new(NULL, NULL); 681bc5f15beSKim Phillips 682bc5f15beSKim Phillips for (i = 0; idle_symbols[i]; i++) 683bc5f15beSKim Phillips strlist__add(idle_symbols_list, idle_symbols[i]); 684bc5f15beSKim Phillips 685bc5f15beSKim Phillips return strlist__has_entry(idle_symbols_list, name); 68682d1deb0SDavid Ahern } 68782d1deb0SDavid Ahern 688682b335aSArnaldo Carvalho de Melo static int map__process_kallsym_symbol(void *arg, const char *name, 68982151520SCody P Schafer char type, u64 start) 690682b335aSArnaldo Carvalho de Melo { 691682b335aSArnaldo Carvalho de Melo struct symbol *sym; 692333cc76cSArnaldo Carvalho de Melo struct dso *dso = arg; 6937137ff50SDavidlohr Bueso struct rb_root_cached *root = &dso->symbols; 694682b335aSArnaldo Carvalho de Melo 6953183f8caSArnaldo Carvalho de Melo if (!symbol_type__filter(type)) 696682b335aSArnaldo Carvalho de Melo return 0; 697682b335aSArnaldo Carvalho de Melo 69882151520SCody P Schafer /* 69982151520SCody P Schafer * module symbols are not sorted so we add all 70082151520SCody P Schafer * symbols, setting length to 0, and rely on 70182151520SCody P Schafer * symbols__fixup_end() to fix it up. 70282151520SCody P Schafer */ 703af30bffaSArnaldo Carvalho de Melo sym = symbol__new(start, 0, kallsyms2elf_binding(type), kallsyms2elf_type(type), name); 7042e538c4aSArnaldo Carvalho de Melo if (sym == NULL) 705682b335aSArnaldo Carvalho de Melo return -ENOMEM; 70682164161SArnaldo Carvalho de Melo /* 70782164161SArnaldo Carvalho de Melo * We will pass the symbols to the filter later, in 7084e06255fSArnaldo Carvalho de Melo * map__split_kallsyms, when we have split the maps per module 70982164161SArnaldo Carvalho de Melo */ 710608c34deSArnaldo Carvalho de Melo __symbols__insert(root, sym, !strchr(name, '[')); 711a1645ce1SZhang, Yanmin 712682b335aSArnaldo Carvalho de Melo return 0; 7132e538c4aSArnaldo Carvalho de Melo } 7142e538c4aSArnaldo Carvalho de Melo 715682b335aSArnaldo Carvalho de Melo /* 716682b335aSArnaldo Carvalho de Melo * Loads the function entries in /proc/kallsyms into kernel_map->dso, 717682b335aSArnaldo Carvalho de Melo * so that we can in the next step set the symbol ->end address and then 718682b335aSArnaldo Carvalho de Melo * call kernel_maps__split_kallsyms. 719682b335aSArnaldo Carvalho de Melo */ 720333cc76cSArnaldo Carvalho de Melo static int dso__load_all_kallsyms(struct dso *dso, const char *filename) 721682b335aSArnaldo Carvalho de Melo { 722333cc76cSArnaldo Carvalho de Melo return kallsyms__parse(filename, dso, map__process_kallsym_symbol); 7232e538c4aSArnaldo Carvalho de Melo } 7242e538c4aSArnaldo Carvalho de Melo 72579b6bb73SArnaldo Carvalho de Melo static int maps__split_kallsyms_for_kcore(struct maps *kmaps, struct dso *dso) 7268e0cf965SAdrian Hunter { 7278e0cf965SAdrian Hunter struct map *curr_map; 7288e0cf965SAdrian Hunter struct symbol *pos; 729866548ddSAdrian Hunter int count = 0; 7307137ff50SDavidlohr Bueso struct rb_root_cached old_root = dso->symbols; 7317137ff50SDavidlohr Bueso struct rb_root_cached *root = &dso->symbols; 7327137ff50SDavidlohr Bueso struct rb_node *next = rb_first_cached(root); 7338e0cf965SAdrian Hunter 734ba92732eSWang Nan if (!kmaps) 735ba92732eSWang Nan return -1; 736ba92732eSWang Nan 7377137ff50SDavidlohr Bueso *root = RB_ROOT_CACHED; 738866548ddSAdrian Hunter 7398e0cf965SAdrian Hunter while (next) { 7408e0cf965SAdrian Hunter char *module; 7418e0cf965SAdrian Hunter 7428e0cf965SAdrian Hunter pos = rb_entry(next, struct symbol, rb_node); 7438e0cf965SAdrian Hunter next = rb_next(&pos->rb_node); 7448e0cf965SAdrian Hunter 7457137ff50SDavidlohr Bueso rb_erase_cached(&pos->rb_node, &old_root); 7467137ff50SDavidlohr Bueso RB_CLEAR_NODE(&pos->rb_node); 7478e0cf965SAdrian Hunter module = strchr(pos->name, '\t'); 7488e0cf965SAdrian Hunter if (module) 7498e0cf965SAdrian Hunter *module = '\0'; 7508e0cf965SAdrian Hunter 75179b6bb73SArnaldo Carvalho de Melo curr_map = maps__find(kmaps, pos->start); 7528e0cf965SAdrian Hunter 753be39db9fSArnaldo Carvalho de Melo if (!curr_map) { 7548e0cf965SAdrian Hunter symbol__delete(pos); 755866548ddSAdrian Hunter continue; 756866548ddSAdrian Hunter } 757866548ddSAdrian Hunter 7588e0cf965SAdrian Hunter pos->start -= curr_map->start - curr_map->pgoff; 759d6d45745SAdrian Hunter if (pos->end > curr_map->end) 760d6d45745SAdrian Hunter pos->end = curr_map->end; 7618e0cf965SAdrian Hunter if (pos->end) 7628e0cf965SAdrian Hunter pos->end -= curr_map->start - curr_map->pgoff; 7633183f8caSArnaldo Carvalho de Melo symbols__insert(&curr_map->dso->symbols, pos); 7648e0cf965SAdrian Hunter ++count; 7658e0cf965SAdrian Hunter } 7668e0cf965SAdrian Hunter 7678e0cf965SAdrian Hunter /* Symbols have been adjusted */ 7688e0cf965SAdrian Hunter dso->adjust_symbols = 1; 7698e0cf965SAdrian Hunter 770866548ddSAdrian Hunter return count; 7718e0cf965SAdrian Hunter } 7728e0cf965SAdrian Hunter 7732e538c4aSArnaldo Carvalho de Melo /* 7742e538c4aSArnaldo Carvalho de Melo * Split the symbols into maps, making sure there are no overlaps, i.e. the 7752e538c4aSArnaldo Carvalho de Melo * kernel range is broken in several maps, named [kernel].N, as we don't have 7762e538c4aSArnaldo Carvalho de Melo * the original ELF section names vmlinux have. 7772e538c4aSArnaldo Carvalho de Melo */ 77879b6bb73SArnaldo Carvalho de Melo static int maps__split_kallsyms(struct maps *kmaps, struct dso *dso, u64 delta, 77915e0e2d4SArnaldo Carvalho de Melo struct map *initial_map) 7802e538c4aSArnaldo Carvalho de Melo { 781ba92732eSWang Nan struct machine *machine; 78215e0e2d4SArnaldo Carvalho de Melo struct map *curr_map = initial_map; 7832e538c4aSArnaldo Carvalho de Melo struct symbol *pos; 7848a953312SArnaldo Carvalho de Melo int count = 0, moved = 0; 7857137ff50SDavidlohr Bueso struct rb_root_cached *root = &dso->symbols; 7867137ff50SDavidlohr Bueso struct rb_node *next = rb_first_cached(root); 7872e538c4aSArnaldo Carvalho de Melo int kernel_range = 0; 7884d004365SAdrian Hunter bool x86_64; 7892e538c4aSArnaldo Carvalho de Melo 790ba92732eSWang Nan if (!kmaps) 791ba92732eSWang Nan return -1; 792ba92732eSWang Nan 793ba92732eSWang Nan machine = kmaps->machine; 794ba92732eSWang Nan 7954d004365SAdrian Hunter x86_64 = machine__is(machine, "x86_64"); 7964d004365SAdrian Hunter 7972e538c4aSArnaldo Carvalho de Melo while (next) { 7982e538c4aSArnaldo Carvalho de Melo char *module; 7992e538c4aSArnaldo Carvalho de Melo 8002e538c4aSArnaldo Carvalho de Melo pos = rb_entry(next, struct symbol, rb_node); 8012e538c4aSArnaldo Carvalho de Melo next = rb_next(&pos->rb_node); 8022e538c4aSArnaldo Carvalho de Melo 8032e538c4aSArnaldo Carvalho de Melo module = strchr(pos->name, '\t'); 8042e538c4aSArnaldo Carvalho de Melo if (module) { 80575be6cf4SArnaldo Carvalho de Melo if (!symbol_conf.use_modules) 8061de8e245SArnaldo Carvalho de Melo goto discard_symbol; 8071de8e245SArnaldo Carvalho de Melo 8082e538c4aSArnaldo Carvalho de Melo *module++ = '\0'; 8092e538c4aSArnaldo Carvalho de Melo 810b7cece76SArnaldo Carvalho de Melo if (strcmp(curr_map->dso->short_name, module)) { 81115e0e2d4SArnaldo Carvalho de Melo if (curr_map != initial_map && 8121c695c88SJiri Olsa dso->kernel == DSO_SPACE__KERNEL_GUEST && 81323346f21SArnaldo Carvalho de Melo machine__is_default_guest(machine)) { 814a1645ce1SZhang, Yanmin /* 815a1645ce1SZhang, Yanmin * We assume all symbols of a module are 816a1645ce1SZhang, Yanmin * continuous in * kallsyms, so curr_map 817a1645ce1SZhang, Yanmin * points to a module and all its 818a1645ce1SZhang, Yanmin * symbols are in its kmap. Mark it as 819a1645ce1SZhang, Yanmin * loaded. 820a1645ce1SZhang, Yanmin */ 8213183f8caSArnaldo Carvalho de Melo dso__set_loaded(curr_map->dso); 822af427bf5SArnaldo Carvalho de Melo } 823b7cece76SArnaldo Carvalho de Melo 82479b6bb73SArnaldo Carvalho de Melo curr_map = maps__find_by_name(kmaps, module); 825a1645ce1SZhang, Yanmin if (curr_map == NULL) { 8262f51903bSArnaldo Carvalho de Melo pr_debug("%s/proc/{kallsyms,modules} " 827a1645ce1SZhang, Yanmin "inconsistency while looking " 828a1645ce1SZhang, Yanmin "for \"%s\" module!\n", 82923346f21SArnaldo Carvalho de Melo machine->root_dir, module); 83015e0e2d4SArnaldo Carvalho de Melo curr_map = initial_map; 831a1645ce1SZhang, Yanmin goto discard_symbol; 832a1645ce1SZhang, Yanmin } 833a1645ce1SZhang, Yanmin 834a1645ce1SZhang, Yanmin if (curr_map->dso->loaded && 83523346f21SArnaldo Carvalho de Melo !machine__is_default_guest(machine)) 836b7cece76SArnaldo Carvalho de Melo goto discard_symbol; 837af427bf5SArnaldo Carvalho de Melo } 83886470930SIngo Molnar /* 8392e538c4aSArnaldo Carvalho de Melo * So that we look just like we get from .ko files, 84015e0e2d4SArnaldo Carvalho de Melo * i.e. not prelinked, relative to initial_map->start. 84186470930SIngo Molnar */ 8424e06255fSArnaldo Carvalho de Melo pos->start = curr_map->map_ip(curr_map, pos->start); 8434e06255fSArnaldo Carvalho de Melo pos->end = curr_map->map_ip(curr_map, pos->end); 8444d004365SAdrian Hunter } else if (x86_64 && is_entry_trampoline(pos->name)) { 8454d004365SAdrian Hunter /* 8464d004365SAdrian Hunter * These symbols are not needed anymore since the 8474d004365SAdrian Hunter * trampoline maps refer to the text section and it's 8484d004365SAdrian Hunter * symbols instead. Avoid having to deal with 8494d004365SAdrian Hunter * relocations, and the assumption that the first symbol 8504d004365SAdrian Hunter * is the start of kernel text, by simply removing the 8514d004365SAdrian Hunter * symbols at this point. 8524d004365SAdrian Hunter */ 8534d004365SAdrian Hunter goto discard_symbol; 85415e0e2d4SArnaldo Carvalho de Melo } else if (curr_map != initial_map) { 8552e538c4aSArnaldo Carvalho de Melo char dso_name[PATH_MAX]; 856aeafcbafSArnaldo Carvalho de Melo struct dso *ndso; 85786470930SIngo Molnar 858d9b62abaSAdrian Hunter if (delta) { 859d9b62abaSAdrian Hunter /* Kernel was relocated at boot time */ 860d9b62abaSAdrian Hunter pos->start -= delta; 861d9b62abaSAdrian Hunter pos->end -= delta; 862d9b62abaSAdrian Hunter } 863d9b62abaSAdrian Hunter 8648a953312SArnaldo Carvalho de Melo if (count == 0) { 86515e0e2d4SArnaldo Carvalho de Melo curr_map = initial_map; 866be39db9fSArnaldo Carvalho de Melo goto add_symbol; 8678a953312SArnaldo Carvalho de Melo } 8688a953312SArnaldo Carvalho de Melo 8691c695c88SJiri Olsa if (dso->kernel == DSO_SPACE__KERNEL_GUEST) 870a1645ce1SZhang, Yanmin snprintf(dso_name, sizeof(dso_name), 871a1645ce1SZhang, Yanmin "[guest.kernel].%d", 872a1645ce1SZhang, Yanmin kernel_range++); 873a1645ce1SZhang, Yanmin else 874a1645ce1SZhang, Yanmin snprintf(dso_name, sizeof(dso_name), 875a1645ce1SZhang, Yanmin "[kernel].%d", 8762e538c4aSArnaldo Carvalho de Melo kernel_range++); 87786470930SIngo Molnar 878aeafcbafSArnaldo Carvalho de Melo ndso = dso__new(dso_name); 879aeafcbafSArnaldo Carvalho de Melo if (ndso == NULL) 8802e538c4aSArnaldo Carvalho de Melo return -1; 8812e538c4aSArnaldo Carvalho de Melo 882aeafcbafSArnaldo Carvalho de Melo ndso->kernel = dso->kernel; 883a1645ce1SZhang, Yanmin 8843183f8caSArnaldo Carvalho de Melo curr_map = map__new2(pos->start, ndso); 88537fe5fcbSZhang, Yanmin if (curr_map == NULL) { 886d3a7c489SArnaldo Carvalho de Melo dso__put(ndso); 8872e538c4aSArnaldo Carvalho de Melo return -1; 8882e538c4aSArnaldo Carvalho de Melo } 8892e538c4aSArnaldo Carvalho de Melo 8904e06255fSArnaldo Carvalho de Melo curr_map->map_ip = curr_map->unmap_ip = identity__map_ip; 89179b6bb73SArnaldo Carvalho de Melo maps__insert(kmaps, curr_map); 8922e538c4aSArnaldo Carvalho de Melo ++kernel_range; 893d9b62abaSAdrian Hunter } else if (delta) { 894d9b62abaSAdrian Hunter /* Kernel was relocated at boot time */ 895d9b62abaSAdrian Hunter pos->start -= delta; 896d9b62abaSAdrian Hunter pos->end -= delta; 8972e538c4aSArnaldo Carvalho de Melo } 898be39db9fSArnaldo Carvalho de Melo add_symbol: 89915e0e2d4SArnaldo Carvalho de Melo if (curr_map != initial_map) { 9007137ff50SDavidlohr Bueso rb_erase_cached(&pos->rb_node, root); 9013183f8caSArnaldo Carvalho de Melo symbols__insert(&curr_map->dso->symbols, pos); 9028a953312SArnaldo Carvalho de Melo ++moved; 9038a953312SArnaldo Carvalho de Melo } else 9048a953312SArnaldo Carvalho de Melo ++count; 905be39db9fSArnaldo Carvalho de Melo 906be39db9fSArnaldo Carvalho de Melo continue; 907be39db9fSArnaldo Carvalho de Melo discard_symbol: 9087137ff50SDavidlohr Bueso rb_erase_cached(&pos->rb_node, root); 909be39db9fSArnaldo Carvalho de Melo symbol__delete(pos); 91086470930SIngo Molnar } 91186470930SIngo Molnar 91215e0e2d4SArnaldo Carvalho de Melo if (curr_map != initial_map && 9131c695c88SJiri Olsa dso->kernel == DSO_SPACE__KERNEL_GUEST && 91423346f21SArnaldo Carvalho de Melo machine__is_default_guest(kmaps->machine)) { 9153183f8caSArnaldo Carvalho de Melo dso__set_loaded(curr_map->dso); 916a1645ce1SZhang, Yanmin } 917a1645ce1SZhang, Yanmin 9188a953312SArnaldo Carvalho de Melo return count + moved; 91986470930SIngo Molnar } 92086470930SIngo Molnar 9213f067dcaSArnaldo Carvalho de Melo bool symbol__restricted_filename(const char *filename, 922ec80fde7SArnaldo Carvalho de Melo const char *restricted_filename) 923ec80fde7SArnaldo Carvalho de Melo { 924ec80fde7SArnaldo Carvalho de Melo bool restricted = false; 925ec80fde7SArnaldo Carvalho de Melo 926ec80fde7SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict) { 927ec80fde7SArnaldo Carvalho de Melo char *r = realpath(filename, NULL); 928ec80fde7SArnaldo Carvalho de Melo 929ec80fde7SArnaldo Carvalho de Melo if (r != NULL) { 930ec80fde7SArnaldo Carvalho de Melo restricted = strcmp(r, restricted_filename) == 0; 931ec80fde7SArnaldo Carvalho de Melo free(r); 932ec80fde7SArnaldo Carvalho de Melo return restricted; 933ec80fde7SArnaldo Carvalho de Melo } 934ec80fde7SArnaldo Carvalho de Melo } 935ec80fde7SArnaldo Carvalho de Melo 936ec80fde7SArnaldo Carvalho de Melo return restricted; 937ec80fde7SArnaldo Carvalho de Melo } 938ec80fde7SArnaldo Carvalho de Melo 93952afdaf9SAdrian Hunter struct module_info { 94052afdaf9SAdrian Hunter struct rb_node rb_node; 94152afdaf9SAdrian Hunter char *name; 94252afdaf9SAdrian Hunter u64 start; 94352afdaf9SAdrian Hunter }; 94452afdaf9SAdrian Hunter 94552afdaf9SAdrian Hunter static void add_module(struct module_info *mi, struct rb_root *modules) 94652afdaf9SAdrian Hunter { 94752afdaf9SAdrian Hunter struct rb_node **p = &modules->rb_node; 94852afdaf9SAdrian Hunter struct rb_node *parent = NULL; 94952afdaf9SAdrian Hunter struct module_info *m; 95052afdaf9SAdrian Hunter 95152afdaf9SAdrian Hunter while (*p != NULL) { 95252afdaf9SAdrian Hunter parent = *p; 95352afdaf9SAdrian Hunter m = rb_entry(parent, struct module_info, rb_node); 95452afdaf9SAdrian Hunter if (strcmp(mi->name, m->name) < 0) 95552afdaf9SAdrian Hunter p = &(*p)->rb_left; 95652afdaf9SAdrian Hunter else 95752afdaf9SAdrian Hunter p = &(*p)->rb_right; 95852afdaf9SAdrian Hunter } 95952afdaf9SAdrian Hunter rb_link_node(&mi->rb_node, parent, p); 96052afdaf9SAdrian Hunter rb_insert_color(&mi->rb_node, modules); 96152afdaf9SAdrian Hunter } 96252afdaf9SAdrian Hunter 96352afdaf9SAdrian Hunter static void delete_modules(struct rb_root *modules) 96452afdaf9SAdrian Hunter { 96552afdaf9SAdrian Hunter struct module_info *mi; 96652afdaf9SAdrian Hunter struct rb_node *next = rb_first(modules); 96752afdaf9SAdrian Hunter 96852afdaf9SAdrian Hunter while (next) { 96952afdaf9SAdrian Hunter mi = rb_entry(next, struct module_info, rb_node); 97052afdaf9SAdrian Hunter next = rb_next(&mi->rb_node); 97152afdaf9SAdrian Hunter rb_erase(&mi->rb_node, modules); 97274cf249dSArnaldo Carvalho de Melo zfree(&mi->name); 97352afdaf9SAdrian Hunter free(mi); 97452afdaf9SAdrian Hunter } 97552afdaf9SAdrian Hunter } 97652afdaf9SAdrian Hunter 97752afdaf9SAdrian Hunter static struct module_info *find_module(const char *name, 97852afdaf9SAdrian Hunter struct rb_root *modules) 97952afdaf9SAdrian Hunter { 98052afdaf9SAdrian Hunter struct rb_node *n = modules->rb_node; 98152afdaf9SAdrian Hunter 98252afdaf9SAdrian Hunter while (n) { 98352afdaf9SAdrian Hunter struct module_info *m; 98452afdaf9SAdrian Hunter int cmp; 98552afdaf9SAdrian Hunter 98652afdaf9SAdrian Hunter m = rb_entry(n, struct module_info, rb_node); 98752afdaf9SAdrian Hunter cmp = strcmp(name, m->name); 98852afdaf9SAdrian Hunter if (cmp < 0) 98952afdaf9SAdrian Hunter n = n->rb_left; 99052afdaf9SAdrian Hunter else if (cmp > 0) 99152afdaf9SAdrian Hunter n = n->rb_right; 99252afdaf9SAdrian Hunter else 99352afdaf9SAdrian Hunter return m; 99452afdaf9SAdrian Hunter } 99552afdaf9SAdrian Hunter 99652afdaf9SAdrian Hunter return NULL; 99752afdaf9SAdrian Hunter } 99852afdaf9SAdrian Hunter 9999ad4652bSThomas Richter static int __read_proc_modules(void *arg, const char *name, u64 start, 10009ad4652bSThomas Richter u64 size __maybe_unused) 100152afdaf9SAdrian Hunter { 100252afdaf9SAdrian Hunter struct rb_root *modules = arg; 100352afdaf9SAdrian Hunter struct module_info *mi; 100452afdaf9SAdrian Hunter 100552afdaf9SAdrian Hunter mi = zalloc(sizeof(struct module_info)); 100652afdaf9SAdrian Hunter if (!mi) 100752afdaf9SAdrian Hunter return -ENOMEM; 100852afdaf9SAdrian Hunter 100952afdaf9SAdrian Hunter mi->name = strdup(name); 101052afdaf9SAdrian Hunter mi->start = start; 101152afdaf9SAdrian Hunter 101252afdaf9SAdrian Hunter if (!mi->name) { 101352afdaf9SAdrian Hunter free(mi); 101452afdaf9SAdrian Hunter return -ENOMEM; 101552afdaf9SAdrian Hunter } 101652afdaf9SAdrian Hunter 101752afdaf9SAdrian Hunter add_module(mi, modules); 101852afdaf9SAdrian Hunter 101952afdaf9SAdrian Hunter return 0; 102052afdaf9SAdrian Hunter } 102152afdaf9SAdrian Hunter 102252afdaf9SAdrian Hunter static int read_proc_modules(const char *filename, struct rb_root *modules) 102352afdaf9SAdrian Hunter { 102452afdaf9SAdrian Hunter if (symbol__restricted_filename(filename, "/proc/modules")) 102552afdaf9SAdrian Hunter return -1; 102652afdaf9SAdrian Hunter 102752afdaf9SAdrian Hunter if (modules__parse(filename, modules, __read_proc_modules)) { 102852afdaf9SAdrian Hunter delete_modules(modules); 102952afdaf9SAdrian Hunter return -1; 103052afdaf9SAdrian Hunter } 103152afdaf9SAdrian Hunter 103252afdaf9SAdrian Hunter return 0; 103352afdaf9SAdrian Hunter } 103452afdaf9SAdrian Hunter 1035fc1b691dSAdrian Hunter int compare_proc_modules(const char *from, const char *to) 1036fc1b691dSAdrian Hunter { 1037fc1b691dSAdrian Hunter struct rb_root from_modules = RB_ROOT; 1038fc1b691dSAdrian Hunter struct rb_root to_modules = RB_ROOT; 1039fc1b691dSAdrian Hunter struct rb_node *from_node, *to_node; 1040fc1b691dSAdrian Hunter struct module_info *from_m, *to_m; 1041fc1b691dSAdrian Hunter int ret = -1; 1042fc1b691dSAdrian Hunter 1043fc1b691dSAdrian Hunter if (read_proc_modules(from, &from_modules)) 1044fc1b691dSAdrian Hunter return -1; 1045fc1b691dSAdrian Hunter 1046fc1b691dSAdrian Hunter if (read_proc_modules(to, &to_modules)) 1047fc1b691dSAdrian Hunter goto out_delete_from; 1048fc1b691dSAdrian Hunter 1049fc1b691dSAdrian Hunter from_node = rb_first(&from_modules); 1050fc1b691dSAdrian Hunter to_node = rb_first(&to_modules); 1051fc1b691dSAdrian Hunter while (from_node) { 1052fc1b691dSAdrian Hunter if (!to_node) 1053fc1b691dSAdrian Hunter break; 1054fc1b691dSAdrian Hunter 1055fc1b691dSAdrian Hunter from_m = rb_entry(from_node, struct module_info, rb_node); 1056fc1b691dSAdrian Hunter to_m = rb_entry(to_node, struct module_info, rb_node); 1057fc1b691dSAdrian Hunter 1058fc1b691dSAdrian Hunter if (from_m->start != to_m->start || 1059fc1b691dSAdrian Hunter strcmp(from_m->name, to_m->name)) 1060fc1b691dSAdrian Hunter break; 1061fc1b691dSAdrian Hunter 1062fc1b691dSAdrian Hunter from_node = rb_next(from_node); 1063fc1b691dSAdrian Hunter to_node = rb_next(to_node); 1064fc1b691dSAdrian Hunter } 1065fc1b691dSAdrian Hunter 1066fc1b691dSAdrian Hunter if (!from_node && !to_node) 1067fc1b691dSAdrian Hunter ret = 0; 1068fc1b691dSAdrian Hunter 1069fc1b691dSAdrian Hunter delete_modules(&to_modules); 1070fc1b691dSAdrian Hunter out_delete_from: 1071fc1b691dSAdrian Hunter delete_modules(&from_modules); 1072fc1b691dSAdrian Hunter 1073fc1b691dSAdrian Hunter return ret; 1074fc1b691dSAdrian Hunter } 1075fc1b691dSAdrian Hunter 107679b6bb73SArnaldo Carvalho de Melo static int do_validate_kcore_modules(const char *filename, struct maps *kmaps) 107752afdaf9SAdrian Hunter { 107852afdaf9SAdrian Hunter struct rb_root modules = RB_ROOT; 107952afdaf9SAdrian Hunter struct map *old_map; 108052afdaf9SAdrian Hunter int err; 108152afdaf9SAdrian Hunter 108252afdaf9SAdrian Hunter err = read_proc_modules(filename, &modules); 108352afdaf9SAdrian Hunter if (err) 108452afdaf9SAdrian Hunter return err; 108552afdaf9SAdrian Hunter 108679b6bb73SArnaldo Carvalho de Melo maps__for_each_entry(kmaps, old_map) { 108752afdaf9SAdrian Hunter struct module_info *mi; 108852afdaf9SAdrian Hunter 10895759a682SAdrian Hunter if (!__map__is_kmodule(old_map)) { 109052afdaf9SAdrian Hunter continue; 109152afdaf9SAdrian Hunter } 109252afdaf9SAdrian Hunter 109352afdaf9SAdrian Hunter /* Module must be in memory at the same address */ 109452afdaf9SAdrian Hunter mi = find_module(old_map->dso->short_name, &modules); 109552afdaf9SAdrian Hunter if (!mi || mi->start != old_map->start) { 109652afdaf9SAdrian Hunter err = -EINVAL; 109752afdaf9SAdrian Hunter goto out; 109852afdaf9SAdrian Hunter } 109952afdaf9SAdrian Hunter } 110052afdaf9SAdrian Hunter out: 110152afdaf9SAdrian Hunter delete_modules(&modules); 110252afdaf9SAdrian Hunter return err; 110352afdaf9SAdrian Hunter } 110452afdaf9SAdrian Hunter 110552afdaf9SAdrian Hunter /* 110652afdaf9SAdrian Hunter * If kallsyms is referenced by name then we look for filename in the same 110752afdaf9SAdrian Hunter * directory. 110852afdaf9SAdrian Hunter */ 110952afdaf9SAdrian Hunter static bool filename_from_kallsyms_filename(char *filename, 111052afdaf9SAdrian Hunter const char *base_name, 111152afdaf9SAdrian Hunter const char *kallsyms_filename) 111252afdaf9SAdrian Hunter { 111352afdaf9SAdrian Hunter char *name; 111452afdaf9SAdrian Hunter 111552afdaf9SAdrian Hunter strcpy(filename, kallsyms_filename); 111652afdaf9SAdrian Hunter name = strrchr(filename, '/'); 111752afdaf9SAdrian Hunter if (!name) 111852afdaf9SAdrian Hunter return false; 111952afdaf9SAdrian Hunter 112052afdaf9SAdrian Hunter name += 1; 112152afdaf9SAdrian Hunter 112252afdaf9SAdrian Hunter if (!strcmp(name, "kallsyms")) { 112352afdaf9SAdrian Hunter strcpy(name, base_name); 112452afdaf9SAdrian Hunter return true; 112552afdaf9SAdrian Hunter } 112652afdaf9SAdrian Hunter 112752afdaf9SAdrian Hunter return false; 112852afdaf9SAdrian Hunter } 112952afdaf9SAdrian Hunter 113052afdaf9SAdrian Hunter static int validate_kcore_modules(const char *kallsyms_filename, 113152afdaf9SAdrian Hunter struct map *map) 113252afdaf9SAdrian Hunter { 113379b6bb73SArnaldo Carvalho de Melo struct maps *kmaps = map__kmaps(map); 113452afdaf9SAdrian Hunter char modules_filename[PATH_MAX]; 113552afdaf9SAdrian Hunter 1136ba92732eSWang Nan if (!kmaps) 1137ba92732eSWang Nan return -EINVAL; 1138ba92732eSWang Nan 113952afdaf9SAdrian Hunter if (!filename_from_kallsyms_filename(modules_filename, "modules", 114052afdaf9SAdrian Hunter kallsyms_filename)) 114152afdaf9SAdrian Hunter return -EINVAL; 114252afdaf9SAdrian Hunter 11435759a682SAdrian Hunter if (do_validate_kcore_modules(modules_filename, kmaps)) 114452afdaf9SAdrian Hunter return -EINVAL; 114552afdaf9SAdrian Hunter 114652afdaf9SAdrian Hunter return 0; 114752afdaf9SAdrian Hunter } 114852afdaf9SAdrian Hunter 1149a00d28cbSAdrian Hunter static int validate_kcore_addresses(const char *kallsyms_filename, 1150a00d28cbSAdrian Hunter struct map *map) 1151a00d28cbSAdrian Hunter { 1152a00d28cbSAdrian Hunter struct kmap *kmap = map__kmap(map); 1153a00d28cbSAdrian Hunter 1154ba92732eSWang Nan if (!kmap) 1155ba92732eSWang Nan return -EINVAL; 1156ba92732eSWang Nan 1157a00d28cbSAdrian Hunter if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) { 1158a00d28cbSAdrian Hunter u64 start; 1159a00d28cbSAdrian Hunter 1160b843f62aSArnaldo Carvalho de Melo if (kallsyms__get_function_start(kallsyms_filename, 1161b843f62aSArnaldo Carvalho de Melo kmap->ref_reloc_sym->name, &start)) 1162b843f62aSArnaldo Carvalho de Melo return -ENOENT; 1163a00d28cbSAdrian Hunter if (start != kmap->ref_reloc_sym->addr) 1164a00d28cbSAdrian Hunter return -EINVAL; 1165a00d28cbSAdrian Hunter } 1166a00d28cbSAdrian Hunter 1167a00d28cbSAdrian Hunter return validate_kcore_modules(kallsyms_filename, map); 1168a00d28cbSAdrian Hunter } 1169a00d28cbSAdrian Hunter 11708e0cf965SAdrian Hunter struct kcore_mapfn_data { 11718e0cf965SAdrian Hunter struct dso *dso; 11728e0cf965SAdrian Hunter struct list_head maps; 11738e0cf965SAdrian Hunter }; 11748e0cf965SAdrian Hunter 11758e0cf965SAdrian Hunter static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) 11768e0cf965SAdrian Hunter { 11778e0cf965SAdrian Hunter struct kcore_mapfn_data *md = data; 11788e0cf965SAdrian Hunter struct map *map; 11798e0cf965SAdrian Hunter 11803183f8caSArnaldo Carvalho de Melo map = map__new2(start, md->dso); 11818e0cf965SAdrian Hunter if (map == NULL) 11828e0cf965SAdrian Hunter return -ENOMEM; 11838e0cf965SAdrian Hunter 11848e0cf965SAdrian Hunter map->end = map->start + len; 11858e0cf965SAdrian Hunter map->pgoff = pgoff; 11868e0cf965SAdrian Hunter 11878e0cf965SAdrian Hunter list_add(&map->node, &md->maps); 11888e0cf965SAdrian Hunter 11898e0cf965SAdrian Hunter return 0; 11908e0cf965SAdrian Hunter } 11918e0cf965SAdrian Hunter 1192fb5a88d4SJiri Olsa /* 119379b6bb73SArnaldo Carvalho de Melo * Merges map into maps by splitting the new map within the existing map 119479b6bb73SArnaldo Carvalho de Melo * regions. 1195fb5a88d4SJiri Olsa */ 119679b6bb73SArnaldo Carvalho de Melo int maps__merge_in(struct maps *kmaps, struct map *new_map) 1197fb5a88d4SJiri Olsa { 1198fb5a88d4SJiri Olsa struct map *old_map; 1199fb5a88d4SJiri Olsa LIST_HEAD(merged); 1200fb5a88d4SJiri Olsa 120179b6bb73SArnaldo Carvalho de Melo maps__for_each_entry(kmaps, old_map) { 1202fb5a88d4SJiri Olsa /* no overload with this one */ 1203fb5a88d4SJiri Olsa if (new_map->end < old_map->start || 1204fb5a88d4SJiri Olsa new_map->start >= old_map->end) 1205fb5a88d4SJiri Olsa continue; 1206fb5a88d4SJiri Olsa 1207fb5a88d4SJiri Olsa if (new_map->start < old_map->start) { 1208fb5a88d4SJiri Olsa /* 1209fb5a88d4SJiri Olsa * |new...... 1210fb5a88d4SJiri Olsa * |old.... 1211fb5a88d4SJiri Olsa */ 1212fb5a88d4SJiri Olsa if (new_map->end < old_map->end) { 1213fb5a88d4SJiri Olsa /* 1214fb5a88d4SJiri Olsa * |new......| -> |new..| 1215fb5a88d4SJiri Olsa * |old....| -> |old....| 1216fb5a88d4SJiri Olsa */ 1217fb5a88d4SJiri Olsa new_map->end = old_map->start; 1218fb5a88d4SJiri Olsa } else { 1219fb5a88d4SJiri Olsa /* 1220fb5a88d4SJiri Olsa * |new.............| -> |new..| |new..| 1221fb5a88d4SJiri Olsa * |old....| -> |old....| 1222fb5a88d4SJiri Olsa */ 1223fb5a88d4SJiri Olsa struct map *m = map__clone(new_map); 1224fb5a88d4SJiri Olsa 1225fb5a88d4SJiri Olsa if (!m) 1226fb5a88d4SJiri Olsa return -ENOMEM; 1227fb5a88d4SJiri Olsa 1228fb5a88d4SJiri Olsa m->end = old_map->start; 1229fb5a88d4SJiri Olsa list_add_tail(&m->node, &merged); 12300affd0e5SAdrian Hunter new_map->pgoff += old_map->end - new_map->start; 1231fb5a88d4SJiri Olsa new_map->start = old_map->end; 1232fb5a88d4SJiri Olsa } 1233fb5a88d4SJiri Olsa } else { 1234fb5a88d4SJiri Olsa /* 1235fb5a88d4SJiri Olsa * |new...... 1236fb5a88d4SJiri Olsa * |old.... 1237fb5a88d4SJiri Olsa */ 1238fb5a88d4SJiri Olsa if (new_map->end < old_map->end) { 1239fb5a88d4SJiri Olsa /* 1240fb5a88d4SJiri Olsa * |new..| -> x 1241fb5a88d4SJiri Olsa * |old.........| -> |old.........| 1242fb5a88d4SJiri Olsa */ 1243fb5a88d4SJiri Olsa map__put(new_map); 1244fb5a88d4SJiri Olsa new_map = NULL; 1245fb5a88d4SJiri Olsa break; 1246fb5a88d4SJiri Olsa } else { 1247fb5a88d4SJiri Olsa /* 1248fb5a88d4SJiri Olsa * |new......| -> |new...| 1249fb5a88d4SJiri Olsa * |old....| -> |old....| 1250fb5a88d4SJiri Olsa */ 12510affd0e5SAdrian Hunter new_map->pgoff += old_map->end - new_map->start; 1252fb5a88d4SJiri Olsa new_map->start = old_map->end; 1253fb5a88d4SJiri Olsa } 1254fb5a88d4SJiri Olsa } 1255fb5a88d4SJiri Olsa } 1256fb5a88d4SJiri Olsa 1257fb5a88d4SJiri Olsa while (!list_empty(&merged)) { 1258fb5a88d4SJiri Olsa old_map = list_entry(merged.next, struct map, node); 1259fb5a88d4SJiri Olsa list_del_init(&old_map->node); 126079b6bb73SArnaldo Carvalho de Melo maps__insert(kmaps, old_map); 1261fb5a88d4SJiri Olsa map__put(old_map); 1262fb5a88d4SJiri Olsa } 1263fb5a88d4SJiri Olsa 1264fb5a88d4SJiri Olsa if (new_map) { 126579b6bb73SArnaldo Carvalho de Melo maps__insert(kmaps, new_map); 1266fb5a88d4SJiri Olsa map__put(new_map); 1267fb5a88d4SJiri Olsa } 1268fb5a88d4SJiri Olsa return 0; 1269fb5a88d4SJiri Olsa } 1270fb5a88d4SJiri Olsa 12718e0cf965SAdrian Hunter static int dso__load_kcore(struct dso *dso, struct map *map, 12728e0cf965SAdrian Hunter const char *kallsyms_filename) 12738e0cf965SAdrian Hunter { 127479b6bb73SArnaldo Carvalho de Melo struct maps *kmaps = map__kmaps(map); 12758e0cf965SAdrian Hunter struct kcore_mapfn_data md; 127650481461SArnaldo Carvalho de Melo struct map *old_map, *new_map, *replacement_map = NULL, *next; 12771c5aae77SAdrian Hunter struct machine *machine; 12788e0cf965SAdrian Hunter bool is_64_bit; 12798e0cf965SAdrian Hunter int err, fd; 12808e0cf965SAdrian Hunter char kcore_filename[PATH_MAX]; 128156549978SAdrian Hunter u64 stext; 12828e0cf965SAdrian Hunter 1283ba92732eSWang Nan if (!kmaps) 1284ba92732eSWang Nan return -EINVAL; 1285ba92732eSWang Nan 12861c5aae77SAdrian Hunter machine = kmaps->machine; 12871c5aae77SAdrian Hunter 12888e0cf965SAdrian Hunter /* This function requires that the map is the kernel map */ 1289efdd5c6bSArnaldo Carvalho de Melo if (!__map__is_kernel(map)) 12908e0cf965SAdrian Hunter return -EINVAL; 12918e0cf965SAdrian Hunter 129252afdaf9SAdrian Hunter if (!filename_from_kallsyms_filename(kcore_filename, "kcore", 12938e0cf965SAdrian Hunter kallsyms_filename)) 12948e0cf965SAdrian Hunter return -EINVAL; 12958e0cf965SAdrian Hunter 1296a00d28cbSAdrian Hunter /* Modules and kernel must be present at their original addresses */ 1297a00d28cbSAdrian Hunter if (validate_kcore_addresses(kallsyms_filename, map)) 129852afdaf9SAdrian Hunter return -EINVAL; 129952afdaf9SAdrian Hunter 13008e0cf965SAdrian Hunter md.dso = dso; 13018e0cf965SAdrian Hunter INIT_LIST_HEAD(&md.maps); 13028e0cf965SAdrian Hunter 13038e0cf965SAdrian Hunter fd = open(kcore_filename, O_RDONLY); 130436c8bb56SLi Zhang if (fd < 0) { 1305133de940SAdrian Hunter pr_debug("Failed to open %s. Note /proc/kcore requires CAP_SYS_RAWIO capability to access.\n", 130636c8bb56SLi Zhang kcore_filename); 13078e0cf965SAdrian Hunter return -EINVAL; 130836c8bb56SLi Zhang } 13098e0cf965SAdrian Hunter 13108e0cf965SAdrian Hunter /* Read new maps into temporary lists */ 1311e9814df8SArnaldo Carvalho de Melo err = file__read_maps(fd, map->prot & PROT_EXEC, kcore_mapfn, &md, 13128e0cf965SAdrian Hunter &is_64_bit); 13138e0cf965SAdrian Hunter if (err) 13148e0cf965SAdrian Hunter goto out_err; 1315c6d8f2a4SAdrian Hunter dso->is_64_bit = is_64_bit; 13168e0cf965SAdrian Hunter 13178e0cf965SAdrian Hunter if (list_empty(&md.maps)) { 13188e0cf965SAdrian Hunter err = -EINVAL; 13198e0cf965SAdrian Hunter goto out_err; 13208e0cf965SAdrian Hunter } 13218e0cf965SAdrian Hunter 13228e0cf965SAdrian Hunter /* Remove old maps */ 132379b6bb73SArnaldo Carvalho de Melo maps__for_each_entry_safe(kmaps, old_map, next) { 1324fb5a88d4SJiri Olsa /* 1325fb5a88d4SJiri Olsa * We need to preserve eBPF maps even if they are 1326fb5a88d4SJiri Olsa * covered by kcore, because we need to access 1327fb5a88d4SJiri Olsa * eBPF dso for source data. 1328fb5a88d4SJiri Olsa */ 1329fb5a88d4SJiri Olsa if (old_map != map && !__map__is_bpf_prog(old_map)) 133079b6bb73SArnaldo Carvalho de Melo maps__remove(kmaps, old_map); 13318e0cf965SAdrian Hunter } 13321c5aae77SAdrian Hunter machine->trampolines_mapped = false; 13338e0cf965SAdrian Hunter 133456549978SAdrian Hunter /* Find the kernel map using the '_stext' symbol */ 133556549978SAdrian Hunter if (!kallsyms__get_function_start(kallsyms_filename, "_stext", &stext)) { 13368e0cf965SAdrian Hunter list_for_each_entry(new_map, &md.maps, node) { 133756549978SAdrian Hunter if (stext >= new_map->start && stext < new_map->end) { 13388e0cf965SAdrian Hunter replacement_map = new_map; 13398e0cf965SAdrian Hunter break; 13408e0cf965SAdrian Hunter } 13418e0cf965SAdrian Hunter } 134256549978SAdrian Hunter } 13438e0cf965SAdrian Hunter 13448e0cf965SAdrian Hunter if (!replacement_map) 13458e0cf965SAdrian Hunter replacement_map = list_entry(md.maps.next, struct map, node); 13468e0cf965SAdrian Hunter 13478e0cf965SAdrian Hunter /* Add new maps */ 13488e0cf965SAdrian Hunter while (!list_empty(&md.maps)) { 13498e0cf965SAdrian Hunter new_map = list_entry(md.maps.next, struct map, node); 1350facf3f06SArnaldo Carvalho de Melo list_del_init(&new_map->node); 13518e0cf965SAdrian Hunter if (new_map == replacement_map) { 13528e0cf965SAdrian Hunter map->start = new_map->start; 13538e0cf965SAdrian Hunter map->end = new_map->end; 13548e0cf965SAdrian Hunter map->pgoff = new_map->pgoff; 13558e0cf965SAdrian Hunter map->map_ip = new_map->map_ip; 13568e0cf965SAdrian Hunter map->unmap_ip = new_map->unmap_ip; 13578e0cf965SAdrian Hunter /* Ensure maps are correctly ordered */ 135884c2cafaSArnaldo Carvalho de Melo map__get(map); 135979b6bb73SArnaldo Carvalho de Melo maps__remove(kmaps, map); 136079b6bb73SArnaldo Carvalho de Melo maps__insert(kmaps, map); 136184c2cafaSArnaldo Carvalho de Melo map__put(map); 136284c2cafaSArnaldo Carvalho de Melo map__put(new_map); 1363fb5a88d4SJiri Olsa } else { 1364fb5a88d4SJiri Olsa /* 1365fb5a88d4SJiri Olsa * Merge kcore map into existing maps, 1366fb5a88d4SJiri Olsa * and ensure that current maps (eBPF) 1367fb5a88d4SJiri Olsa * stay intact. 1368fb5a88d4SJiri Olsa */ 136979b6bb73SArnaldo Carvalho de Melo if (maps__merge_in(kmaps, new_map)) 1370fb5a88d4SJiri Olsa goto out_err; 1371fb5a88d4SJiri Olsa } 13728e0cf965SAdrian Hunter } 13738e0cf965SAdrian Hunter 13741c5aae77SAdrian Hunter if (machine__is(machine, "x86_64")) { 13751c5aae77SAdrian Hunter u64 addr; 13761c5aae77SAdrian Hunter 13771c5aae77SAdrian Hunter /* 13781c5aae77SAdrian Hunter * If one of the corresponding symbols is there, assume the 13791c5aae77SAdrian Hunter * entry trampoline maps are too. 13801c5aae77SAdrian Hunter */ 13811c5aae77SAdrian Hunter if (!kallsyms__get_function_start(kallsyms_filename, 13821c5aae77SAdrian Hunter ENTRY_TRAMPOLINE_NAME, 13831c5aae77SAdrian Hunter &addr)) 13841c5aae77SAdrian Hunter machine->trampolines_mapped = true; 13851c5aae77SAdrian Hunter } 13861c5aae77SAdrian Hunter 13878e0cf965SAdrian Hunter /* 13888e0cf965SAdrian Hunter * Set the data type and long name so that kcore can be read via 13898e0cf965SAdrian Hunter * dso__data_read_addr(). 13908e0cf965SAdrian Hunter */ 13911c695c88SJiri Olsa if (dso->kernel == DSO_SPACE__KERNEL_GUEST) 13925f70619dSArnaldo Carvalho de Melo dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE; 13938e0cf965SAdrian Hunter else 13945f70619dSArnaldo Carvalho de Melo dso->binary_type = DSO_BINARY_TYPE__KCORE; 13957e155d4dSArnaldo Carvalho de Melo dso__set_long_name(dso, strdup(kcore_filename), true); 13968e0cf965SAdrian Hunter 13978e0cf965SAdrian Hunter close(fd); 13988e0cf965SAdrian Hunter 1399e9814df8SArnaldo Carvalho de Melo if (map->prot & PROT_EXEC) 14008e0cf965SAdrian Hunter pr_debug("Using %s for kernel object code\n", kcore_filename); 14018e0cf965SAdrian Hunter else 14028e0cf965SAdrian Hunter pr_debug("Using %s for kernel data\n", kcore_filename); 14038e0cf965SAdrian Hunter 14048e0cf965SAdrian Hunter return 0; 14058e0cf965SAdrian Hunter 14068e0cf965SAdrian Hunter out_err: 14078e0cf965SAdrian Hunter while (!list_empty(&md.maps)) { 14088e0cf965SAdrian Hunter map = list_entry(md.maps.next, struct map, node); 1409facf3f06SArnaldo Carvalho de Melo list_del_init(&map->node); 141084c2cafaSArnaldo Carvalho de Melo map__put(map); 14118e0cf965SAdrian Hunter } 14128e0cf965SAdrian Hunter close(fd); 14138e0cf965SAdrian Hunter return -EINVAL; 14148e0cf965SAdrian Hunter } 14158e0cf965SAdrian Hunter 1416d9b62abaSAdrian Hunter /* 1417d9b62abaSAdrian Hunter * If the kernel is relocated at boot time, kallsyms won't match. Compute the 1418d9b62abaSAdrian Hunter * delta based on the relocation reference symbol. 1419d9b62abaSAdrian Hunter */ 1420019c6820SArnaldo Carvalho de Melo static int kallsyms__delta(struct kmap *kmap, const char *filename, u64 *delta) 1421d9b62abaSAdrian Hunter { 1422d9b62abaSAdrian Hunter u64 addr; 1423d9b62abaSAdrian Hunter 1424d9b62abaSAdrian Hunter if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name) 1425d9b62abaSAdrian Hunter return 0; 1426d9b62abaSAdrian Hunter 1427b843f62aSArnaldo Carvalho de Melo if (kallsyms__get_function_start(filename, kmap->ref_reloc_sym->name, &addr)) 1428d9b62abaSAdrian Hunter return -1; 1429d9b62abaSAdrian Hunter 1430d9b62abaSAdrian Hunter *delta = addr - kmap->ref_reloc_sym->addr; 1431d9b62abaSAdrian Hunter return 0; 1432d9b62abaSAdrian Hunter } 1433d9b62abaSAdrian Hunter 1434e02092b9SArnaldo Carvalho de Melo int __dso__load_kallsyms(struct dso *dso, const char *filename, 1435be39db9fSArnaldo Carvalho de Melo struct map *map, bool no_kcore) 14362e538c4aSArnaldo Carvalho de Melo { 1437019c6820SArnaldo Carvalho de Melo struct kmap *kmap = map__kmap(map); 1438d9b62abaSAdrian Hunter u64 delta = 0; 1439d9b62abaSAdrian Hunter 1440ec80fde7SArnaldo Carvalho de Melo if (symbol__restricted_filename(filename, "/proc/kallsyms")) 1441ec80fde7SArnaldo Carvalho de Melo return -1; 1442ec80fde7SArnaldo Carvalho de Melo 1443019c6820SArnaldo Carvalho de Melo if (!kmap || !kmap->kmaps) 1444019c6820SArnaldo Carvalho de Melo return -1; 1445019c6820SArnaldo Carvalho de Melo 1446333cc76cSArnaldo Carvalho de Melo if (dso__load_all_kallsyms(dso, filename) < 0) 14472e538c4aSArnaldo Carvalho de Melo return -1; 14482e538c4aSArnaldo Carvalho de Melo 1449019c6820SArnaldo Carvalho de Melo if (kallsyms__delta(kmap, filename, &delta)) 1450d9b62abaSAdrian Hunter return -1; 1451d9b62abaSAdrian Hunter 14523183f8caSArnaldo Carvalho de Melo symbols__fixup_end(&dso->symbols); 14533183f8caSArnaldo Carvalho de Melo symbols__fixup_duplicate(&dso->symbols); 14543f5a4272SAnton Blanchard 14551c695c88SJiri Olsa if (dso->kernel == DSO_SPACE__KERNEL_GUEST) 145644f24cb3SJiri Olsa dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; 1457a1645ce1SZhang, Yanmin else 145844f24cb3SJiri Olsa dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS; 14592e538c4aSArnaldo Carvalho de Melo 1460e02092b9SArnaldo Carvalho de Melo if (!no_kcore && !dso__load_kcore(dso, map, filename)) 146179b6bb73SArnaldo Carvalho de Melo return maps__split_kallsyms_for_kcore(kmap->kmaps, dso); 14628e0cf965SAdrian Hunter else 146379b6bb73SArnaldo Carvalho de Melo return maps__split_kallsyms(kmap->kmaps, dso, delta, map); 1464af427bf5SArnaldo Carvalho de Melo } 1465af427bf5SArnaldo Carvalho de Melo 1466e02092b9SArnaldo Carvalho de Melo int dso__load_kallsyms(struct dso *dso, const char *filename, 1467be39db9fSArnaldo Carvalho de Melo struct map *map) 1468e02092b9SArnaldo Carvalho de Melo { 1469be39db9fSArnaldo Carvalho de Melo return __dso__load_kallsyms(dso, filename, map, false); 1470e02092b9SArnaldo Carvalho de Melo } 1471e02092b9SArnaldo Carvalho de Melo 14723183f8caSArnaldo Carvalho de Melo static int dso__load_perf_map(const char *map_path, struct dso *dso) 147380d496beSPekka Enberg { 147480d496beSPekka Enberg char *line = NULL; 147580d496beSPekka Enberg size_t n; 147680d496beSPekka Enberg FILE *file; 147780d496beSPekka Enberg int nr_syms = 0; 147880d496beSPekka Enberg 1479bf2e710bSKrister Johansen file = fopen(map_path, "r"); 148080d496beSPekka Enberg if (file == NULL) 148180d496beSPekka Enberg goto out_failure; 148280d496beSPekka Enberg 148380d496beSPekka Enberg while (!feof(file)) { 14849cffa8d5SPaul Mackerras u64 start, size; 148580d496beSPekka Enberg struct symbol *sym; 148680d496beSPekka Enberg int line_len, len; 148780d496beSPekka Enberg 148880d496beSPekka Enberg line_len = getline(&line, &n, file); 148980d496beSPekka Enberg if (line_len < 0) 149080d496beSPekka Enberg break; 149180d496beSPekka Enberg 149280d496beSPekka Enberg if (!line) 149380d496beSPekka Enberg goto out_failure; 149480d496beSPekka Enberg 149580d496beSPekka Enberg line[--line_len] = '\0'; /* \n */ 149680d496beSPekka Enberg 149780d496beSPekka Enberg len = hex2u64(line, &start); 149880d496beSPekka Enberg 149980d496beSPekka Enberg len++; 150080d496beSPekka Enberg if (len + 2 >= line_len) 150180d496beSPekka Enberg continue; 150280d496beSPekka Enberg 150380d496beSPekka Enberg len += hex2u64(line + len, &size); 150480d496beSPekka Enberg 150580d496beSPekka Enberg len++; 150680d496beSPekka Enberg if (len + 2 >= line_len) 150780d496beSPekka Enberg continue; 150880d496beSPekka Enberg 1509af30bffaSArnaldo Carvalho de Melo sym = symbol__new(start, size, STB_GLOBAL, STT_FUNC, line + len); 151080d496beSPekka Enberg 151180d496beSPekka Enberg if (sym == NULL) 151280d496beSPekka Enberg goto out_delete_line; 151380d496beSPekka Enberg 15143183f8caSArnaldo Carvalho de Melo symbols__insert(&dso->symbols, sym); 151580d496beSPekka Enberg nr_syms++; 151680d496beSPekka Enberg } 151780d496beSPekka Enberg 151880d496beSPekka Enberg free(line); 151980d496beSPekka Enberg fclose(file); 152080d496beSPekka Enberg 152180d496beSPekka Enberg return nr_syms; 152280d496beSPekka Enberg 152380d496beSPekka Enberg out_delete_line: 152480d496beSPekka Enberg free(line); 152580d496beSPekka Enberg out_failure: 152680d496beSPekka Enberg return -1; 152780d496beSPekka Enberg } 152880d496beSPekka Enberg 1529eac9a434SRemi Bernon #ifdef HAVE_LIBBFD_SUPPORT 1530eac9a434SRemi Bernon #define PACKAGE 'perf' 1531eac9a434SRemi Bernon #include <bfd.h> 1532eac9a434SRemi Bernon 1533eac9a434SRemi Bernon static int bfd_symbols__cmpvalue(const void *a, const void *b) 1534eac9a434SRemi Bernon { 1535eac9a434SRemi Bernon const asymbol *as = *(const asymbol **)a, *bs = *(const asymbol **)b; 1536eac9a434SRemi Bernon 1537eac9a434SRemi Bernon if (bfd_asymbol_value(as) != bfd_asymbol_value(bs)) 1538eac9a434SRemi Bernon return bfd_asymbol_value(as) - bfd_asymbol_value(bs); 1539eac9a434SRemi Bernon 1540eac9a434SRemi Bernon return bfd_asymbol_name(as)[0] - bfd_asymbol_name(bs)[0]; 1541eac9a434SRemi Bernon } 1542eac9a434SRemi Bernon 1543eac9a434SRemi Bernon static int bfd2elf_binding(asymbol *symbol) 1544eac9a434SRemi Bernon { 1545eac9a434SRemi Bernon if (symbol->flags & BSF_WEAK) 1546eac9a434SRemi Bernon return STB_WEAK; 1547eac9a434SRemi Bernon if (symbol->flags & BSF_GLOBAL) 1548eac9a434SRemi Bernon return STB_GLOBAL; 1549eac9a434SRemi Bernon if (symbol->flags & BSF_LOCAL) 1550eac9a434SRemi Bernon return STB_LOCAL; 1551eac9a434SRemi Bernon return -1; 1552eac9a434SRemi Bernon } 1553eac9a434SRemi Bernon 1554eac9a434SRemi Bernon int dso__load_bfd_symbols(struct dso *dso, const char *debugfile) 1555eac9a434SRemi Bernon { 1556eac9a434SRemi Bernon int err = -1; 1557eac9a434SRemi Bernon long symbols_size, symbols_count; 1558eac9a434SRemi Bernon asection *section; 1559eac9a434SRemi Bernon asymbol **symbols, *sym; 1560eac9a434SRemi Bernon struct symbol *symbol; 1561eac9a434SRemi Bernon bfd *abfd; 1562eac9a434SRemi Bernon u_int i; 1563eac9a434SRemi Bernon u64 start, len; 1564eac9a434SRemi Bernon 1565eac9a434SRemi Bernon abfd = bfd_openr(dso->long_name, NULL); 1566eac9a434SRemi Bernon if (!abfd) 1567eac9a434SRemi Bernon return -1; 1568eac9a434SRemi Bernon 1569eac9a434SRemi Bernon if (!bfd_check_format(abfd, bfd_object)) { 1570eac9a434SRemi Bernon pr_debug2("%s: cannot read %s bfd file.\n", __func__, 1571eac9a434SRemi Bernon dso->long_name); 1572eac9a434SRemi Bernon goto out_close; 1573eac9a434SRemi Bernon } 1574eac9a434SRemi Bernon 1575eac9a434SRemi Bernon if (bfd_get_flavour(abfd) == bfd_target_elf_flavour) 1576eac9a434SRemi Bernon goto out_close; 1577eac9a434SRemi Bernon 1578eac9a434SRemi Bernon section = bfd_get_section_by_name(abfd, ".text"); 1579eac9a434SRemi Bernon if (section) 1580eac9a434SRemi Bernon dso->text_offset = section->vma - section->filepos; 1581eac9a434SRemi Bernon 1582eac9a434SRemi Bernon bfd_close(abfd); 1583eac9a434SRemi Bernon 1584eac9a434SRemi Bernon abfd = bfd_openr(debugfile, NULL); 1585eac9a434SRemi Bernon if (!abfd) 1586eac9a434SRemi Bernon return -1; 1587eac9a434SRemi Bernon 1588eac9a434SRemi Bernon if (!bfd_check_format(abfd, bfd_object)) { 1589eac9a434SRemi Bernon pr_debug2("%s: cannot read %s bfd file.\n", __func__, 1590eac9a434SRemi Bernon debugfile); 1591eac9a434SRemi Bernon goto out_close; 1592eac9a434SRemi Bernon } 1593eac9a434SRemi Bernon 1594eac9a434SRemi Bernon if (bfd_get_flavour(abfd) == bfd_target_elf_flavour) 1595eac9a434SRemi Bernon goto out_close; 1596eac9a434SRemi Bernon 1597eac9a434SRemi Bernon symbols_size = bfd_get_symtab_upper_bound(abfd); 1598eac9a434SRemi Bernon if (symbols_size == 0) { 1599eac9a434SRemi Bernon bfd_close(abfd); 1600eac9a434SRemi Bernon return 0; 1601eac9a434SRemi Bernon } 1602eac9a434SRemi Bernon 1603eac9a434SRemi Bernon if (symbols_size < 0) 1604eac9a434SRemi Bernon goto out_close; 1605eac9a434SRemi Bernon 1606eac9a434SRemi Bernon symbols = malloc(symbols_size); 1607eac9a434SRemi Bernon if (!symbols) 1608eac9a434SRemi Bernon goto out_close; 1609eac9a434SRemi Bernon 1610eac9a434SRemi Bernon symbols_count = bfd_canonicalize_symtab(abfd, symbols); 1611eac9a434SRemi Bernon if (symbols_count < 0) 1612eac9a434SRemi Bernon goto out_free; 1613eac9a434SRemi Bernon 1614eac9a434SRemi Bernon qsort(symbols, symbols_count, sizeof(asymbol *), bfd_symbols__cmpvalue); 1615eac9a434SRemi Bernon 1616eac9a434SRemi Bernon #ifdef bfd_get_section 1617eac9a434SRemi Bernon #define bfd_asymbol_section bfd_get_section 1618eac9a434SRemi Bernon #endif 1619eac9a434SRemi Bernon for (i = 0; i < symbols_count; ++i) { 1620eac9a434SRemi Bernon sym = symbols[i]; 1621eac9a434SRemi Bernon section = bfd_asymbol_section(sym); 1622eac9a434SRemi Bernon if (bfd2elf_binding(sym) < 0) 1623eac9a434SRemi Bernon continue; 1624eac9a434SRemi Bernon 1625eac9a434SRemi Bernon while (i + 1 < symbols_count && 1626eac9a434SRemi Bernon bfd_asymbol_section(symbols[i + 1]) == section && 1627eac9a434SRemi Bernon bfd2elf_binding(symbols[i + 1]) < 0) 1628eac9a434SRemi Bernon i++; 1629eac9a434SRemi Bernon 1630eac9a434SRemi Bernon if (i + 1 < symbols_count && 1631eac9a434SRemi Bernon bfd_asymbol_section(symbols[i + 1]) == section) 1632eac9a434SRemi Bernon len = symbols[i + 1]->value - sym->value; 1633eac9a434SRemi Bernon else 1634eac9a434SRemi Bernon len = section->size - sym->value; 1635eac9a434SRemi Bernon 1636eac9a434SRemi Bernon start = bfd_asymbol_value(sym) - dso->text_offset; 1637eac9a434SRemi Bernon symbol = symbol__new(start, len, bfd2elf_binding(sym), STT_FUNC, 1638eac9a434SRemi Bernon bfd_asymbol_name(sym)); 1639eac9a434SRemi Bernon if (!symbol) 1640eac9a434SRemi Bernon goto out_free; 1641eac9a434SRemi Bernon 1642eac9a434SRemi Bernon symbols__insert(&dso->symbols, symbol); 1643eac9a434SRemi Bernon } 1644eac9a434SRemi Bernon #ifdef bfd_get_section 1645eac9a434SRemi Bernon #undef bfd_asymbol_section 1646eac9a434SRemi Bernon #endif 1647eac9a434SRemi Bernon 1648eac9a434SRemi Bernon symbols__fixup_end(&dso->symbols); 1649eac9a434SRemi Bernon symbols__fixup_duplicate(&dso->symbols); 1650eac9a434SRemi Bernon dso->adjust_symbols = 1; 1651eac9a434SRemi Bernon 1652eac9a434SRemi Bernon err = 0; 1653eac9a434SRemi Bernon out_free: 1654eac9a434SRemi Bernon free(symbols); 1655eac9a434SRemi Bernon out_close: 1656eac9a434SRemi Bernon bfd_close(abfd); 1657eac9a434SRemi Bernon return err; 1658eac9a434SRemi Bernon } 1659eac9a434SRemi Bernon #endif 1660eac9a434SRemi Bernon 16611029f9feSNamhyung Kim static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod, 16621029f9feSNamhyung Kim enum dso_binary_type type) 16631029f9feSNamhyung Kim { 16641029f9feSNamhyung Kim switch (type) { 16651029f9feSNamhyung Kim case DSO_BINARY_TYPE__JAVA_JIT: 16661029f9feSNamhyung Kim case DSO_BINARY_TYPE__DEBUGLINK: 16671029f9feSNamhyung Kim case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: 16681029f9feSNamhyung Kim case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: 16691029f9feSNamhyung Kim case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: 167085afd355SAdrian Hunter case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO: 16711029f9feSNamhyung Kim case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: 16721029f9feSNamhyung Kim case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: 16731c695c88SJiri Olsa return !kmod && dso->kernel == DSO_SPACE__USER; 16741029f9feSNamhyung Kim 16751029f9feSNamhyung Kim case DSO_BINARY_TYPE__KALLSYMS: 16761029f9feSNamhyung Kim case DSO_BINARY_TYPE__VMLINUX: 16771029f9feSNamhyung Kim case DSO_BINARY_TYPE__KCORE: 16781c695c88SJiri Olsa return dso->kernel == DSO_SPACE__KERNEL; 16791029f9feSNamhyung Kim 16801029f9feSNamhyung Kim case DSO_BINARY_TYPE__GUEST_KALLSYMS: 16811029f9feSNamhyung Kim case DSO_BINARY_TYPE__GUEST_VMLINUX: 16821029f9feSNamhyung Kim case DSO_BINARY_TYPE__GUEST_KCORE: 16831c695c88SJiri Olsa return dso->kernel == DSO_SPACE__KERNEL_GUEST; 16841029f9feSNamhyung Kim 16851029f9feSNamhyung Kim case DSO_BINARY_TYPE__GUEST_KMODULE: 1686c00c48fcSNamhyung Kim case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: 16871029f9feSNamhyung Kim case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: 1688c00c48fcSNamhyung Kim case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: 16891029f9feSNamhyung Kim /* 16901029f9feSNamhyung Kim * kernel modules know their symtab type - it's set when 1691a94ab91aSArnaldo Carvalho de Melo * creating a module dso in machine__addnew_module_map(). 16921029f9feSNamhyung Kim */ 16931029f9feSNamhyung Kim return kmod && dso->symtab_type == type; 16941029f9feSNamhyung Kim 16951029f9feSNamhyung Kim case DSO_BINARY_TYPE__BUILD_ID_CACHE: 1696d2396999SKrister Johansen case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: 16971029f9feSNamhyung Kim return true; 16981029f9feSNamhyung Kim 16999b86d04dSSong Liu case DSO_BINARY_TYPE__BPF_PROG_INFO: 17003c29d448SJiri Olsa case DSO_BINARY_TYPE__BPF_IMAGE: 1701789e2419SAdrian Hunter case DSO_BINARY_TYPE__OOL: 17021029f9feSNamhyung Kim case DSO_BINARY_TYPE__NOT_FOUND: 17031029f9feSNamhyung Kim default: 17041029f9feSNamhyung Kim return false; 17051029f9feSNamhyung Kim } 17061029f9feSNamhyung Kim } 17071029f9feSNamhyung Kim 1708bf2e710bSKrister Johansen /* Checks for the existence of the perf-<pid>.map file in two different 1709bf2e710bSKrister Johansen * locations. First, if the process is a separate mount namespace, check in 1710bf2e710bSKrister Johansen * that namespace using the pid of the innermost pid namespace. If's not in a 1711bf2e710bSKrister Johansen * namespace, or the file can't be found there, try in the mount namespace of 1712bf2e710bSKrister Johansen * the tracing process using our view of its pid. 1713bf2e710bSKrister Johansen */ 1714bf2e710bSKrister Johansen static int dso__find_perf_map(char *filebuf, size_t bufsz, 1715bf2e710bSKrister Johansen struct nsinfo **nsip) 1716bf2e710bSKrister Johansen { 1717bf2e710bSKrister Johansen struct nscookie nsc; 1718bf2e710bSKrister Johansen struct nsinfo *nsi; 1719bf2e710bSKrister Johansen struct nsinfo *nnsi; 1720bf2e710bSKrister Johansen int rc = -1; 1721bf2e710bSKrister Johansen 1722bf2e710bSKrister Johansen nsi = *nsip; 1723bf2e710bSKrister Johansen 1724bf2e710bSKrister Johansen if (nsi->need_setns) { 1725bf2e710bSKrister Johansen snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nsi->nstgid); 1726bf2e710bSKrister Johansen nsinfo__mountns_enter(nsi, &nsc); 1727bf2e710bSKrister Johansen rc = access(filebuf, R_OK); 1728bf2e710bSKrister Johansen nsinfo__mountns_exit(&nsc); 1729bf2e710bSKrister Johansen if (rc == 0) 1730bf2e710bSKrister Johansen return rc; 1731bf2e710bSKrister Johansen } 1732bf2e710bSKrister Johansen 1733bf2e710bSKrister Johansen nnsi = nsinfo__copy(nsi); 1734bf2e710bSKrister Johansen if (nnsi) { 1735bf2e710bSKrister Johansen nsinfo__put(nsi); 1736bf2e710bSKrister Johansen 1737bf2e710bSKrister Johansen nnsi->need_setns = false; 1738bf2e710bSKrister Johansen snprintf(filebuf, bufsz, "/tmp/perf-%d.map", nnsi->tgid); 1739bf2e710bSKrister Johansen *nsip = nnsi; 1740bf2e710bSKrister Johansen rc = 0; 1741bf2e710bSKrister Johansen } 1742bf2e710bSKrister Johansen 1743bf2e710bSKrister Johansen return rc; 1744bf2e710bSKrister Johansen } 1745bf2e710bSKrister Johansen 1746be39db9fSArnaldo Carvalho de Melo int dso__load(struct dso *dso, struct map *map) 174786470930SIngo Molnar { 1748c338aee8SArnaldo Carvalho de Melo char *name; 174986470930SIngo Molnar int ret = -1; 175044f24cb3SJiri Olsa u_int i; 175193fcce96SArnaldo Carvalho de Melo struct machine *machine = NULL; 175244f24cb3SJiri Olsa char *root_dir = (char *) ""; 17533aafe5aeSCody P Schafer int ss_pos = 0; 17543aafe5aeSCody P Schafer struct symsrc ss_[2]; 17553aafe5aeSCody P Schafer struct symsrc *syms_ss = NULL, *runtime_ss = NULL; 17561029f9feSNamhyung Kim bool kmod; 1757bf2e710bSKrister Johansen bool perfmap; 1758f766819cSJiri Olsa struct build_id bid; 1759843ff37bSKrister Johansen struct nscookie nsc; 1760bf2e710bSKrister Johansen char newmapname[PATH_MAX]; 1761bf2e710bSKrister Johansen const char *map_path = dso->long_name; 1762bf2e710bSKrister Johansen 1763bf2e710bSKrister Johansen perfmap = strncmp(dso->name, "/tmp/perf-", 10) == 0; 1764bf2e710bSKrister Johansen if (perfmap) { 1765bf2e710bSKrister Johansen if (dso->nsinfo && (dso__find_perf_map(newmapname, 1766bf2e710bSKrister Johansen sizeof(newmapname), &dso->nsinfo) == 0)) { 1767bf2e710bSKrister Johansen map_path = newmapname; 1768bf2e710bSKrister Johansen } 1769bf2e710bSKrister Johansen } 177086470930SIngo Molnar 1771843ff37bSKrister Johansen nsinfo__mountns_enter(dso->nsinfo, &nsc); 17724a936edcSNamhyung Kim pthread_mutex_lock(&dso->lock); 177366bd8424SArnaldo Carvalho de Melo 17744a936edcSNamhyung Kim /* check again under the dso->lock */ 17753183f8caSArnaldo Carvalho de Melo if (dso__loaded(dso)) { 17764a936edcSNamhyung Kim ret = 1; 17774a936edcSNamhyung Kim goto out; 17784a936edcSNamhyung Kim } 17794a936edcSNamhyung Kim 1780b5c09518SArnaldo Carvalho de Melo kmod = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || 1781b5c09518SArnaldo Carvalho de Melo dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP || 1782b5c09518SArnaldo Carvalho de Melo dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE || 1783b5c09518SArnaldo Carvalho de Melo dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; 1784b5c09518SArnaldo Carvalho de Melo 1785b5c09518SArnaldo Carvalho de Melo if (dso->kernel && !kmod) { 17861c695c88SJiri Olsa if (dso->kernel == DSO_SPACE__KERNEL) 1787be39db9fSArnaldo Carvalho de Melo ret = dso__load_kernel_sym(dso, map); 17881c695c88SJiri Olsa else if (dso->kernel == DSO_SPACE__KERNEL_GUEST) 1789be39db9fSArnaldo Carvalho de Melo ret = dso__load_guest_kernel_sym(dso, map); 17904a936edcSNamhyung Kim 179193fcce96SArnaldo Carvalho de Melo machine = map__kmaps(map)->machine; 17924d99e413SAdrian Hunter if (machine__is(machine, "x86_64")) 17934d99e413SAdrian Hunter machine__map_x86_64_entry_trampolines(machine, dso); 17944a936edcSNamhyung Kim goto out; 17954a936edcSNamhyung Kim } 1796a1645ce1SZhang, Yanmin 1797aeafcbafSArnaldo Carvalho de Melo dso->adjust_symbols = 0; 1798f5812a7aSArnaldo Carvalho de Melo 1799bf2e710bSKrister Johansen if (perfmap) { 18003183f8caSArnaldo Carvalho de Melo ret = dso__load_perf_map(map_path, dso); 180144f24cb3SJiri Olsa dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT : 180244f24cb3SJiri Olsa DSO_BINARY_TYPE__NOT_FOUND; 18034a936edcSNamhyung Kim goto out; 180494cb9e38SArnaldo Carvalho de Melo } 180594cb9e38SArnaldo Carvalho de Melo 180644f24cb3SJiri Olsa if (machine) 180744f24cb3SJiri Olsa root_dir = machine->root_dir; 180844f24cb3SJiri Olsa 1809164c800eSDavid Ahern name = malloc(PATH_MAX); 1810164c800eSDavid Ahern if (!name) 18114a936edcSNamhyung Kim goto out; 1812164c800eSDavid Ahern 18135baecbcdSDima Kogan /* 18145baecbcdSDima Kogan * Read the build id if possible. This is required for 18155baecbcdSDima Kogan * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work 18165baecbcdSDima Kogan */ 1817ed6c166cSKan Liang if (!dso->has_build_id && 18189b200653SVictor Kamensky is_regular_file(dso->long_name)) { 18199b200653SVictor Kamensky __symbol__join_symfs(name, PATH_MAX, dso->long_name); 1820f766819cSJiri Olsa if (filename__read_build_id(name, &bid) > 0) 1821*8dfdf440SJiri Olsa dso__set_build_id(dso, &bid); 18229b200653SVictor Kamensky } 18235baecbcdSDima Kogan 18241029f9feSNamhyung Kim /* 18251029f9feSNamhyung Kim * Iterate over candidate debug images. 18263aafe5aeSCody P Schafer * Keep track of "interesting" ones (those which have a symtab, dynsym, 18273aafe5aeSCody P Schafer * and/or opd section) for processing. 18286da80ce8SDave Martin */ 182944f24cb3SJiri Olsa for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) { 18303aafe5aeSCody P Schafer struct symsrc *ss = &ss_[ss_pos]; 18313aafe5aeSCody P Schafer bool next_slot = false; 1832f045b8c4SKrister Johansen bool is_reg; 1833d2396999SKrister Johansen bool nsexit; 1834eac9a434SRemi Bernon int bfdrc = -1; 1835c3962961SJiri Olsa int sirc = -1; 183644f24cb3SJiri Olsa 1837005f9294SCody P Schafer enum dso_binary_type symtab_type = binary_type_symtab[i]; 183844f24cb3SJiri Olsa 1839d2396999SKrister Johansen nsexit = (symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE || 1840d2396999SKrister Johansen symtab_type == DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO); 1841d2396999SKrister Johansen 18421029f9feSNamhyung Kim if (!dso__is_compatible_symtab_type(dso, kmod, symtab_type)) 18431029f9feSNamhyung Kim continue; 18441029f9feSNamhyung Kim 1845ee4e9625SArnaldo Carvalho de Melo if (dso__read_binary_type_filename(dso, symtab_type, 184644f24cb3SJiri Olsa root_dir, name, PATH_MAX)) 18476da80ce8SDave Martin continue; 184886470930SIngo Molnar 1849d2396999SKrister Johansen if (nsexit) 1850f045b8c4SKrister Johansen nsinfo__mountns_exit(&nsc); 185140356721SJiri Olsa 1852f045b8c4SKrister Johansen is_reg = is_regular_file(name); 1853eac9a434SRemi Bernon #ifdef HAVE_LIBBFD_SUPPORT 1854c3962961SJiri Olsa if (is_reg) 1855eac9a434SRemi Bernon bfdrc = dso__load_bfd_symbols(dso, name); 1856eac9a434SRemi Bernon #endif 1857eac9a434SRemi Bernon if (is_reg && bfdrc < 0) 1858f045b8c4SKrister Johansen sirc = symsrc__init(ss, dso, name, symtab_type); 1859f045b8c4SKrister Johansen 1860d2396999SKrister Johansen if (nsexit) 1861f045b8c4SKrister Johansen nsinfo__mountns_enter(dso->nsinfo, &nsc); 1862f045b8c4SKrister Johansen 1863eac9a434SRemi Bernon if (bfdrc == 0) 1864eac9a434SRemi Bernon break; 1865eac9a434SRemi Bernon 1866c3962961SJiri Olsa if (!is_reg || sirc < 0) 18676da80ce8SDave Martin continue; 18686da80ce8SDave Martin 18693aafe5aeSCody P Schafer if (!syms_ss && symsrc__has_symtab(ss)) { 18703aafe5aeSCody P Schafer syms_ss = ss; 18713aafe5aeSCody P Schafer next_slot = true; 18720058aef6SAdrian Hunter if (!dso->symsrc_filename) 18730058aef6SAdrian Hunter dso->symsrc_filename = strdup(name); 1874d26cd12bSCody P Schafer } 1875d26cd12bSCody P Schafer 18763aafe5aeSCody P Schafer if (!runtime_ss && symsrc__possibly_runtime(ss)) { 18773aafe5aeSCody P Schafer runtime_ss = ss; 18783aafe5aeSCody P Schafer next_slot = true; 1879a44f605bSCody P Schafer } 188086470930SIngo Molnar 18813aafe5aeSCody P Schafer if (next_slot) { 18823aafe5aeSCody P Schafer ss_pos++; 188333ff581eSJiri Olsa 18843aafe5aeSCody P Schafer if (syms_ss && runtime_ss) 18856da80ce8SDave Martin break; 188698e9f03bSNamhyung Kim } else { 188798e9f03bSNamhyung Kim symsrc__destroy(ss); 1888a25e46c4SArnaldo Carvalho de Melo } 18893aafe5aeSCody P Schafer 18906da80ce8SDave Martin } 18916da80ce8SDave Martin 18923aafe5aeSCody P Schafer if (!runtime_ss && !syms_ss) 18933aafe5aeSCody P Schafer goto out_free; 18943aafe5aeSCody P Schafer 18953aafe5aeSCody P Schafer if (runtime_ss && !syms_ss) { 18963aafe5aeSCody P Schafer syms_ss = runtime_ss; 189760e4b10cSArnaldo Carvalho de Melo } 189860e4b10cSArnaldo Carvalho de Melo 18993aafe5aeSCody P Schafer /* We'll have to hope for the best */ 19003aafe5aeSCody P Schafer if (!runtime_ss && syms_ss) 19013aafe5aeSCody P Schafer runtime_ss = syms_ss; 19023aafe5aeSCody P Schafer 19031029f9feSNamhyung Kim if (syms_ss) 1904be39db9fSArnaldo Carvalho de Melo ret = dso__load_sym(dso, map, syms_ss, runtime_ss, kmod); 19051029f9feSNamhyung Kim else 19063aafe5aeSCody P Schafer ret = -1; 19073aafe5aeSCody P Schafer 1908f47b58b7SDavid Ahern if (ret > 0) { 19093aafe5aeSCody P Schafer int nr_plt; 19103aafe5aeSCody P Schafer 19113183f8caSArnaldo Carvalho de Melo nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss); 19123aafe5aeSCody P Schafer if (nr_plt > 0) 19133aafe5aeSCody P Schafer ret += nr_plt; 19143aafe5aeSCody P Schafer } 19153aafe5aeSCody P Schafer 19163aafe5aeSCody P Schafer for (; ss_pos > 0; ss_pos--) 19173aafe5aeSCody P Schafer symsrc__destroy(&ss_[ss_pos - 1]); 19183aafe5aeSCody P Schafer out_free: 191986470930SIngo Molnar free(name); 1920aeafcbafSArnaldo Carvalho de Melo if (ret < 0 && strstr(dso->name, " (deleted)") != NULL) 19214a936edcSNamhyung Kim ret = 0; 19224a936edcSNamhyung Kim out: 19233183f8caSArnaldo Carvalho de Melo dso__set_loaded(dso); 19244a936edcSNamhyung Kim pthread_mutex_unlock(&dso->lock); 1925843ff37bSKrister Johansen nsinfo__mountns_exit(&nsc); 19264a936edcSNamhyung Kim 192786470930SIngo Molnar return ret; 192886470930SIngo Molnar } 192986470930SIngo Molnar 1930a7c2b572SArnaldo Carvalho de Melo static int map__strcmp(const void *a, const void *b) 1931a7c2b572SArnaldo Carvalho de Melo { 1932a7c2b572SArnaldo Carvalho de Melo const struct map *ma = *(const struct map **)a, *mb = *(const struct map **)b; 1933a7c2b572SArnaldo Carvalho de Melo return strcmp(ma->dso->short_name, mb->dso->short_name); 1934a7c2b572SArnaldo Carvalho de Melo } 1935a7c2b572SArnaldo Carvalho de Melo 1936a7c2b572SArnaldo Carvalho de Melo static int map__strcmp_name(const void *name, const void *b) 1937a7c2b572SArnaldo Carvalho de Melo { 1938a7c2b572SArnaldo Carvalho de Melo const struct map *map = *(const struct map **)b; 1939a7c2b572SArnaldo Carvalho de Melo return strcmp(name, map->dso->short_name); 1940a7c2b572SArnaldo Carvalho de Melo } 1941a7c2b572SArnaldo Carvalho de Melo 19429a29ceeeSArnaldo Carvalho de Melo void __maps__sort_by_name(struct maps *maps) 1943a7c2b572SArnaldo Carvalho de Melo { 19449a29ceeeSArnaldo Carvalho de Melo qsort(maps->maps_by_name, maps->nr_maps, sizeof(struct map *), map__strcmp); 1945a7c2b572SArnaldo Carvalho de Melo } 1946a7c2b572SArnaldo Carvalho de Melo 19479a29ceeeSArnaldo Carvalho de Melo static int map__groups__sort_by_name_from_rbtree(struct maps *maps) 1948a7c2b572SArnaldo Carvalho de Melo { 1949a7c2b572SArnaldo Carvalho de Melo struct map *map; 19509a29ceeeSArnaldo Carvalho de Melo struct map **maps_by_name = realloc(maps->maps_by_name, maps->nr_maps * sizeof(map)); 1951a7c2b572SArnaldo Carvalho de Melo int i = 0; 1952a7c2b572SArnaldo Carvalho de Melo 1953a7c2b572SArnaldo Carvalho de Melo if (maps_by_name == NULL) 1954a7c2b572SArnaldo Carvalho de Melo return -1; 1955a7c2b572SArnaldo Carvalho de Melo 19569a29ceeeSArnaldo Carvalho de Melo maps->maps_by_name = maps_by_name; 19579a29ceeeSArnaldo Carvalho de Melo maps->nr_maps_allocated = maps->nr_maps; 1958a7c2b572SArnaldo Carvalho de Melo 19599a29ceeeSArnaldo Carvalho de Melo maps__for_each_entry(maps, map) 1960a7c2b572SArnaldo Carvalho de Melo maps_by_name[i++] = map; 1961a7c2b572SArnaldo Carvalho de Melo 19629a29ceeeSArnaldo Carvalho de Melo __maps__sort_by_name(maps); 1963a7c2b572SArnaldo Carvalho de Melo return 0; 1964a7c2b572SArnaldo Carvalho de Melo } 1965a7c2b572SArnaldo Carvalho de Melo 19669a29ceeeSArnaldo Carvalho de Melo static struct map *__maps__find_by_name(struct maps *maps, const char *name) 1967a7c2b572SArnaldo Carvalho de Melo { 1968a7c2b572SArnaldo Carvalho de Melo struct map **mapp; 1969a7c2b572SArnaldo Carvalho de Melo 19709a29ceeeSArnaldo Carvalho de Melo if (maps->maps_by_name == NULL && 19719a29ceeeSArnaldo Carvalho de Melo map__groups__sort_by_name_from_rbtree(maps)) 1972a7c2b572SArnaldo Carvalho de Melo return NULL; 1973a7c2b572SArnaldo Carvalho de Melo 19749a29ceeeSArnaldo Carvalho de Melo mapp = bsearch(name, maps->maps_by_name, maps->nr_maps, sizeof(*mapp), map__strcmp_name); 1975a7c2b572SArnaldo Carvalho de Melo if (mapp) 1976a7c2b572SArnaldo Carvalho de Melo return *mapp; 1977a7c2b572SArnaldo Carvalho de Melo return NULL; 1978a7c2b572SArnaldo Carvalho de Melo } 1979a7c2b572SArnaldo Carvalho de Melo 19809a29ceeeSArnaldo Carvalho de Melo struct map *maps__find_by_name(struct maps *maps, const char *name) 1981439d473bSArnaldo Carvalho de Melo { 19824bb7123dSArnaldo Carvalho de Melo struct map *map; 1983439d473bSArnaldo Carvalho de Melo 19840a7c74eaSArnaldo Carvalho de Melo down_read(&maps->lock); 19856a2ffcddSArnaldo Carvalho de Melo 19869a29ceeeSArnaldo Carvalho de Melo if (maps->last_search_by_name && strcmp(maps->last_search_by_name->dso->short_name, name) == 0) { 19879a29ceeeSArnaldo Carvalho de Melo map = maps->last_search_by_name; 19886a2ffcddSArnaldo Carvalho de Melo goto out_unlock; 19891ae14516SArnaldo Carvalho de Melo } 1990a7c2b572SArnaldo Carvalho de Melo /* 19919a29ceeeSArnaldo Carvalho de Melo * If we have maps->maps_by_name, then the name isn't in the rbtree, 19929a29ceeeSArnaldo Carvalho de Melo * as maps->maps_by_name mirrors the rbtree when lookups by name are 1993a7c2b572SArnaldo Carvalho de Melo * made. 1994a7c2b572SArnaldo Carvalho de Melo */ 19959a29ceeeSArnaldo Carvalho de Melo map = __maps__find_by_name(maps, name); 19969a29ceeeSArnaldo Carvalho de Melo if (map || maps->maps_by_name != NULL) 1997a7c2b572SArnaldo Carvalho de Melo goto out_unlock; 19981ae14516SArnaldo Carvalho de Melo 1999a7c2b572SArnaldo Carvalho de Melo /* Fallback to traversing the rbtree... */ 20001ae14516SArnaldo Carvalho de Melo maps__for_each_entry(maps, map) 20011ae14516SArnaldo Carvalho de Melo if (strcmp(map->dso->short_name, name) == 0) { 20029a29ceeeSArnaldo Carvalho de Melo maps->last_search_by_name = map; 20031ae14516SArnaldo Carvalho de Melo goto out_unlock; 20041ae14516SArnaldo Carvalho de Melo } 2005439d473bSArnaldo Carvalho de Melo 20066a2ffcddSArnaldo Carvalho de Melo map = NULL; 20076a2ffcddSArnaldo Carvalho de Melo 20086a2ffcddSArnaldo Carvalho de Melo out_unlock: 20090a7c74eaSArnaldo Carvalho de Melo up_read(&maps->lock); 20106a2ffcddSArnaldo Carvalho de Melo return map; 2011439d473bSArnaldo Carvalho de Melo } 2012439d473bSArnaldo Carvalho de Melo 2013aeafcbafSArnaldo Carvalho de Melo int dso__load_vmlinux(struct dso *dso, struct map *map, 2014be39db9fSArnaldo Carvalho de Melo const char *vmlinux, bool vmlinux_allocated) 201586470930SIngo Molnar { 2016b68e2f91SCody P Schafer int err = -1; 2017b68e2f91SCody P Schafer struct symsrc ss; 2018ec5761eaSDavid Ahern char symfs_vmlinux[PATH_MAX]; 2019005f9294SCody P Schafer enum dso_binary_type symtab_type; 202086470930SIngo Molnar 20215698d2c9SNamhyung Kim if (vmlinux[0] == '/') 20225698d2c9SNamhyung Kim snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux); 20235698d2c9SNamhyung Kim else 2024972f393bSArnaldo Carvalho de Melo symbol__join_symfs(symfs_vmlinux, vmlinux); 202586470930SIngo Molnar 20261c695c88SJiri Olsa if (dso->kernel == DSO_SPACE__KERNEL_GUEST) 2027005f9294SCody P Schafer symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX; 202821ea4539SCody P Schafer else 2029005f9294SCody P Schafer symtab_type = DSO_BINARY_TYPE__VMLINUX; 203021ea4539SCody P Schafer 2031005f9294SCody P Schafer if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type)) 2032b68e2f91SCody P Schafer return -1; 2033b68e2f91SCody P Schafer 2034be39db9fSArnaldo Carvalho de Melo err = dso__load_sym(dso, map, &ss, &ss, 0); 2035b68e2f91SCody P Schafer symsrc__destroy(&ss); 203686470930SIngo Molnar 2037515850e4SCody P Schafer if (err > 0) { 20381c695c88SJiri Olsa if (dso->kernel == DSO_SPACE__KERNEL_GUEST) 20395f70619dSArnaldo Carvalho de Melo dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX; 204039b12f78SAdrian Hunter else 20415f70619dSArnaldo Carvalho de Melo dso->binary_type = DSO_BINARY_TYPE__VMLINUX; 2042bf4414aeSArnaldo Carvalho de Melo dso__set_long_name(dso, vmlinux, vmlinux_allocated); 20433183f8caSArnaldo Carvalho de Melo dso__set_loaded(dso); 2044ec5761eaSDavid Ahern pr_debug("Using %s for symbols\n", symfs_vmlinux); 2045515850e4SCody P Schafer } 20463846df2eSArnaldo Carvalho de Melo 204786470930SIngo Molnar return err; 204886470930SIngo Molnar } 204986470930SIngo Molnar 2050be39db9fSArnaldo Carvalho de Melo int dso__load_vmlinux_path(struct dso *dso, struct map *map) 2051a19afe46SArnaldo Carvalho de Melo { 2052a19afe46SArnaldo Carvalho de Melo int i, err = 0; 205300dc8657SNamhyung Kim char *filename = NULL; 2054a19afe46SArnaldo Carvalho de Melo 2055dc38218eSArnaldo Carvalho de Melo pr_debug("Looking at the vmlinux_path (%d entries long)\n", 2056dc38218eSArnaldo Carvalho de Melo vmlinux_path__nr_entries + 1); 2057dc38218eSArnaldo Carvalho de Melo 2058dc38218eSArnaldo Carvalho de Melo for (i = 0; i < vmlinux_path__nr_entries; ++i) { 2059be39db9fSArnaldo Carvalho de Melo err = dso__load_vmlinux(dso, map, vmlinux_path[i], false); 2060dc38218eSArnaldo Carvalho de Melo if (err > 0) 2061dc38218eSArnaldo Carvalho de Melo goto out; 2062dc38218eSArnaldo Carvalho de Melo } 2063dc38218eSArnaldo Carvalho de Melo 206400dc8657SNamhyung Kim if (!symbol_conf.ignore_vmlinux_buildid) 2065d2396999SKrister Johansen filename = dso__build_id_filename(dso, NULL, 0, false); 20665ad90e4eSArnaldo Carvalho de Melo if (filename != NULL) { 2067be39db9fSArnaldo Carvalho de Melo err = dso__load_vmlinux(dso, map, filename, true); 20685230fb7dSArnaldo Carvalho de Melo if (err > 0) 20695ad90e4eSArnaldo Carvalho de Melo goto out; 20705ad90e4eSArnaldo Carvalho de Melo free(filename); 20715ad90e4eSArnaldo Carvalho de Melo } 20725ad90e4eSArnaldo Carvalho de Melo out: 2073a19afe46SArnaldo Carvalho de Melo return err; 2074a19afe46SArnaldo Carvalho de Melo } 2075a19afe46SArnaldo Carvalho de Melo 2076c48903b8SMasami Hiramatsu static bool visible_dir_filter(const char *name, struct dirent *d) 2077c48903b8SMasami Hiramatsu { 2078c48903b8SMasami Hiramatsu if (d->d_type != DT_DIR) 2079c48903b8SMasami Hiramatsu return false; 2080c48903b8SMasami Hiramatsu return lsdir_no_dot_filter(name, d); 2081c48903b8SMasami Hiramatsu } 2082c48903b8SMasami Hiramatsu 20830544d422SAdrian Hunter static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz) 20840544d422SAdrian Hunter { 20850544d422SAdrian Hunter char kallsyms_filename[PATH_MAX]; 20860544d422SAdrian Hunter int ret = -1; 2087c48903b8SMasami Hiramatsu struct strlist *dirs; 2088c48903b8SMasami Hiramatsu struct str_node *nd; 20890544d422SAdrian Hunter 2090c48903b8SMasami Hiramatsu dirs = lsdir(dir, visible_dir_filter); 2091c48903b8SMasami Hiramatsu if (!dirs) 20920544d422SAdrian Hunter return -1; 20930544d422SAdrian Hunter 2094602a1f4dSArnaldo Carvalho de Melo strlist__for_each_entry(nd, dirs) { 20950544d422SAdrian Hunter scnprintf(kallsyms_filename, sizeof(kallsyms_filename), 2096c48903b8SMasami Hiramatsu "%s/%s/kallsyms", dir, nd->s); 2097a00d28cbSAdrian Hunter if (!validate_kcore_addresses(kallsyms_filename, map)) { 20980544d422SAdrian Hunter strlcpy(dir, kallsyms_filename, dir_sz); 20990544d422SAdrian Hunter ret = 0; 21000544d422SAdrian Hunter break; 21010544d422SAdrian Hunter } 21020544d422SAdrian Hunter } 21030544d422SAdrian Hunter 2104c48903b8SMasami Hiramatsu strlist__delete(dirs); 21050544d422SAdrian Hunter 21060544d422SAdrian Hunter return ret; 21070544d422SAdrian Hunter } 21080544d422SAdrian Hunter 210911870d71SMasami Hiramatsu /* 211011870d71SMasami Hiramatsu * Use open(O_RDONLY) to check readability directly instead of access(R_OK) 211111870d71SMasami Hiramatsu * since access(R_OK) only checks with real UID/GID but open() use effective 211211870d71SMasami Hiramatsu * UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO). 211311870d71SMasami Hiramatsu */ 211411870d71SMasami Hiramatsu static bool filename__readable(const char *file) 211511870d71SMasami Hiramatsu { 211611870d71SMasami Hiramatsu int fd = open(file, O_RDONLY); 211711870d71SMasami Hiramatsu if (fd < 0) 211811870d71SMasami Hiramatsu return false; 211911870d71SMasami Hiramatsu close(fd); 212011870d71SMasami Hiramatsu return true; 212111870d71SMasami Hiramatsu } 212211870d71SMasami Hiramatsu 21230544d422SAdrian Hunter static char *dso__find_kallsyms(struct dso *dso, struct map *map) 21240544d422SAdrian Hunter { 21253ff1b8c8SJiri Olsa struct build_id bid; 2126b5d8bbe8SMasami Hiramatsu char sbuild_id[SBUILD_ID_SIZE]; 21270544d422SAdrian Hunter bool is_host = false; 21280544d422SAdrian Hunter char path[PATH_MAX]; 21290544d422SAdrian Hunter 21300544d422SAdrian Hunter if (!dso->has_build_id) { 21310544d422SAdrian Hunter /* 21320544d422SAdrian Hunter * Last resort, if we don't have a build-id and couldn't find 21330544d422SAdrian Hunter * any vmlinux file, try the running kernel kallsyms table. 21340544d422SAdrian Hunter */ 21350544d422SAdrian Hunter goto proc_kallsyms; 21360544d422SAdrian Hunter } 21370544d422SAdrian Hunter 21383ff1b8c8SJiri Olsa if (sysfs__read_build_id("/sys/kernel/notes", &bid) == 0) 21393ff1b8c8SJiri Olsa is_host = dso__build_id_equal(dso, bid.data); 21400544d422SAdrian Hunter 21414e4b6c06SMasami Hiramatsu /* Try a fast path for /proc/kallsyms if possible */ 21420544d422SAdrian Hunter if (is_host) { 21430544d422SAdrian Hunter /* 214411870d71SMasami Hiramatsu * Do not check the build-id cache, unless we know we cannot use 214511870d71SMasami Hiramatsu * /proc/kcore or module maps don't match to /proc/kallsyms. 214611870d71SMasami Hiramatsu * To check readability of /proc/kcore, do not use access(R_OK) 214711870d71SMasami Hiramatsu * since /proc/kcore requires CAP_SYS_RAWIO to read and access 214811870d71SMasami Hiramatsu * can't check it. 21490544d422SAdrian Hunter */ 215011870d71SMasami Hiramatsu if (filename__readable("/proc/kcore") && 215111870d71SMasami Hiramatsu !validate_kcore_addresses("/proc/kallsyms", map)) 21520544d422SAdrian Hunter goto proc_kallsyms; 21530544d422SAdrian Hunter } 21540544d422SAdrian Hunter 2155bf541169SJiri Olsa build_id__sprintf(&dso->bid, sbuild_id); 21564e4b6c06SMasami Hiramatsu 2157449867e3SAdrian Hunter /* Find kallsyms in build-id cache with kcore */ 21584e4b6c06SMasami Hiramatsu scnprintf(path, sizeof(path), "%s/%s/%s", 21594e4b6c06SMasami Hiramatsu buildid_dir, DSO__NAME_KCORE, sbuild_id); 21604e4b6c06SMasami Hiramatsu 2161449867e3SAdrian Hunter if (!find_matching_kcore(map, path, sizeof(path))) 2162449867e3SAdrian Hunter return strdup(path); 2163449867e3SAdrian Hunter 21644e4b6c06SMasami Hiramatsu /* Use current /proc/kallsyms if possible */ 21654e4b6c06SMasami Hiramatsu if (is_host) { 21664e4b6c06SMasami Hiramatsu proc_kallsyms: 21674e4b6c06SMasami Hiramatsu return strdup("/proc/kallsyms"); 21684e4b6c06SMasami Hiramatsu } 21694e4b6c06SMasami Hiramatsu 21704e4b6c06SMasami Hiramatsu /* Finally, find a cache of kallsyms */ 217101412261SMasami Hiramatsu if (!build_id_cache__kallsyms_path(sbuild_id, path, sizeof(path))) { 21720544d422SAdrian Hunter pr_err("No kallsyms or vmlinux with build-id %s was found\n", 21730544d422SAdrian Hunter sbuild_id); 21740544d422SAdrian Hunter return NULL; 21750544d422SAdrian Hunter } 21760544d422SAdrian Hunter 21770544d422SAdrian Hunter return strdup(path); 21780544d422SAdrian Hunter } 21790544d422SAdrian Hunter 2180be39db9fSArnaldo Carvalho de Melo static int dso__load_kernel_sym(struct dso *dso, struct map *map) 218186470930SIngo Molnar { 2182cc612d81SArnaldo Carvalho de Melo int err; 21839e201442SArnaldo Carvalho de Melo const char *kallsyms_filename = NULL; 21849e201442SArnaldo Carvalho de Melo char *kallsyms_allocated_filename = NULL; 2185dc8d6ab2SArnaldo Carvalho de Melo /* 2186b226a5a7SDavid Ahern * Step 1: if the user specified a kallsyms or vmlinux filename, use 2187b226a5a7SDavid Ahern * it and only it, reporting errors to the user if it cannot be used. 2188dc8d6ab2SArnaldo Carvalho de Melo * 2189dc8d6ab2SArnaldo Carvalho de Melo * For instance, try to analyse an ARM perf.data file _without_ a 2190dc8d6ab2SArnaldo Carvalho de Melo * build-id, or if the user specifies the wrong path to the right 2191dc8d6ab2SArnaldo Carvalho de Melo * vmlinux file, obviously we can't fallback to another vmlinux (a 2192dc8d6ab2SArnaldo Carvalho de Melo * x86_86 one, on the machine where analysis is being performed, say), 2193dc8d6ab2SArnaldo Carvalho de Melo * or worse, /proc/kallsyms. 2194dc8d6ab2SArnaldo Carvalho de Melo * 2195dc8d6ab2SArnaldo Carvalho de Melo * If the specified file _has_ a build-id and there is a build-id 2196dc8d6ab2SArnaldo Carvalho de Melo * section in the perf.data file, we will still do the expected 2197dc8d6ab2SArnaldo Carvalho de Melo * validation in dso__load_vmlinux and will bail out if they don't 2198dc8d6ab2SArnaldo Carvalho de Melo * match. 2199dc8d6ab2SArnaldo Carvalho de Melo */ 2200b226a5a7SDavid Ahern if (symbol_conf.kallsyms_name != NULL) { 2201b226a5a7SDavid Ahern kallsyms_filename = symbol_conf.kallsyms_name; 2202b226a5a7SDavid Ahern goto do_kallsyms; 2203b226a5a7SDavid Ahern } 2204b226a5a7SDavid Ahern 2205fc2be696SWilly Tarreau if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) { 2206be39db9fSArnaldo Carvalho de Melo return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name, false); 2207dc8d6ab2SArnaldo Carvalho de Melo } 2208439d473bSArnaldo Carvalho de Melo 2209fc2be696SWilly Tarreau if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) { 2210be39db9fSArnaldo Carvalho de Melo err = dso__load_vmlinux_path(dso, map); 2211a19afe46SArnaldo Carvalho de Melo if (err > 0) 221239b12f78SAdrian Hunter return err; 2213cc612d81SArnaldo Carvalho de Melo } 2214cc612d81SArnaldo Carvalho de Melo 2215ec5761eaSDavid Ahern /* do not try local files if a symfs was given */ 2216ec5761eaSDavid Ahern if (symbol_conf.symfs[0] != 0) 2217ec5761eaSDavid Ahern return -1; 2218ec5761eaSDavid Ahern 22190544d422SAdrian Hunter kallsyms_allocated_filename = dso__find_kallsyms(dso, map); 22200544d422SAdrian Hunter if (!kallsyms_allocated_filename) 22218d0591f6SArnaldo Carvalho de Melo return -1; 22228d0591f6SArnaldo Carvalho de Melo 222319fc2dedSArnaldo Carvalho de Melo kallsyms_filename = kallsyms_allocated_filename; 222419fc2dedSArnaldo Carvalho de Melo 2225dc8d6ab2SArnaldo Carvalho de Melo do_kallsyms: 2226be39db9fSArnaldo Carvalho de Melo err = dso__load_kallsyms(dso, kallsyms_filename, map); 22273846df2eSArnaldo Carvalho de Melo if (err > 0) 22283846df2eSArnaldo Carvalho de Melo pr_debug("Using %s for symbols\n", kallsyms_filename); 2229dc8d6ab2SArnaldo Carvalho de Melo free(kallsyms_allocated_filename); 2230dc8d6ab2SArnaldo Carvalho de Melo 22318e0cf965SAdrian Hunter if (err > 0 && !dso__is_kcore(dso)) { 2232bdac0bcfSAdrian Hunter dso->binary_type = DSO_BINARY_TYPE__KALLSYMS; 22330a77582fSMasami Hiramatsu dso__set_long_name(dso, DSO__NAME_KALLSYMS, false); 22346a4694a4SArnaldo Carvalho de Melo map__fixup_start(map); 22356a4694a4SArnaldo Carvalho de Melo map__fixup_end(map); 2236439d473bSArnaldo Carvalho de Melo } 223794cb9e38SArnaldo Carvalho de Melo 223886470930SIngo Molnar return err; 223986470930SIngo Molnar } 224086470930SIngo Molnar 2241be39db9fSArnaldo Carvalho de Melo static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map) 2242a1645ce1SZhang, Yanmin { 2243a1645ce1SZhang, Yanmin int err; 2244a1645ce1SZhang, Yanmin const char *kallsyms_filename = NULL; 224593fcce96SArnaldo Carvalho de Melo struct machine *machine = map__kmaps(map)->machine; 2246a1645ce1SZhang, Yanmin char path[PATH_MAX]; 2247a1645ce1SZhang, Yanmin 224823346f21SArnaldo Carvalho de Melo if (machine__is_default_guest(machine)) { 2249a1645ce1SZhang, Yanmin /* 2250a1645ce1SZhang, Yanmin * if the user specified a vmlinux filename, use it and only 2251a1645ce1SZhang, Yanmin * it, reporting errors to the user if it cannot be used. 2252a1645ce1SZhang, Yanmin * Or use file guest_kallsyms inputted by user on commandline 2253a1645ce1SZhang, Yanmin */ 2254a1645ce1SZhang, Yanmin if (symbol_conf.default_guest_vmlinux_name != NULL) { 2255aeafcbafSArnaldo Carvalho de Melo err = dso__load_vmlinux(dso, map, 22565230fb7dSArnaldo Carvalho de Melo symbol_conf.default_guest_vmlinux_name, 2257be39db9fSArnaldo Carvalho de Melo false); 225839b12f78SAdrian Hunter return err; 2259a1645ce1SZhang, Yanmin } 2260a1645ce1SZhang, Yanmin 2261a1645ce1SZhang, Yanmin kallsyms_filename = symbol_conf.default_guest_kallsyms; 2262a1645ce1SZhang, Yanmin if (!kallsyms_filename) 2263a1645ce1SZhang, Yanmin return -1; 2264a1645ce1SZhang, Yanmin } else { 226523346f21SArnaldo Carvalho de Melo sprintf(path, "%s/proc/kallsyms", machine->root_dir); 2266a1645ce1SZhang, Yanmin kallsyms_filename = path; 2267a1645ce1SZhang, Yanmin } 2268a1645ce1SZhang, Yanmin 2269be39db9fSArnaldo Carvalho de Melo err = dso__load_kallsyms(dso, kallsyms_filename, map); 22708e0cf965SAdrian Hunter if (err > 0) 227139b12f78SAdrian Hunter pr_debug("Using %s for symbols\n", kallsyms_filename); 22728e0cf965SAdrian Hunter if (err > 0 && !dso__is_kcore(dso)) { 2273bdac0bcfSAdrian Hunter dso->binary_type = DSO_BINARY_TYPE__GUEST_KALLSYMS; 22748c7f1bb3SJiri Olsa dso__set_long_name(dso, machine->mmap_name, false); 2275a1645ce1SZhang, Yanmin map__fixup_start(map); 2276a1645ce1SZhang, Yanmin map__fixup_end(map); 2277a1645ce1SZhang, Yanmin } 2278a1645ce1SZhang, Yanmin 2279a1645ce1SZhang, Yanmin return err; 2280a1645ce1SZhang, Yanmin } 2281cd84c2acSFrederic Weisbecker 2282cc612d81SArnaldo Carvalho de Melo static void vmlinux_path__exit(void) 22832446042cSArnaldo Carvalho de Melo { 228404662523SArnaldo Carvalho de Melo while (--vmlinux_path__nr_entries >= 0) 228504662523SArnaldo Carvalho de Melo zfree(&vmlinux_path[vmlinux_path__nr_entries]); 2286c4f03547SWang Nan vmlinux_path__nr_entries = 0; 2287cc612d81SArnaldo Carvalho de Melo 228804662523SArnaldo Carvalho de Melo zfree(&vmlinux_path); 2289cc612d81SArnaldo Carvalho de Melo } 2290cc612d81SArnaldo Carvalho de Melo 2291aac48647SEkaterina Tumanova static const char * const vmlinux_paths[] = { 2292aac48647SEkaterina Tumanova "vmlinux", 2293aac48647SEkaterina Tumanova "/boot/vmlinux" 2294aac48647SEkaterina Tumanova }; 2295aac48647SEkaterina Tumanova 2296aac48647SEkaterina Tumanova static const char * const vmlinux_paths_upd[] = { 2297aac48647SEkaterina Tumanova "/boot/vmlinux-%s", 2298aac48647SEkaterina Tumanova "/usr/lib/debug/boot/vmlinux-%s", 2299aac48647SEkaterina Tumanova "/lib/modules/%s/build/vmlinux", 2300f55ae954SEkaterina Tumanova "/usr/lib/debug/lib/modules/%s/vmlinux", 2301f55ae954SEkaterina Tumanova "/usr/lib/debug/boot/vmlinux-%s.debug" 2302aac48647SEkaterina Tumanova }; 2303aac48647SEkaterina Tumanova 2304aac48647SEkaterina Tumanova static int vmlinux_path__add(const char *new_entry) 2305aac48647SEkaterina Tumanova { 2306aac48647SEkaterina Tumanova vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry); 2307aac48647SEkaterina Tumanova if (vmlinux_path[vmlinux_path__nr_entries] == NULL) 2308aac48647SEkaterina Tumanova return -1; 2309aac48647SEkaterina Tumanova ++vmlinux_path__nr_entries; 2310aac48647SEkaterina Tumanova 2311aac48647SEkaterina Tumanova return 0; 2312aac48647SEkaterina Tumanova } 2313aac48647SEkaterina Tumanova 2314ce80d3beSKan Liang static int vmlinux_path__init(struct perf_env *env) 2315cc612d81SArnaldo Carvalho de Melo { 2316cc612d81SArnaldo Carvalho de Melo struct utsname uts; 2317cc612d81SArnaldo Carvalho de Melo char bf[PATH_MAX]; 23180a7e6d1bSNamhyung Kim char *kernel_version; 2319aac48647SEkaterina Tumanova unsigned int i; 2320cc612d81SArnaldo Carvalho de Melo 2321aac48647SEkaterina Tumanova vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) + 2322aac48647SEkaterina Tumanova ARRAY_SIZE(vmlinux_paths_upd))); 2323cc612d81SArnaldo Carvalho de Melo if (vmlinux_path == NULL) 2324cc612d81SArnaldo Carvalho de Melo return -1; 2325cc612d81SArnaldo Carvalho de Melo 2326aac48647SEkaterina Tumanova for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++) 2327aac48647SEkaterina Tumanova if (vmlinux_path__add(vmlinux_paths[i]) < 0) 2328cc612d81SArnaldo Carvalho de Melo goto out_fail; 2329ec5761eaSDavid Ahern 23300a7e6d1bSNamhyung Kim /* only try kernel version if no symfs was given */ 2331ec5761eaSDavid Ahern if (symbol_conf.symfs[0] != 0) 2332ec5761eaSDavid Ahern return 0; 2333ec5761eaSDavid Ahern 23340a7e6d1bSNamhyung Kim if (env) { 23350a7e6d1bSNamhyung Kim kernel_version = env->os_release; 23360a7e6d1bSNamhyung Kim } else { 2337ec5761eaSDavid Ahern if (uname(&uts) < 0) 2338e96c674fSNamhyung Kim goto out_fail; 2339ec5761eaSDavid Ahern 23400a7e6d1bSNamhyung Kim kernel_version = uts.release; 23410a7e6d1bSNamhyung Kim } 23420a7e6d1bSNamhyung Kim 2343aac48647SEkaterina Tumanova for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) { 2344aac48647SEkaterina Tumanova snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version); 2345aac48647SEkaterina Tumanova if (vmlinux_path__add(bf) < 0) 2346cc612d81SArnaldo Carvalho de Melo goto out_fail; 2347aac48647SEkaterina Tumanova } 2348cc612d81SArnaldo Carvalho de Melo 2349cc612d81SArnaldo Carvalho de Melo return 0; 2350cc612d81SArnaldo Carvalho de Melo 2351cc612d81SArnaldo Carvalho de Melo out_fail: 2352cc612d81SArnaldo Carvalho de Melo vmlinux_path__exit(); 2353cc612d81SArnaldo Carvalho de Melo return -1; 2354cc612d81SArnaldo Carvalho de Melo } 2355cc612d81SArnaldo Carvalho de Melo 23563bfe5f81SDavid Ahern int setup_list(struct strlist **list, const char *list_str, 2357655000e7SArnaldo Carvalho de Melo const char *list_name) 2358655000e7SArnaldo Carvalho de Melo { 2359655000e7SArnaldo Carvalho de Melo if (list_str == NULL) 2360655000e7SArnaldo Carvalho de Melo return 0; 2361655000e7SArnaldo Carvalho de Melo 23624a77e218SArnaldo Carvalho de Melo *list = strlist__new(list_str, NULL); 2363655000e7SArnaldo Carvalho de Melo if (!*list) { 2364655000e7SArnaldo Carvalho de Melo pr_err("problems parsing %s list\n", list_name); 2365655000e7SArnaldo Carvalho de Melo return -1; 2366655000e7SArnaldo Carvalho de Melo } 23670bc2f2f7SArnaldo Carvalho de Melo 23680bc2f2f7SArnaldo Carvalho de Melo symbol_conf.has_filter = true; 2369655000e7SArnaldo Carvalho de Melo return 0; 2370655000e7SArnaldo Carvalho de Melo } 2371655000e7SArnaldo Carvalho de Melo 2372e03eaa40SDavid Ahern int setup_intlist(struct intlist **list, const char *list_str, 2373e03eaa40SDavid Ahern const char *list_name) 2374e03eaa40SDavid Ahern { 2375e03eaa40SDavid Ahern if (list_str == NULL) 2376e03eaa40SDavid Ahern return 0; 2377e03eaa40SDavid Ahern 2378e03eaa40SDavid Ahern *list = intlist__new(list_str); 2379e03eaa40SDavid Ahern if (!*list) { 2380e03eaa40SDavid Ahern pr_err("problems parsing %s list\n", list_name); 2381e03eaa40SDavid Ahern return -1; 2382e03eaa40SDavid Ahern } 2383e03eaa40SDavid Ahern return 0; 2384e03eaa40SDavid Ahern } 2385e03eaa40SDavid Ahern 2386ec80fde7SArnaldo Carvalho de Melo static bool symbol__read_kptr_restrict(void) 2387ec80fde7SArnaldo Carvalho de Melo { 2388ec80fde7SArnaldo Carvalho de Melo bool value = false; 2389ec80fde7SArnaldo Carvalho de Melo FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r"); 239038272dc4SWang Nan 2391ec80fde7SArnaldo Carvalho de Melo if (fp != NULL) { 2392ec80fde7SArnaldo Carvalho de Melo char line[8]; 2393ec80fde7SArnaldo Carvalho de Melo 2394ec80fde7SArnaldo Carvalho de Melo if (fgets(line, sizeof(line), fp) != NULL) 23958859aedeSIgor Lubashev value = perf_cap__capable(CAP_SYSLOG) ? 23968859aedeSIgor Lubashev (atoi(line) >= 2) : 23978859aedeSIgor Lubashev (atoi(line) != 0); 2398ec80fde7SArnaldo Carvalho de Melo 2399ec80fde7SArnaldo Carvalho de Melo fclose(fp); 2400ec80fde7SArnaldo Carvalho de Melo } 2401ec80fde7SArnaldo Carvalho de Melo 24028859aedeSIgor Lubashev /* Per kernel/kallsyms.c: 24038859aedeSIgor Lubashev * we also restrict when perf_event_paranoid > 1 w/o CAP_SYSLOG 24048859aedeSIgor Lubashev */ 24058859aedeSIgor Lubashev if (perf_event_paranoid() > 1 && !perf_cap__capable(CAP_SYSLOG)) 24068859aedeSIgor Lubashev value = true; 24078859aedeSIgor Lubashev 2408ec80fde7SArnaldo Carvalho de Melo return value; 2409ec80fde7SArnaldo Carvalho de Melo } 2410ec80fde7SArnaldo Carvalho de Melo 2411b01141f4SArnaldo Carvalho de Melo int symbol__annotation_init(void) 2412b01141f4SArnaldo Carvalho de Melo { 24137b366142SArnaldo Carvalho de Melo if (symbol_conf.init_annotation) 24147b366142SArnaldo Carvalho de Melo return 0; 24157b366142SArnaldo Carvalho de Melo 2416b01141f4SArnaldo Carvalho de Melo if (symbol_conf.initialized) { 2417b01141f4SArnaldo Carvalho de Melo pr_err("Annotation needs to be init before symbol__init()\n"); 2418b01141f4SArnaldo Carvalho de Melo return -1; 2419b01141f4SArnaldo Carvalho de Melo } 2420b01141f4SArnaldo Carvalho de Melo 2421b01141f4SArnaldo Carvalho de Melo symbol_conf.priv_size += sizeof(struct annotation); 2422b01141f4SArnaldo Carvalho de Melo symbol_conf.init_annotation = true; 2423b01141f4SArnaldo Carvalho de Melo return 0; 2424b01141f4SArnaldo Carvalho de Melo } 2425b01141f4SArnaldo Carvalho de Melo 2426ce80d3beSKan Liang int symbol__init(struct perf_env *env) 2427cc612d81SArnaldo Carvalho de Melo { 2428ec5761eaSDavid Ahern const char *symfs; 2429ec5761eaSDavid Ahern 243085e00b55SJovi Zhang if (symbol_conf.initialized) 243185e00b55SJovi Zhang return 0; 243285e00b55SJovi Zhang 24339ac3e487SIrina Tirdea symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64)); 24344d439517SDavid S. Miller 2435166ccc9cSNamhyung Kim symbol__elf_init(); 2436166ccc9cSNamhyung Kim 243775be6cf4SArnaldo Carvalho de Melo if (symbol_conf.sort_by_name) 243875be6cf4SArnaldo Carvalho de Melo symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) - 243979406cd7SArnaldo Carvalho de Melo sizeof(struct symbol)); 2440b32d133aSArnaldo Carvalho de Melo 24410a7e6d1bSNamhyung Kim if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0) 2442cc612d81SArnaldo Carvalho de Melo return -1; 2443cc612d81SArnaldo Carvalho de Melo 2444c410a338SArnaldo Carvalho de Melo if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') { 2445c410a338SArnaldo Carvalho de Melo pr_err("'.' is the only non valid --field-separator argument\n"); 2446c410a338SArnaldo Carvalho de Melo return -1; 2447c410a338SArnaldo Carvalho de Melo } 2448c410a338SArnaldo Carvalho de Melo 2449655000e7SArnaldo Carvalho de Melo if (setup_list(&symbol_conf.dso_list, 2450655000e7SArnaldo Carvalho de Melo symbol_conf.dso_list_str, "dso") < 0) 2451655000e7SArnaldo Carvalho de Melo return -1; 2452655000e7SArnaldo Carvalho de Melo 2453655000e7SArnaldo Carvalho de Melo if (setup_list(&symbol_conf.comm_list, 2454655000e7SArnaldo Carvalho de Melo symbol_conf.comm_list_str, "comm") < 0) 2455655000e7SArnaldo Carvalho de Melo goto out_free_dso_list; 2456655000e7SArnaldo Carvalho de Melo 2457e03eaa40SDavid Ahern if (setup_intlist(&symbol_conf.pid_list, 2458e03eaa40SDavid Ahern symbol_conf.pid_list_str, "pid") < 0) 2459e03eaa40SDavid Ahern goto out_free_comm_list; 2460e03eaa40SDavid Ahern 2461e03eaa40SDavid Ahern if (setup_intlist(&symbol_conf.tid_list, 2462e03eaa40SDavid Ahern symbol_conf.tid_list_str, "tid") < 0) 2463e03eaa40SDavid Ahern goto out_free_pid_list; 2464e03eaa40SDavid Ahern 2465655000e7SArnaldo Carvalho de Melo if (setup_list(&symbol_conf.sym_list, 2466655000e7SArnaldo Carvalho de Melo symbol_conf.sym_list_str, "symbol") < 0) 2467e03eaa40SDavid Ahern goto out_free_tid_list; 2468655000e7SArnaldo Carvalho de Melo 246964eff7d9SDavid Ahern if (setup_list(&symbol_conf.bt_stop_list, 247064eff7d9SDavid Ahern symbol_conf.bt_stop_list_str, "symbol") < 0) 247164eff7d9SDavid Ahern goto out_free_sym_list; 247264eff7d9SDavid Ahern 2473ec5761eaSDavid Ahern /* 2474ec5761eaSDavid Ahern * A path to symbols of "/" is identical to "" 2475ec5761eaSDavid Ahern * reset here for simplicity. 2476ec5761eaSDavid Ahern */ 2477ec5761eaSDavid Ahern symfs = realpath(symbol_conf.symfs, NULL); 2478ec5761eaSDavid Ahern if (symfs == NULL) 2479ec5761eaSDavid Ahern symfs = symbol_conf.symfs; 2480ec5761eaSDavid Ahern if (strcmp(symfs, "/") == 0) 2481ec5761eaSDavid Ahern symbol_conf.symfs = ""; 2482ec5761eaSDavid Ahern if (symfs != symbol_conf.symfs) 2483ec5761eaSDavid Ahern free((void *)symfs); 2484ec5761eaSDavid Ahern 2485ec80fde7SArnaldo Carvalho de Melo symbol_conf.kptr_restrict = symbol__read_kptr_restrict(); 2486ec80fde7SArnaldo Carvalho de Melo 248785e00b55SJovi Zhang symbol_conf.initialized = true; 24884aa65636SArnaldo Carvalho de Melo return 0; 2489655000e7SArnaldo Carvalho de Melo 249064eff7d9SDavid Ahern out_free_sym_list: 249164eff7d9SDavid Ahern strlist__delete(symbol_conf.sym_list); 2492e03eaa40SDavid Ahern out_free_tid_list: 2493e03eaa40SDavid Ahern intlist__delete(symbol_conf.tid_list); 2494e03eaa40SDavid Ahern out_free_pid_list: 2495e03eaa40SDavid Ahern intlist__delete(symbol_conf.pid_list); 2496655000e7SArnaldo Carvalho de Melo out_free_comm_list: 2497655000e7SArnaldo Carvalho de Melo strlist__delete(symbol_conf.comm_list); 2498d74c896bSNamhyung Kim out_free_dso_list: 2499d74c896bSNamhyung Kim strlist__delete(symbol_conf.dso_list); 2500655000e7SArnaldo Carvalho de Melo return -1; 2501cc612d81SArnaldo Carvalho de Melo } 2502cc612d81SArnaldo Carvalho de Melo 2503d65a458bSArnaldo Carvalho de Melo void symbol__exit(void) 2504d65a458bSArnaldo Carvalho de Melo { 250585e00b55SJovi Zhang if (!symbol_conf.initialized) 250685e00b55SJovi Zhang return; 250764eff7d9SDavid Ahern strlist__delete(symbol_conf.bt_stop_list); 2508d65a458bSArnaldo Carvalho de Melo strlist__delete(symbol_conf.sym_list); 2509d65a458bSArnaldo Carvalho de Melo strlist__delete(symbol_conf.dso_list); 2510d65a458bSArnaldo Carvalho de Melo strlist__delete(symbol_conf.comm_list); 2511e03eaa40SDavid Ahern intlist__delete(symbol_conf.tid_list); 2512e03eaa40SDavid Ahern intlist__delete(symbol_conf.pid_list); 2513d65a458bSArnaldo Carvalho de Melo vmlinux_path__exit(); 2514d65a458bSArnaldo Carvalho de Melo symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL; 251564eff7d9SDavid Ahern symbol_conf.bt_stop_list = NULL; 251685e00b55SJovi Zhang symbol_conf.initialized = false; 2517d65a458bSArnaldo Carvalho de Melo } 2518a7066709SHe Kuang 2519a7066709SHe Kuang int symbol__config_symfs(const struct option *opt __maybe_unused, 2520a7066709SHe Kuang const char *dir, int unset __maybe_unused) 2521a7066709SHe Kuang { 2522a7066709SHe Kuang char *bf = NULL; 2523a7066709SHe Kuang int ret; 2524a7066709SHe Kuang 2525a7066709SHe Kuang symbol_conf.symfs = strdup(dir); 2526a7066709SHe Kuang if (symbol_conf.symfs == NULL) 2527a7066709SHe Kuang return -ENOMEM; 2528a7066709SHe Kuang 2529a7066709SHe Kuang /* skip the locally configured cache if a symfs is given, and 2530a7066709SHe Kuang * config buildid dir to symfs/.debug 2531a7066709SHe Kuang */ 2532a7066709SHe Kuang ret = asprintf(&bf, "%s/%s", dir, ".debug"); 2533a7066709SHe Kuang if (ret < 0) 2534a7066709SHe Kuang return -ENOMEM; 2535a7066709SHe Kuang 2536a7066709SHe Kuang set_buildid_dir(bf); 2537a7066709SHe Kuang 2538a7066709SHe Kuang free(bf); 2539a7066709SHe Kuang return 0; 2540a7066709SHe Kuang } 25419f87498fSJiri Olsa 25429f87498fSJiri Olsa struct mem_info *mem_info__get(struct mem_info *mi) 25439f87498fSJiri Olsa { 25449f87498fSJiri Olsa if (mi) 25459f87498fSJiri Olsa refcount_inc(&mi->refcnt); 25469f87498fSJiri Olsa return mi; 25479f87498fSJiri Olsa } 25489f87498fSJiri Olsa 25499f87498fSJiri Olsa void mem_info__put(struct mem_info *mi) 25509f87498fSJiri Olsa { 25519f87498fSJiri Olsa if (mi && refcount_dec_and_test(&mi->refcnt)) 25529f87498fSJiri Olsa free(mi); 25539f87498fSJiri Olsa } 25549f87498fSJiri Olsa 25559f87498fSJiri Olsa struct mem_info *mem_info__new(void) 25569f87498fSJiri Olsa { 25579f87498fSJiri Olsa struct mem_info *mi = zalloc(sizeof(*mi)); 25589f87498fSJiri Olsa 25599f87498fSJiri Olsa if (mi) 25609f87498fSJiri Olsa refcount_set(&mi->refcnt, 1); 25619f87498fSJiri Olsa return mi; 25629f87498fSJiri Olsa } 2563