symbol.c 44 KB

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