map.c 13 KB

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