home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / QPROC.C < prev    next >
Text File  |  1994-10-19  |  15KB  |  397 lines

  1. #include "all.h"
  2.  
  3. char *GetProcessStatus( ULONG );
  4. char *GetProcessState( ULONG );
  5. char *GetShrMemName( qsMrec_t *, USHORT );
  6. char *GetSysSemName( qsS16Headrec_t *, USHORT );
  7. void  GetSlotOwner( qsPrec_t *, USHORT, USHORT *, char *, ULONG, void *);
  8.  
  9.  #define BUFFER_SIZE 64*1024-1
  10.  
  11. void QueryProcesses( void )
  12. {
  13.  APIRET      rc;
  14.  char        ModuleName[CCHMAXPATH];
  15.  int         n;
  16.  USHORT     *p;
  17.  USHORT      count;
  18.  qsTrec_t   *pt;
  19.  qsS16rec_t *ps;
  20.  qsLrec_t   *pl;
  21.  qsMrec_t   *pm;
  22.  USHORT      SemOwner;
  23.  USHORT     *pmte;
  24.  char       *cp;
  25.  ULONG       sleepid;
  26.  USHORT      semindex;
  27.  USHORT      semid;
  28.  USHORT      slotnum;
  29.  USHORT      tid;
  30.  FILE       *dmp;
  31.  ULONG       flags;
  32.  void       *buffer;
  33.  
  34.  qsGrec_t        *pGlobalRec;           /* ptr to the global data section    */
  35.  qsPrec_t        *pProcRec;             /* ptr to process record section     */
  36.  qsS16Headrec_t  *p16SemRec;            /* ptr to 16 bit sem section         */
  37.  qsMrec_t        *pShrMemRec;           /* ptr to shared mem section         */
  38.  qsLrec_t        *pLibRec;              /* ptr to exe module recrd section   */
  39.  
  40.  
  41.  dmp = fopen("log.dat","w");
  42.  
  43.  
  44.  /****************************************************************************/
  45.  /* Read the status buffer.                                                  */
  46.  /****************************************************************************/
  47.  flags = PAG_COMMIT|PAG_READ|PAG_WRITE|OBJ_TILE;
  48.  DosAllocMem( &buffer,BUFFER_SIZE,flags);
  49.  DosQProcStatus( (ULONG*)buffer , BUFFER_SIZE );
  50.  
  51.  /****************************************************************************/
  52.  /* Define the pointers to the various sections of information.              */
  53.  /****************************************************************************/
  54.  pGlobalRec = (qsGrec_t       *)((qsPtrRec_t*)buffer)->pGlobalRec;
  55.  pProcRec   = (qsPrec_t       *)((qsPtrRec_t*)buffer)->pProcRec;
  56.  p16SemRec  = (qsS16Headrec_t *)((qsPtrRec_t*)buffer)->p16SemRec;
  57.  pShrMemRec = (qsMrec_t       *)((qsPtrRec_t*)buffer)->pShrMemRec;
  58.  pLibRec    = (qsLrec_t       *)((qsPtrRec_t*)buffer)->pLibRec;
  59.  
  60.  /****************************************************************************/
  61.  /* Print the Global Data Section.                                           */
  62.  /****************************************************************************/
  63.  fprintf(dmp,"\nGlobal Data Section\n");
  64.  fprintf(dmp,"\n   Number of Threads in Use = %d",pGlobalRec->cThrds);
  65.  fprintf(dmp,"\n\n===============================================================");
  66.  
  67.  /****************************************************************************/
  68.  /* Print the Process Data Section.                                          */
  69.  /****************************************************************************/
  70.  fprintf(dmp,"\nProcess Data Section\n");
  71.  
  72.  for(;;)
  73.  {
  74.   memset(ModuleName,' ',sizeof(ModuleName) );
  75.   rc = DosQueryModuleName(pProcRec->hMte,sizeof(ModuleName),ModuleName);
  76.   if(rc != 0 )
  77.    fprintf(dmp,"\nDosQueryModuleName() Error rc=%d",rc);
  78.  
  79.   fprintf(dmp,"\n  %s",ModuleName);
  80.   fprintf(dmp,"\n    Process ID                   = %-6d(%#04x)",pProcRec->pid,
  81.                                                             pProcRec->pid);
  82.  
  83.   fprintf(dmp,"\n    Parent Process ID            = %-6d(%#04x)",pProcRec->ppid,
  84.                                                             pProcRec->ppid);
  85.  
  86.   fprintf(dmp,"\n    Process Type                 = %-6d(%#04x)",pProcRec->type,
  87.                                                             pProcRec->type);
  88.  
  89.   cp = GetProcessStatus(pProcRec->stat);
  90.   fprintf(dmp,"\n    Process Status               = %-6d(%#04x) %s",pProcRec->stat,
  91.                                                                pProcRec->stat,
  92.                                                                cp);
  93.  
  94.   fprintf(dmp,"\n    Process Screen Grp           = %-6d(%#04x)",pProcRec->sgid,
  95.                                                             pProcRec->sgid);
  96.  
  97.   fprintf(dmp,"\n    Process Mte                  = %-6d(%#04x)",pProcRec->hMte,
  98.                                                             pProcRec->hMte);
  99.  
  100.   fprintf(dmp,"\n    Number of TCBs               = %d",pProcRec->cTCB);
  101.   fprintf(dmp,"\n    Number of 16 bit Sema4s      = %d",pProcRec->c16Sem);
  102.   fprintf(dmp,"\n    Number of Runtime Libs       = %d",pProcRec->cLib);
  103.   fprintf(dmp,"\n    Number of Shared Mem Handles = %d",pProcRec->cShrMem);
  104.  
  105.   /****************************************************************************/
  106.   /* Define the pointers to the sema4,lib, and shared memory blocks for       */
  107.   /* this process.                                                            */
  108.   /****************************************************************************/
  109.   count = pProcRec->cLib;
  110.   if(count != 0)
  111.   {
  112.    fprintf(dmp,"\n\n    DLL Handles/Names");
  113.    for(p=pProcRec->pLibRec,n=1; n<=count ;n++,p++)
  114.    {
  115.     strcpy(ModuleName,"Invalid");
  116.     DosQueryModuleName(*p,sizeof(ModuleName),ModuleName);
  117.     fprintf(dmp,"\n      %-6d(%04x) %s",*p,*p,ModuleName);
  118.    }
  119.   }
  120.  
  121.   count = pProcRec->c16Sem;
  122.   if(count != 0)
  123.   {
  124.    fprintf(dmp,"\n\n    16 Bit Sema4 Indices\n");
  125.    for(p=pProcRec->p16SemRec,n=1; n<=count ;n++,p++)
  126.     fprintf(dmp,"\n      %-6d(%#04x) %s",*p,*p,"xxxxx");
  127.   }
  128.  
  129.   count = pProcRec->cShrMem;
  130.   if(count != 0)
  131.   {
  132.    fprintf(dmp,"\n\n    Shared Memory Handles/Names\n");
  133.    for(p=pProcRec->pShrMemRec,n=1; n<=count ;n++,p++)
  134.    {
  135.     cp = GetShrMemName(pShrMemRec,*p);
  136.     fprintf(dmp,"\n      %-6d(%04x) %s",*p,*p,cp);
  137.    }
  138.   }
  139.   fprintf(dmp,"\n");
  140.  
  141.  
  142.   /***************************************************************************/
  143.   /* Now, print the thread info for this process.                            */
  144.   /***************************************************************************/
  145.   fprintf(dmp,"\n\n    Thread Info\n");
  146.   fprintf(dmp,"\n      tid   slot   sleepid  priority systime  usertime state");
  147.   fprintf(dmp,"\n      (hex) (hex)  (hex)    (hex)    (hex)    (hex)    (hex)");
  148.  
  149.   count = pProcRec->cTCB;
  150.   pt = pProcRec->pThrdRec;
  151.   for(n=1; n <= count; n++ )
  152.   {
  153.    fprintf(dmp,"\n      %04x  ",pt->tid);
  154.    fprintf(dmp,"%04x   " ,pt->slot);
  155.    fprintf(dmp,"%08x "   ,pt->sleepid);
  156.    fprintf(dmp,"%08x "   ,pt->priority);
  157.    fprintf(dmp,"%08x "   ,pt->systime);
  158.    fprintf(dmp,"%08x "   ,pt->usertime);
  159.    fprintf(dmp,"%08x %s" ,pt->state,GetProcessState(pt->state));
  160.    pt = (qsTrec_t*)( (char*)pt + sizeof(qsTrec_t) );
  161.   }
  162.   fprintf(dmp,"\n");
  163.   /***************************************************************************/
  164.   /* Print the name of the sema4 a thread is blocked on.                     */
  165.   /***************************************************************************/
  166.   count = pProcRec->cTCB;
  167.   pt = pProcRec->pThrdRec;
  168.   for(n=1; n <= count; n++ )
  169.   {
  170.    sleepid  = pt->sleepid;
  171.    semindex = *(USHORT*)( (char*)&sleepid + 2);
  172.    switch( semindex )
  173.    {
  174.     case 0x400:
  175.      semid    = *(USHORT*)&sleepid;
  176.      cp =  GetSysSemName( p16SemRec ,semid);
  177.      fprintf(dmp,"\n      tid=%04x is blocked on system sema4 %s",pt->tid,cp);
  178.      break;
  179.  
  180.     case 0xfffe:
  181.     case 0xfffd:
  182.      slotnum = *(USHORT*)&sleepid;
  183.      memset(ModuleName,' ',sizeof(ModuleName) );
  184.      GetSlotOwner( pProcRec, slotnum, &tid, ModuleName, sizeof(ModuleName),
  185.                        (void*)p16SemRec );
  186.      if( semindex == 0xfffe )
  187.       fprintf(dmp,"\n      tid=%04x is blocked on Ramsem owned by tid=%04x of %s",
  188.                                                                 pt->tid,
  189.                                                                 tid,
  190.                                                                 ModuleName);
  191.      else
  192.       fprintf(dmp,"\n      tid=%04x is blocked on Muxsem owned by tid=%04x of %s",
  193.                                                                  pt->tid,
  194.                                                                  tid,
  195.                                                                  ModuleName);
  196.       break;
  197.  
  198.    }
  199.    pt = (qsTrec_t*)( (char*)pt + sizeof(qsTrec_t) );
  200.   }
  201.   fprintf(dmp,"\n  -------------------------------------------------------------\n");
  202.   /****************************************************************************/
  203.   /* Get a pointer to the next process block and test for past end of block.  */
  204.   /****************************************************************************/
  205.   pProcRec = (qsPrec_t *)( (char*)(pProcRec->pThrdRec) +
  206.                                   (pProcRec->cTCB)*sizeof(qsTrec_t));
  207.  
  208.   if((void*)pProcRec >= (void*)p16SemRec )
  209.    break;
  210.  }
  211.  fprintf(dmp,"\n=================================================================");
  212.  /****************************************************************************/
  213.  /* Print the 16 Bit Semaphore Section.                                      */
  214.  /****************************************************************************/
  215.  fprintf(dmp,"\n16 Bit Semaphore Section\n");
  216.  fprintf(dmp,"\nSema4  Owner  Sema4  Ref    Proc ");
  217.  fprintf(dmp,"\nIndex  Slot   Flag   Count  count");
  218.  fprintf(dmp,"\n(hex)  (hex)  (hex)  (dec)  (dec)\n");
  219.  
  220.  ps = (qsS16rec_t*)( (char*)p16SemRec + sizeof(qsS16Headrec_t) );
  221.  SemOwner = ps->s_SysSemOwner;
  222.  for(; ps != NULL ; ps = (qsS16rec_t*)ps->NextRec)
  223.  {
  224.   if( ps->s_SysSemOwner != 0 )
  225.    SemOwner = ps->s_SysSemOwner;
  226.   fprintf(dmp,"\n%04x   "  ,ps->s_SysSemNameIndex.SemIndex);
  227.   fprintf(dmp,"%04x   "    ,SemOwner);
  228.   fprintf(dmp,"%02x     "  ,ps->s_SysSemFlag);
  229.   fprintf(dmp,"%-#7d"      ,ps->s_SysSemRefCnt);
  230.   fprintf(dmp,"%-#7d"      ,ps->s_SysSemProcCnt);
  231.   fprintf(dmp,"%s"         ,&(ps->s_SysSemNameIndex.SemName.SemName));
  232.  
  233.  }
  234.  fprintf(dmp,"\n=================================================================");
  235.  /****************************************************************************/
  236.  /* Print the Executable Module Section.                                     */
  237.  /****************************************************************************/
  238.  fprintf(dmp,"\n\nExecutable Module Section");
  239.  fprintf(dmp,"\n\nmte  Name\n");
  240.  
  241.  pl = (qsLrec_t*)pLibRec;
  242.  for( ;pl != NULL; pl = pl->pNextRec)
  243.  {
  244.   fprintf(dmp,"\n\n%04X %s",pl->hmte,pl->pName);
  245.   pmte = (USHORT*)&(pl->ImpModTable);
  246.   for(n = 1; n <= (int)pl->ctImpMod; n++,pmte++ )
  247.   {
  248.    fprintf(dmp,"\n%04X",*pmte);
  249.    strcpy(ModuleName,"Invalid");
  250.    DosQueryModuleName(*pmte,sizeof(ModuleName),ModuleName);
  251.    fprintf(dmp,"         %s",ModuleName);
  252.   }
  253.  }
  254.  fprintf(dmp,"\n=================================================================");
  255.  /****************************************************************************/
  256.  /* Print the Shared Memory Section.                                         */
  257.  /****************************************************************************/
  258.  fprintf(dmp,"\n\nShared Memory Section\n");
  259.  fprintf(dmp,"\nMem           Ref        ");
  260.  fprintf(dmp,"\nHandle Sel    Count  Name");
  261.  fprintf(dmp,"\n(hex)  (hex)  (dec)\n");
  262.  
  263.  pm = (qsMrec_t*)pShrMemRec;
  264.  for(; pm != NULL ; pm = (qsMrec_t*)pm->pNextRec)
  265.  {
  266.   fprintf(dmp,"\n%04x   " ,pm->hmem);
  267.   fprintf(dmp,"%04x   "   ,pm->sel);
  268.   fprintf(dmp,"%-#7d"     ,pm->refcnt);
  269.   fprintf(dmp,"%s"        ,&(pm->Memname));
  270.  }
  271.  fclose(dmp);
  272. }
  273.  
  274. /*****************************************************************************/
  275. /* Get the meaning of the status value.                                      */
  276. /*****************************************************************************/
  277. static STATUS statdefs[] =
  278. {
  279.  0x00,"Normal",
  280.  0x01,"Doing ExitList Processing"     ,
  281.  0x02,"Exiting thread 1"              ,
  282.  0x04,"The whole process is exiting"  ,
  283.  0x10,"Parent cares about termination",
  284.  0x20,"Parent did an exec-and-wait"   ,
  285.  0x40,"Process is dying"              ,
  286.  0x80,"Process in embryonic state"    ,
  287.  0xFF,NULL
  288. };
  289.  
  290. char *GetProcessStatus( ULONG status)
  291. {
  292.  int    i;
  293.  ULONG  temp;
  294.  
  295.  for( i=0,temp=statdefs[0].stat;
  296.       (temp != 0xff) && (temp != status);
  297.       temp=statdefs[++i].stat
  298.     ){;}
  299.  return(statdefs[i].statmsg);
  300. }
  301.  
  302. /*****************************************************************************/
  303. /* Get the meaning of the status value.                                      */
  304. /*****************************************************************************/
  305. static STATUS statedefs[] =
  306. {
  307.  0x01, "Ready to run" ,
  308.  0x02, "Blocked"      ,
  309.  0x05, "Running"      ,
  310.  0x09, "Frozen"       ,
  311.  0xFF,NULL
  312. };
  313.  
  314. char *GetProcessState( ULONG state)
  315. {
  316.  int    i;
  317.  ULONG  temp;
  318.  
  319.  for( i=0,temp=statedefs[0].stat;
  320.       (temp != 0xff) && (temp != state);
  321.       temp=statedefs[++i].stat
  322.     ){;}
  323.  return(statedefs[i].statmsg);
  324. }
  325.  
  326. /*****************************************************************************/
  327. /* Get the name of a shared memory segment.                                  */
  328. /*****************************************************************************/
  329. char *GetShrMemName( qsMrec_t *pShrMemRec, USHORT hmem)
  330. {
  331.  qsMrec_t *pm;
  332.  
  333.  pm = pShrMemRec;
  334.  for(; pm != NULL ; pm = (qsMrec_t*)pm->pNextRec )
  335.  {
  336.   if( pm->hmem == hmem)
  337.    return( &(pm->Memname) );
  338.  }
  339.  return(NULL);
  340. }
  341.  
  342. /*****************************************************************************/
  343. /* Get the name of a system semaphore.                                       */
  344. /*****************************************************************************/
  345. char *GetSysSemName( qsS16Headrec_t *p16SemRec, USHORT semid)
  346. {
  347.  qsS16rec_t *ps;
  348.  
  349.  ps = (qsS16rec_t*)( (char*)p16SemRec + sizeof(qsS16Headrec_t) );
  350.  for(; ps != NULL ; ps = (qsS16rec_t*)ps->NextRec)
  351.  {
  352.   if( semid == ps->s_SysSemNameIndex.SemIndex)
  353.    return( &(ps->s_SysSemNameIndex.SemName.SemName) );
  354.  }
  355.  return(NULL);
  356. }
  357.  
  358. /*****************************************************************************/
  359. /* Get the owner of a slot.                                                  */
  360. /*****************************************************************************/
  361. void GetSlotOwner( qsPrec_t *pProcRec,
  362.                        USHORT slotnum ,
  363.                        USHORT *ptid,
  364.                        char   *pModuleName,
  365.                        ULONG   buflen,
  366.                        void   *pend
  367.                  )
  368. {
  369.  qsTrec_t *pt;
  370.  USHORT    count;
  371.  int       n;
  372.  
  373.  for(;;)
  374.  {
  375.   count = pProcRec->cTCB;
  376.   pt = pProcRec->pThrdRec;
  377.   for(n=1; n <= count; n++ )
  378.   {
  379.    if(pt->slot == slotnum)
  380.    {
  381.     *ptid = pt->tid;
  382.     DosQueryModuleName(pProcRec->hMte,buflen,pModuleName);
  383.     return;
  384.    }
  385.    pt = (qsTrec_t*)( (char*)pt + sizeof(qsTrec_t) );
  386.   }
  387.   /****************************************************************************/
  388.   /* Get a pointer to the next process block and test for past end of block.  */
  389.   /****************************************************************************/
  390.   pProcRec = (qsPrec_t *)( (char*)(pProcRec->pThrdRec) +
  391.                                   (pProcRec->cTCB)*sizeof(qsTrec_t));
  392.  
  393.   if((void*)pProcRec >= (void*)pend )
  394.    break;
  395.  }
  396. }
  397.