symbol.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  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. symbol__delete(next);
  138. goto again;
  139. } else {
  140. nd = rb_next(&curr->rb_node);
  141. rb_erase(&curr->rb_node, symbols);
  142. symbol__delete(curr);
  143. }
  144. }
  145. }
  146. void symbols__fixup_end(struct rb_root *symbols)
  147. {
  148. struct rb_node *nd, *prevnd = rb_first(symbols);
  149. struct symbol *curr, *prev;
  150. if (prevnd == NULL)
  151. return;
  152. curr = rb_entry(prevnd, struct symbol, rb_node);
  153. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  154. prev = curr;
  155. curr = rb_entry(nd, struct symbol, rb_node);
  156. if (prev->end == prev->start && prev->end != curr->start)
  157. prev->end = curr->start - 1;
  158. }
  159. /* Last entry */
  160. if (curr->end == curr->start)
  161. curr->end = roundup(curr->start, 4096);
  162. }
  163. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
  164. {
  165. struct map *prev, *curr;
  166. struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
  167. if (prevnd == NULL)
  168. return;
  169. curr = rb_entry(prevnd, struct map, rb_node);
  170. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  171. prev = curr;
  172. curr = rb_entry(nd, struct map, rb_node);
  173. prev->end = curr->start - 1;
  174. }
  175. /*
  176. * We still haven't the actual symbols, so guess the
  177. * last map final address.
  178. */
  179. curr->end = ~0ULL;
  180. }
  181. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
  182. {
  183. size_t namelen = strlen(name) + 1;
  184. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  185. sizeof(*sym) + namelen));
  186. if (sym == NULL)
  187. return NULL;
  188. if (symbol_conf.priv_size)
  189. sym = ((void *)sym) + symbol_conf.priv_size;
  190. sym->start = start;
  191. sym->end = len ? start + len - 1 : start;
  192. sym->binding = binding;
  193. sym->namelen = namelen - 1;
  194. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  195. __func__, name, start, sym->end);
  196. memcpy(sym->name, name, namelen);
  197. return sym;
  198. }
  199. void symbol__delete(struct symbol *sym)
  200. {
  201. free(((void *)sym) - symbol_conf.priv_size);
  202. }
  203. size_t symbol__fprintf(struct symbol *sym, FILE *fp)
  204. {
  205. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
  206. sym->start, sym->end,
  207. sym->binding == STB_GLOBAL ? 'g' :
  208. sym->binding == STB_LOCAL ? 'l' : 'w',
  209. sym->name);
  210. }
  211. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  212. const struct addr_location *al, FILE *fp)
  213. {
  214. unsigned long offset;
  215. size_t length;
  216. if (sym && sym->name) {
  217. length = fprintf(fp, "%s", sym->name);
  218. if (al) {
  219. if (al->addr < sym->end)
  220. offset = al->addr - sym->start;
  221. else
  222. offset = al->addr - al->map->start - sym->start;
  223. length += fprintf(fp, "+0x%lx", offset);
  224. }
  225. return length;
  226. } else
  227. return fprintf(fp, "[unknown]");
  228. }
  229. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
  230. {
  231. return symbol__fprintf_symname_offs(sym, NULL, fp);
  232. }
  233. void symbols__delete(struct rb_root *symbols)
  234. {
  235. struct symbol *pos;
  236. struct rb_node *next = rb_first(symbols);
  237. while (next) {
  238. pos = rb_entry(next, struct symbol, rb_node);
  239. next = rb_next(&pos->rb_node);
  240. rb_erase(&pos->rb_node, symbols);
  241. symbol__delete(pos);
  242. }
  243. }
  244. void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  245. {
  246. struct rb_node **p = &symbols->rb_node;
  247. struct rb_node *parent = NULL;
  248. const u64 ip = sym->start;
  249. struct symbol *s;
  250. while (*p != NULL) {
  251. parent = *p;
  252. s = rb_entry(parent, struct symbol, rb_node);
  253. if (ip < s->start)
  254. p = &(*p)->rb_left;
  255. else
  256. p = &(*p)->rb_right;
  257. }
  258. rb_link_node(&sym->rb_node, parent, p);
  259. rb_insert_color(&sym->rb_node, symbols);
  260. }
  261. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  262. {
  263. struct rb_node *n;
  264. if (symbols == NULL)
  265. return NULL;
  266. n = symbols->rb_node;
  267. while (n) {
  268. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  269. if (ip < s->start)
  270. n = n->rb_left;
  271. else if (ip > s->end)
  272. n = n->rb_right;
  273. else
  274. return s;
  275. }
  276. return NULL;
  277. }
  278. static struct symbol *symbols__first(struct rb_root *symbols)
  279. {
  280. struct rb_node *n = rb_first(symbols);
  281. if (n)
  282. return rb_entry(n, struct symbol, rb_node);
  283. return NULL;
  284. }
  285. struct symbol_name_rb_node {
  286. struct rb_node rb_node;
  287. struct symbol sym;
  288. };
  289. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  290. {
  291. struct rb_node **p = &symbols->rb_node;
  292. struct rb_node *parent = NULL;
  293. struct symbol_name_rb_node *symn, *s;
  294. symn = container_of(sym, struct symbol_name_rb_node, sym);
  295. while (*p != NULL) {
  296. parent = *p;
  297. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  298. if (strcmp(sym->name, s->sym.name) < 0)
  299. p = &(*p)->rb_left;
  300. else
  301. p = &(*p)->rb_right;
  302. }
  303. rb_link_node(&symn->rb_node, parent, p);
  304. rb_insert_color(&symn->rb_node, symbols);
  305. }
  306. static void symbols__sort_by_name(struct rb_root *symbols,
  307. struct rb_root *source)
  308. {
  309. struct rb_node *nd;
  310. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  311. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  312. symbols__insert_by_name(symbols, pos);
  313. }
  314. }
  315. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  316. const char *name)
  317. {
  318. struct rb_node *n;
  319. if (symbols == NULL)
  320. return NULL;
  321. n = symbols->rb_node;
  322. while (n) {
  323. struct symbol_name_rb_node *s;
  324. int cmp;
  325. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  326. cmp = strcmp(name, s->sym.name);
  327. if (cmp < 0)
  328. n = n->rb_left;
  329. else if (cmp > 0)
  330. n = n->rb_right;
  331. else
  332. return &s->sym;
  333. }
  334. return NULL;
  335. }
  336. struct symbol *dso__find_symbol(struct dso *dso,
  337. enum map_type type, u64 addr)
  338. {
  339. return symbols__find(&dso->symbols[type], addr);
  340. }
  341. struct symbol *dso__first_symbol(struct dso *dso, enum map_type type)
  342. {
  343. return symbols__first(&dso->symbols[type]);
  344. }
  345. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  346. const char *name)
  347. {
  348. return symbols__find_by_name(&dso->symbol_names[type], name);
  349. }
  350. void dso__sort_by_name(struct dso *dso, enum map_type type)
  351. {
  352. dso__set_sorted_by_name(dso, type);
  353. return symbols__sort_by_name(&dso->symbol_names[type],
  354. &dso->symbols[type]);
  355. }
  356. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  357. enum map_type type, FILE *fp)
  358. {
  359. size_t ret = 0;
  360. struct rb_node *nd;
  361. struct symbol_name_rb_node *pos;
  362. for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
  363. pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  364. fprintf(fp, "%s\n", pos->sym.name);
  365. }
  366. return ret;
  367. }
  368. int kallsyms__parse(const char *filename, void *arg,
  369. int (*process_symbol)(void *arg, const char *name,
  370. char type, u64 start))
  371. {
  372. char *line = NULL;
  373. size_t n;
  374. int err = -1;
  375. FILE *file = fopen(filename, "r");
  376. if (file == NULL)
  377. goto out_failure;
  378. err = 0;
  379. while (!feof(file)) {
  380. u64 start;
  381. int line_len, len;
  382. char symbol_type;
  383. char *symbol_name;
  384. line_len = getline(&line, &n, file);
  385. if (line_len < 0 || !line)
  386. break;
  387. line[--line_len] = '\0'; /* \n */
  388. len = hex2u64(line, &start);
  389. len++;
  390. if (len + 2 >= line_len)
  391. continue;
  392. symbol_type = line[len];
  393. len += 2;
  394. symbol_name = line + len;
  395. len = line_len - len;
  396. if (len >= KSYM_NAME_LEN) {
  397. err = -1;
  398. break;
  399. }
  400. err = process_symbol(arg, symbol_name,
  401. symbol_type, start);
  402. if (err)
  403. break;
  404. }
  405. free(line);
  406. fclose(file);
  407. return err;
  408. out_failure:
  409. return -1;
  410. }
  411. int modules__parse(const char *filename, void *arg,
  412. int (*process_module)(void *arg, const char *name,
  413. u64 start))
  414. {
  415. char *line = NULL;
  416. size_t n;
  417. FILE *file;
  418. int err = 0;
  419. file = fopen(filename, "r");
  420. if (file == NULL)
  421. return -1;
  422. while (1) {
  423. char name[PATH_MAX];
  424. u64 start;
  425. char *sep;
  426. ssize_t line_len;
  427. line_len = getline(&line, &n, file);
  428. if (line_len < 0) {
  429. if (feof(file))
  430. break;
  431. err = -1;
  432. goto out;
  433. }
  434. if (!line) {
  435. err = -1;
  436. goto out;
  437. }
  438. line[--line_len] = '\0'; /* \n */
  439. sep = strrchr(line, 'x');
  440. if (sep == NULL)
  441. continue;
  442. hex2u64(sep + 1, &start);
  443. sep = strchr(line, ' ');
  444. if (sep == NULL)
  445. continue;
  446. *sep = '\0';
  447. scnprintf(name, sizeof(name), "[%s]", line);
  448. err = process_module(arg, name, start);
  449. if (err)
  450. break;
  451. }
  452. out:
  453. free(line);
  454. fclose(file);
  455. return err;
  456. }
  457. struct process_kallsyms_args {
  458. struct map *map;
  459. struct dso *dso;
  460. };
  461. static u8 kallsyms2elf_type(char type)
  462. {
  463. if (type == 'W')
  464. return STB_WEAK;
  465. return isupper(type) ? STB_GLOBAL : STB_LOCAL;
  466. }
  467. static int map__process_kallsym_symbol(void *arg, const char *name,
  468. char type, u64 start)
  469. {
  470. struct symbol *sym;
  471. struct process_kallsyms_args *a = arg;
  472. struct rb_root *root = &a->dso->symbols[a->map->type];
  473. if (!symbol_type__is_a(type, a->map->type))
  474. return 0;
  475. /*
  476. * module symbols are not sorted so we add all
  477. * symbols, setting length to 0, and rely on
  478. * symbols__fixup_end() to fix it up.
  479. */
  480. sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
  481. if (sym == NULL)
  482. return -ENOMEM;
  483. /*
  484. * We will pass the symbols to the filter later, in
  485. * map__split_kallsyms, when we have split the maps per module
  486. */
  487. symbols__insert(root, sym);
  488. return 0;
  489. }
  490. /*
  491. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  492. * so that we can in the next step set the symbol ->end address and then
  493. * call kernel_maps__split_kallsyms.
  494. */
  495. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  496. struct map *map)
  497. {
  498. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  499. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  500. }
  501. static int dso__split_kallsyms_for_kcore(struct dso *dso, struct map *map,
  502. symbol_filter_t filter)
  503. {
  504. struct map_groups *kmaps = map__kmap(map)->kmaps;
  505. struct map *curr_map;
  506. struct symbol *pos;
  507. int count = 0, moved = 0;
  508. struct rb_root *root = &dso->symbols[map->type];
  509. struct rb_node *next = rb_first(root);
  510. while (next) {
  511. char *module;
  512. pos = rb_entry(next, struct symbol, rb_node);
  513. next = rb_next(&pos->rb_node);
  514. module = strchr(pos->name, '\t');
  515. if (module)
  516. *module = '\0';
  517. curr_map = map_groups__find(kmaps, map->type, pos->start);
  518. if (!curr_map || (filter && filter(curr_map, pos))) {
  519. rb_erase(&pos->rb_node, root);
  520. symbol__delete(pos);
  521. } else {
  522. pos->start -= curr_map->start - curr_map->pgoff;
  523. if (pos->end)
  524. pos->end -= curr_map->start - curr_map->pgoff;
  525. if (curr_map != map) {
  526. rb_erase(&pos->rb_node, root);
  527. symbols__insert(
  528. &curr_map->dso->symbols[curr_map->type],
  529. pos);
  530. ++moved;
  531. } else {
  532. ++count;
  533. }
  534. }
  535. }
  536. /* Symbols have been adjusted */
  537. dso->adjust_symbols = 1;
  538. return count + moved;
  539. }
  540. /*
  541. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  542. * kernel range is broken in several maps, named [kernel].N, as we don't have
  543. * the original ELF section names vmlinux have.
  544. */
  545. static int dso__split_kallsyms(struct dso *dso, struct map *map,
  546. symbol_filter_t filter)
  547. {
  548. struct map_groups *kmaps = map__kmap(map)->kmaps;
  549. struct machine *machine = kmaps->machine;
  550. struct map *curr_map = map;
  551. struct symbol *pos;
  552. int count = 0, moved = 0;
  553. struct rb_root *root = &dso->symbols[map->type];
  554. struct rb_node *next = rb_first(root);
  555. int kernel_range = 0;
  556. while (next) {
  557. char *module;
  558. pos = rb_entry(next, struct symbol, rb_node);
  559. next = rb_next(&pos->rb_node);
  560. module = strchr(pos->name, '\t');
  561. if (module) {
  562. if (!symbol_conf.use_modules)
  563. goto discard_symbol;
  564. *module++ = '\0';
  565. if (strcmp(curr_map->dso->short_name, module)) {
  566. if (curr_map != map &&
  567. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  568. machine__is_default_guest(machine)) {
  569. /*
  570. * We assume all symbols of a module are
  571. * continuous in * kallsyms, so curr_map
  572. * points to a module and all its
  573. * symbols are in its kmap. Mark it as
  574. * loaded.
  575. */
  576. dso__set_loaded(curr_map->dso,
  577. curr_map->type);
  578. }
  579. curr_map = map_groups__find_by_name(kmaps,
  580. map->type, module);
  581. if (curr_map == NULL) {
  582. pr_debug("%s/proc/{kallsyms,modules} "
  583. "inconsistency while looking "
  584. "for \"%s\" module!\n",
  585. machine->root_dir, module);
  586. curr_map = map;
  587. goto discard_symbol;
  588. }
  589. if (curr_map->dso->loaded &&
  590. !machine__is_default_guest(machine))
  591. goto discard_symbol;
  592. }
  593. /*
  594. * So that we look just like we get from .ko files,
  595. * i.e. not prelinked, relative to map->start.
  596. */
  597. pos->start = curr_map->map_ip(curr_map, pos->start);
  598. pos->end = curr_map->map_ip(curr_map, pos->end);
  599. } else if (curr_map != map) {
  600. char dso_name[PATH_MAX];
  601. struct dso *ndso;
  602. if (count == 0) {
  603. curr_map = map;
  604. goto filter_symbol;
  605. }
  606. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  607. snprintf(dso_name, sizeof(dso_name),
  608. "[guest.kernel].%d",
  609. kernel_range++);
  610. else
  611. snprintf(dso_name, sizeof(dso_name),
  612. "[kernel].%d",
  613. kernel_range++);
  614. ndso = dso__new(dso_name);
  615. if (ndso == NULL)
  616. return -1;
  617. ndso->kernel = dso->kernel;
  618. curr_map = map__new2(pos->start, ndso, map->type);
  619. if (curr_map == NULL) {
  620. dso__delete(ndso);
  621. return -1;
  622. }
  623. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  624. map_groups__insert(kmaps, curr_map);
  625. ++kernel_range;
  626. }
  627. filter_symbol:
  628. if (filter && filter(curr_map, pos)) {
  629. discard_symbol: rb_erase(&pos->rb_node, root);
  630. symbol__delete(pos);
  631. } else {
  632. if (curr_map != map) {
  633. rb_erase(&pos->rb_node, root);
  634. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  635. ++moved;
  636. } else
  637. ++count;
  638. }
  639. }
  640. if (curr_map != map &&
  641. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  642. machine__is_default_guest(kmaps->machine)) {
  643. dso__set_loaded(curr_map->dso, curr_map->type);
  644. }
  645. return count + moved;
  646. }
  647. bool symbol__restricted_filename(const char *filename,
  648. const char *restricted_filename)
  649. {
  650. bool restricted = false;
  651. if (symbol_conf.kptr_restrict) {
  652. char *r = realpath(filename, NULL);
  653. if (r != NULL) {
  654. restricted = strcmp(r, restricted_filename) == 0;
  655. free(r);
  656. return restricted;
  657. }
  658. }
  659. return restricted;
  660. }
  661. struct module_info {
  662. struct rb_node rb_node;
  663. char *name;
  664. u64 start;
  665. };
  666. static void add_module(struct module_info *mi, struct rb_root *modules)
  667. {
  668. struct rb_node **p = &modules->rb_node;
  669. struct rb_node *parent = NULL;
  670. struct module_info *m;
  671. while (*p != NULL) {
  672. parent = *p;
  673. m = rb_entry(parent, struct module_info, rb_node);
  674. if (strcmp(mi->name, m->name) < 0)
  675. p = &(*p)->rb_left;
  676. else
  677. p = &(*p)->rb_right;
  678. }
  679. rb_link_node(&mi->rb_node, parent, p);
  680. rb_insert_color(&mi->rb_node, modules);
  681. }
  682. static void delete_modules(struct rb_root *modules)
  683. {
  684. struct module_info *mi;
  685. struct rb_node *next = rb_first(modules);
  686. while (next) {
  687. mi = rb_entry(next, struct module_info, rb_node);
  688. next = rb_next(&mi->rb_node);
  689. rb_erase(&mi->rb_node, modules);
  690. free(mi->name);
  691. free(mi);
  692. }
  693. }
  694. static struct module_info *find_module(const char *name,
  695. struct rb_root *modules)
  696. {
  697. struct rb_node *n = modules->rb_node;
  698. while (n) {
  699. struct module_info *m;
  700. int cmp;
  701. m = rb_entry(n, struct module_info, rb_node);
  702. cmp = strcmp(name, m->name);
  703. if (cmp < 0)
  704. n = n->rb_left;
  705. else if (cmp > 0)
  706. n = n->rb_right;
  707. else
  708. return m;
  709. }
  710. return NULL;
  711. }
  712. static int __read_proc_modules(void *arg, const char *name, u64 start)
  713. {
  714. struct rb_root *modules = arg;
  715. struct module_info *mi;
  716. mi = zalloc(sizeof(struct module_info));
  717. if (!mi)
  718. return -ENOMEM;
  719. mi->name = strdup(name);
  720. mi->start = start;
  721. if (!mi->name) {
  722. free(mi);
  723. return -ENOMEM;
  724. }
  725. add_module(mi, modules);
  726. return 0;
  727. }
  728. static int read_proc_modules(const char *filename, struct rb_root *modules)
  729. {
  730. if (symbol__restricted_filename(filename, "/proc/modules"))
  731. return -1;
  732. if (modules__parse(filename, modules, __read_proc_modules)) {
  733. delete_modules(modules);
  734. return -1;
  735. }
  736. return 0;
  737. }
  738. int compare_proc_modules(const char *from, const char *to)
  739. {
  740. struct rb_root from_modules = RB_ROOT;
  741. struct rb_root to_modules = RB_ROOT;
  742. struct rb_node *from_node, *to_node;
  743. struct module_info *from_m, *to_m;
  744. int ret = -1;
  745. if (read_proc_modules(from, &from_modules))
  746. return -1;
  747. if (read_proc_modules(to, &to_modules))
  748. goto out_delete_from;
  749. from_node = rb_first(&from_modules);
  750. to_node = rb_first(&to_modules);
  751. while (from_node) {
  752. if (!to_node)
  753. break;
  754. from_m = rb_entry(from_node, struct module_info, rb_node);
  755. to_m = rb_entry(to_node, struct module_info, rb_node);
  756. if (from_m->start != to_m->start ||
  757. strcmp(from_m->name, to_m->name))
  758. break;
  759. from_node = rb_next(from_node);
  760. to_node = rb_next(to_node);
  761. }
  762. if (!from_node && !to_node)
  763. ret = 0;
  764. delete_modules(&to_modules);
  765. out_delete_from:
  766. delete_modules(&from_modules);
  767. return ret;
  768. }
  769. static int do_validate_kcore_modules(const char *filename, struct map *map,
  770. struct map_groups *kmaps)
  771. {
  772. struct rb_root modules = RB_ROOT;
  773. struct map *old_map;
  774. int err;
  775. err = read_proc_modules(filename, &modules);
  776. if (err)
  777. return err;
  778. old_map = map_groups__first(kmaps, map->type);
  779. while (old_map) {
  780. struct map *next = map_groups__next(old_map);
  781. struct module_info *mi;
  782. if (old_map == map || old_map->start == map->start) {
  783. /* The kernel map */
  784. old_map = next;
  785. continue;
  786. }
  787. /* Module must be in memory at the same address */
  788. mi = find_module(old_map->dso->short_name, &modules);
  789. if (!mi || mi->start != old_map->start) {
  790. err = -EINVAL;
  791. goto out;
  792. }
  793. old_map = next;
  794. }
  795. out:
  796. delete_modules(&modules);
  797. return err;
  798. }
  799. /*
  800. * If kallsyms is referenced by name then we look for filename in the same
  801. * directory.
  802. */
  803. static bool filename_from_kallsyms_filename(char *filename,
  804. const char *base_name,
  805. const char *kallsyms_filename)
  806. {
  807. char *name;
  808. strcpy(filename, kallsyms_filename);
  809. name = strrchr(filename, '/');
  810. if (!name)
  811. return false;
  812. name += 1;
  813. if (!strcmp(name, "kallsyms")) {
  814. strcpy(name, base_name);
  815. return true;
  816. }
  817. return false;
  818. }
  819. static int validate_kcore_modules(const char *kallsyms_filename,
  820. struct map *map)
  821. {
  822. struct map_groups *kmaps = map__kmap(map)->kmaps;
  823. char modules_filename[PATH_MAX];
  824. if (!filename_from_kallsyms_filename(modules_filename, "modules",
  825. kallsyms_filename))
  826. return -EINVAL;
  827. if (do_validate_kcore_modules(modules_filename, map, kmaps))
  828. return -EINVAL;
  829. return 0;
  830. }
  831. struct kcore_mapfn_data {
  832. struct dso *dso;
  833. enum map_type type;
  834. struct list_head maps;
  835. };
  836. static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
  837. {
  838. struct kcore_mapfn_data *md = data;
  839. struct map *map;
  840. map = map__new2(start, md->dso, md->type);
  841. if (map == NULL)
  842. return -ENOMEM;
  843. map->end = map->start + len;
  844. map->pgoff = pgoff;
  845. list_add(&map->node, &md->maps);
  846. return 0;
  847. }
  848. static int dso__load_kcore(struct dso *dso, struct map *map,
  849. const char *kallsyms_filename)
  850. {
  851. struct map_groups *kmaps = map__kmap(map)->kmaps;
  852. struct machine *machine = kmaps->machine;
  853. struct kcore_mapfn_data md;
  854. struct map *old_map, *new_map, *replacement_map = NULL;
  855. bool is_64_bit;
  856. int err, fd;
  857. char kcore_filename[PATH_MAX];
  858. struct symbol *sym;
  859. /* This function requires that the map is the kernel map */
  860. if (map != machine->vmlinux_maps[map->type])
  861. return -EINVAL;
  862. if (!filename_from_kallsyms_filename(kcore_filename, "kcore",
  863. kallsyms_filename))
  864. return -EINVAL;
  865. /* All modules must be present at their original addresses */
  866. if (validate_kcore_modules(kallsyms_filename, map))
  867. return -EINVAL;
  868. md.dso = dso;
  869. md.type = map->type;
  870. INIT_LIST_HEAD(&md.maps);
  871. fd = open(kcore_filename, O_RDONLY);
  872. if (fd < 0)
  873. return -EINVAL;
  874. /* Read new maps into temporary lists */
  875. err = file__read_maps(fd, md.type == MAP__FUNCTION, kcore_mapfn, &md,
  876. &is_64_bit);
  877. if (err)
  878. goto out_err;
  879. if (list_empty(&md.maps)) {
  880. err = -EINVAL;
  881. goto out_err;
  882. }
  883. /* Remove old maps */
  884. old_map = map_groups__first(kmaps, map->type);
  885. while (old_map) {
  886. struct map *next = map_groups__next(old_map);
  887. if (old_map != map)
  888. map_groups__remove(kmaps, old_map);
  889. old_map = next;
  890. }
  891. /* Find the kernel map using the first symbol */
  892. sym = dso__first_symbol(dso, map->type);
  893. list_for_each_entry(new_map, &md.maps, node) {
  894. if (sym && sym->start >= new_map->start &&
  895. sym->start < new_map->end) {
  896. replacement_map = new_map;
  897. break;
  898. }
  899. }
  900. if (!replacement_map)
  901. replacement_map = list_entry(md.maps.next, struct map, node);
  902. /* Add new maps */
  903. while (!list_empty(&md.maps)) {
  904. new_map = list_entry(md.maps.next, struct map, node);
  905. list_del(&new_map->node);
  906. if (new_map == replacement_map) {
  907. map->start = new_map->start;
  908. map->end = new_map->end;
  909. map->pgoff = new_map->pgoff;
  910. map->map_ip = new_map->map_ip;
  911. map->unmap_ip = new_map->unmap_ip;
  912. map__delete(new_map);
  913. /* Ensure maps are correctly ordered */
  914. map_groups__remove(kmaps, map);
  915. map_groups__insert(kmaps, map);
  916. } else {
  917. map_groups__insert(kmaps, new_map);
  918. }
  919. }
  920. /*
  921. * Set the data type and long name so that kcore can be read via
  922. * dso__data_read_addr().
  923. */
  924. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  925. dso->data_type = DSO_BINARY_TYPE__GUEST_KCORE;
  926. else
  927. dso->data_type = DSO_BINARY_TYPE__KCORE;
  928. dso__set_long_name(dso, strdup(kcore_filename));
  929. close(fd);
  930. if (map->type == MAP__FUNCTION)
  931. pr_debug("Using %s for kernel object code\n", kcore_filename);
  932. else
  933. pr_debug("Using %s for kernel data\n", kcore_filename);
  934. return 0;
  935. out_err:
  936. while (!list_empty(&md.maps)) {
  937. map = list_entry(md.maps.next, struct map, node);
  938. list_del(&map->node);
  939. map__delete(map);
  940. }
  941. close(fd);
  942. return -EINVAL;
  943. }
  944. int dso__load_kallsyms(struct dso *dso, const char *filename,
  945. struct map *map, symbol_filter_t filter)
  946. {
  947. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  948. return -1;
  949. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  950. return -1;
  951. symbols__fixup_duplicate(&dso->symbols[map->type]);
  952. symbols__fixup_end(&dso->symbols[map->type]);
  953. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  954. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  955. else
  956. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  957. if (!dso__load_kcore(dso, map, filename))
  958. return dso__split_kallsyms_for_kcore(dso, map, filter);
  959. else
  960. return dso__split_kallsyms(dso, map, filter);
  961. }
  962. static int dso__load_perf_map(struct dso *dso, struct map *map,
  963. symbol_filter_t filter)
  964. {
  965. char *line = NULL;
  966. size_t n;
  967. FILE *file;
  968. int nr_syms = 0;
  969. file = fopen(dso->long_name, "r");
  970. if (file == NULL)
  971. goto out_failure;
  972. while (!feof(file)) {
  973. u64 start, size;
  974. struct symbol *sym;
  975. int line_len, len;
  976. line_len = getline(&line, &n, file);
  977. if (line_len < 0)
  978. break;
  979. if (!line)
  980. goto out_failure;
  981. line[--line_len] = '\0'; /* \n */
  982. len = hex2u64(line, &start);
  983. len++;
  984. if (len + 2 >= line_len)
  985. continue;
  986. len += hex2u64(line + len, &size);
  987. len++;
  988. if (len + 2 >= line_len)
  989. continue;
  990. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  991. if (sym == NULL)
  992. goto out_delete_line;
  993. if (filter && filter(map, sym))
  994. symbol__delete(sym);
  995. else {
  996. symbols__insert(&dso->symbols[map->type], sym);
  997. nr_syms++;
  998. }
  999. }
  1000. free(line);
  1001. fclose(file);
  1002. return nr_syms;
  1003. out_delete_line:
  1004. free(line);
  1005. out_failure:
  1006. return -1;
  1007. }
  1008. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  1009. {
  1010. char *name;
  1011. int ret = -1;
  1012. u_int i;
  1013. struct machine *machine;
  1014. char *root_dir = (char *) "";
  1015. int ss_pos = 0;
  1016. struct symsrc ss_[2];
  1017. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  1018. dso__set_loaded(dso, map->type);
  1019. if (dso->kernel == DSO_TYPE_KERNEL)
  1020. return dso__load_kernel_sym(dso, map, filter);
  1021. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1022. return dso__load_guest_kernel_sym(dso, map, filter);
  1023. if (map->groups && map->groups->machine)
  1024. machine = map->groups->machine;
  1025. else
  1026. machine = NULL;
  1027. dso->adjust_symbols = 0;
  1028. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  1029. struct stat st;
  1030. if (lstat(dso->name, &st) < 0)
  1031. return -1;
  1032. if (st.st_uid && (st.st_uid != geteuid())) {
  1033. pr_warning("File %s not owned by current user or root, "
  1034. "ignoring it.\n", dso->name);
  1035. return -1;
  1036. }
  1037. ret = dso__load_perf_map(dso, map, filter);
  1038. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  1039. DSO_BINARY_TYPE__NOT_FOUND;
  1040. return ret;
  1041. }
  1042. if (machine)
  1043. root_dir = machine->root_dir;
  1044. name = malloc(PATH_MAX);
  1045. if (!name)
  1046. return -1;
  1047. /* Iterate over candidate debug images.
  1048. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  1049. * and/or opd section) for processing.
  1050. */
  1051. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  1052. struct symsrc *ss = &ss_[ss_pos];
  1053. bool next_slot = false;
  1054. enum dso_binary_type symtab_type = binary_type_symtab[i];
  1055. if (dso__binary_type_file(dso, symtab_type,
  1056. root_dir, name, PATH_MAX))
  1057. continue;
  1058. /* Name is now the name of the next image to try */
  1059. if (symsrc__init(ss, dso, name, symtab_type) < 0)
  1060. continue;
  1061. if (!syms_ss && symsrc__has_symtab(ss)) {
  1062. syms_ss = ss;
  1063. next_slot = true;
  1064. }
  1065. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  1066. runtime_ss = ss;
  1067. next_slot = true;
  1068. }
  1069. if (next_slot) {
  1070. ss_pos++;
  1071. if (syms_ss && runtime_ss)
  1072. break;
  1073. }
  1074. }
  1075. if (!runtime_ss && !syms_ss)
  1076. goto out_free;
  1077. if (runtime_ss && !syms_ss) {
  1078. syms_ss = runtime_ss;
  1079. }
  1080. /* We'll have to hope for the best */
  1081. if (!runtime_ss && syms_ss)
  1082. runtime_ss = syms_ss;
  1083. if (syms_ss) {
  1084. int km;
  1085. km = dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
  1086. dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
  1087. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, km);
  1088. } else {
  1089. ret = -1;
  1090. }
  1091. if (ret > 0) {
  1092. int nr_plt;
  1093. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
  1094. if (nr_plt > 0)
  1095. ret += nr_plt;
  1096. }
  1097. for (; ss_pos > 0; ss_pos--)
  1098. symsrc__destroy(&ss_[ss_pos - 1]);
  1099. out_free:
  1100. free(name);
  1101. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  1102. return 0;
  1103. return ret;
  1104. }
  1105. struct map *map_groups__find_by_name(struct map_groups *mg,
  1106. enum map_type type, const char *name)
  1107. {
  1108. struct rb_node *nd;
  1109. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  1110. struct map *map = rb_entry(nd, struct map, rb_node);
  1111. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  1112. return map;
  1113. }
  1114. return NULL;
  1115. }
  1116. int dso__load_vmlinux(struct dso *dso, struct map *map,
  1117. const char *vmlinux, symbol_filter_t filter)
  1118. {
  1119. int err = -1;
  1120. struct symsrc ss;
  1121. char symfs_vmlinux[PATH_MAX];
  1122. enum dso_binary_type symtab_type;
  1123. if (vmlinux[0] == '/')
  1124. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
  1125. else
  1126. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  1127. symbol_conf.symfs, vmlinux);
  1128. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1129. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1130. else
  1131. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  1132. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  1133. return -1;
  1134. err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
  1135. symsrc__destroy(&ss);
  1136. if (err > 0) {
  1137. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  1138. dso->data_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  1139. else
  1140. dso->data_type = DSO_BINARY_TYPE__VMLINUX;
  1141. dso__set_long_name(dso, (char *)vmlinux);
  1142. dso__set_loaded(dso, map->type);
  1143. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  1144. }
  1145. return err;
  1146. }
  1147. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  1148. symbol_filter_t filter)
  1149. {
  1150. int i, err = 0;
  1151. char *filename;
  1152. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  1153. vmlinux_path__nr_entries + 1);
  1154. filename = dso__build_id_filename(dso, NULL, 0);
  1155. if (filename != NULL) {
  1156. err = dso__load_vmlinux(dso, map, filename, filter);
  1157. if (err > 0) {
  1158. dso->lname_alloc = 1;
  1159. goto out;
  1160. }
  1161. free(filename);
  1162. }
  1163. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  1164. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  1165. if (err > 0) {
  1166. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  1167. dso->lname_alloc = 1;
  1168. break;
  1169. }
  1170. }
  1171. out:
  1172. return err;
  1173. }
  1174. static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
  1175. {
  1176. char kallsyms_filename[PATH_MAX];
  1177. struct dirent *dent;
  1178. int ret = -1;
  1179. DIR *d;
  1180. d = opendir(dir);
  1181. if (!d)
  1182. return -1;
  1183. while (1) {
  1184. dent = readdir(d);
  1185. if (!dent)
  1186. break;
  1187. if (dent->d_type != DT_DIR)
  1188. continue;
  1189. scnprintf(kallsyms_filename, sizeof(kallsyms_filename),
  1190. "%s/%s/kallsyms", dir, dent->d_name);
  1191. if (!validate_kcore_modules(kallsyms_filename, map)) {
  1192. strlcpy(dir, kallsyms_filename, dir_sz);
  1193. ret = 0;
  1194. break;
  1195. }
  1196. }
  1197. closedir(d);
  1198. return ret;
  1199. }
  1200. static char *dso__find_kallsyms(struct dso *dso, struct map *map)
  1201. {
  1202. u8 host_build_id[BUILD_ID_SIZE];
  1203. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  1204. bool is_host = false;
  1205. char path[PATH_MAX];
  1206. if (!dso->has_build_id) {
  1207. /*
  1208. * Last resort, if we don't have a build-id and couldn't find
  1209. * any vmlinux file, try the running kernel kallsyms table.
  1210. */
  1211. goto proc_kallsyms;
  1212. }
  1213. if (sysfs__read_build_id("/sys/kernel/notes", host_build_id,
  1214. sizeof(host_build_id)) == 0)
  1215. is_host = dso__build_id_equal(dso, host_build_id);
  1216. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  1217. /* Use /proc/kallsyms if possible */
  1218. if (is_host) {
  1219. DIR *d;
  1220. int fd;
  1221. /* If no cached kcore go with /proc/kallsyms */
  1222. scnprintf(path, sizeof(path), "%s/[kernel.kcore]/%s",
  1223. buildid_dir, sbuild_id);
  1224. d = opendir(path);
  1225. if (!d)
  1226. goto proc_kallsyms;
  1227. closedir(d);
  1228. /*
  1229. * Do not check the build-id cache, until we know we cannot use
  1230. * /proc/kcore.
  1231. */
  1232. fd = open("/proc/kcore", O_RDONLY);
  1233. if (fd != -1) {
  1234. close(fd);
  1235. /* If module maps match go with /proc/kallsyms */
  1236. if (!validate_kcore_modules("/proc/kallsyms", map))
  1237. goto proc_kallsyms;
  1238. }
  1239. /* Find kallsyms in build-id cache with kcore */
  1240. if (!find_matching_kcore(map, path, sizeof(path)))
  1241. return strdup(path);
  1242. goto proc_kallsyms;
  1243. }
  1244. scnprintf(path, sizeof(path), "%s/[kernel.kallsyms]/%s",
  1245. buildid_dir, sbuild_id);
  1246. if (access(path, F_OK)) {
  1247. pr_err("No kallsyms or vmlinux with build-id %s was found\n",
  1248. sbuild_id);
  1249. return NULL;
  1250. }
  1251. return strdup(path);
  1252. proc_kallsyms:
  1253. return strdup("/proc/kallsyms");
  1254. }
  1255. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  1256. symbol_filter_t filter)
  1257. {
  1258. int err;
  1259. const char *kallsyms_filename = NULL;
  1260. char *kallsyms_allocated_filename = NULL;
  1261. /*
  1262. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  1263. * it and only it, reporting errors to the user if it cannot be used.
  1264. *
  1265. * For instance, try to analyse an ARM perf.data file _without_ a
  1266. * build-id, or if the user specifies the wrong path to the right
  1267. * vmlinux file, obviously we can't fallback to another vmlinux (a
  1268. * x86_86 one, on the machine where analysis is being performed, say),
  1269. * or worse, /proc/kallsyms.
  1270. *
  1271. * If the specified file _has_ a build-id and there is a build-id
  1272. * section in the perf.data file, we will still do the expected
  1273. * validation in dso__load_vmlinux and will bail out if they don't
  1274. * match.
  1275. */
  1276. if (symbol_conf.kallsyms_name != NULL) {
  1277. kallsyms_filename = symbol_conf.kallsyms_name;
  1278. goto do_kallsyms;
  1279. }
  1280. if (!symbol_conf.ignore_vmlinux && symbol_conf.vmlinux_name != NULL) {
  1281. err = dso__load_vmlinux(dso, map,
  1282. symbol_conf.vmlinux_name, filter);
  1283. if (err > 0) {
  1284. dso__set_long_name(dso,
  1285. strdup(symbol_conf.vmlinux_name));
  1286. dso->lname_alloc = 1;
  1287. return err;
  1288. }
  1289. return err;
  1290. }
  1291. if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
  1292. err = dso__load_vmlinux_path(dso, map, filter);
  1293. if (err > 0)
  1294. return err;
  1295. }
  1296. /* do not try local files if a symfs was given */
  1297. if (symbol_conf.symfs[0] != 0)
  1298. return -1;
  1299. kallsyms_allocated_filename = dso__find_kallsyms(dso, map);
  1300. if (!kallsyms_allocated_filename)
  1301. return -1;
  1302. kallsyms_filename = kallsyms_allocated_filename;
  1303. do_kallsyms:
  1304. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1305. if (err > 0)
  1306. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1307. free(kallsyms_allocated_filename);
  1308. if (err > 0 && !dso__is_kcore(dso)) {
  1309. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  1310. map__fixup_start(map);
  1311. map__fixup_end(map);
  1312. }
  1313. return err;
  1314. }
  1315. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  1316. symbol_filter_t filter)
  1317. {
  1318. int err;
  1319. const char *kallsyms_filename = NULL;
  1320. struct machine *machine;
  1321. char path[PATH_MAX];
  1322. if (!map->groups) {
  1323. pr_debug("Guest kernel map hasn't the point to groups\n");
  1324. return -1;
  1325. }
  1326. machine = map->groups->machine;
  1327. if (machine__is_default_guest(machine)) {
  1328. /*
  1329. * if the user specified a vmlinux filename, use it and only
  1330. * it, reporting errors to the user if it cannot be used.
  1331. * Or use file guest_kallsyms inputted by user on commandline
  1332. */
  1333. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  1334. err = dso__load_vmlinux(dso, map,
  1335. symbol_conf.default_guest_vmlinux_name, filter);
  1336. return err;
  1337. }
  1338. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  1339. if (!kallsyms_filename)
  1340. return -1;
  1341. } else {
  1342. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  1343. kallsyms_filename = path;
  1344. }
  1345. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  1346. if (err > 0)
  1347. pr_debug("Using %s for symbols\n", kallsyms_filename);
  1348. if (err > 0 && !dso__is_kcore(dso)) {
  1349. machine__mmap_name(machine, path, sizeof(path));
  1350. dso__set_long_name(dso, strdup(path));
  1351. map__fixup_start(map);
  1352. map__fixup_end(map);
  1353. }
  1354. return err;
  1355. }
  1356. static void vmlinux_path__exit(void)
  1357. {
  1358. while (--vmlinux_path__nr_entries >= 0) {
  1359. free(vmlinux_path[vmlinux_path__nr_entries]);
  1360. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  1361. }
  1362. free(vmlinux_path);
  1363. vmlinux_path = NULL;
  1364. }
  1365. static int vmlinux_path__init(void)
  1366. {
  1367. struct utsname uts;
  1368. char bf[PATH_MAX];
  1369. vmlinux_path = malloc(sizeof(char *) * 5);
  1370. if (vmlinux_path == NULL)
  1371. return -1;
  1372. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  1373. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1374. goto out_fail;
  1375. ++vmlinux_path__nr_entries;
  1376. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  1377. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1378. goto out_fail;
  1379. ++vmlinux_path__nr_entries;
  1380. /* only try running kernel version if no symfs was given */
  1381. if (symbol_conf.symfs[0] != 0)
  1382. return 0;
  1383. if (uname(&uts) < 0)
  1384. return -1;
  1385. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  1386. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1387. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1388. goto out_fail;
  1389. ++vmlinux_path__nr_entries;
  1390. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  1391. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1392. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1393. goto out_fail;
  1394. ++vmlinux_path__nr_entries;
  1395. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  1396. uts.release);
  1397. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  1398. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  1399. goto out_fail;
  1400. ++vmlinux_path__nr_entries;
  1401. return 0;
  1402. out_fail:
  1403. vmlinux_path__exit();
  1404. return -1;
  1405. }
  1406. static int setup_list(struct strlist **list, const char *list_str,
  1407. const char *list_name)
  1408. {
  1409. if (list_str == NULL)
  1410. return 0;
  1411. *list = strlist__new(true, list_str);
  1412. if (!*list) {
  1413. pr_err("problems parsing %s list\n", list_name);
  1414. return -1;
  1415. }
  1416. return 0;
  1417. }
  1418. static bool symbol__read_kptr_restrict(void)
  1419. {
  1420. bool value = false;
  1421. if (geteuid() != 0) {
  1422. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  1423. if (fp != NULL) {
  1424. char line[8];
  1425. if (fgets(line, sizeof(line), fp) != NULL)
  1426. value = atoi(line) != 0;
  1427. fclose(fp);
  1428. }
  1429. }
  1430. return value;
  1431. }
  1432. int symbol__init(void)
  1433. {
  1434. const char *symfs;
  1435. if (symbol_conf.initialized)
  1436. return 0;
  1437. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  1438. symbol__elf_init();
  1439. if (symbol_conf.sort_by_name)
  1440. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1441. sizeof(struct symbol));
  1442. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1443. return -1;
  1444. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1445. pr_err("'.' is the only non valid --field-separator argument\n");
  1446. return -1;
  1447. }
  1448. if (setup_list(&symbol_conf.dso_list,
  1449. symbol_conf.dso_list_str, "dso") < 0)
  1450. return -1;
  1451. if (setup_list(&symbol_conf.comm_list,
  1452. symbol_conf.comm_list_str, "comm") < 0)
  1453. goto out_free_dso_list;
  1454. if (setup_list(&symbol_conf.sym_list,
  1455. symbol_conf.sym_list_str, "symbol") < 0)
  1456. goto out_free_comm_list;
  1457. /*
  1458. * A path to symbols of "/" is identical to ""
  1459. * reset here for simplicity.
  1460. */
  1461. symfs = realpath(symbol_conf.symfs, NULL);
  1462. if (symfs == NULL)
  1463. symfs = symbol_conf.symfs;
  1464. if (strcmp(symfs, "/") == 0)
  1465. symbol_conf.symfs = "";
  1466. if (symfs != symbol_conf.symfs)
  1467. free((void *)symfs);
  1468. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1469. symbol_conf.initialized = true;
  1470. return 0;
  1471. out_free_comm_list:
  1472. strlist__delete(symbol_conf.comm_list);
  1473. out_free_dso_list:
  1474. strlist__delete(symbol_conf.dso_list);
  1475. return -1;
  1476. }
  1477. void symbol__exit(void)
  1478. {
  1479. if (!symbol_conf.initialized)
  1480. return;
  1481. strlist__delete(symbol_conf.sym_list);
  1482. strlist__delete(symbol_conf.dso_list);
  1483. strlist__delete(symbol_conf.comm_list);
  1484. vmlinux_path__exit();
  1485. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1486. symbol_conf.initialized = false;
  1487. }