map.c 21 KB

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