symbol.c 36 KB

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