symbol.c 39 KB

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