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