1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1993 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
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 <vm/vm.h>
44 #include <vm/vm_param.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_map.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 void db_ps_proc(struct proc *p);
56 static int ps_mode;
57
58 /*
59 * At least one non-optional show-command must be implemented using
60 * DB_SHOW_ALL_COMMAND() so that db_show_all_cmd_set gets created.
61 * Here is one.
62 */
DB_SHOW_ALL_COMMAND(procs,db_procs_cmd)63 DB_SHOW_ALL_COMMAND(procs, db_procs_cmd)
64 {
65 db_ps(addr, have_addr, count, modif);
66 }
67
68 static void
dump_args(volatile struct proc * p)69 dump_args(volatile struct proc *p)
70 {
71 char *args;
72 int i, len;
73
74 if (p->p_args == NULL)
75 return;
76 args = p->p_args->ar_args;
77 len = (int)p->p_args->ar_length;
78 for (i = 0; i < len; i++) {
79 if (args[i] == '\0')
80 db_printf(" ");
81 else
82 db_printf("%c", args[i]);
83 }
84 }
85
86 /*
87 * Layout:
88 * - column counts
89 * - header
90 * - single-threaded process
91 * - multi-threaded process
92 * - thread in a MT process
93 *
94 * 1 2 3 4 5 6 7
95 * 1234567890123456789012345678901234567890123456789012345678901234567890
96 * pid ppid pgrp uid state wmesg wchan cmd
97 * <pid> <ppi> <pgi> <uid> <stat> <wmesg> <wchan > <name>
98 * <pid> <ppi> <pgi> <uid> <stat> (threaded) <command>
99 * <tid > <stat> <wmesg> <wchan > <name>
100 *
101 * For machines with 64-bit pointers, we expand the wchan field 8 more
102 * characters.
103 */
104 void
db_ps(db_expr_t addr,bool hasaddr,db_expr_t count,char * modif)105 db_ps(db_expr_t addr, bool hasaddr, db_expr_t count, char *modif)
106 {
107 struct proc *p;
108 int i;
109
110 ps_mode = modif[0] == 'a' ? PRINT_ARGS : PRINT_NONE;
111
112 #ifdef __LP64__
113 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
114 #else
115 db_printf(" pid ppid pgrp uid state wmesg wchan cmd\n");
116 #endif
117
118 if (!LIST_EMPTY(&allproc))
119 p = LIST_FIRST(&allproc);
120 else
121 p = &proc0;
122 for (; p != NULL && !db_pager_quit; p = LIST_NEXT(p, p_list))
123 db_ps_proc(p);
124
125 /*
126 * Processes such as zombies not in allproc.
127 */
128 for (i = 0; i <= pidhash && !db_pager_quit; i++) {
129 LIST_FOREACH(p, &pidhashtbl[i], p_hash) {
130 if (p->p_list.le_prev == NULL)
131 db_ps_proc(p);
132 }
133 }
134 }
135
136 static void
db_ps_proc(struct proc * p)137 db_ps_proc(struct proc *p)
138 {
139 volatile struct proc *pp;
140 volatile struct thread *td;
141 struct ucred *cred;
142 struct pgrp *pgrp;
143 char state[9];
144 int rflag, sflag, dflag, lflag, wflag;
145
146 pp = p->p_pptr;
147 if (pp == NULL)
148 pp = p;
149
150 cred = p->p_ucred;
151 pgrp = p->p_pgrp;
152 db_printf("%5d %5d %5d %5d ", p->p_pid, pp->p_pid,
153 pgrp != NULL ? pgrp->pg_id : 0,
154 cred != NULL ? cred->cr_ruid : 0);
155
156 /* Determine our primary process state. */
157 switch (p->p_state) {
158 case PRS_NORMAL:
159 if (P_SHOULDSTOP(p))
160 state[0] = 'T';
161 else {
162 /*
163 * One of D, L, R, S, W. For a
164 * multithreaded process we will use
165 * the state of the thread with the
166 * highest precedence. The
167 * precendence order from high to low
168 * is R, L, D, S, W. If no thread is
169 * in a sane state we use '?' for our
170 * primary state.
171 */
172 rflag = sflag = dflag = lflag = wflag = 0;
173 FOREACH_THREAD_IN_PROC(p, td) {
174 if (td->td_state == TDS_RUNNING ||
175 td->td_state == TDS_RUNQ ||
176 td->td_state == TDS_CAN_RUN)
177 rflag++;
178 if (TD_ON_LOCK(td))
179 lflag++;
180 if (TD_IS_SLEEPING(td)) {
181 if (!(td->td_flags & TDF_SINTR))
182 dflag++;
183 else
184 sflag++;
185 }
186 if (TD_AWAITING_INTR(td))
187 wflag++;
188 }
189 if (rflag)
190 state[0] = 'R';
191 else if (lflag)
192 state[0] = 'L';
193 else if (dflag)
194 state[0] = 'D';
195 else if (sflag)
196 state[0] = 'S';
197 else if (wflag)
198 state[0] = 'W';
199 else
200 state[0] = '?';
201 }
202 break;
203 case PRS_NEW:
204 state[0] = 'N';
205 break;
206 case PRS_ZOMBIE:
207 state[0] = 'Z';
208 break;
209 default:
210 state[0] = 'U';
211 break;
212 }
213 state[1] = '\0';
214
215 /* Additional process state flags. */
216 if (!(p->p_flag & P_INMEM))
217 strlcat(state, "W", sizeof(state));
218 if (p->p_flag & P_TRACED)
219 strlcat(state, "X", sizeof(state));
220 if (p->p_flag & P_WEXIT && p->p_state != PRS_ZOMBIE)
221 strlcat(state, "E", sizeof(state));
222 if (p->p_flag & P_PPWAIT)
223 strlcat(state, "V", sizeof(state));
224 if (p->p_flag & P_SYSTEM || p->p_lock > 0)
225 strlcat(state, "L", sizeof(state));
226 if (p->p_pgrp != NULL && p->p_session != NULL &&
227 SESS_LEADER(p))
228 strlcat(state, "s", sizeof(state));
229 /* Cheated here and didn't compare pgid's. */
230 if (p->p_flag & P_CONTROLT)
231 strlcat(state, "+", sizeof(state));
232 if (cred != NULL && jailed(cred))
233 strlcat(state, "J", sizeof(state));
234 db_printf(" %-6.6s ", state);
235 if (p->p_flag & P_HADTHREADS) {
236 #ifdef __LP64__
237 db_printf(" (threaded) ");
238 #else
239 db_printf(" (threaded) ");
240 #endif
241 if (p->p_flag & P_SYSTEM)
242 db_printf("[");
243 db_printf("%s", p->p_comm);
244 if (p->p_flag & P_SYSTEM)
245 db_printf("]");
246 if (ps_mode == PRINT_ARGS) {
247 db_printf(" ");
248 dump_args(p);
249 }
250 db_printf("\n");
251 }
252 FOREACH_THREAD_IN_PROC(p, td) {
253 dumpthread(p, td, p->p_flag & P_HADTHREADS);
254 if (db_pager_quit)
255 break;
256 }
257 }
258
259 static void
dumpthread(volatile struct proc * p,volatile struct thread * td,int all)260 dumpthread(volatile struct proc *p, volatile struct thread *td, int all)
261 {
262 char state[9], wprefix;
263 const char *wmesg;
264 const void *wchan;
265
266 if (all) {
267 db_printf("%6d ", td->td_tid);
268 switch (td->td_state) {
269 case TDS_RUNNING:
270 snprintf(state, sizeof(state), "Run");
271 break;
272 case TDS_RUNQ:
273 snprintf(state, sizeof(state), "RunQ");
274 break;
275 case TDS_CAN_RUN:
276 snprintf(state, sizeof(state), "CanRun");
277 break;
278 case TDS_INACTIVE:
279 snprintf(state, sizeof(state), "Inactv");
280 break;
281 case TDS_INHIBITED:
282 state[0] = '\0';
283 if (TD_ON_LOCK(td))
284 strlcat(state, "L", sizeof(state));
285 if (TD_IS_SLEEPING(td)) {
286 if (td->td_flags & TDF_SINTR)
287 strlcat(state, "S", sizeof(state));
288 else
289 strlcat(state, "D", sizeof(state));
290 }
291 if (TD_IS_SWAPPED(td))
292 strlcat(state, "W", sizeof(state));
293 if (TD_AWAITING_INTR(td))
294 strlcat(state, "I", sizeof(state));
295 if (TD_IS_SUSPENDED(td))
296 strlcat(state, "s", sizeof(state));
297 if (state[0] != '\0')
298 break;
299 default:
300 snprintf(state, sizeof(state), "???");
301 }
302 db_printf(" %-6.6s ", state);
303 }
304 wprefix = ' ';
305 if (TD_ON_LOCK(td)) {
306 wprefix = '*';
307 wmesg = td->td_lockname;
308 wchan = td->td_blocked;
309 } else if (TD_ON_SLEEPQ(td)) {
310 wmesg = td->td_wmesg;
311 wchan = td->td_wchan;
312 } else if (TD_IS_RUNNING(td)) {
313 snprintf(state, sizeof(state), "CPU %d", td->td_oncpu);
314 wmesg = state;
315 wchan = NULL;
316 } else {
317 wmesg = "";
318 wchan = NULL;
319 }
320 db_printf("%c%-7.7s ", wprefix, wmesg);
321 if (wchan == NULL)
322 #ifdef __LP64__
323 db_printf("%18s ", "");
324 #else
325 db_printf("%10s ", "");
326 #endif
327 else
328 db_printf("%p ", wchan);
329 if (p->p_flag & P_SYSTEM)
330 db_printf("[");
331 if (td->td_name[0] != '\0')
332 db_printf("%s", td->td_name);
333 else
334 db_printf("%s", td->td_proc->p_comm);
335 if (p->p_flag & P_SYSTEM)
336 db_printf("]");
337 if (ps_mode == PRINT_ARGS && all == 0) {
338 db_printf(" ");
339 dump_args(p);
340 }
341 db_printf("\n");
342 }
343
DB_SHOW_COMMAND(thread,db_show_thread)344 DB_SHOW_COMMAND(thread, db_show_thread)
345 {
346 struct thread *td;
347 struct lock_object *lock;
348 u_int delta;
349 bool comma;
350
351 /* Determine which thread to examine. */
352 if (have_addr)
353 td = db_lookup_thread(addr, false);
354 else
355 td = kdb_thread;
356 lock = (struct lock_object *)td->td_lock;
357
358 db_printf("Thread %d at %p:\n", td->td_tid, td);
359 db_printf(" proc (pid %d): %p\n", td->td_proc->p_pid, td->td_proc);
360 if (td->td_name[0] != '\0')
361 db_printf(" name: %s\n", td->td_name);
362 db_printf(" pcb: %p\n", td->td_pcb);
363 db_printf(" stack: %p-%p\n", (void *)td->td_kstack,
364 (void *)(td->td_kstack + td->td_kstack_pages * PAGE_SIZE - 1));
365 db_printf(" flags: %#x ", td->td_flags);
366 db_printf(" pflags: %#x\n", td->td_pflags);
367 db_printf(" state: ");
368 switch (td->td_state) {
369 case TDS_INACTIVE:
370 db_printf("INACTIVE\n");
371 break;
372 case TDS_CAN_RUN:
373 db_printf("CAN RUN\n");
374 break;
375 case TDS_RUNQ:
376 db_printf("RUNQ\n");
377 break;
378 case TDS_RUNNING:
379 db_printf("RUNNING (CPU %d)\n", td->td_oncpu);
380 break;
381 case TDS_INHIBITED:
382 db_printf("INHIBITED: {");
383 comma = false;
384 if (TD_IS_SLEEPING(td)) {
385 db_printf("SLEEPING");
386 comma = true;
387 }
388 if (TD_IS_SUSPENDED(td)) {
389 if (comma)
390 db_printf(", ");
391 db_printf("SUSPENDED");
392 comma = true;
393 }
394 if (TD_IS_SWAPPED(td)) {
395 if (comma)
396 db_printf(", ");
397 db_printf("SWAPPED");
398 comma = true;
399 }
400 if (TD_ON_LOCK(td)) {
401 if (comma)
402 db_printf(", ");
403 db_printf("LOCK");
404 comma = true;
405 }
406 if (TD_AWAITING_INTR(td)) {
407 if (comma)
408 db_printf(", ");
409 db_printf("IWAIT");
410 }
411 db_printf("}\n");
412 break;
413 default:
414 db_printf("??? (%#x)\n", td->td_state);
415 break;
416 }
417 if (TD_ON_LOCK(td))
418 db_printf(" lock: %s turnstile: %p\n", td->td_lockname,
419 td->td_blocked);
420 if (TD_ON_SLEEPQ(td))
421 db_printf(
422 " wmesg: %s wchan: %p sleeptimo %lx. %jx (curr %lx. %jx)\n",
423 td->td_wmesg, td->td_wchan,
424 (long)sbttobt(td->td_sleeptimo).sec,
425 (uintmax_t)sbttobt(td->td_sleeptimo).frac,
426 (long)sbttobt(sbinuptime()).sec,
427 (uintmax_t)sbttobt(sbinuptime()).frac);
428 db_printf(" priority: %d\n", td->td_priority);
429 db_printf(" container lock: %s (%p)\n", lock->lo_name, lock);
430 if (td->td_swvoltick != 0) {
431 delta = ticks - td->td_swvoltick;
432 db_printf(" last voluntary switch: %u.%03u s ago\n",
433 delta / hz, (delta % hz) * 1000 / hz);
434 }
435 if (td->td_swinvoltick != 0) {
436 delta = ticks - td->td_swinvoltick;
437 db_printf(" last involuntary switch: %u.%03u s ago\n",
438 delta / hz, (delta % hz) * 1000 / hz);
439 }
440 }
441
DB_SHOW_COMMAND(proc,db_show_proc)442 DB_SHOW_COMMAND(proc, db_show_proc)
443 {
444 struct thread *td;
445 struct proc *p;
446 int i;
447
448 /* Determine which process to examine. */
449 if (have_addr)
450 p = db_lookup_proc(addr);
451 else
452 p = kdb_thread->td_proc;
453
454 db_printf("Process %d (%s) at %p:\n", p->p_pid, p->p_comm, p);
455 db_printf(" state: ");
456 switch (p->p_state) {
457 case PRS_NEW:
458 db_printf("NEW\n");
459 break;
460 case PRS_NORMAL:
461 db_printf("NORMAL\n");
462 break;
463 case PRS_ZOMBIE:
464 db_printf("ZOMBIE\n");
465 break;
466 default:
467 db_printf("??? (%#x)\n", p->p_state);
468 }
469 if (p->p_ucred != NULL) {
470 db_printf(" uid: %d gids: ", p->p_ucred->cr_uid);
471 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
472 db_printf("%d", p->p_ucred->cr_groups[i]);
473 if (i < (p->p_ucred->cr_ngroups - 1))
474 db_printf(", ");
475 }
476 db_printf("\n");
477 }
478 if (p->p_pptr != NULL)
479 db_printf(" parent: pid %d at %p\n", p->p_pptr->p_pid,
480 p->p_pptr);
481 if (p->p_leader != NULL && p->p_leader != p)
482 db_printf(" leader: pid %d at %p\n", p->p_leader->p_pid,
483 p->p_leader);
484 if (p->p_sysent != NULL)
485 db_printf(" ABI: %s\n", p->p_sysent->sv_name);
486 db_printf(" flag: %#x ", p->p_flag);
487 db_printf(" flag2: %#x\n", p->p_flag2);
488 if (p->p_args != NULL) {
489 db_printf(" arguments: ");
490 dump_args(p);
491 db_printf("\n");
492 }
493 db_printf(" reaper: %p reapsubtree: %d\n",
494 p->p_reaper, p->p_reapsubtree);
495 db_printf(" sigparent: %d\n", p->p_sigparent);
496 db_printf(" vmspace: %p\n", p->p_vmspace);
497 db_printf(" (map %p)\n",
498 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map : 0);
499 db_printf(" (map.pmap %p)\n",
500 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_map.pmap : 0);
501 db_printf(" (pmap %p)\n",
502 (p->p_vmspace != NULL) ? &p->p_vmspace->vm_pmap : 0);
503 db_printf(" threads: %d\n", p->p_numthreads);
504 FOREACH_THREAD_IN_PROC(p, td) {
505 dumpthread(p, td, 1);
506 if (db_pager_quit)
507 break;
508 }
509 }
510
511 void
db_findstack_cmd(db_expr_t addr,bool have_addr,db_expr_t dummy3 __unused,char * dummy4 __unused)512 db_findstack_cmd(db_expr_t addr, bool have_addr, db_expr_t dummy3 __unused,
513 char *dummy4 __unused)
514 {
515 struct thread *td;
516 vm_offset_t saddr;
517
518 if (have_addr)
519 saddr = addr;
520 else {
521 db_printf("Usage: findstack <address>\n");
522 return;
523 }
524
525 for (td = kdb_thr_first(); td != NULL; td = kdb_thr_next(td)) {
526 if (kstack_contains(td, saddr, 1)) {
527 db_printf("Thread %p\n", td);
528 return;
529 }
530 }
531 }
532