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