summaryrefslogtreecommitdiffstats
path: root/tools/guest-diag/nvkmstrace.c
blob: 411de6b3cbd6b1b1be251036c56e196fb95ab2ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// nvkmstrace.c - LD_PRELOAD shim: log NVKMS_IOCTL_IOWR commands
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <dlfcn.h>
#include <sys/ioctl.h>

struct nvkms_ioctl_params {
	uint32_t cmd;
	uint32_t size;
	uint64_t address;
};

#define NVKMS_REQ 0xC0106D00UL

static int (*real_ioctl)(int, unsigned long, ...);

int ioctl(int fd, unsigned long req, ...)
{
	va_list ap;
	void *arg;

	va_start(ap, req);
	arg = va_arg(ap, void *);
	va_end(ap);
	if (!real_ioctl)
		real_ioctl = dlsym(RTLD_NEXT, "ioctl");
	if (req == NVKMS_REQ && arg) {
		struct nvkms_ioctl_params *p = arg;
		int rc;

		rc = real_ioctl(fd, req, arg);
		fprintf(stderr, "NVKMS_IOCTL cmd=%u size=%u addr=%p rc=%d\n",
			p->cmd, p->size, (void *)(uintptr_t)p->address, rc);
		if (p->address && p->size) {
			unsigned char *q = (unsigned char *)(uintptr_t)p->address;
			unsigned n = p->size < 64 ? p->size : 64, i;
			fprintf(stderr, "  data:");
			for (i = 0; i < n; i++)
				fprintf(stderr, " %02x", q[i]);
			fprintf(stderr, "\n");
		}
		return rc;
	}
	return real_ioctl(fd, req, arg);
}