]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ddb/db_ps.c
- Overhaul the 'ps' command in ddb to be mostly readable again. :) It is
[FreeBSD/FreeBSD.git] / sys / ddb / db_ps.c
1 /*-
2  * Copyright (c) 1993 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/jail.h>
36 #include <sys/kdb.h>
37 #include <sys/linker_set.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/sysent.h>
42 #include <sys/cons.h>
43 #include <vm/vm.h>
44 #include <vm/vm_param.h>
45 #include <vm/pmap.h>
46
47 #include <ddb/ddb.h>
48
49 /* XXX I'd prefer a better way. */
50 #if defined(__alpha__) || defined(__amd64__) || defined(__ia64__) || defined(__sparc64__)
51 #define PTR64
52 #endif
53
54 #ifdef PTR64
55 CTASSERT(sizeof(uintptr_t) == sizeof(uint64_t));
56 #else
57 CTASSERT(sizeof(uintptr_t) == sizeof(uint32_t));
58 #endif
59
60 static void     dumpthread(volatile struct proc *p, volatile struct thread *td,
61                     int all);
62
63 /*
64  * Layout:
65  * - column counts
66  * - header
67  * - single-threaded process
68  * - multi-threaded process
69  * - thread in a MT process
70  *
71  *          1         2         3         4         5         6         7
72  * 1234567890123456789012345678901234567890123456789012345678901234567890
73  *  pid   uid  ppid  pgrp  state   wmesg      wchan    cmd
74  * <pid> <ui> <ppi> <pgi> <stat> < wmesg > <  wchan  > <name>
75  * <pid> <ui> <ppi> <pgi> <stat>  (threaded)           <command>
76  *  <tid    >             <stat> < wmesg > <  wchan  > <name>
77  *
78  * For machines with 64-bit pointers, we expand the wchan field 8 more
79  * characters.
80  */
81 void
82 db_ps(db_expr_t addr, boolean_t hasaddr, db_expr_t count, char *modif)
83 {
84         volatile struct proc *p, *pp;
85         volatile struct thread *td;
86         struct ucred *cred;
87         struct pgrp *pgrp;
88         char state[9];
89         int np, quit, rflag, sflag, dflag, lflag, wflag;
90
91         np = nprocs;
92         quit = 0;
93
94         /* sx_slock(&allproc_lock); */
95         if (!LIST_EMPTY(&allproc))
96                 p = LIST_FIRST(&allproc);
97         else
98                 p = &proc0;
99
100         db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
101 #ifdef PTR64
102         db_printf(" pid   uid  ppid  pgrp  state   wmesg          wchan        cmd\n");
103 #else
104         db_printf(" pid   uid  ppid  pgrp  state   wmesg      wchan    cmd\n");
105 #endif
106         while (--np >= 0 && !quit) {
107                 if (p == NULL) {
108                         db_printf("oops, ran out of processes early!\n");
109                         break;
110                 }
111                 /* PROC_LOCK(p); */
112                 pp = p->p_pptr;
113                 if (pp == NULL)
114                         pp = p;
115
116                 cred = p->p_ucred;
117                 pgrp = p->p_pgrp;
118                 db_printf("%5d %4d %5d %5d ", p->p_pid,
119                     cred != NULL ? cred->cr_ruid : 0, pp->p_pid,
120                     pgrp != NULL ? pgrp->pg_id : 0);
121
122                 /* Determine our primary process state. */
123                 switch(p->p_state) {
124                 case PRS_NORMAL:
125                         if (P_SHOULDSTOP(p))
126                                 state[0] = 'T';
127                         else {
128                                 /*
129                                  * One of D, L, R, S, W.  For a
130                                  * multithreaded process we will use
131                                  * the state of the thread with the
132                                  * highest precedence.  The
133                                  * precendence order from high to low
134                                  * is R, L, D, S, W.  If no thread is
135                                  * in a sane state we use '?' for our
136                                  * primary state.
137                                  */
138                                 rflag = sflag = dflag = lflag = wflag = 0;
139                                 FOREACH_THREAD_IN_PROC(p, td) {
140                                         if (td->td_state == TDS_RUNNING ||
141                                             td->td_state == TDS_RUNQ ||
142                                             td->td_state == TDS_CAN_RUN)
143                                                 rflag++;
144                                         if (TD_ON_LOCK(td))
145                                                 lflag++;
146                                         if (TD_IS_SLEEPING(td)) {
147                                                 if (!td->td_flags & TDF_SINTR)
148                                                         dflag++;
149                                                 else
150                                                         sflag++;
151                                         }
152                                         if (TD_AWAITING_INTR(td))
153                                                 wflag++;
154                                 }
155                                 if (rflag)
156                                         state[0] = 'R';
157                                 else if (lflag)
158                                         state[0] = 'L';
159                                 else if (dflag)
160                                         state[0] = 'D';
161                                 else if (sflag)
162                                         state[0] = 'S';
163                                 else if (wflag)
164                                         state[0] = 'W';
165                                 else
166                                         state[0] = '?';                         
167                         }
168                         break;
169                 case PRS_NEW:
170                         state[0] = 'N';
171                         break;
172                 case PRS_ZOMBIE:
173                         state[0] = 'Z';
174                         break;
175                 default:
176                         state[0] = 'U';
177                         break;
178                 }
179                 state[1] = '\0';
180
181                 /* Additional process state flags. */
182                 if (!p->p_sflag & PS_INMEM)
183                         strlcat(state, "W", sizeof(state));
184                 if (p->p_flag & P_TRACED)
185                         strlcat(state, "X", sizeof(state));
186                 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
187                         strlcat(state, "E", sizeof(state));
188                 if (p->p_flag & P_PPWAIT)
189                         strlcat(state, "V", sizeof(state));
190                 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
191                         strlcat(state, "L", sizeof(state));
192                 if (p->p_session != NULL && SESS_LEADER(p))
193                         strlcat(state, "s", sizeof(state));
194                 /* Cheated here and didn't compare pgid's. */
195                 if (p->p_flag & P_CONTROLT)
196                         strlcat(state, "+", sizeof(state));
197                 if (cred != NULL && jailed(cred))
198                         strlcat(state, "J", sizeof(state));
199                 db_printf(" %-6.6s ", state);
200                 if (p->p_flag & P_HADTHREADS)
201 #ifdef PTR64
202                         db_printf(" (threaded)                  %s\n",
203                             p->p_comm);
204 #else
205                         db_printf(" (threaded)          %s\n", p->p_comm);
206 #endif
207                 FOREACH_THREAD_IN_PROC(p, td) {
208                         dumpthread(p, td, p->p_flag & P_HADTHREADS);
209                         if (quit)
210                                 break;
211                 }
212                 /* PROC_UNLOCK(p); */
213
214                 p = LIST_NEXT(p, p_list);
215                 if (p == NULL && np > 0)
216                         p = LIST_FIRST(&zombproc);
217         }
218         /* sx_sunlock(&allproc_lock); */
219 }
220
221 static void
222 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
223 {
224         char state[9], wprefix;
225         const char *wmesg;
226         void *wchan;
227         
228         if (all) {
229                 db_printf(" %9d             ", td->td_tid);
230                 switch (td->td_state) {
231                 case TDS_RUNNING:
232                         snprintf(state, sizeof(state), "Run");
233                         break;
234                 case TDS_RUNQ:
235                         snprintf(state, sizeof(state), "RunQ");
236                         break;
237                 case TDS_CAN_RUN:
238                         snprintf(state, sizeof(state), "CanRun");
239                         break;
240                 case TDS_INACTIVE:
241                         snprintf(state, sizeof(state), "Inactv");
242                         break;
243                 case TDS_INHIBITED:
244                         state[0] = '\0';
245                         if (TD_ON_LOCK(td))
246                                 strlcat(state, "L", sizeof(state));
247                         if (TD_IS_SLEEPING(td)) {
248                                 if (td->td_flags & TDF_SINTR)
249                                         strlcat(state, "S", sizeof(state));
250                                 else
251                                         strlcat(state, "D", sizeof(state));
252                         }
253                         if (TD_IS_SWAPPED(td))
254                                 strlcat(state, "W", sizeof(state));
255                         if (TD_AWAITING_INTR(td))
256                                 strlcat(state, "I", sizeof(state));
257                         if (TD_IS_SUSPENDED(td))
258                                 strlcat(state, "s", sizeof(state));
259                         if (state[0] != '\0')
260                                 break;
261                 default:
262                         snprintf(state, sizeof(state), "???");
263                 }                       
264                 db_printf(" %-6.6s ", state);
265         }
266         wprefix = ' ';
267         if (TD_ON_LOCK(td)) {
268                 wprefix = '*';
269                 wmesg = td->td_lockname;
270                 wchan = td->td_blocked;
271         } else if (TD_ON_SLEEPQ(td)) {
272                 wmesg = td->td_wmesg;
273                 wchan = td->td_wchan;
274         } else if (TD_IS_RUNNING(td)) {
275                 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
276                 wmesg = state;
277                 wchan = NULL;
278         } else {
279                 wmesg = "";
280                 wchan = NULL;
281         }
282         db_printf("%c%-8.8s ", wprefix, wmesg);
283         if (wchan == NULL)
284 #ifdef PTR64
285                 db_printf("%18s ", "");
286 #else
287                 db_printf("%10s ", "");
288 #endif
289         else
290                 db_printf("%p ", wchan);
291         if (p->p_flag & P_SYSTEM)
292                 db_printf("[");
293         if (td->td_name[0] != '\0')
294                 db_printf("%s", td->td_name);
295         else
296                 db_printf("%s", td->td_proc->p_comm);
297         if (p->p_flag & P_SYSTEM)
298                 db_printf("]");
299         db_printf("\n");
300 }
301
302 DB_SHOW_COMMAND(thread, db_show_thread)
303 {
304         struct thread *td;
305         boolean_t comma;
306
307         /* Determine which thread to examine. */
308         if (have_addr)
309                 td = db_lookup_thread(addr, FALSE);
310         else
311                 td = kdb_thread;
312
313         db_printf("Thread %d at %p:\n", td->td_tid, td);
314         db_printf(" proc (pid %d): %p ", td->td_proc->p_pid, td->td_proc);
315         db_printf(" ksegrp: %p\n", td->td_ksegrp);
316         if (td->td_name[0] != '\0')
317                 db_printf(" name: %s\n", td->td_name);
318         db_printf(" flags: %#x ", td->td_flags);
319         db_printf(" pflags: %#x\n", td->td_pflags);
320         db_printf(" state: ");
321         switch (td->td_state) {
322         case TDS_INACTIVE:
323                 db_printf("INACTIVE\n");
324                 break;
325         case TDS_CAN_RUN:
326                 db_printf("CAN RUN\n");
327                 break;
328         case TDS_RUNQ:
329                 db_printf("RUNQ\n");
330                 break;
331         case TDS_RUNNING:
332                 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
333                 break;
334         case TDS_INHIBITED:
335                 db_printf("INHIBITED: {");
336                 comma = FALSE;
337                 if (TD_IS_SLEEPING(td)) {
338                         db_printf("SLEEPING");
339                         comma = TRUE;
340                 }
341                 if (TD_IS_SUSPENDED(td)) {
342                         if (comma)
343                                 db_printf(", ");
344                         db_printf("SUSPENDED");
345                         comma = TRUE;
346                 }
347                 if (TD_IS_SWAPPED(td)) {
348                         if (comma)
349                                 db_printf(", ");
350                         db_printf("SWAPPED");
351                         comma = TRUE;
352                 }
353                 if (TD_ON_LOCK(td)) {
354                         if (comma)
355                                 db_printf(", ");
356                         db_printf("LOCK");
357                         comma = TRUE;
358                 }
359                 if (TD_AWAITING_INTR(td)) {
360                         if (comma)
361                                 db_printf(", ");
362                         db_printf("IWAIT");
363                 }
364                 db_printf("}\n");
365                 break;
366         default:
367                 db_printf("??? (%#x)\n", td->td_state);
368                 break;
369         }
370         if (TD_ON_LOCK(td))
371                 db_printf(" lock: %s  turnstile: %p\n", td->td_lockname,
372                     td->td_blocked);
373         if (TD_ON_SLEEPQ(td))
374                 db_printf(" wmesg: %s  wchan: %p\n", td->td_wmesg,
375                     td->td_wchan);
376         db_printf(" priority: %d\n", td->td_priority);
377 }
378
379 DB_SHOW_COMMAND(proc, db_show_proc)
380 {
381         struct thread *td;
382         struct proc *p;
383         int i, quit;
384
385         /* Determine which process to examine. */
386         if (have_addr)
387                 p = db_lookup_proc(addr);
388         else
389                 p = kdb_thread->td_proc;
390
391         quit = 0;
392         db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
393         db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
394         db_printf(" state: ");
395         switch (p->p_state) {
396         case PRS_NEW:
397                 db_printf("NEW\n");
398                 break;
399         case PRS_NORMAL:
400                 db_printf("NORMAL\n");
401                 break;
402         case PRS_ZOMBIE:
403                 db_printf("ZOMBIE\n");
404                 break;
405         default:
406                 db_printf("??? (%#x)\n", p->p_state);
407         }
408         if (p->p_ucred != NULL) {
409                 db_printf(" uid: %d  gids: ", p->p_ucred->cr_uid);
410                 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
411                         db_printf("%d", p->p_ucred->cr_groups[i]);
412                         if (i < (p->p_ucred->cr_ngroups - 1))
413                                 db_printf(", ");
414                 }
415                 db_printf("\n");
416         }
417         if (p->p_pptr != NULL)
418                 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
419                     p->p_pptr);
420         if (p->p_leader != NULL && p->p_leader != p)
421                 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
422                     p->p_leader);
423         if (p->p_sysent != NULL)
424                 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
425         if (p->p_args != NULL)
426                 db_printf(" arguments: %.*s\n", (int)p->p_args->ar_length,
427                     p->p_args->ar_args);
428         db_printf(" threads: %d\n", p->p_numthreads);
429         FOREACH_THREAD_IN_PROC(p, td) {
430                 dumpthread(p, td, 1);
431                 if (quit)
432                         break;
433         }
434 }