build-id.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * build-id.c
  3. *
  4. * build-id support
  5. *
  6. * Copyright (C) 2009, 2010 Red Hat Inc.
  7. * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
  8. */
  9. #include "util.h"
  10. #include <stdio.h>
  11. #include "build-id.h"
  12. #include "event.h"
  13. #include "symbol.h"
  14. #include <linux/kernel.h>
  15. #include "debug.h"
  16. #include "session.h"
  17. #include "tool.h"
  18. int build_id__mark_dso_hit(struct perf_tool *tool __maybe_unused,
  19. union perf_event *event,
  20. struct perf_sample *sample,
  21. struct perf_evsel *evsel __maybe_unused,
  22. struct machine *machine)
  23. {
  24. struct addr_location al;
  25. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  26. struct thread *thread = machine__findnew_thread(machine, sample->pid,
  27. sample->pid);
  28. if (thread == NULL) {
  29. pr_err("problem processing %d event, skipping it.\n",
  30. event->header.type);
  31. return -1;
  32. }
  33. thread__find_addr_map(thread, machine, cpumode, MAP__FUNCTION,
  34. sample->ip, &al);
  35. if (al.map != NULL)
  36. al.map->dso->hit = 1;
  37. return 0;
  38. }
  39. static int perf_event__exit_del_thread(struct perf_tool *tool __maybe_unused,
  40. union perf_event *event,
  41. struct perf_sample *sample
  42. __maybe_unused,
  43. struct machine *machine)
  44. {
  45. struct thread *thread = machine__findnew_thread(machine,
  46. event->fork.pid,
  47. event->fork.tid);
  48. dump_printf("(%d:%d):(%d:%d)\n", event->fork.pid, event->fork.tid,
  49. event->fork.ppid, event->fork.ptid);
  50. if (thread) {
  51. rb_erase(&thread->rb_node, &machine->threads);
  52. machine->last_match = NULL;
  53. thread__delete(thread);
  54. }
  55. return 0;
  56. }
  57. struct perf_tool build_id__mark_dso_hit_ops = {
  58. .sample = build_id__mark_dso_hit,
  59. .mmap = perf_event__process_mmap,
  60. .mmap2 = perf_event__process_mmap2,
  61. .fork = perf_event__process_fork,
  62. .exit = perf_event__exit_del_thread,
  63. .attr = perf_event__process_attr,
  64. .build_id = perf_event__process_build_id,
  65. };
  66. int build_id__sprintf(const u8 *build_id, int len, char *bf)
  67. {
  68. char *bid = bf;
  69. const u8 *raw = build_id;
  70. int i;
  71. for (i = 0; i < len; ++i) {
  72. sprintf(bid, "%02x", *raw);
  73. ++raw;
  74. bid += 2;
  75. }
  76. return raw - build_id;
  77. }
  78. char *dso__build_id_filename(struct dso *self, char *bf, size_t size)
  79. {
  80. char build_id_hex[BUILD_ID_SIZE * 2 + 1];
  81. if (!self->has_build_id)
  82. return NULL;
  83. build_id__sprintf(self->build_id, sizeof(self->build_id), build_id_hex);
  84. if (bf == NULL) {
  85. if (asprintf(&bf, "%s/.build-id/%.2s/%s", buildid_dir,
  86. build_id_hex, build_id_hex + 2) < 0)
  87. return NULL;
  88. } else
  89. snprintf(bf, size, "%s/.build-id/%.2s/%s", buildid_dir,
  90. build_id_hex, build_id_hex + 2);
  91. return bf;
  92. }