1 /*******************************************************************************
\r
2 * Tracealyzer v2.5.0 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 * Copyright Percepio AB, 2013.
\r
36 ******************************************************************************/
\r
41 #define TRACE_MINOR_VERSION 2
\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
48 #include "trcKernelPort.h"
\r
50 #if (USE_TRACEALYZER_RECORDER == 1)
\r
55 #ifndef USE_SEPARATE_USER_EVENT_BUFFER
\r
56 #define USE_SEPARATE_USER_EVENT_BUFFER 0
\r
59 /* Max number of event codes supported */
\r
60 #define NEventCodes 0x100
\r
62 extern volatile int recorder_busy; // This is used to keep track of the recorder's critical sections, to determine if it is busy
\r
63 // Our local critical sections for the recorder - updates an internal busy flag
\r
64 #define trcCRITICAL_SECTION_BEGIN() {TRACE_ENTER_CRITICAL_SECTION(); recorder_busy++;}
\r
65 #define trcCRITICAL_SECTION_END() {recorder_busy--; TRACE_EXIT_CRITICAL_SECTION();}
\r
67 /* 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
68 extern uint8_t excludedObjects[(TRACE_KERNEL_OBJECT_COUNT + TRACE_NCLASSES) / 8 + 1];
\r
70 /* Structure to handle the exclude flags for all event codes */
\r
71 extern uint8_t excludedEventCodes[NEventCodes / 8 + 1];
\r
73 /******************************************************************************
\r
75 * This data-structure is used to provide a mechanism for 1-byte trace object
\r
76 * handles. This way, only 1 byte is necessary instead of 4 bytes (a pointer)
\r
77 * when storing a reference to an object. This allows for up to 255 objects of
\r
78 * each object class active at any given moment. There can be more "historic"
\r
79 * objects, that have been deleted - that number is only limited by the size of
\r
81 * Note that handle zero (0) is not used, it is a code for an invalid handle.
\r
83 * This data structure keeps track of the FREE handles, not the handles in use.
\r
84 * This data structure contains one stack per object class. When a handle is
\r
85 * allocated to an object, the next free handle is popped from the stack. When
\r
86 * a handle is released (on object delete), it is pushed back on the stack.
\r
87 * Note that there is no initialization code that pushed the free handles
\r
88 * initially, that is not necessary due to the following optimization:
\r
90 * The stack of handles (objectHandles) is initially all zeros. Since zero
\r
91 * is not a valid handle, that is a signal of additional handles needed.
\r
92 * If a zero is received when popping a new handle, it is replaced by the
\r
93 * index of the popped handle instead.
\r
95 *****************************************************************************/
\r
98 /* For each object class, the index of the next handle to allocate */
\r
99 int16_t indexOfNextAvailableHandle[ TRACE_NCLASSES ];
\r
101 /* The lowest index of this class (constant) */
\r
102 int16_t lowestIndexOfClass[ TRACE_NCLASSES ];
\r
104 /* The highest index of this class (constant) */
\r
105 int16_t highestIndexOfClass[ TRACE_NCLASSES ];
\r
107 /* The highest use count for this class (for statistics) */
\r
108 int16_t handleCountWaterMarksOfClass[ TRACE_NCLASSES ];
\r
110 /* The free object handles - a set of stacks within this array */
\r
111 objectHandleType objectHandles[ TRACE_KERNEL_OBJECT_COUNT ];
\r
113 } objectHandleStackType;
\r
115 extern objectHandleStackType objectHandleStacks;
\r
117 /******************************************************************************
\r
118 * Object Property Table
\r
119 * The Object Table contains name and other properties of the objects (tasks,
\r
120 * queues, mutexes, etc). The below data structures defines the properties of
\r
121 * each object class and are used to cast the byte buffer into a cleaner format.
\r
123 * The values in the object table are continuously overwritten and always
\r
124 * represent the current state. If a property is changed during runtime, the OLD
\r
125 * value should be stored in the trace buffer, not the new value (since the new
\r
126 * value is found in the Object Property Table).
\r
127 * For close events this mechanism is the old names are stored in the symbol
\r
128 * table), for "priority set" (the old priority is stored in the event data)
\r
129 * and for "isActive", where the value decides if the task switch event type
\r
130 * should be "new" or "resume".
\r
131 ******************************************************************************/
\r
136 uint32_t NumberOfObjectClasses;
\r
138 uint32_t ObjectPropertyTableSizeInBytes;
\r
140 /* This is used to calculate the index in the dynamic object table
\r
141 (handle - 1 - nofStaticObjects = index)*/
\r
142 uint8_t NumberOfObjectsPerClass[ 4*((TRACE_NCLASSES+3)/4)];
\r
144 /* Allocation size rounded up to the closest multiple of 4 */
\r
145 uint8_t NameLengthPerClass[ 4*((TRACE_NCLASSES+3)/4) ];
\r
147 uint8_t TotalPropertyBytesPerClass[ 4*((TRACE_NCLASSES+3)/4) ];
\r
149 /* Allocation size rounded up to the closest multiple of 2 */
\r
150 uint16_t StartIndexOfClass[ 2*((TRACE_NCLASSES+1)/2) ];
\r
152 /* The actual handles issued, should be Initiated to all zeros */
\r
153 uint8_t objbytes[ 4*((TRACE_OBJECT_TABLE_SIZE+3)/4) ];
\r
154 } ObjectPropertyTableType;
\r
156 /* Symbol table data structure */
\r
159 /* = SYMBOL_HISTORY_TABLE_SIZE_IN_BYTES */
\r
160 uint32_t symTableSize;
\r
162 /* Entry 0 is reserved. Any reference to entry 0 implies NULL*/
\r
163 uint32_t nextFreeSymbolIndex;
\r
165 /* Size rounded up to closest multiple of 4, to avoid alignment issues*/
\r
166 uint8_t symbytes[4*((SYMBOL_TABLE_SIZE+3)/4)];
\r
168 /* Used for lookups - Up to 64 linked lists within the symbol table
\r
169 connecting all entries with the same 6 bit checksum.
\r
170 This field holds the current list heads. Should be initiated to zeros */
\r
171 uint16_t latestEntryOfChecksum[64];
\r
175 /*******************************************************************************
\r
176 * The data structures of the different events, all 4 bytes long
\r
177 ******************************************************************************/
\r
182 objectHandleType objHandle;
\r
183 uint16_t dts; /* differential timestamp - time since last event */
\r
184 } TSEvent, TREvent;
\r
190 uint16_t dts; /* differential timestamp - time since last event */
\r
197 uint16_t dts; /* differential timestamp - time since last event */
\r
203 objectHandleType objHandle;
\r
205 uint8_t dts; /* differential timestamp - time since last event */
\r
206 } KernelCallWithParamAndHandle;
\r
211 uint8_t dts; /* differential timestamp - time since last event */
\r
213 } KernelCallWithParam16;
\r
218 objectHandleType objHandle; /* the handle of the closed object */
\r
219 uint16_t symbolIndex; /* the name of the closed object */
\r
220 } ObjCloseNameEvent;
\r
228 } ObjClosePropEvent;
\r
234 uint16_t payload; /* the name of the user event */
\r
241 /* 8 bits extra for storing DTS, if it does not fit in ordinary event
\r
242 (this one is always MSB if used) */
\r
245 /* 16 bits extra for storing DTS, if it does not fit in ordinary event. */
\r
257 /*******************************************************************************
\r
258 * The separate user event buffer structure. Can be enabled in trcConfig.h.
\r
259 ******************************************************************************/
\r
261 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
265 traceLabel defaultFormat;
\r
266 } ChannelFormatPair;
\r
272 uint32_t wraparoundCounter;
\r
273 uint32_t numberOfSlots;
\r
274 uint32_t nextSlotToWrite;
\r
275 uint8_t numberOfChannels;
\r
279 ChannelFormatPair channels[CHANNEL_FORMAT_PAIRS+1];
\r
280 uint8_t channelBuffer[(USER_EVENT_BUFFER_SIZE + 3) & 0xFFFFFFFC]; /* 1 byte per slot, with padding for 4 byte alignment */
\r
281 uint8_t dataBuffer[USER_EVENT_BUFFER_SIZE * 4]; /* 4 bytes per slot */
\r
286 /*******************************************************************************
\r
287 * The main data structure, read by Tracealyzer from the RAM dump
\r
288 ******************************************************************************/
\r
292 uint8_t startmarker0;
\r
293 uint8_t startmarker1;
\r
294 uint8_t startmarker2;
\r
295 uint8_t startmarker3;
\r
296 uint8_t startmarker4;
\r
297 uint8_t startmarker5;
\r
298 uint8_t startmarker6;
\r
299 uint8_t startmarker7;
\r
300 uint8_t startmarker8;
\r
301 uint8_t startmarker9;
\r
302 uint8_t startmarker10;
\r
303 uint8_t startmarker11;
\r
305 /* Used to determine Kernel and Endianess */
\r
308 /* Currently 1 for v2.2.2 (0 earlier)*/
\r
309 uint8_t minor_version;
\r
311 /* This should be 0 if lower IRQ priority values implies higher priority
\r
312 levels, such as on ARM Cortex M. If the opposite scheme is used, i.e.,
\r
313 if higher IRQ priority values means higher priority, this should be 1. */
\r
314 uint8_t irq_priority_order;
\r
316 /* sizeof(RecorderDataType) - just for control */
\r
319 /* Current number of events recorded */
\r
320 uint32_t numEvents;
\r
322 /* The buffer size, in number of event records */
\r
323 uint32_t maxEvents;
\r
325 /* The event buffer index, where to write the next event */
\r
326 uint32_t nextFreeIndex;
\r
328 /* 1 if the buffer is full, 0 otherwise */
\r
329 uint32_t bufferIsFull;
\r
331 /* The frequency of the clock/timer/counter used as time base */
\r
332 uint32_t frequency;
\r
334 /* The absolute timestamp of the last stored event, in the native
\r
335 timebase, modulo frequency! */
\r
336 uint32_t absTimeLastEvent;
\r
338 /* The number of seconds in total - lasts for 136 years */
\r
339 uint32_t absTimeLastEventSecond;
\r
341 /* 1 if the recorder has been started, 0 if not yet started or stopped.
\r
342 This is a 32 bit variable due to alignment issues. */
\r
343 uint32_t recorderActive;
\r
345 /* For storing a Team License key */
\r
346 uint8_t teamLicenceKey[32];
\r
348 /* 0xF0F0F0F0 - for control only */
\r
349 int32_t debugMarker0;
\r
351 /* The Object Property Table holds information about currently active
\r
352 tasks, queues, and other recorded objects. This is updated on each
\r
353 create call and includes object name and other properties. */
\r
354 ObjectPropertyTableType ObjectPropertyTable;
\r
356 /* 0xF1F1F1F1 - for control only */
\r
357 int32_t debugMarker1;
\r
359 /* The Symbol Table stores strings for User Events and is also used to
\r
360 store names of deleted objects, which still may be in the trace but no
\r
361 longer are available. */
\r
362 symbolTableType SymbolTable;
\r
364 /* For inclusion of float support, and for endian detection of floats.
\r
365 The value should be (float)1 or (uint32_t)0 */
\r
366 #if (INCLUDE_FLOAT_SUPPORT == 1)
\r
367 float exampleFloatEncoding;
\r
369 uint32_t exampleFloatEncoding;
\r
371 /* This is non-zero if an internal error occurred in the recorder, e.g., if
\r
372 one of the Nxxx constants was too small. The systemInfo string will then
\r
373 contain an error message that is displayed when attempting to view the
\r
375 uint32_t internalErrorOccured;
\r
377 /* 0xF2F2F2F2 - for control only */
\r
378 int32_t debugMarker2;
\r
380 /* Generic system information string, presented in the tool. Note that this
\r
381 is also used for storing any internal error messages from the recorder, so
\r
382 do not make TRACE_DESCRIPTION_MAX_LENGTH too small. 80 is recommended. */
\r
383 char systemInfo[TRACE_DESCRIPTION_MAX_LENGTH];
\r
385 /* 0xF3F3F3F3 - for control only */
\r
386 int32_t debugMarker3;
\r
388 /* The event data, in 4-byte records */
\r
389 uint8_t eventData[ EVENT_BUFFER_SIZE * 4 ];
\r
391 #if (USE_SEPARATE_USER_EVENT_BUFFER == 1)
\r
392 UserEventBuffer userEventBuffer;
\r
395 /* This should always be 0 */
\r
396 uint32_t endOfSecondaryBlocks;
\r
398 uint8_t endmarker0;
\r
399 uint8_t endmarker1;
\r
400 uint8_t endmarker2;
\r
401 uint8_t endmarker3;
\r
402 uint8_t endmarker4;
\r
403 uint8_t endmarker5;
\r
404 uint8_t endmarker6;
\r
405 uint8_t endmarker7;
\r
406 uint8_t endmarker8;
\r
407 uint8_t endmarker9;
\r
408 uint8_t endmarker10;
\r
409 uint8_t endmarker11;
\r
410 } RecorderDataType;
\r
412 extern RecorderDataType* RecorderDataPtr;
\r
414 /* Internal functions */
\r
416 uint16_t prvTraceGetDTS(uint16_t param_maxDTS);
\r
418 void prvTraceGetChecksum(const char *pname, uint8_t* pcrc, uint8_t* plength);
\r
420 traceLabel prvTraceCreateSymbolTableEntry(const char* name,
\r
423 traceLabel channel);
\r
425 traceLabel prvTraceLookupSymbolTableEntry(const char* name,
\r
428 traceLabel channel);
\r
430 traceLabel prvTraceOpenSymbol(const char* name, traceLabel userEventChannel);
\r
432 void prvTraceUpdateCounters(void);
\r
434 void prvCheckDataToBeOverwrittenForMultiEntryEvents(uint8_t nEntries);
\r
436 objectHandleType xTraceGetObjectHandle(traceObjectClass objectclass);
\r
438 void vTraceFreeObjectHandle(traceObjectClass objectclass,
\r
439 objectHandleType handle);
\r
441 void vTraceSetObjectName(traceObjectClass objectclass,
\r
442 objectHandleType handle,
\r
445 void* xTraceNextFreeEventBufferSlot(void);
\r
447 uint16_t uiIndexOfObject(objectHandleType objecthandle,
\r
448 uint8_t objectclass);
\r
451 /*******************************************************************************
\r
454 * Called by various parts in the recorder. Stops the recorder and stores a
\r
455 * pointer to an error message, which is printed by the monitor task.
\r
456 ******************************************************************************/
\r
457 void vTraceError(const char* msg);
\r
459 /*******************************************************************************
\r
460 * prvTraceInitTraceData
\r
462 * Allocates and initializes the recorder data structure, based on the constants
\r
463 * in trcConfig.h. This allows for allocating the data on the heap, instead of
\r
464 * using a static declaration.
\r
465 ******************************************************************************/
\r
466 void prvTraceInitTraceData(void);
\r
468 /* Internal macros */
\r
470 #define TRACE_PROPERTY_NAME_GET(objectclass, objecthandle) \
\r
471 (const char*)(& RecorderDataPtr->ObjectPropertyTable.objbytes \
\r
472 [uiIndexOfObject(objecthandle, objectclass)])
\r
474 #define TRACE_PROPERTY_OBJECT_STATE(objectclass, handle) \
\r
475 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \
\r
476 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass]]
\r
478 #define TRACE_PROPERTY_ACTOR_PRIORITY(objectclass, handle) \
\r
479 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \
\r
480 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass] + 1]
\r
482 #define TRACE_SET_FLAG_ISEXCLUDED(flags, bitIndex) flags[(bitIndex) >> 3] |= (1 << ((bitIndex) & 7))
\r
483 #define TRACE_CLEAR_FLAG_ISEXCLUDED(flags, bitIndex) flags[(bitIndex) >> 3] &= ~(1 << ((bitIndex) & 7))
\r
484 #define TRACE_GET_FLAG_ISEXCLUDED(flags, bitIndex) (flags[(bitIndex) >> 3] & (1 << ((bitIndex) & 7)))
\r
486 #define TRACE_SET_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) TRACE_SET_FLAG_ISEXCLUDED(excludedEventCodes, eventCode)
\r
487 #define TRACE_CLEAR_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) TRACE_CLEAR_FLAG_ISEXCLUDED(excludedEventCodes, eventCode)
\r
488 #define TRACE_GET_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) TRACE_GET_FLAG_ISEXCLUDED(excludedEventCodes, eventCode)
\r
490 /* DEBUG ASSERTS */
\r
491 #if defined USE_TRACE_ASSERT && USE_TRACE_ASSERT != 0
\r
492 #define TRACE_ASSERT(eval, msg, defRetVal) \
\r
495 vTraceError("TRACE_ASSERT: " msg); \
\r
496 return defRetVal; \
\r
499 #define TRACE_ASSERT(eval, msg, defRetVal)
\r