更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
trace.c
浏览该文件的文档.
1/*!
2 * @file trace.c
3 * @brief 用户态
4 * @link section3.2 http://weharmonyos.com/openharmony/zh-cn/device-dev/kernel/kernel-small-debug-trace.html#section3.2 @endlink
5 @verbatim
6 新增trace字符设备,位于"/dev/trace",通过对设备节点的read\write\ioctl,实现用户态trace的读写和控制:
7 read: 用户态读取Trace记录数据
8
9 write: 用户态事件写入
10
11 ioctl: 用户态Trace控制操作,包括
12 #define TRACE_IOC_MAGIC 'T'
13 #define TRACE_START _IO(TRACE_IOC_MAGIC, 1)
14 #define TRACE_STOP _IO(TRACE_IOC_MAGIC, 2)
15 #define TRACE_RESET _IO(TRACE_IOC_MAGIC, 3)
16 #define TRACE_DUMP _IO(TRACE_IOC_MAGIC, 4)
17 #define TRACE_SET_MASK _IO(TRACE_IOC_MAGIC, 5)
18 分别对应Trace启动(LOS_TraceStart)、停止(LOS_TraceStop)、清除记录(LOS_TraceReset)、
19 dump记录(LOS_TraceRecordDump)、设置事件过滤掩码(LOS_TraceEventMaskSet)
20
21 用户态开发流程
22 通过在menuconfig配置"Driver->Enable TRACE DRIVER",开启Trace驱动。该配置仅在内核Enable Trace Feature后,
23 才可在Driver的选项中可见。
24
25 打开“/dev/trace”字符文件,进行读写和IOCTL操作;
26 系统提供用户态的trace命令,该命令位于/bin目录下,cd bin 后可执行如下命令:
27 ./trace reset 清除Trace记录
28
29 ./trace mask num 设置Trace事件过滤掩码
30
31 ./trace start 启动Trace
32
33 ./trace stop 停止Trace
34
35 ./trace dump 0/1 格式化输出Trace记录
36
37 ./trace read nBytes 读取Trace记录
38
39 ./trace write type id params... 写用户态事件 如:./trace write 0x1 0x1001 0x2 0x3 则写入一条用户态事件,
40 其事件类型为0x1, id为0x1001,参数有2个,分别是0x2和0x3.
41
42 用户态命令行的典型使用方法如下:
43
44 ./trace start
45
46 ./trace write 0x1 0x1001 0x2 0x3
47
48 ./trace stop
49
50 ./trace dump 0
51 @endverbatim
52 * @version
53 * @author weharmonyos.com | 鸿蒙研究站 | 每天死磕一点点
54 * @date 2021-11-22
55 */
56/*
57 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
58 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without modification,
61 * are permitted provided that the following conditions are met:
62 *
63 * 1. Redistributions of source code must retain the above copyright notice, this list of
64 * conditions and the following disclaimer.
65 *
66 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
67 * of conditions and the following disclaimer in the documentation and/or other materials
68 * provided with the distribution.
69 *
70 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
71 * to endorse or promote products derived from this software without specific prior written
72 * permission.
73 *
74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
75 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
76 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
77 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
78 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
79 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
80 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
81 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
82 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
83 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
84 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
85 */
86
87#include <stdio.h>
88#include <stdlib.h>
89#include <string.h>
90#include <sys/types.h>
91#include <sys/stat.h>
92#include <sys/ioctl.h>
93#include <fcntl.h>
94#include <unistd.h>
95#include <sys/mman.h>
96#include <stdint.h>
97
98//分别对应Trace启动(LOS_TraceStart)、停止(LOS_TraceStop)、清除记录(LOS_TraceReset)、
99//dump记录(LOS_TraceRecordDump)、设置事件过滤掩码(LOS_TraceEventMaskSet)
100#define TRACE_IOC_MAGIC 'T'
101#define TRACE_START _IO(TRACE_IOC_MAGIC, 1)
102#define TRACE_STOP _IO(TRACE_IOC_MAGIC, 2)
103#define TRACE_RESET _IO(TRACE_IOC_MAGIC, 3)
104#define TRACE_DUMP _IO(TRACE_IOC_MAGIC, 4)
105#define TRACE_SET_MASK _IO(TRACE_IOC_MAGIC, 5)
106
107#define TRACE_USR_MAX_PARAMS 3
108typedef struct {
109 unsigned int eventType;
110 uintptr_t identity;
111 uintptr_t params[TRACE_USR_MAX_PARAMS];
113
114static void TraceUsage(void)
115{
116 printf("\nUsage: ./trace [start] Start to trace events.\n");
117 printf("\nUsage: ./trace [stop] Stop trace.\n");
118 printf("\nUsage: ./trace [reset] Clear the trace record buffer.\n");
119 printf("\nUsage: ./trace [dump 0/1] Format printf trace data,"
120 "0/1 stands for whether to send data to studio for analysis.\n");
121 printf("\nUsage: ./trace [mask num] Set trace filter event mask.\n");
122 printf("\nUsage: ./trace [read nBytes] Read nBytes raw data from trace buffer.\n");
123 printf("\nUsage: ./trace [write type id params..] Write a user event, no more than 3 parameters.\n");
124}
125
126static void TraceRead(int fd, size_t size)
127{
128 ssize_t i;
129 ssize_t len;
130 if (size <= 0) {
131 return;
132 }
133
134 char *buffer = (char *)malloc(size);
135 if (buffer == NULL) {
136 printf("Read buffer malloc failed.\n");
137 return;
138 }
139
140 len = read(fd, buffer, size);
141 for (i = 0; i < len; i++) {
142 printf("%02x ", buffer[i] & 0xFF);
143 }
144 printf("\n");
145 free(buffer);
146}
147
148static void TraceWrite(int fd, int argc, char **argv)
149{
150 int i;
151 UsrEventInfo info = {0};
152 info.eventType = strtoul(argv[2], NULL, 0); /* 2, argv number */
153 info.identity = strtoul(argv[3], NULL, 0); /* 3, argv number */
154 /* 4, argc -4 means user argv that does not contain argv[0]~argv[3] */
155 int paramNum = (argc - 4) > TRACE_USR_MAX_PARAMS ? TRACE_USR_MAX_PARAMS : (argc - 4);
156
157 for (i = 0; i < paramNum; i++) {
158 /* 4, argc -4 means user argv that does not contain argv[0]~argv[3] */
159 info.params[i] = strtoul(argv[4 + i], NULL, 0);
160 }
161 (void)write(fd, &info, sizeof(UsrEventInfo));
162}
163
164/*!
165 * @brief main 用户态示例代码功能如下:
166 @verbatim
167 1. 打开trace字符设备。
168 2. 设置事件掩码。
169 3. 启动trace。
170 4. 写trace事件。
171 5. 停止trace。
172 6. 格式化输出trace数据。
173 7. 输出结果
174 *******TraceInfo begin*******
175 clockFreq = 50000000
176 CurEvtIndex = 2
177 Index Time(cycles) EventType CurTask Identity params
178 0 0x366d5e88 0xfffffff1 0x1 0x1 0x1 0x2 0x3
179 1 0x366d74ae 0xfffffff2 0x1 0x2 0x1 0x2 0x3
180 *******TraceInfo end*******
181 示例代码中调用了2次write,对应产生2条Trace记录; 用户层事件的EventType高28位固定均为1(即0xfffffff0),
182 仅低4位表示具体的事件类型。
183 @endverbatim
184 * @param argc
185 * @param argv
186 * @return
187 *
188 * @see
189 */
190int main(int argc, char **argv)
191{
192 int fd = open("/dev/trace", O_RDWR);
193 if (fd == -1) {
194 printf("Trace open failed.\n");
195 exit(EXIT_FAILURE);
196 }
197
198 if (argc == 1) {
199 TraceUsage();
200 } else if (argc == 2 && strcmp(argv[1], "start") == 0) { /* 2, argv num, no special meaning */
201 ioctl(fd, TRACE_START, NULL);
202 } else if (argc == 2 && strcmp(argv[1], "stop") == 0) { /* 2, argv num, no special meaning */
203 ioctl(fd, TRACE_STOP, NULL);
204 } else if (argc == 2 && strcmp(argv[1], "reset") == 0) { /* 2, argv num, no special meaning */
205 ioctl(fd, TRACE_RESET, NULL);
206 } else if (argc == 3 && strcmp(argv[1], "mask") == 0) { /* 3, argv num, no special meaning */
207 size_t mask = strtoul(argv[2], NULL, 0);
208 ioctl(fd, TRACE_SET_MASK, mask);
209 } else if (argc == 3 && strcmp(argv[1], "dump") == 0) { /* 3, argv num, no special meaning */
210 size_t flag = strtoul(argv[2], NULL, 0);
211 ioctl(fd, TRACE_DUMP, flag);
212 } else if (argc == 3 && strcmp(argv[1], "read") == 0) { /* 3, argv num, no special meaning */
213 size_t size = strtoul(argv[2], NULL, 0);
214 TraceRead(fd, size);
215 } else if (argc >= 4 && strcmp(argv[1], "write") == 0) { /* 4, argv num, no special meaning */
216 TraceWrite(fd, argc, argv);
217 } else {
218 printf("Unsupported trace command.\n");
219 TraceUsage();
220 }
221
222 close(fd);
223 return 0;
224}
static void TraceRead(int fd, size_t size)
Definition: trace.c:126
int main(int argc, char **argv)
main 用户态示例代码功能如下:
Definition: trace.c:190
static void TraceWrite(int fd, int argc, char **argv)
Definition: trace.c:148
static void TraceUsage(void)
Definition: trace.c:114
INT64 ssize_t
Definition: los_typedef.h:79
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
void exit(int status)
Definition: stdlib.c:60
unsigned int eventType
Definition: trace.c:109
uintptr_t params[TRACE_USR_MAX_PARAMS]
Definition: trace.c:111
uintptr_t identity
Definition: trace.c:110
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void