debug.h 3.7 KB

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