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 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); 112 gdb_put_packet(buf); 113 } 114 115 int gdb_handlesig(CPUState *cpu, int sig) 116 { 117 char buf[256]; 118 int n; 119 120 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) { 121 return sig; 122 } 123 124 /* disable single step if it was enabled */ 125 cpu_single_step(cpu, 0); 126 tb_flush(cpu); 127 128 if (sig != 0) { 129 gdb_set_stop_cpu(cpu); 130 g_string_printf(gdbserver_state.str_buf, 131 "T%02xthread:", gdb_target_signal_to_gdb(sig)); 132 gdb_append_thread_id(cpu, gdbserver_state.str_buf); 133 g_string_append_c(gdbserver_state.str_buf, ';'); 134 gdb_put_strbuf(); 135 } 136 /* 137 * gdb_put_packet() might have detected that the peer terminated the 138 * connection. 139 */ 140 if (gdbserver_user_state.fd < 0) { 141 return sig; 142 } 143 144 sig = 0; 145 gdbserver_state.state = RS_IDLE; 146 gdbserver_user_state.running_state = 0; 147 while (gdbserver_user_state.running_state == 0) { 148 n = read(gdbserver_user_state.fd, buf, 256); 149 if (n > 0) { 150 int i; 151 152 for (i = 0; i < n; i++) { 153 gdb_read_byte(buf[i]); 154 } 155 } else { 156 /* 157 * XXX: Connection closed. Should probably wait for another 158 * connection before continuing. 159 */ 160 if (n == 0) { 161 close(gdbserver_user_state.fd); 162 } 163 gdbserver_user_state.fd = -1; 164 return sig; 165 } 166 } 167 sig = gdbserver_state.signal; 168 gdbserver_state.signal = 0; 169 return sig; 170 } 171 172 /* Tell the remote gdb that the process has exited due to SIG. */ 173 void gdb_signalled(CPUArchState *env, int sig) 174 { 175 char buf[4]; 176 177 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) { 178 return; 179 } 180 181 snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig)); 182 gdb_put_packet(buf); 183 } 184 185 static void gdb_accept_init(int fd) 186 { 187 gdb_init_gdbserver_state(); 188 gdb_create_default_process(&gdbserver_state); 189 gdbserver_state.processes[0].attached = true; 190 gdbserver_state.c_cpu = gdb_first_attached_cpu(); 191 gdbserver_state.g_cpu = gdbserver_state.c_cpu; 192 gdbserver_user_state.fd = fd; 193 gdb_has_xml = false; 194 } 195 196 static bool gdb_accept_socket(int gdb_fd) 197 { 198 int fd; 199 200 for (;;) { 201 fd = accept(gdb_fd, NULL, NULL); 202 if (fd < 0 && errno != EINTR) { 203 perror("accept socket"); 204 return false; 205 } else if (fd >= 0) { 206 qemu_set_cloexec(fd); 207 break; 208 } 209 } 210 211 gdb_accept_init(fd); 212 return true; 213 } 214 215 static int gdbserver_open_socket(const char *path) 216 { 217 struct sockaddr_un sockaddr = {}; 218 int fd, ret; 219 220 fd = socket(AF_UNIX, SOCK_STREAM, 0); 221 if (fd < 0) { 222 perror("create socket"); 223 return -1; 224 } 225 226 sockaddr.sun_family = AF_UNIX; 227 pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path); 228 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); 229 if (ret < 0) { 230 perror("bind socket"); 231 close(fd); 232 return -1; 233 } 234 ret = listen(fd, 1); 235 if (ret < 0) { 236 perror("listen socket"); 237 close(fd); 238 return -1; 239 } 240 241 return fd; 242 } 243 244 static bool gdb_accept_tcp(int gdb_fd) 245 { 246 struct sockaddr_in sockaddr = {}; 247 socklen_t len; 248 int fd; 249 250 for (;;) { 251 len = sizeof(sockaddr); 252 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len); 253 if (fd < 0 && errno != EINTR) { 254 perror("accept"); 255 return false; 256 } else if (fd >= 0) { 257 qemu_set_cloexec(fd); 258 break; 259 } 260 } 261 262 /* set short latency */ 263 if (socket_set_nodelay(fd)) { 264 perror("setsockopt"); 265 close(fd); 266 return false; 267 } 268 269 gdb_accept_init(fd); 270 return true; 271 } 272 273 static int gdbserver_open_port(int port) 274 { 275 struct sockaddr_in sockaddr; 276 int fd, ret; 277 278 fd = socket(PF_INET, SOCK_STREAM, 0); 279 if (fd < 0) { 280 perror("socket"); 281 return -1; 282 } 283 qemu_set_cloexec(fd); 284 285 socket_set_fast_reuse(fd); 286 287 sockaddr.sin_family = AF_INET; 288 sockaddr.sin_port = htons(port); 289 sockaddr.sin_addr.s_addr = 0; 290 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); 291 if (ret < 0) { 292 perror("bind"); 293 close(fd); 294 return -1; 295 } 296 ret = listen(fd, 1); 297 if (ret < 0) { 298 perror("listen"); 299 close(fd); 300 return -1; 301 } 302 303 return fd; 304 } 305 306 int gdbserver_start(const char *port_or_path) 307 { 308 int port = g_ascii_strtoull(port_or_path, NULL, 10); 309 int gdb_fd; 310 311 if (port > 0) { 312 gdb_fd = gdbserver_open_port(port); 313 } else { 314 gdb_fd = gdbserver_open_socket(port_or_path); 315 } 316 317 if (gdb_fd < 0) { 318 return -1; 319 } 320 321 if (port > 0 && gdb_accept_tcp(gdb_fd)) { 322 return 0; 323 } else if (gdb_accept_socket(gdb_fd)) { 324 gdbserver_user_state.socket_path = g_strdup(port_or_path); 325 return 0; 326 } 327 328 /* gone wrong */ 329 close(gdb_fd); 330 return -1; 331 } 332 333 /* Disable gdb stub for child processes. */ 334 void gdbserver_fork(CPUState *cpu) 335 { 336 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) { 337 return; 338 } 339 close(gdbserver_user_state.fd); 340 gdbserver_user_state.fd = -1; 341 cpu_breakpoint_remove_all(cpu, BP_GDB); 342 /* no cpu_watchpoint_remove_all for user-mode */ 343 } 344 345 /* 346 * Execution state helpers 347 */ 348 349 void gdb_handle_query_attached(GArray *params, void *user_ctx) 350 { 351 gdb_put_packet("0"); 352 } 353 354 void gdb_continue(void) 355 { 356 gdbserver_user_state.running_state = 1; 357 trace_gdbstub_op_continue(); 358 } 359 360 /* 361 * Resume execution, for user-mode emulation it's equivalent to 362 * gdb_continue. 363 */ 364 int gdb_continue_partial(char *newstates) 365 { 366 CPUState *cpu; 367 int res = 0; 368 /* 369 * This is not exactly accurate, but it's an improvement compared to the 370 * previous situation, where only one CPU would be single-stepped. 371 */ 372 CPU_FOREACH(cpu) { 373 if (newstates[cpu->cpu_index] == 's') { 374 trace_gdbstub_op_stepping(cpu->cpu_index); 375 cpu_single_step(cpu, gdbserver_state.sstep_flags); 376 } 377 } 378 gdbserver_user_state.running_state = 1; 379 return res; 380 } 381 382 /* 383 * Memory access helpers 384 */ 385 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr, 386 uint8_t *buf, int len, bool is_write) 387 { 388 CPUClass *cc; 389 390 cc = CPU_GET_CLASS(cpu); 391 if (cc->memory_rw_debug) { 392 return cc->memory_rw_debug(cpu, addr, buf, len, is_write); 393 } 394 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); 395 } 396 397 /* 398 * cpu helpers 399 */ 400 401 unsigned int gdb_get_max_cpus(void) 402 { 403 CPUState *cpu; 404 unsigned int max_cpus = 1; 405 406 CPU_FOREACH(cpu) { 407 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus; 408 } 409 410 return max_cpus; 411 } 412 413 /* replay not supported for user-mode */ 414 bool gdb_can_reverse(void) 415 { 416 return false; 417 } 418 419 /* 420 * Break/Watch point helpers 421 */ 422 423 bool gdb_supports_guest_debug(void) 424 { 425 /* user-mode == TCG == supported */ 426 return true; 427 } 428 429 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len) 430 { 431 CPUState *cpu; 432 int err = 0; 433 434 switch (type) { 435 case GDB_BREAKPOINT_SW: 436 case GDB_BREAKPOINT_HW: 437 CPU_FOREACH(cpu) { 438 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL); 439 if (err) { 440 break; 441 } 442 } 443 return err; 444 default: 445 /* user-mode doesn't support watchpoints */ 446 return -ENOSYS; 447 } 448 } 449 450 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len) 451 { 452 CPUState *cpu; 453 int err = 0; 454 455 switch (type) { 456 case GDB_BREAKPOINT_SW: 457 case GDB_BREAKPOINT_HW: 458 CPU_FOREACH(cpu) { 459 err = cpu_breakpoint_remove(cpu, addr, BP_GDB); 460 if (err) { 461 break; 462 } 463 } 464 return err; 465 default: 466 /* user-mode doesn't support watchpoints */ 467 return -ENOSYS; 468 } 469 } 470 471 void gdb_breakpoint_remove_all(CPUState *cs) 472 { 473 cpu_breakpoint_remove_all(cs, BP_GDB); 474 } 475 476 /* 477 * For user-mode syscall support we send the system call immediately 478 * and then return control to gdb for it to process the syscall request. 479 * Since the protocol requires that gdb hands control back to us 480 * using a "here are the results" F packet, we don't need to check 481 * gdb_handlesig's return value (which is the signal to deliver if 482 * execution was resumed via a continue packet). 483 */ 484 void gdb_syscall_handling(const char *syscall_packet) 485 { 486 gdb_put_packet(syscall_packet); 487 gdb_handlesig(gdbserver_state.c_cpu, 0); 488 } 489