1 /****************************************************************************
2 *
3 * Realmode X86 Emulator Library
4 *
5 * Copyright (C) 1991-2004 SciTech Software, Inc.
6 * Copyright (C) David Mosberger-Tang
7 * Copyright (C) 1999 Egbert Eich
8 *
9 * ========================================================================
10 *
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation for any purpose is hereby granted without fee,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation, and that the name of the authors not be used
16 * in advertising or publicity pertaining to distribution of the software
17 * without specific, written prior permission. The authors makes no
18 * representations about the suitability of this software for any purpose.
19 * It is provided "as is" without express or implied warranty.
20 *
21 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
22 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
23 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
25 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27 * PERFORMANCE OF THIS SOFTWARE.
28 *
29 * ========================================================================
30 *
31 * Language: ANSI C
32 * Environment: Any
33 * Developer: Kendall Bennett
34 *
35 * Description: This file contains the code to handle debugging of the
36 * emulator.
37 *
38 ****************************************************************************/
39
40 #include "x86emu/x86emui.h"
41 #include <stdarg.h>
42
43 #define stdin 0
44 /*----------------------------- Implementation ----------------------------*/
45 #ifdef DEBUG
46
47 static void print_encoded_bytes (u16 s, u16 o);
48 static void print_decoded_instruction (void);
49 static int parse_line (char *s, int *ps, int *n);
50
51 /* should look something like debug's output. */
X86EMU_trace_regs(void)52 void X86EMU_trace_regs (void)
53 {
54 if (DEBUG_TRACE()) {
55 x86emu_dump_regs();
56 }
57 if (DEBUG_DECODE() && ! DEBUG_DECODE_NOPRINT()) {
58 printk("%04x:%04x ",M.x86.saved_cs, M.x86.saved_ip);
59 print_encoded_bytes( M.x86.saved_cs, M.x86.saved_ip);
60 print_decoded_instruction();
61 }
62 }
63
X86EMU_trace_xregs(void)64 void X86EMU_trace_xregs (void)
65 {
66 if (DEBUG_TRACE()) {
67 x86emu_dump_xregs();
68 }
69 }
70
x86emu_just_disassemble(void)71 void x86emu_just_disassemble (void)
72 {
73 /*
74 * This routine called if the flag DEBUG_DISASSEMBLE is set kind
75 * of a hack!
76 */
77 printk("%04x:%04x ",M.x86.saved_cs, M.x86.saved_ip);
78 print_encoded_bytes( M.x86.saved_cs, M.x86.saved_ip);
79 print_decoded_instruction();
80 }
81
disassemble_forward(u16 seg,u16 off,int n)82 static void disassemble_forward (u16 seg, u16 off, int n)
83 {
84 X86EMU_sysEnv tregs;
85 int i;
86 u8 op1;
87 /*
88 * hack, hack, hack. What we do is use the exact machinery set up
89 * for execution, except that now there is an additional state
90 * flag associated with the "execution", and we are using a copy
91 * of the register struct. All the major opcodes, once fully
92 * decoded, have the following two steps: TRACE_REGS(r,m);
93 * SINGLE_STEP(r,m); which disappear if DEBUG is not defined to
94 * the preprocessor. The TRACE_REGS macro expands to:
95 *
96 * if (debug&DEBUG_DISASSEMBLE)
97 * {just_disassemble(); goto EndOfInstruction;}
98 * if (debug&DEBUG_TRACE) trace_regs(r,m);
99 *
100 * ...... and at the last line of the routine.
101 *
102 * EndOfInstruction: end_instr();
103 *
104 * Up to the point where TRACE_REG is expanded, NO modifications
105 * are done to any register EXCEPT the IP register, for fetch and
106 * decoding purposes.
107 *
108 * This was done for an entirely different reason, but makes a
109 * nice way to get the system to help debug codes.
110 */
111 tregs = M;
112 tregs.x86.R_IP = off;
113 tregs.x86.R_CS = seg;
114
115 /* reset the decoding buffers */
116 tregs.x86.enc_str_pos = 0;
117 tregs.x86.enc_pos = 0;
118
119 /* turn on the "disassemble only, no execute" flag */
120 tregs.x86.debug |= DEBUG_DISASSEMBLE_F;
121
122 /* DUMP NEXT n instructions to screen in straight_line fashion */
123 /*
124 * This looks like the regular instruction fetch stream, except
125 * that when this occurs, each fetched opcode, upon seeing the
126 * DEBUG_DISASSEMBLE flag set, exits immediately after decoding
127 * the instruction. XXX --- CHECK THAT MEM IS NOT AFFECTED!!!
128 * Note the use of a copy of the register structure...
129 */
130 for (i=0; i<n; i++) {
131 op1 = (*sys_rdb)(((u32)M.x86.R_CS<<4) + (M.x86.R_IP++));
132 (x86emu_optab[op1])(op1);
133 }
134 /* end major hack mode. */
135 }
136
x86emu_check_ip_access(void)137 void x86emu_check_ip_access (void)
138 {
139 /* NULL as of now */
140 }
141
x86emu_check_sp_access(void)142 void x86emu_check_sp_access (void)
143 {
144 }
145
x86emu_check_mem_access(u32 dummy)146 void x86emu_check_mem_access (u32 dummy)
147 {
148 /* check bounds, etc */
149 }
150
x86emu_check_data_access(uint dummy1,uint dummy2)151 void x86emu_check_data_access (uint dummy1, uint dummy2)
152 {
153 /* check bounds, etc */
154 }
155
x86emu_inc_decoded_inst_len(int x)156 void x86emu_inc_decoded_inst_len (int x)
157 {
158 M.x86.enc_pos += x;
159 }
160
x86emu_decode_printf(char * x)161 void x86emu_decode_printf (char *x)
162 {
163 sprintf(M.x86.decoded_buf+M.x86.enc_str_pos,"%s",x);
164 M.x86.enc_str_pos += strlen(x);
165 }
166
x86emu_decode_printf2(char * x,int y)167 void x86emu_decode_printf2 (char *x, int y)
168 {
169 char temp[100];
170 sprintf(temp,x,y);
171 sprintf(M.x86.decoded_buf+M.x86.enc_str_pos,"%s",temp);
172 M.x86.enc_str_pos += strlen(temp);
173 }
174
x86emu_end_instr(void)175 void x86emu_end_instr (void)
176 {
177 M.x86.enc_str_pos = 0;
178 M.x86.enc_pos = 0;
179 }
180
print_encoded_bytes(u16 s,u16 o)181 static void print_encoded_bytes (u16 s, u16 o)
182 {
183 int i;
184 char buf1[64];
185 for (i=0; i< M.x86.enc_pos; i++) {
186 sprintf(buf1+2*i,"%02x", fetch_data_byte_abs(s,o+i));
187 }
188 printk("%-20s",buf1);
189 }
190
print_decoded_instruction(void)191 static void print_decoded_instruction (void)
192 {
193 printk("%s", M.x86.decoded_buf);
194 }
195
x86emu_print_int_vect(u16 iv)196 void x86emu_print_int_vect (u16 iv)
197 {
198 u16 seg,off;
199
200 if (iv > 256) return;
201 seg = fetch_data_word_abs(0,iv*4);
202 off = fetch_data_word_abs(0,iv*4+2);
203 printk("%04x:%04x ", seg, off);
204 }
205
X86EMU_dump_memory(u16 seg,u16 off,u32 amt)206 void X86EMU_dump_memory (u16 seg, u16 off, u32 amt)
207 {
208 u32 start = off & 0xfffffff0;
209 u32 end = (off+16) & 0xfffffff0;
210 u32 i;
211 u32 current;
212
213 current = start;
214 while (end <= off + amt) {
215 printk("%04x:%04x ", seg, start);
216 for (i=start; i< off; i++)
217 printk(" ");
218 for ( ; i< end; i++)
219 printk("%02x ", fetch_data_byte_abs(seg,i));
220 printk("\n");
221 start = end;
222 end = start + 16;
223 }
224 }
225
x86emu_single_step(void)226 void x86emu_single_step (void)
227 {
228 char s[1024];
229 int ps[10];
230 int ntok;
231 int cmd;
232 int done;
233 int segment;
234 int offset;
235 static int breakpoint;
236 static int noDecode = 1;
237
238 char *p;
239
240 if (DEBUG_BREAK()) {
241 if (M.x86.saved_ip != breakpoint) {
242 return;
243 } else {
244 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
245 M.x86.debug |= DEBUG_TRACE_F;
246 M.x86.debug &= ~DEBUG_BREAK_F;
247 print_decoded_instruction ();
248 X86EMU_trace_regs();
249 }
250 }
251 done=0;
252 offset = M.x86.saved_ip;
253 while (!done) {
254 printk("-");
255 //p = fgets(s, 1023, stdin);
256 cons_gets(s);
257 cmd = parse_line(s, ps, &ntok);
258 switch(cmd) {
259 case 'u':
260 disassemble_forward(M.x86.saved_cs,(u16)offset,10);
261 break;
262 case 'd':
263 if (ntok == 2) {
264 segment = M.x86.saved_cs;
265 offset = ps[1];
266 X86EMU_dump_memory(segment,(u16)offset,16);
267 offset += 16;
268 } else if (ntok == 3) {
269 segment = ps[1];
270 offset = ps[2];
271 X86EMU_dump_memory(segment,(u16)offset,16);
272 offset += 16;
273 } else {
274 segment = M.x86.saved_cs;
275 X86EMU_dump_memory(segment,(u16)offset,16);
276 offset += 16;
277 }
278 break;
279 case 'c':
280 M.x86.debug ^= DEBUG_TRACECALL_F;
281 break;
282 case 's':
283 M.x86.debug ^= DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F;
284 break;
285 case 'r':
286 X86EMU_trace_regs();
287 break;
288 case 'x':
289 X86EMU_trace_xregs();
290 break;
291 case 'g':
292 if (ntok == 2) {
293 breakpoint = ps[1];
294 if (noDecode) {
295 M.x86.debug |= DEBUG_DECODE_NOPRINT_F;
296 } else {
297 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
298 }
299 M.x86.debug &= ~DEBUG_TRACE_F;
300 M.x86.debug |= DEBUG_BREAK_F;
301 done = 1;
302 }
303 break;
304 case 'q':
305 M.x86.debug |= DEBUG_EXIT;
306 return;
307 case 'P':
308 noDecode = (noDecode)?0:1;
309 printk("Toggled decoding to %s\n",(noDecode)?"FALSE":"TRUE");
310 break;
311 case 't':
312 case 0:
313 done = 1;
314 break;
315 }
316 }
317 }
318
X86EMU_trace_on(void)319 int X86EMU_trace_on(void)
320 {
321 return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
322 }
323
X86EMU_trace_off(void)324 int X86EMU_trace_off(void)
325 {
326 return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F);
327 }
328
parse_line(char * s,int * ps,int * n)329 static int parse_line (char *s, int *ps, int *n)
330 {
331 int cmd;
332
333 *n = 0;
334 while(*s == ' ' || *s == '\t') s++;
335 ps[*n] = *s;
336 switch (*s) {
337 case '\n':
338 *n += 1;
339 return 0;
340 default:
341 cmd = *s;
342 *n += 1;
343 }
344
345 while (1) {
346 while (*s != ' ' && *s != '\t' && *s != '\n') s++;
347
348 if (*s == '\n')
349 return cmd;
350
351 while(*s == ' ' || *s == '\t') s++;
352
353 ps[*n]=atoi(s);
354 //sscanf(s,"%x",&ps[*n]);
355 *n += 1;
356 }
357 }
358
359 #endif /* DEBUG */
360
x86emu_dump_regs(void)361 void x86emu_dump_regs (void)
362 {
363 printk("\tAX=%04x ", M.x86.R_AX );
364 printk("BX=%04x ", M.x86.R_BX );
365 printk("CX=%04x ", M.x86.R_CX );
366 printk("DX=%04x ", M.x86.R_DX );
367 printk("SP=%04x ", M.x86.R_SP );
368 printk("BP=%04x ", M.x86.R_BP );
369 printk("SI=%04x ", M.x86.R_SI );
370 printk("DI=%04x\n", M.x86.R_DI );
371 printk("\tDS=%04x ", M.x86.R_DS );
372 printk("ES=%04x ", M.x86.R_ES );
373 printk("SS=%04x ", M.x86.R_SS );
374 printk("CS=%04x ", M.x86.R_CS );
375 printk("IP=%04x ", M.x86.R_IP );
376 if (ACCESS_FLAG(F_OF)) printk("OV "); /* CHECKED... */
377 else printk("NV ");
378 if (ACCESS_FLAG(F_DF)) printk("DN ");
379 else printk("UP ");
380 if (ACCESS_FLAG(F_IF)) printk("EI ");
381 else printk("DI ");
382 if (ACCESS_FLAG(F_SF)) printk("NG ");
383 else printk("PL ");
384 if (ACCESS_FLAG(F_ZF)) printk("ZR ");
385 else printk("NZ ");
386 if (ACCESS_FLAG(F_AF)) printk("AC ");
387 else printk("NA ");
388 if (ACCESS_FLAG(F_PF)) printk("PE ");
389 else printk("PO ");
390 if (ACCESS_FLAG(F_CF)) printk("CY ");
391 else printk("NC ");
392 printk("\n");
393 }
394
x86emu_dump_xregs(void)395 void x86emu_dump_xregs (void)
396 {
397 printk("\tEAX=%08x ", M.x86.R_EAX );
398 printk("EBX=%08x ", M.x86.R_EBX );
399 printk("ECX=%08x ", M.x86.R_ECX );
400 printk("EDX=%08x \n", M.x86.R_EDX );
401 printk("\tESP=%08x ", M.x86.R_ESP );
402 printk("EBP=%08x ", M.x86.R_EBP );
403 printk("ESI=%08x ", M.x86.R_ESI );
404 printk("EDI=%08x\n", M.x86.R_EDI );
405 printk("\tDS=%04x ", M.x86.R_DS );
406 printk("ES=%04x ", M.x86.R_ES );
407 printk("SS=%04x ", M.x86.R_SS );
408 printk("CS=%04x ", M.x86.R_CS );
409 printk("EIP=%08x\n\t", M.x86.R_EIP );
410 if (ACCESS_FLAG(F_OF)) printk("OV "); /* CHECKED... */
411 else printk("NV ");
412 if (ACCESS_FLAG(F_DF)) printk("DN ");
413 else printk("UP ");
414 if (ACCESS_FLAG(F_IF)) printk("EI ");
415 else printk("DI ");
416 if (ACCESS_FLAG(F_SF)) printk("NG ");
417 else printk("PL ");
418 if (ACCESS_FLAG(F_ZF)) printk("ZR ");
419 else printk("NZ ");
420 if (ACCESS_FLAG(F_AF)) printk("AC ");
421 else printk("NA ");
422 if (ACCESS_FLAG(F_PF)) printk("PE ");
423 else printk("PO ");
424 if (ACCESS_FLAG(F_CF)) printk("CY ");
425 else printk("NC ");
426 printk("\n");
427 }
428