6 * Copyright (C) 2009, 2010 Red Hat Inc.
7 * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
14 #include <linux/kernel.h>
22 static bool no_buildid_cache
;
24 int build_id__mark_dso_hit(struct perf_tool
*tool __maybe_unused
,
25 union perf_event
*event
,
26 struct perf_sample
*sample
,
27 struct perf_evsel
*evsel __maybe_unused
,
28 struct machine
*machine
)
30 struct addr_location al
;
31 u8 cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
32 struct thread
*thread
= machine__findnew_thread(machine
, sample
->pid
,
36 pr_err("problem processing %d event, skipping it.\n",
41 thread__find_addr_map(thread
, cpumode
, MAP__FUNCTION
, sample
->ip
, &al
);
49 static int perf_event__exit_del_thread(struct perf_tool
*tool __maybe_unused
,
50 union perf_event
*event
,
51 struct perf_sample
*sample
53 struct machine
*machine
)
55 struct thread
*thread
= machine__findnew_thread(machine
,
59 dump_printf("(%d:%d):(%d:%d)\n", event
->fork
.pid
, event
->fork
.tid
,
60 event
->fork
.ppid
, event
->fork
.ptid
);
63 rb_erase(&thread
->rb_node
, &machine
->threads
);
64 machine
->last_match
= NULL
;
65 thread__delete(thread
);
71 struct perf_tool build_id__mark_dso_hit_ops
= {
72 .sample
= build_id__mark_dso_hit
,
73 .mmap
= perf_event__process_mmap
,
74 .mmap2
= perf_event__process_mmap2
,
75 .fork
= perf_event__process_fork
,
76 .exit
= perf_event__exit_del_thread
,
77 .attr
= perf_event__process_attr
,
78 .build_id
= perf_event__process_build_id
,
81 int build_id__sprintf(const u8
*build_id
, int len
, char *bf
)
84 const u8
*raw
= build_id
;
87 for (i
= 0; i
< len
; ++i
) {
88 sprintf(bid
, "%02x", *raw
);
93 return raw
- build_id
;
96 char *dso__build_id_filename(const struct dso
*dso
, char *bf
, size_t size
)
98 char build_id_hex
[BUILD_ID_SIZE
* 2 + 1];
100 if (!dso
->has_build_id
)
103 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), build_id_hex
);
105 if (asprintf(&bf
, "%s/.build-id/%.2s/%s", buildid_dir
,
106 build_id_hex
, build_id_hex
+ 2) < 0)
109 snprintf(bf
, size
, "%s/.build-id/%.2s/%s", buildid_dir
,
110 build_id_hex
, build_id_hex
+ 2);
114 #define dsos__for_each_with_build_id(pos, head) \
115 list_for_each_entry(pos, head, node) \
116 if (!pos->has_build_id) \
120 static int write_buildid(const char *name
, size_t name_len
, u8
*build_id
,
121 pid_t pid
, u16 misc
, int fd
)
124 struct build_id_event b
;
128 len
= PERF_ALIGN(len
, NAME_ALIGN
);
130 memset(&b
, 0, sizeof(b
));
131 memcpy(&b
.build_id
, build_id
, BUILD_ID_SIZE
);
133 b
.header
.misc
= misc
;
134 b
.header
.size
= sizeof(b
) + len
;
136 err
= writen(fd
, &b
, sizeof(b
));
140 return write_padded(fd
, name
, name_len
+ 1, len
);
143 static int __dsos__write_buildid_table(struct list_head
*head
,
144 struct machine
*machine
,
145 pid_t pid
, u16 misc
, int fd
)
150 dsos__for_each_with_build_id(pos
, head
) {
158 if (dso__is_vdso(pos
)) {
159 name
= pos
->short_name
;
160 name_len
= pos
->short_name_len
+ 1;
161 } else if (dso__is_kcore(pos
)) {
162 machine__mmap_name(machine
, nm
, sizeof(nm
));
164 name_len
= strlen(nm
) + 1;
166 name
= pos
->long_name
;
167 name_len
= pos
->long_name_len
+ 1;
170 err
= write_buildid(name
, name_len
, pos
->build_id
,
179 static int machine__write_buildid_table(struct machine
*machine
, int fd
)
182 u16 kmisc
= PERF_RECORD_MISC_KERNEL
,
183 umisc
= PERF_RECORD_MISC_USER
;
185 if (!machine__is_host(machine
)) {
186 kmisc
= PERF_RECORD_MISC_GUEST_KERNEL
;
187 umisc
= PERF_RECORD_MISC_GUEST_USER
;
190 err
= __dsos__write_buildid_table(&machine
->kernel_dsos
.head
, machine
,
191 machine
->pid
, kmisc
, fd
);
193 err
= __dsos__write_buildid_table(&machine
->user_dsos
.head
,
194 machine
, machine
->pid
, umisc
,
199 int perf_session__write_buildid_table(struct perf_session
*session
, int fd
)
202 int err
= machine__write_buildid_table(&session
->machines
.host
, fd
);
207 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
208 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
209 err
= machine__write_buildid_table(pos
, fd
);
216 static int __dsos__hit_all(struct list_head
*head
)
220 list_for_each_entry(pos
, head
, node
)
226 static int machine__hit_all_dsos(struct machine
*machine
)
230 err
= __dsos__hit_all(&machine
->kernel_dsos
.head
);
234 return __dsos__hit_all(&machine
->user_dsos
.head
);
237 int dsos__hit_all(struct perf_session
*session
)
242 err
= machine__hit_all_dsos(&session
->machines
.host
);
246 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
247 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
249 err
= machine__hit_all_dsos(pos
);
257 void disable_buildid_cache(void)
259 no_buildid_cache
= true;
262 int build_id_cache__add_s(const char *sbuild_id
, const char *debugdir
,
263 const char *name
, bool is_kallsyms
, bool is_vdso
)
265 const size_t size
= PATH_MAX
;
266 char *realname
, *filename
= zalloc(size
),
267 *linkname
= zalloc(size
), *targetname
;
269 bool slash
= is_kallsyms
|| is_vdso
;
272 if (symbol_conf
.kptr_restrict
) {
273 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
277 realname
= (char *) name
;
279 realname
= realpath(name
, NULL
);
281 if (realname
== NULL
|| filename
== NULL
|| linkname
== NULL
)
284 len
= scnprintf(filename
, size
, "%s%s%s",
285 debugdir
, slash
? "/" : "",
286 is_vdso
? DSO__NAME_VDSO
: realname
);
287 if (mkdir_p(filename
, 0755))
290 snprintf(filename
+ len
, size
- len
, "/%s", sbuild_id
);
292 if (access(filename
, F_OK
)) {
294 if (copyfile("/proc/kallsyms", filename
))
296 } else if (link(realname
, filename
) && copyfile(name
, filename
))
300 len
= scnprintf(linkname
, size
, "%s/.build-id/%.2s",
301 debugdir
, sbuild_id
);
303 if (access(linkname
, X_OK
) && mkdir_p(linkname
, 0755))
306 snprintf(linkname
+ len
, size
- len
, "/%s", sbuild_id
+ 2);
307 targetname
= filename
+ strlen(debugdir
) - 5;
308 memcpy(targetname
, "../..", 5);
310 if (symlink(targetname
, linkname
) == 0)
320 static int build_id_cache__add_b(const u8
*build_id
, size_t build_id_size
,
321 const char *name
, const char *debugdir
,
322 bool is_kallsyms
, bool is_vdso
)
324 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
326 build_id__sprintf(build_id
, build_id_size
, sbuild_id
);
328 return build_id_cache__add_s(sbuild_id
, debugdir
, name
,
329 is_kallsyms
, is_vdso
);
332 int build_id_cache__remove_s(const char *sbuild_id
, const char *debugdir
)
334 const size_t size
= PATH_MAX
;
335 char *filename
= zalloc(size
),
336 *linkname
= zalloc(size
);
339 if (filename
== NULL
|| linkname
== NULL
)
342 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
343 debugdir
, sbuild_id
, sbuild_id
+ 2);
345 if (access(linkname
, F_OK
))
348 if (readlink(linkname
, filename
, size
- 1) < 0)
351 if (unlink(linkname
))
355 * Since the link is relative, we must make it absolute:
357 snprintf(linkname
, size
, "%s/.build-id/%.2s/%s",
358 debugdir
, sbuild_id
, filename
);
360 if (unlink(linkname
))
370 static int dso__cache_build_id(struct dso
*dso
, struct machine
*machine
,
371 const char *debugdir
)
373 bool is_kallsyms
= dso
->kernel
&& dso
->long_name
[0] != '/';
374 bool is_vdso
= dso__is_vdso(dso
);
375 const char *name
= dso
->long_name
;
378 if (dso__is_kcore(dso
)) {
380 machine__mmap_name(machine
, nm
, sizeof(nm
));
383 return build_id_cache__add_b(dso
->build_id
, sizeof(dso
->build_id
), name
,
384 debugdir
, is_kallsyms
, is_vdso
);
387 static int __dsos__cache_build_ids(struct list_head
*head
,
388 struct machine
*machine
, const char *debugdir
)
393 dsos__for_each_with_build_id(pos
, head
)
394 if (dso__cache_build_id(pos
, machine
, debugdir
))
400 static int machine__cache_build_ids(struct machine
*machine
, const char *debugdir
)
402 int ret
= __dsos__cache_build_ids(&machine
->kernel_dsos
.head
, machine
,
404 ret
|= __dsos__cache_build_ids(&machine
->user_dsos
.head
, machine
,
409 int perf_session__cache_build_ids(struct perf_session
*session
)
414 if (no_buildid_cache
)
417 if (mkdir(buildid_dir
, 0755) != 0 && errno
!= EEXIST
)
420 ret
= machine__cache_build_ids(&session
->machines
.host
, buildid_dir
);
422 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
423 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
424 ret
|= machine__cache_build_ids(pos
, buildid_dir
);
429 static bool machine__read_build_ids(struct machine
*machine
, bool with_hits
)
433 ret
= __dsos__read_build_ids(&machine
->kernel_dsos
.head
, with_hits
);
434 ret
|= __dsos__read_build_ids(&machine
->user_dsos
.head
, with_hits
);
438 bool perf_session__read_build_ids(struct perf_session
*session
, bool with_hits
)
441 bool ret
= machine__read_build_ids(&session
->machines
.host
, with_hits
);
443 for (nd
= rb_first(&session
->machines
.guests
); nd
; nd
= rb_next(nd
)) {
444 struct machine
*pos
= rb_entry(nd
, struct machine
, rb_node
);
445 ret
|= machine__read_build_ids(pos
, with_hits
);