symbol.c 45 KB

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