symbol.c 39 KB

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