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/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
domain_id_cmp(uintptr_t id0,uintptr_t id1)58 static bool domain_id_cmp(uintptr_t id0, uintptr_t id1)
59 {
60 return id0 == id1;
61 }
62
source_id_cmp(uintptr_t id0,uintptr_t id1)63 static bool source_id_cmp(uintptr_t id0, uintptr_t id1)
64 {
65 union log_source_ids s0 = { .raw = id0 };
66 union log_source_ids s1 = { .raw = id1 };
67
68 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 */
get_link_domain(uint8_t domain_id,uint8_t * rel_domain_id)84 static const struct log_link *get_link_domain(uint8_t domain_id, uint8_t *rel_domain_id)
85 {
86 uint8_t domain_max;
87
88 STRUCT_SECTION_FOREACH(log_link, link) {
89 domain_max = link->ctrl_blk->domain_offset +
90 link->ctrl_blk->domain_cnt;
91 if (domain_id < domain_max) {
92
93 *rel_domain_id = domain_id - link->ctrl_blk->domain_offset;
94 return link;
95 }
96 }
97
98 *rel_domain_id = 0;
99
100 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 */
get_source_offset(const struct log_link * link,uint8_t rel_domain_id)109 static uint32_t get_source_offset(const struct log_link *link,
110 uint8_t rel_domain_id)
111 {
112 uint32_t offset = 0;
113
114 for (uint8_t i = 0; i < rel_domain_id; i++) {
115 offset += log_link_sources_count(link, i);
116 }
117
118 return offset;
119 }
120
z_log_link_get_dynamic_filter(uint8_t domain_id,uint32_t source_id)121 uint32_t *z_log_link_get_dynamic_filter(uint8_t domain_id, uint32_t source_id)
122 {
123 uint8_t rel_domain_id;
124 const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
125 uint32_t source_offset = 0;
126
127 __ASSERT_NO_MSG(link != NULL);
128
129 source_offset = get_source_offset(link, rel_domain_id);
130
131 return &link->ctrl_blk->filters[source_offset + source_id];
132 }
133
134 #ifdef CONFIG_LOG_MULTIDOMAIN
link_filters_init(const struct log_link * link)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
cache_init(void)154 static void cache_init(void)
155 {
156 int err;
157 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 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 err = log_cache_init(&dname_cache, &dname_cache_config);
171 __ASSERT_NO_MSG(err == 0);
172
173 err = log_cache_init(&sname_cache, &sname_cache_config);
174 __ASSERT_NO_MSG(err == 0);
175 }
176
z_log_ext_domain_count(void)177 uint8_t z_log_ext_domain_count(void)
178 {
179 uint8_t cnt = 0;
180
181 STRUCT_SECTION_FOREACH(log_link, link) {
182 cnt += log_link_domains_count(link);
183 }
184
185 return cnt;
186 }
187
link_source_count(uint8_t domain_id)188 static uint16_t link_source_count(uint8_t domain_id)
189 {
190 uint8_t rel_domain_id;
191 const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
192
193 __ASSERT_NO_MSG(link != NULL);
194
195 return log_link_sources_count(link, rel_domain_id);
196 }
197
log_src_cnt_get(uint32_t domain_id)198 uint32_t log_src_cnt_get(uint32_t domain_id)
199 {
200 if (z_log_is_local_domain(domain_id)) {
201 return z_log_sources_count();
202 }
203
204 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 */
link_source_name_get(uint8_t domain_id,uint32_t source_id)212 static const char *link_source_name_get(uint8_t domain_id, uint32_t source_id)
213 {
214 uint8_t *cached;
215 size_t cache_size = sname_cache.item_size;
216 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 if (!log_cache_get(&sname_cache, id.raw, &cached)) {
225 uint8_t rel_domain_id;
226 const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
227 int err;
228
229 __ASSERT_NO_MSG(link != NULL);
230
231 err = log_link_get_source_name(link, rel_domain_id, source_id,
232 cached, &cache_size);
233 if (err < 0) {
234 return NULL;
235 }
236
237 log_cache_put(&sname_cache, cached);
238 }
239
240 return (const char *)cached;
241 }
242
log_source_name_get(uint32_t domain_id,uint32_t source_id)243 const char *log_source_name_get(uint32_t domain_id, uint32_t source_id)
244 {
245 if (z_log_is_local_domain(domain_id)) {
246 if (source_id < log_src_cnt_get(domain_id)) {
247 return TYPE_SECTION_START(log_const)[source_id].name;
248 } else {
249 return NULL;
250 }
251 }
252
253 return link_source_name_get(domain_id, source_id);
254 }
255
256 /* First check in cache if not there fetch from remote.
257 * When fetched from remote put in cache.
258 *
259 * @note Execution time depends on whether entry is in cache.
260 */
link_domain_name_get(uint8_t domain_id)261 static const char *link_domain_name_get(uint8_t domain_id)
262 {
263 uint8_t *cached;
264 size_t cache_size = dname_cache.item_size;
265 uintptr_t id = (uintptr_t)domain_id;
266 static const char *invalid_domain = "invalid";
267
268 /* If not in cache fetch from link and cache it. */
269 if (!log_cache_get(&dname_cache, id, &cached)) {
270 uint8_t rel_domain_id;
271 const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
272 int err;
273
274 __ASSERT_NO_MSG(link != NULL);
275
276 err = log_link_get_domain_name(link, rel_domain_id, cached, &cache_size);
277 if (err < 0) {
278 log_cache_release(&dname_cache, cached);
279 return invalid_domain;
280 }
281
282 log_cache_put(&dname_cache, cached);
283 }
284
285 return (const char *)cached;
286 }
287
log_domain_name_get(uint32_t domain_id)288 const char *log_domain_name_get(uint32_t domain_id)
289 {
290 if (z_log_is_local_domain(domain_id)) {
291 return CONFIG_LOG_DOMAIN_NAME;
292 }
293
294 return link_domain_name_get(domain_id);
295 }
296
link_compiled_level_get(uint8_t domain_id,uint32_t source_id)297 static uint8_t link_compiled_level_get(uint8_t domain_id, uint32_t source_id)
298 {
299 uint8_t rel_domain_id;
300 const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
301 uint8_t level;
302
303 __ASSERT_NO_MSG(link != NULL);
304
305 return !log_link_get_levels(link, rel_domain_id, source_id, &level, NULL) ?
306 level : 0;
307 }
308
log_compiled_level_get(uint8_t domain_id,uint32_t source_id)309 uint8_t log_compiled_level_get(uint8_t domain_id, uint32_t source_id)
310 {
311 if (z_log_is_local_domain(domain_id)) {
312 if (source_id < log_src_cnt_get(domain_id)) {
313 return TYPE_SECTION_START(log_const)[source_id].level;
314 } else {
315 return LOG_LEVEL_NONE;
316 }
317 }
318
319 return link_compiled_level_get(domain_id, source_id);
320 }
321
z_log_link_set_runtime_level(uint8_t domain_id,uint16_t source_id,uint8_t level)322 int z_log_link_set_runtime_level(uint8_t domain_id, uint16_t source_id, uint8_t level)
323 {
324 uint8_t rel_domain_id;
325 const struct log_link *link = get_link_domain(domain_id, &rel_domain_id);
326
327 __ASSERT_NO_MSG(link != NULL);
328
329 return log_link_set_runtime_level(link, rel_domain_id, source_id, level);
330 }
331
get_dynamic_filter(uint8_t domain_id,uint32_t source_id)332 static uint32_t *get_dynamic_filter(uint8_t domain_id, uint32_t source_id)
333 {
334 if (z_log_is_local_domain(domain_id)) {
335 return &TYPE_SECTION_START(log_dynamic)[source_id].filters;
336 }
337
338 return z_log_link_get_dynamic_filter(domain_id, source_id);
339 }
340
z_log_runtime_filters_init(void)341 void z_log_runtime_filters_init(void)
342 {
343 /*
344 * Initialize aggregated runtime filter levels (no backends are
345 * attached yet, so leave backend slots in each dynamic filter set
346 * alone for now).
347 *
348 * Each log source's aggregated runtime level is set to match its
349 * compile-time level. When backends are attached later on in
350 * log_init(), they'll be initialized to the same value.
351 */
352 for (int i = 0; i < z_log_sources_count(); i++) {
353 uint32_t *filters = z_log_dynamic_filters_get(i);
354 uint8_t level = log_compiled_level_get(Z_LOG_LOCAL_DOMAIN_ID, i);
355
356 level = MAX(level, CONFIG_LOG_OVERRIDE_LEVEL);
357 LOG_FILTER_SLOT_SET(filters,
358 LOG_FILTER_AGGR_SLOT_IDX,
359 level);
360 }
361 }
362
log_source_id_get(const char * name)363 int log_source_id_get(const char *name)
364 {
365 for (int i = 0; i < log_src_cnt_get(Z_LOG_LOCAL_DOMAIN_ID); i++) {
366 const char *sname = log_source_name_get(Z_LOG_LOCAL_DOMAIN_ID, i);
367
368 if ((sname != NULL) && (strcmp(sname, name) == 0)) {
369 return i;
370 }
371 }
372 return -1;
373 }
374
max_filter_get(uint32_t filters)375 static uint32_t max_filter_get(uint32_t filters)
376 {
377 uint32_t max_filter = LOG_LEVEL_NONE;
378 int first_slot = LOG_FILTER_FIRST_BACKEND_SLOT_IDX;
379 int i;
380
381 for (i = first_slot; i < LOG_FILTERS_NUM_OF_SLOTS; i++) {
382 uint32_t tmp_filter = LOG_FILTER_SLOT_GET(&filters, i);
383
384 if (tmp_filter > max_filter) {
385 max_filter = tmp_filter;
386 }
387 }
388
389 return max_filter;
390 }
391
set_runtime_filter(uint8_t backend_id,uint8_t domain_id,uint32_t source_id,uint32_t level)392 static void set_runtime_filter(uint8_t backend_id, uint8_t domain_id,
393 uint32_t source_id, uint32_t level)
394 {
395 uint32_t prev_max;
396 uint32_t new_max;
397 uint32_t *filters = get_dynamic_filter(domain_id, source_id);
398
399 prev_max = LOG_FILTER_SLOT_GET(filters, LOG_FILTER_AGGR_SLOT_IDX);
400
401 LOG_FILTER_SLOT_SET(filters, backend_id, level);
402
403 /* Once current backend filter is updated recalculate
404 * aggregated maximal level
405 */
406 new_max = max_filter_get(*filters);
407
408 LOG_FILTER_SLOT_SET(filters, LOG_FILTER_AGGR_SLOT_IDX, new_max);
409
410 if (!z_log_is_local_domain(domain_id) && (new_max != prev_max)) {
411 (void)z_log_link_set_runtime_level(domain_id, source_id, level);
412 }
413 }
414
z_impl_log_filter_set(struct log_backend const * const backend,uint32_t domain_id,int16_t source_id,uint32_t level)415 uint32_t z_impl_log_filter_set(struct log_backend const *const backend,
416 uint32_t domain_id, int16_t source_id,
417 uint32_t level)
418 {
419 if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
420 return log_compiled_level_get(domain_id, source_id);
421 }
422
423 __ASSERT_NO_MSG(source_id < log_src_cnt_get(domain_id));
424
425
426 if (backend == NULL) {
427 uint32_t max = 0U;
428
429 STRUCT_SECTION_FOREACH(log_backend, iter_backend) {
430 uint32_t current = log_filter_set(iter_backend,
431 domain_id, source_id, level);
432
433 max = MAX(current, max);
434 }
435
436 return max;
437 }
438
439 level = MIN(level, MAX(log_filter_get(backend, domain_id, source_id, false),
440 CONFIG_LOG_OVERRIDE_LEVEL));
441 set_runtime_filter(log_backend_id_get(backend), domain_id, source_id, level);
442
443 return level;
444 }
445
446 #ifdef CONFIG_USERSPACE
z_vrfy_log_filter_set(struct log_backend const * const backend,uint32_t domain_id,int16_t src_id,uint32_t level)447 uint32_t z_vrfy_log_filter_set(struct log_backend const *const backend,
448 uint32_t domain_id,
449 int16_t src_id,
450 uint32_t level)
451 {
452 Z_OOPS(Z_SYSCALL_VERIFY_MSG(backend == NULL,
453 "Setting per-backend filters from user mode is not supported"));
454 Z_OOPS(Z_SYSCALL_VERIFY_MSG(domain_id == Z_LOG_LOCAL_DOMAIN_ID,
455 "Invalid log domain_id"));
456 Z_OOPS(Z_SYSCALL_VERIFY_MSG(src_id < (int16_t)log_src_cnt_get(domain_id),
457 "Invalid log source id"));
458 Z_OOPS(Z_SYSCALL_VERIFY_MSG(
459 (level <= LOG_LEVEL_DBG),
460 "Invalid log level"));
461
462 return z_impl_log_filter_set(NULL, domain_id, src_id, level);
463 }
464 #include <syscalls/log_filter_set_mrsh.c>
465 #endif
466
link_filter_set(const struct log_link * link,struct log_backend const * const backend,uint32_t level)467 static void link_filter_set(const struct log_link *link,
468 struct log_backend const *const backend,
469 uint32_t level)
470 {
471 if (log_link_is_active(link) != 0) {
472 return;
473 }
474
475 for (uint8_t d = link->ctrl_blk->domain_offset;
476 d < link->ctrl_blk->domain_offset + link->ctrl_blk->domain_cnt; d++) {
477 for (uint16_t s = 0; s < log_src_cnt_get(d); s++) {
478 log_filter_set(backend, d, s, level);
479 }
480 }
481 }
482
backend_filter_set(struct log_backend const * const backend,uint32_t level)483 static void backend_filter_set(struct log_backend const *const backend,
484 uint32_t level)
485 {
486 if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
487 return;
488 }
489
490 for (uint16_t s = 0; s < log_src_cnt_get(0); s++) {
491 log_filter_set(backend, 0, s, level);
492 }
493
494 if (!IS_ENABLED(CONFIG_LOG_MULTIDOMAIN)) {
495 return;
496 }
497
498 /* Set level in activated links. */
499 STRUCT_SECTION_FOREACH(log_link, link) {
500 link_filter_set(link, backend, level);
501 }
502 }
503
log_backend_get_by_name(const char * backend_name)504 const struct log_backend *log_backend_get_by_name(const char *backend_name)
505 {
506 STRUCT_SECTION_FOREACH(log_backend, backend) {
507 if (strcmp(backend_name, backend->name) == 0) {
508 return backend;
509 }
510 }
511
512 return NULL;
513 }
514
log_backend_enable(struct log_backend const * const backend,void * ctx,uint32_t level)515 void log_backend_enable(struct log_backend const *const backend,
516 void *ctx,
517 uint32_t level)
518 {
519 /* As first slot in filtering mask is reserved, backend ID has offset.*/
520 uint32_t id = LOG_FILTER_FIRST_BACKEND_SLOT_IDX;
521
522 id += backend - log_backend_get(0);
523
524 log_backend_id_set(backend, id);
525 backend->cb->level = level;
526 backend_filter_set(backend, level);
527 log_backend_activate(backend, ctx);
528
529 z_log_notify_backend_enabled();
530 }
531
log_backend_disable(struct log_backend const * const backend)532 void log_backend_disable(struct log_backend const *const backend)
533 {
534 if (log_backend_is_active(backend)) {
535 backend_filter_set(backend, LOG_LEVEL_NONE);
536 }
537
538 log_backend_deactivate(backend);
539 }
540
log_filter_get(struct log_backend const * const backend,uint32_t domain_id,int16_t source_id,bool runtime)541 uint32_t log_filter_get(struct log_backend const *const backend,
542 uint32_t domain_id, int16_t source_id, bool runtime)
543 {
544 __ASSERT_NO_MSG(source_id < log_src_cnt_get(domain_id));
545
546 if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) && runtime) {
547 if (source_id < 0) {
548 return LOG_LEVEL_DBG;
549 }
550
551 return LOG_FILTER_SLOT_GET(get_dynamic_filter(domain_id, source_id),
552 log_backend_id_get(backend));
553 }
554
555 return log_compiled_level_get(domain_id, source_id);
556 }
557
z_log_links_initiate(void)558 void z_log_links_initiate(void)
559 {
560 int err;
561
562 cache_init();
563
564 STRUCT_SECTION_FOREACH(log_link, link) {
565 #ifdef CONFIG_MPSC_PBUF
566 if (link->mpsc_pbuf) {
567 mpsc_pbuf_init(link->mpsc_pbuf, link->mpsc_pbuf_config);
568 }
569 #endif
570
571 err = log_link_initiate(link, NULL);
572 __ASSERT(err == 0, "Failed to initialize link");
573 }
574 }
575
576 #ifdef CONFIG_LOG_MULTIDOMAIN
backends_link_init(const struct log_link * link)577 static void backends_link_init(const struct log_link *link)
578 {
579 for (int i = 0; i < log_backend_count_get(); i++) {
580 const struct log_backend *backend = log_backend_get(i);
581
582
583 if (!log_backend_is_active(backend)) {
584 continue;
585 }
586
587 link_filter_set(link, backend, backend->cb->level);
588 }
589 }
590
z_log_links_activate(uint32_t active_mask,uint8_t * offset)591 uint32_t z_log_links_activate(uint32_t active_mask, uint8_t *offset)
592 {
593 uint32_t mask = 0x1;
594 uint32_t out_mask = 0;
595
596 /* Initiate offset to 1. */
597 if (*offset == 0) {
598 *offset = 1;
599 }
600
601 STRUCT_SECTION_FOREACH(log_link, link) {
602 if (active_mask & mask) {
603 int err = log_link_activate(link);
604
605 if (err == 0) {
606 uint8_t domain_cnt = log_link_domains_count(link);
607
608 link->ctrl_blk->domain_offset = *offset;
609 link->ctrl_blk->domain_cnt = domain_cnt;
610 *offset += domain_cnt;
611 if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
612 link_filters_init(link);
613 backends_link_init(link);
614 }
615 } else {
616 __ASSERT_NO_MSG(err == -EINPROGRESS);
617 out_mask |= mask;
618 }
619 }
620
621 mask <<= 1;
622 }
623
624 return out_mask;
625 }
626 #endif
627