Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
9486aa38 | 2 | #include <inttypes.h> |
4b8cf846 | 3 | #include <limits.h> |
9d31d18b | 4 | #include <stdio.h> |
66e274f3 FW |
5 | #include <stdlib.h> |
6 | #include <string.h> | |
9d31d18b IR |
7 | #include <linux/string.h> |
8 | #include <linux/zalloc.h> | |
fbef103f | 9 | #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */ |
9d31d18b | 10 | #include "debug.h" |
4a3cec84 | 11 | #include "dso.h" |
4b8cf846 | 12 | #include "map.h" |
9d31d18b IR |
13 | #include "namespaces.h" |
14 | #include "srcline.h" | |
15 | #include "symbol.h" | |
5cd95c2d | 16 | #include "thread.h" |
7dbf4dcf | 17 | #include "vdso.h" |
6a2ffcdd | 18 | |
eca81836 ML |
19 | static inline int is_android_lib(const char *filename) |
20 | { | |
bdadd647 ACM |
21 | return strstarts(filename, "/data/app-lib/") || |
22 | strstarts(filename, "/system/lib/"); | |
eca81836 ML |
23 | } |
24 | ||
25 | static inline bool replace_android_lib(const char *filename, char *newfilename) | |
26 | { | |
27 | const char *libname; | |
28 | char *app_abi; | |
29 | size_t app_abi_length, new_length; | |
30 | size_t lib_length = 0; | |
31 | ||
32 | libname = strrchr(filename, '/'); | |
33 | if (libname) | |
34 | lib_length = strlen(libname); | |
35 | ||
36 | app_abi = getenv("APP_ABI"); | |
37 | if (!app_abi) | |
38 | return false; | |
39 | ||
40 | app_abi_length = strlen(app_abi); | |
41 | ||
bdadd647 | 42 | if (strstarts(filename, "/data/app-lib/")) { |
eca81836 ML |
43 | char *apk_path; |
44 | ||
45 | if (!app_abi_length) | |
46 | return false; | |
47 | ||
48 | new_length = 7 + app_abi_length + lib_length; | |
49 | ||
50 | apk_path = getenv("APK_PATH"); | |
51 | if (apk_path) { | |
52 | new_length += strlen(apk_path) + 1; | |
53 | if (new_length > PATH_MAX) | |
54 | return false; | |
55 | snprintf(newfilename, new_length, | |
56 | "%s/libs/%s/%s", apk_path, app_abi, libname); | |
57 | } else { | |
58 | if (new_length > PATH_MAX) | |
59 | return false; | |
60 | snprintf(newfilename, new_length, | |
61 | "libs/%s/%s", app_abi, libname); | |
62 | } | |
63 | return true; | |
64 | } | |
65 | ||
bdadd647 | 66 | if (strstarts(filename, "/system/lib/")) { |
eca81836 ML |
67 | char *ndk, *app; |
68 | const char *arch; | |
77d02bd0 | 69 | int ndk_length, app_length; |
eca81836 ML |
70 | |
71 | ndk = getenv("NDK_ROOT"); | |
72 | app = getenv("APP_PLATFORM"); | |
73 | ||
74 | if (!(ndk && app)) | |
75 | return false; | |
76 | ||
77 | ndk_length = strlen(ndk); | |
78 | app_length = strlen(app); | |
79 | ||
80 | if (!(ndk_length && app_length && app_abi_length)) | |
81 | return false; | |
82 | ||
83 | arch = !strncmp(app_abi, "arm", 3) ? "arm" : | |
84 | !strncmp(app_abi, "mips", 4) ? "mips" : | |
85 | !strncmp(app_abi, "x86", 3) ? "x86" : NULL; | |
86 | ||
87 | if (!arch) | |
88 | return false; | |
89 | ||
90 | new_length = 27 + ndk_length + | |
91 | app_length + lib_length | |
92 | + strlen(arch); | |
93 | ||
94 | if (new_length > PATH_MAX) | |
95 | return false; | |
96 | snprintf(newfilename, new_length, | |
77d02bd0 ACM |
97 | "%.*s/platforms/%.*s/arch-%s/usr/lib/%s", |
98 | ndk_length, ndk, app_length, app, arch, libname); | |
eca81836 ML |
99 | |
100 | return true; | |
101 | } | |
102 | return false; | |
103 | } | |
104 | ||
3183f8ca | 105 | void map__init(struct map *map, u64 start, u64 end, u64 pgoff, struct dso *dso) |
afb7b4f0 | 106 | { |
e6a9efce ACM |
107 | map__set_start(map, start); |
108 | map__set_end(map, end); | |
109 | map__set_pgoff(map, pgoff); | |
110 | map__set_reloc(map, 0); | |
111 | map__set_dso(map, dso__get(dso)); | |
9fa688ea | 112 | map__set_mapping_type(map, MAPPING_TYPE__DSO); |
e6a9efce | 113 | map__set_erange_warned(map, false); |
e1805aae | 114 | refcount_set(map__refcnt(map), 1); |
afb7b4f0 ACM |
115 | } |
116 | ||
2a03068c | 117 | struct map *map__new(struct machine *machine, u64 start, u64 len, |
4a7380a5 | 118 | u64 pgoff, struct dso_id *id, |
1ca6e802 JO |
119 | u32 prot, u32 flags, struct build_id *bid, |
120 | char *filename, struct thread *thread) | |
66e274f3 | 121 | { |
2832ef81 IR |
122 | struct map *result; |
123 | RC_STRUCT(map) *map; | |
bf2e710b KJ |
124 | struct nsinfo *nsi = NULL; |
125 | struct nsinfo *nnsi; | |
66e274f3 | 126 | |
2832ef81 IR |
127 | map = malloc(sizeof(*map)); |
128 | if (ADD_RC_CHK(result, map)) { | |
66e274f3 | 129 | char newfilename[PATH_MAX]; |
f693dac4 | 130 | struct dso *dso, *header_bid_dso; |
eca81836 | 131 | int anon, no_dso, vdso, android; |
66e274f3 | 132 | |
eca81836 | 133 | android = is_android_lib(filename); |
e7b60c5a | 134 | anon = is_anon_memory(filename) || flags & MAP_HUGETLB; |
7dbf4dcf | 135 | vdso = is_vdso_map(filename); |
87ffef79 | 136 | no_dso = is_no_dso_memory(filename); |
7ef80703 DZ |
137 | map->prot = prot; |
138 | map->flags = flags; | |
ee84a303 | 139 | nsi = nsinfo__get(thread__nsinfo(thread)); |
5c5e854b | 140 | |
d183b261 | 141 | if ((anon || no_dso) && nsi && (prot & PROT_EXEC)) { |
bf2e710b | 142 | snprintf(newfilename, sizeof(newfilename), |
bcaf0a97 | 143 | "/tmp/perf-%d.map", nsinfo__pid(nsi)); |
66e274f3 FW |
144 | filename = newfilename; |
145 | } | |
146 | ||
eca81836 ML |
147 | if (android) { |
148 | if (replace_android_lib(filename, newfilename)) | |
149 | filename = newfilename; | |
150 | } | |
151 | ||
7dbf4dcf | 152 | if (vdso) { |
bf2e710b KJ |
153 | /* The vdso maps are always on the host and not the |
154 | * container. Ensure that we don't use setns to look | |
155 | * them up. | |
156 | */ | |
157 | nnsi = nsinfo__copy(nsi); | |
158 | if (nnsi) { | |
159 | nsinfo__put(nsi); | |
bcaf0a97 | 160 | nsinfo__clear_need_setns(nnsi); |
bf2e710b KJ |
161 | nsi = nnsi; |
162 | } | |
7dbf4dcf | 163 | pgoff = 0; |
9a4388c7 | 164 | dso = machine__findnew_vdso(machine, thread); |
7dbf4dcf | 165 | } else |
0e3149f8 | 166 | dso = machine__findnew_dso_id(machine, filename, id); |
7dbf4dcf | 167 | |
afb7b4f0 | 168 | if (dso == NULL) |
66e274f3 FW |
169 | goto out_delete; |
170 | ||
659ad349 | 171 | assert(!dso->kernel); |
2832ef81 | 172 | map__init(result, start, start + len, pgoff, dso); |
afb7b4f0 | 173 | |
87ffef79 | 174 | if (anon || no_dso) { |
9fa688ea | 175 | map->mapping_type = MAPPING_TYPE__IDENTITY; |
87ffef79 JO |
176 | |
177 | /* | |
178 | * Set memory without DSO as loaded. All map__find_* | |
179 | * functions still return NULL, and we avoid the | |
180 | * unnecessary map__load warning. | |
181 | */ | |
d183b261 | 182 | if (!(prot & PROT_EXEC)) |
3183f8ca | 183 | dso__set_loaded(dso); |
8d92c02a | 184 | } |
e54dea69 IR |
185 | mutex_lock(&dso->lock); |
186 | nsinfo__put(dso->nsinfo); | |
bf2e710b | 187 | dso->nsinfo = nsi; |
e54dea69 | 188 | mutex_unlock(&dso->lock); |
1ca6e802 | 189 | |
f693dac4 | 190 | if (build_id__is_defined(bid)) { |
1ca6e802 | 191 | dso__set_build_id(dso, bid); |
f693dac4 JC |
192 | } else { |
193 | /* | |
194 | * If the mmap event had no build ID, search for an existing dso from the | |
195 | * build ID header by name. Otherwise only the dso loaded at the time of | |
196 | * reading the header will have the build ID set and all future mmaps will | |
197 | * have it missing. | |
198 | */ | |
199 | down_read(&machine->dsos.lock); | |
200 | header_bid_dso = __dsos__find(&machine->dsos, filename, false); | |
201 | up_read(&machine->dsos.lock); | |
202 | if (header_bid_dso && header_bid_dso->header_build_id) { | |
203 | dso__set_build_id(dso, &header_bid_dso->bid); | |
204 | dso->header_build_id = 1; | |
205 | } | |
206 | } | |
d3a7c489 | 207 | dso__put(dso); |
66e274f3 | 208 | } |
2832ef81 | 209 | return result; |
66e274f3 | 210 | out_delete: |
bf2e710b | 211 | nsinfo__put(nsi); |
2832ef81 | 212 | RC_CHK_FREE(result); |
66e274f3 FW |
213 | return NULL; |
214 | } | |
215 | ||
e5a1845f NK |
216 | /* |
217 | * Constructor variant for modules (where we know from /proc/modules where | |
218 | * they are loaded) and for vmlinux, where only after we load all the | |
219 | * symbols we'll know where it starts and ends. | |
220 | */ | |
3183f8ca | 221 | struct map *map__new2(u64 start, struct dso *dso) |
e5a1845f | 222 | { |
2832ef81 IR |
223 | struct map *result; |
224 | RC_STRUCT(map) *map; | |
225 | ||
226 | map = calloc(1, sizeof(*map) + (dso->kernel ? sizeof(struct kmap) : 0)); | |
227 | if (ADD_RC_CHK(result, map)) { | |
e5a1845f NK |
228 | /* |
229 | * ->end will be filled after we load all the symbols | |
230 | */ | |
2832ef81 | 231 | map__init(result, start, 0, 0, dso); |
e5a1845f NK |
232 | } |
233 | ||
2832ef81 | 234 | return result; |
e5a1845f NK |
235 | } |
236 | ||
e6ce7126 ACM |
237 | bool __map__is_kernel(const struct map *map) |
238 | { | |
63df0e4b | 239 | if (!map__dso(map)->kernel) |
de90d513 | 240 | return false; |
5ab6d715 | 241 | return machine__kernel_map(maps__machine(map__kmaps((struct map *)map))) == map; |
e6ce7126 ACM |
242 | } |
243 | ||
5759a682 AH |
244 | bool __map__is_extra_kernel_map(const struct map *map) |
245 | { | |
246 | struct kmap *kmap = __map__kmap((struct map *)map); | |
247 | ||
248 | return kmap && kmap->name[0]; | |
249 | } | |
250 | ||
a93e0b23 SL |
251 | bool __map__is_bpf_prog(const struct map *map) |
252 | { | |
253 | const char *name; | |
63df0e4b | 254 | struct dso *dso = map__dso(map); |
a93e0b23 | 255 | |
63df0e4b | 256 | if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO) |
a93e0b23 SL |
257 | return true; |
258 | ||
259 | /* | |
260 | * If PERF_RECORD_BPF_EVENT is not included, the dso will not have | |
261 | * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can | |
262 | * guess the type based on name. | |
263 | */ | |
63df0e4b | 264 | name = dso->short_name; |
a93e0b23 SL |
265 | return name && (strstr(name, "bpf_prog_") == name); |
266 | } | |
267 | ||
830fadfd JO |
268 | bool __map__is_bpf_image(const struct map *map) |
269 | { | |
270 | const char *name; | |
63df0e4b | 271 | struct dso *dso = map__dso(map); |
830fadfd | 272 | |
63df0e4b | 273 | if (dso->binary_type == DSO_BINARY_TYPE__BPF_IMAGE) |
830fadfd JO |
274 | return true; |
275 | ||
276 | /* | |
277 | * If PERF_RECORD_KSYMBOL is not included, the dso will not have | |
278 | * type of DSO_BINARY_TYPE__BPF_IMAGE. In such cases, we can | |
279 | * guess the type based on name. | |
280 | */ | |
63df0e4b | 281 | name = dso->short_name; |
830fadfd JO |
282 | return name && is_bpf_image(name); |
283 | } | |
284 | ||
789e2419 AH |
285 | bool __map__is_ool(const struct map *map) |
286 | { | |
63df0e4b IR |
287 | const struct dso *dso = map__dso(map); |
288 | ||
289 | return dso && dso->binary_type == DSO_BINARY_TYPE__OOL; | |
789e2419 AH |
290 | } |
291 | ||
e94b861a ACM |
292 | bool map__has_symbols(const struct map *map) |
293 | { | |
63df0e4b | 294 | return dso__has_symbols(map__dso(map)); |
e94b861a ACM |
295 | } |
296 | ||
d3a7c489 | 297 | static void map__exit(struct map *map) |
c338aee8 | 298 | { |
e1805aae | 299 | BUG_ON(refcount_read(map__refcnt(map)) != 0); |
2832ef81 | 300 | dso__zput(RC_CHK_ACCESS(map)->dso); |
d3a7c489 ACM |
301 | } |
302 | ||
303 | void map__delete(struct map *map) | |
304 | { | |
305 | map__exit(map); | |
2832ef81 | 306 | RC_CHK_FREE(map); |
c338aee8 ACM |
307 | } |
308 | ||
84c2cafa ACM |
309 | void map__put(struct map *map) |
310 | { | |
e1805aae | 311 | if (map && refcount_dec_and_test(map__refcnt(map))) |
84c2cafa | 312 | map__delete(map); |
2832ef81 IR |
313 | else |
314 | RC_CHK_PUT(map); | |
84c2cafa ACM |
315 | } |
316 | ||
237a7e04 | 317 | void map__fixup_start(struct map *map) |
c338aee8 | 318 | { |
63df0e4b IR |
319 | struct dso *dso = map__dso(map); |
320 | struct rb_root_cached *symbols = &dso->symbols; | |
7137ff50 | 321 | struct rb_node *nd = rb_first_cached(symbols); |
63df0e4b | 322 | |
c338aee8 ACM |
323 | if (nd != NULL) { |
324 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
63df0e4b | 325 | |
e6a9efce | 326 | map__set_start(map, sym->start); |
c338aee8 ACM |
327 | } |
328 | } | |
329 | ||
237a7e04 | 330 | void map__fixup_end(struct map *map) |
c338aee8 | 331 | { |
63df0e4b IR |
332 | struct dso *dso = map__dso(map); |
333 | struct rb_root_cached *symbols = &dso->symbols; | |
7137ff50 | 334 | struct rb_node *nd = rb_last(&symbols->rb_root); |
63df0e4b | 335 | |
c338aee8 ACM |
336 | if (nd != NULL) { |
337 | struct symbol *sym = rb_entry(nd, struct symbol, rb_node); | |
e6a9efce | 338 | map__set_end(map, sym->end); |
c338aee8 ACM |
339 | } |
340 | } | |
341 | ||
d70a5402 ACM |
342 | #define DSO__DELETED "(deleted)" |
343 | ||
be39db9f | 344 | int map__load(struct map *map) |
66bd8424 | 345 | { |
63df0e4b IR |
346 | struct dso *dso = map__dso(map); |
347 | const char *name = dso->long_name; | |
a128168d | 348 | int nr; |
79406cd7 | 349 | |
63df0e4b | 350 | if (dso__loaded(dso)) |
a128168d MH |
351 | return 0; |
352 | ||
63df0e4b | 353 | nr = dso__load(dso, map); |
79406cd7 | 354 | if (nr < 0) { |
63df0e4b | 355 | if (dso->has_build_id) { |
b5d8bbe8 | 356 | char sbuild_id[SBUILD_ID_SIZE]; |
79406cd7 | 357 | |
63df0e4b | 358 | build_id__sprintf(&dso->bid, sbuild_id); |
d8e75a11 | 359 | pr_debug("%s with build id %s not found", name, sbuild_id); |
79406cd7 | 360 | } else |
d8e75a11 | 361 | pr_debug("Failed to open %s", name); |
79406cd7 | 362 | |
d8e75a11 | 363 | pr_debug(", continuing without symbols\n"); |
79406cd7 ACM |
364 | return -1; |
365 | } else if (nr == 0) { | |
89fe808a | 366 | #ifdef HAVE_LIBELF_SUPPORT |
79406cd7 ACM |
367 | const size_t len = strlen(name); |
368 | const size_t real_len = len - sizeof(DSO__DELETED); | |
369 | ||
370 | if (len > sizeof(DSO__DELETED) && | |
371 | strcmp(name + real_len + 1, DSO__DELETED) == 0) { | |
d8e75a11 | 372 | pr_debug("%.*s was updated (is prelink enabled?). " |
e77b15bd | 373 | "Restart the long running apps that use it!\n", |
79406cd7 ACM |
374 | (int)real_len, name); |
375 | } else { | |
d8e75a11 | 376 | pr_debug("no symbols found in %s, maybe install a debug package?\n", name); |
66bd8424 | 377 | } |
393be2e3 | 378 | #endif |
79406cd7 | 379 | return -1; |
66bd8424 ACM |
380 | } |
381 | ||
79406cd7 ACM |
382 | return 0; |
383 | } | |
384 | ||
be39db9f | 385 | struct symbol *map__find_symbol(struct map *map, u64 addr) |
79406cd7 | 386 | { |
be39db9f | 387 | if (map__load(map) < 0) |
79406cd7 ACM |
388 | return NULL; |
389 | ||
63df0e4b | 390 | return dso__find_symbol(map__dso(map), addr); |
66bd8424 ACM |
391 | } |
392 | ||
259dce91 | 393 | struct symbol *map__find_symbol_by_name_idx(struct map *map, const char *name, size_t *idx) |
79406cd7 | 394 | { |
63df0e4b IR |
395 | struct dso *dso; |
396 | ||
be39db9f | 397 | if (map__load(map) < 0) |
79406cd7 ACM |
398 | return NULL; |
399 | ||
63df0e4b | 400 | dso = map__dso(map); |
ce5b2934 | 401 | dso__sort_by_name(dso); |
79406cd7 | 402 | |
259dce91 IR |
403 | return dso__find_symbol_by_name(dso, name, idx); |
404 | } | |
405 | ||
406 | struct symbol *map__find_symbol_by_name(struct map *map, const char *name) | |
407 | { | |
408 | size_t idx; | |
409 | ||
410 | return map__find_symbol_by_name_idx(map, name, &idx); | |
79406cd7 ACM |
411 | } |
412 | ||
66671d00 | 413 | struct map *map__clone(struct map *from) |
66e274f3 | 414 | { |
2832ef81 IR |
415 | struct map *result; |
416 | RC_STRUCT(map) *map; | |
417 | size_t size = sizeof(RC_STRUCT(map)); | |
63df0e4b | 418 | struct dso *dso = map__dso(from); |
66671d00 | 419 | |
63df0e4b | 420 | if (dso && dso->kernel) |
7ce66139 JO |
421 | size += sizeof(struct kmap); |
422 | ||
2832ef81 IR |
423 | map = memdup(RC_CHK_ACCESS(from), size); |
424 | if (ADD_RC_CHK(result, map)) { | |
e3a42cdd | 425 | refcount_set(&map->refcnt, 1); |
ec417ad4 | 426 | map->dso = dso__get(dso); |
66671d00 ACM |
427 | } |
428 | ||
2832ef81 | 429 | return result; |
66e274f3 FW |
430 | } |
431 | ||
237a7e04 | 432 | size_t map__fprintf(struct map *map, FILE *fp) |
66e274f3 | 433 | { |
63df0e4b IR |
434 | const struct dso *dso = map__dso(map); |
435 | ||
9486aa38 | 436 | return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %" PRIx64 " %s\n", |
2a6e5e8a | 437 | map__start(map), map__end(map), map__pgoff(map), dso->name); |
66e274f3 | 438 | } |
7a2b6209 | 439 | |
d3b52f71 AH |
440 | static bool prefer_dso_long_name(const struct dso *dso, bool print_off) |
441 | { | |
442 | return dso->long_name && | |
443 | (symbol_conf.show_kernel_path || | |
444 | (print_off && (dso->name[0] == '[' || dso__is_kcore(dso)))); | |
445 | } | |
446 | ||
447 | static size_t __map__fprintf_dsoname(struct map *map, bool print_off, FILE *fp) | |
547a92e0 | 448 | { |
1c492422 | 449 | char buf[symbol_conf.pad_output_len_dso + 1]; |
8f28f19a | 450 | const char *dsoname = "[unknown]"; |
63df0e4b | 451 | const struct dso *dso = map ? map__dso(map) : NULL; |
547a92e0 | 452 | |
63df0e4b | 453 | if (dso) { |
d3b52f71 | 454 | if (prefer_dso_long_name(dso, print_off)) |
63df0e4b | 455 | dsoname = dso->long_name; |
5eae7d84 | 456 | else |
63df0e4b | 457 | dsoname = dso->name; |
8f28f19a | 458 | } |
547a92e0 | 459 | |
1c492422 JO |
460 | if (symbol_conf.pad_output_len_dso) { |
461 | scnprintf_pad(buf, symbol_conf.pad_output_len_dso, "%s", dsoname); | |
462 | dsoname = buf; | |
463 | } | |
464 | ||
547a92e0 AN |
465 | return fprintf(fp, "%s", dsoname); |
466 | } | |
467 | ||
d3b52f71 AH |
468 | size_t map__fprintf_dsoname(struct map *map, FILE *fp) |
469 | { | |
470 | return __map__fprintf_dsoname(map, false, fp); | |
471 | } | |
472 | ||
2b433fad CD |
473 | size_t map__fprintf_dsoname_dsoff(struct map *map, bool print_off, u64 addr, FILE *fp) |
474 | { | |
d3b52f71 | 475 | const struct dso *dso = map ? map__dso(map) : NULL; |
2b433fad CD |
476 | int printed = 0; |
477 | ||
d3b52f71 AH |
478 | if (print_off && (!dso || !dso__is_object_file(dso))) |
479 | print_off = false; | |
2b433fad | 480 | printed += fprintf(fp, " ("); |
d3b52f71 AH |
481 | printed += __map__fprintf_dsoname(map, print_off, fp); |
482 | if (print_off) | |
2b433fad CD |
483 | printed += fprintf(fp, "+0x%" PRIx64, addr); |
484 | printed += fprintf(fp, ")"); | |
485 | ||
486 | return printed; | |
487 | } | |
488 | ||
e2d88aaa ACM |
489 | char *map__srcline(struct map *map, u64 addr, struct symbol *sym) |
490 | { | |
491 | if (map == NULL) | |
492 | return SRCLINE_UNKNOWN; | |
63df0e4b IR |
493 | |
494 | return get_srcline(map__dso(map), map__rip_2objdump(map, addr), sym, true, true, addr); | |
e2d88aaa ACM |
495 | } |
496 | ||
cc8fae1d AH |
497 | int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix, |
498 | FILE *fp) | |
499 | { | |
63df0e4b | 500 | const struct dso *dso = map ? map__dso(map) : NULL; |
cc8fae1d AH |
501 | int ret = 0; |
502 | ||
63df0e4b | 503 | if (dso) { |
e2d88aaa | 504 | char *srcline = map__srcline(map, addr, NULL); |
922db21d | 505 | if (srcline != SRCLINE_UNKNOWN) |
cc8fae1d | 506 | ret = fprintf(fp, "%s%s", prefix, srcline); |
625db36e | 507 | zfree_srcline(&srcline); |
cc8fae1d AH |
508 | } |
509 | return ret; | |
510 | } | |
511 | ||
dd2e18e9 AK |
512 | void srccode_state_free(struct srccode_state *state) |
513 | { | |
514 | zfree(&state->srcfile); | |
515 | state->line = 0; | |
516 | } | |
517 | ||
1d5077bd AH |
518 | /** |
519 | * map__rip_2objdump - convert symbol start address to objdump address. | |
520 | * @map: memory map | |
521 | * @rip: symbol start address | |
522 | * | |
7a2b6209 | 523 | * objdump wants/reports absolute IPs for ET_EXEC, and RIPs for ET_DYN. |
0131c4ec AH |
524 | * map->dso->adjust_symbols==1 for ET_EXEC-like cases except ET_REL which is |
525 | * relative to section start. | |
1d5077bd AH |
526 | * |
527 | * Return: Address suitable for passing to "objdump --start-address=" | |
7a2b6209 KS |
528 | */ |
529 | u64 map__rip_2objdump(struct map *map, u64 rip) | |
530 | { | |
97802f3b | 531 | struct kmap *kmap = __map__kmap(map); |
63df0e4b | 532 | const struct dso *dso = map__dso(map); |
97802f3b AH |
533 | |
534 | /* | |
535 | * vmlinux does not have program headers for PTI entry trampolines and | |
536 | * kcore may not either. However the trampoline object code is on the | |
537 | * main kernel map, so just use that instead. | |
538 | */ | |
5ab6d715 IR |
539 | if (kmap && is_entry_trampoline(kmap->name) && kmap->kmaps) { |
540 | struct machine *machine = maps__machine(kmap->kmaps); | |
97802f3b | 541 | |
5ab6d715 IR |
542 | if (machine) { |
543 | struct map *kernel_map = machine__kernel_map(machine); | |
544 | ||
545 | if (kernel_map) | |
546 | map = kernel_map; | |
547 | } | |
97802f3b AH |
548 | } |
549 | ||
63df0e4b | 550 | if (!dso->adjust_symbols) |
0131c4ec AH |
551 | return rip; |
552 | ||
63df0e4b | 553 | if (dso->rel) |
2a6e5e8a | 554 | return rip - map__pgoff(map); |
0131c4ec | 555 | |
63df0e4b IR |
556 | if (dso->kernel == DSO_SPACE__USER) |
557 | return rip + dso->text_offset; | |
a58f7033 | 558 | |
2a6e5e8a | 559 | return map__unmap_ip(map, rip) - map__reloc(map); |
7a2b6209 | 560 | } |
ee11b90b | 561 | |
1d5077bd AH |
562 | /** |
563 | * map__objdump_2mem - convert objdump address to a memory address. | |
564 | * @map: memory map | |
565 | * @ip: objdump address | |
566 | * | |
567 | * Closely related to map__rip_2objdump(), this function takes an address from | |
568 | * objdump and converts it to a memory address. Note this assumes that @map | |
569 | * contains the address. To be sure the result is valid, check it forwards | |
78a1f7cd | 570 | * e.g. map__rip_2objdump(map__map_ip(map, map__objdump_2mem(map, ip))) == ip |
1d5077bd AH |
571 | * |
572 | * Return: Memory address. | |
573 | */ | |
574 | u64 map__objdump_2mem(struct map *map, u64 ip) | |
575 | { | |
63df0e4b IR |
576 | const struct dso *dso = map__dso(map); |
577 | ||
578 | if (!dso->adjust_symbols) | |
78a1f7cd | 579 | return map__unmap_ip(map, ip); |
1d5077bd | 580 | |
63df0e4b | 581 | if (dso->rel) |
2a6e5e8a | 582 | return map__unmap_ip(map, ip + map__pgoff(map)); |
1d5077bd | 583 | |
63df0e4b | 584 | if (dso->kernel == DSO_SPACE__USER) |
78a1f7cd | 585 | return map__unmap_ip(map, ip - dso->text_offset); |
a58f7033 | 586 | |
2a6e5e8a | 587 | return ip + map__reloc(map); |
1d5077bd AH |
588 | } |
589 | ||
59835f55 | 590 | bool map__contains_symbol(const struct map *map, const struct symbol *sym) |
03db8b58 | 591 | { |
78a1f7cd | 592 | u64 ip = map__unmap_ip(map, sym->start); |
03db8b58 | 593 | |
e5116f46 | 594 | return ip >= map__start(map) && ip < map__end(map); |
03db8b58 AH |
595 | } |
596 | ||
5759a682 | 597 | struct kmap *__map__kmap(struct map *map) |
ba92732e | 598 | { |
63df0e4b IR |
599 | const struct dso *dso = map__dso(map); |
600 | ||
601 | if (!dso || !dso->kernel) | |
ba92732e | 602 | return NULL; |
2832ef81 | 603 | return (struct kmap *)(&RC_CHK_ACCESS(map)[1]); |
ba92732e WN |
604 | } |
605 | ||
5759a682 AH |
606 | struct kmap *map__kmap(struct map *map) |
607 | { | |
608 | struct kmap *kmap = __map__kmap(map); | |
609 | ||
610 | if (!kmap) | |
611 | pr_err("Internal error: map__kmap with a non-kernel map\n"); | |
612 | return kmap; | |
613 | } | |
614 | ||
79b6bb73 | 615 | struct maps *map__kmaps(struct map *map) |
ba92732e WN |
616 | { |
617 | struct kmap *kmap = map__kmap(map); | |
618 | ||
619 | if (!kmap || !kmap->kmaps) { | |
620 | pr_err("Internal error: map__kmaps with a non-kernel map\n"); | |
621 | return NULL; | |
622 | } | |
623 | return kmap->kmaps; | |
624 | } |