symbol.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include "util.h"
  2. #include "../perf.h"
  3. #include "string.h"
  4. #include "symbol.h"
  5. #include <libelf.h>
  6. #include <gelf.h>
  7. #include <elf.h>
  8. static struct symbol *symbol__new(uint64_t start, uint64_t len,
  9. const char *name, unsigned int priv_size)
  10. {
  11. size_t namelen = strlen(name) + 1;
  12. struct symbol *self = malloc(priv_size + sizeof(*self) + namelen);
  13. if (self != NULL) {
  14. if (priv_size) {
  15. memset(self, 0, priv_size);
  16. self = ((void *)self) + priv_size;
  17. }
  18. self->start = start;
  19. self->end = start + len;
  20. memcpy(self->name, name, namelen);
  21. }
  22. return self;
  23. }
  24. static void symbol__delete(struct symbol *self, unsigned int priv_size)
  25. {
  26. free(((void *)self) - priv_size);
  27. }
  28. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  29. {
  30. return fprintf(fp, " %llx-%llx %s\n",
  31. self->start, self->end, self->name);
  32. }
  33. struct dso *dso__new(const char *name, unsigned int sym_priv_size)
  34. {
  35. struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
  36. if (self != NULL) {
  37. strcpy(self->name, name);
  38. self->syms = RB_ROOT;
  39. self->sym_priv_size = sym_priv_size;
  40. }
  41. return self;
  42. }
  43. static void dso__delete_symbols(struct dso *self)
  44. {
  45. struct symbol *pos;
  46. struct rb_node *next = rb_first(&self->syms);
  47. while (next) {
  48. pos = rb_entry(next, struct symbol, rb_node);
  49. next = rb_next(&pos->rb_node);
  50. rb_erase(&pos->rb_node, &self->syms);
  51. symbol__delete(pos, self->sym_priv_size);
  52. }
  53. }
  54. void dso__delete(struct dso *self)
  55. {
  56. dso__delete_symbols(self);
  57. free(self);
  58. }
  59. static void dso__insert_symbol(struct dso *self, struct symbol *sym)
  60. {
  61. struct rb_node **p = &self->syms.rb_node;
  62. struct rb_node *parent = NULL;
  63. const uint64_t ip = sym->start;
  64. struct symbol *s;
  65. while (*p != NULL) {
  66. parent = *p;
  67. s = rb_entry(parent, struct symbol, rb_node);
  68. if (ip < s->start)
  69. p = &(*p)->rb_left;
  70. else
  71. p = &(*p)->rb_right;
  72. }
  73. rb_link_node(&sym->rb_node, parent, p);
  74. rb_insert_color(&sym->rb_node, &self->syms);
  75. }
  76. struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
  77. {
  78. struct rb_node *n;
  79. if (self == NULL)
  80. return NULL;
  81. n = self->syms.rb_node;
  82. while (n) {
  83. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  84. if (ip < s->start)
  85. n = n->rb_left;
  86. else if (ip > s->end)
  87. n = n->rb_right;
  88. else
  89. return s;
  90. }
  91. return NULL;
  92. }
  93. size_t dso__fprintf(struct dso *self, FILE *fp)
  94. {
  95. size_t ret = fprintf(fp, "dso: %s\n", self->name);
  96. struct rb_node *nd;
  97. for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
  98. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  99. ret += symbol__fprintf(pos, fp);
  100. }
  101. return ret;
  102. }
  103. static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter)
  104. {
  105. struct rb_node *nd, *prevnd;
  106. char *line = NULL;
  107. size_t n;
  108. FILE *file = fopen("/proc/kallsyms", "r");
  109. if (file == NULL)
  110. goto out_failure;
  111. while (!feof(file)) {
  112. __u64 start;
  113. struct symbol *sym;
  114. int line_len, len;
  115. char symbol_type;
  116. line_len = getline(&line, &n, file);
  117. if (line_len < 0)
  118. break;
  119. if (!line)
  120. goto out_failure;
  121. line[--line_len] = '\0'; /* \n */
  122. len = hex2u64(line, &start);
  123. len++;
  124. if (len + 2 >= line_len)
  125. continue;
  126. symbol_type = toupper(line[len]);
  127. /*
  128. * We're interested only in code ('T'ext)
  129. */
  130. if (symbol_type != 'T' && symbol_type != 'W')
  131. continue;
  132. /*
  133. * Well fix up the end later, when we have all sorted.
  134. */
  135. sym = symbol__new(start, 0xdead, line + len + 2,
  136. self->sym_priv_size);
  137. if (sym == NULL)
  138. goto out_delete_line;
  139. if (filter && filter(self, sym))
  140. symbol__delete(sym, self->sym_priv_size);
  141. else
  142. dso__insert_symbol(self, sym);
  143. }
  144. /*
  145. * Now that we have all sorted out, just set the ->end of all
  146. * symbols
  147. */
  148. prevnd = rb_first(&self->syms);
  149. if (prevnd == NULL)
  150. goto out_delete_line;
  151. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  152. struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
  153. *curr = rb_entry(nd, struct symbol, rb_node);
  154. prev->end = curr->start - 1;
  155. prevnd = nd;
  156. }
  157. free(line);
  158. fclose(file);
  159. return 0;
  160. out_delete_line:
  161. free(line);
  162. out_failure:
  163. return -1;
  164. }
  165. /**
  166. * elf_symtab__for_each_symbol - iterate thru all the symbols
  167. *
  168. * @self: struct elf_symtab instance to iterate
  169. * @index: uint32_t index
  170. * @sym: GElf_Sym iterator
  171. */
  172. #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
  173. for (index = 0, gelf_getsym(syms, index, &sym);\
  174. index < nr_syms; \
  175. index++, gelf_getsym(syms, index, &sym))
  176. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  177. {
  178. return GELF_ST_TYPE(sym->st_info);
  179. }
  180. static inline int elf_sym__is_function(const GElf_Sym *sym)
  181. {
  182. return elf_sym__type(sym) == STT_FUNC &&
  183. sym->st_name != 0 &&
  184. sym->st_shndx != SHN_UNDEF &&
  185. sym->st_size != 0;
  186. }
  187. static inline const char *elf_sym__name(const GElf_Sym *sym,
  188. const Elf_Data *symstrs)
  189. {
  190. return symstrs->d_buf + sym->st_name;
  191. }
  192. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  193. GElf_Shdr *shp, const char *name,
  194. size_t *index)
  195. {
  196. Elf_Scn *sec = NULL;
  197. size_t cnt = 1;
  198. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  199. char *str;
  200. gelf_getshdr(sec, shp);
  201. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  202. if (!strcmp(name, str)) {
  203. if (index)
  204. *index = cnt;
  205. break;
  206. }
  207. ++cnt;
  208. }
  209. return sec;
  210. }
  211. static int dso__load_sym(struct dso *self, int fd, const char *name,
  212. symbol_filter_t filter)
  213. {
  214. Elf_Data *symstrs;
  215. uint32_t nr_syms;
  216. int err = -1;
  217. uint32_t index;
  218. GElf_Ehdr ehdr;
  219. GElf_Shdr shdr;
  220. Elf_Data *syms;
  221. GElf_Sym sym;
  222. Elf_Scn *sec;
  223. Elf *elf;
  224. int nr = 0;
  225. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  226. if (elf == NULL) {
  227. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  228. __func__, name);
  229. goto out_close;
  230. }
  231. if (gelf_getehdr(elf, &ehdr) == NULL) {
  232. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  233. goto out_elf_end;
  234. }
  235. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  236. if (sec == NULL)
  237. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  238. if (sec == NULL)
  239. goto out_elf_end;
  240. syms = elf_getdata(sec, NULL);
  241. if (syms == NULL)
  242. goto out_elf_end;
  243. sec = elf_getscn(elf, shdr.sh_link);
  244. if (sec == NULL)
  245. goto out_elf_end;
  246. symstrs = elf_getdata(sec, NULL);
  247. if (symstrs == NULL)
  248. goto out_elf_end;
  249. nr_syms = shdr.sh_size / shdr.sh_entsize;
  250. elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
  251. struct symbol *f;
  252. if (!elf_sym__is_function(&sym))
  253. continue;
  254. sec = elf_getscn(elf, sym.st_shndx);
  255. if (!sec)
  256. goto out_elf_end;
  257. gelf_getshdr(sec, &shdr);
  258. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  259. f = symbol__new(sym.st_value, sym.st_size,
  260. elf_sym__name(&sym, symstrs),
  261. self->sym_priv_size);
  262. if (!f)
  263. goto out_elf_end;
  264. if (filter && filter(self, f))
  265. symbol__delete(f, self->sym_priv_size);
  266. else {
  267. dso__insert_symbol(self, f);
  268. nr++;
  269. }
  270. }
  271. err = nr;
  272. out_elf_end:
  273. elf_end(elf);
  274. out_close:
  275. return err;
  276. }
  277. int dso__load(struct dso *self, symbol_filter_t filter)
  278. {
  279. int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
  280. char *name = malloc(size);
  281. int variant = 0;
  282. int ret = -1;
  283. int fd;
  284. if (!name)
  285. return -1;
  286. more:
  287. do {
  288. switch (variant) {
  289. case 0: /* Fedora */
  290. snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
  291. break;
  292. case 1: /* Ubuntu */
  293. snprintf(name, size, "/usr/lib/debug%s", self->name);
  294. break;
  295. case 2: /* Sane people */
  296. snprintf(name, size, "%s", self->name);
  297. break;
  298. default:
  299. goto out;
  300. }
  301. variant++;
  302. fd = open(name, O_RDONLY);
  303. } while (fd < 0);
  304. ret = dso__load_sym(self, fd, name, filter);
  305. close(fd);
  306. /*
  307. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  308. */
  309. if (!ret)
  310. goto more;
  311. out:
  312. free(name);
  313. return ret;
  314. }
  315. static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
  316. symbol_filter_t filter)
  317. {
  318. int err, fd = open(vmlinux, O_RDONLY);
  319. if (fd < 0)
  320. return -1;
  321. err = dso__load_sym(self, fd, vmlinux, filter);
  322. close(fd);
  323. return err;
  324. }
  325. int dso__load_kernel(struct dso *self, const char *vmlinux, symbol_filter_t filter)
  326. {
  327. int err = -1;
  328. if (vmlinux)
  329. err = dso__load_vmlinux(self, vmlinux, filter);
  330. if (err)
  331. err = dso__load_kallsyms(self, filter);
  332. return err;
  333. }
  334. void symbol__init(void)
  335. {
  336. elf_version(EV_CURRENT);
  337. }