los_trace.c 11.8 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 40 41 42 43
#include "los_process.h"

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

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

L
LiteOS2021 已提交
50 51 52 53
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 已提交
54

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

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

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

L
LiteOS2021 已提交
65
LITE_OS_SEC_BSS STATIC TRACE_HWI_FILTER_HOOK g_traceHwiFliterHook = NULL;
M
mamingshuai 已提交
66

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

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

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

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

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

    TRACE_LOCK(intSave);
L
LiteOS2021 已提交
96 97 98 99 100 101 102 103 104 105 106 107
    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;
    frame->core.taskLockCnt = MIN(OsPercpuGet()->taskLockCnt, 0xF); /* taskLockCnt is 4 bits, max vaule = 0xF */
    frame->core.paramCount = paramCount;
#endif
M
mamingshuai 已提交
108

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

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

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

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

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

L
LiteOS2021 已提交
133 134 135
    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 已提交
136
    }
L
LiteOS2021 已提交
137
}
M
mamingshuai 已提交
138

L
LiteOS2021 已提交
139 140 141 142 143
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 已提交
144 145
    }

L
LiteOS2021 已提交
146 147 148 149 150
    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 已提交
151
            }
L
LiteOS2021 已提交
152 153 154 155 156 157 158
        } 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 已提交
159
        }
L
LiteOS2021 已提交
160 161 162

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

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

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

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

L
LiteOS2021 已提交
185
STATIC VOID OsTraceCmdHandle(const TraceClientCmd *msg)
M
mamingshuai 已提交
186
{
L
LiteOS2021 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    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 已提交
207 208 209
    }
}

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

L
LiteOS2021 已提交
215 216 217 218 219 220
    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 已提交
221 222 223 224
        }
    }
}

L
LiteOS2021 已提交
225
STATIC UINT32 OsCreateTraceAgentTask(VOID)
M
mamingshuai 已提交
226
{
L
LiteOS2021 已提交
227 228 229 230 231 232 233 234 235 236
    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 已提交
237
#endif
L
LiteOS2021 已提交
238 239
    ret = LOS_TaskCreate(&g_traceTaskId, &taskInitParam);
    return ret;
M
mamingshuai 已提交
240
}
L
LiteOS2021 已提交
241
#endif
M
mamingshuai 已提交
242

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

L
LiteOS2021 已提交
247 248 249 250
    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 已提交
251 252
    }

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

L
LiteOS2021 已提交
260 261 262 263 264
#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 已提交
265
    }
L
LiteOS2021 已提交
266
#endif
M
mamingshuai 已提交
267

L
LiteOS2021 已提交
268 269 270
    ret = OsTraceBufInit(LOSCFG_TRACE_BUFFER_SIZE);
    if (ret != LOS_OK) {
        goto LOS_RELEASE;
M
mamingshuai 已提交
271 272
    }

L
LiteOS2021 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    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_RELEASE:
#ifdef LOSCFG_TRACE_CONTROL_AGENT
    LOS_TaskDelete(g_traceTaskId);
#endif
LOS_ERREND:
    return ret;
M
mamingshuai 已提交
292 293
}

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

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

L
LiteOS2021 已提交
304 305 306 307
    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 已提交
308 309
    }

L
LiteOS2021 已提交
310
    OsTraceNotifyStart();
M
mamingshuai 已提交
311

L
LiteOS2021 已提交
312 313
    g_enableTrace = TRUE;
    g_traceState = TRACE_STARTED;
M
mamingshuai 已提交
314

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

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

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

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

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

L
LiteOS2021 已提交
344 345 346 347 348
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 已提交
349
    }
L
LiteOS2021 已提交
350 351
    OsTraceRecordDump(toClient);
}
M
mamingshuai 已提交
352

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

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

    OsTraceReset();
M
mamingshuai 已提交
366 367
}

L
LiteOS2021 已提交
368
VOID LOS_TraceHwiFilterHookReg(TRACE_HWI_FILTER_HOOK hook)
M
mamingshuai 已提交
369
{
L
LiteOS2021 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    UINT32 intSave;

    TRACE_LOCK(intSave);
    g_traceHwiFliterHook = hook;
    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 已提交
390
    } else {
L
LiteOS2021 已提交
391
        mask = strtoul(argv[0], &endPtr, 0);
M
mamingshuai 已提交
392
    }
L
LiteOS2021 已提交
393
    LOS_TraceEventMaskSet((UINT32)mask);
M
mamingshuai 已提交
394 395 396
    return LOS_OK;
}

L
LiteOS2021 已提交
397
LITE_OS_SEC_TEXT_MINOR UINT32 OsShellCmdTraceDump(INT32 argc, const CHAR **argv)
M
mamingshuai 已提交
398
{
L
LiteOS2021 已提交
399 400 401 402 403 404 405 406 407 408
    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 已提交
409
    } else {
L
LiteOS2021 已提交
410
        toClient = strtoul(argv[0], &endPtr, 0) != 0 ? TRUE : FALSE;
M
mamingshuai 已提交
411
    }
L
LiteOS2021 已提交
412
    LOS_TraceRecordDump(toClient);
M
mamingshuai 已提交
413 414 415
    return LOS_OK;
}

L
LiteOS2021 已提交
416 417 418 419 420
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 已提交
421 422
#endif

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