• 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_queue_debug_pri.h"
33 #include "los_hw_pri.h"
34 #include "los_ipcdebug_pri.h"
35 #ifdef LOSCFG_SHELL
36 #include "shcmd.h"
37 #endif /* LOSCFG_SHELL */
38 
39 
40 #ifdef LOSCFG_DEBUG_QUEUE
41 
42 typedef struct {
43     TSK_ENTRY_FUNC creator; /* The task entry who created this queue */
44     UINT64  lastAccessTime; /* The last access time */
45 } QueueDebugCB;
46 STATIC QueueDebugCB *g_queueDebugArray = NULL;
47 
QueueCompareValue(const IpcSortParam * sortParam,UINT32 left,UINT32 right)48 STATIC BOOL QueueCompareValue(const IpcSortParam *sortParam, UINT32 left, UINT32 right)
49 {
50     return (*((UINT64 *)(VOID *)SORT_ELEM_ADDR(sortParam, left)) >
51             *((UINT64 *)(VOID *)SORT_ELEM_ADDR(sortParam, right)));
52 }
53 
OsQueueDbgInit(VOID)54 UINT32 OsQueueDbgInit(VOID)
55 {
56     UINT32 size = LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(QueueDebugCB);
57     /* system resident memory, don't free */
58     g_queueDebugArray = (QueueDebugCB *)LOS_MemAlloc(m_aucSysMem1, size);
59     if (g_queueDebugArray == NULL) {
60         PRINT_ERR("%s: malloc failed!\n", __FUNCTION__);
61         return LOS_NOK;
62     }
63     (VOID)memset_s(g_queueDebugArray, size, 0, size);
64     return LOS_OK;
65 }
66 
OsQueueDbgTimeUpdate(UINT32 queueID)67 VOID OsQueueDbgTimeUpdate(UINT32 queueID)
68 {
69     QueueDebugCB *queueDebug = &g_queueDebugArray[GET_QUEUE_INDEX(queueID)];
70     queueDebug->lastAccessTime = LOS_TickCountGet();
71     return;
72 }
73 
OsQueueDbgUpdate(UINT32 queueID,TSK_ENTRY_FUNC entry)74 VOID OsQueueDbgUpdate(UINT32 queueID, TSK_ENTRY_FUNC entry)
75 {
76     QueueDebugCB *queueDebug = &g_queueDebugArray[GET_QUEUE_INDEX(queueID)];
77     queueDebug->creator = entry;
78     queueDebug->lastAccessTime = LOS_TickCountGet();
79     return;
80 }
81 
OsQueueInfoOutPut(const LosQueueCB * node)82 STATIC INLINE VOID OsQueueInfoOutPut(const LosQueueCB *node)
83 {
84     PRINTK("Queue ID <0x%x> may leak, queue len is 0x%x, "
85            "readable cnt:0x%x, writable cnt:0x%x, ",
86            node->queueID,
87            node->queueLen,
88            node->readWriteableCnt[OS_QUEUE_READ],
89            node->readWriteableCnt[OS_QUEUE_WRITE]);
90 }
91 
OsQueueOpsOutput(const QueueDebugCB * node)92 STATIC INLINE VOID OsQueueOpsOutput(const QueueDebugCB *node)
93 {
94     PRINTK("TaskEntry of creator:0x%p, Latest operation time: 0x%llx\n",
95            node->creator, node->lastAccessTime);
96 }
97 
SortQueueIndexArray(UINT32 * indexArray,UINT32 count)98 STATIC VOID SortQueueIndexArray(UINT32 *indexArray, UINT32 count)
99 {
100     LosQueueCB queueNode = {0};
101     QueueDebugCB queueDebugNode = {0};
102     UINT32 index, intSave;
103     IpcSortParam queueSortParam;
104     queueSortParam.buf = (CHAR *)g_queueDebugArray;
105     queueSortParam.ipcDebugCBSize = sizeof(QueueDebugCB);
106     queueSortParam.ipcDebugCBCnt = LOSCFG_BASE_IPC_SEM_LIMIT;
107     queueSortParam.sortElemOff = LOS_OFF_SET_OF(QueueDebugCB, lastAccessTime);
108 
109     if (count > 0) {
110         SCHEDULER_LOCK(intSave);
111         OsArraySortByTime(indexArray, 0, count - 1, &queueSortParam, QueueCompareValue);
112         SCHEDULER_UNLOCK(intSave);
113         for (index = 0; index < count; index++) {
114             SCHEDULER_LOCK(intSave);
115             (VOID)memcpy_s(&queueNode, sizeof(LosQueueCB),
116                            GET_QUEUE_HANDLE(indexArray[index]), sizeof(LosQueueCB));
117             (VOID)memcpy_s(&queueDebugNode, sizeof(QueueDebugCB),
118                            &g_queueDebugArray[indexArray[index]], sizeof(QueueDebugCB));
119             SCHEDULER_UNLOCK(intSave);
120             if (queueNode.queueState == OS_QUEUE_UNUSED) {
121                 continue;
122             }
123             OsQueueInfoOutPut(&queueNode);
124             OsQueueOpsOutput(&queueDebugNode);
125         }
126     }
127     (VOID)LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, indexArray);
128 }
129 
OsQueueCheck(VOID)130 VOID OsQueueCheck(VOID)
131 {
132     LosQueueCB queueNode = {0};
133     QueueDebugCB queueDebugNode = {0};
134     UINT32 index, intSave;
135     UINT32 count = 0;
136 
137     /*
138      * This return value does not need to be judged immediately,
139      * and the following code logic has already distinguished the return value from null and non-empty,
140      * and there is no case of accessing the null pointer.
141      */
142     UINT32 *indexArray = (UINT32 *)LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, LOSCFG_BASE_IPC_QUEUE_LIMIT * sizeof(UINT32));
143 
144     for (index = 0; index < LOSCFG_BASE_IPC_QUEUE_LIMIT; index++) {
145         SCHEDULER_LOCK(intSave);
146         (VOID)memcpy_s(&queueNode, sizeof(LosQueueCB),
147                        GET_QUEUE_HANDLE(index), sizeof(LosQueueCB));
148         (VOID)memcpy_s(&queueDebugNode, sizeof(QueueDebugCB),
149                        &g_queueDebugArray[index], sizeof(QueueDebugCB));
150         SCHEDULER_UNLOCK(intSave);
151         if ((queueNode.queueState == OS_QUEUE_UNUSED) ||
152             ((queueNode.queueState == OS_QUEUE_INUSED) && (queueDebugNode.creator == NULL))) {
153             continue;
154         }
155         if ((queueNode.queueState == OS_QUEUE_INUSED) &&
156             (queueNode.queueLen == queueNode.readWriteableCnt[OS_QUEUE_WRITE]) &&
157             LOS_ListEmpty(&queueNode.readWriteList[OS_QUEUE_READ]) &&
158             LOS_ListEmpty(&queueNode.readWriteList[OS_QUEUE_WRITE]) &&
159             LOS_ListEmpty(&queueNode.memList)) {
160             PRINTK("Queue ID <0x%x> may leak, No task uses it, "
161                    "QueueLen is 0x%x, ",
162                    queueNode.queueID,
163                    queueNode.queueLen);
164             OsQueueOpsOutput(&queueDebugNode);
165         } else {
166             if (indexArray != NULL) {
167                 *(indexArray + count) = index;
168                 count++;
169             } else {
170                 OsQueueInfoOutPut(&queueNode);
171                 OsQueueOpsOutput(&queueDebugNode);
172             }
173         }
174     }
175 
176     if (indexArray != NULL) {
177         SortQueueIndexArray(indexArray, count);
178     }
179 
180     return;
181 }
182 
183 #ifdef LOSCFG_SHELL_CMD_DEBUG
OsShellCmdQueueInfoGet(UINT32 argc,const CHAR ** argv)184 LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdQueueInfoGet(UINT32 argc, const CHAR **argv)
185 {
186     if (argc > 0) {
187         PRINTK("\nUsage: queue\n");
188         return OS_ERROR;
189     }
190     PRINTK("used queues information: \n");
191     OsQueueCheck();
192     return LOS_OK;
193 }
194 
195 SHELLCMD_ENTRY(queue_shellcmd, CMD_TYPE_EX, "queue", 0, (CmdCallBackFunc)OsShellCmdQueueInfoGet);
196 #endif /* LOSCFG_SHELL */
197 #endif /* LOSCFG_DEBUG_QUEUE */
198 
199