• 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 "fcntl.h"
33 #include "linux/kernel.h"
34 #include "sys/ioctl.h"
35 #include "fs/driver.h"
36 #include "los_dev_trace.h"
37 #include "los_trace.h"
38 #include "los_hook.h"
39 #include "los_init.h"
40 
41 #define TRACE_DRIVER "/dev/trace"
42 #define TRACE_DRIVER_MODE 0666
43 
44 /* trace ioctl */
45 #define TRACE_IOC_MAGIC     'T'
46 #define TRACE_START         _IO(TRACE_IOC_MAGIC, 1)
47 #define TRACE_STOP          _IO(TRACE_IOC_MAGIC, 2)
48 #define TRACE_RESET         _IO(TRACE_IOC_MAGIC, 3)
49 #define TRACE_DUMP          _IO(TRACE_IOC_MAGIC, 4)
50 #define TRACE_SET_MASK      _IO(TRACE_IOC_MAGIC, 5)
51 
TraceOpen(struct file * filep)52 static int TraceOpen(struct file *filep)
53 {
54     return 0;
55 }
56 
TraceClose(struct file * filep)57 static int TraceClose(struct file *filep)
58 {
59     return 0;
60 }
61 
TraceRead(struct file * filep,char * buffer,size_t buflen)62 static ssize_t TraceRead(struct file *filep, char *buffer, size_t buflen)
63 {
64     /* trace record buffer read */
65     ssize_t len = buflen;
66     OfflineHead *records;
67     int ret;
68     int realLen;
69 
70     if (len % sizeof(unsigned int)) {
71         PRINT_ERR("Buffer size not aligned by 4 bytes\n");
72         return -EINVAL;
73     }
74 
75     records = LOS_TraceRecordGet();
76     if (records == NULL) {
77         PRINT_ERR("Trace read failed, check whether trace mode is set to offline\n");
78         return -EINVAL;
79     }
80 
81     realLen = buflen < records->totalLen ? buflen : records->totalLen;
82     ret = LOS_CopyFromKernel((void *)buffer, buflen, (void *)records, realLen);
83     if (ret != 0) {
84         return -EINVAL;
85     }
86 
87     return realLen;
88 }
89 
TraceWrite(struct file * filep,const char * buffer,size_t buflen)90 static ssize_t TraceWrite(struct file *filep, const char *buffer, size_t buflen)
91 {
92     /* trace usr event here */
93     int ret;
94     UsrEventInfo *info = NULL;
95     int infoLen = sizeof(UsrEventInfo);
96 
97     if (buflen != infoLen) {
98         PRINT_ERR("Buffer size not %d bytes\n", infoLen);
99         return -EINVAL;
100     }
101 
102     info = LOS_MemAlloc(m_aucSysMem0, infoLen);
103     if (info == NULL) {
104         return -ENOMEM;
105     }
106     (void)memset_s(info, infoLen, 0, infoLen);
107 
108     ret = LOS_CopyToKernel(info, infoLen, buffer, buflen);
109     if (ret != 0) {
110         LOS_MemFree(m_aucSysMem0, info);
111         return -EINVAL;
112     }
113     OsHookCall(LOS_HOOK_TYPE_USR_EVENT, info, infoLen);
114     return 0;
115 }
116 
TraceIoctl(struct file * filep,int cmd,unsigned long arg)117 static int TraceIoctl(struct file *filep, int cmd, unsigned long arg)
118 {
119     switch (cmd) {
120         case TRACE_START:
121             return LOS_TraceStart();
122         case TRACE_STOP:
123             LOS_TraceStop();
124             break;
125         case TRACE_RESET:
126             LOS_TraceReset();
127             break;
128         case TRACE_DUMP:
129             LOS_TraceRecordDump((BOOL)arg);
130             break;
131         case TRACE_SET_MASK:
132             LOS_TraceEventMaskSet((UINT32)arg);
133             break;
134         default:
135             PRINT_ERR("Unknown trace ioctl cmd:%d\n", cmd);
136             return -EINVAL;
137     }
138     return 0;
139 }
140 
141 static const struct file_operations_vfs g_traceDevOps = {
142     TraceOpen,       /* open */
143     TraceClose,      /* close */
144     TraceRead,       /* read */
145     TraceWrite,      /* write */
146     NULL,            /* seek */
147     TraceIoctl,      /* ioctl */
148     NULL,            /* mmap */
149 #ifndef CONFIG_DISABLE_POLL
150     NULL,            /* poll */
151 #endif
152     NULL,            /* unlink */
153 };
154 
DevTraceRegister(void)155 int DevTraceRegister(void)
156 {
157     return register_driver(TRACE_DRIVER, &g_traceDevOps, TRACE_DRIVER_MODE, 0); /* 0666: file mode */
158 }
159 
160 LOS_MODULE_INIT(DevTraceRegister, LOS_INIT_LEVEL_KMOD_EXTENDED);