symbol.c 36 KB

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