1 /****************************************************************************
3 * Realmode X86 Emulator Library
5 * Copyright (C) 1991-2004 SciTech Software, Inc.
6 * Copyright (C) David Mosberger-Tang
7 * Copyright (C) 1999 Egbert Eich
9 * ========================================================================
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.
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.
29 * ========================================================================
33 * Developer: Kendall Bennett
35 * Description: This file contains the code to handle debugging of the
38 ****************************************************************************/
41 // #include <stdarg.h>
43 /*----------------------------- Implementation ----------------------------*/
47 static void print_encoded_bytes (u16 s, u16 o);
48 static void print_decoded_instruction (void);
49 int parse_line (char *s, int *ps, int *n);
51 /* should look something like debug's output. */
52 void X86EMU_trace_regs (void)
55 if (M.x86.mode & (SYSMODE_PREFIX_DATA | SYSMODE_PREFIX_ADDR)) {
61 if (DEBUG_DECODE() && ! DEBUG_DECODE_NOPRINT()) {
62 printk("%04x:%04x ",M.x86.saved_cs, M.x86.saved_ip);
63 print_encoded_bytes( M.x86.saved_cs, M.x86.saved_ip);
64 print_decoded_instruction();
68 void X86EMU_trace_xregs (void)
75 void x86emu_just_disassemble (void)
78 * This routine called if the flag DEBUG_DISASSEMBLE is set kind
81 printk("%04x:%04x ",M.x86.saved_cs, M.x86.saved_ip);
82 print_encoded_bytes( M.x86.saved_cs, M.x86.saved_ip);
83 print_decoded_instruction();
86 void disassemble_forward (u16 seg, u16 off, int n)
92 * hack, hack, hack. What we do is use the exact machinery set up
93 * for execution, except that now there is an additional state
94 * flag associated with the "execution", and we are using a copy
95 * of the register struct. All the major opcodes, once fully
96 * decoded, have the following two steps: TRACE_REGS(r,m);
97 * SINGLE_STEP(r,m); which disappear if DEBUG is not defined to
98 * the preprocessor. The TRACE_REGS macro expands to:
100 * if (debug&DEBUG_DISASSEMBLE)
101 * {just_disassemble(); goto EndOfInstruction;}
102 * if (debug&DEBUG_TRACE) trace_regs(r,m);
104 * ...... and at the last line of the routine.
106 * EndOfInstruction: end_instr();
108 * Up to the point where TRACE_REG is expanded, NO modifications
109 * are done to any register EXCEPT the IP register, for fetch and
112 * This was done for an entirely different reason, but makes a
113 * nice way to get the system to help debug codes.
116 tregs.x86.R_IP = off;
117 tregs.x86.R_CS = seg;
119 /* reset the decoding buffers */
120 tregs.x86.enc_str_pos = 0;
121 tregs.x86.enc_pos = 0;
123 /* turn on the "disassemble only, no execute" flag */
124 tregs.x86.debug |= DEBUG_DISASSEMBLE_F;
126 /* DUMP NEXT n instructions to screen in straight_line fashion */
128 * This looks like the regular instruction fetch stream, except
129 * that when this occurs, each fetched opcode, upon seeing the
130 * DEBUG_DISASSEMBLE flag set, exits immediately after decoding
131 * the instruction. XXX --- CHECK THAT MEM IS NOT AFFECTED!!!
132 * Note the use of a copy of the register structure...
134 for (i=0; i<n; i++) {
135 op1 = (*sys_rdb)(((u32)M.x86.R_CS<<4) + (M.x86.R_IP++));
136 (x86emu_optab[op1])(op1);
138 /* end major hack mode. */
141 void x86emu_check_ip_access (void)
146 void x86emu_check_sp_access (void)
150 void x86emu_check_mem_access (u32 dummy)
152 /* check bounds, etc */
155 void x86emu_check_data_access (uint dummy1, uint dummy2)
157 /* check bounds, etc */
160 void x86emu_inc_decoded_inst_len (int x)
165 void x86emu_decode_printf (char *x)
167 sprintf(M.x86.decoded_buf+M.x86.enc_str_pos,"%s",x);
168 M.x86.enc_str_pos += strlen(x);
171 void x86emu_decode_printf2 (char *x, int y)
175 sprintf(M.x86.decoded_buf+M.x86.enc_str_pos,"%s",temp);
176 M.x86.enc_str_pos += strlen(temp);
179 void x86emu_end_instr (void)
181 M.x86.enc_str_pos = 0;
185 static void print_encoded_bytes (u16 s, u16 o)
189 for (i=0; i< M.x86.enc_pos; i++) {
190 sprintf(buf1+2*i,"%02x", fetch_data_byte_abs(s,o+i));
192 printk("%-20s ",buf1);
195 static void print_decoded_instruction (void)
197 printk("%s", M.x86.decoded_buf);
200 void x86emu_print_int_vect (u16 iv)
204 if (iv > 256) return;
205 seg = fetch_data_word_abs(0,iv*4);
206 off = fetch_data_word_abs(0,iv*4+2);
207 printk("%04x:%04x ", seg, off);
210 void X86EMU_dump_memory (u16 seg, u16 off, u32 amt)
212 u32 start = off & 0xfffffff0;
213 u32 end = (off+16) & 0xfffffff0;
218 while (end <= off + amt) {
219 printk("%04x:%04x ", seg, start);
220 for (i=start; i< off; i++)
223 printk("%02x ", fetch_data_byte_abs(seg,i));
230 void x86emu_single_step (void)
240 static int breakpoint;
241 static int noDecode = 1;
246 if (M.x86.saved_ip != breakpoint) {
249 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
250 M.x86.debug |= DEBUG_TRACE_F;
251 M.x86.debug &= ~DEBUG_BREAK_F;
252 print_decoded_instruction ();
257 offset = M.x86.saved_ip;
260 p = fgets(s, 1023, stdin);
261 cmd = parse_line(s, ps, &ntok);
264 disassemble_forward(M.x86.saved_cs,(u16)offset,10);
268 segment = M.x86.saved_cs;
270 X86EMU_dump_memory(segment,(u16)offset,16);
272 } else if (ntok == 3) {
275 X86EMU_dump_memory(segment,(u16)offset,16);
278 segment = M.x86.saved_cs;
279 X86EMU_dump_memory(segment,(u16)offset,16);
284 M.x86.debug ^= DEBUG_TRACECALL_F;
287 M.x86.debug ^= DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F;
293 X86EMU_trace_xregs();
299 M.x86.debug |= DEBUG_DECODE_NOPRINT_F;
301 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
303 M.x86.debug &= ~DEBUG_TRACE_F;
304 M.x86.debug |= DEBUG_BREAK_F;
309 M.x86.debug |= DEBUG_EXIT;
312 noDecode = (noDecode)?0:1;
313 printk("Toggled decoding to %s\n",(noDecode)?"FALSE":"TRUE");
324 int X86EMU_trace_on(void)
326 return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
329 int X86EMU_trace_off(void)
331 return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F);
334 int parse_line (char *s, int *ps, int *n)
340 while(*s == ' ' || *s == '\t') s++;
352 while (*s != ' ' && *s != '\t' && *s != '\n') s++;
357 while(*s == ' ' || *s == '\t') s++;
359 sscanf(s,"%x",&ps[*n]);
369 void x86emu_dump_regs (void)
371 printk("\tAX=%04x ", M.x86.R_AX );
372 printk("BX=%04x ", M.x86.R_BX );
373 printk("CX=%04x ", M.x86.R_CX );
374 printk("DX=%04x ", M.x86.R_DX );
375 printk("SP=%04x ", M.x86.R_SP );
376 printk("BP=%04x ", M.x86.R_BP );
377 printk("SI=%04x ", M.x86.R_SI );
378 printk("DI=%04x\n", M.x86.R_DI );
379 printk("\tDS=%04x ", M.x86.R_DS );
380 printk("ES=%04x ", M.x86.R_ES );
381 printk("SS=%04x ", M.x86.R_SS );
382 printk("CS=%04x ", M.x86.R_CS );
383 printk("IP=%04x ", M.x86.R_IP );
384 if (ACCESS_FLAG(F_OF)) printk("OV "); /* CHECKED... */
386 if (ACCESS_FLAG(F_DF)) printk("DN ");
388 if (ACCESS_FLAG(F_IF)) printk("EI ");
390 if (ACCESS_FLAG(F_SF)) printk("NG ");
392 if (ACCESS_FLAG(F_ZF)) printk("ZR ");
394 if (ACCESS_FLAG(F_AF)) printk("AC ");
396 if (ACCESS_FLAG(F_PF)) printk("PE ");
398 if (ACCESS_FLAG(F_CF)) printk("CY ");
403 void x86emu_dump_xregs (void)
405 printk("\tEAX=%08x ", M.x86.R_EAX );
406 printk("EBX=%08x ", M.x86.R_EBX );
407 printk("ECX=%08x ", M.x86.R_ECX );
408 printk("EDX=%08x \n", M.x86.R_EDX );
409 printk("\tESP=%08x ", M.x86.R_ESP );
410 printk("EBP=%08x ", M.x86.R_EBP );
411 printk("ESI=%08x ", M.x86.R_ESI );
412 printk("EDI=%08x\n", M.x86.R_EDI );
413 printk("\tDS=%04x ", M.x86.R_DS );
414 printk("ES=%04x ", M.x86.R_ES );
415 printk("SS=%04x ", M.x86.R_SS );
416 printk("CS=%04x ", M.x86.R_CS );
417 printk("EIP=%08x\n\t", M.x86.R_EIP );
418 if (ACCESS_FLAG(F_OF)) printk("OV "); /* CHECKED... */
420 if (ACCESS_FLAG(F_DF)) printk("DN ");
422 if (ACCESS_FLAG(F_IF)) printk("EI ");
424 if (ACCESS_FLAG(F_SF)) printk("NG ");
426 if (ACCESS_FLAG(F_ZF)) printk("ZR ");
428 if (ACCESS_FLAG(F_AF)) printk("AC ");
430 if (ACCESS_FLAG(F_PF)) printk("PE ");
432 if (ACCESS_FLAG(F_CF)) printk("CY ");