esp_gdbstub_common.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include <stdbool.h>
  18. #include "gdbstub_target_config.h"
  19. #include "esp_gdbstub_arch.h"
  20. #include "sdkconfig.h"
  21. #ifdef CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/task.h"
  24. #include "freertos/task_snapshot.h"
  25. #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  26. /* Internal error codes used by the routines that parse the incoming gdb packet */
  27. #define GDBSTUB_ST_ENDPACKET -1
  28. #define GDBSTUB_ST_ERR -2
  29. #define GDBSTUB_ST_OK -3
  30. #define GDBSTUB_ST_CONT -4
  31. /* Special task index values */
  32. #define GDBSTUB_CUR_TASK_INDEX_UNKNOWN -1
  33. /* Cab be set to a lower value in gdbstub_target_config.h */
  34. #ifndef GDBSTUB_CMD_BUFLEN
  35. #define GDBSTUB_CMD_BUFLEN 512
  36. #endif
  37. #if CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  38. typedef enum {
  39. GDBSTUB_NOT_STARTED,
  40. GDBSTUB_STARTED,
  41. GDBSTUB_TASK_SUPPORT_DISABLED
  42. } esp_gdbstub_state_t;
  43. #define GDBSTUB_TASKS_NUM CONFIG_ESP_GDBSTUB_MAX_TASKS
  44. #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  45. /* gdbstub temporary run-time data, stored in .bss to reduce stack usage */
  46. typedef struct {
  47. esp_gdbstub_gdb_regfile_t regfile;
  48. int signal;
  49. #if CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  50. esp_gdbstub_state_t state;
  51. int task_count;
  52. int paniced_task_index;
  53. int current_task_index;
  54. int thread_info_index; //!< index of the last task passed to qsThreadInfo
  55. esp_gdbstub_frame_t paniced_frame;
  56. TaskSnapshot_t tasks[GDBSTUB_TASKS_NUM]; // TODO: add an API to get snapshots one by one
  57. #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  58. } esp_gdbstub_scratch_t;
  59. /**** Functions provided by the architecture specific part ****/
  60. /**
  61. * @param frame exception frame pointer
  62. * @return the appropriate "signal" number for the given exception cause
  63. */
  64. int esp_gdbstub_get_signal(const esp_gdbstub_frame_t *frame);
  65. /**
  66. * Write registers from the exception frame to the GDB register file
  67. * @param frame exception frame to parse
  68. * @param dst pointer to the GDB register file
  69. */
  70. void esp_gdbstub_frame_to_regfile(const esp_gdbstub_frame_t *frame, esp_gdbstub_gdb_regfile_t *dst);
  71. #if CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  72. /**
  73. * Write registers from the saved frame of a given task to the GDB register file
  74. * @param tcb pointer to the TCB of the task
  75. * @param dst pointer to the GDB register file
  76. */
  77. void esp_gdbstub_tcb_to_regfile(TaskHandle_t tcb, esp_gdbstub_gdb_regfile_t *dst);
  78. #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  79. /**** Functions provided by the target specific part ****/
  80. /**
  81. * Do target-specific initialization before gdbstub can start communicating.
  82. * This may involve, for example, configuring the UART.
  83. */
  84. void esp_gdbstub_target_init(void);
  85. /**
  86. * Receive a byte from the GDB client. Blocks until a byte is available.
  87. * @return received byte
  88. */
  89. int esp_gdbstub_getchar(void);
  90. /**
  91. * Send a byte to the GDB client
  92. * @param c byte to send
  93. */
  94. void esp_gdbstub_putchar(int c);
  95. /**
  96. * Read a byte from target memory
  97. * @param ptr address
  98. * @return byte value, or GDBSTUB_ST_ERR if the address is not readable
  99. */
  100. int esp_gdbstub_readmem(intptr_t addr);
  101. /**
  102. * Make sure all bytes sent using putchar() end up at the host.
  103. * (Usually stubbed for UART, but can be useful for other channels)
  104. */
  105. void esp_gdbstub_flush(void);
  106. /**
  107. * Write a byte to target memory
  108. * @param addr address
  109. * @param data data byte
  110. * @return 0 in case of success, -1 in case of error
  111. */
  112. int esp_gdbstub_writemem(unsigned int addr, unsigned char data);
  113. /**
  114. * Read a data from fifo and detect start symbol
  115. * @return 1 if break symbol was detected, or 0 if not
  116. */
  117. int esp_gdbstub_getfifo(void);
  118. /**** GDB packet related functions ****/
  119. /** Begin a packet */
  120. void esp_gdbstub_send_start(void);
  121. /** Send a character as part of the packet */
  122. void esp_gdbstub_send_char(char c);
  123. /** Send a string as part of the packet */
  124. void esp_gdbstub_send_str(const char *s);
  125. /** Send a hex value as part of the packet */
  126. void esp_gdbstub_send_hex(int val, int bits);
  127. /** Finish sending the packet */
  128. void esp_gdbstub_send_end(void);
  129. /** Send a packet with a string as content */
  130. void esp_gdbstub_send_str_packet(const char *str);
  131. /** Get a hex value from the gdb packet */
  132. uint32_t esp_gdbstub_gethex(const unsigned char **ptr, int bits);
  133. /** Read, unescape, and validate the incoming GDB command */
  134. int esp_gdbstub_read_command(unsigned char **out_cmd, size_t *out_size);
  135. /** Handle a command received from gdb */
  136. int esp_gdbstub_handle_command(unsigned char *cmd, int len);