map.c 17 KB

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