home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PRCTST.ZIP / PRCTST.C
C/C++ Source or Header  |  1992-03-31  |  15KB  |  317 lines

  1. #define PROGRAM       "PROCTEST"         // Program Name
  2.  
  3. #pragma title (PROGRAM " - Test DosQProcStatus")
  4. #pragma linesize(132)
  5. #pragma pagesize(55)
  6.  
  7. /***************************************************************************
  8. **            PROCTEST - Test DosQProcStatus API under OS/2 2.0.          **
  9. ***************************************************************************/
  10. #define  INCL_DOS                      // Include Base OS/2 Calls
  11. #define  INCL_DOSERRORS                // Include Base OS/2 Errors
  12. #include <os2.h>                       // Standard OS/2 Definitions
  13. #include <stdio.h>                     // C/2 Standard I/O Defs
  14. #include <stdlib.h>                    // C/2 Standard Library
  15. #include <string.h>                    // C/2 Standard String Defs
  16.  
  17. #define  BUFFERSIZE  (65535)           // Buffer size
  18.  
  19. #pragma subtitle ("Define Headers and stuff")
  20. #pragma page()
  21. /**************************************************************************
  22. **           The story so far on the DosQProcStatus dataarea             **
  23. **************************************************************************/
  24.  
  25. /* #pragma linkage( DosQProcStatus, far16 pascal ) */
  26. USHORT APIENTRY DosQProcStatus( PVOID pvBuf, USHORT cbBuf );
  27.  
  28. #define PROCESS_END_INDICATOR   3      // Indicates end of process structs
  29.  
  30. #pragma pack(1)
  31.  
  32. typedef struct _SUMMARY                // Size = 12
  33. {
  34.     ULONG   ulThreadCount;             // Number of threads in system
  35.     ULONG   ulProcessCount;            // Number of processes in system
  36.     ULONG   ulModuleCount;             // Number of modules in system
  37. } SUMMARY, *PSUMMARY;
  38.  
  39. typedef struct _THREADINFO             // Size = 28
  40. {
  41.     UCHAR   uchDontKnow1;              //
  42.     UCHAR   uchDontKnow2;              //
  43.     USHORT  usDontKnow3;               //
  44.     USHORT  tidWithinProcess;          // TID within process (TID is 4 bytes!!)
  45.     USHORT  tidWithinSystem;           // TID within system
  46.     ULONG   ulBlockId;                 // Block ID (?)
  47.     USHORT  usPriority;                // Priority
  48.     USHORT  usDontKnow4;               //
  49.     USHORT  usDontKnow5;               //
  50.     USHORT  usDontKnow6;               //
  51.     USHORT  usDontKnow7;               //
  52.     USHORT  usDontKnow8;               //
  53.     USHORT  usThreadStatus;            // 2=blocked or ready, 5=running
  54.     USHORT  usDontKnow9;               //
  55. } THREADINFO, *PTHREADINFO;
  56.  
  57. typedef struct _PROCESSINFO            // Size = 56
  58. {
  59.     ULONG       ulEndIndicator;        // 1 means not end, 3 means last entry
  60.     PTHREADINFO ptiFirst;              // Address of the 1st Thread Control Blk
  61.     USHORT      pid;                   // Process ID (2 bytes - PID is 4 bytes)
  62.     USHORT      pidParent;             // Parent's process ID
  63.     USHORT      usDontKnow2;           //
  64.     USHORT      usDontKnow3;           //
  65.     USHORT      usDontKnow4;           //
  66.     USHORT      usDontKnow5;           //
  67.     USHORT      idSession;             // Session ID
  68.     USHORT      usDontKnow6;           //
  69.     USHORT      hModRef;               // Module handle of EXE
  70.     USHORT      usThreadCount;         // Number of threads in this process
  71.     USHORT      usSessionType;         // Session type (SSF_TYPE_xx)
  72.     CHAR        achDontKnow7[ 6 ];     //
  73.     USHORT      usThreadIdCount;       // Number of USHORTs in Thread id table?
  74.     USHORT      usModCount;            // Number of USHORTs in ModHandle table?
  75.     USHORT      usUshortCount;         // Number of USHORTs in Ushort table?
  76.     USHORT      usDontKnow8;           //
  77.     ULONG       ulThreadIdTableAddr;   // Maybe the address of a TID table
  78.     ULONG       ulModHandleTableAddr;  // Address of a ModHandle table (DLLS?)
  79.     ULONG       ulUshortTableAddr;     // Address of a table of USHORTs (?)
  80. } PROCESSINFO, *PPROCESSINFO;
  81.  
  82. typedef struct _SEMINFO                // Size = 19 + length of name
  83. {
  84.     struct _SEMINFO *pNext;            // Pointer to next block
  85.     USHORT   idOwningThread;           // ID of owning thread?
  86.     USHORT   fsFlags;                  // (MSB-LSB)
  87.     UCHAR    uchReferenceCount;        // Number of references
  88.     UCHAR    uchRequestCount;          // Number of requests
  89.     CHAR     achDontKnow1[ 6 ];        // Unknown but was 7
  90.     USHORT   usIndex;                  // Index (?)
  91.     CHAR     szSemName[ 1 ];           // ASCIIZ semaphore name
  92. } SEMINFO, *PSEMINFO;
  93.  
  94. typedef struct _SHRMEMINFO             // Size = 11 + length of name
  95. {
  96.     struct _SHRMEMINFO *pNext;         // Pointer to next block
  97.     USHORT      usMemHandle;           // Shared memory handle (?)
  98.     SEL         selMem;                // Selector
  99.     USHORT      usReferenceCount;      // Number of references
  100.     CHAR        szMemName[ 1 ];        // ASCIIZ shared memory name
  101. } SHRMEMINFO, *PSHRMEMINFO;
  102.  
  103. typedef struct _MODINFO                // Size = 26 + length of name + 1
  104. {
  105.     struct _MODINFO *pNext;            // Pointer to next block
  106.     USHORT   hMod;                     // Module handle
  107.     USHORT   usModType;                // Module type (0=16bit,1=32bit)
  108.     ULONG    ulModRefCount;            // Count of module references
  109.     ULONG    ulSegmentCount;           // Number of segments in module
  110.     ULONG    ulDontKnow1;              //
  111.     PSZ      szModName;                // Addr of fully qualified module name                                   
  112.     USHORT   usModRef[ 1 ];            // Handles of module references
  113. } MODINFO, *PMODINFO;
  114.  
  115. typedef struct _BUFFHEADER             // Size = 32
  116. {
  117.     PSUMMARY        psumm;             // Pointer to SUMMARY section
  118.     PPROCESSINFO    ppi;               // Pointer to PROCESS section
  119.     PSEMINFO        psi;               // Pointer to SEMAPHORE section
  120.     PVOID           pDontKnow1;        //
  121.     PSHRMEMINFO     psmi;              // Pointer to SHARED MEMORY section
  122.     PMODINFO        pmi;               // Pointer to MODULE section
  123.     PVOID           pDontKnow2;        //
  124.     PVOID           pDontKnow3;        //
  125. } BUFFHEADER, *PBUFFHEADER;
  126.  
  127. #pragma pack()
  128.  
  129. #pragma subtitle ("Define Static & Global Data Areas")
  130. #pragma page()
  131. /**********************************************************************
  132. **                     Define Static Data Areas                      **
  133. **********************************************************************/
  134. static CHAR const szEyePopper[] = PROGRAM;
  135.  
  136. #pragma subtitle ("Display Modules Section")
  137. #pragma page()
  138. /**********************************************************************
  139. **                       Display Modules Section                     **
  140. **********************************************************************/
  141. VOID DisplayModules (PMODINFO pMod)
  142.   {
  143.   PMODINFO pTmp;                       // Temp ptr
  144.  
  145.     printf("\n\tModules Section...: %p\n", pMod);
  146.     pTmp = pMod;                       // Get ptr
  147.     while (pTmp->pNext)                // Display Modules
  148.       {
  149.         SELECTOROF(pTmp->szModName) = SELECTOROF(pTmp);  // Fix selector
  150.         SELECTOROF(pTmp->pNext)     = SELECTOROF(pTmp);  // Fix selector
  151.         printf("\t%p ModName: %s\n", pTmp, pTmp->szModName);
  152.         pTmp = pTmp->pNext;                    // Get next ctl block
  153.       }
  154.   }
  155.  
  156. #pragma subtitle ("Display Shared Memory Section")
  157. #pragma page()
  158. /**********************************************************************
  159. **                    Display Shared Memory Section                  **
  160. **********************************************************************/
  161. VOID DisplayShareMem (PSHRMEMINFO pMem)
  162.   {
  163.   PSHRMEMINFO pTmp;                    // Temp ptr
  164.  
  165.     printf("\n\tShared Memory Section...: %p\n", pMem);
  166.     pTmp = pMem;                       // Get ptr
  167.     while (pTmp->pNext)                // Display Shared Memory
  168.       {
  169.         SELECTOROF(pTmp->pNext) = SELECTOROF(pMem);    // Fix selector
  170.         printf("\t%p MemName: %s\n", pTmp, pTmp->szMemName);
  171.         pTmp = pTmp->pNext;            // Get next ctl block
  172.       }
  173.   }
  174.  
  175. #pragma subtitle ("Display Semaphore Section")
  176. #pragma page()
  177. /**********************************************************************
  178. **                      Display Semaphore Section                    **
  179. /*********************************************************************/
  180. VOID DisplaySemaphore (PSEMINFO pSem)
  181.   {
  182.   PSEMINFO pTmp;                       // Temp semaphore
  183.  
  184.     OFFSETOF(pSem) += 16;              // This must be a bug!
  185.     printf("\n\tSemaphore Section...: %p\n", pSem);
  186.     pTmp = pSem;                       // Need temp ptr due to strangeness
  187.     while (pTmp->pNext)                // Display Semaphores
  188.       {
  189.         SELECTOROF(pTmp->pNext) = SELECTOROF(pSem);    // Fix selector
  190.         printf("\t%p SemName: %s\n", pTmp, pTmp->szSemName);
  191.         pTmp = pTmp->pNext;            // Get next ctl block
  192.       }
  193.   }
  194.  
  195. #pragma subtitle ("Display Process Section")
  196. #pragma page()
  197. /**********************************************************************
  198. **                       Display Process Section                     **
  199. /*********************************************************************/
  200. VOID DisplayProcess (PPROCESSINFO pProc)
  201.   {
  202.   PTHREADINFO pThread;                 // Thread pointer
  203.   USHORT   i;                          // Index
  204.  
  205.     printf("\n\tProcess Section...: %p\n", pProc);
  206.     while(pProc->ulEndIndicator != PROCESS_END_INDICATOR)
  207.       {
  208.         printf("\n\t%p Pid=%04d Ppid=%04d Sess=%02d hMod=%04X Tids=%02d Type=%04d\n",
  209.             pProc, pProc->pid, pProc->pidParent, pProc->idSession,
  210.             pProc->hModRef, pProc->usThreadCount, pProc->usSessionType);
  211.  
  212.         SELECTOROF(pProc->ptiFirst) = SELECTOROF(pProc);   // Fix selector
  213.         pThread = pProc->ptiFirst;     // Get first Thread address
  214.  
  215.         for (i=0; i < pProc->usThreadCount; i++)   // Loop thru threads
  216.           {
  217.             printf("\t\t%p Tid=%04d SysTid=%04d\n", pThread,
  218.                 pThread->tidWithinProcess, pThread->tidWithinSystem);
  219.             pThread++;                 // Bump to next Thread
  220.           }
  221.         pProc = (PPROCESSINFO) pThread;    // Next Process follows Thread
  222.       }
  223.   }
  224.  
  225. #pragma subtitle ("Display Summary Section")
  226. #pragma page()
  227. /**********************************************************************
  228. **                       Display Summary Section                     **
  229. /*********************************************************************/
  230. VOID DisplaySummary (PSUMMARY pSum)
  231.   {
  232.     printf("\n\tSummary Section:\n");
  233.     printf("\tNumber of threads in system.....: %ld\n", pSum->ulThreadCount);
  234.     printf("\tNumber of processes in system...: %ld\n", pSum->ulProcessCount);
  235.     printf("\tNumber of modules in system.....: %ld\n", pSum->ulModuleCount);
  236.   }
  237.  
  238. #pragma subtitle ("Display Buffer Header")
  239. #pragma page()
  240. /**********************************************************************
  241. **                        Display Buffer Header                      **
  242. /*********************************************************************/
  243. VOID DisplayBuffHeader (PBUFFHEADER pBufHdr)
  244.   {
  245.     /* Fix screwed-up selector values in buffer header */
  246.     SELECTOROF(pBufHdr->psumm) = SELECTOROF(pBufHdr);  // Fix selector
  247.     SELECTOROF(pBufHdr->ppi)   = SELECTOROF(pBufHdr);  // Fix selector
  248.     SELECTOROF(pBufHdr->psi)   = SELECTOROF(pBufHdr);  // Fix selector
  249.     SELECTOROF(pBufHdr->psmi)  = SELECTOROF(pBufHdr);  // Fix selector
  250.     SELECTOROF(pBufHdr->pmi)   = SELECTOROF(pBufHdr);  // Fix selector
  251.  
  252.     printf("\n\tBuffer header...........: %p\n", pBufHdr);
  253.     printf("\tSummary Pointer.........: %p\n", pBufHdr->psumm);
  254.     printf("\tProcess Pointer.........: %p\n", pBufHdr->ppi);
  255.     printf("\tSemaphore Pointer.......: %p\n", pBufHdr->psi);
  256.     printf("\tShared Memory Pointer...: %p\n", pBufHdr->psmi);
  257.     printf("\tModule Pointer..........: %p\n", pBufHdr->pmi);
  258.   }
  259.  
  260. #pragma subtitle ("Main Program")
  261. #pragma page()
  262. /**********************************************************************
  263. **                           Main Entry Point                        **
  264. /*********************************************************************/
  265. USHORT main (USHORT argc, PCHAR argv[])
  266.   {
  267.   USHORT      rc = NO_ERROR;           // Return Code
  268.   USHORT      cbBuffer = BUFFERSIZE;   // Buffer length
  269.   PBUFFHEADER pBuf;                    // Buffer Header pointer
  270.   FILE        *fpTest;                 // File pointer
  271.  
  272.     OFFSETOF(pBuf) = 0;                // Set ptr offset to zero
  273.     if ((rc = DosAllocSeg(cbBuffer, &SELECTOROF(pBuf), 0))) // Failed
  274.       {
  275.         printf("\a%s: Buffer allocation failed! RC=%d\n", PROGRAM, rc);
  276.         return (rc);                   // Error exit
  277.       }
  278.  
  279.     memset(pBuf, 0, cbBuffer);         // Zero memory for debugging
  280.     rc = DosQProcStatus(pBuf, cbBuffer);   // Get process data
  281.  
  282.     if (rc == NO_ERROR)                // Continue
  283.       {
  284.         printf("%s: Size of buffer = %u\n", PROGRAM, cbBuffer);
  285.  
  286.         if ((fpTest = fopen("PROCTEST.DAT", "wb"))) // File open
  287.           {
  288.             if (fwrite(pBuf, cbBuffer, 1, fpTest) == 1) // Write successful
  289.               {
  290.                 DisplayBuffHeader(pBuf);       // Display header
  291.                 DisplaySummary(pBuf->psumm);   // Display Summary section
  292.                 DisplayProcess(pBuf->ppi);     // Display Process section
  293.                 DisplaySemaphore(pBuf->psi);   // Display Semaphore section
  294.                 DisplayShareMem(pBuf->psmi);   // Display SharedMem section
  295.                 DisplayModules(pBuf->pmi);     // Display Modules section
  296.               }
  297.             else                       // Write failed
  298.               {
  299.                 rc = _doserrno;            // Get OS/2 Error
  300.                 printf("\a%s: Test file write failed, RC=%d\n", PROGRAM, rc);
  301.               }
  302.             fclose(fpTest);            // Close output file
  303.           }
  304.         else                           // fopen() failed
  305.           {
  306.             rc = _doserrno;            // Get OS/2 error
  307.             printf("\a%s: Test file open failed, RC=%d\n", PROGRAM, rc);
  308.           }
  309.       }
  310.     else                               // DosQProcStatus failed
  311.       {
  312.         printf("\a%s: DosQProcStatus failed, RC=%d\n", PROGRAM, rc);
  313.       }
  314.  
  315.     return rc;                         // Return To OS/2
  316.   }
  317.