xref: /qemu/include/exec/gdbstub.h (revision a976ed3f)
1 #ifndef GDBSTUB_H
2 #define GDBSTUB_H
3 
4 #define DEFAULT_GDBSTUB_PORT "1234"
5 
6 /* GDB breakpoint/watchpoint types */
7 #define GDB_BREAKPOINT_SW        0
8 #define GDB_BREAKPOINT_HW        1
9 #define GDB_WATCHPOINT_WRITE     2
10 #define GDB_WATCHPOINT_READ      3
11 #define GDB_WATCHPOINT_ACCESS    4
12 
13 #ifdef NEED_CPU_H
14 #include "cpu.h"
15 
16 typedef void (*gdb_syscall_complete_cb)(CPUState *cpu,
17                                         target_ulong ret, target_ulong err);
18 
19 /**
20  * gdb_do_syscall:
21  * @cb: function to call when the system call has completed
22  * @fmt: gdb syscall format string
23  * ...: list of arguments to interpolate into @fmt
24  *
25  * Send a GDB syscall request. This function will return immediately;
26  * the callback function will be called later when the remote system
27  * call has completed.
28  *
29  * @fmt should be in the 'call-id,parameter,parameter...' format documented
30  * for the F request packet in the GDB remote protocol. A limited set of
31  * printf-style format specifiers is supported:
32  *   %x  - target_ulong argument printed in hex
33  *   %lx - 64-bit argument printed in hex
34  *   %s  - string pointer (target_ulong) and length (int) pair
35  */
36 void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...);
37 /**
38  * gdb_do_syscallv:
39  * @cb: function to call when the system call has completed
40  * @fmt: gdb syscall format string
41  * @va: arguments to interpolate into @fmt
42  *
43  * As gdb_do_syscall, but taking a va_list rather than a variable
44  * argument list.
45  */
46 void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va);
47 int use_gdb_syscalls(void);
48 void gdb_set_stop_cpu(CPUState *cpu);
49 void gdb_exit(CPUArchState *, int);
50 #ifdef CONFIG_USER_ONLY
51 /**
52  * gdb_handlesig: yield control to gdb
53  * @cpu: CPU
54  * @sig: if non-zero, the signal number which caused us to stop
55  *
56  * This function yields control to gdb, when a user-mode-only target
57  * needs to stop execution. If @sig is non-zero, then we will send a
58  * stop packet to tell gdb that we have stopped because of this signal.
59  *
60  * This function will block (handling protocol requests from gdb)
61  * until gdb tells us to continue target execution. When it does
62  * return, the return value is a signal to deliver to the target,
63  * or 0 if no signal should be delivered, ie the signal that caused
64  * us to stop should be ignored.
65  */
66 int gdb_handlesig(CPUState *, int);
67 void gdb_signalled(CPUArchState *, int);
68 void gdbserver_fork(CPUState *);
69 #endif
70 /* Get or set a register.  Returns the size of the register.  */
71 typedef int (*gdb_get_reg_cb)(CPUArchState *env, GByteArray *buf, int reg);
72 typedef int (*gdb_set_reg_cb)(CPUArchState *env, uint8_t *buf, int reg);
73 void gdb_register_coprocessor(CPUState *cpu,
74                               gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
75                               int num_regs, const char *xml, int g_pos);
76 
77 /*
78  * The GDB remote protocol transfers values in target byte order. As
79  * the gdbstub may be batching up several register values we always
80  * append to the array.
81  */
82 
83 static inline int gdb_get_reg8(GByteArray *buf, uint8_t val)
84 {
85     g_byte_array_append(buf, &val, 1);
86     return 1;
87 }
88 
89 static inline int gdb_get_reg16(GByteArray *buf, uint16_t val)
90 {
91     uint16_t to_word = tswap16(val);
92     g_byte_array_append(buf, (uint8_t *) &to_word, 2);
93     return 2;
94 }
95 
96 static inline int gdb_get_reg32(GByteArray *buf, uint32_t val)
97 {
98     uint32_t to_long = tswap32(val);
99     g_byte_array_append(buf, (uint8_t *) &to_long, 4);
100     return 4;
101 }
102 
103 static inline int gdb_get_reg64(GByteArray *buf, uint64_t val)
104 {
105     uint64_t to_quad = tswap64(val);
106     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
107     return 8;
108 }
109 
110 static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
111                                  uint64_t val_lo)
112 {
113     uint64_t to_quad;
114 #ifdef TARGET_WORDS_BIGENDIAN
115     to_quad = tswap64(val_hi);
116     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
117     to_quad = tswap64(val_lo);
118     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
119 #else
120     to_quad = tswap64(val_lo);
121     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
122     to_quad = tswap64(val_hi);
123     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
124 #endif
125     return 16;
126 }
127 
128 static inline int gdb_get_float32(GByteArray *array, float32 val)
129 {
130     uint8_t buf[sizeof(CPU_FloatU)];
131 
132     stfl_p(buf, val);
133     g_byte_array_append(array, buf, sizeof(buf));
134 
135     return sizeof(buf);
136 }
137 
138 static inline int gdb_get_float64(GByteArray *array, float64 val)
139 {
140     uint8_t buf[sizeof(CPU_DoubleU)];
141 
142     stfq_p(buf, val);
143     g_byte_array_append(array, buf, sizeof(buf));
144 
145     return sizeof(buf);
146 }
147 
148 static inline int gdb_get_zeroes(GByteArray *array, size_t len)
149 {
150     guint oldlen = array->len;
151     g_byte_array_set_size(array, oldlen + len);
152     memset(array->data + oldlen, 0, len);
153 
154     return len;
155 }
156 
157 /**
158  * gdb_get_reg_ptr: get pointer to start of last element
159  * @len: length of element
160  *
161  * This is a helper function to extract the pointer to the last
162  * element for additional processing. Some front-ends do additional
163  * dynamic swapping of the elements based on CPU state.
164  */
165 static inline uint8_t * gdb_get_reg_ptr(GByteArray *buf, int len)
166 {
167     return buf->data + buf->len - len;
168 }
169 
170 #if TARGET_LONG_BITS == 64
171 #define gdb_get_regl(buf, val) gdb_get_reg64(buf, val)
172 #define ldtul_p(addr) ldq_p(addr)
173 #else
174 #define gdb_get_regl(buf, val) gdb_get_reg32(buf, val)
175 #define ldtul_p(addr) ldl_p(addr)
176 #endif
177 
178 #endif
179 
180 /**
181  * gdbserver_start: start the gdb server
182  * @port_or_device: connection spec for gdb
183  *
184  * For CONFIG_USER this is either a tcp port or a path to a fifo. For
185  * system emulation you can use a full chardev spec for your gdbserver
186  * port.
187  */
188 int gdbserver_start(const char *port_or_device);
189 
190 void gdbserver_cleanup(void);
191 
192 /**
193  * gdb_has_xml:
194  * This is an ugly hack to cope with both new and old gdb.
195  * If gdb sends qXfer:features:read then assume we're talking to a newish
196  * gdb that understands target descriptions.
197  */
198 extern bool gdb_has_xml;
199 
200 /* in gdbstub-xml.c, generated by scripts/feature_to_c.sh */
201 extern const char *const xml_builtin[][2];
202 
203 #endif
204