symbol.c 40 KB

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