event.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/string.h>
  4. #include "../../util/machine.h"
  5. #include "../../util/tool.h"
  6. #include "../../util/map.h"
  7. #include "../../util/util.h"
  8. #include "../../util/debug.h"
  9. #if defined(__x86_64__)
  10. int perf_event__synthesize_extra_kmaps(struct perf_tool *tool,
  11. perf_event__handler_t process,
  12. struct machine *machine)
  13. {
  14. int rc = 0;
  15. struct map *pos;
  16. struct map_groups *kmaps = &machine->kmaps;
  17. struct maps *maps = &kmaps->maps;
  18. union perf_event *event = zalloc(sizeof(event->mmap) +
  19. machine->id_hdr_size);
  20. if (!event) {
  21. pr_debug("Not enough memory synthesizing mmap event "
  22. "for extra kernel maps\n");
  23. return -1;
  24. }
  25. for (pos = maps__first(maps); pos; pos = map__next(pos)) {
  26. struct kmap *kmap;
  27. size_t size;
  28. if (!__map__is_extra_kernel_map(pos))
  29. continue;
  30. kmap = map__kmap(pos);
  31. size = sizeof(event->mmap) - sizeof(event->mmap.filename) +
  32. PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) +
  33. machine->id_hdr_size;
  34. memset(event, 0, size);
  35. event->mmap.header.type = PERF_RECORD_MMAP;
  36. /*
  37. * kernel uses 0 for user space maps, see kernel/perf_event.c
  38. * __perf_event_mmap
  39. */
  40. if (machine__is_host(machine))
  41. event->header.misc = PERF_RECORD_MISC_KERNEL;
  42. else
  43. event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
  44. event->mmap.header.size = size;
  45. event->mmap.start = pos->start;
  46. event->mmap.len = pos->end - pos->start;
  47. event->mmap.pgoff = pos->pgoff;
  48. event->mmap.pid = machine->pid;
  49. strlcpy(event->mmap.filename, kmap->name, PATH_MAX);
  50. if (perf_tool__process_synth_event(tool, event, machine,
  51. process) != 0) {
  52. rc = -1;
  53. break;
  54. }
  55. }
  56. free(event);
  57. return rc;
  58. }
  59. #endif