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_sem_debug_pri.h"
33 #include "stdlib.h"
34 #include "los_typedef.h"
35 #include "los_task_pri.h"
36 #include "los_ipcdebug_pri.h"
37 #ifdef LOSCFG_SHELL
38 #include "shcmd.h"
39 #endif /* LOSCFG_SHELL */
40
41
42 #define OS_ALL_SEM_MASK 0xffffffff
43
44 #if defined(LOSCFG_DEBUG_SEMAPHORE) || defined(LOSCFG_SHELL_CMD_DEBUG)
OsSemPendedTaskNamePrint(LosSemCB * semNode)45 STATIC VOID OsSemPendedTaskNamePrint(LosSemCB *semNode)
46 {
47 LosTaskCB *tskCB = NULL;
48 CHAR *nameArr[LOSCFG_BASE_CORE_TSK_LIMIT] = {0};
49 UINT32 i, intSave;
50 UINT32 num = 0;
51
52 SCHEDULER_LOCK(intSave);
53 if ((semNode->semStat == OS_SEM_UNUSED) || (LOS_ListEmpty(&semNode->semList))) {
54 SCHEDULER_UNLOCK(intSave);
55 return;
56 }
57
58 LOS_DL_LIST_FOR_EACH_ENTRY(tskCB, &semNode->semList, LosTaskCB, pendList) {
59 nameArr[num++] = tskCB->taskName;
60 if (num == LOSCFG_BASE_CORE_TSK_LIMIT) {
61 break;
62 }
63 }
64 SCHEDULER_UNLOCK(intSave);
65
66 PRINTK("Pended task list : ");
67 for (i = 0; i < num; i++) {
68 if (i == 0) {
69 PRINTK("\n%s", nameArr[i]);
70 } else {
71 PRINTK(", %s", nameArr[i]);
72 }
73 }
74 PRINTK("\n");
75 }
76 #endif
77
78 #ifdef LOSCFG_DEBUG_SEMAPHORE
79
80 typedef struct {
81 UINT16 origSemCount; /* Number of original available semaphores */
82 UINT64 lastAccessTime; /* The last operation time */
83 TSK_ENTRY_FUNC creator; /* The task entry who created this sem */
84 } SemDebugCB;
85 STATIC SemDebugCB *g_semDebugArray = NULL;
86
SemCompareValue(const IpcSortParam * sortParam,UINT32 left,UINT32 right)87 STATIC BOOL SemCompareValue(const IpcSortParam *sortParam, UINT32 left, UINT32 right)
88 {
89 return (*((UINT64 *)(VOID *)SORT_ELEM_ADDR(sortParam, left)) >
90 *((UINT64 *)(VOID *)SORT_ELEM_ADDR(sortParam, right)));
91 }
92
OsSemDbgInit(VOID)93 UINT32 OsSemDbgInit(VOID)
94 {
95 UINT32 size = LOSCFG_BASE_IPC_SEM_LIMIT * sizeof(SemDebugCB);
96 /* system resident memory, don't free */
97 g_semDebugArray = (SemDebugCB *)LOS_MemAlloc(m_aucSysMem1, size);
98 if (g_semDebugArray == NULL) {
99 PRINT_ERR("%s: malloc failed!\n", __FUNCTION__);
100 return LOS_NOK;
101 }
102 (VOID)memset_s(g_semDebugArray, size, 0, size);
103 return LOS_OK;
104 }
105
OsSemDbgTimeUpdate(UINT32 semID)106 VOID OsSemDbgTimeUpdate(UINT32 semID)
107 {
108 SemDebugCB *semDebug = &g_semDebugArray[GET_SEM_INDEX(semID)];
109 semDebug->lastAccessTime = LOS_TickCountGet();
110 return;
111 }
112
OsSemDbgUpdate(UINT32 semID,TSK_ENTRY_FUNC creator,UINT16 count)113 VOID OsSemDbgUpdate(UINT32 semID, TSK_ENTRY_FUNC creator, UINT16 count)
114 {
115 SemDebugCB *semDebug = &g_semDebugArray[GET_SEM_INDEX(semID)];
116 semDebug->creator = creator;
117 semDebug->lastAccessTime = LOS_TickCountGet();
118 semDebug->origSemCount = count;
119 return;
120 }
121
OsSemSort(UINT32 * semIndexArray,UINT32 usedCount)122 STATIC VOID OsSemSort(UINT32 *semIndexArray, UINT32 usedCount)
123 {
124 UINT32 i, intSave;
125 LosSemCB *semCB = NULL;
126 LosSemCB semNode = {0};
127 SemDebugCB semDebug = {0};
128 IpcSortParam semSortParam;
129 semSortParam.buf = (CHAR *)g_semDebugArray;
130 semSortParam.ipcDebugCBSize = sizeof(SemDebugCB);
131 semSortParam.ipcDebugCBCnt = LOSCFG_BASE_IPC_SEM_LIMIT;
132 semSortParam.sortElemOff = LOS_OFF_SET_OF(SemDebugCB, lastAccessTime);
133
134 /* It will Print out ALL the Used Semaphore List. */
135 PRINTK("Used Semaphore List: \n");
136 PRINTK("\r\n SemID Count OriginalCount Creator(TaskEntry) LastAccessTime\n");
137 PRINTK(" ------ ------ ------------- ------------------ -------------- \n");
138
139 SCHEDULER_LOCK(intSave);
140 OsArraySortByTime(semIndexArray, 0, usedCount - 1, &semSortParam, SemCompareValue);
141 SCHEDULER_UNLOCK(intSave);
142 for (i = 0; i < usedCount; i++) {
143 semCB = GET_SEM(semIndexArray[i]);
144 SCHEDULER_LOCK(intSave);
145 (VOID)memcpy_s(&semNode, sizeof(LosSemCB), semCB, sizeof(LosSemCB));
146 (VOID)memcpy_s(&semDebug, sizeof(SemDebugCB), &g_semDebugArray[semIndexArray[i]], sizeof(SemDebugCB));
147 SCHEDULER_UNLOCK(intSave);
148 if ((semNode.semStat != OS_SEM_USED) || (semDebug.creator == NULL)) {
149 continue;
150 }
151 PRINTK(" 0x%-07x0x%-07u0x%-14u%-22p0x%llx\n", semNode.semID, semDebug.origSemCount,
152 semNode.semCount, semDebug.creator, semDebug.lastAccessTime);
153 if (!LOS_ListEmpty(&semNode.semList)) {
154 OsSemPendedTaskNamePrint(semCB);
155 }
156 }
157 }
158
OsSemInfoGetFullData(VOID)159 UINT32 OsSemInfoGetFullData(VOID)
160 {
161 UINT32 usedSemCnt = 0;
162 LosSemCB *semNode = NULL;
163 SemDebugCB *semDebug = NULL;
164 UINT32 i;
165 UINT32 *semIndexArray = NULL;
166 UINT32 count, intSave;
167
168 SCHEDULER_LOCK(intSave);
169 /* Get the used semaphore count. */
170 for (i = 0; i < LOSCFG_BASE_IPC_SEM_LIMIT; i++) {
171 semNode = GET_SEM(i);
172 semDebug = &g_semDebugArray[i];
173 if ((semNode->semStat == OS_SEM_USED) && (semDebug->creator != NULL)) {
174 usedSemCnt++;
175 }
176 }
177 SCHEDULER_UNLOCK(intSave);
178
179 if (usedSemCnt > 0) {
180 semIndexArray = (UINT32 *)LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, usedSemCnt * sizeof(UINT32));
181 if (semIndexArray == NULL) {
182 PRINTK("LOS_MemAlloc failed in %s \n", __func__);
183 return LOS_NOK;
184 }
185
186 /* Fill the semIndexArray with the real index. */
187 count = 0;
188
189 SCHEDULER_LOCK(intSave);
190 for (i = 0; i < LOSCFG_BASE_IPC_SEM_LIMIT; i++) {
191 semNode = GET_SEM(i);
192 semDebug = &g_semDebugArray[i];
193 if ((semNode->semStat != OS_SEM_USED) || (semDebug->creator == NULL)) {
194 continue;
195 }
196 *(semIndexArray + count) = i;
197 count++;
198 /* if the count is touched usedSemCnt break. */
199 if (count >= usedSemCnt) {
200 break;
201 }
202 }
203 SCHEDULER_UNLOCK(intSave);
204 OsSemSort(semIndexArray, count);
205
206 /* free the index array. */
207 (VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, semIndexArray);
208 }
209 return LOS_OK;
210 }
211 #endif /* LOSCFG_DEBUG_SEMAPHORE */
212
213 #ifdef LOSCFG_SHELL_CMD_DEBUG
OsSemInfoOutput(size_t semID)214 STATIC UINT32 OsSemInfoOutput(size_t semID)
215 {
216 UINT32 loop, semCnt, intSave;
217 LosSemCB *semCB = NULL;
218 LosSemCB semNode = {0};
219
220 if (semID == OS_ALL_SEM_MASK) {
221 for (loop = 0, semCnt = 0; loop < LOSCFG_BASE_IPC_SEM_LIMIT; loop++) {
222 semCB = GET_SEM(loop);
223 SCHEDULER_LOCK(intSave);
224 if (semCB->semStat == OS_SEM_USED) {
225 (VOID)memcpy_s(&semNode, sizeof(LosSemCB), semCB, sizeof(LosSemCB));
226 SCHEDULER_UNLOCK(intSave);
227 semCnt++;
228 PRINTK("\r\n SemID Count\n ---------- -----\n");
229 PRINTK(" 0x%08x %u\n", semNode.semID, semNode.semCount);
230 continue;
231 }
232 SCHEDULER_UNLOCK(intSave);
233 }
234 PRINTK(" SemUsingNum : %u\n\n", semCnt);
235 return LOS_OK;
236 } else {
237 if (GET_SEM_INDEX(semID) >= LOSCFG_BASE_IPC_SEM_LIMIT) {
238 PRINTK("\nInvalid semaphore id!\n");
239 return LOS_OK;
240 }
241
242 semCB = GET_SEM(semID);
243 SCHEDULER_LOCK(intSave);
244 (VOID)memcpy_s(&semNode, sizeof(LosSemCB), semCB, sizeof(LosSemCB));
245 SCHEDULER_UNLOCK(intSave);
246 if ((semNode.semID != semID) || (semNode.semStat != OS_SEM_USED)) {
247 PRINTK("\nThe semaphore is not in use!\n");
248 return LOS_OK;
249 }
250
251 PRINTK("\r\n SemID Count\n ---------- -----\n");
252 PRINTK(" 0x%08x 0x%u\n", semNode.semID, semNode.semCount);
253
254 if (LOS_ListEmpty(&semNode.semList)) {
255 PRINTK("No task is pended on this semaphore!\n");
256 return LOS_OK;
257 } else {
258 OsSemPendedTaskNamePrint(semCB);
259 }
260 }
261 return LOS_OK;
262 }
263
OsShellCmdSemInfoGet(UINT32 argc,const CHAR ** argv)264 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdSemInfoGet(UINT32 argc, const CHAR **argv)
265 {
266 size_t semID;
267 CHAR *endPtr = NULL;
268 UINT32 ret;
269
270 if (argc > 1) {
271 #ifdef LOSCFG_DEBUG_SEMAPHORE
272 PRINTK("\nUsage: sem [fulldata|ID]\n");
273 #else
274 PRINTK("\nUsage: sem [ID]\n");
275 #endif
276 return OS_ERROR;
277 }
278
279 if (argc == 0) {
280 semID = OS_ALL_SEM_MASK;
281 } else {
282 #ifdef LOSCFG_DEBUG_SEMAPHORE
283 if (strcmp(argv[0], "fulldata") == 0) {
284 ret = OsSemInfoGetFullData();
285 return ret;
286 }
287 #endif
288 semID = strtoul(argv[0], &endPtr, 0);
289 if ((endPtr == NULL) || (*endPtr != 0)) {
290 PRINTK("\nsem ID can't access %s.\n", argv[0]);
291 return 0;
292 }
293 }
294
295 ret = OsSemInfoOutput(semID);
296 return ret;
297 }
298
299 SHELLCMD_ENTRY(sem_shellcmd, CMD_TYPE_EX, "sem", 1, (CmdCallBackFunc)OsShellCmdSemInfoGet);
300 #endif
301
302