VirtualBox

Changeset 37409 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jun 10, 2011 3:10:29 PM (14 years ago)
Author:
vboxsync
Message:

iprt/trace: Added the CPU/APIC ID to the trace entires. Added some introspection APIs

Location:
trunk/src/VBox/Runtime
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/Makefile.kmk

    r37396 r37409  
    14521452        common/log/logcom.cpp \
    14531453        common/log/logformat.cpp \
     1454        common/log/tracebuf.cpp \
     1455        common/log/tracedefault.cpp \
    14541456        common/misc/RTAssertMsg1Weak.cpp \
    14551457        common/misc/RTAssertMsg2.cpp \
     
    14641466        common/misc/sanity-cpp.cpp \
    14651467        common/misc/term.cpp \
     1468        common/path/RTPathFilename.cpp \
    14661469        common/string/strncmp.cpp \
    14671470        common/string/strpbrk.cpp \
     
    19331936        common/log/logformat.cpp \
    19341937        common/log/tracebuf.cpp \
     1938        common/log/tracedefault.cpp \
    19351939        common/misc/RTAssertMsg1Weak.cpp \
    19361940        common/misc/RTAssertMsg2.cpp \
     
    19441948        common/misc/sanity-c.c \
    19451949        common/misc/sanity-cpp.cpp \
     1950        common/path/RTPathFilename.cpp \
    19461951        common/string/strformat.cpp \
    19471952        common/string/strformatnum.cpp \
     
    19511956        common/string/strpbrk.cpp \
    19521957        common/string/strprintf.cpp \
     1958        common/string/RTStrCopy.cpp \
     1959        common/string/RTStrCopyEx.cpp \
    19531960        common/table/avllu32.cpp \
    19541961        common/table/avlou32.cpp \
  • trunk/src/VBox/Runtime/common/log/tracebuf.cpp

    r37400 r37409  
    1414# include <iprt/mem.h>
    1515#endif
     16#if defined(IN_RING0) || (!defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86))
     17# include <iprt/mp.h>
     18#else
     19# include <iprt/asm-amd64-x86.h>
     20#endif
     21#include <iprt/path.h>
    1622#include <iprt/string.h>
    1723#include <iprt/time.h>
     
    5056    /** Reference counter. */
    5157    uint32_t volatile   cRefs;
    52     /** The current entry. */
     58    /** The next entry to make use of. */
    5359    uint32_t volatile   iEntry;
    5460} RTTRACEBUFVOLATILE;
     
    6470    /** The nano second entry time stamp. */
    6571    uint64_t            NanoTS;
     72    /** The ID of the CPU the event was recorded.  */
     73    RTCPUID             idCpu;
    6674    /** The message. */
    67     char                szMsg[RTTRACEBUF_ALIGNMENT - sizeof(uint64_t)];
     75    char                szMsg[RTTRACEBUF_ALIGNMENT - sizeof(uint64_t) - sizeof(RTCPUID)];
    6876} RTTRACEBUFENTRY;
    6977AssertCompile(sizeof(RTTRACEBUFENTRY) <= RTTRACEBUF_ALIGNMENT);
     
    104112*   Defined Constants And Macros                                               *
    105113*******************************************************************************/
     114/**
     115 * Get the current CPU Id.
     116 */
     117#if defined(IN_RING0) || (!defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86))
     118# define RTTRACEBUF_CUR_CPU()   RTMpCpuId()
     119#else
     120# define RTTRACEBUF_CUR_CPU()   ASMGetApicId()
     121#endif
     122
    106123/** Calculates the address of the volatile trace buffer members. */
    107124#define RTTRACEBUF_TO_VOLATILE(a_pThis)     ((PRTTRACEBUFVOLATILE)((uint8_t *)(a_pThis) + (a_pThis)->offVolatile))
     
    144161        \
    145162        cRefs = ASMAtomicIncU32(&RTTRACEBUF_TO_VOLATILE(a_pThis)->cRefs); \
    146         if (RT_UNLIKELY(cRefs > 1 && cRefs < _1M)) \
     163        if (RT_UNLIKELY(cRefs < 1 || cRefs >= _1M)) \
    147164        { \
    148165            ASMAtomicDecU32(&RTTRACEBUF_TO_VOLATILE(a_pThis)->cRefs); \
     
    192209            return VERR_NOT_FOUND; \
    193210    } \
    194     else \
     211    else if ((a_hTraceBuf) != NIL_RTTRACEBUF) \
    195212    { \
    196213        pThis = (a_hTraceBuf); \
    197214        AssertPtrReturn(pThis, VERR_INVALID_HANDLE); \
    198215    } \
     216    else \
     217        return VERR_INVALID_HANDLE; \
     218    \
    199219    AssertReturn(pThis->u32Magic == RTTRACEBUF_MAGIC, VERR_INVALID_HANDLE); \
     220    if (pThis->fFlags & RTTRACEBUF_FLAGS_DISABLED) \
     221        return VINF_SUCCESS; \
    200222    AssertReturn(pThis->offVolatile < RTTRACEBUF_ALIGNMENT * 2, VERR_INVALID_HANDLE); \
    201223    pVolatile = RTTRACEBUF_TO_VOLATILE(pThis); \
     
    203225    /* Grab a reference. */ \
    204226    cRefs = ASMAtomicIncU32(&pVolatile->cRefs); \
    205     if (RT_UNLIKELY(cRefs > 1 && cRefs < _1M)) \
     227    if (RT_UNLIKELY(cRefs < 1 || cRefs >= _1M)) \
    206228    { \
    207229        ASMAtomicDecU32(&pVolatile->cRefs); \
     
    210232    \
    211233    /* Grab the next entry and set the time stamp. */ \
    212     iEntry  = ASMAtomicIncU32(&pVolatile->iEntry); \
     234    iEntry  = ASMAtomicIncU32(&pVolatile->iEntry) - 1; \
    213235    iEntry %= pThis->cEntries; \
    214     pEntry = RTTRACEBUF_TO_ENTRY(pThis, iEntry); \
     236    pEntry  = RTTRACEBUF_TO_ENTRY(pThis, iEntry); \
    215237    pEntry->NanoTS = RTTimeNanoTS(); \
    216     pszBuf = &pEntry->szMsg[0]; \
     238    pEntry->idCpu  = RTTRACEBUF_CUR_CPU(); \
     239    pszBuf  = &pEntry->szMsg[0]; \
    217240    *pszBuf = '\0'; \
    218     cchBuf = pThis->cbEntry - RT_OFFSETOF(RTTRACEBUFENTRY, szMsg) - 1; \
    219     rc     = VINF_SUCCESS
     241    cchBuf  = pThis->cbEntry - RT_OFFSETOF(RTTRACEBUFENTRY, szMsg) - 1; \
     242    rc      = VINF_SUCCESS
    220243
    221244
     
    230253    do { \
    231254        /* file(line): - no path */ \
    232         size_t cchPos = RTStrPrintf(pszBuf, cchBuf, "%Rfn(%d): ", pszFile, iLine); \
     255        size_t cchPos = RTStrPrintf(pszBuf, cchBuf, "%s(%d): ", RTPathFilename(pszFile), iLine); \
    233256        pszBuf += cchPos; \
    234257        cchBuf -= cchPos; \
     
    253276{
    254277    AssertPtrReturn(phTraceBuf, VERR_INVALID_POINTER);
    255     AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
     278    AssertReturn(!(fFlags & ~(RTTRACEBUF_FLAGS_MASK & ~ RTTRACEBUF_FLAGS_FREE_ME)), VERR_INVALID_PARAMETER);
    256279    AssertMsgReturn(cbEntry  <= RTTRACEBUF_MAX_ENTRIES,    ("%#x\n", cbEntry),  VERR_OUT_OF_RANGE);
    257280    AssertMsgReturn(cEntries <= RTTRACEBUF_MAX_ENTRY_SIZE, ("%#x\n", cEntries), VERR_OUT_OF_RANGE);
     
    304327    AssertMsgReturn(cbEntry  <= RTTRACEBUF_MAX_ENTRIES,    ("%#x\n", cbEntry),  VERR_OUT_OF_RANGE);
    305328    AssertMsgReturn(cEntries <= RTTRACEBUF_MAX_ENTRY_SIZE, ("%#x\n", cEntries), VERR_OUT_OF_RANGE);
    306     AssertPtrReturn(pvBlock, VERR_INVALID_POINTER);
    307329    AssertPtrReturn(pcbBlock, VERR_INVALID_POINTER);
     330    size_t const cbBlock = *pcbBlock;
     331    AssertReturn(RT_VALID_PTR(pvBlock) || !cbBlock, VERR_INVALID_POINTER);
    308332
    309333    /*
     
    311335     * This code can be made a bit more clever, if someone feels like it.
    312336     */
    313     size_t const cbBlock    = *pcbBlock;
    314337    size_t const cbHdr      = RT_ALIGN_Z(sizeof(RTTRACEBUFINT),      RTTRACEBUF_ALIGNMENT)
    315338                            + RT_ALIGN_Z(sizeof(RTTRACEBUFVOLATILE), RTTRACEBUF_ALIGNMENT);
     
    513536
    514537
    515 
    516 RTDECL(int) RTTraceBufDumpToLog(RTTRACEBUF hTraceBuf)
    517 {
     538RTDECL(int) RTTraceBufEnumEntries(RTTRACEBUF hTraceBuf, PFNRTTRACEBUFCALLBACK pfnCallback, void *pvUser)
     539{
     540    int                 rc = VINF_SUCCESS;
    518541    uint32_t            iBase;
    519542    uint32_t            cLeft;
     
    530553        pEntry = RTTRACEBUF_TO_ENTRY(pThis, iBase);
    531554        if (pEntry->NanoTS)
    532             RTLogPrintf("%u/%'llu: %s\n", cLeft, pEntry->NanoTS, pEntry->szMsg);
     555        {
     556            rc = pfnCallback((RTTRACEBUF)pThis, cLeft, pEntry->NanoTS, pEntry->idCpu, pEntry->szMsg, pvUser);
     557            if (rc != VINF_SUCCESS)
     558                break;
     559        }
    533560
    534561        /* next */
     
    537564
    538565    RTTRACEBUF_DROP_REFERENCE(pThis);
    539     return VINF_SUCCESS;
    540 }
    541 
    542 
    543 RTDECL(int) RTTraceBufDumpToAssert(RTTRACEBUF hTraceBuf)
     566    return rc;
     567}
     568
     569
     570RTDECL(uint32_t) RTTraceBufGetEntrySize(RTTRACEBUF hTraceBuf)
     571{
     572    PCRTTRACEBUFINT pThis = hTraceBuf;
     573    RTTRACEBUF_VALID_RETURN_RC(pThis, 0);
     574    return pThis->cbEntry;
     575}
     576
     577
     578RTDECL(uint32_t) RTTraceBufGetEntryCount(RTTRACEBUF hTraceBuf)
     579{
     580    PCRTTRACEBUFINT pThis = hTraceBuf;
     581    RTTRACEBUF_VALID_RETURN_RC(pThis, 0);
     582    return pThis->cEntries;
     583}
     584
     585
     586RTDECL(bool) RTTraceBufDisable(RTTRACEBUF hTraceBuf)
     587{
     588    PCRTTRACEBUFINT pThis = hTraceBuf;
     589    RTTRACEBUF_VALID_RETURN_RC(pThis, false);
     590    return !ASMAtomicBitTestAndSet((void volatile *)&pThis->fFlags, RTTRACEBUF_FLAGS_DISABLED_BIT);
     591}
     592
     593
     594RTDECL(bool) RTTraceBufEnable(RTTRACEBUF hTraceBuf)
     595{
     596    PCRTTRACEBUFINT pThis = hTraceBuf;
     597    RTTRACEBUF_VALID_RETURN_RC(pThis, false);
     598    return !ASMAtomicBitTestAndClear((void volatile *)&pThis->fFlags, RTTRACEBUF_FLAGS_DISABLED_BIT);
     599}
     600
     601
     602/*
     603 *
     604 * Move the following to a separate file, consider using the enumerator.
     605 *
     606 */
     607
     608RTDECL(int) RTTraceBufDumpToLog(RTTRACEBUF hTraceBuf)
    544609{
    545610    uint32_t            iBase;
     
    557622        pEntry = RTTRACEBUF_TO_ENTRY(pThis, iBase);
    558623        if (pEntry->NanoTS)
    559             RTAssertMsg2Add("%u/%'llu: %s\n", cLeft, pEntry->NanoTS, pEntry->szMsg);
     624            RTLogPrintf("%04u/%'llu/%02x: %s\n", cLeft, pEntry->NanoTS, pEntry->idCpu, pEntry->szMsg);
    560625
    561626        /* next */
     
    567632}
    568633
     634
     635RTDECL(int) RTTraceBufDumpToAssert(RTTRACEBUF hTraceBuf)
     636{
     637    uint32_t            iBase;
     638    uint32_t            cLeft;
     639    PCRTTRACEBUFINT     pThis;
     640    RTTRACEBUF_RESOLVE_VALIDATE_RETAIN_RETURN(hTraceBuf, pThis);
     641
     642    iBase = ASMAtomicReadU32(&RTTRACEBUF_TO_VOLATILE(pThis)->iEntry);
     643    cLeft = pThis->cEntries;
     644    while (cLeft--)
     645    {
     646        PRTTRACEBUFENTRY pEntry;
     647
     648        iBase %= pThis->cEntries;
     649        pEntry = RTTRACEBUF_TO_ENTRY(pThis, iBase);
     650        if (pEntry->NanoTS)
     651            RTAssertMsg2AddWeak("%u/%'llu/%02x: %s\n", cLeft, pEntry->NanoTS, pEntry->idCpu, pEntry->szMsg);
     652
     653        /* next */
     654        iBase += 1;
     655    }
     656
     657    RTTRACEBUF_DROP_REFERENCE(pThis);
     658    return VINF_SUCCESS;
     659}
     660
     661
     662
  • trunk/src/VBox/Runtime/common/log/tracedefault.cpp

    r37396 r37409  
    3030
    3131    RTTRACEBUF hOldTraceBuf;
     32#ifdef IN_RC
     33    hOldTraceBuf = (RTTRACEBUF)ASMAtomicXchgPtr((void **)&g_hDefaultTraceBuf, hTraceBuf);
     34#else
    3235    ASMAtomicXchgHandle(&g_hDefaultTraceBuf, hTraceBuf, &hOldTraceBuf);
     36#endif
    3337
    3438    if (    hOldTraceBuf != NIL_RTTRACEBUF
     
    3640    {
    3741        /* Race prevention kludge. */
     42#ifndef IN_RC
    3843        RTThreadSleep(33);
     44#endif
    3945        RTTraceBufRelease(hOldTraceBuf);
    4046    }
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