forked from percepio/TraceRecorderSource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trcSnapshotRecorder.c
3282 lines (2805 loc) · 108 KB
/
trcSnapshotRecorder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Trace Recorder for Tracealyzer v4.6.3
* Copyright 2021 Percepio AB
* www.percepio.com
*
* SPDX-License-Identifier: Apache-2.0
*
* The generic core of the trace recorder's snapshot mode.
*/
#include <trcRecorder.h>
#if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_SNAPSHOT)
#if (TRC_USE_TRACEALYZER_RECORDER == 1)
#include <string.h>
#include <stdarg.h>
#ifndef TRC_CFG_RECORDER_DATA_INIT
#define TRC_CFG_RECORDER_DATA_INIT 1
#endif
#if ((TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_INCR) || (TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_DECR))
#error "CUSTOM timestamping mode is not (yet) supported in snapshot mode!"
#endif
/* DO NOT CHANGE */
#define TRACE_MINOR_VERSION 7
/* Keeps track of the task's stack low mark */
typedef struct {
void* tcb;
uint32_t uiPreviousLowMark;
} TaskStackMonitorEntry_t;
TraceKernelPortDataBuffer_t xKernelPortDataBuffer;
#if (TRC_CFG_INCLUDE_ISR_TRACING == 1)
/*******************************************************************************
* isrstack
*
* Keeps track of nested interrupts.
******************************************************************************/
static traceHandle isrstack[TRC_CFG_MAX_ISR_NESTING];
/*******************************************************************************
* isPendingContextSwitch
*
* Used to indicate if there is a pending context switch.
* If there is a pending context switch the recorder will not create an event
* when returning from the ISR.
******************************************************************************/
int32_t isPendingContextSwitch = 0;
#endif /* (TRC_CFG_INCLUDE_ISR_TRACING == 1) */
/*******************************************************************************
* readyEventsEnabled
*
* This can be used to dynamically disable ready events.
******************************************************************************/
#if !defined TRC_CFG_INCLUDE_READY_EVENTS || TRC_CFG_INCLUDE_READY_EVENTS == 1
static int readyEventsEnabled = 1;
#endif /*!defined TRC_CFG_INCLUDE_READY_EVENTS || TRC_CFG_INCLUDE_READY_EVENTS == 1*/
/*******************************************************************************
* uiTraceTickCount
*
* This variable is should be updated by the Kernel tick interrupt. This does
* not need to be modified when developing a new timer port. It is preferred to
* keep any timer port changes in the HWTC macro definitions, which typically
* give sufficient flexibility.
******************************************************************************/
uint32_t uiTraceTickCount = 0;
/*******************************************************************************
* trace_disable_timestamp
*
* This can be used to disable timestamps as it will cause
* prvTracePortGetTimeStamp() to return the previous timestamp.
******************************************************************************/
uint32_t trace_disable_timestamp = 0;
/*******************************************************************************
* last_timestamp
*
* The most recent timestamp.
******************************************************************************/
static uint32_t last_timestamp = 0;
/*******************************************************************************
* uiTraceSystemState
*
* Indicates if we are currently performing a context switch or just running application code.
******************************************************************************/
volatile uint32_t uiTraceSystemState = TRC_STATE_IN_STARTUP;
/*******************************************************************************
* recorder_busy
*
* Flag that shows if inside a critical section of the recorder.
******************************************************************************/
volatile int recorder_busy = 0;
/*******************************************************************************
* timestampFrequency
*
* Holds the value set by vTraceSetFrequency.
******************************************************************************/
uint32_t timestampFrequency = 0;
/*******************************************************************************
* nISRactive
*
* The number of currently active (including preempted) ISRs.
******************************************************************************/
int8_t nISRactive = 0;
/*******************************************************************************
* handle_of_last_logged_task
*
* The current task.
******************************************************************************/
traceHandle handle_of_last_logged_task = 0;
/*******************************************************************************
* vTraceStopHookPtr
*
* Called when the recorder is stopped, set by vTraceSetStopHook.
******************************************************************************/
TRACE_STOP_HOOK vTraceStopHookPtr = (TRACE_STOP_HOOK)0;
/*******************************************************************************
* init_hwtc_count
*
* Initial TRC_HWTC_COUNT value, for detecting if the time-stamping source is
* enabled. If using the OS periodic timer for time-stamping, this might not
* have been configured on the earliest events during the startup.
******************************************************************************/
uint32_t init_hwtc_count;
/*******************************************************************************
* CurrentFilterMask
*
* The filter mask that will be checked against each object's FilterGroup to see
* if they should be included in the trace or not.
******************************************************************************/
uint16_t CurrentFilterMask TRC_CFG_RECORDER_DATA_ATTRIBUTE;
/*******************************************************************************
* CurrentFilterGroup
*
* The current filter group that will be assigned to newly created objects.
******************************************************************************/
uint16_t CurrentFilterGroup TRC_CFG_RECORDER_DATA_ATTRIBUTE;
/*******************************************************************************
* objectHandleStacks
*
* A set of stacks that keeps track of available object handles for each class.
* The stacks are empty initially, meaning that allocation of new handles will be
* based on a counter (for each object class). Any delete operation will
* return the handle to the corresponding stack, for reuse on the next allocate.
******************************************************************************/
objectHandleStackType objectHandleStacks TRC_CFG_RECORDER_DATA_ATTRIBUTE;
/*******************************************************************************
* traceErrorMessage
*
* The last error message of the recorder. NULL if no error message.
******************************************************************************/
const char* traceErrorMessage TRC_CFG_RECORDER_DATA_ATTRIBUTE;
#if defined(TRC_CFG_ENABLE_STACK_MONITOR) && (TRC_CFG_ENABLE_STACK_MONITOR == 1) && (TRC_CFG_SCHEDULING_ONLY == 0)
/*******************************************************************************
* tasksInStackMonitor
*
* Keeps track of all stack low marks for tasks.
******************************************************************************/
TaskStackMonitorEntry_t tasksInStackMonitor[TRC_CFG_STACK_MONITOR_MAX_TASKS] TRC_CFG_RECORDER_DATA_ATTRIBUTE;
/*******************************************************************************
* tasksNotIncluded
*
* The number of tasks that did not fit in the stack monitor.
******************************************************************************/
int tasksNotIncluded TRC_CFG_RECORDER_DATA_ATTRIBUTE;
#endif /* defined(TRC_CFG_ENABLE_STACK_MONITOR) && (TRC_CFG_ENABLE_STACK_MONITOR == 1) && (TRC_CFG_SCHEDULING_ONLY == 0) */
/*******************************************************************************
* RecorderData
*
* The main data structure in snapshot mode, when using the default static memory
* allocation (TRC_RECORDER_BUFFER_ALLOCATION_STATIC). The recorder uses a pointer
* RecorderDataPtr to access the data, to also allow for dynamic or custom data
* allocation (see TRC_CFG_RECORDER_BUFFER_ALLOCATION).
******************************************************************************/
#if (TRC_CFG_RECORDER_BUFFER_ALLOCATION == TRC_RECORDER_BUFFER_ALLOCATION_STATIC)
RecorderDataType RecorderData TRC_CFG_RECORDER_DATA_ATTRIBUTE;
#endif
/* Pointer to the main data structure, when in snapshot mode */
RecorderDataType* RecorderDataPtr TRC_CFG_RECORDER_DATA_ATTRIBUTE;
#if (TRC_CFG_RECORDER_DATA_INIT != 0)
uint32_t RecorderInitialized = 0;
#else /* (TRC_CFG_RECORDER_DATA_INIT != 0) */
uint32_t RecorderInitialized TRC_CFG_RECORDER_DATA_ATTRIBUTE;
#endif /* (TRC_CFG_RECORDER_DATA_INIT != 0) */
/*************** Private Functions *******************************************/
static void prvStrncpy(char* dst, const char* src, uint32_t maxLength);
static uint8_t prvTraceGetObjectState(uint8_t objectclass, traceHandle id);
static void prvTraceGetChecksum(const char *pname, uint8_t* pcrc, uint8_t* plength);
static void* prvTraceNextFreeEventBufferSlot(void);
static uint16_t prvTraceGetDTS(uint16_t param_maxDTS);
static TraceStringHandle_t prvTraceOpenSymbol(const char* name, TraceStringHandle_t userEventChannel);
static void prvTraceUpdateCounters(void);
void vTraceStoreMemMangEvent(uint32_t ecode, uint32_t address, int32_t signed_size);
#if (TRC_CFG_SNAPSHOT_MODE == TRC_SNAPSHOT_MODE_RING_BUFFER)
static void prvCheckDataToBeOverwrittenForMultiEntryEvents(uint8_t nEntries);
#endif
static TraceStringHandle_t prvTraceCreateSymbolTableEntry(const char* name,
uint8_t crc6,
uint8_t len,
TraceStringHandle_t channel);
static TraceStringHandle_t prvTraceLookupSymbolTableEntry(const char* name,
uint8_t crc6,
uint8_t len,
TraceStringHandle_t channel);
#if (TRC_CFG_INCLUDE_ISR_TRACING == 0)
/* ISR tracing is turned off */
void prvTraceIncreaseISRActive(void);
void prvTraceDecreaseISRActive(void);
#endif /*(TRC_CFG_INCLUDE_ISR_TRACING == 0)*/
#if (TRC_CFG_USE_16BIT_OBJECT_HANDLES == 1)
static uint8_t prvTraceGet8BitHandle(traceHandle handle);
#else
#define prvTraceGet8BitHandle(x) ((uint8_t)x)
#endif
#if (TRC_CFG_SCHEDULING_ONLY == 0)
static uint32_t prvTraceGetParam(uint32_t, uint32_t);
#endif
/*******************************************************************************
* prvTracePortGetTimeStamp
*
* Returns the current time based on the HWTC macros which provide a hardware
* isolation layer towards the hardware timer/counter.
*
* The HWTC macros and prvTracePortGetTimeStamp is the main porting issue
* or the trace recorder library. Typically you should not need to change
* the code of prvTracePortGetTimeStamp if using the HWTC macros.
*
******************************************************************************/
void prvTracePortGetTimeStamp(uint32_t *puiTimestamp);
static void prvTraceTaskInstanceFinish(int8_t direct);
/*******************************************************************************
* prvTraceInitTimestamps
*
* This will only be called once the recorder is started, and we can assume that
* all hardware has been initialized.
******************************************************************************/
static void prvTraceInitTimestamps(void);
static void prvTraceStart(void);
static void prvTraceStop(void);
#if ((TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1))
#if (TRC_CFG_USE_SEPARATE_USER_EVENT_BUFFER == 1)
static void vTraceUBData_Helper(traceUBChannel channelPair, va_list vl);
static void prvTraceUBHelper1(traceUBChannel channel, TraceStringHandle_t eventLabel, TraceStringHandle_t formatLabel, va_list vl);
static void prvTraceUBHelper2(traceUBChannel channel, uint32_t* data, uint32_t noOfSlots);
#endif /* (TRC_CFG_USE_SEPARATE_USER_EVENT_BUFFER == 1) */
#endif /* ((TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1)) */
uint16_t uiIndexOfObject(traceHandle objecthandle, uint8_t objectclass);
/*******************************************************************************
* prvTraceError
*
* Called by various parts in the recorder. Stops the recorder and stores a
* pointer to an error message, which is printed by the monitor task.
******************************************************************************/
void prvTraceError(const char* msg);
/********* Public Functions **************************************************/
#if (TRC_CFG_RECORDER_BUFFER_ALLOCATION == TRC_RECORDER_BUFFER_ALLOCATION_CUSTOM)
traceResult xTraceSetBuffer(TraceRecorderDataBuffer_t* pxBuffer)
{
if (pxBuffer == 0)
{
return TRC_FAIL;
}
RecorderDataPtr = (RecorderDataType*)pxBuffer;
return TRC_SUCCESS;
}
#endif
traceResult xTraceGetEventBuffer(void** ppvBuffer, TraceUnsignedBaseType_t* puiSize)
{
if (ppvBuffer == 0 || puiSize == 0)
{
return TRC_FAIL;
}
*ppvBuffer = (void*)RecorderDataPtr;
*puiSize = sizeof(RecorderDataType);
return TRC_SUCCESS;
}
traceResult xTraceEnable(uint32_t uiStartOption)
{
/* Make sure recorder data is initialized */
if (xTraceInitialize() == TRC_FAIL)
{
return TRC_FAIL;
}
if (uiStartOption == TRC_START)
{
if (xTraceKernelPortEnable() == TRC_FAIL)
{
return TRC_FAIL;
}
prvTraceInitTimestamps();
prvTraceStart();
}
else if (uiStartOption == TRC_START_AWAIT_HOST)
{
prvTraceError("xTraceEnable(TRC_START_AWAIT_HOST) not allowed in Snapshot mode");
return TRC_FAIL;
}
else if (uiStartOption != TRC_START_FROM_HOST)
{
prvTraceError("xTraceEnable(TRC_START_FROM_HOST) not allowed in Snapshot mode");
return TRC_FAIL;
}
return TRC_SUCCESS;
}
traceResult xTraceDisable(void)
{
prvTraceStop();
return TRC_SUCCESS;
}
void vTraceSetStopHook(TRACE_STOP_HOOK stopHookFunction)
{
vTraceStopHookPtr = stopHookFunction;
}
void vTraceClear(void)
{
TRACE_ALLOC_CRITICAL_SECTION();
trcCRITICAL_SECTION_BEGIN();
RecorderDataPtr->absTimeLastEventSecond = 0;
RecorderDataPtr->absTimeLastEvent = 0;
RecorderDataPtr->nextFreeIndex = 0;
RecorderDataPtr->numEvents = 0;
RecorderDataPtr->bufferIsFull = 0;
traceErrorMessage = 0;
RecorderDataPtr->internalErrorOccured = 0;
(void)memset(RecorderDataPtr->eventData, 0, RecorderDataPtr->maxEvents * 4);
handle_of_last_logged_task = 0;
trcCRITICAL_SECTION_END();
}
static void prvTraceStart(void)
{
traceHandle handle;
TRACE_ALLOC_CRITICAL_SECTION();
handle = 0;
if (RecorderDataPtr == 0)
{
TRACE_ASSERT(RecorderDataPtr != 0, "Recorder not initialized. Use vTraceEnable() instead!", TRC_UNUSED);
return;
}
if (RecorderDataPtr->recorderActive == 1)
return; /* Already running */
if (traceErrorMessage == 0)
{
trcCRITICAL_SECTION_BEGIN();
RecorderDataPtr->recorderActive = 1;
handle = TRACE_GET_TASK_NUMBER(TRACE_GET_CURRENT_TASK());
if (handle == 0)
{
/* This occurs if the scheduler is not yet started.
This creates a dummy "(startup)" task entry internally in the
recorder */
handle = prvTraceGetObjectHandle(TRACE_CLASS_TASK);
prvTraceSetObjectName(TRACE_CLASS_TASK, handle, "(startup)");
prvTraceSetPriorityProperty(TRACE_CLASS_TASK, handle, 0);
}
prvTraceStoreTaskswitch(handle); /* Register the currently running task */
trcCRITICAL_SECTION_END();
}
}
/*******************************************************************************
* prvTraceStop
*
* Stops the recorder. The recording can be resumed by calling vTraceStart.
* This does not reset the recorder. Use vTraceClear if that is desired.
******************************************************************************/
static void prvTraceStop(void)
{
if (RecorderDataPtr != 0)
{
RecorderDataPtr->recorderActive = 0;
}
if (vTraceStopHookPtr != (TRACE_STOP_HOOK)0)
{
(*vTraceStopHookPtr)(); /* An application call-back function. */
}
}
/*******************************************************************************
* xTraceIsRecorderEnabled
* Returns true (1) if the recorder is enabled (i.e. is recording), otherwise 0.
******************************************************************************/
uint32_t xTraceIsRecorderEnabled(void)
{
if (RecorderInitialized == 1 && RecorderDataPtr != 0)
{
return RecorderDataPtr->recorderActive;
}
else
{
return 0;
}
}
/******************************************************************************
* xTraceIsRecorderInitialized
*
* Returns true (1) if the recorder is initialized.
******************************************************************************/
uint32_t xTraceIsRecorderInitialized(void)
{
return RecorderInitialized;
}
/*******************************************************************************
* xTraceErrorGetLast
*
* Gives the last error message, if any. NULL if no error message is stored.
* Any error message is also presented when opening a trace file.
******************************************************************************/
const char* xTraceErrorGetLast(void)
{
return traceErrorMessage;
}
/*******************************************************************************
* vTraceClearError
*
* Removes any previous error message generated by recorder calling prvTraceError.
* By calling this function, it may be possible to start/restart the trace
* despite errors in the recorder, but there is no guarantee that the trace
* recorder will work correctly in that case, depending on the type of error.
******************************************************************************/
void vTraceClearError(void)
{
traceErrorMessage = 0;
if (RecorderDataPtr != 0)
{
RecorderDataPtr->internalErrorOccured = 0;
}
}
/*******************************************************************************
* xTraceGetTraceBuffer
*
* Returns a pointer to the recorder data structure. Use this together with
* uiTraceGetTraceBufferSize if you wish to implement an own store/upload
* solution, e.g., in case a debugger connection is not available for uploading
* the data.
******************************************************************************/
void* xTraceGetTraceBuffer(void)
{
return RecorderDataPtr;
}
/*******************************************************************************
* uiTraceGetTraceBufferSize
*
* Gets the size of the recorder data structure. For use together with
* vTraceGetTraceBuffer if you wish to implement an own store/upload solution,
* e.g., in case a debugger connection is not available for uploading the data.
******************************************************************************/
uint32_t uiTraceGetTraceBufferSize(void)
{
return sizeof(RecorderDataType);
}
/*******************************************************************************
* prvTraceInitTimestamps
*
* If vTraceEnable(TRC_INIT) was called BEFORE the clock was initialized, this
* function must be called AFTER the clock is initialized to set a proper
* initial timestamp value. If vTraceEnable(...) is only called AFTER clock is
* initialized, there is no need to call this function.
******************************************************************************/
static void prvTraceInitTimestamps(void)
{
init_hwtc_count = TRC_HWTC_COUNT;
}
/******************************************************************************
* prvTraceTaskInstanceFinish
*
* Private common function for the vTraceTaskInstanceFinishXXX functions.
*****************************************************************************/
static void prvTraceTaskInstanceFinish(int8_t direct)
{
TaskInstanceStatusEvent* tis;
uint8_t dts45;
TRACE_ALLOC_CRITICAL_SECTION();
trcCRITICAL_SECTION_BEGIN();
if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
{
dts45 = (uint8_t)prvTraceGetDTS(0xFF);
tis = (TaskInstanceStatusEvent*) prvTraceNextFreeEventBufferSlot();
if (tis != 0)
{
if (direct == 0)
tis->type = TASK_INSTANCE_FINISHED_NEXT_KSE;
else
tis->type = TASK_INSTANCE_FINISHED_DIRECT;
tis->dts = dts45;
prvTraceUpdateCounters();
}
}
trcCRITICAL_SECTION_END();
}
/******************************************************************************
* xTraceTaskInstanceFinishedNext(void)
*
* Marks the current task instance as finished on the next kernel call.
*
* If that kernel call is blocking, the instance ends after the blocking event
* and the corresponding return event is then the start of the next instance.
* If the kernel call is not blocking, the viewer instead splits the current
* fragment right before the kernel call, which makes this call the first event
* of the next instance.
*
* See also TRC_CFG_USE_IMPLICIT_IFE_RULES in trcConfig.h
*
* Example:
*
* while(1)
* {
* xQueueReceive(CommandQueue, &command, timeoutDuration);
* processCommand(command);
* xTraceTaskInstanceFinishedNext();
* }
*****************************************************************************/
traceResult xTraceTaskInstanceFinishedNext(void)
{
prvTraceTaskInstanceFinish(0);
return TRC_SUCCESS;
}
/******************************************************************************
* xTraceTaskInstanceFinishedNow(void)
*
* Marks the current task instance as finished at this very instant.
* This makes the viewer to splits the current fragment at this point and begin
* a new actor instance.
*
* See also TRC_CFG_USE_IMPLICIT_IFE_RULES in trcConfig.h
*
* Example:
*
* This example will generate two instances for each loop iteration.
* The first instance ends at xTraceTaskInstanceFinishedNow(), while the second
* instance ends at the next xQueueReceive call.
*
* while (1)
* {
* xQueueReceive(CommandQueue, &command, timeoutDuration);
* ProcessCommand(command);
* xTraceTaskInstanceFinishedNow();
* DoSometingElse();
* xTraceTaskInstanceFinishedNext();
* }
*****************************************************************************/
traceResult xTraceTaskInstanceFinishedNow(void)
{
prvTraceTaskInstanceFinish(1);
return TRC_SUCCESS;
}
/*******************************************************************************
* Interrupt recording functions
******************************************************************************/
#if (TRC_CFG_INCLUDE_ISR_TRACING == 1)
traceResult xTraceISRRegister(const char* szName, uint32_t uiPriority, TraceISRHandle_t* pxISRHandle)
{
static TraceISRHandle_t xISRHandle = 0;
TRACE_ASSERT(RecorderDataPtr != 0, "Recorder not initialized, call vTraceEnable() first!", TRC_FAIL);
TRACE_ASSERT(xISRHandle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "xTraceSetISRProperties: Invalid value for handle", TRC_FAIL);
TRACE_ASSERT(szName != 0, "xTraceSetISRProperties: name == NULL", TRC_FAIL);
xISRHandle++;
prvTraceSetObjectName(TRACE_CLASS_ISR, xISRHandle, szName);
prvTraceSetPriorityProperty(TRACE_CLASS_ISR, xISRHandle, uiPriority);
*pxISRHandle = xISRHandle;
return TRC_SUCCESS;
}
traceHandle xTraceSetISRProperties(const char* szName, uint8_t uiPriority)
{
TraceISRHandle_t xISRHandle;
xTraceISRRegister(szName, uiPriority, &xISRHandle);
return xISRHandle;
}
traceResult xTraceISRBegin(TraceISRHandle_t handle)
{
TRACE_ALLOC_CRITICAL_SECTION();
if (recorder_busy)
{
/*************************************************************************
* This occurs if an ISR calls a trace function, preempting a previous
* trace call that is being processed in a different ISR or task.
* If this occurs, there is probably a problem in the definition of the
* recorder's internal critical sections (TRACE_ENTER_CRITICAL_SECTION and
* TRACE_EXIT_CRITICAL_SECTION). They must disable the RTOS tick interrupt
* and any other ISRs that calls the trace recorder directly or via
* traced kernel functions. The ARM port disables all interrupts using the
* PRIMASK register to avoid this issue.
*************************************************************************/
prvTraceError("vTraceStoreISRBegin - recorder busy! See code comment.");
return TRC_FAIL;
}
trcCRITICAL_SECTION_BEGIN();
if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
{
uint16_t dts4;
TRACE_ASSERT(handle != 0, "vTraceStoreISRBegin: Invalid ISR handle (NULL)", TRC_FAIL);
TRACE_ASSERT(handle <= RecorderDataPtr->ObjectPropertyTable.NumberOfObjectsPerClass[TRACE_CLASS_ISR], "vTraceStoreISRBegin: Invalid ISR handle (> NISR)", TRC_FAIL);
dts4 = (uint16_t)prvTraceGetDTS(0xFFFF);
if (RecorderDataPtr->recorderActive) /* Need to repeat this check! */
{
if (nISRactive < TRC_CFG_MAX_ISR_NESTING)
{
TSEvent* ts;
uint8_t hnd8 = prvTraceGet8BitHandle(handle);
isrstack[nISRactive] = handle;
nISRactive++;
ts = (TSEvent*)prvTraceNextFreeEventBufferSlot();
if (ts != 0)
{
ts->type = TS_ISR_BEGIN;
ts->dts = dts4;
ts->objHandle = hnd8;
prvTraceUpdateCounters();
}
}
else
{
/* This should not occur unless something is very wrong */
prvTraceError("Too many nested interrupts!");
}
}
}
trcCRITICAL_SECTION_END();
return TRC_SUCCESS;
}
traceResult xTraceISREnd(int pendingISR)
{
TSEvent* ts;
uint16_t dts5;
uint8_t hnd8 = 0, type = 0;
TRACE_ALLOC_CRITICAL_SECTION();
if (! RecorderDataPtr->recorderActive || ! handle_of_last_logged_task)
{
return TRC_SUCCESS;
}
if (recorder_busy)
{
/*************************************************************************
* This occurs if an ISR calls a trace function, preempting a previous
* trace call that is being processed in a different ISR or task.
* If this occurs, there is probably a problem in the definition of the
* recorder's internal critical sections (TRACE_ENTER_CRITICAL_SECTION and
* TRACE_EXIT_CRITICAL_SECTION). They must disable the RTOS tick interrupt
* and any other ISRs that calls the trace recorder directly or via
* traced kernel functions. The ARM port disables all interrupts using the
* PRIMASK register to avoid this issue.
*************************************************************************/
prvTraceError("vTraceStoreISREnd - recorder busy! See code comment.");
return TRC_FAIL;
}
if (nISRactive == 0)
{
prvTraceError("Unmatched call to vTraceStoreISREnd (nISRactive == 0, expected > 0)");
return TRC_FAIL;
}
trcCRITICAL_SECTION_BEGIN();
isPendingContextSwitch |= pendingISR; /* Is there a pending context switch right now? If so, we will not create an event since we will get an event when that context switch is executed. */
nISRactive--;
if (nISRactive > 0)
{
/* Return to another ISR */
type = TS_ISR_RESUME;
hnd8 = prvTraceGet8BitHandle(isrstack[nISRactive - 1]); /* isrstack[nISRactive] is the handle of the ISR we're currently exiting. isrstack[nISRactive - 1] is the handle of the ISR that was executing previously. */
}
else if ((isPendingContextSwitch == 0) || (xTraceKernelPortIsSchedulerSuspended()))
{
/* Return to interrupted task, if no context switch will occur in between. */
type = TS_TASK_RESUME;
hnd8 = prvTraceGet8BitHandle(handle_of_last_logged_task);
}
if (type != 0)
{
dts5 = (uint16_t)prvTraceGetDTS(0xFFFF);
ts = (TSEvent*)prvTraceNextFreeEventBufferSlot();
if (ts != 0)
{
ts->type = type;
ts->objHandle = hnd8;
ts->dts = dts5;
prvTraceUpdateCounters();
}
}
trcCRITICAL_SECTION_END();
return TRC_SUCCESS;
}
#else
/* ISR tracing is turned off */
void prvTraceIncreaseISRActive(void)
{
if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
nISRactive++;
}
void prvTraceDecreaseISRActive(void)
{
if (RecorderDataPtr->recorderActive && handle_of_last_logged_task)
nISRactive--;
}
#endif /* (TRC_CFG_INCLUDE_ISR_TRACING == 1)*/
/********************************************************************************/
/* User Event functions */
/********************************************************************************/
#define MAX_ARG_SIZE (4+32)
#if ((TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1))
static uint8_t writeInt8(void * buffer, uint8_t i, uint8_t value)
{
TRACE_ASSERT(buffer != 0, "writeInt8: buffer == NULL", 0);
if (i >= MAX_ARG_SIZE)
{
return 255;
}
((uint8_t*)buffer)[i] = value;
if (i + 1 > MAX_ARG_SIZE)
{
return 255;
}
return ((uint8_t) (i + 1));
}
#endif
#if ((TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1))
static uint8_t writeInt16(void * buffer, uint8_t i, uint16_t value)
{
TRACE_ASSERT(buffer != 0, "writeInt16: buffer == NULL", 0);
/* Align to multiple of 2 */
while ((i % 2) != 0)
{
if (i >= MAX_ARG_SIZE)
{
return 255;
}
((uint8_t*)buffer)[i] = 0;
i++;
}
if (i + 2 > MAX_ARG_SIZE)
{
return 255;
}
((uint16_t*)buffer)[i/2] = value;
return ((uint8_t) (i + 2));
}
#endif
#if ((TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1))
static uint8_t writeInt32(void * buffer, uint8_t i, uint32_t value)
{
TRACE_ASSERT(buffer != 0, "writeInt32: buffer == NULL", 0);
/* A 32 bit value should begin at an even 4-byte address */
while ((i % 4) != 0)
{
if (i >= MAX_ARG_SIZE)
{
return 255;
}
((uint8_t*)buffer)[i] = 0;
i++;
}
if (i + 4 > MAX_ARG_SIZE)
{
return 255;
}
((uint32_t*)buffer)[i/4] = value;
return ((uint8_t) (i + 4));
}
#endif
#if ((TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1) && (TRC_CFG_INCLUDE_FLOAT_SUPPORT))
static uint8_t writeFloat(void * buffer, uint8_t i, float value)
{
TRACE_ASSERT(buffer != 0, "writeFloat: buffer == NULL", 0);
/* A 32 bit value should begin at an even 4-byte address */
while ((i % 4) != 0)
{
if (i >= MAX_ARG_SIZE)
{
return 255;
}
((uint8_t*)buffer)[i] = 0;
i++;
}
if (i + 4 > MAX_ARG_SIZE)
{
return 255;
}
((float*)buffer)[i/4] = value;
return i + 4;
}
#endif
#if ((TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1) && (TRC_CFG_INCLUDE_FLOAT_SUPPORT))
static uint8_t writeDouble(void * buffer, uint8_t i, double value)
{
uint32_t * dest;
uint32_t * src = (uint32_t*)&value;
TRACE_ASSERT(buffer != 0, "writeDouble: buffer == NULL", 0);
/* The double is written as two 32 bit values, and should begin at an even
4-byte address (to avoid having to align with 8 byte) */
while (i % 4 != 0)
{
if (i >= MAX_ARG_SIZE)
{
return 255;
}
((uint8_t*)buffer)[i] = 0;
i++;
}
if (i + 8 > MAX_ARG_SIZE)
{
return 255;
}
dest = &(((uint32_t *)buffer)[i/4]);
dest[0] = src[0];
dest[1] = src[1];
return i + 8;
}
#endif
/*******************************************************************************
* prvTraceUserEventFormat
*
* Parses the format string and stores the arguments in the buffer.
******************************************************************************/
#if ((TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1))
static uint8_t prvTraceUserEventFormat(const char* formatStr, va_list vl, uint8_t* buffer, uint8_t byteOffset)
{
uint16_t formatStrIndex = 0;
uint8_t argCounter = 0;
uint8_t i = byteOffset;
while (formatStr[formatStrIndex] != '\0')
{
if (formatStr[formatStrIndex] == '%')
{
if (formatStr[formatStrIndex + 1] == '%')
{
formatStrIndex += 2;
continue;
}
/* We found a possible argument */
argCounter++;
formatStrIndex++;
while ((formatStr[formatStrIndex] >= '0' && formatStr[formatStrIndex] <= '9') || formatStr[formatStrIndex] == '#' || formatStr[formatStrIndex] == '.')
formatStrIndex++;
/* This check is necessary to avoid moving past end of string. */
if (formatStr[formatStrIndex] != '\0')
{
switch (formatStr[formatStrIndex])
{
case 'd':
i = writeInt32( buffer,
i,
(uint32_t)va_arg(vl, uint32_t));
break;
case 'x':
case 'X':
case 'u':