1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <trace/trace_marker.h>
17 
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <pthread.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <test.h>
26 #include <unistd.h>
27 
28 #define BUFFER_LEN 10240
29 #define READ_BUFFER_SIZE 4096
30 #define OUTFILE "/data/local/tmp/musl.trace"
31 #define EXPECT_TRUE(c)                \
32     do                                \
33     {                                 \
34         if (!(c))                     \
35             t_error("[%s] failed\n"); \
36     } while (0)
37 #define EXPECT_FALSE(c)                \
38     do                                 \
39     {                                  \
40         if ((c))                       \
41             t_error("[%s] failed \n"); \
42     } while (0)
43 
44 #ifndef TRACE_TEMP_FAILURE_RETRY
45 #define TRACE_TEMP_FAILURE_RETRY(exp)          \
46     ({                                         \
47     long int _rc;                              \
48     do {                                       \
49         _rc = (long int)(exp);                 \
50     } while ((_rc == -1) && (errno == EINTR)); \
51     _rc;                                       \
52     })
53 #endif
54 #define SET_TRACE(str) \
55     do                                 \
56     {                                  \
57         system("echo "#str" > /sys/kernel/debug/tracing/tracing_on"); \
58     } while (0)
59 
60 
61 typedef void (*TEST_FUN)(void);
62 static const int WAIT_TIME = 1;
63 static const int count = 100;
64 
clear_tracenull65 static void clear_trace()
66 {
67     if (access("/sys/kernel/tracing/trace", F_OK) == 0) {
68         system("echo > /sys/kernel/tracing/trace");
69     }
70 
71     if (access("/sys/kernel/debug/tracing/trace", F_OK) == 0) {
72         system("echo > /sys/kernel/tracing/trace");
73     }
74 
75     return;
76 }
77 
dump_trace(int trace_fd)78 static void dump_trace(int trace_fd)
79 {
80     char buffer[READ_BUFFER_SIZE];
81     int nwrite;
82     int nread;
83     int out_fd = open(OUTFILE, O_WRONLY | O_CREAT, TEST_MODE);
84     if (out_fd == -1) {
85         return;
86     }
87     do {
88         nread = TRACE_TEMP_FAILURE_RETRY(read(trace_fd, buffer, READ_BUFFER_SIZE));
89         if ((nread == 0) || (nread == -1)) {
90             break;
91         }
92         nwrite = TRACE_TEMP_FAILURE_RETRY(write(out_fd, buffer, nread));
93     } while (nwrite > 0);
94     close(out_fd);
95 }
96 
97 /**
98  * @tc.name      : trace_marker
99  * @tc.desc      : Test trace_marker_begin and trace_marker_end.
100  * @tc.level     : Level 0
101  */
trace_marker_0010(void)102 static void trace_marker_0010(void)
103 {
104     clear_trace();
105     SET_TRACE(1);
106     trace_marker_begin(HITRACE_TAG_MUSL, "Musl_Trace_Marker_0010", "");
107     trace_marker_end(HITRACE_TAG_MUSL);
108     SET_TRACE(0);
109 
110     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
111     if (trace_fd == -1) {
112         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
113         if (trace_fd == -1) {
114             return;
115         }
116     }
117     bool trace_sucess = false;
118     char buffer[BUFFER_LEN] = {0};
119     char buf_begin[BUFFER_LEN] = {0};
120     char buf_end[BUFFER_LEN] = {0};
121 
122     int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Musl_Trace_Marker_0010");
123     if (buf_begin_fd < 0) {
124         close(trace_fd);
125         return;
126     }
127 
128     int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
129     if (buf_end_fd < 0) {
130         close(trace_fd);
131         return;
132     }
133     for (int i = 0; i < count; i++) {
134         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
135         if (read_fd == -1) {
136             close(trace_fd);
137             return;
138         }
139         if (strstr(buffer, buf_begin) != NULL && strstr(buffer, buf_end) != NULL) {
140             trace_sucess = true;
141             break;
142         }
143     }
144     EXPECT_TRUE(trace_sucess);
145     close(trace_fd);
146 }
147 
148 /**
149  * @tc.name      : trace_marker_async
150  * @tc.desc      : Test trace_marker_async_begin and trace_marker_async_end.
151  * @tc.level     : Level 0
152  */
trace_marker_0020(void)153 static void trace_marker_0020(void)
154 {
155     clear_trace();
156     SET_TRACE(1);
157     trace_marker_async_begin(HITRACE_TAG_MUSL, "async_begin_0200", "trace_async",1);
158     trace_marker_async_end(HITRACE_TAG_MUSL, "async_end_0200", "trace_async",1);
159     SET_TRACE(0);
160 
161     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
162     if (trace_fd == -1) {
163         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
164         if (trace_fd == -1) {
165             return;
166         }
167     }
168 
169     bool trace_async_sucess = false;
170     char buffer[BUFFER_LEN] = {0};
171     char buf_async_begin[BUFFER_LEN] = {0};
172     char buf_async_end[BUFFER_LEN] = {0};
173     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "async_begin_0200", "trace_async" , 1);
174     if (buf_async_begin_fd < 0) {
175         close(trace_fd);
176         return;
177     }
178 
179     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "async_end_0200", "trace_async" , 1);
180     if (buf_async_end_fd < 0) {
181         close(trace_fd);
182         return;
183     }
184     for (int i = 0; i < count; i++) {
185         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
186         if (read_fd == -1) {
187             close(trace_fd);
188             return;
189         }
190         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
191             trace_async_sucess = true;
192             break;
193         }
194     }
195     EXPECT_TRUE(trace_async_sucess);
196     close(trace_fd);
197 }
198 
199 /**
200  * @tc.name      : trace_marker
201  * @tc.desc      : Test trace_marker_begin and trace_marker_end.
202  * @tc.level     : Level 0
203  */
trace_marker_0030(void)204 static void trace_marker_0030(void)
205 {
206     clear_trace();
207     SET_TRACE(1);
208     int traceCount = 5;
209     trace_marker_count(HITRACE_TAG_MUSL, "traceCount", traceCount);
210     SET_TRACE(0);
211 
212     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
213     if (trace_fd == -1) {
214         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
215         if (trace_fd == -1) {
216             return;
217         }
218     }
219 
220     bool trace_count_sucess = false;
221     char buffer[BUFFER_LEN] = {0};
222     char buf_count[BUFFER_LEN] = {0};
223 
224     int buf_begin_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount", traceCount);
225     if (buf_begin_fd < 0) {
226         close(trace_fd);
227         return;
228     }
229 
230     for (int i = 0; i < count; i++) {
231         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
232         if (read_fd == -1) {
233             close(trace_fd);
234             return;
235         }
236         if (strstr(buffer, buf_count) != NULL) {
237             trace_count_sucess = true;
238             break;
239         }
240     }
241     EXPECT_TRUE(trace_count_sucess);
242     close(trace_fd);
243 }
244 
245 /**
246  * @tc.name      : trace_marker
247  * @tc.desc      : Test the multiple processes of trace_marker.
248  * @tc.level     : Level 0
249  */
trace_marker_0040(void)250 static void trace_marker_0040(void)
251 {
252     clear_trace();
253     bool trace_sucess = false;
254     char buffer_fir[BUFFER_LEN] = {0};
255     char buffer_sec[BUFFER_LEN] = {0};
256     char buf_begin[BUFFER_LEN] = {0};
257     char buf_end[BUFFER_LEN] = {0};
258 
259     pid_t fpid;
260     fpid = fork();
261     if (fpid < 0) {
262         printf("error in fork! \n");
263     } else if (fpid == 0) {
264         int pidChild = getpid();
265         SET_TRACE(1);
266         trace_marker_begin(HITRACE_TAG_MUSL, "Trace_Marker0400_Forkfir", "");
267         trace_marker_end(HITRACE_TAG_MUSL);
268         SET_TRACE(0);
269 
270         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
271         if (trace_fd == -1) {
272             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
273             if (trace_fd == -1) {
274                 return;
275             }
276         }
277         int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Trace_Marker0400_Forkfir");
278         if (buf_begin_fd < 0) {
279             close(trace_fd);
280             return;
281         }
282 
283         int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
284         if (buf_end_fd < 0) {
285             close(trace_fd);
286             return;
287         }
288         for (int i = 0; i < count; i++) {
289             int read_fd = read(trace_fd, buffer_fir, BUFFER_LEN * i);
290             if (read_fd == -1) {
291                 close(trace_fd);
292                 return;
293             }
294             if (strstr(buffer_fir, buf_begin) != NULL && strstr(buffer_fir, buf_end) != NULL) {
295                 trace_sucess = true;
296                 break;
297             }
298         }
299         EXPECT_TRUE(trace_sucess);
300         close(trace_fd);
301         exit(pidChild);
302     } else {
303         SET_TRACE(1);
304         trace_marker_begin(HITRACE_TAG_MUSL, "Trace_Marker0400_Forksec", "");
305         trace_marker_end(HITRACE_TAG_MUSL);
306         SET_TRACE(0);
307 
308         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
309         if (trace_fd == -1) {
310             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
311             if (trace_fd == -1) {
312                 return;
313             }
314         }
315         int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Trace_Marker0400_Forksec");
316         if (buf_begin_fd < 0) {
317             close(trace_fd);
318             return;
319         }
320 
321         int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
322         if (buf_end_fd < 0) {
323             close(trace_fd);
324             return;
325         }
326         for (int i = 0; i < count; i++) {
327             int read_fd = read(trace_fd, buffer_sec, BUFFER_LEN * i);
328             if (read_fd == -1) {
329                 close(trace_fd);
330                 return;
331             }
332             if (strstr(buffer_sec, buf_begin) != NULL && strstr(buffer_sec, buf_end) != NULL) {
333                 trace_sucess = true;
334                 break;
335             }
336     }
337     EXPECT_TRUE(trace_sucess);
338     close(trace_fd);
339     }
340 }
341 
342 /**
343  * @tc.name      : trace_marker
344  * @tc.desc      : Test the multiple processes of trace_marker.
345  * @tc.level     : Level 0
346  */
trace_marker_0050(void)347 static void trace_marker_0050(void)
348 {
349     clear_trace();
350     bool trace_async_sucess = false;
351     char buffer_forkFir[BUFFER_LEN] = {0};
352     char buffer_forkSec[BUFFER_LEN] = {0};
353     char buf_async_begin[BUFFER_LEN] = {0};
354     char buf_async_end[BUFFER_LEN] = {0};
355 
356     pid_t fpid;
357     fpid = fork();
358     if (fpid < 0) {
359         printf("error in fork! \n");
360     } else if (fpid == 0) {
361         int pidChild = getpid();
362         SET_TRACE(1);
363         trace_marker_async_begin(HITRACE_TAG_MUSL, "async0500_Forkfir", "begin_fir", 2);
364         trace_marker_async_end(HITRACE_TAG_MUSL, "async0500_Forkfir", "end_fir", 2);
365         SET_TRACE(0);
366 
367         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
368         if (trace_fd == -1) {
369             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
370             if (trace_fd == -1) {
371                 return;
372             }
373         }
374         int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "async0500_Forkfir", "begin_fir", 2);
375         if (buf_async_begin_fd < 0) {
376             close(trace_fd);
377             return;
378         }
379 
380         int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "async0500_Forkfir", "end_fir", 2);
381         if (buf_async_end_fd < 0) {
382             close(trace_fd);
383             return;
384         }
385         for (int i = 0; i < count; i++) {
386             int read_fd = read(trace_fd, buffer_forkFir, BUFFER_LEN * i);
387             if (read_fd == -1) {
388                 close(trace_fd);
389                 return;
390             }
391             if (strstr(buffer_forkFir, buf_async_begin) != NULL && strstr(buffer_forkFir, buf_async_end) != NULL) {
392                 trace_async_sucess = true;
393                 break;
394             }
395         }
396         EXPECT_TRUE(trace_async_sucess);
397         close(trace_fd);
398         exit(pidChild);
399     } else {
400         SET_TRACE(1);
401         trace_marker_async_begin(HITRACE_TAG_MUSL, "async0500_Forksec", "begin_sec", 3);
402         trace_marker_async_end(HITRACE_TAG_MUSL, "async0500_Forksec", "end_sec", 3);
403         SET_TRACE(0);
404 
405         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
406         if (trace_fd == -1) {
407             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
408             if (trace_fd == -1) {
409                 return;
410             }
411         }
412         int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "async0500_Forksec", "begin_sec", 3);
413         if (buf_async_begin_fd < 0) {
414             close(trace_fd);
415             return;
416         }
417 
418         int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "async0500_Forksec", "end_sec", 3);
419         if (buf_async_end_fd < 0) {
420             close(trace_fd);
421             return;
422         }
423         for (int i = 0; i < count; i++) {
424             int read_fd = read(trace_fd, buffer_forkSec, BUFFER_LEN * i);
425             if (read_fd == -1) {
426                 close(trace_fd);
427                 return;
428             }
429             if (strstr(buffer_forkSec, buf_async_begin) != NULL && strstr(buffer_forkSec, buf_async_end) != NULL) {
430                 trace_async_sucess = true;
431                 break;
432             }
433         }
434     EXPECT_TRUE(trace_async_sucess);
435     close(trace_fd);
436     }
437 }
438 
439 /**
440  * @tc.name      : trace_marker
441  * @tc.desc      : Test the multiple processes of trace_marker.
442  * @tc.level     : Level 0
443  */
trace_marker_0060(void)444 static void trace_marker_0060(void)
445 {
446     clear_trace();
447     int traceCount = 5;
448     bool trace_count_sucess = false;
449     char buffer_forkFir[BUFFER_LEN] = {0};
450     char buffer_forkSec[BUFFER_LEN] = {0};
451     char buf_count[BUFFER_LEN] = {0};
452 
453     pid_t fpid;
454     fpid = fork();
455     if (fpid < 0) {
456         printf("error in fork! \n");
457     } else if (fpid == 0) {
458         int pidChild = getpid();
459 
460         SET_TRACE(1);
461         trace_marker_count(HITRACE_TAG_MUSL, "traceCount_forkfir", traceCount);
462         SET_TRACE(0);
463 
464         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
465         if (trace_fd == -1) {
466             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
467             if (trace_fd == -1) {
468                 return;
469             }
470         }
471         int buf_count_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount_forkfir", traceCount);
472         if (buf_count_fd < 0) {
473             close(trace_fd);
474             return;
475         }
476         for (int i = 0; i < count; i++) {
477             int read_fd = read(trace_fd, buffer_forkFir, BUFFER_LEN * i);
478             if (read_fd == -1) {
479                 close(trace_fd);
480                 return;
481             }
482             if (strstr(buf_count, buffer_forkFir) != NULL) {
483                 trace_count_sucess = true;
484                 break;
485             }
486         }
487         EXPECT_TRUE(trace_count_sucess);
488         close(trace_fd);
489         exit(pidChild);
490     } else {
491         SET_TRACE(1);
492         trace_marker_count(HITRACE_TAG_MUSL, "traceCount_forksec", traceCount);
493         SET_TRACE(0);
494 
495         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
496         if (trace_fd == -1) {
497             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
498             if (trace_fd == -1) {
499                 return;
500             }
501         }
502         int buf_count_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount_forksec", traceCount);
503         if (buf_count_fd < 0) {
504             close(trace_fd);
505             return;
506         }
507         for (int i = 0; i < count; i++) {
508             int read_fd = read(trace_fd, buffer_forkSec, BUFFER_LEN * i);
509             if (read_fd == -1) {
510                 close(trace_fd);
511                 return;
512             }
513             if (strstr(buf_count, buffer_forkSec) != NULL) {
514                 trace_count_sucess = true;
515                 break;
516             }
517         }
518         EXPECT_TRUE(trace_count_sucess);
519         close(trace_fd);
520     }
521 }
522 
ThreadTraceMarkerFir(void *arg)523 static void *ThreadTraceMarkerFir(void *arg)
524 {
525     trace_marker_begin(HITRACE_TAG_MUSL, "Trace_Marker_Threadfir", "pthreadfir");
526     trace_marker_end(HITRACE_TAG_MUSL);
527 
528     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
529     if (trace_fd == -1) {
530         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
531         if (trace_fd == -1) {
532             return NULL;
533         }
534     }
535     bool trace_sucess = false;
536     char buffer[BUFFER_LEN] = {0};
537     char buf_begin[BUFFER_LEN] = {0};
538     char buf_end[BUFFER_LEN] = {0};
539 
540     int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Trace_Marker_Threadfir");
541     if (buf_begin_fd < 0) {
542         close(trace_fd);
543         return NULL;
544     }
545 
546     int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
547     if (buf_end_fd < 0) {
548         close(trace_fd);
549         return NULL;
550     }
551     for (int i = 0; i < count; i++) {
552         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
553         if (read_fd == -1) {
554             close(trace_fd);
555             return NULL;
556         }
557         if (strstr(buffer, buf_begin) != NULL && strstr(buffer, buf_end) != NULL) {
558             trace_sucess = true;
559             break;
560         }
561     }
562     EXPECT_TRUE(trace_sucess);
563     close(trace_fd);
564     pthread_exit("ThreadTraceMarkerFir Exit");
565 }
566 
ThreadTraceMarkerSec(void *arg)567 static void *ThreadTraceMarkerSec(void *arg)
568 {
569     trace_marker_begin(HITRACE_TAG_MUSL, "Trace_Marker_Threadsec", "pthreadsec");
570     trace_marker_end(HITRACE_TAG_MUSL);
571 
572     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
573     if (trace_fd == -1) {
574         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
575         if (trace_fd == -1) {
576             return NULL;
577         }
578     }
579     bool trace_sucess = false;
580     char buffer[BUFFER_LEN] = {0};
581     char buf_begin[BUFFER_LEN] = {0};
582     char buf_end[BUFFER_LEN] = {0};
583 
584     int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Trace_Marker_Threadsec");
585     if (buf_begin_fd < 0) {
586         close(trace_fd);
587         return NULL;
588     }
589 
590     int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
591     if (buf_end_fd < 0) {
592         close(trace_fd);
593         return NULL;
594     }
595     for (int i = 0; i < count; i++) {
596         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
597         if (read_fd == -1) {
598             close(trace_fd);
599             return NULL;
600         }
601         if (strstr(buffer, buf_begin) != NULL && strstr(buffer, buf_end) != NULL) {
602             trace_sucess = true;
603             break;
604         }
605     }
606     EXPECT_TRUE(trace_sucess);
607     close(trace_fd);
608     pthread_exit("ThreadTraceMarkerSec Exit");
609 }
610 
611 /**
612  * @tc.number: trace_marker_0070
613  * @tc.name: trace_marker
614  * @tc.desc: Test the multithreading of trace_marker.
615  */
trace_marker_0070(void)616 static void trace_marker_0070(void)
617 {
618     SET_TRACE(1);
619     clear_trace();
620     int res;
621     const char msgThread1[1024] = {"msgThread1"};
622     const char msgThread2[1024] = {"msgThread2"};
623     pthread_t fatalMessageThread1, fatalMessageThread2;
624     res = pthread_create(&fatalMessageThread1, NULL, ThreadTraceMarkerFir, (void *)msgThread1);
625     if (res != 0) {
626         t_printf("pthread_create1 error.");
627     }
628     res = pthread_create(&fatalMessageThread2, NULL, ThreadTraceMarkerSec, (void *)msgThread2);
629     if (res != 0) {
630         t_printf("pthread_create2 error.");
631     }
632     pthread_join(fatalMessageThread1, NULL);
633     pthread_join(fatalMessageThread2, NULL);
634     SET_TRACE(0);
635 }
636 
ThreadTraceMarkerAsyncFir(void *arg)637 static void *ThreadTraceMarkerAsyncFir(void *arg)
638 {
639     trace_marker_async_begin(HITRACE_TAG_MUSL, "Async_Threadfir", "begin_threadfir",4);
640     trace_marker_async_end(HITRACE_TAG_MUSL, "Async_Threadfir", "end_threadfir", 4);
641 
642     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
643     if (trace_fd == -1) {
644         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
645         if (trace_fd == -1) {
646             return NULL;
647         }
648     }
649 
650     bool trace_async_sucess = false;
651     char buffer[BUFFER_LEN] = {0};
652     char buf_async_begin[BUFFER_LEN] = {0};
653     char buf_async_end[BUFFER_LEN] = {0};
654     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "Async_Threadfir", "begin_threadfir", 4);
655     if (buf_async_begin_fd < 0) {
656         close(trace_fd);
657         return NULL;
658     }
659 
660     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "Async_Threadfir", "end_threadfir", 4);
661     if (buf_async_end_fd < 0) {
662         close(trace_fd);
663         return NULL;
664     }
665     for (int i = 0; i < count; i++) {
666         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
667         if (read_fd == -1) {
668             close(trace_fd);
669             return NULL;
670         }
671         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
672             trace_async_sucess = true;
673             break;
674         }
675     }
676     EXPECT_TRUE(trace_async_sucess);
677     close(trace_fd);
678     pthread_exit("ThreadTraceMarkerAsyncFir Exit");
679 }
680 
ThreadTraceMarkerAsyncSec(void *arg)681 static void *ThreadTraceMarkerAsyncSec(void *arg)
682 {
683     trace_marker_async_begin(HITRACE_TAG_MUSL, "Async_Threadsec", "begin_threadsec",5);
684     trace_marker_async_end(HITRACE_TAG_MUSL, "Async_Threadsec", "end_threadsec",5);
685 
686     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
687     if (trace_fd == -1) {
688         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
689         if (trace_fd == -1) {
690             return NULL;
691         }
692     }
693 
694     bool trace_async_sucess = false;
695     char buffer[BUFFER_LEN] = {0};
696     char buf_async_begin[BUFFER_LEN] = {0};
697     char buf_async_end[BUFFER_LEN] = {0};
698     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "Async_Threadsec", "begin_threadsec", 5);
699     if (buf_async_begin_fd < 0) {
700         close(trace_fd);
701         return NULL;
702     }
703 
704     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "Async_Threadsec", "end_threadsec", 5);
705     if (buf_async_end_fd < 0) {
706         close(trace_fd);
707         return NULL;
708     }
709     for (int i = 0; i < count; i++) {
710         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
711         if (read_fd == -1) {
712             close(trace_fd);
713             return NULL;
714         }
715         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
716             trace_async_sucess = true;
717             break;
718         }
719     }
720     EXPECT_TRUE(trace_async_sucess);
721     close(trace_fd);
722     pthread_exit("ThreadTraceMarkerAsyncSec Exit");
723 }
724 
725 /**
726  * @tc.number: trace_marker_0080
727  * @tc.name: trace_marker
728  * @tc.desc: Test the multithreading of trace_marker.
729  */
trace_marker_0080(void)730 static void trace_marker_0080(void)
731 {
732     SET_TRACE(1);
733     clear_trace();
734     int res;
735     const char msgThread1[1024] = {"msgThread3"};
736     const char msgThread2[1024] = {"msgThread4"};
737     pthread_t fatalMessageThread1, fatalMessageThread2;
738     res = pthread_create(&fatalMessageThread1, NULL, ThreadTraceMarkerAsyncFir, (void *)msgThread1);
739     if (res != 0) {
740         t_printf("pthread_create3 error.");
741     }
742     res = pthread_create(&fatalMessageThread2, NULL, ThreadTraceMarkerAsyncSec, (void *)msgThread2);
743     if (res != 0) {
744         t_printf("pthread_create4 error.");
745     }
746     pthread_join(fatalMessageThread1, NULL);
747     pthread_join(fatalMessageThread2, NULL);
748     SET_TRACE(0);
749 }
750 
ThreadTraceMarkerCountFir(void *arg)751 static void *ThreadTraceMarkerCountFir(void *arg)
752 {
753     int traceCount = 5;
754     trace_marker_count(HITRACE_TAG_MUSL, "traceCount_Threadfir", traceCount);
755 
756     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
757     if (trace_fd == -1) {
758         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
759         if (trace_fd == -1) {
760             return NULL;
761         }
762     }
763 
764     bool trace_count_sucess = false;
765     char buffer[BUFFER_LEN] = {0};
766     char buf_count[BUFFER_LEN] = {0};
767 
768     int buf_begin_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount_Threadfir", traceCount);
769     if (buf_begin_fd < 0) {
770         close(trace_fd);
771         return NULL;
772     }
773 
774     for (int i = 0; i < count; i++) {
775         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
776         if (read_fd == -1) {
777             close(trace_fd);
778             return NULL;
779         }
780         if (strstr(buffer, buf_count) != NULL) {
781             trace_count_sucess = true;
782             break;
783         }
784     }
785     EXPECT_TRUE(trace_count_sucess);
786     close(trace_fd);
787     pthread_exit("ThreadTraceMarkerCountFir Exit");
788 }
789 
ThreadTraceMarkerCountSec(void *arg)790 static void *ThreadTraceMarkerCountSec(void *arg)
791 {
792     int traceCount = 5;
793     trace_marker_count(HITRACE_TAG_MUSL, "traceCount_Threadsec", traceCount);
794 
795     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
796     if (trace_fd == -1) {
797         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
798         if (trace_fd == -1) {
799             return NULL;
800         }
801     }
802 
803     bool trace_count_sucess = false;
804     char buffer[BUFFER_LEN] = {0};
805     char buf_count[BUFFER_LEN] = {0};
806 
807     int buf_begin_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount_Threadsec", traceCount);
808     if (buf_begin_fd < 0) {
809         close(trace_fd);
810         return NULL;
811     }
812 
813     for (int i = 0; i < count; i++) {
814         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
815         if (read_fd == -1) {
816             close(trace_fd);
817             return NULL;
818         }
819         if (strstr(buffer, buf_count) != NULL) {
820             trace_count_sucess = true;
821             break;
822         }
823     }
824     EXPECT_TRUE(trace_count_sucess);
825     close(trace_fd);
826     pthread_exit("ThreadTraceMarkerCountSec Exit");
827 }
828 /**
829  * @tc.number: trace_marker_0090
830  * @tc.name: trace_marker
831  * @tc.desc: Test the multithreading of trace_marker.
832  */
trace_marker_0090(void)833 static void trace_marker_0090(void)
834 {
835     SET_TRACE(1);
836     clear_trace();
837     int res;
838     const char msgThread1[1024] = {"msgThread5"};
839     const char msgThread2[1024] = {"msgThread6"};
840     pthread_t fatalMessageThread1, fatalMessageThread2;
841     res = pthread_create(&fatalMessageThread1, NULL, ThreadTraceMarkerCountFir, (void *)msgThread1);
842     if (res != 0) {
843         t_printf("pthread_create5 error.");
844     }
845     res = pthread_create(&fatalMessageThread2, NULL, ThreadTraceMarkerCountSec, (void *)msgThread2);
846     if (res != 0) {
847         t_printf("pthread_create6 error.");
848     }
849     pthread_join(fatalMessageThread1, NULL);
850     pthread_join(fatalMessageThread2, NULL);
851     SET_TRACE(0);
852 }
853 
854 /**
855  * @tc.name      : trace_marker
856  * @tc.desc      : Test trace_marker_begin and trace_marker_end, value is null.
857  * @tc.level     : Level 2
858  */
trace_marker_0100(void)859 static void trace_marker_0100(void)
860 {
861     clear_trace();
862     SET_TRACE(1);
863     trace_marker_begin(HITRACE_TAG_MUSL, "Musl_Trace_Marker_0100", NULL);
864     trace_marker_end(HITRACE_TAG_MUSL);
865     SET_TRACE(0);
866 
867     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
868     if (trace_fd == -1) {
869         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
870         if (trace_fd == -1) {
871             return;
872         }
873     }
874     bool trace_sucess = false;
875     char buffer[BUFFER_LEN] = {0};
876     char buf_begin[BUFFER_LEN] = {0};
877     char buf_end[BUFFER_LEN] = {0};
878 
879     int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Musl_Trace_Marker_0100");
880     if (buf_begin_fd < 0) {
881         close(trace_fd);
882         return;
883     }
884 
885      int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
886     if (buf_end_fd < 0) {
887         close(trace_fd);
888         return;
889     }
890     for (int i = 0; i < count; i++) {
891         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
892         if (read_fd == -1) {
893             close(trace_fd);
894             return;
895         }
896         if (strstr(buffer, buf_begin) != NULL && strstr(buffer, buf_end) != NULL) {
897             trace_sucess = true;
898             break;
899         }
900 
901     }
902     EXPECT_TRUE(trace_sucess);
903     close(trace_fd);
904 }
905 
906 /**
907  * @tc.name      : trace_marker
908  * @tc.desc      : Test trace_marker_begin and trace_marker_end, the size of message is 1026.
909  * @tc.level     : Level 2
910  */
trace_marker_0110(void)911 static void trace_marker_0110(void)
912 {
913     clear_trace();
914     char message[1026]= {0};
915     memset(message, 1, 1025);
916     message[1025] = '\0';
917 
918     SET_TRACE(1);
919     trace_marker_begin(HITRACE_TAG_MUSL, message, "");
920     trace_marker_end(HITRACE_TAG_MUSL);
921     SET_TRACE(0);
922 
923     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
924     if (trace_fd == -1) {
925         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
926         if (trace_fd == -1) {
927             return;
928         }
929     }
930     bool trace_sucess = true;
931     char buffer[BUFFER_LEN] = {0};
932     char buf_begin[BUFFER_LEN] = {0};
933     char buf_end[BUFFER_LEN] = {0};
934 
935     int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), message);
936     if (buf_begin_fd < 0) {
937         close(trace_fd);
938         return;
939     }
940 
941      int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
942     if (buf_end_fd < 0) {
943         close(trace_fd);
944         return;
945     }
946     for (int i = 0; i < count; i++) {
947         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
948         if (read_fd == -1) {
949             close(trace_fd);
950             return;
951         }
952         if (strstr(buffer, buf_begin) != NULL && strstr(buffer, buf_end) != NULL) {
953             trace_sucess = false;
954             break;
955         }
956 
957     }
958 
959     EXPECT_TRUE(trace_sucess);
960     close(trace_fd);
961 }
962 
963 /**
964  * @tc.name      : trace_marker
965  * @tc.desc      : Test trace_marker_begin and trace_marker_end, message is null.
966  * @tc.level     : Level 2
967  */
trace_marker_0120(void)968 static void trace_marker_0120(void)
969 {
970     clear_trace();
971     SET_TRACE(1);
972     trace_marker_begin(HITRACE_TAG_MUSL, NULL, "");
973     trace_marker_end(HITRACE_TAG_MUSL);
974     SET_TRACE(0);
975 
976     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
977     if (trace_fd == -1) {
978         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
979         if (trace_fd == -1) {
980             return;
981         }
982     }
983     bool trace_sucess = false;
984     char buffer[BUFFER_LEN] = {0};
985     char buf_end[BUFFER_LEN] = {0};
986 
987     int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
988     if (buf_end_fd < 0) {
989         close(trace_fd);
990         return;
991     }
992     for (int i = 0; i < count; i++) {
993         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
994         if (read_fd == -1) {
995             close(trace_fd);
996             return;
997         }
998         if (strstr(buffer, buf_end) != NULL) {
999             trace_sucess = true;
1000             break;
1001         }
1002     }
1003     EXPECT_TRUE(trace_sucess);
1004     close(trace_fd);
1005 }
1006 
1007 /**
1008  * @tc.name      : trace_marker
1009  * @tc.desc      : Test trace_marker_async_begin and trace_marker_async_end, the value is null.
1010  * @tc.level     : Level 2
1011  */
trace_marker_0140(void)1012 static void trace_marker_0140(void)
1013 {
1014     clear_trace();
1015     SET_TRACE(1);
1016     trace_marker_async_begin(HITRACE_TAG_MUSL, "async_begin_0200", NULL,1);
1017     trace_marker_async_end(HITRACE_TAG_MUSL, "async_end_0200", NULL,1);
1018     SET_TRACE(0);
1019 
1020     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
1021     if (trace_fd == -1) {
1022         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
1023         if (trace_fd == -1) {
1024             return;
1025         }
1026     }
1027 
1028     bool trace_async_sucess = false;
1029     char buffer[BUFFER_LEN] = {0};
1030     char buf_async_begin[BUFFER_LEN] = {0};
1031     char buf_async_end[BUFFER_LEN] = {0};
1032 
1033     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s %d", getpid(), "async_begin_0200",1);
1034     if (buf_async_begin_fd < 0) {
1035         close(trace_fd);
1036         return;
1037     }
1038 
1039     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s %d", getpid(), "async_end_0200" ,1);
1040     if (buf_async_end_fd < 0) {
1041         close(trace_fd);
1042         return;
1043     }
1044     for (int i = 0; i < count; i++) {
1045         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
1046         if (read_fd == -1) {
1047             close(trace_fd);
1048             return;
1049         }
1050         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
1051             trace_async_sucess = true;
1052             break;
1053         }
1054     }
1055     EXPECT_TRUE(trace_async_sucess);
1056     close(trace_fd);
1057 }
1058 
1059 /**
1060  * @tc.name      : trace_marker
1061  * @tc.desc      : Test trace_marker_async_begin and trace_marker_async_end, the size of message
1062  *                 is 1026.
1063  * @tc.level     : Level 2
1064  */
trace_marker_0150(void)1065 static void trace_marker_0150(void)
1066 {
1067     clear_trace();
1068     char message[1026]= {0};
1069     memset(message, 1, 1025);
1070     message[1025] = '\0';
1071 
1072     SET_TRACE(1);
1073     trace_marker_async_begin(HITRACE_TAG_MUSL, message, "trace_async",1);
1074     trace_marker_async_end(HITRACE_TAG_MUSL, message, "trace_async",1);
1075     SET_TRACE(0);
1076 
1077     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
1078     if (trace_fd == -1) {
1079         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
1080         if (trace_fd == -1) {
1081             return;
1082         }
1083     }
1084 
1085     bool trace_async_sucess = true;
1086     char buffer[BUFFER_LEN] = {0};
1087     char buf_async_begin[BUFFER_LEN] = {0};
1088     char buf_async_end[BUFFER_LEN] = {0};
1089     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), message, "trace_async" , 1);
1090     if (buf_async_begin_fd < 0) {
1091         close(trace_fd);
1092         return;
1093     }
1094 
1095     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), message, "trace_async" , 1);
1096     if (buf_async_end_fd < 0) {
1097         close(trace_fd);
1098         return;
1099     }
1100     for (int i = 0; i < count; i++) {
1101         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
1102         if (read_fd == -1) {
1103             close(trace_fd);
1104             return;
1105         }
1106         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
1107             trace_async_sucess = false;
1108             break;
1109         }
1110     }
1111     EXPECT_TRUE(trace_async_sucess);
1112     close(trace_fd);
1113 }
1114 
1115 /**
1116  * @tc.name      : trace_marker
1117  * @tc.desc      : Test trace_marker_async_begin and trace_marker_async_end, message is null.
1118  * @tc.level     : Level 2
1119  */
trace_marker_0160(void)1120 static void trace_marker_0160(void)
1121 {
1122     clear_trace();
1123     SET_TRACE(1);
1124     trace_marker_async_begin(HITRACE_TAG_MUSL, NULL, "trace_async",1);
1125     trace_marker_async_end(HITRACE_TAG_MUSL, NULL, "trace_async",1);
1126     SET_TRACE(0);
1127 
1128     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
1129     if (trace_fd == -1) {
1130         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
1131         if (trace_fd == -1) {
1132             return;
1133         }
1134     }
1135 
1136     bool trace_async_sucess = true;
1137     char buffer[BUFFER_LEN] = {0};
1138     char buf_async_begin[BUFFER_LEN] = {0};
1139     char buf_async_end[BUFFER_LEN] = {0};
1140     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s %d", getpid(), "trace_async" , 1);
1141     if (buf_async_begin_fd < 0) {
1142         close(trace_fd);
1143         return;
1144     }
1145 
1146     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s %d", getpid(), "trace_async" , 1);
1147     if (buf_async_end_fd < 0) {
1148         close(trace_fd);
1149         return;
1150     }
1151     for (int i = 0; i < count; i++) {
1152         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
1153         if (read_fd == -1) {
1154             close(trace_fd);
1155             return;
1156         }
1157         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
1158             trace_async_sucess = false;
1159             break;
1160         }
1161     }
1162     EXPECT_TRUE(trace_async_sucess);
1163     close(trace_fd);
1164 }
1165 
1166 /**
1167  * @tc.name      : trace_marker
1168  * @tc.desc      : Test trace_marker_count, the size of messge is 1026.
1169  * @tc.level     : Level 2
1170  */
trace_marker_0180(void)1171 static void trace_marker_0180(void)
1172 {
1173     clear_trace();
1174     SET_TRACE(1);
1175     int traceCount = 5;
1176     char message[1026]= {0};
1177     memset(message, 1, 1025);
1178     message[1025] = '\0';
1179 
1180     trace_marker_count(HITRACE_TAG_MUSL, message, traceCount);
1181     SET_TRACE(0);
1182 
1183     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
1184     if (trace_fd == -1) {
1185         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
1186         if (trace_fd == -1) {
1187             return;
1188         }
1189     }
1190 
1191     bool trace_count_sucess = true;
1192     char buffer[BUFFER_LEN] = {0};
1193     char buf_count[BUFFER_LEN] = {0};
1194 
1195     int buf_begin_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), message, traceCount);
1196     if (buf_begin_fd < 0) {
1197         close(trace_fd);
1198         return;
1199     }
1200 
1201     for (int i = 0; i < count; i++) {
1202         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
1203         if (read_fd == -1) {
1204             close(trace_fd);
1205             return;
1206         }
1207         if (strstr(buffer, buf_count) != NULL) {
1208             trace_count_sucess = false;
1209             break;
1210         }
1211     }
1212     EXPECT_TRUE(trace_count_sucess);
1213     close(trace_fd);
1214 }
1215 
1216 /**
1217  * @tc.name      : trace_marker
1218  * @tc.desc      : Test trace_marker_count, message is null.
1219  * @tc.level     : Level 2
1220  */
trace_marker_0190(void)1221 static void trace_marker_0190(void)
1222 {
1223     clear_trace();
1224     SET_TRACE(1);
1225     int traceCount = 5;
1226     trace_marker_count(HITRACE_TAG_MUSL, NULL, traceCount);
1227     SET_TRACE(0);
1228 
1229     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
1230     if (trace_fd == -1) {
1231         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
1232         if (trace_fd == -1) {
1233             return;
1234         }
1235     }
1236 
1237     bool trace_count_sucess = true;
1238     char buffer[BUFFER_LEN] = {0};
1239     char buf_count[BUFFER_LEN] = {0};
1240     int buf_begin_fd = snprintf(buf_count, BUFFER_LEN, "C|%d %d", getpid(), traceCount);
1241     if (buf_begin_fd < 0) {
1242         close(trace_fd);
1243         return;
1244     }
1245 
1246     for (int i = 0; i < count; i++) {
1247         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
1248         if (read_fd == -1) {
1249             close(trace_fd);
1250             return;
1251         }
1252         if (strstr(buffer, buf_count) != NULL) {
1253             trace_count_sucess = false;
1254             break;
1255         }
1256     }
1257     EXPECT_TRUE(trace_count_sucess);
1258     close(trace_fd);
1259 }
1260 
1261 TEST_FUN G_Fun_Array[] = {
1262     trace_marker_0010,
1263     trace_marker_0020,
1264     trace_marker_0030,
1265     trace_marker_0040,
1266     trace_marker_0050,
1267     trace_marker_0060,
1268     trace_marker_0070,
1269     trace_marker_0080,
1270     trace_marker_0090,
1271     trace_marker_0100,
1272     trace_marker_0110,
1273     trace_marker_0120,
1274     trace_marker_0140,
1275     trace_marker_0150,
1276     trace_marker_0160,
1277     trace_marker_0180,
1278     trace_marker_0190,
1279 };
1280 
main(void)1281 int main(void)
1282 {
1283     trace_marker_reset();
1284     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
1285     for (int pos = 0; pos < num; ++pos) {
1286         G_Fun_Array[pos]();
1287     }
1288 
1289     return t_status;
1290 }