1 /* 2 * gdbstub user-mode helper routines. 3 * 4 * We know for user-mode we are using TCG so we can call stuff directly. 5 * 6 * Copyright (c) 2003-2005 Fabrice Bellard 7 * Copyright (c) 2022 Linaro Ltd 8 * 9 * SPDX-License-Identifier: LGPL-2.0+ 10 */ 11 12 #include "qemu/osdep.h" 13 #include "qemu/cutils.h" 14 #include "qemu/sockets.h" 15 #include "exec/hwaddr.h" 16 #include "exec/tb-flush.h" 17 #include "exec/gdbstub.h" 18 #include "gdbstub/syscalls.h" 19 #include "gdbstub/user.h" 20 #include "hw/core/cpu.h" 21 #include "trace.h" 22 #include "internals.h" 23 24 /* User-mode specific state */ 25 typedef struct { 26 int fd; 27 char *socket_path; 28 int running_state; 29 } GDBUserState; 30 31 static GDBUserState gdbserver_user_state; 32 33 int gdb_get_char(void) 34 { 35 uint8_t ch; 36 int ret; 37 38 for (;;) { 39 ret = recv(gdbserver_user_state.fd, &ch, 1, 0); 40 if (ret < 0) { 41 if (errno == ECONNRESET) { 42 gdbserver_user_state.fd = -1; 43 } 44 if (errno != EINTR) { 45 return -1; 46 } 47 } else if (ret == 0) { 48 close(gdbserver_user_state.fd); 49 gdbserver_user_state.fd = -1; 50 return -1; 51 } else { 52 break; 53 } 54 } 55 return ch; 56 } 57 58 bool gdb_got_immediate_ack(void) 59 { 60 int i; 61 62 i = gdb_get_char(); 63 if (i < 0) { 64 /* no response, continue anyway */ 65 return true; 66 } 67 68 if (i == '+') { 69 /* received correctly, continue */ 70 return true; 71 } 72 73 /* anything else, including '-' then try again */ 74 return false; 75 } 76 77 void gdb_put_buffer(const uint8_t *buf, int len) 78 { 79 int ret; 80 81 while (len > 0) { 82 ret = send(gdbserver_user_state.fd, buf, len, 0); 83 if (ret < 0) { 84 if (errno != EINTR) { 85 return; 86 } 87 } else { 88 buf += ret; 89 len -= ret; 90 } 91 } 92 } 93 94 /* Tell the remote gdb that the process has exited. */ 95 void gdb_exit(int code) 96 { 97 char buf[4]; 98 99 if (!gdbserver_state.init) { 100 return; 101 } 102 if (gdbserver_user_state.socket_path) { 103 unlink(gdbserver_user_state.socket_path); 104 } 105 if (gdbserver_user_state.fd < 0) { 106 return; 107 } 108 109 trace_gdbstub_op_exiting((uint8_t)code); 110 111 if (gdbserver_state.allow_stop_reply) { 112 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); 113 gdb_put_packet(buf); 114 gdbserver_state.allow_stop_reply = false; 115 } 116 } 117 118 int gdb_handlesig(CPUState *cpu, int sig) 119 { 120 char buf[256]; 121 int n; 122 123 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) { 124 return sig; 125 } 126 127 /* disable single step if it was enabled */ 128 cpu_single_step(cpu, 0); 129 tb_flush(cpu); 130 131 if (sig != 0) { 132 gdb_set_stop_cpu(cpu); 133 if (gdbserver_state.allow_stop_reply) { 134 g_string_printf(gdbserver_state.str_buf, 135 "T%02xthread:", gdb_target_signal_to_gdb(sig)); 136 gdb_append_thread_id(cpu, gdbserver_state.str_buf); 137 g_string_append_c(gdbserver_state.str_buf, ';'); 138 gdb_put_strbuf(); 139 gdbserver_state.allow_stop_reply = false; 140 } 141 } 142 /* 143 * gdb_put_packet() might have detected that the peer terminated the 144 * connection. 145 */ 146 if (gdbserver_user_state.fd < 0) { 147 return sig; 148 } 149 150 sig = 0; 151 gdbserver_state.state = RS_IDLE; 152 gdbserver_user_state.running_state = 0; 153 while (gdbserver_user_state.running_state == 0) { 154 n = read(gdbserver_user_state.fd, buf, 256); 155 if (n > 0) { 156 int i; 157 158 for (i = 0; i < n; i++) { 159 gdb_read_byte(buf[i]); 160 } 161 } else { 162 /* 163 * XXX: Connection closed. Should probably wait for another 164 * connection before continuing. 165 */ 166 if (n == 0) { 167 close(gdbserver_user_state.fd); 168 } 169 gdbserver_user_state.fd = -1; 170 return sig; 171 } 172 } 173 sig = gdbserver_state.signal; 174 gdbserver_state.signal = 0; 175 return sig; 176 } 177 178 /* Tell the remote gdb that the process has exited due to SIG. */ 179 void gdb_signalled(CPUArchState *env, int sig) 180 { 181 char buf[4]; 182 183 if (!gdbserver_state.init || gdbserver_user_state.fd < 0 || 184 !gdbserver_state.allow_stop_reply) { 185 return; 186 } 187 188 snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig)); 189 gdb_put_packet(buf); 190 gdbserver_state.allow_stop_reply = false; 191 } 192 193 static void gdb_accept_init(int fd) 194 { 195 gdb_init_gdbserver_state(); 196 gdb_create_default_process(&gdbserver_state); 197 gdbserver_state.processes[0].attached = true; 198 gdbserver_state.c_cpu = gdb_first_attached_cpu(); 199 gdbserver_state.g_cpu = gdbserver_state.c_cpu; 200 gdbserver_user_state.fd = fd; 201 gdb_has_xml = false; 202 } 203 204 static bool gdb_accept_socket(int gdb_fd) 205 { 206 int fd; 207 208 for (;;) { 209 fd = accept(gdb_fd, NULL, NULL); 210 if (fd < 0 && errno != EINTR) { 211 perror("accept socket"); 212 return false; 213 } else if (fd >= 0) { 214 qemu_set_cloexec(fd); 215 break; 216 } 217 } 218 219 gdb_accept_init(fd); 220 return true; 221 } 222 223 static int gdbserver_open_socket(const char *path) 224 { 225 struct sockaddr_un sockaddr = {}; 226 int fd, ret; 227 228 fd = socket(AF_UNIX, SOCK_STREAM, 0); 229 if (fd < 0) { 230 perror("create socket"); 231 return -1; 232 } 233 234 sockaddr.sun_family = AF_UNIX; 235 pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path); 236 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); 237 if (ret < 0) { 238 perror("bind socket"); 239 close(fd); 240 return -1; 241 } 242 ret = listen(fd, 1); 243 if (ret < 0) { 244 perror("listen socket"); 245 close(fd); 246 return -1; 247 } 248 249 return fd; 250 } 251 252 static bool gdb_accept_tcp(int gdb_fd) 253 { 254 struct sockaddr_in sockaddr = {}; 255 socklen_t len; 256 int fd; 257 258 for (;;) { 259 len = sizeof(sockaddr); 260 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len); 261 if (fd < 0 && errno != EINTR) { 262 perror("accept"); 263 return false; 264 } else if (fd >= 0) { 265 qemu_set_cloexec(fd); 266 break; 267 } 268 } 269 270 /* set short latency */ 271 if (socket_set_nodelay(fd)) { 272 perror("setsockopt"); 273 close(fd); 274 return false; 275 } 276 277 gdb_accept_init(fd); 278 return true; 279 } 280 281 static int gdbserver_open_port(int port) 282 { 283 struct sockaddr_in sockaddr; 284 int fd, ret; 285 286 fd = socket(PF_INET, SOCK_STREAM, 0); 287 if (fd < 0) { 288 perror("socket"); 289 return -1; 290 } 291 qemu_set_cloexec(fd); 292 293 socket_set_fast_reuse(fd); 294 295 sockaddr.sin_family = AF_INET; 296 sockaddr.sin_port = htons(port); 297 sockaddr.sin_addr.s_addr = 0; 298 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); 299 if (ret < 0) { 300 perror("bind"); 301 close(fd); 302 return -1; 303 } 304 ret = listen(fd, 1); 305 if (ret < 0) { 306 perror("listen"); 307 close(fd); 308 return -1; 309 } 310 311 return fd; 312 } 313 314 int gdbserver_start(const char *port_or_path) 315 { 316 int port = g_ascii_strtoull(port_or_path, NULL, 10); 317 int gdb_fd; 318 319 if (port > 0) { 320 gdb_fd = gdbserver_open_port(port); 321 } else { 322 gdb_fd = gdbserver_open_socket(port_or_path); 323 } 324 325 if (gdb_fd < 0) { 326 return -1; 327 } 328 329 if (port > 0 && gdb_accept_tcp(gdb_fd)) { 330 return 0; 331 } else if (gdb_accept_socket(gdb_fd)) { 332 gdbserver_user_state.socket_path = g_strdup(port_or_path); 333 return 0; 334 } 335 336 /* gone wrong */ 337 close(gdb_fd); 338 return -1; 339 } 340 341 /* Disable gdb stub for child processes. */ 342 void gdbserver_fork(CPUState *cpu) 343 { 344 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) { 345 return; 346 } 347 close(gdbserver_user_state.fd); 348 gdbserver_user_state.fd = -1; 349 cpu_breakpoint_remove_all(cpu, BP_GDB); 350 /* no cpu_watchpoint_remove_all for user-mode */ 351 } 352 353 /* 354 * Execution state helpers 355 */ 356 357 void gdb_handle_query_attached(GArray *params, void *user_ctx) 358 { 359 gdb_put_packet("0"); 360 } 361 362 void gdb_continue(void) 363 { 364 gdbserver_user_state.running_state = 1; 365 trace_gdbstub_op_continue(); 366 } 367 368 /* 369 * Resume execution, for user-mode emulation it's equivalent to 370 * gdb_continue. 371 */ 372 int gdb_continue_partial(char *newstates) 373 { 374 CPUState *cpu; 375 int res = 0; 376 /* 377 * This is not exactly accurate, but it's an improvement compared to the 378 * previous situation, where only one CPU would be single-stepped. 379 */ 380 CPU_FOREACH(cpu) { 381 if (newstates[cpu->cpu_index] == 's') { 382 trace_gdbstub_op_stepping(cpu->cpu_index); 383 cpu_single_step(cpu, gdbserver_state.sstep_flags); 384 } 385 } 386 gdbserver_user_state.running_state = 1; 387 return res; 388 } 389 390 /* 391 * Memory access helpers 392 */ 393 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr, 394 uint8_t *buf, int len, bool is_write) 395 { 396 CPUClass *cc; 397 398 cc = CPU_GET_CLASS(cpu); 399 if (cc->memory_rw_debug) { 400 return cc->memory_rw_debug(cpu, addr, buf, len, is_write); 401 } 402 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); 403 } 404 405 /* 406 * cpu helpers 407 */ 408 409 unsigned int gdb_get_max_cpus(void) 410 { 411 CPUState *cpu; 412 unsigned int max_cpus = 1; 413 414 CPU_FOREACH(cpu) { 415 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus; 416 } 417 418 return max_cpus; 419 } 420 421 /* replay not supported for user-mode */ 422 bool gdb_can_reverse(void) 423 { 424 return false; 425 } 426 427 /* 428 * Break/Watch point helpers 429 */ 430 431 bool gdb_supports_guest_debug(void) 432 { 433 /* user-mode == TCG == supported */ 434 return true; 435 } 436 437 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len) 438 { 439 CPUState *cpu; 440 int err = 0; 441 442 switch (type) { 443 case GDB_BREAKPOINT_SW: 444 case GDB_BREAKPOINT_HW: 445 CPU_FOREACH(cpu) { 446 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL); 447 if (err) { 448 break; 449 } 450 } 451 return err; 452 default: 453 /* user-mode doesn't support watchpoints */ 454 return -ENOSYS; 455 } 456 } 457 458 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len) 459 { 460 CPUState *cpu; 461 int err = 0; 462 463 switch (type) { 464 case GDB_BREAKPOINT_SW: 465 case GDB_BREAKPOINT_HW: 466 CPU_FOREACH(cpu) { 467 err = cpu_breakpoint_remove(cpu, addr, BP_GDB); 468 if (err) { 469 break; 470 } 471 } 472 return err; 473 default: 474 /* user-mode doesn't support watchpoints */ 475 return -ENOSYS; 476 } 477 } 478 479 void gdb_breakpoint_remove_all(CPUState *cs) 480 { 481 cpu_breakpoint_remove_all(cs, BP_GDB); 482 } 483 484 /* 485 * For user-mode syscall support we send the system call immediately 486 * and then return control to gdb for it to process the syscall request. 487 * Since the protocol requires that gdb hands control back to us 488 * using a "here are the results" F packet, we don't need to check 489 * gdb_handlesig's return value (which is the signal to deliver if 490 * execution was resumed via a continue packet). 491 */ 492 void gdb_syscall_handling(const char *syscall_packet) 493 { 494 gdb_put_packet(syscall_packet); 495 gdb_handlesig(gdbserver_state.c_cpu, 0); 496 } 497