Changeset 8504 for trunk/src/kernel32/oslibdebug.cpp
- Timestamp:
- May 28, 2002, 11:53:34 AM (23 years ago)
- 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:20sandervl Exp $ */1 /* $Id: oslibdebug.cpp,v 1.7 2002-05-28 09:53:34 sandervl Exp $ */ 2 2 3 3 /* … … 26 26 #include <winconst.h> 27 27 #include "oslibdebug.h" 28 #include <stdio.h> 28 29 29 30 #define DBG_LOCALLOG DBG_oslibdebug 30 31 #include "dbglocal.h" 31 32 static int superpid = 0; 33 34 #define DEBUG_SEMNAME "\\SEM32\\ODINTRACE\\" 32 35 #define DEBUG_QUEUENAME "\\QUEUES\\ODINTRACE\\" 33 36 #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 42 typedef 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 // 52 MSGDESC 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 65 INT gcMessages = sizeof(debugMsgs) / sizeof(MSGDESC); 66 67 char *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 35 80 36 81 //****************************************************************************** … … 38 83 VOID _Optlink DebugThread(VOID *argpid) 39 84 { 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; 49 98 char path[CCHMAXPATH]; 50 99 Win32DllBase *winmod; 51 LPDEBUG_EVENT lpde; 100 REQUESTDATA Request = {0}; 101 LPDEBUG_EVENT lpde,lpde2; 52 102 ULONG *pid = (ULONG*)argpid; 53 103 ULONG staticPid = *pid; 104 ULONG ulDataLen = 0, ulElemCode = 0, ulNumCalled = 0; 105 PVOID DataBuffer; 106 BYTE Priority = 0; 54 107 char tmp[12]; 55 108 56 109 dprintf(("KERNEL32: DebugThread pid:%d", *pid)); 57 58 strcat(QueueName, itoa( getpid(), tmp, 10));110 //------------ Output queue ---------------- 111 strcat(QueueName, itoa(*pid, tmp, 10)); 59 112 rc = DosCreateQueue( &QueueHandle , QUE_FIFO, QueueName); 60 113 if(rc != 0) 61 114 { 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)); 63 116 return; 64 117 } 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)); 66 130 rc = DosCreateEventSem(SemName, &hevSem, 0, TRUE); 67 131 if(rc != 0) 68 132 { 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); 70 148 DosCloseQueue(QueueHandle); 71 149 return; 72 150 } 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)); 74 155 rc = DosCreateEventSem(QSemName, &hevQSem, 0, FALSE); 75 156 if(rc != 0) 76 157 { 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)); 78 159 DosCloseEventSem(hevSem); 160 DosCloseEventSem(hevWinQSem); 161 DosCloseQueue(WinQueueHandle); 79 162 DosCloseQueue(QueueHandle); 80 163 return; … … 91 174 { 92 175 dprintf(("DosDebug error: rc = %d error:%d", rc, DbgBuf.Value)); 176 DosCloseQueue(WinQueueHandle); 93 177 DosCloseQueue(QueueHandle); 94 178 DosCloseEventSem(hevSem); … … 99 183 { 100 184 DosWaitEventSem(hevSem, SEM_INDEFINITE_WAIT); 101 102 DosDebug_GO: 185 DosResetEventSem(hevSem,&ulNumCalled); 186 187 DosDebug_GO: 103 188 DbgBuf.Cmd = DBG_C_Go; 104 189 DbgBuf.Pid = *pid; … … 151 236 // DosWriteQueue(QueueHandle, 0, sizeof(DEBUG_EVENT), lpde, 0); 152 237 // 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; 156 278 157 279 case DBG_N_ModuleLoad: … … 246 368 lpde->u.CreateProcessInfo.hFile = 0; 247 369 lpde->u.CreateProcessInfo.hProcess = 0; 248 lpde->u.CreateProcessInfo.hThread = 0;370 lpde->u.CreateProcessInfo.hThread = 10; 249 371 lpde->u.CreateProcessInfo.lpBaseOfImage = NULL; 250 372 lpde->u.CreateProcessInfo.dwDebugInfoFileOffset = 0; … … 275 397 { 276 398 dprintf(("DosDebug: os/2 module [%s], suppress", path)); 277 break;399 goto DosDebug_GO; 278 400 } 279 401 lpde = (LPDEBUG_EVENT) malloc(sizeof(DEBUG_EVENT)); … … 299 421 dprintf(("DosDebug - ending the service thread")); 300 422 DosCloseQueue(QueueHandle); 423 DosCloseQueue(WinQueueHandle); 301 424 DosCloseEventSem(hevSem); 302 425 DosCloseEventSem(hevQSem); … … 321 444 USHORT sel = RestoreOS2FS(); 322 445 323 strcat(SemName, itoa( getpid(),tmp, 10));446 strcat(SemName, itoa(superpid,tmp, 10)); 324 447 rc = DosOpenEventSem(SemName, &hevQSem); 325 448 if(rc != 0) … … 327 450 328 451 // get a DebugEvent from our DebugThread 329 strcat(QueueName, itoa( getpid(), tmp, 10));452 strcat(QueueName, itoa(superpid, tmp, 10)); 330 453 rc = DosOpenQueue(&pidOwner, &QueueHandle, QueueName); 331 454 Request.pid = pidOwner; … … 343 466 // copy DebugEvent to user space and free queue pointer 344 467 memcpy(lpde, lpde_queue, len); 468 // free our lpd 345 469 free(lpde_queue); 346 470 // DosCloseEventSem(hevSem); … … 368 492 369 493 // only continue DebugThread, if queue is empty 370 strcat(QueueName, itoa( getpid(), tmp, 10));494 strcat(QueueName, itoa(superpid, tmp, 10)); 371 495 rc = DosOpenQueue(&pidOwner, &QueueHandle, QueueName); 372 496 rc = DosQueryQueue(QueueHandle, &QEntries); … … 376 500 } 377 501 // continue DebugThread 378 strcat(SemName, itoa( getpid(), tmp, 10));502 strcat(SemName, itoa(superpid, tmp, 10)); 379 503 rc = DosOpenEventSem(SemName, &hev); 504 if (rc != 0) 505 { 506 dprintf(("OSLibContinueDebugEvent: Failed to open even semaphore rc:%d",rc)); 507 return FALSE; 508 } 380 509 rc = DosPostEventSem(hev); 510 if (rc != 0) 511 { 512 dprintf(("OSLibContinueDebugEvent: Failed to trigger semaphore rc:%d",rc)); 513 return FALSE; 514 } 381 515 // DosCloseEventSem(hev); 382 516 SetFS(sel); 383 return (rc == 0) ? TRUE : FALSE;517 return TRUE; 384 518 } 385 519 //****************************************************************************** … … 441 575 442 576 tid = _beginthread(DebugThread, NULL, 1024, (PVOID) pid); 577 superpid = *pid; 443 578 if (tid == 0) 444 579 { … … 452 587 //****************************************************************************** 453 588 //****************************************************************************** 589 VOID 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 //****************************************************************************** 614 BOOL 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; 669 fail: 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.