symbol.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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 <sys/utsname.h>
  21. #ifndef KSYM_NAME_LEN
  22. #define KSYM_NAME_LEN 256
  23. #endif
  24. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  25. symbol_filter_t filter);
  26. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  27. symbol_filter_t filter);
  28. int vmlinux_path__nr_entries;
  29. char **vmlinux_path;
  30. struct symbol_conf symbol_conf = {
  31. .use_modules = true,
  32. .try_vmlinux_path = true,
  33. .annotate_src = true,
  34. .demangle = true,
  35. .symfs = "",
  36. };
  37. static enum dso_binary_type binary_type_symtab[] = {
  38. DSO_BINARY_TYPE__KALLSYMS,
  39. DSO_BINARY_TYPE__GUEST_KALLSYMS,
  40. DSO_BINARY_TYPE__JAVA_JIT,
  41. DSO_BINARY_TYPE__DEBUGLINK,
  42. DSO_BINARY_TYPE__BUILD_ID_CACHE,
  43. DSO_BINARY_TYPE__FEDORA_DEBUGINFO,
  44. DSO_BINARY_TYPE__UBUNTU_DEBUGINFO,
  45. DSO_BINARY_TYPE__BUILDID_DEBUGINFO,
  46. DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
  47. DSO_BINARY_TYPE__GUEST_KMODULE,
  48. DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE,
  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. goto again;
  137. } else {
  138. nd = rb_next(&curr->rb_node);
  139. rb_erase(&curr->rb_node, symbols);
  140. }
  141. }
  142. }
  143. void symbols__fixup_end(struct rb_root *symbols)
  144. {
  145. struct rb_node *nd, *prevnd = rb_first(symbols);
  146. struct symbol *curr, *prev;
  147. if (prevnd == NULL)
  148. return;
  149. curr = rb_entry(prevnd, struct symbol, rb_node);
  150. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  151. prev = curr;
  152. curr = rb_entry(nd, struct symbol, rb_node);
  153. if (prev->end == prev->start && prev->end != curr->start)
  154. prev->end = curr->start - 1;
  155. }
  156. /* Last entry */
  157. if (curr->end == curr->start)
  158. curr->end = roundup(curr->start, 4096);
  159. }
  160. void __map_groups__fixup_end(struct map_groups *mg, enum map_type type)
  161. {
  162. struct map *prev, *curr;
  163. struct rb_node *nd, *prevnd = rb_first(&mg->maps[type]);
  164. if (prevnd == NULL)
  165. return;
  166. curr = rb_entry(prevnd, struct map, rb_node);
  167. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  168. prev = curr;
  169. curr = rb_entry(nd, struct map, rb_node);
  170. prev->end = curr->start - 1;
  171. }
  172. /*
  173. * We still haven't the actual symbols, so guess the
  174. * last map final address.
  175. */
  176. curr->end = ~0ULL;
  177. }
  178. struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name)
  179. {
  180. size_t namelen = strlen(name) + 1;
  181. struct symbol *sym = calloc(1, (symbol_conf.priv_size +
  182. sizeof(*sym) + namelen));
  183. if (sym == NULL)
  184. return NULL;
  185. if (symbol_conf.priv_size)
  186. sym = ((void *)sym) + symbol_conf.priv_size;
  187. sym->start = start;
  188. sym->end = len ? start + len - 1 : start;
  189. sym->binding = binding;
  190. sym->namelen = namelen - 1;
  191. pr_debug4("%s: %s %#" PRIx64 "-%#" PRIx64 "\n",
  192. __func__, name, start, sym->end);
  193. memcpy(sym->name, name, namelen);
  194. return sym;
  195. }
  196. void symbol__delete(struct symbol *sym)
  197. {
  198. free(((void *)sym) - symbol_conf.priv_size);
  199. }
  200. size_t symbol__fprintf(struct symbol *sym, FILE *fp)
  201. {
  202. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
  203. sym->start, sym->end,
  204. sym->binding == STB_GLOBAL ? 'g' :
  205. sym->binding == STB_LOCAL ? 'l' : 'w',
  206. sym->name);
  207. }
  208. size_t symbol__fprintf_symname_offs(const struct symbol *sym,
  209. const struct addr_location *al, FILE *fp)
  210. {
  211. unsigned long offset;
  212. size_t length;
  213. if (sym && sym->name) {
  214. length = fprintf(fp, "%s", sym->name);
  215. if (al) {
  216. offset = al->addr - sym->start;
  217. length += fprintf(fp, "+0x%lx", offset);
  218. }
  219. return length;
  220. } else
  221. return fprintf(fp, "[unknown]");
  222. }
  223. size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp)
  224. {
  225. return symbol__fprintf_symname_offs(sym, NULL, fp);
  226. }
  227. void symbols__delete(struct rb_root *symbols)
  228. {
  229. struct symbol *pos;
  230. struct rb_node *next = rb_first(symbols);
  231. while (next) {
  232. pos = rb_entry(next, struct symbol, rb_node);
  233. next = rb_next(&pos->rb_node);
  234. rb_erase(&pos->rb_node, symbols);
  235. symbol__delete(pos);
  236. }
  237. }
  238. void symbols__insert(struct rb_root *symbols, struct symbol *sym)
  239. {
  240. struct rb_node **p = &symbols->rb_node;
  241. struct rb_node *parent = NULL;
  242. const u64 ip = sym->start;
  243. struct symbol *s;
  244. while (*p != NULL) {
  245. parent = *p;
  246. s = rb_entry(parent, struct symbol, rb_node);
  247. if (ip < s->start)
  248. p = &(*p)->rb_left;
  249. else
  250. p = &(*p)->rb_right;
  251. }
  252. rb_link_node(&sym->rb_node, parent, p);
  253. rb_insert_color(&sym->rb_node, symbols);
  254. }
  255. static struct symbol *symbols__find(struct rb_root *symbols, u64 ip)
  256. {
  257. struct rb_node *n;
  258. if (symbols == NULL)
  259. return NULL;
  260. n = symbols->rb_node;
  261. while (n) {
  262. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  263. if (ip < s->start)
  264. n = n->rb_left;
  265. else if (ip > s->end)
  266. n = n->rb_right;
  267. else
  268. return s;
  269. }
  270. return NULL;
  271. }
  272. struct symbol_name_rb_node {
  273. struct rb_node rb_node;
  274. struct symbol sym;
  275. };
  276. static void symbols__insert_by_name(struct rb_root *symbols, struct symbol *sym)
  277. {
  278. struct rb_node **p = &symbols->rb_node;
  279. struct rb_node *parent = NULL;
  280. struct symbol_name_rb_node *symn, *s;
  281. symn = container_of(sym, struct symbol_name_rb_node, sym);
  282. while (*p != NULL) {
  283. parent = *p;
  284. s = rb_entry(parent, struct symbol_name_rb_node, rb_node);
  285. if (strcmp(sym->name, s->sym.name) < 0)
  286. p = &(*p)->rb_left;
  287. else
  288. p = &(*p)->rb_right;
  289. }
  290. rb_link_node(&symn->rb_node, parent, p);
  291. rb_insert_color(&symn->rb_node, symbols);
  292. }
  293. static void symbols__sort_by_name(struct rb_root *symbols,
  294. struct rb_root *source)
  295. {
  296. struct rb_node *nd;
  297. for (nd = rb_first(source); nd; nd = rb_next(nd)) {
  298. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  299. symbols__insert_by_name(symbols, pos);
  300. }
  301. }
  302. static struct symbol *symbols__find_by_name(struct rb_root *symbols,
  303. const char *name)
  304. {
  305. struct rb_node *n;
  306. if (symbols == NULL)
  307. return NULL;
  308. n = symbols->rb_node;
  309. while (n) {
  310. struct symbol_name_rb_node *s;
  311. int cmp;
  312. s = rb_entry(n, struct symbol_name_rb_node, rb_node);
  313. cmp = strcmp(name, s->sym.name);
  314. if (cmp < 0)
  315. n = n->rb_left;
  316. else if (cmp > 0)
  317. n = n->rb_right;
  318. else
  319. return &s->sym;
  320. }
  321. return NULL;
  322. }
  323. struct symbol *dso__find_symbol(struct dso *dso,
  324. enum map_type type, u64 addr)
  325. {
  326. return symbols__find(&dso->symbols[type], addr);
  327. }
  328. struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
  329. const char *name)
  330. {
  331. return symbols__find_by_name(&dso->symbol_names[type], name);
  332. }
  333. void dso__sort_by_name(struct dso *dso, enum map_type type)
  334. {
  335. dso__set_sorted_by_name(dso, type);
  336. return symbols__sort_by_name(&dso->symbol_names[type],
  337. &dso->symbols[type]);
  338. }
  339. size_t dso__fprintf_symbols_by_name(struct dso *dso,
  340. enum map_type type, FILE *fp)
  341. {
  342. size_t ret = 0;
  343. struct rb_node *nd;
  344. struct symbol_name_rb_node *pos;
  345. for (nd = rb_first(&dso->symbol_names[type]); nd; nd = rb_next(nd)) {
  346. pos = rb_entry(nd, struct symbol_name_rb_node, rb_node);
  347. fprintf(fp, "%s\n", pos->sym.name);
  348. }
  349. return ret;
  350. }
  351. int kallsyms__parse(const char *filename, void *arg,
  352. int (*process_symbol)(void *arg, const char *name,
  353. char type, u64 start))
  354. {
  355. char *line = NULL;
  356. size_t n;
  357. int err = -1;
  358. FILE *file = fopen(filename, "r");
  359. if (file == NULL)
  360. goto out_failure;
  361. err = 0;
  362. while (!feof(file)) {
  363. u64 start;
  364. int line_len, len;
  365. char symbol_type;
  366. char *symbol_name;
  367. line_len = getline(&line, &n, file);
  368. if (line_len < 0 || !line)
  369. break;
  370. line[--line_len] = '\0'; /* \n */
  371. len = hex2u64(line, &start);
  372. len++;
  373. if (len + 2 >= line_len)
  374. continue;
  375. symbol_type = line[len];
  376. len += 2;
  377. symbol_name = line + len;
  378. len = line_len - len;
  379. if (len >= KSYM_NAME_LEN) {
  380. err = -1;
  381. break;
  382. }
  383. err = process_symbol(arg, symbol_name,
  384. symbol_type, start);
  385. if (err)
  386. break;
  387. }
  388. free(line);
  389. fclose(file);
  390. return err;
  391. out_failure:
  392. return -1;
  393. }
  394. struct process_kallsyms_args {
  395. struct map *map;
  396. struct dso *dso;
  397. };
  398. static u8 kallsyms2elf_type(char type)
  399. {
  400. if (type == 'W')
  401. return STB_WEAK;
  402. return isupper(type) ? STB_GLOBAL : STB_LOCAL;
  403. }
  404. static int map__process_kallsym_symbol(void *arg, const char *name,
  405. char type, u64 start)
  406. {
  407. struct symbol *sym;
  408. struct process_kallsyms_args *a = arg;
  409. struct rb_root *root = &a->dso->symbols[a->map->type];
  410. if (!symbol_type__is_a(type, a->map->type))
  411. return 0;
  412. /*
  413. * module symbols are not sorted so we add all
  414. * symbols, setting length to 0, and rely on
  415. * symbols__fixup_end() to fix it up.
  416. */
  417. sym = symbol__new(start, 0, kallsyms2elf_type(type), name);
  418. if (sym == NULL)
  419. return -ENOMEM;
  420. /*
  421. * We will pass the symbols to the filter later, in
  422. * map__split_kallsyms, when we have split the maps per module
  423. */
  424. symbols__insert(root, sym);
  425. return 0;
  426. }
  427. /*
  428. * Loads the function entries in /proc/kallsyms into kernel_map->dso,
  429. * so that we can in the next step set the symbol ->end address and then
  430. * call kernel_maps__split_kallsyms.
  431. */
  432. static int dso__load_all_kallsyms(struct dso *dso, const char *filename,
  433. struct map *map)
  434. {
  435. struct process_kallsyms_args args = { .map = map, .dso = dso, };
  436. return kallsyms__parse(filename, &args, map__process_kallsym_symbol);
  437. }
  438. /*
  439. * Split the symbols into maps, making sure there are no overlaps, i.e. the
  440. * kernel range is broken in several maps, named [kernel].N, as we don't have
  441. * the original ELF section names vmlinux have.
  442. */
  443. static int dso__split_kallsyms(struct dso *dso, struct map *map,
  444. symbol_filter_t filter)
  445. {
  446. struct map_groups *kmaps = map__kmap(map)->kmaps;
  447. struct machine *machine = kmaps->machine;
  448. struct map *curr_map = map;
  449. struct symbol *pos;
  450. int count = 0, moved = 0;
  451. struct rb_root *root = &dso->symbols[map->type];
  452. struct rb_node *next = rb_first(root);
  453. int kernel_range = 0;
  454. while (next) {
  455. char *module;
  456. pos = rb_entry(next, struct symbol, rb_node);
  457. next = rb_next(&pos->rb_node);
  458. module = strchr(pos->name, '\t');
  459. if (module) {
  460. if (!symbol_conf.use_modules)
  461. goto discard_symbol;
  462. *module++ = '\0';
  463. if (strcmp(curr_map->dso->short_name, module)) {
  464. if (curr_map != map &&
  465. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  466. machine__is_default_guest(machine)) {
  467. /*
  468. * We assume all symbols of a module are
  469. * continuous in * kallsyms, so curr_map
  470. * points to a module and all its
  471. * symbols are in its kmap. Mark it as
  472. * loaded.
  473. */
  474. dso__set_loaded(curr_map->dso,
  475. curr_map->type);
  476. }
  477. curr_map = map_groups__find_by_name(kmaps,
  478. map->type, module);
  479. if (curr_map == NULL) {
  480. pr_debug("%s/proc/{kallsyms,modules} "
  481. "inconsistency while looking "
  482. "for \"%s\" module!\n",
  483. machine->root_dir, module);
  484. curr_map = map;
  485. goto discard_symbol;
  486. }
  487. if (curr_map->dso->loaded &&
  488. !machine__is_default_guest(machine))
  489. goto discard_symbol;
  490. }
  491. /*
  492. * So that we look just like we get from .ko files,
  493. * i.e. not prelinked, relative to map->start.
  494. */
  495. pos->start = curr_map->map_ip(curr_map, pos->start);
  496. pos->end = curr_map->map_ip(curr_map, pos->end);
  497. } else if (curr_map != map) {
  498. char dso_name[PATH_MAX];
  499. struct dso *ndso;
  500. if (count == 0) {
  501. curr_map = map;
  502. goto filter_symbol;
  503. }
  504. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  505. snprintf(dso_name, sizeof(dso_name),
  506. "[guest.kernel].%d",
  507. kernel_range++);
  508. else
  509. snprintf(dso_name, sizeof(dso_name),
  510. "[kernel].%d",
  511. kernel_range++);
  512. ndso = dso__new(dso_name);
  513. if (ndso == NULL)
  514. return -1;
  515. ndso->kernel = dso->kernel;
  516. curr_map = map__new2(pos->start, ndso, map->type);
  517. if (curr_map == NULL) {
  518. dso__delete(ndso);
  519. return -1;
  520. }
  521. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  522. map_groups__insert(kmaps, curr_map);
  523. ++kernel_range;
  524. }
  525. filter_symbol:
  526. if (filter && filter(curr_map, pos)) {
  527. discard_symbol: rb_erase(&pos->rb_node, root);
  528. symbol__delete(pos);
  529. } else {
  530. if (curr_map != map) {
  531. rb_erase(&pos->rb_node, root);
  532. symbols__insert(&curr_map->dso->symbols[curr_map->type], pos);
  533. ++moved;
  534. } else
  535. ++count;
  536. }
  537. }
  538. if (curr_map != map &&
  539. dso->kernel == DSO_TYPE_GUEST_KERNEL &&
  540. machine__is_default_guest(kmaps->machine)) {
  541. dso__set_loaded(curr_map->dso, curr_map->type);
  542. }
  543. return count + moved;
  544. }
  545. bool symbol__restricted_filename(const char *filename,
  546. const char *restricted_filename)
  547. {
  548. bool restricted = false;
  549. if (symbol_conf.kptr_restrict) {
  550. char *r = realpath(filename, NULL);
  551. if (r != NULL) {
  552. restricted = strcmp(r, restricted_filename) == 0;
  553. free(r);
  554. return restricted;
  555. }
  556. }
  557. return restricted;
  558. }
  559. int dso__load_kallsyms(struct dso *dso, const char *filename,
  560. struct map *map, symbol_filter_t filter)
  561. {
  562. if (symbol__restricted_filename(filename, "/proc/kallsyms"))
  563. return -1;
  564. if (dso__load_all_kallsyms(dso, filename, map) < 0)
  565. return -1;
  566. symbols__fixup_duplicate(&dso->symbols[map->type]);
  567. symbols__fixup_end(&dso->symbols[map->type]);
  568. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  569. dso->symtab_type = DSO_BINARY_TYPE__GUEST_KALLSYMS;
  570. else
  571. dso->symtab_type = DSO_BINARY_TYPE__KALLSYMS;
  572. return dso__split_kallsyms(dso, map, filter);
  573. }
  574. static int dso__load_perf_map(struct dso *dso, struct map *map,
  575. symbol_filter_t filter)
  576. {
  577. char *line = NULL;
  578. size_t n;
  579. FILE *file;
  580. int nr_syms = 0;
  581. file = fopen(dso->long_name, "r");
  582. if (file == NULL)
  583. goto out_failure;
  584. while (!feof(file)) {
  585. u64 start, size;
  586. struct symbol *sym;
  587. int line_len, len;
  588. line_len = getline(&line, &n, file);
  589. if (line_len < 0)
  590. break;
  591. if (!line)
  592. goto out_failure;
  593. line[--line_len] = '\0'; /* \n */
  594. len = hex2u64(line, &start);
  595. len++;
  596. if (len + 2 >= line_len)
  597. continue;
  598. len += hex2u64(line + len, &size);
  599. len++;
  600. if (len + 2 >= line_len)
  601. continue;
  602. sym = symbol__new(start, size, STB_GLOBAL, line + len);
  603. if (sym == NULL)
  604. goto out_delete_line;
  605. if (filter && filter(map, sym))
  606. symbol__delete(sym);
  607. else {
  608. symbols__insert(&dso->symbols[map->type], sym);
  609. nr_syms++;
  610. }
  611. }
  612. free(line);
  613. fclose(file);
  614. return nr_syms;
  615. out_delete_line:
  616. free(line);
  617. out_failure:
  618. return -1;
  619. }
  620. int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
  621. {
  622. char *name;
  623. int ret = -1;
  624. u_int i;
  625. struct machine *machine;
  626. char *root_dir = (char *) "";
  627. int ss_pos = 0;
  628. struct symsrc ss_[2];
  629. struct symsrc *syms_ss = NULL, *runtime_ss = NULL;
  630. dso__set_loaded(dso, map->type);
  631. if (dso->kernel == DSO_TYPE_KERNEL)
  632. return dso__load_kernel_sym(dso, map, filter);
  633. else if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  634. return dso__load_guest_kernel_sym(dso, map, filter);
  635. if (map->groups && map->groups->machine)
  636. machine = map->groups->machine;
  637. else
  638. machine = NULL;
  639. dso->adjust_symbols = 0;
  640. if (strncmp(dso->name, "/tmp/perf-", 10) == 0) {
  641. struct stat st;
  642. if (lstat(dso->name, &st) < 0)
  643. return -1;
  644. if (st.st_uid && (st.st_uid != geteuid())) {
  645. pr_warning("File %s not owned by current user or root, "
  646. "ignoring it.\n", dso->name);
  647. return -1;
  648. }
  649. ret = dso__load_perf_map(dso, map, filter);
  650. dso->symtab_type = ret > 0 ? DSO_BINARY_TYPE__JAVA_JIT :
  651. DSO_BINARY_TYPE__NOT_FOUND;
  652. return ret;
  653. }
  654. if (machine)
  655. root_dir = machine->root_dir;
  656. name = malloc(PATH_MAX);
  657. if (!name)
  658. return -1;
  659. /* Iterate over candidate debug images.
  660. * Keep track of "interesting" ones (those which have a symtab, dynsym,
  661. * and/or opd section) for processing.
  662. */
  663. for (i = 0; i < DSO_BINARY_TYPE__SYMTAB_CNT; i++) {
  664. struct symsrc *ss = &ss_[ss_pos];
  665. bool next_slot = false;
  666. enum dso_binary_type symtab_type = binary_type_symtab[i];
  667. if (dso__binary_type_file(dso, symtab_type,
  668. root_dir, name, PATH_MAX))
  669. continue;
  670. /* Name is now the name of the next image to try */
  671. if (symsrc__init(ss, dso, name, symtab_type) < 0)
  672. continue;
  673. if (!syms_ss && symsrc__has_symtab(ss)) {
  674. syms_ss = ss;
  675. next_slot = true;
  676. }
  677. if (!runtime_ss && symsrc__possibly_runtime(ss)) {
  678. runtime_ss = ss;
  679. next_slot = true;
  680. }
  681. if (next_slot) {
  682. ss_pos++;
  683. if (syms_ss && runtime_ss)
  684. break;
  685. }
  686. }
  687. if (!runtime_ss && !syms_ss)
  688. goto out_free;
  689. if (runtime_ss && !syms_ss) {
  690. syms_ss = runtime_ss;
  691. }
  692. /* We'll have to hope for the best */
  693. if (!runtime_ss && syms_ss)
  694. runtime_ss = syms_ss;
  695. if (syms_ss)
  696. ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, 0);
  697. else
  698. ret = -1;
  699. if (ret > 0) {
  700. int nr_plt;
  701. nr_plt = dso__synthesize_plt_symbols(dso, runtime_ss, map, filter);
  702. if (nr_plt > 0)
  703. ret += nr_plt;
  704. }
  705. for (; ss_pos > 0; ss_pos--)
  706. symsrc__destroy(&ss_[ss_pos - 1]);
  707. out_free:
  708. free(name);
  709. if (ret < 0 && strstr(dso->name, " (deleted)") != NULL)
  710. return 0;
  711. return ret;
  712. }
  713. struct map *map_groups__find_by_name(struct map_groups *mg,
  714. enum map_type type, const char *name)
  715. {
  716. struct rb_node *nd;
  717. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  718. struct map *map = rb_entry(nd, struct map, rb_node);
  719. if (map->dso && strcmp(map->dso->short_name, name) == 0)
  720. return map;
  721. }
  722. return NULL;
  723. }
  724. int dso__load_vmlinux(struct dso *dso, struct map *map,
  725. const char *vmlinux, symbol_filter_t filter)
  726. {
  727. int err = -1;
  728. struct symsrc ss;
  729. char symfs_vmlinux[PATH_MAX];
  730. enum dso_binary_type symtab_type;
  731. if (vmlinux[0] == '/')
  732. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s", vmlinux);
  733. else
  734. snprintf(symfs_vmlinux, sizeof(symfs_vmlinux), "%s%s",
  735. symbol_conf.symfs, vmlinux);
  736. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  737. symtab_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  738. else
  739. symtab_type = DSO_BINARY_TYPE__VMLINUX;
  740. if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
  741. return -1;
  742. err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
  743. symsrc__destroy(&ss);
  744. if (err > 0) {
  745. if (dso->kernel == DSO_TYPE_GUEST_KERNEL)
  746. dso->data_type = DSO_BINARY_TYPE__GUEST_VMLINUX;
  747. else
  748. dso->data_type = DSO_BINARY_TYPE__VMLINUX;
  749. dso__set_long_name(dso, (char *)vmlinux);
  750. dso__set_loaded(dso, map->type);
  751. pr_debug("Using %s for symbols\n", symfs_vmlinux);
  752. }
  753. return err;
  754. }
  755. int dso__load_vmlinux_path(struct dso *dso, struct map *map,
  756. symbol_filter_t filter)
  757. {
  758. int i, err = 0;
  759. char *filename;
  760. pr_debug("Looking at the vmlinux_path (%d entries long)\n",
  761. vmlinux_path__nr_entries + 1);
  762. filename = dso__build_id_filename(dso, NULL, 0);
  763. if (filename != NULL) {
  764. err = dso__load_vmlinux(dso, map, filename, filter);
  765. if (err > 0) {
  766. dso->lname_alloc = 1;
  767. goto out;
  768. }
  769. free(filename);
  770. }
  771. for (i = 0; i < vmlinux_path__nr_entries; ++i) {
  772. err = dso__load_vmlinux(dso, map, vmlinux_path[i], filter);
  773. if (err > 0) {
  774. dso__set_long_name(dso, strdup(vmlinux_path[i]));
  775. dso->lname_alloc = 1;
  776. break;
  777. }
  778. }
  779. out:
  780. return err;
  781. }
  782. static int dso__load_kernel_sym(struct dso *dso, struct map *map,
  783. symbol_filter_t filter)
  784. {
  785. int err;
  786. const char *kallsyms_filename = NULL;
  787. char *kallsyms_allocated_filename = NULL;
  788. /*
  789. * Step 1: if the user specified a kallsyms or vmlinux filename, use
  790. * it and only it, reporting errors to the user if it cannot be used.
  791. *
  792. * For instance, try to analyse an ARM perf.data file _without_ a
  793. * build-id, or if the user specifies the wrong path to the right
  794. * vmlinux file, obviously we can't fallback to another vmlinux (a
  795. * x86_86 one, on the machine where analysis is being performed, say),
  796. * or worse, /proc/kallsyms.
  797. *
  798. * If the specified file _has_ a build-id and there is a build-id
  799. * section in the perf.data file, we will still do the expected
  800. * validation in dso__load_vmlinux and will bail out if they don't
  801. * match.
  802. */
  803. if (symbol_conf.kallsyms_name != NULL) {
  804. kallsyms_filename = symbol_conf.kallsyms_name;
  805. goto do_kallsyms;
  806. }
  807. if (symbol_conf.vmlinux_name != NULL) {
  808. err = dso__load_vmlinux(dso, map,
  809. symbol_conf.vmlinux_name, filter);
  810. if (err > 0) {
  811. dso__set_long_name(dso,
  812. strdup(symbol_conf.vmlinux_name));
  813. dso->lname_alloc = 1;
  814. return err;
  815. }
  816. return err;
  817. }
  818. if (vmlinux_path != NULL) {
  819. err = dso__load_vmlinux_path(dso, map, filter);
  820. if (err > 0)
  821. return err;
  822. }
  823. /* do not try local files if a symfs was given */
  824. if (symbol_conf.symfs[0] != 0)
  825. return -1;
  826. /*
  827. * Say the kernel DSO was created when processing the build-id header table,
  828. * we have a build-id, so check if it is the same as the running kernel,
  829. * using it if it is.
  830. */
  831. if (dso->has_build_id) {
  832. u8 kallsyms_build_id[BUILD_ID_SIZE];
  833. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  834. if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
  835. sizeof(kallsyms_build_id)) == 0) {
  836. if (dso__build_id_equal(dso, kallsyms_build_id)) {
  837. kallsyms_filename = "/proc/kallsyms";
  838. goto do_kallsyms;
  839. }
  840. }
  841. /*
  842. * Now look if we have it on the build-id cache in
  843. * $HOME/.debug/[kernel.kallsyms].
  844. */
  845. build_id__sprintf(dso->build_id, sizeof(dso->build_id),
  846. sbuild_id);
  847. if (asprintf(&kallsyms_allocated_filename,
  848. "%s/.debug/[kernel.kallsyms]/%s",
  849. getenv("HOME"), sbuild_id) == -1) {
  850. pr_err("Not enough memory for kallsyms file lookup\n");
  851. return -1;
  852. }
  853. kallsyms_filename = kallsyms_allocated_filename;
  854. if (access(kallsyms_filename, F_OK)) {
  855. pr_err("No kallsyms or vmlinux with build-id %s "
  856. "was found\n", sbuild_id);
  857. free(kallsyms_allocated_filename);
  858. return -1;
  859. }
  860. } else {
  861. /*
  862. * Last resort, if we don't have a build-id and couldn't find
  863. * any vmlinux file, try the running kernel kallsyms table.
  864. */
  865. kallsyms_filename = "/proc/kallsyms";
  866. }
  867. do_kallsyms:
  868. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  869. if (err > 0)
  870. pr_debug("Using %s for symbols\n", kallsyms_filename);
  871. free(kallsyms_allocated_filename);
  872. if (err > 0) {
  873. dso__set_long_name(dso, strdup("[kernel.kallsyms]"));
  874. map__fixup_start(map);
  875. map__fixup_end(map);
  876. }
  877. return err;
  878. }
  879. static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
  880. symbol_filter_t filter)
  881. {
  882. int err;
  883. const char *kallsyms_filename = NULL;
  884. struct machine *machine;
  885. char path[PATH_MAX];
  886. if (!map->groups) {
  887. pr_debug("Guest kernel map hasn't the point to groups\n");
  888. return -1;
  889. }
  890. machine = map->groups->machine;
  891. if (machine__is_default_guest(machine)) {
  892. /*
  893. * if the user specified a vmlinux filename, use it and only
  894. * it, reporting errors to the user if it cannot be used.
  895. * Or use file guest_kallsyms inputted by user on commandline
  896. */
  897. if (symbol_conf.default_guest_vmlinux_name != NULL) {
  898. err = dso__load_vmlinux(dso, map,
  899. symbol_conf.default_guest_vmlinux_name, filter);
  900. return err;
  901. }
  902. kallsyms_filename = symbol_conf.default_guest_kallsyms;
  903. if (!kallsyms_filename)
  904. return -1;
  905. } else {
  906. sprintf(path, "%s/proc/kallsyms", machine->root_dir);
  907. kallsyms_filename = path;
  908. }
  909. err = dso__load_kallsyms(dso, kallsyms_filename, map, filter);
  910. if (err > 0) {
  911. pr_debug("Using %s for symbols\n", kallsyms_filename);
  912. machine__mmap_name(machine, path, sizeof(path));
  913. dso__set_long_name(dso, strdup(path));
  914. map__fixup_start(map);
  915. map__fixup_end(map);
  916. }
  917. return err;
  918. }
  919. static void vmlinux_path__exit(void)
  920. {
  921. while (--vmlinux_path__nr_entries >= 0) {
  922. free(vmlinux_path[vmlinux_path__nr_entries]);
  923. vmlinux_path[vmlinux_path__nr_entries] = NULL;
  924. }
  925. free(vmlinux_path);
  926. vmlinux_path = NULL;
  927. }
  928. static int vmlinux_path__init(void)
  929. {
  930. struct utsname uts;
  931. char bf[PATH_MAX];
  932. vmlinux_path = malloc(sizeof(char *) * 5);
  933. if (vmlinux_path == NULL)
  934. return -1;
  935. vmlinux_path[vmlinux_path__nr_entries] = strdup("vmlinux");
  936. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  937. goto out_fail;
  938. ++vmlinux_path__nr_entries;
  939. vmlinux_path[vmlinux_path__nr_entries] = strdup("/boot/vmlinux");
  940. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  941. goto out_fail;
  942. ++vmlinux_path__nr_entries;
  943. /* only try running kernel version if no symfs was given */
  944. if (symbol_conf.symfs[0] != 0)
  945. return 0;
  946. if (uname(&uts) < 0)
  947. return -1;
  948. snprintf(bf, sizeof(bf), "/boot/vmlinux-%s", uts.release);
  949. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  950. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  951. goto out_fail;
  952. ++vmlinux_path__nr_entries;
  953. snprintf(bf, sizeof(bf), "/lib/modules/%s/build/vmlinux", uts.release);
  954. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  955. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  956. goto out_fail;
  957. ++vmlinux_path__nr_entries;
  958. snprintf(bf, sizeof(bf), "/usr/lib/debug/lib/modules/%s/vmlinux",
  959. uts.release);
  960. vmlinux_path[vmlinux_path__nr_entries] = strdup(bf);
  961. if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
  962. goto out_fail;
  963. ++vmlinux_path__nr_entries;
  964. return 0;
  965. out_fail:
  966. vmlinux_path__exit();
  967. return -1;
  968. }
  969. static int setup_list(struct strlist **list, const char *list_str,
  970. const char *list_name)
  971. {
  972. if (list_str == NULL)
  973. return 0;
  974. *list = strlist__new(true, list_str);
  975. if (!*list) {
  976. pr_err("problems parsing %s list\n", list_name);
  977. return -1;
  978. }
  979. return 0;
  980. }
  981. static bool symbol__read_kptr_restrict(void)
  982. {
  983. bool value = false;
  984. if (geteuid() != 0) {
  985. FILE *fp = fopen("/proc/sys/kernel/kptr_restrict", "r");
  986. if (fp != NULL) {
  987. char line[8];
  988. if (fgets(line, sizeof(line), fp) != NULL)
  989. value = atoi(line) != 0;
  990. fclose(fp);
  991. }
  992. }
  993. return value;
  994. }
  995. int symbol__init(void)
  996. {
  997. const char *symfs;
  998. if (symbol_conf.initialized)
  999. return 0;
  1000. symbol_conf.priv_size = PERF_ALIGN(symbol_conf.priv_size, sizeof(u64));
  1001. symbol__elf_init();
  1002. if (symbol_conf.sort_by_name)
  1003. symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
  1004. sizeof(struct symbol));
  1005. if (symbol_conf.try_vmlinux_path && vmlinux_path__init() < 0)
  1006. return -1;
  1007. if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
  1008. pr_err("'.' is the only non valid --field-separator argument\n");
  1009. return -1;
  1010. }
  1011. if (setup_list(&symbol_conf.dso_list,
  1012. symbol_conf.dso_list_str, "dso") < 0)
  1013. return -1;
  1014. if (setup_list(&symbol_conf.comm_list,
  1015. symbol_conf.comm_list_str, "comm") < 0)
  1016. goto out_free_dso_list;
  1017. if (setup_list(&symbol_conf.sym_list,
  1018. symbol_conf.sym_list_str, "symbol") < 0)
  1019. goto out_free_comm_list;
  1020. /*
  1021. * A path to symbols of "/" is identical to ""
  1022. * reset here for simplicity.
  1023. */
  1024. symfs = realpath(symbol_conf.symfs, NULL);
  1025. if (symfs == NULL)
  1026. symfs = symbol_conf.symfs;
  1027. if (strcmp(symfs, "/") == 0)
  1028. symbol_conf.symfs = "";
  1029. if (symfs != symbol_conf.symfs)
  1030. free((void *)symfs);
  1031. symbol_conf.kptr_restrict = symbol__read_kptr_restrict();
  1032. symbol_conf.initialized = true;
  1033. return 0;
  1034. out_free_comm_list:
  1035. strlist__delete(symbol_conf.comm_list);
  1036. out_free_dso_list:
  1037. strlist__delete(symbol_conf.dso_list);
  1038. return -1;
  1039. }
  1040. void symbol__exit(void)
  1041. {
  1042. if (!symbol_conf.initialized)
  1043. return;
  1044. strlist__delete(symbol_conf.sym_list);
  1045. strlist__delete(symbol_conf.dso_list);
  1046. strlist__delete(symbol_conf.comm_list);
  1047. vmlinux_path__exit();
  1048. symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
  1049. symbol_conf.initialized = false;
  1050. }