user.c 11 KB

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