symbol.c 44 KB

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