Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

rtthread_port.c 9.1KB

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