user.c 11 KB

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