The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/ddb/db_ps.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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: stable/11/sys/ddb/db_ps.c 339857 2018-10-29 12:47:15Z avg $");
   32 
   33 #include "opt_kstack_pages.h"
   34 
   35 #include <sys/param.h>
   36 #include <sys/cons.h>
   37 #include <sys/jail.h>
   38 #include <sys/kdb.h>
   39 #include <sys/kernel.h>
   40 #include <sys/proc.h>
   41 #include <sys/sysent.h>
   42 #include <sys/systm.h>
   43 #include <sys/_kstack_cache.h>
   44 #include <vm/vm.h>
   45 #include <vm/vm_param.h>
   46 #include <vm/pmap.h>
   47 
   48 #include <ddb/ddb.h>
   49 
   50 #define PRINT_NONE      0
   51 #define PRINT_ARGS      1
   52 
   53 static void     dumpthread(volatile struct proc *p, volatile struct thread *td,
   54                     int all);
   55 static int      ps_mode;
   56 
   57 /*
   58  * At least one non-optional show-command must be implemented using
   59  * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
   60  * Here is one.
   61  */
   62 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
   63 {
   64         db_ps(addr, have_addr, count, modif);
   65 }
   66 
   67 static void
   68 dump_args(volatile struct proc *p)
   69 {
   70         char *args;
   71         int i, len;
   72 
   73         if (p->p_args == NULL)
   74                 return;
   75         args = p->p_args->ar_args;
   76         len = (int)p->p_args->ar_length;
   77         for (i = 0; i < len; i++) {
   78                 if (args[i] == '\0')
   79                         db_printf(" ");
   80                 else
   81                         db_printf("%c", args[i]);
   82         }
   83 }
   84 
   85 /*
   86  * Layout:
   87  * - column counts
   88  * - header
   89  * - single-threaded process
   90  * - multi-threaded process
   91  * - thread in a MT process
   92  *
   93  *          1         2         3         4         5         6         7
   94  * 1234567890123456789012345678901234567890123456789012345678901234567890
   95  *   pid  ppid  pgrp   uid   state   wmesg     wchan    cmd
   96  * <pid> <ppi> <pgi> <uid>  <stat> < wmesg > < wchan  > <name>
   97  * <pid> <ppi> <pgi> <uid>  <stat>  (threaded)          <command>
   98  * <tid >                   <stat> < wmesg > < wchan  > <name>
   99  *
  100  * For machines with 64-bit pointers, we expand the wchan field 8 more
  101  * characters.
  102  */
  103 void
  104 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif)
  105 {
  106         volatile struct proc *p, *pp;
  107         volatile struct thread *td;
  108         struct ucred *cred;
  109         struct pgrp *pgrp;
  110         char state[9];
  111         int np, rflag, sflag, dflag, lflag, wflag;
  112 
  113         ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE;
  114         np = nprocs;
  115 
  116         if (!LIST_EMPTY(&allproc))
  117                 p = LIST_FIRST(&allproc);
  118         else
  119                 p = &proc0;
  120 
  121 #ifdef __LP64__
  122         db_printf("  pid  ppid  pgrp   uid   state   wmesg         wchan        cmd\n");
  123 #else
  124         db_printf("  pid  ppid  pgrp   uid   state   wmesg     wchan    cmd\n");
  125 #endif
  126         while (--np >= 0 && !db_pager_quit) {
  127                 if (p == NULL) {
  128                         db_printf("oops, ran out of processes early!\n");
  129                         break;
  130                 }
  131                 pp = p->p_pptr;
  132                 if (pp == NULL)
  133                         pp = p;
  134 
  135                 cred = p->p_ucred;
  136                 pgrp = p->p_pgrp;
  137                 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
  138                     pgrp != NULL ? pgrp->pg_id : 0,
  139                     cred != NULL ? cred->cr_ruid : 0);
  140 
  141                 /* Determine our primary process state. */
  142                 switch (p->p_state) {
  143                 case PRS_NORMAL:
  144                         if (P_SHOULDSTOP(p))
  145                                 state[0] = 'T';
  146                         else {
  147                                 /*
  148                                  * One of D, L, R, S, W.  For a
  149                                  * multithreaded process we will use
  150                                  * the state of the thread with the
  151                                  * highest precedence.  The
  152                                  * precendence order from high to low
  153                                  * is R, L, D, S, W.  If no thread is
  154                                  * in a sane state we use '?' for our
  155                                  * primary state.
  156                                  */
  157                                 rflag = sflag = dflag = lflag = wflag = 0;
  158                                 FOREACH_THREAD_IN_PROC(p, td) {
  159                                         if (td->td_state == TDS_RUNNING ||
  160                                             td->td_state == TDS_RUNQ ||
  161                                             td->td_state == TDS_CAN_RUN)
  162                                                 rflag++;
  163                                         if (TD_ON_LOCK(td))
  164                                                 lflag++;
  165                                         if (TD_IS_SLEEPING(td)) {
  166                                                 if (!(td->td_flags & TDF_SINTR))
  167                                                         dflag++;
  168                                                 else
  169                                                         sflag++;
  170                                         }
  171                                         if (TD_AWAITING_INTR(td))
  172                                                 wflag++;
  173                                 }
  174                                 if (rflag)
  175                                         state[0] = 'R';
  176                                 else if (lflag)
  177                                         state[0] = 'L';
  178                                 else if (dflag)
  179                                         state[0] = 'D';
  180                                 else if (sflag)
  181                                         state[0] = 'S';
  182                                 else if (wflag)
  183                                         state[0] = 'W';
  184                                 else
  185                                         state[0] = '?';
  186                         }
  187                         break;
  188                 case PRS_NEW:
  189                         state[0] = 'N';
  190                         break;
  191                 case PRS_ZOMBIE:
  192                         state[0] = 'Z';
  193                         break;
  194                 default:
  195                         state[0] = 'U';
  196                         break;
  197                 }
  198                 state[1] = '\0';
  199 
  200                 /* Additional process state flags. */
  201                 if (!(p->p_flag & P_INMEM))
  202                         strlcat(state, "W", sizeof(state));
  203                 if (p->p_flag & P_TRACED)
  204                         strlcat(state, "X", sizeof(state));
  205                 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
  206                         strlcat(state, "E", sizeof(state));
  207                 if (p->p_flag & P_PPWAIT)
  208                         strlcat(state, "V", sizeof(state));
  209                 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
  210                         strlcat(state, "L", sizeof(state));
  211                 if (p->p_pgrp != NULL && p->p_session != NULL &&
  212                     SESS_LEADER(p))
  213                         strlcat(state, "s", sizeof(state));
  214                 /* Cheated here and didn't compare pgid's. */
  215                 if (p->p_flag & P_CONTROLT)
  216                         strlcat(state, "+", sizeof(state));
  217                 if (cred != NULL && jailed(cred))
  218                         strlcat(state, "J", sizeof(state));
  219                 db_printf(" %-6.6s ", state);
  220                 if (p->p_flag & P_HADTHREADS) {
  221 #ifdef __LP64__
  222                         db_printf(" (threaded)                  ");
  223 #else
  224                         db_printf(" (threaded)          ");
  225 #endif
  226                         if (p->p_flag & P_SYSTEM)
  227                                 db_printf("[");
  228                         db_printf("%s", p->p_comm);
  229                         if (p->p_flag & P_SYSTEM)
  230                                 db_printf("]");
  231                         if (ps_mode == PRINT_ARGS) {
  232                                 db_printf(" ");
  233                                 dump_args(p);
  234                         }
  235                         db_printf("\n");
  236                 }
  237                 FOREACH_THREAD_IN_PROC(p, td) {
  238                         dumpthread(p, td, p->p_flag & P_HADTHREADS);
  239                         if (db_pager_quit)
  240                                 break;
  241                 }
  242 
  243                 p = LIST_NEXT(p, p_list);
  244                 if (p == NULL && np > 0)
  245                         p = LIST_FIRST(&zombproc);
  246         }
  247 }
  248 
  249 static void
  250 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
  251 {
  252         char state[9], wprefix;
  253         const char *wmesg;
  254         void *wchan;
  255         
  256         if (all) {
  257                 db_printf("%6d                  ", td->td_tid);
  258                 switch (td->td_state) {
  259                 case TDS_RUNNING:
  260                         snprintf(state, sizeof(state), "Run");
  261                         break;
  262                 case TDS_RUNQ:
  263                         snprintf(state, sizeof(state), "RunQ");
  264                         break;
  265                 case TDS_CAN_RUN:
  266                         snprintf(state, sizeof(state), "CanRun");
  267                         break;
  268                 case TDS_INACTIVE:
  269                         snprintf(state, sizeof(state), "Inactv");
  270                         break;
  271                 case TDS_INHIBITED:
  272                         state[0] = '\0';
  273                         if (TD_ON_LOCK(td))
  274                                 strlcat(state, "L", sizeof(state));
  275                         if (TD_IS_SLEEPING(td)) {
  276                                 if (td->td_flags & TDF_SINTR)
  277                                         strlcat(state, "S", sizeof(state));
  278                                 else
  279                                         strlcat(state, "D", sizeof(state));
  280                         }
  281                         if (TD_IS_SWAPPED(td))
  282                                 strlcat(state, "W", sizeof(state));
  283                         if (TD_AWAITING_INTR(td))
  284                                 strlcat(state, "I", sizeof(state));
  285                         if (TD_IS_SUSPENDED(td))
  286                                 strlcat(state, "s", sizeof(state));
  287                         if (state[0] != '\0')
  288                                 break;
  289                 default:
  290                         snprintf(state, sizeof(state), "???");
  291                 }                       
  292                 db_printf(" %-6.6s ", state);
  293         }
  294         wprefix = ' ';
  295         if (TD_ON_LOCK(td)) {
  296                 wprefix = '*';
  297                 wmesg = td->td_lockname;
  298                 wchan = td->td_blocked;
  299         } else if (TD_ON_SLEEPQ(td)) {
  300                 wmesg = td->td_wmesg;
  301                 wchan = td->td_wchan;
  302         } else if (TD_IS_RUNNING(td)) {
  303                 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
  304                 wmesg = state;
  305                 wchan = NULL;
  306         } else {
  307                 wmesg = "";
  308                 wchan = NULL;
  309         }
  310         db_printf("%c%-8.8s ", wprefix, wmesg);
  311         if (wchan == NULL)
  312 #ifdef __LP64__
  313                 db_printf("%18s ", "");
  314 #else
  315                 db_printf("%10s ", "");
  316 #endif
  317         else
  318                 db_printf("%p ", wchan);
  319         if (p->p_flag & P_SYSTEM)
  320                 db_printf("[");
  321         if (td->td_name[0] != '\0')
  322                 db_printf("%s", td->td_name);
  323         else
  324                 db_printf("%s", td->td_proc->p_comm);
  325         if (p->p_flag & P_SYSTEM)
  326                 db_printf("]");
  327         if (ps_mode == PRINT_ARGS && all == 0) {
  328                 db_printf(" ");
  329                 dump_args(p);
  330         }
  331         db_printf("\n");
  332 }
  333 
  334 DB_SHOW_COMMAND(thread, db_show_thread)
  335 {
  336         struct thread *td;
  337         struct lock_object *lock;
  338         bool comma;
  339         int delta;
  340 
  341         /* Determine which thread to examine. */
  342         if (have_addr)
  343                 td = db_lookup_thread(addr, false);
  344         else
  345                 td = kdb_thread;
  346         lock = (struct lock_object *)td->td_lock;
  347 
  348         db_printf("Thread %d at %p:\n", td->td_tid, td);
  349         db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
  350         if (td->td_name[0] != '\0')
  351                 db_printf(" name: %s\n", td->td_name);
  352         db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
  353             (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
  354         db_printf(" flags: %#x ", td->td_flags);
  355         db_printf(" pflags: %#x\n", td->td_pflags);
  356         db_printf(" state: ");
  357         switch (td->td_state) {
  358         case TDS_INACTIVE:
  359                 db_printf("INACTIVE\n");
  360                 break;
  361         case TDS_CAN_RUN:
  362                 db_printf("CAN RUN\n");
  363                 break;
  364         case TDS_RUNQ:
  365                 db_printf("RUNQ\n");
  366                 break;
  367         case TDS_RUNNING:
  368                 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
  369                 break;
  370         case TDS_INHIBITED:
  371                 db_printf("INHIBITED: {");
  372                 comma = false;
  373                 if (TD_IS_SLEEPING(td)) {
  374                         db_printf("SLEEPING");
  375                         comma = true;
  376                 }
  377                 if (TD_IS_SUSPENDED(td)) {
  378                         if (comma)
  379                                 db_printf(", ");
  380                         db_printf("SUSPENDED");
  381                         comma = true;
  382                 }
  383                 if (TD_IS_SWAPPED(td)) {
  384                         if (comma)
  385                                 db_printf(", ");
  386                         db_printf("SWAPPED");
  387                         comma = true;
  388                 }
  389                 if (TD_ON_LOCK(td)) {
  390                         if (comma)
  391                                 db_printf(", ");
  392                         db_printf("LOCK");
  393                         comma = true;
  394                 }
  395                 if (TD_AWAITING_INTR(td)) {
  396                         if (comma)
  397                                 db_printf(", ");
  398                         db_printf("IWAIT");
  399                 }
  400                 db_printf("}\n");
  401                 break;
  402         default:
  403                 db_printf("??? (%#x)\n", td->td_state);
  404                 break;
  405         }
  406         if (TD_ON_LOCK(td))
  407                 db_printf(" lock: %s  turnstile: %p\n", td->td_lockname,
  408                     td->td_blocked);
  409         if (TD_ON_SLEEPQ(td))
  410                 db_printf(
  411             " wmesg: %s  wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
  412                     td->td_wmesg, td->td_wchan,
  413                     (long)sbttobt(td->td_sleeptimo).sec,
  414                     (uintmax_t)sbttobt(td->td_sleeptimo).frac,
  415                     (long)sbttobt(sbinuptime()).sec,
  416                     (uintmax_t)sbttobt(sbinuptime()).frac);
  417         db_printf(" priority: %d\n", td->td_priority);
  418         db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
  419         if (td->td_swvoltick != 0) {
  420                 delta = (u_int)ticks - (u_int)td->td_swvoltick;
  421                 db_printf(" last voluntary switch: %d ms ago\n",
  422                     1000 * delta / hz);
  423         }
  424         if (td->td_swinvoltick != 0) {
  425                 delta = (u_int)ticks - (u_int)td->td_swinvoltick;
  426                 db_printf(" last involuntary switch: %d ms ago\n",
  427                     1000 * delta / hz);
  428         }
  429 }
  430 
  431 DB_SHOW_COMMAND(proc, db_show_proc)
  432 {
  433         struct thread *td;
  434         struct proc *p;
  435         int i;
  436 
  437         /* Determine which process to examine. */
  438         if (have_addr)
  439                 p = db_lookup_proc(addr);
  440         else
  441                 p = kdb_thread->td_proc;
  442 
  443         db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
  444         db_printf(" state: ");
  445         switch (p->p_state) {
  446         case PRS_NEW:
  447                 db_printf("NEW\n");
  448                 break;
  449         case PRS_NORMAL:
  450                 db_printf("NORMAL\n");
  451                 break;
  452         case PRS_ZOMBIE:
  453                 db_printf("ZOMBIE\n");
  454                 break;
  455         default:
  456                 db_printf("??? (%#x)\n", p->p_state);
  457         }
  458         if (p->p_ucred != NULL) {
  459                 db_printf(" uid: %d  gids: ", p->p_ucred->cr_uid);
  460                 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
  461                         db_printf("%d", p->p_ucred->cr_groups[i]);
  462                         if (i < (p->p_ucred->cr_ngroups - 1))
  463                                 db_printf(", ");
  464                 }
  465                 db_printf("\n");
  466         }
  467         if (p->p_pptr != NULL)
  468                 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
  469                     p->p_pptr);
  470         if (p->p_leader != NULL && p->p_leader != p)
  471                 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
  472                     p->p_leader);
  473         if (p->p_sysent != NULL)
  474                 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
  475         if (p->p_args != NULL) {
  476                 db_printf(" arguments: ");
  477                 dump_args(p);
  478                 db_printf("\n");
  479         }
  480         db_printf(" threads: %d\n", p->p_numthreads);
  481         FOREACH_THREAD_IN_PROC(p, td) {
  482                 dumpthread(p, td, 1);
  483                 if (db_pager_quit)
  484                         break;
  485         }
  486 }
  487 
  488 void
  489 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
  490     char *dummy4 __unused)
  491 {
  492         struct proc *p;
  493         struct thread *td;
  494         struct kstack_cache_entry *ks_ce;
  495         vm_offset_t saddr;
  496 
  497         if (have_addr)
  498                 saddr = addr;
  499         else {
  500                 db_printf("Usage: findstack <address>\n");
  501                 return;
  502         }
  503 
  504         FOREACH_PROC_IN_SYSTEM(p) {
  505                 FOREACH_THREAD_IN_PROC(p, td) {
  506                         if (td->td_kstack <= saddr && saddr < td->td_kstack +
  507                             PAGE_SIZE * td->td_kstack_pages) {
  508                                 db_printf("Thread %p\n", td);
  509                                 return;
  510                         }
  511                 }
  512         }
  513 
  514         for (ks_ce = kstack_cache; ks_ce != NULL;
  515              ks_ce = ks_ce->next_ks_entry) {
  516                 if ((vm_offset_t)ks_ce <= saddr && saddr < (vm_offset_t)ks_ce +
  517                     PAGE_SIZE * kstack_pages) {
  518                         db_printf("Cached stack %p\n", ks_ce);
  519                         return;
  520                 }
  521         }
  522 }

Cache object: 8f0b6a5542996d84330096a291a9bc89


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.