map.c 20 KB

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