Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

rtthread_port.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-08-16 Rbb666 first version
  9. */
  10. #include <rtthread.h>
  11. #include "coredump.h"
  12. #include "arch/mcd_arch_interface.h"
  13. #define PRINT_COREDUMP_INFO_STRING 1 // 打印字符串格式的coredump信息
  14. /*
  15. * RT-Thread OS abstraction layer implementation for MCoreDump
  16. */
  17. typedef struct
  18. {
  19. rt_int32_t thr_cnts;
  20. rt_int32_t cur_idx;
  21. } rtthread_ti_priv_t;
  22. static rt_int32_t is_thread_object(rt_thread_t thread)
  23. {
  24. /* Check if the object is a thread (both static and dynamic) */
  25. return ((thread->type & ~RT_Object_Class_Static) == RT_Object_Class_Thread);
  26. }
  27. static int32_t rtthread_thread_cnts(struct thread_info_ops *ops)
  28. {
  29. rtthread_ti_priv_t *priv = (rtthread_ti_priv_t *)ops->priv;
  30. rt_int32_t idx = 0;
  31. struct rt_object_information *information;
  32. struct rt_object *object;
  33. struct rt_list_node *node;
  34. rt_thread_t current_thread;
  35. if (-1 == priv->thr_cnts)
  36. {
  37. information = rt_object_get_information(RT_Object_Class_Thread);
  38. mcd_assert(information != RT_NULL);
  39. current_thread = rt_thread_self();
  40. priv->cur_idx = -1; /* Initialize current thread index */
  41. for (node = information->object_list.next;
  42. node != &(information->object_list);
  43. node = node->next)
  44. {
  45. object = rt_list_entry(node, struct rt_object, list);
  46. rt_thread_t thread = (rt_thread_t)object;
  47. if (is_thread_object(thread))
  48. {
  49. /* Check if this is the current thread */
  50. if (thread == current_thread)
  51. {
  52. priv->cur_idx = idx;
  53. }
  54. idx++;
  55. }
  56. }
  57. priv->thr_cnts = idx;
  58. /* If current thread not found, default to 0 */
  59. if (priv->cur_idx == -1)
  60. {
  61. priv->cur_idx = 0;
  62. mcd_print("MCD DEBUG: Current thread not found, defaulting to index 0\n");
  63. }
  64. }
  65. return priv->thr_cnts;
  66. }
  67. static int32_t rtthread_cur_index(struct thread_info_ops *ops)
  68. {
  69. rtthread_ti_priv_t *priv = (rtthread_ti_priv_t *)ops->priv;
  70. if (-1 == priv->cur_idx)
  71. rtthread_thread_cnts(ops);
  72. return priv->cur_idx;
  73. }
  74. static void rtthread_thread_register(struct thread_info_ops *ops, int32_t idx,
  75. core_regset_type *core_regset,
  76. fp_regset_type *fp_regset)
  77. {
  78. rt_int32_t idx_l = 0;
  79. rt_int32_t current_idx = rtthread_cur_index(ops);
  80. struct rt_object_information *information;
  81. struct rt_object *object;
  82. struct rt_list_node *node;
  83. information = rt_object_get_information(RT_Object_Class_Thread);
  84. mcd_assert(information != RT_NULL);
  85. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  86. {
  87. object = rt_list_entry(node, struct rt_object, list);
  88. rt_thread_t thread = (rt_thread_t)object;
  89. if (is_thread_object(thread))
  90. {
  91. if (idx == idx_l)
  92. {
  93. /* If this is the current thread, use current registers */
  94. if (idx_l == current_idx)
  95. {
  96. mcd_memcpy(core_regset, get_cur_core_regset_address(), sizeof(core_regset_type)); // 异常中填充
  97. mcd_memcpy(fp_regset, get_cur_fp_regset_address(), sizeof(fp_regset_type));
  98. }
  99. else
  100. {
  101. /* Debug: Dynamic thread for comparison */
  102. collect_registers((uint32_t *)thread->sp, core_regset, fp_regset);
  103. }
  104. return;
  105. }
  106. idx_l++;
  107. }
  108. }
  109. }
  110. static int32_t rtthread_get_mem_cnts(struct thread_info_ops *ops)
  111. {
  112. return rtthread_thread_cnts(ops);
  113. }
  114. static int32_t rtthread_get_memarea(struct thread_info_ops *ops, int32_t idx,
  115. uint32_t *addr, uint32_t *memlen)
  116. {
  117. rt_int32_t idx_l = 0;
  118. rt_int32_t current_idx = rtthread_cur_index(ops);
  119. struct rt_object_information *information;
  120. struct rt_object *object;
  121. struct rt_list_node *node;
  122. information = rt_object_get_information(RT_Object_Class_Thread);
  123. mcd_assert(information != RT_NULL);
  124. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  125. {
  126. object = rt_list_entry(node, struct rt_object, list);
  127. rt_thread_t thread = (rt_thread_t)object;
  128. if (is_thread_object(thread))
  129. {
  130. if (idx == idx_l)
  131. {
  132. /* If this is the current thread, use current stack pointer */
  133. if (idx_l == current_idx)
  134. {
  135. *addr = get_cur_core_regset_address()->sp; // 异常中填充
  136. *memlen = (rt_uint32_t)thread->stack_addr + thread->stack_size - (rt_uint32_t)thread->sp;
  137. if (*memlen > thread->stack_size)
  138. {
  139. mcd_println("The stack may overflow!!!");
  140. *memlen = thread->stack_size;
  141. }
  142. }
  143. else
  144. {
  145. *addr = (rt_uint32_t)thread->sp;
  146. *memlen = (rt_uint32_t)thread->stack_addr + thread->stack_size - (rt_uint32_t)thread->sp;
  147. }
  148. return 0;
  149. }
  150. idx_l++;
  151. }
  152. }
  153. return 0;
  154. }
  155. #if PRINT_COREDUMP_INFO_STRING
  156. static int mcd_print_coredump_info_string(struct thread_info_ops *ops)
  157. {
  158. rt_int32_t current_idx = rtthread_cur_index(ops);
  159. rt_int32_t idx_l = 0;
  160. struct rt_object_information *information;
  161. struct rt_object *object;
  162. struct rt_list_node *node;
  163. uint32_t addr;
  164. uint32_t memlen;
  165. information = rt_object_get_information(RT_Object_Class_Thread);
  166. mcd_assert(information != RT_NULL);
  167. mcd_print("\n\n");
  168. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  169. {
  170. object = rt_list_entry(node, struct rt_object, list);
  171. rt_thread_t thread = (rt_thread_t)object;
  172. if (is_thread_object(thread))
  173. {
  174. /* If this is the current thread, use current stack pointer */
  175. if (idx_l == current_idx)
  176. {
  177. addr = (rt_uint32_t)get_cur_core_regset_address()->sp; // 异常中填充
  178. memlen = (rt_uint32_t)thread->stack_addr + thread->stack_size - (rt_uint32_t)thread->sp;
  179. if (memlen > thread->stack_size)
  180. {
  181. mcd_println("The stack may overflow!!!");
  182. memlen = thread->stack_size;
  183. }
  184. print_registers((void *)get_cur_core_regset_address(), get_cur_fp_regset_address(), thread->name);
  185. }
  186. else
  187. {
  188. addr = (rt_uint32_t)thread->sp;
  189. memlen = (rt_uint32_t)thread->stack_addr + thread->stack_size - (rt_uint32_t)thread->sp;
  190. core_regset_type core_regset;
  191. fp_regset_type fp_regset;
  192. collect_registers((uint32_t *)thread->sp, &core_regset, &fp_regset); // rtos栈寄存器格式转换成core_regset类型的
  193. print_registers(&core_regset, &fp_regset, thread->name);
  194. }
  195. print_mem(addr, memlen);
  196. idx_l++;
  197. }
  198. }
  199. mcd_variable_dump();
  200. return 0;
  201. }
  202. #endif
  203. void mcd_rtos_thread_ops(struct thread_info_ops *ops)
  204. {
  205. static rtthread_ti_priv_t priv;
  206. ops->get_threads_count = rtthread_thread_cnts;
  207. ops->get_current_thread_idx = rtthread_cur_index;
  208. ops->get_thread_regset = rtthread_thread_register;
  209. ops->get_memarea_count = rtthread_get_mem_cnts;
  210. ops->get_memarea = rtthread_get_memarea;
  211. #if PRINT_COREDUMP_INFO_STRING
  212. ops->print_info_string = mcd_print_coredump_info_string;
  213. #endif
  214. ops->priv = &priv;
  215. priv.cur_idx = -1;
  216. priv.thr_cnts = -1;
  217. }
  218. MCD_WEAK rt_err_t rtt_hard_fault_exception_hook(void *context)
  219. {
  220. arch_hard_fault_exception_hook(context);
  221. return -RT_ERROR;
  222. }
  223. MCD_WEAK void rtt_assert_hook(const char *ex, const char *func, rt_size_t line)
  224. {
  225. volatile uint8_t _continue = 1;
  226. rt_interrupt_enter();
  227. mcd_print("(%s) has assert failed at %s:%ld.\n", ex, func, line);
  228. mcd_faultdump(MCD_OUTPUT_SERIAL);
  229. rt_interrupt_leave();
  230. while (_continue == 1);
  231. }
  232. static int mcd_coredump_init(void)
  233. {
  234. //mcd_print_memoryinfo();
  235. rt_hw_exception_install(rtt_hard_fault_exception_hook);
  236. #ifdef RT_DEBUG
  237. rt_assert_set_hook(rtt_assert_hook);
  238. #endif
  239. return 0;
  240. }
  241. INIT_DEVICE_EXPORT(mcd_coredump_init);