Delete the toolchain builddir after building the toolchain.
[AROS.git] / workbench / c / TaskList.c
blob19d6406d6b7a4ff29e222a6ca1bcf359eb5c5e09
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: english
7 */
9 /******************************************************************************
12 NAME
14 TaskList
16 SYNOPSIS
18 (N/A)
20 LOCATION
24 FUNCTION
26 Prints a list of all tasks.
28 INPUTS
30 RESULT
32 NOTES
34 EXAMPLE
36 BUGS
38 SEE ALSO
40 INTERNALS
42 HISTORY
44 ******************************************************************************/
46 #include <exec/memory.h>
47 #include <exec/tasks.h>
48 #include <exec/execbase.h>
49 #include <proto/exec.h>
50 #include <proto/timer.h>
51 #include <aros/debug.h>
52 #include <devices/timer.h>
53 #include <dos/dosextens.h>
54 #include <proto/dos.h>
56 /* Dirty hack! Is there a better way? */
57 #include "../../rom/exec/etask.h"
59 const TEXT version[] = "$VER: tasklist 41.2 (15.5.2009)\n";
61 ULONG eclock;
63 struct task
65 STRPTR name;
66 APTR address;
67 WORD type;
68 WORD state;
69 IPTR stacksize;
70 IPTR stackused;
71 WORD pri;
72 UQUAD cputime;
75 static int addtask(struct Task *task, struct task **t, STRPTR *e)
77 STRPTR s1,s2;
78 (*t)->cputime = GetIntETask(task)->iet_CpuTime;
79 (*t)->address=task;
80 (*t)->type=task->tc_Node.ln_Type;
81 (*t)->pri =(WORD)task->tc_Node.ln_Pri;
82 (*t)->state=task->tc_State;
83 (*t)->stacksize=(STRPTR)task->tc_SPUpper-(STRPTR)task->tc_SPLower;
84 #if AROS_STACK_GROWS_DOWNWARDS
85 (*t)->stackused=(STRPTR)task->tc_SPUpper-SP_OFFSET-(STRPTR)task->tc_SPReg;
86 #else
87 (*t)->stackused=(STRPTR)task->tc_SPReg-SP_OFFSET-(STRPTR)task->tc_SPLower;
88 #endif
89 if(task->tc_State==TS_RUN)
90 /* The tc_SPReg for the actual process is invalid
91 if it had no context switch yet */
92 (*t)->stackused=0;
93 s1=task->tc_Node.ln_Name;
94 if(task->tc_Node.ln_Type==NT_PROCESS&&((struct Process *)task)->pr_CLI)
96 /* TODO: Use cli_CommandName field for the name */
97 (*t)->type=-1;
99 if(s1!=NULL)
101 s2=s1;
102 while(*s2++)
104 while(s2>s1)
106 if(*e<=(STRPTR)t)
107 return 0;
108 *--(*e)=*--s2;
111 if((STRPTR)(*t+1)>*e)
112 return 0;
113 (*t)->name=*e;
114 ++*t;
115 return 1;
118 static int fillbuffer(struct task **buffer, IPTR size)
120 STRPTR end=(STRPTR)*buffer+size;
121 struct Task *task;
122 Disable();
123 if(!addtask(SysBase->ThisTask,buffer,&end))
125 Enable();
126 return 0;
128 for(task=(struct Task *)SysBase->TaskReady.lh_Head;
129 task->tc_Node.ln_Succ!=NULL;
130 task=(struct Task *)task->tc_Node.ln_Succ)
131 if(!addtask(task,buffer,&end))
133 Enable();
134 return 0;
136 for(task=(struct Task *)SysBase->TaskWait.lh_Head;
137 task->tc_Node.ln_Succ!=NULL;
138 task=(struct Task *)task->tc_Node.ln_Succ)
139 if(!addtask(task,buffer,&end))
141 Enable();
142 return 0;
144 Enable();
145 return 1;
148 int __nocommandline;
150 int main(void)
152 IPTR size;
153 struct task *buffer,*tasks,*tasks2;
155 /* Is all this code really needed to read the EClock frequency? sigh... */
156 struct TimerBase *TimerBase = NULL;
157 struct EClockVal ec;
158 struct MsgPort *port = CreateMsgPort();
159 struct timerequest *io = (struct timerequest *)CreateIORequest(port, sizeof(struct timerequest));
160 OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)io, 0);
161 TimerBase = (struct TimerBase *)io->tr_node.io_Device;
162 eclock = ReadEClock(&ec);
163 CloseDevice((struct IORequest *)io);
164 DeleteIORequest((struct IORequest *)io);
165 DeleteMsgPort(port);
167 for(size=2048;;size+=2048)
169 buffer=AllocVec(size,MEMF_ANY);
170 if(buffer==NULL)
172 FPuts(Output(),"Not Enough memory for task buffer\n");
173 return 20;
175 tasks=buffer;
176 if(fillbuffer(&tasks,size))
178 FPuts(Output(),"Address\t\tType\tPri\tState\tCPU Time\tStack\tUsed\tName\n");
179 for(tasks2=buffer;tasks2<tasks;tasks2++)
181 ULONG time;
183 /* If eclock was not null, use it */
184 if (eclock)
185 time = tasks2->cputime / eclock;
186 else /* Otherwise we cannot calculate the cpu time :/ */
187 time = 0;
189 IPTR args[10];
190 args[0]=(IPTR)tasks2->address;
191 args[1]=(IPTR)(tasks2->type==NT_TASK?"task":
192 tasks2->type==NT_PROCESS?"process":"CLI");
193 args[2]=tasks2->pri;
194 args[3]=(IPTR)(tasks2->state==TS_RUN?"running":
195 tasks2->state==TS_READY?"ready":"waiting");
196 args[6]=time % 60;
197 time /= 60;
198 args[5]=time % 60;
199 time /= 60;
200 args[4]=time;
201 args[7]=tasks2->stacksize;
202 args[8]=tasks2->stackused;
203 args[9]=tasks2->name!=NULL?(IPTR)tasks2->name:0;
204 VPrintf("0x%08.lx\t%s\t%ld\t%s\t%02ld:%02ld:%02ld\t%ld\t%ld\t%s\n",args);
206 FreeVec(buffer);
207 return 0;
209 FreeVec(buffer);