shell.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (C) 2021 Marcin Niestroj
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <shell/shell.h>
  7. #include <mbedtls/memory_buffer_alloc.h>
  8. #if defined(MBEDTLS_MEMORY_DEBUG)
  9. static int cmd_mbedtls_heap_details(const struct shell *sh, size_t argc,
  10. char **argv)
  11. {
  12. mbedtls_memory_buffer_alloc_status();
  13. return 0;
  14. }
  15. static int cmd_mbedtls_heap_max_reset(const struct shell *sh, size_t argc,
  16. char **argv)
  17. {
  18. mbedtls_memory_buffer_alloc_max_reset();
  19. return 0;
  20. }
  21. static int cmd_mbedtls_heap(const struct shell *sh, size_t argc, char **argv)
  22. {
  23. size_t max_used, max_blocks;
  24. size_t cur_used, cur_blocks;
  25. mbedtls_memory_buffer_alloc_max_get(&max_used, &max_blocks);
  26. mbedtls_memory_buffer_alloc_cur_get(&cur_used, &cur_blocks);
  27. shell_print(sh, "Maximum (peak): %zu bytes, %zu blocks",
  28. max_used, max_blocks);
  29. shell_print(sh, "Current: %zu bytes, %zu blocks",
  30. cur_used, cur_blocks);
  31. return 0;
  32. }
  33. SHELL_STATIC_SUBCMD_SET_CREATE(mbedtls_heap_cmds,
  34. SHELL_CMD_ARG(details, NULL, "Print heap details",
  35. cmd_mbedtls_heap_details, 1, 0),
  36. SHELL_CMD_ARG(max_reset, NULL, "Reset max heap statistics",
  37. cmd_mbedtls_heap_max_reset, 1, 0),
  38. SHELL_SUBCMD_SET_END /* Array terminated. */
  39. );
  40. #endif
  41. SHELL_STATIC_SUBCMD_SET_CREATE(mbedtls_cmds,
  42. #if defined(MBEDTLS_MEMORY_DEBUG)
  43. SHELL_CMD_ARG(heap, &mbedtls_heap_cmds, "Show heap status",
  44. cmd_mbedtls_heap, 1, 0),
  45. #endif
  46. SHELL_SUBCMD_SET_END /* Array terminated. */
  47. );
  48. SHELL_CMD_REGISTER(mbedtls, &mbedtls_cmds, "mbed TLS commands", NULL);