xref: /titanic_44/usr/src/cmd/mdb/common/mdb/mdb_value.c (revision 5151fb1220e0ceafdc172203863c73da4285c170)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Immediate Value Target
31  *
32  * The immediate value target is used when the '=' verb is used to
33  * format an immediate value, or with ::print -i.  The target is
34  * initialized with a specific value, and then simply copies bytes from
35  * this integer in its read routine.  Two notes:
36  *
37  * (1) the address parameter of value_read is treated as an offset into
38  * the immediate value.
39  *
40  * (2) on big-endian systems, we need to be careful about the place we
41  * copy data from. If the caller specified a typesize in the argv array
42  * we use that for offsetting, otherwise we use the read size.
43  * If the user didn't specify the typesize, then 'addr' is ignored,
44  * and all reads are at an offset of 0 into the immediate value. This
45  * covers both the usage of ::print -i, and the semantics of adb
46  * commands like "0x1234=X", which should produce 0x1234 as a result;
47  * the adb model is for it to act like a cast down to the smaller
48  * integer type; this is handled as mentioned.
49  */
50 
51 #include <mdb/mdb_target_impl.h>
52 #include <mdb/mdb_types.h>
53 #include <mdb/mdb_conf.h>
54 #include <mdb/mdb_err.h>
55 
56 #include <sys/isa_defs.h>
57 #include <strings.h>
58 
59 void mdb_value_tgt_destroy(mdb_tgt_t *);
60 
61 typedef struct mdb_value_data {
62 	uintmax_t mvd_data;
63 	size_t mvd_typesize;
64 } mdb_value_data_t;
65 
66 static ssize_t
67 value_read(mdb_tgt_t *t, void *dst, size_t nbytes, uintptr_t addr)
68 {
69 	mdb_value_data_t *data = t->t_data;
70 	size_t size = data->mvd_typesize;
71 	const char *src = (const char *)&data->mvd_data;
72 	size_t off;
73 
74 	/*
75 	 * If no output size was specified, use the current read size.
76 	 * In this case, "addr" is not an offset into the mvd_data,
77 	 * so we ignore it.
78 	 */
79 	if (size == 0) {
80 		size = nbytes;
81 		addr = 0;
82 	} else {
83 		nbytes = MIN(size, nbytes);
84 	}
85 
86 	off = addr;
87 #ifdef _BIG_ENDIAN
88 	if (sizeof (uintmax_t) >= size)
89 		off += sizeof (uintmax_t) - size;
90 #endif
91 
92 	if (off > sizeof (uintmax_t))
93 		return (0);
94 	if (off + nbytes > sizeof (uintmax_t))
95 		nbytes = sizeof (uintmax_t) - off;
96 
97 	if (nbytes != 0)
98 		bcopy(src + off, dst, nbytes);
99 
100 	return (nbytes);
101 }
102 
103 /*ARGSUSED*/
104 static ssize_t
105 value_write(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
106 {
107 	return (nbytes); /* We allow writes to silently fail */
108 }
109 
110 static const mdb_tgt_ops_t value_ops = {
111 	(int (*)()) mdb_tgt_notsup,		/* t_setflags */
112 	(int (*)()) mdb_tgt_notsup,		/* t_setcontext */
113 	(void (*)()) mdb_tgt_nop,		/* t_activate */
114 	(void (*)()) mdb_tgt_nop,		/* t_deactivate */
115 	(void (*)()) mdb_tgt_nop,		/* t_periodic */
116 	mdb_value_tgt_destroy,			/* t_destroy */
117 	(const char *(*)()) mdb_tgt_null,	/* t_name */
118 	(const char *(*)()) mdb_conf_isa,	/* t_isa */
119 	(const char *(*)()) mdb_conf_platform,	/* t_platform */
120 	(int (*)()) mdb_tgt_notsup,		/* t_uname */
121 	(int (*)()) mdb_tgt_notsup,		/* t_dmodel */
122 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_aread */
123 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_awrite */
124 	value_read,				/* t_vread */
125 	value_write,				/* t_vwrite */
126 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_pread */
127 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_pwrite */
128 	value_read,				/* t_fread */
129 	value_write,				/* t_fwrite */
130 	value_read,				/* t_ioread */
131 	value_write,				/* t_iowrite */
132 	(int (*)()) mdb_tgt_notsup,		/* t_vtop */
133 	(int (*)()) mdb_tgt_notsup,		/* t_lookup_by_name */
134 	(int (*)()) mdb_tgt_notsup,		/* t_lookup_by_addr */
135 	(int (*)()) mdb_tgt_notsup,		/* t_symbol_iter */
136 	(int (*)()) mdb_tgt_notsup,		/* t_mapping_iter */
137 	(int (*)()) mdb_tgt_notsup,		/* t_object_iter */
138 	(const mdb_map_t *(*)()) mdb_tgt_null,	/* t_addr_to_map */
139 	(const mdb_map_t *(*)()) mdb_tgt_null,	/* t_name_to_map */
140 	(struct ctf_file *(*)()) mdb_tgt_null,	/* t_addr_to_ctf */
141 	(struct ctf_file *(*)()) mdb_tgt_null,	/* t_name_to_ctf */
142 	(int (*)()) mdb_tgt_notsup,		/* t_status */
143 	(int (*)()) mdb_tgt_notsup,		/* t_run */
144 	(int (*)()) mdb_tgt_notsup,		/* t_step */
145 	(int (*)()) mdb_tgt_notsup,		/* t_step_out */
146 	(int (*)()) mdb_tgt_notsup,		/* t_step_branch */
147 	(int (*)()) mdb_tgt_notsup,		/* t_next */
148 	(int (*)()) mdb_tgt_notsup,		/* t_cont */
149 	(int (*)()) mdb_tgt_notsup,		/* t_signal */
150 	(int (*)()) mdb_tgt_null,		/* t_add_vbrkpt */
151 	(int (*)()) mdb_tgt_null,		/* t_add_sbrkpt */
152 	(int (*)()) mdb_tgt_null,		/* t_add_pwapt */
153 	(int (*)()) mdb_tgt_null,		/* t_add_vwapt */
154 	(int (*)()) mdb_tgt_null,		/* t_add_iowapt */
155 	(int (*)()) mdb_tgt_null,		/* t_add_sysenter */
156 	(int (*)()) mdb_tgt_null,		/* t_add_sysexit */
157 	(int (*)()) mdb_tgt_null,		/* t_add_signal */
158 	(int (*)()) mdb_tgt_null,		/* t_add_fault */
159 	(int (*)()) mdb_tgt_notsup,		/* t_getareg */
160 	(int (*)()) mdb_tgt_notsup,		/* t_putareg */
161 	(int (*)()) mdb_tgt_nop			/* t_stack_iter */
162 };
163 
164 int
165 mdb_value_tgt_create(mdb_tgt_t *t, int argc, const char *argv[])
166 {
167 	mdb_value_data_t *data;
168 
169 	if (argc < 1 || argv[0] == NULL)
170 		return (set_errno(EINVAL));
171 	if (argc == 2 && argv[1] == NULL)
172 		return (set_errno(EINVAL));
173 	if (argc > 2)
174 		return (set_errno(EINVAL));
175 
176 	t->t_ops = &value_ops;
177 	data = mdb_zalloc(sizeof (mdb_value_data_t), UM_SLEEP);
178 	t->t_data = data;
179 	data->mvd_data = *((uintmax_t *)(void *)argv[0]);
180 	if (argc == 2)
181 		data->mvd_typesize = *((size_t *)(void *)argv[1]);
182 
183 	return (0);
184 }
185 
186 void
187 mdb_value_tgt_destroy(mdb_tgt_t *t)
188 {
189 	mdb_free(t->t_data, sizeof (mdb_value_data_t));
190 }
191