mps_trace.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. str[ret] = '\0';
  57. mbedtls_printf("[%d|L%d]: %s\n", id, line, str);
  58. }
  59. }
  60. int mbedtls_mps_trace_get_depth()
  61. {
  62. return trace_depth;
  63. }
  64. void mbedtls_mps_trace_dec_depth()
  65. {
  66. trace_depth--;
  67. }
  68. void mbedtls_mps_trace_inc_depth()
  69. {
  70. trace_depth++;
  71. }
  72. void mbedtls_mps_trace_color(int id)
  73. {
  74. if (id > (int) (sizeof(colors) / sizeof(*colors))) {
  75. return;
  76. }
  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. while (--level) {
  83. printf("| ");
  84. }
  85. printf("| ");
  86. }
  87. switch (ty) {
  88. case MBEDTLS_MPS_TRACE_TYPE_COMMENT:
  89. mbedtls_printf("@ ");
  90. break;
  91. case MBEDTLS_MPS_TRACE_TYPE_CALL:
  92. mbedtls_printf("+--> ");
  93. break;
  94. case MBEDTLS_MPS_TRACE_TYPE_ERROR:
  95. mbedtls_printf("E ");
  96. break;
  97. case MBEDTLS_MPS_TRACE_TYPE_RETURN:
  98. mbedtls_printf("< ");
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. #endif /* MBEDTLS_MPS_ENABLE_TRACE */
  105. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */