1 /*******************************************************************************
\r
2 * FreeRTOS+Trace v2.2.3 Recorder Library
\r
3 * Percepio AB, www.percepio.se
\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.se/index.php?page=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
55 #define VERSION 0x1AA1
\r
56 #define MINOR_VERSION 1
\r
57 #define STORE_MODE_STOP_WHEN_FULL 1
\r
58 #define STORE_MODE_RING_BUFFER 2
\r
59 #define TRACE_DATA_ALLOCATION_STATIC 1
\r
60 #define TRACE_DATA_ALLOCATION_DYNAMIC 2
\r
62 /******************************************************************************
\r
63 * Object Property Table
\r
64 * The Object Table contains name and other properties of the objects (tasks,
\r
65 * queues, mutexes, etc). The below data structures defines the properties of
\r
66 * each object class and are used to cast the byte buffer into a cleaner format.
\r
68 * The values in the object table are continously overwritten and always
\r
69 * represent the current state. If a property is changed during runtime, the OLD
\r
70 * value should be stored in the trace buffer, not the new value (since the new
\r
71 * value is found in the Object Property Table).
\r
72 * For close events this mechanism is the old names are stored in the symbol
\r
73 * table), for "priority set" (the old priority is stored in the event data)
\r
74 * and for "isActive", where the value decides is the taskswitch event type
\r
75 * should be "new" or "resume".
\r
76 ******************************************************************************/
\r
78 /* The size of the Object Property Table entries, in bytes, per object */
\r
80 /* Queue properties (except name): current number of message in queue */
\r
81 #define PropertyTableSizeQueue (NameLenQueue + 1)
\r
83 /* Semaphore properties (except name): state (signaled = 1, cleared = 0) */
\r
84 #define PropertyTableSizeSemaphore (NameLenSemaphore + 1)
\r
86 /* Mutex properties (except name): owner (task handle, 0 = free) */
\r
87 #define PropertyTableSizeMutex (NameLenMutex + 1)
\r
89 /* Task properties (except name): Byte 0: Current priority
\r
90 Byte 1: state (if already active)
\r
91 Byte 2: InstanceFinishEvent_ServiceCode
\r
92 Byte 3: InstanceFinishEvent_ObjHandle */
\r
93 #define PropertyTableSizeTask (NameLenTask + 4)
\r
95 /* ISR properties: Byte 0: priority
\r
96 Byte 1: state (if already active) */
\r
97 #define PropertyTableSizeISR (NameLenISR + 2)
\r
99 /* The layout of the byte array representing the Object Property Table */
\r
100 #define StartIndexQueue 0
\r
101 #define StartIndexSemaphore StartIndexQueue + NQueue * PropertyTableSizeQueue
\r
102 #define StartIndexMutex StartIndexSemaphore + NSemaphore * PropertyTableSizeSemaphore
\r
103 #define StartIndexTask StartIndexMutex + NMutex * PropertyTableSizeMutex
\r
104 #define StartIndexISR StartIndexTask + NTask * PropertyTableSizeTask
\r
105 #define DynObjTableSize StartIndexISR + NISR * PropertyTableSizeISR
\r
110 uint32_t NumberOfObjectClasses;
\r
112 uint32_t ObjectPropertyTableSizeInBytes;
\r
114 /* This is used to calculate the index in the dynamic object table
\r
115 (handle - 1 - nofStaticObjects = index)*/
\r
116 uint8_t NumberOfObjectsPerClass[ 4*((NCLASSES+3)/4)];
\r
118 /* Allocation size rounded up to the closest multiple of 4 */
\r
119 uint8_t NameLengthPerClass[ 4*((NCLASSES+3)/4) ];
\r
121 uint8_t TotalPropertyBytesPerClass[ 4*((NCLASSES+3)/4) ];
\r
123 /* Allocation size rounded up to the closest multiple of 2 */
\r
124 uint16_t StartIndexOfClass[ 2*((NCLASSES+1)/2) ];
\r
126 /* The actual handles issued, should be Initiated to all zeros */
\r
127 uint8_t objbytes[ 4*((DynObjTableSize+3)/4) ];
\r
128 } ObjectPropertyTableType;
\r
130 /* Symbol table data structure */
\r
133 /* = SYMBOL_HISTORY_TABLE_SIZE_IN_BYTES */
\r
134 uint32_t symTableSize;
\r
136 /* Entry 0 is reserved. Any reference to entry 0 implies NULL*/
\r
137 uint32_t nextFreeSymbolIndex;
\r
139 /* Size rounded up to closest multiple of 4, to avoid alignment issues*/
\r
140 uint8_t symbytes[4*((SYMBOL_TABLE_SIZE+3)/4)];
\r
142 /* Used for lookups - Up to 64 linked lists within the symbol table
\r
143 connecting all entries with the same 6 bit checksum.
\r
144 This field holds the current list heads. Should be initiated to zeros */
\r
145 uint16_t latestEntryOfChecksum[64];
\r
149 /*******************************************************************************
\r
150 * The data structures of the different events, all 4 bytes long
\r
151 ******************************************************************************/
\r
156 objectHandleType objHandle;
\r
157 uint16_t dts; /* differential timestamp - time since last event */
\r
170 objectHandleType objHandle;
\r
173 } KernelCallWithParamAndHandle;
\r
180 } KernelCallWithParam16;
\r
185 objectHandleType objHandle; /* the handle of the closed object */
\r
186 uint16_t symbolIndex; /* the name of the closed object */
\r
187 } ObjCloseNameEvent;
\r
195 } ObjClosePropEvent;
\r
201 uint16_t payload; /* the name of the user event */
\r
208 /* 8 bits extra for storing DTS, if it does not fit in ordinary event
\r
209 (this one is always MSB if used) */
\r
212 /* 16 bits extra for storing DTS, if it does not fit in ordinary event. */
\r
217 /*******************************************************************************
\r
218 * The main datastructure, read by FreeRTOS+Trace from the RAM dump
\r
219 ******************************************************************************/
\r
223 uint8_t startmarker0;
\r
224 uint8_t startmarker1;
\r
225 uint8_t startmarker2;
\r
226 uint8_t startmarker3;
\r
227 uint8_t startmarker4;
\r
228 uint8_t startmarker5;
\r
229 uint8_t startmarker6;
\r
230 uint8_t startmarker7;
\r
231 uint8_t startmarker8;
\r
232 uint8_t startmarker9;
\r
233 uint8_t startmarker10;
\r
234 uint8_t startmarker11;
\r
236 /* For FreeRTOS: 0x1AA1 */
\r
239 /* Currently 1 for v2.2.2 (0 earlier)*/
\r
240 uint8_t minor_version;
\r
242 /* This should be 0 if lower irq priority values implies higher priority
\r
243 levels, such as on ARM Cortex M. If the opposite scheme is used, i.e.,
\r
244 if higher irq priority values means higher priority, this should be 1. */
\r
245 uint8_t irq_priority_order;
\r
247 /* sizeof(RecorderDataType) - just for control */
\r
248 uint32_t filesize;
\r
250 /* Current number of events recorded */
\r
251 uint32_t numEvents;
\r
253 /* The buffer size, in number of event records */
\r
254 uint32_t maxEvents;
\r
256 /* The event buffer index, where to write the next event */
\r
257 uint32_t nextFreeIndex;
\r
259 /* 1 if the buffer is full, 0 otherwise */
\r
260 uint32_t bufferIsFull;
\r
262 /* The frequency of the clock/timer/counter used as time base */
\r
263 uint32_t frequency;
\r
265 /* The absolute timestamp of the last stored event, in the native
\r
266 timebase, modulo frequency! */
\r
267 uint32_t absTimeLastEvent;
\r
269 /* The number of seconds in total - lasts for 136 years */
\r
270 uint32_t absTimeLastEventSecond;
\r
272 /* 1 if the recorder has been started, 0 if not yet started or stopped.
\r
273 This is a 32 bit variable due to alignment issues. */
\r
274 uint32_t recorderActive;
\r
276 /* For storing a Team License key */
\r
277 uint8_t teamLicenceKey[32];
\r
279 /* 0xF0F0F0F0 - for control only */
\r
280 int32_t debugMarker0;
\r
282 /* The Object Property Table holds information about currently active
\r
283 tasks, queues, and other recorded objects. This is updated on each
\r
284 create call and includes object name and other properties. */
\r
285 ObjectPropertyTableType ObjectPropertyTable;
\r
287 /* 0xF1F1F1F1 - for control only */
\r
288 int32_t debugMarker1;
\r
290 /* The Symbol Table stores strings for User Events and is also used to
\r
291 store names of deleted objects, which still may be in the trace but no
\r
292 longer are available. */
\r
293 symbolTableType SymbolTable;
\r
295 /* For includsion of float support, and for endian detection of floats.
\r
296 The value should be (float)1 or (uint32_t)0 */
\r
297 #if (INCLUDE_FLOAT_SUPPORT == 1)
\r
298 float exampleFloatEncoding;
\r
300 uint32_t exampleFloatEncoding;
\r
302 /* This is non-zero if an internal error occured in the recorder, e.g., if
\r
303 one of the Nxxx constants was too small. The systemInfo string will then
\r
304 contain an error message that is displayed when attempting to view the
\r
306 uint32_t internalErrorOccured;
\r
308 /* 0xF2F2F2F2 - for control only */
\r
309 int32_t debugMarker2;
\r
311 /* Generic system information string, presented in the tool. Note that this
\r
312 is also used for storing any internal error messages from the recorder, so
\r
313 do not make TRACE_DESCRIPTION_MAX_LENGTH too small. 80 is recommended. */
\r
314 char systemInfo[TRACE_DESCRIPTION_MAX_LENGTH];
\r
316 /* 0xF3F3F3F3 - for control only */
\r
317 int32_t debugMarker3;
\r
319 /* The event data, in 4-byte records */
\r
320 uint8_t eventData[ EVENT_BUFFER_SIZE * 4 ];
\r
322 uint8_t endmarker0;
\r
323 uint8_t endmarker1;
\r
324 uint8_t endmarker2;
\r
325 uint8_t endmarker3;
\r
326 uint8_t endmarker4;
\r
327 uint8_t endmarker5;
\r
328 uint8_t endmarker6;
\r
329 uint8_t endmarker7;
\r
330 uint8_t endmarker8;
\r
331 uint8_t endmarker9;
\r
332 uint8_t endmarker10;
\r
333 uint8_t endmarker11;
\r
335 } RecorderDataType;
\r
337 extern RecorderDataType* RecorderDataPtr;
\r
339 /******************************************************************************
\r
340 * ObjectHandleStack
\r
341 * This data-structure is used to provide a mechanism for 1-byte trace object
\r
342 * handles. This way, only 1 byte is necessary instead of 4 bytes (a pointer)
\r
343 * when storing a reference to an object. This allows for up to 255 objects of
\r
344 * each object class - Task, ISR, Semaphore, CountingSemaphore, Mutex and Queue,
\r
345 * active at any given moment. There can be more "historic" objects, that have
\r
346 * been deleted - that number is only limited by the size of the symbol table.
\r
347 * Note that handle zero (0) is not used, it is a code for an invalid handle.
\r
349 * This data structure keeps track of the FREE handles, not the handles in use.
\r
350 * This datastructure contains one stack per object class. When a handle is
\r
351 * allocated to an object, the next free handle is popped from the stack. When
\r
352 * a handle is released (on object delete), it is pushed back on the stack.
\r
353 * Note that there is no initialization code that pushed the free handles
\r
354 * initially, that is not necessary due to the following optimization:
\r
356 * The stack of handles (objectHandles) is initially all zeros. Since zero
\r
357 * is not a valid handle, that is a signal of additional handles needed.
\r
358 * If a zero is received when popping a new handle, it is replaced by the
\r
359 * index of the popped handle instead.
\r
361 *****************************************************************************/
\r
364 /* For each object class, the index of the next handle to allocate */
\r
365 int16_t indexOfNextAvailableHandle[ NCLASSES ];
\r
367 /* The lowest index of this class (constant) */
\r
368 int16_t lowestIndexOfClass[ NCLASSES ];
\r
370 /* The highest index of this class (constant) */
\r
371 int16_t highestIndexOfClass[ NCLASSES ];
\r
373 /* The highest use count for this class (for statistics) */
\r
374 int16_t handleCountWaterMarksOfClass[ NCLASSES ];
\r
376 /* The free object handles - a set of stacks within this array */
\r
377 objectHandleType objectHandles[ NTask+NISR+NSemaphore+NMutex+NQueue ];
\r
379 } objectHandleStackType;
\r
381 /* Internal data */
\r
383 extern objectHandleStackType objectHandleStacks;
\r
385 extern uint8_t taskFlags[NTask];
\r
387 /* Internal functions */
\r
389 uint32_t prvTraceGetDTS(uint32_t param_maxDTS);
\r
391 void prvTraceGetChecksum(const char *pname, uint8_t* pcrc, uint8_t* plength);
\r
393 traceLabel prvTraceCreateSymbolTableEntry(const char* name,
\r
396 traceLabel channel);
\r
398 traceLabel prvTraceLookupSymbolTableEntry(const char* name,
\r
401 traceLabel channel);
\r
403 traceLabel prvTraceOpenSymbol(const char* name, traceLabel userEventChannel);
\r
405 void prvTraceUpdateCounters(void);
\r
407 void prvCheckDataToBeOverwrittenForMultiEntryUserEvents(uint32_t nEntries);
\r
409 objectHandleType xTraceGetObjectHandle(traceObjectClass objectclass);
\r
411 void vTraceFreeObjectHandle(traceObjectClass objectclass,
\r
412 objectHandleType handle);
\r
414 void vTraceSetObjectName(traceObjectClass objectclass,
\r
415 objectHandleType handle,
\r
418 void* xTraceNextFreeEventBufferSlot(void);
\r
420 uint32_t uiIndexOfObject(objectHandleType objecthandle,
\r
421 uint8_t objectclass);
\r
424 /*******************************************************************************
\r
427 * Called by various parts in the recorder. Stops the recorder and stores a
\r
428 * pointer to an error message, which is printed by the monitor task.
\r
429 ******************************************************************************/
\r
430 void vTraceError(char* msg);
\r
432 /*******************************************************************************
\r
433 * xTraceGetLastError
\r
435 * Gives the last error message, if any. NULL if no error message is stored.
\r
436 * The message is cleared on read.
\r
437 ******************************************************************************/
\r
438 char* xTraceGetLastError(void);
\r
440 /*******************************************************************************
\r
441 * xTraceInitTraceData
\r
443 * Allocates and initializes the recorder datastructure, based on the constants
\r
444 * in trcConfig.h. This allows for allocating the data on the heap, instead of
\r
445 * using a static declaration.
\r
446 ******************************************************************************/
\r
447 RecorderDataType* xTraceInitTraceData(void);
\r
449 /* Internal macros */
\r
451 #define PROPERTY_NAME_GET(objectclass, objecthandle) \
\r
452 (const char*)(& RecorderDataPtr->ObjectPropertyTable.objbytes \
\r
453 [uiIndexOfObject(objecthandle, objectclass)])
\r
455 #define PROPERTY_OBJECT_STATE(objectclass, handle) \
\r
456 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \
\r
457 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass]]
\r
459 #define PROPERTY_ACTOR_PRIORITY(objectclass, handle) \
\r
460 RecorderDataPtr->ObjectPropertyTable.objbytes[uiIndexOfObject(handle, objectclass) \
\r
461 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[objectclass] + 1]
\r
463 #define PROPERTY_TASK_IFE_SERVICECODE(handle) \
\r
464 RecorderDataPtr->ObjectPropertyTable.objbytes \
\r
465 [uiIndexOfObject(handle, TRACE_CLASS_TASK) \
\r
466 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[TRACE_CLASS_TASK]+2]
\r
468 #define PROPERTY_TASK_IFE_OBJHANDLE(handle) \
\r
469 RecorderDataPtr->ObjectPropertyTable.objbytes \
\r
470 [uiIndexOfObject(handle, TRACE_CLASS_TASK) \
\r
471 + RecorderDataPtr->ObjectPropertyTable.NameLengthPerClass[TRACE_CLASS_TASK]+3]
\r
473 #define TASK_FLAG_BITMASK_ExcludeTaskFromTrace 1
\r
474 #define TASK_FLAG_BITMASK_MarkNextEventAsTaskInstanceFinish 2
\r
476 #define SET_TASK_FLAG_ISEXCLUDED(taskHandle) taskFlags[taskHandle] |= 0x01
\r
477 #define CLEAR_TASK_FLAG_ISEXCLUDED(taskHandle) taskFlags[taskHandle] &= 0xFE
\r
478 #define GET_TASK_FLAG_ISEXCLUDED(taskHandle) (taskFlags[taskHandle] & 0x01)
\r
480 #define SET_TASK_FLAG_MARKIFE(taskHandle) taskFlags[taskHandle] |= 0x02
\r
481 #define CLEAR_TASK_FLAG_MARKIFE(taskHandle) taskFlags[taskHandle] &= 0xFD
\r
482 #define GET_TASK_FLAG_MARKIFE(taskHandle) (taskFlags[taskHandle] & 0x02)
\r
485 /* For debug printouts - the names of the object classes */
\r
486 extern char OBJECTCLASSNAME[NCLASSES][10];
\r