mps_trace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Message Processing Stack, Trace module
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of Mbed TLS (https://tls.mbed.org)
  20. */
  21. #include "common.h"
  22. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  23. #include "mps_common.h"
  24. #if defined(MBEDTLS_MPS_ENABLE_TRACE)
  25. #include "mps_trace.h"
  26. #include <stdarg.h>
  27. static int trace_depth = 0;
  28. #define color_default "\x1B[0m"
  29. #define color_red "\x1B[1;31m"
  30. #define color_green "\x1B[1;32m"
  31. #define color_yellow "\x1B[1;33m"
  32. #define color_blue "\x1B[1;34m"
  33. #define color_magenta "\x1B[1;35m"
  34. #define color_cyan "\x1B[1;36m"
  35. #define color_white "\x1B[1;37m"
  36. static char const * colors[] =
  37. {
  38. color_default,
  39. color_green,
  40. color_yellow,
  41. color_magenta,
  42. color_cyan,
  43. color_blue,
  44. color_white
  45. };
  46. #define MPS_TRACE_BUF_SIZE 100
  47. void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... )
  48. {
  49. int ret;
  50. char str[MPS_TRACE_BUF_SIZE];
  51. va_list argp;
  52. va_start( argp, format );
  53. ret = mbedtls_vsnprintf( str, MPS_TRACE_BUF_SIZE, format, argp );
  54. va_end( argp );
  55. if( ret >= 0 && ret < MPS_TRACE_BUF_SIZE )
  56. {
  57. str[ret] = '\0';
  58. mbedtls_printf( "[%d|L%d]: %s\n", id, line, str );
  59. }
  60. }
  61. int mbedtls_mps_trace_get_depth()
  62. {
  63. return trace_depth;
  64. }
  65. void mbedtls_mps_trace_dec_depth()
  66. {
  67. trace_depth--;
  68. }
  69. void mbedtls_mps_trace_inc_depth()
  70. {
  71. trace_depth++;
  72. }
  73. void mbedtls_mps_trace_color( int id )
  74. {
  75. if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) )
  76. return;
  77. printf( "%s", colors[ id ] );
  78. }
  79. void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty )
  80. {
  81. if( level > 0 )
  82. {
  83. while( --level )
  84. printf( "| " );
  85. printf( "| " );
  86. }
  87. switch( ty )
  88. {
  89. case MBEDTLS_MPS_TRACE_TYPE_COMMENT:
  90. mbedtls_printf( "@ " );
  91. break;
  92. case MBEDTLS_MPS_TRACE_TYPE_CALL:
  93. mbedtls_printf( "+--> " );
  94. break;
  95. case MBEDTLS_MPS_TRACE_TYPE_ERROR:
  96. mbedtls_printf( "E " );
  97. break;
  98. case MBEDTLS_MPS_TRACE_TYPE_RETURN:
  99. mbedtls_printf( "< " );
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. #endif /* MBEDTLS_MPS_ENABLE_TRACE */
  106. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */