2018-05-22 13:54:37 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/string.h>
|
2019-07-04 11:32:27 -03:00
|
|
|
#include <linux/zalloc.h>
|
2022-10-25 17:32:19 -03:00
|
|
|
#include <stdlib.h>
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2020-03-05 23:11:08 -08:00
|
|
|
#include "../../../util/event.h"
|
|
|
|
#include "../../../util/synthetic-events.h"
|
|
|
|
#include "../../../util/machine.h"
|
|
|
|
#include "../../../util/tool.h"
|
|
|
|
#include "../../../util/map.h"
|
|
|
|
#include "../../../util/debug.h"
|
2022-10-26 17:24:27 -03:00
|
|
|
#include "util/sample.h"
|
2018-05-22 13:54:37 +03:00
|
|
|
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
struct perf_event__synthesize_extra_kmaps_cb_args {
|
2024-08-12 13:46:55 -07:00
|
|
|
const struct perf_tool *tool;
|
2023-12-06 17:16:37 -08:00
|
|
|
perf_event__handler_t process;
|
|
|
|
struct machine *machine;
|
|
|
|
union perf_event *event;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int perf_event__synthesize_extra_kmaps_cb(struct map *map, void *data)
|
2018-05-22 13:54:37 +03:00
|
|
|
{
|
2023-12-06 17:16:37 -08:00
|
|
|
struct perf_event__synthesize_extra_kmaps_cb_args *args = data;
|
|
|
|
union perf_event *event = args->event;
|
|
|
|
struct kmap *kmap;
|
|
|
|
size_t size;
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
if (!__map__is_extra_kernel_map(map))
|
|
|
|
return 0;
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
kmap = map__kmap(map);
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
size = sizeof(event->mmap) - sizeof(event->mmap.filename) +
|
|
|
|
PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) +
|
|
|
|
args->machine->id_hdr_size;
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
memset(event, 0, size);
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
event->mmap.header.type = PERF_RECORD_MMAP;
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
/*
|
|
|
|
* kernel uses 0 for user space maps, see kernel/perf_event.c
|
|
|
|
* __perf_event_mmap
|
|
|
|
*/
|
|
|
|
if (machine__is_host(args->machine))
|
|
|
|
event->header.misc = PERF_RECORD_MISC_KERNEL;
|
|
|
|
else
|
|
|
|
event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
event->mmap.header.size = size;
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
event->mmap.start = map__start(map);
|
|
|
|
event->mmap.len = map__size(map);
|
|
|
|
event->mmap.pgoff = map__pgoff(map);
|
|
|
|
event->mmap.pid = args->machine->pid;
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
strlcpy(event->mmap.filename, kmap->name, PATH_MAX);
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
if (perf_tool__process_synth_event(args->tool, event, args->machine, args->process) != 0)
|
|
|
|
return -1;
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-22 13:54:37 +03:00
|
|
|
|
2024-08-12 13:46:55 -07:00
|
|
|
int perf_event__synthesize_extra_kmaps(const struct perf_tool *tool,
|
2023-12-06 17:16:37 -08:00
|
|
|
perf_event__handler_t process,
|
|
|
|
struct machine *machine)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct maps *kmaps = machine__kernel_maps(machine);
|
|
|
|
struct perf_event__synthesize_extra_kmaps_cb_args args = {
|
|
|
|
.tool = tool,
|
|
|
|
.process = process,
|
|
|
|
.machine = machine,
|
|
|
|
.event = zalloc(sizeof(args.event->mmap) + machine->id_hdr_size),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!args.event) {
|
|
|
|
pr_debug("Not enough memory synthesizing mmap event "
|
|
|
|
"for extra kernel maps\n");
|
|
|
|
return -1;
|
2018-05-22 13:54:37 +03:00
|
|
|
}
|
|
|
|
|
2023-12-06 17:16:37 -08:00
|
|
|
rc = maps__for_each_map(kmaps, perf_event__synthesize_extra_kmaps_cb, &args);
|
|
|
|
|
|
|
|
free(args.event);
|
2018-05-22 13:54:37 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|