Skip to content

Commit

Permalink
modules: mbedtls: support extracting __FILE__ basename at buildtime
Browse files Browse the repository at this point in the history
So far there was a runtime basename extraction of filenames passed to
mbedTLS debug hook. This has both runtime penalty as well as code size
penalty.

Introduce a buildtime support of extracting basename of source filenames
logged using logging subsystem, so that there is no need to do it at
runtime.

Provide Kconfig options for both buildtime and runtime basename extraction,
as in some cases the buildtime basename extraction might not work,
depending on toolchain used for building Zephyr. Default to buildtime when
using Zephyr SDK, as that is proven to work. Use runtime basename
extraction in other cases (other toolchains used).

This saves approximately 204 bytes of code footprint for sample
application with native TLS sockets built for nRF52840.

Signed-off-by: Marcin Niestroj <[email protected]>
  • Loading branch information
mniestroj authored and carlescufi committed Aug 17, 2022
1 parent 6653fd9 commit e4c11fd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
4 changes: 4 additions & 0 deletions modules/mbedtls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ if(CONFIG_MBEDTLS_BUILTIN)
${ZEPHYR_CURRENT_MODULE_DIR}/library/*.c
)

if(CONFIG_MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_BUILDTIME)
zephyr_cc_option(-fmacro-prefix-map=${ZEPHYR_CURRENT_MODULE_DIR}/library/=)
endif()

zephyr_library_sources(
zephyr_init.c
${mbedtls_sources}
Expand Down
35 changes: 34 additions & 1 deletion modules/mbedtls/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ config MBEDTLS_DEBUG
function in your application. Alternatively implement your own debug
hook function if zephyr_mbedtls_debug() doesn't suit your needs.

if MBEDTLS_DEBUG

config MBEDTLS_DEBUG_LEVEL
int "mbed TLS default debug level"
depends on MBEDTLS_DEBUG
default 0
range 0 4
help
Expand All @@ -111,6 +112,38 @@ config MBEDTLS_DEBUG_LEVEL
This makes Zephyr call mbedtls_debug_set_threshold() function during
mbedTLS initialization, with the configured debug log level.

choice MBEDTLS_DEBUG_EXTRACT_BASENAME
prompt "Extract basename from filenames"
default MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_BUILDTIME if "$(ZEPHYR_TOOLCHAIN_VARIANT)" = "zephyr"
default MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_RUNTIME

config MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_BUILDTIME
bool "Buildtime"
help
Adds compile options, which should convert full source paths in
__FILE__ macro to files' basenames. This will reduce code footprint
when debug messages are enabled.

This is compiler dependent, so if it does not work then please
fallback to MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_RUNTIME instead.

config MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_RUNTIME
bool "Runtime"
help
Filename passed as argument to debug hook will be stripped from
directory, so that only basename part is left and logged.

config MBEDTLS_DEBUG_EXTRACT_BASENAME_DISABLED
bool "Disabled"
help
Disable basename extraction from filenames in log mesasges. This will
result in full paths or paths relative to west root directory
appearing in log messages generated by mbedTLS library.

endchoice

endif # MBEDTLS_DEBUG

config MBEDTLS_MEMORY_DEBUG
bool "mbed TLS memory debug activation"
depends on MBEDTLS_BUILTIN
Expand Down
10 changes: 6 additions & 4 deletions modules/mbedtls/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LOG_MODULE_REGISTER(mbedtls, CONFIG_MBEDTLS_LOG_LEVEL);

void zephyr_mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str)
{
const char *p, *basename;
const char *p, *basename = file;

ARG_UNUSED(ctx);

Expand All @@ -21,9 +21,11 @@ void zephyr_mbedtls_debug(void *ctx, int level, const char *file, int line, cons
}

/* Extract basename from file */
for (p = basename = file; *p != '\0'; p++) {
if (*p == '/' || *p == '\\') {
basename = p + 1;
if (IS_ENABLED(CONFIG_MBEDTLS_DEBUG_EXTRACT_BASENAME_AT_RUNTIME)) {
for (p = basename = file; *p != '\0'; p++) {
if (*p == '/' || *p == '\\') {
basename = p + 1;
}
}
}

Expand Down

0 comments on commit e4c11fd

Please sign in to comment.