symbol.c 42 KB

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