2
0

internals.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. char target_xml[1024];
  30. } GDBProcess;
  31. enum RSState {
  32. RS_INACTIVE,
  33. RS_IDLE,
  34. RS_GETLINE,
  35. RS_GETLINE_ESC,
  36. RS_GETLINE_RLE,
  37. RS_CHKSUM1,
  38. RS_CHKSUM2,
  39. };
  40. typedef struct GDBState {
  41. bool init; /* have we been initialised? */
  42. CPUState *c_cpu; /* current CPU for step/continue ops */
  43. CPUState *g_cpu; /* current CPU for other ops */
  44. CPUState *query_cpu; /* for q{f|s}ThreadInfo */
  45. enum RSState state; /* parsing state */
  46. char line_buf[MAX_PACKET_LENGTH];
  47. int line_buf_index;
  48. int line_sum; /* running checksum */
  49. int line_csum; /* checksum at the end of the packet */
  50. GByteArray *last_packet;
  51. int signal;
  52. bool multiprocess;
  53. GDBProcess *processes;
  54. int process_num;
  55. GString *str_buf;
  56. GByteArray *mem_buf;
  57. int sstep_flags;
  58. int supported_sstep_flags;
  59. } GDBState;
  60. /* lives in main gdbstub.c */
  61. extern GDBState gdbserver_state;
  62. /*
  63. * Inline utility function, convert from int to hex and back
  64. */
  65. static inline int fromhex(int v)
  66. {
  67. if (v >= '0' && v <= '9') {
  68. return v - '0';
  69. } else if (v >= 'A' && v <= 'F') {
  70. return v - 'A' + 10;
  71. } else if (v >= 'a' && v <= 'f') {
  72. return v - 'a' + 10;
  73. } else {
  74. return 0;
  75. }
  76. }
  77. static inline int tohex(int v)
  78. {
  79. if (v < 10) {
  80. return v + '0';
  81. } else {
  82. return v - 10 + 'a';
  83. }
  84. }
  85. /*
  86. * Connection helpers for both softmmu and user backends
  87. */
  88. void gdb_put_strbuf(void);
  89. int gdb_put_packet(const char *buf);
  90. int gdb_put_packet_binary(const char *buf, int len, bool dump);
  91. void gdb_hextomem(GByteArray *mem, const char *buf, int len);
  92. void gdb_memtohex(GString *buf, const uint8_t *mem, int len);
  93. void gdb_memtox(GString *buf, const char *mem, int len);
  94. void gdb_read_byte(uint8_t ch);
  95. /*
  96. * Packet acknowledgement - we handle this slightly differently
  97. * between user and softmmu mode, mainly to deal with the differences
  98. * between the flexible chardev and the direct fd approaches.
  99. *
  100. * We currently don't support a negotiated QStartNoAckMode
  101. */
  102. /**
  103. * gdb_got_immediate_ack() - check ok to continue
  104. *
  105. * Returns true to continue, false to re-transmit for user only, the
  106. * softmmu stub always returns true.
  107. */
  108. bool gdb_got_immediate_ack(void);
  109. /* utility helpers */
  110. CPUState *gdb_first_attached_cpu(void);
  111. void gdb_append_thread_id(CPUState *cpu, GString *buf);
  112. int gdb_get_cpu_index(CPUState *cpu);
  113. unsigned int gdb_get_max_cpus(void); /* both */
  114. bool gdb_can_reverse(void); /* softmmu, stub for user */
  115. void gdb_create_default_process(GDBState *s);
  116. /* signal mapping, common for softmmu, specialised for user-mode */
  117. int gdb_signal_to_target(int sig);
  118. int gdb_target_signal_to_gdb(int sig);
  119. int gdb_get_char(void); /* user only */
  120. /**
  121. * gdb_continue() - handle continue in mode specific way.
  122. */
  123. void gdb_continue(void);
  124. /**
  125. * gdb_continue_partial() - handle partial continue in mode specific way.
  126. */
  127. int gdb_continue_partial(char *newstates);
  128. /*
  129. * Helpers with separate softmmu and user implementations
  130. */
  131. void gdb_put_buffer(const uint8_t *buf, int len);
  132. /*
  133. * Command handlers - either specialised or softmmu or user only
  134. */
  135. void gdb_init_gdbserver_state(void);
  136. typedef enum GDBThreadIdKind {
  137. GDB_ONE_THREAD = 0,
  138. GDB_ALL_THREADS, /* One process, all threads */
  139. GDB_ALL_PROCESSES,
  140. GDB_READ_THREAD_ERR
  141. } GDBThreadIdKind;
  142. typedef union GdbCmdVariant {
  143. const char *data;
  144. uint8_t opcode;
  145. unsigned long val_ul;
  146. unsigned long long val_ull;
  147. struct {
  148. GDBThreadIdKind kind;
  149. uint32_t pid;
  150. uint32_t tid;
  151. } thread_id;
  152. } GdbCmdVariant;
  153. #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
  154. void gdb_handle_query_rcmd(GArray *params, void *user_ctx); /* softmmu */
  155. void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */
  156. void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */
  157. void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */
  158. /* softmmu only */
  159. void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *user_ctx);
  160. void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx);
  161. /* sycall handling */
  162. void gdb_handle_file_io(GArray *params, void *user_ctx);
  163. bool gdb_handled_syscall(void);
  164. void gdb_disable_syscalls(void);
  165. void gdb_syscall_reset(void);
  166. /* user/softmmu specific syscall handling */
  167. void gdb_syscall_handling(const char *syscall_packet);
  168. /*
  169. * Break/Watch point support - there is an implementation for softmmu
  170. * and user mode.
  171. */
  172. bool gdb_supports_guest_debug(void);
  173. int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len);
  174. int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len);
  175. void gdb_breakpoint_remove_all(CPUState *cs);
  176. /**
  177. * gdb_target_memory_rw_debug() - handle debug access to memory
  178. * @cs: CPUState
  179. * @addr: nominal address, could be an entire physical address
  180. * @buf: data
  181. * @len: length of access
  182. * @is_write: is it a write operation
  183. *
  184. * This function is specialised depending on the mode we are running
  185. * in. For softmmu guests we can switch the interpretation of the
  186. * address to a physical address.
  187. */
  188. int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
  189. uint8_t *buf, int len, bool is_write);
  190. #endif /* GDBSTUB_INTERNALS_H */