Ignore:
Timestamp:
May 28, 2002, 11:53:34 AM (23 years ago)
Author:
sandervl
Message:

PF: Debugger updates

File:
1 edited

Legend:

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

    r6975 r8504  
    1 /* $Id: oslibdebug.cpp,v 1.6 2001-10-09 20:25:20 sandervl Exp $ */
     1/* $Id: oslibdebug.cpp,v 1.7 2002-05-28 09:53:34 sandervl Exp $ */
    22
    33/*
     
    2626#include <winconst.h>
    2727#include "oslibdebug.h"
     28#include <stdio.h>
    2829
    2930#define DBG_LOCALLOG    DBG_oslibdebug
    3031#include "dbglocal.h"
    31 
     32static int superpid = 0;
     33
     34#define DEBUG_SEMNAME   "\\SEM32\\ODINTRACE\\"
    3235#define DEBUG_QUEUENAME "\\QUEUES\\ODINTRACE\\"
    3336#define DEBUG_QSEMNAME  "\\SEM32\\ODINTRACEQ\\"
    34 #define DEBUG_SEMNAME   "\\SEM32\\ODINTRACE\\"
     37
     38#define DEBUG_WINQSEMNAME  "\\SEM32\\WINTRACEQ\\"
     39#define DEBUG_WINQUEUENAME "\\QUEUES\\WINTRACE\\"
     40
     41#ifdef DEBUG
     42typedef struct
     43{
     44    LPSTR pszMsg;
     45    UINT msg;
     46} MSGDESC, *PMSGDESC;
     47
     48//
     49// Message description table.  Describes each message that can be spied on.
     50// This table must be kept in sorted order.
     51//
     52MSGDESC debugMsgs[] =
     53{
     54    { "EXCEPTION_DEBUG_EVENT", EXCEPTION_DEBUG_EVENT},
     55    { "CREATE_THREAD_DEBUG_EVENT", CREATE_THREAD_DEBUG_EVENT},                               // 0x0001
     56    { "CREATE_PROCESS_DEBUG_EVENT", CREATE_PROCESS_DEBUG_EVENT},
     57    { "EXIT_THREAD_DEBUG_EVENT", EXIT_THREAD_DEBUG_EVENT},
     58    { "EXIT_PROCESS_DEBUG_EVENT", EXIT_PROCESS_DEBUG_EVENT},                                   // 0x0005
     59    { "LOAD_DLL_DEBUG_EVENT", LOAD_DLL_DEBUG_EVENT},
     60    { "UNLOAD_DLL_DEBUG_EVENT", UNLOAD_DLL_DEBUG_EVENT},
     61    { "OUTPUT_DEBUG_STRING_EVENT", OUTPUT_DEBUG_STRING_EVENT},
     62    { "RIP_EVENT", RIP_EVENT}
     63};
     64
     65INT gcMessages = sizeof(debugMsgs) / sizeof(MSGDESC);
     66
     67char *GetDebugMsgText(int Msg)
     68{
     69 static char msgtxt[64];
     70 int i;
     71
     72  for(i=0;i<gcMessages;i++) {
     73        if(debugMsgs[i].msg == Msg)
     74                return(debugMsgs[i].pszMsg);
     75  }
     76  sprintf(msgtxt, "%s %X ","Unknown Message ", Msg);
     77  return(msgtxt);
     78}
     79#endif
    3580
    3681//******************************************************************************
     
    3883VOID _Optlink DebugThread(VOID *argpid)
    3984{
    40   BOOL   fTerminate    = FALSE;
    41   CHAR   QueueName[30] = DEBUG_QUEUENAME;
    42   CHAR   SemName[30]   = DEBUG_SEMNAME;
    43   CHAR   QSemName[30]  = DEBUG_QSEMNAME;
    44   HQUEUE QueueHandle   = 0;
    45   HEV    hevSem        = 0,
    46          hevQSem       = 0;
    47   uDB_t  DbgBuf        = {0};
    48   int    rc;
     85  BOOL   fTerminate       = FALSE;
     86  CHAR   QueueName[30]    = DEBUG_QUEUENAME;
     87  CHAR   WinQueueName[30] = DEBUG_WINQUEUENAME;
     88  CHAR   SemName[30]      = DEBUG_SEMNAME;
     89  CHAR   QSemName[30]     = DEBUG_QSEMNAME;
     90  CHAR   WinQSemName[30]  = DEBUG_WINQSEMNAME;
     91  HQUEUE QueueHandle      = 0;
     92  HQUEUE WinQueueHandle   = 0;
     93  HEV    hevSem           = 0,
     94         hevQSem          = 0,
     95         hevWinQSem       = 0;   
     96  uDB_t  DbgBuf           = {0};
     97  int    rc, rc2;
    4998  char   path[CCHMAXPATH];
    5099  Win32DllBase *winmod;
    51   LPDEBUG_EVENT lpde;
     100  REQUESTDATA Request     = {0};
     101  LPDEBUG_EVENT lpde,lpde2;
    52102  ULONG  *pid = (ULONG*)argpid;
    53103  ULONG  staticPid = *pid;
     104  ULONG  ulDataLen = 0, ulElemCode = 0, ulNumCalled = 0;
     105  PVOID  DataBuffer;
     106  BYTE   Priority = 0;
    54107  char   tmp[12];
    55108
    56109  dprintf(("KERNEL32: DebugThread pid:%d", *pid));
    57 
    58   strcat(QueueName, itoa(getpid(), tmp, 10));
     110  //------------ Output queue ----------------
     111  strcat(QueueName, itoa(*pid, tmp, 10));
    59112  rc = DosCreateQueue( &QueueHandle , QUE_FIFO, QueueName);
    60113  if(rc != 0)
    61114  {
    62      dprintf(("DebugThread: Could not create queue:%s rc:%d", QueueName, rc));
     115     dprintf(("DebugThread: Could not create output queue:%s rc:%d", QueueName, rc));
    63116     return;
    64117  }
    65   strcat(SemName, itoa(getpid(), tmp, 10));
     118  dprintf(("DebugThread: Output queue %s created", QueueName));
     119  //------------ Odin internal queue ----------------
     120  strcat(WinQueueName, itoa(*pid, tmp, 10));
     121  rc = DosCreateQueue( &WinQueueHandle , QUE_FIFO, WinQueueName);
     122  if(rc != 0)
     123  {
     124     dprintf(("DebugThread: Could not create Odin queue:%s rc:%d", WinQueueName, rc));
     125     return;
     126  }
     127  dprintf(("DebugThread: Odin internal win32 queue %s created", WinQueueName));
     128  //------------- Main Debug Semaphore -----------------
     129  strcat(SemName, itoa(*pid, tmp, 10));
    66130  rc = DosCreateEventSem(SemName, &hevSem, 0, TRUE);
    67131  if(rc != 0)
    68132  {
    69      dprintf(("DebugThread: Could not create event sem:%s rc:%d", SemName, rc));
     133     dprintf(("DebugThread: Could not create main debug sem:%s rc:%d", SemName, rc));
     134     DosCloseQueue(QueueHandle);
     135     DosCloseQueue(WinQueueHandle);
     136     return;
     137  }
     138  dprintf(("DebugThread: Main debug semaphore %s created", SemName));
     139
     140  //------------- Odin internal queue semaphor ---------------
     141  strcat(WinQSemName, itoa(*pid, tmp, 10));
     142  rc = DosCreateEventSem(WinQSemName, &hevWinQSem, 0, FALSE);
     143  if(rc != 0)
     144  {
     145     dprintf(("DebugThread: Could not create odin internal queue sem:%s rc:%d", QSemName, rc));
     146     DosCloseEventSem(hevSem);
     147     DosCloseQueue(WinQueueHandle);
    70148     DosCloseQueue(QueueHandle);
    71149     return;
    72150  }
    73   strcat(QSemName, itoa(getpid(), tmp, 10));
     151  dprintf(("DebugThread: Odin internal queue semaphore %s created", WinQSemName));
     152
     153  //------------- Output queue semaphor ---------------
     154  strcat(QSemName, itoa(*pid, tmp, 10));
    74155  rc = DosCreateEventSem(QSemName, &hevQSem, 0, FALSE);
    75156  if(rc != 0)
    76157  {
    77      dprintf(("DebugThread: Could not create event sem:%s rc:%d", QSemName, rc));
     158     dprintf(("DebugThread: Could not create event output queue sem:%s rc:%d", QSemName, rc));
    78159     DosCloseEventSem(hevSem);
     160     DosCloseEventSem(hevWinQSem);
     161     DosCloseQueue(WinQueueHandle);
    79162     DosCloseQueue(QueueHandle);
    80163     return;
     
    91174  {
    92175    dprintf(("DosDebug error: rc = %d error:%d", rc, DbgBuf.Value));
     176    DosCloseQueue(WinQueueHandle);
    93177    DosCloseQueue(QueueHandle);
    94178    DosCloseEventSem(hevSem);
     
    99183  {
    100184    DosWaitEventSem(hevSem, SEM_INDEFINITE_WAIT);
    101 
    102     DosDebug_GO:
     185    DosResetEventSem(hevSem,&ulNumCalled);
     186
     187DosDebug_GO:
    103188    DbgBuf.Cmd = DBG_C_Go;
    104189    DbgBuf.Pid = *pid;
     
    151236        // DosWriteQueue(QueueHandle, 0, sizeof(DEBUG_EVENT), lpde, 0);
    152237        // break;
    153         DbgBuf.Cmd = DBG_C_Continue;
    154         DbgBuf.Value = XCPT_CONTINUE_SEARCH;
    155         goto DebugApi;
     238        if (DbgBuf.Value == 0 && DbgBuf.Buffer == XCPT_BREAKPOINT)
     239        {
     240          dprintf(("Breakpoint encountered"));
     241          // This may be win32 event exception as well as common int3
     242          Priority = 0;
     243          ulElemCode = 0;
     244          rc2 = DosPeekQueue(WinQueueHandle,&Request, &ulDataLen, (PPVOID)&lpde, &ulElemCode,DCWW_NOWAIT, &Priority, hevWinQSem);
     245          if(rc2 == 0)
     246          {
     247             //There is a win32 event here
     248             rc = DosReadQueue(WinQueueHandle, &Request, &ulDataLen, (PPVOID) &lpde, 0, DCWW_NOWAIT,
     249                            &Priority, hevWinQSem);
     250             if (rc != 0)
     251             dprintf(("DebugThread - DosReadQueue failed!"));
     252             //Forward it to receiver
     253             lpde2 = (LPDEBUG_EVENT) malloc(sizeof(DEBUG_EVENT));
     254             OSLibDebugReadMemory ( lpde, lpde2,sizeof(DEBUG_EVENT),NULL); 
     255             dprintf(("DebugThread Win32 Event %s",GetDebugMsgText(lpde2->dwDebugEventCode)));
     256             DosWriteQueue(QueueHandle, 0, sizeof(DEBUG_EVENT), lpde2, 0);
     257             //Stay stopped
     258          }
     259          dprintf(("DebugThread - waiting for continue signal"));
     260          DosWaitEventSem(hevSem, SEM_INDEFINITE_WAIT);         
     261          DosResetEventSem(hevSem,&ulNumCalled);
     262          DbgBuf.Cmd = DBG_C_ReadReg;
     263          rc = DosDebug(&DbgBuf);
     264          if (rc != 0)
     265             dprintf(("DosDebug error: rc = %d", rc));
     266          DbgBuf.EIP++;
     267          DbgBuf.Cmd = DBG_C_WriteReg;
     268          rc = DosDebug(&DbgBuf);
     269          if (rc != 0)
     270             dprintf(("DosDebug error: rc = %d", rc));
     271          DbgBuf.Cmd = DBG_C_Continue;
     272          DbgBuf.Value = XCPT_CONTINUE_EXECUTION;
     273          goto DebugApi;
     274       }
     275       DbgBuf.Cmd = DBG_C_Continue;
     276       DbgBuf.Value = XCPT_CONTINUE_SEARCH;
     277       goto DebugApi;
    156278
    157279      case DBG_N_ModuleLoad:
     
    246368            lpde->u.CreateProcessInfo.hFile = 0;
    247369            lpde->u.CreateProcessInfo.hProcess = 0;
    248             lpde->u.CreateProcessInfo.hThread = 0;
     370            lpde->u.CreateProcessInfo.hThread = 10;
    249371            lpde->u.CreateProcessInfo.lpBaseOfImage = NULL;
    250372            lpde->u.CreateProcessInfo.dwDebugInfoFileOffset = 0;
     
    275397        {
    276398          dprintf(("DosDebug: os/2 module [%s], suppress", path));
    277           break;
     399          goto DosDebug_GO;
    278400        }
    279401        lpde = (LPDEBUG_EVENT) malloc(sizeof(DEBUG_EVENT));
     
    299421  dprintf(("DosDebug - ending the service thread"));
    300422  DosCloseQueue(QueueHandle);
     423  DosCloseQueue(WinQueueHandle);
    301424  DosCloseEventSem(hevSem);
    302425  DosCloseEventSem(hevQSem);
     
    321444  USHORT sel = RestoreOS2FS();
    322445
    323   strcat(SemName, itoa(getpid(),tmp, 10));
     446  strcat(SemName, itoa(superpid,tmp, 10));
    324447  rc = DosOpenEventSem(SemName, &hevQSem);
    325448  if(rc != 0)
     
    327450
    328451  // get a DebugEvent from our DebugThread
    329   strcat(QueueName, itoa(getpid(), tmp, 10));
     452  strcat(QueueName, itoa(superpid, tmp, 10));
    330453  rc = DosOpenQueue(&pidOwner, &QueueHandle, QueueName);
    331454  Request.pid = pidOwner;
     
    343466  // copy DebugEvent to user space and free queue pointer
    344467  memcpy(lpde, lpde_queue, len);
     468  // free our lpd
    345469  free(lpde_queue);
    346470  // DosCloseEventSem(hevSem);
     
    368492
    369493  // only continue DebugThread, if queue is empty
    370   strcat(QueueName, itoa(getpid(), tmp, 10));
     494  strcat(QueueName, itoa(superpid, tmp, 10));
    371495  rc = DosOpenQueue(&pidOwner, &QueueHandle, QueueName);
    372496  rc = DosQueryQueue(QueueHandle, &QEntries);
     
    376500  }
    377501  // continue DebugThread
    378   strcat(SemName, itoa(getpid(), tmp, 10));
     502  strcat(SemName, itoa(superpid, tmp, 10));
    379503  rc = DosOpenEventSem(SemName, &hev);
     504  if (rc != 0)
     505  {
     506    dprintf(("OSLibContinueDebugEvent: Failed to open even semaphore rc:%d",rc));
     507    return FALSE;
     508  }
    380509  rc = DosPostEventSem(hev);
     510  if (rc != 0)
     511  {
     512    dprintf(("OSLibContinueDebugEvent: Failed to trigger semaphore rc:%d",rc));
     513    return FALSE;
     514  }
    381515  // DosCloseEventSem(hev);
    382516  SetFS(sel);
    383   return (rc == 0) ? TRUE : FALSE;
     517  return TRUE;
    384518}
    385519//******************************************************************************
     
    441575
    442576   tid = _beginthread(DebugThread, NULL, 1024, (PVOID) pid);
     577   superpid = *pid;
    443578   if (tid == 0)
    444579   {
     
    452587//******************************************************************************
    453588//******************************************************************************
     589VOID OSLibDebugReadMemory(LPCVOID lpBaseAddress,LPVOID lpBuffer, DWORD cbRead, LPDWORD lpNumberOfBytesRead)
     590{
     591  uDB_t  DbgBuf        = {0};
     592  USHORT sel = RestoreOS2FS();
     593  APIRET rc;
     594  dprintf(("OSLibDebugReadMemory - reading from pid %d",superpid));       
     595  DbgBuf.Pid = superpid;
     596  DbgBuf.Cmd = DBG_C_ReadMemBuf;
     597  DbgBuf.Addr  = (ULONG)lpBaseAddress;
     598  DbgBuf.Buffer  = (ULONG)lpBuffer;
     599  DbgBuf.Len  = cbRead;
     600  rc = DosDebug(&DbgBuf);
     601  if (rc != 0)
     602  {
     603    dprintf(("OSLibDebugReadMemory(DosDebug) error: rc = %d error:%d", rc, DbgBuf.Value));
     604    SetFS(sel);
     605    return;
     606  }
     607 if (lpNumberOfBytesRead)
     608  *lpNumberOfBytesRead = cbRead;
     609 SetFS(sel);
     610 return;
     611}
     612//******************************************************************************
     613//******************************************************************************
     614BOOL OSLibAddWin32Event(LPDEBUG_EVENT lpde)
     615{
     616  uDB_t  DbgBuf        = {0};
     617  USHORT sel = RestoreOS2FS();
     618  APIRET rc;
     619  CHAR WinQueueName[30] = DEBUG_WINQUEUENAME;
     620  CHAR SemName[30]      = DEBUG_SEMNAME;
     621  HEV hevSem = 0;
     622  HANDLE WinQueueHandle;
     623  LPDEBUG_EVENT lpde_copy = NULL;
     624  char   tmp[12];
     625  PID    pidOwner;
     626
     627  dprintf(("OSLibAddWin32Event"));         
     628  // open main debug semaphore
     629  strcat(SemName, itoa(getpid(),tmp, 10));
     630  rc = DosOpenEventSem(SemName, &hevSem);
     631  if(rc != 0)
     632  {
     633    dprintf(("OSLibAddWin32Event failed to open semaphore %s - rc %d",SemName, rc));
     634    goto fail;
     635  }
     636
     637  // open Queues
     638  strcat(WinQueueName, itoa(getpid(), tmp, 10));
     639  rc = DosOpenQueue(&pidOwner, &WinQueueHandle, WinQueueName);
     640  if (rc != 0)
     641  {
     642    dprintf(("OSLibAddWin32Event failed to open queue - rc %d",rc));
     643    goto fail;
     644  }
     645
     646  // copy data to our buffer
     647  lpde_copy = (LPDEBUG_EVENT) malloc(sizeof(DEBUG_EVENT));
     648  memcpy(lpde_copy,lpde,sizeof(DEBUG_EVENT));
     649  rc = DosWriteQueue(WinQueueHandle, 0, sizeof(DEBUG_EVENT), lpde_copy, 0);
     650  if (rc !=0 )
     651  {
     652    dprintf(("OSLibAddWin32Event failed to write to queue - rc %d",rc));
     653    goto fail;
     654  }
     655
     656  // and post notification
     657  rc = DosPostEventSem(hevSem);
     658  if (rc != 0)
     659  {
     660    dprintf(("OSLibAddWin32Event failed to trigger semaphore - rc %d",rc));
     661    goto fail;
     662  }
     663  _interrupt(3);
     664  free(lpde_copy); 
     665  DosCloseEventSem(hevSem);
     666  DosCloseQueue(WinQueueHandle);
     667  SetFS(sel);
     668  return TRUE;
     669fail:
     670  if (lpde_copy) free(lpde_copy); 
     671  DosCloseEventSem(hevSem);
     672  DosCloseQueue(WinQueueHandle);
     673  SetFS(sel);
     674  return FALSE;
     675}
Note: See TracChangeset for help on using the changeset viewer.