map.c 14 KB

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