map.c 13 KB

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