The Pedigree Project  0.1
system/kernel/machine/mach_pc/x86emu/x86emu/debug.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef __X86EMU_DEBUG_H
21 #define __X86EMU_DEBUG_H
22 
23 /*---------------------- Macros and type definitions ----------------------*/
24 
25 /* checks to be enabled for "runtime" */
26 
27 #define CHECK_IP_FETCH_F 0x1
28 #define CHECK_SP_ACCESS_F 0x2
29 #define CHECK_MEM_ACCESS_F 0x4 /*using regular linear pointer */
30 #define CHECK_DATA_ACCESS_F 0x8 /*using segment:offset*/
31 
32 #ifdef DEBUG
33 #define CHECK_IP_FETCH() (M.x86.check & CHECK_IP_FETCH_F)
34 #define CHECK_SP_ACCESS() (M.x86.check & CHECK_SP_ACCESS_F)
35 #define CHECK_MEM_ACCESS() (M.x86.check & CHECK_MEM_ACCESS_F)
36 #define CHECK_DATA_ACCESS() (M.x86.check & CHECK_DATA_ACCESS_F)
37 #else
38 #define CHECK_IP_FETCH()
39 #define CHECK_SP_ACCESS()
40 #define CHECK_MEM_ACCESS()
41 #define CHECK_DATA_ACCESS()
42 #endif
43 
44 #ifdef DEBUG
45 #define DEBUG_INSTRUMENT() (M.x86.debug & DEBUG_INSTRUMENT_F)
46 #define DEBUG_DECODE() (M.x86.debug & DEBUG_DECODE_F)
47 #define DEBUG_TRACE() (M.x86.debug & DEBUG_TRACE_F)
48 #define DEBUG_STEP() (M.x86.debug & DEBUG_STEP_F)
49 #define DEBUG_DISASSEMBLE() (M.x86.debug & DEBUG_DISASSEMBLE_F)
50 #define DEBUG_BREAK() (M.x86.debug & DEBUG_BREAK_F)
51 #define DEBUG_SVC() (M.x86.debug & DEBUG_SVC_F)
52 #define DEBUG_SAVE_IP_CS() (M.x86.debug & DEBUG_SAVE_IP_CS_F)
53 
54 #define DEBUG_FS() (M.x86.debug & DEBUG_FS_F)
55 #define DEBUG_PROC() (M.x86.debug & DEBUG_PROC_F)
56 #define DEBUG_SYSINT() (M.x86.debug & DEBUG_SYSINT_F)
57 #define DEBUG_TRACECALL() (M.x86.debug & DEBUG_TRACECALL_F)
58 #define DEBUG_TRACECALLREGS() (M.x86.debug & DEBUG_TRACECALL_REGS_F)
59 #define DEBUG_SYS() (M.x86.debug & DEBUG_SYS_F)
60 #define DEBUG_MEM_TRACE() (M.x86.debug & DEBUG_MEM_TRACE_F)
61 #define DEBUG_IO_TRACE() (M.x86.debug & DEBUG_IO_TRACE_F)
62 #define DEBUG_DECODE_NOPRINT() (M.x86.debug & DEBUG_DECODE_NOPRINT_F)
63 #else
64 #define DEBUG_INSTRUMENT() 0
65 #define DEBUG_DECODE() 0
66 #define DEBUG_TRACE() 0
67 #define DEBUG_STEP() 0
68 #define DEBUG_DISASSEMBLE() 0
69 #define DEBUG_BREAK() 0
70 #define DEBUG_SVC() 0
71 #define DEBUG_SAVE_IP_CS() 0
72 #define DEBUG_FS() 0
73 #define DEBUG_PROC() 0
74 #define DEBUG_SYSINT() 0
75 #define DEBUG_TRACECALL() 0
76 #define DEBUG_TRACECALLREGS() 0
77 #define DEBUG_SYS() 0
78 #define DEBUG_MEM_TRACE() 0
79 #define DEBUG_IO_TRACE() 0
80 #define DEBUG_DECODE_NOPRINT() 0
81 #endif
82 
83 #ifdef DEBUG
84 
85 #define DECODE_PRINTF(x) \
86  if (DEBUG_DECODE()) \
87  x86emu_decode_printf(x)
88 #define DECODE_PRINTF2(x, y) \
89  if (DEBUG_DECODE()) \
90  x86emu_decode_printf2(x, y)
91 
92 /*
93  * The following allow us to look at the bytes of an instruction. The
94  * first INCR_INSTRN_LEN, is called everytime bytes are consumed in
95  * the decoding process. The SAVE_IP_CS is called initially when the
96  * major opcode of the instruction is accessed.
97  */
98 #define INC_DECODED_INST_LEN(x) \
99  if (DEBUG_DECODE()) \
100  x86emu_inc_decoded_inst_len(x)
101 
102 #define SAVE_IP_CS(x, y) \
103  if (DEBUG_DECODE() | DEBUG_TRACECALL() | DEBUG_BREAK() | \
104  DEBUG_IO_TRACE() | DEBUG_SAVE_IP_CS()) \
105  { \
106  M.x86.saved_cs = x; \
107  M.x86.saved_ip = y; \
108  }
109 #else
110 #define INC_DECODED_INST_LEN(x)
111 #define DECODE_PRINTF(x)
112 #define DECODE_PRINTF2(x, y)
113 #define SAVE_IP_CS(x, y)
114 #endif
115 
116 #ifdef DEBUG
117 #define TRACE_REGS() \
118  if (DEBUG_DISASSEMBLE()) \
119  { \
120  x86emu_just_disassemble(); \
121  goto EndOfTheInstructionProcedure; \
122  } \
123  if (DEBUG_TRACE() || DEBUG_DECODE()) \
124  X86EMU_trace_regs()
125 #else
126 #define TRACE_REGS()
127 #endif
128 
129 #ifdef DEBUG
130 #define SINGLE_STEP() \
131  if (DEBUG_STEP()) \
132  x86emu_single_step()
133 #else
134 #define SINGLE_STEP()
135 #endif
136 
137 #define TRACE_AND_STEP() \
138  TRACE_REGS(); \
139  SINGLE_STEP()
140 
141 #ifdef DEBUG
142 #define START_OF_INSTR()
143 #define END_OF_INSTR() \
144  EndOfTheInstructionProcedure: \
145  x86emu_end_instr();
146 #define END_OF_INSTR_NO_TRACE() x86emu_end_instr();
147 #else
148 #define START_OF_INSTR()
149 #define END_OF_INSTR()
150 #define END_OF_INSTR_NO_TRACE()
151 #endif
152 
153 #ifdef DEBUG
154 #define CALL_TRACE(u, v, w, x, s) \
155  if (DEBUG_TRACECALLREGS()) \
156  x86emu_dump_regs(); \
157  if (DEBUG_TRACECALL()) \
158  printk("%04x:%04x: CALL %s%04x:%04x\n", u, v, s, w, x);
159 #define RETURN_TRACE(n, u, v) \
160  if (DEBUG_TRACECALLREGS()) \
161  x86emu_dump_regs(); \
162  if (DEBUG_TRACECALL()) \
163  printk("%04x:%04x: %s\n", u, v, n);
164 #else
165 #define CALL_TRACE(u, v, w, x, s)
166 #define RETURN_TRACE(n, u, v)
167 #endif
168 
169 #ifdef DEBUG
170 #define DB(x) x
171 #else
172 #define DB(x)
173 #endif
174 
175 /*-------------------------- Function Prototypes --------------------------*/
176 
177 #ifdef __cplusplus
178 extern "C" { /* Use "C" linkage when in C++ mode */
179 #endif
180 
181 extern void x86emu_inc_decoded_inst_len(int x);
182 extern void x86emu_decode_printf(char *x);
183 extern void x86emu_decode_printf2(char *x, int y);
184 extern void x86emu_just_disassemble(void);
185 extern void x86emu_single_step(void);
186 extern void x86emu_end_instr(void);
187 extern void x86emu_dump_regs(void);
188 extern void x86emu_dump_xregs(void);
189 extern void x86emu_print_int_vect(u16 iv);
190 extern void x86emu_instrument_instruction(void);
191 extern void x86emu_check_ip_access(void);
192 extern void x86emu_check_sp_access(void);
193 extern void x86emu_check_mem_access(u32 p);
194 extern void x86emu_check_data_access(uint s, uint o);
195 
196 #ifdef __cplusplus
197 } /* End of "C" linkage for C++ */
198 #endif
199 
200 #endif /* __X86EMU_DEBUG_H */