assert.cpp revision 94d4a89729276abd319c0db2f5e9fb4a17f69e5f
2N/A/* $Id$ */
2N/A/** @file
2N/A * IPRT - Assertion Workers.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (C) 2006-2007 Sun Microsystems, Inc.
2N/A *
2N/A * This file is part of VirtualBox Open Source Edition (OSE), as
2N/A * available from http://www.virtualbox.org. This file is free software;
2N/A * you can redistribute it and/or modify it under the terms of the GNU
2N/A * General Public License (GPL) as published by the Free Software
2N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
2N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2N/A *
2N/A * The contents of this file may alternatively be used under the terms
2N/A * of the Common Development and Distribution License Version 1.0
2N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
2N/A * VirtualBox OSE distribution, in which case the provisions of the
2N/A * CDDL are applicable instead of those of the GPL.
2N/A *
2N/A * You may elect to license modified versions of this file under the
2N/A * terms and conditions of either the GPL or the CDDL or both.
2N/A *
2N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
2N/A * Clara, CA 95054 USA or visit http://www.sun.com if you need
2N/A * additional information or have any questions.
2N/A */
2N/A
2N/A
2N/A/*******************************************************************************
2N/A* Header Files *
2N/A*******************************************************************************/
2N/A#include <iprt/assert.h>
2N/A#include <iprt/log.h>
2N/A#include <iprt/string.h>
2N/A#include <iprt/stdarg.h>
2N/A#ifdef IN_RING3
2N/A# include <stdio.h>
2N/A#endif
2N/A
2N/A
2N/A#if defined(IN_GUEST_R0) && (defined(RT_OS_LINUX) || defined(RT_OS_WINDOWS))
2N/A/*
2N/A * This is legacy that should be eliminated. OS specific code deals with
2N/A * R0 assertions now and it will do the backdoor printfs in addition to
2N/A * proper OS specific printfs and panics / BSODs / IPEs.
2N/A */
2N/A#include <VBox/log.h>
2N/A
2N/A
2N/A/**
2N/A * The 1st part of an assert message.
2N/A *
2N/A * @param pszExpr Expression. Can be NULL.
2N/A * @param uLine Location line number.
2N/A * @param pszFile Location file name.
2N/A * @param pszFunction Location function name.
2N/A * @remark This API exists in HC Ring-3 and GC.
2N/A */
2N/ARTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
2N/A{
2N/A RTLogBackdoorPrintf("\n!!Assertion Failed!!\n"
2N/A "Expression: %s\n"
2N/A "Location : %s(%d) %s\n",
2N/A pszExpr, pszFile, uLine, pszFunction);
2N/A}
2N/A
2N/A
2N/A/**
2N/A * The 2nd (optional) part of an assert message.
2N/A *
2N/A * @param pszFormat Printf like format string.
2N/A * @param ... Arguments to that string.
2N/A * @remark This API exists in HC Ring-3 and GC.
2N/A */
2N/ARTDECL(void) AssertMsg2(const char *pszFormat, ...)
2N/A{ /* forwarder. */
2N/A va_list args;
2N/A va_start(args, pszFormat);
2N/A RTLogBackdoorPrintfV(pszFormat, args);
2N/A va_end(args);
2N/A}
2N/A
2N/A# if defined(RT_OS_LINUX) && defined(IN_MODULE)
2N/A/*
2N/A * When we build this in the Linux kernel module, we wish to make the
2N/A * symbols available to other modules as well.
2N/A */
2N/A# include "the-linux-kernel.h"
2N/AEXPORT_SYMBOL(AssertMsg1);
2N/AEXPORT_SYMBOL(AssertMsg2);
2N/A# endif
2N/A
2N/A#elif defined(IN_RING0)
2N/A
2N/A/* OS specific. */
2N/A
2N/A#else /* !IN_RING0 */
2N/A
2N/A
2N/A/** The last assert message, 1st part. */
2N/ARTDATADECL(char) g_szRTAssertMsg1[1024];
2N/A/** The last assert message, 2nd part. */
2N/ARTDATADECL(char) g_szRTAssertMsg2[2048];
2N/A
2N/A/**
2N/A * The 1st part of an assert message.
2N/A *
2N/A * @param pszExpr Expression. Can be NULL.
2N/A * @param uLine Location line number.
2N/A * @param pszFile Location file name.
2N/A * @param pszFunction Location function name.
2N/A * @remark This API exists in HC Ring-3 and GC.
2N/A */
2N/ARTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
2N/A{
2N/A#if !defined(IN_RING3) && !defined(LOG_NO_COM)
2N/A RTLogComPrintf("\n!!Assertion Failed!!\n"
2N/A "Expression: %s\n"
2N/A "Location : %s(%d) %s\n",
2N/A pszExpr, pszFile, uLine, pszFunction);
2N/A#endif
2N/A
2N/A PRTLOGGER pLog = RTLogRelDefaultInstance();
2N/A if (pLog)
2N/A {
2N/A RTLogRelPrintf("\n!!Assertion Failed!!\n"
2N/A "Expression: %s\n"
2N/A "Location : %s(%d) %s\n",
2N/A pszExpr, pszFile, uLine, pszFunction);
2N/A RTLogFlush(pLog);
2N/A }
2N/A
2N/A#ifndef LOG_ENABLED
2N/A if (!pLog)
2N/A#endif
2N/A {
2N/A pLog = RTLogDefaultInstance();
2N/A if (pLog)
2N/A {
2N/A RTLogPrintf("\n!!Assertion Failed!!\n"
2N/A "Expression: %s\n"
2N/A "Location : %s(%d) %s\n",
2N/A pszExpr, pszFile, uLine, pszFunction);
2N/A RTLogFlush(pLog);
2N/A }
2N/A }
2N/A
2N/A#ifdef IN_RING3
2N/A /* print to stderr, helps user and gdb debugging. */
2N/A fprintf(stderr,
2N/A "\n!!Assertion Failed!!\n"
2N/A "Expression: %s\n"
2N/A "Location : %s(%d) %s\n",
2N/A VALID_PTR(pszExpr) ? pszExpr : "<none>",
2N/A VALID_PTR(pszFile) ? pszFile : "<none>",
2N/A uLine,
2N/A VALID_PTR(pszFunction) ? pszFunction : "");
2N/A fflush(stderr);
#endif
RTStrPrintf(g_szRTAssertMsg1, sizeof(g_szRTAssertMsg1),
"\n!!Assertion Failed!!\n"
"Expression: %s\n"
"Location : %s(%d) %s\n",
pszExpr, pszFile, uLine, pszFunction);
}
/**
* The 2nd (optional) part of an assert message.
*
* @param pszFormat Printf like format string.
* @param ... Arguments to that string.
* @remark This API exists in HC Ring-3 and GC.
*/
RTDECL(void) AssertMsg2(const char *pszFormat, ...)
{
va_list args;
#if !defined(IN_RING3) && !defined(LOG_NO_COM)
va_start(args, pszFormat);
RTLogComPrintfV(pszFormat, args);
va_end(args);
#endif
PRTLOGGER pLog = RTLogRelDefaultInstance();
if (pLog)
{
va_start(args, pszFormat);
RTLogRelPrintfV(pszFormat, args);
va_end(args);
RTLogFlush(pLog);
}
pLog = RTLogDefaultInstance();
if (pLog)
{
va_start(args, pszFormat);
RTLogPrintfV(pszFormat, args);
va_end(args);
RTLogFlush(pLog);
}
#ifdef IN_RING3
/* print to stderr, helps user and gdb debugging. */
char szMsg[1024];
va_start(args, pszFormat);
RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, args);
va_end(args);
fprintf(stderr, "%s", szMsg);
fflush(stderr);
#endif
va_start(args, pszFormat);
RTStrPrintfV(g_szRTAssertMsg2, sizeof(g_szRTAssertMsg2), pszFormat, args);
va_end(args);
}
#endif /* !IN_RING0 */