home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / varie / jstatus / source / jstatus.c < prev   
Encoding:
C/C++ Source or Header  |  2000-01-08  |  11.2 KB  |  342 lines

  1. /* JStatus: extended Status command, prints all process and which they are
  2.             threaded from ... */
  3.  
  4. /* main.c
  5.    $VER: JStatus.c 1.3 (21.3.98) ©1998 by Matthias Faust
  6. */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/execbase.h>
  10. #include <exec/nodes.h>
  11. #include <exec/lists.h>
  12. #include <exec/libraries.h>
  13. #include <exec/semaphores.h>
  14. #include <exec/memory.h>
  15.  
  16. #include <dos/rdargs.h>
  17. #include <dos/dosextens.h>
  18. #include <dos/dos.h>
  19.  
  20. #include <utility/hooks.h>
  21. #include <utility/tagitem.h>
  22.  
  23. #include <proto/utility.h>
  24.  
  25. #include <powerup/ppclib/interface.h>
  26. #include <powerup/ppclib/message.h>
  27. #include <powerup/ppclib/tasks.h>
  28. #include <powerup/PPCLib/object.h>
  29. #include <powerup/proto/ppc_gcc.h>
  30.  
  31. #include "compiler.h"
  32. #include "Hook_utility.h"
  33.  
  34. #define TEMPLATE ("PROCESS/N,FULL/S,PCB=TCB/S,WB=ALL/S,EWILL=E/S,COM=COMMAND/K,NP=NOPPC/S,S=STATE/K")
  35. char __aligned *version="$VER: JStatus 1.4 (12.12.99) ©1998 by Matthias Faust";
  36.  
  37. #define STATE_HALT  "HALT"
  38. #define STATE_CONT  "CONT"
  39. #define STATE_BREAK "STOP"
  40. #define JS_SM       "« JStatus Halter »"
  41. #define JS_NOMEM    "No memory !"
  42. #define JSS_HALT    "Halt"
  43. #define JSS_RUN     "Run "
  44. #define TS_HALT     8
  45.  
  46. typedef long LONGBOOL;
  47.  
  48. struct JS_Args
  49. {
  50.     ULONG *process;
  51.     LONGBOOL full;
  52.     LONGBOOL tcb;
  53.     LONGBOOL all;
  54.     LONGBOOL eprog;
  55.     STRPTR  com;
  56.     LONGBOOL noppc;
  57.     STRPTR  state;
  58. };
  59.  
  60. struct JS_Args jsa;
  61. struct Hook ViewHook;
  62. struct Library *PPCLibBase=NULL;
  63.  
  64. ULONG tasks=0;
  65.  
  66. BOOL names=TRUE;
  67.  
  68. long __oslibversion=37; /* OS 2.0 required */
  69.  
  70. char *GetProcName(struct Process *pr);
  71. void GetTCB(struct Process *pr);
  72. BOOL iseprogram(struct Process *pr);
  73. void printPPCTasks();
  74. void __saveds ASM HookFunc(register __a0 struct Hook   *VHook GNUCREG(a0),
  75.                            register __a2 void          *MyTask GNUCREG(a2),
  76.                            register __a1 void          *MyMessage GNUCREG(a1));
  77.  
  78. int main()
  79. {
  80.     struct RDArgs *rd=NULL;
  81.     struct ExecBase *sb=NULL;
  82.     struct Task *tc,*mytask;
  83.     struct Process *pr;
  84.     struct List *TaskList,*lists[3];
  85.     struct MsgPort *mp;
  86.     ULONG  ioerr=0,i=0,start=1,end=1;
  87.     BOOL   notfound=FALSE,hasppc=FALSE,error=TRUE;
  88.  
  89.     memset(&jsa,0,sizeof(struct JS_Args));
  90.     memset(&ViewHook,0,sizeof(struct Hook));
  91.  
  92.     if (!(UtilityBase=(struct UtilityBase *)OpenLibrary("utility.library",37)))
  93.         {
  94.             Printf("Can`t open `utility.library' V37++ !\n");
  95.             exit(0);
  96.         }
  97.  
  98.     if (hasppc=((PPCLibBase=(struct Library *)OpenLibrary("ppc.library",0))!=NULL))
  99.         {
  100.             InstallHook(&ViewHook,HookFunc,PPCLibBase);
  101.         }
  102.  
  103.     if (rd=(struct RDArgs *)ReadArgs(TEMPLATE,&jsa,NULL))
  104.         {
  105.             jsa.tcb|=jsa.full; /* If FULL then also TCB */
  106.             names&=!jsa.tcb;   /* If no TCB then print names */
  107.             names|=jsa.full;   /* If FULL then print also names */
  108.             names&=((jsa.com)==NULL); /* If no command given then print no name */
  109.             jsa.all&=((jsa.process)==NULL); /* If process number is given then print no list */
  110.             jsa.all&=((jsa.com)==NULL);
  111.  
  112.             Forbid();
  113.  
  114.             sb=(*((struct ExecBase **) 4));
  115.             lists[0]=&sb->TaskWait;
  116.             lists[1]=&sb->TaskReady;
  117.             lists[2]=NULL;
  118.  
  119.             /* First print all CLI processes */
  120.  
  121.             start=(jsa.process)?(*jsa.process):1;
  122.             end=(jsa.process)?(*jsa.process)+1:MaxCli();
  123.  
  124.             if (jsa.state&&*jsa.process)
  125.                 {
  126.                     if (Stricmp(jsa.state,(STRPTR)STATE_HALT)==0)
  127.                         {
  128.                             if (!(pr=(struct Process *)FindCliProc(*jsa.process))) goto notexist;
  129.                             pr->pr_Task.tc_State=TS_HALT;
  130.                         }
  131.                     else if (Stricmp(jsa.state,(STRPTR)STATE_CONT)==0)
  132.                         {
  133.                             if (!(pr=(struct Process *)FindCliProc(*jsa.process))) goto notexist;
  134.                           runtask:
  135.                             mytask=(struct Task *)FindTask(0);
  136.                             pr->pr_Task.tc_State=TS_READY; // This is quite senseless, but safe
  137.                             Remove(pr);
  138.                             Insert(&sb->TaskWait,pr,mytask->tc_Node); //Insert behind me
  139.  
  140.                         }
  141.                     else if (Stricmp(jsa.state,(STRPTR)STATE_BREAK)==0)
  142.                         {
  143.                             if (pr=(struct Process *)FindCliProc(*jsa.process))
  144.                                 {
  145.                                     Signal(pr,SIGBREAKF_CTRL_C);
  146.                                     goto runtask;
  147.                                 }
  148.                         }
  149.                     else
  150.                         {
  151.                             Printf("Unknown state `%s' !\n",jsa.state);
  152.                         }
  153.                 }
  154.             else
  155.                 {
  156.                     for (i=start;i<end;i++)
  157.                         {
  158.                             if (pr=(struct Process *)FindCliProc(i))
  159.                                 {
  160.                                     if (!jsa.com)
  161.                                         {
  162.                                             if (!iseprogram(pr)) Printf("CLI-Process %2ld: ",i);else Printf("E program ? %2ld: ",i);
  163.                                             if (jsa.tcb) GetTCB(pr);
  164.                                             if (names) Printf("%sName: %s",(jsa.full)?", ":" ",GetProcName(pr));
  165.                                             Printf("\n");
  166.                                         }
  167.                                     else
  168.                                         {
  169.                                             if (Stricmp(GetProcName(pr),jsa.com)==NULL)
  170.                                                 {
  171.                                                     Printf(" %ld\n",i);
  172.                                                 }
  173.                                                 else notfound=TRUE;
  174.                                         }
  175.                                 }
  176.                             else
  177.                                 {
  178.                                   notexist:
  179.                                     if (jsa.process) Printf("Process %ld does not exist\n",i);
  180.                                 }
  181.                         }
  182.                 }
  183.  
  184.         if (jsa.all)
  185.             {
  186.                 i=0;
  187.                 Printf("\nSystem processes:\n");
  188.                 Printf("*****************\n");
  189.  
  190.                 while (lists[i])
  191.                     {
  192.                     TaskList=(struct List *)lists[i];
  193.                     tc=(struct Task *)TaskList->lh_Head;
  194.  
  195.                     while (tc->tc_Node.ln_Succ)
  196.                         {
  197.                             if (tc->tc_Node.ln_Type==NT_PROCESS)
  198.                                 {
  199.                                     pr=(struct Process *)tc;
  200.                                     if (!pr->pr_TaskNum)
  201.                                         {
  202.                                             if (!iseprogram(pr)) Printf("%16lc",0x20);else Printf("E program ?   ");
  203.                                             if (jsa.tcb) GetTCB(pr);
  204.                                             if (names) Printf("%sName: %s",(jsa.full)?", ":" ",GetProcName(pr));
  205.                                             Printf("\n");
  206.                                         }
  207.                                 }
  208.                             tc=(struct Task *)tc->tc_Node.ln_Succ;
  209.                         }
  210.                         i++;
  211.                     }
  212.  
  213.                 ioerr=0;
  214.             }
  215.         firstexit:
  216.             Permit();
  217.  
  218.             /* I can currently guess how long this will delay, but I am sure,
  219.                if I didn`t Permit() -> Nice to meet you Mr.Guru.... */
  220.  
  221.             if ((hasppc)&&!jsa.noppc&&jsa.all)) printPPCTasks();
  222.  
  223.             FreeArgs(rd);
  224.         }
  225.     else
  226.         {
  227.             ioerr=IoErr();
  228.         }
  229.  
  230.     CloseLibrary(UtilityBase);
  231.     if (PPCLibBase) CloseLibrary(PPCLibBase);
  232.     SetIoErr(ioerr);
  233.     if (notfound) exit(RETURN_WARN);else exit((ioerr)?RETURN_FAIL:RETURN_OK);
  234. }
  235.  
  236. char *GetProcName(struct Process *pr)
  237. {
  238.     char *name;
  239.  
  240.     struct CommandLineInterface *cli;
  241.  
  242.     if (pr->pr_CLI)
  243.         {
  244.             cli=BADDR(pr->pr_CLI);
  245.             if (cli->cli_CommandName) name=(char *)BADDR(cli->cli_CommandName)+1; /* BSTR x: x[0] = length;x[1..255]=str;x>>2 */
  246.         }
  247.     else
  248.         {
  249.             name=(char *)pr->pr_Task.tc_Node.ln_Name;
  250.         }
  251.  
  252.     return(((name)&&(strlen(name)))?name:"(no command)");
  253. }
  254.  
  255. BOOL iseprogram(struct Process *pr)
  256. {
  257.     ULONG stacksize;
  258.     long usedstack,*globvec;
  259.  
  260.     stacksize=pr->pr_Task.tc_SPUpper-pr->pr_Task.tc_SPLower;
  261.     usedstack=pr->pr_Task.tc_SPUpper-pr->pr_Task.tc_SPReg;
  262.  
  263.     return(!((usedstack>-1)&&(usedstack<=stacksize))&&jsa.eprog);
  264. }
  265.  
  266. void GetTCB(struct Process *pr)
  267. {
  268.     ULONG stacksize;
  269.     long usedstack,*globvec;
  270.  
  271.     stacksize=pr->pr_Task.tc_SPUpper-pr->pr_Task.tc_SPLower;
  272.     usedstack=pr->pr_Task.tc_SPUpper-pr->pr_Task.tc_SPReg;
  273.     globvec=(pr->pr_GlobVec)?pr->pr_GlobVec:NULL;
  274.     if (!jsa.full) Printf("Addr: 0x%08lx, ",pr);
  275.     if ((usedstack>-1)&&(usedstack<=stacksize)) Printf("Stk: %-4ld of %-5ld, ",usedstack,stacksize);
  276.     else (jsa.eprog)?Printf("Has own stack.%4lc, ",0x20):Printf("Stk: %-4ld (unused), ",stacksize);
  277.     if (globvec) Printf("GV: %3ld, ",*globvec);else Printf("no GV%4lc, ",0x20);
  278.     Printf("Pri: %3ld, ",pr->pr_Task.tc_Node.ln_Pri);
  279.     Printf("St: %s",(pr->pr_Task.tc_State==TS_HALT)?(STRPTR)JSS_HALT:(STRPTR)JSS_RUN);
  280. }
  281.  
  282. /* Based on Laires PPCShowTasks.c */
  283.  
  284. void printPPCTasks()
  285. {
  286.     struct TagItem Tags[3];
  287.  
  288.     ViewHook.h_Entry    =(ULONG (*)(void)) HookFunc;
  289.     ViewHook.h_SubEntry =(ULONG (*)(void)) NULL;
  290.     ViewHook.h_Data     =(APTR) PPCLibBase;
  291.  
  292.     Tags[0].ti_Tag  =PPCTASKINFOTAG_ALLTASK;
  293.     Tags[0].ti_Data =TRUE;
  294.     Tags[1].ti_Tag  =PPCTASKINFOTAG_HOOK;
  295.     Tags[1].ti_Data =(ULONG) &ViewHook;
  296.     Tags[2].ti_Tag  =TAG_DONE;
  297.  
  298.     PPCGetTaskAttrs(NULL,(struct TagItem*)&Tags);
  299. }
  300.  
  301. void __saveds ASM HookFunc(register __a0 struct Hook   *VHook GNUCREG(a0),
  302.                            register __a2 void          *MyTask GNUCREG(a2),
  303.                            register __a1 void          *MyMessage GNUCREG(a1))
  304. {
  305.     struct Library  *PPCLibBase;
  306.     struct TagItem  Tags[3];
  307.     char            *Name;
  308.     int             Pri;
  309.     ULONG           StackSize;
  310.  
  311.       PPCLibBase    =       (struct Library*) VHook->h_Data;
  312.  
  313.       Tags[1].ti_Tag        =       TAG_END;
  314.       Tags[0].ti_Tag        =       PPCTASKINFOTAG_NAME;
  315.       Name                  =       (char*) PPCGetTaskAttrs(MyTask,(struct TagItem*) &Tags);
  316.  
  317.       Tags[0].ti_Tag        =       PPCTASKINFOTAG_PRIORITY;
  318.       Pri                   =       PPCGetTaskAttrs(MyTask,(struct TagItem*) &Tags);
  319.  
  320.       Tags[0].ti_Tag        =       PPCTASKINFOTAG_STACKSIZE;
  321.       StackSize             =       PPCGetTaskAttrs(MyTask,(struct TagItem*) &Tags);
  322.  
  323.       tasks++;
  324.  
  325.       if (tasks==1) Printf("\nPPC tasks:\n"\
  326.                            "**********\n");
  327.  
  328.       if (tasks)
  329.           {
  330.               Printf("%16lc",0x20);
  331.               if (jsa.tcb)
  332.                   {
  333.                       if (!jsa.full) Printf("Addr: %08lx, ",MyTask);
  334.                       Printf("Stk: %-5ld, Num: %ld, Pri: %ld",StackSize,tasks,Pri);
  335.                   }
  336.  
  337.               if (names) Printf("%sName: %s",(jsa.full)?", ":" ",Name);
  338.               Printf("\n");
  339.           }
  340.  
  341. }
  342.