symbol.c 46 KB

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