4 * Copyright (c) 2022 Linaro Ltd
6 * SPDX-License-Identifier: GPL-2.0-or-later
9 #ifndef GDBSTUB_INTERNALS_H
10 #define GDBSTUB_INTERNALS_H
12 #include "exec/cpu-common.h"
14 #define MAX_PACKET_LENGTH 4096
17 * Shared structures and definitions
29 GDB_SIGNAL_UNKNOWN
= 143
32 typedef struct GDBProcess
{
36 /* If gdb sends qXfer:features:read:target.xml this will be populated */
50 typedef struct GDBState
{
51 bool init
; /* have we been initialised? */
52 CPUState
*c_cpu
; /* current CPU for step/continue ops */
53 CPUState
*g_cpu
; /* current CPU for other ops */
54 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
55 enum RSState state
; /* parsing state */
56 char line_buf
[MAX_PACKET_LENGTH
];
58 int line_sum
; /* running checksum */
59 int line_csum
; /* checksum at the end of the packet */
60 GByteArray
*last_packet
;
63 GDBProcess
*processes
;
68 int supported_sstep_flags
;
70 * Whether we are allowed to send a stop reply packet at this moment.
71 * Must be set off after sending the stop reply itself.
73 bool allow_stop_reply
;
76 /* lives in main gdbstub.c */
77 extern GDBState gdbserver_state
;
80 * Inline utility function, convert from int to hex and back
83 static inline int fromhex(int v
)
85 if (v
>= '0' && v
<= '9') {
87 } else if (v
>= 'A' && v
<= 'F') {
89 } else if (v
>= 'a' && v
<= 'f') {
96 static inline int tohex(int v
)
106 * Connection helpers for both system and user backends
109 void gdb_put_strbuf(void);
110 int gdb_put_packet(const char *buf
);
111 int gdb_put_packet_binary(const char *buf
, int len
, bool dump
);
112 void gdb_hextomem(GByteArray
*mem
, const char *buf
, int len
);
113 void gdb_memtohex(GString
*buf
, const uint8_t *mem
, int len
);
114 void gdb_memtox(GString
*buf
, const char *mem
, int len
);
115 void gdb_read_byte(uint8_t ch
);
118 * Packet acknowledgement - we handle this slightly differently
119 * between user and softmmu mode, mainly to deal with the differences
120 * between the flexible chardev and the direct fd approaches.
122 * We currently don't support a negotiated QStartNoAckMode
126 * gdb_got_immediate_ack() - check ok to continue
128 * Returns true to continue, false to re-transmit for user only, the
129 * softmmu stub always returns true.
131 bool gdb_got_immediate_ack(void);
132 /* utility helpers */
133 GDBProcess
*gdb_get_process(uint32_t pid
);
134 CPUState
*gdb_get_first_cpu_in_process(GDBProcess
*process
);
135 CPUState
*gdb_first_attached_cpu(void);
136 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
);
137 int gdb_get_cpu_index(CPUState
*cpu
);
138 unsigned int gdb_get_max_cpus(void); /* both */
139 bool gdb_can_reverse(void); /* softmmu, stub for user */
141 void gdb_create_default_process(GDBState
*s
);
143 /* signal mapping, common for softmmu, specialised for user-mode */
144 int gdb_signal_to_target(int sig
);
145 int gdb_target_signal_to_gdb(int sig
);
147 int gdb_get_char(void); /* user only */
150 * gdb_continue() - handle continue in mode specific way.
152 void gdb_continue(void);
155 * gdb_continue_partial() - handle partial continue in mode specific way.
157 int gdb_continue_partial(char *newstates
);
160 * Helpers with separate softmmu and user implementations
162 void gdb_put_buffer(const uint8_t *buf
, int len
);
165 * Command handlers - either specialised or softmmu or user only
167 void gdb_init_gdbserver_state(void);
169 typedef enum GDBThreadIdKind
{
171 GDB_ALL_THREADS
, /* One process, all threads */
176 typedef union GdbCmdVariant
{
179 unsigned long val_ul
;
180 unsigned long long val_ull
;
182 GDBThreadIdKind kind
;
188 #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
190 void gdb_handle_query_rcmd(GArray
*params
, void *user_ctx
); /* softmmu */
191 void gdb_handle_query_offsets(GArray
*params
, void *user_ctx
); /* user */
192 void gdb_handle_query_xfer_auxv(GArray
*params
, void *user_ctx
); /*user */
193 void gdb_handle_v_file_open(GArray
*params
, void *user_ctx
); /* user */
194 void gdb_handle_v_file_close(GArray
*params
, void *user_ctx
); /* user */
195 void gdb_handle_v_file_pread(GArray
*params
, void *user_ctx
); /* user */
196 void gdb_handle_v_file_readlink(GArray
*params
, void *user_ctx
); /* user */
197 void gdb_handle_query_xfer_exec_file(GArray
*params
, void *user_ctx
); /* user */
199 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
); /* both */
202 void gdb_handle_query_qemu_phy_mem_mode(GArray
*params
, void *user_ctx
);
203 void gdb_handle_set_qemu_phy_mem_mode(GArray
*params
, void *user_ctx
);
205 /* sycall handling */
206 void gdb_handle_file_io(GArray
*params
, void *user_ctx
);
207 bool gdb_handled_syscall(void);
208 void gdb_disable_syscalls(void);
209 void gdb_syscall_reset(void);
211 /* user/softmmu specific syscall handling */
212 void gdb_syscall_handling(const char *syscall_packet
);
215 * Break/Watch point support - there is an implementation for softmmu
218 bool gdb_supports_guest_debug(void);
219 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
);
220 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
);
221 void gdb_breakpoint_remove_all(CPUState
*cs
);
224 * gdb_target_memory_rw_debug() - handle debug access to memory
226 * @addr: nominal address, could be an entire physical address
228 * @len: length of access
229 * @is_write: is it a write operation
231 * This function is specialised depending on the mode we are running
232 * in. For system guests we can switch the interpretation of the
233 * address to a physical address.
235 int gdb_target_memory_rw_debug(CPUState
*cs
, hwaddr addr
,
236 uint8_t *buf
, int len
, bool is_write
);
238 #endif /* GDBSTUB_INTERNALS_H */