VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/VUSBSnifferVmx.cpp@ 59767

Last change on this file since 59767 was 59633, checked in by vboxsync, 9 years ago

USB/Sniffer: Add format writers for Linux usbmon ASCII output and VMware debug logs so analyzing logs with vusb-analyzer is possible

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* $Id: VUSBSnifferVmx.cpp 59633 2016-02-10 15:39:58Z vboxsync $ */
2/** @file
3 * Virtual USB Sniffer facility - VMX USBIO format.
4 */
5
6/*
7 * Copyright (C) 2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_VUSB
23#include <VBox/log.h>
24#include <iprt/mem.h>
25#include <iprt/buildconfig.h>
26#include <iprt/string.h>
27#include <iprt/system.h>
28#include <iprt/time.h>
29
30#include "VUSBSnifferInternal.h"
31
32
33/*********************************************************************************************************************************
34* Defined Constants And Macros *
35*********************************************************************************************************************************/
36
37/*********************************************************************************************************************************
38* Structures and Typedefs *
39*********************************************************************************************************************************/
40
41/**
42 * The internal VUSB sniffer state.
43 */
44typedef struct VUSBSNIFFERFMTINT
45{
46 /** Stream handle. */
47 PVUSBSNIFFERSTRM pStrm;
48} VUSBSNIFFERFMTINT;
49
50
51/*********************************************************************************************************************************
52* Static Variables *
53*********************************************************************************************************************************/
54
55/**
56 * Supported file extensions.
57 */
58static const char *s_apszFileExts[] =
59{
60 "vmx",
61 "vmware",
62 "usbio",
63 NULL
64};
65
66
67/**
68 * Month strings.
69 */
70static const char *s_apszMonths[] =
71{
72 "Jan",
73 "Feb",
74 "Mar",
75 "Apr",
76 "May",
77 "Jun",
78 "Jul",
79 "Aug",
80 "Sep",
81 "Oct",
82 "Nov",
83 "Dec"
84};
85
86/*********************************************************************************************************************************
87* Internal Functions *
88*********************************************************************************************************************************/
89
90
91static int vusbSnifferFmtVmxLogData(PVUSBSNIFFERFMTINT pThis, PRTTIME pTime, uint8_t *pbBuf, size_t cbBuf)
92{
93 int rc = VINF_SUCCESS;
94 char aszLineBuf[256];
95 uint16_t off = 0;
96
97 do
98 {
99 size_t cch = RTStrPrintf(&aszLineBuf[0], sizeof(aszLineBuf),
100 "%s %02u %02u:%02u:%02u.%3.*u: vmx| USBIO: %03x: %16.*Rhxs\n",
101 s_apszMonths[pTime->u8Month - 1], pTime->u8MonthDay, pTime->u8Hour, pTime->u8Minute, pTime->u8Second, 3, pTime->u32Nanosecond,
102 off, RT_MIN(cbBuf - off, 16), pbBuf);
103 rc = pThis->pStrm->pfnWrite(pThis->pStrm, &aszLineBuf[0], cch);
104 off += RT_MIN(cbBuf, 16);
105 pbBuf += RT_MIN(cbBuf, 16);
106 } while (RT_SUCCESS(rc) && off < cbBuf);
107
108 return rc;
109}
110
111/** @copydoc VUSBSNIFFERFMT::pfnInit */
112static DECLCALLBACK(int) vusbSnifferFmtVmxInit(PVUSBSNIFFERFMTINT pThis, PVUSBSNIFFERSTRM pStrm)
113{
114 pThis->pStrm = pStrm;
115 return VINF_SUCCESS;
116}
117
118
119/** @copydoc VUSBSNIFFERFMT::pfnDestroy */
120static DECLCALLBACK(void) vusbSnifferFmtVmxDestroy(PVUSBSNIFFERFMTINT pThis)
121{
122 NOREF(pThis);
123}
124
125
126/** @copydoc VUSBSNIFFERFMT::pfnRecordEvent */
127static DECLCALLBACK(int) vusbSnifferFmtVmxRecordEvent(PVUSBSNIFFERFMTINT pThis, PVUSBURB pUrb, VUSBSNIFFEREVENT enmEvent)
128{
129 RTTIMESPEC TimeNow;
130 RTTIME Time;
131 char aszLineBuf[256];
132 const char *pszEvt = enmEvent == VUSBSNIFFEREVENT_SUBMIT ? "Down" : "Up";
133 uint8_t cIsocPkts = pUrb->enmType == VUSBXFERTYPE_ISOC ? pUrb->cIsocPkts : 0;
134
135 if (pUrb->enmType == VUSBXFERTYPE_MSG)
136 return VINF_SUCCESS;
137
138 RT_ZERO(aszLineBuf);
139
140 RTTimeNow(&TimeNow);
141 RTTimeExplode(&Time, &TimeNow);
142
143 size_t cch = RTStrPrintf(&aszLineBuf[0], sizeof(aszLineBuf),
144 "%s %02u %02u:%02u:%02u.%3.*u: vmx| USBIO: %s dev=%u endpt=%x datalen=%u numPackets=%u status=%u 0\n",
145 s_apszMonths[Time.u8Month - 1], Time.u8MonthDay, Time.u8Hour, Time.u8Minute, Time.u8Second, 3, Time.u32Nanosecond,
146 pszEvt, pUrb->DstAddress, pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0x00),
147 pUrb->cbData, cIsocPkts, pUrb->enmStatus);
148 int rc = pThis->pStrm->pfnWrite(pThis->pStrm, &aszLineBuf[0], cch);
149 if (RT_SUCCESS(rc))
150 {
151 /* Log the data in the appropriate stage. */
152 if ( pUrb->enmType == VUSBXFERTYPE_CTRL
153 || pUrb->enmType == VUSBXFERTYPE_MSG)
154 {
155 if (enmEvent == VUSBSNIFFEREVENT_SUBMIT)
156 rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[0], sizeof(VUSBSETUP));
157 else if (enmEvent == VUSBSNIFFEREVENT_COMPLETE)
158 {
159 rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[0], sizeof(VUSBSETUP));
160 if ( RT_SUCCESS(rc)
161 && pUrb->cbData > sizeof(VUSBSETUP))
162 rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[sizeof(VUSBSETUP)], pUrb->cbData - sizeof(VUSBSETUP));
163 }
164 }
165 else
166 {
167 if ( enmEvent == VUSBSNIFFEREVENT_SUBMIT
168 && pUrb->enmDir == VUSBDIRECTION_OUT)
169 rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[0], pUrb->cbData);
170 else if ( enmEvent == VUSBSNIFFEREVENT_COMPLETE
171 && pUrb->enmDir == VUSBDIRECTION_IN)
172 rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[0], pUrb->cbData);
173 }
174 }
175
176 return rc;
177}
178
179/**
180 * VUSB sniffer format writer.
181 */
182const VUSBSNIFFERFMT g_VUsbSnifferFmtVmx =
183{
184 /** szName */
185 "VMX",
186 /** pszDesc */
187 "VMX log format writer supported by vusb-analyzer: http://vusb-analyzer.sourceforge.net",
188 /** papszFileExts */
189 &s_apszFileExts[0],
190 /** cbFmt */
191 sizeof(VUSBSNIFFERFMTINT),
192 /** pfnInit */
193 vusbSnifferFmtVmxInit,
194 /** pfnDestroy */
195 vusbSnifferFmtVmxDestroy,
196 /** pfnRecordEvent */
197 vusbSnifferFmtVmxRecordEvent
198};
199
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette