gdb.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * This provides the callbacks and functions that KGDB needs to share between
  3. * the core, I/O and arch-specific portions.
  4. *
  5. * Author: Amit Kale <amitkale@linsyssoft.com> and
  6. * Tom Rini <trini@kernel.crashing.org>
  7. *
  8. * Copyright (C) 2008 Wind River Systems, Inc. *
  9. * 2001-2004 (c) Amit S. Kale and 2003-2005 (c) MontaVista Software, Inc.
  10. * This file is licensed under the terms of the GNU General Public License
  11. * version 2. This program is licensed "as is" without any warranty of any
  12. * kind, whether express or implied.
  13. */
  14. /* and, since gdb can't do error strings, well, we have this bullshit. */
  15. #define Einval "22"
  16. #define Enoent "02"
  17. #define Eio "05"
  18. extern int connected;
  19. extern int setting_breakpoint;
  20. extern int cpu_doing_single_step;
  21. extern struct task_struct *usethread;
  22. extern struct task_struct *contthread;
  23. extern char breakpoint[], ebreakpoint[];
  24. enum bptype {
  25. BP_BREAKPOINT = 0,
  26. BP_HARDWARE_BREAKPOINT,
  27. BP_WRITE_WATCHPOINT,
  28. BP_READ_WATCHPOINT,
  29. BP_ACCESS_WATCHPOINT,
  30. BP_POKE_BREAKPOINT,
  31. };
  32. enum bpstate {
  33. BP_UNDEFINED = 0,
  34. BP_REMOVED,
  35. BP_SET,
  36. BP_ACTIVE
  37. };
  38. /*
  39. * Copyright (C) 2001-2004 Amit S. Kale
  40. */
  41. /*
  42. * BUFMAX defines the maximum number of characters in inbound/outbound
  43. * buffers at least NUMREGBYTES*2 are needed for register packets
  44. * Longer buffer is needed to list all threads
  45. */
  46. #define BUFMAX 1024
  47. /*
  48. * Note that this register image is in a different order than
  49. * the register image that Linux produces at interrupt time.
  50. *
  51. * Linux's register image is defined by struct pt_regs in ptrace.h.
  52. * Just why GDB uses a different order is a historical mystery.
  53. */
  54. /* this is very x86_64 specific. Later, all code that messes with such things
  55. * needs to be in amd64.c
  56. * Please don't add an #ifdef here. Please
  57. */
  58. enum regnames {
  59. GDB_AX, /* 0 */
  60. GDB_BX, /* 1 */
  61. GDB_CX, /* 2 */
  62. GDB_DX, /* 3 */
  63. GDB_SI, /* 4 */
  64. GDB_DI, /* 5 */
  65. GDB_BP, /* 6 */
  66. GDB_SP, /* 7 */
  67. GDB_R8, /* 8 */
  68. GDB_R9, /* 9 */
  69. GDB_R10, /* 10 */
  70. GDB_R11, /* 11 */
  71. GDB_R12, /* 12 */
  72. GDB_R13, /* 13 */
  73. GDB_R14, /* 14 */
  74. GDB_R15, /* 15 */
  75. GDB_PC, /* 16 */
  76. GDB_PS, /* 17 */
  77. GDB_CS, /* 18 */
  78. GDB_SS, /* 19 */
  79. GDB_DS, /* 20 */
  80. GDB_ES, /* 21 */
  81. GDB_FS, /* 22 */
  82. GDB_GS, /* 23 */
  83. };
  84. // Again, this is very gdb-specific. Order should be maintained the table above
  85. static const
  86. char* regstrs[] = {
  87. "AX",
  88. "BX",
  89. "CX",
  90. "DX",
  91. "SI",
  92. "DI",
  93. "BP",
  94. "SP",
  95. "R8",
  96. "R9",
  97. "R10",
  98. "R11",
  99. "R12",
  100. "R13",
  101. "R14",
  102. "R15",
  103. "PC",
  104. "PS",
  105. "CS",
  106. "SS",
  107. "DS",
  108. "ES",
  109. "FS",
  110. "GS",
  111. };
  112. #define GDB_ORIG_AX 57
  113. #define DBG_MAX_REG_NUM 24
  114. /* 17 64 bit regs and 5 32 bit regs */
  115. #define NUMREGBYTES ((17 * 8) + (5 * 4))
  116. struct bkpt {
  117. unsigned long bpt_addr;
  118. unsigned char saved_instr[16];
  119. enum bptype type;
  120. enum bpstate state;
  121. };
  122. extern uint64_t arch_get_pc(struct state *ks);
  123. extern char *dbg_get_reg(int regno, void *mem, uintptr_t *regs);
  124. extern int dbg_set_reg(int regno, void *mem, uintptr_t *regs);
  125. /**
  126. * arch_handle_exception - Handle architecture specific GDB packets.
  127. * @vector: The error vector of the exception that happened.
  128. * @signo: The signal number of the exception that happened.
  129. * @err_code: The error code of the exception that happened.
  130. * @remcom_in_buffer: The buffer of the packet we have read.
  131. * @remcom_out_buffer: The buffer of %BUFMAX bytes to write a packet into.
  132. * @regs: The &struct pt_regs of the current process.
  133. *
  134. * This function MUST handle the 'c' and 's' command packets,
  135. * as well packets to set / remove a hardware breakpoint, if used.
  136. * If there are additional packets which the hardware needs to handle,
  137. * they are handled here. The code should return -1 if it wants to
  138. * process more packets, and a %0 or %1 if it wants to exit from the
  139. * kgdb callback.
  140. */
  141. extern int
  142. arch_handle_exception(int vector, int signo, int err_code,
  143. char *remcom_in_buffer,
  144. char *remcom_out_buffer,
  145. uintptr_t*regs);
  146. /**
  147. * arch_set_pc - Generic call back to the program counter
  148. * @regs: Current &struct pt_regs.
  149. * @pc: The new value for the program counter
  150. *
  151. * This function handles updating the program counter and requires an
  152. * architecture specific implementation.
  153. */
  154. extern void arch_set_pc(uintptr_t *regs, unsigned long pc);
  155. /* Optional functions. */
  156. extern int validate_break_address(unsigned long addr);
  157. extern char *arch_set_breakpoint(struct state *ks, struct bkpt *bpt);
  158. extern char *arch_remove_breakpoint(struct state *ks, struct bkpt *bpt);
  159. // Leave this for now but we should probably just use the chan abstraction
  160. // for this nonsense.
  161. /**
  162. * struct io - Describe the interface for an I/O driver to talk with KGDB.
  163. * @name: Name of the I/O driver.
  164. * @read_char: Pointer to a function that will return one char.
  165. * @write_char: Pointer to a function that will write one char.
  166. * @flush: Pointer to a function that will flush any pending writes.
  167. * @init: Pointer to a function that will initialize the device.
  168. * @pre_exception: Pointer to a function that will do any prep work for
  169. * the I/O driver.
  170. * @post_exception: Pointer to a function that will do any cleanup work
  171. * for the I/O driver.
  172. * @is_console: 1 if the end device is a console 0 if the I/O device is
  173. * not a console
  174. */
  175. struct io {
  176. const char *name;
  177. int (*read_char) (void);
  178. void (*write_char) (uint8_t);
  179. void (*flush) (void);
  180. int (*init) (void);
  181. void (*pre_exception) (void);
  182. void (*post_exception) (void);
  183. int is_console;
  184. };
  185. int hex2long(char **ptr, unsigned long *long_val);
  186. char *mem2hex(unsigned char *mem, char *buf, int count);
  187. char *hex2mem(char *buf, unsigned char *mem, int count);
  188. void gdb_cmd_reg_get(struct state *ks);
  189. void gdb_cmd_reg_set(struct state *ks);
  190. uint64_t arch_get_reg(struct state *ks, int regnum);
  191. extern int isremovedbreak(unsigned long addr);
  192. extern void schedule_breakpoint(void);
  193. extern int
  194. handle_exception(int ex_vector, int signo, int err_code,
  195. uintptr_t *regs);