symbol.c 44 KB

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