xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_frame.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8a6a72fdSaf  * Common Development and Distribution License (the "License").
6*8a6a72fdSaf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*8a6a72fdSaf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Utility routines to manage debugger frames and commands.  A debugger frame
287c478bd9Sstevel@tonic-gate  * is used by each invocation of mdb_run() (the main parsing loop) to manage
297c478bd9Sstevel@tonic-gate  * its state.  Refer to the comments in mdb.c for more information on frames.
307c478bd9Sstevel@tonic-gate  * Each frame has a list of commands (that is, a dcmd, argument list, and
317c478bd9Sstevel@tonic-gate  * optional address list) that represent a pipeline after it has been parsed.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
357c478bd9Sstevel@tonic-gate #include <mdb/mdb_frame.h>
367c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
377c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
387c478bd9Sstevel@tonic-gate #include <mdb/mdb_lex.h>
397c478bd9Sstevel@tonic-gate #include <mdb/mdb_io.h>
407c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate mdb_cmd_t *
mdb_cmd_create(mdb_idcmd_t * idcp,mdb_argvec_t * argv)437c478bd9Sstevel@tonic-gate mdb_cmd_create(mdb_idcmd_t *idcp, mdb_argvec_t *argv)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	mdb_cmd_t *cp = mdb_zalloc(sizeof (mdb_cmd_t), UM_NOSLEEP);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
487c478bd9Sstevel@tonic-gate 		warn("failed to allocate memory for command");
497c478bd9Sstevel@tonic-gate 		longjmp(mdb.m_frame->f_pcb, MDB_ERR_NOMEM);
507c478bd9Sstevel@tonic-gate 	}
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	mdb_list_append(&mdb.m_frame->f_cmds, cp);
537c478bd9Sstevel@tonic-gate 	mdb_argvec_copy(&cp->c_argv, argv);
547c478bd9Sstevel@tonic-gate 	mdb_argvec_zero(argv);
557c478bd9Sstevel@tonic-gate 	cp->c_dcmd = idcp;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	return (cp);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate void
mdb_cmd_destroy(mdb_cmd_t * cp)617c478bd9Sstevel@tonic-gate mdb_cmd_destroy(mdb_cmd_t *cp)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	mdb_addrvec_destroy(&cp->c_addrv);
647c478bd9Sstevel@tonic-gate 	mdb_argvec_destroy(&cp->c_argv);
657c478bd9Sstevel@tonic-gate 	mdb_vcb_purge(cp->c_vcbs);
667c478bd9Sstevel@tonic-gate 	mdb_free(cp, sizeof (mdb_cmd_t));
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate void
mdb_cmd_reset(mdb_cmd_t * cp)707c478bd9Sstevel@tonic-gate mdb_cmd_reset(mdb_cmd_t *cp)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	mdb_addrvec_destroy(&cp->c_addrv);
737c478bd9Sstevel@tonic-gate 	mdb_vcb_purge(cp->c_vcbs);
747c478bd9Sstevel@tonic-gate 	cp->c_vcbs = NULL;
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate void
mdb_frame_reset(mdb_frame_t * fp)787c478bd9Sstevel@tonic-gate mdb_frame_reset(mdb_frame_t *fp)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	mdb_cmd_t *cp;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	while ((cp = mdb_list_next(&fp->f_cmds)) != NULL) {
837c478bd9Sstevel@tonic-gate 		mdb_list_delete(&fp->f_cmds, cp);
847c478bd9Sstevel@tonic-gate 		mdb_cmd_destroy(cp);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 	fp->f_cp = NULL;
877c478bd9Sstevel@tonic-gate 	fp->f_pcmd = NULL;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	while (mdb_iob_stack_size(&fp->f_ostk) != 0) {
907c478bd9Sstevel@tonic-gate 		mdb_iob_destroy(mdb.m_out);
917c478bd9Sstevel@tonic-gate 		mdb.m_out = mdb_iob_stack_pop(&fp->f_ostk);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	mdb_wcb_purge(&fp->f_wcbs);
957c478bd9Sstevel@tonic-gate 	mdb_recycle(&fp->f_mblks);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate void
mdb_frame_push(mdb_frame_t * fp)997c478bd9Sstevel@tonic-gate mdb_frame_push(mdb_frame_t *fp)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	mdb_intr_disable();
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if (mdb.m_fmark == NULL)
1047c478bd9Sstevel@tonic-gate 		mdb.m_fmark = fp;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	mdb_lex_state_save(mdb.m_frame->f_lstate);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	bzero(fp, sizeof (mdb_frame_t));
1097c478bd9Sstevel@tonic-gate 	mdb_lex_state_create(fp);
1107c478bd9Sstevel@tonic-gate 	mdb_list_append(&mdb.m_flist, fp);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	fp->f_flags = mdb.m_flags & MDB_FL_VOLATILE;
1137c478bd9Sstevel@tonic-gate 	fp->f_pcmd = mdb.m_frame->f_pcmd;
1147c478bd9Sstevel@tonic-gate 	fp->f_id = mdb.m_fid++;
1157c478bd9Sstevel@tonic-gate 	mdb.m_frame->f_dot = mdb_nv_get_value(mdb.m_dot);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	mdb.m_frame = fp;
1187c478bd9Sstevel@tonic-gate 	mdb.m_depth++;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_DSTK, "push frame <%u> mark=%p in=%s out=%s\n",
1217c478bd9Sstevel@tonic-gate 	    fp->f_id, (void *)mdb.m_fmark,
1227c478bd9Sstevel@tonic-gate 	    mdb_iob_name(mdb.m_in), mdb_iob_name(mdb.m_out));
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	mdb_intr_enable();
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate void
mdb_frame_pop(mdb_frame_t * fp,int err)1287c478bd9Sstevel@tonic-gate mdb_frame_pop(mdb_frame_t *fp, int err)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	mdb_intr_disable();
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	ASSERT(mdb_iob_stack_size(&fp->f_istk) == 0);
1337c478bd9Sstevel@tonic-gate 	ASSERT(mdb_iob_stack_size(&fp->f_ostk) == 0);
1347c478bd9Sstevel@tonic-gate 	ASSERT(mdb_list_next(&fp->f_cmds) == NULL);
1357c478bd9Sstevel@tonic-gate 	ASSERT(fp->f_mblks == NULL);
1367c478bd9Sstevel@tonic-gate 	ASSERT(fp->f_wcbs == NULL);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_DSTK, "pop frame <%u> status=%s\n",
1397c478bd9Sstevel@tonic-gate 	    fp->f_id, mdb_err2str(err));
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (mdb.m_frame == fp) {
1427c478bd9Sstevel@tonic-gate 		mdb.m_flags &= ~MDB_FL_VOLATILE;
1437c478bd9Sstevel@tonic-gate 		mdb.m_flags |= fp->f_flags;
1447c478bd9Sstevel@tonic-gate 		mdb_frame_switch(mdb_list_prev(fp));
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (mdb.m_fmark == fp)
1487c478bd9Sstevel@tonic-gate 		mdb.m_fmark = NULL;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	mdb_lex_state_destroy(fp);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	mdb_list_delete(&mdb.m_flist, fp);
1537c478bd9Sstevel@tonic-gate 	ASSERT(mdb.m_depth != 0);
1547c478bd9Sstevel@tonic-gate 	mdb.m_depth--;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	mdb_intr_enable();
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate void
mdb_frame_switch(mdb_frame_t * frame)1607c478bd9Sstevel@tonic-gate mdb_frame_switch(mdb_frame_t *frame)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	mdb_lex_state_save(mdb.m_frame->f_lstate);
1637c478bd9Sstevel@tonic-gate 	mdb.m_frame->f_dot = mdb_nv_get_value(mdb.m_dot);
1647c478bd9Sstevel@tonic-gate 	mdb.m_frame = frame;
1657c478bd9Sstevel@tonic-gate 	mdb_lex_state_restore(mdb.m_frame->f_lstate);
1667c478bd9Sstevel@tonic-gate 	mdb_dprintf(MDB_DBG_DSTK, "switch to frame <%u>\n", mdb.m_frame->f_id);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	mdb_nv_set_value(mdb.m_dot, frame->f_dot);
1697c478bd9Sstevel@tonic-gate }
170*8a6a72fdSaf 
171*8a6a72fdSaf void
mdb_frame_set_pipe(mdb_frame_t * frame)172*8a6a72fdSaf mdb_frame_set_pipe(mdb_frame_t *frame)
173*8a6a72fdSaf {
174*8a6a72fdSaf 	frame->pipe = TRUE;
175*8a6a72fdSaf }
176*8a6a72fdSaf 
177*8a6a72fdSaf void
mdb_frame_clear_pipe(mdb_frame_t * frame)178*8a6a72fdSaf mdb_frame_clear_pipe(mdb_frame_t *frame)
179*8a6a72fdSaf {
180*8a6a72fdSaf 	frame->pipe = FALSE;
181*8a6a72fdSaf }
182*8a6a72fdSaf 
183*8a6a72fdSaf mdb_frame_t *
mdb_frame_pipe(void)184*8a6a72fdSaf mdb_frame_pipe(void)
185*8a6a72fdSaf {
186*8a6a72fdSaf 	mdb_frame_t *frame = mdb_list_prev(mdb.m_frame);
187*8a6a72fdSaf 
188*8a6a72fdSaf 	while (frame && frame->pipe == FALSE)
189*8a6a72fdSaf 		frame = mdb_list_prev(frame);
190*8a6a72fdSaf 
191*8a6a72fdSaf 	return (frame);
192*8a6a72fdSaf }
193