10d163575Sopenharmony_ci/* 20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 40d163575Sopenharmony_ci * 50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 60d163575Sopenharmony_ci * are permitted provided that the following conditions are met: 70d163575Sopenharmony_ci * 80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 90d163575Sopenharmony_ci * conditions and the following disclaimer. 100d163575Sopenharmony_ci * 110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 120d163575Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 130d163575Sopenharmony_ci * provided with the distribution. 140d163575Sopenharmony_ci * 150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 160d163575Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 170d163575Sopenharmony_ci * permission. 180d163575Sopenharmony_ci * 190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 300d163575Sopenharmony_ci */ 310d163575Sopenharmony_ci 320d163575Sopenharmony_ci#include "los_base.h" 330d163575Sopenharmony_ci#ifdef LOSCFG_LIB_LIBC 340d163575Sopenharmony_ci#include "stdlib.h" 350d163575Sopenharmony_ci#include "unistd.h" 360d163575Sopenharmony_ci#endif 370d163575Sopenharmony_ci#include "los_hwi.h" 380d163575Sopenharmony_ci#include "los_memory_pri.h" 390d163575Sopenharmony_ci#include "los_process_pri.h" 400d163575Sopenharmony_ci#ifdef LOSCFG_FS_VFS 410d163575Sopenharmony_ci#include "console.h" 420d163575Sopenharmony_ci#endif 430d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_DMESG 440d163575Sopenharmony_ci#include "dmesg_pri.h" 450d163575Sopenharmony_ci#endif 460d163575Sopenharmony_ci#ifdef LOSCFG_SAVE_EXCINFO 470d163575Sopenharmony_ci#include "los_excinfo_pri.h" 480d163575Sopenharmony_ci#endif 490d163575Sopenharmony_ci#include "los_exc_pri.h" 500d163575Sopenharmony_ci#include "los_sched_pri.h" 510d163575Sopenharmony_ci 520d163575Sopenharmony_ci#define SIZEBUF 256 530d163575Sopenharmony_ci 540d163575Sopenharmony_ciconst CHAR *g_logString[] = { 550d163575Sopenharmony_ci "EMG", 560d163575Sopenharmony_ci "COMMON", 570d163575Sopenharmony_ci "ERR", 580d163575Sopenharmony_ci "WARN", 590d163575Sopenharmony_ci "INFO", 600d163575Sopenharmony_ci "DEBUG", 610d163575Sopenharmony_ci "TRACE" 620d163575Sopenharmony_ci}; 630d163575Sopenharmony_ci 640d163575Sopenharmony_ciconst CHAR *OsLogLvGet(INT32 level) 650d163575Sopenharmony_ci{ 660d163575Sopenharmony_ci return g_logString[level]; 670d163575Sopenharmony_ci} 680d163575Sopenharmony_ci 690d163575Sopenharmony_ciSTATIC VOID ErrorMsg(VOID) 700d163575Sopenharmony_ci{ 710d163575Sopenharmony_ci const CHAR *p = "Output illegal string! vsnprintf_s failed!\n"; 720d163575Sopenharmony_ci UartPuts(p, (UINT32)strlen(p), UART_WITH_LOCK); 730d163575Sopenharmony_ci} 740d163575Sopenharmony_ci 750d163575Sopenharmony_ciSTATIC VOID UartOutput(const CHAR *str, UINT32 len, BOOL isLock) 760d163575Sopenharmony_ci{ 770d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_DMESG 780d163575Sopenharmony_ci if (!OsCheckUartLock()) { 790d163575Sopenharmony_ci UartPuts(str, len, isLock); 800d163575Sopenharmony_ci } 810d163575Sopenharmony_ci if (isLock != UART_WITHOUT_LOCK) { 820d163575Sopenharmony_ci (VOID)OsLogMemcpyRecord(str, len); 830d163575Sopenharmony_ci } 840d163575Sopenharmony_ci#else 850d163575Sopenharmony_ci UartPuts(str, len, isLock); 860d163575Sopenharmony_ci#endif 870d163575Sopenharmony_ci} 880d163575Sopenharmony_ci 890d163575Sopenharmony_ci#ifdef LOSCFG_PLATFORM_CONSOLE 900d163575Sopenharmony_ciSTATIC VOID ConsoleOutput(const CHAR *str, UINT32 len) 910d163575Sopenharmony_ci{ 920d163575Sopenharmony_ci ssize_t written = 0; 930d163575Sopenharmony_ci ssize_t cnt; 940d163575Sopenharmony_ci ssize_t toWrite = len; 950d163575Sopenharmony_ci 960d163575Sopenharmony_ci for (;;) { 970d163575Sopenharmony_ci cnt = write(STDOUT_FILENO, str + written, (size_t)toWrite); 980d163575Sopenharmony_ci if ((cnt < 0) || ((cnt == 0) && ((!OsPreemptable()) || (OS_INT_ACTIVE))) || (toWrite == cnt)) { 990d163575Sopenharmony_ci break; 1000d163575Sopenharmony_ci } 1010d163575Sopenharmony_ci written += cnt; 1020d163575Sopenharmony_ci toWrite -= cnt; 1030d163575Sopenharmony_ci } 1040d163575Sopenharmony_ci} 1050d163575Sopenharmony_ci#endif 1060d163575Sopenharmony_ci 1070d163575Sopenharmony_ciVOID OutputControl(const CHAR *str, UINT32 len, OutputType type) 1080d163575Sopenharmony_ci{ 1090d163575Sopenharmony_ci switch (type) { 1100d163575Sopenharmony_ci case CONSOLE_OUTPUT: 1110d163575Sopenharmony_ci#ifdef LOSCFG_PLATFORM_CONSOLE 1120d163575Sopenharmony_ci if (ConsoleEnable() == TRUE) { 1130d163575Sopenharmony_ci ConsoleOutput(str, len); 1140d163575Sopenharmony_ci break; 1150d163575Sopenharmony_ci } 1160d163575Sopenharmony_ci#endif 1170d163575Sopenharmony_ci /* fall-through */ 1180d163575Sopenharmony_ci case UART_OUTPUT: 1190d163575Sopenharmony_ci UartOutput(str, len, UART_WITH_LOCK); 1200d163575Sopenharmony_ci break; 1210d163575Sopenharmony_ci case EXC_OUTPUT: 1220d163575Sopenharmony_ci UartPuts(str, len, UART_WITH_LOCK); 1230d163575Sopenharmony_ci break; 1240d163575Sopenharmony_ci default: 1250d163575Sopenharmony_ci break; 1260d163575Sopenharmony_ci } 1270d163575Sopenharmony_ci return; 1280d163575Sopenharmony_ci} 1290d163575Sopenharmony_ci 1300d163575Sopenharmony_ciSTATIC VOID OsVprintfFree(CHAR *buf, UINT32 bufLen) 1310d163575Sopenharmony_ci{ 1320d163575Sopenharmony_ci if (bufLen != SIZEBUF) { 1330d163575Sopenharmony_ci (VOID)LOS_MemFree(m_aucSysMem0, buf); 1340d163575Sopenharmony_ci } 1350d163575Sopenharmony_ci} 1360d163575Sopenharmony_ci 1370d163575Sopenharmony_ciVOID OsVprintf(const CHAR *fmt, va_list ap, OutputType type) 1380d163575Sopenharmony_ci{ 1390d163575Sopenharmony_ci INT32 len; 1400d163575Sopenharmony_ci const CHAR *errMsgMalloc = "OsVprintf, malloc failed!\n"; 1410d163575Sopenharmony_ci const CHAR *errMsgLen = "OsVprintf, length overflow!\n"; 1420d163575Sopenharmony_ci CHAR aBuf[SIZEBUF] = {0}; 1430d163575Sopenharmony_ci CHAR *bBuf = NULL; 1440d163575Sopenharmony_ci UINT32 bufLen = SIZEBUF; 1450d163575Sopenharmony_ci UINT32 systemStatus; 1460d163575Sopenharmony_ci 1470d163575Sopenharmony_ci bBuf = aBuf; 1480d163575Sopenharmony_ci len = vsnprintf_s(bBuf, bufLen, bufLen - 1, fmt, ap); 1490d163575Sopenharmony_ci if ((len == -1) && (*bBuf == '\0')) { 1500d163575Sopenharmony_ci /* parameter is illegal or some features in fmt dont support */ 1510d163575Sopenharmony_ci ErrorMsg(); 1520d163575Sopenharmony_ci return; 1530d163575Sopenharmony_ci } 1540d163575Sopenharmony_ci 1550d163575Sopenharmony_ci while (len == -1) { 1560d163575Sopenharmony_ci /* bBuf is not enough */ 1570d163575Sopenharmony_ci OsVprintfFree(bBuf, bufLen); 1580d163575Sopenharmony_ci 1590d163575Sopenharmony_ci bufLen = bufLen << 1; 1600d163575Sopenharmony_ci if ((INT32)bufLen <= 0) { 1610d163575Sopenharmony_ci UartPuts(errMsgLen, (UINT32)strlen(errMsgLen), UART_WITH_LOCK); 1620d163575Sopenharmony_ci return; 1630d163575Sopenharmony_ci } 1640d163575Sopenharmony_ci bBuf = (CHAR *)LOS_MemAlloc(m_aucSysMem0, bufLen); 1650d163575Sopenharmony_ci if (bBuf == NULL) { 1660d163575Sopenharmony_ci UartPuts(errMsgMalloc, (UINT32)strlen(errMsgMalloc), UART_WITH_LOCK); 1670d163575Sopenharmony_ci return; 1680d163575Sopenharmony_ci } 1690d163575Sopenharmony_ci len = vsnprintf_s(bBuf, bufLen, bufLen - 1, fmt, ap); 1700d163575Sopenharmony_ci if (*bBuf == '\0') { 1710d163575Sopenharmony_ci /* parameter is illegal or some features in fmt dont support */ 1720d163575Sopenharmony_ci (VOID)LOS_MemFree(m_aucSysMem0, bBuf); 1730d163575Sopenharmony_ci ErrorMsg(); 1740d163575Sopenharmony_ci return; 1750d163575Sopenharmony_ci } 1760d163575Sopenharmony_ci } 1770d163575Sopenharmony_ci *(bBuf + len) = '\0'; 1780d163575Sopenharmony_ci 1790d163575Sopenharmony_ci systemStatus = OsGetSystemStatus(); 1800d163575Sopenharmony_ci if ((systemStatus == OS_SYSTEM_NORMAL) || (systemStatus == OS_SYSTEM_EXC_OTHER_CPU)) { 1810d163575Sopenharmony_ci OutputControl(bBuf, len, type); 1820d163575Sopenharmony_ci } else if (systemStatus == OS_SYSTEM_EXC_CURR_CPU) { 1830d163575Sopenharmony_ci OutputControl(bBuf, len, EXC_OUTPUT); 1840d163575Sopenharmony_ci } 1850d163575Sopenharmony_ci OsVprintfFree(bBuf, bufLen); 1860d163575Sopenharmony_ci} 1870d163575Sopenharmony_ci 1880d163575Sopenharmony_ciVOID UartVprintf(const CHAR *fmt, va_list ap) 1890d163575Sopenharmony_ci{ 1900d163575Sopenharmony_ci OsVprintf(fmt, ap, UART_OUTPUT); 1910d163575Sopenharmony_ci} 1920d163575Sopenharmony_ci 1930d163575Sopenharmony_ci__attribute__((noinline)) VOID UartPrintf(const CHAR *fmt, ...) 1940d163575Sopenharmony_ci{ 1950d163575Sopenharmony_ci va_list ap; 1960d163575Sopenharmony_ci va_start(ap, fmt); 1970d163575Sopenharmony_ci OsVprintf(fmt, ap, UART_OUTPUT); 1980d163575Sopenharmony_ci va_end(ap); 1990d163575Sopenharmony_ci} 2000d163575Sopenharmony_ci 2010d163575Sopenharmony_ci#ifndef LOSCFG_LIBC_NEWLIB 2020d163575Sopenharmony_ci__attribute__((noinline)) VOID dprintf(const CHAR *fmt, ...) 2030d163575Sopenharmony_ci{ 2040d163575Sopenharmony_ci va_list ap; 2050d163575Sopenharmony_ci va_start(ap, fmt); 2060d163575Sopenharmony_ci OsVprintf(fmt, ap, CONSOLE_OUTPUT); 2070d163575Sopenharmony_ci#ifdef LOSCFG_SAVE_EXCINFO 2080d163575Sopenharmony_ci if (OsGetSystemStatus() == OS_SYSTEM_EXC_CURR_CPU) { 2090d163575Sopenharmony_ci WriteExcBufVa(fmt, ap); 2100d163575Sopenharmony_ci } 2110d163575Sopenharmony_ci#endif 2120d163575Sopenharmony_ci va_end(ap); 2130d163575Sopenharmony_ci} 2140d163575Sopenharmony_ci#endif 2150d163575Sopenharmony_ci 2160d163575Sopenharmony_ciVOID LkDprintf(const CHAR *fmt, va_list ap) 2170d163575Sopenharmony_ci{ 2180d163575Sopenharmony_ci OsVprintf(fmt, ap, CONSOLE_OUTPUT); 2190d163575Sopenharmony_ci#ifdef LOSCFG_SAVE_EXCINFO 2200d163575Sopenharmony_ci if (OsGetSystemStatus() == OS_SYSTEM_EXC_CURR_CPU) { 2210d163575Sopenharmony_ci WriteExcBufVa(fmt, ap); 2220d163575Sopenharmony_ci } 2230d163575Sopenharmony_ci#endif 2240d163575Sopenharmony_ci} 2250d163575Sopenharmony_ci 2260d163575Sopenharmony_ci#ifdef LOSCFG_SHELL_DMESG 2270d163575Sopenharmony_ciVOID DmesgPrintf(const CHAR *fmt, va_list ap) 2280d163575Sopenharmony_ci{ 2290d163575Sopenharmony_ci OsVprintf(fmt, ap, CONSOLE_OUTPUT); 2300d163575Sopenharmony_ci} 2310d163575Sopenharmony_ci#endif 2320d163575Sopenharmony_ci 2330d163575Sopenharmony_ci#ifdef LOSCFG_PLATFORM_UART_WITHOUT_VFS 2340d163575Sopenharmony_ci__attribute__((noinline)) INT32 printf(const CHAR *fmt, ...) 2350d163575Sopenharmony_ci{ 2360d163575Sopenharmony_ci va_list ap; 2370d163575Sopenharmony_ci va_start(ap, fmt); 2380d163575Sopenharmony_ci OsVprintf(fmt, ap, UART_OUTPUT); 2390d163575Sopenharmony_ci va_end(ap); 2400d163575Sopenharmony_ci return 0; 2410d163575Sopenharmony_ci} 2420d163575Sopenharmony_ci#endif 2430d163575Sopenharmony_ci 2440d163575Sopenharmony_ci__attribute__((noinline)) VOID syslog(INT32 level, const CHAR *fmt, ...) 2450d163575Sopenharmony_ci{ 2460d163575Sopenharmony_ci va_list ap; 2470d163575Sopenharmony_ci va_start(ap, fmt); 2480d163575Sopenharmony_ci OsVprintf(fmt, ap, CONSOLE_OUTPUT); 2490d163575Sopenharmony_ci va_end(ap); 2500d163575Sopenharmony_ci (VOID)level; 2510d163575Sopenharmony_ci} 2520d163575Sopenharmony_ci 2530d163575Sopenharmony_ci__attribute__((noinline)) VOID ExcPrintf(const CHAR *fmt, ...) 2540d163575Sopenharmony_ci{ 2550d163575Sopenharmony_ci va_list ap; 2560d163575Sopenharmony_ci va_start(ap, fmt); 2570d163575Sopenharmony_ci /* uart output without print-spinlock */ 2580d163575Sopenharmony_ci OsVprintf(fmt, ap, EXC_OUTPUT); 2590d163575Sopenharmony_ci va_end(ap); 2600d163575Sopenharmony_ci} 2610d163575Sopenharmony_ci 2620d163575Sopenharmony_ciVOID PrintExcInfo(const CHAR *fmt, ...) 2630d163575Sopenharmony_ci{ 2640d163575Sopenharmony_ci va_list ap; 2650d163575Sopenharmony_ci va_start(ap, fmt); 2660d163575Sopenharmony_ci /* uart output without print-spinlock */ 2670d163575Sopenharmony_ci OsVprintf(fmt, ap, EXC_OUTPUT); 2680d163575Sopenharmony_ci#ifdef LOSCFG_SAVE_EXCINFO 2690d163575Sopenharmony_ci WriteExcBufVa(fmt, ap); 2700d163575Sopenharmony_ci#endif 2710d163575Sopenharmony_ci va_end(ap); 2720d163575Sopenharmony_ci} 2730d163575Sopenharmony_ci 2740d163575Sopenharmony_ci#ifndef LOSCFG_SHELL_LK 2750d163575Sopenharmony_ciVOID LOS_LkPrint(INT32 level, const CHAR *func, INT32 line, const CHAR *fmt, ...) 2760d163575Sopenharmony_ci{ 2770d163575Sopenharmony_ci va_list ap; 2780d163575Sopenharmony_ci 2790d163575Sopenharmony_ci if (level > PRINT_LEVEL) { 2800d163575Sopenharmony_ci return; 2810d163575Sopenharmony_ci } 2820d163575Sopenharmony_ci 2830d163575Sopenharmony_ci if ((level != LOS_COMMON_LEVEL) && ((level > LOS_EMG_LEVEL) && (level <= LOS_TRACE_LEVEL))) { 2840d163575Sopenharmony_ci PRINTK("[%s][%s:%s]", g_logString[level], 2850d163575Sopenharmony_ci ((OsCurrProcessGet() == NULL) ? "NULL" : OsCurrProcessGet()->processName), 2860d163575Sopenharmony_ci ((OsCurrTaskGet() == NULL) ? "NULL" : OsCurrTaskGet()->taskName)); 2870d163575Sopenharmony_ci } 2880d163575Sopenharmony_ci 2890d163575Sopenharmony_ci va_start(ap, fmt); 2900d163575Sopenharmony_ci OsVprintf(fmt, ap, CONSOLE_OUTPUT); 2910d163575Sopenharmony_ci#ifdef LOSCFG_SAVE_EXCINFO 2920d163575Sopenharmony_ci if (OsGetSystemStatus() == OS_SYSTEM_EXC_CURR_CPU) { 2930d163575Sopenharmony_ci WriteExcBufVa(fmt, ap); 2940d163575Sopenharmony_ci } 2950d163575Sopenharmony_ci#endif 2960d163575Sopenharmony_ci va_end(ap); 2970d163575Sopenharmony_ci} 2980d163575Sopenharmony_ci#endif 2990d163575Sopenharmony_ci 300