• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/ioctl.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <sys/mman.h>
41 #include <stdint.h>
42 
43 #define TRACE_IOC_MAGIC     'T'
44 #define TRACE_START         _IO(TRACE_IOC_MAGIC, 1)
45 #define TRACE_STOP          _IO(TRACE_IOC_MAGIC, 2)
46 #define TRACE_RESET         _IO(TRACE_IOC_MAGIC, 3)
47 #define TRACE_DUMP          _IO(TRACE_IOC_MAGIC, 4)
48 #define TRACE_SET_MASK      _IO(TRACE_IOC_MAGIC, 5)
49 
50 #define TRACE_USR_MAX_PARAMS 3
51 typedef struct {
52     unsigned int eventType;
53     uintptr_t identity;
54     uintptr_t params[TRACE_USR_MAX_PARAMS];
55 } UsrEventInfo;
56 
TraceUsage(void)57 static void TraceUsage(void)
58 {
59     printf("\nUsage: ./trace [start] Start to trace events.\n");
60     printf("\nUsage: ./trace [stop] Stop trace.\n");
61     printf("\nUsage: ./trace [reset] Clear the trace record buffer.\n");
62     printf("\nUsage: ./trace [dump 0/1] Format printf trace data,"
63                 "0/1 stands for whether to send data to studio for analysis.\n");
64     printf("\nUsage: ./trace [mask num] Set trace filter event mask.\n");
65     printf("\nUsage: ./trace [read nBytes] Read nBytes raw data from trace buffer.\n");
66     printf("\nUsage: ./trace [write type id params..] Write a user event, no more than 3 parameters.\n");
67 }
68 
TraceRead(int fd,size_t size)69 static void TraceRead(int fd, size_t size)
70 {
71     ssize_t i;
72     ssize_t len;
73     if (size == 0) {
74         return;
75     }
76 
77     char *buffer = (char *)malloc(size);
78     if (buffer == NULL) {
79         printf("Read buffer malloc failed.\n");
80         return;
81     }
82 
83     len = read(fd, buffer, size);
84     for (i = 0; i < len; i++) {
85         printf("%02x ", buffer[i] & 0xFF);
86     }
87     printf("\n");
88     free(buffer);
89 }
90 
TraceWrite(int fd,int argc,char ** argv)91 static void TraceWrite(int fd, int argc, char **argv)
92 {
93     int i;
94     UsrEventInfo info = {0};
95     info.eventType = strtoul(argv[2], NULL, 0); /* 2, argv number  */
96     info.identity = strtoul(argv[3], NULL, 0); /* 3, argv number  */
97     /* 4, argc -4 means user argv that does not contain argv[0]~argv[3] */
98     int paramNum = (argc - 4) > TRACE_USR_MAX_PARAMS ? TRACE_USR_MAX_PARAMS : (argc - 4);
99 
100     for (i = 0; i < paramNum; i++) {
101         /* 4, argc -4 means user argv that does not contain argv[0]~argv[3] */
102         info.params[i] = strtoul(argv[4 + i], NULL, 0);
103     }
104     (void)write(fd, &info, sizeof(UsrEventInfo));
105 }
106 
main(int argc,char ** argv)107 int main(int argc, char **argv)
108 {
109     int fd = open("/dev/trace", O_RDWR);
110     if (fd == -1) {
111         printf("Trace open failed.\n");
112         exit(EXIT_FAILURE);
113     }
114 
115     if (argc == 1) {
116         TraceUsage();
117     } else if (argc == 2 && strcmp(argv[1], "start") == 0) { /* 2, argv num, no special meaning */
118         ioctl(fd, TRACE_START, NULL);
119     } else if (argc == 2 && strcmp(argv[1], "stop") == 0) { /* 2, argv num, no special meaning */
120         ioctl(fd, TRACE_STOP, NULL);
121     } else if (argc == 2 && strcmp(argv[1], "reset") == 0) { /* 2, argv num, no special meaning */
122         ioctl(fd, TRACE_RESET, NULL);
123     } else if (argc == 3 && strcmp(argv[1], "mask") == 0) { /* 3, argv num, no special meaning */
124         size_t mask = strtoul(argv[2], NULL, 0);
125         ioctl(fd, TRACE_SET_MASK, mask);
126     } else if (argc == 3 && strcmp(argv[1], "dump") == 0) { /* 3, argv num, no special meaning */
127         size_t flag = strtoul(argv[2], NULL, 0);
128         ioctl(fd, TRACE_DUMP, flag);
129     } else if (argc == 3 && strcmp(argv[1], "read") == 0) { /* 3, argv num, no special meaning */
130         size_t size = strtoul(argv[2], NULL, 0);
131         TraceRead(fd, size);
132     } else if (argc >= 4 && strcmp(argv[1], "write") == 0) { /* 4, argv num, no special meaning */
133         TraceWrite(fd, argc, argv);
134     } else {
135         printf("Unsupported trace command.\n");
136         TraceUsage();
137     }
138 
139     close(fd);
140     return 0;
141 }
142