symbol.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. #include "util.h"
  2. #include "../perf.h"
  3. #include "string.h"
  4. #include "symbol.h"
  5. #include "thread.h"
  6. #include "debug.h"
  7. #include <libelf.h>
  8. #include <gelf.h>
  9. #include <elf.h>
  10. #include <sys/utsname.h>
  11. const char *sym_hist_filter;
  12. enum dso_origin {
  13. DSO__ORIG_KERNEL = 0,
  14. DSO__ORIG_JAVA_JIT,
  15. DSO__ORIG_FEDORA,
  16. DSO__ORIG_UBUNTU,
  17. DSO__ORIG_BUILDID,
  18. DSO__ORIG_DSO,
  19. DSO__ORIG_KMODULE,
  20. DSO__ORIG_NOT_FOUND,
  21. };
  22. static void dsos__add(struct dso *dso);
  23. static struct dso *dsos__find(const char *name);
  24. static struct map *map__new2(u64 start, struct dso *dso);
  25. static void kernel_maps__insert(struct map *map);
  26. static struct rb_root kernel_maps;
  27. static void dso__fixup_sym_end(struct dso *self)
  28. {
  29. struct rb_node *nd, *prevnd = rb_first(&self->syms);
  30. struct symbol *curr, *prev;
  31. if (prevnd == NULL)
  32. return;
  33. curr = rb_entry(prevnd, struct symbol, rb_node);
  34. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  35. prev = curr;
  36. curr = rb_entry(nd, struct symbol, rb_node);
  37. if (prev->end == prev->start)
  38. prev->end = curr->start - 1;
  39. }
  40. /* Last entry */
  41. if (curr->end == curr->start)
  42. curr->end = roundup(curr->start, 4096);
  43. }
  44. static void kernel_maps__fixup_end(void)
  45. {
  46. struct map *prev, *curr;
  47. struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
  48. if (prevnd == NULL)
  49. return;
  50. curr = rb_entry(prevnd, struct map, rb_node);
  51. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  52. prev = curr;
  53. curr = rb_entry(nd, struct map, rb_node);
  54. prev->end = curr->start - 1;
  55. }
  56. nd = rb_last(&curr->dso->syms);
  57. if (nd) {
  58. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  59. curr->end = sym->end;
  60. }
  61. }
  62. static struct symbol *symbol__new(u64 start, u64 len, const char *name,
  63. unsigned int priv_size, int v)
  64. {
  65. size_t namelen = strlen(name) + 1;
  66. struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
  67. if (!self)
  68. return NULL;
  69. if (v > 2)
  70. printf("new symbol: %016Lx [%08lx]: %s, hist: %p\n",
  71. start, (unsigned long)len, name, self->hist);
  72. self->hist = NULL;
  73. self->hist_sum = 0;
  74. if (sym_hist_filter && !strcmp(name, sym_hist_filter))
  75. self->hist = calloc(sizeof(u64), len);
  76. if (priv_size) {
  77. memset(self, 0, priv_size);
  78. self = ((void *)self) + priv_size;
  79. }
  80. self->start = start;
  81. self->end = len ? start + len - 1 : start;
  82. memcpy(self->name, name, namelen);
  83. return self;
  84. }
  85. static void symbol__delete(struct symbol *self, unsigned int priv_size)
  86. {
  87. free(((void *)self) - priv_size);
  88. }
  89. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  90. {
  91. return fprintf(fp, " %llx-%llx %s\n",
  92. self->start, self->end, self->name);
  93. }
  94. struct dso *dso__new(const char *name, unsigned int sym_priv_size)
  95. {
  96. struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
  97. if (self != NULL) {
  98. strcpy(self->name, name);
  99. self->long_name = self->name;
  100. self->short_name = self->name;
  101. self->syms = RB_ROOT;
  102. self->sym_priv_size = sym_priv_size;
  103. self->find_symbol = dso__find_symbol;
  104. self->slen_calculated = 0;
  105. self->origin = DSO__ORIG_NOT_FOUND;
  106. }
  107. return self;
  108. }
  109. static void dso__delete_symbols(struct dso *self)
  110. {
  111. struct symbol *pos;
  112. struct rb_node *next = rb_first(&self->syms);
  113. while (next) {
  114. pos = rb_entry(next, struct symbol, rb_node);
  115. next = rb_next(&pos->rb_node);
  116. rb_erase(&pos->rb_node, &self->syms);
  117. symbol__delete(pos, self->sym_priv_size);
  118. }
  119. }
  120. void dso__delete(struct dso *self)
  121. {
  122. dso__delete_symbols(self);
  123. if (self->long_name != self->name)
  124. free(self->long_name);
  125. free(self);
  126. }
  127. static void dso__insert_symbol(struct dso *self, struct symbol *sym)
  128. {
  129. struct rb_node **p = &self->syms.rb_node;
  130. struct rb_node *parent = NULL;
  131. const u64 ip = sym->start;
  132. struct symbol *s;
  133. while (*p != NULL) {
  134. parent = *p;
  135. s = rb_entry(parent, struct symbol, rb_node);
  136. if (ip < s->start)
  137. p = &(*p)->rb_left;
  138. else
  139. p = &(*p)->rb_right;
  140. }
  141. rb_link_node(&sym->rb_node, parent, p);
  142. rb_insert_color(&sym->rb_node, &self->syms);
  143. }
  144. struct symbol *dso__find_symbol(struct dso *self, u64 ip)
  145. {
  146. struct rb_node *n;
  147. if (self == NULL)
  148. return NULL;
  149. n = self->syms.rb_node;
  150. while (n) {
  151. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  152. if (ip < s->start)
  153. n = n->rb_left;
  154. else if (ip > s->end)
  155. n = n->rb_right;
  156. else
  157. return s;
  158. }
  159. return NULL;
  160. }
  161. size_t dso__fprintf(struct dso *self, FILE *fp)
  162. {
  163. size_t ret = fprintf(fp, "dso: %s\n", self->short_name);
  164. struct rb_node *nd;
  165. for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
  166. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  167. ret += symbol__fprintf(pos, fp);
  168. }
  169. return ret;
  170. }
  171. /*
  172. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  173. * so that we can in the next step set the symbol ->end address and then
  174. * call kernel_maps__split_kallsyms.
  175. */
  176. static int kernel_maps__load_all_kallsyms(int v)
  177. {
  178. char *line = NULL;
  179. size_t n;
  180. FILE *file = fopen("/proc/kallsyms", "r");
  181. if (file == NULL)
  182. goto out_failure;
  183. while (!feof(file)) {
  184. u64 start;
  185. struct symbol *sym;
  186. int line_len, len;
  187. char symbol_type;
  188. char *symbol_name;
  189. line_len = getline(&line, &n, file);
  190. if (line_len < 0)
  191. break;
  192. if (!line)
  193. goto out_failure;
  194. line[--line_len] = '\0'; /* \n */
  195. len = hex2u64(line, &start);
  196. len++;
  197. if (len + 2 >= line_len)
  198. continue;
  199. symbol_type = toupper(line[len]);
  200. /*
  201. * We're interested only in code ('T'ext)
  202. */
  203. if (symbol_type != 'T' && symbol_type != 'W')
  204. continue;
  205. symbol_name = line + len + 2;
  206. /*
  207. * Will fix up the end later, when we have all symbols sorted.
  208. */
  209. sym = symbol__new(start, 0, symbol_name,
  210. kernel_map->dso->sym_priv_size, v);
  211. if (sym == NULL)
  212. goto out_delete_line;
  213. dso__insert_symbol(kernel_map->dso, sym);
  214. }
  215. free(line);
  216. fclose(file);
  217. return 0;
  218. out_delete_line:
  219. free(line);
  220. out_failure:
  221. return -1;
  222. }
  223. /*
  224. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  225. * kernel range is broken in several maps, named [kernel].N, as we don't have
  226. * the original ELF section names vmlinux have.
  227. */
  228. static int kernel_maps__split_kallsyms(symbol_filter_t filter, int use_modules)
  229. {
  230. struct map *map = kernel_map;
  231. struct symbol *pos;
  232. int count = 0;
  233. struct rb_node *next = rb_first(&kernel_map->dso->syms);
  234. int kernel_range = 0;
  235. while (next) {
  236. char *module;
  237. pos = rb_entry(next, struct symbol, rb_node);
  238. next = rb_next(&pos->rb_node);
  239. module = strchr(pos->name, '\t');
  240. if (module) {
  241. if (!use_modules)
  242. goto delete_symbol;
  243. *module++ = '\0';
  244. if (strcmp(map->dso->name, module)) {
  245. map = kernel_maps__find_by_dso_name(module);
  246. if (!map) {
  247. fputs("/proc/{kallsyms,modules} "
  248. "inconsistency!\n", stderr);
  249. return -1;
  250. }
  251. }
  252. /*
  253. * So that we look just like we get from .ko files,
  254. * i.e. not prelinked, relative to map->start.
  255. */
  256. pos->start = map->map_ip(map, pos->start);
  257. pos->end = map->map_ip(map, pos->end);
  258. } else if (map != kernel_map) {
  259. char dso_name[PATH_MAX];
  260. struct dso *dso;
  261. snprintf(dso_name, sizeof(dso_name), "[kernel].%d",
  262. kernel_range++);
  263. dso = dso__new(dso_name,
  264. kernel_map->dso->sym_priv_size);
  265. if (dso == NULL)
  266. return -1;
  267. map = map__new2(pos->start, dso);
  268. if (map == NULL) {
  269. dso__delete(dso);
  270. return -1;
  271. }
  272. map->map_ip = vdso__map_ip;
  273. kernel_maps__insert(map);
  274. ++kernel_range;
  275. }
  276. if (filter && filter(map, pos)) {
  277. delete_symbol:
  278. rb_erase(&pos->rb_node, &kernel_map->dso->syms);
  279. symbol__delete(pos, kernel_map->dso->sym_priv_size);
  280. } else {
  281. if (map != kernel_map) {
  282. rb_erase(&pos->rb_node, &kernel_map->dso->syms);
  283. dso__insert_symbol(map->dso, pos);
  284. }
  285. count++;
  286. }
  287. }
  288. return count;
  289. }
  290. static int kernel_maps__load_kallsyms(symbol_filter_t filter,
  291. int use_modules, int v)
  292. {
  293. if (kernel_maps__load_all_kallsyms(v))
  294. return -1;
  295. dso__fixup_sym_end(kernel_map->dso);
  296. return kernel_maps__split_kallsyms(filter, use_modules);
  297. }
  298. static size_t kernel_maps__fprintf(FILE *fp, int v)
  299. {
  300. size_t printed = fprintf(stderr, "Kernel maps:\n");
  301. struct rb_node *nd;
  302. for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
  303. struct map *pos = rb_entry(nd, struct map, rb_node);
  304. printed += fprintf(fp, "Map:");
  305. printed += map__fprintf(pos, fp);
  306. if (v > 1) {
  307. printed += dso__fprintf(pos->dso, fp);
  308. printed += fprintf(fp, "--\n");
  309. }
  310. }
  311. return printed + fprintf(stderr, "END kernel maps\n");
  312. }
  313. static int dso__load_perf_map(struct dso *self, struct map *map,
  314. symbol_filter_t filter, int v)
  315. {
  316. char *line = NULL;
  317. size_t n;
  318. FILE *file;
  319. int nr_syms = 0;
  320. file = fopen(self->long_name, "r");
  321. if (file == NULL)
  322. goto out_failure;
  323. while (!feof(file)) {
  324. u64 start, size;
  325. struct symbol *sym;
  326. int line_len, len;
  327. line_len = getline(&line, &n, file);
  328. if (line_len < 0)
  329. break;
  330. if (!line)
  331. goto out_failure;
  332. line[--line_len] = '\0'; /* \n */
  333. len = hex2u64(line, &start);
  334. len++;
  335. if (len + 2 >= line_len)
  336. continue;
  337. len += hex2u64(line + len, &size);
  338. len++;
  339. if (len + 2 >= line_len)
  340. continue;
  341. sym = symbol__new(start, size, line + len,
  342. self->sym_priv_size, v);
  343. if (sym == NULL)
  344. goto out_delete_line;
  345. if (filter && filter(map, sym))
  346. symbol__delete(sym, self->sym_priv_size);
  347. else {
  348. dso__insert_symbol(self, sym);
  349. nr_syms++;
  350. }
  351. }
  352. free(line);
  353. fclose(file);
  354. return nr_syms;
  355. out_delete_line:
  356. free(line);
  357. out_failure:
  358. return -1;
  359. }
  360. /**
  361. * elf_symtab__for_each_symbol - iterate thru all the symbols
  362. *
  363. * @self: struct elf_symtab instance to iterate
  364. * @idx: uint32_t idx
  365. * @sym: GElf_Sym iterator
  366. */
  367. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  368. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  369. idx < nr_syms; \
  370. idx++, gelf_getsym(syms, idx, &sym))
  371. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  372. {
  373. return GELF_ST_TYPE(sym->st_info);
  374. }
  375. static inline int elf_sym__is_function(const GElf_Sym *sym)
  376. {
  377. return elf_sym__type(sym) == STT_FUNC &&
  378. sym->st_name != 0 &&
  379. sym->st_shndx != SHN_UNDEF;
  380. }
  381. static inline int elf_sym__is_label(const GElf_Sym *sym)
  382. {
  383. return elf_sym__type(sym) == STT_NOTYPE &&
  384. sym->st_name != 0 &&
  385. sym->st_shndx != SHN_UNDEF &&
  386. sym->st_shndx != SHN_ABS;
  387. }
  388. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  389. const Elf_Data *secstrs)
  390. {
  391. return secstrs->d_buf + shdr->sh_name;
  392. }
  393. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  394. const Elf_Data *secstrs)
  395. {
  396. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  397. }
  398. static inline const char *elf_sym__name(const GElf_Sym *sym,
  399. const Elf_Data *symstrs)
  400. {
  401. return symstrs->d_buf + sym->st_name;
  402. }
  403. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  404. GElf_Shdr *shp, const char *name,
  405. size_t *idx)
  406. {
  407. Elf_Scn *sec = NULL;
  408. size_t cnt = 1;
  409. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  410. char *str;
  411. gelf_getshdr(sec, shp);
  412. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  413. if (!strcmp(name, str)) {
  414. if (idx)
  415. *idx = cnt;
  416. break;
  417. }
  418. ++cnt;
  419. }
  420. return sec;
  421. }
  422. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  423. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  424. idx < nr_entries; \
  425. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  426. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  427. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  428. idx < nr_entries; \
  429. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  430. /*
  431. * We need to check if we have a .dynsym, so that we can handle the
  432. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  433. * .dynsym or .symtab).
  434. * And always look at the original dso, not at debuginfo packages, that
  435. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  436. */
  437. static int dso__synthesize_plt_symbols(struct dso *self, int v)
  438. {
  439. uint32_t nr_rel_entries, idx;
  440. GElf_Sym sym;
  441. u64 plt_offset;
  442. GElf_Shdr shdr_plt;
  443. struct symbol *f;
  444. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  445. Elf_Data *reldata, *syms, *symstrs;
  446. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  447. size_t dynsym_idx;
  448. GElf_Ehdr ehdr;
  449. char sympltname[1024];
  450. Elf *elf;
  451. int nr = 0, symidx, fd, err = 0;
  452. fd = open(self->long_name, O_RDONLY);
  453. if (fd < 0)
  454. goto out;
  455. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  456. if (elf == NULL)
  457. goto out_close;
  458. if (gelf_getehdr(elf, &ehdr) == NULL)
  459. goto out_elf_end;
  460. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  461. ".dynsym", &dynsym_idx);
  462. if (scn_dynsym == NULL)
  463. goto out_elf_end;
  464. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  465. ".rela.plt", NULL);
  466. if (scn_plt_rel == NULL) {
  467. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  468. ".rel.plt", NULL);
  469. if (scn_plt_rel == NULL)
  470. goto out_elf_end;
  471. }
  472. err = -1;
  473. if (shdr_rel_plt.sh_link != dynsym_idx)
  474. goto out_elf_end;
  475. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  476. goto out_elf_end;
  477. /*
  478. * Fetch the relocation section to find the idxes to the GOT
  479. * and the symbols in the .dynsym they refer to.
  480. */
  481. reldata = elf_getdata(scn_plt_rel, NULL);
  482. if (reldata == NULL)
  483. goto out_elf_end;
  484. syms = elf_getdata(scn_dynsym, NULL);
  485. if (syms == NULL)
  486. goto out_elf_end;
  487. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  488. if (scn_symstrs == NULL)
  489. goto out_elf_end;
  490. symstrs = elf_getdata(scn_symstrs, NULL);
  491. if (symstrs == NULL)
  492. goto out_elf_end;
  493. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  494. plt_offset = shdr_plt.sh_offset;
  495. if (shdr_rel_plt.sh_type == SHT_RELA) {
  496. GElf_Rela pos_mem, *pos;
  497. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  498. nr_rel_entries) {
  499. symidx = GELF_R_SYM(pos->r_info);
  500. plt_offset += shdr_plt.sh_entsize;
  501. gelf_getsym(syms, symidx, &sym);
  502. snprintf(sympltname, sizeof(sympltname),
  503. "%s@plt", elf_sym__name(&sym, symstrs));
  504. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  505. sympltname, self->sym_priv_size, v);
  506. if (!f)
  507. goto out_elf_end;
  508. dso__insert_symbol(self, f);
  509. ++nr;
  510. }
  511. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  512. GElf_Rel pos_mem, *pos;
  513. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  514. nr_rel_entries) {
  515. symidx = GELF_R_SYM(pos->r_info);
  516. plt_offset += shdr_plt.sh_entsize;
  517. gelf_getsym(syms, symidx, &sym);
  518. snprintf(sympltname, sizeof(sympltname),
  519. "%s@plt", elf_sym__name(&sym, symstrs));
  520. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  521. sympltname, self->sym_priv_size, v);
  522. if (!f)
  523. goto out_elf_end;
  524. dso__insert_symbol(self, f);
  525. ++nr;
  526. }
  527. }
  528. err = 0;
  529. out_elf_end:
  530. elf_end(elf);
  531. out_close:
  532. close(fd);
  533. if (err == 0)
  534. return nr;
  535. out:
  536. fprintf(stderr, "%s: problems reading %s PLT info.\n",
  537. __func__, self->long_name);
  538. return 0;
  539. }
  540. static int dso__load_sym(struct dso *self, struct map *map, const char *name,
  541. int fd, symbol_filter_t filter, int kernel,
  542. int kmodule, int v)
  543. {
  544. struct map *curr_map = map;
  545. struct dso *curr_dso = self;
  546. size_t dso_name_len = strlen(self->short_name);
  547. Elf_Data *symstrs, *secstrs;
  548. uint32_t nr_syms;
  549. int err = -1;
  550. uint32_t idx;
  551. GElf_Ehdr ehdr;
  552. GElf_Shdr shdr;
  553. Elf_Data *syms;
  554. GElf_Sym sym;
  555. Elf_Scn *sec, *sec_strndx;
  556. Elf *elf;
  557. int nr = 0;
  558. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  559. if (elf == NULL) {
  560. if (v)
  561. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  562. __func__, name);
  563. goto out_close;
  564. }
  565. if (gelf_getehdr(elf, &ehdr) == NULL) {
  566. if (v)
  567. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  568. goto out_elf_end;
  569. }
  570. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  571. if (sec == NULL) {
  572. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  573. if (sec == NULL)
  574. goto out_elf_end;
  575. }
  576. syms = elf_getdata(sec, NULL);
  577. if (syms == NULL)
  578. goto out_elf_end;
  579. sec = elf_getscn(elf, shdr.sh_link);
  580. if (sec == NULL)
  581. goto out_elf_end;
  582. symstrs = elf_getdata(sec, NULL);
  583. if (symstrs == NULL)
  584. goto out_elf_end;
  585. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  586. if (sec_strndx == NULL)
  587. goto out_elf_end;
  588. secstrs = elf_getdata(sec_strndx, NULL);
  589. if (secstrs == NULL)
  590. goto out_elf_end;
  591. nr_syms = shdr.sh_size / shdr.sh_entsize;
  592. memset(&sym, 0, sizeof(sym));
  593. if (!kernel) {
  594. self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  595. elf_section_by_name(elf, &ehdr, &shdr,
  596. ".gnu.prelink_undo",
  597. NULL) != NULL);
  598. } else self->adjust_symbols = 0;
  599. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  600. struct symbol *f;
  601. const char *elf_name;
  602. char *demangled = NULL;
  603. int is_label = elf_sym__is_label(&sym);
  604. const char *section_name;
  605. if (!is_label && !elf_sym__is_function(&sym))
  606. continue;
  607. sec = elf_getscn(elf, sym.st_shndx);
  608. if (!sec)
  609. goto out_elf_end;
  610. gelf_getshdr(sec, &shdr);
  611. if (is_label && !elf_sec__is_text(&shdr, secstrs))
  612. continue;
  613. elf_name = elf_sym__name(&sym, symstrs);
  614. section_name = elf_sec__name(&shdr, secstrs);
  615. if (kernel || kmodule) {
  616. char dso_name[PATH_MAX];
  617. if (strcmp(section_name,
  618. curr_dso->short_name + dso_name_len) == 0)
  619. goto new_symbol;
  620. if (strcmp(section_name, ".text") == 0) {
  621. curr_map = map;
  622. curr_dso = self;
  623. goto new_symbol;
  624. }
  625. snprintf(dso_name, sizeof(dso_name),
  626. "%s%s", self->short_name, section_name);
  627. curr_map = kernel_maps__find_by_dso_name(dso_name);
  628. if (curr_map == NULL) {
  629. u64 start = sym.st_value;
  630. if (kmodule)
  631. start += map->start + shdr.sh_offset;
  632. curr_dso = dso__new(dso_name, self->sym_priv_size);
  633. if (curr_dso == NULL)
  634. goto out_elf_end;
  635. curr_map = map__new2(start, curr_dso);
  636. if (curr_map == NULL) {
  637. dso__delete(curr_dso);
  638. goto out_elf_end;
  639. }
  640. curr_map->map_ip = vdso__map_ip;
  641. curr_dso->origin = DSO__ORIG_KERNEL;
  642. kernel_maps__insert(curr_map);
  643. dsos__add(curr_dso);
  644. } else
  645. curr_dso = curr_map->dso;
  646. goto new_symbol;
  647. }
  648. if (curr_dso->adjust_symbols) {
  649. if (v > 2)
  650. printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
  651. (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
  652. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  653. }
  654. /*
  655. * We need to figure out if the object was created from C++ sources
  656. * DWARF DW_compile_unit has this, but we don't always have access
  657. * to it...
  658. */
  659. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  660. if (demangled != NULL)
  661. elf_name = demangled;
  662. new_symbol:
  663. f = symbol__new(sym.st_value, sym.st_size, elf_name,
  664. curr_dso->sym_priv_size, v);
  665. free(demangled);
  666. if (!f)
  667. goto out_elf_end;
  668. if (filter && filter(curr_map, f))
  669. symbol__delete(f, curr_dso->sym_priv_size);
  670. else {
  671. dso__insert_symbol(curr_dso, f);
  672. nr++;
  673. }
  674. }
  675. /*
  676. * For misannotated, zeroed, ASM function sizes.
  677. */
  678. if (nr > 0)
  679. dso__fixup_sym_end(self);
  680. err = nr;
  681. out_elf_end:
  682. elf_end(elf);
  683. out_close:
  684. return err;
  685. }
  686. #define BUILD_ID_SIZE 128
  687. static char *dso__read_build_id(struct dso *self, int v)
  688. {
  689. int i;
  690. GElf_Ehdr ehdr;
  691. GElf_Shdr shdr;
  692. Elf_Data *build_id_data;
  693. Elf_Scn *sec;
  694. char *build_id = NULL, *bid;
  695. unsigned char *raw;
  696. Elf *elf;
  697. int fd = open(self->long_name, O_RDONLY);
  698. if (fd < 0)
  699. goto out;
  700. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  701. if (elf == NULL) {
  702. if (v)
  703. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  704. __func__, self->long_name);
  705. goto out_close;
  706. }
  707. if (gelf_getehdr(elf, &ehdr) == NULL) {
  708. if (v)
  709. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  710. goto out_elf_end;
  711. }
  712. sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
  713. if (sec == NULL)
  714. goto out_elf_end;
  715. build_id_data = elf_getdata(sec, NULL);
  716. if (build_id_data == NULL)
  717. goto out_elf_end;
  718. build_id = malloc(BUILD_ID_SIZE);
  719. if (build_id == NULL)
  720. goto out_elf_end;
  721. raw = build_id_data->d_buf + 16;
  722. bid = build_id;
  723. for (i = 0; i < 20; ++i) {
  724. sprintf(bid, "%02x", *raw);
  725. ++raw;
  726. bid += 2;
  727. }
  728. if (v >= 2)
  729. printf("%s(%s): %s\n", __func__, self->long_name, build_id);
  730. out_elf_end:
  731. elf_end(elf);
  732. out_close:
  733. close(fd);
  734. out:
  735. return build_id;
  736. }
  737. char dso__symtab_origin(const struct dso *self)
  738. {
  739. static const char origin[] = {
  740. [DSO__ORIG_KERNEL] = 'k',
  741. [DSO__ORIG_JAVA_JIT] = 'j',
  742. [DSO__ORIG_FEDORA] = 'f',
  743. [DSO__ORIG_UBUNTU] = 'u',
  744. [DSO__ORIG_BUILDID] = 'b',
  745. [DSO__ORIG_DSO] = 'd',
  746. [DSO__ORIG_KMODULE] = 'K',
  747. };
  748. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  749. return '!';
  750. return origin[self->origin];
  751. }
  752. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter, int v)
  753. {
  754. int size = PATH_MAX;
  755. char *name = malloc(size), *build_id = NULL;
  756. int ret = -1;
  757. int fd;
  758. if (!name)
  759. return -1;
  760. self->adjust_symbols = 0;
  761. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  762. ret = dso__load_perf_map(self, map, filter, v);
  763. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  764. DSO__ORIG_NOT_FOUND;
  765. return ret;
  766. }
  767. self->origin = DSO__ORIG_FEDORA - 1;
  768. more:
  769. do {
  770. self->origin++;
  771. switch (self->origin) {
  772. case DSO__ORIG_FEDORA:
  773. snprintf(name, size, "/usr/lib/debug%s.debug",
  774. self->long_name);
  775. break;
  776. case DSO__ORIG_UBUNTU:
  777. snprintf(name, size, "/usr/lib/debug%s",
  778. self->long_name);
  779. break;
  780. case DSO__ORIG_BUILDID:
  781. build_id = dso__read_build_id(self, v);
  782. if (build_id != NULL) {
  783. snprintf(name, size,
  784. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  785. build_id, build_id + 2);
  786. free(build_id);
  787. break;
  788. }
  789. self->origin++;
  790. /* Fall thru */
  791. case DSO__ORIG_DSO:
  792. snprintf(name, size, "%s", self->long_name);
  793. break;
  794. default:
  795. goto out;
  796. }
  797. fd = open(name, O_RDONLY);
  798. } while (fd < 0);
  799. ret = dso__load_sym(self, map, name, fd, filter, 0, 0, v);
  800. close(fd);
  801. /*
  802. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  803. */
  804. if (!ret)
  805. goto more;
  806. if (ret > 0) {
  807. int nr_plt = dso__synthesize_plt_symbols(self, v);
  808. if (nr_plt > 0)
  809. ret += nr_plt;
  810. }
  811. out:
  812. free(name);
  813. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  814. return 0;
  815. return ret;
  816. }
  817. struct map *kernel_map;
  818. static void kernel_maps__insert(struct map *map)
  819. {
  820. maps__insert(&kernel_maps, map);
  821. }
  822. struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
  823. {
  824. struct map *map = maps__find(&kernel_maps, ip);
  825. if (mapp)
  826. *mapp = map;
  827. if (map) {
  828. ip = map->map_ip(map, ip);
  829. return map->dso->find_symbol(map->dso, ip);
  830. }
  831. return NULL;
  832. }
  833. struct map *kernel_maps__find_by_dso_name(const char *name)
  834. {
  835. struct rb_node *nd;
  836. for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
  837. struct map *map = rb_entry(nd, struct map, rb_node);
  838. if (map->dso && strcmp(map->dso->name, name) == 0)
  839. return map;
  840. }
  841. return NULL;
  842. }
  843. static int dso__load_module_sym(struct dso *self, struct map *map,
  844. symbol_filter_t filter, int v)
  845. {
  846. int err = 0, fd = open(self->long_name, O_RDONLY);
  847. if (fd < 0) {
  848. if (v)
  849. fprintf(stderr, "%s: cannot open %s\n",
  850. __func__, self->long_name);
  851. return err;
  852. }
  853. err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1, v);
  854. close(fd);
  855. return err;
  856. }
  857. static int dsos__load_modules_sym_dir(char *dirname,
  858. symbol_filter_t filter, int v)
  859. {
  860. struct dirent *dent;
  861. int nr_symbols = 0, err;
  862. DIR *dir = opendir(dirname);
  863. if (!dir) {
  864. if (v)
  865. fprintf(stderr, "%s: cannot open %s dir\n", __func__,
  866. dirname);
  867. return -1;
  868. }
  869. while ((dent = readdir(dir)) != NULL) {
  870. char path[PATH_MAX];
  871. if (dent->d_type == DT_DIR) {
  872. if (!strcmp(dent->d_name, ".") ||
  873. !strcmp(dent->d_name, ".."))
  874. continue;
  875. snprintf(path, sizeof(path), "%s/%s",
  876. dirname, dent->d_name);
  877. err = dsos__load_modules_sym_dir(path, filter, v);
  878. if (err < 0)
  879. goto failure;
  880. } else {
  881. char *dot = strrchr(dent->d_name, '.'),
  882. dso_name[PATH_MAX];
  883. struct map *map;
  884. struct rb_node *last;
  885. if (dot == NULL || strcmp(dot, ".ko"))
  886. continue;
  887. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  888. (int)(dot - dent->d_name), dent->d_name);
  889. strxfrchar(dso_name, '-', '_');
  890. map = kernel_maps__find_by_dso_name(dso_name);
  891. if (map == NULL)
  892. continue;
  893. snprintf(path, sizeof(path), "%s/%s",
  894. dirname, dent->d_name);
  895. map->dso->long_name = strdup(path);
  896. if (map->dso->long_name == NULL)
  897. goto failure;
  898. err = dso__load_module_sym(map->dso, map, filter, v);
  899. if (err < 0)
  900. goto failure;
  901. last = rb_last(&map->dso->syms);
  902. if (last) {
  903. struct symbol *sym;
  904. /*
  905. * We do this here as well, even having the
  906. * symbol size found in the symtab because
  907. * misannotated ASM symbols may have the size
  908. * set to zero.
  909. */
  910. dso__fixup_sym_end(map->dso);
  911. sym = rb_entry(last, struct symbol, rb_node);
  912. map->end = map->start + sym->end;
  913. }
  914. }
  915. nr_symbols += err;
  916. }
  917. return nr_symbols;
  918. failure:
  919. closedir(dir);
  920. return -1;
  921. }
  922. static int dsos__load_modules_sym(symbol_filter_t filter, int v)
  923. {
  924. struct utsname uts;
  925. char modules_path[PATH_MAX];
  926. if (uname(&uts) < 0)
  927. return -1;
  928. snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
  929. uts.release);
  930. return dsos__load_modules_sym_dir(modules_path, filter, v);
  931. }
  932. /*
  933. * Constructor variant for modules (where we know from /proc/modules where
  934. * they are loaded) and for vmlinux, where only after we load all the
  935. * symbols we'll know where it starts and ends.
  936. */
  937. static struct map *map__new2(u64 start, struct dso *dso)
  938. {
  939. struct map *self = malloc(sizeof(*self));
  940. if (self != NULL) {
  941. self->start = start;
  942. /*
  943. * Will be filled after we load all the symbols
  944. */
  945. self->end = 0;
  946. self->pgoff = 0;
  947. self->dso = dso;
  948. self->map_ip = map__map_ip;
  949. RB_CLEAR_NODE(&self->rb_node);
  950. }
  951. return self;
  952. }
  953. static int dsos__load_modules(unsigned int sym_priv_size)
  954. {
  955. char *line = NULL;
  956. size_t n;
  957. FILE *file = fopen("/proc/modules", "r");
  958. struct map *map;
  959. if (file == NULL)
  960. return -1;
  961. while (!feof(file)) {
  962. char name[PATH_MAX];
  963. u64 start;
  964. struct dso *dso;
  965. char *sep;
  966. int line_len;
  967. line_len = getline(&line, &n, file);
  968. if (line_len < 0)
  969. break;
  970. if (!line)
  971. goto out_failure;
  972. line[--line_len] = '\0'; /* \n */
  973. sep = strrchr(line, 'x');
  974. if (sep == NULL)
  975. continue;
  976. hex2u64(sep + 1, &start);
  977. sep = strchr(line, ' ');
  978. if (sep == NULL)
  979. continue;
  980. *sep = '\0';
  981. snprintf(name, sizeof(name), "[%s]", line);
  982. dso = dso__new(name, sym_priv_size);
  983. if (dso == NULL)
  984. goto out_delete_line;
  985. map = map__new2(start, dso);
  986. if (map == NULL) {
  987. dso__delete(dso);
  988. goto out_delete_line;
  989. }
  990. dso->origin = DSO__ORIG_KMODULE;
  991. kernel_maps__insert(map);
  992. dsos__add(dso);
  993. }
  994. free(line);
  995. fclose(file);
  996. return 0;
  997. out_delete_line:
  998. free(line);
  999. out_failure:
  1000. return -1;
  1001. }
  1002. static int dso__load_vmlinux(struct dso *self, struct map *map,
  1003. const char *vmlinux,
  1004. symbol_filter_t filter, int v)
  1005. {
  1006. int err, fd = open(vmlinux, O_RDONLY);
  1007. if (fd < 0)
  1008. return -1;
  1009. err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0, v);
  1010. close(fd);
  1011. return err;
  1012. }
  1013. int dsos__load_kernel(const char *vmlinux, unsigned int sym_priv_size,
  1014. symbol_filter_t filter, int v, int use_modules)
  1015. {
  1016. int err = -1;
  1017. struct dso *dso = dso__new(vmlinux, sym_priv_size);
  1018. if (dso == NULL)
  1019. return -1;
  1020. dso->short_name = "[kernel]";
  1021. kernel_map = map__new2(0, dso);
  1022. if (kernel_map == NULL)
  1023. goto out_delete_dso;
  1024. kernel_map->map_ip = vdso__map_ip;
  1025. if (use_modules && dsos__load_modules(sym_priv_size) < 0) {
  1026. fprintf(stderr, "Failed to load list of modules in use! "
  1027. "Continuing...\n");
  1028. use_modules = 0;
  1029. }
  1030. if (vmlinux) {
  1031. err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter, v);
  1032. if (err > 0 && use_modules) {
  1033. int syms = dsos__load_modules_sym(filter, v);
  1034. if (syms < 0)
  1035. fprintf(stderr, "Failed to read module symbols!"
  1036. " Continuing...\n");
  1037. else
  1038. err += syms;
  1039. }
  1040. }
  1041. if (err <= 0)
  1042. err = kernel_maps__load_kallsyms(filter, use_modules, v);
  1043. if (err > 0) {
  1044. struct rb_node *node = rb_first(&dso->syms);
  1045. struct symbol *sym = rb_entry(node, struct symbol, rb_node);
  1046. kernel_map->start = sym->start;
  1047. node = rb_last(&dso->syms);
  1048. sym = rb_entry(node, struct symbol, rb_node);
  1049. kernel_map->end = sym->end;
  1050. dso->origin = DSO__ORIG_KERNEL;
  1051. kernel_maps__insert(kernel_map);
  1052. /*
  1053. * Now that we have all sorted out, just set the ->end of all
  1054. * maps:
  1055. */
  1056. kernel_maps__fixup_end();
  1057. dsos__add(dso);
  1058. if (v > 0)
  1059. kernel_maps__fprintf(stderr, v);
  1060. }
  1061. return err;
  1062. out_delete_dso:
  1063. dso__delete(dso);
  1064. return -1;
  1065. }
  1066. LIST_HEAD(dsos);
  1067. struct dso *vdso;
  1068. const char *vmlinux_name = "vmlinux";
  1069. int modules;
  1070. static void dsos__add(struct dso *dso)
  1071. {
  1072. list_add_tail(&dso->node, &dsos);
  1073. }
  1074. static struct dso *dsos__find(const char *name)
  1075. {
  1076. struct dso *pos;
  1077. list_for_each_entry(pos, &dsos, node)
  1078. if (strcmp(pos->name, name) == 0)
  1079. return pos;
  1080. return NULL;
  1081. }
  1082. struct dso *dsos__findnew(const char *name)
  1083. {
  1084. struct dso *dso = dsos__find(name);
  1085. int nr;
  1086. if (dso)
  1087. return dso;
  1088. dso = dso__new(name, 0);
  1089. if (!dso)
  1090. goto out_delete_dso;
  1091. nr = dso__load(dso, NULL, NULL, verbose);
  1092. if (nr < 0) {
  1093. eprintf("Failed to open: %s\n", name);
  1094. goto out_delete_dso;
  1095. }
  1096. if (!nr)
  1097. eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
  1098. dsos__add(dso);
  1099. return dso;
  1100. out_delete_dso:
  1101. dso__delete(dso);
  1102. return NULL;
  1103. }
  1104. void dsos__fprintf(FILE *fp)
  1105. {
  1106. struct dso *pos;
  1107. list_for_each_entry(pos, &dsos, node)
  1108. dso__fprintf(pos, fp);
  1109. }
  1110. int load_kernel(void)
  1111. {
  1112. if (dsos__load_kernel(vmlinux_name, 0, NULL, verbose, modules) <= 0)
  1113. return -1;
  1114. vdso = dso__new("[vdso]", 0);
  1115. if (!vdso)
  1116. return -1;
  1117. dsos__add(vdso);
  1118. return 0;
  1119. }
  1120. void symbol__init(void)
  1121. {
  1122. elf_version(EV_CURRENT);
  1123. }