Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2020 Nordic Semiconductor
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : : #include <zephyr/kernel.h>
7 : : #include <zephyr/internal/syscall_handler.h>
8 : : #include <zephyr/logging/log_internal.h>
9 : : #include <zephyr/logging/log_ctrl.h>
10 : : #include <zephyr/logging/log_frontend.h>
11 : : #include <zephyr/logging/log_backend.h>
12 : : #include <zephyr/logging/log.h>
13 : : #include <zephyr/llext/symbol.h>
14 : : LOG_MODULE_DECLARE(log);
15 : :
16 : : BUILD_ASSERT(sizeof(struct log_msg_desc) == sizeof(uint32_t),
17 : : "Descriptor must fit in 32 bits");
18 : :
19 : : /* Returns true if any backend is in use. */
20 : : #define BACKENDS_IN_USE() \
21 : : !(IS_ENABLED(CONFIG_LOG_FRONTEND) && \
22 : : (IS_ENABLED(CONFIG_LOG_FRONTEND_ONLY) || log_backend_count_get() == 0))
23 : :
24 : : #define CBPRINTF_DESC_SIZE32 (sizeof(struct cbprintf_package_desc) / sizeof(uint32_t))
25 : :
26 : : /*
27 : : * cbvprintf_package() requires its buffer to be aligned at least to the size
28 : : * of a pointer. Avoid alignment arithmetic when the stack already provides
29 : : * that guarantee.
30 : : *
31 : : * Use the logging alignment for the fallback so the package address matches
32 : : * the alignment assumed when its size was calculated.
33 : : */
34 : : #define LOG_MSG_ALIGNED_ALLOCA(_size) \
35 : : (IS_ALIGNED(ARCH_STACK_PTR_ALIGN, sizeof(void *)) \
36 : : ? alloca(_size) \
37 : : : (void *)ROUND_UP((uintptr_t)alloca((_size) + Z_LOG_MSG_ALIGNMENT - 1U), \
38 : : Z_LOG_MSG_ALIGNMENT))
39 : :
40 : : /* For simplified message handling cprintf package must have only 1 word. */
41 : : BUILD_ASSERT(!IS_ENABLED(CONFIG_LOG_SIMPLE_MSG_OPTIMIZE) ||
42 : : (IS_ENABLED(CONFIG_LOG_SIMPLE_MSG_OPTIMIZE) && (CBPRINTF_DESC_SIZE32 == 1)));
43 : :
44 : :
45 : 209 : void z_log_msg_finalize(struct log_msg *msg, const void *source,
46 : : const struct log_msg_desc desc, const void *data)
47 : : {
48 [ - + ]: 209 : if (!msg) {
49 : 0 : z_log_dropped(false);
50 : :
51 : 0 : return;
52 : : }
53 : :
54 [ - + ]: 209 : if (data) {
55 : 0 : uint8_t *d = msg->data + desc.package_len;
56 : :
57 : 0 : memcpy(d, data, desc.data_len);
58 : : }
59 : :
60 : 209 : msg->hdr.desc = desc;
61 : 209 : msg->hdr.source = source;
62 : : #if CONFIG_LOG_THREAD_ID_PREFIX
63 : : msg->hdr.tid = (k_is_in_isr() || k_is_pre_kernel()) ? NULL : k_current_get();
64 : : #endif
65 : : #if CONFIG_LOG_CORE_ID_PREFIX
66 : : msg->hdr.core_id = arch_proc_id();
67 : : #endif
68 : 209 : z_log_msg_commit(msg);
69 : : }
70 : :
71 : : static bool frontend_runtime_filtering(const void *source, uint32_t level)
72 : : {
73 : : if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
74 : : return true;
75 : : }
76 : :
77 : : /* If only frontend is used and log got here it means that it was accepted
78 : : * unless userspace is enabled then runtime filtering is done here.
79 : : */
80 : : if (!IS_ENABLED(CONFIG_USERSPACE) && IS_ENABLED(CONFIG_LOG_FRONTEND_ONLY)) {
81 : : return true;
82 : : }
83 : :
84 : : if (level == LOG_LEVEL_NONE) {
85 : : return true;
86 : : }
87 : :
88 : : struct log_source_dynamic_data *dynamic = (struct log_source_dynamic_data *)source;
89 : : uint32_t f_level = LOG_FILTER_SLOT_GET(&dynamic->filters, LOG_FRONTEND_SLOT_ID);
90 : :
91 : : return level <= f_level;
92 : : }
93 : :
94 : : /** @brief Create a log message using simplified method.
95 : : *
96 : : * Simple log message has 0-2 32 bit word arguments so creating cbprintf package
97 : : * is straightforward as there is no padding or alignment to concern about.
98 : : * This function takes input data which is fmt pointer + 0-2 arguments, creates
99 : : * package header which is very simple as it only contain non-zero length field.
100 : : * Then space is allocated and message is committed. Such simple approach can
101 : : * be applied because it is known that input string does not have any arguments
102 : : * which complicate things (string pointers, floating numbers). Simple method is
103 : : * also limited to 32 bit arch.
104 : : *
105 : : * @param source Source.
106 : : * @param level Severity level.
107 : : * @param data Package content (without header).
108 : : * @param len Package content length in words.
109 : : */
110 : 0 : static void z_log_msg_simple_create(const void *source, uint32_t level, uint32_t *data, size_t len)
111 : : {
112 : : /* Package length (in words) is increased by the header. */
113 : 0 : size_t plen32 = len + CBPRINTF_DESC_SIZE32;
114 : : /* Package length in bytes. */
115 : 0 : size_t plen8 = sizeof(uint32_t) * plen32 +
116 : : (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? 1 : 0);
117 : 0 : struct log_msg *msg = z_log_msg_alloc(Z_LOG_MSG_ALIGNED_WLEN(plen8, 0));
118 : 0 : union cbprintf_package_hdr package_hdr = {
119 : : .desc = {
120 : : .len = plen32,
121 : : .ro_str_cnt = IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? 1 : 0
122 : : }
123 : : };
124 : :
125 [ # # ]: 0 : if (msg) {
126 : 0 : uint32_t *package = (uint32_t *)msg->data;
127 : :
128 : 0 : *package++ = (uint32_t)(uintptr_t)package_hdr.raw;
129 [ # # ]: 0 : for (size_t i = 0; i < len; i++) {
130 : 0 : *package++ = data[i];
131 : : }
132 : : if (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC)) {
133 : : /* fmt string located at index 1 */
134 : : *(uint8_t *)package = 1;
135 : : }
136 : : }
137 : :
138 : 0 : struct log_msg_desc desc = {
139 : : .level = level,
140 : : .package_len = plen8,
141 : : .data_len = 0,
142 : : };
143 : :
144 : 0 : z_log_msg_finalize(msg, source, desc, NULL);
145 : 0 : }
146 : :
147 : 0 : void z_impl_z_log_msg_simple_create_0(const void *source, uint32_t level, const char *fmt)
148 : : {
149 : :
150 : 0 : if (IS_ENABLED(CONFIG_LOG_FRONTEND) && frontend_runtime_filtering(source, level)) {
151 : : if (IS_ENABLED(CONFIG_LOG_FRONTEND_OPT_API)) {
152 : : log_frontend_simple_0(source, level, fmt);
153 : : } else {
154 : : /* If frontend does not support optimized API prepare data for
155 : : * the generic call.
156 : : */
157 : : uint32_t plen32 = CBPRINTF_DESC_SIZE32 + 1;
158 : : union cbprintf_package_hdr hdr = {
159 : : .desc = {
160 : : .len = plen32,
161 : : .ro_str_cnt =
162 : : IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? 1 : 0
163 : : }
164 : : };
165 : : uint8_t package[sizeof(uint32_t) * (CBPRINTF_DESC_SIZE32 + 1) +
166 : : (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? 1 : 0)]
167 : : __aligned(sizeof(uint32_t));
168 : : uint32_t *p32 = (uint32_t *)package;
169 : :
170 : : *p32++ = (uint32_t)(uintptr_t)hdr.raw;
171 : : *p32++ = (uint32_t)(uintptr_t)fmt;
172 : : if (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC)) {
173 : : /* fmt string located at index 1 */
174 : : *(uint8_t *)p32 = 1;
175 : : }
176 : :
177 : : struct log_msg_desc desc = {
178 : : .level = level,
179 : : .package_len = sizeof(package),
180 : : .data_len = 0,
181 : : };
182 : :
183 : : log_frontend_msg(source, desc, package, NULL);
184 : : }
185 : : }
186 : :
187 : 0 : if (!BACKENDS_IN_USE()) {
188 : : return;
189 : : }
190 : :
191 : 0 : uint32_t data[] = {(uint32_t)(uintptr_t)fmt};
192 : :
193 : 0 : z_log_msg_simple_create(source, level, data, ARRAY_SIZE(data));
194 : : }
195 : :
196 : 0 : void z_impl_z_log_msg_simple_create_1(const void *source, uint32_t level,
197 : : const char *fmt, uint32_t arg)
198 : : {
199 : 0 : if (IS_ENABLED(CONFIG_LOG_FRONTEND) && frontend_runtime_filtering(source, level)) {
200 : : if (IS_ENABLED(CONFIG_LOG_FRONTEND_OPT_API)) {
201 : : log_frontend_simple_1(source, level, fmt, arg);
202 : : } else {
203 : : /* If frontend does not support optimized API prepare data for
204 : : * the generic call.
205 : : */
206 : : uint32_t plen32 = CBPRINTF_DESC_SIZE32 + 2;
207 : : union cbprintf_package_hdr hdr = {
208 : : .desc = {
209 : : .len = plen32,
210 : : .ro_str_cnt =
211 : : IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? 1 : 0
212 : : }
213 : : };
214 : : uint8_t package[sizeof(uint32_t) * (CBPRINTF_DESC_SIZE32 + 2) +
215 : : (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? 1 : 0)]
216 : : __aligned(sizeof(uint32_t));
217 : : uint32_t *p32 = (uint32_t *)package;
218 : :
219 : : *p32++ = (uint32_t)(uintptr_t)hdr.raw;
220 : : *p32++ = (uint32_t)(uintptr_t)fmt;
221 : : *p32++ = arg;
222 : : if (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC)) {
223 : : /* fmt string located at index 1 */
224 : : *(uint8_t *)p32 = 1;
225 : : }
226 : :
227 : : struct log_msg_desc desc = {
228 : : .level = level,
229 : : .package_len = sizeof(package),
230 : : .data_len = 0,
231 : : };
232 : :
233 : : log_frontend_msg(source, desc, package, NULL);
234 : : }
235 : : }
236 : :
237 : 0 : if (!BACKENDS_IN_USE()) {
238 : : return;
239 : : }
240 : :
241 : 0 : uint32_t data[] = {(uint32_t)(uintptr_t)fmt, arg};
242 : :
243 : 0 : z_log_msg_simple_create(source, level, data, ARRAY_SIZE(data));
244 : : }
245 : :
246 : 0 : void z_impl_z_log_msg_simple_create_2(const void *source, uint32_t level,
247 : : const char *fmt, uint32_t arg0, uint32_t arg1)
248 : : {
249 : 0 : if (IS_ENABLED(CONFIG_LOG_FRONTEND) && frontend_runtime_filtering(source, level)) {
250 : : if (IS_ENABLED(CONFIG_LOG_FRONTEND_OPT_API)) {
251 : : log_frontend_simple_2(source, level, fmt, arg0, arg1);
252 : : } else {
253 : : /* If frontend does not support optimized API prepare data for
254 : : * the generic call.
255 : : */
256 : : uint32_t plen32 = CBPRINTF_DESC_SIZE32 + 3;
257 : : union cbprintf_package_hdr hdr = {
258 : : .desc = {
259 : : .len = plen32,
260 : : .ro_str_cnt =
261 : : IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? 1 : 0
262 : : }
263 : : };
264 : : uint8_t package[sizeof(uint32_t) * (CBPRINTF_DESC_SIZE32 + 3) +
265 : : (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ? 1 : 0)]
266 : : __aligned(sizeof(uint32_t));
267 : : uint32_t *p32 = (uint32_t *)package;
268 : :
269 : : *p32++ = (uint32_t)(uintptr_t)hdr.raw;
270 : : *p32++ = (uint32_t)(uintptr_t)fmt;
271 : : *p32++ = arg0;
272 : : *p32++ = arg1;
273 : : if (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC)) {
274 : : /* fmt string located at index 1 */
275 : : *(uint8_t *)p32 = 1;
276 : : }
277 : :
278 : : struct log_msg_desc desc = {
279 : : .level = level,
280 : : .package_len = sizeof(package),
281 : : .data_len = 0,
282 : : };
283 : :
284 : : log_frontend_msg(source, desc, package, NULL);
285 : : }
286 : : }
287 : :
288 : 0 : if (!BACKENDS_IN_USE()) {
289 : : return;
290 : : }
291 : :
292 : 0 : uint32_t data[] = {(uint32_t)(uintptr_t)fmt, arg0, arg1};
293 : :
294 : 0 : z_log_msg_simple_create(source, level, data, ARRAY_SIZE(data));
295 : : }
296 : :
297 : 0 : void z_impl_z_log_msg_static_create(const void *source,
298 : : const struct log_msg_desc desc,
299 : : uint8_t *package, const void *data)
300 : : {
301 : 0 : if (IS_ENABLED(CONFIG_LOG_FRONTEND) && frontend_runtime_filtering(source, desc.level)) {
302 : : log_frontend_msg(source, desc, package, data);
303 : : }
304 : :
305 : 0 : if (!BACKENDS_IN_USE()) {
306 : 0 : return;
307 : : }
308 : :
309 : 0 : struct log_msg_desc out_desc = desc;
310 : 0 : int inlen = desc.package_len;
311 : 0 : struct log_msg *msg;
312 : :
313 [ # # ]: 0 : if (inlen > 0) {
314 : 0 : uint32_t flags = CBPRINTF_PACKAGE_CONVERT_RW_STR |
315 : : (IS_ENABLED(CONFIG_LOG_MSG_APPEND_RO_STRING_LOC) ?
316 : : CBPRINTF_PACKAGE_CONVERT_KEEP_RO_STR : 0) |
317 : : (IS_ENABLED(CONFIG_LOG_FMT_SECTION_STRIP) ?
318 : : 0 : CBPRINTF_PACKAGE_CONVERT_PTR_CHECK);
319 : 0 : uint16_t strl[4];
320 : 0 : int len;
321 : :
322 : 0 : len = cbprintf_package_copy(package, inlen,
323 : : NULL, 0, flags,
324 : : strl, ARRAY_SIZE(strl));
325 : :
326 [ # # ]: 0 : if (len > Z_LOG_MSG_MAX_PACKAGE) {
327 : 0 : struct cbprintf_package_hdr_ext *pkg =
328 : : (struct cbprintf_package_hdr_ext *)package;
329 : :
330 : 0 : LOG_WRN("Message (\"%s\") dropped because it exceeds size limitation (%u)",
331 : : pkg->fmt, (uint32_t)Z_LOG_MSG_MAX_PACKAGE);
332 : 0 : return;
333 : : }
334 : : /* Update package length with calculated value (which may be extended
335 : : * when strings are copied into the package.
336 : : */
337 : 0 : out_desc.package_len = len;
338 : 0 : msg = z_log_msg_alloc(log_msg_get_total_wlen(out_desc));
339 [ # # ]: 0 : if (msg) {
340 : 0 : len = cbprintf_package_copy(package, inlen,
341 : 0 : msg->data, out_desc.package_len,
342 : : flags, strl, ARRAY_SIZE(strl));
343 [ # # ]: 0 : __ASSERT_NO_MSG(len >= 0);
344 : : }
345 : : } else {
346 : 0 : msg = z_log_msg_alloc(log_msg_get_total_wlen(out_desc));
347 : : }
348 : :
349 : 0 : z_log_msg_finalize(msg, source, out_desc, data);
350 : : }
351 : :
352 : : #ifdef CONFIG_USERSPACE
353 : : static inline void z_vrfy_z_log_msg_static_create(const void *source,
354 : : const struct log_msg_desc desc,
355 : : uint8_t *package, const void *data)
356 : : {
357 : : K_OOPS(K_SYSCALL_VERIFY(desc.package_len <= Z_LOG_MSG_MAX_PACKAGE));
358 : :
359 : : K_OOPS(K_SYSCALL_VERIFY((desc.package_len == 0) || (package != NULL)));
360 : : K_OOPS(K_SYSCALL_VERIFY((desc.data_len == 0) || (data != NULL)));
361 : :
362 : : if (desc.package_len > 0) {
363 : : K_OOPS(K_SYSCALL_MEMORY_READ(package, desc.package_len));
364 : : }
365 : :
366 : : if (desc.data_len > 0) {
367 : : K_OOPS(K_SYSCALL_MEMORY_READ(data, desc.data_len));
368 : : }
369 : :
370 : : if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING) &&
371 : : IS_ENABLED(CONFIG_LOG_FRONTEND) &&
372 : : (desc.level != LOG_LEVEL_NONE)) {
373 : : size_t source_size = sizeof(struct log_source_dynamic_data);
374 : :
375 : : K_OOPS(K_SYSCALL_VERIFY(source != NULL));
376 : : K_OOPS(K_SYSCALL_MEMORY_READ(source, source_size));
377 : : }
378 : :
379 : : z_impl_z_log_msg_static_create(source, desc, package, data);
380 : : }
381 : : #include <zephyr/syscalls/z_log_msg_static_create_mrsh.c>
382 : : #endif
383 : :
384 : 209 : void z_log_msg_runtime_vcreate(uint8_t domain_id, const void *source,
385 : : uint8_t level, const void *data, size_t dlen,
386 : : uint32_t package_flags, const char *fmt, va_list ap)
387 : : {
388 : 209 : int plen;
389 : :
390 [ + - ]: 209 : if (fmt) {
391 : 209 : va_list ap2;
392 : :
393 : 209 : va_copy(ap2, ap);
394 : 209 : plen = cbvprintf_package(NULL, Z_LOG_MSG_ALIGN_OFFSET,
395 : : package_flags, fmt, ap2);
396 [ - + ]: 209 : __ASSERT_NO_MSG(plen >= 0);
397 : 209 : va_end(ap2);
398 : : } else {
399 : : plen = 0;
400 : : }
401 : :
402 [ - + ]: 209 : if (plen > Z_LOG_MSG_MAX_PACKAGE) {
403 : 0 : LOG_WRN("Message dropped because it exceeds size limitation (%u)",
404 : : (uint32_t)Z_LOG_MSG_MAX_PACKAGE);
405 : 0 : return;
406 : : }
407 : :
408 : 209 : size_t msg_wlen = Z_LOG_MSG_ALIGNED_WLEN(plen, dlen);
409 : 209 : struct log_msg *msg;
410 : 209 : uint8_t *pkg;
411 : 209 : struct log_msg_desc desc =
412 : : Z_LOG_MSG_DESC_INITIALIZER(domain_id, level, plen, dlen);
413 : :
414 [ - + ]: 209 : if (k_is_user_context()) {
415 : 0 : pkg = LOG_MSG_ALIGNED_ALLOCA(plen);
416 : 0 : msg = NULL;
417 : 209 : } else if (IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) && BACKENDS_IN_USE()) {
418 : : compiler_barrier();
419 : : msg = z_log_msg_alloc(msg_wlen);
420 : : if (IS_ENABLED(CONFIG_LOG_FRONTEND) && msg == NULL) {
421 : : pkg = LOG_MSG_ALIGNED_ALLOCA(plen);
422 : : } else {
423 : : pkg = msg ? msg->data : NULL;
424 : : }
425 : : } else {
426 : 209 : compiler_barrier();
427 : 209 : msg = LOG_MSG_ALIGNED_ALLOCA(msg_wlen * sizeof(int));
428 : 209 : pkg = msg->data;
429 : : }
430 : :
431 [ + - ]: 209 : if (pkg && fmt) {
432 : 209 : plen = cbvprintf_package(pkg, (size_t)plen, package_flags, fmt, ap);
433 [ - + ]: 209 : __ASSERT_NO_MSG(plen >= 0);
434 : : }
435 : :
436 [ - + ]: 209 : if (k_is_user_context()) {
437 : 0 : z_log_msg_static_create(source, desc, pkg, data);
438 : : } else {
439 : 209 : compiler_barrier();
440 : 209 : if (IS_ENABLED(CONFIG_LOG_FRONTEND) &&
441 : : frontend_runtime_filtering(source, desc.level)) {
442 : : log_frontend_msg(source, desc, pkg, data);
443 : : }
444 : :
445 : 209 : if (BACKENDS_IN_USE()) {
446 : 209 : z_log_msg_finalize(msg, source, desc, data);
447 : : }
448 : : }
449 : : }
450 : : EXPORT_SYMBOL(z_log_msg_runtime_vcreate);
451 : :
452 : 209 : int16_t log_msg_get_source_id(struct log_msg *msg)
453 : : {
454 [ - + ]: 209 : if (!z_log_is_local_domain(log_msg_get_domain(msg))) {
455 : : /* Remote domain is converting source pointer to ID */
456 : 0 : return (int16_t)(uintptr_t)log_msg_get_source(msg);
457 : : }
458 : :
459 : 209 : void *source = (void *)log_msg_get_source(msg);
460 : :
461 [ - + ]: 209 : if (source != NULL) {
462 : 0 : return log_source_id(source);
463 : : }
464 : :
465 : : return -1;
466 : : }
|