symbol.c 40 KB

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