home *** CD-ROM | disk | FTP | other *** search
- /* JStatus: extended Status command, prints all process and which they are
- threaded from ... */
-
- /* main.c
- $VER: JStatus.c 1.3 (21.3.98) ©1998 by Matthias Faust
- */
-
- #include <exec/types.h>
- #include <exec/execbase.h>
- #include <exec/nodes.h>
- #include <exec/lists.h>
- #include <exec/libraries.h>
- #include <exec/semaphores.h>
- #include <exec/memory.h>
-
- #include <dos/rdargs.h>
- #include <dos/dosextens.h>
- #include <dos/dos.h>
-
- #include <utility/hooks.h>
- #include <utility/tagitem.h>
-
- #include <proto/utility.h>
-
- #include <powerup/ppclib/interface.h>
- #include <powerup/ppclib/message.h>
- #include <powerup/ppclib/tasks.h>
- #include <powerup/PPCLib/object.h>
- #include <powerup/proto/ppc_gcc.h>
-
- #include "compiler.h"
- #include "Hook_utility.h"
-
- #define TEMPLATE ("PROCESS/N,FULL/S,PCB=TCB/S,WB=ALL/S,EWILL=E/S,COM=COMMAND/K,NP=NOPPC/S,S=STATE/K")
- char __aligned *version="$VER: JStatus 1.4 (12.12.99) ©1998 by Matthias Faust";
-
- #define STATE_HALT "HALT"
- #define STATE_CONT "CONT"
- #define STATE_BREAK "STOP"
- #define JS_SM "« JStatus Halter »"
- #define JS_NOMEM "No memory !"
- #define JSS_HALT "Halt"
- #define JSS_RUN "Run "
- #define TS_HALT 8
-
- typedef long LONGBOOL;
-
- struct JS_Args
- {
- ULONG *process;
- LONGBOOL full;
- LONGBOOL tcb;
- LONGBOOL all;
- LONGBOOL eprog;
- STRPTR com;
- LONGBOOL noppc;
- STRPTR state;
- };
-
- struct JS_Args jsa;
- struct Hook ViewHook;
- struct Library *PPCLibBase=NULL;
-
- ULONG tasks=0;
-
- BOOL names=TRUE;
-
- long __oslibversion=37; /* OS 2.0 required */
-
- char *GetProcName(struct Process *pr);
- void GetTCB(struct Process *pr);
- BOOL iseprogram(struct Process *pr);
- void printPPCTasks();
- void __saveds ASM HookFunc(register __a0 struct Hook *VHook GNUCREG(a0),
- register __a2 void *MyTask GNUCREG(a2),
- register __a1 void *MyMessage GNUCREG(a1));
-
- int main()
- {
- struct RDArgs *rd=NULL;
- struct ExecBase *sb=NULL;
- struct Task *tc,*mytask;
- struct Process *pr;
- struct List *TaskList,*lists[3];
- struct MsgPort *mp;
- ULONG ioerr=0,i=0,start=1,end=1;
- BOOL notfound=FALSE,hasppc=FALSE,error=TRUE;
-
- memset(&jsa,0,sizeof(struct JS_Args));
- memset(&ViewHook,0,sizeof(struct Hook));
-
- if (!(UtilityBase=(struct UtilityBase *)OpenLibrary("utility.library",37)))
- {
- Printf("Can`t open `utility.library' V37++ !\n");
- exit(0);
- }
-
- if (hasppc=((PPCLibBase=(struct Library *)OpenLibrary("ppc.library",0))!=NULL))
- {
- InstallHook(&ViewHook,HookFunc,PPCLibBase);
- }
-
- if (rd=(struct RDArgs *)ReadArgs(TEMPLATE,&jsa,NULL))
- {
- jsa.tcb|=jsa.full; /* If FULL then also TCB */
- names&=!jsa.tcb; /* If no TCB then print names */
- names|=jsa.full; /* If FULL then print also names */
- names&=((jsa.com)==NULL); /* If no command given then print no name */
- jsa.all&=((jsa.process)==NULL); /* If process number is given then print no list */
- jsa.all&=((jsa.com)==NULL);
-
- Forbid();
-
- sb=(*((struct ExecBase **) 4));
- lists[0]=&sb->TaskWait;
- lists[1]=&sb->TaskReady;
- lists[2]=NULL;
-
- /* First print all CLI processes */
-
- start=(jsa.process)?(*jsa.process):1;
- end=(jsa.process)?(*jsa.process)+1:MaxCli();
-
- if (jsa.state&&*jsa.process)
- {
- if (Stricmp(jsa.state,(STRPTR)STATE_HALT)==0)
- {
- if (!(pr=(struct Process *)FindCliProc(*jsa.process))) goto notexist;
- pr->pr_Task.tc_State=TS_HALT;
- }
- else if (Stricmp(jsa.state,(STRPTR)STATE_CONT)==0)
- {
- if (!(pr=(struct Process *)FindCliProc(*jsa.process))) goto notexist;
- runtask:
- mytask=(struct Task *)FindTask(0);
- pr->pr_Task.tc_State=TS_READY; // This is quite senseless, but safe
- Remove(pr);
- Insert(&sb->TaskWait,pr,mytask->tc_Node); //Insert behind me
-
- }
- else if (Stricmp(jsa.state,(STRPTR)STATE_BREAK)==0)
- {
- if (pr=(struct Process *)FindCliProc(*jsa.process))
- {
- Signal(pr,SIGBREAKF_CTRL_C);
- goto runtask;
- }
- }
- else
- {
- Printf("Unknown state `%s' !\n",jsa.state);
- }
- }
- else
- {
- for (i=start;i<end;i++)
- {
- if (pr=(struct Process *)FindCliProc(i))
- {
- if (!jsa.com)
- {
- if (!iseprogram(pr)) Printf("CLI-Process %2ld: ",i);else Printf("E program ? %2ld: ",i);
- if (jsa.tcb) GetTCB(pr);
- if (names) Printf("%sName: %s",(jsa.full)?", ":" ",GetProcName(pr));
- Printf("\n");
- }
- else
- {
- if (Stricmp(GetProcName(pr),jsa.com)==NULL)
- {
- Printf(" %ld\n",i);
- }
- else notfound=TRUE;
- }
- }
- else
- {
- notexist:
- if (jsa.process) Printf("Process %ld does not exist\n",i);
- }
- }
- }
-
- if (jsa.all)
- {
- i=0;
- Printf("\nSystem processes:\n");
- Printf("*****************\n");
-
- while (lists[i])
- {
- TaskList=(struct List *)lists[i];
- tc=(struct Task *)TaskList->lh_Head;
-
- while (tc->tc_Node.ln_Succ)
- {
- if (tc->tc_Node.ln_Type==NT_PROCESS)
- {
- pr=(struct Process *)tc;
- if (!pr->pr_TaskNum)
- {
- if (!iseprogram(pr)) Printf("%16lc",0x20);else Printf("E program ? ");
- if (jsa.tcb) GetTCB(pr);
- if (names) Printf("%sName: %s",(jsa.full)?", ":" ",GetProcName(pr));
- Printf("\n");
- }
- }
- tc=(struct Task *)tc->tc_Node.ln_Succ;
- }
- i++;
- }
-
- ioerr=0;
- }
- firstexit:
- Permit();
-
- /* I can currently guess how long this will delay, but I am sure,
- if I didn`t Permit() -> Nice to meet you Mr.Guru.... */
-
- if ((hasppc)&&!jsa.noppc&&jsa.all)) printPPCTasks();
-
- FreeArgs(rd);
- }
- else
- {
- ioerr=IoErr();
- }
-
- CloseLibrary(UtilityBase);
- if (PPCLibBase) CloseLibrary(PPCLibBase);
- SetIoErr(ioerr);
- if (notfound) exit(RETURN_WARN);else exit((ioerr)?RETURN_FAIL:RETURN_OK);
- }
-
- char *GetProcName(struct Process *pr)
- {
- char *name;
-
- struct CommandLineInterface *cli;
-
- if (pr->pr_CLI)
- {
- cli=BADDR(pr->pr_CLI);
- if (cli->cli_CommandName) name=(char *)BADDR(cli->cli_CommandName)+1; /* BSTR x: x[0] = length;x[1..255]=str;x>>2 */
- }
- else
- {
- name=(char *)pr->pr_Task.tc_Node.ln_Name;
- }
-
- return(((name)&&(strlen(name)))?name:"(no command)");
- }
-
- BOOL iseprogram(struct Process *pr)
- {
- ULONG stacksize;
- long usedstack,*globvec;
-
- stacksize=pr->pr_Task.tc_SPUpper-pr->pr_Task.tc_SPLower;
- usedstack=pr->pr_Task.tc_SPUpper-pr->pr_Task.tc_SPReg;
-
- return(!((usedstack>-1)&&(usedstack<=stacksize))&&jsa.eprog);
- }
-
- void GetTCB(struct Process *pr)
- {
- ULONG stacksize;
- long usedstack,*globvec;
-
- stacksize=pr->pr_Task.tc_SPUpper-pr->pr_Task.tc_SPLower;
- usedstack=pr->pr_Task.tc_SPUpper-pr->pr_Task.tc_SPReg;
- globvec=(pr->pr_GlobVec)?pr->pr_GlobVec:NULL;
- if (!jsa.full) Printf("Addr: 0x%08lx, ",pr);
- if ((usedstack>-1)&&(usedstack<=stacksize)) Printf("Stk: %-4ld of %-5ld, ",usedstack,stacksize);
- else (jsa.eprog)?Printf("Has own stack.%4lc, ",0x20):Printf("Stk: %-4ld (unused), ",stacksize);
- if (globvec) Printf("GV: %3ld, ",*globvec);else Printf("no GV%4lc, ",0x20);
- Printf("Pri: %3ld, ",pr->pr_Task.tc_Node.ln_Pri);
- Printf("St: %s",(pr->pr_Task.tc_State==TS_HALT)?(STRPTR)JSS_HALT:(STRPTR)JSS_RUN);
- }
-
- /* Based on Laires PPCShowTasks.c */
-
- void printPPCTasks()
- {
- struct TagItem Tags[3];
-
- ViewHook.h_Entry =(ULONG (*)(void)) HookFunc;
- ViewHook.h_SubEntry =(ULONG (*)(void)) NULL;
- ViewHook.h_Data =(APTR) PPCLibBase;
-
- Tags[0].ti_Tag =PPCTASKINFOTAG_ALLTASK;
- Tags[0].ti_Data =TRUE;
- Tags[1].ti_Tag =PPCTASKINFOTAG_HOOK;
- Tags[1].ti_Data =(ULONG) &ViewHook;
- Tags[2].ti_Tag =TAG_DONE;
-
- PPCGetTaskAttrs(NULL,(struct TagItem*)&Tags);
- }
-
- void __saveds ASM HookFunc(register __a0 struct Hook *VHook GNUCREG(a0),
- register __a2 void *MyTask GNUCREG(a2),
- register __a1 void *MyMessage GNUCREG(a1))
- {
- struct Library *PPCLibBase;
- struct TagItem Tags[3];
- char *Name;
- int Pri;
- ULONG StackSize;
-
- PPCLibBase = (struct Library*) VHook->h_Data;
-
- Tags[1].ti_Tag = TAG_END;
- Tags[0].ti_Tag = PPCTASKINFOTAG_NAME;
- Name = (char*) PPCGetTaskAttrs(MyTask,(struct TagItem*) &Tags);
-
- Tags[0].ti_Tag = PPCTASKINFOTAG_PRIORITY;
- Pri = PPCGetTaskAttrs(MyTask,(struct TagItem*) &Tags);
-
- Tags[0].ti_Tag = PPCTASKINFOTAG_STACKSIZE;
- StackSize = PPCGetTaskAttrs(MyTask,(struct TagItem*) &Tags);
-
- tasks++;
-
- if (tasks==1) Printf("\nPPC tasks:\n"\
- "**********\n");
-
- if (tasks)
- {
- Printf("%16lc",0x20);
- if (jsa.tcb)
- {
- if (!jsa.full) Printf("Addr: %08lx, ",MyTask);
- Printf("Stk: %-5ld, Num: %ld, Pri: %ld",StackSize,tasks,Pri);
- }
-
- if (names) Printf("%sName: %s",(jsa.full)?", ":" ",Name);
- Printf("\n");
- }
-
- }
-