xref: /illumos-gate/usr/src/cmd/mdb/common/mdb/mdb_value.c (revision a48fdbef3a00fbebe91cb7211c789eff0e39c957)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2018 Joyent, Inc.
26  * Copyright 2024 Oxide Computer Company
27  */
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 (*)())(uintptr_t)mdb_tgt_notsup,	/* t_setflags */
112 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_setcontext */
113 	(void (*)())(uintptr_t)mdb_tgt_nop,	/* t_activate */
114 	(void (*)())(uintptr_t)mdb_tgt_nop,	/* t_deactivate */
115 	(void (*)())(uintptr_t)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 (*)())(uintptr_t)mdb_tgt_notsup,	/* t_uname */
121 	(int (*)())(uintptr_t)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 (*)())(uintptr_t)mdb_tgt_notsup,	/* t_vtop */
133 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_lookup_by_name */
134 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_lookup_by_addr */
135 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_symbol_iter */
136 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_mapping_iter */
137 	(int (*)())(uintptr_t)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 (*)())(uintptr_t)mdb_tgt_notsup,	/* t_status */
143 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_run */
144 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_step */
145 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_step_out */
146 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_next */
147 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_cont */
148 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_signal */
149 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_vbrkpt */
150 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_sbrkpt */
151 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_pwapt */
152 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_vwapt */
153 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_iowapt */
154 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_sysenter */
155 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_sysexit */
156 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_signal */
157 	(int (*)())(uintptr_t)mdb_tgt_null,	/* t_add_fault */
158 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_getareg */
159 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_putareg */
160 	(int (*)())(uintptr_t)mdb_tgt_nop,	/* t_stack_iter */
161 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_auxv */
162 	(int (*)())(uintptr_t)mdb_tgt_notsup,	/* t_thread_name */
163 };
164 
165 int
166 mdb_value_tgt_create(mdb_tgt_t *t, int argc, const char *argv[])
167 {
168 	mdb_value_data_t *data;
169 
170 	if (argc < 1 || argv[0] == NULL)
171 		return (set_errno(EINVAL));
172 	if (argc == 2 && argv[1] == NULL)
173 		return (set_errno(EINVAL));
174 	if (argc > 2)
175 		return (set_errno(EINVAL));
176 
177 	t->t_ops = &value_ops;
178 	data = mdb_zalloc(sizeof (mdb_value_data_t), UM_SLEEP);
179 	t->t_data = data;
180 	data->mvd_data = *((uintmax_t *)(void *)argv[0]);
181 	if (argc == 2)
182 		data->mvd_typesize = *((size_t *)(void *)argv[1]);
183 
184 	return (0);
185 }
186 
187 void
188 mdb_value_tgt_destroy(mdb_tgt_t *t)
189 {
190 	mdb_free(t->t_data, sizeof (mdb_value_data_t));
191 }
192