home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sherlock.zip / PROCESS.C < prev    next >
C/C++ Source or Header  |  1994-06-29  |  6KB  |  251 lines

  1. /*
  2. **  Sherlock - Copyright 1992, 1993, 1994
  3. **    Harfmann Software
  4. **    Compuserve: 73147,213
  5. **    All rights reserved
  6. */
  7. #define INCL_DOSERRORS
  8. #define INCL_DOSPROCESS
  9. #include <os2.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "debug.h"
  14. #include "debugger.h"
  15. #include "procstat.h"
  16.  
  17. /*
  18. ** Local buffer for the process status.
  19. */
  20. static PBUFFHEADER  buff;
  21. #define BUFF_SIZE   0x8000
  22.  
  23. /*
  24. ** Display the name associated with a module handle.
  25. */
  26. static void DisplayName(USHORT hMod)
  27. {
  28. PMODINFO    pmi = buff->pmi;
  29. int        i;
  30.  
  31.     /*
  32.     ** Find the module requested.
  33.     */
  34.     while(pmi && (hMod != pmi->hMod)) {
  35.     pmi = pmi->pNext;
  36.     }
  37.  
  38.     /*
  39.     ** Sanity check
  40.     */
  41.     if((pmi == NULL) || (hMod != pmi->hMod)) {
  42.     fprintf(logFile, "Error finding module %04x.\n", hMod);
  43.     return;
  44.     }
  45.  
  46.     /*
  47.     ** Dump the information.
  48.     */
  49.     fprintf(logFile, " %04x  %s\n", pmi->hMod, pmi->szModName);
  50. }
  51.  
  52. /*
  53. ** List the status of a thread.
  54. */
  55. static void DisplayThread(PTHREADINFO pThread)
  56. {
  57.     fprintf(logFile, "  %04x",     pThread->tidWithinProcess);
  58.     fprintf(logFile, "   %04x",  pThread->usPriority);
  59.     fprintf(logFile, "   %08x", pThread->ulBlockId);
  60.     switch(pThread->usThreadStatus) {
  61.     case 2:     fprintf(logFile, "   blocked");
  62.             break;
  63.     case 5:     fprintf(logFile, "   running");
  64.             break;
  65.     default:    fprintf(logFile, "   %04x", pThread->usThreadStatus);
  66.             break;
  67.     }
  68.     fprintf(logFile, "  %04x\n", pThread->tidWithinSystem);
  69. }
  70.  
  71. /*
  72. ** List the tasks in the system.
  73. */
  74. static void DisplayTask(PID pid)
  75. {
  76. PPROCESSINFO ppiLocal = buff->ppi;
  77. PTHREADINFO  pThread;
  78. int         i;
  79.  
  80.     while((ppiLocal->ulEndIndicator != PROCESS_END_INDICATOR) &&
  81.       (pid != ppiLocal->pid)) {
  82.  
  83.     /*
  84.     ** Next PROCESSINFO struct found by taking the address of the first
  85.     ** thread control block of the current PROCESSINFO structure and
  86.     ** adding the size of a THREADINFO structure times the number of
  87.     ** threads
  88.     */
  89.     ppiLocal = (PPROCESSINFO) (ppiLocal->ptiFirst+ppiLocal->usThreadCount);
  90.     }
  91.  
  92.     /*
  93.     ** Make sure we found the PID
  94.     */
  95.     if(ppiLocal->ulEndIndicator == PROCESS_END_INDICATOR) {
  96.     fprintf(logFile, "Unable to find PID\n");
  97.     return;
  98.     }
  99.  
  100.     /*
  101.     ** Dump the ProcessInfo struct.
  102.     */
  103.     DisplayName(ppiLocal->hModRef);
  104.  
  105.     /*
  106.     ** Dump the thread status information.
  107.     */
  108.     pThread = ppiLocal->ptiFirst;
  109.     fprintf(logFile, " TID  Priority  Block ID     State  System TID\n");
  110.     for(i=0; i<ppiLocal->usThreadCount; i++)
  111.     DisplayThread(&pThread[i]);
  112.  
  113.     /*
  114.     ** Dump the modules used.
  115.     */
  116.     fprintf(logFile, "  Modules Referenced\n");
  117.     fprintf(logFile, "     ID    Name\n");
  118.     for(i=0; i<ppiLocal->usModCount; i++) {
  119.     fprintf(logFile, "    ");
  120.     DisplayName(ppiLocal->pModHandleTable[i]);
  121.     }
  122.     fprintf(logFile, "\n");
  123. }
  124.  
  125. /*
  126. ** List the tasks in the system.
  127. */
  128. static void ListTasks()
  129. {
  130. PPROCESSINFO ppiLocal = buff->ppi;
  131.  
  132.     fprintf(logFile, "\nTasks\n");
  133.     while(ppiLocal->ulEndIndicator != PROCESS_END_INDICATOR) {
  134.     DisplayTask(ppiLocal->pid);
  135.  
  136.     /*
  137.     ** Next PROCESSINFO struct found by taking the address of the first
  138.     ** thread control block of the current PROCESS
  139.     */
  140.     ppiLocal = (PPROCESSINFO) (ppiLocal->ptiFirst+ppiLocal->usThreadCount);
  141.     }
  142. }
  143.  
  144. /*
  145. ** Display the state of a system semaphore.
  146. */
  147. static DisplaySemaphore(PSEMINFO psi)
  148. {
  149.     fprintf(logFile, "   %04x",      psi->usIndex);
  150.     fprintf(logFile, "  %10d",         psi->uchReferenceCount);
  151.     fprintf(logFile, "  %8d",         psi->uchRequestCount);
  152.     fprintf(logFile, "  %04x",         psi->fsFlags);
  153.     fprintf(logFile, "     \\S%s\n", psi->szSemName);
  154. }
  155.  
  156. /*
  157. ** List the semaphores in the system.
  158. */
  159. static void ListSemaphores()
  160. {
  161. PSEMINFO ppiSem = (PSEMINFO) (((char *) buff->psi) + sizeof(SEMHEADER));
  162.  
  163.     fprintf(logFile, "\nSemaphores\n");
  164.     fprintf(logFile, "  Index  References  Requests  Flag     Name\n");
  165.     while(ppiSem->pNext) {
  166.     DisplaySemaphore(ppiSem);
  167.     ppiSem = ppiSem->pNext;
  168.     }
  169. }
  170.  
  171. /*
  172. ** Display the shared memory
  173. */
  174. static void DisplaySharedMemory(PSHRMEMINFO psmi)
  175. {
  176.     fprintf(logFile, "   %04x ",     psmi->usMemHandle);
  177.     fprintf(logFile, "    %04x  ",   psmi->selMem);
  178.     fprintf(logFile, "     %04x   ", psmi->usReferenceCount);
  179.     fprintf(logFile, "  %s\n",         psmi->szMemName);
  180. }
  181.  
  182. /*
  183. ** List the shared memory in the system.
  184. */
  185. static void ListSharedMemory()
  186. {
  187. PSHRMEMINFO psmi = buff->psmi;
  188.  
  189.     fprintf(logFile, "\nShared Memory\n");
  190.     fprintf(logFile, "  Handle  Selector  References  Shared Memory Name\n");
  191.     while(psmi->pNext) {
  192.     DisplaySharedMemory(psmi);
  193.     psmi = psmi->pNext;
  194.     }
  195. }
  196.  
  197. /*
  198. ** Display the module list
  199. */
  200. static void DisplayModule(PMODINFO pmi)
  201. {
  202. int i;
  203.  
  204.     fprintf(logFile, "  %04x",     pmi->hMod);
  205.     fprintf(logFile, "  %04x",     pmi->usModType);
  206.     fprintf(logFile, "  %8d",     pmi->ulSegmentCount);
  207.     fprintf(logFile, "  %08x",     pmi->ulDontKnow1);
  208.     fprintf(logFile, "  '%s'\n", pmi->szModName);
  209.     fprintf(logFile, "  References:\n");
  210.     fprintf(logFile, "     ID    Name\n");
  211.     for(i=0; i<pmi->ulModRefCount; i++) {
  212.     fprintf(logFile, "    ");
  213.     DisplayName(pmi->usModRef[i]);
  214.     }
  215.     fprintf(logFile, "\n");
  216. }
  217.  
  218. /*
  219. ** List the modules
  220. */
  221. static void ListModules()
  222. {
  223. PMODINFO pmi = buff->pmi;
  224.  
  225.     fprintf(logFile, "\nModules\n");
  226.     fprintf(logFile, " Module Type  SegCnt    Unknown   Name\n");
  227.     fprintf(logFile, "\n");
  228.     while(pmi) {
  229.     DisplayModule(pmi);
  230.     pmi = pmi->pNext;
  231.     }
  232. }
  233.  
  234. /*
  235. ** Dump pstat info
  236. */
  237. void dumpPStat()
  238. {
  239. return;
  240.     buff = malloc(BUFF_SIZE);
  241.     if(DosQProcStatus(buff, BUFF_SIZE) == 0) {
  242.     fprintf(logFile, "==================================================================\n");
  243.     ListTasks();
  244.     ListSemaphores();
  245.     ListSharedMemory();
  246.     ListModules();
  247.     fprintf(logFile, "==================================================================\n");
  248.     }
  249.     free(buff);
  250. }
  251.