VirtualBoxBase.cpp revision 84029357bc8c5780585a1cd1b40319bbc23d022f
/* $Id$ */
/** @file
*
* VirtualBox COM base classes implementation
*/
/*
* Copyright (C) 2006-2014 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <iprt/semaphore.h>
#include <typeinfo>
#if !defined(VBOX_WITH_XPCOM)
#include <windows.h>
#include <dbghelp.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 "Logging.h"
#include "VBox/com/ErrorInfo.h"
#include "VBox/com/MultiResult.h"
////////////////////////////////////////////////////////////////////////////////
//
// VirtualBoxBase
//
////////////////////////////////////////////////////////////////////////////////
{
mObjectLock = NULL;
}
{
if (mObjectLock)
delete mObjectLock;
}
/**
* 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 */
{
/* lazy initialization */
if (RT_UNLIKELY(!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
{
delete objLock;
}
return objLock;
}
return mObjectLock;
}
/**
* 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 (...)
{
rc = VirtualBox::handleUnexpectedExceptions(this, RT_SRC_POS);
}
* @endcode
*
* @param aThis object where the exception happened
* @param RT_SRC_POS_DECL "RT_SRC_POS" macro instantiation.
* */
/* static */
{
try
{
/* re-throw the current exception */
throw;
}
{
false /* aWarning */,
true /* aLogIt */);
}
{
false /* aWarning */,
true /* aLogIt */);
}
catch (...)
{
false /* aWarning */,
true /* aLogIt */);
}
/* should not get here */
AssertFailed();
return E_FAIL;
}
/**
* 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 */
const char *pcszComponent,
bool aWarning,
bool aLogIt)
{
/* whether multi-error mode is turned on */
if (aLogIt)
LogRel(("%s [COM]: aRC=%Rhrc (%#08x) aIID={%RTuuid} aComponent={%s} aText={%s}, preserve=%RTbool\n",
&aIID,
preserve));
/* these are mandatory, others -- not */
E_FAIL);
/* reset the error severity bit if it's a warning */
if (aWarning)
aResultCode &= ~0x80000000;
{
/* Some default info */
switch (aResultCode)
{
case VBOX_E_OBJECT_NOT_FOUND: aText = "Object corresponding to the supplied arguments does not exist"; break;
case VBOX_E_INVALID_VM_STATE: aText = "Current virtual machine state prevents the operation"; break;
default: aText = "Unknown error"; break;
}
}
do
{
#if !defined(VBOX_WITH_XPCOM)
if (preserve)
{
/* get the current error info if any */
{
/* create a IVirtualBoxErrorInfo wrapper for the native
* IErrorInfo object */
{
}
}
}
/* On failure, curInfo will stay null */
/* set the current error info and preserve the previous one if any */
#else // !defined(VBOX_WITH_XPCOM)
if (NS_SUCCEEDED(rc))
{
if (preserve)
{
/* get the current error info if any */
{
/* create a IVirtualBoxErrorInfo wrapper for the native
* nsIException object */
{
}
}
}
/* On failure, curInfo will stay null */
/* set the current error info and preserve the previous one if any */
}
else if (rc == 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).
*/
LogWarningFunc(("Will not set an exception because nsIExceptionService is not available "
"(NS_ERROR_UNEXPECTED). XPCOM is being shutdown?\n"));
}
#endif // !defined(VBOX_WITH_XPCOM)
}
while (0);
}
/**
* 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
*/
{
return setErrorInternal(aResultCode,
this->getClassIID(),
this->getComponentName(),
"",
false /* aWarning */,
true /* aLogIt */);
}
/**
* 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
*/
{
this->getClassIID(),
this->getComponentName(),
false /* aWarning */,
true /* aLogIt */);
return rc;
}
/**
* 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
*/
{
/* whether multi-error mode is turned on */
do
{
#if !defined(VBOX_WITH_XPCOM)
if (preserve)
{
/* get the current error info if any */
{
/* create a IVirtualBoxErrorInfo wrapper for the native
* IErrorInfo object */
{
}
}
}
/* On failure, curInfo will stay null */
/* set the current error info and preserve the previous one if any */
#else // !defined(VBOX_WITH_XPCOM)
if (NS_SUCCEEDED(rc))
{
if (preserve)
{
/* get the current error info if any */
{
/* create a IVirtualBoxErrorInfo wrapper for the native
* nsIException object */
{
}
}
}
/* On failure, curInfo will stay null */
/* set the current error info and preserve the previous one if any */
}
else if (rc == 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).
*/
LogWarningFunc(("Will not set an exception because nsIExceptionService is not available "
"(NS_ERROR_UNEXPECTED). XPCOM is being shutdown?\n"));
}
#endif // !defined(VBOX_WITH_XPCOM)
}
while (0);
}
/**
* Like setError(), but sets the "warning" bit in the call to setErrorInternal().
* @param aResultCode
* @param pcsz
* @return
*/
{
this->getClassIID(),
this->getComponentName(),
true /* aWarning */,
true /* aLogIt */);
return rc;
}
/**
* Like setError(), but disables the "log" flag in the call to setErrorInternal().
* @param aResultCode
* @param pcsz
* @return
*/
{
this->getClassIID(),
this->getComponentName(),
false /* aWarning */,
false /* aLogIt */);
return rc;
}
/**
* Clear the current error information.
*/
/*static*/
void VirtualBoxBase::clearError(void)
{
#if !defined(VBOX_WITH_XPCOM)
::SetErrorInfo(0, NULL);
#else
if (NS_SUCCEEDED(rc))
{
}
#endif
}
////////////////////////////////////////////////////////////////////////////////
//
// MultiResult methods
//
////////////////////////////////////////////////////////////////////////////////
/*static*/
void MultiResult::incCounter()
{
{
sCounter = RTTlsAlloc();
}
++counter;
}
/*static*/
void MultiResult::decCounter()
{
AssertReturnVoid(counter != 0);
--counter;
}
/*static*/
bool MultiResult::isMultiEnabled()
{
return false;
}