2
0

internals.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_IO = 23,
  23. GDB_SIGNAL_XCPU = 24,
  24. GDB_SIGNAL_UNKNOWN = 143
  25. };
  26. typedef struct GDBProcess {
  27. uint32_t pid;
  28. bool attached;
  29. /* If gdb sends qXfer:features:read:target.xml this will be populated */
  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 softmmu and user backends
  93. */
  94. void gdb_put_strbuf(void);
  95. int gdb_put_packet(const char *buf);
  96. int gdb_put_packet_binary(const char *buf, int len, bool dump);
  97. void gdb_hextomem(GByteArray *mem, const char *buf, int len);
  98. void gdb_memtohex(GString *buf, const uint8_t *mem, int len);
  99. void gdb_memtox(GString *buf, const char *mem, int len);
  100. void gdb_read_byte(uint8_t ch);
  101. /*
  102. * Packet acknowledgement - we handle this slightly differently
  103. * between user and softmmu mode, mainly to deal with the differences
  104. * between the flexible chardev and the direct fd approaches.
  105. *
  106. * We currently don't support a negotiated QStartNoAckMode
  107. */
  108. /**
  109. * gdb_got_immediate_ack() - check ok to continue
  110. *
  111. * Returns true to continue, false to re-transmit for user only, the
  112. * softmmu stub always returns true.
  113. */
  114. bool gdb_got_immediate_ack(void);
  115. /* utility helpers */
  116. GDBProcess *gdb_get_process(uint32_t pid);
  117. CPUState *gdb_get_first_cpu_in_process(GDBProcess *process);
  118. CPUState *gdb_first_attached_cpu(void);
  119. void gdb_append_thread_id(CPUState *cpu, GString *buf);
  120. int gdb_get_cpu_index(CPUState *cpu);
  121. unsigned int gdb_get_max_cpus(void); /* both */
  122. bool gdb_can_reverse(void); /* softmmu, stub for user */
  123. void gdb_create_default_process(GDBState *s);
  124. /* signal mapping, common for softmmu, specialised for user-mode */
  125. int gdb_signal_to_target(int sig);
  126. int gdb_target_signal_to_gdb(int sig);
  127. int gdb_get_char(void); /* user only */
  128. /**
  129. * gdb_continue() - handle continue in mode specific way.
  130. */
  131. void gdb_continue(void);
  132. /**
  133. * gdb_continue_partial() - handle partial continue in mode specific way.
  134. */
  135. int gdb_continue_partial(char *newstates);
  136. /*
  137. * Helpers with separate softmmu and user implementations
  138. */
  139. void gdb_put_buffer(const uint8_t *buf, int len);
  140. /*
  141. * Command handlers - either specialised or softmmu or user only
  142. */
  143. void gdb_init_gdbserver_state(void);
  144. typedef enum GDBThreadIdKind {
  145. GDB_ONE_THREAD = 0,
  146. GDB_ALL_THREADS, /* One process, all threads */
  147. GDB_ALL_PROCESSES,
  148. GDB_READ_THREAD_ERR
  149. } GDBThreadIdKind;
  150. typedef union GdbCmdVariant {
  151. const char *data;
  152. uint8_t opcode;
  153. unsigned long val_ul;
  154. unsigned long long val_ull;
  155. struct {
  156. GDBThreadIdKind kind;
  157. uint32_t pid;
  158. uint32_t tid;
  159. } thread_id;
  160. } GdbCmdVariant;
  161. #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
  162. void gdb_handle_query_rcmd(GArray *params, void *user_ctx); /* softmmu */
  163. void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */
  164. void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */
  165. void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */
  166. void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */
  167. void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */
  168. void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */
  169. void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */
  170. void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */
  171. /* softmmu only */
  172. void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *user_ctx);
  173. void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx);
  174. /* sycall handling */
  175. void gdb_handle_file_io(GArray *params, void *user_ctx);
  176. bool gdb_handled_syscall(void);
  177. void gdb_disable_syscalls(void);
  178. void gdb_syscall_reset(void);
  179. /* user/softmmu specific syscall handling */
  180. void gdb_syscall_handling(const char *syscall_packet);
  181. /*
  182. * Break/Watch point support - there is an implementation for softmmu
  183. * and user mode.
  184. */
  185. bool gdb_supports_guest_debug(void);
  186. int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len);
  187. int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len);
  188. void gdb_breakpoint_remove_all(CPUState *cs);
  189. /**
  190. * gdb_target_memory_rw_debug() - handle debug access to memory
  191. * @cs: CPUState
  192. * @addr: nominal address, could be an entire physical address
  193. * @buf: data
  194. * @len: length of access
  195. * @is_write: is it a write operation
  196. *
  197. * This function is specialised depending on the mode we are running
  198. * in. For softmmu guests we can switch the interpretation of the
  199. * address to a physical address.
  200. */
  201. int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
  202. uint8_t *buf, int len, bool is_write);
  203. #endif /* GDBSTUB_INTERNALS_H */