Unleashed v1.4
[unleashed.git] / usr / src / cmd / mdb / common / kmdb / kmdb_promio.c
blobaccec88fce99bed733b22b0d3243dccd427156aa
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * PROM I/O backend
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/obpdefs.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <ctype.h>
37 #include <mdb/mdb_modapi.h>
38 #include <mdb/mdb_io_impl.h>
39 #include <kmdb/kmdb_promif.h>
40 #include <kmdb/kmdb_io.h>
41 #include <mdb/mdb_debug.h>
42 #include <mdb/mdb_err.h>
43 #include <mdb/mdb.h>
45 #define PIO_FL_TIO_READ 0x001
47 typedef struct pio_data {
48 char pio_name[MAXPATHLEN];
49 ihandle_t pio_fd;
50 uint_t pio_flags;
51 struct termios pio_ti;
52 } pio_data_t;
54 static pid_t pio_pgrp;
56 static ssize_t
57 pio_read(mdb_io_t *io, void *buf, size_t nbytes)
59 pio_data_t *pdp = io->io_data;
61 if (io->io_next == NULL)
62 return (kmdb_prom_read(buf, nbytes, &pdp->pio_ti));
64 return (IOP_READ(io->io_next, buf, nbytes));
67 static ssize_t
68 pio_write(mdb_io_t *io, const void *buf, size_t nbytes)
70 pio_data_t *pdp = io->io_data;
72 if (io->io_next == NULL)
73 return (kmdb_prom_write(buf, nbytes, &pdp->pio_ti));
75 return (IOP_WRITE(io->io_next, buf, nbytes));
78 static off64_t
79 pio_seek(mdb_io_t *io, off64_t offset, int whence)
81 if (io->io_next == NULL)
82 return (set_errno(ENOTSUP));
84 return (IOP_SEEK(io->io_next, offset, whence));
87 static int
88 pio_ctl(mdb_io_t *io, int req, void *arg)
90 pio_data_t *pdp = io->io_data;
92 if (io->io_next != NULL)
93 return (IOP_CTL(io->io_next, req, arg));
95 switch (req) {
96 case TIOCGWINSZ:
97 return (kmdb_prom_term_ctl(TIOCGWINSZ, arg));
99 case TCGETS: {
100 struct termios *ti = arg;
102 if (!(pdp->pio_flags & PIO_FL_TIO_READ)) {
103 (void) kmdb_prom_term_ctl(TCGETS, &pdp->pio_ti);
104 pdp->pio_flags |= PIO_FL_TIO_READ;
107 bcopy(&pdp->pio_ti, ti, sizeof (struct termios));
109 mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: gets: i: 0%o o: 0%o c: "
110 "0%o l: 0%o\n", ti->c_iflag, ti->c_oflag, ti->c_cflag,
111 ti->c_lflag);
112 return (0);
115 case TCSETSW: {
116 struct termios *ti = arg;
118 mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: setsw: i: 0%o o: 0%o c: "
119 "0%o l: 0%o\n", ti->c_iflag, ti->c_oflag, ti->c_cflag,
120 ti->c_lflag);
122 bcopy(ti, &pdp->pio_ti, sizeof (struct termios));
124 return (0);
127 case TIOCSPGRP:
128 pio_pgrp = *(pid_t *)arg;
129 mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: spgrp: %ld\n",
130 (long)pio_pgrp);
131 return (0);
133 case TIOCGPGRP:
134 mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: gpgrp: %ld\n",
135 (long)pio_pgrp);
136 *(pid_t *)arg = pio_pgrp;
137 return (0);
139 case MDB_IOC_CTTY:
140 mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: ignoring MDB_IOC_CTTY\n");
141 return (0);
143 case MDB_IOC_GETFD:
144 return (set_errno(ENOTSUP));
146 default:
147 warn("Unknown ioctl %d\n", req);
148 return (set_errno(EINVAL));
152 void
153 pio_close(mdb_io_t *io)
155 pio_data_t *pdp = io->io_data;
157 mdb_free(pdp, sizeof (pio_data_t));
160 static const char *
161 pio_name(mdb_io_t *io)
163 pio_data_t *pdp = io->io_data;
165 if (io->io_next == NULL)
166 return (pdp->pio_name);
168 return (IOP_NAME(io->io_next));
171 static const mdb_io_ops_t promio_ops = {
172 pio_read,
173 pio_write,
174 pio_seek,
175 pio_ctl,
176 pio_close,
177 pio_name,
178 no_io_link,
179 no_io_unlink,
180 no_io_setattr,
181 no_io_suspend,
182 no_io_resume
185 mdb_io_t *
186 kmdb_promio_create(char *name)
188 mdb_io_t *io;
189 pio_data_t *pdp;
190 ihandle_t hdl = kmdb_prom_get_handle(name);
192 if (hdl == -1)
193 return (NULL);
195 io = mdb_zalloc(sizeof (mdb_io_t), UM_SLEEP);
196 pdp = mdb_zalloc(sizeof (pio_data_t), UM_SLEEP);
198 (void) strlcpy(pdp->pio_name, name, MAXPATHLEN);
199 pdp->pio_fd = hdl;
201 pdp->pio_ti.c_lflag |= ECHO;
203 io->io_data = pdp;
204 io->io_ops = &promio_ops;
206 return (io);
209 char
210 kmdb_getchar(void)
212 char c;
214 while (IOP_READ(mdb.m_term, &c, 1) != 1)
215 continue;
216 if (isprint(c) && c != '\n')
217 mdb_iob_printf(mdb.m_out, "%c", c);
218 mdb_iob_printf(mdb.m_out, "\n");
220 return (c);