2
0

user.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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-or-later
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qemu/bitops.h"
  13. #include "qemu/cutils.h"
  14. #include "qemu/sockets.h"
  15. #include "qapi/error.h"
  16. #include "exec/hwaddr.h"
  17. #include "exec/tb-flush.h"
  18. #include "exec/gdbstub.h"
  19. #include "gdbstub/commands.h"
  20. #include "gdbstub/syscalls.h"
  21. #include "gdbstub/user.h"
  22. #include "gdbstub/enums.h"
  23. #include "hw/core/cpu.h"
  24. #include "user/signal.h"
  25. #include "trace.h"
  26. #include "internals.h"
  27. #define GDB_NR_SYSCALLS 1024
  28. typedef unsigned long GDBSyscallsMask[BITS_TO_LONGS(GDB_NR_SYSCALLS)];
  29. /*
  30. * Forked child talks to its parent in order to let GDB enforce the
  31. * follow-fork-mode. This happens inside a start_exclusive() section, so that
  32. * the other threads, which may be forking too, do not interfere. The
  33. * implementation relies on GDB not sending $vCont until it has detached
  34. * either from the parent (follow-fork-mode child) or from the child
  35. * (follow-fork-mode parent).
  36. *
  37. * The parent and the child share the GDB socket; at any given time only one
  38. * of them is allowed to use it, as is reflected in the respective fork_state.
  39. * This is negotiated via the fork_sockets pair as a reaction to $Hg.
  40. *
  41. * Below is a short summary of the possible state transitions:
  42. *
  43. * ENABLED : Terminal state.
  44. * DISABLED : Terminal state.
  45. * ACTIVE : Parent initial state.
  46. * INACTIVE : Child initial state.
  47. * ACTIVE -> DEACTIVATING: On $Hg.
  48. * ACTIVE -> ENABLING : On $D.
  49. * ACTIVE -> DISABLING : On $D.
  50. * ACTIVE -> DISABLED : On communication error.
  51. * DEACTIVATING -> INACTIVE : On gdb_read_byte() return.
  52. * DEACTIVATING -> DISABLED : On communication error.
  53. * INACTIVE -> ACTIVE : On $Hg in the peer.
  54. * INACTIVE -> ENABLE : On $D in the peer.
  55. * INACTIVE -> DISABLE : On $D in the peer.
  56. * INACTIVE -> DISABLED : On communication error.
  57. * ENABLING -> ENABLED : On gdb_read_byte() return.
  58. * ENABLING -> DISABLED : On communication error.
  59. * DISABLING -> DISABLED : On gdb_read_byte() return.
  60. */
  61. enum GDBForkState {
  62. /* Fully owning the GDB socket. */
  63. GDB_FORK_ENABLED,
  64. /* Working with the GDB socket; the peer is inactive. */
  65. GDB_FORK_ACTIVE,
  66. /* Handing off the GDB socket to the peer. */
  67. GDB_FORK_DEACTIVATING,
  68. /* The peer is working with the GDB socket. */
  69. GDB_FORK_INACTIVE,
  70. /* Asking the peer to close its GDB socket fd. */
  71. GDB_FORK_ENABLING,
  72. /* Asking the peer to take over, closing our GDB socket fd. */
  73. GDB_FORK_DISABLING,
  74. /* The peer has taken over, our GDB socket fd is closed. */
  75. GDB_FORK_DISABLED,
  76. };
  77. enum GDBForkMessage {
  78. GDB_FORK_ACTIVATE = 'a',
  79. GDB_FORK_ENABLE = 'e',
  80. GDB_FORK_DISABLE = 'd',
  81. };
  82. /* User-mode specific state */
  83. typedef struct {
  84. int fd;
  85. char *socket_path;
  86. int running_state;
  87. /*
  88. * Store syscalls mask without memory allocation in order to avoid
  89. * implementing synchronization.
  90. */
  91. bool catch_all_syscalls;
  92. GDBSyscallsMask catch_syscalls_mask;
  93. bool fork_events;
  94. enum GDBForkState fork_state;
  95. int fork_sockets[2];
  96. pid_t fork_peer_pid, fork_peer_tid;
  97. uint8_t siginfo[MAX_SIGINFO_LENGTH];
  98. unsigned long siginfo_len;
  99. } GDBUserState;
  100. static GDBUserState gdbserver_user_state;
  101. int gdb_get_char(void)
  102. {
  103. uint8_t ch;
  104. int ret;
  105. for (;;) {
  106. ret = recv(gdbserver_user_state.fd, &ch, 1, 0);
  107. if (ret < 0) {
  108. if (errno == ECONNRESET) {
  109. gdbserver_user_state.fd = -1;
  110. }
  111. if (errno != EINTR) {
  112. return -1;
  113. }
  114. } else if (ret == 0) {
  115. close(gdbserver_user_state.fd);
  116. gdbserver_user_state.fd = -1;
  117. return -1;
  118. } else {
  119. break;
  120. }
  121. }
  122. return ch;
  123. }
  124. bool gdb_got_immediate_ack(void)
  125. {
  126. int i;
  127. i = gdb_get_char();
  128. if (i < 0) {
  129. /* no response, continue anyway */
  130. return true;
  131. }
  132. if (i == '+') {
  133. /* received correctly, continue */
  134. return true;
  135. }
  136. /* anything else, including '-' then try again */
  137. return false;
  138. }
  139. void gdb_put_buffer(const uint8_t *buf, int len)
  140. {
  141. int ret;
  142. while (len > 0) {
  143. ret = send(gdbserver_user_state.fd, buf, len, 0);
  144. if (ret < 0) {
  145. if (errno != EINTR) {
  146. return;
  147. }
  148. } else {
  149. buf += ret;
  150. len -= ret;
  151. }
  152. }
  153. }
  154. /* Tell the remote gdb that the process has exited. */
  155. void gdb_exit(int code)
  156. {
  157. char buf[4];
  158. if (!gdbserver_state.init) {
  159. return;
  160. }
  161. if (gdbserver_user_state.socket_path) {
  162. unlink(gdbserver_user_state.socket_path);
  163. }
  164. if (gdbserver_user_state.fd < 0) {
  165. return;
  166. }
  167. trace_gdbstub_op_exiting((uint8_t)code);
  168. if (gdbserver_state.allow_stop_reply) {
  169. snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
  170. gdb_put_packet(buf);
  171. gdbserver_state.allow_stop_reply = false;
  172. }
  173. }
  174. void gdb_qemu_exit(int code)
  175. {
  176. exit(code);
  177. }
  178. int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
  179. int siginfo_len)
  180. {
  181. char buf[256];
  182. int n;
  183. if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
  184. return sig;
  185. }
  186. if (siginfo) {
  187. /*
  188. * Save target-specific siginfo.
  189. *
  190. * siginfo size, i.e. siginfo_len, is asserted at compile-time to fit in
  191. * gdbserver_user_state.siginfo, usually in the source file calling
  192. * gdb_handlesig. See, for instance, {linux,bsd}-user/signal.c.
  193. */
  194. memcpy(gdbserver_user_state.siginfo, siginfo, siginfo_len);
  195. gdbserver_user_state.siginfo_len = siginfo_len;
  196. }
  197. /* disable single step if it was enabled */
  198. cpu_single_step(cpu, 0);
  199. tb_flush(cpu);
  200. if (sig != 0) {
  201. gdb_set_stop_cpu(cpu);
  202. if (gdbserver_state.allow_stop_reply) {
  203. g_string_printf(gdbserver_state.str_buf,
  204. "T%02xthread:", gdb_target_signal_to_gdb(sig));
  205. gdb_append_thread_id(cpu, gdbserver_state.str_buf);
  206. g_string_append_c(gdbserver_state.str_buf, ';');
  207. if (reason) {
  208. g_string_append(gdbserver_state.str_buf, reason);
  209. }
  210. gdb_put_strbuf();
  211. gdbserver_state.allow_stop_reply = false;
  212. }
  213. }
  214. /*
  215. * gdb_put_packet() might have detected that the peer terminated the
  216. * connection.
  217. */
  218. if (gdbserver_user_state.fd < 0) {
  219. return sig;
  220. }
  221. sig = 0;
  222. gdbserver_state.state = RS_IDLE;
  223. gdbserver_user_state.running_state = 0;
  224. while (gdbserver_user_state.running_state == 0) {
  225. n = read(gdbserver_user_state.fd, buf, 256);
  226. if (n > 0) {
  227. int i;
  228. for (i = 0; i < n; i++) {
  229. gdb_read_byte(buf[i]);
  230. }
  231. } else {
  232. /*
  233. * XXX: Connection closed. Should probably wait for another
  234. * connection before continuing.
  235. */
  236. if (n == 0) {
  237. close(gdbserver_user_state.fd);
  238. }
  239. gdbserver_user_state.fd = -1;
  240. return sig;
  241. }
  242. }
  243. sig = gdbserver_state.signal;
  244. gdbserver_state.signal = 0;
  245. return sig;
  246. }
  247. /* Tell the remote gdb that the process has exited due to SIG. */
  248. void gdb_signalled(CPUArchState *env, int sig)
  249. {
  250. char buf[4];
  251. if (!gdbserver_state.init || gdbserver_user_state.fd < 0 ||
  252. !gdbserver_state.allow_stop_reply) {
  253. return;
  254. }
  255. snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig));
  256. gdb_put_packet(buf);
  257. gdbserver_state.allow_stop_reply = false;
  258. }
  259. static void gdb_accept_init(int fd)
  260. {
  261. gdb_init_gdbserver_state();
  262. gdb_create_default_process(&gdbserver_state);
  263. gdbserver_state.processes[0].attached = true;
  264. gdbserver_state.c_cpu = gdb_first_attached_cpu();
  265. gdbserver_state.g_cpu = gdbserver_state.c_cpu;
  266. gdbserver_user_state.fd = fd;
  267. }
  268. static bool gdb_accept_socket(int gdb_fd)
  269. {
  270. int fd;
  271. for (;;) {
  272. fd = accept(gdb_fd, NULL, NULL);
  273. if (fd < 0 && errno != EINTR) {
  274. perror("accept socket");
  275. return false;
  276. } else if (fd >= 0) {
  277. qemu_set_cloexec(fd);
  278. break;
  279. }
  280. }
  281. gdb_accept_init(fd);
  282. return true;
  283. }
  284. static int gdbserver_open_socket(const char *path, Error **errp)
  285. {
  286. g_autoptr(GString) buf = g_string_new("");
  287. char *pid_placeholder;
  288. pid_placeholder = strstr(path, "%d");
  289. if (pid_placeholder != NULL) {
  290. g_string_append_len(buf, path, pid_placeholder - path);
  291. g_string_append_printf(buf, "%d", qemu_get_thread_id());
  292. g_string_append(buf, pid_placeholder + 2);
  293. path = buf->str;
  294. }
  295. return unix_listen(path, errp);
  296. }
  297. static bool gdb_accept_tcp(int gdb_fd)
  298. {
  299. struct sockaddr_in sockaddr = {};
  300. socklen_t len;
  301. int fd;
  302. for (;;) {
  303. len = sizeof(sockaddr);
  304. fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
  305. if (fd < 0 && errno != EINTR) {
  306. perror("accept");
  307. return false;
  308. } else if (fd >= 0) {
  309. qemu_set_cloexec(fd);
  310. break;
  311. }
  312. }
  313. /* set short latency */
  314. if (socket_set_nodelay(fd)) {
  315. perror("setsockopt");
  316. close(fd);
  317. return false;
  318. }
  319. gdb_accept_init(fd);
  320. return true;
  321. }
  322. static int gdbserver_open_port(int port, Error **errp)
  323. {
  324. struct sockaddr_in sockaddr;
  325. int fd, ret;
  326. fd = socket(PF_INET, SOCK_STREAM, 0);
  327. if (fd < 0) {
  328. error_setg_errno(errp, errno, "Failed to create socket");
  329. return -1;
  330. }
  331. qemu_set_cloexec(fd);
  332. socket_set_fast_reuse(fd);
  333. sockaddr.sin_family = AF_INET;
  334. sockaddr.sin_port = htons(port);
  335. sockaddr.sin_addr.s_addr = 0;
  336. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  337. if (ret < 0) {
  338. error_setg_errno(errp, errno, "Failed to bind socket");
  339. close(fd);
  340. return -1;
  341. }
  342. ret = listen(fd, 1);
  343. if (ret < 0) {
  344. error_setg_errno(errp, errno, "Failed to listen to socket");
  345. close(fd);
  346. return -1;
  347. }
  348. return fd;
  349. }
  350. static bool gdbserver_accept(int port, int gdb_fd, const char *path)
  351. {
  352. bool ret;
  353. if (port > 0) {
  354. ret = gdb_accept_tcp(gdb_fd);
  355. } else {
  356. ret = gdb_accept_socket(gdb_fd);
  357. if (ret) {
  358. gdbserver_user_state.socket_path = g_strdup(path);
  359. }
  360. }
  361. if (!ret) {
  362. close(gdb_fd);
  363. }
  364. return ret;
  365. }
  366. struct {
  367. int port;
  368. int gdb_fd;
  369. char *path;
  370. } gdbserver_args;
  371. static void do_gdb_handlesig(CPUState *cs, run_on_cpu_data arg)
  372. {
  373. int sig;
  374. sig = target_to_host_signal(gdb_handlesig(cs, 0, NULL, NULL, 0));
  375. if (sig >= 1 && sig < NSIG) {
  376. qemu_kill_thread(gdb_get_cpu_index(cs), sig);
  377. }
  378. }
  379. static void *gdbserver_accept_thread(void *arg)
  380. {
  381. if (gdbserver_accept(gdbserver_args.port, gdbserver_args.gdb_fd,
  382. gdbserver_args.path)) {
  383. CPUState *cs = first_cpu;
  384. async_safe_run_on_cpu(cs, do_gdb_handlesig, RUN_ON_CPU_NULL);
  385. qemu_kill_thread(gdb_get_cpu_index(cs), host_interrupt_signal);
  386. }
  387. g_free(gdbserver_args.path);
  388. gdbserver_args.path = NULL;
  389. return NULL;
  390. }
  391. #define USAGE "\nUsage: -g {port|path}[,suspend={y|n}]"
  392. bool gdbserver_start(const char *args, Error **errp)
  393. {
  394. g_auto(GStrv) argv = g_strsplit(args, ",", 0);
  395. const char *port_or_path = NULL;
  396. bool suspend = true;
  397. int gdb_fd, port;
  398. GStrv arg;
  399. for (arg = argv; *arg; arg++) {
  400. g_auto(GStrv) tokens = g_strsplit(*arg, "=", 2);
  401. if (g_strcmp0(tokens[0], "suspend") == 0) {
  402. if (tokens[1] == NULL) {
  403. error_setg(errp,
  404. "gdbstub: missing \"suspend\" option value" USAGE);
  405. return false;
  406. } else if (!qapi_bool_parse(tokens[0], tokens[1],
  407. &suspend, errp)) {
  408. return false;
  409. }
  410. } else {
  411. if (port_or_path) {
  412. error_setg(errp, "gdbstub: unknown option \"%s\"" USAGE, *arg);
  413. return false;
  414. }
  415. port_or_path = *arg;
  416. }
  417. }
  418. if (!port_or_path) {
  419. error_setg(errp, "gdbstub: port or path not specified" USAGE);
  420. return false;
  421. }
  422. port = g_ascii_strtoull(port_or_path, NULL, 10);
  423. if (port > 0) {
  424. gdb_fd = gdbserver_open_port(port, errp);
  425. } else {
  426. gdb_fd = gdbserver_open_socket(port_or_path, errp);
  427. }
  428. if (gdb_fd < 0) {
  429. return false;
  430. }
  431. if (suspend) {
  432. if (gdbserver_accept(port, gdb_fd, port_or_path)) {
  433. gdb_handlesig(first_cpu, 0, NULL, NULL, 0);
  434. return true;
  435. } else {
  436. error_setg(errp, "gdbstub: failed to accept connection");
  437. return false;
  438. }
  439. } else {
  440. QemuThread thread;
  441. gdbserver_args.port = port;
  442. gdbserver_args.gdb_fd = gdb_fd;
  443. gdbserver_args.path = g_strdup(port_or_path);
  444. qemu_thread_create(&thread, "gdb-accept",
  445. &gdbserver_accept_thread, NULL,
  446. QEMU_THREAD_DETACHED);
  447. return true;
  448. }
  449. }
  450. void gdbserver_fork_start(void)
  451. {
  452. if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
  453. return;
  454. }
  455. if (!gdbserver_user_state.fork_events ||
  456. qemu_socketpair(AF_UNIX, SOCK_STREAM, 0,
  457. gdbserver_user_state.fork_sockets) < 0) {
  458. gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
  459. return;
  460. }
  461. gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
  462. gdbserver_user_state.fork_peer_pid = getpid();
  463. gdbserver_user_state.fork_peer_tid = qemu_get_thread_id();
  464. }
  465. static void disable_gdbstub(CPUState *thread_cpu)
  466. {
  467. CPUState *cpu;
  468. close(gdbserver_user_state.fd);
  469. gdbserver_user_state.fd = -1;
  470. CPU_FOREACH(cpu) {
  471. cpu_breakpoint_remove_all(cpu, BP_GDB);
  472. /* no cpu_watchpoint_remove_all for user-mode */
  473. cpu_single_step(cpu, 0);
  474. }
  475. tb_flush(thread_cpu);
  476. }
  477. void gdbserver_fork_end(CPUState *cpu, pid_t pid)
  478. {
  479. char b;
  480. int fd;
  481. if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
  482. return;
  483. }
  484. if (pid == -1) {
  485. if (gdbserver_user_state.fork_state != GDB_FORK_DISABLED) {
  486. g_assert(gdbserver_user_state.fork_state == GDB_FORK_INACTIVE);
  487. close(gdbserver_user_state.fork_sockets[0]);
  488. close(gdbserver_user_state.fork_sockets[1]);
  489. }
  490. return;
  491. }
  492. if (gdbserver_user_state.fork_state == GDB_FORK_DISABLED) {
  493. if (pid == 0) {
  494. disable_gdbstub(cpu);
  495. }
  496. return;
  497. }
  498. if (pid == 0) {
  499. close(gdbserver_user_state.fork_sockets[0]);
  500. fd = gdbserver_user_state.fork_sockets[1];
  501. g_assert(gdbserver_state.process_num == 1);
  502. g_assert(gdbserver_state.processes[0].pid ==
  503. gdbserver_user_state.fork_peer_pid);
  504. g_assert(gdbserver_state.processes[0].attached);
  505. gdbserver_state.processes[0].pid = getpid();
  506. } else {
  507. close(gdbserver_user_state.fork_sockets[1]);
  508. fd = gdbserver_user_state.fork_sockets[0];
  509. gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
  510. gdbserver_user_state.fork_peer_pid = pid;
  511. gdbserver_user_state.fork_peer_tid = pid;
  512. if (!gdbserver_state.allow_stop_reply) {
  513. goto fail;
  514. }
  515. g_string_printf(gdbserver_state.str_buf,
  516. "T%02xfork:p%02x.%02x;thread:p%02x.%02x;",
  517. gdb_target_signal_to_gdb(gdb_target_sigtrap()),
  518. pid, pid, (int)getpid(), qemu_get_thread_id());
  519. gdb_put_strbuf();
  520. }
  521. gdbserver_state.state = RS_IDLE;
  522. gdbserver_state.allow_stop_reply = false;
  523. gdbserver_user_state.running_state = 0;
  524. for (;;) {
  525. switch (gdbserver_user_state.fork_state) {
  526. case GDB_FORK_ENABLED:
  527. if (gdbserver_user_state.running_state) {
  528. close(fd);
  529. return;
  530. }
  531. QEMU_FALLTHROUGH;
  532. case GDB_FORK_ACTIVE:
  533. if (read(gdbserver_user_state.fd, &b, 1) != 1) {
  534. goto fail;
  535. }
  536. gdb_read_byte(b);
  537. break;
  538. case GDB_FORK_DEACTIVATING:
  539. b = GDB_FORK_ACTIVATE;
  540. if (write(fd, &b, 1) != 1) {
  541. goto fail;
  542. }
  543. gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
  544. break;
  545. case GDB_FORK_INACTIVE:
  546. if (read(fd, &b, 1) != 1) {
  547. goto fail;
  548. }
  549. switch (b) {
  550. case GDB_FORK_ACTIVATE:
  551. gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
  552. break;
  553. case GDB_FORK_ENABLE:
  554. gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
  555. break;
  556. case GDB_FORK_DISABLE:
  557. gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
  558. break;
  559. default:
  560. g_assert_not_reached();
  561. }
  562. break;
  563. case GDB_FORK_ENABLING:
  564. b = GDB_FORK_DISABLE;
  565. if (write(fd, &b, 1) != 1) {
  566. goto fail;
  567. }
  568. gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
  569. break;
  570. case GDB_FORK_DISABLING:
  571. b = GDB_FORK_ENABLE;
  572. if (write(fd, &b, 1) != 1) {
  573. goto fail;
  574. }
  575. gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
  576. break;
  577. case GDB_FORK_DISABLED:
  578. close(fd);
  579. disable_gdbstub(cpu);
  580. return;
  581. default:
  582. g_assert_not_reached();
  583. }
  584. }
  585. fail:
  586. close(fd);
  587. if (pid == 0) {
  588. disable_gdbstub(cpu);
  589. }
  590. }
  591. void gdb_handle_query_supported_user(const char *gdb_supported)
  592. {
  593. if (strstr(gdb_supported, "fork-events+")) {
  594. gdbserver_user_state.fork_events = true;
  595. }
  596. g_string_append(gdbserver_state.str_buf, ";fork-events+");
  597. }
  598. bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid)
  599. {
  600. if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE &&
  601. pid == gdbserver_user_state.fork_peer_pid &&
  602. tid == gdbserver_user_state.fork_peer_tid) {
  603. gdbserver_user_state.fork_state = GDB_FORK_DEACTIVATING;
  604. gdb_put_packet("OK");
  605. return true;
  606. }
  607. return false;
  608. }
  609. bool gdb_handle_detach_user(uint32_t pid)
  610. {
  611. bool enable;
  612. if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE) {
  613. enable = pid == gdbserver_user_state.fork_peer_pid;
  614. if (enable || pid == getpid()) {
  615. gdbserver_user_state.fork_state = enable ? GDB_FORK_ENABLING :
  616. GDB_FORK_DISABLING;
  617. gdb_put_packet("OK");
  618. return true;
  619. }
  620. }
  621. return false;
  622. }
  623. /*
  624. * Execution state helpers
  625. */
  626. void gdb_handle_query_attached(GArray *params, void *user_ctx)
  627. {
  628. gdb_put_packet("0");
  629. }
  630. void gdb_continue(void)
  631. {
  632. gdbserver_user_state.running_state = 1;
  633. trace_gdbstub_op_continue();
  634. }
  635. /*
  636. * Resume execution, for user-mode emulation it's equivalent to
  637. * gdb_continue.
  638. */
  639. int gdb_continue_partial(char *newstates)
  640. {
  641. CPUState *cpu;
  642. int res = 0;
  643. /*
  644. * This is not exactly accurate, but it's an improvement compared to the
  645. * previous situation, where only one CPU would be single-stepped.
  646. */
  647. CPU_FOREACH(cpu) {
  648. if (newstates[cpu->cpu_index] == 's') {
  649. trace_gdbstub_op_stepping(cpu->cpu_index);
  650. cpu_single_step(cpu, gdbserver_state.sstep_flags);
  651. }
  652. }
  653. gdbserver_user_state.running_state = 1;
  654. return res;
  655. }
  656. /*
  657. * Memory access helpers
  658. */
  659. int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
  660. uint8_t *buf, int len, bool is_write)
  661. {
  662. CPUClass *cc;
  663. cc = CPU_GET_CLASS(cpu);
  664. if (cc->memory_rw_debug) {
  665. return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
  666. }
  667. return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
  668. }
  669. /*
  670. * cpu helpers
  671. */
  672. unsigned int gdb_get_max_cpus(void)
  673. {
  674. CPUState *cpu;
  675. unsigned int max_cpus = 1;
  676. CPU_FOREACH(cpu) {
  677. max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
  678. }
  679. return max_cpus;
  680. }
  681. /* replay not supported for user-mode */
  682. bool gdb_can_reverse(void)
  683. {
  684. return false;
  685. }
  686. /*
  687. * Break/Watch point helpers
  688. */
  689. bool gdb_supports_guest_debug(void)
  690. {
  691. /* user-mode == TCG == supported */
  692. return true;
  693. }
  694. int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
  695. {
  696. CPUState *cpu;
  697. int err = 0;
  698. switch (type) {
  699. case GDB_BREAKPOINT_SW:
  700. case GDB_BREAKPOINT_HW:
  701. CPU_FOREACH(cpu) {
  702. err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
  703. if (err) {
  704. break;
  705. }
  706. }
  707. return err;
  708. default:
  709. /* user-mode doesn't support watchpoints */
  710. return -ENOSYS;
  711. }
  712. }
  713. int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
  714. {
  715. CPUState *cpu;
  716. int err = 0;
  717. switch (type) {
  718. case GDB_BREAKPOINT_SW:
  719. case GDB_BREAKPOINT_HW:
  720. CPU_FOREACH(cpu) {
  721. err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
  722. if (err) {
  723. break;
  724. }
  725. }
  726. return err;
  727. default:
  728. /* user-mode doesn't support watchpoints */
  729. return -ENOSYS;
  730. }
  731. }
  732. void gdb_breakpoint_remove_all(CPUState *cs)
  733. {
  734. cpu_breakpoint_remove_all(cs, BP_GDB);
  735. }
  736. /*
  737. * For user-mode syscall support we send the system call immediately
  738. * and then return control to gdb for it to process the syscall request.
  739. * Since the protocol requires that gdb hands control back to us
  740. * using a "here are the results" F packet, we don't need to check
  741. * gdb_handlesig's return value (which is the signal to deliver if
  742. * execution was resumed via a continue packet).
  743. */
  744. void gdb_syscall_handling(const char *syscall_packet)
  745. {
  746. gdb_put_packet(syscall_packet);
  747. gdb_handlesig(gdbserver_state.c_cpu, 0, NULL, NULL, 0);
  748. }
  749. static bool should_catch_syscall(int num)
  750. {
  751. if (gdbserver_user_state.catch_all_syscalls) {
  752. return true;
  753. }
  754. if (num < 0 || num >= GDB_NR_SYSCALLS) {
  755. return false;
  756. }
  757. return test_bit(num, gdbserver_user_state.catch_syscalls_mask);
  758. }
  759. void gdb_syscall_entry(CPUState *cs, int num)
  760. {
  761. if (should_catch_syscall(num)) {
  762. g_autofree char *reason = g_strdup_printf("syscall_entry:%x;", num);
  763. gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
  764. }
  765. }
  766. void gdb_syscall_return(CPUState *cs, int num)
  767. {
  768. if (should_catch_syscall(num)) {
  769. g_autofree char *reason = g_strdup_printf("syscall_return:%x;", num);
  770. gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
  771. }
  772. }
  773. void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx)
  774. {
  775. const char *param = gdb_get_cmd_param(params, 0)->data;
  776. GDBSyscallsMask catch_syscalls_mask;
  777. bool catch_all_syscalls;
  778. unsigned int num;
  779. const char *p;
  780. /* "0" means not catching any syscalls. */
  781. if (strcmp(param, "0") == 0) {
  782. gdbserver_user_state.catch_all_syscalls = false;
  783. memset(gdbserver_user_state.catch_syscalls_mask, 0,
  784. sizeof(gdbserver_user_state.catch_syscalls_mask));
  785. gdb_put_packet("OK");
  786. return;
  787. }
  788. /* "1" means catching all syscalls. */
  789. if (strcmp(param, "1") == 0) {
  790. gdbserver_user_state.catch_all_syscalls = true;
  791. gdb_put_packet("OK");
  792. return;
  793. }
  794. /*
  795. * "1;..." means catching only the specified syscalls.
  796. * The syscall list must not be empty.
  797. */
  798. if (param[0] == '1' && param[1] == ';') {
  799. catch_all_syscalls = false;
  800. memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask));
  801. for (p = &param[2];; p++) {
  802. if (qemu_strtoui(p, &p, 16, &num) || (*p && *p != ';')) {
  803. goto err;
  804. }
  805. if (num >= GDB_NR_SYSCALLS) {
  806. /*
  807. * Fall back to reporting all syscalls. Reporting extra
  808. * syscalls is inefficient, but the spec explicitly allows it.
  809. * Keep parsing in case there is a syntax error ahead.
  810. */
  811. catch_all_syscalls = true;
  812. } else {
  813. set_bit(num, catch_syscalls_mask);
  814. }
  815. if (!*p) {
  816. break;
  817. }
  818. }
  819. gdbserver_user_state.catch_all_syscalls = catch_all_syscalls;
  820. if (!catch_all_syscalls) {
  821. memcpy(gdbserver_user_state.catch_syscalls_mask,
  822. catch_syscalls_mask, sizeof(catch_syscalls_mask));
  823. }
  824. gdb_put_packet("OK");
  825. return;
  826. }
  827. err:
  828. gdb_put_packet("E00");
  829. }
  830. void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
  831. {
  832. unsigned long offset, len;
  833. uint8_t *siginfo_offset;
  834. offset = gdb_get_cmd_param(params, 0)->val_ul;
  835. len = gdb_get_cmd_param(params, 1)->val_ul;
  836. if (offset + len > gdbserver_user_state.siginfo_len) {
  837. /* Invalid offset and/or requested length. */
  838. gdb_put_packet("E01");
  839. return;
  840. }
  841. siginfo_offset = (uint8_t *)gdbserver_user_state.siginfo + offset;
  842. /* Reply */
  843. g_string_assign(gdbserver_state.str_buf, "l");
  844. gdb_memtox(gdbserver_state.str_buf, (const char *)siginfo_offset, len);
  845. gdb_put_packet_binary(gdbserver_state.str_buf->str,
  846. gdbserver_state.str_buf->len, true);
  847. }