Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * gdbstub internals |
| 3 | * |
| 4 | * Copyright (c) 2022 Linaro Ltd |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | */ |
| 8 | |
Alex Bennée | 9774855 | 2023-03-02 18:57:37 -0800 | [diff] [blame] | 9 | #ifndef GDBSTUB_INTERNALS_H |
| 10 | #define GDBSTUB_INTERNALS_H |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 11 | |
Philippe Mathieu-Daudé | 55b5b8e | 2022-12-06 16:20:27 +0100 | [diff] [blame] | 12 | #include "exec/cpu-common.h" |
| 13 | |
Alex Bennée | 9f56787 | 2023-03-02 18:57:42 -0800 | [diff] [blame] | 14 | #define MAX_PACKET_LENGTH 4096 |
| 15 | |
| 16 | /* |
| 17 | * Shared structures and definitions |
| 18 | */ |
| 19 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 20 | enum { |
| 21 | GDB_SIGNAL_0 = 0, |
| 22 | GDB_SIGNAL_INT = 2, |
| 23 | GDB_SIGNAL_QUIT = 3, |
| 24 | GDB_SIGNAL_TRAP = 5, |
| 25 | GDB_SIGNAL_ABRT = 6, |
| 26 | GDB_SIGNAL_ALRM = 14, |
| 27 | GDB_SIGNAL_IO = 23, |
| 28 | GDB_SIGNAL_XCPU = 24, |
| 29 | GDB_SIGNAL_UNKNOWN = 143 |
| 30 | }; |
| 31 | |
Alex Bennée | 9f56787 | 2023-03-02 18:57:42 -0800 | [diff] [blame] | 32 | typedef struct GDBProcess { |
| 33 | uint32_t pid; |
| 34 | bool attached; |
Alex Bennée | 56e534b | 2023-08-29 17:15:26 +0100 | [diff] [blame] | 35 | char *target_xml; |
Alex Bennée | 9f56787 | 2023-03-02 18:57:42 -0800 | [diff] [blame] | 36 | } GDBProcess; |
| 37 | |
| 38 | enum RSState { |
| 39 | RS_INACTIVE, |
| 40 | RS_IDLE, |
| 41 | RS_GETLINE, |
| 42 | RS_GETLINE_ESC, |
| 43 | RS_GETLINE_RLE, |
| 44 | RS_CHKSUM1, |
| 45 | RS_CHKSUM2, |
| 46 | }; |
| 47 | |
| 48 | typedef struct GDBState { |
| 49 | bool init; /* have we been initialised? */ |
| 50 | CPUState *c_cpu; /* current CPU for step/continue ops */ |
| 51 | CPUState *g_cpu; /* current CPU for other ops */ |
| 52 | CPUState *query_cpu; /* for q{f|s}ThreadInfo */ |
| 53 | enum RSState state; /* parsing state */ |
| 54 | char line_buf[MAX_PACKET_LENGTH]; |
| 55 | int line_buf_index; |
| 56 | int line_sum; /* running checksum */ |
| 57 | int line_csum; /* checksum at the end of the packet */ |
| 58 | GByteArray *last_packet; |
| 59 | int signal; |
| 60 | bool multiprocess; |
| 61 | GDBProcess *processes; |
| 62 | int process_num; |
Alex Bennée | 9f56787 | 2023-03-02 18:57:42 -0800 | [diff] [blame] | 63 | GString *str_buf; |
| 64 | GByteArray *mem_buf; |
| 65 | int sstep_flags; |
| 66 | int supported_sstep_flags; |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 67 | /* |
| 68 | * Whether we are allowed to send a stop reply packet at this moment. |
| 69 | * Must be set off after sending the stop reply itself. |
| 70 | */ |
| 71 | bool allow_stop_reply; |
Alex Bennée | 9f56787 | 2023-03-02 18:57:42 -0800 | [diff] [blame] | 72 | } GDBState; |
| 73 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 74 | /* lives in main gdbstub.c */ |
| 75 | extern GDBState gdbserver_state; |
Alex Bennée | 1678ea0 | 2023-03-02 18:57:44 -0800 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Inline utility function, convert from int to hex and back |
| 79 | */ |
| 80 | |
| 81 | static inline int fromhex(int v) |
| 82 | { |
| 83 | if (v >= '0' && v <= '9') { |
| 84 | return v - '0'; |
| 85 | } else if (v >= 'A' && v <= 'F') { |
| 86 | return v - 'A' + 10; |
| 87 | } else if (v >= 'a' && v <= 'f') { |
| 88 | return v - 'a' + 10; |
| 89 | } else { |
| 90 | return 0; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | static inline int tohex(int v) |
| 95 | { |
| 96 | if (v < 10) { |
| 97 | return v + '0'; |
| 98 | } else { |
| 99 | return v - 10 + 'a'; |
| 100 | } |
| 101 | } |
| 102 | |
Alex Bennée | 9f56787 | 2023-03-02 18:57:42 -0800 | [diff] [blame] | 103 | /* |
Philippe Mathieu-Daudé | 3f7d1bd | 2023-10-04 11:06:22 +0200 | [diff] [blame] | 104 | * Connection helpers for both system and user backends |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 105 | */ |
| 106 | |
| 107 | void gdb_put_strbuf(void); |
| 108 | int gdb_put_packet(const char *buf); |
| 109 | int gdb_put_packet_binary(const char *buf, int len, bool dump); |
| 110 | void gdb_hextomem(GByteArray *mem, const char *buf, int len); |
| 111 | void gdb_memtohex(GString *buf, const uint8_t *mem, int len); |
| 112 | void gdb_memtox(GString *buf, const char *mem, int len); |
| 113 | void gdb_read_byte(uint8_t ch); |
| 114 | |
Alex Bennée | a7e0f9b | 2023-03-02 18:57:49 -0800 | [diff] [blame] | 115 | /* |
| 116 | * Packet acknowledgement - we handle this slightly differently |
| 117 | * between user and softmmu mode, mainly to deal with the differences |
| 118 | * between the flexible chardev and the direct fd approaches. |
| 119 | * |
| 120 | * We currently don't support a negotiated QStartNoAckMode |
| 121 | */ |
| 122 | |
| 123 | /** |
| 124 | * gdb_got_immediate_ack() - check ok to continue |
| 125 | * |
| 126 | * Returns true to continue, false to re-transmit for user only, the |
| 127 | * softmmu stub always returns true. |
| 128 | */ |
| 129 | bool gdb_got_immediate_ack(void); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 130 | /* utility helpers */ |
Ilya Leoshkevich | a3fcc11 | 2023-06-30 19:04:19 +0100 | [diff] [blame] | 131 | GDBProcess *gdb_get_process(uint32_t pid); |
| 132 | CPUState *gdb_get_first_cpu_in_process(GDBProcess *process); |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 133 | CPUState *gdb_first_attached_cpu(void); |
| 134 | void gdb_append_thread_id(CPUState *cpu, GString *buf); |
| 135 | int gdb_get_cpu_index(CPUState *cpu); |
Alex Bennée | 7ea0c33 | 2023-03-02 18:57:52 -0800 | [diff] [blame] | 136 | unsigned int gdb_get_max_cpus(void); /* both */ |
Alex Bennée | 505601d | 2023-03-02 18:57:53 -0800 | [diff] [blame] | 137 | bool gdb_can_reverse(void); /* softmmu, stub for user */ |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 138 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 139 | void gdb_create_default_process(GDBState *s); |
| 140 | |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 141 | /* signal mapping, common for softmmu, specialised for user-mode */ |
| 142 | int gdb_signal_to_target(int sig); |
| 143 | int gdb_target_signal_to_gdb(int sig); |
| 144 | |
| 145 | int gdb_get_char(void); /* user only */ |
| 146 | |
| 147 | /** |
| 148 | * gdb_continue() - handle continue in mode specific way. |
| 149 | */ |
| 150 | void gdb_continue(void); |
| 151 | |
| 152 | /** |
| 153 | * gdb_continue_partial() - handle partial continue in mode specific way. |
| 154 | */ |
| 155 | int gdb_continue_partial(char *newstates); |
| 156 | |
Alex Bennée | 36e067b | 2023-03-02 18:57:45 -0800 | [diff] [blame] | 157 | /* |
| 158 | * Helpers with separate softmmu and user implementations |
| 159 | */ |
| 160 | void gdb_put_buffer(const uint8_t *buf, int len); |
| 161 | |
| 162 | /* |
Alex Bennée | 8a2025b | 2023-03-02 18:57:50 -0800 | [diff] [blame] | 163 | * Command handlers - either specialised or softmmu or user only |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 164 | */ |
| 165 | void gdb_init_gdbserver_state(void); |
| 166 | |
| 167 | typedef enum GDBThreadIdKind { |
| 168 | GDB_ONE_THREAD = 0, |
| 169 | GDB_ALL_THREADS, /* One process, all threads */ |
| 170 | GDB_ALL_PROCESSES, |
| 171 | GDB_READ_THREAD_ERR |
| 172 | } GDBThreadIdKind; |
| 173 | |
| 174 | typedef union GdbCmdVariant { |
| 175 | const char *data; |
| 176 | uint8_t opcode; |
| 177 | unsigned long val_ul; |
| 178 | unsigned long long val_ull; |
| 179 | struct { |
| 180 | GDBThreadIdKind kind; |
| 181 | uint32_t pid; |
| 182 | uint32_t tid; |
| 183 | } thread_id; |
| 184 | } GdbCmdVariant; |
| 185 | |
| 186 | #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i)) |
| 187 | |
| 188 | void gdb_handle_query_rcmd(GArray *params, void *user_ctx); /* softmmu */ |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 189 | void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */ |
| 190 | void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */ |
Ilya Leoshkevich | e282010 | 2023-06-30 19:04:21 +0100 | [diff] [blame] | 191 | void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */ |
| 192 | void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */ |
| 193 | void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */ |
| 194 | void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */ |
| 195 | void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */ |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 196 | |
Alex Bennée | 8a2025b | 2023-03-02 18:57:50 -0800 | [diff] [blame] | 197 | void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */ |
| 198 | |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 199 | /* softmmu only */ |
| 200 | void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *user_ctx); |
| 201 | void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx); |
| 202 | |
Alex Bennée | c566080 | 2023-03-02 18:57:57 -0800 | [diff] [blame] | 203 | /* sycall handling */ |
| 204 | void gdb_handle_file_io(GArray *params, void *user_ctx); |
| 205 | bool gdb_handled_syscall(void); |
| 206 | void gdb_disable_syscalls(void); |
| 207 | void gdb_syscall_reset(void); |
| 208 | |
Alex Bennée | 131f387 | 2023-03-02 18:58:01 -0800 | [diff] [blame] | 209 | /* user/softmmu specific syscall handling */ |
| 210 | void gdb_syscall_handling(const char *syscall_packet); |
| 211 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 212 | /* |
Alex Bennée | 9f56787 | 2023-03-02 18:57:42 -0800 | [diff] [blame] | 213 | * Break/Watch point support - there is an implementation for softmmu |
| 214 | * and user mode. |
| 215 | */ |
Alex Bennée | a48e7d9 | 2022-09-29 12:42:25 +0100 | [diff] [blame] | 216 | bool gdb_supports_guest_debug(void); |
Philippe Mathieu-Daudé | 55b5b8e | 2022-12-06 16:20:27 +0100 | [diff] [blame] | 217 | int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len); |
| 218 | int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len); |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 219 | void gdb_breakpoint_remove_all(CPUState *cs); |
| 220 | |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 221 | /** |
| 222 | * gdb_target_memory_rw_debug() - handle debug access to memory |
| 223 | * @cs: CPUState |
| 224 | * @addr: nominal address, could be an entire physical address |
| 225 | * @buf: data |
| 226 | * @len: length of access |
| 227 | * @is_write: is it a write operation |
| 228 | * |
| 229 | * This function is specialised depending on the mode we are running |
Philippe Mathieu-Daudé | 3f7d1bd | 2023-10-04 11:06:22 +0200 | [diff] [blame] | 230 | * in. For system guests we can switch the interpretation of the |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 231 | * address to a physical address. |
| 232 | */ |
| 233 | int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr, |
| 234 | uint8_t *buf, int len, bool is_write); |
| 235 | |
Alex Bennée | 9774855 | 2023-03-02 18:57:37 -0800 | [diff] [blame] | 236 | #endif /* GDBSTUB_INTERNALS_H */ |