VirtualBox

Changeset 95955 in vbox for trunk/src/VBox/Additions/WINNT


Ignore:
Timestamp:
Jul 29, 2022 8:52:37 PM (3 years ago)
Author:
vboxsync
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/WINNT/Graphics/Video/disp/wddm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp

    r93115 r95955  
    1919
    2020#ifdef VBOXWDDMDISP_DEBUG_VEHANDLER
    21 #include <Psapi.h>
    22 #endif
    23 
    24 #include <stdio.h>
    25 #include <stdarg.h>
     21# include <Psapi.h>
     22#endif
    2623
    2724#include <iprt/asm.h>
    2825#include <iprt/assert.h>
    29 
    30 static DWORD g_VBoxVDbgFIsModuleNameInited = 0;
    31 static char g_VBoxVDbgModuleName[MAX_PATH];
    32 
    33 char *vboxVDbgDoGetModuleName()
    34 {
    35     if (!g_VBoxVDbgFIsModuleNameInited)
    36     {
    37         DWORD cName = GetModuleFileNameA(NULL, g_VBoxVDbgModuleName, RT_ELEMENTS(g_VBoxVDbgModuleName));
     26#include <iprt/process.h>
     27#include <iprt/string.h>
     28
     29#ifndef IPRT_NO_CRT
     30# include <stdio.h>
     31#endif
     32
     33
     34#if defined(VBOXWDDMDISP_DEBUG) || defined(LOG_TO_BACKDOOR_DRV)
     35static const char *vboxVDbgDoGetExeName(void)
     36{
     37# ifdef IPRT_NO_CRT
     38    /** @todo use RTProcShortName instead?   */
     39    return RTProcExecutablePath(); /* should've been initialized by nocrt-startup-dll-win.cpp already */
     40# else
     41    static bool volatile s_fInitialized = false;
     42    static char          s_szExePath[MAX_PATH];
     43    if (!s_fInitialized)
     44    {
     45        DWORD cName = GetModuleFileNameA(NULL, s_szExePath, RT_ELEMENTS(s_szExePath));
    3846        if (!cName)
    3947        {
    40 #ifdef LOG_ENABLED
     48#  ifdef LOG_ENABLED
    4149            DWORD winEr = GetLastError();
    42 #endif
     50#  endif
    4351            WARN(("GetModuleFileNameA failed, winEr %d", winEr));
    4452            return NULL;
    4553        }
    46         g_VBoxVDbgFIsModuleNameInited = TRUE;
    47     }
    48     return g_VBoxVDbgModuleName;
    49 }
    50 
    51 static void vboxDispLogDbgFormatStringV(char * szBuffer, uint32_t cbBuffer, const char * szString, va_list pArgList)
    52 {
    53     uint32_t cbWritten = sprintf(szBuffer, "['%s' 0x%lx.0x%lx] Disp: ", vboxVDbgDoGetModuleName(), GetCurrentProcessId(), GetCurrentThreadId());
    54     if (cbWritten > cbBuffer)
    55     {
    56         AssertReleaseFailed();
    57         return;
    58     }
    59 
    60     _vsnprintf(szBuffer + cbWritten, cbBuffer - cbWritten, szString, pArgList);
    61 }
    62 
     54        s_fInitialized = TRUE;
     55    }
     56    return s_szExePath;
     57#endif /* !IPRT_NO_CRT */
     58}
     59
     60static void vboxDispLogDbgFormatStringV(char *pszBuffer, uint32_t cbBuffer, const char *pszFormat, va_list va)
     61{
     62# ifdef IPRT_NO_CRT
     63    va_list vaCopy;
     64    va_copy(vaCopy, va); /* The &va for a %N must not be taken from an parameter list, so copy it onto the stack. */
     65    RTStrPrintf(pszBuffer, cbBuffer, "['%s' 0x%lx.0x%lx] Disp: %N",
     66                vboxVDbgDoGetExeName(), GetCurrentProcessId(), GetCurrentThreadId(), pszFormat, &vaCopy);
     67    va_end(vaCopy);
     68# else
     69    int cch = _snprintf(pszBuffer, cbBuffer, "['%s' 0x%lx.0x%lx] Disp: ",
     70                        vboxVDbgDoGetExeName(), GetCurrentProcessId(), GetCurrentThreadId());
     71    AssertReturnVoid(cch > 0);
     72    AssertReturnVoid((unsigned)cch + 2 <= cbBuffer);
     73    cbBuffer  -= (unsigned)cch;
     74    pszBuffer += (unsigned)cch;
     75
     76    cch = _vsnprintf(pszBuffer, cbBuffer, pszFormat, va);
     77    pszBuffer[cbBuffer - 1] = '\0'; /* Don't trust _vsnprintf to terminate the output. */
     78# endif
     79}
     80
     81#endif /* VBOXWDDMDISP_DEBUG || LOG_TO_BACKDOOR_DRV */
    6382#if defined(VBOXWDDMDISP_DEBUG)
    6483LONG g_VBoxVDbgFIsDwm = -1;
     
    108127PVBOXWDDMDISP_RESOURCE g_VBoxVDbgInternalRc = NULL;
    109128
    110 VOID vboxVDbgDoPrintDmlCmd(const char* pszDesc, const char* pszCmd)
    111 {
     129VOID vboxVDbgDoPrintDmlCmd(const char *pszDesc, const char *pszCmd)
     130{
     131#ifdef IPRT_NO_CRT
     132    /* DML isn't exactly following XML rules, docs + examples are not consistent
     133       about escaping sequences even. But assume it groks the typical XML
     134       escape sequences for now. */
     135    vboxVDbgPrint(("<?dml?><exec cmd=\"%RMas\">%RMes</exec>, ( %s )\n", pszCmd, pszDesc, pszCmd)); /** @todo escape the last pszCmd too? */
     136#else
    112137    vboxVDbgPrint(("<?dml?><exec cmd=\"%s\">%s</exec>, ( %s )\n", pszCmd, pszDesc, pszCmd));
     138#endif
    113139}
    114140
    115141VOID vboxVDbgDoPrintDumpCmd(const char* pszDesc, const void *pvData, uint32_t width, uint32_t height, uint32_t bpp, uint32_t pitch)
    116142{
    117     char Cmd[1024];
    118     sprintf(Cmd, "!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d", pvData, width, height, bpp, pitch);
    119     vboxVDbgDoPrintDmlCmd(pszDesc, Cmd);
     143    char szCmd[1024];
     144#ifdef IPRT_NO_CRT
     145    RTStrPrintf(szCmd, sizeof(szCmd), "!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d", pvData, width, height, bpp, pitch);
     146#else
     147    _snprintf(szCmd, sizeof(szCmd), "!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d", pvData, width, height, bpp, pitch);
     148    szCmd[sizeof(szCmd) - 1] = '\0';
     149#endif
     150    vboxVDbgDoPrintDmlCmd(pszDesc, szCmd);
    120151}
    121152
     
    140171    const VBOXWDDMDISP_ALLOCATION *pAlloc = pInfo->pAlloc;
    141172    IDirect3DResource9 *pD3DRc = pInfo->pD3DRc;
    142     char rectBuf[24];
    143     if (pInfo->pRect)
    144         _snprintf(rectBuf, sizeof(rectBuf) / sizeof(rectBuf[0]), "(%ld:%ld);(%ld:%ld)",
    145                 pInfo->pRect->left, pInfo->pRect->top,
    146                 pInfo->pRect->right, pInfo->pRect->bottom);
     173    const RECT * const  pRect  = pInfo->pRect;
     174    char szRectBuf[24];
     175    if (pRect)
     176#ifdef IPRT_NO_CRT
     177        RTStrPrintf(szRectBuf, sizeof(szRectBuf), "(%ld:%ld);(%ld:%ld)", pRect->left, pRect->top, pRect->right, pRect->bottom);
     178#else
     179        _snprintf(szRectBuf, sizeof(szRectBuf), "(%ld:%ld);(%ld:%ld)", pRect->left, pRect->top, pRect->right, pRect->bottom);
     180#endif
    147181    else
    148         strcpy(rectBuf, "n/a");
     182        strcpy(szRectBuf, "n/a");
    149183
    150184    vboxVDbgPrint(("%s Sh(0x%p), Rc(0x%p), pAlloc(0x%x), pD3DIf(0x%p), Type(%s), Rect(%s), Locks(%d) %s",
     
    155189                    pD3DRc,
    156190                    pD3DRc ? vboxDispLogD3DRcType(pD3DRc->GetType()) : "n/a",
    157                     rectBuf,
     191                    szRectBuf,
    158192                    pAlloc ? pAlloc->LockInfo.cLocks : 0,
    159193                    pSuffix ? pSuffix : ""));
     
    624658BOOL vboxVDbgDoCheckExe(const char * pszName)
    625659{
    626     char *pszModule = vboxVDbgDoGetModuleName();
     660    char const *pszModule = vboxVDbgDoGetExeName();
    627661    if (!pszModule)
    628662        return FALSE;
    629     size_t cbModule, cbName;
    630     cbModule = strlen(pszModule);
    631     cbName = strlen(pszName);
    632     if (cbName > cbModule)
    633         return FALSE;
    634     if (_stricmp(pszName, pszModule + (cbModule - cbName)))
     663    size_t cchModule = strlen(pszModule);
     664    size_t cchName   = strlen(pszName);
     665    if (cchName > cchModule)
     666        return FALSE;
     667# ifdef IPRT_NO_CRT
     668    if (RTStrICmp(pszName, &pszModule[cchModule - cchName]))
     669# else
     670    if (_stricmp(pszName, &pszModule[cchModule - cchName]))
     671# endif
    635672        return FALSE;
    636673    return TRUE;
     
    741778
    742779#endif /* VBOXWDDMDISP_DEBUG_VEHANDLER */
    743 
    744780#if defined(VBOXWDDMDISP_DEBUG) || defined(LOG_TO_BACKDOOR_DRV)
    745 void vboxDispLogDrvF(char * szString, ...)
    746 {
    747     char szBuffer[4096] = {0};
    748     va_list pArgList;
    749     va_start(pArgList, szString);
    750     vboxDispLogDbgFormatStringV(szBuffer, sizeof (szBuffer), szString, pArgList);
    751     va_end(pArgList);
     781
     782void vboxDispLogDrvF(char const *pszFormat, ...)
     783{
     784    char szBuffer[4096];
     785    va_list va;
     786    va_start(va, pszFormat);
     787    vboxDispLogDbgFormatStringV(szBuffer, sizeof(szBuffer), pszFormat, va);
     788    va_end(va);
    752789
    753790    VBoxDispMpLoggerLog(szBuffer);
    754791}
    755792
    756 void vboxDispLogDbgPrintF(char * szString, ...)
    757 {
    758     char szBuffer[4096] = { 0 };
    759     va_list pArgList;
    760     va_start(pArgList, szString);
    761     vboxDispLogDbgFormatStringV(szBuffer, sizeof(szBuffer), szString, pArgList);
    762     va_end(pArgList);
     793void vboxDispLogDbgPrintF(const char *pszFormat, ...)
     794{
     795    char szBuffer[4096];
     796    va_list va;
     797    va_start(va, pszFormat);
     798    vboxDispLogDbgFormatStringV(szBuffer, sizeof(szBuffer), pszFormat, va);
     799    va_end(va);
    763800
    764801    OutputDebugStringA(szBuffer);
    765802}
    766 #endif
     803
     804#endif
  • trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h

    r93115 r95955  
    188188VBOXDISPMPLOGGER_DECL(void) VBoxDispMpLoggerDumpD3DCAPS9(struct _D3DCAPS9 *pCaps);
    189189
    190 void vboxDispLogDrvF(char * szString, ...);
     190void vboxDispLogDrvF(const char *pszFormat, ...);
    191191
    192192# define vboxDispDumpD3DCAPS9(_pCaps) do { VBoxDispMpLoggerDumpD3DCAPS9(_pCaps); } while (0)
     
    197197#ifdef VBOXWDDMDISP_DEBUG
    198198
    199 void vboxDispLogDbgPrintF(char * szString, ...);
     199void vboxDispLogDbgPrintF(const char *pszFormat, ...);
    200200
    201201typedef struct VBOXWDDMDISP_ALLOCATION *PVBOXWDDMDISP_ALLOCATION;
  • trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/shared/VBoxDispMpLogger.cpp

    r93299 r95955  
    22/** @file
    33 * VBox WDDM Display backdoor logger implementation
     4 *
     5 * We're unable to use standard r3 vbgl-based backdoor logging API because win8
     6 * Metro apps can not do CreateFile/Read/Write by default.  This is why we use
     7 * miniport escape functionality to issue backdoor log string to the miniport
     8 * and submit it to host via standard r0 backdoor logging api accordingly.
    49 */
    510
     
    1621 */
    1722
    18 /* We're unable to use standard r3 vbgl-based backdoor logging API because win8 Metro apps
    19  * can not do CreateFile/Read/Write by default
    20  * this is why we use miniport escape functionality to issue backdoor log string to the miniport
    21  * and submit it to host via standard r0 backdoor logging api accordingly */
    22 
    2323#include <VBoxDispMpLogger.h>
    2424#include <iprt/win/windows.h>
     
    3535#include <iprt/errcore.h>
    3636#include <iprt/mem.h>
    37 
    38 #include <stdio.h>
     37#include <iprt/process.h>
     38
     39#ifndef IPRT_NO_CRT
     40# include <stdio.h>
     41#endif
     42
    3943
    4044typedef enum
     
    151155}
    152156
    153 VBOXDISPMPLOGGER_DECL(void) VBoxDispMpLoggerLogF(const char *pszString, ...)
     157VBOXDISPMPLOGGER_DECL(void) VBoxDispMpLoggerLogF(const char *pszFormat, ...)
    154158{
    155159    PVBOXDISPMPLOGGER pLogger = vboxDispMpLoggerGet();
     
    157161        return;
    158162
    159     char szBuffer[4096] = {0};
    160     va_list pArgList;
    161     va_start(pArgList, pszString);
    162     _vsnprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), pszString, pArgList);
    163     va_end(pArgList);
     163    char szBuffer[4096];
     164    va_list va;
     165    va_start(va, pszFormat);
     166#ifdef IPRT_NO_CRT
     167    RTStrPrintfV(szBuffer, sizeof(szBuffer), pszFormat, va);
     168#else
     169    int cch = _vsnprintf(szBuffer, sizeof(szBuffer), pszFormat, va);
     170    AssertReturnVoid(cch >= 0);            /* unlikely that we'll have string encoding problems, but just in case. */
     171    szBuffer[sizeof(szBuffer) - 1] = '\0'; /* doesn't necessarily terminate the buffer on overflow */
     172#endif
     173    va_end(va);
    164174
    165175    VBoxDispMpLoggerLog(szBuffer);
     
    222232
    223233/*
    224  * Prefix the output string with module name and pid/tid.
     234 * Prefix the output string with exe name and pid/tid.
    225235 */
    226 static const char *vboxUmLogGetModuleName(void)
    227 {
     236static const char *vboxUmLogGetExeName(void)
     237{
     238#ifdef IPRT_NO_CRT
     239    /** @todo use RTProcShortName instead?   */
     240    return RTProcExecutablePath(); /* should've been initialized by nocrt-startup-dll-win.cpp already */
     241#else
    228242    static int s_fModuleNameInited = 0;
    229243    static char s_szModuleName[MAX_PATH];
    230 
    231244    if (!s_fModuleNameInited)
    232245    {
    233246        const DWORD cchName = GetModuleFileNameA(NULL, s_szModuleName, RT_ELEMENTS(s_szModuleName));
    234247        if (cchName == 0)
    235         {
    236248            return "<no module>";
    237         }
    238249        s_fModuleNameInited = 1;
    239250    }
    240251    return &s_szModuleName[0];
     252#endif
    241253}
    242254
     
    244256{
    245257    char szBuffer[4096];
    246     const int cbBuffer = sizeof(szBuffer);
    247     char *pszBuffer = &szBuffer[0];
    248 
    249     int cbWritten = _snprintf(pszBuffer, cbBuffer, "['%s' 0x%lx.0x%lx]: ",
    250                               vboxUmLogGetModuleName(), GetCurrentProcessId(), GetCurrentThreadId());
    251     if (cbWritten < 0 || cbWritten >= cbBuffer)
    252     {
    253         AssertFailed();
    254         pszBuffer[0] = 0;
    255         cbWritten = 0;
    256     }
    257 
    258     const size_t cbLeft = cbBuffer - cbWritten;
    259     const size_t cbString = strlen(pszString) + 1;
    260     if (cbString <= cbLeft)
    261     {
    262         memcpy(pszBuffer + cbWritten, pszString, cbString);
    263     }
    264     else
    265     {
    266         memcpy(pszBuffer + cbWritten, pszString, cbLeft - 1);
    267         pszBuffer[cbWritten + cbLeft - 1] = 0;
    268     }
     258#ifdef IPRT_NO_CRT
     259    RTStrPrintf(szBuffer, sizeof(szBuffer), "['%s' 0x%lx.0x%lx]: %s",
     260                vboxUmLogGetExeName(), GetCurrentProcessId(), GetCurrentThreadId(), pszString);
     261#else
     262    int cch = _snprintf(szBuffer, sizeof(szBuffer), "['%s' 0x%lx.0x%lx]: %s",
     263                        vboxUmLogGetExeName(), GetCurrentProcessId(), GetCurrentThreadId(), pszString);
     264    AssertReturnVoid(cch > 0);             /* unlikely that we'll have string encoding problems, but just in case. */
     265    szBuffer[sizeof(szBuffer) - 1] = '\0'; /* the function doesn't necessarily terminate the buffer on overflow. */
     266#endif
    269267
    270268    VBoxDispMpLoggerLog(szBuffer);
  • trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/shared/VBoxDispMpLogger.h

    r93115 r95955  
    4040VBOXDISPMPLOGGER_DECL(void) VBoxDispMpLoggerLog(const char *pszString);
    4141
    42 VBOXDISPMPLOGGER_DECL(void) VBoxDispMpLoggerLogF(const char *pszString, ...);
     42VBOXDISPMPLOGGER_DECL(void) VBoxDispMpLoggerLogF(const char *pszFormat, ...);
    4343
    4444DECLCALLBACK(void) VBoxWddmUmLog(const char *pszString);
Note: See TracChangeset for help on using the changeset viewer.

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