symbol.c 45 KB

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