1 /*******************************************************************************
\r
2 * FreeRTOS+Trace v2.3.0 Recorder Library
\r
3 * Percepio AB, www.percepio.com
\r
7 * Core functionallity of the FreeRTOS+Trace 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 trcPort.c and trcPort.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 * FreeRTOS+Trace is available as Free Edition and in two premium editions.
\r
35 * You may use the premium features during 30 days for evaluation.
\r
36 * Download FreeRTOS+Trace at http://www.percepio.com/products/downloads/
\r
38 * Copyright Percepio AB, 2012.
\r
40 ******************************************************************************/
\r
48 #include "FreeRTOS.h"
\r
49 #include "trcConfig.h"
\r
50 #include "trcTypes.h"
\r
51 #include "trcPort.h"
\r
53 extern volatile int recorder_busy;
\r
55 #define trcCRITICAL_SECTION_BEGIN() {taskENTER_CRITICAL(); recorder_busy++;}
\r
56 #define trcCRITICAL_SECTION_END() {recorder_busy--; taskEXIT_CRITICAL();}
\r
59 #define VERSION 0x1AA1
\r
60 #define MINOR_VERSION 1
\r
61 #define STORE_MODE_STOP_WHEN_FULL 1
\r
62 #define STORE_MODE_RING_BUFFER 2
\r
63 #define TRACE_DATA_ALLOCATION_STATIC 1
\r
64 #define TRACE_DATA_ALLOCATION_DYNAMIC 2
\r
66 /******************************************************************************
\r
67 * Object Property Table
\r
68 * The Object Table contains name and other properties of the objects (tasks,
\r
69 * queues, mutexes, etc). The below data structures defines the properties of
\r
70 * each object class and are used to cast the byte buffer into a cleaner format.
\r
72 * The values in the object table are continously overwritten and always
\r
73 * represent the current state. If a property is changed during runtime, the OLD
\r
74 * value should be stored in the trace buffer, not the new value (since the new
\r
75 * value is found in the Object Property Table).
\r
76 * For close events this mechanism is the old names are stored in the symbol
\r
77 * table), for "priority set" (the old priority is stored in the event data)
\r
78 * and for "isActive", where the value decides is the taskswitch event type
\r
79 * should be "new" or "resume".
\r
80 ******************************************************************************/
\r
82 /* The size of the Object Property Table entries, in bytes, per object */
\r
84 /* Queue properties (except name): current number of message in queue */
\r
85 #define PropertyTableSizeQueue (NameLenQueue + 1)
\r
87 /* Semaphore properties (except name): state (signaled = 1, cleared = 0) */
\r
88 #define PropertyTableSizeSemaphore (NameLenSemaphore + 1)
\r
90 /* Mutex properties (except name): owner (task handle, 0 = free) */
\r
91 #define PropertyTableSizeMutex (NameLenMutex + 1)
\r
93 /* Task properties (except name): Byte 0: Current priority
\r
94 Byte 1: state (if already active)
\r
95 Byte 2: InstanceFinishEvent_ServiceCode
\r
96 Byte 3: InstanceFinishEvent_ObjHandle */
\r
97 #define PropertyTableSizeTask (NameLenTask + 4)
\r
99 /* ISR properties: Byte 0: priority
\r
100 Byte 1: state (if already active) */
\r
101 #define PropertyTableSizeISR (NameLenISR + 2)
\r
103 /* The layout of the byte array representing the Object Property Table */
\r
104 #define StartIndexQueue 0
\r
105 #define StartIndexSemaphore StartIndexQueue + NQueue * PropertyTableSizeQueue
\r
106 #define StartIndexMutex StartIndexSemaphore + NSemaphore * PropertyTableSizeSemaphore
\r
107 #define StartIndexTask StartIndexMutex + NMutex * PropertyTableSizeMutex
\r
108 #define StartIndexISR StartIndexTask + NTask * PropertyTableSizeTask
\r
109 #define DynObjTableSize StartIndexISR + NISR * PropertyTableSizeISR
\r
114 uint32_t NumberOfObjectClasses;
\r
116 uint32_t ObjectPropertyTableSizeInBytes;
\r
118 /* This is used to calculate the index in the dynamic object table
\r
119 (handle - 1 - nofStaticObjects = index)*/
\r
120 uint8_t NumberOfObjectsPerClass[ 4*((NCLASSES+3)/4)];
\r
122 /* Allocation size rounded up to the closest multiple of 4 */
\r
123 uint8_t NameLengthPerClass[ 4*((NCLASSES+3)/4) ];
\r
125 uint8_t TotalPropertyBytesPerClass[ 4*((NCLASSES+3)/4) ];
\r
127 /* Allocation size rounded up to the closest multiple of 2 */
\r
128 uint16_t StartIndexOfClass[ 2*((NCLASSES+1)/2) ];
\r
130 /* The actual handles issued, should be Initiated to all zeros */
\r
131 uint8_t objbytes[ 4*((DynObjTableSize+3)/4) ];
\r
132 } ObjectPropertyTableType;
\r
134 /* Symbol table data structure */
\r
137 /* = SYMBOL_HISTORY_TABLE_SIZE_IN_BYTES */
\r
138 uint32_t symTableSize;
\r
140 /* Entry 0 is reserved. Any reference to entry 0 implies NULL*/
\r
141 uint32_t nextFreeSymbolIndex;
\r
143 /* Size rounded up to closest multiple of 4, to avoid alignment issues*/
\r
144 uint8_t symbytes[4*((SYMBOL_TABLE_SIZE+3)/4)];
\r
146 /* Used for lookups - Up to 64 linked lists within the symbol table
\r
147 connecting all entries with the same 6 bit checksum.
\r
148 This field holds the current list heads. Should be initiated to zeros */
\r
149 uint16_t latestEntryOfChecksum[64];
\r
153 /*******************************************************************************
\r
154 * The data structures of the different events, all 4 bytes long
\r
155 ******************************************************************************/
\r
160 objectHandleType objHandle;
\r
161 uint16_t dts; /* differential timestamp - time since last event */
\r
162 } TSEvent, TREvent;
\r
174 objectHandleType objHandle;
\r
177 } KernelCallWithParamAndHandle;
\r
184 } KernelCallWithParam16;
\r
189 objectHandleType objHandle; /* the handle of the closed object */
\r
190 uint16_t symbolIndex; /* the name of the closed object */
\r
191 } ObjCloseNameEvent;
\r
199 } ObjClosePropEvent;
\r
205 uint16_t payload; /* the name of the user event */
\r
212 /* 8 bits extra for storing DTS, if it does not fit in ordinary event
\r
213 (this one is always MSB if used) */
\r
216 /* 16 bits extra for storing DTS, if it does not fit in ordinary event. */
\r
221 /*******************************************************************************
\r
222 * The main datastructure, read by FreeRTOS+Trace from the RAM dump
\r
223 ******************************************************************************/
\r
227 uint8_t startmarker0;
\r
228 uint8_t startmarker1;
\r
229 uint8_t startmarker2;
\r
230 uint8_t startmarker3;
\r
231 uint8_t startmarker4;
\r
232 uint8_t startmarker5;
\r
233 uint8_t startmarker6;
\r
234 uint8_t startmarker7;
\r
235 uint8_t startmarker8;
\r
236 uint8_t startmarker9;
\r
237 uint8_t startmarker10;
\r
238 uint8_t startmarker11;
\r
240 /* For FreeRTOS: 0x1AA1 */
\r
243 /* Currently 1 for v2.2.2 (0 earlier)*/
\r
244 uint8_t minor_version;
\r
246 /* This should be 0 if lower irq priority values implies higher priority
\r
247 levels, such as on ARM Cortex M. If the opposite scheme is used, i.e.,
\r
248 if higher irq priority values means higher priority, this should be 1. */
\r
249 uint8_t irq_priority_order;
\r
251 /* sizeof(RecorderDataType) - just for control */
\r
254 /* Current number of events recorded */
\r
255 uint32_t numEvents;
\r
257 /* The buffer size, in number of event records */
\r
258 uint32_t maxEvents;
\r
260 /* The event buffer index, where to write the next event */
\r
261 uint32_t nextFreeIndex;
\r
263 /* 1 if the buffer is full, 0 otherwise */
\r
264 uint32_t bufferIsFull;
\r
266 /* The frequency of the clock/timer/counter used as time base */
\r
267 uint32_t frequency;
\r
269 /* The absolute timestamp of the last stored event, in the native
\r
270 timebase, modulo frequency! */
\r
271 uint32_t absTimeLastEvent;
\r
273 /* The number of seconds in total - lasts for 136 years */
\r
274 uint32_t absTimeLastEventSecond;
\r
276 /* 1 if the recorder has been started, 0 if not yet started or stopped.
\r
277 This is a 32 bit variable due to alignment issues. */
\r
278 uint32_t recorderActive;
\r
280 /* For storing a Team License key */
\r
281 uint8_t teamLicenceKey[32];
\r
283 /* 0xF0F0F0F0 - for control only */
\r
284 int32_t debugMarker0;
\r
286 /* The Object Property Table holds information about currently active
\r
287 tasks, queues, and other recorded objects. This is updated on each
\r
288 create call and includes object name and other properties. */
\r
289 ObjectPropertyTableType ObjectPropertyTable;
\r
291 /* 0xF1F1F1F1 - for control only */
\r
292 int32_t debugMarker1;
\r
294 /* The Symbol Table stores strings for User Events and is also used to
\r
295 store names of deleted objects, which still may be in the trace but no
\r
296 longer are available. */
\r
297 symbolTableType SymbolTable;
\r
299 /* For includsion of float support, and for endian detection of floats.
\r
300 The value should be (float)1 or (uint32_t)0 */
\r
301 #if (INCLUDE_FLOAT_SUPPORT == 1)
\r
302 float exampleFloatEncoding;
\r
304 uint32_t exampleFloatEncoding;
\r
306 /* This is non-zero if an internal error occured in the recorder, e.g., if
\r
307 one of the Nxxx constants was too small. The systemInfo string will then
\r
308 contain an error message that is displayed when attempting to view the
\r
310 uint32_t internalErrorOccured;
\r
312 /* 0xF2F2F2F2 - for control only */
\r
313 int32_t debugMarker2;
\r
315 /* Generic system information string, presented in the tool. Note that this
\r
316 is also used for storing any internal error messages from the recorder, so
\r
317 do not make TRACE_DESCRIPTION_MAX_LENGTH too small. 80 is recommended. */
\r
318 char systemInfo[TRACE_DESCRIPTION_MAX_LENGTH];
\r
320 /* 0xF3F3F3F3 - for control only */
\r
321 int32_t debugMarker3;
\r
323 /* The event data, in 4-byte records */
\r
324 uint8_t eventData[ EVENT_BUFFER_SIZE * 4 ];
\r
326 uint8_t endmarker0;
\r
327 uint8_t endmarker1;
\r
328 uint8_t endmarker2;
\r
329 uint8_t endmarker3;
\r
330 uint8_t endmarker4;
\r
331 uint8_t endmarker5;
\r
332 uint8_t endmarker6;
\r
333 uint8_t endmarker7;
\r
334 uint8_t endmarker8;
\r
335 uint8_t endmarker9;
\r
336 uint8_t endmarker10;
\r
337 uint8_t endmarker11;
\r
339 } RecorderDataType;
\r
341 extern RecorderDataType* RecorderDataPtr;
\r
343 /******************************************************************************
\r
344 * ObjectHandleStack
\r
345 * This data-structure is used to provide a mechanism for 1-byte trace object
\r
346 * handles. This way, only 1 byte is necessary instead of 4 bytes (a pointer)
\r
347 * when storing a reference to an object. This allows for up to 255 objects of
\r
348 * each object class - Task, ISR, Semaphore, CountingSemaphore, Mutex and Queue,
\r
349 * active at any given moment. There can be more "historic" objects, that have
\r
350 * been deleted - that number is only limited by the size of the symbol table.
\r
351 * Note that handle zero (0) is not used, it is a code for an invalid handle.
\r
353 * This data structure keeps track of the FREE handles, not the handles in use.
\r
354 * This datastructure contains one stack per object class. When a handle is
\r
355 * allocated to an object, the next free handle is popped from the stack. When
\r
356 * a handle is released (on object delete), it is pushed back on the stack.
\r
357 * Note that there is no initialization code that pushed the free handles
\r
358 * initially, that is not necessary due to the following optimization:
\r
360 * The stack of handles (objectHandles) is initially all zeros. Since zero
\r
361 * is not a valid handle, that is a signal of additional handles needed.
\r
362 * If a zero is received when popping a new handle, it is replaced by the
\r
363 * index of the popped handle instead.
\r
365 *****************************************************************************/
\r
368 /* For each object class, the index of the next handle to allocate */
\r
369 int16_t indexOfNextAvailableHandle[ NCLASSES ];
\r
371 /* The lowest index of this class (constant) */
\r
372 int16_t lowestIndexOfClass[ NCLASSES ];
\r
374 /* The highest index of this class (constant) */
\r
375 int16_t highestIndexOfClass[ NCLASSES ];
\r
377 /* The highest use count for this class (for statistics) */
\r
378 int16_t handleCountWaterMarksOfClass[ NCLASSES ];
\r
380 /* The free object handles - a set of stacks within this array */
\r
381 objectHandleType objectHandles[ NTask+NISR+NSemaphore+NMutex+NQueue ];
\r
383 } objectHandleStackType;
\r
385 /* Internal data */
\r
387 extern objectHandleStackType objectHandleStacks;
\r
389 /* Structures to handle the exclude flags for all objects, tasks and event codes */
\r
390 #define NEventCodes 0x100
\r
391 extern uint8_t excludedFlags[(NEventCodes+NQueue+NSemaphore+NMutex+NTask) / 8 + 1];
\r
392 extern uint8_t ifeFlags[NTask / 8 + 1];
\r
394 /* Internal functions */
\r
396 uint16_t prvTraceGetDTS(uint16_t param_maxDTS);
\r
398 void prvTraceGetChecksum(const char *pname, uint8_t* pcrc, uint8_t* plength);
\r
400 traceLabel prvTraceCreateSymbolTableEntry(const char* name,
\r
403 traceLabel channel);
\r
405 traceLabel prvTraceLookupSymbolTableEntry(const char* name,
\r
408 traceLabel channel);
\r
410 traceLabel prvTraceOpenSymbol(const char* name, traceLabel userEventChannel);
\r
412 void prvTraceUpdateCounters(void);
\r
414 void prvCheckDataToBeOverwrittenForMultiEntryUserEvents(uint8_t nEntries);
\r
416 objectHandleType xTraceGetObjectHandle(traceObjectClass objectclass);
\r
418 void vTraceFreeObjectHandle(traceObjectClass objectclass,
\r
419 objectHandleType handle);
\r
421 void vTraceSetObjectName(traceObjectClass objectclass,
\r
422 objectHandleType handle,
\r
425 void* xTraceNextFreeEventBufferSlot(void);
\r
427 uint16_t uiIndexOfObject(objectHandleType objecthandle,
\r
428 uint8_t objectclass);
\r
431 /*******************************************************************************
\r
434 * Called by various parts in the recorder. Stops the recorder and stores a
\r
435 * pointer to an error message, which is printed by the monitor task.
\r
436 ******************************************************************************/
\r
437 void vTraceError(const char* msg);
\r
439 /*******************************************************************************
\r
440 * xTraceGetLastError
\r
442 * Gives the last error message, if any. NULL if no error message is stored.
\r
443 * The message is cleared on read.
\r
444 ******************************************************************************/
\r
445 char* xTraceGetLastError(void);
\r
447 /*******************************************************************************
\r
448 * xTraceInitTraceData
\r
450 * Allocates and initializes the recorder datastructure, based on the constants
\r
451 * in trcConfig.h. This allows for allocating the data on the heap, instead of
\r
452 * using a static declaration.
\r
453 ******************************************************************************/
\r
454 RecorderDataType* xTraceInitTraceData(void);
\r
456 /* Internal macros */
\r
458 #define PROPERTY_NAME_GET(objectclass, objecthandle) \
\r
459 (const char*)(& RecorderDataPtr->ObjectPropertyTable.objbytes \
\r
460 [uiIndexOfObject(objecthandle, objectclass)])
\r
462 #define PROPERTY_OBJECT_STATE(objectclass, handle) \
\r
463 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \
\r
464 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass]]
\r
466 #define PROPERTY_ACTOR_PRIORITY(objectclass, handle) \
\r
467 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \
\r
468 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass] + 1]
\r
470 #define PROPERTY_TASK_IFE_SERVICECODE(handle) \
\r
471 RecorderDataPtr->ObjectPropertyTable.objbytes \
\r
472 [uiIndexOfObject(handle, TRACE_CLASS_TASK) \
\r
473 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[TRACE_CLASS_TASK]+2]
\r
475 #define PROPERTY_TASK_IFE_OBJHANDLE(handle) \
\r
476 RecorderDataPtr->ObjectPropertyTable.objbytes \
\r
477 [uiIndexOfObject(handle, TRACE_CLASS_TASK) \
\r
478 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[TRACE_CLASS_TASK]+3]
\r
480 #define SET_FLAG_ISEXCLUDED(bitIndex) excludedFlags[(bitIndex) >> 3] |= (1 << ((bitIndex) & 7))
\r
481 #define CLEAR_FLAG_ISEXCLUDED(bitIndex) excludedFlags[(bitIndex) >> 3] &= ~(1 << ((bitIndex) & 7))
\r
482 #define GET_FLAG_ISEXCLUDED(bitIndex) (excludedFlags[(bitIndex) >> 3] & (1 << ((bitIndex) & 7)))
\r
484 #define SET_FLAG_MARKIFE(bitIndex) ifeFlags[(bitIndex) >> 3] |= (1 << ((bitIndex) & 7))
\r
485 #define CLEAR_FLAG_MARKIFE(bitIndex) ifeFlags[(bitIndex) >> 3] &= ~(1 << ((bitIndex) & 7))
\r
486 #define GET_FLAG_MARKIFE(bitIndex) (ifeFlags[(bitIndex) >> 3] & (1 << ((bitIndex) & 7)))
\r
488 #define SET_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) SET_FLAG_ISEXCLUDED(eventCode)
\r
489 #define CLEAR_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) CLEAR_FLAG_ISEXCLUDED(eventCode)
\r
490 #define GET_EVENT_CODE_FLAG_ISEXCLUDED(eventCode) GET_FLAG_ISEXCLUDED(eventCode)
\r
492 #define SET_QUEUE_FLAG_ISEXCLUDED(queueHandle) SET_FLAG_ISEXCLUDED(NEventCodes+queueHandle-1)
\r
493 #define CLEAR_QUEUE_FLAG_ISEXCLUDED(queueHandle) CLEAR_FLAG_ISEXCLUDED(NEventCodes+queueHandle-1)
\r
494 #define GET_QUEUE_FLAG_ISEXCLUDED(queueHandle) GET_FLAG_ISEXCLUDED(NEventCodes+queueHandle-1)
\r
496 #define SET_SEMAPHORE_FLAG_ISEXCLUDED(semaphoreHandle) SET_FLAG_ISEXCLUDED(NEventCodes+NQueue+semaphoreHandle-1)
\r
497 #define CLEAR_SEMAPHORE_FLAG_ISEXCLUDED(semaphoreHandle) CLEAR_FLAG_ISEXCLUDED(NEventCodes+NQueue+semaphoreHandle-1)
\r
498 #define GET_SEMAPHORE_FLAG_ISEXCLUDED(semaphoreHandle) GET_FLAG_ISEXCLUDED(NEventCodes+NQueue+semaphoreHandle-1)
\r
500 #define SET_MUTEX_FLAG_ISEXCLUDED(mutexHandle) SET_FLAG_ISEXCLUDED(NEventCodes+NQueue+NSemaphore+mutexHandle-1)
\r
501 #define CLEAR_MUTEX_FLAG_ISEXCLUDED(mutexHandle) CLEAR_FLAG_ISEXCLUDED(NEventCodes+NQueue+NSemaphore+mutexHandle-1)
\r
502 #define GET_MUTEX_FLAG_ISEXCLUDED(mutexHandle) GET_FLAG_ISEXCLUDED(NEventCodes+NQueue+NSemaphore+mutexHandle-1)
\r
504 #define SET_TASK_FLAG_ISEXCLUDED(taskHandle) SET_FLAG_ISEXCLUDED(NEventCodes+NQueue+NSemaphore+NMutex+taskHandle-1)
\r
505 #define CLEAR_TASK_FLAG_ISEXCLUDED(taskHandle) CLEAR_FLAG_ISEXCLUDED(NEventCodes+NQueue+NSemaphore+NMutex+taskHandle-1)
\r
506 #define GET_TASK_FLAG_ISEXCLUDED(taskHandle) GET_FLAG_ISEXCLUDED(NEventCodes+NQueue+NSemaphore+NMutex+taskHandle-1)
\r
508 #define SET_TASK_FLAG_MARKIFE(bitIndex) SET_FLAG_MARKIFE(bitIndex-1)
\r
509 #define CLEAR_TASK_FLAG_MARKIFE(bitIndex) CLEAR_FLAG_MARKIFE(bitIndex-1)
\r
510 #define GET_TASK_FLAG_MARKIFE(bitIndex) GET_FLAG_MARKIFE(bitIndex-1)
\r
512 /* For debug printouts - the names of the object classes */
\r
513 extern char OBJECTCLASSNAME[NCLASSES][10];
\r