map.c 20 KB

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