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
{
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
];
56 int line_sum
; /* running checksum */
57 int line_csum
; /* checksum at the end of the packet */
58 GByteArray
*last_packet
;
61 GDBProcess
*processes
;
66 int supported_sstep_flags
;
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.
71 bool allow_stop_reply
;
74 /* lives in main gdbstub.c */
75 extern GDBState gdbserver_state
;
78 * Inline utility function, convert from int to hex and back
81 static inline int fromhex(int v
)
83 if (v
>= '0' && v
<= '9') {
85 } else if (v
>= 'A' && v
<= 'F') {
87 } else if (v
>= 'a' && v
<= 'f') {
94 static inline int tohex(int v
)
104 * Connection helpers for both system and user backends
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
);
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.
120 * We currently don't support a negotiated QStartNoAckMode
124 * gdb_got_immediate_ack() - check ok to continue
126 * Returns true to continue, false to re-transmit for user only, the
127 * softmmu stub always returns true.
129 bool gdb_got_immediate_ack(void);
130 /* utility helpers */
131 GDBProcess
*gdb_get_process(uint32_t pid
);
132 CPUState
*gdb_get_first_cpu_in_process(GDBProcess
*process
);
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
);
136 unsigned int gdb_get_max_cpus(void); /* both */
137 bool gdb_can_reverse(void); /* softmmu, stub for user */
139 void gdb_create_default_process(GDBState
*s
);
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
);
145 int gdb_get_char(void); /* user only */
148 * gdb_continue() - handle continue in mode specific way.
150 void gdb_continue(void);
153 * gdb_continue_partial() - handle partial continue in mode specific way.
155 int gdb_continue_partial(char *newstates
);
158 * Helpers with separate softmmu and user implementations
160 void gdb_put_buffer(const uint8_t *buf
, int len
);
163 * Command handlers - either specialised or softmmu or user only
165 void gdb_init_gdbserver_state(void);
167 typedef enum GDBThreadIdKind
{
169 GDB_ALL_THREADS
, /* One process, all threads */
174 typedef union GdbCmdVariant
{
177 unsigned long val_ul
;
178 unsigned long long val_ull
;
180 GDBThreadIdKind kind
;
186 #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
188 void gdb_handle_query_rcmd(GArray
*params
, void *user_ctx
); /* softmmu */
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 */
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 */
197 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
); /* both */
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
);
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);
209 /* user/softmmu specific syscall handling */
210 void gdb_syscall_handling(const char *syscall_packet
);
213 * Break/Watch point support - there is an implementation for softmmu
216 bool gdb_supports_guest_debug(void);
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
);
219 void gdb_breakpoint_remove_all(CPUState
*cs
);
222 * gdb_target_memory_rw_debug() - handle debug access to memory
224 * @addr: nominal address, could be an entire physical address
226 * @len: length of access
227 * @is_write: is it a write operation
229 * This function is specialised depending on the mode we are running
230 * in. For system guests we can switch the interpretation of the
231 * address to a physical address.
233 int gdb_target_memory_rw_debug(CPUState
*cs
, hwaddr addr
,
234 uint8_t *buf
, int len
, bool is_write
);
236 #endif /* GDBSTUB_INTERNALS_H */