symbol.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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. static struct symbol *symbols__first(struct rb_root *symbols)
  273. {
  274. struct rb_node *n = rb_first(symbols);
  275. if (n)
  276. return rb_entry(n, struct symbol, rb_node);
  277. return NULL;
  278. }
  279. struct symbol_name_rb_node {
  280. struct rb_node rb_node;
  281. struct symbol sym;
  282. };
  283. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  284. {
  285. struct rb_node **p = &symbols->rb_node;
  286. struct rb_node *parent = NULL;
  287. struct symbol_name_rb_node *symn, *s;
  288. symn = container_of(sym, struct symbol_name_rb_node, sym);
  289. while (*p != NULL) {
  290. parent = *p;
  291. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  292. if (strcmp(sym->name, s->sym.name) < 0)
  293. p = &(*p)->rb_left;
  294. else
  295. p = &(*p)->rb_right;
  296. }
  297. rb_link_node(&symn->rb_node, parent, p);
  298. rb_insert_color(&symn->rb_node, symbols);
  299. }
  300. static void symbols__sort_by_name(struct rb_root *symbols,
  301. struct rb_root *source)
  302. {
  303. struct rb_node *nd;
  304. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  305. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  306. symbols__insert_by_name(symbols, pos);
  307. }
  308. }
  309. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  310. const char *name)
  311. {
  312. struct rb_node *n;
  313. if (symbols == NULL)
  314. return NULL;
  315. n = symbols->rb_node;
  316. while (n) {
  317. struct symbol_name_rb_node *s;
  318. int cmp;
  319. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  320. cmp = strcmp(name, s->sym.name);
  321. if (cmp < 0)
  322. n = n->rb_left;
  323. else if (cmp > 0)
  324. n = n->rb_right;
  325. else
  326. return &s->sym;
  327. }
  328. return NULL;
  329. }
  330. struct symbol *dso__find_symbol(struct dso *dso,
  331. enum map_type type, u64 addr)
  332. {
  333. return symbols__find(&dso->symbols[type], addr);
  334. }
  335. struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
  336. {
  337. return symbols__first(&dso->symbols[type]);
  338. }
  339. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  340. const char *name)
  341. {
  342. return symbols__find_by_name(&dso->symbol_names[type], name);
  343. }
  344. void dso__sort_by_name(struct dso *dso, enum map_type type)
  345. {
  346. dso__set_sorted_by_name(dso, type);
  347. return symbols__sort_by_name(&dso->symbol_names[type],
  348. &dso->symbols[type]);
  349. }
  350. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  351. enum map_type type, FILE *fp)
  352. {
  353. size_t ret = 0;
  354. struct rb_node *nd;
  355. struct symbol_name_rb_node *pos;
  356. for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
  357. pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  358. fprintf(fp, "%s\n", pos->sym.name);
  359. }
  360. return ret;
  361. }
  362. int kallsyms__parse(const char *filename, void *arg,
  363. int (*process_symbol)(void *arg, const char *name,
  364. char type, u64 start))
  365. {
  366. char *line = NULL;
  367. size_t n;
  368. int err = -1;
  369. FILE *file = fopen(filename, "r");
  370. if (file == NULL)
  371. goto out_failure;
  372. err = 0;
  373. while (!feof(file)) {
  374. u64 start;
  375. int line_len, len;
  376. char symbol_type;
  377. char *symbol_name;
  378. line_len = getline(&line, &n, file);
  379. if (line_len < 0 || !line)
  380. break;
  381. line[--line_len] = '\0'; /* \n */
  382. len = hex2u64(line, &start);
  383. len++;
  384. if (len + 2 >= line_len)
  385. continue;
  386. symbol_type = line[len];
  387. len += 2;
  388. symbol_name = line + len;
  389. len = line_len - len;
  390. if (len >= KSYM_NAME_LEN) {
  391. err = -1;
  392. break;
  393. }
  394. err = process_symbol(arg, symbol_name,
  395. symbol_type, start);
  396. if (err)
  397. break;
  398. }
  399. free(line);
  400. fclose(file);
  401. return err;
  402. out_failure:
  403. return -1;
  404. }
  405. struct process_kallsyms_args {
  406. struct map *map;
  407. struct dso *dso;
  408. };
  409. static u8 kallsyms2elf_type(char type)
  410. {
  411. if (type == 'W')
  412. return STB_WEAK;
  413. return isupper(type) ? STB_GLOBAL : STB_LOCAL;
  414. }
  415. static int map__process_kallsym_symbol(void *arg, const char *name,
  416. char type, u64 start)
  417. {
  418. struct symbol *sym;
  419. struct process_kallsyms_args *a = arg;
  420. struct rb_root *root = &a->dso->symbols[a->map->type];
  421. if (!symbol_type__is_a(type, a->map->type))
  422. return 0;
  423. /*
  424. * module symbols are not sorted so we add all
  425. * symbols, setting length to 0, and rely on
  426. * symbols__fixup_end() to fix it up.
  427. */
  428. sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
  429. if (sym == NULL)
  430. return -ENOMEM;
  431. /*
  432. * We will pass the symbols to the filter later, in
  433. * map__split_kallsyms, when we have split the maps per module
  434. */
  435. symbols__insert(root, sym);
  436. return 0;
  437. }
  438. /*
  439. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  440. * so that we can in the next step set the symbol ->end address and then
  441. * call kernel_maps__split_kallsyms.
  442. */
  443. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  444. struct map *map)
  445. {
  446. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  447. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  448. }
  449. static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
  450. symbol_filter_t filter)
  451. {
  452. struct map_groups *kmaps = map__kmap(map)->kmaps;
  453. struct map *curr_map;
  454. struct symbol *pos;
  455. int count = 0, moved = 0;
  456. struct rb_root *root = &dso->symbols[map->type];
  457. struct rb_node *next = rb_first(root);
  458. while (next) {
  459. char *module;
  460. pos = rb_entry(next, struct symbol, rb_node);
  461. next = rb_next(&pos->rb_node);
  462. module = strchr(pos->name, '\t');
  463. if (module)
  464. *module = '\0';
  465. curr_map = map_groups__find(kmaps, map->type, pos->start);
  466. if (!curr_map || (filter && filter(curr_map, pos))) {
  467. rb_erase(&pos->rb_node, root);
  468. symbol__delete(pos);
  469. } else {
  470. pos->start -= curr_map->start - curr_map->pgoff;
  471. if (pos->end)
  472. pos->end -= curr_map->start - curr_map->pgoff;
  473. if (curr_map != map) {
  474. rb_erase(&pos->rb_node, root);
  475. symbols__insert(
  476. &curr_map->dso->symbols[curr_map->type],
  477. pos);
  478. ++moved;
  479. } else {
  480. ++count;
  481. }
  482. }
  483. }
  484. /* Symbols have been adjusted */
  485. dso->adjust_symbols = 1;
  486. return count + moved;
  487. }
  488. /*
  489. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  490. * kernel range is broken in several maps, named [kernel].N, as we don't have
  491. * the original ELF section names vmlinux have.
  492. */
  493. static int dso__split_kallsyms(struct dso *dso, struct map *map,
  494. symbol_filter_t filter)
  495. {
  496. struct map_groups *kmaps = map__kmap(map)->kmaps;
  497. struct machine *machine = kmaps->machine;
  498. struct map *curr_map = map;
  499. struct symbol *pos;
  500. int count = 0, moved = 0;
  501. struct rb_root *root = &dso->symbols[map->type];
  502. struct rb_node *next = rb_first(root);
  503. int kernel_range = 0;
  504. while (next) {
  505. char *module;
  506. pos = rb_entry(next, struct symbol, rb_node);
  507. next = rb_next(&pos->rb_node);
  508. module = strchr(pos->name, '\t');
  509. if (module) {
  510. if (!symbol_conf.use_modules)
  511. goto discard_symbol;
  512. *module++ = '\0';
  513. if (strcmp(curr_map->dso->short_name, module)) {
  514. if (curr_map != map &&
  515. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  516. machine__is_default_guest(machine)) {
  517. /*
  518. * We assume all symbols of a module are
  519. * continuous in * kallsyms, so curr_map
  520. * points to a module and all its
  521. * symbols are in its kmap. Mark it as
  522. * loaded.
  523. */
  524. dso__set_loaded(curr_map->dso,
  525. curr_map->type);
  526. }
  527. curr_map = map_groups__find_by_name(kmaps,
  528. map->type, module);
  529. if (curr_map == NULL) {
  530. pr_debug("%s/proc/{kallsyms,modules} "
  531. "inconsistency while looking "
  532. "for \"%s\" module!\n",
  533. machine->root_dir, module);
  534. curr_map = map;
  535. goto discard_symbol;
  536. }
  537. if (curr_map->dso->loaded &&
  538. !machine__is_default_guest(machine))
  539. goto discard_symbol;
  540. }
  541. /*
  542. * So that we look just like we get from .ko files,
  543. * i.e. not prelinked, relative to map->start.
  544. */
  545. pos->start = curr_map->map_ip(curr_map, pos->start);
  546. pos->end = curr_map->map_ip(curr_map, pos->end);
  547. } else if (curr_map != map) {
  548. char dso_name[PATH_MAX];
  549. struct dso *ndso;
  550. if (count == 0) {
  551. curr_map = map;
  552. goto filter_symbol;
  553. }
  554. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  555. snprintf(dso_name, sizeof(dso_name),
  556. "[guest.kernel].%d",
  557. kernel_range++);
  558. else
  559. snprintf(dso_name, sizeof(dso_name),
  560. "[kernel].%d",
  561. kernel_range++);
  562. ndso = dso__new(dso_name);
  563. if (ndso == NULL)
  564. return -1;
  565. ndso->kernel = dso->kernel;
  566. curr_map = map__new2(pos->start, ndso, map->type);
  567. if (curr_map == NULL) {
  568. dso__delete(ndso);
  569. return -1;
  570. }
  571. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  572. map_groups__insert(kmaps, curr_map);
  573. ++kernel_range;
  574. }
  575. filter_symbol:
  576. if (filter && filter(curr_map, pos)) {
  577. discard_symbol: rb_erase(&pos->rb_node, root);
  578. symbol__delete(pos);
  579. } else {
  580. if (curr_map != map) {
  581. rb_erase(&pos->rb_node, root);
  582. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  583. ++moved;
  584. } else
  585. ++count;
  586. }
  587. }
  588. if (curr_map != map &&
  589. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  590. machine__is_default_guest(kmaps->machine)) {
  591. dso__set_loaded(curr_map->dso, curr_map->type);
  592. }
  593. return count + moved;
  594. }
  595. bool symbol__restricted_filename(const char *filename,
  596. const char *restricted_filename)
  597. {
  598. bool restricted = false;
  599. if (symbol_conf.kptr_restrict) {
  600. char *r = realpath(filename, NULL);
  601. if (r != NULL) {
  602. restricted = strcmp(r, restricted_filename) == 0;
  603. free(r);
  604. return restricted;
  605. }
  606. }
  607. return restricted;
  608. }
  609. struct kcore_mapfn_data {
  610. struct dso *dso;
  611. enum map_type type;
  612. struct list_head maps;
  613. };
  614. static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
  615. {
  616. struct kcore_mapfn_data *md = data;
  617. struct map *map;
  618. map = map__new2(start, md->dso, md->type);
  619. if (map == NULL)
  620. return -ENOMEM;
  621. map->end = map->start + len;
  622. map->pgoff = pgoff;
  623. list_add(&map->node, &md->maps);
  624. return 0;
  625. }
  626. /*
  627. * If kallsyms is referenced by name then we look for kcore in the same
  628. * directory.
  629. */
  630. static bool kcore_filename_from_kallsyms_filename(char *kcore_filename,
  631. const char *kallsyms_filename)
  632. {
  633. char *name;
  634. strcpy(kcore_filename, kallsyms_filename);
  635. name = strrchr(kcore_filename, '/');
  636. if (!name)
  637. return false;
  638. if (!strcmp(name, "/kallsyms")) {
  639. strcpy(name, "/kcore");
  640. return true;
  641. }
  642. return false;
  643. }
  644. static int dso__load_kcore(struct dso *dso, struct map *map,
  645. const char *kallsyms_filename)
  646. {
  647. struct map_groups *kmaps = map__kmap(map)->kmaps;
  648. struct machine *machine = kmaps->machine;
  649. struct kcore_mapfn_data md;
  650. struct map *old_map, *new_map, *replacement_map = NULL;
  651. bool is_64_bit;
  652. int err, fd;
  653. char kcore_filename[PATH_MAX];
  654. struct symbol *sym;
  655. /* This function requires that the map is the kernel map */
  656. if (map != machine->vmlinux_maps[map->type])
  657. return -EINVAL;
  658. if (!kcore_filename_from_kallsyms_filename(kcore_filename,
  659. kallsyms_filename))
  660. return -EINVAL;
  661. md.dso = dso;
  662. md.type = map->type;
  663. INIT_LIST_HEAD(&md.maps);
  664. fd = open(kcore_filename, O_RDONLY);
  665. if (fd < 0)
  666. return -EINVAL;
  667. /* Read new maps into temporary lists */
  668. err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
  669. &is_64_bit);
  670. if (err)
  671. goto out_err;
  672. if (list_empty(&md.maps)) {
  673. err = -EINVAL;
  674. goto out_err;
  675. }
  676. /* Remove old maps */
  677. old_map = map_groups__first(kmaps, map->type);
  678. while (old_map) {
  679. struct map *next = map_groups__next(old_map);
  680. if (old_map != map)
  681. map_groups__remove(kmaps, old_map);
  682. old_map = next;
  683. }
  684. /* Find the kernel map using the first symbol */
  685. sym = dso__first_symbol(dso, map->type);
  686. list_for_each_entry(new_map, &md.maps, node) {
  687. if (sym && sym->start >= new_map->start &&
  688. sym->start < new_map->end) {
  689. replacement_map = new_map;
  690. break;
  691. }
  692. }
  693. if (!replacement_map)
  694. replacement_map = list_entry(md.maps.next, struct map, node);
  695. /* Add new maps */
  696. while (!list_empty(&md.maps)) {
  697. new_map = list_entry(md.maps.next, struct map, node);
  698. list_del(&new_map->node);
  699. if (new_map == replacement_map) {
  700. map->start = new_map->start;
  701. map->end = new_map->end;
  702. map->pgoff = new_map->pgoff;
  703. map->map_ip = new_map->map_ip;
  704. map->unmap_ip = new_map->unmap_ip;
  705. map__delete(new_map);
  706. /* Ensure maps are correctly ordered */
  707. map_groups__remove(kmaps, map);
  708. map_groups__insert(kmaps, map);
  709. } else {
  710. map_groups__insert(kmaps, new_map);
  711. }
  712. }
  713. /*
  714. * Set the data type and long name so that kcore can be read via
  715. * dso__data_read_addr().
  716. */
  717. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  718. dso->data_type = DSO_BINARY_TYPE__GUEST_KCORE;
  719. else
  720. dso->data_type = DSO_BINARY_TYPE__KCORE;
  721. dso__set_long_name(dso, strdup(kcore_filename));
  722. close(fd);
  723. if (map->type == MAP__FUNCTION)
  724. pr_debug("Using %s for kernel object code\n", kcore_filename);
  725. else
  726. pr_debug("Using %s for kernel data\n", kcore_filename);
  727. return 0;
  728. out_err:
  729. while (!list_empty(&md.maps)) {
  730. map = list_entry(md.maps.next, struct map, node);
  731. list_del(&map->node);
  732. map__delete(map);
  733. }
  734. close(fd);
  735. return -EINVAL;
  736. }
  737. int dso__load_kallsyms(struct dso *dso, const char *filename,
  738. struct map *map, symbol_filter_t filter)
  739. {
  740. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  741. return -1;
  742. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  743. return -1;
  744. symbols__fixup_duplicate(&dso->symbols[map->type]);
  745. symbols__fixup_end(&dso->symbols[map->type]);
  746. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  747. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  748. else
  749. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  750. if (!dso__load_kcore(dso, map, filename))
  751. return dso__split_kallsyms_for_kcore(dso, map, filter);
  752. else
  753. return dso__split_kallsyms(dso, map, filter);
  754. }
  755. static int dso__load_perf_map(struct dso *dso, struct map *map,
  756. symbol_filter_t filter)
  757. {
  758. char *line = NULL;
  759. size_t n;
  760. FILE *file;
  761. int nr_syms = 0;
  762. file = fopen(dso->long_name, "r");
  763. if (file == NULL)
  764. goto out_failure;
  765. while (!feof(file)) {
  766. u64 start, size;
  767. struct symbol *sym;
  768. int line_len, len;
  769. line_len = getline(&line, &n, file);
  770. if (line_len < 0)
  771. break;
  772. if (!line)
  773. goto out_failure;
  774. line[--line_len] = '\0'; /* \n */
  775. len = hex2u64(line, &start);
  776. len++;
  777. if (len + 2 >= line_len)
  778. continue;
  779. len += hex2u64(line + len, &size);
  780. len++;
  781. if (len + 2 >= line_len)
  782. continue;
  783. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  784. if (sym == NULL)
  785. goto out_delete_line;
  786. if (filter && filter(map, sym))
  787. symbol__delete(sym);
  788. else {
  789. symbols__insert(&dso->symbols[map->type], sym);
  790. nr_syms++;
  791. }
  792. }
  793. free(line);
  794. fclose(file);
  795. return nr_syms;
  796. out_delete_line:
  797. free(line);
  798. out_failure:
  799. return -1;
  800. }
  801. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  802. {
  803. char *name;
  804. int ret = -1;
  805. u_int i;
  806. struct machine *machine;
  807. char *root_dir = (char *) "";
  808. int ss_pos = 0;
  809. struct symsrc ss_[2];
  810. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  811. dso__set_loaded(dso, map->type);
  812. if (dso->kernel == DSO_TYPE_KERNEL)
  813. return dso__load_kernel_sym(dso, map, filter);
  814. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  815. return dso__load_guest_kernel_sym(dso, map, filter);
  816. if (map->groups && map->groups->machine)
  817. machine = map->groups->machine;
  818. else
  819. machine = NULL;
  820. dso->adjust_symbols = 0;
  821. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  822. struct stat st;
  823. if (lstat(dso->name, &st) < 0)
  824. return -1;
  825. if (st.st_uid && (st.st_uid != geteuid())) {
  826. pr_warning("File %s not owned by current user or root, "
  827. "ignoring it.\n", dso->name);
  828. return -1;
  829. }
  830. ret = dso__load_perf_map(dso, map, filter);
  831. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  832. DSO_BINARY_TYPE__NOT_FOUND;
  833. return ret;
  834. }
  835. if (machine)
  836. root_dir = machine->root_dir;
  837. name = malloc(PATH_MAX);
  838. if (!name)
  839. return -1;
  840. /* Iterate over candidate debug images.
  841. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  842. * and/or opd section) for processing.
  843. */
  844. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  845. struct symsrc *ss = &ss_[ss_pos];
  846. bool next_slot = false;
  847. enum dso_binary_type symtab_type = binary_type_symtab[i];
  848. if (dso__binary_type_file(dso, symtab_type,
  849. root_dir, name, PATH_MAX))
  850. continue;
  851. /* Name is now the name of the next image to try */
  852. if (symsrc__init(ss, dso, name, symtab_type) < 0)
  853. continue;
  854. if (!syms_ss && symsrc__has_symtab(ss)) {
  855. syms_ss = ss;
  856. next_slot = true;
  857. }
  858. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  859. runtime_ss = ss;
  860. next_slot = true;
  861. }
  862. if (next_slot) {
  863. ss_pos++;
  864. if (syms_ss && runtime_ss)
  865. break;
  866. }
  867. }
  868. if (!runtime_ss && !syms_ss)
  869. goto out_free;
  870. if (runtime_ss && !syms_ss) {
  871. syms_ss = runtime_ss;
  872. }
  873. /* We'll have to hope for the best */
  874. if (!runtime_ss && syms_ss)
  875. runtime_ss = syms_ss;
  876. if (syms_ss) {
  877. int km;
  878. km = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  879. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
  880. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, km);
  881. } else {
  882. ret = -1;
  883. }
  884. if (ret > 0) {
  885. int nr_plt;
  886. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
  887. if (nr_plt > 0)
  888. ret += nr_plt;
  889. }
  890. for (; ss_pos > 0; ss_pos--)
  891. symsrc__destroy(&ss_[ss_pos - 1]);
  892. out_free:
  893. free(name);
  894. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  895. return 0;
  896. return ret;
  897. }
  898. struct map *map_groups__find_by_name(struct map_groups *mg,
  899. enum map_type type, const char *name)
  900. {
  901. struct rb_node *nd;
  902. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  903. struct map *map = rb_entry(nd, struct map, rb_node);
  904. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  905. return map;
  906. }
  907. return NULL;
  908. }
  909. int dso__load_vmlinux(struct dso *dso, struct map *map,
  910. const char *vmlinux, symbol_filter_t filter)
  911. {
  912. int err = -1;
  913. struct symsrc ss;
  914. char symfs_vmlinux[PATH_MAX];
  915. enum dso_binary_type symtab_type;
  916. if (vmlinux[0] == '/')
  917. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
  918. else
  919. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  920. symbol_conf.symfs, vmlinux);
  921. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  922. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  923. else
  924. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  925. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  926. return -1;
  927. err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
  928. symsrc__destroy(&ss);
  929. if (err > 0) {
  930. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  931. dso->data_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  932. else
  933. dso->data_type = DSO_BINARY_TYPE__VMLINUX;
  934. dso__set_long_name(dso, (char *)vmlinux);
  935. dso__set_loaded(dso, map->type);
  936. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  937. }
  938. return err;
  939. }
  940. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  941. symbol_filter_t filter)
  942. {
  943. int i, err = 0;
  944. char *filename;
  945. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  946. vmlinux_path__nr_entries + 1);
  947. filename = dso__build_id_filename(dso, NULL, 0);
  948. if (filename != NULL) {
  949. err = dso__load_vmlinux(dso, map, filename, filter);
  950. if (err > 0) {
  951. dso->lname_alloc = 1;
  952. goto out;
  953. }
  954. free(filename);
  955. }
  956. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  957. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  958. if (err > 0) {
  959. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  960. dso->lname_alloc = 1;
  961. break;
  962. }
  963. }
  964. out:
  965. return err;
  966. }
  967. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  968. symbol_filter_t filter)
  969. {
  970. int err;
  971. const char *kallsyms_filename = NULL;
  972. char *kallsyms_allocated_filename = NULL;
  973. /*
  974. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  975. * it and only it, reporting errors to the user if it cannot be used.
  976. *
  977. * For instance, try to analyse an ARM perf.data file _without_ a
  978. * build-id, or if the user specifies the wrong path to the right
  979. * vmlinux file, obviously we can't fallback to another vmlinux (a
  980. * x86_86 one, on the machine where analysis is being performed, say),
  981. * or worse, /proc/kallsyms.
  982. *
  983. * If the specified file _has_ a build-id and there is a build-id
  984. * section in the perf.data file, we will still do the expected
  985. * validation in dso__load_vmlinux and will bail out if they don't
  986. * match.
  987. */
  988. if (symbol_conf.kallsyms_name != NULL) {
  989. kallsyms_filename = symbol_conf.kallsyms_name;
  990. goto do_kallsyms;
  991. }
  992. if (symbol_conf.vmlinux_name != NULL) {
  993. err = dso__load_vmlinux(dso, map,
  994. symbol_conf.vmlinux_name, filter);
  995. if (err > 0) {
  996. dso__set_long_name(dso,
  997. strdup(symbol_conf.vmlinux_name));
  998. dso->lname_alloc = 1;
  999. return err;
  1000. }
  1001. return err;
  1002. }
  1003. if (vmlinux_path != NULL) {
  1004. err = dso__load_vmlinux_path(dso, map, filter);
  1005. if (err > 0)
  1006. return err;
  1007. }
  1008. /* do not try local files if a symfs was given */
  1009. if (symbol_conf.symfs[0] != 0)
  1010. return -1;
  1011. /*
  1012. * Say the kernel DSO was created when processing the build-id header table,
  1013. * we have a build-id, so check if it is the same as the running kernel,
  1014. * using it if it is.
  1015. */
  1016. if (dso->has_build_id) {
  1017. u8 kallsyms_build_id[BUILD_ID_SIZE];
  1018. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1019. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  1020. sizeof(kallsyms_build_id)) == 0) {
  1021. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  1022. kallsyms_filename = "/proc/kallsyms";
  1023. goto do_kallsyms;
  1024. }
  1025. }
  1026. /*
  1027. * Now look if we have it on the build-id cache in
  1028. * $HOME/.debug/[kernel.kallsyms].
  1029. */
  1030. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  1031. sbuild_id);
  1032. if (asprintf(&kallsyms_allocated_filename,
  1033. "%s/.debug/[kernel.kallsyms]/%s",
  1034. getenv("HOME"), sbuild_id) == -1) {
  1035. pr_err("Not enough memory for kallsyms file lookup\n");
  1036. return -1;
  1037. }
  1038. kallsyms_filename = kallsyms_allocated_filename;
  1039. if (access(kallsyms_filename, F_OK)) {
  1040. pr_err("No kallsyms or vmlinux with build-id %s "
  1041. "was found\n", sbuild_id);
  1042. free(kallsyms_allocated_filename);
  1043. return -1;
  1044. }
  1045. } else {
  1046. /*
  1047. * Last resort, if we don't have a build-id and couldn't find
  1048. * any vmlinux file, try the running kernel kallsyms table.
  1049. */
  1050. kallsyms_filename = "/proc/kallsyms";
  1051. }
  1052. do_kallsyms:
  1053. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1054. if (err > 0)
  1055. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1056. free(kallsyms_allocated_filename);
  1057. if (err > 0 && !dso__is_kcore(dso)) {
  1058. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1059. map__fixup_start(map);
  1060. map__fixup_end(map);
  1061. }
  1062. return err;
  1063. }
  1064. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1065. symbol_filter_t filter)
  1066. {
  1067. int err;
  1068. const char *kallsyms_filename = NULL;
  1069. struct machine *machine;
  1070. char path[PATH_MAX];
  1071. if (!map->groups) {
  1072. pr_debug("Guest kernel map hasn't the point to groups\n");
  1073. return -1;
  1074. }
  1075. machine = map->groups->machine;
  1076. if (machine__is_default_guest(machine)) {
  1077. /*
  1078. * if the user specified a vmlinux filename, use it and only
  1079. * it, reporting errors to the user if it cannot be used.
  1080. * Or use file guest_kallsyms inputted by user on commandline
  1081. */
  1082. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1083. err = dso__load_vmlinux(dso, map,
  1084. symbol_conf.default_guest_vmlinux_name, filter);
  1085. return err;
  1086. }
  1087. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1088. if (!kallsyms_filename)
  1089. return -1;
  1090. } else {
  1091. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1092. kallsyms_filename = path;
  1093. }
  1094. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1095. if (err > 0)
  1096. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1097. if (err > 0 && !dso__is_kcore(dso)) {
  1098. machine__mmap_name(machine, path, sizeof(path));
  1099. dso__set_long_name(dso, strdup(path));
  1100. map__fixup_start(map);
  1101. map__fixup_end(map);
  1102. }
  1103. return err;
  1104. }
  1105. static void vmlinux_path__exit(void)
  1106. {
  1107. while (--vmlinux_path__nr_entries >= 0) {
  1108. free(vmlinux_path[vmlinux_path__nr_entries]);
  1109. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1110. }
  1111. free(vmlinux_path);
  1112. vmlinux_path = NULL;
  1113. }
  1114. static int vmlinux_path__init(void)
  1115. {
  1116. struct utsname uts;
  1117. char bf[PATH_MAX];
  1118. vmlinux_path = malloc(sizeof(char *) * 5);
  1119. if (vmlinux_path == NULL)
  1120. return -1;
  1121. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1122. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1123. goto out_fail;
  1124. ++vmlinux_path__nr_entries;
  1125. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1126. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1127. goto out_fail;
  1128. ++vmlinux_path__nr_entries;
  1129. /* only try running kernel version if no symfs was given */
  1130. if (symbol_conf.symfs[0] != 0)
  1131. return 0;
  1132. if (uname(&uts) < 0)
  1133. return -1;
  1134. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1135. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1136. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1137. goto out_fail;
  1138. ++vmlinux_path__nr_entries;
  1139. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1140. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1141. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1142. goto out_fail;
  1143. ++vmlinux_path__nr_entries;
  1144. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1145. uts.release);
  1146. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1147. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1148. goto out_fail;
  1149. ++vmlinux_path__nr_entries;
  1150. return 0;
  1151. out_fail:
  1152. vmlinux_path__exit();
  1153. return -1;
  1154. }
  1155. static int setup_list(struct strlist **list, const char *list_str,
  1156. const char *list_name)
  1157. {
  1158. if (list_str == NULL)
  1159. return 0;
  1160. *list = strlist__new(true, list_str);
  1161. if (!*list) {
  1162. pr_err("problems parsing %s list\n", list_name);
  1163. return -1;
  1164. }
  1165. return 0;
  1166. }
  1167. static bool symbol__read_kptr_restrict(void)
  1168. {
  1169. bool value = false;
  1170. if (geteuid() != 0) {
  1171. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  1172. if (fp != NULL) {
  1173. char line[8];
  1174. if (fgets(line, sizeof(line), fp) != NULL)
  1175. value = atoi(line) != 0;
  1176. fclose(fp);
  1177. }
  1178. }
  1179. return value;
  1180. }
  1181. int symbol__init(void)
  1182. {
  1183. const char *symfs;
  1184. if (symbol_conf.initialized)
  1185. return 0;
  1186. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  1187. symbol__elf_init();
  1188. if (symbol_conf.sort_by_name)
  1189. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1190. sizeof(struct symbol));
  1191. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1192. return -1;
  1193. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1194. pr_err("'.' is the only non valid --field-separator argument\n");
  1195. return -1;
  1196. }
  1197. if (setup_list(&symbol_conf.dso_list,
  1198. symbol_conf.dso_list_str, "dso") < 0)
  1199. return -1;
  1200. if (setup_list(&symbol_conf.comm_list,
  1201. symbol_conf.comm_list_str, "comm") < 0)
  1202. goto out_free_dso_list;
  1203. if (setup_list(&symbol_conf.sym_list,
  1204. symbol_conf.sym_list_str, "symbol") < 0)
  1205. goto out_free_comm_list;
  1206. /*
  1207. * A path to symbols of "/" is identical to ""
  1208. * reset here for simplicity.
  1209. */
  1210. symfs = realpath(symbol_conf.symfs, NULL);
  1211. if (symfs == NULL)
  1212. symfs = symbol_conf.symfs;
  1213. if (strcmp(symfs, "/") == 0)
  1214. symbol_conf.symfs = "";
  1215. if (symfs != symbol_conf.symfs)
  1216. free((void *)symfs);
  1217. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1218. symbol_conf.initialized = true;
  1219. return 0;
  1220. out_free_comm_list:
  1221. strlist__delete(symbol_conf.comm_list);
  1222. out_free_dso_list:
  1223. strlist__delete(symbol_conf.dso_list);
  1224. return -1;
  1225. }
  1226. void symbol__exit(void)
  1227. {
  1228. if (!symbol_conf.initialized)
  1229. return;
  1230. strlist__delete(symbol_conf.sym_list);
  1231. strlist__delete(symbol_conf.dso_list);
  1232. strlist__delete(symbol_conf.comm_list);
  1233. vmlinux_path__exit();
  1234. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1235. symbol_conf.initialized = false;
  1236. }