symbol.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. #include <dirent.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/param.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <inttypes.h>
  12. #include "build-id.h"
  13. #include "util.h"
  14. #include "debug.h"
  15. #include "machine.h"
  16. #include "symbol.h"
  17. #include "strlist.h"
  18. #include <elf.h>
  19. #include <limits.h>
  20. #include <sys/utsname.h>
  21. #ifndef KSYM_NAME_LEN
  22. #define KSYM_NAME_LEN 256
  23. #endif
  24. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  25. symbol_filter_t filter);
  26. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  27. symbol_filter_t filter);
  28. int vmlinux_path__nr_entries;
  29. char **vmlinux_path;
  30. struct symbol_conf symbol_conf = {
  31. .exclude_other = true,
  32. .use_modules = true,
  33. .try_vmlinux_path = true,
  34. .annotate_src = true,
  35. .demangle = true,
  36. .symfs = "",
  37. };
  38. static enum dso_binary_type binary_type_symtab[] = {
  39. DSO_BINARY_TYPE__KALLSYMS,
  40. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  41. DSO_BINARY_TYPE__JAVA_JIT,
  42. DSO_BINARY_TYPE__DEBUGLINK,
  43. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  44. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  45. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  46. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  47. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  48. DSO_BINARY_TYPE__GUEST_KMODULE,
  49. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  50. DSO_BINARY_TYPE__NOT_FOUND,
  51. };
  52. #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
  53. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  54. {
  55. symbol_type = toupper(symbol_type);
  56. switch (map_type) {
  57. case MAP__FUNCTION:
  58. return symbol_type == 'T' || symbol_type == 'W';
  59. case MAP__VARIABLE:
  60. return symbol_type == 'D';
  61. default:
  62. return false;
  63. }
  64. }
  65. static int prefix_underscores_count(const char *str)
  66. {
  67. const char *tail = str;
  68. while (*tail == '_')
  69. tail++;
  70. return tail - str;
  71. }
  72. #define SYMBOL_A 0
  73. #define SYMBOL_B 1
  74. static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
  75. {
  76. s64 a;
  77. s64 b;
  78. /* Prefer a symbol with non zero length */
  79. a = syma->end - syma->start;
  80. b = symb->end - symb->start;
  81. if ((b == 0) && (a > 0))
  82. return SYMBOL_A;
  83. else if ((a == 0) && (b > 0))
  84. return SYMBOL_B;
  85. /* Prefer a non weak symbol over a weak one */
  86. a = syma->binding == STB_WEAK;
  87. b = symb->binding == STB_WEAK;
  88. if (b && !a)
  89. return SYMBOL_A;
  90. if (a && !b)
  91. return SYMBOL_B;
  92. /* Prefer a global symbol over a non global one */
  93. a = syma->binding == STB_GLOBAL;
  94. b = symb->binding == STB_GLOBAL;
  95. if (a && !b)
  96. return SYMBOL_A;
  97. if (b && !a)
  98. return SYMBOL_B;
  99. /* Prefer a symbol with less underscores */
  100. a = prefix_underscores_count(syma->name);
  101. b = prefix_underscores_count(symb->name);
  102. if (b > a)
  103. return SYMBOL_A;
  104. else if (a > b)
  105. return SYMBOL_B;
  106. /* If all else fails, choose the symbol with the longest name */
  107. if (strlen(syma->name) >= strlen(symb->name))
  108. return SYMBOL_A;
  109. else
  110. return SYMBOL_B;
  111. }
  112. void symbols__fixup_duplicate(struct rb_root *symbols)
  113. {
  114. struct rb_node *nd;
  115. struct symbol *curr, *next;
  116. nd = rb_first(symbols);
  117. while (nd) {
  118. curr = rb_entry(nd, struct symbol, rb_node);
  119. again:
  120. nd = rb_next(&curr->rb_node);
  121. next = rb_entry(nd, struct symbol, rb_node);
  122. if (!nd)
  123. break;
  124. if (curr->start != next->start)
  125. continue;
  126. if (choose_best_symbol(curr, next) == SYMBOL_A) {
  127. rb_erase(&next->rb_node, symbols);
  128. goto again;
  129. } else {
  130. nd = rb_next(&curr->rb_node);
  131. rb_erase(&curr->rb_node, symbols);
  132. }
  133. }
  134. }
  135. void symbols__fixup_end(struct rb_root *symbols)
  136. {
  137. struct rb_node *nd, *prevnd = rb_first(symbols);
  138. struct symbol *curr, *prev;
  139. if (prevnd == NULL)
  140. return;
  141. curr = rb_entry(prevnd, struct symbol, rb_node);
  142. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  143. prev = curr;
  144. curr = rb_entry(nd, struct symbol, rb_node);
  145. if (prev->end == prev->start && prev->end != curr->start)
  146. prev->end = curr->start - 1;
  147. }
  148. /* Last entry */
  149. if (curr->end == curr->start)
  150. curr->end = roundup(curr->start, 4096);
  151. }
  152. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
  153. {
  154. struct map *prev, *curr;
  155. struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
  156. if (prevnd == NULL)
  157. return;
  158. curr = rb_entry(prevnd, struct map, rb_node);
  159. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  160. prev = curr;
  161. curr = rb_entry(nd, struct map, rb_node);
  162. prev->end = curr->start - 1;
  163. }
  164. /*
  165. * We still haven't the actual symbols, so guess the
  166. * last map final address.
  167. */
  168. curr->end = ~0ULL;
  169. }
  170. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
  171. {
  172. size_t namelen = strlen(name) + 1;
  173. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  174. sizeof(*sym) + namelen));
  175. if (sym == NULL)
  176. return NULL;
  177. if (symbol_conf.priv_size)
  178. sym = ((void *)sym) + symbol_conf.priv_size;
  179. sym->start = start;
  180. sym->end = len ? start + len - 1 : start;
  181. sym->binding = binding;
  182. sym->namelen = namelen - 1;
  183. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  184. __func__, name, start, sym->end);
  185. memcpy(sym->name, name, namelen);
  186. return sym;
  187. }
  188. void symbol__delete(struct symbol *sym)
  189. {
  190. free(((void *)sym) - symbol_conf.priv_size);
  191. }
  192. size_t symbol__fprintf(struct symbol *sym, FILE *fp)
  193. {
  194. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
  195. sym->start, sym->end,
  196. sym->binding == STB_GLOBAL ? 'g' :
  197. sym->binding == STB_LOCAL ? 'l' : 'w',
  198. sym->name);
  199. }
  200. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  201. const struct addr_location *al, FILE *fp)
  202. {
  203. unsigned long offset;
  204. size_t length;
  205. if (sym && sym->name) {
  206. length = fprintf(fp, "%s", sym->name);
  207. if (al) {
  208. offset = al->addr - sym->start;
  209. length += fprintf(fp, "+0x%lx", offset);
  210. }
  211. return length;
  212. } else
  213. return fprintf(fp, "[unknown]");
  214. }
  215. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
  216. {
  217. return symbol__fprintf_symname_offs(sym, NULL, fp);
  218. }
  219. void symbols__delete(struct rb_root *symbols)
  220. {
  221. struct symbol *pos;
  222. struct rb_node *next = rb_first(symbols);
  223. while (next) {
  224. pos = rb_entry(next, struct symbol, rb_node);
  225. next = rb_next(&pos->rb_node);
  226. rb_erase(&pos->rb_node, symbols);
  227. symbol__delete(pos);
  228. }
  229. }
  230. void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  231. {
  232. struct rb_node **p = &symbols->rb_node;
  233. struct rb_node *parent = NULL;
  234. const u64 ip = sym->start;
  235. struct symbol *s;
  236. while (*p != NULL) {
  237. parent = *p;
  238. s = rb_entry(parent, struct symbol, rb_node);
  239. if (ip < s->start)
  240. p = &(*p)->rb_left;
  241. else
  242. p = &(*p)->rb_right;
  243. }
  244. rb_link_node(&sym->rb_node, parent, p);
  245. rb_insert_color(&sym->rb_node, symbols);
  246. }
  247. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  248. {
  249. struct rb_node *n;
  250. if (symbols == NULL)
  251. return NULL;
  252. n = symbols->rb_node;
  253. while (n) {
  254. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  255. if (ip < s->start)
  256. n = n->rb_left;
  257. else if (ip > s->end)
  258. n = n->rb_right;
  259. else
  260. return s;
  261. }
  262. return NULL;
  263. }
  264. struct symbol_name_rb_node {
  265. struct rb_node rb_node;
  266. struct symbol sym;
  267. };
  268. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  269. {
  270. struct rb_node **p = &symbols->rb_node;
  271. struct rb_node *parent = NULL;
  272. struct symbol_name_rb_node *symn, *s;
  273. symn = container_of(sym, struct symbol_name_rb_node, sym);
  274. while (*p != NULL) {
  275. parent = *p;
  276. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  277. if (strcmp(sym->name, s->sym.name) < 0)
  278. p = &(*p)->rb_left;
  279. else
  280. p = &(*p)->rb_right;
  281. }
  282. rb_link_node(&symn->rb_node, parent, p);
  283. rb_insert_color(&symn->rb_node, symbols);
  284. }
  285. static void symbols__sort_by_name(struct rb_root *symbols,
  286. struct rb_root *source)
  287. {
  288. struct rb_node *nd;
  289. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  290. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  291. symbols__insert_by_name(symbols, pos);
  292. }
  293. }
  294. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  295. const char *name)
  296. {
  297. struct rb_node *n;
  298. if (symbols == NULL)
  299. return NULL;
  300. n = symbols->rb_node;
  301. while (n) {
  302. struct symbol_name_rb_node *s;
  303. int cmp;
  304. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  305. cmp = strcmp(name, s->sym.name);
  306. if (cmp < 0)
  307. n = n->rb_left;
  308. else if (cmp > 0)
  309. n = n->rb_right;
  310. else
  311. return &s->sym;
  312. }
  313. return NULL;
  314. }
  315. struct symbol *dso__find_symbol(struct dso *dso,
  316. enum map_type type, u64 addr)
  317. {
  318. return symbols__find(&dso->symbols[type], addr);
  319. }
  320. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  321. const char *name)
  322. {
  323. return symbols__find_by_name(&dso->symbol_names[type], name);
  324. }
  325. void dso__sort_by_name(struct dso *dso, enum map_type type)
  326. {
  327. dso__set_sorted_by_name(dso, type);
  328. return symbols__sort_by_name(&dso->symbol_names[type],
  329. &dso->symbols[type]);
  330. }
  331. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  332. enum map_type type, FILE *fp)
  333. {
  334. size_t ret = 0;
  335. struct rb_node *nd;
  336. struct symbol_name_rb_node *pos;
  337. for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
  338. pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  339. fprintf(fp, "%s\n", pos->sym.name);
  340. }
  341. return ret;
  342. }
  343. int kallsyms__parse(const char *filename, void *arg,
  344. int (*process_symbol)(void *arg, const char *name,
  345. char type, u64 start))
  346. {
  347. char *line = NULL;
  348. size_t n;
  349. int err = -1;
  350. FILE *file = fopen(filename, "r");
  351. if (file == NULL)
  352. goto out_failure;
  353. err = 0;
  354. while (!feof(file)) {
  355. u64 start;
  356. int line_len, len;
  357. char symbol_type;
  358. char *symbol_name;
  359. line_len = getline(&line, &n, file);
  360. if (line_len < 0 || !line)
  361. break;
  362. line[--line_len] = '\0'; /* \n */
  363. len = hex2u64(line, &start);
  364. len++;
  365. if (len + 2 >= line_len)
  366. continue;
  367. symbol_type = line[len];
  368. len += 2;
  369. symbol_name = line + len;
  370. len = line_len - len;
  371. if (len >= KSYM_NAME_LEN) {
  372. err = -1;
  373. break;
  374. }
  375. err = process_symbol(arg, symbol_name,
  376. symbol_type, start);
  377. if (err)
  378. break;
  379. }
  380. free(line);
  381. fclose(file);
  382. return err;
  383. out_failure:
  384. return -1;
  385. }
  386. struct process_kallsyms_args {
  387. struct map *map;
  388. struct dso *dso;
  389. };
  390. static u8 kallsyms2elf_type(char type)
  391. {
  392. if (type == 'W')
  393. return STB_WEAK;
  394. return isupper(type) ? STB_GLOBAL : STB_LOCAL;
  395. }
  396. static int map__process_kallsym_symbol(void *arg, const char *name,
  397. char type, u64 start)
  398. {
  399. struct symbol *sym;
  400. struct process_kallsyms_args *a = arg;
  401. struct rb_root *root = &a->dso->symbols[a->map->type];
  402. if (!symbol_type__is_a(type, a->map->type))
  403. return 0;
  404. /*
  405. * module symbols are not sorted so we add all
  406. * symbols, setting length to 0, and rely on
  407. * symbols__fixup_end() to fix it up.
  408. */
  409. sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
  410. if (sym == NULL)
  411. return -ENOMEM;
  412. /*
  413. * We will pass the symbols to the filter later, in
  414. * map__split_kallsyms, when we have split the maps per module
  415. */
  416. symbols__insert(root, sym);
  417. return 0;
  418. }
  419. /*
  420. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  421. * so that we can in the next step set the symbol ->end address and then
  422. * call kernel_maps__split_kallsyms.
  423. */
  424. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  425. struct map *map)
  426. {
  427. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  428. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  429. }
  430. /*
  431. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  432. * kernel range is broken in several maps, named [kernel].N, as we don't have
  433. * the original ELF section names vmlinux have.
  434. */
  435. static int dso__split_kallsyms(struct dso *dso, struct map *map,
  436. symbol_filter_t filter)
  437. {
  438. struct map_groups *kmaps = map__kmap(map)->kmaps;
  439. struct machine *machine = kmaps->machine;
  440. struct map *curr_map = map;
  441. struct symbol *pos;
  442. int count = 0, moved = 0;
  443. struct rb_root *root = &dso->symbols[map->type];
  444. struct rb_node *next = rb_first(root);
  445. int kernel_range = 0;
  446. while (next) {
  447. char *module;
  448. pos = rb_entry(next, struct symbol, rb_node);
  449. next = rb_next(&pos->rb_node);
  450. module = strchr(pos->name, '\t');
  451. if (module) {
  452. if (!symbol_conf.use_modules)
  453. goto discard_symbol;
  454. *module++ = '\0';
  455. if (strcmp(curr_map->dso->short_name, module)) {
  456. if (curr_map != map &&
  457. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  458. machine__is_default_guest(machine)) {
  459. /*
  460. * We assume all symbols of a module are
  461. * continuous in * kallsyms, so curr_map
  462. * points to a module and all its
  463. * symbols are in its kmap. Mark it as
  464. * loaded.
  465. */
  466. dso__set_loaded(curr_map->dso,
  467. curr_map->type);
  468. }
  469. curr_map = map_groups__find_by_name(kmaps,
  470. map->type, module);
  471. if (curr_map == NULL) {
  472. pr_debug("%s/proc/{kallsyms,modules} "
  473. "inconsistency while looking "
  474. "for \"%s\" module!\n",
  475. machine->root_dir, module);
  476. curr_map = map;
  477. goto discard_symbol;
  478. }
  479. if (curr_map->dso->loaded &&
  480. !machine__is_default_guest(machine))
  481. goto discard_symbol;
  482. }
  483. /*
  484. * So that we look just like we get from .ko files,
  485. * i.e. not prelinked, relative to map->start.
  486. */
  487. pos->start = curr_map->map_ip(curr_map, pos->start);
  488. pos->end = curr_map->map_ip(curr_map, pos->end);
  489. } else if (curr_map != map) {
  490. char dso_name[PATH_MAX];
  491. struct dso *ndso;
  492. if (count == 0) {
  493. curr_map = map;
  494. goto filter_symbol;
  495. }
  496. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  497. snprintf(dso_name, sizeof(dso_name),
  498. "[guest.kernel].%d",
  499. kernel_range++);
  500. else
  501. snprintf(dso_name, sizeof(dso_name),
  502. "[kernel].%d",
  503. kernel_range++);
  504. ndso = dso__new(dso_name);
  505. if (ndso == NULL)
  506. return -1;
  507. ndso->kernel = dso->kernel;
  508. curr_map = map__new2(pos->start, ndso, map->type);
  509. if (curr_map == NULL) {
  510. dso__delete(ndso);
  511. return -1;
  512. }
  513. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  514. map_groups__insert(kmaps, curr_map);
  515. ++kernel_range;
  516. }
  517. filter_symbol:
  518. if (filter && filter(curr_map, pos)) {
  519. discard_symbol: rb_erase(&pos->rb_node, root);
  520. symbol__delete(pos);
  521. } else {
  522. if (curr_map != map) {
  523. rb_erase(&pos->rb_node, root);
  524. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  525. ++moved;
  526. } else
  527. ++count;
  528. }
  529. }
  530. if (curr_map != map &&
  531. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  532. machine__is_default_guest(kmaps->machine)) {
  533. dso__set_loaded(curr_map->dso, curr_map->type);
  534. }
  535. return count + moved;
  536. }
  537. bool symbol__restricted_filename(const char *filename,
  538. const char *restricted_filename)
  539. {
  540. bool restricted = false;
  541. if (symbol_conf.kptr_restrict) {
  542. char *r = realpath(filename, NULL);
  543. if (r != NULL) {
  544. restricted = strcmp(r, restricted_filename) == 0;
  545. free(r);
  546. return restricted;
  547. }
  548. }
  549. return restricted;
  550. }
  551. int dso__load_kallsyms(struct dso *dso, const char *filename,
  552. struct map *map, symbol_filter_t filter)
  553. {
  554. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  555. return -1;
  556. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  557. return -1;
  558. symbols__fixup_duplicate(&dso->symbols[map->type]);
  559. symbols__fixup_end(&dso->symbols[map->type]);
  560. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  561. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  562. else
  563. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  564. return dso__split_kallsyms(dso, map, filter);
  565. }
  566. static int dso__load_perf_map(struct dso *dso, struct map *map,
  567. symbol_filter_t filter)
  568. {
  569. char *line = NULL;
  570. size_t n;
  571. FILE *file;
  572. int nr_syms = 0;
  573. file = fopen(dso->long_name, "r");
  574. if (file == NULL)
  575. goto out_failure;
  576. while (!feof(file)) {
  577. u64 start, size;
  578. struct symbol *sym;
  579. int line_len, len;
  580. line_len = getline(&line, &n, file);
  581. if (line_len < 0)
  582. break;
  583. if (!line)
  584. goto out_failure;
  585. line[--line_len] = '\0'; /* \n */
  586. len = hex2u64(line, &start);
  587. len++;
  588. if (len + 2 >= line_len)
  589. continue;
  590. len += hex2u64(line + len, &size);
  591. len++;
  592. if (len + 2 >= line_len)
  593. continue;
  594. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  595. if (sym == NULL)
  596. goto out_delete_line;
  597. if (filter && filter(map, sym))
  598. symbol__delete(sym);
  599. else {
  600. symbols__insert(&dso->symbols[map->type], sym);
  601. nr_syms++;
  602. }
  603. }
  604. free(line);
  605. fclose(file);
  606. return nr_syms;
  607. out_delete_line:
  608. free(line);
  609. out_failure:
  610. return -1;
  611. }
  612. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  613. {
  614. char *name;
  615. int ret = -1;
  616. u_int i;
  617. struct machine *machine;
  618. char *root_dir = (char *) "";
  619. int ss_pos = 0;
  620. struct symsrc ss_[2];
  621. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  622. dso__set_loaded(dso, map->type);
  623. if (dso->kernel == DSO_TYPE_KERNEL)
  624. return dso__load_kernel_sym(dso, map, filter);
  625. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  626. return dso__load_guest_kernel_sym(dso, map, filter);
  627. if (map->groups && map->groups->machine)
  628. machine = map->groups->machine;
  629. else
  630. machine = NULL;
  631. dso->adjust_symbols = 0;
  632. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  633. struct stat st;
  634. if (lstat(dso->name, &st) < 0)
  635. return -1;
  636. if (st.st_uid && (st.st_uid != geteuid())) {
  637. pr_warning("File %s not owned by current user or root, "
  638. "ignoring it.\n", dso->name);
  639. return -1;
  640. }
  641. ret = dso__load_perf_map(dso, map, filter);
  642. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  643. DSO_BINARY_TYPE__NOT_FOUND;
  644. return ret;
  645. }
  646. if (machine)
  647. root_dir = machine->root_dir;
  648. name = malloc(PATH_MAX);
  649. if (!name)
  650. return -1;
  651. /* Iterate over candidate debug images.
  652. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  653. * and/or opd section) for processing.
  654. */
  655. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  656. struct symsrc *ss = &ss_[ss_pos];
  657. bool next_slot = false;
  658. enum dso_binary_type symtab_type = binary_type_symtab[i];
  659. if (dso__binary_type_file(dso, symtab_type,
  660. root_dir, name, PATH_MAX))
  661. continue;
  662. /* Name is now the name of the next image to try */
  663. if (symsrc__init(ss, dso, name, symtab_type) < 0)
  664. continue;
  665. if (!syms_ss && symsrc__has_symtab(ss)) {
  666. syms_ss = ss;
  667. next_slot = true;
  668. }
  669. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  670. runtime_ss = ss;
  671. next_slot = true;
  672. }
  673. if (next_slot) {
  674. ss_pos++;
  675. if (syms_ss && runtime_ss)
  676. break;
  677. }
  678. }
  679. if (!runtime_ss && !syms_ss)
  680. goto out_free;
  681. if (runtime_ss && !syms_ss) {
  682. syms_ss = runtime_ss;
  683. }
  684. /* We'll have to hope for the best */
  685. if (!runtime_ss && syms_ss)
  686. runtime_ss = syms_ss;
  687. if (syms_ss)
  688. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0);
  689. else
  690. ret = -1;
  691. if (ret > 0) {
  692. int nr_plt;
  693. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
  694. if (nr_plt > 0)
  695. ret += nr_plt;
  696. }
  697. for (; ss_pos > 0; ss_pos--)
  698. symsrc__destroy(&ss_[ss_pos - 1]);
  699. out_free:
  700. free(name);
  701. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  702. return 0;
  703. return ret;
  704. }
  705. struct map *map_groups__find_by_name(struct map_groups *mg,
  706. enum map_type type, const char *name)
  707. {
  708. struct rb_node *nd;
  709. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  710. struct map *map = rb_entry(nd, struct map, rb_node);
  711. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  712. return map;
  713. }
  714. return NULL;
  715. }
  716. int dso__load_vmlinux(struct dso *dso, struct map *map,
  717. const char *vmlinux, symbol_filter_t filter)
  718. {
  719. int err = -1;
  720. struct symsrc ss;
  721. char symfs_vmlinux[PATH_MAX];
  722. enum dso_binary_type symtab_type;
  723. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  724. symbol_conf.symfs, vmlinux);
  725. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  726. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  727. else
  728. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  729. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  730. return -1;
  731. err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
  732. symsrc__destroy(&ss);
  733. if (err > 0) {
  734. dso__set_long_name(dso, (char *)vmlinux);
  735. dso__set_loaded(dso, map->type);
  736. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  737. }
  738. return err;
  739. }
  740. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  741. symbol_filter_t filter)
  742. {
  743. int i, err = 0;
  744. char *filename;
  745. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  746. vmlinux_path__nr_entries + 1);
  747. filename = dso__build_id_filename(dso, NULL, 0);
  748. if (filename != NULL) {
  749. err = dso__load_vmlinux(dso, map, filename, filter);
  750. if (err > 0) {
  751. dso->lname_alloc = 1;
  752. goto out;
  753. }
  754. free(filename);
  755. }
  756. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  757. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  758. if (err > 0) {
  759. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  760. dso->lname_alloc = 1;
  761. break;
  762. }
  763. }
  764. out:
  765. return err;
  766. }
  767. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  768. symbol_filter_t filter)
  769. {
  770. int err;
  771. const char *kallsyms_filename = NULL;
  772. char *kallsyms_allocated_filename = NULL;
  773. /*
  774. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  775. * it and only it, reporting errors to the user if it cannot be used.
  776. *
  777. * For instance, try to analyse an ARM perf.data file _without_ a
  778. * build-id, or if the user specifies the wrong path to the right
  779. * vmlinux file, obviously we can't fallback to another vmlinux (a
  780. * x86_86 one, on the machine where analysis is being performed, say),
  781. * or worse, /proc/kallsyms.
  782. *
  783. * If the specified file _has_ a build-id and there is a build-id
  784. * section in the perf.data file, we will still do the expected
  785. * validation in dso__load_vmlinux and will bail out if they don't
  786. * match.
  787. */
  788. if (symbol_conf.kallsyms_name != NULL) {
  789. kallsyms_filename = symbol_conf.kallsyms_name;
  790. goto do_kallsyms;
  791. }
  792. if (symbol_conf.vmlinux_name != NULL) {
  793. err = dso__load_vmlinux(dso, map,
  794. symbol_conf.vmlinux_name, filter);
  795. if (err > 0) {
  796. dso__set_long_name(dso,
  797. strdup(symbol_conf.vmlinux_name));
  798. dso->lname_alloc = 1;
  799. goto out_fixup;
  800. }
  801. return err;
  802. }
  803. if (vmlinux_path != NULL) {
  804. err = dso__load_vmlinux_path(dso, map, filter);
  805. if (err > 0)
  806. goto out_fixup;
  807. }
  808. /* do not try local files if a symfs was given */
  809. if (symbol_conf.symfs[0] != 0)
  810. return -1;
  811. /*
  812. * Say the kernel DSO was created when processing the build-id header table,
  813. * we have a build-id, so check if it is the same as the running kernel,
  814. * using it if it is.
  815. */
  816. if (dso->has_build_id) {
  817. u8 kallsyms_build_id[BUILD_ID_SIZE];
  818. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  819. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  820. sizeof(kallsyms_build_id)) == 0) {
  821. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  822. kallsyms_filename = "/proc/kallsyms";
  823. goto do_kallsyms;
  824. }
  825. }
  826. /*
  827. * Now look if we have it on the build-id cache in
  828. * $HOME/.debug/[kernel.kallsyms].
  829. */
  830. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  831. sbuild_id);
  832. if (asprintf(&kallsyms_allocated_filename,
  833. "%s/.debug/[kernel.kallsyms]/%s",
  834. getenv("HOME"), sbuild_id) == -1) {
  835. pr_err("Not enough memory for kallsyms file lookup\n");
  836. return -1;
  837. }
  838. kallsyms_filename = kallsyms_allocated_filename;
  839. if (access(kallsyms_filename, F_OK)) {
  840. pr_err("No kallsyms or vmlinux with build-id %s "
  841. "was found\n", sbuild_id);
  842. free(kallsyms_allocated_filename);
  843. return -1;
  844. }
  845. } else {
  846. /*
  847. * Last resort, if we don't have a build-id and couldn't find
  848. * any vmlinux file, try the running kernel kallsyms table.
  849. */
  850. kallsyms_filename = "/proc/kallsyms";
  851. }
  852. do_kallsyms:
  853. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  854. if (err > 0)
  855. pr_debug("Using %s for symbols\n", kallsyms_filename);
  856. free(kallsyms_allocated_filename);
  857. if (err > 0) {
  858. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  859. out_fixup:
  860. map__fixup_start(map);
  861. map__fixup_end(map);
  862. }
  863. return err;
  864. }
  865. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  866. symbol_filter_t filter)
  867. {
  868. int err;
  869. const char *kallsyms_filename = NULL;
  870. struct machine *machine;
  871. char path[PATH_MAX];
  872. if (!map->groups) {
  873. pr_debug("Guest kernel map hasn't the point to groups\n");
  874. return -1;
  875. }
  876. machine = map->groups->machine;
  877. if (machine__is_default_guest(machine)) {
  878. /*
  879. * if the user specified a vmlinux filename, use it and only
  880. * it, reporting errors to the user if it cannot be used.
  881. * Or use file guest_kallsyms inputted by user on commandline
  882. */
  883. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  884. err = dso__load_vmlinux(dso, map,
  885. symbol_conf.default_guest_vmlinux_name, filter);
  886. goto out_try_fixup;
  887. }
  888. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  889. if (!kallsyms_filename)
  890. return -1;
  891. } else {
  892. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  893. kallsyms_filename = path;
  894. }
  895. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  896. if (err > 0)
  897. pr_debug("Using %s for symbols\n", kallsyms_filename);
  898. out_try_fixup:
  899. if (err > 0) {
  900. if (kallsyms_filename != NULL) {
  901. machine__mmap_name(machine, path, sizeof(path));
  902. dso__set_long_name(dso, strdup(path));
  903. }
  904. map__fixup_start(map);
  905. map__fixup_end(map);
  906. }
  907. return err;
  908. }
  909. static void vmlinux_path__exit(void)
  910. {
  911. while (--vmlinux_path__nr_entries >= 0) {
  912. free(vmlinux_path[vmlinux_path__nr_entries]);
  913. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  914. }
  915. free(vmlinux_path);
  916. vmlinux_path = NULL;
  917. }
  918. static int vmlinux_path__init(void)
  919. {
  920. struct utsname uts;
  921. char bf[PATH_MAX];
  922. vmlinux_path = malloc(sizeof(char *) * 5);
  923. if (vmlinux_path == NULL)
  924. return -1;
  925. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  926. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  927. goto out_fail;
  928. ++vmlinux_path__nr_entries;
  929. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  930. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  931. goto out_fail;
  932. ++vmlinux_path__nr_entries;
  933. /* only try running kernel version if no symfs was given */
  934. if (symbol_conf.symfs[0] != 0)
  935. return 0;
  936. if (uname(&uts) < 0)
  937. return -1;
  938. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  939. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  940. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  941. goto out_fail;
  942. ++vmlinux_path__nr_entries;
  943. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  944. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  945. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  946. goto out_fail;
  947. ++vmlinux_path__nr_entries;
  948. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  949. uts.release);
  950. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  951. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  952. goto out_fail;
  953. ++vmlinux_path__nr_entries;
  954. return 0;
  955. out_fail:
  956. vmlinux_path__exit();
  957. return -1;
  958. }
  959. static int setup_list(struct strlist **list, const char *list_str,
  960. const char *list_name)
  961. {
  962. if (list_str == NULL)
  963. return 0;
  964. *list = strlist__new(true, list_str);
  965. if (!*list) {
  966. pr_err("problems parsing %s list\n", list_name);
  967. return -1;
  968. }
  969. return 0;
  970. }
  971. static bool symbol__read_kptr_restrict(void)
  972. {
  973. bool value = false;
  974. if (geteuid() != 0) {
  975. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  976. if (fp != NULL) {
  977. char line[8];
  978. if (fgets(line, sizeof(line), fp) != NULL)
  979. value = atoi(line) != 0;
  980. fclose(fp);
  981. }
  982. }
  983. return value;
  984. }
  985. int symbol__init(void)
  986. {
  987. const char *symfs;
  988. if (symbol_conf.initialized)
  989. return 0;
  990. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  991. symbol__elf_init();
  992. if (symbol_conf.sort_by_name)
  993. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  994. sizeof(struct symbol));
  995. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  996. return -1;
  997. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  998. pr_err("'.' is the only non valid --field-separator argument\n");
  999. return -1;
  1000. }
  1001. if (setup_list(&symbol_conf.dso_list,
  1002. symbol_conf.dso_list_str, "dso") < 0)
  1003. return -1;
  1004. if (setup_list(&symbol_conf.comm_list,
  1005. symbol_conf.comm_list_str, "comm") < 0)
  1006. goto out_free_dso_list;
  1007. if (setup_list(&symbol_conf.sym_list,
  1008. symbol_conf.sym_list_str, "symbol") < 0)
  1009. goto out_free_comm_list;
  1010. /*
  1011. * A path to symbols of "/" is identical to ""
  1012. * reset here for simplicity.
  1013. */
  1014. symfs = realpath(symbol_conf.symfs, NULL);
  1015. if (symfs == NULL)
  1016. symfs = symbol_conf.symfs;
  1017. if (strcmp(symfs, "/") == 0)
  1018. symbol_conf.symfs = "";
  1019. if (symfs != symbol_conf.symfs)
  1020. free((void *)symfs);
  1021. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1022. symbol_conf.initialized = true;
  1023. return 0;
  1024. out_free_comm_list:
  1025. strlist__delete(symbol_conf.comm_list);
  1026. out_free_dso_list:
  1027. strlist__delete(symbol_conf.dso_list);
  1028. return -1;
  1029. }
  1030. void symbol__exit(void)
  1031. {
  1032. if (!symbol_conf.initialized)
  1033. return;
  1034. strlist__delete(symbol_conf.sym_list);
  1035. strlist__delete(symbol_conf.dso_list);
  1036. strlist__delete(symbol_conf.comm_list);
  1037. vmlinux_path__exit();
  1038. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1039. symbol_conf.initialized = false;
  1040. }