Changeset 9910 for trunk/src


Ignore:
Timestamp:
Mar 6, 2003, 11:22:27 AM (22 years ago)
Author:
sandervl
Message:

KSO: logging changes; fast PID & TID retrieval; added functions to set/clear the odin environment (fs, exception handler)

Location:
trunk/src/kernel32
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/critsection.cpp

    r7854 r9910  
    1 /* $Id: critsection.cpp,v 1.8 2002-02-09 17:27:30 sandervl Exp $ */
     1/* $Id: critsection.cpp,v 1.9 2003-03-06 10:22:26 sandervl Exp $ */
    22/*
    33 * Win32 critical sections
     
    1414#include <assert.h>
    1515#include <stdio.h>
    16 #include "debugtools.h"
    17 #include <misc.h>
     16#include <dbglog.h>
     17#include <debugtools.h>
    1818#include <odinwrap.h>
     19#include <wprocess.h>
    1920
    2021#define DBG_LOCALLOG    DBG_critsection
     
    2223
    2324DECLARE_DEBUG_CHANNEL(relay)
     25
    2426
    2527
     
    3436    crit->OwningThread   = 0;
    3537    crit->LockSemaphore  = CreateSemaphoreA( NULL, 0, 1, NULL );
    36     crit->Reserved       = GetCurrentProcessId();
     38    crit->Reserved       = ODIN_GetCurrentProcessId();
    3739}
    3840
     
    7577    if (InterlockedIncrement( &crit->LockCount ))
    7678    {
    77         if (crit->OwningThread == GetCurrentThreadId())
     79        if (crit->OwningThread == ODIN_GetCurrentThreadId())
    7880        {
    7981            crit->RecursionCount++;
     
    112114        }
    113115    }
    114     crit->OwningThread   = GetCurrentThreadId();
     116    crit->OwningThread   = ODIN_GetCurrentThreadId();
    115117    crit->RecursionCount = 1;
    116118}
     
    125127    if (InterlockedIncrement( &crit->LockCount ))
    126128    {
    127         if (crit->OwningThread == GetCurrentThreadId())
     129        if (crit->OwningThread == ODIN_GetCurrentThreadId())
    128130        {
    129131            crit->RecursionCount++;
     
    134136        return FALSE;
    135137    }
    136     crit->OwningThread   = GetCurrentThreadId();
     138    crit->OwningThread   = ODIN_GetCurrentThreadId();
    137139    crit->RecursionCount = 1;
    138140    return TRUE;
     
    146148{
    147149    dprintf2(("LeaveCriticalSection %x", crit));
    148     if (crit->OwningThread != GetCurrentThreadId()) return;
     150    if (crit->OwningThread != ODIN_GetCurrentThreadId()) return;
    149151       
    150152    if (--crit->RecursionCount)
     
    183185        InitializeCriticalSection( crit );
    184186
    185     else if ( crit->Reserved && crit->Reserved != GetCurrentProcessId() )
     187    else if ( crit->Reserved && crit->Reserved != ODIN_GetCurrentProcessId() )
    186188    {
    187189        dprintf(("(%p) called for %08lx first, %08lx now: making global\n",
    188                crit, crit->Reserved, GetCurrentProcessId() ));
     190               crit, crit->Reserved, ODIN_GetCurrentProcessId() ));
    189191
    190192        MakeCriticalSectionGlobal( crit );
     
    205207        else
    206208            dprintf(("(%p) for %08lx: Crst is global, don't know whether to delete\n",
    207                      crit, GetCurrentProcessId() ));
    208     }
    209 }
    210 
     209                     crit, ODIN_GetCurrentProcessId() ));
     210    }
     211}
     212
  • trunk/src/kernel32/dbglog.cpp

    r9806 r9910  
    1 /* $Id: dbglog.cpp,v 1.7 2003-02-14 13:24:48 sandervl Exp $ */
     1/* $Id: dbglog.cpp,v 1.8 2003-03-06 10:22:26 sandervl Exp $ */
    22
    33/*
     
    309309#ifdef WIN32_IP_LOGGING
    310310        char *logserver = getenv("WIN32LOG_IPSERVER");
    311         if(logserver) {
     311        if(logserver && loadNr == 0) {
    312312             sock_init();
    313313
     
    320320        }
    321321#endif
    322         char logname[CCHMAXPATH];
    323 
    324         sprintf(logname, "odin32_%d.log", loadNr);
    325         flog = fopen(logname, "w");
    326         if(flog == NULL) {//probably running exe on readonly device
    327             sprintf(logname, "%sodin32_%d.log", kernel32Path, loadNr);
    328             flog = fopen(logname, "w");
     322        char szLogFile[CCHMAXPATH];
     323        const char *pszLogBase = getenv("WIN32LOG_FILEBASE");
     324        if (!pszLogBase)
     325            pszLogBase = "odin32_";
     326
     327        sprintf(szLogFile, "%s%d.log", pszLogBase, loadNr);
     328        flog = fopen(szLogFile, "w");
     329        if(flog == NULL)
     330        {//probably running exe on readonly device
     331            sprintf(szLogFile, "%sodin32_%d.log", kernel32Path, loadNr);
     332            flog = fopen(szLogFile, "w");
    329333        }
    330334        oldcrtmsghandle = _set_crt_msg_handle(fileno(flog));
     
    333337      fLogging = FALSE;
    334338
    335     if(getenv("DISABLE_THREAD1")) {
    336       fDisableThread[0] = TRUE;
    337     }
    338     if(getenv("DISABLE_THREAD2")) {
    339       fDisableThread[1] = TRUE;
    340     }
    341     if(getenv("DISABLE_THREAD3")) {
    342       fDisableThread[2] = TRUE;
    343     }
    344     if(getenv("DISABLE_THREAD4")) {
    345       fDisableThread[3] = TRUE;
    346     }
    347     if(getenv("DISABLE_THREAD5")) {
    348       fDisableThread[4] = TRUE;
    349     }
     339    fDisableThread[0] = getenv("DISABLE_THREAD1") != NULL;
     340    fDisableThread[1] = getenv("DISABLE_THREAD2") != NULL;
     341    fDisableThread[2] = getenv("DISABLE_THREAD3") != NULL;
     342    fDisableThread[3] = getenv("DISABLE_THREAD4") != NULL;
     343    fDisableThread[4] = getenv("DISABLE_THREAD5") != NULL;
    350344  }
    351345
     
    443437            ulCallDepth = 0;
    444438#endif
     439#ifdef LOG_TIME
     440            if(sel == 0x150b && fSwitchTIBSel)
     441                sprintf(logbuffer, "t%02d (%3d): %x (FS=150B) ",
     442                        LOWORD(teb->o.odin.threadId), ulCallDepth, GetTickCount());
     443            else
     444                sprintf(logbuffer, "t%02d (%3d): %x ",
     445                        LOWORD(teb->o.odin.threadId), ulCallDepth, GetTickCount());
     446#else
    445447            if(sel == 0x150b && fSwitchTIBSel)
    446448                sprintf(logbuffer, "t%02d (%3d): (FS=150B) ",
     
    449451                sprintf(logbuffer, "t%02d (%3d): ",
    450452                        LOWORD(teb->o.odin.threadId), ulCallDepth);
     453#endif
    451454            prefixlen = strlen(logbuffer);
    452455        }
  • trunk/src/kernel32/exceptions.cpp

    r9893 r9910  
    1 /* $Id: exceptions.cpp,v 1.71 2003-03-03 16:41:03 sandervl Exp $ */
     1/* $Id: exceptions.cpp,v 1.72 2003-03-06 10:22:26 sandervl Exp $ */
    22
    33/*
     
    610610                szModName, iObj, offObj);
    611611    }
    612    
     612
    613613/*  This is very dangerous. Can hang PM.
    614614    rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, message, "Application Error",
     
    715715    char   szLineException[128];
    716716    char   szLineExceptionType[128];
    717    
     717
    718718    szLineException[0]  = 0;                                              /* initialize */
    719719    szLineExceptionType[0] = 0;                                              /* initialize */
     
    813813    case XCPT_DATATYPE_MISALIGNMENT:
    814814        strcpy(szLineException, "Datatype Misalignment");
    815         sprintf(szLineExceptionType, "R/W %08x alignment %08x at %08x.", pERepRec->ExceptionInfo[0], 
     815        sprintf(szLineExceptionType, "R/W %08x alignment %08x at %08x.", pERepRec->ExceptionInfo[0],
    816816                pERepRec->ExceptionInfo[1], pERepRec->ExceptionInfo[2]);
    817817        break;
     
    914914    }
    915915
    916     sprintf(szTrapDump, "---[Exception Information]------------\n   %s", szLineException);
    917 
    918     strcat(szTrapDump, " (");
     916    sprintf(szTrapDump, "---[Exception Information]------------\n   %s (", szLineException);
    919917
    920918    if (fExcptSoftware == TRUE)            /* software or hardware generated ? */
     
    935933    strcat(szTrapDump, ")\n");                                    /* add trailing brace */
    936934
     935    if (szLineExceptionType[0])
     936        sprintf(szTrapDump + strlen(szTrapDump), "   %s\n", szLineExceptionType);
    937937
    938938    rc = DosQueryModFromEIP(&ulModule, &ulObject, sizeof(szModule),
     
    947947        strcat(szTrapDump, szLineException);
    948948    }
    949     else 
     949    else
    950950    {   /* fault in DosAllocMem allocated memory, hence PE loader.. */
    951951        Win32ImageBase * pMod;
     
    10071007
    10081008        sprintf(szLineException, "   Env[0]=%08x Env[1]=%08x Env[2]=%08x Env[3]=%08x\n",
    1009                  pCtxRec->ctx_env[0], pCtxRec->ctx_env[1], 
     1009                 pCtxRec->ctx_env[0], pCtxRec->ctx_env[1],
    10101010                 pCtxRec->ctx_env[2], pCtxRec->ctx_env[3]);
    10111011        strcat(szTrapDump, szLineException);
     
    10531053//Override filename of exception log (expects full path)
    10541054//*****************************************************************************
    1055 void WIN32API SetCustomExceptionLog(LPSTR lpszLogName) 
     1055void WIN32API SetCustomExceptionLog(LPSTR lpszLogName)
    10561056{
    10571057    strcpy(szExceptionLogFileName, lpszLogName);
     
    10591059//*****************************************************************************
    10601060//*****************************************************************************
    1061 void WIN32API SetExceptionLogging(BOOL fEnable) 
     1061void WIN32API SetExceptionLogging(BOOL fEnable)
    10621062{
    10631063    fExceptionLoggging = fEnable;
     
    10881088                 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE,
    10891089                 0L);                            /* No extended attribute */
    1090    
     1090
    10911091    if(rc == NO_ERROR) {
    10921092        DosSetFilePtr(hFile, 0, FILE_END, &ulBytesWritten);
     
    10951095
    10961096            lpszExeName = WinExe->getModuleName();
    1097            
     1097
    10981098            if(lpszExeName) {
    10991099                DosWrite(hFile, "\n", 2, &ulBytesWritten);
     
    13401340
    13411341        dprintf(("KERNEL32: OS2ExceptionHandler: Continue and kill\n"));
    1342        
     1342
    13431343        pCtxRec->ctx_RegEip = (ULONG)KillWin32Process;
    13441344        pCtxRec->ctx_RegEsp = pCtxRec->ctx_RegEsp + 0x10;
     
    13511351    {
    13521352        //NOTE:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1353         //Don't print anything here -> fatal hang if exception occurred 
     1353        //Don't print anything here -> fatal hang if exception occurred
    13541354        //inside fprintf
    13551355        //NOTE:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    13561356
     1357        Win32MemMap *map;
     1358        BOOL  fWriteAccess = FALSE, ret;
     1359        ULONG offset, accessflag;
     1360
     1361        switch(pERepRec->ExceptionInfo[0]) {
     1362        case XCPT_READ_ACCESS:
     1363                accessflag = MEMMAP_ACCESS_READ;
     1364                break;
     1365        case XCPT_WRITE_ACCESS:
     1366                accessflag = MEMMAP_ACCESS_WRITE;
     1367                fWriteAccess = TRUE;
     1368                break;
     1369        default:
     1370                goto continueGuardException;
     1371        }
     1372
     1373        map = Win32MemMapView::findMapByView(pERepRec->ExceptionInfo[1], &offset, accessflag);
     1374        if(map == NULL) {
     1375            goto continueGuardException;
     1376        }
     1377        ret = map->commitGuardPage(pERepRec->ExceptionInfo[1], offset, fWriteAccess);
     1378        map->Release();
     1379        if(ret == TRUE)
     1380            goto continueexecution;
     1381
     1382continueGuardException:
    13571383        goto continuesearch;
    13581384    }
  • trunk/src/kernel32/kernel32.mak

    r9895 r9910  
    1 # $Id: kernel32.mak,v 1.40 2003-03-03 16:49:54 sandervl Exp $
     1# $Id: kernel32.mak,v 1.41 2003-03-06 10:22:27 sandervl Exp $
    22
    33#
     
    100100$(OBJDIR)\time.obj \
    101101$(OBJDIR)\mmap.obj \
     102$(OBJDIR)\mmapview.obj \
     103$(OBJDIR)\mmapdup.obj \
    102104$(OBJDIR)\winimagepe2lx.obj \
    103105$(OBJDIR)\winimagepeldr.obj \
     
    110112$(OBJDIR)\winexedummy.obj \
    111113$(OBJDIR)\critsection.obj \
     114$(OBJDIR)\fastinfoblocksa.obj \
     115$(OBJDIR)\fastinfoblocks.obj \
    112116$(OBJDIR)\pefile.obj \
    113117$(OBJDIR)\winimgres.obj \
  • trunk/src/kernel32/thread.cpp

    r9748 r9910  
    1 /* $Id: thread.cpp,v 1.50 2003-02-04 11:29:03 sandervl Exp $ */
     1/* $Id: thread.cpp,v 1.51 2003-03-06 10:22:27 sandervl Exp $ */
    22
    33/*
     
    3535#include <codepage.h>
    3636
     37#include <FastInfoBlocks.h>
     38
    3739#define DBG_LOCALLOG    DBG_thread
    3840#include "dbglocal.h"
     
    4850    // check cached identifier
    4951    TEB *teb = GetThreadTEB();
    50     if(teb != NULL && teb->o.odin.threadId != 0xFFFFFFFF) 
     52    if(teb != NULL && teb->o.odin.threadId != 0xFFFFFFFF)
    5153    {
    5254        // this is set in InitializeTIB() already.
    5355        return teb->o.odin.threadId;
    5456    }
    55  
     57
    5658////  dprintf(("GetCurrentThreadId\n"));
    5759    return MAKE_THREADID(O32_GetCurrentProcessId(), O32_GetCurrentThreadId());
     
    6668    if(teb == 0) {
    6769        DebugInt3();
    68         SetLastError(ERROR_INVALID_HANDLE); //todo 
     70        SetLastError(ERROR_INVALID_HANDLE); //todo
    6971        return 0;
    7072    }
     
    111113#ifdef DEBUG
    112114  TEB *teb;
    113  
     115
    114116  // embedded dbg_IncThreadCallDepth
    115117  teb = GetThreadTEB();
    116118  if(teb == NULL)
    117119    return;
    118    
     120
    119121  // add caller name to call stack trace
    120122  int iIndex = teb->o.odin.dbgCallDepth;
    121  
     123
    122124  // allocate callstack on demand
    123125  if (teb->o.odin.arrstrCallStack == NULL)
    124126    teb->o.odin.arrstrCallStack = (PVOID*)malloc( sizeof(LPSTR) * MAX_CALLSTACK_SIZE);
    125  
     127
    126128  // insert entry
    127129  if (iIndex < MAX_CALLSTACK_SIZE)
     
    151153#ifdef DEBUG
    152154  TEB *teb;
    153  
     155
    154156  // embedded dbg_DecThreadCallDepth
    155157  teb = GetThreadTEB();
    156158  if(teb == NULL)
    157159    return;
    158  
     160
    159161  --(teb->o.odin.dbgCallDepth);
    160  
     162
    161163  // add caller name to call stack trace
    162164  int iIndex = teb->o.odin.dbgCallDepth;
    163  
     165
    164166  // insert entry
    165167  if (teb->o.odin.arrstrCallStack)
     
    175177  // retrieve last caller name from stack
    176178  TEB *teb;
    177  
     179
    178180  // embedded dbg_DecThreadCallDepth
    179181  teb = GetThreadTEB();
     
    188190  }
    189191#endif
    190  
     192
    191193  return NULL;
    192194}
     
    323325    OS2SetExceptionHandler((void *)&exceptFrame);
    324326    winteb->o.odin.exceptFrame = (ULONG)&exceptFrame;
    325    
     327
    326328    //Determine if thread callback is inside a PE dll; if true, then force
    327329    //switch to win32 TIB (FS selector)
     
    367369//******************************************************************************
    368370//******************************************************************************
     371
     372/**
     373 * Enter odin context with this thread.
     374 *
     375 * Is called when an OS/2 process is calling into an Odin32 DLL.
     376 * This may be called also in a nested fashion and supports that.
     377 * The conterpart of ODIN_ThreadLeaveOdinContext().
     378 *
     379 * @returns The old FS selector.
     380 * @returns 0 if TEB creation failed.
     381 * @param   pExceptionRegRec    OS/2 Exception Registration Record (2 ULONGs)
     382 *                              must be located on the callers stack.
     383 * @param   fForceFSSwitch      If set we will force switching to Odin32 FS selector.
     384 *                              If clear it depends on defaults.
     385 */
     386USHORT WIN32API ODIN_ThreadEnterOdinContext(void *pExceptionRegRec, BOOL fForceFSSwitch)
     387{
     388    USHORT  selFSOld = 0;
     389
     390    /*
     391     * Get TEB pointer, create it if necessary.
     392     * @todo    Check if this really is the thread which the TEB was created
     393     *          for. If not create the TEB.
     394     */
     395    TEB *pTeb = GetThreadTEB();
     396    if (!pTeb)
     397    {
     398        BOOL fMainThread = fibGetTid() == 1;
     399        HANDLE hThreadMain = HMCreateThread(NULL, 0, 0, 0, 0, 0, fMainThread);
     400        pTeb = CreateTEB(hThreadMain, fibGetTid());
     401        if (!pTeb || InitializeThread(pTeb, fMainThread) == FALSE)
     402        {
     403            dprintf(("ODIN_ThreadEnterOdinContext: Failed to create TEB!"));
     404        }
     405    }
     406
     407    /*
     408     * Install the Odin32 exception handler.
     409     * Note: The Win32 exception structure referenced by FS:[0] is the same in OS/2
     410     */
     411    if (pExceptionRegRec)
     412        OS2SetExceptionHandler(pExceptionRegRec);
     413    if (    pTeb
     414        &&  !pTeb->o.odin.exceptFrame)  /* if allready present, we'll keep the first one. */
     415        pTeb->o.odin.exceptFrame = (ULONG)pExceptionRegRec;
     416
     417    /*
     418     * Switch Selector if TIB was created.
     419     */
     420    if (pTeb)
     421        selFSOld = SetWin32TIB(fForceFSSwitch ? TIB_SWITCH_FORCE_WIN32 : TIB_SWITCH_DEFAULT);
     422
     423    return selFSOld;
     424}
     425
     426
     427/**
     428 * Leave odin context with this thread.
     429 *
     430 * Is called when an OS/2 process is returning from an Odin32 DLL.
     431 * This may be called also in a nested fashion and supports that.
     432 * The conterpart of ODIN_ThreadEnterOdinContext().
     433 *
     434 * @returns The old FS selector.
     435 * @returns 0 if TEB creation failed.
     436 * @param   pExceptionRegRec    OS/2 Exception Registration Record (2 ULONGs)
     437 *                              must be located on the callers stack.
     438 * @param   fForceFSSwitch      If set we will force switching to Odin32 FS selector.
     439 *                              If clear it depends on defaults.
     440 */
     441void   WIN32API ODIN_ThreadLeaveOdinContext(void *pExceptionRegRec, USHORT selFSOld)
     442{
     443    /*
     444     * Install the Odin32 exception handler.
     445     * Note: The Win32 exception structure referenced by FS:[0] is the same in OS/2
     446     */
     447    if (pExceptionRegRec)
     448        OS2UnsetExceptionHandler(pExceptionRegRec);
     449    TEB *pTeb = GetThreadTEB();
     450    if (    pTeb
     451        &&  pTeb->o.odin.exceptFrame == (ULONG)pExceptionRegRec)
     452        pTeb->o.odin.exceptFrame = 0;
     453
     454    /*
     455     * Switch Back FS Selector.
     456     */
     457    if (selFSOld)
     458        SetFS(selFSOld);
     459}
     460
     461
     462/**
     463 * Leave odin context to call back into OS/2 code.
     464 *
     465 * Is called when and Odin32 Dll/Exe calls back into the OS/2 code.
     466 * The conterpart of ODIN_ThreadEnterOdinContextNested().
     467 *
     468 * @returns The old FS selector.
     469 * @returns 0 on failure.
     470 * @param   pExceptionRegRec    New OS/2 exception handler frame which are to be registered
     471 *                              before the Odin handler in the chain.
     472 *                              Must be located on the callers stack.
     473 * @param   fRemoveOdinExcpt    Remove the odin exception handler.
     474 */
     475USHORT WIN32API ODIN_ThreadLeaveOdinContextNested(void *pExceptionRegRec, BOOL fRemoveOdinExcpt)
     476{
     477    /*
     478     * Set OS/2 FS Selector.
     479     */
     480    USHORT  selFSOld = RestoreOS2FS();
     481
     482    /*
     483     * Remove the Odin exception handler (if requested).
     484     */
     485    if (fRemoveOdinExcpt)
     486    {
     487        TEB *pTeb = GetThreadTEB();
     488        if (pTeb)
     489            OS2UnsetExceptionHandler((void*)pTeb->o.odin.exceptFrame);
     490        /* else: no TEB created propbably no exception handler to remove. */
     491    }
     492
     493    /*
     494     * Change exception handler if required.
     495     */
     496    if (pExceptionRegRec)
     497    {
     498        extern unsigned long _System DosSetExceptionHandler(void *);
     499        DosSetExceptionHandler(pExceptionRegRec);
     500    }
     501
     502    return selFSOld;
     503}
     504
     505
     506/**
     507 * Re-enter Odin context after being back in OS/2 code.
     508 *
     509 * Is called when returning to Odin from OS/2 code.
     510 * The conterpart of ODIN_ThreadLeaveOdinContextNested().
     511 *
     512 * @param   pExceptionRegRec    The exception registration record for the OS/2
     513 *                              exception handler used with Nested Leave. NULL
     514 *                              if not used.
     515 * @param   fRestoreOdinExcpt   Restore the Odin exception handler.
     516 *                              This flag must not be set unless fRemoveOdinExcpt
     517 *                              was set when leaving the Odin context!
     518 * @param   selFSOld            The Odin FS selector returned by the Nested Leave api.
     519 *
     520 */
     521void   WIN32API ODIN_ThreadEnterOdinContextNested(void *pExceptionRegRec, BOOL fRestoreOdinExcpt, USHORT selFSOld)
     522{
     523    /*
     524     * Remove the exception handler registered in ODIN_ThreadLeaveOdinContextNested
     525     */
     526    if (pExceptionRegRec)
     527        OS2UnsetExceptionHandler(pExceptionRegRec);
     528
     529    /*
     530     * Restore Odin exception handler (if requested).
     531     */
     532    if (fRestoreOdinExcpt)
     533    {
     534        TEB *pTeb = GetThreadTEB();
     535        if (pTeb)
     536            OS2SetExceptionHandler((void*)pTeb->o.odin.exceptFrame);
     537    }
     538
     539    /*
     540     * Switch Back FS Selector.
     541     */
     542    if (selFSOld)
     543        SetFS(selFSOld);
     544}
     545
     546
Note: See TracChangeset for help on using the changeset viewer.