1 | /* $Id: VBoxBugReportWin.cpp 59797 2016-02-24 13:55:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxBugReportWin - VirtualBox command-line diagnostics tool,
|
---|
4 | * Windows-specific part.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <VBox/com/com.h>
|
---|
20 | #include <VBox/com/string.h>
|
---|
21 |
|
---|
22 | #include <iprt/cpp/exception.h>
|
---|
23 |
|
---|
24 | #include "VBoxBugReport.h"
|
---|
25 |
|
---|
26 | #include <netcfgx.h>
|
---|
27 | #include <setupapi.h>
|
---|
28 | #include <initguid.h>
|
---|
29 | #include <devguid.h>
|
---|
30 | #include <usbiodef.h>
|
---|
31 | #include <usbioctl.h>
|
---|
32 |
|
---|
33 | #define ReleaseAndReset(obj) \
|
---|
34 | if (obj) \
|
---|
35 | obj->Release(); \
|
---|
36 | obj = NULL;
|
---|
37 |
|
---|
38 |
|
---|
39 | class BugReportNetworkAdaptersWin : public BugReportStream
|
---|
40 | {
|
---|
41 | public:
|
---|
42 | BugReportNetworkAdaptersWin() : BugReportStream("NetworkAdapters") {};
|
---|
43 | virtual ~BugReportNetworkAdaptersWin() {};
|
---|
44 | virtual PRTSTREAM getStream(void) { collect(); return BugReportStream::getStream(); };
|
---|
45 | private:
|
---|
46 | struct CharacteristicsName
|
---|
47 | {
|
---|
48 | DWORD dwChar;
|
---|
49 | const char *szName;
|
---|
50 | };
|
---|
51 | void printCharteristics(DWORD dwChars);
|
---|
52 | void collect();
|
---|
53 | void collectNetCfgComponentInfo(int ident, bool fEnabled, INetCfgComponent *pComponent);
|
---|
54 | };
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | void BugReportNetworkAdaptersWin::printCharteristics(DWORD dwChars)
|
---|
59 | {
|
---|
60 | static CharacteristicsName cMap[] =
|
---|
61 | {
|
---|
62 | { NCF_VIRTUAL, "virtual" },
|
---|
63 | { NCF_SOFTWARE_ENUMERATED, "software_enumerated" },
|
---|
64 | { NCF_PHYSICAL, "physical" },
|
---|
65 | { NCF_HIDDEN, "hidden" },
|
---|
66 | { NCF_NO_SERVICE, "no_service" },
|
---|
67 | { NCF_NOT_USER_REMOVABLE, "not_user_removable" },
|
---|
68 | { NCF_MULTIPORT_INSTANCED_ADAPTER, "multiport_instanced_adapter" },
|
---|
69 | { NCF_HAS_UI, "has_ui" },
|
---|
70 | { NCF_SINGLE_INSTANCE, "single_instance" },
|
---|
71 | { NCF_FILTER, "filter" },
|
---|
72 | { NCF_DONTEXPOSELOWER, "dontexposelower" },
|
---|
73 | { NCF_HIDE_BINDING, "hide_binding" },
|
---|
74 | { NCF_NDIS_PROTOCOL, "ndis_protocol" },
|
---|
75 | { NCF_FIXED_BINDING, "fixed_binding" },
|
---|
76 | { NCF_LW_FILTER, "lw_filter" }
|
---|
77 | };
|
---|
78 | bool fPrintDelim = false;
|
---|
79 |
|
---|
80 | for (int i = 0; i < RT_ELEMENTS(cMap); ++i)
|
---|
81 | {
|
---|
82 | if (dwChars & cMap[i].dwChar)
|
---|
83 | {
|
---|
84 | if (fPrintDelim)
|
---|
85 | {
|
---|
86 | putStr(", ");
|
---|
87 | fPrintDelim = false;
|
---|
88 | }
|
---|
89 | putStr(cMap[i].szName);
|
---|
90 | fPrintDelim = true;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | void BugReportNetworkAdaptersWin::collectNetCfgComponentInfo(int ident, bool fEnabled, INetCfgComponent *pComponent)
|
---|
96 | {
|
---|
97 | LPWSTR pwszName = NULL;
|
---|
98 | HRESULT hr = pComponent->GetDisplayName(&pwszName);
|
---|
99 | if (FAILED(hr))
|
---|
100 | throw RTCError(com::Utf8StrFmt("Failed to get component display name, hr=0x%x.\n", hr));
|
---|
101 | printf("%s%c %ls [", RTCString(ident, ' ').c_str(), fEnabled ? '+' : '-', pwszName);
|
---|
102 | if (pwszName)
|
---|
103 | CoTaskMemFree(pwszName);
|
---|
104 |
|
---|
105 | DWORD dwChars = 0;
|
---|
106 | hr = pComponent->GetCharacteristics(&dwChars);
|
---|
107 | if (FAILED(hr))
|
---|
108 | throw RTCError(com::Utf8StrFmt("Failed to get component characteristics, hr=0x%x.\n", hr));
|
---|
109 | printCharteristics(dwChars);
|
---|
110 | putStr("]\n");
|
---|
111 | }
|
---|
112 |
|
---|
113 | void BugReportNetworkAdaptersWin::collect(void)
|
---|
114 | {
|
---|
115 | INetCfg *pNetCfg = NULL;
|
---|
116 | IEnumNetCfgComponent *pEnumAdapters = NULL;
|
---|
117 | INetCfgComponent *pNetCfgAdapter = NULL;
|
---|
118 | INetCfgComponentBindings *pAdapterBindings = NULL;
|
---|
119 | IEnumNetCfgBindingPath *pEnumBp = NULL;
|
---|
120 | INetCfgBindingPath *pBp = NULL;
|
---|
121 | IEnumNetCfgBindingInterface *pEnumBi = NULL;
|
---|
122 | INetCfgBindingInterface *pBi = NULL;
|
---|
123 | INetCfgComponent *pUpperComponent = NULL;
|
---|
124 |
|
---|
125 | try
|
---|
126 | {
|
---|
127 | HRESULT hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (PVOID*)&pNetCfg);
|
---|
128 | if (FAILED(hr))
|
---|
129 | throw RTCError(com::Utf8StrFmt("Failed to create instance of INetCfg, hr=0x%x.\n", hr));
|
---|
130 | hr = pNetCfg->Initialize(NULL);
|
---|
131 | if (FAILED(hr))
|
---|
132 | throw RTCError(com::Utf8StrFmt("Failed to initialize instance of INetCfg, hr=0x%x.\n", hr));
|
---|
133 |
|
---|
134 | hr = pNetCfg->EnumComponents(&GUID_DEVCLASS_NET, &pEnumAdapters);
|
---|
135 | if (FAILED(hr))
|
---|
136 | throw RTCError(com::Utf8StrFmt("Failed enumerate network adapters, hr=0x%x.\n", hr));
|
---|
137 |
|
---|
138 | hr = pEnumAdapters->Reset();
|
---|
139 | Assert(SUCCEEDED(hr));
|
---|
140 | do
|
---|
141 | {
|
---|
142 | hr = pEnumAdapters->Next(1, &pNetCfgAdapter, NULL);
|
---|
143 | if (hr == S_FALSE)
|
---|
144 | break;
|
---|
145 | if (hr != S_OK)
|
---|
146 | throw RTCError(com::Utf8StrFmt("Failed to get next network adapter, hr=0x%x.\n", hr));
|
---|
147 | hr = pNetCfgAdapter->QueryInterface(IID_INetCfgComponentBindings, (PVOID*)&pAdapterBindings);
|
---|
148 | if (FAILED(hr))
|
---|
149 | throw RTCError(com::Utf8StrFmt("Failed to query INetCfgComponentBindings, hr=0x%x.\n", hr));
|
---|
150 | hr = pAdapterBindings->EnumBindingPaths(EBP_ABOVE, &pEnumBp);
|
---|
151 | if (FAILED(hr))
|
---|
152 | throw RTCError(com::Utf8StrFmt("Failed to enumerate binding paths, hr=0x%x.\n", hr));
|
---|
153 | hr = pEnumBp->Reset();
|
---|
154 | if (FAILED(hr))
|
---|
155 | throw RTCError(com::Utf8StrFmt("Failed to reset enumeration of binding paths (0x%x)\n", hr));
|
---|
156 | do
|
---|
157 | {
|
---|
158 | hr = pEnumBp->Next(1, &pBp, NULL);
|
---|
159 | if (hr == S_FALSE)
|
---|
160 | break;
|
---|
161 | if (hr != S_OK)
|
---|
162 | throw RTCError(com::Utf8StrFmt("Failed to get next network adapter, hr=0x%x.\n", hr));
|
---|
163 | bool fBpEnabled;
|
---|
164 | hr = pBp->IsEnabled();
|
---|
165 | if (hr == S_FALSE)
|
---|
166 | fBpEnabled = false;
|
---|
167 | else if (hr != S_OK)
|
---|
168 | throw RTCError(com::Utf8StrFmt("Failed to check if bind path is enabled, hr=0x%x.\n", hr));
|
---|
169 | else
|
---|
170 | fBpEnabled = true;
|
---|
171 | hr = pBp->EnumBindingInterfaces(&pEnumBi);
|
---|
172 | if (FAILED(hr))
|
---|
173 | throw RTCError(com::Utf8StrFmt("Failed to enumerate binding interfaces (0x%x)\n", hr));
|
---|
174 | hr = pEnumBi->Reset();
|
---|
175 | if (FAILED(hr))
|
---|
176 | throw RTCError(com::Utf8StrFmt("Failed to reset enumeration of binding interfaces (0x%x)\n", hr));
|
---|
177 | int ident;
|
---|
178 | for (ident = 0;; ++ident)
|
---|
179 | {
|
---|
180 | hr = pEnumBi->Next(1, &pBi, NULL);
|
---|
181 | if (hr == S_FALSE)
|
---|
182 | break;
|
---|
183 | if (hr != S_OK)
|
---|
184 | throw RTCError(com::Utf8StrFmt("Failed to get next binding interface, hr=0x%x.\n", hr));
|
---|
185 | hr = pBi->GetUpperComponent(&pUpperComponent);
|
---|
186 | if (FAILED(hr))
|
---|
187 | throw RTCError(com::Utf8StrFmt("Failed to get upper component, hr=0x%x.\n", hr));
|
---|
188 | collectNetCfgComponentInfo(ident, fBpEnabled, pUpperComponent);
|
---|
189 | ReleaseAndReset(pUpperComponent);
|
---|
190 | ReleaseAndReset(pBi);
|
---|
191 | }
|
---|
192 | collectNetCfgComponentInfo(ident, fBpEnabled, pNetCfgAdapter);
|
---|
193 | ReleaseAndReset(pEnumBi);
|
---|
194 | ReleaseAndReset(pBp);
|
---|
195 | } while (true);
|
---|
196 |
|
---|
197 | ReleaseAndReset(pEnumBp);
|
---|
198 | ReleaseAndReset(pAdapterBindings);
|
---|
199 | ReleaseAndReset(pNetCfgAdapter);
|
---|
200 | } while (true);
|
---|
201 | ReleaseAndReset(pEnumAdapters);
|
---|
202 | ReleaseAndReset(pNetCfg);
|
---|
203 | }
|
---|
204 |
|
---|
205 | catch (RTCError &e)
|
---|
206 | {
|
---|
207 | ReleaseAndReset(pUpperComponent);
|
---|
208 | ReleaseAndReset(pBi);
|
---|
209 | ReleaseAndReset(pEnumBi);
|
---|
210 | ReleaseAndReset(pBp);
|
---|
211 | ReleaseAndReset(pEnumBp);
|
---|
212 | ReleaseAndReset(pAdapterBindings);
|
---|
213 | ReleaseAndReset(pNetCfgAdapter);
|
---|
214 | ReleaseAndReset(pEnumAdapters);
|
---|
215 | ReleaseAndReset(pNetCfg);
|
---|
216 | RTPrintf("ERROR in osCollect: %s\n", e.what());
|
---|
217 | throw;
|
---|
218 | }
|
---|
219 |
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | class ErrorHandler
|
---|
224 | {
|
---|
225 | public:
|
---|
226 | ErrorHandler(const char *pszFunction, int iLine)
|
---|
227 | : m_function(pszFunction), m_line(iLine) {};
|
---|
228 | void handleWinError(DWORD uError, const char *pszMsgFmt, ...)
|
---|
229 | {
|
---|
230 | if (uError != ERROR_SUCCESS)
|
---|
231 | {
|
---|
232 | va_list va;
|
---|
233 | va_start(va, pszMsgFmt);
|
---|
234 | RTCString msgArgs(pszMsgFmt, va);
|
---|
235 | va_end(va);
|
---|
236 | LPSTR pBuf = NULL;
|
---|
237 | DWORD cb = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
---|
238 | NULL, uError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&pBuf, 0, NULL);
|
---|
239 | RTCStringFmt msg("%s at %s(%d): err=%u %s", msgArgs.c_str(), m_function, m_line, uError, pBuf);
|
---|
240 | LocalFree(pBuf);
|
---|
241 | throw RTCError(msg.c_str());
|
---|
242 | }
|
---|
243 | };
|
---|
244 | private:
|
---|
245 | const char *m_function;
|
---|
246 | int m_line;
|
---|
247 | };
|
---|
248 | #define handleWinError ErrorHandler(__FUNCTION__, __LINE__).handleWinError
|
---|
249 |
|
---|
250 |
|
---|
251 | class BugReportUsbTreeWin : public BugReportStream
|
---|
252 | {
|
---|
253 | public:
|
---|
254 | BugReportUsbTreeWin();
|
---|
255 | virtual ~BugReportUsbTreeWin();
|
---|
256 | virtual PRTSTREAM getStream(void) { enumerate(); return BugReportStream::getStream(); };
|
---|
257 | private:
|
---|
258 | class AutoHandle {
|
---|
259 | public:
|
---|
260 | AutoHandle(HANDLE h) { m_h = h; };
|
---|
261 | ~AutoHandle() { close(); };
|
---|
262 | bool isValid() { return m_h != INVALID_HANDLE_VALUE; };
|
---|
263 | operator HANDLE() { return m_h; };
|
---|
264 | void close(void) { if (isValid()) { CloseHandle(m_h); m_h = INVALID_HANDLE_VALUE; } };
|
---|
265 | private:
|
---|
266 | HANDLE m_h;
|
---|
267 | };
|
---|
268 | void enumerate();
|
---|
269 |
|
---|
270 | void enumerateController(PSP_DEVINFO_DATA pInfoData, PSP_DEVICE_INTERFACE_DATA pInterfaceData);
|
---|
271 | void enumerateHub(RTCString strFullName, RTCString strPrefix);
|
---|
272 | void enumeratePorts(HANDLE hHub, unsigned cPorts, RTCString strPrefix);
|
---|
273 | PBYTE getDeviceRegistryProperty(HDEVINFO hDev, PSP_DEVINFO_DATA pInfoData, DWORD uProperty,
|
---|
274 | DWORD uExpectedType, PDWORD puSize);
|
---|
275 | RTCString getDeviceRegistryPropertyString(HDEVINFO hDev, PSP_DEVINFO_DATA pInfoData, DWORD uProperty);
|
---|
276 |
|
---|
277 | RTCString getDeviceDescByDriverName(RTCString strDrvName);
|
---|
278 | RTCString getDriverKeyName(HANDLE hHub, int iPort);
|
---|
279 | RTCString getExternalHubName(HANDLE hHub, int iPort);
|
---|
280 |
|
---|
281 | HDEVINFO m_hDevInfo;
|
---|
282 | PSP_DEVICE_INTERFACE_DETAIL_DATA m_pDetailData;
|
---|
283 | HANDLE m_hHostCtrlDev;
|
---|
284 | };
|
---|
285 |
|
---|
286 | BugReportUsbTreeWin::BugReportUsbTreeWin() : BugReportStream("HostUsbTree")
|
---|
287 | {
|
---|
288 | m_hDevInfo = INVALID_HANDLE_VALUE;
|
---|
289 | m_pDetailData = NULL;
|
---|
290 | m_hHostCtrlDev = INVALID_HANDLE_VALUE;
|
---|
291 | }
|
---|
292 |
|
---|
293 | BugReportUsbTreeWin::~BugReportUsbTreeWin()
|
---|
294 | {
|
---|
295 | if (m_hHostCtrlDev != INVALID_HANDLE_VALUE)
|
---|
296 | CloseHandle(m_hHostCtrlDev);
|
---|
297 | if (m_pDetailData)
|
---|
298 | RTMemFree(m_pDetailData);
|
---|
299 | if (m_hDevInfo != INVALID_HANDLE_VALUE)
|
---|
300 | SetupDiDestroyDeviceInfoList(m_hDevInfo);
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | PBYTE BugReportUsbTreeWin::getDeviceRegistryProperty(HDEVINFO hDev,
|
---|
305 | PSP_DEVINFO_DATA pInfoData,
|
---|
306 | DWORD uProperty,
|
---|
307 | DWORD uExpectedType,
|
---|
308 | PDWORD puSize)
|
---|
309 | {
|
---|
310 | DWORD uActualType, cbNeeded = 0;
|
---|
311 | if (!SetupDiGetDeviceRegistryProperty(hDev, pInfoData, uProperty, &uActualType,
|
---|
312 | NULL, 0, &cbNeeded)
|
---|
313 | && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
---|
314 | {
|
---|
315 | if (GetLastError() == ERROR_INVALID_DATA)
|
---|
316 | return NULL;
|
---|
317 | handleWinError(GetLastError(), "SetupDiGetDeviceRegistryProperty(0x%x) failed", uProperty);
|
---|
318 | }
|
---|
319 | if (uExpectedType != REG_NONE && uActualType != uExpectedType)
|
---|
320 | throw RTCError(RTCStringFmt("SetupDiGetDeviceRegistryProperty(0x%x) returned type %d instead of %d",
|
---|
321 | uActualType, uExpectedType).c_str());
|
---|
322 | PBYTE pBuffer = (PBYTE)RTMemAlloc(cbNeeded);
|
---|
323 | if (!pBuffer)
|
---|
324 | throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());
|
---|
325 | if (!SetupDiGetDeviceRegistryProperty(hDev, pInfoData, uProperty, NULL,
|
---|
326 | pBuffer, cbNeeded, &cbNeeded))
|
---|
327 | {
|
---|
328 | DWORD dwErr = GetLastError();
|
---|
329 | RTMemFree(pBuffer);
|
---|
330 | pBuffer = NULL;
|
---|
331 | handleWinError(dwErr, "SetupDiGetDeviceRegistryProperty(0x%x) failed", uProperty);
|
---|
332 | }
|
---|
333 | if (puSize)
|
---|
334 | *puSize = cbNeeded;
|
---|
335 |
|
---|
336 | return pBuffer;
|
---|
337 | }
|
---|
338 |
|
---|
339 | RTCString BugReportUsbTreeWin::getDeviceRegistryPropertyString(HDEVINFO hDev, PSP_DEVINFO_DATA pInfoData, DWORD uProperty)
|
---|
340 | {
|
---|
341 | DWORD cbString = 0;
|
---|
342 | PWSTR pUnicodeString = (PWSTR)getDeviceRegistryProperty(hDev, pInfoData, uProperty, REG_SZ, NULL);
|
---|
343 |
|
---|
344 | if (!pUnicodeString)
|
---|
345 | return RTCString();
|
---|
346 |
|
---|
347 | RTCStringFmt utf8string("%ls", pUnicodeString);
|
---|
348 | RTMemFree(pUnicodeString);
|
---|
349 | return utf8string;
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | RTCString BugReportUsbTreeWin::getDeviceDescByDriverName(RTCString strDrvName)
|
---|
354 | {
|
---|
355 | DWORD dwErr;
|
---|
356 | SP_DEVINFO_DATA devInfoData;
|
---|
357 | HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
|
---|
358 |
|
---|
359 | if (hDevInfo == INVALID_HANDLE_VALUE)
|
---|
360 | handleWinError(GetLastError(), "SetupDiGetClassDevs failed");
|
---|
361 |
|
---|
362 | bool fFound = false;
|
---|
363 | devInfoData.cbSize = sizeof(devInfoData);
|
---|
364 | for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &devInfoData); ++i)
|
---|
365 | {
|
---|
366 | if (getDeviceRegistryPropertyString(hDevInfo, &devInfoData, SPDRP_DRIVER).equals(strDrvName))
|
---|
367 | {
|
---|
368 | fFound = true;
|
---|
369 | break;
|
---|
370 | }
|
---|
371 | }
|
---|
372 | if (!fFound)
|
---|
373 | {
|
---|
374 | dwErr = GetLastError();
|
---|
375 | SetupDiDestroyDeviceInfoList(hDevInfo);
|
---|
376 | handleWinError(dwErr, "SetupDiEnumDeviceInfo failed");
|
---|
377 | }
|
---|
378 |
|
---|
379 | RTCString strDesc = getDeviceRegistryPropertyString(hDevInfo, &devInfoData, SPDRP_DEVICEDESC);
|
---|
380 | SetupDiDestroyDeviceInfoList(hDevInfo);
|
---|
381 | return strDesc;
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | RTCString BugReportUsbTreeWin::getDriverKeyName(HANDLE hHub, int iPort)
|
---|
386 | {
|
---|
387 | USB_NODE_CONNECTION_DRIVERKEY_NAME name;
|
---|
388 | ULONG cbNeeded = 0;
|
---|
389 |
|
---|
390 | name.ConnectionIndex = iPort;
|
---|
391 | if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
|
---|
392 | &name, sizeof(name), &name, sizeof(name), &cbNeeded, NULL))
|
---|
393 | handleWinError(GetLastError(), "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME) failed");
|
---|
394 | cbNeeded = name.ActualLength;
|
---|
395 | PUSB_NODE_CONNECTION_DRIVERKEY_NAME pName = (PUSB_NODE_CONNECTION_DRIVERKEY_NAME)RTMemAlloc(cbNeeded);
|
---|
396 | if (!pName)
|
---|
397 | throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());
|
---|
398 | pName->ConnectionIndex = iPort;
|
---|
399 | if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
|
---|
400 | pName, cbNeeded, pName, cbNeeded, &cbNeeded, NULL))
|
---|
401 | {
|
---|
402 | DWORD dwErr = GetLastError();
|
---|
403 | RTMemFree(pName);
|
---|
404 | handleWinError(dwErr, "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME) failed");
|
---|
405 | }
|
---|
406 | RTCStringFmt strName("%ls", pName->DriverKeyName);
|
---|
407 | RTMemFree(pName);
|
---|
408 | return strName;
|
---|
409 | }
|
---|
410 |
|
---|
411 |
|
---|
412 | RTCString BugReportUsbTreeWin::getExternalHubName(HANDLE hHub, int iPort)
|
---|
413 | {
|
---|
414 | USB_NODE_CONNECTION_NAME name;
|
---|
415 | ULONG cbNeeded = 0;
|
---|
416 |
|
---|
417 | name.ConnectionIndex = iPort;
|
---|
418 | if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_NAME,
|
---|
419 | &name, sizeof(name), &name, sizeof(name), &cbNeeded, NULL))
|
---|
420 | handleWinError(GetLastError(), "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_NAME) failed");
|
---|
421 | cbNeeded = name.ActualLength;
|
---|
422 | PUSB_NODE_CONNECTION_NAME pName = (PUSB_NODE_CONNECTION_NAME)RTMemAlloc(cbNeeded);
|
---|
423 | if (!pName)
|
---|
424 | throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());
|
---|
425 | pName->ConnectionIndex = iPort;
|
---|
426 | if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_NAME,
|
---|
427 | pName, cbNeeded, pName, cbNeeded, &cbNeeded, NULL))
|
---|
428 | {
|
---|
429 | DWORD dwErr = GetLastError();
|
---|
430 | RTMemFree(pName);
|
---|
431 | handleWinError(dwErr, "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_NAME) failed");
|
---|
432 | }
|
---|
433 | RTCStringFmt strName("%ls", pName->NodeName);
|
---|
434 | RTMemFree(pName);
|
---|
435 | return strName;
|
---|
436 | }
|
---|
437 |
|
---|
438 |
|
---|
439 | void BugReportUsbTreeWin::enumeratePorts(HANDLE hHub, unsigned cPorts, RTCString strPrefix)
|
---|
440 | {
|
---|
441 | DWORD cbInfo = sizeof(USB_NODE_CONNECTION_INFORMATION_EX) + 30 * sizeof(USB_PIPE_INFO);
|
---|
442 | PUSB_NODE_CONNECTION_INFORMATION_EX pInfo = (PUSB_NODE_CONNECTION_INFORMATION_EX)RTMemAlloc(cbInfo);
|
---|
443 | if (!pInfo)
|
---|
444 | throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbInfo).c_str());
|
---|
445 | for (unsigned i = 1; i <= cPorts; ++i)
|
---|
446 | {
|
---|
447 | pInfo->ConnectionIndex = i;
|
---|
448 | if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
|
---|
449 | pInfo, cbInfo, pInfo, cbInfo, &cbInfo, NULL))
|
---|
450 | {
|
---|
451 | DWORD dwErr = GetLastError();
|
---|
452 | RTMemFree(pInfo);
|
---|
453 | handleWinError(dwErr, "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_INFORMATION) failed");
|
---|
454 | }
|
---|
455 | if (pInfo->ConnectionStatus == NoDeviceConnected)
|
---|
456 | printf("%s[Port %d]\n", strPrefix.c_str(), i);
|
---|
457 | else
|
---|
458 | {
|
---|
459 | RTCString strName = getDeviceDescByDriverName(getDriverKeyName(hHub, i));
|
---|
460 | printf("%s[Port %d] %s\n", strPrefix.c_str(), i, strName.c_str());
|
---|
461 | if (pInfo->DeviceIsHub)
|
---|
462 | enumerateHub(getExternalHubName(hHub, i), strPrefix + " ");
|
---|
463 | }
|
---|
464 | }
|
---|
465 | RTMemFree(pInfo);
|
---|
466 | }
|
---|
467 |
|
---|
468 | void BugReportUsbTreeWin::enumerateHub(RTCString strFullName, RTCString strPrefix)
|
---|
469 | {
|
---|
470 | AutoHandle hHubDev(CreateFileA(RTCString("\\\\.\\").append(strFullName).c_str(),
|
---|
471 | GENERIC_WRITE, FILE_SHARE_WRITE,
|
---|
472 | NULL, OPEN_EXISTING, 0, NULL));
|
---|
473 | if (!hHubDev.isValid())
|
---|
474 | handleWinError(GetLastError(), "CreateFile(%s) failed", strFullName.c_str());
|
---|
475 | ULONG cb;
|
---|
476 | USB_NODE_INFORMATION hubInfo;
|
---|
477 | if (!DeviceIoControl(hHubDev,
|
---|
478 | IOCTL_USB_GET_NODE_INFORMATION,
|
---|
479 | &hubInfo,
|
---|
480 | sizeof(USB_NODE_INFORMATION),
|
---|
481 | &hubInfo,
|
---|
482 | sizeof(USB_NODE_INFORMATION),
|
---|
483 | &cb,
|
---|
484 | NULL))
|
---|
485 | handleWinError(GetLastError(), "DeviceIoControl(IOCTL_USB_GET_NODE_INFORMATION) failed");
|
---|
486 | enumeratePorts(hHubDev, hubInfo.u.HubInformation.HubDescriptor.bNumberOfPorts, strPrefix);
|
---|
487 | }
|
---|
488 |
|
---|
489 | void BugReportUsbTreeWin::enumerateController(PSP_DEVINFO_DATA pInfoData, PSP_DEVICE_INTERFACE_DATA pInterfaceData)
|
---|
490 | {
|
---|
491 | RTCString strCtrlDesc = getDeviceRegistryPropertyString(m_hDevInfo, pInfoData, SPDRP_DEVICEDESC);
|
---|
492 | printf("%s\n", strCtrlDesc.c_str());
|
---|
493 |
|
---|
494 | ULONG cbNeeded;
|
---|
495 | USB_ROOT_HUB_NAME rootHub;
|
---|
496 | /* Find out the name length first */
|
---|
497 | if (!DeviceIoControl(m_hHostCtrlDev, IOCTL_USB_GET_ROOT_HUB_NAME, NULL, 0,
|
---|
498 | &rootHub, sizeof(rootHub),
|
---|
499 | &cbNeeded, NULL))
|
---|
500 | handleWinError(GetLastError(), "DeviceIoControl(IOCTL_USB_GET_ROOT_HUB_NAME) failed");
|
---|
501 | cbNeeded = rootHub.ActualLength;
|
---|
502 | PUSB_ROOT_HUB_NAME pUnicodeName = (PUSB_ROOT_HUB_NAME)RTMemAlloc(cbNeeded);
|
---|
503 | if (!pUnicodeName)
|
---|
504 | throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());
|
---|
505 |
|
---|
506 | if (!DeviceIoControl(m_hHostCtrlDev, IOCTL_USB_GET_ROOT_HUB_NAME, NULL, 0,
|
---|
507 | pUnicodeName, cbNeeded,
|
---|
508 | &cbNeeded, NULL))
|
---|
509 | {
|
---|
510 | DWORD dwErr = GetLastError();
|
---|
511 | RTMemFree(pUnicodeName);
|
---|
512 | handleWinError(dwErr, "DeviceIoControl(IOCTL_USB_GET_ROOT_HUB_NAME) failed");
|
---|
513 | }
|
---|
514 |
|
---|
515 | RTCStringFmt strRootHubName("%ls", pUnicodeName->RootHubName);
|
---|
516 | RTMemFree(pUnicodeName);
|
---|
517 | printf(" Root Hub\n");
|
---|
518 | enumerateHub(strRootHubName, " ");
|
---|
519 | }
|
---|
520 |
|
---|
521 | void BugReportUsbTreeWin::enumerate()
|
---|
522 | {
|
---|
523 | m_hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVINTERFACE_USB_HOST_CONTROLLER, NULL, NULL,
|
---|
524 | DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
---|
525 | if (m_hDevInfo == INVALID_HANDLE_VALUE)
|
---|
526 | handleWinError(GetLastError(), "SetupDiGetClassDevs(GUID_DEVINTERFACE_USB_HOST_CONTROLLER) failed");
|
---|
527 |
|
---|
528 | SP_DEVINFO_DATA deviceInfoData;
|
---|
529 | deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
|
---|
530 | for (int i = 0; SetupDiEnumDeviceInfo(m_hDevInfo, i, &deviceInfoData); ++i)
|
---|
531 | {
|
---|
532 | SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
|
---|
533 | deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
|
---|
534 | if (!SetupDiEnumDeviceInterfaces(m_hDevInfo, 0, (LPGUID)&GUID_DEVINTERFACE_USB_HOST_CONTROLLER,
|
---|
535 | i, &deviceInterfaceData))
|
---|
536 | handleWinError(GetLastError(), "SetupDiEnumDeviceInterfaces(GUID_DEVINTERFACE_USB_HOST_CONTROLLER) failed");
|
---|
537 |
|
---|
538 | ULONG cbNeeded = 0;
|
---|
539 | if (!SetupDiGetDeviceInterfaceDetail(m_hDevInfo, &deviceInterfaceData, NULL, 0, &cbNeeded, NULL)
|
---|
540 | && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
---|
541 | handleWinError(GetLastError(), "SetupDiGetDeviceInterfaceDetail failed");
|
---|
542 |
|
---|
543 | m_pDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)RTMemAlloc(cbNeeded);
|
---|
544 | if (!m_pDetailData)
|
---|
545 | throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());
|
---|
546 |
|
---|
547 | m_pDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
---|
548 | if (!SetupDiGetDeviceInterfaceDetail(m_hDevInfo, &deviceInterfaceData, m_pDetailData, cbNeeded, &cbNeeded, NULL))
|
---|
549 | handleWinError(GetLastError(), "SetupDiGetDeviceInterfaceDetail failed");
|
---|
550 |
|
---|
551 | m_hHostCtrlDev = CreateFile(m_pDetailData->DevicePath, GENERIC_WRITE, FILE_SHARE_WRITE,
|
---|
552 | NULL, OPEN_EXISTING, 0, NULL);
|
---|
553 | if (m_hHostCtrlDev == INVALID_HANDLE_VALUE)
|
---|
554 | handleWinError(GetLastError(), "CreateFile(%ls) failed", m_pDetailData);
|
---|
555 |
|
---|
556 | enumerateController(&deviceInfoData, &deviceInterfaceData);
|
---|
557 | }
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 | void createBugReportOsSpecific(BugReport* report, const char *pszHome)
|
---|
562 | {
|
---|
563 | WCHAR szWinDir[MAX_PATH];
|
---|
564 |
|
---|
565 | int cbNeeded = GetWindowsDirectory(szWinDir, RT_ELEMENTS(szWinDir));
|
---|
566 | if (cbNeeded == 0)
|
---|
567 | throw RTCError(RTCStringFmt("Failed to get Windows directory (err=%d)\n", GetLastError()));
|
---|
568 | if (cbNeeded > MAX_PATH)
|
---|
569 | throw RTCError(RTCStringFmt("Failed to get Windows directory (needed %d-byte buffer)\n", cbNeeded));
|
---|
570 | RTCStringFmt WinInfDir("%ls/inf", szWinDir);
|
---|
571 | report->addItem(new BugReportFile(PathJoin(WinInfDir.c_str(), "setupapi.app.log"), "setupapi.app.log"));
|
---|
572 | report->addItem(new BugReportFile(PathJoin(WinInfDir.c_str(), "setupapi.dev.log"), "setupapi.dev.log"));
|
---|
573 | report->addItem(new BugReportNetworkAdaptersWin);
|
---|
574 | RTCStringFmt WinSysDir("%ls/System32", szWinDir);
|
---|
575 | report->addItem(new BugReportCommand("SystemEvents", PathJoin(WinSysDir.c_str(), "wevtutil.exe"),
|
---|
576 | "qe", "System",
|
---|
577 | "/q:*[System[Provider[@Name='VBoxUSBMon' or @Name='VBoxNetLwf']]]", NULL));
|
---|
578 | report->addItem(new BugReportCommand("UpdateHistory", PathJoin(WinSysDir.c_str(), "wbem/wmic.exe"),
|
---|
579 | "qfe", "list", "brief", NULL));
|
---|
580 | report->addItem(new BugReportCommand("DriverServices", PathJoin(WinSysDir.c_str(), "sc.exe"),
|
---|
581 | "query", "type=", "driver", "state=", "all", NULL));
|
---|
582 | report->addItem(new BugReportUsbTreeWin);
|
---|
583 | }
|
---|