gdbstub.c (379b42e8b7681ebea45cde4af8ffd1694f98949d) gdbstub.c (c566080cd37fe328077a3c49d7fd248ce2a06bfe)
1/*
2 * gdb server stub
3 *
4 * This implements a subset of the remote protocol as described in:
5 *
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
7 *
8 * Copyright (c) 2003-2005 Fabrice Bellard

--- 15 unchanged lines hidden (view full) ---

24 */
25
26#include "qemu/osdep.h"
27#include "qemu/ctype.h"
28#include "qemu/cutils.h"
29#include "qemu/module.h"
30#include "trace.h"
31#include "exec/gdbstub.h"
1/*
2 * gdb server stub
3 *
4 * This implements a subset of the remote protocol as described in:
5 *
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
7 *
8 * Copyright (c) 2003-2005 Fabrice Bellard

--- 15 unchanged lines hidden (view full) ---

24 */
25
26#include "qemu/osdep.h"
27#include "qemu/ctype.h"
28#include "qemu/cutils.h"
29#include "qemu/module.h"
30#include "trace.h"
31#include "exec/gdbstub.h"
32#include "gdbstub/syscalls.h"
32#ifdef CONFIG_USER_ONLY
33#include "gdbstub/user.h"
34#else
35#include "hw/cpu/cluster.h"
36#include "hw/boards.h"
37#endif
38
39#include "sysemu/hw_accel.h"
40#include "sysemu/runstate.h"
33#ifdef CONFIG_USER_ONLY
34#include "gdbstub/user.h"
35#else
36#include "hw/cpu/cluster.h"
37#include "hw/boards.h"
38#endif
39
40#include "sysemu/hw_accel.h"
41#include "sysemu/runstate.h"
41#include "semihosting/semihost.h"
42#include "exec/exec-all.h"
43#include "exec/replay-core.h"
44#include "exec/tb-flush.h"
45#include "exec/hwaddr.h"
46
47#include "internals.h"
48
49typedef struct GDBRegisterState {

--- 23 unchanged lines hidden (view full) ---

73 */
74 gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
75 gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
76 gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
77}
78
79bool gdb_has_xml;
80
42#include "exec/exec-all.h"
43#include "exec/replay-core.h"
44#include "exec/tb-flush.h"
45#include "exec/hwaddr.h"
46
47#include "internals.h"
48
49typedef struct GDBRegisterState {

--- 23 unchanged lines hidden (view full) ---

73 */
74 gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
75 gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
76 gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
77}
78
79bool gdb_has_xml;
80
81/*
82 * Return true if there is a GDB currently connected to the stub
83 * and attached to a CPU
84 */
85static bool gdb_attached(void)
86{
87 return gdbserver_state.init && gdbserver_state.c_cpu;
88}
89
90static enum {
91 GDB_SYS_UNKNOWN,
92 GDB_SYS_ENABLED,
93 GDB_SYS_DISABLED,
94} gdb_syscall_mode;
95
96/* Decide if either remote gdb syscalls or native file IO should be used. */
97int use_gdb_syscalls(void)
98{
99 SemihostingTarget target = semihosting_get_target();
100 if (target == SEMIHOSTING_TARGET_NATIVE) {
101 /* -semihosting-config target=native */
102 return false;
103 } else if (target == SEMIHOSTING_TARGET_GDB) {
104 /* -semihosting-config target=gdb */
105 return true;
106 }
107
108 /* -semihosting-config target=auto */
109 /* On the first call check if gdb is connected and remember. */
110 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
111 gdb_syscall_mode = gdb_attached() ? GDB_SYS_ENABLED : GDB_SYS_DISABLED;
112 }
113 return gdb_syscall_mode == GDB_SYS_ENABLED;
114}
115
116/* writes 2*len+1 bytes in buf */
117void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
118{
119 int i, c;
120 for(i = 0; i < len; i++) {
121 c = mem[i];
122 g_string_append_c(buf, tohex(c >> 4));
123 g_string_append_c(buf, tohex(c & 0xf));

--- 793 unchanged lines hidden (view full) ---

917 }
918
919 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
920 gdbserver_state.g_cpu = gdb_first_attached_cpu();
921 }
922
923 if (!gdbserver_state.c_cpu) {
924 /* No more process attached */
81/* writes 2*len+1 bytes in buf */
82void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
83{
84 int i, c;
85 for(i = 0; i < len; i++) {
86 c = mem[i];
87 g_string_append_c(buf, tohex(c >> 4));
88 g_string_append_c(buf, tohex(c & 0xf));

--- 793 unchanged lines hidden (view full) ---

882 }
883
884 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
885 gdbserver_state.g_cpu = gdb_first_attached_cpu();
886 }
887
888 if (!gdbserver_state.c_cpu) {
889 /* No more process attached */
925 gdb_syscall_mode = GDB_SYS_DISABLED;
890 gdb_disable_syscalls();
926 gdb_continue();
927 }
928 gdb_put_packet("OK");
929}
930
931static void handle_thread_alive(GArray *params, void *user_ctx)
932{
933 CPUState *cpu;

--- 296 unchanged lines hidden (view full) ---

1230 reg_id);
1231 }
1232 g_assert(len == gdbserver_state.mem_buf->len);
1233
1234 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
1235 gdb_put_strbuf();
1236}
1237
891 gdb_continue();
892 }
893 gdb_put_packet("OK");
894}
895
896static void handle_thread_alive(GArray *params, void *user_ctx)
897{
898 CPUState *cpu;

--- 296 unchanged lines hidden (view full) ---

1195 reg_id);
1196 }
1197 g_assert(len == gdbserver_state.mem_buf->len);
1198
1199 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
1200 gdb_put_strbuf();
1201}
1202
1238static void handle_file_io(GArray *params, void *user_ctx)
1239{
1240 if (params->len >= 1 && gdbserver_state.current_syscall_cb) {
1241 uint64_t ret;
1242 int err;
1243
1203
1244 ret = get_param(params, 0)->val_ull;
1245 if (params->len >= 2) {
1246 err = get_param(params, 1)->val_ull;
1247 } else {
1248 err = 0;
1249 }
1250
1251 /* Convert GDB error numbers back to host error numbers. */
1252#define E(X) case GDB_E##X: err = E##X; break
1253 switch (err) {
1254 case 0:
1255 break;
1256 E(PERM);
1257 E(NOENT);
1258 E(INTR);
1259 E(BADF);
1260 E(ACCES);
1261 E(FAULT);
1262 E(BUSY);
1263 E(EXIST);
1264 E(NODEV);
1265 E(NOTDIR);
1266 E(ISDIR);
1267 E(INVAL);
1268 E(NFILE);
1269 E(MFILE);
1270 E(FBIG);
1271 E(NOSPC);
1272 E(SPIPE);
1273 E(ROFS);
1274 E(NAMETOOLONG);
1275 default:
1276 err = EINVAL;
1277 break;
1278 }
1279#undef E
1280
1281 gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
1282 gdbserver_state.current_syscall_cb = NULL;
1283 }
1284
1285 if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') {
1286 gdb_put_packet("T02");
1287 return;
1288 }
1289
1290 gdb_continue();
1291}
1292
1293static void handle_step(GArray *params, void *user_ctx)
1294{
1295 if (params->len) {
1296 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
1297 }
1298
1299 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
1300 gdb_continue();

--- 588 unchanged lines hidden (view full) ---

1889 .schema = "o0"
1890 };
1891 cmd_parser = &backward_cmd_desc;
1892 }
1893 break;
1894 case 'F':
1895 {
1896 static const GdbCmdParseEntry file_io_cmd_desc = {
1204static void handle_step(GArray *params, void *user_ctx)
1205{
1206 if (params->len) {
1207 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
1208 }
1209
1210 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
1211 gdb_continue();

--- 588 unchanged lines hidden (view full) ---

1800 .schema = "o0"
1801 };
1802 cmd_parser = &backward_cmd_desc;
1803 }
1804 break;
1805 case 'F':
1806 {
1807 static const GdbCmdParseEntry file_io_cmd_desc = {
1897 .handler = handle_file_io,
1808 .handler = gdb_handle_file_io,
1898 .cmd = "F",
1899 .cmd_startswith = 1,
1900 .schema = "L,L,o0"
1901 };
1902 cmd_parser = &file_io_cmd_desc;
1903 }
1904 break;
1905 case 'g':

--- 151 unchanged lines hidden (view full) ---

2057 */
2058 return;
2059 }
2060
2061 gdbserver_state.c_cpu = cpu;
2062 gdbserver_state.g_cpu = cpu;
2063}
2064
1809 .cmd = "F",
1810 .cmd_startswith = 1,
1811 .schema = "L,L,o0"
1812 };
1813 cmd_parser = &file_io_cmd_desc;
1814 }
1815 break;
1816 case 'g':

--- 151 unchanged lines hidden (view full) ---

1968 */
1969 return;
1970 }
1971
1972 gdbserver_state.c_cpu = cpu;
1973 gdbserver_state.g_cpu = cpu;
1974}
1975
2065/* Send a gdb syscall request.
2066 This accepts limited printf-style format specifiers, specifically:
2067 %x - target_ulong argument printed in hex.
2068 %lx - 64-bit argument printed in hex.
2069 %s - string pointer (target_ulong) and length (int) pair. */
2070void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
2071{
2072 char *p;
2073 char *p_end;
2074 target_ulong addr;
2075 uint64_t i64;
2076
2077 if (!gdb_attached()) {
2078 return;
2079 }
2080
2081 gdbserver_state.current_syscall_cb = cb;
2082#ifndef CONFIG_USER_ONLY
2083 vm_stop(RUN_STATE_DEBUG);
2084#endif
2085 p = &gdbserver_state.syscall_buf[0];
2086 p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
2087 *(p++) = 'F';
2088 while (*fmt) {
2089 if (*fmt == '%') {
2090 fmt++;
2091 switch (*fmt++) {
2092 case 'x':
2093 addr = va_arg(va, target_ulong);
2094 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
2095 break;
2096 case 'l':
2097 if (*(fmt++) != 'x')
2098 goto bad_format;
2099 i64 = va_arg(va, uint64_t);
2100 p += snprintf(p, p_end - p, "%" PRIx64, i64);
2101 break;
2102 case 's':
2103 addr = va_arg(va, target_ulong);
2104 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
2105 addr, va_arg(va, int));
2106 break;
2107 default:
2108 bad_format:
2109 error_report("gdbstub: Bad syscall format string '%s'",
2110 fmt - 1);
2111 break;
2112 }
2113 } else {
2114 *(p++) = *(fmt++);
2115 }
2116 }
2117 *p = 0;
2118#ifdef CONFIG_USER_ONLY
2119 gdb_put_packet(gdbserver_state.syscall_buf);
2120 /* Return control to gdb for it to process the syscall request.
2121 * Since the protocol requires that gdb hands control back to us
2122 * using a "here are the results" F packet, we don't need to check
2123 * gdb_handlesig's return value (which is the signal to deliver if
2124 * execution was resumed via a continue packet).
2125 */
2126 gdb_handlesig(gdbserver_state.c_cpu, 0);
2127#else
2128 /* In this case wait to send the syscall packet until notification that
2129 the CPU has stopped. This must be done because if the packet is sent
2130 now the reply from the syscall request could be received while the CPU
2131 is still in the running state, which can cause packets to be dropped
2132 and state transition 'T' packets to be sent while the syscall is still
2133 being processed. */
2134 qemu_cpu_kick(gdbserver_state.c_cpu);
2135#endif
2136}
2137
2138void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2139{
2140 va_list va;
2141
2142 va_start(va, fmt);
2143 gdb_do_syscallv(cb, fmt, va);
2144 va_end(va);
2145}
2146
2147void gdb_read_byte(uint8_t ch)
2148{
2149 uint8_t reply;
2150
2151#ifndef CONFIG_USER_ONLY
2152 if (gdbserver_state.last_packet->len) {
2153 /* Waiting for a response to the last packet. If we see the start
2154 of a new command then abandon the previous response. */

--- 164 unchanged lines hidden ---
1976void gdb_read_byte(uint8_t ch)
1977{
1978 uint8_t reply;
1979
1980#ifndef CONFIG_USER_ONLY
1981 if (gdbserver_state.last_packet->len) {
1982 /* Waiting for a response to the last packet. If we see the start
1983 of a new command then abandon the previous response. */

--- 164 unchanged lines hidden ---