map.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. #include "symbol.h"
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <limits.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include "map.h"
  10. const char *map_type__name[MAP__NR_TYPES] = {
  11. [MAP__FUNCTION] = "Functions",
  12. [MAP__VARIABLE] = "Variables",
  13. };
  14. static inline int is_anon_memory(const char *filename)
  15. {
  16. return strcmp(filename, "//anon") == 0;
  17. }
  18. void map__init(struct map *self, enum map_type type,
  19. u64 start, u64 end, u64 pgoff, struct dso *dso)
  20. {
  21. self->type = type;
  22. self->start = start;
  23. self->end = end;
  24. self->pgoff = pgoff;
  25. self->dso = dso;
  26. self->map_ip = map__map_ip;
  27. self->unmap_ip = map__unmap_ip;
  28. RB_CLEAR_NODE(&self->rb_node);
  29. self->groups = NULL;
  30. self->referenced = false;
  31. }
  32. struct map *map__new(struct list_head *dsos__list, u64 start, u64 len,
  33. u64 pgoff, u32 pid, char *filename,
  34. enum map_type type)
  35. {
  36. struct map *self = malloc(sizeof(*self));
  37. if (self != NULL) {
  38. char newfilename[PATH_MAX];
  39. struct dso *dso;
  40. int anon;
  41. anon = is_anon_memory(filename);
  42. if (anon) {
  43. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
  44. filename = newfilename;
  45. }
  46. dso = __dsos__findnew(dsos__list, filename);
  47. if (dso == NULL)
  48. goto out_delete;
  49. map__init(self, type, start, start + len, pgoff, dso);
  50. if (anon) {
  51. set_identity:
  52. self->map_ip = self->unmap_ip = identity__map_ip;
  53. } else if (strcmp(filename, "[vdso]") == 0) {
  54. dso__set_loaded(dso, self->type);
  55. goto set_identity;
  56. }
  57. }
  58. return self;
  59. out_delete:
  60. free(self);
  61. return NULL;
  62. }
  63. void map__delete(struct map *self)
  64. {
  65. free(self);
  66. }
  67. void map__fixup_start(struct map *self)
  68. {
  69. struct rb_root *symbols = &self->dso->symbols[self->type];
  70. struct rb_node *nd = rb_first(symbols);
  71. if (nd != NULL) {
  72. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  73. self->start = sym->start;
  74. }
  75. }
  76. void map__fixup_end(struct map *self)
  77. {
  78. struct rb_root *symbols = &self->dso->symbols[self->type];
  79. struct rb_node *nd = rb_last(symbols);
  80. if (nd != NULL) {
  81. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  82. self->end = sym->end;
  83. }
  84. }
  85. #define DSO__DELETED "(deleted)"
  86. int map__load(struct map *self, symbol_filter_t filter)
  87. {
  88. const char *name = self->dso->long_name;
  89. int nr;
  90. if (dso__loaded(self->dso, self->type))
  91. return 0;
  92. nr = dso__load(self->dso, self, filter);
  93. if (nr < 0) {
  94. if (self->dso->has_build_id) {
  95. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  96. build_id__sprintf(self->dso->build_id,
  97. sizeof(self->dso->build_id),
  98. sbuild_id);
  99. pr_warning("%s with build id %s not found",
  100. name, sbuild_id);
  101. } else
  102. pr_warning("Failed to open %s", name);
  103. pr_warning(", continuing without symbols\n");
  104. return -1;
  105. } else if (nr == 0) {
  106. const size_t len = strlen(name);
  107. const size_t real_len = len - sizeof(DSO__DELETED);
  108. if (len > sizeof(DSO__DELETED) &&
  109. strcmp(name + real_len + 1, DSO__DELETED) == 0) {
  110. pr_warning("%.*s was updated, restart the long "
  111. "running apps that use it!\n",
  112. (int)real_len, name);
  113. } else {
  114. pr_warning("no symbols found in %s, maybe install "
  115. "a debug package?\n", name);
  116. }
  117. return -1;
  118. }
  119. /*
  120. * Only applies to the kernel, as its symtabs aren't relative like the
  121. * module ones.
  122. */
  123. if (self->dso->kernel)
  124. map__reloc_vmlinux(self);
  125. return 0;
  126. }
  127. struct symbol *map__find_symbol(struct map *self, u64 addr,
  128. symbol_filter_t filter)
  129. {
  130. if (map__load(self, filter) < 0)
  131. return NULL;
  132. return dso__find_symbol(self->dso, self->type, addr);
  133. }
  134. struct symbol *map__find_symbol_by_name(struct map *self, const char *name,
  135. symbol_filter_t filter)
  136. {
  137. if (map__load(self, filter) < 0)
  138. return NULL;
  139. if (!dso__sorted_by_name(self->dso, self->type))
  140. dso__sort_by_name(self->dso, self->type);
  141. return dso__find_symbol_by_name(self->dso, self->type, name);
  142. }
  143. struct map *map__clone(struct map *self)
  144. {
  145. struct map *map = malloc(sizeof(*self));
  146. if (!map)
  147. return NULL;
  148. memcpy(map, self, sizeof(*self));
  149. return map;
  150. }
  151. int map__overlap(struct map *l, struct map *r)
  152. {
  153. if (l->start > r->start) {
  154. struct map *t = l;
  155. l = r;
  156. r = t;
  157. }
  158. if (l->end > r->start)
  159. return 1;
  160. return 0;
  161. }
  162. size_t map__fprintf(struct map *self, FILE *fp)
  163. {
  164. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
  165. self->start, self->end, self->pgoff, self->dso->name);
  166. }
  167. /*
  168. * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
  169. * map->dso->adjust_symbols==1 for ET_EXEC-like cases.
  170. */
  171. u64 map__rip_2objdump(struct map *map, u64 rip)
  172. {
  173. u64 addr = map->dso->adjust_symbols ?
  174. map->unmap_ip(map, rip) : /* RIP -> IP */
  175. rip;
  176. return addr;
  177. }
  178. u64 map__objdump_2ip(struct map *map, u64 addr)
  179. {
  180. u64 ip = map->dso->adjust_symbols ?
  181. addr :
  182. map->unmap_ip(map, addr); /* RIP -> IP */
  183. return ip;
  184. }
  185. void map_groups__init(struct map_groups *self)
  186. {
  187. int i;
  188. for (i = 0; i < MAP__NR_TYPES; ++i) {
  189. self->maps[i] = RB_ROOT;
  190. INIT_LIST_HEAD(&self->removed_maps[i]);
  191. }
  192. self->machine = NULL;
  193. }
  194. static void maps__delete(struct rb_root *self)
  195. {
  196. struct rb_node *next = rb_first(self);
  197. while (next) {
  198. struct map *pos = rb_entry(next, struct map, rb_node);
  199. next = rb_next(&pos->rb_node);
  200. rb_erase(&pos->rb_node, self);
  201. map__delete(pos);
  202. }
  203. }
  204. static void maps__delete_removed(struct list_head *self)
  205. {
  206. struct map *pos, *n;
  207. list_for_each_entry_safe(pos, n, self, node) {
  208. list_del(&pos->node);
  209. map__delete(pos);
  210. }
  211. }
  212. void map_groups__exit(struct map_groups *self)
  213. {
  214. int i;
  215. for (i = 0; i < MAP__NR_TYPES; ++i) {
  216. maps__delete(&self->maps[i]);
  217. maps__delete_removed(&self->removed_maps[i]);
  218. }
  219. }
  220. void map_groups__flush(struct map_groups *self)
  221. {
  222. int type;
  223. for (type = 0; type < MAP__NR_TYPES; type++) {
  224. struct rb_root *root = &self->maps[type];
  225. struct rb_node *next = rb_first(root);
  226. while (next) {
  227. struct map *pos = rb_entry(next, struct map, rb_node);
  228. next = rb_next(&pos->rb_node);
  229. rb_erase(&pos->rb_node, root);
  230. /*
  231. * We may have references to this map, for
  232. * instance in some hist_entry instances, so
  233. * just move them to a separate list.
  234. */
  235. list_add_tail(&pos->node, &self->removed_maps[pos->type]);
  236. }
  237. }
  238. }
  239. struct symbol *map_groups__find_symbol(struct map_groups *self,
  240. enum map_type type, u64 addr,
  241. struct map **mapp,
  242. symbol_filter_t filter)
  243. {
  244. struct map *map = map_groups__find(self, type, addr);
  245. if (map != NULL) {
  246. if (mapp != NULL)
  247. *mapp = map;
  248. return map__find_symbol(map, map->map_ip(map, addr), filter);
  249. }
  250. return NULL;
  251. }
  252. struct symbol *map_groups__find_symbol_by_name(struct map_groups *self,
  253. enum map_type type,
  254. const char *name,
  255. struct map **mapp,
  256. symbol_filter_t filter)
  257. {
  258. struct rb_node *nd;
  259. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  260. struct map *pos = rb_entry(nd, struct map, rb_node);
  261. struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
  262. if (sym == NULL)
  263. continue;
  264. if (mapp != NULL)
  265. *mapp = pos;
  266. return sym;
  267. }
  268. return NULL;
  269. }
  270. size_t __map_groups__fprintf_maps(struct map_groups *self,
  271. enum map_type type, int verbose, FILE *fp)
  272. {
  273. size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
  274. struct rb_node *nd;
  275. for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
  276. struct map *pos = rb_entry(nd, struct map, rb_node);
  277. printed += fprintf(fp, "Map:");
  278. printed += map__fprintf(pos, fp);
  279. if (verbose > 2) {
  280. printed += dso__fprintf(pos->dso, type, fp);
  281. printed += fprintf(fp, "--\n");
  282. }
  283. }
  284. return printed;
  285. }
  286. size_t map_groups__fprintf_maps(struct map_groups *self, int verbose, FILE *fp)
  287. {
  288. size_t printed = 0, i;
  289. for (i = 0; i < MAP__NR_TYPES; ++i)
  290. printed += __map_groups__fprintf_maps(self, i, verbose, fp);
  291. return printed;
  292. }
  293. static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
  294. enum map_type type,
  295. int verbose, FILE *fp)
  296. {
  297. struct map *pos;
  298. size_t printed = 0;
  299. list_for_each_entry(pos, &self->removed_maps[type], node) {
  300. printed += fprintf(fp, "Map:");
  301. printed += map__fprintf(pos, fp);
  302. if (verbose > 1) {
  303. printed += dso__fprintf(pos->dso, type, fp);
  304. printed += fprintf(fp, "--\n");
  305. }
  306. }
  307. return printed;
  308. }
  309. static size_t map_groups__fprintf_removed_maps(struct map_groups *self,
  310. int verbose, FILE *fp)
  311. {
  312. size_t printed = 0, i;
  313. for (i = 0; i < MAP__NR_TYPES; ++i)
  314. printed += __map_groups__fprintf_removed_maps(self, i, verbose, fp);
  315. return printed;
  316. }
  317. size_t map_groups__fprintf(struct map_groups *self, int verbose, FILE *fp)
  318. {
  319. size_t printed = map_groups__fprintf_maps(self, verbose, fp);
  320. printed += fprintf(fp, "Removed maps:\n");
  321. return printed + map_groups__fprintf_removed_maps(self, verbose, fp);
  322. }
  323. int map_groups__fixup_overlappings(struct map_groups *self, struct map *map,
  324. int verbose, FILE *fp)
  325. {
  326. struct rb_root *root = &self->maps[map->type];
  327. struct rb_node *next = rb_first(root);
  328. int err = 0;
  329. while (next) {
  330. struct map *pos = rb_entry(next, struct map, rb_node);
  331. next = rb_next(&pos->rb_node);
  332. if (!map__overlap(pos, map))
  333. continue;
  334. if (verbose >= 2) {
  335. fputs("overlapping maps:\n", fp);
  336. map__fprintf(map, fp);
  337. map__fprintf(pos, fp);
  338. }
  339. rb_erase(&pos->rb_node, root);
  340. /*
  341. * Now check if we need to create new maps for areas not
  342. * overlapped by the new map:
  343. */
  344. if (map->start > pos->start) {
  345. struct map *before = map__clone(pos);
  346. if (before == NULL) {
  347. err = -ENOMEM;
  348. goto move_map;
  349. }
  350. before->end = map->start - 1;
  351. map_groups__insert(self, before);
  352. if (verbose >= 2)
  353. map__fprintf(before, fp);
  354. }
  355. if (map->end < pos->end) {
  356. struct map *after = map__clone(pos);
  357. if (after == NULL) {
  358. err = -ENOMEM;
  359. goto move_map;
  360. }
  361. after->start = map->end + 1;
  362. map_groups__insert(self, after);
  363. if (verbose >= 2)
  364. map__fprintf(after, fp);
  365. }
  366. move_map:
  367. /*
  368. * If we have references, just move them to a separate list.
  369. */
  370. if (pos->referenced)
  371. list_add_tail(&pos->node, &self->removed_maps[map->type]);
  372. else
  373. map__delete(pos);
  374. if (err)
  375. return err;
  376. }
  377. return 0;
  378. }
  379. /*
  380. * XXX This should not really _copy_ te maps, but refcount them.
  381. */
  382. int map_groups__clone(struct map_groups *self,
  383. struct map_groups *parent, enum map_type type)
  384. {
  385. struct rb_node *nd;
  386. for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
  387. struct map *map = rb_entry(nd, struct map, rb_node);
  388. struct map *new = map__clone(map);
  389. if (new == NULL)
  390. return -ENOMEM;
  391. map_groups__insert(self, new);
  392. }
  393. return 0;
  394. }
  395. static u64 map__reloc_map_ip(struct map *map, u64 ip)
  396. {
  397. return ip + (s64)map->pgoff;
  398. }
  399. static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
  400. {
  401. return ip - (s64)map->pgoff;
  402. }
  403. void map__reloc_vmlinux(struct map *self)
  404. {
  405. struct kmap *kmap = map__kmap(self);
  406. s64 reloc;
  407. if (!kmap->ref_reloc_sym || !kmap->ref_reloc_sym->unrelocated_addr)
  408. return;
  409. reloc = (kmap->ref_reloc_sym->unrelocated_addr -
  410. kmap->ref_reloc_sym->addr);
  411. if (!reloc)
  412. return;
  413. self->map_ip = map__reloc_map_ip;
  414. self->unmap_ip = map__reloc_unmap_ip;
  415. self->pgoff = reloc;
  416. }
  417. void maps__insert(struct rb_root *maps, struct map *map)
  418. {
  419. struct rb_node **p = &maps->rb_node;
  420. struct rb_node *parent = NULL;
  421. const u64 ip = map->start;
  422. struct map *m;
  423. while (*p != NULL) {
  424. parent = *p;
  425. m = rb_entry(parent, struct map, rb_node);
  426. if (ip < m->start)
  427. p = &(*p)->rb_left;
  428. else
  429. p = &(*p)->rb_right;
  430. }
  431. rb_link_node(&map->rb_node, parent, p);
  432. rb_insert_color(&map->rb_node, maps);
  433. }
  434. void maps__remove(struct rb_root *self, struct map *map)
  435. {
  436. rb_erase(&map->rb_node, self);
  437. }
  438. struct map *maps__find(struct rb_root *maps, u64 ip)
  439. {
  440. struct rb_node **p = &maps->rb_node;
  441. struct rb_node *parent = NULL;
  442. struct map *m;
  443. while (*p != NULL) {
  444. parent = *p;
  445. m = rb_entry(parent, struct map, rb_node);
  446. if (ip < m->start)
  447. p = &(*p)->rb_left;
  448. else if (ip > m->end)
  449. p = &(*p)->rb_right;
  450. else
  451. return m;
  452. }
  453. return NULL;
  454. }
  455. int machine__init(struct machine *self, const char *root_dir, pid_t pid)
  456. {
  457. map_groups__init(&self->kmaps);
  458. RB_CLEAR_NODE(&self->rb_node);
  459. INIT_LIST_HEAD(&self->user_dsos);
  460. INIT_LIST_HEAD(&self->kernel_dsos);
  461. self->kmaps.machine = self;
  462. self->pid = pid;
  463. self->root_dir = strdup(root_dir);
  464. return self->root_dir == NULL ? -ENOMEM : 0;
  465. }
  466. static void dsos__delete(struct list_head *self)
  467. {
  468. struct dso *pos, *n;
  469. list_for_each_entry_safe(pos, n, self, node) {
  470. list_del(&pos->node);
  471. dso__delete(pos);
  472. }
  473. }
  474. void machine__exit(struct machine *self)
  475. {
  476. map_groups__exit(&self->kmaps);
  477. dsos__delete(&self->user_dsos);
  478. dsos__delete(&self->kernel_dsos);
  479. free(self->root_dir);
  480. self->root_dir = NULL;
  481. }
  482. void machine__delete(struct machine *self)
  483. {
  484. machine__exit(self);
  485. free(self);
  486. }
  487. struct machine *machines__add(struct rb_root *self, pid_t pid,
  488. const char *root_dir)
  489. {
  490. struct rb_node **p = &self->rb_node;
  491. struct rb_node *parent = NULL;
  492. struct machine *pos, *machine = malloc(sizeof(*machine));
  493. if (!machine)
  494. return NULL;
  495. if (machine__init(machine, root_dir, pid) != 0) {
  496. free(machine);
  497. return NULL;
  498. }
  499. while (*p != NULL) {
  500. parent = *p;
  501. pos = rb_entry(parent, struct machine, rb_node);
  502. if (pid < pos->pid)
  503. p = &(*p)->rb_left;
  504. else
  505. p = &(*p)->rb_right;
  506. }
  507. rb_link_node(&machine->rb_node, parent, p);
  508. rb_insert_color(&machine->rb_node, self);
  509. return machine;
  510. }
  511. struct machine *machines__find(struct rb_root *self, pid_t pid)
  512. {
  513. struct rb_node **p = &self->rb_node;
  514. struct rb_node *parent = NULL;
  515. struct machine *machine;
  516. struct machine *default_machine = NULL;
  517. while (*p != NULL) {
  518. parent = *p;
  519. machine = rb_entry(parent, struct machine, rb_node);
  520. if (pid < machine->pid)
  521. p = &(*p)->rb_left;
  522. else if (pid > machine->pid)
  523. p = &(*p)->rb_right;
  524. else
  525. return machine;
  526. if (!machine->pid)
  527. default_machine = machine;
  528. }
  529. return default_machine;
  530. }
  531. struct machine *machines__findnew(struct rb_root *self, pid_t pid)
  532. {
  533. char path[PATH_MAX];
  534. const char *root_dir;
  535. struct machine *machine = machines__find(self, pid);
  536. if (!machine || machine->pid != pid) {
  537. if (pid == HOST_KERNEL_ID || pid == DEFAULT_GUEST_KERNEL_ID)
  538. root_dir = "";
  539. else {
  540. if (!symbol_conf.guestmount)
  541. goto out;
  542. sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
  543. if (access(path, R_OK)) {
  544. pr_err("Can't access file %s\n", path);
  545. goto out;
  546. }
  547. root_dir = path;
  548. }
  549. machine = machines__add(self, pid, root_dir);
  550. }
  551. out:
  552. return machine;
  553. }
  554. void machines__process(struct rb_root *self, machine__process_t process, void *data)
  555. {
  556. struct rb_node *nd;
  557. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  558. struct machine *pos = rb_entry(nd, struct machine, rb_node);
  559. process(pos, data);
  560. }
  561. }
  562. char *machine__mmap_name(struct machine *self, char *bf, size_t size)
  563. {
  564. if (machine__is_host(self))
  565. snprintf(bf, size, "[%s]", "kernel.kallsyms");
  566. else if (machine__is_default_guest(self))
  567. snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
  568. else
  569. snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", self->pid);
  570. return bf;
  571. }