modules: mbedtls: support extracting __FILE__ basename at buildtime

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 <m.niestroj@emb.dev>
This commit is contained in:
Marcin Niestroj 2022-06-15 12:39:17 +02:00 committed by Carles Cufí
commit e4c11fd8aa
3 changed files with 44 additions and 5 deletions

View file

@ -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}

View file

@ -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
@ -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

View file

@ -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);
@ -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;
}
}
}