Print this page
patch fix-lint
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdisasm/common/libdisasm.c
+++ new/usr/src/lib/libdisasm/common/libdisasm.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
26 + * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
26 27 */
27 28
28 29 #include <libdisasm.h>
29 30 #include <stdlib.h>
30 31 #ifdef DIS_STANDALONE
31 32 #include <mdb/mdb_modapi.h>
33 +#define _MDB
34 +#include <mdb/mdb_io.h>
35 +#else
36 +#include <stdio.h>
32 37 #endif
33 38
34 39 #include "libdisasm_impl.h"
35 40
36 41 static int _dis_errno;
37 42
38 43 /*
39 44 * If we're building the standalone library, then we only want to
40 45 * include support for disassembly of the native architecture.
41 46 * The regular shared library should include support for all
42 47 * architectures.
43 48 */
44 49 #if !defined(DIS_STANDALONE) || defined(__i386) || defined(__amd64)
45 50 extern dis_arch_t dis_arch_i386;
46 51 #endif
47 52 #if !defined(DIS_STANDALONE) || defined(__sparc)
48 53 extern dis_arch_t dis_arch_sparc;
49 54 #endif
50 55
51 56 static dis_arch_t *dis_archs[] = {
52 57 #if !defined(DIS_STANDALONE) || defined(__i386) || defined(__amd64)
53 58 &dis_arch_i386,
54 59 #endif
55 60 #if !defined(DIS_STANDALONE) || defined(__sparc)
56 61 &dis_arch_sparc,
57 62 #endif
58 63 NULL
59 64 };
60 65
61 66 /*
62 67 * For the standalone library, we need to link against mdb's malloc/free.
63 68 * Otherwise, use the standard malloc/free.
64 69 */
65 70 #ifdef DIS_STANDALONE
66 71 void *
67 72 dis_zalloc(size_t bytes)
68 73 {
69 74 return (mdb_zalloc(bytes, UM_SLEEP));
70 75 }
71 76
72 77 void
73 78 dis_free(void *ptr, size_t bytes)
74 79 {
75 80 mdb_free(ptr, bytes);
76 81 }
77 82 #else
78 83 void *
79 84 dis_zalloc(size_t bytes)
80 85 {
81 86 return (calloc(1, bytes));
82 87 }
83 88
84 89 /*ARGSUSED*/
85 90 void
86 91 dis_free(void *ptr, size_t bytes)
87 92 {
88 93 free(ptr);
89 94 }
90 95 #endif
91 96
92 97 int
93 98 dis_seterrno(int error)
94 99 {
95 100 _dis_errno = error;
96 101 return (-1);
97 102 }
98 103
99 104 int
100 105 dis_errno(void)
101 106 {
102 107 return (_dis_errno);
103 108 }
104 109
105 110 const char *
106 111 dis_strerror(int error)
107 112 {
108 113 switch (error) {
109 114 case E_DIS_NOMEM:
110 115 return ("out of memory");
111 116 case E_DIS_INVALFLAG:
112 117 return ("invalid flags for this architecture");
113 118 case E_DIS_UNSUPARCH:
114 119 return ("unsupported machine architecture");
115 120 default:
116 121 return ("unknown error");
117 122 }
118 123 }
119 124
120 125 void
121 126 dis_set_data(dis_handle_t *dhp, void *data)
122 127 {
123 128 dhp->dh_data = data;
124 129 }
125 130
126 131 void
127 132 dis_flags_set(dis_handle_t *dhp, int f)
128 133 {
129 134 dhp->dh_flags |= f;
130 135 }
131 136
132 137 void
133 138 dis_flags_clear(dis_handle_t *dhp, int f)
134 139 {
135 140 dhp->dh_flags &= ~f;
136 141 }
137 142
138 143 void
139 144 dis_handle_destroy(dis_handle_t *dhp)
140 145 {
141 146 dhp->dh_arch->da_handle_detach(dhp);
142 147 dis_free(dhp, sizeof (dis_handle_t));
143 148 }
144 149
145 150 dis_handle_t *
146 151 dis_handle_create(int flags, void *data, dis_lookup_f lookup_func,
147 152 dis_read_f read_func)
148 153 {
149 154 dis_handle_t *dhp;
150 155 dis_arch_t *arch = NULL;
151 156 int i;
152 157
153 158 /* Select an architecture based on flags */
154 159 for (i = 0; dis_archs[i] != NULL; i++) {
155 160 if (dis_archs[i]->da_supports_flags(flags)) {
156 161 arch = dis_archs[i];
157 162 break;
158 163 }
159 164 }
160 165 if (arch == NULL) {
161 166 (void) dis_seterrno(E_DIS_UNSUPARCH);
162 167 return (NULL);
163 168 }
164 169
165 170 if ((dhp = dis_zalloc(sizeof (dis_handle_t))) == NULL) {
166 171 (void) dis_seterrno(E_DIS_NOMEM);
167 172 return (NULL);
168 173 }
169 174 dhp->dh_arch = arch;
170 175 dhp->dh_lookup = lookup_func;
171 176 dhp->dh_read = read_func;
172 177 dhp->dh_flags = flags;
173 178 dhp->dh_data = data;
174 179
175 180 /*
176 181 * Allow the architecture-specific code to allocate
177 182 * its private data.
178 183 */
179 184 if (arch->da_handle_attach(dhp) != 0) {
180 185 dis_free(dhp, sizeof (dis_handle_t));
181 186 /* dis errno already set */
182 187 return (NULL);
183 188 }
184 189
185 190 return (dhp);
186 191 }
187 192
188 193 int
189 194 dis_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf, size_t buflen)
190 195 {
191 196 return (dhp->dh_arch->da_disassemble(dhp, addr, buf, buflen));
192 197 }
193 198
194 199 uint64_t
195 200 dis_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
196 201 {
197 202 return (dhp->dh_arch->da_previnstr(dhp, pc, n));
198 203 }
199 204
200 205 int
201 206 dis_min_instrlen(dis_handle_t *dhp)
202 207 {
203 208 return (dhp->dh_arch->da_min_instrlen(dhp));
204 209 }
205 210
206 211 int
↓ open down ↓ |
165 lines elided |
↑ open up ↑ |
207 212 dis_max_instrlen(dis_handle_t *dhp)
208 213 {
209 214 return (dhp->dh_arch->da_max_instrlen(dhp));
210 215 }
211 216
212 217 int
213 218 dis_instrlen(dis_handle_t *dhp, uint64_t pc)
214 219 {
215 220 return (dhp->dh_arch->da_instrlen(dhp, pc));
216 221 }
222 +
223 +int
224 +dis_vsnprintf(char *restrict s, size_t n, const char *restrict format,
225 + va_list args)
226 +{
227 +#ifdef DIS_STANDALONE
228 + return (mdb_iob_vsnprintf(s, n, format, args));
229 +#else
230 + return (vsnprintf(s, n, format, args));
231 +#endif
232 +}
233 +
234 +int
235 +dis_snprintf(char *restrict s, size_t n, const char *restrict format, ...)
236 +{
237 + va_list args;
238 +
239 + va_start(args, format);
240 + n = dis_vsnprintf(s, n, format, args);
241 + va_end(args);
242 +
243 + return (n);
244 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX