map.c 20 KB

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