1515#include "memfault/ports/zephyr/version.h"
1616
1717#if MEMFAULT_ZEPHYR_VERSION_GT (2 , 1 )
18- #include <arch/arm/aarch32/cortex_m/cmsis.h>
18+ #include <arch/arm/aarch32/cortex_m/cmsis.h>
1919#else
20- #include <arch/arm/cortex_m/cmsis.h>
20+ #include <arch/arm/cortex_m/cmsis.h>
2121#endif
2222
2323#include "memfault/core/compiler.h"
2424#include "memfault/core/math.h"
2525#include "memfault/ports/zephyr/coredump.h"
2626
27- static sMfltCoredumpRegion s_coredump_regions [
28- MEMFAULT_COREDUMP_MAX_TASK_REGIONS
29- + 2 /* active stack(s) */
30- + 1 /* _kernel variable */
31- + 1 /* s_task_watermarks */
32- + 1 /* s_task_tcbs */
33- #if CONFIG_MEMFAULT_COREDUMP_COLLECT_DATA_REGIONS
34- + 1
35- #endif
36- #if CONFIG_MEMFAULT_COREDUMP_COLLECT_BSS_REGIONS
37- + 1
38- #endif
39- ];
40-
41- MEMFAULT_WEAK
42- const sMfltCoredumpRegion * memfault_platform_coredump_get_regions (
43- const sCoredumpCrashInfo * crash_info , size_t * num_regions ) {
27+ size_t memfault_zephyr_coredump_get_regions (const sCoredumpCrashInfo * crash_info ,
28+ sMfltCoredumpRegion * regions , size_t num_regions ) {
29+ // Check that regions is valid and has enough space to store all required regions
30+ if (regions == NULL || num_regions < MEMFAULT_ZEPHYR_COREDUMP_REGIONS ) {
31+ return 0 ;
32+ }
4433
4534 const bool msp_was_active = (crash_info -> exception_reg_state -> exc_return & (1 << 2 )) == 0 ;
46- int region_idx = 0 ;
35+ size_t region_idx = 0 ;
4736
4837 size_t stack_size_to_collect = memfault_platform_sanitize_address_range (
49- crash_info -> stack_address , CONFIG_MEMFAULT_COREDUMP_STACK_SIZE_TO_COLLECT );
38+ crash_info -> stack_address , CONFIG_MEMFAULT_COREDUMP_STACK_SIZE_TO_COLLECT );
5039
51- s_coredump_regions [region_idx ] = MEMFAULT_COREDUMP_MEMORY_REGION_INIT (
52- crash_info -> stack_address , stack_size_to_collect );
40+ regions [region_idx ] =
41+ MEMFAULT_COREDUMP_MEMORY_REGION_INIT ( crash_info -> stack_address , stack_size_to_collect );
5342 region_idx ++ ;
5443
5544 if (msp_was_active ) {
@@ -60,19 +49,15 @@ const sMfltCoredumpRegion *memfault_platform_coredump_get_regions(
6049 // exception frame that will have been stacked on it as well
6150 const uint32_t extra_stack_bytes = 128 ;
6251 stack_size_to_collect = memfault_platform_sanitize_address_range (
63- psp , CONFIG_MEMFAULT_COREDUMP_STACK_SIZE_TO_COLLECT + extra_stack_bytes );
64- s_coredump_regions [region_idx ] = MEMFAULT_COREDUMP_MEMORY_REGION_INIT (
65- psp , stack_size_to_collect );
52+ psp , CONFIG_MEMFAULT_COREDUMP_STACK_SIZE_TO_COLLECT + extra_stack_bytes );
53+ regions [region_idx ] = MEMFAULT_COREDUMP_MEMORY_REGION_INIT (psp , stack_size_to_collect );
6654 region_idx ++ ;
6755 }
6856
69- s_coredump_regions [region_idx ] = MEMFAULT_COREDUMP_MEMORY_REGION_INIT (
70- & _kernel , sizeof (_kernel ));
57+ regions [region_idx ] = MEMFAULT_COREDUMP_MEMORY_REGION_INIT (& _kernel , sizeof (_kernel ));
7158 region_idx ++ ;
7259
73- region_idx += memfault_zephyr_get_task_regions (
74- & s_coredump_regions [region_idx ],
75- MEMFAULT_ARRAY_SIZE (s_coredump_regions ) - region_idx );
60+ region_idx += memfault_zephyr_get_task_regions (& regions [region_idx ], num_regions - region_idx );
7661
7762 //
7863 // Now that we have captured all the task state, we will
@@ -81,15 +66,22 @@ const sMfltCoredumpRegion *memfault_platform_coredump_get_regions(
8166 //
8267
8368#if CONFIG_MEMFAULT_COREDUMP_COLLECT_DATA_REGIONS
84- region_idx += memfault_zephyr_get_data_regions (
85- & s_coredump_regions [region_idx ], MEMFAULT_ARRAY_SIZE (s_coredump_regions ) - region_idx );
69+ region_idx += memfault_zephyr_get_data_regions (& regions [region_idx ], num_regions - region_idx );
8670#endif
8771
8872#if CONFIG_MEMFAULT_COREDUMP_COLLECT_BSS_REGIONS
89- region_idx += memfault_zephyr_get_bss_regions (
90- & s_coredump_regions [region_idx ], MEMFAULT_ARRAY_SIZE (s_coredump_regions ) - region_idx );
73+ region_idx += memfault_zephyr_get_bss_regions (& regions [region_idx ], num_regions - region_idx );
9174#endif
9275
93- * num_regions = region_idx ;
76+ return region_idx ;
77+ }
78+
79+ MEMFAULT_WEAK
80+ const sMfltCoredumpRegion * memfault_platform_coredump_get_regions (
81+ const sCoredumpCrashInfo * crash_info , size_t * num_regions ) {
82+ static sMfltCoredumpRegion s_coredump_regions [MEMFAULT_ZEPHYR_COREDUMP_REGIONS ];
83+
84+ * num_regions = memfault_zephyr_coredump_get_regions (crash_info , s_coredump_regions ,
85+ MEMFAULT_ARRAY_SIZE (s_coredump_regions ));
9486 return & s_coredump_regions [0 ];
9587}
0 commit comments