10d163575Sopenharmony_ci/* 20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 40d163575Sopenharmony_ci * 50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 60d163575Sopenharmony_ci * are permitted provided that the following conditions are met: 70d163575Sopenharmony_ci * 80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 90d163575Sopenharmony_ci * conditions and the following disclaimer. 100d163575Sopenharmony_ci * 110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 120d163575Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 130d163575Sopenharmony_ci * provided with the distribution. 140d163575Sopenharmony_ci * 150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 160d163575Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 170d163575Sopenharmony_ci * permission. 180d163575Sopenharmony_ci * 190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 300d163575Sopenharmony_ci */ 310d163575Sopenharmony_ci 320d163575Sopenharmony_ci#include "fcntl.h" 330d163575Sopenharmony_ci#include "linux/kernel.h" 340d163575Sopenharmony_ci#include "sys/ioctl.h" 350d163575Sopenharmony_ci#include "fs/driver.h" 360d163575Sopenharmony_ci#include "los_dev_trace.h" 370d163575Sopenharmony_ci#include "los_trace.h" 380d163575Sopenharmony_ci#include "los_hook.h" 390d163575Sopenharmony_ci#include "los_init.h" 400d163575Sopenharmony_ci 410d163575Sopenharmony_ci#define TRACE_DRIVER "/dev/trace" 420d163575Sopenharmony_ci#define TRACE_DRIVER_MODE 0666 430d163575Sopenharmony_ci 440d163575Sopenharmony_ci/* trace ioctl */ 450d163575Sopenharmony_ci#define TRACE_IOC_MAGIC 'T' 460d163575Sopenharmony_ci#define TRACE_START _IO(TRACE_IOC_MAGIC, 1) 470d163575Sopenharmony_ci#define TRACE_STOP _IO(TRACE_IOC_MAGIC, 2) 480d163575Sopenharmony_ci#define TRACE_RESET _IO(TRACE_IOC_MAGIC, 3) 490d163575Sopenharmony_ci#define TRACE_DUMP _IO(TRACE_IOC_MAGIC, 4) 500d163575Sopenharmony_ci#define TRACE_SET_MASK _IO(TRACE_IOC_MAGIC, 5) 510d163575Sopenharmony_ci 520d163575Sopenharmony_cistatic int TraceOpen(struct file *filep) 530d163575Sopenharmony_ci{ 540d163575Sopenharmony_ci return 0; 550d163575Sopenharmony_ci} 560d163575Sopenharmony_ci 570d163575Sopenharmony_cistatic int TraceClose(struct file *filep) 580d163575Sopenharmony_ci{ 590d163575Sopenharmony_ci return 0; 600d163575Sopenharmony_ci} 610d163575Sopenharmony_ci 620d163575Sopenharmony_cistatic ssize_t TraceRead(struct file *filep, char *buffer, size_t buflen) 630d163575Sopenharmony_ci{ 640d163575Sopenharmony_ci /* trace record buffer read */ 650d163575Sopenharmony_ci ssize_t len = buflen; 660d163575Sopenharmony_ci OfflineHead *records; 670d163575Sopenharmony_ci int ret; 680d163575Sopenharmony_ci int realLen; 690d163575Sopenharmony_ci 700d163575Sopenharmony_ci if (len % sizeof(unsigned int)) { 710d163575Sopenharmony_ci PRINT_ERR("Buffer size not aligned by 4 bytes\n"); 720d163575Sopenharmony_ci return -EINVAL; 730d163575Sopenharmony_ci } 740d163575Sopenharmony_ci 750d163575Sopenharmony_ci records = LOS_TraceRecordGet(); 760d163575Sopenharmony_ci if (records == NULL) { 770d163575Sopenharmony_ci PRINT_ERR("Trace read failed, check whether trace mode is set to offline\n"); 780d163575Sopenharmony_ci return -EINVAL; 790d163575Sopenharmony_ci } 800d163575Sopenharmony_ci 810d163575Sopenharmony_ci realLen = buflen < records->totalLen ? buflen : records->totalLen; 820d163575Sopenharmony_ci ret = LOS_CopyFromKernel((void *)buffer, buflen, (void *)records, realLen); 830d163575Sopenharmony_ci if (ret != 0) { 840d163575Sopenharmony_ci return -EINVAL; 850d163575Sopenharmony_ci } 860d163575Sopenharmony_ci 870d163575Sopenharmony_ci return realLen; 880d163575Sopenharmony_ci} 890d163575Sopenharmony_ci 900d163575Sopenharmony_cistatic ssize_t TraceWrite(struct file *filep, const char *buffer, size_t buflen) 910d163575Sopenharmony_ci{ 920d163575Sopenharmony_ci /* trace usr event here */ 930d163575Sopenharmony_ci int ret; 940d163575Sopenharmony_ci UsrEventInfo *info = NULL; 950d163575Sopenharmony_ci int infoLen = sizeof(UsrEventInfo); 960d163575Sopenharmony_ci 970d163575Sopenharmony_ci if (buflen != infoLen) { 980d163575Sopenharmony_ci PRINT_ERR("Buffer size not %d bytes\n", infoLen); 990d163575Sopenharmony_ci return -EINVAL; 1000d163575Sopenharmony_ci } 1010d163575Sopenharmony_ci 1020d163575Sopenharmony_ci info = LOS_MemAlloc(m_aucSysMem0, infoLen); 1030d163575Sopenharmony_ci if (info == NULL) { 1040d163575Sopenharmony_ci return -ENOMEM; 1050d163575Sopenharmony_ci } 1060d163575Sopenharmony_ci (void)memset_s(info, infoLen, 0, infoLen); 1070d163575Sopenharmony_ci 1080d163575Sopenharmony_ci ret = LOS_CopyToKernel(info, infoLen, buffer, buflen); 1090d163575Sopenharmony_ci if (ret != 0) { 1100d163575Sopenharmony_ci LOS_MemFree(m_aucSysMem0, info); 1110d163575Sopenharmony_ci return -EINVAL; 1120d163575Sopenharmony_ci } 1130d163575Sopenharmony_ci OsHookCall(LOS_HOOK_TYPE_USR_EVENT, info, infoLen); 1140d163575Sopenharmony_ci return 0; 1150d163575Sopenharmony_ci} 1160d163575Sopenharmony_ci 1170d163575Sopenharmony_cistatic int TraceIoctl(struct file *filep, int cmd, unsigned long arg) 1180d163575Sopenharmony_ci{ 1190d163575Sopenharmony_ci switch (cmd) { 1200d163575Sopenharmony_ci case TRACE_START: 1210d163575Sopenharmony_ci return LOS_TraceStart(); 1220d163575Sopenharmony_ci case TRACE_STOP: 1230d163575Sopenharmony_ci LOS_TraceStop(); 1240d163575Sopenharmony_ci break; 1250d163575Sopenharmony_ci case TRACE_RESET: 1260d163575Sopenharmony_ci LOS_TraceReset(); 1270d163575Sopenharmony_ci break; 1280d163575Sopenharmony_ci case TRACE_DUMP: 1290d163575Sopenharmony_ci LOS_TraceRecordDump((BOOL)arg); 1300d163575Sopenharmony_ci break; 1310d163575Sopenharmony_ci case TRACE_SET_MASK: 1320d163575Sopenharmony_ci LOS_TraceEventMaskSet((UINT32)arg); 1330d163575Sopenharmony_ci break; 1340d163575Sopenharmony_ci default: 1350d163575Sopenharmony_ci PRINT_ERR("Unknown trace ioctl cmd:%d\n", cmd); 1360d163575Sopenharmony_ci return -EINVAL; 1370d163575Sopenharmony_ci } 1380d163575Sopenharmony_ci return 0; 1390d163575Sopenharmony_ci} 1400d163575Sopenharmony_ci 1410d163575Sopenharmony_cistatic const struct file_operations_vfs g_traceDevOps = { 1420d163575Sopenharmony_ci TraceOpen, /* open */ 1430d163575Sopenharmony_ci TraceClose, /* close */ 1440d163575Sopenharmony_ci TraceRead, /* read */ 1450d163575Sopenharmony_ci TraceWrite, /* write */ 1460d163575Sopenharmony_ci NULL, /* seek */ 1470d163575Sopenharmony_ci TraceIoctl, /* ioctl */ 1480d163575Sopenharmony_ci NULL, /* mmap */ 1490d163575Sopenharmony_ci#ifndef CONFIG_DISABLE_POLL 1500d163575Sopenharmony_ci NULL, /* poll */ 1510d163575Sopenharmony_ci#endif 1520d163575Sopenharmony_ci NULL, /* unlink */ 1530d163575Sopenharmony_ci}; 1540d163575Sopenharmony_ci 1550d163575Sopenharmony_ciint DevTraceRegister(void) 1560d163575Sopenharmony_ci{ 1570d163575Sopenharmony_ci return register_driver(TRACE_DRIVER, &g_traceDevOps, TRACE_DRIVER_MODE, 0); /* 0666: file mode */ 1580d163575Sopenharmony_ci} 1590d163575Sopenharmony_ci 1600d163575Sopenharmony_ciLOS_MODULE_INIT(DevTraceRegister, LOS_INIT_LEVEL_KMOD_EXTENDED);