xref: /titanic_51/usr/src/cmd/mdb/common/kmdb/kmdb_main.c (revision b453864f3587ccc3324d7a3b0438a1e542dcfde7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/mman.h>
30 #include <sys/resource.h>
31 #include <sys/termios.h>
32 #include <sys/param.h>
33 #include <sys/salib.h>
34 
35 #include <alloca.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <libctf.h>
41 #include <errno.h>
42 #include <ctype.h>
43 
44 #include <kmdb/kmdb_promif.h>
45 #include <kmdb/kmdb_dpi.h>
46 #include <kmdb/kmdb_umemglue.h>
47 #include <kmdb/kmdb_io.h>
48 #include <kmdb/kmdb_dpi.h>
49 #include <kmdb/kmdb_wr.h>
50 #include <kmdb/kmdb_start.h>
51 #include <kmdb/kmdb_kdi.h>
52 #include <kmdb/kmdb_kvm.h>
53 #include <mdb/mdb_lex.h>
54 #include <mdb/mdb_debug.h>
55 #include <mdb/mdb_signal.h>
56 #include <mdb/mdb_string.h>
57 #include <mdb/mdb_modapi.h>
58 #include <mdb/mdb_target.h>
59 #include <mdb/mdb_gelf.h>
60 #include <mdb/mdb_conf.h>
61 #include <mdb/mdb_err.h>
62 #include <mdb/mdb_io_impl.h>
63 #include <mdb/mdb_frame.h>
64 #include <mdb/mdb_set.h>
65 #include <mdb/mdb.h>
66 
67 #ifdef __sparc
68 #define	KMDB_STACK_SIZE	(384 * 1024)
69 #else
70 #define	KMDB_STACK_SIZE (192 * 1024)
71 #endif
72 
73 caddr_t kmdb_main_stack;
74 size_t kmdb_main_stack_size;
75 
76 #define	KMDB_DEF_IPATH	"internal"
77 #if defined(_LP64)
78 #define	KMDB_DEF_LPATH	\
79 	"%r/platform/%p/kernel/kmdb/%i:" \
80 	"%r/platform/%m/kernel/kmdb/%i:" \
81 	"%r/kernel/kmdb/%i"
82 #else
83 #define	KMDB_DEF_LPATH	\
84 	"%r/platform/%m/kernel/kmdb:" \
85 	"%r/kernel/kmdb"
86 #endif
87 
88 #define	MDB_DEF_PROMPT "[%<_cpuid>]> "
89 
90 #define	KMDB_DEF_TERM_TYPE	"vt100"
91 
92 /*
93  * Similar to the panic_* variables in the kernel, we keep some relevant
94  * information stored in a set of global _mdb_abort_* variables; in the
95  * event that the debugger dumps core, these will aid core dump analysis.
96  */
97 const char *volatile _mdb_abort_str;	/* reason for failure */
98 
99 /*
100  * The kernel supplies a space-delimited list of directories
101  * (/platform/sun4u/kernel /kernel /usr/kernel ...) which we must transform into
102  * a debugger-friendly, colon-delimited module search path.  We add the kmdb
103  * module directory to each component and change the delimiter.
104  */
105 static char *
106 kmdb_modpath2lpath(const char *modpath)
107 {
108 #ifdef	_LP64
109 	static const char suffix[] = "/kmdb/%i:";
110 #else
111 	static const char suffix[] = "/kmdb:";
112 #endif
113 	const char *c;
114 	char *lpath, *lpend, *nlpath;
115 	size_t lpsz, lpres;
116 
117 	if (strchr(modpath, ':') != NULL) {
118 		warn("invalid kernel module path\n");
119 		return (NULL);
120 	}
121 
122 	lpres = lpsz = strlen(modpath) + MAXPATHLEN;
123 	lpend = lpath = mdb_zalloc(lpsz, UM_SLEEP);
124 
125 	while (isspace(*modpath))
126 		modpath++;
127 
128 	for (; *modpath != '\0'; modpath = c) {
129 		size_t sz;
130 
131 		for (c = modpath; !isspace(*c) && *c != '\0'; c++)
132 			continue;
133 
134 		sz = (c - modpath) + sizeof (suffix) - 1;
135 		if (sz >= lpres)
136 			continue;
137 
138 		(void) strncpy(lpend, modpath, c - modpath);
139 		(void) strcpy(lpend + (c - modpath), suffix);
140 
141 		lpend += sz;
142 		lpres -= sz;
143 
144 		while (isspace(*c))
145 			c++;
146 	}
147 
148 	if (lpend != lpath)
149 		lpend[-1] = '\0';	/* eat trailing colon */
150 
151 	nlpath = strdup(lpath);
152 	mdb_free(lpath, lpsz);
153 	return (nlpath);
154 }
155 
156 /*
157  * called while the kernel is running
158  */
159 int
160 kmdb_init(const char *execname, kmdb_auxv_t *kav)
161 {
162 	mdb_io_t *in_io, *out_io, *err_io, *null_io;
163 	mdb_tgt_ctor_f *tgt_ctor = kmdb_kvm_create;
164 	mdb_tgt_t *tgt;
165 	int i;
166 
167 	/*
168 	 * The beginnings of debugger initialization are a bit of a dance, due
169 	 * to interlocking dependencies between kmdb_prom_init,
170 	 * mdb_umem_startup, and mdb_create.  In particular, allocator
171 	 * initialization can't begin until prom_init() is called,
172 	 * kmdb_prom_init can't finish until the allocator is ready and
173 	 * mdb_create has been called.  We therefore split kmdb_prom_init into
174 	 * two pieces, and call thembefore and after umem initialization and
175 	 * mdb_create.
176 	 */
177 	kmdb_prom_init_begin("kmdb", kav);
178 	mdb_umem_startup(kav->kav_dseg, kav->kav_dseg_size,
179 	    kav->kav_pagesize);
180 	mdb_create(execname, "kmdb");
181 	kmdb_prom_init_finish(kav);
182 
183 	mdb.m_dseg = kav->kav_dseg;
184 	mdb.m_dsegsz = kav->kav_dseg_size;
185 
186 	out_io = kmdb_promio_create("stdout");
187 	mdb.m_out = mdb_iob_create(out_io, MDB_IOB_WRONLY);
188 
189 	err_io = kmdb_promio_create("stderr");
190 	mdb.m_err = mdb_iob_create(err_io, MDB_IOB_WRONLY);
191 	mdb_iob_clrflags(mdb.m_err, MDB_IOB_AUTOWRAP);
192 
193 	null_io = mdb_nullio_create();
194 	mdb.m_null = mdb_iob_create(null_io, MDB_IOB_WRONLY);
195 
196 	in_io = kmdb_promio_create("stdin");
197 	mdb.m_term = NULL;
198 
199 	if (kav->kav_config != NULL)
200 		mdb_set_config(kav->kav_config);
201 
202 	if (kav->kav_argv != NULL) {
203 		for (i = 0; kav->kav_argv[i] != NULL; i++) {
204 			if (!mdb_set_options(kav->kav_argv[i], TRUE))
205 				return (-1);
206 		}
207 	}
208 
209 	if (kav->kav_flags & KMDB_AUXV_FL_NOUNLOAD)
210 		mdb.m_flags |= MDB_FL_NOUNLOAD;
211 
212 	mdb.m_in = mdb_iob_create(in_io, MDB_IOB_RDONLY);
213 	mdb_iob_setflags(mdb.m_in, MDB_IOB_TTYLIKE);
214 
215 	mdb_lex_reset();
216 
217 	kmdb_kdi_init(kav->kav_kdi, kav);
218 
219 	if (kmdb_dpi_init(kav) < 0) {
220 		warn("Couldn't initialize kernel/PROM interface\n");
221 		return (-1);
222 	}
223 
224 	/*
225 	 * Path evaluation part 1: Create the initial module path to allow
226 	 * the target constructor to load a support module.  We base kmdb's
227 	 * module path off the kernel's module path unless the user has
228 	 * explicitly supplied one.
229 	 */
230 	mdb_set_ipath(KMDB_DEF_IPATH);
231 	if (strlen(mdb.m_lpathstr) > 0) {
232 		mdb_set_lpath(mdb.m_lpathstr);
233 	} else {
234 		char *lpath;
235 
236 		if (kav->kav_modpath != NULL && *kav->kav_modpath != '\0' &&
237 		    (lpath = kmdb_modpath2lpath(kav->kav_modpath)) != NULL) {
238 			mdb_set_lpath(lpath);
239 			strfree(lpath);
240 		} else {
241 			mdb_set_lpath(KMDB_DEF_LPATH);
242 		}
243 	}
244 
245 	if (mdb_get_prompt() == NULL)
246 		(void) mdb_set_prompt(MDB_DEF_PROMPT);
247 
248 	tgt = mdb_tgt_create(tgt_ctor, mdb.m_tgtflags, 0, NULL);
249 
250 	if (tgt == NULL) {
251 		warn("failed to initialize target");
252 		return (-1);
253 	}
254 
255 	mdb_tgt_activate(tgt);
256 
257 	mdb_create_loadable_disasms();
258 
259 	/*
260 	 * Path evaluation part 2: Re-evaluate the path now that the target
261 	 * is ready (and thus we have access to the real platform string).
262 	 */
263 	mdb_set_ipath(mdb.m_ipathstr);
264 	mdb_set_lpath(mdb.m_lpathstr);
265 
266 	if (!(mdb.m_flags & MDB_FL_NOMODS))
267 		mdb_module_load_all(MDB_MOD_DEFER);
268 
269 	/* Allocate the main debugger stack */
270 	kmdb_main_stack = mdb_alloc(KMDB_STACK_SIZE, UM_SLEEP);
271 	kmdb_main_stack_size = KMDB_STACK_SIZE;
272 
273 	kmdb_kdi_end_init();
274 
275 	return (0);
276 }
277 
278 #ifdef sun4v
279 
280 void
281 kmdb_init_promif(char *pgmname, kmdb_auxv_t *kav)
282 {
283 	kmdb_prom_init_promif(pgmname, kav);
284 }
285 
286 #else
287 
288 /*ARGSUSED*/
289 void
290 kmdb_init_promif(char *pgmname, kmdb_auxv_t *kav)
291 {
292 	/*
293 	 * Fake function for non sun4v. See comments in kmdb_ctl.h
294 	 */
295 	ASSERT(0);
296 }
297 
298 #endif
299 
300 /*
301  * First-time kmdb startup.  Run when kmdb has control of the machine for the
302  * first time.
303  */
304 static void
305 kmdb_startup(void)
306 {
307 	mdb_io_t *inio, *outio;
308 
309 	if (mdb.m_termtype == NULL) {
310 		/*
311 		 * The terminal type wasn't specified, so we guess.  If we're
312 		 * on console, we'll get a terminal type from the PROM.  If not,
313 		 * we'll use the default.
314 		 */
315 		const char *ttype;
316 
317 		if ((ttype = kmdb_prom_term_type()) == NULL) {
318 			ttype = KMDB_DEF_TERM_TYPE;
319 			warn("unable to determine terminal type: "
320 			    "assuming `%s'\n", ttype);
321 		}
322 
323 		mdb.m_flags |= MDB_FL_TERMGUESS;
324 		mdb.m_termtype = strdup(ttype);
325 
326 	} else if (mdb.m_flags & MDB_FL_TERMGUESS) {
327 		/*
328 		 * The terminal type wasn't specified by the user, but a guess
329 		 * was made using either $TERM or a property from the SMF.  A
330 		 * terminal type from the PROM code overrides the guess, so
331 		 * we'll use that if we can.
332 		 */
333 		char *promttype;
334 
335 		if ((promttype = kmdb_prom_term_type()) != NULL) {
336 			strfree(mdb.m_termtype);
337 			mdb.m_termtype = strdup(promttype);
338 		}
339 	}
340 
341 	inio = kmdb_promio_create("stdin");
342 	outio = kmdb_promio_create("stdout");
343 
344 	if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, inio, outio)) ==
345 	    NULL && strcmp(mdb.m_termtype, KMDB_DEF_TERM_TYPE) != 0) {
346 		warn("failed to set terminal type to `%s', using `"
347 		    KMDB_DEF_TERM_TYPE "'\n", mdb.m_termtype);
348 
349 		strfree(mdb.m_termtype);
350 		mdb.m_termtype = strdup(KMDB_DEF_TERM_TYPE);
351 
352 		if ((mdb.m_term = mdb_termio_create(mdb.m_termtype, inio,
353 		    outio)) == NULL) {
354 			fail("failed to set terminal type to `"
355 			    KMDB_DEF_TERM_TYPE "'\n");
356 		}
357 	}
358 
359 	mdb_iob_destroy(mdb.m_in);
360 	mdb.m_in = mdb_iob_create(mdb.m_term, MDB_IOB_RDONLY);
361 	mdb_iob_setpager(mdb.m_out, mdb.m_term);
362 	mdb_iob_setflags(mdb.m_out, MDB_IOB_PGENABLE);
363 
364 	kmdb_kvm_startup();
365 
366 	/*
367 	 * kmdb_init() and kctl_activate() may have been talking to each other,
368 	 * and may have left some messages for us.  The driver -> debugger
369 	 * queue is normally processed during the resume path, so we have to
370 	 * do it manually here if we want it to be run for first startup.
371 	 */
372 	kmdb_dpi_process_work_queue();
373 
374 	kmdb_kvm_poststartup();
375 }
376 
377 void
378 kmdb_main(void)
379 {
380 	int status;
381 
382 	kmdb_dpi_set_state(DPI_STATE_STOPPED, 0);
383 	mdb_printf("\nWelcome to kmdb\n");
384 	kmdb_startup();
385 
386 	/*
387 	 * Debugger termination is a bit tricky.  For compatibility with kadb,
388 	 * neither an EOF on stdin nor a normal ::quit will cause the debugger
389 	 * to unload.  In both cases, they get a trip to OBP, after which the
390 	 * debugger returns.
391 	 *
392 	 * The only supported way to cause the debugger to unload is to specify
393 	 * the unload flag to ::quit, or to have the driver request it.  The
394 	 * driver request is the primary exit mechanism - the ::quit flag is
395 	 * provided for convenience.
396 	 *
397 	 * Both forms of "exit" (unqualified ::quit that won't cause an unload,
398 	 * and a driver request that will) are signalled by an MDB_ERR_QUIT.  In
399 	 * the latter case, however, the KDI will have the unload request.
400 	 */
401 	for (;;) {
402 		status = mdb_run();
403 
404 		if (status == MDB_ERR_QUIT && kmdb_kdi_get_unload_request()) {
405 			break;
406 
407 		} else if (status == MDB_ERR_QUIT || status == 0) {
408 			kmdb_dpi_enter_mon();
409 
410 		} else if (status == MDB_ERR_OUTPUT) {
411 			/*
412 			 * If a write failed on stdout, give up.  A more
413 			 * informative error message will already have been
414 			 * printed by mdb_run().
415 			 */
416 			if (mdb_iob_getflags(mdb.m_out) & MDB_IOB_ERR)
417 				fail("write to stdout failed, exiting\n");
418 
419 		} else if (status != MDB_ERR_ABORT) {
420 			fail("debugger exited abnormally (status = %s)\n",
421 			    mdb_err2str(status));
422 		}
423 	}
424 
425 	mdb_destroy();
426 
427 	kmdb_dpi_resume_unload();
428 
429 	/*NOTREACHED*/
430 }
431