• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fcntl.h>
19 #include <pthread.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <test.h>
25 #include <unistd.h>
26 
27 #define BUFFER_LEN 10240
28 #define EXPECT_TRUE(c)                \
29     do                                \
30     {                                 \
31         if (!(c))                     \
32             t_error("[%s] failed\n"); \
33     } while (0)
34 #define EXPECT_FALSE(c)                \
35     do                                 \
36     {                                  \
37         if ((c))                       \
38             t_error("[%s] failed \n"); \
39     } while (0)
40 
41 typedef void (*TEST_FUN)(void);
42 static const int WAIT_TIME = 1;
43 static const int count = 10;
44 
45 /**
46  * @tc.name      : trace_marker
47  * @tc.desc      : Test trace_marker_begin and trace_marker_end.
48  * @tc.level     : Level 0
49  */
50 
trace_marker_0010(void)51 static void trace_marker_0010(void)
52 {
53     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
54     trace_marker_begin("Musl_Trace_Marker_0100", "");
55     trace_marker_end();
56     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
57 
58     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
59     if (trace_fd == -1) {
60         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
61         if (trace_fd == -1) {
62             return;
63         }
64     }
65     bool trace_sucess = false;
66     char buffer[BUFFER_LEN] = {0};
67     char buf_begin[BUFFER_LEN] = {0};
68     char buf_end[BUFFER_LEN] = {0};
69 
70     int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Musl_Trace_Marker_0100");
71     if (buf_begin_fd < 0) {
72         close(trace_fd);
73         return;
74     }
75 
76     int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
77     if (buf_end_fd < 0) {
78         close(trace_fd);
79         return;
80     }
81     for (int i = 0; i < count; i++) {
82         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
83         if (read_fd == -1) {
84             close(trace_fd);
85             return;
86         }
87         if (strstr(buffer, buf_begin) != NULL && strstr(buffer, buf_end) != NULL) {
88             trace_sucess = true;
89             break;
90         }
91     }
92     EXPECT_TRUE(trace_sucess);
93     close(trace_fd);
94 }
95 
96 /**
97  * @tc.name      : trace_marker_async
98  * @tc.desc      : Test trace_marker_async_begin and trace_marker_async_end.
99  * @tc.level     : Level 0
100  */
trace_marker_0020(void)101 static void trace_marker_0020(void)
102 {
103     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
104     trace_marker_async_begin("async_begin_0200", "trace_async",1);
105     trace_marker_async_end("async_end_0200", "trace_async",1);
106     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
107 
108     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
109     if (trace_fd == -1) {
110         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
111         if (trace_fd == -1) {
112             return;
113         }
114     }
115 
116     bool trace_async_sucess = false;
117     char buffer[BUFFER_LEN] = {0};
118     char buf_async_begin[BUFFER_LEN] = {0};
119     char buf_async_end[BUFFER_LEN] = {0};
120     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "async_begin_0200", "trace_async" , 1);
121     if (buf_async_begin_fd < 0) {
122         close(trace_fd);
123         return;
124     }
125 
126     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "async_end_0200", "trace_async" , 1);
127     if (buf_async_end_fd < 0) {
128         close(trace_fd);
129         return;
130     }
131     for (int i = 0; i < count; i++) {
132         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
133         if (read_fd == -1) {
134             close(trace_fd);
135             return;
136         }
137         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
138             trace_async_sucess = true;
139             break;
140         }
141     }
142     EXPECT_TRUE(trace_async_sucess);
143     close(trace_fd);
144 }
145 
146 /**
147  * @tc.name      : trace_marker
148  * @tc.desc      : Test trace_marker_begin and trace_marker_end.
149  * @tc.level     : Level 0
150  */
trace_marker_0030(void)151 static void trace_marker_0030(void)
152 {
153     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
154     int traceCount = 5;
155     trace_marker_count("traceCount", traceCount);
156     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
157 
158     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
159     if (trace_fd == -1) {
160         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
161         if (trace_fd == -1) {
162             return;
163         }
164     }
165 
166     bool trace_count_sucess = false;
167     char buffer[BUFFER_LEN] = {0};
168     char buf_count[BUFFER_LEN] = {0};
169 
170     int buf_begin_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount", traceCount);
171     if (buf_begin_fd < 0) {
172         close(trace_fd);
173         return;
174     }
175 
176     for (int i = 0; i < count; i++) {
177         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
178         if (read_fd == -1) {
179             close(trace_fd);
180             return;
181         }
182         if (strstr(buffer, buf_count) != NULL) {
183             trace_count_sucess = true;
184             break;
185         }
186     }
187     EXPECT_TRUE(trace_count_sucess);
188     close(trace_fd);
189 }
190 
191 /**
192  * @tc.name      : trace_marker
193  * @tc.desc      : Test the multiple processes of trace_marker.
194  * @tc.level     : Level 0
195  */
trace_marker_0040(void)196 static void trace_marker_0040(void)
197 {
198     bool trace_sucess = false;
199     char buffer_fir[BUFFER_LEN] = {0};
200     char buffer_sec[BUFFER_LEN] = {0};
201     char buf_begin[BUFFER_LEN] = {0};
202     char buf_end[BUFFER_LEN] = {0};
203 
204     pid_t fpid;
205     fpid = fork();
206     if (fpid < 0) {
207         printf("error in fork! \n");
208     } else if (fpid == 0) {
209         int pidChild = getpid();
210         system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
211         trace_marker_begin("Trace_Marker0400_Forkfir", "");
212         trace_marker_end();
213         system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
214 
215         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
216         if (trace_fd == -1) {
217             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
218             if (trace_fd == -1) {
219                 return;
220             }
221         }
222         int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Trace_Marker0400_Forkfir");
223         if (buf_begin_fd < 0) {
224             close(trace_fd);
225             return;
226         }
227 
228         int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
229         if (buf_end_fd < 0) {
230             close(trace_fd);
231             return;
232         }
233         for (int i = 0; i < count; i++) {
234             int read_fd = read(trace_fd, buffer_fir, BUFFER_LEN * i);
235             if (read_fd == -1) {
236                 close(trace_fd);
237                 return;
238             }
239             if (strstr(buffer_fir, buf_begin) != NULL && strstr(buffer_fir, buf_end) != NULL) {
240                 trace_sucess = true;
241                 break;
242             }
243         }
244         EXPECT_TRUE(trace_sucess);
245         close(trace_fd);
246         exit(pidChild);
247     } else {
248         system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
249         trace_marker_begin("Trace_Marker0400_Forksec", "");
250         trace_marker_end();
251         system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
252 
253         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
254         if (trace_fd == -1) {
255             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
256             if (trace_fd == -1) {
257                 return;
258             }
259         }
260         int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Trace_Marker0400_Forksec");
261         if (buf_begin_fd < 0) {
262             close(trace_fd);
263             return;
264         }
265 
266         int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
267         if (buf_end_fd < 0) {
268             close(trace_fd);
269             return;
270         }
271         for (int i = 0; i < count; i++) {
272             int read_fd = read(trace_fd, buffer_sec, BUFFER_LEN * i);
273             if (read_fd == -1) {
274                 close(trace_fd);
275                 return;
276             }
277             if (strstr(buffer_sec, buf_begin) != NULL && strstr(buffer_sec, buf_end) != NULL) {
278                 trace_sucess = true;
279                 break;
280             }
281     }
282     EXPECT_TRUE(trace_sucess);
283     close(trace_fd);
284     }
285 }
286 
287 /**
288  * @tc.name      : trace_marker
289  * @tc.desc      : Test the multiple processes of trace_marker.
290  * @tc.level     : Level 0
291  */
trace_marker_0050(void)292 static void trace_marker_0050(void)
293 {
294     bool trace_async_sucess = false;
295     char buffer_forkFir[BUFFER_LEN] = {0};
296     char buffer_forkSec[BUFFER_LEN] = {0};
297     char buf_async_begin[BUFFER_LEN] = {0};
298     char buf_async_end[BUFFER_LEN] = {0};
299 
300     pid_t fpid;
301     fpid = fork();
302     if (fpid < 0) {
303         printf("error in fork! \n");
304     } else if (fpid == 0) {
305         int pidChild = getpid();
306         system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
307         trace_marker_async_begin("async0500_Forkfir", "begin_fir", 2);
308         trace_marker_async_end("async0500_Forkfir", "end_fir", 2);
309         system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
310 
311         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
312         if (trace_fd == -1) {
313             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
314             if (trace_fd == -1) {
315                 return;
316             }
317         }
318         int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "async0500_Forkfir", "begin_fir", 2);
319         if (buf_async_begin_fd < 0) {
320             close(trace_fd);
321             return;
322         }
323 
324         int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "async0500_Forkfir", "end_fir", 2);
325         if (buf_async_end_fd < 0) {
326             close(trace_fd);
327             return;
328         }
329         for (int i = 0; i < count; i++) {
330             int read_fd = read(trace_fd, buffer_forkFir, BUFFER_LEN * i);
331             if (read_fd == -1) {
332                 close(trace_fd);
333                 return;
334             }
335             if (strstr(buffer_forkFir, buf_async_begin) != NULL && strstr(buffer_forkFir, buf_async_end) != NULL) {
336                 trace_async_sucess = true;
337                 break;
338             }
339         }
340         EXPECT_TRUE(trace_async_sucess);
341         close(trace_fd);
342         exit(pidChild);
343     } else {
344         system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
345         trace_marker_async_begin("async0500_Forksec", "begin_sec", 3);
346         trace_marker_async_end("async0500_Forksec", "end_sec", 3);
347         system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
348 
349         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
350         if (trace_fd == -1) {
351             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
352             if (trace_fd == -1) {
353                 return;
354             }
355         }
356         int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "async0500_Forksec", "begin_sec", 3);
357         if (buf_async_begin_fd < 0) {
358             close(trace_fd);
359             return;
360         }
361 
362         int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "async0500_Forksec", "end_sec", 3);
363         if (buf_async_end_fd < 0) {
364             close(trace_fd);
365             return;
366         }
367         for (int i = 0; i < count; i++) {
368             int read_fd = read(trace_fd, buffer_forkSec, BUFFER_LEN * i);
369             if (read_fd == -1) {
370                 close(trace_fd);
371                 return;
372             }
373             if (strstr(buffer_forkSec, buf_async_begin) != NULL && strstr(buffer_forkSec, buf_async_end) != NULL) {
374                 trace_async_sucess = true;
375                 break;
376             }
377         }
378     EXPECT_TRUE(trace_async_sucess);
379     close(trace_fd);
380     }
381 }
382 
383 /**
384  * @tc.name      : trace_marker
385  * @tc.desc      : Test the multiple processes of trace_marker.
386  * @tc.level     : Level 0
387  */
trace_marker_0060(void)388 static void trace_marker_0060(void)
389 {
390     int traceCount = 5;
391     bool trace_count_sucess = false;
392     char buffer_forkFir[BUFFER_LEN] = {0};
393     char buffer_forkSec[BUFFER_LEN] = {0};
394     char buf_count[BUFFER_LEN] = {0};
395 
396     pid_t fpid;
397     fpid = fork();
398     if (fpid < 0) {
399         printf("error in fork! \n");
400     } else if (fpid == 0) {
401         int pidChild = getpid();
402 
403         system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
404         trace_marker_count("traceCount_forkfir", traceCount);
405         system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
406 
407         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
408         if (trace_fd == -1) {
409             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
410             if (trace_fd == -1) {
411                 return;
412             }
413         }
414         int buf_count_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount_forkfir", traceCount);
415         if (buf_count_fd < 0) {
416             close(trace_fd);
417             return;
418         }
419         for (int i = 0; i < count; i++) {
420             int read_fd = read(trace_fd, buffer_forkFir, BUFFER_LEN * i);
421             if (read_fd == -1) {
422                 close(trace_fd);
423                 return;
424             }
425             if (strstr(buf_count, buffer_forkFir) != NULL) {
426                 trace_count_sucess = true;
427                 break;
428             }
429         }
430         EXPECT_TRUE(trace_count_sucess);
431         close(trace_fd);
432         exit(pidChild);
433     } else {
434         system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
435         trace_marker_count("traceCount_forksec", traceCount);
436         system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
437 
438         int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
439         if (trace_fd == -1) {
440             trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY | O_APPEND);
441             if (trace_fd == -1) {
442                 return;
443             }
444         }
445         int buf_count_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount_forksec", traceCount);
446         if (buf_count_fd < 0) {
447             close(trace_fd);
448             return;
449         }
450         for (int i = 0; i < count; i++) {
451             int read_fd = read(trace_fd, buffer_forkSec, BUFFER_LEN * i);
452             if (read_fd == -1) {
453                 close(trace_fd);
454                 return;
455             }
456             if (strstr(buf_count, buffer_forkSec) != NULL) {
457                 trace_count_sucess = true;
458                 break;
459             }
460         }
461         EXPECT_TRUE(trace_count_sucess);
462         close(trace_fd);
463     }
464 }
465 
ThreadTraceMarkerFir(void * arg)466 static void *ThreadTraceMarkerFir(void *arg)
467 {
468     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
469     trace_marker_begin("Trace_Marker_Threadfir", "pthreadfir");
470     trace_marker_end();
471     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
472 
473     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
474     if (trace_fd == -1) {
475         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
476         if (trace_fd == -1) {
477             return NULL;
478         }
479     }
480     bool trace_sucess = false;
481     char buffer[BUFFER_LEN] = {0};
482     char buf_begin[BUFFER_LEN] = {0};
483     char buf_end[BUFFER_LEN] = {0};
484 
485     int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Trace_Marker_Threadfir");
486     if (buf_begin_fd < 0) {
487         close(trace_fd);
488         return NULL;
489     }
490 
491     int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
492     if (buf_end_fd < 0) {
493         close(trace_fd);
494         return NULL;
495     }
496     for (int i = 0; i < count; i++) {
497         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
498         if (read_fd == -1) {
499             close(trace_fd);
500             return NULL;
501         }
502         if (strstr(buffer, buf_begin) != NULL && strstr(buffer, buf_end) != NULL) {
503             trace_sucess = true;
504             break;
505         }
506     }
507     EXPECT_TRUE(trace_sucess);
508     close(trace_fd);
509     pthread_exit("ThreadTraceMarkerFir Exit");
510 }
511 
ThreadTraceMarkerSec(void * arg)512 static void *ThreadTraceMarkerSec(void *arg)
513 {
514     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
515     trace_marker_begin("Trace_Marker_Threadsec", "pthreadsec");
516     trace_marker_end();
517     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
518 
519     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
520     if (trace_fd == -1) {
521         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
522         if (trace_fd == -1) {
523             return NULL;
524         }
525     }
526     bool trace_sucess = false;
527     char buffer[BUFFER_LEN] = {0};
528     char buf_begin[BUFFER_LEN] = {0};
529     char buf_end[BUFFER_LEN] = {0};
530 
531     int buf_begin_fd = snprintf(buf_begin, BUFFER_LEN, "B|%d|%s", getpid(), "Trace_Marker_Threadsec");
532     if (buf_begin_fd < 0) {
533         close(trace_fd);
534         return NULL;
535     }
536 
537     int buf_end_fd = snprintf(buf_end, BUFFER_LEN, "E|%d", getpid());
538     if (buf_end_fd < 0) {
539         close(trace_fd);
540         return NULL;
541     }
542     for (int i = 0; i < count; i++) {
543         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
544         if (read_fd == -1) {
545             close(trace_fd);
546             return NULL;
547         }
548         if (strstr(buffer, buf_begin) != NULL && strstr(buffer, buf_end) != NULL) {
549             trace_sucess = true;
550             break;
551         }
552     }
553     EXPECT_TRUE(trace_sucess);
554     close(trace_fd);
555     pthread_exit("ThreadTraceMarkerSec Exit");
556 }
557 
558 /**
559  * @tc.number: trace_marker_0070
560  * @tc.name: trace_marker
561  * @tc.desc: Test the multithreading of trace_marker.
562  */
trace_marker_0070(void)563 static void trace_marker_0070(void)
564 {
565     int res;
566     const char msgThread1[1024] = {"msgThread1"};
567     const char msgThread2[1024] = {"msgThread2"};
568     pthread_t fatalMessageThread1, fatalMessageThread2;
569     res = pthread_create(&fatalMessageThread1, NULL, ThreadTraceMarkerFir, (void *)msgThread1);
570     if (res != 0) {
571         t_printf("pthread_create1 error.");
572     }
573     sleep(WAIT_TIME);
574     res = pthread_create(&fatalMessageThread2, NULL, ThreadTraceMarkerSec, (void *)msgThread2);
575     if (res != 0) {
576         t_printf("pthread_create2 error.");
577     }
578     pthread_join(fatalMessageThread1, NULL);
579     pthread_join(fatalMessageThread2, NULL);
580 }
581 
ThreadTraceMarkerAsyncFir(void * arg)582 static void *ThreadTraceMarkerAsyncFir(void *arg)
583 {
584     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
585     trace_marker_async_begin("Async_Threadfir", "begin_threadfir",4);
586     trace_marker_async_end("Async_Threadfir", "end_threadfir", 4);
587     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
588 
589     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
590     if (trace_fd == -1) {
591         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
592         if (trace_fd == -1) {
593             return NULL;
594         }
595     }
596 
597     bool trace_async_sucess = false;
598     char buffer[BUFFER_LEN] = {0};
599     char buf_async_begin[BUFFER_LEN] = {0};
600     char buf_async_end[BUFFER_LEN] = {0};
601     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "Async_Threadfir", "begin_threadfir", 4);
602     if (buf_async_begin_fd < 0) {
603         close(trace_fd);
604         return NULL;
605     }
606 
607     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "Async_Threadfir", "end_threadfir", 4);
608     if (buf_async_end_fd < 0) {
609         close(trace_fd);
610         return NULL;
611     }
612     for (int i = 0; i < count; i++) {
613         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
614         if (read_fd == -1) {
615             close(trace_fd);
616             return NULL;
617         }
618         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
619             trace_async_sucess = true;
620             break;
621         }
622     }
623     EXPECT_TRUE(trace_async_sucess);
624     close(trace_fd);
625     pthread_exit("ThreadTraceMarkerAsyncFir Exit");
626 }
627 
ThreadTraceMarkerAsyncSec(void * arg)628 static void *ThreadTraceMarkerAsyncSec(void *arg)
629 {
630     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
631     trace_marker_async_begin("Async_Threadsec", "begin_threadsec",5);
632     trace_marker_async_end("Async_Threadsec", "end_threadsec",5);
633     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
634 
635     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
636     if (trace_fd == -1) {
637         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
638         if (trace_fd == -1) {
639             return NULL;
640         }
641     }
642 
643     bool trace_async_sucess = false;
644     char buffer[BUFFER_LEN] = {0};
645     char buf_async_begin[BUFFER_LEN] = {0};
646     char buf_async_end[BUFFER_LEN] = {0};
647     int buf_async_begin_fd = snprintf(buf_async_begin, BUFFER_LEN, "S|%d|%s|%s %d", getpid(), "Async_Threadsec", "begin_threadsec", 5);
648     if (buf_async_begin_fd < 0) {
649         close(trace_fd);
650         return NULL;
651     }
652 
653     int buf_async_end_fd = snprintf(buf_async_end, BUFFER_LEN, "F|%d|%s|%s %d", getpid(), "Async_Threadsec", "end_threadsec", 5);
654     if (buf_async_end_fd < 0) {
655         close(trace_fd);
656         return NULL;
657     }
658     for (int i = 0; i < count; i++) {
659         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
660         if (read_fd == -1) {
661             close(trace_fd);
662             return NULL;
663         }
664         if (strstr(buffer, buf_async_begin) != NULL && strstr(buffer, buf_async_end) != NULL) {
665             trace_async_sucess = true;
666             break;
667         }
668     }
669     EXPECT_TRUE(trace_async_sucess);
670     close(trace_fd);
671     pthread_exit("ThreadTraceMarkerAsyncSec Exit");
672 }
673 
674 /**
675  * @tc.number: trace_marker_0080
676  * @tc.name: trace_marker
677  * @tc.desc: Test the multithreading of trace_marker.
678  */
trace_marker_0080(void)679 static void trace_marker_0080(void)
680 {
681     int res;
682     const char msgThread1[1024] = {"msgThread3"};
683     const char msgThread2[1024] = {"msgThread4"};
684     pthread_t fatalMessageThread1, fatalMessageThread2;
685     res = pthread_create(&fatalMessageThread1, NULL, ThreadTraceMarkerAsyncFir, (void *)msgThread1);
686     if (res != 0) {
687         t_printf("pthread_create3 error.");
688     }
689     sleep(WAIT_TIME);
690     res = pthread_create(&fatalMessageThread2, NULL, ThreadTraceMarkerAsyncSec, (void *)msgThread2);
691     if (res != 0) {
692         t_printf("pthread_create4 error.");
693     }
694     pthread_join(fatalMessageThread1, NULL);
695     pthread_join(fatalMessageThread2, NULL);
696 }
697 
ThreadTraceMarkerCountFir(void * arg)698 static void *ThreadTraceMarkerCountFir(void *arg)
699 {
700     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
701     int traceCount = 5;
702     trace_marker_count("traceCount_Threadfir", traceCount);
703     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
704 
705     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
706     if (trace_fd == -1) {
707         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
708         if (trace_fd == -1) {
709             return NULL;
710         }
711     }
712 
713     bool trace_count_sucess = false;
714     char buffer[BUFFER_LEN] = {0};
715     char buf_count[BUFFER_LEN] = {0};
716 
717     int buf_begin_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount_Threadfir", traceCount);
718     if (buf_begin_fd < 0) {
719         close(trace_fd);
720         return NULL;
721     }
722 
723     for (int i = 0; i < count; i++) {
724         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
725         if (read_fd == -1) {
726             close(trace_fd);
727             return NULL;
728         }
729         if (strstr(buffer, buf_count) != NULL) {
730             trace_count_sucess = true;
731             break;
732         }
733     }
734     EXPECT_TRUE(trace_count_sucess);
735     close(trace_fd);
736     pthread_exit("ThreadTraceMarkerCountFir Exit");
737 }
738 
ThreadTraceMarkerCountSec(void * arg)739 static void *ThreadTraceMarkerCountSec(void *arg)
740 {
741     system("cd /sys/kernel/debug/tracing;echo 1 > tracing_on");
742     int traceCount = 5;
743     trace_marker_count("traceCount_Threadsec", traceCount);
744     system("cd /sys/kernel/debug/tracing;echo 0 > tracing_on");
745 
746     int trace_fd = open("/sys/kernel/tracing/trace", O_CLOEXEC | O_RDONLY);
747     if (trace_fd == -1) {
748         trace_fd = open("/sys/kernel/debug/tracing/trace", O_CLOEXEC | O_RDONLY);
749         if (trace_fd == -1) {
750             return NULL;
751         }
752     }
753 
754     bool trace_count_sucess = false;
755     char buffer[BUFFER_LEN] = {0};
756     char buf_count[BUFFER_LEN] = {0};
757 
758     int buf_begin_fd = snprintf(buf_count, BUFFER_LEN, "C|%d|%s %d", getpid(), "traceCount_Threadsec", traceCount);
759     if (buf_begin_fd < 0) {
760         close(trace_fd);
761         return NULL;
762     }
763 
764     for (int i = 0; i < count; i++) {
765         int read_fd = read(trace_fd, buffer, BUFFER_LEN * i);
766         if (read_fd == -1) {
767             close(trace_fd);
768             return NULL;
769         }
770         if (strstr(buffer, buf_count) != NULL) {
771             trace_count_sucess = true;
772             break;
773         }
774     }
775     EXPECT_TRUE(trace_count_sucess);
776     close(trace_fd);
777     pthread_exit("ThreadTraceMarkerCountSec Exit");
778 }
779 /**
780  * @tc.number: trace_marker_0090
781  * @tc.name: trace_marker
782  * @tc.desc: Test the multithreading of trace_marker.
783  */
trace_marker_0090(void)784 static void trace_marker_0090(void)
785 {
786     int res;
787     const char msgThread1[1024] = {"msgThread5"};
788     const char msgThread2[1024] = {"msgThread6"};
789     pthread_t fatalMessageThread1, fatalMessageThread2;
790     res = pthread_create(&fatalMessageThread1, NULL, ThreadTraceMarkerCountFir, (void *)msgThread1);
791     if (res != 0) {
792         t_printf("pthread_create5 error.");
793     }
794     sleep(WAIT_TIME);
795     res = pthread_create(&fatalMessageThread2, NULL, ThreadTraceMarkerCountSec, (void *)msgThread2);
796     if (res != 0) {
797         t_printf("pthread_create6 error.");
798     }
799     pthread_join(fatalMessageThread1, NULL);
800     pthread_join(fatalMessageThread2, NULL);
801 }
802 
803 TEST_FUN G_Fun_Array[] = {
804     trace_marker_0010,
805     trace_marker_0020,
806     trace_marker_0030,
807     trace_marker_0040,
808     trace_marker_0050,
809     trace_marker_0060,
810     trace_marker_0070,
811     trace_marker_0080,
812     trace_marker_0090,
813     };
814 
main(void)815 int main(void)
816 {
817     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
818     for (int pos = 0; pos < num; ++pos) {
819         G_Fun_Array[pos]();
820     }
821 
822     return t_status;
823 }
824