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