symbol.c 36 KB

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