Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / xfree86 / x86emu / debug.c
CommitLineData
a09e091a
JB
1/****************************************************************************
2*
3* Realmode X86 Emulator Library
4*
5* Copyright (C) 1996-1999 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 <stdio.h>
42#include <string.h>
43#ifndef NO_SYS_HEADERS
44#include <stdarg.h>
45#include <stdlib.h>
46#endif
47
48/*----------------------------- Implementation ----------------------------*/
49
50#ifdef DEBUG
51
52static void print_encoded_bytes(u16 s, u16 o);
53static void print_decoded_instruction(void);
54static int parse_line(char *s, int *ps, int *n);
55
56/* should look something like debug's output. */
57void
58X86EMU_trace_regs(void)
59{
60 if (DEBUG_TRACE()) {
61 x86emu_dump_regs();
62 }
63 if (DEBUG_DECODE() && !DEBUG_DECODE_NOPRINT()) {
64 printk("%04x:%04x ", M.x86.saved_cs, M.x86.saved_ip);
65 print_encoded_bytes(M.x86.saved_cs, M.x86.saved_ip);
66 print_decoded_instruction();
67 }
68}
69
70void
71X86EMU_trace_xregs(void)
72{
73 if (DEBUG_TRACE()) {
74 x86emu_dump_xregs();
75 }
76}
77
78void
79x86emu_just_disassemble(void)
80{
81 /*
82 * This routine called if the flag DEBUG_DISASSEMBLE is set kind
83 * of a hack!
84 */
85 printk("%04x:%04x ", M.x86.saved_cs, M.x86.saved_ip);
86 print_encoded_bytes(M.x86.saved_cs, M.x86.saved_ip);
87 print_decoded_instruction();
88}
89
90static void
91disassemble_forward(u16 seg, u16 off, int n)
92{
93 X86EMU_sysEnv tregs;
94 int i;
95 u8 op1;
96
97 /*
98 * hack, hack, hack. What we do is use the exact machinery set up
99 * for execution, except that now there is an additional state
100 * flag associated with the "execution", and we are using a copy
101 * of the register struct. All the major opcodes, once fully
102 * decoded, have the following two steps: TRACE_REGS(r,m);
103 * SINGLE_STEP(r,m); which disappear if DEBUG is not defined to
104 * the preprocessor. The TRACE_REGS macro expands to:
105 *
106 * if (debug&DEBUG_DISASSEMBLE)
107 * {just_disassemble(); goto EndOfInstruction;}
108 * if (debug&DEBUG_TRACE) trace_regs(r,m);
109 *
110 * ...... and at the last line of the routine.
111 *
112 * EndOfInstruction: end_instr();
113 *
114 * Up to the point where TRACE_REG is expanded, NO modifications
115 * are done to any register EXCEPT the IP register, for fetch and
116 * decoding purposes.
117 *
118 * This was done for an entirely different reason, but makes a
119 * nice way to get the system to help debug codes.
120 */
121 tregs = M;
122 tregs.x86.R_IP = off;
123 tregs.x86.R_CS = seg;
124
125 /* reset the decoding buffers */
126 tregs.x86.enc_str_pos = 0;
127 tregs.x86.enc_pos = 0;
128
129 /* turn on the "disassemble only, no execute" flag */
130 tregs.x86.debug |= DEBUG_DISASSEMBLE_F;
131
132 /* DUMP NEXT n instructions to screen in straight_line fashion */
133 /*
134 * This looks like the regular instruction fetch stream, except
135 * that when this occurs, each fetched opcode, upon seeing the
136 * DEBUG_DISASSEMBLE flag set, exits immediately after decoding
137 * the instruction. XXX --- CHECK THAT MEM IS NOT AFFECTED!!!
138 * Note the use of a copy of the register structure...
139 */
140 for (i = 0; i < n; i++) {
141 op1 = (*sys_rdb) (((u32) M.x86.R_CS << 4) + (M.x86.R_IP++));
142 (x86emu_optab[op1]) (op1);
143 }
144 /* end major hack mode. */
145}
146
147void
148x86emu_check_ip_access(void)
149{
150 /* NULL as of now */
151}
152
153void
154x86emu_check_sp_access(void)
155{
156}
157
158void
159x86emu_check_mem_access(u32 dummy)
160{
161 /* check bounds, etc */
162}
163
164void
165x86emu_check_data_access(uint dummy1, uint dummy2)
166{
167 /* check bounds, etc */
168}
169
170void
171x86emu_inc_decoded_inst_len(int x)
172{
173 M.x86.enc_pos += x;
174}
175
176void
177x86emu_decode_printf(const char *x)
178{
179 sprintf(M.x86.decoded_buf + M.x86.enc_str_pos, "%s", x);
180 M.x86.enc_str_pos += strlen(x);
181}
182
183void
184x86emu_decode_printf2(const char *x, int y)
185{
186 char temp[100];
187
188 snprintf(temp, sizeof(temp), x, y);
189 sprintf(M.x86.decoded_buf + M.x86.enc_str_pos, "%s", temp);
190 M.x86.enc_str_pos += strlen(temp);
191}
192
193void
194x86emu_end_instr(void)
195{
196 M.x86.enc_str_pos = 0;
197 M.x86.enc_pos = 0;
198}
199
200static void
201print_encoded_bytes(u16 s, u16 o)
202{
203 int i;
204 char buf1[64];
205
206 for (i = 0; i < M.x86.enc_pos; i++) {
207 sprintf(buf1 + 2 * i, "%02x", fetch_data_byte_abs(s, o + i));
208 }
209 printk("%-20s", buf1);
210}
211
212static void
213print_decoded_instruction(void)
214{
215 printk("%s", M.x86.decoded_buf);
216}
217
218void
219x86emu_print_int_vect(u16 iv)
220{
221 u16 seg, off;
222
223 if (iv > 256)
224 return;
225 seg = fetch_data_word_abs(0, iv * 4);
226 off = fetch_data_word_abs(0, iv * 4 + 2);
227 printk("%04x:%04x ", seg, off);
228}
229
230void
231X86EMU_dump_memory(u16 seg, u16 off, u32 amt)
232{
233 u32 start = off & 0xfffffff0;
234 u32 end = (off + 16) & 0xfffffff0;
235 u32 i;
236 u32 current;
237
238 current = start;
239 while (end <= off + amt) {
240 printk("%04x:%04x ", seg, start);
241 for (i = start; i < off; i++)
242 printk(" ");
243 for (; i < end; i++)
244 printk("%02x ", fetch_data_byte_abs(seg, i));
245 printk("\n");
246 start = end;
247 end = start + 16;
248 }
249}
250
251void
252x86emu_single_step(void)
253{
254 char s[1024];
255 int ps[10];
256 int ntok;
257 int cmd;
258 int done;
259 int segment;
260 int offset;
261 static int breakpoint;
262 static int noDecode = 1;
263
264 char *p;
265
266 if (DEBUG_BREAK()) {
267 if (M.x86.saved_ip != breakpoint) {
268 return;
269 }
270 else {
271 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
272 M.x86.debug |= DEBUG_TRACE_F;
273 M.x86.debug &= ~DEBUG_BREAK_F;
274 print_decoded_instruction();
275 X86EMU_trace_regs();
276 }
277 }
278 done = 0;
279 offset = M.x86.saved_ip;
280 while (!done) {
281 printk("-");
282 p = fgets(s, 1023, stdin);
283 cmd = parse_line(s, ps, &ntok);
284 switch (cmd) {
285 case 'u':
286 disassemble_forward(M.x86.saved_cs, (u16) offset, 10);
287 break;
288 case 'd':
289 if (ntok == 2) {
290 segment = M.x86.saved_cs;
291 offset = ps[1];
292 X86EMU_dump_memory(segment, (u16) offset, 16);
293 offset += 16;
294 }
295 else if (ntok == 3) {
296 segment = ps[1];
297 offset = ps[2];
298 X86EMU_dump_memory(segment, (u16) offset, 16);
299 offset += 16;
300 }
301 else {
302 segment = M.x86.saved_cs;
303 X86EMU_dump_memory(segment, (u16) offset, 16);
304 offset += 16;
305 }
306 break;
307 case 'c':
308 M.x86.debug ^= DEBUG_TRACECALL_F;
309 break;
310 case 's':
311 M.x86.debug ^= DEBUG_SVC_F | DEBUG_SYS_F | DEBUG_SYSINT_F;
312 break;
313 case 'r':
314 X86EMU_trace_regs();
315 break;
316 case 'x':
317 X86EMU_trace_xregs();
318 break;
319 case 'g':
320 if (ntok == 2) {
321 breakpoint = ps[1];
322 if (noDecode) {
323 M.x86.debug |= DEBUG_DECODE_NOPRINT_F;
324 }
325 else {
326 M.x86.debug &= ~DEBUG_DECODE_NOPRINT_F;
327 }
328 M.x86.debug &= ~DEBUG_TRACE_F;
329 M.x86.debug |= DEBUG_BREAK_F;
330 done = 1;
331 }
332 break;
333 case 'q':
334 M.x86.debug |= DEBUG_EXIT;
335 return;
336 case 'P':
337 noDecode = (noDecode) ? 0 : 1;
338 printk("Toggled decoding to %s\n", (noDecode) ? "FALSE" : "TRUE");
339 break;
340 case 't':
341 case 0:
342 done = 1;
343 break;
344 }
345 }
346}
347
348int
349X86EMU_trace_on(void)
350{
351 return M.x86.debug |= DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F;
352}
353
354int
355X86EMU_trace_off(void)
356{
357 return M.x86.debug &= ~(DEBUG_STEP_F | DEBUG_DECODE_F | DEBUG_TRACE_F);
358}
359
360static int
361parse_line(char *s, int *ps, int *n)
362{
363 int cmd;
364
365 *n = 0;
366 while (*s == ' ' || *s == '\t')
367 s++;
368 ps[*n] = *s;
369 switch (*s) {
370 case '\n':
371 *n += 1;
372 return 0;
373 default:
374 cmd = *s;
375 *n += 1;
376 }
377
378 while (1) {
379 while (*s != ' ' && *s != '\t' && *s != '\n')
380 s++;
381
382 if (*s == '\n')
383 return cmd;
384
385 while (*s == ' ' || *s == '\t')
386 s++;
387
388 sscanf(s, "%x", &ps[*n]);
389 *n += 1;
390 }
391}
392
393#endif /* DEBUG */
394
395void
396x86emu_dump_regs(void)
397{
398 printk("\tAX=%04x ", M.x86.R_AX);
399 printk("BX=%04x ", M.x86.R_BX);
400 printk("CX=%04x ", M.x86.R_CX);
401 printk("DX=%04x ", M.x86.R_DX);
402 printk("SP=%04x ", M.x86.R_SP);
403 printk("BP=%04x ", M.x86.R_BP);
404 printk("SI=%04x ", M.x86.R_SI);
405 printk("DI=%04x\n", M.x86.R_DI);
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("IP=%04x ", M.x86.R_IP);
411 if (ACCESS_FLAG(F_OF))
412 printk("OV "); /* CHECKED... */
413 else
414 printk("NV ");
415 if (ACCESS_FLAG(F_DF))
416 printk("DN ");
417 else
418 printk("UP ");
419 if (ACCESS_FLAG(F_IF))
420 printk("EI ");
421 else
422 printk("DI ");
423 if (ACCESS_FLAG(F_SF))
424 printk("NG ");
425 else
426 printk("PL ");
427 if (ACCESS_FLAG(F_ZF))
428 printk("ZR ");
429 else
430 printk("NZ ");
431 if (ACCESS_FLAG(F_AF))
432 printk("AC ");
433 else
434 printk("NA ");
435 if (ACCESS_FLAG(F_PF))
436 printk("PE ");
437 else
438 printk("PO ");
439 if (ACCESS_FLAG(F_CF))
440 printk("CY ");
441 else
442 printk("NC ");
443 printk("\n");
444}
445
446void
447x86emu_dump_xregs(void)
448{
449 printk("\tEAX=%08x ", M.x86.R_EAX);
450 printk("EBX=%08x ", M.x86.R_EBX);
451 printk("ECX=%08x ", M.x86.R_ECX);
452 printk("EDX=%08x \n", M.x86.R_EDX);
453 printk("\tESP=%08x ", M.x86.R_ESP);
454 printk("EBP=%08x ", M.x86.R_EBP);
455 printk("ESI=%08x ", M.x86.R_ESI);
456 printk("EDI=%08x\n", M.x86.R_EDI);
457 printk("\tDS=%04x ", M.x86.R_DS);
458 printk("ES=%04x ", M.x86.R_ES);
459 printk("SS=%04x ", M.x86.R_SS);
460 printk("CS=%04x ", M.x86.R_CS);
461 printk("EIP=%08x\n\t", M.x86.R_EIP);
462 if (ACCESS_FLAG(F_OF))
463 printk("OV "); /* CHECKED... */
464 else
465 printk("NV ");
466 if (ACCESS_FLAG(F_DF))
467 printk("DN ");
468 else
469 printk("UP ");
470 if (ACCESS_FLAG(F_IF))
471 printk("EI ");
472 else
473 printk("DI ");
474 if (ACCESS_FLAG(F_SF))
475 printk("NG ");
476 else
477 printk("PL ");
478 if (ACCESS_FLAG(F_ZF))
479 printk("ZR ");
480 else
481 printk("NZ ");
482 if (ACCESS_FLAG(F_AF))
483 printk("AC ");
484 else
485 printk("NA ");
486 if (ACCESS_FLAG(F_PF))
487 printk("PE ");
488 else
489 printk("PO ");
490 if (ACCESS_FLAG(F_CF))
491 printk("CY ");
492 else
493 printk("NC ");
494 printk("\n");
495}