]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-Trace/Include/trcBase.h
Update FreeRTOS+Trace recorder library to v3.0.2
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-Trace / Include / trcBase.h
1 /*******************************************************************************\r
2  * Tracealyzer v3.0.2 Recorder Library\r
3  * Percepio AB, www.percepio.com\r
4  *\r
5  * trcBase.h\r
6  *\r
7  * Core functionality of the Tracealyzer recorder library.\r
8  *\r
9  * Terms of Use\r
10  * This software is copyright Percepio AB. The recorder library is free for\r
11  * use together with Percepio products. You may distribute the recorder library\r
12  * in its original form, including modifications in trcHardwarePort.c/.h\r
13  * given that these modification are clearly marked as your own modifications\r
14  * and documented in the initial comment section of these source files.\r
15  * This software is the intellectual property of Percepio AB and may not be\r
16  * sold or in other ways commercially redistributed without explicit written\r
17  * permission by Percepio AB.\r
18  *\r
19  * Disclaimer\r
20  * The trace tool and recorder library is being delivered to you AS IS and\r
21  * Percepio AB makes no warranty as to its use or performance. Percepio AB does\r
22  * not and cannot warrant the performance or results you may obtain by using the\r
23  * software or documentation. Percepio AB make no warranties, express or\r
24  * implied, as to noninfringement of third party rights, merchantability, or\r
25  * fitness for any particular purpose. In no event will Percepio AB, its\r
26  * technology partners, or distributors be liable to you for any consequential,\r
27  * incidental or special damages, including any lost profits or lost savings,\r
28  * even if a representative of Percepio AB has been advised of the possibility\r
29  * of such damages, or for any claim by any third party. Some jurisdictions do\r
30  * not allow the exclusion or limitation of incidental, consequential or special\r
31  * damages, or the exclusion of implied warranties or limitations on how long an\r
32  * implied warranty may last, so the above limitations may not apply to you.\r
33  *\r
34  * Tabs are used for indent in this file (1 tab = 4 spaces)\r
35  *\r
36  * Copyright Percepio AB, 2014.\r
37  * www.percepio.com\r
38  ******************************************************************************/\r
39 \r
40 #ifndef TRCBASE_H\r
41 #define TRCBASE_H\r
42 \r
43 #define TRACE_MINOR_VERSION 4\r
44 #define TRACE_STORE_MODE_STOP_WHEN_FULL 1\r
45 #define TRACE_STORE_MODE_RING_BUFFER 2\r
46 #define TRACE_DATA_ALLOCATION_STATIC 1\r
47 #define TRACE_DATA_ALLOCATION_DYNAMIC 2\r
48 #define TRACE_DATA_ALLOCATION_CUSTOM 3\r
49 \r
50 #include "trcKernelPort.h"\r
51 \r
52 #if (USE_TRACEALYZER_RECORDER == 1)\r
53 \r
54 #include <stdio.h>\r
55 #include <string.h>\r
56 \r
57 #ifndef USE_SEPARATE_USER_EVENT_BUFFER\r
58 #define USE_SEPARATE_USER_EVENT_BUFFER 0\r
59 #endif\r
60 \r
61 #ifndef TRACE_SR_ALLOC_CRITICAL_SECTION\r
62 #define TRACE_SR_ALLOC_CRITICAL_SECTION()\r
63 #endif\r
64 \r
65 /* Max number of event codes supported */\r
66 #define NEventCodes 0x100\r
67 \r
68 /* Keeps track of the recorder's critical sections */\r
69 extern volatile int recorder_busy;\r
70 \r
71 /* Our local critical sections for the recorder */\r
72 #define trcCRITICAL_SECTION_BEGIN() {TRACE_ENTER_CRITICAL_SECTION(); recorder_busy++;}\r
73 #define trcCRITICAL_SECTION_END() {recorder_busy--; TRACE_EXIT_CRITICAL_SECTION();}\r
74 \r
75 /* Structure to handle the exclude flags for all objects and tasks. We add some extra objects since index 0 is not used for each object class. */\r
76 extern uint8_t excludedObjects[(TRACE_KERNEL_OBJECT_COUNT + TRACE_NCLASSES) / 8 + 1];\r
77 \r
78 /* Structure to handle the exclude flags for all event codes */\r
79 extern uint8_t excludedEventCodes[NEventCodes / 8 + 1];\r
80 \r
81 /******************************************************************************\r
82  * ObjectHandleStack\r
83  * This data-structure is used to provide a mechanism for 1-byte trace object\r
84  * handles. This way, only 1 byte is necessary instead of 4 bytes (a pointer)\r
85  * when storing a reference to an object. This allows for up to 255 objects of\r
86  * each object class active at any given moment. There can be more "historic"\r
87  * objects, that have been deleted - that number is only limited by the size of\r
88  * the symbol table.\r
89  * Note that handle zero (0) is not used, it is a code for an invalid handle.\r
90  *\r
91  * This data structure keeps track of the FREE handles, not the handles in use.\r
92  * This data structure contains one stack per object class. When a handle is\r
93  * allocated to an object, the next free handle is popped from the stack. When\r
94  * a handle is released (on object delete), it is pushed back on the stack.\r
95  * Note that there is no initialization code that pushed the free handles\r
96  * initially, that is not necessary due to the following optimization:\r
97  *\r
98  * The stack of handles (objectHandles) is initially all zeros. Since zero\r
99  * is not a valid handle, that is a signal of additional handles needed.\r
100  * If a zero is received when popping a new handle, it is replaced by the\r
101  * index of the popped handle instead.\r
102  *\r
103  *****************************************************************************/\r
104 typedef struct\r
105 {\r
106         /* For each object class, the index of the next handle to allocate */\r
107         uint16_t indexOfNextAvailableHandle[ TRACE_NCLASSES ];\r
108 \r
109         /* The lowest index of this class (constant) */\r
110         uint16_t lowestIndexOfClass[ TRACE_NCLASSES ];\r
111 \r
112         /* The highest index of this class (constant) */\r
113         uint16_t highestIndexOfClass[ TRACE_NCLASSES ];\r
114 \r
115         /* The highest use count for this class (for statistics) */\r
116         uint16_t handleCountWaterMarksOfClass[ TRACE_NCLASSES ];\r
117 \r
118         /* The free object handles - a set of stacks within this array */\r
119         objectHandleType objectHandles[ TRACE_KERNEL_OBJECT_COUNT ];\r
120 \r
121 } objectHandleStackType;\r
122 \r
123 extern objectHandleStackType objectHandleStacks;\r
124 \r
125 /******************************************************************************\r
126  * Object Property Table\r
127  * The Object Table contains name and other properties of the objects (tasks,\r
128  * queues, mutexes, etc). The below data structures defines the properties of\r
129  * each object class and are used to cast the byte buffer into a cleaner format.\r
130  *\r
131  * The values in the object table are continuously overwritten and always\r
132  * represent the current state. If a property is changed during runtime, the OLD\r
133  * value should be stored in the trace buffer, not the new value (since the new\r
134  * value is found in the Object Property Table).\r
135  * For close events this mechanism is the old names are stored in the symbol\r
136  * table), for "priority set" (the old priority is stored in the event data)\r
137  * and for "isActive", where the value decides if the task switch event type\r
138  * should be "new" or "resume".\r
139  ******************************************************************************/\r
140 \r
141 typedef struct\r
142 {\r
143         /* = NCLASSES */\r
144         uint32_t NumberOfObjectClasses;\r
145 \r
146         uint32_t ObjectPropertyTableSizeInBytes;\r
147 \r
148         /* This is used to calculate the index in the dynamic object table\r
149         (handle - 1 - nofStaticObjects = index)*/\r
150 #if (USE_16BIT_OBJECT_HANDLES == 1)\r
151         objectHandleType NumberOfObjectsPerClass[2*((TRACE_NCLASSES+1)/2)];\r
152 #else\r
153         objectHandleType NumberOfObjectsPerClass[4*((TRACE_NCLASSES+3)/4)];\r
154 #endif\r
155 \r
156         /* Allocation size rounded up to the closest multiple of 4 */\r
157         uint8_t NameLengthPerClass[ 4*((TRACE_NCLASSES+3)/4) ];\r
158 \r
159         uint8_t TotalPropertyBytesPerClass[ 4*((TRACE_NCLASSES+3)/4) ];\r
160 \r
161         /* Allocation size rounded up to the closest multiple of 2 */\r
162         uint16_t StartIndexOfClass[ 2*((TRACE_NCLASSES+1)/2) ];\r
163 \r
164         /* The actual handles issued, should be Initiated to all zeros */\r
165         uint8_t objbytes[ 4*((TRACE_OBJECT_TABLE_SIZE+3)/4) ];\r
166 } ObjectPropertyTableType;\r
167 \r
168 /* Symbol table data structure */\r
169 typedef struct\r
170 {\r
171         /* = SYMBOL_HISTORY_TABLE_SIZE_IN_BYTES */\r
172         uint32_t symTableSize;\r
173 \r
174         /* Entry 0 is reserved. Any reference to entry 0 implies NULL*/\r
175         uint32_t nextFreeSymbolIndex;\r
176 \r
177         /* Size rounded up to closest multiple of 4, to avoid alignment issues*/\r
178         uint8_t symbytes[4*((SYMBOL_TABLE_SIZE+3)/4)];\r
179 \r
180         /* Used for lookups - Up to 64 linked lists within the symbol table\r
181         connecting all entries with the same 6 bit checksum.\r
182         This field holds the current list heads. Should be initiated to zeros */\r
183         uint16_t latestEntryOfChecksum[64];\r
184 } symbolTableType;\r
185 \r
186 \r
187 /*******************************************************************************\r
188  * The data structures of the different events, all 4 bytes long\r
189  ******************************************************************************/\r
190 \r
191 typedef struct\r
192 {\r
193         uint8_t type;\r
194         uint8_t objHandle;\r
195         uint16_t dts;   /* differential timestamp - time since last event */\r
196 } TSEvent, TREvent;\r
197 \r
198 typedef struct\r
199 {\r
200         uint8_t type;\r
201         uint8_t dummy;\r
202         uint16_t dts;   /* differential timestamp - time since last event */\r
203 } LPEvent;\r
204 \r
205 typedef struct\r
206 {\r
207         uint8_t type;\r
208         uint8_t objHandle;\r
209         uint16_t dts;   /* differential timestamp - time since last event */\r
210 } KernelCall;\r
211 \r
212 typedef struct\r
213 {\r
214         uint8_t type;\r
215         uint8_t objHandle;\r
216         uint8_t param;\r
217         uint8_t dts;    /* differential timestamp - time since last event */\r
218 } KernelCallWithParamAndHandle;\r
219 \r
220 typedef struct\r
221 {\r
222         uint8_t type;\r
223         uint8_t dts;    /* differential timestamp - time since last event */\r
224         uint16_t param;\r
225 } KernelCallWithParam16;\r
226 \r
227 typedef struct\r
228 {\r
229         uint8_t type;\r
230         uint8_t objHandle;      /* the handle of the closed object */\r
231         uint16_t symbolIndex;            /* the name of the closed object */\r
232 } ObjCloseNameEvent;\r
233 \r
234 typedef struct\r
235 {\r
236         uint8_t type;\r
237         uint8_t arg1;\r
238         uint8_t arg2;\r
239         uint8_t arg3;\r
240 } ObjClosePropEvent;\r
241 \r
242 typedef struct\r
243 {\r
244         uint8_t type;\r
245         uint8_t unused1;\r
246         uint8_t unused2;\r
247         uint8_t dts;\r
248 } TaskInstanceStatusEvent;\r
249 \r
250 typedef struct\r
251 {\r
252         uint8_t type;\r
253         uint8_t dts;\r
254         uint16_t payload;                /* the name of the user event */\r
255 } UserEvent;\r
256 \r
257 typedef struct\r
258 {\r
259         uint8_t type;\r
260 \r
261         /* 8 bits extra for storing DTS, if it does not fit in ordinary event\r
262         (this one is always MSB if used) */\r
263         uint8_t xts_8;\r
264 \r
265         /* 16 bits extra for storing DTS, if it does not fit in ordinary event. */\r
266         uint16_t xts_16;\r
267 } XTSEvent;\r
268 \r
269 typedef struct\r
270 {\r
271         uint8_t type;\r
272 \r
273         uint8_t xps_8;\r
274         uint16_t xps_16;\r
275 } XPSEvent;\r
276 \r
277 typedef struct{\r
278         uint8_t type;\r
279         uint8_t dts;\r
280         uint16_t size;\r
281 } MemEventSize;\r
282 \r
283 typedef struct{\r
284         uint8_t type;\r
285         uint8_t addr_high;\r
286         uint16_t addr_low;\r
287 } MemEventAddr;\r
288 \r
289 /*******************************************************************************\r
290  * The separate user event buffer structure. Can be enabled in trcConfig.h.\r
291  ******************************************************************************/\r
292 \r
293 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
294 typedef struct\r
295 {\r
296         traceLabel name;\r
297         traceLabel defaultFormat;\r
298 } ChannelFormatPair;\r
299 \r
300 typedef struct\r
301 {\r
302         uint16_t bufferID;\r
303         uint16_t version;\r
304         uint32_t wraparoundCounter;\r
305         uint32_t numberOfSlots;\r
306         uint32_t nextSlotToWrite;\r
307         uint8_t numberOfChannels;\r
308         uint8_t padding1;\r
309         uint8_t padding2;\r
310         uint8_t padding3;\r
311         ChannelFormatPair channels[CHANNEL_FORMAT_PAIRS+1];\r
312         uint8_t channelBuffer[(USER_EVENT_BUFFER_SIZE + 3) & 0xFFFFFFFC]; /* 1 byte per slot, with padding for 4 byte alignment */\r
313         uint8_t dataBuffer[USER_EVENT_BUFFER_SIZE * 4]; /* 4 bytes per slot */\r
314 \r
315 } UserEventBuffer;\r
316 #endif\r
317 \r
318 /*******************************************************************************\r
319  * The main data structure, read by Tracealyzer from the RAM dump\r
320  ******************************************************************************/\r
321 \r
322 typedef struct\r
323 {\r
324         uint8_t startmarker0;\r
325         uint8_t startmarker1;\r
326         uint8_t startmarker2;\r
327         uint8_t startmarker3;\r
328         uint8_t startmarker4;\r
329         uint8_t startmarker5;\r
330         uint8_t startmarker6;\r
331         uint8_t startmarker7;\r
332         uint8_t startmarker8;\r
333         uint8_t startmarker9;\r
334         uint8_t startmarker10;\r
335         uint8_t startmarker11;\r
336 \r
337         /* Used to determine Kernel and Endianess */\r
338         uint16_t version;\r
339 \r
340         /* Currently 3 */\r
341         uint8_t minor_version;\r
342 \r
343         /* This should be 0 if lower IRQ priority values implies higher priority\r
344         levels, such as on ARM Cortex M. If the opposite scheme is used, i.e.,\r
345         if higher IRQ priority values means higher priority, this should be 1. */\r
346         uint8_t irq_priority_order;\r
347 \r
348         /* sizeof(RecorderDataType) - just for control */\r
349         uint32_t filesize;\r
350 \r
351         /* Current number of events recorded */\r
352         uint32_t numEvents;\r
353 \r
354         /* The buffer size, in number of event records */\r
355         uint32_t maxEvents;\r
356 \r
357         /* The event buffer index, where to write the next event */\r
358         uint32_t nextFreeIndex;\r
359 \r
360         /* 1 if the buffer is full, 0 otherwise */\r
361         uint32_t bufferIsFull;\r
362 \r
363         /* The frequency of the clock/timer/counter used as time base */\r
364         uint32_t frequency;\r
365 \r
366         /* The absolute timestamp of the last stored event, in the native\r
367         timebase, modulo frequency! */\r
368         uint32_t absTimeLastEvent;\r
369 \r
370         /* The number of seconds in total - lasts for 136 years */\r
371         uint32_t absTimeLastEventSecond;\r
372 \r
373         /* 1 if the recorder has been started, 0 if not yet started or stopped.\r
374         This is a 32 bit variable due to alignment issues. */\r
375         uint32_t recorderActive;\r
376 \r
377         /* Not used, remains for compatibility and future use */\r
378         uint8_t notused[28];\r
379 \r
380         /* The amount of heap memory remaining at the last malloc or free event */\r
381         uint32_t heapMemUsage;\r
382 \r
383         /* 0xF0F0F0F0 - for control only */\r
384         int32_t debugMarker0;\r
385 \r
386         /* Set to value of USE_16BIT_OBJECT_HANDLES */\r
387         uint32_t isUsing16bitHandles;\r
388 \r
389         /* The Object Property Table holds information about currently active\r
390         tasks, queues, and other recorded objects. This is updated on each\r
391         create call and includes object name and other properties. */\r
392         ObjectPropertyTableType ObjectPropertyTable;\r
393 \r
394         /* 0xF1F1F1F1 - for control only */\r
395         int32_t debugMarker1;\r
396 \r
397         /* The Symbol Table stores strings for User Events and is also used to\r
398         store names of deleted objects, which still may be in the trace but no\r
399         longer are available. */\r
400         symbolTableType SymbolTable;\r
401 \r
402         /* For inclusion of float support, and for endian detection of floats.\r
403         The value should be (float)1 or (uint32_t)0 */\r
404 #if (INCLUDE_FLOAT_SUPPORT == 1)\r
405         float exampleFloatEncoding;\r
406 #else\r
407         uint32_t exampleFloatEncoding;\r
408 #endif\r
409         /* This is non-zero if an internal error occurred in the recorder, e.g., if\r
410         one of the Nxxx constants was too small. The systemInfo string will then\r
411         contain an error message that is displayed when attempting to view the\r
412         trace file. */\r
413         uint32_t internalErrorOccured;\r
414 \r
415         /* 0xF2F2F2F2 - for control only */\r
416         int32_t debugMarker2;\r
417 \r
418         /* Error messages from the recorder. */\r
419         char systemInfo[80];\r
420 \r
421         /* 0xF3F3F3F3 - for control only */\r
422         int32_t debugMarker3;\r
423 \r
424         /* The event data, in 4-byte records */\r
425         uint8_t eventData[ EVENT_BUFFER_SIZE * 4 ];\r
426 \r
427 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)\r
428         UserEventBuffer userEventBuffer;\r
429 #endif\r
430 \r
431         /* This should always be 0 */\r
432         uint32_t endOfSecondaryBlocks;\r
433 \r
434         uint8_t endmarker0;\r
435         uint8_t endmarker1;\r
436         uint8_t endmarker2;\r
437         uint8_t endmarker3;\r
438         uint8_t endmarker4;\r
439         uint8_t endmarker5;\r
440         uint8_t endmarker6;\r
441         uint8_t endmarker7;\r
442         uint8_t endmarker8;\r
443         uint8_t endmarker9;\r
444         uint8_t endmarker10;\r
445         uint8_t endmarker11;\r
446 } RecorderDataType;\r
447 \r
448 extern RecorderDataType* RecorderDataPtr;\r
449 \r
450 /* Internal functions */\r
451 \r
452 uint16_t prvTraceGetDTS(uint16_t param_maxDTS);\r
453 \r
454 void prvTraceGetChecksum(const char *pname, uint8_t* pcrc, uint8_t* plength);\r
455 \r
456 traceLabel prvTraceCreateSymbolTableEntry(const char* name,\r
457                                                                                  uint8_t crc6,\r
458                                                                                  uint8_t len,\r
459                                                                                  traceLabel channel);\r
460 \r
461 traceLabel prvTraceLookupSymbolTableEntry(const char* name,\r
462                                                                                  uint8_t crc6,\r
463                                                                                  uint8_t len,\r
464                                                                                  traceLabel channel);\r
465 \r
466 traceLabel prvTraceOpenSymbol(const char* name, traceLabel userEventChannel);\r
467 \r
468 void prvTraceUpdateCounters(void);\r
469 \r
470 void prvCheckDataToBeOverwrittenForMultiEntryEvents(uint8_t nEntries);\r
471 \r
472 objectHandleType xTraceGetObjectHandle(traceObjectClass objectclass);\r
473 \r
474 void vTraceFreeObjectHandle(traceObjectClass objectclass,\r
475                                                         objectHandleType handle);\r
476 \r
477 void vTraceSetObjectName(traceObjectClass objectclass,\r
478                                                         objectHandleType handle,\r
479                                                         const char* name);\r
480 \r
481 void* xTraceNextFreeEventBufferSlot(void);\r
482 \r
483 #if (USE_16BIT_OBJECT_HANDLES == 1)\r
484 unsigned char prvTraceGet8BitHandle(objectHandleType handle);\r
485 #else\r
486 #define prvTraceGet8BitHandle(x) ((unsigned char)x)\r
487 #endif\r
488 \r
489 \r
490 uint16_t uiIndexOfObject(objectHandleType objecthandle,\r
491                                                  uint8_t objectclass);\r
492 \r
493 /*******************************************************************************\r
494  * vTraceError\r
495  *\r
496  * Called by various parts in the recorder. Stops the recorder and stores a\r
497  * pointer to an error message, which is printed by the monitor task.\r
498  ******************************************************************************/\r
499 void vTraceError(const char* msg);\r
500 \r
501 /*******************************************************************************\r
502  * prvTraceInitTraceData\r
503  *\r
504  * Allocates and initializes the recorder data structure, based on the constants\r
505  * in trcConfig.h. This allows for allocating the data on the heap, instead of\r
506  * using a static declaration.\r
507  ******************************************************************************/\r
508 void prvTraceInitTraceData(void);\r
509 \r
510 /* Internal macros */\r
511 \r
512 #define TRACE_PROPERTY_NAME_GET(objectclass, objecthandle) \\r
513 (const char*)(& RecorderDataPtr->ObjectPropertyTable.objbytes \\r
514 [uiIndexOfObject(objecthandle, objectclass)])\r
515 \r
516 #define TRACE_PROPERTY_OBJECT_STATE(objectclass, handle) \\r
517 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \\r
518 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass]]\r
519 \r
520 #define TRACE_PROPERTY_ACTOR_PRIORITY(objectclass, handle) \\r
521 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \\r
522 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass] + 1]\r
523 \r
524 #define TRACE_SET_FLAG_ISEXCLUDED(flags, bitIndex) flags[(bitIndex) >> 3] |= (1 << ((bitIndex) & 7))\r
525 #define TRACE_CLEAR_FLAG_ISEXCLUDED(flags, bitIndex) flags[(bitIndex) >> 3] &= ~(1 << ((bitIndex) & 7))\r
526 #define TRACE_GET_FLAG_ISEXCLUDED(flags, bitIndex) (flags[(bitIndex) >> 3] & (1 << ((bitIndex) & 7)))\r
527 \r
528 #define TRACE_SET_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) TRACE_SET_FLAG_ISEXCLUDED(excludedEventCodes, eventCode)\r
529 #define TRACE_CLEAR_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) TRACE_CLEAR_FLAG_ISEXCLUDED(excludedEventCodes, eventCode)\r
530 #define TRACE_GET_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) TRACE_GET_FLAG_ISEXCLUDED(excludedEventCodes, eventCode)\r
531 \r
532 /* DEBUG ASSERTS */\r
533 #if defined USE_TRACE_ASSERT && USE_TRACE_ASSERT != 0\r
534 #define TRACE_ASSERT(eval, msg, defRetVal) \\r
535 if (!(eval)) \\r
536 { \\r
537         vTraceError("TRACE_ASSERT: " msg); \\r
538         return defRetVal; \\r
539 }\r
540 #else\r
541 #define TRACE_ASSERT(eval, msg, defRetVal)\r
542 #endif\r
543 \r
544 #endif\r
545 \r
546 #endif\r
547 \r
548 \r
549 \r
550 \r