summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-all/VirtualBoxBase.cpp
blob: 0fa8d8d367b5f5c587d376ae40390aa69dff98d5 (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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/* $Id: VirtualBoxBase.cpp $ */
/** @file
 * VirtualBox COM base classes implementation
 */

/*
 * 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
 */

#define LOG_GROUP LOG_GROUP_MAIN
#include <iprt/semaphore.h>
#include <iprt/asm.h>
#include <iprt/cpp/exception.h>

#include <typeinfo>

#if !defined(VBOX_WITH_XPCOM)
# include <iprt/win/windows.h>
#else /* !defined(VBOX_WITH_XPCOM) */
/// @todo remove when VirtualBoxErrorInfo goes away from here
# include <nsIServiceManager.h>
# include <nsIExceptionService.h>
#endif /* !defined(VBOX_WITH_XPCOM) */

#include "VirtualBoxBase.h"
#include "AutoCaller.h"
#include "VirtualBoxErrorInfoImpl.h"
#include "VirtualBoxTranslator.h"
#include "Global.h"
#include "LoggingNew.h"

#include "VBox/com/ErrorInfo.h"
#include "VBox/com/MultiResult.h"

////////////////////////////////////////////////////////////////////////////////
//
// VirtualBoxBase
//
////////////////////////////////////////////////////////////////////////////////

CLASSFACTORY_STAT g_aClassFactoryStats[CLASSFACTORYSTATS_MAX] =
{
    { "--- totals ---", 0 },
    { NULL, 0 }
};

RWLockHandle *g_pClassFactoryStatsLock = NULL;


VirtualBoxBase::VirtualBoxBase() :
    mState(this),
    iFactoryStat(~0U)
{
    mObjectLock = NULL;

    if (!g_pClassFactoryStatsLock)
    {
        RWLockHandle *lock = new RWLockHandle(LOCKCLASS_OBJECTSTATE);
        if (!ASMAtomicCmpXchgPtr(&g_pClassFactoryStatsLock, lock, NULL))
            delete lock;
    }
    Assert(g_pClassFactoryStatsLock);
}

VirtualBoxBase::~VirtualBoxBase()
{
    Assert(iFactoryStat == ~0U);
    if (mObjectLock)
        delete mObjectLock;
}

HRESULT VirtualBoxBase::BaseFinalConstruct()
{
    Assert(iFactoryStat == ~0U);
    if (g_pClassFactoryStatsLock)
    {
        AutoWriteLock alock(g_pClassFactoryStatsLock COMMA_LOCKVAL_SRC_POS);
        g_aClassFactoryStats[0].current++;
        g_aClassFactoryStats[0].overall++;
        const char *pszName = getComponentName();
        uint32_t i = 1;
        while (i < CLASSFACTORYSTATS_MAX && g_aClassFactoryStats[i].psz)
        {
            if (g_aClassFactoryStats[i].psz == pszName)
                break;
            i++;
        }
        if (i < CLASSFACTORYSTATS_MAX)
        {
            if (!g_aClassFactoryStats[i].psz)
            {
                g_aClassFactoryStats[i].psz = pszName;
                g_aClassFactoryStats[i].current = 0;
                g_aClassFactoryStats[i].overall = 0;
            }
            iFactoryStat = i;
            g_aClassFactoryStats[i].current++;
            g_aClassFactoryStats[i].overall++;
        }
        else
            AssertMsg(i < CLASSFACTORYSTATS_MAX, ("%u exhausts size of factory housekeeping array\n", i));
    }
    else
        Assert(g_pClassFactoryStatsLock);

#ifdef RT_OS_WINDOWS
    return CoCreateFreeThreadedMarshaler(this, //GetControllingUnknown(),
                                         m_pUnkMarshaler.asOutParam());
#else
    return S_OK;
#endif
}

void VirtualBoxBase::BaseFinalRelease()
{
    if (g_pClassFactoryStatsLock)
    {
        AutoWriteLock alock(g_pClassFactoryStatsLock COMMA_LOCKVAL_SRC_POS);
        g_aClassFactoryStats[0].current--;
        const char *pszName = getComponentName();
        if (iFactoryStat < CLASSFACTORYSTATS_MAX)
        {
            if (g_aClassFactoryStats[iFactoryStat].psz == pszName)
            {
                g_aClassFactoryStats[iFactoryStat].current--;
                iFactoryStat = ~0U;
            }
            else
                AssertMsgFailed(("could not find factory housekeeping array entry for %s (index %u contains %s)\n", pszName, iFactoryStat, g_aClassFactoryStats[iFactoryStat].psz));
        }
        else
            AssertMsgFailed(("factory housekeeping array corruption, index %u is too large\n", iFactoryStat));
    }
    else
        Assert(g_pClassFactoryStatsLock);

#ifdef RT_OS_WINDOWS
     m_pUnkMarshaler.setNull();
#endif
}

void APIDumpComponentFactoryStats()
{
    if (g_pClassFactoryStatsLock)
    {
        AutoReadLock alock(g_pClassFactoryStatsLock COMMA_LOCKVAL_SRC_POS);
        for (uint32_t i = 0; i < CLASSFACTORYSTATS_MAX && g_aClassFactoryStats[i].psz; i++)
            LogRel(("CFS: component %-30s current %-10u total %-10u\n",
                    g_aClassFactoryStats[i].psz, g_aClassFactoryStats[i].current,
                    g_aClassFactoryStats[i].overall));
    }
    else
        Assert(g_pClassFactoryStatsLock);
}

/**
 * This virtual method returns an RWLockHandle that can be used to
 * protect instance data. This RWLockHandle is generally referred to
 * as the "object lock"; its locking class (for lock order validation)
 * must be returned by another virtual method, getLockingClass(), which
 * by default returns LOCKCLASS_OTHEROBJECT but is overridden by several
 * subclasses such as VirtualBox, Host, Machine and others.
 *
 * On the first call this method lazily creates the RWLockHandle.
 *
 * @return
 */
/* virtual */
RWLockHandle *VirtualBoxBase::lockHandle() const
{
    /* lazy initialization */
    if (RT_LIKELY(mObjectLock))
        return mObjectLock;

    AssertCompile(sizeof(RWLockHandle *) == sizeof(void *));

    // getLockingClass() is overridden by many subclasses to return
    // one of the locking classes listed at the top of AutoLock.h
    RWLockHandle *objLock = new RWLockHandle(getLockingClass());
    if (!ASMAtomicCmpXchgPtr(&mObjectLock, objLock, NULL))
    {
        delete objLock;
        objLock = ASMAtomicReadPtrT(&mObjectLock, RWLockHandle *);
    }
    return objLock;
}

/**
 * Handles unexpected exceptions by turning them into COM errors in release
 * builds or by hitting a breakpoint in the release builds.
 *
 * Usage pattern:
 * @code
        try
        {
            // ...
        }
        catch (LaLalA)
        {
            // ...
        }
        catch (...)
        {
            hrc = VirtualBox::handleUnexpectedExceptions(this, RT_SRC_POS);
        }
 * @endcode
 *
 * @param aThis     object where the exception happened
 * @param SRC_POS   "RT_SRC_POS" macro instantiation.
 *  */
/* static */
HRESULT VirtualBoxBase::handleUnexpectedExceptions(VirtualBoxBase *const aThis, RT_SRC_POS_DECL)
{
    try
    {
        /* re-throw the current exception */
        throw;
    }
    catch (const RTCError &err)      // includes all XML exceptions
    {
        return setErrorInternalF(E_FAIL, aThis->getClassIID(), aThis->getComponentName(),
                                 false /* aWarning */,
                                 true /* aLogIt */,
                                 0 /* aResultDetail */,
                                 "%s.\n%s[%d] (%s)",
                                 err.what(), pszFile, iLine, pszFunction);
    }
    catch (const std::exception &err)
    {
        return setErrorInternalF(E_FAIL, aThis->getClassIID(), aThis->getComponentName(),
                                 false /* aWarning */,
                                 true /* aLogIt */,
                                 0 /* aResultDetail */,
                                 tr("Unexpected exception: %s [%s]\n%s[%d] (%s)"),
                                 err.what(), typeid(err).name(), pszFile, iLine, pszFunction);
    }
    catch (...)
    {
        return setErrorInternalF(E_FAIL, aThis->getClassIID(), aThis->getComponentName(),
                                 false /* aWarning */,
                                 true /* aLogIt */,
                                 0 /* aResultDetail */,
                                 tr("Unknown exception\n%s[%d] (%s)"),
                                 pszFile, iLine, pszFunction);
    }

#ifndef _MSC_VER /* (unreachable) */
    /* should not get here */
    AssertFailed();
    return E_FAIL;
#endif
}


/**
 *  Sets error info for the current thread. This is an internal function that
 *  gets eventually called by all public variants.  If @a aWarning is
 *  @c true, then the highest (31) bit in the @a aResultCode value which
 *  indicates the error severity is reset to zero to make sure the receiver will
 *  recognize that the created error info object represents a warning rather
 *  than an error.
 */
/* static */
HRESULT VirtualBoxBase::setErrorInternalF(HRESULT aResultCode,
                                          const GUID &aIID,
                                          const char *aComponent,
                                          bool aWarning,
                                          bool aLogIt,
                                          LONG aResultDetail,
                                          const char *aText, ...)
{
    va_list va;
    va_start(va, aText);
    HRESULT hres = setErrorInternalV(aResultCode, aIID, aComponent, aText, va,
                                     aWarning, aLogIt, aResultDetail);
    va_end(va);
    return hres;
}

/**
 *  Sets error info for the current thread. This is an internal function that
 *  gets eventually called by all public variants.  If @a aWarning is
 *  @c true, then the highest (31) bit in the @a aResultCode value which
 *  indicates the error severity is reset to zero to make sure the receiver will
 *  recognize that the created error info object represents a warning rather
 *  than an error.
 */
/* static */
HRESULT VirtualBoxBase::setErrorInternalV(HRESULT aResultCode,
                                          const GUID &aIID,
                                          const char *aComponent,
                                          const char *aText,
                                          va_list aArgs,
                                          bool aWarning,
                                          bool aLogIt,
                                          LONG aResultDetail /* = 0*/)
{
    /* whether multi-error mode is turned on */
    bool preserve = MultiResult::isMultiEnabled();

    com::Utf8Str strText;
    if (aLogIt)
    {
#ifdef VBOX_WITH_MAIN_NLS
        strText = VirtualBoxTranslator::trSource(aText);
#else
        strText = aText;
#endif
        va_list va2;
        va_copy(va2, aArgs);
        LogRel(("%s [COM]: aRC=%Rhrc (%#08x) aIID={%RTuuid} aComponent={%s} aText={%N}, preserve=%RTbool aResultDetail=%d\n",
                aWarning ? "WARNING" : "ERROR",
                aResultCode,
                aResultCode,
                &aIID,
                aComponent,
                strText.c_str(),
                &va2,
                preserve,
                aResultDetail));
        va_end(va2);
    }

    /* these are mandatory, others -- not */
    AssertReturn((!aWarning && FAILED(aResultCode)) ||
                  (aWarning && aResultCode != S_OK),
                  E_FAIL);

    /* reset the error severity bit if it's a warning */
    if (aWarning)
        aResultCode &= ~0x80000000;

    HRESULT hrc = S_OK;

    if (aText == NULL || aText[0] == '\0')
    {
        /* Some default info */
        switch (aResultCode)
        {
            case E_INVALIDARG:                 strText = tr("A parameter has an invalid value"); break;
            case E_POINTER:                    strText = tr("A parameter is an invalid pointer"); break;
            case E_UNEXPECTED:                 strText = tr("The result of the operation is unexpected"); break;
            case E_ACCESSDENIED:               strText = tr("The access to an object is not allowed"); break;
            case E_OUTOFMEMORY:                strText = tr("The allocation of new memory failed"); break;
            case E_NOTIMPL:                    strText = tr("The requested operation is not implemented"); break;
            case E_NOINTERFACE:                strText = tr("The requested interface is not implemented"); break;
            case E_FAIL:                       strText = tr("A general error occurred"); break;
            case E_ABORT:                      strText = tr("The operation was canceled"); break;
            case VBOX_E_OBJECT_NOT_FOUND:      strText = tr("Object corresponding to the supplied arguments does not exist"); break;
            case VBOX_E_INVALID_VM_STATE:      strText = tr("Current virtual machine state prevents the operation"); break;
            case VBOX_E_VM_ERROR:              strText = tr("Virtual machine error occurred attempting the operation"); break;
            case VBOX_E_FILE_ERROR:            strText = tr("File not accessible or erroneous file contents"); break;
            case VBOX_E_IPRT_ERROR:            strText = tr("Runtime subsystem error"); break;
            case VBOX_E_PDM_ERROR:             strText = tr("Pluggable Device Manager error"); break;
            case VBOX_E_INVALID_OBJECT_STATE:  strText = tr("Current object state prohibits operation"); break;
            case VBOX_E_HOST_ERROR:            strText = tr("Host operating system related error"); break;
            case VBOX_E_NOT_SUPPORTED:         strText = tr("Requested operation is not supported"); break;
            case VBOX_E_XML_ERROR:             strText = tr("Invalid XML found"); break;
            case VBOX_E_INVALID_SESSION_STATE: strText = tr("Current session state prohibits operation"); break;
            case VBOX_E_OBJECT_IN_USE:         strText = tr("Object being in use prohibits operation"); break;
            case VBOX_E_PASSWORD_INCORRECT:    strText = tr("Incorrect password provided"); break;
            default:                           strText = tr("Unknown error"); break;
        }
    }
    else
    {
        va_list va2;
        va_copy(va2, aArgs);
        strText = com::Utf8StrFmt("%N", aText, &va2);
        va_end(va2);
    }

    do
    {
        ComObjPtr<VirtualBoxErrorInfo> info;
        hrc = info.createObject();
        if (FAILED(hrc)) break;

#if !defined(VBOX_WITH_XPCOM)

        ComPtr<IVirtualBoxErrorInfo> curInfo;
        if (preserve)
        {
            /* get the current error info if any */
            ComPtr<IErrorInfo> err;
            hrc = ::GetErrorInfo(0, err.asOutParam());
            if (FAILED(hrc)) break;
            hrc = err.queryInterfaceTo(curInfo.asOutParam());
            if (FAILED(hrc))
            {
                /* create a IVirtualBoxErrorInfo wrapper for the native
                 * IErrorInfo object */
                ComObjPtr<VirtualBoxErrorInfo> wrapper;
                hrc = wrapper.createObject();
                if (SUCCEEDED(hrc))
                {
                    hrc = wrapper->init(err);
                    if (SUCCEEDED(hrc))
                        curInfo = wrapper;
                }
            }
        }
        /* On failure, curInfo will stay null */
        Assert(SUCCEEDED(hrc) || curInfo.isNull());

        /* set the current error info and preserve the previous one if any */
        hrc = info->initEx(aResultCode, aResultDetail, aIID, aComponent, strText, curInfo);
        if (FAILED(hrc)) break;

        ComPtr<IErrorInfo> err;
        hrc = info.queryInterfaceTo(err.asOutParam());
        if (SUCCEEDED(hrc))
            hrc = ::SetErrorInfo(0, err);

#else // !defined(VBOX_WITH_XPCOM)

        nsCOMPtr <nsIExceptionService> es;
        es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &hrc);
        if (NS_SUCCEEDED(hrc))
        {
            nsCOMPtr <nsIExceptionManager> em;
            hrc = es->GetCurrentExceptionManager(getter_AddRefs(em));
            if (FAILED(hrc)) break;

            ComPtr<IVirtualBoxErrorInfo> curInfo;
            if (preserve)
            {
                /* get the current error info if any */
                ComPtr<nsIException> ex;
                hrc = em->GetCurrentException(ex.asOutParam());
                if (FAILED(hrc)) break;
                hrc = ex.queryInterfaceTo(curInfo.asOutParam());
                if (FAILED(hrc))
                {
                    /* create a IVirtualBoxErrorInfo wrapper for the native
                     * nsIException object */
                    ComObjPtr<VirtualBoxErrorInfo> wrapper;
                    hrc = wrapper.createObject();
                    if (SUCCEEDED(hrc))
                    {
                        hrc = wrapper->init(ex);
                        if (SUCCEEDED(hrc))
                            curInfo = wrapper;
                    }
                }
            }
            /* On failure, curInfo will stay null */
            Assert(SUCCEEDED(hrc) || curInfo.isNull());

            /* set the current error info and preserve the previous one if any */
            hrc = info->initEx(aResultCode, aResultDetail, aIID, aComponent, Bstr(strText), curInfo);
            if (FAILED(hrc)) break;

            ComPtr<nsIException> ex;
            hrc = info.queryInterfaceTo(ex.asOutParam());
            if (SUCCEEDED(hrc))
                hrc = em->SetCurrentException(ex);
        }
        else if (hrc == NS_ERROR_UNEXPECTED)
        {
            /*
             *  It is possible that setError() is being called by the object
             *  after the XPCOM shutdown sequence has been initiated
             *  (for example, when XPCOM releases all instances it internally
             *  references, which can cause object's FinalConstruct() and then
             *  uninit()). In this case, do_GetService() above will return
             *  NS_ERROR_UNEXPECTED and it doesn't actually make sense to
             *  set the exception (nobody will be able to read it).
             */
            Log1WarningFunc(("Will not set an exception because nsIExceptionService is not available (NS_ERROR_UNEXPECTED). XPCOM is being shutdown?\n"));
            hrc = NS_OK;
        }

#endif // !defined(VBOX_WITH_XPCOM)
    }
    while (0);

    AssertComRC(hrc);

    return SUCCEEDED(hrc) ? aResultCode : hrc;
}

/**
 * Shortcut instance method to calling the static setErrorInternal with the
 * class interface ID and component name inserted correctly. This uses the
 * virtual getClassIID() and getComponentName() methods which are automatically
 * defined by the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro.
 * @param   aResultCode
 * @return
 */
HRESULT VirtualBoxBase::setError(HRESULT aResultCode)
{
    return setErrorInternalF(aResultCode,
                             this->getClassIID(),
                             this->getComponentName(),
                             false /* aWarning */,
                             true /* aLogIt */,
                             0 /* aResultDetail */,
                             NULL);
}

/**
 * Shortcut instance method to calling the static setErrorInternal with the
 * class interface ID and component name inserted correctly. This uses the
 * virtual getClassIID() and getComponentName() methods which are automatically
 * defined by the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro.
 * @param   aResultCode
 * @param   pcsz
 * @return
 */
HRESULT VirtualBoxBase::setError(HRESULT aResultCode, const char *pcsz, ...)
{
    va_list args;
    va_start(args, pcsz);
    HRESULT hrc = setErrorInternalV(aResultCode,
                                    this->getClassIID(),
                                    this->getComponentName(),
                                    pcsz, args,
                                    false /* aWarning */,
                                    true /* aLogIt */);
    va_end(args);
    return hrc;
}

/**
 * Shortcut instance method to calling the static setErrorInternal with the
 * class interface ID and component name inserted correctly. This uses the
 * virtual getClassIID() and getComponentName() methods which are automatically
 * defined by the VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT macro.
 * @param   ei
 * @return
 */
HRESULT VirtualBoxBase::setError(const com::ErrorInfo &ei)
{
    /* whether multi-error mode is turned on */
    bool preserve = MultiResult::isMultiEnabled();

    HRESULT hrc = S_OK;

    do
    {
        ComObjPtr<VirtualBoxErrorInfo> info;
        hrc = info.createObject();
        if (FAILED(hrc)) break;

#if !defined(VBOX_WITH_XPCOM)

        ComPtr<IVirtualBoxErrorInfo> curInfo;
        if (preserve)
        {
            /* get the current error info if any */
            ComPtr<IErrorInfo> err;
            hrc = ::GetErrorInfo(0, err.asOutParam());
            if (FAILED(hrc)) break;
            hrc = err.queryInterfaceTo(curInfo.asOutParam());
            if (FAILED(hrc))
            {
                /* create a IVirtualBoxErrorInfo wrapper for the native
                 * IErrorInfo object */
                ComObjPtr<VirtualBoxErrorInfo> wrapper;
                hrc = wrapper.createObject();
                if (SUCCEEDED(hrc))
                {
                    hrc = wrapper->init(err);
                    if (SUCCEEDED(hrc))
                        curInfo = wrapper;
                }
            }
        }
        /* On failure, curInfo will stay null */
        Assert(SUCCEEDED(hrc) || curInfo.isNull());

        /* set the current error info and preserve the previous one if any */
        hrc = info->init(ei, curInfo);
        if (FAILED(hrc)) break;

        ComPtr<IErrorInfo> err;
        hrc = info.queryInterfaceTo(err.asOutParam());
        if (SUCCEEDED(hrc))
            hrc = ::SetErrorInfo(0, err);

#else // !defined(VBOX_WITH_XPCOM)

        nsCOMPtr <nsIExceptionService> es;
        es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &hrc);
        if (NS_SUCCEEDED(hrc))
        {
            nsCOMPtr <nsIExceptionManager> em;
            hrc = es->GetCurrentExceptionManager(getter_AddRefs(em));
            if (FAILED(hrc)) break;

            ComPtr<IVirtualBoxErrorInfo> curInfo;
            if (preserve)
            {
                /* get the current error info if any */
                ComPtr<nsIException> ex;
                hrc = em->GetCurrentException(ex.asOutParam());
                if (FAILED(hrc)) break;
                hrc = ex.queryInterfaceTo(curInfo.asOutParam());
                if (FAILED(hrc))
                {
                    /* create a IVirtualBoxErrorInfo wrapper for the native
                     * nsIException object */
                    ComObjPtr<VirtualBoxErrorInfo> wrapper;
                    hrc = wrapper.createObject();
                    if (SUCCEEDED(hrc))
                    {
                        hrc = wrapper->init(ex);
                        if (SUCCEEDED(hrc))
                            curInfo = wrapper;
                    }
                }
            }
            /* On failure, curInfo will stay null */
            Assert(SUCCEEDED(hrc) || curInfo.isNull());

            /* set the current error info and preserve the previous one if any */
            hrc = info->init(ei, curInfo);
            if (FAILED(hrc)) break;

            ComPtr<nsIException> ex;
            hrc = info.queryInterfaceTo(ex.asOutParam());
            if (SUCCEEDED(hrc))
                hrc = em->SetCurrentException(ex);
        }
        else if (hrc == NS_ERROR_UNEXPECTED)
        {
            /*
             *  It is possible that setError() is being called by the object
             *  after the XPCOM shutdown sequence has been initiated
             *  (for example, when XPCOM releases all instances it internally
             *  references, which can cause object's FinalConstruct() and then
             *  uninit()). In this case, do_GetService() above will return
             *  NS_ERROR_UNEXPECTED and it doesn't actually make sense to
             *  set the exception (nobody will be able to read it).
             */
            Log1WarningFunc(("Will not set an exception because nsIExceptionService is not available (NS_ERROR_UNEXPECTED). XPCOM is being shutdown?\n"));
            hrc = NS_OK;
        }

#endif // !defined(VBOX_WITH_XPCOM)
    }
    while (0);

    AssertComRC(hrc);

    return SUCCEEDED(hrc) ? ei.getResultCode() : hrc;
}

/**
 * Converts the VBox status code a COM one and sets the error info.
 *
 * The VBox status code is made available to the API user via
 * IVirtualBoxErrorInfo::resultDetail attribute.
 *
 * @param   vrc             The VBox status code.
 * @return  COM status code appropriate for @a vrc.
 *
 * @sa      VirtualBoxBase::setError(HRESULT)
 */
HRESULT VirtualBoxBase::setErrorVrc(int vrc)
{
    return setErrorInternalF(Global::vboxStatusCodeToCOM(vrc),
                             this->getClassIID(),
                             this->getComponentName(),
                             false /* aWarning */,
                             true /* aLogIt */,
                             vrc /* aResultDetail */,
                             Utf8StrFmt("%Rrc", vrc).c_str());
}

/**
 * Converts the VBox status code a COM one and sets the error info.
 *
 * @param   vrc             The VBox status code.
 * @param   pcszMsgFmt      Error message format string.
 * @param   va_args         Error message format string.
 * @return  COM status code appropriate for @a vrc.
 *
 * @sa      VirtualBoxBase::setError(HRESULT, const char *, ...)
 */
HRESULT VirtualBoxBase::setErrorVrcV(int vrc, const char *pcszMsgFmt, va_list va_args)
{
    return setErrorInternalV(Global::vboxStatusCodeToCOM(vrc),
                             this->getClassIID(),
                             this->getComponentName(),
                             pcszMsgFmt, va_args,
                             false /* aWarning */,
                             true /* aLogIt */,
                             vrc /* aResultDetail */);
}

/**
 * Converts the VBox status code a COM one and sets the error info.
 *
 * @param   vrc             The VBox status code.
 * @param   pcszMsgFmt      Error message format string.
 * @param   ...             Argument specified in the @a pcszMsgFmt
 * @return  COM status code appropriate for @a vrc.
 *
 * @sa      VirtualBoxBase::setError(HRESULT, const char *, ...)
 */
HRESULT VirtualBoxBase::setErrorVrc(int vrc, const char *pcszMsgFmt, ...)
{
    va_list va;
    va_start(va, pcszMsgFmt);
    HRESULT hrc = setErrorVrcV(vrc, pcszMsgFmt, va);
    va_end(va);
    return hrc;
}

/**
 * Sets error info with both a COM status and an VBox status code.
 *
 * The VBox status code is made available to the API user via
 * IVirtualBoxErrorInfo::resultDetail attribute.
 *
 * @param   hrc             The COM status code to return.
 * @param   vrc             The VBox status code.
 * @return  Most likely @a hrc, see setErrorInternal.
 *
 * @sa      VirtualBoxBase::setError(HRESULT)
 */
HRESULT VirtualBoxBase::setErrorBoth(HRESULT hrc, int vrc)
{
    return setErrorInternalF(hrc,
                             this->getClassIID(),
                             this->getComponentName(),
                             false /* aWarning */,
                             true /* aLogIt */,
                             vrc /* aResultDetail */,
                             Utf8StrFmt("%Rrc", vrc).c_str());
}

/**
 * Sets error info with a message and both a COM status and an VBox status code.
 *
 * The VBox status code is made available to the API user via
 * IVirtualBoxErrorInfo::resultDetail attribute.
 *
 * @param   hrc             The COM status code to return.
 * @param   vrc             The VBox status code.
 * @param   pcszMsgFmt      Error message format string.
 * @param   ...             Argument specified in the @a pcszMsgFmt
 * @return  Most likely @a hrc, see setErrorInternal.
 *
 * @sa      VirtualBoxBase::setError(HRESULT, const char *, ...)
 */
HRESULT VirtualBoxBase::setErrorBoth(HRESULT hrc, int vrc, const char *pcszMsgFmt, ...)
{
    va_list va;
    va_start(va, pcszMsgFmt);
    hrc = setErrorInternalV(hrc,
                            this->getClassIID(),
                            this->getComponentName(),
                            pcszMsgFmt, va,
                            false /* aWarning */,
                            true /* aLogIt */,
                            vrc /* aResultDetail */);
    va_end(va);
    return hrc;
}

/**
 * Like setError(), but sets the "warning" bit in the call to setErrorInternal().
 * @param aResultCode
 * @param pcsz
 * @return
 */
HRESULT VirtualBoxBase::setWarning(HRESULT aResultCode, const char *pcsz, ...)
{
    va_list args;
    va_start(args, pcsz);
    HRESULT hrc = setErrorInternalV(aResultCode,
                                    this->getClassIID(),
                                    this->getComponentName(),
                                    pcsz, args,
                                    true /* aWarning */,
                                    true /* aLogIt */);
    va_end(args);
    return hrc;
}

/**
 * Like setError(), but disables the "log" flag in the call to setErrorInternal().
 * @param aResultCode
 * @param pcsz
 * @return
 */
HRESULT VirtualBoxBase::setErrorNoLog(HRESULT aResultCode, const char *pcsz, ...)
{
    va_list args;
    va_start(args, pcsz);
    HRESULT hrc = setErrorInternalV(aResultCode,
                                    this->getClassIID(),
                                    this->getComponentName(),
                                    pcsz, args,
                                    false /* aWarning */,
                                    false /* aLogIt */);
    va_end(args);
    return hrc;
}

/**
 * Clear the current error information.
 */
/*static*/
void VirtualBoxBase::clearError(void)
{
#if !defined(VBOX_WITH_XPCOM)
    ::SetErrorInfo(0, NULL);
#else
    HRESULT hrc = S_OK;
    nsCOMPtr <nsIExceptionService> es;
    es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &hrc);
    if (NS_SUCCEEDED(hrc))
    {
        nsCOMPtr <nsIExceptionManager> em;
        hrc = es->GetCurrentExceptionManager(getter_AddRefs(em));
        if (SUCCEEDED(hrc))
            em->SetCurrentException(NULL);
    }
#endif
}


////////////////////////////////////////////////////////////////////////////////
//
// MultiResult methods
//
////////////////////////////////////////////////////////////////////////////////

RTTLS MultiResult::sCounter = NIL_RTTLS;

/*static*/
void MultiResult::incCounter()
{
    if (sCounter == NIL_RTTLS)
    {
        sCounter = RTTlsAlloc();
        AssertReturnVoid(sCounter != NIL_RTTLS);
    }

    uintptr_t counter = (uintptr_t)RTTlsGet(sCounter);
    ++counter;
    RTTlsSet(sCounter, (void*)counter);
}

/*static*/
void MultiResult::decCounter()
{
    uintptr_t counter = (uintptr_t)RTTlsGet(sCounter);
    AssertReturnVoid(counter != 0);
    --counter;
    RTTlsSet(sCounter, (void*)counter);
}

/*static*/
bool MultiResult::isMultiEnabled()
{
    if (sCounter == NIL_RTTLS)
       return false;

    return ((uintptr_t)RTTlsGet(MultiResult::sCounter)) > 0;
}