symbol.c 43 KB

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