Logo Search packages:      
Sourcecode: virtualbox-ose version File versions  Download package

int kPrfTerminate ( void   ) 

Stops, dumps, and terminates the profiling.

This should typically be called from some kind of module destruction function, so we can profile parts of the termination sequence too.

Returns:
0 on success

-1 on failure.

Definition at line 1518 of file kProfileR3.cpp.

References g_dwThreadTLS, KPRF_BITS, KPRF_NAME, KPRF_TYPE, kPrfFreeMem(), kPrfGetEnvString(), kPrfGetProcessId(), kPrfMutexDelete(), kPrfRWLockDelete(), and kPrfWriteFile().

Referenced by DllMain().

{
    /*
     * Stop the profiling.
     * As a safety precaution, sleep a little bit to allow threads
     * still at large inside profiler code some time to get out.
     */
    g_fEnabled = false;
    KPRF_TYPE(P,HDR) pHdr = g_pHdr;
    g_pHdr = NULL;
    if (!pHdr)
        return -1;

#if K_OS == K_OS_WINDOWS
    Sleep(10);
#elif K_OS == K_OS_OS2
    DosSleep(10);
#else
    usleep(10000);
#endif

    /*
     * Unwind all active threads and so forth.
     */
    KPRF_NAME(TerminateAll)(pHdr);

    /*
     * Use the stack space to fill in process details.
     */
#if K_OS == K_OS_WINDOWS
    /* all is one single string */
    const char *pszCommandLine = GetCommandLine();
    if (pszCommandLine)
        KPRF_NAME(SetCommandLine)(pHdr, 1, &pszCommandLine);

#elif K_OS == K_OS_OS2 || K_OS == K_OS_OS2
    PTIB pTib;
    PPIB pPib;
    DosGetInfoBlocks(&pTib, &pPib);
    if (pPib->pib_pchcmd)
    {
        /* Tradition say that the commandline is made up of two zero terminate strings
         * - first the executable name, then the arguments. Similar to what unix does,
         *   only completely mocked up because of the CMD.EXE tradition.
         */
        const char *apszArgs[2];
        apszArgs[0] = pPib->pib_pchcmd;
        apszArgs[1] = pPib->pib_pchcmd;
        while (apszArgs[1][0])
            apszArgs[1]++;
        apszArgs[1]++;
        KPRF_NAME(SetCommandLine)(pHdr, 2, apszArgs);
    }

#else
    /* linux can read /proc/self/something I guess. Don't know about the rest... */

#endif

    /*
     * Write the file to disk.
     */
    char szName[260 + 16];
    kPrfGetEnvString("KPRF2_FILE", szName, sizeof(szName) - 16, "kPrf2-");

    /* append the process id */
    KUPTR pid = kPrfGetProcessId();
    char *psz = szName;
    while (*psz)
        psz++;

    static char s_szDigits[0x11] = "0123456789abcdef";
    KU32 uShift = KPRF_BITS - 4;
    while (     uShift > 0
           &&   !(pid & (0xf << uShift)))
        uShift -= 4;
    *psz++ = s_szDigits[(pid >> uShift) & 0xf];
     while (uShift > 0)
     {
         uShift -= 4;
         *psz++ = s_szDigits[(pid >> uShift) & 0xf];
     }

    /* .kPrf2 */
    *psz++ = '.';
    *psz++ = 'k';
    *psz++ = 'P';
    *psz++ = 'r';
    *psz++ = 'f';
    *psz++ = '2';
    *psz++ = '\0';

    /* write the file. */
    int rc = kPrfWriteFile(szName, pHdr, pHdr->cb);

    /*
     * Free resources.
     */
    kPrfFreeMem(pHdr);
#if K_OS == K_OS_WINDOWS
    TlsFree(g_dwThreadTLS);
    g_dwThreadTLS = TLS_OUT_OF_INDEXES;

#elif defined(KPRF_USE_PTHREAD)
    pthread_key_delete(g_ThreadKey);
    g_ThreadKey = (pthread_key_t)-1;

#elif K_OS == K_OS_OS2
    DosFreeThreadLocalMemory((PULONG)g_ppThread);
    g_ppThread = NULL;

#else
# error "port me!"
#endif

    kPrfMutexDelete(&g_ThreadsMutex);
    kPrfMutexDelete(&g_ModSegsMutex);
    kPrfRWLockDelete(&g_FunctionsRWLock);

    return rc;
}


Generated by  Doxygen 1.6.0   Back to index