更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_excinfo.c
浏览该文件的文档.
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
43STATIC log_read_write_fn g_excInfoRW = NULL; /* the hook of read-writing exception information */ //读写异常信息的钩子函数
44STATIC CHAR *g_excInfoBuf = NULL; /* pointer to the buffer for storing the exception information */ //指向存储异常信息的缓冲区的指针
45STATIC UINT32 g_excInfoIndex = 0xFFFFFFFF; /* the index of the buffer for storing the exception information */ //用于存储异常信息的缓冲区的索引
46STATIC UINT32 g_recordAddr = 0; /* the address of storing the exception information */ //存储异常信息的地址
47STATIC UINT32 g_recordSpace = 0; /* the size of storing the exception information */ //存储异常信息的大小
48//设置异常信息的读写函数
50{
51 g_excInfoRW = func;
52}
53///获取异常信息读写函数
55{
56 return g_excInfoRW;
57}
58///设置异常信息的缓存
60{
61 g_excInfoBuf = buf;
62}
63///获取异常信息的缓存
65{
66 return g_excInfoBuf;
67}
68///设置异常信息索引位
70{
71 g_excInfoIndex = index;
72}
73///获取异常信息索引位
75{
76 return g_excInfoIndex;
77}
78
80{
81 g_recordAddr = addr;
82}
83
85{
86 return g_recordAddr;
87}
88
90{
91 g_recordSpace = space;
92}
93
95{
96 return g_recordSpace;
97}
98///vsnprintf 为C标准库可变参数的实现函数 见于 ..\third_party\musl\kernel\src\stdio\vsnprintf.c
99VOID WriteExcBufVa(const CHAR *format, va_list arglist)
100{
101 errno_t ret;
102
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///写异常信息到系统异常信息中心
115{
116 va_list arglist;//va_arg
117 va_start(arglist, format);//从任务栈中取出入栈参数
118 WriteExcBufVa(format, arglist);//入栈参数列表作为实参传入交由vsnprintf处理
119 va_end(arglist);//释放资源
120}
121///用于注册记录异常信息函数,并指定位置、空间和大小
122VOID LOS_ExcInfoRegHook(UINT32 startAddr, UINT32 space, CHAR *buf, log_read_write_fn hook)
123{
124 if ((hook == NULL) || (buf == NULL)) {
125 PRINT_ERR("Buf or hook is null.\n");
126 return;
127 }
128
129 g_recordAddr = startAddr;
130 g_recordSpace = space;
131 g_excInfoBuf = buf;
132 g_excInfoRW = hook;
133
134#ifdef LOSCFG_FS_VFS
135 los_vfs_init();//初始化虚拟文件系统
136#endif
137}
138
139/* Be called in the exception. */ //异常发生时回调这里
140VOID OsReadWriteExceptionInfo(UINT32 startAddr, UINT32 space, UINT32 flag, CHAR *buf)
141{
142 if ((buf == NULL) || (space == 0)) {
143 PRINT_ERR("buffer is null or space is zero\n");
144 return;
145 }
146 // user can write exception information to files here
147}
148///记录异常信息产生的时间
150{
151#ifdef LOSCFG_FS_VFS
152#define NOW_TIME_LENGTH 24
153 time_t t;
154 struct tm *tmTime = NULL;
155 CHAR nowTime[NOW_TIME_LENGTH];
156
157 (VOID)time(&t);
158 tmTime = localtime(&t);//获取本地时间的标准C库函数
159 if (tmTime == NULL) {
160 return;
161 }
162 (VOID)memset_s(nowTime, sizeof(nowTime), 0, sizeof(nowTime));
163 (VOID)strftime(nowTime, NOW_TIME_LENGTH, "%Y-%m-%d %H:%M:%S", tmTime);//生成时间格式
164#undef NOW_TIME_LENGTH
165 WriteExcInfoToBuf("%s \n", nowTime);
166#endif
167}
168
169#ifdef LOSCFG_SHELL
171{
172#define EXCINFO_ALIGN_SIZE 64
173 UINT32 recordSpace = GetRecordSpace();
174
175 (VOID)argc;
176 (VOID)argv;
177
178 CHAR *buf = (CHAR *)LOS_MemAllocAlign((VOID *)OS_SYS_MEM_ADDR, recordSpace + 1, EXCINFO_ALIGN_SIZE);
179 if (buf == NULL) {
180 return LOS_NOK;
181 }
182 (VOID)memset_s(buf, recordSpace + 1, 0, recordSpace + 1);
183
185 if (hook != NULL) {
186 hook(GetRecordAddr(), recordSpace, 1, buf);
187 }
188 PRINTK("%s\n", buf);
189 (VOID)LOS_MemFree((void *)OS_SYS_MEM_ADDR, buf);
190 buf = NULL;
191 return LOS_OK;
192}
193
195#endif
196
197#endif
198
@ CMD_TYPE_EX
不支持标准命令参数输入,会把用户填写的命令关键字屏蔽掉,例如:输入ls /ramfs,传入给注册函数的参数只有/ramfs,而ls命令关键字并不会被传入。
Definition: shell.h:91
int format(const char *dev, int sectors, int option)
formatting sd card
Definition: format.c:44
void los_vfs_init(void)
Initializes the vfs filesystem
Definition: vfs_init.c:70
VOID(* log_read_write_fn)(UINT32 startAddr, UINT32 space, UINT32 rwFlag, CHAR *buf)
define the type of functions for reading or writing exception information.
Definition: los_config.h:435
VOID LOS_ExcInfoRegHook(UINT32 startAddr, UINT32 space, CHAR *buf, log_read_write_fn hook)
用于注册记录异常信息函数,并指定位置、空间和大小
Definition: los_excinfo.c:122
VOID * LOS_MemAllocAlign(VOID *pool, UINT32 size, UINT32 boundary)
从指定内存池中申请size长度的内存且地址按boundary字节对齐的内存
Definition: los_memory.c:1150
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
UINT32 GetExcInfoIndex(VOID)
获取异常信息索引位
Definition: los_excinfo.c:74
STATIC UINT32 g_recordSpace
Definition: los_excinfo.c:47
SHELLCMD_ENTRY(readExcInfo_shellcmd, CMD_TYPE_EX, "excInfo", 0,(CmdCallBackFunc) OsShellCmdReadExcInfo)
STATIC UINT32 g_excInfoIndex
Definition: los_excinfo.c:45
VOID SetRecordSpace(UINT32 space)
Definition: los_excinfo.c:89
CHAR * GetExcInfoBuf(VOID)
获取异常信息的缓存
Definition: los_excinfo.c:64
STATIC CHAR * g_excInfoBuf
Definition: los_excinfo.c:44
INT32 OsShellCmdReadExcInfo(INT32 argc, CHAR **argv)
Definition: los_excinfo.c:170
UINT32 GetRecordAddr(VOID)
Definition: los_excinfo.c:84
VOID WriteExcInfoToBuf(const CHAR *format,...)
写异常信息到系统异常信息中心
Definition: los_excinfo.c:114
UINT32 GetRecordSpace(VOID)
Definition: los_excinfo.c:94
VOID SetRecordAddr(UINT32 addr)
Definition: los_excinfo.c:79
log_read_write_fn GetExcInfoRW(VOID)
获取异常信息读写函数
Definition: los_excinfo.c:54
VOID WriteExcBufVa(const CHAR *format, va_list arglist)
vsnprintf 为C标准库可变参数的实现函数 见于 ..\third_party\musl\kernel\src\stdio\vsnprintf.c
Definition: los_excinfo.c:99
VOID SetExcInfoRW(log_read_write_fn func)
Definition: los_excinfo.c:49
VOID SetExcInfoBuf(CHAR *buf)
设置异常信息的缓存
Definition: los_excinfo.c:59
STATIC UINT32 g_recordAddr
Definition: los_excinfo.c:46
STATIC log_read_write_fn g_excInfoRW
Definition: los_excinfo.c:43
VOID OsRecordExcInfoTime(VOID)
记录异常信息产生的时间
Definition: los_excinfo.c:149
VOID OsReadWriteExceptionInfo(UINT32 startAddr, UINT32 space, UINT32 flag, CHAR *buf)
Definition: los_excinfo.c:140
VOID SetExcInfoIndex(UINT32 index)
设置异常信息索引位
Definition: los_excinfo.c:69
signed int INT32
Definition: los_typedef.h:60
unsigned int UINT32
Definition: los_typedef.h:57
char CHAR
Definition: los_typedef.h:63
time_t time(time_t *t)
Definition: time.c:1224
u32_t(* CmdCallBackFunc)(u32_t argc, const char **argv)
Definition: types_adapt.h:86