symbol.c 34 KB

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