summaryrefslogtreecommitdiffstats
path: root/src/VBox/Frontends/VBoxBugReport/VBoxBugReportWin.cpp
blob: 28a267945bd0569c1f4e85289769d36263b92be2 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/* $Id: VBoxBugReportWin.cpp $ */
/** @file
 * VBoxBugReportWin - VirtualBox command-line diagnostics tool, Windows-specific part.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include <VBox/com/com.h>
#include <VBox/com/string.h>

#include <iprt/cpp/exception.h>

#include "VBoxBugReport.h"

#include <netcfgx.h>
#include <iprt/win/setupapi.h>
#include <initguid.h>
#include <devguid.h>
#include <usbiodef.h>
#include <usbioctl.h>
#include <psapi.h>

#define ReleaseAndReset(obj) \
    if (obj) \
        obj->Release(); \
    obj = NULL;


class BugReportNetworkAdaptersWin : public BugReportStream
{
public:
    BugReportNetworkAdaptersWin() : BugReportStream("NetworkAdapters") {};
    virtual ~BugReportNetworkAdaptersWin() {};
    virtual RTVFSIOSTREAM getStream(void) { collect(); return BugReportStream::getStream(); };
private:
    struct CharacteristicsName
    {
        DWORD dwChar;
        const char *szName;
    };
    void printCharteristics(DWORD dwChars);
    void collect();
    void collectNetCfgComponentInfo(int ident, bool fEnabled, INetCfgComponent *pComponent);
};



void BugReportNetworkAdaptersWin::printCharteristics(DWORD dwChars)
{
    static CharacteristicsName cMap[] =
    {
        { NCF_VIRTUAL, "virtual" },
        { NCF_SOFTWARE_ENUMERATED, "software_enumerated" },
        { NCF_PHYSICAL, "physical" },
        { NCF_HIDDEN, "hidden" },
        { NCF_NO_SERVICE, "no_service" },
        { NCF_NOT_USER_REMOVABLE, "not_user_removable" },
        { NCF_MULTIPORT_INSTANCED_ADAPTER, "multiport_instanced_adapter" },
        { NCF_HAS_UI, "has_ui" },
        { NCF_SINGLE_INSTANCE, "single_instance" },
        { NCF_FILTER, "filter" },
        { NCF_DONTEXPOSELOWER, "dontexposelower" },
        { NCF_HIDE_BINDING, "hide_binding" },
        { NCF_NDIS_PROTOCOL, "ndis_protocol" },
        { NCF_FIXED_BINDING, "fixed_binding" },
        { NCF_LW_FILTER, "lw_filter" }
    };
    bool fPrintDelim = false;

    for (int i = 0; i < RT_ELEMENTS(cMap); ++i)
    {
        if (dwChars & cMap[i].dwChar)
        {
            if (fPrintDelim)
            {
                putStr(", ");
                fPrintDelim = false;
            }
            putStr(cMap[i].szName);
            fPrintDelim = true;
        }
    }
}

void BugReportNetworkAdaptersWin::collectNetCfgComponentInfo(int ident, bool fEnabled, INetCfgComponent *pComponent)
{
    LPWSTR pwszName = NULL;
    HRESULT hr = pComponent->GetDisplayName(&pwszName);
    if (FAILED(hr))
        throw RTCError(com::Utf8StrFmt("Failed to get component display name, hr=0x%x.\n", hr));
    printf("%s%c %ls [", RTCString(ident, ' ').c_str(), fEnabled ? '+' : '-', pwszName);
    if (pwszName)
        CoTaskMemFree(pwszName);

    DWORD dwChars = 0;
    hr = pComponent->GetCharacteristics(&dwChars);
    if (FAILED(hr))
        throw RTCError(com::Utf8StrFmt("Failed to get component characteristics, hr=0x%x.\n", hr));
    printCharteristics(dwChars);
    putStr("]\n");
}

void BugReportNetworkAdaptersWin::collect(void)
{
    INetCfg                     *pNetCfg          = NULL;
    IEnumNetCfgComponent        *pEnumAdapters    = NULL;
    INetCfgComponent            *pNetCfgAdapter   = NULL;
    INetCfgComponentBindings    *pAdapterBindings = NULL;
    IEnumNetCfgBindingPath      *pEnumBp          = NULL;
    INetCfgBindingPath          *pBp              = NULL;
    IEnumNetCfgBindingInterface *pEnumBi          = NULL;
    INetCfgBindingInterface     *pBi              = NULL;
    INetCfgComponent            *pUpperComponent  = NULL;

    try
    {
        HRESULT hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (PVOID*)&pNetCfg);
        if (FAILED(hr))
            throw RTCError(com::Utf8StrFmt("Failed to create instance of INetCfg, hr=0x%x.\n", hr));
        hr = pNetCfg->Initialize(NULL);
        if (FAILED(hr))
            throw RTCError(com::Utf8StrFmt("Failed to initialize instance of INetCfg, hr=0x%x.\n", hr));

        hr = pNetCfg->EnumComponents(&GUID_DEVCLASS_NET, &pEnumAdapters);
        if (FAILED(hr))
            throw RTCError(com::Utf8StrFmt("Failed enumerate network adapters, hr=0x%x.\n", hr));

        hr = pEnumAdapters->Reset();
        Assert(SUCCEEDED(hr));
        do
        {
            hr = pEnumAdapters->Next(1, &pNetCfgAdapter, NULL);
            if (hr == S_FALSE)
                break;
            if (hr != S_OK)
                throw RTCError(com::Utf8StrFmt("Failed to get next network adapter, hr=0x%x.\n", hr));
            hr = pNetCfgAdapter->QueryInterface(IID_INetCfgComponentBindings, (PVOID*)&pAdapterBindings);
            if (FAILED(hr))
                throw RTCError(com::Utf8StrFmt("Failed to query INetCfgComponentBindings, hr=0x%x.\n", hr));
            hr = pAdapterBindings->EnumBindingPaths(EBP_ABOVE, &pEnumBp);
            if (FAILED(hr))
                throw RTCError(com::Utf8StrFmt("Failed to enumerate binding paths, hr=0x%x.\n", hr));
            hr = pEnumBp->Reset();
            if (FAILED(hr))
                throw RTCError(com::Utf8StrFmt("Failed to reset enumeration of binding paths (0x%x)\n", hr));
            do
            {
                hr = pEnumBp->Next(1, &pBp, NULL);
                if (hr == S_FALSE)
                    break;
                if (hr != S_OK)
                    throw RTCError(com::Utf8StrFmt("Failed to get next network adapter, hr=0x%x.\n", hr));
                bool fBpEnabled;
                hr =  pBp->IsEnabled();
                if (hr == S_FALSE)
                    fBpEnabled = false;
                else if (hr != S_OK)
                    throw RTCError(com::Utf8StrFmt("Failed to check if bind path is enabled, hr=0x%x.\n", hr));
                else
                    fBpEnabled = true;
                hr = pBp->EnumBindingInterfaces(&pEnumBi);
                if (FAILED(hr))
                    throw RTCError(com::Utf8StrFmt("Failed to enumerate binding interfaces (0x%x)\n", hr));
                hr = pEnumBi->Reset();
                if (FAILED(hr))
                    throw RTCError(com::Utf8StrFmt("Failed to reset enumeration of binding interfaces (0x%x)\n", hr));
                int ident;
                for (ident = 0;; ++ident)
                {
                    hr = pEnumBi->Next(1, &pBi, NULL);
                    if (hr == S_FALSE)
                        break;
                    if (hr != S_OK)
                        throw RTCError(com::Utf8StrFmt("Failed to get next binding interface, hr=0x%x.\n", hr));
                    hr = pBi->GetUpperComponent(&pUpperComponent);
                    if (FAILED(hr))
                        throw RTCError(com::Utf8StrFmt("Failed to get upper component, hr=0x%x.\n", hr));
                    collectNetCfgComponentInfo(ident, fBpEnabled, pUpperComponent);
                    ReleaseAndReset(pUpperComponent);
                    ReleaseAndReset(pBi);
                }
                collectNetCfgComponentInfo(ident, fBpEnabled, pNetCfgAdapter);
                ReleaseAndReset(pEnumBi);
                ReleaseAndReset(pBp);
            } while (true);

            ReleaseAndReset(pEnumBp);
            ReleaseAndReset(pAdapterBindings);
            ReleaseAndReset(pNetCfgAdapter);
        } while (true);
        ReleaseAndReset(pEnumAdapters);
        ReleaseAndReset(pNetCfg);
    }

    catch (RTCError &e)
    {
        ReleaseAndReset(pUpperComponent);
        ReleaseAndReset(pBi);
        ReleaseAndReset(pEnumBi);
        ReleaseAndReset(pBp);
        ReleaseAndReset(pEnumBp);
        ReleaseAndReset(pAdapterBindings);
        ReleaseAndReset(pNetCfgAdapter);
        ReleaseAndReset(pEnumAdapters);
        ReleaseAndReset(pNetCfg);
        RTPrintf("ERROR in osCollect: %s\n", e.what());
        throw;
    }

}


class ErrorHandler
{
public:
    ErrorHandler(const char *pszFunction, int iLine)
        : m_function(pszFunction), m_line(iLine)
    { }

    void handleWinError(DWORD uError, const char *pszMsgFmt, ...)
    {
        if (uError != ERROR_SUCCESS)
        {
            va_list va;
            va_start(va, pszMsgFmt);
            RTCString msgArgs(pszMsgFmt, va);
            va_end(va);

            LPSTR pBuf = NULL;
            FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                           NULL, uError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&pBuf, 0, NULL);
            RTCStringFmt msg("%s at %s(%d): err=%u %s", msgArgs.c_str(), m_function, m_line, uError, pBuf);
            LocalFree(pBuf);
            throw RTCError(msg.c_str());
        }
    }

private:
    const char *m_function;
    int m_line;
};
#define handleWinError ErrorHandler(__FUNCTION__, __LINE__).handleWinError


class BugReportUsbTreeWin : public BugReportStream
{
public:
    BugReportUsbTreeWin();
    virtual ~BugReportUsbTreeWin();
    virtual RTVFSIOSTREAM getStream(void) { enumerate(); return BugReportStream::getStream(); }
private:
    class AutoHandle {
    public:
        AutoHandle(HANDLE h) { m_h = h; }
        ~AutoHandle() { close(); }
        bool isValid() { return m_h != INVALID_HANDLE_VALUE; }
        operator HANDLE() { return m_h; }
        void close(void)  {   if (isValid()) { CloseHandle(m_h); m_h = INVALID_HANDLE_VALUE; }    }
    private:
        HANDLE m_h;
    };
    void enumerate();

    void enumerateController(PSP_DEVINFO_DATA pInfoData, PSP_DEVICE_INTERFACE_DATA pInterfaceData);
    void enumerateHub(RTCString strFullName, RTCString strPrefix);
    void enumeratePorts(HANDLE hHub, unsigned cPorts, RTCString strPrefix);
    PBYTE getDeviceRegistryProperty(HDEVINFO hDev, PSP_DEVINFO_DATA pInfoData, DWORD uProperty,
                                    DWORD uExpectedType, PDWORD puSize);
    RTCString getDeviceRegistryPropertyString(HDEVINFO hDev, PSP_DEVINFO_DATA pInfoData, DWORD uProperty);

    RTCString getDeviceDescByDriverName(RTCString strDrvName);
    RTCString getDriverKeyName(HANDLE hHub, int iPort);
    RTCString getExternalHubName(HANDLE hHub, int iPort);

    HDEVINFO m_hDevInfo;
    PSP_DEVICE_INTERFACE_DETAIL_DATA m_pDetailData;
    HANDLE m_hHostCtrlDev;
};

BugReportUsbTreeWin::BugReportUsbTreeWin() : BugReportStream("HostUsbTree")
{
    m_hDevInfo = INVALID_HANDLE_VALUE;
    m_pDetailData = NULL;
    m_hHostCtrlDev = INVALID_HANDLE_VALUE;
}

BugReportUsbTreeWin::~BugReportUsbTreeWin()
{
    if (m_hHostCtrlDev != INVALID_HANDLE_VALUE)
        CloseHandle(m_hHostCtrlDev);
    if (m_pDetailData)
        RTMemFree(m_pDetailData);
    if (m_hDevInfo != INVALID_HANDLE_VALUE)
        SetupDiDestroyDeviceInfoList(m_hDevInfo);
}


PBYTE BugReportUsbTreeWin::getDeviceRegistryProperty(HDEVINFO hDev,
                                                     PSP_DEVINFO_DATA pInfoData,
                                                     DWORD uProperty,
                                                     DWORD uExpectedType,
                                                     PDWORD puSize)
{
    DWORD uActualType, cbNeeded = 0;
    if (!SetupDiGetDeviceRegistryProperty(hDev, pInfoData, uProperty, &uActualType,
                                          NULL, 0, &cbNeeded)
        && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
    {
        if (GetLastError() == ERROR_INVALID_DATA)
            return NULL;
        handleWinError(GetLastError(), "SetupDiGetDeviceRegistryProperty(0x%x) failed", uProperty);
    }
    if (uExpectedType != REG_NONE && uActualType != uExpectedType)
        throw RTCError(RTCStringFmt("SetupDiGetDeviceRegistryProperty(0x%x) returned type %d instead of %d",
                                    uActualType, uExpectedType).c_str());
    PBYTE pBuffer = (PBYTE)RTMemAlloc(cbNeeded);
    if (!pBuffer)
        throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());
    if (!SetupDiGetDeviceRegistryProperty(hDev, pInfoData, uProperty, NULL,
                                          pBuffer, cbNeeded, &cbNeeded))
    {
        DWORD dwErr = GetLastError();
        RTMemFree(pBuffer);
        pBuffer = NULL;
        handleWinError(dwErr, "SetupDiGetDeviceRegistryProperty(0x%x) failed", uProperty);
    }
    if (puSize)
        *puSize = cbNeeded;

    return pBuffer;
}

RTCString BugReportUsbTreeWin::getDeviceRegistryPropertyString(HDEVINFO hDev, PSP_DEVINFO_DATA pInfoData, DWORD uProperty)
{
    PWSTR pUnicodeString = (PWSTR)getDeviceRegistryProperty(hDev, pInfoData, uProperty, REG_SZ, NULL);

    if (!pUnicodeString)
        return RTCString();

    RTCStringFmt utf8string("%ls", pUnicodeString);
    RTMemFree(pUnicodeString);
    return utf8string;
}


RTCString BugReportUsbTreeWin::getDeviceDescByDriverName(RTCString strDrvName)
{
    DWORD dwErr;
    SP_DEVINFO_DATA devInfoData;
    HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);

    if (hDevInfo == INVALID_HANDLE_VALUE)
        handleWinError(GetLastError(), "SetupDiGetClassDevs failed");

    bool fFound = false;
    devInfoData.cbSize = sizeof(devInfoData);
    for (int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &devInfoData); ++i)
    {
        if (getDeviceRegistryPropertyString(hDevInfo, &devInfoData, SPDRP_DRIVER).equals(strDrvName))
        {
            fFound = true;
            break;
        }
    }
    if (!fFound)
    {
        dwErr = GetLastError();
        SetupDiDestroyDeviceInfoList(hDevInfo);
        handleWinError(dwErr, "SetupDiEnumDeviceInfo failed");
    }

    RTCString strDesc = getDeviceRegistryPropertyString(hDevInfo, &devInfoData, SPDRP_DEVICEDESC);
    SetupDiDestroyDeviceInfoList(hDevInfo);
    return strDesc;
}


RTCString BugReportUsbTreeWin::getDriverKeyName(HANDLE hHub, int iPort)
{
    USB_NODE_CONNECTION_DRIVERKEY_NAME name;
    ULONG cbNeeded = 0;

    name.ConnectionIndex = iPort;
    if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
                         &name, sizeof(name), &name, sizeof(name), &cbNeeded, NULL))
        handleWinError(GetLastError(), "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME) failed");
    cbNeeded = name.ActualLength;
    PUSB_NODE_CONNECTION_DRIVERKEY_NAME pName = (PUSB_NODE_CONNECTION_DRIVERKEY_NAME)RTMemAlloc(cbNeeded);
    if (!pName)
        throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());
    pName->ConnectionIndex = iPort;
    if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
                         pName, cbNeeded, pName, cbNeeded, &cbNeeded, NULL))
    {
        DWORD dwErr = GetLastError();
        RTMemFree(pName);
        handleWinError(dwErr, "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME) failed");
    }
    RTCStringFmt strName("%ls", pName->DriverKeyName);
    RTMemFree(pName);
    return strName;
}


RTCString BugReportUsbTreeWin::getExternalHubName(HANDLE hHub, int iPort)
{
    USB_NODE_CONNECTION_NAME name;
    ULONG cbNeeded = 0;

    name.ConnectionIndex = iPort;
    if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_NAME,
                         &name, sizeof(name), &name, sizeof(name), &cbNeeded, NULL))
        handleWinError(GetLastError(), "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_NAME) failed");
    cbNeeded = name.ActualLength;
    PUSB_NODE_CONNECTION_NAME pName = (PUSB_NODE_CONNECTION_NAME)RTMemAlloc(cbNeeded);
    if (!pName)
        throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());
    pName->ConnectionIndex = iPort;
    if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_NAME,
                         pName, cbNeeded, pName, cbNeeded, &cbNeeded, NULL))
    {
        DWORD dwErr = GetLastError();
        RTMemFree(pName);
        handleWinError(dwErr, "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_NAME) failed");
    }
    RTCStringFmt strName("%ls", pName->NodeName);
    RTMemFree(pName);
    return strName;
}


void BugReportUsbTreeWin::enumeratePorts(HANDLE hHub, unsigned cPorts, RTCString strPrefix)
{
    DWORD cbInfo = sizeof(USB_NODE_CONNECTION_INFORMATION_EX) + 30 * sizeof(USB_PIPE_INFO);
    PUSB_NODE_CONNECTION_INFORMATION_EX pInfo = (PUSB_NODE_CONNECTION_INFORMATION_EX)RTMemAlloc(cbInfo);
    if (!pInfo)
        throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbInfo).c_str());
    for (unsigned i = 1; i <= cPorts; ++i)
    {
        pInfo->ConnectionIndex = i;
        if (!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
                             pInfo, cbInfo, pInfo, cbInfo, &cbInfo, NULL))
        {
            DWORD dwErr = GetLastError();
            RTMemFree(pInfo);
            handleWinError(dwErr, "DeviceIoControl(IOCTL_USB_GET_NODE_CONNECTION_INFORMATION) failed");
        }
        if (pInfo->ConnectionStatus == NoDeviceConnected)
            printf("%s[Port %d]\n", strPrefix.c_str(), i);
        else
        {
            RTCString strName = getDeviceDescByDriverName(getDriverKeyName(hHub, i));
            printf("%s[Port %d] %s\n", strPrefix.c_str(), i, strName.c_str());
            if (pInfo->DeviceIsHub)
                enumerateHub(getExternalHubName(hHub, i), strPrefix + "   ");
        }
    }
    RTMemFree(pInfo);
}

void BugReportUsbTreeWin::enumerateHub(RTCString strFullName, RTCString strPrefix)
{
    AutoHandle hHubDev(CreateFileA(RTCString("\\\\.\\").append(strFullName).c_str(),
                                   GENERIC_WRITE, FILE_SHARE_WRITE,
                                   NULL, OPEN_EXISTING, 0, NULL));
    if (!hHubDev.isValid())
        handleWinError(GetLastError(), "CreateFile(%s) failed", strFullName.c_str());
    ULONG cb;
    USB_NODE_INFORMATION hubInfo;
    if (!DeviceIoControl(hHubDev,
                         IOCTL_USB_GET_NODE_INFORMATION,
                         &hubInfo,
                         sizeof(USB_NODE_INFORMATION),
                         &hubInfo,
                         sizeof(USB_NODE_INFORMATION),
                         &cb,
                         NULL))
        handleWinError(GetLastError(), "DeviceIoControl(IOCTL_USB_GET_NODE_INFORMATION) failed");
    enumeratePorts(hHubDev, hubInfo.u.HubInformation.HubDescriptor.bNumberOfPorts, strPrefix);
}

void BugReportUsbTreeWin::enumerateController(PSP_DEVINFO_DATA pInfoData, PSP_DEVICE_INTERFACE_DATA pInterfaceData)
{
    RT_NOREF(pInterfaceData);
    RTCString strCtrlDesc = getDeviceRegistryPropertyString(m_hDevInfo, pInfoData, SPDRP_DEVICEDESC);
    printf("%s\n", strCtrlDesc.c_str());

    ULONG cbNeeded;
    USB_ROOT_HUB_NAME rootHub;
    /* Find out the name length first */
    if (!DeviceIoControl(m_hHostCtrlDev, IOCTL_USB_GET_ROOT_HUB_NAME, NULL, 0,
                         &rootHub, sizeof(rootHub),
                         &cbNeeded, NULL))
        handleWinError(GetLastError(), "DeviceIoControl(IOCTL_USB_GET_ROOT_HUB_NAME) failed");
    cbNeeded = rootHub.ActualLength;
    PUSB_ROOT_HUB_NAME pUnicodeName = (PUSB_ROOT_HUB_NAME)RTMemAlloc(cbNeeded);
    if (!pUnicodeName)
        throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());

    if (!DeviceIoControl(m_hHostCtrlDev, IOCTL_USB_GET_ROOT_HUB_NAME, NULL, 0,
                         pUnicodeName, cbNeeded,
                         &cbNeeded, NULL))
    {
        DWORD dwErr = GetLastError();
        RTMemFree(pUnicodeName);
        handleWinError(dwErr, "DeviceIoControl(IOCTL_USB_GET_ROOT_HUB_NAME) failed");
    }

    RTCStringFmt strRootHubName("%ls", pUnicodeName->RootHubName);
    RTMemFree(pUnicodeName);
    printf("   Root Hub\n");
    enumerateHub(strRootHubName, "      ");
}

void BugReportUsbTreeWin::enumerate()
{
    m_hDevInfo = SetupDiGetClassDevs((LPGUID)&GUID_DEVINTERFACE_USB_HOST_CONTROLLER, NULL, NULL,
                                     DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (m_hDevInfo == INVALID_HANDLE_VALUE)
        handleWinError(GetLastError(), "SetupDiGetClassDevs(GUID_DEVINTERFACE_USB_HOST_CONTROLLER) failed");

    SP_DEVINFO_DATA deviceInfoData;
    deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    for (int i = 0; SetupDiEnumDeviceInfo(m_hDevInfo, i, &deviceInfoData); ++i)
    {
        if (m_hHostCtrlDev != INVALID_HANDLE_VALUE)
            CloseHandle(m_hHostCtrlDev);
        if (m_pDetailData)
            RTMemFree(m_pDetailData);

        SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
        deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
        if (!SetupDiEnumDeviceInterfaces(m_hDevInfo, 0, (LPGUID)&GUID_DEVINTERFACE_USB_HOST_CONTROLLER,
                                         i, &deviceInterfaceData))
            handleWinError(GetLastError(), "SetupDiEnumDeviceInterfaces(GUID_DEVINTERFACE_USB_HOST_CONTROLLER) failed");

        ULONG cbNeeded = 0;
        if (!SetupDiGetDeviceInterfaceDetail(m_hDevInfo, &deviceInterfaceData, NULL, 0, &cbNeeded, NULL)
            && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
            handleWinError(GetLastError(), "SetupDiGetDeviceInterfaceDetail failed");

        m_pDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)RTMemAlloc(cbNeeded);
        if (!m_pDetailData)
            throw RTCError(RTCStringFmt("Failed to allocate %u bytes", cbNeeded).c_str());

        m_pDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
        if (!SetupDiGetDeviceInterfaceDetail(m_hDevInfo, &deviceInterfaceData, m_pDetailData, cbNeeded, &cbNeeded, NULL))
            handleWinError(GetLastError(), "SetupDiGetDeviceInterfaceDetail failed");

        m_hHostCtrlDev = CreateFile(m_pDetailData->DevicePath, GENERIC_WRITE, FILE_SHARE_WRITE,
                                    NULL, OPEN_EXISTING, 0, NULL);
        if (m_hHostCtrlDev == INVALID_HANDLE_VALUE)
            handleWinError(GetLastError(), "CreateFile(%ls) failed", m_pDetailData);

        enumerateController(&deviceInfoData, &deviceInterfaceData);
    }
}

class BugReportDriversWin : public BugReportStream
{
public:
    BugReportDriversWin();
    virtual ~BugReportDriversWin();
    virtual RTVFSIOSTREAM getStream(void) { enumerateDrivers(); return BugReportStream::getStream(); }
private:
    void enumerateDrivers(void);

    WCHAR  *m_pwszSystemRoot;
    UINT    m_cSystemRoot;
    LPVOID *m_pDrivers;
    DWORD   m_cDrivers;
    LPVOID  m_pVerInfo;
    DWORD   m_cbVerInfo;
};

BugReportDriversWin::BugReportDriversWin() : BugReportStream("DriverVersions")
{
    m_cSystemRoot = MAX_PATH;
    m_pwszSystemRoot = new WCHAR[MAX_PATH];
    m_cDrivers = 1024;
    m_pDrivers = new LPVOID[m_cDrivers];
    m_pVerInfo = NULL;
    m_cbVerInfo = 0;
}

BugReportDriversWin::~BugReportDriversWin()
{
    if (m_pVerInfo)
        RTMemTmpFree(m_pVerInfo);
    delete[] m_pDrivers;
    delete[] m_pwszSystemRoot;
}

void BugReportDriversWin::enumerateDrivers()
{
    UINT cNeeded = GetWindowsDirectory(m_pwszSystemRoot, m_cSystemRoot);
    if (cNeeded > m_cSystemRoot)
    {
        /* Re-allocate and try again */
        m_cSystemRoot = cNeeded;
        delete[] m_pwszSystemRoot;
        m_pwszSystemRoot = new WCHAR[m_cSystemRoot];
        cNeeded = GetWindowsDirectory(m_pwszSystemRoot, m_cSystemRoot);
    }
    if (cNeeded == 0)
        handleWinError(GetLastError(), "GetWindowsDirectory failed");

    DWORD cbNeeded = 0;
    if (    !EnumDeviceDrivers(m_pDrivers, m_cDrivers * sizeof(m_pDrivers[0]), &cbNeeded)
         || cbNeeded > m_cDrivers * sizeof(m_pDrivers[0]))
    {
        /* Re-allocate and try again */
        m_cDrivers = cbNeeded / sizeof(m_pDrivers[0]);
        delete[] m_pDrivers;
        m_pDrivers = new LPVOID[m_cDrivers];
        if (!EnumDeviceDrivers(m_pDrivers, cbNeeded, &cbNeeded))
            handleWinError(GetLastError(), "EnumDeviceDrivers failed (%p, %u)", m_pDrivers, cbNeeded);
    }

    WCHAR wszDriver[1024];
    for (unsigned i = 0; i < m_cDrivers; i++)
    {
        if (GetDeviceDriverBaseName(m_pDrivers[i], wszDriver, RT_ELEMENTS(wszDriver)))
        {
            if (_wcsnicmp(L"vbox", wszDriver, 4))
                continue;
        }
        else
            continue;
        if (GetDeviceDriverFileName(m_pDrivers[i], wszDriver, RT_ELEMENTS(wszDriver)))
        {
            WCHAR wszTmpDrv[1024];
            WCHAR *pwszDrv = wszDriver;
            if (!wcsncmp(L"\\SystemRoot", wszDriver, 11))
            {
                wcsncpy_s(wszTmpDrv, m_pwszSystemRoot, m_cSystemRoot);
                wcsncat_s(wszTmpDrv, wszDriver + 11, RT_ELEMENTS(wszTmpDrv) - m_cSystemRoot);
                pwszDrv = wszTmpDrv;
            }
            else if (!wcsncmp(L"\\??\\", wszDriver, 4))
                pwszDrv = wszDriver + 4;


            /* Allocate a buffer for version info. Reuse if large enough. */
            DWORD cbNewVerInfo = GetFileVersionInfoSize(pwszDrv, NULL);
            if (cbNewVerInfo > m_cbVerInfo)
            {
                if (m_pVerInfo)
                    RTMemTmpFree(m_pVerInfo);
                m_cbVerInfo = cbNewVerInfo;
                m_pVerInfo = RTMemTmpAlloc(m_cbVerInfo);
                if (!m_pVerInfo)
                    throw RTCError(RTCStringFmt("Failed to allocate %u bytes", m_cbVerInfo).c_str());
            }

            if (GetFileVersionInfo(pwszDrv, NULL, m_cbVerInfo, m_pVerInfo))
            {
                UINT   cbSize = 0;
                LPBYTE lpBuffer = NULL;
                if (VerQueryValue(m_pVerInfo, L"\\", (VOID FAR* FAR*)&lpBuffer, &cbSize))
                {
                    if (cbSize)
                    {
                        VS_FIXEDFILEINFO *pFileInfo = (VS_FIXEDFILEINFO *)lpBuffer;
                        if (pFileInfo->dwSignature == 0xfeef04bd)
                        {
                            printf("%ls (Version: %d.%d.%d.%d)\n", pwszDrv,
                                   (pFileInfo->dwFileVersionMS >> 16) & 0xffff,
                                   (pFileInfo->dwFileVersionMS >> 0) & 0xffff,
                                   (pFileInfo->dwFileVersionLS >> 16) & 0xffff,
                                   (pFileInfo->dwFileVersionLS >> 0) & 0xffff);
                        }
                        else
                            printf("%ls - invalid signature\n", pwszDrv);
                    }
                    else
                        printf("%ls - version info size is 0\n", pwszDrv);
                }
                else
                    printf("%ls - failed to query version info size\n", pwszDrv);
            }
            else
                printf("%ls - failed to get version info with 0x%x\n", pwszDrv, GetLastError());
        }
        else
            printf("%ls - GetDeviceDriverFileName failed with 0x%x\n", wszDriver, GetLastError());
    }
}


class BugReportFilterRegistryWin : public BugReportFilter
{
public:
    BugReportFilterRegistryWin() {};
    virtual ~BugReportFilterRegistryWin() {};
    virtual void *apply(void *pvSource, size_t *pcbInOut);
};

void *BugReportFilterRegistryWin::apply(void *pvSource, size_t *pcbInOut)
{
    /*
     * The following implementation is not optimal by any means. It serves to
     * illustrate and test the case when filter's output is longer than its
     * input.
     */
    RT_NOREF(pcbInOut);
    /* Registry export files are encoded in UTF-16 (little endian on Intel x86). */
    void *pvDest = pvSource;
    uint16_t *pwsSource = (uint16_t *)pvSource;
    if (*pwsSource++ == 0xFEFF && *pcbInOut > 48)
    {
        if (!memcmp(pwsSource, L"Windows Registry Editor", 46))
        {
            *pcbInOut += 2;
            pvDest = allocateBuffer(*pcbInOut);
            uint16_t *pwsDest = (uint16_t *)pvDest;
            *pwsDest++ = 0xFEFF;
            *pwsDest++ = '#';
            /* Leave space for 0xFEFF and '#' */
            memcpy(pwsDest, pwsSource, *pcbInOut - 4);
        }
    }
    return pvDest;
}


void createBugReportOsSpecific(BugReport* report, const char *pszHome)
{
    RT_NOREF(pszHome);
    WCHAR szWinDir[MAX_PATH];

    int cbNeeded = GetWindowsDirectory(szWinDir, RT_ELEMENTS(szWinDir));
    if (cbNeeded == 0)
        throw RTCError(RTCStringFmt("Failed to get Windows directory (err=%d)\n", GetLastError()));
    if (cbNeeded > MAX_PATH)
        throw RTCError(RTCStringFmt("Failed to get Windows directory (needed %d-byte buffer)\n", cbNeeded));
    RTCStringFmt WinInfDir("%ls/inf", szWinDir);
    report->addItem(new BugReportFile(PathJoin(WinInfDir.c_str(), "setupapi.app.log"), "setupapi.app.log"));
    report->addItem(new BugReportFile(PathJoin(WinInfDir.c_str(), "setupapi.dev.log"), "setupapi.dev.log"));
    report->addItem(new BugReportNetworkAdaptersWin);
    RTCStringFmt WinSysDir("%ls/System32", szWinDir);
    report->addItem(new BugReportCommand("IpConfig", PathJoin(WinSysDir.c_str(), "ipconfig.exe"), "/all", NULL));
    report->addItem(new BugReportCommand("RouteTable", PathJoin(WinSysDir.c_str(), "netstat.exe"), "-rn", NULL));
    report->addItem(new BugReportCommand("SystemEvents", PathJoin(WinSysDir.c_str(), "wevtutil.exe"),
                                         "qe", "System",
                                         "/q:*[System[Provider[@Name='VBoxUSBMon' or @Name='VBoxNetLwf']]]", NULL));
    report->addItem(new BugReportCommand("UpdateHistory", PathJoin(WinSysDir.c_str(), "wbem/wmic.exe"),
                                         "qfe", "list", "brief", NULL));
    report->addItem(new BugReportCommand("DriverServices", PathJoin(WinSysDir.c_str(), "sc.exe"),
                                         "query", "type=", "driver", "state=", "all", NULL));
    report->addItem(new BugReportCommand("DriverStore", PathJoin(WinSysDir.c_str(), "pnputil.exe"), "-e", NULL));
    report->addItem(new BugReportCommandTemp("RegDevKeys", PathJoin(WinSysDir.c_str(), "reg.exe"), "export",
                                             "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\Root\\NET", NULL),
                    new BugReportFilterRegistryWin());
    report->addItem(new BugReportCommandTemp("RegDrvKeys", PathJoin(WinSysDir.c_str(), "reg.exe"), "export",
        "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", NULL),
                    new BugReportFilterRegistryWin());
    report->addItem(new BugReportCommandTemp("RegNetwork", PathJoin(WinSysDir.c_str(), "reg.exe"), "export",
        "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Network", NULL),
                    new BugReportFilterRegistryWin());
    report->addItem(new BugReportCommandTemp("RegNetFltNobj", PathJoin(WinSysDir.c_str(), "reg.exe"), "export",
        "HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\CLSID\\{f374d1a0-bf08-4bdc-9cb2-c15ddaeef955}", NULL),
                    new BugReportFilterRegistryWin());
    report->addItem(new BugReportUsbTreeWin);
    report->addItem(new BugReportDriversWin);
}