From 0f6dba145a4be998e0e5e4ccd94d9df8609eb327 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 19 Jan 2021 20:44:51 +0100 Subject: usb: add pcap support. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Log all traffic of a specific usb device to a pcap file for later inspection. File format is compatible with linux usb monitor. Usage: qemu -device usb-${somedevice},pcap=file.pcap wireshark file.pcap Signed-off-by: Gerd Hoffmann Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20210119194452.2148048-1-kraxel@redhat.com> Signed-off-by: Gerd Hoffmann --- hw/usb/bus.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'hw/usb/bus.c') diff --git a/hw/usb/bus.c b/hw/usb/bus.c index 2b11041451..064f94e9c3 100644 --- a/hw/usb/bus.c +++ b/hw/usb/bus.c @@ -23,6 +23,7 @@ static Property usb_props[] = { USB_DEV_FLAG_FULL_PATH, true), DEFINE_PROP_BIT("msos-desc", USBDevice, flags, USB_DEV_FLAG_MSOS_DESC_ENABLE, true), + DEFINE_PROP_STRING("pcap", USBDevice, pcap_filename), DEFINE_PROP_END_OF_LIST() }; @@ -270,6 +271,17 @@ static void usb_qdev_realize(DeviceState *qdev, Error **errp) return; } } + + if (dev->pcap_filename) { + int fd = qemu_open_old(dev->pcap_filename, O_CREAT | O_WRONLY | O_TRUNC, 0666); + if (fd < 0) { + error_setg(errp, "open %s failed", dev->pcap_filename); + usb_qdev_unrealize(qdev); + return; + } + dev->pcap = fdopen(fd, "w"); + usb_pcap_init(dev->pcap); + } } static void usb_qdev_unrealize(DeviceState *qdev) @@ -283,6 +295,10 @@ static void usb_qdev_unrealize(DeviceState *qdev) g_free(s); } + if (dev->pcap) { + fclose(dev->pcap); + } + if (dev->attached) { usb_device_detach(dev); } -- cgit 1.4.1