symbol.c 44 KB

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