symbol.c 40 KB

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