更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_stackinfo.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_stackinfo_pri.h"
33#include "los_printf_pri.h"
34#include "los_config.h"
35#include "securec.h"
36#ifdef LOSCFG_SHELL
37#include "shcmd.h"
38#include "shell.h"
39#endif
40
41/**
42 * @file los_stackinfo.c
43 * @brief 栈内容
44 * @verbatim
45 @note_pic OsExcStackInfo 各个CPU栈布局图,其他栈也是一样,CPU各核硬件栈都是紧挨着
46 __undef_stack(SMP)
47 +-------------------+ <--- cpu1 top
48 | |
49 | CPU core1 |
50 | |
51 +--------------------<--- cpu2 top
52 | |
53 | cpu core 2 |
54 | |
55 +--------------------<--- cpu3 top
56 | |
57 | cpu core 3 |
58 | |
59 +--------------------<--- cpu4 top
60 | |
61 | cpu core 4 |
62 | |
63 +-------------------+
64 * @endverbatim
65 */
66
67const StackInfo *g_stackInfo = NULL; ///< CPU所有工作模式的栈信息
68UINT32 g_stackNum; ///< CPU所有工作模式的栈数量
69///获取栈的吃水线
70UINT32 OsStackWaterLineGet(const UINTPTR *stackBottom, const UINTPTR *stackTop, UINT32 *peakUsed)
71{
72 UINT32 size;
73 const UINTPTR *tmp = NULL;
74 if (*stackTop == OS_STACK_MAGIC_WORD) {//栈顶值是否等于 magic 0xCCCCCCCC
75 tmp = stackTop + 1;
76 while ((tmp < stackBottom) && (*tmp == OS_STACK_INIT)) {//记录从栈顶到栈低有多少个连续的 0xCACACACA
77 tmp++;
78 }
79 size = (UINT32)((UINTPTR)stackBottom - (UINTPTR)tmp);//剩余多少非0xCACACACA的栈空间
80 *peakUsed = (size == 0) ? size : (size + sizeof(CHAR *));//得出高峰用值,还剩多少可用
81 return LOS_OK;
82 } else {
83 *peakUsed = OS_INVALID_WATERLINE;//栈溢出了
84 return LOS_NOK;
85 }
86}
87///异常情况下的栈检查,主要就是检查栈顶值有没有被改写
89{
90 UINT32 index;
91 UINT32 cpuid;
92 UINTPTR *stackTop = NULL;
93
94 if (g_stackInfo == NULL) {
95 return;
96 }
97 for (index = 0; index < g_stackNum; index++) {
98 for (cpuid = 0; cpuid < LOSCFG_KERNEL_CORE_NUM; cpuid++) {
99 stackTop = (UINTPTR *)((UINTPTR)g_stackInfo[index].stackTop + cpuid * g_stackInfo[index].stackSize);
100 if (*stackTop != OS_STACK_MAGIC_WORD) {// 只要栈顶内容不是 0xCCCCCCCCC 就是溢出了.
101 PRINT_ERR("cpu:%u %s overflow , magic word changed to 0x%x\n",
102 LOSCFG_KERNEL_CORE_NUM - 1 - cpuid, g_stackInfo[index].stackName, *stackTop);
103 }
104 }
105 }
106}
107
108///打印栈的信息 把每个CPU的栈信息打印出来
110{
111 UINT32 index;
112 UINT32 cpuid;
113 UINT32 size;
114 UINTPTR *stackTop = NULL;
115 UINTPTR *stack = NULL;
116
117 if (g_stackInfo == NULL) {
118 return;
119 }
120
121 PrintExcInfo("\n stack name cpu id stack addr total size used size\n"
122 " ---------- ------ --------- -------- --------\n");
123 for (index = 0; index < g_stackNum; index++) {
124 for (cpuid = 0; cpuid < LOSCFG_KERNEL_CORE_NUM; cpuid++) {//可以看出 各个CPU的栈是紧挨的的
125 stackTop = (UINTPTR *)((UINTPTR)g_stackInfo[index].stackTop + cpuid * g_stackInfo[index].stackSize);
126 stack = (UINTPTR *)((UINTPTR)stackTop + g_stackInfo[index].stackSize);
127 (VOID)OsStackWaterLineGet(stack, stackTop, &size);//获取吃水线, 鸿蒙用WaterLine 这个词用的很妙
128
129 PrintExcInfo("%11s %-5d %-10p 0x%-8x 0x%-4x\n", g_stackInfo[index].stackName,
130 LOSCFG_KERNEL_CORE_NUM - 1 - cpuid, stackTop, g_stackInfo[index].stackSize, size);
131 }
132 }
133
134 OsExcStackCheck();//发生异常时栈检查
135}
136
137///注册栈信息
138VOID OsExcStackInfoReg(const StackInfo *stackInfo, UINT32 stackNum)
139{
140 g_stackInfo = stackInfo; //全局变量指向g_excStack
141 g_stackNum = stackNum;
142}
143
144///task栈的初始化,设置固定的值. 0xcccccccc 和 0xcacacaca
145VOID OsStackInit(VOID *stacktop, UINT32 stacksize)
146{
147 /* initialize the task stack, write magic num to stack top */
148 errno_t ret = memset_s(stacktop, stacksize, (INT32)OS_STACK_INIT, stacksize);//清一色填 0xCACACACA
149 if (ret == EOK) {
150 *((UINTPTR *)stacktop) = OS_STACK_MAGIC_WORD;//0xCCCCCCCCC 中文就是"烫烫烫烫" 这几个字懂点计算机的人都不会陌生了.
151 }
152}
153
154#ifdef LOSCFG_SHELL_CMD_DEBUG
155SHELLCMD_ENTRY(stack_shellcmd, CMD_TYPE_EX, "stack", 1, (CmdCallBackFunc)OsExcStackInfo);//采用shell命令静态注册方式
156#endif
@ CMD_TYPE_EX
不支持标准命令参数输入,会把用户填写的命令关键字屏蔽掉,例如:输入ls /ramfs,传入给注册函数的参数只有/ramfs,而ls命令关键字并不会被传入。
Definition: shell.h:91
macro EXC_SP_SET stackBottom
Definition: asm.h:50
macro EXC_SP_SET stackSize
Definition: asm.h:50
UINT32 OsStackWaterLineGet(const UINTPTR *stackBottom, const UINTPTR *stackTop, UINT32 *peakUsed)
获取栈的吃水线
Definition: los_stackinfo.c:70
VOID PrintExcInfo(const CHAR *fmt,...)
打印异常信息
Definition: los_printf.c:263
const StackInfo * g_stackInfo
CPU所有工作模式的栈信息
Definition: los_stackinfo.c:67
VOID OsExcStackInfoReg(const StackInfo *stackInfo, UINT32 stackNum)
注册栈信息
VOID OsExcStackCheck(VOID)
异常情况下的栈检查,主要就是检查栈顶值有没有被改写
Definition: los_stackinfo.c:88
VOID OsStackInit(VOID *stacktop, UINT32 stacksize)
task栈的初始化,设置固定的值. 0xcccccccc 和 0xcacacaca
UINT32 g_stackNum
Definition: los_stackinfo.c:68
SHELLCMD_ENTRY(stack_shellcmd, CMD_TYPE_EX, "stack", 1,(CmdCallBackFunc) OsExcStackInfo)
VOID OsExcStackInfo(VOID)
打印栈的信息 把每个CPU的栈信息打印出来
signed int INT32
Definition: los_typedef.h:60
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57
char CHAR
Definition: los_typedef.h:63
UINT32 stackSize
栈大小
VOID * stackTop
栈顶
u32_t(* CmdCallBackFunc)(u32_t argc, const char **argv)
Definition: types_adapt.h:86