map.c 20 KB

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