xref: /netbsd-src/sys/ddb/db_access.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: db_access.c,v 1.22 2015/06/06 22:06:05 matt Exp $	*/
2 
3 /*
4  * Mach Operating System
5  * Copyright (c) 1991,1990 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  *
28  *	Author: David B. Golub, Carnegie Mellon University
29  *	Date:	7/90
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: db_access.c,v 1.22 2015/06/06 22:06:05 matt Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_kgdb.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/proc.h>
41 #include <sys/endian.h>
42 
43 #include <ddb/ddb.h>
44 
45 /*
46  * Access unaligned data items on aligned (longword)
47  * boundaries.
48  *
49  * This file is shared by ddb, kgdb and crash(8).
50  */
51 
52 #if defined(DDB) || !defined(DDB) && !defined(KGDB)
53 #define	_COMPILE_THIS
54 #endif
55 
56 #if defined(_COMPILE_THIS) || defined(KGDB) && defined(SOFTWARE_SSTEP)
57 
58 db_expr_t
59 db_get_value(db_addr_t addr, size_t size, bool is_signed)
60 {
61 	char data[sizeof(db_expr_t)] __aligned(sizeof(db_expr_t));
62 	db_expr_t value;
63 	size_t i;
64 
65 	db_read_bytes(addr, size, data);
66 
67 	value = 0;
68 #if BYTE_ORDER == LITTLE_ENDIAN
69 	for (i = size; i-- > 0;)
70 #else /* BYTE_ORDER == BIG_ENDIAN */
71 	for (i = 0; i < size; i++)
72 #endif /* BYTE_ORDER */
73 		value = (value << 8) + (data[i] & 0xFF);
74 
75 	if (size < sizeof(db_expr_t) && is_signed
76 	    && (value & ((db_expr_t)1 << (8*size - 1)))) {
77 		value |= ~(db_expr_t)0 << (8*size - 1);
78 	}
79 	return (value);
80 }
81 
82 void
83 db_put_value(db_addr_t addr, size_t size, db_expr_t value)
84 {
85 	char data[sizeof(db_expr_t)] __aligned(sizeof(db_expr_t));
86 	size_t i;
87 
88 #if BYTE_ORDER == LITTLE_ENDIAN
89 	for (i = 0; i < size; i++)
90 #else /* BYTE_ORDER == BIG_ENDIAN */
91 	for (i = size; i-- > 0;)
92 #endif /* BYTE_ORDER */
93 	{
94 		data[i] = value & 0xFF;
95 		value >>= 8;
96 	}
97 
98 	db_write_bytes(addr, size, data);
99 }
100 
101 #endif	/* _COMPILE_THIS || KGDB && SOFTWARE_SSTEP */
102 
103 #ifdef	_COMPILE_THIS
104 
105 void *
106 db_read_ptr(const char *name)
107 {
108 	db_expr_t val;
109 	void *p;
110 
111 	if (!db_value_of_name(name, &val)) {
112 		db_printf("db_read_ptr: cannot find `%s'\n", name);
113 		db_error(NULL);
114 		/* NOTREACHED */
115 	}
116 	db_read_bytes((db_addr_t)val, sizeof(p), (char *)&p);
117 	return p;
118 }
119 
120 int
121 db_read_int(const char *name)
122 {
123 	db_expr_t val;
124 	int p;
125 
126 	if (!db_value_of_name(name, &val)) {
127 		db_printf("db_read_int: cannot find `%s'\n", name);
128 		db_error(NULL);
129 		/* NOTREACHED */
130 	}
131 	db_read_bytes((db_addr_t)val, sizeof(p), (char *)&p);
132 	return p;
133 }
134 
135 #endif	/* _COMPILE_THIS */
136