symbol.c 28 KB

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