/*++ Copyright (c) 1992 Microsoft Corporation Module Name: endp.c Abstract: Implements the endp, state, port, and proc commands. Author: Keith Moore (keithmo) 19-Apr-1995 Environment: User Mode. Revision History: --*/ #include "afdkdp.h" #pragma hdrstop // // Private prototypes. // BOOL DumpEndpointCallback( ULONG64 ActualAddress, ULONG64 Context ); BOOL FindStateCallback( ULONG64 ActualAddress, ULONG64 Context ); BOOL FindPortCallback( ULONG64 ActualAddress, ULONG64 Context ); BOOL FindProcessCallback( ULONG64 ActualAddress, ULONG64 Context ); ULONG64 FindProcessByPid ( ULONG64 Pid ); ULONG FindProcessByPidCallback ( PFIELD_INFO pField, PVOID UserContext ); // // Public functions. // DECLARE_API( endp ) /*++ Routine Description: Dumps the AFD_ENDPOINT structure at the specified address, if given or all endpoints. Arguments: None. Return Value: None. --*/ { ULONG result; INT i; CHAR expr[MAX_ADDRESS_EXPRESSION]; PCHAR argp; ULONG64 address; gClient = pClient; if (!CheckKmGlobals ()) { return E_INVALIDARG; } argp = ProcessOptions ((PCHAR)args); if (argp==NULL) return E_INVALIDARG; if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_HEADER); } if ((argp[0]==0) || (Options & AFDKD_ENDPOINT_SCAN)) { EnumEndpoints( DumpEndpointCallback, 0 ); dprintf ("\nTotal endpoints: %ld", EntityCount); } else { // // Snag the address from the command line. // while (sscanf( argp, "%s%n", expr, &i )==1) { if( CheckControlC() ) { break; } argp+=i; address = GetExpression (expr); result = (ULONG)InitTypeRead (address, AFD!AFD_ENDPOINT); if (result!=0) { dprintf ("\nendp: Could not read AFD_ENDPOINT @ %p, err: %d\n", address, result); break; } if (Options & AFDKD_BRIEF_DISPLAY) { DumpAfdEndpointBrief ( address ); } else { DumpAfdEndpoint ( address ); } if (Options & AFDKD_FIELD_DISPLAY) { ProcessFieldOutput (address, "AFD!AFD_ENDPOINT"); } } } if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_TRAILER); } else { dprintf ("\n"); } return S_OK; } // endp // // Public functions. // DECLARE_API( file ) /*++ Routine Description: Dumps the AFD_ENDPOINT structure associated with AFD file object. Arguments: None. Return Value: None. --*/ { ULONG result; INT i; CHAR expr[MAX_ADDRESS_EXPRESSION]; PCHAR argp; ULONG64 address, endpAddr; gClient = pClient; if (!CheckKmGlobals ()) { return E_INVALIDARG; } argp = ProcessOptions ((PCHAR)args); if (argp==NULL) return E_INVALIDARG; if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_HEADER); } // // Snag the address from the command line. // while (sscanf( argp, "%s%n", expr, &i )==1) { if( CheckControlC() ) { break; } argp += i; address = GetExpression (expr); result = GetFieldValue (address, "NT!_FILE_OBJECT", "FsContext", endpAddr); if (result!=0) { dprintf ("\nfile: Could not read FILE_OBJECT @ %p, err: %d\n", address, result); break; } result = (ULONG)InitTypeRead (endpAddr, AFD!AFD_ENDPOINT); if (result!=0) { dprintf ("\nfile: Could not read AFD_ENDPOINT @ %p, err: %d\n", endpAddr, result); break; } if (Options & AFDKD_BRIEF_DISPLAY) { DumpAfdEndpointBrief ( endpAddr ); } else { DumpAfdEndpoint ( endpAddr ); } if (Options & AFDKD_FIELD_DISPLAY) { ProcessFieldOutput (endpAddr, "AFD!AFD_ENDPOINT"); } } if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_TRAILER); } return S_OK; } // file DECLARE_API( state ) /*++ Routine Description: Dumps all AFD_ENDPOINT structures in the given state. Arguments: None. Return Value: None. --*/ { INT i; CHAR expr[MAX_ADDRESS_EXPRESSION]; PCHAR argp; ULONG64 val; gClient = pClient; if (!CheckKmGlobals ()) { return E_INVALIDARG; } argp = ProcessOptions ((PCHAR)args); if (argp==NULL) return E_INVALIDARG; if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_HEADER); } // // Snag the state from the command line. // while (sscanf( argp, "%s%n", expr, &i )==1) { if( CheckControlC() ) { break; } argp+=i; val = GetExpression (expr); dprintf ("\nLooking for endpoints in state 0x%I64x ", val); EnumEndpoints( FindStateCallback, val ); dprintf ("\nTotal endpoints: %ld", EntityCount); } if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_TRAILER); } else { dprintf ("\n"); } return S_OK; } // state DECLARE_API( port ) /*++ Routine Description: Dumps all AFD_ENDPOINT structures bound to the given port. Arguments: None. Return Value: None. --*/ { INT i; CHAR expr[MAX_ADDRESS_EXPRESSION]; PCHAR argp; ULONG64 val; gClient = pClient; if (!CheckKmGlobals ()) { return E_INVALIDARG; } argp = ProcessOptions ((PCHAR)args); if (argp==NULL) return E_INVALIDARG; if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_HEADER); } // // Snag the port from the command line. // while (sscanf( argp, "%s%n", expr, &i)==1) { if( CheckControlC() ) { break; } argp+=i; val = GetExpression (expr); dprintf ("\nLooking for endpoints bound to port 0x%I64x (0x%I64d) ", val, val); EnumEndpoints( FindPortCallback, val ); dprintf ("\nTotal endpoints: %ld", EntityCount); } if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_TRAILER); } else { dprintf ("\n"); } return S_OK; } // port DECLARE_API( proc ) /*++ Routine Description: Dumps all AFD_ENDPOINT structures owned by the given process. Arguments: None. Return Value: None. --*/ { INT i; CHAR expr[MAX_ADDRESS_EXPRESSION]; PCHAR argp; ULONG64 val; BOOLEAN dumpedSomething = FALSE; gClient = pClient; if (!CheckKmGlobals ()) { return E_INVALIDARG; } argp = ProcessOptions ((PCHAR)args); if (argp==NULL) return E_INVALIDARG; if (Options&AFDKD_BRIEF_DISPLAY) { dprintf (AFDKD_BRIEF_ENDPOINT_DISPLAY_HEADER); } // // Snag the process from the command line. // expr[0] = 0; i = 0; while (sscanf( argp, "%s%n", expr, &i )==1 || !dumpedSomething ) { dumpedSomething = TRUE; if( CheckControlC() ) { break; } argp+=i; val = GetExpression (expr); if (val=2419) { process = ReadField (OwningProcess); } else { process = ReadField (ProcessCharge.Process); } if( process == Context ) { if (!(Options & AFDKD_CONDITIONAL) || CheckConditional (ActualAddress, "AFD!AFD_ENDPOINT") ) { if (Options & AFDKD_NO_DISPLAY) dprintf ("+"); else { if (Options & AFDKD_BRIEF_DISPLAY) { DumpAfdEndpointBrief ( ActualAddress ); } else { DumpAfdEndpoint ( ActualAddress ); } if (Options & AFDKD_FIELD_DISPLAY) { ProcessFieldOutput (ActualAddress, "AFD!AFD_ENDPOINT"); } } EntityCount += 1; } else dprintf ("."); } else { dprintf ("."); } return TRUE; } // FindProcessCallback ULONG FindProcessByPidCallback ( PFIELD_INFO pField, PVOID UserContext ) { PULONG64 pProcess = UserContext; ULONG64 Pid; ULONG result; result = GetFieldValue ( pField->address, "NT!_EPROCESS", "UniqueProcessId", Pid ); if (result==0) { if (Pid==*pProcess) { *pProcess = pField->address; result = 1; } else dprintf ("."); } else { dprintf ("\nFindProcessByPidCallback: Could not read process @ %p, err: %ld\n", pField->address, result); *pProcess = 0; } return result; } ULONG64 FindProcessByPid ( ULONG64 Pid ) { ULONG64 Process, Start; if (DebuggerData.PsActiveProcessHead==0) { dprintf ("\nFindProcessByPid: PsActiveProcessHead is NULL!!!\n"); return 0; } if (ReadPtr (DebuggerData.PsActiveProcessHead, &Start)!=0) { dprintf ("\nFindProcessByPid: Can't read PsActiveProcessHead!!!\n"); return 0; } Process = Pid; ListType ( "NT!_EPROCESS", // Type Start, // Address 1, // ListByFieldAddress "ActiveProcessLinks.Flink", // NextPointer &Process, // Context FindProcessByPidCallback ); if (Process!=Pid) return Process; else return 0; }