user.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. gdb_has_xml = false;
  177. }
  178. static bool gdb_accept_socket(int gdb_fd)
  179. {
  180. int fd;
  181. for (;;) {
  182. fd = accept(gdb_fd, NULL, NULL);
  183. if (fd < 0 && errno != EINTR) {
  184. perror("accept socket");
  185. return false;
  186. } else if (fd >= 0) {
  187. qemu_set_cloexec(fd);
  188. break;
  189. }
  190. }
  191. gdb_accept_init(fd);
  192. return true;
  193. }
  194. static int gdbserver_open_socket(const char *path)
  195. {
  196. struct sockaddr_un sockaddr = {};
  197. int fd, ret;
  198. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  199. if (fd < 0) {
  200. perror("create socket");
  201. return -1;
  202. }
  203. sockaddr.sun_family = AF_UNIX;
  204. pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
  205. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  206. if (ret < 0) {
  207. perror("bind socket");
  208. close(fd);
  209. return -1;
  210. }
  211. ret = listen(fd, 1);
  212. if (ret < 0) {
  213. perror("listen socket");
  214. close(fd);
  215. return -1;
  216. }
  217. return fd;
  218. }
  219. static bool gdb_accept_tcp(int gdb_fd)
  220. {
  221. struct sockaddr_in sockaddr = {};
  222. socklen_t len;
  223. int fd;
  224. for (;;) {
  225. len = sizeof(sockaddr);
  226. fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
  227. if (fd < 0 && errno != EINTR) {
  228. perror("accept");
  229. return false;
  230. } else if (fd >= 0) {
  231. qemu_set_cloexec(fd);
  232. break;
  233. }
  234. }
  235. /* set short latency */
  236. if (socket_set_nodelay(fd)) {
  237. perror("setsockopt");
  238. close(fd);
  239. return false;
  240. }
  241. gdb_accept_init(fd);
  242. return true;
  243. }
  244. static int gdbserver_open_port(int port)
  245. {
  246. struct sockaddr_in sockaddr;
  247. int fd, ret;
  248. fd = socket(PF_INET, SOCK_STREAM, 0);
  249. if (fd < 0) {
  250. perror("socket");
  251. return -1;
  252. }
  253. qemu_set_cloexec(fd);
  254. socket_set_fast_reuse(fd);
  255. sockaddr.sin_family = AF_INET;
  256. sockaddr.sin_port = htons(port);
  257. sockaddr.sin_addr.s_addr = 0;
  258. ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  259. if (ret < 0) {
  260. perror("bind");
  261. close(fd);
  262. return -1;
  263. }
  264. ret = listen(fd, 1);
  265. if (ret < 0) {
  266. perror("listen");
  267. close(fd);
  268. return -1;
  269. }
  270. return fd;
  271. }
  272. int gdbserver_start(const char *port_or_path)
  273. {
  274. int port = g_ascii_strtoull(port_or_path, NULL, 10);
  275. int gdb_fd;
  276. if (port > 0) {
  277. gdb_fd = gdbserver_open_port(port);
  278. } else {
  279. gdb_fd = gdbserver_open_socket(port_or_path);
  280. }
  281. if (gdb_fd < 0) {
  282. return -1;
  283. }
  284. if (port > 0 && gdb_accept_tcp(gdb_fd)) {
  285. return 0;
  286. } else if (gdb_accept_socket(gdb_fd)) {
  287. gdbserver_user_state.socket_path = g_strdup(port_or_path);
  288. return 0;
  289. }
  290. /* gone wrong */
  291. close(gdb_fd);
  292. return -1;
  293. }
  294. /* Disable gdb stub for child processes. */
  295. void gdbserver_fork(CPUState *cpu)
  296. {
  297. if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
  298. return;
  299. }
  300. close(gdbserver_user_state.fd);
  301. gdbserver_user_state.fd = -1;
  302. cpu_breakpoint_remove_all(cpu, BP_GDB);
  303. /* no cpu_watchpoint_remove_all for user-mode */
  304. }
  305. /*
  306. * Execution state helpers
  307. */
  308. void gdb_handle_query_attached(GArray *params, void *user_ctx)
  309. {
  310. gdb_put_packet("0");
  311. }
  312. void gdb_continue(void)
  313. {
  314. gdbserver_user_state.running_state = 1;
  315. trace_gdbstub_op_continue();
  316. }
  317. /*
  318. * Resume execution, for user-mode emulation it's equivalent to
  319. * gdb_continue.
  320. */
  321. int gdb_continue_partial(char *newstates)
  322. {
  323. CPUState *cpu;
  324. int res = 0;
  325. /*
  326. * This is not exactly accurate, but it's an improvement compared to the
  327. * previous situation, where only one CPU would be single-stepped.
  328. */
  329. CPU_FOREACH(cpu) {
  330. if (newstates[cpu->cpu_index] == 's') {
  331. trace_gdbstub_op_stepping(cpu->cpu_index);
  332. cpu_single_step(cpu, gdbserver_state.sstep_flags);
  333. }
  334. }
  335. gdbserver_user_state.running_state = 1;
  336. return res;
  337. }
  338. /*
  339. * Memory access helpers
  340. */
  341. int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
  342. uint8_t *buf, int len, bool is_write)
  343. {
  344. CPUClass *cc;
  345. cc = CPU_GET_CLASS(cpu);
  346. if (cc->memory_rw_debug) {
  347. return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
  348. }
  349. return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
  350. }
  351. /*
  352. * cpu helpers
  353. */
  354. unsigned int gdb_get_max_cpus(void)
  355. {
  356. CPUState *cpu;
  357. unsigned int max_cpus = 1;
  358. CPU_FOREACH(cpu) {
  359. max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
  360. }
  361. return max_cpus;
  362. }
  363. /* replay not supported for user-mode */
  364. bool gdb_can_reverse(void)
  365. {
  366. return false;
  367. }
  368. /*
  369. * Break/Watch point helpers
  370. */
  371. bool gdb_supports_guest_debug(void)
  372. {
  373. /* user-mode == TCG == supported */
  374. return true;
  375. }
  376. int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
  377. {
  378. CPUState *cpu;
  379. int err = 0;
  380. switch (type) {
  381. case GDB_BREAKPOINT_SW:
  382. case GDB_BREAKPOINT_HW:
  383. CPU_FOREACH(cpu) {
  384. err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
  385. if (err) {
  386. break;
  387. }
  388. }
  389. return err;
  390. default:
  391. /* user-mode doesn't support watchpoints */
  392. return -ENOSYS;
  393. }
  394. }
  395. int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
  396. {
  397. CPUState *cpu;
  398. int err = 0;
  399. switch (type) {
  400. case GDB_BREAKPOINT_SW:
  401. case GDB_BREAKPOINT_HW:
  402. CPU_FOREACH(cpu) {
  403. err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
  404. if (err) {
  405. break;
  406. }
  407. }
  408. return err;
  409. default:
  410. /* user-mode doesn't support watchpoints */
  411. return -ENOSYS;
  412. }
  413. }
  414. void gdb_breakpoint_remove_all(CPUState *cs)
  415. {
  416. cpu_breakpoint_remove_all(cs, BP_GDB);
  417. }
  418. /*
  419. * For user-mode syscall support we send the system call immediately
  420. * and then return control to gdb for it to process the syscall request.
  421. * Since the protocol requires that gdb hands control back to us
  422. * using a "here are the results" F packet, we don't need to check
  423. * gdb_handlesig's return value (which is the signal to deliver if
  424. * execution was resumed via a continue packet).
  425. */
  426. void gdb_syscall_handling(const char *syscall_packet)
  427. {
  428. gdb_put_packet(syscall_packet);
  429. gdb_handlesig(gdbserver_state.c_cpu, 0);
  430. }