1 /*
2 * Percepio Trace Recorder SDK for Tracealyzer v4.6.6
3 * Copyright 2021 Percepio AB
4 * www.percepio.com
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8 
9 /**
10  * @file
11  *
12  * @brief Public trace stack monitor APIs.
13  */
14 
15 #ifndef TRC_STACK_MONITOR_H
16 #define TRC_STACK_MONITOR_H
17 
18 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
19 
20 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
21 
22 #include <stdint.h>
23 #include <trcRecorder.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /**
30  * @defgroup trace_stack_monitor_apis Trace Stack Monitor APIs
31  * @ingroup trace_recorder_apis
32  * @{
33  */
34 
35 #if (((TRC_CFG_ENABLE_STACK_MONITOR) == 1) && ((TRC_CFG_SCHEDULING_ONLY) == 0))
36 
37 #define TRACE_STACK_MONITOR_BUFFER_SIZE ((sizeof(void*) + sizeof(TraceUnsignedBaseType_t)) * (TRC_CFG_STACK_MONITOR_MAX_TASKS) + sizeof(uint32_t))
38 
39 /**
40  * @internal Trace Stack Monitor Buffer Structure
41  */
42 typedef struct TraceStackMonitorBuffer
43 {
44 	uint32_t buffer[(TRACE_STACK_MONITOR_BUFFER_SIZE) / sizeof(uint32_t)];
45 } TraceStackMonitorBuffer_t;
46 
47 /**
48  * @internal Initialize trace stack monitor system.
49  *
50  * @param[in] pxBuffer Pointer to memory that will be used by the trace
51  * stack monitor system.
52  *
53  * @retval TRC_FAIL Failure
54  * @retval TRC_SUCCESS Success
55  */
56 traceResult xTraceStackMonitorInitialize(TraceStackMonitorBuffer_t* pxBuffer);
57 
58 /**
59  * @brief Adds task/thread to trace stack monitor.
60  *
61  * @param[in] pvTask Task/Thread.
62  *
63  * @retval TRC_FAIL Failure
64  * @retval TRC_SUCCESS Success
65  */
66 traceResult xTraceStackMonitorAdd(void* pvTask);
67 
68 /**
69  * @brief Removes task/thread from trace stack monitor.
70  *
71  * @param[in] pvTask Task/Thread.
72  *
73  * @retval TRC_FAIL Failure
74  * @retval TRC_SUCCESS Success
75  */
76 traceResult xTraceStackMonitorRemove(void* pvTask);
77 
78 /**
79  * @brief Gets trace stack monitor tread/task at index.
80  *
81  * @param[in] uiIndex Index.
82  * @param[in] ppvTask Task/Thread.
83  * @param[out] puxLowWaterMark Low water mark.
84  *
85  * @retval TRC_FAIL Failure
86  * @retval TRC_SUCCESS Success
87  */
88 traceResult xTraceStackMonitorGetAtIndex(uint32_t uiIndex, void** ppvTask, TraceUnsignedBaseType_t* puxLowWaterMark);
89 
90 /**
91  * @brief Performs trace stack monitor reporting.
92  *
93  * This routine performs a trace stack monitor check and report
94  * for TRC_CFG_STACK_MONITOR_MAX_REPORTS number of registered
95  * tasks/threads.
96  *
97  * @retval TRC_FAIL Failure
98  * @retval TRC_SUCCESS Success
99  */
100 traceResult xTraceStackMonitorReport(void);
101 
102 #else /* (((TRC_CFG_ENABLE_STACK_MONITOR) == 1) && ((TRC_CFG_SCHEDULING_ONLY) == 0)) */
103 
104 typedef struct TraceStackMonitorBuffer
105 {
106 	uint32_t buffer[1];
107 } TraceStackMonitorBuffer_t;
108 
109 #define xTraceStackMonitorInitialize(pxBuffer) ((void)pxBuffer, TRC_SUCCESS)
110 
111 #define xTraceStackMonitorDiagnosticsGet(xType, puiValue) ((void)xType, puiValue != 0 ? *puiValue = 0 : 0, puiValue != 0 ? TRC_SUCCESS : TRC_FAIL)
112 
113 #define xTraceStackMonitorDiagnosticsSet(xType, uiValue) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_3((void)xType, (void)uiValue, TRC_SUCCESS)
114 
115 #define xTraceStackMonitorAdd(pvTask) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2((void)pvTask, TRC_SUCCESS)
116 
117 #define xTraceStackMonitorRemove(pvTask) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2((void)pvTask, TRC_SUCCESS)
118 
119 #define xTraceStackMonitorGetAtIndex(uiIndex, ppvTask, puxLowWaterMark) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_4((void)uiIndex, (void)ppvTask, (void)puxLowWaterMark, TRC_SUCCESS)
120 
121 #define xTraceStackMonitorReport() TRC_COMMA_EXPR_TO_STATEMENT_EXPR_1(TRC_SUCCESS)
122 
123 #endif /* (((TRC_CFG_ENABLE_STACK_MONITOR) == 1) && ((TRC_CFG_SCHEDULING_ONLY) == 0)) */
124 
125 /** @} */
126 
127 #ifdef __cplusplus
128 }
129 #endif
130 
131 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
132 
133 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
134 
135 #endif /* TRC_STACK_MONITOR_H */
136