symbol.c 42 KB

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