map.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "symbol.h"
  3. #include <errno.h>
  4. #include <inttypes.h>
  5. #include <limits.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
  11. #include "map.h"
  12. #include "thread.h"
  13. #include "vdso.h"
  14. #include "build-id.h"
  15. #include "util.h"
  16. #include "debug.h"
  17. #include "machine.h"
  18. #include <linux/string.h>
  19. #include "srcline.h"
  20. #include "namespaces.h"
  21. #include "unwind.h"
  22. static void __maps__insert(struct maps *maps, struct map *map);
  23. static inline int is_anon_memory(const char *filename, u32 flags)
  24. {
  25. return flags & MAP_HUGETLB ||
  26. !strcmp(filename, "//anon") ||
  27. !strncmp(filename, "/dev/zero", sizeof("/dev/zero") - 1) ||
  28. !strncmp(filename, "/anon_hugepage", sizeof("/anon_hugepage") - 1);
  29. }
  30. static inline int is_no_dso_memory(const char *filename)
  31. {
  32. return !strncmp(filename, "[stack", 6) ||
  33. !strncmp(filename, "/SYSV",5) ||
  34. !strcmp(filename, "[heap]");
  35. }
  36. static inline int is_android_lib(const char *filename)
  37. {
  38. return !strncmp(filename, "/data/app-lib", 13) ||
  39. !strncmp(filename, "/system/lib", 11);
  40. }
  41. static inline bool replace_android_lib(const char *filename, char *newfilename)
  42. {
  43. const char *libname;
  44. char *app_abi;
  45. size_t app_abi_length, new_length;
  46. size_t lib_length = 0;
  47. libname = strrchr(filename, '/');
  48. if (libname)
  49. lib_length = strlen(libname);
  50. app_abi = getenv("APP_ABI");
  51. if (!app_abi)
  52. return false;
  53. app_abi_length = strlen(app_abi);
  54. if (!strncmp(filename, "/data/app-lib", 13)) {
  55. char *apk_path;
  56. if (!app_abi_length)
  57. return false;
  58. new_length = 7 + app_abi_length + lib_length;
  59. apk_path = getenv("APK_PATH");
  60. if (apk_path) {
  61. new_length += strlen(apk_path) + 1;
  62. if (new_length > PATH_MAX)
  63. return false;
  64. snprintf(newfilename, new_length,
  65. "%s/libs/%s/%s", apk_path, app_abi, libname);
  66. } else {
  67. if (new_length > PATH_MAX)
  68. return false;
  69. snprintf(newfilename, new_length,
  70. "libs/%s/%s", app_abi, libname);
  71. }
  72. return true;
  73. }
  74. if (!strncmp(filename, "/system/lib/", 11)) {
  75. char *ndk, *app;
  76. const char *arch;
  77. size_t ndk_length;
  78. size_t 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, 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. int map__overlap(struct map *l, struct map *r)
  323. {
  324. if (l->start > r->start) {
  325. struct map *t = l;
  326. l = r;
  327. r = t;
  328. }
  329. if (l->end > r->start)
  330. return 1;
  331. return 0;
  332. }
  333. size_t map__fprintf(struct map *map, FILE *fp)
  334. {
  335. return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n",
  336. map->start, map->end, map->pgoff, map->dso->name);
  337. }
  338. size_t map__fprintf_dsoname(struct map *map, FILE *fp)
  339. {
  340. const char *dsoname = "[unknown]";
  341. if (map && map->dso) {
  342. if (symbol_conf.show_kernel_path && map->dso->long_name)
  343. dsoname = map->dso->long_name;
  344. else
  345. dsoname = map->dso->name;
  346. }
  347. return fprintf(fp, "%s", dsoname);
  348. }
  349. char *map__srcline(struct map *map, u64 addr, struct symbol *sym)
  350. {
  351. if (map == NULL)
  352. return SRCLINE_UNKNOWN;
  353. return get_srcline(map->dso, map__rip_2objdump(map, addr), sym, true, true, addr);
  354. }
  355. int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
  356. FILE *fp)
  357. {
  358. int ret = 0;
  359. if (map && map->dso) {
  360. char *srcline = map__srcline(map, addr, NULL);
  361. if (srcline != SRCLINE_UNKNOWN)
  362. ret = fprintf(fp, "%s%s", prefix, srcline);
  363. free_srcline(srcline);
  364. }
  365. return ret;
  366. }
  367. /**
  368. * map__rip_2objdump - convert symbol start address to objdump address.
  369. * @map: memory map
  370. * @rip: symbol start address
  371. *
  372. * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN.
  373. * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is
  374. * relative to section start.
  375. *
  376. * Return: Address suitable for passing to "objdump --start-address="
  377. */
  378. u64 map__rip_2objdump(struct map *map, u64 rip)
  379. {
  380. struct kmap *kmap = __map__kmap(map);
  381. /*
  382. * vmlinux does not have program headers for PTI entry trampolines and
  383. * kcore may not either. However the trampoline object code is on the
  384. * main kernel map, so just use that instead.
  385. */
  386. if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps && kmap->kmaps->machine) {
  387. struct map *kernel_map = machine__kernel_map(kmap->kmaps->machine);
  388. if (kernel_map)
  389. map = kernel_map;
  390. }
  391. if (!map->dso->adjust_symbols)
  392. return rip;
  393. if (map->dso->rel)
  394. return rip - map->pgoff;
  395. /*
  396. * kernel modules also have DSO_TYPE_USER in dso->kernel,
  397. * but all kernel modules are ET_REL, so won't get here.
  398. */
  399. if (map->dso->kernel == DSO_TYPE_USER)
  400. return rip + map->dso->text_offset;
  401. return map->unmap_ip(map, rip) - map->reloc;
  402. }
  403. /**
  404. * map__objdump_2mem - convert objdump address to a memory address.
  405. * @map: memory map
  406. * @ip: objdump address
  407. *
  408. * Closely related to map__rip_2objdump(), this function takes an address from
  409. * objdump and converts it to a memory address. Note this assumes that @map
  410. * contains the address. To be sure the result is valid, check it forwards
  411. * e.g. map__rip_2objdump(map->map_ip(map, map__objdump_2mem(map, ip))) == ip
  412. *
  413. * Return: Memory address.
  414. */
  415. u64 map__objdump_2mem(struct map *map, u64 ip)
  416. {
  417. if (!map->dso->adjust_symbols)
  418. return map->unmap_ip(map, ip);
  419. if (map->dso->rel)
  420. return map->unmap_ip(map, ip + map->pgoff);
  421. /*
  422. * kernel modules also have DSO_TYPE_USER in dso->kernel,
  423. * but all kernel modules are ET_REL, so won't get here.
  424. */
  425. if (map->dso->kernel == DSO_TYPE_USER)
  426. return map->unmap_ip(map, ip - map->dso->text_offset);
  427. return ip + map->reloc;
  428. }
  429. static void maps__init(struct maps *maps)
  430. {
  431. maps->entries = RB_ROOT;
  432. init_rwsem(&maps->lock);
  433. }
  434. void map_groups__init(struct map_groups *mg, struct machine *machine)
  435. {
  436. maps__init(&mg->maps);
  437. mg->machine = machine;
  438. refcount_set(&mg->refcnt, 1);
  439. }
  440. static void __maps__purge(struct maps *maps)
  441. {
  442. struct rb_root *root = &maps->entries;
  443. struct rb_node *next = rb_first(root);
  444. while (next) {
  445. struct map *pos = rb_entry(next, struct map, rb_node);
  446. next = rb_next(&pos->rb_node);
  447. rb_erase_init(&pos->rb_node, root);
  448. map__put(pos);
  449. }
  450. }
  451. static void maps__exit(struct maps *maps)
  452. {
  453. down_write(&maps->lock);
  454. __maps__purge(maps);
  455. up_write(&maps->lock);
  456. }
  457. void map_groups__exit(struct map_groups *mg)
  458. {
  459. maps__exit(&mg->maps);
  460. }
  461. bool map_groups__empty(struct map_groups *mg)
  462. {
  463. return !maps__first(&mg->maps);
  464. }
  465. struct map_groups *map_groups__new(struct machine *machine)
  466. {
  467. struct map_groups *mg = malloc(sizeof(*mg));
  468. if (mg != NULL)
  469. map_groups__init(mg, machine);
  470. return mg;
  471. }
  472. void map_groups__delete(struct map_groups *mg)
  473. {
  474. map_groups__exit(mg);
  475. free(mg);
  476. }
  477. void map_groups__put(struct map_groups *mg)
  478. {
  479. if (mg && refcount_dec_and_test(&mg->refcnt))
  480. map_groups__delete(mg);
  481. }
  482. struct symbol *map_groups__find_symbol(struct map_groups *mg,
  483. u64 addr, struct map **mapp)
  484. {
  485. struct map *map = map_groups__find(mg, addr);
  486. /* Ensure map is loaded before using map->map_ip */
  487. if (map != NULL && map__load(map) >= 0) {
  488. if (mapp != NULL)
  489. *mapp = map;
  490. return map__find_symbol(map, map->map_ip(map, addr));
  491. }
  492. return NULL;
  493. }
  494. struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name,
  495. struct map **mapp)
  496. {
  497. struct symbol *sym;
  498. struct rb_node *nd;
  499. down_read(&maps->lock);
  500. for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
  501. struct map *pos = rb_entry(nd, struct map, rb_node);
  502. sym = map__find_symbol_by_name(pos, name);
  503. if (sym == NULL)
  504. continue;
  505. if (mapp != NULL)
  506. *mapp = pos;
  507. goto out;
  508. }
  509. sym = NULL;
  510. out:
  511. up_read(&maps->lock);
  512. return sym;
  513. }
  514. struct symbol *map_groups__find_symbol_by_name(struct map_groups *mg,
  515. const char *name,
  516. struct map **mapp)
  517. {
  518. return maps__find_symbol_by_name(&mg->maps, name, mapp);
  519. }
  520. int map_groups__find_ams(struct addr_map_symbol *ams)
  521. {
  522. if (ams->addr < ams->map->start || ams->addr >= ams->map->end) {
  523. if (ams->map->groups == NULL)
  524. return -1;
  525. ams->map = map_groups__find(ams->map->groups, ams->addr);
  526. if (ams->map == NULL)
  527. return -1;
  528. }
  529. ams->al_addr = ams->map->map_ip(ams->map, ams->addr);
  530. ams->sym = map__find_symbol(ams->map, ams->al_addr);
  531. return ams->sym ? 0 : -1;
  532. }
  533. static size_t maps__fprintf(struct maps *maps, FILE *fp)
  534. {
  535. size_t printed = 0;
  536. struct rb_node *nd;
  537. down_read(&maps->lock);
  538. for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
  539. struct map *pos = rb_entry(nd, struct map, rb_node);
  540. printed += fprintf(fp, "Map:");
  541. printed += map__fprintf(pos, fp);
  542. if (verbose > 2) {
  543. printed += dso__fprintf(pos->dso, fp);
  544. printed += fprintf(fp, "--\n");
  545. }
  546. }
  547. up_read(&maps->lock);
  548. return printed;
  549. }
  550. size_t map_groups__fprintf(struct map_groups *mg, FILE *fp)
  551. {
  552. return maps__fprintf(&mg->maps, fp);
  553. }
  554. static void __map_groups__insert(struct map_groups *mg, struct map *map)
  555. {
  556. __maps__insert(&mg->maps, map);
  557. map->groups = mg;
  558. }
  559. static int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
  560. {
  561. struct rb_root *root;
  562. struct rb_node *next;
  563. int err = 0;
  564. down_write(&maps->lock);
  565. root = &maps->entries;
  566. next = rb_first(root);
  567. while (next) {
  568. struct map *pos = rb_entry(next, struct map, rb_node);
  569. next = rb_next(&pos->rb_node);
  570. if (!map__overlap(pos, map))
  571. continue;
  572. if (verbose >= 2) {
  573. if (use_browser) {
  574. pr_warning("overlapping maps in %s "
  575. "(disable tui for more info)\n",
  576. map->dso->name);
  577. } else {
  578. fputs("overlapping maps:\n", fp);
  579. map__fprintf(map, fp);
  580. map__fprintf(pos, fp);
  581. }
  582. }
  583. rb_erase_init(&pos->rb_node, root);
  584. /*
  585. * Now check if we need to create new maps for areas not
  586. * overlapped by the new map:
  587. */
  588. if (map->start > pos->start) {
  589. struct map *before = map__clone(pos);
  590. if (before == NULL) {
  591. err = -ENOMEM;
  592. goto put_map;
  593. }
  594. before->end = map->start;
  595. __map_groups__insert(pos->groups, before);
  596. if (verbose >= 2 && !use_browser)
  597. map__fprintf(before, fp);
  598. map__put(before);
  599. }
  600. if (map->end < pos->end) {
  601. struct map *after = map__clone(pos);
  602. if (after == NULL) {
  603. err = -ENOMEM;
  604. goto put_map;
  605. }
  606. after->start = map->end;
  607. __map_groups__insert(pos->groups, after);
  608. if (verbose >= 2 && !use_browser)
  609. map__fprintf(after, fp);
  610. map__put(after);
  611. }
  612. put_map:
  613. map__put(pos);
  614. if (err)
  615. goto out;
  616. }
  617. err = 0;
  618. out:
  619. up_write(&maps->lock);
  620. return err;
  621. }
  622. int map_groups__fixup_overlappings(struct map_groups *mg, struct map *map,
  623. FILE *fp)
  624. {
  625. return maps__fixup_overlappings(&mg->maps, map, fp);
  626. }
  627. /*
  628. * XXX This should not really _copy_ te maps, but refcount them.
  629. */
  630. int map_groups__clone(struct thread *thread, struct map_groups *parent)
  631. {
  632. struct map_groups *mg = thread->mg;
  633. int err = -ENOMEM;
  634. struct map *map;
  635. struct maps *maps = &parent->maps;
  636. down_read(&maps->lock);
  637. for (map = maps__first(maps); map; map = map__next(map)) {
  638. struct map *new = map__clone(map);
  639. if (new == NULL)
  640. goto out_unlock;
  641. err = unwind__prepare_access(thread, new, NULL);
  642. if (err)
  643. goto out_unlock;
  644. map_groups__insert(mg, new);
  645. map__put(new);
  646. }
  647. err = 0;
  648. out_unlock:
  649. up_read(&maps->lock);
  650. return err;
  651. }
  652. static void __maps__insert(struct maps *maps, struct map *map)
  653. {
  654. struct rb_node **p = &maps->entries.rb_node;
  655. struct rb_node *parent = NULL;
  656. const u64 ip = map->start;
  657. struct map *m;
  658. while (*p != NULL) {
  659. parent = *p;
  660. m = rb_entry(parent, struct map, rb_node);
  661. if (ip < m->start)
  662. p = &(*p)->rb_left;
  663. else
  664. p = &(*p)->rb_right;
  665. }
  666. rb_link_node(&map->rb_node, parent, p);
  667. rb_insert_color(&map->rb_node, &maps->entries);
  668. map__get(map);
  669. }
  670. void maps__insert(struct maps *maps, struct map *map)
  671. {
  672. down_write(&maps->lock);
  673. __maps__insert(maps, map);
  674. up_write(&maps->lock);
  675. }
  676. static void __maps__remove(struct maps *maps, struct map *map)
  677. {
  678. rb_erase_init(&map->rb_node, &maps->entries);
  679. map__put(map);
  680. }
  681. void maps__remove(struct maps *maps, struct map *map)
  682. {
  683. down_write(&maps->lock);
  684. __maps__remove(maps, map);
  685. up_write(&maps->lock);
  686. }
  687. struct map *maps__find(struct maps *maps, u64 ip)
  688. {
  689. struct rb_node **p, *parent = NULL;
  690. struct map *m;
  691. down_read(&maps->lock);
  692. p = &maps->entries.rb_node;
  693. while (*p != NULL) {
  694. parent = *p;
  695. m = rb_entry(parent, struct map, rb_node);
  696. if (ip < m->start)
  697. p = &(*p)->rb_left;
  698. else if (ip >= m->end)
  699. p = &(*p)->rb_right;
  700. else
  701. goto out;
  702. }
  703. m = NULL;
  704. out:
  705. up_read(&maps->lock);
  706. return m;
  707. }
  708. struct map *maps__first(struct maps *maps)
  709. {
  710. struct rb_node *first = rb_first(&maps->entries);
  711. if (first)
  712. return rb_entry(first, struct map, rb_node);
  713. return NULL;
  714. }
  715. struct map *map__next(struct map *map)
  716. {
  717. struct rb_node *next = rb_next(&map->rb_node);
  718. if (next)
  719. return rb_entry(next, struct map, rb_node);
  720. return NULL;
  721. }
  722. struct kmap *__map__kmap(struct map *map)
  723. {
  724. if (!map->dso || !map->dso->kernel)
  725. return NULL;
  726. return (struct kmap *)(map + 1);
  727. }
  728. struct kmap *map__kmap(struct map *map)
  729. {
  730. struct kmap *kmap = __map__kmap(map);
  731. if (!kmap)
  732. pr_err("Internal error: map__kmap with a non-kernel map\n");
  733. return kmap;
  734. }
  735. struct map_groups *map__kmaps(struct map *map)
  736. {
  737. struct kmap *kmap = map__kmap(map);
  738. if (!kmap || !kmap->kmaps) {
  739. pr_err("Internal error: map__kmaps with a non-kernel map\n");
  740. return NULL;
  741. }
  742. return kmap->kmaps;
  743. }