esp_gdbstub_common.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "esp_gdbstub.h"
  19. #include "sdkconfig.h"
  20. #ifdef CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/task.h"
  23. #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  24. /* Internal error codes used by the routines that parse the incoming gdb packet */
  25. #define GDBSTUB_ST_ENDPACKET -1
  26. #define GDBSTUB_ST_ERR -2
  27. #define GDBSTUB_ST_OK -3
  28. /* Special task index values */
  29. #define GDBSTUB_CUR_TASK_INDEX_UNKNOWN -1
  30. /* Cab be set to a lower value in gdbstub_target_config.h */
  31. #ifndef GDBSTUB_CMD_BUFLEN
  32. #define GDBSTUB_CMD_BUFLEN 512
  33. #endif
  34. #if CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  35. typedef enum {
  36. GDBSTUB_NOT_STARTED,
  37. GDBSTUB_STARTED,
  38. GDBSTUB_TASK_SUPPORT_DISABLED
  39. } esp_gdbstub_state_t;
  40. #define GDBSTUB_TASKS_NUM CONFIG_ESP_GDBSTUB_MAX_TASKS
  41. #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  42. /* gdbstub temporary run-time data, stored in .bss to reduce stack usage */
  43. typedef struct {
  44. esp_gdbstub_gdb_regfile_t regfile;
  45. int signal;
  46. #if CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  47. esp_gdbstub_state_t state;
  48. int task_count;
  49. int paniced_task_index;
  50. int current_task_index;
  51. int thread_info_index; //!< index of the last task passed to qsThreadInfo
  52. esp_gdbstub_frame_t paniced_frame;
  53. TaskSnapshot_t tasks[GDBSTUB_TASKS_NUM]; // TODO: add an API to get snapshots one by one
  54. #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  55. } esp_gdbstub_scratch_t;
  56. /**** Functions provided by the architecture specific part ****/
  57. /**
  58. * @param frame exception frame pointer
  59. * @return the appropriate "signal" number for the given exception cause
  60. */
  61. int esp_gdbstub_get_signal(const esp_gdbstub_frame_t *frame);
  62. /**
  63. * Write registers from the exception frame to the GDB register file
  64. * @param frame exception frame to parse
  65. * @param dst pointer to the GDB register file
  66. */
  67. void esp_gdbstub_frame_to_regfile(const esp_gdbstub_frame_t *frame, esp_gdbstub_gdb_regfile_t *dst);
  68. #if CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  69. /**
  70. * Write registers from the saved frame of a given task to the GDB register file
  71. * @param tcb pointer to the TCB of the task
  72. * @param dst pointer to the GDB register file
  73. */
  74. void esp_gdbstub_tcb_to_regfile(TaskHandle_t tcb, esp_gdbstub_gdb_regfile_t *dst);
  75. #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
  76. /**** Functions provided by the target specific part ****/
  77. /**
  78. * Do target-specific initialization before gdbstub can start communicating.
  79. * This may involve, for example, configuring the UART.
  80. */
  81. void esp_gdbstub_target_init(void);
  82. /**
  83. * Receive a byte from the GDB client. Blocks until a byte is available.
  84. * @return received byte
  85. */
  86. int esp_gdbstub_getchar(void);
  87. /**
  88. * Send a byte to the GDB client
  89. * @param c byte to send
  90. */
  91. void esp_gdbstub_putchar(int c);
  92. /**
  93. * Read a byte from target memory
  94. * @param ptr address
  95. * @return byte value, or GDBSTUB_ST_ERR if the address is not readable
  96. */
  97. int esp_gdbstub_readmem(intptr_t addr);
  98. /**** GDB packet related functions ****/
  99. /** Begin a packet */
  100. void esp_gdbstub_send_start(void);
  101. /** Send a character as part of the packet */
  102. void esp_gdbstub_send_char(char c);
  103. /** Send a string as part of the packet */
  104. void esp_gdbstub_send_str(const char *s);
  105. /** Send a hex value as part of the packet */
  106. void esp_gdbstub_send_hex(int val, int bits);
  107. /** Finish sending the packet */
  108. void esp_gdbstub_send_end(void);
  109. /** Send a packet with a string as content */
  110. void esp_gdbstub_send_str_packet(const char* str);
  111. /** Get a hex value from the gdb packet */
  112. uint32_t esp_gdbstub_gethex(const unsigned char **ptr, int bits);
  113. /** Read, unescape, and validate the incoming GDB command */
  114. int esp_gdbstub_read_command(unsigned char **out_cmd, size_t *out_size);
  115. /** Handle a command received from gdb */
  116. int esp_gdbstub_handle_command(unsigned char *cmd, int len);