diff options
Diffstat (limited to 'tools/perf/util/build-id.c')
| -rw-r--r-- | tools/perf/util/build-id.c | 348 |
1 files changed, 346 insertions, 2 deletions
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index a904a4cfe7d3..e8d79e5bfaf7 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c | |||
| @@ -15,6 +15,11 @@ | |||
| 15 | #include "debug.h" | 15 | #include "debug.h" |
| 16 | #include "session.h" | 16 | #include "session.h" |
| 17 | #include "tool.h" | 17 | #include "tool.h" |
| 18 | #include "header.h" | ||
| 19 | #include "vdso.h" | ||
| 20 | |||
| 21 | |||
| 22 | static bool no_buildid_cache; | ||
| 18 | 23 | ||
| 19 | int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused, | 24 | int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused, |
| 20 | union perf_event *event, | 25 | union perf_event *event, |
| @@ -33,8 +38,7 @@ int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused, | |||
| 33 | return -1; | 38 | return -1; |
| 34 | } | 39 | } |
| 35 | 40 | ||
| 36 | thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION, | 41 | thread__find_addr_map(thread, cpumode, MAP__FUNCTION, sample->ip, &al); |
| 37 | sample->ip, &al); | ||
| 38 | 42 | ||
| 39 | if (al.map != NULL) | 43 | if (al.map != NULL) |
| 40 | al.map->dso->hit = 1; | 44 | al.map->dso->hit = 1; |
| @@ -106,3 +110,343 @@ char *dso__build_id_filename(const struct dso *dso, char *bf, size_t size) | |||
| 106 | build_id_hex, build_id_hex + 2); | 110 | build_id_hex, build_id_hex + 2); |
| 107 | return bf; | 111 | return bf; |
| 108 | } | 112 | } |
| 113 | |||
| 114 | #define dsos__for_each_with_build_id(pos, head) \ | ||
| 115 | list_for_each_entry(pos, head, node) \ | ||
| 116 | if (!pos->has_build_id) \ | ||
| 117 | continue; \ | ||
| 118 | else | ||
| 119 | |||
| 120 | static int write_buildid(const char *name, size_t name_len, u8 *build_id, | ||
| 121 | pid_t pid, u16 misc, int fd) | ||
| 122 | { | ||
| 123 | int err; | ||
| 124 | struct build_id_event b; | ||
| 125 | size_t len; | ||
| 126 | |||
| 127 | len = name_len + 1; | ||
| 128 | len = PERF_ALIGN(len, NAME_ALIGN); | ||
| 129 | |||
| 130 | memset(&b, 0, sizeof(b)); | ||
| 131 | memcpy(&b.build_id, build_id, BUILD_ID_SIZE); | ||
| 132 | b.pid = pid; | ||
| 133 | b.header.misc = misc; | ||
| 134 | b.header.size = sizeof(b) + len; | ||
| 135 | |||
| 136 | err = writen(fd, &b, sizeof(b)); | ||
| 137 | if (err < 0) | ||
| 138 | return err; | ||
| 139 | |||
| 140 | return write_padded(fd, name, name_len + 1, len); | ||
| 141 | } | ||
| 142 | |||
| 143 | static int __dsos__write_buildid_table(struct list_head *head, | ||
| 144 | struct machine *machine, | ||
| 145 | pid_t pid, u16 misc, int fd) | ||
| 146 | { | ||
| 147 | char nm[PATH_MAX]; | ||
| 148 | struct dso *pos; | ||
| 149 | |||
| 150 | dsos__for_each_with_build_id(pos, head) { | ||
| 151 | int err; | ||
| 152 | const char *name; | ||
| 153 | size_t name_len; | ||
| 154 | |||
| 155 | if (!pos->hit) | ||
| 156 | continue; | ||
| 157 | |||
| 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)); | ||
| 163 | name = nm; | ||
| 164 | name_len = strlen(nm) + 1; | ||
| 165 | } else { | ||
| 166 | name = pos->long_name; | ||
| 167 | name_len = pos->long_name_len + 1; | ||
| 168 | } | ||
| 169 | |||
| 170 | err = write_buildid(name, name_len, pos->build_id, | ||
| 171 | pid, misc, fd); | ||
| 172 | if (err) | ||
| 173 | return err; | ||
| 174 | } | ||
| 175 | |||
| 176 | return 0; | ||
| 177 | } | ||
| 178 | |||
| 179 | static int machine__write_buildid_table(struct machine *machine, int fd) | ||
| 180 | { | ||
| 181 | int err; | ||
| 182 | u16 kmisc = PERF_RECORD_MISC_KERNEL, | ||
| 183 | umisc = PERF_RECORD_MISC_USER; | ||
| 184 | |||
| 185 | if (!machine__is_host(machine)) { | ||
| 186 | kmisc = PERF_RECORD_MISC_GUEST_KERNEL; | ||
| 187 | umisc = PERF_RECORD_MISC_GUEST_USER; | ||
| 188 | } | ||
| 189 | |||
| 190 | err = __dsos__write_buildid_table(&machine->kernel_dsos.head, machine, | ||
| 191 | machine->pid, kmisc, fd); | ||
| 192 | if (err == 0) | ||
| 193 | err = __dsos__write_buildid_table(&machine->user_dsos.head, | ||
| 194 | machine, machine->pid, umisc, | ||
| 195 | fd); | ||
| 196 | return err; | ||
| 197 | } | ||
| 198 | |||
| 199 | int perf_session__write_buildid_table(struct perf_session *session, int fd) | ||
| 200 | { | ||
| 201 | struct rb_node *nd; | ||
| 202 | int err = machine__write_buildid_table(&session->machines.host, fd); | ||
| 203 | |||
| 204 | if (err) | ||
| 205 | return err; | ||
| 206 | |||
| 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); | ||
| 210 | if (err) | ||
| 211 | break; | ||
| 212 | } | ||
| 213 | return err; | ||
| 214 | } | ||
| 215 | |||
| 216 | static int __dsos__hit_all(struct list_head *head) | ||
| 217 | { | ||
| 218 | struct dso *pos; | ||
| 219 | |||
| 220 | list_for_each_entry(pos, head, node) | ||
| 221 | pos->hit = true; | ||
| 222 | |||
| 223 | return 0; | ||
| 224 | } | ||
| 225 | |||
| 226 | static int machine__hit_all_dsos(struct machine *machine) | ||
| 227 | { | ||
| 228 | int err; | ||
| 229 | |||
| 230 | err = __dsos__hit_all(&machine->kernel_dsos.head); | ||
| 231 | if (err) | ||
| 232 | return err; | ||
| 233 | |||
| 234 | return __dsos__hit_all(&machine->user_dsos.head); | ||
| 235 | } | ||
| 236 | |||
| 237 | int dsos__hit_all(struct perf_session *session) | ||
| 238 | { | ||
| 239 | struct rb_node *nd; | ||
| 240 | int err; | ||
| 241 | |||
| 242 | err = machine__hit_all_dsos(&session->machines.host); | ||
| 243 | if (err) | ||
| 244 | return err; | ||
| 245 | |||
| 246 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { | ||
| 247 | struct machine *pos = rb_entry(nd, struct machine, rb_node); | ||
| 248 | |||
| 249 | err = machine__hit_all_dsos(pos); | ||
| 250 | if (err) | ||
| 251 | return err; | ||
| 252 | } | ||
| 253 | |||
| 254 | return 0; | ||
| 255 | } | ||
| 256 | |||
| 257 | void disable_buildid_cache(void) | ||
| 258 | { | ||
| 259 | no_buildid_cache = true; | ||
| 260 | } | ||
| 261 | |||
| 262 | int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, | ||
| 263 | const char *name, bool is_kallsyms, bool is_vdso) | ||
| 264 | { | ||
| 265 | const size_t size = PATH_MAX; | ||
| 266 | char *realname, *filename = zalloc(size), | ||
| 267 | *linkname = zalloc(size), *targetname; | ||
| 268 | int len, err = -1; | ||
| 269 | bool slash = is_kallsyms || is_vdso; | ||
| 270 | |||
| 271 | if (is_kallsyms) { | ||
| 272 | if (symbol_conf.kptr_restrict) { | ||
| 273 | pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); | ||
| 274 | err = 0; | ||
| 275 | goto out_free; | ||
| 276 | } | ||
| 277 | realname = (char *) name; | ||
| 278 | } else | ||
| 279 | realname = realpath(name, NULL); | ||
| 280 | |||
| 281 | if (realname == NULL || filename == NULL || linkname == NULL) | ||
| 282 | goto out_free; | ||
| 283 | |||
| 284 | len = scnprintf(filename, size, "%s%s%s", | ||
| 285 | debugdir, slash ? "/" : "", | ||
| 286 | is_vdso ? DSO__NAME_VDSO : realname); | ||
| 287 | if (mkdir_p(filename, 0755)) | ||
| 288 | goto out_free; | ||
| 289 | |||
| 290 | snprintf(filename + len, size - len, "/%s", sbuild_id); | ||
| 291 | |||
| 292 | if (access(filename, F_OK)) { | ||
| 293 | if (is_kallsyms) { | ||
| 294 | if (copyfile("/proc/kallsyms", filename)) | ||
| 295 | goto out_free; | ||
| 296 | } else if (link(realname, filename) && copyfile(name, filename)) | ||
| 297 | goto out_free; | ||
| 298 | } | ||
| 299 | |||
| 300 | len = scnprintf(linkname, size, "%s/.build-id/%.2s", | ||
| 301 | debugdir, sbuild_id); | ||
| 302 | |||
| 303 | if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) | ||
| 304 | goto out_free; | ||
| 305 | |||
| 306 | snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); | ||
| 307 | targetname = filename + strlen(debugdir) - 5; | ||
| 308 | memcpy(targetname, "../..", 5); | ||
| 309 | |||
| 310 | if (symlink(targetname, linkname) == 0) | ||
| 311 | err = 0; | ||
| 312 | out_free: | ||
| 313 | if (!is_kallsyms) | ||
| 314 | free(realname); | ||
| 315 | free(filename); | ||
| 316 | free(linkname); | ||
| 317 | return err; | ||
| 318 | } | ||
| 319 | |||
| 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) | ||
| 323 | { | ||
| 324 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; | ||
| 325 | |||
| 326 | build_id__sprintf(build_id, build_id_size, sbuild_id); | ||
| 327 | |||
| 328 | return build_id_cache__add_s(sbuild_id, debugdir, name, | ||
| 329 | is_kallsyms, is_vdso); | ||
| 330 | } | ||
| 331 | |||
| 332 | int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) | ||
| 333 | { | ||
| 334 | const size_t size = PATH_MAX; | ||
| 335 | char *filename = zalloc(size), | ||
| 336 | *linkname = zalloc(size); | ||
| 337 | int err = -1; | ||
| 338 | |||
| 339 | if (filename == NULL || linkname == NULL) | ||
| 340 | goto out_free; | ||
| 341 | |||
| 342 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", | ||
| 343 | debugdir, sbuild_id, sbuild_id + 2); | ||
| 344 | |||
| 345 | if (access(linkname, F_OK)) | ||
| 346 | goto out_free; | ||
| 347 | |||
| 348 | if (readlink(linkname, filename, size - 1) < 0) | ||
| 349 | goto out_free; | ||
| 350 | |||
| 351 | if (unlink(linkname)) | ||
| 352 | goto out_free; | ||
| 353 | |||
| 354 | /* | ||
| 355 | * Since the link is relative, we must make it absolute: | ||
| 356 | */ | ||
| 357 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", | ||
| 358 | debugdir, sbuild_id, filename); | ||
| 359 | |||
| 360 | if (unlink(linkname)) | ||
| 361 | goto out_free; | ||
| 362 | |||
| 363 | err = 0; | ||
| 364 | out_free: | ||
| 365 | free(filename); | ||
| 366 | free(linkname); | ||
| 367 | return err; | ||
| 368 | } | ||
| 369 | |||
| 370 | static int dso__cache_build_id(struct dso *dso, struct machine *machine, | ||
| 371 | const char *debugdir) | ||
| 372 | { | ||
| 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; | ||
| 376 | char nm[PATH_MAX]; | ||
| 377 | |||
| 378 | if (dso__is_kcore(dso)) { | ||
| 379 | is_kallsyms = true; | ||
| 380 | machine__mmap_name(machine, nm, sizeof(nm)); | ||
| 381 | name = nm; | ||
| 382 | } | ||
| 383 | return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name, | ||
| 384 | debugdir, is_kallsyms, is_vdso); | ||
| 385 | } | ||
| 386 | |||
| 387 | static int __dsos__cache_build_ids(struct list_head *head, | ||
| 388 | struct machine *machine, const char *debugdir) | ||
| 389 | { | ||
| 390 | struct dso *pos; | ||
| 391 | int err = 0; | ||
| 392 | |||
| 393 | dsos__for_each_with_build_id(pos, head) | ||
| 394 | if (dso__cache_build_id(pos, machine, debugdir)) | ||
| 395 | err = -1; | ||
| 396 | |||
| 397 | return err; | ||
| 398 | } | ||
| 399 | |||
| 400 | static int machine__cache_build_ids(struct machine *machine, const char *debugdir) | ||
| 401 | { | ||
| 402 | int ret = __dsos__cache_build_ids(&machine->kernel_dsos.head, machine, | ||
| 403 | debugdir); | ||
| 404 | ret |= __dsos__cache_build_ids(&machine->user_dsos.head, machine, | ||
| 405 | debugdir); | ||
| 406 | return ret; | ||
| 407 | } | ||
| 408 | |||
| 409 | int perf_session__cache_build_ids(struct perf_session *session) | ||
| 410 | { | ||
| 411 | struct rb_node *nd; | ||
| 412 | int ret; | ||
| 413 | char debugdir[PATH_MAX]; | ||
| 414 | |||
| 415 | if (no_buildid_cache) | ||
| 416 | return 0; | ||
| 417 | |||
| 418 | snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); | ||
| 419 | |||
| 420 | if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) | ||
| 421 | return -1; | ||
| 422 | |||
| 423 | ret = machine__cache_build_ids(&session->machines.host, debugdir); | ||
| 424 | |||
| 425 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { | ||
| 426 | struct machine *pos = rb_entry(nd, struct machine, rb_node); | ||
| 427 | ret |= machine__cache_build_ids(pos, debugdir); | ||
| 428 | } | ||
| 429 | return ret ? -1 : 0; | ||
| 430 | } | ||
| 431 | |||
| 432 | static bool machine__read_build_ids(struct machine *machine, bool with_hits) | ||
| 433 | { | ||
| 434 | bool ret; | ||
| 435 | |||
| 436 | ret = __dsos__read_build_ids(&machine->kernel_dsos.head, with_hits); | ||
| 437 | ret |= __dsos__read_build_ids(&machine->user_dsos.head, with_hits); | ||
| 438 | return ret; | ||
| 439 | } | ||
| 440 | |||
| 441 | bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) | ||
| 442 | { | ||
| 443 | struct rb_node *nd; | ||
| 444 | bool ret = machine__read_build_ids(&session->machines.host, with_hits); | ||
| 445 | |||
| 446 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { | ||
| 447 | struct machine *pos = rb_entry(nd, struct machine, rb_node); | ||
| 448 | ret |= machine__read_build_ids(pos, with_hits); | ||
| 449 | } | ||
| 450 | |||
| 451 | return ret; | ||
| 452 | } | ||
