internals.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * gdbstub internals
  3. *
  4. * Copyright (c) 2022 Linaro Ltd
  5. *
  6. * SPDX-License-Identifier: GPL-2.0-or-later
  7. */
  8. #ifndef GDBSTUB_INTERNALS_H
  9. #define GDBSTUB_INTERNALS_H
  10. #include "exec/cpu-common.h"
  11. #define MAX_PACKET_LENGTH 4096
  12. /*
  13. * Shared structures and definitions
  14. */
  15. enum {
  16. GDB_SIGNAL_0 = 0,
  17. GDB_SIGNAL_INT = 2,
  18. GDB_SIGNAL_QUIT = 3,
  19. GDB_SIGNAL_TRAP = 5,
  20. GDB_SIGNAL_ABRT = 6,
  21. GDB_SIGNAL_ALRM = 14,
  22. GDB_SIGNAL_STOP = 17,
  23. GDB_SIGNAL_IO = 23,
  24. GDB_SIGNAL_XCPU = 24,
  25. GDB_SIGNAL_UNKNOWN = 143
  26. };
  27. typedef struct GDBProcess {
  28. uint32_t pid;
  29. bool attached;
  30. char *target_xml;
  31. } GDBProcess;
  32. enum RSState {
  33. RS_INACTIVE,
  34. RS_IDLE,
  35. RS_GETLINE,
  36. RS_GETLINE_ESC,
  37. RS_GETLINE_RLE,
  38. RS_CHKSUM1,
  39. RS_CHKSUM2,
  40. };
  41. typedef struct GDBState {
  42. bool init; /* have we been initialised? */
  43. CPUState *c_cpu; /* current CPU for step/continue ops */
  44. CPUState *g_cpu; /* current CPU for other ops */
  45. CPUState *query_cpu; /* for q{f|s}ThreadInfo */
  46. enum RSState state; /* parsing state */
  47. char line_buf[MAX_PACKET_LENGTH];
  48. int line_buf_index;
  49. int line_sum; /* running checksum */
  50. int line_csum; /* checksum at the end of the packet */
  51. GByteArray *last_packet;
  52. int signal;
  53. bool multiprocess;
  54. GDBProcess *processes;
  55. int process_num;
  56. GString *str_buf;
  57. GByteArray *mem_buf;
  58. int sstep_flags;
  59. int supported_sstep_flags;
  60. /*
  61. * Whether we are allowed to send a stop reply packet at this moment.
  62. * Must be set off after sending the stop reply itself.
  63. */
  64. bool allow_stop_reply;
  65. } GDBState;
  66. /* lives in main gdbstub.c */
  67. extern GDBState gdbserver_state;
  68. /*
  69. * Inline utility function, convert from int to hex and back
  70. */
  71. static inline int fromhex(int v)
  72. {
  73. if (v >= '0' && v <= '9') {
  74. return v - '0';
  75. } else if (v >= 'A' && v <= 'F') {
  76. return v - 'A' + 10;
  77. } else if (v >= 'a' && v <= 'f') {
  78. return v - 'a' + 10;
  79. } else {
  80. return 0;
  81. }
  82. }
  83. static inline int tohex(int v)
  84. {
  85. if (v < 10) {
  86. return v + '0';
  87. } else {
  88. return v - 10 + 'a';
  89. }
  90. }
  91. /*
  92. * Connection helpers for both system and user backends
  93. */
  94. void gdb_put_strbuf(void);
  95. int gdb_put_packet_binary(const char *buf, int len, bool dump);
  96. void gdb_memtohex(GString *buf, const uint8_t *mem, int len);
  97. void gdb_memtox(GString *buf, const char *mem, int len);
  98. void gdb_read_byte(uint8_t ch);
  99. /*
  100. * Packet acknowledgement - we handle this slightly differently
  101. * between user and system mode, mainly to deal with the differences
  102. * between the flexible chardev and the direct fd approaches.
  103. *
  104. * We currently don't support a negotiated QStartNoAckMode
  105. */
  106. /**
  107. * gdb_got_immediate_ack() - check ok to continue
  108. *
  109. * Returns true to continue, false to re-transmit for user only, the
  110. * system stub always returns true.
  111. */
  112. bool gdb_got_immediate_ack(void);
  113. /* utility helpers */
  114. GDBProcess *gdb_get_process(uint32_t pid);
  115. CPUState *gdb_get_first_cpu_in_process(GDBProcess *process);
  116. CPUState *gdb_first_attached_cpu(void);
  117. void gdb_append_thread_id(CPUState *cpu, GString *buf);
  118. int gdb_get_cpu_index(CPUState *cpu);
  119. unsigned int gdb_get_max_cpus(void); /* both */
  120. bool gdb_can_reverse(void); /* system emulation, stub for user */
  121. int gdb_target_sigtrap(void); /* user */
  122. void gdb_create_default_process(GDBState *s);
  123. /* signal mapping, common for system, specialised for user-mode */
  124. int gdb_signal_to_target(int sig);
  125. int gdb_target_signal_to_gdb(int sig);
  126. int gdb_get_char(void); /* user only */
  127. /**
  128. * gdb_continue() - handle continue in mode specific way.
  129. */
  130. void gdb_continue(void);
  131. /**
  132. * gdb_continue_partial() - handle partial continue in mode specific way.
  133. */
  134. int gdb_continue_partial(char *newstates);
  135. /*
  136. * Helpers with separate system and user implementations
  137. */
  138. void gdb_put_buffer(const uint8_t *buf, int len);
  139. /*
  140. * Command handlers - either specialised or system or user only
  141. */
  142. void gdb_init_gdbserver_state(void);
  143. void gdb_handle_query_rcmd(GArray *params, void *ctx); /* system */
  144. void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */
  145. void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */
  146. void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx); /*user */
  147. void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */
  148. void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */
  149. void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */
  150. void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */
  151. void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */
  152. void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx); /* user */
  153. void gdb_handle_query_supported_user(const char *gdb_supported); /* user */
  154. bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid); /* user */
  155. bool gdb_handle_detach_user(uint32_t pid); /* user */
  156. void gdb_handle_query_attached(GArray *params, void *ctx); /* both */
  157. /* system only */
  158. void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *ctx);
  159. void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx);
  160. /* sycall handling */
  161. void gdb_handle_file_io(GArray *params, void *user_ctx);
  162. bool gdb_handled_syscall(void);
  163. void gdb_disable_syscalls(void);
  164. void gdb_syscall_reset(void);
  165. /* user/system specific syscall handling */
  166. void gdb_syscall_handling(const char *syscall_packet);
  167. /*
  168. * Break/Watch point support - there is an implementation for system
  169. * and user mode.
  170. */
  171. bool gdb_supports_guest_debug(void);
  172. int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len);
  173. int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len);
  174. void gdb_breakpoint_remove_all(CPUState *cs);
  175. /**
  176. * gdb_target_memory_rw_debug() - handle debug access to memory
  177. * @cs: CPUState
  178. * @addr: nominal address, could be an entire physical address
  179. * @buf: data
  180. * @len: length of access
  181. * @is_write: is it a write operation
  182. *
  183. * This function is specialised depending on the mode we are running
  184. * in. For system guests we can switch the interpretation of the
  185. * address to a physical address.
  186. */
  187. int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
  188. uint8_t *buf, int len, bool is_write);
  189. #endif /* GDBSTUB_INTERNALS_H */