LCOV - code coverage report
Current view: top level - /home/runner/zephyrproject/zephyr/subsys/logging - log_mgmt.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 3.8 % 160 6
Test Date: 2026-03-12 12:01:18 Functions: 3.6 % 28 1
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 76 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2021 Nordic Semiconductor ASA
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : #include <zephyr/logging/log_internal.h>
       7                 :             : #include <zephyr/logging/log_ctrl.h>
       8                 :             : #include <zephyr/internal/syscall_handler.h>
       9                 :             : #include <zephyr/init.h>
      10                 :             : #include <zephyr/logging/log.h>
      11                 :             : #include <zephyr/logging/log_link.h>
      12                 :             : #include <zephyr/sys/iterable_sections.h>
      13                 :             : 
      14                 :             : #include "log_cache.h"
      15                 :             : 
      16                 :             : LOG_MODULE_REGISTER(log_mgmt);
      17                 :             : 
      18                 :             : #ifndef CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_SIZE
      19                 :             : #define CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_SIZE 1
      20                 :             : #endif
      21                 :             : 
      22                 :             : #ifndef CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_COUNT
      23                 :             : #define CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_COUNT 1
      24                 :             : #endif
      25                 :             : 
      26                 :             : #ifndef CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_SIZE
      27                 :             : #define CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_SIZE 1
      28                 :             : #endif
      29                 :             : 
      30                 :             : #ifndef CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_COUNT
      31                 :             : #define CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_COUNT 1
      32                 :             : #endif
      33                 :             : 
      34                 :             : #define DCACHE_BUF_SIZE \
      35                 :             :         (CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_SIZE + sizeof(struct log_cache_entry)) * \
      36                 :             :         CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_COUNT
      37                 :             : 
      38                 :             : #define SCACHE_BUF_SIZE \
      39                 :             :         (CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_SIZE + sizeof(struct log_cache_entry)) * \
      40                 :             :         CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_COUNT
      41                 :             : 
      42                 :             : static uint8_t dname_cache_buffer[DCACHE_BUF_SIZE] __aligned(sizeof(uint32_t));
      43                 :             : static uint8_t sname_cache_buffer[SCACHE_BUF_SIZE] __aligned(sizeof(uint32_t));
      44                 :             : 
      45                 :             : static struct log_cache dname_cache;
      46                 :             : static struct log_cache sname_cache;
      47                 :             : 
      48                 :             : struct log_source_id {
      49                 :             :         uint8_t domain_id;
      50                 :             :         uint16_t source_id;
      51                 :             : };
      52                 :             : 
      53                 :             : union log_source_ids {
      54                 :             :         struct log_source_id id;
      55                 :             :         uintptr_t raw;
      56                 :             : };
      57                 :             : 
      58                 :           0 : static bool domain_id_cmp(uintptr_t id0, uintptr_t id1)
      59                 :             : {
      60                 :           0 :         return id0 == id1;
      61                 :             : }
      62                 :             : 
      63                 :           0 : static bool source_id_cmp(uintptr_t id0, uintptr_t id1)
      64                 :             : {
      65                 :           0 :         union log_source_ids s0 = { .raw = id0 };
      66                 :           0 :         union log_source_ids s1 = { .raw = id1 };
      67                 :             : 
      68                 :           0 :         return (s0.id.source_id == s1.id.source_id) &&
      69                 :             :                 (s0.id.domain_id == s1.id.domain_id);
      70                 :             : }
      71                 :             : 
      72                 :             : /* Implementation of functions related to controlling logging sources and backends:
      73                 :             :  * - getting/setting source details like name, filtering
      74                 :             :  * - controlling backends filtering
      75                 :             :  */
      76                 :             : 
      77                 :             : /** @brief Return link and relative domain id based on absolute domain id.
      78                 :             :  *
      79                 :             :  * @param[in]  domain_id        Aboslute domain ID.
      80                 :             :  * @param[out] rel_domain_id    Domain ID elative to the link domain ID as output.
      81                 :             :  *
      82                 :             :  * @return Link to which given domain belongs. NULL if link was not found.
      83                 :             :  */
      84                 :           0 : static const struct log_link *get_link_domain(uint8_t domain_id, uint8_t *rel_domain_id)
      85                 :             : {
      86                 :           0 :         uint8_t domain_max;
      87                 :             : 
      88   [ #  #  #  # ]:           0 :         STRUCT_SECTION_FOREACH(log_link, link) {
      89                 :           0 :                 domain_max = link->ctrl_blk->domain_offset +
      90                 :           0 :                                 link->ctrl_blk->domain_cnt;
      91         [ #  # ]:           0 :                 if (domain_id < domain_max) {
      92                 :             : 
      93                 :           0 :                         *rel_domain_id = domain_id - link->ctrl_blk->domain_offset;
      94                 :           0 :                         return link;
      95                 :             :                 }
      96                 :             :         }
      97                 :             : 
      98                 :           0 :         *rel_domain_id = 0;
      99                 :             : 
     100                 :           0 :         return NULL;
     101                 :             : }
     102                 :             : 
     103                 :             : /** @brief Get source offset used for getting runtime filter.
     104                 :             :  *
     105                 :             :  * Runtime filters for each link are dynamically allocated as an array of
     106                 :             :  * filters for all domains in the link. In order to fetch link associated with
     107                 :             :  * given source an index in the array must be retrieved.
     108                 :             :  */
     109                 :           0 : static uint32_t get_source_offset(const struct log_link *link,
     110                 :             :                                   uint8_t rel_domain_id)
     111                 :             : {
     112                 :           0 :         uint32_t offset = 0;
     113                 :             : 
     114         [ #  # ]:           0 :         for (uint8_t i = 0; i < rel_domain_id; i++) {
     115                 :           0 :                 offset += log_link_sources_count(link, i);
     116                 :             :         }
     117                 :             : 
     118                 :           0 :         return offset;
     119                 :             : }
     120                 :             : 
     121                 :           0 : uint32_t *z_log_link_get_dynamic_filter(uint8_t domain_id, uint32_t source_id)
     122                 :             : {
     123                 :           0 :         uint8_t rel_domain_id;
     124                 :           0 :         const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
     125                 :           0 :         uint32_t source_offset = 0;
     126                 :             : 
     127         [ #  # ]:           0 :         __ASSERT_NO_MSG(link != NULL);
     128                 :             : 
     129                 :           0 :         source_offset = get_source_offset(link, rel_domain_id);
     130                 :             : 
     131                 :           0 :         return &link->ctrl_blk->filters[source_offset + source_id];
     132                 :             : }
     133                 :             : 
     134                 :             : #ifdef CONFIG_LOG_MULTIDOMAIN
     135                 :             : static int link_filters_init(const struct log_link *link)
     136                 :             : {
     137                 :             :         uint32_t total_cnt = get_source_offset(link, link->ctrl_blk->domain_cnt);
     138                 :             : 
     139                 :             :         link->ctrl_blk->filters = k_malloc(sizeof(uint32_t) * total_cnt);
     140                 :             :         if (link->ctrl_blk->filters == NULL) {
     141                 :             :                 LOG_ERR("Failed to allocate buffer for runtime filtering.");
     142                 :             :                 __ASSERT(0, "Failed to allocate buffer.");
     143                 :             :                 return -ENOMEM;
     144                 :             :         }
     145                 :             : 
     146                 :             :         memset(link->ctrl_blk->filters, 0, sizeof(uint32_t) * total_cnt);
     147                 :             :         LOG_DBG("%s: heap used for filters:%d",
     148                 :             :                 link->name, (int)(total_cnt * sizeof(uint32_t)));
     149                 :             : 
     150                 :             :         return 0;
     151                 :             : }
     152                 :             : #endif
     153                 :             : 
     154                 :           0 : static void cache_init(void)
     155                 :             : {
     156                 :           0 :         int err;
     157                 :           0 :         static const struct log_cache_config dname_cache_config = {
     158                 :             :                 .buf = dname_cache_buffer,
     159                 :             :                 .buf_len = sizeof(dname_cache_buffer),
     160                 :             :                 .item_size = CONFIG_LOG_DOMAIN_NAME_CACHE_ENTRY_SIZE,
     161                 :             :                 .cmp = domain_id_cmp
     162                 :             :         };
     163                 :           0 :         static const struct log_cache_config sname_cache_config = {
     164                 :             :                 .buf = sname_cache_buffer,
     165                 :             :                 .buf_len = sizeof(sname_cache_buffer),
     166                 :             :                 .item_size = CONFIG_LOG_SOURCE_NAME_CACHE_ENTRY_SIZE,
     167                 :             :                 .cmp = source_id_cmp
     168                 :             :         };
     169                 :             : 
     170                 :           0 :         err = log_cache_init(&dname_cache, &dname_cache_config);
     171         [ #  # ]:           0 :         __ASSERT_NO_MSG(err == 0);
     172                 :             : 
     173                 :           0 :         err = log_cache_init(&sname_cache, &sname_cache_config);
     174         [ #  # ]:           0 :         __ASSERT_NO_MSG(err == 0);
     175                 :           0 : }
     176                 :             : 
     177                 :           0 : uint8_t z_log_ext_domain_count(void)
     178                 :             : {
     179                 :           0 :         uint8_t cnt = 0;
     180                 :             : 
     181   [ #  #  #  # ]:           0 :         STRUCT_SECTION_FOREACH(log_link, link) {
     182                 :           0 :                 cnt += log_link_domains_count(link);
     183                 :             :         }
     184                 :             : 
     185                 :           0 :         return cnt;
     186                 :             : }
     187                 :             : 
     188                 :           0 : static uint16_t link_source_count(uint8_t domain_id)
     189                 :             : {
     190                 :           0 :         uint8_t rel_domain_id;
     191                 :           0 :         const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
     192                 :             : 
     193         [ #  # ]:           0 :         __ASSERT_NO_MSG(link != NULL);
     194                 :             : 
     195                 :           0 :         return log_link_sources_count(link, rel_domain_id);
     196                 :             : }
     197                 :             : 
     198                 :           0 : uint32_t log_src_cnt_get(uint32_t domain_id)
     199                 :             : {
     200         [ #  # ]:           0 :         if (z_log_is_local_domain(domain_id)) {
     201                 :           0 :                 return z_log_sources_count();
     202                 :             :         }
     203                 :             : 
     204                 :           0 :         return link_source_count(domain_id);
     205                 :             : }
     206                 :             : 
     207                 :             : /* First check in cache if not there fetch from remote.
     208                 :             :  * When fetched from remote put in cache.
     209                 :             :  *
     210                 :             :  * @note Execution time depends on whether entry is in cache.
     211                 :             :  */
     212                 :           0 : static const char *link_source_name_get(uint8_t domain_id, uint32_t source_id)
     213                 :             : {
     214                 :           0 :         uint8_t *cached;
     215                 :           0 :         size_t cache_size = sname_cache.item_size;
     216                 :           0 :         union log_source_ids id = {
     217                 :             :                 .id = {
     218                 :             :                         .domain_id = domain_id,
     219                 :             :                         .source_id = source_id
     220                 :             :                 }
     221                 :             :         };
     222                 :             : 
     223                 :             :         /* If not in cache fetch from link and cache it. */
     224         [ #  # ]:           0 :         if (!log_cache_get(&sname_cache, id.raw, &cached)) {
     225                 :           0 :                 uint8_t rel_domain_id;
     226                 :           0 :                 const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
     227                 :           0 :                 int err;
     228                 :             : 
     229         [ #  # ]:           0 :                 __ASSERT_NO_MSG(link != NULL);
     230                 :             : 
     231                 :           0 :                 err = log_link_get_source_name(link, rel_domain_id, source_id,
     232                 :             :                                                cached, &cache_size);
     233         [ #  # ]:           0 :                 if (err < 0) {
     234                 :           0 :                         return NULL;
     235                 :             :                 }
     236                 :             : 
     237                 :           0 :                 log_cache_put(&sname_cache, cached);
     238                 :             :         }
     239                 :             : 
     240                 :           0 :         return (const char *)cached;
     241                 :             : }
     242                 :             : 
     243                 :           0 : const char *log_source_name_get(uint32_t domain_id, uint32_t source_id)
     244                 :             : {
     245         [ #  # ]:           0 :         if (z_log_is_local_domain(domain_id)) {
     246                 :           0 :                 if (IS_ENABLED(CONFIG_LOG_FMT_SECTION_STRIP)) {
     247                 :             :                         return "unknown";
     248                 :             :                 }
     249         [ #  # ]:           0 :                 if (source_id < log_src_cnt_get(domain_id)) {
     250                 :           0 :                         return TYPE_SECTION_START(log_const)[source_id].name;
     251                 :             :                 } else {
     252                 :             :                         return NULL;
     253                 :             :                 }
     254                 :             :         }
     255                 :             : 
     256                 :           0 :         return link_source_name_get(domain_id, source_id);
     257                 :             : }
     258                 :             : 
     259                 :             : /* First check in cache if not there fetch from remote.
     260                 :             :  * When fetched from remote put in cache.
     261                 :             :  *
     262                 :             :  * @note Execution time depends on whether entry is in cache.
     263                 :             :  */
     264                 :           0 : static const char *link_domain_name_get(uint8_t domain_id)
     265                 :             : {
     266                 :           0 :         uint8_t *cached;
     267                 :           0 :         size_t cache_size = dname_cache.item_size;
     268                 :           0 :         uintptr_t id = (uintptr_t)domain_id;
     269                 :           0 :         static const char *invalid_domain = "invalid";
     270                 :             : 
     271                 :             :         /* If not in cache fetch from link and cache it. */
     272         [ #  # ]:           0 :         if (!log_cache_get(&dname_cache, id, &cached)) {
     273                 :           0 :                 uint8_t rel_domain_id;
     274                 :           0 :                 const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
     275                 :           0 :                 int err;
     276                 :             : 
     277         [ #  # ]:           0 :                 __ASSERT_NO_MSG(link != NULL);
     278                 :             : 
     279                 :           0 :                 err = log_link_get_domain_name(link, rel_domain_id, cached, &cache_size);
     280         [ #  # ]:           0 :                 if (err < 0) {
     281                 :           0 :                         log_cache_release(&dname_cache, cached);
     282                 :           0 :                         return invalid_domain;
     283                 :             :                 }
     284                 :             : 
     285                 :           0 :                 log_cache_put(&dname_cache, cached);
     286                 :             :         }
     287                 :             : 
     288                 :           0 :         return (const char *)cached;
     289                 :             : }
     290                 :             : 
     291                 :           0 : const char *log_domain_name_get(uint32_t domain_id)
     292                 :             : {
     293         [ #  # ]:           0 :         if (z_log_is_local_domain(domain_id)) {
     294                 :             :                 return CONFIG_LOG_DOMAIN_NAME;
     295                 :             :         }
     296                 :             : 
     297                 :           0 :         return link_domain_name_get(domain_id);
     298                 :             : }
     299                 :             : 
     300                 :           0 : static uint8_t link_compiled_level_get(uint8_t domain_id, uint32_t source_id)
     301                 :             : {
     302                 :           0 :         uint8_t rel_domain_id;
     303                 :           0 :         const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
     304                 :           0 :         uint8_t level;
     305                 :             : 
     306         [ #  # ]:           0 :         __ASSERT_NO_MSG(link != NULL);
     307                 :             : 
     308                 :           0 :         return !log_link_get_levels(link, rel_domain_id, source_id, &level, NULL) ?
     309         [ #  # ]:           0 :                 level : 0;
     310                 :             : }
     311                 :             : 
     312                 :           0 : uint8_t log_compiled_level_get(uint8_t domain_id, uint32_t source_id)
     313                 :             : {
     314         [ #  # ]:           0 :         if (z_log_is_local_domain(domain_id)) {
     315         [ #  # ]:           0 :                 if (source_id < log_src_cnt_get(domain_id)) {
     316                 :           0 :                         return TYPE_SECTION_START(log_const)[source_id].level;
     317                 :             :                 } else {
     318                 :             :                         return LOG_LEVEL_NONE;
     319                 :             :                 }
     320                 :             :         }
     321                 :             : 
     322                 :           0 :         return link_compiled_level_get(domain_id, source_id);
     323                 :             : }
     324                 :             : 
     325                 :           0 : int z_log_link_set_runtime_level(uint8_t domain_id, uint16_t source_id, uint8_t level)
     326                 :             : {
     327                 :           0 :         uint8_t rel_domain_id;
     328                 :           0 :         const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
     329                 :             : 
     330         [ #  # ]:           0 :         __ASSERT_NO_MSG(link != NULL);
     331                 :             : 
     332                 :           0 :         return log_link_set_runtime_level(link, rel_domain_id, source_id, level);
     333                 :             : }
     334                 :             : 
     335                 :             : static uint32_t *get_dynamic_filter(uint8_t domain_id, uint32_t source_id)
     336                 :             : {
     337                 :             :         if (z_log_is_local_domain(domain_id)) {
     338                 :             :                 return &TYPE_SECTION_START(log_dynamic)[source_id].filters;
     339                 :             :         }
     340                 :             : 
     341                 :             :         return z_log_link_get_dynamic_filter(domain_id, source_id);
     342                 :             : }
     343                 :             : 
     344                 :           0 : void z_log_runtime_filters_init(void)
     345                 :             : {
     346                 :             :         /*
     347                 :             :          * Initialize aggregated runtime filter levels (no backends are
     348                 :             :          * attached yet, so leave backend slots in each dynamic filter set
     349                 :             :          * alone for now).
     350                 :             :          *
     351                 :             :          * Each log source's aggregated runtime level is set to match its
     352                 :             :          * compile-time level. When backends are attached later on in
     353                 :             :          * log_init(), they'll be initialized to the same value.
     354                 :             :          */
     355         [ #  # ]:           0 :         for (int i = 0; i < z_log_sources_count(); i++) {
     356                 :           0 :                 uint32_t *filters = z_log_dynamic_filters_get(i);
     357                 :           0 :                 uint8_t level;
     358                 :             : 
     359                 :             : #ifdef CONFIG_LOG_RUNTIME_DEFAULT_LEVEL
     360                 :             :                 /* Use configured runtime default level for initial filter */
     361                 :             :                 level = CONFIG_LOG_RUNTIME_DEFAULT_LEVEL;
     362                 :             : #else
     363                 :             :                 /* Fall back to compile-time level */
     364                 :           0 :                 level = log_compiled_level_get(Z_LOG_LOCAL_DOMAIN_ID, i);
     365                 :             : #endif
     366                 :           0 :                 level = MAX(level, CONFIG_LOG_OVERRIDE_LEVEL);
     367                 :           0 :                 LOG_FILTER_SLOT_SET(filters,
     368                 :             :                                     LOG_FILTER_AGGR_SLOT_IDX,
     369                 :             :                                     level);
     370                 :             :         }
     371                 :           0 : }
     372                 :             : 
     373                 :           0 : int log_source_id_get(const char *name)
     374                 :             : {
     375                 :           0 :         if (IS_ENABLED(CONFIG_LOG_FMT_SECTION_STRIP)) {
     376                 :             :                 return -1;
     377                 :             :         }
     378                 :             : 
     379         [ #  # ]:           0 :         for (int i = 0; i < log_src_cnt_get(Z_LOG_LOCAL_DOMAIN_ID); i++) {
     380                 :           0 :                 const char *sname = log_source_name_get(Z_LOG_LOCAL_DOMAIN_ID, i);
     381                 :             : 
     382   [ #  #  #  # ]:           0 :                 if ((sname != NULL) && (strcmp(sname, name) == 0)) {
     383                 :           0 :                         return i;
     384                 :             :                 }
     385                 :             :         }
     386                 :             :         return -1;
     387                 :             : }
     388                 :             : 
     389                 :             : static uint32_t max_filter_get(uint32_t filters)
     390                 :             : {
     391                 :             :         uint32_t max_filter = LOG_LEVEL_NONE;
     392                 :             :         int first_slot = LOG_FILTER_FIRST_BACKEND_SLOT_IDX;
     393                 :             :         int i;
     394                 :             : 
     395                 :             :         for (i = first_slot; i < LOG_FILTERS_NUM_OF_SLOTS; i++) {
     396                 :             :                 uint32_t tmp_filter = LOG_FILTER_SLOT_GET(&filters, i);
     397                 :             : 
     398                 :             :                 if (tmp_filter > max_filter) {
     399                 :             :                         max_filter = tmp_filter;
     400                 :             :                 }
     401                 :             :         }
     402                 :             : 
     403                 :             :         return max_filter;
     404                 :             : }
     405                 :             : 
     406                 :             : static void set_runtime_filter(uint8_t backend_id, uint8_t domain_id,
     407                 :             :                                uint32_t source_id, uint32_t level)
     408                 :             : {
     409                 :             :         uint32_t prev_max;
     410                 :             :         uint32_t new_max;
     411                 :             :         uint32_t *filters = get_dynamic_filter(domain_id, source_id);
     412                 :             : 
     413                 :             :         prev_max = LOG_FILTER_SLOT_GET(filters, LOG_FILTER_AGGR_SLOT_IDX);
     414                 :             : 
     415                 :             :         LOG_FILTER_SLOT_SET(filters, backend_id, level);
     416                 :             : 
     417                 :             :         /* Once current backend filter is updated recalculate
     418                 :             :          * aggregated maximal level
     419                 :             :          */
     420                 :             :         new_max = max_filter_get(*filters);
     421                 :             : 
     422                 :             :         LOG_FILTER_SLOT_SET(filters, LOG_FILTER_AGGR_SLOT_IDX, new_max);
     423                 :             : 
     424                 :             :         if (!z_log_is_local_domain(domain_id) && (new_max != prev_max)) {
     425                 :             :                 (void)z_log_link_set_runtime_level(domain_id, source_id, level);
     426                 :             :         }
     427                 :             : }
     428                 :             : 
     429                 :           0 : static uint32_t filter_get(uint8_t id, uint32_t domain_id, int16_t source_id, bool runtime)
     430                 :             : {
     431         [ #  # ]:           0 :         __ASSERT_NO_MSG(source_id < log_src_cnt_get(domain_id));
     432                 :             : 
     433                 :           0 :         if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && runtime) {
     434                 :             :                 if (source_id < 0) {
     435                 :             :                         return LOG_LEVEL_DBG;
     436                 :             :                 }
     437                 :             : 
     438                 :             :                 return LOG_FILTER_SLOT_GET(get_dynamic_filter(domain_id, source_id), id);
     439                 :             :         }
     440                 :             : 
     441                 :           0 :         return log_compiled_level_get(domain_id, source_id);
     442                 :             : }
     443                 :             : 
     444                 :             : 
     445                 :           0 : uint32_t filter_set(int id, uint32_t domain_id, int16_t source_id, uint32_t level)
     446                 :             : {
     447                 :           0 :         if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
     448                 :           0 :                 return log_compiled_level_get(domain_id, source_id);
     449                 :             :         }
     450                 :             : 
     451                 :             :         __ASSERT_NO_MSG(source_id < log_src_cnt_get(domain_id));
     452                 :             : 
     453                 :             : 
     454                 :             :         if (id < 0) {
     455                 :             :                 uint32_t max = 0U;
     456                 :             :                 size_t backend_cnt;
     457                 :             : 
     458                 :             :                 if (IS_ENABLED(CONFIG_LOG_FRONTEND)) {
     459                 :             :                         max = filter_set(LOG_FRONTEND_SLOT_ID, domain_id, source_id, level);
     460                 :             :                         if (IS_ENABLED(CONFIG_LOG_FRONTEND_ONLY)) {
     461                 :             :                                 return max;
     462                 :             :                         }
     463                 :             :                 }
     464                 :             : 
     465                 :             :                 STRUCT_SECTION_COUNT(log_backend, &backend_cnt);
     466                 :             :                 for (size_t i = 0; i < backend_cnt; i++) {
     467                 :             :                         uint32_t current = filter_set(log_backend_id_get(log_backend_get(i)),
     468                 :             :                                                         domain_id, source_id, level);
     469                 :             : 
     470                 :             :                         max = MAX(current, max);
     471                 :             :                 }
     472                 :             : 
     473                 :             :                 return max;
     474                 :             :         }
     475                 :             : 
     476                 :             :         level = MIN(level, MAX(filter_get(id, domain_id, source_id, false),
     477                 :             :                                CONFIG_LOG_OVERRIDE_LEVEL));
     478                 :             :         set_runtime_filter(id, domain_id, source_id, level);
     479                 :             : 
     480                 :             :         return level;
     481                 :             : }
     482                 :             : 
     483                 :           0 : uint32_t z_impl_log_filter_set(struct log_backend const *const backend,
     484                 :             :                                uint32_t domain_id, int16_t source_id,
     485                 :             :                                uint32_t level)
     486                 :             : {
     487         [ #  # ]:           0 :         int id = (backend == NULL) ? -1 : log_backend_id_get(backend);
     488                 :             : 
     489                 :           0 :         return filter_set(id, domain_id, source_id, level);
     490                 :             : }
     491                 :             : 
     492                 :           0 : uint32_t z_impl_log_frontend_filter_set(int16_t source_id, uint32_t level)
     493                 :             : {
     494                 :           0 :         return filter_set(LOG_FRONTEND_SLOT_ID, Z_LOG_LOCAL_DOMAIN_ID, source_id, level);
     495                 :             : }
     496                 :             : 
     497                 :             : #ifdef CONFIG_USERSPACE
     498                 :             : uint32_t z_vrfy_log_filter_set(struct log_backend const *const backend,
     499                 :             :                             uint32_t domain_id,
     500                 :             :                             int16_t src_id,
     501                 :             :                             uint32_t level)
     502                 :             : {
     503                 :             :         K_OOPS(K_SYSCALL_VERIFY_MSG(backend == NULL,
     504                 :             :                 "Setting per-backend filters from user mode is not supported"));
     505                 :             :         K_OOPS(K_SYSCALL_VERIFY_MSG(domain_id == Z_LOG_LOCAL_DOMAIN_ID,
     506                 :             :                 "Invalid log domain_id"));
     507                 :             :         K_OOPS(K_SYSCALL_VERIFY_MSG(src_id < (int16_t)log_src_cnt_get(domain_id),
     508                 :             :                 "Invalid log source id"));
     509                 :             :         K_OOPS(K_SYSCALL_VERIFY_MSG(
     510                 :             :                 (level <= LOG_LEVEL_DBG),
     511                 :             :                 "Invalid log level"));
     512                 :             : 
     513                 :             :         return z_impl_log_filter_set(NULL, domain_id, src_id, level);
     514                 :             : }
     515                 :             : #include <zephyr/syscalls/log_filter_set_mrsh.c>
     516                 :             : #endif
     517                 :             : 
     518                 :             : static void link_filter_set(const struct log_link *link,
     519                 :             :                             struct log_backend const *const backend,
     520                 :             :                             uint32_t level)
     521                 :             : {
     522                 :             :         if (log_link_is_active(link) != 0) {
     523                 :             :                 return;
     524                 :             :         }
     525                 :             : 
     526                 :             :         for (uint8_t d = link->ctrl_blk->domain_offset;
     527                 :             :              d < link->ctrl_blk->domain_offset + link->ctrl_blk->domain_cnt; d++) {
     528                 :             :                 for (uint16_t s = 0; s < log_src_cnt_get(d); s++) {
     529                 :             :                         log_filter_set(backend, d, s, level);
     530                 :             :                 }
     531                 :             :         }
     532                 :             : }
     533                 :             : 
     534                 :             : static void backend_filter_set(struct log_backend const *const backend,
     535                 :             :                                uint32_t level)
     536                 :             : {
     537                 :             :         if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
     538                 :             :                 return;
     539                 :             :         }
     540                 :             : 
     541                 :             :         for (uint16_t s = 0; s < log_src_cnt_get(0); s++) {
     542                 :             :                 log_filter_set(backend, 0, s, level);
     543                 :             :         }
     544                 :             : 
     545                 :             :         if (!IS_ENABLED(CONFIG_LOG_MULTIDOMAIN)) {
     546                 :             :                 return;
     547                 :             :         }
     548                 :             : 
     549                 :             :         /* Set level in activated links. */
     550                 :             :         STRUCT_SECTION_FOREACH(log_link, link) {
     551                 :             :                 link_filter_set(link, backend, level);
     552                 :             :         }
     553                 :             : }
     554                 :             : 
     555                 :           0 : const struct log_backend *log_backend_get_by_name(const char *backend_name)
     556                 :             : {
     557   [ #  #  #  # ]:           0 :         STRUCT_SECTION_FOREACH(log_backend, backend) {
     558         [ #  # ]:           0 :                 if (strcmp(backend_name, backend->name) == 0) {
     559                 :           0 :                         return backend;
     560                 :             :                 }
     561                 :             :         }
     562                 :             : 
     563                 :             :         return NULL;
     564                 :             : }
     565                 :             : 
     566                 :           1 : void log_backend_enable(struct log_backend const *const backend,
     567                 :             :                         void *ctx,
     568                 :             :                         uint32_t level)
     569                 :             : {
     570                 :           1 :         backend->cb->level = level;
     571                 :           1 :         backend_filter_set(backend, level);
     572                 :           1 :         log_backend_activate(backend, ctx);
     573                 :             : 
     574                 :           1 :         z_log_notify_backend_enabled();
     575                 :           1 : }
     576                 :             : 
     577                 :           0 : void log_backend_disable(struct log_backend const *const backend)
     578                 :             : {
     579                 :           0 :         if (log_backend_is_active(backend)) {
     580                 :             :                 backend_filter_set(backend, LOG_LEVEL_NONE);
     581                 :             :         }
     582                 :             : 
     583                 :           0 :         log_backend_deactivate(backend);
     584                 :           0 : }
     585                 :             : 
     586                 :           0 : uint32_t log_filter_get(struct log_backend const *const backend,
     587                 :             :                         uint32_t domain_id, int16_t source_id, bool runtime)
     588                 :             : {
     589         [ #  # ]:           0 :         int id = (backend == NULL) ? -1 : log_backend_id_get(backend);
     590                 :             : 
     591                 :           0 :         return filter_get(id, domain_id, source_id, runtime);
     592                 :             : }
     593                 :             : 
     594                 :           0 : uint32_t log_frontend_filter_get(int16_t source_id, bool runtime)
     595                 :             : {
     596                 :           0 :         if (!IS_ENABLED(CONFIG_LOG_FRONTEND)) {
     597                 :           0 :                 return LOG_LEVEL_NONE;
     598                 :             :         }
     599                 :             : 
     600                 :             :         return filter_get(LOG_FRONTEND_SLOT_ID, Z_LOG_LOCAL_DOMAIN_ID, source_id, runtime);
     601                 :             : }
     602                 :             : 
     603                 :           0 : void z_log_links_initiate(void)
     604                 :             : {
     605                 :           0 :         int err;
     606                 :             : 
     607                 :           0 :         cache_init();
     608                 :             : 
     609   [ #  #  #  # ]:           0 :         STRUCT_SECTION_FOREACH(log_link, link) {
     610                 :             : #ifdef CONFIG_MPSC_PBUF
     611                 :             :                 if (link->mpsc_pbuf) {
     612                 :             :                         mpsc_pbuf_init(link->mpsc_pbuf, link->mpsc_pbuf_config);
     613                 :             :                 }
     614                 :             : #endif
     615                 :             : 
     616                 :           0 :                 err = log_link_initiate(link, NULL);
     617         [ #  # ]:           0 :                 __ASSERT(err == 0, "Failed to initialize link");
     618                 :             :         }
     619                 :           0 : }
     620                 :             : 
     621                 :             : #ifdef CONFIG_LOG_MULTIDOMAIN
     622                 :             : static void backends_link_init(const struct log_link *link)
     623                 :             : {
     624                 :             :         for (int i = 0; i < log_backend_count_get(); i++) {
     625                 :             :                 const struct log_backend *backend = log_backend_get(i);
     626                 :             : 
     627                 :             : 
     628                 :             :                 if (!log_backend_is_active(backend)) {
     629                 :             :                         continue;
     630                 :             :                 }
     631                 :             : 
     632                 :             :                 link_filter_set(link, backend, backend->cb->level);
     633                 :             :         }
     634                 :             : }
     635                 :             : 
     636                 :             : uint32_t z_log_links_activate(uint32_t active_mask, uint8_t *offset)
     637                 :             : {
     638                 :             :         uint32_t mask = 0x1;
     639                 :             :         uint32_t out_mask = 0;
     640                 :             : 
     641                 :             :         /* Initiate offset to 1. */
     642                 :             :         if (*offset == 0) {
     643                 :             :                 *offset = 1;
     644                 :             :         }
     645                 :             : 
     646                 :             :         STRUCT_SECTION_FOREACH(log_link, link) {
     647                 :             :                 if (active_mask & mask) {
     648                 :             :                         int err = log_link_activate(link);
     649                 :             : 
     650                 :             :                         if (err == 0) {
     651                 :             :                                 uint8_t domain_cnt = log_link_domains_count(link);
     652                 :             : 
     653                 :             :                                 link->ctrl_blk->domain_offset = *offset;
     654                 :             :                                 link->ctrl_blk->domain_cnt = domain_cnt;
     655                 :             :                                 *offset += domain_cnt;
     656                 :             :                                 if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
     657                 :             :                                         link_filters_init(link);
     658                 :             :                                         backends_link_init(link);
     659                 :             :                                 }
     660                 :             :                         } else {
     661                 :             :                                 __ASSERT_NO_MSG(err == -EINPROGRESS);
     662                 :             :                                 out_mask |= mask;
     663                 :             :                         }
     664                 :             :                 }
     665                 :             : 
     666                 :             :                 mask <<= 1;
     667                 :             :         }
     668                 :             : 
     669                 :             :         return out_mask;
     670                 :             : }
     671                 :             : #endif
        

Generated by: LCOV version 2.0-1