VirtualBox

Changeset 95955 in vbox for trunk/src/VBox/Additions/3D/win


Ignore:
Timestamp:
Jul 29, 2022 8:52:37 PM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
152711
Message:

Add/Nt/Graphics: Made the debug logging code compile in IPRT_NO_CRT mode, cleaning it up a little in the process (_snpring doesn't terminate buffers on overflow, don't use sprintf). bugref:10261

Location:
trunk/src/VBox/Additions/3D/win/VBoxWddmUmHlp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/3D/win/VBoxWddmUmHlp/VBoxMpLogger.cpp

    r93115 r95955  
    2222 */
    2323
     24#define IPRT_NO_CRT_FOR_3RD_PARTY /* To get malloc and free wrappers in IPRT_NO_CRT mode. Doesn't link with IPRT in non-no-CRT mode. */
    2425#include "UmHlpInternal.h"
    2526
    2627#include <../../../common/wddm/VBoxMPIf.h>
    27 
    28 #include <stdio.h>
     28#include <stdlib.h>
     29#ifdef IPRT_NO_CRT
     30# include <iprt/process.h>
     31# include <iprt/string.h>
     32#else
     33# include <stdio.h>
     34#endif
    2935
    3036DECLCALLBACK(void) VBoxDispMpLoggerLog(const char *pszString)
     
    7076}
    7177
    72 DECLCALLBACK(void) VBoxDispMpLoggerLogF(const char *pszString, ...)
     78DECLCALLBACK(void) VBoxDispMpLoggerLogF(const char *pszFormat, ...)
    7379{
    74     char szBuffer[4096] = {0};
    75     va_list pArgList;
    76     va_start(pArgList, pszString);
    77     _vsnprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), pszString, pArgList);
    78     va_end(pArgList);
     80    /** @todo would make a whole lot more sense to just allocate
     81     *        VBOXDISPIFESCAPE_DBGPRINT here and printf into it's buffer than
     82     *        double buffering it like this */
     83    char szBuffer[4096];
     84    va_list va;
     85    va_start(va, pszFormat);
     86#ifdef IPRT_NO_CRT
     87    RTStrPrintf(szBuffer, sizeof(szBuffer), pszFormat, va);
     88#else
     89    _vsnprintf(szBuffer, sizeof(szBuffer), pszFormat, va);
     90    szBuffer[sizeof(szBuffer) - 1] = '\0'; /* Don't trust the _vsnprintf function terminate the string! */
     91#endif
     92    va_end(va);
    7993
    8094    VBoxDispMpLoggerLog(szBuffer);
    8195}
    8296
    83 /*
    84  * Prefix the output string with module name and pid/tid.
     97/**
     98 * Prefix the output string with exe name and pid/tid.
    8599 */
    86 static const char *vboxUmLogGetModuleName(void)
     100#ifndef IPRT_NO_CRT
     101static const char *vboxUmLogGetExeName(void)
    87102{
    88103    static int s_fModuleNameInited = 0;
     
    93108        const DWORD cchName = GetModuleFileNameA(NULL, s_szModuleName, RT_ELEMENTS(s_szModuleName));
    94109        if (cchName == 0)
    95         {
    96110            return "<no module>";
    97         }
    98111        s_fModuleNameInited = 1;
    99112    }
    100113    return &s_szModuleName[0];
    101114}
     115#endif
    102116
    103117DECLCALLBACK(void) VBoxWddmUmLog(const char *pszString)
    104118{
     119    /** @todo Allocate VBOXDISPIFESCAPE_DBGPRINT here and format right into it
     120     *        instead? That would be a lot more flexible and a little faster. */
    105121    char szBuffer[4096];
    106     const int cbBuffer = sizeof(szBuffer);
    107     char *pszBuffer = &szBuffer[0];
    108 
    109     int cbWritten = _snprintf(pszBuffer, cbBuffer, "['%s' 0x%lx.0x%lx]: ",
    110                               vboxUmLogGetModuleName(), GetCurrentProcessId(), GetCurrentThreadId());
    111     if (cbWritten < 0 || cbWritten >= cbBuffer)
    112     {
    113         Assert(0);
    114         pszBuffer[0] = 0;
    115         cbWritten = 0;
    116     }
    117 
    118     const size_t cbLeft = cbBuffer - cbWritten;
    119     const size_t cbString = strlen(pszString) + 1;
    120     if (cbString <= cbLeft)
    121     {
    122         memcpy(pszBuffer + cbWritten, pszString, cbString);
    123     }
    124     else
    125     {
    126         memcpy(pszBuffer + cbWritten, pszString, cbLeft - 1);
    127         pszBuffer[cbWritten + cbLeft - 1] = 0;
    128     }
     122#ifdef IPRT_NO_CRT
     123    /** @todo use RTProcShortName instead of RTProcExecutablePath? Will avoid
     124     *        chopping off log text if the executable path is too long. */
     125    RTStrPrintf(szBuffer, sizeof(szBuffer), "['%s' 0x%lx.0x%lx]: %s",
     126                RTProcExecutablePath() /* should've been initialized by nocrt-startup-dll-win.cpp already */,
     127                GetCurrentProcessId(), GetCurrentThreadId(), pszString);
     128#else
     129    int cch = _snprintf(szBuffer, sizeof(szBuffer), "['%s' 0x%lx.0x%lx]: %s",
     130                        vboxUmLogGetExeName(), GetCurrentProcessId(), GetCurrentThreadId(), pszString);
     131    AssertReturnVoid(cch > 0);             /* unlikely that we'll have string encoding problems, but just in case. */
     132    szBuffer[sizeof(szBuffer) - 1] = '\0'; /* the function doesn't necessarily terminate the buffer on overflow. */
     133#endif
    129134
    130135    VBoxDispMpLoggerLog(szBuffer);
  • trunk/src/VBox/Additions/3D/win/VBoxWddmUmHlp/VBoxWddmUmHlp.h

    r93115 r95955  
    3030
    3131/* Do not require IPRT library. */
    32 #if defined(Assert)
    33 #undef Assert
    34 #endif
    35 #ifdef RT_STRICT
    36 #define Assert(_e) (void)( (!!(_e)) || (ASMBreakpoint(), 0) )
    37 #else
    38 #define Assert(_e) (void)( 0 )
     32/** @todo r=bird: It is *NOT* okay to redefine Assert* (or Log*) macros!  It
     33 * causes confusing as the code no longer behaves in the way one expect.  Thus,
     34 * it is strictly forbidden. */
     35#ifndef IPRT_NO_CRT
     36# undef Assert
     37# undef AssertReturnVoid
     38# ifdef RT_STRICT
     39#  define Assert(_e) (void)( (!!(_e)) || (ASMBreakpoint(), 0) )
     40#  define AssertReturnVoid(a_Expr) do { if (RT_LIKELY(a_Expr)) {} else { ASMBreakpoint(); return; } } while (0)
     41# else
     42#  define Assert(_e) (void)( 0 )
     43#  define AssertReturnVoid(a_Expr) do { if (RT_LIKELY(a_Expr)) {} else return; } while (0)
     44# endif
    3945#endif
    4046
     
    8894DECLCALLBACK(D3DKMTFUNCTIONS const *) D3DKMTFunctions(void);
    8995
    90 DECLCALLBACK(void) VBoxDispMpLoggerLogF(const char *pszString, ...);
     96DECLCALLBACK(void) VBoxDispMpLoggerLogF(const char *pszFormat, ...);
    9197DECLCALLBACK(void) VBoxWddmUmLog(const char *pszString);
    9298
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette