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

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