symbol.c 42 KB

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