1 /* 2 * Copyright (c) 2013-2026, Arm Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef DEBUG_H 8 #define DEBUG_H 9 10 #include <lib/utils_def.h> 11 12 /* 13 * The log output macros print output to the console. These macros produce 14 * compiled log output only if the LOG_LEVEL defined in the makefile (or the 15 * make command line) is greater or equal than the level required for that 16 * type of log output. 17 * 18 * The format expected is the same as for printf(). For example: 19 * INFO("Info %s.\n", "message") -> INFO: Info message. 20 * WARN("Warning %s.\n", "message") -> WARNING: Warning message. 21 */ 22 23 #define LOG_LEVEL_NONE U(0) 24 #define LOG_LEVEL_ERROR U(10) 25 #define LOG_LEVEL_NOTICE U(20) 26 #define LOG_LEVEL_WARNING U(30) 27 #define LOG_LEVEL_INFO U(40) 28 #define LOG_LEVEL_VERBOSE U(50) 29 /* 30 * LOG_DEBUG is another file level build flag. This build flag can be used to 31 * hide module internal detail debug logs.These detail logs may not be required 32 * even when LOG_LEVEL is VERBOSE. In such case, hide detail logs under this 33 * file level build flag and enable it when module debugging is required. 34 */ 35 36 #ifndef __ASSEMBLER__ 37 38 #include <cdefs.h> 39 #include <stdarg.h> 40 #include <stdbool.h> 41 #include <stdint.h> 42 #include <stdio.h> 43 44 #include <drivers/console.h> 45 46 /* 47 * Define Log Markers corresponding to each log level which will 48 * be embedded in the format string and is expected by tf_log() to determine 49 * the log level. 50 */ 51 #define LOG_MARKER_ERROR "\xa" /* 10 */ 52 #define LOG_MARKER_NOTICE "\x14" /* 20 */ 53 #define LOG_MARKER_WARNING "\x1e" /* 30 */ 54 #define LOG_MARKER_INFO "\x28" /* 40 */ 55 #define LOG_MARKER_VERBOSE "\x32" /* 50 */ 56 57 /* 58 * If the log output is too low then this macro is used in place of tf_log() 59 * below. The intent is to get the compiler to evaluate the function call for 60 * type checking and format specifier correctness but let it optimize it out. 61 */ 62 #define no_tf_log(fmt, ...) \ 63 do { \ 64 if (false) { \ 65 tf_log(fmt, ##__VA_ARGS__); \ 66 } \ 67 } while (false) 68 69 #if LOG_LEVEL >= LOG_LEVEL_ERROR 70 # define ERROR(...) tf_log(LOG_MARKER_ERROR __VA_ARGS__) 71 # define ERROR_NL() tf_log_newline(LOG_MARKER_ERROR) 72 #else 73 # define ERROR(...) no_tf_log(LOG_MARKER_ERROR __VA_ARGS__) 74 # define ERROR_NL() 75 #endif 76 77 #if LOG_LEVEL >= LOG_LEVEL_NOTICE 78 # define NOTICE(...) tf_log(LOG_MARKER_NOTICE __VA_ARGS__) 79 #else 80 # define NOTICE(...) no_tf_log(LOG_MARKER_NOTICE __VA_ARGS__) 81 #endif 82 83 #if LOG_LEVEL >= LOG_LEVEL_WARNING 84 # define WARN(...) tf_log(LOG_MARKER_WARNING __VA_ARGS__) 85 #else 86 # define WARN(...) no_tf_log(LOG_MARKER_WARNING __VA_ARGS__) 87 #endif 88 89 #if LOG_LEVEL >= LOG_LEVEL_INFO 90 # define INFO(...) tf_log(LOG_MARKER_INFO __VA_ARGS__) 91 #else 92 # define INFO(...) no_tf_log(LOG_MARKER_INFO __VA_ARGS__) 93 #endif 94 95 #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 96 # define VERBOSE(...) tf_log(LOG_MARKER_VERBOSE __VA_ARGS__) 97 #else 98 # define VERBOSE(...) no_tf_log(LOG_MARKER_VERBOSE __VA_ARGS__) 99 #endif 100 101 #if EARLY_CONSOLE 102 #define EARLY_ERROR(...) ERROR(__VA_ARGS__) 103 #else /* !EARLY_CONSOLE */ 104 #define EARLY_ERROR(...) no_tf_log(LOG_MARKER_ERROR __VA_ARGS__) 105 #endif /* EARLY_CONSOLE */ 106 107 const char *get_el_str(unsigned int el); 108 const char *get_mode_str(unsigned int spsr_mode); 109 110 #if ENABLE_BACKTRACE 111 void backtrace(const char *cookie); 112 #else 113 #define backtrace(x) 114 #endif 115 116 void __dead2 el3_panic(void); 117 118 #define panic() \ 119 do { \ 120 backtrace(__func__); \ 121 console_flush(); \ 122 el3_panic(); \ 123 } while (false) 124 125 /* Function called when stack protection check code detects a corrupted stack */ 126 void __dead2 __stack_chk_fail(void); 127 128 void tf_log(const char *fmt, ...) __printflike(1, 2); 129 void tf_log_newline(const char log_fmt[2]); 130 void tf_log_set_max_level(uint32_t log_level); 131 132 #endif /* __ASSEMBLER__ */ 133 #endif /* DEBUG_H */ 134