14ecca339SDan Handley /* 2cf24229eSSandrine Bailleux * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved. 34ecca339SDan Handley * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 54ecca339SDan Handley */ 64ecca339SDan Handley 7870ce3ddSAntonio Nino Diaz #ifndef DEBUG_H 8870ce3ddSAntonio Nino Diaz #define DEBUG_H 94ecca339SDan Handley 10*09d40e0eSAntonio Nino Diaz #include <lib/utils_def.h> 115a22e461SAntonio Nino Diaz 12cf24229eSSandrine Bailleux /* 13cf24229eSSandrine Bailleux * The log output macros print output to the console. These macros produce 14289c28a8SDan Handley * compiled log output only if the LOG_LEVEL defined in the makefile (or the 15289c28a8SDan Handley * make command line) is greater or equal than the level required for that 16289c28a8SDan Handley * type of log output. 17cf24229eSSandrine Bailleux * 18289c28a8SDan Handley * The format expected is the same as for printf(). For example: 194ecca339SDan Handley * INFO("Info %s.\n", "message") -> INFO: Info message. 20289c28a8SDan Handley * WARN("Warning %s.\n", "message") -> WARNING: Warning message. 214ecca339SDan Handley */ 22289c28a8SDan Handley 235a22e461SAntonio Nino Diaz #define LOG_LEVEL_NONE U(0) 245a22e461SAntonio Nino Diaz #define LOG_LEVEL_ERROR U(10) 255a22e461SAntonio Nino Diaz #define LOG_LEVEL_NOTICE U(20) 265a22e461SAntonio Nino Diaz #define LOG_LEVEL_WARNING U(30) 275a22e461SAntonio Nino Diaz #define LOG_LEVEL_INFO U(40) 285a22e461SAntonio Nino Diaz #define LOG_LEVEL_VERBOSE U(50) 29289c28a8SDan Handley 301319e7b1SSoby Mathew #ifndef __ASSEMBLY__ 31*09d40e0eSAntonio Nino Diaz 3293c78ed2SAntonio Nino Diaz #include <cdefs.h> 332d7e8282SSoby Mathew #include <stdarg.h> 343e530d8eSAntonio Nino Diaz #include <stdbool.h> 351319e7b1SSoby Mathew #include <stdio.h> 36289c28a8SDan Handley 37*09d40e0eSAntonio Nino Diaz #include <drivers/console.h> 38*09d40e0eSAntonio Nino Diaz 397f56e9a3SSoby Mathew /* 407f56e9a3SSoby Mathew * Define Log Markers corresponding to each log level which will 417f56e9a3SSoby Mathew * be embedded in the format string and is expected by tf_log() to determine 427f56e9a3SSoby Mathew * the log level. 437f56e9a3SSoby Mathew */ 447f56e9a3SSoby Mathew #define LOG_MARKER_ERROR "\xa" /* 10 */ 457f56e9a3SSoby Mathew #define LOG_MARKER_NOTICE "\x14" /* 20 */ 467f56e9a3SSoby Mathew #define LOG_MARKER_WARNING "\x1e" /* 30 */ 477f56e9a3SSoby Mathew #define LOG_MARKER_INFO "\x28" /* 40 */ 487f56e9a3SSoby Mathew #define LOG_MARKER_VERBOSE "\x32" /* 50 */ 497f56e9a3SSoby Mathew 50cf24229eSSandrine Bailleux /* 51cf24229eSSandrine Bailleux * If the log output is too low then this macro is used in place of tf_log() 52cf24229eSSandrine Bailleux * below. The intent is to get the compiler to evaluate the function call for 53cf24229eSSandrine Bailleux * type checking and format specifier correctness but let it optimize it out. 54cf24229eSSandrine Bailleux */ 55cf24229eSSandrine Bailleux #define no_tf_log(fmt, ...) \ 56cf24229eSSandrine Bailleux do { \ 575a22e461SAntonio Nino Diaz if (false) { \ 58cf24229eSSandrine Bailleux tf_log(fmt, ##__VA_ARGS__); \ 59cf24229eSSandrine Bailleux } \ 605a22e461SAntonio Nino Diaz } while (false) 61cf24229eSSandrine Bailleux 62289c28a8SDan Handley #if LOG_LEVEL >= LOG_LEVEL_ERROR 637f56e9a3SSoby Mathew # define ERROR(...) tf_log(LOG_MARKER_ERROR __VA_ARGS__) 64289c28a8SDan Handley #else 65cf24229eSSandrine Bailleux # define ERROR(...) no_tf_log(LOG_MARKER_ERROR __VA_ARGS__) 66289c28a8SDan Handley #endif 67289c28a8SDan Handley 68701b498cSJohn Tsichritzis #if LOG_LEVEL >= LOG_LEVEL_NOTICE 69701b498cSJohn Tsichritzis # define NOTICE(...) tf_log(LOG_MARKER_NOTICE __VA_ARGS__) 70701b498cSJohn Tsichritzis #else 71701b498cSJohn Tsichritzis # define NOTICE(...) no_tf_log(LOG_MARKER_NOTICE __VA_ARGS__) 72701b498cSJohn Tsichritzis #endif 73701b498cSJohn Tsichritzis 74289c28a8SDan Handley #if LOG_LEVEL >= LOG_LEVEL_WARNING 757f56e9a3SSoby Mathew # define WARN(...) tf_log(LOG_MARKER_WARNING __VA_ARGS__) 76289c28a8SDan Handley #else 77cf24229eSSandrine Bailleux # define WARN(...) no_tf_log(LOG_MARKER_WARNING __VA_ARGS__) 784ecca339SDan Handley #endif 794ecca339SDan Handley 80289c28a8SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO 817f56e9a3SSoby Mathew # define INFO(...) tf_log(LOG_MARKER_INFO __VA_ARGS__) 82289c28a8SDan Handley #else 83cf24229eSSandrine Bailleux # define INFO(...) no_tf_log(LOG_MARKER_INFO __VA_ARGS__) 84289c28a8SDan Handley #endif 85289c28a8SDan Handley 86289c28a8SDan Handley #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 877f56e9a3SSoby Mathew # define VERBOSE(...) tf_log(LOG_MARKER_VERBOSE __VA_ARGS__) 88289c28a8SDan Handley #else 89cf24229eSSandrine Bailleux # define VERBOSE(...) no_tf_log(LOG_MARKER_VERBOSE __VA_ARGS__) 90289c28a8SDan Handley #endif 91289c28a8SDan Handley 920c62883fSDouglas Raillard #if ENABLE_BACKTRACE 930c62883fSDouglas Raillard void backtrace(const char *cookie); 940c62883fSDouglas Raillard #else 950c62883fSDouglas Raillard #define backtrace(x) 960c62883fSDouglas Raillard #endif 970c62883fSDouglas Raillard 98c6bc0710SDan Handley void __dead2 do_panic(void); 993e530d8eSAntonio Nino Diaz 1003e530d8eSAntonio Nino Diaz #define panic() \ 1013e530d8eSAntonio Nino Diaz do { \ 1023e530d8eSAntonio Nino Diaz backtrace(__func__); \ 1033e530d8eSAntonio Nino Diaz (void)console_flush(); \ 1043e530d8eSAntonio Nino Diaz do_panic(); \ 1053e530d8eSAntonio Nino Diaz } while (false) 106a43d431bSSoby Mathew 10751faada7SDouglas Raillard /* Function called when stack protection check code detects a corrupted stack */ 10851faada7SDouglas Raillard void __dead2 __stack_chk_fail(void); 10951faada7SDouglas Raillard 1107f56e9a3SSoby Mathew void tf_log(const char *fmt, ...) __printflike(1, 2); 1117f56e9a3SSoby Mathew void tf_log_set_max_level(unsigned int log_level); 112b79af934SSoby Mathew 1131319e7b1SSoby Mathew #endif /* __ASSEMBLY__ */ 114870ce3ddSAntonio Nino Diaz #endif /* DEBUG_H */ 115