symbol.c 43 KB

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