blob: 66c939c67f5dedf0a06a97be8e049bda85b09d1f [file] [log] [blame]
Alex Bennéeae7467b2022-09-29 12:42:24 +01001/*
2 * gdbstub internals
3 *
4 * Copyright (c) 2022 Linaro Ltd
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
Alex Bennée97748552023-03-02 18:57:37 -08009#ifndef GDBSTUB_INTERNALS_H
10#define GDBSTUB_INTERNALS_H
Alex Bennéeae7467b2022-09-29 12:42:24 +010011
Philippe Mathieu-Daudé55b5b8e2022-12-06 16:20:27 +010012#include "exec/cpu-common.h"
13
Alex Bennée9f567872023-03-02 18:57:42 -080014#define MAX_PACKET_LENGTH 4096
15
16/*
17 * Shared structures and definitions
18 */
19
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080020enum {
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,
Alex Bennée5dcf6332023-12-01 09:36:27 +000027 GDB_SIGNAL_STOP = 17,
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080028 GDB_SIGNAL_IO = 23,
29 GDB_SIGNAL_XCPU = 24,
30 GDB_SIGNAL_UNKNOWN = 143
31};
32
Alex Bennée9f567872023-03-02 18:57:42 -080033typedef struct GDBProcess {
34 uint32_t pid;
35 bool attached;
Alex Bennée56e534b2023-08-29 17:15:26 +010036 char *target_xml;
Alex Bennée9f567872023-03-02 18:57:42 -080037} GDBProcess;
38
39enum RSState {
40 RS_INACTIVE,
41 RS_IDLE,
42 RS_GETLINE,
43 RS_GETLINE_ESC,
44 RS_GETLINE_RLE,
45 RS_CHKSUM1,
46 RS_CHKSUM2,
47};
48
49typedef struct GDBState {
50 bool init; /* have we been initialised? */
51 CPUState *c_cpu; /* current CPU for step/continue ops */
52 CPUState *g_cpu; /* current CPU for other ops */
53 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
54 enum RSState state; /* parsing state */
55 char line_buf[MAX_PACKET_LENGTH];
56 int line_buf_index;
57 int line_sum; /* running checksum */
58 int line_csum; /* checksum at the end of the packet */
59 GByteArray *last_packet;
60 int signal;
61 bool multiprocess;
62 GDBProcess *processes;
63 int process_num;
Alex Bennée9f567872023-03-02 18:57:42 -080064 GString *str_buf;
65 GByteArray *mem_buf;
66 int sstep_flags;
67 int supported_sstep_flags;
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -030068 /*
69 * Whether we are allowed to send a stop reply packet at this moment.
70 * Must be set off after sending the stop reply itself.
71 */
72 bool allow_stop_reply;
Alex Bennée9f567872023-03-02 18:57:42 -080073} GDBState;
74
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -080075/* lives in main gdbstub.c */
76extern GDBState gdbserver_state;
Alex Bennée1678ea02023-03-02 18:57:44 -080077
78/*
79 * Inline utility function, convert from int to hex and back
80 */
81
82static inline int fromhex(int v)
83{
84 if (v >= '0' && v <= '9') {
85 return v - '0';
86 } else if (v >= 'A' && v <= 'F') {
87 return v - 'A' + 10;
88 } else if (v >= 'a' && v <= 'f') {
89 return v - 'a' + 10;
90 } else {
91 return 0;
92 }
93}
94
95static inline int tohex(int v)
96{
97 if (v < 10) {
98 return v + '0';
99 } else {
100 return v - 10 + 'a';
101 }
102}
103
Alex Bennée9f567872023-03-02 18:57:42 -0800104/*
Philippe Mathieu-Daudé3f7d1bd2023-10-04 11:06:22 +0200105 * Connection helpers for both system and user backends
Alex Bennée36e067b2023-03-02 18:57:45 -0800106 */
107
108void gdb_put_strbuf(void);
109int gdb_put_packet(const char *buf);
110int gdb_put_packet_binary(const char *buf, int len, bool dump);
111void gdb_hextomem(GByteArray *mem, const char *buf, int len);
112void gdb_memtohex(GString *buf, const uint8_t *mem, int len);
113void gdb_memtox(GString *buf, const char *mem, int len);
114void gdb_read_byte(uint8_t ch);
115
Alex Bennéea7e0f9b2023-03-02 18:57:49 -0800116/*
117 * Packet acknowledgement - we handle this slightly differently
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100118 * between user and system mode, mainly to deal with the differences
Alex Bennéea7e0f9b2023-03-02 18:57:49 -0800119 * between the flexible chardev and the direct fd approaches.
120 *
121 * We currently don't support a negotiated QStartNoAckMode
122 */
123
124/**
125 * gdb_got_immediate_ack() - check ok to continue
126 *
127 * Returns true to continue, false to re-transmit for user only, the
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100128 * system stub always returns true.
Alex Bennéea7e0f9b2023-03-02 18:57:49 -0800129 */
130bool gdb_got_immediate_ack(void);
Alex Bennée36e067b2023-03-02 18:57:45 -0800131/* utility helpers */
Ilya Leoshkevicha3fcc112023-06-30 19:04:19 +0100132GDBProcess *gdb_get_process(uint32_t pid);
133CPUState *gdb_get_first_cpu_in_process(GDBProcess *process);
Alex Bennée36e067b2023-03-02 18:57:45 -0800134CPUState *gdb_first_attached_cpu(void);
135void gdb_append_thread_id(CPUState *cpu, GString *buf);
136int gdb_get_cpu_index(CPUState *cpu);
Alex Bennée7ea0c332023-03-02 18:57:52 -0800137unsigned int gdb_get_max_cpus(void); /* both */
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100138bool gdb_can_reverse(void); /* system emulation, stub for user */
Ilya Leoshkevich4aad0962024-02-07 16:38:08 +0000139int gdb_target_sigtrap(void); /* user */
Alex Bennée36e067b2023-03-02 18:57:45 -0800140
Alex Bennée36e067b2023-03-02 18:57:45 -0800141void gdb_create_default_process(GDBState *s);
142
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100143/* signal mapping, common for system, specialised for user-mode */
Alex Bennéed96bf492023-03-02 18:57:47 -0800144int gdb_signal_to_target(int sig);
145int gdb_target_signal_to_gdb(int sig);
146
147int gdb_get_char(void); /* user only */
148
149/**
150 * gdb_continue() - handle continue in mode specific way.
151 */
152void gdb_continue(void);
153
154/**
155 * gdb_continue_partial() - handle partial continue in mode specific way.
156 */
157int gdb_continue_partial(char *newstates);
158
Alex Bennée36e067b2023-03-02 18:57:45 -0800159/*
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100160 * Helpers with separate system and user implementations
Alex Bennée36e067b2023-03-02 18:57:45 -0800161 */
162void gdb_put_buffer(const uint8_t *buf, int len);
163
164/*
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100165 * Command handlers - either specialised or system or user only
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800166 */
167void gdb_init_gdbserver_state(void);
168
169typedef enum GDBThreadIdKind {
170 GDB_ONE_THREAD = 0,
171 GDB_ALL_THREADS, /* One process, all threads */
172 GDB_ALL_PROCESSES,
173 GDB_READ_THREAD_ERR
174} GDBThreadIdKind;
175
176typedef union GdbCmdVariant {
177 const char *data;
178 uint8_t opcode;
179 unsigned long val_ul;
180 unsigned long long val_ull;
181 struct {
182 GDBThreadIdKind kind;
183 uint32_t pid;
184 uint32_t tid;
185 } thread_id;
186} GdbCmdVariant;
187
188#define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
189
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100190void gdb_handle_query_rcmd(GArray *params, void *user_ctx); /* system */
Alex Bennéed96bf492023-03-02 18:57:47 -0800191void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */
192void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */
Gustavo Romero9ae58012024-03-09 03:09:00 +0000193void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx); /*user */
Ilya Leoshkeviche2820102023-06-30 19:04:21 +0100194void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */
195void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */
196void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */
197void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */
198void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */
Ilya Leoshkevich046f1432024-02-07 16:38:11 +0000199void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx); /* user */
Ilya Leoshkevich6d923112024-03-05 12:09:45 +0000200void gdb_handle_query_supported_user(const char *gdb_supported); /* user */
Ilya Leoshkeviche454f2f2024-03-05 12:09:46 +0000201bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid); /* user */
Ilya Leoshkevich539cb4e2024-03-05 12:09:47 +0000202bool gdb_handle_detach_user(uint32_t pid); /* user */
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800203
Alex Bennée8a2025b2023-03-02 18:57:50 -0800204void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */
205
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100206/* system only */
Alex Bennée589a5862023-03-02 18:57:51 -0800207void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *user_ctx);
208void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx);
209
Alex Bennéec5660802023-03-02 18:57:57 -0800210/* sycall handling */
211void gdb_handle_file_io(GArray *params, void *user_ctx);
212bool gdb_handled_syscall(void);
213void gdb_disable_syscalls(void);
214void gdb_syscall_reset(void);
215
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100216/* user/system specific syscall handling */
Alex Bennée131f3872023-03-02 18:58:01 -0800217void gdb_syscall_handling(const char *syscall_packet);
218
Alex Bennéeb6fa2ec2023-03-02 18:57:46 -0800219/*
Philippe Mathieu-Daudé25f34eb2024-03-13 21:09:38 +0100220 * Break/Watch point support - there is an implementation for system
Alex Bennée9f567872023-03-02 18:57:42 -0800221 * and user mode.
222 */
Alex Bennéea48e7d92022-09-29 12:42:25 +0100223bool gdb_supports_guest_debug(void);
Philippe Mathieu-Daudé55b5b8e2022-12-06 16:20:27 +0100224int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len);
225int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len);
Alex Bennéeae7467b2022-09-29 12:42:24 +0100226void gdb_breakpoint_remove_all(CPUState *cs);
227
Alex Bennée589a5862023-03-02 18:57:51 -0800228/**
229 * gdb_target_memory_rw_debug() - handle debug access to memory
230 * @cs: CPUState
231 * @addr: nominal address, could be an entire physical address
232 * @buf: data
233 * @len: length of access
234 * @is_write: is it a write operation
235 *
236 * This function is specialised depending on the mode we are running
Philippe Mathieu-Daudé3f7d1bd2023-10-04 11:06:22 +0200237 * in. For system guests we can switch the interpretation of the
Alex Bennée589a5862023-03-02 18:57:51 -0800238 * address to a physical address.
239 */
240int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
241 uint8_t *buf, int len, bool is_write);
242
Alex Bennée97748552023-03-02 18:57:37 -0800243#endif /* GDBSTUB_INTERNALS_H */