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
|
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation */
/* Copyright (c) 2026 Eclipse ThreadX contributors */
/* */
/* This program and the accompanying materials are made available under */
/* the terms of the MIT License which is available at */
/* https://opensource.org/licenses/MIT. */
/* */
/* SPDX-License-Identifier: MIT */
/***************************************************************************/
/* This test is designed to test trace functionality in ThreadX. */
#include <stdio.h>
#include "tx_api.h"
#define TX_SOURCE_CODE
#include "tx_trace.h"
/* Define the ISR dispatch. */
extern VOID (*test_isr_dispatch)(void);
extern UINT _tx_trace_interrupt_control(UINT new_posture);
extern VOID _tx_trace_object_register(UCHAR object_type, VOID *object_ptr, CHAR *object_name, ULONG parameter_1, ULONG parameter_2);
extern VOID _tx_trace_object_unregister(VOID *object_ptr);
static unsigned long thread_0_counter = 0;
static unsigned long thread_1_counter = 0;
static unsigned long thread_2_counter = 0;
static TX_THREAD thread_0;
static TX_THREAD thread_1;
static TX_THREAD thread_2;
static TX_TIMER timer_0;
static TX_BLOCK_POOL block_pool_0;
static TX_BYTE_POOL byte_pool_0;
static TX_EVENT_FLAGS_GROUP group_0;
static TX_MUTEX mutex_0;
static TX_QUEUE queue_0;
static TX_SEMAPHORE semaphore_0;
/* Define a bunch of semaphores for loading up the trace buffer. */
static TX_SEMAPHORE semaphore_1;
static TX_SEMAPHORE semaphore_2;
static TX_SEMAPHORE semaphore_3;
static TX_SEMAPHORE semaphore_4;
static TX_SEMAPHORE semaphore_5;
static TX_SEMAPHORE semaphore_6;
static TX_SEMAPHORE semaphore_7;
static TX_SEMAPHORE semaphore_8;
static TX_SEMAPHORE semaphore_9;
static TX_SEMAPHORE semaphore_10;
static TX_SEMAPHORE semaphore_11;
static TX_SEMAPHORE semaphore_12;
static TX_SEMAPHORE semaphore_13;
static TX_SEMAPHORE semaphore_14;
static TX_SEMAPHORE semaphore_15;
static TX_SEMAPHORE semaphore_16;
static unsigned long error = 0;
static unsigned long full_buffer = 0;
static void *save_pointer;
#if defined(TX_WIN32_MEMORY_SIZE) || defined(TX_LINUX_MEMORY_SIZE)
static FILE *trace_dump_file;
static CHAR trace_dump_file_name[]= "tracedump000.trx";
#endif
/* Define task prototypes. */
static void thread_0_entry(ULONG task_input);
static void thread_1_entry(ULONG task_input);
static void thread_2_entry(ULONG task_input);
/* Define the trace buffer. */
UCHAR trace_buffer[16384];
/* Prototype for test control return. */
void test_control_return(UINT status);
UINT _tx_thread_interrupt_control(UINT new_posture);
void trace_dump(void)
{
#if defined(TX_WIN32_MEMORY_SIZE) || defined(TX_LINUX_MEMORY_SIZE)
/* Win32 automatic dump! */
UINT old_interrupt;
/* Disable interrupts. */
old_interrupt = _tx_thread_interrupt_control(TX_INT_DISABLE);
/* If win32, we can actually dump the file! */
trace_dump_file = fopen(trace_dump_file_name, "wb+");
fwrite(trace_buffer, 1, sizeof(trace_buffer), trace_dump_file);
fclose(trace_dump_file);
/* Restore interrupts. */
_tx_thread_interrupt_control(old_interrupt);
/* Prepare for next file dump. */
trace_dump_file_name[11]++;
if (trace_dump_file_name[11] > '9')
{
trace_dump_file_name[11] = '0';
trace_dump_file_name[10]++;
if (trace_dump_file_name[10] > '9')
{
trace_dump_file_name[10] = '0';
trace_dump_file_name[9]++;
if (trace_dump_file_name[9] > '9')
trace_dump_file_name[9] = '0';
}
}
#endif
}
/* Define the timer for this test. */
static void timer_entry(ULONG i)
{
/* Resume thread 1. */
tx_thread_resume(&thread_1);
}
/* Define the ISR dispatch routine. */
static void test_isr(void)
{
/* Make ISR entry event. */
tx_trace_isr_enter_insert(1);
/* Resume thread 2. */
tx_thread_resume(&thread_2);
/* Make ISR entry event. */
tx_trace_isr_exit_insert(1);
}
/* Define the trace buffer full notify function. */
static VOID trace_buffer_full(void *ptr)
{
/* Dump trace file! */
trace_dump();
full_buffer++;
}
/* Define what the initial system looks like. */
#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void threadx_trace_basic_application_define(void *first_unused_memory)
#endif
{
INT status;
CHAR *pointer;
/* Setup a pointer. */
pointer = (CHAR *) first_unused_memory;
/* Adjust it forward just to make sure there is some space for the test below. */
pointer = pointer + 200;
/* Create a bunch of objects before being enabled. */
/* Create a timer for the test. */
save_pointer = (void *) pointer;
tx_timer_create(&timer_0, "timer 0", timer_entry, 0, 2, 2, TX_AUTO_ACTIVATE);
tx_block_pool_create(&block_pool_0, "block pool 0", 20, pointer, 100);
pointer = pointer + 100;
tx_byte_pool_create(&byte_pool_0, "byte pool 0", pointer, 1000);
pointer = pointer + 1000;
tx_event_flags_create(&group_0, "event flags group 0");
tx_mutex_create(&mutex_0, "mutex 0", TX_NO_INHERIT);
tx_queue_create(&queue_0, "queue 0", 16, pointer, 400);
pointer = pointer + 400;
tx_semaphore_create(&semaphore_0, "semaphore 0", 1);
/* Enable event tracing. */
_tx_trace_initialize();
status = tx_trace_enable(trace_buffer, sizeof(trace_buffer), 16);
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_SUCCESS)
{
printf("Running Trace Basic Test............................................ ERROR #1\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
printf("Running Trace Basic Test............................................ ERROR #2\n");
test_control_return(1);
}
#endif
/* Put system definition stuff in here, e.g. thread creates and other assorted
create information. */
status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
pointer, TEST_STACK_SIZE_PRINTF,
16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
pointer = pointer + TEST_STACK_SIZE_PRINTF;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Trace Basic Test............................................ ERROR #3\n");
test_control_return(1);
}
status = tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
pointer, TEST_STACK_SIZE_PRINTF,
15, 15, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + TEST_STACK_SIZE_PRINTF;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Trace Basic Test............................................ ERROR #4\n");
test_control_return(1);
}
status = tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
pointer, TEST_STACK_SIZE_PRINTF,
14, 14, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + TEST_STACK_SIZE_PRINTF;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Trace Basic Test............................................ ERROR #5\n");
test_control_return(1);
}
}
/* Define the test threads. */
static void thread_0_entry(ULONG thread_input)
{
UINT status;
UINT old_interrupt;
CHAR *pointer;
TX_INTERRUPT_SAVE_AREA
ULONG object;
/* Coverage for build without TraceX enabled. */
tx_saved_posture = _tx_trace_interrupt_control(TX_INT_DISABLE);
_tx_trace_interrupt_control(tx_saved_posture);
#ifndef TX_ENABLE_EVENT_TRACE
_tx_trace_object_register(0, TX_NULL, TX_NULL, 0, 0);
_tx_trace_object_register(1, TX_NULL, TX_NULL, 0, 0);
_tx_trace_object_register(1, &object, TX_NULL, 0, 0);
_tx_trace_object_register(1, &object, "fake name", 0, 0);
_tx_trace_object_register(1, &object, "fake name", 1, 0);
#endif
_tx_trace_object_register(1, &object, "fake name", 1, 1);
_tx_trace_object_unregister(TX_NULL);
_tx_trace_object_unregister(&object);
/* Increment thread 0 counter. */
thread_0_counter++;
/* Inform user of success getting to this test. */
printf("Running Trace Basic Test............................................ ");
/* Delete the additional objects. */
tx_timer_delete(&timer_0);
tx_block_pool_delete(&block_pool_0);
tx_byte_pool_delete(&byte_pool_0);
tx_event_flags_delete(&group_0);
tx_mutex_delete(&mutex_0);
tx_queue_delete(&queue_0);
tx_semaphore_delete(&semaphore_0);
/* Create all the semaphores. */
tx_semaphore_create(&semaphore_1, "semaphore 1", 1);
tx_semaphore_create(&semaphore_2, "semaphore 2", 1);
tx_semaphore_create(&semaphore_3, "semaphore 3", 1);
tx_semaphore_create(&semaphore_4, "semaphore 4", 1);
tx_semaphore_create(&semaphore_5, "semaphore 5", 1);
tx_semaphore_create(&semaphore_6, "semaphore 6", 1);
tx_semaphore_create(&semaphore_7, "semaphore 7", 1);
tx_semaphore_create(&semaphore_8, "semaphore 8", 1);
tx_semaphore_create(&semaphore_9, "semaphore 9", 1);
tx_semaphore_create(&semaphore_10, "semaphore 10", 1);
tx_semaphore_create(&semaphore_11, "semaphore 11", 1);
tx_semaphore_create(&semaphore_12, "semaphore 12", 1);
tx_semaphore_create(&semaphore_13, "semaphore 13", 1);
tx_semaphore_create(&semaphore_14, "semaphore 14", 1);
tx_semaphore_create(&semaphore_15, "semaphore 15", 1);
tx_semaphore_create(&semaphore_16, "semaphore 16", 1);
/* Delete them all. */
tx_semaphore_delete(&semaphore_1);
tx_semaphore_delete(&semaphore_2);
tx_semaphore_delete(&semaphore_3);
tx_semaphore_delete(&semaphore_4);
tx_semaphore_delete(&semaphore_5);
tx_semaphore_delete(&semaphore_6);
tx_semaphore_delete(&semaphore_7);
tx_semaphore_delete(&semaphore_8);
tx_semaphore_delete(&semaphore_9);
tx_semaphore_delete(&semaphore_10);
tx_semaphore_delete(&semaphore_11);
tx_semaphore_delete(&semaphore_12);
tx_semaphore_delete(&semaphore_13);
tx_semaphore_delete(&semaphore_14);
tx_semaphore_delete(&semaphore_15);
tx_semaphore_delete(&semaphore_16);
/* Create one over again. */
tx_semaphore_create(&semaphore_1, "semaphore 1", 1);
/* Now, create them all again. */
pointer = (CHAR *) save_pointer;
tx_block_pool_create(&block_pool_0, "block pool 0", 20, pointer, 100);
pointer = pointer + 100;
tx_byte_pool_create(&byte_pool_0, "byte pool 0", pointer, 1000);
pointer = pointer + 1000;
tx_event_flags_create(&group_0, "event flags group 0");
tx_mutex_create(&mutex_0, "mutex 0", TX_NO_INHERIT);
tx_queue_create(&queue_0, "queue 0", 16, pointer, 400);
pointer = pointer + 400;
tx_semaphore_create(&semaphore_0, "semaphore 0", 1);
/* Attempt to enable event tracing again. */
status = tx_trace_enable(trace_buffer, sizeof(trace_buffer), 8);
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_NOT_DONE)
{
printf("ERROR #6\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
printf("ERROR #7\n");
test_control_return(1);
}
/* Attempt to enable event tracing again. */
status = tx_trace_enable(TX_NULL, 0, 8);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
printf("ERROR #8\n");
test_control_return(1);
}
/* Attempt to enable event tracing again. */
status = tx_trace_enable(TX_NULL, 1, 0);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
printf("ERROR #9\n");
test_control_return(1);
}
/* Attempt to enable event tracing again. */
status = tx_trace_enable(TX_NULL, 1, 8);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
printf("ERROR #10\n");
test_control_return(1);
}
#endif
/* Filter all events. */
status = tx_trace_event_filter(TX_TRACE_ALL_EVENTS);
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_SUCCESS)
{
/* Trace error. */
printf("ERROR #11\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #12\n");
test_control_return(1);
}
/* Filter all events. */
status = tx_trace_event_filter(0);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #13\n");
test_control_return(1);
}
#endif
/* Unfilter all events. */
status = tx_trace_event_unfilter(TX_TRACE_ALL_EVENTS);
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_SUCCESS)
{
/* Trace error. */
printf("ERROR #14\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #15\n");
test_control_return(1);
}
/* Unfilter all events. */
status = tx_trace_event_unfilter(0);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #16\n");
test_control_return(1);
}
#endif
/* Register the trace buffer full notification routine. */
status = tx_trace_buffer_full_notify(trace_buffer_full);
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_SUCCESS)
{
/* Trace error. */
printf("ERROR #17\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #18\n");
test_control_return(1);
}
/* Check the NULL path with trace disabled. */
status = tx_trace_buffer_full_notify(TX_NULL);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #19\n");
test_control_return(1);
}
#endif
/* Create a timer for the test. */
tx_timer_create(&timer_0, "timer 0", timer_entry, 0, 2, 2, TX_AUTO_ACTIVATE);
/* Setup the ISR. */
test_isr_dispatch = test_isr;
while (full_buffer < 40)
{
if (full_buffer < 20)
{
/* Disable interrupts. */
old_interrupt = tx_interrupt_control(TX_INT_DISABLE);
/* Restore interrupts. */
tx_interrupt_control(old_interrupt);
}
/* Insert user event. */
status = tx_trace_user_event_insert(1027, 1, 2, 3, 4);
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_SUCCESS)
{
/* Trace error. */
printf("ERROR #20\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #21\n");
test_control_return(1);
}
/* Insert user event. */
status = tx_trace_user_event_insert(0, 1, 2, 3, 4);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #22\n");
test_control_return(1);
}
/* Insert user event. */
status = tx_trace_user_event_insert(0, 0, 2, 3, 4);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #23\n");
test_control_return(1);
}
/* Insert user event. */
status = tx_trace_user_event_insert(0, 0, 0, 3, 4);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #24\n");
test_control_return(1);
}
/* Insert user event. */
status = tx_trace_user_event_insert(0, 0, 0, 0, 4);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #25\n");
test_control_return(1);
}
/* Insert user event. */
status = tx_trace_user_event_insert(0, 0, 0, 0, 0);
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #26\n");
test_control_return(1);
}
break;
#endif
}
/* Clear the ISR. */
test_isr_dispatch = TX_NULL;
/* Disable the trace buffer. */
status = tx_trace_disable();
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_SUCCESS)
{
/* Trace error. */
printf("ERROR #27\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #28\n");
test_control_return(1);
}
#endif
/* Attempt to disable again, just to get the TX_NOT_DONE error code. */
status = tx_trace_disable();
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_NOT_DONE)
{
/* Trace error. */
printf("ERROR #29\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #30\n");
test_control_return(1);
}
#endif
/* Attempt to enable event tracing with a bogus size. */
status = tx_trace_enable(trace_buffer, 1, 8);
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_SIZE_ERROR)
{
/* Trace error. */
printf("ERROR #31\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #32\n");
test_control_return(1);
}
#endif
/* Attempt to make an user event when tracing is not enabled. */
status = tx_trace_user_event_insert(1027, 1, 2, 3, 4);
#ifdef TX_ENABLE_EVENT_TRACE
/* Check status. */
if (status != TX_NOT_DONE)
{
/* Trace error. */
printf("ERROR #33\n");
test_control_return(1);
}
#else
/* Check status. */
if (status != TX_FEATURE_NOT_ENABLED)
{
/* Trace error. */
printf("ERROR #34\n");
test_control_return(1);
}
#endif
/* Check status. */
if (error)
{
/* Trace error. */
printf("ERROR #35\n");
test_control_return(1);
}
else
{
/* Successful test. */
printf("SUCCESS!\n");
test_control_return(0);
}
}
static void thread_1_entry(ULONG task_input)
{
while(1)
{
thread_1_counter++;
tx_thread_suspend(&thread_1);
}
}
static void thread_2_entry(ULONG task_input)
{
while(1)
{
thread_2_counter++;
tx_thread_suspend(&thread_2);
}
}
|