symbol.c 32 KB

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