Add preliminary support for the x86emu backend.
[v86d.git] / libs / x86emu / debug.c
blob6fd7f11cfdc0ad628d59fb584d1ec563158bbb61
1 /****************************************************************************
3 * Realmode X86 Emulator Library
5 * Copyright (C) 1996-1999 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 * ========================================================================
31 * Language: ANSI C
32 * Environment: Any
33 * Developer: Kendall Bennett
35 * Description: This file contains the code to handle debugging of the
36 * emulator.
38 ****************************************************************************/
40 #include "x86emu/x86emui.h"
41 #ifndef NO_SYS_HEADERS
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #endif
46 /*----------------------------- Implementation ----------------------------*/
48 #ifdef DEBUG
50 static void print_encoded_bytes (u16 s, u16 o);
51 static void print_decoded_instruction (void);
52 static int parse_line (char *s, int *ps, int *n);
54 /* should look something like debug's output. */
55 void X86EMU_trace_regs (void)
57 if (DEBUG_TRACE()) {
58 x86emu_dump_regs();
60 if (DEBUG_DECODE() && ! DEBUG_DECODE_NOPRINT()) {
61 printk("%04x:%04x ",M.x86.saved_cs, M.x86.saved_ip);
62 print_encoded_bytes( M.x86.saved_cs, M.x86.saved_ip);
63 print_decoded_instruction();
67 void X86EMU_trace_xregs (void)
69 if (DEBUG_TRACE()) {
70 x86emu_dump_xregs();
74 void x86emu_just_disassemble (void)
77 * This routine called if the flag DEBUG_DISASSEMBLE is set kind
78 * of a hack!
80 printk("%04x:%04x ",M.x86.saved_cs, M.x86.saved_ip);
81 print_encoded_bytes( M.x86.saved_cs, M.x86.saved_ip);
82 print_decoded_instruction();
85 static void disassemble_forward (u16 seg, u16 off, int n)
87 X86EMU_sysEnv tregs;
88 int i;
89 u8 op1;
91 * hack, hack, hack. What we do is use the exact machinery set up
92 * for execution, except that now there is an additional state
93 * flag associated with the "execution", and we are using a copy
94 * of the register struct. All the major opcodes, once fully
95 * decoded, have the following two steps: TRACE_REGS(r,m);
96 * SINGLE_STEP(r,m); which disappear if DEBUG is not defined to
97 * the preprocessor. The TRACE_REGS macro expands to:
99 * if (debug&DEBUG_DISASSEMBLE)
100 * {just_disassemble(); goto EndOfInstruction;}
101 * if (debug&DEBUG_TRACE) trace_regs(r,m);
103 * ...... and at the last line of the routine.
105 * EndOfInstruction: end_instr();
107 * Up to the point where TRACE_REG is expanded, NO modifications
108 * are done to any register EXCEPT the IP register, for fetch and
109 * decoding purposes.
111 * This was done for an entirely different reason, but makes a
112 * nice way to get the system to help debug codes.
114 tregs = M;
115 tregs.x86.R_IP = off;
116 tregs.x86.R_CS = seg;
118 /* reset the decoding buffers */
119 tregs.x86.enc_str_pos = 0;
120 tregs.x86.enc_pos = 0;
122 /* turn on the "disassemble only, no execute" flag */
123 tregs.x86.debug |= DEBUG_DISASSEMBLE_F;
125 /* DUMP NEXT n instructions to screen in straight_line fashion */
127 * This looks like the regular instruction fetch stream, except
128 * that when this occurs, each fetched opcode, upon seeing the
129 * DEBUG_DISASSEMBLE flag set, exits immediately after decoding
130 * the instruction. XXX --- CHECK THAT MEM IS NOT AFFECTED!!!
131 * Note the use of a copy of the register structure...
133 for (i=0; i<n; i++) {
134 op1 = (*sys_rdb)(((u32)M.x86.R_CS<<4) + (M.x86.R_IP++));
135 (x86emu_optab[op1])(op1);
137 /* end major hack mode. */
140 void x86emu_check_ip_access (void)
142 /* NULL as of now */
145 void x86emu_check_sp_access (void)
149 void x86emu_check_mem_access (u32 dummy)
151 /* check bounds, etc */
154 void x86emu_check_data_access (uint dummy1, uint dummy2)
156 /* check bounds, etc */
159 void x86emu_inc_decoded_inst_len (int x)
161 M.x86.enc_pos += x;
164 void x86emu_decode_printf (char *x)
166 sprintf(M.x86.decoded_buf+M.x86.enc_str_pos,"%s",x);
167 M.x86.enc_str_pos += strlen(x);
170 void x86emu_decode_printf2 (char *x, int y)
172 char temp[100];
173 sprintf(temp,x,y);
174 sprintf(M.x86.decoded_buf+M.x86.enc_str_pos,"%s",temp);
175 M.x86.enc_str_pos += strlen(temp);
178 void x86emu_end_instr (void)
180 M.x86.enc_str_pos = 0;
181 M.x86.enc_pos = 0;
184 static void print_encoded_bytes (u16 s, u16 o)
186 int i;
187 char buf1[64];
188 for (i=0; i< M.x86.enc_pos; i++) {
189 sprintf(buf1+2*i,"%02x", fetch_data_byte_abs(s,o+i));
191 printk("%-20s",buf1);
194 static void print_decoded_instruction (void)
196 printk("%s", M.x86.decoded_buf);
199 void x86emu_print_int_vect (u16 iv)
201 u16 seg,off;
203 if (iv > 256) return;
204 seg = fetch_data_word_abs(0,iv*4);
205 off = fetch_data_word_abs(0,iv*4+2);
206 printk("%04x:%04x ", seg, off);
209 void X86EMU_dump_memory (u16 seg, u16 off, u32 amt)
211 u32 start = off & 0xfffffff0;
212 u32 end = (off+16) & 0xfffffff0;
213 u32 i;
214 u32 current;
216 current = start;
217 while (end <= off + amt) {
218 printk("%04x:%04x ", seg, start);
219 for (i=start; i< off; i++)
220 printk(" ");
221 for ( ; i< end; i++)
222 printk("%02x ", fetch_data_byte_abs(seg,i));
223 printk("\n");
224 start = end;
225 end = start + 16;
229 void x86emu_single_step (void)
231 char s[1024];
232 int ps[10];
233 int ntok;
234 int cmd;
235 int done;
236 int segment;
237 int offset;
238 static int breakpoint;
239 static int noDecode = 1;
241 char *p;
243 if (DEBUG_BREAK()) {
244 if (M.x86.saved_ip != breakpoint) {
245 return;
246 } else {
247 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
248 M.x86.debug |= DEBUG_TRACE_F;
249 M.x86.debug &= ~DEBUG_BREAK_F;
250 print_decoded_instruction ();
251 X86EMU_trace_regs();
254 done=0;
255 offset = M.x86.saved_ip;
256 while (!done) {
257 printk("-");
258 p = fgets(s, 1023, stdin);
259 cmd = parse_line(s, ps, &ntok);
260 switch(cmd) {
261 case 'u':
262 disassemble_forward(M.x86.saved_cs,(u16)offset,10);
263 break;
264 case 'd':
265 if (ntok == 2) {
266 segment = M.x86.saved_cs;
267 offset = ps[1];
268 X86EMU_dump_memory(segment,(u16)offset,16);
269 offset += 16;
270 } else if (ntok == 3) {
271 segment = ps[1];
272 offset = ps[2];
273 X86EMU_dump_memory(segment,(u16)offset,16);
274 offset += 16;
275 } else {
276 segment = M.x86.saved_cs;
277 X86EMU_dump_memory(segment,(u16)offset,16);
278 offset += 16;
280 break;
281 case 'c':
282 M.x86.debug ^= DEBUG_TRACECALL_F;
283 break;
284 case 's':
285 M.x86.debug ^= DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F;
286 break;
287 case 'r':
288 X86EMU_trace_regs();
289 break;
290 case 'x':
291 X86EMU_trace_xregs();
292 break;
293 case 'g':
294 if (ntok == 2) {
295 breakpoint = ps[1];
296 if (noDecode) {
297 M.x86.debug |= DEBUG_DECODE_NOPRINT_F;
298 } else {
299 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
301 M.x86.debug &= ~DEBUG_TRACE_F;
302 M.x86.debug |= DEBUG_BREAK_F;
303 done = 1;
305 break;
306 case 'q':
307 M.x86.debug |= DEBUG_EXIT;
308 return;
309 case 'P':
310 noDecode = (noDecode)?0:1;
311 printk("Toggled decoding to %s\n",(noDecode)?"FALSE":"TRUE");
312 break;
313 case 't':
314 case 0:
315 done = 1;
316 break;
321 int X86EMU_trace_on(void)
323 return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
326 int X86EMU_trace_off(void)
328 return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F);
331 static int parse_line (char *s, int *ps, int *n)
333 int cmd;
335 *n = 0;
336 while(*s == ' ' || *s == '\t') s++;
337 ps[*n] = *s;
338 switch (*s) {
339 case '\n':
340 *n += 1;
341 return 0;
342 default:
343 cmd = *s;
344 *n += 1;
347 while (1) {
348 while (*s != ' ' && *s != '\t' && *s != '\n') s++;
350 if (*s == '\n')
351 return cmd;
353 while(*s == ' ' || *s == '\t') s++;
355 sscanf(s,"%x",&ps[*n]);
356 *n += 1;
360 #endif /* DEBUG */
362 void x86emu_dump_regs (void)
364 printk("\tAX=%04x ", M.x86.R_AX );
365 printk("BX=%04x ", M.x86.R_BX );
366 printk("CX=%04x ", M.x86.R_CX );
367 printk("DX=%04x ", M.x86.R_DX );
368 printk("SP=%04x ", M.x86.R_SP );
369 printk("BP=%04x ", M.x86.R_BP );
370 printk("SI=%04x ", M.x86.R_SI );
371 printk("DI=%04x\n", M.x86.R_DI );
372 printk("\tDS=%04x ", M.x86.R_DS );
373 printk("ES=%04x ", M.x86.R_ES );
374 printk("SS=%04x ", M.x86.R_SS );
375 printk("CS=%04x ", M.x86.R_CS );
376 printk("IP=%04x ", M.x86.R_IP );
377 if (ACCESS_FLAG(F_OF)) printk("OV "); /* CHECKED... */
378 else printk("NV ");
379 if (ACCESS_FLAG(F_DF)) printk("DN ");
380 else printk("UP ");
381 if (ACCESS_FLAG(F_IF)) printk("EI ");
382 else printk("DI ");
383 if (ACCESS_FLAG(F_SF)) printk("NG ");
384 else printk("PL ");
385 if (ACCESS_FLAG(F_ZF)) printk("ZR ");
386 else printk("NZ ");
387 if (ACCESS_FLAG(F_AF)) printk("AC ");
388 else printk("NA ");
389 if (ACCESS_FLAG(F_PF)) printk("PE ");
390 else printk("PO ");
391 if (ACCESS_FLAG(F_CF)) printk("CY ");
392 else printk("NC ");
393 printk("\n");
396 void x86emu_dump_xregs (void)
398 printk("\tEAX=%08x ", M.x86.R_EAX );
399 printk("EBX=%08x ", M.x86.R_EBX );
400 printk("ECX=%08x ", M.x86.R_ECX );
401 printk("EDX=%08x \n", M.x86.R_EDX );
402 printk("\tESP=%08x ", M.x86.R_ESP );
403 printk("EBP=%08x ", M.x86.R_EBP );
404 printk("ESI=%08x ", M.x86.R_ESI );
405 printk("EDI=%08x\n", M.x86.R_EDI );
406 printk("\tDS=%04x ", M.x86.R_DS );
407 printk("ES=%04x ", M.x86.R_ES );
408 printk("SS=%04x ", M.x86.R_SS );
409 printk("CS=%04x ", M.x86.R_CS );
410 printk("EIP=%08x\n\t", M.x86.R_EIP );
411 if (ACCESS_FLAG(F_OF)) printk("OV "); /* CHECKED... */
412 else printk("NV ");
413 if (ACCESS_FLAG(F_DF)) printk("DN ");
414 else printk("UP ");
415 if (ACCESS_FLAG(F_IF)) printk("EI ");
416 else printk("DI ");
417 if (ACCESS_FLAG(F_SF)) printk("NG ");
418 else printk("PL ");
419 if (ACCESS_FLAG(F_ZF)) printk("ZR ");
420 else printk("NZ ");
421 if (ACCESS_FLAG(F_AF)) printk("AC ");
422 else printk("NA ");
423 if (ACCESS_FLAG(F_PF)) printk("PE ");
424 else printk("PO ");
425 if (ACCESS_FLAG(F_CF)) printk("CY ");
426 else printk("NC ");
427 printk("\n");