17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
58a6a72faf * Common Development and Distribution License (the "License").
68a6a72faf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
228014889Surya Prakki * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate/*
263b6e0a5Matt Amdur * Copyright (c) 2012 by Delphix. All rights reserved.
27d75f374John Levon * Copyright (c) 2018, Joyent, Inc.
283b6e0a5Matt Amdur */
293b6e0a5Matt Amdur
303b6e0a5Matt Amdur/*
317c478bdstevel@tonic-gate * Modular Debugger (MDB)
327c478bdstevel@tonic-gate *
337c478bdstevel@tonic-gate * Refer to the white paper "A Modular Debugger for Solaris" for information
347c478bdstevel@tonic-gate * on the design, features, and goals of MDB.  See /shared/sac/PSARC/1999/169
357c478bdstevel@tonic-gate * for copies of the paper and related documentation.
367c478bdstevel@tonic-gate *
377c478bdstevel@tonic-gate * This file provides the basic construction and destruction of the debugger's
387c478bdstevel@tonic-gate * global state, as well as the main execution loop, mdb_run().  MDB maintains
397c478bdstevel@tonic-gate * a stack of execution frames (mdb_frame_t's) that keep track of its current
407c478bdstevel@tonic-gate * state, including a stack of input and output buffers, walk and memory
417c478bdstevel@tonic-gate * garbage collect lists, and a list of commands (mdb_cmd_t's).  As the
427c478bdstevel@tonic-gate * parser consumes input, it fills in a list of commands to execute, and then
437c478bdstevel@tonic-gate * invokes mdb_call(), below.  A command consists of a dcmd, telling us
447c478bdstevel@tonic-gate * what function to execute, and a list of arguments and other invocation-
457c478bdstevel@tonic-gate * specific data.  Each frame may have more than one command, kept on a list,
467c478bdstevel@tonic-gate * when multiple commands are separated by | operators.  New frames may be
477c478bdstevel@tonic-gate * stacked on old ones by nested calls to mdb_run: this occurs when, for
487c478bdstevel@tonic-gate * example, in the middle of processing one input source (such as a file
497c478bdstevel@tonic-gate * or the terminal), we invoke a dcmd that in turn calls mdb_eval().  mdb_eval
507c478bdstevel@tonic-gate * will construct a new frame whose input source is the string passed to
517c478bdstevel@tonic-gate * the eval function, and then execute this frame to completion.
527c478bdstevel@tonic-gate */
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gate#include <sys/param.h>
557c478bdstevel@tonic-gate#include <stropts.h>
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate#define	_MDB_PRIVATE
587c478bdstevel@tonic-gate#include <mdb/mdb.h>
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate#include <mdb/mdb_context.h>
617c478bdstevel@tonic-gate#include <mdb/mdb_argvec.h>
627c478bdstevel@tonic-gate#include <mdb/mdb_signal.h>
637c478bdstevel@tonic-gate#include <mdb/mdb_macalias.h>
647c478bdstevel@tonic-gate#include <mdb/mdb_module.h>
657c478bdstevel@tonic-gate#include <mdb/mdb_modapi.h>
667c478bdstevel@tonic-gate#include <mdb/mdb_string.h>
677c478bdstevel@tonic-gate#include <mdb/mdb_callb.h>
687c478bdstevel@tonic-gate#include <mdb/mdb_debug.h>
697c478bdstevel@tonic-gate#include <mdb/mdb_frame.h>
707c478bdstevel@tonic-gate#include <mdb/mdb_conf.h>
717c478bdstevel@tonic-gate#include <mdb/mdb_err.h>
727c478bdstevel@tonic-gate#include <mdb/mdb_lex.h>
737c478bdstevel@tonic-gate#include <mdb/mdb_io.h>
740a47c91Robert Mustacchi#include <mdb/mdb_ctf.h>
757c478bdstevel@tonic-gate#ifdef _KMDB
767c478bdstevel@tonic-gate#include <kmdb/kmdb_module.h>
777c478bdstevel@tonic-gate#endif
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate/*
807c478bdstevel@tonic-gate * Macro for testing if a dcmd's return status (x) indicates that we should
817c478bdstevel@tonic-gate * abort the current loop or pipeline.
827c478bdstevel@tonic-gate */
837c478bdstevel@tonic-gate#define	DCMD_ABORTED(x)	((x) == DCMD_USAGE || (x) == DCMD_ABORT)
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gateextern const mdb_dcmd_t mdb_dcmd_builtins[];
867c478bdstevel@tonic-gateextern mdb_dis_ctor_f *const mdb_dis_builtins[];
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate/*
897c478bdstevel@tonic-gate * Variable discipline for toggling MDB_FL_PSYM based on the value of the
907c478bdstevel@tonic-gate * undocumented '_' variable.  Once adb(1) has been removed from the system,
917c478bdstevel@tonic-gate * we should just remove this functionality and always disable PSYM for macros.
927c478bdstevel@tonic-gate */
937c478bdstevel@tonic-gatestatic uintmax_t
947c478bdstevel@tonic-gatepsym_disc_get(const mdb_var_t *v)
957c478bdstevel@tonic-gate{
967c478bdstevel@tonic-gate	int i = (mdb.m_flags & MDB_FL_PSYM) ? 1 : 0;
977c478bdstevel@tonic-gate	int j = (MDB_NV_VALUE(v) != 0) ? 1 : 0;
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate	if ((i ^ j) == 0)
1007c478bdstevel@tonic-gate		MDB_NV_VALUE((mdb_var_t *)v) = j ^ 1;
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate	return (MDB_NV_VALUE(v));
1037c478bdstevel@tonic-gate}
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gatestatic void
1067c478bdstevel@tonic-gatepsym_disc_set(mdb_var_t *v, uintmax_t value)
1077c478bdstevel@tonic-gate{
1087c478bdstevel@tonic-gate	if (value == 0)
1097c478bdstevel@tonic-gate		mdb.m_flags |= MDB_FL_PSYM;
1107c478bdstevel@tonic-gate	else
1117c478bdstevel@tonic-gate		mdb.m_flags &= ~MDB_FL_PSYM;
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gate	MDB_NV_VALUE(v) = value;
1147c478bdstevel@tonic-gate}
1157c478bdstevel@tonic-gate
1167c478bdstevel@tonic-gate/*
1177c478bdstevel@tonic-gate * Variable discipline for making <1 (most recent offset) behave properly.
1187c478bdstevel@tonic-gate */
1197c478bdstevel@tonic-gatestatic uintmax_t
1207c478bdstevel@tonic-gateroff_disc_get(const mdb_var_t *v)
1217c478bdstevel@tonic-gate{
1227c478bdstevel@tonic-gate	return (MDB_NV_VALUE(v));
1237c478bdstevel@tonic-gate}
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gatestatic void
1267c478bdstevel@tonic-gateroff_disc_set(mdb_var_t *v, uintmax_t value)
1277c478bdstevel@tonic-gate{
1287c478bdstevel@tonic-gate	mdb_nv_set_value(mdb.m_proffset, MDB_NV_VALUE(v));
1297c478bdstevel@tonic-gate	MDB_NV_VALUE(v) = value;
1307c478bdstevel@tonic-gate}
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate/*
1337c478bdstevel@tonic-gate * Variable discipline for exporting the representative thread.
1347c478bdstevel@tonic-gate */
1357c478bdstevel@tonic-gatestatic uintmax_t
1367c478bdstevel@tonic-gatethr_disc_get(const mdb_var_t *v)
1377c478bdstevel@tonic-gate{
1387c478bdstevel@tonic-gate	mdb_tgt_status_t s;
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gate	if (mdb.m_target != NULL && mdb_tgt_status(mdb.m_target, &s) == 0)
1417c478bdstevel@tonic-gate		return (s.st_tid);
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gate	return (MDB_NV_VALUE(v));
1447c478bdstevel@tonic-gate}
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gateconst char **
1477c478bdstevel@tonic-gatemdb_path_alloc(const char *s, size_t *newlen)
1487c478bdstevel@tonic-gate{
1497c478bdstevel@tonic-gate	char *format = mdb_alloc(strlen(s) * 2 + 1, UM_NOSLEEP);
1507c478bdstevel@tonic-gate	const char **path;
1517c478bdstevel@tonic-gate	char *p, *q;
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	struct utsname uts;
1547c478bdstevel@tonic-gate	size_t len;
1557c478bdstevel@tonic-gate	int i;
1567c478bdstevel@tonic-gate
1577c478bdstevel@tonic-gate	mdb_arg_t arg_i, arg_m, arg_p, arg_r, arg_t, arg_R, arg_V;
1587c478bdstevel@tonic-gate	mdb_argvec_t argv;
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate	static const char *empty_path[] = { NULL };
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gate	if (format == NULL)
1637c478bdstevel@tonic-gate		goto nomem;
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate	while (*s == ':')
1667c478bdstevel@tonic-gate		s++; /* strip leading delimiters */
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate	if (*s == '\0') {
1697c478bdstevel@tonic-gate		*newlen = 0;
1707c478bdstevel@tonic-gate		return (empty_path);
1717c478bdstevel@tonic-gate	}
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate	(void) strcpy(format, s);
1747c478bdstevel@tonic-gate	mdb_argvec_create(&argv);
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gate	/*
1777c478bdstevel@tonic-gate	 * %i embedded in path string expands to ISA.
1787c478bdstevel@tonic-gate	 */
1797c478bdstevel@tonic-gate	arg_i.a_type = MDB_TYPE_STRING;
1807c478bdstevel@tonic-gate	if (mdb.m_target != NULL)
1817c478bdstevel@tonic-gate		arg_i.a_un.a_str = mdb_tgt_isa(mdb.m_target);
1827c478bdstevel@tonic-gate	else
1837c478bdstevel@tonic-gate		arg_i.a_un.a_str = mdb_conf_isa();
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate	/*
1867c478bdstevel@tonic-gate	 * %p embedded in path string expands to the platform name.
1877c478bdstevel@tonic-gate	 */
1887c478bdstevel@tonic-gate	arg_p.a_type = MDB_TYPE_STRING;
1897c478bdstevel@tonic-gate	if (mdb.m_target != NULL)
1907c478bdstevel@tonic-gate		arg_p.a_un.a_str = mdb_tgt_platform(mdb.m_target);
1917c478bdstevel@tonic-gate	else
1927c478bdstevel@tonic-gate		arg_p.a_un.a_str = mdb_conf_platform();
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate	/*
1957c478bdstevel@tonic-gate	 * %r embedded in path string expands to root directory, or
1967c478bdstevel@tonic-gate	 * to the empty string if root is "/" (to avoid // in paths).
1977c478bdstevel@tonic-gate	 */
1987c478bdstevel@tonic-gate	arg_r.a_type = MDB_TYPE_STRING;
1997c478bdstevel@tonic-gate	arg_r.a_un.a_str = strcmp(mdb.m_root, "/") ? mdb.m_root : "";
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate	/*
202843e198johnlev	 * %t embedded in path string expands to the target name, defaulting to
203843e198johnlev	 * kvm; this is so we can find mdb_kb, which is used during bootstrap.
2047c478bdstevel@tonic-gate	 */
2057c478bdstevel@tonic-gate	arg_t.a_type = MDB_TYPE_STRING;
206843e198johnlev	arg_t.a_un.a_str = mdb.m_target ? mdb_tgt_name(mdb.m_target) : "kvm";
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate	/*
2097c478bdstevel@tonic-gate	 * %R and %V expand to uname -r (release) and uname -v (version).
2107c478bdstevel@tonic-gate	 */
2117c478bdstevel@tonic-gate	if (mdb.m_target == NULL || mdb_tgt_uname(mdb.m_target, &uts) < 0)
2127c478bdstevel@tonic-gate		mdb_conf_uname(&uts);
2137c478bdstevel@tonic-gate
2147c478bdstevel@tonic-gate	arg_m.a_type = MDB_TYPE_STRING;
2157c478bdstevel@tonic-gate	arg_m.a_un.a_str = uts.machine;
2167c478bdstevel@tonic-gate
2177c478bdstevel@tonic-gate	arg_R.a_type = MDB_TYPE_STRING;
2187c478bdstevel@tonic-gate	arg_R.a_un.a_str = uts.release;
2197c478bdstevel@tonic-gate
2207c478bdstevel@tonic-gate	arg_V.a_type = MDB_TYPE_STRING;
2217c478bdstevel@tonic-gate	if (mdb.m_flags & MDB_FL_LATEST)
2227c478bdstevel@tonic-gate		arg_V.a_un.a_str = "latest";
2237c478bdstevel@tonic-gate	else
2247c478bdstevel@tonic-gate		arg_V.a_un.a_str = uts.version;
2257c478bdstevel@tonic-gate
2267c478bdstevel@tonic-gate	/*
2277c478bdstevel@tonic-gate	 * In order to expand the buffer, we examine the format string for
2287c478bdstevel@tonic-gate	 * our % tokens and construct an argvec, replacing each % token
2297c478bdstevel@tonic-gate	 * with %s along the way.  If we encounter an unknown token, we
2307c478bdstevel@tonic-gate	 * shift over the remaining format buffer and stick in %%.
2317c478bdstevel@tonic-gate	 */
2327c478bdstevel@tonic-gate	for (q = format; (q = strchr(q, '%')) != NULL; q++) {
2337c478bdstevel@tonic-gate		switch (q[1]) {
2347c478bdstevel@tonic-gate		case 'i':
2357c478bdstevel@tonic-gate			mdb_argvec_append(&argv, &arg_i);
2367c478bdstevel@tonic-gate			*++q = 's';
2377c478bdstevel@tonic-gate			break;
2387c478bdstevel@tonic-gate		case 'm':
2397c478bdstevel@tonic-gate			mdb_argvec_append(&argv, &arg_m);
2407c478bdstevel@tonic-gate			*++q = 's';
2417c478bdstevel@tonic-gate			break;
2427c478bdstevel@tonic-gate		case 'p':
2437c478bdstevel@tonic-gate			mdb_argvec_append(&argv, &arg_p);
2447c478bdstevel@tonic-gate			*++q = 's';
2457c478bdstevel@tonic-gate			break;
2467c478bdstevel@tonic-gate		case 'r':
2477c478bdstevel@tonic-gate			mdb_argvec_append(&argv, &arg_r);
2487c478bdstevel@tonic-gate			*++q = 's';
2497c478bdstevel@tonic-gate			break;
2507c478bdstevel@tonic-gate		case 't':
2517c478bdstevel@tonic-gate			mdb_argvec_append(&argv, &arg_t);
2527c478bdstevel@tonic-gate			*++q = 's';
2537c478bdstevel@tonic-gate			break;
2547c478bdstevel@tonic-gate		case 'R':
2557c478bdstevel@tonic-gate			mdb_argvec_append(&argv, &arg_R);
2567c478bdstevel@tonic-gate			*++q = 's';
2577c478bdstevel@tonic-gate			break;
2587c478bdstevel@tonic-gate		case 'V':
2597c478bdstevel@tonic-gate			mdb_argvec_append(&argv, &arg_V);
2607c478bdstevel@tonic-gate			*++q = 's';
2617c478bdstevel@tonic-gate			break;
2627c478bdstevel@tonic-gate		default:
2637c478bdstevel@tonic-gate			bcopy(q + 1, q + 2, strlen(q));
2647c478bdstevel@tonic-gate			*++q = '%';
2657c478bdstevel@tonic-gate		}
2667c478bdstevel@tonic-gate	}
2677c478bdstevel@tonic-gate
2687c478bdstevel@tonic-gate	/*
2697c478bdstevel@tonic-gate	 * We're now ready to use our printf engine to format the final string.
2707c478bdstevel@tonic-gate	 * Take one lap with a NULL buffer to determine how long the final
2717c478bdstevel@tonic-gate	 * string will be, allocate it, and format it.
2727c478bdstevel@tonic-gate	 */
2737c478bdstevel@tonic-gate	len = mdb_iob_asnprintf(NULL, 0, format, argv.a_data);
2747c478bdstevel@tonic-gate	if ((p = mdb_alloc(len + 1, UM_NOSLEEP)) != NULL)
2757c478bdstevel@tonic-gate		(void) mdb_iob_asnprintf(p, len + 1, format, argv.a_data);
2767c478bdstevel@tonic-gate	else
2777c478bdstevel@tonic-gate		goto nomem;
2787c478bdstevel@tonic-gate
2797c478bdstevel@tonic-gate	mdb_argvec_zero(&argv);
2807c478bdstevel@tonic-gate	mdb_argvec_destroy(&argv);
2817c478bdstevel@tonic-gate
2827c478bdstevel@tonic-gate	mdb_free(format, strlen(s) * 2 + 1);
2837c478bdstevel@tonic-gate	format = NULL;
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate	/*
2867c478bdstevel@tonic-gate	 * Compress the string to exclude any leading delimiters.
2877c478bdstevel@tonic-gate	 */
2887c478bdstevel@tonic-gate	for (q = p; *q == ':'; q++)
2897c478bdstevel@tonic-gate		continue;
2907c478bdstevel@tonic-gate	if (q != p)
2917c478bdstevel@tonic-gate		bcopy(q, p, strlen(q) + 1);
2927c478bdstevel@tonic-gate
2937c478bdstevel@tonic-gate	/*
2947c478bdstevel@tonic-gate	 * Count up the number of delimited elements.  A sequence of
2957c478bdstevel@tonic-gate	 * consecutive delimiters is only counted once.
2967c478bdstevel@tonic-gate	 */
2977c478bdstevel@tonic-gate	for (i = 1, q = p; (q = strchr(q, ':')) != NULL; i++) {
2987c478bdstevel@tonic-gate		while (*q == ':')
2997c478bdstevel@tonic-gate			q++;
3007c478bdstevel@tonic-gate	}
3017c478bdstevel@tonic-gate
3027c478bdstevel@tonic-gate	if ((path = mdb_alloc(sizeof (char *) * (i + 1), UM_NOSLEEP)) == NULL) {
3037c478bdstevel@tonic-gate		mdb_free(p, len + 1);
3047c478bdstevel@tonic-gate		goto nomem;
3057c478bdstevel@tonic-gate	}
3067c478bdstevel@tonic-gate
3077c478bdstevel@tonic-gate	for (i = 0, q = strtok(p, ":"); q != NULL; q = strtok(NULL, ":"))
3087c478bdstevel@tonic-gate		path[i++] = q;
3097c478bdstevel@tonic-gate
3107c478bdstevel@tonic-gate	path[i] = NULL;
3117c478bdstevel@tonic-gate	*newlen = len + 1;
3127c478bdstevel@tonic-gate	return (path);
3137c478bdstevel@tonic-gate
3147c478bdstevel@tonic-gatenomem:
3157c478bdstevel@tonic-gate	warn("failed to allocate memory for path");
3167c478bdstevel@tonic-gate	if (format != NULL)
3177c478bdstevel@tonic-gate		mdb_free(format, strlen(s) * 2 + 1);
3187c478bdstevel@tonic-gate	*newlen = 0;
3197c478bdstevel@tonic-gate	return (empty_path);
3207c478bdstevel@tonic-gate}
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gateconst char **
3237c478bdstevel@tonic-gatemdb_path_dup(const char *path[], size_t pathlen, size_t *npathlenp)
3247c478bdstevel@tonic-gate{
3257c478bdstevel@tonic-gate	char **npath;
3267c478bdstevel@tonic-gate	int i, j;
3277c478bdstevel@tonic-gate
3287c478bdstevel@tonic-gate	for (i = 0; path[i] != NULL; i++)
3297c478bdstevel@tonic-gate		continue; /* count the path elements */
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate	npath = mdb_zalloc(sizeof (char *) * (i + 1), UM_SLEEP);
3327c478bdstevel@tonic-gate	if (pathlen > 0) {
3337c478bdstevel@tonic-gate		npath[0] = mdb_alloc(pathlen, UM_SLEEP);
3347c478bdstevel@tonic-gate		bcopy(path[0], npath[0], pathlen);
3357c478bdstevel@tonic-gate	}
3367c478bdstevel@tonic-gate
3377c478bdstevel@tonic-gate	for (j = 1; j < i; j++)
3387c478bdstevel@tonic-gate		npath[j] = npath[0] + (path[j] - path[0]);
3397c478bdstevel@tonic-gate	npath[i] = NULL;
3407c478bdstevel@tonic-gate
3417c478bdstevel@tonic-gate	*npathlenp = pathlen;
3427c478bdstevel@tonic-gate	return ((const char **)npath);
3437c478bdstevel@tonic-gate}
3447c478bdstevel@tonic-gate
3457c478bdstevel@tonic-gatevoid
3467c478bdstevel@tonic-gatemdb_path_free(const char *path[], size_t pathlen)
3477c478bdstevel@tonic-gate{
3487c478bdstevel@tonic-gate	int i;
3497c478bdstevel@tonic-gate
3507c478bdstevel@tonic-gate	for (i = 0; path[i] != NULL; i++)
3517c478bdstevel@tonic-gate		continue; /* count the path elements */
3527c478bdstevel@tonic-gate
3537c478bdstevel@tonic-gate	if (i > 0) {
3547c478bdstevel@tonic-gate		mdb_free((void *)path[0], pathlen);
3557c478bdstevel@tonic-gate		mdb_free(path, sizeof (char *) * (i + 1));
3567c478bdstevel@tonic-gate	}
3577c478bdstevel@tonic-gate}
3587c478bdstevel@tonic-gate
3597c478bdstevel@tonic-gate/*
3607c478bdstevel@tonic-gate * Convert path string "s" to canonical form, expanding any %o tokens that are
3617c478bdstevel@tonic-gate * found within the path.  The old path string is specified by "path", a buffer
3627c478bdstevel@tonic-gate * of size MAXPATHLEN which is then overwritten with the new path string.
3637c478bdstevel@tonic-gate */
3647c478bdstevel@tonic-gatestatic const char *
3657c478bdstevel@tonic-gatepath_canon(char *path, const char *s)
3667c478bdstevel@tonic-gate{
3677c478bdstevel@tonic-gate	char *p = path;
3687c478bdstevel@tonic-gate	char *q = p + MAXPATHLEN - 1;
3697c478bdstevel@tonic-gate
3707c478bdstevel@tonic-gate	char old[MAXPATHLEN];
3717c478bdstevel@tonic-gate	char c;
3727c478bdstevel@tonic-gate
3737c478bdstevel@tonic-gate	(void) strcpy(old, p);
3747c478bdstevel@tonic-gate	*q = '\0';
3757c478bdstevel@tonic-gate
3767c478bdstevel@tonic-gate	while (p < q && (c = *s++) != '\0') {
3777c478bdstevel@tonic-gate		if (c == '%') {
3787c478bdstevel@tonic-gate			if ((c = *s++) == 'o') {
3797c478bdstevel@tonic-gate				(void) strncpy(p, old, (size_t)(q - p));
3807c478bdstevel@tonic-gate				p += strlen(p);
3817c478bdstevel@tonic-gate			} else {
3827c478bdstevel@tonic-gate				*p++ = '%';
3837c478bdstevel@tonic-gate				if (p < q && c != '\0')
3847c478bdstevel@tonic-gate					*p++ = c;
3857c478bdstevel@tonic-gate				else
3867c478bdstevel@tonic-gate					break;
3877c478bdstevel@tonic-gate			}
3887c478bdstevel@tonic-gate		} else
3897c478bdstevel@tonic-gate			*p++ = c;
3907c478bdstevel@tonic-gate	}
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate	*p = '\0';
3937c478bdstevel@tonic-gate	return (path);
3947c478bdstevel@tonic-gate}
3957c478bdstevel@tonic-gate
3967c478bdstevel@tonic-gatevoid
3977c478bdstevel@tonic-gatemdb_set_ipath(const char *path)
3987c478bdstevel@tonic-gate{
3997c478bdstevel@tonic-gate	if (mdb.m_ipath != NULL)
4007c478bdstevel@tonic-gate		mdb_path_free(mdb.m_ipath, mdb.m_ipathlen);
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gate	path = path_canon(mdb.m_ipathstr, path);
4037c478bdstevel@tonic-gate	mdb.m_ipath = mdb_path_alloc(path, &mdb.m_ipathlen);
4047c478bdstevel@tonic-gate}
4057c478bdstevel@tonic-gate
4067c478bdstevel@tonic-gatevoid
4077c478bdstevel@tonic-gatemdb_set_lpath(const char *path)
4087c478bdstevel@tonic-gate{
4097c478bdstevel@tonic-gate	if (mdb.m_lpath != NULL)
4107c478bdstevel@tonic-gate		mdb_path_free(mdb.m_lpath, mdb.m_lpathlen);
4117c478bdstevel@tonic-gate
4127c478bdstevel@tonic-gate	path = path_canon(mdb.m_lpathstr, path);
4137c478bdstevel@tonic-gate	mdb.m_lpath = mdb_path_alloc(path, &mdb.m_lpathlen);
4147c478bdstevel@tonic-gate
4157c478bdstevel@tonic-gate#ifdef _KMDB
4167c478bdstevel@tonic-gate	kmdb_module_path_set(mdb.m_lpath, mdb.m_lpathlen);
4177c478bdstevel@tonic-gate#endif
4187c478bdstevel@tonic-gate}
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gatestatic void
4217c478bdstevel@tonic-gateprompt_update(void)
4227c478bdstevel@tonic-gate{
4237c478bdstevel@tonic-gate	(void) mdb_snprintf(mdb.m_prompt, sizeof (mdb.m_prompt),
4247c478bdstevel@tonic-gate	    mdb.m_promptraw);
4257c478bdstevel@tonic-gate	mdb.m_promptlen = strlen(mdb.m_prompt);
4267c478bdstevel@tonic-gate}
4277c478bdstevel@tonic-gate
4287c478bdstevel@tonic-gateconst char *
4297c478bdstevel@tonic-gatemdb_get_prompt(void)
4307c478bdstevel@tonic-gate{
4317c478bdstevel@tonic-gate	if (mdb.m_promptlen == 0)
4327c478bdstevel@tonic-gate		return (NULL);
4337c478bdstevel@tonic-gate	else
4347c478bdstevel@tonic-gate		return (mdb.m_prompt);
4357c478bdstevel@tonic-gate}
4367c478bdstevel@tonic-gate
4377c478bdstevel@tonic-gateint
4387c478bdstevel@tonic-gatemdb_set_prompt(const char *p)
4397c478bdstevel@tonic-gate{
4407c478bdstevel@tonic-gate	size_t len = strlen(p);
4417c478bdstevel@tonic-gate
4427c478bdstevel@tonic-gate	if (len > MDB_PROMPTLEN) {
4437c478bdstevel@tonic-gate		warn("prompt may not exceed %d characters\n", MDB_PROMPTLEN);
4447c478bdstevel@tonic-gate		return (0);
4457c478bdstevel@tonic-gate	}
4467c478bdstevel@tonic-gate
4477c478bdstevel@tonic-gate	(void) strcpy(mdb.m_promptraw, p);
4487c478bdstevel@tonic-gate	prompt_update();
4497c478bdstevel@tonic-gate	return (1);
4507c478bdstevel@tonic-gate}
4517c478bdstevel@tonic-gate
4527c478bdstevel@tonic-gatestatic mdb_frame_t frame0;
4537c478bdstevel@tonic-gate
4547c478bdstevel@tonic-gatevoid
4557c478bdstevel@tonic-gatemdb_create(const char *execname, const char *arg0)
4567c478bdstevel@tonic-gate{
4577c478bdstevel@tonic-gate	static const mdb_nv_disc_t psym_disc = { psym_disc_set, psym_disc_get };
4587c478bdstevel@tonic-gate	static const mdb_nv_disc_t roff_disc = { roff_disc_set, roff_disc_get };
4597c478bdstevel@tonic-gate	static const mdb_nv_disc_t thr_disc = { NULL, thr_disc_get };
4607c478bdstevel@tonic-gate
4617c478bdstevel@tonic-gate	static char rootdir[MAXPATHLEN];
4627c478bdstevel@tonic-gate
4637c478bdstevel@tonic-gate	const mdb_dcmd_t *dcp;
4647c478bdstevel@tonic-gate	int i;
4657c478bdstevel@tonic-gate
4667c478bdstevel@tonic-gate	bzero(&mdb, sizeof (mdb_t));
4677c478bdstevel@tonic-gate
4687c478bdstevel@tonic-gate	mdb.m_flags = MDB_FL_PSYM | MDB_FL_PAGER | MDB_FL_BPTNOSYMSTOP |
4697c478bdstevel@tonic-gate	    MDB_FL_READBACK;
4707c478bdstevel@tonic-gate	mdb.m_radix = MDB_DEF_RADIX;
4717c478bdstevel@tonic-gate	mdb.m_nargs = MDB_DEF_NARGS;
4727c478bdstevel@tonic-gate	mdb.m_histlen = MDB_DEF_HISTLEN;
4737c478bdstevel@tonic-gate	mdb.m_armemlim = MDB_DEF_ARRMEM;
4747c478bdstevel@tonic-gate	mdb.m_arstrlim = MDB_DEF_ARRSTR;
4757c478bdstevel@tonic-gate
4767c478bdstevel@tonic-gate	mdb.m_pname = strbasename(arg0);
4777c478bdstevel@tonic-gate	if (strcmp(mdb.m_pname, "adb") == 0) {
4787c478bdstevel@tonic-gate		mdb.m_flags |= MDB_FL_NOMODS | MDB_FL_ADB | MDB_FL_REPLAST;
4797c478bdstevel@tonic-gate		mdb.m_flags &= ~MDB_FL_PAGER;
4807c478bdstevel@tonic-gate	}
4817c478bdstevel@tonic-gate
4827c478bdstevel@tonic-gate	mdb.m_ipathstr = mdb_zalloc(MAXPATHLEN, UM_SLEEP);
4837c478bdstevel@tonic-gate	mdb.m_lpathstr = mdb_zalloc(MAXPATHLEN, UM_SLEEP);
4847c478bdstevel@tonic-gate
4857c478bdstevel@tonic-gate	(void) strncpy(rootdir, execname, sizeof (rootdir));
4867c478bdstevel@tonic-gate	rootdir[sizeof (rootdir) - 1] = '\0';
4877c478bdstevel@tonic-gate	(void) strdirname(rootdir);
4887c478bdstevel@tonic-gate
4897c478bdstevel@tonic-gate	if (strcmp(strbasename(rootdir), "sparcv9") == 0 ||
4907c478bdstevel@tonic-gate	    strcmp(strbasename(rootdir), "sparcv7") == 0 ||
4917c478bdstevel@tonic-gate	    strcmp(strbasename(rootdir), "amd64") == 0 ||
4927c478bdstevel@tonic-gate	    strcmp(strbasename(rootdir), "i86") == 0)
4937c478bdstevel@tonic-gate		(void) strdirname(rootdir);
4947c478bdstevel@tonic-gate
4957c478bdstevel@tonic-gate	if (strcmp(strbasename(rootdir), "bin") == 0) {
4967c478bdstevel@tonic-gate		(void) strdirname(rootdir);
4977c478bdstevel@tonic-gate		if (strcmp(strbasename(rootdir), "usr") == 0)
4987c478bdstevel@tonic-gate			(void) strdirname(rootdir);
4997c478bdstevel@tonic-gate	} else
5007c478bdstevel@tonic-gate		(void) strcpy(rootdir, "/");
5017c478bdstevel@tonic-gate
5027c478bdstevel@tonic-gate	mdb.m_root = rootdir;
5037c478bdstevel@tonic-gate
5047c478bdstevel@tonic-gate	mdb.m_rminfo.mi_dvers = MDB_API_VERSION;
5057c478bdstevel@tonic-gate	mdb.m_rminfo.mi_dcmds = mdb_dcmd_builtins;
5067c478bdstevel@tonic-gate	mdb.m_rminfo.mi_walkers = NULL;
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate	(void) mdb_nv_create(&mdb.m_rmod.mod_walkers, UM_SLEEP);
5097c478bdstevel@tonic-gate	(void) mdb_nv_create(&mdb.m_rmod.mod_dcmds, UM_SLEEP);
5107c478bdstevel@tonic-gate
5117c478bdstevel@tonic-gate	mdb.m_rmod.mod_name = mdb.m_pname;
5127c478bdstevel@tonic-gate	mdb.m_rmod.mod_info = &mdb.m_rminfo;
5137c478bdstevel@tonic-gate
5147c478bdstevel@tonic-gate	(void) mdb_nv_create(&mdb.m_disasms, UM_SLEEP);
5157c478bdstevel@tonic-gate	(void) mdb_nv_create(&mdb.m_modules, UM_SLEEP);
5167c478bdstevel@tonic-gate	(void) mdb_nv_create(&mdb.m_dcmds, UM_SLEEP);
5177c478bdstevel@tonic-gate	(void) mdb_nv_create(&mdb.m_walkers, UM_SLEEP);
5187c478bdstevel@tonic-gate	(void) mdb_nv_create(&mdb.m_nv, UM_SLEEP);
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gate	mdb.m_dot = mdb_nv_insert(&mdb.m_nv, ".", NULL, 0, MDB_NV_PERSIST);
5217c478bdstevel@tonic-gate	mdb.m_rvalue = mdb_nv_insert(&mdb.m_nv, "0", NULL, 0, MDB_NV_PERSIST);
5227c478bdstevel@tonic-gate
5237c478bdstevel@tonic-gate	mdb.m_roffset =
5247c478bdstevel@tonic-gate	    mdb_nv_insert(&mdb.m_nv, "1", &roff_disc, 0, MDB_NV_PERSIST);
5257c478bdstevel@tonic-gate
5267c478bdstevel@tonic-gate	mdb.m_proffset = mdb_nv_insert(&mdb.m_nv, "2", NULL, 0, MDB_NV_PERSIST);
5277c478bdstevel@tonic-gate	mdb.m_rcount = mdb_nv_insert(&mdb.m_nv, "9", NULL, 0, MDB_NV_PERSIST);
5287c478bdstevel@tonic-gate
5297c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_nv, "b", NULL, 0, MDB_NV_PERSIST);
5307c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_nv, "d", NULL, 0, MDB_NV_PERSIST);
5317c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_nv, "e", NULL, 0, MDB_NV_PERSIST);
5327c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_nv, "m", NULL, 0, MDB_NV_PERSIST);
5337c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_nv, "t", NULL, 0, MDB_NV_PERSIST);
5347c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_nv, "_", &psym_disc, 0, MDB_NV_PERSIST);
5357c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_nv, "hits", NULL, 0, MDB_NV_PERSIST);
5367c478bdstevel@tonic-gate
5377c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_nv, "thread", &thr_disc, 0,
5387c478bdstevel@tonic-gate	    MDB_NV_PERSIST | MDB_NV_RDONLY);
5397c478bdstevel@tonic-gate
5407c478bdstevel@tonic-gate	mdb.m_prsym = mdb_gelf_symtab_create_mutable();
5417c478bdstevel@tonic-gate
5427c478bdstevel@tonic-gate	(void) mdb_nv_insert(&mdb.m_modules, mdb.m_pname, NULL,
5437c478bdstevel@tonic-gate	    (uintptr_t)&mdb.m_rmod, MDB_NV_RDONLY);
5447c478bdstevel@tonic-gate
5457c478bdstevel@tonic-gate	for (dcp = &mdb_dcmd_builtins[0]; dcp->dc_name != NULL; dcp++)
5467c478bdstevel@tonic-gate		(void) mdb_module_add_dcmd(&mdb.m_rmod, dcp, 0);
5477c478bdstevel@tonic-gate
5487c478bdstevel@tonic-gate	for (i = 0; mdb_dis_builtins[i] != NULL; i++)
5497c478bdstevel@tonic-gate		(void) mdb_dis_create(mdb_dis_builtins[i]);
5507c478bdstevel@tonic-gate
5517c478bdstevel@tonic-gate	mdb_macalias_create();
5527c478bdstevel@tonic-gate
5537c478bdstevel@tonic-gate	mdb_create_builtin_tgts();
5547c478bdstevel@tonic-gate
5557c478bdstevel@tonic-gate	(void) mdb_callb_add(NULL, MDB_CALLB_PROMPT, (mdb_callb_f)prompt_update,
5567c478bdstevel@tonic-gate	    NULL);
5577c478bdstevel@tonic-gate
5580a47c91Robert Mustacchi	/*
5590a47c91Robert Mustacchi	 * The call to ctf_create that this does can in fact fail, but that's
5600a47c91Robert Mustacchi	 * okay. All of the ctf functions that might use the synthetic types
5610a47c91Robert Mustacchi	 * make sure that this is safe.
5620a47c91Robert Mustacchi	 */
5630a47c91Robert Mustacchi	(void) mdb_ctf_synthetics_init();
5640a47c91Robert Mustacchi
5657c478bdstevel@tonic-gate#ifdef _KMDB
5667c478bdstevel@tonic-gate	(void) mdb_nv_create(&mdb.m_dmodctl, UM_SLEEP);
5677c478bdstevel@tonic-gate#endif
5687c478bdstevel@tonic-gate	mdb_lex_state_create(&frame0);
5697c478bdstevel@tonic-gate
5707c478bdstevel@tonic-gate	mdb_list_append(&mdb.m_flist, &frame0);
5717c478bdstevel@tonic-gate	mdb.m_frame = &frame0;
5727c478bdstevel@tonic-gate}
5737c478bdstevel@tonic-gate
5747c478bdstevel@tonic-gatevoid
5757c478bdstevel@tonic-gatemdb_destroy(void)
5767c478bdstevel@tonic-gate{
5777c478bdstevel@tonic-gate	const mdb_dcmd_t *dcp;
5787c478bdstevel@tonic-gate	mdb_var_t *v;
5797c478bdstevel@tonic-gate	int unload_mode = MDB_MOD_SILENT;
5807c478bdstevel@tonic-gate
5817c478bdstevel@tonic-gate#ifdef _KMDB
5827c478bdstevel@tonic-gate	unload_mode |= MDB_MOD_DEFER;
5837c478bdstevel@tonic-gate#endif
5847c478bdstevel@tonic-gate
5857c478bdstevel@tonic-gate	mdb_intr_disable();
5867c478bdstevel@tonic-gate
5870a47c91Robert Mustacchi	mdb_ctf_synthetics_fini();
5880a47c91Robert Mustacchi
5897c478bdstevel@tonic-gate	mdb_macalias_destroy();
5907c478bdstevel@tonic-gate
5917c478bdstevel@tonic-gate	/*
592843e198johnlev	 * Some targets use modules during ->t_destroy, so do it first.
593843e198johnlev	 */
594843e198johnlev	if (mdb.m_target != NULL)
595843e198johnlev		(void) mdb_tgt_destroy(mdb.m_target);
596843e198johnlev
597843e198johnlev	/*
5987c478bdstevel@tonic-gate	 * Unload modules _before_ destroying the disassemblers since a
5997c478bdstevel@tonic-gate	 * module that installs a disassembler should try to clean up after
6007c478bdstevel@tonic-gate	 * itself.
6017c478bdstevel@tonic-gate	 */
6027c478bdstevel@tonic-gate	mdb_module_unload_all(unload_mode);
6037c478bdstevel@tonic-gate
6047c478bdstevel@tonic-gate	mdb_nv_rewind(&mdb.m_disasms);
6057c478bdstevel@tonic-gate	while ((v = mdb_nv_advance(&mdb.m_disasms)) != NULL)
6067c478bdstevel@tonic-gate		mdb_dis_destroy(mdb_nv_get_cookie(v));
6077c478bdstevel@tonic-gate
6087c478bdstevel@tonic-gate	mdb_callb_remove_all();
6097c478bdstevel@tonic-gate
6107c478bdstevel@tonic-gate	if (mdb.m_defdisasm != NULL)
6117c478bdstevel@tonic-gate		strfree(mdb.m_defdisasm);
6127c478bdstevel@tonic-gate
6137c478bdstevel@tonic-gate	if (mdb.m_prsym != NULL)
6147c478bdstevel@tonic-gate		mdb_gelf_symtab_destroy(mdb.m_prsym);
6157c478bdstevel@tonic-gate
6167c478bdstevel@tonic-gate	for (dcp = &mdb_dcmd_builtins[0]; dcp->dc_name != NULL; dcp++)
6177c478bdstevel@tonic-gate		(void) mdb_module_remove_dcmd(&mdb.m_rmod, dcp->dc_name);
6187c478bdstevel@tonic-gate
6197c478bdstevel@tonic-gate	mdb_nv_destroy(&mdb.m_nv);
6207c478bdstevel@tonic-gate	mdb_nv_destroy(&mdb.m_walkers);
6217c478bdstevel@tonic-gate	mdb_nv_destroy(&mdb.m_dcmds);
6227c478bdstevel@tonic-gate	mdb_nv_destroy(&mdb.m_modules);
6237c478bdstevel@tonic-gate	mdb_nv_destroy(&mdb.m_disasms);
6247c478bdstevel@tonic-gate
6257c478bdstevel@tonic-gate	mdb_free(mdb.m_ipathstr, MAXPATHLEN);
6267c478bdstevel@tonic-gate	mdb_free(mdb.m_lpathstr, MAXPATHLEN);
6277c478bdstevel@tonic-gate
6287c478bdstevel@tonic-gate	if (mdb.m_ipath != NULL)
6297c478bdstevel@tonic-gate		mdb_path_free(mdb.m_ipath, mdb.m_ipathlen);
6307c478bdstevel@tonic-gate
6317c478bdstevel@tonic-gate	if (mdb.m_lpath != NULL)
6327c478bdstevel@tonic-gate		mdb_path_free(mdb.m_lpath, mdb.m_lpathlen);
6337c478bdstevel@tonic-gate
6347c478bdstevel@tonic-gate	if (mdb.m_in != NULL)
6357c478bdstevel@tonic-gate		mdb_iob_destroy(mdb.m_in);
6367c478bdstevel@tonic-gate
6377c478bdstevel@tonic-gate	mdb_iob_destroy(mdb.m_out);
6387c478bdstevel@tonic-gate	mdb.m_out = NULL;
6397c478bdstevel@tonic-gate	mdb_iob_destroy(mdb.m_err);
6407c478bdstevel@tonic-gate	mdb.m_err = NULL;
6417c478bdstevel@tonic-gate
6427c478bdstevel@tonic-gate	if (mdb.m_log != NULL)
6437c478bdstevel@tonic-gate		mdb_io_rele(mdb.m_log);
6447c478bdstevel@tonic-gate
6457c478bdstevel@tonic-gate	mdb_lex_state_destroy(&frame0);
6467c478bdstevel@tonic-gate}
6477c478bdstevel@tonic-gate
6487c478bdstevel@tonic-gate/*
6497c478bdstevel@tonic-gate * The real main loop of the debugger: create a new execution frame on the
6507c478bdstevel@tonic-gate * debugger stack, and while we have input available, call into the parser.
6517c478bdstevel@tonic-gate */
6527c478bdstevel@tonic-gateint
6537c478bdstevel@tonic-gatemdb_run(void)
6547c478bdstevel@tonic-gate{
6557c478bdstevel@tonic-gate	volatile int err;
6567c478bdstevel@tonic-gate	mdb_frame_t f;
6577c478bdstevel@tonic-gate
6587c478bdstevel@tonic-gate	mdb_intr_disable();
6597c478bdstevel@tonic-gate	mdb_frame_push(&f);
6607c478bdstevel@tonic-gate
6617c478bdstevel@tonic-gate	/*
6627c478bdstevel@tonic-gate	 * This is a fresh mdb context, so ignore any pipe command we may have
6637c478bdstevel@tonic-gate	 * inherited from the previous frame.
6647c478bdstevel@tonic-gate	 */
6657c478bdstevel@tonic-gate	f.f_pcmd = NULL;
6667c478bdstevel@tonic-gate
6677c478bdstevel@tonic-gate	if ((err = setjmp(f.f_pcb)) != 0) {
6687c478bdstevel@tonic-gate		int pop = (mdb.m_in != NULL &&
6697c478bdstevel@tonic-gate		    (mdb_iob_isapipe(mdb.m_in) || mdb_iob_isastr(mdb.m_in)));
6707c478bdstevel@tonic-gate		int fromcmd = (f.f_cp != NULL);
6717c478bdstevel@tonic-gate
6727c478bdstevel@tonic-gate		mdb_dprintf(MDB_DBG_DSTK, "frame <%u> caught event %s\n",
6737c478bdstevel@tonic-gate		    f.f_id, mdb_err2str(err));
6747c478bdstevel@tonic-gate
6757c478bdstevel@tonic-gate		/*
6767c478bdstevel@tonic-gate		 * If a syntax error or other failure has occurred, pop all
6777c478bdstevel@tonic-gate		 * input buffers pushed by commands executed in this frame.
6787c478bdstevel@tonic-gate		 */
6797c478bdstevel@tonic-gate		while (mdb_iob_stack_size(&f.f_istk) != 0) {
6807c478bdstevel@tonic-gate			if (mdb.m_in != NULL)
6817c478bdstevel@tonic-gate				mdb_iob_destroy(mdb.m_in);
6827c478bdstevel@tonic-gate			mdb.m_in = mdb_iob_stack_pop(&f.f_istk);
6837c478bdstevel@tonic-gate			yylineno = mdb_iob_lineno(mdb.m_in);
6847c478bdstevel@tonic-gate		}
6857c478bdstevel@tonic-gate
6867c478bdstevel@tonic-gate		/*
6877c478bdstevel@tonic-gate		 * Reset standard output and the current frame to a known,
6887c478bdstevel@tonic-gate		 * clean state, so we can continue execution.
6897c478bdstevel@tonic-gate		 */
6907c478bdstevel@tonic-gate		mdb_iob_margin(mdb.m_out, MDB_IOB_DEFMARGIN);
6917c478bdstevel@tonic-gate		mdb_iob_clrflags(mdb.m_out, MDB_IOB_INDENT);
6927c478bdstevel@tonic-gate		mdb_iob_discard(mdb.m_out);
6937c478bdstevel@tonic-gate		mdb_frame_reset(&f);
6947c478bdstevel@tonic-gate
6957c478bdstevel@tonic-gate		/*
6967c478bdstevel@tonic-gate		 * If there was an error writing to output, display a warning
6977c478bdstevel@tonic-gate		 * message if this is the topmost frame.
6987c478bdstevel@tonic-gate		 */
6997c478bdstevel@tonic-gate		if (err == MDB_ERR_OUTPUT && mdb.m_depth == 1 && errno != EPIPE)
7007c478bdstevel@tonic-gate			mdb_warn("write failed");
7017c478bdstevel@tonic-gate
7027c478bdstevel@tonic-gate		/*
7037c478bdstevel@tonic-gate		 * If an interrupt or quit signal is reported, we may have been
7047c478bdstevel@tonic-gate		 * in the middle of typing or processing the command line:
7057c478bdstevel@tonic-gate		 * print a newline and discard everything in the parser's iob.
7067c478bdstevel@tonic-gate		 * Note that we do this after m_out has been reset, otherwise
7077c478bdstevel@tonic-gate		 * we could trigger a pipe context switch or cause a write
7087c478bdstevel@tonic-gate		 * to a broken pipe (in the case of a shell command) when
7097c478bdstevel@tonic-gate		 * writing the newline.
7107c478bdstevel@tonic-gate		 */
7117c478bdstevel@tonic-gate		if (err == MDB_ERR_SIGINT || err == MDB_ERR_QUIT) {
7127c478bdstevel@tonic-gate			mdb_iob_nl(mdb.m_out);
7137c478bdstevel@tonic-gate			yydiscard();
7147c478bdstevel@tonic-gate		}
7157c478bdstevel@tonic-gate
7167c478bdstevel@tonic-gate		/*
7177c478bdstevel@tonic-gate		 * If we quit or abort using the output pager, reset the
7187c478bdstevel@tonic-gate		 * line count on standard output back to zero.
7197c478bdstevel@tonic-gate		 */
7207c478bdstevel@tonic-gate		if (err == MDB_ERR_PAGER || MDB_ERR_IS_FATAL(err))
7217c478bdstevel@tonic-gate			mdb_iob_clearlines(mdb.m_out);
7227c478bdstevel@tonic-gate
7237c478bdstevel@tonic-gate		/*
7247c478bdstevel@tonic-gate		 * If the user requested the debugger quit or abort back to
7257c478bdstevel@tonic-gate		 * the top, or if standard input is a pipe or mdb_eval("..."),
7267c478bdstevel@tonic-gate		 * then propagate the error up the debugger stack.
7277c478bdstevel@tonic-gate		 */
7287c478bdstevel@tonic-gate		if (MDB_ERR_IS_FATAL(err) || pop != 0 ||
7297c478bdstevel@tonic-gate		    (err == MDB_ERR_PAGER && mdb.m_fmark != &f) ||
7307c478bdstevel@tonic-gate		    (err == MDB_ERR_NOMEM && !fromcmd)) {
7317c478bdstevel@tonic-gate			mdb_frame_pop(&f, err);
7327c478bdstevel@tonic-gate			return (err);
7337c478bdstevel@tonic-gate		}
7347c478bdstevel@tonic-gate
7357c478bdstevel@tonic-gate		/*
7367c478bdstevel@tonic-gate		 * If we've returned here from a context where signals were
7377c478bdstevel@tonic-gate		 * blocked (e.g. a signal handler), we can now unblock them.
7387c478bdstevel@tonic-gate		 */
7397c478bdstevel@tonic-gate		if (err == MDB_ERR_SIGINT)
7407c478bdstevel@tonic-gate			(void) mdb_signal_unblock(SIGINT);
7417c478bdstevel@tonic-gate	} else
7427c478bdstevel@tonic-gate		mdb_intr_enable();
7437c478bdstevel@tonic-gate
7447c478bdstevel@tonic-gate	for (;;) {
7457c478bdstevel@tonic-gate		while (mdb.m_in != NULL && (mdb_iob_getflags(mdb.m_in) &
7467c478bdstevel@tonic-gate		    (MDB_IOB_ERR | MDB_IOB_EOF)) == 0) {
7477c478bdstevel@tonic-gate			if (mdb.m_depth == 1 &&
7487c478bdstevel@tonic-gate			    mdb_iob_stack_size(&f.f_istk) == 0) {
7497c478bdstevel@tonic-gate				mdb_iob_clearlines(mdb.m_out);
7507c478bdstevel@tonic-gate				mdb_tgt_periodic(mdb.m_target);
7517c478bdstevel@tonic-gate			}
7527c478bdstevel@tonic-gate
7537c478bdstevel@tonic-gate			(void) yyparse();
7547c478bdstevel@tonic-gate		}
7557c478bdstevel@tonic-gate
7567c478bdstevel@tonic-gate		if (mdb.m_in != NULL) {
7577c478bdstevel@tonic-gate			if (mdb_iob_err(mdb.m_in)) {
7587c478bdstevel@tonic-gate				warn("error reading input stream %s\n",
7597c478bdstevel@tonic-gate				    mdb_iob_name(mdb.m_in));
7607c478bdstevel@tonic-gate			}
7617c478bdstevel@tonic-gate			mdb_iob_destroy(mdb.m_in);
7627c478bdstevel@tonic-gate			mdb.m_in = NULL;
7637c478bdstevel@tonic-gate		}
7647c478bdstevel@tonic-gate
7657c478bdstevel@tonic-gate		if (mdb_iob_stack_size(&f.f_istk) == 0)
7667c478bdstevel@tonic-gate			break; /* return when we're out of input */
7677c478bdstevel@tonic-gate
7687c478bdstevel@tonic-gate		mdb.m_in = mdb_iob_stack_pop(&f.f_istk);
7697c478bdstevel@tonic-gate		yylineno = mdb_iob_lineno(mdb.m_in);
7707c478bdstevel@tonic-gate	}
7717c478bdstevel@tonic-gate
7727c478bdstevel@tonic-gate	mdb_frame_pop(&f, 0);
7737c478bdstevel@tonic-gate
7747c478bdstevel@tonic-gate	/*
7757c478bdstevel@tonic-gate	 * The value of '.' is a per-frame attribute, to preserve it properly
7767c478bdstevel@tonic-gate	 * when switching frames.  But in the case of calling mdb_run()
7777c478bdstevel@tonic-gate	 * explicitly (such as through mdb_eval), we want to propagate the value
7787c478bdstevel@tonic-gate	 * of '.' to the parent.
7797c478bdstevel@tonic-gate	 */
7807c478bdstevel@tonic-gate	mdb_nv_set_value(mdb.m_dot, f.f_dot);
7817c478bdstevel@tonic-gate
7827c478bdstevel@tonic-gate	return (0);
7837c478bdstevel@tonic-gate}
7847c478bdstevel@tonic-gate
7857c478bdstevel@tonic-gate/*
7867c478bdstevel@tonic-gate * The read-side of the pipe executes this service routine.  We simply call
7877c478bdstevel@tonic-gate * mdb_run to create a new frame on the execution stack and run the MDB parser,
7887c478bdstevel@tonic-gate * and then propagate any error code back to the previous frame.
7897c478bdstevel@tonic-gate */
7907c478bdstevel@tonic-gatestatic int
7917c478bdstevel@tonic-gaterunsvc(void)
7927c478bdstevel@tonic-gate{
7937c478bdstevel@tonic-gate	int err = mdb_run();
7947c478bdstevel@tonic-gate
7957c478bdstevel@tonic-gate	if (err != 0) {
7967c478bdstevel@tonic-gate		mdb_dprintf(MDB_DBG_DSTK, "forwarding error %s from pipeline\n",
7977c478bdstevel@tonic-gate		    mdb_err2str(err));
7987c478bdstevel@tonic-gate		longjmp(mdb.m_frame->f_pcb, err);
7997c478bdstevel@tonic-gate	}
8007c478bdstevel@tonic-gate
8017c478bdstevel@tonic-gate	return (err);
8027c478bdstevel@tonic-gate}
8037c478bdstevel@tonic-gate
8047c478bdstevel@tonic-gate/*
8057c478bdstevel@tonic-gate * Read-side pipe service routine: if we longjmp here, just return to the read
8067c478bdstevel@tonic-gate * routine because now we have more data to consume.  Otherwise:
8077c478bdstevel@tonic-gate * (1) if ctx_data is non-NULL, longjmp to the write-side to produce more data;
8087c478bdstevel@tonic-gate * (2) if wriob is NULL, there is no writer but this is the first read, so we
8097c478bdstevel@tonic-gate *     can just execute mdb_run() to completion on the current stack;
8107c478bdstevel@tonic-gate * (3) if (1) and (2) are false, then there is a writer and this is the first
8117c478bdstevel@tonic-gate *     read, so create a co-routine context to execute mdb_run().
8127c478bdstevel@tonic-gate */
8137c478bdstevel@tonic-gate/*ARGSUSED*/
8147c478bdstevel@tonic-gatestatic void
8157c478bdstevel@tonic-gaterdsvc(mdb_iob_t *rdiob, mdb_iob_t *wriob, mdb_iob_ctx_t *ctx)
8167c478bdstevel@tonic-gate{
8177c478bdstevel@tonic-gate	if (setjmp(ctx->ctx_rpcb) == 0) {
8187c478bdstevel@tonic-gate		/*
8197c478bdstevel@tonic-gate		 * Save the current standard input into the pipe context, and
8207c478bdstevel@tonic-gate		 * reset m_in to point to the pipe.  We will restore it on
8217c478bdstevel@tonic-gate		 * the way back in wrsvc() below.
8227c478bdstevel@tonic-gate		 */
8237c478bdstevel@tonic-gate		ctx->ctx_iob = mdb.m_in;
8247c478bdstevel@tonic-gate		mdb.m_in = rdiob;
8257c478bdstevel@tonic-gate
8267c478bdstevel@tonic-gate		ctx->ctx_rptr = mdb.m_frame;
8277c478bdstevel@tonic-gate		if (ctx->ctx_wptr != NULL)
8287c478bdstevel@tonic-gate			mdb_frame_switch(ctx->ctx_wptr);
8297c478bdstevel@tonic-gate
8307c478bdstevel@tonic-gate		if (ctx->ctx_data != NULL)
8317c478bdstevel@tonic-gate			longjmp(ctx->ctx_wpcb, 1);
8327c478bdstevel@tonic-gate		else if (wriob == NULL)
8337c478bdstevel@tonic-gate			(void) runsvc();
8347c478bdstevel@tonic-gate		else if ((ctx->ctx_data = mdb_context_create(runsvc)) != NULL)
8357c478bdstevel@tonic-gate			mdb_context_switch(ctx->ctx_data);
8367c478bdstevel@tonic-gate		else
8377c478bdstevel@tonic-gate			mdb_warn("failed to create pipe context");
8387c478bdstevel@tonic-gate	}
8397c478bdstevel@tonic-gate}
8407c478bdstevel@tonic-gate
8417c478bdstevel@tonic-gate/*
8427c478bdstevel@tonic-gate * Write-side pipe service routine: if we longjmp here, just return to the
8437c478bdstevel@tonic-gate * write routine because now we have free space in the pipe buffer for writing;
8447c478bdstevel@tonic-gate * otherwise longjmp to the read-side to consume data and create space for us.
8457c478bdstevel@tonic-gate */
8467c478bdstevel@tonic-gate/*ARGSUSED*/
8477c478bdstevel@tonic-gatestatic void
8487c478bdstevel@tonic-gatewrsvc(mdb_iob_t *rdiob, mdb_iob_t *wriob, mdb_iob_ctx_t *ctx)
8497c478bdstevel@tonic-gate{
8507c478bdstevel@tonic-gate	if (setjmp(ctx->ctx_wpcb) == 0) {
8517c478bdstevel@tonic-gate		ctx->ctx_wptr = mdb.m_frame;
8527c478bdstevel@tonic-gate		if (ctx->ctx_rptr != NULL)
8537c478bdstevel@tonic-gate			mdb_frame_switch(ctx->ctx_rptr);
8547c478bdstevel@tonic-gate
8557c478bdstevel@tonic-gate		mdb.m_in = ctx->ctx_iob;
8567c478bdstevel@tonic-gate		longjmp(ctx->ctx_rpcb, 1);
8577c478bdstevel@tonic-gate	}
8587c478bdstevel@tonic-gate}
8597c478bdstevel@tonic-gate
8607c478bdstevel@tonic-gate/*
8617c478bdstevel@tonic-gate * Call the current frame's mdb command.  This entry point is used by the
8627c478bdstevel@tonic-gate * MDB parser to actually execute a command once it has successfully parsed
8637c478bdstevel@tonic-gate * a line of input.  The command is waiting for us in the current frame.
8647c478bdstevel@tonic-gate * We loop through each command on the list, executing its dcmd with the
8657c478bdstevel@tonic-gate * appropriate argument.  If the command has a successor, we know it had
8667c478bdstevel@tonic-gate * a | operator after it, and so we need to create a pipe and replace
8677c478bdstevel@tonic-gate * stdout with the pipe's output buffer.
8687c478bdstevel@tonic-gate */
8697c478bdstevel@tonic-gateint
8707c478bdstevel@tonic-gatemdb_call(uintmax_t addr, uintmax_t count, uint_t flags)
8717c478bdstevel@tonic-gate{
8727c478bdstevel@tonic-gate	mdb_frame_t *fp = mdb.m_frame;
8737c478bdstevel@tonic-gate	mdb_cmd_t *cp, *ncp;
8747c478bdstevel@tonic-gate	mdb_iob_t *iobs[2];
8757c478bdstevel@tonic-gate	int status, err = 0;
8767c478bdstevel@tonic-gate	jmp_buf pcb;
8777c478bdstevel@tonic-gate
8787c478bdstevel@tonic-gate	if (mdb_iob_isapipe(mdb.m_in))
8797c478bdstevel@tonic-gate		yyerror("syntax error");
8807c478bdstevel@tonic-gate
8817c478bdstevel@tonic-gate	mdb_intr_disable();
8827c478bdstevel@tonic-gate	fp->f_cp = mdb_list_next(&fp->f_cmds);
8837c478bdstevel@tonic-gate
8847c478bdstevel@tonic-gate	if (flags & DCMD_LOOP)
8857c478bdstevel@tonic-gate		flags |= DCMD_LOOPFIRST; /* set LOOPFIRST if this is a loop */
8867c478bdstevel@tonic-gate
8877c478bdstevel@tonic-gate	for (cp = mdb_list_next(&fp->f_cmds); cp; cp = mdb_list_next(cp)) {
8887c478bdstevel@tonic-gate		if (mdb_list_next(cp) != NULL) {
8897c478bdstevel@tonic-gate			mdb_iob_pipe(iobs, rdsvc, wrsvc);
8907c478bdstevel@tonic-gate
8917c478bdstevel@tonic-gate			mdb_iob_stack_push(&fp->f_istk, mdb.m_in, yylineno);
8927c478bdstevel@tonic-gate			mdb.m_in = iobs[MDB_IOB_RDIOB];
8937c478bdstevel@tonic-gate
8947c478bdstevel@tonic-gate			mdb_iob_stack_push(&fp->f_ostk, mdb.m_out, 0);
8957c478bdstevel@tonic-gate			mdb.m_out = iobs[MDB_IOB_WRIOB];
8967c478bdstevel@tonic-gate
8977c478bdstevel@tonic-gate			ncp = mdb_list_next(cp);
8987c478bdstevel@tonic-gate			mdb_vcb_inherit(cp, ncp);
8997c478bdstevel@tonic-gate
9007c478bdstevel@tonic-gate			bcopy(fp->f_pcb, pcb, sizeof (jmp_buf));
9017c478bdstevel@tonic-gate			ASSERT(fp->f_pcmd == NULL);
9027c478bdstevel@tonic-gate			fp->f_pcmd = ncp;
9037c478bdstevel@tonic-gate
9048a6a72faf			mdb_frame_set_pipe(fp);
9058a6a72faf
9067c478bdstevel@tonic-gate			if ((err = setjmp(fp->f_pcb)) == 0) {
9077c478bdstevel@tonic-gate				status = mdb_call_idcmd(cp->c_dcmd, addr, count,
9087c478bdstevel@tonic-gate				    flags | DCMD_PIPE_OUT, &cp->c_argv,
9097c478bdstevel@tonic-gate				    &cp->c_addrv, cp->c_vcbs);
9107c478bdstevel@tonic-gate
9110a47c91Robert Mustacchi				mdb.m_lastret = status;
9120a47c91Robert Mustacchi
9137c478bdstevel@tonic-gate				ASSERT(mdb.m_in == iobs[MDB_IOB_RDIOB]);
9147c478bdstevel@tonic-gate				ASSERT(mdb.m_out == iobs[MDB_IOB_WRIOB]);
9157c478bdstevel@tonic-gate			} else {
9167c478bdstevel@tonic-gate				mdb_dprintf(MDB_DBG_DSTK, "frame <%u> caught "
9177c478bdstevel@tonic-gate				    "error %s from pipeline\n", fp->f_id,
9187c478bdstevel@tonic-gate				    mdb_err2str(err));
9197c478bdstevel@tonic-gate			}
9207c478bdstevel@tonic-gate
9217c478bdstevel@tonic-gate			if (err != 0 || DCMD_ABORTED(status)) {
9227c478bdstevel@tonic-gate				mdb_iob_setflags(mdb.m_in, MDB_IOB_ERR);
9237c478bdstevel@tonic-gate				mdb_iob_setflags(mdb.m_out, MDB_IOB_ERR);
9247c478bdstevel@tonic-gate			} else {
9257c478bdstevel@tonic-gate				mdb_iob_flush(mdb.m_out);
9267c478bdstevel@tonic-gate				(void) mdb_iob_ctl(mdb.m_out, I_FLUSH,
9277c478bdstevel@tonic-gate				    (void *)FLUSHW);
9287c478bdstevel@tonic-gate			}
9297c478bdstevel@tonic-gate
9308a6a72faf			mdb_frame_clear_pipe(fp);
9318a6a72faf
9327c478bdstevel@tonic-gate			mdb_iob_destroy(mdb.m_out);
9337c478bdstevel@tonic-gate			mdb.m_out = mdb_iob_stack_pop(&fp->f_ostk);
9347c478bdstevel@tonic-gate
9357c478bdstevel@tonic-gate			if (mdb.m_in != NULL)
9367c478bdstevel@tonic-gate				mdb_iob_destroy(mdb.m_in);
9377c478bdstevel@tonic-gate
9387c478bdstevel@tonic-gate			mdb.m_in = mdb_iob_stack_pop(&fp->f_istk);
9397c478bdstevel@tonic-gate			yylineno = mdb_iob_lineno(mdb.m_in);
9407c478bdstevel@tonic-gate
9417c478bdstevel@tonic-gate			fp->f_pcmd = NULL;
9427c478bdstevel@tonic-gate			bcopy(pcb, fp->f_pcb, sizeof (jmp_buf));
9437c478bdstevel@tonic-gate
9447c478bdstevel@tonic-gate			if (MDB_ERR_IS_FATAL(err))
9457c478bdstevel@tonic-gate				longjmp(fp->f_pcb, err);
9467c478bdstevel@tonic-gate
9477c478bdstevel@tonic-gate			if (err != 0 || DCMD_ABORTED(status) ||
9487c478bdstevel@tonic-gate			    mdb_addrvec_length(&ncp->c_addrv) == 0)
9497c478bdstevel@tonic-gate				break;
9507c478bdstevel@tonic-gate
9517c478bdstevel@tonic-gate			addr = mdb_nv_get_value(mdb.m_dot);
9527c478bdstevel@tonic-gate			count = 1;
9537c478bdstevel@tonic-gate			flags = 0;
9547c478bdstevel@tonic-gate
9557c478bdstevel@tonic-gate		} else {
9567c478bdstevel@tonic-gate			mdb_intr_enable();
9570a47c91Robert Mustacchi			mdb.m_lastret = mdb_call_idcmd(cp->c_dcmd, addr, count,
9580a47c91Robert Mustacchi			    flags, &cp->c_argv, &cp->c_addrv, cp->c_vcbs);
9597c478bdstevel@tonic-gate			mdb_intr_disable();
9607c478bdstevel@tonic-gate		}
9617c478bdstevel@tonic-gate
9627c478bdstevel@tonic-gate		fp->f_cp = mdb_list_next(cp);
9637c478bdstevel@tonic-gate		mdb_cmd_reset(cp);
9647c478bdstevel@tonic-gate	}
9657c478bdstevel@tonic-gate
9667c478bdstevel@tonic-gate	/*
9677c478bdstevel@tonic-gate	 * If our last-command list is non-empty, destroy it.  Then copy the
9687c478bdstevel@tonic-gate	 * current frame's cmd list to the m_lastc list and reset the frame.
9697c478bdstevel@tonic-gate	 */
9707c478bdstevel@tonic-gate	while ((cp = mdb_list_next(&mdb.m_lastc)) != NULL) {
9717c478bdstevel@tonic-gate		mdb_list_delete(&mdb.m_lastc, cp);
9727c478bdstevel@tonic-gate		mdb_cmd_destroy(cp);
9737c478bdstevel@tonic-gate	}
9747c478bdstevel@tonic-gate
9757c478bdstevel@tonic-gate	mdb_list_move(&fp->f_cmds, &mdb.m_lastc);
9767c478bdstevel@tonic-gate	mdb_frame_reset(fp);
9777c478bdstevel@tonic-gate	mdb_intr_enable();
9787c478bdstevel@tonic-gate	return (err == 0);
9797c478bdstevel@tonic-gate}
9807c478bdstevel@tonic-gate
9817c478bdstevel@tonic-gateuintmax_t
9827c478bdstevel@tonic-gatemdb_dot_incr(const char *op)
9837c478bdstevel@tonic-gate{
9847c478bdstevel@tonic-gate	uintmax_t odot, ndot;
9857c478bdstevel@tonic-gate
9867c478bdstevel@tonic-gate	odot = mdb_nv_get_value(mdb.m_dot);
9877c478bdstevel@tonic-gate	ndot = odot + mdb.m_incr;
9887c478bdstevel@tonic-gate
9897c478bdstevel@tonic-gate	if ((odot ^ ndot) & 0x8000000000000000ull)
9907c478bdstevel@tonic-gate		yyerror("'%s' would cause '.' to overflow\n", op);
9917c478bdstevel@tonic-gate
9927c478bdstevel@tonic-gate	return (ndot);
9937c478bdstevel@tonic-gate}
9947c478bdstevel@tonic-gate
9957c478bdstevel@tonic-gateuintmax_t
9967c478bdstevel@tonic-gatemdb_dot_decr(const char *op)
9977c478bdstevel@tonic-gate{
9987c478bdstevel@tonic-gate	uintmax_t odot, ndot;
9997c478bdstevel@tonic-gate
10007c478bdstevel@tonic-gate	odot = mdb_nv_get_value(mdb.m_dot);
10017c478bdstevel@tonic-gate	ndot = odot - mdb.m_incr;
10027c478bdstevel@tonic-gate
10037c478bdstevel@tonic-gate	if (ndot > odot)
10047c478bdstevel@tonic-gate		yyerror("'%s' would cause '.' to underflow\n", op);
10057c478bdstevel@tonic-gate
10067c478bdstevel@tonic-gate	return (ndot);
10077c478bdstevel@tonic-gate}
10087c478bdstevel@tonic-gate
10097c478bdstevel@tonic-gatemdb_iwalker_t *
10107c478bdstevel@tonic-gatemdb_walker_lookup(const char *s)
10117c478bdstevel@tonic-gate{
10127c478bdstevel@tonic-gate	const char *p = strchr(s, '`');
10137c478bdstevel@tonic-gate	mdb_var_t *v;
10147c478bdstevel@tonic-gate
10157c478bdstevel@tonic-gate	if (p != NULL) {
10167c478bdstevel@tonic-gate		size_t nbytes = MIN((size_t)(p - s), MDB_NV_NAMELEN - 1);
10177c478bdstevel@tonic-gate		char mname[MDB_NV_NAMELEN];
10187c478bdstevel@tonic-gate		mdb_module_t *mod;
10197c478bdstevel@tonic-gate
10207c478bdstevel@tonic-gate		(void) strncpy(mname, s, nbytes);
10217c478bdstevel@tonic-gate		mname[nbytes] = '\0';
10227c478bdstevel@tonic-gate
10237c478bdstevel@tonic-gate		if ((v = mdb_nv_lookup(&mdb.m_modules, mname)) == NULL) {
10247c478bdstevel@tonic-gate			(void) set_errno(EMDB_NOMOD);
10257c478bdstevel@tonic-gate			return (NULL);
10267c478bdstevel@tonic-gate		}
10277c478bdstevel@tonic-gate
10287c478bdstevel@tonic-gate		mod = mdb_nv_get_cookie(v);
10297c478bdstevel@tonic-gate
10307c478bdstevel@tonic-gate		if ((v = mdb_nv_lookup(&mod->mod_walkers, ++p)) != NULL)
10317c478bdstevel@tonic-gate			return (mdb_nv_get_cookie(v));
10327c478bdstevel@tonic-gate
10337c478bdstevel@tonic-gate	} else if ((v = mdb_nv_lookup(&mdb.m_walkers, s)) != NULL)
10347c478bdstevel@tonic-gate		return (mdb_nv_get_cookie(mdb_nv_get_cookie(v)));
10357c478bdstevel@tonic-gate
10367c478bdstevel@tonic-gate	(void) set_errno(EMDB_NOWALK);
10377c478bdstevel@tonic-gate	return (NULL);
10387c478bdstevel@tonic-gate}
10397c478bdstevel@tonic-gate
10407c478bdstevel@tonic-gatemdb_idcmd_t *
10417c478bdstevel@tonic-gatemdb_dcmd_lookup(const char *s)
10427c478bdstevel@tonic-gate{
10437c478bdstevel@tonic-gate	const char *p = strchr(s, '`');
10447c478bdstevel@tonic-gate	mdb_var_t *v;
10457c478bdstevel@tonic-gate
10467c478bdstevel@tonic-gate	if (p != NULL) {
10477c478bdstevel@tonic-gate		size_t nbytes = MIN((size_t)(p - s), MDB_NV_NAMELEN - 1);
10487c478bdstevel@tonic-gate		char mname[MDB_NV_NAMELEN];
10497c478bdstevel@tonic-gate		mdb_module_t *mod;
10507c478bdstevel@tonic-gate
10517c478bdstevel@tonic-gate		(void) strncpy(mname, s, nbytes);
10527c478bdstevel@tonic-gate		mname[nbytes] = '\0';
10537c478bdstevel@tonic-gate
10547c478bdstevel@tonic-gate		if ((v = mdb_nv_lookup(&mdb.m_modules, mname)) == NULL) {
10557c478bdstevel@tonic-gate			(void) set_errno(EMDB_NOMOD);
10567c478bdstevel@tonic-gate			return (NULL);
10577c478bdstevel@tonic-gate		}
10587c478bdstevel@tonic-gate
10597c478bdstevel@tonic-gate		mod = mdb_nv_get_cookie(v);
10607c478bdstevel@tonic-gate
10617c478bdstevel@tonic-gate		if ((v = mdb_nv_lookup(&mod->mod_dcmds, ++p)) != NULL)
10627c478bdstevel@tonic-gate			return (mdb_nv_get_cookie(v));
10637c478bdstevel@tonic-gate
10647c478bdstevel@tonic-gate	} else if ((v = mdb_nv_lookup(&mdb.m_dcmds, s)) != NULL)
10657c478bdstevel@tonic-gate		return (mdb_nv_get_cookie(mdb_nv_get_cookie(v)));
10667c478bdstevel@tonic-gate
10677c478bdstevel@tonic-gate	(void) set_errno(EMDB_NODCMD);
10687c478bdstevel@tonic-gate	return (NULL);
10697c478bdstevel@tonic-gate}
10707c478bdstevel@tonic-gate
10717c478bdstevel@tonic-gatevoid
10727c478bdstevel@tonic-gatemdb_dcmd_usage(const mdb_idcmd_t *idcp, mdb_iob_t *iob)
10737c478bdstevel@tonic-gate{
10747c478bdstevel@tonic-gate	const char *prefix = "", *usage = "";
10757c478bdstevel@tonic-gate	char name0 = idcp->idc_name[0];
10767c478bdstevel@tonic-gate
10777c478bdstevel@tonic-gate	if (idcp->idc_usage != NULL) {
10787c478bdstevel@tonic-gate		if (idcp->idc_usage[0] == ':') {
10797c478bdstevel@tonic-gate			if (name0 != ':' && name0 != '$')
10807c478bdstevel@tonic-gate				prefix = "address::";
10817c478bdstevel@tonic-gate			else
10827c478bdstevel@tonic-gate				prefix = "address";
10837c478bdstevel@tonic-gate			usage = &idcp->idc_usage[1];
10847c478bdstevel@tonic-gate
10857c478bdstevel@tonic-gate		} else if (idcp->idc_usage[0] == '?') {
10867c478bdstevel@tonic-gate			if (name0 != ':' && name0 != '$')
10877c478bdstevel@tonic-gate				prefix = "[address]::";
10887c478bdstevel@tonic-gate			else
10897c478bdstevel@tonic-gate				prefix = "[address]";
10907c478bdstevel@tonic-gate			usage = &idcp->idc_usage[1];
10917c478bdstevel@tonic-gate
10927c478bdstevel@tonic-gate		} else
10937c478bdstevel@tonic-gate			usage = idcp->idc_usage;
10947c478bdstevel@tonic-gate	}
10957c478bdstevel@tonic-gate
10967c478bdstevel@tonic-gate	mdb_iob_printf(iob, "Usage: %s%s %s\n", prefix, idcp->idc_name, usage);
10977c478bdstevel@tonic-gate
10987c478bdstevel@tonic-gate	if (idcp->idc_help != NULL) {
10997c478bdstevel@tonic-gate		mdb_iob_printf(iob, "%s: try '::help %s' for more "
11007c478bdstevel@tonic-gate		    "information\n", mdb.m_pname, idcp->idc_name);
11017c478bdstevel@tonic-gate	}
11027c478bdstevel@tonic-gate}
11037c478bdstevel@tonic-gate
11047c478bdstevel@tonic-gatestatic mdb_idcmd_t *
11057c478bdstevel@tonic-gatedcmd_ndef(const mdb_idcmd_t *idcp)
11067c478bdstevel@tonic-gate{
11077c478bdstevel@tonic-gate	mdb_var_t *v = mdb_nv_get_ndef(idcp->idc_var);
11087c478bdstevel@tonic-gate
11097c478bdstevel@tonic-gate	if (v != NULL)
11107c478bdstevel@tonic-gate		return (mdb_nv_get_cookie(mdb_nv_get_cookie(v)));
11117c478bdstevel@tonic-gate
11127c478bdstevel@tonic-gate	return (NULL);
11137c478bdstevel@tonic-gate}
11147c478bdstevel@tonic-gate
11157c478bdstevel@tonic-gatestatic int
11167c478bdstevel@tonic-gatedcmd_invoke(mdb_idcmd_t *idcp, uintptr_t addr, uint_t flags,
11177c478bdstevel@tonic-gate    int argc, const mdb_arg_t *argv, const mdb_vcb_t *vcbs)
11187c478bdstevel@tonic-gate{
11197c478bdstevel@tonic-gate	int status;
11207c478bdstevel@tonic-gate
11217c478bdstevel@tonic-gate	mdb_dprintf(MDB_DBG_DCMD, "dcmd %s`%s dot = %lr incr = %llr\n",
11227c478bdstevel@tonic-gate	    idcp->idc_modp->mod_name, idcp->idc_name, addr, mdb.m_incr);
11237c478bdstevel@tonic-gate
11247c478bdstevel@tonic-gate	if ((status = idcp->idc_funcp(addr, flags, argc, argv)) == DCMD_USAGE) {
11257c478bdstevel@tonic-gate		mdb_dcmd_usage(idcp, mdb.m_err);
11267c478bdstevel@tonic-gate		goto done;
11277c478bdstevel@tonic-gate	}
11287c478bdstevel@tonic-gate
11297c478bdstevel@tonic-gate	while (status == DCMD_NEXT && (idcp = dcmd_ndef(idcp)) != NULL)
11307c478bdstevel@tonic-gate		status = idcp->idc_funcp(addr, flags, argc, argv);
11317c478bdstevel@tonic-gate
11327c478bdstevel@tonic-gate	if (status == DCMD_USAGE)
11337c478bdstevel@tonic-gate		mdb_dcmd_usage(idcp, mdb.m_err);
11347c478bdstevel@tonic-gate
11357c478bdstevel@tonic-gate	if (status == DCMD_NEXT)
11367c478bdstevel@tonic-gate		status = DCMD_OK;
11377c478bdstevel@tonic-gatedone:
11387c478bdstevel@tonic-gate	/*
11397c478bdstevel@tonic-gate	 * If standard output is a pipe and there are vcbs active, we need to
11407c478bdstevel@tonic-gate	 * flush standard out and the write-side of the pipe.  The reasons for
11417c478bdstevel@tonic-gate	 * this are explained in more detail in mdb_vcb.c.
11427c478bdstevel@tonic-gate	 */
11437c478bdstevel@tonic-gate	if ((flags & DCMD_PIPE_OUT) && (vcbs != NULL)) {
11447c478bdstevel@tonic-gate		mdb_iob_flush(mdb.m_out);
11457c478bdstevel@tonic-gate		(void) mdb_iob_ctl(mdb.m_out, I_FLUSH, (void *)FLUSHW);
11467c478bdstevel@tonic-gate	}
11477c478bdstevel@tonic-gate
11487c478bdstevel@tonic-gate	return (status);
11497c478bdstevel@tonic-gate}
11507c478bdstevel@tonic-gate
11513b6e0a5Matt Amdurvoid
11523b6e0a5Matt Amdurmdb_call_tab(mdb_idcmd_t *idcp, mdb_tab_cookie_t *mcp, uint_t flags,
11533b6e0a5Matt Amdur    uintmax_t argc, mdb_arg_t *argv)
11543b6e0a5Matt Amdur{
11553b6e0a5Matt Amdur	if (idcp->idc_tabp == NULL)
11563b6e0a5Matt Amdur		return;
11573b6e0a5Matt Amdur
1158d75f374John Levon	(void) idcp->idc_tabp(mcp, flags, argc, argv);
11593b6e0a5Matt Amdur}
11603b6e0a5Matt Amdur
11617c478bdstevel@tonic-gate/*
11627c478bdstevel@tonic-gate * Call an internal dcmd directly: this code is used by module API functions
11637c478bdstevel@tonic-gate * that need to execute dcmds, and by mdb_call() above.
11647c478bdstevel@tonic-gate */
11657c478bdstevel@tonic-gateint
11667c478bdstevel@tonic-gatemdb_call_idcmd(mdb_idcmd_t *idcp, uintmax_t addr, uintmax_t count,
11677c478bdstevel@tonic-gate    uint_t flags, mdb_argvec_t *avp, mdb_addrvec_t *adp, mdb_vcb_t *vcbs)
11687c478bdstevel@tonic-gate{
11697c478bdstevel@tonic-gate	int is_exec = (strcmp(idcp->idc_name, "$<") == 0);
11707c478bdstevel@tonic-gate	mdb_arg_t *argv;
11717c478bdstevel@tonic-gate	int argc;
11727c478bdstevel@tonic-gate	uintmax_t i;
11737c478bdstevel@tonic-gate	int status;
11747c478bdstevel@tonic-gate
11757c478bdstevel@tonic-gate	/*
11767c478bdstevel@tonic-gate	 * Update the values of dot and the most recent address and count
11777c478bdstevel@tonic-gate	 * to the values of our input parameters.
11787c478bdstevel@tonic-gate	 */
11797c478bdstevel@tonic-gate	mdb_nv_set_value(mdb.m_dot, addr);
11807c478bdstevel@tonic-gate	mdb.m_raddr = addr;
11817c478bdstevel@tonic-gate	mdb.m_dcount = count;
11827c478bdstevel@tonic-gate
11837c478bdstevel@tonic-gate	/*
11847c478bdstevel@tonic-gate	 * Here the adb(1) man page lies: '9' is only set to count
11857c478bdstevel@tonic-gate	 * when the command is $<, not when it's $<<.
11867c478bdstevel@tonic-gate	 */
11877c478bdstevel@tonic-gate	if (is_exec)
11887c478bdstevel@tonic-gate		mdb_nv_set_value(mdb.m_rcount, count);
11897c478bdstevel@tonic-gate
11907c478bdstevel@tonic-gate	/*
11917c478bdstevel@tonic-gate	 * We can now return if the repeat count is zero.
11927c478bdstevel@tonic-gate	 */
11937c478bdstevel@tonic-gate	if (count == 0)
11947c478bdstevel@tonic-gate		return (DCMD_OK);
11957c478bdstevel@tonic-gate
11967c478bdstevel@tonic-gate	/*
11977c478bdstevel@tonic-gate	 * To guard against bad dcmds, we avoid passing the actual argv that
11987c478bdstevel@tonic-gate	 * we will use to free argument strings directly to the dcmd.  Instead,
11997c478bdstevel@tonic-gate	 * we pass a copy that will be garbage collected automatically.
12007c478bdstevel@tonic-gate	 */
12017c478bdstevel@tonic-gate	argc = avp->a_nelems;
12027c478bdstevel@tonic-gate	argv = mdb_alloc(sizeof (mdb_arg_t) * argc, UM_SLEEP | UM_GC);
12037c478bdstevel@tonic-gate	bcopy(avp->a_data, argv, sizeof (mdb_arg_t) * argc);
12047c478bdstevel@tonic-gate
12057c478bdstevel@tonic-gate	if (mdb_addrvec_length(adp) != 0) {
12067c478bdstevel@tonic-gate		flags |= DCMD_PIPE | DCMD_LOOP | DCMD_LOOPFIRST | DCMD_ADDRSPEC;
12077c478bdstevel@tonic-gate		addr = mdb_addrvec_shift(adp);
12087c478bdstevel@tonic-gate		mdb_nv_set_value(mdb.m_dot, addr);
12097c478bdstevel@tonic-gate		mdb_vcb_propagate(vcbs);
12107c478bdstevel@tonic-gate		count = 1;
12117c478bdstevel@tonic-gate	}
12127c478bdstevel@tonic-gate
12137c478bdstevel@tonic-gate	status = dcmd_invoke(idcp, addr, flags, argc, argv, vcbs);
12147c478bdstevel@tonic-gate	if (DCMD_ABORTED(status))
12157c478bdstevel@tonic-gate		goto done;
12167c478bdstevel@tonic-gate
12177c478bdstevel@tonic-gate	/*
12187c478bdstevel@tonic-gate	 * If the command is $< and we're not receiving input from a pipe, we
12197c478bdstevel@tonic-gate	 * ignore the repeat count and just return since the macro file is now
12207c478bdstevel@tonic-gate	 * pushed on to the input stack.
12217c478bdstevel@tonic-gate	 */
12227c478bdstevel@tonic-gate	if (is_exec && mdb_addrvec_length(adp) == 0)
12237c478bdstevel@tonic-gate		goto done;
12247c478bdstevel@tonic-gate
12257c478bdstevel@tonic-gate	/*
12267c478bdstevel@tonic-gate	 * If we're going to loop, we've already executed the dcmd once,
12277c478bdstevel@tonic-gate	 * so clear the LOOPFIRST flag before proceeding.
12287c478bdstevel@tonic-gate	 */
12297c478bdstevel@tonic-gate	if (flags & DCMD_LOOP)
12307c478bdstevel@tonic-gate		flags &= ~DCMD_LOOPFIRST;
12317c478bdstevel@tonic-gate
12327c478bdstevel@tonic-gate	for (i = 1; i < count; i++) {
12337c478bdstevel@tonic-gate		addr = mdb_dot_incr(",");
12347c478bdstevel@tonic-gate		mdb_nv_set_value(mdb.m_dot, addr);
12357c478bdstevel@tonic-gate		status = dcmd_invoke(idcp, addr, flags, argc, argv, vcbs);
12367c478bdstevel@tonic-gate		if (DCMD_ABORTED(status))
12377c478bdstevel@tonic-gate			goto done;
12387c478bdstevel@tonic-gate	}
12397c478bdstevel@tonic-gate
12407c478bdstevel@tonic-gate	while (mdb_addrvec_length(adp) != 0) {
12417c478bdstevel@tonic-gate		addr = mdb_addrvec_shift(adp);
12427c478bdstevel@tonic-gate		mdb_nv_set_value(mdb.m_dot, addr);
12437c478bdstevel@tonic-gate		mdb_vcb_propagate(vcbs);
12447c478bdstevel@tonic-gate		status = dcmd_invoke(idcp, addr, flags, argc, argv, vcbs);
12457c478bdstevel@tonic-gate		if (DCMD_ABORTED(status))
12467c478bdstevel@tonic-gate			goto done;
12477c478bdstevel@tonic-gate	}
12487c478bdstevel@tonic-gatedone:
12497c478bdstevel@tonic-gate	mdb_iob_nlflush(mdb.m_out);
12507c478bdstevel@tonic-gate	return (status);
12517c478bdstevel@tonic-gate}
12527c478bdstevel@tonic-gate
12537c478bdstevel@tonic-gatevoid
12547c478bdstevel@tonic-gatemdb_intr_enable(void)
12557c478bdstevel@tonic-gate{
12567c478bdstevel@tonic-gate	ASSERT(mdb.m_intr >= 1);
12577c478bdstevel@tonic-gate	if (mdb.m_intr == 1 && mdb.m_pend != 0) {
12587c478bdstevel@tonic-gate		(void) mdb_signal_block(SIGINT);
12597c478bdstevel@tonic-gate		mdb.m_intr = mdb.m_pend = 0;
12607c478bdstevel@tonic-gate		mdb_dprintf(MDB_DBG_DSTK, "delivering pending INT\n");
12617c478bdstevel@tonic-gate		longjmp(mdb.m_frame->f_pcb, MDB_ERR_SIGINT);
12627c478bdstevel@tonic-gate	} else
12637c478bdstevel@tonic-gate		mdb.m_intr--;
12647c478bdstevel@tonic-gate}
12657c478bdstevel@tonic-gate
12667c478bdstevel@tonic-gatevoid
12677c478bdstevel@tonic-gatemdb_intr_disable(void)
12687c478bdstevel@tonic-gate{
12697c478bdstevel@tonic-gate	mdb.m_intr++;
12707c478bdstevel@tonic-gate	ASSERT(mdb.m_intr >= 1);
12717c478bdstevel@tonic-gate}
12727c478bdstevel@tonic-gate
12737c478bdstevel@tonic-gate/*
12747c478bdstevel@tonic-gate * Create an encoded string representing the internal user-modifiable
12757c478bdstevel@tonic-gate * configuration of the debugger and return a pointer to it.  The string can be
12767c478bdstevel@tonic-gate * used to initialize another instance of the debugger with the same
12777c478bdstevel@tonic-gate * configuration as this one.
12787c478bdstevel@tonic-gate */
12797c478bdstevel@tonic-gatechar *
12807c478bdstevel@tonic-gatemdb_get_config(void)
12817c478bdstevel@tonic-gate{
12827c478bdstevel@tonic-gate	size_t r, n = 0;
12837c478bdstevel@tonic-gate	char *s = NULL;
12847c478bdstevel@tonic-gate
12857c478bdstevel@tonic-gate	while ((r = mdb_snprintf(s, n,
12867c478bdstevel@tonic-gate	    "%x;%x;%x;%x;%x;%x;%lx;%x;%x;%s;%s;%s;%s;%s",
12877c478bdstevel@tonic-gate	    mdb.m_tgtflags, mdb.m_flags, mdb.m_debug, mdb.m_radix, mdb.m_nargs,
12887c478bdstevel@tonic-gate	    mdb.m_histlen, (ulong_t)mdb.m_symdist, mdb.m_execmode,
12897c478bdstevel@tonic-gate	    mdb.m_forkmode, mdb.m_root, mdb.m_termtype, mdb.m_ipathstr,
12907c478bdstevel@tonic-gate	    mdb.m_lpathstr, mdb.m_prompt)) > n) {
12917c478bdstevel@tonic-gate
12927c478bdstevel@tonic-gate		mdb_free(s, n);
12937c478bdstevel@tonic-gate		n = r + 1;
12947c478bdstevel@tonic-gate		s = mdb_alloc(r + 1, UM_SLEEP);
12957c478bdstevel@tonic-gate	}
12967c478bdstevel@tonic-gate
12977c478bdstevel@tonic-gate	return (s);
12987c478bdstevel@tonic-gate}
12997c478bdstevel@tonic-gate
13007c478bdstevel@tonic-gate/*
13017c478bdstevel@tonic-gate * Decode a configuration string created with mdb_get_config() and reset the
13027c478bdstevel@tonic-gate * appropriate parts of the global mdb_t accordingly.
13037c478bdstevel@tonic-gate */
13047c478bdstevel@tonic-gatevoid
13057c478bdstevel@tonic-gatemdb_set_config(const char *s)
13067c478bdstevel@tonic-gate{
13077c478bdstevel@tonic-gate	const char *p;
13087c478bdstevel@tonic-gate	size_t len;
13097c478bdstevel@tonic-gate
13107c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13117c478bdstevel@tonic-gate		mdb.m_tgtflags = strntoul(s, (size_t)(p - s), 16);
13127c478bdstevel@tonic-gate		s = p + 1;
13137c478bdstevel@tonic-gate	}
13147c478bdstevel@tonic-gate
13157c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13167c478bdstevel@tonic-gate		mdb.m_flags = strntoul(s, (size_t)(p - s), 16);
13177c478bdstevel@tonic-gate		mdb.m_flags &= ~(MDB_FL_LOG | MDB_FL_LATEST);
13187c478bdstevel@tonic-gate		s = p + 1;
13197c478bdstevel@tonic-gate	}
13207c478bdstevel@tonic-gate
13217c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13227c478bdstevel@tonic-gate		mdb.m_debug = strntoul(s, (size_t)(p - s), 16);
13237c478bdstevel@tonic-gate		s = p + 1;
13247c478bdstevel@tonic-gate	}
13257c478bdstevel@tonic-gate
13267c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13277c478bdstevel@tonic-gate		mdb.m_radix = (int)strntoul(s, (size_t)(p - s), 16);
13287c478bdstevel@tonic-gate		if (mdb.m_radix < 2 || mdb.m_radix > 16)
13297c478bdstevel@tonic-gate			mdb.m_radix = MDB_DEF_RADIX;
13307c478bdstevel@tonic-gate		s = p + 1;
13317c478bdstevel@tonic-gate	}
13327c478bdstevel@tonic-gate
13337c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13347c478bdstevel@tonic-gate		mdb.m_nargs = (int)strntoul(s, (size_t)(p - s), 16);
13357c478bdstevel@tonic-gate		mdb.m_nargs = MAX(mdb.m_nargs, 0);
13367c478bdstevel@tonic-gate		s = p + 1;
13377c478bdstevel@tonic-gate	}
13387c478bdstevel@tonic-gate
13397c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13407c478bdstevel@tonic-gate		mdb.m_histlen = (int)strntoul(s, (size_t)(p - s), 16);
13417c478bdstevel@tonic-gate		mdb.m_histlen = MAX(mdb.m_histlen, 1);
13427c478bdstevel@tonic-gate		s = p + 1;
13437c478bdstevel@tonic-gate	}
13447c478bdstevel@tonic-gate
13457c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13467c478bdstevel@tonic-gate		mdb.m_symdist = strntoul(s, (size_t)(p - s), 16);
13477c478bdstevel@tonic-gate		s = p + 1;
13487c478bdstevel@tonic-gate	}
13497c478bdstevel@tonic-gate
13507c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13517c478bdstevel@tonic-gate		mdb.m_execmode = (uchar_t)strntoul(s, (size_t)(p - s), 16);
13527c478bdstevel@tonic-gate		if (mdb.m_execmode > MDB_EM_FOLLOW)
13537c478bdstevel@tonic-gate			mdb.m_execmode = MDB_EM_ASK;
13547c478bdstevel@tonic-gate		s = p + 1;
13557c478bdstevel@tonic-gate	}
13567c478bdstevel@tonic-gate
13577c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13587c478bdstevel@tonic-gate		mdb.m_forkmode = (uchar_t)strntoul(s, (size_t)(p - s), 16);
13597c478bdstevel@tonic-gate		if (mdb.m_forkmode > MDB_FM_CHILD)
13607c478bdstevel@tonic-gate			mdb.m_forkmode = MDB_FM_ASK;
13617c478bdstevel@tonic-gate		s = p + 1;
13627c478bdstevel@tonic-gate	}
13637c478bdstevel@tonic-gate
13647c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13657c478bdstevel@tonic-gate		mdb.m_root = strndup(s, (size_t)(p - s));
13667c478bdstevel@tonic-gate		s = p + 1;
13677c478bdstevel@tonic-gate	}
13687c478bdstevel@tonic-gate
13697c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13707c478bdstevel@tonic-gate		mdb.m_termtype = strndup(s, (size_t)(p - s));
13717c478bdstevel@tonic-gate		s = p + 1;
13727c478bdstevel@tonic-gate	}
13737c478bdstevel@tonic-gate
13747c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13757c478bdstevel@tonic-gate		size_t len = MIN(sizeof (mdb.m_ipathstr) - 1, p - s);
13768014889Surya Prakki		(void) strncpy(mdb.m_ipathstr, s, len);
13777c478bdstevel@tonic-gate		mdb.m_ipathstr[len] = '\0';
13787c478bdstevel@tonic-gate		s = p + 1;
13797c478bdstevel@tonic-gate	}
13807c478bdstevel@tonic-gate
13817c478bdstevel@tonic-gate	if ((p = strchr(s, ';')) != NULL) {
13827c478bdstevel@tonic-gate		size_t len = MIN(sizeof (mdb.m_lpathstr) - 1, p - s);
13838014889Surya Prakki		(void) strncpy(mdb.m_lpathstr, s, len);
13847c478bdstevel@tonic-gate		mdb.m_lpathstr[len] = '\0';
13857c478bdstevel@tonic-gate		s = p + 1;
13867c478bdstevel@tonic-gate	}
13877c478bdstevel@tonic-gate
13887c478bdstevel@tonic-gate	p = s + strlen(s);
13897c478bdstevel@tonic-gate	len = MIN(MDB_PROMPTLEN, (size_t)(p - s));
13907c478bdstevel@tonic-gate	(void) strncpy(mdb.m_prompt, s, len);
13917c478bdstevel@tonic-gate	mdb.m_prompt[len] = '\0';
13927c478bdstevel@tonic-gate	mdb.m_promptlen = len;
13937c478bdstevel@tonic-gate}
13947c478bdstevel@tonic-gate
13957c478bdstevel@tonic-gatemdb_module_t *
13967c478bdstevel@tonic-gatemdb_get_module(void)
13977c478bdstevel@tonic-gate{
13987c478bdstevel@tonic-gate	if (mdb.m_lmod)
13997c478bdstevel@tonic-gate		return (mdb.m_lmod);
14007c478bdstevel@tonic-gate
1401843e198johnlev	if (mdb.m_frame == NULL)
1402843e198johnlev		return (NULL);
1403843e198johnlev
1404843e198johnlev	if (mdb.m_frame->f_wcbs && mdb.m_frame->f_wcbs->w_walker &&
140528e4da2Matthew Ahrens	    mdb.m_frame->f_wcbs->w_walker->iwlk_modp &&
140628e4da2Matthew Ahrens	    !mdb.m_frame->f_cbactive)
1407843e198johnlev		return (mdb.m_frame->f_wcbs->w_walker->iwlk_modp);
1408843e198johnlev
1409843e198johnlev	if (mdb.m_frame->f_cp && mdb.m_frame->f_cp->c_dcmd)
14107c478bdstevel@tonic-gate		return (mdb.m_frame->f_cp->c_dcmd->idc_modp);
14117c478bdstevel@tonic-gate
14127c478bdstevel@tonic-gate	return (NULL);
14137c478bdstevel@tonic-gate}
1414