map.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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 "util.h"
  15. #include "debug.h"
  16. #include "machine.h"
  17. #include <linux/string.h>
  18. const char *map_type__name[MAP__NR_TYPES] = {
  19. [MAP__FUNCTION] = "Functions",
  20. [MAP__VARIABLE] = "Variables",
  21. };
  22. static inline int is_anon_memory(const char *filename)
  23. {
  24. return !strcmp(filename, "//anon") ||
  25. !strcmp(filename, "/dev/zero (deleted)") ||
  26. !strcmp(filename, "/anon_hugepage (deleted)");
  27. }
  28. static inline int is_no_dso_memory(const char *filename)
  29. {
  30. return !strncmp(filename, "[stack", 6) ||
  31. !strncmp(filename, "/SYSV",5) ||
  32. !strcmp(filename, "[heap]");
  33. }
  34. static inline int is_android_lib(const char *filename)
  35. {
  36. return !strncmp(filename, "/data/app-lib", 13) ||
  37. !strncmp(filename, "/system/lib", 11);
  38. }
  39. static inline bool replace_android_lib(const char *filename, char *newfilename)
  40. {
  41. const char *libname;
  42. char *app_abi;
  43. size_t app_abi_length, new_length;
  44. size_t lib_length = 0;
  45. libname = strrchr(filename, '/');
  46. if (libname)
  47. lib_length = strlen(libname);
  48. app_abi = getenv("APP_ABI");
  49. if (!app_abi)
  50. return false;
  51. app_abi_length = strlen(app_abi);
  52. if (!strncmp(filename, "/data/app-lib", 13)) {
  53. char *apk_path;
  54. if (!app_abi_length)
  55. return false;
  56. new_length = 7 + app_abi_length + lib_length;
  57. apk_path = getenv("APK_PATH");
  58. if (apk_path) {
  59. new_length += strlen(apk_path) + 1;
  60. if (new_length > PATH_MAX)
  61. return false;
  62. snprintf(newfilename, new_length,
  63. "%s/libs/%s/%s", apk_path, app_abi, libname);
  64. } else {
  65. if (new_length > PATH_MAX)
  66. return false;
  67. snprintf(newfilename, new_length,
  68. "libs/%s/%s", app_abi, libname);
  69. }
  70. return true;
  71. }
  72. if (!strncmp(filename, "/system/lib/", 11)) {
  73. char *ndk, *app;
  74. const char *arch;
  75. size_t ndk_length;
  76. size_t app_length;
  77. ndk = getenv("NDK_ROOT");
  78. app = getenv("APP_PLATFORM");
  79. if (!(ndk && app))
  80. return false;
  81. ndk_length = strlen(ndk);
  82. app_length = strlen(app);
  83. if (!(ndk_length && app_length && app_abi_length))
  84. return false;
  85. arch = !strncmp(app_abi, "arm", 3) ? "arm" :
  86. !strncmp(app_abi, "mips", 4) ? "mips" :
  87. !strncmp(app_abi, "x86", 3) ? "x86" : NULL;
  88. if (!arch)
  89. return false;
  90. new_length = 27 + ndk_length +
  91. app_length + lib_length
  92. + strlen(arch);
  93. if (new_length > PATH_MAX)
  94. return false;
  95. snprintf(newfilename, new_length,
  96. "%s/platforms/%s/arch-%s/usr/lib/%s",
  97. ndk, app, arch, libname);
  98. return true;
  99. }
  100. return false;
  101. }
  102. void map__init(struct map *map, enum map_type type,
  103. u64 start, u64 end, u64 pgoff, struct dso *dso)
  104. {
  105. map->type = type;
  106. map->start = start;
  107. map->end = end;
  108. map->pgoff = pgoff;
  109. map->reloc = 0;
  110. map->dso = dso;
  111. map->map_ip = map__map_ip;
  112. map->unmap_ip = map__unmap_ip;
  113. RB_CLEAR_NODE(&map->rb_node);
  114. map->groups = NULL;
  115. map->referenced = false;
  116. map->erange_warned = false;
  117. }
  118. struct map *map__new(struct machine *machine, u64 start, u64 len,
  119. u64 pgoff, u32 pid, u32 d_maj, u32 d_min, u64 ino,
  120. u64 ino_gen, u32 prot, u32 flags, char *filename,
  121. enum map_type type, struct thread *thread)
  122. {
  123. struct map *map = malloc(sizeof(*map));
  124. if (map != NULL) {
  125. char newfilename[PATH_MAX];
  126. struct dso *dso;
  127. int anon, no_dso, vdso, android;
  128. android = is_android_lib(filename);
  129. anon = is_anon_memory(filename);
  130. vdso = is_vdso_map(filename);
  131. no_dso = is_no_dso_memory(filename);
  132. map->maj = d_maj;
  133. map->min = d_min;
  134. map->ino = ino;
  135. map->ino_generation = ino_gen;
  136. map->prot = prot;
  137. map->flags = flags;
  138. if ((anon || no_dso) && type == MAP__FUNCTION) {
  139. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", pid);
  140. filename = newfilename;
  141. }
  142. if (android) {
  143. if (replace_android_lib(filename, newfilename))
  144. filename = newfilename;
  145. }
  146. if (vdso) {
  147. pgoff = 0;
  148. dso = vdso__dso_findnew(machine, thread);
  149. } else
  150. dso = __dsos__findnew(&machine->user_dsos, filename);
  151. if (dso == NULL)
  152. goto out_delete;
  153. map__init(map, type, start, start + len, pgoff, dso);
  154. if (anon || no_dso) {
  155. map->map_ip = map->unmap_ip = identity__map_ip;
  156. /*
  157. * Set memory without DSO as loaded. All map__find_*
  158. * functions still return NULL, and we avoid the
  159. * unnecessary map__load warning.
  160. */
  161. if (type != MAP__FUNCTION)
  162. dso__set_loaded(dso, map->type);
  163. }
  164. }
  165. return map;
  166. out_delete:
  167. free(map);
  168. return NULL;
  169. }
  170. /*
  171. * Constructor variant for modules (where we know from /proc/modules where
  172. * they are loaded) and for vmlinux, where only after we load all the
  173. * symbols we'll know where it starts and ends.
  174. */
  175. struct map *map__new2(u64 start, struct dso *dso, enum map_type type)
  176. {
  177. struct map *map = calloc(1, (sizeof(*map) +
  178. (dso->kernel ? sizeof(struct kmap) : 0)));
  179. if (map != NULL) {
  180. /*
  181. * ->end will be filled after we load all the symbols
  182. */
  183. map__init(map, type, start, 0, 0, dso);
  184. }
  185. return map;
  186. }
  187. void map__delete(struct map *map)
  188. {
  189. free(map);
  190. }
  191. void map__fixup_start(struct map *map)
  192. {
  193. struct rb_root *symbols = &map->dso->symbols[map->type];
  194. struct rb_node *nd = rb_first(symbols);
  195. if (nd != NULL) {
  196. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  197. map->start = sym->start;
  198. }
  199. }
  200. void map__fixup_end(struct map *map)
  201. {
  202. struct rb_root *symbols = &map->dso->symbols[map->type];
  203. struct rb_node *nd = rb_last(symbols);
  204. if (nd != NULL) {
  205. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  206. map->end = sym->end;
  207. }
  208. }
  209. #define DSO__DELETED "(deleted)"
  210. int map__load(struct map *map, symbol_filter_t filter)
  211. {
  212. const char *name = map->dso->long_name;
  213. int nr;
  214. if (dso__loaded(map->dso, map->type))
  215. return 0;
  216. nr = dso__load(map->dso, map, filter);
  217. if (nr < 0) {
  218. if (map->dso->has_build_id) {
  219. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  220. build_id__sprintf(map->dso->build_id,
  221. sizeof(map->dso->build_id),
  222. sbuild_id);
  223. pr_warning("%s with build id %s not found",
  224. name, sbuild_id);
  225. } else
  226. pr_warning("Failed to open %s", name);
  227. pr_warning(", continuing without symbols\n");
  228. return -1;
  229. } else if (nr == 0) {
  230. #ifdef HAVE_LIBELF_SUPPORT
  231. const size_t len = strlen(name);
  232. const size_t real_len = len - sizeof(DSO__DELETED);
  233. if (len > sizeof(DSO__DELETED) &&
  234. strcmp(name + real_len + 1, DSO__DELETED) == 0) {
  235. pr_warning("%.*s was updated (is prelink enabled?). "
  236. "Restart the long running apps that use it!\n",
  237. (int)real_len, name);
  238. } else {
  239. pr_warning("no symbols found in %s, maybe install "
  240. "a debug package?\n", name);
  241. }
  242. #endif
  243. return -1;
  244. }
  245. return 0;
  246. }
  247. struct symbol *map__find_symbol(struct map *map, u64 addr,
  248. symbol_filter_t filter)
  249. {
  250. if (map__load(map, filter) < 0)
  251. return NULL;
  252. return dso__find_symbol(map->dso, map->type, addr);
  253. }
  254. struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
  255. symbol_filter_t filter)
  256. {
  257. if (map__load(map, filter) < 0)
  258. return NULL;
  259. if (!dso__sorted_by_name(map->dso, map->type))
  260. dso__sort_by_name(map->dso, map->type);
  261. return dso__find_symbol_by_name(map->dso, map->type, name);
  262. }
  263. struct map *map__clone(struct map *map)
  264. {
  265. return memdup(map, sizeof(*map));
  266. }
  267. int map__overlap(struct map *l, struct map *r)
  268. {
  269. if (l->start > r->start) {
  270. struct map *t = l;
  271. l = r;
  272. r = t;
  273. }
  274. if (l->end > r->start)
  275. return 1;
  276. return 0;
  277. }
  278. size_t map__fprintf(struct map *map, FILE *fp)
  279. {
  280. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
  281. map->start, map->end, map->pgoff, map->dso->name);
  282. }
  283. size_t map__fprintf_dsoname(struct map *map, FILE *fp)
  284. {
  285. const char *dsoname = "[unknown]";
  286. if (map && map->dso && (map->dso->name || map->dso->long_name)) {
  287. if (symbol_conf.show_kernel_path && map->dso->long_name)
  288. dsoname = map->dso->long_name;
  289. else if (map->dso->name)
  290. dsoname = map->dso->name;
  291. }
  292. return fprintf(fp, "%s", dsoname);
  293. }
  294. int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
  295. FILE *fp)
  296. {
  297. char *srcline;
  298. int ret = 0;
  299. if (map && map->dso) {
  300. srcline = get_srcline(map->dso,
  301. map__rip_2objdump(map, addr), NULL, true);
  302. if (srcline != SRCLINE_UNKNOWN)
  303. ret = fprintf(fp, "%s%s", prefix, srcline);
  304. free_srcline(srcline);
  305. }
  306. return ret;
  307. }
  308. /**
  309. * map__rip_2objdump - convert symbol start address to objdump address.
  310. * @map: memory map
  311. * @rip: symbol start address
  312. *
  313. * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
  314. * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
  315. * relative to section start.
  316. *
  317. * Return: Address suitable for passing to "objdump --start-address="
  318. */
  319. u64 map__rip_2objdump(struct map *map, u64 rip)
  320. {
  321. if (!map->dso->adjust_symbols)
  322. return rip;
  323. if (map->dso->rel)
  324. return rip - map->pgoff;
  325. return map->unmap_ip(map, rip) - map->reloc;
  326. }
  327. /**
  328. * map__objdump_2mem - convert objdump address to a memory address.
  329. * @map: memory map
  330. * @ip: objdump address
  331. *
  332. * Closely related to map__rip_2objdump(), this function takes an address from
  333. * objdump and converts it to a memory address. Note this assumes that @map
  334. * contains the address. To be sure the result is valid, check it forwards
  335. * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
  336. *
  337. * Return: Memory address.
  338. */
  339. u64 map__objdump_2mem(struct map *map, u64 ip)
  340. {
  341. if (!map->dso->adjust_symbols)
  342. return map->unmap_ip(map, ip);
  343. if (map->dso->rel)
  344. return map->unmap_ip(map, ip + map->pgoff);
  345. return ip + map->reloc;
  346. }
  347. void map_groups__init(struct map_groups *mg, struct machine *machine)
  348. {
  349. int i;
  350. for (i = 0; i < MAP__NR_TYPES; ++i) {
  351. mg->maps[i] = RB_ROOT;
  352. INIT_LIST_HEAD(&mg->removed_maps[i]);
  353. }
  354. mg->machine = machine;
  355. mg->refcnt = 1;
  356. }
  357. static void maps__delete(struct rb_root *maps)
  358. {
  359. struct rb_node *next = rb_first(maps);
  360. while (next) {
  361. struct map *pos = rb_entry(next, struct map, rb_node);
  362. next = rb_next(&pos->rb_node);
  363. rb_erase(&pos->rb_node, maps);
  364. map__delete(pos);
  365. }
  366. }
  367. static void maps__delete_removed(struct list_head *maps)
  368. {
  369. struct map *pos, *n;
  370. list_for_each_entry_safe(pos, n, maps, node) {
  371. list_del(&pos->node);
  372. map__delete(pos);
  373. }
  374. }
  375. void map_groups__exit(struct map_groups *mg)
  376. {
  377. int i;
  378. for (i = 0; i < MAP__NR_TYPES; ++i) {
  379. maps__delete(&mg->maps[i]);
  380. maps__delete_removed(&mg->removed_maps[i]);
  381. }
  382. }
  383. bool map_groups__empty(struct map_groups *mg)
  384. {
  385. int i;
  386. for (i = 0; i < MAP__NR_TYPES; ++i) {
  387. if (maps__first(&mg->maps[i]))
  388. return false;
  389. if (!list_empty(&mg->removed_maps[i]))
  390. return false;
  391. }
  392. return true;
  393. }
  394. struct map_groups *map_groups__new(struct machine *machine)
  395. {
  396. struct map_groups *mg = malloc(sizeof(*mg));
  397. if (mg != NULL)
  398. map_groups__init(mg, machine);
  399. return mg;
  400. }
  401. void map_groups__delete(struct map_groups *mg)
  402. {
  403. map_groups__exit(mg);
  404. free(mg);
  405. }
  406. void map_groups__put(struct map_groups *mg)
  407. {
  408. if (--mg->refcnt == 0)
  409. map_groups__delete(mg);
  410. }
  411. void map_groups__flush(struct map_groups *mg)
  412. {
  413. int type;
  414. for (type = 0; type < MAP__NR_TYPES; type++) {
  415. struct rb_root *root = &mg->maps[type];
  416. struct rb_node *next = rb_first(root);
  417. while (next) {
  418. struct map *pos = rb_entry(next, struct map, rb_node);
  419. next = rb_next(&pos->rb_node);
  420. rb_erase(&pos->rb_node, root);
  421. /*
  422. * We may have references to this map, for
  423. * instance in some hist_entry instances, so
  424. * just move them to a separate list.
  425. */
  426. list_add_tail(&pos->node, &mg->removed_maps[pos->type]);
  427. }
  428. }
  429. }
  430. struct symbol *map_groups__find_symbol(struct map_groups *mg,
  431. enum map_type type, u64 addr,
  432. struct map **mapp,
  433. symbol_filter_t filter)
  434. {
  435. struct map *map = map_groups__find(mg, type, addr);
  436. /* Ensure map is loaded before using map->map_ip */
  437. if (map != NULL && map__load(map, filter) >= 0) {
  438. if (mapp != NULL)
  439. *mapp = map;
  440. return map__find_symbol(map, map->map_ip(map, addr), filter);
  441. }
  442. return NULL;
  443. }
  444. struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
  445. enum map_type type,
  446. const char *name,
  447. struct map **mapp,
  448. symbol_filter_t filter)
  449. {
  450. struct rb_node *nd;
  451. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  452. struct map *pos = rb_entry(nd, struct map, rb_node);
  453. struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
  454. if (sym == NULL)
  455. continue;
  456. if (mapp != NULL)
  457. *mapp = pos;
  458. return sym;
  459. }
  460. return NULL;
  461. }
  462. int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
  463. {
  464. if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
  465. if (ams->map->groups == NULL)
  466. return -1;
  467. ams->map = map_groups__find(ams->map->groups, ams->map->type,
  468. ams->addr);
  469. if (ams->map == NULL)
  470. return -1;
  471. }
  472. ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
  473. ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
  474. return ams->sym ? 0 : -1;
  475. }
  476. size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
  477. FILE *fp)
  478. {
  479. size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
  480. struct rb_node *nd;
  481. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  482. struct map *pos = rb_entry(nd, struct map, rb_node);
  483. printed += fprintf(fp, "Map:");
  484. printed += map__fprintf(pos, fp);
  485. if (verbose > 2) {
  486. printed += dso__fprintf(pos->dso, type, fp);
  487. printed += fprintf(fp, "--\n");
  488. }
  489. }
  490. return printed;
  491. }
  492. static size_t map_groups__fprintf_maps(struct map_groups *mg, FILE *fp)
  493. {
  494. size_t printed = 0, i;
  495. for (i = 0; i < MAP__NR_TYPES; ++i)
  496. printed += __map_groups__fprintf_maps(mg, i, fp);
  497. return printed;
  498. }
  499. static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
  500. enum map_type type, FILE *fp)
  501. {
  502. struct map *pos;
  503. size_t printed = 0;
  504. list_for_each_entry(pos, &mg->removed_maps[type], node) {
  505. printed += fprintf(fp, "Map:");
  506. printed += map__fprintf(pos, fp);
  507. if (verbose > 1) {
  508. printed += dso__fprintf(pos->dso, type, fp);
  509. printed += fprintf(fp, "--\n");
  510. }
  511. }
  512. return printed;
  513. }
  514. static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
  515. FILE *fp)
  516. {
  517. size_t printed = 0, i;
  518. for (i = 0; i < MAP__NR_TYPES; ++i)
  519. printed += __map_groups__fprintf_removed_maps(mg, i, fp);
  520. return printed;
  521. }
  522. size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
  523. {
  524. size_t printed = map_groups__fprintf_maps(mg, fp);
  525. printed += fprintf(fp, "Removed maps:\n");
  526. return printed + map_groups__fprintf_removed_maps(mg, fp);
  527. }
  528. int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
  529. FILE *fp)
  530. {
  531. struct rb_root *root = &mg->maps[map->type];
  532. struct rb_node *next = rb_first(root);
  533. int err = 0;
  534. while (next) {
  535. struct map *pos = rb_entry(next, struct map, rb_node);
  536. next = rb_next(&pos->rb_node);
  537. if (!map__overlap(pos, map))
  538. continue;
  539. if (verbose >= 2) {
  540. fputs("overlapping maps:\n", fp);
  541. map__fprintf(map, fp);
  542. map__fprintf(pos, fp);
  543. }
  544. rb_erase(&pos->rb_node, root);
  545. /*
  546. * Now check if we need to create new maps for areas not
  547. * overlapped by the new map:
  548. */
  549. if (map->start > pos->start) {
  550. struct map *before = map__clone(pos);
  551. if (before == NULL) {
  552. err = -ENOMEM;
  553. goto move_map;
  554. }
  555. before->end = map->start;
  556. map_groups__insert(mg, before);
  557. if (verbose >= 2)
  558. map__fprintf(before, fp);
  559. }
  560. if (map->end < pos->end) {
  561. struct map *after = map__clone(pos);
  562. if (after == NULL) {
  563. err = -ENOMEM;
  564. goto move_map;
  565. }
  566. after->start = map->end;
  567. map_groups__insert(mg, after);
  568. if (verbose >= 2)
  569. map__fprintf(after, fp);
  570. }
  571. move_map:
  572. /*
  573. * If we have references, just move them to a separate list.
  574. */
  575. if (pos->referenced)
  576. list_add_tail(&pos->node, &mg->removed_maps[map->type]);
  577. else
  578. map__delete(pos);
  579. if (err)
  580. return err;
  581. }
  582. return 0;
  583. }
  584. /*
  585. * XXX This should not really _copy_ te maps, but refcount them.
  586. */
  587. int map_groups__clone(struct map_groups *mg,
  588. struct map_groups *parent, enum map_type type)
  589. {
  590. struct rb_node *nd;
  591. for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
  592. struct map *map = rb_entry(nd, struct map, rb_node);
  593. struct map *new = map__clone(map);
  594. if (new == NULL)
  595. return -ENOMEM;
  596. map_groups__insert(mg, new);
  597. }
  598. return 0;
  599. }
  600. void maps__insert(struct rb_root *maps, struct map *map)
  601. {
  602. struct rb_node **p = &maps->rb_node;
  603. struct rb_node *parent = NULL;
  604. const u64 ip = map->start;
  605. struct map *m;
  606. while (*p != NULL) {
  607. parent = *p;
  608. m = rb_entry(parent, struct map, rb_node);
  609. if (ip < m->start)
  610. p = &(*p)->rb_left;
  611. else
  612. p = &(*p)->rb_right;
  613. }
  614. rb_link_node(&map->rb_node, parent, p);
  615. rb_insert_color(&map->rb_node, maps);
  616. }
  617. void maps__remove(struct rb_root *maps, struct map *map)
  618. {
  619. rb_erase(&map->rb_node, maps);
  620. }
  621. struct map *maps__find(struct rb_root *maps, u64 ip)
  622. {
  623. struct rb_node **p = &maps->rb_node;
  624. struct rb_node *parent = NULL;
  625. struct map *m;
  626. while (*p != NULL) {
  627. parent = *p;
  628. m = rb_entry(parent, struct map, rb_node);
  629. if (ip < m->start)
  630. p = &(*p)->rb_left;
  631. else if (ip >= m->end)
  632. p = &(*p)->rb_right;
  633. else
  634. return m;
  635. }
  636. return NULL;
  637. }
  638. struct map *maps__first(struct rb_root *maps)
  639. {
  640. struct rb_node *first = rb_first(maps);
  641. if (first)
  642. return rb_entry(first, struct map, rb_node);
  643. return NULL;
  644. }
  645. struct map *maps__next(struct map *map)
  646. {
  647. struct rb_node *next = rb_next(&map->rb_node);
  648. if (next)
  649. return rb_entry(next, struct map, rb_node);
  650. return NULL;
  651. }