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 char target_xml
[1024];
49 typedef 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
];
57 int line_sum
; /* running checksum */
58 int line_csum
; /* checksum at the end of the packet */
59 GByteArray
*last_packet
;
62 GDBProcess
*processes
;
67 int supported_sstep_flags
;
70 /* lives in main gdbstub.c */
71 extern GDBState gdbserver_state
;
74 * Inline utility function, convert from int to hex and back
77 static inline int fromhex(int v
)
79 if (v
>= '0' && v
<= '9') {
81 } else if (v
>= 'A' && v
<= 'F') {
83 } else if (v
>= 'a' && v
<= 'f') {
90 static inline int tohex(int v
)
100 * Connection helpers for both softmmu and user backends
103 void gdb_put_strbuf(void);
104 int gdb_put_packet(const char *buf
);
105 int gdb_put_packet_binary(const char *buf
, int len
, bool dump
);
106 void gdb_hextomem(GByteArray
*mem
, const char *buf
, int len
);
107 void gdb_memtohex(GString
*buf
, const uint8_t *mem
, int len
);
108 void gdb_memtox(GString
*buf
, const char *mem
, int len
);
109 void gdb_read_byte(uint8_t ch
);
112 * Packet acknowledgement - we handle this slightly differently
113 * between user and softmmu mode, mainly to deal with the differences
114 * between the flexible chardev and the direct fd approaches.
116 * We currently don't support a negotiated QStartNoAckMode
120 * gdb_got_immediate_ack() - check ok to continue
122 * Returns true to continue, false to re-transmit for user only, the
123 * softmmu stub always returns true.
125 bool gdb_got_immediate_ack(void);
126 /* utility helpers */
127 CPUState
*gdb_first_attached_cpu(void);
128 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
);
129 int gdb_get_cpu_index(CPUState
*cpu
);
130 unsigned int gdb_get_max_cpus(void); /* both */
131 bool gdb_can_reverse(void); /* softmmu, stub for user */
133 void gdb_create_default_process(GDBState
*s
);
135 /* signal mapping, common for softmmu, specialised for user-mode */
136 int gdb_signal_to_target(int sig
);
137 int gdb_target_signal_to_gdb(int sig
);
139 int gdb_get_char(void); /* user only */
142 * gdb_continue() - handle continue in mode specific way.
144 void gdb_continue(void);
147 * gdb_continue_partial() - handle partial continue in mode specific way.
149 int gdb_continue_partial(char *newstates
);
152 * Helpers with separate softmmu and user implementations
154 void gdb_put_buffer(const uint8_t *buf
, int len
);
157 * Command handlers - either specialised or softmmu or user only
159 void gdb_init_gdbserver_state(void);
161 typedef enum GDBThreadIdKind
{
163 GDB_ALL_THREADS
, /* One process, all threads */
168 typedef union GdbCmdVariant
{
171 unsigned long val_ul
;
172 unsigned long long val_ull
;
174 GDBThreadIdKind kind
;
180 #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
182 void gdb_handle_query_rcmd(GArray
*params
, void *user_ctx
); /* softmmu */
183 void gdb_handle_query_offsets(GArray
*params
, void *user_ctx
); /* user */
184 void gdb_handle_query_xfer_auxv(GArray
*params
, void *user_ctx
); /*user */
186 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
); /* both */
189 void gdb_handle_query_qemu_phy_mem_mode(GArray
*params
, void *user_ctx
);
190 void gdb_handle_set_qemu_phy_mem_mode(GArray
*params
, void *user_ctx
);
192 /* sycall handling */
193 void gdb_handle_file_io(GArray
*params
, void *user_ctx
);
194 bool gdb_handled_syscall(void);
195 void gdb_disable_syscalls(void);
196 void gdb_syscall_reset(void);
198 /* user/softmmu specific syscall handling */
199 void gdb_syscall_handling(const char *syscall_packet
);
202 * Break/Watch point support - there is an implementation for softmmu
205 bool gdb_supports_guest_debug(void);
206 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
);
207 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
);
208 void gdb_breakpoint_remove_all(CPUState
*cs
);
211 * gdb_target_memory_rw_debug() - handle debug access to memory
213 * @addr: nominal address, could be an entire physical address
215 * @len: length of access
216 * @is_write: is it a write operation
218 * This function is specialised depending on the mode we are running
219 * in. For softmmu guests we can switch the interpretation of the
220 * address to a physical address.
222 int gdb_target_memory_rw_debug(CPUState
*cs
, hwaddr addr
,
223 uint8_t *buf
, int len
, bool is_write
);
225 #endif /* GDBSTUB_INTERNALS_H */