Home | History | Annotate | Line # | Download | only in ddb
db_xxx.c revision 1.66.8.2
      1  1.66.8.2  jdolecek /*	$NetBSD: db_xxx.c,v 1.66.8.2 2017/12/03 11:36:58 jdolecek Exp $	*/
      2       1.1       gwr 
      3       1.1       gwr /*
      4       1.1       gwr  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5       1.1       gwr  *	The Regents of the University of California.  All rights reserved.
      6       1.1       gwr  *
      7       1.1       gwr  * Redistribution and use in source and binary forms, with or without
      8       1.1       gwr  * modification, are permitted provided that the following conditions
      9       1.1       gwr  * are met:
     10       1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     11       1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     12       1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       gwr  *    documentation and/or other materials provided with the distribution.
     15      1.26       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.1       gwr  *    may be used to endorse or promote products derived from this software
     17       1.1       gwr  *    without specific prior written permission.
     18       1.1       gwr  *
     19       1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.1       gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.1       gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.1       gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.1       gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.1       gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.1       gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.1       gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.1       gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1       gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1       gwr  * SUCH DAMAGE.
     30       1.1       gwr  *
     31       1.1       gwr  *	from: kern_proc.c	8.4 (Berkeley) 1/4/94
     32       1.1       gwr  */
     33       1.1       gwr 
     34       1.1       gwr /*
     35       1.1       gwr  * Miscellaneous DDB functions that are intimate (xxx) with various
     36       1.1       gwr  * data structures and functions used by the kernel (proc, callout).
     37       1.1       gwr  */
     38      1.15     lukem 
     39      1.46       dsl #include <sys/cdefs.h>
     40  1.66.8.2  jdolecek __KERNEL_RCSID(0, "$NetBSD: db_xxx.c,v 1.66.8.2 2017/12/03 11:36:58 jdolecek Exp $");
     41      1.46       dsl 
     42      1.57        ad #ifdef _KERNEL_OPT
     43      1.21    briggs #include "opt_kgdb.h"
     44      1.53        ad #include "opt_aio.h"
     45      1.62     rmind #include "opt_mqueue.h"
     46      1.57        ad #endif
     47      1.57        ad 
     48      1.59       mrg #ifndef _KERNEL
     49      1.59       mrg #include <stdbool.h>
     50      1.59       mrg #endif
     51      1.59       mrg 
     52       1.1       gwr #include <sys/param.h>
     53       1.1       gwr #include <sys/systm.h>
     54       1.1       gwr #include <sys/kernel.h>
     55       1.1       gwr #include <sys/proc.h>
     56      1.12    atatat #include <sys/msgbuf.h>
     57       1.1       gwr #include <sys/callout.h>
     58      1.49     blymn #include <sys/file.h>
     59      1.49     blymn #include <sys/filedesc.h>
     60      1.49     blymn #include <sys/lockdebug.h>
     61       1.1       gwr #include <sys/signalvar.h>
     62       1.3      ross #include <sys/resourcevar.h>
     63      1.34        he #include <sys/pool.h>
     64      1.57        ad #include <sys/uio.h>
     65      1.38      elad #include <sys/kauth.h>
     66      1.47     rmind #include <sys/mqueue.h>
     67      1.49     blymn #include <sys/vnode.h>
     68      1.54        ad #include <sys/module.h>
     69      1.55        ad #include <sys/cpu.h>
     70      1.56      yamt #include <sys/vmem.h>
     71       1.1       gwr 
     72      1.57        ad #include <ddb/ddb.h>
     73      1.58       mrg #include <ddb/db_user.h>
     74       1.1       gwr 
     75      1.21    briggs #ifdef KGDB
     76      1.21    briggs #include <sys/kgdb.h>
     77      1.21    briggs #endif
     78       1.1       gwr 
     79       1.1       gwr void
     80      1.42      matt db_kill_proc(db_expr_t addr, bool haddr,
     81      1.40  christos     db_expr_t count, const char *modif)
     82       1.1       gwr {
     83      1.63  christos #ifdef _KERNEL	/* XXX CRASH(8) */
     84      1.63  christos 	struct proc *p;
     85      1.63  christos 	ksiginfo_t	ksi;
     86      1.63  christos 	db_expr_t pid, sig;
     87      1.63  christos 	int t;
     88      1.63  christos 
     89      1.63  christos 	/* What pid? */
     90      1.63  christos 	if (!db_expression(&pid)) {
     91      1.63  christos 	       db_error("pid?\n");
     92      1.63  christos 	       /*NOTREACHED*/
     93      1.63  christos 	}
     94      1.63  christos 	/* What sig? */
     95      1.63  christos 	t = db_read_token();
     96      1.63  christos 	if (t == tCOMMA) {
     97      1.63  christos 	       if (!db_expression(&sig)) {
     98      1.63  christos 		       db_error("sig?\n");
     99      1.63  christos 		       /*NOTREACHED*/
    100      1.63  christos 	       }
    101      1.63  christos 	} else {
    102      1.63  christos 	       db_unread_token(t);
    103      1.63  christos 	       sig = 15;
    104      1.63  christos 	}
    105      1.63  christos 	if (db_read_token() != tEOL) {
    106      1.63  christos 	       db_error("?\n");
    107      1.63  christos 	       /*NOTREACHED*/
    108      1.63  christos 	}
    109      1.65  christos 	/* We might stop when the mutex is held or when not */
    110      1.65  christos 	t = mutex_tryenter(proc_lock);
    111      1.66  christos #ifdef DIAGNOSTIC
    112      1.66  christos 	if (!t) {
    113      1.66  christos 	       db_error("could not acquire proc_lock mutex\n");
    114      1.66  christos 	       /*NOTREACHED*/
    115      1.66  christos 	}
    116      1.66  christos #endif
    117      1.63  christos 	p = proc_find((pid_t)pid);
    118      1.63  christos 	if (p == NULL) {
    119      1.66  christos 		if (t)
    120      1.66  christos 			mutex_exit(proc_lock);
    121      1.66  christos 		db_error("no such proc\n");
    122      1.66  christos 		/*NOTREACHED*/
    123      1.63  christos 	}
    124      1.63  christos 	KSI_INIT(&ksi);
    125      1.63  christos 	ksi.ksi_signo = sig;
    126      1.63  christos 	ksi.ksi_code = SI_USER;
    127      1.63  christos 	ksi.ksi_pid = 0;
    128      1.63  christos 	ksi.ksi_uid = 0;
    129      1.66  christos 	mutex_enter(p->p_lock);
    130      1.63  christos 	kpsignal2(p, &ksi);
    131      1.66  christos 	mutex_exit(p->p_lock);
    132      1.66  christos 	if (t)
    133      1.66  christos 		mutex_exit(proc_lock);
    134      1.63  christos #else
    135      1.57        ad 	db_printf("This command is not currently supported.\n");
    136      1.63  christos #endif
    137       1.1       gwr }
    138      1.21    briggs 
    139      1.21    briggs #ifdef KGDB
    140      1.21    briggs void
    141      1.42      matt db_kgdb_cmd(db_expr_t addr, bool haddr,
    142      1.40  christos     db_expr_t count, const char *modif)
    143      1.21    briggs {
    144      1.21    briggs 	kgdb_active++;
    145      1.21    briggs 	kgdb_trap(db_trap_type, DDB_REGS);
    146      1.21    briggs 	kgdb_active--;
    147      1.21    briggs }
    148      1.21    briggs #endif
    149       1.1       gwr 
    150       1.1       gwr void
    151      1.49     blymn db_show_files_cmd(db_expr_t addr, bool haddr,
    152      1.49     blymn 	      db_expr_t count, const char *modif)
    153      1.49     blymn {
    154      1.57        ad #ifdef _KERNEL	/* XXX CRASH(8) */
    155      1.49     blymn 	struct proc *p;
    156      1.49     blymn 	int i;
    157      1.50     blymn 	filedesc_t *fdp;
    158      1.50     blymn 	fdfile_t *ff;
    159      1.50     blymn 	file_t *fp;
    160  1.66.8.2  jdolecek 	struct vnode *vp;
    161      1.49     blymn 	bool full = false;
    162      1.61        ad 	fdtab_t *dt;
    163      1.49     blymn 
    164  1.66.8.2  jdolecek 	if (!haddr) {
    165  1.66.8.2  jdolecek 		db_printf("usage: show files address\n");
    166  1.66.8.2  jdolecek 		db_printf("\taddress == an address of a proc structure\n");
    167  1.66.8.2  jdolecek 		return;
    168  1.66.8.2  jdolecek 	}
    169  1.66.8.2  jdolecek 
    170      1.49     blymn 	if (modif[0] == 'f')
    171      1.49     blymn 		full = true;
    172      1.49     blymn 
    173      1.52     rmind 	p = (struct proc *) (uintptr_t) addr;
    174      1.49     blymn 
    175      1.50     blymn 	fdp = p->p_fd;
    176      1.61        ad 	dt = fdp->fd_dt;
    177      1.61        ad 	for (i = 0; i < dt->dt_nfiles; i++) {
    178      1.61        ad 		if ((ff = dt->dt_ff[i]) == NULL)
    179      1.49     blymn 			continue;
    180      1.49     blymn 
    181      1.49     blymn 		fp = ff->ff_file;
    182      1.49     blymn 
    183      1.49     blymn 		/* Only look at vnodes... */
    184  1.66.8.2  jdolecek 		if (fp != NULL && fp->f_type == DTYPE_VNODE
    185  1.66.8.2  jdolecek 		    && fp->f_vnode != NULL) {
    186  1.66.8.2  jdolecek 			vp = fp->f_vnode;
    187  1.66.8.2  jdolecek 			vfs_vnode_print(vp, full, db_printf);
    188      1.49     blymn 
    189      1.49     blymn #ifdef LOCKDEBUG
    190  1.66.8.2  jdolecek 			db_printf("\nv_uobj.vmobjlock lock details:\n");
    191  1.66.8.2  jdolecek 			lockdebug_lock_print(vp->v_uobj.vmobjlock, db_printf);
    192  1.66.8.2  jdolecek 			db_printf("\n");
    193      1.49     blymn #endif
    194      1.49     blymn 		}
    195      1.49     blymn 	}
    196      1.57        ad #endif
    197      1.49     blymn }
    198      1.49     blymn 
    199      1.53        ad #ifdef AIO
    200      1.49     blymn void
    201      1.43     rmind db_show_aio_jobs(db_expr_t addr, bool haddr,
    202      1.43     rmind     db_expr_t count, const char *modif)
    203      1.43     rmind {
    204      1.57        ad 
    205      1.43     rmind 	aio_print_jobs(db_printf);
    206      1.43     rmind }
    207      1.53        ad #endif
    208      1.43     rmind 
    209      1.62     rmind #ifdef MQUEUE
    210      1.43     rmind void
    211      1.47     rmind db_show_mqueue_cmd(db_expr_t addr, bool haddr,
    212      1.47     rmind     db_expr_t count, const char *modif)
    213      1.47     rmind {
    214      1.57        ad 
    215      1.57        ad #ifdef _KERNEL	/* XXX CRASH(8) */
    216      1.47     rmind 	mqueue_print_list(db_printf);
    217      1.57        ad #endif
    218      1.47     rmind }
    219      1.62     rmind #endif
    220      1.47     rmind 
    221      1.47     rmind void
    222      1.54        ad db_show_module_cmd(db_expr_t addr, bool haddr,
    223      1.54        ad     db_expr_t count, const char *modif)
    224      1.54        ad {
    225      1.57        ad 
    226      1.57        ad #ifdef _KERNEL	/* XXX CRASH(8) */
    227      1.54        ad 	module_print_list(db_printf);
    228      1.57        ad #endif
    229      1.12    atatat }
    230      1.12    atatat 
    231      1.12    atatat void
    232      1.42      matt db_show_all_pools(db_expr_t addr, bool haddr,
    233      1.40  christos     db_expr_t count, const char *modif)
    234      1.33      yamt {
    235      1.33      yamt 
    236      1.57        ad #ifdef _KERNEL	/* XXX CRASH(8) */
    237      1.33      yamt 	pool_printall(modif, db_printf);
    238      1.57        ad #endif
    239      1.33      yamt }
    240      1.33      yamt 
    241      1.33      yamt void
    242      1.56      yamt db_show_all_vmems(db_expr_t addr, bool have_addr,
    243      1.56      yamt     db_expr_t count, const char *modif)
    244      1.56      yamt {
    245      1.56      yamt 
    246      1.57        ad #ifdef _KERNEL	/* XXX CRASH(8) */
    247      1.56      yamt 	vmem_printall(modif, db_printf);
    248      1.57        ad #endif
    249      1.56      yamt }
    250      1.56      yamt 
    251      1.56      yamt void
    252      1.60        ad db_dmesg(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    253      1.12    atatat {
    254      1.60        ad 	struct kern_msgbuf mb, *mbp;
    255      1.25    simonb 	db_expr_t print;
    256      1.60        ad 	int newl, skip, i;
    257      1.60        ad 	char *p, *bufdata, ch;
    258      1.22    atatat 
    259      1.60        ad 	if (!db_read_int("msgbufenabled")) {
    260      1.60        ad 		db_printf("message buffer not available\n");
    261      1.60        ad 		return;
    262      1.60        ad 	}
    263      1.60        ad 	mbp = (struct kern_msgbuf *)db_read_ptr("msgbufp");
    264      1.60        ad 	db_read_bytes((db_addr_t)mbp, sizeof(mb), (char *)&mb);
    265      1.60        ad 	if (mb.msg_magic != MSG_MAGIC) {
    266      1.22    atatat 		db_printf("message buffer not available\n");
    267      1.22    atatat 		return;
    268      1.22    atatat 	}
    269      1.12    atatat 
    270      1.12    atatat 	bufdata = &mbp->msg_bufc[0];
    271      1.12    atatat 
    272      1.60        ad 	if (haddr && addr < mb.msg_bufs)
    273      1.25    simonb 		print = addr;
    274      1.25    simonb 	else
    275      1.60        ad 		print = mb.msg_bufs;
    276      1.25    simonb 
    277      1.60        ad 	for (newl = skip = i = 0, p = bufdata + mb.msg_bufx;
    278      1.60        ad 	    i < mb.msg_bufs; i++, p++) {
    279      1.60        ad 		if (p == bufdata + mb.msg_bufs)
    280      1.12    atatat 			p = bufdata;
    281      1.60        ad 		if (i < mb.msg_bufs - print) {
    282      1.25    simonb 			continue;
    283      1.60        ad 		}
    284      1.60        ad 		db_read_bytes((db_addr_t)p, sizeof(ch), &ch);
    285      1.12    atatat 		/* Skip "\n<.*>" syslog sequences. */
    286      1.12    atatat 		if (skip) {
    287      1.12    atatat 			if (ch == '>')
    288      1.12    atatat 				newl = skip = 0;
    289      1.12    atatat 			continue;
    290      1.12    atatat 		}
    291      1.12    atatat 		if (newl && ch == '<') {
    292      1.12    atatat 			skip = 1;
    293      1.12    atatat 			continue;
    294      1.12    atatat 		}
    295      1.12    atatat 		if (ch == '\0')
    296      1.12    atatat 			continue;
    297      1.12    atatat 		newl = ch == '\n';
    298      1.12    atatat 		db_printf("%c", ch);
    299      1.12    atatat 	}
    300      1.12    atatat 	if (!newl)
    301      1.12    atatat 		db_printf("\n");
    302      1.28   thorpej }
    303      1.28   thorpej 
    304      1.28   thorpej void
    305      1.42      matt db_show_sched_qs(db_expr_t addr, bool haddr,
    306      1.40  christos     db_expr_t count, const char *modif)
    307      1.28   thorpej {
    308      1.44      yamt 
    309      1.57        ad #ifdef _KERNEL	/* XXX CRASH(8) */
    310      1.44      yamt 	sched_print_runqueue(db_printf);
    311      1.57        ad #endif
    312       1.1       gwr }
    313  1.66.8.1       tls 
    314  1.66.8.1       tls void
    315  1.66.8.1       tls db_show_panic(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    316  1.66.8.1       tls {
    317  1.66.8.1       tls #ifdef _KERNEL	/* XXX CRASH(8) */
    318  1.66.8.1       tls         int s;
    319  1.66.8.1       tls 
    320  1.66.8.1       tls 	s = splhigh();
    321  1.66.8.1       tls 
    322  1.66.8.1       tls 	db_printf("Panic string: %s\n", panicstr);
    323  1.66.8.1       tls 
    324  1.66.8.1       tls 	(void)splx(s);
    325  1.66.8.1       tls #endif
    326  1.66.8.1       tls }
    327