• 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 "los_base.h"
33 #include "los_hwi.h"
34 #ifdef LOSCFG_SHELL
35 #include "shcmd.h"
36 #endif
37 #ifdef LOSCFG_FS_VFS
38 #include "fs/fs.h"
39 #include "fs/fs_operation.h"
40 #endif
41 
42 #ifdef LOSCFG_SAVE_EXCINFO
43 STATIC log_read_write_fn g_excInfoRW = NULL; /* the hook of read-writing exception information */
44 STATIC CHAR *g_excInfoBuf = NULL;            /* pointer to the buffer for storing the exception information */
45 STATIC UINT32 g_excInfoIndex = 0xFFFFFFFF;   /* the index of the buffer for storing the exception information */
46 STATIC UINT32 g_recordAddr = 0;              /* the address of storing the exception information */
47 STATIC UINT32 g_recordSpace = 0;             /* the size of storing the exception information */
48 
SetExcInfoRW(log_read_write_fn func)49 VOID SetExcInfoRW(log_read_write_fn func)
50 {
51     g_excInfoRW = func;
52 }
53 
GetExcInfoRW(VOID)54 log_read_write_fn GetExcInfoRW(VOID)
55 {
56     return g_excInfoRW;
57 }
58 
SetExcInfoBuf(CHAR * buf)59 VOID SetExcInfoBuf(CHAR *buf)
60 {
61     g_excInfoBuf = buf;
62 }
63 
GetExcInfoBuf(VOID)64 CHAR *GetExcInfoBuf(VOID)
65 {
66     return g_excInfoBuf;
67 }
68 
SetExcInfoIndex(UINT32 index)69 VOID SetExcInfoIndex(UINT32 index)
70 {
71     g_excInfoIndex = index;
72 }
73 
GetExcInfoIndex(VOID)74 UINT32 GetExcInfoIndex(VOID)
75 {
76     return g_excInfoIndex;
77 }
78 
SetRecordAddr(UINT32 addr)79 VOID SetRecordAddr(UINT32 addr)
80 {
81     g_recordAddr = addr;
82 }
83 
GetRecordAddr(VOID)84 UINT32 GetRecordAddr(VOID)
85 {
86     return g_recordAddr;
87 }
88 
SetRecordSpace(UINT32 space)89 VOID SetRecordSpace(UINT32 space)
90 {
91     g_recordSpace = space;
92 }
93 
GetRecordSpace(VOID)94 UINT32 GetRecordSpace(VOID)
95 {
96     return g_recordSpace;
97 }
98 
WriteExcBufVa(const CHAR * format,va_list arglist)99 VOID WriteExcBufVa(const CHAR *format, va_list arglist)
100 {
101     errno_t ret;
102 
103     if (g_recordSpace > g_excInfoIndex) {
104         ret = vsnprintf_s((g_excInfoBuf + g_excInfoIndex), (g_recordSpace - g_excInfoIndex),
105                           (g_recordSpace - g_excInfoIndex - 1), format, arglist);
106         if (ret == -1) {
107             PRINT_ERR("exc info buffer is not enough or vsnprintf_s is error.\n");
108             return;
109         }
110         g_excInfoIndex += ret;
111     }
112 }
113 
WriteExcInfoToBuf(const CHAR * format,...)114 VOID WriteExcInfoToBuf(const CHAR *format, ...)
115 {
116     va_list arglist;
117 
118     va_start(arglist, format);
119     WriteExcBufVa(format, arglist);
120     va_end(arglist);
121 }
122 
LOS_ExcInfoRegHook(UINT32 startAddr,UINT32 space,CHAR * buf,log_read_write_fn hook)123 VOID LOS_ExcInfoRegHook(UINT32 startAddr, UINT32 space, CHAR *buf, log_read_write_fn hook)
124 {
125     if ((hook == NULL) || (buf == NULL)) {
126         PRINT_ERR("Buf or hook is null.\n");
127         return;
128     }
129 
130     g_recordAddr = startAddr;
131     g_recordSpace = space;
132     g_excInfoBuf = buf;
133     g_excInfoRW = hook;
134 
135 #ifdef LOSCFG_FS_VFS
136     los_vfs_init();
137 #endif
138 }
139 
140 /* Be called in the exception. */
OsReadWriteExceptionInfo(UINT32 startAddr,UINT32 space,UINT32 flag,CHAR * buf)141 VOID OsReadWriteExceptionInfo(UINT32 startAddr, UINT32 space, UINT32 flag, CHAR *buf)
142 {
143     if ((buf == NULL) || (space == 0)) {
144         PRINT_ERR("buffer is null or space is zero\n");
145         return;
146     }
147     // user can write exception information to files here
148 }
149 
OsRecordExcInfoTime(VOID)150 VOID OsRecordExcInfoTime(VOID)
151 {
152 #ifdef LOSCFG_FS_VFS
153 #define NOW_TIME_LENGTH 24
154     time_t t;
155     struct tm *tmTime = NULL;
156     CHAR nowTime[NOW_TIME_LENGTH];
157 
158     (VOID)time(&t);
159     tmTime = localtime(&t);
160     if (tmTime == NULL) {
161         return;
162     }
163     (VOID)memset_s(nowTime, sizeof(nowTime), 0, sizeof(nowTime));
164     (VOID)strftime(nowTime, NOW_TIME_LENGTH, "%Y-%m-%d %H:%M:%S", tmTime);
165 #undef NOW_TIME_LENGTH
166     WriteExcInfoToBuf("%s \n", nowTime);
167 #endif
168 }
169 
170 #ifdef LOSCFG_SHELL
OsShellCmdReadExcInfo(INT32 argc,CHAR ** argv)171 INT32 OsShellCmdReadExcInfo(INT32 argc, CHAR **argv)
172 {
173 #define EXCINFO_ALIGN_SIZE 64
174     UINT32 recordSpace = GetRecordSpace();
175 
176     (VOID)argc;
177     (VOID)argv;
178 
179     CHAR *buf = (CHAR *)LOS_MemAllocAlign((VOID *)OS_SYS_MEM_ADDR, recordSpace + 1, EXCINFO_ALIGN_SIZE);
180     if (buf == NULL) {
181         return LOS_NOK;
182     }
183     (VOID)memset_s(buf, recordSpace + 1, 0, recordSpace + 1);
184 
185     log_read_write_fn hook = GetExcInfoRW();
186     if (hook != NULL) {
187         hook(GetRecordAddr(), recordSpace, 1, buf);
188     }
189     PRINTK("%s\n", buf);
190     (VOID)LOS_MemFree((void *)OS_SYS_MEM_ADDR, buf);
191     buf = NULL;
192     return LOS_OK;
193 }
194 
195 SHELLCMD_ENTRY(readExcInfo_shellcmd, CMD_TYPE_EX, "excInfo", 0, (CmdCallBackFunc)OsShellCmdReadExcInfo);
196 #endif
197 
198 #endif
199 
200