symbol.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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 <symbol/kallsyms.h>
  21. #include <sys/utsname.h>
  22. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  23. symbol_filter_t filter);
  24. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  25. symbol_filter_t filter);
  26. int vmlinux_path__nr_entries;
  27. char **vmlinux_path;
  28. struct symbol_conf symbol_conf = {
  29. .use_modules = true,
  30. .try_vmlinux_path = true,
  31. .annotate_src = true,
  32. .demangle = true,
  33. .symfs = "",
  34. };
  35. static enum dso_binary_type binary_type_symtab[] = {
  36. DSO_BINARY_TYPE__KALLSYMS,
  37. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  38. DSO_BINARY_TYPE__JAVA_JIT,
  39. DSO_BINARY_TYPE__DEBUGLINK,
  40. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  41. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  42. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  43. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  44. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  45. DSO_BINARY_TYPE__GUEST_KMODULE,
  46. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  47. DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO,
  48. DSO_BINARY_TYPE__NOT_FOUND,
  49. };
  50. #define DSO_BINARY_TYPE__SYMTAB_CNT ARRAY_SIZE(binary_type_symtab)
  51. bool symbol_type__is_a(char symbol_type, enum map_type map_type)
  52. {
  53. symbol_type = toupper(symbol_type);
  54. switch (map_type) {
  55. case MAP__FUNCTION:
  56. return symbol_type == 'T' || symbol_type == 'W';
  57. case MAP__VARIABLE:
  58. return symbol_type == 'D';
  59. default:
  60. return false;
  61. }
  62. }
  63. static int prefix_underscores_count(const char *str)
  64. {
  65. const char *tail = str;
  66. while (*tail == '_')
  67. tail++;
  68. return tail - str;
  69. }
  70. #define SYMBOL_A 0
  71. #define SYMBOL_B 1
  72. static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
  73. {
  74. s64 a;
  75. s64 b;
  76. size_t na, nb;
  77. /* Prefer a symbol with non zero length */
  78. a = syma->end - syma->start;
  79. b = symb->end - symb->start;
  80. if ((b == 0) && (a > 0))
  81. return SYMBOL_A;
  82. else if ((a == 0) && (b > 0))
  83. return SYMBOL_B;
  84. /* Prefer a non weak symbol over a weak one */
  85. a = syma->binding == STB_WEAK;
  86. b = symb->binding == STB_WEAK;
  87. if (b && !a)
  88. return SYMBOL_A;
  89. if (a && !b)
  90. return SYMBOL_B;
  91. /* Prefer a global symbol over a non global one */
  92. a = syma->binding == STB_GLOBAL;
  93. b = symb->binding == STB_GLOBAL;
  94. if (a && !b)
  95. return SYMBOL_A;
  96. if (b && !a)
  97. return SYMBOL_B;
  98. /* Prefer a symbol with less underscores */
  99. a = prefix_underscores_count(syma->name);
  100. b = prefix_underscores_count(symb->name);
  101. if (b > a)
  102. return SYMBOL_A;
  103. else if (a > b)
  104. return SYMBOL_B;
  105. /* Choose the symbol with the longest name */
  106. na = strlen(syma->name);
  107. nb = strlen(symb->name);
  108. if (na > nb)
  109. return SYMBOL_A;
  110. else if (na < nb)
  111. return SYMBOL_B;
  112. /* Avoid "SyS" kernel syscall aliases */
  113. if (na >= 3 && !strncmp(syma->name, "SyS", 3))
  114. return SYMBOL_B;
  115. if (na >= 10 && !strncmp(syma->name, "compat_SyS", 10))
  116. return SYMBOL_B;
  117. return SYMBOL_A;
  118. }
  119. void symbols__fixup_duplicate(struct rb_root *symbols)
  120. {
  121. struct rb_node *nd;
  122. struct symbol *curr, *next;
  123. nd = rb_first(symbols);
  124. while (nd) {
  125. curr = rb_entry(nd, struct symbol, rb_node);
  126. again:
  127. nd = rb_next(&curr->rb_node);
  128. next = rb_entry(nd, struct symbol, rb_node);
  129. if (!nd)
  130. break;
  131. if (curr->start != next->start)
  132. continue;
  133. if (choose_best_symbol(curr, next) == SYMBOL_A) {
  134. rb_erase(&next->rb_node, symbols);
  135. symbol__delete(next);
  136. goto again;
  137. } else {
  138. nd = rb_next(&curr->rb_node);
  139. rb_erase(&curr->rb_node, symbols);
  140. symbol__delete(curr);
  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 modules__parse(const char *filename, void *arg,
  367. int (*process_module)(void *arg, const char *name,
  368. u64 start))
  369. {
  370. char *line = NULL;
  371. size_t n;
  372. FILE *file;
  373. int err = 0;
  374. file = fopen(filename, "r");
  375. if (file == NULL)
  376. return -1;
  377. while (1) {
  378. char name[PATH_MAX];
  379. u64 start;
  380. char *sep;
  381. ssize_t line_len;
  382. line_len = getline(&line, &n, file);
  383. if (line_len < 0) {
  384. if (feof(file))
  385. break;
  386. err = -1;
  387. goto out;
  388. }
  389. if (!line) {
  390. err = -1;
  391. goto out;
  392. }
  393. line[--line_len] = '\0'; /* \n */
  394. sep = strrchr(line, 'x');
  395. if (sep == NULL)
  396. continue;
  397. hex2u64(sep + 1, &start);
  398. sep = strchr(line, ' ');
  399. if (sep == NULL)
  400. continue;
  401. *sep = '\0';
  402. scnprintf(name, sizeof(name), "[%s]", line);
  403. err = process_module(arg, name, start);
  404. if (err)
  405. break;
  406. }
  407. out:
  408. free(line);
  409. fclose(file);
  410. return err;
  411. }
  412. struct process_kallsyms_args {
  413. struct map *map;
  414. struct dso *dso;
  415. };
  416. bool symbol__is_idle(struct symbol *sym)
  417. {
  418. const char * const idle_symbols[] = {
  419. "cpu_idle",
  420. "intel_idle",
  421. "default_idle",
  422. "native_safe_halt",
  423. "enter_idle",
  424. "exit_idle",
  425. "mwait_idle",
  426. "mwait_idle_with_hints",
  427. "poll_idle",
  428. "ppc64_runlatch_off",
  429. "pseries_dedicated_idle_sleep",
  430. NULL
  431. };
  432. int i;
  433. if (!sym)
  434. return false;
  435. for (i = 0; idle_symbols[i]; i++) {
  436. if (!strcmp(idle_symbols[i], sym->name))
  437. return true;
  438. }
  439. return false;
  440. }
  441. static int map__process_kallsym_symbol(void *arg, const char *name,
  442. char type, u64 start)
  443. {
  444. struct symbol *sym;
  445. struct process_kallsyms_args *a = arg;
  446. struct rb_root *root = &a->dso->symbols[a->map->type];
  447. if (!symbol_type__is_a(type, a->map->type))
  448. return 0;
  449. /*
  450. * module symbols are not sorted so we add all
  451. * symbols, setting length to 0, and rely on
  452. * symbols__fixup_end() to fix it up.
  453. */
  454. sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
  455. if (sym == NULL)
  456. return -ENOMEM;
  457. /*
  458. * We will pass the symbols to the filter later, in
  459. * map__split_kallsyms, when we have split the maps per module
  460. */
  461. symbols__insert(root, sym);
  462. return 0;
  463. }
  464. /*
  465. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  466. * so that we can in the next step set the symbol ->end address and then
  467. * call kernel_maps__split_kallsyms.
  468. */
  469. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  470. struct map *map)
  471. {
  472. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  473. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  474. }
  475. static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
  476. symbol_filter_t filter)
  477. {
  478. struct map_groups *kmaps = map__kmap(map)->kmaps;
  479. struct map *curr_map;
  480. struct symbol *pos;
  481. int count = 0, moved = 0;
  482. struct rb_root *root = &dso->symbols[map->type];
  483. struct rb_node *next = rb_first(root);
  484. while (next) {
  485. char *module;
  486. pos = rb_entry(next, struct symbol, rb_node);
  487. next = rb_next(&pos->rb_node);
  488. module = strchr(pos->name, '\t');
  489. if (module)
  490. *module = '\0';
  491. curr_map = map_groups__find(kmaps, map->type, pos->start);
  492. if (!curr_map || (filter && filter(curr_map, pos))) {
  493. rb_erase(&pos->rb_node, root);
  494. symbol__delete(pos);
  495. } else {
  496. pos->start -= curr_map->start - curr_map->pgoff;
  497. if (pos->end)
  498. pos->end -= curr_map->start - curr_map->pgoff;
  499. if (curr_map != map) {
  500. rb_erase(&pos->rb_node, root);
  501. symbols__insert(
  502. &curr_map->dso->symbols[curr_map->type],
  503. pos);
  504. ++moved;
  505. } else {
  506. ++count;
  507. }
  508. }
  509. }
  510. /* Symbols have been adjusted */
  511. dso->adjust_symbols = 1;
  512. return count + moved;
  513. }
  514. /*
  515. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  516. * kernel range is broken in several maps, named [kernel].N, as we don't have
  517. * the original ELF section names vmlinux have.
  518. */
  519. static int dso__split_kallsyms(struct dso *dso, struct map *map, u64 delta,
  520. symbol_filter_t filter)
  521. {
  522. struct map_groups *kmaps = map__kmap(map)->kmaps;
  523. struct machine *machine = kmaps->machine;
  524. struct map *curr_map = map;
  525. struct symbol *pos;
  526. int count = 0, moved = 0;
  527. struct rb_root *root = &dso->symbols[map->type];
  528. struct rb_node *next = rb_first(root);
  529. int kernel_range = 0;
  530. while (next) {
  531. char *module;
  532. pos = rb_entry(next, struct symbol, rb_node);
  533. next = rb_next(&pos->rb_node);
  534. module = strchr(pos->name, '\t');
  535. if (module) {
  536. if (!symbol_conf.use_modules)
  537. goto discard_symbol;
  538. *module++ = '\0';
  539. if (strcmp(curr_map->dso->short_name, module)) {
  540. if (curr_map != map &&
  541. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  542. machine__is_default_guest(machine)) {
  543. /*
  544. * We assume all symbols of a module are
  545. * continuous in * kallsyms, so curr_map
  546. * points to a module and all its
  547. * symbols are in its kmap. Mark it as
  548. * loaded.
  549. */
  550. dso__set_loaded(curr_map->dso,
  551. curr_map->type);
  552. }
  553. curr_map = map_groups__find_by_name(kmaps,
  554. map->type, module);
  555. if (curr_map == NULL) {
  556. pr_debug("%s/proc/{kallsyms,modules} "
  557. "inconsistency while looking "
  558. "for \"%s\" module!\n",
  559. machine->root_dir, module);
  560. curr_map = map;
  561. goto discard_symbol;
  562. }
  563. if (curr_map->dso->loaded &&
  564. !machine__is_default_guest(machine))
  565. goto discard_symbol;
  566. }
  567. /*
  568. * So that we look just like we get from .ko files,
  569. * i.e. not prelinked, relative to map->start.
  570. */
  571. pos->start = curr_map->map_ip(curr_map, pos->start);
  572. pos->end = curr_map->map_ip(curr_map, pos->end);
  573. } else if (curr_map != map) {
  574. char dso_name[PATH_MAX];
  575. struct dso *ndso;
  576. if (delta) {
  577. /* Kernel was relocated at boot time */
  578. pos->start -= delta;
  579. pos->end -= delta;
  580. }
  581. if (count == 0) {
  582. curr_map = map;
  583. goto filter_symbol;
  584. }
  585. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  586. snprintf(dso_name, sizeof(dso_name),
  587. "[guest.kernel].%d",
  588. kernel_range++);
  589. else
  590. snprintf(dso_name, sizeof(dso_name),
  591. "[kernel].%d",
  592. kernel_range++);
  593. ndso = dso__new(dso_name);
  594. if (ndso == NULL)
  595. return -1;
  596. ndso->kernel = dso->kernel;
  597. curr_map = map__new2(pos->start, ndso, map->type);
  598. if (curr_map == NULL) {
  599. dso__delete(ndso);
  600. return -1;
  601. }
  602. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  603. map_groups__insert(kmaps, curr_map);
  604. ++kernel_range;
  605. } else if (delta) {
  606. /* Kernel was relocated at boot time */
  607. pos->start -= delta;
  608. pos->end -= delta;
  609. }
  610. filter_symbol:
  611. if (filter && filter(curr_map, pos)) {
  612. discard_symbol: rb_erase(&pos->rb_node, root);
  613. symbol__delete(pos);
  614. } else {
  615. if (curr_map != map) {
  616. rb_erase(&pos->rb_node, root);
  617. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  618. ++moved;
  619. } else
  620. ++count;
  621. }
  622. }
  623. if (curr_map != map &&
  624. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  625. machine__is_default_guest(kmaps->machine)) {
  626. dso__set_loaded(curr_map->dso, curr_map->type);
  627. }
  628. return count + moved;
  629. }
  630. bool symbol__restricted_filename(const char *filename,
  631. const char *restricted_filename)
  632. {
  633. bool restricted = false;
  634. if (symbol_conf.kptr_restrict) {
  635. char *r = realpath(filename, NULL);
  636. if (r != NULL) {
  637. restricted = strcmp(r, restricted_filename) == 0;
  638. free(r);
  639. return restricted;
  640. }
  641. }
  642. return restricted;
  643. }
  644. struct module_info {
  645. struct rb_node rb_node;
  646. char *name;
  647. u64 start;
  648. };
  649. static void add_module(struct module_info *mi, struct rb_root *modules)
  650. {
  651. struct rb_node **p = &modules->rb_node;
  652. struct rb_node *parent = NULL;
  653. struct module_info *m;
  654. while (*p != NULL) {
  655. parent = *p;
  656. m = rb_entry(parent, struct module_info, rb_node);
  657. if (strcmp(mi->name, m->name) < 0)
  658. p = &(*p)->rb_left;
  659. else
  660. p = &(*p)->rb_right;
  661. }
  662. rb_link_node(&mi->rb_node, parent, p);
  663. rb_insert_color(&mi->rb_node, modules);
  664. }
  665. static void delete_modules(struct rb_root *modules)
  666. {
  667. struct module_info *mi;
  668. struct rb_node *next = rb_first(modules);
  669. while (next) {
  670. mi = rb_entry(next, struct module_info, rb_node);
  671. next = rb_next(&mi->rb_node);
  672. rb_erase(&mi->rb_node, modules);
  673. zfree(&mi->name);
  674. free(mi);
  675. }
  676. }
  677. static struct module_info *find_module(const char *name,
  678. struct rb_root *modules)
  679. {
  680. struct rb_node *n = modules->rb_node;
  681. while (n) {
  682. struct module_info *m;
  683. int cmp;
  684. m = rb_entry(n, struct module_info, rb_node);
  685. cmp = strcmp(name, m->name);
  686. if (cmp < 0)
  687. n = n->rb_left;
  688. else if (cmp > 0)
  689. n = n->rb_right;
  690. else
  691. return m;
  692. }
  693. return NULL;
  694. }
  695. static int __read_proc_modules(void *arg, const char *name, u64 start)
  696. {
  697. struct rb_root *modules = arg;
  698. struct module_info *mi;
  699. mi = zalloc(sizeof(struct module_info));
  700. if (!mi)
  701. return -ENOMEM;
  702. mi->name = strdup(name);
  703. mi->start = start;
  704. if (!mi->name) {
  705. free(mi);
  706. return -ENOMEM;
  707. }
  708. add_module(mi, modules);
  709. return 0;
  710. }
  711. static int read_proc_modules(const char *filename, struct rb_root *modules)
  712. {
  713. if (symbol__restricted_filename(filename, "/proc/modules"))
  714. return -1;
  715. if (modules__parse(filename, modules, __read_proc_modules)) {
  716. delete_modules(modules);
  717. return -1;
  718. }
  719. return 0;
  720. }
  721. int compare_proc_modules(const char *from, const char *to)
  722. {
  723. struct rb_root from_modules = RB_ROOT;
  724. struct rb_root to_modules = RB_ROOT;
  725. struct rb_node *from_node, *to_node;
  726. struct module_info *from_m, *to_m;
  727. int ret = -1;
  728. if (read_proc_modules(from, &from_modules))
  729. return -1;
  730. if (read_proc_modules(to, &to_modules))
  731. goto out_delete_from;
  732. from_node = rb_first(&from_modules);
  733. to_node = rb_first(&to_modules);
  734. while (from_node) {
  735. if (!to_node)
  736. break;
  737. from_m = rb_entry(from_node, struct module_info, rb_node);
  738. to_m = rb_entry(to_node, struct module_info, rb_node);
  739. if (from_m->start != to_m->start ||
  740. strcmp(from_m->name, to_m->name))
  741. break;
  742. from_node = rb_next(from_node);
  743. to_node = rb_next(to_node);
  744. }
  745. if (!from_node && !to_node)
  746. ret = 0;
  747. delete_modules(&to_modules);
  748. out_delete_from:
  749. delete_modules(&from_modules);
  750. return ret;
  751. }
  752. static int do_validate_kcore_modules(const char *filename, struct map *map,
  753. struct map_groups *kmaps)
  754. {
  755. struct rb_root modules = RB_ROOT;
  756. struct map *old_map;
  757. int err;
  758. err = read_proc_modules(filename, &modules);
  759. if (err)
  760. return err;
  761. old_map = map_groups__first(kmaps, map->type);
  762. while (old_map) {
  763. struct map *next = map_groups__next(old_map);
  764. struct module_info *mi;
  765. if (old_map == map || old_map->start == map->start) {
  766. /* The kernel map */
  767. old_map = next;
  768. continue;
  769. }
  770. /* Module must be in memory at the same address */
  771. mi = find_module(old_map->dso->short_name, &modules);
  772. if (!mi || mi->start != old_map->start) {
  773. err = -EINVAL;
  774. goto out;
  775. }
  776. old_map = next;
  777. }
  778. out:
  779. delete_modules(&modules);
  780. return err;
  781. }
  782. /*
  783. * If kallsyms is referenced by name then we look for filename in the same
  784. * directory.
  785. */
  786. static bool filename_from_kallsyms_filename(char *filename,
  787. const char *base_name,
  788. const char *kallsyms_filename)
  789. {
  790. char *name;
  791. strcpy(filename, kallsyms_filename);
  792. name = strrchr(filename, '/');
  793. if (!name)
  794. return false;
  795. name += 1;
  796. if (!strcmp(name, "kallsyms")) {
  797. strcpy(name, base_name);
  798. return true;
  799. }
  800. return false;
  801. }
  802. static int validate_kcore_modules(const char *kallsyms_filename,
  803. struct map *map)
  804. {
  805. struct map_groups *kmaps = map__kmap(map)->kmaps;
  806. char modules_filename[PATH_MAX];
  807. if (!filename_from_kallsyms_filename(modules_filename, "modules",
  808. kallsyms_filename))
  809. return -EINVAL;
  810. if (do_validate_kcore_modules(modules_filename, map, kmaps))
  811. return -EINVAL;
  812. return 0;
  813. }
  814. static int validate_kcore_addresses(const char *kallsyms_filename,
  815. struct map *map)
  816. {
  817. struct kmap *kmap = map__kmap(map);
  818. if (kmap->ref_reloc_sym && kmap->ref_reloc_sym->name) {
  819. u64 start;
  820. start = kallsyms__get_function_start(kallsyms_filename,
  821. kmap->ref_reloc_sym->name);
  822. if (start != kmap->ref_reloc_sym->addr)
  823. return -EINVAL;
  824. }
  825. return validate_kcore_modules(kallsyms_filename, map);
  826. }
  827. struct kcore_mapfn_data {
  828. struct dso *dso;
  829. enum map_type type;
  830. struct list_head maps;
  831. };
  832. static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
  833. {
  834. struct kcore_mapfn_data *md = data;
  835. struct map *map;
  836. map = map__new2(start, md->dso, md->type);
  837. if (map == NULL)
  838. return -ENOMEM;
  839. map->end = map->start + len;
  840. map->pgoff = pgoff;
  841. list_add(&map->node, &md->maps);
  842. return 0;
  843. }
  844. static int dso__load_kcore(struct dso *dso, struct map *map,
  845. const char *kallsyms_filename)
  846. {
  847. struct map_groups *kmaps = map__kmap(map)->kmaps;
  848. struct machine *machine = kmaps->machine;
  849. struct kcore_mapfn_data md;
  850. struct map *old_map, *new_map, *replacement_map = NULL;
  851. bool is_64_bit;
  852. int err, fd;
  853. char kcore_filename[PATH_MAX];
  854. struct symbol *sym;
  855. /* This function requires that the map is the kernel map */
  856. if (map != machine->vmlinux_maps[map->type])
  857. return -EINVAL;
  858. if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
  859. kallsyms_filename))
  860. return -EINVAL;
  861. /* Modules and kernel must be present at their original addresses */
  862. if (validate_kcore_addresses(kallsyms_filename, map))
  863. return -EINVAL;
  864. md.dso = dso;
  865. md.type = map->type;
  866. INIT_LIST_HEAD(&md.maps);
  867. fd = open(kcore_filename, O_RDONLY);
  868. if (fd < 0)
  869. return -EINVAL;
  870. /* Read new maps into temporary lists */
  871. err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
  872. &is_64_bit);
  873. if (err)
  874. goto out_err;
  875. if (list_empty(&md.maps)) {
  876. err = -EINVAL;
  877. goto out_err;
  878. }
  879. /* Remove old maps */
  880. old_map = map_groups__first(kmaps, map->type);
  881. while (old_map) {
  882. struct map *next = map_groups__next(old_map);
  883. if (old_map != map)
  884. map_groups__remove(kmaps, old_map);
  885. old_map = next;
  886. }
  887. /* Find the kernel map using the first symbol */
  888. sym = dso__first_symbol(dso, map->type);
  889. list_for_each_entry(new_map, &md.maps, node) {
  890. if (sym && sym->start >= new_map->start &&
  891. sym->start < new_map->end) {
  892. replacement_map = new_map;
  893. break;
  894. }
  895. }
  896. if (!replacement_map)
  897. replacement_map = list_entry(md.maps.next, struct map, node);
  898. /* Add new maps */
  899. while (!list_empty(&md.maps)) {
  900. new_map = list_entry(md.maps.next, struct map, node);
  901. list_del(&new_map->node);
  902. if (new_map == replacement_map) {
  903. map->start = new_map->start;
  904. map->end = new_map->end;
  905. map->pgoff = new_map->pgoff;
  906. map->map_ip = new_map->map_ip;
  907. map->unmap_ip = new_map->unmap_ip;
  908. map__delete(new_map);
  909. /* Ensure maps are correctly ordered */
  910. map_groups__remove(kmaps, map);
  911. map_groups__insert(kmaps, map);
  912. } else {
  913. map_groups__insert(kmaps, new_map);
  914. }
  915. }
  916. /*
  917. * Set the data type and long name so that kcore can be read via
  918. * dso__data_read_addr().
  919. */
  920. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  921. dso->binary_type = DSO_BINARY_TYPE__GUEST_KCORE;
  922. else
  923. dso->binary_type = DSO_BINARY_TYPE__KCORE;
  924. dso__set_long_name(dso, strdup(kcore_filename), true);
  925. close(fd);
  926. if (map->type == MAP__FUNCTION)
  927. pr_debug("Using %s for kernel object code\n", kcore_filename);
  928. else
  929. pr_debug("Using %s for kernel data\n", kcore_filename);
  930. return 0;
  931. out_err:
  932. while (!list_empty(&md.maps)) {
  933. map = list_entry(md.maps.next, struct map, node);
  934. list_del(&map->node);
  935. map__delete(map);
  936. }
  937. close(fd);
  938. return -EINVAL;
  939. }
  940. /*
  941. * If the kernel is relocated at boot time, kallsyms won't match. Compute the
  942. * delta based on the relocation reference symbol.
  943. */
  944. static int kallsyms__delta(struct map *map, const char *filename, u64 *delta)
  945. {
  946. struct kmap *kmap = map__kmap(map);
  947. u64 addr;
  948. if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->name)
  949. return 0;
  950. addr = kallsyms__get_function_start(filename,
  951. kmap->ref_reloc_sym->name);
  952. if (!addr)
  953. return -1;
  954. *delta = addr - kmap->ref_reloc_sym->addr;
  955. return 0;
  956. }
  957. int dso__load_kallsyms(struct dso *dso, const char *filename,
  958. struct map *map, symbol_filter_t filter)
  959. {
  960. u64 delta = 0;
  961. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  962. return -1;
  963. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  964. return -1;
  965. if (kallsyms__delta(map, filename, &delta))
  966. return -1;
  967. symbols__fixup_duplicate(&dso->symbols[map->type]);
  968. symbols__fixup_end(&dso->symbols[map->type]);
  969. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  970. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  971. else
  972. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  973. if (!dso__load_kcore(dso, map, filename))
  974. return dso__split_kallsyms_for_kcore(dso, map, filter);
  975. else
  976. return dso__split_kallsyms(dso, map, delta, filter);
  977. }
  978. static int dso__load_perf_map(struct dso *dso, struct map *map,
  979. symbol_filter_t filter)
  980. {
  981. char *line = NULL;
  982. size_t n;
  983. FILE *file;
  984. int nr_syms = 0;
  985. file = fopen(dso->long_name, "r");
  986. if (file == NULL)
  987. goto out_failure;
  988. while (!feof(file)) {
  989. u64 start, size;
  990. struct symbol *sym;
  991. int line_len, len;
  992. line_len = getline(&line, &n, file);
  993. if (line_len < 0)
  994. break;
  995. if (!line)
  996. goto out_failure;
  997. line[--line_len] = '\0'; /* \n */
  998. len = hex2u64(line, &start);
  999. len++;
  1000. if (len + 2 >= line_len)
  1001. continue;
  1002. len += hex2u64(line + len, &size);
  1003. len++;
  1004. if (len + 2 >= line_len)
  1005. continue;
  1006. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  1007. if (sym == NULL)
  1008. goto out_delete_line;
  1009. if (filter && filter(map, sym))
  1010. symbol__delete(sym);
  1011. else {
  1012. symbols__insert(&dso->symbols[map->type], sym);
  1013. nr_syms++;
  1014. }
  1015. }
  1016. free(line);
  1017. fclose(file);
  1018. return nr_syms;
  1019. out_delete_line:
  1020. free(line);
  1021. out_failure:
  1022. return -1;
  1023. }
  1024. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  1025. {
  1026. char *name;
  1027. int ret = -1;
  1028. u_int i;
  1029. struct machine *machine;
  1030. char *root_dir = (char *) "";
  1031. int ss_pos = 0;
  1032. struct symsrc ss_[2];
  1033. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  1034. dso__set_loaded(dso, map->type);
  1035. if (dso->kernel == DSO_TYPE_KERNEL)
  1036. return dso__load_kernel_sym(dso, map, filter);
  1037. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1038. return dso__load_guest_kernel_sym(dso, map, filter);
  1039. if (map->groups && map->groups->machine)
  1040. machine = map->groups->machine;
  1041. else
  1042. machine = NULL;
  1043. dso->adjust_symbols = 0;
  1044. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  1045. struct stat st;
  1046. if (lstat(dso->name, &st) < 0)
  1047. return -1;
  1048. if (st.st_uid && (st.st_uid != geteuid())) {
  1049. pr_warning("File %s not owned by current user or root, "
  1050. "ignoring it.\n", dso->name);
  1051. return -1;
  1052. }
  1053. ret = dso__load_perf_map(dso, map, filter);
  1054. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  1055. DSO_BINARY_TYPE__NOT_FOUND;
  1056. return ret;
  1057. }
  1058. if (machine)
  1059. root_dir = machine->root_dir;
  1060. name = malloc(PATH_MAX);
  1061. if (!name)
  1062. return -1;
  1063. /* Iterate over candidate debug images.
  1064. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  1065. * and/or opd section) for processing.
  1066. */
  1067. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  1068. struct symsrc *ss = &ss_[ss_pos];
  1069. bool next_slot = false;
  1070. enum dso_binary_type symtab_type = binary_type_symtab[i];
  1071. if (dso__read_binary_type_filename(dso, symtab_type,
  1072. root_dir, name, PATH_MAX))
  1073. continue;
  1074. /* Name is now the name of the next image to try */
  1075. if (symsrc__init(ss, dso, name, symtab_type) < 0)
  1076. continue;
  1077. if (!syms_ss && symsrc__has_symtab(ss)) {
  1078. syms_ss = ss;
  1079. next_slot = true;
  1080. if (!dso->symsrc_filename)
  1081. dso->symsrc_filename = strdup(name);
  1082. }
  1083. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  1084. runtime_ss = ss;
  1085. next_slot = true;
  1086. }
  1087. if (next_slot) {
  1088. ss_pos++;
  1089. if (syms_ss && runtime_ss)
  1090. break;
  1091. } else {
  1092. symsrc__destroy(ss);
  1093. }
  1094. }
  1095. if (!runtime_ss && !syms_ss)
  1096. goto out_free;
  1097. if (runtime_ss && !syms_ss) {
  1098. syms_ss = runtime_ss;
  1099. }
  1100. /* We'll have to hope for the best */
  1101. if (!runtime_ss && syms_ss)
  1102. runtime_ss = syms_ss;
  1103. if (syms_ss) {
  1104. int km;
  1105. km = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  1106. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
  1107. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, km);
  1108. } else {
  1109. ret = -1;
  1110. }
  1111. if (ret > 0) {
  1112. int nr_plt;
  1113. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
  1114. if (nr_plt > 0)
  1115. ret += nr_plt;
  1116. }
  1117. for (; ss_pos > 0; ss_pos--)
  1118. symsrc__destroy(&ss_[ss_pos - 1]);
  1119. out_free:
  1120. free(name);
  1121. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1122. return 0;
  1123. return ret;
  1124. }
  1125. struct map *map_groups__find_by_name(struct map_groups *mg,
  1126. enum map_type type, const char *name)
  1127. {
  1128. struct rb_node *nd;
  1129. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  1130. struct map *map = rb_entry(nd, struct map, rb_node);
  1131. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1132. return map;
  1133. }
  1134. return NULL;
  1135. }
  1136. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1137. const char *vmlinux, bool vmlinux_allocated,
  1138. symbol_filter_t filter)
  1139. {
  1140. int err = -1;
  1141. struct symsrc ss;
  1142. char symfs_vmlinux[PATH_MAX];
  1143. enum dso_binary_type symtab_type;
  1144. if (vmlinux[0] == '/')
  1145. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
  1146. else
  1147. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1148. symbol_conf.symfs, vmlinux);
  1149. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1150. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1151. else
  1152. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  1153. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  1154. return -1;
  1155. err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
  1156. symsrc__destroy(&ss);
  1157. if (err > 0) {
  1158. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1159. dso->binary_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1160. else
  1161. dso->binary_type = DSO_BINARY_TYPE__VMLINUX;
  1162. dso__set_long_name(dso, vmlinux, vmlinux_allocated);
  1163. dso__set_loaded(dso, map->type);
  1164. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1165. }
  1166. return err;
  1167. }
  1168. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1169. symbol_filter_t filter)
  1170. {
  1171. int i, err = 0;
  1172. char *filename;
  1173. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1174. vmlinux_path__nr_entries + 1);
  1175. filename = dso__build_id_filename(dso, NULL, 0);
  1176. if (filename != NULL) {
  1177. err = dso__load_vmlinux(dso, map, filename, true, filter);
  1178. if (err > 0)
  1179. goto out;
  1180. free(filename);
  1181. }
  1182. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1183. err = dso__load_vmlinux(dso, map, vmlinux_path[i], false, filter);
  1184. if (err > 0)
  1185. break;
  1186. }
  1187. out:
  1188. return err;
  1189. }
  1190. static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
  1191. {
  1192. char kallsyms_filename[PATH_MAX];
  1193. struct dirent *dent;
  1194. int ret = -1;
  1195. DIR *d;
  1196. d = opendir(dir);
  1197. if (!d)
  1198. return -1;
  1199. while (1) {
  1200. dent = readdir(d);
  1201. if (!dent)
  1202. break;
  1203. if (dent->d_type != DT_DIR)
  1204. continue;
  1205. scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
  1206. "%s/%s/kallsyms", dir, dent->d_name);
  1207. if (!validate_kcore_addresses(kallsyms_filename, map)) {
  1208. strlcpy(dir, kallsyms_filename, dir_sz);
  1209. ret = 0;
  1210. break;
  1211. }
  1212. }
  1213. closedir(d);
  1214. return ret;
  1215. }
  1216. static char *dso__find_kallsyms(struct dso *dso, struct map *map)
  1217. {
  1218. u8 host_build_id[BUILD_ID_SIZE];
  1219. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1220. bool is_host = false;
  1221. char path[PATH_MAX];
  1222. if (!dso->has_build_id) {
  1223. /*
  1224. * Last resort, if we don't have a build-id and couldn't find
  1225. * any vmlinux file, try the running kernel kallsyms table.
  1226. */
  1227. goto proc_kallsyms;
  1228. }
  1229. if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
  1230. sizeof(host_build_id)) == 0)
  1231. is_host = dso__build_id_equal(dso, host_build_id);
  1232. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  1233. scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s", buildid_dir,
  1234. sbuild_id);
  1235. /* Use /proc/kallsyms if possible */
  1236. if (is_host) {
  1237. DIR *d;
  1238. int fd;
  1239. /* If no cached kcore go with /proc/kallsyms */
  1240. d = opendir(path);
  1241. if (!d)
  1242. goto proc_kallsyms;
  1243. closedir(d);
  1244. /*
  1245. * Do not check the build-id cache, until we know we cannot use
  1246. * /proc/kcore.
  1247. */
  1248. fd = open("/proc/kcore", O_RDONLY);
  1249. if (fd != -1) {
  1250. close(fd);
  1251. /* If module maps match go with /proc/kallsyms */
  1252. if (!validate_kcore_addresses("/proc/kallsyms", map))
  1253. goto proc_kallsyms;
  1254. }
  1255. /* Find kallsyms in build-id cache with kcore */
  1256. if (!find_matching_kcore(map, path, sizeof(path)))
  1257. return strdup(path);
  1258. goto proc_kallsyms;
  1259. }
  1260. /* Find kallsyms in build-id cache with kcore */
  1261. if (!find_matching_kcore(map, path, sizeof(path)))
  1262. return strdup(path);
  1263. scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
  1264. buildid_dir, sbuild_id);
  1265. if (access(path, F_OK)) {
  1266. pr_err("No kallsyms or vmlinux with build-id %s was found\n",
  1267. sbuild_id);
  1268. return NULL;
  1269. }
  1270. return strdup(path);
  1271. proc_kallsyms:
  1272. return strdup("/proc/kallsyms");
  1273. }
  1274. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1275. symbol_filter_t filter)
  1276. {
  1277. int err;
  1278. const char *kallsyms_filename = NULL;
  1279. char *kallsyms_allocated_filename = NULL;
  1280. /*
  1281. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1282. * it and only it, reporting errors to the user if it cannot be used.
  1283. *
  1284. * For instance, try to analyse an ARM perf.data file _without_ a
  1285. * build-id, or if the user specifies the wrong path to the right
  1286. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1287. * x86_86 one, on the machine where analysis is being performed, say),
  1288. * or worse, /proc/kallsyms.
  1289. *
  1290. * If the specified file _has_ a build-id and there is a build-id
  1291. * section in the perf.data file, we will still do the expected
  1292. * validation in dso__load_vmlinux and will bail out if they don't
  1293. * match.
  1294. */
  1295. if (symbol_conf.kallsyms_name != NULL) {
  1296. kallsyms_filename = symbol_conf.kallsyms_name;
  1297. goto do_kallsyms;
  1298. }
  1299. if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
  1300. return dso__load_vmlinux(dso, map, symbol_conf.vmlinux_name,
  1301. false, filter);
  1302. }
  1303. if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
  1304. err = dso__load_vmlinux_path(dso, map, filter);
  1305. if (err > 0)
  1306. return err;
  1307. }
  1308. /* do not try local files if a symfs was given */
  1309. if (symbol_conf.symfs[0] != 0)
  1310. return -1;
  1311. kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
  1312. if (!kallsyms_allocated_filename)
  1313. return -1;
  1314. kallsyms_filename = kallsyms_allocated_filename;
  1315. do_kallsyms:
  1316. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1317. if (err > 0)
  1318. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1319. free(kallsyms_allocated_filename);
  1320. if (err > 0 && !dso__is_kcore(dso)) {
  1321. dso__set_long_name(dso, "[kernel.kallsyms]", false);
  1322. map__fixup_start(map);
  1323. map__fixup_end(map);
  1324. }
  1325. return err;
  1326. }
  1327. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1328. symbol_filter_t filter)
  1329. {
  1330. int err;
  1331. const char *kallsyms_filename = NULL;
  1332. struct machine *machine;
  1333. char path[PATH_MAX];
  1334. if (!map->groups) {
  1335. pr_debug("Guest kernel map hasn't the point to groups\n");
  1336. return -1;
  1337. }
  1338. machine = map->groups->machine;
  1339. if (machine__is_default_guest(machine)) {
  1340. /*
  1341. * if the user specified a vmlinux filename, use it and only
  1342. * it, reporting errors to the user if it cannot be used.
  1343. * Or use file guest_kallsyms inputted by user on commandline
  1344. */
  1345. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1346. err = dso__load_vmlinux(dso, map,
  1347. symbol_conf.default_guest_vmlinux_name,
  1348. false, filter);
  1349. return err;
  1350. }
  1351. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1352. if (!kallsyms_filename)
  1353. return -1;
  1354. } else {
  1355. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1356. kallsyms_filename = path;
  1357. }
  1358. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1359. if (err > 0)
  1360. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1361. if (err > 0 && !dso__is_kcore(dso)) {
  1362. machine__mmap_name(machine, path, sizeof(path));
  1363. dso__set_long_name(dso, strdup(path), true);
  1364. map__fixup_start(map);
  1365. map__fixup_end(map);
  1366. }
  1367. return err;
  1368. }
  1369. static void vmlinux_path__exit(void)
  1370. {
  1371. while (--vmlinux_path__nr_entries >= 0)
  1372. zfree(&vmlinux_path[vmlinux_path__nr_entries]);
  1373. zfree(&vmlinux_path);
  1374. }
  1375. static int vmlinux_path__init(void)
  1376. {
  1377. struct utsname uts;
  1378. char bf[PATH_MAX];
  1379. vmlinux_path = malloc(sizeof(char *) * 5);
  1380. if (vmlinux_path == NULL)
  1381. return -1;
  1382. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1383. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1384. goto out_fail;
  1385. ++vmlinux_path__nr_entries;
  1386. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1387. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1388. goto out_fail;
  1389. ++vmlinux_path__nr_entries;
  1390. /* only try running kernel version if no symfs was given */
  1391. if (symbol_conf.symfs[0] != 0)
  1392. return 0;
  1393. if (uname(&uts) < 0)
  1394. return -1;
  1395. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1396. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1397. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1398. goto out_fail;
  1399. ++vmlinux_path__nr_entries;
  1400. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1401. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1402. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1403. goto out_fail;
  1404. ++vmlinux_path__nr_entries;
  1405. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1406. uts.release);
  1407. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1408. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1409. goto out_fail;
  1410. ++vmlinux_path__nr_entries;
  1411. return 0;
  1412. out_fail:
  1413. vmlinux_path__exit();
  1414. return -1;
  1415. }
  1416. int setup_list(struct strlist **list, const char *list_str,
  1417. const char *list_name)
  1418. {
  1419. if (list_str == NULL)
  1420. return 0;
  1421. *list = strlist__new(true, list_str);
  1422. if (!*list) {
  1423. pr_err("problems parsing %s list\n", list_name);
  1424. return -1;
  1425. }
  1426. return 0;
  1427. }
  1428. static bool symbol__read_kptr_restrict(void)
  1429. {
  1430. bool value = false;
  1431. if (geteuid() != 0) {
  1432. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  1433. if (fp != NULL) {
  1434. char line[8];
  1435. if (fgets(line, sizeof(line), fp) != NULL)
  1436. value = atoi(line) != 0;
  1437. fclose(fp);
  1438. }
  1439. }
  1440. return value;
  1441. }
  1442. int symbol__init(void)
  1443. {
  1444. const char *symfs;
  1445. if (symbol_conf.initialized)
  1446. return 0;
  1447. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  1448. symbol__elf_init();
  1449. if (symbol_conf.sort_by_name)
  1450. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1451. sizeof(struct symbol));
  1452. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1453. return -1;
  1454. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1455. pr_err("'.' is the only non valid --field-separator argument\n");
  1456. return -1;
  1457. }
  1458. if (setup_list(&symbol_conf.dso_list,
  1459. symbol_conf.dso_list_str, "dso") < 0)
  1460. return -1;
  1461. if (setup_list(&symbol_conf.comm_list,
  1462. symbol_conf.comm_list_str, "comm") < 0)
  1463. goto out_free_dso_list;
  1464. if (setup_list(&symbol_conf.sym_list,
  1465. symbol_conf.sym_list_str, "symbol") < 0)
  1466. goto out_free_comm_list;
  1467. /*
  1468. * A path to symbols of "/" is identical to ""
  1469. * reset here for simplicity.
  1470. */
  1471. symfs = realpath(symbol_conf.symfs, NULL);
  1472. if (symfs == NULL)
  1473. symfs = symbol_conf.symfs;
  1474. if (strcmp(symfs, "/") == 0)
  1475. symbol_conf.symfs = "";
  1476. if (symfs != symbol_conf.symfs)
  1477. free((void *)symfs);
  1478. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1479. symbol_conf.initialized = true;
  1480. return 0;
  1481. out_free_comm_list:
  1482. strlist__delete(symbol_conf.comm_list);
  1483. out_free_dso_list:
  1484. strlist__delete(symbol_conf.dso_list);
  1485. return -1;
  1486. }
  1487. void symbol__exit(void)
  1488. {
  1489. if (!symbol_conf.initialized)
  1490. return;
  1491. strlist__delete(symbol_conf.sym_list);
  1492. strlist__delete(symbol_conf.dso_list);
  1493. strlist__delete(symbol_conf.comm_list);
  1494. vmlinux_path__exit();
  1495. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1496. symbol_conf.initialized = false;
  1497. }