Blame view
tools/perf/util/build-id.c
10.3 KB
7b2567c1f perf build-id: Mo... |
1 2 3 4 5 6 7 8 |
/* * build-id.c * * build-id support * * Copyright (C) 2009, 2010 Red Hat Inc. * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com> */ |
b36f19d57 perf annotate: Us... |
9 10 |
#include "util.h" #include <stdio.h> |
7b2567c1f perf build-id: Mo... |
11 12 13 14 |
#include "build-id.h" #include "event.h" #include "symbol.h" #include <linux/kernel.h> |
591765fda perf tools: Relea... |
15 |
#include "debug.h" |
d20deb64e perf tools: Pass ... |
16 |
#include "session.h" |
45694aa77 perf tools: Renam... |
17 |
#include "tool.h" |
e195fac80 perf build-id: Mo... |
18 19 |
#include "header.h" #include "vdso.h" |
7b2567c1f perf build-id: Mo... |
20 |
|
73c5d224b perf build-id: Mo... |
21 22 |
static bool no_buildid_cache; |
54a3cf59b perf inject: Mark... |
23 24 |
int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused, union perf_event *event, |
ef89325f7 perf tools: Remov... |
25 |
struct perf_sample *sample, |
54a3cf59b perf inject: Mark... |
26 27 |
struct perf_evsel *evsel __maybe_unused, struct machine *machine) |
7b2567c1f perf build-id: Mo... |
28 29 30 |
{ struct addr_location al; u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
ef89325f7 perf tools: Remov... |
31 |
struct thread *thread = machine__findnew_thread(machine, sample->pid, |
13ce34df1 perf tools: Use t... |
32 |
sample->tid); |
7b2567c1f perf build-id: Mo... |
33 34 35 36 37 38 39 |
if (thread == NULL) { pr_err("problem processing %d event, skipping it. ", event->header.type); return -1; } |
bb871a9c8 perf tools: A thr... |
40 |
thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al); |
7b2567c1f perf build-id: Mo... |
41 42 43 44 45 46 |
if (al.map != NULL) al.map->dso->hit = 1; return 0; } |
1d037ca16 perf tools: Use _... |
47 |
static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused, |
d20deb64e perf tools: Pass ... |
48 |
union perf_event *event, |
1d037ca16 perf tools: Use _... |
49 50 |
struct perf_sample *sample __maybe_unused, |
743eb8686 perf tools: Resol... |
51 |
struct machine *machine) |
591765fda perf tools: Relea... |
52 |
{ |
314add6b1 perf tools: chang... |
53 54 55 |
struct thread *thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid); |
591765fda perf tools: Relea... |
56 |
|
8115d60c3 perf tools: Kill ... |
57 58 59 |
dump_printf("(%d:%d):(%d:%d) ", event->fork.pid, event->fork.tid, event->fork.ppid, event->fork.ptid); |
591765fda perf tools: Relea... |
60 61 |
if (thread) { |
743eb8686 perf tools: Resol... |
62 63 |
rb_erase(&thread->rb_node, &machine->threads); machine->last_match = NULL; |
591765fda perf tools: Relea... |
64 65 66 67 68 |
thread__delete(thread); } return 0; } |
45694aa77 perf tools: Renam... |
69 |
struct perf_tool build_id__mark_dso_hit_ops = { |
7b2567c1f perf build-id: Mo... |
70 |
.sample = build_id__mark_dso_hit, |
8115d60c3 perf tools: Kill ... |
71 |
.mmap = perf_event__process_mmap, |
5c5e854bc perf tools: Add a... |
72 |
.mmap2 = perf_event__process_mmap2, |
f62d3f0f4 perf event: No ne... |
73 |
.fork = perf_event__process_fork, |
8115d60c3 perf tools: Kill ... |
74 |
.exit = perf_event__exit_del_thread, |
299c34520 perf buildid-list... |
75 76 |
.attr = perf_event__process_attr, .build_id = perf_event__process_build_id, |
7b2567c1f perf build-id: Mo... |
77 |
}; |
b36f19d57 perf annotate: Us... |
78 |
|
ebb296c27 perf tools: Move ... |
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
int build_id__sprintf(const u8 *build_id, int len, char *bf) { char *bid = bf; const u8 *raw = build_id; int i; for (i = 0; i < len; ++i) { sprintf(bid, "%02x", *raw); ++raw; bid += 2; } return raw - build_id; } |
3344996e4 perf symbols: Con... |
93 |
char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size) |
b36f19d57 perf annotate: Us... |
94 95 |
{ char build_id_hex[BUILD_ID_SIZE * 2 + 1]; |
b36f19d57 perf annotate: Us... |
96 |
|
c824c4338 perf tools: Stop ... |
97 |
if (!dso->has_build_id) |
b36f19d57 perf annotate: Us... |
98 |
return NULL; |
c824c4338 perf tools: Stop ... |
99 |
build_id__sprintf(dso->build_id, sizeof(dso->build_id), build_id_hex); |
b36f19d57 perf annotate: Us... |
100 |
if (bf == NULL) { |
45de34bbe perf buildid: add... |
101 102 |
if (asprintf(&bf, "%s/.build-id/%.2s/%s", buildid_dir, build_id_hex, build_id_hex + 2) < 0) |
b36f19d57 perf annotate: Us... |
103 104 |
return NULL; } else |
45de34bbe perf buildid: add... |
105 106 |
snprintf(bf, size, "%s/.build-id/%.2s/%s", buildid_dir, build_id_hex, build_id_hex + 2); |
b36f19d57 perf annotate: Us... |
107 108 |
return bf; } |
e195fac80 perf build-id: Mo... |
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
#define dsos__for_each_with_build_id(pos, head) \ list_for_each_entry(pos, head, node) \ if (!pos->has_build_id) \ continue; \ else static int write_buildid(const char *name, size_t name_len, u8 *build_id, pid_t pid, u16 misc, int fd) { int err; struct build_id_event b; size_t len; len = name_len + 1; len = PERF_ALIGN(len, NAME_ALIGN); memset(&b, 0, sizeof(b)); memcpy(&b.build_id, build_id, BUILD_ID_SIZE); b.pid = pid; b.header.misc = misc; b.header.size = sizeof(b) + len; err = writen(fd, &b, sizeof(b)); if (err < 0) return err; return write_padded(fd, name, name_len + 1, len); } static int __dsos__write_buildid_table(struct list_head *head, struct machine *machine, pid_t pid, u16 misc, int fd) { char nm[PATH_MAX]; struct dso *pos; dsos__for_each_with_build_id(pos, head) { int err; const char *name; size_t name_len; if (!pos->hit) continue; if (dso__is_vdso(pos)) { name = pos->short_name; name_len = pos->short_name_len + 1; } else if (dso__is_kcore(pos)) { machine__mmap_name(machine, nm, sizeof(nm)); name = nm; name_len = strlen(nm) + 1; } else { name = pos->long_name; name_len = pos->long_name_len + 1; } err = write_buildid(name, name_len, pos->build_id, pid, misc, fd); if (err) return err; } return 0; } static int machine__write_buildid_table(struct machine *machine, int fd) { int err; u16 kmisc = PERF_RECORD_MISC_KERNEL, umisc = PERF_RECORD_MISC_USER; if (!machine__is_host(machine)) { kmisc = PERF_RECORD_MISC_GUEST_KERNEL; umisc = PERF_RECORD_MISC_GUEST_USER; } err = __dsos__write_buildid_table(&machine->kernel_dsos.head, machine, machine->pid, kmisc, fd); if (err == 0) err = __dsos__write_buildid_table(&machine->user_dsos.head, machine, machine->pid, umisc, fd); return err; } int perf_session__write_buildid_table(struct perf_session *session, int fd) { struct rb_node *nd; int err = machine__write_buildid_table(&session->machines.host, fd); if (err) return err; for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { struct machine *pos = rb_entry(nd, struct machine, rb_node); err = machine__write_buildid_table(pos, fd); if (err) break; } return err; } static int __dsos__hit_all(struct list_head *head) { struct dso *pos; list_for_each_entry(pos, head, node) pos->hit = true; return 0; } static int machine__hit_all_dsos(struct machine *machine) { int err; err = __dsos__hit_all(&machine->kernel_dsos.head); if (err) return err; return __dsos__hit_all(&machine->user_dsos.head); } int dsos__hit_all(struct perf_session *session) { struct rb_node *nd; int err; err = machine__hit_all_dsos(&session->machines.host); if (err) return err; for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { struct machine *pos = rb_entry(nd, struct machine, rb_node); err = machine__hit_all_dsos(pos); if (err) return err; } return 0; } |
73c5d224b perf build-id: Mo... |
252 253 254 255 |
void disable_buildid_cache(void) { no_buildid_cache = true; } |
e195fac80 perf build-id: Mo... |
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, const char *name, bool is_kallsyms, bool is_vdso) { const size_t size = PATH_MAX; char *realname, *filename = zalloc(size), *linkname = zalloc(size), *targetname; int len, err = -1; bool slash = is_kallsyms || is_vdso; if (is_kallsyms) { if (symbol_conf.kptr_restrict) { pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms "); err = 0; goto out_free; } realname = (char *) name; } else realname = realpath(name, NULL); if (realname == NULL || filename == NULL || linkname == NULL) goto out_free; len = scnprintf(filename, size, "%s%s%s", debugdir, slash ? "/" : "", is_vdso ? DSO__NAME_VDSO : realname); if (mkdir_p(filename, 0755)) goto out_free; snprintf(filename + len, size - len, "/%s", sbuild_id); if (access(filename, F_OK)) { if (is_kallsyms) { if (copyfile("/proc/kallsyms", filename)) goto out_free; } else if (link(realname, filename) && copyfile(name, filename)) goto out_free; } len = scnprintf(linkname, size, "%s/.build-id/%.2s", debugdir, sbuild_id); if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) goto out_free; snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); targetname = filename + strlen(debugdir) - 5; memcpy(targetname, "../..", 5); if (symlink(targetname, linkname) == 0) err = 0; out_free: if (!is_kallsyms) free(realname); free(filename); free(linkname); return err; } static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, const char *name, const char *debugdir, bool is_kallsyms, bool is_vdso) { char sbuild_id[BUILD_ID_SIZE * 2 + 1]; build_id__sprintf(build_id, build_id_size, sbuild_id); return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms, is_vdso); } int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) { const size_t size = PATH_MAX; char *filename = zalloc(size), *linkname = zalloc(size); int err = -1; if (filename == NULL || linkname == NULL) goto out_free; snprintf(linkname, size, "%s/.build-id/%.2s/%s", debugdir, sbuild_id, sbuild_id + 2); if (access(linkname, F_OK)) goto out_free; if (readlink(linkname, filename, size - 1) < 0) goto out_free; if (unlink(linkname)) goto out_free; /* * Since the link is relative, we must make it absolute: */ snprintf(linkname, size, "%s/.build-id/%.2s/%s", debugdir, sbuild_id, filename); if (unlink(linkname)) goto out_free; err = 0; out_free: free(filename); free(linkname); return err; } static int dso__cache_build_id(struct dso *dso, struct machine *machine, const char *debugdir) { bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; bool is_vdso = dso__is_vdso(dso); const char *name = dso->long_name; char nm[PATH_MAX]; if (dso__is_kcore(dso)) { is_kallsyms = true; machine__mmap_name(machine, nm, sizeof(nm)); name = nm; } return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name, debugdir, is_kallsyms, is_vdso); } static int __dsos__cache_build_ids(struct list_head *head, struct machine *machine, const char *debugdir) { struct dso *pos; int err = 0; dsos__for_each_with_build_id(pos, head) if (dso__cache_build_id(pos, machine, debugdir)) err = -1; return err; } static int machine__cache_build_ids(struct machine *machine, const char *debugdir) { int ret = __dsos__cache_build_ids(&machine->kernel_dsos.head, machine, debugdir); ret |= __dsos__cache_build_ids(&machine->user_dsos.head, machine, debugdir); return ret; } int perf_session__cache_build_ids(struct perf_session *session) { struct rb_node *nd; int ret; |
e195fac80 perf build-id: Mo... |
408 |
|
73c5d224b perf build-id: Mo... |
409 410 |
if (no_buildid_cache) return 0; |
498922adf perf buildid-cach... |
411 |
if (mkdir(buildid_dir, 0755) != 0 && errno != EEXIST) |
e195fac80 perf build-id: Mo... |
412 |
return -1; |
498922adf perf buildid-cach... |
413 |
ret = machine__cache_build_ids(&session->machines.host, buildid_dir); |
e195fac80 perf build-id: Mo... |
414 415 416 |
for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { struct machine *pos = rb_entry(nd, struct machine, rb_node); |
498922adf perf buildid-cach... |
417 |
ret |= machine__cache_build_ids(pos, buildid_dir); |
e195fac80 perf build-id: Mo... |
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
} return ret ? -1 : 0; } static bool machine__read_build_ids(struct machine *machine, bool with_hits) { bool ret; ret = __dsos__read_build_ids(&machine->kernel_dsos.head, with_hits); ret |= __dsos__read_build_ids(&machine->user_dsos.head, with_hits); return ret; } bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) { struct rb_node *nd; bool ret = machine__read_build_ids(&session->machines.host, with_hits); for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { struct machine *pos = rb_entry(nd, struct machine, rb_node); ret |= machine__read_build_ids(pos, with_hits); } return ret; } |