mps_trace.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright The Mbed TLS Contributors
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * This file is part of mbed TLS (https://tls.mbed.org)
  18. */
  19. /**
  20. * \file mps_trace.h
  21. *
  22. * \brief Tracing module for MPS
  23. */
  24. #ifndef MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
  25. #define MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
  26. #include "common.h"
  27. #include "mps_common.h"
  28. #include "mps_trace.h"
  29. #if defined(MBEDTLS_PLATFORM_C)
  30. #include "mbedtls/platform.h"
  31. #else
  32. #include <stdio.h>
  33. #define mbedtls_printf printf
  34. #define mbedtls_vsnprintf vsnprintf
  35. #endif /* MBEDTLS_PLATFORM_C */
  36. #if defined(MBEDTLS_MPS_ENABLE_TRACE)
  37. /*
  38. * Adapt this to enable/disable tracing output
  39. * from the various layers of the MPS.
  40. */
  41. #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_1
  42. #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_2
  43. #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_3
  44. #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_4
  45. #define MBEDTLS_MPS_TRACE_ENABLE_READER
  46. #define MBEDTLS_MPS_TRACE_ENABLE_WRITER
  47. /*
  48. * To use the existing trace module, only change
  49. * MBEDTLS_MPS_TRACE_ENABLE_XXX above, but don't modify the
  50. * rest of this file.
  51. */
  52. typedef enum
  53. {
  54. MBEDTLS_MPS_TRACE_TYPE_COMMENT,
  55. MBEDTLS_MPS_TRACE_TYPE_CALL,
  56. MBEDTLS_MPS_TRACE_TYPE_ERROR,
  57. MBEDTLS_MPS_TRACE_TYPE_RETURN
  58. } mbedtls_mps_trace_type;
  59. #define MBEDTLS_MPS_TRACE_BIT_LAYER_1 1
  60. #define MBEDTLS_MPS_TRACE_BIT_LAYER_2 2
  61. #define MBEDTLS_MPS_TRACE_BIT_LAYER_3 3
  62. #define MBEDTLS_MPS_TRACE_BIT_LAYER_4 4
  63. #define MBEDTLS_MPS_TRACE_BIT_WRITER 5
  64. #define MBEDTLS_MPS_TRACE_BIT_READER 6
  65. #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_1)
  66. #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_1 )
  67. #else
  68. #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 0
  69. #endif
  70. #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_2)
  71. #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_2 )
  72. #else
  73. #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 0
  74. #endif
  75. #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_3)
  76. #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_3 )
  77. #else
  78. #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 0
  79. #endif
  80. #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_4)
  81. #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_4 )
  82. #else
  83. #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 0
  84. #endif
  85. #if defined(MBEDTLS_MPS_TRACE_ENABLE_READER)
  86. #define MBEDTLS_MPS_TRACE_MASK_READER (1u << MBEDTLS_MPS_TRACE_BIT_READER )
  87. #else
  88. #define MBEDTLS_MPS_TRACE_MASK_READER 0
  89. #endif
  90. #if defined(MBEDTLS_MPS_TRACE_ENABLE_WRITER)
  91. #define MBEDTLS_MPS_TRACE_MASK_WRITER (1u << MBEDTLS_MPS_TRACE_BIT_WRITER )
  92. #else
  93. #define MBEDTLS_MPS_TRACE_MASK_WRITER 0
  94. #endif
  95. #define MBEDTLS_MPS_TRACE_MASK ( MBEDTLS_MPS_TRACE_MASK_LAYER_1 | \
  96. MBEDTLS_MPS_TRACE_MASK_LAYER_2 | \
  97. MBEDTLS_MPS_TRACE_MASK_LAYER_3 | \
  98. MBEDTLS_MPS_TRACE_MASK_LAYER_4 | \
  99. MBEDTLS_MPS_TRACE_MASK_READER | \
  100. MBEDTLS_MPS_TRACE_MASK_WRITER )
  101. /* We have to avoid globals because E-ACSL chokes on them...
  102. * Wrap everything in stub functions. */
  103. int mbedtls_mps_trace_get_depth( void );
  104. void mbedtls_mps_trace_inc_depth( void );
  105. void mbedtls_mps_trace_dec_depth( void );
  106. void mbedtls_mps_trace_color( int id );
  107. void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty );
  108. void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... );
  109. #define MBEDTLS_MPS_TRACE( type, ... ) \
  110. do { \
  111. if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
  112. break; \
  113. mbedtls_mps_trace_indent( mbedtls_mps_trace_get_depth(), type ); \
  114. mbedtls_mps_trace_color( mbedtls_mps_trace_id ); \
  115. mbedtls_mps_trace_print_msg( mbedtls_mps_trace_id, __LINE__, __VA_ARGS__ ); \
  116. mbedtls_mps_trace_color( 0 ); \
  117. } while( 0 )
  118. #define MBEDTLS_MPS_TRACE_INIT( ... ) \
  119. do { \
  120. if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
  121. break; \
  122. MBEDTLS_MPS_TRACE( MBEDTLS_MPS_TRACE_TYPE_CALL, __VA_ARGS__ ); \
  123. mbedtls_mps_trace_inc_depth(); \
  124. } while( 0 )
  125. #define MBEDTLS_MPS_TRACE_END( val ) \
  126. do { \
  127. if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
  128. break; \
  129. MBEDTLS_MPS_TRACE( MBEDTLS_MPS_TRACE_TYPE_RETURN, "%d (-%#04x)", \
  130. (int) (val), -((unsigned)(val)) ); \
  131. mbedtls_mps_trace_dec_depth(); \
  132. } while( 0 )
  133. #define MBEDTLS_MPS_TRACE_RETURN( val ) \
  134. do { \
  135. /* Breaks tail recursion. */ \
  136. int ret__ = val; \
  137. MBEDTLS_MPS_TRACE_END( ret__ ); \
  138. return( ret__ ); \
  139. } while( 0 )
  140. #else /* MBEDTLS_MPS_TRACE */
  141. #define MBEDTLS_MPS_TRACE( type, ... ) do { } while( 0 )
  142. #define MBEDTLS_MPS_TRACE_INIT( ... ) do { } while( 0 )
  143. #define MBEDTLS_MPS_TRACE_END do { } while( 0 )
  144. #define MBEDTLS_MPS_TRACE_RETURN( val ) return( val );
  145. #endif /* MBEDTLS_MPS_TRACE */
  146. #endif /* MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H */