Harry Liebel | 57bb658 | 2013-12-19 13:30:58 +0000 | [diff] [blame] | 1 | /* |
Alexei Fedorov | 813c9f9 | 2020-03-03 13:31:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved. |
Harry Liebel | 57bb658 | 2013-12-19 13:30:58 +0000 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Harry Liebel | 57bb658 | 2013-12-19 13:30:58 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 7 | #ifndef DEBUG_H |
| 8 | #define DEBUG_H |
Harry Liebel | 57bb658 | 2013-12-19 13:30:58 +0000 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <lib/utils_def.h> |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 11 | |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 12 | /* |
| 13 | * The log output macros print output to the console. These macros produce |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 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. |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 17 | * |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 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. |
Harry Liebel | 57bb658 | 2013-12-19 13:30:58 +0000 | [diff] [blame] | 21 | */ |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 22 | |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 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) |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 29 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 30 | #ifndef __ASSEMBLER__ |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 31 | |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 32 | #include <cdefs.h> |
Soby Mathew | f583a06 | 2017-09-04 11:45:52 +0100 | [diff] [blame] | 33 | #include <stdarg.h> |
Antonio Nino Diaz | d587646 | 2018-08-23 15:13:58 +0100 | [diff] [blame] | 34 | #include <stdbool.h> |
Soby Mathew | 6b28c57 | 2016-03-21 10:36:47 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 36 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 37 | #include <drivers/console.h> |
| 38 | |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 39 | /* |
| 40 | * Define Log Markers corresponding to each log level which will |
| 41 | * be embedded in the format string and is expected by tf_log() to determine |
| 42 | * the log level. |
| 43 | */ |
| 44 | #define LOG_MARKER_ERROR "\xa" /* 10 */ |
| 45 | #define LOG_MARKER_NOTICE "\x14" /* 20 */ |
| 46 | #define LOG_MARKER_WARNING "\x1e" /* 30 */ |
| 47 | #define LOG_MARKER_INFO "\x28" /* 40 */ |
| 48 | #define LOG_MARKER_VERBOSE "\x32" /* 50 */ |
| 49 | |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 50 | /* |
| 51 | * If the log output is too low then this macro is used in place of tf_log() |
| 52 | * below. The intent is to get the compiler to evaluate the function call for |
| 53 | * type checking and format specifier correctness but let it optimize it out. |
| 54 | */ |
| 55 | #define no_tf_log(fmt, ...) \ |
| 56 | do { \ |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 57 | if (false) { \ |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 58 | tf_log(fmt, ##__VA_ARGS__); \ |
| 59 | } \ |
Antonio Nino Diaz | 14fabd0 | 2018-08-28 11:44:44 +0100 | [diff] [blame] | 60 | } while (false) |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 61 | |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 62 | #if LOG_LEVEL >= LOG_LEVEL_ERROR |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 63 | # define ERROR(...) tf_log(LOG_MARKER_ERROR __VA_ARGS__) |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 64 | #else |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 65 | # define ERROR(...) no_tf_log(LOG_MARKER_ERROR __VA_ARGS__) |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 66 | #endif |
| 67 | |
John Tsichritzis | e75b9a5 | 2018-09-07 14:42:09 +0100 | [diff] [blame] | 68 | #if LOG_LEVEL >= LOG_LEVEL_NOTICE |
| 69 | # define NOTICE(...) tf_log(LOG_MARKER_NOTICE __VA_ARGS__) |
| 70 | #else |
| 71 | # define NOTICE(...) no_tf_log(LOG_MARKER_NOTICE __VA_ARGS__) |
| 72 | #endif |
| 73 | |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 74 | #if LOG_LEVEL >= LOG_LEVEL_WARNING |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 75 | # define WARN(...) tf_log(LOG_MARKER_WARNING __VA_ARGS__) |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 76 | #else |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 77 | # define WARN(...) no_tf_log(LOG_MARKER_WARNING __VA_ARGS__) |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 78 | #endif |
| 79 | |
| 80 | #if LOG_LEVEL >= LOG_LEVEL_INFO |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 81 | # define INFO(...) tf_log(LOG_MARKER_INFO __VA_ARGS__) |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 82 | #else |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 83 | # define INFO(...) no_tf_log(LOG_MARKER_INFO __VA_ARGS__) |
Dan Handley | cba2c50 | 2014-08-08 14:36:42 +0100 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 87 | # define VERBOSE(...) tf_log(LOG_MARKER_VERBOSE __VA_ARGS__) |
Harry Liebel | 57bb658 | 2013-12-19 13:30:58 +0000 | [diff] [blame] | 88 | #else |
Sandrine Bailleux | bf789e7 | 2018-06-20 14:50:48 +0200 | [diff] [blame] | 89 | # define VERBOSE(...) no_tf_log(LOG_MARKER_VERBOSE __VA_ARGS__) |
Harry Liebel | 57bb658 | 2013-12-19 13:30:58 +0000 | [diff] [blame] | 90 | #endif |
| 91 | |
Douglas Raillard | 7741463 | 2018-08-21 12:54:45 +0100 | [diff] [blame] | 92 | #if ENABLE_BACKTRACE |
| 93 | void backtrace(const char *cookie); |
Alexei Fedorov | 813c9f9 | 2020-03-03 13:31:58 +0000 | [diff] [blame] | 94 | const char *get_el_str(unsigned int el); |
Douglas Raillard | 7741463 | 2018-08-21 12:54:45 +0100 | [diff] [blame] | 95 | #else |
| 96 | #define backtrace(x) |
| 97 | #endif |
| 98 | |
Dan Handley | a17fefa | 2014-05-14 12:38:32 +0100 | [diff] [blame] | 99 | void __dead2 do_panic(void); |
Antonio Nino Diaz | d587646 | 2018-08-23 15:13:58 +0100 | [diff] [blame] | 100 | |
| 101 | #define panic() \ |
| 102 | do { \ |
| 103 | backtrace(__func__); \ |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 104 | console_flush(); \ |
Antonio Nino Diaz | d587646 | 2018-08-23 15:13:58 +0100 | [diff] [blame] | 105 | do_panic(); \ |
| 106 | } while (false) |
Soby Mathew | 5e5c207 | 2014-04-07 15:28:55 +0100 | [diff] [blame] | 107 | |
Douglas Raillard | 306593d | 2017-02-24 18:14:15 +0000 | [diff] [blame] | 108 | /* Function called when stack protection check code detects a corrupted stack */ |
| 109 | void __dead2 __stack_chk_fail(void); |
| 110 | |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 111 | void tf_log(const char *fmt, ...) __printflike(1, 2); |
Soby Mathew | aaf15f5 | 2017-09-04 11:49:29 +0100 | [diff] [blame] | 112 | void tf_log_set_max_level(unsigned int log_level); |
Soby Mathew | afe7e2f | 2014-06-12 17:23:58 +0100 | [diff] [blame] | 113 | |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 114 | #endif /* __ASSEMBLER__ */ |
Antonio Nino Diaz | c0c8eb6 | 2018-08-15 17:02:28 +0100 | [diff] [blame] | 115 | #endif /* DEBUG_H */ |