1 /*******************************************************************************
\r
2 * Tracealyzer v3.0.2 Recorder Library
\r
3 * Percepio AB, www.percepio.com
\r
7 * Core functionality of the Tracealyzer recorder library.
\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
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
34 * Tabs are used for indent in this file (1 tab = 4 spaces)
\r
36 * Copyright Percepio AB, 2014.
\r
38 ******************************************************************************/
\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
50 #include "trcKernelPort.h"
\r
52 #if (USE_TRACEALYZER_RECORDER == 1)
\r
57 #ifndef USE_SEPARATE_USER_EVENT_BUFFER
\r
58 #define USE_SEPARATE_USER_EVENT_BUFFER 0
\r
61 #ifndef TRACE_SR_ALLOC_CRITICAL_SECTION
\r
62 #define TRACE_SR_ALLOC_CRITICAL_SECTION()
\r
65 /* Max number of event codes supported */
\r
66 #define NEventCodes 0x100
\r
68 /* Keeps track of the recorder's critical sections */
\r
69 extern volatile int recorder_busy;
\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
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
78 /* Structure to handle the exclude flags for all event codes */
\r
79 extern uint8_t excludedEventCodes[NEventCodes / 8 + 1];
\r
81 /******************************************************************************
\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
89 * Note that handle zero (0) is not used, it is a code for an invalid handle.
\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
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
103 *****************************************************************************/
\r
106 /* For each object class, the index of the next handle to allocate */
\r
107 uint16_t indexOfNextAvailableHandle[ TRACE_NCLASSES ];
\r
109 /* The lowest index of this class (constant) */
\r
110 uint16_t lowestIndexOfClass[ TRACE_NCLASSES ];
\r
112 /* The highest index of this class (constant) */
\r
113 uint16_t highestIndexOfClass[ TRACE_NCLASSES ];
\r
115 /* The highest use count for this class (for statistics) */
\r
116 uint16_t handleCountWaterMarksOfClass[ TRACE_NCLASSES ];
\r
118 /* The free object handles - a set of stacks within this array */
\r
119 objectHandleType objectHandles[ TRACE_KERNEL_OBJECT_COUNT ];
\r
121 } objectHandleStackType;
\r
123 extern objectHandleStackType objectHandleStacks;
\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
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
144 uint32_t NumberOfObjectClasses;
\r
146 uint32_t ObjectPropertyTableSizeInBytes;
\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
153 objectHandleType NumberOfObjectsPerClass[4*((TRACE_NCLASSES+3)/4)];
\r
156 /* Allocation size rounded up to the closest multiple of 4 */
\r
157 uint8_t NameLengthPerClass[ 4*((TRACE_NCLASSES+3)/4) ];
\r
159 uint8_t TotalPropertyBytesPerClass[ 4*((TRACE_NCLASSES+3)/4) ];
\r
161 /* Allocation size rounded up to the closest multiple of 2 */
\r
162 uint16_t StartIndexOfClass[ 2*((TRACE_NCLASSES+1)/2) ];
\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
168 /* Symbol table data structure */
\r
171 /* = SYMBOL_HISTORY_TABLE_SIZE_IN_BYTES */
\r
172 uint32_t symTableSize;
\r
174 /* Entry 0 is reserved. Any reference to entry 0 implies NULL*/
\r
175 uint32_t nextFreeSymbolIndex;
\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
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
187 /*******************************************************************************
\r
188 * The data structures of the different events, all 4 bytes long
\r
189 ******************************************************************************/
\r
195 uint16_t dts; /* differential timestamp - time since last event */
\r
196 } TSEvent, TREvent;
\r
202 uint16_t dts; /* differential timestamp - time since last event */
\r
209 uint16_t dts; /* differential timestamp - time since last event */
\r
217 uint8_t dts; /* differential timestamp - time since last event */
\r
218 } KernelCallWithParamAndHandle;
\r
223 uint8_t dts; /* differential timestamp - time since last event */
\r
225 } KernelCallWithParam16;
\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
240 } ObjClosePropEvent;
\r
248 } TaskInstanceStatusEvent;
\r
254 uint16_t payload; /* the name of the user event */
\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
265 /* 16 bits extra for storing DTS, if it does not fit in ordinary event. */
\r
289 /*******************************************************************************
\r
290 * The separate user event buffer structure. Can be enabled in trcConfig.h.
\r
291 ******************************************************************************/
\r
293 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
297 traceLabel defaultFormat;
\r
298 } ChannelFormatPair;
\r
304 uint32_t wraparoundCounter;
\r
305 uint32_t numberOfSlots;
\r
306 uint32_t nextSlotToWrite;
\r
307 uint8_t numberOfChannels;
\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
318 /*******************************************************************************
\r
319 * The main data structure, read by Tracealyzer from the RAM dump
\r
320 ******************************************************************************/
\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
337 /* Used to determine Kernel and Endianess */
\r
341 uint8_t minor_version;
\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
348 /* sizeof(RecorderDataType) - just for control */
\r
351 /* Current number of events recorded */
\r
352 uint32_t numEvents;
\r
354 /* The buffer size, in number of event records */
\r
355 uint32_t maxEvents;
\r
357 /* The event buffer index, where to write the next event */
\r
358 uint32_t nextFreeIndex;
\r
360 /* 1 if the buffer is full, 0 otherwise */
\r
361 uint32_t bufferIsFull;
\r
363 /* The frequency of the clock/timer/counter used as time base */
\r
364 uint32_t frequency;
\r
366 /* The absolute timestamp of the last stored event, in the native
\r
367 timebase, modulo frequency! */
\r
368 uint32_t absTimeLastEvent;
\r
370 /* The number of seconds in total - lasts for 136 years */
\r
371 uint32_t absTimeLastEventSecond;
\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
377 /* Not used, remains for compatibility and future use */
\r
378 uint8_t notused[28];
\r
380 /* The amount of heap memory remaining at the last malloc or free event */
\r
381 uint32_t heapMemUsage;
\r
383 /* 0xF0F0F0F0 - for control only */
\r
384 int32_t debugMarker0;
\r
386 /* Set to value of USE_16BIT_OBJECT_HANDLES */
\r
387 uint32_t isUsing16bitHandles;
\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
394 /* 0xF1F1F1F1 - for control only */
\r
395 int32_t debugMarker1;
\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
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
407 uint32_t exampleFloatEncoding;
\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
413 uint32_t internalErrorOccured;
\r
415 /* 0xF2F2F2F2 - for control only */
\r
416 int32_t debugMarker2;
\r
418 /* Error messages from the recorder. */
\r
419 char systemInfo[80];
\r
421 /* 0xF3F3F3F3 - for control only */
\r
422 int32_t debugMarker3;
\r
424 /* The event data, in 4-byte records */
\r
425 uint8_t eventData[ EVENT_BUFFER_SIZE * 4 ];
\r
427 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
428 UserEventBuffer userEventBuffer;
\r
431 /* This should always be 0 */
\r
432 uint32_t endOfSecondaryBlocks;
\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
448 extern RecorderDataType* RecorderDataPtr;
\r
450 /* Internal functions */
\r
452 uint16_t prvTraceGetDTS(uint16_t param_maxDTS);
\r
454 void prvTraceGetChecksum(const char *pname, uint8_t* pcrc, uint8_t* plength);
\r
456 traceLabel prvTraceCreateSymbolTableEntry(const char* name,
\r
459 traceLabel channel);
\r
461 traceLabel prvTraceLookupSymbolTableEntry(const char* name,
\r
464 traceLabel channel);
\r
466 traceLabel prvTraceOpenSymbol(const char* name, traceLabel userEventChannel);
\r
468 void prvTraceUpdateCounters(void);
\r
470 void prvCheckDataToBeOverwrittenForMultiEntryEvents(uint8_t nEntries);
\r
472 objectHandleType xTraceGetObjectHandle(traceObjectClass objectclass);
\r
474 void vTraceFreeObjectHandle(traceObjectClass objectclass,
\r
475 objectHandleType handle);
\r
477 void vTraceSetObjectName(traceObjectClass objectclass,
\r
478 objectHandleType handle,
\r
481 void* xTraceNextFreeEventBufferSlot(void);
\r
483 #if (USE_16BIT_OBJECT_HANDLES == 1)
\r
484 unsigned char prvTraceGet8BitHandle(objectHandleType handle);
\r
486 #define prvTraceGet8BitHandle(x) ((unsigned char)x)
\r
490 uint16_t uiIndexOfObject(objectHandleType objecthandle,
\r
491 uint8_t objectclass);
\r
493 /*******************************************************************************
\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
501 /*******************************************************************************
\r
502 * prvTraceInitTraceData
\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
510 /* Internal macros */
\r
512 #define TRACE_PROPERTY_NAME_GET(objectclass, objecthandle) \
\r
513 (const char*)(& RecorderDataPtr->ObjectPropertyTable.objbytes \
\r
514 [uiIndexOfObject(objecthandle, objectclass)])
\r
516 #define TRACE_PROPERTY_OBJECT_STATE(objectclass, handle) \
\r
517 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \
\r
518 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass]]
\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
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
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
532 /* DEBUG ASSERTS */
\r
533 #if defined USE_TRACE_ASSERT && USE_TRACE_ASSERT != 0
\r
534 #define TRACE_ASSERT(eval, msg, defRetVal) \
\r
537 vTraceError("TRACE_ASSERT: " msg); \
\r
538 return defRetVal; \
\r
541 #define TRACE_ASSERT(eval, msg, defRetVal)
\r