symbol.c 36 KB

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