symbol.c 46 KB

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