mps_trace.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Message Processing Stack, Trace module
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  9. #include "mps_common.h"
  10. #if defined(MBEDTLS_MPS_ENABLE_TRACE)
  11. #include "mps_trace.h"
  12. #include <stdarg.h>
  13. static int trace_depth = 0;
  14. #define color_default "\x1B[0m"
  15. #define color_red "\x1B[1;31m"
  16. #define color_green "\x1B[1;32m"
  17. #define color_yellow "\x1B[1;33m"
  18. #define color_blue "\x1B[1;34m"
  19. #define color_magenta "\x1B[1;35m"
  20. #define color_cyan "\x1B[1;36m"
  21. #define color_white "\x1B[1;37m"
  22. static char const *colors[] =
  23. {
  24. color_default,
  25. color_green,
  26. color_yellow,
  27. color_magenta,
  28. color_cyan,
  29. color_blue,
  30. color_white
  31. };
  32. #define MPS_TRACE_BUF_SIZE 100
  33. void mbedtls_mps_trace_print_msg(int id, int line, const char *format, ...)
  34. {
  35. int ret;
  36. char str[MPS_TRACE_BUF_SIZE];
  37. va_list argp;
  38. va_start(argp, format);
  39. ret = mbedtls_vsnprintf(str, MPS_TRACE_BUF_SIZE, format, argp);
  40. va_end(argp);
  41. if (ret >= 0 && ret < MPS_TRACE_BUF_SIZE) {
  42. str[ret] = '\0';
  43. mbedtls_printf("[%d|L%d]: %s\n", id, line, str);
  44. }
  45. }
  46. int mbedtls_mps_trace_get_depth()
  47. {
  48. return trace_depth;
  49. }
  50. void mbedtls_mps_trace_dec_depth()
  51. {
  52. trace_depth--;
  53. }
  54. void mbedtls_mps_trace_inc_depth()
  55. {
  56. trace_depth++;
  57. }
  58. void mbedtls_mps_trace_color(int id)
  59. {
  60. if (id > (int) (sizeof(colors) / sizeof(*colors))) {
  61. return;
  62. }
  63. printf("%s", colors[id]);
  64. }
  65. void mbedtls_mps_trace_indent(int level, mbedtls_mps_trace_type ty)
  66. {
  67. if (level > 0) {
  68. while (--level) {
  69. printf("| ");
  70. }
  71. printf("| ");
  72. }
  73. switch (ty) {
  74. case MBEDTLS_MPS_TRACE_TYPE_COMMENT:
  75. mbedtls_printf("@ ");
  76. break;
  77. case MBEDTLS_MPS_TRACE_TYPE_CALL:
  78. mbedtls_printf("+--> ");
  79. break;
  80. case MBEDTLS_MPS_TRACE_TYPE_ERROR:
  81. mbedtls_printf("E ");
  82. break;
  83. case MBEDTLS_MPS_TRACE_TYPE_RETURN:
  84. mbedtls_printf("< ");
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. #endif /* MBEDTLS_MPS_ENABLE_TRACE */
  91. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */