xref: /titanic_41/usr/src/cmd/mdb/common/mdb/mdb_logio.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1997-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Log I/O Backend
31  *
32  * This backend provides the ability to form a T in an iob's output routine.
33  * We use this capability to provide interactive session logging.  We create
34  * a log i/o and give it a pointer to another i/o backend representing the
35  * log file, and then stack this on top of the existing stdio i/o backend.
36  * As each write occurs, the log i/o writes to the log file, and also passes
37  * the write request along to io->io_next.
38  */
39 
40 #include <mdb/mdb_modapi.h>
41 #include <mdb/mdb_err.h>
42 #include <mdb/mdb_io_impl.h>
43 #include <mdb/mdb.h>
44 
45 static ssize_t
logio_read(mdb_io_t * io,void * buf,size_t nbytes)46 logio_read(mdb_io_t *io, void *buf, size_t nbytes)
47 {
48 	mdb_io_t *logio = io->io_data;
49 	ssize_t rbytes;
50 
51 	if (io->io_next != NULL) {
52 		rbytes = IOP_READ(io->io_next, buf, nbytes);
53 
54 		if (rbytes > 0) {
55 			(void) IOP_WRITE(logio, mdb.m_prompt, mdb.m_promptlen);
56 			(void) IOP_WRITE(logio, buf, rbytes);
57 		}
58 
59 		return (rbytes);
60 	}
61 
62 	return (-1);
63 }
64 
65 static ssize_t
logio_write(mdb_io_t * io,const void * buf,size_t nbytes)66 logio_write(mdb_io_t *io, const void *buf, size_t nbytes)
67 {
68 	mdb_io_t *logio = io->io_data;
69 	ssize_t wbytes;
70 
71 	if (io->io_next != NULL) {
72 		wbytes = IOP_WRITE(io->io_next, buf, nbytes);
73 
74 		if (wbytes > 0)
75 			(void) IOP_WRITE(logio, buf, wbytes);
76 
77 		return (wbytes);
78 	}
79 
80 	return (-1);
81 }
82 
83 static void
logio_close(mdb_io_t * io)84 logio_close(mdb_io_t *io)
85 {
86 	mdb_io_rele(io->io_data);
87 }
88 
89 static const char *
logio_name(mdb_io_t * io)90 logio_name(mdb_io_t *io)
91 {
92 	if (io->io_next != NULL)
93 		return (IOP_NAME(io->io_next));
94 
95 	return ("(log)");
96 }
97 
98 static const mdb_io_ops_t logio_ops = {
99 	logio_read,
100 	logio_write,
101 	no_io_seek,
102 	no_io_ctl,
103 	logio_close,
104 	logio_name,
105 	no_io_link,
106 	no_io_unlink,
107 	no_io_setattr,
108 	no_io_suspend,
109 	no_io_resume
110 };
111 
112 mdb_io_t *
mdb_logio_create(mdb_io_t * logio)113 mdb_logio_create(mdb_io_t *logio)
114 {
115 	mdb_io_t *io = mdb_alloc(sizeof (mdb_io_t), UM_SLEEP);
116 
117 	io->io_ops = &logio_ops;
118 	io->io_data = mdb_io_hold(logio);
119 	io->io_next = NULL;
120 	io->io_refcnt = 0;
121 
122 	return (io);
123 }
124