symbol.c 39 KB

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