symbol.c 35 KB

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