xref: /openbmc/qemu/include/exec/gdbstub.h (revision d96bf49ba842e8e1b73c7884d2be084582f34228)
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 typedef void (*gdb_syscall_complete_cb)(CPUState *cpu, uint64_t ret, int err);
75 
76 /**
77  * gdb_do_syscall:
78  * @cb: function to call when the system call has completed
79  * @fmt: gdb syscall format string
80  * ...: list of arguments to interpolate into @fmt
81  *
82  * Send a GDB syscall request. This function will return immediately;
83  * the callback function will be called later when the remote system
84  * call has completed.
85  *
86  * @fmt should be in the 'call-id,parameter,parameter...' format documented
87  * for the F request packet in the GDB remote protocol. A limited set of
88  * printf-style format specifiers is supported:
89  *   %x  - target_ulong argument printed in hex
90  *   %lx - 64-bit argument printed in hex
91  *   %s  - string pointer (target_ulong) and length (int) pair
92  */
93 void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...);
94 /**
95  * gdb_do_syscallv:
96  * @cb: function to call when the system call has completed
97  * @fmt: gdb syscall format string
98  * @va: arguments to interpolate into @fmt
99  *
100  * As gdb_do_syscall, but taking a va_list rather than a variable
101  * argument list.
102  */
103 void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va);
104 int use_gdb_syscalls(void);
105 
106 /* Get or set a register.  Returns the size of the register.  */
107 typedef int (*gdb_get_reg_cb)(CPUArchState *env, GByteArray *buf, int reg);
108 typedef int (*gdb_set_reg_cb)(CPUArchState *env, uint8_t *buf, int reg);
109 void gdb_register_coprocessor(CPUState *cpu,
110                               gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
111                               int num_regs, const char *xml, int g_pos);
112 
113 #ifdef NEED_CPU_H
114 #include "cpu.h"
115 
116 /*
117  * The GDB remote protocol transfers values in target byte order. As
118  * the gdbstub may be batching up several register values we always
119  * append to the array.
120  */
121 
122 static inline int gdb_get_reg8(GByteArray *buf, uint8_t val)
123 {
124     g_byte_array_append(buf, &val, 1);
125     return 1;
126 }
127 
128 static inline int gdb_get_reg16(GByteArray *buf, uint16_t val)
129 {
130     uint16_t to_word = tswap16(val);
131     g_byte_array_append(buf, (uint8_t *) &to_word, 2);
132     return 2;
133 }
134 
135 static inline int gdb_get_reg32(GByteArray *buf, uint32_t val)
136 {
137     uint32_t to_long = tswap32(val);
138     g_byte_array_append(buf, (uint8_t *) &to_long, 4);
139     return 4;
140 }
141 
142 static inline int gdb_get_reg64(GByteArray *buf, uint64_t val)
143 {
144     uint64_t to_quad = tswap64(val);
145     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
146     return 8;
147 }
148 
149 static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
150                                  uint64_t val_lo)
151 {
152     uint64_t to_quad;
153 #if TARGET_BIG_ENDIAN
154     to_quad = tswap64(val_hi);
155     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
156     to_quad = tswap64(val_lo);
157     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
158 #else
159     to_quad = tswap64(val_lo);
160     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
161     to_quad = tswap64(val_hi);
162     g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
163 #endif
164     return 16;
165 }
166 
167 static inline int gdb_get_zeroes(GByteArray *array, size_t len)
168 {
169     guint oldlen = array->len;
170     g_byte_array_set_size(array, oldlen + len);
171     memset(array->data + oldlen, 0, len);
172 
173     return len;
174 }
175 
176 /**
177  * gdb_get_reg_ptr: get pointer to start of last element
178  * @len: length of element
179  *
180  * This is a helper function to extract the pointer to the last
181  * element for additional processing. Some front-ends do additional
182  * dynamic swapping of the elements based on CPU state.
183  */
184 static inline uint8_t * gdb_get_reg_ptr(GByteArray *buf, int len)
185 {
186     return buf->data + buf->len - len;
187 }
188 
189 #if TARGET_LONG_BITS == 64
190 #define gdb_get_regl(buf, val) gdb_get_reg64(buf, val)
191 #define ldtul_p(addr) ldq_p(addr)
192 #else
193 #define gdb_get_regl(buf, val) gdb_get_reg32(buf, val)
194 #define ldtul_p(addr) ldl_p(addr)
195 #endif
196 
197 #endif /* NEED_CPU_H */
198 
199 /**
200  * gdbserver_start: start the gdb server
201  * @port_or_device: connection spec for gdb
202  *
203  * For CONFIG_USER this is either a tcp port or a path to a fifo. For
204  * system emulation you can use a full chardev spec for your gdbserver
205  * port.
206  */
207 int gdbserver_start(const char *port_or_device);
208 
209 /**
210  * gdb_exit: exit gdb session, reporting inferior status
211  * @code: exit code reported
212  *
213  * This closes the session and sends a final packet to GDB reporting
214  * the exit status of the program. It also cleans up any connections
215  * detritus before returning.
216  */
217 void gdb_exit(int code);
218 
219 void gdb_set_stop_cpu(CPUState *cpu);
220 
221 /**
222  * gdb_has_xml:
223  * This is an ugly hack to cope with both new and old gdb.
224  * If gdb sends qXfer:features:read then assume we're talking to a newish
225  * gdb that understands target descriptions.
226  */
227 extern bool gdb_has_xml;
228 
229 /* in gdbstub-xml.c, generated by scripts/feature_to_c.sh */
230 extern const char *const xml_builtin[][2];
231 
232 #endif
233