symbol.c 34 KB

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