Skip to content

Commit 84c5a46

Browse files
mniestrojcfriedt
authored andcommitted
module: mbedtls: add shell module
Add mbedTLS specific shell module, which allows (for now) to show heap allocation statistics. Signed-off-by: Marcin Niestroj <[email protected]>
1 parent 608ad43 commit 84c5a46

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

modules/mbedtls/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ if(CONFIG_MBEDTLS_BUILTIN)
2323
${mbedtls_sources}
2424
)
2525

26+
zephyr_library_sources_ifdef(CONFIG_MBEDTLS_SHELL shell.c)
27+
2628
zephyr_library_app_memory(k_mbedtls_partition)
2729
if(CONFIG_ARCH_POSIX AND CONFIG_ASAN AND NOT CONFIG_64BIT)
2830
# i386 assembly code used in MBEDTLS does not compile with size optimization

modules/mbedtls/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ config MBEDTLS_HEAP_SIZE
151151
be needed. For some dedicated and specific usage of mbedtls API, the
152152
1000 bytes might be ok.
153153

154+
config MBEDTLS_SHELL
155+
bool "mbed TLS shell"
156+
depends on MBEDTLS
157+
depends on SHELL
158+
help
159+
Enable mbed TLS shell module, which allows to show debug information
160+
about mbed TLS library, such as heap usage.
161+
154162
config APP_LINK_WITH_MBEDTLS
155163
bool "Link 'app' with MBEDTLS"
156164
default y

modules/mbedtls/shell.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) 2021 Marcin Niestroj
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <shell/shell.h>
8+
#include <mbedtls/memory_buffer_alloc.h>
9+
10+
#if defined(MBEDTLS_MEMORY_DEBUG)
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+
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+
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);

0 commit comments

Comments
 (0)