xref: /qemu/include/exec/gdbstub.h (revision a976a99a)
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 /* For gdb file i/o remote protocol open flags. */
14 #define GDB_O_RDONLY  0
15 #define GDB_O_WRONLY  1
16 #define GDB_O_RDWR    2
17 #define GDB_O_APPEND  8
18 #define GDB_O_CREAT   0x200
19 #define GDB_O_TRUNC   0x400
20 #define GDB_O_EXCL    0x800
21 
22 /* For gdb file i/o remote protocol errno values */
23 #define GDB_EPERM           1
24 #define GDB_ENOENT          2
25 #define GDB_EINTR           4
26 #define GDB_EBADF           9
27 #define GDB_EACCES         13
28 #define GDB_EFAULT         14
29 #define GDB_EBUSY          16
30 #define GDB_EEXIST         17
31 #define GDB_ENODEV         19
32 #define GDB_ENOTDIR        20
33 #define GDB_EISDIR         21
34 #define GDB_EINVAL         22
35 #define GDB_ENFILE         23
36 #define GDB_EMFILE         24
37 #define GDB_EFBIG          27
38 #define GDB_ENOSPC         28
39 #define GDB_ESPIPE         29
40 #define GDB_EROFS          30
41 #define GDB_ENAMETOOLONG   91
42 #define GDB_EUNKNOWN       9999
43 
44 /* For gdb file i/o remote protocol lseek whence. */
45 #define GDB_SEEK_SET  0
46 #define GDB_SEEK_CUR  1
47 #define GDB_SEEK_END  2
48 
49 /* For gdb file i/o stat/fstat. */
50 typedef uint32_t gdb_mode_t;
51 typedef uint32_t gdb_time_t;
52 
53 struct gdb_stat {
54   uint32_t    gdb_st_dev;     /* device */
55   uint32_t    gdb_st_ino;     /* inode */
56   gdb_mode_t  gdb_st_mode;    /* protection */
57   uint32_t    gdb_st_nlink;   /* number of hard links */
58   uint32_t    gdb_st_uid;     /* user ID of owner */
59   uint32_t    gdb_st_gid;     /* group ID of owner */
60   uint32_t    gdb_st_rdev;    /* device type (if inode device) */
61   uint64_t    gdb_st_size;    /* total size, in bytes */
62   uint64_t    gdb_st_blksize; /* blocksize for filesystem I/O */
63   uint64_t    gdb_st_blocks;  /* number of blocks allocated */
64   gdb_time_t  gdb_st_atime;   /* time of last access */
65   gdb_time_t  gdb_st_mtime;   /* time of last modification */
66   gdb_time_t  gdb_st_ctime;   /* time of last change */
67 } QEMU_PACKED;
68 
69 struct gdb_timeval {
70   gdb_time_t tv_sec;  /* second */
71   uint64_t tv_usec;   /* microsecond */
72 } QEMU_PACKED;
73 
74 #ifdef NEED_CPU_H
75 #include "cpu.h"
76 
77 typedef void (*gdb_syscall_complete_cb)(CPUState *cpu, uint64_t ret, int err);
78 
79 /**
80  * gdb_do_syscall:
81  * @cb: function to call when the system call has completed
82  * @fmt: gdb syscall format string
83  * ...: list of arguments to interpolate into @fmt
84  *
85  * Send a GDB syscall request. This function will return immediately;
86  * the callback function will be called later when the remote system
87  * call has completed.
88  *
89  * @fmt should be in the 'call-id,parameter,parameter...' format documented
90  * for the F request packet in the GDB remote protocol. A limited set of
91  * printf-style format specifiers is supported:
92  *   %x  - target_ulong argument printed in hex
93  *   %lx - 64-bit argument printed in hex
94  *   %s  - string pointer (target_ulong) and length (int) pair
95  */
96 void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...);
97 /**
98  * gdb_do_syscallv:
99  * @cb: function to call when the system call has completed
100  * @fmt: gdb syscall format string
101  * @va: arguments to interpolate into @fmt
102  *
103  * As gdb_do_syscall, but taking a va_list rather than a variable
104  * argument list.
105  */
106 void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va);
107 int use_gdb_syscalls(void);
108 
109 #ifdef CONFIG_USER_ONLY
110 /**
111  * gdb_handlesig: yield control to gdb
112  * @cpu: CPU
113  * @sig: if non-zero, the signal number which caused us to stop
114  *
115  * This function yields control to gdb, when a user-mode-only target
116  * needs to stop execution. If @sig is non-zero, then we will send a
117  * stop packet to tell gdb that we have stopped because of this signal.
118  *
119  * This function will block (handling protocol requests from gdb)
120  * until gdb tells us to continue target execution. When it does
121  * return, the return value is a signal to deliver to the target,
122  * or 0 if no signal should be delivered, ie the signal that caused
123  * us to stop should be ignored.
124  */
125 int gdb_handlesig(CPUState *, int);
126 void gdb_signalled(CPUArchState *, int);
127 void gdbserver_fork(CPUState *);
128 #endif
129 /* Get or set a register.  Returns the size of the register.  */
130 typedef int (*gdb_get_reg_cb)(CPUArchState *env, GByteArray *buf, int reg);
131 typedef int (*gdb_set_reg_cb)(CPUArchState *env, uint8_t *buf, int reg);
132 void gdb_register_coprocessor(CPUState *cpu,
133                               gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
134                               int num_regs, const char *xml, int g_pos);
135 
136 /*
137  * The GDB remote protocol transfers values in target byte order. As
138  * the gdbstub may be batching up several register values we always
139  * append to the array.
140  */
141 
142 static inline int gdb_get_reg8(GByteArray *buf, uint8_t val)
143 {
144     g_byte_array_append(buf, &val, 1);
145     return 1;
146 }
147 
148 static inline int gdb_get_reg16(GByteArray *buf, uint16_t val)
149 {
150     uint16_t to_word = tswap16(val);
151     g_byte_array_append(buf, (uint8_t *) &to_word, 2);
152     return 2;
153 }
154 
155 static inline int gdb_get_reg32(GByteArray *buf, uint32_t val)
156 {
157     uint32_t to_long = tswap32(val);
158     g_byte_array_append(buf, (uint8_t *) &to_long, 4);
159     return 4;
160 }
161 
162 static inline int gdb_get_reg64(GByteArray *buf, uint64_t val)
163 {
164     uint64_t to_quad = tswap64(val);
165     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
166     return 8;
167 }
168 
169 static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
170                                  uint64_t val_lo)
171 {
172     uint64_t to_quad;
173 #if TARGET_BIG_ENDIAN
174     to_quad = tswap64(val_hi);
175     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
176     to_quad = tswap64(val_lo);
177     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
178 #else
179     to_quad = tswap64(val_lo);
180     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
181     to_quad = tswap64(val_hi);
182     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
183 #endif
184     return 16;
185 }
186 
187 static inline int gdb_get_zeroes(GByteArray *array, size_t len)
188 {
189     guint oldlen = array->len;
190     g_byte_array_set_size(array, oldlen + len);
191     memset(array->data + oldlen, 0, len);
192 
193     return len;
194 }
195 
196 /**
197  * gdb_get_reg_ptr: get pointer to start of last element
198  * @len: length of element
199  *
200  * This is a helper function to extract the pointer to the last
201  * element for additional processing. Some front-ends do additional
202  * dynamic swapping of the elements based on CPU state.
203  */
204 static inline uint8_t * gdb_get_reg_ptr(GByteArray *buf, int len)
205 {
206     return buf->data + buf->len - len;
207 }
208 
209 #if TARGET_LONG_BITS == 64
210 #define gdb_get_regl(buf, val) gdb_get_reg64(buf, val)
211 #define ldtul_p(addr) ldq_p(addr)
212 #else
213 #define gdb_get_regl(buf, val) gdb_get_reg32(buf, val)
214 #define ldtul_p(addr) ldl_p(addr)
215 #endif
216 
217 #endif /* NEED_CPU_H */
218 
219 /**
220  * gdbserver_start: start the gdb server
221  * @port_or_device: connection spec for gdb
222  *
223  * For CONFIG_USER this is either a tcp port or a path to a fifo. For
224  * system emulation you can use a full chardev spec for your gdbserver
225  * port.
226  */
227 int gdbserver_start(const char *port_or_device);
228 
229 /**
230  * gdb_exit: exit gdb session, reporting inferior status
231  * @code: exit code reported
232  *
233  * This closes the session and sends a final packet to GDB reporting
234  * the exit status of the program. It also cleans up any connections
235  * detritus before returning.
236  */
237 void gdb_exit(int code);
238 
239 void gdb_set_stop_cpu(CPUState *cpu);
240 
241 /**
242  * gdb_has_xml:
243  * This is an ugly hack to cope with both new and old gdb.
244  * If gdb sends qXfer:features:read then assume we're talking to a newish
245  * gdb that understands target descriptions.
246  */
247 extern bool gdb_has_xml;
248 
249 /* in gdbstub-xml.c, generated by scripts/feature_to_c.sh */
250 extern const char *const xml_builtin[][2];
251 
252 #endif
253