map.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. int __weak arch__compare_symbol_names(const char *namea, const char *nameb)
  248. {
  249. return strcmp(namea, nameb);
  250. }
  251. struct symbol *map__find_symbol(struct map *map, u64 addr,
  252. symbol_filter_t filter)
  253. {
  254. if (map__load(map, filter) < 0)
  255. return NULL;
  256. return dso__find_symbol(map->dso, map->type, addr);
  257. }
  258. struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
  259. symbol_filter_t filter)
  260. {
  261. if (map__load(map, filter) < 0)
  262. return NULL;
  263. if (!dso__sorted_by_name(map->dso, map->type))
  264. dso__sort_by_name(map->dso, map->type);
  265. return dso__find_symbol_by_name(map->dso, map->type, name);
  266. }
  267. struct map *map__clone(struct map *map)
  268. {
  269. return memdup(map, sizeof(*map));
  270. }
  271. int map__overlap(struct map *l, struct map *r)
  272. {
  273. if (l->start > r->start) {
  274. struct map *t = l;
  275. l = r;
  276. r = t;
  277. }
  278. if (l->end > r->start)
  279. return 1;
  280. return 0;
  281. }
  282. size_t map__fprintf(struct map *map, FILE *fp)
  283. {
  284. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
  285. map->start, map->end, map->pgoff, map->dso->name);
  286. }
  287. size_t map__fprintf_dsoname(struct map *map, FILE *fp)
  288. {
  289. const char *dsoname = "[unknown]";
  290. if (map && map->dso && (map->dso->name || map->dso->long_name)) {
  291. if (symbol_conf.show_kernel_path && map->dso->long_name)
  292. dsoname = map->dso->long_name;
  293. else if (map->dso->name)
  294. dsoname = map->dso->name;
  295. }
  296. return fprintf(fp, "%s", dsoname);
  297. }
  298. int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
  299. FILE *fp)
  300. {
  301. char *srcline;
  302. int ret = 0;
  303. if (map && map->dso) {
  304. srcline = get_srcline(map->dso,
  305. map__rip_2objdump(map, addr), NULL, true);
  306. if (srcline != SRCLINE_UNKNOWN)
  307. ret = fprintf(fp, "%s%s", prefix, srcline);
  308. free_srcline(srcline);
  309. }
  310. return ret;
  311. }
  312. /**
  313. * map__rip_2objdump - convert symbol start address to objdump address.
  314. * @map: memory map
  315. * @rip: symbol start address
  316. *
  317. * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
  318. * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
  319. * relative to section start.
  320. *
  321. * Return: Address suitable for passing to "objdump --start-address="
  322. */
  323. u64 map__rip_2objdump(struct map *map, u64 rip)
  324. {
  325. if (!map->dso->adjust_symbols)
  326. return rip;
  327. if (map->dso->rel)
  328. return rip - map->pgoff;
  329. return map->unmap_ip(map, rip) - map->reloc;
  330. }
  331. /**
  332. * map__objdump_2mem - convert objdump address to a memory address.
  333. * @map: memory map
  334. * @ip: objdump address
  335. *
  336. * Closely related to map__rip_2objdump(), this function takes an address from
  337. * objdump and converts it to a memory address. Note this assumes that @map
  338. * contains the address. To be sure the result is valid, check it forwards
  339. * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
  340. *
  341. * Return: Memory address.
  342. */
  343. u64 map__objdump_2mem(struct map *map, u64 ip)
  344. {
  345. if (!map->dso->adjust_symbols)
  346. return map->unmap_ip(map, ip);
  347. if (map->dso->rel)
  348. return map->unmap_ip(map, ip + map->pgoff);
  349. return ip + map->reloc;
  350. }
  351. void map_groups__init(struct map_groups *mg, struct machine *machine)
  352. {
  353. int i;
  354. for (i = 0; i < MAP__NR_TYPES; ++i) {
  355. mg->maps[i] = RB_ROOT;
  356. INIT_LIST_HEAD(&mg->removed_maps[i]);
  357. }
  358. mg->machine = machine;
  359. atomic_set(&mg->refcnt, 1);
  360. }
  361. static void maps__delete(struct rb_root *maps)
  362. {
  363. struct rb_node *next = rb_first(maps);
  364. while (next) {
  365. struct map *pos = rb_entry(next, struct map, rb_node);
  366. next = rb_next(&pos->rb_node);
  367. rb_erase(&pos->rb_node, maps);
  368. map__delete(pos);
  369. }
  370. }
  371. static void maps__delete_removed(struct list_head *maps)
  372. {
  373. struct map *pos, *n;
  374. list_for_each_entry_safe(pos, n, maps, node) {
  375. list_del(&pos->node);
  376. map__delete(pos);
  377. }
  378. }
  379. void map_groups__exit(struct map_groups *mg)
  380. {
  381. int i;
  382. for (i = 0; i < MAP__NR_TYPES; ++i) {
  383. maps__delete(&mg->maps[i]);
  384. maps__delete_removed(&mg->removed_maps[i]);
  385. }
  386. }
  387. bool map_groups__empty(struct map_groups *mg)
  388. {
  389. int i;
  390. for (i = 0; i < MAP__NR_TYPES; ++i) {
  391. if (maps__first(&mg->maps[i]))
  392. return false;
  393. if (!list_empty(&mg->removed_maps[i]))
  394. return false;
  395. }
  396. return true;
  397. }
  398. struct map_groups *map_groups__new(struct machine *machine)
  399. {
  400. struct map_groups *mg = malloc(sizeof(*mg));
  401. if (mg != NULL)
  402. map_groups__init(mg, machine);
  403. return mg;
  404. }
  405. void map_groups__delete(struct map_groups *mg)
  406. {
  407. map_groups__exit(mg);
  408. free(mg);
  409. }
  410. void map_groups__put(struct map_groups *mg)
  411. {
  412. if (mg && atomic_dec_and_test(&mg->refcnt))
  413. map_groups__delete(mg);
  414. }
  415. struct symbol *map_groups__find_symbol(struct map_groups *mg,
  416. enum map_type type, u64 addr,
  417. struct map **mapp,
  418. symbol_filter_t filter)
  419. {
  420. struct map *map = map_groups__find(mg, type, addr);
  421. /* Ensure map is loaded before using map->map_ip */
  422. if (map != NULL && map__load(map, filter) >= 0) {
  423. if (mapp != NULL)
  424. *mapp = map;
  425. return map__find_symbol(map, map->map_ip(map, addr), filter);
  426. }
  427. return NULL;
  428. }
  429. struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
  430. enum map_type type,
  431. const char *name,
  432. struct map **mapp,
  433. symbol_filter_t filter)
  434. {
  435. struct rb_node *nd;
  436. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  437. struct map *pos = rb_entry(nd, struct map, rb_node);
  438. struct symbol *sym = map__find_symbol_by_name(pos, name, filter);
  439. if (sym == NULL)
  440. continue;
  441. if (mapp != NULL)
  442. *mapp = pos;
  443. return sym;
  444. }
  445. return NULL;
  446. }
  447. int map_groups__find_ams(struct addr_map_symbol *ams, symbol_filter_t filter)
  448. {
  449. if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
  450. if (ams->map->groups == NULL)
  451. return -1;
  452. ams->map = map_groups__find(ams->map->groups, ams->map->type,
  453. ams->addr);
  454. if (ams->map == NULL)
  455. return -1;
  456. }
  457. ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
  458. ams->sym = map__find_symbol(ams->map, ams->al_addr, filter);
  459. return ams->sym ? 0 : -1;
  460. }
  461. size_t __map_groups__fprintf_maps(struct map_groups *mg, enum map_type type,
  462. FILE *fp)
  463. {
  464. size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
  465. struct rb_node *nd;
  466. for (nd = rb_first(&mg->maps[type]); nd; nd = rb_next(nd)) {
  467. struct map *pos = rb_entry(nd, struct map, rb_node);
  468. printed += fprintf(fp, "Map:");
  469. printed += map__fprintf(pos, fp);
  470. if (verbose > 2) {
  471. printed += dso__fprintf(pos->dso, type, fp);
  472. printed += fprintf(fp, "--\n");
  473. }
  474. }
  475. return printed;
  476. }
  477. static size_t map_groups__fprintf_maps(struct map_groups *mg, FILE *fp)
  478. {
  479. size_t printed = 0, i;
  480. for (i = 0; i < MAP__NR_TYPES; ++i)
  481. printed += __map_groups__fprintf_maps(mg, i, fp);
  482. return printed;
  483. }
  484. static size_t __map_groups__fprintf_removed_maps(struct map_groups *mg,
  485. enum map_type type, FILE *fp)
  486. {
  487. struct map *pos;
  488. size_t printed = 0;
  489. list_for_each_entry(pos, &mg->removed_maps[type], node) {
  490. printed += fprintf(fp, "Map:");
  491. printed += map__fprintf(pos, fp);
  492. if (verbose > 1) {
  493. printed += dso__fprintf(pos->dso, type, fp);
  494. printed += fprintf(fp, "--\n");
  495. }
  496. }
  497. return printed;
  498. }
  499. static size_t map_groups__fprintf_removed_maps(struct map_groups *mg,
  500. FILE *fp)
  501. {
  502. size_t printed = 0, i;
  503. for (i = 0; i < MAP__NR_TYPES; ++i)
  504. printed += __map_groups__fprintf_removed_maps(mg, i, fp);
  505. return printed;
  506. }
  507. size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
  508. {
  509. size_t printed = map_groups__fprintf_maps(mg, fp);
  510. printed += fprintf(fp, "Removed maps:\n");
  511. return printed + map_groups__fprintf_removed_maps(mg, fp);
  512. }
  513. int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
  514. FILE *fp)
  515. {
  516. struct rb_root *root = &mg->maps[map->type];
  517. struct rb_node *next = rb_first(root);
  518. int err = 0;
  519. while (next) {
  520. struct map *pos = rb_entry(next, struct map, rb_node);
  521. next = rb_next(&pos->rb_node);
  522. if (!map__overlap(pos, map))
  523. continue;
  524. if (verbose >= 2) {
  525. fputs("overlapping maps:\n", fp);
  526. map__fprintf(map, fp);
  527. map__fprintf(pos, fp);
  528. }
  529. rb_erase(&pos->rb_node, root);
  530. /*
  531. * Now check if we need to create new maps for areas not
  532. * overlapped by the new map:
  533. */
  534. if (map->start > pos->start) {
  535. struct map *before = map__clone(pos);
  536. if (before == NULL) {
  537. err = -ENOMEM;
  538. goto move_map;
  539. }
  540. before->end = map->start;
  541. map_groups__insert(mg, before);
  542. if (verbose >= 2)
  543. map__fprintf(before, fp);
  544. }
  545. if (map->end < pos->end) {
  546. struct map *after = map__clone(pos);
  547. if (after == NULL) {
  548. err = -ENOMEM;
  549. goto move_map;
  550. }
  551. after->start = map->end;
  552. map_groups__insert(mg, after);
  553. if (verbose >= 2)
  554. map__fprintf(after, fp);
  555. }
  556. move_map:
  557. /*
  558. * If we have references, just move them to a separate list.
  559. */
  560. if (pos->referenced)
  561. list_add_tail(&pos->node, &mg->removed_maps[map->type]);
  562. else
  563. map__delete(pos);
  564. if (err)
  565. return err;
  566. }
  567. return 0;
  568. }
  569. /*
  570. * XXX This should not really _copy_ te maps, but refcount them.
  571. */
  572. int map_groups__clone(struct map_groups *mg,
  573. struct map_groups *parent, enum map_type type)
  574. {
  575. struct map *map;
  576. struct rb_root *maps = &parent->maps[type];
  577. for (map = maps__first(maps); map; map = map__next(map)) {
  578. struct map *new = map__clone(map);
  579. if (new == NULL)
  580. return -ENOMEM;
  581. map_groups__insert(mg, new);
  582. }
  583. return 0;
  584. }
  585. void maps__insert(struct rb_root *maps, struct map *map)
  586. {
  587. struct rb_node **p = &maps->rb_node;
  588. struct rb_node *parent = NULL;
  589. const u64 ip = map->start;
  590. struct map *m;
  591. while (*p != NULL) {
  592. parent = *p;
  593. m = rb_entry(parent, struct map, rb_node);
  594. if (ip < m->start)
  595. p = &(*p)->rb_left;
  596. else
  597. p = &(*p)->rb_right;
  598. }
  599. rb_link_node(&map->rb_node, parent, p);
  600. rb_insert_color(&map->rb_node, maps);
  601. }
  602. void maps__remove(struct rb_root *maps, struct map *map)
  603. {
  604. rb_erase(&map->rb_node, maps);
  605. }
  606. struct map *maps__find(struct rb_root *maps, u64 ip)
  607. {
  608. struct rb_node **p = &maps->rb_node;
  609. struct rb_node *parent = NULL;
  610. struct map *m;
  611. while (*p != NULL) {
  612. parent = *p;
  613. m = rb_entry(parent, struct map, rb_node);
  614. if (ip < m->start)
  615. p = &(*p)->rb_left;
  616. else if (ip >= m->end)
  617. p = &(*p)->rb_right;
  618. else
  619. return m;
  620. }
  621. return NULL;
  622. }
  623. struct map *maps__first(struct rb_root *maps)
  624. {
  625. struct rb_node *first = rb_first(maps);
  626. if (first)
  627. return rb_entry(first, struct map, rb_node);
  628. return NULL;
  629. }
  630. struct map *map__next(struct map *map)
  631. {
  632. struct rb_node *next = rb_next(&map->rb_node);
  633. if (next)
  634. return rb_entry(next, struct map, rb_node);
  635. return NULL;
  636. }
  637. struct kmap *map__kmap(struct map *map)
  638. {
  639. if (!map->dso || !map->dso->kernel) {
  640. pr_err("Internal error: map__kmap with a non-kernel map\n");
  641. return NULL;
  642. }
  643. return (struct kmap *)(map + 1);
  644. }
  645. struct map_groups *map__kmaps(struct map *map)
  646. {
  647. struct kmap *kmap = map__kmap(map);
  648. if (!kmap || !kmap->kmaps) {
  649. pr_err("Internal error: map__kmaps with a non-kernel map\n");
  650. return NULL;
  651. }
  652. return kmap->kmaps;
  653. }