xref: /titanic_44/usr/src/cmd/mdb/common/mdb/mdb_value.c (revision 7b9b3bf3fd4f7bfad91fce91e3e9fba62ac85c77)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7b9b3bf3Sedp  * Common Development and Distribution License (the "License").
6*7b9b3bf3Sedp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*7b9b3bf3Sedp  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Immediate Value Target
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * The immediate value target is used when the '=' verb is used to
327c478bd9Sstevel@tonic-gate  * format an immediate value, or with ::print -i.  The target is
337c478bd9Sstevel@tonic-gate  * initialized with a specific value, and then simply copies bytes from
347c478bd9Sstevel@tonic-gate  * this integer in its read routine.  Two notes:
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * (1) the address parameter of value_read is treated as an offset into
377c478bd9Sstevel@tonic-gate  * the immediate value.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * (2) on big-endian systems, we need to be careful about the place we
407c478bd9Sstevel@tonic-gate  * copy data from. If the caller specified a typesize in the argv array
417c478bd9Sstevel@tonic-gate  * we use that for offsetting, otherwise we use the read size.
427c478bd9Sstevel@tonic-gate  * If the user didn't specify the typesize, then 'addr' is ignored,
437c478bd9Sstevel@tonic-gate  * and all reads are at an offset of 0 into the immediate value. This
447c478bd9Sstevel@tonic-gate  * covers both the usage of ::print -i, and the semantics of adb
457c478bd9Sstevel@tonic-gate  * commands like "0x1234=X", which should produce 0x1234 as a result;
467c478bd9Sstevel@tonic-gate  * the adb model is for it to act like a cast down to the smaller
477c478bd9Sstevel@tonic-gate  * integer type; this is handled as mentioned.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <mdb/mdb_target_impl.h>
517c478bd9Sstevel@tonic-gate #include <mdb/mdb_types.h>
527c478bd9Sstevel@tonic-gate #include <mdb/mdb_conf.h>
537c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
567c478bd9Sstevel@tonic-gate #include <strings.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate void mdb_value_tgt_destroy(mdb_tgt_t *);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate typedef struct mdb_value_data {
617c478bd9Sstevel@tonic-gate 	uintmax_t mvd_data;
627c478bd9Sstevel@tonic-gate 	size_t mvd_typesize;
637c478bd9Sstevel@tonic-gate } mdb_value_data_t;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static ssize_t
value_read(mdb_tgt_t * t,void * dst,size_t nbytes,uintptr_t addr)667c478bd9Sstevel@tonic-gate value_read(mdb_tgt_t *t, void *dst, size_t nbytes, uintptr_t addr)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	mdb_value_data_t *data = t->t_data;
697c478bd9Sstevel@tonic-gate 	size_t size = data->mvd_typesize;
707c478bd9Sstevel@tonic-gate 	const char *src = (const char *)&data->mvd_data;
717c478bd9Sstevel@tonic-gate 	size_t off;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * If no output size was specified, use the current read size.
757c478bd9Sstevel@tonic-gate 	 * In this case, "addr" is not an offset into the mvd_data,
767c478bd9Sstevel@tonic-gate 	 * so we ignore it.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	if (size == 0) {
797c478bd9Sstevel@tonic-gate 		size = nbytes;
807c478bd9Sstevel@tonic-gate 		addr = 0;
817c478bd9Sstevel@tonic-gate 	} else {
827c478bd9Sstevel@tonic-gate 		nbytes = MIN(size, nbytes);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	off = addr;
867c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
877c478bd9Sstevel@tonic-gate 	if (sizeof (uintmax_t) >= size)
887c478bd9Sstevel@tonic-gate 		off += sizeof (uintmax_t) - size;
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (off > sizeof (uintmax_t))
927c478bd9Sstevel@tonic-gate 		return (0);
937c478bd9Sstevel@tonic-gate 	if (off + nbytes > sizeof (uintmax_t))
947c478bd9Sstevel@tonic-gate 		nbytes = sizeof (uintmax_t) - off;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (nbytes != 0)
977c478bd9Sstevel@tonic-gate 		bcopy(src + off, dst, nbytes);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	return (nbytes);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1037c478bd9Sstevel@tonic-gate static ssize_t
value_write(mdb_tgt_t * t,const void * buf,size_t nbytes,uintptr_t addr)1047c478bd9Sstevel@tonic-gate value_write(mdb_tgt_t *t, const void *buf, size_t nbytes, uintptr_t addr)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	return (nbytes); /* We allow writes to silently fail */
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static const mdb_tgt_ops_t value_ops = {
1107c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_setflags */
1117c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_setcontext */
1127c478bd9Sstevel@tonic-gate 	(void (*)()) mdb_tgt_nop,		/* t_activate */
1137c478bd9Sstevel@tonic-gate 	(void (*)()) mdb_tgt_nop,		/* t_deactivate */
1147c478bd9Sstevel@tonic-gate 	(void (*)()) mdb_tgt_nop,		/* t_periodic */
1157c478bd9Sstevel@tonic-gate 	mdb_value_tgt_destroy,			/* t_destroy */
1167c478bd9Sstevel@tonic-gate 	(const char *(*)()) mdb_tgt_null,	/* t_name */
1177c478bd9Sstevel@tonic-gate 	(const char *(*)()) mdb_conf_isa,	/* t_isa */
1187c478bd9Sstevel@tonic-gate 	(const char *(*)()) mdb_conf_platform,	/* t_platform */
1197c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_uname */
1207c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_dmodel */
1217c478bd9Sstevel@tonic-gate 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_aread */
1227c478bd9Sstevel@tonic-gate 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_awrite */
1237c478bd9Sstevel@tonic-gate 	value_read,				/* t_vread */
1247c478bd9Sstevel@tonic-gate 	value_write,				/* t_vwrite */
1257c478bd9Sstevel@tonic-gate 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_pread */
1267c478bd9Sstevel@tonic-gate 	(ssize_t (*)()) mdb_tgt_notsup,		/* t_pwrite */
1277c478bd9Sstevel@tonic-gate 	value_read,				/* t_fread */
1287c478bd9Sstevel@tonic-gate 	value_write,				/* t_fwrite */
1297c478bd9Sstevel@tonic-gate 	value_read,				/* t_ioread */
1307c478bd9Sstevel@tonic-gate 	value_write,				/* t_iowrite */
1317c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_vtop */
1327c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_lookup_by_name */
1337c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_lookup_by_addr */
1347c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_symbol_iter */
1357c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_mapping_iter */
1367c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_object_iter */
1377c478bd9Sstevel@tonic-gate 	(const mdb_map_t *(*)()) mdb_tgt_null,	/* t_addr_to_map */
1387c478bd9Sstevel@tonic-gate 	(const mdb_map_t *(*)()) mdb_tgt_null,	/* t_name_to_map */
1397c478bd9Sstevel@tonic-gate 	(struct ctf_file *(*)()) mdb_tgt_null,	/* t_addr_to_ctf */
1407c478bd9Sstevel@tonic-gate 	(struct ctf_file *(*)()) mdb_tgt_null,	/* t_name_to_ctf */
1417c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_status */
1427c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_run */
1437c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_step */
1447c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_step_out */
1457c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_step_branch */
1467c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_next */
1477c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_cont */
1487c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_signal */
1497c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_vbrkpt */
1507c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_sbrkpt */
1517c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_pwapt */
1527c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_vwapt */
1537c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_iowapt */
1547c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_sysenter */
1557c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_sysexit */
1567c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_signal */
1577c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_null,		/* t_add_fault */
1587c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_getareg */
1597c478bd9Sstevel@tonic-gate 	(int (*)()) mdb_tgt_notsup,		/* t_putareg */
160*7b9b3bf3Sedp 	(int (*)()) mdb_tgt_nop,		/* t_stack_iter */
161*7b9b3bf3Sedp 	(int (*)()) mdb_tgt_notsup		/* t_auxv */
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate int
mdb_value_tgt_create(mdb_tgt_t * t,int argc,const char * argv[])1657c478bd9Sstevel@tonic-gate mdb_value_tgt_create(mdb_tgt_t *t, int argc, const char *argv[])
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	mdb_value_data_t *data;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (argc < 1 || argv[0] == NULL)
1707c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1717c478bd9Sstevel@tonic-gate 	if (argc == 2 && argv[1] == NULL)
1727c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1737c478bd9Sstevel@tonic-gate 	if (argc > 2)
1747c478bd9Sstevel@tonic-gate 		return (set_errno(EINVAL));
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	t->t_ops = &value_ops;
1777c478bd9Sstevel@tonic-gate 	data = mdb_zalloc(sizeof (mdb_value_data_t), UM_SLEEP);
1787c478bd9Sstevel@tonic-gate 	t->t_data = data;
1797c478bd9Sstevel@tonic-gate 	data->mvd_data = *((uintmax_t *)(void *)argv[0]);
1807c478bd9Sstevel@tonic-gate 	if (argc == 2)
1817c478bd9Sstevel@tonic-gate 		data->mvd_typesize = *((size_t *)(void *)argv[1]);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return (0);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate void
mdb_value_tgt_destroy(mdb_tgt_t * t)1877c478bd9Sstevel@tonic-gate mdb_value_tgt_destroy(mdb_tgt_t *t)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	mdb_free(t->t_data, sizeof (mdb_value_data_t));
1907c478bd9Sstevel@tonic-gate }
191