First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / x86emu / debug.c
blob5eda908051d5897ae824058ab2fa17ca62b50aa5
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 #include <stdio.h>
42 #include <string.h>
43 #ifndef NO_SYS_HEADERS
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #endif
48 /*----------------------------- Implementation ----------------------------*/
50 #ifdef DEBUG
52 static void print_encoded_bytes (u16 s, u16 o);
53 static void print_decoded_instruction (void);
54 static int parse_line (char *s, int *ps, int *n);
56 /* should look something like debug's output. */
57 void X86EMU_trace_regs (void)
59 if (DEBUG_TRACE()) {
60 x86emu_dump_regs();
62 if (DEBUG_DECODE() && ! DEBUG_DECODE_NOPRINT()) {
63 printk("%04x:%04x ",M.x86.saved_cs, M.x86.saved_ip);
64 print_encoded_bytes( M.x86.saved_cs, M.x86.saved_ip);
65 print_decoded_instruction();
69 void X86EMU_trace_xregs (void)
71 if (DEBUG_TRACE()) {
72 x86emu_dump_xregs();
76 void x86emu_just_disassemble (void)
79 * This routine called if the flag DEBUG_DISASSEMBLE is set kind
80 * of a hack!
82 printk("%04x:%04x ",M.x86.saved_cs, M.x86.saved_ip);
83 print_encoded_bytes( M.x86.saved_cs, M.x86.saved_ip);
84 print_decoded_instruction();
87 static void disassemble_forward (u16 seg, u16 off, int n)
89 X86EMU_sysEnv tregs;
90 int i;
91 u8 op1;
93 * hack, hack, hack. What we do is use the exact machinery set up
94 * for execution, except that now there is an additional state
95 * flag associated with the "execution", and we are using a copy
96 * of the register struct. All the major opcodes, once fully
97 * decoded, have the following two steps: TRACE_REGS(r,m);
98 * SINGLE_STEP(r,m); which disappear if DEBUG is not defined to
99 * the preprocessor. The TRACE_REGS macro expands to:
101 * if (debug&DEBUG_DISASSEMBLE)
102 * {just_disassemble(); goto EndOfInstruction;}
103 * if (debug&DEBUG_TRACE) trace_regs(r,m);
105 * ...... and at the last line of the routine.
107 * EndOfInstruction: end_instr();
109 * Up to the point where TRACE_REG is expanded, NO modifications
110 * are done to any register EXCEPT the IP register, for fetch and
111 * decoding purposes.
113 * This was done for an entirely different reason, but makes a
114 * nice way to get the system to help debug codes.
116 tregs = M;
117 tregs.x86.R_IP = off;
118 tregs.x86.R_CS = seg;
120 /* reset the decoding buffers */
121 tregs.x86.enc_str_pos = 0;
122 tregs.x86.enc_pos = 0;
124 /* turn on the "disassemble only, no execute" flag */
125 tregs.x86.debug |= DEBUG_DISASSEMBLE_F;
127 /* DUMP NEXT n instructions to screen in straight_line fashion */
129 * This looks like the regular instruction fetch stream, except
130 * that when this occurs, each fetched opcode, upon seeing the
131 * DEBUG_DISASSEMBLE flag set, exits immediately after decoding
132 * the instruction. XXX --- CHECK THAT MEM IS NOT AFFECTED!!!
133 * Note the use of a copy of the register structure...
135 for (i=0; i<n; i++) {
136 op1 = (*sys_rdb)(((u32)M.x86.R_CS<<4) + (M.x86.R_IP++));
137 (x86emu_optab[op1])(op1);
139 /* end major hack mode. */
142 void x86emu_check_ip_access (void)
144 /* NULL as of now */
147 void x86emu_check_sp_access (void)
151 void x86emu_check_mem_access (u32 dummy)
153 /* check bounds, etc */
156 void x86emu_check_data_access (uint dummy1, uint dummy2)
158 /* check bounds, etc */
161 void x86emu_inc_decoded_inst_len (int x)
163 M.x86.enc_pos += x;
166 void x86emu_decode_printf (char *x)
168 sprintf(M.x86.decoded_buf+M.x86.enc_str_pos,"%s",x);
169 M.x86.enc_str_pos += strlen(x);
172 void x86emu_decode_printf2 (char *x, int y)
174 char temp[100];
175 sprintf(temp,x,y);
176 sprintf(M.x86.decoded_buf+M.x86.enc_str_pos,"%s",temp);
177 M.x86.enc_str_pos += strlen(temp);
180 void x86emu_end_instr (void)
182 M.x86.enc_str_pos = 0;
183 M.x86.enc_pos = 0;
186 static void print_encoded_bytes (u16 s, u16 o)
188 int i;
189 char buf1[64];
190 for (i=0; i< M.x86.enc_pos; i++) {
191 sprintf(buf1+2*i,"%02x", fetch_data_byte_abs(s,o+i));
193 printk("%-20s",buf1);
196 static void print_decoded_instruction (void)
198 printk("%s", M.x86.decoded_buf);
201 void x86emu_print_int_vect (u16 iv)
203 u16 seg,off;
205 if (iv > 256) return;
206 seg = fetch_data_word_abs(0,iv*4);
207 off = fetch_data_word_abs(0,iv*4+2);
208 printk("%04x:%04x ", seg, off);
211 void X86EMU_dump_memory (u16 seg, u16 off, u32 amt)
213 u32 start = off & 0xfffffff0;
214 u32 end = (off+16) & 0xfffffff0;
215 u32 i;
216 u32 current;
218 current = start;
219 while (end <= off + amt) {
220 printk("%04x:%04x ", seg, start);
221 for (i=start; i< off; i++)
222 printk(" ");
223 for ( ; i< end; i++)
224 printk("%02x ", fetch_data_byte_abs(seg,i));
225 printk("\n");
226 start = end;
227 end = start + 16;
231 void x86emu_single_step (void)
233 char s[1024];
234 int ps[10];
235 int ntok;
236 int cmd;
237 int done;
238 int segment;
239 int offset;
240 static int breakpoint;
241 static int noDecode = 1;
243 char *p;
245 if (DEBUG_BREAK()) {
246 if (M.x86.saved_ip != breakpoint) {
247 return;
248 } else {
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 ();
253 X86EMU_trace_regs();
256 done=0;
257 offset = M.x86.saved_ip;
258 while (!done) {
259 printk("-");
260 p = fgets(s, 1023, stdin);
261 cmd = parse_line(s, ps, &ntok);
262 switch(cmd) {
263 case 'u':
264 disassemble_forward(M.x86.saved_cs,(u16)offset,10);
265 break;
266 case 'd':
267 if (ntok == 2) {
268 segment = M.x86.saved_cs;
269 offset = ps[1];
270 X86EMU_dump_memory(segment,(u16)offset,16);
271 offset += 16;
272 } else if (ntok == 3) {
273 segment = ps[1];
274 offset = ps[2];
275 X86EMU_dump_memory(segment,(u16)offset,16);
276 offset += 16;
277 } else {
278 segment = M.x86.saved_cs;
279 X86EMU_dump_memory(segment,(u16)offset,16);
280 offset += 16;
282 break;
283 case 'c':
284 M.x86.debug ^= DEBUG_TRACECALL_F;
285 break;
286 case 's':
287 M.x86.debug ^= DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F;
288 break;
289 case 'r':
290 X86EMU_trace_regs();
291 break;
292 case 'x':
293 X86EMU_trace_xregs();
294 break;
295 case 'g':
296 if (ntok == 2) {
297 breakpoint = ps[1];
298 if (noDecode) {
299 M.x86.debug |= DEBUG_DECODE_NOPRINT_F;
300 } else {
301 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
303 M.x86.debug &= ~DEBUG_TRACE_F;
304 M.x86.debug |= DEBUG_BREAK_F;
305 done = 1;
307 break;
308 case 'q':
309 M.x86.debug |= DEBUG_EXIT;
310 return;
311 case 'P':
312 noDecode = (noDecode)?0:1;
313 printk("Toggled decoding to %s\n",(noDecode)?"FALSE":"TRUE");
314 break;
315 case 't':
316 case 0:
317 done = 1;
318 break;
323 int X86EMU_trace_on(void)
325 return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
328 int X86EMU_trace_off(void)
330 return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F);
333 static int parse_line (char *s, int *ps, int *n)
335 int cmd;
337 *n = 0;
338 while(*s == ' ' || *s == '\t') s++;
339 ps[*n] = *s;
340 switch (*s) {
341 case '\n':
342 *n += 1;
343 return 0;
344 default:
345 cmd = *s;
346 *n += 1;
349 while (1) {
350 while (*s != ' ' && *s != '\t' && *s != '\n') s++;
352 if (*s == '\n')
353 return cmd;
355 while(*s == ' ' || *s == '\t') s++;
357 sscanf(s,"%x",&ps[*n]);
358 *n += 1;
362 #endif /* DEBUG */
364 void x86emu_dump_regs (void)
366 printk("\tAX=%04x ", M.x86.R_AX );
367 printk("BX=%04x ", M.x86.R_BX );
368 printk("CX=%04x ", M.x86.R_CX );
369 printk("DX=%04x ", M.x86.R_DX );
370 printk("SP=%04x ", M.x86.R_SP );
371 printk("BP=%04x ", M.x86.R_BP );
372 printk("SI=%04x ", M.x86.R_SI );
373 printk("DI=%04x\n", M.x86.R_DI );
374 printk("\tDS=%04x ", M.x86.R_DS );
375 printk("ES=%04x ", M.x86.R_ES );
376 printk("SS=%04x ", M.x86.R_SS );
377 printk("CS=%04x ", M.x86.R_CS );
378 printk("IP=%04x ", M.x86.R_IP );
379 if (ACCESS_FLAG(F_OF)) printk("OV "); /* CHECKED... */
380 else printk("NV ");
381 if (ACCESS_FLAG(F_DF)) printk("DN ");
382 else printk("UP ");
383 if (ACCESS_FLAG(F_IF)) printk("EI ");
384 else printk("DI ");
385 if (ACCESS_FLAG(F_SF)) printk("NG ");
386 else printk("PL ");
387 if (ACCESS_FLAG(F_ZF)) printk("ZR ");
388 else printk("NZ ");
389 if (ACCESS_FLAG(F_AF)) printk("AC ");
390 else printk("NA ");
391 if (ACCESS_FLAG(F_PF)) printk("PE ");
392 else printk("PO ");
393 if (ACCESS_FLAG(F_CF)) printk("CY ");
394 else printk("NC ");
395 printk("\n");
398 void x86emu_dump_xregs (void)
400 printk("\tEAX=%08x ", M.x86.R_EAX );
401 printk("EBX=%08x ", M.x86.R_EBX );
402 printk("ECX=%08x ", M.x86.R_ECX );
403 printk("EDX=%08x \n", M.x86.R_EDX );
404 printk("\tESP=%08x ", M.x86.R_ESP );
405 printk("EBP=%08x ", M.x86.R_EBP );
406 printk("ESI=%08x ", M.x86.R_ESI );
407 printk("EDI=%08x\n", M.x86.R_EDI );
408 printk("\tDS=%04x ", M.x86.R_DS );
409 printk("ES=%04x ", M.x86.R_ES );
410 printk("SS=%04x ", M.x86.R_SS );
411 printk("CS=%04x ", M.x86.R_CS );
412 printk("EIP=%08x\n\t", M.x86.R_EIP );
413 if (ACCESS_FLAG(F_OF)) printk("OV "); /* CHECKED... */
414 else printk("NV ");
415 if (ACCESS_FLAG(F_DF)) printk("DN ");
416 else printk("UP ");
417 if (ACCESS_FLAG(F_IF)) printk("EI ");
418 else printk("DI ");
419 if (ACCESS_FLAG(F_SF)) printk("NG ");
420 else printk("PL ");
421 if (ACCESS_FLAG(F_ZF)) printk("ZR ");
422 else printk("NZ ");
423 if (ACCESS_FLAG(F_AF)) printk("AC ");
424 else printk("NA ");
425 if (ACCESS_FLAG(F_PF)) printk("PE ");
426 else printk("PO ");
427 if (ACCESS_FLAG(F_CF)) printk("CY ");
428 else printk("NC ");
429 printk("\n");