los_trace.c 11.9 KB
Newer Older
M
mamingshuai 已提交
1
/*
L
LiteOS2021 已提交
2
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
M
mamingshuai 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "los_trace_pri.h"
L
LiteOS2021 已提交
33 34 35
#include "trace_pipeline.h"
#include "los_memory.h"
#include "los_config.h"
M
mamingshuai 已提交
36
#include "securec.h"
L
LiteOS2021 已提交
37
#include "trace_cnv.h"
38
#include "los_init.h"
L
LiteOS2021 已提交
39
#include "los_process.h"
40
#include "los_sched_pri.h"
L
LiteOS2021 已提交
41 42 43 44

#ifdef LOSCFG_KERNEL_SMP
#include "los_mp.h"
#endif
M
mamingshuai 已提交
45 46 47 48 49 50

#ifdef LOSCFG_SHELL
#include "shcmd.h"
#include "shell.h"
#endif

L
LiteOS2021 已提交
51 52 53 54
LITE_OS_SEC_BSS STATIC UINT32 g_traceEventCount;
LITE_OS_SEC_BSS STATIC volatile enum TraceState g_traceState = TRACE_UNINIT;
LITE_OS_SEC_DATA_INIT STATIC volatile BOOL g_enableTrace = FALSE;
LITE_OS_SEC_BSS STATIC UINT32 g_traceMask = TRACE_DEFAULT_MASK;
M
mamingshuai 已提交
55

L
LiteOS2021 已提交
56 57
TRACE_EVENT_HOOK g_traceEventHook = NULL;
TRACE_DUMP_HOOK g_traceDumpHook = NULL;
M
mamingshuai 已提交
58

L
LiteOS2021 已提交
59 60 61
#ifdef LOSCFG_TRACE_CONTROL_AGENT
LITE_OS_SEC_BSS STATIC UINT32 g_traceTaskId;
#endif
M
mamingshuai 已提交
62

L
LiteOS2021 已提交
63 64
#define EVENT_MASK            0xFFFFFFF0
#define MIN(x, y)             ((x) < (y) ? (x) : (y))
M
mamingshuai 已提交
65

66
LITE_OS_SEC_BSS STATIC TRACE_HWI_FILTER_HOOK g_traceHwiFilterHook = NULL;
M
mamingshuai 已提交
67

L
LiteOS2021 已提交
68 69 70
#ifdef LOSCFG_KERNEL_SMP
LITE_OS_SEC_BSS SPIN_LOCK_INIT(g_traceSpin);
#endif
M
mamingshuai 已提交
71

L
LiteOS2021 已提交
72
STATIC_INLINE BOOL OsTraceHwiFilter(UINT32 hwiNum)
M
mamingshuai 已提交
73
{
L
LiteOS2021 已提交
74 75 76 77
    BOOL ret = ((hwiNum == NUM_HAL_INTERRUPT_UART) || (hwiNum == OS_TICK_INT_NUM));
#ifdef LOSCFG_KERNEL_SMP
    ret |= (hwiNum == LOS_MP_IPI_SCHEDULE);
#endif
78 79
    if (g_traceHwiFilterHook != NULL) {
        ret |= g_traceHwiFilterHook(hwiNum);
L
LiteOS2021 已提交
80 81
    }
    return ret;
M
mamingshuai 已提交
82 83
}

L
LiteOS2021 已提交
84 85
STATIC VOID OsTraceSetFrame(TraceEventFrame *frame, UINT32 eventType, UINTPTR identity, const UINTPTR *params,
    UINT16 paramCount)
M
mamingshuai 已提交
86
{
L
LiteOS2021 已提交
87
    INT32 i;
M
mamingshuai 已提交
88 89
    UINT32 intSave;

L
LiteOS2021 已提交
90 91 92 93 94
    (VOID)memset_s(frame, sizeof(TraceEventFrame), 0, sizeof(TraceEventFrame));

    if (paramCount > LOSCFG_TRACE_FRAME_MAX_PARAMS) {
        paramCount = LOSCFG_TRACE_FRAME_MAX_PARAMS;
    }
M
mamingshuai 已提交
95 96

    TRACE_LOCK(intSave);
L
LiteOS2021 已提交
97 98 99 100 101 102 103 104 105
    frame->curTask   = OsTraceGetMaskTid(LOS_CurTaskIDGet());
    frame->curPid    = LOS_GetCurrProcessID();
    frame->identity  = identity;
    frame->curTime   = HalClockGetCycles();
    frame->eventType = eventType;

#ifdef LOSCFG_TRACE_FRAME_CORE_MSG
    frame->core.cpuId      = ArchCurrCpuid();
    frame->core.hwiActive  = OS_INT_ACTIVE ? TRUE : FALSE;
106
    frame->core.taskLockCnt = MIN(OsSchedLockCountGet(), 0xF); /* taskLockCnt is 4 bits, max value = 0xF */
L
LiteOS2021 已提交
107 108
    frame->core.paramCount = paramCount;
#endif
M
mamingshuai 已提交
109

L
LiteOS2021 已提交
110 111
#ifdef LOS_TRACE_FRAME_LR
    /* Get the linkreg from stack fp and storage to frame */
112
    LOS_RecordLR(frame->linkReg, LOS_TRACE_LR_RECORD, LOS_TRACE_LR_RECORD, LOS_TRACE_LR_IGNORE);
L
LiteOS2021 已提交
113
#endif
M
mamingshuai 已提交
114

L
LiteOS2021 已提交
115 116 117 118
#ifdef LOSCFG_TRACE_FRAME_EVENT_COUNT
    frame->eventCount = g_traceEventCount;
    g_traceEventCount++;
#endif
M
mamingshuai 已提交
119
    TRACE_UNLOCK(intSave);
120

L
LiteOS2021 已提交
121 122 123
    for (i = 0; i < paramCount; i++) {
        frame->params[i] = params[i];
    }
M
mamingshuai 已提交
124 125
}

L
LiteOS2021 已提交
126
VOID OsTraceSetObj(ObjData *obj, const LosTaskCB *tcb)
M
mamingshuai 已提交
127
{
L
LiteOS2021 已提交
128 129 130 131 132
    errno_t ret;
    (VOID)memset_s(obj, sizeof(ObjData), 0, sizeof(ObjData));

    obj->id   = OsTraceGetMaskTid(tcb->taskID);
    obj->prio = tcb->priority;
M
mamingshuai 已提交
133

L
LiteOS2021 已提交
134 135 136
    ret = strncpy_s(obj->name, LOSCFG_TRACE_OBJ_MAX_NAME_SIZE, tcb->taskName, LOSCFG_TRACE_OBJ_MAX_NAME_SIZE - 1);
    if (ret != EOK) {
        TRACE_ERROR("Task name copy failed!\n");
M
mamingshuai 已提交
137
    }
L
LiteOS2021 已提交
138
}
M
mamingshuai 已提交
139

L
LiteOS2021 已提交
140 141 142 143 144
VOID OsTraceHook(UINT32 eventType, UINTPTR identity, const UINTPTR *params, UINT16 paramCount)
{
    TraceEventFrame frame;
    if ((eventType == TASK_CREATE) || (eventType == TASK_PRIOSET)) {
        OsTraceObjAdd(eventType, identity); /* handle important obj info, these can not be filtered */
M
mamingshuai 已提交
145 146
    }

L
LiteOS2021 已提交
147 148 149 150 151
    if ((g_enableTrace == TRUE) && (eventType & g_traceMask)) {
        UINTPTR id = identity;
        if (TRACE_GET_MODE_FLAG(eventType) == TRACE_HWI_FLAG) {
            if (OsTraceHwiFilter(identity)) {
                return;
M
mamingshuai 已提交
152
            }
L
LiteOS2021 已提交
153 154 155 156 157 158 159
        } else if (TRACE_GET_MODE_FLAG(eventType) == TRACE_TASK_FLAG) {
            id = OsTraceGetMaskTid(identity);
        } else if (eventType == MEM_INFO_REQ) {
            LOS_MEM_POOL_STATUS status;
            LOS_MemInfoGet((VOID *)identity, &status);
            LOS_TRACE(MEM_INFO, identity, status.totalUsedSize, status.totalFreeSize);
            return;
M
mamingshuai 已提交
160
        }
L
LiteOS2021 已提交
161 162 163

        OsTraceSetFrame(&frame, eventType, id, params, paramCount);
        OsTraceWriteOrSendEvent(&frame);
M
mamingshuai 已提交
164 165 166
    }
}

L
LiteOS2021 已提交
167
BOOL OsTraceIsEnable(VOID)
M
mamingshuai 已提交
168
{
L
LiteOS2021 已提交
169
    return g_enableTrace;
M
mamingshuai 已提交
170 171
}

L
LiteOS2021 已提交
172
STATIC VOID OsTraceHookInstall(VOID)
M
mamingshuai 已提交
173
{
L
LiteOS2021 已提交
174 175 176 177
    g_traceEventHook = OsTraceHook;
#ifdef LOSCFG_RECORDER_MODE_OFFLINE
    g_traceDumpHook = OsTraceRecordDump;
#endif
M
mamingshuai 已提交
178 179
}

L
LiteOS2021 已提交
180 181
#ifdef LOSCFG_TRACE_CONTROL_AGENT
STATIC BOOL OsTraceCmdIsValid(const TraceClientCmd *msg)
M
mamingshuai 已提交
182
{
L
LiteOS2021 已提交
183
    return ((msg->end == TRACE_CMD_END_CHAR) && (msg->cmd < TRACE_CMD_MAX_CODE));
M
mamingshuai 已提交
184 185
}

L
LiteOS2021 已提交
186
STATIC VOID OsTraceCmdHandle(const TraceClientCmd *msg)
M
mamingshuai 已提交
187
{
L
LiteOS2021 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    if (!OsTraceCmdIsValid(msg)) {
        return;
    }

    switch (msg->cmd) {
        case TRACE_CMD_START:
            LOS_TraceStart();
            break;
        case TRACE_CMD_STOP:
            LOS_TraceStop();
            break;
        case TRACE_CMD_SET_EVENT_MASK:
            /* 4 params(UINT8) composition the mask(UINT32) */
            LOS_TraceEventMaskSet(TRACE_MASK_COMBINE(msg->param1, msg->param2, msg->param3, msg->param4));
            break;
        case TRACE_CMD_RECODE_DUMP:
            LOS_TraceRecordDump(TRUE);
            break;
        default:
            break;
M
mamingshuai 已提交
208 209 210
    }
}

L
LiteOS2021 已提交
211
VOID TraceAgent(VOID)
M
mamingshuai 已提交
212 213
{
    UINT32 ret;
L
LiteOS2021 已提交
214
    TraceClientCmd msg;
M
mamingshuai 已提交
215

L
LiteOS2021 已提交
216 217 218 219 220 221
    while (1) {
        (VOID)memset_s(&msg, sizeof(TraceClientCmd), 0, sizeof(TraceClientCmd));
        ret = OsTraceDataWait();
        if (ret == LOS_OK) {
            OsTraceDataRecv((UINT8 *)&msg, sizeof(TraceClientCmd), 0);
            OsTraceCmdHandle(&msg);
M
mamingshuai 已提交
222 223 224 225
        }
    }
}

L
LiteOS2021 已提交
226
STATIC UINT32 OsCreateTraceAgentTask(VOID)
M
mamingshuai 已提交
227
{
L
LiteOS2021 已提交
228 229 230 231 232 233 234 235 236 237
    UINT32 ret;
    TSK_INIT_PARAM_S taskInitParam;

    (VOID)memset_s((VOID *)(&taskInitParam), sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
    taskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)TraceAgent;
    taskInitParam.usTaskPrio = LOSCFG_TRACE_TASK_PRIORITY;
    taskInitParam.pcName = "TraceAgent";
    taskInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
#ifdef LOSCFG_KERNEL_SMP
    taskInitParam.usCpuAffiMask = CPUID_TO_AFFI_MASK(ArchCurrCpuid());
M
mamingshuai 已提交
238
#endif
L
LiteOS2021 已提交
239 240
    ret = LOS_TaskCreate(&g_traceTaskId, &taskInitParam);
    return ret;
M
mamingshuai 已提交
241
}
L
LiteOS2021 已提交
242
#endif
M
mamingshuai 已提交
243

L
LiteOS2021 已提交
244
STATIC UINT32 OsTraceInit(VOID)
M
mamingshuai 已提交
245
{
L
LiteOS2021 已提交
246
    UINT32 ret;
M
mamingshuai 已提交
247

L
LiteOS2021 已提交
248 249 250 251
    if (g_traceState != TRACE_UNINIT) {
        TRACE_ERROR("trace has been initialized already, the current state is :%d\n", g_traceState);
        ret = LOS_ERRNO_TRACE_ERROR_STATUS;
        goto LOS_ERREND;
M
mamingshuai 已提交
252 253
    }

L
LiteOS2021 已提交
254 255 256 257
#ifdef LOSCFG_TRACE_CLIENT_INTERACT
    ret = OsTracePipelineInit();
    if (ret != LOS_OK) {
        goto LOS_ERREND;
M
mamingshuai 已提交
258
    }
L
LiteOS2021 已提交
259
#endif
M
mamingshuai 已提交
260

L
LiteOS2021 已提交
261 262 263 264 265
#ifdef LOSCFG_TRACE_CONTROL_AGENT
    ret = OsCreateTraceAgentTask();
    if (ret != LOS_OK) {
        TRACE_ERROR("trace init create agentTask error :0x%x\n", ret);
        goto LOS_ERREND;
M
mamingshuai 已提交
266
    }
L
LiteOS2021 已提交
267
#endif
M
mamingshuai 已提交
268

269
#ifdef LOSCFG_RECORDER_MODE_OFFLINE
L
LiteOS2021 已提交
270 271
    ret = OsTraceBufInit(LOSCFG_TRACE_BUFFER_SIZE);
    if (ret != LOS_OK) {
272 273 274 275
#ifdef LOSCFG_TRACE_CONTROL_AGENT
        (VOID)LOS_TaskDelete(g_traceTaskId);
#endif
        goto LOS_ERREND;
M
mamingshuai 已提交
276
    }
277
#endif
M
mamingshuai 已提交
278

L
LiteOS2021 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    OsTraceHookInstall();
    OsTraceCnvInit();

    g_traceEventCount = 0;

#ifdef LOSCFG_RECORDER_MODE_ONLINE  /* Wait trace client to start trace */
    g_enableTrace = FALSE;
    g_traceState = TRACE_INITED;
#else
    g_enableTrace = TRUE;
    g_traceState = TRACE_STARTED;
#endif
    return LOS_OK;
LOS_ERREND:
    return ret;
M
mamingshuai 已提交
294 295
}

L
LiteOS2021 已提交
296
UINT32 LOS_TraceStart(VOID)
M
mamingshuai 已提交
297
{
L
LiteOS2021 已提交
298 299
    UINT32 intSave;
    UINT32 ret = LOS_OK;
M
mamingshuai 已提交
300

L
LiteOS2021 已提交
301 302 303
    TRACE_LOCK(intSave);
    if (g_traceState == TRACE_STARTED) {
        goto START_END;
M
mamingshuai 已提交
304 305
    }

L
LiteOS2021 已提交
306 307 308 309
    if (g_traceState == TRACE_UNINIT) {
        TRACE_ERROR("trace not inited, be sure LOS_TraceInit excute success\n");
        ret = LOS_ERRNO_TRACE_ERROR_STATUS;
        goto START_END;
M
mamingshuai 已提交
310 311
    }

L
LiteOS2021 已提交
312
    OsTraceNotifyStart();
M
mamingshuai 已提交
313

L
LiteOS2021 已提交
314 315
    g_enableTrace = TRUE;
    g_traceState = TRACE_STARTED;
M
mamingshuai 已提交
316

L
LiteOS2021 已提交
317 318 319 320 321 322 323
    TRACE_UNLOCK(intSave);
    LOS_TRACE(MEM_INFO_REQ, m_aucSysMem0);
    return ret;
START_END:
    TRACE_UNLOCK(intSave);
    return ret;
}
M
mamingshuai 已提交
324

L
LiteOS2021 已提交
325 326 327
VOID LOS_TraceStop(VOID)
{
    UINT32 intSave;
M
mamingshuai 已提交
328

L
LiteOS2021 已提交
329 330 331
    TRACE_LOCK(intSave);
    if (g_traceState != TRACE_STARTED) {
        goto STOP_END;
M
mamingshuai 已提交
332
    }
L
LiteOS2021 已提交
333 334 335 336 337 338

    g_enableTrace = FALSE;
    g_traceState = TRACE_STOPED;
    OsTraceNotifyStop();
STOP_END:
    TRACE_UNLOCK(intSave);
M
mamingshuai 已提交
339 340
}

L
LiteOS2021 已提交
341
VOID LOS_TraceEventMaskSet(UINT32 mask)
M
mamingshuai 已提交
342
{
L
LiteOS2021 已提交
343 344
    g_traceMask = mask & EVENT_MASK;
}
M
mamingshuai 已提交
345

L
LiteOS2021 已提交
346 347 348 349 350
VOID LOS_TraceRecordDump(BOOL toClient)
{
    if (g_traceState != TRACE_STOPED) {
        TRACE_ERROR("trace dump must after trace stopped , the current state is : %d\n", g_traceState);
        return;
M
mamingshuai 已提交
351
    }
L
LiteOS2021 已提交
352 353
    OsTraceRecordDump(toClient);
}
M
mamingshuai 已提交
354

L
LiteOS2021 已提交
355 356 357
OfflineHead *LOS_TraceRecordGet(VOID)
{
    return OsTraceRecordGet();
M
mamingshuai 已提交
358 359
}

L
LiteOS2021 已提交
360
VOID LOS_TraceReset(VOID)
M
mamingshuai 已提交
361
{
L
LiteOS2021 已提交
362 363 364
    if (g_traceState == TRACE_UNINIT) {
        TRACE_ERROR("trace not inited, be sure LOS_TraceInit excute success\n");
        return;
M
mamingshuai 已提交
365
    }
L
LiteOS2021 已提交
366 367

    OsTraceReset();
M
mamingshuai 已提交
368 369
}

L
LiteOS2021 已提交
370
VOID LOS_TraceHwiFilterHookReg(TRACE_HWI_FILTER_HOOK hook)
M
mamingshuai 已提交
371
{
L
LiteOS2021 已提交
372 373 374
    UINT32 intSave;

    TRACE_LOCK(intSave);
375
    g_traceHwiFilterHook = hook;
L
LiteOS2021 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    TRACE_UNLOCK(intSave);
}

#ifdef LOSCFG_SHELL
LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdTraceSetMask(INT32 argc, const CHAR **argv)
{
    size_t mask;
    CHAR *endPtr = NULL;

    if (argc >= 2) { /* 2:Just as number of parameters */
        PRINTK("\nUsage: trace_mask or trace_mask ID\n");
        return OS_ERROR;
    }

    if (argc == 0) {
        mask = TRACE_DEFAULT_MASK;
M
mamingshuai 已提交
392
    } else {
L
LiteOS2021 已提交
393
        mask = strtoul(argv[0], &endPtr, 0);
M
mamingshuai 已提交
394
    }
L
LiteOS2021 已提交
395
    LOS_TraceEventMaskSet((UINT32)mask);
M
mamingshuai 已提交
396 397 398
    return LOS_OK;
}

L
LiteOS2021 已提交
399
LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdTraceDump(INT32 argc, const CHAR **argv)
M
mamingshuai 已提交
400
{
L
LiteOS2021 已提交
401 402 403 404 405 406 407 408 409 410
    BOOL toClient;
    CHAR *endPtr = NULL;

    if (argc >= 2) { /* 2:Just as number of parameters */
        PRINTK("\nUsage: trace_dump or trace_dump [1/0]\n");
        return OS_ERROR;
    }

    if (argc == 0) {
        toClient = FALSE;
M
mamingshuai 已提交
411
    } else {
L
LiteOS2021 已提交
412
        toClient = strtoul(argv[0], &endPtr, 0) != 0 ? TRUE : FALSE;
M
mamingshuai 已提交
413
    }
L
LiteOS2021 已提交
414
    LOS_TraceRecordDump(toClient);
M
mamingshuai 已提交
415 416 417
    return LOS_OK;
}

L
LiteOS2021 已提交
418 419 420 421 422
SHELLCMD_ENTRY(tracestart_shellcmd,   CMD_TYPE_EX, "trace_start", 0, (CmdCallBackFunc)LOS_TraceStart);
SHELLCMD_ENTRY(tracestop_shellcmd,    CMD_TYPE_EX, "trace_stop",  0, (CmdCallBackFunc)LOS_TraceStop);
SHELLCMD_ENTRY(tracesetmask_shellcmd, CMD_TYPE_EX, "trace_mask",  1, (CmdCallBackFunc)OsShellCmdTraceSetMask);
SHELLCMD_ENTRY(tracereset_shellcmd,   CMD_TYPE_EX, "trace_reset", 0, (CmdCallBackFunc)LOS_TraceReset);
SHELLCMD_ENTRY(tracedump_shellcmd,    CMD_TYPE_EX, "trace_dump", 1, (CmdCallBackFunc)OsShellCmdTraceDump);
M
mamingshuai 已提交
423 424
#endif

L
LiteOS2021 已提交
425
LOS_MODULE_INIT(OsTraceInit, LOS_INIT_LEVEL_KMOD_EXTENDED);