symbol.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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 dso *dso);
  27. static struct dso *dsos__find(const char *name);
  28. static struct map *map__new2(u64 start, struct dso *dso);
  29. static void kernel_maps__insert(struct map *map);
  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 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(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. bool dsos__read_build_ids(void)
  743. {
  744. bool have_build_id = false;
  745. struct dso *pos;
  746. list_for_each_entry(pos, &dsos, 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. /*
  755. * Align offset to 4 bytes as needed for note name and descriptor data.
  756. */
  757. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  758. int filename__read_build_id(const char *filename, void *bf, size_t size)
  759. {
  760. int fd, err = -1;
  761. GElf_Ehdr ehdr;
  762. GElf_Shdr shdr;
  763. Elf_Data *data;
  764. Elf_Scn *sec;
  765. Elf_Kind ek;
  766. void *ptr;
  767. Elf *elf;
  768. if (size < BUILD_ID_SIZE)
  769. goto out;
  770. fd = open(filename, O_RDONLY);
  771. if (fd < 0)
  772. goto out;
  773. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  774. if (elf == NULL) {
  775. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  776. goto out_close;
  777. }
  778. ek = elf_kind(elf);
  779. if (ek != ELF_K_ELF)
  780. goto out_elf_end;
  781. if (gelf_getehdr(elf, &ehdr) == NULL) {
  782. pr_err("%s: cannot get elf header.\n", __func__);
  783. goto out_elf_end;
  784. }
  785. sec = elf_section_by_name(elf, &ehdr, &shdr,
  786. ".note.gnu.build-id", NULL);
  787. if (sec == NULL) {
  788. sec = elf_section_by_name(elf, &ehdr, &shdr,
  789. ".notes", NULL);
  790. if (sec == NULL)
  791. goto out_elf_end;
  792. }
  793. data = elf_getdata(sec, NULL);
  794. if (data == NULL)
  795. goto out_elf_end;
  796. ptr = data->d_buf;
  797. while (ptr < (data->d_buf + data->d_size)) {
  798. GElf_Nhdr *nhdr = ptr;
  799. int namesz = NOTE_ALIGN(nhdr->n_namesz),
  800. descsz = NOTE_ALIGN(nhdr->n_descsz);
  801. const char *name;
  802. ptr += sizeof(*nhdr);
  803. name = ptr;
  804. ptr += namesz;
  805. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  806. nhdr->n_namesz == sizeof("GNU")) {
  807. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  808. memcpy(bf, ptr, BUILD_ID_SIZE);
  809. err = BUILD_ID_SIZE;
  810. break;
  811. }
  812. }
  813. ptr += descsz;
  814. }
  815. out_elf_end:
  816. elf_end(elf);
  817. out_close:
  818. close(fd);
  819. out:
  820. return err;
  821. }
  822. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  823. {
  824. int fd, err = -1;
  825. if (size < BUILD_ID_SIZE)
  826. goto out;
  827. fd = open(filename, O_RDONLY);
  828. if (fd < 0)
  829. goto out;
  830. while (1) {
  831. char bf[BUFSIZ];
  832. GElf_Nhdr nhdr;
  833. int namesz, descsz;
  834. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  835. break;
  836. namesz = NOTE_ALIGN(nhdr.n_namesz);
  837. descsz = NOTE_ALIGN(nhdr.n_descsz);
  838. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  839. nhdr.n_namesz == sizeof("GNU")) {
  840. if (read(fd, bf, namesz) != namesz)
  841. break;
  842. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  843. if (read(fd, build_id,
  844. BUILD_ID_SIZE) == BUILD_ID_SIZE) {
  845. err = 0;
  846. break;
  847. }
  848. } else if (read(fd, bf, descsz) != descsz)
  849. break;
  850. } else {
  851. int n = namesz + descsz;
  852. if (read(fd, bf, n) != n)
  853. break;
  854. }
  855. }
  856. close(fd);
  857. out:
  858. return err;
  859. }
  860. char dso__symtab_origin(const struct dso *self)
  861. {
  862. static const char origin[] = {
  863. [DSO__ORIG_KERNEL] = 'k',
  864. [DSO__ORIG_JAVA_JIT] = 'j',
  865. [DSO__ORIG_FEDORA] = 'f',
  866. [DSO__ORIG_UBUNTU] = 'u',
  867. [DSO__ORIG_BUILDID] = 'b',
  868. [DSO__ORIG_DSO] = 'd',
  869. [DSO__ORIG_KMODULE] = 'K',
  870. };
  871. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  872. return '!';
  873. return origin[self->origin];
  874. }
  875. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
  876. {
  877. int size = PATH_MAX;
  878. char *name;
  879. u8 build_id[BUILD_ID_SIZE];
  880. int ret = -1;
  881. int fd;
  882. self->loaded = 1;
  883. if (self->kernel)
  884. return dso__load_kernel_sym(self, map, filter);
  885. name = malloc(size);
  886. if (!name)
  887. return -1;
  888. self->adjust_symbols = 0;
  889. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  890. ret = dso__load_perf_map(self, map, filter);
  891. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  892. DSO__ORIG_NOT_FOUND;
  893. return ret;
  894. }
  895. self->origin = DSO__ORIG_FEDORA - 1;
  896. more:
  897. do {
  898. self->origin++;
  899. switch (self->origin) {
  900. case DSO__ORIG_FEDORA:
  901. snprintf(name, size, "/usr/lib/debug%s.debug",
  902. self->long_name);
  903. break;
  904. case DSO__ORIG_UBUNTU:
  905. snprintf(name, size, "/usr/lib/debug%s",
  906. self->long_name);
  907. break;
  908. case DSO__ORIG_BUILDID:
  909. if (filename__read_build_id(self->long_name, build_id,
  910. sizeof(build_id))) {
  911. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  912. build_id__sprintf(build_id, sizeof(build_id),
  913. build_id_hex);
  914. snprintf(name, size,
  915. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  916. build_id_hex, build_id_hex + 2);
  917. if (self->has_build_id)
  918. goto compare_build_id;
  919. break;
  920. }
  921. self->origin++;
  922. /* Fall thru */
  923. case DSO__ORIG_DSO:
  924. snprintf(name, size, "%s", self->long_name);
  925. break;
  926. default:
  927. goto out;
  928. }
  929. if (self->has_build_id) {
  930. if (filename__read_build_id(name, build_id,
  931. sizeof(build_id)) < 0)
  932. goto more;
  933. compare_build_id:
  934. if (!dso__build_id_equal(self, build_id))
  935. goto more;
  936. }
  937. fd = open(name, O_RDONLY);
  938. } while (fd < 0);
  939. ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
  940. close(fd);
  941. /*
  942. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  943. */
  944. if (!ret)
  945. goto more;
  946. if (ret > 0) {
  947. int nr_plt = dso__synthesize_plt_symbols(self, map, filter);
  948. if (nr_plt > 0)
  949. ret += nr_plt;
  950. }
  951. out:
  952. free(name);
  953. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  954. return 0;
  955. return ret;
  956. }
  957. struct map *kernel_map__functions;
  958. static void kernel_maps__insert(struct map *map)
  959. {
  960. maps__insert(&kernel_maps__functions, map);
  961. }
  962. struct symbol *kernel_maps__find_function(u64 ip, struct map **mapp,
  963. symbol_filter_t filter)
  964. {
  965. struct map *map = maps__find(&kernel_maps__functions, ip);
  966. if (mapp)
  967. *mapp = map;
  968. if (map) {
  969. ip = map->map_ip(map, ip);
  970. return map__find_function(map, ip, filter);
  971. } else
  972. WARN_ONCE(RB_EMPTY_ROOT(&kernel_maps__functions),
  973. "Empty kernel_maps, was symbol__init() called?\n");
  974. return NULL;
  975. }
  976. struct map *kernel_maps__find_by_dso_name(const char *name)
  977. {
  978. struct rb_node *nd;
  979. for (nd = rb_first(&kernel_maps__functions); nd; nd = rb_next(nd)) {
  980. struct map *map = rb_entry(nd, struct map, rb_node);
  981. if (map->dso && strcmp(map->dso->name, name) == 0)
  982. return map;
  983. }
  984. return NULL;
  985. }
  986. static int dsos__set_modules_path_dir(char *dirname)
  987. {
  988. struct dirent *dent;
  989. DIR *dir = opendir(dirname);
  990. if (!dir) {
  991. pr_debug("%s: cannot open %s dir\n", __func__, dirname);
  992. return -1;
  993. }
  994. while ((dent = readdir(dir)) != NULL) {
  995. char path[PATH_MAX];
  996. if (dent->d_type == DT_DIR) {
  997. if (!strcmp(dent->d_name, ".") ||
  998. !strcmp(dent->d_name, ".."))
  999. continue;
  1000. snprintf(path, sizeof(path), "%s/%s",
  1001. dirname, dent->d_name);
  1002. if (dsos__set_modules_path_dir(path) < 0)
  1003. goto failure;
  1004. } else {
  1005. char *dot = strrchr(dent->d_name, '.'),
  1006. dso_name[PATH_MAX];
  1007. struct map *map;
  1008. char *long_name;
  1009. if (dot == NULL || strcmp(dot, ".ko"))
  1010. continue;
  1011. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  1012. (int)(dot - dent->d_name), dent->d_name);
  1013. strxfrchar(dso_name, '-', '_');
  1014. map = kernel_maps__find_by_dso_name(dso_name);
  1015. if (map == NULL)
  1016. continue;
  1017. snprintf(path, sizeof(path), "%s/%s",
  1018. dirname, dent->d_name);
  1019. long_name = strdup(path);
  1020. if (long_name == NULL)
  1021. goto failure;
  1022. dso__set_long_name(map->dso, long_name);
  1023. }
  1024. }
  1025. return 0;
  1026. failure:
  1027. closedir(dir);
  1028. return -1;
  1029. }
  1030. static int dsos__set_modules_path(void)
  1031. {
  1032. struct utsname uts;
  1033. char modules_path[PATH_MAX];
  1034. if (uname(&uts) < 0)
  1035. return -1;
  1036. snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
  1037. uts.release);
  1038. return dsos__set_modules_path_dir(modules_path);
  1039. }
  1040. /*
  1041. * Constructor variant for modules (where we know from /proc/modules where
  1042. * they are loaded) and for vmlinux, where only after we load all the
  1043. * symbols we'll know where it starts and ends.
  1044. */
  1045. static struct map *map__new2(u64 start, struct dso *dso)
  1046. {
  1047. struct map *self = malloc(sizeof(*self));
  1048. if (self != NULL) {
  1049. /*
  1050. * ->end will be filled after we load all the symbols
  1051. */
  1052. map__init(self, start, 0, 0, dso);
  1053. }
  1054. return self;
  1055. }
  1056. static int kernel_maps__create_module_maps(void)
  1057. {
  1058. char *line = NULL;
  1059. size_t n;
  1060. FILE *file = fopen("/proc/modules", "r");
  1061. struct map *map;
  1062. if (file == NULL)
  1063. return -1;
  1064. while (!feof(file)) {
  1065. char name[PATH_MAX];
  1066. u64 start;
  1067. struct dso *dso;
  1068. char *sep;
  1069. int line_len;
  1070. line_len = getline(&line, &n, file);
  1071. if (line_len < 0)
  1072. break;
  1073. if (!line)
  1074. goto out_failure;
  1075. line[--line_len] = '\0'; /* \n */
  1076. sep = strrchr(line, 'x');
  1077. if (sep == NULL)
  1078. continue;
  1079. hex2u64(sep + 1, &start);
  1080. sep = strchr(line, ' ');
  1081. if (sep == NULL)
  1082. continue;
  1083. *sep = '\0';
  1084. snprintf(name, sizeof(name), "[%s]", line);
  1085. dso = dso__new(name);
  1086. if (dso == NULL)
  1087. goto out_delete_line;
  1088. map = map__new2(start, dso);
  1089. if (map == NULL) {
  1090. dso__delete(dso);
  1091. goto out_delete_line;
  1092. }
  1093. snprintf(name, sizeof(name),
  1094. "/sys/module/%s/notes/.note.gnu.build-id", line);
  1095. if (sysfs__read_build_id(name, dso->build_id,
  1096. sizeof(dso->build_id)) == 0)
  1097. dso->has_build_id = true;
  1098. dso->origin = DSO__ORIG_KMODULE;
  1099. kernel_maps__insert(map);
  1100. dsos__add(dso);
  1101. }
  1102. free(line);
  1103. fclose(file);
  1104. return dsos__set_modules_path();
  1105. out_delete_line:
  1106. free(line);
  1107. out_failure:
  1108. return -1;
  1109. }
  1110. static int dso__load_vmlinux(struct dso *self, struct map *map,
  1111. const char *vmlinux, symbol_filter_t filter)
  1112. {
  1113. int err = -1, fd;
  1114. if (self->has_build_id) {
  1115. u8 build_id[BUILD_ID_SIZE];
  1116. if (filename__read_build_id(vmlinux, build_id,
  1117. sizeof(build_id)) < 0) {
  1118. pr_debug("No build_id in %s, ignoring it\n", vmlinux);
  1119. return -1;
  1120. }
  1121. if (!dso__build_id_equal(self, build_id)) {
  1122. char expected_build_id[BUILD_ID_SIZE * 2 + 1],
  1123. vmlinux_build_id[BUILD_ID_SIZE * 2 + 1];
  1124. build_id__sprintf(self->build_id,
  1125. sizeof(self->build_id),
  1126. expected_build_id);
  1127. build_id__sprintf(build_id, sizeof(build_id),
  1128. vmlinux_build_id);
  1129. pr_debug("build_id in %s is %s while expected is %s, "
  1130. "ignoring it\n", vmlinux, vmlinux_build_id,
  1131. expected_build_id);
  1132. return -1;
  1133. }
  1134. }
  1135. fd = open(vmlinux, O_RDONLY);
  1136. if (fd < 0)
  1137. return -1;
  1138. self->loaded = 1;
  1139. err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
  1140. close(fd);
  1141. return err;
  1142. }
  1143. static int dso__load_kernel_sym(struct dso *self, struct map *map,
  1144. symbol_filter_t filter)
  1145. {
  1146. int err;
  1147. bool is_kallsyms;
  1148. if (vmlinux_path != NULL) {
  1149. int i;
  1150. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1151. vmlinux_path__nr_entries);
  1152. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1153. err = dso__load_vmlinux(self, map, vmlinux_path[i],
  1154. filter);
  1155. if (err > 0) {
  1156. pr_debug("Using %s for symbols\n",
  1157. vmlinux_path[i]);
  1158. dso__set_long_name(self,
  1159. strdup(vmlinux_path[i]));
  1160. goto out_fixup;
  1161. }
  1162. }
  1163. }
  1164. is_kallsyms = self->long_name[0] == '[';
  1165. if (is_kallsyms)
  1166. goto do_kallsyms;
  1167. err = dso__load_vmlinux(self, map, self->long_name, filter);
  1168. if (err <= 0) {
  1169. pr_info("The file %s cannot be used, "
  1170. "trying to use /proc/kallsyms...", self->long_name);
  1171. sleep(2);
  1172. do_kallsyms:
  1173. err = kernel_maps__load_kallsyms(filter);
  1174. if (err > 0 && !is_kallsyms)
  1175. dso__set_long_name(self, strdup("[kernel.kallsyms]"));
  1176. }
  1177. if (err > 0) {
  1178. out_fixup:
  1179. map__fixup_start(map, &map->dso->functions);
  1180. map__fixup_end(map, &map->dso->functions);
  1181. }
  1182. return err;
  1183. }
  1184. LIST_HEAD(dsos);
  1185. struct dso *vdso;
  1186. static void dsos__add(struct dso *dso)
  1187. {
  1188. list_add_tail(&dso->node, &dsos);
  1189. }
  1190. static struct dso *dsos__find(const char *name)
  1191. {
  1192. struct dso *pos;
  1193. list_for_each_entry(pos, &dsos, node)
  1194. if (strcmp(pos->name, name) == 0)
  1195. return pos;
  1196. return NULL;
  1197. }
  1198. struct dso *dsos__findnew(const char *name)
  1199. {
  1200. struct dso *dso = dsos__find(name);
  1201. if (!dso) {
  1202. dso = dso__new(name);
  1203. if (dso != NULL) {
  1204. dsos__add(dso);
  1205. dso__set_basename(dso);
  1206. }
  1207. }
  1208. return dso;
  1209. }
  1210. void dsos__fprintf(FILE *fp)
  1211. {
  1212. struct dso *pos;
  1213. list_for_each_entry(pos, &dsos, node)
  1214. dso__fprintf(pos, fp);
  1215. }
  1216. size_t dsos__fprintf_buildid(FILE *fp)
  1217. {
  1218. struct dso *pos;
  1219. size_t ret = 0;
  1220. list_for_each_entry(pos, &dsos, node) {
  1221. ret += dso__fprintf_buildid(pos, fp);
  1222. ret += fprintf(fp, " %s\n", pos->long_name);
  1223. }
  1224. return ret;
  1225. }
  1226. static int kernel_maps__create_kernel_map(const struct symbol_conf *conf)
  1227. {
  1228. struct dso *kernel = dso__new(conf->vmlinux_name ?: "[kernel.kallsyms]");
  1229. if (kernel == NULL)
  1230. return -1;
  1231. kernel_map__functions = map__new2(0, kernel);
  1232. if (kernel_map__functions == NULL)
  1233. goto out_delete_kernel_dso;
  1234. kernel_map__functions->map_ip = kernel_map__functions->unmap_ip = identity__map_ip;
  1235. kernel->short_name = "[kernel]";
  1236. kernel->kernel = 1;
  1237. vdso = dso__new("[vdso]");
  1238. if (vdso == NULL)
  1239. goto out_delete_kernel_map;
  1240. vdso->loaded = 1;
  1241. if (sysfs__read_build_id("/sys/kernel/notes", kernel->build_id,
  1242. sizeof(kernel->build_id)) == 0)
  1243. kernel->has_build_id = true;
  1244. kernel_maps__insert(kernel_map__functions);
  1245. dsos__add(kernel);
  1246. dsos__add(vdso);
  1247. return 0;
  1248. out_delete_kernel_map:
  1249. map__delete(kernel_map__functions);
  1250. kernel_map__functions = NULL;
  1251. out_delete_kernel_dso:
  1252. dso__delete(kernel);
  1253. return -1;
  1254. }
  1255. static void vmlinux_path__exit(void)
  1256. {
  1257. while (--vmlinux_path__nr_entries >= 0) {
  1258. free(vmlinux_path[vmlinux_path__nr_entries]);
  1259. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1260. }
  1261. free(vmlinux_path);
  1262. vmlinux_path = NULL;
  1263. }
  1264. static int vmlinux_path__init(void)
  1265. {
  1266. struct utsname uts;
  1267. char bf[PATH_MAX];
  1268. if (uname(&uts) < 0)
  1269. return -1;
  1270. vmlinux_path = malloc(sizeof(char *) * 5);
  1271. if (vmlinux_path == NULL)
  1272. return -1;
  1273. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1274. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1275. goto out_fail;
  1276. ++vmlinux_path__nr_entries;
  1277. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1278. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1279. goto out_fail;
  1280. ++vmlinux_path__nr_entries;
  1281. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1282. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1283. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1284. goto out_fail;
  1285. ++vmlinux_path__nr_entries;
  1286. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1287. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1288. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1289. goto out_fail;
  1290. ++vmlinux_path__nr_entries;
  1291. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1292. uts.release);
  1293. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1294. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1295. goto out_fail;
  1296. ++vmlinux_path__nr_entries;
  1297. return 0;
  1298. out_fail:
  1299. vmlinux_path__exit();
  1300. return -1;
  1301. }
  1302. static int kernel_maps__init(const struct symbol_conf *conf)
  1303. {
  1304. const struct symbol_conf *pconf = conf ?: &symbol_conf__defaults;
  1305. symbol__priv_size = pconf->priv_size;
  1306. if (pconf->try_vmlinux_path && vmlinux_path__init() < 0)
  1307. return -1;
  1308. if (kernel_maps__create_kernel_map(pconf) < 0) {
  1309. vmlinux_path__exit();
  1310. return -1;
  1311. }
  1312. if (pconf->use_modules && kernel_maps__create_module_maps() < 0)
  1313. pr_debug("Failed to load list of modules in use, "
  1314. "continuing...\n");
  1315. /*
  1316. * Now that we have all the maps created, just set the ->end of them:
  1317. */
  1318. kernel_maps__fixup_end();
  1319. return 0;
  1320. }
  1321. int symbol__init(struct symbol_conf *conf)
  1322. {
  1323. elf_version(EV_CURRENT);
  1324. return kernel_maps__init(conf);
  1325. }