xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_memio.c (revision 0c1b95be)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Memory I/O backend.
28  *
29  * Simple backend that has main memory as its backing store.
30  */
31 
32 #include <mdb/mdb_io_impl.h>
33 #include <mdb/mdb.h>
34 
35 typedef struct mem_data {
36 	char *md_buf;
37 	size_t md_size;
38 	offset_t md_off;
39 } mem_data_t;
40 
41 static ssize_t
memio_read(mdb_io_t * io,void * buf,size_t nbytes)42 memio_read(mdb_io_t *io, void *buf, size_t nbytes)
43 {
44 	mem_data_t *mdp = io->io_data;
45 
46 	if (io->io_next == NULL) {
47 		if (mdp->md_off + nbytes > mdp->md_size)
48 			nbytes = (mdp->md_size - mdp->md_off);
49 		bcopy(mdp->md_buf + mdp->md_off, buf, nbytes);
50 		mdp->md_off += nbytes;
51 		return (nbytes);
52 	}
53 
54 	return (IOP_READ(io->io_next, buf, nbytes));
55 }
56 
57 static off64_t
memio_seek(mdb_io_t * io,off64_t offset,int whence)58 memio_seek(mdb_io_t *io, off64_t offset, int whence)
59 {
60 	mem_data_t *mdp = io->io_data;
61 
62 	if (io->io_next == NULL) {
63 		switch (whence) {
64 		case SEEK_SET:
65 			mdp->md_off = offset;
66 			break;
67 		case SEEK_CUR:
68 			mdp->md_off += offset;
69 			break;
70 		case SEEK_END:
71 			mdp->md_off = mdp->md_size + offset;
72 			if (mdp->md_off > mdp->md_size)
73 				mdp->md_off = mdp->md_size;
74 			break;
75 		default:
76 			return (-1);
77 		}
78 
79 		return (mdp->md_off);
80 	}
81 
82 	return (IOP_SEEK(io->io_next, offset, whence));
83 }
84 
85 static const mdb_io_ops_t memio_ops = {
86 	.io_read = memio_read,
87 	.io_write = no_io_write,
88 	.io_seek = memio_seek,
89 	.io_ctl = no_io_ctl,
90 	.io_close = no_io_close,
91 	.io_name = no_io_name,
92 	.io_link = no_io_link,
93 	.io_unlink = no_io_unlink,
94 	.io_setattr = no_io_setattr,
95 	.io_suspend = no_io_suspend,
96 	.io_resume = no_io_resume,
97 };
98 
99 mdb_io_t *
mdb_memio_create(char * buf,size_t size)100 mdb_memio_create(char *buf, size_t size)
101 {
102 	mdb_io_t *io = mdb_alloc(sizeof (mdb_io_t), UM_SLEEP);
103 	mem_data_t *mdp = mdb_alloc(sizeof (mem_data_t), UM_SLEEP);
104 
105 	mdp->md_buf = buf;
106 	mdp->md_size = size;
107 	mdp->md_off = 0;
108 
109 	io->io_ops = &memio_ops;
110 	io->io_data = mdp;
111 	io->io_next = NULL;
112 	io->io_refcnt = 0;
113 
114 	return (io);
115 }
116