xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_logio.c (revision 0c1b95bef39aa56a594619ae1fa17557c8a8b8ab)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1997-2001 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Log I/O Backend
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * This backend provides the ability to form a T in an iob's output routine.
317c478bd9Sstevel@tonic-gate  * We use this capability to provide interactive session logging.  We create
327c478bd9Sstevel@tonic-gate  * a log i/o and give it a pointer to another i/o backend representing the
337c478bd9Sstevel@tonic-gate  * log file, and then stack this on top of the existing stdio i/o backend.
347c478bd9Sstevel@tonic-gate  * As each write occurs, the log i/o writes to the log file, and also passes
357c478bd9Sstevel@tonic-gate  * the write request along to io->io_next.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <mdb/mdb_modapi.h>
397c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
407c478bd9Sstevel@tonic-gate #include <mdb/mdb_io_impl.h>
417c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static ssize_t
logio_read(mdb_io_t * io,void * buf,size_t nbytes)447c478bd9Sstevel@tonic-gate logio_read(mdb_io_t *io, void *buf, size_t nbytes)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	mdb_io_t *logio = io->io_data;
477c478bd9Sstevel@tonic-gate 	ssize_t rbytes;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if (io->io_next != NULL) {
507c478bd9Sstevel@tonic-gate 		rbytes = IOP_READ(io->io_next, buf, nbytes);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 		if (rbytes > 0) {
537c478bd9Sstevel@tonic-gate 			(void) IOP_WRITE(logio, mdb.m_prompt, mdb.m_promptlen);
547c478bd9Sstevel@tonic-gate 			(void) IOP_WRITE(logio, buf, rbytes);
557c478bd9Sstevel@tonic-gate 		}
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 		return (rbytes);
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	return (-1);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static ssize_t
logio_write(mdb_io_t * io,const void * buf,size_t nbytes)647c478bd9Sstevel@tonic-gate logio_write(mdb_io_t *io, const void *buf, size_t nbytes)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	mdb_io_t *logio = io->io_data;
677c478bd9Sstevel@tonic-gate 	ssize_t wbytes;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (io->io_next != NULL) {
707c478bd9Sstevel@tonic-gate 		wbytes = IOP_WRITE(io->io_next, buf, nbytes);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 		if (wbytes > 0)
737c478bd9Sstevel@tonic-gate 			(void) IOP_WRITE(logio, buf, wbytes);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		return (wbytes);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	return (-1);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static void
logio_close(mdb_io_t * io)827c478bd9Sstevel@tonic-gate logio_close(mdb_io_t *io)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	mdb_io_rele(io->io_data);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static const char *
logio_name(mdb_io_t * io)887c478bd9Sstevel@tonic-gate logio_name(mdb_io_t *io)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	if (io->io_next != NULL)
917c478bd9Sstevel@tonic-gate 		return (IOP_NAME(io->io_next));
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	return ("(log)");
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static const mdb_io_ops_t logio_ops = {
97*0c1b95beSRichard Lowe 	.io_read = logio_read,
98*0c1b95beSRichard Lowe 	.io_write = logio_write,
99*0c1b95beSRichard Lowe 	.io_seek = no_io_seek,
100*0c1b95beSRichard Lowe 	.io_ctl = no_io_ctl,
101*0c1b95beSRichard Lowe 	.io_close = logio_close,
102*0c1b95beSRichard Lowe 	.io_name = logio_name,
103*0c1b95beSRichard Lowe 	.io_link = no_io_link,
104*0c1b95beSRichard Lowe 	.io_unlink = no_io_unlink,
105*0c1b95beSRichard Lowe 	.io_setattr = no_io_setattr,
106*0c1b95beSRichard Lowe 	.io_suspend = no_io_suspend,
107*0c1b95beSRichard Lowe 	.io_resume = no_io_resume
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate mdb_io_t *
mdb_logio_create(mdb_io_t * logio)1117c478bd9Sstevel@tonic-gate mdb_logio_create(mdb_io_t *logio)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	mdb_io_t *io = mdb_alloc(sizeof (mdb_io_t), UM_SLEEP);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	io->io_ops = &logio_ops;
1167c478bd9Sstevel@tonic-gate 	io->io_data = mdb_io_hold(logio);
1177c478bd9Sstevel@tonic-gate 	io->io_next = NULL;
1187c478bd9Sstevel@tonic-gate 	io->io_refcnt = 0;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	return (io);
1217c478bd9Sstevel@tonic-gate }
122