lwp_dbg.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-07-11 RT-Thread first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <lwp.h>
  13. /**
  14. * @brief Check if the current thread is in debug state.
  15. *
  16. * @return int Returns 1 if the thread is in debug state, 0 otherwise.
  17. */
  18. int dbg_thread_in_debug(void)
  19. {
  20. int ret = 0;
  21. struct rt_lwp *lwp = lwp_self();
  22. if (lwp && lwp->debug)
  23. {
  24. ret = 1;
  25. }
  26. return ret;
  27. }
  28. struct dbg_ops_t *rt_dbg_ops = RT_NULL;
  29. RTM_EXPORT(rt_dbg_ops);
  30. /**
  31. * @brief Register debugger operations.
  32. *
  33. * @param dbg_ops Pointer to debugger operations structure.
  34. */
  35. void dbg_register(struct dbg_ops_t *dbg_ops)
  36. {
  37. rt_dbg_ops = dbg_ops;
  38. }
  39. RTM_EXPORT(dbg_register);
  40. /**
  41. * @brief Debug command handler function
  42. *
  43. * @param[in] argc Number of command arguments
  44. * @param[in] argv Array of command argument strings
  45. *
  46. * @return int Returns the result from the debug operations handler if available,
  47. * otherwise returns -1 to indicate failure
  48. *
  49. * @note This function serves as a wrapper for debug operations, delegating to the registered
  50. * debug operations handler if available. If no debug operations are registered,
  51. * it prints an error message.
  52. */
  53. static int dbg(int argc, char **argv)
  54. {
  55. int ret = -1;
  56. if (rt_dbg_ops)
  57. {
  58. ret = rt_dbg_ops->dbg(argc, argv);
  59. }
  60. else
  61. {
  62. rt_kprintf("Error: DBG command is not enabled!\n");
  63. }
  64. return ret;
  65. }
  66. MSH_CMD_EXPORT(dbg, dbg);
  67. /**
  68. * @brief Get the current instruction value from debug operations
  69. *
  70. * @return uint32_t Returns the current instruction value if debug operations are available,
  71. * otherwise returns 0
  72. *
  73. * @note This function retrieves the current instruction value by calling the registered
  74. * debug operations handler if available. If no debug operations are registered,
  75. * it returns 0.
  76. */
  77. uint32_t dbg_get_ins(void)
  78. {
  79. uint32_t ins = 0;
  80. if (rt_dbg_ops)
  81. {
  82. ins = rt_dbg_ops->arch_get_ins();
  83. }
  84. return ins;
  85. }
  86. /**
  87. * @brief Activates single-step debugging mode if debug operations are registered
  88. *
  89. * @note This function checks if debug operations are registered (rt_dbg_ops) and if so,
  90. * calls the architecture-specific single-step activation function.
  91. */
  92. void dbg_activate_step(void)
  93. {
  94. if (rt_dbg_ops)
  95. {
  96. rt_dbg_ops->arch_activate_step();
  97. }
  98. }
  99. /**
  100. * @brief Deactivates single-step debugging mode if debug operations are registered
  101. *
  102. * @note This function checks if debug operations are registered (rt_dbg_ops) and if so,
  103. * calls the architecture-specific single-step deactivation function.
  104. */
  105. void dbg_deactivate_step(void)
  106. {
  107. if (rt_dbg_ops)
  108. {
  109. rt_dbg_ops->arch_deactivate_step();
  110. }
  111. }
  112. /**
  113. * @brief Checks for debug events and processes them if debug operations are registered
  114. *
  115. * @param[in] regs Pointer to the hardware exception stack containing register values
  116. * @param[in] esr Exception Syndrome Register value
  117. *
  118. * @return int Returns the result from the debug event check (0 if no debug operations registered)
  119. *
  120. * @note This function checks if debug operations are registered (rt_dbg_ops) and if so,
  121. * calls the debug event checking function with the provided registers and exception status.
  122. * If no debug operations are registered, it returns 0.
  123. */
  124. int dbg_check_event(struct rt_hw_exp_stack *regs, unsigned long esr)
  125. {
  126. int ret = 0;
  127. if (rt_dbg_ops)
  128. {
  129. ret = rt_dbg_ops->check_debug_event(regs, esr);
  130. }
  131. return ret;
  132. }
  133. /**
  134. * @brief Gets the GDB server communication channel if debug operations are registered
  135. *
  136. * @return rt_channel_t Returns the GDB server channel (RT_NULL if no debug operations registered)
  137. *
  138. * @note This function checks if debug operations are registered (rt_dbg_ops) and if so,
  139. * retrieves the GDB server communication channel from the registered operations.
  140. * If no debug operations are registered, it returns RT_NULL.
  141. */
  142. rt_channel_t gdb_server_channel(void)
  143. {
  144. rt_channel_t ret = RT_NULL;
  145. if (rt_dbg_ops)
  146. {
  147. ret = rt_dbg_ops->gdb_get_server_channel();
  148. }
  149. return ret;
  150. }
  151. /**
  152. * @brief Gets the current step type from debug operations
  153. *
  154. * @return int The current step type (0 if no debug operations are registered)
  155. *
  156. * @note This function checks if debug operations are registered (rt_dbg_ops) and if so,
  157. * retrieves the current step type from the registered debug operations.
  158. * If no debug operations are registered, it returns 0.
  159. */
  160. int dbg_step_type(void)
  161. {
  162. int ret = 0;
  163. if (rt_dbg_ops)
  164. {
  165. ret = rt_dbg_ops->gdb_get_step_type();
  166. }
  167. return ret;
  168. }
  169. /**
  170. * @brief Handles debug attach request
  171. *
  172. * @param[in] pc Pointer to the program counter value
  173. *
  174. * @note This function checks if debug operations are registered and calls
  175. * the debug attach request handler if available.
  176. */
  177. void dbg_attach_req(void *pc)
  178. {
  179. if (rt_dbg_ops)
  180. {
  181. rt_dbg_ops->lwp_check_debug_attach_req(pc);
  182. }
  183. }
  184. /**
  185. * @brief Checks if debug suspend is requested
  186. *
  187. * @return int Returns the suspend status (0 if no debug operations are registered)
  188. *
  189. * @note This function checks if debug operations are registered and calls
  190. * the debug suspend check handler if available.
  191. */
  192. int dbg_check_suspend(void)
  193. {
  194. int ret = 0;
  195. if (rt_dbg_ops)
  196. {
  197. ret = rt_dbg_ops->lwp_check_debug_suspend();
  198. }
  199. return ret;
  200. }