map.c 12 KB

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