symbol.c 46 KB

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