Print this page
saveargs: let disasm do the lifting
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdisasm/i386/dis_i386.c
+++ new/usr/src/lib/libdisasm/i386/dis_i386.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
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
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 2007 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 -#pragma ident "%Z%%M% %I% %E% SMI"
28 -
29 27 #include <libdisasm.h>
30 28 #include <stdlib.h>
31 29 #include <stdio.h>
32 30
33 31 #include "dis_tables.h"
34 32 #include "libdisasm_impl.h"
35 33
36 34 struct dis_handle {
37 35 void *dh_data;
38 36 int dh_flags;
39 37 dis_lookup_f dh_lookup;
40 38 dis_read_f dh_read;
41 39 int dh_mode;
42 40 dis86_t dh_dis;
43 41 uint64_t dh_addr;
44 42 uint64_t dh_end;
45 43 };
46 44
47 45 /*
48 46 * Returns true if we are near the end of a function. This is a cheap hack at
49 47 * detecting NULL padding between functions. If we're within a few bytes of the
50 48 * next function, or past the start, then return true.
51 49 */
52 50 static int
53 51 check_func(void *data)
54 52 {
55 53 dis_handle_t *dhp = data;
56 54 uint64_t start;
57 55 size_t len;
58 56
59 57 if (dhp->dh_lookup(dhp->dh_data, dhp->dh_addr, NULL, 0, &start, &len)
60 58 != 0)
61 59 return (0);
62 60
63 61 if (start < dhp->dh_addr)
64 62 return (dhp->dh_addr > start + len - 0x10);
65 63
66 64 return (1);
67 65 }
68 66
69 67 static int
70 68 get_byte(void *data)
71 69 {
72 70 uchar_t byte;
73 71 dis_handle_t *dhp = data;
74 72
75 73 if (dhp->dh_read(dhp->dh_data, dhp->dh_addr, &byte, sizeof (byte)) !=
76 74 sizeof (byte))
77 75 return (-1);
78 76
79 77 dhp->dh_addr++;
80 78
81 79 return ((int)byte);
82 80 }
83 81
84 82 static int
85 83 do_lookup(void *data, uint64_t addr, char *buf, size_t buflen)
86 84 {
87 85 dis_handle_t *dhp = data;
88 86
89 87 return (dhp->dh_lookup(dhp->dh_data, addr, buf, buflen, NULL, NULL));
90 88 }
91 89
92 90 dis_handle_t *
93 91 dis_handle_create(int flags, void *data, dis_lookup_f lookup_func,
94 92 dis_read_f read_func)
95 93 {
96 94 dis_handle_t *dhp;
97 95
98 96 /*
99 97 * Validate architecture flags
100 98 */
101 99 if (flags & ~(DIS_X86_SIZE16 | DIS_X86_SIZE32 | DIS_X86_SIZE64 |
102 100 DIS_OCTAL | DIS_NOIMMSYM)) {
103 101 (void) dis_seterrno(E_DIS_INVALFLAG);
104 102 return (NULL);
105 103 }
106 104
107 105 /*
108 106 * Create and initialize the internal structure
109 107 */
110 108 if ((dhp = dis_zalloc(sizeof (struct dis_handle))) == NULL) {
111 109 (void) dis_seterrno(E_DIS_NOMEM);
112 110 return (NULL);
113 111 }
114 112
115 113 dhp->dh_lookup = lookup_func;
116 114 dhp->dh_read = read_func;
117 115 dhp->dh_flags = flags;
118 116 dhp->dh_data = data;
119 117
120 118 /*
121 119 * Initialize x86-specific architecture structure
122 120 */
123 121 if (flags & DIS_X86_SIZE16)
124 122 dhp->dh_mode = SIZE16;
125 123 else if (flags & DIS_X86_SIZE64)
126 124 dhp->dh_mode = SIZE64;
127 125 else
128 126 dhp->dh_mode = SIZE32;
129 127
130 128 if (flags & DIS_OCTAL)
131 129 dhp->dh_dis.d86_flags = DIS_F_OCTAL;
132 130
133 131 dhp->dh_dis.d86_sprintf_func = snprintf;
134 132 dhp->dh_dis.d86_get_byte = get_byte;
135 133 dhp->dh_dis.d86_sym_lookup = do_lookup;
136 134 dhp->dh_dis.d86_check_func = check_func;
137 135
138 136 dhp->dh_dis.d86_data = dhp;
139 137
140 138 return (dhp);
141 139 }
142 140
143 141 int
144 142 dis_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf, size_t buflen)
145 143 {
146 144 dhp->dh_addr = addr;
147 145
148 146 /* DIS_NOIMMSYM might not be set until now, so update */
149 147 if (dhp->dh_flags & DIS_NOIMMSYM)
150 148 dhp->dh_dis.d86_flags |= DIS_F_NOIMMSYM;
151 149 else
152 150 dhp->dh_dis.d86_flags &= ~DIS_F_NOIMMSYM;
153 151
154 152 if (dtrace_disx86(&dhp->dh_dis, dhp->dh_mode) != 0)
155 153 return (-1);
156 154
157 155 if (buf != NULL)
158 156 dtrace_disx86_str(&dhp->dh_dis, dhp->dh_mode, addr, buf,
159 157 buflen);
160 158
161 159 return (0);
162 160 }
163 161
164 162 void
165 163 dis_handle_destroy(dis_handle_t *dhp)
166 164 {
167 165 dis_free(dhp, sizeof (dis_handle_t));
168 166 }
169 167
170 168 void
171 169 dis_set_data(dis_handle_t *dhp, void *data)
172 170 {
173 171 dhp->dh_data = data;
174 172 }
175 173
176 174 void
177 175 dis_flags_set(dis_handle_t *dhp, int f)
178 176 {
179 177 dhp->dh_flags |= f;
180 178 }
181 179
182 180 void
183 181 dis_flags_clear(dis_handle_t *dhp, int f)
184 182 {
185 183 dhp->dh_flags &= ~f;
186 184 }
187 185
188 186
189 187 /* ARGSUSED */
190 188 int
191 189 dis_max_instrlen(dis_handle_t *dhp)
192 190 {
193 191 return (15);
194 192 }
195 193
196 194 #define MIN(a, b) ((a) < (b) ? (a) : (b))
197 195
198 196 /*
199 197 * Return the previous instruction. On x86, we have no choice except to
200 198 * disassemble everything from the start of the symbol, and stop when we have
201 199 * reached our instruction address. If we're not in the middle of a known
202 200 * symbol, then we return the same address to indicate failure.
203 201 */
204 202 uint64_t
205 203 dis_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
206 204 {
207 205 uint64_t *hist, addr, start;
208 206 int cur, nseen;
209 207 uint64_t res = pc;
210 208
211 209 if (n <= 0)
212 210 return (pc);
213 211
214 212 if (dhp->dh_lookup(dhp->dh_data, pc, NULL, 0, &start, NULL) != 0 ||
215 213 start == pc)
216 214 return (res);
217 215
218 216 hist = dis_zalloc(sizeof (uint64_t) * n);
219 217
220 218 for (cur = 0, nseen = 0, addr = start; addr < pc; addr = dhp->dh_addr) {
221 219 hist[cur] = addr;
222 220 cur = (cur + 1) % n;
223 221 nseen++;
224 222
225 223 /* if we cannot make forward progress, give up */
226 224 if (dis_disassemble(dhp, addr, NULL, 0) != 0)
227 225 goto done;
228 226 }
229 227
230 228 if (addr != pc) {
231 229 /*
232 230 * We scanned past %pc, but didn't find an instruction that
233 231 * started at %pc. This means that either the caller specified
234 232 * an invalid address, or we ran into something other than code
235 233 * during our scan. Virtually any combination of bytes can be
236 234 * construed as a valid Intel instruction, so any non-code bytes
237 235 * we encounter will have thrown off the scan.
↓ open down ↓ |
199 lines elided |
↑ open up ↑ |
238 236 */
239 237 goto done;
240 238 }
241 239
242 240 res = hist[(cur + n - MIN(n, nseen)) % n];
243 241
244 242 done:
245 243 dis_free(hist, sizeof (uint64_t) * n);
246 244 return (res);
247 245 }
246 +
247 +int
248 +dis_instrlen(dis_handle_t *dhp, uint64_t pc)
249 +{
250 + if (dis_disassemble(dhp, pc, NULL, 0) != 0)
251 + return (-1);
252 +
253 + return (dhp->dh_addr - pc);
254 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX