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