xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_debug.c (revision bc1f688b)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
28*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
29*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_io.h>
30*7c478bd9Sstevel@tonic-gate #include <mdb/mdb_lex.h>
31*7c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <libproc.h>
34*7c478bd9Sstevel@tonic-gate #include <libctf.h>
35*7c478bd9Sstevel@tonic-gate #include <rtld_db.h>
36*7c478bd9Sstevel@tonic-gate #include <strings.h>
37*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate typedef struct dbg_mode {
40*7c478bd9Sstevel@tonic-gate 	const char *m_name;
41*7c478bd9Sstevel@tonic-gate 	const char *m_desc;
42*7c478bd9Sstevel@tonic-gate 	uint_t m_bits;
43*7c478bd9Sstevel@tonic-gate } dbg_mode_t;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static const dbg_mode_t dbg_modetab[] = {
46*7c478bd9Sstevel@tonic-gate 	{ "cmdbuf", "debug command editing buffer", MDB_DBG_CMDBUF },
47*7c478bd9Sstevel@tonic-gate #ifdef YYDEBUG
48*7c478bd9Sstevel@tonic-gate 	{ "parser", "debug parser internals", MDB_DBG_PARSER },
49*7c478bd9Sstevel@tonic-gate #endif
50*7c478bd9Sstevel@tonic-gate 	{ "help", "display this listing", MDB_DBG_HELP },
51*7c478bd9Sstevel@tonic-gate 	{ "module", "debug module processing", MDB_DBG_MODULE },
52*7c478bd9Sstevel@tonic-gate 	{ "dcmd", "debug dcmd processing", MDB_DBG_DCMD },
53*7c478bd9Sstevel@tonic-gate 	{ "elf", "debug ELF file processing", MDB_DBG_ELF },
54*7c478bd9Sstevel@tonic-gate 	{ "mach", "debug machine-dependent code", MDB_DBG_MACH },
55*7c478bd9Sstevel@tonic-gate 	{ "shell", "debug shell escapes", MDB_DBG_SHELL },
56*7c478bd9Sstevel@tonic-gate 	{ "kmod", "debug kernel module processing", MDB_DBG_KMOD },
57*7c478bd9Sstevel@tonic-gate 	{ "walk", "debug walk callback processing", MDB_DBG_WALK },
58*7c478bd9Sstevel@tonic-gate 	{ "umem", "debug memory management", MDB_DBG_UMEM },
59*7c478bd9Sstevel@tonic-gate 	{ "dstk", "debug execution stack", MDB_DBG_DSTK },
60*7c478bd9Sstevel@tonic-gate 	{ "tgt", "debug target backends", MDB_DBG_TGT },
61*7c478bd9Sstevel@tonic-gate 	{ "psvc", "debug proc_service clients", MDB_DBG_PSVC },
62*7c478bd9Sstevel@tonic-gate 	{ "proc", "debug libproc internals", MDB_DBG_PROC },
63*7c478bd9Sstevel@tonic-gate 	{ "ctf", "debug libctf internals", MDB_DBG_CTF },
64*7c478bd9Sstevel@tonic-gate 	{ "dpi", "debugger/PROM interface (kmdb only)", MDB_DBG_DPI },
65*7c478bd9Sstevel@tonic-gate 	{ "kdi", "kernel/debugger interface (kmdb only)", MDB_DBG_KDI },
66*7c478bd9Sstevel@tonic-gate 	{ "callb", "debug callback invocations", MDB_DBG_CALLB },
67*7c478bd9Sstevel@tonic-gate 	{ "all", "set all debug modes", (uint_t)~MDB_DBG_HELP },
68*7c478bd9Sstevel@tonic-gate 	{ "none", "unset all debug modes", 0 },
69*7c478bd9Sstevel@tonic-gate 	{ NULL, 0 }
70*7c478bd9Sstevel@tonic-gate };
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate static const char dbg_prefix[] = "mdb DEBUG: ";
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
75*7c478bd9Sstevel@tonic-gate void
mdb_dprintf(uint_t mode,const char * format,...)76*7c478bd9Sstevel@tonic-gate mdb_dprintf(uint_t mode, const char *format, ...)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	if ((mdb.m_debug & mode) == mode && mdb.m_err != NULL) {
79*7c478bd9Sstevel@tonic-gate 		va_list alist;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 		mdb_iob_puts(mdb.m_err, dbg_prefix);
82*7c478bd9Sstevel@tonic-gate 		va_start(alist, format);
83*7c478bd9Sstevel@tonic-gate 		mdb_iob_vprintf(mdb.m_err, format, alist);
84*7c478bd9Sstevel@tonic-gate 		va_end(alist);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate void
mdb_dvprintf(uint_t mode,const char * format,va_list alist)89*7c478bd9Sstevel@tonic-gate mdb_dvprintf(uint_t mode, const char *format, va_list alist)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	if ((mdb.m_debug & mode) == mode && format != NULL && *format != '\0' &&
92*7c478bd9Sstevel@tonic-gate 	    mdb.m_err != NULL) {
93*7c478bd9Sstevel@tonic-gate 		mdb_iob_puts(mdb.m_err, dbg_prefix);
94*7c478bd9Sstevel@tonic-gate 		mdb_iob_vprintf(mdb.m_err, format, alist);
95*7c478bd9Sstevel@tonic-gate 		if (format[strlen(format) - 1] != '\n')
96*7c478bd9Sstevel@tonic-gate 			mdb_iob_nl(mdb.m_err);
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate uint_t
mdb_dstr2mode(const char * s)101*7c478bd9Sstevel@tonic-gate mdb_dstr2mode(const char *s)
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	const dbg_mode_t *mp;
104*7c478bd9Sstevel@tonic-gate 	const char *name;
105*7c478bd9Sstevel@tonic-gate 	char dstr[256];
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	uint_t bits = 0;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (s == NULL)
110*7c478bd9Sstevel@tonic-gate 		return (0);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	(void) strncpy(dstr, s, sizeof (dstr));
113*7c478bd9Sstevel@tonic-gate 	dstr[sizeof (dstr) - 1] = '\0';
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	for (name = strtok(dstr, ","); name; name = strtok(NULL, ",")) {
116*7c478bd9Sstevel@tonic-gate 		for (mp = dbg_modetab; mp->m_name != NULL; mp++) {
117*7c478bd9Sstevel@tonic-gate 			if (strcmp(name, mp->m_name) == 0) {
118*7c478bd9Sstevel@tonic-gate 				if (mp->m_bits != 0)
119*7c478bd9Sstevel@tonic-gate 					bits |= mp->m_bits;
120*7c478bd9Sstevel@tonic-gate 				else
121*7c478bd9Sstevel@tonic-gate 					bits = 0;
122*7c478bd9Sstevel@tonic-gate 				break;
123*7c478bd9Sstevel@tonic-gate 			}
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 		if (mp->m_name == NULL)
127*7c478bd9Sstevel@tonic-gate 			warn("unknown debug option \"%s\"\n", name);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (bits & MDB_DBG_HELP) {
131*7c478bd9Sstevel@tonic-gate 		warn("Debugging tokens:\n");
132*7c478bd9Sstevel@tonic-gate 		for (mp = dbg_modetab; mp->m_name != NULL; mp++)
133*7c478bd9Sstevel@tonic-gate 			warn("\t%s: %s\n", mp->m_name, mp->m_desc);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	return (bits);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate void
mdb_dmode(uint_t bits)140*7c478bd9Sstevel@tonic-gate mdb_dmode(uint_t bits)
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate 	int *libproc_debugp, *libctf_debugp;
143*7c478bd9Sstevel@tonic-gate 	void (*rd_logp)(const int);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	if ((libproc_debugp = dlsym(RTLD_SELF, "_libproc_debug")) != NULL)
146*7c478bd9Sstevel@tonic-gate 		*libproc_debugp = (bits & MDB_DBG_PROC) != 0;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if ((libctf_debugp = dlsym(RTLD_SELF, "_libctf_debug")) != NULL)
149*7c478bd9Sstevel@tonic-gate 		*libctf_debugp = (bits & MDB_DBG_CTF) != 0;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if ((rd_logp = (void (*)())dlsym(RTLD_SELF, "rd_log")) != NULL)
152*7c478bd9Sstevel@tonic-gate 		rd_logp((bits & MDB_DBG_PSVC) != 0);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	mdb_lex_debug(bits & MDB_DBG_PARSER);
155*7c478bd9Sstevel@tonic-gate 	mdb.m_debug = bits;
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate int
mdb_dassert(const char * expr,const char * file,int line)159*7c478bd9Sstevel@tonic-gate mdb_dassert(const char *expr, const char *file, int line)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	fail("\"%s\", line %d: assertion failed: %s\n", file, line, expr);
162*7c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
163*7c478bd9Sstevel@tonic-gate 	return (0);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate /*
167*7c478bd9Sstevel@tonic-gate  * Function to convert mdb longjmp codes (see <mdb/mdb.h>) into a string for
168*7c478bd9Sstevel@tonic-gate  * debugging routines.
169*7c478bd9Sstevel@tonic-gate  */
170*7c478bd9Sstevel@tonic-gate const char *
mdb_err2str(int err)171*7c478bd9Sstevel@tonic-gate mdb_err2str(int err)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	static const char *const errtab[] = {
174*7c478bd9Sstevel@tonic-gate 		"0", "PARSE", "NOMEM", "PAGER", "SIGINT",
175*7c478bd9Sstevel@tonic-gate 		"QUIT", "ASSERT", "API", "ABORT", "OUTPUT"
176*7c478bd9Sstevel@tonic-gate 	};
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	static char buf[32];
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (err >= 0 && err < sizeof (errtab) / sizeof (errtab[0]))
181*7c478bd9Sstevel@tonic-gate 		return (errtab[err]);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	(void) mdb_iob_snprintf(buf, sizeof (buf), "ERR#%d", err);
184*7c478bd9Sstevel@tonic-gate 	return (buf);
185*7c478bd9Sstevel@tonic-gate }
186