symbol.c 29 KB

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